WP Crontrol - Version 1.8.0

Version Description

Download this release

Release Info

Developer johnbillion
Plugin Icon 128x128 WP Crontrol
Version 1.8.0
Comparing to
See all releases

Code changes from version 1.7.1 to 1.8.0

COPYING DELETED
@@ -1,280 +0,0 @@
1
- GNU GENERAL PUBLIC LICENSE
2
- Version 2, June 1991
3
-
4
- Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6
- Everyone is permitted to copy and distribute verbatim copies
7
- of this license document, but changing it is not allowed.
8
-
9
- Preamble
10
-
11
- The licenses for most software are designed to take away your
12
- freedom to share and change it. By contrast, the GNU General Public
13
- License is intended to guarantee your freedom to share and change free
14
- software--to make sure the software is free for all its users. This
15
- General Public License applies to most of the Free Software
16
- Foundation's software and to any other program whose authors commit to
17
- using it. (Some other Free Software Foundation software is covered by
18
- the GNU Library General Public License instead.) You can apply it to
19
- your programs, too.
20
-
21
- When we speak of free software, we are referring to freedom, not
22
- price. Our General Public Licenses are designed to make sure that you
23
- have the freedom to distribute copies of free software (and charge for
24
- this service if you wish), that you receive source code or can get it
25
- if you want it, that you can change the software or use pieces of it
26
- in new free programs; and that you know you can do these things.
27
-
28
- To protect your rights, we need to make restrictions that forbid
29
- anyone to deny you these rights or to ask you to surrender the rights.
30
- These restrictions translate to certain responsibilities for you if you
31
- distribute copies of the software, or if you modify it.
32
-
33
- For example, if you distribute copies of such a program, whether
34
- gratis or for a fee, you must give the recipients all the rights that
35
- you have. You must make sure that they, too, receive or can get the
36
- source code. And you must show them these terms so they know their
37
- rights.
38
-
39
- We protect your rights with two steps: (1) copyright the software, and
40
- (2) offer you this license which gives you legal permission to copy,
41
- distribute and/or modify the software.
42
-
43
- Also, for each author's protection and ours, we want to make certain
44
- that everyone understands that there is no warranty for this free
45
- software. If the software is modified by someone else and passed on, we
46
- want its recipients to know that what they have is not the original, so
47
- that any problems introduced by others will not reflect on the original
48
- authors' reputations.
49
-
50
- Finally, any free program is threatened constantly by software
51
- patents. We wish to avoid the danger that redistributors of a free
52
- program will individually obtain patent licenses, in effect making the
53
- program proprietary. To prevent this, we have made it clear that any
54
- patent must be licensed for everyone's free use or not licensed at all.
55
-
56
- The precise terms and conditions for copying, distribution and
57
- modification follow.
58
-
59
- GNU GENERAL PUBLIC LICENSE
60
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
-
62
- 0. This License applies to any program or other work which contains
63
- a notice placed by the copyright holder saying it may be distributed
64
- under the terms of this General Public License. The "Program", below,
65
- refers to any such program or work, and a "work based on the Program"
66
- means either the Program or any derivative work under copyright law:
67
- that is to say, a work containing the Program or a portion of it,
68
- either verbatim or with modifications and/or translated into another
69
- language. (Hereinafter, translation is included without limitation in
70
- the term "modification".) Each licensee is addressed as "you".
71
-
72
- Activities other than copying, distribution and modification are not
73
- covered by this License; they are outside its scope. The act of
74
- running the Program is not restricted, and the output from the Program
75
- is covered only if its contents constitute a work based on the
76
- Program (independent of having been made by running the Program).
77
- Whether that is true depends on what the Program does.
78
-
79
- 1. You may copy and distribute verbatim copies of the Program's
80
- source code as you receive it, in any medium, provided that you
81
- conspicuously and appropriately publish on each copy an appropriate
82
- copyright notice and disclaimer of warranty; keep intact all the
83
- notices that refer to this License and to the absence of any warranty;
84
- and give any other recipients of the Program a copy of this License
85
- along with the Program.
86
-
87
- You may charge a fee for the physical act of transferring a copy, and
88
- you may at your option offer warranty protection in exchange for a fee.
89
-
90
- 2. You may modify your copy or copies of the Program or any portion
91
- of it, thus forming a work based on the Program, and copy and
92
- distribute such modifications or work under the terms of Section 1
93
- above, provided that you also meet all of these conditions:
94
-
95
- a) You must cause the modified files to carry prominent notices
96
- stating that you changed the files and the date of any change.
97
-
98
- b) You must cause any work that you distribute or publish, that in
99
- whole or in part contains or is derived from the Program or any
100
- part thereof, to be licensed as a whole at no charge to all third
101
- parties under the terms of this License.
102
-
103
- c) If the modified program normally reads commands interactively
104
- when run, you must cause it, when started running for such
105
- interactive use in the most ordinary way, to print or display an
106
- announcement including an appropriate copyright notice and a
107
- notice that there is no warranty (or else, saying that you provide
108
- a warranty) and that users may redistribute the program under
109
- these conditions, and telling the user how to view a copy of this
110
- License. (Exception: if the Program itself is interactive but
111
- does not normally print such an announcement, your work based on
112
- the Program is not required to print an announcement.)
113
-
114
- These requirements apply to the modified work as a whole. If
115
- identifiable sections of that work are not derived from the Program,
116
- and can be reasonably considered independent and separate works in
117
- themselves, then this License, and its terms, do not apply to those
118
- sections when you distribute them as separate works. But when you
119
- distribute the same sections as part of a whole which is a work based
120
- on the Program, the distribution of the whole must be on the terms of
121
- this License, whose permissions for other licensees extend to the
122
- entire whole, and thus to each and every part regardless of who wrote it.
123
-
124
- Thus, it is not the intent of this section to claim rights or contest
125
- your rights to work written entirely by you; rather, the intent is to
126
- exercise the right to control the distribution of derivative or
127
- collective works based on the Program.
128
-
129
- In addition, mere aggregation of another work not based on the Program
130
- with the Program (or with a work based on the Program) on a volume of
131
- a storage or distribution medium does not bring the other work under
132
- the scope of this License.
133
-
134
- 3. You may copy and distribute the Program (or a work based on it,
135
- under Section 2) in object code or executable form under the terms of
136
- Sections 1 and 2 above provided that you also do one of the following:
137
-
138
- a) Accompany it with the complete corresponding machine-readable
139
- source code, which must be distributed under the terms of Sections
140
- 1 and 2 above on a medium customarily used for software interchange; or,
141
-
142
- b) Accompany it with a written offer, valid for at least three
143
- years, to give any third party, for a charge no more than your
144
- cost of physically performing source distribution, a complete
145
- machine-readable copy of the corresponding source code, to be
146
- distributed under the terms of Sections 1 and 2 above on a medium
147
- customarily used for software interchange; or,
148
-
149
- c) Accompany it with the information you received as to the offer
150
- to distribute corresponding source code. (This alternative is
151
- allowed only for noncommercial distribution and only if you
152
- received the program in object code or executable form with such
153
- an offer, in accord with Subsection b above.)
154
-
155
- The source code for a work means the preferred form of the work for
156
- making modifications to it. For an executable work, complete source
157
- code means all the source code for all modules it contains, plus any
158
- associated interface definition files, plus the scripts used to
159
- control compilation and installation of the executable. However, as a
160
- special exception, the source code distributed need not include
161
- anything that is normally distributed (in either source or binary
162
- form) with the major components (compiler, kernel, and so on) of the
163
- operating system on which the executable runs, unless that component
164
- itself accompanies the executable.
165
-
166
- If distribution of executable or object code is made by offering
167
- access to copy from a designated place, then offering equivalent
168
- access to copy the source code from the same place counts as
169
- distribution of the source code, even though third parties are not
170
- compelled to copy the source along with the object code.
171
-
172
- 4. You may not copy, modify, sublicense, or distribute the Program
173
- except as expressly provided under this License. Any attempt
174
- otherwise to copy, modify, sublicense or distribute the Program is
175
- void, and will automatically terminate your rights under this License.
176
- However, parties who have received copies, or rights, from you under
177
- this License will not have their licenses terminated so long as such
178
- parties remain in full compliance.
179
-
180
- 5. You are not required to accept this License, since you have not
181
- signed it. However, nothing else grants you permission to modify or
182
- distribute the Program or its derivative works. These actions are
183
- prohibited by law if you do not accept this License. Therefore, by
184
- modifying or distributing the Program (or any work based on the
185
- Program), you indicate your acceptance of this License to do so, and
186
- all its terms and conditions for copying, distributing or modifying
187
- the Program or works based on it.
188
-
189
- 6. Each time you redistribute the Program (or any work based on the
190
- Program), the recipient automatically receives a license from the
191
- original licensor to copy, distribute or modify the Program subject to
192
- these terms and conditions. You may not impose any further
193
- restrictions on the recipients' exercise of the rights granted herein.
194
- You are not responsible for enforcing compliance by third parties to
195
- this License.
196
-
197
- 7. If, as a consequence of a court judgment or allegation of patent
198
- infringement or for any other reason (not limited to patent issues),
199
- conditions are imposed on you (whether by court order, agreement or
200
- otherwise) that contradict the conditions of this License, they do not
201
- excuse you from the conditions of this License. If you cannot
202
- distribute so as to satisfy simultaneously your obligations under this
203
- License and any other pertinent obligations, then as a consequence you
204
- may not distribute the Program at all. For example, if a patent
205
- license would not permit royalty-free redistribution of the Program by
206
- all those who receive copies directly or indirectly through you, then
207
- the only way you could satisfy both it and this License would be to
208
- refrain entirely from distribution of the Program.
209
-
210
- If any portion of this section is held invalid or unenforceable under
211
- any particular circumstance, the balance of the section is intended to
212
- apply and the section as a whole is intended to apply in other
213
- circumstances.
214
-
215
- It is not the purpose of this section to induce you to infringe any
216
- patents or other property right claims or to contest validity of any
217
- such claims; this section has the sole purpose of protecting the
218
- integrity of the free software distribution system, which is
219
- implemented by public license practices. Many people have made
220
- generous contributions to the wide range of software distributed
221
- through that system in reliance on consistent application of that
222
- system; it is up to the author/donor to decide if he or she is willing
223
- to distribute software through any other system and a licensee cannot
224
- impose that choice.
225
-
226
- This section is intended to make thoroughly clear what is believed to
227
- be a consequence of the rest of this License.
228
-
229
- 8. If the distribution and/or use of the Program is restricted in
230
- certain countries either by patents or by copyrighted interfaces, the
231
- original copyright holder who places the Program under this License
232
- may add an explicit geographical distribution limitation excluding
233
- those countries, so that distribution is permitted only in or among
234
- countries not thus excluded. In such case, this License incorporates
235
- the limitation as if written in the body of this License.
236
-
237
- 9. The Free Software Foundation may publish revised and/or new versions
238
- of the General Public License from time to time. Such new versions will
239
- be similar in spirit to the present version, but may differ in detail to
240
- address new problems or concerns.
241
-
242
- Each version is given a distinguishing version number. If the Program
243
- specifies a version number of this License which applies to it and "any
244
- later version", you have the option of following the terms and conditions
245
- either of that version or of any later version published by the Free
246
- Software Foundation. If the Program does not specify a version number of
247
- this License, you may choose any version ever published by the Free Software
248
- Foundation.
249
-
250
- 10. If you wish to incorporate parts of the Program into other free
251
- programs whose distribution conditions are different, write to the author
252
- to ask for permission. For software which is copyrighted by the Free
253
- Software Foundation, write to the Free Software Foundation; we sometimes
254
- make exceptions for this. Our decision will be guided by the two goals
255
- of preserving the free status of all derivatives of our free software and
256
- of promoting the sharing and reuse of software generally.
257
-
258
- NO WARRANTY
259
-
260
- 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
- FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
- OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
- PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
- OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
- TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
- PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
- REPAIR OR CORRECTION.
269
-
270
- 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
- WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
- REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
- INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
- OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
- TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
- YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
- PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
- POSSIBILITY OF SUCH DAMAGES.
279
-
280
- END OF TERMS AND CONDITIONS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
composer.json CHANGED
@@ -1,24 +1,38 @@
1
  {
2
- "name" : "johnbillion/wp-crontrol",
3
  "description": "WP Crontrol lets you view and control what's happening in the WP-Cron system.",
4
- "homepage" : "https://github.com/johnbillion/wp-crontrol/",
5
- "license" : "GPL-2.0+",
6
- "type" : "wordpress-plugin",
7
- "authors" : [
8
  {
9
- "name" : "John Blackbourn",
10
  "homepage": "https://johnblackbourn.com/"
11
  },
12
  {
13
- "name" : "Edward Dale",
14
  "homepage": "http://scompt.com/"
15
  }
16
  ],
 
 
 
 
17
  "require-dev": {
18
- "wp-coding-standards/wpcs": "^2",
19
- "dealerdirect/phpcodesniffer-composer-installer": "^0.5"
 
20
  },
21
  "require": {
 
22
  "composer/installers": "~1.0"
 
 
 
 
 
 
 
 
23
  }
24
  }
1
  {
2
+ "name": "johnbillion/wp-crontrol",
3
  "description": "WP Crontrol lets you view and control what's happening in the WP-Cron system.",
4
+ "homepage": "https://github.com/johnbillion/wp-crontrol/",
5
+ "license": "GPL-2.0+",
6
+ "type": "wordpress-plugin",
7
+ "authors": [
8
  {
9
+ "name": "John Blackbourn",
10
  "homepage": "https://johnblackbourn.com/"
11
  },
12
  {
13
+ "name": "Edward Dale",
14
  "homepage": "http://scompt.com/"
15
  }
16
  ],
17
+ "config": {
18
+ "sort-packages": true,
19
+ "preferred-install": "dist"
20
+ },
21
  "require-dev": {
22
+ "dealerdirect/phpcodesniffer-composer-installer": "^0.5",
23
+ "phpcompatibility/phpcompatibility-wp": "^2.0",
24
+ "wp-coding-standards/wpcs": "2.1.0"
25
  },
26
  "require": {
27
+ "php": ">=5.3.6",
28
  "composer/installers": "~1.0"
29
+ },
30
+ "scripts": {
31
+ "test:cs": [
32
+ "vendor/bin/phpcs -ps --colors --report-code --report-summary --report-width=80 --cache=tests/cache/phpcs ."
33
+ ],
34
+ "test": [
35
+ "@test:cs"
36
+ ]
37
  }
38
  }
