Completed
Push — master ( caa748...46d5e9 )
by David
01:05
created
src/includes/class-wordlift-entity-link-service.php 2 patches
Indentation   +184 added lines, -184 removed lines patch added patch discarded remove patch
@@ -25,194 +25,194 @@
 block discarded – undo
25 25
  */
26 26
 class Wordlift_Entity_Link_Service {
27 27
 
28
-	/**
29
-	 * The entity type service.
30
-	 *
31
-	 * @since  3.6.0
32
-	 * @access private
33
-	 * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service.
34
-	 */
35
-	private $entity_type_service;
36
-
37
-	/**
38
-	 * The entity post type slug.
39
-	 *
40
-	 * @since  3.6.0
41
-	 * @access private
42
-	 * @var string $slug The entity post type slug.
43
-	 */
44
-	private $slug;
45
-
46
-	/**
47
-	 * A logger instance.
48
-	 *
49
-	 * @since  3.6.0
50
-	 * @access private
51
-	 * @var Wordlift_Log_Service
52
-	 */
53
-	private $log;
54
-
55
-	/**
56
-	 * Wordlift_Entity_Link_Service constructor.
57
-	 *
58
-	 * @since 3.6.0
59
-	 *
60
-	 * @param Wordlift_Entity_Post_Type_Service $entity_type_service
61
-	 * @param string                            $slug The entity post type slug.
62
-	 */
63
-	public function __construct( $entity_type_service, $slug ) {
64
-
65
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
66
-
67
-		$this->entity_type_service = $entity_type_service;
68
-		$this->slug                = $slug;
69
-
70
-	}
71
-
72
-	/**
73
-	 * Intercept link generation to posts in order to customize links to entities.
74
-	 *
75
-	 * @since 3.6.0
76
-	 *
77
-	 * @param string  $post_link The post's permalink.
78
-	 * @param WP_Post $post      The post in question.
79
-	 *
80
-	 * @return string The link to the post.
81
-	 */
82
-	public function post_type_link( $post_link, $post ) {
83
-
84
-		// Return the post link if this is not our post type.
85
-		if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
86
-			return $post_link;
87
-		}
88
-
89
-		// Replace /slug/post_name/ with /post_name/
90
-		// The slug comes from the Entity Type Service since that service is responsible for registering the default
91
-		// slug.
92
-		return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
93
-	}
94
-
95
-	/**
96
-	 * Alter the query to look for our own custom type.
97
-	 *
98
-	 * @since 3.6.0
99
-	 *
100
-	 * @param WP_Query $query
101
-	 */
102
-	public function pre_get_posts( $query ) {
103
-
104
-		// If a slug has been set, we don't need to alter the query.
105
-		if ( ! empty( $this->slug ) ) {
106
-			return;
107
-		}
108
-
109
-		// Check if it's a query we should extend with our own custom post type.
110
-		//
111
-		// The `$query->query` count could be > 2 if the preview parameter is passed too.
112
-		//
113
-		// See https://github.com/insideout10/wordlift-plugin/issues/439
114
-		if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
115
-			return;
116
-		}
117
-
118
-		// Add our own post type to the query.
119
-		$post_types = '' === $query->get( 'post_type' )
120
-			? Wordlift_Entity_Service::valid_entity_post_types()
121
-			: array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() );
122
-		$query->set( 'post_type', $post_types );
123
-
124
-	}
125
-
126
-	/**
127
-	 * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved.
128
-	 *
129
-	 * @since 3.6.0
130
-	 *
131
-	 * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
132
-	 * @param string $slug      The post slug.
133
-	 * @param string $post_type Post type.
134
-	 *
135
-	 * @return bool Whether the slug is bad.
136
-	 */
137
-	public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
138
-
139
-		// The list of post types that might have conflicting slugs.
140
-		$post_types = Wordlift_Entity_Service::valid_entity_post_types();
141
-
142
-		// Ignore post types different from the ones we need to check.
143
-		if ( ! in_array( $post_type, $post_types, true ) ) {
144
-			return $bad_slug;
145
-		}
146
-
147
-		// We remove the request post type since WordPress is already checking that the slug doesn't conflict.
148
-		$exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
149
-
150
-		$this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' );
151
-
152
-		return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type );
153
-	}
154
-
155
-	/**
156
-	 * Check whether a slug exists already for the specified post types.
157
-	 *
158
-	 * @since 3.6.0
159
-	 *
160
-	 * @param string $slug       The slug.
161
-	 * @param array  $post_types An array of post types.
162
-	 *
163
-	 * @return bool True if the slug exists, otherwise false.
164
-	 */
165
-	private function slug_exists( $slug, $post_types ) {
166
-		global $wpdb;
167
-
168
-		// Loop through all post types and check
169
-		// whether they have archive pages and if
170
-		// the archive slug matches the post slug.
171
-		//
172
-		// Note that the condition below checks only post types used by WordLift.
173
-		// We don't check other post types for archive pages,
174
-		// because this is a job of WordPress.
175
-		//
176
-		// There is a open ticket that should solve this, when it's merged:
177
-		// https://core.trac.wordpress.org/ticket/13459
178
-		$all_post_types = Wordlift_Entity_Service::valid_entity_post_types();
179
-		foreach ( $all_post_types as $post_type ) {
180
-
181
-			// Get the post type object for current post type.
182
-			$post_type_object = get_post_type_object( $post_type );
183
-
184
-			if (
185
-				// Check whether the post type object is not empty.
186
-				! empty( $post_type_object ) &&
187
-				// And the post type has archive page.
188
-				$post_type_object->has_archive &&
189
-				// And `rewrite` options exists..
190
-				! empty( $post_type_object->rewrite ) &&
191
-				// And the `rewrite` slug property is not empty.
192
-				! empty( $post_type_object->rewrite['slug'] ) &&
193
-				// And if the rewrite slug equals to the slug.
194
-				$post_type_object->rewrite['slug'] === $slug
195
-			) {
196
-				// Return true which means that the slug is already in use.
197
-				return true;
198
-			}
199
-		}
200
-
201
-		return null !== $wpdb->get_var(
202
-			$wpdb->prepare(
203
-				sprintf(
204
-					"SELECT post_name
28
+    /**
29
+     * The entity type service.
30
+     *
31
+     * @since  3.6.0
32
+     * @access private
33
+     * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service.
34
+     */
35
+    private $entity_type_service;
36
+
37
+    /**
38
+     * The entity post type slug.
39
+     *
40
+     * @since  3.6.0
41
+     * @access private
42
+     * @var string $slug The entity post type slug.
43
+     */
44
+    private $slug;
45
+
46
+    /**
47
+     * A logger instance.
48
+     *
49
+     * @since  3.6.0
50
+     * @access private
51
+     * @var Wordlift_Log_Service
52
+     */
53
+    private $log;
54
+
55
+    /**
56
+     * Wordlift_Entity_Link_Service constructor.
57
+     *
58
+     * @since 3.6.0
59
+     *
60
+     * @param Wordlift_Entity_Post_Type_Service $entity_type_service
61
+     * @param string                            $slug The entity post type slug.
62
+     */
63
+    public function __construct( $entity_type_service, $slug ) {
64
+
65
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
66
+
67
+        $this->entity_type_service = $entity_type_service;
68
+        $this->slug                = $slug;
69
+
70
+    }
71
+
72
+    /**
73
+     * Intercept link generation to posts in order to customize links to entities.
74
+     *
75
+     * @since 3.6.0
76
+     *
77
+     * @param string  $post_link The post's permalink.
78
+     * @param WP_Post $post      The post in question.
79
+     *
80
+     * @return string The link to the post.
81
+     */
82
+    public function post_type_link( $post_link, $post ) {
83
+
84
+        // Return the post link if this is not our post type.
85
+        if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
86
+            return $post_link;
87
+        }
88
+
89
+        // Replace /slug/post_name/ with /post_name/
90
+        // The slug comes from the Entity Type Service since that service is responsible for registering the default
91
+        // slug.
92
+        return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
93
+    }
94
+
95
+    /**
96
+     * Alter the query to look for our own custom type.
97
+     *
98
+     * @since 3.6.0
99
+     *
100
+     * @param WP_Query $query
101
+     */
102
+    public function pre_get_posts( $query ) {
103
+
104
+        // If a slug has been set, we don't need to alter the query.
105
+        if ( ! empty( $this->slug ) ) {
106
+            return;
107
+        }
108
+
109
+        // Check if it's a query we should extend with our own custom post type.
110
+        //
111
+        // The `$query->query` count could be > 2 if the preview parameter is passed too.
112
+        //
113
+        // See https://github.com/insideout10/wordlift-plugin/issues/439
114
+        if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
115
+            return;
116
+        }
117
+
118
+        // Add our own post type to the query.
119
+        $post_types = '' === $query->get( 'post_type' )
120
+            ? Wordlift_Entity_Service::valid_entity_post_types()
121
+            : array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() );
122
+        $query->set( 'post_type', $post_types );
123
+
124
+    }
125
+
126
+    /**
127
+     * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved.
128
+     *
129
+     * @since 3.6.0
130
+     *
131
+     * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
132
+     * @param string $slug      The post slug.
133
+     * @param string $post_type Post type.
134
+     *
135
+     * @return bool Whether the slug is bad.
136
+     */
137
+    public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
138
+
139
+        // The list of post types that might have conflicting slugs.
140
+        $post_types = Wordlift_Entity_Service::valid_entity_post_types();
141
+
142
+        // Ignore post types different from the ones we need to check.
143
+        if ( ! in_array( $post_type, $post_types, true ) ) {
144
+            return $bad_slug;
145
+        }
146
+
147
+        // We remove the request post type since WordPress is already checking that the slug doesn't conflict.
148
+        $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
149
+
150
+        $this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' );
151
+
152
+        return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type );
153
+    }
154
+
155
+    /**
156
+     * Check whether a slug exists already for the specified post types.
157
+     *
158
+     * @since 3.6.0
159
+     *
160
+     * @param string $slug       The slug.
161
+     * @param array  $post_types An array of post types.
162
+     *
163
+     * @return bool True if the slug exists, otherwise false.
164
+     */
165
+    private function slug_exists( $slug, $post_types ) {
166
+        global $wpdb;
167
+
168
+        // Loop through all post types and check
169
+        // whether they have archive pages and if
170
+        // the archive slug matches the post slug.
171
+        //
172
+        // Note that the condition below checks only post types used by WordLift.
173
+        // We don't check other post types for archive pages,
174
+        // because this is a job of WordPress.
175
+        //
176
+        // There is a open ticket that should solve this, when it's merged:
177
+        // https://core.trac.wordpress.org/ticket/13459
178
+        $all_post_types = Wordlift_Entity_Service::valid_entity_post_types();
179
+        foreach ( $all_post_types as $post_type ) {
180
+
181
+            // Get the post type object for current post type.
182
+            $post_type_object = get_post_type_object( $post_type );
183
+
184
+            if (
185
+                // Check whether the post type object is not empty.
186
+                ! empty( $post_type_object ) &&
187
+                // And the post type has archive page.
188
+                $post_type_object->has_archive &&
189
+                // And `rewrite` options exists..
190
+                ! empty( $post_type_object->rewrite ) &&
191
+                // And the `rewrite` slug property is not empty.
192
+                ! empty( $post_type_object->rewrite['slug'] ) &&
193
+                // And if the rewrite slug equals to the slug.
194
+                $post_type_object->rewrite['slug'] === $slug
195
+            ) {
196
+                // Return true which means that the slug is already in use.
197
+                return true;
198
+            }
199
+        }
200
+
201
+        return null !== $wpdb->get_var(
202
+            $wpdb->prepare(
203
+                sprintf(
204
+                    "SELECT post_name
205 205
 			FROM $wpdb->posts
206 206
 			WHERE post_name = %s
207 207
 			AND post_type IN (%s)
208 208
 			LIMIT 1
209 209
 			",
210
-					'%s',
211
-					implode( ',', array_fill( 0, count( $post_types ), '%s' ) )
212
-				),
213
-				array_merge( array( $slug ), $post_types )
214
-			)
215
-		);
216
-	}
210
+                    '%s',
211
+                    implode( ',', array_fill( 0, count( $post_types ), '%s' ) )
212
+                ),
213
+                array_merge( array( $slug ), $post_types )
214
+            )
215
+        );
216
+    }
217 217
 
218 218
 }
Please login to merge, or discard this patch.
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -60,9 +60,9 @@  discard block
 block discarded – undo
60 60
 	 * @param Wordlift_Entity_Post_Type_Service $entity_type_service
61 61
 	 * @param string                            $slug The entity post type slug.
62 62
 	 */
