Passed
Push — add/image-automation ( 57094b...b4ff43 )
by Warwick
03:09
created

Cron::sync_accommodation_images()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
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_action( 'lsx_wetu_importer_settings_before', array( $this, 'watch_for_trigger' ), 200 );
37
		add_action( 'lsx_wetu_accommodation_images_cron', array( $this, 'process' ), 10, 1 );
38
	}
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...
39
40
	/**
41
	 * Return an instance of this class.
42
	 *
43
	 * @since 1.0.0
44
	 *
45
	 * @return    object Cron()    A single instance of this class.
46
	 */
47
	public static function get_instance() {
48
		// If the single instance hasn't been set, set it now.
49
		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...
50
			self::$instance = new self();
51
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
52
		return self::$instance;
53
	}
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...
54
55
	/**
56
	 * Watches for changes in the button triggers.
57
	 *
58
	 * @return void
59
	 */
60
	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...
61
62
		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...
63
			$options = lsx_wetu_get_options();
64
65
			// Check what state the option is in.
66
			$accommodation_cron = 'deactivate';
67
			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...
68
				$accommodation_cron = 'activate';
69
			}
70
71
			// Check what state the cron is in.
72
			$schedule = false;
73
			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...
74
				$schedule = true;
75
			}
76
77
			// If activate and its not running.
78
			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...
79
				$schedule = 'daily';
80
				$this->schedule( 'lsx_wetu_accommodation_images_cron', $schedule );
81
			} 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...
82
				$this->deactivate();
83
			}
84
		}
85
	}
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...
86
87
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$task" missing
Loading history...
88
	 * Remove our cron from the shedule.
89
	 *
90
	 * @return void
91
	 */
92
	public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) {
93
		wp_clear_scheduled_hook( $task );
94
	}
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...
95
96
	/**
97
	 * This function will schedule the cron event.
98
	 *
99
	 * @param string $task
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
100
	 * @param string $schedule
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
101
	 * @param string $time
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
102
	 * @return void
103
	 */
104
	public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'daily', $time = '' ) {
105
		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...
106
			$time = time();
107
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
108
		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

108
		wp_schedule_event( /** @scrutinizer ignore-type */ $time, $schedule, $task, array( $task ) );
Loading history...
109
	}
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...
110
111
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$task" missing
Loading history...
112
	 * This is the function that will be triggered by the cron event.
113
	 *
114
	 * @return void
115
	 */
116
	public function process( $task = '' ) {
117
		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...
118
			case 'lsx_wetu_accommodation_images_cron':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
119
120
				break;
121
122
			default:
123
				break;
124
		}
125
	}
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...
126
127
	/**
128
	 * This is the function that will be triggered by the cron event.
129
	 *
130
	 * @return void
131
	 */
132
	public function sync_accommodation_images() {
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':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
135
				
136
				break;
137
138
			default:
139
				break;
140
		}
141
	}
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...
142
}
143
Cron::get_instance();
144