From 8404e6b2c23e550a55ed8298df2e795644308ddf Mon Sep 17 00:00:00 2001 From: defunctl Date: Mon, 13 Jan 2020 11:32:45 -0700 Subject: [PATCH 1/6] Ignore IDE files --- .gitignore | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.gitignore b/.gitignore index dfd6caa4..78cfd071 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,21 @@ +# OS generated files # +###################### + +.DS_Store* +ehthumbs.db +._* +*~ +.svn +.cvs +*.bak +*.swp +Thumbs.db +.DS_Store +.idea +pimple.json +.phpstorm.meta.php + +# Project specific # +####################### /vendor composer.lock \ No newline at end of file From fab5467b897f8ee961fa0b2bf134acfa71db0795 Mon Sep 17 00:00:00 2001 From: defunctl Date: Mon, 13 Jan 2020 11:34:13 -0700 Subject: [PATCH 2/6] Add composer.json --- src/Cron/composer.json | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/Cron/composer.json diff --git a/src/Cron/composer.json b/src/Cron/composer.json new file mode 100644 index 00000000..af2310e7 --- /dev/null +++ b/src/Cron/composer.json @@ -0,0 +1,23 @@ +{ + "name": "moderntribe/square1-cron", + "type": "library", + "description": "Abstract cron jobs and schedules for square one", + "license": "GPL-2.0-only", + "config": { + "vendor-dir": "vendor", + "preferred-install": "dist" + }, + "require": { + "php": "^7.0" + }, + "autoload": { + "psr-4": { + "Tribe\\Libs\\Cron\\": "" + } + }, + "extra": { + "branch-alias": { + "dev-master": "2.1-dev" + } + } +} From 42349e1e9b1a541bd1d22bf46bb6d089b0f911fa Mon Sep 17 00:00:00 2001 From: defunctl Date: Mon, 13 Jan 2020 14:05:38 -0700 Subject: [PATCH 3/6] Add abstract cron, schedule, collection and README with examples --- src/Cron/Abstract_Cron.php | 83 +++++++++++++++ src/Cron/Abstract_Schedule.php | 65 ++++++++++++ src/Cron/README.md | 177 +++++++++++++++++++++++++++++++ src/Cron/Schedule_Collection.php | 44 ++++++++ 4 files changed, 369 insertions(+) create mode 100644 src/Cron/Abstract_Cron.php create mode 100644 src/Cron/Abstract_Schedule.php create mode 100644 src/Cron/README.md create mode 100644 src/Cron/Schedule_Collection.php diff --git a/src/Cron/Abstract_Cron.php b/src/Cron/Abstract_Cron.php new file mode 100644 index 00000000..5f956edf --- /dev/null +++ b/src/Cron/Abstract_Cron.php @@ -0,0 +1,83 @@ +hook = $hook; + $this->recurrence = $recurrence; + $this->args = $args; + } + + /** + * Registers the cron job + * + * @param Abstract_Cron $instance + */ + public function register( Abstract_Cron $instance ) { + add_action( $this->hook, [ $instance, 'run' ], 10, 1 ); + } + + /** + * Enables the cron job + */ + public function enable() { + if ( ! wp_next_scheduled( $this->hook ) ) { + wp_schedule_event( time(), $this->recurrence, $this->hook, $this->args ); + } + } + + /** + * Disables the cron job + */ + public function disable() { + wp_clear_scheduled_hook( $this->hook ); + } + + /** + * Executes when the cron job runs + * + * @param array $args + * + * @return mixed + */ + abstract public function run( array $args = [] ); +} diff --git a/src/Cron/Abstract_Schedule.php b/src/Cron/Abstract_Schedule.php new file mode 100644 index 00000000..e17d21b0 --- /dev/null +++ b/src/Cron/Abstract_Schedule.php @@ -0,0 +1,65 @@ +display = $display; + } + + /** + * Returns the schedule formatted for the 'cron_schedules' filter. + * + * @return array + */ + public function get(): array { + $schedules[ static::KEY ] = [ + 'interval' => static::INTERVAL, + 'display' => esc_html__( $this->display, 'tribe' ), + ]; + + return (array) $schedules; + } +} \ No newline at end of file diff --git a/src/Cron/README.md b/src/Cron/README.md new file mode 100644 index 00000000..81aa9bcf --- /dev/null +++ b/src/Cron/README.md @@ -0,0 +1,177 @@ +# moderntribe/square1-cron + +This package provides some abstract classes to be used to create both custom schedules and cron jobs in WordPress. + +### Example Custom Schedule + +##### src/Cron/Thirty_Minutes.php + +``` +schedules( $container ); + $this->cron_jobs( $container ); + } + + private function schedules( Container $container ) { + $container[ 'cron.thirty_minutes' ] = function() { + return new Thirty_Minutes( 'Thirty Minutes' ); + }; + + $container[ 'cron.schedule_collection' ] = function( Container $container ) { + return new Schedule_Collection( $container[ 'cron.thirty_minutes' ] ); + }; + + foreach( $container[ 'cron.schedule_collection' ] as $schedule ) { + add_filter( 'cron_schedules', function( $schedules ) use ( $schedule ) { + return array_merge( $schedules, $schedule->get() ); + }, 10, 1 ); + } + } + + private function cron_jobs( Container $container ) { + $container[ 'cron.my_cron' ] = function() { + return new My_Cron( 'my_cron', Thirty_Minutes::KEY ); + }; + + $container[ 'cron.cron_collection' ] = function( Container $container ) { + return [ + $container[ 'cron.my_cron' ], + ]; + }; + + /** @var Abstract_Cron $cron */ + foreach( $container[ 'cron.cron_collection' ] as $cron ) { + $cron->register( $cron ); + } + } +} +``` + +Don't forget to register the above provider in `src/Core.php` + +### Activating Cron Jobs + +Cron jobs need to be enabled, but only once. One solution is to use [square1-schema](https://github.com/moderntribe/square1-schema) +to do this. + +##### src/Schema/Cron_Schema.php + +``` + [ $this, 'enable_cron_jobs' ] + ]; + } + + /** + * Enable Cron Jobs + */ + public function enable_cron_jobs() { + $container = tribe_project()->container(); + + /** @var Abstract_Cron $cron */ + foreach( $container[ 'cron.cron_collection' ] as $cron ) { + $cron->enable(); + } + } +} +``` + +##### src/Service_Providers/Schema_Provider.php + +``` +update_required() ) { + $container[ 'schema.cron' ]->do_updates(); + } + } + }, 10, 0 ); + } + +} +``` \ No newline at end of file diff --git a/src/Cron/Schedule_Collection.php b/src/Cron/Schedule_Collection.php new file mode 100644 index 00000000..dfa62427 --- /dev/null +++ b/src/Cron/Schedule_Collection.php @@ -0,0 +1,44 @@ +schedules[ $schedule::KEY ] = $schedule; + } + } + + /** + * Get all the schedules in the collection + * + * @return array + */ + public function schedules(): array { + return $this->schedules; + } + + /** + * Get a specific schedule by key + * + * @param string $key + * + * @return Abstract_Schedule|null + */ + public function get( string $key ) { + if ( isset( $this->schedules[ $key ] ) ) { + return $this->schedules[ $key ]; + } + + return null; + } +} \ No newline at end of file From 8f24292554fc6e5aa43ad35311571620bbb7b6f6 Mon Sep 17 00:00:00 2001 From: defunctl Date: Mon, 13 Jan 2020 14:10:19 -0700 Subject: [PATCH 4/6] Fix spacing --- src/Cron/README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Cron/README.md b/src/Cron/README.md index 81aa9bcf..0232a53f 100644 --- a/src/Cron/README.md +++ b/src/Cron/README.md @@ -32,13 +32,13 @@ namespace Tribe\Project\Cron; class My_Cron extends Abstract_Cron { - /** - * Executes when the cron job runs - * - * @param array $args - * - * @return mixed - */ + /** + * Executes when the cron job runs + * + * @param array $args + * + * @return mixed + */ public function run( array $args = [] ) { update_option( 'my_cron_option', 'ran', false ); } From 686c7acf853ee43e9d2635f2d51e528a853ccffd Mon Sep 17 00:00:00 2001 From: defunctl Date: Mon, 13 Jan 2020 14:14:08 -0700 Subject: [PATCH 5/6] Fix namespaces --- src/Cron/README.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Cron/README.md b/src/Cron/README.md index 0232a53f..adbf8b42 100644 --- a/src/Cron/README.md +++ b/src/Cron/README.md @@ -11,6 +11,8 @@ This package provides some abstract classes to be used to create both custom sch namespace Tribe\Project\Cron; +use Tribe\Libs\Cron\Abstract_Schedule; + class Thirty_Minutes extends Abstract_Schedule { /** @var string array friendly key name **/ @@ -30,15 +32,17 @@ class Thirty_Minutes extends Abstract_Schedule { namespace Tribe\Project\Cron; +use Tribe\Libs\Cron\Abstract_Cron; + class My_Cron extends Abstract_Cron { /** - * Executes when the cron job runs - * - * @param array $args - * - * @return mixed - */ + * Executes when the cron job runs + * + * @param array $args + * + * @return mixed + */ public function run( array $args = [] ) { update_option( 'my_cron_option', 'ran', false ); } @@ -61,7 +65,7 @@ use Tribe\Project\Container\Service_Provider; use Tribe\Project\Cron\Thirty_Minutes; use Tribe\Project\Cron\Schedule_Collection; use Tribe\Project\Cron\My_Cron; -use Tribe\Project\Cron\Abstract_Cron; +use Tribe\Libs\Cron\Abstract_Cron; class Cron_Provider extends Service_Provider { From 2f3294c0b7189ceeb251702dc55324ff479de8d6 Mon Sep 17 00:00:00 2001 From: defunctl Date: Mon, 13 Jan 2020 14:15:28 -0700 Subject: [PATCH 6/6] Fix spacing --- src/Cron/README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Cron/README.md b/src/Cron/README.md index adbf8b42..8678eeb4 100644 --- a/src/Cron/README.md +++ b/src/Cron/README.md @@ -128,25 +128,25 @@ use Tribe\Project\Cron\Abstract_Cron; class Cron_Schema extends Schema { - protected $schema_version = 1; +protected $schema_version = 1; - public function get_updates() { - return [ - 1 => [ $this, 'enable_cron_jobs' ] - ]; - } - - /** - * Enable Cron Jobs - */ - public function enable_cron_jobs() { - $container = tribe_project()->container(); - - /** @var Abstract_Cron $cron */ + public function get_updates() { + return [ + 1 => [ $this, 'enable_cron_jobs' ] + ]; + } + + /** + * Enable Cron Jobs + */ + public function enable_cron_jobs() { + $container = tribe_project()->container(); + + /** @var Abstract_Cron $cron */ foreach( $container[ 'cron.cron_collection' ] as $cron ) { $cron->enable(); } - } + } } ```