utils.php 3.91 KB
Newer Older
1
2
<?php

3
namespace Automattic\WP\Cron_Control;
4
5
6
7
8
9

/**
 * Produce a simplified version of the cron events array
 *
 * Also removes superfluous, non-event data that Core stores in the option
 */
10
function collapse_events_array( $events, $timestamp = null ) {
11
12
	$collapsed_events = array();

13
	// Ensure an event is always returned
14
15
16
17
	if ( ! is_array( $events ) ) {
		return $collapsed_events;
	}

18
19
20
21
22
23
24
25
26
27
28
29
	// Allow filtering to only events with a given timestamp
	if ( is_numeric( $timestamp ) ) {
		if ( isset( $events[ $timestamp ] ) ) {
			$_events = $events[ $timestamp ];
			$events  = array( $timestamp => $_events, );
			unset( $_events );
		} else {
			return $collapsed_events;
		}
	}

	// Collapse whatever events we have into an easier format to deal with
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
	foreach ( $events as $timestamp => $timestamp_events ) {
		// Skip non-event data that Core includes in the option
		if ( ! is_numeric( $timestamp ) ) {
			continue;
		}

		foreach ( $timestamp_events as $action => $action_instances ) {
			foreach ( $action_instances as $instance => $instance_args ) {
				$collapsed_events[] = array(
					'timestamp' => $timestamp,
					'action'    => $action,
					'instance'  => $instance,
					'args'      => $instance_args,
				);
			}
		}
	}

	return $collapsed_events;
}
50
51
52
53
54
55
56

/**
 * Parse request using Core's logic
 *
 * We have occasion to check the request before Core has done so, such as when preparing the environment to run a cron job
 */
function parse_request() {
57
58
59
60
61
62
63
	// Hold onto this as it won't change during the request
	static $parsed_request = null;
	if ( is_array( $parsed_request ) ) {
		return $parsed_request;
	}

	// Starting somewhere
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
	$rewrite_index = 'index.php';

	/**
	 * Start what's borrowed from Core
	 *
	 * References to $wp_rewrite->index were replaced with $rewrite_index, and whitespace updated, but otherwise, this is directly from WP::parse_request()
	 */
	$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
	list( $pathinfo ) = explode( '?', $pathinfo );
	$pathinfo = str_replace( "%", "%25", $pathinfo );

	list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
	$self = $_SERVER['PHP_SELF'];
	$home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
	$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );

	// Trim path info from the end and the leading home path from the
	// front. For path info requests, this leaves us with the requesting
	// filename, if any. For 404 requests, this leaves us with the
	// requested permalink.
	$req_uri = str_replace( $pathinfo, '', $req_uri );
	$req_uri = trim( $req_uri, '/' );
	$req_uri = preg_replace( $home_path_regex, '', $req_uri );
	$req_uri = trim( $req_uri, '/' );
	$pathinfo = trim( $pathinfo, '/' );
	$pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
	$pathinfo = trim( $pathinfo, '/' );
	$self = trim( $self, '/' );
	$self = preg_replace( $home_path_regex, '', $self );
	$self = trim( $self, '/' );

	// The requested permalink is in $pathinfo for path info requests and
	//  $req_uri for other requests.
	if ( ! empty( $pathinfo ) && !preg_match( '|^.*' . $rewrite_index . '$|', $pathinfo ) ) {
		$requested_path = $pathinfo;
	} else {
		// If the request uri is the index, blank it out so that we don't try to match it against a rule.
		if ( $req_uri == $rewrite_index ) {
			$req_uri = '';
		}

		$requested_path = $req_uri;
	}

	$requested_file = $req_uri;
	/**
	 * End what's borrowed from Core
	 */

	// Return array of data about the request
114
115
116
	$parsed_request = compact( 'requested_path', 'requested_file', 'self' );

	return $parsed_request;
117
}
118
119
120
121
122
123
124
125
126
127
128
129
130

/**
 * Consistently set flag Core uses to indicate cron execution is ongoing
 */
function set_doing_cron() {
	if ( ! defined( 'DOING_CRON' ) ) {
		define( 'DOING_CRON', true );
	}

	// WP 4.8 introduced the `wp_doing_cron()` function and filter
	// These can be used to override the `DOING_CRON` constant, which may cause problems for plugin's requests
	add_filter( 'wp_doing_cron', '__return_true', 99999 );
}