@@ -16,283 +16,283 @@ |
||
16 | 16 | */ |
17 | 17 | class Cron { |
18 | 18 | |
19 | - /** |
|
20 | - * Holds class instance |
|
21 | - * |
|
22 | - * @since 1.0.0 |
|
23 | - * |
|
24 | - * @var object|Module_Template |
|
25 | - */ |
|
26 | - protected static $instance = null; |
|
27 | - |
|
28 | - /** |
|
29 | - * Initialize the plugin by setting localization, filters, and administration functions. |
|
30 | - * |
|
31 | - * @since 1.0.0 |
|
32 | - * |
|
33 | - * @access private |
|
34 | - */ |
|
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, 'metaboxes' ) ); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Return an instance of this class. |
|
45 | - * |
|
46 | - * @since 1.0.0 |
|
47 | - * |
|
48 | - * @return object Cron() A single instance of this class. |
|
49 | - */ |
|
50 | - public static function get_instance() { |
|
51 | - // If the single instance hasn't been set, set it now. |
|
52 | - if ( null === self::$instance ) { |
|
53 | - self::$instance = new self(); |
|
54 | - } |
|
55 | - return self::$instance; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Define the metabox and field configurations. |
|
60 | - * |
|
61 | - * @param array $meta_boxes |
|
62 | - * @return array |
|
63 | - */ |
|
64 | - public function metaboxes( array $meta_boxes ) { |
|
65 | - // Allowed post types. |
|
66 | - $allowed_post_types = array( 'accommodation' ); |
|
67 | - |
|
68 | - $fields = array(); |
|
69 | - |
|
70 | - /*$fields[] = array( |
|
19 | + /** |
|
20 | + * Holds class instance |
|
21 | + * |
|
22 | + * @since 1.0.0 |
|
23 | + * |
|
24 | + * @var object|Module_Template |
|
25 | + */ |
|
26 | + protected static $instance = null; |
|
27 | + |
|
28 | + /** |
|
29 | + * Initialize the plugin by setting localization, filters, and administration functions. |
|
30 | + * |
|
31 | + * @since 1.0.0 |
|
32 | + * |
|
33 | + * @access private |
|
34 | + */ |
|
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, 'metaboxes' ) ); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Return an instance of this class. |
|
45 | + * |
|
46 | + * @since 1.0.0 |
|
47 | + * |
|
48 | + * @return object Cron() A single instance of this class. |
|
49 | + */ |
|
50 | + public static function get_instance() { |
|
51 | + // If the single instance hasn't been set, set it now. |
|
52 | + if ( null === self::$instance ) { |
|
53 | + self::$instance = new self(); |
|
54 | + } |
|
55 | + return self::$instance; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Define the metabox and field configurations. |
|
60 | + * |
|
61 | + * @param array $meta_boxes |
|
62 | + * @return array |
|
63 | + */ |
|
64 | + public function metaboxes( array $meta_boxes ) { |
|
65 | + // Allowed post types. |
|
66 | + $allowed_post_types = array( 'accommodation' ); |
|
67 | + |
|
68 | + $fields = array(); |
|
69 | + |
|
70 | + /*$fields[] = array( |
|
71 | 71 | 'id' => 'wetu_skip_banner', |
72 | 72 | 'name' => esc_html__( 'Skip Banner Image', 'lsx-banners' ), |
73 | 73 | 'type' => 'checkbox', |
74 | 74 | );*/ |
75 | 75 | |
76 | - $fields[] = array( |
|
77 | - 'id' => 'wetu_skip_featured', |
|
78 | - 'name' => esc_html__( 'Skip Featured Image', 'lsx-banners' ), |
|
79 | - 'type' => 'checkbox', |
|
80 | - ); |
|
81 | - |
|
82 | - $meta_boxes[] = array( |
|
83 | - 'title' => esc_html__( 'WETU Settings', 'lsx-banners' ), |
|
84 | - 'pages' => $allowed_post_types, |
|
85 | - 'fields' => $fields, |
|
86 | - 'context' => 'side', |
|
87 | - 'priority' => 'low', |
|
88 | - ); |
|
89 | - |
|
90 | - return $meta_boxes; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Registers a 5 min schedule for us to use. |
|
95 | - * |
|
96 | - * @param array $schedules |
|
97 | - * @return array |
|
98 | - */ |
|
99 | - public function register_schedule( $schedules ) { |
|
100 | - $schedules['wetu-5-minutes'] = array( |
|
101 | - 'interval' => 5 * MINUTE_IN_SECONDS, |
|
102 | - 'display' => __( 'Every 5 minutes', 'lsx-wetu-importer' ), |
|
103 | - ); |
|
104 | - return $schedules; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Watches for changes in the button triggers. |
|
109 | - * |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function watch_for_trigger() { |
|
113 | - |
|
114 | - if ( isset( $_GET['page'] ) && 'lsx-wetu-importer' === $_GET['page'] && isset( $_GET['tab'] ) && 'settings' === $_GET['tab'] ) { |
|
115 | - $options = lsx_wetu_get_options(); |
|
116 | - |
|
117 | - // Check what state the option is in. |
|
118 | - $accommodation_cron = 'deactivate'; |
|
119 | - if ( isset( $options['accommodation_images_cron'] ) && '' !== $options['accommodation_images_cron'] ) { |
|
120 | - $accommodation_cron = 'activate'; |
|
121 | - } |
|
122 | - |
|
123 | - // Check what state the cron is in. |
|
124 | - $scheduled = false; |
|
125 | - if ( wp_next_scheduled( 'lsx_wetu_accommodation_images_cron' ) ) { |
|
126 | - $scheduled = true; |
|
127 | - } |
|
128 | - |
|
129 | - // If activate and its not running. |
|
130 | - if ( false === $scheduled && 'activate' === $accommodation_cron ) { |
|
131 | - $schedule = 'weekly'; |
|
132 | - $this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule ); |
|
133 | - } elseif ( true === $scheduled && 'deactivate' === $accommodation_cron ) { |
|
134 | - $this->deactivate(); |
|
135 | - } |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Remove our cron from the shedule. |
|
141 | - * |
|
142 | - * @return void |
|
143 | - */ |
|
144 | - public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) { |
|
145 | - wp_clear_scheduled_hook( $task, array( $task ) ); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * This function will schedule the cron event. |
|
150 | - * |
|
151 | - * @param string $task |
|
152 | - * @param string $schedule |
|
153 | - * @param string $time |
|
154 | - * @return void |
|
155 | - */ |
|
156 | - public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm' ) { |
|
157 | - $args = array( $task ); |
|
158 | - if ( '' === $time ) { |
|
159 | - $time = time(); |
|
160 | - } |
|
161 | - |
|
162 | - if ( isset( $_GET['accommodation_images_cron_featured'] ) && '' !== $_GET['accommodation_images_cron_featured'] ) { |
|
163 | - $args[] = 'featured_image'; |
|
164 | - } |
|
165 | - wp_schedule_event( $time, $schedule, $task, $args ); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * This is the function that will be triggered by the cron event. |
|
170 | - * |
|
171 | - * @return void |
|
172 | - */ |
|
173 | - public function process( $task = '' ) { |
|
174 | - switch ( $task ) { |
|
175 | - case 'lsx_wetu_accommodation_images_cron': |
|
176 | - $this->register_accommodation_images_sync(); |
|
177 | - break; |
|
178 | - |
|
179 | - default: |
|
180 | - break; |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * This is the function that will be triggered by the cron event. |
|
186 | - * |
|
187 | - * @return void |
|
188 | - */ |
|
189 | - public function register_accommodation_images_sync() { |
|
190 | - $time = strtotime( '+5 min' ); |
|
191 | - if ( ! wp_next_scheduled( 'lsx_wetu_accommodation_images_sync' ) ) { |
|
192 | - $this->load_items_to_sync( 'accommodation_images' ); |
|
193 | - $this->schedule( 'lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time ); |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * This is the function that will be triggered by the cron event. |
|
199 | - * |
|
200 | - * @return void |
|
201 | - */ |
|
202 | - public function cron_callback( $task = '', $featured_image = '' ) { |
|
203 | - $has_accommodation = get_option( $task ); |
|
204 | - if ( false !== $has_accommodation && ! empty( $has_accommodation ) ) { |
|
205 | - $next_time = array_slice( $has_accommodation, 3 ); |
|
206 | - $this_time = array_slice( $has_accommodation, 0, 2 ); |
|
207 | - |
|
208 | - $api_key = $this->get_api_key(); |
|
209 | - $url = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids='; |
|
210 | - |
|
211 | - // Run through the current items. |
|
212 | - foreach ( $this_time as $accommodation ) { |
|
213 | - $wetu_id = get_post_meta( $accommodation, 'lsx_wetu_id', true ); |
|
214 | - $last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true ); |
|
215 | - |
|
216 | - // Grabbing the image sync. |
|
217 | - $featured_image = get_post_meta( $accommodation, 'wetu_skip_featured', true ); |
|
218 | - |
|
219 | - $accommodation_info = wp_remote_get( $url . $wetu_id ); |
|
220 | - if ( ! empty( $accommodation_info ) && isset( $accommodation_info['response'] ) && isset( $accommodation_info['response']['code'] ) && 200 === $accommodation_info['response']['code'] ) { |
|
221 | - $adata = json_decode( $accommodation_info['body'], true ); |
|
222 | - |
|
223 | - if ( isset( $adata[0] ) && isset( $adata[0]['last_modified'] ) && '' !== $adata[0]['last_modified'] ) { |
|
224 | - $modified_time = strtotime( $adata[0]['last_modified'] ); |
|
225 | - if ( $modified_time > $last_date ) { |
|
226 | - $accommodation_importer = new \LSX_WETU_Importer_Accommodation(); |
|
227 | - |
|
228 | - if ( false !== $featured_image && '' !== $featured_image ) { |
|
229 | - $accommodation_importer->set_featured_image( $adata, $accommodation ); |
|
230 | - } |
|
231 | - |
|
232 | - $accommodation_importer->create_main_gallery( $adata, $accommodation ); |
|
233 | - update_post_meta( $accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date ); |
|
234 | - } |
|
235 | - } |
|
236 | - } |
|
237 | - } |
|
238 | - |
|
239 | - // Save the values for next time. |
|
240 | - if ( ! empty( $next_time ) ) { |
|
241 | - update_option( $task, $next_time ); |
|
242 | - } else { |
|
243 | - delete_option( $task ); |
|
244 | - $this->deactivate( $task ); |
|
245 | - } |
|
246 | - } else { |
|
247 | - $this->deactivate( $task ); |
|
248 | - update_option( 'lsx_wetu_nexttime', $task ); |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * This will grab the accommodation ids and load them up into an option field. |
|
254 | - * |
|
255 | - * @param string $task |
|
256 | - * @return void |
|
257 | - */ |
|
258 | - public function load_items_to_sync( $task = 'accommodation_images' ) { |
|
259 | - $args = array( |
|
260 | - 'post_status' => 'publish', |
|
261 | - 'posts_per_page' => -1, |
|
262 | - 'nopagin' => true, |
|
263 | - 'fields' => 'ids', |
|
264 | - ); |
|
265 | - switch ( $task ) { |
|
266 | - case 'accommodation_images': |
|
267 | - $args['post_type'] = 'accommodation'; |
|
268 | - break; |
|
269 | - |
|
270 | - default: |
|
271 | - break; |
|
272 | - } |
|
273 | - $items = new \WP_Query( $args ); |
|
274 | - if ( $items->have_posts() ) { |
|
275 | - update_option( 'lsx_wetu_' . $task . '_sync', $items->posts ); |
|
276 | - } |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Gets the API key stored in the options table. |
|
281 | - * |
|
282 | - * @return string |
|
283 | - */ |
|
284 | - public function get_api_key() { |
|
285 | - $api_key = false; |
|
286 | - $options = lsx_wetu_get_options(); |
|
287 | - |
|
288 | - if ( ! defined( 'WETU_API_KEY' ) ) { |
|
289 | - if ( isset( $options['api_key'] ) && '' !== $options['api_key'] ) { |
|
290 | - $api_key = $options['api_key']; |
|
291 | - } |
|
292 | - } else { |
|
293 | - $api_key = WETU_API_KEY; |
|
294 | - } |
|
295 | - return $api_key; |
|
296 | - } |
|
76 | + $fields[] = array( |
|
77 | + 'id' => 'wetu_skip_featured', |
|
78 | + 'name' => esc_html__( 'Skip Featured Image', 'lsx-banners' ), |
|
79 | + 'type' => 'checkbox', |
|
80 | + ); |
|
81 | + |
|
82 | + $meta_boxes[] = array( |
|
83 | + 'title' => esc_html__( 'WETU Settings', 'lsx-banners' ), |
|
84 | + 'pages' => $allowed_post_types, |
|
85 | + 'fields' => $fields, |
|
86 | + 'context' => 'side', |
|
87 | + 'priority' => 'low', |
|
88 | + ); |
|
89 | + |
|
90 | + return $meta_boxes; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Registers a 5 min schedule for us to use. |
|
95 | + * |
|
96 | + * @param array $schedules |
|
97 | + * @return array |
|
98 | + */ |
|
99 | + public function register_schedule( $schedules ) { |
|
100 | + $schedules['wetu-5-minutes'] = array( |
|
101 | + 'interval' => 5 * MINUTE_IN_SECONDS, |
|
102 | + 'display' => __( 'Every 5 minutes', 'lsx-wetu-importer' ), |
|
103 | + ); |
|
104 | + return $schedules; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Watches for changes in the button triggers. |
|
109 | + * |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function watch_for_trigger() { |
|
113 | + |
|
114 | + if ( isset( $_GET['page'] ) && 'lsx-wetu-importer' === $_GET['page'] && isset( $_GET['tab'] ) && 'settings' === $_GET['tab'] ) { |
|
115 | + $options = lsx_wetu_get_options(); |
|
116 | + |
|
117 | + // Check what state the option is in. |
|
118 | + $accommodation_cron = 'deactivate'; |
|
119 | + if ( isset( $options['accommodation_images_cron'] ) && '' !== $options['accommodation_images_cron'] ) { |
|
120 | + $accommodation_cron = 'activate'; |
|
121 | + } |
|
122 | + |
|
123 | + // Check what state the cron is in. |
|
124 | + $scheduled = false; |
|
125 | + if ( wp_next_scheduled( 'lsx_wetu_accommodation_images_cron' ) ) { |
|
126 | + $scheduled = true; |
|
127 | + } |
|
128 | + |
|
129 | + // If activate and its not running. |
|
130 | + if ( false === $scheduled && 'activate' === $accommodation_cron ) { |
|
131 | + $schedule = 'weekly'; |
|
132 | + $this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule ); |
|
133 | + } elseif ( true === $scheduled && 'deactivate' === $accommodation_cron ) { |
|
134 | + $this->deactivate(); |
|
135 | + } |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Remove our cron from the shedule. |
|
141 | + * |
|
142 | + * @return void |
|
143 | + */ |
|
144 | + public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) { |
|
145 | + wp_clear_scheduled_hook( $task, array( $task ) ); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * This function will schedule the cron event. |
|
150 | + * |
|
151 | + * @param string $task |
|
152 | + * @param string $schedule |
|
153 | + * @param string $time |
|
154 | + * @return void |
|
155 | + */ |
|
156 | + public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm' ) { |
|
157 | + $args = array( $task ); |
|
158 | + if ( '' === $time ) { |
|
159 | + $time = time(); |
|
160 | + } |
|
161 | + |
|
162 | + if ( isset( $_GET['accommodation_images_cron_featured'] ) && '' !== $_GET['accommodation_images_cron_featured'] ) { |
|
163 | + $args[] = 'featured_image'; |
|
164 | + } |
|
165 | + wp_schedule_event( $time, $schedule, $task, $args ); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * This is the function that will be triggered by the cron event. |
|
170 | + * |
|
171 | + * @return void |
|
172 | + */ |
|
173 | + public function process( $task = '' ) { |
|
174 | + switch ( $task ) { |
|
175 | + case 'lsx_wetu_accommodation_images_cron': |
|
176 | + $this->register_accommodation_images_sync(); |
|
177 | + break; |
|
178 | + |
|
179 | + default: |
|
180 | + break; |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * This is the function that will be triggered by the cron event. |
|
186 | + * |
|
187 | + * @return void |
|
188 | + */ |
|
189 | + public function register_accommodation_images_sync() { |
|
190 | + $time = strtotime( '+5 min' ); |
|
191 | + if ( ! wp_next_scheduled( 'lsx_wetu_accommodation_images_sync' ) ) { |
|
192 | + $this->load_items_to_sync( 'accommodation_images' ); |
|
193 | + $this->schedule( 'lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time ); |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * This is the function that will be triggered by the cron event. |
|
199 | + * |
|
200 | + * @return void |
|
201 | + */ |
|
202 | + public function cron_callback( $task = '', $featured_image = '' ) { |
|
203 | + $has_accommodation = get_option( $task ); |
|
204 | + if ( false !== $has_accommodation && ! empty( $has_accommodation ) ) { |
|
205 | + $next_time = array_slice( $has_accommodation, 3 ); |
|
206 | + $this_time = array_slice( $has_accommodation, 0, 2 ); |
|
207 | + |
|
208 | + $api_key = $this->get_api_key(); |
|
209 | + $url = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids='; |
|
210 | + |
|
211 | + // Run through the current items. |
|
212 | + foreach ( $this_time as $accommodation ) { |
|
213 | + $wetu_id = get_post_meta( $accommodation, 'lsx_wetu_id', true ); |
|
214 | + $last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true ); |
|
215 | + |
|
216 | + // Grabbing the image sync. |
|
217 | + $featured_image = get_post_meta( $accommodation, 'wetu_skip_featured', true ); |
|
218 | + |
|
219 | + $accommodation_info = wp_remote_get( $url . $wetu_id ); |
|
220 | + if ( ! empty( $accommodation_info ) && isset( $accommodation_info['response'] ) && isset( $accommodation_info['response']['code'] ) && 200 === $accommodation_info['response']['code'] ) { |
|
221 | + $adata = json_decode( $accommodation_info['body'], true ); |
|
222 | + |
|
223 | + if ( isset( $adata[0] ) && isset( $adata[0]['last_modified'] ) && '' !== $adata[0]['last_modified'] ) { |
|
224 | + $modified_time = strtotime( $adata[0]['last_modified'] ); |
|
225 | + if ( $modified_time > $last_date ) { |
|
226 | + $accommodation_importer = new \LSX_WETU_Importer_Accommodation(); |
|
227 | + |
|
228 | + if ( false !== $featured_image && '' !== $featured_image ) { |
|
229 | + $accommodation_importer->set_featured_image( $adata, $accommodation ); |
|
230 | + } |
|
231 | + |
|
232 | + $accommodation_importer->create_main_gallery( $adata, $accommodation ); |
|
233 | + update_post_meta( $accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date ); |
|
234 | + } |
|
235 | + } |
|
236 | + } |
|
237 | + } |
|
238 | + |
|
239 | + // Save the values for next time. |
|
240 | + if ( ! empty( $next_time ) ) { |
|
241 | + update_option( $task, $next_time ); |
|
242 | + } else { |
|
243 | + delete_option( $task ); |
|
244 | + $this->deactivate( $task ); |
|
245 | + } |
|
246 | + } else { |
|
247 | + $this->deactivate( $task ); |
|
248 | + update_option( 'lsx_wetu_nexttime', $task ); |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * This will grab the accommodation ids and load them up into an option field. |
|
254 | + * |
|
255 | + * @param string $task |
|
256 | + * @return void |
|
257 | + */ |
|
258 | + public function load_items_to_sync( $task = 'accommodation_images' ) { |
|
259 | + $args = array( |
|
260 | + 'post_status' => 'publish', |
|
261 | + 'posts_per_page' => -1, |
|
262 | + 'nopagin' => true, |
|
263 | + 'fields' => 'ids', |
|
264 | + ); |
|
265 | + switch ( $task ) { |
|
266 | + case 'accommodation_images': |
|
267 | + $args['post_type'] = 'accommodation'; |
|
268 | + break; |
|
269 | + |
|
270 | + default: |
|
271 | + break; |
|
272 | + } |
|
273 | + $items = new \WP_Query( $args ); |
|
274 | + if ( $items->have_posts() ) { |
|
275 | + update_option( 'lsx_wetu_' . $task . '_sync', $items->posts ); |
|
276 | + } |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Gets the API key stored in the options table. |
|
281 | + * |
|
282 | + * @return string |
|
283 | + */ |
|
284 | + public function get_api_key() { |
|
285 | + $api_key = false; |
|
286 | + $options = lsx_wetu_get_options(); |
|
287 | + |
|
288 | + if ( ! defined( 'WETU_API_KEY' ) ) { |
|
289 | + if ( isset( $options['api_key'] ) && '' !== $options['api_key'] ) { |
|
290 | + $api_key = $options['api_key']; |
|
291 | + } |
|
292 | + } else { |
|
293 | + $api_key = WETU_API_KEY; |
|
294 | + } |
|
295 | + return $api_key; |
|
296 | + } |
|
297 | 297 | } |
298 | 298 | Cron::get_instance(); |
@@ -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, 'metaboxes' ) ); |
|
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, 'metaboxes')); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -49,7 +49,7 @@ 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; |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | * @param array $meta_boxes |
62 | 62 | * @return array |
63 | 63 | */ |
64 | - public function metaboxes( array $meta_boxes ) { |
|
64 | + public function metaboxes(array $meta_boxes) { |
|
65 | 65 | // Allowed post types. |
66 | - $allowed_post_types = array( 'accommodation' ); |
|
66 | + $allowed_post_types = array('accommodation'); |
|
67 | 67 | |
68 | 68 | $fields = array(); |
69 | 69 | |
@@ -75,12 +75,12 @@ discard block |
||
75 | 75 | |
76 | 76 | $fields[] = array( |
77 | 77 | 'id' => 'wetu_skip_featured', |
78 | - 'name' => esc_html__( 'Skip Featured Image', 'lsx-banners' ), |
|
78 | + 'name' => esc_html__('Skip Featured Image', 'lsx-banners'), |
|
79 | 79 | 'type' => 'checkbox', |
80 | 80 | ); |
81 | 81 | |
82 | 82 | $meta_boxes[] = array( |
83 | - 'title' => esc_html__( 'WETU Settings', 'lsx-banners' ), |
|
83 | + 'title' => esc_html__('WETU Settings', 'lsx-banners'), |
|
84 | 84 | 'pages' => $allowed_post_types, |
85 | 85 | 'fields' => $fields, |
86 | 86 | 'context' => 'side', |
@@ -96,10 +96,10 @@ discard block |
||
96 | 96 | * @param array $schedules |
97 | 97 | * @return array |
98 | 98 | */ |
99 | - public function register_schedule( $schedules ) { |
|
99 | + public function register_schedule($schedules) { |
|
100 | 100 | $schedules['wetu-5-minutes'] = array( |
101 | 101 | 'interval' => 5 * MINUTE_IN_SECONDS, |
102 | - 'display' => __( 'Every 5 minutes', 'lsx-wetu-importer' ), |
|
102 | + 'display' => __('Every 5 minutes', 'lsx-wetu-importer'), |
|
103 | 103 | ); |
104 | 104 | return $schedules; |
105 | 105 | } |
@@ -111,26 +111,26 @@ discard block |
||
111 | 111 | */ |
112 | 112 | public function watch_for_trigger() { |
113 | 113 | |
114 | - if ( isset( $_GET['page'] ) && 'lsx-wetu-importer' === $_GET['page'] && isset( $_GET['tab'] ) && 'settings' === $_GET['tab'] ) { |
|
114 | + if (isset($_GET['page']) && 'lsx-wetu-importer' === $_GET['page'] && isset($_GET['tab']) && 'settings' === $_GET['tab']) { |
|
115 | 115 | $options = lsx_wetu_get_options(); |
116 | 116 | |
117 | 117 | // Check what state the option is in. |
118 | 118 | $accommodation_cron = 'deactivate'; |
119 | - if ( isset( $options['accommodation_images_cron'] ) && '' !== $options['accommodation_images_cron'] ) { |
|
119 | + if (isset($options['accommodation_images_cron']) && '' !== $options['accommodation_images_cron']) { |
|
120 | 120 | $accommodation_cron = 'activate'; |
121 | 121 | } |
122 | 122 | |
123 | 123 | // Check what state the cron is in. |
124 | 124 | $scheduled = false; |
125 | - if ( wp_next_scheduled( 'lsx_wetu_accommodation_images_cron' ) ) { |
|
125 | + if (wp_next_scheduled('lsx_wetu_accommodation_images_cron')) { |
|
126 | 126 | $scheduled = true; |
127 | 127 | } |
128 | 128 | |
129 | 129 | // If activate and its not running. |
130 | - if ( false === $scheduled && 'activate' === $accommodation_cron ) { |
|
130 | + if (false === $scheduled && 'activate' === $accommodation_cron) { |
|
131 | 131 | $schedule = 'weekly'; |
132 | - $this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule ); |
|
133 | - } elseif ( true === $scheduled && 'deactivate' === $accommodation_cron ) { |
|
132 | + $this->schedule('lsx_wetu_accommodation_images_cron', $schedule); |
|
133 | + } elseif (true === $scheduled && 'deactivate' === $accommodation_cron) { |
|
134 | 134 | $this->deactivate(); |
135 | 135 | } |
136 | 136 | } |
@@ -141,8 +141,8 @@ discard block |
||
141 | 141 | * |
142 | 142 | * @return void |
143 | 143 | */ |
144 | - public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) { |
|
145 | - wp_clear_scheduled_hook( $task, array( $task ) ); |
|
144 | + public function deactivate($task = 'lsx_wetu_accommodation_images_cron') { |
|
145 | + wp_clear_scheduled_hook($task, array($task)); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
@@ -153,16 +153,16 @@ discard block |
||
153 | 153 | * @param string $time |
154 | 154 | * @return void |
155 | 155 | */ |
156 | - public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm' ) { |
|
157 | - $args = array( $task ); |
|
158 | - if ( '' === $time ) { |
|
156 | + public function schedule($task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm') { |
|
157 | + $args = array($task); |
|
158 | + if ('' === $time) { |
|
159 | 159 | $time = time(); |
160 | 160 | } |
161 | 161 | |
162 | - if ( isset( $_GET['accommodation_images_cron_featured'] ) && '' !== $_GET['accommodation_images_cron_featured'] ) { |
|
162 | + if (isset($_GET['accommodation_images_cron_featured']) && '' !== $_GET['accommodation_images_cron_featured']) { |
|
163 | 163 | $args[] = 'featured_image'; |
164 | 164 | } |
165 | - wp_schedule_event( $time, $schedule, $task, $args ); |
|
165 | + wp_schedule_event($time, $schedule, $task, $args); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | /** |
@@ -170,8 +170,8 @@ discard block |
||
170 | 170 | * |
171 | 171 | * @return void |
172 | 172 | */ |
173 | - public function process( $task = '' ) { |
|
174 | - switch ( $task ) { |
|
173 | + public function process($task = '') { |
|
174 | + switch ($task) { |
|
175 | 175 | case 'lsx_wetu_accommodation_images_cron': |
176 | 176 | $this->register_accommodation_images_sync(); |
177 | 177 | break; |
@@ -187,10 +187,10 @@ discard block |
||
187 | 187 | * @return void |
188 | 188 | */ |
189 | 189 | public function register_accommodation_images_sync() { |
190 | - $time = strtotime( '+5 min' ); |
|
191 | - if ( ! wp_next_scheduled( 'lsx_wetu_accommodation_images_sync' ) ) { |
|
192 | - $this->load_items_to_sync( 'accommodation_images' ); |
|
193 | - $this->schedule( 'lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time ); |
|
190 | + $time = strtotime('+5 min'); |
|
191 | + if (!wp_next_scheduled('lsx_wetu_accommodation_images_sync')) { |
|
192 | + $this->load_items_to_sync('accommodation_images'); |
|
193 | + $this->schedule('lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time); |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | |
@@ -199,53 +199,53 @@ discard block |
||
199 | 199 | * |
200 | 200 | * @return void |
201 | 201 | */ |
202 | - public function cron_callback( $task = '', $featured_image = '' ) { |
|
203 | - $has_accommodation = get_option( $task ); |
|
204 | - if ( false !== $has_accommodation && ! empty( $has_accommodation ) ) { |
|
205 | - $next_time = array_slice( $has_accommodation, 3 ); |
|
206 | - $this_time = array_slice( $has_accommodation, 0, 2 ); |
|
202 | + public function cron_callback($task = '', $featured_image = '') { |
|
203 | + $has_accommodation = get_option($task); |
|
204 | + if (false !== $has_accommodation && !empty($has_accommodation)) { |
|
205 | + $next_time = array_slice($has_accommodation, 3); |
|
206 | + $this_time = array_slice($has_accommodation, 0, 2); |
|
207 | 207 | |
208 | 208 | $api_key = $this->get_api_key(); |
209 | - $url = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids='; |
|
209 | + $url = 'https://wetu.com/API/Pins/'.$api_key.'/Get?all=include&ids='; |
|
210 | 210 | |
211 | 211 | // Run through the current items. |
212 | - foreach ( $this_time as $accommodation ) { |
|
213 | - $wetu_id = get_post_meta( $accommodation, 'lsx_wetu_id', true ); |
|
214 | - $last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true ); |
|
212 | + foreach ($this_time as $accommodation) { |
|
213 | + $wetu_id = get_post_meta($accommodation, 'lsx_wetu_id', true); |
|
214 | + $last_date = get_post_meta($accommodation, 'lsx_wetu_modified_date', true); |
|
215 | 215 | |
216 | 216 | // Grabbing the image sync. |
217 | - $featured_image = get_post_meta( $accommodation, 'wetu_skip_featured', true ); |
|
217 | + $featured_image = get_post_meta($accommodation, 'wetu_skip_featured', true); |
|
218 | 218 | |
219 | - $accommodation_info = wp_remote_get( $url . $wetu_id ); |
|
220 | - if ( ! empty( $accommodation_info ) && isset( $accommodation_info['response'] ) && isset( $accommodation_info['response']['code'] ) && 200 === $accommodation_info['response']['code'] ) { |
|
221 | - $adata = json_decode( $accommodation_info['body'], true ); |
|
219 | + $accommodation_info = wp_remote_get($url.$wetu_id); |
|
220 | + if (!empty($accommodation_info) && isset($accommodation_info['response']) && isset($accommodation_info['response']['code']) && 200 === $accommodation_info['response']['code']) { |
|
221 | + $adata = json_decode($accommodation_info['body'], true); |
|
222 | 222 | |
223 | - if ( isset( $adata[0] ) && isset( $adata[0]['last_modified'] ) && '' !== $adata[0]['last_modified'] ) { |
|
224 | - $modified_time = strtotime( $adata[0]['last_modified'] ); |
|
225 | - if ( $modified_time > $last_date ) { |
|
223 | + if (isset($adata[0]) && isset($adata[0]['last_modified']) && '' !== $adata[0]['last_modified']) { |
|
224 | + $modified_time = strtotime($adata[0]['last_modified']); |
|
225 | + if ($modified_time > $last_date) { |
|
226 | 226 | $accommodation_importer = new \LSX_WETU_Importer_Accommodation(); |
227 | 227 | |
228 | - if ( false !== $featured_image && '' !== $featured_image ) { |
|
229 | - $accommodation_importer->set_featured_image( $adata, $accommodation ); |
|
228 | + if (false !== $featured_image && '' !== $featured_image) { |
|
229 | + $accommodation_importer->set_featured_image($adata, $accommodation); |
|
230 | 230 | } |
231 | 231 | |
232 | - $accommodation_importer->create_main_gallery( $adata, $accommodation ); |
|
233 | - update_post_meta( $accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date ); |
|
232 | + $accommodation_importer->create_main_gallery($adata, $accommodation); |
|
233 | + update_post_meta($accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date); |
|
234 | 234 | } |
235 | 235 | } |
236 | 236 | } |
237 | 237 | } |
238 | 238 | |
239 | 239 | // Save the values for next time. |
240 | - if ( ! empty( $next_time ) ) { |
|
241 | - update_option( $task, $next_time ); |
|
242 | - } else { |
|
243 | - delete_option( $task ); |
|
244 | - $this->deactivate( $task ); |
|
240 | + if (!empty($next_time)) { |
|
241 | + update_option($task, $next_time); |
|
242 | + }else { |
|
243 | + delete_option($task); |
|
244 | + $this->deactivate($task); |
|
245 | 245 | } |
246 | - } else { |
|
247 | - $this->deactivate( $task ); |
|
248 | - update_option( 'lsx_wetu_nexttime', $task ); |
|
246 | + }else { |
|
247 | + $this->deactivate($task); |
|
248 | + update_option('lsx_wetu_nexttime', $task); |
|
249 | 249 | } |
250 | 250 | } |
251 | 251 | |
@@ -255,14 +255,14 @@ discard block |
||
255 | 255 | * @param string $task |
256 | 256 | * @return void |
257 | 257 | */ |
258 | - public function load_items_to_sync( $task = 'accommodation_images' ) { |
|
258 | + public function load_items_to_sync($task = 'accommodation_images') { |
|
259 | 259 | $args = array( |
260 | 260 | 'post_status' => 'publish', |
261 | 261 | 'posts_per_page' => -1, |
262 | 262 | 'nopagin' => true, |
263 | 263 | 'fields' => 'ids', |
264 | 264 | ); |
265 | - switch ( $task ) { |
|
265 | + switch ($task) { |
|
266 | 266 | case 'accommodation_images': |
267 | 267 | $args['post_type'] = 'accommodation'; |
268 | 268 | break; |
@@ -270,9 +270,9 @@ discard block |
||
270 | 270 | default: |
271 | 271 | break; |
272 | 272 | } |
273 | - $items = new \WP_Query( $args ); |
|
274 | - if ( $items->have_posts() ) { |
|
275 | - update_option( 'lsx_wetu_' . $task . '_sync', $items->posts ); |
|
273 | + $items = new \WP_Query($args); |
|
274 | + if ($items->have_posts()) { |
|
275 | + update_option('lsx_wetu_'.$task.'_sync', $items->posts); |
|
276 | 276 | } |
277 | 277 | } |
278 | 278 | |
@@ -285,11 +285,11 @@ discard block |
||
285 | 285 | $api_key = false; |
286 | 286 | $options = lsx_wetu_get_options(); |
287 | 287 | |
288 | - if ( ! defined( 'WETU_API_KEY' ) ) { |
|
289 | - if ( isset( $options['api_key'] ) && '' !== $options['api_key'] ) { |
|
288 | + if (!defined('WETU_API_KEY')) { |
|
289 | + if (isset($options['api_key']) && '' !== $options['api_key']) { |
|
290 | 290 | $api_key = $options['api_key']; |
291 | 291 | } |
292 | - } else { |
|
292 | + }else { |
|
293 | 293 | $api_key = WETU_API_KEY; |
294 | 294 | } |
295 | 295 | return $api_key; |