@@ -18,16 +18,16 @@ discard block |
||
18 | 18 | */ |
19 | 19 | function wl_core_check_relation_predicate_is_supported( $predicate ) { |
20 | 20 | |
21 | - return in_array( |
|
22 | - $predicate, |
|
23 | - array( |
|
24 | - WL_WHAT_RELATION, |
|
25 | - WL_WHEN_RELATION, |
|
26 | - WL_WHERE_RELATION, |
|
27 | - WL_WHO_RELATION, |
|
28 | - ), |
|
29 | - true |
|
30 | - ); |
|
21 | + return in_array( |
|
22 | + $predicate, |
|
23 | + array( |
|
24 | + WL_WHAT_RELATION, |
|
25 | + WL_WHEN_RELATION, |
|
26 | + WL_WHERE_RELATION, |
|
27 | + WL_WHO_RELATION, |
|
28 | + ), |
|
29 | + true |
|
30 | + ); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | /** |
@@ -37,8 +37,8 @@ discard block |
||
37 | 37 | */ |
38 | 38 | function wl_core_get_validation_rules() { |
39 | 39 | |
40 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
41 | - return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
40 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
41 | + return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
@@ -48,10 +48,10 @@ discard block |
||
48 | 48 | */ |
49 | 49 | function wl_core_get_relation_instances_table_name() { |
50 | 50 | |
51 | - global $wpdb; |
|
52 | - $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
51 | + global $wpdb; |
|
52 | + $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
53 | 53 | |
54 | - return $table_name; |
|
54 | + return $table_name; |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -68,55 +68,55 @@ discard block |
||
68 | 68 | */ |
69 | 69 | function wl_core_add_relation_instance( $subject_id, $predicate, $object_id, $subject_type = Object_Type_Enum::POST, $object_type = Object_Type_Enum::POST ) { |
70 | 70 | |
71 | - // Checks on subject and object |
|
72 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
73 | - return false; |
|
74 | - } |
|
75 | - |
|
76 | - // Checks on the given relation |
|
77 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
78 | - return false; |
|
79 | - } |
|
80 | - |
|
81 | - // Ensure these are `int`s. This is especially useful to ensure that parties that hook to our actions receive |
|
82 | - // the right var types. |
|
83 | - $subject_id = (int) $subject_id; |
|
84 | - $object_id = (int) $object_id; |
|
85 | - |
|
86 | - // Prepare interaction with db |
|
87 | - global $wpdb; |
|
88 | - |
|
89 | - // Checks passed. Add relation if not exists: |
|
90 | - // |
|
91 | - // See https://codex.wordpress.org/Class_Reference/wpdb#REPLACE_row |
|
92 | - $wpdb->replace( |
|
93 | - wl_core_get_relation_instances_table_name(), |
|
94 | - array( |
|
95 | - 'subject_id' => $subject_id, |
|
96 | - 'predicate' => $predicate, |
|
97 | - 'object_id' => $object_id, |
|
98 | - 'subject_type' => $subject_type, |
|
99 | - 'object_type' => $object_type, |
|
100 | - ), |
|
101 | - array( '%d', '%s', '%d', '%d', '%d' ) |
|
102 | - ); |
|
103 | - |
|
104 | - /** |
|
105 | - * Hooks: Relation Added. |
|
106 | - * |
|
107 | - * Fire a hook when a new relation between a post/entity and an entity is |
|
108 | - * added (the relation may already exists). |
|
109 | - * |
|
110 | - * @param int $subject_id The subject {@link WP_Post} id. |
|
111 | - * @param string $predicate The predicate. |
|
112 | - * @param int $object_id The object {@link WP_Post} id. |
|
113 | - * |
|
114 | - * @since 3.16.0 |
|
115 | - */ |
|
116 | - do_action( 'wl_relation_added', $subject_id, $predicate, $object_id ); |
|
117 | - |
|
118 | - // Return record id |
|
119 | - return $wpdb->insert_id; |
|
71 | + // Checks on subject and object |
|
72 | + if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
73 | + return false; |
|
74 | + } |
|
75 | + |
|
76 | + // Checks on the given relation |
|
77 | + if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
78 | + return false; |
|
79 | + } |
|
80 | + |
|
81 | + // Ensure these are `int`s. This is especially useful to ensure that parties that hook to our actions receive |
|
82 | + // the right var types. |
|
83 | + $subject_id = (int) $subject_id; |
|
84 | + $object_id = (int) $object_id; |
|
85 | + |
|
86 | + // Prepare interaction with db |
|
87 | + global $wpdb; |
|
88 | + |
|
89 | + // Checks passed. Add relation if not exists: |
|
90 | + // |
|
91 | + // See https://codex.wordpress.org/Class_Reference/wpdb#REPLACE_row |
|
92 | + $wpdb->replace( |
|
93 | + wl_core_get_relation_instances_table_name(), |
|
94 | + array( |
|
95 | + 'subject_id' => $subject_id, |
|
96 | + 'predicate' => $predicate, |
|
97 | + 'object_id' => $object_id, |
|
98 | + 'subject_type' => $subject_type, |
|
99 | + 'object_type' => $object_type, |
|
100 | + ), |
|
101 | + array( '%d', '%s', '%d', '%d', '%d' ) |
|
102 | + ); |
|
103 | + |
|
104 | + /** |
|
105 | + * Hooks: Relation Added. |
|
106 | + * |
|
107 | + * Fire a hook when a new relation between a post/entity and an entity is |
|
108 | + * added (the relation may already exists). |
|
109 | + * |
|
110 | + * @param int $subject_id The subject {@link WP_Post} id. |
|
111 | + * @param string $predicate The predicate. |
|
112 | + * @param int $object_id The object {@link WP_Post} id. |
|
113 | + * |
|
114 | + * @since 3.16.0 |
|
115 | + */ |
|
116 | + do_action( 'wl_relation_added', $subject_id, $predicate, $object_id ); |
|
117 | + |
|
118 | + // Return record id |
|
119 | + return $wpdb->insert_id; |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
@@ -129,41 +129,41 @@ discard block |
||
129 | 129 | */ |
130 | 130 | function wl_core_delete_relation_instances( $subject_id ) { |
131 | 131 | |
132 | - // Checks on subject and object |
|
133 | - if ( ! is_numeric( $subject_id ) ) { |
|
134 | - return false; |
|
135 | - } |
|
136 | - |
|
137 | - // Ensure these are `int`s. This is especially useful to ensure that parties that hook to our actions receive |
|
138 | - // the right var types. |
|
139 | - $subject_id = (int) $subject_id; |
|
140 | - |
|
141 | - // Prepare interaction with db |
|
142 | - global $wpdb; |
|
143 | - |
|
144 | - // wl_write_log( "Going to delete relation instances [ subject_id :: $subject_id ]"); |
|
145 | - |
|
146 | - // @see https://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
147 | - $wpdb->delete( |
|
148 | - wl_core_get_relation_instances_table_name(), |
|
149 | - array( |
|
150 | - 'subject_id' => $subject_id, |
|
151 | - ), |
|
152 | - array( '%d' ) |
|
153 | - ); |
|
154 | - |
|
155 | - /** |
|
156 | - * Hooks: Relation Deleted. |
|
157 | - * |
|
158 | - * The hook is fired after the relations with this post/entity are deleted. |
|
159 | - * |
|
160 | - * @param int $subject_id The subject {@link WP_Post} id. |
|
161 | - * |
|
162 | - * @since 3.16.0 |
|
163 | - */ |
|
164 | - do_action( 'wl_relation_deleted', $subject_id ); |
|
165 | - |
|
166 | - return true; |
|
132 | + // Checks on subject and object |
|
133 | + if ( ! is_numeric( $subject_id ) ) { |
|
134 | + return false; |
|
135 | + } |
|
136 | + |
|
137 | + // Ensure these are `int`s. This is especially useful to ensure that parties that hook to our actions receive |
|
138 | + // the right var types. |
|
139 | + $subject_id = (int) $subject_id; |
|
140 | + |
|
141 | + // Prepare interaction with db |
|
142 | + global $wpdb; |
|
143 | + |
|
144 | + // wl_write_log( "Going to delete relation instances [ subject_id :: $subject_id ]"); |
|
145 | + |
|
146 | + // @see https://codex.wordpress.org/it:Riferimento_classi/wpdb#DELETE_di_righe |
|
147 | + $wpdb->delete( |
|
148 | + wl_core_get_relation_instances_table_name(), |
|
149 | + array( |
|
150 | + 'subject_id' => $subject_id, |
|
151 | + ), |
|
152 | + array( '%d' ) |
|
153 | + ); |
|
154 | + |
|
155 | + /** |
|
156 | + * Hooks: Relation Deleted. |
|
157 | + * |
|
158 | + * The hook is fired after the relations with this post/entity are deleted. |
|
159 | + * |
|
160 | + * @param int $subject_id The subject {@link WP_Post} id. |
|
161 | + * |
|
162 | + * @since 3.16.0 |
|
163 | + */ |
|
164 | + do_action( 'wl_relation_deleted', $subject_id ); |
|
165 | + |
|
166 | + return true; |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -179,18 +179,18 @@ discard block |
||
179 | 179 | */ |
180 | 180 | function wl_core_validate_filters_for_related( $filters ) { |
181 | 181 | |
182 | - if ( ! is_array( $filters ) ) { |
|
183 | - $filters = array(); |
|
184 | - } |
|
182 | + if ( ! is_array( $filters ) ) { |
|
183 | + $filters = array(); |
|
184 | + } |
|
185 | 185 | |
186 | - if ( ! isset( $filters['predicate'] ) ) { |
|
187 | - $filters['predicate'] = null; |
|
188 | - } |
|
189 | - if ( ! isset( $filters['status'] ) ) { |
|
190 | - $filters['status'] = null; |
|
191 | - } |
|
186 | + if ( ! isset( $filters['predicate'] ) ) { |
|
187 | + $filters['predicate'] = null; |
|
188 | + } |
|
189 | + if ( ! isset( $filters['status'] ) ) { |
|
190 | + $filters['status'] = null; |
|
191 | + } |
|
192 | 192 | |
193 | - return $filters; |
|
193 | + return $filters; |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | // ** |
@@ -237,14 +237,14 @@ discard block |
||
237 | 237 | */ |
238 | 238 | function wl_core_get_related_entity_ids( $subject_id, $filters = array() ) { |
239 | 239 | |
240 | - $status = isset( $filters['status'] ) ? $filters['status'] : null; |
|
241 | - $predicate = isset( $filters['predicate'] ) ? $filters['predicate'] : null; |
|
240 | + $status = isset( $filters['status'] ) ? $filters['status'] : null; |
|
241 | + $predicate = isset( $filters['predicate'] ) ? $filters['predicate'] : null; |
|
242 | 242 | |
243 | - return Wordlift_Relation_Service::get_instance()->get_objects( $subject_id, 'ids', $predicate, $status ); |
|
243 | + return Wordlift_Relation_Service::get_instance()->get_objects( $subject_id, 'ids', $predicate, $status ); |
|
244 | 244 | |
245 | - // $filters = wl_core_validate_filters_for_related( $filters ); |
|
246 | - // |
|
247 | - // return wl_core_inner_get_related_entities( 'post_ids', $subject_id, $filters['predicate'], $filters['status'] ); |
|
245 | + // $filters = wl_core_validate_filters_for_related( $filters ); |
|
246 | + // |
|
247 | + // return wl_core_inner_get_related_entities( 'post_ids', $subject_id, $filters['predicate'], $filters['status'] ); |
|
248 | 248 | } |
249 | 249 | |
250 | 250 | /** |
@@ -260,14 +260,14 @@ discard block |
||
260 | 260 | */ |
261 | 261 | function wl_core_get_related_entities( $subject_id, $filters = array() ) { |
262 | 262 | |
263 | - $ids = wl_core_get_related_entity_ids( $subject_id, $filters ); |
|
263 | + $ids = wl_core_get_related_entity_ids( $subject_id, $filters ); |
|
264 | 264 | |
265 | - return array_map( |
|
266 | - function ( $item ) { |
|
267 | - return get_post( $item ); |
|
268 | - }, |
|
269 | - $ids |
|
270 | - ); |
|
265 | + return array_map( |
|
266 | + function ( $item ) { |
|
267 | + return get_post( $item ); |
|
268 | + }, |
|
269 | + $ids |
|
270 | + ); |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | // |
@@ -344,16 +344,16 @@ discard block |
||
344 | 344 | */ |
345 | 345 | function wl_core_get_related_post_ids( $object_id, $filters = array() ) { |
346 | 346 | |
347 | - $relation_service = Wordlift_Relation_Service::get_instance(); |
|
347 | + $relation_service = Wordlift_Relation_Service::get_instance(); |
|
348 | 348 | |
349 | - $status = isset( $filters['status'] ) ? $filters['status'] : null; |
|
350 | - $predicate = isset( $filters['predicate'] ) ? $filters['predicate'] : null; |
|
349 | + $status = isset( $filters['status'] ) ? $filters['status'] : null; |
|
350 | + $predicate = isset( $filters['predicate'] ) ? $filters['predicate'] : null; |
|
351 | 351 | |
352 | - return $relation_service->get_article_subjects( $object_id, 'ids', $predicate, $status ); |
|
353 | - // |
|
354 | - // $filters = wl_core_validate_filters_for_related( $filters ); |
|
355 | - // |
|
356 | - // return wl_core_inner_get_related_posts( 'post_ids', $object_id, $filters['predicate'], $filters['status'] ); |
|
352 | + return $relation_service->get_article_subjects( $object_id, 'ids', $predicate, $status ); |
|
353 | + // |
|
354 | + // $filters = wl_core_validate_filters_for_related( $filters ); |
|
355 | + // |
|
356 | + // return wl_core_inner_get_related_posts( 'post_ids', $object_id, $filters['predicate'], $filters['status'] ); |
|
357 | 357 | } |
358 | 358 | |
359 | 359 | /** |
@@ -371,14 +371,14 @@ discard block |
||
371 | 371 | */ |
372 | 372 | function wl_core_get_related_posts( $subject_id, $filters = array() ) { |
373 | 373 | |
374 | - $ids = wl_core_get_related_post_ids( $subject_id, $filters ); |
|
374 | + $ids = wl_core_get_related_post_ids( $subject_id, $filters ); |
|
375 | 375 | |
376 | - return array_map( |
|
377 | - function ( $item ) { |
|
378 | - return get_post( $item ); |
|
379 | - }, |
|
380 | - $ids |
|
381 | - ); |
|
376 | + return array_map( |
|
377 | + function ( $item ) { |
|
378 | + return get_post( $item ); |
|
379 | + }, |
|
380 | + $ids |
|
381 | + ); |
|
382 | 382 | } |
383 | 383 | |
384 | 384 | // ** |
@@ -464,129 +464,129 @@ discard block |
||
464 | 464 | */ |
465 | 465 | function wl_core_sql_query_builder( $args ) { |
466 | 466 | |
467 | - // Prepare interaction with db |
|
468 | - global $wpdb; |
|
469 | - |
|
470 | - // Retrieve Wordlift relation instances table name |
|
471 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
472 | - |
|
473 | - // When type is set to `post` we're looking for `post`s that are not |
|
474 | - // configured as entities. |
|
475 | - // When the type is set to `entity` we're looking also for `post`s that are |
|
476 | - // configured as entities. |
|
477 | - |
|
478 | - // Since we want Find only articles, based on the entity type, we need |
|
479 | - // to figure out the relevant sql statements to add to the join and where |
|
480 | - // parts. |
|
481 | - if ( 'entity' === $args['post_type'] ) { |
|
482 | - $tax_query = array( |
|
483 | - 'relation' => 'AND', |
|
484 | - array( |
|
485 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
486 | - 'operator' => 'EXISTS', |
|
487 | - ), |
|
488 | - array( |
|
489 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
490 | - 'field' => 'slug', |
|
491 | - 'terms' => 'article', |
|
492 | - 'operator' => 'NOT IN', |
|
493 | - ), |
|
494 | - ); |
|
495 | - } else { |
|
496 | - $tax_query = array( |
|
497 | - 'relation' => 'OR', |
|
498 | - array( |
|
499 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
500 | - 'operator' => 'NOT EXISTS', |
|
501 | - ), |
|
502 | - array( |
|
503 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
504 | - 'field' => 'slug', |
|
505 | - 'terms' => 'article', |
|
506 | - ), |
|
507 | - ); |
|
508 | - } |
|
509 | - |
|
510 | - // Use "p" as the table to match the initial join. |
|
511 | - $tax_sql = get_tax_sql( $tax_query, 'p', 'ID' ); |
|
512 | - |
|
513 | - // Sql Action |
|
514 | - $sql = 'SELECT '; |
|
515 | - // Determine what has to be returned depending on 'get' argument value |
|
516 | - switch ( $args['get'] ) { |
|
517 | - case 'posts': |
|
518 | - $sql .= 'p.*'; |
|
519 | - break; |
|
520 | - case 'post_ids': |
|
521 | - $sql .= 'p.id'; |
|
522 | - break; |
|
523 | - } |
|
524 | - |
|
525 | - // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
|
526 | - $join_column = $args['as'] . '_id'; |
|
527 | - |
|
528 | - $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
|
529 | - |
|
530 | - // Changing left join generate by the tax query into an inner since the term relationship has to exist. |
|
531 | - $sql .= str_replace( 'LEFT JOIN', 'INNER JOIN', $tax_sql['join'] ); |
|
532 | - |
|
533 | - // Sql add post type filter |
|
534 | - $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
535 | - $sql .= " AND p.post_type IN ('" . join( "', '", esc_sql( $post_types ) ) . "') AND"; |
|
536 | - |
|
537 | - // Sql add post status filter |
|
538 | - if ( isset( $args['post_status'] ) && $args['post_status'] !== null ) { |
|
539 | - $sql .= $wpdb->prepare( ' p.post_status = %s AND', $args['post_status'] ); |
|
540 | - } |
|
541 | - |
|
542 | - // Add filtering conditions |
|
543 | - // If we look for posts related as objects this means that |
|
544 | - // related_to is a reference for a subject: subject_id is the filtering column |
|
545 | - // If we look for posts related as subject this means that |
|
546 | - // related_to is reference for an object: object_id is the filtering column |
|
547 | - |
|
548 | - $filtering_column = ( 'object' === $args['as'] ) ? 'subject_id' : 'object_id'; |
|
549 | - |
|
550 | - if ( isset( $args['related_to'] ) ) { |
|
551 | - // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
552 | - $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
553 | - } |
|
554 | - if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
555 | - $sql .= ' AND'; |
|
556 | - } |
|
557 | - if ( isset( $args['related_to__in'] ) ) { |
|
558 | - $sql .= " r.$filtering_column IN (" . implode( ',', $args['related_to__in'] ) . ')'; |
|
559 | - // The IDs used for filtering shouldn't be in the results. |
|
560 | - $sql .= ' AND p.ID NOT IN (' . implode( ',', $args['related_to__in'] ) . ')'; |
|
561 | - } |
|
562 | - if ( isset( $args['post__not_in'] ) ) { |
|
563 | - $sql .= ' AND r.' . $args['as'] . '_id NOT IN (' . implode( ',', $args['post__not_in'] ) . ')'; |
|
564 | - } |
|
565 | - if ( isset( $args['post__in'] ) ) { |
|
566 | - $sql .= ' AND r.' . $args['as'] . '_id IN (' . implode( ',', $args['post__in'] ) . ')'; |
|
567 | - } |
|
568 | - // Add predicate filter if required. |
|
569 | - if ( isset( $args['with_predicate'] ) ) { |
|
570 | - // Sql Inner Join clause. |
|
571 | - $sql .= $wpdb->prepare( ' AND r.predicate = %s', $args['with_predicate'] ); |
|
572 | - } |
|
573 | - |
|
574 | - // Add the taxonomy related sql. |
|
575 | - $sql .= $tax_sql['where']; |
|
576 | - |
|
577 | - // Add a group by clause to avoid duplicated rows |
|
578 | - // @todo: isn't a distinct a better choice? |
|
579 | - $sql .= ' GROUP BY p.id'; |
|
580 | - |
|
581 | - // @todo: how does `first` represent the limit? |
|
582 | - if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
583 | - // Sql Inner Join clause. |
|
584 | - $sql .= $wpdb->prepare( ' LIMIT %d', $args['first'] ); |
|
585 | - } |
|
586 | - // Close sql statement |
|
587 | - $sql .= ';'; |
|
588 | - |
|
589 | - return $sql; |
|
467 | + // Prepare interaction with db |
|
468 | + global $wpdb; |
|
469 | + |
|
470 | + // Retrieve Wordlift relation instances table name |
|
471 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
472 | + |
|
473 | + // When type is set to `post` we're looking for `post`s that are not |
|
474 | + // configured as entities. |
|
475 | + // When the type is set to `entity` we're looking also for `post`s that are |
|
476 | + // configured as entities. |
|
477 | + |
|
478 | + // Since we want Find only articles, based on the entity type, we need |
|
479 | + // to figure out the relevant sql statements to add to the join and where |
|
480 | + // parts. |
|
481 | + if ( 'entity' === $args['post_type'] ) { |
|
482 | + $tax_query = array( |
|
483 | + 'relation' => 'AND', |
|
484 | + array( |
|
485 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
486 | + 'operator' => 'EXISTS', |
|
487 | + ), |
|
488 | + array( |
|
489 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
490 | + 'field' => 'slug', |
|
491 | + 'terms' => 'article', |
|
492 | + 'operator' => 'NOT IN', |
|
493 | + ), |
|
494 | + ); |
|
495 | + } else { |
|
496 | + $tax_query = array( |
|
497 | + 'relation' => 'OR', |
|
498 | + array( |
|
499 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
500 | + 'operator' => 'NOT EXISTS', |
|
501 | + ), |
|
502 | + array( |
|
503 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
504 | + 'field' => 'slug', |
|
505 | + 'terms' => 'article', |
|
506 | + ), |
|
507 | + ); |
|
508 | + } |
|
509 | + |
|
510 | + // Use "p" as the table to match the initial join. |
|
511 | + $tax_sql = get_tax_sql( $tax_query, 'p', 'ID' ); |
|
512 | + |
|
513 | + // Sql Action |
|
514 | + $sql = 'SELECT '; |
|
515 | + // Determine what has to be returned depending on 'get' argument value |
|
516 | + switch ( $args['get'] ) { |
|
517 | + case 'posts': |
|
518 | + $sql .= 'p.*'; |
|
519 | + break; |
|
520 | + case 'post_ids': |
|
521 | + $sql .= 'p.id'; |
|
522 | + break; |
|
523 | + } |
|
524 | + |
|
525 | + // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
|
526 | + $join_column = $args['as'] . '_id'; |
|
527 | + |
|
528 | + $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
|
529 | + |
|
530 | + // Changing left join generate by the tax query into an inner since the term relationship has to exist. |
|
531 | + $sql .= str_replace( 'LEFT JOIN', 'INNER JOIN', $tax_sql['join'] ); |
|
532 | + |
|
533 | + // Sql add post type filter |
|
534 | + $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
535 | + $sql .= " AND p.post_type IN ('" . join( "', '", esc_sql( $post_types ) ) . "') AND"; |
|
536 | + |
|
537 | + // Sql add post status filter |
|
538 | + if ( isset( $args['post_status'] ) && $args['post_status'] !== null ) { |
|
539 | + $sql .= $wpdb->prepare( ' p.post_status = %s AND', $args['post_status'] ); |
|
540 | + } |
|
541 | + |
|
542 | + // Add filtering conditions |
|
543 | + // If we look for posts related as objects this means that |
|
544 | + // related_to is a reference for a subject: subject_id is the filtering column |
|
545 | + // If we look for posts related as subject this means that |
|
546 | + // related_to is reference for an object: object_id is the filtering column |
|
547 | + |
|
548 | + $filtering_column = ( 'object' === $args['as'] ) ? 'subject_id' : 'object_id'; |
|
549 | + |
|
550 | + if ( isset( $args['related_to'] ) ) { |
|
551 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
552 | + $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
553 | + } |
|
554 | + if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
555 | + $sql .= ' AND'; |
|
556 | + } |
|
557 | + if ( isset( $args['related_to__in'] ) ) { |
|
558 | + $sql .= " r.$filtering_column IN (" . implode( ',', $args['related_to__in'] ) . ')'; |
|
559 | + // The IDs used for filtering shouldn't be in the results. |
|
560 | + $sql .= ' AND p.ID NOT IN (' . implode( ',', $args['related_to__in'] ) . ')'; |
|
561 | + } |
|
562 | + if ( isset( $args['post__not_in'] ) ) { |
|
563 | + $sql .= ' AND r.' . $args['as'] . '_id NOT IN (' . implode( ',', $args['post__not_in'] ) . ')'; |
|
564 | + } |
|
565 | + if ( isset( $args['post__in'] ) ) { |
|
566 | + $sql .= ' AND r.' . $args['as'] . '_id IN (' . implode( ',', $args['post__in'] ) . ')'; |
|
567 | + } |
|
568 | + // Add predicate filter if required. |
|
569 | + if ( isset( $args['with_predicate'] ) ) { |
|
570 | + // Sql Inner Join clause. |
|
571 | + $sql .= $wpdb->prepare( ' AND r.predicate = %s', $args['with_predicate'] ); |
|
572 | + } |
|
573 | + |
|
574 | + // Add the taxonomy related sql. |
|
575 | + $sql .= $tax_sql['where']; |
|
576 | + |
|
577 | + // Add a group by clause to avoid duplicated rows |
|
578 | + // @todo: isn't a distinct a better choice? |
|
579 | + $sql .= ' GROUP BY p.id'; |
|
580 | + |
|
581 | + // @todo: how does `first` represent the limit? |
|
582 | + if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
583 | + // Sql Inner Join clause. |
|
584 | + $sql .= $wpdb->prepare( ' LIMIT %d', $args['first'] ); |
|
585 | + } |
|
586 | + // Close sql statement |
|
587 | + $sql .= ';'; |
|
588 | + |
|
589 | + return $sql; |
|
590 | 590 | |
591 | 591 | } |
592 | 592 | |
@@ -605,73 +605,73 @@ discard block |
||
605 | 605 | */ |
606 | 606 | function wl_core_get_posts( $args, $returned_type = OBJECT ) { |
607 | 607 | |
608 | - // Merge given args with defaults args value |
|
609 | - $args = array_merge( |
|
610 | - array( |
|
611 | - 'with_predicate' => null, |
|
612 | - 'as' => 'subject', |
|
613 | - 'post_type' => 'post', |
|
614 | - 'get' => 'posts', |
|
615 | - 'post_status' => null, |
|
616 | - ), |
|
617 | - $args |
|
618 | - ); |
|
619 | - |
|
620 | - // Arguments validation rules |
|
621 | - // At least one between related_to and related_to__in has to be set |
|
622 | - if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
623 | - return false; |
|
624 | - } |
|
625 | - if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
626 | - return false; |
|
627 | - } |
|
628 | - |
|
629 | - // The same check is applied to post_in, post__not_in and related_to__in options |
|
630 | - // Only arrays with at least one numeric value are considerad valid |
|
631 | - // The argument value is further sanitized in order to clean up not numeric values |
|
632 | - foreach ( |
|
633 | - array( |
|
634 | - 'post__in', |
|
635 | - 'post__not_in', |
|
636 | - 'related_to__in', |
|
637 | - ) as $option_name |
|
638 | - ) { |
|
639 | - if ( isset( $args[ $option_name ] ) ) { |
|
640 | - if ( ! is_array( $args[ $option_name ] ) || 0 === count( array_filter( $args[ $option_name ], 'is_numeric' ) ) ) { |
|
641 | - return false; |
|
642 | - } |
|
643 | - // Sanitize value removing non numeric values from the array |
|
644 | - $args[ $option_name ] = array_filter( $args[ $option_name ], 'is_numeric' ); |
|
645 | - } |
|
646 | - } |
|
647 | - // Performing validation rules |
|
648 | - foreach ( wl_core_get_validation_rules() as $option_name => $accepted_values ) { |
|
649 | - if ( isset( $args[ $option_name ] ) && ( $args[ $option_name ] ) !== null ) { |
|
650 | - if ( ! in_array( $args[ $option_name ], $accepted_values, true ) ) { |
|
651 | - return false; |
|
652 | - } |
|
653 | - } |
|
654 | - } |
|
655 | - |
|
656 | - // Prepare interaction with db |
|
657 | - global $wpdb; |
|
658 | - |
|
659 | - // Build sql statement with given arguments |
|
660 | - $sql_statement = wl_core_sql_query_builder( $args ); |
|
661 | - |
|
662 | - // If ids are required, returns a one-dimensional array containing ids. |
|
663 | - // Otherwise an array of associative arrays representing the post | relation object |
|
664 | - if ( 'post_ids' === $args['get'] ) { |
|
665 | - // See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
|
666 | - $results = $wpdb->get_col( $sql_statement ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
667 | - } else { |
|
668 | - $results = $wpdb->get_results( $sql_statement, $returned_type ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
669 | - } |
|
670 | - // If there were an error performing the query then false is returned |
|
671 | - if ( ! empty( $wpdb->last_error ) ) { |
|
672 | - return false; |
|
673 | - } |
|
674 | - |
|
675 | - // Finally |
|
676 | - return $results; |
|
608 | + // Merge given args with defaults args value |
|
609 | + $args = array_merge( |
|
610 | + array( |
|
611 | + 'with_predicate' => null, |
|
612 | + 'as' => 'subject', |
|
613 | + 'post_type' => 'post', |
|
614 | + 'get' => 'posts', |
|
615 | + 'post_status' => null, |
|
616 | + ), |
|
617 | + $args |
|
618 | + ); |
|
619 | + |
|
620 | + // Arguments validation rules |
|
621 | + // At least one between related_to and related_to__in has to be set |
|
622 | + if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
623 | + return false; |
|
624 | + } |
|
625 | + if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
626 | + return false; |
|
627 | + } |
|
628 | + |
|
629 | + // The same check is applied to post_in, post__not_in and related_to__in options |
|
630 | + // Only arrays with at least one numeric value are considerad valid |
|
631 | + // The argument value is further sanitized in order to clean up not numeric values |
|
632 | + foreach ( |
|
633 | + array( |
|
634 | + 'post__in', |
|
635 | + 'post__not_in', |
|
636 | + 'related_to__in', |
|
637 | + ) as $option_name |
|
638 | + ) { |
|
639 | + if ( isset( $args[ $option_name ] ) ) { |
|
640 | + if ( ! is_array( $args[ $option_name ] ) || 0 === count( array_filter( $args[ $option_name ], 'is_numeric' ) ) ) { |
|
641 | + return false; |
|
642 | + } |
|
643 | + // Sanitize value removing non numeric values from the array |
|
644 | + $args[ $option_name ] = array_filter( $args[ $option_name ], 'is_numeric' ); |
|
645 | + } |
|
646 | + } |
|
647 | + // Performing validation rules |
|
648 | + foreach ( wl_core_get_validation_rules() as $option_name => $accepted_values ) { |
|
649 | + if ( isset( $args[ $option_name ] ) && ( $args[ $option_name ] ) !== null ) { |
|
650 | + if ( ! in_array( $args[ $option_name ], $accepted_values, true ) ) { |
|
651 | + return false; |
|
652 | + } |
|
653 | + } |
|
654 | + } |
|
655 | + |
|
656 | + // Prepare interaction with db |
|
657 | + global $wpdb; |
|
658 | + |
|
659 | + // Build sql statement with given arguments |
|
660 | + $sql_statement = wl_core_sql_query_builder( $args ); |
|
661 | + |
|
662 | + // If ids are required, returns a one-dimensional array containing ids. |
|
663 | + // Otherwise an array of associative arrays representing the post | relation object |
|
664 | + if ( 'post_ids' === $args['get'] ) { |
|
665 | + // See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
|
666 | + $results = $wpdb->get_col( $sql_statement ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
667 | + } else { |
|
668 | + $results = $wpdb->get_results( $sql_statement, $returned_type ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
669 | + } |
|
670 | + // If there were an error performing the query then false is returned |
|
671 | + if ( ! empty( $wpdb->last_error ) ) { |
|
672 | + return false; |
|
673 | + } |
|
674 | + |
|
675 | + // Finally |
|
676 | + return $results; |
|
677 | 677 | } |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * |
17 | 17 | * @return bool Return true if supported, false otherwise |
18 | 18 | */ |
19 | -function wl_core_check_relation_predicate_is_supported( $predicate ) { |
|
19 | +function wl_core_check_relation_predicate_is_supported($predicate) { |
|
20 | 20 | |
21 | 21 | return in_array( |
22 | 22 | $predicate, |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | function wl_core_get_validation_rules() { |
39 | 39 | |
40 | 40 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
41 | - return unserialize( WL_CORE_GET_POSTS_VALIDATION_RULES ); |
|
41 | + return unserialize(WL_CORE_GET_POSTS_VALIDATION_RULES); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | function wl_core_get_relation_instances_table_name() { |
50 | 50 | |
51 | 51 | global $wpdb; |
52 | - $table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
52 | + $table_name = $wpdb->prefix.WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
53 | 53 | |
54 | 54 | return $table_name; |
55 | 55 | } |
@@ -66,15 +66,15 @@ discard block |
||
66 | 66 | * @return integer|boolean Return then relation instance ID or false. |
67 | 67 | * @uses $wpdb->replace() to perform the query |
68 | 68 | */ |
69 | -function wl_core_add_relation_instance( $subject_id, $predicate, $object_id, $subject_type = Object_Type_Enum::POST, $object_type = Object_Type_Enum::POST ) { |
|
69 | +function wl_core_add_relation_instance($subject_id, $predicate, $object_id, $subject_type = Object_Type_Enum::POST, $object_type = Object_Type_Enum::POST) { |
|
70 | 70 | |
71 | 71 | // Checks on subject and object |
72 | - if ( ! is_numeric( $subject_id ) || ! is_numeric( $object_id ) ) { |
|
72 | + if ( ! is_numeric($subject_id) || ! is_numeric($object_id)) { |
|
73 | 73 | return false; |
74 | 74 | } |
75 | 75 | |
76 | 76 | // Checks on the given relation |
77 | - if ( ! wl_core_check_relation_predicate_is_supported( $predicate ) ) { |
|
77 | + if ( ! wl_core_check_relation_predicate_is_supported($predicate)) { |
|
78 | 78 | return false; |
79 | 79 | } |
80 | 80 | |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | 'subject_type' => $subject_type, |
99 | 99 | 'object_type' => $object_type, |
100 | 100 | ), |
101 | - array( '%d', '%s', '%d', '%d', '%d' ) |
|
101 | + array('%d', '%s', '%d', '%d', '%d') |
|
102 | 102 | ); |
103 | 103 | |
104 | 104 | /** |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | * |
114 | 114 | * @since 3.16.0 |
115 | 115 | */ |
116 | - do_action( 'wl_relation_added', $subject_id, $predicate, $object_id ); |
|
116 | + do_action('wl_relation_added', $subject_id, $predicate, $object_id); |
|
117 | 117 | |
118 | 118 | // Return record id |
119 | 119 | return $wpdb->insert_id; |
@@ -127,10 +127,10 @@ discard block |
||
127 | 127 | * |
128 | 128 | * @return bool False for failure. True for success. |
129 | 129 | */ |
130 | -function wl_core_delete_relation_instances( $subject_id ) { |
|
130 | +function wl_core_delete_relation_instances($subject_id) { |
|
131 | 131 | |
132 | 132 | // Checks on subject and object |
133 | - if ( ! is_numeric( $subject_id ) ) { |
|
133 | + if ( ! is_numeric($subject_id)) { |
|
134 | 134 | return false; |
135 | 135 | } |
136 | 136 | |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | array( |
150 | 150 | 'subject_id' => $subject_id, |
151 | 151 | ), |
152 | - array( '%d' ) |
|
152 | + array('%d') |
|
153 | 153 | ); |
154 | 154 | |
155 | 155 | /** |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @since 3.16.0 |
163 | 163 | */ |
164 | - do_action( 'wl_relation_deleted', $subject_id ); |
|
164 | + do_action('wl_relation_deleted', $subject_id); |
|
165 | 165 | |
166 | 166 | return true; |
167 | 167 | } |
@@ -177,16 +177,16 @@ discard block |
||
177 | 177 | * 'status' => null |
178 | 178 | * ); |
179 | 179 | */ |
180 | -function wl_core_validate_filters_for_related( $filters ) { |
|
180 | +function wl_core_validate_filters_for_related($filters) { |
|
181 | 181 | |
182 | - if ( ! is_array( $filters ) ) { |
|
182 | + if ( ! is_array($filters)) { |
|
183 | 183 | $filters = array(); |
184 | 184 | } |
185 | 185 | |
186 | - if ( ! isset( $filters['predicate'] ) ) { |
|
186 | + if ( ! isset($filters['predicate'])) { |
|
187 | 187 | $filters['predicate'] = null; |
188 | 188 | } |
189 | - if ( ! isset( $filters['status'] ) ) { |
|
189 | + if ( ! isset($filters['status'])) { |
|
190 | 190 | $filters['status'] = null; |
191 | 191 | } |
192 | 192 | |
@@ -235,12 +235,12 @@ discard block |
||
235 | 235 | * |
236 | 236 | * @deprecated use Wordlift_Relation_Service::get_instance()->get_objects( $subject_id, 'ids', $predicate, $status ); |
237 | 237 | */ |
238 | -function wl_core_get_related_entity_ids( $subject_id, $filters = array() ) { |
|
238 | +function wl_core_get_related_entity_ids($subject_id, $filters = array()) { |
|
239 | 239 | |
240 | - $status = isset( $filters['status'] ) ? $filters['status'] : null; |
|
241 | - $predicate = isset( $filters['predicate'] ) ? $filters['predicate'] : null; |
|
240 | + $status = isset($filters['status']) ? $filters['status'] : null; |
|
241 | + $predicate = isset($filters['predicate']) ? $filters['predicate'] : null; |
|
242 | 242 | |
243 | - return Wordlift_Relation_Service::get_instance()->get_objects( $subject_id, 'ids', $predicate, $status ); |
|
243 | + return Wordlift_Relation_Service::get_instance()->get_objects($subject_id, 'ids', $predicate, $status); |
|
244 | 244 | |
245 | 245 | // $filters = wl_core_validate_filters_for_related( $filters ); |
246 | 246 | // |
@@ -258,13 +258,13 @@ discard block |
||
258 | 258 | * @return array An array of {@link WP_Post}s. |
259 | 259 | * @deprecated use Wordlift_Relation_Service::get_instance()->get_objects() |
260 | 260 | */ |
261 | -function wl_core_get_related_entities( $subject_id, $filters = array() ) { |
|
261 | +function wl_core_get_related_entities($subject_id, $filters = array()) { |
|
262 | 262 | |
263 | - $ids = wl_core_get_related_entity_ids( $subject_id, $filters ); |
|
263 | + $ids = wl_core_get_related_entity_ids($subject_id, $filters); |
|
264 | 264 | |
265 | 265 | return array_map( |
266 | - function ( $item ) { |
|
267 | - return get_post( $item ); |
|
266 | + function($item) { |
|
267 | + return get_post($item); |
|
268 | 268 | }, |
269 | 269 | $ids |
270 | 270 | ); |
@@ -342,14 +342,14 @@ discard block |
||
342 | 342 | * |
343 | 343 | * @deprecated use Wordlift_Relation_Service::get_instance()->get_article_subjects( $object_id, 'ids', $status ); |
344 | 344 | */ |
345 | -function wl_core_get_related_post_ids( $object_id, $filters = array() ) { |
|
345 | +function wl_core_get_related_post_ids($object_id, $filters = array()) { |
|
346 | 346 | |
347 | 347 | $relation_service = Wordlift_Relation_Service::get_instance(); |
348 | 348 | |
349 | - $status = isset( $filters['status'] ) ? $filters['status'] : null; |
|
350 | - $predicate = isset( $filters['predicate'] ) ? $filters['predicate'] : null; |
|
349 | + $status = isset($filters['status']) ? $filters['status'] : null; |
|
350 | + $predicate = isset($filters['predicate']) ? $filters['predicate'] : null; |
|
351 | 351 | |
352 | - return $relation_service->get_article_subjects( $object_id, 'ids', $predicate, $status ); |
|
352 | + return $relation_service->get_article_subjects($object_id, 'ids', $predicate, $status); |
|
353 | 353 | // |
354 | 354 | // $filters = wl_core_validate_filters_for_related( $filters ); |
355 | 355 | // |
@@ -369,13 +369,13 @@ discard block |
||
369 | 369 | * @return array An array of {@link WP_Post}s. |
370 | 370 | * @deprecated use Wordlift_Relation_Service::get_instance()->get_article_subjects() |
371 | 371 | */ |
372 | -function wl_core_get_related_posts( $subject_id, $filters = array() ) { |
|
372 | +function wl_core_get_related_posts($subject_id, $filters = array()) { |
|
373 | 373 | |
374 | - $ids = wl_core_get_related_post_ids( $subject_id, $filters ); |
|
374 | + $ids = wl_core_get_related_post_ids($subject_id, $filters); |
|
375 | 375 | |
376 | 376 | return array_map( |
377 | - function ( $item ) { |
|
378 | - return get_post( $item ); |
|
377 | + function($item) { |
|
378 | + return get_post($item); |
|
379 | 379 | }, |
380 | 380 | $ids |
381 | 381 | ); |
@@ -462,7 +462,7 @@ discard block |
||
462 | 462 | * |
463 | 463 | * @return string|false String representing a sql statement, or false in case of error |
464 | 464 | */ |
465 | -function wl_core_sql_query_builder( $args ) { |
|
465 | +function wl_core_sql_query_builder($args) { |
|
466 | 466 | |
467 | 467 | // Prepare interaction with db |
468 | 468 | global $wpdb; |
@@ -478,7 +478,7 @@ discard block |
||
478 | 478 | // Since we want Find only articles, based on the entity type, we need |
479 | 479 | // to figure out the relevant sql statements to add to the join and where |
480 | 480 | // parts. |
481 | - if ( 'entity' === $args['post_type'] ) { |
|
481 | + if ('entity' === $args['post_type']) { |
|
482 | 482 | $tax_query = array( |
483 | 483 | 'relation' => 'AND', |
484 | 484 | array( |
@@ -508,12 +508,12 @@ discard block |
||
508 | 508 | } |
509 | 509 | |
510 | 510 | // Use "p" as the table to match the initial join. |
511 | - $tax_sql = get_tax_sql( $tax_query, 'p', 'ID' ); |
|
511 | + $tax_sql = get_tax_sql($tax_query, 'p', 'ID'); |
|
512 | 512 | |
513 | 513 | // Sql Action |
514 | 514 | $sql = 'SELECT '; |
515 | 515 | // Determine what has to be returned depending on 'get' argument value |
516 | - switch ( $args['get'] ) { |
|
516 | + switch ($args['get']) { |
|
517 | 517 | case 'posts': |
518 | 518 | $sql .= 'p.*'; |
519 | 519 | break; |
@@ -523,20 +523,20 @@ discard block |
||
523 | 523 | } |
524 | 524 | |
525 | 525 | // If we look for posts related as objects the JOIN has to be done with the object_id column and viceversa |
526 | - $join_column = $args['as'] . '_id'; |
|
526 | + $join_column = $args['as'].'_id'; |
|
527 | 527 | |
528 | 528 | $sql .= " FROM $wpdb->posts as p JOIN $table_name as r ON p.id = r.$join_column"; |
529 | 529 | |
530 | 530 | // Changing left join generate by the tax query into an inner since the term relationship has to exist. |
531 | - $sql .= str_replace( 'LEFT JOIN', 'INNER JOIN', $tax_sql['join'] ); |
|
531 | + $sql .= str_replace('LEFT JOIN', 'INNER JOIN', $tax_sql['join']); |
|
532 | 532 | |
533 | 533 | // Sql add post type filter |
534 | 534 | $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
535 | - $sql .= " AND p.post_type IN ('" . join( "', '", esc_sql( $post_types ) ) . "') AND"; |
|
535 | + $sql .= " AND p.post_type IN ('".join("', '", esc_sql($post_types))."') AND"; |
|
536 | 536 | |
537 | 537 | // Sql add post status filter |
538 | - if ( isset( $args['post_status'] ) && $args['post_status'] !== null ) { |
|
539 | - $sql .= $wpdb->prepare( ' p.post_status = %s AND', $args['post_status'] ); |
|
538 | + if (isset($args['post_status']) && $args['post_status'] !== null) { |
|
539 | + $sql .= $wpdb->prepare(' p.post_status = %s AND', $args['post_status']); |
|
540 | 540 | } |
541 | 541 | |
542 | 542 | // Add filtering conditions |
@@ -545,30 +545,30 @@ discard block |
||
545 | 545 | // If we look for posts related as subject this means that |
546 | 546 | // related_to is reference for an object: object_id is the filtering column |
547 | 547 | |
548 | - $filtering_column = ( 'object' === $args['as'] ) ? 'subject_id' : 'object_id'; |
|
548 | + $filtering_column = ('object' === $args['as']) ? 'subject_id' : 'object_id'; |
|
549 | 549 | |
550 | - if ( isset( $args['related_to'] ) ) { |
|
550 | + if (isset($args['related_to'])) { |
|
551 | 551 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
552 | - $sql .= $wpdb->prepare( " r.$filtering_column = %d", $args['related_to'] ); |
|
552 | + $sql .= $wpdb->prepare(" r.$filtering_column = %d", $args['related_to']); |
|
553 | 553 | } |
554 | - if ( isset( $args['related_to'] ) && isset( $args['related_to__in'] ) ) { |
|
554 | + if (isset($args['related_to']) && isset($args['related_to__in'])) { |
|
555 | 555 | $sql .= ' AND'; |
556 | 556 | } |
557 | - if ( isset( $args['related_to__in'] ) ) { |
|
558 | - $sql .= " r.$filtering_column IN (" . implode( ',', $args['related_to__in'] ) . ')'; |
|
557 | + if (isset($args['related_to__in'])) { |
|
558 | + $sql .= " r.$filtering_column IN (".implode(',', $args['related_to__in']).')'; |
|
559 | 559 | // The IDs used for filtering shouldn't be in the results. |
560 | - $sql .= ' AND p.ID NOT IN (' . implode( ',', $args['related_to__in'] ) . ')'; |
|
560 | + $sql .= ' AND p.ID NOT IN ('.implode(',', $args['related_to__in']).')'; |
|
561 | 561 | } |
562 | - if ( isset( $args['post__not_in'] ) ) { |
|
563 | - $sql .= ' AND r.' . $args['as'] . '_id NOT IN (' . implode( ',', $args['post__not_in'] ) . ')'; |
|
562 | + if (isset($args['post__not_in'])) { |
|
563 | + $sql .= ' AND r.'.$args['as'].'_id NOT IN ('.implode(',', $args['post__not_in']).')'; |
|
564 | 564 | } |
565 | - if ( isset( $args['post__in'] ) ) { |
|
566 | - $sql .= ' AND r.' . $args['as'] . '_id IN (' . implode( ',', $args['post__in'] ) . ')'; |
|
565 | + if (isset($args['post__in'])) { |
|
566 | + $sql .= ' AND r.'.$args['as'].'_id IN ('.implode(',', $args['post__in']).')'; |
|
567 | 567 | } |
568 | 568 | // Add predicate filter if required. |
569 | - if ( isset( $args['with_predicate'] ) ) { |
|
569 | + if (isset($args['with_predicate'])) { |
|
570 | 570 | // Sql Inner Join clause. |
571 | - $sql .= $wpdb->prepare( ' AND r.predicate = %s', $args['with_predicate'] ); |
|
571 | + $sql .= $wpdb->prepare(' AND r.predicate = %s', $args['with_predicate']); |
|
572 | 572 | } |
573 | 573 | |
574 | 574 | // Add the taxonomy related sql. |
@@ -579,9 +579,9 @@ discard block |
||
579 | 579 | $sql .= ' GROUP BY p.id'; |
580 | 580 | |
581 | 581 | // @todo: how does `first` represent the limit? |
582 | - if ( isset( $args['first'] ) && is_numeric( $args['first'] ) ) { |
|
582 | + if (isset($args['first']) && is_numeric($args['first'])) { |
|
583 | 583 | // Sql Inner Join clause. |
584 | - $sql .= $wpdb->prepare( ' LIMIT %d', $args['first'] ); |
|
584 | + $sql .= $wpdb->prepare(' LIMIT %d', $args['first']); |
|
585 | 585 | } |
586 | 586 | // Close sql statement |
587 | 587 | $sql .= ';'; |
@@ -603,7 +603,7 @@ discard block |
||
603 | 603 | * |
604 | 604 | * @uses wl_core_sql_query_builder() to compose the sql statement |
605 | 605 | */ |
606 | -function wl_core_get_posts( $args, $returned_type = OBJECT ) { |
|
606 | +function wl_core_get_posts($args, $returned_type = OBJECT) { |
|
607 | 607 | |
608 | 608 | // Merge given args with defaults args value |
609 | 609 | $args = array_merge( |
@@ -619,10 +619,10 @@ discard block |
||
619 | 619 | |
620 | 620 | // Arguments validation rules |
621 | 621 | // At least one between related_to and related_to__in has to be set |
622 | - if ( ! isset( $args['related_to'] ) && ! isset( $args['related_to__in'] ) ) { |
|
622 | + if ( ! isset($args['related_to']) && ! isset($args['related_to__in'])) { |
|
623 | 623 | return false; |
624 | 624 | } |
625 | - if ( isset( $args['related_to'] ) && ! is_numeric( $args['related_to'] ) ) { |
|
625 | + if (isset($args['related_to']) && ! is_numeric($args['related_to'])) { |
|
626 | 626 | return false; |
627 | 627 | } |
628 | 628 | |
@@ -636,18 +636,18 @@ discard block |
||
636 | 636 | 'related_to__in', |
637 | 637 | ) as $option_name |
638 | 638 | ) { |
639 | - if ( isset( $args[ $option_name ] ) ) { |
|
640 | - if ( ! is_array( $args[ $option_name ] ) || 0 === count( array_filter( $args[ $option_name ], 'is_numeric' ) ) ) { |
|
639 | + if (isset($args[$option_name])) { |
|
640 | + if ( ! is_array($args[$option_name]) || 0 === count(array_filter($args[$option_name], 'is_numeric'))) { |
|
641 | 641 | return false; |
642 | 642 | } |
643 | 643 | // Sanitize value removing non numeric values from the array |
644 | - $args[ $option_name ] = array_filter( $args[ $option_name ], 'is_numeric' ); |
|
644 | + $args[$option_name] = array_filter($args[$option_name], 'is_numeric'); |
|
645 | 645 | } |
646 | 646 | } |
647 | 647 | // Performing validation rules |
648 | - foreach ( wl_core_get_validation_rules() as $option_name => $accepted_values ) { |
|
649 | - if ( isset( $args[ $option_name ] ) && ( $args[ $option_name ] ) !== null ) { |
|
650 | - if ( ! in_array( $args[ $option_name ], $accepted_values, true ) ) { |
|
648 | + foreach (wl_core_get_validation_rules() as $option_name => $accepted_values) { |
|
649 | + if (isset($args[$option_name]) && ($args[$option_name]) !== null) { |
|
650 | + if ( ! in_array($args[$option_name], $accepted_values, true)) { |
|
651 | 651 | return false; |
652 | 652 | } |
653 | 653 | } |
@@ -657,18 +657,18 @@ discard block |
||
657 | 657 | global $wpdb; |
658 | 658 | |
659 | 659 | // Build sql statement with given arguments |
660 | - $sql_statement = wl_core_sql_query_builder( $args ); |
|
660 | + $sql_statement = wl_core_sql_query_builder($args); |
|
661 | 661 | |
662 | 662 | // If ids are required, returns a one-dimensional array containing ids. |
663 | 663 | // Otherwise an array of associative arrays representing the post | relation object |
664 | - if ( 'post_ids' === $args['get'] ) { |
|
664 | + if ('post_ids' === $args['get']) { |
|
665 | 665 | // See https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column |
666 | - $results = $wpdb->get_col( $sql_statement ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
666 | + $results = $wpdb->get_col($sql_statement); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
667 | 667 | } else { |
668 | - $results = $wpdb->get_results( $sql_statement, $returned_type ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
668 | + $results = $wpdb->get_results($sql_statement, $returned_type); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
669 | 669 | } |
670 | 670 | // If there were an error performing the query then false is returned |
671 | - if ( ! empty( $wpdb->last_error ) ) { |
|
671 | + if ( ! empty($wpdb->last_error)) { |
|
672 | 672 | return false; |
673 | 673 | } |
674 | 674 |
@@ -42,375 +42,375 @@ discard block |
||
42 | 42 | */ |
43 | 43 | class ClassLoader { |
44 | 44 | |
45 | - // PSR-4 |
|
46 | - private $prefixLengthsPsr4 = array(); |
|
47 | - private $prefixDirsPsr4 = array(); |
|
48 | - private $fallbackDirsPsr4 = array(); |
|
49 | - |
|
50 | - // PSR-0 |
|
51 | - private $prefixesPsr0 = array(); |
|
52 | - private $fallbackDirsPsr0 = array(); |
|
53 | - |
|
54 | - private $useIncludePath = false; |
|
55 | - private $classMap = array(); |
|
56 | - private $classMapAuthoritative = false; |
|
57 | - private $missingClasses = array(); |
|
58 | - private $apcuPrefix; |
|
59 | - |
|
60 | - public function getPrefixes() { |
|
61 | - if ( ! empty( $this->prefixesPsr0 ) ) { |
|
62 | - return call_user_func_array( 'array_merge', array_values( $this->prefixesPsr0 ) ); |
|
63 | - } |
|
64 | - |
|
65 | - return array(); |
|
66 | - } |
|
67 | - |
|
68 | - public function getPrefixesPsr4() { |
|
69 | - return $this->prefixDirsPsr4; |
|
70 | - } |
|
71 | - |
|
72 | - public function getFallbackDirs() { |
|
73 | - return $this->fallbackDirsPsr0; |
|
74 | - } |
|
75 | - |
|
76 | - public function getFallbackDirsPsr4() { |
|
77 | - return $this->fallbackDirsPsr4; |
|
78 | - } |
|
79 | - |
|
80 | - public function getClassMap() { |
|
81 | - return $this->classMap; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @param array $classMap Class to filename map |
|
86 | - */ |
|
87 | - public function addClassMap( array $classMap ) { |
|
88 | - if ( $this->classMap ) { |
|
89 | - $this->classMap = array_merge( $this->classMap, $classMap ); |
|
90 | - } else { |
|
91 | - $this->classMap = $classMap; |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Registers a set of PSR-0 directories for a given prefix, either |
|
97 | - * appending or prepending to the ones previously set for this prefix. |
|
98 | - * |
|
99 | - * @param string $prefix The prefix |
|
100 | - * @param array|string $paths The PSR-0 root directories |
|
101 | - * @param bool $prepend Whether to prepend the directories |
|
102 | - */ |
|
103 | - public function add( $prefix, $paths, $prepend = false ) { |
|
104 | - if ( ! $prefix ) { |
|
105 | - if ( $prepend ) { |
|
106 | - $this->fallbackDirsPsr0 = array_merge( |
|
107 | - (array) $paths, |
|
108 | - $this->fallbackDirsPsr0 |
|
109 | - ); |
|
110 | - } else { |
|
111 | - $this->fallbackDirsPsr0 = array_merge( |
|
112 | - $this->fallbackDirsPsr0, |
|
113 | - (array) $paths |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - return; |
|
118 | - } |
|
119 | - |
|
120 | - $first = $prefix[0]; |
|
121 | - if ( ! isset( $this->prefixesPsr0[ $first ][ $prefix ] ) ) { |
|
122 | - $this->prefixesPsr0[ $first ][ $prefix ] = (array) $paths; |
|
123 | - |
|
124 | - return; |
|
125 | - } |
|
126 | - if ( $prepend ) { |
|
127 | - $this->prefixesPsr0[ $first ][ $prefix ] = array_merge( |
|
128 | - (array) $paths, |
|
129 | - $this->prefixesPsr0[ $first ][ $prefix ] |
|
130 | - ); |
|
131 | - } else { |
|
132 | - $this->prefixesPsr0[ $first ][ $prefix ] = array_merge( |
|
133 | - $this->prefixesPsr0[ $first ][ $prefix ], |
|
134 | - (array) $paths |
|
135 | - ); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Registers a set of PSR-4 directories for a given namespace, either |
|
141 | - * appending or prepending to the ones previously set for this namespace. |
|
142 | - * |
|
143 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
144 | - * @param array|string $paths The PSR-4 base directories |
|
145 | - * @param bool $prepend Whether to prepend the directories |
|
146 | - * |
|
147 | - * @throws \InvalidArgumentException |
|
148 | - */ |
|
149 | - public function addPsr4( $prefix, $paths, $prepend = false ) { |
|
150 | - if ( ! $prefix ) { |
|
151 | - // Register directories for the root namespace. |
|
152 | - if ( $prepend ) { |
|
153 | - $this->fallbackDirsPsr4 = array_merge( |
|
154 | - (array) $paths, |
|
155 | - $this->fallbackDirsPsr4 |
|
156 | - ); |
|
157 | - } else { |
|
158 | - $this->fallbackDirsPsr4 = array_merge( |
|
159 | - $this->fallbackDirsPsr4, |
|
160 | - (array) $paths |
|
161 | - ); |
|
162 | - } |
|
163 | - } elseif ( ! isset( $this->prefixDirsPsr4[ $prefix ] ) ) { |
|
164 | - // Register directories for a new namespace. |
|
165 | - $length = strlen( $prefix ); |
|
166 | - if ( '\\' !== $prefix[ $length - 1 ] ) { |
|
167 | - throw new \InvalidArgumentException( 'A non-empty PSR-4 prefix must end with a namespace separator.' ); |
|
168 | - } |
|
169 | - $this->prefixLengthsPsr4[ $prefix[0] ][ $prefix ] = $length; |
|
170 | - $this->prefixDirsPsr4[ $prefix ] = (array) $paths; |
|
171 | - } elseif ( $prepend ) { |
|
172 | - // Prepend directories for an already registered namespace. |
|
173 | - $this->prefixDirsPsr4[ $prefix ] = array_merge( |
|
174 | - (array) $paths, |
|
175 | - $this->prefixDirsPsr4[ $prefix ] |
|
176 | - ); |
|
177 | - } else { |
|
178 | - // Append directories for an already registered namespace. |
|
179 | - $this->prefixDirsPsr4[ $prefix ] = array_merge( |
|
180 | - $this->prefixDirsPsr4[ $prefix ], |
|
181 | - (array) $paths |
|
182 | - ); |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Registers a set of PSR-0 directories for a given prefix, |
|
188 | - * replacing any others previously set for this prefix. |
|
189 | - * |
|
190 | - * @param string $prefix The prefix |
|
191 | - * @param array|string $paths The PSR-0 base directories |
|
192 | - */ |
|
193 | - public function set( $prefix, $paths ) { |
|
194 | - if ( ! $prefix ) { |
|
195 | - $this->fallbackDirsPsr0 = (array) $paths; |
|
196 | - } else { |
|
197 | - $this->prefixesPsr0[ $prefix[0] ][ $prefix ] = (array) $paths; |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Registers a set of PSR-4 directories for a given namespace, |
|
203 | - * replacing any others previously set for this namespace. |
|
204 | - * |
|
205 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
206 | - * @param array|string $paths The PSR-4 base directories |
|
207 | - * |
|
208 | - * @throws \InvalidArgumentException |
|
209 | - */ |
|
210 | - public function setPsr4( $prefix, $paths ) { |
|
211 | - if ( ! $prefix ) { |
|
212 | - $this->fallbackDirsPsr4 = (array) $paths; |
|
213 | - } else { |
|
214 | - $length = strlen( $prefix ); |
|
215 | - if ( '\\' !== $prefix[ $length - 1 ] ) { |
|
216 | - throw new \InvalidArgumentException( 'A non-empty PSR-4 prefix must end with a namespace separator.' ); |
|
217 | - } |
|
218 | - $this->prefixLengthsPsr4[ $prefix[0] ][ $prefix ] = $length; |
|
219 | - $this->prefixDirsPsr4[ $prefix ] = (array) $paths; |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Turns on searching the include path for class files. |
|
225 | - * |
|
226 | - * @param bool $useIncludePath |
|
227 | - */ |
|
228 | - public function setUseIncludePath( $useIncludePath ) { |
|
229 | - $this->useIncludePath = $useIncludePath; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Can be used to check if the autoloader uses the include path to check |
|
234 | - * for classes. |
|
235 | - * |
|
236 | - * @return bool |
|
237 | - */ |
|
238 | - public function getUseIncludePath() { |
|
239 | - return $this->useIncludePath; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Turns off searching the prefix and fallback directories for classes |
|
244 | - * that have not been registered with the class map. |
|
245 | - * |
|
246 | - * @param bool $classMapAuthoritative |
|
247 | - */ |
|
248 | - public function setClassMapAuthoritative( $classMapAuthoritative ) { |
|
249 | - $this->classMapAuthoritative = $classMapAuthoritative; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Should class lookup fail if not found in the current class map? |
|
254 | - * |
|
255 | - * @return bool |
|
256 | - */ |
|
257 | - public function isClassMapAuthoritative() { |
|
258 | - return $this->classMapAuthoritative; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
263 | - * |
|
264 | - * @param string|null $apcuPrefix |
|
265 | - */ |
|
266 | - public function setApcuPrefix( $apcuPrefix ) { |
|
267 | - $this->apcuPrefix = function_exists( 'apcu_fetch' ) && filter_var( ini_get( 'apc.enabled' ), FILTER_VALIDATE_BOOLEAN ) ? $apcuPrefix : null; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * The APCu prefix in use, or null if APCu caching is not enabled. |
|
272 | - * |
|
273 | - * @return string|null |
|
274 | - */ |
|
275 | - public function getApcuPrefix() { |
|
276 | - return $this->apcuPrefix; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Registers this instance as an autoloader. |
|
281 | - * |
|
282 | - * @param bool $prepend Whether to prepend the autoloader or not |
|
283 | - */ |
|
284 | - public function register( $prepend = false ) { |
|
285 | - spl_autoload_register( array( $this, 'loadClass' ), true, $prepend ); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Unregisters this instance as an autoloader. |
|
290 | - */ |
|
291 | - public function unregister() { |
|
292 | - spl_autoload_unregister( array( $this, 'loadClass' ) ); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Loads the given class or interface. |
|
297 | - * |
|
298 | - * @param string $class The name of the class |
|
299 | - * @return bool|null True if loaded, null otherwise |
|
300 | - */ |
|
301 | - public function loadClass( $class ) { |
|
302 | - if ( $file = $this->findFile( $class ) ) { |
|
303 | - includeFile( $file ); |
|
304 | - |
|
305 | - return true; |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Finds the path to the file where the class is defined. |
|
311 | - * |
|
312 | - * @param string $class The name of the class |
|
313 | - * |
|
314 | - * @return string|false The path if found, false otherwise |
|
315 | - */ |
|
316 | - public function findFile( $class ) { |
|
317 | - // class map lookup |
|
318 | - if ( isset( $this->classMap[ $class ] ) ) { |
|
319 | - return $this->classMap[ $class ]; |
|
320 | - } |
|
321 | - if ( $this->classMapAuthoritative || isset( $this->missingClasses[ $class ] ) ) { |
|
322 | - return false; |
|
323 | - } |
|
324 | - if ( null !== $this->apcuPrefix ) { |
|
325 | - $file = apcu_fetch( $this->apcuPrefix . $class, $hit ); |
|
326 | - if ( $hit ) { |
|
327 | - return $file; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - $file = $this->findFileWithExtension( $class, '.php' ); |
|
332 | - |
|
333 | - // Search for Hack files if we are running on HHVM |
|
334 | - if ( false === $file && defined( 'HHVM_VERSION' ) ) { |
|
335 | - $file = $this->findFileWithExtension( $class, '.hh' ); |
|
336 | - } |
|
337 | - |
|
338 | - if ( null !== $this->apcuPrefix ) { |
|
339 | - apcu_add( $this->apcuPrefix . $class, $file ); |
|
340 | - } |
|
341 | - |
|
342 | - if ( false === $file ) { |
|
343 | - // Remember that this class does not exist. |
|
344 | - $this->missingClasses[ $class ] = true; |
|
345 | - } |
|
346 | - |
|
347 | - return $file; |
|
348 | - } |
|
349 | - |
|
350 | - private function findFileWithExtension( $class, $ext ) { |
|
351 | - // PSR-4 lookup |
|
352 | - $logicalPathPsr4 = strtr( $class, '\\', DIRECTORY_SEPARATOR ) . $ext; |
|
353 | - |
|
354 | - $first = $class[0]; |
|
355 | - if ( isset( $this->prefixLengthsPsr4[ $first ] ) ) { |
|
356 | - $subPath = $class; |
|
357 | - while ( false !== $lastPos = strrpos( $subPath, '\\' ) ) { |
|
358 | - $subPath = substr( $subPath, 0, $lastPos ); |
|
359 | - $search = $subPath . '\\'; |
|
360 | - if ( isset( $this->prefixDirsPsr4[ $search ] ) ) { |
|
361 | - $pathEnd = DIRECTORY_SEPARATOR . substr( $logicalPathPsr4, $lastPos + 1 ); |
|
362 | - foreach ( $this->prefixDirsPsr4[ $search ] as $dir ) { |
|
363 | - if ( file_exists( $file = $dir . $pathEnd ) ) { |
|
364 | - return $file; |
|
365 | - } |
|
366 | - } |
|
367 | - } |
|
368 | - } |
|
369 | - } |
|
370 | - |
|
371 | - // PSR-4 fallback dirs |
|
372 | - foreach ( $this->fallbackDirsPsr4 as $dir ) { |
|
373 | - if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4 ) ) { |
|
374 | - return $file; |
|
375 | - } |
|
376 | - } |
|
377 | - |
|
378 | - // PSR-0 lookup |
|
379 | - if ( false !== $pos = strrpos( $class, '\\' ) ) { |
|
380 | - // namespaced class name |
|
381 | - $logicalPathPsr0 = substr( $logicalPathPsr4, 0, $pos + 1 ) |
|
382 | - . strtr( substr( $logicalPathPsr4, $pos + 1 ), '_', DIRECTORY_SEPARATOR ); |
|
383 | - } else { |
|
384 | - // PEAR-like class name |
|
385 | - $logicalPathPsr0 = strtr( $class, '_', DIRECTORY_SEPARATOR ) . $ext; |
|
386 | - } |
|
387 | - |
|
388 | - if ( isset( $this->prefixesPsr0[ $first ] ) ) { |
|
389 | - foreach ( $this->prefixesPsr0[ $first ] as $prefix => $dirs ) { |
|
390 | - if ( 0 === strpos( $class, $prefix ) ) { |
|
391 | - foreach ( $dirs as $dir ) { |
|
392 | - if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0 ) ) { |
|
393 | - return $file; |
|
394 | - } |
|
395 | - } |
|
396 | - } |
|
397 | - } |
|
398 | - } |
|
399 | - |
|
400 | - // PSR-0 fallback dirs |
|
401 | - foreach ( $this->fallbackDirsPsr0 as $dir ) { |
|
402 | - if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0 ) ) { |
|
403 | - return $file; |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - // PSR-0 include paths. |
|
408 | - if ( $this->useIncludePath && $file = stream_resolve_include_path( $logicalPathPsr0 ) ) { |
|
409 | - return $file; |
|
410 | - } |
|
411 | - |
|
412 | - return false; |
|
413 | - } |
|
45 | + // PSR-4 |
|
46 | + private $prefixLengthsPsr4 = array(); |
|
47 | + private $prefixDirsPsr4 = array(); |
|
48 | + private $fallbackDirsPsr4 = array(); |
|
49 | + |
|
50 | + // PSR-0 |
|
51 | + private $prefixesPsr0 = array(); |
|
52 | + private $fallbackDirsPsr0 = array(); |
|
53 | + |
|
54 | + private $useIncludePath = false; |
|
55 | + private $classMap = array(); |
|
56 | + private $classMapAuthoritative = false; |
|
57 | + private $missingClasses = array(); |
|
58 | + private $apcuPrefix; |
|
59 | + |
|
60 | + public function getPrefixes() { |
|
61 | + if ( ! empty( $this->prefixesPsr0 ) ) { |
|
62 | + return call_user_func_array( 'array_merge', array_values( $this->prefixesPsr0 ) ); |
|
63 | + } |
|
64 | + |
|
65 | + return array(); |
|
66 | + } |
|
67 | + |
|
68 | + public function getPrefixesPsr4() { |
|
69 | + return $this->prefixDirsPsr4; |
|
70 | + } |
|
71 | + |
|
72 | + public function getFallbackDirs() { |
|
73 | + return $this->fallbackDirsPsr0; |
|
74 | + } |
|
75 | + |
|
76 | + public function getFallbackDirsPsr4() { |
|
77 | + return $this->fallbackDirsPsr4; |
|
78 | + } |
|
79 | + |
|
80 | + public function getClassMap() { |
|
81 | + return $this->classMap; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @param array $classMap Class to filename map |
|
86 | + */ |
|
87 | + public function addClassMap( array $classMap ) { |
|
88 | + if ( $this->classMap ) { |
|
89 | + $this->classMap = array_merge( $this->classMap, $classMap ); |
|
90 | + } else { |
|
91 | + $this->classMap = $classMap; |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Registers a set of PSR-0 directories for a given prefix, either |
|
97 | + * appending or prepending to the ones previously set for this prefix. |
|
98 | + * |
|
99 | + * @param string $prefix The prefix |
|
100 | + * @param array|string $paths The PSR-0 root directories |
|
101 | + * @param bool $prepend Whether to prepend the directories |
|
102 | + */ |
|
103 | + public function add( $prefix, $paths, $prepend = false ) { |
|
104 | + if ( ! $prefix ) { |
|
105 | + if ( $prepend ) { |
|
106 | + $this->fallbackDirsPsr0 = array_merge( |
|
107 | + (array) $paths, |
|
108 | + $this->fallbackDirsPsr0 |
|
109 | + ); |
|
110 | + } else { |
|
111 | + $this->fallbackDirsPsr0 = array_merge( |
|
112 | + $this->fallbackDirsPsr0, |
|
113 | + (array) $paths |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + return; |
|
118 | + } |
|
119 | + |
|
120 | + $first = $prefix[0]; |
|
121 | + if ( ! isset( $this->prefixesPsr0[ $first ][ $prefix ] ) ) { |
|
122 | + $this->prefixesPsr0[ $first ][ $prefix ] = (array) $paths; |
|
123 | + |
|
124 | + return; |
|
125 | + } |
|
126 | + if ( $prepend ) { |
|
127 | + $this->prefixesPsr0[ $first ][ $prefix ] = array_merge( |
|
128 | + (array) $paths, |
|
129 | + $this->prefixesPsr0[ $first ][ $prefix ] |
|
130 | + ); |
|
131 | + } else { |
|
132 | + $this->prefixesPsr0[ $first ][ $prefix ] = array_merge( |
|
133 | + $this->prefixesPsr0[ $first ][ $prefix ], |
|
134 | + (array) $paths |
|
135 | + ); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Registers a set of PSR-4 directories for a given namespace, either |
|
141 | + * appending or prepending to the ones previously set for this namespace. |
|
142 | + * |
|
143 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
144 | + * @param array|string $paths The PSR-4 base directories |
|
145 | + * @param bool $prepend Whether to prepend the directories |
|
146 | + * |
|
147 | + * @throws \InvalidArgumentException |
|
148 | + */ |
|
149 | + public function addPsr4( $prefix, $paths, $prepend = false ) { |
|
150 | + if ( ! $prefix ) { |
|
151 | + // Register directories for the root namespace. |
|
152 | + if ( $prepend ) { |
|
153 | + $this->fallbackDirsPsr4 = array_merge( |
|
154 | + (array) $paths, |
|
155 | + $this->fallbackDirsPsr4 |
|
156 | + ); |
|
157 | + } else { |
|
158 | + $this->fallbackDirsPsr4 = array_merge( |
|
159 | + $this->fallbackDirsPsr4, |
|
160 | + (array) $paths |
|
161 | + ); |
|
162 | + } |
|
163 | + } elseif ( ! isset( $this->prefixDirsPsr4[ $prefix ] ) ) { |
|
164 | + // Register directories for a new namespace. |
|
165 | + $length = strlen( $prefix ); |
|
166 | + if ( '\\' !== $prefix[ $length - 1 ] ) { |
|
167 | + throw new \InvalidArgumentException( 'A non-empty PSR-4 prefix must end with a namespace separator.' ); |
|
168 | + } |
|
169 | + $this->prefixLengthsPsr4[ $prefix[0] ][ $prefix ] = $length; |
|
170 | + $this->prefixDirsPsr4[ $prefix ] = (array) $paths; |
|
171 | + } elseif ( $prepend ) { |
|
172 | + // Prepend directories for an already registered namespace. |
|
173 | + $this->prefixDirsPsr4[ $prefix ] = array_merge( |
|
174 | + (array) $paths, |
|
175 | + $this->prefixDirsPsr4[ $prefix ] |
|
176 | + ); |
|
177 | + } else { |
|
178 | + // Append directories for an already registered namespace. |
|
179 | + $this->prefixDirsPsr4[ $prefix ] = array_merge( |
|
180 | + $this->prefixDirsPsr4[ $prefix ], |
|
181 | + (array) $paths |
|
182 | + ); |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Registers a set of PSR-0 directories for a given prefix, |
|
188 | + * replacing any others previously set for this prefix. |
|
189 | + * |
|
190 | + * @param string $prefix The prefix |
|
191 | + * @param array|string $paths The PSR-0 base directories |
|
192 | + */ |
|
193 | + public function set( $prefix, $paths ) { |
|
194 | + if ( ! $prefix ) { |
|
195 | + $this->fallbackDirsPsr0 = (array) $paths; |
|
196 | + } else { |
|
197 | + $this->prefixesPsr0[ $prefix[0] ][ $prefix ] = (array) $paths; |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Registers a set of PSR-4 directories for a given namespace, |
|
203 | + * replacing any others previously set for this namespace. |
|
204 | + * |
|
205 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
206 | + * @param array|string $paths The PSR-4 base directories |
|
207 | + * |
|
208 | + * @throws \InvalidArgumentException |
|
209 | + */ |
|
210 | + public function setPsr4( $prefix, $paths ) { |
|
211 | + if ( ! $prefix ) { |
|
212 | + $this->fallbackDirsPsr4 = (array) $paths; |
|
213 | + } else { |
|
214 | + $length = strlen( $prefix ); |
|
215 | + if ( '\\' !== $prefix[ $length - 1 ] ) { |
|
216 | + throw new \InvalidArgumentException( 'A non-empty PSR-4 prefix must end with a namespace separator.' ); |
|
217 | + } |
|
218 | + $this->prefixLengthsPsr4[ $prefix[0] ][ $prefix ] = $length; |
|
219 | + $this->prefixDirsPsr4[ $prefix ] = (array) $paths; |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Turns on searching the include path for class files. |
|
225 | + * |
|
226 | + * @param bool $useIncludePath |
|
227 | + */ |
|
228 | + public function setUseIncludePath( $useIncludePath ) { |
|
229 | + $this->useIncludePath = $useIncludePath; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Can be used to check if the autoloader uses the include path to check |
|
234 | + * for classes. |
|
235 | + * |
|
236 | + * @return bool |
|
237 | + */ |
|
238 | + public function getUseIncludePath() { |
|
239 | + return $this->useIncludePath; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Turns off searching the prefix and fallback directories for classes |
|
244 | + * that have not been registered with the class map. |
|
245 | + * |
|
246 | + * @param bool $classMapAuthoritative |
|
247 | + */ |
|
248 | + public function setClassMapAuthoritative( $classMapAuthoritative ) { |
|
249 | + $this->classMapAuthoritative = $classMapAuthoritative; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Should class lookup fail if not found in the current class map? |
|
254 | + * |
|
255 | + * @return bool |
|
256 | + */ |
|
257 | + public function isClassMapAuthoritative() { |
|
258 | + return $this->classMapAuthoritative; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
263 | + * |
|
264 | + * @param string|null $apcuPrefix |
|
265 | + */ |
|
266 | + public function setApcuPrefix( $apcuPrefix ) { |
|
267 | + $this->apcuPrefix = function_exists( 'apcu_fetch' ) && filter_var( ini_get( 'apc.enabled' ), FILTER_VALIDATE_BOOLEAN ) ? $apcuPrefix : null; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * The APCu prefix in use, or null if APCu caching is not enabled. |
|
272 | + * |
|
273 | + * @return string|null |
|
274 | + */ |
|
275 | + public function getApcuPrefix() { |
|
276 | + return $this->apcuPrefix; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Registers this instance as an autoloader. |
|
281 | + * |
|
282 | + * @param bool $prepend Whether to prepend the autoloader or not |
|
283 | + */ |
|
284 | + public function register( $prepend = false ) { |
|
285 | + spl_autoload_register( array( $this, 'loadClass' ), true, $prepend ); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Unregisters this instance as an autoloader. |
|
290 | + */ |
|
291 | + public function unregister() { |
|
292 | + spl_autoload_unregister( array( $this, 'loadClass' ) ); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Loads the given class or interface. |
|
297 | + * |
|
298 | + * @param string $class The name of the class |
|
299 | + * @return bool|null True if loaded, null otherwise |
|
300 | + */ |
|
301 | + public function loadClass( $class ) { |
|
302 | + if ( $file = $this->findFile( $class ) ) { |
|
303 | + includeFile( $file ); |
|
304 | + |
|
305 | + return true; |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Finds the path to the file where the class is defined. |
|
311 | + * |
|
312 | + * @param string $class The name of the class |
|
313 | + * |
|
314 | + * @return string|false The path if found, false otherwise |
|
315 | + */ |
|
316 | + public function findFile( $class ) { |
|
317 | + // class map lookup |
|
318 | + if ( isset( $this->classMap[ $class ] ) ) { |
|
319 | + return $this->classMap[ $class ]; |
|
320 | + } |
|
321 | + if ( $this->classMapAuthoritative || isset( $this->missingClasses[ $class ] ) ) { |
|
322 | + return false; |
|
323 | + } |
|
324 | + if ( null !== $this->apcuPrefix ) { |
|
325 | + $file = apcu_fetch( $this->apcuPrefix . $class, $hit ); |
|
326 | + if ( $hit ) { |
|
327 | + return $file; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + $file = $this->findFileWithExtension( $class, '.php' ); |
|
332 | + |
|
333 | + // Search for Hack files if we are running on HHVM |
|
334 | + if ( false === $file && defined( 'HHVM_VERSION' ) ) { |
|
335 | + $file = $this->findFileWithExtension( $class, '.hh' ); |
|
336 | + } |
|
337 | + |
|
338 | + if ( null !== $this->apcuPrefix ) { |
|
339 | + apcu_add( $this->apcuPrefix . $class, $file ); |
|
340 | + } |
|
341 | + |
|
342 | + if ( false === $file ) { |
|
343 | + // Remember that this class does not exist. |
|
344 | + $this->missingClasses[ $class ] = true; |
|
345 | + } |
|
346 | + |
|
347 | + return $file; |
|
348 | + } |
|
349 | + |
|
350 | + private function findFileWithExtension( $class, $ext ) { |
|
351 | + // PSR-4 lookup |
|
352 | + $logicalPathPsr4 = strtr( $class, '\\', DIRECTORY_SEPARATOR ) . $ext; |
|
353 | + |
|
354 | + $first = $class[0]; |
|
355 | + if ( isset( $this->prefixLengthsPsr4[ $first ] ) ) { |
|
356 | + $subPath = $class; |
|
357 | + while ( false !== $lastPos = strrpos( $subPath, '\\' ) ) { |
|
358 | + $subPath = substr( $subPath, 0, $lastPos ); |
|
359 | + $search = $subPath . '\\'; |
|
360 | + if ( isset( $this->prefixDirsPsr4[ $search ] ) ) { |
|
361 | + $pathEnd = DIRECTORY_SEPARATOR . substr( $logicalPathPsr4, $lastPos + 1 ); |
|
362 | + foreach ( $this->prefixDirsPsr4[ $search ] as $dir ) { |
|
363 | + if ( file_exists( $file = $dir . $pathEnd ) ) { |
|
364 | + return $file; |
|
365 | + } |
|
366 | + } |
|
367 | + } |
|
368 | + } |
|
369 | + } |
|
370 | + |
|
371 | + // PSR-4 fallback dirs |
|
372 | + foreach ( $this->fallbackDirsPsr4 as $dir ) { |
|
373 | + if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4 ) ) { |
|
374 | + return $file; |
|
375 | + } |
|
376 | + } |
|
377 | + |
|
378 | + // PSR-0 lookup |
|
379 | + if ( false !== $pos = strrpos( $class, '\\' ) ) { |
|
380 | + // namespaced class name |
|
381 | + $logicalPathPsr0 = substr( $logicalPathPsr4, 0, $pos + 1 ) |
|
382 | + . strtr( substr( $logicalPathPsr4, $pos + 1 ), '_', DIRECTORY_SEPARATOR ); |
|
383 | + } else { |
|
384 | + // PEAR-like class name |
|
385 | + $logicalPathPsr0 = strtr( $class, '_', DIRECTORY_SEPARATOR ) . $ext; |
|
386 | + } |
|
387 | + |
|
388 | + if ( isset( $this->prefixesPsr0[ $first ] ) ) { |
|
389 | + foreach ( $this->prefixesPsr0[ $first ] as $prefix => $dirs ) { |
|
390 | + if ( 0 === strpos( $class, $prefix ) ) { |
|
391 | + foreach ( $dirs as $dir ) { |
|
392 | + if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0 ) ) { |
|
393 | + return $file; |
|
394 | + } |
|
395 | + } |
|
396 | + } |
|
397 | + } |
|
398 | + } |
|
399 | + |
|
400 | + // PSR-0 fallback dirs |
|
401 | + foreach ( $this->fallbackDirsPsr0 as $dir ) { |
|
402 | + if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0 ) ) { |
|
403 | + return $file; |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + // PSR-0 include paths. |
|
408 | + if ( $this->useIncludePath && $file = stream_resolve_include_path( $logicalPathPsr0 ) ) { |
|
409 | + return $file; |
|
410 | + } |
|
411 | + |
|
412 | + return false; |
|
413 | + } |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | /** |
@@ -419,5 +419,5 @@ discard block |
||
419 | 419 | * Prevents access to $this/self from included files. |
420 | 420 | */ |
421 | 421 | function includeFile( $file ) { |
422 | - include $file; |
|
422 | + include $file; |
|
423 | 423 | } |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | private $apcuPrefix; |
59 | 59 | |
60 | 60 | public function getPrefixes() { |
61 | - if ( ! empty( $this->prefixesPsr0 ) ) { |
|
62 | - return call_user_func_array( 'array_merge', array_values( $this->prefixesPsr0 ) ); |
|
61 | + if ( ! empty($this->prefixesPsr0)) { |
|
62 | + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | return array(); |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | /** |
85 | 85 | * @param array $classMap Class to filename map |
86 | 86 | */ |
87 | - public function addClassMap( array $classMap ) { |
|
88 | - if ( $this->classMap ) { |
|
89 | - $this->classMap = array_merge( $this->classMap, $classMap ); |
|
87 | + public function addClassMap(array $classMap) { |
|
88 | + if ($this->classMap) { |
|
89 | + $this->classMap = array_merge($this->classMap, $classMap); |
|
90 | 90 | } else { |
91 | 91 | $this->classMap = $classMap; |
92 | 92 | } |
@@ -100,9 +100,9 @@ discard block |
||
100 | 100 | * @param array|string $paths The PSR-0 root directories |
101 | 101 | * @param bool $prepend Whether to prepend the directories |
102 | 102 | */ |
103 | - public function add( $prefix, $paths, $prepend = false ) { |
|
104 | - if ( ! $prefix ) { |
|
105 | - if ( $prepend ) { |
|
103 | + public function add($prefix, $paths, $prepend = false) { |
|
104 | + if ( ! $prefix) { |
|
105 | + if ($prepend) { |
|
106 | 106 | $this->fallbackDirsPsr0 = array_merge( |
107 | 107 | (array) $paths, |
108 | 108 | $this->fallbackDirsPsr0 |
@@ -118,19 +118,19 @@ discard block |
||
118 | 118 | } |
119 | 119 | |
120 | 120 | $first = $prefix[0]; |
121 | - if ( ! isset( $this->prefixesPsr0[ $first ][ $prefix ] ) ) { |
|
122 | - $this->prefixesPsr0[ $first ][ $prefix ] = (array) $paths; |
|
121 | + if ( ! isset($this->prefixesPsr0[$first][$prefix])) { |
|
122 | + $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
123 | 123 | |
124 | 124 | return; |
125 | 125 | } |
126 | - if ( $prepend ) { |
|
127 | - $this->prefixesPsr0[ $first ][ $prefix ] = array_merge( |
|
126 | + if ($prepend) { |
|
127 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
128 | 128 | (array) $paths, |
129 | - $this->prefixesPsr0[ $first ][ $prefix ] |
|
129 | + $this->prefixesPsr0[$first][$prefix] |
|
130 | 130 | ); |
131 | 131 | } else { |
132 | - $this->prefixesPsr0[ $first ][ $prefix ] = array_merge( |
|
133 | - $this->prefixesPsr0[ $first ][ $prefix ], |
|
132 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
133 | + $this->prefixesPsr0[$first][$prefix], |
|
134 | 134 | (array) $paths |
135 | 135 | ); |
136 | 136 | } |
@@ -146,10 +146,10 @@ discard block |
||
146 | 146 | * |
147 | 147 | * @throws \InvalidArgumentException |
148 | 148 | */ |
149 | - public function addPsr4( $prefix, $paths, $prepend = false ) { |
|
150 | - if ( ! $prefix ) { |
|
149 | + public function addPsr4($prefix, $paths, $prepend = false) { |
|
150 | + if ( ! $prefix) { |
|
151 | 151 | // Register directories for the root namespace. |
152 | - if ( $prepend ) { |
|
152 | + if ($prepend) { |
|
153 | 153 | $this->fallbackDirsPsr4 = array_merge( |
154 | 154 | (array) $paths, |
155 | 155 | $this->fallbackDirsPsr4 |
@@ -160,24 +160,24 @@ discard block |
||
160 | 160 | (array) $paths |
161 | 161 | ); |
162 | 162 | } |
163 | - } elseif ( ! isset( $this->prefixDirsPsr4[ $prefix ] ) ) { |
|
163 | + } elseif ( ! isset($this->prefixDirsPsr4[$prefix])) { |
|
164 | 164 | // Register directories for a new namespace. |
165 | - $length = strlen( $prefix ); |
|
166 | - if ( '\\' !== $prefix[ $length - 1 ] ) { |
|
167 | - throw new \InvalidArgumentException( 'A non-empty PSR-4 prefix must end with a namespace separator.' ); |
|
165 | + $length = strlen($prefix); |
|
166 | + if ('\\' !== $prefix[$length - 1]) { |
|
167 | + throw new \InvalidArgumentException('A non-empty PSR-4 prefix must end with a namespace separator.'); |
|
168 | 168 | } |
169 | - $this->prefixLengthsPsr4[ $prefix[0] ][ $prefix ] = $length; |
|
170 | - $this->prefixDirsPsr4[ $prefix ] = (array) $paths; |
|
171 | - } elseif ( $prepend ) { |
|
169 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
170 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
171 | + } elseif ($prepend) { |
|
172 | 172 | // Prepend directories for an already registered namespace. |
173 | - $this->prefixDirsPsr4[ $prefix ] = array_merge( |
|
173 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
174 | 174 | (array) $paths, |
175 | - $this->prefixDirsPsr4[ $prefix ] |
|
175 | + $this->prefixDirsPsr4[$prefix] |
|
176 | 176 | ); |
177 | 177 | } else { |
178 | 178 | // Append directories for an already registered namespace. |
179 | - $this->prefixDirsPsr4[ $prefix ] = array_merge( |
|
180 | - $this->prefixDirsPsr4[ $prefix ], |
|
179 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
180 | + $this->prefixDirsPsr4[$prefix], |
|
181 | 181 | (array) $paths |
182 | 182 | ); |
183 | 183 | } |
@@ -190,11 +190,11 @@ discard block |
||
190 | 190 | * @param string $prefix The prefix |
191 | 191 | * @param array|string $paths The PSR-0 base directories |
192 | 192 | */ |
193 | - public function set( $prefix, $paths ) { |
|
194 | - if ( ! $prefix ) { |
|
193 | + public function set($prefix, $paths) { |
|
194 | + if ( ! $prefix) { |
|
195 | 195 | $this->fallbackDirsPsr0 = (array) $paths; |
196 | 196 | } else { |
197 | - $this->prefixesPsr0[ $prefix[0] ][ $prefix ] = (array) $paths; |
|
197 | + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
198 | 198 | } |
199 | 199 | } |
200 | 200 | |
@@ -207,16 +207,16 @@ discard block |
||
207 | 207 | * |
208 | 208 | * @throws \InvalidArgumentException |
209 | 209 | */ |
210 | - public function setPsr4( $prefix, $paths ) { |
|
211 | - if ( ! $prefix ) { |
|
210 | + public function setPsr4($prefix, $paths) { |
|
211 | + if ( ! $prefix) { |
|
212 | 212 | $this->fallbackDirsPsr4 = (array) $paths; |
213 | 213 | } else { |
214 | - $length = strlen( $prefix ); |
|
215 | - if ( '\\' !== $prefix[ $length - 1 ] ) { |
|
216 | - throw new \InvalidArgumentException( 'A non-empty PSR-4 prefix must end with a namespace separator.' ); |
|
214 | + $length = strlen($prefix); |
|
215 | + if ('\\' !== $prefix[$length - 1]) { |
|
216 | + throw new \InvalidArgumentException('A non-empty PSR-4 prefix must end with a namespace separator.'); |
|
217 | 217 | } |
218 | - $this->prefixLengthsPsr4[ $prefix[0] ][ $prefix ] = $length; |
|
219 | - $this->prefixDirsPsr4[ $prefix ] = (array) $paths; |
|
218 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
219 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
220 | 220 | } |
221 | 221 | } |
222 | 222 | |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | * |
226 | 226 | * @param bool $useIncludePath |
227 | 227 | */ |
228 | - public function setUseIncludePath( $useIncludePath ) { |
|
228 | + public function setUseIncludePath($useIncludePath) { |
|
229 | 229 | $this->useIncludePath = $useIncludePath; |
230 | 230 | } |
231 | 231 | |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | * |
246 | 246 | * @param bool $classMapAuthoritative |
247 | 247 | */ |
248 | - public function setClassMapAuthoritative( $classMapAuthoritative ) { |
|
248 | + public function setClassMapAuthoritative($classMapAuthoritative) { |
|
249 | 249 | $this->classMapAuthoritative = $classMapAuthoritative; |
250 | 250 | } |
251 | 251 | |
@@ -263,8 +263,8 @@ discard block |
||
263 | 263 | * |
264 | 264 | * @param string|null $apcuPrefix |
265 | 265 | */ |
266 | - public function setApcuPrefix( $apcuPrefix ) { |
|
267 | - $this->apcuPrefix = function_exists( 'apcu_fetch' ) && filter_var( ini_get( 'apc.enabled' ), FILTER_VALIDATE_BOOLEAN ) ? $apcuPrefix : null; |
|
266 | + public function setApcuPrefix($apcuPrefix) { |
|
267 | + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | /** |
@@ -281,15 +281,15 @@ discard block |
||
281 | 281 | * |
282 | 282 | * @param bool $prepend Whether to prepend the autoloader or not |
283 | 283 | */ |
284 | - public function register( $prepend = false ) { |
|
285 | - spl_autoload_register( array( $this, 'loadClass' ), true, $prepend ); |
|
284 | + public function register($prepend = false) { |
|
285 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | /** |
289 | 289 | * Unregisters this instance as an autoloader. |
290 | 290 | */ |
291 | 291 | public function unregister() { |
292 | - spl_autoload_unregister( array( $this, 'loadClass' ) ); |
|
292 | + spl_autoload_unregister(array($this, 'loadClass')); |
|
293 | 293 | } |
294 | 294 | |
295 | 295 | /** |
@@ -298,9 +298,9 @@ discard block |
||
298 | 298 | * @param string $class The name of the class |
299 | 299 | * @return bool|null True if loaded, null otherwise |
300 | 300 | */ |
301 | - public function loadClass( $class ) { |
|
302 | - if ( $file = $this->findFile( $class ) ) { |
|
303 | - includeFile( $file ); |
|
301 | + public function loadClass($class) { |
|
302 | + if ($file = $this->findFile($class)) { |
|
303 | + includeFile($file); |
|
304 | 304 | |
305 | 305 | return true; |
306 | 306 | } |
@@ -313,54 +313,54 @@ discard block |
||
313 | 313 | * |
314 | 314 | * @return string|false The path if found, false otherwise |
315 | 315 | */ |
316 | - public function findFile( $class ) { |
|
316 | + public function findFile($class) { |
|
317 | 317 | // class map lookup |
318 | - if ( isset( $this->classMap[ $class ] ) ) { |
|
319 | - return $this->classMap[ $class ]; |
|
318 | + if (isset($this->classMap[$class])) { |
|
319 | + return $this->classMap[$class]; |
|
320 | 320 | } |
321 | - if ( $this->classMapAuthoritative || isset( $this->missingClasses[ $class ] ) ) { |
|
321 | + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
322 | 322 | return false; |
323 | 323 | } |
324 | - if ( null !== $this->apcuPrefix ) { |
|
325 | - $file = apcu_fetch( $this->apcuPrefix . $class, $hit ); |
|
326 | - if ( $hit ) { |
|
324 | + if (null !== $this->apcuPrefix) { |
|
325 | + $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
326 | + if ($hit) { |
|
327 | 327 | return $file; |
328 | 328 | } |
329 | 329 | } |
330 | 330 | |
331 | - $file = $this->findFileWithExtension( $class, '.php' ); |
|
331 | + $file = $this->findFileWithExtension($class, '.php'); |
|
332 | 332 | |
333 | 333 | // Search for Hack files if we are running on HHVM |
334 | - if ( false === $file && defined( 'HHVM_VERSION' ) ) { |
|
335 | - $file = $this->findFileWithExtension( $class, '.hh' ); |
|
334 | + if (false === $file && defined('HHVM_VERSION')) { |
|
335 | + $file = $this->findFileWithExtension($class, '.hh'); |
|
336 | 336 | } |
337 | 337 | |
338 | - if ( null !== $this->apcuPrefix ) { |
|
339 | - apcu_add( $this->apcuPrefix . $class, $file ); |
|
338 | + if (null !== $this->apcuPrefix) { |
|
339 | + apcu_add($this->apcuPrefix.$class, $file); |
|
340 | 340 | } |
341 | 341 | |
342 | - if ( false === $file ) { |
|
342 | + if (false === $file) { |
|
343 | 343 | // Remember that this class does not exist. |
344 | - $this->missingClasses[ $class ] = true; |
|
344 | + $this->missingClasses[$class] = true; |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | return $file; |
348 | 348 | } |
349 | 349 | |
350 | - private function findFileWithExtension( $class, $ext ) { |
|
350 | + private function findFileWithExtension($class, $ext) { |
|
351 | 351 | // PSR-4 lookup |
352 | - $logicalPathPsr4 = strtr( $class, '\\', DIRECTORY_SEPARATOR ) . $ext; |
|
352 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR).$ext; |
|
353 | 353 | |
354 | 354 | $first = $class[0]; |
355 | - if ( isset( $this->prefixLengthsPsr4[ $first ] ) ) { |
|
355 | + if (isset($this->prefixLengthsPsr4[$first])) { |
|
356 | 356 | $subPath = $class; |
357 | - while ( false !== $lastPos = strrpos( $subPath, '\\' ) ) { |
|
358 | - $subPath = substr( $subPath, 0, $lastPos ); |
|
359 | - $search = $subPath . '\\'; |
|
360 | - if ( isset( $this->prefixDirsPsr4[ $search ] ) ) { |
|
361 | - $pathEnd = DIRECTORY_SEPARATOR . substr( $logicalPathPsr4, $lastPos + 1 ); |
|
362 | - foreach ( $this->prefixDirsPsr4[ $search ] as $dir ) { |
|
363 | - if ( file_exists( $file = $dir . $pathEnd ) ) { |
|
357 | + while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
358 | + $subPath = substr($subPath, 0, $lastPos); |
|
359 | + $search = $subPath.'\\'; |
|
360 | + if (isset($this->prefixDirsPsr4[$search])) { |
|
361 | + $pathEnd = DIRECTORY_SEPARATOR.substr($logicalPathPsr4, $lastPos + 1); |
|
362 | + foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
363 | + if (file_exists($file = $dir.$pathEnd)) { |
|
364 | 364 | return $file; |
365 | 365 | } |
366 | 366 | } |
@@ -369,27 +369,27 @@ discard block |
||
369 | 369 | } |
370 | 370 | |
371 | 371 | // PSR-4 fallback dirs |
372 | - foreach ( $this->fallbackDirsPsr4 as $dir ) { |
|
373 | - if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4 ) ) { |
|
372 | + foreach ($this->fallbackDirsPsr4 as $dir) { |
|
373 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr4)) { |
|
374 | 374 | return $file; |
375 | 375 | } |
376 | 376 | } |
377 | 377 | |
378 | 378 | // PSR-0 lookup |
379 | - if ( false !== $pos = strrpos( $class, '\\' ) ) { |
|
379 | + if (false !== $pos = strrpos($class, '\\')) { |
|
380 | 380 | // namespaced class name |
381 | - $logicalPathPsr0 = substr( $logicalPathPsr4, 0, $pos + 1 ) |
|
382 | - . strtr( substr( $logicalPathPsr4, $pos + 1 ), '_', DIRECTORY_SEPARATOR ); |
|
381 | + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
382 | + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
383 | 383 | } else { |
384 | 384 | // PEAR-like class name |
385 | - $logicalPathPsr0 = strtr( $class, '_', DIRECTORY_SEPARATOR ) . $ext; |
|
385 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR).$ext; |
|
386 | 386 | } |
387 | 387 | |
388 | - if ( isset( $this->prefixesPsr0[ $first ] ) ) { |
|
389 | - foreach ( $this->prefixesPsr0[ $first ] as $prefix => $dirs ) { |
|
390 | - if ( 0 === strpos( $class, $prefix ) ) { |
|
391 | - foreach ( $dirs as $dir ) { |
|
392 | - if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0 ) ) { |
|
388 | + if (isset($this->prefixesPsr0[$first])) { |
|
389 | + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
390 | + if (0 === strpos($class, $prefix)) { |
|
391 | + foreach ($dirs as $dir) { |
|
392 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr0)) { |
|
393 | 393 | return $file; |
394 | 394 | } |
395 | 395 | } |
@@ -398,14 +398,14 @@ discard block |
||
398 | 398 | } |
399 | 399 | |
400 | 400 | // PSR-0 fallback dirs |
401 | - foreach ( $this->fallbackDirsPsr0 as $dir ) { |
|
402 | - if ( file_exists( $file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0 ) ) { |
|
401 | + foreach ($this->fallbackDirsPsr0 as $dir) { |
|
402 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr0)) { |
|
403 | 403 | return $file; |
404 | 404 | } |
405 | 405 | } |
406 | 406 | |
407 | 407 | // PSR-0 include paths. |
408 | - if ( $this->useIncludePath && $file = stream_resolve_include_path( $logicalPathPsr0 ) ) { |
|
408 | + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
409 | 409 | return $file; |
410 | 410 | } |
411 | 411 | |
@@ -418,6 +418,6 @@ discard block |
||
418 | 418 | * |
419 | 419 | * Prevents access to $this/self from included files. |
420 | 420 | */ |
421 | -function includeFile( $file ) { |
|
421 | +function includeFile($file) { |
|
422 | 422 | include $file; |
423 | 423 | } |
@@ -2,7 +2,7 @@ |
||
2 | 2 | |
3 | 3 | // autoload_namespaces.php @generated by Composer |
4 | 4 | |
5 | -$vendorDir = dirname( ( __DIR__ ) ); |
|
6 | -$baseDir = dirname( $vendorDir ); |
|
5 | +$vendorDir = dirname((__DIR__)); |
|
6 | +$baseDir = dirname($vendorDir); |
|
7 | 7 | |
8 | 8 | return array(); |
@@ -11,17 +11,17 @@ discard block |
||
11 | 11 | * @since 3.27.6 |
12 | 12 | */ |
13 | 13 | function wl_ajax_analyze_disabled_action() { |
14 | - // adding the below header for debugging purpose. |
|
15 | - if ( ! headers_sent() ) { |
|
16 | - header( 'X-WordLift-Analysis: OFF' ); |
|
17 | - } |
|
18 | - wp_send_json_success( |
|
19 | - array( |
|
20 | - 'entities' => array(), |
|
21 | - 'annotations' => array(), |
|
22 | - 'topics' => array(), |
|
23 | - ) |
|
24 | - ); |
|
14 | + // adding the below header for debugging purpose. |
|
15 | + if ( ! headers_sent() ) { |
|
16 | + header( 'X-WordLift-Analysis: OFF' ); |
|
17 | + } |
|
18 | + wp_send_json_success( |
|
19 | + array( |
|
20 | + 'entities' => array(), |
|
21 | + 'annotations' => array(), |
|
22 | + 'topics' => array(), |
|
23 | + ) |
|
24 | + ); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
@@ -35,16 +35,16 @@ discard block |
||
35 | 35 | */ |
36 | 36 | function wl_ajax_analyze_action() { |
37 | 37 | |
38 | - check_admin_referer( 'wl_analyze' ); |
|
39 | - $data = ''; |
|
38 | + check_admin_referer( 'wl_analyze' ); |
|
39 | + $data = ''; |
|
40 | 40 | |
41 | - if ( isset( $_POST['data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
42 | - // We need to send the data from editor without sanitizing to analysis service. |
|
43 | - $filtered_data = filter_var_array( $_POST, array( 'data' => array( 'flags' => FILTER_UNSAFE_RAW ) ) ); |
|
44 | - $data = $filtered_data['data']; |
|
45 | - } |
|
41 | + if ( isset( $_POST['data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
42 | + // We need to send the data from editor without sanitizing to analysis service. |
|
43 | + $filtered_data = filter_var_array( $_POST, array( 'data' => array( 'flags' => FILTER_UNSAFE_RAW ) ) ); |
|
44 | + $data = $filtered_data['data']; |
|
45 | + } |
|
46 | 46 | |
47 | - wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
47 | + wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
48 | 48 | |
49 | 49 | } |
50 | 50 | |
@@ -66,75 +66,75 @@ discard block |
||
66 | 66 | */ |
67 | 67 | function wl_analyze_content( $data, $content_type ) { |
68 | 68 | |
69 | - $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
70 | - $request_body = json_decode( $data, true ); |
|
71 | - |
|
72 | - $post_id = isset( $_REQUEST['postId'] ) ? intval( $_REQUEST['postId'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
73 | - |
|
74 | - if ( null === $request_body ) { |
|
75 | - /** |
|
76 | - * @since 3.27.7 |
|
77 | - * |
|
78 | - * Conditionally try using stripslashes as $_POST['data'] could be escaped |
|
79 | - */ |
|
80 | - $request_body = json_decode( stripslashes( $data ), true ); |
|
81 | - } |
|
82 | - $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
83 | - $excluded_uris = array_key_exists( 'exclude', $request_body ) ? (array) $request_body['exclude'] : array(); |
|
84 | - $data = wp_json_encode( $request_body ); |
|
85 | - |
|
86 | - // If dataset is not enabled, return a locally prepared response without analysis API. |
|
87 | - if ( ! apply_filters( 'wl_feature__enable__dataset', true ) ) { |
|
88 | - |
|
89 | - return Analysis_Response_Ops_Factory::get_instance() |
|
90 | - ->create( $default_response, $post_id ) |
|
91 | - ->make_entities_local() |
|
92 | - ->add_occurrences( $request_body['content'] ) |
|
93 | - ->add_local_entities() |
|
94 | - ->get_json(); |
|
95 | - } |
|
96 | - |
|
97 | - add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
98 | - |
|
99 | - $json = Analysis_Service_Factory::get_instance( $post_id ) |
|
100 | - ->get_analysis_response( $data, $content_type, $post_id ); |
|
101 | - |
|
102 | - remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
103 | - |
|
104 | - // If it's an error log it. |
|
105 | - if ( is_wp_error( $json ) ) { |
|
106 | - return Analysis_Response_Ops_Factory::get_instance() |
|
107 | - ->create( $default_response, $post_id ) |
|
108 | - ->make_entities_local() |
|
109 | - ->add_occurrences( $request_body['content'] ) |
|
110 | - ->get_json(); |
|
111 | - } |
|
112 | - |
|
113 | - /* |
|
69 | + $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
70 | + $request_body = json_decode( $data, true ); |
|
71 | + |
|
72 | + $post_id = isset( $_REQUEST['postId'] ) ? intval( $_REQUEST['postId'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
73 | + |
|
74 | + if ( null === $request_body ) { |
|
75 | + /** |
|
76 | + * @since 3.27.7 |
|
77 | + * |
|
78 | + * Conditionally try using stripslashes as $_POST['data'] could be escaped |
|
79 | + */ |
|
80 | + $request_body = json_decode( stripslashes( $data ), true ); |
|
81 | + } |
|
82 | + $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
83 | + $excluded_uris = array_key_exists( 'exclude', $request_body ) ? (array) $request_body['exclude'] : array(); |
|
84 | + $data = wp_json_encode( $request_body ); |
|
85 | + |
|
86 | + // If dataset is not enabled, return a locally prepared response without analysis API. |
|
87 | + if ( ! apply_filters( 'wl_feature__enable__dataset', true ) ) { |
|
88 | + |
|
89 | + return Analysis_Response_Ops_Factory::get_instance() |
|
90 | + ->create( $default_response, $post_id ) |
|
91 | + ->make_entities_local() |
|
92 | + ->add_occurrences( $request_body['content'] ) |
|
93 | + ->add_local_entities() |
|
94 | + ->get_json(); |
|
95 | + } |
|
96 | + |
|
97 | + add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
98 | + |
|
99 | + $json = Analysis_Service_Factory::get_instance( $post_id ) |
|
100 | + ->get_analysis_response( $data, $content_type, $post_id ); |
|
101 | + |
|
102 | + remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
103 | + |
|
104 | + // If it's an error log it. |
|
105 | + if ( is_wp_error( $json ) ) { |
|
106 | + return Analysis_Response_Ops_Factory::get_instance() |
|
107 | + ->create( $default_response, $post_id ) |
|
108 | + ->make_entities_local() |
|
109 | + ->add_occurrences( $request_body['content'] ) |
|
110 | + ->get_json(); |
|
111 | + } |
|
112 | + |
|
113 | + /* |
|
114 | 114 | * We pass the response to the Analysis_Response_Ops to ensure that we make remote entities local. |
115 | 115 | * |
116 | 116 | * @see https://github.com/insideout10/wordlift-plugin/issues/944 |
117 | 117 | * @since 3.21.5 |
118 | 118 | */ |
119 | 119 | |
120 | - // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
|
121 | - // the occurrences for the local entities. |
|
122 | - if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
123 | - $request_content = $request_body['content']; |
|
124 | - } else { |
|
125 | - $request_content = $data; |
|
126 | - } |
|
127 | - |
|
128 | - return Analysis_Response_Ops_Factory::get_instance() |
|
129 | - ->create( $json, $post_id ) |
|
130 | - ->make_entities_local() |
|
131 | - ->remove_excluded_entities( $excluded_uris ) |
|
132 | - ->add_occurrences( $request_content ) |
|
133 | - ->get_json(); |
|
120 | + // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
|
121 | + // the occurrences for the local entities. |
|
122 | + if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
123 | + $request_content = $request_body['content']; |
|
124 | + } else { |
|
125 | + $request_content = $data; |
|
126 | + } |
|
127 | + |
|
128 | + return Analysis_Response_Ops_Factory::get_instance() |
|
129 | + ->create( $json, $post_id ) |
|
130 | + ->make_entities_local() |
|
131 | + ->remove_excluded_entities( $excluded_uris ) |
|
132 | + ->add_occurrences( $request_content ) |
|
133 | + ->get_json(); |
|
134 | 134 | |
135 | 135 | } |
136 | 136 | |
137 | 137 | function wl_use_analysis_on_api_wordlift_io( $value ) { |
138 | 138 | |
139 | - return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
139 | + return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
140 | 140 | } |
@@ -12,8 +12,8 @@ discard block |
||
12 | 12 | */ |
13 | 13 | function wl_ajax_analyze_disabled_action() { |
14 | 14 | // adding the below header for debugging purpose. |
15 | - if ( ! headers_sent() ) { |
|
16 | - header( 'X-WordLift-Analysis: OFF' ); |
|
15 | + if ( ! headers_sent()) { |
|
16 | + header('X-WordLift-Analysis: OFF'); |
|
17 | 17 | } |
18 | 18 | wp_send_json_success( |
19 | 19 | array( |
@@ -35,16 +35,16 @@ discard block |
||
35 | 35 | */ |
36 | 36 | function wl_ajax_analyze_action() { |
37 | 37 | |
38 | - check_admin_referer( 'wl_analyze' ); |
|
38 | + check_admin_referer('wl_analyze'); |
|
39 | 39 | $data = ''; |
40 | 40 | |
41 | - if ( isset( $_POST['data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
41 | + if (isset($_POST['data'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
42 | 42 | // We need to send the data from editor without sanitizing to analysis service. |
43 | - $filtered_data = filter_var_array( $_POST, array( 'data' => array( 'flags' => FILTER_UNSAFE_RAW ) ) ); |
|
43 | + $filtered_data = filter_var_array($_POST, array('data' => array('flags' => FILTER_UNSAFE_RAW))); |
|
44 | 44 | $data = $filtered_data['data']; |
45 | 45 | } |
46 | 46 | |
47 | - wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
47 | + wp_send_json_success(wl_analyze_content($data, 'application/json; charset='.strtolower(get_bloginfo('charset')))); |
|
48 | 48 | |
49 | 49 | } |
50 | 50 | |
@@ -64,49 +64,49 @@ discard block |
||
64 | 64 | * @since 3.24.2 We don't return an error anymore, but an empty analysis response. This is required to allow the editor |
65 | 65 | * to manage entities or to manually add them even when analysis isn't available. |
66 | 66 | */ |
67 | -function wl_analyze_content( $data, $content_type ) { |
|
67 | +function wl_analyze_content($data, $content_type) { |
|
68 | 68 | |
69 | - $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
70 | - $request_body = json_decode( $data, true ); |
|
69 | + $default_response = json_decode('{ "entities": {}, "annotations": {}, "topics": {} }'); |
|
70 | + $request_body = json_decode($data, true); |
|
71 | 71 | |
72 | - $post_id = isset( $_REQUEST['postId'] ) ? intval( $_REQUEST['postId'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
72 | + $post_id = isset($_REQUEST['postId']) ? intval($_REQUEST['postId']) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
73 | 73 | |
74 | - if ( null === $request_body ) { |
|
74 | + if (null === $request_body) { |
|
75 | 75 | /** |
76 | 76 | * @since 3.27.7 |
77 | 77 | * |
78 | 78 | * Conditionally try using stripslashes as $_POST['data'] could be escaped |
79 | 79 | */ |
80 | - $request_body = json_decode( stripslashes( $data ), true ); |
|
80 | + $request_body = json_decode(stripslashes($data), true); |
|
81 | 81 | } |
82 | 82 | $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
83 | - $excluded_uris = array_key_exists( 'exclude', $request_body ) ? (array) $request_body['exclude'] : array(); |
|
84 | - $data = wp_json_encode( $request_body ); |
|
83 | + $excluded_uris = array_key_exists('exclude', $request_body) ? (array) $request_body['exclude'] : array(); |
|
84 | + $data = wp_json_encode($request_body); |
|
85 | 85 | |
86 | 86 | // If dataset is not enabled, return a locally prepared response without analysis API. |
87 | - if ( ! apply_filters( 'wl_feature__enable__dataset', true ) ) { |
|
87 | + if ( ! apply_filters('wl_feature__enable__dataset', true)) { |
|
88 | 88 | |
89 | 89 | return Analysis_Response_Ops_Factory::get_instance() |
90 | - ->create( $default_response, $post_id ) |
|
90 | + ->create($default_response, $post_id) |
|
91 | 91 | ->make_entities_local() |
92 | - ->add_occurrences( $request_body['content'] ) |
|
92 | + ->add_occurrences($request_body['content']) |
|
93 | 93 | ->add_local_entities() |
94 | 94 | ->get_json(); |
95 | 95 | } |
96 | 96 | |
97 | - add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
97 | + add_filter('wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io'); |
|
98 | 98 | |
99 | - $json = Analysis_Service_Factory::get_instance( $post_id ) |
|
100 | - ->get_analysis_response( $data, $content_type, $post_id ); |
|
99 | + $json = Analysis_Service_Factory::get_instance($post_id) |
|
100 | + ->get_analysis_response($data, $content_type, $post_id); |
|
101 | 101 | |
102 | - remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
102 | + remove_filter('wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io'); |
|
103 | 103 | |
104 | 104 | // If it's an error log it. |
105 | - if ( is_wp_error( $json ) ) { |
|
105 | + if (is_wp_error($json)) { |
|
106 | 106 | return Analysis_Response_Ops_Factory::get_instance() |
107 | - ->create( $default_response, $post_id ) |
|
107 | + ->create($default_response, $post_id) |
|
108 | 108 | ->make_entities_local() |
109 | - ->add_occurrences( $request_body['content'] ) |
|
109 | + ->add_occurrences($request_body['content']) |
|
110 | 110 | ->get_json(); |
111 | 111 | } |
112 | 112 | |
@@ -119,22 +119,22 @@ discard block |
||
119 | 119 | |
120 | 120 | // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
121 | 121 | // the occurrences for the local entities. |
122 | - if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
122 | + if (0 === strpos($content_type, 'application/json')) { |
|
123 | 123 | $request_content = $request_body['content']; |
124 | 124 | } else { |
125 | 125 | $request_content = $data; |
126 | 126 | } |
127 | 127 | |
128 | 128 | return Analysis_Response_Ops_Factory::get_instance() |
129 | - ->create( $json, $post_id ) |
|
129 | + ->create($json, $post_id) |
|
130 | 130 | ->make_entities_local() |
131 | - ->remove_excluded_entities( $excluded_uris ) |
|
132 | - ->add_occurrences( $request_content ) |
|
131 | + ->remove_excluded_entities($excluded_uris) |
|
132 | + ->add_occurrences($request_content) |
|
133 | 133 | ->get_json(); |
134 | 134 | |
135 | 135 | } |
136 | 136 | |
137 | -function wl_use_analysis_on_api_wordlift_io( $value ) { |
|
137 | +function wl_use_analysis_on_api_wordlift_io($value) { |
|
138 | 138 | |
139 | - return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
139 | + return preg_replace('|https://api\.wordlift\.it/|', apply_filters('wl_api_base_url', 'https://api.wordlift.io').'/', $value); |
|
140 | 140 | } |
@@ -8,55 +8,55 @@ |
||
8 | 8 | */ |
9 | 9 | abstract class Wordlift_Shortcode { |
10 | 10 | |
11 | - /** |
|
12 | - * The shortcode, set by extending classes. |
|
13 | - */ |
|
14 | - const SHORTCODE = null; |
|
15 | - |
|
16 | - /** |
|
17 | - * Create a shortcode instance by registering the shortcode with the render |
|
18 | - * function. |
|
19 | - * |
|
20 | - * @since 3.5.4 |
|
21 | - */ |
|
22 | - public function __construct() { |
|
23 | - |
|
24 | - add_shortcode( static::SHORTCODE, array( $this, 'render' ) ); |
|
25 | - |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Render the shortcode. |
|
30 | - * |
|
31 | - * @param array $atts An array of shortcode attributes as set by the editor. |
|
32 | - * |
|
33 | - * @return string The output html code. |
|
34 | - * @since 3.5.4 |
|
35 | - */ |
|
36 | - abstract public function render( $atts ); |
|
37 | - |
|
38 | - /** |
|
39 | - * Enqueue scripts. Called by the shortcode implementations in their render |
|
40 | - * method. |
|
41 | - * |
|
42 | - * @since 3.5.4 |
|
43 | - */ |
|
44 | - protected function enqueue_scripts() { |
|
45 | - |
|
46 | - wp_enqueue_script( 'angularjs', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular.min.js', array(), '1.3.11', false ); |
|
47 | - wp_enqueue_script( 'angularjs-touch', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular-touch.min.js', array( 'angularjs' ), '1.3.11', false ); |
|
48 | - wp_enqueue_script( |
|
49 | - 'wordlift-ui', |
|
50 | - dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', |
|
51 | - array( |
|
52 | - 'jquery', |
|
53 | - 'angularjs', |
|
54 | - 'angularjs-touch', |
|
55 | - ), |
|
56 | - WORDLIFT_VERSION, |
|
57 | - false |
|
58 | - ); |
|
59 | - |
|
60 | - } |
|
11 | + /** |
|
12 | + * The shortcode, set by extending classes. |
|
13 | + */ |
|
14 | + const SHORTCODE = null; |
|
15 | + |
|
16 | + /** |
|
17 | + * Create a shortcode instance by registering the shortcode with the render |
|
18 | + * function. |
|
19 | + * |
|
20 | + * @since 3.5.4 |
|
21 | + */ |
|
22 | + public function __construct() { |
|
23 | + |
|
24 | + add_shortcode( static::SHORTCODE, array( $this, 'render' ) ); |
|
25 | + |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Render the shortcode. |
|
30 | + * |
|
31 | + * @param array $atts An array of shortcode attributes as set by the editor. |
|
32 | + * |
|
33 | + * @return string The output html code. |
|
34 | + * @since 3.5.4 |
|
35 | + */ |
|
36 | + abstract public function render( $atts ); |
|
37 | + |
|
38 | + /** |
|
39 | + * Enqueue scripts. Called by the shortcode implementations in their render |
|
40 | + * method. |
|
41 | + * |
|
42 | + * @since 3.5.4 |
|
43 | + */ |
|
44 | + protected function enqueue_scripts() { |
|
45 | + |
|
46 | + wp_enqueue_script( 'angularjs', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular.min.js', array(), '1.3.11', false ); |
|
47 | + wp_enqueue_script( 'angularjs-touch', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular-touch.min.js', array( 'angularjs' ), '1.3.11', false ); |
|
48 | + wp_enqueue_script( |
|
49 | + 'wordlift-ui', |
|
50 | + dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', |
|
51 | + array( |
|
52 | + 'jquery', |
|
53 | + 'angularjs', |
|
54 | + 'angularjs-touch', |
|
55 | + ), |
|
56 | + WORDLIFT_VERSION, |
|
57 | + false |
|
58 | + ); |
|
59 | + |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public function __construct() { |
23 | 23 | |
24 | - add_shortcode( static::SHORTCODE, array( $this, 'render' ) ); |
|
24 | + add_shortcode(static::SHORTCODE, array($this, 'render')); |
|
25 | 25 | |
26 | 26 | } |
27 | 27 | |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @return string The output html code. |
34 | 34 | * @since 3.5.4 |
35 | 35 | */ |
36 | - abstract public function render( $atts ); |
|
36 | + abstract public function render($atts); |
|
37 | 37 | |
38 | 38 | /** |
39 | 39 | * Enqueue scripts. Called by the shortcode implementations in their render |
@@ -43,11 +43,11 @@ discard block |
||
43 | 43 | */ |
44 | 44 | protected function enqueue_scripts() { |
45 | 45 | |
46 | - wp_enqueue_script( 'angularjs', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular.min.js', array(), '1.3.11', false ); |
|
47 | - wp_enqueue_script( 'angularjs-touch', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular-touch.min.js', array( 'angularjs' ), '1.3.11', false ); |
|
46 | + wp_enqueue_script('angularjs', plugin_dir_url(__DIR__).'/js/3rdparty/angular.min.js', array(), '1.3.11', false); |
|
47 | + wp_enqueue_script('angularjs-touch', plugin_dir_url(__DIR__).'/js/3rdparty/angular-touch.min.js', array('angularjs'), '1.3.11', false); |
|
48 | 48 | wp_enqueue_script( |
49 | 49 | 'wordlift-ui', |
50 | - dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', |
|
50 | + dirname(plugin_dir_url(__FILE__)).'/js/wordlift-ui'.( ! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG ? '.min' : '').'.js', |
|
51 | 51 | array( |
52 | 52 | 'jquery', |
53 | 53 | 'angularjs', |
@@ -20,45 +20,45 @@ discard block |
||
20 | 20 | */ |
21 | 21 | function wl_shortcode_faceted_search( $request ) { |
22 | 22 | |
23 | - // Filter the allowed parameters for caching. |
|
24 | - $cache_params = array_intersect_key( |
|
25 | - $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
26 | - array( |
|
27 | - 'post_id' => 1, |
|
28 | - 'post_types' => 1, |
|
29 | - 'limit' => 1, |
|
30 | - 'amp' => 1, |
|
31 | - 'sort' => 1, |
|
32 | - ) |
|
33 | - ); |
|
23 | + // Filter the allowed parameters for caching. |
|
24 | + $cache_params = array_intersect_key( |
|
25 | + $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
26 | + array( |
|
27 | + 'post_id' => 1, |
|
28 | + 'post_types' => 1, |
|
29 | + 'limit' => 1, |
|
30 | + 'amp' => 1, |
|
31 | + 'sort' => 1, |
|
32 | + ) |
|
33 | + ); |
|
34 | 34 | |
35 | - // Create the cache key. |
|
36 | - $cache_key = array( |
|
37 | - 'request_params' => $cache_params, |
|
38 | - ); |
|
35 | + // Create the cache key. |
|
36 | + $cache_key = array( |
|
37 | + 'request_params' => $cache_params, |
|
38 | + ); |
|
39 | 39 | |
40 | - // Create the TTL cache and try to get the results. |
|
41 | - $cache = new Ttl_Cache( 'faceted-search', 8 * 60 * 60 ); // 8 hours. |
|
42 | - $cache_results = $cache->get( $cache_key ); |
|
40 | + // Create the TTL cache and try to get the results. |
|
41 | + $cache = new Ttl_Cache( 'faceted-search', 8 * 60 * 60 ); // 8 hours. |
|
42 | + $cache_results = $cache->get( $cache_key ); |
|
43 | 43 | |
44 | - // So that the endpoint can be used remotely |
|
45 | - header( 'Access-Control-Allow-Origin: *' ); |
|
44 | + // So that the endpoint can be used remotely |
|
45 | + header( 'Access-Control-Allow-Origin: *' ); |
|
46 | 46 | |
47 | - if ( isset( $cache_results ) ) { |
|
48 | - header( 'X-WordLift-Cache: HIT' ); |
|
49 | - wl_core_send_json( $cache_results ); |
|
47 | + if ( isset( $cache_results ) ) { |
|
48 | + header( 'X-WordLift-Cache: HIT' ); |
|
49 | + wl_core_send_json( $cache_results ); |
|
50 | 50 | |
51 | - return; |
|
52 | - } |
|
51 | + return; |
|
52 | + } |
|
53 | 53 | |
54 | - header( 'X-WordLift-Cache: MISS' ); |
|
54 | + header( 'X-WordLift-Cache: MISS' ); |
|
55 | 55 | |
56 | - $results = wl_shortcode_faceted_search_origin( $request->get_query_params() ); |
|
56 | + $results = wl_shortcode_faceted_search_origin( $request->get_query_params() ); |
|
57 | 57 | |
58 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
59 | - $cache->put( $cache_key, $results ); |
|
58 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
59 | + $cache->put( $cache_key, $results ); |
|
60 | 60 | |
61 | - wl_core_send_json( $results ); |
|
61 | + wl_core_send_json( $results ); |
|
62 | 62 | |
63 | 63 | } |
64 | 64 | |
@@ -70,141 +70,141 @@ discard block |
||
70 | 70 | */ |
71 | 71 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
72 | 72 | function wl_shortcode_faceted_search_origin( $request ) { |
73 | - // Post ID must be defined. |
|
74 | - if ( ! isset( $request['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
75 | - wp_die( 'No post_id given' ); |
|
76 | - |
|
77 | - return; |
|
78 | - } |
|
79 | - |
|
80 | - $current_post_id = (int) $request['post_id']; // WPCS: input var ok; CSRF ok. |
|
81 | - $current_post = get_post( $current_post_id ); |
|
82 | - $faceted_id = isset( $request['uniqid'] ) ? sanitize_text_field( wp_unslash( (string) $request['uniqid'] ) ) : ''; |
|
83 | - |
|
84 | - $post_types = isset( $request['post_types'] ) ? sanitize_text_field( wp_unslash( (string) $request['post_types'] ) ) : ''; |
|
85 | - $post_types = explode( ',', $post_types ); |
|
86 | - $existing_post_types = get_post_types(); |
|
87 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
88 | - |
|
89 | - // Post ID has to match an existing item. |
|
90 | - if ( null === $current_post ) { |
|
91 | - wp_die( 'No valid post_id given' ); |
|
92 | - |
|
93 | - return; |
|
94 | - } |
|
95 | - |
|
96 | - // If the current post is an entity, |
|
97 | - // the current post is used as main entity. |
|
98 | - // Otherwise, current post related entities are used. |
|
99 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
100 | - |
|
101 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
102 | - array( $current_post->ID ) : |
|
103 | - $entity_service->get_related_entities( $current_post->ID ); |
|
104 | - |
|
105 | - // If there are no entities we cannot render the widget. |
|
106 | - if ( 0 === count( $entity_ids ) ) { |
|
107 | - /** |
|
108 | - * If this function is not called from ajax |
|
109 | - * then this should not throw an error. |
|
110 | - * Note: Used in scripbox longtail project on json endpoint. |
|
111 | - */ |
|
112 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
113 | - wp_die( 'No entities available' ); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - // phpcs:ignore Standard.Category.SniffName.ErrorCode |
|
118 | - $limit = ( isset( $request['limit'] ) ) ? (int) $request['limit'] : 4; |
|
119 | - $amp = isset( $request['amp'] ); |
|
120 | - |
|
121 | - /** |
|
122 | - * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
123 | - * The ordering should be descending by date on default. |
|
124 | - */ |
|
125 | - $order_by = 'DESC'; |
|
126 | - if ( isset( $request['sort'] ) && is_string( $request['sort'] ) ) { |
|
127 | - $order_by = sanitize_sql_orderby( wp_unslash( (string) $request['sort'] ) ); |
|
128 | - } |
|
129 | - |
|
130 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
131 | - $entity_ids, |
|
132 | - '*', |
|
133 | - null, |
|
134 | - 'publish', |
|
135 | - array( $current_post_id ), |
|
136 | - $limit, |
|
137 | - null, |
|
138 | - $order_by, |
|
139 | - $post_types |
|
140 | - ); |
|
141 | - |
|
142 | - $referencing_post_ids = array_map( |
|
143 | - function ( $p ) { |
|
144 | - return $p->ID; |
|
145 | - }, |
|
146 | - $referencing_posts |
|
147 | - ); |
|
148 | - |
|
149 | - $post_results = array(); |
|
150 | - $entity_results = array(); |
|
151 | - |
|
152 | - // Populate $post_results |
|
153 | - |
|
154 | - if ( $referencing_posts ) { |
|
155 | - foreach ( $referencing_posts as $post_obj ) { |
|
156 | - |
|
157 | - /** |
|
158 | - * Use the thumbnail. |
|
159 | - * |
|
160 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
161 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
162 | - * |
|
163 | - * @since 3.19.3 We're using the medium size image. |
|
164 | - */ |
|
165 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
166 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
167 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
168 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
169 | - $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
170 | - $post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
171 | - $result = $post_obj; |
|
172 | - $post_results[] = $result; |
|
173 | - } |
|
174 | - } |
|
175 | - |
|
176 | - // Add filler posts if needed |
|
177 | - |
|
178 | - $filler_count = $limit - count( $post_results ); |
|
179 | - if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
180 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
181 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
182 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
183 | - |
|
184 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
185 | - } |
|
186 | - $referencing_post_ids = array_map( |
|
187 | - function ( $post ) { |
|
188 | - return $post->ID; |
|
189 | - }, |
|
190 | - $post_results |
|
191 | - ); |
|
192 | - |
|
193 | - // Populate $entity_results |
|
194 | - |
|
195 | - global $wpdb; |
|
196 | - |
|
197 | - // Retrieve Wordlift relation instances table name. |
|
198 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
199 | - |
|
200 | - /* |
|
73 | + // Post ID must be defined. |
|
74 | + if ( ! isset( $request['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
75 | + wp_die( 'No post_id given' ); |
|
76 | + |
|
77 | + return; |
|
78 | + } |
|
79 | + |
|
80 | + $current_post_id = (int) $request['post_id']; // WPCS: input var ok; CSRF ok. |
|
81 | + $current_post = get_post( $current_post_id ); |
|
82 | + $faceted_id = isset( $request['uniqid'] ) ? sanitize_text_field( wp_unslash( (string) $request['uniqid'] ) ) : ''; |
|
83 | + |
|
84 | + $post_types = isset( $request['post_types'] ) ? sanitize_text_field( wp_unslash( (string) $request['post_types'] ) ) : ''; |
|
85 | + $post_types = explode( ',', $post_types ); |
|
86 | + $existing_post_types = get_post_types(); |
|
87 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
88 | + |
|
89 | + // Post ID has to match an existing item. |
|
90 | + if ( null === $current_post ) { |
|
91 | + wp_die( 'No valid post_id given' ); |
|
92 | + |
|
93 | + return; |
|
94 | + } |
|
95 | + |
|
96 | + // If the current post is an entity, |
|
97 | + // the current post is used as main entity. |
|
98 | + // Otherwise, current post related entities are used. |
|
99 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
100 | + |
|
101 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
102 | + array( $current_post->ID ) : |
|
103 | + $entity_service->get_related_entities( $current_post->ID ); |
|
104 | + |
|
105 | + // If there are no entities we cannot render the widget. |
|
106 | + if ( 0 === count( $entity_ids ) ) { |
|
107 | + /** |
|
108 | + * If this function is not called from ajax |
|
109 | + * then this should not throw an error. |
|
110 | + * Note: Used in scripbox longtail project on json endpoint. |
|
111 | + */ |
|
112 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
113 | + wp_die( 'No entities available' ); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + // phpcs:ignore Standard.Category.SniffName.ErrorCode |
|
118 | + $limit = ( isset( $request['limit'] ) ) ? (int) $request['limit'] : 4; |
|
119 | + $amp = isset( $request['amp'] ); |
|
120 | + |
|
121 | + /** |
|
122 | + * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
123 | + * The ordering should be descending by date on default. |
|
124 | + */ |
|
125 | + $order_by = 'DESC'; |
|
126 | + if ( isset( $request['sort'] ) && is_string( $request['sort'] ) ) { |
|
127 | + $order_by = sanitize_sql_orderby( wp_unslash( (string) $request['sort'] ) ); |
|
128 | + } |
|
129 | + |
|
130 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
131 | + $entity_ids, |
|
132 | + '*', |
|
133 | + null, |
|
134 | + 'publish', |
|
135 | + array( $current_post_id ), |
|
136 | + $limit, |
|
137 | + null, |
|
138 | + $order_by, |
|
139 | + $post_types |
|
140 | + ); |
|
141 | + |
|
142 | + $referencing_post_ids = array_map( |
|
143 | + function ( $p ) { |
|
144 | + return $p->ID; |
|
145 | + }, |
|
146 | + $referencing_posts |
|
147 | + ); |
|
148 | + |
|
149 | + $post_results = array(); |
|
150 | + $entity_results = array(); |
|
151 | + |
|
152 | + // Populate $post_results |
|
153 | + |
|
154 | + if ( $referencing_posts ) { |
|
155 | + foreach ( $referencing_posts as $post_obj ) { |
|
156 | + |
|
157 | + /** |
|
158 | + * Use the thumbnail. |
|
159 | + * |
|
160 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
161 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
162 | + * |
|
163 | + * @since 3.19.3 We're using the medium size image. |
|
164 | + */ |
|
165 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
166 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
167 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
168 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
169 | + $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
170 | + $post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
171 | + $result = $post_obj; |
|
172 | + $post_results[] = $result; |
|
173 | + } |
|
174 | + } |
|
175 | + |
|
176 | + // Add filler posts if needed |
|
177 | + |
|
178 | + $filler_count = $limit - count( $post_results ); |
|
179 | + if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
180 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
181 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
182 | + $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
183 | + |
|
184 | + $post_results = array_merge( $post_results, $filler_posts ); |
|
185 | + } |
|
186 | + $referencing_post_ids = array_map( |
|
187 | + function ( $post ) { |
|
188 | + return $post->ID; |
|
189 | + }, |
|
190 | + $post_results |
|
191 | + ); |
|
192 | + |
|
193 | + // Populate $entity_results |
|
194 | + |
|
195 | + global $wpdb; |
|
196 | + |
|
197 | + // Retrieve Wordlift relation instances table name. |
|
198 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
199 | + |
|
200 | + /* |
|
201 | 201 | * Make sure we have some referenced post, otherwise the IN parts of |
202 | 202 | * the SQL will produce an SQL error. |
203 | 203 | */ |
204 | - if ( ! empty( $referencing_post_ids ) ) { |
|
205 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
204 | + if ( ! empty( $referencing_post_ids ) ) { |
|
205 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
206 | 206 | |
207 | - $query = " |
|
207 | + $query = " |
|
208 | 208 | SELECT |
209 | 209 | object_id AS ID, |
210 | 210 | count( object_id ) AS counter |
@@ -216,71 +216,71 @@ discard block |
||
216 | 216 | LIMIT $limit; |
217 | 217 | "; |
218 | 218 | |
219 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
219 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
220 | 220 | |
221 | - $entities = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
221 | + $entities = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
222 | 222 | |
223 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
223 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
224 | 224 | |
225 | - foreach ( $entities as $obj ) { |
|
225 | + foreach ( $entities as $obj ) { |
|
226 | 226 | |
227 | - $entity = get_post( $obj->ID ); |
|
227 | + $entity = get_post( $obj->ID ); |
|
228 | 228 | |
229 | - // Ensure only valid and published entities are returned. |
|
230 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
229 | + // Ensure only valid and published entities are returned. |
|
230 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
231 | 231 | |
232 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
233 | - $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
234 | - $serialized_entity['counter'] = $obj->counter; |
|
235 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
236 | - $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
237 | - $obj->ID, |
|
238 | - 'ids', |
|
239 | - null, |
|
240 | - null, |
|
241 | - array(), |
|
242 | - null, |
|
243 | - $referencing_post_ids |
|
244 | - ); |
|
245 | - $entity_results[] = $serialized_entity; |
|
246 | - } |
|
247 | - } |
|
248 | - } |
|
232 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
233 | + $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
234 | + $serialized_entity['counter'] = $obj->counter; |
|
235 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
236 | + $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
237 | + $obj->ID, |
|
238 | + 'ids', |
|
239 | + null, |
|
240 | + null, |
|
241 | + array(), |
|
242 | + null, |
|
243 | + $referencing_post_ids |
|
244 | + ); |
|
245 | + $entity_results[] = $serialized_entity; |
|
246 | + } |
|
247 | + } |
|
248 | + } |
|
249 | 249 | |
250 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
250 | + $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
251 | 251 | |
252 | - // Add srcset attribute. |
|
253 | - $post_results = array_map( |
|
254 | - function ( $post ) { |
|
255 | - $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
252 | + // Add srcset attribute. |
|
253 | + $post_results = array_map( |
|
254 | + function ( $post ) { |
|
255 | + $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
256 | 256 | |
257 | - return $post; |
|
258 | - }, |
|
259 | - $post_results |
|
260 | - ); |
|
257 | + return $post; |
|
258 | + }, |
|
259 | + $post_results |
|
260 | + ); |
|
261 | 261 | |
262 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
262 | + $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
263 | 263 | |
264 | - return array( |
|
265 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
266 | - 'entities' => $entity_results, |
|
267 | - ); |
|
264 | + return array( |
|
265 | + 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
266 | + 'entities' => $entity_results, |
|
267 | + ); |
|
268 | 268 | |
269 | 269 | } |
270 | 270 | |
271 | 271 | function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
272 | 272 | |
273 | - $title = wp_strip_all_tags( get_the_title( $post_id ) ); |
|
273 | + $title = wp_strip_all_tags( get_the_title( $post_id ) ); |
|
274 | 274 | |
275 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
276 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
275 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
276 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
277 | 277 | |
278 | - if ( count( $alternative_labels ) > 0 ) { |
|
279 | - $title = $alternative_labels[0]; |
|
280 | - } |
|
281 | - } |
|
278 | + if ( count( $alternative_labels ) > 0 ) { |
|
279 | + $title = $alternative_labels[0]; |
|
280 | + } |
|
281 | + } |
|
282 | 282 | |
283 | - return remove_accents( $title ); |
|
283 | + return remove_accents( $title ); |
|
284 | 284 | |
285 | 285 | } |
286 | 286 | |
@@ -288,16 +288,16 @@ discard block |
||
288 | 288 | * Adding `rest_api_init` action for network faceted-search |
289 | 289 | */ |
290 | 290 | add_action( |
291 | - 'rest_api_init', |
|
292 | - function () { |
|
293 | - register_rest_route( |
|
294 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
295 | - '/faceted-search', |
|
296 | - array( |
|
297 | - 'methods' => 'GET', |
|
298 | - 'callback' => 'wl_shortcode_faceted_search', |
|
299 | - 'permission_callback' => '__return_true', |
|
300 | - ) |
|
301 | - ); |
|
302 | - } |
|
291 | + 'rest_api_init', |
|
292 | + function () { |
|
293 | + register_rest_route( |
|
294 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
295 | + '/faceted-search', |
|
296 | + array( |
|
297 | + 'methods' => 'GET', |
|
298 | + 'callback' => 'wl_shortcode_faceted_search', |
|
299 | + 'permission_callback' => '__return_true', |
|
300 | + ) |
|
301 | + ); |
|
302 | + } |
|
303 | 303 | ); |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | * @since 3.21.4 fix the cache key by also using the request body. |
19 | 19 | * @since 3.21.2 add a caching layer. |
20 | 20 | */ |
21 | -function wl_shortcode_faceted_search( $request ) { |
|
21 | +function wl_shortcode_faceted_search($request) { |
|
22 | 22 | |
23 | 23 | // Filter the allowed parameters for caching. |
24 | 24 | $cache_params = array_intersect_key( |
@@ -38,27 +38,27 @@ discard block |
||
38 | 38 | ); |
39 | 39 | |
40 | 40 | // Create the TTL cache and try to get the results. |
41 | - $cache = new Ttl_Cache( 'faceted-search', 8 * 60 * 60 ); // 8 hours. |
|
42 | - $cache_results = $cache->get( $cache_key ); |
|
41 | + $cache = new Ttl_Cache('faceted-search', 8 * 60 * 60); // 8 hours. |
|
42 | + $cache_results = $cache->get($cache_key); |
|
43 | 43 | |
44 | 44 | // So that the endpoint can be used remotely |
45 | - header( 'Access-Control-Allow-Origin: *' ); |
|
45 | + header('Access-Control-Allow-Origin: *'); |
|
46 | 46 | |
47 | - if ( isset( $cache_results ) ) { |
|
48 | - header( 'X-WordLift-Cache: HIT' ); |
|
49 | - wl_core_send_json( $cache_results ); |
|
47 | + if (isset($cache_results)) { |
|
48 | + header('X-WordLift-Cache: HIT'); |
|
49 | + wl_core_send_json($cache_results); |
|
50 | 50 | |
51 | 51 | return; |
52 | 52 | } |
53 | 53 | |
54 | - header( 'X-WordLift-Cache: MISS' ); |
|
54 | + header('X-WordLift-Cache: MISS'); |
|
55 | 55 | |
56 | - $results = wl_shortcode_faceted_search_origin( $request->get_query_params() ); |
|
56 | + $results = wl_shortcode_faceted_search_origin($request->get_query_params()); |
|
57 | 57 | |
58 | 58 | // Put the result before sending the json to the client, since sending the json will terminate us. |
59 | - $cache->put( $cache_key, $results ); |
|
59 | + $cache->put($cache_key, $results); |
|
60 | 60 | |
61 | - wl_core_send_json( $results ); |
|
61 | + wl_core_send_json($results); |
|
62 | 62 | |
63 | 63 | } |
64 | 64 | |
@@ -69,26 +69,26 @@ discard block |
||
69 | 69 | * @since 3.26.0 |
70 | 70 | */ |
71 | 71 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
72 | -function wl_shortcode_faceted_search_origin( $request ) { |
|
72 | +function wl_shortcode_faceted_search_origin($request) { |
|
73 | 73 | // Post ID must be defined. |
74 | - if ( ! isset( $request['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
75 | - wp_die( 'No post_id given' ); |
|
74 | + if ( ! isset($request['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
75 | + wp_die('No post_id given'); |
|
76 | 76 | |
77 | 77 | return; |
78 | 78 | } |
79 | 79 | |
80 | 80 | $current_post_id = (int) $request['post_id']; // WPCS: input var ok; CSRF ok. |
81 | - $current_post = get_post( $current_post_id ); |
|
82 | - $faceted_id = isset( $request['uniqid'] ) ? sanitize_text_field( wp_unslash( (string) $request['uniqid'] ) ) : ''; |
|
81 | + $current_post = get_post($current_post_id); |
|
82 | + $faceted_id = isset($request['uniqid']) ? sanitize_text_field(wp_unslash((string) $request['uniqid'])) : ''; |
|
83 | 83 | |
84 | - $post_types = isset( $request['post_types'] ) ? sanitize_text_field( wp_unslash( (string) $request['post_types'] ) ) : ''; |
|
85 | - $post_types = explode( ',', $post_types ); |
|
84 | + $post_types = isset($request['post_types']) ? sanitize_text_field(wp_unslash((string) $request['post_types'])) : ''; |
|
85 | + $post_types = explode(',', $post_types); |
|
86 | 86 | $existing_post_types = get_post_types(); |
87 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
87 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
88 | 88 | |
89 | 89 | // Post ID has to match an existing item. |
90 | - if ( null === $current_post ) { |
|
91 | - wp_die( 'No valid post_id given' ); |
|
90 | + if (null === $current_post) { |
|
91 | + wp_die('No valid post_id given'); |
|
92 | 92 | |
93 | 93 | return; |
94 | 94 | } |
@@ -98,33 +98,32 @@ discard block |
||
98 | 98 | // Otherwise, current post related entities are used. |
99 | 99 | $entity_service = Wordlift_Entity_Service::get_instance(); |
100 | 100 | |
101 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
102 | - array( $current_post->ID ) : |
|
103 | - $entity_service->get_related_entities( $current_post->ID ); |
|
101 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
102 | + array($current_post->ID) : $entity_service->get_related_entities($current_post->ID); |
|
104 | 103 | |
105 | 104 | // If there are no entities we cannot render the widget. |
106 | - if ( 0 === count( $entity_ids ) ) { |
|
105 | + if (0 === count($entity_ids)) { |
|
107 | 106 | /** |
108 | 107 | * If this function is not called from ajax |
109 | 108 | * then this should not throw an error. |
110 | 109 | * Note: Used in scripbox longtail project on json endpoint. |
111 | 110 | */ |
112 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
113 | - wp_die( 'No entities available' ); |
|
111 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
112 | + wp_die('No entities available'); |
|
114 | 113 | } |
115 | 114 | } |
116 | 115 | |
117 | 116 | // phpcs:ignore Standard.Category.SniffName.ErrorCode |
118 | - $limit = ( isset( $request['limit'] ) ) ? (int) $request['limit'] : 4; |
|
119 | - $amp = isset( $request['amp'] ); |
|
117 | + $limit = (isset($request['limit'])) ? (int) $request['limit'] : 4; |
|
118 | + $amp = isset($request['amp']); |
|
120 | 119 | |
121 | 120 | /** |
122 | 121 | * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
123 | 122 | * The ordering should be descending by date on default. |
124 | 123 | */ |
125 | 124 | $order_by = 'DESC'; |
126 | - if ( isset( $request['sort'] ) && is_string( $request['sort'] ) ) { |
|
127 | - $order_by = sanitize_sql_orderby( wp_unslash( (string) $request['sort'] ) ); |
|
125 | + if (isset($request['sort']) && is_string($request['sort'])) { |
|
126 | + $order_by = sanitize_sql_orderby(wp_unslash((string) $request['sort'])); |
|
128 | 127 | } |
129 | 128 | |
130 | 129 | $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -132,7 +131,7 @@ discard block |
||
132 | 131 | '*', |
133 | 132 | null, |
134 | 133 | 'publish', |
135 | - array( $current_post_id ), |
|
134 | + array($current_post_id), |
|
136 | 135 | $limit, |
137 | 136 | null, |
138 | 137 | $order_by, |
@@ -140,7 +139,7 @@ discard block |
||
140 | 139 | ); |
141 | 140 | |
142 | 141 | $referencing_post_ids = array_map( |
143 | - function ( $p ) { |
|
142 | + function($p) { |
|
144 | 143 | return $p->ID; |
145 | 144 | }, |
146 | 145 | $referencing_posts |
@@ -151,8 +150,8 @@ discard block |
||
151 | 150 | |
152 | 151 | // Populate $post_results |
153 | 152 | |
154 | - if ( $referencing_posts ) { |
|
155 | - foreach ( $referencing_posts as $post_obj ) { |
|
153 | + if ($referencing_posts) { |
|
154 | + foreach ($referencing_posts as $post_obj) { |
|
156 | 155 | |
157 | 156 | /** |
158 | 157 | * Use the thumbnail. |
@@ -162,12 +161,12 @@ discard block |
||
162 | 161 | * |
163 | 162 | * @since 3.19.3 We're using the medium size image. |
164 | 163 | */ |
165 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
166 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
164 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
165 | + $post_obj->thumbnail = ($thumbnail) ? |
|
167 | 166 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
168 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
169 | - $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
170 | - $post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
167 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
168 | + $post_obj->srcset = Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
169 | + $post_obj->post_title = wp_strip_all_tags(html_entity_decode($post_obj->post_title, ENT_QUOTES, 'UTF-8')); |
|
171 | 170 | $result = $post_obj; |
172 | 171 | $post_results[] = $result; |
173 | 172 | } |
@@ -175,16 +174,16 @@ discard block |
||
175 | 174 | |
176 | 175 | // Add filler posts if needed |
177 | 176 | |
178 | - $filler_count = $limit - count( $post_results ); |
|
179 | - if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
180 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
181 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
182 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
177 | + $filler_count = $limit - count($post_results); |
|
178 | + if (0 < apply_filters('wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids)) { |
|
179 | + $filler_posts_util = new Filler_Posts_Util($current_post_id); |
|
180 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
181 | + $filler_posts = $filler_posts_util->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
183 | 182 | |
184 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
183 | + $post_results = array_merge($post_results, $filler_posts); |
|
185 | 184 | } |
186 | 185 | $referencing_post_ids = array_map( |
187 | - function ( $post ) { |
|
186 | + function($post) { |
|
188 | 187 | return $post->ID; |
189 | 188 | }, |
190 | 189 | $post_results |
@@ -201,8 +200,8 @@ discard block |
||
201 | 200 | * Make sure we have some referenced post, otherwise the IN parts of |
202 | 201 | * the SQL will produce an SQL error. |
203 | 202 | */ |
204 | - if ( ! empty( $referencing_post_ids ) ) { |
|
205 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
203 | + if ( ! empty($referencing_post_ids)) { |
|
204 | + $subject_ids = implode(',', $referencing_post_ids); |
|
206 | 205 | |
207 | 206 | $query = " |
208 | 207 | SELECT |
@@ -216,21 +215,21 @@ discard block |
||
216 | 215 | LIMIT $limit; |
217 | 216 | "; |
218 | 217 | |
219 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
218 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
220 | 219 | |
221 | - $entities = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
220 | + $entities = $wpdb->get_results($query, OBJECT); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
222 | 221 | |
223 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
222 | + wl_write_log('Entities found '.count($entities)); |
|
224 | 223 | |
225 | - foreach ( $entities as $obj ) { |
|
224 | + foreach ($entities as $obj) { |
|
226 | 225 | |
227 | - $entity = get_post( $obj->ID ); |
|
226 | + $entity = get_post($obj->ID); |
|
228 | 227 | |
229 | 228 | // Ensure only valid and published entities are returned. |
230 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
229 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
231 | 230 | |
232 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
233 | - $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
231 | + $serialized_entity = wl_serialize_entity($entity); |
|
232 | + $serialized_entity['label'] = html_entity_decode(wl_shortcode_faceted_search_get_the_title($obj->ID), ENT_QUOTES, 'UTF-8'); |
|
234 | 233 | $serialized_entity['counter'] = $obj->counter; |
235 | 234 | $serialized_entity['createdAt'] = $entity->post_date; |
236 | 235 | $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -242,45 +241,45 @@ discard block |
||
242 | 241 | null, |
243 | 242 | $referencing_post_ids |
244 | 243 | ); |
245 | - $entity_results[] = $serialized_entity; |
|
244 | + $entity_results[] = $serialized_entity; |
|
246 | 245 | } |
247 | 246 | } |
248 | 247 | } |
249 | 248 | |
250 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
249 | + $post_results = apply_filters('wl_faceted_data_posts', $post_results, $faceted_id); |
|
251 | 250 | |
252 | 251 | // Add srcset attribute. |
253 | 252 | $post_results = array_map( |
254 | - function ( $post ) { |
|
255 | - $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
253 | + function($post) { |
|
254 | + $post->srcset = Srcset_Util::get_srcset($post->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
256 | 255 | |
257 | 256 | return $post; |
258 | 257 | }, |
259 | 258 | $post_results |
260 | 259 | ); |
261 | 260 | |
262 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
261 | + $entity_results = apply_filters('wl_faceted_data_entities', $entity_results, $faceted_id); |
|
263 | 262 | |
264 | 263 | return array( |
265 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
264 | + 'posts' => $amp ? array(array('values' => $post_results)) : $post_results, |
|
266 | 265 | 'entities' => $entity_results, |
267 | 266 | ); |
268 | 267 | |
269 | 268 | } |
270 | 269 | |
271 | -function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
|
270 | +function wl_shortcode_faceted_search_get_the_title($post_id) { |
|
272 | 271 | |
273 | - $title = wp_strip_all_tags( get_the_title( $post_id ) ); |
|
272 | + $title = wp_strip_all_tags(get_the_title($post_id)); |
|
274 | 273 | |
275 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
276 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
274 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
275 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
277 | 276 | |
278 | - if ( count( $alternative_labels ) > 0 ) { |
|
277 | + if (count($alternative_labels) > 0) { |
|
279 | 278 | $title = $alternative_labels[0]; |
280 | 279 | } |
281 | 280 | } |
282 | 281 | |
283 | - return remove_accents( $title ); |
|
282 | + return remove_accents($title); |
|
284 | 283 | |
285 | 284 | } |
286 | 285 | |
@@ -289,7 +288,7 @@ discard block |
||
289 | 288 | */ |
290 | 289 | add_action( |
291 | 290 | 'rest_api_init', |
292 | - function () { |
|
291 | + function() { |
|
293 | 292 | register_rest_route( |
294 | 293 | WL_REST_ROUTE_DEFAULT_NAMESPACE, |
295 | 294 | '/faceted-search', |
@@ -19,176 +19,176 @@ discard block |
||
19 | 19 | */ |
20 | 20 | function wl_shortcode_navigator_data() { |
21 | 21 | |
22 | - // Create the cache key. |
|
23 | - $cache_params = array_intersect_key( |
|
24 | - $_REQUEST, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
25 | - array( |
|
26 | - 'limit' => 1, |
|
27 | - 'offset' => 1, |
|
28 | - 'sort' => 1, |
|
29 | - 'post_types' => 1, |
|
30 | - 'post_id' => 1, |
|
31 | - ) |
|
32 | - ); |
|
22 | + // Create the cache key. |
|
23 | + $cache_params = array_intersect_key( |
|
24 | + $_REQUEST, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
25 | + array( |
|
26 | + 'limit' => 1, |
|
27 | + 'offset' => 1, |
|
28 | + 'sort' => 1, |
|
29 | + 'post_types' => 1, |
|
30 | + 'post_id' => 1, |
|
31 | + ) |
|
32 | + ); |
|
33 | 33 | |
34 | - $cache_key = array( 'request_params' => $cache_params ); |
|
34 | + $cache_key = array( 'request_params' => $cache_params ); |
|
35 | 35 | |
36 | - // Create the TTL cache and try to get the results. |
|
37 | - $cache = new Ttl_Cache( 'navigator', 8 * 60 * 60 ); // 8 hours. |
|
38 | - $cache_results = $cache->get( $cache_key ); |
|
36 | + // Create the TTL cache and try to get the results. |
|
37 | + $cache = new Ttl_Cache( 'navigator', 8 * 60 * 60 ); // 8 hours. |
|
38 | + $cache_results = $cache->get( $cache_key ); |
|
39 | 39 | |
40 | - // So that the endpoint can be used remotely |
|
41 | - header( 'Access-Control-Allow-Origin: *' ); |
|
40 | + // So that the endpoint can be used remotely |
|
41 | + header( 'Access-Control-Allow-Origin: *' ); |
|
42 | 42 | |
43 | - if ( isset( $cache_results ) ) { |
|
44 | - header( 'X-WordLift-Cache: HIT' ); |
|
43 | + if ( isset( $cache_results ) ) { |
|
44 | + header( 'X-WordLift-Cache: HIT' ); |
|
45 | 45 | |
46 | - return $cache_results; |
|
47 | - } |
|
46 | + return $cache_results; |
|
47 | + } |
|
48 | 48 | |
49 | - header( 'X-WordLift-Cache: MISS' ); |
|
49 | + header( 'X-WordLift-Cache: MISS' ); |
|
50 | 50 | |
51 | - $results = _wl_navigator_get_data(); |
|
51 | + $results = _wl_navigator_get_data(); |
|
52 | 52 | |
53 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
54 | - $cache->put( $cache_key, $results ); |
|
53 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
54 | + $cache->put( $cache_key, $results ); |
|
55 | 55 | |
56 | - return $results; |
|
56 | + return $results; |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | function _wl_navigator_get_data() { |
60 | 60 | |
61 | - check_ajax_referer( 'wl_navigator' ); |
|
62 | - |
|
63 | - // Post ID must be defined |
|
64 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
65 | - wp_send_json_error( 'No post_id given' ); |
|
66 | - |
|
67 | - return array(); |
|
68 | - } |
|
69 | - |
|
70 | - // Post ID must be defined |
|
71 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
72 | - wp_send_json_error( 'No uniqid given' ); |
|
73 | - |
|
74 | - return array(); |
|
75 | - } |
|
76 | - |
|
77 | - // Limit the results (defaults to 4) |
|
78 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
79 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
80 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( wp_unslash( $_GET['sort'] ) ) : 'ID DESC'; |
|
81 | - $post_types = isset( $_GET['post_types'] ) ? sanitize_text_field( wp_unslash( $_GET['post_types'] ) ) : ''; |
|
82 | - $post_types = explode( ',', $post_types ); |
|
83 | - $existing_post_types = get_post_types(); |
|
84 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
85 | - $current_post_id = (int) $_GET['post_id']; |
|
86 | - $current_post = get_post( $current_post_id ); |
|
87 | - |
|
88 | - $navigator_id = sanitize_text_field( wp_unslash( $_GET['uniqid'] ) ); |
|
89 | - |
|
90 | - // Post ID has to match an existing item |
|
91 | - if ( null === $current_post ) { |
|
92 | - wp_send_json_error( 'No valid post_id given' ); |
|
93 | - |
|
94 | - return array(); |
|
95 | - } |
|
96 | - |
|
97 | - // Determine navigator type and call respective _get_results |
|
98 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
99 | - |
|
100 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( |
|
101 | - $current_post_id, |
|
102 | - array( |
|
103 | - 'ID', |
|
104 | - 'post_title', |
|
105 | - ), |
|
106 | - $order_by, |
|
107 | - $navigator_length, |
|
108 | - $navigator_offset, |
|
109 | - $post_types |
|
110 | - ); |
|
111 | - } else { |
|
112 | - $referencing_posts = Navigator_Data::post_navigator_get_results( |
|
113 | - $current_post_id, |
|
114 | - array( |
|
115 | - 'ID', |
|
116 | - 'post_title', |
|
117 | - ), |
|
118 | - $order_by, |
|
119 | - $navigator_length, |
|
120 | - $navigator_offset, |
|
121 | - $post_types |
|
122 | - ); |
|
123 | - |
|
124 | - } |
|
125 | - |
|
126 | - // loop over them and take the first one which is not already in the $related_posts |
|
127 | - $results = array(); |
|
128 | - foreach ( $referencing_posts as $referencing_post ) { |
|
129 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
130 | - |
|
131 | - /** |
|
132 | - * Use the thumbnail. |
|
133 | - * |
|
134 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
135 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
136 | - * |
|
137 | - * @since 3.19.3 We're using the medium size image. |
|
138 | - */ |
|
139 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
140 | - |
|
141 | - $result = array( |
|
142 | - 'post' => array( |
|
143 | - 'id' => $referencing_post->ID, |
|
144 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
145 | - 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
146 | - 'thumbnail' => $thumbnail, |
|
147 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ), |
|
148 | - ), |
|
149 | - 'entity' => array( |
|
150 | - 'id' => $referencing_post->entity_id, |
|
151 | - 'label' => $serialized_entity['label'], |
|
152 | - 'mainType' => $serialized_entity['mainType'], |
|
153 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
154 | - ), |
|
155 | - ); |
|
156 | - |
|
157 | - $results[] = $result; |
|
158 | - } |
|
159 | - |
|
160 | - if ( count( $results ) < $navigator_length ) { |
|
161 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
162 | - } |
|
163 | - |
|
164 | - // Add filler posts if needed |
|
165 | - $filler_count = $navigator_length - count( $results ); |
|
166 | - if ( $filler_count > 0 ) { |
|
167 | - $referencing_post_ids = array_map( |
|
168 | - function ( $p ) { |
|
169 | - return $p->ID; |
|
170 | - }, |
|
171 | - $referencing_posts |
|
172 | - ); |
|
173 | - /** |
|
174 | - * @since 3.27.8 |
|
175 | - * Filler posts are fetched using this util. |
|
176 | - */ |
|
177 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
178 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
179 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
180 | - $results = array_merge( $results, $filler_posts ); |
|
181 | - } |
|
182 | - |
|
183 | - // Apply filters after fillers are added |
|
184 | - foreach ( $results as $result_index => $result ) { |
|
185 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
186 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
187 | - } |
|
188 | - |
|
189 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
190 | - |
|
191 | - return $results; |
|
61 | + check_ajax_referer( 'wl_navigator' ); |
|
62 | + |
|
63 | + // Post ID must be defined |
|
64 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
65 | + wp_send_json_error( 'No post_id given' ); |
|
66 | + |
|
67 | + return array(); |
|
68 | + } |
|
69 | + |
|
70 | + // Post ID must be defined |
|
71 | + if ( ! isset( $_GET['uniqid'] ) ) { |
|
72 | + wp_send_json_error( 'No uniqid given' ); |
|
73 | + |
|
74 | + return array(); |
|
75 | + } |
|
76 | + |
|
77 | + // Limit the results (defaults to 4) |
|
78 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
79 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
80 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( wp_unslash( $_GET['sort'] ) ) : 'ID DESC'; |
|
81 | + $post_types = isset( $_GET['post_types'] ) ? sanitize_text_field( wp_unslash( $_GET['post_types'] ) ) : ''; |
|
82 | + $post_types = explode( ',', $post_types ); |
|
83 | + $existing_post_types = get_post_types(); |
|
84 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
85 | + $current_post_id = (int) $_GET['post_id']; |
|
86 | + $current_post = get_post( $current_post_id ); |
|
87 | + |
|
88 | + $navigator_id = sanitize_text_field( wp_unslash( $_GET['uniqid'] ) ); |
|
89 | + |
|
90 | + // Post ID has to match an existing item |
|
91 | + if ( null === $current_post ) { |
|
92 | + wp_send_json_error( 'No valid post_id given' ); |
|
93 | + |
|
94 | + return array(); |
|
95 | + } |
|
96 | + |
|
97 | + // Determine navigator type and call respective _get_results |
|
98 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
99 | + |
|
100 | + $referencing_posts = Navigator_Data::entity_navigator_get_results( |
|
101 | + $current_post_id, |
|
102 | + array( |
|
103 | + 'ID', |
|
104 | + 'post_title', |
|
105 | + ), |
|
106 | + $order_by, |
|
107 | + $navigator_length, |
|
108 | + $navigator_offset, |
|
109 | + $post_types |
|
110 | + ); |
|
111 | + } else { |
|
112 | + $referencing_posts = Navigator_Data::post_navigator_get_results( |
|
113 | + $current_post_id, |
|
114 | + array( |
|
115 | + 'ID', |
|
116 | + 'post_title', |
|
117 | + ), |
|
118 | + $order_by, |
|
119 | + $navigator_length, |
|
120 | + $navigator_offset, |
|
121 | + $post_types |
|
122 | + ); |
|
123 | + |
|
124 | + } |
|
125 | + |
|
126 | + // loop over them and take the first one which is not already in the $related_posts |
|
127 | + $results = array(); |
|
128 | + foreach ( $referencing_posts as $referencing_post ) { |
|
129 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
130 | + |
|
131 | + /** |
|
132 | + * Use the thumbnail. |
|
133 | + * |
|
134 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
135 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
136 | + * |
|
137 | + * @since 3.19.3 We're using the medium size image. |
|
138 | + */ |
|
139 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
140 | + |
|
141 | + $result = array( |
|
142 | + 'post' => array( |
|
143 | + 'id' => $referencing_post->ID, |
|
144 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
145 | + 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
146 | + 'thumbnail' => $thumbnail, |
|
147 | + 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ), |
|
148 | + ), |
|
149 | + 'entity' => array( |
|
150 | + 'id' => $referencing_post->entity_id, |
|
151 | + 'label' => $serialized_entity['label'], |
|
152 | + 'mainType' => $serialized_entity['mainType'], |
|
153 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
154 | + ), |
|
155 | + ); |
|
156 | + |
|
157 | + $results[] = $result; |
|
158 | + } |
|
159 | + |
|
160 | + if ( count( $results ) < $navigator_length ) { |
|
161 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
162 | + } |
|
163 | + |
|
164 | + // Add filler posts if needed |
|
165 | + $filler_count = $navigator_length - count( $results ); |
|
166 | + if ( $filler_count > 0 ) { |
|
167 | + $referencing_post_ids = array_map( |
|
168 | + function ( $p ) { |
|
169 | + return $p->ID; |
|
170 | + }, |
|
171 | + $referencing_posts |
|
172 | + ); |
|
173 | + /** |
|
174 | + * @since 3.27.8 |
|
175 | + * Filler posts are fetched using this util. |
|
176 | + */ |
|
177 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
178 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
179 | + $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
180 | + $results = array_merge( $results, $filler_posts ); |
|
181 | + } |
|
182 | + |
|
183 | + // Apply filters after fillers are added |
|
184 | + foreach ( $results as $result_index => $result ) { |
|
185 | + $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
186 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
187 | + } |
|
188 | + |
|
189 | + $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
190 | + |
|
191 | + return $results; |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
@@ -198,9 +198,9 @@ discard block |
||
198 | 198 | */ |
199 | 199 | function wl_shortcode_navigator_ajax() { |
200 | 200 | |
201 | - // Temporary blocking the Navigator. |
|
202 | - $results = wl_shortcode_navigator_data(); |
|
203 | - wl_core_send_json( $results ); |
|
201 | + // Temporary blocking the Navigator. |
|
202 | + $results = wl_shortcode_navigator_data(); |
|
203 | + wl_core_send_json( $results ); |
|
204 | 204 | |
205 | 205 | } |
206 | 206 | |
@@ -212,16 +212,16 @@ discard block |
||
212 | 212 | */ |
213 | 213 | function wl_shortcode_navigator_wp_json() { |
214 | 214 | |
215 | - $results = wl_shortcode_navigator_data(); |
|
216 | - if ( ob_get_contents() ) { |
|
217 | - ob_clean(); |
|
218 | - } |
|
215 | + $results = wl_shortcode_navigator_data(); |
|
216 | + if ( ob_get_contents() ) { |
|
217 | + ob_clean(); |
|
218 | + } |
|
219 | 219 | |
220 | - return array( |
|
221 | - 'items' => array( |
|
222 | - array( 'values' => $results ), |
|
223 | - ), |
|
224 | - ); |
|
220 | + return array( |
|
221 | + 'items' => array( |
|
222 | + array( 'values' => $results ), |
|
223 | + ), |
|
224 | + ); |
|
225 | 225 | |
226 | 226 | } |
227 | 227 | |
@@ -229,18 +229,18 @@ discard block |
||
229 | 229 | * Adding `rest_api_init` action for amp backend of navigator |
230 | 230 | */ |
231 | 231 | add_action( |
232 | - 'rest_api_init', |
|
233 | - function () { |
|
234 | - register_rest_route( |
|
235 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
236 | - '/navigator', |
|
237 | - array( |
|
238 | - 'methods' => 'GET', |
|
239 | - 'permission_callback' => '__return_true', |
|
240 | - 'callback' => 'wl_shortcode_navigator_wp_json', |
|
241 | - ) |
|
242 | - ); |
|
243 | - } |
|
232 | + 'rest_api_init', |
|
233 | + function () { |
|
234 | + register_rest_route( |
|
235 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
236 | + '/navigator', |
|
237 | + array( |
|
238 | + 'methods' => 'GET', |
|
239 | + 'permission_callback' => '__return_true', |
|
240 | + 'callback' => 'wl_shortcode_navigator_wp_json', |
|
241 | + ) |
|
242 | + ); |
|
243 | + } |
|
244 | 244 | ); |
245 | 245 | |
246 | 246 | /** |
@@ -249,31 +249,31 @@ discard block |
||
249 | 249 | * @since 2.2.0 |
250 | 250 | */ |
251 | 251 | add_action( |
252 | - 'plugins_loaded', |
|
253 | - function () { |
|
254 | - $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
255 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
256 | - return; |
|
257 | - } |
|
258 | - |
|
259 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
260 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
261 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
262 | - }, |
|
263 | - 0 |
|
252 | + 'plugins_loaded', |
|
253 | + function () { |
|
254 | + $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
255 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
256 | + return; |
|
257 | + } |
|
258 | + |
|
259 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
260 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
261 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
262 | + }, |
|
263 | + 0 |
|
264 | 264 | ); |
265 | 265 | |
266 | 266 | add_action( |
267 | - 'init', |
|
268 | - function () { |
|
269 | - $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
270 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
271 | - return; |
|
272 | - } |
|
273 | - |
|
274 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
275 | - remove_action( 'init', 'gglcptch_init' ); |
|
276 | - }, |
|
277 | - 0 |
|
267 | + 'init', |
|
268 | + function () { |
|
269 | + $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
270 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
271 | + return; |
|
272 | + } |
|
273 | + |
|
274 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
275 | + remove_action( 'init', 'gglcptch_init' ); |
|
276 | + }, |
|
277 | + 0 |
|
278 | 278 | ); |
279 | 279 |
@@ -31,71 +31,71 @@ discard block |
||
31 | 31 | ) |
32 | 32 | ); |
33 | 33 | |
34 | - $cache_key = array( 'request_params' => $cache_params ); |
|
34 | + $cache_key = array('request_params' => $cache_params); |
|
35 | 35 | |
36 | 36 | // Create the TTL cache and try to get the results. |
37 | - $cache = new Ttl_Cache( 'navigator', 8 * 60 * 60 ); // 8 hours. |
|
38 | - $cache_results = $cache->get( $cache_key ); |
|
37 | + $cache = new Ttl_Cache('navigator', 8 * 60 * 60); // 8 hours. |
|
38 | + $cache_results = $cache->get($cache_key); |
|
39 | 39 | |
40 | 40 | // So that the endpoint can be used remotely |
41 | - header( 'Access-Control-Allow-Origin: *' ); |
|
41 | + header('Access-Control-Allow-Origin: *'); |
|
42 | 42 | |
43 | - if ( isset( $cache_results ) ) { |
|
44 | - header( 'X-WordLift-Cache: HIT' ); |
|
43 | + if (isset($cache_results)) { |
|
44 | + header('X-WordLift-Cache: HIT'); |
|
45 | 45 | |
46 | 46 | return $cache_results; |
47 | 47 | } |
48 | 48 | |
49 | - header( 'X-WordLift-Cache: MISS' ); |
|
49 | + header('X-WordLift-Cache: MISS'); |
|
50 | 50 | |
51 | 51 | $results = _wl_navigator_get_data(); |
52 | 52 | |
53 | 53 | // Put the result before sending the json to the client, since sending the json will terminate us. |
54 | - $cache->put( $cache_key, $results ); |
|
54 | + $cache->put($cache_key, $results); |
|
55 | 55 | |
56 | 56 | return $results; |
57 | 57 | } |
58 | 58 | |
59 | 59 | function _wl_navigator_get_data() { |
60 | 60 | |
61 | - check_ajax_referer( 'wl_navigator' ); |
|
61 | + check_ajax_referer('wl_navigator'); |
|
62 | 62 | |
63 | 63 | // Post ID must be defined |
64 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
65 | - wp_send_json_error( 'No post_id given' ); |
|
64 | + if ( ! isset($_GET['post_id'])) { |
|
65 | + wp_send_json_error('No post_id given'); |
|
66 | 66 | |
67 | 67 | return array(); |
68 | 68 | } |
69 | 69 | |
70 | 70 | // Post ID must be defined |
71 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
72 | - wp_send_json_error( 'No uniqid given' ); |
|
71 | + if ( ! isset($_GET['uniqid'])) { |
|
72 | + wp_send_json_error('No uniqid given'); |
|
73 | 73 | |
74 | 74 | return array(); |
75 | 75 | } |
76 | 76 | |
77 | 77 | // Limit the results (defaults to 4) |
78 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
79 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
80 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( wp_unslash( $_GET['sort'] ) ) : 'ID DESC'; |
|
81 | - $post_types = isset( $_GET['post_types'] ) ? sanitize_text_field( wp_unslash( $_GET['post_types'] ) ) : ''; |
|
82 | - $post_types = explode( ',', $post_types ); |
|
78 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
79 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
80 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby(wp_unslash($_GET['sort'])) : 'ID DESC'; |
|
81 | + $post_types = isset($_GET['post_types']) ? sanitize_text_field(wp_unslash($_GET['post_types'])) : ''; |
|
82 | + $post_types = explode(',', $post_types); |
|
83 | 83 | $existing_post_types = get_post_types(); |
84 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
84 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
85 | 85 | $current_post_id = (int) $_GET['post_id']; |
86 | - $current_post = get_post( $current_post_id ); |
|
86 | + $current_post = get_post($current_post_id); |
|
87 | 87 | |
88 | - $navigator_id = sanitize_text_field( wp_unslash( $_GET['uniqid'] ) ); |
|
88 | + $navigator_id = sanitize_text_field(wp_unslash($_GET['uniqid'])); |
|
89 | 89 | |
90 | 90 | // Post ID has to match an existing item |
91 | - if ( null === $current_post ) { |
|
92 | - wp_send_json_error( 'No valid post_id given' ); |
|
91 | + if (null === $current_post) { |
|
92 | + wp_send_json_error('No valid post_id given'); |
|
93 | 93 | |
94 | 94 | return array(); |
95 | 95 | } |
96 | 96 | |
97 | 97 | // Determine navigator type and call respective _get_results |
98 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
98 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
99 | 99 | |
100 | 100 | $referencing_posts = Navigator_Data::entity_navigator_get_results( |
101 | 101 | $current_post_id, |
@@ -125,8 +125,8 @@ discard block |
||
125 | 125 | |
126 | 126 | // loop over them and take the first one which is not already in the $related_posts |
127 | 127 | $results = array(); |
128 | - foreach ( $referencing_posts as $referencing_post ) { |
|
129 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
128 | + foreach ($referencing_posts as $referencing_post) { |
|
129 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
130 | 130 | |
131 | 131 | /** |
132 | 132 | * Use the thumbnail. |
@@ -136,36 +136,36 @@ discard block |
||
136 | 136 | * |
137 | 137 | * @since 3.19.3 We're using the medium size image. |
138 | 138 | */ |
139 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
139 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
140 | 140 | |
141 | 141 | $result = array( |
142 | 142 | 'post' => array( |
143 | 143 | 'id' => $referencing_post->ID, |
144 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
145 | - 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
144 | + 'permalink' => get_permalink($referencing_post->ID), |
|
145 | + 'title' => html_entity_decode($referencing_post->post_title, ENT_QUOTES, 'UTF-8'), |
|
146 | 146 | 'thumbnail' => $thumbnail, |
147 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ), |
|
147 | + 'srcset' => Srcset_Util::get_srcset($referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET), |
|
148 | 148 | ), |
149 | 149 | 'entity' => array( |
150 | 150 | 'id' => $referencing_post->entity_id, |
151 | 151 | 'label' => $serialized_entity['label'], |
152 | 152 | 'mainType' => $serialized_entity['mainType'], |
153 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
153 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
154 | 154 | ), |
155 | 155 | ); |
156 | 156 | |
157 | 157 | $results[] = $result; |
158 | 158 | } |
159 | 159 | |
160 | - if ( count( $results ) < $navigator_length ) { |
|
161 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
160 | + if (count($results) < $navigator_length) { |
|
161 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | // Add filler posts if needed |
165 | - $filler_count = $navigator_length - count( $results ); |
|
166 | - if ( $filler_count > 0 ) { |
|
165 | + $filler_count = $navigator_length - count($results); |
|
166 | + if ($filler_count > 0) { |
|
167 | 167 | $referencing_post_ids = array_map( |
168 | - function ( $p ) { |
|
168 | + function($p) { |
|
169 | 169 | return $p->ID; |
170 | 170 | }, |
171 | 171 | $referencing_posts |
@@ -174,19 +174,19 @@ discard block |
||
174 | 174 | * @since 3.27.8 |
175 | 175 | * Filler posts are fetched using this util. |
176 | 176 | */ |
177 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
178 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
179 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
180 | - $results = array_merge( $results, $filler_posts ); |
|
177 | + $filler_posts_util = new Filler_Posts_Util($current_post_id, $post_types); |
|
178 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
179 | + $filler_posts = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded); |
|
180 | + $results = array_merge($results, $filler_posts); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | // Apply filters after fillers are added |
184 | - foreach ( $results as $result_index => $result ) { |
|
185 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
186 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
184 | + foreach ($results as $result_index => $result) { |
|
185 | + $results[$result_index]['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id); |
|
186 | + $results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
187 | 187 | } |
188 | 188 | |
189 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
189 | + $results = apply_filters('wl_navigator_results', $results, $navigator_id, $current_post_id); |
|
190 | 190 | |
191 | 191 | return $results; |
192 | 192 | } |
@@ -200,12 +200,12 @@ discard block |
||
200 | 200 | |
201 | 201 | // Temporary blocking the Navigator. |
202 | 202 | $results = wl_shortcode_navigator_data(); |
203 | - wl_core_send_json( $results ); |
|
203 | + wl_core_send_json($results); |
|
204 | 204 | |
205 | 205 | } |
206 | 206 | |
207 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
208 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
207 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
208 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
209 | 209 | |
210 | 210 | /** |
211 | 211 | * wp-json call for the navigator widget |
@@ -213,13 +213,13 @@ discard block |
||
213 | 213 | function wl_shortcode_navigator_wp_json() { |
214 | 214 | |
215 | 215 | $results = wl_shortcode_navigator_data(); |
216 | - if ( ob_get_contents() ) { |
|
216 | + if (ob_get_contents()) { |
|
217 | 217 | ob_clean(); |
218 | 218 | } |
219 | 219 | |
220 | 220 | return array( |
221 | 221 | 'items' => array( |
222 | - array( 'values' => $results ), |
|
222 | + array('values' => $results), |
|
223 | 223 | ), |
224 | 224 | ); |
225 | 225 | |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | */ |
231 | 231 | add_action( |
232 | 232 | 'rest_api_init', |
233 | - function () { |
|
233 | + function() { |
|
234 | 234 | register_rest_route( |
235 | 235 | WL_REST_ROUTE_DEFAULT_NAMESPACE, |
236 | 236 | '/navigator', |
@@ -250,29 +250,29 @@ discard block |
||
250 | 250 | */ |
251 | 251 | add_action( |
252 | 252 | 'plugins_loaded', |
253 | - function () { |
|
254 | - $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
255 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
253 | + function() { |
|
254 | + $action = array_key_exists('action', $_REQUEST) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
255 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
256 | 256 | return; |
257 | 257 | } |
258 | 258 | |
259 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
260 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
261 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
259 | + remove_action('plugins_loaded', 'rocket_init'); |
|
260 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
261 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
262 | 262 | }, |
263 | 263 | 0 |
264 | 264 | ); |
265 | 265 | |
266 | 266 | add_action( |
267 | 267 | 'init', |
268 | - function () { |
|
269 | - $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
270 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
268 | + function() { |
|
269 | + $action = array_key_exists('action', $_REQUEST) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
270 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
271 | 271 | return; |
272 | 272 | } |
273 | 273 | |
274 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
275 | - remove_action( 'init', 'gglcptch_init' ); |
|
274 | + remove_action('init', 'wp_widgets_init', 1); |
|
275 | + remove_action('init', 'gglcptch_init'); |
|
276 | 276 | }, |
277 | 277 | 0 |
278 | 278 | ); |
@@ -20,80 +20,80 @@ |
||
20 | 20 | */ |
21 | 21 | class Wordlift_WpRocket_Adapter { |
22 | 22 | |
23 | - /** |
|
24 | - * Create a {@link Wordlift_WpRocket_Adapter} instance. |
|
25 | - * |
|
26 | - * @since 3.19.4 |
|
27 | - */ |
|
28 | - public function __construct() { |
|
23 | + /** |
|
24 | + * Create a {@link Wordlift_WpRocket_Adapter} instance. |
|
25 | + * |
|
26 | + * @since 3.19.4 |
|
27 | + */ |
|
28 | + public function __construct() { |
|
29 | 29 | |
30 | - add_filter( 'rocket_exclude_js', array( $this, 'exclude_js' ) ); |
|
31 | - add_filter( 'rocket_excluded_inline_js_content', array( $this, 'excluded_inline_js_content' ) ); |
|
30 | + add_filter( 'rocket_exclude_js', array( $this, 'exclude_js' ) ); |
|
31 | + add_filter( 'rocket_excluded_inline_js_content', array( $this, 'excluded_inline_js_content' ) ); |
|
32 | 32 | |
33 | - } |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Get the absolute path for the specified URL. |
|
37 | - * |
|
38 | - * @param string $url The full URL. |
|
39 | - * |
|
40 | - * @return string The absolute path. |
|
41 | - */ |
|
42 | - private function get_absolute_path( $url ) { |
|
35 | + /** |
|
36 | + * Get the absolute path for the specified URL. |
|
37 | + * |
|
38 | + * @param string $url The full URL. |
|
39 | + * |
|
40 | + * @return string The absolute path. |
|
41 | + */ |
|
42 | + private function get_absolute_path( $url ) { |
|
43 | 43 | |
44 | - if ( 1 !== preg_match( '|https?://[^/]+(/.*)$|', $url, $matches ) ) { |
|
45 | - return $url; |
|
46 | - } |
|
44 | + if ( 1 !== preg_match( '|https?://[^/]+(/.*)$|', $url, $matches ) ) { |
|
45 | + return $url; |
|
46 | + } |
|
47 | 47 | |
48 | - return $matches[1]; |
|
49 | - } |
|
48 | + return $matches[1]; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Hook to `rocket_exclude_defer_js` filter. |
|
53 | - * |
|
54 | - * @param array $excluded_js The preset excluded files. |
|
55 | - * |
|
56 | - * @return array The updated excluded files array. |
|
57 | - * |
|
58 | - * @since 3.25.0 We realized that WP Rocket has an issue with these path: while it seems to expect a path relative |
|
59 | - * to home_url, it is actually looking for a path relative to the root. We leave both exclusions so |
|
60 | - * that we'll be compatible in case WP Rocket fixes its implementation. |
|
61 | - * @since 3.23.0 add the Cloud js. |
|
62 | - * @since 3.20.0 hook to `rocket_exclude_js`. |
|
63 | - * @since 3.19.4 |
|
64 | - */ |
|
65 | - public function exclude_js( $excluded_js = array() ) { |
|
51 | + /** |
|
52 | + * Hook to `rocket_exclude_defer_js` filter. |
|
53 | + * |
|
54 | + * @param array $excluded_js The preset excluded files. |
|
55 | + * |
|
56 | + * @return array The updated excluded files array. |
|
57 | + * |
|
58 | + * @since 3.25.0 We realized that WP Rocket has an issue with these path: while it seems to expect a path relative |
|
59 | + * to home_url, it is actually looking for a path relative to the root. We leave both exclusions so |
|
60 | + * that we'll be compatible in case WP Rocket fixes its implementation. |
|
61 | + * @since 3.23.0 add the Cloud js. |
|
62 | + * @since 3.20.0 hook to `rocket_exclude_js`. |
|
63 | + * @since 3.19.4 |
|
64 | + */ |
|
65 | + public function exclude_js( $excluded_js = array() ) { |
|
66 | 66 | |
67 | - // Exclude our own public JS. |
|
68 | - $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_public_js_url() ); |
|
69 | - $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_cloud_js_url() ); |
|
67 | + // Exclude our own public JS. |
|
68 | + $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_public_js_url() ); |
|
69 | + $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_cloud_js_url() ); |
|
70 | 70 | |
71 | - $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/bundle.js' ); |
|
72 | - $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/wordlift-cloud.js' ); |
|
71 | + $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/bundle.js' ); |
|
72 | + $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/wordlift-cloud.js' ); |
|
73 | 73 | |
74 | - return $excluded_js; |
|
75 | - } |
|
74 | + return $excluded_js; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Filters inline JS excluded from being combined |
|
79 | - * |
|
80 | - * @link https://github.com/insideout10/wordlift-plugin/issues/868 |
|
81 | - * |
|
82 | - * @since 3.20.0 |
|
83 | - * |
|
84 | - * @param array $pattern Patterns to match. |
|
85 | - * |
|
86 | - * @return array Patterns to match. |
|
87 | - */ |
|
88 | - public function excluded_inline_js_content( $pattern = array() ) { |
|
77 | + /** |
|
78 | + * Filters inline JS excluded from being combined |
|
79 | + * |
|
80 | + * @link https://github.com/insideout10/wordlift-plugin/issues/868 |
|
81 | + * |
|
82 | + * @since 3.20.0 |
|
83 | + * |
|
84 | + * @param array $pattern Patterns to match. |
|
85 | + * |
|
86 | + * @return array Patterns to match. |
|
87 | + */ |
|
88 | + public function excluded_inline_js_content( $pattern = array() ) { |
|
89 | 89 | |
90 | - $pattern[] = 'wlSettings'; |
|
91 | - $pattern[] = 'wlNavigators'; |
|
92 | - $pattern[] = '_wlCloudSettings'; |
|
93 | - $pattern[] = 'wlProductsNavigators'; |
|
94 | - $pattern[] = 'wlFaceteds'; |
|
90 | + $pattern[] = 'wlSettings'; |
|
91 | + $pattern[] = 'wlNavigators'; |
|
92 | + $pattern[] = '_wlCloudSettings'; |
|
93 | + $pattern[] = 'wlProductsNavigators'; |
|
94 | + $pattern[] = 'wlFaceteds'; |
|
95 | 95 | |
96 | - return $pattern; |
|
97 | - } |
|
96 | + return $pattern; |
|
97 | + } |
|
98 | 98 | |
99 | 99 | } |
@@ -27,8 +27,8 @@ discard block |
||
27 | 27 | */ |
28 | 28 | public function __construct() { |
29 | 29 | |
30 | - add_filter( 'rocket_exclude_js', array( $this, 'exclude_js' ) ); |
|
31 | - add_filter( 'rocket_excluded_inline_js_content', array( $this, 'excluded_inline_js_content' ) ); |
|
30 | + add_filter('rocket_exclude_js', array($this, 'exclude_js')); |
|
31 | + add_filter('rocket_excluded_inline_js_content', array($this, 'excluded_inline_js_content')); |
|
32 | 32 | |
33 | 33 | } |
34 | 34 | |
@@ -39,9 +39,9 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @return string The absolute path. |
41 | 41 | */ |
42 | - private function get_absolute_path( $url ) { |
|
42 | + private function get_absolute_path($url) { |
|
43 | 43 | |
44 | - if ( 1 !== preg_match( '|https?://[^/]+(/.*)$|', $url, $matches ) ) { |
|
44 | + if (1 !== preg_match('|https?://[^/]+(/.*)$|', $url, $matches)) { |
|
45 | 45 | return $url; |
46 | 46 | } |
47 | 47 | |
@@ -62,14 +62,14 @@ discard block |
||
62 | 62 | * @since 3.20.0 hook to `rocket_exclude_js`. |
63 | 63 | * @since 3.19.4 |
64 | 64 | */ |
65 | - public function exclude_js( $excluded_js = array() ) { |
|
65 | + public function exclude_js($excluded_js = array()) { |
|
66 | 66 | |
67 | 67 | // Exclude our own public JS. |
68 | - $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_public_js_url() ); |
|
69 | - $excluded_js[] = $this->get_absolute_path( Wordlift_Public::get_cloud_js_url() ); |
|
68 | + $excluded_js[] = $this->get_absolute_path(Wordlift_Public::get_public_js_url()); |
|
69 | + $excluded_js[] = $this->get_absolute_path(Wordlift_Public::get_cloud_js_url()); |
|
70 | 70 | |
71 | - $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/bundle.js' ); |
|
72 | - $excluded_js[] = str_replace( home_url(), '', plugin_dir_url( __DIR__ ) . '/js/dist/wordlift-cloud.js' ); |
|
71 | + $excluded_js[] = str_replace(home_url(), '', plugin_dir_url(__DIR__).'/js/dist/bundle.js'); |
|
72 | + $excluded_js[] = str_replace(home_url(), '', plugin_dir_url(__DIR__).'/js/dist/wordlift-cloud.js'); |
|
73 | 73 | |
74 | 74 | return $excluded_js; |
75 | 75 | } |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * |
86 | 86 | * @return array Patterns to match. |
87 | 87 | */ |
88 | - public function excluded_inline_js_content( $pattern = array() ) { |
|
88 | + public function excluded_inline_js_content($pattern = array()) { |
|
89 | 89 | |
90 | 90 | $pattern[] = 'wlSettings'; |
91 | 91 | $pattern[] = 'wlNavigators'; |
@@ -18,267 +18,267 @@ discard block |
||
18 | 18 | */ |
19 | 19 | class Wordlift_Vocabulary_Shortcode extends Wordlift_Shortcode { |
20 | 20 | |
21 | - /** |
|
22 | - * The shortcode. |
|
23 | - * |
|
24 | - * @since 3.17.0 |
|
25 | - */ |
|
26 | - const SHORTCODE = 'wl_vocabulary'; |
|
27 | - |
|
28 | - /** |
|
29 | - * A {@link Wordlift_Log_Service} instance. |
|
30 | - * |
|
31 | - * @since 3.17.0 |
|
32 | - * @access private |
|
33 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
34 | - */ |
|
35 | - private $log; |
|
36 | - |
|
37 | - /** |
|
38 | - * The vocabulary id |
|
39 | - * |
|
40 | - * @since 3.18.3 |
|
41 | - * @access private |
|
42 | - * @var int $vocabulary_id The vocabulary unique id. |
|
43 | - */ |
|
44 | - private static $vocabulary_id = 0; |
|
45 | - |
|
46 | - /** |
|
47 | - * Create a {@link Wordlift_Glossary_Shortcode} instance. |
|
48 | - * |
|
49 | - * @since 3.16.0 |
|
50 | - */ |
|
51 | - public function __construct() { |
|
52 | - parent::__construct(); |
|
53 | - |
|
54 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
55 | - |
|
56 | - $this->register_block_type(); |
|
57 | - |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Check whether the requirements for this shortcode to work are available. |
|
62 | - * |
|
63 | - * @return bool True if the requirements are satisfied otherwise false. |
|
64 | - * @since 3.17.0 |
|
65 | - */ |
|
66 | - private static function are_requirements_satisfied() { |
|
67 | - |
|
68 | - return function_exists( 'mb_strlen' ) && |
|
69 | - function_exists( 'mb_substr' ) && |
|
70 | - function_exists( 'mb_strtolower' ) && |
|
71 | - function_exists( 'mb_strtoupper' ) && |
|
72 | - function_exists( 'mb_convert_case' ); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Render the shortcode. |
|
77 | - * |
|
78 | - * @param array $atts An array of shortcode attributes as set by the editor. |
|
79 | - * |
|
80 | - * @return string The output html code. |
|
81 | - * @since 3.16.0 |
|
82 | - */ |
|
83 | - public function render( $atts ) { |
|
84 | - |
|
85 | - // Bail out if the requirements aren't satisfied: we need mbstring for |
|
86 | - // the vocabulary widget to work. |
|
87 | - if ( ! self::are_requirements_satisfied() ) { |
|
88 | - $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
89 | - |
|
90 | - return ''; |
|
91 | - } |
|
92 | - |
|
93 | - wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css', array(), WORDLIFT_VERSION ); |
|
94 | - |
|
95 | - // Extract attributes and set default values. |
|
96 | - $atts = shortcode_atts( |
|
97 | - array( |
|
98 | - // The entity type, such as `person`, `organization`, ... |
|
99 | - 'type' => 'all', |
|
100 | - // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
|
101 | - 'limit' => 100, |
|
102 | - // Sort by title. |
|
103 | - 'orderby' => 'post_date', |
|
104 | - // Sort DESC. |
|
105 | - 'order' => 'DESC', |
|
106 | - // Allow to specify the category ID. |
|
107 | - 'cat' => '', |
|
108 | - ), |
|
109 | - $atts |
|
110 | - ); |
|
111 | - |
|
112 | - // Get the posts. Note that if a `type` is specified before, then the |
|
113 | - // `tax_query` from the `add_criterias` call isn't added. |
|
114 | - $posts = $this->get_posts( $atts ); |
|
115 | - |
|
116 | - // Get the alphabet. |
|
117 | - $language_code = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
118 | - $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
119 | - |
|
120 | - // Add posts to the alphabet. |
|
121 | - foreach ( $posts as $post ) { |
|
122 | - $this->add_to_alphabet( $alphabet, $post->ID ); |
|
123 | - } |
|
124 | - |
|
125 | - $header = ''; |
|
126 | - $sections = ''; |
|
127 | - |
|
128 | - // Get unique id for each vocabulary shortcode. |
|
129 | - $vocabulary_id = self::get_and_increment_vocabulary_id(); |
|
130 | - |
|
131 | - // Generate the header. |
|
132 | - foreach ( $alphabet as $item => $translations ) { |
|
133 | - $template = ( empty( $translations ) |
|
134 | - ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
|
135 | - : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
136 | - |
|
137 | - $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
138 | - } |
|
139 | - |
|
140 | - // Generate the sections. |
|
141 | - foreach ( $alphabet as $item => $translations ) { |
|
142 | - // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
|
143 | - // since we had `AB` being placed before `Aa` with `asort`. |
|
144 | - // |
|
145 | - // Order the translations alphabetically. |
|
146 | - // asort( $translations ); |
|
147 | - uasort( |
|
148 | - $translations, |
|
149 | - function ( $a, $b ) { |
|
150 | - if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
151 | - || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
152 | - return 0; |
|
153 | - } |
|
154 | - |
|
155 | - return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
156 | - } |
|
157 | - ); |
|
158 | - $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
159 | - } |
|
160 | - |
|
161 | - // Return HTML template. |
|
162 | - ob_start(); |
|
163 | - ?> |
|
21 | + /** |
|
22 | + * The shortcode. |
|
23 | + * |
|
24 | + * @since 3.17.0 |
|
25 | + */ |
|
26 | + const SHORTCODE = 'wl_vocabulary'; |
|
27 | + |
|
28 | + /** |
|
29 | + * A {@link Wordlift_Log_Service} instance. |
|
30 | + * |
|
31 | + * @since 3.17.0 |
|
32 | + * @access private |
|
33 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
34 | + */ |
|
35 | + private $log; |
|
36 | + |
|
37 | + /** |
|
38 | + * The vocabulary id |
|
39 | + * |
|
40 | + * @since 3.18.3 |
|
41 | + * @access private |
|
42 | + * @var int $vocabulary_id The vocabulary unique id. |
|
43 | + */ |
|
44 | + private static $vocabulary_id = 0; |
|
45 | + |
|
46 | + /** |
|
47 | + * Create a {@link Wordlift_Glossary_Shortcode} instance. |
|
48 | + * |
|
49 | + * @since 3.16.0 |
|
50 | + */ |
|
51 | + public function __construct() { |
|
52 | + parent::__construct(); |
|
53 | + |
|
54 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
55 | + |
|
56 | + $this->register_block_type(); |
|
57 | + |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Check whether the requirements for this shortcode to work are available. |
|
62 | + * |
|
63 | + * @return bool True if the requirements are satisfied otherwise false. |
|
64 | + * @since 3.17.0 |
|
65 | + */ |
|
66 | + private static function are_requirements_satisfied() { |
|
67 | + |
|
68 | + return function_exists( 'mb_strlen' ) && |
|
69 | + function_exists( 'mb_substr' ) && |
|
70 | + function_exists( 'mb_strtolower' ) && |
|
71 | + function_exists( 'mb_strtoupper' ) && |
|
72 | + function_exists( 'mb_convert_case' ); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Render the shortcode. |
|
77 | + * |
|
78 | + * @param array $atts An array of shortcode attributes as set by the editor. |
|
79 | + * |
|
80 | + * @return string The output html code. |
|
81 | + * @since 3.16.0 |
|
82 | + */ |
|
83 | + public function render( $atts ) { |
|
84 | + |
|
85 | + // Bail out if the requirements aren't satisfied: we need mbstring for |
|
86 | + // the vocabulary widget to work. |
|
87 | + if ( ! self::are_requirements_satisfied() ) { |
|
88 | + $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
89 | + |
|
90 | + return ''; |
|
91 | + } |
|
92 | + |
|
93 | + wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css', array(), WORDLIFT_VERSION ); |
|
94 | + |
|
95 | + // Extract attributes and set default values. |
|
96 | + $atts = shortcode_atts( |
|
97 | + array( |
|
98 | + // The entity type, such as `person`, `organization`, ... |
|
99 | + 'type' => 'all', |
|
100 | + // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
|
101 | + 'limit' => 100, |
|
102 | + // Sort by title. |
|
103 | + 'orderby' => 'post_date', |
|
104 | + // Sort DESC. |
|
105 | + 'order' => 'DESC', |
|
106 | + // Allow to specify the category ID. |
|
107 | + 'cat' => '', |
|
108 | + ), |
|
109 | + $atts |
|
110 | + ); |
|
111 | + |
|
112 | + // Get the posts. Note that if a `type` is specified before, then the |
|
113 | + // `tax_query` from the `add_criterias` call isn't added. |
|
114 | + $posts = $this->get_posts( $atts ); |
|
115 | + |
|
116 | + // Get the alphabet. |
|
117 | + $language_code = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
118 | + $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
119 | + |
|
120 | + // Add posts to the alphabet. |
|
121 | + foreach ( $posts as $post ) { |
|
122 | + $this->add_to_alphabet( $alphabet, $post->ID ); |
|
123 | + } |
|
124 | + |
|
125 | + $header = ''; |
|
126 | + $sections = ''; |
|
127 | + |
|
128 | + // Get unique id for each vocabulary shortcode. |
|
129 | + $vocabulary_id = self::get_and_increment_vocabulary_id(); |
|
130 | + |
|
131 | + // Generate the header. |
|
132 | + foreach ( $alphabet as $item => $translations ) { |
|
133 | + $template = ( empty( $translations ) |
|
134 | + ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
|
135 | + : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
136 | + |
|
137 | + $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
138 | + } |
|
139 | + |
|
140 | + // Generate the sections. |
|
141 | + foreach ( $alphabet as $item => $translations ) { |
|
142 | + // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
|
143 | + // since we had `AB` being placed before `Aa` with `asort`. |
|
144 | + // |
|
145 | + // Order the translations alphabetically. |
|
146 | + // asort( $translations ); |
|
147 | + uasort( |
|
148 | + $translations, |
|
149 | + function ( $a, $b ) { |
|
150 | + if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
151 | + || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
152 | + return 0; |
|
153 | + } |
|
154 | + |
|
155 | + return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
156 | + } |
|
157 | + ); |
|
158 | + $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
159 | + } |
|
160 | + |
|
161 | + // Return HTML template. |
|
162 | + ob_start(); |
|
163 | + ?> |
|
164 | 164 | <div class='wl-vocabulary'> |
165 | 165 | <nav class='wl-vocabulary-alphabet-nav'> |
166 | 166 | <?php |
167 | - echo wp_kses( |
|
168 | - $header, |
|
169 | - array( |
|
170 | - 'span' => array( 'class' => array() ), |
|
171 | - 'a' => array( 'href' => array() ), |
|
172 | - ) |
|
173 | - ); |
|
174 | - ?> |
|
167 | + echo wp_kses( |
|
168 | + $header, |
|
169 | + array( |
|
170 | + 'span' => array( 'class' => array() ), |
|
171 | + 'a' => array( 'href' => array() ), |
|
172 | + ) |
|
173 | + ); |
|
174 | + ?> |
|
175 | 175 | </nav> |
176 | 176 | <div class='wl-vocabulary-grid'> |
177 | 177 | <?php |
178 | - echo wp_kses( |
|
179 | - $sections, |
|
180 | - array( |
|
181 | - 'div' => array( |
|
182 | - 'class' => array(), |
|
183 | - 'id' => array(), |
|
184 | - ), |
|
185 | - 'aside' => array( 'class' => array() ), |
|
186 | - 'ul' => array( 'class' => array() ), |
|
187 | - 'li' => array(), |
|
188 | - 'a' => array( 'href' => array() ), |
|
189 | - ) |
|
190 | - ); |
|
191 | - ?> |
|
178 | + echo wp_kses( |
|
179 | + $sections, |
|
180 | + array( |
|
181 | + 'div' => array( |
|
182 | + 'class' => array(), |
|
183 | + 'id' => array(), |
|
184 | + ), |
|
185 | + 'aside' => array( 'class' => array() ), |
|
186 | + 'ul' => array( 'class' => array() ), |
|
187 | + 'li' => array(), |
|
188 | + 'a' => array( 'href' => array() ), |
|
189 | + ) |
|
190 | + ); |
|
191 | + ?> |
|
192 | 192 | </div> |
193 | 193 | </div> |
194 | 194 | <?php |
195 | - $html = ob_get_clean(); |
|
196 | - |
|
197 | - return $html; |
|
198 | - |
|
199 | - } |
|
200 | - |
|
201 | - private function register_block_type() { |
|
202 | - |
|
203 | - $scope = $this; |
|
204 | - |
|
205 | - add_action( |
|
206 | - 'init', |
|
207 | - function () use ( $scope ) { |
|
208 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
209 | - // Gutenberg is not active. |
|
210 | - return; |
|
211 | - } |
|
212 | - |
|
213 | - register_block_type( |
|
214 | - 'wordlift/vocabulary', |
|
215 | - array( |
|
216 | - 'editor_script' => 'wl-block-editor', |
|
217 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
218 | - $attr_code = ''; |
|
219 | - foreach ( $attributes as $key => $value ) { |
|
220 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
221 | - } |
|
222 | - |
|
223 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
224 | - }, |
|
225 | - 'attributes' => array( |
|
226 | - 'type' => array( |
|
227 | - 'type' => 'string', |
|
228 | - 'default' => 'all', |
|
229 | - ), |
|
230 | - 'limit' => array( |
|
231 | - 'type' => 'number', |
|
232 | - 'default' => 100, |
|
233 | - ), |
|
234 | - 'orderby' => array( |
|
235 | - 'type' => 'string', |
|
236 | - 'default' => 'post_date', |
|
237 | - ), |
|
238 | - 'order' => array( |
|
239 | - 'type' => 'string', |
|
240 | - 'default' => 'DESC', |
|
241 | - ), |
|
242 | - 'cat' => array( |
|
243 | - 'type' => 'string', |
|
244 | - 'default' => '', |
|
245 | - ), |
|
246 | - 'preview' => array( |
|
247 | - 'type' => 'boolean', |
|
248 | - 'default' => false, |
|
249 | - ), |
|
250 | - 'preview_src' => array( |
|
251 | - 'type' => 'string', |
|
252 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/vocabulary.png', |
|
253 | - ), |
|
254 | - ), |
|
255 | - ) |
|
256 | - ); |
|
257 | - } |
|
258 | - ); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Generate the html code for the section. |
|
263 | - * |
|
264 | - * @param string $letter The section's letter. |
|
265 | - * @param array $posts An array of `$post_id => $post_title` associated with |
|
266 | - * the section. |
|
267 | - * @param int $vocabulary_id Unique vocabulary id. |
|
268 | - * |
|
269 | - * @return string The section html code (or an empty string if the section has |
|
270 | - * no posts). |
|
271 | - * @since 3.17.0 |
|
272 | - */ |
|
273 | - private function get_section( $letter, $posts, $vocabulary_id ) { |
|
274 | - |
|
275 | - // Return an empty string if there are no posts. |
|
276 | - if ( 0 === count( $posts ) ) { |
|
277 | - return ''; |
|
278 | - } |
|
279 | - |
|
280 | - return sprintf( |
|
281 | - ' |
|
195 | + $html = ob_get_clean(); |
|
196 | + |
|
197 | + return $html; |
|
198 | + |
|
199 | + } |
|
200 | + |
|
201 | + private function register_block_type() { |
|
202 | + |
|
203 | + $scope = $this; |
|
204 | + |
|
205 | + add_action( |
|
206 | + 'init', |
|
207 | + function () use ( $scope ) { |
|
208 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
209 | + // Gutenberg is not active. |
|
210 | + return; |
|
211 | + } |
|
212 | + |
|
213 | + register_block_type( |
|
214 | + 'wordlift/vocabulary', |
|
215 | + array( |
|
216 | + 'editor_script' => 'wl-block-editor', |
|
217 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
218 | + $attr_code = ''; |
|
219 | + foreach ( $attributes as $key => $value ) { |
|
220 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
221 | + } |
|
222 | + |
|
223 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
224 | + }, |
|
225 | + 'attributes' => array( |
|
226 | + 'type' => array( |
|
227 | + 'type' => 'string', |
|
228 | + 'default' => 'all', |
|
229 | + ), |
|
230 | + 'limit' => array( |
|
231 | + 'type' => 'number', |
|
232 | + 'default' => 100, |
|
233 | + ), |
|
234 | + 'orderby' => array( |
|
235 | + 'type' => 'string', |
|
236 | + 'default' => 'post_date', |
|
237 | + ), |
|
238 | + 'order' => array( |
|
239 | + 'type' => 'string', |
|
240 | + 'default' => 'DESC', |
|
241 | + ), |
|
242 | + 'cat' => array( |
|
243 | + 'type' => 'string', |
|
244 | + 'default' => '', |
|
245 | + ), |
|
246 | + 'preview' => array( |
|
247 | + 'type' => 'boolean', |
|
248 | + 'default' => false, |
|
249 | + ), |
|
250 | + 'preview_src' => array( |
|
251 | + 'type' => 'string', |
|
252 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/vocabulary.png', |
|
253 | + ), |
|
254 | + ), |
|
255 | + ) |
|
256 | + ); |
|
257 | + } |
|
258 | + ); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Generate the html code for the section. |
|
263 | + * |
|
264 | + * @param string $letter The section's letter. |
|
265 | + * @param array $posts An array of `$post_id => $post_title` associated with |
|
266 | + * the section. |
|
267 | + * @param int $vocabulary_id Unique vocabulary id. |
|
268 | + * |
|
269 | + * @return string The section html code (or an empty string if the section has |
|
270 | + * no posts). |
|
271 | + * @since 3.17.0 |
|
272 | + */ |
|
273 | + private function get_section( $letter, $posts, $vocabulary_id ) { |
|
274 | + |
|
275 | + // Return an empty string if there are no posts. |
|
276 | + if ( 0 === count( $posts ) ) { |
|
277 | + return ''; |
|
278 | + } |
|
279 | + |
|
280 | + return sprintf( |
|
281 | + ' |
|
282 | 282 | <div class="wl-vocabulary-letter-block" id="wl-vocabulary-%d-%s"> |
283 | 283 | <aside class="wl-vocabulary-left-column">%s</aside> |
284 | 284 | <div class="wl-vocabulary-right-column"> |
@@ -288,157 +288,157 @@ discard block |
||
288 | 288 | </div> |
289 | 289 | </div> |
290 | 290 | ', |
291 | - $vocabulary_id, |
|
292 | - esc_attr( $letter ), |
|
293 | - esc_html( $letter ), |
|
294 | - $this->format_posts_as_list( $posts ) |
|
295 | - ); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Format an array post `$post_id => $post_title` as a list. |
|
300 | - * |
|
301 | - * @param array $posts An array of `$post_id => $post_title` key, value pairs. |
|
302 | - * |
|
303 | - * @return string A list. |
|
304 | - * @since 3.17.0 |
|
305 | - */ |
|
306 | - private function format_posts_as_list( $posts ) { |
|
307 | - |
|
308 | - return array_reduce( |
|
309 | - array_keys( $posts ), |
|
310 | - function ( $carry, $item ) use ( $posts ) { |
|
311 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
312 | - }, |
|
313 | - '' |
|
314 | - ); |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * Get the posts from WordPress using the provided attributes. |
|
319 | - * |
|
320 | - * @param array $atts The shortcode attributes. |
|
321 | - * |
|
322 | - * @return array An array of {@link WP_Post}s. |
|
323 | - * @since 3.17.0 |
|
324 | - */ |
|
325 | - private function get_posts( $atts ) { |
|
326 | - // The default arguments for the query. |
|
327 | - $args = array( |
|
328 | - // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page |
|
329 | - 'posts_per_page' => intval( $atts['limit'] ), |
|
330 | - 'update_post_meta_cache' => false, |
|
331 | - 'update_post_term_cache' => false, |
|
332 | - 'orderby' => $atts['orderby'], |
|
333 | - 'order' => $atts['order'], |
|
334 | - // Exclude the publisher. |
|
335 | - 'post__not_in' => array( Wordlift_Configuration_Service::get_instance()->get_publisher_id() ), |
|
336 | - ); |
|
337 | - |
|
338 | - // Limit the based entity type if needed. |
|
339 | - if ( 'all' !== $atts['type'] ) { |
|
340 | - $args['tax_query'] = array( |
|
341 | - array( |
|
342 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
343 | - 'field' => 'slug', |
|
344 | - 'terms' => $atts['type'], |
|
345 | - ), |
|
346 | - ); |
|
347 | - } |
|
348 | - |
|
349 | - if ( ! empty( $atts['cat'] ) ) { |
|
350 | - $args['cat'] = $atts['cat']; |
|
351 | - } |
|
352 | - |
|
353 | - // Get the posts. Note that if a `type` is specified before, then the |
|
354 | - // `tax_query` from the `add_criterias` call isn't added. |
|
355 | - return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
356 | - |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Populate the alphabet with posts. |
|
361 | - * |
|
362 | - * @param array $alphabet An array of letters. |
|
363 | - * @param int $post_id The {@link WP_Post} id. |
|
364 | - * |
|
365 | - * @since 3.17.0 |
|
366 | - */ |
|
367 | - private function add_to_alphabet( &$alphabet, $post_id ) { |
|
368 | - |
|
369 | - // Get the title without accents. |
|
370 | - $title = $this->get_the_title( $post_id ); |
|
371 | - |
|
372 | - // Get the initial letter. |
|
373 | - $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
374 | - |
|
375 | - // Add the post. |
|
376 | - $alphabet[ $letter ][ $post_id ] = $title; |
|
377 | - |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Get the title without accents. |
|
382 | - * If the post is not of type `entity`, use first synonym if synonyms exists. |
|
383 | - * |
|
384 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1096 |
|
385 | - * |
|
386 | - * @since 3.27.0 |
|
387 | - * |
|
388 | - * @param int $post_id The {@link WP_Post} id. |
|
389 | - * |
|
390 | - * @return string |
|
391 | - */ |
|
392 | - private function get_the_title( $post_id ) { |
|
393 | - |
|
394 | - $title = get_the_title( $post_id ); |
|
395 | - |
|
396 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
397 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
398 | - |
|
399 | - if ( count( $alternative_labels ) > 0 ) { |
|
400 | - $title = $alternative_labels[0]; |
|
401 | - } |
|
402 | - } |
|
403 | - |
|
404 | - return remove_accents( $title ); |
|
405 | - |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * Find the first letter in the alphabet. |
|
410 | - * |
|
411 | - * In some alphabets a letter is a compound of letters, therefore this function |
|
412 | - * will look for groups of 2 or 3 letters in the alphabet before looking for a |
|
413 | - * single letter. In case the letter is not found a # (hash) key is returned. |
|
414 | - * |
|
415 | - * @param array $alphabet An array of alphabet letters. |
|
416 | - * @param string $title The title to match. |
|
417 | - * |
|
418 | - * @return string The initial letter or a `#` key. |
|
419 | - * @since 3.17.0 |
|
420 | - */ |
|
421 | - private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
422 | - |
|
423 | - // Need to handle letters which consist of 3 and 2 characters. |
|
424 | - for ( $i = 3; $i > 0; $i -- ) { |
|
425 | - $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
426 | - if ( isset( $alphabet[ $letter ] ) ) { |
|
427 | - return $letter; |
|
428 | - } |
|
429 | - } |
|
430 | - |
|
431 | - return '#'; |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Get and increment the `$vocabulary_id`. |
|
436 | - * |
|
437 | - * @return int The incremented vocabulary id. |
|
438 | - * @since 3.18.3 |
|
439 | - */ |
|
440 | - private static function get_and_increment_vocabulary_id() { |
|
441 | - return self::$vocabulary_id ++; |
|
442 | - } |
|
291 | + $vocabulary_id, |
|
292 | + esc_attr( $letter ), |
|
293 | + esc_html( $letter ), |
|
294 | + $this->format_posts_as_list( $posts ) |
|
295 | + ); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Format an array post `$post_id => $post_title` as a list. |
|
300 | + * |
|
301 | + * @param array $posts An array of `$post_id => $post_title` key, value pairs. |
|
302 | + * |
|
303 | + * @return string A list. |
|
304 | + * @since 3.17.0 |
|
305 | + */ |
|
306 | + private function format_posts_as_list( $posts ) { |
|
307 | + |
|
308 | + return array_reduce( |
|
309 | + array_keys( $posts ), |
|
310 | + function ( $carry, $item ) use ( $posts ) { |
|
311 | + return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
312 | + }, |
|
313 | + '' |
|
314 | + ); |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * Get the posts from WordPress using the provided attributes. |
|
319 | + * |
|
320 | + * @param array $atts The shortcode attributes. |
|
321 | + * |
|
322 | + * @return array An array of {@link WP_Post}s. |
|
323 | + * @since 3.17.0 |
|
324 | + */ |
|
325 | + private function get_posts( $atts ) { |
|
326 | + // The default arguments for the query. |
|
327 | + $args = array( |
|
328 | + // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page |
|
329 | + 'posts_per_page' => intval( $atts['limit'] ), |
|
330 | + 'update_post_meta_cache' => false, |
|
331 | + 'update_post_term_cache' => false, |
|
332 | + 'orderby' => $atts['orderby'], |
|
333 | + 'order' => $atts['order'], |
|
334 | + // Exclude the publisher. |
|
335 | + 'post__not_in' => array( Wordlift_Configuration_Service::get_instance()->get_publisher_id() ), |
|
336 | + ); |
|
337 | + |
|
338 | + // Limit the based entity type if needed. |
|
339 | + if ( 'all' !== $atts['type'] ) { |
|
340 | + $args['tax_query'] = array( |
|
341 | + array( |
|
342 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
343 | + 'field' => 'slug', |
|
344 | + 'terms' => $atts['type'], |
|
345 | + ), |
|
346 | + ); |
|
347 | + } |
|
348 | + |
|
349 | + if ( ! empty( $atts['cat'] ) ) { |
|
350 | + $args['cat'] = $atts['cat']; |
|
351 | + } |
|
352 | + |
|
353 | + // Get the posts. Note that if a `type` is specified before, then the |
|
354 | + // `tax_query` from the `add_criterias` call isn't added. |
|
355 | + return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
356 | + |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Populate the alphabet with posts. |
|
361 | + * |
|
362 | + * @param array $alphabet An array of letters. |
|
363 | + * @param int $post_id The {@link WP_Post} id. |
|
364 | + * |
|
365 | + * @since 3.17.0 |
|
366 | + */ |
|
367 | + private function add_to_alphabet( &$alphabet, $post_id ) { |
|
368 | + |
|
369 | + // Get the title without accents. |
|
370 | + $title = $this->get_the_title( $post_id ); |
|
371 | + |
|
372 | + // Get the initial letter. |
|
373 | + $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
374 | + |
|
375 | + // Add the post. |
|
376 | + $alphabet[ $letter ][ $post_id ] = $title; |
|
377 | + |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Get the title without accents. |
|
382 | + * If the post is not of type `entity`, use first synonym if synonyms exists. |
|
383 | + * |
|
384 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1096 |
|
385 | + * |
|
386 | + * @since 3.27.0 |
|
387 | + * |
|
388 | + * @param int $post_id The {@link WP_Post} id. |
|
389 | + * |
|
390 | + * @return string |
|
391 | + */ |
|
392 | + private function get_the_title( $post_id ) { |
|
393 | + |
|
394 | + $title = get_the_title( $post_id ); |
|
395 | + |
|
396 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
397 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
398 | + |
|
399 | + if ( count( $alternative_labels ) > 0 ) { |
|
400 | + $title = $alternative_labels[0]; |
|
401 | + } |
|
402 | + } |
|
403 | + |
|
404 | + return remove_accents( $title ); |
|
405 | + |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * Find the first letter in the alphabet. |
|
410 | + * |
|
411 | + * In some alphabets a letter is a compound of letters, therefore this function |
|
412 | + * will look for groups of 2 or 3 letters in the alphabet before looking for a |
|
413 | + * single letter. In case the letter is not found a # (hash) key is returned. |
|
414 | + * |
|
415 | + * @param array $alphabet An array of alphabet letters. |
|
416 | + * @param string $title The title to match. |
|
417 | + * |
|
418 | + * @return string The initial letter or a `#` key. |
|
419 | + * @since 3.17.0 |
|
420 | + */ |
|
421 | + private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
422 | + |
|
423 | + // Need to handle letters which consist of 3 and 2 characters. |
|
424 | + for ( $i = 3; $i > 0; $i -- ) { |
|
425 | + $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
426 | + if ( isset( $alphabet[ $letter ] ) ) { |
|
427 | + return $letter; |
|
428 | + } |
|
429 | + } |
|
430 | + |
|
431 | + return '#'; |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Get and increment the `$vocabulary_id`. |
|
436 | + * |
|
437 | + * @return int The incremented vocabulary id. |
|
438 | + * @since 3.18.3 |
|
439 | + */ |
|
440 | + private static function get_and_increment_vocabulary_id() { |
|
441 | + return self::$vocabulary_id ++; |
|
442 | + } |
|
443 | 443 | |
444 | 444 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | public function __construct() { |
52 | 52 | parent::__construct(); |
53 | 53 | |
54 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
54 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
55 | 55 | |
56 | 56 | $this->register_block_type(); |
57 | 57 | |
@@ -65,11 +65,11 @@ discard block |
||
65 | 65 | */ |
66 | 66 | private static function are_requirements_satisfied() { |
67 | 67 | |
68 | - return function_exists( 'mb_strlen' ) && |
|
69 | - function_exists( 'mb_substr' ) && |
|
70 | - function_exists( 'mb_strtolower' ) && |
|
71 | - function_exists( 'mb_strtoupper' ) && |
|
72 | - function_exists( 'mb_convert_case' ); |
|
68 | + return function_exists('mb_strlen') && |
|
69 | + function_exists('mb_substr') && |
|
70 | + function_exists('mb_strtolower') && |
|
71 | + function_exists('mb_strtoupper') && |
|
72 | + function_exists('mb_convert_case'); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | /** |
@@ -80,17 +80,17 @@ discard block |
||
80 | 80 | * @return string The output html code. |
81 | 81 | * @since 3.16.0 |
82 | 82 | */ |
83 | - public function render( $atts ) { |
|
83 | + public function render($atts) { |
|
84 | 84 | |
85 | 85 | // Bail out if the requirements aren't satisfied: we need mbstring for |
86 | 86 | // the vocabulary widget to work. |
87 | - if ( ! self::are_requirements_satisfied() ) { |
|
88 | - $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
87 | + if ( ! self::are_requirements_satisfied()) { |
|
88 | + $this->log->warn("The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements."); |
|
89 | 89 | |
90 | 90 | return ''; |
91 | 91 | } |
92 | 92 | |
93 | - wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css', array(), WORDLIFT_VERSION ); |
|
93 | + wp_enqueue_style('wl-vocabulary-shortcode', dirname(plugin_dir_url(__FILE__)).'/public/css/wordlift-vocabulary-shortcode.css', array(), WORDLIFT_VERSION); |
|
94 | 94 | |
95 | 95 | // Extract attributes and set default values. |
96 | 96 | $atts = shortcode_atts( |
@@ -111,15 +111,15 @@ discard block |
||
111 | 111 | |
112 | 112 | // Get the posts. Note that if a `type` is specified before, then the |
113 | 113 | // `tax_query` from the `add_criterias` call isn't added. |
114 | - $posts = $this->get_posts( $atts ); |
|
114 | + $posts = $this->get_posts($atts); |
|
115 | 115 | |
116 | 116 | // Get the alphabet. |
117 | 117 | $language_code = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
118 | - $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
118 | + $alphabet = Wordlift_Alphabet_Service::get($language_code); |
|
119 | 119 | |
120 | 120 | // Add posts to the alphabet. |
121 | - foreach ( $posts as $post ) { |
|
122 | - $this->add_to_alphabet( $alphabet, $post->ID ); |
|
121 | + foreach ($posts as $post) { |
|
122 | + $this->add_to_alphabet($alphabet, $post->ID); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | $header = ''; |
@@ -129,16 +129,16 @@ discard block |
||
129 | 129 | $vocabulary_id = self::get_and_increment_vocabulary_id(); |
130 | 130 | |
131 | 131 | // Generate the header. |
132 | - foreach ( $alphabet as $item => $translations ) { |
|
133 | - $template = ( empty( $translations ) |
|
132 | + foreach ($alphabet as $item => $translations) { |
|
133 | + $template = (empty($translations) |
|
134 | 134 | ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
135 | - : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>' ); |
|
135 | + : '<a href="#wl-vocabulary-%3$d-%2$s">%1$s</a>'); |
|
136 | 136 | |
137 | - $header .= sprintf( $template, esc_html( $item ), esc_attr( $item ), $vocabulary_id ); |
|
137 | + $header .= sprintf($template, esc_html($item), esc_attr($item), $vocabulary_id); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | // Generate the sections. |
141 | - foreach ( $alphabet as $item => $translations ) { |
|
141 | + foreach ($alphabet as $item => $translations) { |
|
142 | 142 | // @since 3.19.3 we use `mb_strtolower` and `mb_strtoupper` with a custom function to handle sorting, |
143 | 143 | // since we had `AB` being placed before `Aa` with `asort`. |
144 | 144 | // |
@@ -146,16 +146,16 @@ discard block |
||
146 | 146 | // asort( $translations ); |
147 | 147 | uasort( |
148 | 148 | $translations, |
149 | - function ( $a, $b ) { |
|
150 | - if ( mb_strtolower( $a ) === mb_strtolower( $b ) |
|
151 | - || mb_strtoupper( $a ) === mb_strtoupper( $b ) ) { |
|
149 | + function($a, $b) { |
|
150 | + if (mb_strtolower($a) === mb_strtolower($b) |
|
151 | + || mb_strtoupper($a) === mb_strtoupper($b)) { |
|
152 | 152 | return 0; |
153 | 153 | } |
154 | 154 | |
155 | - return ( mb_strtolower( $a ) < mb_strtolower( $b ) ) ? - 1 : 1; |
|
155 | + return (mb_strtolower($a) < mb_strtolower($b)) ? -1 : 1; |
|
156 | 156 | } |
157 | 157 | ); |
158 | - $sections .= $this->get_section( $item, $translations, $vocabulary_id ); |
|
158 | + $sections .= $this->get_section($item, $translations, $vocabulary_id); |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | // Return HTML template. |
@@ -167,8 +167,8 @@ discard block |
||
167 | 167 | echo wp_kses( |
168 | 168 | $header, |
169 | 169 | array( |
170 | - 'span' => array( 'class' => array() ), |
|
171 | - 'a' => array( 'href' => array() ), |
|
170 | + 'span' => array('class' => array()), |
|
171 | + 'a' => array('href' => array()), |
|
172 | 172 | ) |
173 | 173 | ); |
174 | 174 | ?> |
@@ -182,10 +182,10 @@ discard block |
||
182 | 182 | 'class' => array(), |
183 | 183 | 'id' => array(), |
184 | 184 | ), |
185 | - 'aside' => array( 'class' => array() ), |
|
186 | - 'ul' => array( 'class' => array() ), |
|
185 | + 'aside' => array('class' => array()), |
|
186 | + 'ul' => array('class' => array()), |
|
187 | 187 | 'li' => array(), |
188 | - 'a' => array( 'href' => array() ), |
|
188 | + 'a' => array('href' => array()), |
|
189 | 189 | ) |
190 | 190 | ); |
191 | 191 | ?> |
@@ -204,8 +204,8 @@ discard block |
||
204 | 204 | |
205 | 205 | add_action( |
206 | 206 | 'init', |
207 | - function () use ( $scope ) { |
|
208 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
207 | + function() use ($scope) { |
|
208 | + if ( ! function_exists('register_block_type')) { |
|
209 | 209 | // Gutenberg is not active. |
210 | 210 | return; |
211 | 211 | } |
@@ -214,13 +214,13 @@ discard block |
||
214 | 214 | 'wordlift/vocabulary', |
215 | 215 | array( |
216 | 216 | 'editor_script' => 'wl-block-editor', |
217 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
217 | + 'render_callback' => function($attributes) use ($scope) { |
|
218 | 218 | $attr_code = ''; |
219 | - foreach ( $attributes as $key => $value ) { |
|
220 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
219 | + foreach ($attributes as $key => $value) { |
|
220 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
221 | 221 | } |
222 | 222 | |
223 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
223 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
224 | 224 | }, |
225 | 225 | 'attributes' => array( |
226 | 226 | 'type' => array( |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | ), |
250 | 250 | 'preview_src' => array( |
251 | 251 | 'type' => 'string', |
252 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/vocabulary.png', |
|
252 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/vocabulary.png', |
|
253 | 253 | ), |
254 | 254 | ), |
255 | 255 | ) |
@@ -270,10 +270,10 @@ discard block |
||
270 | 270 | * no posts). |
271 | 271 | * @since 3.17.0 |
272 | 272 | */ |
273 | - private function get_section( $letter, $posts, $vocabulary_id ) { |
|
273 | + private function get_section($letter, $posts, $vocabulary_id) { |
|
274 | 274 | |
275 | 275 | // Return an empty string if there are no posts. |
276 | - if ( 0 === count( $posts ) ) { |
|
276 | + if (0 === count($posts)) { |
|
277 | 277 | return ''; |
278 | 278 | } |
279 | 279 | |
@@ -289,9 +289,9 @@ discard block |
||
289 | 289 | </div> |
290 | 290 | ', |
291 | 291 | $vocabulary_id, |
292 | - esc_attr( $letter ), |
|
293 | - esc_html( $letter ), |
|
294 | - $this->format_posts_as_list( $posts ) |
|
292 | + esc_attr($letter), |
|
293 | + esc_html($letter), |
|
294 | + $this->format_posts_as_list($posts) |
|
295 | 295 | ); |
296 | 296 | } |
297 | 297 | |
@@ -303,12 +303,12 @@ discard block |
||
303 | 303 | * @return string A list. |
304 | 304 | * @since 3.17.0 |
305 | 305 | */ |
306 | - private function format_posts_as_list( $posts ) { |
|
306 | + private function format_posts_as_list($posts) { |
|
307 | 307 | |
308 | 308 | return array_reduce( |
309 | - array_keys( $posts ), |
|
310 | - function ( $carry, $item ) use ( $posts ) { |
|
311 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
309 | + array_keys($posts), |
|
310 | + function($carry, $item) use ($posts) { |
|
311 | + return $carry.sprintf('<li><a href="%s">%s</a></li>', esc_attr(get_permalink($item)), esc_html($posts[$item])); |
|
312 | 312 | }, |
313 | 313 | '' |
314 | 314 | ); |
@@ -322,21 +322,21 @@ discard block |
||
322 | 322 | * @return array An array of {@link WP_Post}s. |
323 | 323 | * @since 3.17.0 |
324 | 324 | */ |
325 | - private function get_posts( $atts ) { |
|
325 | + private function get_posts($atts) { |
|
326 | 326 | // The default arguments for the query. |
327 | 327 | $args = array( |
328 | 328 | // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page |
329 | - 'posts_per_page' => intval( $atts['limit'] ), |
|
329 | + 'posts_per_page' => intval($atts['limit']), |
|
330 | 330 | 'update_post_meta_cache' => false, |
331 | 331 | 'update_post_term_cache' => false, |
332 | 332 | 'orderby' => $atts['orderby'], |
333 | 333 | 'order' => $atts['order'], |
334 | 334 | // Exclude the publisher. |
335 | - 'post__not_in' => array( Wordlift_Configuration_Service::get_instance()->get_publisher_id() ), |
|
335 | + 'post__not_in' => array(Wordlift_Configuration_Service::get_instance()->get_publisher_id()), |
|
336 | 336 | ); |
337 | 337 | |
338 | 338 | // Limit the based entity type if needed. |
339 | - if ( 'all' !== $atts['type'] ) { |
|
339 | + if ('all' !== $atts['type']) { |
|
340 | 340 | $args['tax_query'] = array( |
341 | 341 | array( |
342 | 342 | 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
@@ -346,13 +346,13 @@ discard block |
||
346 | 346 | ); |
347 | 347 | } |
348 | 348 | |
349 | - if ( ! empty( $atts['cat'] ) ) { |
|
349 | + if ( ! empty($atts['cat'])) { |
|
350 | 350 | $args['cat'] = $atts['cat']; |
351 | 351 | } |
352 | 352 | |
353 | 353 | // Get the posts. Note that if a `type` is specified before, then the |
354 | 354 | // `tax_query` from the `add_criterias` call isn't added. |
355 | - return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
355 | + return get_posts(Wordlift_Entity_Service::add_criterias($args)); |
|
356 | 356 | |
357 | 357 | } |
358 | 358 | |
@@ -364,16 +364,16 @@ discard block |
||
364 | 364 | * |
365 | 365 | * @since 3.17.0 |
366 | 366 | */ |
367 | - private function add_to_alphabet( &$alphabet, $post_id ) { |
|
367 | + private function add_to_alphabet(&$alphabet, $post_id) { |
|
368 | 368 | |
369 | 369 | // Get the title without accents. |
370 | - $title = $this->get_the_title( $post_id ); |
|
370 | + $title = $this->get_the_title($post_id); |
|
371 | 371 | |
372 | 372 | // Get the initial letter. |
373 | - $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
373 | + $letter = $this->get_first_letter_in_alphabet_or_hash($alphabet, $title); |
|
374 | 374 | |
375 | 375 | // Add the post. |
376 | - $alphabet[ $letter ][ $post_id ] = $title; |
|
376 | + $alphabet[$letter][$post_id] = $title; |
|
377 | 377 | |
378 | 378 | } |
379 | 379 | |
@@ -389,19 +389,19 @@ discard block |
||
389 | 389 | * |
390 | 390 | * @return string |
391 | 391 | */ |
392 | - private function get_the_title( $post_id ) { |
|
392 | + private function get_the_title($post_id) { |
|
393 | 393 | |
394 | - $title = get_the_title( $post_id ); |
|
394 | + $title = get_the_title($post_id); |
|
395 | 395 | |
396 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
397 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
396 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
397 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
398 | 398 | |
399 | - if ( count( $alternative_labels ) > 0 ) { |
|
399 | + if (count($alternative_labels) > 0) { |
|
400 | 400 | $title = $alternative_labels[0]; |
401 | 401 | } |
402 | 402 | } |
403 | 403 | |
404 | - return remove_accents( $title ); |
|
404 | + return remove_accents($title); |
|
405 | 405 | |
406 | 406 | } |
407 | 407 | |
@@ -418,12 +418,12 @@ discard block |
||
418 | 418 | * @return string The initial letter or a `#` key. |
419 | 419 | * @since 3.17.0 |
420 | 420 | */ |
421 | - private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
421 | + private function get_first_letter_in_alphabet_or_hash($alphabet, $title) { |
|
422 | 422 | |
423 | 423 | // Need to handle letters which consist of 3 and 2 characters. |
424 | - for ( $i = 3; $i > 0; $i -- ) { |
|
425 | - $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
426 | - if ( isset( $alphabet[ $letter ] ) ) { |
|
424 | + for ($i = 3; $i > 0; $i--) { |
|
425 | + $letter = mb_convert_case(mb_substr($title, 0, $i), MB_CASE_UPPER); |
|
426 | + if (isset($alphabet[$letter])) { |
|
427 | 427 | return $letter; |
428 | 428 | } |
429 | 429 | } |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | * @since 3.18.3 |
439 | 439 | */ |
440 | 440 | private static function get_and_increment_vocabulary_id() { |
441 | - return self::$vocabulary_id ++; |
|
441 | + return self::$vocabulary_id++; |
|
442 | 442 | } |
443 | 443 | |
444 | 444 | } |