Ben,
Thanks for getting in touch and working with me to complete an example plugin that generates the URLs you need and the video thumbnail images. I am adding a new example plugin, /examples/buddypress-hooks-example.php.txt
, to my next MLA version. I hope you received the final copy of the plugin I sent by e-mail.
Here is the new code for the filters that implement the two new features:
public static function mla_gallery_wp_query_object_action( $query_arguments ) {
//error_log( 'MLABuddyPressHooksExample::mla_gallery_wp_query_object_action $query_arguments = ' . var_export( $query_arguments, true ), 0 );
self::$wp_query_properties = array();
self::$wp_query_properties ['post_count'] = MLAShortcodes::$mla_gallery_wp_query_object->post_count;
if ( empty( self::$shortcode_attributes['buddypress_urls'] ) ) {
return; // Don't need custom URLs
}
if ( 0 == self::$wp_query_properties ['post_count'] ) {
return; // Empty gallery - nothing to do
}
global $wpdb;
// Assemble the WordPress attachment IDs
$post_info = array();
foreach( MLAShortcodes::$mla_gallery_wp_query_object->posts as $value ) {
$post_info[ $value->ID ] = $value->ID;
}
// Build an array of SQL clauses, then run the query
$query = array();
$query_parameters = array();
$query[] = "SELECT rtm.id, rtm.media_id, rtm.media_author, rtm.cover_art, u.user_nicename FROM {$wpdb->prefix}rt_rtm_media AS rtm";
$query[] = "LEFT JOIN {$wpdb->users} as u";
$query[] = "ON (rtm.media_author = u.ID)";
$placeholders = array();
foreach ( $post_info as $value ) {
$placeholders[] = '%s';
$query_parameters[] = $value;
}
$query[] = 'WHERE ( rtm.media_id IN (' . join( ',', $placeholders ) . ') )';
$query = join(' ', $query);
$results = $wpdb->get_results( $wpdb->prepare( $query, $query_parameters ) );
// Save the values, indexed by WordPress attachment ID, for use in the item filter
$post_info = array();
if ( is_array( $results ) ) {
foreach ( $results as $value ) {
$post_info[ $value->media_id ] = $value;
}
}
self::$wp_query_properties ['post_info'] = $post_info;
/*
* Unlike Filters, Actions never return anything
*/
return;
} // mla_gallery_wp_query_object_action
/**
* MLA Gallery Item Values
*
* @since 1.00
*
* @param array parameter_name => parameter_value pairs
*
* @return array updated substitution parameter name => value pairs
*/
public static function mla_gallery_item_values_filter( $item_values ) {
//error_log( 'MLABuddyPressHooksExample::mla_gallery_item_values_filter $item_values = ' . var_export( $item_values, true ), 0 );
/*
* We use a shortcode parameter of our own to apply our filters on a gallery-by-gallery basis,
* leaving other [mla_gallery] instances untouched. If the "my_filter" parameter is not present,
* we have nothing to do.
*/
if ( ! isset( self::$shortcode_attributes['buddypress_urls'] ) ) {
return $item_values; // leave them unchanged
}
$use_cover_art = 'cover' == strtolower( trim( self::$shortcode_attributes['buddypress_urls'] ) );
if ( isset( self::$wp_query_properties ['post_info'][ $item_values['attachment_ID'] ] ) ) {
$post_info = self::$wp_query_properties ['post_info'][ $item_values['attachment_ID'] ];
} else {
return $item_values; // no matching rtMedia item
}
$new_url = $item_values['site_url'] . '/members/' . $post_info->user_nicename . '/media/' . $post_info->id . '/';
$new_link = str_replace( $item_values['link_url'], $new_url, $item_values['link'] );
// Add the "media thumbnail", if desired and present. Note that the size is fixed at 150x150 pixels.
if ( $use_cover_art && ! empty( $post_info->cover_art ) ) {
$new_thumbnail = '<img width="150" height="150" src="' . $post_info->cover_art . '" class="attachment-thumbnail" alt="' . $item_values['thumbnail_content'] . '" />';
$new_link = str_replace( $item_values['thumbnail_content'] . '</a>', $new_thumbnail . '</a>', $new_link );
$item_values['thumbnail_content'] = $new_thumbnail;
$item_values['thumbnail_width'] = '150';
$item_values['thumbnail_height'] = '150';
$item_values['thumbnail_url'] = $post_info->cover_art;
}
$item_values['link_url'] = $new_url;
$item_values['link'] = $new_link;
return $item_values;
} // mla_gallery_item_values_filter
Please let me know if you have any problems or further questions about the work. Thanks for an interesting question and for working with me on a solution.
Thanks especially for the donation you made in support of Fair Trade Judaica’s work!