shortcodes.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Module Name: Shortcode Embeds
  4. * Module Description: Embed media from popular sites without any coding.
  5. * Sort Order: 3
  6. * First Introduced: 1.1
  7. * Major Changes In: 1.2
  8. * Requires Connection: No
  9. * Auto Activate: Yes
  10. * Module Tags: Photos and Videos, Social, Writing, Appearance
  11. * Feature: Writing
  12. * Additional Search Queries: shortcodes, shortcode, embeds, media, bandcamp, dailymotion, facebook, flickr, google calendars, google maps, google+, polldaddy, recipe, recipes, scribd, slideshare, slideshow, slideshows, soundcloud, ted, twitter, vimeo, vine, youtube
  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 Array of old shortcode parameters.
  22. * @param bool $old_format_support true if [shortcode=foo] format is possible.
  23. *
  24. * @return string $params
  25. */
  26. function shortcode_new_to_old_params( $params, $old_format_support = false ) {
  27. $str = '';
  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( '&amp;', '&#038;' ), '&', $str );
  38. }
  39. /**
  40. * Load all available Jetpack shortcode files.
  41. */
  42. function jetpack_load_shortcodes() {
  43. $shortcode_includes = array();
  44. foreach ( Jetpack::glob_php( dirname( __FILE__ ) . '/shortcodes' ) as $file ) {
  45. $filename = substr( basename( $file ), 0, -4 );
  46. $shortcode_includes[ $filename ] = $file;
  47. }
  48. /**
  49. * This filter allows other plugins to override which shortcodes Jetpack loads.
  50. *
  51. * Fires as part of the `plugins_loaded` WP hook, so modifying code needs to be in a plugin, not in a theme's functions.php.
  52. *
  53. * @module shortcodes
  54. *
  55. * @since 2.2.1
  56. * @since 4.2.0 Added filename without extension as array key.
  57. *
  58. * @param array $shortcode_includes An array of which shortcodes to include.
  59. */
  60. $shortcode_includes = apply_filters( 'jetpack_shortcodes_to_include', $shortcode_includes );
  61. foreach ( $shortcode_includes as $include ) {
  62. include_once $include;
  63. }
  64. }
  65. /**
  66. * Runs preg_replace so that replacements don't happen within open tags.
  67. * Parameters are the same as preg_replace, with an added optional search param for improved performance
  68. *
  69. * @param string $pattern Pattern to search for.
  70. * @param string $replacement String to replace.
  71. * @param string $content Post content.
  72. * @param string $search String to search for.
  73. *
  74. * @return string $content Replaced post content.
  75. */
  76. function jetpack_preg_replace_outside_tags( $pattern, $replacement, $content, $search = null ) {
  77. if ( ! function_exists( 'wp_html_split' ) ) {
  78. return $content;
  79. }
  80. if ( $search && false === strpos( $content, $search ) ) {
  81. return $content;
  82. }
  83. $textarr = wp_html_split( $content );
  84. unset( $content );
  85. foreach ( $textarr as &$element ) {
  86. if ( '' === $element || '<' === $element{0} ) {
  87. continue;
  88. }
  89. $element = preg_replace( $pattern, $replacement, $element );
  90. }
  91. return join( $textarr );
  92. }
  93. /**
  94. * Runs preg_replace_callback so that replacements don't happen within open tags.
  95. * Parameters are the same as preg_replace, with an added optional search param for improved performance.
  96. *
  97. * @param string $pattern Pattern to search for.
  98. * @param string $callback Callback returning the replacement string.
  99. * @param string $content Post content.
  100. * @param string $search String to search for.
  101. *
  102. * @return string $content Replaced post content.
  103. */
  104. function jetpack_preg_replace_callback_outside_tags( $pattern, $callback, $content, $search = null ) {
  105. if ( ! function_exists( 'wp_html_split' ) ) {
  106. return $content;
  107. }
  108. if ( $search && false === strpos( $content, $search ) ) {
  109. return $content;
  110. }
  111. $textarr = wp_html_split( $content );
  112. unset( $content );
  113. foreach ( $textarr as &$element ) {
  114. if ( '' === $element || '<' === $element{0} ) {
  115. continue;
  116. }
  117. $element = preg_replace_callback( $pattern, $callback, $element );
  118. }
  119. return join( $textarr );
  120. }
  121. if ( ! function_exists( 'jetpack_shortcode_get_wpvideo_id' ) ) {
  122. /**
  123. * Get VideoPress ID from wpvideo shortcode attributes.
  124. *
  125. * @param array $atts Shortcode attributes.
  126. * @return int $id VideoPress ID.
  127. */
  128. function jetpack_shortcode_get_wpvideo_id( $atts ) {
  129. if ( isset( $atts[0] ) ) {
  130. return $atts[0];
  131. } else {
  132. return 0;
  133. }
  134. }
  135. }
  136. if ( ! function_exists( 'jetpack_shortcode_get_videopress_id' ) ) {
  137. /**
  138. * Get VideoPress ID from videopress shortcode attributes.
  139. *
  140. * @param array $atts Shortcode attributes.
  141. * @return int $id VideoPress ID.
  142. */
  143. function jetpack_shortcode_get_videopress_id( $atts ) {
  144. if ( isset( $atts[0] ) ) {
  145. return $atts[0];
  146. } else {
  147. return 0;
  148. }
  149. }
  150. }
  151. /**
  152. * Common element attributes parsing and sanitizing for src, width and height.
  153. *
  154. * @since 4.5.0
  155. *
  156. * @param array $attrs With original values.
  157. *
  158. * @return array $attrs With sanitized values.
  159. */
  160. function wpcom_shortcodereverse_parseattr( $attrs ) {
  161. $defaults = array(
  162. 'src' => false,
  163. 'width' => false,
  164. 'height' => false,
  165. );
  166. $attrs = shortcode_atts( $defaults, $attrs );
  167. $attrs['src'] = strip_tags( $attrs['src'] ); // For sanity
  168. $attrs['width'] = ( is_numeric( $attrs['width'] ) ) ? abs( intval( $attrs['width'] ) ) : $defaults['width'];
  169. $attrs['height'] = ( is_numeric( $attrs['height'] ) ) ? abs( intval( $attrs['height'] ) ) : $defaults['height'];
  170. return $attrs;
  171. }
  172. jetpack_load_shortcodes();