@@ -16,29 +16,29 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | function wl_shortcode_navigator_data() { |
| 18 | 18 | |
| 19 | - // Create the cache key. |
|
| 20 | - $cache_key_params = $_REQUEST; |
|
| 21 | - unset( $cache_key_params['uniqid'] ); |
|
| 22 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 19 | + // Create the cache key. |
|
| 20 | + $cache_key_params = $_REQUEST; |
|
| 21 | + unset( $cache_key_params['uniqid'] ); |
|
| 22 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 23 | 23 | |
| 24 | - // Create the TTL cache and try to get the results. |
|
| 25 | - $cache = new Ttl_Cache( "navigator", 24 * 60 * 60 ); // 24 hours. |
|
| 26 | - $cache_results = $cache->get( $cache_key ); |
|
| 24 | + // Create the TTL cache and try to get the results. |
|
| 25 | + $cache = new Ttl_Cache( "navigator", 24 * 60 * 60 ); // 24 hours. |
|
| 26 | + $cache_results = $cache->get( $cache_key ); |
|
| 27 | 27 | |
| 28 | - if ( isset( $cache_results ) ) { |
|
| 29 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 28 | + if ( isset( $cache_results ) ) { |
|
| 29 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 30 | 30 | |
| 31 | - return $cache_results; |
|
| 32 | - } |
|
| 31 | + return $cache_results; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 34 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 35 | 35 | |
| 36 | - $results = _wl_navigator_get_data(); |
|
| 36 | + $results = _wl_navigator_get_data(); |
|
| 37 | 37 | |
| 38 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 39 | - $cache->put( $cache_key, $results ); |
|
| 38 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 39 | + $cache->put( $cache_key, $results ); |
|
| 40 | 40 | |
| 41 | - return $results; |
|
| 41 | + return $results; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
@@ -53,195 +53,195 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | function wl_network_navigator_wp_json( $request ) { |
| 55 | 55 | |
| 56 | - // Create the cache key. |
|
| 57 | - $cache_key_params = $_REQUEST; |
|
| 58 | - unset( $cache_key_params['uniqid'] ); |
|
| 59 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 56 | + // Create the cache key. |
|
| 57 | + $cache_key_params = $_REQUEST; |
|
| 58 | + unset( $cache_key_params['uniqid'] ); |
|
| 59 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 60 | 60 | |
| 61 | - // Create the TTL cache and try to get the results. |
|
| 62 | - $cache = new Ttl_Cache( "network-navigator", 24 * 60 * 60 ); // 24 hours. |
|
| 63 | - $cache_results = $cache->get( $cache_key ); |
|
| 61 | + // Create the TTL cache and try to get the results. |
|
| 62 | + $cache = new Ttl_Cache( "network-navigator", 24 * 60 * 60 ); // 24 hours. |
|
| 63 | + $cache_results = $cache->get( $cache_key ); |
|
| 64 | 64 | |
| 65 | - if ( isset( $cache_results ) ) { |
|
| 66 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 65 | + if ( isset( $cache_results ) ) { |
|
| 66 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 67 | 67 | |
| 68 | - return $cache_results; |
|
| 69 | - } |
|
| 68 | + return $cache_results; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 71 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 72 | 72 | |
| 73 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 73 | + $results = _wl_network_navigator_get_data( $request ); |
|
| 74 | 74 | |
| 75 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 76 | - $cache->put( $cache_key, $results ); |
|
| 75 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 76 | + $cache->put( $cache_key, $results ); |
|
| 77 | 77 | |
| 78 | - return $results; |
|
| 78 | + return $results; |
|
| 79 | 79 | |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | function _wl_navigator_get_data() { |
| 83 | 83 | |
| 84 | - // Post ID must be defined |
|
| 85 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 86 | - wp_send_json_error( 'No post_id given' ); |
|
| 87 | - |
|
| 88 | - return array(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Limit the results (defaults to 4) |
|
| 92 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 93 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 94 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 95 | - |
|
| 96 | - $current_post_id = $_GET['post_id']; |
|
| 97 | - $current_post = get_post( $current_post_id ); |
|
| 98 | - |
|
| 99 | - $navigator_id = $_GET['uniqid']; |
|
| 100 | - |
|
| 101 | - // Post ID has to match an existing item |
|
| 102 | - if ( null === $current_post ) { |
|
| 103 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 104 | - |
|
| 105 | - return array(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Determine navigator type and call respective _get_results |
|
| 109 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 110 | - $referencing_posts = _wl_entity_navigator_get_results( $current_post_id, array( |
|
| 111 | - 'ID', |
|
| 112 | - 'post_title', |
|
| 113 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 114 | - } else { |
|
| 115 | - $referencing_posts = _wl_navigator_get_results( $current_post_id, array( |
|
| 116 | - 'ID', |
|
| 117 | - 'post_title', |
|
| 118 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 122 | - $results = array(); |
|
| 123 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 124 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Use the thumbnail. |
|
| 128 | - * |
|
| 129 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 130 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 131 | - * |
|
| 132 | - * @since 3.19.3 We're using the medium size image. |
|
| 133 | - */ |
|
| 134 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 135 | - |
|
| 136 | - $result = array( |
|
| 137 | - 'post' => array( |
|
| 138 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 139 | - 'title' => $referencing_post->post_title, |
|
| 140 | - 'thumbnail' => $thumbnail, |
|
| 141 | - ), |
|
| 142 | - 'entity' => array( |
|
| 143 | - 'label' => $serialized_entity['label'], |
|
| 144 | - 'mainType' => $serialized_entity['mainType'], |
|
| 145 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 146 | - ), |
|
| 147 | - ); |
|
| 148 | - |
|
| 149 | - $result['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 150 | - $result['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 151 | - |
|
| 152 | - $results[] = $result; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if ( count( $results ) < $navigator_length ) { |
|
| 156 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $results; |
|
| 84 | + // Post ID must be defined |
|
| 85 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 86 | + wp_send_json_error( 'No post_id given' ); |
|
| 87 | + |
|
| 88 | + return array(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Limit the results (defaults to 4) |
|
| 92 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 93 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 94 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 95 | + |
|
| 96 | + $current_post_id = $_GET['post_id']; |
|
| 97 | + $current_post = get_post( $current_post_id ); |
|
| 98 | + |
|
| 99 | + $navigator_id = $_GET['uniqid']; |
|
| 100 | + |
|
| 101 | + // Post ID has to match an existing item |
|
| 102 | + if ( null === $current_post ) { |
|
| 103 | + wp_send_json_error( 'No valid post_id given' ); |
|
| 104 | + |
|
| 105 | + return array(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Determine navigator type and call respective _get_results |
|
| 109 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 110 | + $referencing_posts = _wl_entity_navigator_get_results( $current_post_id, array( |
|
| 111 | + 'ID', |
|
| 112 | + 'post_title', |
|
| 113 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 114 | + } else { |
|
| 115 | + $referencing_posts = _wl_navigator_get_results( $current_post_id, array( |
|
| 116 | + 'ID', |
|
| 117 | + 'post_title', |
|
| 118 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 122 | + $results = array(); |
|
| 123 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 124 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Use the thumbnail. |
|
| 128 | + * |
|
| 129 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 130 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 131 | + * |
|
| 132 | + * @since 3.19.3 We're using the medium size image. |
|
| 133 | + */ |
|
| 134 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 135 | + |
|
| 136 | + $result = array( |
|
| 137 | + 'post' => array( |
|
| 138 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 139 | + 'title' => $referencing_post->post_title, |
|
| 140 | + 'thumbnail' => $thumbnail, |
|
| 141 | + ), |
|
| 142 | + 'entity' => array( |
|
| 143 | + 'label' => $serialized_entity['label'], |
|
| 144 | + 'mainType' => $serialized_entity['mainType'], |
|
| 145 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 146 | + ), |
|
| 147 | + ); |
|
| 148 | + |
|
| 149 | + $result['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 150 | + $result['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 151 | + |
|
| 152 | + $results[] = $result; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if ( count( $results ) < $navigator_length ) { |
|
| 156 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $results; |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | function _wl_network_navigator_get_data( $request ) { |
| 163 | 163 | |
| 164 | - // Limit the results (defaults to 4) |
|
| 165 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 166 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 167 | - $navigator_id = $request['uniqid']; |
|
| 168 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 169 | - |
|
| 170 | - $entities = $request['entities']; |
|
| 171 | - |
|
| 172 | - // Post ID has to match an existing item |
|
| 173 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 174 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 178 | - 'ID', |
|
| 179 | - 'post_title', |
|
| 180 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 181 | - |
|
| 182 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 183 | - $results = array(); |
|
| 184 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 185 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Use the thumbnail. |
|
| 189 | - * |
|
| 190 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 191 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 192 | - * |
|
| 193 | - * @since 3.19.3 We're using the medium size image. |
|
| 194 | - */ |
|
| 195 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 196 | - |
|
| 197 | - $result = array( |
|
| 198 | - 'post' => array( |
|
| 199 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 200 | - 'title' => $referencing_post->post_title, |
|
| 201 | - 'thumbnail' => $thumbnail, |
|
| 202 | - ), |
|
| 203 | - 'entity' => array( |
|
| 204 | - 'label' => $serialized_entity['label'], |
|
| 205 | - 'mainType' => $serialized_entity['mainType'], |
|
| 206 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 207 | - ), |
|
| 208 | - ); |
|
| 209 | - |
|
| 210 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 211 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 212 | - |
|
| 213 | - $results[] = $result; |
|
| 214 | - |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if ( count( $results ) < $navigator_length ) { |
|
| 218 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - return $results; |
|
| 164 | + // Limit the results (defaults to 4) |
|
| 165 | + $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 166 | + $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 167 | + $navigator_id = $request['uniqid']; |
|
| 168 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 169 | + |
|
| 170 | + $entities = $request['entities']; |
|
| 171 | + |
|
| 172 | + // Post ID has to match an existing item |
|
| 173 | + if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 174 | + wp_send_json_error( 'No valid entities provided' ); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 178 | + 'ID', |
|
| 179 | + 'post_title', |
|
| 180 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 181 | + |
|
| 182 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 183 | + $results = array(); |
|
| 184 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 185 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Use the thumbnail. |
|
| 189 | + * |
|
| 190 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 191 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 192 | + * |
|
| 193 | + * @since 3.19.3 We're using the medium size image. |
|
| 194 | + */ |
|
| 195 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 196 | + |
|
| 197 | + $result = array( |
|
| 198 | + 'post' => array( |
|
| 199 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 200 | + 'title' => $referencing_post->post_title, |
|
| 201 | + 'thumbnail' => $thumbnail, |
|
| 202 | + ), |
|
| 203 | + 'entity' => array( |
|
| 204 | + 'label' => $serialized_entity['label'], |
|
| 205 | + 'mainType' => $serialized_entity['mainType'], |
|
| 206 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 207 | + ), |
|
| 208 | + ); |
|
| 209 | + |
|
| 210 | + $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 211 | + $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 212 | + |
|
| 213 | + $results[] = $result; |
|
| 214 | + |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if ( count( $results ) < $navigator_length ) { |
|
| 218 | + $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + return $results; |
|
| 222 | 222 | |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | |
| 226 | 226 | function _wl_navigator_get_results( |
| 227 | - $post_id, $fields = array( |
|
| 228 | - 'ID', |
|
| 229 | - 'post_title', |
|
| 227 | + $post_id, $fields = array( |
|
| 228 | + 'ID', |
|
| 229 | + 'post_title', |
|
| 230 | 230 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 231 | 231 | ) { |
| 232 | - global $wpdb; |
|
| 232 | + global $wpdb; |
|
| 233 | 233 | |
| 234 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 235 | - return "p.$item AS $item"; |
|
| 236 | - }, (array) $fields ) ); |
|
| 234 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 235 | + return "p.$item AS $item"; |
|
| 236 | + }, (array) $fields ) ); |
|
| 237 | 237 | |
| 238 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 239 | - return "p.$item"; |
|
| 240 | - }, (array) $order_by ) ); |
|
| 238 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 239 | + return "p.$item"; |
|
| 240 | + }, (array) $order_by ) ); |
|
| 241 | 241 | |
| 242 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 243 | - return $wpdb->get_results( |
|
| 244 | - $wpdb->prepare( <<<EOF |
|
| 242 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 243 | + return $wpdb->get_results( |
|
| 244 | + $wpdb->prepare( <<<EOF |
|
| 245 | 245 | SELECT %4\$s, p2.ID as entity_id |
| 246 | 246 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 247 | 247 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -273,30 +273,30 @@ discard block |
||
| 273 | 273 | LIMIT %2\$d |
| 274 | 274 | OFFSET %3\$d |
| 275 | 275 | EOF |
| 276 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 277 | - ); |
|
| 276 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 277 | + ); |
|
| 278 | 278 | |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | function _wl_entity_navigator_get_results( |
| 282 | - $post_id, $fields = array( |
|
| 283 | - 'ID', |
|
| 284 | - 'post_title', |
|
| 282 | + $post_id, $fields = array( |
|
| 283 | + 'ID', |
|
| 284 | + 'post_title', |
|
| 285 | 285 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 286 | 286 | ) { |
| 287 | - global $wpdb; |
|
| 287 | + global $wpdb; |
|
| 288 | 288 | |
| 289 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 290 | - return "p.$item AS $item"; |
|
| 291 | - }, (array) $fields ) ); |
|
| 289 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 290 | + return "p.$item AS $item"; |
|
| 291 | + }, (array) $fields ) ); |
|
| 292 | 292 | |
| 293 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 294 | - return "p.$item"; |
|
| 295 | - }, (array) $order_by ) ); |
|
| 293 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 294 | + return "p.$item"; |
|
| 295 | + }, (array) $order_by ) ); |
|
| 296 | 296 | |
| 297 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 298 | - return $wpdb->get_results( |
|
| 299 | - $wpdb->prepare( <<<EOF |
|
| 297 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 298 | + return $wpdb->get_results( |
|
| 299 | + $wpdb->prepare( <<<EOF |
|
| 300 | 300 | SELECT %4\$s, p2.ID as entity_id |
| 301 | 301 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 302 | 302 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -325,36 +325,36 @@ discard block |
||
| 325 | 325 | LIMIT %2\$d |
| 326 | 326 | OFFSET %3\$d |
| 327 | 327 | EOF |
| 328 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 329 | - ); |
|
| 328 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 329 | + ); |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | function _wl_network_navigator_get_results( |
| 333 | - $entities, $fields = array( |
|
| 334 | - 'ID', |
|
| 335 | - 'post_title', |
|
| 333 | + $entities, $fields = array( |
|
| 334 | + 'ID', |
|
| 335 | + 'post_title', |
|
| 336 | 336 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 337 | 337 | ) { |
| 338 | - global $wpdb; |
|
| 339 | - |
|
| 340 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 341 | - return "p.$item AS $item"; |
|
| 342 | - }, (array) $fields ) ); |
|
| 343 | - |
|
| 344 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 345 | - return "p.$item"; |
|
| 346 | - }, (array) $order_by ) ); |
|
| 347 | - |
|
| 348 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 349 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 350 | - if ( isset( $entity ) ) { |
|
| 351 | - return $entity->ID; |
|
| 352 | - } |
|
| 353 | - }, $entities ) ); |
|
| 354 | - |
|
| 355 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 356 | - return $wpdb->get_results( |
|
| 357 | - $wpdb->prepare( <<<EOF |
|
| 338 | + global $wpdb; |
|
| 339 | + |
|
| 340 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 341 | + return "p.$item AS $item"; |
|
| 342 | + }, (array) $fields ) ); |
|
| 343 | + |
|
| 344 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 345 | + return "p.$item"; |
|
| 346 | + }, (array) $order_by ) ); |
|
| 347 | + |
|
| 348 | + $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 349 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 350 | + if ( isset( $entity ) ) { |
|
| 351 | + return $entity->ID; |
|
| 352 | + } |
|
| 353 | + }, $entities ) ); |
|
| 354 | + |
|
| 355 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 356 | + return $wpdb->get_results( |
|
| 357 | + $wpdb->prepare( <<<EOF |
|
| 358 | 358 | SELECT %3\$s, p2.ID as entity_id |
| 359 | 359 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 360 | 360 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -380,8 +380,8 @@ discard block |
||
| 380 | 380 | LIMIT %1\$d |
| 381 | 381 | OFFSET %2\$d |
| 382 | 382 | EOF |
| 383 | - , $limit, $offset, $select, $order_by ) |
|
| 384 | - ); |
|
| 383 | + , $limit, $offset, $select, $order_by ) |
|
| 384 | + ); |
|
| 385 | 385 | |
| 386 | 386 | } |
| 387 | 387 | |
@@ -392,9 +392,9 @@ discard block |
||
| 392 | 392 | */ |
| 393 | 393 | function wl_shortcode_navigator_ajax() { |
| 394 | 394 | |
| 395 | - // Temporary blocking the Navigator. |
|
| 396 | - $results = wl_shortcode_navigator_data(); |
|
| 397 | - wl_core_send_json( $results ); |
|
| 395 | + // Temporary blocking the Navigator. |
|
| 396 | + $results = wl_shortcode_navigator_data(); |
|
| 397 | + wl_core_send_json( $results ); |
|
| 398 | 398 | |
| 399 | 399 | } |
| 400 | 400 | |
@@ -406,16 +406,16 @@ discard block |
||
| 406 | 406 | */ |
| 407 | 407 | function wl_shortcode_navigator_wp_json() { |
| 408 | 408 | |
| 409 | - $results = wl_shortcode_navigator_data(); |
|
| 410 | - if ( ob_get_contents() ) { |
|
| 411 | - ob_clean(); |
|
| 412 | - } |
|
| 409 | + $results = wl_shortcode_navigator_data(); |
|
| 410 | + if ( ob_get_contents() ) { |
|
| 411 | + ob_clean(); |
|
| 412 | + } |
|
| 413 | 413 | |
| 414 | - return array( |
|
| 415 | - 'items' => array( |
|
| 416 | - array( 'values' => $results ), |
|
| 417 | - ), |
|
| 418 | - ); |
|
| 414 | + return array( |
|
| 415 | + 'items' => array( |
|
| 416 | + array( 'values' => $results ), |
|
| 417 | + ), |
|
| 418 | + ); |
|
| 419 | 419 | |
| 420 | 420 | } |
| 421 | 421 | |
@@ -423,22 +423,22 @@ discard block |
||
| 423 | 423 | * Adding `rest_api_init` action for amp backend of navigator |
| 424 | 424 | */ |
| 425 | 425 | add_action( 'rest_api_init', function () { |
| 426 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 427 | - 'methods' => 'GET', |
|
| 428 | - 'callback' => 'wl_shortcode_navigator_wp_json', |
|
| 429 | - 'permission_callback' => '__return_true', |
|
| 430 | - ) ); |
|
| 426 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 427 | + 'methods' => 'GET', |
|
| 428 | + 'callback' => 'wl_shortcode_navigator_wp_json', |
|
| 429 | + 'permission_callback' => '__return_true', |
|
| 430 | + ) ); |
|
| 431 | 431 | } ); |
| 432 | 432 | |
| 433 | 433 | /** |
| 434 | 434 | * Adding `rest_api_init` action for backend of network navigator |
| 435 | 435 | */ |
| 436 | 436 | add_action( 'rest_api_init', function () { |
| 437 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 438 | - 'methods' => 'GET', |
|
| 439 | - 'callback' => 'wl_network_navigator_wp_json', |
|
| 440 | - 'permission_callback' => '__return_true', |
|
| 441 | - ) ); |
|
| 437 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 438 | + 'methods' => 'GET', |
|
| 439 | + 'callback' => 'wl_network_navigator_wp_json', |
|
| 440 | + 'permission_callback' => '__return_true', |
|
| 441 | + ) ); |
|
| 442 | 442 | } ); |
| 443 | 443 | |
| 444 | 444 | /** |
@@ -448,22 +448,22 @@ discard block |
||
| 448 | 448 | */ |
| 449 | 449 | add_action( 'plugins_loaded', function () { |
| 450 | 450 | |
| 451 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 452 | - return; |
|
| 453 | - } |
|
| 451 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 452 | + return; |
|
| 453 | + } |
|
| 454 | 454 | |
| 455 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 456 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 457 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 455 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 456 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 457 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 458 | 458 | }, 0 ); |
| 459 | 459 | |
| 460 | 460 | add_action( 'init', function () { |
| 461 | 461 | |
| 462 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 463 | - return; |
|
| 464 | - } |
|
| 462 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 463 | + return; |
|
| 464 | + } |
|
| 465 | 465 | |
| 466 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 467 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 466 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 467 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 468 | 468 | }, 0 ); |
| 469 | 469 | |
@@ -18,25 +18,25 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | // Create the cache key. |
| 20 | 20 | $cache_key_params = $_REQUEST; |
| 21 | - unset( $cache_key_params['uniqid'] ); |
|
| 22 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 21 | + unset($cache_key_params['uniqid']); |
|
| 22 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 23 | 23 | |
| 24 | 24 | // Create the TTL cache and try to get the results. |
| 25 | - $cache = new Ttl_Cache( "navigator", 24 * 60 * 60 ); // 24 hours. |
|
| 26 | - $cache_results = $cache->get( $cache_key ); |
|
| 25 | + $cache = new Ttl_Cache("navigator", 24 * 60 * 60); // 24 hours. |
|
| 26 | + $cache_results = $cache->get($cache_key); |
|
| 27 | 27 | |
| 28 | - if ( isset( $cache_results ) ) { |
|
| 29 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 28 | + if (isset($cache_results)) { |
|
| 29 | + header('X-WordLift-Cache: HIT'); |
|
| 30 | 30 | |
| 31 | 31 | return $cache_results; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 34 | + header('X-WordLift-Cache: MISS'); |
|
| 35 | 35 | |
| 36 | 36 | $results = _wl_navigator_get_data(); |
| 37 | 37 | |
| 38 | 38 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 39 | - $cache->put( $cache_key, $results ); |
|
| 39 | + $cache->put($cache_key, $results); |
|
| 40 | 40 | |
| 41 | 41 | return $results; |
| 42 | 42 | } |
@@ -51,29 +51,29 @@ discard block |
||
| 51 | 51 | * @since 3.22.6 |
| 52 | 52 | * |
| 53 | 53 | */ |
| 54 | -function wl_network_navigator_wp_json( $request ) { |
|
| 54 | +function wl_network_navigator_wp_json($request) { |
|
| 55 | 55 | |
| 56 | 56 | // Create the cache key. |
| 57 | 57 | $cache_key_params = $_REQUEST; |
| 58 | - unset( $cache_key_params['uniqid'] ); |
|
| 59 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 58 | + unset($cache_key_params['uniqid']); |
|
| 59 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 60 | 60 | |
| 61 | 61 | // Create the TTL cache and try to get the results. |
| 62 | - $cache = new Ttl_Cache( "network-navigator", 24 * 60 * 60 ); // 24 hours. |
|
| 63 | - $cache_results = $cache->get( $cache_key ); |
|
| 62 | + $cache = new Ttl_Cache("network-navigator", 24 * 60 * 60); // 24 hours. |
|
| 63 | + $cache_results = $cache->get($cache_key); |
|
| 64 | 64 | |
| 65 | - if ( isset( $cache_results ) ) { |
|
| 66 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 65 | + if (isset($cache_results)) { |
|
| 66 | + header('X-WordLift-Cache: HIT'); |
|
| 67 | 67 | |
| 68 | 68 | return $cache_results; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 71 | + header('X-WordLift-Cache: MISS'); |
|
| 72 | 72 | |
| 73 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 73 | + $results = _wl_network_navigator_get_data($request); |
|
| 74 | 74 | |
| 75 | 75 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 76 | - $cache->put( $cache_key, $results ); |
|
| 76 | + $cache->put($cache_key, $results); |
|
| 77 | 77 | |
| 78 | 78 | return $results; |
| 79 | 79 | |
@@ -82,46 +82,46 @@ discard block |
||
| 82 | 82 | function _wl_navigator_get_data() { |
| 83 | 83 | |
| 84 | 84 | // Post ID must be defined |
| 85 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 86 | - wp_send_json_error( 'No post_id given' ); |
|
| 85 | + if ( ! isset($_GET['post_id'])) { |
|
| 86 | + wp_send_json_error('No post_id given'); |
|
| 87 | 87 | |
| 88 | 88 | return array(); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | // Limit the results (defaults to 4) |
| 92 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 93 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 94 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 92 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 93 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 94 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 95 | 95 | |
| 96 | 96 | $current_post_id = $_GET['post_id']; |
| 97 | - $current_post = get_post( $current_post_id ); |
|
| 97 | + $current_post = get_post($current_post_id); |
|
| 98 | 98 | |
| 99 | 99 | $navigator_id = $_GET['uniqid']; |
| 100 | 100 | |
| 101 | 101 | // Post ID has to match an existing item |
| 102 | - if ( null === $current_post ) { |
|
| 103 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 102 | + if (null === $current_post) { |
|
| 103 | + wp_send_json_error('No valid post_id given'); |
|
| 104 | 104 | |
| 105 | 105 | return array(); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // Determine navigator type and call respective _get_results |
| 109 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 110 | - $referencing_posts = _wl_entity_navigator_get_results( $current_post_id, array( |
|
| 109 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 110 | + $referencing_posts = _wl_entity_navigator_get_results($current_post_id, array( |
|
| 111 | 111 | 'ID', |
| 112 | 112 | 'post_title', |
| 113 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 113 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 114 | 114 | } else { |
| 115 | - $referencing_posts = _wl_navigator_get_results( $current_post_id, array( |
|
| 115 | + $referencing_posts = _wl_navigator_get_results($current_post_id, array( |
|
| 116 | 116 | 'ID', |
| 117 | 117 | 'post_title', |
| 118 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 118 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | // loop over them and take the first one which is not already in the $related_posts |
| 122 | 122 | $results = array(); |
| 123 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 124 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 123 | + foreach ($referencing_posts as $referencing_post) { |
|
| 124 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | 127 | * Use the thumbnail. |
@@ -131,58 +131,58 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @since 3.19.3 We're using the medium size image. |
| 133 | 133 | */ |
| 134 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 134 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 135 | 135 | |
| 136 | 136 | $result = array( |
| 137 | 137 | 'post' => array( |
| 138 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 138 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 139 | 139 | 'title' => $referencing_post->post_title, |
| 140 | 140 | 'thumbnail' => $thumbnail, |
| 141 | 141 | ), |
| 142 | 142 | 'entity' => array( |
| 143 | 143 | 'label' => $serialized_entity['label'], |
| 144 | 144 | 'mainType' => $serialized_entity['mainType'], |
| 145 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 145 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 146 | 146 | ), |
| 147 | 147 | ); |
| 148 | 148 | |
| 149 | - $result['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 150 | - $result['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 149 | + $result['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 150 | + $result['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 151 | 151 | |
| 152 | 152 | $results[] = $result; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - if ( count( $results ) < $navigator_length ) { |
|
| 156 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 155 | + if (count($results) < $navigator_length) { |
|
| 156 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | return $results; |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | -function _wl_network_navigator_get_data( $request ) { |
|
| 162 | +function _wl_network_navigator_get_data($request) { |
|
| 163 | 163 | |
| 164 | 164 | // Limit the results (defaults to 4) |
| 165 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 166 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 165 | + $navigator_length = isset($request['limit']) ? intval($request['limit']) : 4; |
|
| 166 | + $navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0; |
|
| 167 | 167 | $navigator_id = $request['uniqid']; |
| 168 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 168 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 169 | 169 | |
| 170 | 170 | $entities = $request['entities']; |
| 171 | 171 | |
| 172 | 172 | // Post ID has to match an existing item |
| 173 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 174 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 173 | + if ( ! isset($entities) || empty($entities)) { |
|
| 174 | + wp_send_json_error('No valid entities provided'); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 177 | + $referencing_posts = _wl_network_navigator_get_results($entities, array( |
|
| 178 | 178 | 'ID', |
| 179 | 179 | 'post_title', |
| 180 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 180 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 181 | 181 | |
| 182 | 182 | // loop over them and take the first one which is not already in the $related_posts |
| 183 | 183 | $results = array(); |
| 184 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 185 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 184 | + foreach ($referencing_posts as $referencing_post) { |
|
| 185 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 186 | 186 | |
| 187 | 187 | /** |
| 188 | 188 | * Use the thumbnail. |
@@ -192,30 +192,30 @@ discard block |
||
| 192 | 192 | * |
| 193 | 193 | * @since 3.19.3 We're using the medium size image. |
| 194 | 194 | */ |
| 195 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 195 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 196 | 196 | |
| 197 | 197 | $result = array( |
| 198 | 198 | 'post' => array( |
| 199 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 199 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 200 | 200 | 'title' => $referencing_post->post_title, |
| 201 | 201 | 'thumbnail' => $thumbnail, |
| 202 | 202 | ), |
| 203 | 203 | 'entity' => array( |
| 204 | 204 | 'label' => $serialized_entity['label'], |
| 205 | 205 | 'mainType' => $serialized_entity['mainType'], |
| 206 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 206 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 207 | 207 | ), |
| 208 | 208 | ); |
| 209 | 209 | |
| 210 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 211 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 210 | + $result['post'] = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 211 | + $result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 212 | 212 | |
| 213 | 213 | $results[] = $result; |
| 214 | 214 | |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | - if ( count( $results ) < $navigator_length ) { |
|
| 218 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 217 | + if (count($results) < $navigator_length) { |
|
| 218 | + $results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | return $results; |
@@ -231,17 +231,17 @@ discard block |
||
| 231 | 231 | ) { |
| 232 | 232 | global $wpdb; |
| 233 | 233 | |
| 234 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 234 | + $select = implode(', ', array_map(function($item) { |
|
| 235 | 235 | return "p.$item AS $item"; |
| 236 | - }, (array) $fields ) ); |
|
| 236 | + }, (array) $fields)); |
|
| 237 | 237 | |
| 238 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 238 | + $order_by = implode(', ', array_map(function($item) { |
|
| 239 | 239 | return "p.$item"; |
| 240 | - }, (array) $order_by ) ); |
|
| 240 | + }, (array) $order_by)); |
|
| 241 | 241 | |
| 242 | 242 | /** @noinspection SqlNoDataSourceInspection */ |
| 243 | 243 | return $wpdb->get_results( |
| 244 | - $wpdb->prepare( <<<EOF |
|
| 244 | + $wpdb->prepare(<<<EOF |
|
| 245 | 245 | SELECT %4\$s, p2.ID as entity_id |
| 246 | 246 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 247 | 247 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | LIMIT %2\$d |
| 274 | 274 | OFFSET %3\$d |
| 275 | 275 | EOF |
| 276 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 276 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 277 | 277 | ); |
| 278 | 278 | |
| 279 | 279 | } |
@@ -286,17 +286,17 @@ discard block |
||
| 286 | 286 | ) { |
| 287 | 287 | global $wpdb; |
| 288 | 288 | |
| 289 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 289 | + $select = implode(', ', array_map(function($item) { |
|
| 290 | 290 | return "p.$item AS $item"; |
| 291 | - }, (array) $fields ) ); |
|
| 291 | + }, (array) $fields)); |
|
| 292 | 292 | |
| 293 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 293 | + $order_by = implode(', ', array_map(function($item) { |
|
| 294 | 294 | return "p.$item"; |
| 295 | - }, (array) $order_by ) ); |
|
| 295 | + }, (array) $order_by)); |
|
| 296 | 296 | |
| 297 | 297 | /** @noinspection SqlNoDataSourceInspection */ |
| 298 | 298 | return $wpdb->get_results( |
| 299 | - $wpdb->prepare( <<<EOF |
|
| 299 | + $wpdb->prepare(<<<EOF |
|
| 300 | 300 | SELECT %4\$s, p2.ID as entity_id |
| 301 | 301 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 302 | 302 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | LIMIT %2\$d |
| 326 | 326 | OFFSET %3\$d |
| 327 | 327 | EOF |
| 328 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 328 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 329 | 329 | ); |
| 330 | 330 | } |
| 331 | 331 | |
@@ -337,24 +337,24 @@ discard block |
||
| 337 | 337 | ) { |
| 338 | 338 | global $wpdb; |
| 339 | 339 | |
| 340 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 340 | + $select = implode(', ', array_map(function($item) { |
|
| 341 | 341 | return "p.$item AS $item"; |
| 342 | - }, (array) $fields ) ); |
|
| 342 | + }, (array) $fields)); |
|
| 343 | 343 | |
| 344 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 344 | + $order_by = implode(', ', array_map(function($item) { |
|
| 345 | 345 | return "p.$item"; |
| 346 | - }, (array) $order_by ) ); |
|
| 346 | + }, (array) $order_by)); |
|
| 347 | 347 | |
| 348 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 349 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 350 | - if ( isset( $entity ) ) { |
|
| 348 | + $entities_in = implode(',', array_map(function($item) { |
|
| 349 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item)); |
|
| 350 | + if (isset($entity)) { |
|
| 351 | 351 | return $entity->ID; |
| 352 | 352 | } |
| 353 | - }, $entities ) ); |
|
| 353 | + }, $entities)); |
|
| 354 | 354 | |
| 355 | 355 | /** @noinspection SqlNoDataSourceInspection */ |
| 356 | 356 | return $wpdb->get_results( |
| 357 | - $wpdb->prepare( <<<EOF |
|
| 357 | + $wpdb->prepare(<<<EOF |
|
| 358 | 358 | SELECT %3\$s, p2.ID as entity_id |
| 359 | 359 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 360 | 360 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -380,7 +380,7 @@ discard block |
||
| 380 | 380 | LIMIT %1\$d |
| 381 | 381 | OFFSET %2\$d |
| 382 | 382 | EOF |
| 383 | - , $limit, $offset, $select, $order_by ) |
|
| 383 | + , $limit, $offset, $select, $order_by) |
|
| 384 | 384 | ); |
| 385 | 385 | |
| 386 | 386 | } |
@@ -394,12 +394,12 @@ discard block |
||
| 394 | 394 | |
| 395 | 395 | // Temporary blocking the Navigator. |
| 396 | 396 | $results = wl_shortcode_navigator_data(); |
| 397 | - wl_core_send_json( $results ); |
|
| 397 | + wl_core_send_json($results); |
|
| 398 | 398 | |
| 399 | 399 | } |
| 400 | 400 | |
| 401 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 402 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 401 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 402 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 403 | 403 | |
| 404 | 404 | /** |
| 405 | 405 | * wp-json call for the navigator widget |
@@ -407,13 +407,13 @@ discard block |
||
| 407 | 407 | function wl_shortcode_navigator_wp_json() { |
| 408 | 408 | |
| 409 | 409 | $results = wl_shortcode_navigator_data(); |
| 410 | - if ( ob_get_contents() ) { |
|
| 410 | + if (ob_get_contents()) { |
|
| 411 | 411 | ob_clean(); |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | return array( |
| 415 | 415 | 'items' => array( |
| 416 | - array( 'values' => $results ), |
|
| 416 | + array('values' => $results), |
|
| 417 | 417 | ), |
| 418 | 418 | ); |
| 419 | 419 | |
@@ -422,23 +422,23 @@ discard block |
||
| 422 | 422 | /** |
| 423 | 423 | * Adding `rest_api_init` action for amp backend of navigator |
| 424 | 424 | */ |
| 425 | -add_action( 'rest_api_init', function () { |
|
| 426 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 425 | +add_action('rest_api_init', function() { |
|
| 426 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 427 | 427 | 'methods' => 'GET', |
| 428 | 428 | 'callback' => 'wl_shortcode_navigator_wp_json', |
| 429 | 429 | 'permission_callback' => '__return_true', |
| 430 | - ) ); |
|
| 430 | + )); |
|
| 431 | 431 | } ); |
| 432 | 432 | |
| 433 | 433 | /** |
| 434 | 434 | * Adding `rest_api_init` action for backend of network navigator |
| 435 | 435 | */ |
| 436 | -add_action( 'rest_api_init', function () { |
|
| 437 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 436 | +add_action('rest_api_init', function() { |
|
| 437 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 438 | 438 | 'methods' => 'GET', |
| 439 | 439 | 'callback' => 'wl_network_navigator_wp_json', |
| 440 | 440 | 'permission_callback' => '__return_true', |
| 441 | - ) ); |
|
| 441 | + )); |
|
| 442 | 442 | } ); |
| 443 | 443 | |
| 444 | 444 | /** |
@@ -446,24 +446,24 @@ discard block |
||
| 446 | 446 | * |
| 447 | 447 | * @since 2.2.0 |
| 448 | 448 | */ |
| 449 | -add_action( 'plugins_loaded', function () { |
|
| 449 | +add_action('plugins_loaded', function() { |
|
| 450 | 450 | |
| 451 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 451 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action']) { |
|
| 452 | 452 | return; |
| 453 | 453 | } |
| 454 | 454 | |
| 455 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 456 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 457 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 458 | -}, 0 ); |
|
| 455 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 456 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 457 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 458 | +}, 0); |
|
| 459 | 459 | |
| 460 | -add_action( 'init', function () { |
|
| 460 | +add_action('init', function() { |
|
| 461 | 461 | |
| 462 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 462 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action']) { |
|
| 463 | 463 | return; |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 467 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 468 | -}, 0 ); |
|
| 466 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 467 | + remove_action('init', 'gglcptch_init'); |
|
| 468 | +}, 0); |
|
| 469 | 469 | |
@@ -18,30 +18,30 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | function wl_shortcode_faceted_search( $request ) { |
| 20 | 20 | |
| 21 | - // Create the cache key. |
|
| 22 | - $cache_key = array( |
|
| 23 | - 'request_params' => $_GET, |
|
| 24 | - ); |
|
| 21 | + // Create the cache key. |
|
| 22 | + $cache_key = array( |
|
| 23 | + 'request_params' => $_GET, |
|
| 24 | + ); |
|
| 25 | 25 | |
| 26 | - // Create the TTL cache and try to get the results. |
|
| 27 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 28 | - $cache_results = $cache->get( $cache_key ); |
|
| 26 | + // Create the TTL cache and try to get the results. |
|
| 27 | + $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 28 | + $cache_results = $cache->get( $cache_key ); |
|
| 29 | 29 | |
| 30 | - if ( isset( $cache_results ) ) { |
|
| 31 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 32 | - wl_core_send_json( $cache_results ); |
|
| 30 | + if ( isset( $cache_results ) ) { |
|
| 31 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 32 | + wl_core_send_json( $cache_results ); |
|
| 33 | 33 | |
| 34 | - return; |
|
| 35 | - } |
|
| 34 | + return; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 37 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 38 | 38 | |
| 39 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 39 | + $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 40 | 40 | |
| 41 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 42 | - $cache->put( $cache_key, $results ); |
|
| 41 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 42 | + $cache->put( $cache_key, $results ); |
|
| 43 | 43 | |
| 44 | - wl_core_send_json( $results ); |
|
| 44 | + wl_core_send_json( $results ); |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | |
@@ -52,134 +52,134 @@ discard block |
||
| 52 | 52 | * @since 3.26.0 |
| 53 | 53 | */ |
| 54 | 54 | function wl_shortcode_faceted_search_origin( $request ) { |
| 55 | - // Post ID must be defined. |
|
| 56 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 57 | - wp_die( 'No post_id given' ); |
|
| 58 | - |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 63 | - $current_post = get_post( $current_post_id ); |
|
| 64 | - |
|
| 65 | - // Post ID has to match an existing item. |
|
| 66 | - if ( null === $current_post ) { |
|
| 67 | - wp_die( 'No valid post_id given' ); |
|
| 68 | - |
|
| 69 | - return; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // If the current post is an entity, |
|
| 73 | - // the current post is used as main entity. |
|
| 74 | - // Otherwise, current post related entities are used. |
|
| 75 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 76 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 77 | - array( $current_post->ID ) : |
|
| 78 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 79 | - |
|
| 80 | - // If there are no entities we cannot render the widget. |
|
| 81 | - if ( 0 === count( $entity_ids ) ) { |
|
| 82 | - /** |
|
| 83 | - * If this function is not called from ajax |
|
| 84 | - * then this should not throw an error. |
|
| 85 | - * Note: Used in scripbox longtail project on json endpoint. |
|
| 86 | - */ |
|
| 87 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 88 | - wp_die( 'No entities available' ); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 94 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 99 | - * The ordering should be descending by date on default. |
|
| 100 | - */ |
|
| 101 | - $order_by = 'DESC'; |
|
| 102 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 103 | - $order_by = (string) $_GET['sort']; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 107 | - $entity_ids, |
|
| 108 | - '*', |
|
| 109 | - null, |
|
| 110 | - 'publish', |
|
| 111 | - array( $current_post_id ), |
|
| 112 | - $limit, |
|
| 113 | - null, |
|
| 114 | - $order_by |
|
| 115 | - ); |
|
| 116 | - |
|
| 117 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 118 | - return $p->ID; |
|
| 119 | - }, $referencing_posts ); |
|
| 120 | - |
|
| 121 | - $post_results = array(); |
|
| 122 | - $entity_results = array(); |
|
| 123 | - |
|
| 124 | - // Populate $post_results |
|
| 125 | - |
|
| 126 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 127 | - $referencing_posts : |
|
| 128 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 129 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 130 | - '*', |
|
| 131 | - null, |
|
| 132 | - null, |
|
| 133 | - array(), |
|
| 134 | - null, |
|
| 135 | - $referencing_post_ids |
|
| 136 | - ); |
|
| 137 | - |
|
| 138 | - if ( $filtered_posts ) { |
|
| 139 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 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( $post_obj, 'medium' ); |
|
| 150 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 151 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 152 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 153 | - |
|
| 154 | - $result = $post_obj; |
|
| 155 | - $post_results[] = $result; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Add filler posts if needed |
|
| 160 | - |
|
| 161 | - $filler_count = $limit - count( $post_results ); |
|
| 162 | - $filler_posts = wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ); |
|
| 163 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 164 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 165 | - return $post->ID; |
|
| 166 | - }, $post_results ); |
|
| 167 | - |
|
| 168 | - // Populate $entity_results |
|
| 169 | - |
|
| 170 | - global $wpdb; |
|
| 171 | - |
|
| 172 | - // Retrieve Wordlift relation instances table name. |
|
| 173 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 174 | - |
|
| 175 | - /* |
|
| 55 | + // Post ID must be defined. |
|
| 56 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 57 | + wp_die( 'No post_id given' ); |
|
| 58 | + |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 63 | + $current_post = get_post( $current_post_id ); |
|
| 64 | + |
|
| 65 | + // Post ID has to match an existing item. |
|
| 66 | + if ( null === $current_post ) { |
|
| 67 | + wp_die( 'No valid post_id given' ); |
|
| 68 | + |
|
| 69 | + return; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // If the current post is an entity, |
|
| 73 | + // the current post is used as main entity. |
|
| 74 | + // Otherwise, current post related entities are used. |
|
| 75 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 76 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 77 | + array( $current_post->ID ) : |
|
| 78 | + $entity_service->get_related_entities( $current_post->ID ); |
|
| 79 | + |
|
| 80 | + // If there are no entities we cannot render the widget. |
|
| 81 | + if ( 0 === count( $entity_ids ) ) { |
|
| 82 | + /** |
|
| 83 | + * If this function is not called from ajax |
|
| 84 | + * then this should not throw an error. |
|
| 85 | + * Note: Used in scripbox longtail project on json endpoint. |
|
| 86 | + */ |
|
| 87 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 88 | + wp_die( 'No entities available' ); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 94 | + $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 99 | + * The ordering should be descending by date on default. |
|
| 100 | + */ |
|
| 101 | + $order_by = 'DESC'; |
|
| 102 | + if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 103 | + $order_by = (string) $_GET['sort']; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 107 | + $entity_ids, |
|
| 108 | + '*', |
|
| 109 | + null, |
|
| 110 | + 'publish', |
|
| 111 | + array( $current_post_id ), |
|
| 112 | + $limit, |
|
| 113 | + null, |
|
| 114 | + $order_by |
|
| 115 | + ); |
|
| 116 | + |
|
| 117 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 118 | + return $p->ID; |
|
| 119 | + }, $referencing_posts ); |
|
| 120 | + |
|
| 121 | + $post_results = array(); |
|
| 122 | + $entity_results = array(); |
|
| 123 | + |
|
| 124 | + // Populate $post_results |
|
| 125 | + |
|
| 126 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 127 | + $referencing_posts : |
|
| 128 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 129 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 130 | + '*', |
|
| 131 | + null, |
|
| 132 | + null, |
|
| 133 | + array(), |
|
| 134 | + null, |
|
| 135 | + $referencing_post_ids |
|
| 136 | + ); |
|
| 137 | + |
|
| 138 | + if ( $filtered_posts ) { |
|
| 139 | + foreach ( $filtered_posts as $post_obj ) { |
|
| 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( $post_obj, 'medium' ); |
|
| 150 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 151 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 152 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 153 | + |
|
| 154 | + $result = $post_obj; |
|
| 155 | + $post_results[] = $result; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Add filler posts if needed |
|
| 160 | + |
|
| 161 | + $filler_count = $limit - count( $post_results ); |
|
| 162 | + $filler_posts = wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ); |
|
| 163 | + $post_results = array_merge( $post_results, $filler_posts ); |
|
| 164 | + $referencing_post_ids = array_map( function ( $post ) { |
|
| 165 | + return $post->ID; |
|
| 166 | + }, $post_results ); |
|
| 167 | + |
|
| 168 | + // Populate $entity_results |
|
| 169 | + |
|
| 170 | + global $wpdb; |
|
| 171 | + |
|
| 172 | + // Retrieve Wordlift relation instances table name. |
|
| 173 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 174 | + |
|
| 175 | + /* |
|
| 176 | 176 | * Make sure we have some referenced post, otherwise the IN parts of |
| 177 | 177 | * the SQL will produce an SQL error. |
| 178 | 178 | */ |
| 179 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 180 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 179 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 180 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 181 | 181 | |
| 182 | - $query = " |
|
| 182 | + $query = " |
|
| 183 | 183 | SELECT |
| 184 | 184 | object_id AS ID, |
| 185 | 185 | count( object_id ) AS counter |
@@ -191,116 +191,116 @@ discard block |
||
| 191 | 191 | LIMIT $limit; |
| 192 | 192 | "; |
| 193 | 193 | |
| 194 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 194 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 195 | 195 | |
| 196 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 196 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 197 | 197 | |
| 198 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 198 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 199 | 199 | |
| 200 | - foreach ( $entities as $obj ) { |
|
| 200 | + foreach ( $entities as $obj ) { |
|
| 201 | 201 | |
| 202 | - $entity = get_post( $obj->ID ); |
|
| 202 | + $entity = get_post( $obj->ID ); |
|
| 203 | 203 | |
| 204 | - // Ensure only valid and published entities are returned. |
|
| 205 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 204 | + // Ensure only valid and published entities are returned. |
|
| 205 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 206 | 206 | |
| 207 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 208 | - $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 209 | - $serialized_entity['counter'] = $obj->counter; |
|
| 210 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 211 | - $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 212 | - $obj->ID, |
|
| 213 | - 'ids', |
|
| 214 | - null, |
|
| 215 | - null, |
|
| 216 | - array(), |
|
| 217 | - null, |
|
| 218 | - $referencing_post_ids |
|
| 219 | - ); |
|
| 220 | - $entity_results[] = $serialized_entity; |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - } |
|
| 207 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 208 | + $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 209 | + $serialized_entity['counter'] = $obj->counter; |
|
| 210 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 211 | + $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 212 | + $obj->ID, |
|
| 213 | + 'ids', |
|
| 214 | + null, |
|
| 215 | + null, |
|
| 216 | + array(), |
|
| 217 | + null, |
|
| 218 | + $referencing_post_ids |
|
| 219 | + ); |
|
| 220 | + $entity_results[] = $serialized_entity; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | - return array( |
|
| 226 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 227 | - 'entities' => $entity_results |
|
| 228 | - ); |
|
| 225 | + return array( |
|
| 226 | + 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 227 | + 'entities' => $entity_results |
|
| 228 | + ); |
|
| 229 | 229 | |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
| 233 | 233 | |
| 234 | - $title = get_the_title( $post_id ); |
|
| 234 | + $title = get_the_title( $post_id ); |
|
| 235 | 235 | |
| 236 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 237 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 236 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 237 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 238 | 238 | |
| 239 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 240 | - $title = $alternative_labels[0]; |
|
| 241 | - } |
|
| 242 | - } |
|
| 239 | + if ( count( $alternative_labels ) > 0 ) { |
|
| 240 | + $title = $alternative_labels[0]; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - return remove_accents( $title ); |
|
| 244 | + return remove_accents( $title ); |
|
| 245 | 245 | |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | function wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ) { |
| 249 | 249 | |
| 250 | - $filler_posts = array(); |
|
| 251 | - |
|
| 252 | - // First add latest posts from same categories as the current post |
|
| 253 | - if ( $filler_count > 0 ) { |
|
| 254 | - |
|
| 255 | - $current_post_categories = wp_get_post_categories( $current_post_id ); |
|
| 256 | - |
|
| 257 | - $args = array( |
|
| 258 | - 'meta_query' => array( |
|
| 259 | - array( |
|
| 260 | - 'key' => '_thumbnail_id' |
|
| 261 | - ) |
|
| 262 | - ), |
|
| 263 | - 'category__in' => $current_post_categories, |
|
| 264 | - 'numberposts' => $filler_count, |
|
| 265 | - 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids ), |
|
| 266 | - 'ignore_sticky_posts' => 1 |
|
| 267 | - ); |
|
| 268 | - |
|
| 269 | - $filler_posts = get_posts( $args ); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $filler_count = $filler_count - count( $filler_posts ); |
|
| 273 | - $filler_post_ids = array_map( function ( $post ) { |
|
| 274 | - return $post->ID; |
|
| 275 | - }, $filler_posts ); |
|
| 276 | - |
|
| 277 | - // If that does not fill, add latest posts irrespective of category |
|
| 278 | - if ( $filler_count > 0 ) { |
|
| 279 | - |
|
| 280 | - $args = array( |
|
| 281 | - 'meta_query' => array( |
|
| 282 | - array( |
|
| 283 | - 'key' => '_thumbnail_id' |
|
| 284 | - ) |
|
| 285 | - ), |
|
| 286 | - 'numberposts' => $filler_count, |
|
| 287 | - 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids, $filler_post_ids ), |
|
| 288 | - 'ignore_sticky_posts' => 1 |
|
| 289 | - ); |
|
| 290 | - |
|
| 291 | - $filler_posts = array_merge( $filler_posts, get_posts( $args ) ); |
|
| 292 | - |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - // Add thumbnail and permalink to filler posts |
|
| 296 | - foreach ( $filler_posts as $post_obj ) { |
|
| 297 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 298 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 299 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 300 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - return $filler_posts; |
|
| 250 | + $filler_posts = array(); |
|
| 251 | + |
|
| 252 | + // First add latest posts from same categories as the current post |
|
| 253 | + if ( $filler_count > 0 ) { |
|
| 254 | + |
|
| 255 | + $current_post_categories = wp_get_post_categories( $current_post_id ); |
|
| 256 | + |
|
| 257 | + $args = array( |
|
| 258 | + 'meta_query' => array( |
|
| 259 | + array( |
|
| 260 | + 'key' => '_thumbnail_id' |
|
| 261 | + ) |
|
| 262 | + ), |
|
| 263 | + 'category__in' => $current_post_categories, |
|
| 264 | + 'numberposts' => $filler_count, |
|
| 265 | + 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids ), |
|
| 266 | + 'ignore_sticky_posts' => 1 |
|
| 267 | + ); |
|
| 268 | + |
|
| 269 | + $filler_posts = get_posts( $args ); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $filler_count = $filler_count - count( $filler_posts ); |
|
| 273 | + $filler_post_ids = array_map( function ( $post ) { |
|
| 274 | + return $post->ID; |
|
| 275 | + }, $filler_posts ); |
|
| 276 | + |
|
| 277 | + // If that does not fill, add latest posts irrespective of category |
|
| 278 | + if ( $filler_count > 0 ) { |
|
| 279 | + |
|
| 280 | + $args = array( |
|
| 281 | + 'meta_query' => array( |
|
| 282 | + array( |
|
| 283 | + 'key' => '_thumbnail_id' |
|
| 284 | + ) |
|
| 285 | + ), |
|
| 286 | + 'numberposts' => $filler_count, |
|
| 287 | + 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids, $filler_post_ids ), |
|
| 288 | + 'ignore_sticky_posts' => 1 |
|
| 289 | + ); |
|
| 290 | + |
|
| 291 | + $filler_posts = array_merge( $filler_posts, get_posts( $args ) ); |
|
| 292 | + |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + // Add thumbnail and permalink to filler posts |
|
| 296 | + foreach ( $filler_posts as $post_obj ) { |
|
| 297 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 298 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 299 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 300 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + return $filler_posts; |
|
| 304 | 304 | |
| 305 | 305 | } |
| 306 | 306 | |
@@ -308,9 +308,9 @@ discard block |
||
| 308 | 308 | * Adding `rest_api_init` action for network faceted-search |
| 309 | 309 | */ |
| 310 | 310 | add_action( 'rest_api_init', function () { |
| 311 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 312 | - 'methods' => 'GET', |
|
| 313 | - 'callback' => 'wl_shortcode_faceted_search', |
|
| 314 | - 'permission_callback' => '__return_true', |
|
| 315 | - ) ); |
|
| 311 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 312 | + 'methods' => 'GET', |
|
| 313 | + 'callback' => 'wl_shortcode_faceted_search', |
|
| 314 | + 'permission_callback' => '__return_true', |
|
| 315 | + ) ); |
|
| 316 | 316 | } ); |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @since 3.21.4 fix the cache key by also using the request body. |
| 17 | 17 | * @since 3.21.2 add a caching layer. |
| 18 | 18 | */ |
| 19 | -function wl_shortcode_faceted_search( $request ) { |
|
| 19 | +function wl_shortcode_faceted_search($request) { |
|
| 20 | 20 | |
| 21 | 21 | // Create the cache key. |
| 22 | 22 | $cache_key = array( |
@@ -24,24 +24,24 @@ discard block |
||
| 24 | 24 | ); |
| 25 | 25 | |
| 26 | 26 | // Create the TTL cache and try to get the results. |
| 27 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 28 | - $cache_results = $cache->get( $cache_key ); |
|
| 27 | + $cache = new Ttl_Cache("faceted-search", 8 * 60 * 60); // 8 hours. |
|
| 28 | + $cache_results = $cache->get($cache_key); |
|
| 29 | 29 | |
| 30 | - if ( isset( $cache_results ) ) { |
|
| 31 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 32 | - wl_core_send_json( $cache_results ); |
|
| 30 | + if (isset($cache_results)) { |
|
| 31 | + header('X-WordLift-Cache: HIT'); |
|
| 32 | + wl_core_send_json($cache_results); |
|
| 33 | 33 | |
| 34 | 34 | return; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 37 | + header('X-WordLift-Cache: MISS'); |
|
| 38 | 38 | |
| 39 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 39 | + $results = wl_shortcode_faceted_search_origin($request); |
|
| 40 | 40 | |
| 41 | 41 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 42 | - $cache->put( $cache_key, $results ); |
|
| 42 | + $cache->put($cache_key, $results); |
|
| 43 | 43 | |
| 44 | - wl_core_send_json( $results ); |
|
| 44 | + wl_core_send_json($results); |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | |
@@ -51,20 +51,20 @@ discard block |
||
| 51 | 51 | * @return array $results |
| 52 | 52 | * @since 3.26.0 |
| 53 | 53 | */ |
| 54 | -function wl_shortcode_faceted_search_origin( $request ) { |
|
| 54 | +function wl_shortcode_faceted_search_origin($request) { |
|
| 55 | 55 | // Post ID must be defined. |
| 56 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 57 | - wp_die( 'No post_id given' ); |
|
| 56 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 57 | + wp_die('No post_id given'); |
|
| 58 | 58 | |
| 59 | 59 | return; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 63 | - $current_post = get_post( $current_post_id ); |
|
| 63 | + $current_post = get_post($current_post_id); |
|
| 64 | 64 | |
| 65 | 65 | // Post ID has to match an existing item. |
| 66 | - if ( null === $current_post ) { |
|
| 67 | - wp_die( 'No valid post_id given' ); |
|
| 66 | + if (null === $current_post) { |
|
| 67 | + wp_die('No valid post_id given'); |
|
| 68 | 68 | |
| 69 | 69 | return; |
| 70 | 70 | } |
@@ -73,25 +73,24 @@ discard block |
||
| 73 | 73 | // the current post is used as main entity. |
| 74 | 74 | // Otherwise, current post related entities are used. |
| 75 | 75 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 76 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 77 | - array( $current_post->ID ) : |
|
| 78 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 76 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 77 | + array($current_post->ID) : $entity_service->get_related_entities($current_post->ID); |
|
| 79 | 78 | |
| 80 | 79 | // If there are no entities we cannot render the widget. |
| 81 | - if ( 0 === count( $entity_ids ) ) { |
|
| 80 | + if (0 === count($entity_ids)) { |
|
| 82 | 81 | /** |
| 83 | 82 | * If this function is not called from ajax |
| 84 | 83 | * then this should not throw an error. |
| 85 | 84 | * Note: Used in scripbox longtail project on json endpoint. |
| 86 | 85 | */ |
| 87 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 88 | - wp_die( 'No entities available' ); |
|
| 86 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 87 | + wp_die('No entities available'); |
|
| 89 | 88 | } |
| 90 | 89 | |
| 91 | 90 | } |
| 92 | 91 | |
| 93 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 94 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 92 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 93 | + $amp = (isset($_GET['amp'])) ? true : false; |
|
| 95 | 94 | |
| 96 | 95 | |
| 97 | 96 | /** |
@@ -99,7 +98,7 @@ discard block |
||
| 99 | 98 | * The ordering should be descending by date on default. |
| 100 | 99 | */ |
| 101 | 100 | $order_by = 'DESC'; |
| 102 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 101 | + if (isset($_GET['sort']) && is_string($_GET['sort'])) { |
|
| 103 | 102 | $order_by = (string) $_GET['sort']; |
| 104 | 103 | } |
| 105 | 104 | |
@@ -108,25 +107,24 @@ discard block |
||
| 108 | 107 | '*', |
| 109 | 108 | null, |
| 110 | 109 | 'publish', |
| 111 | - array( $current_post_id ), |
|
| 110 | + array($current_post_id), |
|
| 112 | 111 | $limit, |
| 113 | 112 | null, |
| 114 | 113 | $order_by |
| 115 | 114 | ); |
| 116 | 115 | |
| 117 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 116 | + $referencing_post_ids = array_map(function($p) { |
|
| 118 | 117 | return $p->ID; |
| 119 | - }, $referencing_posts ); |
|
| 118 | + }, $referencing_posts); |
|
| 120 | 119 | |
| 121 | 120 | $post_results = array(); |
| 122 | 121 | $entity_results = array(); |
| 123 | 122 | |
| 124 | 123 | // Populate $post_results |
| 125 | 124 | |
| 126 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 127 | - $referencing_posts : |
|
| 128 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 129 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 125 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 126 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 127 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 130 | 128 | '*', |
| 131 | 129 | null, |
| 132 | 130 | null, |
@@ -135,8 +133,8 @@ discard block |
||
| 135 | 133 | $referencing_post_ids |
| 136 | 134 | ); |
| 137 | 135 | |
| 138 | - if ( $filtered_posts ) { |
|
| 139 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 136 | + if ($filtered_posts) { |
|
| 137 | + foreach ($filtered_posts as $post_obj) { |
|
| 140 | 138 | |
| 141 | 139 | /** |
| 142 | 140 | * Use the thumbnail. |
@@ -146,10 +144,10 @@ discard block |
||
| 146 | 144 | * |
| 147 | 145 | * @since 3.19.3 We're using the medium size image. |
| 148 | 146 | */ |
| 149 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 150 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 147 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 148 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 151 | 149 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 152 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 150 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
| 153 | 151 | |
| 154 | 152 | $result = $post_obj; |
| 155 | 153 | $post_results[] = $result; |
@@ -158,12 +156,12 @@ discard block |
||
| 158 | 156 | |
| 159 | 157 | // Add filler posts if needed |
| 160 | 158 | |
| 161 | - $filler_count = $limit - count( $post_results ); |
|
| 162 | - $filler_posts = wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ); |
|
| 163 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 164 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 159 | + $filler_count = $limit - count($post_results); |
|
| 160 | + $filler_posts = wl_shortcode_faceted_search_filler_posts($filler_count, $current_post_id, $referencing_post_ids); |
|
| 161 | + $post_results = array_merge($post_results, $filler_posts); |
|
| 162 | + $referencing_post_ids = array_map(function($post) { |
|
| 165 | 163 | return $post->ID; |
| 166 | - }, $post_results ); |
|
| 164 | + }, $post_results); |
|
| 167 | 165 | |
| 168 | 166 | // Populate $entity_results |
| 169 | 167 | |
@@ -176,8 +174,8 @@ discard block |
||
| 176 | 174 | * Make sure we have some referenced post, otherwise the IN parts of |
| 177 | 175 | * the SQL will produce an SQL error. |
| 178 | 176 | */ |
| 179 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 180 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 177 | + if ( ! empty($referencing_post_ids)) { |
|
| 178 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 181 | 179 | |
| 182 | 180 | $query = " |
| 183 | 181 | SELECT |
@@ -191,21 +189,21 @@ discard block |
||
| 191 | 189 | LIMIT $limit; |
| 192 | 190 | "; |
| 193 | 191 | |
| 194 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 192 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 195 | 193 | |
| 196 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 194 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 197 | 195 | |
| 198 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 196 | + wl_write_log('Entities found '.count($entities)); |
|
| 199 | 197 | |
| 200 | - foreach ( $entities as $obj ) { |
|
| 198 | + foreach ($entities as $obj) { |
|
| 201 | 199 | |
| 202 | - $entity = get_post( $obj->ID ); |
|
| 200 | + $entity = get_post($obj->ID); |
|
| 203 | 201 | |
| 204 | 202 | // Ensure only valid and published entities are returned. |
| 205 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 203 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 206 | 204 | |
| 207 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 208 | - $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 205 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 206 | + $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title($obj->ID); |
|
| 209 | 207 | $serialized_entity['counter'] = $obj->counter; |
| 210 | 208 | $serialized_entity['createdAt'] = $entity->post_date; |
| 211 | 209 | $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -217,42 +215,42 @@ discard block |
||
| 217 | 215 | null, |
| 218 | 216 | $referencing_post_ids |
| 219 | 217 | ); |
| 220 | - $entity_results[] = $serialized_entity; |
|
| 218 | + $entity_results[] = $serialized_entity; |
|
| 221 | 219 | } |
| 222 | 220 | } |
| 223 | 221 | } |
| 224 | 222 | |
| 225 | 223 | return array( |
| 226 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 224 | + 'posts' => $amp ? array(array('values' => $post_results)) : $post_results, |
|
| 227 | 225 | 'entities' => $entity_results |
| 228 | 226 | ); |
| 229 | 227 | |
| 230 | 228 | } |
| 231 | 229 | |
| 232 | -function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
|
| 230 | +function wl_shortcode_faceted_search_get_the_title($post_id) { |
|
| 233 | 231 | |
| 234 | - $title = get_the_title( $post_id ); |
|
| 232 | + $title = get_the_title($post_id); |
|
| 235 | 233 | |
| 236 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 237 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 234 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
| 235 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
| 238 | 236 | |
| 239 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 237 | + if (count($alternative_labels) > 0) { |
|
| 240 | 238 | $title = $alternative_labels[0]; |
| 241 | 239 | } |
| 242 | 240 | } |
| 243 | 241 | |
| 244 | - return remove_accents( $title ); |
|
| 242 | + return remove_accents($title); |
|
| 245 | 243 | |
| 246 | 244 | } |
| 247 | 245 | |
| 248 | -function wl_shortcode_faceted_search_filler_posts( $filler_count, $current_post_id, $referencing_post_ids ) { |
|
| 246 | +function wl_shortcode_faceted_search_filler_posts($filler_count, $current_post_id, $referencing_post_ids) { |
|
| 249 | 247 | |
| 250 | 248 | $filler_posts = array(); |
| 251 | 249 | |
| 252 | 250 | // First add latest posts from same categories as the current post |
| 253 | - if ( $filler_count > 0 ) { |
|
| 251 | + if ($filler_count > 0) { |
|
| 254 | 252 | |
| 255 | - $current_post_categories = wp_get_post_categories( $current_post_id ); |
|
| 253 | + $current_post_categories = wp_get_post_categories($current_post_id); |
|
| 256 | 254 | |
| 257 | 255 | $args = array( |
| 258 | 256 | 'meta_query' => array( |
@@ -262,20 +260,20 @@ discard block |
||
| 262 | 260 | ), |
| 263 | 261 | 'category__in' => $current_post_categories, |
| 264 | 262 | 'numberposts' => $filler_count, |
| 265 | - 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids ), |
|
| 263 | + 'post__not_in' => array_merge(array($current_post_id), $referencing_post_ids), |
|
| 266 | 264 | 'ignore_sticky_posts' => 1 |
| 267 | 265 | ); |
| 268 | 266 | |
| 269 | - $filler_posts = get_posts( $args ); |
|
| 267 | + $filler_posts = get_posts($args); |
|
| 270 | 268 | } |
| 271 | 269 | |
| 272 | - $filler_count = $filler_count - count( $filler_posts ); |
|
| 273 | - $filler_post_ids = array_map( function ( $post ) { |
|
| 270 | + $filler_count = $filler_count - count($filler_posts); |
|
| 271 | + $filler_post_ids = array_map(function($post) { |
|
| 274 | 272 | return $post->ID; |
| 275 | - }, $filler_posts ); |
|
| 273 | + }, $filler_posts); |
|
| 276 | 274 | |
| 277 | 275 | // If that does not fill, add latest posts irrespective of category |
| 278 | - if ( $filler_count > 0 ) { |
|
| 276 | + if ($filler_count > 0) { |
|
| 279 | 277 | |
| 280 | 278 | $args = array( |
| 281 | 279 | 'meta_query' => array( |
@@ -284,20 +282,20 @@ discard block |
||
| 284 | 282 | ) |
| 285 | 283 | ), |
| 286 | 284 | 'numberposts' => $filler_count, |
| 287 | - 'post__not_in' => array_merge( array( $current_post_id ), $referencing_post_ids, $filler_post_ids ), |
|
| 285 | + 'post__not_in' => array_merge(array($current_post_id), $referencing_post_ids, $filler_post_ids), |
|
| 288 | 286 | 'ignore_sticky_posts' => 1 |
| 289 | 287 | ); |
| 290 | 288 | |
| 291 | - $filler_posts = array_merge( $filler_posts, get_posts( $args ) ); |
|
| 289 | + $filler_posts = array_merge($filler_posts, get_posts($args)); |
|
| 292 | 290 | |
| 293 | 291 | } |
| 294 | 292 | |
| 295 | 293 | // Add thumbnail and permalink to filler posts |
| 296 | - foreach ( $filler_posts as $post_obj ) { |
|
| 297 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 298 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 294 | + foreach ($filler_posts as $post_obj) { |
|
| 295 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 296 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 299 | 297 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 300 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 298 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
| 301 | 299 | } |
| 302 | 300 | |
| 303 | 301 | return $filler_posts; |
@@ -307,10 +305,10 @@ discard block |
||
| 307 | 305 | /** |
| 308 | 306 | * Adding `rest_api_init` action for network faceted-search |
| 309 | 307 | */ |
| 310 | -add_action( 'rest_api_init', function () { |
|
| 311 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 308 | +add_action('rest_api_init', function() { |
|
| 309 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 312 | 310 | 'methods' => 'GET', |
| 313 | 311 | 'callback' => 'wl_shortcode_faceted_search', |
| 314 | 312 | 'permission_callback' => '__return_true', |
| 315 | - ) ); |
|
| 313 | + )); |
|
| 316 | 314 | } ); |
@@ -23,108 +23,108 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Jsonld_By_Id_Endpoint { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The {@link Wordlift_Jsonld_Service} instance. |
|
| 28 | - * |
|
| 29 | - * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $jsonld_service; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var \Wordlift_Entity_Uri_Service |
|
| 35 | - */ |
|
| 36 | - private $entity_uri_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Jsonld_Endpoint constructor. |
|
| 40 | - * |
|
| 41 | - * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 42 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 43 | - */ |
|
| 44 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 45 | - |
|
| 46 | - $this->jsonld_service = $jsonld_service; |
|
| 47 | - $this->entity_uri_service = $entity_uri_service; |
|
| 48 | - |
|
| 49 | - add_action( 'rest_api_init', array( $this, 'register_routes', ) ); |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Get the JSON-LD. |
|
| 55 | - * |
|
| 56 | - * @param \WP_REST_Request $request The incoming {@link \WP_REST_Request}. |
|
| 57 | - * |
|
| 58 | - * @return WP_REST_Response The outgoing {@link WP_REST_Response}. |
|
| 59 | - * @throws \Exception |
|
| 60 | - */ |
|
| 61 | - public function jsonld_by_id( $request ) { |
|
| 62 | - |
|
| 63 | - // Get the ids. |
|
| 64 | - $ids = (array) $request->get_param( 'id' ); |
|
| 65 | - |
|
| 66 | - // Preload the URIs to reduce the number of DB roundtrips. |
|
| 67 | - $this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) ); |
|
| 68 | - |
|
| 69 | - $that = $this; |
|
| 70 | - |
|
| 71 | - // Get the posts, filtering out those not found. |
|
| 72 | - $posts = array_filter( array_map( function ( $item ) use ( $that ) { |
|
| 73 | - return $that->entity_uri_service->get_entity( urldecode( $item ) ); |
|
| 74 | - }, $ids ) ); |
|
| 75 | - |
|
| 76 | - // Get the posts' IDs and make the unique. |
|
| 77 | - $post_ids = array_unique( array_map( function ( $item ) { |
|
| 78 | - return $item->ID; |
|
| 79 | - }, $posts ) ); |
|
| 80 | - |
|
| 81 | - // Get the JSON-LD. |
|
| 82 | - $data = array(); |
|
| 83 | - foreach ( $post_ids as $post_id ) { |
|
| 84 | - $data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // Add the WebSite fragment if requested. |
|
| 88 | - if ( $request->get_param( 'website' ) ) { |
|
| 89 | - $data[] = $this->jsonld_service->get_jsonld( true ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function register_routes() { |
|
| 96 | - |
|
| 97 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld', array( |
|
| 98 | - 'methods' => WP_REST_Server::READABLE, |
|
| 99 | - 'callback' => array( $this, 'jsonld_by_id' ), |
|
| 100 | - 'permission_callback' => '__return_true', |
|
| 101 | - 'args' => array( |
|
| 102 | - 'id' => array( |
|
| 103 | - 'description' => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ), |
|
| 104 | - // We expect an array of strings. |
|
| 105 | - 'type' => 'array', |
|
| 106 | - 'items' => array( |
|
| 107 | - 'type' => 'string' |
|
| 108 | - ), |
|
| 109 | - 'validate_callback' => function ( $values, $request, $param ) { |
|
| 110 | - |
|
| 111 | - if ( ! is_array( $values ) ) { |
|
| 112 | - return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - foreach ( $values as $value ) { |
|
| 116 | - if ( 0 !== strpos( $value, 'http' ) ) { |
|
| 117 | - return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - ), |
|
| 122 | - 'website' => array( |
|
| 123 | - 'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ), |
|
| 124 | - ) |
|
| 125 | - ) |
|
| 126 | - ) ); |
|
| 127 | - |
|
| 128 | - } |
|
| 26 | + /** |
|
| 27 | + * The {@link Wordlift_Jsonld_Service} instance. |
|
| 28 | + * |
|
| 29 | + * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $jsonld_service; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var \Wordlift_Entity_Uri_Service |
|
| 35 | + */ |
|
| 36 | + private $entity_uri_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Jsonld_Endpoint constructor. |
|
| 40 | + * |
|
| 41 | + * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 42 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 43 | + */ |
|
| 44 | + public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 45 | + |
|
| 46 | + $this->jsonld_service = $jsonld_service; |
|
| 47 | + $this->entity_uri_service = $entity_uri_service; |
|
| 48 | + |
|
| 49 | + add_action( 'rest_api_init', array( $this, 'register_routes', ) ); |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Get the JSON-LD. |
|
| 55 | + * |
|
| 56 | + * @param \WP_REST_Request $request The incoming {@link \WP_REST_Request}. |
|
| 57 | + * |
|
| 58 | + * @return WP_REST_Response The outgoing {@link WP_REST_Response}. |
|
| 59 | + * @throws \Exception |
|
| 60 | + */ |
|
| 61 | + public function jsonld_by_id( $request ) { |
|
| 62 | + |
|
| 63 | + // Get the ids. |
|
| 64 | + $ids = (array) $request->get_param( 'id' ); |
|
| 65 | + |
|
| 66 | + // Preload the URIs to reduce the number of DB roundtrips. |
|
| 67 | + $this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) ); |
|
| 68 | + |
|
| 69 | + $that = $this; |
|
| 70 | + |
|
| 71 | + // Get the posts, filtering out those not found. |
|
| 72 | + $posts = array_filter( array_map( function ( $item ) use ( $that ) { |
|
| 73 | + return $that->entity_uri_service->get_entity( urldecode( $item ) ); |
|
| 74 | + }, $ids ) ); |
|
| 75 | + |
|
| 76 | + // Get the posts' IDs and make the unique. |
|
| 77 | + $post_ids = array_unique( array_map( function ( $item ) { |
|
| 78 | + return $item->ID; |
|
| 79 | + }, $posts ) ); |
|
| 80 | + |
|
| 81 | + // Get the JSON-LD. |
|
| 82 | + $data = array(); |
|
| 83 | + foreach ( $post_ids as $post_id ) { |
|
| 84 | + $data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // Add the WebSite fragment if requested. |
|
| 88 | + if ( $request->get_param( 'website' ) ) { |
|
| 89 | + $data[] = $this->jsonld_service->get_jsonld( true ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + return Jsonld_Response_Helper::to_response( $data ); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function register_routes() { |
|
| 96 | + |
|
| 97 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld', array( |
|
| 98 | + 'methods' => WP_REST_Server::READABLE, |
|
| 99 | + 'callback' => array( $this, 'jsonld_by_id' ), |
|
| 100 | + 'permission_callback' => '__return_true', |
|
| 101 | + 'args' => array( |
|
| 102 | + 'id' => array( |
|
| 103 | + 'description' => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ), |
|
| 104 | + // We expect an array of strings. |
|
| 105 | + 'type' => 'array', |
|
| 106 | + 'items' => array( |
|
| 107 | + 'type' => 'string' |
|
| 108 | + ), |
|
| 109 | + 'validate_callback' => function ( $values, $request, $param ) { |
|
| 110 | + |
|
| 111 | + if ( ! is_array( $values ) ) { |
|
| 112 | + return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + foreach ( $values as $value ) { |
|
| 116 | + if ( 0 !== strpos( $value, 'http' ) ) { |
|
| 117 | + return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + ), |
|
| 122 | + 'website' => array( |
|
| 123 | + 'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ), |
|
| 124 | + ) |
|
| 125 | + ) |
|
| 126 | + ) ); |
|
| 127 | + |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | } |
@@ -41,12 +41,12 @@ discard block |
||
| 41 | 41 | * @param \Wordlift_Jsonld_Service $jsonld_service |
| 42 | 42 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
| 43 | 43 | */ |
| 44 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 44 | + public function __construct($jsonld_service, $entity_uri_service) { |
|
| 45 | 45 | |
| 46 | 46 | $this->jsonld_service = $jsonld_service; |
| 47 | 47 | $this->entity_uri_service = $entity_uri_service; |
| 48 | 48 | |
| 49 | - add_action( 'rest_api_init', array( $this, 'register_routes', ) ); |
|
| 49 | + add_action('rest_api_init', array($this, 'register_routes',)); |
|
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | |
@@ -58,72 +58,72 @@ discard block |
||
| 58 | 58 | * @return WP_REST_Response The outgoing {@link WP_REST_Response}. |
| 59 | 59 | * @throws \Exception |
| 60 | 60 | */ |
| 61 | - public function jsonld_by_id( $request ) { |
|
| 61 | + public function jsonld_by_id($request) { |
|
| 62 | 62 | |
| 63 | 63 | // Get the ids. |
| 64 | - $ids = (array) $request->get_param( 'id' ); |
|
| 64 | + $ids = (array) $request->get_param('id'); |
|
| 65 | 65 | |
| 66 | 66 | // Preload the URIs to reduce the number of DB roundtrips. |
| 67 | - $this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) ); |
|
| 67 | + $this->entity_uri_service->preload_uris(array_map('urldecode', $ids)); |
|
| 68 | 68 | |
| 69 | 69 | $that = $this; |
| 70 | 70 | |
| 71 | 71 | // Get the posts, filtering out those not found. |
| 72 | - $posts = array_filter( array_map( function ( $item ) use ( $that ) { |
|
| 73 | - return $that->entity_uri_service->get_entity( urldecode( $item ) ); |
|
| 74 | - }, $ids ) ); |
|
| 72 | + $posts = array_filter(array_map(function($item) use ($that) { |
|
| 73 | + return $that->entity_uri_service->get_entity(urldecode($item)); |
|
| 74 | + }, $ids)); |
|
| 75 | 75 | |
| 76 | 76 | // Get the posts' IDs and make the unique. |
| 77 | - $post_ids = array_unique( array_map( function ( $item ) { |
|
| 77 | + $post_ids = array_unique(array_map(function($item) { |
|
| 78 | 78 | return $item->ID; |
| 79 | - }, $posts ) ); |
|
| 79 | + }, $posts)); |
|
| 80 | 80 | |
| 81 | 81 | // Get the JSON-LD. |
| 82 | 82 | $data = array(); |
| 83 | - foreach ( $post_ids as $post_id ) { |
|
| 84 | - $data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) ); |
|
| 83 | + foreach ($post_ids as $post_id) { |
|
| 84 | + $data = array_merge($data, $that->jsonld_service->get_jsonld(false, $post_id)); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // Add the WebSite fragment if requested. |
| 88 | - if ( $request->get_param( 'website' ) ) { |
|
| 89 | - $data[] = $this->jsonld_service->get_jsonld( true ); |
|
| 88 | + if ($request->get_param('website')) { |
|
| 89 | + $data[] = $this->jsonld_service->get_jsonld(true); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 92 | + return Jsonld_Response_Helper::to_response($data); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | public function register_routes() { |
| 96 | 96 | |
| 97 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld', array( |
|
| 97 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld', array( |
|
| 98 | 98 | 'methods' => WP_REST_Server::READABLE, |
| 99 | - 'callback' => array( $this, 'jsonld_by_id' ), |
|
| 99 | + 'callback' => array($this, 'jsonld_by_id'), |
|
| 100 | 100 | 'permission_callback' => '__return_true', |
| 101 | 101 | 'args' => array( |
| 102 | 102 | 'id' => array( |
| 103 | - 'description' => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ), |
|
| 103 | + 'description' => __('One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift'), |
|
| 104 | 104 | // We expect an array of strings. |
| 105 | 105 | 'type' => 'array', |
| 106 | 106 | 'items' => array( |
| 107 | 107 | 'type' => 'string' |
| 108 | 108 | ), |
| 109 | - 'validate_callback' => function ( $values, $request, $param ) { |
|
| 109 | + 'validate_callback' => function($values, $request, $param) { |
|
| 110 | 110 | |
| 111 | - if ( ! is_array( $values ) ) { |
|
| 112 | - return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 111 | + if ( ! is_array($values)) { |
|
| 112 | + return new WP_Error('rest_invalid_param', esc_html__('The id argument must be an array (try passing `id[]=...`.', 'wordlift'), array('status' => 400)); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - foreach ( $values as $value ) { |
|
| 116 | - if ( 0 !== strpos( $value, 'http' ) ) { |
|
| 117 | - return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 115 | + foreach ($values as $value) { |
|
| 116 | + if (0 !== strpos($value, 'http')) { |
|
| 117 | + return new WP_Error('rest_invalid_param', esc_html__('Ids must start with http.', 'wordlift'), array('status' => 400)); |
|
| 118 | 118 | } |
| 119 | 119 | } |
| 120 | 120 | } |
| 121 | 121 | ), |
| 122 | 122 | 'website' => array( |
| 123 | - 'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ), |
|
| 123 | + 'description' => __('Whether to include the WebSite markup.', 'wordlift'), |
|
| 124 | 124 | ) |
| 125 | 125 | ) |
| 126 | - ) ); |
|
| 126 | + )); |
|
| 127 | 127 | |
| 128 | 128 | } |
| 129 | 129 | |
@@ -20,157 +20,157 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class Jsonld_Endpoint { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The {@link Wordlift_Jsonld_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private $jsonld_service; |
|
| 29 | - /** |
|
| 30 | - * @var \Wordlift_Entity_Uri_Service |
|
| 31 | - */ |
|
| 32 | - private $entity_uri_service; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Jsonld_Endpoint constructor. |
|
| 36 | - * |
|
| 37 | - * @param Jsonld_Service $jsonld_service |
|
| 38 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 39 | - */ |
|
| 40 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 41 | - |
|
| 42 | - $this->jsonld_service = $jsonld_service; |
|
| 43 | - $this->entity_uri_service = $entity_uri_service; |
|
| 44 | - |
|
| 45 | - // PHP 5.3 compatibility. |
|
| 46 | - $that = $this; |
|
| 47 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 48 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 49 | - 'methods' => WP_REST_Server::READABLE, |
|
| 50 | - 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 51 | - 'permission_callback' => '__return_true', |
|
| 52 | - 'args' => array( |
|
| 53 | - 'id' => array( |
|
| 54 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 55 | - return is_numeric( $param ); |
|
| 56 | - }, |
|
| 57 | - 'sanitize_callback' => 'absint', |
|
| 58 | - ), |
|
| 59 | - ) |
|
| 60 | - ) ); |
|
| 61 | - |
|
| 62 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 63 | - 'methods' => 'GET', |
|
| 64 | - 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 65 | - 'permission_callback' => '__return_true', |
|
| 66 | - ) ); |
|
| 67 | - |
|
| 68 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 69 | - 'methods' => 'GET', |
|
| 70 | - 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 71 | - 'permission_callback' => '__return_true', |
|
| 72 | - ) ); |
|
| 73 | - |
|
| 74 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
| 75 | - 'methods' => 'GET', |
|
| 76 | - 'callback' => array( $that, 'jsonld_using_meta' ), |
|
| 77 | - 'permission_callback' => '__return_true', |
|
| 78 | - ) ); |
|
| 79 | - |
|
| 80 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 81 | - 'methods' => 'GET', |
|
| 82 | - 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 83 | - 'permission_callback' => '__return_true', |
|
| 84 | - ) ); |
|
| 85 | - |
|
| 86 | - } ); |
|
| 87 | - |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Callback for the JSON-LD request. |
|
| 92 | - * |
|
| 93 | - * @param array $request { |
|
| 94 | - * The request array. |
|
| 95 | - * |
|
| 96 | - * @type int $id The post id. |
|
| 97 | - * } |
|
| 98 | - * |
|
| 99 | - * @return WP_REST_Response |
|
| 100 | - * @throws \Exception |
|
| 101 | - */ |
|
| 102 | - public function jsonld_using_post_id( $request ) { |
|
| 103 | - |
|
| 104 | - $post_id = $request['id']; |
|
| 105 | - $type = ( 0 === $post_id ) ? Jsonld_Service::TYPE_HOMEPAGE : Jsonld_Service::TYPE_POST; |
|
| 106 | - |
|
| 107 | - // Send the generated JSON-LD. |
|
| 108 | - $data = $this->jsonld_service->get( $type, $post_id ); |
|
| 109 | - |
|
| 110 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Provide a JSON-LD given the itemId. |
|
| 115 | - * |
|
| 116 | - * @param array $request { |
|
| 117 | - * The request array. |
|
| 118 | - * |
|
| 119 | - * @type string $item_id The entity item id. |
|
| 120 | - * } |
|
| 121 | - * |
|
| 122 | - * @return WP_REST_Response |
|
| 123 | - * @throws \Exception |
|
| 124 | - */ |
|
| 125 | - public function jsonld_using_item_id( $request ) { |
|
| 126 | - |
|
| 127 | - $item_id = 'http://' . $request['item_id']; |
|
| 128 | - $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 129 | - |
|
| 130 | - if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 131 | - return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - public function jsonld_using_get_page_by_path( $request ) { |
|
| 138 | - |
|
| 139 | - $post_name = $request['post_name']; |
|
| 140 | - $post_type = $request['post_type']; |
|
| 141 | - |
|
| 142 | - global $wpdb; |
|
| 143 | - |
|
| 144 | - $sql = " |
|
| 23 | + /** |
|
| 24 | + * The {@link Wordlift_Jsonld_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private $jsonld_service; |
|
| 29 | + /** |
|
| 30 | + * @var \Wordlift_Entity_Uri_Service |
|
| 31 | + */ |
|
| 32 | + private $entity_uri_service; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Jsonld_Endpoint constructor. |
|
| 36 | + * |
|
| 37 | + * @param Jsonld_Service $jsonld_service |
|
| 38 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 39 | + */ |
|
| 40 | + public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 41 | + |
|
| 42 | + $this->jsonld_service = $jsonld_service; |
|
| 43 | + $this->entity_uri_service = $entity_uri_service; |
|
| 44 | + |
|
| 45 | + // PHP 5.3 compatibility. |
|
| 46 | + $that = $this; |
|
| 47 | + add_action( 'rest_api_init', function () use ( $that ) { |
|
| 48 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 49 | + 'methods' => WP_REST_Server::READABLE, |
|
| 50 | + 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 51 | + 'permission_callback' => '__return_true', |
|
| 52 | + 'args' => array( |
|
| 53 | + 'id' => array( |
|
| 54 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 55 | + return is_numeric( $param ); |
|
| 56 | + }, |
|
| 57 | + 'sanitize_callback' => 'absint', |
|
| 58 | + ), |
|
| 59 | + ) |
|
| 60 | + ) ); |
|
| 61 | + |
|
| 62 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 63 | + 'methods' => 'GET', |
|
| 64 | + 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 65 | + 'permission_callback' => '__return_true', |
|
| 66 | + ) ); |
|
| 67 | + |
|
| 68 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 69 | + 'methods' => 'GET', |
|
| 70 | + 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 71 | + 'permission_callback' => '__return_true', |
|
| 72 | + ) ); |
|
| 73 | + |
|
| 74 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
| 75 | + 'methods' => 'GET', |
|
| 76 | + 'callback' => array( $that, 'jsonld_using_meta' ), |
|
| 77 | + 'permission_callback' => '__return_true', |
|
| 78 | + ) ); |
|
| 79 | + |
|
| 80 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 81 | + 'methods' => 'GET', |
|
| 82 | + 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 83 | + 'permission_callback' => '__return_true', |
|
| 84 | + ) ); |
|
| 85 | + |
|
| 86 | + } ); |
|
| 87 | + |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Callback for the JSON-LD request. |
|
| 92 | + * |
|
| 93 | + * @param array $request { |
|
| 94 | + * The request array. |
|
| 95 | + * |
|
| 96 | + * @type int $id The post id. |
|
| 97 | + * } |
|
| 98 | + * |
|
| 99 | + * @return WP_REST_Response |
|
| 100 | + * @throws \Exception |
|
| 101 | + */ |
|
| 102 | + public function jsonld_using_post_id( $request ) { |
|
| 103 | + |
|
| 104 | + $post_id = $request['id']; |
|
| 105 | + $type = ( 0 === $post_id ) ? Jsonld_Service::TYPE_HOMEPAGE : Jsonld_Service::TYPE_POST; |
|
| 106 | + |
|
| 107 | + // Send the generated JSON-LD. |
|
| 108 | + $data = $this->jsonld_service->get( $type, $post_id ); |
|
| 109 | + |
|
| 110 | + return Jsonld_Response_Helper::to_response( $data ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Provide a JSON-LD given the itemId. |
|
| 115 | + * |
|
| 116 | + * @param array $request { |
|
| 117 | + * The request array. |
|
| 118 | + * |
|
| 119 | + * @type string $item_id The entity item id. |
|
| 120 | + * } |
|
| 121 | + * |
|
| 122 | + * @return WP_REST_Response |
|
| 123 | + * @throws \Exception |
|
| 124 | + */ |
|
| 125 | + public function jsonld_using_item_id( $request ) { |
|
| 126 | + |
|
| 127 | + $item_id = 'http://' . $request['item_id']; |
|
| 128 | + $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 129 | + |
|
| 130 | + if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 131 | + return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + public function jsonld_using_get_page_by_path( $request ) { |
|
| 138 | + |
|
| 139 | + $post_name = $request['post_name']; |
|
| 140 | + $post_type = $request['post_type']; |
|
| 141 | + |
|
| 142 | + global $wpdb; |
|
| 143 | + |
|
| 144 | + $sql = " |
|
| 145 | 145 | SELECT ID |
| 146 | 146 | FROM $wpdb->posts |
| 147 | 147 | WHERE post_name = %s |
| 148 | 148 | AND post_type = %s |
| 149 | 149 | "; |
| 150 | 150 | |
| 151 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 151 | + $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 152 | 152 | |
| 153 | - if ( is_null( $post_id ) ) { |
|
| 154 | - return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 155 | - } |
|
| 153 | + if ( is_null( $post_id ) ) { |
|
| 154 | + return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 158 | - } |
|
| 157 | + return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * @param WP_REST_Request $request |
|
| 162 | - * |
|
| 163 | - * @return WP_REST_Response |
|
| 164 | - * @throws \Exception |
|
| 165 | - */ |
|
| 166 | - public function jsonld_using_post_meta( $request ) { |
|
| 160 | + /** |
|
| 161 | + * @param WP_REST_Request $request |
|
| 162 | + * |
|
| 163 | + * @return WP_REST_Response |
|
| 164 | + * @throws \Exception |
|
| 165 | + */ |
|
| 166 | + public function jsonld_using_post_meta( $request ) { |
|
| 167 | 167 | |
| 168 | - $meta_key = $request['meta_key']; |
|
| 169 | - $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 168 | + $meta_key = $request['meta_key']; |
|
| 169 | + $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 170 | 170 | |
| 171 | - global $wpdb; |
|
| 171 | + global $wpdb; |
|
| 172 | 172 | |
| 173 | - $sql = " |
|
| 173 | + $sql = " |
|
| 174 | 174 | SELECT post_id AS ID |
| 175 | 175 | FROM $wpdb->postmeta |
| 176 | 176 | WHERE meta_key = %s |
@@ -178,24 +178,24 @@ discard block |
||
| 178 | 178 | LIMIT 1 |
| 179 | 179 | "; |
| 180 | 180 | |
| 181 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 181 | + $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 182 | 182 | |
| 183 | - if ( is_null( $post_id ) ) { |
|
| 184 | - return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 185 | - } |
|
| 183 | + if ( is_null( $post_id ) ) { |
|
| 184 | + return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 188 | - } |
|
| 187 | + return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - public function jsonld_using_meta( $request ) { |
|
| 190 | + public function jsonld_using_meta( $request ) { |
|
| 191 | 191 | |
| 192 | - global $wpdb; |
|
| 192 | + global $wpdb; |
|
| 193 | 193 | |
| 194 | - $meta_key = $request['meta_key']; |
|
| 195 | - $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 194 | + $meta_key = $request['meta_key']; |
|
| 195 | + $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 196 | 196 | |
| 197 | - $results = $wpdb->get_results( $wpdb->prepare( |
|
| 198 | - " |
|
| 197 | + $results = $wpdb->get_results( $wpdb->prepare( |
|
| 198 | + " |
|
| 199 | 199 | SELECT pm.post_id AS id, %s AS type |
| 200 | 200 | FROM {$wpdb->postmeta} pm |
| 201 | 201 | INNER JOIN {$wpdb->posts} p |
@@ -206,23 +206,23 @@ discard block |
||
| 206 | 206 | FROM {$wpdb->termmeta} |
| 207 | 207 | WHERE meta_key = %s AND meta_value = %s |
| 208 | 208 | ", |
| 209 | - Jsonld_Service::TYPE_POST, |
|
| 210 | - $meta_key, |
|
| 211 | - $meta_value, |
|
| 212 | - Jsonld_Service::TYPE_TERM, |
|
| 213 | - $meta_key, |
|
| 214 | - $meta_value |
|
| 215 | - ) ); |
|
| 209 | + Jsonld_Service::TYPE_POST, |
|
| 210 | + $meta_key, |
|
| 211 | + $meta_value, |
|
| 212 | + Jsonld_Service::TYPE_TERM, |
|
| 213 | + $meta_key, |
|
| 214 | + $meta_value |
|
| 215 | + ) ); |
|
| 216 | 216 | |
| 217 | - $jsonld_service = $this->jsonld_service; |
|
| 217 | + $jsonld_service = $this->jsonld_service; |
|
| 218 | 218 | |
| 219 | - $data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) { |
|
| 220 | - $jsonld = $jsonld_service->get( $result->type, $result->id ); |
|
| 219 | + $data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) { |
|
| 220 | + $jsonld = $jsonld_service->get( $result->type, $result->id ); |
|
| 221 | 221 | |
| 222 | - return array_merge( $carry, $jsonld ); |
|
| 223 | - }, array() ); |
|
| 222 | + return array_merge( $carry, $jsonld ); |
|
| 223 | + }, array() ); |
|
| 224 | 224 | |
| 225 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 226 | - } |
|
| 225 | + return Jsonld_Response_Helper::to_response( $data ); |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | 228 | } |
@@ -37,51 +37,51 @@ discard block |
||
| 37 | 37 | * @param Jsonld_Service $jsonld_service |
| 38 | 38 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
| 39 | 39 | */ |
| 40 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 40 | + public function __construct($jsonld_service, $entity_uri_service) { |
|
| 41 | 41 | |
| 42 | 42 | $this->jsonld_service = $jsonld_service; |
| 43 | 43 | $this->entity_uri_service = $entity_uri_service; |
| 44 | 44 | |
| 45 | 45 | // PHP 5.3 compatibility. |
| 46 | 46 | $that = $this; |
| 47 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 48 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 47 | + add_action('rest_api_init', function() use ($that) { |
|
| 48 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 49 | 49 | 'methods' => WP_REST_Server::READABLE, |
| 50 | - 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 50 | + 'callback' => array($that, 'jsonld_using_post_id'), |
|
| 51 | 51 | 'permission_callback' => '__return_true', |
| 52 | 52 | 'args' => array( |
| 53 | 53 | 'id' => array( |
| 54 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 55 | - return is_numeric( $param ); |
|
| 54 | + 'validate_callback' => function($param, $request, $key) { |
|
| 55 | + return is_numeric($param); |
|
| 56 | 56 | }, |
| 57 | 57 | 'sanitize_callback' => 'absint', |
| 58 | 58 | ), |
| 59 | 59 | ) |
| 60 | - ) ); |
|
| 60 | + )); |
|
| 61 | 61 | |
| 62 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 62 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 63 | 63 | 'methods' => 'GET', |
| 64 | - 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 64 | + 'callback' => array($that, 'jsonld_using_item_id'), |
|
| 65 | 65 | 'permission_callback' => '__return_true', |
| 66 | - ) ); |
|
| 66 | + )); |
|
| 67 | 67 | |
| 68 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 68 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 69 | 69 | 'methods' => 'GET', |
| 70 | - 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 70 | + 'callback' => array($that, 'jsonld_using_post_meta'), |
|
| 71 | 71 | 'permission_callback' => '__return_true', |
| 72 | - ) ); |
|
| 72 | + )); |
|
| 73 | 73 | |
| 74 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
| 74 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
| 75 | 75 | 'methods' => 'GET', |
| 76 | - 'callback' => array( $that, 'jsonld_using_meta' ), |
|
| 76 | + 'callback' => array($that, 'jsonld_using_meta'), |
|
| 77 | 77 | 'permission_callback' => '__return_true', |
| 78 | - ) ); |
|
| 78 | + )); |
|
| 79 | 79 | |
| 80 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 80 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 81 | 81 | 'methods' => 'GET', |
| 82 | - 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 82 | + 'callback' => array($that, 'jsonld_using_get_page_by_path'), |
|
| 83 | 83 | 'permission_callback' => '__return_true', |
| 84 | - ) ); |
|
| 84 | + )); |
|
| 85 | 85 | |
| 86 | 86 | } ); |
| 87 | 87 | |
@@ -99,15 +99,15 @@ discard block |
||
| 99 | 99 | * @return WP_REST_Response |
| 100 | 100 | * @throws \Exception |
| 101 | 101 | */ |
| 102 | - public function jsonld_using_post_id( $request ) { |
|
| 102 | + public function jsonld_using_post_id($request) { |
|
| 103 | 103 | |
| 104 | 104 | $post_id = $request['id']; |
| 105 | - $type = ( 0 === $post_id ) ? Jsonld_Service::TYPE_HOMEPAGE : Jsonld_Service::TYPE_POST; |
|
| 105 | + $type = (0 === $post_id) ? Jsonld_Service::TYPE_HOMEPAGE : Jsonld_Service::TYPE_POST; |
|
| 106 | 106 | |
| 107 | 107 | // Send the generated JSON-LD. |
| 108 | - $data = $this->jsonld_service->get( $type, $post_id ); |
|
| 108 | + $data = $this->jsonld_service->get($type, $post_id); |
|
| 109 | 109 | |
| 110 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 110 | + return Jsonld_Response_Helper::to_response($data); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
@@ -122,19 +122,19 @@ discard block |
||
| 122 | 122 | * @return WP_REST_Response |
| 123 | 123 | * @throws \Exception |
| 124 | 124 | */ |
| 125 | - public function jsonld_using_item_id( $request ) { |
|
| 125 | + public function jsonld_using_item_id($request) { |
|
| 126 | 126 | |
| 127 | - $item_id = 'http://' . $request['item_id']; |
|
| 128 | - $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 127 | + $item_id = 'http://'.$request['item_id']; |
|
| 128 | + $post = $this->entity_uri_service->get_entity($item_id); |
|
| 129 | 129 | |
| 130 | - if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 131 | - return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 130 | + if ( ! is_a($post, 'WP_Post')) { |
|
| 131 | + return new WP_REST_Response(esc_html("$item_id not found."), 404, array('Content-Type' => 'text/html')); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 134 | + return $this->jsonld_using_post_id(array('id' => $post->ID,)); |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - public function jsonld_using_get_page_by_path( $request ) { |
|
| 137 | + public function jsonld_using_get_page_by_path($request) { |
|
| 138 | 138 | |
| 139 | 139 | $post_name = $request['post_name']; |
| 140 | 140 | $post_type = $request['post_type']; |
@@ -148,13 +148,13 @@ discard block |
||
| 148 | 148 | AND post_type = %s |
| 149 | 149 | "; |
| 150 | 150 | |
| 151 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 151 | + $post_id = $wpdb->get_var($wpdb->prepare($sql, $post_name, $post_type)); |
|
| 152 | 152 | |
| 153 | - if ( is_null( $post_id ) ) { |
|
| 154 | - return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 153 | + if (is_null($post_id)) { |
|
| 154 | + return new WP_REST_Response(esc_html("$post_name of type $post_type not found."), 404, array('Content-Type' => 'text/html')); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 157 | + return $this->jsonld_using_post_id(array('id' => $post_id,)); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | /** |
@@ -163,10 +163,10 @@ discard block |
||
| 163 | 163 | * @return WP_REST_Response |
| 164 | 164 | * @throws \Exception |
| 165 | 165 | */ |
| 166 | - public function jsonld_using_post_meta( $request ) { |
|
| 166 | + public function jsonld_using_post_meta($request) { |
|
| 167 | 167 | |
| 168 | 168 | $meta_key = $request['meta_key']; |
| 169 | - $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 169 | + $meta_value = urldecode(current($request->get_query_params('meta_value'))); |
|
| 170 | 170 | |
| 171 | 171 | global $wpdb; |
| 172 | 172 | |
@@ -178,23 +178,23 @@ discard block |
||
| 178 | 178 | LIMIT 1 |
| 179 | 179 | "; |
| 180 | 180 | |
| 181 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 181 | + $post_id = $wpdb->get_var($wpdb->prepare($sql, $meta_key, $meta_value)); |
|
| 182 | 182 | |
| 183 | - if ( is_null( $post_id ) ) { |
|
| 184 | - return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 183 | + if (is_null($post_id)) { |
|
| 184 | + return new WP_REST_Response(esc_html("Post with meta key $meta_key and value $meta_value not found."), 404, array('Content-Type' => 'text/html')); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 187 | + return $this->jsonld_using_post_id(array('id' => $post_id,)); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - public function jsonld_using_meta( $request ) { |
|
| 190 | + public function jsonld_using_meta($request) { |
|
| 191 | 191 | |
| 192 | 192 | global $wpdb; |
| 193 | 193 | |
| 194 | 194 | $meta_key = $request['meta_key']; |
| 195 | - $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 195 | + $meta_value = urldecode(current($request->get_query_params('meta_value'))); |
|
| 196 | 196 | |
| 197 | - $results = $wpdb->get_results( $wpdb->prepare( |
|
| 197 | + $results = $wpdb->get_results($wpdb->prepare( |
|
| 198 | 198 | " |
| 199 | 199 | SELECT pm.post_id AS id, %s AS type |
| 200 | 200 | FROM {$wpdb->postmeta} pm |
@@ -212,17 +212,17 @@ discard block |
||
| 212 | 212 | Jsonld_Service::TYPE_TERM, |
| 213 | 213 | $meta_key, |
| 214 | 214 | $meta_value |
| 215 | - ) ); |
|
| 215 | + )); |
|
| 216 | 216 | |
| 217 | 217 | $jsonld_service = $this->jsonld_service; |
| 218 | 218 | |
| 219 | - $data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) { |
|
| 220 | - $jsonld = $jsonld_service->get( $result->type, $result->id ); |
|
| 219 | + $data = array_reduce($results, function($carry, $result) use ($jsonld_service) { |
|
| 220 | + $jsonld = $jsonld_service->get($result->type, $result->id); |
|
| 221 | 221 | |
| 222 | - return array_merge( $carry, $jsonld ); |
|
| 223 | - }, array() ); |
|
| 222 | + return array_merge($carry, $jsonld); |
|
| 223 | + }, array()); |
|
| 224 | 224 | |
| 225 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 225 | + return Jsonld_Response_Helper::to_response($data); |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | } |