@@ -8,7 +8,6 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | use Wordlift\Cache\Ttl_Cache; |
| 11 | -use Wordlift\Widgets\Navigator\Filler_Posts; |
|
| 12 | 11 | use Wordlift\Widgets\Navigator\Filler_Posts\Filler_Posts_Util; |
| 13 | 12 | use Wordlift\Widgets\Navigator\Navigator_Data; |
| 14 | 13 | use Wordlift\Widgets\Srcset_Util; |
@@ -20,29 +20,29 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_shortcode_navigator_data() { |
| 22 | 22 | |
| 23 | - // Create the cache key. |
|
| 24 | - $cache_key_params = $_REQUEST; |
|
| 25 | - unset( $cache_key_params['uniqid'] ); |
|
| 26 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 23 | + // Create the cache key. |
|
| 24 | + $cache_key_params = $_REQUEST; |
|
| 25 | + unset( $cache_key_params['uniqid'] ); |
|
| 26 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 27 | 27 | |
| 28 | - // Create the TTL cache and try to get the results. |
|
| 29 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + // Create the TTL cache and try to get the results. |
|
| 29 | + $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | + $cache_results = $cache->get( $cache_key ); |
|
| 31 | 31 | |
| 32 | - if ( isset( $cache_results ) ) { |
|
| 33 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 32 | + if ( isset( $cache_results ) ) { |
|
| 33 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 34 | 34 | |
| 35 | - return $cache_results; |
|
| 36 | - } |
|
| 35 | + return $cache_results; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 38 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 39 | 39 | |
| 40 | - $results = _wl_navigator_get_data(); |
|
| 40 | + $results = _wl_navigator_get_data(); |
|
| 41 | 41 | |
| 42 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 43 | - $cache->put( $cache_key, $results ); |
|
| 42 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 43 | + $cache->put( $cache_key, $results ); |
|
| 44 | 44 | |
| 45 | - return $results; |
|
| 45 | + return $results; |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** |
@@ -57,236 +57,236 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | function wl_network_navigator_wp_json( $request ) { |
| 59 | 59 | |
| 60 | - // Create the cache key. |
|
| 61 | - $cache_key_params = $_REQUEST; |
|
| 62 | - unset( $cache_key_params['uniqid'] ); |
|
| 63 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 60 | + // Create the cache key. |
|
| 61 | + $cache_key_params = $_REQUEST; |
|
| 62 | + unset( $cache_key_params['uniqid'] ); |
|
| 63 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 64 | 64 | |
| 65 | - // Create the TTL cache and try to get the results. |
|
| 66 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 67 | - $cache_results = $cache->get( $cache_key ); |
|
| 65 | + // Create the TTL cache and try to get the results. |
|
| 66 | + $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 67 | + $cache_results = $cache->get( $cache_key ); |
|
| 68 | 68 | |
| 69 | - if ( isset( $cache_results ) ) { |
|
| 70 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 69 | + if ( isset( $cache_results ) ) { |
|
| 70 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 71 | 71 | |
| 72 | - return $cache_results; |
|
| 73 | - } |
|
| 72 | + return $cache_results; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 75 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 76 | 76 | |
| 77 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 77 | + $results = _wl_network_navigator_get_data( $request ); |
|
| 78 | 78 | |
| 79 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 80 | - $cache->put( $cache_key, $results ); |
|
| 79 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 80 | + $cache->put( $cache_key, $results ); |
|
| 81 | 81 | |
| 82 | - return $results; |
|
| 82 | + return $results; |
|
| 83 | 83 | |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | function _wl_navigator_get_data() { |
| 87 | 87 | |
| 88 | - // Post ID must be defined |
|
| 89 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 90 | - wp_send_json_error( 'No post_id given' ); |
|
| 91 | - |
|
| 92 | - return array(); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // Post ID must be defined |
|
| 96 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 97 | - wp_send_json_error( 'No uniqid given' ); |
|
| 98 | - |
|
| 99 | - return array(); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Limit the results (defaults to 4) |
|
| 103 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 104 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 105 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 106 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 107 | - $post_types = explode( ',', $post_types ); |
|
| 108 | - $existing_post_types = get_post_types(); |
|
| 109 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 110 | - $current_post_id = $_GET['post_id']; |
|
| 111 | - $current_post = get_post( $current_post_id ); |
|
| 112 | - |
|
| 113 | - $navigator_id = $_GET['uniqid']; |
|
| 114 | - |
|
| 115 | - // Post ID has to match an existing item |
|
| 116 | - if ( null === $current_post ) { |
|
| 117 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 118 | - |
|
| 119 | - return array(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // Determine navigator type and call respective _get_results |
|
| 123 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 124 | - |
|
| 125 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 126 | - 'ID', |
|
| 127 | - 'post_title', |
|
| 128 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 129 | - } else { |
|
| 130 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 131 | - 'ID', |
|
| 132 | - 'post_title', |
|
| 133 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 138 | - $results = array(); |
|
| 139 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 140 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Use the thumbnail. |
|
| 144 | - * |
|
| 145 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 146 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 147 | - * |
|
| 148 | - * @since 3.19.3 We're using the medium size image. |
|
| 149 | - */ |
|
| 150 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 151 | - |
|
| 152 | - $result = array( |
|
| 153 | - 'post' => array( |
|
| 154 | - 'id' => $referencing_post->ID, |
|
| 155 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 156 | - 'title' => $referencing_post->post_title, |
|
| 157 | - 'thumbnail' => $thumbnail, |
|
| 158 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 159 | - ), |
|
| 160 | - 'entity' => array( |
|
| 161 | - 'id' => $referencing_post->entity_id, |
|
| 162 | - 'label' => $serialized_entity['label'], |
|
| 163 | - 'mainType' => $serialized_entity['mainType'], |
|
| 164 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 165 | - ), |
|
| 166 | - ); |
|
| 167 | - |
|
| 168 | - $results[] = $result; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - if ( count( $results ) < $navigator_length ) { |
|
| 173 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Add filler posts if needed |
|
| 177 | - $filler_count = $navigator_length - count( $results ); |
|
| 178 | - if ( $filler_count > 0 ) { |
|
| 179 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 180 | - return $p->ID; |
|
| 181 | - }, $referencing_posts ); |
|
| 182 | - /** |
|
| 183 | - * @since 3.27.8 |
|
| 184 | - * Filler posts are fetched using this util. |
|
| 185 | - */ |
|
| 186 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 187 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 188 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 189 | - $results = array_merge( $results, $filler_posts ); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // Apply filters after fillers are added |
|
| 193 | - foreach ( $results as $result_index => $result ) { |
|
| 194 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 195 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return $results; |
|
| 88 | + // Post ID must be defined |
|
| 89 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 90 | + wp_send_json_error( 'No post_id given' ); |
|
| 91 | + |
|
| 92 | + return array(); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // Post ID must be defined |
|
| 96 | + if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 97 | + wp_send_json_error( 'No uniqid given' ); |
|
| 98 | + |
|
| 99 | + return array(); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Limit the results (defaults to 4) |
|
| 103 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 104 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 105 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 106 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 107 | + $post_types = explode( ',', $post_types ); |
|
| 108 | + $existing_post_types = get_post_types(); |
|
| 109 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 110 | + $current_post_id = $_GET['post_id']; |
|
| 111 | + $current_post = get_post( $current_post_id ); |
|
| 112 | + |
|
| 113 | + $navigator_id = $_GET['uniqid']; |
|
| 114 | + |
|
| 115 | + // Post ID has to match an existing item |
|
| 116 | + if ( null === $current_post ) { |
|
| 117 | + wp_send_json_error( 'No valid post_id given' ); |
|
| 118 | + |
|
| 119 | + return array(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // Determine navigator type and call respective _get_results |
|
| 123 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 124 | + |
|
| 125 | + $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 126 | + 'ID', |
|
| 127 | + 'post_title', |
|
| 128 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 129 | + } else { |
|
| 130 | + $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 131 | + 'ID', |
|
| 132 | + 'post_title', |
|
| 133 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 138 | + $results = array(); |
|
| 139 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 140 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Use the thumbnail. |
|
| 144 | + * |
|
| 145 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 146 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 147 | + * |
|
| 148 | + * @since 3.19.3 We're using the medium size image. |
|
| 149 | + */ |
|
| 150 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 151 | + |
|
| 152 | + $result = array( |
|
| 153 | + 'post' => array( |
|
| 154 | + 'id' => $referencing_post->ID, |
|
| 155 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 156 | + 'title' => $referencing_post->post_title, |
|
| 157 | + 'thumbnail' => $thumbnail, |
|
| 158 | + 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 159 | + ), |
|
| 160 | + 'entity' => array( |
|
| 161 | + 'id' => $referencing_post->entity_id, |
|
| 162 | + 'label' => $serialized_entity['label'], |
|
| 163 | + 'mainType' => $serialized_entity['mainType'], |
|
| 164 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 165 | + ), |
|
| 166 | + ); |
|
| 167 | + |
|
| 168 | + $results[] = $result; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + if ( count( $results ) < $navigator_length ) { |
|
| 173 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Add filler posts if needed |
|
| 177 | + $filler_count = $navigator_length - count( $results ); |
|
| 178 | + if ( $filler_count > 0 ) { |
|
| 179 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 180 | + return $p->ID; |
|
| 181 | + }, $referencing_posts ); |
|
| 182 | + /** |
|
| 183 | + * @since 3.27.8 |
|
| 184 | + * Filler posts are fetched using this util. |
|
| 185 | + */ |
|
| 186 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 187 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 188 | + $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 189 | + $results = array_merge( $results, $filler_posts ); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // Apply filters after fillers are added |
|
| 193 | + foreach ( $results as $result_index => $result ) { |
|
| 194 | + $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 195 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return $results; |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | function _wl_network_navigator_get_data( $request ) { |
| 202 | 202 | |
| 203 | - // Limit the results (defaults to 4) |
|
| 204 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 205 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 206 | - $navigator_id = $request['uniqid']; |
|
| 207 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 208 | - |
|
| 209 | - $entities = $request['entities']; |
|
| 210 | - |
|
| 211 | - // Post ID has to match an existing item |
|
| 212 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 213 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 217 | - 'ID', |
|
| 218 | - 'post_title', |
|
| 219 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 220 | - |
|
| 221 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 222 | - $results = array(); |
|
| 223 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 224 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Use the thumbnail. |
|
| 228 | - * |
|
| 229 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 230 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 231 | - * |
|
| 232 | - * @since 3.19.3 We're using the medium size image. |
|
| 233 | - */ |
|
| 234 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 235 | - |
|
| 236 | - $result = array( |
|
| 237 | - 'post' => array( |
|
| 238 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 239 | - 'title' => $referencing_post->post_title, |
|
| 240 | - 'thumbnail' => $thumbnail, |
|
| 241 | - ), |
|
| 242 | - 'entity' => array( |
|
| 243 | - 'label' => $serialized_entity['label'], |
|
| 244 | - 'mainType' => $serialized_entity['mainType'], |
|
| 245 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 246 | - ), |
|
| 247 | - ); |
|
| 248 | - |
|
| 249 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 250 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 251 | - |
|
| 252 | - $results[] = $result; |
|
| 253 | - |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - if ( count( $results ) < $navigator_length ) { |
|
| 257 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return $results; |
|
| 203 | + // Limit the results (defaults to 4) |
|
| 204 | + $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 205 | + $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 206 | + $navigator_id = $request['uniqid']; |
|
| 207 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 208 | + |
|
| 209 | + $entities = $request['entities']; |
|
| 210 | + |
|
| 211 | + // Post ID has to match an existing item |
|
| 212 | + if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 213 | + wp_send_json_error( 'No valid entities provided' ); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 217 | + 'ID', |
|
| 218 | + 'post_title', |
|
| 219 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 220 | + |
|
| 221 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 222 | + $results = array(); |
|
| 223 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 224 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Use the thumbnail. |
|
| 228 | + * |
|
| 229 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 230 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 231 | + * |
|
| 232 | + * @since 3.19.3 We're using the medium size image. |
|
| 233 | + */ |
|
| 234 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 235 | + |
|
| 236 | + $result = array( |
|
| 237 | + 'post' => array( |
|
| 238 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 239 | + 'title' => $referencing_post->post_title, |
|
| 240 | + 'thumbnail' => $thumbnail, |
|
| 241 | + ), |
|
| 242 | + 'entity' => array( |
|
| 243 | + 'label' => $serialized_entity['label'], |
|
| 244 | + 'mainType' => $serialized_entity['mainType'], |
|
| 245 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 246 | + ), |
|
| 247 | + ); |
|
| 248 | + |
|
| 249 | + $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 250 | + $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 251 | + |
|
| 252 | + $results[] = $result; |
|
| 253 | + |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + if ( count( $results ) < $navigator_length ) { |
|
| 257 | + $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return $results; |
|
| 261 | 261 | |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | function _wl_network_navigator_get_results( |
| 265 | - $entities, $fields = array( |
|
| 266 | - 'ID', |
|
| 267 | - 'post_title', |
|
| 265 | + $entities, $fields = array( |
|
| 266 | + 'ID', |
|
| 267 | + 'post_title', |
|
| 268 | 268 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 269 | 269 | ) { |
| 270 | - global $wpdb; |
|
| 271 | - |
|
| 272 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 273 | - return "p.$item AS $item"; |
|
| 274 | - }, (array) $fields ) ); |
|
| 275 | - |
|
| 276 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 277 | - return "p.$item"; |
|
| 278 | - }, (array) $order_by ) ); |
|
| 279 | - |
|
| 280 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 281 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 282 | - if ( isset( $entity ) ) { |
|
| 283 | - return $entity->ID; |
|
| 284 | - } |
|
| 285 | - }, $entities ) ); |
|
| 286 | - |
|
| 287 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 288 | - return $wpdb->get_results( |
|
| 289 | - $wpdb->prepare( <<<EOF |
|
| 270 | + global $wpdb; |
|
| 271 | + |
|
| 272 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 273 | + return "p.$item AS $item"; |
|
| 274 | + }, (array) $fields ) ); |
|
| 275 | + |
|
| 276 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 277 | + return "p.$item"; |
|
| 278 | + }, (array) $order_by ) ); |
|
| 279 | + |
|
| 280 | + $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 281 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 282 | + if ( isset( $entity ) ) { |
|
| 283 | + return $entity->ID; |
|
| 284 | + } |
|
| 285 | + }, $entities ) ); |
|
| 286 | + |
|
| 287 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 288 | + return $wpdb->get_results( |
|
| 289 | + $wpdb->prepare( <<<EOF |
|
| 290 | 290 | SELECT %3\$s, p2.ID as entity_id |
| 291 | 291 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 292 | 292 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -312,8 +312,8 @@ discard block |
||
| 312 | 312 | LIMIT %1\$d |
| 313 | 313 | OFFSET %2\$d |
| 314 | 314 | EOF |
| 315 | - , $limit, $offset, $select, $order_by ) |
|
| 316 | - ); |
|
| 315 | + , $limit, $offset, $select, $order_by ) |
|
| 316 | + ); |
|
| 317 | 317 | |
| 318 | 318 | } |
| 319 | 319 | |
@@ -325,9 +325,9 @@ discard block |
||
| 325 | 325 | */ |
| 326 | 326 | function wl_shortcode_navigator_ajax() { |
| 327 | 327 | |
| 328 | - // Temporary blocking the Navigator. |
|
| 329 | - $results = wl_shortcode_navigator_data(); |
|
| 330 | - wl_core_send_json( $results ); |
|
| 328 | + // Temporary blocking the Navigator. |
|
| 329 | + $results = wl_shortcode_navigator_data(); |
|
| 330 | + wl_core_send_json( $results ); |
|
| 331 | 331 | |
| 332 | 332 | } |
| 333 | 333 | |
@@ -339,16 +339,16 @@ discard block |
||
| 339 | 339 | */ |
| 340 | 340 | function wl_shortcode_navigator_wp_json() { |
| 341 | 341 | |
| 342 | - $results = wl_shortcode_navigator_data(); |
|
| 343 | - if ( ob_get_contents() ) { |
|
| 344 | - ob_clean(); |
|
| 345 | - } |
|
| 342 | + $results = wl_shortcode_navigator_data(); |
|
| 343 | + if ( ob_get_contents() ) { |
|
| 344 | + ob_clean(); |
|
| 345 | + } |
|
| 346 | 346 | |
| 347 | - return array( |
|
| 348 | - 'items' => array( |
|
| 349 | - array( 'values' => $results ), |
|
| 350 | - ), |
|
| 351 | - ); |
|
| 347 | + return array( |
|
| 348 | + 'items' => array( |
|
| 349 | + array( 'values' => $results ), |
|
| 350 | + ), |
|
| 351 | + ); |
|
| 352 | 352 | |
| 353 | 353 | } |
| 354 | 354 | |
@@ -356,22 +356,22 @@ discard block |
||
| 356 | 356 | * Adding `rest_api_init` action for amp backend of navigator |
| 357 | 357 | */ |
| 358 | 358 | add_action( 'rest_api_init', function () { |
| 359 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 360 | - 'methods' => 'GET', |
|
| 361 | - 'permission_callback' => '__return_true', |
|
| 362 | - 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 363 | - ) ); |
|
| 359 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 360 | + 'methods' => 'GET', |
|
| 361 | + 'permission_callback' => '__return_true', |
|
| 362 | + 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 363 | + ) ); |
|
| 364 | 364 | } ); |
| 365 | 365 | |
| 366 | 366 | /** |
| 367 | 367 | * Adding `rest_api_init` action for backend of network navigator |
| 368 | 368 | */ |
| 369 | 369 | add_action( 'rest_api_init', function () { |
| 370 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 371 | - 'methods' => 'GET', |
|
| 372 | - 'callback' => 'wl_network_navigator_wp_json', |
|
| 373 | - 'permission_callback' => '__return_true', |
|
| 374 | - ) ); |
|
| 370 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 371 | + 'methods' => 'GET', |
|
| 372 | + 'callback' => 'wl_network_navigator_wp_json', |
|
| 373 | + 'permission_callback' => '__return_true', |
|
| 374 | + ) ); |
|
| 375 | 375 | } ); |
| 376 | 376 | |
| 377 | 377 | /** |
@@ -380,23 +380,23 @@ discard block |
||
| 380 | 380 | * @since 2.2.0 |
| 381 | 381 | */ |
| 382 | 382 | add_action( 'plugins_loaded', function () { |
| 383 | - $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 384 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 385 | - return; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 389 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 390 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 383 | + $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 384 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 385 | + return; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 389 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 390 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 391 | 391 | }, 0 ); |
| 392 | 392 | |
| 393 | 393 | add_action( 'init', function () { |
| 394 | - $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 395 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 396 | - return; |
|
| 397 | - } |
|
| 394 | + $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 395 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 396 | + return; |
|
| 397 | + } |
|
| 398 | 398 | |
| 399 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 400 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 399 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 400 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 401 | 401 | }, 0 ); |
| 402 | 402 | |
@@ -22,25 +22,25 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | // Create the cache key. |
| 24 | 24 | $cache_key_params = $_REQUEST; |
| 25 | - unset( $cache_key_params['uniqid'] ); |
|
| 26 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 25 | + unset($cache_key_params['uniqid']); |
|
| 26 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 27 | 27 | |
| 28 | 28 | // Create the TTL cache and try to get the results. |
| 29 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 29 | + $cache = new Ttl_Cache("navigator", 8 * 60 * 60); // 8 hours. |
|
| 30 | + $cache_results = $cache->get($cache_key); |
|
| 31 | 31 | |
| 32 | - if ( isset( $cache_results ) ) { |
|
| 33 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 32 | + if (isset($cache_results)) { |
|
| 33 | + header('X-WordLift-Cache: HIT'); |
|
| 34 | 34 | |
| 35 | 35 | return $cache_results; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 38 | + header('X-WordLift-Cache: MISS'); |
|
| 39 | 39 | |
| 40 | 40 | $results = _wl_navigator_get_data(); |
| 41 | 41 | |
| 42 | 42 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 43 | - $cache->put( $cache_key, $results ); |
|
| 43 | + $cache->put($cache_key, $results); |
|
| 44 | 44 | |
| 45 | 45 | return $results; |
| 46 | 46 | } |
@@ -55,29 +55,29 @@ discard block |
||
| 55 | 55 | * @since 3.22.6 |
| 56 | 56 | * |
| 57 | 57 | */ |
| 58 | -function wl_network_navigator_wp_json( $request ) { |
|
| 58 | +function wl_network_navigator_wp_json($request) { |
|
| 59 | 59 | |
| 60 | 60 | // Create the cache key. |
| 61 | 61 | $cache_key_params = $_REQUEST; |
| 62 | - unset( $cache_key_params['uniqid'] ); |
|
| 63 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 62 | + unset($cache_key_params['uniqid']); |
|
| 63 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 64 | 64 | |
| 65 | 65 | // Create the TTL cache and try to get the results. |
| 66 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 67 | - $cache_results = $cache->get( $cache_key ); |
|
| 66 | + $cache = new Ttl_Cache("network-navigator", 8 * 60 * 60); // 8 hours. |
|
| 67 | + $cache_results = $cache->get($cache_key); |
|
| 68 | 68 | |
| 69 | - if ( isset( $cache_results ) ) { |
|
| 70 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 69 | + if (isset($cache_results)) { |
|
| 70 | + header('X-WordLift-Cache: HIT'); |
|
| 71 | 71 | |
| 72 | 72 | return $cache_results; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 75 | + header('X-WordLift-Cache: MISS'); |
|
| 76 | 76 | |
| 77 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 77 | + $results = _wl_network_navigator_get_data($request); |
|
| 78 | 78 | |
| 79 | 79 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 80 | - $cache->put( $cache_key, $results ); |
|
| 80 | + $cache->put($cache_key, $results); |
|
| 81 | 81 | |
| 82 | 82 | return $results; |
| 83 | 83 | |
@@ -86,58 +86,58 @@ discard block |
||
| 86 | 86 | function _wl_navigator_get_data() { |
| 87 | 87 | |
| 88 | 88 | // Post ID must be defined |
| 89 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 90 | - wp_send_json_error( 'No post_id given' ); |
|
| 89 | + if ( ! isset($_GET['post_id'])) { |
|
| 90 | + wp_send_json_error('No post_id given'); |
|
| 91 | 91 | |
| 92 | 92 | return array(); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // Post ID must be defined |
| 96 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 97 | - wp_send_json_error( 'No uniqid given' ); |
|
| 96 | + if ( ! isset($_GET['uniqid'])) { |
|
| 97 | + wp_send_json_error('No uniqid given'); |
|
| 98 | 98 | |
| 99 | 99 | return array(); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // Limit the results (defaults to 4) |
| 103 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 104 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 105 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 106 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 107 | - $post_types = explode( ',', $post_types ); |
|
| 103 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 104 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 105 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 106 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 107 | + $post_types = explode(',', $post_types); |
|
| 108 | 108 | $existing_post_types = get_post_types(); |
| 109 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 109 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 110 | 110 | $current_post_id = $_GET['post_id']; |
| 111 | - $current_post = get_post( $current_post_id ); |
|
| 111 | + $current_post = get_post($current_post_id); |
|
| 112 | 112 | |
| 113 | 113 | $navigator_id = $_GET['uniqid']; |
| 114 | 114 | |
| 115 | 115 | // Post ID has to match an existing item |
| 116 | - if ( null === $current_post ) { |
|
| 117 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 116 | + if (null === $current_post) { |
|
| 117 | + wp_send_json_error('No valid post_id given'); |
|
| 118 | 118 | |
| 119 | 119 | return array(); |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // Determine navigator type and call respective _get_results |
| 123 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 123 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 124 | 124 | |
| 125 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 125 | + $referencing_posts = Navigator_Data::entity_navigator_get_results($current_post_id, array( |
|
| 126 | 126 | 'ID', |
| 127 | 127 | 'post_title', |
| 128 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 128 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 129 | 129 | } else { |
| 130 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 130 | + $referencing_posts = Navigator_Data::post_navigator_get_results($current_post_id, array( |
|
| 131 | 131 | 'ID', |
| 132 | 132 | 'post_title', |
| 133 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 133 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 134 | 134 | |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | // loop over them and take the first one which is not already in the $related_posts |
| 138 | 138 | $results = array(); |
| 139 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 140 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 139 | + foreach ($referencing_posts as $referencing_post) { |
|
| 140 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * Use the thumbnail. |
@@ -147,21 +147,21 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | * @since 3.19.3 We're using the medium size image. |
| 149 | 149 | */ |
| 150 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 150 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 151 | 151 | |
| 152 | 152 | $result = array( |
| 153 | 153 | 'post' => array( |
| 154 | 154 | 'id' => $referencing_post->ID, |
| 155 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 155 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 156 | 156 | 'title' => $referencing_post->post_title, |
| 157 | 157 | 'thumbnail' => $thumbnail, |
| 158 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 158 | + 'srcset' => Srcset_Util::get_srcset($referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET) |
|
| 159 | 159 | ), |
| 160 | 160 | 'entity' => array( |
| 161 | 161 | 'id' => $referencing_post->entity_id, |
| 162 | 162 | 'label' => $serialized_entity['label'], |
| 163 | 163 | 'mainType' => $serialized_entity['mainType'], |
| 164 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 164 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 165 | 165 | ), |
| 166 | 166 | ); |
| 167 | 167 | |
@@ -169,59 +169,59 @@ discard block |
||
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | |
| 172 | - if ( count( $results ) < $navigator_length ) { |
|
| 173 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 172 | + if (count($results) < $navigator_length) { |
|
| 173 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // Add filler posts if needed |
| 177 | - $filler_count = $navigator_length - count( $results ); |
|
| 178 | - if ( $filler_count > 0 ) { |
|
| 179 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 177 | + $filler_count = $navigator_length - count($results); |
|
| 178 | + if ($filler_count > 0) { |
|
| 179 | + $referencing_post_ids = array_map(function($p) { |
|
| 180 | 180 | return $p->ID; |
| 181 | - }, $referencing_posts ); |
|
| 181 | + }, $referencing_posts); |
|
| 182 | 182 | /** |
| 183 | 183 | * @since 3.27.8 |
| 184 | 184 | * Filler posts are fetched using this util. |
| 185 | 185 | */ |
| 186 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 187 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 188 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 189 | - $results = array_merge( $results, $filler_posts ); |
|
| 186 | + $filler_posts_util = new Filler_Posts_Util($current_post_id); |
|
| 187 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 188 | + $filler_posts = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded); |
|
| 189 | + $results = array_merge($results, $filler_posts); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | // Apply filters after fillers are added |
| 193 | - foreach ( $results as $result_index => $result ) { |
|
| 194 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 195 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 193 | + foreach ($results as $result_index => $result) { |
|
| 194 | + $results[$result_index]['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id); |
|
| 195 | + $results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | return $results; |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | -function _wl_network_navigator_get_data( $request ) { |
|
| 201 | +function _wl_network_navigator_get_data($request) { |
|
| 202 | 202 | |
| 203 | 203 | // Limit the results (defaults to 4) |
| 204 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 205 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 204 | + $navigator_length = isset($request['limit']) ? intval($request['limit']) : 4; |
|
| 205 | + $navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0; |
|
| 206 | 206 | $navigator_id = $request['uniqid']; |
| 207 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 207 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 208 | 208 | |
| 209 | 209 | $entities = $request['entities']; |
| 210 | 210 | |
| 211 | 211 | // Post ID has to match an existing item |
| 212 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 213 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 212 | + if ( ! isset($entities) || empty($entities)) { |
|
| 213 | + wp_send_json_error('No valid entities provided'); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 216 | + $referencing_posts = _wl_network_navigator_get_results($entities, array( |
|
| 217 | 217 | 'ID', |
| 218 | 218 | 'post_title', |
| 219 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 219 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 220 | 220 | |
| 221 | 221 | // loop over them and take the first one which is not already in the $related_posts |
| 222 | 222 | $results = array(); |
| 223 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 224 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 223 | + foreach ($referencing_posts as $referencing_post) { |
|
| 224 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 225 | 225 | |
| 226 | 226 | /** |
| 227 | 227 | * Use the thumbnail. |
@@ -231,30 +231,30 @@ discard block |
||
| 231 | 231 | * |
| 232 | 232 | * @since 3.19.3 We're using the medium size image. |
| 233 | 233 | */ |
| 234 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 234 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 235 | 235 | |
| 236 | 236 | $result = array( |
| 237 | 237 | 'post' => array( |
| 238 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 238 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 239 | 239 | 'title' => $referencing_post->post_title, |
| 240 | 240 | 'thumbnail' => $thumbnail, |
| 241 | 241 | ), |
| 242 | 242 | 'entity' => array( |
| 243 | 243 | 'label' => $serialized_entity['label'], |
| 244 | 244 | 'mainType' => $serialized_entity['mainType'], |
| 245 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 245 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 246 | 246 | ), |
| 247 | 247 | ); |
| 248 | 248 | |
| 249 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 250 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 249 | + $result['post'] = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 250 | + $result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 251 | 251 | |
| 252 | 252 | $results[] = $result; |
| 253 | 253 | |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - if ( count( $results ) < $navigator_length ) { |
|
| 257 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 256 | + if (count($results) < $navigator_length) { |
|
| 257 | + $results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | return $results; |
@@ -269,24 +269,24 @@ discard block |
||
| 269 | 269 | ) { |
| 270 | 270 | global $wpdb; |
| 271 | 271 | |
| 272 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 272 | + $select = implode(', ', array_map(function($item) { |
|
| 273 | 273 | return "p.$item AS $item"; |
| 274 | - }, (array) $fields ) ); |
|
| 274 | + }, (array) $fields)); |
|
| 275 | 275 | |
| 276 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 276 | + $order_by = implode(', ', array_map(function($item) { |
|
| 277 | 277 | return "p.$item"; |
| 278 | - }, (array) $order_by ) ); |
|
| 278 | + }, (array) $order_by)); |
|
| 279 | 279 | |
| 280 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 281 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 282 | - if ( isset( $entity ) ) { |
|
| 280 | + $entities_in = implode(',', array_map(function($item) { |
|
| 281 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item)); |
|
| 282 | + if (isset($entity)) { |
|
| 283 | 283 | return $entity->ID; |
| 284 | 284 | } |
| 285 | - }, $entities ) ); |
|
| 285 | + }, $entities)); |
|
| 286 | 286 | |
| 287 | 287 | /** @noinspection SqlNoDataSourceInspection */ |
| 288 | 288 | return $wpdb->get_results( |
| 289 | - $wpdb->prepare( <<<EOF |
|
| 289 | + $wpdb->prepare(<<<EOF |
|
| 290 | 290 | SELECT %3\$s, p2.ID as entity_id |
| 291 | 291 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 292 | 292 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | LIMIT %1\$d |
| 313 | 313 | OFFSET %2\$d |
| 314 | 314 | EOF |
| 315 | - , $limit, $offset, $select, $order_by ) |
|
| 315 | + , $limit, $offset, $select, $order_by) |
|
| 316 | 316 | ); |
| 317 | 317 | |
| 318 | 318 | } |
@@ -327,12 +327,12 @@ discard block |
||
| 327 | 327 | |
| 328 | 328 | // Temporary blocking the Navigator. |
| 329 | 329 | $results = wl_shortcode_navigator_data(); |
| 330 | - wl_core_send_json( $results ); |
|
| 330 | + wl_core_send_json($results); |
|
| 331 | 331 | |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 335 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 334 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 335 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 336 | 336 | |
| 337 | 337 | /** |
| 338 | 338 | * wp-json call for the navigator widget |
@@ -340,13 +340,13 @@ discard block |
||
| 340 | 340 | function wl_shortcode_navigator_wp_json() { |
| 341 | 341 | |
| 342 | 342 | $results = wl_shortcode_navigator_data(); |
| 343 | - if ( ob_get_contents() ) { |
|
| 343 | + if (ob_get_contents()) { |
|
| 344 | 344 | ob_clean(); |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | return array( |
| 348 | 348 | 'items' => array( |
| 349 | - array( 'values' => $results ), |
|
| 349 | + array('values' => $results), |
|
| 350 | 350 | ), |
| 351 | 351 | ); |
| 352 | 352 | |
@@ -355,23 +355,23 @@ discard block |
||
| 355 | 355 | /** |
| 356 | 356 | * Adding `rest_api_init` action for amp backend of navigator |
| 357 | 357 | */ |
| 358 | -add_action( 'rest_api_init', function () { |
|
| 359 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 358 | +add_action('rest_api_init', function() { |
|
| 359 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 360 | 360 | 'methods' => 'GET', |
| 361 | 361 | 'permission_callback' => '__return_true', |
| 362 | 362 | 'callback' => 'wl_shortcode_navigator_wp_json' |
| 363 | - ) ); |
|
| 363 | + )); |
|
| 364 | 364 | } ); |
| 365 | 365 | |
| 366 | 366 | /** |
| 367 | 367 | * Adding `rest_api_init` action for backend of network navigator |
| 368 | 368 | */ |
| 369 | -add_action( 'rest_api_init', function () { |
|
| 370 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 369 | +add_action('rest_api_init', function() { |
|
| 370 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 371 | 371 | 'methods' => 'GET', |
| 372 | 372 | 'callback' => 'wl_network_navigator_wp_json', |
| 373 | 373 | 'permission_callback' => '__return_true', |
| 374 | - ) ); |
|
| 374 | + )); |
|
| 375 | 375 | } ); |
| 376 | 376 | |
| 377 | 377 | /** |
@@ -379,24 +379,24 @@ discard block |
||
| 379 | 379 | * |
| 380 | 380 | * @since 2.2.0 |
| 381 | 381 | */ |
| 382 | -add_action( 'plugins_loaded', function () { |
|
| 383 | - $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 384 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 382 | +add_action('plugins_loaded', function() { |
|
| 383 | + $action = array_key_exists('action', $_REQUEST) ? $_REQUEST['action'] : ''; |
|
| 384 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 385 | 385 | return; |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 389 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 390 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 391 | -}, 0 ); |
|
| 388 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 389 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 390 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 391 | +}, 0); |
|
| 392 | 392 | |
| 393 | -add_action( 'init', function () { |
|
| 394 | - $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 395 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 393 | +add_action('init', function() { |
|
| 394 | + $action = array_key_exists('action', $_REQUEST) ? $_REQUEST['action'] : ''; |
|
| 395 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 396 | 396 | return; |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 400 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 401 | -}, 0 ); |
|
| 399 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 400 | + remove_action('init', 'gglcptch_init'); |
|
| 401 | +}, 0); |
|
| 402 | 402 | |
@@ -19,116 +19,116 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Faceted_Search_Shortcode extends Wordlift_Shortcode { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * {@inheritdoc} |
|
| 24 | - */ |
|
| 25 | - const SHORTCODE = 'wl_faceted_search'; |
|
| 26 | - |
|
| 27 | - public function __construct() { |
|
| 28 | - parent::__construct(); |
|
| 29 | - $this->register_block_type(); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * {@inheritdoc} |
|
| 34 | - */ |
|
| 35 | - public function render( $atts ) { |
|
| 36 | - |
|
| 37 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 38 | - : $this->web_shortcode( $atts ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - private function register_block_type() { |
|
| 42 | - |
|
| 43 | - $scope = $this; |
|
| 44 | - |
|
| 45 | - add_action( 'init', function () use ( $scope ) { |
|
| 46 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 47 | - // Gutenberg is not active. |
|
| 48 | - return; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - register_block_type( 'wordlift/faceted-search', array( |
|
| 52 | - 'editor_script' => 'wl-block-editor', |
|
| 53 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 54 | - $attr_code = ''; |
|
| 55 | - foreach ( $attributes as $key => $value ) { |
|
| 56 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 60 | - }, |
|
| 61 | - |
|
| 62 | - 'attributes' => $scope->get_block_attributes(), |
|
| 63 | - ) ); |
|
| 64 | - } ); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 69 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 70 | - * |
|
| 71 | - * @param array $atts Shortcode attributes. |
|
| 72 | - * |
|
| 73 | - * @return array $shortcode_atts |
|
| 74 | - * @since 3.20.0 |
|
| 75 | - * |
|
| 76 | - */ |
|
| 77 | - private function make_shortcode_atts( $atts ) { |
|
| 78 | - |
|
| 79 | - // Extract attributes and set default values. |
|
| 80 | - $shortcode_atts = shortcode_atts( array( |
|
| 81 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 82 | - 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 83 | - 'post_id' => '', |
|
| 84 | - 'template_id' => '', |
|
| 85 | - 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 86 | - 'post_types' => '', |
|
| 87 | - ), $atts ); |
|
| 88 | - |
|
| 89 | - return $shortcode_atts; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 94 | - * |
|
| 95 | - * @param array $atts Shortcode attributes. |
|
| 96 | - * |
|
| 97 | - * @return string Shortcode HTML for web |
|
| 98 | - * @since 3.20.0 |
|
| 99 | - * |
|
| 100 | - */ |
|
| 101 | - private function web_shortcode( $atts ) { |
|
| 102 | - |
|
| 103 | - // attributes extraction and boolean filtering |
|
| 104 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 105 | - |
|
| 106 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 107 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 108 | - return; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 112 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 116 | - |
|
| 117 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 118 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 119 | - $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 120 | - $rest_url = esc_attr( $rest_url ); |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - // avoid building the widget when no valid $rest_url |
|
| 124 | - if ( ! $rest_url ) { |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 129 | - $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 130 | - |
|
| 131 | - return <<<HTML |
|
| 22 | + /** |
|
| 23 | + * {@inheritdoc} |
|
| 24 | + */ |
|
| 25 | + const SHORTCODE = 'wl_faceted_search'; |
|
| 26 | + |
|
| 27 | + public function __construct() { |
|
| 28 | + parent::__construct(); |
|
| 29 | + $this->register_block_type(); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * {@inheritdoc} |
|
| 34 | + */ |
|
| 35 | + public function render( $atts ) { |
|
| 36 | + |
|
| 37 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 38 | + : $this->web_shortcode( $atts ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + private function register_block_type() { |
|
| 42 | + |
|
| 43 | + $scope = $this; |
|
| 44 | + |
|
| 45 | + add_action( 'init', function () use ( $scope ) { |
|
| 46 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 47 | + // Gutenberg is not active. |
|
| 48 | + return; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + register_block_type( 'wordlift/faceted-search', array( |
|
| 52 | + 'editor_script' => 'wl-block-editor', |
|
| 53 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 54 | + $attr_code = ''; |
|
| 55 | + foreach ( $attributes as $key => $value ) { |
|
| 56 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 60 | + }, |
|
| 61 | + |
|
| 62 | + 'attributes' => $scope->get_block_attributes(), |
|
| 63 | + ) ); |
|
| 64 | + } ); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 69 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 70 | + * |
|
| 71 | + * @param array $atts Shortcode attributes. |
|
| 72 | + * |
|
| 73 | + * @return array $shortcode_atts |
|
| 74 | + * @since 3.20.0 |
|
| 75 | + * |
|
| 76 | + */ |
|
| 77 | + private function make_shortcode_atts( $atts ) { |
|
| 78 | + |
|
| 79 | + // Extract attributes and set default values. |
|
| 80 | + $shortcode_atts = shortcode_atts( array( |
|
| 81 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 82 | + 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 83 | + 'post_id' => '', |
|
| 84 | + 'template_id' => '', |
|
| 85 | + 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 86 | + 'post_types' => '', |
|
| 87 | + ), $atts ); |
|
| 88 | + |
|
| 89 | + return $shortcode_atts; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 94 | + * |
|
| 95 | + * @param array $atts Shortcode attributes. |
|
| 96 | + * |
|
| 97 | + * @return string Shortcode HTML for web |
|
| 98 | + * @since 3.20.0 |
|
| 99 | + * |
|
| 100 | + */ |
|
| 101 | + private function web_shortcode( $atts ) { |
|
| 102 | + |
|
| 103 | + // attributes extraction and boolean filtering |
|
| 104 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 105 | + |
|
| 106 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 107 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 108 | + return; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 112 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | + $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 116 | + |
|
| 117 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 118 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 119 | + $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 120 | + $rest_url = esc_attr( $rest_url ); |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + // avoid building the widget when no valid $rest_url |
|
| 124 | + if ( ! $rest_url ) { |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 129 | + $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 130 | + |
|
| 131 | + return <<<HTML |
|
| 132 | 132 | <!-- Faceted {$faceted_id} --> |
| 133 | 133 | <div id="{$faceted_id}" |
| 134 | 134 | class="wl-faceted" |
@@ -138,60 +138,60 @@ discard block |
||
| 138 | 138 | data-template-url="{$template_url}"></div> |
| 139 | 139 | <!-- /Faceted {$faceted_id} --> |
| 140 | 140 | HTML; |
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 145 | - * |
|
| 146 | - * @param array $atts Shortcode attributes. |
|
| 147 | - * |
|
| 148 | - * @return string Shortcode HTML for amp |
|
| 149 | - * @since 3.20.0 |
|
| 150 | - * |
|
| 151 | - */ |
|
| 152 | - private function amp_shortcode( $atts ) { |
|
| 153 | - |
|
| 154 | - // attributes extraction and boolean filtering |
|
| 155 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 156 | - |
|
| 157 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 158 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 159 | - return; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 163 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 164 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 165 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 166 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 167 | - |
|
| 168 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 169 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 170 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 171 | - 'amp', |
|
| 172 | - 'post_id' => $post->ID, |
|
| 173 | - 'limit' => $limit |
|
| 174 | - ) ) ) : false; |
|
| 175 | - |
|
| 176 | - $rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false; |
|
| 177 | - |
|
| 178 | - // avoid building the widget when no valid $rest_url |
|
| 179 | - if ( ! $rest_url ) { |
|
| 180 | - return; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 184 | - // This is a hackish way, but this works for http and https URLs |
|
| 185 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 186 | - |
|
| 187 | - if ( empty( $template_id ) ) { |
|
| 188 | - $template_id = "template-" . $faceted_id; |
|
| 189 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - $srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 193 | - |
|
| 194 | - return <<<HTML |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 145 | + * |
|
| 146 | + * @param array $atts Shortcode attributes. |
|
| 147 | + * |
|
| 148 | + * @return string Shortcode HTML for amp |
|
| 149 | + * @since 3.20.0 |
|
| 150 | + * |
|
| 151 | + */ |
|
| 152 | + private function amp_shortcode( $atts ) { |
|
| 153 | + |
|
| 154 | + // attributes extraction and boolean filtering |
|
| 155 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 156 | + |
|
| 157 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 158 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 159 | + return; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 163 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 164 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 165 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 166 | + $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 167 | + |
|
| 168 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 169 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 170 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 171 | + 'amp', |
|
| 172 | + 'post_id' => $post->ID, |
|
| 173 | + 'limit' => $limit |
|
| 174 | + ) ) ) : false; |
|
| 175 | + |
|
| 176 | + $rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false; |
|
| 177 | + |
|
| 178 | + // avoid building the widget when no valid $rest_url |
|
| 179 | + if ( ! $rest_url ) { |
|
| 180 | + return; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 184 | + // This is a hackish way, but this works for http and https URLs |
|
| 185 | + $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 186 | + |
|
| 187 | + if ( empty( $template_id ) ) { |
|
| 188 | + $template_id = "template-" . $faceted_id; |
|
| 189 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + $srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 193 | + |
|
| 194 | + return <<<HTML |
|
| 195 | 195 | <div id="{$faceted_id}" class="wl-amp-faceted"> |
| 196 | 196 | <h2 class="wl-headline">{$title}</h2> |
| 197 | 197 | <amp-state id="referencedPosts"> |
@@ -275,62 +275,62 @@ discard block |
||
| 275 | 275 | </section> |
| 276 | 276 | </div> |
| 277 | 277 | HTML; |
| 278 | - } |
|
| 279 | - |
|
| 280 | - public function get_block_attributes() { |
|
| 281 | - return array( |
|
| 282 | - 'title' => array( |
|
| 283 | - 'type' => 'string', |
|
| 284 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 285 | - ), |
|
| 286 | - 'template_id' => array( |
|
| 287 | - 'type' => 'string', |
|
| 288 | - 'default' => '', |
|
| 289 | - ), |
|
| 290 | - 'post_id' => array( |
|
| 291 | - 'type' => 'number', |
|
| 292 | - 'default' => '', |
|
| 293 | - ), |
|
| 294 | - 'uniqid' => array( |
|
| 295 | - 'type' => 'string', |
|
| 296 | - 'default' => '', |
|
| 297 | - ), |
|
| 298 | - 'limit' => array( |
|
| 299 | - 'type' => 'number', |
|
| 300 | - 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 301 | - ), |
|
| 302 | - 'preview' => array( |
|
| 303 | - 'type' => 'boolean', |
|
| 304 | - 'default' => false, |
|
| 305 | - ), |
|
| 306 | - 'preview_src' => array( |
|
| 307 | - 'type' => 'string', |
|
| 308 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 309 | - ), |
|
| 310 | - 'post_types' => array( |
|
| 311 | - 'type' => 'string', |
|
| 312 | - 'default' => '', |
|
| 313 | - ) |
|
| 314 | - ); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * @param $post |
|
| 319 | - * @param $delimiter |
|
| 320 | - * @param $limit |
|
| 321 | - * |
|
| 322 | - * @param $post_types |
|
| 323 | - * |
|
| 324 | - * @return bool|string |
|
| 325 | - */ |
|
| 326 | - public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 327 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 328 | - 'post_id' => $post->ID, |
|
| 329 | - 'limit' => $limit, |
|
| 330 | - 'post_types' => $post_types |
|
| 331 | - ) ) ) : false; |
|
| 332 | - |
|
| 333 | - return $rest_url; |
|
| 334 | - } |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + public function get_block_attributes() { |
|
| 281 | + return array( |
|
| 282 | + 'title' => array( |
|
| 283 | + 'type' => 'string', |
|
| 284 | + 'default' => __( 'Related articles', 'wordlift' ), |
|
| 285 | + ), |
|
| 286 | + 'template_id' => array( |
|
| 287 | + 'type' => 'string', |
|
| 288 | + 'default' => '', |
|
| 289 | + ), |
|
| 290 | + 'post_id' => array( |
|
| 291 | + 'type' => 'number', |
|
| 292 | + 'default' => '', |
|
| 293 | + ), |
|
| 294 | + 'uniqid' => array( |
|
| 295 | + 'type' => 'string', |
|
| 296 | + 'default' => '', |
|
| 297 | + ), |
|
| 298 | + 'limit' => array( |
|
| 299 | + 'type' => 'number', |
|
| 300 | + 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 301 | + ), |
|
| 302 | + 'preview' => array( |
|
| 303 | + 'type' => 'boolean', |
|
| 304 | + 'default' => false, |
|
| 305 | + ), |
|
| 306 | + 'preview_src' => array( |
|
| 307 | + 'type' => 'string', |
|
| 308 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 309 | + ), |
|
| 310 | + 'post_types' => array( |
|
| 311 | + 'type' => 'string', |
|
| 312 | + 'default' => '', |
|
| 313 | + ) |
|
| 314 | + ); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * @param $post |
|
| 319 | + * @param $delimiter |
|
| 320 | + * @param $limit |
|
| 321 | + * |
|
| 322 | + * @param $post_types |
|
| 323 | + * |
|
| 324 | + * @return bool|string |
|
| 325 | + */ |
|
| 326 | + public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 327 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 328 | + 'post_id' => $post->ID, |
|
| 329 | + 'limit' => $limit, |
|
| 330 | + 'post_types' => $post_types |
|
| 331 | + ) ) ) : false; |
|
| 332 | + |
|
| 333 | + return $rest_url; |
|
| 334 | + } |
|
| 335 | 335 | |
| 336 | 336 | } |
@@ -32,35 +32,35 @@ discard block |
||
| 32 | 32 | /** |
| 33 | 33 | * {@inheritdoc} |
| 34 | 34 | */ |
| 35 | - public function render( $atts ) { |
|
| 35 | + public function render($atts) { |
|
| 36 | 36 | |
| 37 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 38 | - : $this->web_shortcode( $atts ); |
|
| 37 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode($atts) |
|
| 38 | + : $this->web_shortcode($atts); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | private function register_block_type() { |
| 42 | 42 | |
| 43 | 43 | $scope = $this; |
| 44 | 44 | |
| 45 | - add_action( 'init', function () use ( $scope ) { |
|
| 46 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 45 | + add_action('init', function() use ($scope) { |
|
| 46 | + if ( ! function_exists('register_block_type')) { |
|
| 47 | 47 | // Gutenberg is not active. |
| 48 | 48 | return; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - register_block_type( 'wordlift/faceted-search', array( |
|
| 51 | + register_block_type('wordlift/faceted-search', array( |
|
| 52 | 52 | 'editor_script' => 'wl-block-editor', |
| 53 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 53 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 54 | 54 | $attr_code = ''; |
| 55 | - foreach ( $attributes as $key => $value ) { |
|
| 56 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 55 | + foreach ($attributes as $key => $value) { |
|
| 56 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 59 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 60 | 60 | }, |
| 61 | 61 | |
| 62 | 62 | 'attributes' => $scope->get_block_attributes(), |
| 63 | - ) ); |
|
| 63 | + )); |
|
| 64 | 64 | } ); |
| 65 | 65 | } |
| 66 | 66 | |
@@ -74,17 +74,17 @@ discard block |
||
| 74 | 74 | * @since 3.20.0 |
| 75 | 75 | * |
| 76 | 76 | */ |
| 77 | - private function make_shortcode_atts( $atts ) { |
|
| 77 | + private function make_shortcode_atts($atts) { |
|
| 78 | 78 | |
| 79 | 79 | // Extract attributes and set default values. |
| 80 | - $shortcode_atts = shortcode_atts( array( |
|
| 81 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 82 | - 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 80 | + $shortcode_atts = shortcode_atts(array( |
|
| 81 | + 'title' => __('Related articles', 'wordlift'), |
|
| 82 | + 'limit' => apply_filters('wl_faceted_search_default_limit', 10), |
|
| 83 | 83 | 'post_id' => '', |
| 84 | 84 | 'template_id' => '', |
| 85 | - 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 85 | + 'uniqid' => uniqid('wl-faceted-widget-'), |
|
| 86 | 86 | 'post_types' => '', |
| 87 | - ), $atts ); |
|
| 87 | + ), $atts); |
|
| 88 | 88 | |
| 89 | 89 | return $shortcode_atts; |
| 90 | 90 | } |
@@ -98,35 +98,35 @@ discard block |
||
| 98 | 98 | * @since 3.20.0 |
| 99 | 99 | * |
| 100 | 100 | */ |
| 101 | - private function web_shortcode( $atts ) { |
|
| 101 | + private function web_shortcode($atts) { |
|
| 102 | 102 | |
| 103 | 103 | // attributes extraction and boolean filtering |
| 104 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 104 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 105 | 105 | |
| 106 | 106 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 107 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 107 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 108 | 108 | return; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 112 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 111 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 112 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 113 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 114 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 115 | + $faceted_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-'); |
|
| 116 | 116 | |
| 117 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 118 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 119 | - $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 120 | - $rest_url = esc_attr( $rest_url ); |
|
| 117 | + $permalink_structure = get_option('permalink_structure'); |
|
| 118 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 119 | + $rest_url = $this->get_rest_url($post, $delimiter, $limit, $shortcode_atts['post_types']); |
|
| 120 | + $rest_url = esc_attr($rest_url); |
|
| 121 | 121 | |
| 122 | 122 | |
| 123 | 123 | // avoid building the widget when no valid $rest_url |
| 124 | - if ( ! $rest_url ) { |
|
| 124 | + if ( ! $rest_url) { |
|
| 125 | 125 | return; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 129 | - $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 128 | + wp_enqueue_script('wordlift-cloud'); |
|
| 129 | + $template_url = get_rest_url(null, '/wordlift/v1/faceted-search/template'); |
|
| 130 | 130 | |
| 131 | 131 | return <<<HTML |
| 132 | 132 | <!-- Faceted {$faceted_id} --> |
@@ -149,47 +149,47 @@ discard block |
||
| 149 | 149 | * @since 3.20.0 |
| 150 | 150 | * |
| 151 | 151 | */ |
| 152 | - private function amp_shortcode( $atts ) { |
|
| 152 | + private function amp_shortcode($atts) { |
|
| 153 | 153 | |
| 154 | 154 | // attributes extraction and boolean filtering |
| 155 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 155 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 156 | 156 | |
| 157 | 157 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 158 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 158 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 159 | 159 | return; |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 163 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 164 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 165 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 166 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 162 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 163 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 164 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 165 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 166 | + $faceted_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-'); |
|
| 167 | 167 | |
| 168 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 169 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 170 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 168 | + $permalink_structure = get_option('permalink_structure'); |
|
| 169 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 170 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query(array( |
|
| 171 | 171 | 'amp', |
| 172 | 172 | 'post_id' => $post->ID, |
| 173 | 173 | 'limit' => $limit |
| 174 | - ) ) ) : false; |
|
| 174 | + ))) : false; |
|
| 175 | 175 | |
| 176 | - $rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false; |
|
| 176 | + $rest_url = $post ? rest_url(sprintf("wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit)) : false; |
|
| 177 | 177 | |
| 178 | 178 | // avoid building the widget when no valid $rest_url |
| 179 | - if ( ! $rest_url ) { |
|
| 179 | + if ( ! $rest_url) { |
|
| 180 | 180 | return; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 184 | 184 | // This is a hackish way, but this works for http and https URLs |
| 185 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 185 | + $rest_url = str_replace(array('http:', 'https:'), '', $rest_url); |
|
| 186 | 186 | |
| 187 | - if ( empty( $template_id ) ) { |
|
| 188 | - $template_id = "template-" . $faceted_id; |
|
| 189 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 187 | + if (empty($template_id)) { |
|
| 188 | + $template_id = "template-".$faceted_id; |
|
| 189 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css'); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - $srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 192 | + $srcset = Srcset_Util::get_srcset($post->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
| 193 | 193 | |
| 194 | 194 | return <<<HTML |
| 195 | 195 | <div id="{$faceted_id}" class="wl-amp-faceted"> |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | return array( |
| 282 | 282 | 'title' => array( |
| 283 | 283 | 'type' => 'string', |
| 284 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 284 | + 'default' => __('Related articles', 'wordlift'), |
|
| 285 | 285 | ), |
| 286 | 286 | 'template_id' => array( |
| 287 | 287 | 'type' => 'string', |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | ), |
| 298 | 298 | 'limit' => array( |
| 299 | 299 | 'type' => 'number', |
| 300 | - 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 300 | + 'default' => apply_filters('wl_faceted_search_default_limit', 10), |
|
| 301 | 301 | ), |
| 302 | 302 | 'preview' => array( |
| 303 | 303 | 'type' => 'boolean', |
@@ -305,7 +305,7 @@ discard block |
||
| 305 | 305 | ), |
| 306 | 306 | 'preview_src' => array( |
| 307 | 307 | 'type' => 'string', |
| 308 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 308 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 309 | 309 | ), |
| 310 | 310 | 'post_types' => array( |
| 311 | 311 | 'type' => 'string', |
@@ -323,12 +323,12 @@ discard block |
||
| 323 | 323 | * |
| 324 | 324 | * @return bool|string |
| 325 | 325 | */ |
| 326 | - public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 327 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 326 | + public function get_rest_url($post, $delimiter, $limit, $post_types) { |
|
| 327 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query(array( |
|
| 328 | 328 | 'post_id' => $post->ID, |
| 329 | 329 | 'limit' => $limit, |
| 330 | 330 | 'post_types' => $post_types |
| 331 | - ) ) ) : false; |
|
| 331 | + ))) : false; |
|
| 332 | 332 | |
| 333 | 333 | return $rest_url; |
| 334 | 334 | } |
@@ -19,122 +19,122 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Navigator_Shortcode extends Wordlift_Shortcode { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * {@inheritdoc} |
|
| 24 | - */ |
|
| 25 | - const SHORTCODE = 'wl_navigator'; |
|
| 22 | + /** |
|
| 23 | + * {@inheritdoc} |
|
| 24 | + */ |
|
| 25 | + const SHORTCODE = 'wl_navigator'; |
|
| 26 | 26 | |
| 27 | - public function __construct() { |
|
| 28 | - parent::__construct(); |
|
| 29 | - $this->register_block_type(); |
|
| 30 | - } |
|
| 27 | + public function __construct() { |
|
| 28 | + parent::__construct(); |
|
| 29 | + $this->register_block_type(); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * {@inheritdoc} |
|
| 34 | - */ |
|
| 35 | - public function render( $atts ) { |
|
| 36 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 37 | - : $this->web_shortcode( $atts ); |
|
| 38 | - } |
|
| 32 | + /** |
|
| 33 | + * {@inheritdoc} |
|
| 34 | + */ |
|
| 35 | + public function render( $atts ) { |
|
| 36 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 37 | + : $this->web_shortcode( $atts ); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - private function register_block_type() { |
|
| 40 | + private function register_block_type() { |
|
| 41 | 41 | |
| 42 | - $scope = $this; |
|
| 42 | + $scope = $this; |
|
| 43 | 43 | |
| 44 | - add_action( 'init', function () use ( $scope ) { |
|
| 45 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 46 | - // Gutenberg is not active. |
|
| 47 | - return; |
|
| 48 | - } |
|
| 44 | + add_action( 'init', function () use ( $scope ) { |
|
| 45 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 46 | + // Gutenberg is not active. |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - register_block_type( 'wordlift/navigator', array( |
|
| 51 | - 'editor_script' => 'wl-block-editor', |
|
| 52 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 53 | - $attr_code = ''; |
|
| 54 | - foreach ( $attributes as $key => $value ) { |
|
| 55 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 56 | - } |
|
| 50 | + register_block_type( 'wordlift/navigator', array( |
|
| 51 | + 'editor_script' => 'wl-block-editor', |
|
| 52 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 53 | + $attr_code = ''; |
|
| 54 | + foreach ( $attributes as $key => $value ) { |
|
| 55 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 59 | - }, |
|
| 60 | - 'attributes' => $scope->get_navigator_block_attributes(), |
|
| 61 | - ) ); |
|
| 62 | - } ); |
|
| 63 | - } |
|
| 58 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 59 | + }, |
|
| 60 | + 'attributes' => $scope->get_navigator_block_attributes(), |
|
| 61 | + ) ); |
|
| 62 | + } ); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 67 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 68 | - * |
|
| 69 | - * @param array $atts Shortcode attributes. |
|
| 70 | - * |
|
| 71 | - * @return array $shortcode_atts |
|
| 72 | - * @since 3.20.0 |
|
| 73 | - * |
|
| 74 | - */ |
|
| 75 | - private function make_shortcode_atts( $atts ) { |
|
| 65 | + /** |
|
| 66 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 67 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 68 | + * |
|
| 69 | + * @param array $atts Shortcode attributes. |
|
| 70 | + * |
|
| 71 | + * @return array $shortcode_atts |
|
| 72 | + * @since 3.20.0 |
|
| 73 | + * |
|
| 74 | + */ |
|
| 75 | + private function make_shortcode_atts( $atts ) { |
|
| 76 | 76 | |
| 77 | - // Extract attributes and set default values. |
|
| 78 | - $shortcode_atts = shortcode_atts( array( |
|
| 79 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | - 'limit' => 4, |
|
| 81 | - 'offset' => 0, |
|
| 82 | - 'template_id' => '', |
|
| 83 | - 'post_id' => '', |
|
| 84 | - 'uniqid' => uniqid( 'wl-navigator-widget-' ), |
|
| 85 | - 'order_by' => 'ID DESC', |
|
| 86 | - 'post_types' => '', |
|
| 87 | - ), $atts ); |
|
| 77 | + // Extract attributes and set default values. |
|
| 78 | + $shortcode_atts = shortcode_atts( array( |
|
| 79 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | + 'limit' => 4, |
|
| 81 | + 'offset' => 0, |
|
| 82 | + 'template_id' => '', |
|
| 83 | + 'post_id' => '', |
|
| 84 | + 'uniqid' => uniqid( 'wl-navigator-widget-' ), |
|
| 85 | + 'order_by' => 'ID DESC', |
|
| 86 | + 'post_types' => '', |
|
| 87 | + ), $atts ); |
|
| 88 | 88 | |
| 89 | - return $shortcode_atts; |
|
| 90 | - } |
|
| 89 | + return $shortcode_atts; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Function in charge of displaying the [wl-navigator] in web mode. |
|
| 94 | - * |
|
| 95 | - * @param array $atts Shortcode attributes. |
|
| 96 | - * |
|
| 97 | - * @return string Shortcode HTML for web |
|
| 98 | - * @since 3.20.0 |
|
| 99 | - * |
|
| 100 | - */ |
|
| 101 | - private function web_shortcode( $atts ) { |
|
| 92 | + /** |
|
| 93 | + * Function in charge of displaying the [wl-navigator] in web mode. |
|
| 94 | + * |
|
| 95 | + * @param array $atts Shortcode attributes. |
|
| 96 | + * |
|
| 97 | + * @return string Shortcode HTML for web |
|
| 98 | + * @since 3.20.0 |
|
| 99 | + * |
|
| 100 | + */ |
|
| 101 | + private function web_shortcode( $atts ) { |
|
| 102 | 102 | |
| 103 | - // attributes extraction and boolean filtering |
|
| 104 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 103 | + // attributes extraction and boolean filtering |
|
| 104 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 105 | 105 | |
| 106 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 107 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 108 | - return; |
|
| 109 | - } |
|
| 106 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 107 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 108 | + return; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 112 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 116 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 117 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 111 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 112 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | + $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 116 | + $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 117 | + $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 118 | 118 | |
| 119 | - $rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array( |
|
| 120 | - 'action' => 'wl_navigator', |
|
| 121 | - 'uniqid' => $navigator_id, |
|
| 122 | - 'post_id' => $post->ID, |
|
| 123 | - 'limit' => $limit, |
|
| 124 | - 'offset' => $offset, |
|
| 125 | - 'sort' => $sort, |
|
| 126 | - 'post_types' => $shortcode_atts['post_types'] |
|
| 127 | - ) ) ) : false; |
|
| 119 | + $rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array( |
|
| 120 | + 'action' => 'wl_navigator', |
|
| 121 | + 'uniqid' => $navigator_id, |
|
| 122 | + 'post_id' => $post->ID, |
|
| 123 | + 'limit' => $limit, |
|
| 124 | + 'offset' => $offset, |
|
| 125 | + 'sort' => $sort, |
|
| 126 | + 'post_types' => $shortcode_atts['post_types'] |
|
| 127 | + ) ) ) : false; |
|
| 128 | 128 | |
| 129 | - // avoid building the widget when no valid $rest_url |
|
| 130 | - if ( ! $rest_url ) { |
|
| 131 | - return; |
|
| 132 | - } |
|
| 129 | + // avoid building the widget when no valid $rest_url |
|
| 130 | + if ( ! $rest_url ) { |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 135 | - $template_url = get_rest_url( null, '/wordlift/v1/navigator/template' ); |
|
| 134 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 135 | + $template_url = get_rest_url( null, '/wordlift/v1/navigator/template' ); |
|
| 136 | 136 | |
| 137 | - return <<<HTML |
|
| 137 | + return <<<HTML |
|
| 138 | 138 | <!-- Navigator {$navigator_id} --> |
| 139 | 139 | <div id="{$navigator_id}" |
| 140 | 140 | class="wl-navigator" |
@@ -145,59 +145,59 @@ discard block |
||
| 145 | 145 | data-limit="{$limit}"></div> |
| 146 | 146 | <!-- /Navigator {$navigator_id} --> |
| 147 | 147 | HTML; |
| 148 | - } |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 152 | - * |
|
| 153 | - * @param array $atts Shortcode attributes. |
|
| 154 | - * |
|
| 155 | - * @return string Shortcode HTML for amp |
|
| 156 | - * @since 3.20.0 |
|
| 157 | - * |
|
| 158 | - */ |
|
| 159 | - private function amp_shortcode( $atts ) { |
|
| 160 | - // attributes extraction and boolean filtering |
|
| 161 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 150 | + /** |
|
| 151 | + * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 152 | + * |
|
| 153 | + * @param array $atts Shortcode attributes. |
|
| 154 | + * |
|
| 155 | + * @return string Shortcode HTML for amp |
|
| 156 | + * @since 3.20.0 |
|
| 157 | + * |
|
| 158 | + */ |
|
| 159 | + private function amp_shortcode( $atts ) { |
|
| 160 | + // attributes extraction and boolean filtering |
|
| 161 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 162 | 162 | |
| 163 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 164 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 165 | - return; |
|
| 166 | - } |
|
| 163 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 164 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 165 | + return; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 169 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 170 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 171 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 172 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 173 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 174 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 168 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 169 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 170 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 171 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 172 | + $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 173 | + $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 174 | + $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 175 | 175 | |
| 176 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 177 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 178 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array( |
|
| 179 | - 'uniqid' => $navigator_id, |
|
| 180 | - 'post_id' => $post->ID, |
|
| 181 | - 'limit' => $limit, |
|
| 182 | - 'offset' => $offset, |
|
| 183 | - 'sort' => $sort |
|
| 184 | - ) ) ) : false; |
|
| 176 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 177 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 178 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array( |
|
| 179 | + 'uniqid' => $navigator_id, |
|
| 180 | + 'post_id' => $post->ID, |
|
| 181 | + 'limit' => $limit, |
|
| 182 | + 'offset' => $offset, |
|
| 183 | + 'sort' => $sort |
|
| 184 | + ) ) ) : false; |
|
| 185 | 185 | |
| 186 | - // avoid building the widget when no valid $rest_url |
|
| 187 | - if ( ! $rest_url ) { |
|
| 188 | - return; |
|
| 189 | - } |
|
| 186 | + // avoid building the widget when no valid $rest_url |
|
| 187 | + if ( ! $rest_url ) { |
|
| 188 | + return; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 192 | - // This is a hackish way, but this works for http and https URLs |
|
| 193 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 191 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 192 | + // This is a hackish way, but this works for http and https URLs |
|
| 193 | + $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 194 | 194 | |
| 195 | - if ( empty( $template_id ) ) { |
|
| 196 | - $template_id = "template-" . $navigator_id; |
|
| 197 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 198 | - } |
|
| 195 | + if ( empty( $template_id ) ) { |
|
| 196 | + $template_id = "template-" . $navigator_id; |
|
| 197 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - return <<<HTML |
|
| 200 | + return <<<HTML |
|
| 201 | 201 | <div id="{$navigator_id}" class="wl-amp-navigator" style="width: 100%"> |
| 202 | 202 | <h3 class="wl-headline">{$title}</h3> |
| 203 | 203 | <amp-list |
@@ -230,54 +230,54 @@ discard block |
||
| 230 | 230 | </div> |
| 231 | 231 | </template> |
| 232 | 232 | HTML; |
| 233 | - } |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - /** |
|
| 236 | - * @return array |
|
| 237 | - */ |
|
| 238 | - public function get_navigator_block_attributes() { |
|
| 239 | - return array( |
|
| 240 | - 'title' => array( |
|
| 241 | - 'type' => 'string', |
|
| 242 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 243 | - ), |
|
| 244 | - 'limit' => array( |
|
| 245 | - 'type' => 'number', |
|
| 246 | - 'default' => 4, |
|
| 247 | - ), |
|
| 248 | - 'template_id' => array( |
|
| 249 | - 'type' => 'string', |
|
| 250 | - 'default' => '', |
|
| 251 | - ), |
|
| 252 | - 'post_id' => array( |
|
| 253 | - 'type' => 'number', |
|
| 254 | - 'default' => '', |
|
| 255 | - ), |
|
| 256 | - 'offset' => array( |
|
| 257 | - 'type' => 'number', |
|
| 258 | - 'default' => 0, |
|
| 259 | - ), |
|
| 260 | - 'uniqid' => array( |
|
| 261 | - 'type' => 'string', |
|
| 262 | - 'default' => '', |
|
| 263 | - ), |
|
| 264 | - 'order_by' => array( |
|
| 265 | - 'type' => 'string', |
|
| 266 | - 'default' => 'ID DESC', |
|
| 267 | - ), |
|
| 268 | - 'preview' => array( |
|
| 269 | - 'type' => 'boolean', |
|
| 270 | - 'default' => false, |
|
| 271 | - ), |
|
| 272 | - 'preview_src' => array( |
|
| 273 | - 'type' => 'string', |
|
| 274 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png', |
|
| 275 | - ), |
|
| 276 | - 'post_types' => array( |
|
| 277 | - 'type' => 'string', |
|
| 278 | - 'default' => '', |
|
| 279 | - ) |
|
| 280 | - ); |
|
| 281 | - } |
|
| 235 | + /** |
|
| 236 | + * @return array |
|
| 237 | + */ |
|
| 238 | + public function get_navigator_block_attributes() { |
|
| 239 | + return array( |
|
| 240 | + 'title' => array( |
|
| 241 | + 'type' => 'string', |
|
| 242 | + 'default' => __( 'Related articles', 'wordlift' ), |
|
| 243 | + ), |
|
| 244 | + 'limit' => array( |
|
| 245 | + 'type' => 'number', |
|
| 246 | + 'default' => 4, |
|
| 247 | + ), |
|
| 248 | + 'template_id' => array( |
|
| 249 | + 'type' => 'string', |
|
| 250 | + 'default' => '', |
|
| 251 | + ), |
|
| 252 | + 'post_id' => array( |
|
| 253 | + 'type' => 'number', |
|
| 254 | + 'default' => '', |
|
| 255 | + ), |
|
| 256 | + 'offset' => array( |
|
| 257 | + 'type' => 'number', |
|
| 258 | + 'default' => 0, |
|
| 259 | + ), |
|
| 260 | + 'uniqid' => array( |
|
| 261 | + 'type' => 'string', |
|
| 262 | + 'default' => '', |
|
| 263 | + ), |
|
| 264 | + 'order_by' => array( |
|
| 265 | + 'type' => 'string', |
|
| 266 | + 'default' => 'ID DESC', |
|
| 267 | + ), |
|
| 268 | + 'preview' => array( |
|
| 269 | + 'type' => 'boolean', |
|
| 270 | + 'default' => false, |
|
| 271 | + ), |
|
| 272 | + 'preview_src' => array( |
|
| 273 | + 'type' => 'string', |
|
| 274 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png', |
|
| 275 | + ), |
|
| 276 | + 'post_types' => array( |
|
| 277 | + 'type' => 'string', |
|
| 278 | + 'default' => '', |
|
| 279 | + ) |
|
| 280 | + ); |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | 283 | } |
@@ -32,33 +32,33 @@ discard block |
||
| 32 | 32 | /** |
| 33 | 33 | * {@inheritdoc} |
| 34 | 34 | */ |
| 35 | - public function render( $atts ) { |
|
| 36 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 37 | - : $this->web_shortcode( $atts ); |
|
| 35 | + public function render($atts) { |
|
| 36 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode($atts) |
|
| 37 | + : $this->web_shortcode($atts); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | private function register_block_type() { |
| 41 | 41 | |
| 42 | 42 | $scope = $this; |
| 43 | 43 | |
| 44 | - add_action( 'init', function () use ( $scope ) { |
|
| 45 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 44 | + add_action('init', function() use ($scope) { |
|
| 45 | + if ( ! function_exists('register_block_type')) { |
|
| 46 | 46 | // Gutenberg is not active. |
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - register_block_type( 'wordlift/navigator', array( |
|
| 50 | + register_block_type('wordlift/navigator', array( |
|
| 51 | 51 | 'editor_script' => 'wl-block-editor', |
| 52 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 52 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 53 | 53 | $attr_code = ''; |
| 54 | - foreach ( $attributes as $key => $value ) { |
|
| 55 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 54 | + foreach ($attributes as $key => $value) { |
|
| 55 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 58 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 59 | 59 | }, |
| 60 | 60 | 'attributes' => $scope->get_navigator_block_attributes(), |
| 61 | - ) ); |
|
| 61 | + )); |
|
| 62 | 62 | } ); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -72,19 +72,19 @@ discard block |
||
| 72 | 72 | * @since 3.20.0 |
| 73 | 73 | * |
| 74 | 74 | */ |
| 75 | - private function make_shortcode_atts( $atts ) { |
|
| 75 | + private function make_shortcode_atts($atts) { |
|
| 76 | 76 | |
| 77 | 77 | // Extract attributes and set default values. |
| 78 | - $shortcode_atts = shortcode_atts( array( |
|
| 79 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 78 | + $shortcode_atts = shortcode_atts(array( |
|
| 79 | + 'title' => __('Related articles', 'wordlift'), |
|
| 80 | 80 | 'limit' => 4, |
| 81 | 81 | 'offset' => 0, |
| 82 | 82 | 'template_id' => '', |
| 83 | 83 | 'post_id' => '', |
| 84 | - 'uniqid' => uniqid( 'wl-navigator-widget-' ), |
|
| 84 | + 'uniqid' => uniqid('wl-navigator-widget-'), |
|
| 85 | 85 | 'order_by' => 'ID DESC', |
| 86 | 86 | 'post_types' => '', |
| 87 | - ), $atts ); |
|
| 87 | + ), $atts); |
|
| 88 | 88 | |
| 89 | 89 | return $shortcode_atts; |
| 90 | 90 | } |
@@ -98,25 +98,25 @@ discard block |
||
| 98 | 98 | * @since 3.20.0 |
| 99 | 99 | * |
| 100 | 100 | */ |
| 101 | - private function web_shortcode( $atts ) { |
|
| 101 | + private function web_shortcode($atts) { |
|
| 102 | 102 | |
| 103 | 103 | // attributes extraction and boolean filtering |
| 104 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 104 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 105 | 105 | |
| 106 | 106 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 107 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 107 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 108 | 108 | return; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 112 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 116 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 117 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 111 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 112 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 113 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 114 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 115 | + $offset = esc_attr(sanitize_text_field($shortcode_atts['offset'])); |
|
| 116 | + $sort = esc_attr(sanitize_sql_orderby(sanitize_text_field($shortcode_atts['order_by']))); |
|
| 117 | + $navigator_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-navigator-widget-'); |
|
| 118 | 118 | |
| 119 | - $rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array( |
|
| 119 | + $rest_url = $post ? admin_url('admin-ajax.php?'.build_query(array( |
|
| 120 | 120 | 'action' => 'wl_navigator', |
| 121 | 121 | 'uniqid' => $navigator_id, |
| 122 | 122 | 'post_id' => $post->ID, |
@@ -124,15 +124,15 @@ discard block |
||
| 124 | 124 | 'offset' => $offset, |
| 125 | 125 | 'sort' => $sort, |
| 126 | 126 | 'post_types' => $shortcode_atts['post_types'] |
| 127 | - ) ) ) : false; |
|
| 127 | + ))) : false; |
|
| 128 | 128 | |
| 129 | 129 | // avoid building the widget when no valid $rest_url |
| 130 | - if ( ! $rest_url ) { |
|
| 130 | + if ( ! $rest_url) { |
|
| 131 | 131 | return; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 135 | - $template_url = get_rest_url( null, '/wordlift/v1/navigator/template' ); |
|
| 134 | + wp_enqueue_script('wordlift-cloud'); |
|
| 135 | + $template_url = get_rest_url(null, '/wordlift/v1/navigator/template'); |
|
| 136 | 136 | |
| 137 | 137 | return <<<HTML |
| 138 | 138 | <!-- Navigator {$navigator_id} --> |
@@ -156,45 +156,45 @@ discard block |
||
| 156 | 156 | * @since 3.20.0 |
| 157 | 157 | * |
| 158 | 158 | */ |
| 159 | - private function amp_shortcode( $atts ) { |
|
| 159 | + private function amp_shortcode($atts) { |
|
| 160 | 160 | // attributes extraction and boolean filtering |
| 161 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 161 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 162 | 162 | |
| 163 | 163 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 164 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 164 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 165 | 165 | return; |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 169 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 170 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 171 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 172 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 173 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 174 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 168 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 169 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 170 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 171 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 172 | + $offset = esc_attr(sanitize_text_field($shortcode_atts['offset'])); |
|
| 173 | + $sort = esc_attr(sanitize_sql_orderby(sanitize_text_field($shortcode_atts['order_by']))); |
|
| 174 | + $navigator_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-navigator-widget-'); |
|
| 175 | 175 | |
| 176 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 177 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 178 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array( |
|
| 176 | + $permalink_structure = get_option('permalink_structure'); |
|
| 177 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 178 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/navigator'.$delimiter.build_query(array( |
|
| 179 | 179 | 'uniqid' => $navigator_id, |
| 180 | 180 | 'post_id' => $post->ID, |
| 181 | 181 | 'limit' => $limit, |
| 182 | 182 | 'offset' => $offset, |
| 183 | 183 | 'sort' => $sort |
| 184 | - ) ) ) : false; |
|
| 184 | + ))) : false; |
|
| 185 | 185 | |
| 186 | 186 | // avoid building the widget when no valid $rest_url |
| 187 | - if ( ! $rest_url ) { |
|
| 187 | + if ( ! $rest_url) { |
|
| 188 | 188 | return; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 192 | 192 | // This is a hackish way, but this works for http and https URLs |
| 193 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 193 | + $rest_url = str_replace(array('http:', 'https:'), '', $rest_url); |
|
| 194 | 194 | |
| 195 | - if ( empty( $template_id ) ) { |
|
| 196 | - $template_id = "template-" . $navigator_id; |
|
| 197 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 195 | + if (empty($template_id)) { |
|
| 196 | + $template_id = "template-".$navigator_id; |
|
| 197 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css'); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | return <<<HTML |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | return array( |
| 240 | 240 | 'title' => array( |
| 241 | 241 | 'type' => 'string', |
| 242 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 242 | + 'default' => __('Related articles', 'wordlift'), |
|
| 243 | 243 | ), |
| 244 | 244 | 'limit' => array( |
| 245 | 245 | 'type' => 'number', |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | ), |
| 272 | 272 | 'preview_src' => array( |
| 273 | 273 | 'type' => 'string', |
| 274 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png', |
|
| 274 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/navigator.png', |
|
| 275 | 275 | ), |
| 276 | 276 | 'post_types' => array( |
| 277 | 277 | 'type' => 'string', |
@@ -9,101 +9,101 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Filler_Posts_Util { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var array<Filler_Posts> |
|
| 14 | - */ |
|
| 15 | - private $sources = array(); |
|
| 16 | - |
|
| 17 | - public function __construct( $post_id ) { |
|
| 18 | - |
|
| 19 | - $post_type = get_post_type( $post_id ); |
|
| 20 | - |
|
| 21 | - if ( $post_type === 'post' ) { |
|
| 22 | - $this->sources = array( |
|
| 23 | - new Same_Category_Filler_Posts( $post_id ), |
|
| 24 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 25 | - ); |
|
| 26 | - } else { |
|
| 27 | - $this->sources = array( |
|
| 28 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 29 | - ); |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param $posts array<\WP_Post> |
|
| 36 | - * |
|
| 37 | - * @return array<int> |
|
| 38 | - */ |
|
| 39 | - private function extract_post_ids( $posts ) { |
|
| 40 | - return array_map( function ( $post ) { |
|
| 41 | - /** |
|
| 42 | - * @var $post \WP_Post |
|
| 43 | - */ |
|
| 44 | - return $post->ID; |
|
| 45 | - }, $posts ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 49 | - |
|
| 50 | - $filler_posts = array(); |
|
| 51 | - |
|
| 52 | - foreach ( $this->sources as $source ) { |
|
| 53 | - |
|
| 54 | - if ( $filler_count <= 0 ) { |
|
| 55 | - break; |
|
| 56 | - } |
|
| 57 | - /** |
|
| 58 | - * @var Filler_Posts $source |
|
| 59 | - */ |
|
| 60 | - $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
|
| 61 | - $source->filler_count = $filler_count; |
|
| 62 | - |
|
| 63 | - $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 64 | - $post_ids = $this->extract_post_ids( $posts ); |
|
| 65 | - |
|
| 66 | - // Update the post ids, filler posts and filler count |
|
| 67 | - $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 68 | - $filler_count = $filler_count - count( $posts ); |
|
| 69 | - $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return $filler_posts; |
|
| 75 | - |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Called by wordlift navigator, converts all the posts to response format. |
|
| 80 | - * @param $filler_count |
|
| 81 | - * @param $post_ids_to_be_excluded |
|
| 82 | - * |
|
| 83 | - * @return array |
|
| 84 | - */ |
|
| 85 | - public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 86 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 87 | - // Add thumbnail and permalink to filler posts |
|
| 88 | - $filler_response = array(); |
|
| 89 | - foreach ( $filler_posts as $post_obj ) { |
|
| 90 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 91 | - $filler_response[] = array( |
|
| 92 | - 'post' => array( |
|
| 93 | - 'id' => $post_obj->ID, |
|
| 94 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 95 | - 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 96 | - 'title' => get_the_title( $post_obj->ID ), |
|
| 97 | - 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 98 | - ), |
|
| 99 | - 'entity' => array( |
|
| 100 | - 'id' => 0 |
|
| 101 | - ) |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - return $filler_response; |
|
| 106 | - } |
|
| 12 | + /** |
|
| 13 | + * @var array<Filler_Posts> |
|
| 14 | + */ |
|
| 15 | + private $sources = array(); |
|
| 16 | + |
|
| 17 | + public function __construct( $post_id ) { |
|
| 18 | + |
|
| 19 | + $post_type = get_post_type( $post_id ); |
|
| 20 | + |
|
| 21 | + if ( $post_type === 'post' ) { |
|
| 22 | + $this->sources = array( |
|
| 23 | + new Same_Category_Filler_Posts( $post_id ), |
|
| 24 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 25 | + ); |
|
| 26 | + } else { |
|
| 27 | + $this->sources = array( |
|
| 28 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 29 | + ); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param $posts array<\WP_Post> |
|
| 36 | + * |
|
| 37 | + * @return array<int> |
|
| 38 | + */ |
|
| 39 | + private function extract_post_ids( $posts ) { |
|
| 40 | + return array_map( function ( $post ) { |
|
| 41 | + /** |
|
| 42 | + * @var $post \WP_Post |
|
| 43 | + */ |
|
| 44 | + return $post->ID; |
|
| 45 | + }, $posts ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 49 | + |
|
| 50 | + $filler_posts = array(); |
|
| 51 | + |
|
| 52 | + foreach ( $this->sources as $source ) { |
|
| 53 | + |
|
| 54 | + if ( $filler_count <= 0 ) { |
|
| 55 | + break; |
|
| 56 | + } |
|
| 57 | + /** |
|
| 58 | + * @var Filler_Posts $source |
|
| 59 | + */ |
|
| 60 | + $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
|
| 61 | + $source->filler_count = $filler_count; |
|
| 62 | + |
|
| 63 | + $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 64 | + $post_ids = $this->extract_post_ids( $posts ); |
|
| 65 | + |
|
| 66 | + // Update the post ids, filler posts and filler count |
|
| 67 | + $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 68 | + $filler_count = $filler_count - count( $posts ); |
|
| 69 | + $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return $filler_posts; |
|
| 75 | + |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Called by wordlift navigator, converts all the posts to response format. |
|
| 80 | + * @param $filler_count |
|
| 81 | + * @param $post_ids_to_be_excluded |
|
| 82 | + * |
|
| 83 | + * @return array |
|
| 84 | + */ |
|
| 85 | + public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 86 | + $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 87 | + // Add thumbnail and permalink to filler posts |
|
| 88 | + $filler_response = array(); |
|
| 89 | + foreach ( $filler_posts as $post_obj ) { |
|
| 90 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 91 | + $filler_response[] = array( |
|
| 92 | + 'post' => array( |
|
| 93 | + 'id' => $post_obj->ID, |
|
| 94 | + 'permalink' => get_permalink( $post_obj->ID ), |
|
| 95 | + 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 96 | + 'title' => get_the_title( $post_obj->ID ), |
|
| 97 | + 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 98 | + ), |
|
| 99 | + 'entity' => array( |
|
| 100 | + 'id' => 0 |
|
| 101 | + ) |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + return $filler_response; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | 108 | |
| 109 | 109 | } |
@@ -14,18 +14,18 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | private $sources = array(); |
| 16 | 16 | |
| 17 | - public function __construct( $post_id ) { |
|
| 17 | + public function __construct($post_id) { |
|
| 18 | 18 | |
| 19 | - $post_type = get_post_type( $post_id ); |
|
| 19 | + $post_type = get_post_type($post_id); |
|
| 20 | 20 | |
| 21 | - if ( $post_type === 'post' ) { |
|
| 21 | + if ($post_type === 'post') { |
|
| 22 | 22 | $this->sources = array( |
| 23 | - new Same_Category_Filler_Posts( $post_id ), |
|
| 24 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 23 | + new Same_Category_Filler_Posts($post_id), |
|
| 24 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 25 | 25 | ); |
| 26 | 26 | } else { |
| 27 | 27 | $this->sources = array( |
| 28 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 28 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 29 | 29 | ); |
| 30 | 30 | } |
| 31 | 31 | } |
@@ -36,22 +36,22 @@ discard block |
||
| 36 | 36 | * |
| 37 | 37 | * @return array<int> |
| 38 | 38 | */ |
| 39 | - private function extract_post_ids( $posts ) { |
|
| 40 | - return array_map( function ( $post ) { |
|
| 39 | + private function extract_post_ids($posts) { |
|
| 40 | + return array_map(function($post) { |
|
| 41 | 41 | /** |
| 42 | 42 | * @var $post \WP_Post |
| 43 | 43 | */ |
| 44 | 44 | return $post->ID; |
| 45 | - }, $posts ); |
|
| 45 | + }, $posts); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 48 | + public function get_filler_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 49 | 49 | |
| 50 | 50 | $filler_posts = array(); |
| 51 | 51 | |
| 52 | - foreach ( $this->sources as $source ) { |
|
| 52 | + foreach ($this->sources as $source) { |
|
| 53 | 53 | |
| 54 | - if ( $filler_count <= 0 ) { |
|
| 54 | + if ($filler_count <= 0) { |
|
| 55 | 55 | break; |
| 56 | 56 | } |
| 57 | 57 | /** |
@@ -60,13 +60,13 @@ discard block |
||
| 60 | 60 | $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
| 61 | 61 | $source->filler_count = $filler_count; |
| 62 | 62 | |
| 63 | - $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 64 | - $post_ids = $this->extract_post_ids( $posts ); |
|
| 63 | + $posts = $source->get_posts($filler_count, $post_ids_to_be_excluded); |
|
| 64 | + $post_ids = $this->extract_post_ids($posts); |
|
| 65 | 65 | |
| 66 | 66 | // Update the post ids, filler posts and filler count |
| 67 | - $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 68 | - $filler_count = $filler_count - count( $posts ); |
|
| 69 | - $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 67 | + $post_ids_to_be_excluded = array_merge($post_ids_to_be_excluded, $post_ids); |
|
| 68 | + $filler_count = $filler_count - count($posts); |
|
| 69 | + $filler_posts = array_merge($filler_posts, $posts); |
|
| 70 | 70 | |
| 71 | 71 | |
| 72 | 72 | } |
@@ -82,19 +82,19 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return array |
| 84 | 84 | */ |
| 85 | - public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 86 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 85 | + public function get_filler_response($filler_count, $post_ids_to_be_excluded) { |
|
| 86 | + $filler_posts = $this->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 87 | 87 | // Add thumbnail and permalink to filler posts |
| 88 | 88 | $filler_response = array(); |
| 89 | - foreach ( $filler_posts as $post_obj ) { |
|
| 90 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 89 | + foreach ($filler_posts as $post_obj) { |
|
| 90 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 91 | 91 | $filler_response[] = array( |
| 92 | 92 | 'post' => array( |
| 93 | 93 | 'id' => $post_obj->ID, |
| 94 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 95 | - 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 96 | - 'title' => get_the_title( $post_obj->ID ), |
|
| 97 | - 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 94 | + 'permalink' => get_permalink($post_obj->ID), |
|
| 95 | + 'thumbnail' => ($thumbnail) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 96 | + 'title' => get_the_title($post_obj->ID), |
|
| 97 | + 'srcset' => Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET) |
|
| 98 | 98 | ), |
| 99 | 99 | 'entity' => array( |
| 100 | 100 | 'id' => 0 |
@@ -20,30 +20,30 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_shortcode_faceted_search( $request ) { |
| 22 | 22 | |
| 23 | - // Create the cache key. |
|
| 24 | - $cache_key = array( |
|
| 25 | - 'request_params' => $_GET, |
|
| 26 | - ); |
|
| 23 | + // Create the cache key. |
|
| 24 | + $cache_key = array( |
|
| 25 | + 'request_params' => $_GET, |
|
| 26 | + ); |
|
| 27 | 27 | |
| 28 | - // Create the TTL cache and try to get the results. |
|
| 29 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + // Create the TTL cache and try to get the results. |
|
| 29 | + $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | + $cache_results = $cache->get( $cache_key ); |
|
| 31 | 31 | |
| 32 | - if ( isset( $cache_results ) ) { |
|
| 33 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 34 | - wl_core_send_json( $cache_results ); |
|
| 32 | + if ( isset( $cache_results ) ) { |
|
| 33 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 34 | + wl_core_send_json( $cache_results ); |
|
| 35 | 35 | |
| 36 | - return; |
|
| 37 | - } |
|
| 36 | + return; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 39 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 40 | 40 | |
| 41 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 41 | + $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 42 | 42 | |
| 43 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 44 | - $cache->put( $cache_key, $results ); |
|
| 43 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 44 | + $cache->put( $cache_key, $results ); |
|
| 45 | 45 | |
| 46 | - wl_core_send_json( $results ); |
|
| 46 | + wl_core_send_json( $results ); |
|
| 47 | 47 | |
| 48 | 48 | } |
| 49 | 49 | |
@@ -54,151 +54,151 @@ discard block |
||
| 54 | 54 | * @since 3.26.0 |
| 55 | 55 | */ |
| 56 | 56 | function wl_shortcode_faceted_search_origin( $request ) { |
| 57 | - // Post ID must be defined. |
|
| 58 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 59 | - wp_die( 'No post_id given' ); |
|
| 60 | - |
|
| 61 | - return; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 65 | - $current_post = get_post( $current_post_id ); |
|
| 66 | - $faceted_id = $_GET['uniqid']; |
|
| 67 | - |
|
| 68 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 69 | - $post_types = explode( ',', $post_types ); |
|
| 70 | - $existing_post_types = get_post_types(); |
|
| 71 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - // Post ID has to match an existing item. |
|
| 75 | - if ( null === $current_post ) { |
|
| 76 | - wp_die( 'No valid post_id given' ); |
|
| 77 | - |
|
| 78 | - return; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - // If the current post is an entity, |
|
| 82 | - // the current post is used as main entity. |
|
| 83 | - // Otherwise, current post related entities are used. |
|
| 84 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 85 | - |
|
| 86 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 87 | - array( $current_post->ID ) : |
|
| 88 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 89 | - |
|
| 90 | - // If there are no entities we cannot render the widget. |
|
| 91 | - if ( 0 === count( $entity_ids ) ) { |
|
| 92 | - /** |
|
| 93 | - * If this function is not called from ajax |
|
| 94 | - * then this should not throw an error. |
|
| 95 | - * Note: Used in scripbox longtail project on json endpoint. |
|
| 96 | - */ |
|
| 97 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 98 | - wp_die( 'No entities available' ); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 104 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 109 | - * The ordering should be descending by date on default. |
|
| 110 | - */ |
|
| 111 | - $order_by = 'DESC'; |
|
| 112 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 113 | - $order_by = (string) $_GET['sort']; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 118 | - $entity_ids, |
|
| 119 | - '*', |
|
| 120 | - null, |
|
| 121 | - 'publish', |
|
| 122 | - array( $current_post_id ), |
|
| 123 | - $limit, |
|
| 124 | - null, |
|
| 125 | - $order_by, |
|
| 126 | - $post_types |
|
| 127 | - ); |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 131 | - return $p->ID; |
|
| 132 | - }, $referencing_posts ); |
|
| 133 | - |
|
| 134 | - $post_results = array(); |
|
| 135 | - $entity_results = array(); |
|
| 136 | - |
|
| 137 | - // Populate $post_results |
|
| 138 | - |
|
| 139 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 140 | - $referencing_posts : |
|
| 141 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 142 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 143 | - '*', |
|
| 144 | - null, |
|
| 145 | - null, |
|
| 146 | - array(), |
|
| 147 | - null, |
|
| 148 | - $referencing_post_ids |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - if ( $filtered_posts ) { |
|
| 152 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Use the thumbnail. |
|
| 156 | - * |
|
| 157 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 158 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 159 | - * |
|
| 160 | - * @since 3.19.3 We're using the medium size image. |
|
| 161 | - */ |
|
| 162 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 163 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 164 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 165 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 166 | - $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 167 | - |
|
| 168 | - $result = $post_obj; |
|
| 169 | - $post_results[] = $result; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Add filler posts if needed |
|
| 174 | - |
|
| 175 | - $filler_count = $limit - count( $post_results ); |
|
| 176 | - if ( $filler_count > 0 ) { |
|
| 177 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 178 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 179 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 180 | - |
|
| 181 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 182 | - } |
|
| 183 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 184 | - return $post->ID; |
|
| 185 | - }, $post_results ); |
|
| 186 | - |
|
| 187 | - // Populate $entity_results |
|
| 188 | - |
|
| 189 | - global $wpdb; |
|
| 190 | - |
|
| 191 | - // Retrieve Wordlift relation instances table name. |
|
| 192 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 193 | - |
|
| 194 | - /* |
|
| 57 | + // Post ID must be defined. |
|
| 58 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 59 | + wp_die( 'No post_id given' ); |
|
| 60 | + |
|
| 61 | + return; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 65 | + $current_post = get_post( $current_post_id ); |
|
| 66 | + $faceted_id = $_GET['uniqid']; |
|
| 67 | + |
|
| 68 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 69 | + $post_types = explode( ',', $post_types ); |
|
| 70 | + $existing_post_types = get_post_types(); |
|
| 71 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + // Post ID has to match an existing item. |
|
| 75 | + if ( null === $current_post ) { |
|
| 76 | + wp_die( 'No valid post_id given' ); |
|
| 77 | + |
|
| 78 | + return; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + // If the current post is an entity, |
|
| 82 | + // the current post is used as main entity. |
|
| 83 | + // Otherwise, current post related entities are used. |
|
| 84 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 85 | + |
|
| 86 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 87 | + array( $current_post->ID ) : |
|
| 88 | + $entity_service->get_related_entities( $current_post->ID ); |
|
| 89 | + |
|
| 90 | + // If there are no entities we cannot render the widget. |
|
| 91 | + if ( 0 === count( $entity_ids ) ) { |
|
| 92 | + /** |
|
| 93 | + * If this function is not called from ajax |
|
| 94 | + * then this should not throw an error. |
|
| 95 | + * Note: Used in scripbox longtail project on json endpoint. |
|
| 96 | + */ |
|
| 97 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 98 | + wp_die( 'No entities available' ); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 104 | + $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 109 | + * The ordering should be descending by date on default. |
|
| 110 | + */ |
|
| 111 | + $order_by = 'DESC'; |
|
| 112 | + if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 113 | + $order_by = (string) $_GET['sort']; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 118 | + $entity_ids, |
|
| 119 | + '*', |
|
| 120 | + null, |
|
| 121 | + 'publish', |
|
| 122 | + array( $current_post_id ), |
|
| 123 | + $limit, |
|
| 124 | + null, |
|
| 125 | + $order_by, |
|
| 126 | + $post_types |
|
| 127 | + ); |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 131 | + return $p->ID; |
|
| 132 | + }, $referencing_posts ); |
|
| 133 | + |
|
| 134 | + $post_results = array(); |
|
| 135 | + $entity_results = array(); |
|
| 136 | + |
|
| 137 | + // Populate $post_results |
|
| 138 | + |
|
| 139 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 140 | + $referencing_posts : |
|
| 141 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 142 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 143 | + '*', |
|
| 144 | + null, |
|
| 145 | + null, |
|
| 146 | + array(), |
|
| 147 | + null, |
|
| 148 | + $referencing_post_ids |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + if ( $filtered_posts ) { |
|
| 152 | + foreach ( $filtered_posts as $post_obj ) { |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Use the thumbnail. |
|
| 156 | + * |
|
| 157 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 158 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 159 | + * |
|
| 160 | + * @since 3.19.3 We're using the medium size image. |
|
| 161 | + */ |
|
| 162 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 163 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 164 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 165 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 166 | + $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 167 | + |
|
| 168 | + $result = $post_obj; |
|
| 169 | + $post_results[] = $result; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // Add filler posts if needed |
|
| 174 | + |
|
| 175 | + $filler_count = $limit - count( $post_results ); |
|
| 176 | + if ( $filler_count > 0 ) { |
|
| 177 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 178 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 179 | + $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 180 | + |
|
| 181 | + $post_results = array_merge( $post_results, $filler_posts ); |
|
| 182 | + } |
|
| 183 | + $referencing_post_ids = array_map( function ( $post ) { |
|
| 184 | + return $post->ID; |
|
| 185 | + }, $post_results ); |
|
| 186 | + |
|
| 187 | + // Populate $entity_results |
|
| 188 | + |
|
| 189 | + global $wpdb; |
|
| 190 | + |
|
| 191 | + // Retrieve Wordlift relation instances table name. |
|
| 192 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 193 | + |
|
| 194 | + /* |
|
| 195 | 195 | * Make sure we have some referenced post, otherwise the IN parts of |
| 196 | 196 | * the SQL will produce an SQL error. |
| 197 | 197 | */ |
| 198 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 199 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 198 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 199 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 200 | 200 | |
| 201 | - $query = " |
|
| 201 | + $query = " |
|
| 202 | 202 | SELECT |
| 203 | 203 | object_id AS ID, |
| 204 | 204 | count( object_id ) AS counter |
@@ -210,67 +210,67 @@ discard block |
||
| 210 | 210 | LIMIT $limit; |
| 211 | 211 | "; |
| 212 | 212 | |
| 213 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 213 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 214 | 214 | |
| 215 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 215 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 216 | 216 | |
| 217 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 217 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 218 | 218 | |
| 219 | - foreach ( $entities as $obj ) { |
|
| 219 | + foreach ( $entities as $obj ) { |
|
| 220 | 220 | |
| 221 | - $entity = get_post( $obj->ID ); |
|
| 221 | + $entity = get_post( $obj->ID ); |
|
| 222 | 222 | |
| 223 | - // Ensure only valid and published entities are returned. |
|
| 224 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 223 | + // Ensure only valid and published entities are returned. |
|
| 224 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 225 | 225 | |
| 226 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 227 | - $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 228 | - $serialized_entity['counter'] = $obj->counter; |
|
| 229 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 230 | - $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 231 | - $obj->ID, |
|
| 232 | - 'ids', |
|
| 233 | - null, |
|
| 234 | - null, |
|
| 235 | - array(), |
|
| 236 | - null, |
|
| 237 | - $referencing_post_ids |
|
| 238 | - ); |
|
| 239 | - $entity_results[] = $serialized_entity; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - } |
|
| 226 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 227 | + $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 228 | + $serialized_entity['counter'] = $obj->counter; |
|
| 229 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 230 | + $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 231 | + $obj->ID, |
|
| 232 | + 'ids', |
|
| 233 | + null, |
|
| 234 | + null, |
|
| 235 | + array(), |
|
| 236 | + null, |
|
| 237 | + $referencing_post_ids |
|
| 238 | + ); |
|
| 239 | + $entity_results[] = $serialized_entity; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 244 | + $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 245 | 245 | |
| 246 | - // Add srcset attribute. |
|
| 247 | - $post_results = array_map( function ( $post ) { |
|
| 248 | - $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 249 | - return $post; |
|
| 250 | - }, $post_results ); |
|
| 246 | + // Add srcset attribute. |
|
| 247 | + $post_results = array_map( function ( $post ) { |
|
| 248 | + $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 249 | + return $post; |
|
| 250 | + }, $post_results ); |
|
| 251 | 251 | |
| 252 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 252 | + $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 253 | 253 | |
| 254 | - return array( |
|
| 255 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 256 | - 'entities' => $entity_results |
|
| 257 | - ); |
|
| 254 | + return array( |
|
| 255 | + 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 256 | + 'entities' => $entity_results |
|
| 257 | + ); |
|
| 258 | 258 | |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
| 262 | 262 | |
| 263 | - $title = get_the_title( $post_id ); |
|
| 263 | + $title = get_the_title( $post_id ); |
|
| 264 | 264 | |
| 265 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 266 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 265 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 266 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 267 | 267 | |
| 268 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 269 | - $title = $alternative_labels[0]; |
|
| 270 | - } |
|
| 271 | - } |
|
| 268 | + if ( count( $alternative_labels ) > 0 ) { |
|
| 269 | + $title = $alternative_labels[0]; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - return remove_accents( $title ); |
|
| 273 | + return remove_accents( $title ); |
|
| 274 | 274 | |
| 275 | 275 | } |
| 276 | 276 | |
@@ -278,9 +278,9 @@ discard block |
||
| 278 | 278 | * Adding `rest_api_init` action for network faceted-search |
| 279 | 279 | */ |
| 280 | 280 | add_action( 'rest_api_init', function () { |
| 281 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 282 | - 'methods' => 'GET', |
|
| 283 | - 'callback' => 'wl_shortcode_faceted_search', |
|
| 284 | - 'permission_callback' => '__return_true', |
|
| 285 | - ) ); |
|
| 281 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 282 | + 'methods' => 'GET', |
|
| 283 | + 'callback' => 'wl_shortcode_faceted_search', |
|
| 284 | + 'permission_callback' => '__return_true', |
|
| 285 | + ) ); |
|
| 286 | 286 | } ); |
@@ -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 | // Create the cache key. |
| 24 | 24 | $cache_key = array( |
@@ -26,24 +26,24 @@ discard block |
||
| 26 | 26 | ); |
| 27 | 27 | |
| 28 | 28 | // Create the TTL cache and try to get the results. |
| 29 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 29 | + $cache = new Ttl_Cache("faceted-search", 8 * 60 * 60); // 8 hours. |
|
| 30 | + $cache_results = $cache->get($cache_key); |
|
| 31 | 31 | |
| 32 | - if ( isset( $cache_results ) ) { |
|
| 33 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 34 | - wl_core_send_json( $cache_results ); |
|
| 32 | + if (isset($cache_results)) { |
|
| 33 | + header('X-WordLift-Cache: HIT'); |
|
| 34 | + wl_core_send_json($cache_results); |
|
| 35 | 35 | |
| 36 | 36 | return; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 39 | + header('X-WordLift-Cache: MISS'); |
|
| 40 | 40 | |
| 41 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 41 | + $results = wl_shortcode_faceted_search_origin($request); |
|
| 42 | 42 | |
| 43 | 43 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 44 | - $cache->put( $cache_key, $results ); |
|
| 44 | + $cache->put($cache_key, $results); |
|
| 45 | 45 | |
| 46 | - wl_core_send_json( $results ); |
|
| 46 | + wl_core_send_json($results); |
|
| 47 | 47 | |
| 48 | 48 | } |
| 49 | 49 | |
@@ -53,27 +53,27 @@ discard block |
||
| 53 | 53 | * @return array $results |
| 54 | 54 | * @since 3.26.0 |
| 55 | 55 | */ |
| 56 | -function wl_shortcode_faceted_search_origin( $request ) { |
|
| 56 | +function wl_shortcode_faceted_search_origin($request) { |
|
| 57 | 57 | // Post ID must be defined. |
| 58 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 59 | - wp_die( 'No post_id given' ); |
|
| 58 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 59 | + wp_die('No post_id given'); |
|
| 60 | 60 | |
| 61 | 61 | return; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 65 | - $current_post = get_post( $current_post_id ); |
|
| 65 | + $current_post = get_post($current_post_id); |
|
| 66 | 66 | $faceted_id = $_GET['uniqid']; |
| 67 | 67 | |
| 68 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 69 | - $post_types = explode( ',', $post_types ); |
|
| 68 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 69 | + $post_types = explode(',', $post_types); |
|
| 70 | 70 | $existing_post_types = get_post_types(); |
| 71 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 71 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 72 | 72 | |
| 73 | 73 | |
| 74 | 74 | // Post ID has to match an existing item. |
| 75 | - if ( null === $current_post ) { |
|
| 76 | - wp_die( 'No valid post_id given' ); |
|
| 75 | + if (null === $current_post) { |
|
| 76 | + wp_die('No valid post_id given'); |
|
| 77 | 77 | |
| 78 | 78 | return; |
| 79 | 79 | } |
@@ -83,25 +83,24 @@ discard block |
||
| 83 | 83 | // Otherwise, current post related entities are used. |
| 84 | 84 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 85 | 85 | |
| 86 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 87 | - array( $current_post->ID ) : |
|
| 88 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 86 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 87 | + array($current_post->ID) : $entity_service->get_related_entities($current_post->ID); |
|
| 89 | 88 | |
| 90 | 89 | // If there are no entities we cannot render the widget. |
| 91 | - if ( 0 === count( $entity_ids ) ) { |
|
| 90 | + if (0 === count($entity_ids)) { |
|
| 92 | 91 | /** |
| 93 | 92 | * If this function is not called from ajax |
| 94 | 93 | * then this should not throw an error. |
| 95 | 94 | * Note: Used in scripbox longtail project on json endpoint. |
| 96 | 95 | */ |
| 97 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 98 | - wp_die( 'No entities available' ); |
|
| 96 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 97 | + wp_die('No entities available'); |
|
| 99 | 98 | } |
| 100 | 99 | |
| 101 | 100 | } |
| 102 | 101 | |
| 103 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 104 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 102 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 103 | + $amp = (isset($_GET['amp'])) ? true : false; |
|
| 105 | 104 | |
| 106 | 105 | |
| 107 | 106 | /** |
@@ -109,7 +108,7 @@ discard block |
||
| 109 | 108 | * The ordering should be descending by date on default. |
| 110 | 109 | */ |
| 111 | 110 | $order_by = 'DESC'; |
| 112 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 111 | + if (isset($_GET['sort']) && is_string($_GET['sort'])) { |
|
| 113 | 112 | $order_by = (string) $_GET['sort']; |
| 114 | 113 | } |
| 115 | 114 | |
@@ -119,7 +118,7 @@ discard block |
||
| 119 | 118 | '*', |
| 120 | 119 | null, |
| 121 | 120 | 'publish', |
| 122 | - array( $current_post_id ), |
|
| 121 | + array($current_post_id), |
|
| 123 | 122 | $limit, |
| 124 | 123 | null, |
| 125 | 124 | $order_by, |
@@ -127,19 +126,18 @@ discard block |
||
| 127 | 126 | ); |
| 128 | 127 | |
| 129 | 128 | |
| 130 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 129 | + $referencing_post_ids = array_map(function($p) { |
|
| 131 | 130 | return $p->ID; |
| 132 | - }, $referencing_posts ); |
|
| 131 | + }, $referencing_posts); |
|
| 133 | 132 | |
| 134 | 133 | $post_results = array(); |
| 135 | 134 | $entity_results = array(); |
| 136 | 135 | |
| 137 | 136 | // Populate $post_results |
| 138 | 137 | |
| 139 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 140 | - $referencing_posts : |
|
| 141 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 142 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 138 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 139 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 140 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 143 | 141 | '*', |
| 144 | 142 | null, |
| 145 | 143 | null, |
@@ -148,8 +146,8 @@ discard block |
||
| 148 | 146 | $referencing_post_ids |
| 149 | 147 | ); |
| 150 | 148 | |
| 151 | - if ( $filtered_posts ) { |
|
| 152 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 149 | + if ($filtered_posts) { |
|
| 150 | + foreach ($filtered_posts as $post_obj) { |
|
| 153 | 151 | |
| 154 | 152 | /** |
| 155 | 153 | * Use the thumbnail. |
@@ -159,11 +157,11 @@ discard block |
||
| 159 | 157 | * |
| 160 | 158 | * @since 3.19.3 We're using the medium size image. |
| 161 | 159 | */ |
| 162 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 163 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 160 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 161 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 164 | 162 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 165 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 166 | - $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 163 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
| 164 | + $post_obj->srcset = Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
| 167 | 165 | |
| 168 | 166 | $result = $post_obj; |
| 169 | 167 | $post_results[] = $result; |
@@ -172,17 +170,17 @@ discard block |
||
| 172 | 170 | |
| 173 | 171 | // Add filler posts if needed |
| 174 | 172 | |
| 175 | - $filler_count = $limit - count( $post_results ); |
|
| 176 | - if ( $filler_count > 0 ) { |
|
| 177 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 178 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 179 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 173 | + $filler_count = $limit - count($post_results); |
|
| 174 | + if ($filler_count > 0) { |
|
| 175 | + $filler_posts_util = new Filler_Posts_Util($current_post_id); |
|
| 176 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 177 | + $filler_posts = $filler_posts_util->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 180 | 178 | |
| 181 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 179 | + $post_results = array_merge($post_results, $filler_posts); |
|
| 182 | 180 | } |
| 183 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 181 | + $referencing_post_ids = array_map(function($post) { |
|
| 184 | 182 | return $post->ID; |
| 185 | - }, $post_results ); |
|
| 183 | + }, $post_results); |
|
| 186 | 184 | |
| 187 | 185 | // Populate $entity_results |
| 188 | 186 | |
@@ -195,8 +193,8 @@ discard block |
||
| 195 | 193 | * Make sure we have some referenced post, otherwise the IN parts of |
| 196 | 194 | * the SQL will produce an SQL error. |
| 197 | 195 | */ |
| 198 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 199 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 196 | + if ( ! empty($referencing_post_ids)) { |
|
| 197 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 200 | 198 | |
| 201 | 199 | $query = " |
| 202 | 200 | SELECT |
@@ -210,21 +208,21 @@ discard block |
||
| 210 | 208 | LIMIT $limit; |
| 211 | 209 | "; |
| 212 | 210 | |
| 213 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 211 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 214 | 212 | |
| 215 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 213 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 216 | 214 | |
| 217 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 215 | + wl_write_log('Entities found '.count($entities)); |
|
| 218 | 216 | |
| 219 | - foreach ( $entities as $obj ) { |
|
| 217 | + foreach ($entities as $obj) { |
|
| 220 | 218 | |
| 221 | - $entity = get_post( $obj->ID ); |
|
| 219 | + $entity = get_post($obj->ID); |
|
| 222 | 220 | |
| 223 | 221 | // Ensure only valid and published entities are returned. |
| 224 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 222 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 225 | 223 | |
| 226 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 227 | - $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 224 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 225 | + $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title($obj->ID); |
|
| 228 | 226 | $serialized_entity['counter'] = $obj->counter; |
| 229 | 227 | $serialized_entity['createdAt'] = $entity->post_date; |
| 230 | 228 | $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -236,51 +234,51 @@ discard block |
||
| 236 | 234 | null, |
| 237 | 235 | $referencing_post_ids |
| 238 | 236 | ); |
| 239 | - $entity_results[] = $serialized_entity; |
|
| 237 | + $entity_results[] = $serialized_entity; |
|
| 240 | 238 | } |
| 241 | 239 | } |
| 242 | 240 | } |
| 243 | 241 | |
| 244 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 242 | + $post_results = apply_filters('wl_faceted_data_posts', $post_results, $faceted_id); |
|
| 245 | 243 | |
| 246 | 244 | // Add srcset attribute. |
| 247 | - $post_results = array_map( function ( $post ) { |
|
| 248 | - $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 245 | + $post_results = array_map(function($post) { |
|
| 246 | + $post->srcset = Srcset_Util::get_srcset($post->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
| 249 | 247 | return $post; |
| 250 | - }, $post_results ); |
|
| 248 | + }, $post_results); |
|
| 251 | 249 | |
| 252 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 250 | + $entity_results = apply_filters('wl_faceted_data_entities', $entity_results, $faceted_id); |
|
| 253 | 251 | |
| 254 | 252 | return array( |
| 255 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 253 | + 'posts' => $amp ? array(array('values' => $post_results)) : $post_results, |
|
| 256 | 254 | 'entities' => $entity_results |
| 257 | 255 | ); |
| 258 | 256 | |
| 259 | 257 | } |
| 260 | 258 | |
| 261 | -function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
|
| 259 | +function wl_shortcode_faceted_search_get_the_title($post_id) { |
|
| 262 | 260 | |
| 263 | - $title = get_the_title( $post_id ); |
|
| 261 | + $title = get_the_title($post_id); |
|
| 264 | 262 | |
| 265 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 266 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 263 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
| 264 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
| 267 | 265 | |
| 268 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 266 | + if (count($alternative_labels) > 0) { |
|
| 269 | 267 | $title = $alternative_labels[0]; |
| 270 | 268 | } |
| 271 | 269 | } |
| 272 | 270 | |
| 273 | - return remove_accents( $title ); |
|
| 271 | + return remove_accents($title); |
|
| 274 | 272 | |
| 275 | 273 | } |
| 276 | 274 | |
| 277 | 275 | /** |
| 278 | 276 | * Adding `rest_api_init` action for network faceted-search |
| 279 | 277 | */ |
| 280 | -add_action( 'rest_api_init', function () { |
|
| 281 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 278 | +add_action('rest_api_init', function() { |
|
| 279 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 282 | 280 | 'methods' => 'GET', |
| 283 | 281 | 'callback' => 'wl_shortcode_faceted_search', |
| 284 | 282 | 'permission_callback' => '__return_true', |
| 285 | - ) ); |
|
| 283 | + )); |
|
| 286 | 284 | } ); |
@@ -7,56 +7,56 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Srcset_Util { |
| 9 | 9 | |
| 10 | - const NAVIGATOR_WIDGET = 'navigator'; |
|
| 11 | - |
|
| 12 | - const FACETED_SEARCH_WIDGET = 'faceted_search'; |
|
| 13 | - |
|
| 14 | - public static function get_srcset( $post_id, $widget_name ) { |
|
| 15 | - |
|
| 16 | - $srcset = array(); |
|
| 17 | - $small = get_the_post_thumbnail_url( $post_id, 'small' ); |
|
| 18 | - $medium = get_the_post_thumbnail_url( $post_id, 'medium' ); |
|
| 19 | - $large = get_the_post_thumbnail_url( $post_id, 'large' ); |
|
| 20 | - if ( $small && $width = self::get_image_width( $post_id, 'small' ) ) { |
|
| 21 | - $srcset[] = $small . ' ' . $width . 'w'; |
|
| 22 | - } |
|
| 23 | - if ( $medium && $width = self::get_image_width( $post_id, 'medium' ) ) { |
|
| 24 | - $srcset[] = $medium . ' ' . $width . 'w'; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - if ( $large && $width = self::get_image_width( $post_id, 'large' ) ) { |
|
| 28 | - $srcset[] = $large . ' ' . $width . 'w'; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - $srcset_string = join( ",", $srcset ); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Filter name: wordlift_${widget_name}_thumbnail_srcset |
|
| 35 | - * Filters the srcset string supplied to widgets for each post. |
|
| 36 | - * |
|
| 37 | - * @param $srcset_string string The srcset string |
|
| 38 | - * |
|
| 39 | - * @since 3.28.0 |
|
| 40 | - */ |
|
| 41 | - $srcset_string = apply_filters( "wordlift_${widget_name}_thumbnail_srcset", $srcset_string ); |
|
| 42 | - |
|
| 43 | - return $srcset_string; |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - private static function get_image_width( $post_id, $size ) { |
|
| 49 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 50 | - if ( ! $thumbnail_id ) { |
|
| 51 | - return false; |
|
| 52 | - } |
|
| 53 | - $data = wp_get_attachment_image_src( $thumbnail_id, $size ); |
|
| 54 | - if ( ! $data ) { |
|
| 55 | - return false; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - return array_key_exists( 2, $data ) ? $data[2] : false; |
|
| 59 | - } |
|
| 10 | + const NAVIGATOR_WIDGET = 'navigator'; |
|
| 11 | + |
|
| 12 | + const FACETED_SEARCH_WIDGET = 'faceted_search'; |
|
| 13 | + |
|
| 14 | + public static function get_srcset( $post_id, $widget_name ) { |
|
| 15 | + |
|
| 16 | + $srcset = array(); |
|
| 17 | + $small = get_the_post_thumbnail_url( $post_id, 'small' ); |
|
| 18 | + $medium = get_the_post_thumbnail_url( $post_id, 'medium' ); |
|
| 19 | + $large = get_the_post_thumbnail_url( $post_id, 'large' ); |
|
| 20 | + if ( $small && $width = self::get_image_width( $post_id, 'small' ) ) { |
|
| 21 | + $srcset[] = $small . ' ' . $width . 'w'; |
|
| 22 | + } |
|
| 23 | + if ( $medium && $width = self::get_image_width( $post_id, 'medium' ) ) { |
|
| 24 | + $srcset[] = $medium . ' ' . $width . 'w'; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + if ( $large && $width = self::get_image_width( $post_id, 'large' ) ) { |
|
| 28 | + $srcset[] = $large . ' ' . $width . 'w'; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + $srcset_string = join( ",", $srcset ); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Filter name: wordlift_${widget_name}_thumbnail_srcset |
|
| 35 | + * Filters the srcset string supplied to widgets for each post. |
|
| 36 | + * |
|
| 37 | + * @param $srcset_string string The srcset string |
|
| 38 | + * |
|
| 39 | + * @since 3.28.0 |
|
| 40 | + */ |
|
| 41 | + $srcset_string = apply_filters( "wordlift_${widget_name}_thumbnail_srcset", $srcset_string ); |
|
| 42 | + |
|
| 43 | + return $srcset_string; |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + private static function get_image_width( $post_id, $size ) { |
|
| 49 | + $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 50 | + if ( ! $thumbnail_id ) { |
|
| 51 | + return false; |
|
| 52 | + } |
|
| 53 | + $data = wp_get_attachment_image_src( $thumbnail_id, $size ); |
|
| 54 | + if ( ! $data ) { |
|
| 55 | + return false; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + return array_key_exists( 2, $data ) ? $data[2] : false; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | |
| 62 | 62 | } |
| 63 | 63 | \ No newline at end of file |
@@ -11,24 +11,24 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | const FACETED_SEARCH_WIDGET = 'faceted_search'; |
| 13 | 13 | |
| 14 | - public static function get_srcset( $post_id, $widget_name ) { |
|
| 14 | + public static function get_srcset($post_id, $widget_name) { |
|
| 15 | 15 | |
| 16 | 16 | $srcset = array(); |
| 17 | - $small = get_the_post_thumbnail_url( $post_id, 'small' ); |
|
| 18 | - $medium = get_the_post_thumbnail_url( $post_id, 'medium' ); |
|
| 19 | - $large = get_the_post_thumbnail_url( $post_id, 'large' ); |
|
| 20 | - if ( $small && $width = self::get_image_width( $post_id, 'small' ) ) { |
|
| 21 | - $srcset[] = $small . ' ' . $width . 'w'; |
|
| 17 | + $small = get_the_post_thumbnail_url($post_id, 'small'); |
|
| 18 | + $medium = get_the_post_thumbnail_url($post_id, 'medium'); |
|
| 19 | + $large = get_the_post_thumbnail_url($post_id, 'large'); |
|
| 20 | + if ($small && $width = self::get_image_width($post_id, 'small')) { |
|
| 21 | + $srcset[] = $small.' '.$width.'w'; |
|
| 22 | 22 | } |
| 23 | - if ( $medium && $width = self::get_image_width( $post_id, 'medium' ) ) { |
|
| 24 | - $srcset[] = $medium . ' ' . $width . 'w'; |
|
| 23 | + if ($medium && $width = self::get_image_width($post_id, 'medium')) { |
|
| 24 | + $srcset[] = $medium.' '.$width.'w'; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - if ( $large && $width = self::get_image_width( $post_id, 'large' ) ) { |
|
| 28 | - $srcset[] = $large . ' ' . $width . 'w'; |
|
| 27 | + if ($large && $width = self::get_image_width($post_id, 'large')) { |
|
| 28 | + $srcset[] = $large.' '.$width.'w'; |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - $srcset_string = join( ",", $srcset ); |
|
| 31 | + $srcset_string = join(",", $srcset); |
|
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * Filter name: wordlift_${widget_name}_thumbnail_srcset |
@@ -38,24 +38,24 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @since 3.28.0 |
| 40 | 40 | */ |
| 41 | - $srcset_string = apply_filters( "wordlift_${widget_name}_thumbnail_srcset", $srcset_string ); |
|
| 41 | + $srcset_string = apply_filters("wordlift_${widget_name}_thumbnail_srcset", $srcset_string); |
|
| 42 | 42 | |
| 43 | 43 | return $srcset_string; |
| 44 | 44 | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | |
| 48 | - private static function get_image_width( $post_id, $size ) { |
|
| 49 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 50 | - if ( ! $thumbnail_id ) { |
|
| 48 | + private static function get_image_width($post_id, $size) { |
|
| 49 | + $thumbnail_id = get_post_thumbnail_id($post_id); |
|
| 50 | + if ( ! $thumbnail_id) { |
|
| 51 | 51 | return false; |
| 52 | 52 | } |
| 53 | - $data = wp_get_attachment_image_src( $thumbnail_id, $size ); |
|
| 54 | - if ( ! $data ) { |
|
| 53 | + $data = wp_get_attachment_image_src($thumbnail_id, $size); |
|
| 54 | + if ( ! $data) { |
|
| 55 | 55 | return false; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - return array_key_exists( 2, $data ) ? $data[2] : false; |
|
| 58 | + return array_key_exists(2, $data) ? $data[2] : false; |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | |