Completed
Push — develop ( 258a62...d62d7a )
by David
02:29
created
src/includes/class-wordlift-schema-url-property-service.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
 	 */
67 67
 	public function get_metabox_label() {
68 68
 
69
-		return __( 'Web Site(s)', 'wordlift' );
69
+		return __('Web Site(s)', 'wordlift');
70 70
 	}
71 71
 
72 72
 	/**
@@ -89,20 +89,20 @@  discard block
 block discarded – undo
89 89
 	 * @return array|NULL The schema:url value or NULL if not set.
90 90
 	 * @since 3.6.0
91 91
 	 */
92
-	public function get( $post_id ) {
92
+	public function get($post_id) {
93 93
 
94 94
 		// Get the schema:url values set in WP.
95
-		$values = get_post_meta( $post_id, self::META_KEY, false );
95
+		$values = get_post_meta($post_id, self::META_KEY, false);
96 96
 
97 97
 		// If the property has never been set, we set its default value the first
98 98
 		// time to <permalink>.
99
-		if ( empty( $values ) ) {
100
-			return array( '<permalink>' );
99
+		if (empty($values)) {
100
+			return array('<permalink>');
101 101
 		}
102 102
 
103 103
 		// If there's only one value and that value is empty, we return NULL, i.e.
104 104
 		// variable not set.
105
-		if ( 1 === count( $values ) && empty( $values[0] ) ) {
105
+		if (1 === count($values) && empty($values[0])) {
106 106
 			return null;
107 107
 		}
108 108
 
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
 	/**
114 114
 	 * {@inheritdoc}
115 115
 	 */
116
-	public function sanitize( $value ) {
116
+	public function sanitize($value) {
117 117
 
118 118
 		// TODO: check that it's an URL or that is <permalink>
119 119
 
@@ -133,26 +133,26 @@  discard block
 block discarded – undo
133 133
 	 * @return array|mixed|NULL|string
134 134
 	 * @since 3.6.0
135 135
 	 */
136
-	public function get_post_metadata( $value, $object_id, $meta_key, $single ) {
136
+	public function get_post_metadata($value, $object_id, $meta_key, $single) {
137 137
 
138 138
 		// It's not us, return the value.
139
-		if ( self::META_KEY !== $meta_key ) {
139
+		if (self::META_KEY !== $meta_key) {
140 140
 			return $value;
141 141
 		}
142 142
 
143 143
 		$this->remove_filter_get_post_metadata();
144 144
 
145
-		$new_value = $this->get( $object_id );
145
+		$new_value = $this->get($object_id);
146 146
 
147 147
 		$this->add_filter_get_post_metadata();
148 148
 
149 149
 		// If we must return a single value, but we don't have a value, then return an empty string.
150
-		if ( $single && ( $new_value === null || empty( $new_value ) ) ) {
150
+		if ($single && ($new_value === null || empty($new_value))) {
151 151
 			return '';
152 152
 		}
153 153
 
154 154
 		// If we have a value and we need to return it as single, return the first value.
155
-		if ( $single ) {
155
+		if ($single) {
156 156
 			return $new_value[0];
157 157
 		}
158 158
 
Please login to merge, or discard this patch.
Indentation   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -13,175 +13,175 @@
 block discarded – undo
13 13
  */
14 14
 class Wordlift_Schema_Url_Property_Service extends Wordlift_Property_Service {
15 15
 
16
-	/**
17
-	 * The meta key used to store data for this property. We don't use wl_url to
18
-	 * avoid potential confusion about other URLs.
19
-	 *
20
-	 * @since 3.6.0
21
-	 */
22
-	const META_KEY = 'wl_schema_url';
23
-
24
-	/**
25
-	 * {@inheritdoc}
26
-	 */
27
-	public function get_rdf_predicate() {
28
-
29
-		return 'http://schema.org/url';
30
-	}
31
-
32
-	/**
33
-	 * {@inheritdoc}
34
-	 */
35
-	public function get_rdf_data_type() {
36
-
37
-		return 'xsd:anyURI';
38
-	}
39
-
40
-	/**
41
-	 * {@inheritdoc}
42
-	 */
43
-	public function get_data_type() {
44
-
45
-		return Wordlift_Schema_Service::DATA_TYPE_URI;
46
-	}
47
-
48
-	/**
49
-	 * {@inheritdoc}
50
-	 */
51
-	public function get_cardinality() {
52
-
53
-		return INF;
54
-	}
55
-
56
-	/**
57
-	 * {@inheritdoc}
58
-	 */
59
-	public function get_metabox_class() {
60
-
61
-		return 'Wl_Metabox_Field';
62
-	}
63
-
64
-	/**
65
-	 * {@inheritdoc}
66
-	 */
67
-	public function get_metabox_label() {
68
-
69
-		return __( 'Web Site(s)', 'wordlift' );
70
-	}
71
-
72
-	/**
73
-	 * Create a Wordlift_Schema_Url_Property_Service instance.
74
-	 *
75
-	 * @since 3.6.0
76
-	 */
77
-	public function __construct() {
78
-		parent::__construct();
79
-
80
-		// Finally listen for metadata requests for this field.
81
-		$this->add_filter_get_post_metadata();
82
-	}
83
-
84
-	/**
85
-	 * Get the schema:url value for the specified post/entity.
86
-	 *
87
-	 * @param int $post_id The post id.
88
-	 *
89
-	 * @return array|NULL The schema:url value or NULL if not set.
90
-	 * @since 3.6.0
91
-	 */
92
-	public function get( $post_id ) {
93
-
94
-		// Get the schema:url values set in WP.
95
-		$values = get_post_meta( $post_id, self::META_KEY, false );
96
-
97
-		// If the property has never been set, we set its default value the first
98
-		// time to <permalink>.
99
-		if ( empty( $values ) ) {
100
-			return array( '<permalink>' );
101
-		}
102
-
103
-		// If there's only one value and that value is empty, we return NULL, i.e.
104
-		// variable not set.
105
-		if ( 1 === count( $values ) && empty( $values[0] ) ) {
106
-			return null;
107
-		}
108
-
109
-		// Finally return whatever values the editor set.
110
-		return $values;
111
-	}
112
-
113
-	/**
114
-	 * {@inheritdoc}
115
-	 */
116
-	public function sanitize( $value ) {
117
-
118
-		// TODO: check that it's an URL or that is <permalink>
119
-
120
-		return $value;
121
-	}
122
-
123
-	/**
124
-	 * Get direct calls to read this meta and alter the response according to our
125
-	 * own strategy, i.e. if a value has never been set for this meta, then return
126
-	 * <permalink>.
127
-	 *
128
-	 * @param mixed  $value The original value.
129
-	 * @param int    $object_id The post id.
130
-	 * @param string $meta_key The meta key. We expect wl_schema_url or we return straight the value.
131
-	 * @param bool   $single Whether to return a single value.
132
-	 *
133
-	 * @return array|mixed|NULL|string
134
-	 * @since 3.6.0
135
-	 */
136
-	public function get_post_metadata( $value, $object_id, $meta_key, $single ) {
137
-
138
-		// It's not us, return the value.
139
-		if ( self::META_KEY !== $meta_key ) {
140
-			return $value;
141
-		}
142
-
143
-		$this->remove_filter_get_post_metadata();
144
-
145
-		$new_value = $this->get( $object_id );
146
-
147
-		$this->add_filter_get_post_metadata();
148
-
149
-		// If we must return a single value, but we don't have a value, then return an empty string.
150
-		if ( $single && ( $new_value === null || empty( $new_value ) ) ) {
151
-			return '';
152
-		}
153
-
154
-		// If we have a value and we need to return it as single, return the first value.
155
-		if ( $single ) {
156
-			return $new_value[0];
157
-		}
158
-
159
-		// Otherwise return the array.
160
-		return $new_value;
161
-	}
162
-
163
-	private function add_filter_get_post_metadata() {
164
-
165
-		add_filter(
166
-			'get_post_metadata',
167
-			array(
168
-				$this,
169
-				'get_post_metadata',
170
-			),
171
-			10,
172
-			4
173
-		);
174
-	}
175
-
176
-	private function remove_filter_get_post_metadata() {
177
-
178
-		remove_filter(
179
-			'get_post_metadata',
180
-			array(
181
-				$this,
182
-				'get_post_metadata',
183
-			),
184
-			10
185
-		);
186
-	}
16
+    /**
17
+     * The meta key used to store data for this property. We don't use wl_url to
18
+     * avoid potential confusion about other URLs.
19
+     *
20
+     * @since 3.6.0
21
+     */
22
+    const META_KEY = 'wl_schema_url';
23
+
24
+    /**
25
+     * {@inheritdoc}
26
+     */
27
+    public function get_rdf_predicate() {
28
+
29
+        return 'http://schema.org/url';
30
+    }
31
+
32
+    /**
33
+     * {@inheritdoc}
34
+     */
35
+    public function get_rdf_data_type() {
36
+
37
+        return 'xsd:anyURI';
38
+    }
39
+
40
+    /**
41
+     * {@inheritdoc}
42
+     */
43
+    public function get_data_type() {
44
+
45
+        return Wordlift_Schema_Service::DATA_TYPE_URI;
46
+    }
47
+
48
+    /**
49
+     * {@inheritdoc}
50
+     */
51
+    public function get_cardinality() {
52
+
53
+        return INF;
54
+    }
55
+
56
+    /**
57
+     * {@inheritdoc}
58
+     */
59
+    public function get_metabox_class() {
60
+
61
+        return 'Wl_Metabox_Field';
62
+    }
63
+
64
+    /**
65
+     * {@inheritdoc}
66
+     */
67
+    public function get_metabox_label() {
68
+
69
+        return __( 'Web Site(s)', 'wordlift' );
70
+    }
71
+
72
+    /**
73
+     * Create a Wordlift_Schema_Url_Property_Service instance.
74
+     *
75
+     * @since 3.6.0
76
+     */
77
+    public function __construct() {
78
+        parent::__construct();
79
+
80
+        // Finally listen for metadata requests for this field.
81
+        $this->add_filter_get_post_metadata();
82
+    }
83
+
84
+    /**
85
+     * Get the schema:url value for the specified post/entity.
86
+     *
87
+     * @param int $post_id The post id.
88
+     *
89
+     * @return array|NULL The schema:url value or NULL if not set.
90
+     * @since 3.6.0
91
+     */
92
+    public function get( $post_id ) {
93
+
94
+        // Get the schema:url values set in WP.
95
+        $values = get_post_meta( $post_id, self::META_KEY, false );
96
+
97
+        // If the property has never been set, we set its default value the first
98
+        // time to <permalink>.
99
+        if ( empty( $values ) ) {
100
+            return array( '<permalink>' );
101
+        }
102
+
103
+        // If there's only one value and that value is empty, we return NULL, i.e.
104
+        // variable not set.
105
+        if ( 1 === count( $values ) && empty( $values[0] ) ) {
106
+            return null;
107
+        }
108
+
109
+        // Finally return whatever values the editor set.
110
+        return $values;
111
+    }
112
+
113
+    /**
114
+     * {@inheritdoc}
115
+     */
116
+    public function sanitize( $value ) {
117
+
118
+        // TODO: check that it's an URL or that is <permalink>
119
+
120
+        return $value;
121
+    }
122
+
123
+    /**
124
+     * Get direct calls to read this meta and alter the response according to our
125
+     * own strategy, i.e. if a value has never been set for this meta, then return
126
+     * <permalink>.
127
+     *
128
+     * @param mixed  $value The original value.
129
+     * @param int    $object_id The post id.
130
+     * @param string $meta_key The meta key. We expect wl_schema_url or we return straight the value.
131
+     * @param bool   $single Whether to return a single value.
132
+     *
133
+     * @return array|mixed|NULL|string
134
+     * @since 3.6.0
135
+     */
136
+    public function get_post_metadata( $value, $object_id, $meta_key, $single ) {
137
+
138
+        // It's not us, return the value.
139
+        if ( self::META_KEY !== $meta_key ) {
140
+            return $value;
141
+        }
142
+
143
+        $this->remove_filter_get_post_metadata();
144
+
145
+        $new_value = $this->get( $object_id );
146
+
147
+        $this->add_filter_get_post_metadata();
148
+
149
+        // If we must return a single value, but we don't have a value, then return an empty string.
150
+        if ( $single && ( $new_value === null || empty( $new_value ) ) ) {
151
+            return '';
152
+        }
153
+
154
+        // If we have a value and we need to return it as single, return the first value.
155
+        if ( $single ) {
156
+            return $new_value[0];
157
+        }
158
+
159
+        // Otherwise return the array.
160
+        return $new_value;
161
+    }
162
+
163
+    private function add_filter_get_post_metadata() {
164
+
165
+        add_filter(
166
+            'get_post_metadata',
167
+            array(
168
+                $this,
169
+                'get_post_metadata',
170
+            ),
171
+            10,
172
+            4
173
+        );
174
+    }
175
+
176
+    private function remove_filter_get_post_metadata() {
177
+
178
+        remove_filter(
179
+            'get_post_metadata',
180
+            array(
181
+                $this,
182
+                'get_post_metadata',
183
+            ),
184
+            10
185
+        );
186
+    }
187 187
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-batch-action.php 2 patches
Indentation   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -8,61 +8,61 @@
 block discarded – undo
8 8
 
9 9
 class Wordlift_Batch_Action {
10 10
 
11
-	public static function process( $post_type, $offset, $query, $callback ) {
11
+    public static function process( $post_type, $offset, $query, $callback ) {
12 12
 
13
-		$posts_per_page = 10;
13
+        $posts_per_page = 10;
14 14
 
15
-		$args = array_merge(
16
-			self::get_args( $post_type, $query ),
17
-			array(
18
-				'offset'         => $offset,
19
-				'posts_per_page' => $posts_per_page,
20
-			)
21
-		);
15
+        $args = array_merge(
16
+            self::get_args( $post_type, $query ),
17
+            array(
18
+                'offset'         => $offset,
19
+                'posts_per_page' => $posts_per_page,
20
+            )
21
+        );
22 22
 
23
-		$post_ids = get_posts( $args );
23
+        $post_ids = get_posts( $args );
24 24
 
25
-		foreach ( $post_ids as $post_id ) {
26
-			call_user_func( $callback, $post_id );
27
-		}
25
+        foreach ( $post_ids as $post_id ) {
26
+            call_user_func( $callback, $post_id );
27
+        }
28 28
 
29
-		return array(
30
-			'current' => $offset,
31
-			'next'    => $offset + $posts_per_page,
32
-			'count'   => self::count( $post_type, $query ),
33
-		);
34
-	}
29
+        return array(
30
+            'current' => $offset,
31
+            'next'    => $offset + $posts_per_page,
32
+            'count'   => self::count( $post_type, $query ),
33
+        );
34
+    }
35 35
 
36
-	public static function count( $post_type, $query ) {
37
-		$args = array_merge(
38
-			self::get_args( $post_type, $query ),
39
-			array(
40
-				'posts_per_page' => - 1,
41
-			)
42
-		);
36
+    public static function count( $post_type, $query ) {
37
+        $args = array_merge(
38
+            self::get_args( $post_type, $query ),
39
+            array(
40
+                'posts_per_page' => - 1,
41
+            )
42
+        );
43 43
 
44
-		return count( get_posts( $args ) );
45
-	}
44
+        return count( get_posts( $args ) );
45
+    }
46 46
 
47
-	private static function get_args( $post_type, $query ) {
47
+    private static function get_args( $post_type, $query ) {
48 48
 
49
-		return array_merge(
50
-			array(
51
-				'fields'        => 'ids',
52
-				'post_type'     => $post_type,
53
-				'post_status'   => array(
54
-					'publish',
55
-					'future',
56
-					'draft',
57
-					'pending',
58
-					'private',
59
-					'auto-draft',
60
-					'inherit',
61
-				),
62
-				'cache_results' => false,
63
-			),
64
-			$query
65
-		);
66
-	}
49
+        return array_merge(
50
+            array(
51
+                'fields'        => 'ids',
52
+                'post_type'     => $post_type,
53
+                'post_status'   => array(
54
+                    'publish',
55
+                    'future',
56
+                    'draft',
57
+                    'pending',
58
+                    'private',
59
+                    'auto-draft',
60
+                    'inherit',
61
+                ),
62
+                'cache_results' => false,
63
+            ),
64
+            $query
65
+        );
66
+    }
67 67
 
68 68
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -8,43 +8,43 @@
 block discarded – undo
8 8
 
9 9
 class Wordlift_Batch_Action {
10 10
 
11
-	public static function process( $post_type, $offset, $query, $callback ) {
11
+	public static function process($post_type, $offset, $query, $callback) {
12 12
 
13 13
 		$posts_per_page = 10;
14 14
 
15 15
 		$args = array_merge(
16
-			self::get_args( $post_type, $query ),
16
+			self::get_args($post_type, $query),
17 17
 			array(
18 18
 				'offset'         => $offset,
19 19
 				'posts_per_page' => $posts_per_page,
20 20
 			)
21 21
 		);
22 22
 
23
-		$post_ids = get_posts( $args );
23
+		$post_ids = get_posts($args);
24 24
 
25
-		foreach ( $post_ids as $post_id ) {
26
-			call_user_func( $callback, $post_id );
25
+		foreach ($post_ids as $post_id) {
26
+			call_user_func($callback, $post_id);
27 27
 		}
28 28
 
29 29
 		return array(
30 30
 			'current' => $offset,
31 31
 			'next'    => $offset + $posts_per_page,
32
-			'count'   => self::count( $post_type, $query ),
32
+			'count'   => self::count($post_type, $query),
33 33
 		);
34 34
 	}
35 35
 
36
-	public static function count( $post_type, $query ) {
36
+	public static function count($post_type, $query) {
37 37
 		$args = array_merge(
38
-			self::get_args( $post_type, $query ),
38
+			self::get_args($post_type, $query),
39 39
 			array(
40
-				'posts_per_page' => - 1,
40
+				'posts_per_page' => -1,
41 41
 			)
42 42
 		);
43 43
 
44
-		return count( get_posts( $args ) );
44
+		return count(get_posts($args));
45 45
 	}
46 46
 
47
-	private static function get_args( $post_type, $query ) {
47
+	private static function get_args($post_type, $query) {
48 48
 
49 49
 		return array_merge(
50 50
 			array(
Please login to merge, or discard this patch.
src/includes/batch/class-wordlift-batch-operation-ajax-adapter.php 2 patches
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -48,21 +48,21 @@  discard block
 block discarded – undo
48 48
 	 * @param string                              $action The action name.
49 49
 	 * @param int                                 $access The access level.
50 50
 	 */
51
-	public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) {
51
+	public function __construct($operation, $action, $access = self::ACCESS_ADMIN) {
52 52
 
53 53
 		$this->operation = $operation;
54 54
 
55
-		if ( $access & self::ACCESS_ADMIN ) {
56
-			add_action( "wp_ajax_$action", array( $this, 'process' ) );
57
-			add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) );
55
+		if ($access & self::ACCESS_ADMIN) {
56
+			add_action("wp_ajax_$action", array($this, 'process'));
57
+			add_action("wp_ajax_{$action}_count", array($this, 'count'));
58 58
 
59 59
 			// Add the nonce for the `schemaorg_sync` action.
60
-			add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) );
60
+			add_filter('wl_admin_settings', array($this, 'add_nonce'));
61 61
 		}
62 62
 
63
-		if ( $access & self::ACCESS_ANONYMOUS ) {
64
-			add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) );
65
-			add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) );
63
+		if ($access & self::ACCESS_ANONYMOUS) {
64
+			add_action("wp_ajax_nopriv_$action", array($this, 'process'));
65
+			add_action("wp_ajax_nopriv_{$action}_count", array($this, 'count'));
66 66
 		}
67 67
 
68 68
 		$this->action = $action;
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
76 76
 	 * @return array The updated array of settings.
77 77
 	 * @since 3.20.0
78 78
 	 */
79
-	public function add_nonce( $params ) {
79
+	public function add_nonce($params) {
80 80
 
81 81
 		return array_merge(
82 82
 			$params,
@@ -92,17 +92,17 @@  discard block
 block discarded – undo
92 92
 	 * @since 3.20.0
93 93
 	 */
94 94
 	public function process() {
95
-		$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
95
+		$nonce = isset($_POST['_nonce']) ? sanitize_text_field(wp_unslash((string) $_POST['_nonce'])) : '';
96 96
 		// Validate the nonce.
97
-		if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
98
-			wp_send_json_error( 'Invalid nonce.' );
97
+		if ( ! wp_verify_nonce($nonce, $this->action)) {
98
+			wp_send_json_error('Invalid nonce.');
99 99
 		}
100 100
 
101
-		$offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0;
102
-		$limit  = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10;
101
+		$offset = isset($_POST['offset']) ? (int) $_POST['offset'] : 0;
102
+		$limit  = isset($_POST['limit']) ? (int) $_POST['limit'] : 10;
103 103
 
104 104
 		// Run the batch operation.
105
-		$result = $this->operation->process( $offset, $limit );
105
+		$result = $this->operation->process($offset, $limit);
106 106
 
107 107
 		// Send the results along with a potentially updated nonce.
108 108
 		wp_send_json_success(
@@ -124,9 +124,9 @@  discard block
 block discarded – undo
124 124
 	public function count() {
125 125
 
126 126
 		// Validate the nonce.
127
-		$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
128
-		if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
129
-			wp_send_json_error( 'Invalid nonce.' );
127
+		$nonce = isset($_POST['_nonce']) ? sanitize_text_field(wp_unslash((string) $_POST['_nonce'])) : '';
128
+		if ( ! wp_verify_nonce($nonce, $this->action)) {
129
+			wp_send_json_error('Invalid nonce.');
130 130
 		}
131 131
 
132 132
 		// Run the batch operation.
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
 	 */
151 151
 	public function create_nonce() {
152 152
 
153
-		return wp_create_nonce( $this->action );
153
+		return wp_create_nonce($this->action);
154 154
 	}
155 155
 
156 156
 }
Please login to merge, or discard this patch.
Indentation   +136 added lines, -136 removed lines patch added patch discarded remove patch
@@ -14,140 +14,140 @@
 block discarded – undo
14 14
  */
15 15
 class Wordlift_Batch_Operation_Ajax_Adapter {
16 16
 
17
-	/**
18
-	 * The access levels.
19
-	 *
20
-	 * @since 3.20.0
21
-	 */
22
-	const ACCESS_ANONYMOUS = 1;
23
-	const ACCESS_ADMIN     = 2;
24
-	const ACCESS_ALL       = 3;
25
-
26
-	/**
27
-	 * A {@link Wordlift_Batch_Operation_Interface} instance.
28
-	 *
29
-	 * @since 3.20.0
30
-	 * @access private
31
-	 * @var \Wordlift_Batch_Operation_Interface $operation A {@link Wordlift_Batch_Operation_Interface} instance.
32
-	 */
33
-	private $operation;
34
-
35
-	/**
36
-	 * The ajax action name.
37
-	 *
38
-	 * @since 3.20.0
39
-	 * @access private
40
-	 * @var string $action The ajax action name.
41
-	 */
42
-	private $action;
43
-
44
-	/**
45
-	 * Wordlift_Batch_Operation_Ajax_Adapter constructor.
46
-	 *
47
-	 * @param \Wordlift_Batch_Operation_Interface $operation The batch operation.
48
-	 * @param string                              $action The action name.
49
-	 * @param int                                 $access The access level.
50
-	 */
51
-	public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) {
52
-
53
-		$this->operation = $operation;
54
-
55
-		if ( $access & self::ACCESS_ADMIN ) {
56
-			add_action( "wp_ajax_$action", array( $this, 'process' ) );
57
-			add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) );
58
-
59
-			// Add the nonce for the `schemaorg_sync` action.
60
-			add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) );
61
-		}
62
-
63
-		if ( $access & self::ACCESS_ANONYMOUS ) {
64
-			add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) );
65
-			add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) );
66
-		}
67
-
68
-		$this->action = $action;
69
-	}
70
-
71
-	/**
72
-	 * Hook to `wl_admin_settings`, adds the nonce.
73
-	 *
74
-	 * @param array $params An array of settings.
75
-	 *
76
-	 * @return array The updated array of settings.
77
-	 * @since 3.20.0
78
-	 */
79
-	public function add_nonce( $params ) {
80
-
81
-		return array_merge(
82
-			$params,
83
-			array(
84
-				"{$this->action}_nonce" => $this->create_nonce(),
85
-			)
86
-		);
87
-	}
88
-
89
-	/**
90
-	 * Process the requested operation.
91
-	 *
92
-	 * @since 3.20.0
93
-	 */
94
-	public function process() {
95
-		$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
96
-		// Validate the nonce.
97
-		if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
98
-			wp_send_json_error( 'Invalid nonce.' );
99
-		}
100
-
101
-		$offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0;
102
-		$limit  = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10;
103
-
104
-		// Run the batch operation.
105
-		$result = $this->operation->process( $offset, $limit );
106
-
107
-		// Send the results along with a potentially updated nonce.
108
-		wp_send_json_success(
109
-			array_merge(
110
-				$result,
111
-				array(
112
-					'_nonce' => $this->create_nonce(),
113
-				)
114
-			)
115
-		);
116
-	}
117
-
118
-	/**
119
-	 * Count the number of elements that would be affected by the operation.
120
-	 *
121
-	 * @since 3.20.0
122
-	 */
123
-	public function count() {
124
-
125
-		// Validate the nonce.
126
-		$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
127
-		if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
128
-			wp_send_json_error( 'Invalid nonce.' );
129
-		}
130
-
131
-		// Run the batch operation.
132
-		$result = $this->operation->count();
133
-
134
-		// Send the results along with a potentially updated nonce.
135
-		wp_send_json_success(
136
-			array(
137
-				'count'  => $result,
138
-				'_nonce' => $this->create_nonce(),
139
-			)
140
-		);
141
-	}
142
-
143
-	/**
144
-	 * Create a nonce for the ajax operation.
145
-	 *
146
-	 * @return string The nonce.
147
-	 * @since 3.20.0
148
-	 */
149
-	public function create_nonce() {
150
-
151
-		return wp_create_nonce( $this->action );
152
-	}
17
+    /**
18
+     * The access levels.
19
+     *
20
+     * @since 3.20.0
21
+     */
22
+    const ACCESS_ANONYMOUS = 1;
23
+    const ACCESS_ADMIN     = 2;
24
+    const ACCESS_ALL       = 3;
25
+
26
+    /**
27
+     * A {@link Wordlift_Batch_Operation_Interface} instance.
28
+     *
29
+     * @since 3.20.0
30
+     * @access private
31
+     * @var \Wordlift_Batch_Operation_Interface $operation A {@link Wordlift_Batch_Operation_Interface} instance.
32
+     */
33
+    private $operation;
34
+
35
+    /**
36
+     * The ajax action name.
37
+     *
38
+     * @since 3.20.0
39
+     * @access private
40
+     * @var string $action The ajax action name.
41
+     */
42
+    private $action;
43
+
44
+    /**
45
+     * Wordlift_Batch_Operation_Ajax_Adapter constructor.
46
+     *
47
+     * @param \Wordlift_Batch_Operation_Interface $operation The batch operation.
48
+     * @param string                              $action The action name.
49
+     * @param int                                 $access The access level.
50
+     */
51
+    public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) {
52
+
53
+        $this->operation = $operation;
54
+
55
+        if ( $access & self::ACCESS_ADMIN ) {
56
+            add_action( "wp_ajax_$action", array( $this, 'process' ) );
57
+            add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) );
58
+
59
+            // Add the nonce for the `schemaorg_sync` action.
60
+            add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) );
61
+        }
62
+
63
+        if ( $access & self::ACCESS_ANONYMOUS ) {
64
+            add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) );
65
+            add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) );
66
+        }
67
+
68
+        $this->action = $action;
69
+    }
70
+
71
+    /**
72
+     * Hook to `wl_admin_settings`, adds the nonce.
73
+     *
74
+     * @param array $params An array of settings.
75
+     *
76
+     * @return array The updated array of settings.
77
+     * @since 3.20.0
78
+     */
79
+    public function add_nonce( $params ) {
80
+
81
+        return array_merge(
82
+            $params,
83
+            array(
84
+                "{$this->action}_nonce" => $this->create_nonce(),
85
+            )
86
+        );
87
+    }
88
+
89
+    /**
90
+     * Process the requested operation.
91
+     *
92
+     * @since 3.20.0
93
+     */
94
+    public function process() {
95
+        $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
96
+        // Validate the nonce.
97
+        if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
98
+            wp_send_json_error( 'Invalid nonce.' );
99
+        }
100
+
101
+        $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0;
102
+        $limit  = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10;
103
+
104
+        // Run the batch operation.
105
+        $result = $this->operation->process( $offset, $limit );
106
+
107
+        // Send the results along with a potentially updated nonce.
108
+        wp_send_json_success(
109
+            array_merge(
110
+                $result,
111
+                array(
112
+                    '_nonce' => $this->create_nonce(),
113
+                )
114
+            )
115
+        );
116
+    }
117
+
118
+    /**
119
+     * Count the number of elements that would be affected by the operation.
120
+     *
121
+     * @since 3.20.0
122
+     */
123
+    public function count() {
124
+
125
+        // Validate the nonce.
126
+        $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
127
+        if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
128
+            wp_send_json_error( 'Invalid nonce.' );
129
+        }
130
+
131
+        // Run the batch operation.
132
+        $result = $this->operation->count();
133
+
134
+        // Send the results along with a potentially updated nonce.
135
+        wp_send_json_success(
136
+            array(
137
+                'count'  => $result,
138
+                '_nonce' => $this->create_nonce(),
139
+            )
140
+        );
141
+    }
142
+
143
+    /**
144
+     * Create a nonce for the ajax operation.
145
+     *
146
+     * @return string The nonce.
147
+     * @since 3.20.0
148
+     */
149
+    public function create_nonce() {
150
+
151
+        return wp_create_nonce( $this->action );
152
+    }
153 153
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-schema-location-property-service.php 2 patches
Indentation   +82 added lines, -82 removed lines patch added patch discarded remove patch
@@ -13,87 +13,87 @@
 block discarded – undo
13 13
  */
14 14
 class Wordlift_Schema_Location_Property_Service extends Wordlift_Property_Service {
15 15
 
16
-	/**
17
-	 * The meta key used to store data for this property. We don't use wl_url to
18
-	 * avoid potential confusion about other URLs.
19
-	 *
20
-	 * @since 3.7.0
21
-	 */
22
-	const META_KEY = 'wl_location';
23
-
24
-	/**
25
-	 * {@inheritdoc}
26
-	 */
27
-	public function get_rdf_predicate() {
28
-
29
-		return 'http://schema.org/location';
30
-	}
31
-
32
-	/**
33
-	 * {@inheritdoc}
34
-	 */
35
-	public function get_rdf_data_type() {
36
-
37
-		return 'xsd:anyURI';
38
-	}
39
-
40
-	/**
41
-	 * {@inheritdoc}
42
-	 */
43
-	public function get_data_type() {
44
-
45
-		return Wordlift_Schema_Service::DATA_TYPE_URI;
46
-	}
47
-
48
-	/**
49
-	 * {@inheritdoc}
50
-	 */
51
-	public function get_cardinality() {
52
-
53
-		return INF;
54
-	}
55
-
56
-	/**
57
-	 * {@inheritdoc}
58
-	 */
59
-	public function get_metabox_class() {
60
-
61
-		return 'Wl_Metabox_Field';
62
-	}
63
-
64
-	/**
65
-	 * {@inheritdoc}
66
-	 */
67
-	public function get_metabox_label() {
68
-
69
-		return 'Location(s)';
70
-	}
71
-
72
-	/**
73
-	 * Get the schema:url value for the specified post/entity.
74
-	 *
75
-	 * @param int $post_id The post id.
76
-	 *
77
-	 * @return array|NULL The schema:url value or NULL if not set.
78
-	 * @since 3.7.0
79
-	 */
80
-	public function get( $post_id ) {
81
-
82
-		// Get the schema:url values set in WP.
83
-		$values = get_post_meta( $post_id, self::META_KEY, false );
84
-
85
-		// Finally return whatever values the editor set.
86
-		return $values;
87
-	}
88
-
89
-	/**
90
-	 * {@inheritdoc}
91
-	 */
92
-	public function sanitize( $value ) {
93
-
94
-		// TODO: check that it's an URL or that is <permalink>
95
-
96
-		return $value;
97
-	}
16
+    /**
17
+     * The meta key used to store data for this property. We don't use wl_url to
18
+     * avoid potential confusion about other URLs.
19
+     *
20
+     * @since 3.7.0
21
+     */
22
+    const META_KEY = 'wl_location';
23
+
24
+    /**
25
+     * {@inheritdoc}
26
+     */
27
+    public function get_rdf_predicate() {
28
+
29
+        return 'http://schema.org/location';
30
+    }
31
+
32
+    /**
33
+     * {@inheritdoc}
34
+     */
35
+    public function get_rdf_data_type() {
36
+
37
+        return 'xsd:anyURI';
38
+    }
39
+
40
+    /**
41
+     * {@inheritdoc}
42
+     */
43
+    public function get_data_type() {
44
+
45
+        return Wordlift_Schema_Service::DATA_TYPE_URI;
46
+    }
47
+
48
+    /**
49
+     * {@inheritdoc}
50
+     */
51
+    public function get_cardinality() {
52
+
53
+        return INF;
54
+    }
55
+
56
+    /**
57
+     * {@inheritdoc}
58
+     */
59
+    public function get_metabox_class() {
60
+
61
+        return 'Wl_Metabox_Field';
62
+    }
63
+
64
+    /**
65
+     * {@inheritdoc}
66
+     */
67
+    public function get_metabox_label() {
68
+
69
+        return 'Location(s)';
70
+    }
71
+
72
+    /**
73
+     * Get the schema:url value for the specified post/entity.
74
+     *
75
+     * @param int $post_id The post id.
76
+     *
77
+     * @return array|NULL The schema:url value or NULL if not set.
78
+     * @since 3.7.0
79
+     */
80
+    public function get( $post_id ) {
81
+
82
+        // Get the schema:url values set in WP.
83
+        $values = get_post_meta( $post_id, self::META_KEY, false );
84
+
85
+        // Finally return whatever values the editor set.
86
+        return $values;
87
+    }
88
+
89
+    /**
90
+     * {@inheritdoc}
91
+     */
92
+    public function sanitize( $value ) {
93
+
94
+        // TODO: check that it's an URL or that is <permalink>
95
+
96
+        return $value;
97
+    }
98 98
 
99 99
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -77,10 +77,10 @@  discard block
 block discarded – undo
77 77
 	 * @return array|NULL The schema:url value or NULL if not set.
78 78
 	 * @since 3.7.0
79 79
 	 */
80
-	public function get( $post_id ) {
80
+	public function get($post_id) {
81 81
 
82 82
 		// Get the schema:url values set in WP.
83
-		$values = get_post_meta( $post_id, self::META_KEY, false );
83
+		$values = get_post_meta($post_id, self::META_KEY, false);
84 84
 
85 85
 		// Finally return whatever values the editor set.
86 86
 		return $values;
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
 	/**
90 90
 	 * {@inheritdoc}
91 91
 	 */
92
-	public function sanitize( $value ) {
92
+	public function sanitize($value) {
93 93
 
94 94
 		// TODO: check that it's an URL or that is <permalink>
95 95
 
Please login to merge, or discard this patch.
src/includes/cache/intf-wordlift-cache-service.php 2 patches
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -17,52 +17,52 @@
 block discarded – undo
17 17
  */
18 18
 interface Wordlift_Cache_Service {
19 19
 
20
-	/**
21
-	 * Get the cached response for the specified `id`.
22
-	 *
23
-	 * @since 3.16.0
24
-	 *
25
-	 * @param string $id The cache `id`.
26
-	 *
27
-	 * @return mixed|false The cached contents or false if the cache isn't found.
28
-	 */
29
-	public function get_cache( $id );
20
+    /**
21
+     * Get the cached response for the specified `id`.
22
+     *
23
+     * @since 3.16.0
24
+     *
25
+     * @param string $id The cache `id`.
26
+     *
27
+     * @return mixed|false The cached contents or false if the cache isn't found.
28
+     */
29
+    public function get_cache( $id );
30 30
 
31
-	/**
32
-	 * Check whether we have cached results for the provided id.
33
-	 *
34
-	 * @since 3.16.3
35
-	 *
36
-	 * @param string $id The cache `id`.
37
-	 *
38
-	 * @return bool True if we have cached results otherwise false.
39
-	 */
40
-	public function has_cache( $id );
31
+    /**
32
+     * Check whether we have cached results for the provided id.
33
+     *
34
+     * @since 3.16.3
35
+     *
36
+     * @param string $id The cache `id`.
37
+     *
38
+     * @return bool True if we have cached results otherwise false.
39
+     */
40
+    public function has_cache( $id );
41 41
 
42
-	/**
43
-	 * Set the cache contents for the specified `id`.
44
-	 *
45
-	 * @since 3.16.0
46
-	 *
47
-	 * @param string $id       The cache id.
48
-	 * @param mixed  $contents The cache contents.
49
-	 */
50
-	public function set_cache( $id, $contents );
42
+    /**
43
+     * Set the cache contents for the specified `id`.
44
+     *
45
+     * @since 3.16.0
46
+     *
47
+     * @param string $id       The cache id.
48
+     * @param mixed  $contents The cache contents.
49
+     */
50
+    public function set_cache( $id, $contents );
51 51
 
52
-	/**
53
-	 * Delete the cache for the specified `id`.
54
-	 *
55
-	 * @since 3.16.0
56
-	 *
57
-	 * @param string $id The cache `id`.
58
-	 */
59
-	public function delete_cache( $id );
52
+    /**
53
+     * Delete the cache for the specified `id`.
54
+     *
55
+     * @since 3.16.0
56
+     *
57
+     * @param string $id The cache `id`.
58
+     */
59
+    public function delete_cache( $id );
60 60
 
61
-	/**
62
-	 * Flush the whole cache.
63
-	 *
64
-	 * @since 3.16.0
65
-	 */
66
-	public function flush();
61
+    /**
62
+     * Flush the whole cache.
63
+     *
64
+     * @since 3.16.0
65
+     */
66
+    public function flush();
67 67
 
68 68
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
 	 *
27 27
 	 * @return mixed|false The cached contents or false if the cache isn't found.
28 28
 	 */
29
-	public function get_cache( $id );
29
+	public function get_cache($id);
30 30
 
31 31
 	/**
32 32
 	 * Check whether we have cached results for the provided id.
@@ -37,7 +37,7 @@  discard block
 block discarded – undo
37 37
 	 *
38 38
 	 * @return bool True if we have cached results otherwise false.
39 39
 	 */
40
-	public function has_cache( $id );
40
+	public function has_cache($id);
41 41
 
42 42
 	/**
43 43
 	 * Set the cache contents for the specified `id`.
@@ -47,7 +47,7 @@  discard block
 block discarded – undo
47 47
 	 * @param string $id       The cache id.
48 48
 	 * @param mixed  $contents The cache contents.
49 49
 	 */
50
-	public function set_cache( $id, $contents );
50
+	public function set_cache($id, $contents);
51 51
 
52 52
 	/**
53 53
 	 * Delete the cache for the specified `id`.
@@ -56,7 +56,7 @@  discard block
 block discarded – undo
56 56
 	 *
57 57
 	 * @param string $id The cache `id`.
58 58
 	 */
59
-	public function delete_cache( $id );
59
+	public function delete_cache($id);
60 60
 
61 61
 	/**
62 62
 	 * Flush the whole cache.
Please login to merge, or discard this patch.
src/includes/cache/require.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@
 block discarded – undo
8 8
  * @package    Wordlift
9 9
  * @subpackage Wordlift/includes/cache
10 10
  */
11
-require_once plugin_dir_path( dirname( __DIR__ ) ) . 'includes/cache/intf-wordlift-cache-service.php';
12
-require_once plugin_dir_path( dirname( __DIR__ ) ) . 'includes/cache/class-wordlift-file-cache-service.php';
13
-require_once plugin_dir_path( dirname( __DIR__ ) ) . 'includes/cache/class-wordlift-cached-post-converter.php';
14
-require_once plugin_dir_path( dirname( __DIR__ ) ) . 'includes/cache/class-wordlift-cached-entity-uri-service.php';
11
+require_once plugin_dir_path(dirname(__DIR__)).'includes/cache/intf-wordlift-cache-service.php';
12
+require_once plugin_dir_path(dirname(__DIR__)).'includes/cache/class-wordlift-file-cache-service.php';
13
+require_once plugin_dir_path(dirname(__DIR__)).'includes/cache/class-wordlift-cached-post-converter.php';
14
+require_once plugin_dir_path(dirname(__DIR__)).'includes/cache/class-wordlift-cached-entity-uri-service.php';
Please login to merge, or discard this patch.
src/includes/class-wordlift-topic-taxonomy-service.php 2 patches
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
 	 */
54 54
 	public function __construct() {
55 55
 
56
-		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
56
+		$this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service');
57 57
 
58 58
 		// Set the singleton instance.
59 59
 		self::$instance = $this;
@@ -80,17 +80,17 @@  discard block
 block discarded – undo
80 80
 
81 81
 		// See https://codex.wordpress.org/Function_Reference/register_taxonomy
82 82
 		$labels = array(
83
-			'name'              => _x( 'Topics', 'taxonomy general name', 'wordlift' ),
84
-			'singular_name'     => _x( 'Topic', 'taxonomy singular name', 'wordlift' ),
85
-			'search_items'      => __( 'Search Topics', 'wordlift' ),
86
-			'all_items'         => __( 'All Topics', 'wordlift' ),
87
-			'parent_item'       => __( 'Parent Topic', 'wordlift' ),
88
-			'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ),
89
-			'edit_item'         => __( 'Edit Topic', 'wordlift' ),
90
-			'update_item'       => __( 'Update Topic', 'wordlift' ),
91
-			'add_new_item'      => __( 'Add New Topic', 'wordlift' ),
92
-			'new_item_name'     => __( 'New Topic', 'wordlift' ),
93
-			'menu_name'         => __( 'Topics', 'wordlift' ),
83
+			'name'              => _x('Topics', 'taxonomy general name', 'wordlift'),
84
+			'singular_name'     => _x('Topic', 'taxonomy singular name', 'wordlift'),
85
+			'search_items'      => __('Search Topics', 'wordlift'),
86
+			'all_items'         => __('All Topics', 'wordlift'),
87
+			'parent_item'       => __('Parent Topic', 'wordlift'),
88
+			'parent_item_colon' => __('Parent Topic:', 'wordlift'),
89
+			'edit_item'         => __('Edit Topic', 'wordlift'),
90
+			'update_item'       => __('Update Topic', 'wordlift'),
91
+			'add_new_item'      => __('Add New Topic', 'wordlift'),
92
+			'new_item_name'     => __('New Topic', 'wordlift'),
93
+			'menu_name'         => __('Topics', 'wordlift'),
94 94
 		);
95 95
 
96 96
 		$capabilities = array(
@@ -125,13 +125,13 @@  discard block
 block discarded – undo
125 125
 	 *
126 126
 	 * @since 3.5.0
127 127
 	 */
128
-	public function get_or_create_term_from_topic_entity( $topic ) {
128
+	public function get_or_create_term_from_topic_entity($topic) {
129 129
 
130 130
 		// Define taxonomy term slug
131
-		$term_slug = sanitize_title( $topic->post_title );
131
+		$term_slug = sanitize_title($topic->post_title);
132 132
 		// Look for an existing taxonomy term with a given slug
133
-		$term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME );
134
-		if ( $term ) {
133
+		$term = get_term_by('slug', $term_slug, self::TAXONOMY_NAME);
134
+		if ($term) {
135 135
 			return (int) $term->term_id;
136 136
 		}
137 137
 		// Otherwise create a new term and return it
@@ -152,17 +152,17 @@  discard block
 block discarded – undo
152 152
 	 *
153 153
 	 * @since 3.5.0
154 154
 	 */
155
-	public function set_topic_for( $post_id, $topic_id ) {
155
+	public function set_topic_for($post_id, $topic_id) {
156 156
 		// Retrieve the topic entity post
157
-		$topic_entity_post = get_post( $topic_id );
157
+		$topic_entity_post = get_post($topic_id);
158 158
 		// If current topic does not exist in db false is returned
159
-		if ( null === $topic_entity_post ) {
159
+		if (null === $topic_entity_post) {
160 160
 			return false;
161 161
 		}
162 162
 		// Create the proper taxonomy term if needed
163
-		$term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post );
163
+		$term_id = $this->get_or_create_term_from_topic_entity($topic_entity_post);
164 164
 		// Link the term to the current post
165
-		wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false );
165
+		wp_set_post_terms($post_id, $term_id, self::TAXONOMY_NAME, false);
166 166
 		return true;
167 167
 	}
168 168
 
@@ -171,8 +171,8 @@  discard block
 block discarded – undo
171 171
 	 *
172 172
 	 * @since 3.5.0
173 173
 	 */
174
-	public function unlink_topic_for( $post_id ) {
175
-		wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME );
174
+	public function unlink_topic_for($post_id) {
175
+		wp_delete_object_term_relationships($post_id, self::TAXONOMY_NAME);
176 176
 	}
177 177
 
178 178
 }
Please login to merge, or discard this patch.
Indentation   +165 added lines, -165 removed lines patch added patch discarded remove patch
@@ -7,169 +7,169 @@
 block discarded – undo
7 7
  */
8 8
 class Wordlift_Topic_Taxonomy_Service {
9 9
 
10
-	/**
11
-	 * The Log service.
12
-	 *
13
-	 * @since 3.5.0
14
-	 * @access private
15
-	 * @var \Wordlift_Log_Service $log_service The Log service.
16
-	 */
17
-	private $log_service;
18
-
19
-	/**
20
-	 * Taxonomy name.
21
-	 *
22
-	 * @since 3.5.0
23
-	 */
24
-	const TAXONOMY_NAME = 'wl_topic';
25
-
26
-	/**
27
-	 * Taxonomy object type.
28
-	 *
29
-	 * @since 3.5.0
30
-	 */
31
-	const TAXONOMY_OBJECT_TYPE = 'post';
32
-
33
-	/**
34
-	 * Taxonomy slug.
35
-	 *
36
-	 * @since 3.5.0
37
-	 */
38
-	const TAXONOMY_SLUG = 'wl_topic';
39
-
40
-	/**
41
-	 * A singleton instance of the Wordlift_Topic_Taxonomy_Service service.
42
-	 *
43
-	 * @since 3.5.0
44
-	 * @access private
45
-	 * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service.
46
-	 */
47
-	private static $instance;
48
-
49
-	/**
50
-	 * Create a Wordlift_Topic_Taxonomy_Service instance.
51
-	 *
52
-	 * @since 3.5.0
53
-	 */
54
-	public function __construct() {
55
-
56
-		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
57
-
58
-		// Set the singleton instance.
59
-		self::$instance = $this;
60
-	}
61
-
62
-	/**
63
-	 * Get the singleton instance of the Entity service.
64
-	 *
65
-	 * @since 3.5.0
66
-	 * @return Wordlift_Topic_Taxonomy_Service
67
-	 */
68
-	public static function get_instance() {
69
-
70
-		return self::$instance;
71
-	}
72
-
73
-	/**
74
-	 * Just register the topic taxonomy.
75
-	 *
76
-	 * @since 3.5.0
77
-	 */
78
-	public function init() {
79
-
80
-		// See https://codex.wordpress.org/Function_Reference/register_taxonomy
81
-		$labels = array(
82
-			'name'              => _x( 'Topics', 'taxonomy general name', 'wordlift' ),
83
-			'singular_name'     => _x( 'Topic', 'taxonomy singular name', 'wordlift' ),
84
-			'search_items'      => __( 'Search Topics', 'wordlift' ),
85
-			'all_items'         => __( 'All Topics', 'wordlift' ),
86
-			'parent_item'       => __( 'Parent Topic', 'wordlift' ),
87
-			'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ),
88
-			'edit_item'         => __( 'Edit Topic', 'wordlift' ),
89
-			'update_item'       => __( 'Update Topic', 'wordlift' ),
90
-			'add_new_item'      => __( 'Add New Topic', 'wordlift' ),
91
-			'new_item_name'     => __( 'New Topic', 'wordlift' ),
92
-			'menu_name'         => __( 'Topics', 'wordlift' ),
93
-		);
94
-
95
-		$capabilities = array(
96
-			'manage_terms' => null,
97
-			'edit_terms'   => null,
98
-			'delete_terms' => null,
99
-			'assign_terms' => 'edit_posts',
100
-		);
101
-
102
-		$args = array(
103
-			'labels'            => $labels,
104
-			'capabilities'      => $capabilities,
105
-			'hierarchical'      => true,
106
-			'show_admin_column' => false,
107
-			'show_ui'           => false,
108
-			'rewrite'           => array(
109
-				'slug' => self::TAXONOMY_SLUG,
110
-			),
111
-		);
112
-
113
-		// Register taxonomy
114
-		register_taxonomy(
115
-			self::TAXONOMY_NAME,
116
-			self::TAXONOMY_OBJECT_TYPE,
117
-			$args
118
-		);
119
-	}
120
-
121
-	/**
122
-	 * Get or create a taxonomy term from a given entity topic.
123
-	 *
124
-	 * @since 3.5.0
125
-	 */
126
-	public function get_or_create_term_from_topic_entity( $topic ) {
127
-
128
-		// Define taxonomy term slug
129
-		$term_slug = sanitize_title( $topic->post_title );
130
-		// Look for an existing taxonomy term with a given slug
131
-		$term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME );
132
-		if ( $term ) {
133
-			return (int) $term->term_id;
134
-		}
135
-		// Otherwise create a new term and return it
136
-		$result = wp_insert_term(
137
-			$topic->post_title,
138
-			self::TAXONOMY_NAME,
139
-			array(
140
-				'slug'        => $term_slug,
141
-				'description' => $topic->post_content,
142
-			)
143
-		);
144
-
145
-		return (int) $result['term_id'];
146
-	}
147
-
148
-	/**
149
-	 * Set a topic for a given post.
150
-	 *
151
-	 * @since 3.5.0
152
-	 */
153
-	public function set_topic_for( $post_id, $topic_id ) {
154
-		// Retrieve the topic entity post
155
-		$topic_entity_post = get_post( $topic_id );
156
-		// If current topic does not exist in db false is returned
157
-		if ( null === $topic_entity_post ) {
158
-			return false;
159
-		}
160
-		// Create the proper taxonomy term if needed
161
-		$term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post );
162
-		// Link the term to the current post
163
-		wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false );
164
-		return true;
165
-	}
166
-
167
-	/**
168
-	 * Unlink any topic for a given post.
169
-	 *
170
-	 * @since 3.5.0
171
-	 */
172
-	public function unlink_topic_for( $post_id ) {
173
-		wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME );
174
-	}
10
+    /**
11
+     * The Log service.
12
+     *
13
+     * @since 3.5.0
14
+     * @access private
15
+     * @var \Wordlift_Log_Service $log_service The Log service.
16
+     */
17
+    private $log_service;
18
+
19
+    /**
20
+     * Taxonomy name.
21
+     *
22
+     * @since 3.5.0
23
+     */
24
+    const TAXONOMY_NAME = 'wl_topic';
25
+
26
+    /**
27
+     * Taxonomy object type.
28
+     *
29
+     * @since 3.5.0
30
+     */
31
+    const TAXONOMY_OBJECT_TYPE = 'post';
32
+
33
+    /**
34
+     * Taxonomy slug.
35
+     *
36
+     * @since 3.5.0
37
+     */
38
+    const TAXONOMY_SLUG = 'wl_topic';
39
+
40
+    /**
41
+     * A singleton instance of the Wordlift_Topic_Taxonomy_Service service.
42
+     *
43
+     * @since 3.5.0
44
+     * @access private
45
+     * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service.
46
+     */
47
+    private static $instance;
48
+
49
+    /**
50
+     * Create a Wordlift_Topic_Taxonomy_Service instance.
51
+     *
52
+     * @since 3.5.0
53
+     */
54
+    public function __construct() {
55
+
56
+        $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
57
+
58
+        // Set the singleton instance.
59
+        self::$instance = $this;
60
+    }
61
+
62
+    /**
63
+     * Get the singleton instance of the Entity service.
64
+     *
65
+     * @since 3.5.0
66
+     * @return Wordlift_Topic_Taxonomy_Service
67
+     */
68
+    public static function get_instance() {
69
+
70
+        return self::$instance;
71
+    }
72
+
73
+    /**
74
+     * Just register the topic taxonomy.
75
+     *
76
+     * @since 3.5.0
77
+     */
78
+    public function init() {
79
+
80
+        // See https://codex.wordpress.org/Function_Reference/register_taxonomy
81
+        $labels = array(
82
+            'name'              => _x( 'Topics', 'taxonomy general name', 'wordlift' ),
83
+            'singular_name'     => _x( 'Topic', 'taxonomy singular name', 'wordlift' ),
84
+            'search_items'      => __( 'Search Topics', 'wordlift' ),
85
+            'all_items'         => __( 'All Topics', 'wordlift' ),
86
+            'parent_item'       => __( 'Parent Topic', 'wordlift' ),
87
+            'parent_item_colon' => __( 'Parent Topic:', 'wordlift' ),
88
+            'edit_item'         => __( 'Edit Topic', 'wordlift' ),
89
+            'update_item'       => __( 'Update Topic', 'wordlift' ),
90
+            'add_new_item'      => __( 'Add New Topic', 'wordlift' ),
91
+            'new_item_name'     => __( 'New Topic', 'wordlift' ),
92
+            'menu_name'         => __( 'Topics', 'wordlift' ),
93
+        );
94
+
95
+        $capabilities = array(
96
+            'manage_terms' => null,
97
+            'edit_terms'   => null,
98
+            'delete_terms' => null,
99
+            'assign_terms' => 'edit_posts',
100
+        );
101
+
102
+        $args = array(
103
+            'labels'            => $labels,
104
+            'capabilities'      => $capabilities,
105
+            'hierarchical'      => true,
106
+            'show_admin_column' => false,
107
+            'show_ui'           => false,
108
+            'rewrite'           => array(
109
+                'slug' => self::TAXONOMY_SLUG,
110
+            ),
111
+        );
112
+
113
+        // Register taxonomy
114
+        register_taxonomy(
115
+            self::TAXONOMY_NAME,
116
+            self::TAXONOMY_OBJECT_TYPE,
117
+            $args
118
+        );
119
+    }
120
+
121
+    /**
122
+     * Get or create a taxonomy term from a given entity topic.
123
+     *
124
+     * @since 3.5.0
125
+     */
126
+    public function get_or_create_term_from_topic_entity( $topic ) {
127
+
128
+        // Define taxonomy term slug
129
+        $term_slug = sanitize_title( $topic->post_title );
130
+        // Look for an existing taxonomy term with a given slug
131
+        $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME );
132
+        if ( $term ) {
133
+            return (int) $term->term_id;
134
+        }
135
+        // Otherwise create a new term and return it
136
+        $result = wp_insert_term(
137
+            $topic->post_title,
138
+            self::TAXONOMY_NAME,
139
+            array(
140
+                'slug'        => $term_slug,
141
+                'description' => $topic->post_content,
142
+            )
143
+        );
144
+
145
+        return (int) $result['term_id'];
146
+    }
147
+
148
+    /**
149
+     * Set a topic for a given post.
150
+     *
151
+     * @since 3.5.0
152
+     */
153
+    public function set_topic_for( $post_id, $topic_id ) {
154
+        // Retrieve the topic entity post
155
+        $topic_entity_post = get_post( $topic_id );
156
+        // If current topic does not exist in db false is returned
157
+        if ( null === $topic_entity_post ) {
158
+            return false;
159
+        }
160
+        // Create the proper taxonomy term if needed
161
+        $term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post );
162
+        // Link the term to the current post
163
+        wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false );
164
+        return true;
165
+    }
166
+
167
+    /**
168
+     * Unlink any topic for a given post.
169
+     *
170
+     * @since 3.5.0
171
+     */
172
+    public function unlink_topic_for( $post_id ) {
173
+        wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME );
174
+    }
175 175
 }
Please login to merge, or discard this patch.
deliciousbrains/wp-background-processing/classes/wp-background-process.php 2 patches
Indentation   +487 added lines, -487 removed lines patch added patch discarded remove patch
@@ -13,492 +13,492 @@
 block discarded – undo
13 13
  */
14 14
 abstract class Wordlift_Plugin_WP_Background_Process extends Wordlift_Plugin_WP_Async_Request {
15 15
 
16
-	/**
17
-	 * Action
18
-	 *
19
-	 * (default value: 'background_process')
20
-	 *
21
-	 * @var string
22
-	 * @access protected
23
-	 */
24
-	protected $action = 'background_process';
25
-
26
-	/**
27
-	 * Start time of current process.
28
-	 *
29
-	 * (default value: 0)
30
-	 *
31
-	 * @var int
32
-	 * @access protected
33
-	 */
34
-	protected $start_time = 0;
35
-
36
-	/**
37
-	 * Cron_hook_identifier
38
-	 *
39
-	 * @var mixed
40
-	 * @access protected
41
-	 */
42
-	protected $cron_hook_identifier;
43
-
44
-	/**
45
-	 * Cron_interval_identifier
46
-	 *
47
-	 * @var mixed
48
-	 * @access protected
49
-	 */
50
-	protected $cron_interval_identifier;
51
-
52
-	/**
53
-	 * Initiate new background process
54
-	 */
55
-	public function __construct() {
56
-		parent::__construct();
57
-
58
-		$this->cron_hook_identifier     = $this->identifier . '_cron';
59
-		$this->cron_interval_identifier = $this->identifier . '_cron_interval';
60
-
61
-		add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) );
62
-		add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) );
63
-	}
64
-
65
-	/**
66
-	 * Dispatch
67
-	 *
68
-	 * @access public
69
-	 * @return void
70
-	 */
71
-	public function dispatch() {
72
-		// Schedule the cron healthcheck.
73
-		$this->schedule_event();
74
-
75
-		// Perform remote post.
76
-		return parent::dispatch();
77
-	}
78
-
79
-	/**
80
-	 * Push to queue
81
-	 *
82
-	 * @param mixed $data Data.
83
-	 *
84
-	 * @return $this
85
-	 */
86
-	public function push_to_queue( $data ) {
87
-		$this->data[] = $data;
88
-
89
-		return $this;
90
-	}
91
-
92
-	/**
93
-	 * Save queue
94
-	 *
95
-	 * @return $this
96
-	 */
97
-	public function save() {
98
-		$key = $this->generate_key();
99
-
100
-		if ( ! empty( $this->data ) ) {
101
-			update_site_option( $key, $this->data );
102
-		}
103
-
104
-		return $this;
105
-	}
106
-
107
-	/**
108
-	 * Update queue
109
-	 *
110
-	 * @param string $key  Key.
111
-	 * @param array  $data Data.
112
-	 *
113
-	 * @return $this
114
-	 */
115
-	public function update( $key, $data ) {
116
-		if ( ! empty( $data ) ) {
117
-			update_site_option( $key, $data );
118
-		}
119
-
120
-		return $this;
121
-	}
122
-
123
-	/**
124
-	 * Delete queue
125
-	 *
126
-	 * @param string $key Key.
127
-	 *
128
-	 * @return $this
129
-	 */
130
-	public function delete( $key ) {
131
-		delete_site_option( $key );
132
-
133
-		return $this;
134
-	}
135
-
136
-	/**
137
-	 * Generate key
138
-	 *
139
-	 * Generates a unique key based on microtime. Queue items are
140
-	 * given a unique key so that they can be merged upon save.
141
-	 *
142
-	 * @param int $length Length.
143
-	 *
144
-	 * @return string
145
-	 */
146
-	protected function generate_key( $length = 64 ) {
147
-		$unique  = md5( microtime() . rand() );
148
-		$prepend = $this->identifier . '_batch_';
149
-
150
-		return substr( $prepend . $unique, 0, $length );
151
-	}
152
-
153
-	/**
154
-	 * Maybe process queue
155
-	 *
156
-	 * Checks whether data exists within the queue and that
157
-	 * the process is not already running.
158
-	 */
159
-	public function maybe_handle() {
160
-		// Don't lock up other requests while processing
161
-		session_write_close();
162
-
163
-		if ( $this->is_process_running() ) {
164
-			// Background process already running.
165
-			wp_die();
166
-		}
167
-
168
-		if ( $this->is_queue_empty() ) {
169
-			// No data to process.
170
-			wp_die();
171
-		}
172
-
173
-		check_ajax_referer( $this->identifier, 'nonce' );
174
-
175
-		$this->handle();
176
-
177
-		wp_die();
178
-	}
179
-
180
-	/**
181
-	 * Is queue empty
182
-	 *
183
-	 * @return bool
184
-	 */
185
-	protected function is_queue_empty() {
186
-		global $wpdb;
187
-
188
-		$table  = $wpdb->options;
189
-		$column = 'option_name';
190
-
191
-		if ( is_multisite() ) {
192
-			$table  = $wpdb->sitemeta;
193
-			$column = 'meta_key';
194
-		}
195
-
196
-		$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
197
-
198
-		$count = $wpdb->get_var(
199
-			$wpdb->prepare(
200
-				"SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
201
-				$key
202
-			)
203
-		);
204
-
205
-		return ( $count > 0 ) ? false : true;
206
-	}
207
-
208
-	/**
209
-	 * Is process running
210
-	 *
211
-	 * Check whether the current process is already running
212
-	 * in a background process.
213
-	 */
214
-	protected function is_process_running() {
215
-		if ( get_site_transient( $this->identifier . '_process_lock' ) ) {
216
-			// Process already running.
217
-			return true;
218
-		}
219
-
220
-		return false;
221
-	}
222
-
223
-	/**
224
-	 * Lock process
225
-	 *
226
-	 * Lock the process so that multiple instances can't run simultaneously.
227
-	 * Override if applicable, but the duration should be greater than that
228
-	 * defined in the time_exceeded() method.
229
-	 */
230
-	protected function lock_process() {
231
-		$this->start_time = time(); // Set start time of current process.
232
-
233
-		$lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute
234
-		$lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration );
235
-
236
-		set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration );
237
-	}
238
-
239
-	/**
240
-	 * Unlock process
241
-	 *
242
-	 * Unlock the process so that other instances can spawn.
243
-	 *
244
-	 * @return $this
245
-	 */
246
-	protected function unlock_process() {
247
-		delete_site_transient( $this->identifier . '_process_lock' );
248
-
249
-		return $this;
250
-	}
251
-
252
-	/**
253
-	 * Get batch
254
-	 *
255
-	 * @return stdClass Return the first batch from the queue
256
-	 */
257
-	protected function get_batch() {
258
-		global $wpdb;
259
-
260
-		$table        = $wpdb->options;
261
-		$column       = 'option_name';
262
-		$key_column   = 'option_id';
263
-		$value_column = 'option_value';
264
-
265
-		if ( is_multisite() ) {
266
-			$table        = $wpdb->sitemeta;
267
-			$column       = 'meta_key';
268
-			$key_column   = 'meta_id';
269
-			$value_column = 'meta_value';
270
-		}
271
-
272
-		$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
273
-
274
-		$query = $wpdb->get_row(
275
-			$wpdb->prepare(
276
-				"SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
277
-				$key
278
-			)
279
-		);
280
-
281
-		$batch       = new stdClass();
282
-		$batch->key  = $query->$column;
283
-		$batch->data = maybe_unserialize( $query->$value_column );
284
-
285
-		return $batch;
286
-	}
287
-
288
-	/**
289
-	 * Handle
290
-	 *
291
-	 * Pass each queue item to the task handler, while remaining
292
-	 * within server memory and time limit constraints.
293
-	 */
294
-	protected function handle() {
295
-		$this->lock_process();
296
-
297
-		do {
298
-			$batch = $this->get_batch();
299
-
300
-			foreach ( $batch->data as $key => $value ) {
301
-				$task = $this->task( $value );
302
-
303
-				if ( false !== $task ) {
304
-					$batch->data[ $key ] = $task;
305
-				} else {
306
-					unset( $batch->data[ $key ] );
307
-				}
308
-
309
-				if ( $this->time_exceeded() || $this->memory_exceeded() ) {
310
-					// Batch limits reached.
311
-					break;
312
-				}
313
-			}
314
-
315
-			// Update or delete current batch.
316
-			if ( ! empty( $batch->data ) ) {
317
-				$this->update( $batch->key, $batch->data );
318
-			} else {
319
-				$this->delete( $batch->key );
320
-			}
321
-		} while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() );
322
-
323
-		$this->unlock_process();
324
-
325
-		// Start next batch or complete process.
326
-		if ( ! $this->is_queue_empty() ) {
327
-			$this->dispatch();
328
-		} else {
329
-			$this->complete();
330
-		}
331
-
332
-		wp_die();
333
-	}
334
-
335
-	/**
336
-	 * Memory exceeded
337
-	 *
338
-	 * Ensures the batch process never exceeds 90%
339
-	 * of the maximum WordPress memory.
340
-	 *
341
-	 * @return bool
342
-	 */
343
-	protected function memory_exceeded() {
344
-		$memory_limit   = $this->get_memory_limit() * 0.9; // 90% of max memory
345
-		$current_memory = memory_get_usage( true );
346
-		$return         = false;
347
-
348
-		if ( $current_memory >= $memory_limit ) {
349
-			$return = true;
350
-		}
351
-
352
-		return apply_filters( $this->identifier . '_memory_exceeded', $return );
353
-	}
354
-
355
-	/**
356
-	 * Get memory limit
357
-	 *
358
-	 * @return int
359
-	 */
360
-	protected function get_memory_limit() {
361
-		if ( function_exists( 'ini_get' ) ) {
362
-			$memory_limit = ini_get( 'memory_limit' );
363
-		} else {
364
-			// Sensible default.
365
-			$memory_limit = '128M';
366
-		}
367
-
368
-		if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) {
369
-			// Unlimited, set to 32GB.
370
-			$memory_limit = '32000M';
371
-		}
372
-
373
-		return wp_convert_hr_to_bytes( $memory_limit );
374
-	}
375
-
376
-	/**
377
-	 * Time exceeded.
378
-	 *
379
-	 * Ensures the batch never exceeds a sensible time limit.
380
-	 * A timeout limit of 30s is common on shared hosting.
381
-	 *
382
-	 * @return bool
383
-	 */
384
-	protected function time_exceeded() {
385
-		$finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds
386
-		$return = false;
387
-
388
-		if ( time() >= $finish ) {
389
-			$return = true;
390
-		}
391
-
392
-		return apply_filters( $this->identifier . '_time_exceeded', $return );
393
-	}
394
-
395
-	/**
396
-	 * Complete.
397
-	 *
398
-	 * Override if applicable, but ensure that the below actions are
399
-	 * performed, or, call parent::complete().
400
-	 */
401
-	protected function complete() {
402
-		// Unschedule the cron healthcheck.
403
-		$this->clear_scheduled_event();
404
-	}
405
-
406
-	/**
407
-	 * Schedule cron healthcheck
408
-	 *
409
-	 * @access public
410
-	 *
411
-	 * @param mixed $schedules Schedules.
412
-	 *
413
-	 * @return mixed
414
-	 */
415
-	public function schedule_cron_healthcheck( $schedules ) {
416
-		$interval = apply_filters( $this->identifier . '_cron_interval', 5 );
417
-
418
-		if ( property_exists( $this, 'cron_interval' ) ) {
419
-			$interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval );
420
-		}
421
-
422
-		// Adds every 5 minutes to the existing schedules.
423
-		$schedules[ $this->identifier . '_cron_interval' ] = array(
424
-			'interval' => MINUTE_IN_SECONDS * $interval,
425
-			'display'  => sprintf( __( 'Every %d Minutes' ), $interval ),
426
-		);
427
-
428
-		return $schedules;
429
-	}
430
-
431
-	/**
432
-	 * Handle cron healthcheck
433
-	 *
434
-	 * Restart the background process if not already running
435
-	 * and data exists in the queue.
436
-	 */
437
-	public function handle_cron_healthcheck() {
438
-		if ( $this->is_process_running() ) {
439
-			// Background process already running.
440
-			exit;
441
-		}
442
-
443
-		if ( $this->is_queue_empty() ) {
444
-			// No data to process.
445
-			$this->clear_scheduled_event();
446
-			exit;
447
-		}
448
-
449
-		$this->handle();
450
-
451
-		exit;
452
-	}
453
-
454
-	/**
455
-	 * Schedule event
456
-	 */
457
-	protected function schedule_event() {
458
-		if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
459
-			wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier );
460
-		}
461
-	}
462
-
463
-	/**
464
-	 * Clear scheduled event
465
-	 */
466
-	protected function clear_scheduled_event() {
467
-		$timestamp = wp_next_scheduled( $this->cron_hook_identifier );
468
-
469
-		if ( $timestamp ) {
470
-			wp_unschedule_event( $timestamp, $this->cron_hook_identifier );
471
-		}
472
-	}
473
-
474
-	/**
475
-	 * Cancel Process
476
-	 *
477
-	 * Stop processing queue items, clear cronjob and delete batch.
478
-	 */
479
-	public function cancel_process() {
480
-		if ( ! $this->is_queue_empty() ) {
481
-			$batch = $this->get_batch();
482
-
483
-			$this->delete( $batch->key );
484
-
485
-			wp_clear_scheduled_hook( $this->cron_hook_identifier );
486
-		}
487
-
488
-	}
489
-
490
-	/**
491
-	 * Task
492
-	 *
493
-	 * Override this method to perform any actions required on each
494
-	 * queue item. Return the modified item for further processing
495
-	 * in the next pass through. Or, return false to remove the
496
-	 * item from the queue.
497
-	 *
498
-	 * @param mixed $item Queue item to iterate over.
499
-	 *
500
-	 * @return mixed
501
-	 */
502
-	abstract protected function task( $item );
16
+    /**
17
+     * Action
18
+     *
19
+     * (default value: 'background_process')
20
+     *
21
+     * @var string
22
+     * @access protected
23
+     */
24
+    protected $action = 'background_process';
25
+
26
+    /**
27
+     * Start time of current process.
28
+     *
29
+     * (default value: 0)
30
+     *
31
+     * @var int
32
+     * @access protected
33
+     */
34
+    protected $start_time = 0;
35
+
36
+    /**
37
+     * Cron_hook_identifier
38
+     *
39
+     * @var mixed
40
+     * @access protected
41
+     */
42
+    protected $cron_hook_identifier;
43
+
44
+    /**
45
+     * Cron_interval_identifier
46
+     *
47
+     * @var mixed
48
+     * @access protected
49
+     */
50
+    protected $cron_interval_identifier;
51
+
52
+    /**
53
+     * Initiate new background process
54
+     */
55
+    public function __construct() {
56
+        parent::__construct();
57
+
58
+        $this->cron_hook_identifier     = $this->identifier . '_cron';
59
+        $this->cron_interval_identifier = $this->identifier . '_cron_interval';
60
+
61
+        add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) );
62
+        add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) );
63
+    }
64
+
65
+    /**
66
+     * Dispatch
67
+     *
68
+     * @access public
69
+     * @return void
70
+     */
71
+    public function dispatch() {
72
+        // Schedule the cron healthcheck.
73
+        $this->schedule_event();
74
+
75
+        // Perform remote post.
76
+        return parent::dispatch();
77
+    }
78
+
79
+    /**
80
+     * Push to queue
81
+     *
82
+     * @param mixed $data Data.
83
+     *
84
+     * @return $this
85
+     */
86
+    public function push_to_queue( $data ) {
87
+        $this->data[] = $data;
88
+
89
+        return $this;
90
+    }
91
+
92
+    /**
93
+     * Save queue
94
+     *
95
+     * @return $this
96
+     */
97
+    public function save() {
98
+        $key = $this->generate_key();
99
+
100
+        if ( ! empty( $this->data ) ) {
101
+            update_site_option( $key, $this->data );
102
+        }
103
+
104
+        return $this;
105
+    }
106
+
107
+    /**
108
+     * Update queue
109
+     *
110
+     * @param string $key  Key.
111
+     * @param array  $data Data.
112
+     *
113
+     * @return $this
114
+     */
115
+    public function update( $key, $data ) {
116
+        if ( ! empty( $data ) ) {
117
+            update_site_option( $key, $data );
118
+        }
119
+
120
+        return $this;
121
+    }
122
+
123
+    /**
124
+     * Delete queue
125
+     *
126
+     * @param string $key Key.
127
+     *
128
+     * @return $this
129
+     */
130
+    public function delete( $key ) {
131
+        delete_site_option( $key );
132
+
133
+        return $this;
134
+    }
135
+
136
+    /**
137
+     * Generate key
138
+     *
139
+     * Generates a unique key based on microtime. Queue items are
140
+     * given a unique key so that they can be merged upon save.
141
+     *
142
+     * @param int $length Length.
143
+     *
144
+     * @return string
145
+     */
146
+    protected function generate_key( $length = 64 ) {
147
+        $unique  = md5( microtime() . rand() );
148
+        $prepend = $this->identifier . '_batch_';
149
+
150
+        return substr( $prepend . $unique, 0, $length );
151
+    }
152
+
153
+    /**
154
+     * Maybe process queue
155
+     *
156
+     * Checks whether data exists within the queue and that
157
+     * the process is not already running.
158
+     */
159
+    public function maybe_handle() {
160
+        // Don't lock up other requests while processing
161
+        session_write_close();
162
+
163
+        if ( $this->is_process_running() ) {
164
+            // Background process already running.
165
+            wp_die();
166
+        }
167
+
168
+        if ( $this->is_queue_empty() ) {
169
+            // No data to process.
170
+            wp_die();
171
+        }
172
+
173
+        check_ajax_referer( $this->identifier, 'nonce' );
174
+
175
+        $this->handle();
176
+
177
+        wp_die();
178
+    }
179
+
180
+    /**
181
+     * Is queue empty
182
+     *
183
+     * @return bool
184
+     */
185
+    protected function is_queue_empty() {
186
+        global $wpdb;
187
+
188
+        $table  = $wpdb->options;
189
+        $column = 'option_name';
190
+
191
+        if ( is_multisite() ) {
192
+            $table  = $wpdb->sitemeta;
193
+            $column = 'meta_key';
194
+        }
195
+
196
+        $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
197
+
198
+        $count = $wpdb->get_var(
199
+            $wpdb->prepare(
200
+                "SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
201
+                $key
202
+            )
203
+        );
204
+
205
+        return ( $count > 0 ) ? false : true;
206
+    }
207
+
208
+    /**
209
+     * Is process running
210
+     *
211
+     * Check whether the current process is already running
212
+     * in a background process.
213
+     */
214
+    protected function is_process_running() {
215
+        if ( get_site_transient( $this->identifier . '_process_lock' ) ) {
216
+            // Process already running.
217
+            return true;
218
+        }
219
+
220
+        return false;
221
+    }
222
+
223
+    /**
224
+     * Lock process
225
+     *
226
+     * Lock the process so that multiple instances can't run simultaneously.
227
+     * Override if applicable, but the duration should be greater than that
228
+     * defined in the time_exceeded() method.
229
+     */
230
+    protected function lock_process() {
231
+        $this->start_time = time(); // Set start time of current process.
232
+
233
+        $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute
234
+        $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration );
235
+
236
+        set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration );
237
+    }
238
+
239
+    /**
240
+     * Unlock process
241
+     *
242
+     * Unlock the process so that other instances can spawn.
243
+     *
244
+     * @return $this
245
+     */
246
+    protected function unlock_process() {
247
+        delete_site_transient( $this->identifier . '_process_lock' );
248
+
249
+        return $this;
250
+    }
251
+
252
+    /**
253
+     * Get batch
254
+     *
255
+     * @return stdClass Return the first batch from the queue
256
+     */
257
+    protected function get_batch() {
258
+        global $wpdb;
259
+
260
+        $table        = $wpdb->options;
261
+        $column       = 'option_name';
262
+        $key_column   = 'option_id';
263
+        $value_column = 'option_value';
264
+
265
+        if ( is_multisite() ) {
266
+            $table        = $wpdb->sitemeta;
267
+            $column       = 'meta_key';
268
+            $key_column   = 'meta_id';
269
+            $value_column = 'meta_value';
270
+        }
271
+
272
+        $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
273
+
274
+        $query = $wpdb->get_row(
275
+            $wpdb->prepare(
276
+                "SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
277
+                $key
278
+            )
279
+        );
280
+
281
+        $batch       = new stdClass();
282
+        $batch->key  = $query->$column;
283
+        $batch->data = maybe_unserialize( $query->$value_column );
284
+
285
+        return $batch;
286
+    }
287
+
288
+    /**
289
+     * Handle
290
+     *
291
+     * Pass each queue item to the task handler, while remaining
292
+     * within server memory and time limit constraints.
293
+     */
294
+    protected function handle() {
295
+        $this->lock_process();
296
+
297
+        do {
298
+            $batch = $this->get_batch();
299
+
300
+            foreach ( $batch->data as $key => $value ) {
301
+                $task = $this->task( $value );
302
+
303
+                if ( false !== $task ) {
304
+                    $batch->data[ $key ] = $task;
305
+                } else {
306
+                    unset( $batch->data[ $key ] );
307
+                }
308
+
309
+                if ( $this->time_exceeded() || $this->memory_exceeded() ) {
310
+                    // Batch limits reached.
311
+                    break;
312
+                }
313
+            }
314
+
315
+            // Update or delete current batch.
316
+            if ( ! empty( $batch->data ) ) {
317
+                $this->update( $batch->key, $batch->data );
318
+            } else {
319
+                $this->delete( $batch->key );
320
+            }
321
+        } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() );
322
+
323
+        $this->unlock_process();
324
+
325
+        // Start next batch or complete process.
326
+        if ( ! $this->is_queue_empty() ) {
327
+            $this->dispatch();
328
+        } else {
329
+            $this->complete();
330
+        }
331
+
332
+        wp_die();
333
+    }
334
+
335
+    /**
336
+     * Memory exceeded
337
+     *
338
+     * Ensures the batch process never exceeds 90%
339
+     * of the maximum WordPress memory.
340
+     *
341
+     * @return bool
342
+     */
343
+    protected function memory_exceeded() {
344
+        $memory_limit   = $this->get_memory_limit() * 0.9; // 90% of max memory
345
+        $current_memory = memory_get_usage( true );
346
+        $return         = false;
347
+
348
+        if ( $current_memory >= $memory_limit ) {
349
+            $return = true;
350
+        }
351
+
352
+        return apply_filters( $this->identifier . '_memory_exceeded', $return );
353
+    }
354
+
355
+    /**
356
+     * Get memory limit
357
+     *
358
+     * @return int
359
+     */
360
+    protected function get_memory_limit() {
361
+        if ( function_exists( 'ini_get' ) ) {
362
+            $memory_limit = ini_get( 'memory_limit' );
363
+        } else {
364
+            // Sensible default.
365
+            $memory_limit = '128M';
366
+        }
367
+
368
+        if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) {
369
+            // Unlimited, set to 32GB.
370
+            $memory_limit = '32000M';
371
+        }
372
+
373
+        return wp_convert_hr_to_bytes( $memory_limit );
374
+    }
375
+
376
+    /**
377
+     * Time exceeded.
378
+     *
379
+     * Ensures the batch never exceeds a sensible time limit.
380
+     * A timeout limit of 30s is common on shared hosting.
381
+     *
382
+     * @return bool
383
+     */
384
+    protected function time_exceeded() {
385
+        $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds
386
+        $return = false;
387
+
388
+        if ( time() >= $finish ) {
389
+            $return = true;
390
+        }
391
+
392
+        return apply_filters( $this->identifier . '_time_exceeded', $return );
393
+    }
394
+
395
+    /**
396
+     * Complete.
397
+     *
398
+     * Override if applicable, but ensure that the below actions are
399
+     * performed, or, call parent::complete().
400
+     */
401
+    protected function complete() {
402
+        // Unschedule the cron healthcheck.
403
+        $this->clear_scheduled_event();
404
+    }
405
+
406
+    /**
407
+     * Schedule cron healthcheck
408
+     *
409
+     * @access public
410
+     *
411
+     * @param mixed $schedules Schedules.
412
+     *
413
+     * @return mixed
414
+     */
415
+    public function schedule_cron_healthcheck( $schedules ) {
416
+        $interval = apply_filters( $this->identifier . '_cron_interval', 5 );
417
+
418
+        if ( property_exists( $this, 'cron_interval' ) ) {
419
+            $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval );
420
+        }
421
+
422
+        // Adds every 5 minutes to the existing schedules.
423
+        $schedules[ $this->identifier . '_cron_interval' ] = array(
424
+            'interval' => MINUTE_IN_SECONDS * $interval,
425
+            'display'  => sprintf( __( 'Every %d Minutes' ), $interval ),
426
+        );
427
+
428
+        return $schedules;
429
+    }
430
+
431
+    /**
432
+     * Handle cron healthcheck
433
+     *
434
+     * Restart the background process if not already running
435
+     * and data exists in the queue.
436
+     */
437
+    public function handle_cron_healthcheck() {
438
+        if ( $this->is_process_running() ) {
439
+            // Background process already running.
440
+            exit;
441
+        }
442
+
443
+        if ( $this->is_queue_empty() ) {
444
+            // No data to process.
445
+            $this->clear_scheduled_event();
446
+            exit;
447
+        }
448
+
449
+        $this->handle();
450
+
451
+        exit;
452
+    }
453
+
454
+    /**
455
+     * Schedule event
456
+     */
457
+    protected function schedule_event() {
458
+        if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
459
+            wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier );
460
+        }
461
+    }
462
+
463
+    /**
464
+     * Clear scheduled event
465
+     */
466
+    protected function clear_scheduled_event() {
467
+        $timestamp = wp_next_scheduled( $this->cron_hook_identifier );
468
+
469
+        if ( $timestamp ) {
470
+            wp_unschedule_event( $timestamp, $this->cron_hook_identifier );
471
+        }
472
+    }
473
+
474
+    /**
475
+     * Cancel Process
476
+     *
477
+     * Stop processing queue items, clear cronjob and delete batch.
478
+     */
479
+    public function cancel_process() {
480
+        if ( ! $this->is_queue_empty() ) {
481
+            $batch = $this->get_batch();
482
+
483
+            $this->delete( $batch->key );
484
+
485
+            wp_clear_scheduled_hook( $this->cron_hook_identifier );
486
+        }
487
+
488
+    }
489
+
490
+    /**
491
+     * Task
492
+     *
493
+     * Override this method to perform any actions required on each
494
+     * queue item. Return the modified item for further processing
495
+     * in the next pass through. Or, return false to remove the
496
+     * item from the queue.
497
+     *
498
+     * @param mixed $item Queue item to iterate over.
499
+     *
500
+     * @return mixed
501
+     */
502
+    abstract protected function task( $item );
503 503
 
504 504
 }
Please login to merge, or discard this patch.
Spacing   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -55,11 +55,11 @@  discard block
 block discarded – undo
55 55
 	public function __construct() {
56 56
 		parent::__construct();
57 57
 
58
-		$this->cron_hook_identifier     = $this->identifier . '_cron';
59
-		$this->cron_interval_identifier = $this->identifier . '_cron_interval';
58
+		$this->cron_hook_identifier     = $this->identifier.'_cron';
59
+		$this->cron_interval_identifier = $this->identifier.'_cron_interval';
60 60
 
61
-		add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) );
62
-		add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) );
61
+		add_action($this->cron_hook_identifier, array($this, 'handle_cron_healthcheck'));
62
+		add_filter('cron_schedules', array($this, 'schedule_cron_healthcheck'));
63 63
 	}
64 64
 
65 65
 	/**
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 	 *
84 84
 	 * @return $this
85 85
 	 */
86
-	public function push_to_queue( $data ) {
86
+	public function push_to_queue($data) {
87 87
 		$this->data[] = $data;
88 88
 
89 89
 		return $this;
@@ -97,8 +97,8 @@  discard block
 block discarded – undo
97 97
 	public function save() {
98 98
 		$key = $this->generate_key();
99 99
 
100
-		if ( ! empty( $this->data ) ) {
101
-			update_site_option( $key, $this->data );
100
+		if ( ! empty($this->data)) {
101
+			update_site_option($key, $this->data);
102 102
 		}
103 103
 
104 104
 		return $this;
@@ -112,9 +112,9 @@  discard block
 block discarded – undo
112 112
 	 *
113 113
 	 * @return $this
114 114
 	 */
115
-	public function update( $key, $data ) {
116
-		if ( ! empty( $data ) ) {
117
-			update_site_option( $key, $data );
115
+	public function update($key, $data) {
116
+		if ( ! empty($data)) {
117
+			update_site_option($key, $data);
118 118
 		}
119 119
 
120 120
 		return $this;
@@ -127,8 +127,8 @@  discard block
 block discarded – undo
127 127
 	 *
128 128
 	 * @return $this
129 129
 	 */
130
-	public function delete( $key ) {
131
-		delete_site_option( $key );
130
+	public function delete($key) {
131
+		delete_site_option($key);
132 132
 
133 133
 		return $this;
134 134
 	}
@@ -143,11 +143,11 @@  discard block
 block discarded – undo
143 143
 	 *
144 144
 	 * @return string
145 145
 	 */
146
-	protected function generate_key( $length = 64 ) {
147
-		$unique  = md5( microtime() . rand() );
148
-		$prepend = $this->identifier . '_batch_';
146
+	protected function generate_key($length = 64) {
147
+		$unique  = md5(microtime().rand());
148
+		$prepend = $this->identifier.'_batch_';
149 149
 
150
-		return substr( $prepend . $unique, 0, $length );
150
+		return substr($prepend.$unique, 0, $length);
151 151
 	}
152 152
 
153 153
 	/**
@@ -160,17 +160,17 @@  discard block
 block discarded – undo
160 160
 		// Don't lock up other requests while processing
161 161
 		session_write_close();
162 162
 
163
-		if ( $this->is_process_running() ) {
163
+		if ($this->is_process_running()) {
164 164
 			// Background process already running.
165 165
 			wp_die();
166 166
 		}
167 167
 
168
-		if ( $this->is_queue_empty() ) {
168
+		if ($this->is_queue_empty()) {
169 169
 			// No data to process.
170 170
 			wp_die();
171 171
 		}
172 172
 
173
-		check_ajax_referer( $this->identifier, 'nonce' );
173
+		check_ajax_referer($this->identifier, 'nonce');
174 174
 
175 175
 		$this->handle();
176 176
 
@@ -188,12 +188,12 @@  discard block
 block discarded – undo
188 188
 		$table  = $wpdb->options;
189 189
 		$column = 'option_name';
190 190
 
191
-		if ( is_multisite() ) {
191
+		if (is_multisite()) {
192 192
 			$table  = $wpdb->sitemeta;
193 193
 			$column = 'meta_key';
194 194
 		}
195 195
 
196
-		$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
196
+		$key = $wpdb->esc_like($this->identifier.'_batch_').'%';
197 197
 
198 198
 		$count = $wpdb->get_var(
199 199
 			$wpdb->prepare(
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
 			)
203 203
 		);
204 204
 
205
-		return ( $count > 0 ) ? false : true;
205
+		return ($count > 0) ? false : true;
206 206
 	}
207 207
 
208 208
 	/**
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
 	 * in a background process.
213 213
 	 */
214 214
 	protected function is_process_running() {
215
-		if ( get_site_transient( $this->identifier . '_process_lock' ) ) {
215
+		if (get_site_transient($this->identifier.'_process_lock')) {
216 216
 			// Process already running.
217 217
 			return true;
218 218
 		}
@@ -230,10 +230,10 @@  discard block
 block discarded – undo
230 230
 	protected function lock_process() {
231 231
 		$this->start_time = time(); // Set start time of current process.
232 232
 
233
-		$lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute
234
-		$lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration );
233
+		$lock_duration = (property_exists($this, 'queue_lock_time')) ? $this->queue_lock_time : 60; // 1 minute
234
+		$lock_duration = apply_filters($this->identifier.'_queue_lock_time', $lock_duration);
235 235
 
236
-		set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration );
236
+		set_site_transient($this->identifier.'_process_lock', microtime(), $lock_duration);
237 237
 	}
238 238
 
239 239
 	/**
@@ -244,7 +244,7 @@  discard block
 block discarded – undo
244 244
 	 * @return $this
245 245
 	 */
246 246
 	protected function unlock_process() {
247
-		delete_site_transient( $this->identifier . '_process_lock' );
247
+		delete_site_transient($this->identifier.'_process_lock');
248 248
 
249 249
 		return $this;
250 250
 	}
@@ -262,14 +262,14 @@  discard block
 block discarded – undo
262 262
 		$key_column   = 'option_id';
263 263
 		$value_column = 'option_value';
264 264
 
265
-		if ( is_multisite() ) {
265
+		if (is_multisite()) {
266 266
 			$table        = $wpdb->sitemeta;
267 267
 			$column       = 'meta_key';
268 268
 			$key_column   = 'meta_id';
269 269
 			$value_column = 'meta_value';
270 270
 		}
271 271
 
272
-		$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
272
+		$key = $wpdb->esc_like($this->identifier.'_batch_').'%';
273 273
 
274 274
 		$query = $wpdb->get_row(
275 275
 			$wpdb->prepare(
@@ -280,7 +280,7 @@  discard block
 block discarded – undo
280 280
 
281 281
 		$batch       = new stdClass();
282 282
 		$batch->key  = $query->$column;
283
-		$batch->data = maybe_unserialize( $query->$value_column );
283
+		$batch->data = maybe_unserialize($query->$value_column);
284 284
 
285 285
 		return $batch;
286 286
 	}
@@ -297,33 +297,33 @@  discard block
 block discarded – undo
297 297
 		do {
298 298
 			$batch = $this->get_batch();
299 299
 
300
-			foreach ( $batch->data as $key => $value ) {
301
-				$task = $this->task( $value );
300
+			foreach ($batch->data as $key => $value) {
301
+				$task = $this->task($value);
302 302
 
303
-				if ( false !== $task ) {
304
-					$batch->data[ $key ] = $task;
303
+				if (false !== $task) {
304
+					$batch->data[$key] = $task;
305 305
 				} else {
306
-					unset( $batch->data[ $key ] );
306
+					unset($batch->data[$key]);
307 307
 				}
308 308
 
309
-				if ( $this->time_exceeded() || $this->memory_exceeded() ) {
309
+				if ($this->time_exceeded() || $this->memory_exceeded()) {
310 310
 					// Batch limits reached.
311 311
 					break;
312 312
 				}
313 313
 			}
314 314
 
315 315
 			// Update or delete current batch.
316
-			if ( ! empty( $batch->data ) ) {
317
-				$this->update( $batch->key, $batch->data );
316
+			if ( ! empty($batch->data)) {
317
+				$this->update($batch->key, $batch->data);
318 318
 			} else {
319
-				$this->delete( $batch->key );
319
+				$this->delete($batch->key);
320 320
 			}
321
-		} while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() );
321
+		} while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty());
322 322
 
323 323
 		$this->unlock_process();
324 324
 
325 325
 		// Start next batch or complete process.
326
-		if ( ! $this->is_queue_empty() ) {
326
+		if ( ! $this->is_queue_empty()) {
327 327
 			$this->dispatch();
328 328
 		} else {
329 329
 			$this->complete();
@@ -342,14 +342,14 @@  discard block
 block discarded – undo
342 342
 	 */
343 343
 	protected function memory_exceeded() {
344 344
 		$memory_limit   = $this->get_memory_limit() * 0.9; // 90% of max memory
345
-		$current_memory = memory_get_usage( true );
345
+		$current_memory = memory_get_usage(true);
346 346
 		$return         = false;
347 347
 
348
-		if ( $current_memory >= $memory_limit ) {
348
+		if ($current_memory >= $memory_limit) {
349 349
 			$return = true;
350 350
 		}
351 351
 
352
-		return apply_filters( $this->identifier . '_memory_exceeded', $return );
352
+		return apply_filters($this->identifier.'_memory_exceeded', $return);
353 353
 	}
354 354
 
355 355
 	/**
@@ -358,19 +358,19 @@  discard block
 block discarded – undo
358 358
 	 * @return int
359 359
 	 */
360 360
 	protected function get_memory_limit() {
361
-		if ( function_exists( 'ini_get' ) ) {
362
-			$memory_limit = ini_get( 'memory_limit' );
361
+		if (function_exists('ini_get')) {
362
+			$memory_limit = ini_get('memory_limit');
363 363
 		} else {
364 364
 			// Sensible default.
365 365
 			$memory_limit = '128M';
366 366
 		}
367 367
 
368
-		if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) {
368
+		if ( ! $memory_limit || - 1 === intval($memory_limit)) {
369 369
 			// Unlimited, set to 32GB.
370 370
 			$memory_limit = '32000M';
371 371
 		}
372 372
 
373
-		return wp_convert_hr_to_bytes( $memory_limit );
373
+		return wp_convert_hr_to_bytes($memory_limit);
374 374
 	}
375 375
 
376 376
 	/**
@@ -382,14 +382,14 @@  discard block
 block discarded – undo
382 382
 	 * @return bool
383 383
 	 */
384 384
 	protected function time_exceeded() {
385
-		$finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds
385
+		$finish = $this->start_time + apply_filters($this->identifier.'_default_time_limit', 20); // 20 seconds
386 386
 		$return = false;
387 387
 
388
-		if ( time() >= $finish ) {
388
+		if (time() >= $finish) {
389 389
 			$return = true;
390 390
 		}
391 391
 
392
-		return apply_filters( $this->identifier . '_time_exceeded', $return );
392
+		return apply_filters($this->identifier.'_time_exceeded', $return);
393 393
 	}
394 394
 
395 395
 	/**
@@ -412,17 +412,17 @@  discard block
 block discarded – undo
412 412
 	 *
413 413
 	 * @return mixed
414 414
 	 */
415
-	public function schedule_cron_healthcheck( $schedules ) {
416
-		$interval = apply_filters( $this->identifier . '_cron_interval', 5 );
415
+	public function schedule_cron_healthcheck($schedules) {
416
+		$interval = apply_filters($this->identifier.'_cron_interval', 5);
417 417
 
418
-		if ( property_exists( $this, 'cron_interval' ) ) {
419
-			$interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval );
418
+		if (property_exists($this, 'cron_interval')) {
419
+			$interval = apply_filters($this->identifier.'_cron_interval', $this->cron_interval);
420 420
 		}
421 421
 
422 422
 		// Adds every 5 minutes to the existing schedules.
423
-		$schedules[ $this->identifier . '_cron_interval' ] = array(
423
+		$schedules[$this->identifier.'_cron_interval'] = array(
424 424
 			'interval' => MINUTE_IN_SECONDS * $interval,
425
-			'display'  => sprintf( __( 'Every %d Minutes' ), $interval ),
425
+			'display'  => sprintf(__('Every %d Minutes'), $interval),
426 426
 		);
427 427
 
428 428
 		return $schedules;
@@ -435,12 +435,12 @@  discard block
 block discarded – undo
435 435
 	 * and data exists in the queue.
436 436
 	 */
437 437
 	public function handle_cron_healthcheck() {
438
-		if ( $this->is_process_running() ) {
438
+		if ($this->is_process_running()) {
439 439
 			// Background process already running.
440 440
 			exit;
441 441
 		}
442 442
 
443
-		if ( $this->is_queue_empty() ) {
443
+		if ($this->is_queue_empty()) {
444 444
 			// No data to process.
445 445
 			$this->clear_scheduled_event();
446 446
 			exit;
@@ -455,8 +455,8 @@  discard block
 block discarded – undo
455 455
 	 * Schedule event
456 456
 	 */
457 457
 	protected function schedule_event() {
458
-		if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
459
-			wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier );
458
+		if ( ! wp_next_scheduled($this->cron_hook_identifier)) {
459
+			wp_schedule_event(time(), $this->cron_interval_identifier, $this->cron_hook_identifier);
460 460
 		}
461 461
 	}
462 462
 
@@ -464,10 +464,10 @@  discard block
 block discarded – undo
464 464
 	 * Clear scheduled event
465 465
 	 */
466 466
 	protected function clear_scheduled_event() {
467
-		$timestamp = wp_next_scheduled( $this->cron_hook_identifier );
467
+		$timestamp = wp_next_scheduled($this->cron_hook_identifier);
468 468
 
469
-		if ( $timestamp ) {
470
-			wp_unschedule_event( $timestamp, $this->cron_hook_identifier );
469
+		if ($timestamp) {
470
+			wp_unschedule_event($timestamp, $this->cron_hook_identifier);
471 471
 		}
472 472
 	}
473 473
 
@@ -477,12 +477,12 @@  discard block
 block discarded – undo
477 477
 	 * Stop processing queue items, clear cronjob and delete batch.
478 478
 	 */
479 479
 	public function cancel_process() {
480
-		if ( ! $this->is_queue_empty() ) {
480
+		if ( ! $this->is_queue_empty()) {
481 481
 			$batch = $this->get_batch();
482 482
 
483
-			$this->delete( $batch->key );
483
+			$this->delete($batch->key);
484 484
 
485
-			wp_clear_scheduled_hook( $this->cron_hook_identifier );
485
+			wp_clear_scheduled_hook($this->cron_hook_identifier);
486 486
 		}
487 487
 
488 488
 	}
@@ -499,6 +499,6 @@  discard block
 block discarded – undo
499 499
 	 *
500 500
 	 * @return mixed
501 501
 	 */
502
-	abstract protected function task( $item );
502
+	abstract protected function task($item);
503 503
 
504 504
 }
Please login to merge, or discard this patch.
src/js/dist/gutenberg-faq-plugin.asset.php 2 patches
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php return array(
2
-	'dependencies' => array( 'wp-polyfill', 'wp-rich-text' ),
3
-	'version'      => '18770cde607d785a81b65ef98e409669',
2
+    'dependencies' => array( 'wp-polyfill', 'wp-rich-text' ),
3
+    'version'      => '18770cde607d785a81b65ef98e409669',
4 4
 );
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@
 block discarded – undo
1 1
 <?php return array(
2
-	'dependencies' => array( 'wp-polyfill', 'wp-rich-text' ),
2
+	'dependencies' => array('wp-polyfill', 'wp-rich-text'),
3 3
 	'version'      => '18770cde607d785a81b65ef98e409669',
4 4
 );
Please login to merge, or discard this patch.