Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Manage Crons
- Version: 1.0
- Description: This plugin handles (some) cron jobs.
- Author: Aseda
- Author URI: https://aseda.nl
- http://www.berryplasman.com
- ___ ____ ____ ____
- / _ )/ __/ __/ __/
- / _ / _/ _/ _/
- /____/___/____/____/
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit; // Exit if accessed directly
- }
- if ( ! class_exists( 'Sd8Crons' ) ) :
- /**
- * Main class
- */
- class Sd8Crons {
- var $settings;
- /**
- * A dummy constructor to ensure plugin is only initialized once
- */
- public function __construct() {
- }
- public function initialize() {
- $this->settings = [
- 'path' => trailingslashit( dirname( __FILE__ ) ),
- 'version' => '1.0',
- ];
- register_activation_hook( __FILE__, [ $this, 'plugin_activation' ] );
- register_deactivation_hook( __FILE__, [ $this, 'plugin_deactivation' ] );
- add_action( 'sd_expire_orders', [ $this, 'sd_expire_orders' ] );
- add_filter( 'cron_schedules', [ $this, 'sd_cron_schedules' ] );
- }
- public function plugin_activation() {
- if ( ! wp_next_scheduled( 'sd_expire_orders' ) ) {
- wp_schedule_event( time(), 'every_5_min', 'sd_expire_orders' );
- }
- }
- public function plugin_deactivation() {
- $timestamp = wp_next_scheduled( 'sd_expire_orders' );
- wp_unschedule_event( $timestamp, 'sd_expire_orders' );
- }
- public function sd_cron_schedules( $cron_schedules ) {
- if ( ! array_key_exists( 'every_5_min', $cron_schedules ) ) {
- $cron_schedules[ 'every_5_min' ] = array(
- 'interval' => 5 * MINUTE_IN_SECONDS,
- 'display' => __( 'Every 5 min' ),
- );
- }
- return $cron_schedules;
- }
- /**
- * Expire orders if not paid
- *
- * @return void
- */
- public function sd_expire_orders() {
- if ( function_exists( 'sd_get_unpaid_orders' ) ) {
- $unpaid_orders = sd_get_unpaid_orders();
- if ( ! empty( $unpaid_orders ) ) {
- foreach( $unpaid_orders as $order ) {
- $order_time = (int) $order->order_time;
- $expiry = 'bankwire' == $order->payment_method ? 7 * DAY_IN_SECONDS : 10 * MINUTE_IN_SECONDS;
- $expire_on = $order_time + $expiry;
- if ( $expire_on < sd_get_local_date_time( time(), 'U' ) ) {
- global $wpdb;
- $table = $wpdb->prefix . 'orders';
- $data = [ 'order_status' => 'expired' ];
- $where = [ 'id' => $order->id ];
- $wpdb->update( $table, $data, $where );
- }
- }
- }
- }
- }
- }
- /**
- * The main function responsible for returning the one true Sd8Crons instance to functions everywhere.
- *
- * @return \Sd8Crons
- */
- function init_crons_plugin() {
- global $crons_plugin;
- if ( ! isset( $crons_plugin ) ) {
- $crons_plugin = new Sd8Crons();
- $crons_plugin->initialize();
- }
- return $crons_plugin;
- }
- // initialize
- init_crons_plugin();
- endif; // class_exists check
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement