-
-
Save searchwpgists/620b54aa7119aa44a1b8d10224ced706 to your computer and use it in GitHub Desktop.
Custom SearchWP Source for BuddyPress Activity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom SearchWP Source for BuddyPress Activity. | |
* NOTE: Requires SearchWP 4 | |
*/ | |
add_action( 'plugins_loaded', function() { | |
if ( ! class_exists( '\SearchWP\Source' ) || ! function_exists( 'bp_activity_get' ) ) { | |
return; | |
} | |
class BuddyPressActivity extends \SearchWP\Source { | |
protected $name = 'buddypressactivity'; | |
protected $db_id_column = 'id'; | |
function __construct() { | |
global $wpdb; | |
$this->db_table = $wpdb->base_prefix . 'bp_activity'; | |
$this->labels = [ | |
'plural' => 'BuddyPress Activity', | |
'singular' => 'BuddyPress Activity', | |
]; | |
$this->attributes = [ [ | |
'name' => 'content', | |
'label' => 'Content', | |
'default' => \SearchWP\Utils::get_min_engine_weight(), | |
'data' => function( $entry_id ) { | |
$content = ''; | |
$entry = bp_activity_get( [ 'in' => [ $entry_id ] ] ); | |
if ( ! empty( $entry['activities'] ) ) { | |
$content = $entry['activities'][0]->content; | |
} | |
return $content; | |
}, | |
], ]; | |
} | |
// Restrict to Activity updates. | |
protected function db_where() { | |
return [ [ | |
'column' => 'component', | |
'value' => 'activity', | |
] ]; | |
} | |
// Return results of bp_activity_get() for this Activity entry. | |
public function entry( \SearchWP\Entry $entry, $query = false ) { | |
return bp_activity_get( [ 'in' => [ $entry->get_id() ] ] ); | |
} | |
} | |
add_filter( 'searchwp\sources', function( $sources ) { | |
$sources[] = new BuddyPressActivity(); | |
return $sources; | |
} ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment