This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathclass-customize-posts-wp-cli-command.php
More file actions
136 lines (115 loc) · 4.2 KB
/
class-customize-posts-wp-cli-command.php
File metadata and controls
136 lines (115 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 ) ) );
}
}