| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Tag Groups |
| 5 |
* |
| 6 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 7 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 8 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 9 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 10 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 11 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 12 |
* THE SOFTWARE. |
| 13 |
* @package Tag Groups |
| 14 |
* |
| 15 |
* @author Christoph Amthor |
| 16 |
* @copyright 2018 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 17 |
* @license see official vendor website |
| 18 |
* |
| 19 |
* @since 1.24.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 23 |
if (! class_exists('TagGroups_Cron')) { |
| 24 |
/** |
| 25 |
* The main purpose of this class is to keep all cron-related information in one place. |
| 26 |
* |
| 27 |
*/ |
| 28 |
class TagGroups_Cron |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Registers the CRON identifiers and connects them to a method. |
| 32 |
* |
| 33 |
* @param boolean $test Set to true if you need to run this method in a test environment |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public static function register_cron_handlers($test = false) |
| 37 |
{ |
| 38 |
|
| 39 |
if (defined('CM_UNIT_TESTING ') && ! $test) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Task to migrate tags from the base to the premium plugin in the background |
| 45 |
*/ |
| 46 |
add_action('tag_groups_run_term_migration', array( 'TagGroups_Cron_Handlers', 'run_term_migration' )); |
| 47 |
/** |
| 48 |
* Task to check if we need to migrate the tags (regular task that might launch a one-time task) |
| 49 |
*/ |
| 50 |
add_action('tag_groups_check_tag_migration', array( 'TagGroups_Cron_Handlers', 'maybe_schedule_term_migration' )); |
| 51 |
/** |
| 52 |
* Task to check if the automatic migrations have completed |
| 53 |
*/ |
| 54 |
add_action('tag_groups_check_migrations_done', array( 'TagGroups_Cron_Handlers', 'tag_groups_check_migrations_done' )); |
| 55 |
/** |
| 56 |
* Task to clear the transient cache tag_groups_group_terms |
| 57 |
*/ |
| 58 |
add_action('tag_groups_clear_tag_groups_group_terms', array( 'TagGroups_Cron_Handlers', 'clear_tag_groups_group_terms' )); |
| 59 |
/** |
| 60 |
* Task to purge expired transients |
| 61 |
*/ |
| 62 |
add_action('tag_groups_purge_expired_transients', array( 'TagGroups_Cron_Handlers', 'purge_expired_transients' )); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Schedules a single event |
| 67 |
* |
| 68 |
* @param int $seconds_from_now Time in seconds after which to execute the task. |
| 69 |
* @param string $identifier What we used in register_cron_handlers(); |
| 70 |
* @return boolean Whether the event was properly scheduled. |
| 71 |
*/ |
| 72 |
public static function schedule_in_secs($seconds_from_now, $identifier) |
| 73 |
{ |
| 74 |
|
| 75 |
/** |
| 76 |
* We never schedule the same taske twice. Instead we postpone the execution. |
| 77 |
*/ |
| 78 |
|
| 79 |
if ($timestamp = wp_next_scheduled($identifier)) { |
| 80 |
wp_unschedule_event($timestamp, $identifier); |
| 81 |
} |
| 82 |
|
| 83 |
$cron_result = wp_schedule_single_event(time() + $seconds_from_now, $identifier); |
| 84 |
if (false === $cron_result) { |
| 85 |
TagGroups_Error::log('[Tag Groups Pro] Error scheduling single event %s', $identifier); |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
TagGroups_Error::verbose_log('[Tag Groups Pro] Successfully scheduled single event %s after %d seconds', $identifier, $seconds_from_now); |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Schedules a regular event |
| 95 |
* |
| 96 |
* @param string $recurrence 'hourly', 'twicedaily' or 'daily' |
| 97 |
* @param string $identifier What we used in register_cron_handlers(); |
| 98 |
* @return boolean Whether the event was properly scheduled. |
| 99 |
*/ |
| 100 |
public static function schedule_regular($recurrence, $identifier) |
| 101 |
{ |
| 102 |
|
| 103 |
if (wp_next_scheduled($identifier)) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
$cron_result = wp_schedule_event(time(), $recurrence, $identifier); |
| 108 |
if (false === $cron_result) { |
| 109 |
TagGroups_Error::log('[Tag Groups Pro] Error scheduling regular event %s', $identifier); |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
TagGroups_Error::verbose_log('[Tag Groups Pro] Successfully scheduled regular event %s with recurrence %s', $identifier, $recurrence); |
| 114 |
return true; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
} |
| 120 |
|