@@ -33,11 +33,11 @@ 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 ); |
|
| 40 | - add_filter( 'cmb_meta_boxes', array( $this, 'custom_image_metabox' ), 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 | + add_filter('cmb_meta_boxes', array($this, 'custom_image_metabox'), 10, 1); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -49,13 +49,13 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public static function get_instance() { |
| 51 | 51 | // If the single instance hasn't been set, set it now. |
| 52 | - if ( null === self::$instance ) { |
|
| 52 | + if (null === self::$instance) { |
|
| 53 | 53 | self::$instance = new self(); |
| 54 | 54 | } |
| 55 | 55 | return self::$instance; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - public function custom_image_metabox( &$meta_boxes ) { |
|
| 58 | + public function custom_image_metabox(&$meta_boxes) { |
|
| 59 | 59 | |
| 60 | 60 | $fields = array( |
| 61 | 61 | /** |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | * Metabox instantiation. |
| 72 | 72 | */ |
| 73 | 73 | $meta_boxes[] = array( |
| 74 | - 'title' => __( 'WETU Settings', 'lsx-wetu-importer' ), |
|
| 74 | + 'title' => __('WETU Settings', 'lsx-wetu-importer'), |
|
| 75 | 75 | 'pages' => 'accommodation', |
| 76 | 76 | 'fields' => $fields, |
| 77 | 77 | ); |
@@ -84,10 +84,10 @@ discard block |
||
| 84 | 84 | * @param array $schedules |
| 85 | 85 | * @return array |
| 86 | 86 | */ |
| 87 | - public function register_schedule( $schedules ) { |
|
| 87 | + public function register_schedule($schedules) { |
|
| 88 | 88 | $schedules['wetu-5-minutes'] = array( |
| 89 | 89 | 'interval' => 5 * MINUTE_IN_SECONDS, |
| 90 | - 'display' => __( 'Every 5 minutes', 'lsx-wetu-importer' ), |
|
| 90 | + 'display' => __('Every 5 minutes', 'lsx-wetu-importer'), |
|
| 91 | 91 | ); |
| 92 | 92 | return $schedules; |
| 93 | 93 | } |
@@ -99,26 +99,26 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | public function watch_for_trigger() { |
| 101 | 101 | |
| 102 | - if ( isset( $_GET['page'] ) && 'lsx-wetu-importer' === $_GET['page'] && isset( $_GET['tab'] ) && 'settings' === $_GET['tab'] ) { |
|
| 102 | + if (isset($_GET['page']) && 'lsx-wetu-importer' === $_GET['page'] && isset($_GET['tab']) && 'settings' === $_GET['tab']) { |
|
| 103 | 103 | $options = lsx_wetu_get_options(); |
| 104 | 104 | |
| 105 | 105 | // Check what state the option is in. |
| 106 | 106 | $accommodation_cron = 'deactivate'; |
| 107 | - if ( isset( $options['accommodation_images_cron'] ) && '' !== $options['accommodation_images_cron'] ) { |
|
| 107 | + if (isset($options['accommodation_images_cron']) && '' !== $options['accommodation_images_cron']) { |
|
| 108 | 108 | $accommodation_cron = 'activate'; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | // Check what state the cron is in. |
| 112 | 112 | $scheduled = false; |
| 113 | - if ( wp_next_scheduled( 'lsx_wetu_accommodation_images_cron' ) ) { |
|
| 113 | + if (wp_next_scheduled('lsx_wetu_accommodation_images_cron')) { |
|
| 114 | 114 | $scheduled = true; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // If activate and its not running. |
| 118 | - if ( false === $scheduled && 'activate' === $accommodation_cron ) { |
|
| 118 | + if (false === $scheduled && 'activate' === $accommodation_cron) { |
|
| 119 | 119 | $schedule = 'weekly'; |
| 120 | - $this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule ); |
|
| 121 | - } elseif ( true === $scheduled && 'deactivate' === $accommodation_cron ) { |
|
| 120 | + $this->schedule('lsx_wetu_accommodation_images_cron', $schedule); |
|
| 121 | + } elseif (true === $scheduled && 'deactivate' === $accommodation_cron) { |
|
| 122 | 122 | $this->deactivate(); |
| 123 | 123 | } |
| 124 | 124 | } |
@@ -129,8 +129,8 @@ discard block |
||
| 129 | 129 | * |
| 130 | 130 | * @return void |
| 131 | 131 | */ |
| 132 | - public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) { |
|
| 133 | - wp_clear_scheduled_hook( $task, array( $task ) ); |
|
| 132 | + public function deactivate($task = 'lsx_wetu_accommodation_images_cron') { |
|
| 133 | + wp_clear_scheduled_hook($task, array($task)); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -141,16 +141,16 @@ discard block |
||
| 141 | 141 | * @param string $time |
| 142 | 142 | * @return void |
| 143 | 143 | */ |
| 144 | - public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm' ) { |
|
| 145 | - $args = array( $task ); |
|
| 146 | - if ( '' === $time ) { |
|
| 144 | + public function schedule($task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm') { |
|
| 145 | + $args = array($task); |
|
| 146 | + if ('' === $time) { |
|
| 147 | 147 | $time = time(); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - if ( isset( $_GET['accommodation_images_cron_featured'] ) && '' !== $_GET['accommodation_images_cron_featured'] ) { |
|
| 150 | + if (isset($_GET['accommodation_images_cron_featured']) && '' !== $_GET['accommodation_images_cron_featured']) { |
|
| 151 | 151 | $args[] = 'featured_image'; |
| 152 | 152 | } |
| 153 | - wp_schedule_event( $time, $schedule, $task, $args ); |
|
| 153 | + wp_schedule_event($time, $schedule, $task, $args); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
@@ -158,8 +158,8 @@ discard block |
||
| 158 | 158 | * |
| 159 | 159 | * @return void |
| 160 | 160 | */ |
| 161 | - public function process( $task = '' ) { |
|
| 162 | - switch ( $task ) { |
|
| 161 | + public function process($task = '') { |
|
| 162 | + switch ($task) { |
|
| 163 | 163 | case 'lsx_wetu_accommodation_images_cron': |
| 164 | 164 | $this->register_accommodation_images_sync(); |
| 165 | 165 | break; |
@@ -175,10 +175,10 @@ discard block |
||
| 175 | 175 | * @return void |
| 176 | 176 | */ |
| 177 | 177 | public function register_accommodation_images_sync() { |
| 178 | - $time = strtotime( '+5 min' ); |
|
| 179 | - if ( ! wp_next_scheduled( 'lsx_wetu_accommodation_images_sync' ) ) { |
|
| 180 | - $this->load_items_to_sync( 'accommodation_images' ); |
|
| 181 | - $this->schedule( 'lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time ); |
|
| 178 | + $time = strtotime('+5 min'); |
|
| 179 | + if (!wp_next_scheduled('lsx_wetu_accommodation_images_sync')) { |
|
| 180 | + $this->load_items_to_sync('accommodation_images'); |
|
| 181 | + $this->schedule('lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time); |
|
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | |
@@ -187,48 +187,48 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return void |
| 189 | 189 | */ |
| 190 | - public function cron_callback( $task = '', $featured_image = '' ) { |
|
| 191 | - $has_accommodation = get_option( $task ); |
|
| 192 | - if ( false !== $has_accommodation && ! empty( $has_accommodation ) ) { |
|
| 193 | - $next_time = array_slice( $has_accommodation, 5 ); |
|
| 194 | - $this_time = array_slice( $has_accommodation, 0, 4 ); |
|
| 190 | + public function cron_callback($task = '', $featured_image = '') { |
|
| 191 | + $has_accommodation = get_option($task); |
|
| 192 | + if (false !== $has_accommodation && !empty($has_accommodation)) { |
|
| 193 | + $next_time = array_slice($has_accommodation, 5); |
|
| 194 | + $this_time = array_slice($has_accommodation, 0, 4); |
|
| 195 | 195 | |
| 196 | 196 | $api_key = $this->get_api_key(); |
| 197 | - $url = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids='; |
|
| 197 | + $url = 'https://wetu.com/API/Pins/'.$api_key.'/Get?all=include&ids='; |
|
| 198 | 198 | |
| 199 | 199 | // Run through the current items. |
| 200 | - foreach ( $this_time as $accommodation ) { |
|
| 201 | - $wetu_id = get_post_meta( $accommodation, 'lsx_wetu_id', true ); |
|
| 202 | - $last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true ); |
|
| 200 | + foreach ($this_time as $accommodation) { |
|
| 201 | + $wetu_id = get_post_meta($accommodation, 'lsx_wetu_id', true); |
|
| 202 | + $last_date = get_post_meta($accommodation, 'lsx_wetu_modified_date', true); |
|
| 203 | 203 | |
| 204 | - $accommodation_info = wp_remote_get( $url . $wetu_id ); |
|
| 205 | - if ( ! empty( $accommodation_info ) && isset( $accommodation_info['response'] ) && isset( $accommodation_info['response']['code'] ) && 200 === $accommodation_info['response']['code'] ) { |
|
| 206 | - $adata = json_decode( $accommodation_info['body'], true ); |
|
| 204 | + $accommodation_info = wp_remote_get($url.$wetu_id); |
|
| 205 | + if (!empty($accommodation_info) && isset($accommodation_info['response']) && isset($accommodation_info['response']['code']) && 200 === $accommodation_info['response']['code']) { |
|
| 206 | + $adata = json_decode($accommodation_info['body'], true); |
|
| 207 | 207 | |
| 208 | - if ( isset( $adata[0] ) && isset( $adata[0]['last_modified'] ) && '' !== $adata[0]['last_modified'] ) { |
|
| 209 | - $modified_time = strtotime( $adata[0]['last_modified'] ); |
|
| 210 | - if ( $modified_time > $last_date ) { |
|
| 208 | + if (isset($adata[0]) && isset($adata[0]['last_modified']) && '' !== $adata[0]['last_modified']) { |
|
| 209 | + $modified_time = strtotime($adata[0]['last_modified']); |
|
| 210 | + if ($modified_time > $last_date) { |
|
| 211 | 211 | $accommodation_importer = new \LSX_WETU_Importer_Accommodation(); |
| 212 | - $accommodation_importer->create_main_gallery( $adata, $accommodation ); |
|
| 213 | - if ( '' !== $featured_image ) { |
|
| 214 | - $accommodation_importer->set_featured_image( $adata, $accommodation ); |
|
| 212 | + $accommodation_importer->create_main_gallery($adata, $accommodation); |
|
| 213 | + if ('' !== $featured_image) { |
|
| 214 | + $accommodation_importer->set_featured_image($adata, $accommodation); |
|
| 215 | 215 | } |
| 216 | - update_post_meta( $accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date ); |
|
| 216 | + update_post_meta($accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date); |
|
| 217 | 217 | } |
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | // Save the values for next time. |
| 223 | - if ( ! empty( $next_time ) ) { |
|
| 224 | - update_option( $task, $next_time ); |
|
| 225 | - } else { |
|
| 226 | - delete_option( $task ); |
|
| 227 | - $this->deactivate( $task ); |
|
| 223 | + if (!empty($next_time)) { |
|
| 224 | + update_option($task, $next_time); |
|
| 225 | + }else { |
|
| 226 | + delete_option($task); |
|
| 227 | + $this->deactivate($task); |
|
| 228 | 228 | } |
| 229 | - } else { |
|
| 230 | - $this->deactivate( $task ); |
|
| 231 | - update_option( 'lsx_wetu_nexttime', $task ); |
|
| 229 | + }else { |
|
| 230 | + $this->deactivate($task); |
|
| 231 | + update_option('lsx_wetu_nexttime', $task); |
|
| 232 | 232 | } |
| 233 | 233 | } |
| 234 | 234 | |
@@ -238,14 +238,14 @@ discard block |
||
| 238 | 238 | * @param string $task |
| 239 | 239 | * @return void |
| 240 | 240 | */ |
| 241 | - public function load_items_to_sync( $task = 'accommodation_images' ) { |
|
| 241 | + public function load_items_to_sync($task = 'accommodation_images') { |
|
| 242 | 242 | $args = array( |
| 243 | 243 | 'post_status' => 'publish', |
| 244 | 244 | 'posts_per_page' => -1, |
| 245 | 245 | 'nopagin' => true, |
| 246 | 246 | 'fields' => 'ids', |
| 247 | 247 | ); |
| 248 | - switch ( $task ) { |
|
| 248 | + switch ($task) { |
|
| 249 | 249 | case 'accommodation_images': |
| 250 | 250 | $args['post_type'] = 'accommodation'; |
| 251 | 251 | break; |
@@ -253,9 +253,9 @@ discard block |
||
| 253 | 253 | default: |
| 254 | 254 | break; |
| 255 | 255 | } |
| 256 | - $items = new \WP_Query( $args ); |
|
| 257 | - if ( $items->have_posts() ) { |
|
| 258 | - update_option( 'lsx_wetu_' . $task . '_sync', $items->posts ); |
|
| 256 | + $items = new \WP_Query($args); |
|
| 257 | + if ($items->have_posts()) { |
|
| 258 | + update_option('lsx_wetu_'.$task.'_sync', $items->posts); |
|
| 259 | 259 | } |
| 260 | 260 | } |
| 261 | 261 | |
@@ -268,11 +268,11 @@ discard block |
||
| 268 | 268 | $api_key = false; |
| 269 | 269 | $options = lsx_wetu_get_options(); |
| 270 | 270 | |
| 271 | - if ( ! defined( 'WETU_API_KEY' ) ) { |
|
| 272 | - if ( isset( $options['api_key'] ) && '' !== $options['api_key'] ) { |
|
| 271 | + if (!defined('WETU_API_KEY')) { |
|
| 272 | + if (isset($options['api_key']) && '' !== $options['api_key']) { |
|
| 273 | 273 | $api_key = $options['api_key']; |
| 274 | 274 | } |
| 275 | - } else { |
|
| 275 | + }else { |
|
| 276 | 276 | $api_key = WETU_API_KEY; |
| 277 | 277 | } |
| 278 | 278 | return $api_key; |