Passed
Push — master ( 6f8b88...9b072c )
by Warwick
03:24 queued 12s
created

Cron::custom_image_metabox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 21
rs 9.9332
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, 'custom_image_metabox' ), 10, 1 );
41
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 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 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
57
58
	public function custom_image_metabox( &$meta_boxes ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function custom_image_metabox()
Loading history...
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
59
60
		$fields = array(
61
			/**
62
			 * Single Checkbox Field.
63
			 */
64
			array(
65
				'id'    => 'field-checkbox',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 3 space(s) between "'id'" and double arrow, but found 4.
Loading history...
66
				'name' => 'Checkbox field',
67
				'type' => 'checkbox',
68
			),
69
		);
70
		/**
71
		 * Metabox instantiation.
72
		 */
73
		$meta_boxes[] = array(
74
			'title' => __( 'WETU Settings', 'lsx-wetu-importer' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 2 space(s) between "'title'" and double arrow, but found 1.
Loading history...
75
			'pages' => 'accommodation',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 2 space(s) between "'pages'" and double arrow, but found 1.
Loading history...
76
			'fields' => $fields,
77
		);
78
		return $meta_boxes;
79
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
80
81
	/**
82
	 * Registers a 5 min schedule for us to use.
83
	 *
84
	 * @param  array $schedules
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
85
	 * @return array
86
	 */
87
	public function register_schedule( $schedules ) {
88
		$schedules['wetu-5-minutes'] = array(
89
			'interval' => 5 * MINUTE_IN_SECONDS,
90
			'display'  => __( 'Every 5 minutes', 'lsx-wetu-importer' ),
91
		);
92
		return $schedules;
93
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
94
95
	/**
96
	 * Watches for changes in the button triggers.
97
	 *
98
	 * @return void
99
	 */
100
	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...
101
102
		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...
103
			$options = lsx_wetu_get_options();
104
105
			// Check what state the option is in.
106
			$accommodation_cron = 'deactivate';
107
			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...
108
				$accommodation_cron = 'activate';
109
			}
110
111
			// Check what state the cron is in.
112
			$scheduled = false;
113
			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...
114
				$scheduled = true;
115
			}
116
117
			// If activate and its not running.
118
			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...
119
				$schedule = 'weekly';
120
				$this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule );
121
			} 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...
122
				$this->deactivate();
123
			}
124
		}
125
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
126
127
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$task" missing
Loading history...
128
	 * Remove our cron from the shedule.
129
	 *
130
	 * @return void
131
	 */
132
	public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) {
133
		wp_clear_scheduled_hook( $task, array( $task ) );
134
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
135
136
	/**
137
	 * This function will schedule the cron event.
138
	 *
139
	 * @param string $task
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
140
	 * @param string $schedule
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
141
	 * @param string $time
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
142
	 * @return void
143
	 */
144
	public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm' ) {
145
		$args = array( $task );
146
		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...
147
			$time = time();
148
		}
149
150
		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...
151
			$args[] = 'featured_image';
152
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
153
		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

153
		wp_schedule_event( /** @scrutinizer ignore-type */ $time, $schedule, $task, $args );
Loading history...
154
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
155
156
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$task" missing
Loading history...
157
	 * This is the function that will be triggered by the cron event.
158
	 *
159
	 * @return void
160
	 */
161
	public function process( $task = '' ) {
162
		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...
163
			case 'lsx_wetu_accommodation_images_cron':
164
					$this->register_accommodation_images_sync();
165
				break;
166
167
			default:
168
				break;
169
		}
170
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
171
172
	/**
173
	 * This is the function that will be triggered by the cron event.
174
	 *
175
	 * @return void
176
	 */
177
	public function register_accommodation_images_sync() {
178
		$time = strtotime( '+5 min' );
179
		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...
180
			$this->load_items_to_sync( 'accommodation_images' );
181
			$this->schedule( 'lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time );
182
		}
183
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
184
185
	/**
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...
186
	 * This is the function that will be triggered by the cron event.
187
	 *
188
	 * @return void
189
	 */
190
	public function cron_callback( $task = '', $featured_image = '' ) {
191
		$has_accommodation = get_option( $task );
192
		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...
193
			$next_time = array_slice( $has_accommodation, 5 );
194
			$this_time = array_slice( $has_accommodation, 0, 4 );
195
196
			$api_key = $this->get_api_key();
197
			$url     = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids=';
198
199
			// Run through the current items.
200
			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...
201
				$wetu_id   = get_post_meta( $accommodation, 'lsx_wetu_id', true );
202
				$last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true );
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'] ) {
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...
206
					$adata = json_decode( $accommodation_info['body'], true );
207
208
					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...
209
						$modified_time = strtotime( $adata[0]['last_modified'] );
210
						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...
211
							$accommodation_importer = new \LSX_WETU_Importer_Accommodation();
212
							$accommodation_importer->create_main_gallery( $adata, $accommodation );
213
							if ( '' !== $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...
214
								$accommodation_importer->set_featured_image( $adata, $accommodation );
215
							}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
216
							update_post_meta( $accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date );
217
						}
218
					}
219
				}
220
			}
221
222
			// Save the values for next time.
223
			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...
224
				update_option( $task, $next_time );
225
			} else {
226
				delete_option( $task );
227
				$this->deactivate( $task );
228
			}
229
		} else {
230
			$this->deactivate( $task );
231
			update_option( 'lsx_wetu_nexttime', $task );
232
		}
233
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
234
235
	/**
236
	 * This will grab the accommodation ids and load them up into an option field.
237
	 *
238
	 * @param  string $task
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
239
	 * @return void
240
	 */
241
	public function load_items_to_sync( $task = 'accommodation_images' ) {
242
		$args = array(
243
			'post_status'    => 'publish',
244
			'posts_per_page' => -1,
245
			'nopagin'        => true,
246
			'fields'         => 'ids',
247
		);
248
		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...
249
			case 'accommodation_images':
250
					$args['post_type'] = 'accommodation';
251
				break;
252
253
			default:
254
				break;
255
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
256
		$items = new \WP_Query( $args );
257
		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...
258
			update_option( 'lsx_wetu_' . $task . '_sync', $items->posts );
259
		}
260
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
261
262
	/**
263
	 * Gets the API key stored in the options table.
264
	 *
265
	 * @return string
266
	 */
267
	public function get_api_key() {
268
		$api_key = false;
269
		$options = lsx_wetu_get_options();
270
271
		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...
272
			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...
273
				$api_key = $options['api_key'];
274
			}
275
		} else {
276
			$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...
277
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
278
		return $api_key;
279
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
280
}
281
Cron::get_instance();
282