Нет описания

compat.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. // SPL can be disabled on PHP 5.2
  3. if ( ! function_exists( 'spl_autoload_register' ) ) {
  4. $_wp_spl_autoloaders = array();
  5. /**
  6. * Registers a function to be autoloaded.
  7. *
  8. * @since 4.6.0
  9. *
  10. * @param callable $autoload_function The function to register.
  11. * @param bool $throw Optional. Whether the function should throw an exception
  12. * if the function isn't callable. Default true.
  13. * @param bool $prepend Whether the function should be prepended to the stack.
  14. * Default false.
  15. *
  16. * @throws Exception
  17. */
  18. function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) {
  19. if ( $throw && ! is_callable( $autoload_function ) ) {
  20. // String not translated to match PHP core.
  21. throw new Exception( 'Function not callable' );
  22. }
  23. global $_wp_spl_autoloaders;
  24. // Don't allow multiple registration.
  25. if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) {
  26. return;
  27. }
  28. if ( $prepend ) {
  29. array_unshift( $_wp_spl_autoloaders, $autoload_function );
  30. } else {
  31. $_wp_spl_autoloaders[] = $autoload_function;
  32. }
  33. }
  34. /**
  35. * Unregisters an autoloader function.
  36. *
  37. * @since 4.6.0
  38. *
  39. * @param callable $function The function to unregister.
  40. *
  41. * @return bool True if the function was unregistered, false if it could not be.
  42. */
  43. function spl_autoload_unregister( $function ) {
  44. global $_wp_spl_autoloaders;
  45. foreach ( $_wp_spl_autoloaders as &$autoloader ) {
  46. if ( $autoloader === $function ) {
  47. unset( $autoloader );
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. /**
  54. * Retrieves the registered autoloader functions.
  55. *
  56. * @since 4.6.0
  57. *
  58. * @return array List of autoloader functions.
  59. */
  60. function spl_autoload_functions() {
  61. return $GLOBALS['_wp_spl_autoloaders'];
  62. }
  63. }
  64. if ( ! function_exists( 'current_action' ) ) {
  65. function current_action() {
  66. return current_filter();
  67. }
  68. }
  69. if ( ! function_exists( 'get_called_class' ) ) {
  70. function get_called_class( $bt = false, $l = 1 ) {
  71. if ( ! $bt ) {
  72. $bt = debug_backtrace();
  73. }
  74. if ( ! isset ( $bt[ $l ] ) ) {
  75. throw new Exception ( "Cannot find called class -> stack level too deep." );
  76. }
  77. if ( ! isset( $bt[ $l ]['type'] ) ) {
  78. throw new Exception ( 'type not set' );
  79. } else switch ( $bt[ $l ]['type'] ) {
  80. case '::':
  81. $lines = file( $bt[ $l ]['file'] );
  82. $i = 0;
  83. $callerLine = '';
  84. do {
  85. $i ++;
  86. $callerLine = $lines[ $bt[ $l ]['line'] - $i ] . $callerLine;
  87. } while ( stripos( $callerLine, $bt[ $l ]['function'] ) === false );
  88. preg_match( '/([a-zA-Z0-9\_]+)::' . $bt[ $l ]['function'] . '/', $callerLine, $matches );
  89. if ( ! isset( $matches[1] ) ) {
  90. // must be an edge case.
  91. throw new Exception ( "Could not find caller class: originating method call is obscured." );
  92. }
  93. switch ( $matches[1] ) {
  94. case 'self':
  95. case 'parent':
  96. return get_called_class( $bt, $l + 1 );
  97. default:
  98. return $matches[1];
  99. }
  100. // won't get here.
  101. case '->':
  102. switch ( $bt[ $l ]['function'] ) {
  103. case '__get':
  104. // edge case -> get class of calling object
  105. if ( ! is_object( $bt[ $l ]['object'] ) ) {
  106. throw new Exception ( "Edge case fail. __get called on non object." );
  107. }
  108. return get_class( $bt[ $l ]['object'] );
  109. default:
  110. return get_class( $bt[ $l ]['object'] );
  111. }
  112. default:
  113. throw new Exception ( "Unknown backtrace method type" );
  114. }
  115. }
  116. }
  117. if ( ! function_exists( 'get_term_name' ) ) {
  118. function get_term_name( $term_id, $taxonomy ) {
  119. $term = get_term_by( 'id', absint( $term_id ), $taxonomy );
  120. return $term->name;
  121. }
  122. }
  123. // For WP versions before 3.6
  124. if ( ! function_exists( 'has_shortcode' ) ) {
  125. function has_shortcode( $content, $tag ) {
  126. if ( false === strpos( $content, '[' ) ) {
  127. return false;
  128. }
  129. if ( shortcode_exists( $tag ) ) {
  130. preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
  131. if ( empty( $matches ) ) {
  132. return false;
  133. }
  134. foreach ( $matches as $shortcode ) {
  135. if ( $tag === $shortcode[2] ) {
  136. return true;
  137. } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
  138. return true;
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144. }
  145. if ( ! function_exists( 'shortcode_exists' ) ) {
  146. function shortcode_exists( $tag ) {
  147. global $shortcode_tags;
  148. return array_key_exists( $tag, $shortcode_tags );
  149. }
  150. }
  151. /**
  152. * Deprecated PHP v5.3 functions.
  153. */
  154. if ( ! function_exists( 'array_replace_recursive' ) ) {
  155. function array_replace_recursive( $array, $array1 ) {
  156. // handle the arguments, merge one by one
  157. $args = func_get_args();
  158. $array = $args[0];
  159. if ( ! is_array( $array ) ) {
  160. return $array;
  161. }
  162. for ( $i = 1; $i < count( $args ); $i ++ ) {
  163. if ( is_array( $args[ $i ] ) ) {
  164. $array = recurse( $array, $args[ $i ] );
  165. }
  166. }
  167. return $array;
  168. }
  169. }
  170. if ( ! function_exists( 'recurse' ) ) {
  171. function recurse( $array, $array1 ) {
  172. foreach ( $array1 as $key => $value ) {
  173. // create new key in $array, if it is empty or not an array
  174. if ( ! isset( $array[ $key ] ) || ( isset( $array[ $key ] ) && ! is_array( $array[ $key ] ) ) ) {
  175. $array[ $key ] = array();
  176. }
  177. // overwrite the value in the base array
  178. if ( is_array( $value ) ) {
  179. $value = recurse( $array[ $key ], $value );
  180. }
  181. $array[ $key ] = $value;
  182. }
  183. return $array;
  184. }
  185. }
  186. if ( ! function_exists( 'write_log' ) ) {
  187. function write_log( $log ) {
  188. if ( is_array( $log ) || is_object( $log ) ) {
  189. error_log( print_r( $log, true ) );
  190. } else {
  191. error_log( $log );
  192. }
  193. }
  194. }
  195. if ( ! function_exists( 'boolval' ) ) {
  196. function boolval( $val ) {
  197. return ( bool ) $val;
  198. }
  199. }
  200. if ( ! function_exists( 'maybe_json_attr' ) ) {
  201. function maybe_json_attr( $value, $encode = false ) {
  202. if ( is_object( $value ) || is_array( $value ) ) {
  203. return $encode ? htmlspecialchars( wp_json_encode( $value ) ) : wp_json_encode( $value );
  204. }
  205. return $value;
  206. }
  207. }
  208. if ( ! function_exists( 'has_blocks' ) ) {
  209. /**
  210. * Determine whether a post or content string has blocks.
  211. *
  212. * This test optimizes for performance rather than strict accuracy, detecting
  213. * the pattern of a block but not validating its structure. For strict accuracy,
  214. * you should use the block parser on post content.
  215. *
  216. * @since 5.0.0
  217. * @see parse_blocks()
  218. *
  219. * @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post.
  220. * @return bool Whether the post has blocks.
  221. */
  222. function has_blocks( $post = null ) {
  223. if ( ! is_string( $post ) ) {
  224. $wp_post = get_post( $post );
  225. if ( $wp_post instanceof WP_Post ) {
  226. $post = $wp_post->post_content;
  227. }
  228. }
  229. return false !== strpos( (string) $post, '<!-- wp:' );
  230. }
  231. }