Files
ultimatemember/includes/core/class-cron.php
T

134 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace um\core;
2023-07-07 13:34:55 +03:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
2018-03-26 01:27:46 +03:00
if ( ! class_exists( 'um\core\Cron' ) ) {
2018-03-20 13:24:38 +02:00
2018-03-02 09:55:49 +02:00
/**
* Class Cron
* @package um\core
*/
class Cron {
2018-03-02 09:55:49 +02:00
/**
* Cron constructor.
*/
public function __construct() {
2023-07-07 13:34:55 +03:00
add_filter( 'cron_schedules', array( $this, 'add_schedules' ) );
add_action( 'wp', array( $this, 'schedule_events' ) );
}
2018-02-15 17:31:51 +08:00
2023-07-07 13:34:55 +03:00
/**
* @return bool
*/
private function cron_disabled() {
2018-03-02 09:55:49 +02:00
/**
2023-07-07 13:34:55 +03:00
* Filters variable for disable Ultimate Member WP Cron actions.
2018-03-02 09:55:49 +02:00
*
2023-07-07 13:34:55 +03:00
* @since 2.0
* @hook um_cron_disable
*
* @param {bool} $is_disabled Shortcode arguments.
*
* @return {bool} Do Cron actions are disabled? True for disable.
*
* @example <caption>Disable all Ultimate Member WP Cron actions.</caption>
* add_filter( 'um_cron_disable', '__return_true' );
2018-03-02 09:55:49 +02:00
*/
2023-07-07 13:34:55 +03:00
return apply_filters( 'um_cron_disable', false );
2018-03-02 09:55:49 +02:00
}
/**
2023-07-07 13:34:55 +03:00
* Adds once weekly to the existing schedules.
*
2018-03-02 09:55:49 +02:00
* @param array $schedules
*
* @return array
*/
public function add_schedules( $schedules = array() ) {
2023-07-07 13:34:55 +03:00
if ( $this->cron_disabled() ) {
return $schedules;
}
2018-03-02 09:55:49 +02:00
$schedules['weekly'] = array(
'interval' => 604800,
2023-07-07 13:34:55 +03:00
'display' => __( 'Once Weekly', 'ultimate-member' ),
2018-03-02 09:55:49 +02:00
);
return $schedules;
}
/**
*
*/
2023-07-07 13:34:55 +03:00
public function schedule_events() {
if ( $this->cron_disabled() ) {
return;
}
2018-03-02 09:55:49 +02:00
$this->weekly_events();
$this->daily_events();
$this->twicedaily_events();
$this->hourly_events();
}
/**
*
*/
private function weekly_events() {
2023-07-07 13:34:55 +03:00
$sunday_start = wp_date( 'w' );
$start_of_week = get_option( 'start_of_week', 0 );
$start_of_week = is_numeric( $start_of_week ) ? $start_of_week : 0;
$week_start = $sunday_start - absint( $start_of_week );
2023-07-07 13:34:55 +03:00
$week_start_day = strtotime( '-' . $week_start . ' days' );
$time = mktime( 0, 0, 0, wp_date( 'm', $week_start_day ), wp_date( 'd', $week_start_day ), wp_date( 'Y', $week_start_day ) );
2018-03-02 09:55:49 +02:00
if ( ! wp_next_scheduled( 'um_weekly_scheduled_events' ) ) {
2023-07-07 13:34:55 +03:00
wp_schedule_event( $time, 'weekly', 'um_weekly_scheduled_events' );
2018-03-02 09:55:49 +02:00
}
}
/**
*
*/
private function daily_events() {
if ( ! wp_next_scheduled( 'um_daily_scheduled_events' ) ) {
2023-07-07 13:34:55 +03:00
$time = mktime( 0, 0, 0, wp_date( 'm' ), wp_date( 'd' ), wp_date( 'Y' ) );
wp_schedule_event( $time, 'daily', 'um_daily_scheduled_events' );
2018-03-02 09:55:49 +02:00
}
}
/**
*
*/
private function twicedaily_events() {
if ( ! wp_next_scheduled( 'um_twicedaily_scheduled_events' ) ) {
2023-07-07 13:34:55 +03:00
$time = mktime( 0, 0, 0, wp_date( 'm' ), wp_date( 'd' ), wp_date( 'Y' ) );
wp_schedule_event( $time, 'twicedaily', 'um_twicedaily_scheduled_events' );
2018-03-02 09:55:49 +02:00
}
}
/**
*
*/
private function hourly_events() {
if ( ! wp_next_scheduled( 'um_hourly_scheduled_events' ) ) {
2023-07-07 13:34:55 +03:00
$time = mktime( wp_date( 'H' ), 0, 0, wp_date( 'm' ), wp_date( 'd' ), wp_date( 'Y' ) );
wp_schedule_event( $time, 'hourly', 'um_hourly_scheduled_events' );
2018-03-02 09:55:49 +02:00
}
}
2023-07-07 13:34:55 +03:00
/**
* Breaks all Ultimate Member registered schedule events.
*/
2022-01-03 13:25:43 +02:00
public function unschedule_events() {
wp_clear_scheduled_hook( 'um_weekly_scheduled_events' );
wp_clear_scheduled_hook( 'um_daily_scheduled_events' );
wp_clear_scheduled_hook( 'um_twicedaily_scheduled_events' );
wp_clear_scheduled_hook( 'um_hourly_scheduled_events' );
}
2018-03-02 09:55:49 +02:00
}
2022-01-03 13:25:43 +02:00
}