| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Cron Jobs |
|---|
| 4 | * |
|---|
| 5 | * This class is for loading up cron jobs for the TikTok & Instagram Basic Feed. |
|---|
| 6 | * It will refresh an access token every 24 hours for TikTok and every 54 days for Instagram. |
|---|
| 7 | * Instagram expires every 60 days, but we are setting it to 54 days to be safe. |
|---|
| 8 | * YouTube expires every hour. |
|---|
| 9 | * |
|---|
| 10 | * @class CronJobs |
|---|
| 11 | * @version 4.2.3 |
|---|
| 12 | * @copyright Copyright (c) 2012-2024, SlickRemix |
|---|
| 13 | * @category Class |
|---|
| 14 | * @author feedthemsocial |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | namespace feedthemsocial\admin\cron_jobs; |
|---|
| 18 | |
|---|
| 19 | use feedthemsocial\includes\DebugLog; |
|---|
| 20 | |
|---|
| 21 | // Exit if accessed directly. |
|---|
| 22 | if ( ! \defined( 'ABSPATH' ) ) { |
|---|
| 23 | exit; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | class CronJobs { |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * Feed Functions |
|---|
| 30 | * |
|---|
| 31 | * The Feed Functions Class |
|---|
| 32 | * |
|---|
| 33 | * @var object |
|---|
| 34 | */ |
|---|
| 35 | public $feedFunctions; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Options Functions |
|---|
| 39 | * |
|---|
| 40 | * The settings Functions class. |
|---|
| 41 | * |
|---|
| 42 | * @var object |
|---|
| 43 | */ |
|---|
| 44 | public $optionsFunctions; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Settings Functions |
|---|
| 48 | * |
|---|
| 49 | * The settings Functions class. |
|---|
| 50 | * |
|---|
| 51 | * @var object |
|---|
| 52 | */ |
|---|
| 53 | public $settingsFunctions; |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Feed Cache. |
|---|
| 57 | * |
|---|
| 58 | * Class used for caching. |
|---|
| 59 | * |
|---|
| 60 | * @var object |
|---|
| 61 | */ |
|---|
| 62 | public $feedCache; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Core_Functions constructor. |
|---|
| 66 | */ |
|---|
| 67 | public function __construct( $feedFunctions, $optionsFunctions, $settingsFunctions, $feedCache ) { |
|---|
| 68 | |
|---|
| 69 | // Feed Functions Class. |
|---|
| 70 | $this->feedFunctions = $feedFunctions; |
|---|
| 71 | |
|---|
| 72 | // Options Functions Class. |
|---|
| 73 | $this->optionsFunctions = $optionsFunctions; |
|---|
| 74 | |
|---|
| 75 | // Feed Settings. |
|---|
| 76 | $this->settingsFunctions = $settingsFunctions; |
|---|
| 77 | |
|---|
| 78 | // Set Feed Cache object. |
|---|
| 79 | $this->feedCache = $feedCache; |
|---|
| 80 | |
|---|
| 81 | // Add Actions and Filters. |
|---|
| 82 | $this->addActionsFilters(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Add Action Filters |
|---|
| 87 | */ |
|---|
| 88 | public function addActionsFilters() { |
|---|
| 89 | // Hook for the custom cron schedules |
|---|
| 90 | add_filter('cron_schedules', array($this, 'ftsCronSchedules')); |
|---|
| 91 | |
|---|
| 92 | // Register actions on init |
|---|
| 93 | add_action('init', array($this, 'registerCronActions')); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Adds custom intervals for cron jobs |
|---|
| 98 | */ |
|---|
| 99 | public function ftsCronSchedules($schedules) { |
|---|
| 100 | // Tiktok refreshes every 24 hours. |
|---|
| 101 | if (!isset($schedules['once_daily'])) { |
|---|
| 102 | $schedules['once_daily'] = array( |
|---|
| 103 | 'interval' => 86400, // 86400 is 24 hours in seconds |
|---|
| 104 | 'display' => __('Once Daily') |
|---|
| 105 | ); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | // Instagram refreshes every 54 days. |
|---|
| 109 | if (!isset($schedules['every_54_days'])) { |
|---|
| 110 | $schedules['every_54_days'] = array( |
|---|
| 111 | 'interval' => 54 * DAY_IN_SECONDS, // 54 days in seconds |
|---|
| 112 | 'display' => __('Every 54 Days') |
|---|
| 113 | ); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | // YouTube refreshes every 1 Hour. |
|---|
| 117 | if (!isset($schedules['every_hour'])) { |
|---|
| 118 | $schedules['every_hour'] = array( |
|---|
| 119 | 'interval' => 3600, // 3600 is 1 hour in seconds |
|---|
| 120 | 'display' => __('Once Hourly') |
|---|
| 121 | ); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | if (!isset($schedules['fts_cache_clear'])) { |
|---|
| 125 | // Cache clear interval |
|---|
| 126 | // Production: Must be active. |
|---|
| 127 | $fts_settings = get_option('fts_settings'); |
|---|
| 128 | $cache_time = $fts_settings['fts_cache_time'] ?? null; |
|---|
| 129 | // Testing: Set cache clear interval to 60 seconds. |
|---|
| 130 | // $cache_time = '60'; |
|---|
| 131 | $cache_interval = is_numeric($cache_time) && $cache_time > 0 ? (int)$cache_time : 86400; // Default to 1 Day if not set. |
|---|
| 132 | $schedules['fts_cache_clear'] = array( |
|---|
| 133 | 'interval' => $cache_interval, |
|---|
| 134 | 'display' => __('Cache Clear Interval') |
|---|
| 135 | ); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | return $schedules; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * The task to run for clearing the cache |
|---|
| 143 | * |
|---|
| 144 | * Backup to delete all cache in case transient_timeout fails |
|---|
| 145 | */ |
|---|
| 146 | public function clearCacheTask() { |
|---|
| 147 | // Debug: Log task execution |
|---|
| 148 | DebugLog::log( 'cronJobs', 'Running clearCacheTask', true ); |
|---|
| 149 | $this->feedCache->feedThemClearCache(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * Set up a cron job. |
|---|
| 154 | */ |
|---|
| 155 | public function ftsSetCronJob($cpt_id, $feed_name, $revoke_token) { |
|---|
| 156 | |
|---|
| 157 | if( $cpt_id === 'clear-cache-set-cron-job' ){ |
|---|
| 158 | $event_hook = 'fts_clear_cache_event'; |
|---|
| 159 | |
|---|
| 160 | DebugLog::log( 'cronJobs', 'Running fts_clear_cache_event', true ); |
|---|
| 161 | |
|---|
| 162 | // Unschedule any existing events |
|---|
| 163 | $timestamp = wp_next_scheduled($event_hook); |
|---|
| 164 | if ($timestamp) { |
|---|
| 165 | wp_unschedule_event($timestamp, $event_hook); |
|---|
| 166 | |
|---|
| 167 | DebugLog::log( 'cronJobs', 'Running wp_unschedule_event', true ); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | // Schedule the new event |
|---|
| 171 | wp_schedule_event(time(), 'fts_cache_clear', $event_hook); |
|---|
| 172 | |
|---|
| 173 | DebugLog::log( 'cronJobs', 'Running wp_schedule_event', true ); |
|---|
| 174 | } |
|---|
| 175 | else { |
|---|
| 176 | $event_hook = "fts_{$feed_name}_refresh_token_{$cpt_id}"; |
|---|
| 177 | |
|---|
| 178 | // Unschedule any existing event with this hook |
|---|
| 179 | $timestamp = wp_next_scheduled($event_hook, array($cpt_id)); |
|---|
| 180 | if ($timestamp) { |
|---|
| 181 | wp_unschedule_event($timestamp, $event_hook, array($cpt_id)); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | if( $revoke_token === false ){ |
|---|
| 185 | // Determine the schedule based on the feed type |
|---|
| 186 | if( $feed_name === 'instagram_business_basic' ){ |
|---|
| 187 | $schedule = 'every_54_days'; |
|---|
| 188 | } |
|---|
| 189 | elseif( $feed_name === 'tiktok' ){ |
|---|
| 190 | $schedule = 'once_daily'; |
|---|
| 191 | } |
|---|
| 192 | elseif( $feed_name === 'youtube' ){ |
|---|
| 193 | $schedule = 'every_hour'; |
|---|
| 194 | } |
|---|
| 195 | // Schedule a new event |
|---|
| 196 | wp_schedule_event(time(), $schedule, $event_hook, array($cpt_id)); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | // Store the event hook name in the feed options |
|---|
| 200 | $this->optionsFunctions->updateSingleOption('fts_feed_options_array', "{$feed_name}_scheduled_event", $event_hook, true, $cpt_id, false); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * The task to run for each cron job. |
|---|
| 207 | */ |
|---|
| 208 | public function ftsRefreshTokenTask($cpt_id, $feed_name ) { |
|---|
| 209 | if ($feed_name === 'instagram_business_basic') { |
|---|
| 210 | $this->feedFunctions->ftsInstagramRefreshToken($cpt_id); |
|---|
| 211 | } |
|---|
| 212 | elseif ($feed_name === 'tiktok') { |
|---|
| 213 | $this->feedFunctions->ftsTiktokRefreshToken($cpt_id); |
|---|
| 214 | } |
|---|
| 215 | elseif ($feed_name === 'youtube') { |
|---|
| 216 | $this->feedFunctions->ftsYoutubeRefreshToken($cpt_id); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | /** |
|---|
| 221 | * Register actions for each scheduled cron event. |
|---|
| 222 | */ |
|---|
| 223 | public function registerCronActions() { |
|---|
| 224 | // Retrieve all CPTs that have a cron event hook set |
|---|
| 225 | $args = [ |
|---|
| 226 | 'post_type' => 'fts', // Replace with your CPT name |
|---|
| 227 | 'numberposts' => -1, |
|---|
| 228 | 'fields' => 'ids' |
|---|
| 229 | ]; |
|---|
| 230 | $posts = get_posts($args); |
|---|
| 231 | foreach ($posts as $feedCptId) { |
|---|
| 232 | |
|---|
| 233 | $instagram_hook = $this->feedFunctions->getFeedOption($feedCptId, 'instagram_business_basic_scheduled_event'); |
|---|
| 234 | if (!empty($instagram_hook)) { |
|---|
| 235 | add_action($instagram_hook, function() use ($feedCptId) { |
|---|
| 236 | $this->ftsRefreshTokenTask($feedCptId, 'instagram_business_basic'); |
|---|
| 237 | }); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | $tiktok_hook = $this->feedFunctions->getFeedOption($feedCptId, 'tiktok_scheduled_event'); |
|---|
| 241 | if (!empty($tiktok_hook)) { |
|---|
| 242 | add_action($tiktok_hook, function() use ($feedCptId) { |
|---|
| 243 | $this->ftsRefreshTokenTask($feedCptId, 'tiktok'); |
|---|
| 244 | }); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | $youtube_hook = $this->feedFunctions->getFeedOption($feedCptId, 'youtube_scheduled_event'); |
|---|
| 248 | if (!empty($youtube_hook)) { |
|---|
| 249 | add_action($youtube_hook, function() use ($feedCptId) { |
|---|
| 250 | $this->ftsRefreshTokenTask($feedCptId, 'youtube'); |
|---|
| 251 | }); |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | // Register cache clear action |
|---|
| 256 | add_action('fts_clear_cache_event', array($this, 'clearCacheTask')); |
|---|
| 257 | } |
|---|
| 258 | }//end class |
|---|