@@ -21,25 +21,25 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | // Create the cache key. |
| 23 | 23 | $cache_key_params = $_REQUEST; |
| 24 | - unset( $cache_key_params['uniqid'] ); |
|
| 25 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 24 | + unset($cache_key_params['uniqid']); |
|
| 25 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 26 | 26 | |
| 27 | 27 | // Create the TTL cache and try to get the results. |
| 28 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + $cache = new Ttl_Cache("navigator", 8 * 60 * 60); // 8 hours. |
|
| 29 | + $cache_results = $cache->get($cache_key); |
|
| 30 | 30 | |
| 31 | - if ( isset( $cache_results ) ) { |
|
| 32 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 31 | + if (isset($cache_results)) { |
|
| 32 | + header('X-WordLift-Cache: HIT'); |
|
| 33 | 33 | |
| 34 | 34 | return $cache_results; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 37 | + header('X-WordLift-Cache: MISS'); |
|
| 38 | 38 | |
| 39 | 39 | $results = _wl_navigator_get_data(); |
| 40 | 40 | |
| 41 | 41 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 42 | - $cache->put( $cache_key, $results ); |
|
| 42 | + $cache->put($cache_key, $results); |
|
| 43 | 43 | |
| 44 | 44 | return $results; |
| 45 | 45 | } |
@@ -54,29 +54,29 @@ discard block |
||
| 54 | 54 | * @since 3.22.6 |
| 55 | 55 | * |
| 56 | 56 | */ |
| 57 | -function wl_network_navigator_wp_json( $request ) { |
|
| 57 | +function wl_network_navigator_wp_json($request) { |
|
| 58 | 58 | |
| 59 | 59 | // Create the cache key. |
| 60 | 60 | $cache_key_params = $_REQUEST; |
| 61 | - unset( $cache_key_params['uniqid'] ); |
|
| 62 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 61 | + unset($cache_key_params['uniqid']); |
|
| 62 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 63 | 63 | |
| 64 | 64 | // Create the TTL cache and try to get the results. |
| 65 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 66 | - $cache_results = $cache->get( $cache_key ); |
|
| 65 | + $cache = new Ttl_Cache("network-navigator", 8 * 60 * 60); // 8 hours. |
|
| 66 | + $cache_results = $cache->get($cache_key); |
|
| 67 | 67 | |
| 68 | - if ( isset( $cache_results ) ) { |
|
| 69 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 68 | + if (isset($cache_results)) { |
|
| 69 | + header('X-WordLift-Cache: HIT'); |
|
| 70 | 70 | |
| 71 | 71 | return $cache_results; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 74 | + header('X-WordLift-Cache: MISS'); |
|
| 75 | 75 | |
| 76 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 76 | + $results = _wl_network_navigator_get_data($request); |
|
| 77 | 77 | |
| 78 | 78 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 79 | - $cache->put( $cache_key, $results ); |
|
| 79 | + $cache->put($cache_key, $results); |
|
| 80 | 80 | |
| 81 | 81 | return $results; |
| 82 | 82 | |
@@ -85,57 +85,57 @@ discard block |
||
| 85 | 85 | function _wl_navigator_get_data() { |
| 86 | 86 | |
| 87 | 87 | // Post ID must be defined |
| 88 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 89 | - wp_send_json_error( 'No post_id given' ); |
|
| 88 | + if ( ! isset($_GET['post_id'])) { |
|
| 89 | + wp_send_json_error('No post_id given'); |
|
| 90 | 90 | |
| 91 | 91 | return array(); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | // Post ID must be defined |
| 95 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 96 | - wp_send_json_error( 'No uniqid given' ); |
|
| 95 | + if ( ! isset($_GET['uniqid'])) { |
|
| 96 | + wp_send_json_error('No uniqid given'); |
|
| 97 | 97 | |
| 98 | 98 | return array(); |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Limit the results (defaults to 4) |
| 102 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 103 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 104 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 105 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 106 | - $post_types = explode( ',', $post_types ); |
|
| 102 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 103 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 104 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 105 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 106 | + $post_types = explode(',', $post_types); |
|
| 107 | 107 | $existing_post_types = get_post_types(); |
| 108 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 108 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 109 | 109 | $current_post_id = $_GET['post_id']; |
| 110 | - $current_post = get_post( $current_post_id ); |
|
| 110 | + $current_post = get_post($current_post_id); |
|
| 111 | 111 | |
| 112 | 112 | $navigator_id = $_GET['uniqid']; |
| 113 | 113 | |
| 114 | 114 | // Post ID has to match an existing item |
| 115 | - if ( null === $current_post ) { |
|
| 116 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 115 | + if (null === $current_post) { |
|
| 116 | + wp_send_json_error('No valid post_id given'); |
|
| 117 | 117 | |
| 118 | 118 | return array(); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | // Determine navigator type and call respective _get_results |
| 122 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 122 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 123 | 123 | |
| 124 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 124 | + $referencing_posts = Navigator_Data::entity_navigator_get_results($current_post_id, array( |
|
| 125 | 125 | 'ID', |
| 126 | 126 | 'post_title', |
| 127 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 127 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 128 | 128 | } else { |
| 129 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 129 | + $referencing_posts = Navigator_Data::post_navigator_get_results($current_post_id, array( |
|
| 130 | 130 | 'ID', |
| 131 | 131 | 'post_title', |
| 132 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 132 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | // loop over them and take the first one which is not already in the $related_posts |
| 136 | 136 | $results = array(); |
| 137 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 138 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 137 | + foreach ($referencing_posts as $referencing_post) { |
|
| 138 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | 141 | * Use the thumbnail. |
@@ -145,12 +145,12 @@ discard block |
||
| 145 | 145 | * |
| 146 | 146 | * @since 3.19.3 We're using the medium size image. |
| 147 | 147 | */ |
| 148 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 148 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 149 | 149 | |
| 150 | 150 | $result = array( |
| 151 | 151 | 'post' => array( |
| 152 | 152 | 'id' => $referencing_post->ID, |
| 153 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 153 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 154 | 154 | 'title' => $referencing_post->post_title, |
| 155 | 155 | 'thumbnail' => $thumbnail, |
| 156 | 156 | ), |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | 'id' => $referencing_post->entity_id, |
| 159 | 159 | 'label' => $serialized_entity['label'], |
| 160 | 160 | 'mainType' => $serialized_entity['mainType'], |
| 161 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 161 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 162 | 162 | ), |
| 163 | 163 | ); |
| 164 | 164 | |
@@ -166,59 +166,59 @@ discard block |
||
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | |
| 169 | - if ( count( $results ) < $navigator_length ) { |
|
| 170 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 169 | + if (count($results) < $navigator_length) { |
|
| 170 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Add filler posts if needed |
| 174 | - $filler_count = $navigator_length - count( $results ); |
|
| 175 | - if ( $filler_count > 0 ) { |
|
| 176 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 174 | + $filler_count = $navigator_length - count($results); |
|
| 175 | + if ($filler_count > 0) { |
|
| 176 | + $referencing_post_ids = array_map(function($p) { |
|
| 177 | 177 | return $p->ID; |
| 178 | - }, $referencing_posts ); |
|
| 178 | + }, $referencing_posts); |
|
| 179 | 179 | /** |
| 180 | 180 | * @since 3.27.8 |
| 181 | 181 | * Filler posts are fetched using this util. |
| 182 | 182 | */ |
| 183 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 184 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 185 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 186 | - $results = array_merge( $results, $filler_posts ); |
|
| 183 | + $filler_posts_util = new Filler_Posts_Util($current_post_id); |
|
| 184 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 185 | + $filler_posts = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded); |
|
| 186 | + $results = array_merge($results, $filler_posts); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | // Apply filters after fillers are added |
| 190 | - foreach ( $results as $result_index => $result ) { |
|
| 191 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 192 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 190 | + foreach ($results as $result_index => $result) { |
|
| 191 | + $results[$result_index]['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id); |
|
| 192 | + $results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | return $results; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | -function _wl_network_navigator_get_data( $request ) { |
|
| 198 | +function _wl_network_navigator_get_data($request) { |
|
| 199 | 199 | |
| 200 | 200 | // Limit the results (defaults to 4) |
| 201 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 202 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 201 | + $navigator_length = isset($request['limit']) ? intval($request['limit']) : 4; |
|
| 202 | + $navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0; |
|
| 203 | 203 | $navigator_id = $request['uniqid']; |
| 204 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 204 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 205 | 205 | |
| 206 | 206 | $entities = $request['entities']; |
| 207 | 207 | |
| 208 | 208 | // Post ID has to match an existing item |
| 209 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 210 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 209 | + if ( ! isset($entities) || empty($entities)) { |
|
| 210 | + wp_send_json_error('No valid entities provided'); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 213 | + $referencing_posts = _wl_network_navigator_get_results($entities, array( |
|
| 214 | 214 | 'ID', |
| 215 | 215 | 'post_title', |
| 216 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 216 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 217 | 217 | |
| 218 | 218 | // loop over them and take the first one which is not already in the $related_posts |
| 219 | 219 | $results = array(); |
| 220 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 221 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 220 | + foreach ($referencing_posts as $referencing_post) { |
|
| 221 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 222 | 222 | |
| 223 | 223 | /** |
| 224 | 224 | * Use the thumbnail. |
@@ -228,30 +228,30 @@ discard block |
||
| 228 | 228 | * |
| 229 | 229 | * @since 3.19.3 We're using the medium size image. |
| 230 | 230 | */ |
| 231 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 231 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 232 | 232 | |
| 233 | 233 | $result = array( |
| 234 | 234 | 'post' => array( |
| 235 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 235 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 236 | 236 | 'title' => $referencing_post->post_title, |
| 237 | 237 | 'thumbnail' => $thumbnail, |
| 238 | 238 | ), |
| 239 | 239 | 'entity' => array( |
| 240 | 240 | 'label' => $serialized_entity['label'], |
| 241 | 241 | 'mainType' => $serialized_entity['mainType'], |
| 242 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 242 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 243 | 243 | ), |
| 244 | 244 | ); |
| 245 | 245 | |
| 246 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 247 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 246 | + $result['post'] = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 247 | + $result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 248 | 248 | |
| 249 | 249 | $results[] = $result; |
| 250 | 250 | |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - if ( count( $results ) < $navigator_length ) { |
|
| 254 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 253 | + if (count($results) < $navigator_length) { |
|
| 254 | + $results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | return $results; |
@@ -266,24 +266,24 @@ discard block |
||
| 266 | 266 | ) { |
| 267 | 267 | global $wpdb; |
| 268 | 268 | |
| 269 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 269 | + $select = implode(', ', array_map(function($item) { |
|
| 270 | 270 | return "p.$item AS $item"; |
| 271 | - }, (array) $fields ) ); |
|
| 271 | + }, (array) $fields)); |
|
| 272 | 272 | |
| 273 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 273 | + $order_by = implode(', ', array_map(function($item) { |
|
| 274 | 274 | return "p.$item"; |
| 275 | - }, (array) $order_by ) ); |
|
| 275 | + }, (array) $order_by)); |
|
| 276 | 276 | |
| 277 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 278 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 279 | - if ( isset( $entity ) ) { |
|
| 277 | + $entities_in = implode(',', array_map(function($item) { |
|
| 278 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item)); |
|
| 279 | + if (isset($entity)) { |
|
| 280 | 280 | return $entity->ID; |
| 281 | 281 | } |
| 282 | - }, $entities ) ); |
|
| 282 | + }, $entities)); |
|
| 283 | 283 | |
| 284 | 284 | /** @noinspection SqlNoDataSourceInspection */ |
| 285 | 285 | return $wpdb->get_results( |
| 286 | - $wpdb->prepare( <<<EOF |
|
| 286 | + $wpdb->prepare(<<<EOF |
|
| 287 | 287 | SELECT %3\$s, p2.ID as entity_id |
| 288 | 288 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 289 | 289 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | LIMIT %1\$d |
| 310 | 310 | OFFSET %2\$d |
| 311 | 311 | EOF |
| 312 | - , $limit, $offset, $select, $order_by ) |
|
| 312 | + , $limit, $offset, $select, $order_by) |
|
| 313 | 313 | ); |
| 314 | 314 | |
| 315 | 315 | } |
@@ -324,12 +324,12 @@ discard block |
||
| 324 | 324 | |
| 325 | 325 | // Temporary blocking the Navigator. |
| 326 | 326 | $results = wl_shortcode_navigator_data(); |
| 327 | - wl_core_send_json( $results ); |
|
| 327 | + wl_core_send_json($results); |
|
| 328 | 328 | |
| 329 | 329 | } |
| 330 | 330 | |
| 331 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 332 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 331 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 332 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 333 | 333 | |
| 334 | 334 | /** |
| 335 | 335 | * wp-json call for the navigator widget |
@@ -337,13 +337,13 @@ discard block |
||
| 337 | 337 | function wl_shortcode_navigator_wp_json() { |
| 338 | 338 | |
| 339 | 339 | $results = wl_shortcode_navigator_data(); |
| 340 | - if ( ob_get_contents() ) { |
|
| 340 | + if (ob_get_contents()) { |
|
| 341 | 341 | ob_clean(); |
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | return array( |
| 345 | 345 | 'items' => array( |
| 346 | - array( 'values' => $results ), |
|
| 346 | + array('values' => $results), |
|
| 347 | 347 | ), |
| 348 | 348 | ); |
| 349 | 349 | |
@@ -352,23 +352,23 @@ discard block |
||
| 352 | 352 | /** |
| 353 | 353 | * Adding `rest_api_init` action for amp backend of navigator |
| 354 | 354 | */ |
| 355 | -add_action( 'rest_api_init', function () { |
|
| 356 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 355 | +add_action('rest_api_init', function() { |
|
| 356 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 357 | 357 | 'methods' => 'GET', |
| 358 | 358 | 'permission_callback' => '__return_true', |
| 359 | 359 | 'callback' => 'wl_shortcode_navigator_wp_json' |
| 360 | - ) ); |
|
| 360 | + )); |
|
| 361 | 361 | } ); |
| 362 | 362 | |
| 363 | 363 | /** |
| 364 | 364 | * Adding `rest_api_init` action for backend of network navigator |
| 365 | 365 | */ |
| 366 | -add_action( 'rest_api_init', function () { |
|
| 367 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 366 | +add_action('rest_api_init', function() { |
|
| 367 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 368 | 368 | 'methods' => 'GET', |
| 369 | 369 | 'callback' => 'wl_network_navigator_wp_json', |
| 370 | 370 | 'permission_callback' => '__return_true', |
| 371 | - ) ); |
|
| 371 | + )); |
|
| 372 | 372 | } ); |
| 373 | 373 | |
| 374 | 374 | /** |
@@ -376,24 +376,24 @@ discard block |
||
| 376 | 376 | * |
| 377 | 377 | * @since 2.2.0 |
| 378 | 378 | */ |
| 379 | -add_action( 'plugins_loaded', function () { |
|
| 379 | +add_action('plugins_loaded', function() { |
|
| 380 | 380 | |
| 381 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 381 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action']) { |
|
| 382 | 382 | return; |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 386 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 387 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 388 | -}, 0 ); |
|
| 385 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 386 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 387 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 388 | +}, 0); |
|
| 389 | 389 | |
| 390 | -add_action( 'init', function () { |
|
| 390 | +add_action('init', function() { |
|
| 391 | 391 | |
| 392 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 392 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action']) { |
|
| 393 | 393 | return; |
| 394 | 394 | } |
| 395 | 395 | |
| 396 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 397 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 398 | -}, 0 ); |
|
| 396 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 397 | + remove_action('init', 'gglcptch_init'); |
|
| 398 | +}, 0); |
|
| 399 | 399 | |
@@ -19,29 +19,29 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | function wl_shortcode_navigator_data() { |
| 21 | 21 | |
| 22 | - // Create the cache key. |
|
| 23 | - $cache_key_params = $_REQUEST; |
|
| 24 | - unset( $cache_key_params['uniqid'] ); |
|
| 25 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 22 | + // Create the cache key. |
|
| 23 | + $cache_key_params = $_REQUEST; |
|
| 24 | + unset( $cache_key_params['uniqid'] ); |
|
| 25 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 26 | 26 | |
| 27 | - // Create the TTL cache and try to get the results. |
|
| 28 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | - $cache_results = $cache->get( $cache_key ); |
|
| 27 | + // Create the TTL cache and try to get the results. |
|
| 28 | + $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | + $cache_results = $cache->get( $cache_key ); |
|
| 30 | 30 | |
| 31 | - if ( isset( $cache_results ) ) { |
|
| 32 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 31 | + if ( isset( $cache_results ) ) { |
|
| 32 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 33 | 33 | |
| 34 | - return $cache_results; |
|
| 35 | - } |
|
| 34 | + return $cache_results; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 37 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 38 | 38 | |
| 39 | - $results = _wl_navigator_get_data(); |
|
| 39 | + $results = _wl_navigator_get_data(); |
|
| 40 | 40 | |
| 41 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 42 | - $cache->put( $cache_key, $results ); |
|
| 41 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 42 | + $cache->put( $cache_key, $results ); |
|
| 43 | 43 | |
| 44 | - return $results; |
|
| 44 | + return $results; |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
@@ -56,235 +56,235 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | function wl_network_navigator_wp_json( $request ) { |
| 58 | 58 | |
| 59 | - // Create the cache key. |
|
| 60 | - $cache_key_params = $_REQUEST; |
|
| 61 | - unset( $cache_key_params['uniqid'] ); |
|
| 62 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 59 | + // Create the cache key. |
|
| 60 | + $cache_key_params = $_REQUEST; |
|
| 61 | + unset( $cache_key_params['uniqid'] ); |
|
| 62 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 63 | 63 | |
| 64 | - // Create the TTL cache and try to get the results. |
|
| 65 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 66 | - $cache_results = $cache->get( $cache_key ); |
|
| 64 | + // Create the TTL cache and try to get the results. |
|
| 65 | + $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 66 | + $cache_results = $cache->get( $cache_key ); |
|
| 67 | 67 | |
| 68 | - if ( isset( $cache_results ) ) { |
|
| 69 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 68 | + if ( isset( $cache_results ) ) { |
|
| 69 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 70 | 70 | |
| 71 | - return $cache_results; |
|
| 72 | - } |
|
| 71 | + return $cache_results; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 74 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 75 | 75 | |
| 76 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 76 | + $results = _wl_network_navigator_get_data( $request ); |
|
| 77 | 77 | |
| 78 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 79 | - $cache->put( $cache_key, $results ); |
|
| 78 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 79 | + $cache->put( $cache_key, $results ); |
|
| 80 | 80 | |
| 81 | - return $results; |
|
| 81 | + return $results; |
|
| 82 | 82 | |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | function _wl_navigator_get_data() { |
| 86 | 86 | |
| 87 | - // Post ID must be defined |
|
| 88 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 89 | - wp_send_json_error( 'No post_id given' ); |
|
| 90 | - |
|
| 91 | - return array(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // Post ID must be defined |
|
| 95 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 96 | - wp_send_json_error( 'No uniqid given' ); |
|
| 97 | - |
|
| 98 | - return array(); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // Limit the results (defaults to 4) |
|
| 102 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 103 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 104 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 105 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 106 | - $post_types = explode( ',', $post_types ); |
|
| 107 | - $existing_post_types = get_post_types(); |
|
| 108 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 109 | - $current_post_id = $_GET['post_id']; |
|
| 110 | - $current_post = get_post( $current_post_id ); |
|
| 111 | - |
|
| 112 | - $navigator_id = $_GET['uniqid']; |
|
| 113 | - |
|
| 114 | - // Post ID has to match an existing item |
|
| 115 | - if ( null === $current_post ) { |
|
| 116 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 117 | - |
|
| 118 | - return array(); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // Determine navigator type and call respective _get_results |
|
| 122 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 123 | - |
|
| 124 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 125 | - 'ID', |
|
| 126 | - 'post_title', |
|
| 127 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 128 | - } else { |
|
| 129 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 130 | - 'ID', |
|
| 131 | - 'post_title', |
|
| 132 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 133 | - |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 137 | - $results = array(); |
|
| 138 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 139 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Use the thumbnail. |
|
| 143 | - * |
|
| 144 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 145 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 146 | - * |
|
| 147 | - * @since 3.19.3 We're using the medium size image. |
|
| 148 | - */ |
|
| 149 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 150 | - |
|
| 151 | - $result = array( |
|
| 152 | - 'post' => array( |
|
| 153 | - 'id' => $referencing_post->ID, |
|
| 154 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 155 | - 'title' => $referencing_post->post_title, |
|
| 156 | - 'thumbnail' => $thumbnail, |
|
| 157 | - ), |
|
| 158 | - 'entity' => array( |
|
| 159 | - 'id' => $referencing_post->entity_id, |
|
| 160 | - 'label' => $serialized_entity['label'], |
|
| 161 | - 'mainType' => $serialized_entity['mainType'], |
|
| 162 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 163 | - ), |
|
| 164 | - ); |
|
| 165 | - |
|
| 166 | - $results[] = $result; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - if ( count( $results ) < $navigator_length ) { |
|
| 171 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - // Add filler posts if needed |
|
| 175 | - $filler_count = $navigator_length - count( $results ); |
|
| 176 | - if ( $filler_count > 0 ) { |
|
| 177 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 178 | - return $p->ID; |
|
| 179 | - }, $referencing_posts ); |
|
| 180 | - /** |
|
| 181 | - * @since 3.27.8 |
|
| 182 | - * Filler posts are fetched using this util. |
|
| 183 | - */ |
|
| 184 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 185 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 186 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 187 | - $results = array_merge( $results, $filler_posts ); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - // Apply filters after fillers are added |
|
| 191 | - foreach ( $results as $result_index => $result ) { |
|
| 192 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 193 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return $results; |
|
| 87 | + // Post ID must be defined |
|
| 88 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 89 | + wp_send_json_error( 'No post_id given' ); |
|
| 90 | + |
|
| 91 | + return array(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // Post ID must be defined |
|
| 95 | + if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 96 | + wp_send_json_error( 'No uniqid given' ); |
|
| 97 | + |
|
| 98 | + return array(); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // Limit the results (defaults to 4) |
|
| 102 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 103 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 104 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 105 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 106 | + $post_types = explode( ',', $post_types ); |
|
| 107 | + $existing_post_types = get_post_types(); |
|
| 108 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 109 | + $current_post_id = $_GET['post_id']; |
|
| 110 | + $current_post = get_post( $current_post_id ); |
|
| 111 | + |
|
| 112 | + $navigator_id = $_GET['uniqid']; |
|
| 113 | + |
|
| 114 | + // Post ID has to match an existing item |
|
| 115 | + if ( null === $current_post ) { |
|
| 116 | + wp_send_json_error( 'No valid post_id given' ); |
|
| 117 | + |
|
| 118 | + return array(); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // Determine navigator type and call respective _get_results |
|
| 122 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 123 | + |
|
| 124 | + $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 125 | + 'ID', |
|
| 126 | + 'post_title', |
|
| 127 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 128 | + } else { |
|
| 129 | + $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 130 | + 'ID', |
|
| 131 | + 'post_title', |
|
| 132 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 133 | + |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 137 | + $results = array(); |
|
| 138 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 139 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Use the thumbnail. |
|
| 143 | + * |
|
| 144 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 145 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 146 | + * |
|
| 147 | + * @since 3.19.3 We're using the medium size image. |
|
| 148 | + */ |
|
| 149 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 150 | + |
|
| 151 | + $result = array( |
|
| 152 | + 'post' => array( |
|
| 153 | + 'id' => $referencing_post->ID, |
|
| 154 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 155 | + 'title' => $referencing_post->post_title, |
|
| 156 | + 'thumbnail' => $thumbnail, |
|
| 157 | + ), |
|
| 158 | + 'entity' => array( |
|
| 159 | + 'id' => $referencing_post->entity_id, |
|
| 160 | + 'label' => $serialized_entity['label'], |
|
| 161 | + 'mainType' => $serialized_entity['mainType'], |
|
| 162 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 163 | + ), |
|
| 164 | + ); |
|
| 165 | + |
|
| 166 | + $results[] = $result; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + if ( count( $results ) < $navigator_length ) { |
|
| 171 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + // Add filler posts if needed |
|
| 175 | + $filler_count = $navigator_length - count( $results ); |
|
| 176 | + if ( $filler_count > 0 ) { |
|
| 177 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 178 | + return $p->ID; |
|
| 179 | + }, $referencing_posts ); |
|
| 180 | + /** |
|
| 181 | + * @since 3.27.8 |
|
| 182 | + * Filler posts are fetched using this util. |
|
| 183 | + */ |
|
| 184 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 185 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 186 | + $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 187 | + $results = array_merge( $results, $filler_posts ); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + // Apply filters after fillers are added |
|
| 191 | + foreach ( $results as $result_index => $result ) { |
|
| 192 | + $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 193 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return $results; |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | function _wl_network_navigator_get_data( $request ) { |
| 200 | 200 | |
| 201 | - // Limit the results (defaults to 4) |
|
| 202 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 203 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 204 | - $navigator_id = $request['uniqid']; |
|
| 205 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 206 | - |
|
| 207 | - $entities = $request['entities']; |
|
| 208 | - |
|
| 209 | - // Post ID has to match an existing item |
|
| 210 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 211 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 215 | - 'ID', |
|
| 216 | - 'post_title', |
|
| 217 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 218 | - |
|
| 219 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 220 | - $results = array(); |
|
| 221 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 222 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Use the thumbnail. |
|
| 226 | - * |
|
| 227 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 228 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 229 | - * |
|
| 230 | - * @since 3.19.3 We're using the medium size image. |
|
| 231 | - */ |
|
| 232 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 233 | - |
|
| 234 | - $result = array( |
|
| 235 | - 'post' => array( |
|
| 236 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 237 | - 'title' => $referencing_post->post_title, |
|
| 238 | - 'thumbnail' => $thumbnail, |
|
| 239 | - ), |
|
| 240 | - 'entity' => array( |
|
| 241 | - 'label' => $serialized_entity['label'], |
|
| 242 | - 'mainType' => $serialized_entity['mainType'], |
|
| 243 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 244 | - ), |
|
| 245 | - ); |
|
| 246 | - |
|
| 247 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 248 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 249 | - |
|
| 250 | - $results[] = $result; |
|
| 251 | - |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if ( count( $results ) < $navigator_length ) { |
|
| 255 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - return $results; |
|
| 201 | + // Limit the results (defaults to 4) |
|
| 202 | + $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 203 | + $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 204 | + $navigator_id = $request['uniqid']; |
|
| 205 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 206 | + |
|
| 207 | + $entities = $request['entities']; |
|
| 208 | + |
|
| 209 | + // Post ID has to match an existing item |
|
| 210 | + if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 211 | + wp_send_json_error( 'No valid entities provided' ); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 215 | + 'ID', |
|
| 216 | + 'post_title', |
|
| 217 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 218 | + |
|
| 219 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 220 | + $results = array(); |
|
| 221 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 222 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Use the thumbnail. |
|
| 226 | + * |
|
| 227 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 228 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 229 | + * |
|
| 230 | + * @since 3.19.3 We're using the medium size image. |
|
| 231 | + */ |
|
| 232 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 233 | + |
|
| 234 | + $result = array( |
|
| 235 | + 'post' => array( |
|
| 236 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 237 | + 'title' => $referencing_post->post_title, |
|
| 238 | + 'thumbnail' => $thumbnail, |
|
| 239 | + ), |
|
| 240 | + 'entity' => array( |
|
| 241 | + 'label' => $serialized_entity['label'], |
|
| 242 | + 'mainType' => $serialized_entity['mainType'], |
|
| 243 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 244 | + ), |
|
| 245 | + ); |
|
| 246 | + |
|
| 247 | + $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 248 | + $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 249 | + |
|
| 250 | + $results[] = $result; |
|
| 251 | + |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if ( count( $results ) < $navigator_length ) { |
|
| 255 | + $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + return $results; |
|
| 259 | 259 | |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | function _wl_network_navigator_get_results( |
| 263 | - $entities, $fields = array( |
|
| 264 | - 'ID', |
|
| 265 | - 'post_title', |
|
| 263 | + $entities, $fields = array( |
|
| 264 | + 'ID', |
|
| 265 | + 'post_title', |
|
| 266 | 266 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 267 | 267 | ) { |
| 268 | - global $wpdb; |
|
| 269 | - |
|
| 270 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 271 | - return "p.$item AS $item"; |
|
| 272 | - }, (array) $fields ) ); |
|
| 273 | - |
|
| 274 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 275 | - return "p.$item"; |
|
| 276 | - }, (array) $order_by ) ); |
|
| 277 | - |
|
| 278 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 279 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 280 | - if ( isset( $entity ) ) { |
|
| 281 | - return $entity->ID; |
|
| 282 | - } |
|
| 283 | - }, $entities ) ); |
|
| 284 | - |
|
| 285 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 286 | - return $wpdb->get_results( |
|
| 287 | - $wpdb->prepare( <<<EOF |
|
| 268 | + global $wpdb; |
|
| 269 | + |
|
| 270 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 271 | + return "p.$item AS $item"; |
|
| 272 | + }, (array) $fields ) ); |
|
| 273 | + |
|
| 274 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 275 | + return "p.$item"; |
|
| 276 | + }, (array) $order_by ) ); |
|
| 277 | + |
|
| 278 | + $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 279 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 280 | + if ( isset( $entity ) ) { |
|
| 281 | + return $entity->ID; |
|
| 282 | + } |
|
| 283 | + }, $entities ) ); |
|
| 284 | + |
|
| 285 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 286 | + return $wpdb->get_results( |
|
| 287 | + $wpdb->prepare( <<<EOF |
|
| 288 | 288 | SELECT %3\$s, p2.ID as entity_id |
| 289 | 289 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 290 | 290 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -310,8 +310,8 @@ discard block |
||
| 310 | 310 | LIMIT %1\$d |
| 311 | 311 | OFFSET %2\$d |
| 312 | 312 | EOF |
| 313 | - , $limit, $offset, $select, $order_by ) |
|
| 314 | - ); |
|
| 313 | + , $limit, $offset, $select, $order_by ) |
|
| 314 | + ); |
|
| 315 | 315 | |
| 316 | 316 | } |
| 317 | 317 | |
@@ -323,9 +323,9 @@ discard block |
||
| 323 | 323 | */ |
| 324 | 324 | function wl_shortcode_navigator_ajax() { |
| 325 | 325 | |
| 326 | - // Temporary blocking the Navigator. |
|
| 327 | - $results = wl_shortcode_navigator_data(); |
|
| 328 | - wl_core_send_json( $results ); |
|
| 326 | + // Temporary blocking the Navigator. |
|
| 327 | + $results = wl_shortcode_navigator_data(); |
|
| 328 | + wl_core_send_json( $results ); |
|
| 329 | 329 | |
| 330 | 330 | } |
| 331 | 331 | |
@@ -337,16 +337,16 @@ discard block |
||
| 337 | 337 | */ |
| 338 | 338 | function wl_shortcode_navigator_wp_json() { |
| 339 | 339 | |
| 340 | - $results = wl_shortcode_navigator_data(); |
|
| 341 | - if ( ob_get_contents() ) { |
|
| 342 | - ob_clean(); |
|
| 343 | - } |
|
| 340 | + $results = wl_shortcode_navigator_data(); |
|
| 341 | + if ( ob_get_contents() ) { |
|
| 342 | + ob_clean(); |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - return array( |
|
| 346 | - 'items' => array( |
|
| 347 | - array( 'values' => $results ), |
|
| 348 | - ), |
|
| 349 | - ); |
|
| 345 | + return array( |
|
| 346 | + 'items' => array( |
|
| 347 | + array( 'values' => $results ), |
|
| 348 | + ), |
|
| 349 | + ); |
|
| 350 | 350 | |
| 351 | 351 | } |
| 352 | 352 | |
@@ -354,22 +354,22 @@ discard block |
||
| 354 | 354 | * Adding `rest_api_init` action for amp backend of navigator |
| 355 | 355 | */ |
| 356 | 356 | add_action( 'rest_api_init', function () { |
| 357 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 358 | - 'methods' => 'GET', |
|
| 359 | - 'permission_callback' => '__return_true', |
|
| 360 | - 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 361 | - ) ); |
|
| 357 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 358 | + 'methods' => 'GET', |
|
| 359 | + 'permission_callback' => '__return_true', |
|
| 360 | + 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 361 | + ) ); |
|
| 362 | 362 | } ); |
| 363 | 363 | |
| 364 | 364 | /** |
| 365 | 365 | * Adding `rest_api_init` action for backend of network navigator |
| 366 | 366 | */ |
| 367 | 367 | add_action( 'rest_api_init', function () { |
| 368 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 369 | - 'methods' => 'GET', |
|
| 370 | - 'callback' => 'wl_network_navigator_wp_json', |
|
| 371 | - 'permission_callback' => '__return_true', |
|
| 372 | - ) ); |
|
| 368 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 369 | + 'methods' => 'GET', |
|
| 370 | + 'callback' => 'wl_network_navigator_wp_json', |
|
| 371 | + 'permission_callback' => '__return_true', |
|
| 372 | + ) ); |
|
| 373 | 373 | } ); |
| 374 | 374 | |
| 375 | 375 | /** |
@@ -379,22 +379,22 @@ discard block |
||
| 379 | 379 | */ |
| 380 | 380 | add_action( 'plugins_loaded', function () { |
| 381 | 381 | |
| 382 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 383 | - return; |
|
| 384 | - } |
|
| 382 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 383 | + return; |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 387 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 388 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 386 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 387 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 388 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 389 | 389 | }, 0 ); |
| 390 | 390 | |
| 391 | 391 | add_action( 'init', function () { |
| 392 | 392 | |
| 393 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 394 | - return; |
|
| 395 | - } |
|
| 393 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 394 | + return; |
|
| 395 | + } |
|
| 396 | 396 | |
| 397 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 398 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 397 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 398 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 399 | 399 | }, 0 ); |
| 400 | 400 | |
@@ -7,36 +7,36 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Navigator_Data { |
| 9 | 9 | |
| 10 | - public static function get_post_types_as_string( $post_types ) { |
|
| 11 | - if ( $post_types === array() ) { |
|
| 12 | - $post_types = get_post_types(); |
|
| 13 | - } |
|
| 14 | - $post_types = array_map( function ( $post_type ) { |
|
| 15 | - return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | - }, $post_types ); |
|
| 17 | - |
|
| 18 | - return implode( ',', $post_types ); |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - public static function post_navigator_get_results( |
|
| 22 | - $post_id, $fields = array( |
|
| 23 | - 'ID', |
|
| 24 | - 'post_title', |
|
| 25 | - ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 26 | - ) { |
|
| 27 | - |
|
| 28 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 29 | - global $wpdb; |
|
| 30 | - |
|
| 31 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 32 | - return "p.$item AS $item"; |
|
| 33 | - }, (array) $fields ) ); |
|
| 34 | - |
|
| 35 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 36 | - return "p.$item"; |
|
| 37 | - }, (array) $order_by ) ); |
|
| 38 | - |
|
| 39 | - $sql = $wpdb->prepare( <<<EOF |
|
| 10 | + public static function get_post_types_as_string( $post_types ) { |
|
| 11 | + if ( $post_types === array() ) { |
|
| 12 | + $post_types = get_post_types(); |
|
| 13 | + } |
|
| 14 | + $post_types = array_map( function ( $post_type ) { |
|
| 15 | + return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | + }, $post_types ); |
|
| 17 | + |
|
| 18 | + return implode( ',', $post_types ); |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + public static function post_navigator_get_results( |
|
| 22 | + $post_id, $fields = array( |
|
| 23 | + 'ID', |
|
| 24 | + 'post_title', |
|
| 25 | + ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 26 | + ) { |
|
| 27 | + |
|
| 28 | + $post_types = self::get_post_types_as_string( $post_types ); |
|
| 29 | + global $wpdb; |
|
| 30 | + |
|
| 31 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 32 | + return "p.$item AS $item"; |
|
| 33 | + }, (array) $fields ) ); |
|
| 34 | + |
|
| 35 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 36 | + return "p.$item"; |
|
| 37 | + }, (array) $order_by ) ); |
|
| 38 | + |
|
| 39 | + $sql = $wpdb->prepare( <<<EOF |
|
| 40 | 40 | SELECT %4\$s, p2.ID as entity_id |
| 41 | 41 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 42 | 42 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -70,37 +70,37 @@ discard block |
||
| 70 | 70 | LIMIT %2\$d |
| 71 | 71 | OFFSET %3\$d |
| 72 | 72 | EOF |
| 73 | - , $post_id, $limit, $offset, $select, $order_by ); |
|
| 73 | + , $post_id, $limit, $offset, $select, $order_by ); |
|
| 74 | 74 | |
| 75 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 76 | - return $wpdb->get_results( |
|
| 77 | - $sql |
|
| 78 | - ); |
|
| 75 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 76 | + return $wpdb->get_results( |
|
| 77 | + $sql |
|
| 78 | + ); |
|
| 79 | 79 | |
| 80 | - } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | 82 | |
| 83 | - public static function entity_navigator_get_results( |
|
| 84 | - $post_id, $fields = array( |
|
| 85 | - 'ID', |
|
| 86 | - 'post_title', |
|
| 87 | - ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 88 | - ) { |
|
| 89 | - global $wpdb; |
|
| 83 | + public static function entity_navigator_get_results( |
|
| 84 | + $post_id, $fields = array( |
|
| 85 | + 'ID', |
|
| 86 | + 'post_title', |
|
| 87 | + ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 88 | + ) { |
|
| 89 | + global $wpdb; |
|
| 90 | 90 | |
| 91 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 92 | - return "p.$item AS $item"; |
|
| 93 | - }, (array) $fields ) ); |
|
| 91 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 92 | + return "p.$item AS $item"; |
|
| 93 | + }, (array) $fields ) ); |
|
| 94 | 94 | |
| 95 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 96 | - return "p.$item"; |
|
| 97 | - }, (array) $order_by ) ); |
|
| 95 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 96 | + return "p.$item"; |
|
| 97 | + }, (array) $order_by ) ); |
|
| 98 | 98 | |
| 99 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 99 | + $post_types = self::get_post_types_as_string( $post_types ); |
|
| 100 | 100 | |
| 101 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 102 | - return $wpdb->get_results( |
|
| 103 | - $wpdb->prepare( <<<EOF |
|
| 101 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 102 | + return $wpdb->get_results( |
|
| 103 | + $wpdb->prepare( <<<EOF |
|
| 104 | 104 | SELECT %4\$s, p2.ID as entity_id |
| 105 | 105 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 106 | 106 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -131,9 +131,9 @@ discard block |
||
| 131 | 131 | LIMIT %2\$d |
| 132 | 132 | OFFSET %3\$d |
| 133 | 133 | EOF |
| 134 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 135 | - ); |
|
| 136 | - } |
|
| 134 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 135 | + ); |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | |
| 139 | 139 | } |
@@ -7,15 +7,15 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Navigator_Data { |
| 9 | 9 | |
| 10 | - public static function get_post_types_as_string( $post_types ) { |
|
| 11 | - if ( $post_types === array() ) { |
|
| 10 | + public static function get_post_types_as_string($post_types) { |
|
| 11 | + if ($post_types === array()) { |
|
| 12 | 12 | $post_types = get_post_types(); |
| 13 | 13 | } |
| 14 | - $post_types = array_map( function ( $post_type ) { |
|
| 15 | - return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | - }, $post_types ); |
|
| 14 | + $post_types = array_map(function($post_type) { |
|
| 15 | + return "'".esc_sql($post_type)."'"; |
|
| 16 | + }, $post_types); |
|
| 17 | 17 | |
| 18 | - return implode( ',', $post_types ); |
|
| 18 | + return implode(',', $post_types); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public static function post_navigator_get_results( |
@@ -25,18 +25,18 @@ discard block |
||
| 25 | 25 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
| 26 | 26 | ) { |
| 27 | 27 | |
| 28 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 28 | + $post_types = self::get_post_types_as_string($post_types); |
|
| 29 | 29 | global $wpdb; |
| 30 | 30 | |
| 31 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 31 | + $select = implode(', ', array_map(function($item) { |
|
| 32 | 32 | return "p.$item AS $item"; |
| 33 | - }, (array) $fields ) ); |
|
| 33 | + }, (array) $fields)); |
|
| 34 | 34 | |
| 35 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 35 | + $order_by = implode(', ', array_map(function($item) { |
|
| 36 | 36 | return "p.$item"; |
| 37 | - }, (array) $order_by ) ); |
|
| 37 | + }, (array) $order_by)); |
|
| 38 | 38 | |
| 39 | - $sql = $wpdb->prepare( <<<EOF |
|
| 39 | + $sql = $wpdb->prepare(<<<EOF |
|
| 40 | 40 | SELECT %4\$s, p2.ID as entity_id |
| 41 | 41 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 42 | 42 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | LIMIT %2\$d |
| 71 | 71 | OFFSET %3\$d |
| 72 | 72 | EOF |
| 73 | - , $post_id, $limit, $offset, $select, $order_by ); |
|
| 73 | + , $post_id, $limit, $offset, $select, $order_by); |
|
| 74 | 74 | |
| 75 | 75 | /** @noinspection SqlNoDataSourceInspection */ |
| 76 | 76 | return $wpdb->get_results( |
@@ -88,19 +88,19 @@ discard block |
||
| 88 | 88 | ) { |
| 89 | 89 | global $wpdb; |
| 90 | 90 | |
| 91 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 91 | + $select = implode(', ', array_map(function($item) { |
|
| 92 | 92 | return "p.$item AS $item"; |
| 93 | - }, (array) $fields ) ); |
|
| 93 | + }, (array) $fields)); |
|
| 94 | 94 | |
| 95 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 95 | + $order_by = implode(', ', array_map(function($item) { |
|
| 96 | 96 | return "p.$item"; |
| 97 | - }, (array) $order_by ) ); |
|
| 97 | + }, (array) $order_by)); |
|
| 98 | 98 | |
| 99 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 99 | + $post_types = self::get_post_types_as_string($post_types); |
|
| 100 | 100 | |
| 101 | 101 | /** @noinspection SqlNoDataSourceInspection */ |
| 102 | 102 | return $wpdb->get_results( |
| 103 | - $wpdb->prepare( <<<EOF |
|
| 103 | + $wpdb->prepare(<<<EOF |
|
| 104 | 104 | SELECT %4\$s, p2.ID as entity_id |
| 105 | 105 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 106 | 106 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | LIMIT %2\$d |
| 132 | 132 | OFFSET %3\$d |
| 133 | 133 | EOF |
| 134 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 134 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 135 | 135 | ); |
| 136 | 136 | } |
| 137 | 137 | |