This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from xwp/feature/add-qunit
Add QUnit Tests
- Loading branch information
Showing
9 changed files
with
1,115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule dev-lib
updated
from 52874e to 61a4ad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
/** | ||
* Customize Posts WP CLI Command class. | ||
* | ||
* @package WordPress | ||
* @subpackage Customize | ||
*/ | ||
|
||
/** | ||
* Class Customize_Posts_WP_CLI_Command. | ||
*/ | ||
class Customize_Posts_WP_CLI_Command extends WP_CLI_Command { | ||
|
||
const CORE_BASE_HREF = 'https://develop.svn.wordpress.org/trunk/src/'; | ||
|
||
const PLUGIN_BASE_HREF = '../../'; | ||
|
||
/** | ||
* Plugin script handles that will need to be enqueued. | ||
* | ||
* @var array | ||
*/ | ||
static $plugin_script_handles = array( | ||
'customize-posts-panel', | ||
'customize-post-date-control', | ||
'customize-post-editor-control', | ||
'customize-post-status-control', | ||
'customize-post-section', | ||
'customize-dynamic-control', | ||
'customize-posts', | ||
'customize-nav-menus-posts-extensions', | ||
'customize-preview-setting-validities', | ||
'customize-deferred-partial', | ||
'customize-post-field-partial', | ||
'customize-preview-posts', | ||
'edit-post-preview-admin', | ||
'edit-post-preview-customize', | ||
'customize-page-template', | ||
'edit-post-preview-admin-page-template', | ||
'customize-featured-image', | ||
'edit-post-preview-admin-featured-image', | ||
'customize-preview-featured-image', | ||
'select2', | ||
); | ||
|
||
/** | ||
* Replace the base URL for script sources. | ||
* | ||
* @param string $script_tag Script tag. | ||
* @param string $handle Script handle. | ||
* @return string|boolean Rewritten script src. | ||
*/ | ||
static function filter_script_loader_tag( $script_tag, $handle ) { | ||
if ( in_array( $handle, self::$plugin_script_handles, true ) ) { | ||
$script_tag = preg_replace( '#https?://[^"]+?/wp-content/plugins/[^/]+/#', self::PLUGIN_BASE_HREF, $script_tag ); | ||
} elseif ( 0 === strpos( $handle, 'select2-locale-' ) ) { | ||
$script_tag = ''; | ||
} elseif ( self::is_core_script( $script_tag ) ) { | ||
$script_tag = preg_replace( '#https?://[^"]+?/(?=(wp-includes|wp-admin)/)#', self::CORE_BASE_HREF, $script_tag ); | ||
} else { | ||
$script_tag = false; | ||
} | ||
|
||
return $script_tag; | ||
} | ||
|
||
/** | ||
* Checks to see if its core script. | ||
* | ||
* @param string $script_tag Script handle. | ||
* @return bool | ||
*/ | ||
public static function is_core_script( $script_tag ) { | ||
return false !== strpos( $script_tag, 'wp-includes/' ) || false !== strpos( $script_tag, 'wp-admin/' ); | ||
} | ||
|
||
/** | ||
* Print dependencies for the supplied script handles. | ||
* | ||
* @subcommand generate-qunit-test-suite | ||
*/ | ||
public function generate_qunit_test_suite() { | ||
global $wp_customize; | ||
|
||
$test_suite_file = dirname( __FILE__ ) . '/../tests/qunit/index.html'; | ||
$test_suite_template = dirname( __FILE__ ) . '/../tests/qunit/test-suite-template.html'; | ||
|
||
if ( empty( $wp_customize ) || ! ( $wp_customize instanceof \WP_Customize_Manager ) ) { | ||
require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); | ||
$wp_customize = new \WP_Customize_Manager(); // WPCS: override ok. | ||
} | ||
|
||
foreach ( self::$plugin_script_handles as $script_handle ) { | ||
if ( ! wp_scripts()->query( $script_handle, 'registered' ) ) { | ||
WP_CLI::error( "Script handle not registered: $script_handle" ); | ||
} | ||
} | ||
|
||
add_filter( 'script_loader_tag', array( __CLASS__, 'filter_script_loader_tag' ), 10, 2 ); | ||
|
||
ob_start(); | ||
echo "<div hidden>\n"; | ||
|
||
do_action( 'customize_controls_enqueue_scripts' ); | ||
$wp_scripts = wp_scripts(); | ||
$customize_posts_scripts = array(); | ||
|
||
/** | ||
* Get only customize-posts plugin handles. | ||
*/ | ||
if ( ! empty( $wp_scripts->queue ) ) { | ||
foreach ( $wp_scripts->queue as $script_handle ) { | ||
if ( 0 === strpos( $wp_scripts->registered[ $script_handle ]->src, CUSTOMIZE_POSTS_DIR_URL ) ) { | ||
array_push( $customize_posts_scripts, $script_handle ); | ||
} | ||
} | ||
} | ||
|
||
wp_print_scripts( $customize_posts_scripts ); | ||
|
||
echo "</div>\n"; | ||
$output = ob_get_clean(); | ||
|
||
$output = preg_replace( '/<link.+?>/', '', $output ); | ||
$output = preg_replace( '#<style[^>]*>.+?</style>#s', '', $output ); | ||
|
||
$test_suite = file_get_contents( $test_suite_template ); | ||
$test_suite = preg_replace( '/(?=<html)/', "<!-- WARNING! Do not edit this file. It is generated via: wp customize-posts generate-qunit-test-suite -->\n", $test_suite ); | ||
$test_suite = str_replace( '{{dependencies}}', $output, $test_suite ); | ||
file_put_contents( $test_suite_file, $test_suite ); | ||
|
||
remove_filter( 'script_loader_tag', array( __CLASS__, 'filter_script_loader_tag' ), 10 ); | ||
|
||
WP_CLI::success( sprintf( 'Wrote test suite to %s', realpath( $test_suite_file ) ) ); | ||
} | ||
} |
Oops, something went wrong.