Skip to content

Instantly share code, notes, and snippets.

@JPry
Last active January 26, 2022 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPry/357ee26a46e3d8e2b68d1690f5bbbf75 to your computer and use it in GitHub Desktop.
Save JPry/357ee26a46e3d8e2b68d1690f5bbbf75 to your computer and use it in GitHub Desktop.
This plugin drops into the /wp-content/mu-plugins/ folder to add a page that creates a scheduled action for Action Scheduler.
<?php
/**
* Plugin Name: Add "Create Scheduled Action" page
* Plugin URI: https://gist.github.com/JPry/357ee26a46e3d8e2b68d1690f5bbbf75
* Description: Add a page to the Tools menu that allows for the manual creation of scheduled actions with Action
* Scheduler. Version: 1.0.0
* Author: Jeremy Pry
* Author URI: https://jeremypry.com/
* License: MIT
*/
use ActionScheduler as ActS;
// Prevent direct access to this file
if ( ! defined( 'ABSPATH' ) ) {
die( "You can't do anything by accessing this file directly." );
}
// Set up a custom page that allows for createing scheduled actions
add_action(
'admin_menu',
function() {
// Ensure we actually have Action Scheduler loaded.
if ( ! class_exists( ActS::class, false ) ) {
return;
}
$messages = [];
$hook = add_submenu_page(
'tools.php',
'Create Scheduled Action',
'Create Scheduled Action',
'manage_options',
'jpry-create-scheduled-actions',
function() use ( &$messages ) {
$hook = sanitize_text_field( $_REQUEST['hook'] ?? '' );
$type = sanitize_text_field( $_REQUEST['type'] ?? '' );
$group = sanitize_text_field( $_REQUEST['group'] ?? '' );
$when = sanitize_text_field( $_REQUEST['when'] ?? '' );
?>
<h1>Create a Scheduled Action</h1>
<?php
foreach ( $messages as $message ) {
printf(
'<div class="notice notice-%s"><p>%s</p></div>',
esc_attr( trim( $message['type'] ) ),
esc_html( $message['message'] )
);
}
?>
<div class="wrap">
<form action="tools.php?page=jpry-create-scheduled-actions" method="post">
<?php wp_nonce_field( 'jpry-create-scheduled-action' ); ?>
<table class="form-table">
<tr>
<td><label for="jpry-hook">Hook name:</label></td>
<td>
<input type="text"
name="hook"
id="jpry-hook"
class="regular-text"
value="<?php echo esc_attr( $hook ); ?>"
>
</td>
</tr>
<tr>
<td><label for="jpry-group">Group:</label></td>
<td>
<input type="text"
name="group"
id="jpry-group"
class="regular-text"
value="<?php echo esc_attr( $group ); ?>"
>
</td>
</tr>
<tr>
<td><label for="jpry-type">Type:</label></td>
<td>
<select name="type" id="jpry-type">
<option value="async" <?php selected( $type, 'async' ); ?>>Async</option>
<option value="single" <?php selected( $type, 'single' ); ?>>Single</option>
<!--<option value="recurring" <?php selected(
$type,
'recurring'
); ?>>Recurring</option>-->
</select>
</td>
</tr>
<tr>
<td><label for="jpry-when">When (for single):</label></td>
<td>
<input type="datetime-local"
name="when"
id="jpry-when"
value="<?php echo esc_attr( $when ); ?>"
>
</td>
</tr>
</table>
<input type="submit" value="Submit" class="button">
</form>
</div>
<?php
}
);
add_action(
"load-{$hook}",
function() use ( &$messages ) {
if ( ! array_key_exists( '_wpnonce', $_REQUEST ) ) {
$messages[] = [
'message' => 'No Nonce detected.',
'type' => 'info',
];
return;
}
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'jpry-create-scheduled-action' ) ) {
$messages[] = [
'message' => 'Invalid Nonce!',
'type' => 'error',
];
return;
}
if ( ! isset( $_REQUEST['hook'], $_REQUEST['type'] ) ) {
$messages[] = [
'message' => 'Hook name and type are both required.',
'type' => 'error',
];
return;
}
$hook = sanitize_text_field( $_REQUEST['hook'] );
$type = sanitize_text_field( $_REQUEST['type'] );
$group = sanitize_text_field( $_REQUEST['group'] );
$when = sanitize_text_field( $_REQUEST['when'] );
if ( empty( $when ) ) {
$time = new DateTime( 'now', wp_timezone() );
} else {
$time = date_create_from_format( 'Y-m-d*G:i', $when, wp_timezone() );
}
switch ( $type ) {
case 'async':
ActS::factory()->async( $hook, [], $group );
$messages[] = [
'message' => sprintf( 'Added async hook "%s" with group "%"', $hook, $group ),
'type' => 'info',
];
break;
case 'single':
if ( ! $time instanceof DateTime ) {
$messages[] = [
'message' => 'Error converting date/time to timestamp.',
'type' => 'error',
];
return;
}
ActS::factory()->single( $hook, [], $time->getTimestamp(), $group );
$messages[] = [
'message' => sprintf( 'Added single hook "%s" with group "%"', $hook, $group ),
'type' => 'info',
];
break;
}
}
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment