PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.1
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-cron.php

class-convertkit-cron.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.4.1, at includes/class-convertkit-cron.php

146 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Cron class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Provides functionality for manually running WordPress Cron events registered
11 * by this Plugin.
12 *
13 * @since 2.2.9
14 */
15 class ConvertKit_Cron {
16
17 /**
18 * Runs an existing scheduled event immediately through WordPress' Cron system.
19 *
20 * The scheduled event will still run as its original date and time,
21 * based on the event's existing schedule e.g. daily / weekly.
22 *
23 * @since 2.2.9
24 *
25 * @param string $event_name Event Name.
26 */
27 public function run( $event_name ) {
28
29 // Only allow Administrators to run events.
30 if ( ! current_user_can( 'manage_options' ) ) {
31 return new WP_Error(
32 'convertkit_cron_run_error',
33 esc_html__( 'You are not allowed to run cron events.', 'convertkit' )
34 );
35 }
36
37 // Get the event's arguments.
38 $event_args = $this->get_event_args( $event_name );
39
40 // Bail if no matching event could be found.
41 if ( is_wp_error( $event_args ) ) {
42 return $event_args;
43 }
44
45 // Schedule the event to run now.
46 return $this->schedule_event_now( $event_name, $event_args );
47
48 }
49
50 /**
51 * Returns an array of all scheduled WordPress Cron Events.
52 *
53 * @since 2.2.9
54 *
55 * @return array
56 */
57 private function get_all_events() {
58
59 $events = _get_cron_array();
60
61 if ( empty( $events ) ) {
62 $events = array();
63 }
64
65 return $events;
66
67 }
68
69 /**
70 * Returns the given event name's arguments array, if any were supplied when
71 * the event was registered using wp_schedule_event().
72 *
73 * @since 2.2.9
74 *
75 * @param string $event_name Event Name.
76 * @return WP_Error|array
77 */
78 private function get_event_args( $event_name ) {
79
80 // Fetch all scheduled events.
81 $events = $this->get_all_events();
82
83 // Find the scheduled event matching the given event name.
84 foreach ( $events as $timestamp => $scheduled_event ) {
85 // Skip if this event isn't the one we want.
86 if ( ! array_key_exists( $event_name, $scheduled_event ) ) {
87 continue;
88 }
89
90 // Found a matching event.
91 $event = reset( $scheduled_event[ $event_name ] );
92 return $event['args'];
93 }
94
95 // No event found.
96 return new WP_Error(
97 'convertkit_cron_get_event_args_error',
98 sprintf(
99 '%s %s %s',
100 esc_html__( 'The event', 'convertkit' ),
101 esc_html( $event_name ),
102 esc_html__( 'could not be found in WordPress\' cron.', 'convertkit' )
103 )
104 );
105
106 }
107
108 /**
109 * Schedules the given event name to run immediately in WordPress' Cron system.
110 *
111 * The scheduled event will still run as its original date and time,
112 * based on the event's existing schedule e.g. daily / weekly.
113 *
114 * @since 2.2.9
115 *
116 * @param string $event_name Event Name.
117 * @param array $event_args Event function arguments.
118 * @return WP_Error|bool
119 */
120 private function schedule_event_now( $event_name, $event_args ) {
121
122 // Clear the WordPress Cron transient.
123 delete_transient( 'doing_cron' );
124
125 // Fetch all scheduled events.
126 $events = $this->get_all_events();
127
128 // Set the timestamp to 1, to run it now.
129 $timestamp = 1;
130
131 // Define the key and add the event to the array of scheduled events.
132 $serialized_args = serialize( $event_args ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
133 $key = md5( $serialized_args );
134 $events[ $timestamp ][ $event_name ][ $key ] = array(
135 'schedule' => false,
136 'args' => $event_args,
137 );
138 ksort( $events );
139
140 // Update WordPress' Cron events, returning the result.
141 return _set_cron_array( $events, true );
142
143 }
144
145 }
146