This repository was archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand.php
178 lines (156 loc) · 5.71 KB
/
command.php
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* WP-CLI command to automatically check WordPress core and all installed themes and plugins for
* available updates.
*
* @package Growella\WP_CLI\UpdateCheck
* @author Growella
*/
namespace Growella\WP_CLI;
use WP_CLI;
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
/**
* Checks WordPress core and all installed plugins and themes for available updates.
*/
class UpdateCheck extends \WP_CLI_Command {
/**
* Check WordPress core, themes, and plugins for available updates.
*
* ## OPTIONS
*
* [--email=<email>]
* : If provided, email the generated report to the provided email address
* instead of printing to STDOUT. If the --email option is used but no address
* is provided, the report will be sent to the site's admin_email address.
*
* [--report-current]
* : Send an email report, even if there are no pending updates. Without this
* flag, reports will only be emailed if there is at least one pending update.
*
* [--quiet]
* : Silence all output, useful when running as a cron task.
*
* ## EXAMPLES
*
* wp update-check run
* wp update-check run [email protected]
* wp update-check run [email protected] --quiet
* wp update-check run --email --report-current
*
* @param array $args Optional. Positional arguments. Default is empty.
* @param array $assoc_args Optional. Associative arguments. Default is empty.
*/
public function run( $args = array(), $assoc_args = array() ) {
$report = esc_html( sprintf( __( 'Update check for %s', 'update-check' ), get_bloginfo( 'url' ) ) );
$report .= PHP_EOL . esc_html( sprintf( _x( 'Generated %s', 'report date', 'update-check' ), date( 'r' ) ) );
$report .= PHP_EOL . PHP_EOL;
$quiet = isset( $assoc_args['quiet'] );
$send_email = false;
/*
* WordPress core.
*/
$report .= esc_html_x( 'WordPress Core:', 'update check report section', 'update-check' );
$core = WP_CLI::launch_self( 'core check-update', array(), array( 'format' => 'json' ), false, true );
$core = json_decode( $core->stdout );
if ( ! empty( $core ) ) {
foreach ( $core as $version ) {
$report .= PHP_EOL . esc_html( sprintf(
/** Translators: %1$s is the type (major|minor), %2$s is the version number. */
__( '- [%1$s] WordPress version %2$s is now available, please upgrade as soon as possible.', 'update-check' ),
$version->update_type,
$version->version
) );
}
$send_email = true;
} else {
$report .= PHP_EOL . esc_html__( 'WordPress core is up-to-date.' );
}
$report .= PHP_EOL . PHP_EOL;
/*
* Plugins.
*/
$report .= esc_html_x( 'Plugin Updates:', 'update check report section', 'update-check' );
$plugins = WP_CLI::launch_self( 'plugin update', array(), array( 'all' => true, 'dry-run' => true, 'format' => 'json' ), false, true );
$plugins = json_decode( $plugins->stdout );
if ( ! empty( $plugins ) ) {
foreach ( $plugins as $plugin ) {
$report .= PHP_EOL . sprintf(
/** Translators: %1$s is the plugin name, %2$s is the current version, %3$s is the latest version. */
__( '- An update is available for %1$s (%2$s => %3$s)', 'update-check' ),
$plugin->name,
$plugin->version,
$plugin->update_version
);
}
$send_email = true;
} else {
$report .= PHP_EOL . esc_html__( 'All plugins are up-to-date.' );
}
$report .= PHP_EOL . PHP_EOL;
/*
* Themes.
*/
$report .= esc_html_x( 'Theme Updates:', 'update check report section', 'update-check' );
$themes = WP_CLI::launch_self( 'theme update', array(), array( 'all' => true, 'dry-run' => true, 'format' => 'json' ), false, true );
$themes = json_decode( $themes->stdout );
if ( ! empty( $themes ) ) {
foreach ( $themes as $theme ) {
$report .= PHP_EOL . sprintf(
/** Translators: %1$s is the theme name, %2$s is the current version, %3$s is the latest version. */
__( '- An update is available for %1$s (%2$s => %3$s)', 'update-check' ),
$theme->name,
$theme->version,
$theme->update_version
);
}
$send_email = true;
} else {
$report .= PHP_EOL . esc_html__( 'All themes are up-to-date.' );
}
$report .= PHP_EOL . PHP_EOL;
// Finally, deliver the report.
if ( isset( $assoc_args['email'] ) ) {
// No need to send an email.
if ( ! $send_email && ! isset( $assoc_args['report-current'] ) ) {
return WP_CLI::debug( __( 'Everything up to date, no email has been sent.', 'update-check' ) );
}
$email = $this->send_email( $report, (string) $assoc_args['email'] );
if ( ! $quiet ) {
WP_CLI::success( sprintf( __( 'Report has been sent to %s', 'update-check' ), $email ) );
}
} else {
WP_CLI::log( $report );
}
}
/**
* Send the report email to the specified email address.
*
* @param string $report The report contents.
* @param string $email Optional. The email address to deliver to. If not specified, will
* default to the site's admin email.
* @return string The email address the report was sent to.
*/
protected function send_email( $report, $email = null ) {
$subject = sprintf( __( 'Updates are available for %s', 'update-check' ), get_bloginfo( 'url' ) );
if ( ! $email ) {
$email = get_option( 'admin_email' );
}
/*
* WP-CLI doesn't have access to the $_SERVER['SERVER_NAME'], so we need to explicitly set the
* "from" address.
*/
$hostname = parse_url( site_url(), PHP_URL_HOST );
// Strip 'www.', if it exists.
if ( 'www.' === substr( $hostname, 0, 4 ) ) {
$hostname = substr( $hostname, 4 );
}
// Finally, send the email.
wp_mail( $email, $subject, $report, array(
sprintf( 'From: updates@%s', $hostname ),
) );
return $email;
}
}
WP_CLI::add_command( 'update-check', __NAMESPACE__ . '\UpdateCheck' );