Passed
Push — add/image-automation ( f9204d...c29b91 )
by Warwick
03:05
created

Cron::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 4
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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
	}
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...
41
42
	/**
43
	 * Return an instance of this class.
44
	 *
45
	 * @since 1.0.0
46
	 *
47
	 * @return    object Cron()    A single instance of this class.
48
	 */
49
	public static function get_instance() {
50
		// If the single instance hasn't been set, set it now.
51
		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...
52
			self::$instance = new self();
53
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
54
		return self::$instance;
55
	}
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...
56
57
	/**
58
	 * Registers a 5 min schedule for us to use.
59
	 *
60
	 * @param  array $schedules
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
61
	 * @return array
62
	 */
63
	public function register_schedule( $schedules ) {
64
		$schedules['wetu-5-minutes'] = array(
65
			'interval' => 5 * MINUTE_IN_SECONDS,
66
			'display'  => __( 'Every 5 minutes', 'lsx-wetu-importer' ),
67
		);
68
		return $schedules;
69
	}
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...
70
71
	/**
72
	 * Watches for changes in the button triggers.
73
	 *
74
	 * @return void
75
	 */
76
	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...
77
78
		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...
79
			$options = lsx_wetu_get_options();
80
81
			// Check what state the option is in.
82
			$accommodation_cron = 'deactivate';
83
			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...
84
				$accommodation_cron = 'activate';
85
			}
86
87
			// Check what state the cron is in.
88
			$schedule = false;
89
			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...
90
				$schedule = true;
91
			}
92
93
			// If activate and its not running.
94
			if ( false === $schedule && '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...
95
				$schedule = 'daily';
96
				$this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule );
97
			} elseif ( true === $schedule && '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...
98
				$this->deactivate();
99
			}
100
		}
101
	}
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...
102
103
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$task" missing
Loading history...
104
	 * Remove our cron from the shedule.
105
	 *
106
	 * @return void
107
	 */
108
	public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) {
109
		wp_clear_scheduled_hook( $task, array( $task ) );
110
	}
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...
111
112
	/**
113
	 * This function will schedule the cron event.
114
	 *
115
	 * @param string $task
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
116
	 * @param string $schedule
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
117
	 * @param string $time
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
118
	 * @return void
119
	 */
120
	public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'daily', $time = '' ) {
121
		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...
122
			$time = time();
123
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
124
		wp_schedule_event( $time, $schedule, $task, array( $task ) );
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

124
		wp_schedule_event( /** @scrutinizer ignore-type */ $time, $schedule, $task, array( $task ) );
Loading history...
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
	 * This is the function that will be triggered by the cron event.
129
	 *
130
	 * @return void
131
	 */
132
	public function process( $task = '' ) {
133
		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...
134
			case 'lsx_wetu_accommodation_images_cron':
135
					$this->register_accommodation_images_sync();
136
				break;
137
138
			default:
139
				break;
140
		}
141
	}
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...
142
143
	/**
144
	 * This is the function that will be triggered by the cron event.
145
	 *
146
	 * @return void
147
	 */
148
	public function register_accommodation_images_sync() {
149
		$time = strtotime( '+1 min' );
150
		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...
151
			$this->load_items_to_sync( 'accommodation_images' );
152
			$this->schedule( 'lsx_wetu_accommodation_images_sync', 'wetu-5-minutes', $time );
153
		}
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 cron_callback( $task = '' ) {
162
		$has_accommodation = get_option( $task );
163
		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...
164
			$next_time = array_slice( $has_accommodation, 5 );
165
			$this_time = array_slice( $has_accommodation, 0, 4 );
166
167
			$api_key = $this->get_api_key();
168
			$url     = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids=';
169
170
			// Run through the current items.
171
			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...
172
				$wetu_id   = get_post_meta( $accommodation, 'lsx_wetu_id', true );
173
				$last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true );
174
175
				$accommodation_info = wp_remote_get( $url . $wetu_id );
176
				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...
177
					$adata = json_decode( $accommodation_info['body'], true );
178
179
					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...
180
						$modified_time = strtotime( $adata[0]['last_modified'] );
181
						if ( $modified_time > $last_date ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

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...
182
							/*print_r('<pre>');
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
183
							print_r( $wetu_id );
184
							print_r('</pre>');
185
							print_r('<pre>');
186
							print_r( $last_date );
187
							print_r('</pre>');
188
							print_r('<pre>');
189
							print_r( $modified_time );
190
							print_r('</pre>');
191
							die();*/
192
						}
193
					}
194
				}
195
			}
196
197
			// Save the values for next time.
198
			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...
199
				update_option( $task, $next_time );
200
			} else {
201
				delete_option( $task );
202
				$this->deactivate( $task );
203
			}
204
		} else {
205
			$this->deactivate( $task );
206
			update_option( 'lsx_wetu_nexttime', $task );
207
		}
208
	}
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...
209
210
	/**
211
	 * This will grab the accommodation ids and load them up into an option field.
212
	 *
213
	 * @param  string $task
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
214
	 * @return void
215
	 */
216
	public function load_items_to_sync( $task = 'accommodation_images' ) {
217
		$args = array(
218
			'post_status'    => 'publish',
219
			'posts_per_page' => -1,
220
			'nopagin'        => true,
221
			'fields'         => 'ids',
222
		);
223
		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...
224
			case 'accommodation_images':
225
					$args['post_type'] = 'accommodation';
226
				break;
227
228
			default:
229
				break;
230
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
231
		$items = new \WP_Query( $args );
232
		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...
233
			update_option( 'lsx_wetu_' . $task . '_sync', $items->posts );
234
		}
235
	}
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...
236
237
	/**
238
	 * Gets the API key stored in the options table.
239
	 *
240
	 * @return string
241
	 */
242
	public function get_api_key() {
243
		$api_key = false;
244
		$options = lsx_wetu_get_options();
245
246
		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...
247
			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...
248
				$api_key = $options['api_key'];
249
			}
250
		} else {
251
			$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...
252
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
253
		return $api_key;
254
	}
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...
255
}
256
Cron::get_instance();
257