| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Events |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Cron |
| 6 |
* @version 1.8.9.1 |
| 7 |
* @author David Bisset |
| 8 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Cron' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Cron |
| 21 |
* |
| 22 |
* @since 1.1.0 |
| 23 |
*/ |
| 24 |
class Charitable_Cron { |
| 25 |
|
| 26 |
/** |
| 27 |
* The single instance of this class. |
| 28 |
* |
| 29 |
* @since 1.1.0 |
| 30 |
* |
| 31 |
* @var Charitable_Cron|null |
| 32 |
*/ |
| 33 |
private static $instance = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns and/or create the single instance of this class. |
| 37 |
* |
| 38 |
* @since 1.2.0 |
| 39 |
* |
| 40 |
* @return Charitable_Cron |
| 41 |
*/ |
| 42 |
public static function get_instance() { |
| 43 |
if ( is_null( self::$instance ) ) { |
| 44 |
self::$instance = new self(); |
| 45 |
} |
| 46 |
|
| 47 |
return self::$instance; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Create class object. |
| 52 |
* |
| 53 |
* @since 1.1.0 |
| 54 |
*/ |
| 55 |
private function __construct() { |
| 56 |
add_action( 'charitable_daily_scheduled_events', array( $this, 'check_expired_campaigns' ) ); |
| 57 |
add_action( 'charitable_daily_scheduled_events', array( $this, 'cleanup_old_logs' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Schedule Charitable event hooks. |
| 62 |
* |
| 63 |
* @since 1.1.0 |
| 64 |
* |
| 65 |
* @return boolean |
| 66 |
*/ |
| 67 |
public static function schedule_events() { |
| 68 |
$ret = false; |
| 69 |
|
| 70 |
if ( ! wp_next_scheduled( 'charitable_daily_scheduled_events' ) ) { |
| 71 |
$ret = wp_schedule_event( time(), 'daily', 'charitable_daily_scheduled_events' ); |
| 72 |
} |
| 73 |
|
| 74 |
return false !== $ret; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Check for expired campaigns. |
| 79 |
* |
| 80 |
* @since 1.1.0 |
| 81 |
* @version 1.8.9.1 |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function check_expired_campaigns() { |
| 86 |
$yesterday = date( 'Y-m-d H:i:s', strtotime( '-24 hours' ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 87 |
|
| 88 |
$args = array( |
| 89 |
'fields' => 'ids', |
| 90 |
'post_type' => Charitable::CAMPAIGN_POST_TYPE, |
| 91 |
'posts_per_page' => -1, |
| 92 |
'post_status' => 'publish', |
| 93 |
'meta_query' => array( |
| 94 |
array( |
| 95 |
'key' => '_campaign_end_date', |
| 96 |
'value' => array( $yesterday, date( 'Y-m-d H:i:s' ) ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 97 |
'compare' => 'BETWEEN', |
| 98 |
'type' => 'datetime' |
| 99 |
) |
| 100 |
) |
| 101 |
); |
| 102 |
|
| 103 |
$campaigns = get_posts( $args ); |
| 104 |
|
| 105 |
if ( empty( $campaigns ) ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
foreach ( $campaigns as $campaign_id ) { |
| 110 |
do_action( 'charitable_campaign_end', $campaign_id ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Cleanup old log records based on retention setting. |
| 116 |
* |
| 117 |
* @since 1.8.11 |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public function cleanup_old_logs() { |
| 122 |
if ( ! class_exists( 'Charitable_Log' ) ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
$days = Charitable_Log::get_retention_days(); |
| 127 |
|
| 128 |
/** |
| 129 |
* Filter the number of days to retain log records. |
| 130 |
* |
| 131 |
* @since 1.8.11 |
| 132 |
* |
| 133 |
* @param int $days Number of days to retain. |
| 134 |
*/ |
| 135 |
$days = apply_filters( 'charitable_logs_retention_days', $days ); |
| 136 |
|
| 137 |
$db = Charitable_Log::get_instance()->get_db(); |
| 138 |
|
| 139 |
if ( $db->table_exists() ) { |
| 140 |
$db->delete_older_than( $days ); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
endif; |
| 146 |
|