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 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..8678eeb4 --- /dev/null +++ b/src/Cron/README.md @@ -0,0 +1,181 @@ +# 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 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" + } + } +}