Passed
Push — master ( 9b072c...3e62f5 )
by Warwick
03:45 queued 11s
created

Cron::metaboxes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 27
rs 9.8333
1
<?php
2
/**
3
 * The main plugin class.
4
 *
5
 * @package   LSX_WETU_Importer
6
 * @author    LightSpeed
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2016 LightSpeed
10
 */
11
12
namespace lsx\wetu_importer\classes;
13
14
/**
15
 * The Main plugin class.
16
 */
17
class Cron {
18
19
	/**
20
	 * Holds class instance
21
	 *
22
	 * @since 1.0.0
23
	 *
24
	 * @var      object|Module_Template
0 ignored issues
show
Bug introduced by
The type lsx\wetu_importer\classes\Module_Template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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() {
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
36
		add_filter( 'cron_schedules', array( $this, 'register_schedule' ), 10, 1 );
0 ignored issues
show
introduced by
Scheduling crons at 300 sec ( less than 15 minutes ) is discouraged.
Loading history...
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
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
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 ) {
0 ignored issues
show
introduced by
The condition null === self::instance is always false.
Loading history...
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
53
			self::$instance = new self();
54
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
55
		return self::$instance;
56
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
57
58
	/**
59
	 * Define the metabox and field configurations.
60
	 *
61
	 * @param  array $meta_boxes
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
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(
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Block comment text must start on a new line
Loading history...
71
			'id'   => 'wetu_skip_banner',
72
			'name' => esc_html__( 'Skip Banner Image', 'lsx-banners' ),
73
			'type' => 'checkbox',
74
		);*/
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' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'title'" and double arrow, but found 2.
Loading history...
84
			'pages'  => $allowed_post_types,
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'pages'" and double arrow, but found 2.
Loading history...
85
			'fields' => $fields,
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 3 space(s) between "'fields'" and double arrow, but found 1.
Loading history...
86
			'context'    => 'side',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 2 space(s) between "'context'" and double arrow, but found 4.
Loading history...
87
			'priority'   => 'low',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 1 space(s) between "'priority'" and double arrow, but found 3.
Loading history...
88
		);
89
90
		return $meta_boxes;
91
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
92
93
	/**
94
	 * Registers a 5 min schedule for us to use.
95
	 *
96
	 * @param  array $schedules
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
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
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
	/**
108
	 * Watches for changes in the button triggers.
109
	 *
110
	 * @return void
111
	 */
112
	public function watch_for_trigger() {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
113
114
		if ( isset( $_GET['page'] ) && 'lsx-wetu-importer' === $_GET['page'] && isset( $_GET['tab'] ) && 'settings' === $_GET['tab'] ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
introduced by
Processing form data without nonce verification.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
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'] ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
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' ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
126
				$scheduled = true;
127
			}
128
129
			// If activate and its not running.
130
			if ( false === $scheduled && 'activate' === $accommodation_cron ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
131
				$schedule = 'weekly';
132
				$this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule );
133
			} elseif ( true === $scheduled && 'deactivate' === $accommodation_cron ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
134
				$this->deactivate();
135
			}
136
		}
137
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
138
139
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$task" missing
Loading history...
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
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
	/**
149
	 * This function will schedule the cron event.
150
	 *
151
	 * @param string $task
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
152
	 * @param string $schedule
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
153
	 * @param string $time
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
159
			$time = time();
160
		}
161
162
		if ( isset( $_GET['accommodation_images_cron_featured'] ) && '' !== $_GET['accommodation_images_cron_featured'] ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
introduced by
Processing form data without nonce verification.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
163
			$args[] = 'featured_image';
164
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
165
		wp_schedule_event( $time, $schedule, $task, $args );
0 ignored issues
show
Bug introduced by
It seems like $time can also be of type string; however, parameter $timestamp of wp_schedule_event() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
		wp_schedule_event( /** @scrutinizer ignore-type */ $time, $schedule, $task, $args );
Loading history...
166
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
167
168
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$task" missing
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
175
			case 'lsx_wetu_accommodation_images_cron':
176
					$this->register_accommodation_images_sync();
177
				break;
178
179
			default:
180
				break;
181
		}
182
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
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' ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
192
			$this->load_items_to_sync( 'accommodation_images' );
193
			$this->schedule( 'lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time );
194
		}
195
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
196
197
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$task" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$featured_image" missing
Loading history...
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 ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
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'] ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
221
					$adata = json_decode( $accommodation_info['body'], true );
222
223
					if ( isset( $adata[0] ) && isset( $adata[0]['last_modified'] ) && '' !== $adata[0]['last_modified'] ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
224
						$modified_time = strtotime( $adata[0]['last_modified'] );
225
						if ( $modified_time > $last_date ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
226
							$accommodation_importer = new \LSX_WETU_Importer_Accommodation();
227
228
							if ( false !== $featured_image && '' !== $featured_image ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
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 ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
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
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
251
252
	/**
253
	 * This will grab the accommodation ids and load them up into an option field.
254
	 *
255
	 * @param  string $task
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
266
			case 'accommodation_images':
267
					$args['post_type'] = 'accommodation';
268
				break;
269
270
			default:
271
				break;
272
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
273
		$items = new \WP_Query( $args );
274
		if ( $items->have_posts() ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
275
			update_option( 'lsx_wetu_' . $task . '_sync', $items->posts );
276
		}
277
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
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' ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
289
			if ( isset( $options['api_key'] ) && '' !== $options['api_key'] ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
290
				$api_key = $options['api_key'];
291
			}
292
		} else {
293
			$api_key = WETU_API_KEY;
0 ignored issues
show
Bug introduced by
The constant lsx\wetu_importer\classes\WETU_API_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
294
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
295
		return $api_key;
296
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
297
}
298
Cron::get_instance();
299