| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Module Name: Shortcode Embeds |
| 5 |
* Module Description: Embed content from YouTube, Vimeo, SlideShare, and more, no coding necessary. |
| 6 |
* Sort Order: 3 |
| 7 |
* First Introduced: 1.1 |
| 8 |
* Major Changes In: 1.2 |
| 9 |
* Requires Connection: No |
| 10 |
* Auto Activate: Yes |
| 11 |
* Module Tags: Photos and Videos, Social, Writing, Appearance |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
* Transforms the $atts array into a string that the old functions expected |
| 16 |
* |
| 17 |
* The old way was: |
| 18 |
* [shortcode a=1&b=2&c=3] or [shortcode=1] |
| 19 |
* This is parsed as array( a => '1&b=2&c=3' ) and array( 0 => '=1' ), which is useless |
| 20 |
* |
| 21 |
* @param Array $params |
| 22 |
* @param Bool $old_format_support true if [shortcode=foo] format is possible. |
| 23 |
* @return String $params |
| 24 |
*/ |
| 25 |
function shortcode_new_to_old_params( $params, $old_format_support = false ) { |
| 26 |
$str = ''; |
| 27 |
|
| 28 |
if ( $old_format_support && isset( $params[0] ) ) { |
| 29 |
$str = ltrim( $params[0], '=' ); |
| 30 |
} elseif ( is_array( $params ) ) { |
| 31 |
foreach ( array_keys( $params ) as $key ) { |
| 32 |
if ( ! is_numeric( $key ) ) |
| 33 |
$str = $key . '=' . $params[$key]; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return str_replace( array( '&', '&' ), '&', $str ); |
| 38 |
} |
| 39 |
|
| 40 |
function jetpack_load_shortcodes() { |
| 41 |
global $wp_version; |
| 42 |
|
| 43 |
$shortcode_includes = array(); |
| 44 |
|
| 45 |
foreach ( Jetpack::glob_php( dirname( __FILE__ ) . '/shortcodes' ) as $file ) { |
| 46 |
$shortcode_includes[] = $file; |
| 47 |
} |
| 48 |
|
| 49 |
$shortcode_includes = apply_filters( 'jetpack_shortcodes_to_include', $shortcode_includes ); |
| 50 |
|
| 51 |
foreach ( $shortcode_includes as $include ) { |
| 52 |
if ( version_compare( $wp_version, '3.6-z', '>=' ) && stristr( $include, 'audio.php' ) ) |
| 53 |
continue; |
| 54 |
|
| 55 |
include $include; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Runs preg_replace so that replacements don't happen within open tags. |
| 61 |
* Parameters are the same as preg_replace, with an added optional search param for improved performance |
| 62 |
* |
| 63 |
* @param String $pattern |
| 64 |
* @param String $replacement |
| 65 |
* @param String $content |
| 66 |
* @param String $search |
| 67 |
* @return String $content |
| 68 |
*/ |
| 69 |
function jetpack_preg_replace_outside_tags( $pattern, $replacement, $content, $search = null ) { |
| 70 |
if( ! function_exists( 'wp_html_split' ) ) { |
| 71 |
return $content; |
| 72 |
} |
| 73 |
|
| 74 |
if ( $search && false === strpos( $content, $search ) ) { |
| 75 |
return $content; |
| 76 |
} |
| 77 |
|
| 78 |
$textarr = wp_html_split( $content ); |
| 79 |
unset( $content ); |
| 80 |
foreach( $textarr as &$element ) { |
| 81 |
if ( '' === $element || '<' === $element{0} ) |
| 82 |
continue; |
| 83 |
$element = preg_replace( $pattern, $replacement, $element ); |
| 84 |
} |
| 85 |
|
| 86 |
return join( $textarr ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Runs preg_replace_callback so that replacements don't happen within open tags. |
| 91 |
* Parameters are the same as preg_replace, with an added optional search param for improved performance |
| 92 |
* |
| 93 |
* @param String $pattern |
| 94 |
* @param String $replacement |
| 95 |
* @param String $content |
| 96 |
* @param String $search |
| 97 |
* @return String $content |
| 98 |
*/ |
| 99 |
function jetpack_preg_replace_callback_outside_tags( $pattern, $callback, $content, $search = null ) { |
| 100 |
if( ! function_exists( 'wp_html_split' ) ) { |
| 101 |
return $content; |
| 102 |
} |
| 103 |
|
| 104 |
if ( $search && false === strpos( $content, $search ) ) { |
| 105 |
return $content; |
| 106 |
} |
| 107 |
|
| 108 |
$textarr = wp_html_split( $content ); |
| 109 |
unset( $content ); |
| 110 |
foreach( $textarr as &$element ) { |
| 111 |
if ( '' === $element || '<' === $element{0} ) |
| 112 |
continue; |
| 113 |
$element = preg_replace_callback( $pattern, $callback, $element ); |
| 114 |
} |
| 115 |
|
| 116 |
return join( $textarr ); |
| 117 |
} |
| 118 |
|
| 119 |
global $wp_version; |
| 120 |
|
| 121 |
if ( version_compare( $wp_version, '3.6-z', '>=' ) ) { |
| 122 |
add_filter( 'shortcode_atts_audio', 'jetpack_audio_atts_handler', 10, 3 ); |
| 123 |
|
| 124 |
function jetpack_audio_atts_handler( $out, $pairs, $atts ) { |
| 125 |
if( isset( $atts[0] ) ) |
| 126 |
$out['src'] = $atts[0]; |
| 127 |
|
| 128 |
return $out; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
jetpack_load_shortcodes(); |
| 133 |
|