css/wp-crontrol.css ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ @keyframes crontrol-rotating {
3
+ from {
4
+ transform: rotate(0deg);
5
+ }
6
+ to {
7
+ transform: rotate(360deg);
8
+ }
9
+ }
10
+
11
+ .crontrol-rotating {
12
+ animation: crontrol-rotating 3s linear infinite;
13
+ }
14
+
15
+ table.wp-list-table {
16
+ width: 100%;
17
+ max-width: 100%;
18
+ table-layout: auto;
19
+ overflow-x: auto;
20
+ -webkit-overflow-scrolling: touch;
21
+ -ms-overflow-style: -ms-autohiding-scrollbar;
22
+ }
23
+
24
+ .wp-list-table pre {
25
+ white-space: pre-wrap;
26
+ margin: 0;
27
+ }
28
+
29
+ .wp-list-table code {
30
+ background: transparent;
31
+ padding: 0;
32
+ }
33
+
34
+ .wp-list-table td.column-status {
35
+ white-space: nowrap;
36
+ }
37
+
38
+ .wp-list-table td.column-cb,
39
+ .wp-list-table tbody th {
40
+ border-left: 4px solid transparent;
41
+ }
42
+
43
+ .wp-list-table tr.crontrol-no-action th,
44
+ .wp-list-table tr.crontrol-stalled th,
45
+ .wp-list-table tr.crontrol-warning th {
46
+ border-color: #ffb900;
47
+ }
48
+
49
+ .wp-list-table tr.crontrol-error th {
50
+ border-color: #dc3232;
51
+ }
52
+
53
+ .status-crontrol-complete,
54
+ .wp-list-table tr.crontrol-complete td.column-status {
55
+ color: #080;
56
+ }
57
+
58
+ .status-crontrol-no-action,
59
+ .status-crontrol-stalled,
60
+ .status-crontrol-warning,
61
+ .wp-list-table tr.crontrol-no-action td.column-status,
62
+ .wp-list-table tr.crontrol-stalled td.column-status,
63
+ .wp-list-table tr.crontrol-warning td.column-status {
64
+ color: #c60;
65
+ }
66
+
67
+ .status-crontrol-emergency,
68
+ .status-crontrol-alert,
69
+ .status-crontrol-critical,
70
+ .status-crontrol-error,
71
+ .wp-list-table tr.crontrol-error td.column-status {
72
+ color: #a00;
73
+ }
74
+
75
+ .wp-list-table .column-https,
76
+ .wp-list-table .column-queries,
77
+ .wp-list-table .column-time {
78
+ text-align: end;
79
+ }
80
+
81
+ .column-time,
82
+ .column-ran,
83
+ .column-crontrol_next {
84
+ white-space: nowrap;
85
+ }
86
+
87
+ .row-actions .in-use {
88
+ color: #666;
89
+ }
js/wp-crontrol.js ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Functionality related to Crontrol.
3
+ *
4
+ * @package wp-crontrol
5
+ */
6
+
7
+ const header = document.getElementById( 'crontrol-header' );
8
+ const wpbody = document.getElementById( 'wpbody-content' );
9
+
10
+ if ( header && wpbody ) {
11
+ wpbody.prepend( header );
12
+ }
readme.txt → readme.md RENAMED
@@ -1,76 +1,79 @@
1
- === WP Crontrol ===
2
- Contributors: johnbillion, scompt
3
- Tags: admin, cron, plugin, control, wp-cron, crontrol
4
- Requires at least: 4.1
5
- Tested up to: 5.1
6
- Stable tag: 1.7.1
7
-
8
- WP Crontrol lets you view and control what's happening in the WP-Cron system.
9
-
10
- == Description ==
11
-
12
- WP Crontrol lets you view and control what's happening in the WP-Cron system. From the admin screens you can:
13
-
14
- * View all cron events along with their arguments, recurrence, callback functions, and when they are next due.
15
- * Edit, delete, and immediately run any cron events.
16
- * Add new cron events.
17
- * Bulk delete cron events.
18
- * Add, edit, and remove custom cron schedules.
19
-
20
- The admin screen will show you a warning message if your cron system doesn't appear to be working (for example if your server can't connect to itself to fire scheduled cron events).
21
-
22
- = Usage =
23
-
24
- 1. Go to the `Tools -> Cron Events` menu to manage cron events.
25
- 2. Go to the `Settings -> Cron Schedules` menu to manage cron schedules.
26
-
27
- == Frequently Asked Questions ==
28
-
29
- = What's the use of adding new cron schedules? =
30
-
31
- Cron schedules are used by WordPress and plugins for scheduling events to be executed at regular intervals. Intervals must be provided by the WordPress core or a plugin in order to be used. As an example, many backup plugins provide support for periodic backups. In order to do a weekly backup, a weekly cron schedule must be entered into WP Crontrol first and then a backup plugin can take advantage of it as an interval.
32
-
33
- = How do I create a new PHP cron event? =
34
-
35
- In the Tools -> Cron Events admin panel, click on the "Add PHP Cron Event" tab underneath the cron event table. In the form that appears, enter the schedule and next run time in the boxes. The event schedule is how often your event will be executed. If you don't see a good interval, then add one in the Settings -> Cron Schedules admin panel. In the "Hook code" area, enter the PHP code that should be run when your cron event is executed. You don't need to provide the PHP opening tag (`<?php`).
36
-
37
- = How do I create a new regular cron event? =
38
-
39
- There are two steps to getting a functioning cron event that executes regularly. The first step is telling WordPress about the hook. This is the part that WP Crontrol was created to provide. The second step is calling a function when your hook is executed.
40
-
41
- *Step One: Adding the hook*
42
-
43
- In the Tools -> Cron Events admin panel, enter the details of the hook. You're best off having a hookname that conforms to normal PHP variable naming conventions. The event schedule is how often your hook will be executed. If you don't see a good interval, then add one in the Settings -> Cron Schedules admin panel.
44
-
45
- *Step Two: Writing the function*
46
-
47
- This part takes place in PHP code (for example, in the `functions.php` file from your theme). To execute your hook, WordPress runs an [action](https://codex.wordpress.org/Plugin_API#Actions). For this reason, we need to tell WordPress which function to execute when this action is run. The following line accomplishes that:
48
-
49
- `add_action( 'my_hookname', 'my_function' );`
50
-
51
- The next step is to write your function. Here's a simple example:
52
-
53
- `function my_function() {
54
- wp_mail( 'hello@example.com', 'WP Crontrol', 'WP Crontrol rocks!' );
55
- }`
56
-
57
- = Which users can manage cron events and schedules? =
58
-
59
- Only users with the `manage_options` capability can manage cron events and schedules. By default, only Administrators have this capability.
60
-
61
- = Which users can manage PHP cron events? =
62
-
63
- Only users with the `edit_files` capability can manage PHP cron events. By default, only Administrators have this capability, and with Multisite enabled only Super Admins have this capability.
64
-
65
- = Are any WP-CLI commands available? =
66
-
67
- The cron commands which were previously included in WP Crontrol are now part of WP-CLI (since 0.16), so this plugin no longer provides any WP-CLI commands. See `wp help cron` for more info.
68
-
69
- == Screenshots ==
70
-
71
- 1. New cron events can be added, modified, deleted, and executed.
72
- 2. New cron schedules can be added, giving plugin developers more options when scheduling events.
73
-
74
- == Changelog ==
75
-
76
- For WP Crontrol's changelog, please see [the Releases page on GitHub](https://github.com/johnbillion/wp-crontrol/releases).
 
 
 
1
+ # WP Crontrol #
2
+
3
+ Contributors: johnbillion, scompt
4
+ Tags: cron, wp-cron, crontrol, debug
5
+ Requires at least: 4.1
6
+ Tested up to: 5.4
7
+ Stable tag: 1.8.0
8
+ Requires PHP: 5.3
9
+
10
+ WP Crontrol lets you view and control what's happening in the WP-Cron system.
11
+
12
+ ## Description ##
13
+
14
+ WP Crontrol lets you view and control what's happening in the WP-Cron system. From the admin screens you can:
15
+
16
+ * View all cron events along with their arguments, recurrence, callback functions, and when they are next due.
17
+ * Edit, delete, and immediately run any cron events.
18
+ * Add new cron events.
19
+ * Bulk delete cron events.
20
+ * Add, edit, and remove custom cron schedules.
21
+
22
+ The admin screen will show you a warning message if your cron system doesn't appear to be working (for example if your server can't connect to itself to fire scheduled cron events).
23
+
24
+ ### Usage ###
25
+
26
+ 1. Go to the `Tools → Cron Events` menu to manage cron events.
27
+ 2. Go to the `Settings → Cron Schedules` menu to manage cron schedules.
28
+
29
+ ## Frequently Asked Questions ##
30
+
31
+ ### What's the use of adding new cron schedules? ###
32
+
33
+ Cron schedules are used by WordPress and plugins for scheduling events to be executed at regular intervals. Intervals must be provided by the WordPress core or a plugin in order to be used. As an example, many backup plugins provide support for periodic backups. In order to do a weekly backup, a weekly cron schedule must be entered into WP Crontrol first and then a backup plugin can take advantage of it as an interval.
34
+
35
+ ### How do I create a new PHP cron event? ###
36
+
37
+ In the Tools Cron Events admin panel, click on the "Add PHP Cron Event" tab underneath the cron event table. In the form that appears, enter the schedule and next run time in the boxes. The event schedule is how often your event will be executed. If you don't see a good interval, then add one in the Settings → Cron Schedules admin panel. In the "Hook code" area, enter the PHP code that should be run when your cron event is executed. You don't need to provide the PHP opening tag (`<?php`).
38
+
39
+ ### How do I create a new regular cron event? ###
40
+
41
+ There are two steps to getting a functioning cron event that executes regularly. The first step is telling WordPress about the hook. This is the part that WP Crontrol was created to provide. The second step is calling a function when your hook is executed.
42
+
43
+ *Step One: Adding the hook*
44
+
45
+ In the Tools → Cron Events admin panel, enter the details of the hook. You're best off having a hookname that conforms to normal PHP variable naming conventions. The event schedule is how often your hook will be executed. If you don't see a good interval, then add one in the Settings → Cron Schedules admin panel.
46
+
47
+ *Step Two: Writing the function*
48
+
49
+ This part takes place in PHP code (for example, in the `functions.php` file from your theme). To execute your hook, WordPress runs an action. For this reason, we need to tell WordPress which function to execute when this action is run. The following line accomplishes that:
50
+
51
+ add_action( 'my_hookname', 'my_function' );
52
+
53
+ The next step is to write your function. Here's a simple example:
54
+
55
+ function my_function() {
56
+ wp_mail( 'hello@example.com', 'WP Crontrol', 'WP Crontrol rocks!' );
57
+ }
58
+
59
+ ### Which users can manage cron events and schedules? ###
60
+
61
+ Only users with the `manage_options` capability can manage cron events and schedules. By default, only Administrators have this capability.
62
+
63
+ ### Which users can manage PHP cron events? ###
64
+
65
+ Only users with the `edit_files` capability can manage PHP cron events. By default, only Administrators have this capability, and with Multisite enabled only Super Admins have this capability.
66
+
67
+ ### Are any WP-CLI commands available? ###
68
+
69
+ The cron commands which were previously included in WP Crontrol are now part of WP-CLI (since 0.16), so this plugin no longer provides any WP-CLI commands. See `wp help cron` for more info.
70
+
71
+ ## Screenshots ##
72
+
73
+ 1. New cron events can be added, modified, deleted, and executed<br>![](.wordpress-org/screenshot-1.png)
74
+
75
+ 2. New cron schedules can be added, giving plugin developers more options when scheduling events<br>![](.wordpress-org/screenshot-2.png)
76
+
77
+ ## Changelog ##
78
+
79
+ For WP Crontrol's changelog, please see [the Releases page on GitHub](https://github.com/johnbillion/wp-crontrol/releases).
src/event-list-table.php ADDED
@@ -0,0 +1,428 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * List table for cron events.
4
+ *
5
+ * @package wp-crontrol
6
+ */
7
+
8
+ namespace Crontrol;
9
+
10
+ use stdClass;
11
+
12
+ require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
13
+
14
+ /**
15
+ * Cron event list table class.
16
+ */
17
+ class Event_List_Table extends \WP_List_Table {
18
+
19
+ /**
20
+ * Array of cron event hooks that are persistently added by WordPress core.
21
+ *
22
+ * @var string[] Array of hook names.
23
+ */
24
+ protected static $persistent_core_hooks;
25
+
26
+ /**
27
+ * Whether the current user has the capability to edit files.
28
+ *
29
+ * @var bool Whether the user can edit files.
30
+ */
31
+ protected static $can_edit_files;
32
+
33
+ /**
34
+ * Array of the count of each hook.
35
+ *
36
+ * @var int[] Array of count of each hooked, keyed by hook name.
37
+ */
38
+ protected static $count_by_hook;
39
+
40
+ /**
41
+ * Constructor.
42
+ */
43
+ public function __construct() {
44
+ parent::__construct( array(
45
+ 'singular' => 'crontrol-event',
46
+ 'plural' => 'crontrol-events',
47
+ 'ajax' => false,
48
+ 'screen' => 'crontrol-events',
49
+ ) );
50
+ }
51
+
52
+ /**
53
+ * Prepares the list table items and arguments.
54
+ */
55
+ public function prepare_items() {
56
+ self::$persistent_core_hooks = get_persistent_core_hooks();
57
+ self::$can_edit_files = current_user_can( 'edit_files' );
58
+ self::$count_by_hook = Event\count_by_hook();
59
+
60
+ $events = Event\get();
61
+
62
+ if ( ! empty( $_GET['s'] ) ) {
63
+ $s = sanitize_text_field( wp_unslash( $_GET['s'] ) );
64
+
65
+ $events = array_filter( $events, function( $event ) use ( $s ) {
66
+ return ( false !== strpos( $event->hook, $s ) );
67
+ } );
68
+ }
69
+
70
+ $count = count( $events );
71
+ $per_page = 50;
72
+ $offset = ( $this->get_pagenum() - 1 ) * $per_page;
73
+
74
+ $this->items = array_slice( $events, $offset, $per_page );
75
+
76
+ $this->set_pagination_args( array(
77
+ 'total_items' => $count,
78
+ 'per_page' => $per_page,
79
+ 'total_pages' => ceil( $count / $per_page ),
80
+ ) );
81
+ }
82
+
83
+ /**
84
+ * Returns an array of column names for the table.
85
+ *
86
+ * @return string[] Array of column names keyed by their ID.
87
+ */
88
+ public function get_columns() {
89
+ return array(
90
+ 'cb' => '<input type="checkbox" />',
91
+ 'crontrol_hook' => __( 'Hook', 'wp-crontrol' ),
92
+ 'crontrol_args' => __( 'Arguments', 'wp-crontrol' ),
93
+ 'crontrol_next' => sprintf(
94
+ /* translators: %s: UTC offset */
95
+ __( 'Next Run (%s)', 'wp-crontrol' ),
96
+ get_utc_offset()
97
+ ),
98
+ 'crontrol_actions' => __( 'Action', 'wp-crontrol' ),
99
+ 'crontrol_recurrence' => __( 'Recurrence', 'wp-crontrol' ),
100
+ );
101
+ }
102
+
103
+ /**
104
+ * Returns an array of CSS class names for the table.
105
+ *
106
+ * @return string[] Array of class names.
107
+ */
108
+ protected function get_table_classes() {
109
+ return array( 'widefat', 'striped', $this->_args['plural'] );
110
+ }
111
+
112
+ /**
113
+ * Get an associative array ( option_name => option_title ) with the list
114
+ * of bulk actions available on this table.
115
+ *
116
+ * @since 3.1.0
117
+ *
118
+ * @return array
119
+ */
120
+ protected function get_bulk_actions() {
121
+ return array(
122
+ 'delete_crons' => esc_html__( 'Delete', 'wp-crontrol' ),
123
+ );
124
+ }
125
+
126
+ /**
127
+ * Generates content for a single row of the table.
128
+ *
129
+ * @param object $event The current event.
130
+ */
131
+ public function single_row( $event ) {
132
+ $classes = array();
133
+
134
+ if ( ( 'crontrol_cron_job' === $event->hook ) && ! empty( $event->args['syntax_error_message'] ) ) {
135
+ $classes[] = 'crontrol-error';
136
+ }
137
+
138
+ $schedule_name = ( $event->interval ? Event\get_schedule_name( $event ) : false );
139
+
140
+ if ( is_wp_error( $schedule_name ) ) {
141
+ $classes[] = 'crontrol-error';
142
+ }
143
+
144
+ if ( ! get_hook_callbacks( $event->hook ) ) {
145
+ $classes[] = 'crontrol-warning';
146
+ }
147
+
148
+ if ( Event\is_late( $event ) ) {
149
+ $classes[] = 'crontrol-warning';
150
+ }
151
+
152
+ printf(
153
+ '<tr class="%s">',
154
+ esc_attr( implode( ' ', $classes ) )
155
+ );
156
+
157
+ $this->single_row_columns( $event );
158
+ echo '</tr>';
159
+ }
160
+
161
+ /**
162
+ * Generates and displays row action links for the table.
163
+ *
164
+ * @param stdClass $event The cron event for the current row.
165
+ * @param string $column_name Current column name.
166
+ * @param string $primary Primary column name.
167
+ * @return string The row actions HTML.
168
+ */
169
+ protected function handle_row_actions( $event, $column_name, $primary ) {
170
+ if ( $primary !== $column_name ) {
171
+ return '';
172
+ }
173
+
174
+ $links = array();
175
+
176
+ if ( ( 'crontrol_cron_job' !== $event->hook ) || self::$can_edit_files ) {
177
+ $link = array(
178
+ 'page' => 'crontrol_admin_manage_page',
179
+ 'action' => 'edit-cron',
180
+ 'id' => rawurlencode( $event->hook ),
181
+ 'sig' => rawurlencode( $event->sig ),
182
+ 'next_run_utc' => rawurlencode( $event->time ),
183
+ );
184
+ $link = add_query_arg( $link, admin_url( 'tools.php' ) );
185
+
186
+ $links[] = "<a href='" . esc_url( $link ) . "'>" . esc_html__( 'Edit', 'wp-crontrol' ) . '</a>';
187
+ }
188
+
189
+ $link = array(
190
+ 'page' => 'crontrol_admin_manage_page',
191
+ 'action' => 'run-cron',
192
+ 'id' => rawurlencode( $event->hook ),
193
+ 'sig' => rawurlencode( $event->sig ),
194
+ 'next_run_utc' => rawurlencode( $event->time ),
195
+ );
196
+ $link = add_query_arg( $link, admin_url( 'tools.php' ) );
197
+ $link = wp_nonce_url( $link, "run-cron_{$event->hook}_{$event->sig}" );
198
+
199
+ $links[] = "<a href='" . esc_url( $link ) . "'>" . esc_html__( 'Run Now', 'wp-crontrol' ) . '</a>';
200
+
201
+ if ( ! in_array( $event->hook, self::$persistent_core_hooks, true ) && ( ( 'crontrol_cron_job' !== $event->hook ) || self::$can_edit_files ) ) {
202
+ $link = array(
203
+ 'page' => 'crontrol_admin_manage_page',
204
+ 'action' => 'delete-cron',
205
+ 'id' => rawurlencode( $event->hook ),
206
+ 'sig' => rawurlencode( $event->sig ),
207
+ 'next_run_utc' => rawurlencode( $event->time ),
208
+ );
209
+ $link = add_query_arg( $link, admin_url( 'tools.php' ) );
210
+ $link = wp_nonce_url( $link, "delete-cron_{$event->hook}_{$event->sig}_{$event->time}" );
211
+
212
+ $links[] = "<span class='delete'><a href='" . esc_url( $link ) . "'>" . esc_html__( 'Delete', 'wp-crontrol' ) . '</a></span>';
213
+ }
214
+
215
+ if ( function_exists( 'wp_unschedule_hook' ) && ! in_array( $event->hook, self::$persistent_core_hooks, true ) && ( 'crontrol_cron_job' !== $event->hook ) ) {
216
+ if ( self::$count_by_hook[ $event->hook ] > 1 ) {
217
+ $link = array(
218
+ 'page' => 'crontrol_admin_manage_page',
219
+ 'action' => 'delete-hook',
220
+ 'id' => rawurlencode( $event->hook ),
221
+ );
222
+ $link = add_query_arg( $link, admin_url( 'tools.php' ) );
223
+ $link = wp_nonce_url( $link, "delete-hook_{$event->hook}" );
224
+
225
+ $text = sprintf(
226
+ /* translators: %s: Number of events with a given name */
227
+ _n( 'Delete All %s', 'Delete All %s', self::$count_by_hook[ $event->hook ], 'wp-crontrol' ),
228
+ number_format_i18n( self::$count_by_hook[ $event->hook ] )
229
+ );
230
+ $links[] = "<span class='delete'><a href='" . esc_url( $link ) . "'>" . esc_html( $text ) . '</a></span>';
231
+ }
232
+ }
233
+
234
+ return $this->row_actions( $links );
235
+ }
236
+
237
+ /**
238
+ * Outputs the checkbox cell of a table row.
239
+ *
240
+ * @param stdClass $event The cron event for the current row.
241
+ */
242
+ protected function column_cb( $event ) {
243
+ if ( ! in_array( $event->hook, self::$persistent_core_hooks, true ) && ( ( 'crontrol_cron_job' !== $event->hook ) || self::$can_edit_files ) ) {
244
+ ?>
245
+ <label class="screen-reader-text" for="">
246
+ <?php printf( esc_html__( 'Select this row', 'wp-crontrol' ) ); ?>
247
+ </label>
248
+ <?php
249
+ printf(
250
+ '<input type="checkbox" name="delete[%1$s][%2$s]" value="%3$s">',
251
+ esc_attr( $event->time ),
252
+ esc_attr( rawurlencode( $event->hook ) ),
253
+ esc_attr( $event->sig )
254
+ );
255
+ ?>
256
+ <?php
257
+ }
258
+ }
259
+
260
+ /**
261
+ * Returns the output for the hook name cell of a table row.
262
+ *
263
+ * @param stdClass $event The cron event for the current row.
264
+ * @return string The cell output.
265
+ */
266
+ protected function column_crontrol_hook( $event ) {
267
+ if ( 'crontrol_cron_job' === $event->hook ) {
268
+ if ( ! empty( $event->args['name'] ) ) {
269
+ /* translators: 1: The name of the PHP cron event. */
270
+ return '<em>' . esc_html( sprintf( __( 'PHP Cron (%s)', 'wp-crontrol' ), $event->args['name'] ) ) . '</em>';
271
+ } else {
272
+ return '<em>' . esc_html__( 'PHP Cron', 'wp-crontrol' ) . '</em>';
273
+ }
274
+ }
275
+
276
+ return esc_html( $event->hook );
277
+ }
278
+
279
+ /**
280
+ * Returns the output for the arguments cell of a table row.
281
+ *
282
+ * @param stdClass $event The cron event for the current row.
283
+ * @return string The cell output.
284
+ */
285
+ protected function column_crontrol_args( $event ) {
286
+ if ( ! empty( $event->args ) ) {
287
+ $args = json_output( $event->args );
288
+ }
289
+
290
+ if ( 'crontrol_cron_job' === $event->hook ) {
291
+ $return = '<em>' . esc_html__( 'PHP Code', 'wp-crontrol' ) . '</em>';
292
+
293
+ if ( ! empty( $event->args['syntax_error_message'] ) ) {
294
+ $return .= '<br><span class="status-crontrol-error"><span class="dashicons dashicons-warning" aria-hidden="true"></span> ';
295
+ $return .= sprintf(
296
+ /* translators: 1: Line number, 2: Error message text */
297
+ esc_html__( 'Line %1$s: %2$s', 'wp-crontrol' ),
298
+ esc_html( number_format_i18n( $event->args['syntax_error_line'] ) ),
299
+ esc_html( $event->args['syntax_error_message'] )
300
+ );
301
+ $return .= '</span>';
302
+ }
303
+
304
+ if ( ! empty( $event->args['code'] ) ) {
305
+ $lines = explode( "\n", trim( $event->args['code'] ) );
306
+ $code = reset( $lines );
307
+ $code = substr( $code, 0, 50 );
308
+
309
+ $return .= '<br>';
310
+ $return .= sprintf(
311
+ '<code>%s</code>&hellip;',
312
+ esc_html( $code )
313
+ );
314
+ }
315
+
316
+ return $return;
317
+ } else {
318
+ if ( empty( $event->args ) ) {
319
+ return sprintf(
320
+ '<em>%s</em>',
321
+ esc_html__( 'None', 'wp-crontrol' )
322
+ );
323
+ } else {
324
+ return sprintf(
325
+ '<pre>%s</pre>',
326
+ esc_html( $args )
327
+ );
328
+ }
329
+ }
330
+ }
331
+
332
+ /**
333
+ * Returns the output for the actions cell of a table row.
334
+ *
335
+ * @param stdClass $event The cron event for the current row.
336
+ * @return string The cell output.
337
+ */
338
+ protected function column_crontrol_actions( $event ) {
339
+ $hook_callbacks = get_hook_callbacks( $event->hook );
340
+
341
+ if ( 'crontrol_cron_job' === $event->hook ) {
342
+ return '<em>' . esc_html__( 'WP Crontrol', 'wp-crontrol' ) . '</em>';
343
+ } elseif ( ! empty( $hook_callbacks ) ) {
344
+ $callbacks = array();
345
+
346
+ foreach ( $hook_callbacks as $callback ) {
347
+ $callbacks[] = output_callback( $callback );
348
+ }
349
+
350
+ return implode( '<br>', $callbacks ); // WPCS:: XSS ok.
351
+ } else {
352
+ return sprintf(
353
+ '<span class="status-crontrol-warning"><span class="dashicons dashicons-warning" aria-hidden="true"></span> %s</span>',
354
+ esc_html__( 'None', 'wp-crontrol' )
355
+ );
356
+ }
357
+ }
358
+
359
+ /**
360
+ * Returns the output for the next run cell of a table row.
361
+ *
362
+ * @param stdClass $event The cron event for the current row.
363
+ * @return string The cell output.
364
+ */
365
+ protected function column_crontrol_next( $event ) {
366
+ $date_utc = gmdate( 'Y-m-d\TH:i:s+00:00', $event->time );
367
+ $date_local = get_date_from_gmt( date( 'Y-m-d H:i:s', $event->time ), 'Y-m-d H:i:s' );
368
+
369
+ $time = sprintf(
370
+ '<time datetime="%1$s">%2$s</time>',
371
+ esc_attr( $date_utc ),
372
+ esc_html( $date_local )
373
+ );
374
+
375
+ $until = $event->time - time();
376
+ $late = Event\is_late( $event );
377
+
378
+ if ( $late ) {
379
+ // Show a warning for events that are late.
380
+ $ago = sprintf(
381
+ /* translators: %s: Time period, for example "8 minutes" */
382
+ __( '%s ago', 'wp-crontrol' ),
383
+ interval( abs( $until ) )
384
+ );
385
+ return sprintf(
386
+ '%s<br><span class="status-crontrol-warning"><span class="dashicons dashicons-warning" aria-hidden="true"></span> %s</span>',
387
+ $time,
388
+ esc_html( $ago )
389
+ );
390
+ }
391
+
392
+ return sprintf(
393
+ '%s<br>%s',
394
+ $time,
395
+ esc_html( interval( $until ) )
396
+ );
397
+ }
398
+
399
+ /**
400
+ * Returns the output for the recurrence cell of a table row.
401
+ *
402
+ * @param stdClass $event The cron event for the current row.
403
+ * @return string The cell output.
404
+ */
405
+ protected function column_crontrol_recurrence( $event ) {
406
+ if ( $event->schedule ) {
407
+ $schedule_name = Event\get_schedule_name( $event );
408
+ if ( is_wp_error( $schedule_name ) ) {
409
+ return sprintf(
410
+ '<span class="status-crontrol-error"><span class="dashicons dashicons-warning" aria-hidden="true"></span> %s</span>',
411
+ esc_html( $schedule_name->get_error_message() )
412
+ );
413
+ } else {
414
+ return esc_html( $schedule_name );
415
+ }
416
+ } else {
417
+ return esc_html__( 'Non-repeating', 'wp-crontrol' );
418
+ }
419
+ }
420
+
421
+ /**
422
+ * Outputs a message when there are no items to show in the table.
423
+ */
424
+ public function no_items() {
425
+ esc_html_e( 'There are currently no scheduled cron events.', 'wp-crontrol' );
426
+ }
427
+
428
+ }
src/event.php ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Functions related to cron events.
4
+ *
5
+ * @package wp-crontrol
6
+ */
7
+
8
+ namespace Crontrol\Event;
9
+
10
+ use stdClass;
11
+ use Crontrol\Schedule;
12
+ use WP_Error;
13
+
14
+ /**
15
+ * Executes a cron event immediately.
16
+ *
17
+ * Executes an event by scheduling a new single event with the same arguments.
18
+ *
19
+ * @param string $hookname The hook name of the cron event to run.
20
+ * @param string $sig The cron event signature.
21
+ * @return bool Whether the execution was successful.
22
+ */
23
+ function run( $hookname, $sig ) {
24
+ $crons = _get_cron_array();
25
+ foreach ( $crons as $time => $cron ) {
26
+ if ( isset( $cron[ $hookname ][ $sig ] ) ) {
27
+ $args = $cron[ $hookname ][ $sig ]['args'];
28
+ delete_transient( 'doing_cron' );
29
+ $scheduled = wp_schedule_single_event( time() - 1, $hookname, $args ); // UTC
30
+
31
+ if ( false === $scheduled ) {
32
+ return $scheduled;
33
+ }
34
+
35
+ spawn_cron();
36
+
37
+ sleep( 1 );
38
+
39
+ return true;
40
+ }
41
+ }
42
+ return false;
43
+ }
44
+
45
+ /**
46
+ * Adds a new cron event.
47
+ *
48
+ * @param string $next_run_local The time that the event should be run at, in the site's timezone.
49
+ * @param string $schedule The recurrence of the cron event.
50
+ * @param string $hookname The name of the hook to execute.
51
+ * @param array $args Arguments to add to the cron event.
52
+ * @return bool Whether the addition was successful.
53
+ */
54
+ function add( $next_run_local, $schedule, $hookname, array $args ) {
55
+ $next_run_local = strtotime( $next_run_local, current_time( 'timestamp' ) );
56
+
57
+ if ( false === $next_run_local ) {
58
+ return false;
59
+ }
60
+
61
+ $next_run_utc = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', $next_run_local ), 'U' );
62
+
63
+ if ( ! is_array( $args ) ) {
64
+ $args = array();
65
+ }
66
+
67
+ if ( 'crontrol_cron_job' === $hookname && ! empty( $args['code'] ) && class_exists( '\ParseError' ) ) {
68
+ try {
69
+ // phpcs:ignore Squiz.PHP.Eval.Discouraged
70
+ eval( sprintf(
71
+ 'return true; %s',
72
+ $args['code']
73
+ ) );
74
+ // phpcs:ignore PHPCompatibility.Classes.NewClasses.parseerrorFound
75
+ } catch ( \ParseError $e ) {
76
+ $args['syntax_error_message'] = $e->getMessage();
77
+ $args['syntax_error_line'] = $e->getLine();
78
+ }
79
+ }
80
+
81
+ if ( '_oneoff' === $schedule ) {
82
+ return ( false !== wp_schedule_single_event( $next_run_utc, $hookname, $args ) );
83
+ } else {
84
+ return ( false !== wp_schedule_event( $next_run_utc, $schedule, $hookname, $args ) );
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Deletes a cron event.
90
+ *
91
+ * @param string $to_delete The hook name of the event to delete.
92
+ * @param string $sig The cron event signature.
93
+ * @param string $next_run_utc The UTC time that the event would be run at.
94
+ * @return bool Whether the deletion was successful.
95
+ */
96
+ function delete( $to_delete, $sig, $next_run_utc ) {
97
+ $crons = _get_cron_array();
98
+ if ( isset( $crons[ $next_run_utc ][ $to_delete ][ $sig ] ) ) {
99
+ $args = $crons[ $next_run_utc ][ $to_delete ][ $sig ]['args'];
100
+ $unscheduled = wp_unschedule_event( $next_run_utc, $to_delete, $args );
101
+
102
+ if ( false === $unscheduled ) {
103
+ return $unscheduled;
104
+ }
105
+
106
+ return true;
107
+ }
108
+ return false;
109
+ }
110
+
111
+ /**
112
+ * Returns a flattened array of cron events.
113
+ *
114
+ * @return object[] An array of cron event objects.
115
+ */
116
+ function get() {
117
+ $crons = _get_cron_array();
118
+ $events = array();
119
+
120
+ if ( empty( $crons ) ) {
121
+ return array();
122
+ }
123
+
124
+ foreach ( $crons as $time => $cron ) {
125
+ foreach ( $cron as $hook => $dings ) {
126
+ foreach ( $dings as $sig => $data ) {
127
+
128
+ // This is a prime candidate for a Crontrol_Event class but I'm not bothering currently.
129
+ $events[ "$hook-$sig-$time" ] = (object) array(
130
+ 'hook' => $hook,
131
+ 'time' => $time, // UTC
132
+ 'sig' => $sig,
133
+ 'args' => $data['args'],
134
+ 'schedule' => $data['schedule'],
135
+ 'interval' => isset( $data['interval'] ) ? $data['interval'] : null,
136
+ );
137
+
138
+ }
139
+ }
140
+ }
141
+
142
+ // Ensure events are always returned in date descending order.
143
+ // External cron runners such as Cavalcade don't guarantee events are returned in order of time.
144
+ uasort( $events, function( $a, $b ) {
145
+ if ( $a->time === $b->time ) {
146
+ return 0;
147
+ } else {
148
+ return ( $a->time > $b->time ) ? 1 : -1;
149
+ }
150
+ } );
151
+
152
+ return $events;
153
+ }
154
+
155
+ /**
156
+ * Returns an array of the number of events for each hook.
157
+ *
158
+ * @return int[] Array of number of events for each hook, keyed by the hook name.
159
+ */
160
+ function count_by_hook() {
161
+ $crons = _get_cron_array();
162
+ $events = array();
163
+
164
+ if ( empty( $crons ) ) {
165
+ return array();
166
+ }
167
+
168
+ foreach ( $crons as $time => $cron ) {
169
+ foreach ( $cron as $hook => $dings ) {
170
+ if ( ! isset( $events[ $hook ] ) ) {
171
+ $events[ $hook ] = 0;
172
+ }
173
+
174
+ $events[ $hook ] += count( $dings );
175
+ }
176
+ }
177
+
178
+ return $events;
179
+ }
180
+
181
+ /**
182
+ * Returns the schedule display name for a given event.
183
+ *
184
+ * @param stdClass $event A WP-Cron event.
185
+ * @return string|WP_Error The interval display name, or a WP_Error object if no such schedule exists.
186
+ */
187
+ function get_schedule_name( stdClass $event ) {
188
+ $schedules = Schedule\get();
189
+
190
+ if ( isset( $schedules[ $event->schedule ] ) ) {
191
+ return $schedules[ $event->schedule ]['display'];
192
+ }
193
+
194
+ return new WP_Error( 'unknown_schedule', sprintf(
195
+ /* translators: %s: Schedule name */
196
+ __( 'Unknown (%s)', 'wp-crontrol' ),
197
+ $event->schedule
198
+ ) );
199
+ }
200
+
201
+ /**
202
+ * Determines whether an event is late.
203
+ *
204
+ * An event which has missed its schedule by more than 10 minutes is considered late.
205
+ *
206
+ * @param stdClass $event The event.
207
+ * @return bool Whether the event is late.
208
+ */
209
+ function is_late( $event ) {
210
+ $until = $event->time - time();
211
+
212
+ return ( $until < ( 0 - ( 10 * MINUTE_IN_SECONDS ) ) );
213
+ }
src/schedule-list-table.php ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * List table for cron schedules.
4
+ *
5
+ * @package wp-crontrol
6
+ */
7
+
8
+ namespace Crontrol;
9
+
10
+ require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
11
+
12
+ /**
13
+ * Cron schedule list table class.
14
+ */
15
+ class Schedule_List_Table extends \WP_List_Table {
16
+
17
+ /**
18
+ * Array of cron event schedules that are added by WordPress core.
19
+ *
20
+ * @var string[] Array of schedule names.
21
+ */
22
+ protected static $core_schedules;
23
+
24
+ /**
25
+ * Array of cron event schedule names that are in use by events.
26
+ *
27
+ * @var string[] Array of schedule names.
28
+ */
29
+ protected static $used_schedules;
30
+
31
+ /**
32
+ * Constructor.
33
+ */
34
+ public function __construct() {
35
+ parent::__construct( array(
36
+ 'singular' => 'crontrol-schedule',
37
+ 'plural' => 'crontrol-schedules',
38
+ 'ajax' => false,
39
+ 'screen' => 'crontrol-schedules',
40
+ ) );
41
+ }
42
+
43
+ /**
44
+ * Prepares the list table items and arguments.
45
+ */
46
+ public function prepare_items() {
47
+ $schedules = Schedule\get();
48
+ $count = count( $schedules );
49
+
50
+ self::$core_schedules = get_core_schedules();
51
+ self::$used_schedules = array_unique( wp_list_pluck( Event\get(), 'schedule' ) );
52
+
53
+ $this->items = $schedules;
54
+
55
+ $this->set_pagination_args( array(
56
+ 'total_items' => $count,
57
+ 'per_page' => -1,
58
+ 'total_pages' => 1,
59
+ ) );
60
+ }
61
+
62
+ /**
63
+ * Returns an array of column names for the table.
64
+ *
65
+ * @return string[] Array of column names keyed by their ID.
66
+ */
67
+ public function get_columns() {
68
+ return array(
69
+ 'crontrol_name' => __( 'Name', 'wp-crontrol' ),
70
+ 'crontrol_interval' => __( 'Interval', 'wp-crontrol' ),
71
+ 'crontrol_display' => __( 'Display Name', 'wp-crontrol' ),
72
+ );
73
+ }
74
+
75
+ /**
76
+ * Returns an array of CSS class names for the table.
77
+ *
78
+ * @return string[] Array of class names.
79
+ */
80
+ protected function get_table_classes() {
81
+ return array( 'widefat', 'striped', $this->_args['plural'] );
82
+ }
83
+
84
+ /**
85
+ * Generates and displays row action links for the table.
86
+ *
87
+ * @param array $schedule The schedule for the current row.
88
+ * @param string $column_name Current column name.
89
+ * @param string $primary Primary column name.
90
+ * @return string The row actions HTML.
91
+ */
92
+ protected function handle_row_actions( $schedule, $column_name, $primary ) {
93
+ if ( $primary !== $column_name ) {
94
+ return '';
95
+ }
96
+
97
+ $links = array();
98
+ $new_scheds = get_option( 'crontrol_schedules', array() );
99
+
100
+ if ( in_array( $schedule['name'], self::$core_schedules, true ) ) {
101
+ $links[] = "<span class='in-use'>" . esc_html__( 'This is a WordPress core schedule and cannot be deleted', 'wp-crontrol' ) . '</span>';
102
+ } elseif ( ! isset( $new_scheds[ $schedule['name'] ] ) ) {
103
+ $links[] = "<span class='in-use'>" . esc_html__( 'This schedule is added by another plugin and cannot be deleted', 'wp-crontrol' ) . '</span>';
104
+ } elseif ( in_array( $schedule['name'], self::$used_schedules, true ) ) {
105
+ $links[] = "<span class='in-use'>" . esc_html__( 'This custom schedule is in use and cannot be deleted', 'wp-crontrol' ) . '</span>';
106
+ } else {
107
+ $link = add_query_arg( array(
108
+ 'page' => 'crontrol_admin_options_page',
109
+ 'action' => 'delete-sched',
110
+ 'id' => rawurlencode( $schedule['name'] ),
111
+ ), admin_url( 'options-general.php' ) );
112
+ $link = wp_nonce_url( $link, 'delete-sched_' . $schedule['name'] );
113
+
114
+ $links[] = "<span class='delete'><a href='" . esc_url( $link ) . "'>" . esc_html__( 'Delete', 'wp-crontrol' ) . '</a></span>';
115
+ }
116
+
117
+ return $this->row_actions( $links );
118
+ }
119
+
120
+ /**
121
+ * Returns the output for the schdule name cell of a table row.
122
+ *
123
+ * @param array $schedule The schedule for the current row.
124
+ * @return string The cell output.
125
+ */
126
+ protected function column_crontrol_name( array $schedule ) {
127
+ return esc_html( $schedule['name'] );
128
+ }
129
+
130
+ /**
131
+ * Returns the output for the interval cell of a table row.
132
+ *
133
+ * @param array $schedule The schedule for the current row.
134
+ * @return string The cell output.
135
+ */
136
+ protected function column_crontrol_interval( array $schedule ) {
137
+ return sprintf(
138
+ '%s (%s)',
139
+ esc_html( $schedule['interval'] ),
140
+ esc_html( interval( $schedule['interval'] ) )
141
+ );
142
+ }
143
+
144
+ /**
145
+ * Returns the output for the display name cell of a table row.
146
+ *
147
+ * @param array $schedule The schedule for the current row.
148
+ * @return string The cell output.
149
+ */
150
+ protected function column_crontrol_display( array $schedule ) {
151
+ return esc_html( $schedule['display'] );
152
+ }
153
+
154
+ /**
155
+ * Outputs a message when there are no items to show in the table.
156
+ */
157
+ public function no_items() {
158
+ esc_html_e( 'There are no schedules.', 'wp-crontrol' );
159
+ }
160
+
161
+ }
src/schedule.php ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Functions related to schedules.
4
+ *
5
+ * @package wp-crontrol
6
+ */
7
+
8
+ namespace Crontrol\Schedule;
9
+
10
+ /**
11
+ * Adds a new custom cron schedule.
12
+ *
13
+ * @param string $name The internal name of the schedule.
14
+ * @param int $interval The interval between executions of the new schedule.
15
+ * @param string $display The display name of the schedule.
16
+ */
17
+ function add( $name, $interval, $display ) {
18
+ $old_scheds = get_option( 'crontrol_schedules', array() );
19
+
20
+ $old_scheds[ $name ] = array(
21
+ 'interval' => $interval,
22
+ 'display' => $display,
23
+ );
24
+ update_option( 'crontrol_schedules', $old_scheds );
25
+ }
26
+
27
+ /**
28
+ * Deletes a custom cron schedule.
29
+ *
30
+ * @param string $name The internal_name of the schedule to delete.
31
+ */
32
+ function delete( $name ) {
33
+ $scheds = get_option( 'crontrol_schedules', array() );
34
+ unset( $scheds[ $name ] );
35
+ update_option( 'crontrol_schedules', $scheds );
36
+ }
37
+
38
+ /**
39
+ * Gets a sorted (according to interval) list of the cron schedules
40
+ *
41
+ * @return array[] Array of cron schedule arrays.
42
+ */
43
+ function get() {
44
+ $schedules = wp_get_schedules();
45
+ uasort( $schedules, function( array $a, array $b ) {
46
+ return ( $a['interval'] - $b['interval'] );
47
+ } );
48
+
49
+ array_walk( $schedules, function( array &$schedule, $name ) {
50
+ $schedule['name'] = $name;
51
+ } );
52
+
53
+ return $schedules;
54
+ }
55
+
56
+ /**
57
+ * Displays a dropdown filled with the possible schedules, including non-repeating.
58
+ *
59
+ * @param bool $current The currently selected schedule.
60
+ */
61
+ function dropdown( $current = false ) {
62
+ $schedules = get();
63
+ ?>
64
+ <select class="postform" name="schedule" id="schedule" required>
65
+ <option <?php selected( $current, '_oneoff' ); ?> value="_oneoff"><?php esc_html_e( 'Non-repeating', 'wp-crontrol' ); ?></option>
66
+ <?php foreach ( $schedules as $sched_name => $sched_data ) { ?>
67
+ <option <?php selected( $current, $sched_name ); ?> value="<?php echo esc_attr( $sched_name ); ?>">
68
+ <?php
69
+ printf(
70
+ '%s (%s)',
71
+ esc_html( $sched_data['display'] ),
72
+ esc_html( $sched_name )
73
+ );
74
+ ?>
75
+ </option>
76
+ <?php } ?>
77
+ </select>
78
+ <?php
79
+ }
wp-crontrol.php CHANGED
@@ -1,14 +1,15 @@
1
  <?php
2
  /**
3
- * Plugin Name: WP Crontrol
4
- * Plugin URI: https://wordpress.org/plugins/wp-crontrol/
5
- * Description: WP Crontrol lets you view and control what's happening in the WP-Cron system.
6
- * Author: John Blackbourn & crontributors
7
- * Author URI: https://github.com/johnbillion/wp-crontrol/graphs/contributors
8
- * Version: 1.7.1
9
- * Text Domain: wp-crontrol
10
- * Domain Path: /languages/
11
- * License: GPL v2 or later
 
12
  *
13
  * LICENSE
14
  * This file is part of WP Crontrol.
@@ -23,177 +24,198 @@
23
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
  * GNU General Public License for more details.
25
  *
26
- * @package WP Crontrol
27
- * @author Edward Dale <scompt@scompt.com> & John Blackbourn <john@johnblackbourn.com>
28
- * @copyright Copyright 2008 Edward Dale, 2012-2019 John Blackbourn
29
  * @license http://www.gnu.org/licenses/gpl.txt GPL 2.0
30
  * @link https://wordpress.org/plugins/wp-crontrol/
31
  * @since 0.2
32
  */
33
 
 
 
 
 
34
  defined( 'ABSPATH' ) || die();
35
 
 
 
 
36
  /**
37
- * The main encapsulating class for WP Control.
38
  */
39
- class Crontrol {
 
40
 
41
- /**
42
- * Hook onto all of the actions and filters needed by the plugin.
43
- */
44
- protected function __construct() {
 
 
45
 
46
- $plugin_file = plugin_basename( __FILE__ );
47
 
48
- add_action( 'init', array( $this, 'action_init' ) );
49
- add_action( 'init', array( $this, 'action_handle_posts' ) );
50
- add_action( 'admin_menu', array( $this, 'action_admin_menu' ) );
51
- add_filter( "plugin_action_links_{$plugin_file}", array( $this, 'plugin_action_links' ), 10, 4 );
52
- add_filter( 'removable_query_args', array( $this, 'filter_removable_query_args' ) );
53
 
54
- add_action( 'load-tools_page_crontrol_admin_manage_page', array( $this, 'enqueue_code_editor' ) );
 
 
 
 
 
55
 
56
- register_activation_hook( __FILE__, array( $this, 'action_activate' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- add_filter( 'cron_schedules', array( $this, 'filter_cron_schedules' ) );
59
- add_action( 'crontrol_cron_job', array( $this, 'action_php_cron_event' ) );
60
- }
61
 
62
- /**
63
- * Evaluates the provided code using eval.
64
- *
65
- * @param string $code The PHP code to evaluate.
66
- */
67
- public function action_php_cron_event( $code ) {
68
- eval( $code ); // @codingStandardsIgnoreLine
69
- }
70
 
71
- /**
72
- * Run using the 'init' action.
73
- */
74
- public function action_init() {
75
- load_plugin_textdomain( 'wp-crontrol', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
76
- }
77
 
78
- /**
79
- * Handles any POSTs made by the plugin. Run using the 'init' action.
80
- */
81
- public function action_handle_posts() {
82
- if ( isset( $_POST['new_cron'] ) ) {
83
- if ( ! current_user_can( 'manage_options' ) ) {
84
- wp_die( esc_html__( 'You are not allowed to add new cron events.', 'wp-crontrol' ), 401 );
85
- }
86
- check_admin_referer( 'new-cron' );
87
- extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' );
88
- if ( 'crontrol_cron_job' === $in_hookname && ! current_user_can( 'edit_files' ) ) {
89
- wp_die( esc_html__( 'You are not allowed to add new PHP cron events.', 'wp-crontrol' ), 401 );
90
- }
91
- $in_args = json_decode( $in_args, true );
92
- $next_run = ( 'custom' === $in_next_run_date ) ? $in_next_run_date_custom : $in_next_run_date;
93
- $this->add_cron( $next_run, $in_schedule, $in_hookname, $in_args );
94
- $redirect = array(
95
- 'page' => 'crontrol_admin_manage_page',
96
- 'crontrol_message' => '5',
97
- 'crontrol_name' => rawurlencode( $in_hookname ),
98
- );
99
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
100
- exit;
101
 
102
- } elseif ( isset( $_POST['new_php_cron'] ) ) {
103
- if ( ! current_user_can( 'edit_files' ) ) {
104
- wp_die( esc_html__( 'You are not allowed to add new PHP cron events.', 'wp-crontrol' ), 401 );
105
- }
106
- check_admin_referer( 'new-cron' );
107
- extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' );
108
- $args = array(
109
- 'code' => $in_hookcode,
110
- 'name' => $in_eventname,
111
- );
112
- $next_run = ( 'custom' === $in_next_run_date ) ? $in_next_run_date_custom : $in_next_run_date;
113
- $this->add_cron( $next_run, $in_schedule, 'crontrol_cron_job', $args );
114
- $hookname = ( ! empty( $in_eventname ) ) ? $in_eventname : __( 'PHP Cron', 'wp-crontrol' );
115
- $redirect = array(
116
- 'page' => 'crontrol_admin_manage_page',
117
- 'crontrol_message' => '5',
118
- 'crontrol_name' => rawurlencode( $hookname ),
119
- );
120
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
121
- exit;
122
 
123
- } elseif ( isset( $_POST['edit_cron'] ) ) {
124
- if ( ! current_user_can( 'manage_options' ) ) {
125
- wp_die( esc_html__( 'You are not allowed to edit cron events.', 'wp-crontrol' ), 401 );
126
- }
127
 
128
- extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' );
129
- check_admin_referer( "edit-cron_{$in_original_hookname}_{$in_original_sig}_{$in_original_next_run}" );
 
 
 
 
 
 
 
 
 
130
 
131
- if ( 'crontrol_cron_job' === $in_hookname && ! current_user_can( 'edit_files' ) ) {
132
- wp_die( esc_html__( 'You are not allowed to edit PHP cron events.', 'wp-crontrol' ), 401 );
133
- }
134
 
135
- $in_args = json_decode( $in_args, true );
136
- $i = $this->delete_cron( $in_original_hookname, $in_original_sig, $in_original_next_run );
137
- $next_run = ( 'custom' === $in_next_run_date ) ? $in_next_run_date_custom : $in_next_run_date;
138
- $i = $this->add_cron( $next_run, $in_schedule, $in_hookname, $in_args );
139
- $redirect = array(
140
- 'page' => 'crontrol_admin_manage_page',
141
- 'crontrol_message' => '4',
142
- 'crontrol_name' => rawurlencode( $in_hookname ),
143
- );
144
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
145
- exit;
146
 
147
- } elseif ( isset( $_POST['edit_php_cron'] ) ) {
148
- if ( ! current_user_can( 'edit_files' ) ) {
149
- wp_die( esc_html__( 'You are not allowed to edit PHP cron events.', 'wp-crontrol' ), 401 );
150
- }
151
 
152
- extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' );
153
- check_admin_referer( "edit-cron_{$in_original_hookname}_{$in_original_sig}_{$in_original_next_run}" );
154
- $args['code'] = $in_hookcode;
155
- $args['name'] = $in_eventname;
156
- $args = array(
157
- 'code' => $in_hookcode,
158
- 'name' => $in_eventname,
159
- );
160
- $i = $this->delete_cron( $in_original_hookname, $in_original_sig, $in_original_next_run );
161
- $next_run = ( 'custom' === $in_next_run_date ) ? $in_next_run_date_custom : $in_next_run_date;
162
- $i = $this->add_cron( $next_run, $in_schedule, 'crontrol_cron_job', $args );
163
- $hookname = ( ! empty( $in_eventname ) ) ? $in_eventname : __( 'PHP Cron', 'wp-crontrol' );
164
- $redirect = array(
165
- 'page' => 'crontrol_admin_manage_page',
166
- 'crontrol_message' => '4',
167
- 'crontrol_name' => rawurlencode( $hookname ),
168
- );
169
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
170
- exit;
171
 
172
- } elseif ( isset( $_POST['new_schedule'] ) ) {
173
- if ( ! current_user_can( 'manage_options' ) ) {
174
- wp_die( esc_html__( 'You are not allowed to add new cron schedules.', 'wp-crontrol' ), 401 );
175
- }
176
- check_admin_referer( 'new-sched' );
177
- $name = wp_unslash( $_POST['internal_name'] );
178
- $interval = wp_unslash( $_POST['interval'] );
179
- $display = wp_unslash( $_POST['display_name'] );
180
-
181
- // The user entered something that wasn't a number.
182
- // Try to convert it with strtotime.
183
- if ( ! is_numeric( $interval ) ) {
184
- $now = time();
185
- $future = strtotime( $interval, $now );
186
- if ( false === $future || -1 == $future || $now > $future ) {
187
- $redirect = array(
188
- 'page' => 'crontrol_admin_options_page',
189
- 'crontrol_message' => '7',
190
- 'crontrol_name' => rawurlencode( $interval ),
191
- );
192
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
193
- exit;
194
- }
195
- $interval = $future - $now;
196
- } elseif ( $interval <= 0 ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  $redirect = array(
198
  'page' => 'crontrol_admin_options_page',
199
  'crontrol_message' => '7',
@@ -202,1294 +224,1249 @@ class Crontrol {
202
  wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
203
  exit;
204
  }
205
-
206
- $this->add_schedule( $name, $interval, $display );
207
  $redirect = array(
208
  'page' => 'crontrol_admin_options_page',
209
- 'crontrol_message' => '3',
210
- 'crontrol_name' => rawurlencode( $name ),
211
  );
212
  wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
213
  exit;
 
214
 
215
- } elseif ( isset( $_GET['action'] ) && 'delete-sched' == $_GET['action'] ) {
216
- if ( ! current_user_can( 'manage_options' ) ) {
217
- wp_die( esc_html__( 'You are not allowed to delete cron schedules.', 'wp-crontrol' ), 401 );
218
- }
219
- $id = wp_unslash( $_GET['id'] );
220
- check_admin_referer( "delete-sched_{$id}" );
221
- $this->delete_schedule( $id );
222
- $redirect = array(
223
- 'page' => 'crontrol_admin_options_page',
224
- 'crontrol_message' => '2',
225
- 'crontrol_name' => rawurlencode( $id ),
226
- );
227
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
228
- exit;
229
 
230
- } elseif ( isset( $_POST['delete_crons'] ) ) {
231
- if ( ! current_user_can( 'manage_options' ) ) {
232
- wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ), 401 );
233
- }
234
- check_admin_referer( 'bulk-delete-crons' );
 
 
 
 
 
 
 
 
 
235
 
236
- if ( empty( $_POST['delete'] ) ) {
237
- return;
238
- }
 
 
 
 
 
 
 
 
 
239
 
240
- $delete = wp_unslash( $_POST['delete'] );
241
- $deleted = 0;
242
-
243
- foreach ( $delete as $next_run => $events ) {
244
- foreach ( $events as $id => $sig ) {
245
- if ( 'crontrol_cron_job' === $id && ! current_user_can( 'edit_files' ) ) {
246
- continue;
247
- }
248
- if ( $this->delete_cron( urldecode( $id ), $sig, $next_run ) ) {
249
- $deleted++;
250
- }
251
  }
252
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
 
 
 
 
 
254
  $redirect = array(
255
  'page' => 'crontrol_admin_manage_page',
256
- 'crontrol_name' => $deleted,
257
- 'crontrol_message' => '9',
 
 
 
 
 
 
 
 
258
  );
259
  wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
260
  exit;
261
 
262
- } elseif ( isset( $_GET['action'] ) && 'delete-cron' == $_GET['action'] ) {
263
- if ( ! current_user_can( 'manage_options' ) ) {
264
- wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ), 401 );
265
- }
266
- $id = wp_unslash( $_GET['id'] );
267
- $sig = wp_unslash( $_GET['sig'] );
268
- $next_run = intval( $_GET['next_run'] );
269
- check_admin_referer( "delete-cron_{$id}_{$sig}_{$next_run}" );
270
-
271
- if ( 'crontrol_cron_job' === $id && ! current_user_can( 'edit_files' ) ) {
272
- wp_die( esc_html__( 'You are not allowed to delete PHP cron events.', 'wp-crontrol' ), 401 );
273
- }
274
 
275
- if ( $this->delete_cron( $id, $sig, $next_run ) ) {
276
- $redirect = array(
277
- 'page' => 'crontrol_admin_manage_page',
278
- 'crontrol_message' => '6',
279
- 'crontrol_name' => rawurlencode( $id ),
280
- );
281
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
282
- exit;
283
- } else {
284
- $redirect = array(
285
- 'page' => 'crontrol_admin_manage_page',
286
- 'crontrol_message' => '7',
287
- 'crontrol_name' => rawurlencode( $id ),
288
- );
289
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
290
- exit;
291
-
292
- };
293
 
294
- } elseif ( isset( $_GET['action'] ) && 'run-cron' == $_GET['action'] ) {
295
- if ( ! current_user_can( 'manage_options' ) ) {
296
- wp_die( esc_html__( 'You are not allowed to run cron events.', 'wp-crontrol' ), 401 );
297
- }
298
- $id = wp_unslash( $_GET['id'] );
299
- $sig = wp_unslash( $_GET['sig'] );
300
- check_admin_referer( "run-cron_{$id}_{$sig}" );
301
- if ( $this->run_cron( $id, $sig ) ) {
302
- $redirect = array(
303
- 'page' => 'crontrol_admin_manage_page',
304
- 'crontrol_message' => '1',
305
- 'crontrol_name' => rawurlencode( $id ),
306
- );
307
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
308
- exit;
309
- } else {
310
- $redirect = array(
311
- 'page' => 'crontrol_admin_manage_page',
312
- 'crontrol_message' => '8',
313
- 'crontrol_name' => rawurlencode( $id ),
314
- );
315
- wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
316
- exit;
317
- }
318
  }
319
- }
320
 
321
- /**
322
- * Executes a cron event immediately.
323
- *
324
- * Executes an event by scheduling a new single event with the same arguments.
325
- *
326
- * @param string $hookname The hook name of the cron event to run.
327
- * @param string $sig The cron event signature.
328
- */
329
- public function run_cron( $hookname, $sig ) {
330
- $crons = _get_cron_array();
331
- foreach ( $crons as $time => $cron ) {
332
- if ( isset( $cron[ $hookname ][ $sig ] ) ) {
333
- $args = $cron[ $hookname ][ $sig ]['args'];
334
- delete_transient( 'doing_cron' );
335
- wp_schedule_single_event( time() - 1, $hookname, $args );
336
- spawn_cron();
337
- return true;
338
- }
339
  }
340
- return false;
341
- }
342
 
343
- /**
344
- * Adds a new cron event.
345
- *
346
- * @param string $next_run A GMT time that the event should be run at.
347
- * @param string $schedule The recurrence of the cron event.
348
- * @param string $hookname The name of the hook to execute.
349
- * @param array $args Arguments to add to the cron event.
350
- */
351
- public function add_cron( $next_run, $schedule, $hookname, $args ) {
352
- $next_run = strtotime( $next_run );
353
- if ( false === $next_run ) {
354
- $next_run = time();
 
 
 
 
355
  } else {
356
- $next_run = get_gmt_from_date( date( 'Y-m-d H:i:s', $next_run ), 'U' );
 
 
 
 
 
 
357
  }
358
- if ( ! is_array( $args ) ) {
359
- $args = array();
 
360
  }
361
- if ( '_oneoff' === $schedule ) {
362
- return wp_schedule_single_event( $next_run, $hookname, $args ) === null;
 
 
 
 
 
 
 
 
 
363
  } else {
364
- return wp_schedule_event( $next_run, $schedule, $hookname, $args ) === null;
365
- }
366
- }
367
-
368
- /**
369
- * Deletes a cron event.
370
- *
371
- * @param string $to_delete The hook name of the event to delete.
372
- * @param string $sig The cron event signature.
373
- * @param string $next_run The GMT time that the event would be run at.
374
- */
375
- public function delete_cron( $to_delete, $sig, $next_run ) {
376
- $crons = _get_cron_array();
377
- if ( isset( $crons[ $next_run ][ $to_delete ][ $sig ] ) ) {
378
- $args = $crons[ $next_run ][ $to_delete ][ $sig ]['args'];
379
- wp_unschedule_event( $next_run, $to_delete, $args );
380
- return true;
381
- }
382
- return false;
383
- }
384
-
385
- /**
386
- * Adds a new custom cron schedule.
387
- *
388
- * @param string $name The internal name of the schedule.
389
- * @param int $interval The interval between executions of the new schedule.
390
- * @param string $display The display name of the schedule.
391
- */
392
- public function add_schedule( $name, $interval, $display ) {
393
- $old_scheds = get_option( 'crontrol_schedules', array() );
394
- $old_scheds[ $name ] = array(
395
- 'interval' => $interval,
396
- 'display' => $display,
397
- );
398
- update_option( 'crontrol_schedules', $old_scheds );
399
- }
400
-
401
- /**
402
- * Deletes a custom cron schedule.
403
- *
404
- * @param string $name The internal_name of the schedule to delete.
405
- */
406
- public function delete_schedule( $name ) {
407
- $scheds = get_option( 'crontrol_schedules', array() );
408
- unset( $scheds[ $name ] );
409
- update_option( 'crontrol_schedules', $scheds );
410
- }
411
-
412
- /**
413
- * Sets up the plugin environment upon first activation.
414
- *
415
- * Run using the 'activate_' action.
416
- */
417
- public function action_activate() {
418
- add_option( 'crontrol_schedules', array() );
419
-
420
- // If there's never been a cron event, _get_cron_array will return false.
421
- if ( _get_cron_array() === false ) {
422
- _set_cron_array( array() );
423
  }
424
  }
 
425
 
426
- /**
427
- * Adds options & management pages to the admin menu.
428
- *
429
- * Run using the 'admin_menu' action.
430
- */
431
- public function action_admin_menu() {
432
- add_options_page( esc_html__( 'Cron Schedules', 'wp-crontrol' ), esc_html__( 'Cron Schedules', 'wp-crontrol' ), 'manage_options', 'crontrol_admin_options_page', array( $this, 'admin_options_page' ) );
433
- add_management_page( esc_html__( 'Cron Events', 'wp-crontrol' ), esc_html__( 'Cron Events', 'wp-crontrol' ), 'manage_options', 'crontrol_admin_manage_page', array( $this, 'admin_manage_page' ) );
434
- }
435
 
436
- /**
437
- * Adds items to the plugin's action links on the Plugins listing screen.
438
- *
439
- * @param string[] $actions Array of action links.
440
- * @param string $plugin_file Path to the plugin file relative to the plugins directory.
441
- * @param array $plugin_data An array of plugin data.
442
- * @param string $context The plugin context.
443
- * @return string[] Array of action links.
444
- */
445
- public function plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
446
- $actions['crontrol-events'] = sprintf(
 
447
  '<a href="%s">%s</a>',
448
  esc_url( admin_url( 'tools.php?page=crontrol_admin_manage_page' ) ),
449
- esc_html__( 'Cron Events', 'wp-crontrol' )
450
- );
451
- $actions['crontrol-schedules'] = sprintf(
452
  '<a href="%s">%s</a>',
453
  esc_url( admin_url( 'options-general.php?page=crontrol_admin_options_page' ) ),
454
- esc_html__( 'Cron Schedules', 'wp-crontrol' )
455
- );
456
- return $actions;
457
- }
458
-
459
- /**
460
- * Gives WordPress the plugin's set of cron schedules.
461
- *
462
- * Called by the 'cron_schedules' filter.
463
- *
464
- * @param array $scheds The current cron schedules. Usually an empty array.
465
- * @return array The existing cron schedules along with the plugin's schedules.
466
- */
467
- public function filter_cron_schedules( $scheds ) {
468
- $new_scheds = get_option( 'crontrol_schedules', array() );
469
- return array_merge( $new_scheds, $scheds );
470
- }
471
 
472
- /**
473
- * Displays the options page for the plugin.
474
- */
475
- public function admin_options_page() {
476
- $schedules = $this->get_schedules();
477
- $events = $this->get_cron_events();
478
- $custom_schedules = get_option( 'crontrol_schedules', array() );
479
- $custom_keys = array_keys( $custom_schedules );
480
-
481
- if ( is_wp_error( $events ) ) {
482
- $events = array();
483
- }
484
 
485
- $used_schedules = array_unique( wp_list_pluck( $events, 'schedule' ) );
 
 
 
 
 
 
 
 
 
 
 
486
 
487
- $messages = array(
 
 
 
 
 
488
  /* translators: 1: The name of the cron schedule. */
489
- '2' => __( 'Successfully deleted the cron schedule %s.', 'wp-crontrol' ),
 
 
 
490
  /* translators: 1: The name of the cron schedule. */
491
- '3' => __( 'Successfully added the cron schedule %s.', 'wp-crontrol' ),
 
 
 
492
  /* translators: 1: The name of the cron schedule. */
493
- '7' => __( 'Cron schedule not added because there was a problem parsing %s.', 'wp-crontrol' ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
  );
495
- if ( isset( $_GET['crontrol_message'] ) && isset( $_GET['crontrol_name'] ) && isset( $messages[ $_GET['crontrol_message'] ] ) ) {
496
- $hook = wp_unslash( $_GET['crontrol_name'] );
497
- $message = wp_unslash( $_GET['crontrol_message'] );
498
- $msg = sprintf( esc_html( $messages[ $message ] ), '<strong>' . esc_html( $hook ) . '</strong>' );
499
-
500
- printf( '<div id="message" class="updated notice is-dismissible"><p>%s</p></div>', $msg ); // WPCS:: XSS ok.
501
- }
502
 
503
- ?>
504
- <div class="wrap">
505
- <h1><?php esc_html_e( 'WP-Cron Schedules', 'wp-crontrol' ); ?></h1>
506
- <p><?php esc_html_e( 'WP-Cron schedules are the time intervals that are available for scheduling events. You can only delete custom schedules.', 'wp-crontrol' ); ?></p>
507
- <table class="widefat striped">
508
- <thead>
509
- <tr>
510
- <th scope="col"><?php esc_html_e( 'Name', 'wp-crontrol' ); ?></th>
511
- <th scope="col"><?php esc_html_e( 'Interval', 'wp-crontrol' ); ?></th>
512
- <th scope="col"><?php esc_html_e( 'Display Name', 'wp-crontrol' ); ?></th>
513
- <th scope="col"><?php esc_html_e( 'Delete', 'wp-crontrol' ); ?></th>
514
- </tr>
515
- </thead>
516
- <tbody>
517
- <?php
518
- if ( empty( $schedules ) ) {
519
- ?>
520
- <tr colspan="4"><td><?php esc_html_e( 'You currently have no schedules. Add one below.', 'wp-crontrol' ); ?></td></tr>
521
- <?php
522
- } else {
523
- foreach ( $schedules as $name => $data ) {
524
- printf(
525
- '<tr id="sched-%s">',
526
- esc_attr( $name )
527
- );
528
- printf(
529
- '<td>%s</td>',
530
- esc_html( $name )
531
- );
532
- printf(
533
- '<td>%s (%s)</td>',
534
- esc_html( $data['interval'] ),
535
- esc_html( $this->interval( $data['interval'] ) )
536
- );
537
- printf(
538
- '<td>%s</td>',
539
- esc_html( $data['display'] )
540
- );
 
 
 
 
541
 
542
- echo '<td>';
543
- if ( in_array( $name, $custom_keys, true ) ) {
544
- if ( in_array( $name, $used_schedules, true ) ) {
545
- esc_html_e( 'This custom schedule is in use and cannot be deleted', 'wp-crontrol' );
546
- } else {
547
- $url = add_query_arg( array(
548
- 'page' => 'crontrol_admin_options_page',
549
- 'action' => 'delete-sched',
550
- 'id' => rawurlencode( $name ),
551
- ), admin_url( 'options-general.php' ) );
552
- $url = wp_nonce_url( $url, 'delete-sched_' . $name );
553
- printf( '<span class="row-actions visible"><span class="delete"><a href="%s">%s</a></span></span>',
554
- esc_url( $url ),
555
- esc_html__( 'Delete', 'wp-crontrol' )
556
- );
557
- }
558
- } else {
559
- echo '&nbsp;';
560
- }
561
- echo '</td>';
562
- echo '</tr>';
563
- }
564
  }
565
- ?>
566
- </tbody>
567
- </table>
568
- </div>
569
- <div class="wrap">
570
- <p class="description">
571
- <?php
572
- printf(
573
- '<a href="%s">%s</a>',
574
- esc_url( admin_url( 'tools.php?page=crontrol_admin_manage_page' ) ),
575
- esc_html__( 'Manage Cron Events', 'wp-crontrol' )
576
- );
577
- ?>
578
- </p>
579
- </div>
580
- <div class="wrap narrow">
581
- <h2 class="title"><?php esc_html_e( 'Add Cron Schedule', 'wp-crontrol' ); ?></h2>
582
- <p><?php esc_html_e( 'Adding a new cron schedule will allow you to schedule events that re-occur at the given interval.', 'wp-crontrol' ); ?></p>
583
- <form method="post" action="options-general.php?page=crontrol_admin_options_page">
584
- <table class="form-table">
585
- <tbody>
586
- <tr>
587
- <th valign="top" scope="row"><label for="cron_internal_name"><?php esc_html_e( 'Internal name', 'wp-crontrol' ); ?></label></th>
588
- <td><input type="text" class="regular-text" value="" id="cron_internal_name" name="internal_name" required/></td>
589
- </tr>
590
- <tr>
591
- <th valign="top" scope="row"><label for="cron_interval"><?php esc_html_e( 'Interval (seconds)', 'wp-crontrol' ); ?></label></th>
592
- <td><input type="number" class="regular-text" value="" id="cron_interval" name="interval" min="1" step="1" required/></td>
593
- </tr>
594
- <tr>
595
- <th valign="top" scope="row"><label for="cron_display_name"><?php esc_html_e( 'Display name', 'wp-crontrol' ); ?></label></th>
596
- <td><input type="text" class="regular-text" value="" id="cron_display_name" name="display_name" required/></td>
597
- </tr>
598
- </tbody></table>
599
- <p class="submit"><input id="schedadd-submit" type="submit" class="button-primary" value="<?php esc_attr_e( 'Add Cron Schedule', 'wp-crontrol' ); ?>" name="new_schedule"/></p>
600
- <?php wp_nonce_field( 'new-sched' ); ?>
601
- </form>
602
- </div>
603
- <?php
604
  }
605
 
606
- /**
607
- * Gets a sorted (according to interval) list of the cron schedules
608
- */
609
- public function get_schedules() {
610
- $schedules = wp_get_schedules();
611
- uasort( $schedules, array( $this, 'sort_schedules' ) );
612
- return $schedules;
613
  }
614
 
615
- /**
616
- * Internal sorting comparison callback function. Compares schedules by their interval.
617
- *
618
- * @param array $a The first schedule to compare for sorting.
619
- * @param array $b The second schedule to compare for sorting.
620
- * @return int An integer less than, equal to, or greater than zero if the first argument is considered to be
621
- * respectively less than, equal to, or greater than the second.
622
- */
623
- protected function sort_schedules( $a, $b ) {
624
- return ( $a['interval'] - $b['interval'] );
625
  }
626
 
627
- /**
628
- * Displays a dropdown filled with the possible schedules, including non-repeating.
629
- *
630
- * @param bool $current The currently selected schedule.
631
- */
632
- public function schedules_dropdown( $current = false ) {
633
- $schedules = $this->get_schedules();
634
- ?>
635
- <select class="postform" name="schedule" id="schedule" required>
636
- <option <?php selected( $current, '_oneoff' ); ?> value="_oneoff"><?php esc_html_e( 'Non-repeating', 'wp-crontrol' ); ?></option>
637
- <?php foreach ( $schedules as $sched_name => $sched_data ) { ?>
638
- <option <?php selected( $current, $sched_name ); ?> value="<?php echo esc_attr( $sched_name ); ?>">
639
- <?php
640
- printf(
641
- '%s (%s)',
642
- esc_html( $sched_data['display'] ),
643
- esc_html( $this->interval( $sched_data['interval'] ) )
644
- );
645
- ?>
646
- </option>
647
- <?php } ?>
648
- </select>
649
- <?php
650
- }
651
 
652
- /**
653
- * Gets the status of WP-Cron functionality on the site by performing a test spawn. Cached for one hour when all is well.
654
- *
655
- * @param bool $cache Whether to use the cached result from previous calls.
656
- * @return true|WP_Error Boolean true if the cron spawner is working as expected, or a WP_Error object if not.
657
- */
658
- public function test_cron_spawn( $cache = true ) {
659
- global $wp_version;
660
 
661
- if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
662
- /* translators: 1: The name of the PHP constant that is set. */
663
- return new WP_Error( 'crontrol_info', sprintf( __( 'The %s constant is set to true. WP-Cron spawning is disabled.', 'wp-crontrol' ), 'DISABLE_WP_CRON' ) );
664
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
665
 
666
- if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
667
- /* translators: 1: The name of the PHP constant that is set. */
668
- return new WP_Error( 'crontrol_info', sprintf( __( 'The %s constant is set to true.', 'wp-crontrol' ), 'ALTERNATE_WP_CRON' ) );
669
- }
670
 
671
- $cached_status = get_transient( 'wp-cron-test-ok' );
 
 
 
 
672
 
673
- if ( $cache && $cached_status ) {
674
- return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675
  }
 
 
676
 
677
- $sslverify = version_compare( $wp_version, 4.0, '<' );
678
- $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
 
 
 
 
 
 
679
 
680
- $cron_request = apply_filters( 'cron_request', array(
681
- 'url' => site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ),
682
- 'key' => $doing_wp_cron,
683
- 'args' => array(
684
- 'timeout' => 3,
685
- 'blocking' => true,
686
- 'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify ),
687
- ),
688
- ) );
689
 
690
- $cron_request['args']['blocking'] = true;
 
 
691
 
692
- $result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
 
 
 
 
 
693
 
694
- if ( is_wp_error( $result ) ) {
695
- return $result;
696
- } elseif ( wp_remote_retrieve_response_code( $result ) >= 300 ) {
697
- return new WP_Error( 'unexpected_http_response_code', sprintf(
698
- /* translators: 1: The HTTP response code. */
699
- __( 'Unexpected HTTP response code: %s', 'wp-crontrol' ),
700
- intval( wp_remote_retrieve_response_code( $result ) )
701
- ) );
702
- } else {
703
- set_transient( 'wp-cron-test-ok', 1, 3600 );
704
- return true;
705
- }
706
 
 
 
707
  }
708
 
709
- /**
710
- * Shows the status of WP-Cron functionality on the site. Only displays a message when there's a problem.
711
- */
712
- public function show_cron_status() {
713
-
714
- $status = $this->test_cron_spawn();
 
 
 
 
 
 
715
 
716
- if ( is_wp_error( $status ) ) {
717
- if ( 'crontrol_info' === $status->get_error_code() ) {
718
- ?>
719
- <div id="cron-status-notice" class="notice notice-info">
720
- <p><?php echo esc_html( $status->get_error_message() ); ?></p>
721
- </div>
722
- <?php
723
- } else {
724
- ?>
725
- <div id="cron-status-error" class="error">
726
- <p>
727
- <?php
728
- printf(
729
- /* translators: 1: Error message text. */
730
- esc_html__( 'There was a problem spawning a call to the WP-Cron system on your site. This means WP-Cron events on your site may not work. The problem was: %s', 'wp-crontrol' ),
731
- '<br><strong>' . esc_html( $status->get_error_message() ) . '</strong>'
732
- );
733
- ?>
734
- </p>
735
- </div>
736
- <?php
737
- }
738
- }
739
 
 
 
740
  }
741
 
742
- /**
743
- * Get the display name for the site's timezone.
744
- *
745
- * @return string The name and UTC offset for the site's timezone.
746
- */
747
- public function get_timezone_name() {
748
- $timezone_string = get_option( 'timezone_string', '' );
749
- $gmt_offset = get_option( 'gmt_offset', 0 );
750
-
751
- if ( $gmt_offset >= 0 ) {
752
- $gmt_offset = '+' . $gmt_offset;
753
  }
 
754
 
755
- if ( '' === $timezone_string ) {
756
- $name = sprintf( 'UTC%s', $gmt_offset );
757
- } else {
758
- $name = sprintf( '%s (UTC%s)', str_replace( '_', ' ', $timezone_string ), $gmt_offset );
759
- }
760
 
761
- return $name;
 
 
 
 
 
 
 
762
  }
763
 
764
- /**
765
- * Shows the form used to add/edit cron events.
766
- *
767
- * @param bool $is_php Whether this is a PHP cron event.
768
- * @param mixed $existing An array of existing values for the cron event, or null.
769
- */
770
- public function show_cron_form( $is_php, $existing ) {
771
- $new_tabs = array(
772
- 'cron' => __( 'Add Cron Event', 'wp-crontrol' ),
773
- 'php-cron' => __( 'Add PHP Cron Event', 'wp-crontrol' ),
774
  );
775
- $modify_tabs = array(
776
- 'cron' => __( 'Modify Cron Event', 'wp-crontrol' ),
777
- 'php-cron' => __( 'Modify PHP Cron Event', 'wp-crontrol' ),
778
  );
779
- $new_links = array(
780
- 'cron' => admin_url( 'tools.php?page=crontrol_admin_manage_page&action=new-cron' ) . '#crontrol_form',
781
- 'php-cron' => admin_url( 'tools.php?page=crontrol_admin_manage_page&action=new-php-cron' ) . '#crontrol_form',
782
  );
783
- $display_args = '';
784
- if ( $is_php ) {
785
- $helper_text = esc_html__( 'Cron events trigger actions in your code. Enter the schedule of the event, as well as the PHP code to execute when the action is triggered.', 'wp-crontrol' );
786
- } else {
787
- $helper_text = sprintf(
788
- /* translators: %s: A file name */
789
- esc_html__( 'Cron events trigger actions in your code. A cron event needs a corresponding action hook somewhere in code, e.g. the %1$s file in your theme.', 'wp-crontrol' ),
790
- '<code>functions.php</code>'
791
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
792
  }
793
- if ( is_array( $existing ) ) {
794
- $other_fields = wp_nonce_field( "edit-cron_{$existing['hookname']}_{$existing['sig']}_{$existing['next_run']}", '_wpnonce', true, false );
795
- $other_fields .= sprintf( '<input name="original_hookname" type="hidden" value="%s" />',
796
- esc_attr( $existing['hookname'] )
797
- );
798
- $other_fields .= sprintf( '<input name="original_sig" type="hidden" value="%s" />',
799
- esc_attr( $existing['sig'] )
800
- );
801
- $other_fields .= sprintf( '<input name="original_next_run" type="hidden" value="%s" />',
802
- esc_attr( $existing['next_run'] )
803
- );
804
- if ( ! empty( $existing['args'] ) ) {
805
- $display_args = wp_json_encode( $existing['args'] );
806
- }
807
- $action = $is_php ? 'edit_php_cron' : 'edit_cron';
808
- $button = $is_php ? $modify_tabs['php-cron'] : $modify_tabs['cron'];
809
- $show_edit_tab = true;
810
- $next_run_date = get_date_from_gmt( date( 'Y-m-d H:i:s', $existing['next_run'] ), 'Y-m-d H:i:s' );
811
- } else {
812
- $other_fields = wp_nonce_field( 'new-cron', '_wpnonce', true, false );
813
- $existing = array(
814
- 'hookname' => '',
815
- 'args' => array(),
816
- 'next_run' => 'now',
817
- 'schedule' => false,
818
- );
819
- $action = $is_php ? 'new_php_cron' : 'new_cron';
820
- $button = $is_php ? $new_tabs['php-cron'] : $new_tabs['cron'];
821
- $show_edit_tab = false;
822
- $next_run_date = '';
823
  }
824
- if ( $is_php ) {
825
- if ( ! isset( $existing['args']['code'] ) ) {
826
- $existing['args']['code'] = '';
827
- }
828
- if ( ! isset( $existing['args']['name'] ) ) {
829
- $existing['args']['name'] = '';
 
 
 
 
 
 
 
 
 
 
 
 
 
830
  }
831
- }
832
 
833
- $allowed = ( ! $is_php || current_user_can( 'edit_files' ) );
834
- ?>
835
- <div id="crontrol_form" class="wrap narrow">
836
- <h2 class="nav-tab-wrapper">
837
- <a href="<?php echo esc_url( $new_links['cron'] ); ?>" class="nav-tab<?php echo ( ! $show_edit_tab && ! $is_php ) ? ' nav-tab-active' : ''; ?>"><?php echo esc_html( $new_tabs['cron'] ); ?></a>
838
- <a href="<?php echo esc_url( $new_links['php-cron'] ); ?>" class="nav-tab<?php echo ( ! $show_edit_tab && $is_php ) ? ' nav-tab-active' : ''; ?>"><?php echo esc_html( $new_tabs['php-cron'] ); ?></a>
839
- <?php if ( $show_edit_tab ) { ?>
840
- <span class="nav-tab nav-tab-active"><?php echo esc_html( $button ); ?></span>
841
- <?php } ?>
842
- </h2>
843
- <?php if ( $allowed ) { ?>
844
- <p><?php echo $helper_text; // WPCS:: XSS ok. ?></p>
845
- <form method="post" action="<?php echo esc_url( admin_url( 'tools.php?page=crontrol_admin_manage_page' ) ); ?>">
846
- <?php echo $other_fields; // WPCS:: XSS ok. ?>
847
- <table class="form-table"><tbody>
848
- <?php if ( $is_php ) : ?>
849
- <tr>
850
- <th valign="top" scope="row"><label for="hookcode"><?php esc_html_e( 'PHP Code', 'wp-crontrol' ); ?></label></th>
851
- <td>
852
- <p class="description">
853
- <?php
854
- printf(
855
- /* translators: The PHP tag name */
856
- esc_html__( 'The opening %s tag must not be included.', 'wp-crontrol' ),
857
- '<code>&lt;?php</code>'
858
- );
859
- ?>
860
- </p>
861
- <p><textarea class="large-text code" rows="10" cols="50" id="hookcode" name="hookcode"><?php echo esc_textarea( $existing['args']['code'] ); ?></textarea></p>
862
- </td>
863
- </tr>
864
- <tr>
865
- <th valign="top" scope="row"><label for="eventname"><?php esc_html_e( 'Event Name (optional)', 'wp-crontrol' ); ?></label></th>
866
- <td><input type="text" class="regular-text" id="eventname" name="eventname" value="<?php echo esc_attr( $existing['args']['name'] ); ?>"/></td>
867
- </tr>
868
- <?php else : ?>
869
- <tr>
870
- <th valign="top" scope="row"><label for="hookname"><?php esc_html_e( 'Hook Name', 'wp-crontrol' ); ?></label></th>
871
- <td><input type="text" class="regular-text" id="hookname" name="hookname" value="<?php echo esc_attr( $existing['hookname'] ); ?>" required /></td>
872
- </tr>
873
- <tr>
874
- <th valign="top" scope="row"><label for="args"><?php esc_html_e( 'Arguments (optional)', 'wp-crontrol' ); ?></label></th>
875
- <td>
876
- <input type="text" class="regular-text" id="args" name="args" value="<?php echo esc_attr( $display_args ); ?>"/>
877
- <p class="description">
878
- <?php
879
- printf(
880
- /* translators: 1, 2, and 3: Example values for an input field. */
881
- esc_html__( 'Use a JSON encoded array, e.g. %1$s, %2$s, or %3$s', 'wp-crontrol' ),
882
- '<code>[25]</code>',
883
- '<code>["asdf"]</code>',
884
- '<code>["i","want",25,"cakes"]</code>'
885
- );
886
- ?>
887
- </p>
888
- </td>
889
- </tr>
890
- <?php endif; ?>
891
  <tr>
892
- <th valign="top" scope="row"><label for="next_run_date"><?php esc_html_e( 'Next Run', 'wp-crontrol' ); ?></label></th>
893
  <td>
894
- <ul>
895
- <li>
896
- <label>
897
- <input type="radio" name="next_run_date" value="now()" checked>
898
- <?php esc_html_e( 'Now', 'wp-crontrol' ); ?>
899
- </label>
900
- </li>
901
- <li>
902
- <label>
903
- <input type="radio" name="next_run_date" value="+1 day">
904
- <?php esc_html_e( 'Tomorrow', 'wp-crontrol' ); ?>
905
- </label>
906
- </li>
907
- <li>
908
- <label>
909
- <input type="radio" name="next_run_date" value="custom" id="next_run_date_custom" <?php checked( $show_edit_tab ); ?>>
910
- <?php
911
- printf(
912
- /* translators: %s: An input field for specifying a date and time */
913
- esc_html__( 'At: %s', 'wp-crontrol' ),
914
- sprintf(
915
- '<input type="text" name="next_run_date_custom" value="%s" class="regular-text" onfocus="jQuery(\'#next_run_date_custom\').prop(\'checked\',true);" />',
916
- esc_attr( $next_run_date )
917
- )
918
- );
919
- ?>
920
- </label>
921
- </li>
922
- </ul>
923
-
924
- <p class="description">
925
- <?php
926
- printf(
927
- /* translators: 1: Date/time format for an input field, 2: PHP function name. */
928
- esc_html__( 'Format: %1$s or anything accepted by %2$s', 'wp-crontrol' ),
929
- '<code>YYYY-MM-DD HH:MM:SS</code>',
930
- '<a href="https://www.php.net/manual/en/function.strtotime.php"><code>strtotime()</code></a>'
931
- );
932
- ?>
933
- </p>
934
  <p class="description">
935
  <?php
936
  printf(
937
- /* translators: %s Timezone name. */
938
- esc_html__( 'Timezone: %s', 'wp-crontrol' ),
939
- '<code>' . esc_html( $this->get_timezone_name() ) . '</code>'
940
  );
941
  ?>
942
  </p>
 
 
 
 
 
 
 
 
 
 
 
 
943
  </td>
944
- </tr><tr>
945
- <th valign="top" scope="row"><label for="schedule"><?php esc_html_e( 'Recurrence', 'wp-crontrol' ); ?></label></th>
 
946
  <td>
947
- <?php $this->schedules_dropdown( $existing['schedule'] ); ?>
948
  <p class="description">
949
  <?php
950
- printf(
951
- '<a href="%s">%s</a>',
952
- esc_url( admin_url( 'options-general.php?page=crontrol_admin_options_page' ) ),
953
- esc_html__( 'Manage Cron Schedules', 'wp-crontrol' )
954
- );
 
 
955
  ?>
956
  </p>
957
  </td>
958
  </tr>
959
- </tbody></table>
960
- <p class="submit"><input type="submit" class="button-primary" value="<?php echo esc_attr( $button ); ?>" name="<?php echo esc_attr( $action ); ?>"/></p>
961
- </form>
962
- <?php } else { ?>
963
- <div class="error inline">
964
- <p><?php esc_html_e( 'You cannot add, edit, or delete PHP cron events because your user account does not have the ability to edit files.', 'wp-crontrol' ); ?></p>
965
- </div>
966
- <?php } ?>
967
- </div>
968
- <?php
969
- }
970
-
971
- /**
972
- * Returns a flattened array of cron events.
973
- *
974
- * @return array[]|WP_Error An array of cron event arrays, or a WP_Error object if there's an error or no events.
975
- */
976
- public function get_cron_events() {
977
-
978
- $crons = _get_cron_array();
979
- $events = array();
980
-
981
- if ( empty( $crons ) ) {
982
- return new WP_Error(
983
- 'no_events',
984
- __( 'You currently have no scheduled cron events.', 'wp-crontrol' )
985
- );
986
- }
987
-
988
- foreach ( $crons as $time => $cron ) {
989
- foreach ( $cron as $hook => $dings ) {
990
- foreach ( $dings as $sig => $data ) {
991
-
992
- // This is a prime candidate for a Crontrol_Event class but I'm not bothering currently.
993
- $events[ "$hook-$sig-$time" ] = (object) array(
994
- 'hook' => $hook,
995
- 'time' => $time,
996
- 'sig' => $sig,
997
- 'args' => $data['args'],
998
- 'schedule' => $data['schedule'],
999
- 'interval' => isset( $data['interval'] ) ? $data['interval'] : null,
1000
- );
1001
-
1002
- }
1003
- }
1004
- }
1005
-
1006
- return $events;
1007
 
1008
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1009
 
1010
- /**
1011
- * Displays the manage page for the plugin.
1012
- */
1013
- public function admin_manage_page() {
1014
- $messages = array(
 
1015
  /* translators: 1: The name of the cron event. */
1016
- '1' => __( 'Successfully executed the cron event %s.', 'wp-crontrol' ),
 
 
 
 
1017
  /* translators: 1: The name of the cron event. */
1018
- '4' => __( 'Successfully edited the cron event %s.', 'wp-crontrol' ),
 
 
 
 
1019
  /* translators: 1: The name of the cron event. */
1020
- '5' => __( 'Successfully created the cron event %s.', 'wp-crontrol' ),
 
 
 
 
1021
  /* translators: 1: The name of the cron event. */
1022
- '6' => __( 'Successfully deleted the cron event %s.', 'wp-crontrol' ),
 
 
 
 
1023
  /* translators: 1: The name of the cron event. */
1024
- '7' => __( 'Failed to the delete the cron event %s.', 'wp-crontrol' ),
 
 
 
 
1025
  /* translators: 1: The name of the cron event. */
1026
- '8' => __( 'Failed to the execute the cron event %s.', 'wp-crontrol' ),
1027
- '9' => __( 'Successfully deleted the selected cron events.', 'wp-crontrol' ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1028
  );
1029
- if ( isset( $_GET['crontrol_name'] ) && isset( $_GET['crontrol_message'] ) && isset( $messages[ $_GET['crontrol_message'] ] ) ) {
1030
- $hook = wp_unslash( $_GET['crontrol_name'] );
1031
- $message = wp_unslash( $_GET['crontrol_message'] );
1032
- $msg = sprintf( esc_html( $messages[ $message ] ), '<strong>' . esc_html( $hook ) . '</strong>' );
1033
 
1034
- printf( '<div id="message" class="updated notice is-dismissible"><p>%s</p></div>', $msg ); // WPCS:: XSS ok.
1035
- }
1036
- $events = $this->get_cron_events();
1037
- $doing_edit = ( isset( $_GET['action'] ) && 'edit-cron' === $_GET['action'] ) ? wp_unslash( $_GET['id'] ) : false;
1038
- $time_format = 'Y-m-d H:i:s';
1039
- $can_edit_files = current_user_can( 'edit_files' );
1040
-
1041
- $core_hooks = array(
1042
- 'wp_version_check',
1043
- 'wp_update_plugins',
1044
- 'wp_update_themes',
1045
- 'wp_scheduled_delete',
1046
- 'wp_scheduled_auto_draft_delete',
1047
- 'update_network_counts',
1048
- 'delete_expired_transients',
1049
- );
1050
 
1051
- $this->show_cron_status();
 
1052
 
1053
- ?>
1054
- <div class="wrap">
1055
- <h1><?php esc_html_e( 'WP-Cron Events', 'wp-crontrol' ); ?></h1>
1056
- <form method="post" action="tools.php?page=crontrol_admin_manage_page">
1057
- <table class="widefat striped">
1058
- <thead>
1059
- <tr>
1060
- <td id="cb" class="manage-column column-cb check-column"><label class="screen-reader-text" for="cb-select-all-1"><?php esc_html_e( 'Select All', 'wp-crontrol' ); ?></label><input id="cb-select-all-1" type="checkbox"></td>
1061
- <th scope="col"><?php esc_html_e( 'Hook Name', 'wp-crontrol' ); ?></th>
1062
- <th scope="col"><?php esc_html_e( 'Arguments', 'wp-crontrol' ); ?></th>
1063
- <th scope="col"><?php esc_html_e( 'Actions', 'wp-crontrol' ); ?></th>
1064
- <th scope="col"><?php esc_html_e( 'Next Run', 'wp-crontrol' ); ?></th>
1065
- <th scope="col"><?php esc_html_e( 'Recurrence', 'wp-crontrol' ); ?></th>
1066
- <th scope="col"><span class="screen-reader-text"><?php esc_html_e( 'Actions', 'wp-crontrol' ); ?></span></th>
1067
- </tr>
1068
- </thead>
1069
- <tbody>
1070
- <?php
1071
- if ( is_wp_error( $events ) ) {
1072
  ?>
1073
- <tr><td colspan="7"><?php echo esc_html( $events->get_error_message() ); ?></td></tr>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1074
  <?php
1075
- } else {
1076
- foreach ( $events as $id => $event ) {
1077
-
1078
- if ( $doing_edit && $doing_edit == $event->hook && $event->time == $_GET['next_run'] && $event->sig == $_GET['sig'] ) {
1079
- $doing_edit = array(
1080
- 'hookname' => $event->hook,
1081
- 'next_run' => $event->time,
1082
- 'schedule' => ( $event->schedule ? $event->schedule : '_oneoff' ),
1083
- 'sig' => $event->sig,
1084
- 'args' => $event->args,
1085
- );
1086
- }
1087
 
1088
- if ( empty( $event->args ) ) {
1089
- $args = '<em>' . esc_html__( 'None', 'wp-crontrol' ) . '</em>';
1090
- } else {
1091
- $json_options = 0;
1092
 
1093
- if ( defined( 'JSON_UNESCAPED_SLASHES' ) ) {
1094
- $json_options |= JSON_UNESCAPED_SLASHES;
1095
- }
1096
- if ( defined( 'JSON_PRETTY_PRINT' ) ) {
1097
- $json_options |= JSON_PRETTY_PRINT;
1098
- }
1099
 
1100
- $args = '<pre style="white-space:pre-wrap;margin-top:0">' . wp_json_encode( $event->args, $json_options ) . '</pre>';
1101
- }
 
1102
 
1103
- echo '<tr>';
 
 
1104
 
1105
- echo '<th scope="row" class="check-column">';
1106
- if ( ! in_array( $event->hook, $core_hooks, true ) ) {
1107
- printf(
1108
- '<input type="checkbox" name="delete[%1$s][%2$s]" value="%3$s">',
1109
- esc_attr( $event->time ),
1110
- esc_attr( rawurlencode( $event->hook ) ),
1111
- esc_attr( $event->sig )
1112
- );
1113
- }
1114
- echo '</th>';
1115
-
1116
- if ( 'crontrol_cron_job' === $event->hook ) {
1117
- if ( ! empty( $event->args['name'] ) ) {
1118
- /* translators: 1: The name of the PHP cron event. */
1119
- echo '<td><em>' . esc_html( sprintf( __( 'PHP Cron (%s)', 'wp-crontrol' ), $event->args['name'] ) ) . '</em></td>';
1120
- } else {
1121
- echo '<td><em>' . esc_html__( 'PHP Cron', 'wp-crontrol' ) . '</em></td>';
1122
- }
1123
- echo '<td><em>' . esc_html__( 'PHP Code', 'wp-crontrol' ) . '</em></td>';
1124
- echo '<td><em>' . esc_html__( 'WP Crontrol', 'wp-crontrol' ) . '</em></td>';
1125
- } else {
1126
- echo '<td>' . esc_html( $event->hook ) . '</td>';
1127
- echo '<td>' . $args . '</td>'; // WPCS:: XSS ok.
1128
- echo '<td>';
1129
- $callbacks = array();
1130
- foreach ( $this->get_action_callbacks( $event->hook ) as $callback ) {
1131
- $callbacks[] = '<pre style="margin-top:0">' . self::output_callback( $callback ) . '</pre>';
1132
- }
1133
- echo implode( '', $callbacks ); // WPCS:: XSS ok.
1134
- echo '</td>';
1135
- }
1136
 
1137
- echo '<td style="white-space:nowrap">';
1138
- printf( '%s (%s)',
1139
- esc_html( get_date_from_gmt( date( 'Y-m-d H:i:s', $event->time ), $time_format ) ),
1140
- esc_html( $this->time_since( time(), $event->time ) )
1141
- );
1142
- echo '</td>';
1143
-
1144
- echo '<td style="white-space:nowrap">';
1145
- if ( $event->schedule ) {
1146
- $schedule_name = $this->get_schedule_name( $event );
1147
- if ( is_wp_error( $schedule_name ) ) {
1148
- printf(
1149
- '<span class="dashicons dashicons-warning"></span>%s',
1150
- esc_html( $schedule_name->get_error_message() )
1151
- );
1152
- } else {
1153
- echo esc_html( $schedule_name );
1154
- }
1155
- } else {
1156
- esc_html_e( 'Non-repeating', 'wp-crontrol' );
1157
- }
1158
- echo '</td>';
1159
 
1160
- $links = array();
 
 
 
 
 
 
 
 
 
 
 
 
 
1161
 
1162
- echo '<td style="white-space:nowrap"><span class="row-actions visible">';
 
 
 
 
1163
 
1164
- if ( ( 'crontrol_cron_job' !== $event->hook ) || $can_edit_files ) {
1165
- $link = array(
1166
- 'page' => 'crontrol_admin_manage_page',
1167
- 'action' => 'edit-cron',
1168
- 'id' => rawurlencode( $event->hook ),
1169
- 'sig' => rawurlencode( $event->sig ),
1170
- 'next_run' => rawurlencode( $event->time ),
1171
- );
1172
- $link = add_query_arg( $link, admin_url( 'tools.php' ) ) . '#crontrol_form';
1173
- $links[] = "<a href='" . esc_url( $link ) . "'>" . esc_html__( 'Edit', 'wp-crontrol' ) . '</a>';
1174
- }
1175
 
1176
- $link = array(
1177
- 'page' => 'crontrol_admin_manage_page',
1178
- 'action' => 'run-cron',
1179
- 'id' => rawurlencode( $event->hook ),
1180
- 'sig' => rawurlencode( $event->sig ),
1181
- 'next_run' => rawurlencode( $event->time ),
1182
- );
1183
- $link = add_query_arg( $link, admin_url( 'tools.php' ) );
1184
- $link = wp_nonce_url( $link, "run-cron_{$event->hook}_{$event->sig}" );
1185
- $links[] = "<a href='" . esc_url( $link ) . "'>" . esc_html__( 'Run Now', 'wp-crontrol' ) . '</a>';
1186
-
1187
- if ( ! in_array( $event->hook, $core_hooks, true ) && ( ( 'crontrol_cron_job' !== $event->hook ) || $can_edit_files ) ) {
1188
- $link = array(
1189
- 'page' => 'crontrol_admin_manage_page',
1190
- 'action' => 'delete-cron',
1191
- 'id' => rawurlencode( $event->hook ),
1192
- 'sig' => rawurlencode( $event->sig ),
1193
- 'next_run' => rawurlencode( $event->time ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1194
  );
1195
- $link = add_query_arg( $link, admin_url( 'tools.php' ) );
1196
- $link = wp_nonce_url( $link, "delete-cron_{$event->hook}_{$event->sig}_{$event->time}" );
1197
- $links[] = "<span class='delete'><a href='" . esc_url( $link ) . "'>" . esc_html__( 'Delete', 'wp-crontrol' ) . '</a></span>';
1198
  }
 
1199
 
1200
- echo implode( ' | ', $links ); // WPCS:: XSS ok.
1201
- echo '</span></td>';
1202
- echo '</tr>';
1203
-
 
1204
  }
1205
- }
1206
- ?>
1207
- </tbody>
1208
- </table>
1209
- <p style="float:right">
1210
- <?php
1211
- echo esc_html( sprintf(
1212
- /* translators: %s: The current date and time */
1213
- __( 'Current site time: %s', 'wp-crontrol' ),
1214
- date_i18n( 'Y-m-d H:i:s' )
1215
- ) );
1216
  ?>
1217
- </p>
1218
  <?php
1219
- wp_nonce_field( 'bulk-delete-crons' );
1220
- submit_button(
1221
- __( 'Delete Selected Events', 'wp-crontrol' ),
1222
- 'primary large',
1223
- 'delete_crons'
1224
- );
1225
  ?>
1226
- </form>
1227
-
1228
- </div>
1229
- <?php
1230
- if ( is_array( $doing_edit ) ) {
1231
- $this->show_cron_form( 'crontrol_cron_job' == $doing_edit['hookname'], $doing_edit );
1232
- } else {
1233
- $this->show_cron_form( ( isset( $_GET['action'] ) && 'new-php-cron' === $_GET['action'] ), false );
1234
- }
1235
- }
1236
-
1237
- /**
1238
- * Returns an array of the callback functions that are attached to the given hook name.
1239
- *
1240
- * @param string $name The hook name.
1241
- * @return array[] Array of callbacks attached to the hook.
1242
- */
1243
- protected function get_action_callbacks( $name ) {
1244
- global $wp_filter;
1245
 
1246
- $actions = array();
 
 
 
 
 
 
 
1247
 
1248
- if ( isset( $wp_filter[ $name ] ) ) {
1249
 
1250
- // See http://core.trac.wordpress.org/ticket/17817.
1251
- $action = $wp_filter[ $name ];
 
1252
 
1253
- foreach ( $action as $priority => $callbacks ) {
1254
- foreach ( $callbacks as $callback ) {
1255
- $callback = self::populate_callback( $callback );
1256
 
1257
- $actions[] = array(
1258
- 'priority' => $priority,
1259
- 'callback' => $callback,
1260
- );
1261
- }
1262
  }
1263
  }
1264
-
1265
- return $actions;
1266
  }
1267
 
1268
- /**
1269
- * Populates the details of the given callback function.
1270
- *
1271
- * @param array $callback A callback entry.
1272
- * @return The updated callback entry.
1273
- */
1274
- public static function populate_callback( array $callback ) {
1275
- // If Query Monitor is installed, use its rich callback analysis.
1276
- if ( method_exists( 'QM_Util', 'populate_callback' ) ) {
1277
- return QM_Util::populate_callback( $callback );
1278
- }
1279
 
1280
- if ( is_string( $callback['function'] ) && ( false !== strpos( $callback['function'], '::' ) ) ) {
1281
- $callback['function'] = explode( '::', $callback['function'] );
1282
- }
 
 
 
 
 
 
 
 
1283
 
1284
- if ( is_array( $callback['function'] ) ) {
1285
- if ( is_object( $callback['function'][0] ) ) {
1286
- $class = get_class( $callback['function'][0] );
1287
- $access = '->';
1288
- } else {
1289
- $class = $callback['function'][0];
1290
- $access = '::';
1291
- }
1292
 
1293
- $callback['name'] = $class . $access . $callback['function'][1] . '()';
1294
- } elseif ( is_object( $callback['function'] ) ) {
1295
- if ( is_a( $callback['function'], 'Closure' ) ) {
1296
- $callback['name'] = 'Closure';
1297
- } else {
1298
- $class = get_class( $callback['function'] );
1299
- $callback['name'] = $class . '->__invoke()';
1300
- }
1301
  } else {
1302
- $callback['name'] = $callback['function'] . '()';
 
1303
  }
1304
 
1305
- return $callback;
 
 
 
 
 
1306
 
 
 
 
 
1307
  }
1308
 
1309
- /**
1310
- * Returns a user-friendly representation of the callback function.
1311
- *
1312
- * @param array $callback The callback entry.
1313
- * @return string The displayable version of the callback name.
1314
- */
1315
- public static function output_callback( array $callback ) {
1316
- $qm = WP_PLUGIN_DIR . '/query-monitor/query-monitor.php';
1317
- $html = plugin_dir_path( $qm ) . 'output/Html.php';
1318
-
1319
- // If Query Monitor is installed, use its rich callback output.
1320
- if ( class_exists( 'QueryMonitor' ) && file_exists( $html ) ) {
1321
- require_once $html;
1322
-
1323
- if ( class_exists( 'QM_Output_Html' ) ) {
1324
- return QM_Output_Html::output_filename(
1325
- $callback['callback']['name'],
1326
- $callback['callback']['file'],
1327
- $callback['callback']['line']
1328
- );
 
 
 
 
1329
  }
1330
- }
1331
 
1332
- return $callback['callback']['name'];
 
 
 
 
 
1333
  }
1334
 
1335
- /**
1336
- * Pretty-prints the difference in two times.
1337
- *
1338
- * @param int $older_date Unix timestamp.
1339
- * @param int $newer_date Unix timestamp.
1340
- * @return string The pretty time_since value
1341
- * @link http://binarybonsai.com/code/timesince.txt
1342
- */
1343
- public function time_since( $older_date, $newer_date ) {
1344
- return $this->interval( $newer_date - $older_date );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1345
  }
1346
 
1347
  /**
1348
- * Converts a period of time in seconds into a human-readable format representing the interval.
1349
- *
1350
- * Example:
1351
- *
1352
- * echo self::interval( 90 );
1353
- * // 1 minute 30 seconds
1354
- *
1355
- * @param int $since A period of time in seconds.
1356
- * @return string An interval represented as a string.
1357
  */
1358
- public function interval( $since ) {
1359
- // Array of time period chunks.
1360
- $chunks = array(
1361
- /* translators: 1: The number of years in an interval of time. */
1362
- array( 60 * 60 * 24 * 365, _n_noop( '%s year', '%s years', 'wp-crontrol' ) ),
1363
- /* translators: 1: The number of months in an interval of time. */
1364
- array( 60 * 60 * 24 * 30, _n_noop( '%s month', '%s months', 'wp-crontrol' ) ),
1365
- /* translators: 1: The number of weeks in an interval of time. */
1366
- array( 60 * 60 * 24 * 7, _n_noop( '%s week', '%s weeks', 'wp-crontrol' ) ),
1367
- /* translators: 1: The number of days in an interval of time. */
1368
- array( 60 * 60 * 24, _n_noop( '%s day', '%s days', 'wp-crontrol' ) ),
1369
- /* translators: 1: The number of hours in an interval of time. */
1370
- array( 60 * 60, _n_noop( '%s hour', '%s hours', 'wp-crontrol' ) ),
1371
- /* translators: 1: The number of minutes in an interval of time. */
1372
- array( 60, _n_noop( '%s minute', '%s minutes', 'wp-crontrol' ) ),
1373
- /* translators: 1: The number of seconds in an interval of time. */
1374
- array( 1, _n_noop( '%s second', '%s seconds', 'wp-crontrol' ) ),
1375
- );
1376
 
1377
- if ( $since <= 0 ) {
1378
- return __( 'now', 'wp-crontrol' );
1379
- }
 
1380
 
1381
- /**
1382
- * We only want to output two chunks of time here, eg:
1383
- * x years, xx months
1384
- * x days, xx hours
1385
- * so there's only two bits of calculation below:
1386
- */
1387
- $j = count( $chunks );
1388
-
1389
- // Step one: the first chunk.
1390
- for ( $i = 0; $i < $j; $i++ ) {
1391
- $seconds = $chunks[ $i ][0];
1392
- $name = $chunks[ $i ][1];
1393
-
1394
- // Finding the biggest chunk (if the chunk fits, break).
1395
- $count = floor( $since / $seconds );
1396
- if ( $count ) {
1397
- break;
1398
- }
1399
  }
 
1400
 
1401
- // Set output var.
1402
- $output = sprintf( translate_nooped_plural( $name, $count, 'wp-crontrol' ), $count );
1403
-
1404
- // Step two: the second chunk.
1405
- if ( $i + 1 < $j ) {
1406
- $seconds2 = $chunks[ $i + 1 ][0];
1407
- $name2 = $chunks[ $i + 1 ][1];
1408
- $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
1409
- if ( $count2 ) {
1410
- // Add to output var.
1411
- $output .= ' ' . sprintf( translate_nooped_plural( $name2, $count2, 'wp-crontrol' ), $count2 );
1412
- }
1413
  }
1414
-
1415
- return $output;
1416
  }
1417
 
1418
- /**
1419
- * Returns the schedule display name for a given event.
1420
- *
1421
- * @param stdClass $event A WP-Cron event.
1422
- * @return string|WP_Error The interval display name, or a WP_Error object if no such schedule exists.
1423
- */
1424
- protected function get_schedule_name( stdClass $event ) {
1425
- $schedules = $this->get_schedules();
1426
-
1427
- if ( isset( $schedules[ $event->schedule ] ) ) {
1428
- return $schedules[ $event->schedule ]['display'];
1429
- }
1430
 
1431
- return new WP_Error( 'unknown_schedule', __( 'Unknown', 'wp-crontrol' ) );
 
 
 
 
 
 
 
 
1432
  }
1433
 
1434
- /**
1435
- * Enqueues the editor UI that's used for the PHP cron event code editor.
1436
- */
1437
- public function enqueue_code_editor() {
1438
- if ( ! function_exists( 'wp_enqueue_code_editor' ) ) {
1439
- return;
1440
- }
1441
- if ( ! current_user_can( 'edit_files' ) ) {
1442
- return;
1443
- }
1444
 
1445
- $settings = wp_enqueue_code_editor( array(
1446
- 'type' => 'text/x-php',
1447
- ) );
1448
 
1449
- if ( false === $settings ) {
1450
- return;
1451
- }
 
 
 
 
 
 
1452
 
1453
- wp_add_inline_script( 'code-editor', sprintf(
1454
- 'jQuery( function( $ ) {
1455
- if ( $( "#hookcode" ).length ) {
1456
- wp.codeEditor.initialize( "hookcode", %s );
1457
- }
1458
- } );',
1459
- wp_json_encode( $settings )
1460
- ) );
1461
- }
1462
 
1463
- /**
1464
- * Filters the list of query arguments which get removed from admin area URLs in WordPress.
1465
- *
1466
- * @link https://core.trac.wordpress.org/ticket/23367
1467
- *
1468
- * @param string[] $args List of removable query arguments.
1469
- * @return string[] Updated list of removable query arguments.
1470
- */
1471
- public function filter_removable_query_args( array $args ) {
1472
- return array_merge( $args, array(
1473
- 'crontrol_message',
1474
- 'crontrol_name',
1475
- ) );
1476
  }
1477
 
1478
- /**
1479
- * Singleton instance instantiator.
1480
- */
1481
- public static function init() {
1482
 
1483
- static $instance = null;
 
 
1484
 
1485
- if ( ! $instance ) {
1486
- $instance = new Crontrol();
1487
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1488
 
1489
- return $instance;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1490
 
 
 
 
1491
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1492
  }
1493
 
1494
  // Get this show on the road.
1495
- Crontrol::init();
1
  <?php
2
  /**
3
+ * Plugin Name: WP Crontrol
4
+ * Plugin URI: https://wordpress.org/plugins/wp-crontrol/
5
+ * Description: WP Crontrol lets you view and control what's happening in the WP-Cron system.
6
+ * Author: John Blackbourn & crontributors
7
+ * Author URI: https://github.com/johnbillion/wp-crontrol/graphs/contributors
8
+ * Version: 1.8.0
9
+ * Text Domain: wp-crontrol
10
+ * Domain Path: /languages/
11
+ * Requires PHP: 5.3.6
12
+ * License: GPL v2 or later
13
  *
14
  * LICENSE
15
  * This file is part of WP Crontrol.
24
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
  * GNU General Public License for more details.
26
  *
27
+ * @package wp-crontrol
28
+ * @author John Blackbourn <john@johnblackbourn.com> & Edward Dale <scompt@scompt.com>
29
+ * @copyright Copyright 2008 Edward Dale, 2012-2020 John Blackbourn
30
  * @license http://www.gnu.org/licenses/gpl.txt GPL 2.0
31
  * @link https://wordpress.org/plugins/wp-crontrol/
32
  * @since 0.2
33
  */
34
 
35
+ namespace Crontrol;
36
+
37
+ use WP_Error;
38
+
39
  defined( 'ABSPATH' ) || die();
40
 
41
+ require_once __DIR__ . '/src/event.php';
42
+ require_once __DIR__ . '/src/schedule.php';
43
+
44
  /**
45
+ * Hook onto all of the actions and filters needed by the plugin.
46
  */
47
+ function init_hooks() {
48
+ $plugin_file = plugin_basename( __FILE__ );
49
 
50
+ add_action( 'init', __NAMESPACE__ . '\action_init' );
51
+ add_action( 'init', __NAMESPACE__ . '\action_handle_posts' );
52
+ add_action( 'admin_menu', __NAMESPACE__ . '\action_admin_menu' );
53
+ add_filter( "plugin_action_links_{$plugin_file}", __NAMESPACE__ . '\plugin_action_links', 10, 4 );
54
+ add_filter( 'removable_query_args', __NAMESPACE__ . '\filter_removable_query_args' );
55
+ add_filter( 'in_admin_header', __NAMESPACE__ . '\do_tabs' );
56
 
57
+ add_action( 'load-tools_page_crontrol_admin_manage_page', __NAMESPACE__ . '\enqueue_code_editor' );
58
 
59
+ add_filter( 'cron_schedules', __NAMESPACE__ . '\filter_cron_schedules' );
60
+ add_action( 'crontrol_cron_job', __NAMESPACE__ . '\action_php_cron_event' );
61
+ add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
62
+ }
 
63
 
64
+ /**
65
+ * Run using the 'init' action.
66
+ */
67
+ function action_init() {
68
+ load_plugin_textdomain( 'wp-crontrol', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
69
+ }
70
 
71
+ /**
72
+ * Handles any POSTs made by the plugin. Run using the 'init' action.
73
+ */
74
+ function action_handle_posts() {
75
+ if ( isset( $_POST['new_cron'] ) ) {
76
+ if ( ! current_user_can( 'manage_options' ) ) {
77
+ wp_die( esc_html__( 'You are not allowed to add new cron events.', 'wp-crontrol' ), 401 );
78
+ }
79
+ check_admin_referer( 'new-cron' );
80
+ extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' );
81
+ if ( 'crontrol_cron_job' === $in_hookname && ! current_user_can( 'edit_files' ) ) {
82
+ wp_die( esc_html__( 'You are not allowed to add new PHP cron events.', 'wp-crontrol' ), 401 );
83
+ }
84
+ $in_args = json_decode( $in_args, true );
85
 
86
+ if ( empty( $in_args ) ) {
87
+ $in_args = array();
88
+ }
89
 
90
+ $next_run_local = ( 'custom' === $in_next_run_date_local ) ? $in_next_run_date_local_custom : $in_next_run_date_local;
 
 
 
 
 
 
 
91
 
92
+ $added = Event\add( $next_run_local, $in_schedule, $in_hookname, $in_args );
 
 
 
 
 
93
 
94
+ $redirect = array(
95
+ 'page' => 'crontrol_admin_manage_page',
96
+ 'crontrol_message' => '5',
97
+ 'crontrol_name' => rawurlencode( $in_hookname ),
98
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
+ if ( false === $added ) {
101
+ $redirect['crontrol_message'] = '10';
102
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
105
+ exit;
 
 
106
 
107
+ } elseif ( isset( $_POST['new_php_cron'] ) ) {
108
+ if ( ! current_user_can( 'edit_files' ) ) {
109
+ wp_die( esc_html__( 'You are not allowed to add new PHP cron events.', 'wp-crontrol' ), 401 );
110
+ }
111
+ check_admin_referer( 'new-cron' );
112
+ extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' );
113
+ $next_run_local = ( 'custom' === $in_next_run_date_local ) ? $in_next_run_date_local_custom : $in_next_run_date_local;
114
+ $args = array(
115
+ 'code' => $in_hookcode,
116
+ 'name' => $in_eventname,
117
+ );
118
 
119
+ $added = Event\add( $next_run_local, $in_schedule, 'crontrol_cron_job', $args );
 
 
120
 
121
+ $hookname = ( ! empty( $in_eventname ) ) ? $in_eventname : __( 'PHP Cron', 'wp-crontrol' );
122
+ $redirect = array(
123
+ 'page' => 'crontrol_admin_manage_page',
124
+ 'crontrol_message' => '5',
125
+ 'crontrol_name' => rawurlencode( $hookname ),
126
+ );
 
 
 
 
 
127
 
128
+ if ( false === $added ) {
129
+ $redirect['crontrol_message'] = '10';
130
+ }
 
131
 
132
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
133
+ exit;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
+ } elseif ( isset( $_POST['edit_cron'] ) ) {
136
+ if ( ! current_user_can( 'manage_options' ) ) {
137
+ wp_die( esc_html__( 'You are not allowed to edit cron events.', 'wp-crontrol' ), 401 );
138
+ }
139
+
140
+ extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' );
141
+ check_admin_referer( "edit-cron_{$in_original_hookname}_{$in_original_sig}_{$in_original_next_run_utc}" );
142
+
143
+ if ( 'crontrol_cron_job' === $in_hookname && ! current_user_can( 'edit_files' ) ) {
144
+ wp_die( esc_html__( 'You are not allowed to edit PHP cron events.', 'wp-crontrol' ), 401 );
145
+ }
146
+
147
+ $in_args = json_decode( $in_args, true );
148
+
149
+ if ( empty( $in_args ) ) {
150
+ $in_args = array();
151
+ }
152
+
153
+ Event\delete( $in_original_hookname, $in_original_sig, $in_original_next_run_utc );
154
+
155
+ $next_run_local = ( 'custom' === $in_next_run_date_local ) ? $in_next_run_date_local_custom : $in_next_run_date_local;
156
+
157
+ $added = Event\add( $next_run_local, $in_schedule, $in_hookname, $in_args );
158
+
159
+ $redirect = array(
160
+ 'page' => 'crontrol_admin_manage_page',
161
+ 'crontrol_message' => '4',
162
+ 'crontrol_name' => rawurlencode( $in_hookname ),
163
+ );
164
+
165
+ if ( false === $added ) {
166
+ $redirect['crontrol_message'] = '10';
167
+ }
168
+
169
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
170
+ exit;
171
+
172
+ } elseif ( isset( $_POST['edit_php_cron'] ) ) {
173
+ if ( ! current_user_can( 'edit_files' ) ) {
174
+ wp_die( esc_html__( 'You are not allowed to edit PHP cron events.', 'wp-crontrol' ), 401 );
175
+ }
176
+
177
+ extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' );
178
+ check_admin_referer( "edit-cron_{$in_original_hookname}_{$in_original_sig}_{$in_original_next_run_utc}" );
179
+ $args = array(
180
+ 'code' => $in_hookcode,
181
+ 'name' => $in_eventname,
182
+ );
183
+
184
+ Event\delete( $in_original_hookname, $in_original_sig, $in_original_next_run_utc );
185
+
186
+ $next_run_local = ( 'custom' === $in_next_run_date_local ) ? $in_next_run_date_local_custom : $in_next_run_date_local;
187
+
188
+ $added = Event\add( $next_run_local, $in_schedule, 'crontrol_cron_job', $args );
189
+
190
+ $hookname = ( ! empty( $in_eventname ) ) ? $in_eventname : __( 'PHP Cron', 'wp-crontrol' );
191
+ $redirect = array(
192
+ 'page' => 'crontrol_admin_manage_page',
193
+ 'crontrol_message' => '4',
194
+ 'crontrol_name' => rawurlencode( $hookname ),
195
+ );
196
+
197
+ if ( false === $added ) {
198
+ $redirect['crontrol_message'] = '10';
199
+ }
200
+
201
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
202
+ exit;
203
+
204
+ } elseif ( isset( $_POST['new_schedule'] ) ) {
205
+ if ( ! current_user_can( 'manage_options' ) ) {
206
+ wp_die( esc_html__( 'You are not allowed to add new cron schedules.', 'wp-crontrol' ), 401 );
207
+ }
208
+ check_admin_referer( 'new-sched' );
209
+ $name = wp_unslash( $_POST['internal_name'] );
210
+ $interval = wp_unslash( $_POST['interval'] );
211
+ $display = wp_unslash( $_POST['display_name'] );
212
+
213
+ // The user entered something that wasn't a number.
214
+ // Try to convert it with strtotime.
215
+ if ( ! is_numeric( $interval ) ) {
216
+ $now = time();
217
+ $future = strtotime( $interval, $now );
218
+ if ( false === $future || $now > $future ) {
219
  $redirect = array(
220
  'page' => 'crontrol_admin_options_page',
221
  'crontrol_message' => '7',
224
  wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
225
  exit;
226
  }
227
+ $interval = $future - $now;
228
+ } elseif ( $interval <= 0 ) {
229
  $redirect = array(
230
  'page' => 'crontrol_admin_options_page',
231
+ 'crontrol_message' => '7',
232
+ 'crontrol_name' => rawurlencode( $interval ),
233
  );
234
  wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
235
  exit;
236
+ }
237
 
238
+ Schedule\add( $name, $interval, $display );
239
+ $redirect = array(
240
+ 'page' => 'crontrol_admin_options_page',
241
+ 'crontrol_message' => '3',
242
+ 'crontrol_name' => rawurlencode( $name ),
243
+ );
244
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
245
+ exit;
 
 
 
 
 
 
246
 
247
+ } elseif ( isset( $_GET['action'] ) && 'delete-sched' === $_GET['action'] ) {
248
+ if ( ! current_user_can( 'manage_options' ) ) {
249
+ wp_die( esc_html__( 'You are not allowed to delete cron schedules.', 'wp-crontrol' ), 401 );
250
+ }
251
+ $id = wp_unslash( $_GET['id'] );
252
+ check_admin_referer( "delete-sched_{$id}" );
253
+ Schedule\delete( $id );
254
+ $redirect = array(
255
+ 'page' => 'crontrol_admin_options_page',
256
+ 'crontrol_message' => '2',
257
+ 'crontrol_name' => rawurlencode( $id ),
258
+ );
259
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
260
+ exit;
261
 
262
+ } elseif ( isset( $_POST['action'] ) && 'delete_crons' === $_POST['action'] ) {
263
+ if ( ! current_user_can( 'manage_options' ) ) {
264
+ wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ), 401 );
265
+ }
266
+ check_admin_referer( 'bulk-crontrol-events' );
267
+
268
+ if ( empty( $_POST['delete'] ) ) {
269
+ return;
270
+ }
271
+
272
+ $delete = wp_unslash( $_POST['delete'] );
273
+ $deleted = 0;
274
 
275
+ foreach ( $delete as $next_run_utc => $events ) {
276
+ foreach ( $events as $id => $sig ) {
277
+ if ( 'crontrol_cron_job' === $id && ! current_user_can( 'edit_files' ) ) {
278
+ continue;
279
+ }
280
+ if ( Event\delete( urldecode( $id ), $sig, $next_run_utc ) ) {
281
+ $deleted++;
 
 
 
 
282
  }
283
  }
284
+ }
285
+
286
+ $redirect = array(
287
+ 'page' => 'crontrol_admin_manage_page',
288
+ 'crontrol_name' => $deleted,
289
+ 'crontrol_message' => '9',
290
+ );
291
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
292
+ exit;
293
+
294
+ } elseif ( isset( $_GET['action'] ) && 'delete-cron' === $_GET['action'] ) {
295
+ if ( ! current_user_can( 'manage_options' ) ) {
296
+ wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ), 401 );
297
+ }
298
+ $id = wp_unslash( $_GET['id'] );
299
+ $sig = wp_unslash( $_GET['sig'] );
300
+ $next_run_utc = intval( $_GET['next_run_utc'] );
301
+ check_admin_referer( "delete-cron_{$id}_{$sig}_{$next_run_utc}" );
302
 
303
+ if ( 'crontrol_cron_job' === $id && ! current_user_can( 'edit_files' ) ) {
304
+ wp_die( esc_html__( 'You are not allowed to delete PHP cron events.', 'wp-crontrol' ), 401 );
305
+ }
306
+
307
+ if ( Event\delete( $id, $sig, $next_run_utc ) ) {
308
  $redirect = array(
309
  'page' => 'crontrol_admin_manage_page',
310
+ 'crontrol_message' => '6',
311
+ 'crontrol_name' => rawurlencode( $id ),
312
+ );
313
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
314
+ exit;
315
+ } else {
316
+ $redirect = array(
317
+ 'page' => 'crontrol_admin_manage_page',
318
+ 'crontrol_message' => '7',
319
+ 'crontrol_name' => rawurlencode( $id ),
320
  );
321
  wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
322
  exit;
323
 
324
+ };
 
 
 
 
 
 
 
 
 
 
 
325
 
326
+ } elseif ( isset( $_GET['action'] ) && 'delete-hook' === $_GET['action'] ) {
327
+ if ( ! current_user_can( 'manage_options' ) ) {
328
+ wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ), 401 );
329
+ }
330
+ $id = wp_unslash( $_GET['id'] );
331
+ $deleted = false;
332
+ check_admin_referer( "delete-hook_{$id}" );
 
 
 
 
 
 
 
 
 
 
 
333
 
334
+ if ( 'crontrol_cron_job' === $id ) {
335
+ wp_die( esc_html__( 'You are not allowed to delete PHP cron events.', 'wp-crontrol' ), 401 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
  }
 
337
 
338
+ if ( function_exists( 'wp_unschedule_hook' ) ) {
339
+ $deleted = wp_unschedule_hook( $id );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  }
 
 
341
 
342
+ if ( 0 === $deleted ) {
343
+ $redirect = array(
344
+ 'page' => 'crontrol_admin_manage_page',
345
+ 'crontrol_message' => '3',
346
+ 'crontrol_name' => rawurlencode( $id ),
347
+ );
348
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
349
+ exit;
350
+ } elseif ( $deleted ) {
351
+ $redirect = array(
352
+ 'page' => 'crontrol_admin_manage_page',
353
+ 'crontrol_message' => '2',
354
+ 'crontrol_name' => rawurlencode( $id ),
355
+ );
356
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
357
+ exit;
358
  } else {
359
+ $redirect = array(
360
+ 'page' => 'crontrol_admin_manage_page',
361
+ 'crontrol_message' => '7',
362
+ 'crontrol_name' => rawurlencode( $id ),
363
+ );
364
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
365
+ exit;
366
  }
367
+ } elseif ( isset( $_GET['action'] ) && 'run-cron' === $_GET['action'] ) {
368
+ if ( ! current_user_can( 'manage_options' ) ) {
369
+ wp_die( esc_html__( 'You are not allowed to run cron events.', 'wp-crontrol' ), 401 );
370
  }
371
+ $id = wp_unslash( $_GET['id'] );
372
+ $sig = wp_unslash( $_GET['sig'] );
373
+ check_admin_referer( "run-cron_{$id}_{$sig}" );
374
+ if ( Event\run( $id, $sig ) ) {
375
+ $redirect = array(
376
+ 'page' => 'crontrol_admin_manage_page',
377
+ 'crontrol_message' => '1',
378
+ 'crontrol_name' => rawurlencode( $id ),
379
+ );
380
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
381
+ exit;
382
  } else {
383
+ $redirect = array(
384
+ 'page' => 'crontrol_admin_manage_page',
385
+ 'crontrol_message' => '8',
386
+ 'crontrol_name' => rawurlencode( $id ),
387
+ );
388
+ wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
389
+ exit;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
  }
391
  }
392
+ }
393
 
394
+ /**
395
+ * Adds options & management pages to the admin menu.
396
+ *
397
+ * Run using the 'admin_menu' action.
398
+ */
399
+ function action_admin_menu() {
400
+ add_options_page( esc_html__( 'Cron Schedules', 'wp-crontrol' ), esc_html__( 'Cron Schedules', 'wp-crontrol' ), 'manage_options', 'crontrol_admin_options_page', __NAMESPACE__ . '\admin_options_page' );
401
+ add_management_page( esc_html__( 'Cron Events', 'wp-crontrol' ), esc_html__( 'Cron Events', 'wp-crontrol' ), 'manage_options', 'crontrol_admin_manage_page', __NAMESPACE__ . '\admin_manage_page' );
402
+ }
403
 
404
+ /**
405
+ * Adds items to the plugin's action links on the Plugins listing screen.
406
+ *
407
+ * @param string[] $actions Array of action links.
408
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
409
+ * @param array $plugin_data An array of plugin data.
410
+ * @param string $context The plugin context.
411
+ * @return string[] Array of action links.
412
+ */
413
+ function plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
414
+ $new = array(
415
+ 'crontrol-events' => sprintf(
416
  '<a href="%s">%s</a>',
417
  esc_url( admin_url( 'tools.php?page=crontrol_admin_manage_page' ) ),
418
+ esc_html__( 'Events', 'wp-crontrol' )
419
+ ),
420
+ 'crontrol-schedules' => sprintf(
421
  '<a href="%s">%s</a>',
422
  esc_url( admin_url( 'options-general.php?page=crontrol_admin_options_page' ) ),
423
+ esc_html__( 'Schedules', 'wp-crontrol' )
424
+ ),
425
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426
 
427
+ return array_merge( $new, $actions );
428
+ }
 
 
 
 
 
 
 
 
 
 
429
 
430
+ /**
431
+ * Gives WordPress the plugin's set of cron schedules.
432
+ *
433
+ * Called by the `cron_schedules` filter.
434
+ *
435
+ * @param array[] $scheds Array of cron schedule arrays. Usually empty.
436
+ * @return array[] Array of modified cron schedule arrays.
437
+ */
438
+ function filter_cron_schedules( array $scheds ) {
439
+ $new_scheds = get_option( 'crontrol_schedules', array() );
440
+ return array_merge( $new_scheds, $scheds );
441
+ }
442
 
443
+ /**
444
+ * Displays the options page for the plugin.
445
+ */
446
+ function admin_options_page() {
447
+ $messages = array(
448
+ '2' => array(
449
  /* translators: 1: The name of the cron schedule. */
450
+ __( 'Deleted the cron schedule %s.', 'wp-crontrol' ),
451
+ 'success',
452
+ ),
453
+ '3' => array(
454
  /* translators: 1: The name of the cron schedule. */
455
+ __( 'Added the cron schedule %s.', 'wp-crontrol' ),
456
+ 'success',
457
+ ),
458
+ '7' => array(
459
  /* translators: 1: The name of the cron schedule. */
460
+ __( 'Cron schedule not added because there was a problem parsing %s.', 'wp-crontrol' ),
461
+ 'error',
462
+ ),
463
+ );
464
+ if ( isset( $_GET['crontrol_message'] ) && isset( $_GET['crontrol_name'] ) && isset( $messages[ $_GET['crontrol_message'] ] ) ) {
465
+ $hook = wp_unslash( $_GET['crontrol_name'] );
466
+ $message = wp_unslash( $_GET['crontrol_message'] );
467
+
468
+ printf(
469
+ '<div id="crontrol-message" class="notice notice-%1$s is-dismissible"><p>%2$s</p></div>',
470
+ esc_attr( $messages[ $message ][1] ),
471
+ sprintf(
472
+ esc_html( $messages[ $message ][0] ),
473
+ '<strong>' . esc_html( $hook ) . '</strong>'
474
+ )
475
  );
476
+ }
 
 
 
 
 
 
477
 
478
+ require_once __DIR__ . '/src/schedule-list-table.php';
479
+
480
+ $table = new Schedule_List_Table();
481
+
482
+ $table->prepare_items();
483
+
484
+ ?>
485
+ <div class="wrap">
486
+
487
+ <h1><?php esc_html_e( 'Cron Schedules', 'wp-crontrol' ); ?></h1>
488
+
489
+ <?php $table->views(); ?>
490
+
491
+ <div class="table-responsive">
492
+ <?php $table->display(); ?>
493
+ </div>
494
+
495
+ <div class="wrap narrow">
496
+ <h2 class="title"><?php esc_html_e( 'Add Cron Schedule', 'wp-crontrol' ); ?></h2>
497
+ <p><?php esc_html_e( 'Adding a new cron schedule will allow you to schedule events that re-occur at the given interval.', 'wp-crontrol' ); ?></p>
498
+ <form method="post" action="options-general.php?page=crontrol_admin_options_page">
499
+ <table class="form-table">
500
+ <tbody>
501
+ <tr>
502
+ <th valign="top" scope="row"><label for="cron_internal_name"><?php esc_html_e( 'Internal name', 'wp-crontrol' ); ?></label></th>
503
+ <td><input type="text" class="regular-text" value="" id="cron_internal_name" name="internal_name" required/></td>
504
+ </tr>
505
+ <tr>
506
+ <th valign="top" scope="row"><label for="cron_interval"><?php esc_html_e( 'Interval (seconds)', 'wp-crontrol' ); ?></label></th>
507
+ <td><input type="number" class="regular-text" value="" id="cron_interval" name="interval" min="1" step="1" required/></td>
508
+ </tr>
509
+ <tr>
510
+ <th valign="top" scope="row"><label for="cron_display_name"><?php esc_html_e( 'Display name', 'wp-crontrol' ); ?></label></th>
511
+ <td><input type="text" class="regular-text" value="" id="cron_display_name" name="display_name" required/></td>
512
+ </tr>
513
+ </tbody></table>
514
+ <p class="submit"><input id="schedadd-submit" type="submit" class="button button-primary" value="<?php esc_attr_e( 'Add Cron Schedule', 'wp-crontrol' ); ?>" name="new_schedule"/></p>
515
+ <?php wp_nonce_field( 'new-sched' ); ?>
516
+ </form>
517
+ </div>
518
+ <?php
519
+ }
520
 
521
+ /**
522
+ * Gets the status of WP-Cron functionality on the site by performing a test spawn if necessary. Cached for one hour when all is well.
523
+ *
524
+ * @param bool $cache Whether to use the cached result from previous calls.
525
+ * @return true|WP_Error Boolean true if the cron spawner is working as expected, or a `WP_Error` object if not.
526
+ */
527
+ function test_cron_spawn( $cache = true ) {
528
+ global $wp_version;
529
+
530
+ $cron_runner_plugins = array(
531
+ '\HM\Cavalcade\Plugin\Job' => 'Cavalcade',
532
+ '\Automattic\WP\Cron_Control' => 'Cron Control',
533
+ );
534
+
535
+ foreach ( $cron_runner_plugins as $class => $plugin ) {
536
+ if ( class_exists( $class ) ) {
537
+ return new WP_Error( 'crontrol_info', sprintf(
538
+ /* translators: 1: The name of the plugin that controls the running of cron events. */
539
+ __( 'WP-Cron spawning is being managed by the %s plugin.', 'wp-crontrol' ),
540
+ $plugin
541
+ ) );
 
542
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543
  }
544
 
545
+ if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
546
+ return new WP_Error( 'crontrol_info', sprintf(
547
+ /* translators: 1: The name of the PHP constant that is set. */
548
+ __( 'The %s constant is set to true. WP-Cron spawning is disabled.', 'wp-crontrol' ),
549
+ 'DISABLE_WP_CRON'
550
+ ) );
 
551
  }
552
 
553
+ if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
554
+ return new WP_Error( 'crontrol_info', sprintf(
555
+ /* translators: 1: The name of the PHP constant that is set. */
556
+ __( 'The %s constant is set to true.', 'wp-crontrol' ),
557
+ 'ALTERNATE_WP_CRON'
558
+ ) );
 
 
 
 
559
  }
560
 
561
+ $cached_status = get_transient( 'crontrol-cron-test-ok' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
562
 
563
+ if ( $cache && $cached_status ) {
564
+ return true;
565
+ }
 
 
 
 
 
566
 
567
+ $sslverify = version_compare( $wp_version, 4.0, '<' );
568
+ $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
569
+
570
+ $cron_request = apply_filters( 'cron_request', array(
571
+ 'url' => site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ),
572
+ 'key' => $doing_wp_cron,
573
+ 'args' => array(
574
+ 'timeout' => 3,
575
+ 'blocking' => true,
576
+ 'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify ),
577
+ ),
578
+ ) );
579
+
580
+ $cron_request['args']['blocking'] = true;
581
+
582
+ $result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
583
+
584
+ if ( is_wp_error( $result ) ) {
585
+ return $result;
586
+ } elseif ( wp_remote_retrieve_response_code( $result ) >= 300 ) {
587
+ return new WP_Error( 'unexpected_http_response_code', sprintf(
588
+ /* translators: 1: The HTTP response code. */
589
+ __( 'Unexpected HTTP response code: %s', 'wp-crontrol' ),
590
+ intval( wp_remote_retrieve_response_code( $result ) )
591
+ ) );
592
+ } else {
593
+ set_transient( 'crontrol-cron-test-ok', 1, 3600 );
594
+ return true;
595
+ }
596
 
597
+ }
 
 
 
598
 
599
+ /**
600
+ * Shows the status of WP-Cron functionality on the site. Only displays a message when there's a problem.
601
+ */
602
+ function show_cron_status() {
603
+ $status = test_cron_spawn();
604
 
605
+ if ( is_wp_error( $status ) ) {
606
+ if ( 'crontrol_info' === $status->get_error_code() ) {
607
+ ?>
608
+ <div id="cron-status-notice" class="notice notice-info">
609
+ <p><?php echo esc_html( $status->get_error_message() ); ?></p>
610
+ </div>
611
+ <?php
612
+ } else {
613
+ ?>
614
+ <div id="cron-status-error" class="error">
615
+ <p>
616
+ <?php
617
+ printf(
618
+ /* translators: 1: Error message text. */
619
+ esc_html__( 'There was a problem spawning a call to the WP-Cron system on your site. This means WP-Cron events on your site may not work. The problem was: %s', 'wp-crontrol' ),
620
+ '<br><strong>' . esc_html( $status->get_error_message() ) . '</strong>'
621
+ );
622
+ ?>
623
+ </p>
624
+ </div>
625
+ <?php
626
  }
627
+ }
628
+ }
629
 
630
+ /**
631
+ * Get the display name for the site's timezone.
632
+ *
633
+ * @return string The name and UTC offset for the site's timezone.
634
+ */
635
+ function get_timezone_name() {
636
+ $timezone_string = get_option( 'timezone_string', '' );
637
+ $gmt_offset = get_option( 'gmt_offset', 0 );
638
 
639
+ if ( 'UTC' === $timezone_string || ( empty( $gmt_offset ) && empty( $timezone_string ) ) ) {
640
+ return 'UTC';
641
+ }
 
 
 
 
 
 
642
 
643
+ if ( '' === $timezone_string ) {
644
+ return get_utc_offset();
645
+ }
646
 
647
+ return sprintf(
648
+ '%s, %s',
649
+ str_replace( '_', ' ', $timezone_string ),
650
+ get_utc_offset()
651
+ );
652
+ }
653
 
654
+ /**
655
+ * Returns a display value for a UTC offset.
656
+ *
657
+ * Examples:
658
+ * - UTC
659
+ * - UTC+4
660
+ * - UTC-6
661
+ *
662
+ * @return string The UTC offset display value.
663
+ */
664
+ function get_utc_offset() {
665
+ $offset = get_option( 'gmt_offset', 0 );
666
 
667
+ if ( empty( $offset ) ) {
668
+ return 'UTC';
669
  }
670
 
671
+ if ( 0 <= $offset ) {
672
+ $formatted_offset = '+' . (string) $offset;
673
+ } else {
674
+ $formatted_offset = (string) $offset;
675
+ }
676
+ $formatted_offset = str_replace(
677
+ array( '.25', '.5', '.75' ),
678
+ array( ':15', ':30', ':45' ),
679
+ $formatted_offset
680
+ );
681
+ return 'UTC' . $formatted_offset;
682
+ }
683
 
684
+ /**
685
+ * Shows the form used to add/edit cron events.
686
+ *
687
+ * @param array $events The cron event list.
688
+ * @param bool $editing Whether the form is for the event editor.
689
+ * @param bool $is_php Whether the form is for a PHP event.
690
+ * @return void
691
+ */
692
+ function show_cron_form( array $events, $editing, $is_php = null ) {
693
+ $display_args = '';
694
+ $edit_id = null;
695
+ $existing = false;
 
 
 
 
 
 
 
 
 
 
 
696
 
697
+ if ( ! empty( $_GET['id'] ) ) {
698
+ $edit_id = wp_unslash( $_GET['id'] );
699
  }
700
 
701
+ foreach ( $events as $event ) {
702
+ if ( $edit_id === $event->hook && intval( $_GET['next_run_utc'] ) === $event->time && $event->sig === $_GET['sig'] ) {
703
+ $existing = array(
704
+ 'hookname' => $event->hook,
705
+ 'next_run' => $event->time, // UTC
706
+ 'schedule' => ( $event->schedule ? $event->schedule : '_oneoff' ),
707
+ 'sig' => $event->sig,
708
+ 'args' => $event->args,
709
+ );
710
+ break;
 
711
  }
712
+ }
713
 
714
+ if ( null === $is_php ) {
715
+ $is_php = ( $existing && 'crontrol_cron_job' === $existing['hookname'] );
716
+ }
 
 
717
 
718
+ if ( $is_php ) {
719
+ $helper_text = esc_html__( 'Cron events trigger actions in your code. Enter the schedule of the event, as well as the PHP code to execute when the action is triggered.', 'wp-crontrol' );
720
+ } else {
721
+ $helper_text = sprintf(
722
+ /* translators: %s: A file name */
723
+ esc_html__( 'Cron events trigger actions in your code. A cron event needs a corresponding action hook somewhere in code, e.g. the %1$s file in your theme.', 'wp-crontrol' ),
724
+ '<code>functions.php</code>'
725
+ );
726
  }
727
 
728
+ if ( is_array( $existing ) ) {
729
+ $other_fields = wp_nonce_field( "edit-cron_{$existing['hookname']}_{$existing['sig']}_{$existing['next_run']}", '_wpnonce', true, false );
730
+ $other_fields .= sprintf( '<input name="original_hookname" type="hidden" value="%s" />',
731
+ esc_attr( $existing['hookname'] )
 
 
 
 
 
 
732
  );
733
+ $other_fields .= sprintf( '<input name="original_sig" type="hidden" value="%s" />',
734
+ esc_attr( $existing['sig'] )
 
735
  );
736
+ $other_fields .= sprintf( '<input name="original_next_run_utc" type="hidden" value="%s" />',
737
+ esc_attr( $existing['next_run'] )
 
738
  );
739
+ if ( ! empty( $existing['args'] ) ) {
740
+ $display_args = wp_json_encode( $existing['args'] );
741
+ }
742
+ $action = $is_php ? 'edit_php_cron' : 'edit_cron';
743
+ $button = __( 'Update Event', 'wp-crontrol' );
744
+ $next_run_date_local = get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $existing['next_run'] ), 'Y-m-d H:i:s' );
745
+ } else {
746
+ $other_fields = wp_nonce_field( 'new-cron', '_wpnonce', true, false );
747
+ $existing = array(
748
+ 'hookname' => '',
749
+ 'args' => array(),
750
+ 'next_run' => 'now', // UTC
751
+ 'schedule' => false,
752
+ );
753
+
754
+ $action = $is_php ? 'new_php_cron' : 'new_cron';
755
+ $button = __( 'Add Event', 'wp-crontrol' );
756
+ $next_run_date_local = '';
757
+ }
758
+
759
+ if ( $is_php ) {
760
+ if ( ! isset( $existing['args']['code'] ) ) {
761
+ $existing['args']['code'] = '';
762
  }
763
+ if ( ! isset( $existing['args']['name'] ) ) {
764
+ $existing['args']['name'] = '';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
765
  }
766
+ }
767
+
768
+ $allowed = ( ! $is_php || current_user_can( 'edit_files' ) );
769
+ ?>
770
+ <div id="crontrol_form" class="wrap narrow">
771
+ <?php
772
+ if ( $allowed ) {
773
+ if ( $editing ) {
774
+ if ( $is_php ) {
775
+ $heading = __( 'Edit PHP Cron Event', 'wp-crontrol' );
776
+ } else {
777
+ $heading = __( 'Edit Cron Event', 'wp-crontrol' );
778
+ }
779
+ } else {
780
+ if ( $is_php ) {
781
+ $heading = __( 'Add PHP Cron Event', 'wp-crontrol' );
782
+ } else {
783
+ $heading = __( 'Add Cron Event', 'wp-crontrol' );
784
+ }
785
  }
 
786
 
787
+ printf(
788
+ '<h1>%s</h1>',
789
+ esc_html( $heading )
790
+ );
791
+ printf(
792
+ '<p>%s</p>',
793
+ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
794
+ $helper_text
795
+ );
796
+ ?>
797
+ <form method="post" action="<?php echo esc_url( admin_url( 'tools.php?page=crontrol_admin_manage_page' ) ); ?>">
798
+ <?php
799
+ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
800
+ echo $other_fields;
801
+ ?>
802
+ <table class="form-table"><tbody>
803
+ <?php if ( $is_php ) : ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
804
  <tr>
805
+ <th valign="top" scope="row"><label for="hookcode"><?php esc_html_e( 'PHP Code', 'wp-crontrol' ); ?></label></th>
806
  <td>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
807
  <p class="description">
808
  <?php
809
  printf(
810
+ /* translators: The PHP tag name */
811
+ esc_html__( 'The opening %s tag must not be included.', 'wp-crontrol' ),
812
+ '<code>&lt;?php</code>'
813
  );
814
  ?>
815
  </p>
816
+ <p><textarea class="large-text code" rows="10" cols="50" id="hookcode" name="hookcode"><?php echo esc_textarea( $existing['args']['code'] ); ?></textarea></p>
817
+ </td>
818
+ </tr>
819
+ <tr>
820
+ <th valign="top" scope="row"><label for="eventname"><?php esc_html_e( 'Event Name (optional)', 'wp-crontrol' ); ?></label></th>
821
+ <td><input type="text" class="regular-text" id="eventname" name="eventname" value="<?php echo esc_attr( $existing['args']['name'] ); ?>"/></td>
822
+ </tr>
823
+ <?php else : ?>
824
+ <tr>
825
+ <th valign="top" scope="row"><label for="hookname"><?php esc_html_e( 'Hook Name', 'wp-crontrol' ); ?></label></th>
826
+ <td>
827
+ <input type="text" autocorrect="off" autocapitalize="off" spellcheck="false" class="regular-text" id="hookname" name="hookname" value="<?php echo esc_attr( $existing['hookname'] ); ?>" required />
828
  </td>
829
+ </tr>
830
+ <tr>
831
+ <th valign="top" scope="row"><label for="args"><?php esc_html_e( 'Arguments (optional)', 'wp-crontrol' ); ?></label></th>
832
  <td>
833
+ <input type="text" autocorrect="off" autocapitalize="off" spellcheck="false" class="regular-text" id="args" name="args" value="<?php echo esc_attr( $display_args ); ?>"/>
834
  <p class="description">
835
  <?php
836
+ printf(
837
+ /* translators: 1, 2, and 3: Example values for an input field. */
838
+ esc_html__( 'Use a JSON encoded array, e.g. %1$s, %2$s, or %3$s', 'wp-crontrol' ),
839
+ '<code>[25]</code>',
840
+ '<code>["asdf"]</code>',
841
+ '<code>["i","want",25,"cakes"]</code>'
842
+ );
843
  ?>
844
  </p>
845
  </td>
846
  </tr>
847
+ <?php endif; ?>
848
+ <tr>
849
+ <th valign="top" scope="row"><label for="next_run_date_local"><?php esc_html_e( 'Next Run', 'wp-crontrol' ); ?></label></th>
850
+ <td>
851
+ <ul>
852
+ <li>
853
+ <label>
854
+ <input type="radio" name="next_run_date_local" value="now()" checked>
855
+ <?php esc_html_e( 'Now', 'wp-crontrol' ); ?>
856
+ </label>
857
+ </li>
858
+ <li>
859
+ <label>
860
+ <input type="radio" name="next_run_date_local" value="+1 day">
861
+ <?php esc_html_e( 'Tomorrow', 'wp-crontrol' ); ?>
862
+ </label>
863
+ </li>
864
+ <li>
865
+ <label>
866
+ <input type="radio" name="next_run_date_local" value="custom" id="next_run_date_local_custom" <?php checked( $editing ); ?>>
867
+ <?php
868
+ printf(
869
+ /* translators: %s: An input field for specifying a date and time */
870
+ esc_html__( 'At: %s', 'wp-crontrol' ),
871
+ sprintf(
872
+ '<br><input type="text" autocorrect="off" autocapitalize="off" spellcheck="false" name="next_run_date_local_custom" value="%s" class="regular-text" onfocus="jQuery(\'#next_run_date_local_custom\').prop(\'checked\',true);" />',
873
+ esc_attr( $next_run_date_local )
874
+ )
875
+ );
876
+ ?>
877
+ </label>
878
+ </li>
879
+ </ul>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
880
 
881
+ <p class="description">
882
+ <?php
883
+ printf(
884
+ /* translators: 1: Date/time format for an input field, 2: PHP function name. */
885
+ esc_html__( 'Format: %1$s or anything accepted by %2$s', 'wp-crontrol' ),
886
+ '<code>YYYY-MM-DD HH:MM:SS</code>',
887
+ '<a href="https://www.php.net/manual/function.strtotime.php"><code>strtotime()</code></a>'
888
+ );
889
+ ?>
890
+ </p>
891
+ <p class="description">
892
+ <?php
893
+ printf(
894
+ /* translators: %s Timezone name. */
895
+ esc_html__( 'Timezone: %s', 'wp-crontrol' ),
896
+ esc_html( get_timezone_name() )
897
+ );
898
+ ?>
899
+ </p>
900
+ </td>
901
+ </tr><tr>
902
+ <th valign="top" scope="row"><label for="schedule"><?php esc_html_e( 'Recurrence', 'wp-crontrol' ); ?></label></th>
903
+ <td>
904
+ <?php Schedule\dropdown( $existing['schedule'] ); ?>
905
+ </td>
906
+ </tr>
907
+ </tbody></table>
908
+ <p class="submit"><input type="submit" class="button button-primary" value="<?php echo esc_attr( $button ); ?>" name="<?php echo esc_attr( $action ); ?>"/></p>
909
+ </form>
910
+ <?php } else { ?>
911
+ <div class="error inline">
912
+ <p><?php esc_html_e( 'You cannot add, edit, or delete PHP cron events because your user account does not have the ability to edit files.', 'wp-crontrol' ); ?></p>
913
+ </div>
914
+ <?php } ?>
915
+ </div>
916
+ <?php
917
+ }
918
 
919
+ /**
920
+ * Displays the manage page for the plugin.
921
+ */
922
+ function admin_manage_page() {
923
+ $messages = array(
924
+ '1' => array(
925
  /* translators: 1: The name of the cron event. */
926
+ __( 'Scheduled the cron event %s to run now.', 'wp-crontrol' ),
927
+ 'success',
928
+ true,
929
+ ),
930
+ '2' => array(
931
  /* translators: 1: The name of the cron event. */
932
+ __( 'Deleted all %s cron events.', 'wp-crontrol' ),
933
+ 'success',
934
+ false,
935
+ ),
936
+ '3' => array(
937
  /* translators: 1: The name of the cron event. */
938
+ __( 'There are no %s cron events to delete.', 'wp-crontrol' ),
939
+ 'info',
940
+ false,
941
+ ),
942
+ '4' => array(
943
  /* translators: 1: The name of the cron event. */
944
+ __( 'Saved the cron event %s.', 'wp-crontrol' ),
945
+ 'success',
946
+ false,
947
+ ),
948
+ '5' => array(
949
  /* translators: 1: The name of the cron event. */
950
+ __( 'Created the cron event %s.', 'wp-crontrol' ),
951
+ 'success',
952
+ false,
953
+ ),
954
+ '6' => array(
955
  /* translators: 1: The name of the cron event. */
956
+ __( 'Deleted the cron event %s.', 'wp-crontrol' ),
957
+ 'success',
958
+ false,
959
+ ),
960
+ '7' => array(
961
+ /* translators: 1: The name of the cron event. */
962
+ __( 'Failed to the delete the cron event %s.', 'wp-crontrol' ),
963
+ 'error',
964
+ false,
965
+ ),
966
+ '8' => array(
967
+ /* translators: 1: The name of the cron event. */
968
+ __( 'Failed to the execute the cron event %s.', 'wp-crontrol' ),
969
+ 'error',
970
+ false,
971
+ ),
972
+ '9' => array(
973
+ __( 'Deleted the selected cron events.', 'wp-crontrol' ),
974
+ 'success',
975
+ false,
976
+ ),
977
+ '10' => array(
978
+ /* translators: 1: The name of the cron event. */
979
+ __( 'Failed to save the cron event %s.', 'wp-crontrol' ),
980
+ 'error',
981
+ false,
982
+ ),
983
+ );
984
+
985
+ if ( isset( $_GET['crontrol_name'] ) && isset( $_GET['crontrol_message'] ) && isset( $messages[ $_GET['crontrol_message'] ] ) ) {
986
+ $hook = wp_unslash( $_GET['crontrol_name'] );
987
+ $message = wp_unslash( $_GET['crontrol_message'] );
988
+ $link = '';
989
+
990
+ printf(
991
+ '<div id="crontrol-message" class="notice notice-%1$s is-dismissible"><p>%2$s%3$s</p></div>',
992
+ esc_attr( $messages[ $message ][1] ),
993
+ sprintf(
994
+ esc_html( $messages[ $message ][0] ),
995
+ '<strong>' . esc_html( $hook ) . '</strong>'
996
+ ),
997
+ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
998
+ $link
999
  );
1000
+ }
 
 
 
1001
 
1002
+ require_once __DIR__ . '/src/event-list-table.php';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1003
 
1004
+ $tabs = get_tab_states();
1005
+ $table = new Event_List_Table();
1006
 
1007
+ $table->prepare_items();
1008
+
1009
+ switch ( true ) {
1010
+ case $tabs['events']:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1011
  ?>
1012
+ <div class="wrap">
1013
+ <h1><?php esc_html_e( 'Cron Events', 'wp-crontrol' ); ?></h1>
1014
+
1015
+ <?php $table->views(); ?>
1016
+
1017
+ <form id="posts-filter" method="get" action="tools.php">
1018
+ <input type="hidden" name="page" value="crontrol_admin_manage_page" />
1019
+ <?php $table->search_box( __( 'Search Hook Names', 'wp-crontrol' ), 'cron-event' ); ?>
1020
+ </form>
1021
+
1022
+ <form method="post" action="tools.php?page=crontrol_admin_manage_page">
1023
+ <div class="table-responsive">
1024
+ <?php $table->display(); ?>
1025
+ </div>
1026
+ </form>
1027
+
1028
+ <p>
1029
+ <?php
1030
+ echo esc_html( sprintf(
1031
+ /* translators: 1: Date and time, 2: Timezone */
1032
+ __( 'Site time: %1$s (%2$s)', 'wp-crontrol' ),
1033
+ date_i18n( 'Y-m-d H:i:s' ),
1034
+ get_timezone_name()
1035
+ ) );
1036
+ ?>
1037
+ </p>
1038
+ </div>
1039
  <?php
 
 
 
 
 
 
 
 
 
 
 
 
1040
 
1041
+ break;
 
 
 
1042
 
1043
+ case $tabs['add-event']:
1044
+ show_cron_form( $table->items, false );
1045
+ break;
 
 
 
1046
 
1047
+ case $tabs['add-php-event']:
1048
+ show_cron_form( $table->items, false, true );
1049
+ break;
1050
 
1051
+ case $tabs['edit-event']:
1052
+ show_cron_form( $table->items, true );
1053
+ break;
1054
 
1055
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1056
 
1057
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1058
 
1059
+ /**
1060
+ * Get the states of the various cron-related tabs.
1061
+ *
1062
+ * @return bool[] Array of states keyed by tab name.
1063
+ */
1064
+ function get_tab_states() {
1065
+ return array(
1066
+ 'events' => ( ! empty( $_GET['page'] ) && 'crontrol_admin_manage_page' === $_GET['page'] && empty( $_GET['action'] ) ),
1067
+ 'schedules' => ( ! empty( $_GET['page'] ) && 'crontrol_admin_options_page' === $_GET['page'] ),
1068
+ 'add-event' => ( ! empty( $_GET['action'] ) && 'new-cron' === $_GET['action'] ),
1069
+ 'add-php-event' => ( ! empty( $_GET['action'] ) && 'new-php-cron' === $_GET['action'] ),
1070
+ 'edit-event' => ( ! empty( $_GET['action'] ) && 'edit-cron' === $_GET['action'] ),
1071
+ );
1072
+ }
1073
 
1074
+ /**
1075
+ * Output the cron-related tabs if we're on a cron-related admin screen.
1076
+ */
1077
+ function do_tabs() {
1078
+ $tab = get_tab_states();
1079
 
1080
+ if ( ! array_filter( $tab ) ) {
1081
+ return;
1082
+ }
 
 
 
 
 
 
 
 
1083
 
1084
+ $links = array(
1085
+ 'events' => array(
1086
+ 'tools.php?page=crontrol_admin_manage_page',
1087
+ __( 'Cron Events', 'wp-crontrol' ),
1088
+ ),
1089
+ 'schedules' => array(
1090
+ 'options-general.php?page=crontrol_admin_options_page',
1091
+ __( 'Cron Schedules', 'wp-crontrol' ),
1092
+ ),
1093
+ 'add-event' => array(
1094
+ 'tools.php?page=crontrol_admin_manage_page&action=new-cron',
1095
+ __( 'Add Cron Event', 'wp-crontrol' ),
1096
+ ),
1097
+ 'add-php-event' => array(
1098
+ 'tools.php?page=crontrol_admin_manage_page&action=new-php-cron',
1099
+ __( 'Add PHP Cron Event', 'wp-crontrol' ),
1100
+ ),
1101
+ );
1102
+
1103
+ ?>
1104
+ <div id="crontrol-header">
1105
+ <nav class="nav-tab-wrapper">
1106
+ <?php
1107
+ foreach ( $links as $id => $link ) {
1108
+ if ( $tab[ $id ] ) {
1109
+ printf(
1110
+ '<a href="%s" class="nav-tab nav-tab-active">%s</a>',
1111
+ esc_url( $link[0] ),
1112
+ esc_html( $link[1] )
1113
+ );
1114
+ } else {
1115
+ printf(
1116
+ '<a href="%s" class="nav-tab">%s</a>',
1117
+ esc_url( $link[0] ),
1118
+ esc_html( $link[1] )
1119
  );
 
 
 
1120
  }
1121
+ }
1122
 
1123
+ if ( $tab['edit-event'] ) {
1124
+ printf(
1125
+ '<span class="nav-tab nav-tab-active">%s</span>',
1126
+ esc_html__( 'Edit Cron Event', 'wp-crontrol' )
1127
+ );
1128
  }
 
 
 
 
 
 
 
 
 
 
 
1129
  ?>
1130
+ </nav>
1131
  <?php
1132
+ show_cron_status();
 
 
 
 
 
1133
  ?>
1134
+ </div>
1135
+ <?php
1136
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1137
 
1138
+ /**
1139
+ * Returns an array of the callback functions that are attached to the given hook name.
1140
+ *
1141
+ * @param string $name The hook name.
1142
+ * @return array[] Array of callbacks attached to the hook.
1143
+ */
1144
+ function get_hook_callbacks( $name ) {
1145
+ global $wp_filter;
1146
 
1147
+ $actions = array();
1148
 
1149
+ if ( isset( $wp_filter[ $name ] ) ) {
1150
+ // See http://core.trac.wordpress.org/ticket/17817.
1151
+ $action = $wp_filter[ $name ];
1152
 
1153
+ foreach ( $action as $priority => $callbacks ) {
1154
+ foreach ( $callbacks as $callback ) {
1155
+ $callback = populate_callback( $callback );
1156
 
1157
+ $actions[] = array(
1158
+ 'priority' => $priority,
1159
+ 'callback' => $callback,
1160
+ );
 
1161
  }
1162
  }
 
 
1163
  }
1164
 
1165
+ return $actions;
1166
+ }
 
 
 
 
 
 
 
 
 
1167
 
1168
+ /**
1169
+ * Populates the details of the given callback function.
1170
+ *
1171
+ * @param array $callback A callback entry.
1172
+ * @return array The updated callback entry.
1173
+ */
1174
+ function populate_callback( array $callback ) {
1175
+ // If Query Monitor is installed, use its rich callback analysis.
1176
+ if ( method_exists( '\QM_Util', 'populate_callback' ) ) {
1177
+ return \QM_Util::populate_callback( $callback );
1178
+ }
1179
 
1180
+ if ( is_string( $callback['function'] ) && ( false !== strpos( $callback['function'], '::' ) ) ) {
1181
+ $callback['function'] = explode( '::', $callback['function'] );
1182
+ }
 
 
 
 
 
1183
 
1184
+ if ( is_array( $callback['function'] ) ) {
1185
+ if ( is_object( $callback['function'][0] ) ) {
1186
+ $class = get_class( $callback['function'][0] );
1187
+ $access = '->';
 
 
 
 
1188
  } else {
1189
+ $class = $callback['function'][0];
1190
+ $access = '::';
1191
  }
1192
 
1193
+ $callback['name'] = $class . $access . $callback['function'][1] . '()';
1194
+ } elseif ( is_object( $callback['function'] ) ) {
1195
+ if ( is_a( $callback['function'], 'Closure' ) ) {
1196
+ $callback['name'] = 'Closure';
1197
+ } else {
1198
+ $class = get_class( $callback['function'] );
1199
 
1200
+ $callback['name'] = $class . '->__invoke()';
1201
+ }
1202
+ } else {
1203
+ $callback['name'] = $callback['function'] . '()';
1204
  }
1205
 
1206
+ return $callback;
1207
+ }
1208
+
1209
+ /**
1210
+ * Returns a user-friendly representation of the callback function.
1211
+ *
1212
+ * @param array $callback The callback entry.
1213
+ * @return string The displayable version of the callback name.
1214
+ */
1215
+ function output_callback( array $callback ) {
1216
+ $qm = WP_PLUGIN_DIR . '/query-monitor/query-monitor.php';
1217
+ $html = plugin_dir_path( $qm ) . 'output/Html.php';
1218
+
1219
+ // If Query Monitor is installed, use its rich callback output.
1220
+ if ( class_exists( '\QueryMonitor' ) && file_exists( $html ) ) {
1221
+ require_once $html;
1222
+
1223
+ if ( class_exists( '\QM_Output_Html' ) ) {
1224
+ if ( ! empty( $callback['callback']['error'] ) ) {
1225
+ $return = '<code>' . $callback['callback']['name'] . '</code>';
1226
+ $return .= '<br><span style="color:#c00"><span class="dashicons dashicons-warning" aria-hidden="true"></span> ';
1227
+ $return .= esc_html( $callback['callback']['error']->get_error_message() );
1228
+ $return .= '</span>';
1229
+ return $return;
1230
  }
 
1231
 
1232
+ return \QM_Output_Html::output_filename(
1233
+ $callback['callback']['name'],
1234
+ $callback['callback']['file'],
1235
+ $callback['callback']['line']
1236
+ );
1237
+ }
1238
  }
1239
 
1240
+ return '<code>' . $callback['callback']['name'] . '</code>';
1241
+ }
1242
+
1243
+ /**
1244
+ * Pretty-prints the difference in two times.
1245
+ *
1246
+ * @param int $older_date Unix timestamp.
1247
+ * @param int $newer_date Unix timestamp.
1248
+ * @return string The pretty time_since value
1249
+ * @link http://binarybonsai.com/code/timesince.txt
1250
+ */
1251
+ function time_since( $older_date, $newer_date ) {
1252
+ return interval( $newer_date - $older_date );
1253
+ }
1254
+
1255
+ /**
1256
+ * Converts a period of time in seconds into a human-readable format representing the interval.
1257
+ *
1258
+ * Example:
1259
+ *
1260
+ * echo \Crontrol\interval( 90 );
1261
+ * // 1 minute 30 seconds
1262
+ *
1263
+ * @param int $since A period of time in seconds.
1264
+ * @return string An interval represented as a string.
1265
+ */
1266
+ function interval( $since ) {
1267
+ // Array of time period chunks.
1268
+ $chunks = array(
1269
+ /* translators: 1: The number of years in an interval of time. */
1270
+ array( 60 * 60 * 24 * 365, _n_noop( '%s year', '%s years', 'wp-crontrol' ) ),
1271
+ /* translators: 1: The number of months in an interval of time. */
1272
+ array( 60 * 60 * 24 * 30, _n_noop( '%s month', '%s months', 'wp-crontrol' ) ),
1273
+ /* translators: 1: The number of weeks in an interval of time. */
1274
+ array( 60 * 60 * 24 * 7, _n_noop( '%s week', '%s weeks', 'wp-crontrol' ) ),
1275
+ /* translators: 1: The number of days in an interval of time. */
1276
+ array( 60 * 60 * 24, _n_noop( '%s day', '%s days', 'wp-crontrol' ) ),
1277
+ /* translators: 1: The number of hours in an interval of time. */
1278
+ array( 60 * 60, _n_noop( '%s hour', '%s hours', 'wp-crontrol' ) ),
1279
+ /* translators: 1: The number of minutes in an interval of time. */
1280
+ array( 60, _n_noop( '%s minute', '%s minutes', 'wp-crontrol' ) ),
1281
+ /* translators: 1: The number of seconds in an interval of time. */
1282
+ array( 1, _n_noop( '%s second', '%s seconds', 'wp-crontrol' ) ),
1283
+ );
1284
+
1285
+ if ( $since <= 0 ) {
1286
+ return __( 'now', 'wp-crontrol' );
1287
  }
1288
 
1289
  /**
1290
+ * We only want to output two chunks of time here, eg:
1291
+ * x years, xx months
1292
+ * x days, xx hours
1293
+ * so there's only two bits of calculation below:
 
 
 
 
 
1294
  */
1295
+ $j = count( $chunks );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1296
 
1297
+ // Step one: the first chunk.
1298
+ for ( $i = 0; $i < $j; $i++ ) {
1299
+ $seconds = $chunks[ $i ][0];
1300
+ $name = $chunks[ $i ][1];
1301
 
1302
+ // Finding the biggest chunk (if the chunk fits, break).
1303
+ $count = floor( $since / $seconds );
1304
+ if ( $count ) {
1305
+ break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1306
  }
1307
+ }
1308
 
1309
+ // Set output var.
1310
+ $output = sprintf( translate_nooped_plural( $name, $count, 'wp-crontrol' ), $count );
1311
+
1312
+ // Step two: the second chunk.
1313
+ if ( $i + 1 < $j ) {
1314
+ $seconds2 = $chunks[ $i + 1 ][0];
1315
+ $name2 = $chunks[ $i + 1 ][1];
1316
+ $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
1317
+ if ( $count2 ) {
1318
+ // Add to output var.
1319
+ $output .= ' ' . sprintf( translate_nooped_plural( $name2, $count2, 'wp-crontrol' ), $count2 );
 
1320
  }
 
 
1321
  }
1322
 
1323
+ return $output;
1324
+ }
 
 
 
 
 
 
 
 
 
 
1325
 
1326
+ /**
1327
+ * Enqueues the editor UI that's used for the PHP cron event code editor.
1328
+ */
1329
+ function enqueue_code_editor() {
1330
+ if ( ! function_exists( 'wp_enqueue_code_editor' ) ) {
1331
+ return;
1332
+ }
1333
+ if ( ! current_user_can( 'edit_files' ) ) {
1334
+ return;
1335
  }
1336
 
1337
+ $settings = wp_enqueue_code_editor( array(
1338
+ 'type' => 'text/x-php',
1339
+ ) );
 
 
 
 
 
 
 
1340
 
1341
+ if ( false === $settings ) {
1342
+ return;
1343
+ }
1344
 
1345
+ wp_add_inline_script( 'code-editor', sprintf(
1346
+ 'jQuery( function( $ ) {
1347
+ if ( $( "#hookcode" ).length ) {
1348
+ wp.codeEditor.initialize( "hookcode", %s );
1349
+ }
1350
+ } );',
1351
+ wp_json_encode( $settings )
1352
+ ) );
1353
+ }
1354
 
1355
+ /**
1356
+ * Registers the stylesheet and JavaScript for the admin areas.
1357
+ *
1358
+ * @param string $hook_suffix The admin screen ID.
1359
+ */
1360
+ function enqueue_assets( $hook_suffix ) {
1361
+ $tab = get_tab_states();
 
 
1362
 
1363
+ if ( ! array_filter( $tab ) ) {
1364
+ return;
 
 
 
 
 
 
 
 
 
 
 
1365
  }
1366
 
1367
+ $ver = filemtime( plugin_dir_path( __FILE__ ) . 'css/wp-crontrol.css' );
1368
+ wp_enqueue_style( 'wp-crontrol', plugin_dir_url( __FILE__ ) . 'css/wp-crontrol.css', array(), $ver );
 
 
1369
 
1370
+ $ver = filemtime( plugin_dir_path( __FILE__ ) . 'js/wp-crontrol.js' );
1371
+ wp_enqueue_script( 'wp-crontrol', plugin_dir_url( __FILE__ ) . 'js/wp-crontrol.js', array(), $ver, true );
1372
+ }
1373
 
1374
+ /**
1375
+ * Filters the list of query arguments which get removed from admin area URLs in WordPress.
1376
+ *
1377
+ * @param string[] $args List of removable query arguments.
1378
+ * @return string[] Updated list of removable query arguments.
1379
+ */
1380
+ function filter_removable_query_args( array $args ) {
1381
+ return array_merge( $args, array(
1382
+ 'crontrol_message',
1383
+ 'crontrol_name',
1384
+ ) );
1385
+ }
1386
+
1387
+ /**
1388
+ * Returns an array of cron event hooks that are persistently added by WordPress core.
1389
+ *
1390
+ * @return string[] Array of hook names.
1391
+ */
1392
+ function get_persistent_core_hooks() {
1393
+ return array(
1394
+ 'delete_expired_transients',
1395
+ 'recovery_mode_clean_expired_keys',
1396
+ 'update_network_counts',
1397
+ 'wp_privacy_delete_old_export_files',
1398
+ 'wp_scheduled_auto_draft_delete',
1399
+ 'wp_scheduled_delete',
1400
+ 'wp_update_plugins',
1401
+ 'wp_update_themes',
1402
+ 'wp_version_check',
1403
+ );
1404
+ }
1405
+
1406
+ /**
1407
+ * Returns an array of all cron event hooks that are added by WordPress core.
1408
+ *
1409
+ * @return string[] Array of hook names.
1410
+ */
1411
+ function get_all_core_hooks() {
1412
+ return array_merge(
1413
+ get_persistent_core_hooks(),
1414
+ array(
1415
+ 'do_pings',
1416
+ 'importer_scheduled_cleanup',
1417
+ 'publish_future_post',
1418
+ 'upgrader_scheduled_cleanup',
1419
+ 'wp_maybe_auto_update',
1420
+ 'wp_split_shared_term_batch',
1421
+ )
1422
+ );
1423
+ }
1424
 
1425
+ /**
1426
+ * Returns an array of cron schedules that are added by WordPress core.
1427
+ *
1428
+ * @return string[] Array of schedule names.
1429
+ */
1430
+ function get_core_schedules() {
1431
+ return array(
1432
+ 'hourly',
1433
+ 'twicedaily',
1434
+ 'daily',
1435
+ );
1436
+ }
1437
+
1438
+ /**
1439
+ * Encodes some input as JSON for output.
1440
+ *
1441
+ * @param mixed $input The input.
1442
+ * @return string The JSON-encoded output.
1443
+ */
1444
+ function json_output( $input ) {
1445
+ $json_options = 0;
1446
 
1447
+ if ( defined( 'JSON_UNESCAPED_SLASHES' ) ) {
1448
+ // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_unescaped_slashesFound
1449
+ $json_options |= JSON_UNESCAPED_SLASHES;
1450
  }
1451
+ if ( defined( 'JSON_PRETTY_PRINT' ) ) {
1452
+ $json_options |= JSON_PRETTY_PRINT;
1453
+ }
1454
+
1455
+ return wp_json_encode( $input, $json_options );
1456
+ }
1457
+
1458
+ /**
1459
+ * Evaluates the code in a PHP cron event using eval.
1460
+ *
1461
+ * Security: A user can only add or edit a PHP cron event if they have the `edit_files` capability. This means if a user
1462
+ * cannot edit files on the site (eg. through the plugin or theme editor) then they cannot edit or add a PHP cron event.
1463
+ *
1464
+ * @param string $code The PHP code to evaluate.
1465
+ */
1466
+ function action_php_cron_event( $code ) {
1467
+ // phpcs:ignore Squiz.PHP.Eval.Discouraged
1468
+ eval( $code );
1469
  }
1470
 
1471
  // Get this show on the road.
1472
+ init_hooks();