63
-	public function __construct( $entity_type_service, $slug ) {
63
+	public function __construct($entity_type_service, $slug) {
64 64
 
65
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
65
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Link_Service');
66 66
 
67 67
 		$this->entity_type_service = $entity_type_service;
68 68
 		$this->slug                = $slug;
@@ -79,17 +79,17 @@  discard block
 block discarded – undo
79 79
 	 *
80 80
 	 * @return string The link to the post.
81 81
 	 */
82
-	public function post_type_link( $post_link, $post ) {
82
+	public function post_type_link($post_link, $post) {
83 83
 
84 84
 		// Return the post link if this is not our post type.
85
-		if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
85
+		if ( ! empty($this->slug) || $this->entity_type_service->get_post_type() !== get_post_type($post)) {
86 86
 			return $post_link;
87 87
 		}
88 88
 
89 89
 		// Replace /slug/post_name/ with /post_name/
90 90
 		// The slug comes from the Entity Type Service since that service is responsible for registering the default
91 91
 		// slug.
92
-		return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
92
+		return str_replace("/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link);
93 93
 	}
94 94
 
95 95
 	/**
@@ -99,10 +99,10 @@  discard block
 block discarded – undo
99 99
 	 *
100 100
 	 * @param WP_Query $query
101 101
 	 */
102
-	public function pre_get_posts( $query ) {
102
+	public function pre_get_posts($query) {
103 103
 
104 104
 		// If a slug has been set, we don't need to alter the query.
105
-		if ( ! empty( $this->slug ) ) {
105
+		if ( ! empty($this->slug)) {
106 106
 			return;
107 107
 		}
108 108
 
@@ -111,15 +111,15 @@  discard block
 block discarded – undo
111 111
 		// The `$query->query` count could be > 2 if the preview parameter is passed too.
112 112
 		//
113 113
 		// See https://github.com/insideout10/wordlift-plugin/issues/439
114
-		if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
114
+		if ( ! $query->is_main_query() || 2 > count($query->query) || ! isset($query->query['page']) || empty($query->query['name'])) {
115 115
 			return;
116 116
 		}
117 117
 
118 118
 		// Add our own post type to the query.
119
-		$post_types = '' === $query->get( 'post_type' )
119
+		$post_types = '' === $query->get('post_type')
120 120
 			? Wordlift_Entity_Service::valid_entity_post_types()
121
-			: array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() );
122
-		$query->set( 'post_type', $post_types );
121
+			: array_merge((array) $query->get('post_type'), (array) $this->entity_type_service->get_post_type());
122
+		$query->set('post_type', $post_types);
123 123
 
124 124
 	}
125 125
 
@@ -134,22 +134,22 @@  discard block
 block discarded – undo
134 134
 	 *
135 135
 	 * @return bool Whether the slug is bad.
136 136
 	 */
137
-	public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
137
+	public function wp_unique_post_slug_is_bad_flat_slug($bad_slug, $slug, $post_type) {
138 138
 
139 139
 		// The list of post types that might have conflicting slugs.
140 140
 		$post_types = Wordlift_Entity_Service::valid_entity_post_types();
141 141
 
142 142
 		// Ignore post types different from the ones we need to check.
143
-		if ( ! in_array( $post_type, $post_types, true ) ) {
143
+		if ( ! in_array($post_type, $post_types, true)) {
144 144
 			return $bad_slug;
145 145
 		}
146 146
 
147 147
 		// We remove the request post type since WordPress is already checking that the slug doesn't conflict.
148
-		$exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
148
+		$exists = $this->slug_exists($slug, array_diff($post_types, array($post_type)));
149 149
 
150
-		$this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' );
150
+		$this->log->debug("Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: ".($exists ? 'yes' : 'no').' ]');
151 151
 
152
-		return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type );
152
+		return apply_filters('wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type);
153 153
 	}
154 154
 
155 155
 	/**
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 	 *
163 163
 	 * @return bool True if the slug exists, otherwise false.
164 164
 	 */
165
-	private function slug_exists( $slug, $post_types ) {
165
+	private function slug_exists($slug, $post_types) {
166 166
 		global $wpdb;
167 167
 
168 168
 		// Loop through all post types and check
@@ -176,20 +176,20 @@  discard block
 block discarded – undo
176 176
 		// There is a open ticket that should solve this, when it's merged:
177 177
 		// https://core.trac.wordpress.org/ticket/13459
178 178
 		$all_post_types = Wordlift_Entity_Service::valid_entity_post_types();
179
-		foreach ( $all_post_types as $post_type ) {
179
+		foreach ($all_post_types as $post_type) {
180 180
 
181 181
 			// Get the post type object for current post type.
182
-			$post_type_object = get_post_type_object( $post_type );
182
+			$post_type_object = get_post_type_object($post_type);
183 183
 
184 184
 			if (
185 185
 				// Check whether the post type object is not empty.
186
-				! empty( $post_type_object ) &&
186
+				! empty($post_type_object) &&
187 187
 				// And the post type has archive page.
188 188
 				$post_type_object->has_archive &&
189 189
 				// And `rewrite` options exists..
190
-				! empty( $post_type_object->rewrite ) &&
190
+				! empty($post_type_object->rewrite) &&
191 191
 				// And the `rewrite` slug property is not empty.
192
-				! empty( $post_type_object->rewrite['slug'] ) &&
192
+				! empty($post_type_object->rewrite['slug']) &&
193 193
 				// And if the rewrite slug equals to the slug.
194 194
 				$post_type_object->rewrite['slug'] === $slug
195 195
 			) {
@@ -208,9 +208,9 @@  discard block
 block discarded – undo
208 208
 			LIMIT 1
209 209
 			",
210 210
 					'%s',
211
-					implode( ',', array_fill( 0, count( $post_types ), '%s' ) )
211
+					implode(',', array_fill(0, count($post_types), '%s'))
212 212
 				),
213
-				array_merge( array( $slug ), $post_types )
213
+				array_merge(array($slug), $post_types)
214 214
 			)
215 215
 		);
216 216
 	}
Please login to merge, or discard this patch.
src/includes/batch/class-wordlift-batch-operation-ajax-adapter.php 2 patches
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -14,143 +14,143 @@
 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
-	/**
120
-	 * Count the number of elements that would be affected by the operation.
121
-	 *
122
-	 * @since 3.20.0
123
-	 */
124
-	public function count() {
125
-
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.' );
130
-		}
131
-
132
-		// Run the batch operation.
133
-		$result = $this->operation->count();
134
-
135
-		// Send the results along with a potentially updated nonce.
136
-		wp_send_json_success(
137
-			array(
138
-				'count'  => $result,
139
-				'_nonce' => $this->create_nonce(),
140
-			)
141
-		);
142
-
143
-	}
144
-
145
-	/**
146
-	 * Create a nonce for the ajax operation.
147
-	 *
148
-	 * @return string The nonce.
149
-	 * @since 3.20.0
150
-	 */
151
-	public function create_nonce() {
152
-
153
-		return wp_create_nonce( $this->action );
154
-	}
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
+    /**
120
+     * Count the number of elements that would be affected by the operation.
121
+     *
122
+     * @since 3.20.0
123
+     */
124
+    public function count() {
125
+
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.' );
130
+        }
131
+
132
+        // Run the batch operation.
133
+        $result = $this->operation->count();
134
+
135
+        // Send the results along with a potentially updated nonce.
136
+        wp_send_json_success(
137
+            array(
138
+                'count'  => $result,
139
+                '_nonce' => $this->create_nonce(),
140
+            )
141
+        );
142
+
143
+    }
144
+
145
+    /**
146
+     * Create a nonce for the ajax operation.
147
+     *
148
+     * @return string The nonce.
149
+     * @since 3.20.0
150
+     */
151
+    public function create_nonce() {
152
+
153
+        return wp_create_nonce( $this->action );
154
+    }
155 155
 
156 156
 }
Please login to merge, or discard this patch.
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.
src/includes/class-wordlift-remote-image-service.php 2 patches
Indentation   +144 added lines, -144 removed lines patch added patch discarded remove patch
@@ -8,149 +8,149 @@
 block discarded – undo
8 8
  */
9 9
 class Wordlift_Remote_Image_Service {
10 10
 
11
-	/**
12
-	 * Save the image with the specified URL locally.
13
-	 *
14
-	 * @param string $url The image remote URL.
15
-	 *
16
-	 * @return array|false|WP_Error An array with information about the saved image (*path*: the local path to the image, *url*: the local
17
-	 * url, *content_type*: the image content type) or false on error.
18
-	 * @since 3.18.0
19
-	 * @since 3.23.4 the function may return a WP_Error.
20
-	 */
21
-	public static function save_from_url( $url ) {
22
-
23
-		// Required for REST API calls
24
-		if ( ! function_exists( 'WP_Filesystem' ) ) {
25
-			require_once ABSPATH . 'wp-admin/includes/file.php';
26
-		}
27
-
28
-		// Load `WP_Filesystem`.
29
-		add_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' );
30
-		WP_Filesystem();
31
-		global $wp_filesystem;
32
-
33
-		// Parse the url.
34
-		$parts = wp_parse_url( $url );
35
-
36
-		// Get the bare filename (filename w/o the extension).
37
-		$basename = str_replace(
38
-			DIRECTORY_SEPARATOR,
39
-			'_',
40
-			rawurldecode(
41
-				pathinfo( $parts['path'], PATHINFO_FILENAME )
42
-			)
43
-		);
44
-
45
-		// Get the base dir.
46
-		$wp_upload_dir = wp_upload_dir();
47
-
48
-		// Set the upload directory and URL.
49
-		$upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir'];
50
-		$upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir'];
51
-
52
-		// Get the full path to the local filename.
53
-		$image_full_path = $upload_dir . '/' . $basename;
54
-		$image_full_url  = $upload_url . '/' . $basename;
55
-
56
-		// Create custom directory and bail on failure.
57
-		if ( ! wp_mkdir_p( $upload_dir ) ) {
58
-			Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' )
59
-								->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" );
60
-
61
-			return new WP_Error( 'image_error', "save_image_from_url : failed creating upload dir $upload_dir \n" );
62
-		};
63
-
64
-		$response = wp_remote_get(
65
-			$url,
66
-			array(
67
-				'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
68
-			)
69
-		);
70
-
71
-		// Bail if the response is not set.
72
-		if ( self::is_response_error( $response ) ) {
73
-			Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' )
74
-								// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
75
-								->warn( "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) );
76
-
77
-			// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
78
-			return new WP_Error( 'image_error', "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) );
79
-		}
80
-
81
-		// Get the content type of response.
82
-		$content_type = wp_remote_retrieve_header( $response, 'content-type' );
83
-
84
-		// Get the file extension.
85
-		$extension = self::get_extension_from_content_type( $content_type );
86
-
87
-		// Bail if the content type is not supported.
88
-		if ( empty( $extension ) ) {
89
-			// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
90
-			return new WP_Error( 'image_error', "Unsupported content type [ $content_type ]:\n" . var_export( $response, true ) );
91
-		}
92
-
93
-		// Complete the local filename.
94
-		$image_full_path .= $extension;
95
-		$image_full_url  .= $extension;
96
-
97
-		// Store the data locally.
98
-		$wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) );
99
-		remove_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' );
100
-
101
-		// Return the path.
102
-		return array(
103
-			'path'         => $image_full_path,
104
-			'url'          => $image_full_url,
105
-			'content_type' => $content_type,
106
-		);
107
-	}
108
-
109
-	// phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
110
-	public static function _return_direct() {
111
-		return 'direct';
112
-	}
113
-
114
-	/**
115
-	 * Returns the file extension using the content type.
116
-	 *
117
-	 * @param string $content_type File content type.
118
-	 *
119
-	 * @return string|bool The file extension on success and
120
-	 * false on fail or if the content type is not supported.
121
-	 * @since 3.18.0
122
-	 */
123
-	private static function get_extension_from_content_type( $content_type ) {
124
-
125
-		// Return the extension if match.
126
-		switch ( $content_type ) {
127
-			case 'image/jpeg':
128
-			case 'image/jpg':
129
-				return '.jpg';
130
-			case 'image/gif':
131
-				return '.gif';
132
-			case 'image/png':
133
-				return '.png';
134
-		}
135
-
136
-		// Otherwise return false.
137
-		return false;
138
-	}
139
-
140
-	/**
141
-	 * Checks whether a response is an error.
142
-	 *
143
-	 * @param array|WP_Error $response The response.
144
-	 *
145
-	 * @return bool True if the response is an error, otherwise false.
146
-	 * @since 3.23.4
147
-	 */
148
-	private static function is_response_error( $response ) {
149
-
150
-		return ( is_wp_error( $response )
151
-				 || 200 !== (int) $response['response']['code']
152
-				 || ! isset( $response['body'] )
153
-		);
154
-	}
11
+    /**
12
+     * Save the image with the specified URL locally.
13
+     *
14
+     * @param string $url The image remote URL.
15
+     *
16
+     * @return array|false|WP_Error An array with information about the saved image (*path*: the local path to the image, *url*: the local
17
+     * url, *content_type*: the image content type) or false on error.
18
+     * @since 3.18.0
19
+     * @since 3.23.4 the function may return a WP_Error.
20
+     */
21
+    public static function save_from_url( $url ) {
22
+
23
+        // Required for REST API calls
24
+        if ( ! function_exists( 'WP_Filesystem' ) ) {
25
+            require_once ABSPATH . 'wp-admin/includes/file.php';
26
+        }
27
+
28
+        // Load `WP_Filesystem`.
29
+        add_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' );
30
+        WP_Filesystem();
31
+        global $wp_filesystem;
32
+
33
+        // Parse the url.
34
+        $parts = wp_parse_url( $url );
35
+
36
+        // Get the bare filename (filename w/o the extension).
37
+        $basename = str_replace(
38
+            DIRECTORY_SEPARATOR,
39
+            '_',
40
+            rawurldecode(
41
+                pathinfo( $parts['path'], PATHINFO_FILENAME )
42
+            )
43
+        );
44
+
45
+        // Get the base dir.
46
+        $wp_upload_dir = wp_upload_dir();
47
+
48
+        // Set the upload directory and URL.
49
+        $upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir'];
50
+        $upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir'];
51
+
52
+        // Get the full path to the local filename.
53
+        $image_full_path = $upload_dir . '/' . $basename;
54
+        $image_full_url  = $upload_url . '/' . $basename;
55
+
56
+        // Create custom directory and bail on failure.
57
+        if ( ! wp_mkdir_p( $upload_dir ) ) {
58
+            Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' )
59
+                                ->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" );
60
+
61
+            return new WP_Error( 'image_error', "save_image_from_url : failed creating upload dir $upload_dir \n" );
62
+        };
63
+
64
+        $response = wp_remote_get(
65
+            $url,
66
+            array(
67
+                'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
68
+            )
69
+        );
70
+
71
+        // Bail if the response is not set.
72
+        if ( self::is_response_error( $response ) ) {
73
+            Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' )
74
+                                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
75
+                                ->warn( "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) );
76
+
77
+            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
78
+            return new WP_Error( 'image_error', "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) );
79
+        }
80
+
81
+        // Get the content type of response.
82
+        $content_type = wp_remote_retrieve_header( $response, 'content-type' );
83
+
84
+        // Get the file extension.
85
+        $extension = self::get_extension_from_content_type( $content_type );
86
+
87
+        // Bail if the content type is not supported.
88
+        if ( empty( $extension ) ) {
89
+            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
90
+            return new WP_Error( 'image_error', "Unsupported content type [ $content_type ]:\n" . var_export( $response, true ) );
91
+        }
92
+
93
+        // Complete the local filename.
94
+        $image_full_path .= $extension;
95
+        $image_full_url  .= $extension;
96
+
97
+        // Store the data locally.
98
+        $wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) );
99
+        remove_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' );
100
+
101
+        // Return the path.
102
+        return array(
103
+            'path'         => $image_full_path,
104
+            'url'          => $image_full_url,
105
+            'content_type' => $content_type,
106
+        );
107
+    }
108
+
109
+    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
110
+    public static function _return_direct() {
111
+        return 'direct';
112
+    }
113
+
114
+    /**
115
+     * Returns the file extension using the content type.
116
+     *
117
+     * @param string $content_type File content type.
118
+     *
119
+     * @return string|bool The file extension on success and
120
+     * false on fail or if the content type is not supported.
121
+     * @since 3.18.0
122
+     */
123
+    private static function get_extension_from_content_type( $content_type ) {
124
+
125
+        // Return the extension if match.
126
+        switch ( $content_type ) {
127
+            case 'image/jpeg':
128
+            case 'image/jpg':
129
+                return '.jpg';
130
+            case 'image/gif':
131
+                return '.gif';
132
+            case 'image/png':
133
+                return '.png';
134
+        }
135
+
136
+        // Otherwise return false.
137
+        return false;
138
+    }
139
+
140
+    /**
141
+     * Checks whether a response is an error.
142
+     *
143
+     * @param array|WP_Error $response The response.
144
+     *
145
+     * @return bool True if the response is an error, otherwise false.
146
+     * @since 3.23.4
147
+     */
148
+    private static function is_response_error( $response ) {
149
+
150
+        return ( is_wp_error( $response )
151
+                 || 200 !== (int) $response['response']['code']
152
+                 || ! isset( $response['body'] )
153
+        );
154
+    }
155 155
 
156 156
 }
Please login to merge, or discard this patch.
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -18,27 +18,27 @@  discard block
 block discarded – undo
18 18
 	 * @since 3.18.0
19 19
 	 * @since 3.23.4 the function may return a WP_Error.
20 20
 	 */
21
-	public static function save_from_url( $url ) {
21
+	public static function save_from_url($url) {
22 22
 
23 23
 		// Required for REST API calls
24
-		if ( ! function_exists( 'WP_Filesystem' ) ) {
25
-			require_once ABSPATH . 'wp-admin/includes/file.php';
24
+		if ( ! function_exists('WP_Filesystem')) {
25
+			require_once ABSPATH.'wp-admin/includes/file.php';
26 26
 		}
27 27
 
28 28
 		// Load `WP_Filesystem`.
29
-		add_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' );
29
+		add_filter('filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct');
30 30
 		WP_Filesystem();
31 31
 		global $wp_filesystem;
32 32
 
33 33
 		// Parse the url.
34
-		$parts = wp_parse_url( $url );
34
+		$parts = wp_parse_url($url);
35 35
 
36 36
 		// Get the bare filename (filename w/o the extension).
37 37
 		$basename = str_replace(
38 38
 			DIRECTORY_SEPARATOR,
39 39
 			'_',
40 40
 			rawurldecode(
41
-				pathinfo( $parts['path'], PATHINFO_FILENAME )
41
+				pathinfo($parts['path'], PATHINFO_FILENAME)
42 42
 			)
43 43
 		);
44 44
 
@@ -46,19 +46,19 @@  discard block
 block discarded – undo
46 46
 		$wp_upload_dir = wp_upload_dir();
47 47
 
48 48
 		// Set the upload directory and URL.
49
-		$upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir'];
50
-		$upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir'];
49
+		$upload_dir = $wp_upload_dir['basedir'].'/wl'.$wp_upload_dir['subdir'];
50
+		$upload_url = $wp_upload_dir['baseurl'].'/wl'.$wp_upload_dir['subdir'];
51 51
 
52 52
 		// Get the full path to the local filename.
53
-		$image_full_path = $upload_dir . '/' . $basename;
54
-		$image_full_url  = $upload_url . '/' . $basename;
53
+		$image_full_path = $upload_dir.'/'.$basename;
54
+		$image_full_url  = $upload_url.'/'.$basename;
55 55
 
56 56
 		// Create custom directory and bail on failure.
57
-		if ( ! wp_mkdir_p( $upload_dir ) ) {
58
-			Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' )
59
-								->warn( "save_image_from_url : failed creating upload dir $upload_dir \n" );
57
+		if ( ! wp_mkdir_p($upload_dir)) {
58
+			Wordlift_Log_Service::get_logger('Wordlift_Remote_Image_Service')
59
+								->warn("save_image_from_url : failed creating upload dir $upload_dir \n");
60 60
 
61
-			return new WP_Error( 'image_error', "save_image_from_url : failed creating upload dir $upload_dir \n" );
61
+			return new WP_Error('image_error', "save_image_from_url : failed creating upload dir $upload_dir \n");
62 62
 		};
63 63
 
64 64
 		$response = wp_remote_get(
@@ -69,25 +69,25 @@  discard block
 block discarded – undo
69 69
 		);
70 70
 
71 71
 		// Bail if the response is not set.
72
-		if ( self::is_response_error( $response ) ) {
73
-			Wordlift_Log_Service::get_logger( 'Wordlift_Remote_Image_Service' )
72
+		if (self::is_response_error($response)) {
73
+			Wordlift_Log_Service::get_logger('Wordlift_Remote_Image_Service')
74 74
 								// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
75
-								->warn( "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) );
75
+								->warn("save_image_from_url : failed to fetch the response from: $url\nThe response was:\n".var_export($response, true));
76 76
 
77 77
 			// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
78
-			return new WP_Error( 'image_error', "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n" . var_export( $response, true ) );
78
+			return new WP_Error('image_error', "save_image_from_url : failed to fetch the response from: $url\nThe response was:\n".var_export($response, true));
79 79
 		}
80 80
 
81 81
 		// Get the content type of response.
82
-		$content_type = wp_remote_retrieve_header( $response, 'content-type' );
82
+		$content_type = wp_remote_retrieve_header($response, 'content-type');
83 83
 
84 84
 		// Get the file extension.
85
-		$extension = self::get_extension_from_content_type( $content_type );
85
+		$extension = self::get_extension_from_content_type($content_type);
86 86
 
87 87
 		// Bail if the content type is not supported.
88
-		if ( empty( $extension ) ) {
88
+		if (empty($extension)) {
89 89
 			// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
90
-			return new WP_Error( 'image_error', "Unsupported content type [ $content_type ]:\n" . var_export( $response, true ) );
90
+			return new WP_Error('image_error', "Unsupported content type [ $content_type ]:\n".var_export($response, true));
91 91
 		}
92 92
 
93 93
 		// Complete the local filename.
@@ -95,8 +95,8 @@  discard block
 block discarded – undo
95 95
 		$image_full_url  .= $extension;
96 96
 
97 97
 		// Store the data locally.
98
-		$wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) );
99
-		remove_filter( 'filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct' );
98
+		$wp_filesystem->put_contents($image_full_path, wp_remote_retrieve_body($response));
99
+		remove_filter('filesystem_method', 'Wordlift_Remote_Image_Service::_return_direct');
100 100
 
101 101
 		// Return the path.
102 102
 		return array(
@@ -120,10 +120,10 @@  discard block
 block discarded – undo
120 120
 	 * false on fail or if the content type is not supported.
121 121
 	 * @since 3.18.0
122 122
 	 */
123
-	private static function get_extension_from_content_type( $content_type ) {
123
+	private static function get_extension_from_content_type($content_type) {
124 124
 
125 125
 		// Return the extension if match.
126
-		switch ( $content_type ) {
126
+		switch ($content_type) {
127 127
 			case 'image/jpeg':
128 128
 			case 'image/jpg':
129 129
 				return '.jpg';
@@ -145,11 +145,11 @@  discard block
 block discarded – undo
145 145
 	 * @return bool True if the response is an error, otherwise false.
146 146
 	 * @since 3.23.4
147 147
 	 */
148
-	private static function is_response_error( $response ) {
148
+	private static function is_response_error($response) {
149 149
 
150
-		return ( is_wp_error( $response )
150
+		return (is_wp_error($response)
151 151
 				 || 200 !== (int) $response['response']['code']
152
-				 || ! isset( $response['body'] )
152
+				 || ! isset($response['body'])
153 153
 		);
154 154
 	}
155 155
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-type-service.php 2 patches
Indentation   +469 added lines, -469 removed lines patch added patch discarded remove patch
@@ -19,147 +19,147 @@  discard block
 block discarded – undo
19 19
  */
20 20
 class Wordlift_Entity_Type_Service {
21 21
 
22
-	/**
23
-	 * The {@link Wordlift_Schema_Service} instance.
24
-	 *
25
-	 * @since  3.7.0
26
-	 * @access private
27
-	 * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
28
-	 */
29
-	private $schema_service;
30
-
31
-	/**
32
-	 * A {@link Wordlift_Log_Service} instance.
33
-	 *
34
-	 * @since  3.8.0
35
-	 * @access private
36
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
37
-	 */
38
-	private $log;
39
-
40
-	/**
41
-	 * Wordlift_Entity_Type_Service constructor.
42
-	 *
43
-	 * @since 3.7.0
44
-	 */
45
-	protected function __construct() {
46
-
47
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
48
-
49
-		$this->schema_service = Wordlift_Schema_Service::get_instance();
50
-
51
-		$this->prepare_post_types();
52
-
53
-	}
54
-
55
-	/**
56
-	 * Prepare post types for Gutenberg use
57
-	 *
58
-	 * @since 3.26.0
59
-	 */
60
-	private function prepare_post_types() {
61
-
62
-		add_action(
63
-			'init',
64
-			function () {
65
-				// Add post type support for 'custom-fields' for all post types. Specifically needed in Gutenberg
66
-				$post_types = get_post_types();
67
-				foreach ( $post_types as $post_type ) {
68
-					add_post_type_support( $post_type, 'custom-fields' );
69
-				}
70
-			}
71
-		);
72
-	}
73
-
74
-	/**
75
-	 * The {@link Wordlift_Entity_Type_Service} singleton instance.
76
-	 *
77
-	 * @since  3.7.0
78
-	 * @access private
79
-	 * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance.
80
-	 */
81
-	private static $instance = null;
82
-
83
-	/**
84
-	 * Get the {@link Wordlift_Entity_Type_Service} singleton instance.
85
-	 *
86
-	 * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance.
87
-	 * @since 3.7.0
88
-	 */
89
-	public static function get_instance() {
90
-
91
-		if ( ! isset( self::$instance ) ) {
92
-			self::$instance = new self();
93
-		}
94
-
95
-		return self::$instance;
96
-	}
97
-
98
-	/**
99
-	 * Get the types associated with the specified entity post id.
100
-	 *
101
-	 * We have a strategy to define the entity type, given that everything is
102
-	 * an entity, i.e. also posts/pages and custom post types.
103
-	 *
104
-	 * @param int $post_id The post id.
105
-	 *
106
-	 * @return array|null {
107
-	 * An array of type properties or null if no term is associated
108
-	 *
109
-	 * @type string css_class     The css class, e.g. `wl-thing`.
110
-	 * @type string uri           The schema.org class URI, e.g. `http://schema.org/Thing`.
111
-	 * @type array  same_as       An array of same as attributes.
112
-	 * @type array  custom_fields An array of custom fields.
113
-	 * }
114
-	 * @since 3.33.9 The `linked_data` key has been removed.
115
-	 *
116
-	 * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0.
117
-	 *
118
-	 * @since 3.18.0 The cases are the following:
119
-	 *  1. the post has a term from the Entity Types Taxonomy: the term defines
120
-	 *     the entity type, e.g. Organization, Person, ...
121
-	 *  2. the post doesn't have a term from the Entity Types Taxonomy:
122
-	 *      a) the post is a `wl_entity` custom post type, then the post is
123
-	 *           assigned the `Thing` entity type by default.
124
-	 *      b) the post is a `post` post type, then the post is
125
-	 *           assigned the `Article` entity type by default.
126
-	 *      c) the post is a custom post type then it is
127
-	 *          assigned the `WebPage` entity type by default.
128
-	 */
129
-	public function get( $post_id ) {
130
-
131
-		$this->log->trace( "Getting the post type for post $post_id..." );
132
-
133
-		// Get the post type.
134
-		$post_type = get_post_type( $post_id );
135
-
136
-		// Return `web-page` for non entities.
137
-		if ( ! self::is_valid_entity_post_type( $post_type ) ) {
138
-			$this->log->info( "Returning `web-page` for post $post_id." );
139
-
140
-			return $this->schema_service->get_schema( 'web-page' );
141
-		}
142
-
143
-		// Get the type from the associated classification.
144
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
145
-
146
-		// Return the schema type if there is a term found.
147
-		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
148
-			// Cycle through the terms and return the first one with a valid schema.
149
-			foreach ( $terms as $term ) {
150
-				$this->log->debug( "Found `{$term->slug}` term for post $post_id." );
151
-
152
-				// Try to get the schema for the term.
153
-				$schema = $this->schema_service->get_schema( $term->slug );
154
-
155
-				// If found, return it, ignoring the other types.
156
-				if ( null !== $schema ) {
157
-					// Return the entity type with the specified id.
158
-					return $schema;
159
-				}
160
-			}
161
-
162
-			/*
22
+    /**
23
+     * The {@link Wordlift_Schema_Service} instance.
24
+     *
25
+     * @since  3.7.0
26
+     * @access private
27
+     * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
28
+     */
29
+    private $schema_service;
30
+
31
+    /**
32
+     * A {@link Wordlift_Log_Service} instance.
33
+     *
34
+     * @since  3.8.0
35
+     * @access private
36
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
37
+     */
38
+    private $log;
39
+
40
+    /**
41
+     * Wordlift_Entity_Type_Service constructor.
42
+     *
43
+     * @since 3.7.0
44
+     */
45
+    protected function __construct() {
46
+
47
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
48
+
49
+        $this->schema_service = Wordlift_Schema_Service::get_instance();
50
+
51
+        $this->prepare_post_types();
52
+
53
+    }
54
+
55
+    /**
56
+     * Prepare post types for Gutenberg use
57
+     *
58
+     * @since 3.26.0
59
+     */
60
+    private function prepare_post_types() {
61
+
62
+        add_action(
63
+            'init',
64
+            function () {
65
+                // Add post type support for 'custom-fields' for all post types. Specifically needed in Gutenberg
66
+                $post_types = get_post_types();
67
+                foreach ( $post_types as $post_type ) {
68
+                    add_post_type_support( $post_type, 'custom-fields' );
69
+                }
70
+            }
71
+        );
72
+    }
73
+
74
+    /**
75
+     * The {@link Wordlift_Entity_Type_Service} singleton instance.
76
+     *
77
+     * @since  3.7.0
78
+     * @access private
79
+     * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance.
80
+     */
81
+    private static $instance = null;
82
+
83
+    /**
84
+     * Get the {@link Wordlift_Entity_Type_Service} singleton instance.
85
+     *
86
+     * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance.
87
+     * @since 3.7.0
88
+     */
89
+    public static function get_instance() {
90
+
91
+        if ( ! isset( self::$instance ) ) {
92
+            self::$instance = new self();
93
+        }
94
+
95
+        return self::$instance;
96
+    }
97
+
98
+    /**
99
+     * Get the types associated with the specified entity post id.
100
+     *
101
+     * We have a strategy to define the entity type, given that everything is
102
+     * an entity, i.e. also posts/pages and custom post types.
103
+     *
104
+     * @param int $post_id The post id.
105
+     *
106
+     * @return array|null {
107
+     * An array of type properties or null if no term is associated
108
+     *
109
+     * @type string css_class     The css class, e.g. `wl-thing`.
110
+     * @type string uri           The schema.org class URI, e.g. `http://schema.org/Thing`.
111
+     * @type array  same_as       An array of same as attributes.
112
+     * @type array  custom_fields An array of custom fields.
113
+     * }
114
+     * @since 3.33.9 The `linked_data` key has been removed.
115
+     *
116
+     * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0.
117
+     *
118
+     * @since 3.18.0 The cases are the following:
119
+     *  1. the post has a term from the Entity Types Taxonomy: the term defines
120
+     *     the entity type, e.g. Organization, Person, ...
121
+     *  2. the post doesn't have a term from the Entity Types Taxonomy:
122
+     *      a) the post is a `wl_entity` custom post type, then the post is
123
+     *           assigned the `Thing` entity type by default.
124
+     *      b) the post is a `post` post type, then the post is
125
+     *           assigned the `Article` entity type by default.
126
+     *      c) the post is a custom post type then it is
127
+     *          assigned the `WebPage` entity type by default.
128
+     */
129
+    public function get( $post_id ) {
130
+
131
+        $this->log->trace( "Getting the post type for post $post_id..." );
132
+
133
+        // Get the post type.
134
+        $post_type = get_post_type( $post_id );
135
+
136
+        // Return `web-page` for non entities.
137
+        if ( ! self::is_valid_entity_post_type( $post_type ) ) {
138
+            $this->log->info( "Returning `web-page` for post $post_id." );
139
+
140
+            return $this->schema_service->get_schema( 'web-page' );
141
+        }
142
+
143
+        // Get the type from the associated classification.
144
+        $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
145
+
146
+        // Return the schema type if there is a term found.
147
+        if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
148
+            // Cycle through the terms and return the first one with a valid schema.
149
+            foreach ( $terms as $term ) {
150
+                $this->log->debug( "Found `{$term->slug}` term for post $post_id." );
151
+
152
+                // Try to get the schema for the term.
153
+                $schema = $this->schema_service->get_schema( $term->slug );
154
+
155
+                // If found, return it, ignoring the other types.
156
+                if ( null !== $schema ) {
157
+                    // Return the entity type with the specified id.
158
+                    return $schema;
159
+                }
160
+            }
161
+
162
+            /*
163 163
 			 * When a schema isn't found, we return `thing`. Schema may not be found because
164 164
 			 * the new schema classes that we support since #852 aren't configured in the schema
165 165
 			 * service.
@@ -169,93 +169,93 @@  discard block
 block discarded – undo
169 169
 			 * @since 3.20.0
170 170
 			 */
171 171
 
172
-			return $this->schema_service->get_schema( 'thing' );
173
-		}
174
-
175
-		// If it's a page or post return `Article`.
176
-		if ( in_array( $post_type, array( 'post', 'page' ), true ) ) {
177
-			$this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
178
-
179
-			// Return "Article" schema type for posts.
180
-			return $this->schema_service->get_schema( 'article' );
181
-		}
182
-
183
-		// Return "Thing" schema type for entities.
184
-		$this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
185
-
186
-		// Return the entity type with the specified id.
187
-		return $this->schema_service->get_schema( 'thing' );
188
-
189
-	}
190
-
191
-	/**
192
-	 * Get the term ids of the entity types associated to the specified post.
193
-	 *
194
-	 * @param int $post_id The post id.
195
-	 *
196
-	 * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
197
-	 * @since 3.20.0
198
-	 */
199
-	public function get_ids( $post_id ) {
200
-
201
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids' ) );
202
-	}
203
-
204
-	/**
205
-	 * Get the camel case names of the entity types associated to the specified post.
206
-	 *
207
-	 * @param int $post_id The post id.
208
-	 *
209
-	 * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
210
-	 * @since 3.20.0
211
-	 */
212
-	public function get_names( $post_id ) {
213
-
214
-		$ids = $this->get_ids( $post_id );
215
-
216
-		// Filter out invalid terms (ones without _wl_name term meta)
217
-		return array_values(
218
-			array_filter(
219
-				array_map(
220
-					function ( $id ) {
221
-						return get_term_meta( $id, '_wl_name', true );
222
-					},
223
-					$ids
224
-				)
225
-			)
226
-		);
227
-	}
228
-
229
-	/**
230
-	 * Set the main type for the specified entity post, given the type URI.
231
-	 *
232
-	 * @param int    $post_id The post id.
233
-	 * @param string $type_uri The type URI.
234
-	 * @param bool   $replace Whether the provided type must replace the existing types, by default `true`.
235
-	 *
236
-	 * @since 3.8.0
237
-	 */
238
-	public function set( $post_id, $type_uri, $replace = true ) {
239
-
240
-		// If the type URI is empty we remove the type.
241
-		if ( empty( $type_uri ) ) {
242
-			$this->log->debug( "Removing entity type for post $post_id..." );
243
-
244
-			wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
245
-
246
-			return;
247
-		}
248
-
249
-		$this->log->debug( "Setting entity type for post $post_id..." );
250
-
251
-		// if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
252
-		$term = ( 0 === strpos( $type_uri, 'wl-' ) )
253
-			// Get term by slug.
254
-			? $this->get_term_by_slug( substr( $type_uri, 3 ) )
255
-			// Get term by URI.
256
-			: $this->get_term_by_uri( $type_uri );
257
-
258
-		/*
172
+            return $this->schema_service->get_schema( 'thing' );
173
+        }
174
+
175
+        // If it's a page or post return `Article`.
176
+        if ( in_array( $post_type, array( 'post', 'page' ), true ) ) {
177
+            $this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
178
+
179
+            // Return "Article" schema type for posts.
180
+            return $this->schema_service->get_schema( 'article' );
181
+        }
182
+
183
+        // Return "Thing" schema type for entities.
184
+        $this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
185
+
186
+        // Return the entity type with the specified id.
187
+        return $this->schema_service->get_schema( 'thing' );
188
+
189
+    }
190
+
191
+    /**
192
+     * Get the term ids of the entity types associated to the specified post.
193
+     *
194
+     * @param int $post_id The post id.
195
+     *
196
+     * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
197
+     * @since 3.20.0
198
+     */
199
+    public function get_ids( $post_id ) {
200
+
201
+        return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids' ) );
202
+    }
203
+
204
+    /**
205
+     * Get the camel case names of the entity types associated to the specified post.
206
+     *
207
+     * @param int $post_id The post id.
208
+     *
209
+     * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
210
+     * @since 3.20.0
211
+     */
212
+    public function get_names( $post_id ) {
213
+
214
+        $ids = $this->get_ids( $post_id );
215
+
216
+        // Filter out invalid terms (ones without _wl_name term meta)
217
+        return array_values(
218
+            array_filter(
219
+                array_map(
220
+                    function ( $id ) {
221
+                        return get_term_meta( $id, '_wl_name', true );
222
+                    },
223
+                    $ids
224
+                )
225
+            )
226
+        );
227
+    }
228
+
229
+    /**
230
+     * Set the main type for the specified entity post, given the type URI.
231
+     *
232
+     * @param int    $post_id The post id.
233
+     * @param string $type_uri The type URI.
234
+     * @param bool   $replace Whether the provided type must replace the existing types, by default `true`.
235
+     *
236
+     * @since 3.8.0
237
+     */
238
+    public function set( $post_id, $type_uri, $replace = true ) {
239
+
240
+        // If the type URI is empty we remove the type.
241
+        if ( empty( $type_uri ) ) {
242
+            $this->log->debug( "Removing entity type for post $post_id..." );
243
+
244
+            wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
245
+
246
+            return;
247
+        }
248
+
249
+        $this->log->debug( "Setting entity type for post $post_id..." );
250
+
251
+        // if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
252
+        $term = ( 0 === strpos( $type_uri, 'wl-' ) )
253
+            // Get term by slug.
254
+            ? $this->get_term_by_slug( substr( $type_uri, 3 ) )
255
+            // Get term by URI.
256
+            : $this->get_term_by_uri( $type_uri );
257
+
258
+        /*
259 259
 		 * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be
260 260
 		 * connected to Articles via mentions. We realized that the client JS code is passing `wl-other` when the
261 261
 		 * entity type isn't "notable". In which case we couldn't find an entity type.
@@ -266,246 +266,246 @@  discard block
 block discarded – undo
266 266
 		 *
267 267
 		 * @since 3.23.4
268 268
 		 */
269
-		if ( false === $term ) {
270
-			$this->log->warn( "No term found for URI $type_uri, will use Thing." );
271
-
272
-			$term = $this->get_term_by_slug( 'thing' );
273
-
274
-			// We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
275
-			// is installed.
276
-			if ( false === $term ) {
277
-				return;
278
-			}
279
-		}
280
-
281
-		$this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
282
-
283
-		// `$replace` is passed to decide whether to replace or append the term.
284
-		wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
285
-
286
-	}
287
-
288
-	/**
289
-	 * Get an entity type term given its slug.
290
-	 *
291
-	 * @param string $slug The slug.
292
-	 *
293
-	 * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
294
-	 *                             or `$term` was not found.
295
-	 * @since 3.20.0
296
-	 */
297
-	private function get_term_by_slug( $slug ) {
298
-
299
-		return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
300
-	}
301
-
302
-	/**
303
-	 * Get an entity type term given its URI.
304
-	 *
305
-	 * @param string $uri The uri.
306
-	 *
307
-	 * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
308
-	 *                             or `$term` was not found.
309
-	 * @since 3.20.0
310
-	 */
311
-	public function get_term_by_uri( $uri ) {
312
-
313
-		$terms = get_terms(
314
-			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
315
-			array(
316
-				'fields'     => 'all',
317
-				'get'        => 'all',
318
-				'number'     => 1,
319
-				'meta_query' => array(
320
-					array(
321
-						// Don't use a reference to Wordlift_Schemaorg_Class_Service, unless
322
-						// `WL_ALL_ENTITY_TYPES` is set to true.
323
-						'key'   => '_wl_uri',
324
-						'value' => $uri,
325
-					),
326
-				),
327
-				'orderby'    => 'term_id',
328
-				'order'      => 'ASC',
329
-			)
330
-		);
331
-
332
-		return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
333
-	}
334
-
335
-	/**
336
-	 * Check whether an entity type is set for the {@link WP_Post} with the
337
-	 * specified id.
338
-	 *
339
-	 * @param int    $post_id The {@link WP_Post}'s `id`.
340
-	 * @param string $uri The entity type URI.
341
-	 *
342
-	 * @return bool True if an entity type is set otherwise false.
343
-	 * @since 3.15.0
344
-	 */
345
-	public function has_entity_type( $post_id, $uri = null ) {
346
-
347
-		$this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
348
-
349
-		// If an URI hasn't been specified just check whether we have at least
350
-		// one entity type.
351
-		if ( null === $uri ) {
352
-			return has_term( '', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id );
353
-		}
354
-
355
-		$has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
356
-
357
-		$this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
358
-
359
-		// Check whether the post has an entity type with that URI.
360
-		return $has_entity_type;
361
-	}
362
-
363
-	/**
364
-	 * Get the list of entity types' terms for the specified {@link WP_Post}.
365
-	 *
366
-	 * @param int $post_id The {@link WP_Post} id.
367
-	 *
368
-	 * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
369
-	 * @since 3.15.0
370
-	 */
371
-	private function get_post_terms( $post_id ) {
372
-
373
-		return wp_get_object_terms(
374
-			$post_id,
375
-			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
376
-			array(
377
-				'hide_empty' => false,
378
-				// Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
379
-				// An issue has been opened with the AAM plugin author as well.
380
-				//
381
-				// see https://github.com/insideout10/wordlift-plugin/issues/334
382
-				// see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
383
-				'fields'     => 'all',
384
-			)
385
-		);
386
-	}
387
-
388
-	/**
389
-	 * Get an entity type term given its URI.
390
-	 *
391
-	 * @param int    $post_id The {@link WP_Post} id.
392
-	 * @param string $uri The entity type URI.
393
-	 *
394
-	 * @return bool True if the post has that type URI bound to it otherwise false.
395
-	 * @since 3.15.0
396
-	 *
397
-	 * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
398
-	 */
399
-	private function has_post_term_by_uri( $post_id, $uri ) {
400
-
401
-		// Get the post terms bound to the specified post.
402
-		$terms = $this->get_post_terms( $post_id );
403
-
404
-		// Look for a term if the specified URI.
405
-		foreach ( $terms as $term ) {
406
-			$term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
407
-
408
-			if ( $uri === $term_uri ) {
409
-				return true;
410
-			}
411
-		}
412
-
413
-		// Return null.
414
-		return false;
415
-	}
416
-
417
-	/**
418
-	 * Get the custom fields for a specific post.
419
-	 *
420
-	 * @param int $post_id The post ID.
421
-	 *
422
-	 * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service).
423
-	 * @since 3.25.2
424
-	 */
425
-	public function get_custom_fields_for_post( $post_id ) {
426
-
427
-		// Return custom fields for this specific entity's type.
428
-		$types = $this->get_ids( $post_id );
429
-
430
-		/** @var WP_Term[] $terms */
431
-		$terms = array_filter(
432
-			array_map(
433
-				function ( $item ) {
434
-					return get_term( $item );
435
-				},
436
-				$types
437
-			),
438
-			function ( $item ) {
439
-				return isset( $item ) && is_a( $item, 'WP_Term' );
440
-			}
441
-		);
442
-
443
-		$term_slugs = array_map(
444
-			function ( $item ) {
445
-				return $item->slug;
446
-			},
447
-			$terms
448
-		);
449
-
450
-		$term_slugs[] = 'thing';
451
-
452
-		return $this->get_custom_fields_by_term_slugs( $term_slugs );
453
-	}
454
-
455
-	/**
456
-	 * Get the custom fields for a specific term.
457
-	 *
458
-	 * @param int $term_id The term ID.
459
-	 *
460
-	 * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service).
461
-	 * @since 3.32.0
462
-	 */
463
-	public function get_custom_fields_for_term( $term_id ) {
464
-		$selected_entity_types   = get_term_meta( $term_id, 'wl_entity_type' );
465
-		$selected_entity_types[] = 'thing';
466
-		$selected_entity_types   = array_unique( $selected_entity_types );
467
-
468
-		return $this->get_custom_fields_by_term_slugs( $selected_entity_types );
469
-	}
470
-
471
-	/**
472
-	 * Determines whether a post type can be used for entities.
473
-	 *
474
-	 * Criteria is that the post type is public. The list of valid post types
475
-	 * can be overridden with a filter.
476
-	 *
477
-	 * @param string $post_type A post type name.
478
-	 *
479
-	 * @return bool Return true if the post type can be used for entities, otherwise false.
480
-	 * @since 3.15.0
481
-	 */
482
-	public static function is_valid_entity_post_type( $post_type ) {
483
-
484
-		return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
485
-	}
486
-
487
-	/**
488
-	 * @param $term_slugs
489
-	 *
490
-	 * @return array
491
-	 */
492
-	private function get_custom_fields_by_term_slugs( $term_slugs ) {
493
-		$schema_service = Wordlift_Schema_Service::get_instance();
494
-
495
-		return array_reduce(
496
-			$term_slugs,
497
-			function ( $carry, $item ) use ( $schema_service ) {
498
-
499
-				$schema = $schema_service->get_schema( $item );
500
-
501
-				if ( ! isset( $schema['custom_fields'] ) ) {
502
-					return $carry;
503
-				}
504
-
505
-				return $carry + $schema['custom_fields'];
506
-			},
507
-			array()
508
-		);
509
-	}
269
+        if ( false === $term ) {
270
+            $this->log->warn( "No term found for URI $type_uri, will use Thing." );
271
+
272
+            $term = $this->get_term_by_slug( 'thing' );
273
+
274
+            // We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
275
+            // is installed.
276
+            if ( false === $term ) {
277
+                return;
278
+            }
279
+        }
280
+
281
+        $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
282
+
283
+        // `$replace` is passed to decide whether to replace or append the term.
284
+        wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
285
+
286
+    }
287
+
288
+    /**
289
+     * Get an entity type term given its slug.
290
+     *
291
+     * @param string $slug The slug.
292
+     *
293
+     * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
294
+     *                             or `$term` was not found.
295
+     * @since 3.20.0
296
+     */
297
+    private function get_term_by_slug( $slug ) {
298
+
299
+        return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
300
+    }
301
+
302
+    /**
303
+     * Get an entity type term given its URI.
304
+     *
305
+     * @param string $uri The uri.
306
+     *
307
+     * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
308
+     *                             or `$term` was not found.
309
+     * @since 3.20.0
310
+     */
311
+    public function get_term_by_uri( $uri ) {
312
+
313
+        $terms = get_terms(
314
+            Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
315
+            array(
316
+                'fields'     => 'all',
317
+                'get'        => 'all',
318
+                'number'     => 1,
319
+                'meta_query' => array(
320
+                    array(
321
+                        // Don't use a reference to Wordlift_Schemaorg_Class_Service, unless
322
+                        // `WL_ALL_ENTITY_TYPES` is set to true.
323
+                        'key'   => '_wl_uri',
324
+                        'value' => $uri,
325
+                    ),
326
+                ),
327
+                'orderby'    => 'term_id',
328
+                'order'      => 'ASC',
329
+            )
330
+        );
331
+
332
+        return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
333
+    }
334
+
335
+    /**
336
+     * Check whether an entity type is set for the {@link WP_Post} with the
337
+     * specified id.
338
+     *
339
+     * @param int    $post_id The {@link WP_Post}'s `id`.
340
+     * @param string $uri The entity type URI.
341
+     *
342
+     * @return bool True if an entity type is set otherwise false.
343
+     * @since 3.15.0
344
+     */
345
+    public function has_entity_type( $post_id, $uri = null ) {
346
+
347
+        $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
348
+
349
+        // If an URI hasn't been specified just check whether we have at least
350
+        // one entity type.
351
+        if ( null === $uri ) {
352
+            return has_term( '', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id );
353
+        }
354
+
355
+        $has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
356
+
357
+        $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
358
+
359
+        // Check whether the post has an entity type with that URI.
360
+        return $has_entity_type;
361
+    }
362
+
363
+    /**
364
+     * Get the list of entity types' terms for the specified {@link WP_Post}.
365
+     *
366
+     * @param int $post_id The {@link WP_Post} id.
367
+     *
368
+     * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
369
+     * @since 3.15.0
370
+     */
371
+    private function get_post_terms( $post_id ) {
372
+
373
+        return wp_get_object_terms(
374
+            $post_id,
375
+            Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
376
+            array(
377
+                'hide_empty' => false,
378
+                // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
379
+                // An issue has been opened with the AAM plugin author as well.
380
+                //
381
+                // see https://github.com/insideout10/wordlift-plugin/issues/334
382
+                // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
383
+                'fields'     => 'all',
384
+            )
385
+        );
386
+    }
387
+
388
+    /**
389
+     * Get an entity type term given its URI.
390
+     *
391
+     * @param int    $post_id The {@link WP_Post} id.
392
+     * @param string $uri The entity type URI.
393
+     *
394
+     * @return bool True if the post has that type URI bound to it otherwise false.
395
+     * @since 3.15.0
396
+     *
397
+     * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
398
+     */
399
+    private function has_post_term_by_uri( $post_id, $uri ) {
400
+
401
+        // Get the post terms bound to the specified post.
402
+        $terms = $this->get_post_terms( $post_id );
403
+
404
+        // Look for a term if the specified URI.
405
+        foreach ( $terms as $term ) {
406
+            $term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
407
+
408
+            if ( $uri === $term_uri ) {
409
+                return true;
410
+            }
411
+        }
412
+
413
+        // Return null.
414
+        return false;
415
+    }
416
+
417
+    /**
418
+     * Get the custom fields for a specific post.
419
+     *
420
+     * @param int $post_id The post ID.
421
+     *
422
+     * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service).
423
+     * @since 3.25.2
424
+     */
425
+    public function get_custom_fields_for_post( $post_id ) {
426
+
427
+        // Return custom fields for this specific entity's type.
428
+        $types = $this->get_ids( $post_id );
429
+
430
+        /** @var WP_Term[] $terms */
431
+        $terms = array_filter(
432
+            array_map(
433
+                function ( $item ) {
434
+                    return get_term( $item );
435
+                },
436
+                $types
437
+            ),
438
+            function ( $item ) {
439
+                return isset( $item ) && is_a( $item, 'WP_Term' );
440
+            }
441
+        );
442
+
443
+        $term_slugs = array_map(
444
+            function ( $item ) {
445
+                return $item->slug;
446
+            },
447
+            $terms
448
+        );
449
+
450
+        $term_slugs[] = 'thing';
451
+
452
+        return $this->get_custom_fields_by_term_slugs( $term_slugs );
453
+    }
454
+
455
+    /**
456
+     * Get the custom fields for a specific term.
457
+     *
458
+     * @param int $term_id The term ID.
459
+     *
460
+     * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service).
461
+     * @since 3.32.0
462
+     */
463
+    public function get_custom_fields_for_term( $term_id ) {
464
+        $selected_entity_types   = get_term_meta( $term_id, 'wl_entity_type' );
465
+        $selected_entity_types[] = 'thing';
466
+        $selected_entity_types   = array_unique( $selected_entity_types );
467
+
468
+        return $this->get_custom_fields_by_term_slugs( $selected_entity_types );
469
+    }
470
+
471
+    /**
472
+     * Determines whether a post type can be used for entities.
473
+     *
474
+     * Criteria is that the post type is public. The list of valid post types
475
+     * can be overridden with a filter.
476
+     *
477
+     * @param string $post_type A post type name.
478
+     *
479
+     * @return bool Return true if the post type can be used for entities, otherwise false.
480
+     * @since 3.15.0
481
+     */
482
+    public static function is_valid_entity_post_type( $post_type ) {
483
+
484
+        return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
485
+    }
486
+
487
+    /**
488
+     * @param $term_slugs
489
+     *
490
+     * @return array
491
+     */
492
+    private function get_custom_fields_by_term_slugs( $term_slugs ) {
493
+        $schema_service = Wordlift_Schema_Service::get_instance();
494
+
495
+        return array_reduce(
496
+            $term_slugs,
497
+            function ( $carry, $item ) use ( $schema_service ) {
498
+
499
+                $schema = $schema_service->get_schema( $item );
500
+
501
+                if ( ! isset( $schema['custom_fields'] ) ) {
502
+                    return $carry;
503
+                }
504
+
505
+                return $carry + $schema['custom_fields'];
506
+            },
507
+            array()
508
+        );
509
+    }
510 510
 
511 511
 }
Please login to merge, or discard this patch.
Spacing   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
 	 */
45 45
 	protected function __construct() {
46 46
 
47
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
47
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Service');
48 48
 
49 49
 		$this->schema_service = Wordlift_Schema_Service::get_instance();
50 50
 
@@ -61,11 +61,11 @@  discard block
 block discarded – undo
61 61
 
62 62
 		add_action(
63 63
 			'init',
64
-			function () {
64
+			function() {
65 65
 				// Add post type support for 'custom-fields' for all post types. Specifically needed in Gutenberg
66 66
 				$post_types = get_post_types();
67
-				foreach ( $post_types as $post_type ) {
68
-					add_post_type_support( $post_type, 'custom-fields' );
67
+				foreach ($post_types as $post_type) {
68
+					add_post_type_support($post_type, 'custom-fields');
69 69
 				}
70 70
 			}
71 71
 		);
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
 	 */
89 89
 	public static function get_instance() {
90 90
 
91
-		if ( ! isset( self::$instance ) ) {
91
+		if ( ! isset(self::$instance)) {
92 92
 			self::$instance = new self();
93 93
 		}
94 94
 
@@ -126,34 +126,34 @@  discard block
 block discarded – undo
126 126
 	 *      c) the post is a custom post type then it is
127 127
 	 *          assigned the `WebPage` entity type by default.
128 128
 	 */
129
-	public function get( $post_id ) {
129
+	public function get($post_id) {
130 130
 
131
-		$this->log->trace( "Getting the post type for post $post_id..." );
131
+		$this->log->trace("Getting the post type for post $post_id...");
132 132
 
133 133
 		// Get the post type.
134
-		$post_type = get_post_type( $post_id );
134
+		$post_type = get_post_type($post_id);
135 135
 
136 136
 		// Return `web-page` for non entities.
137
-		if ( ! self::is_valid_entity_post_type( $post_type ) ) {
138
-			$this->log->info( "Returning `web-page` for post $post_id." );
137
+		if ( ! self::is_valid_entity_post_type($post_type)) {
138
+			$this->log->info("Returning `web-page` for post $post_id.");
139 139
 
140
-			return $this->schema_service->get_schema( 'web-page' );
140
+			return $this->schema_service->get_schema('web-page');
141 141
 		}
142 142
 
143 143
 		// Get the type from the associated classification.
144
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
144
+		$terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
145 145
 
146 146
 		// Return the schema type if there is a term found.
147
-		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
147
+		if ( ! is_wp_error($terms) && ! empty($terms)) {
148 148
 			// Cycle through the terms and return the first one with a valid schema.
149
-			foreach ( $terms as $term ) {
150
-				$this->log->debug( "Found `{$term->slug}` term for post $post_id." );
149
+			foreach ($terms as $term) {
150
+				$this->log->debug("Found `{$term->slug}` term for post $post_id.");
151 151
 
152 152
 				// Try to get the schema for the term.
153
-				$schema = $this->schema_service->get_schema( $term->slug );
153
+				$schema = $this->schema_service->get_schema($term->slug);
154 154
 
155 155
 				// If found, return it, ignoring the other types.
156
-				if ( null !== $schema ) {
156
+				if (null !== $schema) {
157 157
 					// Return the entity type with the specified id.
158 158
 					return $schema;
159 159
 				}
@@ -169,22 +169,22 @@  discard block
 block discarded – undo
169 169
 			 * @since 3.20.0
170 170
 			 */
171 171
 
172
-			return $this->schema_service->get_schema( 'thing' );
172
+			return $this->schema_service->get_schema('thing');
173 173
 		}
174 174
 
175 175
 		// If it's a page or post return `Article`.
176
-		if ( in_array( $post_type, array( 'post', 'page' ), true ) ) {
177
-			$this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
176
+		if (in_array($post_type, array('post', 'page'), true)) {
177
+			$this->log->debug("Post $post_id has no terms, and it's a `post` type, returning `Article`.");
178 178
 
179 179
 			// Return "Article" schema type for posts.
180
-			return $this->schema_service->get_schema( 'article' );
180
+			return $this->schema_service->get_schema('article');
181 181
 		}
182 182
 
183 183
 		// Return "Thing" schema type for entities.
184
-		$this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
184
+		$this->log->debug("Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`.");
185 185
 
186 186
 		// Return the entity type with the specified id.
187
-		return $this->schema_service->get_schema( 'thing' );
187
+		return $this->schema_service->get_schema('thing');
188 188
 
189 189
 	}
190 190
 
@@ -196,9 +196,9 @@  discard block
 block discarded – undo
196 196
 	 * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
197 197
 	 * @since 3.20.0
198 198
 	 */
199
-	public function get_ids( $post_id ) {
199
+	public function get_ids($post_id) {
200 200
 
201
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids' ) );
201
+		return wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('fields' => 'ids'));
202 202
 	}
203 203
 
204 204
 	/**
@@ -209,16 +209,16 @@  discard block
 block discarded – undo
209 209
 	 * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
210 210
 	 * @since 3.20.0
211 211
 	 */
212
-	public function get_names( $post_id ) {
212
+	public function get_names($post_id) {
213 213
 
214
-		$ids = $this->get_ids( $post_id );
214
+		$ids = $this->get_ids($post_id);
215 215
 
216 216
 		// Filter out invalid terms (ones without _wl_name term meta)
217 217
 		return array_values(
218 218
 			array_filter(
219 219
 				array_map(
220
-					function ( $id ) {
221
-						return get_term_meta( $id, '_wl_name', true );
220
+					function($id) {
221
+						return get_term_meta($id, '_wl_name', true);
222 222
 					},
223 223
 					$ids
224 224
 				)
@@ -235,25 +235,25 @@  discard block
 block discarded – undo
235 235
 	 *
236 236
 	 * @since 3.8.0
237 237
 	 */
238
-	public function set( $post_id, $type_uri, $replace = true ) {
238
+	public function set($post_id, $type_uri, $replace = true) {
239 239
 
240 240
 		// If the type URI is empty we remove the type.
241
-		if ( empty( $type_uri ) ) {
242
-			$this->log->debug( "Removing entity type for post $post_id..." );
241
+		if (empty($type_uri)) {
242
+			$this->log->debug("Removing entity type for post $post_id...");
243 243
 
244
-			wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
244
+			wp_set_object_terms($post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
245 245
 
246 246
 			return;
247 247
 		}
248 248
 
249
-		$this->log->debug( "Setting entity type for post $post_id..." );
249
+		$this->log->debug("Setting entity type for post $post_id...");
250 250
 
251 251
 		// if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
252
-		$term = ( 0 === strpos( $type_uri, 'wl-' ) )
252
+		$term = (0 === strpos($type_uri, 'wl-'))
253 253
 			// Get term by slug.
254
-			? $this->get_term_by_slug( substr( $type_uri, 3 ) )
254
+			? $this->get_term_by_slug(substr($type_uri, 3))
255 255
 			// Get term by URI.
256
-			: $this->get_term_by_uri( $type_uri );
256
+			: $this->get_term_by_uri($type_uri);
257 257
 
258 258
 		/*
259 259
 		 * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be
@@ -266,22 +266,22 @@  discard block
 block discarded – undo
266 266
 		 *
267 267
 		 * @since 3.23.4
268 268
 		 */
269
-		if ( false === $term ) {
270
-			$this->log->warn( "No term found for URI $type_uri, will use Thing." );
269
+		if (false === $term) {
270
+			$this->log->warn("No term found for URI $type_uri, will use Thing.");
271 271
 
272
-			$term = $this->get_term_by_slug( 'thing' );
272
+			$term = $this->get_term_by_slug('thing');
273 273
 
274 274
 			// We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
275 275
 			// is installed.
276
-			if ( false === $term ) {
276
+			if (false === $term) {
277 277
 				return;
278 278
 			}
279 279
 		}
280 280
 
281
-		$this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
281
+		$this->log->debug("Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]...");
282 282
 
283 283
 		// `$replace` is passed to decide whether to replace or append the term.
284
-		wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
284
+		wp_set_object_terms($post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace);
285 285
 
286 286
 	}
287 287
 
@@ -294,9 +294,9 @@  discard block
 block discarded – undo
294 294
 	 *                             or `$term` was not found.
295 295
 	 * @since 3.20.0
296 296
 	 */
297
-	private function get_term_by_slug( $slug ) {
297
+	private function get_term_by_slug($slug) {
298 298
 
299
-		return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
299
+		return get_term_by('slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
300 300
 	}
301 301
 
302 302
 	/**
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
 	 *                             or `$term` was not found.
309 309
 	 * @since 3.20.0
310 310
 	 */
311
-	public function get_term_by_uri( $uri ) {
311
+	public function get_term_by_uri($uri) {
312 312
 
313 313
 		$terms = get_terms(
314 314
 			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
@@ -329,7 +329,7 @@  discard block
 block discarded – undo
329 329
 			)
330 330
 		);
331 331
 
332
-		return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
332
+		return is_array($terms) && ! empty($terms) ? $terms[0] : false;
333 333
 	}
334 334
 
335 335
 	/**
@@ -342,19 +342,19 @@  discard block
 block discarded – undo
342 342
 	 * @return bool True if an entity type is set otherwise false.
343 343
 	 * @since 3.15.0
344 344
 	 */
345
-	public function has_entity_type( $post_id, $uri = null ) {
345
+	public function has_entity_type($post_id, $uri = null) {
346 346
 
347
-		$this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
347
+		$this->log->debug("Checking if post $post_id has an entity type [ $uri ]...");
348 348
 
349 349
 		// If an URI hasn't been specified just check whether we have at least
350 350
 		// one entity type.
351
-		if ( null === $uri ) {
352
-			return has_term( '', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id );
351
+		if (null === $uri) {
352
+			return has_term('', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id);
353 353
 		}
354 354
 
355
-		$has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
355
+		$has_entity_type = (null !== $this->has_post_term_by_uri($post_id, $uri));
356 356
 
357
-		$this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
357
+		$this->log->debug("Post $post_id has $uri type: ".($has_entity_type ? 'yes' : 'no'));
358 358
 
359 359
 		// Check whether the post has an entity type with that URI.
360 360
 		return $has_entity_type;
@@ -368,7 +368,7 @@  discard block
 block discarded – undo
368 368
 	 * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
369 369
 	 * @since 3.15.0
370 370
 	 */
371
-	private function get_post_terms( $post_id ) {
371
+	private function get_post_terms($post_id) {
372 372
 
373 373
 		return wp_get_object_terms(
374 374
 			$post_id,
@@ -396,16 +396,16 @@  discard block
 block discarded – undo
396 396
 	 *
397 397
 	 * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
398 398
 	 */
399
-	private function has_post_term_by_uri( $post_id, $uri ) {
399
+	private function has_post_term_by_uri($post_id, $uri) {
400 400
 
401 401
 		// Get the post terms bound to the specified post.
402
-		$terms = $this->get_post_terms( $post_id );
402
+		$terms = $this->get_post_terms($post_id);
403 403
 
404 404
 		// Look for a term if the specified URI.
405
-		foreach ( $terms as $term ) {
406
-			$term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
405
+		foreach ($terms as $term) {
406
+			$term_uri = get_term_meta($term->term_id, '_wl_uri', true);
407 407
 
408
-			if ( $uri === $term_uri ) {
408
+			if ($uri === $term_uri) {
409 409
 				return true;
410 410
 			}
411 411
 		}
@@ -422,26 +422,26 @@  discard block
 block discarded – undo
422 422
 	 * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service).
423 423
 	 * @since 3.25.2
424 424
 	 */
425
-	public function get_custom_fields_for_post( $post_id ) {
425
+	public function get_custom_fields_for_post($post_id) {
426 426
 
427 427
 		// Return custom fields for this specific entity's type.
428
-		$types = $this->get_ids( $post_id );
428
+		$types = $this->get_ids($post_id);
429 429
 
430 430
 		/** @var WP_Term[] $terms */
431 431
 		$terms = array_filter(
432 432
 			array_map(
433
-				function ( $item ) {
434
-					return get_term( $item );
433
+				function($item) {
434
+					return get_term($item);
435 435
 				},
436 436
 				$types
437 437
 			),
438
-			function ( $item ) {
439
-				return isset( $item ) && is_a( $item, 'WP_Term' );
438
+			function($item) {
439
+				return isset($item) && is_a($item, 'WP_Term');
440 440
 			}
441 441
 		);
442 442
 
443 443
 		$term_slugs = array_map(
444
-			function ( $item ) {
444
+			function($item) {
445 445
 				return $item->slug;
446 446
 			},
447 447
 			$terms
@@ -449,7 +449,7 @@  discard block
 block discarded – undo
449 449
 
450 450
 		$term_slugs[] = 'thing';
451 451
 
452
-		return $this->get_custom_fields_by_term_slugs( $term_slugs );
452
+		return $this->get_custom_fields_by_term_slugs($term_slugs);
453 453
 	}
454 454
 
455 455
 	/**
@@ -460,12 +460,12 @@  discard block
 block discarded – undo
460 460
 	 * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service).
461 461
 	 * @since 3.32.0
462 462
 	 */
463
-	public function get_custom_fields_for_term( $term_id ) {
464
-		$selected_entity_types   = get_term_meta( $term_id, 'wl_entity_type' );
463
+	public function get_custom_fields_for_term($term_id) {
464
+		$selected_entity_types   = get_term_meta($term_id, 'wl_entity_type');
465 465
 		$selected_entity_types[] = 'thing';
466
-		$selected_entity_types   = array_unique( $selected_entity_types );
466
+		$selected_entity_types   = array_unique($selected_entity_types);
467 467
 
468
-		return $this->get_custom_fields_by_term_slugs( $selected_entity_types );
468
+		return $this->get_custom_fields_by_term_slugs($selected_entity_types);
469 469
 	}
470 470
 
471 471
 	/**
@@ -479,9 +479,9 @@  discard block
 block discarded – undo
479 479
 	 * @return bool Return true if the post type can be used for entities, otherwise false.
480 480
 	 * @since 3.15.0
481 481
 	 */
482
-	public static function is_valid_entity_post_type( $post_type ) {
482
+	public static function is_valid_entity_post_type($post_type) {
483 483
 
484
-		return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
484
+		return in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true);
485 485
 	}
486 486
 
487 487
 	/**
@@ -489,16 +489,16 @@  discard block
 block discarded – undo
489 489
 	 *
490 490
 	 * @return array
491 491
 	 */
492
-	private function get_custom_fields_by_term_slugs( $term_slugs ) {
492
+	private function get_custom_fields_by_term_slugs($term_slugs) {
493 493
 		$schema_service = Wordlift_Schema_Service::get_instance();
494 494
 
495 495
 		return array_reduce(
496 496
 			$term_slugs,
497
-			function ( $carry, $item ) use ( $schema_service ) {
497
+			function($carry, $item) use ($schema_service) {
498 498
 
499
-				$schema = $schema_service->get_schema( $item );
499
+				$schema = $schema_service->get_schema($item);
500 500
 
501
-				if ( ! isset( $schema['custom_fields'] ) ) {
501
+				if ( ! isset($schema['custom_fields'])) {
502 502
 					return $carry;
503 503
 				}
504 504
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-autocomplete-adapter.php 2 patches
Indentation   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 use Wordlift\Autocomplete\Autocomplete_Service;
14 14
 
15 15
 if ( ! defined( 'ABSPATH' ) ) {
16
-	exit;
16
+    exit;
17 17
 }
18 18
 
19 19
 /**
@@ -23,79 +23,79 @@  discard block
 block discarded – undo
23 23
  */
24 24
 class Wordlift_Autocomplete_Adapter {
25 25
 
26
-	/**
27
-	 * The {@link Autocomplete_Service} instance.
28
-	 *
29
-	 * @since  3.15.0
30
-	 * @access private
31
-	 * @var Autocomplete_Service $configuration_service The {@link Autocomplete_Service} instance.
32
-	 */
33
-	private $autocomplete_service;
34
-
35
-	/**
36
-	 * Wordlift_Autocomplete_Adapter constructor.
37
-	 *
38
-	 * @param Autocomplete_Service $autocomplete_service The {@link Autocomplete_Service} instance.
39
-	 *
40
-	 * @since 3.14.2
41
-	 */
42
-	public function __construct( $autocomplete_service ) {
43
-		$this->autocomplete_service = $autocomplete_service;
44
-	}
45
-
46
-	/**
47
-	 * Handle the autocomplete ajax request.
48
-	 *
49
-	 * @since 3.15.0
50
-	 */
51
-	public function wl_autocomplete() {
52
-
53
-		check_ajax_referer( 'wl_autocomplete' );
54
-
55
-		// Return error if the query param is empty.
56
-		if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay.
57
-			$query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay.
58
-		} else {
59
-			wp_send_json_error(
60
-				array(
61
-					'message' => __( 'The query param is empty.', 'wordlift' ),
62
-				)
63
-			);
64
-		}
65
-
66
-		// Get the exclude parameter.
67
-		$exclude = ! empty( $_REQUEST['exclude'] )
68
-			? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : '';
69
-
70
-		$scope = ! empty( $_REQUEST['scope'] )
71
-			? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : WL_AUTOCOMPLETE_SCOPE;
72
-
73
-		/**
74
-		 * @since 3.26.1
75
-		 * Providing a way for term pages to show and save local entities.
76
-		 */
77
-		$show_local_entities = false;
78
-
79
-		if ( isset( $_REQUEST['show_local_entities'] )
80
-			 && ! empty( $_REQUEST['show_local_entities'] ) ) { // Make request.
81
-			$show_local_entities = filter_var( wp_unslash( $_REQUEST['show_local_entities'] ), FILTER_VALIDATE_BOOLEAN );
82
-		}
83
-
84
-		// Add the filter to check if we need to show local entities or not.
85
-		add_filter(
86
-			'wl_show_local_entities',
87
-			// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
88
-			function ( $state ) use ( $show_local_entities ) {
89
-				return $show_local_entities;
90
-			}
91
-		);
92
-
93
-		$results = $this->autocomplete_service->query( $query, $scope, $exclude );
94
-
95
-		// Clear any buffer.
96
-		ob_clean();
97
-
98
-		wp_send_json_success( $results );
99
-
100
-	}
26
+    /**
27
+     * The {@link Autocomplete_Service} instance.
28
+     *
29
+     * @since  3.15.0
30
+     * @access private
31
+     * @var Autocomplete_Service $configuration_service The {@link Autocomplete_Service} instance.
32
+     */
33
+    private $autocomplete_service;
34
+
35
+    /**
36
+     * Wordlift_Autocomplete_Adapter constructor.
37
+     *
38
+     * @param Autocomplete_Service $autocomplete_service The {@link Autocomplete_Service} instance.
39
+     *
40
+     * @since 3.14.2
41
+     */
42
+    public function __construct( $autocomplete_service ) {
43
+        $this->autocomplete_service = $autocomplete_service;
44
+    }
45
+
46
+    /**
47
+     * Handle the autocomplete ajax request.
48
+     *
49
+     * @since 3.15.0
50
+     */
51
+    public function wl_autocomplete() {
52
+
53
+        check_ajax_referer( 'wl_autocomplete' );
54
+
55
+        // Return error if the query param is empty.
56
+        if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay.
57
+            $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay.
58
+        } else {
59
+            wp_send_json_error(
60
+                array(
61
+                    'message' => __( 'The query param is empty.', 'wordlift' ),
62
+                )
63
+            );
64
+        }
65
+
66
+        // Get the exclude parameter.
67
+        $exclude = ! empty( $_REQUEST['exclude'] )
68
+            ? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : '';
69
+
70
+        $scope = ! empty( $_REQUEST['scope'] )
71
+            ? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : WL_AUTOCOMPLETE_SCOPE;
72
+
73
+        /**
74
+         * @since 3.26.1
75
+         * Providing a way for term pages to show and save local entities.
76
+         */
77
+        $show_local_entities = false;
78
+
79
+        if ( isset( $_REQUEST['show_local_entities'] )
80
+             && ! empty( $_REQUEST['show_local_entities'] ) ) { // Make request.
81
+            $show_local_entities = filter_var( wp_unslash( $_REQUEST['show_local_entities'] ), FILTER_VALIDATE_BOOLEAN );
82
+        }
83
+
84
+        // Add the filter to check if we need to show local entities or not.
85
+        add_filter(
86
+            'wl_show_local_entities',
87
+            // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
88
+            function ( $state ) use ( $show_local_entities ) {
89
+                return $show_local_entities;
90
+            }
91
+        );
92
+
93
+        $results = $this->autocomplete_service->query( $query, $scope, $exclude );
94
+
95
+        // Clear any buffer.
96
+        ob_clean();
97
+
98
+        wp_send_json_success( $results );
99
+
100
+    }
101 101
 }
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
 
13 13
 use Wordlift\Autocomplete\Autocomplete_Service;
14 14
 
15
-if ( ! defined( 'ABSPATH' ) ) {
15
+if ( ! defined('ABSPATH')) {
16 16
 	exit;
17 17
 }
18 18
 
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
 	 *
40 40
 	 * @since 3.14.2
41 41
 	 */
42
-	public function __construct( $autocomplete_service ) {
42
+	public function __construct($autocomplete_service) {
43 43
 		$this->autocomplete_service = $autocomplete_service;
44 44
 	}
45 45
 
@@ -50,25 +50,25 @@  discard block
 block discarded – undo
50 50
 	 */
51 51
 	public function wl_autocomplete() {
52 52
 
53
-		check_ajax_referer( 'wl_autocomplete' );
53
+		check_ajax_referer('wl_autocomplete');
54 54
 
55 55
 		// Return error if the query param is empty.
56
-		if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay.
57
-			$query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay.
56
+		if ( ! empty($_REQUEST['query'])) { // Input var okay.
57
+			$query = sanitize_text_field(wp_unslash($_REQUEST['query'])); // Input var okay.
58 58
 		} else {
59 59
 			wp_send_json_error(
60 60
 				array(
61
-					'message' => __( 'The query param is empty.', 'wordlift' ),
61
+					'message' => __('The query param is empty.', 'wordlift'),
62 62
 				)
63 63
 			);
64 64
 		}
65 65
 
66 66
 		// Get the exclude parameter.
67
-		$exclude = ! empty( $_REQUEST['exclude'] )
68
-			? sanitize_text_field( wp_unslash( $_REQUEST['exclude'] ) ) : '';
67
+		$exclude = ! empty($_REQUEST['exclude'])
68
+			? sanitize_text_field(wp_unslash($_REQUEST['exclude'])) : '';
69 69
 
70
-		$scope = ! empty( $_REQUEST['scope'] )
71
-			? sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ) : WL_AUTOCOMPLETE_SCOPE;
70
+		$scope = ! empty($_REQUEST['scope'])
71
+			? sanitize_text_field(wp_unslash($_REQUEST['scope'])) : WL_AUTOCOMPLETE_SCOPE;
72 72
 
73 73
 		/**
74 74
 		 * @since 3.26.1
@@ -76,26 +76,26 @@  discard block
 block discarded – undo
76 76
 		 */
77 77
 		$show_local_entities = false;
78 78
 
79
-		if ( isset( $_REQUEST['show_local_entities'] )
80
-			 && ! empty( $_REQUEST['show_local_entities'] ) ) { // Make request.
81
-			$show_local_entities = filter_var( wp_unslash( $_REQUEST['show_local_entities'] ), FILTER_VALIDATE_BOOLEAN );
79
+		if (isset($_REQUEST['show_local_entities'])
80
+			 && ! empty($_REQUEST['show_local_entities'])) { // Make request.
81
+			$show_local_entities = filter_var(wp_unslash($_REQUEST['show_local_entities']), FILTER_VALIDATE_BOOLEAN);
82 82
 		}
83 83
 
84 84
 		// Add the filter to check if we need to show local entities or not.
85 85
 		add_filter(
86 86
 			'wl_show_local_entities',
87 87
 			// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
88
-			function ( $state ) use ( $show_local_entities ) {
88
+			function($state) use ($show_local_entities) {
89 89
 				return $show_local_entities;
90 90
 			}
91 91
 		);
92 92
 
93
-		$results = $this->autocomplete_service->query( $query, $scope, $exclude );
93
+		$results = $this->autocomplete_service->query($query, $scope, $exclude);
94 94
 
95 95
 		// Clear any buffer.
96 96
 		ob_clean();
97 97
 
98
-		wp_send_json_success( $results );
98
+		wp_send_json_success($results);
99 99
 
100 100
 	}
101 101
 }
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/cache/class-wordlift-cached-entity-uri-service.php 2 patches
Indentation   +198 added lines, -198 removed lines patch added patch discarded remove patch
@@ -14,203 +14,203 @@
 block discarded – undo
14 14
  */
15 15
 class Wordlift_Cached_Entity_Uri_Service extends Wordlift_Entity_Uri_Service {
16 16
 
17
-	/**
18
-	 * The {@link Wordlift_Cache_Service} instance.
19
-	 *
20
-	 * @since  3.16.3
21
-	 * @access private
22
-	 * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance.
23
-	 */
24
-	private $cache_service;
25
-
26
-	/**
27
-	 * A {@link Wordlift_Log_Service} instance.
28
-	 *
29
-	 * @since  3.16.3
30
-	 * @access private
31
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
32
-	 */
33
-	private $log;
34
-
35
-	/**
36
-	 * Create a {@link Wordlift_Cached_Entity_Uri_Service} instance.
37
-	 *
38
-	 * @param \Wordlift_Cache_Service $cache_service
39
-	 *
40
-	 * @since 3.16.3
41
-	 */
42
-	public function __construct( $cache_service ) {
43
-		parent::__construct();
44
-
45
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
46
-
47
-		// Add hooks for meta being added/modified/deleted.
48
-		$this->cache_service = $cache_service;
49
-
50
-		add_action( 'add_post_meta', array( $this, 'on_before_post_meta_add' ), 10, 3 );
51
-		add_action( 'update_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 );
52
-		add_action( 'delete_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 );
53
-
54
-	}
55
-
56
-	/**
57
-	 * Preload the URIs.
58
-	 *
59
-	 * @param array $uris Preload an array of URIs.
60
-	 *
61
-	 * @since 3.16.3
62
-	 */
63
-	public function preload_uris( $uris ) {
64
-
65
-		// Filter the URIs which aren't yet cached.
66
-		$cache_service = $this->cache_service;
67
-		$uris_to_cache = array_filter(
68
-			(array) $uris,
69
-			function ( $item ) use ( $cache_service ) {
70
-				return ! $cache_service->has_cache( $item );
71
-			}
72
-		);
73
-
74
-		// Preload the URIs.
75
-		parent::preload_uris( $uris_to_cache );
76
-
77
-		// Store them in cache.
78
-		if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) {
79
-			foreach ( $this->uri_to_post as $uri => $post_id ) {
80
-				$this->set_cache( $uri, $post_id );
81
-			}
82
-		}
83
-
84
-	}
85
-
86
-	/**
87
-	 * Get the entity post for the specified URI.
88
-	 *
89
-	 * @param string $uri The URI.
90
-	 *
91
-	 * @return null|WP_Post The {@link WP_Post} or null if not found.
92
-	 * @since 3.16.3
93
-	 */
94
-	public function get_entity( $uri ) {
95
-
96
-		$this->log->trace( "Getting entity for uri $uri..." );
97
-
98
-		// Get the cached post for the specified URI.
99
-		$cache = $this->cache_service->get_cache( $uri );
100
-
101
-		// Return the cached data if valid.
102
-		if ( false !== $cache && is_numeric( $cache ) ) {
103
-			$this->log->debug( "Cached entity $cache for uri $uri found." );
104
-
105
-			return get_post( $cache );
106
-		}
107
-
108
-		// Get the actual result.
109
-		$post = parent::get_entity( $uri );
110
-
111
-		// Cache the result.
112
-		if ( null !== $post ) {
113
-			$this->set_cache( $uri, $post->ID );
114
-		}
115
-
116
-		// Return the result.
117
-		return $post;
118
-	}
119
-
120
-	/**
121
-	 * Set the cached URI for the specified {@link WP_Post}.
122
-	 *
123
-	 * @param string $uri The URI.
124
-	 * @param int    $post_id The post ID.
125
-	 *
126
-	 * @since 3.16.3
127
-	 * @since 3.29.0 takes a post ID as input.
128
-	 */
129
-	private function set_cache( $uri, $post_id ) {
130
-
131
-		// Cache the result.
132
-		$this->cache_service->set_cache( $uri, $post_id );
133
-
134
-	}
135
-
136
-	/**
137
-	 * Delete the cache for the specified URIs.
138
-	 *
139
-	 * @param array $uris An array of URIs.
140
-	 *
141
-	 * @since 3.16.3
142
-	 */
143
-	private function delete_cache( $uris ) {
144
-
145
-		// Delete the cache for each URI.
146
-		foreach ( $uris as $uri ) {
147
-			// Delete the single cache file.
148
-			$this->cache_service->delete_cache( $uri );
149
-		}
150
-
151
-	}
152
-
153
-	/**
154
-	 * Before post meta change.
155
-	 *
156
-	 * When a post meta is going to be changed, we check if the `meta_key` is
157
-	 * either the `entity_url` or the `same_as` in which case we delete the cache
158
-	 * for all the associated URIs.
159
-	 *
160
-	 * @param int|array $meta_ids The {@link WP_Post} meta id(s).
161
-	 * @param int       $post_id The {@link WP_Post} id.
162
-	 * @param string    $meta_key The meta key.
163
-	 * @param mixed     $meta_value The meta value(s).
164
-	 *
165
-	 * @since 3.16.3
166
-	 */
167
-	public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) {
168
-
169
-		// Bail out if we're not interested in the meta key.
170
-		if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) {
171
-			return;
172
-		}
173
-
174
-		$this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." );
175
-
176
-		// The list of existing URIs, plus the list of URIs being deleted/updated.
177
-		$old_value = get_post_meta( $post_id, $meta_key );
178
-
179
-		// We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects
180
-		// because of filters. So if it's not we return an empty array.
181
-		if ( ! is_array( $old_value ) ) {
182
-			$old_value = array();
183
-		}
184
-		$new_value = isset( $meta_value ) ? (array) $meta_value : array();
185
-		$uris      = array_merge( $old_value, $new_value );
186
-
187
-		// Delete the cache for those URIs.
188
-		$this->delete_cache( $uris );
189
-
190
-	}
191
-
192
-	/**
193
-	 * Hook to meta add for a {@link WP_Post}, will cause the cache to
194
-	 * invalidate.
195
-	 *
196
-	 * @param int    $post_id The {@link WP_Post} id.
197
-	 * @param string $meta_key The meta key.
198
-	 * @param mixed  $meta_value The meta value(s).
199
-	 *
200
-	 * @since 3.16.3
201
-	 */
202
-	public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) {
203
-
204
-		// Bail out if we're not interested in the meta key.
205
-		if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) {
206
-			return;
207
-		}
208
-
209
-		$this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." );
210
-
211
-		// Delete the cache for the URIs being added.
212
-		$this->delete_cache( (array) $meta_value );
213
-
214
-	}
17
+    /**
18
+     * The {@link Wordlift_Cache_Service} instance.
19
+     *
20
+     * @since  3.16.3
21
+     * @access private
22
+     * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance.
23
+     */
24
+    private $cache_service;
25
+
26
+    /**
27
+     * A {@link Wordlift_Log_Service} instance.
28
+     *
29
+     * @since  3.16.3
30
+     * @access private
31
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
32
+     */
33
+    private $log;
34
+
35
+    /**
36
+     * Create a {@link Wordlift_Cached_Entity_Uri_Service} instance.
37
+     *
38
+     * @param \Wordlift_Cache_Service $cache_service
39
+     *
40
+     * @since 3.16.3
41
+     */
42
+    public function __construct( $cache_service ) {
43
+        parent::__construct();
44
+
45
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
46
+
47
+        // Add hooks for meta being added/modified/deleted.
48
+        $this->cache_service = $cache_service;
49
+
50
+        add_action( 'add_post_meta', array( $this, 'on_before_post_meta_add' ), 10, 3 );
51
+        add_action( 'update_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 );
52
+        add_action( 'delete_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 );
53
+
54
+    }
55
+
56
+    /**
57
+     * Preload the URIs.
58
+     *
59
+     * @param array $uris Preload an array of URIs.
60
+     *
61
+     * @since 3.16.3
62
+     */
63
+    public function preload_uris( $uris ) {
64
+
65
+        // Filter the URIs which aren't yet cached.
66
+        $cache_service = $this->cache_service;
67
+        $uris_to_cache = array_filter(
68
+            (array) $uris,
69
+            function ( $item ) use ( $cache_service ) {
70
+                return ! $cache_service->has_cache( $item );
71
+            }
72
+        );
73
+
74
+        // Preload the URIs.
75
+        parent::preload_uris( $uris_to_cache );
76
+
77
+        // Store them in cache.
78
+        if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) {
79
+            foreach ( $this->uri_to_post as $uri => $post_id ) {
80
+                $this->set_cache( $uri, $post_id );
81
+            }
82
+        }
83
+
84
+    }
85
+
86
+    /**
87
+     * Get the entity post for the specified URI.
88
+     *
89
+     * @param string $uri The URI.
90
+     *
91
+     * @return null|WP_Post The {@link WP_Post} or null if not found.
92
+     * @since 3.16.3
93
+     */
94
+    public function get_entity( $uri ) {
95
+
96
+        $this->log->trace( "Getting entity for uri $uri..." );
97
+
98
+        // Get the cached post for the specified URI.
99
+        $cache = $this->cache_service->get_cache( $uri );
100
+
101
+        // Return the cached data if valid.
102
+        if ( false !== $cache && is_numeric( $cache ) ) {
103
+            $this->log->debug( "Cached entity $cache for uri $uri found." );
104
+
105
+            return get_post( $cache );
106
+        }
107
+
108
+        // Get the actual result.
109
+        $post = parent::get_entity( $uri );
110
+
111
+        // Cache the result.
112
+        if ( null !== $post ) {
113
+            $this->set_cache( $uri, $post->ID );
114
+        }
115
+
116
+        // Return the result.
117
+        return $post;
118
+    }
119
+
120
+    /**
121
+     * Set the cached URI for the specified {@link WP_Post}.
122
+     *
123
+     * @param string $uri The URI.
124
+     * @param int    $post_id The post ID.
125
+     *
126
+     * @since 3.16.3
127
+     * @since 3.29.0 takes a post ID as input.
128
+     */
129
+    private function set_cache( $uri, $post_id ) {
130
+
131
+        // Cache the result.
132
+        $this->cache_service->set_cache( $uri, $post_id );
133
+
134
+    }
135
+
136
+    /**
137
+     * Delete the cache for the specified URIs.
138
+     *
139
+     * @param array $uris An array of URIs.
140
+     *
141
+     * @since 3.16.3
142
+     */
143
+    private function delete_cache( $uris ) {
144
+
145
+        // Delete the cache for each URI.
146
+        foreach ( $uris as $uri ) {
147
+            // Delete the single cache file.
148
+            $this->cache_service->delete_cache( $uri );
149
+        }
150
+
151
+    }
152
+
153
+    /**
154
+     * Before post meta change.
155
+     *
156
+     * When a post meta is going to be changed, we check if the `meta_key` is
157
+     * either the `entity_url` or the `same_as` in which case we delete the cache
158
+     * for all the associated URIs.
159
+     *
160
+     * @param int|array $meta_ids The {@link WP_Post} meta id(s).
161
+     * @param int       $post_id The {@link WP_Post} id.
162
+     * @param string    $meta_key The meta key.
163
+     * @param mixed     $meta_value The meta value(s).
164
+     *
165
+     * @since 3.16.3
166
+     */
167
+    public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) {
168
+
169
+        // Bail out if we're not interested in the meta key.
170
+        if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) {
171
+            return;
172
+        }
173
+
174
+        $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." );
175
+
176
+        // The list of existing URIs, plus the list of URIs being deleted/updated.
177
+        $old_value = get_post_meta( $post_id, $meta_key );
178
+
179
+        // We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects
180
+        // because of filters. So if it's not we return an empty array.
181
+        if ( ! is_array( $old_value ) ) {
182
+            $old_value = array();
183
+        }
184
+        $new_value = isset( $meta_value ) ? (array) $meta_value : array();
185
+        $uris      = array_merge( $old_value, $new_value );
186
+
187
+        // Delete the cache for those URIs.
188
+        $this->delete_cache( $uris );
189
+
190
+    }
191
+
192
+    /**
193
+     * Hook to meta add for a {@link WP_Post}, will cause the cache to
194
+     * invalidate.
195
+     *
196
+     * @param int    $post_id The {@link WP_Post} id.
197
+     * @param string $meta_key The meta key.
198
+     * @param mixed  $meta_value The meta value(s).
199
+     *
200
+     * @since 3.16.3
201
+     */
202
+    public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) {
203
+
204
+        // Bail out if we're not interested in the meta key.
205
+        if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) {
206
+            return;
207
+        }
208
+
209
+        $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." );
210
+
211
+        // Delete the cache for the URIs being added.
212
+        $this->delete_cache( (array) $meta_value );
213
+
214
+    }
215 215
 
216 216
 }
Please login to merge, or discard this patch.
Spacing   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -39,17 +39,17 @@  discard block
 block discarded – undo
39 39
 	 *
40 40
 	 * @since 3.16.3
41 41
 	 */
42
-	public function __construct( $cache_service ) {
42
+	public function __construct($cache_service) {
43 43
 		parent::__construct();
44 44
 
45
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
45
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
46 46
 
47 47
 		// Add hooks for meta being added/modified/deleted.
48 48
 		$this->cache_service = $cache_service;
49 49
 
50
-		add_action( 'add_post_meta', array( $this, 'on_before_post_meta_add' ), 10, 3 );
51
-		add_action( 'update_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 );
52
-		add_action( 'delete_post_meta', array( $this, 'on_before_post_meta_change' ), 10, 4 );
50
+		add_action('add_post_meta', array($this, 'on_before_post_meta_add'), 10, 3);
51
+		add_action('update_post_meta', array($this, 'on_before_post_meta_change'), 10, 4);
52
+		add_action('delete_post_meta', array($this, 'on_before_post_meta_change'), 10, 4);
53 53
 
54 54
 	}
55 55
 
@@ -60,24 +60,24 @@  discard block
 block discarded – undo
60 60
 	 *
61 61
 	 * @since 3.16.3
62 62
 	 */
63
-	public function preload_uris( $uris ) {
63
+	public function preload_uris($uris) {
64 64
 
65 65
 		// Filter the URIs which aren't yet cached.
66 66
 		$cache_service = $this->cache_service;
67 67
 		$uris_to_cache = array_filter(
68 68
 			(array) $uris,
69
-			function ( $item ) use ( $cache_service ) {
70
-				return ! $cache_service->has_cache( $item );
69
+			function($item) use ($cache_service) {
70
+				return ! $cache_service->has_cache($item);
71 71
 			}
72 72
 		);
73 73
 
74 74
 		// Preload the URIs.
75
-		parent::preload_uris( $uris_to_cache );
75
+		parent::preload_uris($uris_to_cache);
76 76
 
77 77
 		// Store them in cache.
78
-		if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) {
79
-			foreach ( $this->uri_to_post as $uri => $post_id ) {
80
-				$this->set_cache( $uri, $post_id );
78
+		if (is_array($this->uri_to_post) && ! empty($this->uri_to_post)) {
79
+			foreach ($this->uri_to_post as $uri => $post_id) {
80
+				$this->set_cache($uri, $post_id);
81 81
 			}
82 82
 		}
83 83
 
@@ -91,26 +91,26 @@  discard block
 block discarded – undo
91 91
 	 * @return null|WP_Post The {@link WP_Post} or null if not found.
92 92
 	 * @since 3.16.3
93 93
 	 */
94
-	public function get_entity( $uri ) {
94
+	public function get_entity($uri) {
95 95
 
96
-		$this->log->trace( "Getting entity for uri $uri..." );
96
+		$this->log->trace("Getting entity for uri $uri...");
97 97
 
98 98
 		// Get the cached post for the specified URI.
99
-		$cache = $this->cache_service->get_cache( $uri );
99
+		$cache = $this->cache_service->get_cache($uri);
100 100
 
101 101
 		// Return the cached data if valid.
102
-		if ( false !== $cache && is_numeric( $cache ) ) {
103
-			$this->log->debug( "Cached entity $cache for uri $uri found." );
102
+		if (false !== $cache && is_numeric($cache)) {
103
+			$this->log->debug("Cached entity $cache for uri $uri found.");
104 104
 
105
-			return get_post( $cache );
105
+			return get_post($cache);
106 106
 		}
107 107
 
108 108
 		// Get the actual result.
109
-		$post = parent::get_entity( $uri );
109
+		$post = parent::get_entity($uri);
110 110
 
111 111
 		// Cache the result.
112
-		if ( null !== $post ) {
113
-			$this->set_cache( $uri, $post->ID );
112
+		if (null !== $post) {
113
+			$this->set_cache($uri, $post->ID);
114 114
 		}
115 115
 
116 116
 		// Return the result.
@@ -126,10 +126,10 @@  discard block
 block discarded – undo
126 126
 	 * @since 3.16.3
127 127
 	 * @since 3.29.0 takes a post ID as input.
128 128
 	 */
129
-	private function set_cache( $uri, $post_id ) {
129
+	private function set_cache($uri, $post_id) {
130 130
 
131 131
 		// Cache the result.
132
-		$this->cache_service->set_cache( $uri, $post_id );
132
+		$this->cache_service->set_cache($uri, $post_id);
133 133
 
134 134
 	}
135 135
 
@@ -140,12 +140,12 @@  discard block
 block discarded – undo
140 140
 	 *
141 141
 	 * @since 3.16.3
142 142
 	 */
143
-	private function delete_cache( $uris ) {
143
+	private function delete_cache($uris) {
144 144
 
145 145
 		// Delete the cache for each URI.
146
-		foreach ( $uris as $uri ) {
146
+		foreach ($uris as $uri) {
147 147
 			// Delete the single cache file.
148
-			$this->cache_service->delete_cache( $uri );
148
+			$this->cache_service->delete_cache($uri);
149 149
 		}
150 150
 
151 151
 	}
@@ -164,28 +164,28 @@  discard block
 block discarded – undo
164 164
 	 *
165 165
 	 * @since 3.16.3
166 166
 	 */
167
-	public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) {
167
+	public function on_before_post_meta_change($meta_ids, $post_id, $meta_key, $meta_value) {
168 168
 
169 169
 		// Bail out if we're not interested in the meta key.
170
-		if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) {
170
+		if (WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key) {
171 171
 			return;
172 172
 		}
173 173
 
174
-		$this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." );
174
+		$this->log->trace("Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache...");
175 175
 
176 176
 		// The list of existing URIs, plus the list of URIs being deleted/updated.
177
-		$old_value = get_post_meta( $post_id, $meta_key );
177
+		$old_value = get_post_meta($post_id, $meta_key);
178 178
 
179 179
 		// We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects
180 180
 		// because of filters. So if it's not we return an empty array.
181
-		if ( ! is_array( $old_value ) ) {
181
+		if ( ! is_array($old_value)) {
182 182
 			$old_value = array();
183 183
 		}
184
-		$new_value = isset( $meta_value ) ? (array) $meta_value : array();
185
-		$uris      = array_merge( $old_value, $new_value );
184
+		$new_value = isset($meta_value) ? (array) $meta_value : array();
185
+		$uris      = array_merge($old_value, $new_value);
186 186
 
187 187
 		// Delete the cache for those URIs.
188
-		$this->delete_cache( $uris );
188
+		$this->delete_cache($uris);
189 189
 
190 190
 	}
191 191
 
@@ -199,17 +199,17 @@  discard block
 block discarded – undo
199 199
 	 *
200 200
 	 * @since 3.16.3
201 201
 	 */
202
-	public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) {
202
+	public function on_before_post_meta_add($post_id, $meta_key, $meta_value) {
203 203
 
204 204
 		// Bail out if we're not interested in the meta key.
205
-		if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) {
205
+		if (WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key) {
206 206
 			return;
207 207
 		}
208 208
 
209
-		$this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." );
209
+		$this->log->trace("Adding $meta_key for post $post_id ($meta_key), invalidating cache...");
210 210
 
211 211
 		// Delete the cache for the URIs being added.
212
-		$this->delete_cache( (array) $meta_value );
212
+		$this->delete_cache((array) $meta_value);
213 213
 
214 214
 	}
215 215
 
Please login to merge, or discard this patch.