Advertisement
Beee

Setup crons

Jul 9th, 2024 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.94 KB | Source Code | 0 0
  1. <?php
  2.     /*
  3.     Plugin Name: Manage Crons
  4.     Version: 1.0
  5.     Description: This plugin handles (some) cron jobs.
  6.     Author: Aseda
  7.     Author URI: https://aseda.nl
  8.  
  9.     http://www.berryplasman.com
  10.        ___  ____ ____ ____
  11.       / _ )/ __/  __/  __/
  12.      / _  / _/   _/   _/
  13.     /____/___/____/____/
  14.  
  15.     */
  16.  
  17.     if ( ! defined( 'ABSPATH' ) ) {
  18.         exit; // Exit if accessed directly
  19.     }
  20.  
  21.     if ( ! class_exists( 'Sd8Crons' ) ) :
  22.  
  23.         /**
  24.          * Main class
  25.          */
  26.         class Sd8Crons {
  27.  
  28.             var $settings;
  29.  
  30.             /**
  31.              *  A dummy constructor to ensure plugin is only initialized once
  32.              */
  33.             public function __construct() {
  34.             }
  35.  
  36.             public function initialize() {
  37.                 $this->settings = [
  38.                     'path'    => trailingslashit( dirname( __FILE__ ) ),
  39.                     'version' => '1.0',
  40.                 ];
  41.  
  42.                 register_activation_hook( __FILE__,     [ $this, 'plugin_activation' ] );
  43.                 register_deactivation_hook( __FILE__,   [ $this, 'plugin_deactivation' ] );
  44.  
  45.                 add_action( 'sd_expire_orders',             [ $this, 'sd_expire_orders' ] );
  46.  
  47.                 add_filter( 'cron_schedules',               [ $this, 'sd_cron_schedules' ] );
  48.             }
  49.  
  50.  
  51.             public function plugin_activation() {
  52.                 if ( ! wp_next_scheduled( 'sd_expire_orders' ) ) {
  53.                     wp_schedule_event( time(), 'every_5_min', 'sd_expire_orders' );
  54.                 }
  55.             }
  56.  
  57.  
  58.             public function plugin_deactivation() {
  59.                 $timestamp = wp_next_scheduled( 'sd_expire_orders' );
  60.                 wp_unschedule_event( $timestamp, 'sd_expire_orders' );
  61.             }
  62.  
  63.  
  64.             public function sd_cron_schedules( $cron_schedules ) {
  65.                 if ( ! array_key_exists( 'every_5_min', $cron_schedules ) ) {
  66.                     $cron_schedules[ 'every_5_min' ] = array(
  67.                         'interval' => 5 * MINUTE_IN_SECONDS,
  68.                         'display'  => __( 'Every 5 min' ),
  69.                     );
  70.                 }
  71.  
  72.                 return $cron_schedules;
  73.             }
  74.  
  75.  
  76.             /**
  77.              * Expire orders if not paid
  78.              *
  79.              * @return void
  80.              */
  81.             public function sd_expire_orders() {
  82.                 if ( function_exists( 'sd_get_unpaid_orders' ) ) {
  83.                     $unpaid_orders = sd_get_unpaid_orders();
  84.                     if ( ! empty( $unpaid_orders ) ) {
  85.                         foreach( $unpaid_orders as $order ) {
  86.                             $order_time = (int) $order->order_time;
  87.                             $expiry     = 'bankwire' == $order->payment_method ? 7 * DAY_IN_SECONDS : 10 * MINUTE_IN_SECONDS;
  88.                             $expire_on  = $order_time + $expiry;
  89.  
  90.                             if ( $expire_on < sd_get_local_date_time( time(), 'U' ) ) {
  91.                                 global $wpdb;
  92.                                 $table = $wpdb->prefix . 'orders';
  93.                                 $data  = [ 'order_status' => 'expired' ];
  94.                                 $where = [ 'id' => $order->id ];
  95.                                 $wpdb->update( $table, $data, $where );
  96.                             }
  97.                         }
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.  
  103.  
  104.         /**
  105.          * The main function responsible for returning the one true Sd8Crons instance to functions everywhere.
  106.          *
  107.          * @return \Sd8Crons
  108.          */
  109.         function init_crons_plugin() {
  110.             global $crons_plugin;
  111.  
  112.             if ( ! isset( $crons_plugin ) ) {
  113.                 $crons_plugin = new Sd8Crons();
  114.                 $crons_plugin->initialize();
  115.             }
  116.  
  117.             return $crons_plugin;
  118.         }
  119.  
  120.         // initialize
  121.         init_crons_plugin();
  122.  
  123.     endif; // class_exists check
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement