Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a new cron event type for fetching a URL #112

Merged
merged 19 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions css/wp-crontrol.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ table.wp-list-table {
width: 100px;
}

.crontrol-edit-event-url .crontrol-event-standard,
.crontrol-edit-event-url .crontrol-event-php,
.crontrol-edit-event-standard .crontrol-event-url,
.crontrol-edit-event-standard .crontrol-event-php,
.crontrol-edit-event-php .crontrol-event-url,
.crontrol-edit-event-php .crontrol-event-standard {
display: none;
}
4 changes: 4 additions & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export default defineConfig({
text: 'PHP cron events',
link: '/docs/php-cron-events/',
},
{
text: 'URL cron events',
link: '/docs/url-cron-events/',
},
{
text: 'What happens if I deactivate WP Crontrol?',
link: '/docs/deactivation/',
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/deactivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ If you deactivate or delete the WP Crontrol plugin, your existing cron events wi

Any cron hooks that you've paused through WP Crontrol will be resumed because the paused functionality is provided by WP Crontrol. If you reactivate WP Crontrol, they will become paused again.

## PHP cron events
## URL cron events and PHP cron events

If you've created a PHP cron event with WP Crontrol, these events will remain in place after you deactivate WP Crontrol but they will _cease to operate_ because these events are processed by WP Crontrol. If you reactivate WP Crontrol, they will resume operating as normal.
If you've created a URL cron event or PHP cron event with WP Crontrol, these events will remain in place after you deactivate WP Crontrol but they will _cease to operate_ because these events are processed by WP Crontrol. If you reactivate WP Crontrol, they will resume operating as normal.

## Custom schedules

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/how-to-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ There are two steps to creating a functioning cron event that executes regularly

### Step one: Adding the hook

In the Tools → Cron Events admin panel, click on "Add New" and enter the details of the hook. You're best off using a hook name 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 first in the Settings → Cron Schedules admin panel.
In the Tools → Cron Events admin panel, click on "Add New Cron Event" and enter the details of the hook. You're best off using a hook name 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 first in the Settings → Cron Schedules admin panel.

### Step two: Writing the function

Expand Down
8 changes: 2 additions & 6 deletions docs/docs/php-cron-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ If you wish to prevent PHP cron events from being added or edited on your site t

## How do I create a new PHP cron event?

In the Tools → Cron Events admin panel, click on "Add New". In the form that appears, select "PHP cron event" and enter the schedule and next run time. In the "PHP Code" area, enter the PHP code that should be run when your cron event is executed. Don't include the PHP opening tag (`<?php`).
In the Tools → Cron Events admin panel, click on "Add New Cron Event". In the form that appears, select the "PHP cron event" option under the "Event Type" list and enter the schedule and next run time. In the "PHP Code" area, enter the PHP code that should be run when your cron event is executed. Don't include the PHP opening tag (`<?php`).

## Can I "lock" PHP cron events so that other users cannot edit them?

Expand All @@ -28,11 +28,7 @@ If you need to edit the event in the future, you can temporarily remove the rele

## How can I create a cron event that requests a URL?

From the Tools → Cron Events → Add New screen, create a PHP cron event that includes PHP that fetches the URL using the WordPress HTTP API. For example:

```php
wp_remote_get( 'http://example.com' );
```
You don't need to use a PHP cron event for this. From the Tools → Cron Events → Add New Cron Event screen, select the "Request a URL" option under the "Event Type" list. Fill out the rest of the details as required and press the "Add Event" button.

## Can the code in PHP cron events be tampered with?

Expand Down
11 changes: 11 additions & 0 deletions docs/docs/url-cron-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# URL cron events

::: tip New
This feature is new in WP Crontrol 1.17
:::

WP Crontrol includes a feature that allows users to create events in the WP-Cron system that request a URL. This is a convenience wrapper around functionality that you would otherwise need to write PHP in order to achieve.

## How do I create a cron event that requests a URL?

From the Tools → Cron Events → Add New Cron Event screen, select the "Request a URL" option under the "Event Type" list. Fill out the rest of the details as required and press the "Add Event" button.
25 changes: 20 additions & 5 deletions js/wp-crontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,39 @@ document.addEventListener( 'DOMContentLoaded', () => {
const customDateElement = document.getElementById( 'crontrol_next_run_date_local_custom_date' );
const customTimeElement = document.getElementById( 'crontrol_next_run_date_local_custom_time' );
const newCronElement = document.querySelector( 'input[value="new_cron"]' );
const newURLCronElement = document.querySelector( 'input[value="new_url_cron"]' );
const newPHPCronElement = document.querySelector( 'input[value="new_php_cron"]' );
const hookCodeElement = document.getElementById( 'crontrol_hookcode' );
const hookNameElement = document.getElementById( 'crontrol_hookname' );
const hookURLElement = document.getElementById( 'crontrol_url' );
const editEventElement = document.querySelector( '.crontrol-edit-event' );

customDateElement && customDateElement.addEventListener( 'change', checkCustom );
customTimeElement && customTimeElement.addEventListener( 'change', checkCustom );

newCronElement && newCronElement.addEventListener( 'click', () => {
editEventElement.classList.remove( 'crontrol-edit-event-url' );
editEventElement.classList.remove( 'crontrol-edit-event-php' );
editEventElement.classList.add( 'crontrol-edit-event-standard' );
hookNameElement.setAttribute( 'required', true );
hookURLElement.removeAttribute( 'required' );
} );

newURLCronElement && newURLCronElement.addEventListener( 'click', () => {
editEventElement.classList.remove( 'crontrol-edit-event-standard' );
editEventElement.classList.remove( 'crontrol-edit-event-php' );
editEventElement.classList.add( 'crontrol-edit-event-url' );
hookURLElement.setAttribute( 'required', true );
hookNameElement.removeAttribute( 'required' );
} );

if ( newPHPCronElement ) {
newCronElement.addEventListener( 'click', () => {
editEventElement.classList.remove( 'crontrol-edit-event-php' );
editEventElement.classList.add( 'crontrol-edit-event-standard' );
hookNameElement.setAttribute( 'required', true );
} );
newPHPCronElement.addEventListener( 'click', () => {
editEventElement.classList.remove( 'crontrol-edit-event-standard' );
editEventElement.classList.remove( 'crontrol-edit-event-url' );
editEventElement.classList.add( 'crontrol-edit-event-php' );
hookNameElement.removeAttribute( 'required' );
hookURLElement.removeAttribute( 'required' );
if ( ! hookCodeElement.classList.contains( 'crontrol-editor-initialized' ) ) {
wp.codeEditor.initialize( 'crontrol_hookcode', window.wpCrontrol.codeEditor );
}
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

<!-- Exclude short description sniff so short `@var` notation can be used -->
<exclude name="Generic.Commenting.DocComment.MissingShort"/>

<!-- Use Yoda condition checks, you may or may not. -->
<exclude name="WordPress.PHP.YodaConditions.NotYoda"/>
</rule>

<!-- Ignore some rules for the tests -->
Expand Down
12 changes: 3 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@ You can change the time and recurrence of a cron event by clicking the "Edit" li

### How can I create a cron event that requests a URL?

From the Tools → Cron Events → Add New screen, create a PHP cron event that includes PHP that fetches the URL using the WordPress HTTP API. For example:

~~~php
wp_remote_get( 'http://example.com' );
~~~

[You can read all about the features and security of PHP cron events on the WP Crontrol website](https://wp-crontrol.com/docs/php-cron-events/).
From the Tools → Cron Events → Add New Cron Event screen, select the "Request a URL" option under the "Event Type" list. Fill out the rest of the details as required and press the "Add Event" button.

### Why do changes that I make to some cron events not get saved?

Expand Down Expand Up @@ -127,7 +121,7 @@ There are two steps to getting a functioning cron event that executes regularly.

*Step One: Adding the hook*

In the Tools → Cron Events admin panel, click on "Add New" and enter the details of the hook. You're best off using a hook name 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.
In the Tools → Cron Events admin panel, click on "Add New Cron Event" and enter the details of the hook. You're best off using a hook name 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.

*Step Two: Writing the function*

Expand All @@ -147,7 +141,7 @@ function my_function() {

### How do I create a new PHP cron event?

In the Tools → Cron Events admin panel, click on "Add New". In the form that appears, select "PHP Cron Event" and enter the schedule and next run time. 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 "PHP 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`).
In the Tools → Cron Events admin panel, click on "Add New Cron Event". In the form that appears, select "PHP cron event" and enter the schedule and next run time. 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 "PHP 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`).

Creating, editing, and running PHP cron events is subject to restrictive security permissions. [You can read all about the features and security of PHP cron events on the WP Crontrol website](https://wp-crontrol.com/docs/php-cron-events/).

Expand Down
Loading