Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created March 27, 2020 14:08
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 jchristopher/7f5d249fddaa6c09d5792879529398c2 to your computer and use it in GitHub Desktop.
Save jchristopher/7f5d249fddaa6c09d5792879529398c2 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;
} );
} );
@ramilbatoy
Copy link

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