-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplugin.php
147 lines (120 loc) · 4.05 KB
/
plugin.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
<?php
/**
* Plugin Name: Cron Runner
* Description: This mu-plugin lets you run WP cron for a site / a network site via a single endpoint.
* Version: 1.0.3
* Authors: Ville Siltala & Ville Pietarinen / Geniem Oy
* Author URI: https://www.geniem.fi/
* License: MIT License
*/
namespace Geniem;
use WP_Error;
// We would use filter_input() for this,
// but it has a known bug on some PHP versions.
// See: https://bugs.php.net/bug.php?id=49184
$request_uri = $_SERVER['REQUEST_URI'];
// Remove the trailing forward slash.
$request_uri = rtrim( $request_uri, '/' );
// Execute our plugin only on exact url match.
if ( $request_uri === '/run-cron' ) {
// Prevent invalid requests.
if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
die( 'WP Cron Runner: Invalid request.' );
}
$cron_excecuted = [];
$scheme = defined( 'REQUEST_SCHEME' ) ? REQUEST_SCHEME : 'https';
// Multisite
if ( defined( 'WP_ALLOW_MULTISITE' ) && WP_ALLOW_MULTISITE === true ) {
global $wpdb;
$sql = "SELECT domain, path FROM $wpdb->blogs WHERE archived=0 AND deleted=0";
$results = $wpdb->get_results( $sql );
if ( is_wp_error( $results ) ) {
// A database error occurred.
wp_die(
$results->get_error_message(),
'WP Cron Runner',
[ 'response' => 500 ]
);
}
elseif ( ! empty( $results ) ) {
foreach ( $results as $blog ) {
if ( empty( $blog->domain ) ) {
// Skip invalid data.
continue;
}
$home_url = $scheme . '://' . $blog->domain;
// Subfolder multisite needs path also
if ( $blog->path !== '/' ) {
$home_url .= $blog->path;
}
run_cron( $home_url );
$cron_excecuted[] = $home_url;
}
}
}
// Single site
else {
$home_url = get_home_url( null, '', $scheme );
run_cron( $home_url );
$cron_excecuted[] = $home_url;
}
ob_start();
?>
<h1>WP Cron Runner executed for sites:</h1>
<?php
if ( ! empty( $cron_excecuted ) ) {
echo '<ul>';
foreach ( $cron_excecuted as $exc_url ) {
echo '<li>' . esc_url( $exc_url ) . '</li>';
}
echo '</ul>';
}
else {
echo '<strong>0 sites.</strong>';
}
?>
<?php
// End the PHP process.
wp_die(
ob_get_clean(),
'WP Cron Runner',
[ 'response' => 200 ]
);
} // End if().
/**
* Excecutes a wp_remote_get() call to run WP cron for a specific site.
*
* @param string $home_url The full site url: https://www.example.com.
* @return WP_Error|array The response or WP_Error on failure.
*/
function run_cron( $home_url ) {
global $wp_version;
$args = array(
'timeout' => 10,
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
'blocking' => true,
'sslverify' => false,
'headers' => [],
);
// If basic auth is used, define these constants.
$user = defined( 'WP_CRON_RUNNER_AUTH_USER' ) ? WP_CRON_RUNNER_AUTH_USER : null;
$pw = defined( 'WP_CRON_RUNNER_AUTH_PW' ) ? WP_CRON_RUNNER_AUTH_PW : null;
if ( ! $user && defined( 'BASIC_AUTH_USER' ) ) {
$user = BASIC_AUTH_USER;
}
if ( ! $pw && defined( 'BASIC_AUTH_PASSWORD' ) ) {
$pw = BASIC_AUTH_PASSWORD;
}
if ( ! $pw && defined( 'BASIC_AUTH_PASSWORD_HASH' ) ) {
$pw = str_replace( '{PLAIN}', '', BASIC_AUTH_PASSWORD_HASH );
}
if ( ! empty( $user ) && ! empty( $pw ) ) {
// If using plain env strip plain from the basic auth password.
$pw = str_replace( '{PLAIN}', '', $pw );
// Set basic auth.
$args['headers']['Authorization'] = 'Basic ' . base64_encode( $user . ':' . $pw );
}
$cron_url = $home_url . '/wp-cron.php';
$response = wp_remote_get( $cron_url, $args );
return $response;
}