| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Google+ embeds |
| 5 |
*/ |
| 6 |
|
| 7 |
define( 'JETPACK_GOOGLEPLUS_EMBED_REGEX', '#^https?://plus\.(sandbox\.)?google\.com/(u/\d+/)?([^/]+)/posts/([^/]+)$#' ); |
| 8 |
|
| 9 |
// Example URL: https://plus.google.com/114986219448604314131/posts/LgHkesWCmJo |
| 10 |
// Alternate example: https://plus.google.com/u/0/100004581596612508203/posts/2UKwN67MBQs (note the /u/0/) |
| 11 |
wp_embed_register_handler( 'googleplus', JETPACK_GOOGLEPLUS_EMBED_REGEX, 'jetpack_googleplus_embed_handler' ); |
| 12 |
|
| 13 |
function jetpack_googleplus_embed_handler( $matches, $attr, $url ) { |
| 14 |
static $did_script; |
| 15 |
|
| 16 |
if ( ! $did_script ) { |
| 17 |
$did_script = true; |
| 18 |
add_action( 'wp_footer', 'jetpack_googleplus_add_script' ); |
| 19 |
} |
| 20 |
|
| 21 |
return sprintf( '<div class="g-post" data-href="%s"></div>', esc_url( $url ) ); |
| 22 |
} |
| 23 |
|
| 24 |
function jetpack_googleplus_add_script() { |
| 25 |
?> |
| 26 |
<script src="https://apis.google.com/js/plusone.js"></script> |
| 27 |
<?php |
| 28 |
} |
| 29 |
|
| 30 |
add_shortcode( 'googleplus', 'jetpack_googleplus_shortcode_handler' ); |
| 31 |
|
| 32 |
function jetpack_googleplus_shortcode_handler( $atts ) { |
| 33 |
global $wp_embed; |
| 34 |
|
| 35 |
if ( empty( $atts['url'] ) ) |
| 36 |
return; |
| 37 |
|
| 38 |
if ( ! preg_match( JETPACK_GOOGLEPLUS_EMBED_REGEX, $atts['url'] ) ) |
| 39 |
return; |
| 40 |
|
| 41 |
return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 42 |
} |
| 43 |
|