diff --git a/README.md b/README.md index 1107ed1f..cd34859a 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,15 @@ You can also lock it in to a specific commit or tag: } ``` +If you are running the libraries in a non-WordPress environment, e.g. a PHPUnit test suite that +tests your code without loading WordPress, you'll need to polyfill the WordPress APIs used by +the php-toolkit libraries: + +```php +require_once __DIR__ . '/vendor/autoload.php'; +polyfill_wordpress_apis(); +``` + For now, there is no way to cherry-pick just the one library you need. It's all or nothing. Note that the composer.json example above downloads more files than the required minimum, e.g. markdowns, unit tests, the `plugins` directory, etc. That's about 50MB of code in total and, most likely, it's not a big deal for your project. If you want a smaller package, the Data Liberation plugin referenced above ships a minified phar file that's about ~500KB compressed. diff --git a/bootstrap.php b/bootstrap.php index 35a4af0b..36a8f90c 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -13,3 +13,5 @@ */ require_once __DIR__ . '/vendor/autoload.php'; + +polyfill_wordpress_apis(); diff --git a/components/Polyfill/wordpress.php b/components/Polyfill/wordpress.php index 5bc8773c..e365e424 100644 --- a/components/Polyfill/wordpress.php +++ b/components/Polyfill/wordpress.php @@ -1,222 +1,224 @@ $callback, - 'accepted_args' => $accepted_args, - ); - - return true; } -} -if ( ! function_exists( 'add_action' ) ) { - function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { - return add_filter( $hook_name, $callback, $priority, $accepted_args ); + if ( ! function_exists( 'esc_html' ) ) { + function esc_html( $input ) { + return htmlspecialchars( $input ); + } } -} -if ( ! function_exists( 'apply_filters' ) ) { - function apply_filters( $hook_name, $value ) { - global $wp_filter; - if ( ! isset( $wp_filter[ $hook_name ] ) ) { - return $value; + if ( ! function_exists( 'esc_url' ) ) { + function esc_url( $url ) { + return htmlspecialchars( $url ); } - $args = func_get_args(); - array_shift( $args ); // Remove hook name + } - ksort( $wp_filter[ $hook_name ] ); - foreach ( $wp_filter[ $hook_name ] as $priority => $functions ) { - foreach ( $functions as $function ) { - $args[0] = $value; - $value = call_user_func_array( $function['function'], array_slice( $args, 0, $function['accepted_args'] ) ); + if ( ! function_exists( 'add_filter' ) ) { + function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { + global $wp_filter; + if ( ! isset( $wp_filter ) ) { + $wp_filter = array(); + } + if ( ! isset( $wp_filter[ $hook_name ] ) ) { + $wp_filter[ $hook_name ] = array(); + } + if ( ! isset( $wp_filter[ $hook_name ][ $priority ] ) ) { + $wp_filter[ $hook_name ][ $priority ] = array(); } + $wp_filter[ $hook_name ][ $priority ][] = array( + 'function' => $callback, + 'accepted_args' => $accepted_args, + ); + + return true; } + } - return $value; + if ( ! function_exists( 'add_action' ) ) { + function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { + return add_filter( $hook_name, $callback, $priority, $accepted_args ); + } } -} -if ( ! function_exists( 'do_action' ) ) { - function do_action( $hook_name ) { - global $wp_filter; - if ( ! isset( $wp_filter[ $hook_name ] ) ) { - return; + if ( ! function_exists( 'apply_filters' ) ) { + function apply_filters( $hook_name, $value ) { + global $wp_filter; + if ( ! isset( $wp_filter[ $hook_name ] ) ) { + return $value; + } + $args = func_get_args(); + array_shift( $args ); // Remove hook name + + ksort( $wp_filter[ $hook_name ] ); + foreach ( $wp_filter[ $hook_name ] as $priority => $functions ) { + foreach ( $functions as $function ) { + $args[0] = $value; + $value = call_user_func_array( $function['function'], array_slice( $args, 0, $function['accepted_args'] ) ); + } + } + + return $value; } - $args = func_get_args(); - array_shift( $args ); // Remove hook name + } - ksort( $wp_filter[ $hook_name ] ); - foreach ( $wp_filter[ $hook_name ] as $priority => $functions ) { - foreach ( $functions as $function ) { - call_user_func_array( $function['function'], array_slice( $args, 0, $function['accepted_args'] ) ); + if ( ! function_exists( 'do_action' ) ) { + function do_action( $hook_name ) { + global $wp_filter; + if ( ! isset( $wp_filter[ $hook_name ] ) ) { + return; + } + $args = func_get_args(); + array_shift( $args ); // Remove hook name + + ksort( $wp_filter[ $hook_name ] ); + foreach ( $wp_filter[ $hook_name ] as $priority => $functions ) { + foreach ( $functions as $function ) { + call_user_func_array( $function['function'], array_slice( $args, 0, $function['accepted_args'] ) ); + } } } } -} -if ( ! function_exists( 'parse_blocks' ) ) { - function parse_blocks( $input ) { - $parser = new WP_Block_Parser(); + if ( ! function_exists( 'parse_blocks' ) ) { + function parse_blocks( $input ) { + $parser = new WP_Block_Parser(); - return $parser->parse( $input ); + return $parser->parse( $input ); + } } -} -if ( ! function_exists( 'serialize_blocks' ) ) { - function serialize_blocks( $blocks ) { - return implode( '', array_map( 'serialize_block', $blocks ) ); + if ( ! function_exists( 'serialize_blocks' ) ) { + function serialize_blocks( $blocks ) { + return implode( '', array_map( 'serialize_block', $blocks ) ); + } } -} -if ( ! function_exists( 'serialize_block' ) ) { - function serialize_block( $block ) { - $block_content = ''; + if ( ! function_exists( 'serialize_block' ) ) { + function serialize_block( $block ) { + $block_content = ''; - $index = 0; - foreach ( $block['innerContent'] as $chunk ) { - $block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index ++ ] ); - } + $index = 0; + foreach ( $block['innerContent'] as $chunk ) { + $block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index ++ ] ); + } - if ( ! is_array( $block['attrs'] ) ) { - $block['attrs'] = array(); - } + if ( ! is_array( $block['attrs'] ) ) { + $block['attrs'] = array(); + } - return get_comment_delimited_block_content( - $block['blockName'], - $block['attrs'], - $block_content - ); + return get_comment_delimited_block_content( + $block['blockName'], + $block['attrs'], + $block_content + ); + } } -} -if ( ! function_exists( 'get_comment_delimited_block_content' ) ) { - function get_comment_delimited_block_content( $block_name, $block_attributes, $block_content ) { - if ( is_null( $block_name ) ) { - return $block_content; - } + if ( ! function_exists( 'get_comment_delimited_block_content' ) ) { + function get_comment_delimited_block_content( $block_name, $block_attributes, $block_content ) { + if ( is_null( $block_name ) ) { + return $block_content; + } - $serialized_block_name = strip_core_block_namespace( $block_name ); - $serialized_attributes = empty( $block_attributes ) ? '' : serialize_block_attributes( $block_attributes ) . ' '; + $serialized_block_name = strip_core_block_namespace( $block_name ); + $serialized_attributes = empty( $block_attributes ) ? '' : serialize_block_attributes( $block_attributes ) . ' '; - if ( empty( $block_content ) ) { - return sprintf( '', $serialized_block_name, $serialized_attributes ); - } + if ( empty( $block_content ) ) { + return sprintf( '', $serialized_block_name, $serialized_attributes ); + } - return sprintf( - '%s', - $serialized_block_name, - $serialized_attributes, - $block_content, - $serialized_block_name - ); + return sprintf( + '%s', + $serialized_block_name, + $serialized_attributes, + $block_content, + $serialized_block_name + ); + } } -} -if ( ! function_exists( 'strip_core_block_namespace' ) ) { - function strip_core_block_namespace( $block_name = null ) { - if ( is_string( $block_name ) && strncmp( $block_name, 'core/', strlen( 'core/' ) ) === 0 ) { - return substr( $block_name, 5 ); - } + if ( ! function_exists( 'strip_core_block_namespace' ) ) { + function strip_core_block_namespace( $block_name = null ) { + if ( is_string( $block_name ) && strncmp( $block_name, 'core/', strlen( 'core/' ) ) === 0 ) { + return substr( $block_name, 5 ); + } - return $block_name; + return $block_name; + } } -} -if ( ! function_exists( 'serialize_block_attributes' ) ) { - function serialize_block_attributes( $block_attributes ) { - $encoded_attributes = json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); - $encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes ); - $encoded_attributes = preg_replace( '//', '\\u003e', $encoded_attributes ); - $encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes ); - // Regex: /\\"/ - $encoded_attributes = preg_replace( '/\\\\"/', '\\u0022', $encoded_attributes ); + if ( ! function_exists( 'serialize_block_attributes' ) ) { + function serialize_block_attributes( $block_attributes ) { + $encoded_attributes = json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); + $encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes ); + $encoded_attributes = preg_replace( '//', '\\u003e', $encoded_attributes ); + $encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes ); + // Regex: /\\"/ + $encoded_attributes = preg_replace( '/\\\\"/', '\\u0022', $encoded_attributes ); - return $encoded_attributes; + return $encoded_attributes; + } } }