| 1 | <?php |
| 2 | /** |
| 3 | * Functions related to embedding single activity items externally. |
| 4 | * |
| 5 | * Relies on WordPress 4.4. |
| 6 | * |
| 7 | * @since 2.5.0 |
| 8 | * |
| 9 | * @package BuddyPress |
| 10 | * @subpackage ActivityEmbeds |
| 11 | */ |
| 12 | |
| 13 | // Exit if accessed directly. |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Register REST route for embedding single activity items. |
| 18 | * |
| 19 | * Almost exactly the same as WP_oEmbed_Controller::register_routes(), but |
| 20 | * with our '/activity' slug appended to the endpoint. |
| 21 | * |
| 22 | * @since 2.5.0 |
| 23 | */ |
| 24 | function bp_activity_embed_register_rest_route() { |
| 25 | /** |
| 26 | * Filter the maxwidth oEmbed parameter. |
| 27 | * |
| 28 | * @since 4.4.0 |
| 29 | * |
| 30 | * @param int $maxwidth Maximum allowed width. Default 600. |
| 31 | */ |
| 32 | $maxwidth = apply_filters( 'oembed_default_width', 600 ); |
| 33 | |
| 34 | register_rest_route( 'oembed/1.0', '/embed/activity', array( |
| 35 | array( |
| 36 | 'methods' => WP_REST_Server::READABLE, |
| 37 | 'callback' => 'bp_activity_embed_rest_route_callback', |
| 38 | 'args' => array( |
| 39 | 'url' => array( |
| 40 | 'required' => true, |
| 41 | 'sanitize_callback' => 'esc_url_raw', |
| 42 | ), |
| 43 | 'format' => array( |
| 44 | 'default' => 'json', |
| 45 | 'sanitize_callback' => 'wp_oembed_ensure_format', |
| 46 | ), |
| 47 | 'maxwidth' => array( |
| 48 | 'default' => $maxwidth, |
| 49 | 'sanitize_callback' => 'absint', |
| 50 | ), |
| 51 | ), |
| 52 | ), |
| 53 | ) ); |
| 54 | |
| 55 | } |
| 56 | add_action( 'rest_api_init', 'bp_activity_embed_register_rest_route' ); |
| 57 | |
| 58 | /** |
| 59 | * Callback for the API endpoint. |
| 60 | * |
| 61 | * Returns the JSON object for the post. |
| 62 | * |
| 63 | * @since 2.5.0 |
| 64 | * |
| 65 | * @param WP_REST_Request $request Full data about the request. |
| 66 | * @return WP_Error|array oEmbed response data or WP_Error on failure. |
| 67 | */ |
| 68 | function bp_activity_embed_rest_route_callback( $request ) { |
| 69 | $url = $request['url']; |
| 70 | |
| 71 | $invalid = $data = false; |
| 72 | |
| 73 | if ( bp_core_enable_root_profiles() ) { |
| 74 | $domain = bp_get_root_domain(); |
| 75 | } else { |
| 76 | $domain = bp_get_members_directory_permalink(); |
| 77 | } |
| 78 | |
| 79 | // Check the URL to see if this is a single activity URL. |
| 80 | if ( 0 !== strpos( $url, $domain ) ) { |
| 81 | $invalid = true; |
| 82 | } |
| 83 | |
| 84 | // Check for activity slug. |
| 85 | if ( false === strpos( $url, '/' . bp_get_activity_slug() . '/' ) ) { |
| 86 | $invalid = true; |
| 87 | } |
| 88 | |
| 89 | // Do more checks. |
| 90 | if ( false === $invalid ) { |
| 91 | $url = trim( untrailingslashit( $url ) ); |
| 92 | |
| 93 | // Grab the activity ID. |
| 94 | $activity_id = (int) substr( |
| 95 | $url, |
| 96 | strrpos( $url, '/' ) + 1 |
| 97 | ); |
| 98 | |
| 99 | if ( ! empty( $activity_id ) ) { |
| 100 | // Check if activity item still exists. |
| 101 | $activity = new BP_Activity_Activity( $activity_id ); |
| 102 | |
| 103 | // Okay, we're good to go! |
| 104 | if ( ! empty( $activity->component ) ) { |
| 105 | // Create dummy post to piggyback off of get_oembed_response_data() |
| 106 | $post = bp_theme_compat_create_dummy_post( array( |
| 107 | 'post_author' => $activity->user_id, |
| 108 | 'post_title' => __( 'Activity', 'buddypress' ), |
| 109 | 'post_content' => $activity->content, |
| 110 | |
| 111 | // This passes the get_oembed_response_data() check. |
| 112 | 'post_status' => 'publish' |
| 113 | ) ); |
| 114 | |
| 115 | // Add markers to tell that we're embedding a single activity. |
| 116 | // This needed for various oEmbed response data filtering. |
| 117 | buddypress()->activity->embedurl_in_progress = $url; |
| 118 | buddypress()->activity->embedid_in_progress = $activity_id; |
| 119 | |
| 120 | // Use WP's oEmbed response data function. |
| 121 | $data = get_oembed_response_data( $post, $request['maxwidth'] ); |
| 122 | |
| 123 | // Make one change here to reference our BP user profile |
| 124 | $data['author_url'] = bp_core_get_user_domain( $activity->user_id ); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if ( ! $data ) { |
| 130 | return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) ); |
| 131 | } |
| 132 | |
| 133 | return $data; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Register oEmbed provider on BuddyPress pages. |
| 138 | * |
| 139 | * This allows single activity items to be embedded. WP's TinyMCE doesn't |
| 140 | * require this somehow, which is why we only do this on BuddyPress pages. |
| 141 | * |
| 142 | * @since 2.5.0 |
| 143 | * |
| 144 | * @todo Fix up issues with javascript not firing to hide the fallback <blockquote> |
| 145 | */ |
| 146 | function bp_activity_embed_add_oembed_provider() { |
| 147 | // Only register our provider on BuddyPress pages. |
| 148 | if ( false === is_buddypress() ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | if ( bp_core_enable_root_profiles() ) { |
| 153 | $domain = bp_get_root_domain(); |
| 154 | } else { |
| 155 | $domain = bp_get_members_directory_permalink(); |
| 156 | } |
| 157 | |
| 158 | add_filter( 'rest_url' , 'bp_activity_embed_filter_rest_url' ); |
| 159 | wp_oembed_add_provider( $domain . '*/activity/*', get_oembed_endpoint_url() ); |
| 160 | remove_filter( 'rest_url' , 'bp_activity_embed_filter_rest_url' ); |
| 161 | } |
| 162 | add_action( 'bp_init', 'bp_activity_embed_add_oembed_provider' ); |
| 163 | |
| 164 | /** |
| 165 | * Use our custom embed template for activity items. |
| 166 | * |
| 167 | * @since 2.5.0 |
| 168 | * |
| 169 | * @param string $retval Current embed template |
| 170 | * @return string |
| 171 | */ |
| 172 | function bp_activity_embed_filter_template( $retval ) { |
| 173 | if ( ! bp_is_single_activity() ) { |
| 174 | return $retval; |
| 175 | } |
| 176 | |
| 177 | // Embed template hierarchy! |
| 178 | return bp_locate_template( array( |
| 179 | 'embeds/template-single-activity.php', |
| 180 | 'embeds/template.php' |
| 181 | ) ); |
| 182 | } |
| 183 | add_filter( 'embed_template', 'bp_activity_embed_filter_template' ); |
| 184 | |
| 185 | /** |
| 186 | * Inject activity content into the embed template. |
| 187 | * |
| 188 | * @since 2.5.0 |
| 189 | */ |
| 190 | function bp_activity_embed_inject_content() { |
| 191 | if ( ! bp_is_single_activity() ) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | bp_get_template_part( 'embeds/activity' ); |
| 196 | } |
| 197 | add_action( 'embed_content', 'bp_activity_embed_inject_content' ); |
| 198 | |
| 199 | /** |
| 200 | * Adds oEmbed discovery links on single activity pages. |
| 201 | * |
| 202 | * @since 2.5.0 |
| 203 | * |
| 204 | * @param string $retval Current discovery links. |
| 205 | * @return string |
| 206 | */ |
| 207 | function bp_activity_embed_oembed_discovery_links( $retval ) { |
| 208 | if ( ! bp_is_single_activity() ) { |
| 209 | return $retval; |
| 210 | } |
| 211 | |
| 212 | $permalink = bp_displayed_user_domain() . bp_get_activity_slug() . '/' . bp_current_action() . '/'; |
| 213 | |
| 214 | add_filter( 'rest_url' , 'bp_activity_embed_filter_rest_url' ); |
| 215 | |
| 216 | $retval = '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( $permalink ) ) . '" />' . "\n"; |
| 217 | |
| 218 | if ( class_exists( 'SimpleXMLElement' ) ) { |
| 219 | $retval .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( $permalink, 'xml' ) ) . '" />' . "\n"; |
| 220 | } |
| 221 | |
| 222 | remove_filter( 'rest_url' , 'bp_activity_embed_filter_rest_url' ); |
| 223 | |
| 224 | return $retval; |
| 225 | } |
| 226 | add_filter( 'oembed_discovery_links', 'bp_activity_embed_oembed_discovery_links' ); |
| 227 | |
| 228 | /** |
| 229 | * Pass our BuddyPress activity permalink for embedding. |
| 230 | * |
| 231 | * @since 2.5.0 |
| 232 | * |
| 233 | * @see bp_activity_embed_rest_route_callback() |
| 234 | * |
| 235 | * @param string $retval Current embed URL |
| 236 | * @return string |
| 237 | */ |
| 238 | function bp_activity_embed_filter_post_embed_url( $retval ) { |
| 239 | if ( false === isset( buddypress()->activity->embedurl_in_progress ) && ! bp_is_single_activity() ) { |
| 240 | return $retval; |
| 241 | } |
| 242 | |
| 243 | $url = bp_is_single_activity() ? bp_displayed_user_domain() . bp_get_activity_slug() . '/' . bp_current_action() . '/' : buddypress()->activity->embedurl_in_progress; |
| 244 | $url = trailingslashit( $url ); |
| 245 | |
| 246 | // This is for the 'WordPress Embed' block |
| 247 | // @see bp_activity_embed_comments_button() |
| 248 | if ( 'the_permalink' !== current_filter() ) { |
| 249 | $url = add_query_arg( 'embed', 'true', trailingslashit( $url ) ); |
| 250 | } |
| 251 | |
| 252 | return $url; |
| 253 | } |
| 254 | add_filter( 'post_embed_url', 'bp_activity_embed_filter_post_embed_url' ); |
| 255 | |
| 256 | /** |
| 257 | * Filters the embed HTML for the default oEmbed fallback HTML. |
| 258 | * |
| 259 | * @since 2.5.0 |
| 260 | * |
| 261 | * @param string $retval Current embed HTML |
| 262 | * @return string |
| 263 | */ |
| 264 | function bp_activity_embed_filter_html( $retval ) { |
| 265 | if ( false === isset( buddypress()->activity->embedurl_in_progress ) && ! bp_is_single_activity() ) { |
| 266 | return $retval; |
| 267 | } |
| 268 | |
| 269 | // Change 'Embedded WordPress Post' to 'Embedded Activity Item' |
| 270 | $retval = str_replace( __( 'Embedded WordPress Post' ), __( 'Embedded Activity Item', 'buddypress' ), $retval ); |
| 271 | |
| 272 | // Remove default <blockquote> |
| 273 | $retval = substr( $retval, strpos( $retval, '</blockquote>' ) + 13 ); |
| 274 | |
| 275 | // Set up new <blockquote> |
| 276 | $activity_id = bp_is_single_activity() ? bp_current_action() : buddypress()->activity->embedid_in_progress; |
| 277 | $activity = new BP_Activity_Activity( $activity_id ); |
| 278 | $mentionname = bp_activity_do_mentions() ? ' (@' . bp_activity_get_user_mentionname( $activity->user_id ) . ')' : ''; |
| 279 | $date = date_i18n( get_option( 'date_format' ), strtotime( $activity->date_recorded ) ); |
| 280 | |
| 281 | // 'wp-embedded-content' CSS class is necessary due to how the embed JS works. |
| 282 | $blockquote = sprintf( '<blockquote class="wp-embedded-content bp-activity-item">%1$s%2$s %3$s</blockquote>', |
| 283 | apply_filters( 'bp_get_activity_content_body', $activity->content ), |
| 284 | '- ' . bp_core_get_user_displayname( $activity->user_id ) . $mentionname, |
| 285 | '<a href="' . esc_url( bp_activity_get_permalink( $activity_id ) ) . '">' . $date . '</a>' |
| 286 | ); |
| 287 | |
| 288 | /** |
| 289 | * Filters the fallback HTML used when embedding a BP activity item. |
| 290 | * |
| 291 | * @since 2.5.0 |
| 292 | * |
| 293 | * @param string $blockquote Current fallback HTML |
| 294 | * @param BP_Activity_Activity $activity Activity object |
| 295 | */ |
| 296 | $blockquote = apply_filters( 'bp_activity_embed_fallback_html', $blockquote, $activity ); |
| 297 | |
| 298 | // Add our custom <blockquote> |
| 299 | return $blockquote . $retval; |
| 300 | } |
| 301 | add_filter( 'embed_html', 'bp_activity_embed_filter_html' ); |
| 302 | |
| 303 | /** |
| 304 | * Prints the markup for the activity embed comments button. |
| 305 | * |
| 306 | * @since 2.5.0 |
| 307 | */ |
| 308 | function bp_activity_embed_comments_button() { |
| 309 | if ( ! bp_is_single_activity() ) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | // Make sure our custom permalink shows up in the 'WordPress Embed' block. |
| 314 | add_filter( 'the_permalink', 'bp_activity_embed_filter_post_embed_url' ); |
| 315 | |
| 316 | // Only show comment bubble if we have some activity comments. |
| 317 | $count = bp_activity_get_comment_count(); |
| 318 | if ( empty( $count ) ) { |
| 319 | return; |
| 320 | } |
| 321 | ?> |
| 322 | |
| 323 | <div class="wp-embed-comments"> |
| 324 | <a href="<?php bp_activity_thread_permalink(); ?>"> |
| 325 | <span class="dashicons dashicons-admin-comments"></span> |
| 326 | <?php |
| 327 | printf( |
| 328 | _n( |
| 329 | '%s <span class="screen-reader-text">Comment</span>', |
| 330 | '%s <span class="screen-reader-text">Comments</span>', |
| 331 | $count |
| 332 | ), |
| 333 | number_format_i18n( $count ) |
| 334 | ); |
| 335 | ?> |
| 336 | </a> |
| 337 | </div> |
| 338 | |
| 339 | <?php |
| 340 | } |
| 341 | add_action( 'embed_content_meta', 'bp_activity_embed_comments_button', 5 ); |
| 342 | |
| 343 | /** |
| 344 | * Append '/activity' to oEmbed endpoint URL. |
| 345 | * |
| 346 | * Meant to be used as a filter on 'rest_url' before any call to |
| 347 | * {@link get_oembed_endpoint_url()} is used. |
| 348 | * |
| 349 | * @since 2.5.0 |
| 350 | * |
| 351 | * @see bp_activity_embed_oembed_discovery_links() |
| 352 | * @see bp_activity_embed_add_oembed_provider() |
| 353 | * |
| 354 | * @param string $retval Current oEmbed endpoint URL |
| 355 | * @return string |
| 356 | */ |
| 357 | function bp_activity_embed_filter_rest_url( $retval = '' ) { |
| 358 | return $retval . '/activity'; |
| 359 | } |
| 360 | No newline at end of file |