Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Created July 6, 2022 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save searchwpgists/620b54aa7119aa44a1b8d10224ced706 to your computer and use it in GitHub Desktop.
Save searchwpgists/620b54aa7119aa44a1b8d10224ced706 to your computer and use it in GitHub Desktop.
Custom SearchWP Source for BuddyPress Activity
<?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