Created
March 27, 2020 14:08
-
-
Save jchristopher/7f5d249fddaa6c09d5792879529398c2 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
Is it possible to use SearchWP\Source to point to a specific media folder or directory using custom engine and has all media attributes?