| 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 oEmbed provider on BuddyPress pages. |
| 18 | * |
| 19 | * This allows single activity items to be embedded. WP's TinyMCE doesn't |
| 20 | * require this somehow, which is why we only do this on BuddyPress pages. |
| 21 | * |
| 22 | * @since 2.5.0 |
| 23 | * |
| 24 | * @todo Fix up issues with javascript not firing to hide the fallback <blockquote> |
| 25 | */ |
| 26 | function bp_activity_embed_add_oembed_provider() { |
| 27 | // Only register our provider on BuddyPress pages. |
| 28 | if ( false === is_buddypress() ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if ( bp_core_enable_root_profiles() ) { |
| 33 | $domain = bp_get_root_domain(); |
| 34 | } else { |
| 35 | $domain = bp_get_members_directory_permalink(); |
| 36 | } |
| 37 | |
| 38 | wp_oembed_add_provider( $domain . '*/activity/*', get_oembed_endpoint_url() ); |
| 39 | } |
| 40 | add_action( 'bp_init', 'bp_activity_embed_add_oembed_provider' ); |
| 41 | |
| 42 | /** |
| 43 | * Use our custom embed template for activity items. |
| 44 | * |
| 45 | * @since 2.5.0 |
| 46 | * |
| 47 | * @param string $retval Current embed template |
| 48 | * @return string |
| 49 | */ |
| 50 | function bp_activity_embed_filter_template( $retval ) { |
| 51 | if ( ! bp_is_single_activity() ) { |
| 52 | return $retval; |
| 53 | } |
| 54 | |
| 55 | // Embed template hierarchy! |
| 56 | return bp_locate_template( array( |
| 57 | 'embeds/template-single-activity.php', |
| 58 | 'embeds/template.php' |
| 59 | ) ); |
| 60 | } |
| 61 | add_filter( 'embed_template', 'bp_activity_embed_filter_template' ); |
| 62 | |
| 63 | /** |
| 64 | * Inject activity content into the embed template. |
| 65 | * |
| 66 | * @since 2.5.0 |
| 67 | */ |
| 68 | function bp_activity_embed_inject_content() { |
| 69 | if ( ! bp_is_single_activity() ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | bp_get_template_part( 'embeds/activity' ); |
| 74 | } |
| 75 | add_action( 'embed_content', 'bp_activity_embed_inject_content' ); |
| 76 | |
| 77 | /** |
| 78 | * Adds oEmbed discovery links on single activity pages. |
| 79 | * |
| 80 | * @since 2.5.0 |
| 81 | * |
| 82 | * @param string $retval Current discovery links. |
| 83 | * @return string |
| 84 | */ |
| 85 | function bp_activity_embed_oembed_discovery_links( $retval ) { |
| 86 | if ( ! bp_is_single_activity() ) { |
| 87 | return $retval; |
| 88 | } |
| 89 | |
| 90 | $permalink = bp_displayed_user_domain() . bp_get_activity_slug() . '/' . bp_current_action() . '/'; |
| 91 | |
| 92 | $retval = '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( $permalink ) ) . '" />' . "\n"; |
| 93 | |
| 94 | if ( class_exists( 'SimpleXMLElement' ) ) { |
| 95 | $retval .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( $permalink, 'xml' ) ) . '" />' . "\n"; |
| 96 | } |
| 97 | |
| 98 | return $retval; |
| 99 | } |
| 100 | add_filter( 'oembed_discovery_links', 'bp_activity_embed_oembed_discovery_links' ); |
| 101 | |
| 102 | /** |
| 103 | * Filter the oEmbed post ID. |
| 104 | * |
| 105 | * WP needs a real WordPress post otherwise oEmbed will fail. So here, we |
| 106 | * pass the Members Directory page ID as a dummy, while adding some markers |
| 107 | * that we will use to filter other parts of oEmbed so activity embedding will |
| 108 | * work. |
| 109 | * |
| 110 | * @since 2.5.0 |
| 111 | * |
| 112 | * @param int $retval Current post ID |
| 113 | * @param string $url Current embed URL |
| 114 | * @return int |
| 115 | */ |
| 116 | function bp_activity_embed_filter_oembed_request_post_id( $retval, $url ) { |
| 117 | // Check the URL to see if this is a single activity URL. |
| 118 | // @todo Do checks for BP root domain |
| 119 | if ( 0 !== strpos( $url, bp_get_members_directory_permalink() ) ) { |
| 120 | return $retval; |
| 121 | } |
| 122 | |
| 123 | // Check for activity slug |
| 124 | if ( false === strpos( $url, '/' . bp_get_activity_slug() . '/' ) ) { |
| 125 | return $retval; |
| 126 | } |
| 127 | |
| 128 | $url = trim( untrailingslashit( $url ) ); |
| 129 | |
| 130 | // Grab the activity ID |
| 131 | $activity_id = (int) substr( |
| 132 | $url, |
| 133 | strrpos( $url, '/' ) + 1 |
| 134 | ); |
| 135 | if ( empty( $activity_id ) ) { |
| 136 | return $retval; |
| 137 | } |
| 138 | |
| 139 | // Check if activity item still exists. |
| 140 | $activity = new BP_Activity_Activity( $activity_id ); |
| 141 | if ( empty( $activity->component ) ) { |
| 142 | return $retval; |
| 143 | } |
| 144 | |
| 145 | // Add markers to tell that we're embedding a single activity |
| 146 | buddypress()->activity->embedurl_in_progress = $url; |
| 147 | buddypress()->activity->embedid_in_progress = $activity_id; |
| 148 | |
| 149 | // Bypass explicit post check by passing members directory page ID |
| 150 | $page_ids = bp_core_get_directory_page_ids(); |
| 151 | return $page_ids['members']; |
| 152 | } |
| 153 | add_filter( 'oembed_request_post_id', 'bp_activity_embed_filter_oembed_request_post_id', 10, 2 ); |
| 154 | |
| 155 | /** |
| 156 | * Pass our BuddyPress activity permalink for embedding. |
| 157 | * |
| 158 | * @since 2.5.0 |
| 159 | * |
| 160 | * @see bp_activity_oembed_request_post_id() |
| 161 | * |
| 162 | * @param string $retval Current embed URL |
| 163 | * @return string |
| 164 | */ |
| 165 | function bp_activity_embed_filter_post_embed_url( $retval ) { |
| 166 | if ( false === isset( buddypress()->activity->embedurl_in_progress ) && ! bp_is_single_activity() ) { |
| 167 | return $retval; |
| 168 | } |
| 169 | |
| 170 | $url = bp_is_single_activity() ? bp_displayed_user_domain() . bp_get_activity_slug() . '/' . bp_current_action() . '/' : buddypress()->activity->embedurl_in_progress; |
| 171 | $url = trailingslashit( $url ); |
| 172 | |
| 173 | // This is for the 'WordPress Embed' block |
| 174 | // @see bp_activity_embed_comments_button() |
| 175 | if ( 'the_permalink' !== current_filter() ) { |
| 176 | $url = add_query_arg( 'embed', 'true', trailingslashit( $url ) ); |
| 177 | } |
| 178 | |
| 179 | return $url; |
| 180 | } |
| 181 | add_filter( 'post_embed_url', 'bp_activity_embed_filter_post_embed_url' ); |
| 182 | |
| 183 | /** |
| 184 | * Filter the oEmbed response data to reference our activity content. |
| 185 | * |
| 186 | * @since 2.5.0 |
| 187 | * |
| 188 | * @param array $retval Current response data |
| 189 | * @return array |
| 190 | */ |
| 191 | function bp_activity_embed_filter_oembed_response_data( $retval ) { |
| 192 | if ( false === isset( buddypress()->activity->embedurl_in_progress ) ) { |
| 193 | return $retval; |
| 194 | } |
| 195 | |
| 196 | $activity = new BP_Activity_Activity( buddypress()->activity->embedid_in_progress ); |
| 197 | |
| 198 | $retval['title'] = __( 'Activity', 'buddypress' ); |
| 199 | $retval['author_url'] = bp_core_get_user_domain( $activity->user_id ); |
| 200 | |
| 201 | return $retval; |
| 202 | } |
| 203 | add_filter( 'oembed_response_data', 'bp_activity_embed_filter_oembed_response_data', 20 ); |
| 204 | |
| 205 | /** |
| 206 | * Filter the embed HTML for default oEmbed fallback HTML. |
| 207 | * |
| 208 | * @param string $retval Current embed HTML |
| 209 | * @return string |
| 210 | */ |
| 211 | function bp_activity_embed_filter_html( $retval ) { |
| 212 | if ( false === isset( buddypress()->activity->embedurl_in_progress ) && ! bp_is_single_activity() ) { |
| 213 | return $retval; |
| 214 | } |
| 215 | |
| 216 | // Change 'Embedded WordPress Post' to 'Embedded Activity Item' |
| 217 | $retval = str_replace( __( 'Embedded WordPress Post' ), __( 'Embedded Activity Item', 'buddypress' ), $retval ); |
| 218 | |
| 219 | // Remove default <blockquote> |
| 220 | $retval = substr( $retval, strpos( $retval, '</blockquote>' ) + 13 ); |
| 221 | |
| 222 | // Set up new <blockquote> |
| 223 | $activity_id = bp_is_single_activity() ? bp_current_action() : buddypress()->activity->embedid_in_progress; |
| 224 | $activity = new BP_Activity_Activity( $activity_id ); |
| 225 | $mentionname = bp_activity_do_mentions() ? ' (@' . bp_activity_get_user_mentionname( $activity->user_id ) . ')' : ''; |
| 226 | $date = date_i18n( get_option( 'date_format' ), strtotime( $activity->date_recorded ) ); |
| 227 | |
| 228 | // 'wp-embedded-content' CSS class is necessary due to how the embed JS works. |
| 229 | $blockquote = sprintf( '<blockquote class="wp-embedded-content bp-activity-item">%1$s%2$s %3$s</blockquote>', |
| 230 | apply_filters( 'bp_get_activity_content_body', $activity->content ), |
| 231 | '- ' . bp_core_get_user_displayname( $activity->user_id ) . $mentionname, |
| 232 | '<a href="' . esc_url( bp_activity_get_permalink( $activity_id ) ) . '">' . $date . '</a>' |
| 233 | ); |
| 234 | |
| 235 | /** |
| 236 | * Filters the fallback HTML used when embedding a BP activity item. |
| 237 | * |
| 238 | * @since 2.5.0 |
| 239 | * |
| 240 | * @param string $blockquote Current fallback HTML |
| 241 | * @param BP_Activity_Activity $activity Activity object |
| 242 | */ |
| 243 | $blockquote = apply_filters( 'bp_activity_embed_fallback_html', $blockquote, $activity ); |
| 244 | |
| 245 | // Add our custom <blockquote> |
| 246 | return $blockquote . $retval; |
| 247 | } |
| 248 | add_filter( 'embed_html', 'bp_activity_embed_filter_html' ); |
| 249 | |
| 250 | /** |
| 251 | * Prints the markup for the activity embed comments button. |
| 252 | * |
| 253 | * @since 2.5.0 |
| 254 | */ |
| 255 | function bp_activity_embed_comments_button() { |
| 256 | if ( ! bp_is_single_activity() ) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // Make sure our custom permalink shows up in the 'WordPress Embed' block. |
| 261 | add_filter( 'the_permalink', 'bp_activity_embed_filter_post_embed_url' ); |
| 262 | |
| 263 | // Only show comment bubble if we have some activity comments. |
| 264 | $count = bp_activity_get_comment_count(); |
| 265 | if ( empty( $count ) ) { |
| 266 | return; |
| 267 | } |
| 268 | ?> |
| 269 | |
| 270 | <div class="wp-embed-comments"> |
| 271 | <a href="<?php bp_activity_thread_permalink(); ?>"> |
| 272 | <span class="dashicons dashicons-admin-comments"></span> |
| 273 | <?php |
| 274 | printf( |
| 275 | _n( |
| 276 | '%s <span class="screen-reader-text">Comment</span>', |
| 277 | '%s <span class="screen-reader-text">Comments</span>', |
| 278 | $count |
| 279 | ), |
| 280 | number_format_i18n( $count ) |
| 281 | ); |
| 282 | ?> |
| 283 | </a> |
| 284 | </div> |
| 285 | |
| 286 | <?php |
| 287 | } |
| 288 | add_action( 'embed_content_meta', 'bp_activity_embed_comments_button', 5 ); |
| 289 | No newline at end of file |