PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 152
Lasso Lite – Affiliate Link Manager & Product Displays v152
157 155 156 154 153 152 151 150 149 148 trunk 0.9.9 104 105 106 107 108 109 110 111 112 113 114 115 116 All 56 releases
simple-urls / classes / class-cron.php

class-cron.php in Lasso Lite – Affiliate Link Manager & Product Displays 152, at classes/class-cron.php

337 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Declare class Config
4 *
5 * @package Config
6 */
7
8 namespace LassoLite\Classes;
9
10 use LassoLite\Classes\Estimate_Earning;
11 use LassoLite\Classes\License;
12 use LassoLite\Classes\Processes\Amazon;
13 use LassoLite\Classes\Processes\Amazon_Shortlink;
14 use LassoLite\Classes\Processes\Import_All;
15 use LassoLite\Classes\Processes\Revert_All;
16 use LassoLite\Classes\Setting;
17 use LassoLite\Classes\Enum;
18 use LassoLite\Admin\Constant;
19
20 /**
21 * Config
22 */
23 class Cron {
24
25 const CRONS = array(
26 'lasso_lite_amazon_shortlink' => 'lasso_lite_15_minutes',
27 'lasso_lite_update_amazon' => 'lasso_lite_15_minutes',
28 'lasso_lite_import_all' => 'lasso_lite_15_minutes',
29 'lasso_lite_revert_all' => 'lasso_lite_15_minutes',
30 'lasso_lite_tracking_support_status' => 'daily',
31 'lasso_lite_update_license_status' => 'daily',
32 'lasso_lite_cron_get_snippet' => 'daily',
33 'lasso_lite_cron_get_js_domain' => 'daily',
34 'lasso_lite_cron_get_info' => 'daily',
35 'lasso_lite_check_lite_user' => 'daily',
36 Estimate_Earning::CRON_HOOK => 'weekly',
37 );
38
39 /**
40 * Cron constructor.
41 */
42 public function register_hooks() {
43 add_filter( 'cron_schedules', array( $this, 'add_lasso_cron' ) );
44 add_action( 'lasso_lite_tracking_support_status', array( $this, 'lasso_lite_tracking_support_status' ) );
45 add_action( 'lasso_lite_import_all', array( $this, 'lasso_import_all' ) );
46 add_action( 'lasso_lite_revert_all', array( $this, 'lasso_revert_all' ) );
47 add_action( 'lasso_lite_update_amazon', array( $this, 'lasso_lite_update_amazon' ) );
48 add_action( 'lasso_lite_amazon_shortlink', array( $this, 'lasso_lite_amazon_shortlink' ) );
49 add_action( 'lasso_lite_update_license_status', array( $this, 'lasso_lite_update_license_status' ) );
50 add_action( 'lasso_lite_cron_get_snippet', array( $this, 'lasso_lite_cron_get_snippet' ) );
51 add_action( 'lasso_lite_cron_get_js_domain', array( $this, 'lasso_lite_cron_get_js_domain' ) );
52 add_action( 'lasso_lite_cron_get_info', array( $this, 'lasso_lite_cron_get_info' ) );
53 add_action( 'lasso_lite_check_lite_user', array( $this, 'lasso_lite_check_lite_user' ) );
54 add_action( Estimate_Earning::CRON_HOOK, array( $this, 'lasso_lite_weekly_estimate_earning' ) );
55 $this->lasso_create_schedule_hook();
56 }
57
58 /**
59 * Add a custom cron to WordPress
60 *
61 * @param array $schedules An array of non-default cron schedules. Default empty.
62 */
63 public function add_lasso_cron( $schedules ) {
64 $schedules['lasso_lite_15_minutes'] = array(
65 'interval' => 15 * MINUTE_IN_SECONDS, // ? 15 minutes in seconds
66 'display' => __( '15 minutes' ),
67 );
68
69 if ( ! isset( $schedules['weekly'] ) ) {
70 $schedules['weekly'] = array(
71 'interval' => WEEK_IN_SECONDS,
72 'display' => __( 'Once Weekly' ),
73 );
74 }
75
76 return $schedules;
77 }
78
79 /**
80 * Upgrade the stored cron option structure to version 2 (hash-keyed args).
81 *
82 * Mirrors WordPress core cron array shape so iteration in this class matches
83 * `wp_next_scheduled` / `wp_schedule_event` expectations.
84 *
85 * @param array $cron Cron info array from lasso_get_stored_cron_array().
86 * @return array Upgraded cron info array.
87 */
88 private function lasso_upgrade_stored_cron_array( $cron ) {
89 if ( isset( $cron['version'] ) && 2 === $cron['version'] ) {
90 return $cron;
91 }
92
93 $new_cron = array();
94
95 foreach ( (array) $cron as $timestamp => $hooks ) {
96 foreach ( (array) $hooks as $hook => $args ) {
97 $key = md5( serialize( $args['args'] ) );
98
99 $new_cron[ $timestamp ][ $hook ][ $key ] = $args;
100 }
101 }
102
103 $new_cron['version'] = 2;
104
105 update_option( 'cron', $new_cron );
106
107 return $new_cron;
108 }
109
110 /**
111 * Load the `cron` option as an array, upgrading legacy shape when needed.
112 *
113 * @return array Cron events (no `version` key in the returned array).
114 */
115 private function lasso_get_stored_cron_array() {
116 $cron = get_option( 'cron' );
117 if ( ! is_array( $cron ) ) {
118 return array();
119 }
120
121 if ( ! isset( $cron['version'] ) ) {
122 $cron = $this->lasso_upgrade_stored_cron_array( $cron );
123 }
124
125 unset( $cron['version'] );
126
127 return $cron;
128 }
129
130 /**
131 * Create hook for the new cron
132 */
133 public function lasso_create_schedule_hook() {
134 $crons = self::CRONS;
135 $events = array();
136 $crons_array = $this->lasso_get_stored_cron_array();
137
138 if ( ! is_array( $crons_array ) ) {
139 return;
140 }
141
142 foreach ( $crons_array as $time => $cron ) {
143 foreach ( $cron as $hook => $dings ) {
144 if ( strpos( $hook, 'lasso_lite_' ) === false ) {
145 continue;
146 }
147
148 foreach ( $dings as $sig => $data ) {
149 $interval = $data['interval'] ?? HOUR_IN_SECONDS;
150
151 // ? get the cron that is less than the existing one
152 if ( isset( $events[ $hook ] ) && $interval >= $events[ $hook ]->interval ) {
153 continue;
154 }
155
156 $events[ $hook ] = (object) array(
157 'hook' => $hook,
158 'time' => $time, // ? UTC
159 'schedule' => $data['schedule'],
160 'interval' => $interval,
161 );
162 }
163 }
164 }
165
166 foreach ( $crons as $cron_name => $interval ) {
167 $next_scheduled = wp_next_scheduled( $cron_name );
168 if ( ! $next_scheduled ) {
169 // No schedule exists - create a new one.
170 wp_schedule_event( time(), $interval, $cron_name );
171 }
172 }
173 }
174
175 /**
176 * Tracking support status
177 */
178 public function lasso_lite_tracking_support_status() {
179 $settings = Setting::get_settings();
180 if ( boolval( $settings[ Enum::SUPPORT_ENABLED ] ) ) {
181 Setting::save_support( false );
182 }
183 }
184
185 /**
186 * Import all
187 */
188 public function lasso_import_all() {
189 $allow_import_all = get_option( Import_All::OPTION, '0' );
190 if ( 1 === intval( $allow_import_all ) ) {
191 $lasso_import_all = new Import_All();
192 $lasso_import_all->import();
193 }
194 }
195
196 /**
197 * Revert all
198 */
199 public function lasso_revert_all() {
200 $allow_revert_all = get_option( Revert_All::OPTION, '0' );
201 if ( 1 === intval( $allow_revert_all ) ) {
202 $lasso_import_all = new Revert_All();
203 $lasso_import_all->revert();
204 }
205 }
206
207 /**
208 * Revert all
209 */
210 public function lasso_lite_update_amazon() {
211 $settings = Setting::get_settings();
212 if ( boolval( $settings['amazon_pricing_daily'] ) ) {
213 $lasso_amazon = new Amazon();
214 $lasso_amazon->run();
215 }
216 }
217
218 /**
219 * Revert all
220 */
221 public function lasso_lite_amazon_shortlink() {
222 $settings = Setting::get_settings();
223 if ( boolval( $settings['amazon_pricing_daily'] ) ) {
224 $lasso_amazon = new Amazon_Shortlink();
225 $lasso_amazon->run();
226 }
227 }
228
229 /**
230 * Update license status.
231 */
232 public function lasso_lite_update_license_status() {
233 License::check_user_license();
234 }
235
236 /**
237 * Daily update snippet: Fetch snippet performance and write to connect-snippet.min.js
238 */
239 public function lasso_lite_cron_get_snippet() {
240 try {
241 $url = Constant::LASSO_LINK . '/api/snippet/performance?ver=' . time();
242 $res = Helper::send_request( 'get', $url );
243
244 $status_code = isset( $res['status_code'] ) ? intval( $res['status_code'] ) : 0;
245 $body = isset( $res['response'] ) ? ( $res['response']->content ?? '' ) : '';
246 $body_str = is_string( $body ) ? $body : wp_json_encode( $body );
247
248 if ( 200 === $status_code && ! empty( $body_str ) && strpos( $body_str, 'LASSO_REDIRECT_AMAZON_URL,' ) !== false ) {
249 $file_path = LASSO_CONNECT_SNIPPET_FILE_LITE;
250 $result = file_put_contents( $file_path, (string) $body_str );
251 if ( false === $result ) {
252 return false;
253 }
254 }
255 return true;
256 } catch ( \Exception $e ) {
257 return false;
258 }
259 }
260
261 /**
262 * Fetches the JS domain URL from the remote API and updates the 'js_domain' option if valid.
263 *
264 * @return string Returns an empty string. Returns early on exception or after processing.
265 * @throws \Exception This method catches all exceptions internally and does not throw.
266 */
267 public function lasso_lite_cron_get_js_domain() {
268 try {
269 $url = Constant::LASSO_LINK . '/api/js-domain?ver=' . time();
270 $res = Helper::send_request( 'get', $url );
271 $status_code = intval( $res['status_code'] ?? 500 );
272 $response = $res['response'] ?? '';
273
274 if ( 200 === $status_code && $response ) {
275 // API returns JSON object that contains a URL to the JS file
276 $file_url = $response->url ?? '';
277 // Only return the URL. Do not fetch or write files here.
278 if ( ! empty( $file_url ) && Helper::validate_url( $file_url ) ) {
279 Helper::update_option( 'js_domain', $file_url );
280 }
281
282 $full_file_url = $response->full_url ?? '';
283 if ( ! empty( $full_file_url ) && Helper::validate_url( $full_file_url ) ) {
284 Helper::update_option( 'full_js_domain', $full_file_url );
285 }
286 }
287 } catch ( \Exception $e ) {
288 return '';
289 }
290
291 return '';
292 }
293
294 /**
295 * Get info
296 */
297 public function lasso_lite_cron_get_info() {
298 try {
299 License::lasso_getinfo(['license_key']);
300 } catch ( \Exception $e ) {
301 return false;
302 }
303
304 return true;
305 }
306
307 /**
308 * Daily: request lite user record for this site's admin email.
309 *
310 * @return bool True when the HTTP request completes with 200.
311 */
312 public function lasso_lite_check_lite_user() {
313 try {
314 $admin_email = get_option( 'admin_email' );
315 if ( empty( $admin_email ) || ! is_email( $admin_email ) ) {
316 return false;
317 }
318
319 $url = Constant::LASSO_LINK . '/plugin/lite/users/' . rawurlencode( $admin_email );
320 $headers = Helper::get_headers();
321 $res = Helper::send_request( 'get', $url, array(), $headers );
322
323 $status_code = isset( $res['status_code'] ) ? intval( $res['status_code'] ) : 0;
324 return 200 === $status_code;
325 } catch ( \Exception $e ) {
326 return false;
327 }
328 }
329
330 /**
331 * Weekly: refresh cached orphan Affiliate+ payout estimate for the weekly banner.
332 */
333 public function lasso_lite_weekly_estimate_earning() {
334 Estimate_Earning::fetch_and_cache();
335 }
336 }
337