admin
11 months ago
core
11 months ago
class-activator.php
11 months ago
class-assets.php
11 months ago
class-builder-page.php
1 year ago
class-deactivator.php
3 years ago
class-debug-info.php
2 years ago
class-feed-ajax.php
1 year ago
class-feed-block.php
1 year ago
class-feed-deserializer.php
11 months ago
class-feed-old.php
4 years ago
class-feed-page.php
11 months ago
class-feed-serializer.php
2 years ago
class-feed-shortcode.php
2 years ago
class-feed-widget.php
4 years ago
class-plugin-overview-ajax.php
1 year ago
class-plugin-overview.php
11 months ago
class-plugin-settings.php
11 months ago
class-plugin-support.php
1 year ago
class-plugin.php
11 months ago
class-post-types.php
3 years ago
class-reviews-cron.php
1 year ago
class-settings-save.php
1 year ago
class-view.php
11 months ago
index.php
4 years ago
page-setting-advance.php
1 year ago
page-setting-fig.php
1 year ago
page-setting-support.php
4 years ago
class-reviews-cron.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Rplg_Google_Reviews\Includes; |
| 4 | |
| 5 | use WP_Rplg_Google_Reviews\Includes\Core\Google_Utils; |
| 6 | |
| 7 | class Reviews_Cron { |
| 8 | |
| 9 | private $google_utils; |
| 10 | private $feed_deserializer; |
| 11 | |
| 12 | public function __construct(Google_Utils $google_utils, Feed_Deserializer $feed_deserializer) { |
| 13 | $this->google_utils = $google_utils; |
| 14 | $this->feed_deserializer = $feed_deserializer; |
| 15 | } |
| 16 | |
| 17 | public function register() { |
| 18 | add_filter('cron_schedules', array($this, 'add_schedules')); |
| 19 | add_action('grw_revupd_schedule', array($this, 'update_schedule')); |
| 20 | |
| 21 | $this->activate(); |
| 22 | } |
| 23 | |
| 24 | public function activate() { |
| 25 | $revupd_cron = get_option('grw_revupd_cron'); |
| 26 | $next_cron_run = wp_next_scheduled('grw_revupd_schedule'); |
| 27 | |
| 28 | if ($revupd_cron !== '0' && !$next_cron_run) { |
| 29 | $start_at = rand(0, 60 * 60 * 12); |
| 30 | $freq = get_option('grw_freq_revs_upd', 'daily'); |
| 31 | wp_schedule_event(time(), $freq, 'grw_revupd_schedule'); |
| 32 | } elseif ($next_cron_run) { |
| 33 | update_option('grw_revupd_cron_timeout', $next_cron_run - time()); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public function add_schedules($schedules = array()) { |
| 38 | $schedules['weekly'] = array( |
| 39 | 'interval' => 60 * 60 * 24 * 7, |
| 40 | 'display' => 'Once Weekly' |
| 41 | ); |
| 42 | $schedules['fortnightly'] = array( |
| 43 | 'interval' => 60 * 60 * 24 * 14, |
| 44 | 'display' => 'Every two weeks' |
| 45 | ); |
| 46 | $schedules['monthly'] = array( |
| 47 | 'interval' => 60 * 60 * 24 * 30, |
| 48 | 'display' => 'Once Monthly' |
| 49 | ); |
| 50 | return $schedules; |
| 51 | } |
| 52 | |
| 53 | public function update_schedule() { |
| 54 | |
| 55 | $start_time = floor(microtime(true) * 1000); |
| 56 | $end_time = 0; |
| 57 | $feed_updated_ids = array(); |
| 58 | |
| 59 | $feed_conn_cache = array(); |
| 60 | |
| 61 | $feed_ids = get_option('grw_feed_ids'); |
| 62 | $ids = explode(",", $feed_ids); |
| 63 | $ids_count = count($ids); |
| 64 | |
| 65 | if ($ids_count > 0) { |
| 66 | |
| 67 | // Trying to walk by all reviews feed to update these |
| 68 | for ($i = 0; $i < $ids_count; $i++) { |
| 69 | |
| 70 | // Get next reviews feed ID |
| 71 | $id = array_shift($ids); |
| 72 | |
| 73 | // Get next reviews feed |
| 74 | $feed = $this->feed_deserializer->get_feed($id); |
| 75 | if ($feed != false && strlen($feed->post_content) > 0) { |
| 76 | |
| 77 | // Parse json content to obtain reviews connectors |
| 78 | $json = json_decode($feed->post_content); |
| 79 | if ($json->connections && count($json->connections) > 0) { |
| 80 | |
| 81 | // Loop by all connectors without repeatable |
| 82 | foreach ($json->connections as $conn) { |
| 83 | |
| 84 | if (isset($conn->refresh) && $conn->refresh) { |
| 85 | |
| 86 | // Create pair 'place_id:lang' to update it |
| 87 | $arg_local_img = isset($conn->local_img) ? $conn->local_img : 'false'; |
| 88 | |
| 89 | $args = array($conn->id, $conn->lang, $arg_local_img); |
| 90 | $args_key = implode(":", $args); |
| 91 | |
| 92 | // If not met, update |
| 93 | if (!in_array($args_key, $feed_conn_cache)) { |
| 94 | $this->google_utils->refresh($args); |
| 95 | array_push($feed_conn_cache, $args_key); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Put reviews feed ID to log |
| 101 | array_push($feed_updated_ids, $id); |
| 102 | |
| 103 | // Put reviews feed ID to the end of feed_ids option |
| 104 | array_push($ids, $id); |
| 105 | update_option('grw_feed_ids', implode(",", $ids)); |
| 106 | |
| 107 | // Clear feed cache |
| 108 | delete_transient('grw_feed_' . GRW_VERSION . '_' . $id . '_reviews', false); |
| 109 | |
| 110 | // Check execution time |
| 111 | $end_time = floor(microtime(true) * 1000) - $start_time; |
| 112 | if ($end_time > 500) { |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | // Log information |
| 123 | $now = floor(microtime(true) * 1000); |
| 124 | update_option('grw_revupd_cron_log', 'Executed at ' . $now . ' in ' . $end_time . 'ms for feeds: ' . implode(", ", $feed_updated_ids)); |
| 125 | } |
| 126 | |
| 127 | public function deactivate() { |
| 128 | $next_scheduled = wp_next_scheduled('grw_revupd_schedule'); |
| 129 | if ($next_scheduled) { |
| 130 | wp_unschedule_event($next_scheduled, 'grw_revupd_schedule'); |
| 131 | update_option('grw_revupd_cron_timeout', ''); |
| 132 | } |
| 133 | } |
| 134 | } |