@@ -14,118 +14,118 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class Wordlift_Property_Service { |
16 | 16 | |
17 | - protected static $instance; |
|
18 | - |
|
19 | - public function __construct() { |
|
20 | - |
|
21 | - static::$instance = $this; |
|
22 | - } |
|
23 | - |
|
24 | - /** |
|
25 | - * Get the field singleton. |
|
26 | - * |
|
27 | - * @since 3.6.0 |
|
28 | - * @return \Wordlift_Schema_Url_Property_Service The singleton instance. |
|
29 | - */ |
|
30 | - public static function get_instance() { |
|
31 | - |
|
32 | - return static::$instance; |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * Get the value for the specified post/entity. |
|
37 | - * |
|
38 | - * @since 3.6.0 |
|
39 | - * |
|
40 | - * @param int $post_id The post id. |
|
41 | - * |
|
42 | - * @return mixed |
|
43 | - */ |
|
44 | - abstract public function get( $post_id ); |
|
45 | - |
|
46 | - /** |
|
47 | - * Sanitize the provided value. |
|
48 | - * |
|
49 | - * @since 3.6.0 |
|
50 | - * |
|
51 | - * @param mixed $value The value to sanitize. |
|
52 | - * |
|
53 | - * @return mixed|NULL The sanitized value or NULL avoid saving this value (see {@link Wl_Metabox_Field}). |
|
54 | - */ |
|
55 | - abstract public function sanitize( $value ); |
|
56 | - |
|
57 | - /** |
|
58 | - * The RDF predicate for the property. |
|
59 | - * |
|
60 | - * @since 3.6.0 |
|
61 | - * @return string The RDF predicate. |
|
62 | - */ |
|
63 | - abstract public function get_rdf_predicate(); |
|
64 | - |
|
65 | - /** |
|
66 | - * The RDF data type. |
|
67 | - * |
|
68 | - * @since 3.6.0 |
|
69 | - * @return string The RDF data type. |
|
70 | - */ |
|
71 | - abstract public function get_rdf_data_type(); |
|
72 | - |
|
73 | - /** |
|
74 | - * The internal data type. |
|
75 | - * |
|
76 | - * @since 3.6.0 |
|
77 | - * @return string The internal data type. |
|
78 | - */ |
|
79 | - abstract public function get_data_type(); |
|
80 | - |
|
81 | - /** |
|
82 | - * The cardinality. |
|
83 | - * |
|
84 | - * @since 3.6.0 |
|
85 | - * @return mixed The cardinality. |
|
86 | - */ |
|
87 | - abstract public function get_cardinality(); |
|
88 | - |
|
89 | - /** |
|
90 | - * The metabox field class name. |
|
91 | - * |
|
92 | - * @since 3.6.0 |
|
93 | - * @return string The metabox field class name. |
|
94 | - */ |
|
95 | - abstract public function get_metabox_class(); |
|
96 | - |
|
97 | - /** |
|
98 | - * The untranslated metabox field label. |
|
99 | - * |
|
100 | - * @since 3.6.0 |
|
101 | - * @return string The untranslated metabox field label. |
|
102 | - */ |
|
103 | - abstract public function get_metabox_label(); |
|
104 | - |
|
105 | - /** |
|
106 | - * The definition of the property returned as a compatible array. |
|
107 | - * |
|
108 | - * @deprecated |
|
109 | - * |
|
110 | - * @since 3.6.0 |
|
111 | - * @return array An array of property definitions. |
|
112 | - */ |
|
113 | - public function get_compat_definition() { |
|
114 | - |
|
115 | - return array( |
|
116 | - 'type' => $this->get_data_type(), |
|
117 | - 'predicate' => $this->get_rdf_predicate(), |
|
118 | - 'export_type' => $this->get_rdf_data_type(), |
|
119 | - 'constraints' => array( |
|
120 | - 'cardinality' => $this->get_cardinality(), |
|
121 | - ), |
|
122 | - // Use the standard metabox for these URI (the URI metabox creates local entities). |
|
123 | - 'metabox' => array( |
|
124 | - 'class' => $this->get_metabox_class(), |
|
125 | - 'label' => $this->get_metabox_label(), |
|
126 | - ), |
|
127 | - 'sanitize' => array( $this, 'sanitize' ), |
|
128 | - ); |
|
129 | - } |
|
17 | + protected static $instance; |
|
18 | + |
|
19 | + public function __construct() { |
|
20 | + |
|
21 | + static::$instance = $this; |
|
22 | + } |
|
23 | + |
|
24 | + /** |
|
25 | + * Get the field singleton. |
|
26 | + * |
|
27 | + * @since 3.6.0 |
|
28 | + * @return \Wordlift_Schema_Url_Property_Service The singleton instance. |
|
29 | + */ |
|
30 | + public static function get_instance() { |
|
31 | + |
|
32 | + return static::$instance; |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * Get the value for the specified post/entity. |
|
37 | + * |
|
38 | + * @since 3.6.0 |
|
39 | + * |
|
40 | + * @param int $post_id The post id. |
|
41 | + * |
|
42 | + * @return mixed |
|
43 | + */ |
|
44 | + abstract public function get( $post_id ); |
|
45 | + |
|
46 | + /** |
|
47 | + * Sanitize the provided value. |
|
48 | + * |
|
49 | + * @since 3.6.0 |
|
50 | + * |
|
51 | + * @param mixed $value The value to sanitize. |
|
52 | + * |
|
53 | + * @return mixed|NULL The sanitized value or NULL avoid saving this value (see {@link Wl_Metabox_Field}). |
|
54 | + */ |
|
55 | + abstract public function sanitize( $value ); |
|
56 | + |
|
57 | + /** |
|
58 | + * The RDF predicate for the property. |
|
59 | + * |
|
60 | + * @since 3.6.0 |
|
61 | + * @return string The RDF predicate. |
|
62 | + */ |
|
63 | + abstract public function get_rdf_predicate(); |
|
64 | + |
|
65 | + /** |
|
66 | + * The RDF data type. |
|
67 | + * |
|
68 | + * @since 3.6.0 |
|
69 | + * @return string The RDF data type. |
|
70 | + */ |
|
71 | + abstract public function get_rdf_data_type(); |
|
72 | + |
|
73 | + /** |
|
74 | + * The internal data type. |
|
75 | + * |
|
76 | + * @since 3.6.0 |
|
77 | + * @return string The internal data type. |
|
78 | + */ |
|
79 | + abstract public function get_data_type(); |
|
80 | + |
|
81 | + /** |
|
82 | + * The cardinality. |
|
83 | + * |
|
84 | + * @since 3.6.0 |
|
85 | + * @return mixed The cardinality. |
|
86 | + */ |
|
87 | + abstract public function get_cardinality(); |
|
88 | + |
|
89 | + /** |
|
90 | + * The metabox field class name. |
|
91 | + * |
|
92 | + * @since 3.6.0 |
|
93 | + * @return string The metabox field class name. |
|
94 | + */ |
|
95 | + abstract public function get_metabox_class(); |
|
96 | + |
|
97 | + /** |
|
98 | + * The untranslated metabox field label. |
|
99 | + * |
|
100 | + * @since 3.6.0 |
|
101 | + * @return string The untranslated metabox field label. |
|
102 | + */ |
|
103 | + abstract public function get_metabox_label(); |
|
104 | + |
|
105 | + /** |
|
106 | + * The definition of the property returned as a compatible array. |
|
107 | + * |
|
108 | + * @deprecated |
|
109 | + * |
|
110 | + * @since 3.6.0 |
|
111 | + * @return array An array of property definitions. |
|
112 | + */ |
|
113 | + public function get_compat_definition() { |
|
114 | + |
|
115 | + return array( |
|
116 | + 'type' => $this->get_data_type(), |
|
117 | + 'predicate' => $this->get_rdf_predicate(), |
|
118 | + 'export_type' => $this->get_rdf_data_type(), |
|
119 | + 'constraints' => array( |
|
120 | + 'cardinality' => $this->get_cardinality(), |
|
121 | + ), |
|
122 | + // Use the standard metabox for these URI (the URI metabox creates local entities). |
|
123 | + 'metabox' => array( |
|
124 | + 'class' => $this->get_metabox_class(), |
|
125 | + 'label' => $this->get_metabox_label(), |
|
126 | + ), |
|
127 | + 'sanitize' => array( $this, 'sanitize' ), |
|
128 | + ); |
|
129 | + } |
|
130 | 130 | |
131 | 131 | } |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return mixed |
43 | 43 | */ |
44 | - abstract public function get( $post_id ); |
|
44 | + abstract public function get($post_id); |
|
45 | 45 | |
46 | 46 | /** |
47 | 47 | * Sanitize the provided value. |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return mixed|NULL The sanitized value or NULL avoid saving this value (see {@link Wl_Metabox_Field}). |
54 | 54 | */ |
55 | - abstract public function sanitize( $value ); |
|
55 | + abstract public function sanitize($value); |
|
56 | 56 | |
57 | 57 | /** |
58 | 58 | * The RDF predicate for the property. |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | 'class' => $this->get_metabox_class(), |
125 | 125 | 'label' => $this->get_metabox_label(), |
126 | 126 | ), |
127 | - 'sanitize' => array( $this, 'sanitize' ), |
|
127 | + 'sanitize' => array($this, 'sanitize'), |
|
128 | 128 | ); |
129 | 129 | } |
130 | 130 |
@@ -19,336 +19,336 @@ |
||
19 | 19 | */ |
20 | 20 | class Wordlift_User_Service { |
21 | 21 | |
22 | - /** |
|
23 | - * The meta key where the user's URI is stored. |
|
24 | - * |
|
25 | - * @since 3.1.7 |
|
26 | - */ |
|
27 | - const URI_META_KEY = '_wl_uri'; |
|
28 | - |
|
29 | - /** |
|
30 | - * The user meta key where the deny entity edit flag is stored. |
|
31 | - * |
|
32 | - * @since 3.14.0 |
|
33 | - */ |
|
34 | - const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
|
35 | - |
|
36 | - /** |
|
37 | - * The meta key holding the entity id representing a {@link WP_User}. |
|
38 | - * |
|
39 | - * @since 3.14.0 |
|
40 | - */ |
|
41 | - const ENTITY_META_KEY = '_wl_entity'; |
|
42 | - |
|
43 | - /** |
|
44 | - * The Log service. |
|
45 | - * |
|
46 | - * @since 3.1.7 |
|
47 | - * @access private |
|
48 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
49 | - */ |
|
50 | - private $log_service; |
|
51 | - |
|
52 | - /** |
|
53 | - * Create an instance of the User service. |
|
54 | - * |
|
55 | - * @since 3.1.7 |
|
56 | - */ |
|
57 | - protected function __construct() { |
|
58 | - |
|
59 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
60 | - |
|
61 | - add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
62 | - |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * The singleton instance of the User service. |
|
67 | - * |
|
68 | - * @since 3.1.7 |
|
69 | - * @access private |
|
70 | - * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
71 | - */ |
|
72 | - private static $instance = null; |
|
73 | - |
|
74 | - /** |
|
75 | - * Get the singleton instance of the User service. |
|
76 | - * |
|
77 | - * @return \Wordlift_User_Service The singleton instance of the User service. |
|
78 | - * @since 3.1.7 |
|
79 | - */ |
|
80 | - public static function get_instance() { |
|
81 | - |
|
82 | - if ( ! isset( self::$instance ) ) { |
|
83 | - self::$instance = new self(); |
|
84 | - } |
|
85 | - |
|
86 | - return self::$instance; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Get the URI for a user. |
|
91 | - * |
|
92 | - * @param int $user_id The user id |
|
93 | - * |
|
94 | - * @return false|string The user's URI or false in case of failure. |
|
95 | - * @since 3.1.7 |
|
96 | - */ |
|
97 | - public function get_uri( $user_id ) { |
|
98 | - |
|
99 | - // Try to get the URI stored in the user's meta and return it if available. |
|
100 | - $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
101 | - $user_uri = $this->_get_uri( $user_id ); |
|
102 | - if ( ! empty( $dataset_uri ) && ! empty( $user_uri ) && 0 === strpos( $user_uri, $dataset_uri ) ) { |
|
103 | - return $user_uri; |
|
104 | - } |
|
105 | - |
|
106 | - // Try to build an URI, return false in case of failure. |
|
107 | - $user_uri = $this->_build_uri( $user_id ); |
|
108 | - if ( false === $user_uri ) { |
|
109 | - return false; |
|
110 | - } |
|
111 | - |
|
112 | - // Store the URI for future requests (we need a "permanent" URI). |
|
113 | - $this->_set_uri( $user_id, $user_uri ); |
|
114 | - |
|
115 | - return $user_uri; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Set the `id` of the entity representing a {@link WP_User}. |
|
120 | - * |
|
121 | - * If the `id` is set to 0 (or less) then the meta is deleted. |
|
122 | - * |
|
123 | - * @param int $user_id The {@link WP_User}. |
|
124 | - * @param int $value The entity {@link WP_Post} `id`. |
|
125 | - * |
|
126 | - * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
|
127 | - * @since 3.14.0 |
|
128 | - */ |
|
129 | - public function set_entity( $user_id, $value ) { |
|
130 | - |
|
131 | - return 0 < $value |
|
132 | - ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
133 | - : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
|
138 | - * |
|
139 | - * @param int $user_id The {@link WP_User}'s `id`. |
|
140 | - * |
|
141 | - * @return string|false The entity {@link WP_Post} `id` or an empty string if not set or false if the object id is invalid |
|
142 | - * @since 3.14.0 |
|
143 | - */ |
|
144 | - public function get_entity( $user_id ) { |
|
145 | - |
|
146 | - return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Get the user's URI stored in the user's meta. |
|
151 | - * |
|
152 | - * @param int $user_id The user id. |
|
153 | - * |
|
154 | - * @return false|string The user's URI or false if not found. |
|
155 | - * @since 3.1.7 |
|
156 | - */ |
|
157 | - // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
|
158 | - private function _get_uri( $user_id ) { |
|
159 | - |
|
160 | - $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
161 | - |
|
162 | - if ( empty( $user_uri ) ) { |
|
163 | - return false; |
|
164 | - } |
|
165 | - |
|
166 | - return $user_uri; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Build an URI for a user. |
|
171 | - * |
|
172 | - * @param int $user_id The user's id. |
|
173 | - * |
|
174 | - * @return false|string The user's URI or false in case of failure. |
|
175 | - * @since 3.1.7 |
|
176 | - */ |
|
177 | - // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
|
178 | - private function _build_uri( $user_id ) { |
|
179 | - |
|
180 | - // Get the user, return false in case of failure. |
|
181 | - $user = get_userdata( $user_id ); |
|
182 | - if ( false === $user ) { |
|
183 | - return false; |
|
184 | - }; |
|
185 | - |
|
186 | - // If the nicename is not set, return a failure. |
|
187 | - if ( empty( $user->user_nicename ) ) { |
|
188 | - return false; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * @since 3.27.7 changed `user` to `author` to avoid potential clashes with CPTs ( `author` is reserved |
|
193 | - * https://developer.wordpress.org/reference/functions/register_post_type/#reserved-post-types ) |
|
194 | - */ |
|
195 | - return untrailingslashit( wl_configuration_get_redlink_dataset_uri() ) . "/author/$user->user_nicename"; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Store the URI in user's meta. |
|
200 | - * |
|
201 | - * @param int $user_id The user's id. |
|
202 | - * @param string $user_uri The user's uri. |
|
203 | - * |
|
204 | - * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
205 | - * @since 3.1.7 |
|
206 | - */ |
|
207 | - // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
|
208 | - private function _set_uri( $user_id, $user_uri ) { |
|
209 | - |
|
210 | - return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Mark an editor user as denied from editing entities. |
|
215 | - * Does nothing if the user is not an editor |
|
216 | - * |
|
217 | - * @param integer $user_id The ID of the user |
|
218 | - * |
|
219 | - * @since 3.14.0 |
|
220 | - */ |
|
221 | - public function deny_editor_entity_create( $user_id ) { |
|
222 | - |
|
223 | - // Bail out if the user is not an editor. |
|
224 | - if ( ! $this->is_editor( $user_id ) ) { |
|
225 | - return; |
|
226 | - } |
|
227 | - |
|
228 | - // The user explicitly do not have the capability. |
|
229 | - update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
230 | - |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Remove the "deny entity editing" mark from an editor user. |
|
235 | - * Does nothing if the user is not an editor |
|
236 | - * |
|
237 | - * @param integer $user_id The ID of the user |
|
238 | - * |
|
239 | - * @since 3.14.0 |
|
240 | - */ |
|
241 | - public function allow_editor_entity_create( $user_id ) { |
|
242 | - |
|
243 | - // Bail out if the user is not an editor. |
|
244 | - if ( ! $this->is_editor( $user_id ) ) { |
|
245 | - return; |
|
246 | - } |
|
247 | - |
|
248 | - // The user explicitly do not have the capability. |
|
249 | - delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
250 | - |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Get whether the 'deny editor entity editing' flag is set. |
|
255 | - * |
|
256 | - * @param int $user_id The {@link WP_User} `id`. |
|
257 | - * |
|
258 | - * @return int bool True if editing is denied otherwise false. |
|
259 | - * @since 3.14.0 |
|
260 | - */ |
|
261 | - public function is_deny_editor_entity_create( $user_id ) { |
|
262 | - |
|
263 | - return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Check whether the {@link WP_User} with the specified `id` is an editor, |
|
268 | - * i.e. has the `editor` role. |
|
269 | - * |
|
270 | - * @param int $user_id The {@link WP_User} `id`. |
|
271 | - * |
|
272 | - * @return bool True if the {@link WP_User} is an editor otherwise false. |
|
273 | - * @since 3.14.0 |
|
274 | - */ |
|
275 | - public function is_editor( $user_id ) { |
|
276 | - |
|
277 | - // Get the user. |
|
278 | - $user = get_user_by( 'id', $user_id ); |
|
279 | - |
|
280 | - // Return true, if the user is found and has the `editor` role. |
|
281 | - return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles, true ); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Check if an editor can create entities. |
|
286 | - * |
|
287 | - * @param int $user_id The user id of the user being checked. |
|
288 | - * |
|
289 | - * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
|
290 | - * @since 3.14.0 |
|
291 | - */ |
|
292 | - public function editor_can_create_entities( $user_id ) { |
|
293 | - |
|
294 | - // Return true if not an editor. |
|
295 | - if ( ! $this->is_editor( $user_id ) ) { |
|
296 | - return true; |
|
297 | - } |
|
298 | - |
|
299 | - // Check if the user explicitly denied. |
|
300 | - return ! $this->is_deny_editor_entity_create( $user_id ); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Filter capabilities of user. |
|
305 | - * |
|
306 | - * Deny the capability of managing and editing entities for some users. |
|
307 | - * |
|
308 | - * @param array $allcaps All the capabilities of the user |
|
309 | - * @param array $cap [0] Required capability |
|
310 | - * @param array $args [0] Requested capability |
|
311 | - * [1] User ID |
|
312 | - * [2] Associated object ID |
|
313 | - * |
|
314 | - * @return array The capabilities array. |
|
315 | - * @since 3.14.0 |
|
316 | - */ |
|
317 | - public function has_cap( $allcaps, $cap, $args ) { |
|
318 | - /* |
|
22 | + /** |
|
23 | + * The meta key where the user's URI is stored. |
|
24 | + * |
|
25 | + * @since 3.1.7 |
|
26 | + */ |
|
27 | + const URI_META_KEY = '_wl_uri'; |
|
28 | + |
|
29 | + /** |
|
30 | + * The user meta key where the deny entity edit flag is stored. |
|
31 | + * |
|
32 | + * @since 3.14.0 |
|
33 | + */ |
|
34 | + const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
|
35 | + |
|
36 | + /** |
|
37 | + * The meta key holding the entity id representing a {@link WP_User}. |
|
38 | + * |
|
39 | + * @since 3.14.0 |
|
40 | + */ |
|
41 | + const ENTITY_META_KEY = '_wl_entity'; |
|
42 | + |
|
43 | + /** |
|
44 | + * The Log service. |
|
45 | + * |
|
46 | + * @since 3.1.7 |
|
47 | + * @access private |
|
48 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
49 | + */ |
|
50 | + private $log_service; |
|
51 | + |
|
52 | + /** |
|
53 | + * Create an instance of the User service. |
|
54 | + * |
|
55 | + * @since 3.1.7 |
|
56 | + */ |
|
57 | + protected function __construct() { |
|
58 | + |
|
59 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
60 | + |
|
61 | + add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
62 | + |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * The singleton instance of the User service. |
|
67 | + * |
|
68 | + * @since 3.1.7 |
|
69 | + * @access private |
|
70 | + * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
71 | + */ |
|
72 | + private static $instance = null; |
|
73 | + |
|
74 | + /** |
|
75 | + * Get the singleton instance of the User service. |
|
76 | + * |
|
77 | + * @return \Wordlift_User_Service The singleton instance of the User service. |
|
78 | + * @since 3.1.7 |
|
79 | + */ |
|
80 | + public static function get_instance() { |
|
81 | + |
|
82 | + if ( ! isset( self::$instance ) ) { |
|
83 | + self::$instance = new self(); |
|
84 | + } |
|
85 | + |
|
86 | + return self::$instance; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Get the URI for a user. |
|
91 | + * |
|
92 | + * @param int $user_id The user id |
|
93 | + * |
|
94 | + * @return false|string The user's URI or false in case of failure. |
|
95 | + * @since 3.1.7 |
|
96 | + */ |
|
97 | + public function get_uri( $user_id ) { |
|
98 | + |
|
99 | + // Try to get the URI stored in the user's meta and return it if available. |
|
100 | + $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
101 | + $user_uri = $this->_get_uri( $user_id ); |
|
102 | + if ( ! empty( $dataset_uri ) && ! empty( $user_uri ) && 0 === strpos( $user_uri, $dataset_uri ) ) { |
|
103 | + return $user_uri; |
|
104 | + } |
|
105 | + |
|
106 | + // Try to build an URI, return false in case of failure. |
|
107 | + $user_uri = $this->_build_uri( $user_id ); |
|
108 | + if ( false === $user_uri ) { |
|
109 | + return false; |
|
110 | + } |
|
111 | + |
|
112 | + // Store the URI for future requests (we need a "permanent" URI). |
|
113 | + $this->_set_uri( $user_id, $user_uri ); |
|
114 | + |
|
115 | + return $user_uri; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Set the `id` of the entity representing a {@link WP_User}. |
|
120 | + * |
|
121 | + * If the `id` is set to 0 (or less) then the meta is deleted. |
|
122 | + * |
|
123 | + * @param int $user_id The {@link WP_User}. |
|
124 | + * @param int $value The entity {@link WP_Post} `id`. |
|
125 | + * |
|
126 | + * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
|
127 | + * @since 3.14.0 |
|
128 | + */ |
|
129 | + public function set_entity( $user_id, $value ) { |
|
130 | + |
|
131 | + return 0 < $value |
|
132 | + ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
133 | + : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
|
138 | + * |
|
139 | + * @param int $user_id The {@link WP_User}'s `id`. |
|
140 | + * |
|
141 | + * @return string|false The entity {@link WP_Post} `id` or an empty string if not set or false if the object id is invalid |
|
142 | + * @since 3.14.0 |
|
143 | + */ |
|
144 | + public function get_entity( $user_id ) { |
|
145 | + |
|
146 | + return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Get the user's URI stored in the user's meta. |
|
151 | + * |
|
152 | + * @param int $user_id The user id. |
|
153 | + * |
|
154 | + * @return false|string The user's URI or false if not found. |
|
155 | + * @since 3.1.7 |
|
156 | + */ |
|
157 | + // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
|
158 | + private function _get_uri( $user_id ) { |
|
159 | + |
|
160 | + $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
161 | + |
|
162 | + if ( empty( $user_uri ) ) { |
|
163 | + return false; |
|
164 | + } |
|
165 | + |
|
166 | + return $user_uri; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Build an URI for a user. |
|
171 | + * |
|
172 | + * @param int $user_id The user's id. |
|
173 | + * |
|
174 | + * @return false|string The user's URI or false in case of failure. |
|
175 | + * @since 3.1.7 |
|
176 | + */ |
|
177 | + // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
|
178 | + private function _build_uri( $user_id ) { |
|
179 | + |
|
180 | + // Get the user, return false in case of failure. |
|
181 | + $user = get_userdata( $user_id ); |
|
182 | + if ( false === $user ) { |
|
183 | + return false; |
|
184 | + }; |
|
185 | + |
|
186 | + // If the nicename is not set, return a failure. |
|
187 | + if ( empty( $user->user_nicename ) ) { |
|
188 | + return false; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * @since 3.27.7 changed `user` to `author` to avoid potential clashes with CPTs ( `author` is reserved |
|
193 | + * https://developer.wordpress.org/reference/functions/register_post_type/#reserved-post-types ) |
|
194 | + */ |
|
195 | + return untrailingslashit( wl_configuration_get_redlink_dataset_uri() ) . "/author/$user->user_nicename"; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Store the URI in user's meta. |
|
200 | + * |
|
201 | + * @param int $user_id The user's id. |
|
202 | + * @param string $user_uri The user's uri. |
|
203 | + * |
|
204 | + * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
205 | + * @since 3.1.7 |
|
206 | + */ |
|
207 | + // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
|
208 | + private function _set_uri( $user_id, $user_uri ) { |
|
209 | + |
|
210 | + return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Mark an editor user as denied from editing entities. |
|
215 | + * Does nothing if the user is not an editor |
|
216 | + * |
|
217 | + * @param integer $user_id The ID of the user |
|
218 | + * |
|
219 | + * @since 3.14.0 |
|
220 | + */ |
|
221 | + public function deny_editor_entity_create( $user_id ) { |
|
222 | + |
|
223 | + // Bail out if the user is not an editor. |
|
224 | + if ( ! $this->is_editor( $user_id ) ) { |
|
225 | + return; |
|
226 | + } |
|
227 | + |
|
228 | + // The user explicitly do not have the capability. |
|
229 | + update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
230 | + |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Remove the "deny entity editing" mark from an editor user. |
|
235 | + * Does nothing if the user is not an editor |
|
236 | + * |
|
237 | + * @param integer $user_id The ID of the user |
|
238 | + * |
|
239 | + * @since 3.14.0 |
|
240 | + */ |
|
241 | + public function allow_editor_entity_create( $user_id ) { |
|
242 | + |
|
243 | + // Bail out if the user is not an editor. |
|
244 | + if ( ! $this->is_editor( $user_id ) ) { |
|
245 | + return; |
|
246 | + } |
|
247 | + |
|
248 | + // The user explicitly do not have the capability. |
|
249 | + delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
250 | + |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Get whether the 'deny editor entity editing' flag is set. |
|
255 | + * |
|
256 | + * @param int $user_id The {@link WP_User} `id`. |
|
257 | + * |
|
258 | + * @return int bool True if editing is denied otherwise false. |
|
259 | + * @since 3.14.0 |
|
260 | + */ |
|
261 | + public function is_deny_editor_entity_create( $user_id ) { |
|
262 | + |
|
263 | + return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Check whether the {@link WP_User} with the specified `id` is an editor, |
|
268 | + * i.e. has the `editor` role. |
|
269 | + * |
|
270 | + * @param int $user_id The {@link WP_User} `id`. |
|
271 | + * |
|
272 | + * @return bool True if the {@link WP_User} is an editor otherwise false. |
|
273 | + * @since 3.14.0 |
|
274 | + */ |
|
275 | + public function is_editor( $user_id ) { |
|
276 | + |
|
277 | + // Get the user. |
|
278 | + $user = get_user_by( 'id', $user_id ); |
|
279 | + |
|
280 | + // Return true, if the user is found and has the `editor` role. |
|
281 | + return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles, true ); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Check if an editor can create entities. |
|
286 | + * |
|
287 | + * @param int $user_id The user id of the user being checked. |
|
288 | + * |
|
289 | + * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
|
290 | + * @since 3.14.0 |
|
291 | + */ |
|
292 | + public function editor_can_create_entities( $user_id ) { |
|
293 | + |
|
294 | + // Return true if not an editor. |
|
295 | + if ( ! $this->is_editor( $user_id ) ) { |
|
296 | + return true; |
|
297 | + } |
|
298 | + |
|
299 | + // Check if the user explicitly denied. |
|
300 | + return ! $this->is_deny_editor_entity_create( $user_id ); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Filter capabilities of user. |
|
305 | + * |
|
306 | + * Deny the capability of managing and editing entities for some users. |
|
307 | + * |
|
308 | + * @param array $allcaps All the capabilities of the user |
|
309 | + * @param array $cap [0] Required capability |
|
310 | + * @param array $args [0] Requested capability |
|
311 | + * [1] User ID |
|
312 | + * [2] Associated object ID |
|
313 | + * |
|
314 | + * @return array The capabilities array. |
|
315 | + * @since 3.14.0 |
|
316 | + */ |
|
317 | + public function has_cap( $allcaps, $cap, $args ) { |
|
318 | + /* |
|
319 | 319 | * For entity management/editing related capabilities |
320 | 320 | * check that an editor was not explicitly denied (in user profile) |
321 | 321 | * the capability. |
322 | 322 | */ |
323 | 323 | |
324 | - /* |
|
324 | + /* |
|
325 | 325 | * Need protection against the case of edit_user and likes which do not |
326 | 326 | * require a capability, just request one. |
327 | 327 | */ |
328 | - if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
329 | - return $allcaps; |
|
330 | - } |
|
331 | - |
|
332 | - if ( |
|
333 | - ( 'edit_wordlift_entity' === $cap[0] ) || |
|
334 | - ( 'edit_wordlift_entities' === $cap[0] ) || |
|
335 | - ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
336 | - ( 'publish_wordlift_entities' === $cap[0] ) || |
|
337 | - ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
338 | - ( 'delete_wordlift_entity' === $cap[0] ) || |
|
339 | - ( 'delete_wordlift_entities' === $cap[0] ) || |
|
340 | - ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
341 | - ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
342 | - ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
343 | - ) { |
|
344 | - $user_id = $args[1]; |
|
345 | - |
|
346 | - if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
347 | - $allcaps[ $cap[0] ] = false; |
|
348 | - } |
|
349 | - } |
|
350 | - |
|
351 | - return $allcaps; |
|
352 | - } |
|
328 | + if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
329 | + return $allcaps; |
|
330 | + } |
|
331 | + |
|
332 | + if ( |
|
333 | + ( 'edit_wordlift_entity' === $cap[0] ) || |
|
334 | + ( 'edit_wordlift_entities' === $cap[0] ) || |
|
335 | + ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
336 | + ( 'publish_wordlift_entities' === $cap[0] ) || |
|
337 | + ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
338 | + ( 'delete_wordlift_entity' === $cap[0] ) || |
|
339 | + ( 'delete_wordlift_entities' === $cap[0] ) || |
|
340 | + ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
341 | + ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
342 | + ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
343 | + ) { |
|
344 | + $user_id = $args[1]; |
|
345 | + |
|
346 | + if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
347 | + $allcaps[ $cap[0] ] = false; |
|
348 | + } |
|
349 | + } |
|
350 | + |
|
351 | + return $allcaps; |
|
352 | + } |
|
353 | 353 | |
354 | 354 | } |
@@ -56,9 +56,9 @@ discard block |
||
56 | 56 | */ |
57 | 57 | protected function __construct() { |
58 | 58 | |
59 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
59 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_User_Service'); |
|
60 | 60 | |
61 | - add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
61 | + add_filter('user_has_cap', array($this, 'has_cap'), 10, 3); |
|
62 | 62 | |
63 | 63 | } |
64 | 64 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | */ |
80 | 80 | public static function get_instance() { |
81 | 81 | |
82 | - if ( ! isset( self::$instance ) ) { |
|
82 | + if ( ! isset(self::$instance)) { |
|
83 | 83 | self::$instance = new self(); |
84 | 84 | } |
85 | 85 | |
@@ -94,23 +94,23 @@ discard block |
||
94 | 94 | * @return false|string The user's URI or false in case of failure. |
95 | 95 | * @since 3.1.7 |
96 | 96 | */ |
97 | - public function get_uri( $user_id ) { |
|
97 | + public function get_uri($user_id) { |
|
98 | 98 | |
99 | 99 | // Try to get the URI stored in the user's meta and return it if available. |
100 | 100 | $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
101 | - $user_uri = $this->_get_uri( $user_id ); |
|
102 | - if ( ! empty( $dataset_uri ) && ! empty( $user_uri ) && 0 === strpos( $user_uri, $dataset_uri ) ) { |
|
101 | + $user_uri = $this->_get_uri($user_id); |
|
102 | + if ( ! empty($dataset_uri) && ! empty($user_uri) && 0 === strpos($user_uri, $dataset_uri)) { |
|
103 | 103 | return $user_uri; |
104 | 104 | } |
105 | 105 | |
106 | 106 | // Try to build an URI, return false in case of failure. |
107 | - $user_uri = $this->_build_uri( $user_id ); |
|
108 | - if ( false === $user_uri ) { |
|
107 | + $user_uri = $this->_build_uri($user_id); |
|
108 | + if (false === $user_uri) { |
|
109 | 109 | return false; |
110 | 110 | } |
111 | 111 | |
112 | 112 | // Store the URI for future requests (we need a "permanent" URI). |
113 | - $this->_set_uri( $user_id, $user_uri ); |
|
113 | + $this->_set_uri($user_id, $user_uri); |
|
114 | 114 | |
115 | 115 | return $user_uri; |
116 | 116 | } |
@@ -126,11 +126,11 @@ discard block |
||
126 | 126 | * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
127 | 127 | * @since 3.14.0 |
128 | 128 | */ |
129 | - public function set_entity( $user_id, $value ) { |
|
129 | + public function set_entity($user_id, $value) { |
|
130 | 130 | |
131 | 131 | return 0 < $value |
132 | - ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
133 | - : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
132 | + ? update_user_meta($user_id, self::ENTITY_META_KEY, $value) |
|
133 | + : delete_user_meta($user_id, self::ENTITY_META_KEY); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -141,9 +141,9 @@ discard block |
||
141 | 141 | * @return string|false The entity {@link WP_Post} `id` or an empty string if not set or false if the object id is invalid |
142 | 142 | * @since 3.14.0 |
143 | 143 | */ |
144 | - public function get_entity( $user_id ) { |
|
144 | + public function get_entity($user_id) { |
|
145 | 145 | |
146 | - return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
146 | + return get_user_meta($user_id, self::ENTITY_META_KEY, true); |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | /** |
@@ -155,11 +155,11 @@ discard block |
||
155 | 155 | * @since 3.1.7 |
156 | 156 | */ |
157 | 157 | // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
158 | - private function _get_uri( $user_id ) { |
|
158 | + private function _get_uri($user_id) { |
|
159 | 159 | |
160 | - $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
160 | + $user_uri = get_user_meta($user_id, self::URI_META_KEY, true); |
|
161 | 161 | |
162 | - if ( empty( $user_uri ) ) { |
|
162 | + if (empty($user_uri)) { |
|
163 | 163 | return false; |
164 | 164 | } |
165 | 165 | |
@@ -175,16 +175,16 @@ discard block |
||
175 | 175 | * @since 3.1.7 |
176 | 176 | */ |
177 | 177 | // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
178 | - private function _build_uri( $user_id ) { |
|
178 | + private function _build_uri($user_id) { |
|
179 | 179 | |
180 | 180 | // Get the user, return false in case of failure. |
181 | - $user = get_userdata( $user_id ); |
|
182 | - if ( false === $user ) { |
|
181 | + $user = get_userdata($user_id); |
|
182 | + if (false === $user) { |
|
183 | 183 | return false; |
184 | 184 | }; |
185 | 185 | |
186 | 186 | // If the nicename is not set, return a failure. |
187 | - if ( empty( $user->user_nicename ) ) { |
|
187 | + if (empty($user->user_nicename)) { |
|
188 | 188 | return false; |
189 | 189 | } |
190 | 190 | |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | * @since 3.27.7 changed `user` to `author` to avoid potential clashes with CPTs ( `author` is reserved |
193 | 193 | * https://developer.wordpress.org/reference/functions/register_post_type/#reserved-post-types ) |
194 | 194 | */ |
195 | - return untrailingslashit( wl_configuration_get_redlink_dataset_uri() ) . "/author/$user->user_nicename"; |
|
195 | + return untrailingslashit(wl_configuration_get_redlink_dataset_uri())."/author/$user->user_nicename"; |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | /** |
@@ -205,9 +205,9 @@ discard block |
||
205 | 205 | * @since 3.1.7 |
206 | 206 | */ |
207 | 207 | // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
208 | - private function _set_uri( $user_id, $user_uri ) { |
|
208 | + private function _set_uri($user_id, $user_uri) { |
|
209 | 209 | |
210 | - return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
210 | + return update_user_meta($user_id, self::URI_META_KEY, $user_uri); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | /** |
@@ -218,15 +218,15 @@ discard block |
||
218 | 218 | * |
219 | 219 | * @since 3.14.0 |
220 | 220 | */ |
221 | - public function deny_editor_entity_create( $user_id ) { |
|
221 | + public function deny_editor_entity_create($user_id) { |
|
222 | 222 | |
223 | 223 | // Bail out if the user is not an editor. |
224 | - if ( ! $this->is_editor( $user_id ) ) { |
|
224 | + if ( ! $this->is_editor($user_id)) { |
|
225 | 225 | return; |
226 | 226 | } |
227 | 227 | |
228 | 228 | // The user explicitly do not have the capability. |
229 | - update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
229 | + update_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes'); |
|
230 | 230 | |
231 | 231 | } |
232 | 232 | |
@@ -238,15 +238,15 @@ discard block |
||
238 | 238 | * |
239 | 239 | * @since 3.14.0 |
240 | 240 | */ |
241 | - public function allow_editor_entity_create( $user_id ) { |
|
241 | + public function allow_editor_entity_create($user_id) { |
|
242 | 242 | |
243 | 243 | // Bail out if the user is not an editor. |
244 | - if ( ! $this->is_editor( $user_id ) ) { |
|
244 | + if ( ! $this->is_editor($user_id)) { |
|
245 | 245 | return; |
246 | 246 | } |
247 | 247 | |
248 | 248 | // The user explicitly do not have the capability. |
249 | - delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
249 | + delete_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY); |
|
250 | 250 | |
251 | 251 | } |
252 | 252 | |
@@ -258,9 +258,9 @@ discard block |
||
258 | 258 | * @return int bool True if editing is denied otherwise false. |
259 | 259 | * @since 3.14.0 |
260 | 260 | */ |
261 | - public function is_deny_editor_entity_create( $user_id ) { |
|
261 | + public function is_deny_editor_entity_create($user_id) { |
|
262 | 262 | |
263 | - return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
263 | + return 'yes' === get_user_option(self::DENY_ENTITY_CREATE_META_KEY, $user_id); |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | /** |
@@ -272,13 +272,13 @@ discard block |
||
272 | 272 | * @return bool True if the {@link WP_User} is an editor otherwise false. |
273 | 273 | * @since 3.14.0 |
274 | 274 | */ |
275 | - public function is_editor( $user_id ) { |
|
275 | + public function is_editor($user_id) { |
|
276 | 276 | |
277 | 277 | // Get the user. |
278 | - $user = get_user_by( 'id', $user_id ); |
|
278 | + $user = get_user_by('id', $user_id); |
|
279 | 279 | |
280 | 280 | // Return true, if the user is found and has the `editor` role. |
281 | - return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles, true ); |
|
281 | + return is_a($user, 'WP_User') && in_array('editor', (array) $user->roles, true); |
|
282 | 282 | } |
283 | 283 | |
284 | 284 | /** |
@@ -289,15 +289,15 @@ discard block |
||
289 | 289 | * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
290 | 290 | * @since 3.14.0 |
291 | 291 | */ |
292 | - public function editor_can_create_entities( $user_id ) { |
|
292 | + public function editor_can_create_entities($user_id) { |
|
293 | 293 | |
294 | 294 | // Return true if not an editor. |
295 | - if ( ! $this->is_editor( $user_id ) ) { |
|
295 | + if ( ! $this->is_editor($user_id)) { |
|
296 | 296 | return true; |
297 | 297 | } |
298 | 298 | |
299 | 299 | // Check if the user explicitly denied. |
300 | - return ! $this->is_deny_editor_entity_create( $user_id ); |
|
300 | + return ! $this->is_deny_editor_entity_create($user_id); |
|
301 | 301 | } |
302 | 302 | |
303 | 303 | /** |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | * @return array The capabilities array. |
315 | 315 | * @since 3.14.0 |
316 | 316 | */ |
317 | - public function has_cap( $allcaps, $cap, $args ) { |
|
317 | + public function has_cap($allcaps, $cap, $args) { |
|
318 | 318 | /* |
319 | 319 | * For entity management/editing related capabilities |
320 | 320 | * check that an editor was not explicitly denied (in user profile) |
@@ -325,26 +325,26 @@ discard block |
||
325 | 325 | * Need protection against the case of edit_user and likes which do not |
326 | 326 | * require a capability, just request one. |
327 | 327 | */ |
328 | - if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
328 | + if (empty($cap) || ! isset($cap[0])) { |
|
329 | 329 | return $allcaps; |
330 | 330 | } |
331 | 331 | |
332 | 332 | if ( |
333 | - ( 'edit_wordlift_entity' === $cap[0] ) || |
|
334 | - ( 'edit_wordlift_entities' === $cap[0] ) || |
|
335 | - ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
336 | - ( 'publish_wordlift_entities' === $cap[0] ) || |
|
337 | - ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
338 | - ( 'delete_wordlift_entity' === $cap[0] ) || |
|
339 | - ( 'delete_wordlift_entities' === $cap[0] ) || |
|
340 | - ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
341 | - ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
342 | - ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
333 | + ('edit_wordlift_entity' === $cap[0]) || |
|
334 | + ('edit_wordlift_entities' === $cap[0]) || |
|
335 | + ('edit_others_wordlift_entities' === $cap[0]) || |
|
336 | + ('publish_wordlift_entities' === $cap[0]) || |
|
337 | + ('read_private_wordlift_entities' === $cap[0]) || |
|
338 | + ('delete_wordlift_entity' === $cap[0]) || |
|
339 | + ('delete_wordlift_entities' === $cap[0]) || |
|
340 | + ('delete_others_wordlift_entities' === $cap[0]) || |
|
341 | + ('delete_published_wordlift_entities' === $cap[0]) || |
|
342 | + ('delete_private_wordlift_entities' === $cap[0]) |
|
343 | 343 | ) { |
344 | 344 | $user_id = $args[1]; |
345 | 345 | |
346 | - if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
347 | - $allcaps[ $cap[0] ] = false; |
|
346 | + if ( ! $this->editor_can_create_entities($user_id)) { |
|
347 | + $allcaps[$cap[0]] = false; |
|
348 | 348 | } |
349 | 349 | } |
350 | 350 |
@@ -17,59 +17,59 @@ |
||
17 | 17 | */ |
18 | 18 | class Wordlift_Sample_Data_Ajax_Adapter { |
19 | 19 | |
20 | - /** |
|
21 | - * The {@link Wordlift_Sample_Data_Service} instance. |
|
22 | - * |
|
23 | - * @since 3.12.0 |
|
24 | - * @access private |
|
25 | - * @var \Wordlift_Sample_Data_Service $sample_data_service The {@link Wordlift_Sample_Data_Service} instance. |
|
26 | - */ |
|
27 | - private $sample_data_service; |
|
20 | + /** |
|
21 | + * The {@link Wordlift_Sample_Data_Service} instance. |
|
22 | + * |
|
23 | + * @since 3.12.0 |
|
24 | + * @access private |
|
25 | + * @var \Wordlift_Sample_Data_Service $sample_data_service The {@link Wordlift_Sample_Data_Service} instance. |
|
26 | + */ |
|
27 | + private $sample_data_service; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Create a {@link Wordlift_Sample_Data_Ajax_Adapter} instance. |
|
31 | - * |
|
32 | - * @since 3.12.0 |
|
33 | - * |
|
34 | - * @param \Wordlift_Sample_Data_Service $sample_data_service The {@link Wordlift_Sample_Data_Service} instance. |
|
35 | - */ |
|
36 | - public function __construct( $sample_data_service ) { |
|
29 | + /** |
|
30 | + * Create a {@link Wordlift_Sample_Data_Ajax_Adapter} instance. |
|
31 | + * |
|
32 | + * @since 3.12.0 |
|
33 | + * |
|
34 | + * @param \Wordlift_Sample_Data_Service $sample_data_service The {@link Wordlift_Sample_Data_Service} instance. |
|
35 | + */ |
|
36 | + public function __construct( $sample_data_service ) { |
|
37 | 37 | |
38 | - $this->sample_data_service = $sample_data_service; |
|
38 | + $this->sample_data_service = $sample_data_service; |
|
39 | 39 | |
40 | - } |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Handle the `wl_sample_data_create` ajax action. |
|
44 | - * |
|
45 | - * @since 3.12.0 |
|
46 | - */ |
|
47 | - public function create() { |
|
42 | + /** |
|
43 | + * Handle the `wl_sample_data_create` ajax action. |
|
44 | + * |
|
45 | + * @since 3.12.0 |
|
46 | + */ |
|
47 | + public function create() { |
|
48 | 48 | |
49 | - // Clean any potential garbage before us. |
|
50 | - ob_clean(); |
|
49 | + // Clean any potential garbage before us. |
|
50 | + ob_clean(); |
|
51 | 51 | |
52 | - // Create the sample data. |
|
53 | - $this->sample_data_service->create(); |
|
52 | + // Create the sample data. |
|
53 | + $this->sample_data_service->create(); |
|
54 | 54 | |
55 | - // Send success. |
|
56 | - wp_send_json_success(); |
|
55 | + // Send success. |
|
56 | + wp_send_json_success(); |
|
57 | 57 | |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - public function delete() { |
|
60 | + public function delete() { |
|
61 | 61 | |
62 | - // Clean any potential garbage before us. |
|
63 | - ob_clean(); |
|
62 | + // Clean any potential garbage before us. |
|
63 | + ob_clean(); |
|
64 | 64 | |
65 | - // Create the sample data. |
|
66 | - $this->sample_data_service->delete(); |
|
65 | + // Create the sample data. |
|
66 | + $this->sample_data_service->delete(); |
|
67 | 67 | |
68 | - // Send success. |
|
69 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
70 | - @header( 'Content-Disposition: inline' ); |
|
71 | - wp_send_json_success(); |
|
68 | + // Send success. |
|
69 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
70 | + @header( 'Content-Disposition: inline' ); |
|
71 | + wp_send_json_success(); |
|
72 | 72 | |
73 | - } |
|
73 | + } |
|
74 | 74 | |
75 | 75 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @param \Wordlift_Sample_Data_Service $sample_data_service The {@link Wordlift_Sample_Data_Service} instance. |
35 | 35 | */ |
36 | - public function __construct( $sample_data_service ) { |
|
36 | + public function __construct($sample_data_service) { |
|
37 | 37 | |
38 | 38 | $this->sample_data_service = $sample_data_service; |
39 | 39 | |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | |
68 | 68 | // Send success. |
69 | 69 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
70 | - @header( 'Content-Disposition: inline' ); |
|
70 | + @header('Content-Disposition: inline'); |
|
71 | 71 | wp_send_json_success(); |
72 | 72 | |
73 | 73 | } |
@@ -26,38 +26,38 @@ |
||
26 | 26 | */ |
27 | 27 | class Wordlift_I18n { |
28 | 28 | |
29 | - /** |
|
30 | - * The domain specified for this plugin. |
|
31 | - * |
|
32 | - * @since 1.0.0 |
|
33 | - * @access private |
|
34 | - * @var string $domain The domain identifier for this plugin. |
|
35 | - */ |
|
36 | - private $domain; |
|
37 | - |
|
38 | - /** |
|
39 | - * Load the plugin text domain for translation. |
|
40 | - * |
|
41 | - * @since 1.0.0 |
|
42 | - */ |
|
43 | - public function load_plugin_textdomain() { |
|
44 | - |
|
45 | - load_plugin_textdomain( |
|
46 | - $this->domain, |
|
47 | - false, |
|
48 | - dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' |
|
49 | - ); |
|
50 | - |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Set the domain equal to that of the specified domain. |
|
55 | - * |
|
56 | - * @since 1.0.0 |
|
57 | - * @param string $domain The domain that represents the locale of this plugin. |
|
58 | - */ |
|
59 | - public function set_domain( $domain ) { |
|
60 | - $this->domain = $domain; |
|
61 | - } |
|
29 | + /** |
|
30 | + * The domain specified for this plugin. |
|
31 | + * |
|
32 | + * @since 1.0.0 |
|
33 | + * @access private |
|
34 | + * @var string $domain The domain identifier for this plugin. |
|
35 | + */ |
|
36 | + private $domain; |
|
37 | + |
|
38 | + /** |
|
39 | + * Load the plugin text domain for translation. |
|
40 | + * |
|
41 | + * @since 1.0.0 |
|
42 | + */ |
|
43 | + public function load_plugin_textdomain() { |
|
44 | + |
|
45 | + load_plugin_textdomain( |
|
46 | + $this->domain, |
|
47 | + false, |
|
48 | + dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' |
|
49 | + ); |
|
50 | + |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Set the domain equal to that of the specified domain. |
|
55 | + * |
|
56 | + * @since 1.0.0 |
|
57 | + * @param string $domain The domain that represents the locale of this plugin. |
|
58 | + */ |
|
59 | + public function set_domain( $domain ) { |
|
60 | + $this->domain = $domain; |
|
61 | + } |
|
62 | 62 | |
63 | 63 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | load_plugin_textdomain( |
46 | 46 | $this->domain, |
47 | 47 | false, |
48 | - dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' |
|
48 | + dirname(dirname(plugin_basename(__FILE__))).'/languages/' |
|
49 | 49 | ); |
50 | 50 | |
51 | 51 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | * @since 1.0.0 |
57 | 57 | * @param string $domain The domain that represents the locale of this plugin. |
58 | 58 | */ |
59 | - public function set_domain( $domain ) { |
|
59 | + public function set_domain($domain) { |
|
60 | 60 | $this->domain = $domain; |
61 | 61 | } |
62 | 62 |
@@ -13,214 +13,214 @@ |
||
13 | 13 | */ |
14 | 14 | class Wordlift_Entity_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
15 | 15 | |
16 | - /** |
|
17 | - * The {@link Wordlift_Schemaorg_Property_Service} or null if not provided. |
|
18 | - * |
|
19 | - * @since 3.20.0 |
|
20 | - * @access private |
|
21 | - * @var null|Wordlift_Schemaorg_Property_Service The {@link Wordlift_Schemaorg_Property_Service} or null if not provided. |
|
22 | - */ |
|
23 | - private $schemaorg_property_service; |
|
24 | - |
|
25 | - /** |
|
26 | - * The {@link Wordlift_Post_To_Jsonld_Converter} is used to convert entities that also have the `article` term. |
|
27 | - * |
|
28 | - * @since 3.25.2 |
|
29 | - * @var \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance. |
|
30 | - */ |
|
31 | - private $post_to_jsonld_converter; |
|
32 | - |
|
33 | - /** |
|
34 | - * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
35 | - * |
|
36 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
37 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
38 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
39 | - * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
|
40 | - * @param \Wordlift_Schemaorg_Property_Service $schemaorg_property_service A {@link Wordlift_Schemaorg_Property_Service} instance. |
|
41 | - * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance. |
|
42 | - * |
|
43 | - * @since 3.8.0 |
|
44 | - */ |
|
45 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) { |
|
46 | - parent::__construct( $entity_type_service, $user_service, $attachment_service, $property_getter ); |
|
47 | - $this->schemaorg_property_service = $schemaorg_property_service; |
|
48 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
53 | - * found while processing the post is set in the $references array. |
|
54 | - * |
|
55 | - * @param int $post_id The {@link WP_Post} id. |
|
56 | - * @param array $references An array of entity references. |
|
57 | - * @param array $references_infos |
|
58 | - * |
|
59 | - * @return array A JSON-LD array. |
|
60 | - * @since 3.8.0 |
|
61 | - */ |
|
62 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
63 | - |
|
64 | - // Get the post instance. |
|
65 | - $post = get_post( $post_id ); |
|
66 | - if ( null === $post ) { |
|
67 | - // Post not found. |
|
68 | - return null; |
|
69 | - } |
|
70 | - |
|
71 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
72 | - if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) { |
|
73 | - $jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $references_infos ); |
|
74 | - } else { |
|
75 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
76 | - } |
|
77 | - |
|
78 | - // Get the entity name. |
|
79 | - $jsonld['name'] = html_entity_decode( $post->post_title ); |
|
80 | - |
|
81 | - // 3.13.0, add alternate names. |
|
82 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
83 | - if ( 0 < count( $alternative_labels ) ) { |
|
84 | - $jsonld['alternateName'] = $alternative_labels; |
|
85 | - } |
|
86 | - |
|
87 | - // Get the entity `@type` with custom fields set by the Wordlift_Schema_Service. |
|
88 | - // |
|
89 | - // This allows us to gather the basic properties as defined by the `Thing` entity type. |
|
90 | - // Get the configured type custom fields. |
|
91 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id ); |
|
92 | - |
|
93 | - if ( isset( $custom_fields ) ) { |
|
94 | - $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos ); |
|
95 | - } |
|
96 | - |
|
97 | - /* |
|
16 | + /** |
|
17 | + * The {@link Wordlift_Schemaorg_Property_Service} or null if not provided. |
|
18 | + * |
|
19 | + * @since 3.20.0 |
|
20 | + * @access private |
|
21 | + * @var null|Wordlift_Schemaorg_Property_Service The {@link Wordlift_Schemaorg_Property_Service} or null if not provided. |
|
22 | + */ |
|
23 | + private $schemaorg_property_service; |
|
24 | + |
|
25 | + /** |
|
26 | + * The {@link Wordlift_Post_To_Jsonld_Converter} is used to convert entities that also have the `article` term. |
|
27 | + * |
|
28 | + * @since 3.25.2 |
|
29 | + * @var \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance. |
|
30 | + */ |
|
31 | + private $post_to_jsonld_converter; |
|
32 | + |
|
33 | + /** |
|
34 | + * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
35 | + * |
|
36 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
37 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
38 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
39 | + * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
|
40 | + * @param \Wordlift_Schemaorg_Property_Service $schemaorg_property_service A {@link Wordlift_Schemaorg_Property_Service} instance. |
|
41 | + * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance. |
|
42 | + * |
|
43 | + * @since 3.8.0 |
|
44 | + */ |
|
45 | + public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) { |
|
46 | + parent::__construct( $entity_type_service, $user_service, $attachment_service, $property_getter ); |
|
47 | + $this->schemaorg_property_service = $schemaorg_property_service; |
|
48 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
53 | + * found while processing the post is set in the $references array. |
|
54 | + * |
|
55 | + * @param int $post_id The {@link WP_Post} id. |
|
56 | + * @param array $references An array of entity references. |
|
57 | + * @param array $references_infos |
|
58 | + * |
|
59 | + * @return array A JSON-LD array. |
|
60 | + * @since 3.8.0 |
|
61 | + */ |
|
62 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
63 | + |
|
64 | + // Get the post instance. |
|
65 | + $post = get_post( $post_id ); |
|
66 | + if ( null === $post ) { |
|
67 | + // Post not found. |
|
68 | + return null; |
|
69 | + } |
|
70 | + |
|
71 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
72 | + if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) { |
|
73 | + $jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $references_infos ); |
|
74 | + } else { |
|
75 | + $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
76 | + } |
|
77 | + |
|
78 | + // Get the entity name. |
|
79 | + $jsonld['name'] = html_entity_decode( $post->post_title ); |
|
80 | + |
|
81 | + // 3.13.0, add alternate names. |
|
82 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
83 | + if ( 0 < count( $alternative_labels ) ) { |
|
84 | + $jsonld['alternateName'] = $alternative_labels; |
|
85 | + } |
|
86 | + |
|
87 | + // Get the entity `@type` with custom fields set by the Wordlift_Schema_Service. |
|
88 | + // |
|
89 | + // This allows us to gather the basic properties as defined by the `Thing` entity type. |
|
90 | + // Get the configured type custom fields. |
|
91 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id ); |
|
92 | + |
|
93 | + if ( isset( $custom_fields ) ) { |
|
94 | + $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos ); |
|
95 | + } |
|
96 | + |
|
97 | + /* |
|
98 | 98 | * Get the properties attached to the post. |
99 | 99 | * |
100 | 100 | * @since 3.20.0 We attach properties directly to the posts. |
101 | 101 | * |
102 | 102 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
103 | 103 | */ |
104 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
105 | - if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
106 | - $this->process_post_properties( $jsonld, $post_id ); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
111 | - * |
|
112 | - * @param array $value { |
|
113 | - * |
|
114 | - * @type array $jsonld The JSON-LD structure. |
|
115 | - * @type int[] $references An array of post IDs. |
|
116 | - * } |
|
117 | - * @since 3.25.0 |
|
118 | - * |
|
119 | - * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
120 | - * |
|
121 | - * @api |
|
122 | - */ |
|
123 | - $ret_val = apply_filters( |
|
124 | - 'wl_entity_jsonld_array', |
|
125 | - array( |
|
126 | - 'jsonld' => $jsonld, |
|
127 | - 'references' => $references, |
|
128 | - ), |
|
129 | - $post_id |
|
130 | - ); |
|
131 | - $jsonld = $ret_val['jsonld']; |
|
132 | - $references = $ret_val['references']; |
|
133 | - |
|
134 | - /** |
|
135 | - * Call the `wl_entity_jsonld` filter. |
|
136 | - * |
|
137 | - * @param array $jsonld The JSON-LD structure. |
|
138 | - * @param int $post_id The {@link WP_Post} `id`. |
|
139 | - * @param array $references The array of referenced entities. |
|
140 | - * |
|
141 | - * @since 3.20.0 |
|
142 | - * |
|
143 | - * @api |
|
144 | - */ |
|
145 | - return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references ); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Process the properties attached to the {@link WP_Post}. |
|
150 | - * |
|
151 | - * @param array $jsonld The JSON-LD array. |
|
152 | - * @param int $post_id The target {@link WP_Post} id. |
|
153 | - * |
|
154 | - * @since 3.20.0 |
|
155 | - */ |
|
156 | - private function process_post_properties( &$jsonld, $post_id ) { |
|
157 | - |
|
158 | - // Get all the props. |
|
159 | - $props = $this->schemaorg_property_service->get_all( $post_id ); |
|
160 | - |
|
161 | - // Process all the props. |
|
162 | - foreach ( $props as $name => $instances ) { |
|
163 | - |
|
164 | - // Get the values. |
|
165 | - $values = array_map( |
|
166 | - function ( $instance ) { |
|
167 | - return $instance['value']; |
|
168 | - }, |
|
169 | - $instances |
|
170 | - ); |
|
171 | - |
|
172 | - // We might receive empty values, remove them. |
|
173 | - $non_empty_values = array_filter( |
|
174 | - $values, |
|
175 | - function ( $value ) { |
|
176 | - return ! empty( $value ); |
|
177 | - } |
|
178 | - ); |
|
179 | - |
|
180 | - // Skip empty properties. |
|
181 | - if ( empty( $non_empty_values ) ) { |
|
182 | - continue; |
|
183 | - } |
|
184 | - |
|
185 | - // @@todo: need to handle maybe Numbers and URLs differently. |
|
186 | - // Make an array a single value when possible. |
|
187 | - $jsonld[ $name ] = self::make_one( $non_empty_values ); |
|
188 | - } |
|
189 | - |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Post process the generated JSON to reorganize values which are stored as 1st |
|
194 | - * level in WP but are really 2nd level. |
|
195 | - * |
|
196 | - * @param array $jsonld An array of JSON-LD properties and values. |
|
197 | - * |
|
198 | - * @return array The array remapped. |
|
199 | - * @since 3.8.0 |
|
200 | - */ |
|
201 | - private function post_process( $jsonld ) { |
|
202 | - |
|
203 | - foreach ( $jsonld as $key => $value ) { |
|
204 | - if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
205 | - $jsonld['address']['@type'] = 'PostalAddress'; |
|
206 | - $jsonld['address'][ $key ] = $value; |
|
207 | - unset( $jsonld[ $key ] ); |
|
208 | - } |
|
209 | - |
|
210 | - if ( 'latitude' === $key || 'longitude' === $key ) { |
|
211 | - $jsonld['geo']['@type'] = 'GeoCoordinates'; |
|
212 | - $jsonld['geo'][ $key ] = $value; |
|
213 | - unset( $jsonld[ $key ] ); |
|
214 | - } |
|
215 | - |
|
216 | - if ( 'calories' === $key ) { |
|
217 | - $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
|
218 | - $jsonld['nutrition'][ $key ] = $value; |
|
219 | - unset( $jsonld[ $key ] ); |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - return $jsonld; |
|
224 | - } |
|
104 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
105 | + if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
106 | + $this->process_post_properties( $jsonld, $post_id ); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
111 | + * |
|
112 | + * @param array $value { |
|
113 | + * |
|
114 | + * @type array $jsonld The JSON-LD structure. |
|
115 | + * @type int[] $references An array of post IDs. |
|
116 | + * } |
|
117 | + * @since 3.25.0 |
|
118 | + * |
|
119 | + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
120 | + * |
|
121 | + * @api |
|
122 | + */ |
|
123 | + $ret_val = apply_filters( |
|
124 | + 'wl_entity_jsonld_array', |
|
125 | + array( |
|
126 | + 'jsonld' => $jsonld, |
|
127 | + 'references' => $references, |
|
128 | + ), |
|
129 | + $post_id |
|
130 | + ); |
|
131 | + $jsonld = $ret_val['jsonld']; |
|
132 | + $references = $ret_val['references']; |
|
133 | + |
|
134 | + /** |
|
135 | + * Call the `wl_entity_jsonld` filter. |
|
136 | + * |
|
137 | + * @param array $jsonld The JSON-LD structure. |
|
138 | + * @param int $post_id The {@link WP_Post} `id`. |
|
139 | + * @param array $references The array of referenced entities. |
|
140 | + * |
|
141 | + * @since 3.20.0 |
|
142 | + * |
|
143 | + * @api |
|
144 | + */ |
|
145 | + return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references ); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Process the properties attached to the {@link WP_Post}. |
|
150 | + * |
|
151 | + * @param array $jsonld The JSON-LD array. |
|
152 | + * @param int $post_id The target {@link WP_Post} id. |
|
153 | + * |
|
154 | + * @since 3.20.0 |
|
155 | + */ |
|
156 | + private function process_post_properties( &$jsonld, $post_id ) { |
|
157 | + |
|
158 | + // Get all the props. |
|
159 | + $props = $this->schemaorg_property_service->get_all( $post_id ); |
|
160 | + |
|
161 | + // Process all the props. |
|
162 | + foreach ( $props as $name => $instances ) { |
|
163 | + |
|
164 | + // Get the values. |
|
165 | + $values = array_map( |
|
166 | + function ( $instance ) { |
|
167 | + return $instance['value']; |
|
168 | + }, |
|
169 | + $instances |
|
170 | + ); |
|
171 | + |
|
172 | + // We might receive empty values, remove them. |
|
173 | + $non_empty_values = array_filter( |
|
174 | + $values, |
|
175 | + function ( $value ) { |
|
176 | + return ! empty( $value ); |
|
177 | + } |
|
178 | + ); |
|
179 | + |
|
180 | + // Skip empty properties. |
|
181 | + if ( empty( $non_empty_values ) ) { |
|
182 | + continue; |
|
183 | + } |
|
184 | + |
|
185 | + // @@todo: need to handle maybe Numbers and URLs differently. |
|
186 | + // Make an array a single value when possible. |
|
187 | + $jsonld[ $name ] = self::make_one( $non_empty_values ); |
|
188 | + } |
|
189 | + |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Post process the generated JSON to reorganize values which are stored as 1st |
|
194 | + * level in WP but are really 2nd level. |
|
195 | + * |
|
196 | + * @param array $jsonld An array of JSON-LD properties and values. |
|
197 | + * |
|
198 | + * @return array The array remapped. |
|
199 | + * @since 3.8.0 |
|
200 | + */ |
|
201 | + private function post_process( $jsonld ) { |
|
202 | + |
|
203 | + foreach ( $jsonld as $key => $value ) { |
|
204 | + if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
205 | + $jsonld['address']['@type'] = 'PostalAddress'; |
|
206 | + $jsonld['address'][ $key ] = $value; |
|
207 | + unset( $jsonld[ $key ] ); |
|
208 | + } |
|
209 | + |
|
210 | + if ( 'latitude' === $key || 'longitude' === $key ) { |
|
211 | + $jsonld['geo']['@type'] = 'GeoCoordinates'; |
|
212 | + $jsonld['geo'][ $key ] = $value; |
|
213 | + unset( $jsonld[ $key ] ); |
|
214 | + } |
|
215 | + |
|
216 | + if ( 'calories' === $key ) { |
|
217 | + $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
|
218 | + $jsonld['nutrition'][ $key ] = $value; |
|
219 | + unset( $jsonld[ $key ] ); |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + return $jsonld; |
|
224 | + } |
|
225 | 225 | |
226 | 226 | } |
@@ -42,8 +42,8 @@ discard block |
||
42 | 42 | * |
43 | 43 | * @since 3.8.0 |
44 | 44 | */ |
45 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) { |
|
46 | - parent::__construct( $entity_type_service, $user_service, $attachment_service, $property_getter ); |
|
45 | + public function __construct($entity_type_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter) { |
|
46 | + parent::__construct($entity_type_service, $user_service, $attachment_service, $property_getter); |
|
47 | 47 | $this->schemaorg_property_service = $schemaorg_property_service; |
48 | 48 | $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
49 | 49 | } |
@@ -59,28 +59,28 @@ discard block |
||
59 | 59 | * @return array A JSON-LD array. |
60 | 60 | * @since 3.8.0 |
61 | 61 | */ |
62 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
62 | + public function convert($post_id, &$references = array(), &$references_infos = array()) { |
|
63 | 63 | |
64 | 64 | // Get the post instance. |
65 | - $post = get_post( $post_id ); |
|
66 | - if ( null === $post ) { |
|
65 | + $post = get_post($post_id); |
|
66 | + if (null === $post) { |
|
67 | 67 | // Post not found. |
68 | 68 | return null; |
69 | 69 | } |
70 | 70 | |
71 | 71 | // Get the base JSON-LD and the list of entities referenced by this entity. |
72 | - if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) { |
|
73 | - $jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $references_infos ); |
|
72 | + if (has_term('article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id)) { |
|
73 | + $jsonld = $this->post_to_jsonld_converter->convert($post_id, $references, $references_infos); |
|
74 | 74 | } else { |
75 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
75 | + $jsonld = parent::convert($post_id, $references, $references_infos); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | // Get the entity name. |
79 | - $jsonld['name'] = html_entity_decode( $post->post_title ); |
|
79 | + $jsonld['name'] = html_entity_decode($post->post_title); |
|
80 | 80 | |
81 | 81 | // 3.13.0, add alternate names. |
82 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
83 | - if ( 0 < count( $alternative_labels ) ) { |
|
82 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
83 | + if (0 < count($alternative_labels)) { |
|
84 | 84 | $jsonld['alternateName'] = $alternative_labels; |
85 | 85 | } |
86 | 86 | |
@@ -88,10 +88,10 @@ discard block |
||
88 | 88 | // |
89 | 89 | // This allows us to gather the basic properties as defined by the `Thing` entity type. |
90 | 90 | // Get the configured type custom fields. |
91 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_post( $post_id ); |
|
91 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_post($post_id); |
|
92 | 92 | |
93 | - if ( isset( $custom_fields ) ) { |
|
94 | - $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references, $references_infos ); |
|
93 | + if (isset($custom_fields)) { |
|
94 | + $this->process_type_custom_fields($jsonld, $custom_fields, $post, $references, $references_infos); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /* |
@@ -102,8 +102,8 @@ discard block |
||
102 | 102 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
103 | 103 | */ |
104 | 104 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
105 | - if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
106 | - $this->process_post_properties( $jsonld, $post_id ); |
|
105 | + if (apply_filters('wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES)) { |
|
106 | + $this->process_post_properties($jsonld, $post_id); |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | /** |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | * |
121 | 121 | * @api |
122 | 122 | */ |
123 | - $ret_val = apply_filters( |
|
123 | + $ret_val = apply_filters( |
|
124 | 124 | 'wl_entity_jsonld_array', |
125 | 125 | array( |
126 | 126 | 'jsonld' => $jsonld, |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | * |
143 | 143 | * @api |
144 | 144 | */ |
145 | - return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references ); |
|
145 | + return apply_filters('wl_entity_jsonld', $this->post_process($jsonld), $post_id, $references); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
@@ -153,17 +153,17 @@ discard block |
||
153 | 153 | * |
154 | 154 | * @since 3.20.0 |
155 | 155 | */ |
156 | - private function process_post_properties( &$jsonld, $post_id ) { |
|
156 | + private function process_post_properties(&$jsonld, $post_id) { |
|
157 | 157 | |
158 | 158 | // Get all the props. |
159 | - $props = $this->schemaorg_property_service->get_all( $post_id ); |
|
159 | + $props = $this->schemaorg_property_service->get_all($post_id); |
|
160 | 160 | |
161 | 161 | // Process all the props. |
162 | - foreach ( $props as $name => $instances ) { |
|
162 | + foreach ($props as $name => $instances) { |
|
163 | 163 | |
164 | 164 | // Get the values. |
165 | 165 | $values = array_map( |
166 | - function ( $instance ) { |
|
166 | + function($instance) { |
|
167 | 167 | return $instance['value']; |
168 | 168 | }, |
169 | 169 | $instances |
@@ -172,19 +172,19 @@ discard block |
||
172 | 172 | // We might receive empty values, remove them. |
173 | 173 | $non_empty_values = array_filter( |
174 | 174 | $values, |
175 | - function ( $value ) { |
|
176 | - return ! empty( $value ); |
|
175 | + function($value) { |
|
176 | + return ! empty($value); |
|
177 | 177 | } |
178 | 178 | ); |
179 | 179 | |
180 | 180 | // Skip empty properties. |
181 | - if ( empty( $non_empty_values ) ) { |
|
181 | + if (empty($non_empty_values)) { |
|
182 | 182 | continue; |
183 | 183 | } |
184 | 184 | |
185 | 185 | // @@todo: need to handle maybe Numbers and URLs differently. |
186 | 186 | // Make an array a single value when possible. |
187 | - $jsonld[ $name ] = self::make_one( $non_empty_values ); |
|
187 | + $jsonld[$name] = self::make_one($non_empty_values); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | } |
@@ -198,25 +198,25 @@ discard block |
||
198 | 198 | * @return array The array remapped. |
199 | 199 | * @since 3.8.0 |
200 | 200 | */ |
201 | - private function post_process( $jsonld ) { |
|
201 | + private function post_process($jsonld) { |
|
202 | 202 | |
203 | - foreach ( $jsonld as $key => $value ) { |
|
204 | - if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
203 | + foreach ($jsonld as $key => $value) { |
|
204 | + if ('streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key) { |
|
205 | 205 | $jsonld['address']['@type'] = 'PostalAddress'; |
206 | - $jsonld['address'][ $key ] = $value; |
|
207 | - unset( $jsonld[ $key ] ); |
|
206 | + $jsonld['address'][$key] = $value; |
|
207 | + unset($jsonld[$key]); |
|
208 | 208 | } |
209 | 209 | |
210 | - if ( 'latitude' === $key || 'longitude' === $key ) { |
|
210 | + if ('latitude' === $key || 'longitude' === $key) { |
|
211 | 211 | $jsonld['geo']['@type'] = 'GeoCoordinates'; |
212 | - $jsonld['geo'][ $key ] = $value; |
|
213 | - unset( $jsonld[ $key ] ); |
|
212 | + $jsonld['geo'][$key] = $value; |
|
213 | + unset($jsonld[$key]); |
|
214 | 214 | } |
215 | 215 | |
216 | - if ( 'calories' === $key ) { |
|
216 | + if ('calories' === $key) { |
|
217 | 217 | $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
218 | - $jsonld['nutrition'][ $key ] = $value; |
|
219 | - unset( $jsonld[ $key ] ); |
|
218 | + $jsonld['nutrition'][$key] = $value; |
|
219 | + unset($jsonld[$key]); |
|
220 | 220 | } |
221 | 221 | } |
222 | 222 |
@@ -13,178 +13,178 @@ |
||
13 | 13 | */ |
14 | 14 | class Wordlift_Schema_Url_Property_Service extends Wordlift_Property_Service { |
15 | 15 | |
16 | - /** |
|
17 | - * The meta key used to store data for this property. We don't use wl_url to |
|
18 | - * avoid potential confusion about other URLs. |
|
19 | - * |
|
20 | - * @since 3.6.0 |
|
21 | - */ |
|
22 | - const META_KEY = 'wl_schema_url'; |
|
23 | - |
|
24 | - /** |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - public function get_rdf_predicate() { |
|
28 | - |
|
29 | - return 'http://schema.org/url'; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * {@inheritdoc} |
|
34 | - */ |
|
35 | - public function get_rdf_data_type() { |
|
36 | - |
|
37 | - return 'xsd:anyURI'; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - public function get_data_type() { |
|
44 | - |
|
45 | - return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * {@inheritdoc} |
|
50 | - */ |
|
51 | - public function get_cardinality() { |
|
52 | - |
|
53 | - return INF; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - public function get_metabox_class() { |
|
60 | - |
|
61 | - return 'Wl_Metabox_Field'; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - public function get_metabox_label() { |
|
68 | - |
|
69 | - return __( 'Web Site(s)', 'wordlift' ); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Create a Wordlift_Schema_Url_Property_Service instance. |
|
74 | - * |
|
75 | - * @since 3.6.0 |
|
76 | - */ |
|
77 | - public function __construct() { |
|
78 | - parent::__construct(); |
|
79 | - |
|
80 | - // Finally listen for metadata requests for this field. |
|
81 | - $this->add_filter_get_post_metadata(); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Get the schema:url value for the specified post/entity. |
|
86 | - * |
|
87 | - * @param int $post_id The post id. |
|
88 | - * |
|
89 | - * @return array|NULL The schema:url value or NULL if not set. |
|
90 | - * @since 3.6.0 |
|
91 | - */ |
|
92 | - public function get( $post_id ) { |
|
93 | - |
|
94 | - // Get the schema:url values set in WP. |
|
95 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
96 | - |
|
97 | - // If the property has never been set, we set its default value the first |
|
98 | - // time to <permalink>. |
|
99 | - if ( empty( $values ) ) { |
|
100 | - return array( '<permalink>' ); |
|
101 | - } |
|
102 | - |
|
103 | - // If there's only one value and that value is empty, we return NULL, i.e. |
|
104 | - // variable not set. |
|
105 | - if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
106 | - return null; |
|
107 | - } |
|
108 | - |
|
109 | - // Finally return whatever values the editor set. |
|
110 | - return $values; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * {@inheritdoc} |
|
115 | - */ |
|
116 | - public function sanitize( $value ) { |
|
117 | - |
|
118 | - // TODO: check that it's an URL or that is <permalink> |
|
119 | - |
|
120 | - return $value; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Get direct calls to read this meta and alter the response according to our |
|
125 | - * own strategy, i.e. if a value has never been set for this meta, then return |
|
126 | - * <permalink>. |
|
127 | - * |
|
128 | - * @param mixed $value The original value. |
|
129 | - * @param int $object_id The post id. |
|
130 | - * @param string $meta_key The meta key. We expect wl_schema_url or we return straight the value. |
|
131 | - * @param bool $single Whether to return a single value. |
|
132 | - * |
|
133 | - * @return array|mixed|NULL|string |
|
134 | - * @since 3.6.0 |
|
135 | - */ |
|
136 | - public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
137 | - |
|
138 | - // It's not us, return the value. |
|
139 | - if ( self::META_KEY !== $meta_key ) { |
|
140 | - return $value; |
|
141 | - } |
|
142 | - |
|
143 | - $this->remove_filter_get_post_metadata(); |
|
144 | - |
|
145 | - $new_value = $this->get( $object_id ); |
|
146 | - |
|
147 | - $this->add_filter_get_post_metadata(); |
|
148 | - |
|
149 | - // If we must return a single value, but we don't have a value, then return an empty string. |
|
150 | - if ( $single && ( $new_value === null || empty( $new_value ) ) ) { |
|
151 | - return ''; |
|
152 | - } |
|
153 | - |
|
154 | - // If we have a value and we need to return it as single, return the first value. |
|
155 | - if ( $single ) { |
|
156 | - return $new_value[0]; |
|
157 | - } |
|
158 | - |
|
159 | - // Otherwise return the array. |
|
160 | - return $new_value; |
|
161 | - } |
|
162 | - |
|
163 | - private function add_filter_get_post_metadata() { |
|
164 | - |
|
165 | - add_filter( |
|
166 | - 'get_post_metadata', |
|
167 | - array( |
|
168 | - $this, |
|
169 | - 'get_post_metadata', |
|
170 | - ), |
|
171 | - 10, |
|
172 | - 4 |
|
173 | - ); |
|
174 | - |
|
175 | - } |
|
176 | - |
|
177 | - private function remove_filter_get_post_metadata() { |
|
178 | - |
|
179 | - remove_filter( |
|
180 | - 'get_post_metadata', |
|
181 | - array( |
|
182 | - $this, |
|
183 | - 'get_post_metadata', |
|
184 | - ), |
|
185 | - 10 |
|
186 | - ); |
|
187 | - |
|
188 | - } |
|
16 | + /** |
|
17 | + * The meta key used to store data for this property. We don't use wl_url to |
|
18 | + * avoid potential confusion about other URLs. |
|
19 | + * |
|
20 | + * @since 3.6.0 |
|
21 | + */ |
|
22 | + const META_KEY = 'wl_schema_url'; |
|
23 | + |
|
24 | + /** |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + public function get_rdf_predicate() { |
|
28 | + |
|
29 | + return 'http://schema.org/url'; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * {@inheritdoc} |
|
34 | + */ |
|
35 | + public function get_rdf_data_type() { |
|
36 | + |
|
37 | + return 'xsd:anyURI'; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + public function get_data_type() { |
|
44 | + |
|
45 | + return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * {@inheritdoc} |
|
50 | + */ |
|
51 | + public function get_cardinality() { |
|
52 | + |
|
53 | + return INF; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + public function get_metabox_class() { |
|
60 | + |
|
61 | + return 'Wl_Metabox_Field'; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + public function get_metabox_label() { |
|
68 | + |
|
69 | + return __( 'Web Site(s)', 'wordlift' ); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Create a Wordlift_Schema_Url_Property_Service instance. |
|
74 | + * |
|
75 | + * @since 3.6.0 |
|
76 | + */ |
|
77 | + public function __construct() { |
|
78 | + parent::__construct(); |
|
79 | + |
|
80 | + // Finally listen for metadata requests for this field. |
|
81 | + $this->add_filter_get_post_metadata(); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Get the schema:url value for the specified post/entity. |
|
86 | + * |
|
87 | + * @param int $post_id The post id. |
|
88 | + * |
|
89 | + * @return array|NULL The schema:url value or NULL if not set. |
|
90 | + * @since 3.6.0 |
|
91 | + */ |
|
92 | + public function get( $post_id ) { |
|
93 | + |
|
94 | + // Get the schema:url values set in WP. |
|
95 | + $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
96 | + |
|
97 | + // If the property has never been set, we set its default value the first |
|
98 | + // time to <permalink>. |
|
99 | + if ( empty( $values ) ) { |
|
100 | + return array( '<permalink>' ); |
|
101 | + } |
|
102 | + |
|
103 | + // If there's only one value and that value is empty, we return NULL, i.e. |
|
104 | + // variable not set. |
|
105 | + if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
106 | + return null; |
|
107 | + } |
|
108 | + |
|
109 | + // Finally return whatever values the editor set. |
|
110 | + return $values; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * {@inheritdoc} |
|
115 | + */ |
|
116 | + public function sanitize( $value ) { |
|
117 | + |
|
118 | + // TODO: check that it's an URL or that is <permalink> |
|
119 | + |
|
120 | + return $value; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Get direct calls to read this meta and alter the response according to our |
|
125 | + * own strategy, i.e. if a value has never been set for this meta, then return |
|
126 | + * <permalink>. |
|
127 | + * |
|
128 | + * @param mixed $value The original value. |
|
129 | + * @param int $object_id The post id. |
|
130 | + * @param string $meta_key The meta key. We expect wl_schema_url or we return straight the value. |
|
131 | + * @param bool $single Whether to return a single value. |
|
132 | + * |
|
133 | + * @return array|mixed|NULL|string |
|
134 | + * @since 3.6.0 |
|
135 | + */ |
|
136 | + public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
137 | + |
|
138 | + // It's not us, return the value. |
|
139 | + if ( self::META_KEY !== $meta_key ) { |
|
140 | + return $value; |
|
141 | + } |
|
142 | + |
|
143 | + $this->remove_filter_get_post_metadata(); |
|
144 | + |
|
145 | + $new_value = $this->get( $object_id ); |
|
146 | + |
|
147 | + $this->add_filter_get_post_metadata(); |
|
148 | + |
|
149 | + // If we must return a single value, but we don't have a value, then return an empty string. |
|
150 | + if ( $single && ( $new_value === null || empty( $new_value ) ) ) { |
|
151 | + return ''; |
|
152 | + } |
|
153 | + |
|
154 | + // If we have a value and we need to return it as single, return the first value. |
|
155 | + if ( $single ) { |
|
156 | + return $new_value[0]; |
|
157 | + } |
|
158 | + |
|
159 | + // Otherwise return the array. |
|
160 | + return $new_value; |
|
161 | + } |
|
162 | + |
|
163 | + private function add_filter_get_post_metadata() { |
|
164 | + |
|
165 | + add_filter( |
|
166 | + 'get_post_metadata', |
|
167 | + array( |
|
168 | + $this, |
|
169 | + 'get_post_metadata', |
|
170 | + ), |
|
171 | + 10, |
|
172 | + 4 |
|
173 | + ); |
|
174 | + |
|
175 | + } |
|
176 | + |
|
177 | + private function remove_filter_get_post_metadata() { |
|
178 | + |
|
179 | + remove_filter( |
|
180 | + 'get_post_metadata', |
|
181 | + array( |
|
182 | + $this, |
|
183 | + 'get_post_metadata', |
|
184 | + ), |
|
185 | + 10 |
|
186 | + ); |
|
187 | + |
|
188 | + } |
|
189 | 189 | |
190 | 190 | } |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | */ |
67 | 67 | public function get_metabox_label() { |
68 | 68 | |
69 | - return __( 'Web Site(s)', 'wordlift' ); |
|
69 | + return __('Web Site(s)', 'wordlift'); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | /** |
@@ -89,20 +89,20 @@ discard block |
||
89 | 89 | * @return array|NULL The schema:url value or NULL if not set. |
90 | 90 | * @since 3.6.0 |
91 | 91 | */ |
92 | - public function get( $post_id ) { |
|
92 | + public function get($post_id) { |
|
93 | 93 | |
94 | 94 | // Get the schema:url values set in WP. |
95 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
95 | + $values = get_post_meta($post_id, self::META_KEY, false); |
|
96 | 96 | |
97 | 97 | // If the property has never been set, we set its default value the first |
98 | 98 | // time to <permalink>. |
99 | - if ( empty( $values ) ) { |
|
100 | - return array( '<permalink>' ); |
|
99 | + if (empty($values)) { |
|
100 | + return array('<permalink>'); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | // If there's only one value and that value is empty, we return NULL, i.e. |
104 | 104 | // variable not set. |
105 | - if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
105 | + if (1 === count($values) && empty($values[0])) { |
|
106 | 106 | return null; |
107 | 107 | } |
108 | 108 | |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | /** |
114 | 114 | * {@inheritdoc} |
115 | 115 | */ |
116 | - public function sanitize( $value ) { |
|
116 | + public function sanitize($value) { |
|
117 | 117 | |
118 | 118 | // TODO: check that it's an URL or that is <permalink> |
119 | 119 | |
@@ -133,26 +133,26 @@ discard block |
||
133 | 133 | * @return array|mixed|NULL|string |
134 | 134 | * @since 3.6.0 |
135 | 135 | */ |
136 | - public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
136 | + public function get_post_metadata($value, $object_id, $meta_key, $single) { |
|
137 | 137 | |
138 | 138 | // It's not us, return the value. |
139 | - if ( self::META_KEY !== $meta_key ) { |
|
139 | + if (self::META_KEY !== $meta_key) { |
|
140 | 140 | return $value; |
141 | 141 | } |
142 | 142 | |
143 | 143 | $this->remove_filter_get_post_metadata(); |
144 | 144 | |
145 | - $new_value = $this->get( $object_id ); |
|
145 | + $new_value = $this->get($object_id); |
|
146 | 146 | |
147 | 147 | $this->add_filter_get_post_metadata(); |
148 | 148 | |
149 | 149 | // If we must return a single value, but we don't have a value, then return an empty string. |
150 | - if ( $single && ( $new_value === null || empty( $new_value ) ) ) { |
|
150 | + if ($single && ($new_value === null || empty($new_value))) { |
|
151 | 151 | return ''; |
152 | 152 | } |
153 | 153 | |
154 | 154 | // If we have a value and we need to return it as single, return the first value. |
155 | - if ( $single ) { |
|
155 | + if ($single) { |
|
156 | 156 | return $new_value[0]; |
157 | 157 | } |
158 | 158 |
@@ -16,56 +16,56 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Postid_To_Jsonld_Converter implements Wordlift_Post_Converter { |
18 | 18 | |
19 | - private $entity_post_to_jsonld_converter; |
|
20 | - private $post_to_jsonld_converter; |
|
19 | + private $entity_post_to_jsonld_converter; |
|
20 | + private $post_to_jsonld_converter; |
|
21 | 21 | |
22 | - /** |
|
23 | - * A {@link Wordlift_Log_Service} instance. |
|
24 | - * |
|
25 | - * @since 3.16.0 |
|
26 | - * @access private |
|
27 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
28 | - */ |
|
29 | - private $log; |
|
22 | + /** |
|
23 | + * A {@link Wordlift_Log_Service} instance. |
|
24 | + * |
|
25 | + * @since 3.16.0 |
|
26 | + * @access private |
|
27 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
28 | + */ |
|
29 | + private $log; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
33 | - * |
|
34 | - * @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_post_to_jsonld_converter |
|
35 | - * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
36 | - * |
|
37 | - * @since 3.8.0 |
|
38 | - */ |
|
39 | - public function __construct( $entity_post_to_jsonld_converter, $post_to_jsonld_converter ) { |
|
31 | + /** |
|
32 | + * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
33 | + * |
|
34 | + * @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_post_to_jsonld_converter |
|
35 | + * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
36 | + * |
|
37 | + * @since 3.8.0 |
|
38 | + */ |
|
39 | + public function __construct( $entity_post_to_jsonld_converter, $post_to_jsonld_converter ) { |
|
40 | 40 | |
41 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
41 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
42 | 42 | |
43 | - $this->entity_post_to_jsonld_converter = $entity_post_to_jsonld_converter; |
|
44 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
43 | + $this->entity_post_to_jsonld_converter = $entity_post_to_jsonld_converter; |
|
44 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
45 | 45 | |
46 | - } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Convert a post to JSON-LD. |
|
50 | - * |
|
51 | - * @param string $id A post id (post or entity). |
|
52 | - * @param array $references |
|
53 | - * @param array $references_infos |
|
54 | - * |
|
55 | - * @return array|NULL A JSON-LD array representation of the post with the provided id, or NULL if not found. |
|
56 | - * @since 3.8.0 |
|
57 | - */ |
|
58 | - public function convert( $id, &$references = array(), &$references_infos = array() ) { |
|
48 | + /** |
|
49 | + * Convert a post to JSON-LD. |
|
50 | + * |
|
51 | + * @param string $id A post id (post or entity). |
|
52 | + * @param array $references |
|
53 | + * @param array $references_infos |
|
54 | + * |
|
55 | + * @return array|NULL A JSON-LD array representation of the post with the provided id, or NULL if not found. |
|
56 | + * @since 3.8.0 |
|
57 | + */ |
|
58 | + public function convert( $id, &$references = array(), &$references_infos = array() ) { |
|
59 | 59 | |
60 | - $this->log->trace( "Converting post $id..." ); |
|
60 | + $this->log->trace( "Converting post $id..." ); |
|
61 | 61 | |
62 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
62 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
63 | 63 | |
64 | - return $entity_service->is_entity( $id ) |
|
65 | - // Entity. |
|
66 | - ? $this->entity_post_to_jsonld_converter->convert( $id, $references, $references_infos ) |
|
67 | - // Post/Page. |
|
68 | - : $this->post_to_jsonld_converter->convert( $id, $references ); |
|
69 | - } |
|
64 | + return $entity_service->is_entity( $id ) |
|
65 | + // Entity. |
|
66 | + ? $this->entity_post_to_jsonld_converter->convert( $id, $references, $references_infos ) |
|
67 | + // Post/Page. |
|
68 | + : $this->post_to_jsonld_converter->convert( $id, $references ); |
|
69 | + } |
|
70 | 70 | |
71 | 71 | } |
@@ -36,9 +36,9 @@ discard block |
||
36 | 36 | * |
37 | 37 | * @since 3.8.0 |
38 | 38 | */ |
39 | - public function __construct( $entity_post_to_jsonld_converter, $post_to_jsonld_converter ) { |
|
39 | + public function __construct($entity_post_to_jsonld_converter, $post_to_jsonld_converter) { |
|
40 | 40 | |
41 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
41 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
42 | 42 | |
43 | 43 | $this->entity_post_to_jsonld_converter = $entity_post_to_jsonld_converter; |
44 | 44 | $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
@@ -55,17 +55,17 @@ discard block |
||
55 | 55 | * @return array|NULL A JSON-LD array representation of the post with the provided id, or NULL if not found. |
56 | 56 | * @since 3.8.0 |
57 | 57 | */ |
58 | - public function convert( $id, &$references = array(), &$references_infos = array() ) { |
|
58 | + public function convert($id, &$references = array(), &$references_infos = array()) { |
|
59 | 59 | |
60 | - $this->log->trace( "Converting post $id..." ); |
|
60 | + $this->log->trace("Converting post $id..."); |
|
61 | 61 | |
62 | 62 | $entity_service = Wordlift_Entity_Service::get_instance(); |
63 | 63 | |
64 | - return $entity_service->is_entity( $id ) |
|
64 | + return $entity_service->is_entity($id) |
|
65 | 65 | // Entity. |
66 | - ? $this->entity_post_to_jsonld_converter->convert( $id, $references, $references_infos ) |
|
66 | + ? $this->entity_post_to_jsonld_converter->convert($id, $references, $references_infos) |
|
67 | 67 | // Post/Page. |
68 | - : $this->post_to_jsonld_converter->convert( $id, $references ); |
|
68 | + : $this->post_to_jsonld_converter->convert($id, $references); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | } |
@@ -16,176 +16,176 @@ |
||
16 | 16 | */ |
17 | 17 | class Wordlift_Entity_Post_Type_Service { |
18 | 18 | |
19 | - /** |
|
20 | - * The entity post type. |
|
21 | - * |
|
22 | - * @since 3.6.0 |
|
23 | - * @access private |
|
24 | - * @var string $post_type The entity post type. |
|
25 | - */ |
|
26 | - private $post_type; |
|
27 | - |
|
28 | - /** |
|
29 | - * The entity type slug. |
|
30 | - * |
|
31 | - * @since 3.6.0 |
|
32 | - * @access private |
|
33 | - * @var string $slug The entity type slug. |
|
34 | - */ |
|
35 | - private $slug; |
|
36 | - |
|
37 | - /** |
|
38 | - * A {@link Wordlift_Log_Service} instance. |
|
39 | - * |
|
40 | - * @since 3.16.3 |
|
41 | - * @access private |
|
42 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
43 | - */ |
|
44 | - private $log; |
|
45 | - |
|
46 | - /** |
|
47 | - * A singleton instance of the entity type service. |
|
48 | - * |
|
49 | - * @since 3.6.0 |
|
50 | - * @access private |
|
51 | - * @var Wordlift_Entity_Post_Type_Service |
|
52 | - */ |
|
53 | - private static $instance; |
|
54 | - |
|
55 | - /** |
|
56 | - * Create an entity type service instance. |
|
57 | - * |
|
58 | - * @param string $post_type The post type, e.g. entity. |
|
59 | - * @param string $slug The entity type slug, if the slug is empty, the default slug will be used. |
|
60 | - * |
|
61 | - * @since 3.6.0 |
|
62 | - */ |
|
63 | - public function __construct( $post_type, $slug ) { |
|
64 | - |
|
65 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
66 | - |
|
67 | - $this->post_type = $post_type; |
|
68 | - |
|
69 | - // We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default |
|
70 | - // to the type name. |
|
71 | - $this->slug = $slug ? $slug : $post_type; |
|
72 | - |
|
73 | - self::$instance = $this; |
|
74 | - |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Get the entity type service singleton instance. |
|
79 | - * |
|
80 | - * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance. |
|
81 | - * @since 3.6.0 |
|
82 | - */ |
|
83 | - public static function get_instance() { |
|
84 | - |
|
85 | - return self::$instance; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Get the entity type slug. |
|
90 | - * |
|
91 | - * @return string The entity type slug. |
|
92 | - * @since 3.6.0 |
|
93 | - */ |
|
94 | - public function get_slug() { |
|
95 | - |
|
96 | - return $this->slug; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Get the entity post type name. |
|
101 | - * |
|
102 | - * @return string The entity post type. |
|
103 | - * @since 3.6.0 |
|
104 | - */ |
|
105 | - public function get_post_type() { |
|
106 | - |
|
107 | - return $this->post_type; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Register the WordLift entity post type. This method is hooked to WordPress' init action. |
|
112 | - * |
|
113 | - * @since 3.6.0 |
|
114 | - */ |
|
115 | - public function register() { |
|
116 | - |
|
117 | - $this->log->debug( "Registering post type $this->post_type..." ); |
|
118 | - |
|
119 | - $labels = array( |
|
120 | - 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
121 | - 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
122 | - 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
123 | - 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
124 | - 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
125 | - 'new_item' => __( 'New Entity', 'wordlift' ), |
|
126 | - 'all_items' => __( 'All Entities', 'wordlift' ), |
|
127 | - 'view_item' => __( 'View Entity', 'wordlift' ), |
|
128 | - 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
129 | - 'not_found' => __( 'No entities found', 'wordlift' ), |
|
130 | - 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
131 | - 'parent_item_colon' => '', |
|
132 | - 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
133 | - ); |
|
134 | - |
|
135 | - $args = array( |
|
136 | - 'labels' => $labels, |
|
137 | - 'description' => 'Holds our vocabulary (set of entities) and entity specific data', |
|
138 | - 'public' => true, |
|
139 | - 'menu_position' => 20, |
|
140 | - // after the pages menu. |
|
141 | - // Add support for 'authors' and 'revisions': |
|
142 | - // * see https://github.com/insideout10/wordlift-plugin/issues/395 |
|
143 | - // * see https://github.com/insideout10/wordlift-plugin/issues/376 |
|
144 | - 'supports' => array( |
|
145 | - 'title', |
|
146 | - 'editor', |
|
147 | - 'thumbnail', |
|
148 | - 'excerpt', |
|
149 | - 'comments', |
|
150 | - 'author', |
|
151 | - 'revisions', |
|
152 | - 'custom-fields', |
|
153 | - ), |
|
154 | - 'has_archive' => true, |
|
155 | - 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
156 | - // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
|
157 | - 'rewrite' => array( 'slug' => $this->slug ), |
|
158 | - 'capability_type' => array( |
|
159 | - 'wordlift_entity', |
|
160 | - 'wordlift_entities', |
|
161 | - ), |
|
162 | - 'capabilities' => array( |
|
163 | - 'delete_posts' => 'delete_wordlift_entities', |
|
164 | - ), |
|
165 | - 'rest_base' => 'entities', |
|
166 | - 'show_in_rest' => true, |
|
167 | - /** |
|
168 | - * Filter: wl_feature__enable__vocabulary. |
|
169 | - * |
|
170 | - * @param bool whether the vocabulary needs to be shown in menu. |
|
171 | - * |
|
172 | - * @return bool |
|
173 | - * @since 3.27.6 |
|
174 | - */ |
|
175 | - 'show_in_menu' => apply_filters( 'wl_feature__enable__vocabulary', true ), |
|
176 | - ); |
|
177 | - |
|
178 | - register_post_type( $this->post_type, $args ); |
|
179 | - |
|
180 | - // Enable WP's standard `category` taxonomy for entities. |
|
181 | - // |
|
182 | - // While this enables editors to bind entities to the WP posts' category |
|
183 | - // taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter |
|
184 | - // WP's main category query to include the `entity` post type. |
|
185 | - // |
|
186 | - // See https://github.com/insideout10/wordlift-plugin/issues/442 |
|
187 | - register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
188 | - |
|
189 | - } |
|
19 | + /** |
|
20 | + * The entity post type. |
|
21 | + * |
|
22 | + * @since 3.6.0 |
|
23 | + * @access private |
|
24 | + * @var string $post_type The entity post type. |
|
25 | + */ |
|
26 | + private $post_type; |
|
27 | + |
|
28 | + /** |
|
29 | + * The entity type slug. |
|
30 | + * |
|
31 | + * @since 3.6.0 |
|
32 | + * @access private |
|
33 | + * @var string $slug The entity type slug. |
|
34 | + */ |
|
35 | + private $slug; |
|
36 | + |
|
37 | + /** |
|
38 | + * A {@link Wordlift_Log_Service} instance. |
|
39 | + * |
|
40 | + * @since 3.16.3 |
|
41 | + * @access private |
|
42 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
43 | + */ |
|
44 | + private $log; |
|
45 | + |
|
46 | + /** |
|
47 | + * A singleton instance of the entity type service. |
|
48 | + * |
|
49 | + * @since 3.6.0 |
|
50 | + * @access private |
|
51 | + * @var Wordlift_Entity_Post_Type_Service |
|
52 | + */ |
|
53 | + private static $instance; |
|
54 | + |
|
55 | + /** |
|
56 | + * Create an entity type service instance. |
|
57 | + * |
|
58 | + * @param string $post_type The post type, e.g. entity. |
|
59 | + * @param string $slug The entity type slug, if the slug is empty, the default slug will be used. |
|
60 | + * |
|
61 | + * @since 3.6.0 |
|
62 | + */ |
|
63 | + public function __construct( $post_type, $slug ) { |
|
64 | + |
|
65 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
66 | + |
|
67 | + $this->post_type = $post_type; |
|
68 | + |
|
69 | + // We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default |
|
70 | + // to the type name. |
|
71 | + $this->slug = $slug ? $slug : $post_type; |
|
72 | + |
|
73 | + self::$instance = $this; |
|
74 | + |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Get the entity type service singleton instance. |
|
79 | + * |
|
80 | + * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance. |
|
81 | + * @since 3.6.0 |
|
82 | + */ |
|
83 | + public static function get_instance() { |
|
84 | + |
|
85 | + return self::$instance; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Get the entity type slug. |
|
90 | + * |
|
91 | + * @return string The entity type slug. |
|
92 | + * @since 3.6.0 |
|
93 | + */ |
|
94 | + public function get_slug() { |
|
95 | + |
|
96 | + return $this->slug; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Get the entity post type name. |
|
101 | + * |
|
102 | + * @return string The entity post type. |
|
103 | + * @since 3.6.0 |
|
104 | + */ |
|
105 | + public function get_post_type() { |
|
106 | + |
|
107 | + return $this->post_type; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Register the WordLift entity post type. This method is hooked to WordPress' init action. |
|
112 | + * |
|
113 | + * @since 3.6.0 |
|
114 | + */ |
|
115 | + public function register() { |
|
116 | + |
|
117 | + $this->log->debug( "Registering post type $this->post_type..." ); |
|
118 | + |
|
119 | + $labels = array( |
|
120 | + 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
121 | + 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
122 | + 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
123 | + 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
124 | + 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
125 | + 'new_item' => __( 'New Entity', 'wordlift' ), |
|
126 | + 'all_items' => __( 'All Entities', 'wordlift' ), |
|
127 | + 'view_item' => __( 'View Entity', 'wordlift' ), |
|
128 | + 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
129 | + 'not_found' => __( 'No entities found', 'wordlift' ), |
|
130 | + 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
131 | + 'parent_item_colon' => '', |
|
132 | + 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
133 | + ); |
|
134 | + |
|
135 | + $args = array( |
|
136 | + 'labels' => $labels, |
|
137 | + 'description' => 'Holds our vocabulary (set of entities) and entity specific data', |
|
138 | + 'public' => true, |
|
139 | + 'menu_position' => 20, |
|
140 | + // after the pages menu. |
|
141 | + // Add support for 'authors' and 'revisions': |
|
142 | + // * see https://github.com/insideout10/wordlift-plugin/issues/395 |
|
143 | + // * see https://github.com/insideout10/wordlift-plugin/issues/376 |
|
144 | + 'supports' => array( |
|
145 | + 'title', |
|
146 | + 'editor', |
|
147 | + 'thumbnail', |
|
148 | + 'excerpt', |
|
149 | + 'comments', |
|
150 | + 'author', |
|
151 | + 'revisions', |
|
152 | + 'custom-fields', |
|
153 | + ), |
|
154 | + 'has_archive' => true, |
|
155 | + 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
156 | + // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
|
157 | + 'rewrite' => array( 'slug' => $this->slug ), |
|
158 | + 'capability_type' => array( |
|
159 | + 'wordlift_entity', |
|
160 | + 'wordlift_entities', |
|
161 | + ), |
|
162 | + 'capabilities' => array( |
|
163 | + 'delete_posts' => 'delete_wordlift_entities', |
|
164 | + ), |
|
165 | + 'rest_base' => 'entities', |
|
166 | + 'show_in_rest' => true, |
|
167 | + /** |
|
168 | + * Filter: wl_feature__enable__vocabulary. |
|
169 | + * |
|
170 | + * @param bool whether the vocabulary needs to be shown in menu. |
|
171 | + * |
|
172 | + * @return bool |
|
173 | + * @since 3.27.6 |
|
174 | + */ |
|
175 | + 'show_in_menu' => apply_filters( 'wl_feature__enable__vocabulary', true ), |
|
176 | + ); |
|
177 | + |
|
178 | + register_post_type( $this->post_type, $args ); |
|
179 | + |
|
180 | + // Enable WP's standard `category` taxonomy for entities. |
|
181 | + // |
|
182 | + // While this enables editors to bind entities to the WP posts' category |
|
183 | + // taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter |
|
184 | + // WP's main category query to include the `entity` post type. |
|
185 | + // |
|
186 | + // See https://github.com/insideout10/wordlift-plugin/issues/442 |
|
187 | + register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
188 | + |
|
189 | + } |
|
190 | 190 | |
191 | 191 | } |
@@ -60,9 +60,9 @@ discard block |
||
60 | 60 | * |
61 | 61 | * @since 3.6.0 |
62 | 62 | */ |
63 | - public function __construct( $post_type, $slug ) { |
|
63 | + public function __construct($post_type, $slug) { |
|
64 | 64 | |
65 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
65 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
66 | 66 | |
67 | 67 | $this->post_type = $post_type; |
68 | 68 | |
@@ -114,22 +114,22 @@ discard block |
||
114 | 114 | */ |
115 | 115 | public function register() { |
116 | 116 | |
117 | - $this->log->debug( "Registering post type $this->post_type..." ); |
|
117 | + $this->log->debug("Registering post type $this->post_type..."); |
|
118 | 118 | |
119 | 119 | $labels = array( |
120 | - 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
121 | - 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
122 | - 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
123 | - 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
124 | - 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
125 | - 'new_item' => __( 'New Entity', 'wordlift' ), |
|
126 | - 'all_items' => __( 'All Entities', 'wordlift' ), |
|
127 | - 'view_item' => __( 'View Entity', 'wordlift' ), |
|
128 | - 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
129 | - 'not_found' => __( 'No entities found', 'wordlift' ), |
|
130 | - 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
120 | + 'name' => _x('Vocabulary', 'post type general name', 'wordlift'), |
|
121 | + 'singular_name' => _x('Entity', 'post type singular name', 'wordlift'), |
|
122 | + 'add_new' => _x('Add New Entity', 'entity', 'wordlift'), |
|
123 | + 'add_new_item' => __('Add New Entity', 'wordlift'), |
|
124 | + 'edit_item' => __('Edit Entity', 'wordlift'), |
|
125 | + 'new_item' => __('New Entity', 'wordlift'), |
|
126 | + 'all_items' => __('All Entities', 'wordlift'), |
|
127 | + 'view_item' => __('View Entity', 'wordlift'), |
|
128 | + 'search_items' => __('Search in Vocabulary', 'wordlift'), |
|
129 | + 'not_found' => __('No entities found', 'wordlift'), |
|
130 | + 'not_found_in_trash' => __('No entities found in the Trash', 'wordlift'), |
|
131 | 131 | 'parent_item_colon' => '', |
132 | - 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
132 | + 'menu_name' => __('Vocabulary', 'wordlift'), |
|
133 | 133 | ); |
134 | 134 | |
135 | 135 | $args = array( |
@@ -152,9 +152,9 @@ discard block |
||
152 | 152 | 'custom-fields', |
153 | 153 | ), |
154 | 154 | 'has_archive' => true, |
155 | - 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
155 | + 'menu_icon' => WP_CONTENT_URL.'/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
156 | 156 | // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
157 | - 'rewrite' => array( 'slug' => $this->slug ), |
|
157 | + 'rewrite' => array('slug' => $this->slug), |
|
158 | 158 | 'capability_type' => array( |
159 | 159 | 'wordlift_entity', |
160 | 160 | 'wordlift_entities', |
@@ -172,10 +172,10 @@ discard block |
||
172 | 172 | * @return bool |
173 | 173 | * @since 3.27.6 |
174 | 174 | */ |
175 | - 'show_in_menu' => apply_filters( 'wl_feature__enable__vocabulary', true ), |
|
175 | + 'show_in_menu' => apply_filters('wl_feature__enable__vocabulary', true), |
|
176 | 176 | ); |
177 | 177 | |
178 | - register_post_type( $this->post_type, $args ); |
|
178 | + register_post_type($this->post_type, $args); |
|
179 | 179 | |
180 | 180 | // Enable WP's standard `category` taxonomy for entities. |
181 | 181 | // |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | // WP's main category query to include the `entity` post type. |
185 | 185 | // |
186 | 186 | // See https://github.com/insideout10/wordlift-plugin/issues/442 |
187 | - register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
187 | + register_taxonomy_for_object_type('category', $this->post_type); |
|
188 | 188 | |
189 | 189 | } |
190 | 190 |
@@ -18,235 +18,235 @@ |
||
18 | 18 | */ |
19 | 19 | class Wordlift_Publisher_Service { |
20 | 20 | |
21 | - protected function __construct() { |
|
22 | - |
|
23 | - } |
|
24 | - |
|
25 | - private static $instance = null; |
|
26 | - |
|
27 | - public static function get_instance() { |
|
28 | - |
|
29 | - if ( ! isset( self::$instance ) ) { |
|
30 | - self::$instance = new self(); |
|
31 | - } |
|
32 | - |
|
33 | - return self::$instance; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Counts the number of potential publishers. |
|
38 | - * |
|
39 | - * @return int The number of potential publishers. |
|
40 | - * @since 3.11.0 |
|
41 | - */ |
|
42 | - public function count() { |
|
43 | - |
|
44 | - // Search for entities which are either a Person |
|
45 | - // or Organization. |
|
46 | - |
|
47 | - // Get only the ids as all we need is the count. |
|
48 | - $entities = get_posts( |
|
49 | - array( |
|
50 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
51 | - 'post_status' => 'publish', |
|
52 | - 'posts_per_page' => - 1, |
|
53 | - 'tax_query' => array( |
|
54 | - array( |
|
55 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
56 | - 'field' => 'slug', |
|
57 | - 'terms' => array( 'organization', 'person' ), |
|
58 | - ), |
|
59 | - ), |
|
60 | - 'fields' => 'ids', |
|
61 | - ) |
|
62 | - ); |
|
63 | - |
|
64 | - // Finally return the count. |
|
65 | - return count( $entities ); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Search SQL filter for matching against post title only. |
|
70 | - * |
|
71 | - * @link http://wordpress.stackexchange.com/a/11826/1685 |
|
72 | - * |
|
73 | - * @since 3.15.0 |
|
74 | - * |
|
75 | - * @param string $search The search string. |
|
76 | - * @param WP_Query $wp_query The {@link WP_Query} instance. |
|
77 | - * |
|
78 | - * @return array|string An array of results. |
|
79 | - */ |
|
80 | - public function limit_search_to_title( $search, $wp_query ) { |
|
81 | - |
|
82 | - // Bail out if the search or the `search_terms` haven't been set. |
|
83 | - if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) { |
|
84 | - return $search; |
|
85 | - } |
|
86 | - |
|
87 | - global $wpdb; |
|
88 | - |
|
89 | - $query_vars = $wp_query->query_vars; |
|
90 | - $percent = ! empty( $query_vars['exact'] ) ? '' : '%'; |
|
91 | - $search = array(); |
|
92 | - |
|
93 | - foreach ( (array) $query_vars['search_terms'] as $term ) { |
|
94 | - $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent ); |
|
95 | - } |
|
96 | - |
|
97 | - if ( ! is_user_logged_in() ) { |
|
98 | - $search[] = "$wpdb->posts.post_password = ''"; |
|
99 | - } |
|
100 | - |
|
101 | - $search = ' AND ' . implode( ' AND ', $search ); |
|
102 | - |
|
103 | - return $search; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Query WP for potential publishers, i.e. {@link WP_Post}s which are associated` |
|
108 | - * with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`. |
|
109 | - * |
|
110 | - * @param string $filter The title filter. |
|
111 | - * |
|
112 | - * @return array An array of results in a select2 friendly format. |
|
113 | - * @since 3.11.0 |
|
114 | - */ |
|
115 | - public function query( $filter = '' ) { |
|
116 | - |
|
117 | - // Search for the filter in the titles only. |
|
118 | - add_filter( |
|
119 | - 'posts_search', |
|
120 | - array( |
|
121 | - $this, |
|
122 | - 'limit_search_to_title', |
|
123 | - ), |
|
124 | - 10, |
|
125 | - 2 |
|
126 | - ); |
|
127 | - |
|
128 | - /* |
|
21 | + protected function __construct() { |
|
22 | + |
|
23 | + } |
|
24 | + |
|
25 | + private static $instance = null; |
|
26 | + |
|
27 | + public static function get_instance() { |
|
28 | + |
|
29 | + if ( ! isset( self::$instance ) ) { |
|
30 | + self::$instance = new self(); |
|
31 | + } |
|
32 | + |
|
33 | + return self::$instance; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Counts the number of potential publishers. |
|
38 | + * |
|
39 | + * @return int The number of potential publishers. |
|
40 | + * @since 3.11.0 |
|
41 | + */ |
|
42 | + public function count() { |
|
43 | + |
|
44 | + // Search for entities which are either a Person |
|
45 | + // or Organization. |
|
46 | + |
|
47 | + // Get only the ids as all we need is the count. |
|
48 | + $entities = get_posts( |
|
49 | + array( |
|
50 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
51 | + 'post_status' => 'publish', |
|
52 | + 'posts_per_page' => - 1, |
|
53 | + 'tax_query' => array( |
|
54 | + array( |
|
55 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
56 | + 'field' => 'slug', |
|
57 | + 'terms' => array( 'organization', 'person' ), |
|
58 | + ), |
|
59 | + ), |
|
60 | + 'fields' => 'ids', |
|
61 | + ) |
|
62 | + ); |
|
63 | + |
|
64 | + // Finally return the count. |
|
65 | + return count( $entities ); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Search SQL filter for matching against post title only. |
|
70 | + * |
|
71 | + * @link http://wordpress.stackexchange.com/a/11826/1685 |
|
72 | + * |
|
73 | + * @since 3.15.0 |
|
74 | + * |
|
75 | + * @param string $search The search string. |
|
76 | + * @param WP_Query $wp_query The {@link WP_Query} instance. |
|
77 | + * |
|
78 | + * @return array|string An array of results. |
|
79 | + */ |
|
80 | + public function limit_search_to_title( $search, $wp_query ) { |
|
81 | + |
|
82 | + // Bail out if the search or the `search_terms` haven't been set. |
|
83 | + if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) { |
|
84 | + return $search; |
|
85 | + } |
|
86 | + |
|
87 | + global $wpdb; |
|
88 | + |
|
89 | + $query_vars = $wp_query->query_vars; |
|
90 | + $percent = ! empty( $query_vars['exact'] ) ? '' : '%'; |
|
91 | + $search = array(); |
|
92 | + |
|
93 | + foreach ( (array) $query_vars['search_terms'] as $term ) { |
|
94 | + $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent ); |
|
95 | + } |
|
96 | + |
|
97 | + if ( ! is_user_logged_in() ) { |
|
98 | + $search[] = "$wpdb->posts.post_password = ''"; |
|
99 | + } |
|
100 | + |
|
101 | + $search = ' AND ' . implode( ' AND ', $search ); |
|
102 | + |
|
103 | + return $search; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Query WP for potential publishers, i.e. {@link WP_Post}s which are associated` |
|
108 | + * with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`. |
|
109 | + * |
|
110 | + * @param string $filter The title filter. |
|
111 | + * |
|
112 | + * @return array An array of results in a select2 friendly format. |
|
113 | + * @since 3.11.0 |
|
114 | + */ |
|
115 | + public function query( $filter = '' ) { |
|
116 | + |
|
117 | + // Search for the filter in the titles only. |
|
118 | + add_filter( |
|
119 | + 'posts_search', |
|
120 | + array( |
|
121 | + $this, |
|
122 | + 'limit_search_to_title', |
|
123 | + ), |
|
124 | + 10, |
|
125 | + 2 |
|
126 | + ); |
|
127 | + |
|
128 | + /* |
|
129 | 129 | * Search for entities which are either a Person |
130 | 130 | * or Organization. Sort the results by title in ascending order. |
131 | 131 | */ |
132 | - $entities = get_posts( |
|
133 | - array( |
|
134 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
135 | - 'post_status' => 'publish', |
|
136 | - 'posts_per_page' => - 1, |
|
137 | - 'tax_query' => array( |
|
138 | - array( |
|
139 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
140 | - 'field' => 'slug', |
|
141 | - 'terms' => array( 'organization', 'person' ), |
|
142 | - ), |
|
143 | - ), |
|
144 | - 's' => $filter, |
|
145 | - 'orderby' => 'title', |
|
146 | - 'order' => 'ASC', |
|
147 | - ) |
|
148 | - ); |
|
149 | - |
|
150 | - // Remove the search filter added before the query. |
|
151 | - remove_filter( |
|
152 | - 'posts_search', |
|
153 | - array( |
|
154 | - $this, |
|
155 | - 'limit_search_to_title', |
|
156 | - ), |
|
157 | - 10, |
|
158 | - 2 |
|
159 | - ); |
|
160 | - |
|
161 | - // Set a reference to ourselves to pass to the closure. |
|
162 | - $publisher_service = $this; |
|
163 | - |
|
164 | - // Map the results in a `Select2` compatible array. |
|
165 | - return array_map( |
|
166 | - function ( $entity ) use ( $publisher_service ) { |
|
167 | - $type = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
168 | - $thumb_id = get_post_thumbnail_id( $entity->ID ); |
|
169 | - |
|
170 | - return array( |
|
171 | - 'id' => $entity->ID, |
|
172 | - 'text' => $entity->post_title, |
|
173 | - 'type' => $type[0]->name, |
|
174 | - 'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ), |
|
175 | - ); |
|
176 | - }, |
|
177 | - $entities |
|
178 | - ); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the thumbnail's URL. |
|
183 | - * |
|
184 | - * @param int $attachment_id The attachment id. |
|
185 | - * @param string $size The attachment size (default = 'thumbnail'). |
|
186 | - * |
|
187 | - * @return string|bool The image URL or false if not found. |
|
188 | - * @since 3.11.0 |
|
189 | - */ |
|
190 | - public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) { |
|
191 | - |
|
192 | - $image = wp_get_attachment_image_src( $attachment_id, $size ); |
|
193 | - |
|
194 | - return isset( $image['0'] ) ? $image['0'] : false; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Add additional instructions to featured image metabox |
|
199 | - * when the entity type is the publisher. |
|
200 | - * |
|
201 | - * @param string $content Current metabox content. |
|
202 | - * |
|
203 | - * @return string $content metabox content with additional instructions. |
|
204 | - * @since 3.19.0 |
|
205 | - */ |
|
206 | - public function add_featured_image_instruction( $content ) { |
|
207 | - // Get the current post ID. |
|
208 | - $post_id = get_the_ID(); |
|
209 | - |
|
210 | - // Get the publisher id. |
|
211 | - $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
|
212 | - |
|
213 | - // Bail if for some reason the post id is not set. |
|
214 | - if ( |
|
215 | - empty( $post_id ) || |
|
216 | - $post_id !== (int) $publisher_id |
|
217 | - ) { |
|
218 | - return $content; |
|
219 | - } |
|
220 | - |
|
221 | - $terms = wp_get_post_terms( |
|
222 | - $post_id, // The post id. |
|
223 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, // The taxonomy slug. |
|
224 | - array( |
|
225 | - 'fields' => 'slugs', |
|
226 | - // We don't need all fields, but only slugs. |
|
227 | - ) |
|
228 | - ); |
|
229 | - |
|
230 | - // Check that the entity type is "Organization". |
|
231 | - if ( in_array( 'organization', $terms, true ) ) { |
|
232 | - // Add the featured image description when the type is "Organization". |
|
233 | - |
|
234 | - $link = sprintf( |
|
235 | - '<a target="_blank" href="%s">%s</a>', |
|
236 | - esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ), |
|
237 | - esc_html__( 'AMP logo guidelines', 'wordlift' ) |
|
238 | - ); |
|
239 | - $content .= sprintf( |
|
240 | - '<p>' |
|
241 | - /* translators: %s: AMP logo guidelines. */ |
|
242 | - . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' ) |
|
243 | - . '</p>', |
|
244 | - $link |
|
245 | - ); |
|
246 | - } |
|
247 | - |
|
248 | - // Finally return the content. |
|
249 | - return $content; |
|
250 | - } |
|
132 | + $entities = get_posts( |
|
133 | + array( |
|
134 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
135 | + 'post_status' => 'publish', |
|
136 | + 'posts_per_page' => - 1, |
|
137 | + 'tax_query' => array( |
|
138 | + array( |
|
139 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
140 | + 'field' => 'slug', |
|
141 | + 'terms' => array( 'organization', 'person' ), |
|
142 | + ), |
|
143 | + ), |
|
144 | + 's' => $filter, |
|
145 | + 'orderby' => 'title', |
|
146 | + 'order' => 'ASC', |
|
147 | + ) |
|
148 | + ); |
|
149 | + |
|
150 | + // Remove the search filter added before the query. |
|
151 | + remove_filter( |
|
152 | + 'posts_search', |
|
153 | + array( |
|
154 | + $this, |
|
155 | + 'limit_search_to_title', |
|
156 | + ), |
|
157 | + 10, |
|
158 | + 2 |
|
159 | + ); |
|
160 | + |
|
161 | + // Set a reference to ourselves to pass to the closure. |
|
162 | + $publisher_service = $this; |
|
163 | + |
|
164 | + // Map the results in a `Select2` compatible array. |
|
165 | + return array_map( |
|
166 | + function ( $entity ) use ( $publisher_service ) { |
|
167 | + $type = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
168 | + $thumb_id = get_post_thumbnail_id( $entity->ID ); |
|
169 | + |
|
170 | + return array( |
|
171 | + 'id' => $entity->ID, |
|
172 | + 'text' => $entity->post_title, |
|
173 | + 'type' => $type[0]->name, |
|
174 | + 'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ), |
|
175 | + ); |
|
176 | + }, |
|
177 | + $entities |
|
178 | + ); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the thumbnail's URL. |
|
183 | + * |
|
184 | + * @param int $attachment_id The attachment id. |
|
185 | + * @param string $size The attachment size (default = 'thumbnail'). |
|
186 | + * |
|
187 | + * @return string|bool The image URL or false if not found. |
|
188 | + * @since 3.11.0 |
|
189 | + */ |
|
190 | + public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) { |
|
191 | + |
|
192 | + $image = wp_get_attachment_image_src( $attachment_id, $size ); |
|
193 | + |
|
194 | + return isset( $image['0'] ) ? $image['0'] : false; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Add additional instructions to featured image metabox |
|
199 | + * when the entity type is the publisher. |
|
200 | + * |
|
201 | + * @param string $content Current metabox content. |
|
202 | + * |
|
203 | + * @return string $content metabox content with additional instructions. |
|
204 | + * @since 3.19.0 |
|
205 | + */ |
|
206 | + public function add_featured_image_instruction( $content ) { |
|
207 | + // Get the current post ID. |
|
208 | + $post_id = get_the_ID(); |
|
209 | + |
|
210 | + // Get the publisher id. |
|
211 | + $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
|
212 | + |
|
213 | + // Bail if for some reason the post id is not set. |
|
214 | + if ( |
|
215 | + empty( $post_id ) || |
|
216 | + $post_id !== (int) $publisher_id |
|
217 | + ) { |
|
218 | + return $content; |
|
219 | + } |
|
220 | + |
|
221 | + $terms = wp_get_post_terms( |
|
222 | + $post_id, // The post id. |
|
223 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, // The taxonomy slug. |
|
224 | + array( |
|
225 | + 'fields' => 'slugs', |
|
226 | + // We don't need all fields, but only slugs. |
|
227 | + ) |
|
228 | + ); |
|
229 | + |
|
230 | + // Check that the entity type is "Organization". |
|
231 | + if ( in_array( 'organization', $terms, true ) ) { |
|
232 | + // Add the featured image description when the type is "Organization". |
|
233 | + |
|
234 | + $link = sprintf( |
|
235 | + '<a target="_blank" href="%s">%s</a>', |
|
236 | + esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ), |
|
237 | + esc_html__( 'AMP logo guidelines', 'wordlift' ) |
|
238 | + ); |
|
239 | + $content .= sprintf( |
|
240 | + '<p>' |
|
241 | + /* translators: %s: AMP logo guidelines. */ |
|
242 | + . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' ) |
|
243 | + . '</p>', |
|
244 | + $link |
|
245 | + ); |
|
246 | + } |
|
247 | + |
|
248 | + // Finally return the content. |
|
249 | + return $content; |
|
250 | + } |
|
251 | 251 | |
252 | 252 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | |
27 | 27 | public static function get_instance() { |
28 | 28 | |
29 | - if ( ! isset( self::$instance ) ) { |
|
29 | + if ( ! isset(self::$instance)) { |
|
30 | 30 | self::$instance = new self(); |
31 | 31 | } |
32 | 32 | |
@@ -49,12 +49,12 @@ discard block |
||
49 | 49 | array( |
50 | 50 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
51 | 51 | 'post_status' => 'publish', |
52 | - 'posts_per_page' => - 1, |
|
52 | + 'posts_per_page' => -1, |
|
53 | 53 | 'tax_query' => array( |
54 | 54 | array( |
55 | 55 | 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
56 | 56 | 'field' => 'slug', |
57 | - 'terms' => array( 'organization', 'person' ), |
|
57 | + 'terms' => array('organization', 'person'), |
|
58 | 58 | ), |
59 | 59 | ), |
60 | 60 | 'fields' => 'ids', |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | ); |
63 | 63 | |
64 | 64 | // Finally return the count. |
65 | - return count( $entities ); |
|
65 | + return count($entities); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | /** |
@@ -77,28 +77,28 @@ discard block |
||
77 | 77 | * |
78 | 78 | * @return array|string An array of results. |
79 | 79 | */ |
80 | - public function limit_search_to_title( $search, $wp_query ) { |
|
80 | + public function limit_search_to_title($search, $wp_query) { |
|
81 | 81 | |
82 | 82 | // Bail out if the search or the `search_terms` haven't been set. |
83 | - if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) { |
|
83 | + if (empty($search) || empty($wp_query->query_vars['search_terms'])) { |
|
84 | 84 | return $search; |
85 | 85 | } |
86 | 86 | |
87 | 87 | global $wpdb; |
88 | 88 | |
89 | 89 | $query_vars = $wp_query->query_vars; |
90 | - $percent = ! empty( $query_vars['exact'] ) ? '' : '%'; |
|
90 | + $percent = ! empty($query_vars['exact']) ? '' : '%'; |
|
91 | 91 | $search = array(); |
92 | 92 | |
93 | - foreach ( (array) $query_vars['search_terms'] as $term ) { |
|
94 | - $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent ); |
|
93 | + foreach ((array) $query_vars['search_terms'] as $term) { |
|
94 | + $search[] = $wpdb->prepare("$wpdb->posts.post_title LIKE %s", $percent.$wpdb->esc_like($term).$percent); |
|
95 | 95 | } |
96 | 96 | |
97 | - if ( ! is_user_logged_in() ) { |
|
97 | + if ( ! is_user_logged_in()) { |
|
98 | 98 | $search[] = "$wpdb->posts.post_password = ''"; |
99 | 99 | } |
100 | 100 | |
101 | - $search = ' AND ' . implode( ' AND ', $search ); |
|
101 | + $search = ' AND '.implode(' AND ', $search); |
|
102 | 102 | |
103 | 103 | return $search; |
104 | 104 | } |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | * @return array An array of results in a select2 friendly format. |
113 | 113 | * @since 3.11.0 |
114 | 114 | */ |
115 | - public function query( $filter = '' ) { |
|
115 | + public function query($filter = '') { |
|
116 | 116 | |
117 | 117 | // Search for the filter in the titles only. |
118 | 118 | add_filter( |
@@ -133,12 +133,12 @@ discard block |
||
133 | 133 | array( |
134 | 134 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
135 | 135 | 'post_status' => 'publish', |
136 | - 'posts_per_page' => - 1, |
|
136 | + 'posts_per_page' => -1, |
|
137 | 137 | 'tax_query' => array( |
138 | 138 | array( |
139 | 139 | 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
140 | 140 | 'field' => 'slug', |
141 | - 'terms' => array( 'organization', 'person' ), |
|
141 | + 'terms' => array('organization', 'person'), |
|
142 | 142 | ), |
143 | 143 | ), |
144 | 144 | 's' => $filter, |
@@ -163,15 +163,15 @@ discard block |
||
163 | 163 | |
164 | 164 | // Map the results in a `Select2` compatible array. |
165 | 165 | return array_map( |
166 | - function ( $entity ) use ( $publisher_service ) { |
|
167 | - $type = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
168 | - $thumb_id = get_post_thumbnail_id( $entity->ID ); |
|
166 | + function($entity) use ($publisher_service) { |
|
167 | + $type = wp_get_post_terms($entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
168 | + $thumb_id = get_post_thumbnail_id($entity->ID); |
|
169 | 169 | |
170 | 170 | return array( |
171 | 171 | 'id' => $entity->ID, |
172 | 172 | 'text' => $entity->post_title, |
173 | 173 | 'type' => $type[0]->name, |
174 | - 'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ), |
|
174 | + 'thumbnail_url' => $publisher_service->get_attachment_image_url($thumb_id), |
|
175 | 175 | ); |
176 | 176 | }, |
177 | 177 | $entities |
@@ -187,11 +187,11 @@ discard block |
||
187 | 187 | * @return string|bool The image URL or false if not found. |
188 | 188 | * @since 3.11.0 |
189 | 189 | */ |
190 | - public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) { |
|
190 | + public function get_attachment_image_url($attachment_id, $size = 'thumbnail') { |
|
191 | 191 | |
192 | - $image = wp_get_attachment_image_src( $attachment_id, $size ); |
|
192 | + $image = wp_get_attachment_image_src($attachment_id, $size); |
|
193 | 193 | |
194 | - return isset( $image['0'] ) ? $image['0'] : false; |
|
194 | + return isset($image['0']) ? $image['0'] : false; |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | * @return string $content metabox content with additional instructions. |
204 | 204 | * @since 3.19.0 |
205 | 205 | */ |
206 | - public function add_featured_image_instruction( $content ) { |
|
206 | + public function add_featured_image_instruction($content) { |
|
207 | 207 | // Get the current post ID. |
208 | 208 | $post_id = get_the_ID(); |
209 | 209 | |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | |
213 | 213 | // Bail if for some reason the post id is not set. |
214 | 214 | if ( |
215 | - empty( $post_id ) || |
|
215 | + empty($post_id) || |
|
216 | 216 | $post_id !== (int) $publisher_id |
217 | 217 | ) { |
218 | 218 | return $content; |
@@ -228,18 +228,18 @@ discard block |
||
228 | 228 | ); |
229 | 229 | |
230 | 230 | // Check that the entity type is "Organization". |
231 | - if ( in_array( 'organization', $terms, true ) ) { |
|
231 | + if (in_array('organization', $terms, true)) { |
|
232 | 232 | // Add the featured image description when the type is "Organization". |
233 | 233 | |
234 | - $link = sprintf( |
|
234 | + $link = sprintf( |
|
235 | 235 | '<a target="_blank" href="%s">%s</a>', |
236 | - esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ), |
|
237 | - esc_html__( 'AMP logo guidelines', 'wordlift' ) |
|
236 | + esc_attr__('https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift'), |
|
237 | + esc_html__('AMP logo guidelines', 'wordlift') |
|
238 | 238 | ); |
239 | 239 | $content .= sprintf( |
240 | 240 | '<p>' |
241 | 241 | /* translators: %s: AMP logo guidelines. */ |
242 | - . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' ) |
|
242 | + . esc_html_x('According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift') |
|
243 | 243 | . '</p>', |
244 | 244 | $link |
245 | 245 | ); |