Bez popisu

shortcodes.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Module Name: Shortcode Embeds
  4. * Module Description: Shortcodes are WordPress-specific markup that let you add media from popular sites. This feature is no longer necessary as the editor now handles media embeds rather gracefully.
  5. * Sort Order: 3
  6. * First Introduced: 1.1
  7. * Major Changes In: 1.2
  8. * Requires Connection: No
  9. * Auto Activate: No
  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 ( $search && false === strpos( $content, $search ) ) {
  78. return $content;
  79. }
  80. $textarr = wp_html_split( $content );
  81. unset( $content );
  82. foreach ( $textarr as &$element ) {
  83. if ( '' === $element || '<' === $element[0] ) {
  84. continue;
  85. }
  86. $element = preg_replace( $pattern, $replacement, $element );
  87. }
  88. return join( $textarr );
  89. }
  90. /**
  91. * Runs preg_replace_callback so that replacements don't happen within open tags.
  92. * Parameters are the same as preg_replace, with an added optional search param for improved performance.
  93. *
  94. * @param string $pattern Pattern to search for.
  95. * @param string $callback Callback returning the replacement string.
  96. * @param string $content Post content.
  97. * @param string $search String to search for.
  98. *
  99. * @return string $content Replaced post content.
  100. */
  101. function jetpack_preg_replace_callback_outside_tags( $pattern, $callback, $content, $search = null ) {
  102. if ( $search && false === strpos( $content, $search ) ) {
  103. return $content;
  104. }
  105. $textarr = wp_html_split( $content );
  106. unset( $content );
  107. foreach ( $textarr as &$element ) {
  108. if ( '' === $element || '<' === $element[0] ) {
  109. continue;
  110. }
  111. $element = preg_replace_callback( $pattern, $callback, $element );
  112. }
  113. return join( $textarr );
  114. }
  115. if ( ! function_exists( 'jetpack_shortcode_get_wpvideo_id' ) ) {
  116. /**
  117. * Get VideoPress ID from wpvideo shortcode attributes.
  118. *
  119. * @param array $atts Shortcode attributes.
  120. * @return int $id VideoPress ID.
  121. */
  122. function jetpack_shortcode_get_wpvideo_id( $atts ) {
  123. if ( isset( $atts[0] ) ) {
  124. return $atts[0];
  125. } else {
  126. return 0;
  127. }
  128. }
  129. }
  130. if ( ! function_exists( 'jetpack_shortcode_get_videopress_id' ) ) {
  131. /**
  132. * Get VideoPress ID from videopress shortcode attributes.
  133. *
  134. * @param array $atts Shortcode attributes.
  135. * @return int $id VideoPress ID.
  136. */
  137. function jetpack_shortcode_get_videopress_id( $atts ) {
  138. if ( isset( $atts[0] ) ) {
  139. return $atts[0];
  140. } else {
  141. return 0;
  142. }
  143. }
  144. }
  145. /**
  146. * Common element attributes parsing and sanitizing for src, width and height.
  147. *
  148. * @since 4.5.0
  149. *
  150. * @param array $attrs With original values.
  151. *
  152. * @return array $attrs With sanitized values.
  153. */
  154. function wpcom_shortcodereverse_parseattr( $attrs ) {
  155. $defaults = array(
  156. 'src' => false,
  157. 'width' => false,
  158. 'height' => false,
  159. );
  160. $attrs = shortcode_atts( $defaults, $attrs );
  161. $attrs['src'] = strip_tags( $attrs['src'] ); // For sanity
  162. $attrs['width'] = ( is_numeric( $attrs['width'] ) ) ? abs( (int) $attrs['width'] ) : $defaults['width'];
  163. $attrs['height'] = ( is_numeric( $attrs['height'] ) ) ? abs( (int) $attrs['height'] ) : $defaults['height'];
  164. return $attrs;
  165. }
  166. /**
  167. * When an embed service goes away, we can use this handler
  168. * to output a link for history's sake.
  169. */
  170. function jetpack_deprecated_embed_handler( $matches, $attr, $url ) {
  171. return sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html( esc_url( $url ) ) );
  172. }
  173. jetpack_load_shortcodes();