@@ -33,10 +33,10 @@ discard block |
||
| 33 | 33 | * @access private |
| 34 | 34 | */ |
| 35 | 35 | public function __construct() { |
| 36 | - add_filter( 'cron_schedules', array( $this, 'register_schedule' ), 10, 1 ); |
|
| 37 | - add_action( 'lsx_wetu_importer_settings_before', array( $this, 'watch_for_trigger' ), 200 ); |
|
| 38 | - add_action( 'lsx_wetu_accommodation_images_cron', array( $this, 'process' ), 10, 1 ); |
|
| 39 | - add_action( 'lsx_wetu_accommodation_images_sync', array( $this, 'cron_callback' ), 10, 1 ); |
|
| 36 | + add_filter('cron_schedules', array($this, 'register_schedule'), 10, 1); |
|
| 37 | + add_action('lsx_wetu_importer_settings_before', array($this, 'watch_for_trigger'), 200); |
|
| 38 | + add_action('lsx_wetu_accommodation_images_cron', array($this, 'process'), 10, 1); |
|
| 39 | + add_action('lsx_wetu_accommodation_images_sync', array($this, 'cron_callback'), 10, 1); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | public static function get_instance() { |
| 50 | 50 | // If the single instance hasn't been set, set it now. |
| 51 | - if ( null === self::$instance ) { |
|
| 51 | + if (null === self::$instance) { |
|
| 52 | 52 | self::$instance = new self(); |
| 53 | 53 | } |
| 54 | 54 | return self::$instance; |
@@ -60,10 +60,10 @@ discard block |
||
| 60 | 60 | * @param array $schedules |
| 61 | 61 | * @return array |
| 62 | 62 | */ |
| 63 | - public function register_schedule( $schedules ) { |
|
| 63 | + public function register_schedule($schedules) { |
|
| 64 | 64 | $schedules['wetu-5-minutes'] = array( |
| 65 | 65 | 'interval' => 5 * MINUTE_IN_SECONDS, |
| 66 | - 'display' => __( 'Every 5 minutes', 'lsx-wetu-importer' ), |
|
| 66 | + 'display' => __('Every 5 minutes', 'lsx-wetu-importer'), |
|
| 67 | 67 | ); |
| 68 | 68 | return $schedules; |
| 69 | 69 | } |
@@ -75,26 +75,26 @@ discard block |
||
| 75 | 75 | */ |
| 76 | 76 | public function watch_for_trigger() { |
| 77 | 77 | |
| 78 | - if ( isset( $_GET['page'] ) && 'lsx-wetu-importer' === $_GET['page'] && isset( $_GET['tab'] ) && 'settings' === $_GET['tab'] ) { |
|
| 78 | + if (isset($_GET['page']) && 'lsx-wetu-importer' === $_GET['page'] && isset($_GET['tab']) && 'settings' === $_GET['tab']) { |
|
| 79 | 79 | $options = lsx_wetu_get_options(); |
| 80 | 80 | |
| 81 | 81 | // Check what state the option is in. |
| 82 | 82 | $accommodation_cron = 'deactivate'; |
| 83 | - if ( isset( $options['accommodation_images_cron'] ) && '' !== $options['accommodation_images_cron'] ) { |
|
| 83 | + if (isset($options['accommodation_images_cron']) && '' !== $options['accommodation_images_cron']) { |
|
| 84 | 84 | $accommodation_cron = 'activate'; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // Check what state the cron is in. |
| 88 | 88 | $scheduled = false; |
| 89 | - if ( wp_next_scheduled( 'lsx_wetu_accommodation_images_cron' ) ) { |
|
| 89 | + if (wp_next_scheduled('lsx_wetu_accommodation_images_cron')) { |
|
| 90 | 90 | $scheduled = true; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | // If activate and its not running. |
| 94 | - if ( false === $scheduled && 'activate' === $accommodation_cron ) { |
|
| 94 | + if (false === $scheduled && 'activate' === $accommodation_cron) { |
|
| 95 | 95 | $schedule = 'weekly'; |
| 96 | - $this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule ); |
|
| 97 | - } elseif ( true === $scheduled && 'deactivate' === $accommodation_cron ) { |
|
| 96 | + $this->schedule('lsx_wetu_accommodation_images_cron', $schedule); |
|
| 97 | + } elseif (true === $scheduled && 'deactivate' === $accommodation_cron) { |
|
| 98 | 98 | $this->deactivate(); |
| 99 | 99 | } |
| 100 | 100 | } |
@@ -105,8 +105,8 @@ discard block |
||
| 105 | 105 | * |
| 106 | 106 | * @return void |
| 107 | 107 | */ |
| 108 | - public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) { |
|
| 109 | - wp_clear_scheduled_hook( $task, array( $task ) ); |
|
| 108 | + public function deactivate($task = 'lsx_wetu_accommodation_images_cron') { |
|
| 109 | + wp_clear_scheduled_hook($task, array($task)); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -117,16 +117,16 @@ discard block |
||
| 117 | 117 | * @param string $time |
| 118 | 118 | * @return void |
| 119 | 119 | */ |
| 120 | - public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm' ) { |
|
| 121 | - $args = array( $task ); |
|
| 122 | - if ( '' === $time ) { |
|
| 120 | + public function schedule($task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm') { |
|
| 121 | + $args = array($task); |
|
| 122 | + if ('' === $time) { |
|
| 123 | 123 | $time = time(); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - if ( isset( $_GET['accommodation_images_cron_featured'] ) && '' !== $_GET['accommodation_images_cron_featured'] ) { |
|
| 126 | + if (isset($_GET['accommodation_images_cron_featured']) && '' !== $_GET['accommodation_images_cron_featured']) { |
|
| 127 | 127 | $args[] = 'featured_image'; |
| 128 | 128 | } |
| 129 | - wp_schedule_event( $time, $schedule, $task, $args ); |
|
| 129 | + wp_schedule_event($time, $schedule, $task, $args); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -134,8 +134,8 @@ discard block |
||
| 134 | 134 | * |
| 135 | 135 | * @return void |
| 136 | 136 | */ |
| 137 | - public function process( $task = '' ) { |
|
| 138 | - switch ( $task ) { |
|
| 137 | + public function process($task = '') { |
|
| 138 | + switch ($task) { |
|
| 139 | 139 | case 'lsx_wetu_accommodation_images_cron': |
| 140 | 140 | $this->register_accommodation_images_sync(); |
| 141 | 141 | break; |
@@ -151,10 +151,10 @@ discard block |
||
| 151 | 151 | * @return void |
| 152 | 152 | */ |
| 153 | 153 | public function register_accommodation_images_sync() { |
| 154 | - $time = strtotime( '+5 min' ); |
|
| 155 | - if ( ! wp_next_scheduled( 'lsx_wetu_accommodation_images_sync' ) ) { |
|
| 156 | - $this->load_items_to_sync( 'accommodation_images' ); |
|
| 157 | - $this->schedule( 'lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time ); |
|
| 154 | + $time = strtotime('+5 min'); |
|
| 155 | + if (!wp_next_scheduled('lsx_wetu_accommodation_images_sync')) { |
|
| 156 | + $this->load_items_to_sync('accommodation_images'); |
|
| 157 | + $this->schedule('lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time); |
|
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | |
@@ -163,48 +163,48 @@ discard block |
||
| 163 | 163 | * |
| 164 | 164 | * @return void |
| 165 | 165 | */ |
| 166 | - public function cron_callback( $task = '', $featured_image = '' ) { |
|
| 167 | - $has_accommodation = get_option( $task ); |
|
| 168 | - if ( false !== $has_accommodation && ! empty( $has_accommodation ) ) { |
|
| 169 | - $next_time = array_slice( $has_accommodation, 5 ); |
|
| 170 | - $this_time = array_slice( $has_accommodation, 0, 4 ); |
|
| 166 | + public function cron_callback($task = '', $featured_image = '') { |
|
| 167 | + $has_accommodation = get_option($task); |
|
| 168 | + if (false !== $has_accommodation && !empty($has_accommodation)) { |
|
| 169 | + $next_time = array_slice($has_accommodation, 5); |
|
| 170 | + $this_time = array_slice($has_accommodation, 0, 4); |
|
| 171 | 171 | |
| 172 | 172 | $api_key = $this->get_api_key(); |
| 173 | - $url = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids='; |
|
| 173 | + $url = 'https://wetu.com/API/Pins/'.$api_key.'/Get?all=include&ids='; |
|
| 174 | 174 | |
| 175 | 175 | // Run through the current items. |
| 176 | - foreach ( $this_time as $accommodation ) { |
|
| 177 | - $wetu_id = get_post_meta( $accommodation, 'lsx_wetu_id', true ); |
|
| 178 | - $last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true ); |
|
| 176 | + foreach ($this_time as $accommodation) { |
|
| 177 | + $wetu_id = get_post_meta($accommodation, 'lsx_wetu_id', true); |
|
| 178 | + $last_date = get_post_meta($accommodation, 'lsx_wetu_modified_date', true); |
|
| 179 | 179 | |
| 180 | - $accommodation_info = wp_remote_get( $url . $wetu_id ); |
|
| 181 | - if ( ! empty( $accommodation_info ) && isset( $accommodation_info['response'] ) && isset( $accommodation_info['response']['code'] ) && 200 === $accommodation_info['response']['code'] ) { |
|
| 182 | - $adata = json_decode( $accommodation_info['body'], true ); |
|
| 180 | + $accommodation_info = wp_remote_get($url.$wetu_id); |
|
| 181 | + if (!empty($accommodation_info) && isset($accommodation_info['response']) && isset($accommodation_info['response']['code']) && 200 === $accommodation_info['response']['code']) { |
|
| 182 | + $adata = json_decode($accommodation_info['body'], true); |
|
| 183 | 183 | |
| 184 | - if ( isset( $adata[0] ) && isset( $adata[0]['last_modified'] ) && '' !== $adata[0]['last_modified'] ) { |
|
| 185 | - $modified_time = strtotime( $adata[0]['last_modified'] ); |
|
| 186 | - if ( $modified_time > $last_date ) { |
|
| 184 | + if (isset($adata[0]) && isset($adata[0]['last_modified']) && '' !== $adata[0]['last_modified']) { |
|
| 185 | + $modified_time = strtotime($adata[0]['last_modified']); |
|
| 186 | + if ($modified_time > $last_date) { |
|
| 187 | 187 | $accommodation_importer = new \LSX_WETU_Importer_Accommodation(); |
| 188 | - $accommodation_importer->create_main_gallery( $adata, $accommodation ); |
|
| 189 | - if ( '' !== $featured_image ) { |
|
| 190 | - $accommodation_importer->set_featured_image( $adata, $accommodation ); |
|
| 188 | + $accommodation_importer->create_main_gallery($adata, $accommodation); |
|
| 189 | + if ('' !== $featured_image) { |
|
| 190 | + $accommodation_importer->set_featured_image($adata, $accommodation); |
|
| 191 | 191 | } |
| 192 | - update_post_meta( $accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date ); |
|
| 192 | + update_post_meta($accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date); |
|
| 193 | 193 | } |
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | // Save the values for next time. |
| 199 | - if ( ! empty( $next_time ) ) { |
|
| 200 | - update_option( $task, $next_time ); |
|
| 201 | - } else { |
|
| 202 | - delete_option( $task ); |
|
| 203 | - $this->deactivate( $task ); |
|
| 199 | + if (!empty($next_time)) { |
|
| 200 | + update_option($task, $next_time); |
|
| 201 | + }else { |
|
| 202 | + delete_option($task); |
|
| 203 | + $this->deactivate($task); |
|
| 204 | 204 | } |
| 205 | - } else { |
|
| 206 | - $this->deactivate( $task ); |
|
| 207 | - update_option( 'lsx_wetu_nexttime', $task ); |
|
| 205 | + }else { |
|
| 206 | + $this->deactivate($task); |
|
| 207 | + update_option('lsx_wetu_nexttime', $task); |
|
| 208 | 208 | } |
| 209 | 209 | } |
| 210 | 210 | |
@@ -214,14 +214,14 @@ discard block |
||
| 214 | 214 | * @param string $task |
| 215 | 215 | * @return void |
| 216 | 216 | */ |
| 217 | - public function load_items_to_sync( $task = 'accommodation_images' ) { |
|
| 217 | + public function load_items_to_sync($task = 'accommodation_images') { |
|
| 218 | 218 | $args = array( |
| 219 | 219 | 'post_status' => 'publish', |
| 220 | 220 | 'posts_per_page' => -1, |
| 221 | 221 | 'nopagin' => true, |
| 222 | 222 | 'fields' => 'ids', |
| 223 | 223 | ); |
| 224 | - switch ( $task ) { |
|
| 224 | + switch ($task) { |
|
| 225 | 225 | case 'accommodation_images': |
| 226 | 226 | $args['post_type'] = 'accommodation'; |
| 227 | 227 | break; |
@@ -229,9 +229,9 @@ discard block |
||
| 229 | 229 | default: |
| 230 | 230 | break; |
| 231 | 231 | } |
| 232 | - $items = new \WP_Query( $args ); |
|
| 233 | - if ( $items->have_posts() ) { |
|
| 234 | - update_option( 'lsx_wetu_' . $task . '_sync', $items->posts ); |
|
| 232 | + $items = new \WP_Query($args); |
|
| 233 | + if ($items->have_posts()) { |
|
| 234 | + update_option('lsx_wetu_'.$task.'_sync', $items->posts); |
|
| 235 | 235 | } |
| 236 | 236 | } |
| 237 | 237 | |
@@ -244,11 +244,11 @@ discard block |
||
| 244 | 244 | $api_key = false; |
| 245 | 245 | $options = lsx_wetu_get_options(); |
| 246 | 246 | |
| 247 | - if ( ! defined( 'WETU_API_KEY' ) ) { |
|
| 248 | - if ( isset( $options['api_key'] ) && '' !== $options['api_key'] ) { |
|
| 247 | + if (!defined('WETU_API_KEY')) { |
|
| 248 | + if (isset($options['api_key']) && '' !== $options['api_key']) { |
|
| 249 | 249 | $api_key = $options['api_key']; |
| 250 | 250 | } |
| 251 | - } else { |
|
| 251 | + }else { |
|
| 252 | 252 | $api_key = WETU_API_KEY; |
| 253 | 253 | } |
| 254 | 254 | return $api_key; |