@@ -18,30 +18,30 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | function wl_shortcode_faceted_search( $request ) { |
| 20 | 20 | |
| 21 | - // Create the cache key. |
|
| 22 | - $cache_key = array( |
|
| 23 | - 'request_params' => $_GET, |
|
| 24 | - ); |
|
| 21 | + // Create the cache key. |
|
| 22 | + $cache_key = array( |
|
| 23 | + 'request_params' => $_GET, |
|
| 24 | + ); |
|
| 25 | 25 | |
| 26 | - // Create the TTL cache and try to get the results. |
|
| 27 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 28 | - $cache_results = $cache->get( $cache_key ); |
|
| 26 | + // Create the TTL cache and try to get the results. |
|
| 27 | + $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 28 | + $cache_results = $cache->get( $cache_key ); |
|
| 29 | 29 | |
| 30 | - if ( isset( $cache_results ) ) { |
|
| 31 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 32 | - wl_core_send_json( $cache_results ); |
|
| 30 | + if ( isset( $cache_results ) ) { |
|
| 31 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 32 | + wl_core_send_json( $cache_results ); |
|
| 33 | 33 | |
| 34 | - return; |
|
| 35 | - } |
|
| 34 | + return; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 37 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 38 | 38 | |
| 39 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 39 | + $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 40 | 40 | |
| 41 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 42 | - $cache->put( $cache_key, $results ); |
|
| 41 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 42 | + $cache->put( $cache_key, $results ); |
|
| 43 | 43 | |
| 44 | - wl_core_send_json( $results ); |
|
| 44 | + wl_core_send_json( $results ); |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | |
@@ -52,128 +52,128 @@ discard block |
||
| 52 | 52 | * @since 3.26.0 |
| 53 | 53 | */ |
| 54 | 54 | function wl_shortcode_faceted_search_origin( $request ) { |
| 55 | - // Post ID must be defined. |
|
| 56 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 57 | - wp_die( 'No post_id given' ); |
|
| 58 | - |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 63 | - $current_post = get_post( $current_post_id ); |
|
| 64 | - |
|
| 65 | - // Post ID has to match an existing item. |
|
| 66 | - if ( null === $current_post ) { |
|
| 67 | - wp_die( 'No valid post_id given' ); |
|
| 68 | - |
|
| 69 | - return; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // If the current post is an entity, |
|
| 73 | - // the current post is used as main entity. |
|
| 74 | - // Otherwise, current post related entities are used. |
|
| 75 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 76 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 77 | - array( $current_post->ID ) : |
|
| 78 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 79 | - |
|
| 80 | - // If there are no entities we cannot render the widget. |
|
| 81 | - if ( 0 === count( $entity_ids ) ) { |
|
| 82 | - wp_die( 'No entities available' ); |
|
| 83 | - |
|
| 84 | - return; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 88 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 93 | - * The ordering should be descending by date on default. |
|
| 94 | - */ |
|
| 95 | - $order_by = 'DESC'; |
|
| 96 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 97 | - $order_by = (string) $_GET['sort']; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 101 | - $entity_ids, |
|
| 102 | - '*', |
|
| 103 | - null, |
|
| 104 | - 'publish', |
|
| 105 | - array( $current_post_id ), |
|
| 106 | - $limit, |
|
| 107 | - null, |
|
| 108 | - $order_by |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 112 | - return $p->ID; |
|
| 113 | - }, $referencing_posts ); |
|
| 114 | - |
|
| 115 | - $post_results = array(); |
|
| 116 | - $entity_results = array(); |
|
| 117 | - |
|
| 118 | - // Populate $post_results |
|
| 119 | - |
|
| 120 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 121 | - $referencing_posts : |
|
| 122 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 123 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 124 | - '*', |
|
| 125 | - null, |
|
| 126 | - null, |
|
| 127 | - array(), |
|
| 128 | - null, |
|
| 129 | - $referencing_post_ids |
|
| 130 | - ); |
|
| 131 | - |
|
| 132 | - if ( $filtered_posts ) { |
|
| 133 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Use the thumbnail. |
|
| 137 | - * |
|
| 138 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 139 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 140 | - * |
|
| 141 | - * @since 3.19.3 We're using the medium size image. |
|
| 142 | - */ |
|
| 143 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 144 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 145 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 146 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 147 | - |
|
| 148 | - $result = $post_obj; |
|
| 149 | - $post_results[] = $result; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // Add filler posts if needed |
|
| 154 | - |
|
| 155 | - $filler_count = $limit - count( $post_results ); |
|
| 156 | - $filler_posts = wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ); |
|
| 157 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 158 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 159 | - return $post->ID; |
|
| 160 | - }, $post_results ); |
|
| 161 | - |
|
| 162 | - // Populate $entity_results |
|
| 163 | - |
|
| 164 | - global $wpdb; |
|
| 165 | - |
|
| 166 | - // Retrieve Wordlift relation instances table name. |
|
| 167 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 168 | - |
|
| 169 | - /* |
|
| 55 | + // Post ID must be defined. |
|
| 56 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 57 | + wp_die( 'No post_id given' ); |
|
| 58 | + |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 63 | + $current_post = get_post( $current_post_id ); |
|
| 64 | + |
|
| 65 | + // Post ID has to match an existing item. |
|
| 66 | + if ( null === $current_post ) { |
|
| 67 | + wp_die( 'No valid post_id given' ); |
|
| 68 | + |
|
| 69 | + return; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // If the current post is an entity, |
|
| 73 | + // the current post is used as main entity. |
|
| 74 | + // Otherwise, current post related entities are used. |
|
| 75 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 76 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 77 | + array( $current_post->ID ) : |
|
| 78 | + $entity_service->get_related_entities( $current_post->ID ); |
|
| 79 | + |
|
| 80 | + // If there are no entities we cannot render the widget. |
|
| 81 | + if ( 0 === count( $entity_ids ) ) { |
|
| 82 | + wp_die( 'No entities available' ); |
|
| 83 | + |
|
| 84 | + return; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 88 | + $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 93 | + * The ordering should be descending by date on default. |
|
| 94 | + */ |
|
| 95 | + $order_by = 'DESC'; |
|
| 96 | + if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 97 | + $order_by = (string) $_GET['sort']; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 101 | + $entity_ids, |
|
| 102 | + '*', |
|
| 103 | + null, |
|
| 104 | + 'publish', |
|
| 105 | + array( $current_post_id ), |
|
| 106 | + $limit, |
|
| 107 | + null, |
|
| 108 | + $order_by |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 112 | + return $p->ID; |
|
| 113 | + }, $referencing_posts ); |
|
| 114 | + |
|
| 115 | + $post_results = array(); |
|
| 116 | + $entity_results = array(); |
|
| 117 | + |
|
| 118 | + // Populate $post_results |
|
| 119 | + |
|
| 120 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 121 | + $referencing_posts : |
|
| 122 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 123 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 124 | + '*', |
|
| 125 | + null, |
|
| 126 | + null, |
|
| 127 | + array(), |
|
| 128 | + null, |
|
| 129 | + $referencing_post_ids |
|
| 130 | + ); |
|
| 131 | + |
|
| 132 | + if ( $filtered_posts ) { |
|
| 133 | + foreach ( $filtered_posts as $post_obj ) { |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Use the thumbnail. |
|
| 137 | + * |
|
| 138 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 139 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 140 | + * |
|
| 141 | + * @since 3.19.3 We're using the medium size image. |
|
| 142 | + */ |
|
| 143 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 144 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 145 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 146 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 147 | + |
|
| 148 | + $result = $post_obj; |
|
| 149 | + $post_results[] = $result; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // Add filler posts if needed |
|
| 154 | + |
|
| 155 | + $filler_count = $limit - count( $post_results ); |
|
| 156 | + $filler_posts = wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ); |
|
| 157 | + $post_results = array_merge( $post_results, $filler_posts ); |
|
| 158 | + $referencing_post_ids = array_map( function ( $post ) { |
|
| 159 | + return $post->ID; |
|
| 160 | + }, $post_results ); |
|
| 161 | + |
|
| 162 | + // Populate $entity_results |
|
| 163 | + |
|
| 164 | + global $wpdb; |
|
| 165 | + |
|
| 166 | + // Retrieve Wordlift relation instances table name. |
|
| 167 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 168 | + |
|
| 169 | + /* |
|
| 170 | 170 | * Make sure we have some referenced post, otherwise the IN parts of |
| 171 | 171 | * the SQL will produce an SQL error. |
| 172 | 172 | */ |
| 173 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 174 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 173 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 174 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 175 | 175 | |
| 176 | - $query = " |
|
| 176 | + $query = " |
|
| 177 | 177 | SELECT |
| 178 | 178 | object_id AS ID, |
| 179 | 179 | count( object_id ) AS counter |
@@ -185,116 +185,116 @@ discard block |
||
| 185 | 185 | LIMIT $limit; |
| 186 | 186 | "; |
| 187 | 187 | |
| 188 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 188 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 189 | 189 | |
| 190 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 190 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 191 | 191 | |
| 192 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 192 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 193 | 193 | |
| 194 | - foreach ( $entities as $obj ) { |
|
| 194 | + foreach ( $entities as $obj ) { |
|
| 195 | 195 | |
| 196 | - $entity = get_post( $obj->ID ); |
|
| 196 | + $entity = get_post( $obj->ID ); |
|
| 197 | 197 | |
| 198 | - // Ensure only valid and published entities are returned. |
|
| 199 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 198 | + // Ensure only valid and published entities are returned. |
|
| 199 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 200 | 200 | |
| 201 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 202 | - $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 203 | - $serialized_entity['counter'] = $obj->counter; |
|
| 204 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 205 | - $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 206 | - $obj->ID, |
|
| 207 | - 'ids', |
|
| 208 | - null, |
|
| 209 | - null, |
|
| 210 | - array(), |
|
| 211 | - null, |
|
| 212 | - $referencing_post_ids |
|
| 213 | - ); |
|
| 214 | - $entity_results[] = $serialized_entity; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 201 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 202 | + $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 203 | + $serialized_entity['counter'] = $obj->counter; |
|
| 204 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 205 | + $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 206 | + $obj->ID, |
|
| 207 | + 'ids', |
|
| 208 | + null, |
|
| 209 | + null, |
|
| 210 | + array(), |
|
| 211 | + null, |
|
| 212 | + $referencing_post_ids |
|
| 213 | + ); |
|
| 214 | + $entity_results[] = $serialized_entity; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - return array( |
|
| 220 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 221 | - 'entities' => $entity_results |
|
| 222 | - ); |
|
| 219 | + return array( |
|
| 220 | + 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 221 | + 'entities' => $entity_results |
|
| 222 | + ); |
|
| 223 | 223 | |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
| 227 | 227 | |
| 228 | - $title = get_the_title( $post_id ); |
|
| 228 | + $title = get_the_title( $post_id ); |
|
| 229 | 229 | |
| 230 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 231 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 230 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 231 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 232 | 232 | |
| 233 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 234 | - $title = $alternative_labels[0]; |
|
| 235 | - } |
|
| 236 | - } |
|
| 233 | + if ( count( $alternative_labels ) > 0 ) { |
|
| 234 | + $title = $alternative_labels[0]; |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - return remove_accents( $title ); |
|
| 238 | + return remove_accents( $title ); |
|
| 239 | 239 | |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | function wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ) { |
| 243 | 243 | |
| 244 | - $filler_posts = array(); |
|
| 245 | - |
|
| 246 | - // First add latest posts from same categories as the current post |
|
| 247 | - if ( $filler_count > 0 ) { |
|
| 248 | - |
|
| 249 | - $current_post_categories = wp_get_post_categories( $current_post_id ); |
|
| 250 | - |
|
| 251 | - $args = array( |
|
| 252 | - 'meta_query' => array( |
|
| 253 | - array( |
|
| 254 | - 'key' => '_thumbnail_id' |
|
| 255 | - ) |
|
| 256 | - ), |
|
| 257 | - 'category__in' => $current_post_categories, |
|
| 258 | - 'numberposts' => $filler_count, |
|
| 259 | - 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids ), |
|
| 260 | - 'ignore_sticky_posts' => 1 |
|
| 261 | - ); |
|
| 262 | - |
|
| 263 | - $filler_posts = get_posts( $args ); |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $filler_count = $filler_count - count( $filler_posts ); |
|
| 267 | - $filler_post_ids = array_map( function ( $post ) { |
|
| 268 | - return $post->ID; |
|
| 269 | - }, $filler_posts ); |
|
| 270 | - |
|
| 271 | - // If that does not fill, add latest posts irrespective of category |
|
| 272 | - if ( $filler_count > 0 ) { |
|
| 273 | - |
|
| 274 | - $args = array( |
|
| 275 | - 'meta_query' => array( |
|
| 276 | - array( |
|
| 277 | - 'key' => '_thumbnail_id' |
|
| 278 | - ) |
|
| 279 | - ), |
|
| 280 | - 'numberposts' => $filler_count, |
|
| 281 | - 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids, $filler_post_ids ), |
|
| 282 | - 'ignore_sticky_posts' => 1 |
|
| 283 | - ); |
|
| 284 | - |
|
| 285 | - $filler_posts = array_merge( $filler_posts, get_posts( $args ) ); |
|
| 286 | - |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - // Add thumbnail and permalink to filler posts |
|
| 290 | - foreach ( $filler_posts as $post_obj ) { |
|
| 291 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 292 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 293 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 294 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return $filler_posts; |
|
| 244 | + $filler_posts = array(); |
|
| 245 | + |
|
| 246 | + // First add latest posts from same categories as the current post |
|
| 247 | + if ( $filler_count > 0 ) { |
|
| 248 | + |
|
| 249 | + $current_post_categories = wp_get_post_categories( $current_post_id ); |
|
| 250 | + |
|
| 251 | + $args = array( |
|
| 252 | + 'meta_query' => array( |
|
| 253 | + array( |
|
| 254 | + 'key' => '_thumbnail_id' |
|
| 255 | + ) |
|
| 256 | + ), |
|
| 257 | + 'category__in' => $current_post_categories, |
|
| 258 | + 'numberposts' => $filler_count, |
|
| 259 | + 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids ), |
|
| 260 | + 'ignore_sticky_posts' => 1 |
|
| 261 | + ); |
|
| 262 | + |
|
| 263 | + $filler_posts = get_posts( $args ); |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $filler_count = $filler_count - count( $filler_posts ); |
|
| 267 | + $filler_post_ids = array_map( function ( $post ) { |
|
| 268 | + return $post->ID; |
|
| 269 | + }, $filler_posts ); |
|
| 270 | + |
|
| 271 | + // If that does not fill, add latest posts irrespective of category |
|
| 272 | + if ( $filler_count > 0 ) { |
|
| 273 | + |
|
| 274 | + $args = array( |
|
| 275 | + 'meta_query' => array( |
|
| 276 | + array( |
|
| 277 | + 'key' => '_thumbnail_id' |
|
| 278 | + ) |
|
| 279 | + ), |
|
| 280 | + 'numberposts' => $filler_count, |
|
| 281 | + 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids, $filler_post_ids ), |
|
| 282 | + 'ignore_sticky_posts' => 1 |
|
| 283 | + ); |
|
| 284 | + |
|
| 285 | + $filler_posts = array_merge( $filler_posts, get_posts( $args ) ); |
|
| 286 | + |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + // Add thumbnail and permalink to filler posts |
|
| 290 | + foreach ( $filler_posts as $post_obj ) { |
|
| 291 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 292 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 293 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 294 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return $filler_posts; |
|
| 298 | 298 | |
| 299 | 299 | } |
| 300 | 300 | |
@@ -302,8 +302,8 @@ discard block |
||
| 302 | 302 | * Adding `rest_api_init` action for network faceted-search |
| 303 | 303 | */ |
| 304 | 304 | add_action( 'rest_api_init', function () { |
| 305 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 306 | - 'methods' => 'GET', |
|
| 307 | - 'callback' => 'wl_shortcode_faceted_search', |
|
| 308 | - ) ); |
|
| 305 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 306 | + 'methods' => 'GET', |
|
| 307 | + 'callback' => 'wl_shortcode_faceted_search', |
|
| 308 | + ) ); |
|
| 309 | 309 | } ); |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @since 3.21.4 fix the cache key by also using the request body. |
| 17 | 17 | * @since 3.21.2 add a caching layer. |
| 18 | 18 | */ |
| 19 | -function wl_shortcode_faceted_search( $request ) { |
|
| 19 | +function wl_shortcode_faceted_search($request) { |
|
| 20 | 20 | |
| 21 | 21 | // Create the cache key. |
| 22 | 22 | $cache_key = array( |
@@ -24,24 +24,24 @@ discard block |
||
| 24 | 24 | ); |
| 25 | 25 | |
| 26 | 26 | // Create the TTL cache and try to get the results. |
| 27 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 28 | - $cache_results = $cache->get( $cache_key ); |
|
| 27 | + $cache = new Ttl_Cache("faceted-search", 8 * 60 * 60); // 8 hours. |
|
| 28 | + $cache_results = $cache->get($cache_key); |
|
| 29 | 29 | |
| 30 | - if ( isset( $cache_results ) ) { |
|
| 31 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 32 | - wl_core_send_json( $cache_results ); |
|
| 30 | + if (isset($cache_results)) { |
|
| 31 | + header('X-WordLift-Cache: HIT'); |
|
| 32 | + wl_core_send_json($cache_results); |
|
| 33 | 33 | |
| 34 | 34 | return; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 37 | + header('X-WordLift-Cache: MISS'); |
|
| 38 | 38 | |
| 39 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 39 | + $results = wl_shortcode_faceted_search_origin($request); |
|
| 40 | 40 | |
| 41 | 41 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 42 | - $cache->put( $cache_key, $results ); |
|
| 42 | + $cache->put($cache_key, $results); |
|
| 43 | 43 | |
| 44 | - wl_core_send_json( $results ); |
|
| 44 | + wl_core_send_json($results); |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | |
@@ -51,20 +51,20 @@ discard block |
||
| 51 | 51 | * @return array $results |
| 52 | 52 | * @since 3.26.0 |
| 53 | 53 | */ |
| 54 | -function wl_shortcode_faceted_search_origin( $request ) { |
|
| 54 | +function wl_shortcode_faceted_search_origin($request) { |
|
| 55 | 55 | // Post ID must be defined. |
| 56 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 57 | - wp_die( 'No post_id given' ); |
|
| 56 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 57 | + wp_die('No post_id given'); |
|
| 58 | 58 | |
| 59 | 59 | return; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 63 | - $current_post = get_post( $current_post_id ); |
|
| 63 | + $current_post = get_post($current_post_id); |
|
| 64 | 64 | |
| 65 | 65 | // Post ID has to match an existing item. |
| 66 | - if ( null === $current_post ) { |
|
| 67 | - wp_die( 'No valid post_id given' ); |
|
| 66 | + if (null === $current_post) { |
|
| 67 | + wp_die('No valid post_id given'); |
|
| 68 | 68 | |
| 69 | 69 | return; |
| 70 | 70 | } |
@@ -73,19 +73,18 @@ discard block |
||
| 73 | 73 | // the current post is used as main entity. |
| 74 | 74 | // Otherwise, current post related entities are used. |
| 75 | 75 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 76 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 77 | - array( $current_post->ID ) : |
|
| 78 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 76 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 77 | + array($current_post->ID) : $entity_service->get_related_entities($current_post->ID); |
|
| 79 | 78 | |
| 80 | 79 | // If there are no entities we cannot render the widget. |
| 81 | - if ( 0 === count( $entity_ids ) ) { |
|
| 82 | - wp_die( 'No entities available' ); |
|
| 80 | + if (0 === count($entity_ids)) { |
|
| 81 | + wp_die('No entities available'); |
|
| 83 | 82 | |
| 84 | 83 | return; |
| 85 | 84 | } |
| 86 | 85 | |
| 87 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 88 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 86 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 87 | + $amp = (isset($_GET['amp'])) ? true : false; |
|
| 89 | 88 | |
| 90 | 89 | |
| 91 | 90 | /** |
@@ -93,7 +92,7 @@ discard block |
||
| 93 | 92 | * The ordering should be descending by date on default. |
| 94 | 93 | */ |
| 95 | 94 | $order_by = 'DESC'; |
| 96 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 95 | + if (isset($_GET['sort']) && is_string($_GET['sort'])) { |
|
| 97 | 96 | $order_by = (string) $_GET['sort']; |
| 98 | 97 | } |
| 99 | 98 | |
@@ -102,25 +101,24 @@ discard block |
||
| 102 | 101 | '*', |
| 103 | 102 | null, |
| 104 | 103 | 'publish', |
| 105 | - array( $current_post_id ), |
|
| 104 | + array($current_post_id), |
|
| 106 | 105 | $limit, |
| 107 | 106 | null, |
| 108 | 107 | $order_by |
| 109 | 108 | ); |
| 110 | 109 | |
| 111 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 110 | + $referencing_post_ids = array_map(function($p) { |
|
| 112 | 111 | return $p->ID; |
| 113 | - }, $referencing_posts ); |
|
| 112 | + }, $referencing_posts); |
|
| 114 | 113 | |
| 115 | 114 | $post_results = array(); |
| 116 | 115 | $entity_results = array(); |
| 117 | 116 | |
| 118 | 117 | // Populate $post_results |
| 119 | 118 | |
| 120 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 121 | - $referencing_posts : |
|
| 122 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 123 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 119 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 120 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 121 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 124 | 122 | '*', |
| 125 | 123 | null, |
| 126 | 124 | null, |
@@ -129,8 +127,8 @@ discard block |
||
| 129 | 127 | $referencing_post_ids |
| 130 | 128 | ); |
| 131 | 129 | |
| 132 | - if ( $filtered_posts ) { |
|
| 133 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 130 | + if ($filtered_posts) { |
|
| 131 | + foreach ($filtered_posts as $post_obj) { |
|
| 134 | 132 | |
| 135 | 133 | /** |
| 136 | 134 | * Use the thumbnail. |
@@ -140,10 +138,10 @@ discard block |
||
| 140 | 138 | * |
| 141 | 139 | * @since 3.19.3 We're using the medium size image. |
| 142 | 140 | */ |
| 143 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 144 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 141 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 142 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 145 | 143 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 146 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 144 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
| 147 | 145 | |
| 148 | 146 | $result = $post_obj; |
| 149 | 147 | $post_results[] = $result; |
@@ -152,12 +150,12 @@ discard block |
||
| 152 | 150 | |
| 153 | 151 | // Add filler posts if needed |
| 154 | 152 | |
| 155 | - $filler_count = $limit - count( $post_results ); |
|
| 156 | - $filler_posts = wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ); |
|
| 157 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 158 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 153 | + $filler_count = $limit - count($post_results); |
|
| 154 | + $filler_posts = wl_shortcode_faceted_search_filler_posts($filler_count, $current_post_id, $referencing_post_ids); |
|
| 155 | + $post_results = array_merge($post_results, $filler_posts); |
|
| 156 | + $referencing_post_ids = array_map(function($post) { |
|
| 159 | 157 | return $post->ID; |
| 160 | - }, $post_results ); |
|
| 158 | + }, $post_results); |
|
| 161 | 159 | |
| 162 | 160 | // Populate $entity_results |
| 163 | 161 | |
@@ -170,8 +168,8 @@ discard block |
||
| 170 | 168 | * Make sure we have some referenced post, otherwise the IN parts of |
| 171 | 169 | * the SQL will produce an SQL error. |
| 172 | 170 | */ |
| 173 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 174 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 171 | + if ( ! empty($referencing_post_ids)) { |
|
| 172 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 175 | 173 | |
| 176 | 174 | $query = " |
| 177 | 175 | SELECT |
@@ -185,21 +183,21 @@ discard block |
||
| 185 | 183 | LIMIT $limit; |
| 186 | 184 | "; |
| 187 | 185 | |
| 188 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 186 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 189 | 187 | |
| 190 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 188 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 191 | 189 | |
| 192 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 190 | + wl_write_log('Entities found '.count($entities)); |
|
| 193 | 191 | |
| 194 | - foreach ( $entities as $obj ) { |
|
| 192 | + foreach ($entities as $obj) { |
|
| 195 | 193 | |
| 196 | - $entity = get_post( $obj->ID ); |
|
| 194 | + $entity = get_post($obj->ID); |
|
| 197 | 195 | |
| 198 | 196 | // Ensure only valid and published entities are returned. |
| 199 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 197 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 200 | 198 | |
| 201 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 202 | - $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 199 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 200 | + $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title($obj->ID); |
|
| 203 | 201 | $serialized_entity['counter'] = $obj->counter; |
| 204 | 202 | $serialized_entity['createdAt'] = $entity->post_date; |
| 205 | 203 | $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -211,42 +209,42 @@ discard block |
||
| 211 | 209 | null, |
| 212 | 210 | $referencing_post_ids |
| 213 | 211 | ); |
| 214 | - $entity_results[] = $serialized_entity; |
|
| 212 | + $entity_results[] = $serialized_entity; |
|
| 215 | 213 | } |
| 216 | 214 | } |
| 217 | 215 | } |
| 218 | 216 | |
| 219 | 217 | return array( |
| 220 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 218 | + 'posts' => $amp ? array(array('values' => $post_results)) : $post_results, |
|
| 221 | 219 | 'entities' => $entity_results |
| 222 | 220 | ); |
| 223 | 221 | |
| 224 | 222 | } |
| 225 | 223 | |
| 226 | -function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
|
| 224 | +function wl_shortcode_faceted_search_get_the_title($post_id) { |
|
| 227 | 225 | |
| 228 | - $title = get_the_title( $post_id ); |
|
| 226 | + $title = get_the_title($post_id); |
|
| 229 | 227 | |
| 230 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 231 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 228 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
| 229 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
| 232 | 230 | |
| 233 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 231 | + if (count($alternative_labels) > 0) { |
|
| 234 | 232 | $title = $alternative_labels[0]; |
| 235 | 233 | } |
| 236 | 234 | } |
| 237 | 235 | |
| 238 | - return remove_accents( $title ); |
|
| 236 | + return remove_accents($title); |
|
| 239 | 237 | |
| 240 | 238 | } |
| 241 | 239 | |
| 242 | -function wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ) { |
|
| 240 | +function wl_shortcode_faceted_search_filler_posts($filler_count, $current_post_id, $referencing_post_ids) { |
|
| 243 | 241 | |
| 244 | 242 | $filler_posts = array(); |
| 245 | 243 | |
| 246 | 244 | // First add latest posts from same categories as the current post |
| 247 | - if ( $filler_count > 0 ) { |
|
| 245 | + if ($filler_count > 0) { |
|
| 248 | 246 | |
| 249 | - $current_post_categories = wp_get_post_categories( $current_post_id ); |
|
| 247 | + $current_post_categories = wp_get_post_categories($current_post_id); |
|
| 250 | 248 | |
| 251 | 249 | $args = array( |
| 252 | 250 | 'meta_query' => array( |
@@ -256,20 +254,20 @@ discard block |
||
| 256 | 254 | ), |
| 257 | 255 | 'category__in' => $current_post_categories, |
| 258 | 256 | 'numberposts' => $filler_count, |
| 259 | - 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids ), |
|
| 257 | + 'post__not_in' => array_merge(array($current_post_id), $referencing_post_ids), |
|
| 260 | 258 | 'ignore_sticky_posts' => 1 |
| 261 | 259 | ); |
| 262 | 260 | |
| 263 | - $filler_posts = get_posts( $args ); |
|
| 261 | + $filler_posts = get_posts($args); |
|
| 264 | 262 | } |
| 265 | 263 | |
| 266 | - $filler_count = $filler_count - count( $filler_posts ); |
|
| 267 | - $filler_post_ids = array_map( function ( $post ) { |
|
| 264 | + $filler_count = $filler_count - count($filler_posts); |
|
| 265 | + $filler_post_ids = array_map(function($post) { |
|
| 268 | 266 | return $post->ID; |
| 269 | - }, $filler_posts ); |
|
| 267 | + }, $filler_posts); |
|
| 270 | 268 | |
| 271 | 269 | // If that does not fill, add latest posts irrespective of category |
| 272 | - if ( $filler_count > 0 ) { |
|
| 270 | + if ($filler_count > 0) { |
|
| 273 | 271 | |
| 274 | 272 | $args = array( |
| 275 | 273 | 'meta_query' => array( |
@@ -278,20 +276,20 @@ discard block |
||
| 278 | 276 | ) |
| 279 | 277 | ), |
| 280 | 278 | 'numberposts' => $filler_count, |
| 281 | - 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids, $filler_post_ids ), |
|
| 279 | + 'post__not_in' => array_merge(array($current_post_id), $referencing_post_ids, $filler_post_ids), |
|
| 282 | 280 | 'ignore_sticky_posts' => 1 |
| 283 | 281 | ); |
| 284 | 282 | |
| 285 | - $filler_posts = array_merge( $filler_posts, get_posts( $args ) ); |
|
| 283 | + $filler_posts = array_merge($filler_posts, get_posts($args)); |
|
| 286 | 284 | |
| 287 | 285 | } |
| 288 | 286 | |
| 289 | 287 | // Add thumbnail and permalink to filler posts |
| 290 | - foreach ( $filler_posts as $post_obj ) { |
|
| 291 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 292 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 288 | + foreach ($filler_posts as $post_obj) { |
|
| 289 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 290 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 293 | 291 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 294 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 292 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
| 295 | 293 | } |
| 296 | 294 | |
| 297 | 295 | return $filler_posts; |
@@ -301,9 +299,9 @@ discard block |
||
| 301 | 299 | /** |
| 302 | 300 | * Adding `rest_api_init` action for network faceted-search |
| 303 | 301 | */ |
| 304 | -add_action( 'rest_api_init', function () { |
|
| 305 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 302 | +add_action('rest_api_init', function() { |
|
| 303 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 306 | 304 | 'methods' => 'GET', |
| 307 | 305 | 'callback' => 'wl_shortcode_faceted_search', |
| 308 | - ) ); |
|
| 306 | + )); |
|
| 309 | 307 | } ); |
@@ -18,398 +18,398 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Relation_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The singleton instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Relation_Service $instance The singleton instance. |
|
| 27 | - */ |
|
| 28 | - private static $instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The relation table name in MySQL, set during instantiation. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var string $relation_table The relation table name. |
|
| 36 | - */ |
|
| 37 | - private $relation_table; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * A {@link Wordlift_Log_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.3 |
|
| 43 | - * |
|
| 44 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private static $log; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Create a {@link Wordlift_Relation_Service} instance. |
|
| 50 | - * |
|
| 51 | - * @since 3.15.0 |
|
| 52 | - */ |
|
| 53 | - public function __construct() { |
|
| 54 | - |
|
| 55 | - self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | - |
|
| 57 | - global $wpdb; |
|
| 58 | - |
|
| 59 | - // The relations table. |
|
| 60 | - $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 61 | - |
|
| 62 | - self::$instance = $this; |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the singleton instance. |
|
| 68 | - * |
|
| 69 | - * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
|
| 70 | - * singleton instance. |
|
| 71 | - * @since 3.15.0 |
|
| 72 | - * @access public |
|
| 73 | - */ |
|
| 74 | - public static function get_instance() { |
|
| 75 | - |
|
| 76 | - return self::$instance; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 81 | - * |
|
| 82 | - * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 83 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 84 | - * '*' to return all fields, by default '*'. |
|
| 85 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 86 | - * @param null|string $status The status, by default all. |
|
| 87 | - * @param array $excludes An array of ids to exclude from the results. |
|
| 88 | - * @param null|int $limit The maximum number of results, by default |
|
| 89 | - * no limit. |
|
| 90 | - * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 91 | - * |
|
| 92 | - * @param null | string $order_by |
|
| 93 | - * |
|
| 94 | - * @return array|object|null Database query results |
|
| 95 | - * @since 3.15.0 |
|
| 96 | - * |
|
| 97 | - */ |
|
| 98 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null ) { |
|
| 99 | - global $wpdb; |
|
| 100 | - |
|
| 101 | - // The output fields. |
|
| 102 | - $actual_fields = self::fields( $fields ); |
|
| 103 | - |
|
| 104 | - self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 105 | - |
|
| 106 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 107 | - |
|
| 108 | - // If there are no related objects, return an empty array. |
|
| 109 | - if ( empty( $objects ) ) { |
|
| 110 | - self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 111 | - |
|
| 112 | - return array(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 116 | - |
|
| 117 | - $sql = |
|
| 118 | - " |
|
| 21 | + /** |
|
| 22 | + * The singleton instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Relation_Service $instance The singleton instance. |
|
| 27 | + */ |
|
| 28 | + private static $instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The relation table name in MySQL, set during instantiation. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var string $relation_table The relation table name. |
|
| 36 | + */ |
|
| 37 | + private $relation_table; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * A {@link Wordlift_Log_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.3 |
|
| 43 | + * |
|
| 44 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private static $log; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Create a {@link Wordlift_Relation_Service} instance. |
|
| 50 | + * |
|
| 51 | + * @since 3.15.0 |
|
| 52 | + */ |
|
| 53 | + public function __construct() { |
|
| 54 | + |
|
| 55 | + self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | + |
|
| 57 | + global $wpdb; |
|
| 58 | + |
|
| 59 | + // The relations table. |
|
| 60 | + $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 61 | + |
|
| 62 | + self::$instance = $this; |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the singleton instance. |
|
| 68 | + * |
|
| 69 | + * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
|
| 70 | + * singleton instance. |
|
| 71 | + * @since 3.15.0 |
|
| 72 | + * @access public |
|
| 73 | + */ |
|
| 74 | + public static function get_instance() { |
|
| 75 | + |
|
| 76 | + return self::$instance; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 81 | + * |
|
| 82 | + * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 83 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 84 | + * '*' to return all fields, by default '*'. |
|
| 85 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 86 | + * @param null|string $status The status, by default all. |
|
| 87 | + * @param array $excludes An array of ids to exclude from the results. |
|
| 88 | + * @param null|int $limit The maximum number of results, by default |
|
| 89 | + * no limit. |
|
| 90 | + * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 91 | + * |
|
| 92 | + * @param null | string $order_by |
|
| 93 | + * |
|
| 94 | + * @return array|object|null Database query results |
|
| 95 | + * @since 3.15.0 |
|
| 96 | + * |
|
| 97 | + */ |
|
| 98 | + public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null ) { |
|
| 99 | + global $wpdb; |
|
| 100 | + |
|
| 101 | + // The output fields. |
|
| 102 | + $actual_fields = self::fields( $fields ); |
|
| 103 | + |
|
| 104 | + self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 105 | + |
|
| 106 | + $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 107 | + |
|
| 108 | + // If there are no related objects, return an empty array. |
|
| 109 | + if ( empty( $objects ) ) { |
|
| 110 | + self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 111 | + |
|
| 112 | + return array(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 116 | + |
|
| 117 | + $sql = |
|
| 118 | + " |
|
| 119 | 119 | SELECT DISTINCT p.$actual_fields |
| 120 | 120 | FROM {$this->relation_table} r |
| 121 | 121 | INNER JOIN $wpdb->posts p |
| 122 | 122 | ON p.id = r.subject_id |
| 123 | 123 | " |
| 124 | - // Add the status clause. |
|
| 125 | - . self::and_status( $status ) |
|
| 126 | - . self::inner_join_is_article() |
|
| 127 | - . self::where_object_id( $objects ) |
|
| 128 | - // Since `object_id` can be an article ID we need to exclude it from |
|
| 129 | - // the results. |
|
| 130 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 131 | - . self::and_article_in( $include ) |
|
| 132 | - . self::and_post_type_in() |
|
| 133 | - . self::and_predicate( $predicate ) |
|
| 134 | - . self::order_by( $order_by ) |
|
| 135 | - . self::limit( $limit ); |
|
| 136 | - |
|
| 137 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * The `post_type IN` clause. |
|
| 142 | - * |
|
| 143 | - * @return string The `post_type IN` clause. |
|
| 144 | - * @since 3.15.3 |
|
| 145 | - * |
|
| 146 | - */ |
|
| 147 | - private static function and_post_type_in() { |
|
| 148 | - |
|
| 149 | - return " AND p.post_type IN ( '" |
|
| 150 | - . implode( |
|
| 151 | - "','", |
|
| 152 | - array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) |
|
| 153 | - ) |
|
| 154 | - . "' )"; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Add the limit clause if specified. |
|
| 159 | - * |
|
| 160 | - * @param null|int $limit The maximum number of results. |
|
| 161 | - * |
|
| 162 | - * @return string The limit clause (empty if no limit has been specified). |
|
| 163 | - * @since 3.15.0 |
|
| 164 | - * |
|
| 165 | - */ |
|
| 166 | - private static function limit( $limit = null ) { |
|
| 167 | - |
|
| 168 | - if ( null === $limit ) { |
|
| 169 | - return ''; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - return "LIMIT $limit"; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param $order_by string | null |
|
| 177 | - * |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - private static function order_by( $order_by ) { |
|
| 181 | - if ( ! $order_by ) { |
|
| 182 | - return ''; |
|
| 183 | - } |
|
| 184 | - $order_by = (string) $order_by; |
|
| 185 | - $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 186 | - |
|
| 187 | - if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 188 | - return " ORDER BY p.post_modified ${order_by} "; |
|
| 189 | - } else { |
|
| 190 | - return ' ORDER BY p.post_modified DESC '; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 196 | - * or get the entities if it's a post). |
|
| 197 | - * |
|
| 198 | - * @param int|array $object_id An array of posts/entities' ids. |
|
| 199 | - * |
|
| 200 | - * @return array An array of entities' ids. |
|
| 201 | - * @since 3.15.0 |
|
| 202 | - * |
|
| 203 | - */ |
|
| 204 | - private function article_id_to_entity_id( $object_id ) { |
|
| 205 | - |
|
| 206 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 207 | - |
|
| 208 | - $relation_service = $this; |
|
| 209 | - |
|
| 210 | - return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 211 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 212 | - return array_merge( $carry, (array) $item ); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 216 | - }, array() ); |
|
| 217 | - |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Add the WHERE clause. |
|
| 222 | - * |
|
| 223 | - * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 224 | - * |
|
| 225 | - * @return string The WHERE clause. |
|
| 226 | - * @since 3.15.0 |
|
| 227 | - * |
|
| 228 | - */ |
|
| 229 | - private static function where_object_id( $object_id ) { |
|
| 230 | - |
|
| 231 | - if ( empty( $object_id ) ) { |
|
| 232 | - // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 233 | - |
|
| 234 | - return ' WHERE 1 = 1'; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Add the exclude clause. |
|
| 242 | - * |
|
| 243 | - * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 244 | - * |
|
| 245 | - * @return string The exclude clause. |
|
| 246 | - * @since 3.15.0 |
|
| 247 | - * |
|
| 248 | - */ |
|
| 249 | - private static function and_article_not_in( $exclude ) { |
|
| 250 | - |
|
| 251 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Add the include clause. |
|
| 256 | - * |
|
| 257 | - * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 258 | - * |
|
| 259 | - * @return string An empty string if $include is null otherwise the include |
|
| 260 | - * clause. |
|
| 261 | - * @since 3.15.0 |
|
| 262 | - * |
|
| 263 | - */ |
|
| 264 | - private static function and_article_in( $include = null ) { |
|
| 265 | - |
|
| 266 | - if ( null === $include ) { |
|
| 267 | - return ''; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 275 | - * |
|
| 276 | - * @param int $object_id The object {@link WP_Post}'s id. |
|
| 277 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 278 | - * '*' to return all fields, by default '*'. |
|
| 279 | - * @param null|string $status The status, by default all. |
|
| 280 | - * |
|
| 281 | - * @return array|object|null Database query results |
|
| 282 | - * @since 3.15.0 |
|
| 283 | - * |
|
| 284 | - */ |
|
| 285 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 286 | - global $wpdb; |
|
| 287 | - |
|
| 288 | - // The output fields. |
|
| 289 | - $actual_fields = self::fields( $fields ); |
|
| 290 | - |
|
| 291 | - $sql = $wpdb->prepare( |
|
| 292 | - " |
|
| 124 | + // Add the status clause. |
|
| 125 | + . self::and_status( $status ) |
|
| 126 | + . self::inner_join_is_article() |
|
| 127 | + . self::where_object_id( $objects ) |
|
| 128 | + // Since `object_id` can be an article ID we need to exclude it from |
|
| 129 | + // the results. |
|
| 130 | + . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 131 | + . self::and_article_in( $include ) |
|
| 132 | + . self::and_post_type_in() |
|
| 133 | + . self::and_predicate( $predicate ) |
|
| 134 | + . self::order_by( $order_by ) |
|
| 135 | + . self::limit( $limit ); |
|
| 136 | + |
|
| 137 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * The `post_type IN` clause. |
|
| 142 | + * |
|
| 143 | + * @return string The `post_type IN` clause. |
|
| 144 | + * @since 3.15.3 |
|
| 145 | + * |
|
| 146 | + */ |
|
| 147 | + private static function and_post_type_in() { |
|
| 148 | + |
|
| 149 | + return " AND p.post_type IN ( '" |
|
| 150 | + . implode( |
|
| 151 | + "','", |
|
| 152 | + array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) |
|
| 153 | + ) |
|
| 154 | + . "' )"; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Add the limit clause if specified. |
|
| 159 | + * |
|
| 160 | + * @param null|int $limit The maximum number of results. |
|
| 161 | + * |
|
| 162 | + * @return string The limit clause (empty if no limit has been specified). |
|
| 163 | + * @since 3.15.0 |
|
| 164 | + * |
|
| 165 | + */ |
|
| 166 | + private static function limit( $limit = null ) { |
|
| 167 | + |
|
| 168 | + if ( null === $limit ) { |
|
| 169 | + return ''; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + return "LIMIT $limit"; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param $order_by string | null |
|
| 177 | + * |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + private static function order_by( $order_by ) { |
|
| 181 | + if ( ! $order_by ) { |
|
| 182 | + return ''; |
|
| 183 | + } |
|
| 184 | + $order_by = (string) $order_by; |
|
| 185 | + $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 186 | + |
|
| 187 | + if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 188 | + return " ORDER BY p.post_modified ${order_by} "; |
|
| 189 | + } else { |
|
| 190 | + return ' ORDER BY p.post_modified DESC '; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 196 | + * or get the entities if it's a post). |
|
| 197 | + * |
|
| 198 | + * @param int|array $object_id An array of posts/entities' ids. |
|
| 199 | + * |
|
| 200 | + * @return array An array of entities' ids. |
|
| 201 | + * @since 3.15.0 |
|
| 202 | + * |
|
| 203 | + */ |
|
| 204 | + private function article_id_to_entity_id( $object_id ) { |
|
| 205 | + |
|
| 206 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 207 | + |
|
| 208 | + $relation_service = $this; |
|
| 209 | + |
|
| 210 | + return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 211 | + if ( $entity_service->is_entity( $item ) ) { |
|
| 212 | + return array_merge( $carry, (array) $item ); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 216 | + }, array() ); |
|
| 217 | + |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Add the WHERE clause. |
|
| 222 | + * |
|
| 223 | + * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 224 | + * |
|
| 225 | + * @return string The WHERE clause. |
|
| 226 | + * @since 3.15.0 |
|
| 227 | + * |
|
| 228 | + */ |
|
| 229 | + private static function where_object_id( $object_id ) { |
|
| 230 | + |
|
| 231 | + if ( empty( $object_id ) ) { |
|
| 232 | + // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 233 | + |
|
| 234 | + return ' WHERE 1 = 1'; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Add the exclude clause. |
|
| 242 | + * |
|
| 243 | + * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 244 | + * |
|
| 245 | + * @return string The exclude clause. |
|
| 246 | + * @since 3.15.0 |
|
| 247 | + * |
|
| 248 | + */ |
|
| 249 | + private static function and_article_not_in( $exclude ) { |
|
| 250 | + |
|
| 251 | + return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Add the include clause. |
|
| 256 | + * |
|
| 257 | + * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 258 | + * |
|
| 259 | + * @return string An empty string if $include is null otherwise the include |
|
| 260 | + * clause. |
|
| 261 | + * @since 3.15.0 |
|
| 262 | + * |
|
| 263 | + */ |
|
| 264 | + private static function and_article_in( $include = null ) { |
|
| 265 | + |
|
| 266 | + if ( null === $include ) { |
|
| 267 | + return ''; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 275 | + * |
|
| 276 | + * @param int $object_id The object {@link WP_Post}'s id. |
|
| 277 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 278 | + * '*' to return all fields, by default '*'. |
|
| 279 | + * @param null|string $status The status, by default all. |
|
| 280 | + * |
|
| 281 | + * @return array|object|null Database query results |
|
| 282 | + * @since 3.15.0 |
|
| 283 | + * |
|
| 284 | + */ |
|
| 285 | + public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 286 | + global $wpdb; |
|
| 287 | + |
|
| 288 | + // The output fields. |
|
| 289 | + $actual_fields = self::fields( $fields ); |
|
| 290 | + |
|
| 291 | + $sql = $wpdb->prepare( |
|
| 292 | + " |
|
| 293 | 293 | SELECT p.$actual_fields |
| 294 | 294 | FROM {$this->relation_table} r |
| 295 | 295 | INNER JOIN $wpdb->posts p |
| 296 | 296 | ON p.id = r.subject_id |
| 297 | 297 | " |
| 298 | - // Add the status clause. |
|
| 299 | - . self::and_status( $status ) |
|
| 300 | - . self::inner_join_is_not_article() |
|
| 301 | - . " WHERE r.object_id = %d " |
|
| 302 | - . self::and_post_type_in() |
|
| 303 | - , |
|
| 304 | - $object_id |
|
| 305 | - ); |
|
| 306 | - |
|
| 307 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Get the entities referenced by the specified {@link WP_Post}. |
|
| 312 | - * |
|
| 313 | - * @param int $subject_id The {@link WP_Post}'s id. |
|
| 314 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 315 | - * '*' to return all fields, by default '*'. |
|
| 316 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 317 | - * @param null|string $status The status, by default all. |
|
| 318 | - * |
|
| 319 | - * @return array|object|null Database query results |
|
| 320 | - * @since 3.15.0 |
|
| 321 | - * |
|
| 322 | - */ |
|
| 323 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 324 | - global $wpdb; |
|
| 325 | - |
|
| 326 | - // The output fields. |
|
| 327 | - $actual_fields = self::fields( $fields ); |
|
| 328 | - |
|
| 329 | - $sql = $wpdb->prepare( |
|
| 330 | - " |
|
| 298 | + // Add the status clause. |
|
| 299 | + . self::and_status( $status ) |
|
| 300 | + . self::inner_join_is_not_article() |
|
| 301 | + . " WHERE r.object_id = %d " |
|
| 302 | + . self::and_post_type_in() |
|
| 303 | + , |
|
| 304 | + $object_id |
|
| 305 | + ); |
|
| 306 | + |
|
| 307 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Get the entities referenced by the specified {@link WP_Post}. |
|
| 312 | + * |
|
| 313 | + * @param int $subject_id The {@link WP_Post}'s id. |
|
| 314 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 315 | + * '*' to return all fields, by default '*'. |
|
| 316 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 317 | + * @param null|string $status The status, by default all. |
|
| 318 | + * |
|
| 319 | + * @return array|object|null Database query results |
|
| 320 | + * @since 3.15.0 |
|
| 321 | + * |
|
| 322 | + */ |
|
| 323 | + public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 324 | + global $wpdb; |
|
| 325 | + |
|
| 326 | + // The output fields. |
|
| 327 | + $actual_fields = self::fields( $fields ); |
|
| 328 | + |
|
| 329 | + $sql = $wpdb->prepare( |
|
| 330 | + " |
|
| 331 | 331 | SELECT p.$actual_fields |
| 332 | 332 | FROM {$this->relation_table} r |
| 333 | 333 | INNER JOIN $wpdb->posts p |
| 334 | 334 | ON p.id = r.object_id |
| 335 | 335 | " |
| 336 | - // Add the status clause. |
|
| 337 | - . self::and_status( $status ) |
|
| 338 | - . self::inner_join_is_not_article() |
|
| 339 | - . " WHERE r.subject_id = %d " |
|
| 340 | - . self::and_post_type_in() |
|
| 341 | - . self::and_predicate( $predicate ) |
|
| 342 | - , |
|
| 343 | - $subject_id |
|
| 344 | - ); |
|
| 345 | - |
|
| 346 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Add the `post_status` clause. |
|
| 351 | - * |
|
| 352 | - * @param null|string|array $status The status values. |
|
| 353 | - * |
|
| 354 | - * @return string An empty string if $status is null, otherwise the status clause. |
|
| 355 | - * @since 3.15.0 |
|
| 356 | - * |
|
| 357 | - */ |
|
| 358 | - private static function and_status( $status = null ) { |
|
| 359 | - |
|
| 360 | - if ( null === $status ) { |
|
| 361 | - return ''; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Add the `predicate` clause. |
|
| 369 | - * |
|
| 370 | - * @param null|string|array $predicate An array of predicates. |
|
| 371 | - * |
|
| 372 | - * @return string An empty string if $predicate is null otherwise the predicate |
|
| 373 | - * clause. |
|
| 374 | - * @since 3.15.0 |
|
| 375 | - * |
|
| 376 | - */ |
|
| 377 | - private static function and_predicate( $predicate = null ) { |
|
| 378 | - |
|
| 379 | - if ( null === $predicate ) { |
|
| 380 | - return ''; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * The select fields. |
|
| 388 | - * |
|
| 389 | - * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 390 | - * |
|
| 391 | - * @return string The `id` field if `ids` otherwise `*`. |
|
| 392 | - * @since 3.15.0 |
|
| 393 | - * |
|
| 394 | - */ |
|
| 395 | - private static function fields( $fields = '*' ) { |
|
| 396 | - |
|
| 397 | - // The output fields. |
|
| 398 | - return 'ids' === $fields ? 'id' : '*'; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * The inner join clause for articles. |
|
| 403 | - * |
|
| 404 | - * @return string The articles inner join clause. |
|
| 405 | - * @since 3.15.0 |
|
| 406 | - * |
|
| 407 | - */ |
|
| 408 | - private static function inner_join_is_article() { |
|
| 409 | - global $wpdb; |
|
| 410 | - |
|
| 411 | - return $wpdb->prepare( |
|
| 412 | - " |
|
| 336 | + // Add the status clause. |
|
| 337 | + . self::and_status( $status ) |
|
| 338 | + . self::inner_join_is_not_article() |
|
| 339 | + . " WHERE r.subject_id = %d " |
|
| 340 | + . self::and_post_type_in() |
|
| 341 | + . self::and_predicate( $predicate ) |
|
| 342 | + , |
|
| 343 | + $subject_id |
|
| 344 | + ); |
|
| 345 | + |
|
| 346 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Add the `post_status` clause. |
|
| 351 | + * |
|
| 352 | + * @param null|string|array $status The status values. |
|
| 353 | + * |
|
| 354 | + * @return string An empty string if $status is null, otherwise the status clause. |
|
| 355 | + * @since 3.15.0 |
|
| 356 | + * |
|
| 357 | + */ |
|
| 358 | + private static function and_status( $status = null ) { |
|
| 359 | + |
|
| 360 | + if ( null === $status ) { |
|
| 361 | + return ''; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Add the `predicate` clause. |
|
| 369 | + * |
|
| 370 | + * @param null|string|array $predicate An array of predicates. |
|
| 371 | + * |
|
| 372 | + * @return string An empty string if $predicate is null otherwise the predicate |
|
| 373 | + * clause. |
|
| 374 | + * @since 3.15.0 |
|
| 375 | + * |
|
| 376 | + */ |
|
| 377 | + private static function and_predicate( $predicate = null ) { |
|
| 378 | + |
|
| 379 | + if ( null === $predicate ) { |
|
| 380 | + return ''; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * The select fields. |
|
| 388 | + * |
|
| 389 | + * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 390 | + * |
|
| 391 | + * @return string The `id` field if `ids` otherwise `*`. |
|
| 392 | + * @since 3.15.0 |
|
| 393 | + * |
|
| 394 | + */ |
|
| 395 | + private static function fields( $fields = '*' ) { |
|
| 396 | + |
|
| 397 | + // The output fields. |
|
| 398 | + return 'ids' === $fields ? 'id' : '*'; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * The inner join clause for articles. |
|
| 403 | + * |
|
| 404 | + * @return string The articles inner join clause. |
|
| 405 | + * @since 3.15.0 |
|
| 406 | + * |
|
| 407 | + */ |
|
| 408 | + private static function inner_join_is_article() { |
|
| 409 | + global $wpdb; |
|
| 410 | + |
|
| 411 | + return $wpdb->prepare( |
|
| 412 | + " |
|
| 413 | 413 | INNER JOIN $wpdb->term_relationships tr |
| 414 | 414 | ON p.id = tr.object_id |
| 415 | 415 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -419,23 +419,23 @@ discard block |
||
| 419 | 419 | ON t.term_id = tt.term_id |
| 420 | 420 | AND t.slug = %s |
| 421 | 421 | ", |
| 422 | - 'wl_entity_type', |
|
| 423 | - 'article' |
|
| 424 | - ); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * The inner join clause for non-articles. |
|
| 429 | - * |
|
| 430 | - * @return string The non-articles inner join clause. |
|
| 431 | - * @since 3.15.0 |
|
| 432 | - * |
|
| 433 | - */ |
|
| 434 | - private static function inner_join_is_not_article() { |
|
| 435 | - global $wpdb; |
|
| 436 | - |
|
| 437 | - return $wpdb->prepare( |
|
| 438 | - " |
|
| 422 | + 'wl_entity_type', |
|
| 423 | + 'article' |
|
| 424 | + ); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * The inner join clause for non-articles. |
|
| 429 | + * |
|
| 430 | + * @return string The non-articles inner join clause. |
|
| 431 | + * @since 3.15.0 |
|
| 432 | + * |
|
| 433 | + */ |
|
| 434 | + private static function inner_join_is_not_article() { |
|
| 435 | + global $wpdb; |
|
| 436 | + |
|
| 437 | + return $wpdb->prepare( |
|
| 438 | + " |
|
| 439 | 439 | INNER JOIN $wpdb->term_relationships tr |
| 440 | 440 | ON p.id = tr.object_id |
| 441 | 441 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -445,29 +445,29 @@ discard block |
||
| 445 | 445 | ON t.term_id = tt.term_id |
| 446 | 446 | AND NOT t.slug = %s |
| 447 | 447 | ", |
| 448 | - 'wl_entity_type', |
|
| 449 | - 'article' |
|
| 450 | - ); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Find all the subject IDs and their referenced/related object IDs. The |
|
| 455 | - * object IDs are returned as comma separated IDs in the `object_ids` key. |
|
| 456 | - * |
|
| 457 | - * @return mixed Database query results |
|
| 458 | - * @since 3.18.0 |
|
| 459 | - */ |
|
| 460 | - public function find_all_grouped_by_subject_id() { |
|
| 461 | - global $wpdb; |
|
| 462 | - |
|
| 463 | - return $wpdb->get_results( |
|
| 464 | - " |
|
| 448 | + 'wl_entity_type', |
|
| 449 | + 'article' |
|
| 450 | + ); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Find all the subject IDs and their referenced/related object IDs. The |
|
| 455 | + * object IDs are returned as comma separated IDs in the `object_ids` key. |
|
| 456 | + * |
|
| 457 | + * @return mixed Database query results |
|
| 458 | + * @since 3.18.0 |
|
| 459 | + */ |
|
| 460 | + public function find_all_grouped_by_subject_id() { |
|
| 461 | + global $wpdb; |
|
| 462 | + |
|
| 463 | + return $wpdb->get_results( |
|
| 464 | + " |
|
| 465 | 465 | SELECT subject_id, GROUP_CONCAT( DISTINCT object_id ORDER BY object_id SEPARATOR ',' ) AS object_ids |
| 466 | 466 | FROM $this->relation_table |
| 467 | 467 | GROUP BY subject_id |
| 468 | 468 | " |
| 469 | - ); |
|
| 469 | + ); |
|
| 470 | 470 | |
| 471 | - } |
|
| 471 | + } |
|
| 472 | 472 | |
| 473 | 473 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function __construct() { |
| 54 | 54 | |
| 55 | - self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 55 | + self::$log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 56 | 56 | |
| 57 | 57 | global $wpdb; |
| 58 | 58 | |
@@ -95,24 +95,24 @@ discard block |
||
| 95 | 95 | * @since 3.15.0 |
| 96 | 96 | * |
| 97 | 97 | */ |
| 98 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null ) { |
|
| 98 | + public function get_article_subjects($object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null) { |
|
| 99 | 99 | global $wpdb; |
| 100 | 100 | |
| 101 | 101 | // The output fields. |
| 102 | - $actual_fields = self::fields( $fields ); |
|
| 102 | + $actual_fields = self::fields($fields); |
|
| 103 | 103 | |
| 104 | - self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 104 | + self::$log->trace('Getting article subjects for object '.implode(', ', (array) $object_id).'...'); |
|
| 105 | 105 | |
| 106 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 106 | + $objects = $this->article_id_to_entity_id($object_id); |
|
| 107 | 107 | |
| 108 | 108 | // If there are no related objects, return an empty array. |
| 109 | - if ( empty( $objects ) ) { |
|
| 110 | - self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 109 | + if (empty($objects)) { |
|
| 110 | + self::$log->debug('No entities found for object '.implode(', ', (array) $object_id).'.'); |
|
| 111 | 111 | |
| 112 | 112 | return array(); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 115 | + self::$log->debug(count($objects).' entity id(s) found for object '.implode(', ', (array) $object_id).'.'); |
|
| 116 | 116 | |
| 117 | 117 | $sql = |
| 118 | 118 | " |
@@ -122,19 +122,19 @@ discard block |
||
| 122 | 122 | ON p.id = r.subject_id |
| 123 | 123 | " |
| 124 | 124 | // Add the status clause. |
| 125 | - . self::and_status( $status ) |
|
| 125 | + . self::and_status($status) |
|
| 126 | 126 | . self::inner_join_is_article() |
| 127 | - . self::where_object_id( $objects ) |
|
| 127 | + . self::where_object_id($objects) |
|
| 128 | 128 | // Since `object_id` can be an article ID we need to exclude it from |
| 129 | 129 | // the results. |
| 130 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 131 | - . self::and_article_in( $include ) |
|
| 130 | + . self::and_article_not_in(array_merge($excludes, (array) $object_id)) |
|
| 131 | + . self::and_article_in($include) |
|
| 132 | 132 | . self::and_post_type_in() |
| 133 | - . self::and_predicate( $predicate ) |
|
| 134 | - . self::order_by( $order_by ) |
|
| 135 | - . self::limit( $limit ); |
|
| 133 | + . self::and_predicate($predicate) |
|
| 134 | + . self::order_by($order_by) |
|
| 135 | + . self::limit($limit); |
|
| 136 | 136 | |
| 137 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 137 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | return " AND p.post_type IN ( '" |
| 150 | 150 | . implode( |
| 151 | 151 | "','", |
| 152 | - array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) |
|
| 152 | + array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types()) |
|
| 153 | 153 | ) |
| 154 | 154 | . "' )"; |
| 155 | 155 | } |
@@ -163,9 +163,9 @@ discard block |
||
| 163 | 163 | * @since 3.15.0 |
| 164 | 164 | * |
| 165 | 165 | */ |
| 166 | - private static function limit( $limit = null ) { |
|
| 166 | + private static function limit($limit = null) { |
|
| 167 | 167 | |
| 168 | - if ( null === $limit ) { |
|
| 168 | + if (null === $limit) { |
|
| 169 | 169 | return ''; |
| 170 | 170 | } |
| 171 | 171 | |
@@ -177,14 +177,14 @@ discard block |
||
| 177 | 177 | * |
| 178 | 178 | * @return string |
| 179 | 179 | */ |
| 180 | - private static function order_by( $order_by ) { |
|
| 181 | - if ( ! $order_by ) { |
|
| 180 | + private static function order_by($order_by) { |
|
| 181 | + if ( ! $order_by) { |
|
| 182 | 182 | return ''; |
| 183 | 183 | } |
| 184 | 184 | $order_by = (string) $order_by; |
| 185 | - $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 185 | + $order_by_clauses = array('DESC', 'ASC'); |
|
| 186 | 186 | |
| 187 | - if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 187 | + if (in_array($order_by, $order_by_clauses, true)) { |
|
| 188 | 188 | return " ORDER BY p.post_modified ${order_by} "; |
| 189 | 189 | } else { |
| 190 | 190 | return ' ORDER BY p.post_modified DESC '; |
@@ -201,19 +201,19 @@ discard block |
||
| 201 | 201 | * @since 3.15.0 |
| 202 | 202 | * |
| 203 | 203 | */ |
| 204 | - private function article_id_to_entity_id( $object_id ) { |
|
| 204 | + private function article_id_to_entity_id($object_id) { |
|
| 205 | 205 | |
| 206 | 206 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 207 | 207 | |
| 208 | 208 | $relation_service = $this; |
| 209 | 209 | |
| 210 | - return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 211 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 212 | - return array_merge( $carry, (array) $item ); |
|
| 210 | + return array_reduce((array) $object_id, function($carry, $item) use ($entity_service, $relation_service) { |
|
| 211 | + if ($entity_service->is_entity($item)) { |
|
| 212 | + return array_merge($carry, (array) $item); |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 216 | - }, array() ); |
|
| 215 | + return array_merge($carry, $relation_service->get_objects($item, 'ids')); |
|
| 216 | + }, array()); |
|
| 217 | 217 | |
| 218 | 218 | } |
| 219 | 219 | |
@@ -226,15 +226,15 @@ discard block |
||
| 226 | 226 | * @since 3.15.0 |
| 227 | 227 | * |
| 228 | 228 | */ |
| 229 | - private static function where_object_id( $object_id ) { |
|
| 229 | + private static function where_object_id($object_id) { |
|
| 230 | 230 | |
| 231 | - if ( empty( $object_id ) ) { |
|
| 231 | + if (empty($object_id)) { |
|
| 232 | 232 | // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
| 233 | 233 | |
| 234 | 234 | return ' WHERE 1 = 1'; |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 237 | + return ' WHERE r.object_id IN ( '.implode(',', wp_parse_id_list((array) $object_id)).' )'; |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | /** |
@@ -246,9 +246,9 @@ discard block |
||
| 246 | 246 | * @since 3.15.0 |
| 247 | 247 | * |
| 248 | 248 | */ |
| 249 | - private static function and_article_not_in( $exclude ) { |
|
| 249 | + private static function and_article_not_in($exclude) { |
|
| 250 | 250 | |
| 251 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 251 | + return ' AND NOT p.ID IN ( '.implode(',', wp_parse_id_list((array) $exclude)).' )'; |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | /** |
@@ -261,13 +261,13 @@ discard block |
||
| 261 | 261 | * @since 3.15.0 |
| 262 | 262 | * |
| 263 | 263 | */ |
| 264 | - private static function and_article_in( $include = null ) { |
|
| 264 | + private static function and_article_in($include = null) { |
|
| 265 | 265 | |
| 266 | - if ( null === $include ) { |
|
| 266 | + if (null === $include) { |
|
| 267 | 267 | return ''; |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 270 | + return ' AND p.ID IN ( '.implode(',', wp_parse_id_list((array) $include)).' )'; |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | /** |
@@ -282,11 +282,11 @@ discard block |
||
| 282 | 282 | * @since 3.15.0 |
| 283 | 283 | * |
| 284 | 284 | */ |
| 285 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 285 | + public function get_non_article_subjects($object_id, $fields = '*', $status = null) { |
|
| 286 | 286 | global $wpdb; |
| 287 | 287 | |
| 288 | 288 | // The output fields. |
| 289 | - $actual_fields = self::fields( $fields ); |
|
| 289 | + $actual_fields = self::fields($fields); |
|
| 290 | 290 | |
| 291 | 291 | $sql = $wpdb->prepare( |
| 292 | 292 | " |
@@ -296,7 +296,7 @@ discard block |
||
| 296 | 296 | ON p.id = r.subject_id |
| 297 | 297 | " |
| 298 | 298 | // Add the status clause. |
| 299 | - . self::and_status( $status ) |
|
| 299 | + . self::and_status($status) |
|
| 300 | 300 | . self::inner_join_is_not_article() |
| 301 | 301 | . " WHERE r.object_id = %d " |
| 302 | 302 | . self::and_post_type_in() |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | $object_id |
| 305 | 305 | ); |
| 306 | 306 | |
| 307 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 307 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | /** |
@@ -320,11 +320,11 @@ discard block |
||
| 320 | 320 | * @since 3.15.0 |
| 321 | 321 | * |
| 322 | 322 | */ |
| 323 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 323 | + public function get_objects($subject_id, $fields = '*', $predicate = null, $status = null) { |
|
| 324 | 324 | global $wpdb; |
| 325 | 325 | |
| 326 | 326 | // The output fields. |
| 327 | - $actual_fields = self::fields( $fields ); |
|
| 327 | + $actual_fields = self::fields($fields); |
|
| 328 | 328 | |
| 329 | 329 | $sql = $wpdb->prepare( |
| 330 | 330 | " |
@@ -334,16 +334,16 @@ discard block |
||
| 334 | 334 | ON p.id = r.object_id |
| 335 | 335 | " |
| 336 | 336 | // Add the status clause. |
| 337 | - . self::and_status( $status ) |
|
| 337 | + . self::and_status($status) |
|
| 338 | 338 | . self::inner_join_is_not_article() |
| 339 | 339 | . " WHERE r.subject_id = %d " |
| 340 | 340 | . self::and_post_type_in() |
| 341 | - . self::and_predicate( $predicate ) |
|
| 341 | + . self::and_predicate($predicate) |
|
| 342 | 342 | , |
| 343 | 343 | $subject_id |
| 344 | 344 | ); |
| 345 | 345 | |
| 346 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 346 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 347 | 347 | } |
| 348 | 348 | |
| 349 | 349 | /** |
@@ -355,13 +355,13 @@ discard block |
||
| 355 | 355 | * @since 3.15.0 |
| 356 | 356 | * |
| 357 | 357 | */ |
| 358 | - private static function and_status( $status = null ) { |
|
| 358 | + private static function and_status($status = null) { |
|
| 359 | 359 | |
| 360 | - if ( null === $status ) { |
|
| 360 | + if (null === $status) { |
|
| 361 | 361 | return ''; |
| 362 | 362 | } |
| 363 | 363 | |
| 364 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 364 | + return " AND p.post_status IN ('".implode("', '", array_map('esc_sql', (array) $status))."')"; |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | /** |
@@ -374,13 +374,13 @@ discard block |
||
| 374 | 374 | * @since 3.15.0 |
| 375 | 375 | * |
| 376 | 376 | */ |
| 377 | - private static function and_predicate( $predicate = null ) { |
|
| 377 | + private static function and_predicate($predicate = null) { |
|
| 378 | 378 | |
| 379 | - if ( null === $predicate ) { |
|
| 379 | + if (null === $predicate) { |
|
| 380 | 380 | return ''; |
| 381 | 381 | } |
| 382 | 382 | |
| 383 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 383 | + return " AND r.predicate IN ('".implode("', '", array_map('esc_sql', (array) $predicate))."')"; |
|
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | /** |
@@ -392,7 +392,7 @@ discard block |
||
| 392 | 392 | * @since 3.15.0 |
| 393 | 393 | * |
| 394 | 394 | */ |
| 395 | - private static function fields( $fields = '*' ) { |
|
| 395 | + private static function fields($fields = '*') { |
|
| 396 | 396 | |
| 397 | 397 | // The output fields. |
| 398 | 398 | return 'ids' === $fields ? 'id' : '*'; |