| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Events |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Cron |
| 6 |
* @version 1.1.0 |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2014, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_Cron' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable_Cron |
| 19 |
* |
| 20 |
* @since 1.1.0 |
| 21 |
*/ |
| 22 |
class Charitable_Cron { |
| 23 |
|
| 24 |
/** |
| 25 |
* The single instance of this class. |
| 26 |
* |
| 27 |
* @var Charitable_Cron|null |
| 28 |
* @access private |
| 29 |
* @static |
| 30 |
*/ |
| 31 |
private static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns and/or create the single instance of this class. |
| 35 |
* |
| 36 |
* @return Charitable_Cron |
| 37 |
* @access public |
| 38 |
* @since 1.2.0 |
| 39 |
*/ |
| 40 |
public static function get_instance() { |
| 41 |
if ( is_null( self::$instance ) ) { |
| 42 |
self::$instance = new Charitable_Cron(); |
| 43 |
} |
| 44 |
|
| 45 |
return self::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create class object. |
| 50 |
* |
| 51 |
* @access private |
| 52 |
* @since 1.1.0 |
| 53 |
*/ |
| 54 |
private function __construct() { |
| 55 |
add_action( 'charitable_daily_scheduled_events', array( $this, 'check_expired_campaigns' ) ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Schedule Charitable event hooks. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
* @access public |
| 63 |
* @static |
| 64 |
* @since 1.1.0 |
| 65 |
*/ |
| 66 |
public static function schedule_events() { |
| 67 |
if ( ! wp_next_scheduled( 'charitable_daily_scheduled_events' ) ) { |
| 68 |
wp_schedule_event( time(), 'daily', 'charitable_daily_scheduled_events' ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Check for expired campaigns. |
| 74 |
* |
| 75 |
* @return void |
| 76 |
* @access public |
| 77 |
* @since 1.1.0 |
| 78 |
*/ |
| 79 |
public function check_expired_campaigns() { |
| 80 |
$yesterday = date( 'Y-m-d H:i:s', strtotime( '-24 hours' ) ); |
| 81 |
|
| 82 |
$args = array( |
| 83 |
'fields' => 'ids', |
| 84 |
'post_type' => Charitable::CAMPAIGN_POST_TYPE, |
| 85 |
'posts_per_page' => -1, |
| 86 |
'post_status' => 'publish', |
| 87 |
'meta_query' => array( |
| 88 |
array( |
| 89 |
'key' => '_campaign_end_date', |
| 90 |
'value' => array( $yesterday, date( 'Y-m-d H:i:s' ) ), |
| 91 |
'compare' => 'BETWEEN', |
| 92 |
'type' => 'datetime' |
| 93 |
) |
| 94 |
) |
| 95 |
); |
| 96 |
|
| 97 |
$campaigns = get_posts( $args ); |
| 98 |
|
| 99 |
if ( empty( $campaigns ) ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
foreach ( $campaigns as $campaign_id ) { |
| 104 |
do_action( 'charitable_campaign_end', $campaign_id ); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
endif; // End class_exists check |