Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
} );
} );
@ramilbatoy

This comment has been minimized.

Copy link

@ramilbatoy ramilbatoy commented Jan 8, 2021

Is it possible to use SearchWP\Source to point to a specific media folder or directory using custom engine and has all media attributes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment