forked from Nikschavan/elementor-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.php
More file actions
117 lines (92 loc) · 3.9 KB
/
command.php
File metadata and controls
117 lines (92 loc) · 3.9 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
<?php
use Elementor\Global_CSS_File;
use Elementor\Post_CSS_File;
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
/**
* WP ClI Commands for Elementor
*/
class Elementor_Command extends WP_CLI_Command {
/**
* Just a test to check if the package is installed and callable
*/
public function hello() {
WP_CLI::success( 'Hello ' . __CLASS__ );
}
/**
* Rebuild the page CSS file for every page in every site on the network
*
* [--network]
* Regenerate CSS of for all the sites in the network.
*
* @param $args
* @param $assoc_args
*/
public function rebuild_css( $args, $assoc_args ) {
if ( class_exists( '\Elementor\Plugin' ) ) {
$current_blog_id = get_current_blog_id();
if ( isset( $assoc_args['network'] ) ) {
$options = [
'return' => true, // Return 'STDOUT'; use 'all' for full object.
'parse' => 'json', // Parse captured STDOUT to JSON array.
'launch' => false, // Reuse the current process.
'exit_error' => true, // Halt script execution on error.
];
$sites = WP_CLI::runcommand( 'site list --format=json --fields=blog_id,domain', $options );
WP_CLI::line( WP_CLI::colorize( '%c[NETWORK] Number of sites%n ' . count( $sites ) ) );
} else {
// running for first site in the network
$sites = [ (array) get_blog_details( $current_blog_id, false ) ];
}
if ( $sites ) {
foreach ( $sites as $site ) {
if ( isset( $assoc_args['network'] ) ) {
WP_CLI::line( WP_CLI::colorize( '%3%k[NETWORK] Switching to site%n ' . $site['domain'] . ' (' . $site['blog_id'] . ')' ) );
}
switch_to_blog( $site['blog_id'] );
WP_CLI::line( WP_CLI::colorize( '%cRebuilding global CSS file%n' ) );
$global_css_file = new Global_CSS_File();
$global_css_file->update();
$pages = get_posts( [
'post_type' => 'page',
'post_status' => 'any',
'posts_per_page' => - 1,
'offset' => 0,
] );
WP_CLI::line( WP_CLI::colorize( '%mNumber of pages%n ' . count( $pages ) ) );
foreach ( $pages as $page ) {
WP_CLI::line( WP_CLI::colorize( '%yRebuilding CSS for page %n' . $page->ID ) );
$css_file = new Post_CSS_File( $page->ID );
$css_file->update();
}
}
}
$global_css_file = new Global_CSS_File();
$global_css_file->update();
if ( isset( $assoc_args['network'] ) ) {
WP_CLI::line( WP_CLI::colorize( '%3%k[NETWORK] Switching back to site%n ' . $current_blog_id ) );
switch_to_blog( $current_blog_id );
}
WP_CLI::success( 'CSS has been rebuilt for every page' );
} else {
WP_CLI::error( 'Elementor is not installed on this site' );
}
}
/**
* Clear the CSS cache for every page on the current site (delete files and remove meta from database)
* TODO : handle --network argument
*
* @param $args
* @param $assoc_args
*/
public function clear_css_cache( $args, $assoc_args ) {
if ( class_exists( '\Elementor\Plugin' ) ) {
\Elementor\Plugin::$instance->posts_css_manager->clear_cache();
WP_CLI::success( 'Regenerated the Elementor CSS' );
} else {
WP_CLI::error( 'Elementor is not installed on this site' );
}
}
}
WP_CLI::add_command( 'elementor', 'Elementor_Command' );