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