@@ -26,6 +26,7 @@ |
||
| 26 | 26 | * Filler_Posts constructor. |
| 27 | 27 | * |
| 28 | 28 | * @param $post_id |
| 29 | + * @param string $alternate_post_type |
|
| 29 | 30 | */ |
| 30 | 31 | public function __construct( $post_id, $alternate_post_type = null ) { |
| 31 | 32 | |
@@ -7,55 +7,55 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | abstract class Filler_Posts { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var int |
|
| 12 | - */ |
|
| 13 | - public $filler_count; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @var array<int> |
|
| 17 | - */ |
|
| 18 | - public $post_ids_to_be_excluded; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var $post_id int |
|
| 22 | - */ |
|
| 23 | - protected $post_id; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Filler_Posts constructor. |
|
| 27 | - * |
|
| 28 | - * @param $post_id |
|
| 29 | - */ |
|
| 30 | - public function __construct( $post_id, $alternate_post_type = null ) { |
|
| 31 | - |
|
| 32 | - $this->post_id = $post_id; |
|
| 33 | - $this->alternate_post_type = $alternate_post_type; |
|
| 34 | - |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - protected function get_posts_config($filler_count, $post_ids_to_be_excluded) { |
|
| 38 | - |
|
| 39 | - return array( |
|
| 40 | - 'meta_query' => array( |
|
| 41 | - array( |
|
| 42 | - 'key' => '_thumbnail_id' |
|
| 43 | - ) |
|
| 44 | - ), |
|
| 45 | - 'numberposts' => $filler_count, |
|
| 46 | - 'post__not_in' => $post_ids_to_be_excluded, |
|
| 47 | - 'ignore_sticky_posts' => 1 |
|
| 48 | - ); |
|
| 49 | - |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param $filler_count |
|
| 54 | - * @param $post_ids_to_be_excluded |
|
| 55 | - * |
|
| 56 | - * @return array<\WP_Post> |
|
| 57 | - */ |
|
| 58 | - abstract function get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 10 | + /** |
|
| 11 | + * @var int |
|
| 12 | + */ |
|
| 13 | + public $filler_count; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @var array<int> |
|
| 17 | + */ |
|
| 18 | + public $post_ids_to_be_excluded; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var $post_id int |
|
| 22 | + */ |
|
| 23 | + protected $post_id; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Filler_Posts constructor. |
|
| 27 | + * |
|
| 28 | + * @param $post_id |
|
| 29 | + */ |
|
| 30 | + public function __construct( $post_id, $alternate_post_type = null ) { |
|
| 31 | + |
|
| 32 | + $this->post_id = $post_id; |
|
| 33 | + $this->alternate_post_type = $alternate_post_type; |
|
| 34 | + |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + protected function get_posts_config($filler_count, $post_ids_to_be_excluded) { |
|
| 38 | + |
|
| 39 | + return array( |
|
| 40 | + 'meta_query' => array( |
|
| 41 | + array( |
|
| 42 | + 'key' => '_thumbnail_id' |
|
| 43 | + ) |
|
| 44 | + ), |
|
| 45 | + 'numberposts' => $filler_count, |
|
| 46 | + 'post__not_in' => $post_ids_to_be_excluded, |
|
| 47 | + 'ignore_sticky_posts' => 1 |
|
| 48 | + ); |
|
| 49 | + |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param $filler_count |
|
| 54 | + * @param $post_ids_to_be_excluded |
|
| 55 | + * |
|
| 56 | + * @return array<\WP_Post> |
|
| 57 | + */ |
|
| 58 | + abstract function get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | 61 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @param $post_id |
| 29 | 29 | */ |
| 30 | - public function __construct( $post_id, $alternate_post_type = null ) { |
|
| 30 | + public function __construct($post_id, $alternate_post_type = null) { |
|
| 31 | 31 | |
| 32 | 32 | $this->post_id = $post_id; |
| 33 | 33 | $this->alternate_post_type = $alternate_post_type; |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | * |
| 56 | 56 | * @return array<\WP_Post> |
| 57 | 57 | */ |
| 58 | - abstract function get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 58 | + abstract function get_posts($filler_count, $post_ids_to_be_excluded); |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | 61 | } |
@@ -4,208 +4,208 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | class Wordlift_Products_Navigator_Shortcode_REST extends Wordlift_Shortcode_REST { |
| 6 | 6 | |
| 7 | - const CACHE_TTL = 3600; // 1 hour |
|
| 8 | - |
|
| 9 | - public function __construct() { |
|
| 10 | - parent::__construct( |
|
| 11 | - '/products-navigator', |
|
| 12 | - array( |
|
| 13 | - 'post_id' => array( |
|
| 14 | - 'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ), |
|
| 15 | - 'type' => 'integer', |
|
| 16 | - 'required' => true |
|
| 17 | - ), |
|
| 18 | - 'uniqid' => array( |
|
| 19 | - 'description' => __( 'Navigator uniqueid', 'wordlift' ), |
|
| 20 | - 'type' => 'string', |
|
| 21 | - 'required' => true |
|
| 22 | - ), |
|
| 23 | - 'limit' => array( |
|
| 24 | - 'default' => 4, |
|
| 25 | - 'type' => 'integer', |
|
| 26 | - 'sanitize_callback' => 'absint' |
|
| 27 | - ), |
|
| 28 | - 'offset' => array( |
|
| 29 | - 'default' => 0, |
|
| 30 | - 'type' => 'integer', |
|
| 31 | - 'sanitize_callback' => 'absint' |
|
| 32 | - ), |
|
| 33 | - 'sort' => array( |
|
| 34 | - 'default' => 'ID DESC', |
|
| 35 | - 'sanitize_callback' => 'sanitize_sql_orderby' |
|
| 36 | - ), |
|
| 37 | - 'amp' => array( |
|
| 38 | - 'sanitize_callback' => 'rest_sanitize_boolean' |
|
| 39 | - ) |
|
| 40 | - ) |
|
| 41 | - ); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - public function get_data( $request ) { |
|
| 45 | - |
|
| 46 | - // Sanitize and set defaults |
|
| 47 | - $navigator_length = $request['limit']; |
|
| 48 | - $navigator_offset = $request['offset']; |
|
| 49 | - $order_by = $request['sort']; |
|
| 50 | - $post_id = $request['post_id']; |
|
| 51 | - $navigator_id = $request['uniqid']; |
|
| 52 | - $amp = $request['amp']; |
|
| 53 | - |
|
| 54 | - $post = get_post( $post_id ); |
|
| 55 | - |
|
| 56 | - // Post ID has to match an existing item |
|
| 57 | - if ( null === $post ) { |
|
| 58 | - return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // Determine navigator type and call respective get_*_results |
|
| 62 | - if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 63 | - $referencing_posts = $this->get_entity_results( $post_id, array( |
|
| 64 | - 'ID', |
|
| 65 | - 'post_title', |
|
| 66 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 67 | - } else { |
|
| 68 | - $referencing_posts = $this->get_post_results( $post_id, array( |
|
| 69 | - 'ID', |
|
| 70 | - 'post_title', |
|
| 71 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - // Fetch directly referencing posts excluding referencing posts via entities |
|
| 75 | - $directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) { |
|
| 76 | - return $referencing_post->ID; |
|
| 77 | - }, $referencing_posts ) ); |
|
| 78 | - |
|
| 79 | - // Combine directly referencing posts and referencing posts via entities |
|
| 80 | - $referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts ); |
|
| 81 | - |
|
| 82 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 83 | - $results = array(); |
|
| 84 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 85 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 86 | - $product = wc_get_product( $referencing_post->ID ); |
|
| 87 | - |
|
| 88 | - $result = array( |
|
| 89 | - 'product' => array( |
|
| 90 | - 'id' => $referencing_post->ID, |
|
| 91 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 92 | - 'title' => $referencing_post->post_title, |
|
| 93 | - 'thumbnail' => get_the_post_thumbnail_url( $referencing_post, 'medium' ), |
|
| 94 | - 'regular_price' => $product->get_regular_price(), |
|
| 95 | - 'sale_price' => $product->get_sale_price(), |
|
| 96 | - 'price' => $product->get_price(), |
|
| 97 | - 'currency_symbol' => get_woocommerce_currency_symbol(), |
|
| 98 | - 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 99 | - 'average_rating' => $product->get_average_rating(), |
|
| 100 | - 'rating_count' => $product->get_rating_count(), |
|
| 101 | - 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 102 | - ), |
|
| 103 | - 'entity' => array( |
|
| 104 | - 'id' => $referencing_post->entity_id, |
|
| 105 | - 'label' => $serialized_entity['label'], |
|
| 106 | - 'mainType' => $serialized_entity['mainType'], |
|
| 107 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 108 | - ), |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - $results[] = $result; |
|
| 112 | - |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - if ( count( $results ) < $navigator_length ) { |
|
| 116 | - $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - // Add filler posts if needed |
|
| 121 | - $filler_count = $navigator_length - count( $results ); |
|
| 122 | - if ( $filler_count > 0 ) { |
|
| 123 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 124 | - return $p->ID; |
|
| 125 | - }, $referencing_posts ); |
|
| 126 | - /** |
|
| 127 | - * @since 3.28.0 |
|
| 128 | - * Filler posts are fetched using this util. |
|
| 129 | - */ |
|
| 130 | - $filler_posts_util = new Filler_Posts_Util( $post_id, 'product' ); |
|
| 131 | - $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 132 | - $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 133 | - $results = array_merge( $results, $filler_posts ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Apply filters after fillers are added |
|
| 137 | - foreach ( $results as $result_index => $result ) { |
|
| 138 | - $results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id ); |
|
| 139 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id ); |
|
| 143 | - |
|
| 144 | - return $amp ? array( |
|
| 145 | - 'items' => array( |
|
| 146 | - array( 'values' => $results ), |
|
| 147 | - ), |
|
| 148 | - ) : $results; |
|
| 149 | - |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 153 | - |
|
| 154 | - $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 155 | - |
|
| 156 | - $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 157 | - |
|
| 158 | - $directly_referencing_posts = get_posts( array( |
|
| 159 | - 'meta_query' => array( |
|
| 160 | - array( |
|
| 161 | - 'key' => '_thumbnail_id' |
|
| 162 | - ), |
|
| 163 | - array( |
|
| 164 | - 'key' => '_stock_status', |
|
| 165 | - 'value' => 'instock' |
|
| 166 | - ) |
|
| 167 | - ), |
|
| 168 | - 'post__in' => $post__in, |
|
| 169 | - 'post_type' => 'product', |
|
| 170 | - 'ignore_sticky_posts' => 1 |
|
| 171 | - ) ); |
|
| 172 | - |
|
| 173 | - $results = array(); |
|
| 174 | - |
|
| 175 | - foreach ( $directly_referencing_posts as $post ) { |
|
| 176 | - $result = new stdClass(); |
|
| 177 | - $result->ID = $post->ID; |
|
| 178 | - $result->post_title = $post->post_title; |
|
| 179 | - $result->entity_id = $post->ID; |
|
| 180 | - $results[] = $result; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return $results; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - private function get_entity_results( |
|
| 187 | - $post_id, |
|
| 188 | - $fields = array( |
|
| 189 | - 'ID', |
|
| 190 | - 'post_title', |
|
| 191 | - ), |
|
| 192 | - $order_by = 'ID DESC', |
|
| 193 | - $limit = 10, |
|
| 194 | - $offset = 0 |
|
| 195 | - ) { |
|
| 196 | - global $wpdb; |
|
| 197 | - |
|
| 198 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 199 | - return "p.$item AS $item"; |
|
| 200 | - }, (array) $fields ) ); |
|
| 201 | - |
|
| 202 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 203 | - return "p.$item"; |
|
| 204 | - }, (array) $order_by ) ); |
|
| 205 | - |
|
| 206 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 207 | - return $wpdb->get_results( |
|
| 208 | - $wpdb->prepare( <<<EOF |
|
| 7 | + const CACHE_TTL = 3600; // 1 hour |
|
| 8 | + |
|
| 9 | + public function __construct() { |
|
| 10 | + parent::__construct( |
|
| 11 | + '/products-navigator', |
|
| 12 | + array( |
|
| 13 | + 'post_id' => array( |
|
| 14 | + 'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ), |
|
| 15 | + 'type' => 'integer', |
|
| 16 | + 'required' => true |
|
| 17 | + ), |
|
| 18 | + 'uniqid' => array( |
|
| 19 | + 'description' => __( 'Navigator uniqueid', 'wordlift' ), |
|
| 20 | + 'type' => 'string', |
|
| 21 | + 'required' => true |
|
| 22 | + ), |
|
| 23 | + 'limit' => array( |
|
| 24 | + 'default' => 4, |
|
| 25 | + 'type' => 'integer', |
|
| 26 | + 'sanitize_callback' => 'absint' |
|
| 27 | + ), |
|
| 28 | + 'offset' => array( |
|
| 29 | + 'default' => 0, |
|
| 30 | + 'type' => 'integer', |
|
| 31 | + 'sanitize_callback' => 'absint' |
|
| 32 | + ), |
|
| 33 | + 'sort' => array( |
|
| 34 | + 'default' => 'ID DESC', |
|
| 35 | + 'sanitize_callback' => 'sanitize_sql_orderby' |
|
| 36 | + ), |
|
| 37 | + 'amp' => array( |
|
| 38 | + 'sanitize_callback' => 'rest_sanitize_boolean' |
|
| 39 | + ) |
|
| 40 | + ) |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + public function get_data( $request ) { |
|
| 45 | + |
|
| 46 | + // Sanitize and set defaults |
|
| 47 | + $navigator_length = $request['limit']; |
|
| 48 | + $navigator_offset = $request['offset']; |
|
| 49 | + $order_by = $request['sort']; |
|
| 50 | + $post_id = $request['post_id']; |
|
| 51 | + $navigator_id = $request['uniqid']; |
|
| 52 | + $amp = $request['amp']; |
|
| 53 | + |
|
| 54 | + $post = get_post( $post_id ); |
|
| 55 | + |
|
| 56 | + // Post ID has to match an existing item |
|
| 57 | + if ( null === $post ) { |
|
| 58 | + return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // Determine navigator type and call respective get_*_results |
|
| 62 | + if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 63 | + $referencing_posts = $this->get_entity_results( $post_id, array( |
|
| 64 | + 'ID', |
|
| 65 | + 'post_title', |
|
| 66 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 67 | + } else { |
|
| 68 | + $referencing_posts = $this->get_post_results( $post_id, array( |
|
| 69 | + 'ID', |
|
| 70 | + 'post_title', |
|
| 71 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + // Fetch directly referencing posts excluding referencing posts via entities |
|
| 75 | + $directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) { |
|
| 76 | + return $referencing_post->ID; |
|
| 77 | + }, $referencing_posts ) ); |
|
| 78 | + |
|
| 79 | + // Combine directly referencing posts and referencing posts via entities |
|
| 80 | + $referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts ); |
|
| 81 | + |
|
| 82 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 83 | + $results = array(); |
|
| 84 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 85 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 86 | + $product = wc_get_product( $referencing_post->ID ); |
|
| 87 | + |
|
| 88 | + $result = array( |
|
| 89 | + 'product' => array( |
|
| 90 | + 'id' => $referencing_post->ID, |
|
| 91 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 92 | + 'title' => $referencing_post->post_title, |
|
| 93 | + 'thumbnail' => get_the_post_thumbnail_url( $referencing_post, 'medium' ), |
|
| 94 | + 'regular_price' => $product->get_regular_price(), |
|
| 95 | + 'sale_price' => $product->get_sale_price(), |
|
| 96 | + 'price' => $product->get_price(), |
|
| 97 | + 'currency_symbol' => get_woocommerce_currency_symbol(), |
|
| 98 | + 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 99 | + 'average_rating' => $product->get_average_rating(), |
|
| 100 | + 'rating_count' => $product->get_rating_count(), |
|
| 101 | + 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 102 | + ), |
|
| 103 | + 'entity' => array( |
|
| 104 | + 'id' => $referencing_post->entity_id, |
|
| 105 | + 'label' => $serialized_entity['label'], |
|
| 106 | + 'mainType' => $serialized_entity['mainType'], |
|
| 107 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 108 | + ), |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + $results[] = $result; |
|
| 112 | + |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + if ( count( $results ) < $navigator_length ) { |
|
| 116 | + $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + // Add filler posts if needed |
|
| 121 | + $filler_count = $navigator_length - count( $results ); |
|
| 122 | + if ( $filler_count > 0 ) { |
|
| 123 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 124 | + return $p->ID; |
|
| 125 | + }, $referencing_posts ); |
|
| 126 | + /** |
|
| 127 | + * @since 3.28.0 |
|
| 128 | + * Filler posts are fetched using this util. |
|
| 129 | + */ |
|
| 130 | + $filler_posts_util = new Filler_Posts_Util( $post_id, 'product' ); |
|
| 131 | + $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 132 | + $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 133 | + $results = array_merge( $results, $filler_posts ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Apply filters after fillers are added |
|
| 137 | + foreach ( $results as $result_index => $result ) { |
|
| 138 | + $results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id ); |
|
| 139 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id ); |
|
| 143 | + |
|
| 144 | + return $amp ? array( |
|
| 145 | + 'items' => array( |
|
| 146 | + array( 'values' => $results ), |
|
| 147 | + ), |
|
| 148 | + ) : $results; |
|
| 149 | + |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 153 | + |
|
| 154 | + $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 155 | + |
|
| 156 | + $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 157 | + |
|
| 158 | + $directly_referencing_posts = get_posts( array( |
|
| 159 | + 'meta_query' => array( |
|
| 160 | + array( |
|
| 161 | + 'key' => '_thumbnail_id' |
|
| 162 | + ), |
|
| 163 | + array( |
|
| 164 | + 'key' => '_stock_status', |
|
| 165 | + 'value' => 'instock' |
|
| 166 | + ) |
|
| 167 | + ), |
|
| 168 | + 'post__in' => $post__in, |
|
| 169 | + 'post_type' => 'product', |
|
| 170 | + 'ignore_sticky_posts' => 1 |
|
| 171 | + ) ); |
|
| 172 | + |
|
| 173 | + $results = array(); |
|
| 174 | + |
|
| 175 | + foreach ( $directly_referencing_posts as $post ) { |
|
| 176 | + $result = new stdClass(); |
|
| 177 | + $result->ID = $post->ID; |
|
| 178 | + $result->post_title = $post->post_title; |
|
| 179 | + $result->entity_id = $post->ID; |
|
| 180 | + $results[] = $result; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return $results; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + private function get_entity_results( |
|
| 187 | + $post_id, |
|
| 188 | + $fields = array( |
|
| 189 | + 'ID', |
|
| 190 | + 'post_title', |
|
| 191 | + ), |
|
| 192 | + $order_by = 'ID DESC', |
|
| 193 | + $limit = 10, |
|
| 194 | + $offset = 0 |
|
| 195 | + ) { |
|
| 196 | + global $wpdb; |
|
| 197 | + |
|
| 198 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 199 | + return "p.$item AS $item"; |
|
| 200 | + }, (array) $fields ) ); |
|
| 201 | + |
|
| 202 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 203 | + return "p.$item"; |
|
| 204 | + }, (array) $order_by ) ); |
|
| 205 | + |
|
| 206 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 207 | + return $wpdb->get_results( |
|
| 208 | + $wpdb->prepare( <<<EOF |
|
| 209 | 209 | SELECT %4\$s, p2.ID as entity_id |
| 210 | 210 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 211 | 211 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -238,33 +238,33 @@ discard block |
||
| 238 | 238 | LIMIT %2\$d |
| 239 | 239 | OFFSET %3\$d |
| 240 | 240 | EOF |
| 241 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 242 | - ); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - private function get_post_results( |
|
| 246 | - $post_id, |
|
| 247 | - $fields = array( |
|
| 248 | - 'ID', |
|
| 249 | - 'post_title', |
|
| 250 | - ), |
|
| 251 | - $order_by = 'ID DESC', |
|
| 252 | - $limit = 10, |
|
| 253 | - $offset = 0 |
|
| 254 | - ) { |
|
| 255 | - global $wpdb; |
|
| 256 | - |
|
| 257 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 258 | - return "p.$item AS $item"; |
|
| 259 | - }, (array) $fields ) ); |
|
| 260 | - |
|
| 261 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 262 | - return "p.$item"; |
|
| 263 | - }, (array) $order_by ) ); |
|
| 264 | - |
|
| 265 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 266 | - return $wpdb->get_results( |
|
| 267 | - $wpdb->prepare( <<<EOF |
|
| 241 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + private function get_post_results( |
|
| 246 | + $post_id, |
|
| 247 | + $fields = array( |
|
| 248 | + 'ID', |
|
| 249 | + 'post_title', |
|
| 250 | + ), |
|
| 251 | + $order_by = 'ID DESC', |
|
| 252 | + $limit = 10, |
|
| 253 | + $offset = 0 |
|
| 254 | + ) { |
|
| 255 | + global $wpdb; |
|
| 256 | + |
|
| 257 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 258 | + return "p.$item AS $item"; |
|
| 259 | + }, (array) $fields ) ); |
|
| 260 | + |
|
| 261 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 262 | + return "p.$item"; |
|
| 263 | + }, (array) $order_by ) ); |
|
| 264 | + |
|
| 265 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 266 | + return $wpdb->get_results( |
|
| 267 | + $wpdb->prepare( <<<EOF |
|
| 268 | 268 | SELECT %4\$s, p2.ID as entity_id |
| 269 | 269 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 270 | 270 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -300,8 +300,8 @@ discard block |
||
| 300 | 300 | LIMIT %2\$d |
| 301 | 301 | OFFSET %3\$d |
| 302 | 302 | EOF |
| 303 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 304 | - ); |
|
| 305 | - } |
|
| 303 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 304 | + ); |
|
| 305 | + } |
|
| 306 | 306 | |
| 307 | 307 | } |
@@ -11,12 +11,12 @@ discard block |
||
| 11 | 11 | '/products-navigator', |
| 12 | 12 | array( |
| 13 | 13 | 'post_id' => array( |
| 14 | - 'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ), |
|
| 14 | + 'description' => __('Post ID for which Navigator has to be queried', 'wordlift'), |
|
| 15 | 15 | 'type' => 'integer', |
| 16 | 16 | 'required' => true |
| 17 | 17 | ), |
| 18 | 18 | 'uniqid' => array( |
| 19 | - 'description' => __( 'Navigator uniqueid', 'wordlift' ), |
|
| 19 | + 'description' => __('Navigator uniqueid', 'wordlift'), |
|
| 20 | 20 | 'type' => 'string', |
| 21 | 21 | 'required' => true |
| 22 | 22 | ), |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | ); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - public function get_data( $request ) { |
|
| 44 | + public function get_data($request) { |
|
| 45 | 45 | |
| 46 | 46 | // Sanitize and set defaults |
| 47 | 47 | $navigator_length = $request['limit']; |
@@ -51,60 +51,60 @@ discard block |
||
| 51 | 51 | $navigator_id = $request['uniqid']; |
| 52 | 52 | $amp = $request['amp']; |
| 53 | 53 | |
| 54 | - $post = get_post( $post_id ); |
|
| 54 | + $post = get_post($post_id); |
|
| 55 | 55 | |
| 56 | 56 | // Post ID has to match an existing item |
| 57 | - if ( null === $post ) { |
|
| 58 | - return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) ); |
|
| 57 | + if (null === $post) { |
|
| 58 | + return new WP_Error('rest_invalid_post_id', __('Invalid post_id', 'wordlift'), array('status' => 404)); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | // Determine navigator type and call respective get_*_results |
| 62 | - if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 63 | - $referencing_posts = $this->get_entity_results( $post_id, array( |
|
| 62 | + if (get_post_type($post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 63 | + $referencing_posts = $this->get_entity_results($post_id, array( |
|
| 64 | 64 | 'ID', |
| 65 | 65 | 'post_title', |
| 66 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 66 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 67 | 67 | } else { |
| 68 | - $referencing_posts = $this->get_post_results( $post_id, array( |
|
| 68 | + $referencing_posts = $this->get_post_results($post_id, array( |
|
| 69 | 69 | 'ID', |
| 70 | 70 | 'post_title', |
| 71 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 71 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | // Fetch directly referencing posts excluding referencing posts via entities |
| 75 | - $directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) { |
|
| 75 | + $directly_referencing_posts = $this->get_directly_referencing_posts($post_id, array_map(function($referencing_post) { |
|
| 76 | 76 | return $referencing_post->ID; |
| 77 | - }, $referencing_posts ) ); |
|
| 77 | + }, $referencing_posts)); |
|
| 78 | 78 | |
| 79 | 79 | // Combine directly referencing posts and referencing posts via entities |
| 80 | - $referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts ); |
|
| 80 | + $referencing_posts = array_merge($directly_referencing_posts, $referencing_posts); |
|
| 81 | 81 | |
| 82 | 82 | // loop over them and take the first one which is not already in the $related_posts |
| 83 | 83 | $results = array(); |
| 84 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 85 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 86 | - $product = wc_get_product( $referencing_post->ID ); |
|
| 84 | + foreach ($referencing_posts as $referencing_post) { |
|
| 85 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 86 | + $product = wc_get_product($referencing_post->ID); |
|
| 87 | 87 | |
| 88 | 88 | $result = array( |
| 89 | 89 | 'product' => array( |
| 90 | 90 | 'id' => $referencing_post->ID, |
| 91 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 91 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 92 | 92 | 'title' => $referencing_post->post_title, |
| 93 | - 'thumbnail' => get_the_post_thumbnail_url( $referencing_post, 'medium' ), |
|
| 93 | + 'thumbnail' => get_the_post_thumbnail_url($referencing_post, 'medium'), |
|
| 94 | 94 | 'regular_price' => $product->get_regular_price(), |
| 95 | 95 | 'sale_price' => $product->get_sale_price(), |
| 96 | 96 | 'price' => $product->get_price(), |
| 97 | 97 | 'currency_symbol' => get_woocommerce_currency_symbol(), |
| 98 | - 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 98 | + 'discount_pc' => ($product->get_sale_price() && ($product->get_regular_price() > 0)) ? round(1 - ($product->get_sale_price() / $product->get_regular_price()), 2) * 100 : 0, |
|
| 99 | 99 | 'average_rating' => $product->get_average_rating(), |
| 100 | 100 | 'rating_count' => $product->get_rating_count(), |
| 101 | - 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 101 | + 'rating_html' => wc_get_rating_html($product->get_average_rating(), $product->get_rating_count()) |
|
| 102 | 102 | ), |
| 103 | 103 | 'entity' => array( |
| 104 | 104 | 'id' => $referencing_post->entity_id, |
| 105 | 105 | 'label' => $serialized_entity['label'], |
| 106 | 106 | 'mainType' => $serialized_entity['mainType'], |
| 107 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 107 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 108 | 108 | ), |
| 109 | 109 | ); |
| 110 | 110 | |
@@ -112,50 +112,50 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - if ( count( $results ) < $navigator_length ) { |
|
| 116 | - $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 115 | + if (count($results) < $navigator_length) { |
|
| 116 | + $results = apply_filters('wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | |
| 120 | 120 | // Add filler posts if needed |
| 121 | - $filler_count = $navigator_length - count( $results ); |
|
| 122 | - if ( $filler_count > 0 ) { |
|
| 123 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 121 | + $filler_count = $navigator_length - count($results); |
|
| 122 | + if ($filler_count > 0) { |
|
| 123 | + $referencing_post_ids = array_map(function($p) { |
|
| 124 | 124 | return $p->ID; |
| 125 | - }, $referencing_posts ); |
|
| 125 | + }, $referencing_posts); |
|
| 126 | 126 | /** |
| 127 | 127 | * @since 3.28.0 |
| 128 | 128 | * Filler posts are fetched using this util. |
| 129 | 129 | */ |
| 130 | - $filler_posts_util = new Filler_Posts_Util( $post_id, 'product' ); |
|
| 131 | - $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 132 | - $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 133 | - $results = array_merge( $results, $filler_posts ); |
|
| 130 | + $filler_posts_util = new Filler_Posts_Util($post_id, 'product'); |
|
| 131 | + $post_ids_to_be_excluded = array_merge(array($post_id), $referencing_post_ids); |
|
| 132 | + $filler_posts = $filler_posts_util->get_product_navigator_response($filler_count, $post_ids_to_be_excluded); |
|
| 133 | + $results = array_merge($results, $filler_posts); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | // Apply filters after fillers are added |
| 137 | - foreach ( $results as $result_index => $result ) { |
|
| 138 | - $results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id ); |
|
| 139 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 137 | + foreach ($results as $result_index => $result) { |
|
| 138 | + $results[$result_index]['product'] = apply_filters('wl_products_navigator_data_post', $result['product'], intval($result['product']['id']), $navigator_id); |
|
| 139 | + $results[$result_index]['entity'] = apply_filters('wl_products_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - $results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id ); |
|
| 142 | + $results = apply_filters('wl_products_navigator_results', $results, $navigator_id); |
|
| 143 | 143 | |
| 144 | 144 | return $amp ? array( |
| 145 | 145 | 'items' => array( |
| 146 | - array( 'values' => $results ), |
|
| 146 | + array('values' => $results), |
|
| 147 | 147 | ), |
| 148 | 148 | ) : $results; |
| 149 | 149 | |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | - private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 152 | + private function get_directly_referencing_posts($post_id, $referencing_post_ids) { |
|
| 153 | 153 | |
| 154 | - $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 154 | + $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities($post_id); |
|
| 155 | 155 | |
| 156 | - $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 156 | + $post__in = array_diff($directly_referencing_post_ids, $referencing_post_ids); |
|
| 157 | 157 | |
| 158 | - $directly_referencing_posts = get_posts( array( |
|
| 158 | + $directly_referencing_posts = get_posts(array( |
|
| 159 | 159 | 'meta_query' => array( |
| 160 | 160 | array( |
| 161 | 161 | 'key' => '_thumbnail_id' |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | 'post__in' => $post__in, |
| 169 | 169 | 'post_type' => 'product', |
| 170 | 170 | 'ignore_sticky_posts' => 1 |
| 171 | - ) ); |
|
| 171 | + )); |
|
| 172 | 172 | |
| 173 | 173 | $results = array(); |
| 174 | 174 | |
| 175 | - foreach ( $directly_referencing_posts as $post ) { |
|
| 175 | + foreach ($directly_referencing_posts as $post) { |
|
| 176 | 176 | $result = new stdClass(); |
| 177 | 177 | $result->ID = $post->ID; |
| 178 | 178 | $result->post_title = $post->post_title; |
@@ -195,17 +195,17 @@ discard block |
||
| 195 | 195 | ) { |
| 196 | 196 | global $wpdb; |
| 197 | 197 | |
| 198 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 198 | + $select = implode(', ', array_map(function($item) { |
|
| 199 | 199 | return "p.$item AS $item"; |
| 200 | - }, (array) $fields ) ); |
|
| 200 | + }, (array) $fields)); |
|
| 201 | 201 | |
| 202 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 202 | + $order_by = implode(', ', array_map(function($item) { |
|
| 203 | 203 | return "p.$item"; |
| 204 | - }, (array) $order_by ) ); |
|
| 204 | + }, (array) $order_by)); |
|
| 205 | 205 | |
| 206 | 206 | /** @noinspection SqlNoDataSourceInspection */ |
| 207 | 207 | return $wpdb->get_results( |
| 208 | - $wpdb->prepare( <<<EOF |
|
| 208 | + $wpdb->prepare(<<<EOF |
|
| 209 | 209 | SELECT %4\$s, p2.ID as entity_id |
| 210 | 210 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 211 | 211 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | LIMIT %2\$d |
| 239 | 239 | OFFSET %3\$d |
| 240 | 240 | EOF |
| 241 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 241 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 242 | 242 | ); |
| 243 | 243 | } |
| 244 | 244 | |
@@ -254,17 +254,17 @@ discard block |
||
| 254 | 254 | ) { |
| 255 | 255 | global $wpdb; |
| 256 | 256 | |
| 257 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 257 | + $select = implode(', ', array_map(function($item) { |
|
| 258 | 258 | return "p.$item AS $item"; |
| 259 | - }, (array) $fields ) ); |
|
| 259 | + }, (array) $fields)); |
|
| 260 | 260 | |
| 261 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 261 | + $order_by = implode(', ', array_map(function($item) { |
|
| 262 | 262 | return "p.$item"; |
| 263 | - }, (array) $order_by ) ); |
|
| 263 | + }, (array) $order_by)); |
|
| 264 | 264 | |
| 265 | 265 | /** @noinspection SqlNoDataSourceInspection */ |
| 266 | 266 | return $wpdb->get_results( |
| 267 | - $wpdb->prepare( <<<EOF |
|
| 267 | + $wpdb->prepare(<<<EOF |
|
| 268 | 268 | SELECT %4\$s, p2.ID as entity_id |
| 269 | 269 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 270 | 270 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | LIMIT %2\$d |
| 301 | 301 | OFFSET %3\$d |
| 302 | 302 | EOF |
| 303 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 303 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 304 | 304 | ); |
| 305 | 305 | } |
| 306 | 306 | |
@@ -20,32 +20,32 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_shortcode_navigator_data() { |
| 22 | 22 | |
| 23 | - // Create the cache key. |
|
| 24 | - $cache_key_params = $_REQUEST; |
|
| 25 | - unset( $cache_key_params['uniqid'] ); |
|
| 26 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 23 | + // Create the cache key. |
|
| 24 | + $cache_key_params = $_REQUEST; |
|
| 25 | + unset( $cache_key_params['uniqid'] ); |
|
| 26 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 27 | 27 | |
| 28 | - // Create the TTL cache and try to get the results. |
|
| 29 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + // Create the TTL cache and try to get the results. |
|
| 29 | + $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | + $cache_results = $cache->get( $cache_key ); |
|
| 31 | 31 | |
| 32 | - // So that the endpoint can be used remotely |
|
| 33 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 32 | + // So that the endpoint can be used remotely |
|
| 33 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 34 | 34 | |
| 35 | - if ( isset( $cache_results ) ) { |
|
| 36 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 35 | + if ( isset( $cache_results ) ) { |
|
| 36 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 37 | 37 | |
| 38 | - return $cache_results; |
|
| 39 | - } |
|
| 38 | + return $cache_results; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 41 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 42 | 42 | |
| 43 | - $results = _wl_navigator_get_data(); |
|
| 43 | + $results = _wl_navigator_get_data(); |
|
| 44 | 44 | |
| 45 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 46 | - $cache->put( $cache_key, $results ); |
|
| 45 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 46 | + $cache->put( $cache_key, $results ); |
|
| 47 | 47 | |
| 48 | - return $results; |
|
| 48 | + return $results; |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
@@ -60,240 +60,240 @@ discard block |
||
| 60 | 60 | */ |
| 61 | 61 | function wl_network_navigator_wp_json( $request ) { |
| 62 | 62 | |
| 63 | - // Create the cache key. |
|
| 64 | - $cache_key_params = $_REQUEST; |
|
| 65 | - unset( $cache_key_params['uniqid'] ); |
|
| 66 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 63 | + // Create the cache key. |
|
| 64 | + $cache_key_params = $_REQUEST; |
|
| 65 | + unset( $cache_key_params['uniqid'] ); |
|
| 66 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 67 | 67 | |
| 68 | - // Create the TTL cache and try to get the results. |
|
| 69 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | - $cache_results = $cache->get( $cache_key ); |
|
| 68 | + // Create the TTL cache and try to get the results. |
|
| 69 | + $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | + $cache_results = $cache->get( $cache_key ); |
|
| 71 | 71 | |
| 72 | - if ( isset( $cache_results ) ) { |
|
| 73 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 72 | + if ( isset( $cache_results ) ) { |
|
| 73 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 74 | 74 | |
| 75 | - return $cache_results; |
|
| 76 | - } |
|
| 75 | + return $cache_results; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 78 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 79 | 79 | |
| 80 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 80 | + $results = _wl_network_navigator_get_data( $request ); |
|
| 81 | 81 | |
| 82 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 83 | - $cache->put( $cache_key, $results ); |
|
| 82 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 83 | + $cache->put( $cache_key, $results ); |
|
| 84 | 84 | |
| 85 | - return $results; |
|
| 85 | + return $results; |
|
| 86 | 86 | |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | function _wl_navigator_get_data() { |
| 90 | 90 | |
| 91 | - // Post ID must be defined |
|
| 92 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | - wp_send_json_error( 'No post_id given' ); |
|
| 94 | - |
|
| 95 | - return array(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // Post ID must be defined |
|
| 99 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | - wp_send_json_error( 'No uniqid given' ); |
|
| 101 | - |
|
| 102 | - return array(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // Limit the results (defaults to 4) |
|
| 106 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | - $post_types = explode( ',', $post_types ); |
|
| 111 | - $existing_post_types = get_post_types(); |
|
| 112 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 113 | - $current_post_id = $_GET['post_id']; |
|
| 114 | - $current_post = get_post( $current_post_id ); |
|
| 115 | - |
|
| 116 | - $navigator_id = $_GET['uniqid']; |
|
| 117 | - |
|
| 118 | - // Post ID has to match an existing item |
|
| 119 | - if ( null === $current_post ) { |
|
| 120 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 121 | - |
|
| 122 | - return array(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Determine navigator type and call respective _get_results |
|
| 126 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 127 | - |
|
| 128 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 129 | - 'ID', |
|
| 130 | - 'post_title', |
|
| 131 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 132 | - } else { |
|
| 133 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 134 | - 'ID', |
|
| 135 | - 'post_title', |
|
| 136 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 137 | - |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 141 | - $results = array(); |
|
| 142 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Use the thumbnail. |
|
| 147 | - * |
|
| 148 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 149 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 150 | - * |
|
| 151 | - * @since 3.19.3 We're using the medium size image. |
|
| 152 | - */ |
|
| 153 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 154 | - |
|
| 155 | - $result = array( |
|
| 156 | - 'post' => array( |
|
| 157 | - 'id' => $referencing_post->ID, |
|
| 158 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | - 'title' => html_entity_decode( $referencing_post->post_title ), |
|
| 160 | - 'thumbnail' => $thumbnail, |
|
| 161 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 162 | - ), |
|
| 163 | - 'entity' => array( |
|
| 164 | - 'id' => $referencing_post->entity_id, |
|
| 165 | - 'label' => $serialized_entity['label'], |
|
| 166 | - 'mainType' => $serialized_entity['mainType'], |
|
| 167 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 168 | - ), |
|
| 169 | - ); |
|
| 170 | - |
|
| 171 | - $results[] = $result; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - if ( count( $results ) < $navigator_length ) { |
|
| 176 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Add filler posts if needed |
|
| 180 | - $filler_count = $navigator_length - count( $results ); |
|
| 181 | - if ( $filler_count > 0 ) { |
|
| 182 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 183 | - return $p->ID; |
|
| 184 | - }, $referencing_posts ); |
|
| 185 | - /** |
|
| 186 | - * @since 3.27.8 |
|
| 187 | - * Filler posts are fetched using this util. |
|
| 188 | - */ |
|
| 189 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | - $results = array_merge( $results, $filler_posts ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // Apply filters after fillers are added |
|
| 196 | - foreach ( $results as $result_index => $result ) { |
|
| 197 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id ); |
|
| 202 | - |
|
| 203 | - return $results; |
|
| 91 | + // Post ID must be defined |
|
| 92 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | + wp_send_json_error( 'No post_id given' ); |
|
| 94 | + |
|
| 95 | + return array(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // Post ID must be defined |
|
| 99 | + if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | + wp_send_json_error( 'No uniqid given' ); |
|
| 101 | + |
|
| 102 | + return array(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // Limit the results (defaults to 4) |
|
| 106 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | + $post_types = explode( ',', $post_types ); |
|
| 111 | + $existing_post_types = get_post_types(); |
|
| 112 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 113 | + $current_post_id = $_GET['post_id']; |
|
| 114 | + $current_post = get_post( $current_post_id ); |
|
| 115 | + |
|
| 116 | + $navigator_id = $_GET['uniqid']; |
|
| 117 | + |
|
| 118 | + // Post ID has to match an existing item |
|
| 119 | + if ( null === $current_post ) { |
|
| 120 | + wp_send_json_error( 'No valid post_id given' ); |
|
| 121 | + |
|
| 122 | + return array(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Determine navigator type and call respective _get_results |
|
| 126 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 127 | + |
|
| 128 | + $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 129 | + 'ID', |
|
| 130 | + 'post_title', |
|
| 131 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 132 | + } else { |
|
| 133 | + $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 134 | + 'ID', |
|
| 135 | + 'post_title', |
|
| 136 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 141 | + $results = array(); |
|
| 142 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Use the thumbnail. |
|
| 147 | + * |
|
| 148 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 149 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 150 | + * |
|
| 151 | + * @since 3.19.3 We're using the medium size image. |
|
| 152 | + */ |
|
| 153 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 154 | + |
|
| 155 | + $result = array( |
|
| 156 | + 'post' => array( |
|
| 157 | + 'id' => $referencing_post->ID, |
|
| 158 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | + 'title' => html_entity_decode( $referencing_post->post_title ), |
|
| 160 | + 'thumbnail' => $thumbnail, |
|
| 161 | + 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 162 | + ), |
|
| 163 | + 'entity' => array( |
|
| 164 | + 'id' => $referencing_post->entity_id, |
|
| 165 | + 'label' => $serialized_entity['label'], |
|
| 166 | + 'mainType' => $serialized_entity['mainType'], |
|
| 167 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 168 | + ), |
|
| 169 | + ); |
|
| 170 | + |
|
| 171 | + $results[] = $result; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + if ( count( $results ) < $navigator_length ) { |
|
| 176 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // Add filler posts if needed |
|
| 180 | + $filler_count = $navigator_length - count( $results ); |
|
| 181 | + if ( $filler_count > 0 ) { |
|
| 182 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 183 | + return $p->ID; |
|
| 184 | + }, $referencing_posts ); |
|
| 185 | + /** |
|
| 186 | + * @since 3.27.8 |
|
| 187 | + * Filler posts are fetched using this util. |
|
| 188 | + */ |
|
| 189 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | + $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | + $results = array_merge( $results, $filler_posts ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // Apply filters after fillers are added |
|
| 196 | + foreach ( $results as $result_index => $result ) { |
|
| 197 | + $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $results = apply_filters( 'wl_navigator_results', $results, $navigator_id ); |
|
| 202 | + |
|
| 203 | + return $results; |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | function _wl_network_navigator_get_data( $request ) { |
| 207 | 207 | |
| 208 | - // Limit the results (defaults to 4) |
|
| 209 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 211 | - $navigator_id = $request['uniqid']; |
|
| 212 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 213 | - |
|
| 214 | - $entities = $request['entities']; |
|
| 215 | - |
|
| 216 | - // Post ID has to match an existing item |
|
| 217 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 222 | - 'ID', |
|
| 223 | - 'post_title', |
|
| 224 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 225 | - |
|
| 226 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 227 | - $results = array(); |
|
| 228 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Use the thumbnail. |
|
| 233 | - * |
|
| 234 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 235 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 236 | - * |
|
| 237 | - * @since 3.19.3 We're using the medium size image. |
|
| 238 | - */ |
|
| 239 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 240 | - |
|
| 241 | - $result = array( |
|
| 242 | - 'post' => array( |
|
| 243 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 244 | - 'title' => $referencing_post->post_title, |
|
| 245 | - 'thumbnail' => $thumbnail, |
|
| 246 | - ), |
|
| 247 | - 'entity' => array( |
|
| 248 | - 'label' => $serialized_entity['label'], |
|
| 249 | - 'mainType' => $serialized_entity['mainType'], |
|
| 250 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 251 | - ), |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 256 | - |
|
| 257 | - $results[] = $result; |
|
| 258 | - |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - if ( count( $results ) < $navigator_length ) { |
|
| 262 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 266 | - |
|
| 267 | - return $results; |
|
| 208 | + // Limit the results (defaults to 4) |
|
| 209 | + $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | + $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 211 | + $navigator_id = $request['uniqid']; |
|
| 212 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 213 | + |
|
| 214 | + $entities = $request['entities']; |
|
| 215 | + |
|
| 216 | + // Post ID has to match an existing item |
|
| 217 | + if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | + wp_send_json_error( 'No valid entities provided' ); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 222 | + 'ID', |
|
| 223 | + 'post_title', |
|
| 224 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 225 | + |
|
| 226 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 227 | + $results = array(); |
|
| 228 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Use the thumbnail. |
|
| 233 | + * |
|
| 234 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 235 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 236 | + * |
|
| 237 | + * @since 3.19.3 We're using the medium size image. |
|
| 238 | + */ |
|
| 239 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 240 | + |
|
| 241 | + $result = array( |
|
| 242 | + 'post' => array( |
|
| 243 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 244 | + 'title' => $referencing_post->post_title, |
|
| 245 | + 'thumbnail' => $thumbnail, |
|
| 246 | + ), |
|
| 247 | + 'entity' => array( |
|
| 248 | + 'label' => $serialized_entity['label'], |
|
| 249 | + 'mainType' => $serialized_entity['mainType'], |
|
| 250 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 251 | + ), |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | + $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 256 | + |
|
| 257 | + $results[] = $result; |
|
| 258 | + |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + if ( count( $results ) < $navigator_length ) { |
|
| 262 | + $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 266 | + |
|
| 267 | + return $results; |
|
| 268 | 268 | |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | function _wl_network_navigator_get_results( |
| 272 | - $entities, $fields = array( |
|
| 273 | - 'ID', |
|
| 274 | - 'post_title', |
|
| 272 | + $entities, $fields = array( |
|
| 273 | + 'ID', |
|
| 274 | + 'post_title', |
|
| 275 | 275 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 276 | 276 | ) { |
| 277 | - global $wpdb; |
|
| 278 | - |
|
| 279 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 280 | - return "p.$item AS $item"; |
|
| 281 | - }, (array) $fields ) ); |
|
| 282 | - |
|
| 283 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 284 | - return "p.$item"; |
|
| 285 | - }, (array) $order_by ) ); |
|
| 286 | - |
|
| 287 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | - if ( isset( $entity ) ) { |
|
| 290 | - return $entity->ID; |
|
| 291 | - } |
|
| 292 | - }, $entities ) ); |
|
| 293 | - |
|
| 294 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 295 | - return $wpdb->get_results( |
|
| 296 | - $wpdb->prepare( <<<EOF |
|
| 277 | + global $wpdb; |
|
| 278 | + |
|
| 279 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 280 | + return "p.$item AS $item"; |
|
| 281 | + }, (array) $fields ) ); |
|
| 282 | + |
|
| 283 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 284 | + return "p.$item"; |
|
| 285 | + }, (array) $order_by ) ); |
|
| 286 | + |
|
| 287 | + $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | + if ( isset( $entity ) ) { |
|
| 290 | + return $entity->ID; |
|
| 291 | + } |
|
| 292 | + }, $entities ) ); |
|
| 293 | + |
|
| 294 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 295 | + return $wpdb->get_results( |
|
| 296 | + $wpdb->prepare( <<<EOF |
|
| 297 | 297 | SELECT %3\$s, p2.ID as entity_id |
| 298 | 298 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 299 | 299 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -319,8 +319,8 @@ discard block |
||
| 319 | 319 | LIMIT %1\$d |
| 320 | 320 | OFFSET %2\$d |
| 321 | 321 | EOF |
| 322 | - , $limit, $offset, $select, $order_by ) |
|
| 323 | - ); |
|
| 322 | + , $limit, $offset, $select, $order_by ) |
|
| 323 | + ); |
|
| 324 | 324 | |
| 325 | 325 | } |
| 326 | 326 | |
@@ -332,9 +332,9 @@ discard block |
||
| 332 | 332 | */ |
| 333 | 333 | function wl_shortcode_navigator_ajax() { |
| 334 | 334 | |
| 335 | - // Temporary blocking the Navigator. |
|
| 336 | - $results = wl_shortcode_navigator_data(); |
|
| 337 | - wl_core_send_json( $results ); |
|
| 335 | + // Temporary blocking the Navigator. |
|
| 336 | + $results = wl_shortcode_navigator_data(); |
|
| 337 | + wl_core_send_json( $results ); |
|
| 338 | 338 | |
| 339 | 339 | } |
| 340 | 340 | |
@@ -346,16 +346,16 @@ discard block |
||
| 346 | 346 | */ |
| 347 | 347 | function wl_shortcode_navigator_wp_json() { |
| 348 | 348 | |
| 349 | - $results = wl_shortcode_navigator_data(); |
|
| 350 | - if ( ob_get_contents() ) { |
|
| 351 | - ob_clean(); |
|
| 352 | - } |
|
| 349 | + $results = wl_shortcode_navigator_data(); |
|
| 350 | + if ( ob_get_contents() ) { |
|
| 351 | + ob_clean(); |
|
| 352 | + } |
|
| 353 | 353 | |
| 354 | - return array( |
|
| 355 | - 'items' => array( |
|
| 356 | - array( 'values' => $results ), |
|
| 357 | - ), |
|
| 358 | - ); |
|
| 354 | + return array( |
|
| 355 | + 'items' => array( |
|
| 356 | + array( 'values' => $results ), |
|
| 357 | + ), |
|
| 358 | + ); |
|
| 359 | 359 | |
| 360 | 360 | } |
| 361 | 361 | |
@@ -363,22 +363,22 @@ discard block |
||
| 363 | 363 | * Adding `rest_api_init` action for amp backend of navigator |
| 364 | 364 | */ |
| 365 | 365 | add_action( 'rest_api_init', function () { |
| 366 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | - 'methods' => 'GET', |
|
| 368 | - 'permission_callback' => '__return_true', |
|
| 369 | - 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 370 | - ) ); |
|
| 366 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | + 'methods' => 'GET', |
|
| 368 | + 'permission_callback' => '__return_true', |
|
| 369 | + 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 370 | + ) ); |
|
| 371 | 371 | } ); |
| 372 | 372 | |
| 373 | 373 | /** |
| 374 | 374 | * Adding `rest_api_init` action for backend of network navigator |
| 375 | 375 | */ |
| 376 | 376 | add_action( 'rest_api_init', function () { |
| 377 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | - 'methods' => 'GET', |
|
| 379 | - 'callback' => 'wl_network_navigator_wp_json', |
|
| 380 | - 'permission_callback' => '__return_true', |
|
| 381 | - ) ); |
|
| 377 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | + 'methods' => 'GET', |
|
| 379 | + 'callback' => 'wl_network_navigator_wp_json', |
|
| 380 | + 'permission_callback' => '__return_true', |
|
| 381 | + ) ); |
|
| 382 | 382 | } ); |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -387,23 +387,23 @@ discard block |
||
| 387 | 387 | * @since 2.2.0 |
| 388 | 388 | */ |
| 389 | 389 | add_action( 'plugins_loaded', function () { |
| 390 | - $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 391 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 392 | - return; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 390 | + $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 391 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 392 | + return; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 398 | 398 | }, 0 ); |
| 399 | 399 | |
| 400 | 400 | add_action( 'init', function () { |
| 401 | - $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 402 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 403 | - return; |
|
| 404 | - } |
|
| 401 | + $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 402 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 403 | + return; |
|
| 404 | + } |
|
| 405 | 405 | |
| 406 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 406 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 408 | 408 | }, 0 ); |
| 409 | 409 | |
@@ -22,28 +22,28 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | // Create the cache key. |
| 24 | 24 | $cache_key_params = $_REQUEST; |
| 25 | - unset( $cache_key_params['uniqid'] ); |
|
| 26 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 25 | + unset($cache_key_params['uniqid']); |
|
| 26 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 27 | 27 | |
| 28 | 28 | // Create the TTL cache and try to get the results. |
| 29 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 29 | + $cache = new Ttl_Cache("navigator", 8 * 60 * 60); // 8 hours. |
|
| 30 | + $cache_results = $cache->get($cache_key); |
|
| 31 | 31 | |
| 32 | 32 | // So that the endpoint can be used remotely |
| 33 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 33 | + header('Access-Control-Allow-Origin: *'); |
|
| 34 | 34 | |
| 35 | - if ( isset( $cache_results ) ) { |
|
| 36 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 35 | + if (isset($cache_results)) { |
|
| 36 | + header('X-WordLift-Cache: HIT'); |
|
| 37 | 37 | |
| 38 | 38 | return $cache_results; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 41 | + header('X-WordLift-Cache: MISS'); |
|
| 42 | 42 | |
| 43 | 43 | $results = _wl_navigator_get_data(); |
| 44 | 44 | |
| 45 | 45 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 46 | - $cache->put( $cache_key, $results ); |
|
| 46 | + $cache->put($cache_key, $results); |
|
| 47 | 47 | |
| 48 | 48 | return $results; |
| 49 | 49 | } |
@@ -58,29 +58,29 @@ discard block |
||
| 58 | 58 | * @since 3.22.6 |
| 59 | 59 | * |
| 60 | 60 | */ |
| 61 | -function wl_network_navigator_wp_json( $request ) { |
|
| 61 | +function wl_network_navigator_wp_json($request) { |
|
| 62 | 62 | |
| 63 | 63 | // Create the cache key. |
| 64 | 64 | $cache_key_params = $_REQUEST; |
| 65 | - unset( $cache_key_params['uniqid'] ); |
|
| 66 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 65 | + unset($cache_key_params['uniqid']); |
|
| 66 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 67 | 67 | |
| 68 | 68 | // Create the TTL cache and try to get the results. |
| 69 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | - $cache_results = $cache->get( $cache_key ); |
|
| 69 | + $cache = new Ttl_Cache("network-navigator", 8 * 60 * 60); // 8 hours. |
|
| 70 | + $cache_results = $cache->get($cache_key); |
|
| 71 | 71 | |
| 72 | - if ( isset( $cache_results ) ) { |
|
| 73 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 72 | + if (isset($cache_results)) { |
|
| 73 | + header('X-WordLift-Cache: HIT'); |
|
| 74 | 74 | |
| 75 | 75 | return $cache_results; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 78 | + header('X-WordLift-Cache: MISS'); |
|
| 79 | 79 | |
| 80 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 80 | + $results = _wl_network_navigator_get_data($request); |
|
| 81 | 81 | |
| 82 | 82 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 83 | - $cache->put( $cache_key, $results ); |
|
| 83 | + $cache->put($cache_key, $results); |
|
| 84 | 84 | |
| 85 | 85 | return $results; |
| 86 | 86 | |
@@ -89,58 +89,58 @@ discard block |
||
| 89 | 89 | function _wl_navigator_get_data() { |
| 90 | 90 | |
| 91 | 91 | // Post ID must be defined |
| 92 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | - wp_send_json_error( 'No post_id given' ); |
|
| 92 | + if ( ! isset($_GET['post_id'])) { |
|
| 93 | + wp_send_json_error('No post_id given'); |
|
| 94 | 94 | |
| 95 | 95 | return array(); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | // Post ID must be defined |
| 99 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | - wp_send_json_error( 'No uniqid given' ); |
|
| 99 | + if ( ! isset($_GET['uniqid'])) { |
|
| 100 | + wp_send_json_error('No uniqid given'); |
|
| 101 | 101 | |
| 102 | 102 | return array(); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // Limit the results (defaults to 4) |
| 106 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | - $post_types = explode( ',', $post_types ); |
|
| 106 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 107 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 108 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 109 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 110 | + $post_types = explode(',', $post_types); |
|
| 111 | 111 | $existing_post_types = get_post_types(); |
| 112 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 112 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 113 | 113 | $current_post_id = $_GET['post_id']; |
| 114 | - $current_post = get_post( $current_post_id ); |
|
| 114 | + $current_post = get_post($current_post_id); |
|
| 115 | 115 | |
| 116 | 116 | $navigator_id = $_GET['uniqid']; |
| 117 | 117 | |
| 118 | 118 | // Post ID has to match an existing item |
| 119 | - if ( null === $current_post ) { |
|
| 120 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 119 | + if (null === $current_post) { |
|
| 120 | + wp_send_json_error('No valid post_id given'); |
|
| 121 | 121 | |
| 122 | 122 | return array(); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Determine navigator type and call respective _get_results |
| 126 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 126 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 127 | 127 | |
| 128 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 128 | + $referencing_posts = Navigator_Data::entity_navigator_get_results($current_post_id, array( |
|
| 129 | 129 | 'ID', |
| 130 | 130 | 'post_title', |
| 131 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 131 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 132 | 132 | } else { |
| 133 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 133 | + $referencing_posts = Navigator_Data::post_navigator_get_results($current_post_id, array( |
|
| 134 | 134 | 'ID', |
| 135 | 135 | 'post_title', |
| 136 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 136 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 137 | 137 | |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // loop over them and take the first one which is not already in the $related_posts |
| 141 | 141 | $results = array(); |
| 142 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 142 | + foreach ($referencing_posts as $referencing_post) { |
|
| 143 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | 146 | * Use the thumbnail. |
@@ -150,21 +150,21 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @since 3.19.3 We're using the medium size image. |
| 152 | 152 | */ |
| 153 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 153 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 154 | 154 | |
| 155 | 155 | $result = array( |
| 156 | 156 | 'post' => array( |
| 157 | 157 | 'id' => $referencing_post->ID, |
| 158 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | - 'title' => html_entity_decode( $referencing_post->post_title ), |
|
| 158 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 159 | + 'title' => html_entity_decode($referencing_post->post_title), |
|
| 160 | 160 | 'thumbnail' => $thumbnail, |
| 161 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 161 | + 'srcset' => Srcset_Util::get_srcset($referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET) |
|
| 162 | 162 | ), |
| 163 | 163 | 'entity' => array( |
| 164 | 164 | 'id' => $referencing_post->entity_id, |
| 165 | 165 | 'label' => $serialized_entity['label'], |
| 166 | 166 | 'mainType' => $serialized_entity['mainType'], |
| 167 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 167 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 168 | 168 | ), |
| 169 | 169 | ); |
| 170 | 170 | |
@@ -172,61 +172,61 @@ discard block |
||
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | |
| 175 | - if ( count( $results ) < $navigator_length ) { |
|
| 176 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 175 | + if (count($results) < $navigator_length) { |
|
| 176 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Add filler posts if needed |
| 180 | - $filler_count = $navigator_length - count( $results ); |
|
| 181 | - if ( $filler_count > 0 ) { |
|
| 182 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 180 | + $filler_count = $navigator_length - count($results); |
|
| 181 | + if ($filler_count > 0) { |
|
| 182 | + $referencing_post_ids = array_map(function($p) { |
|
| 183 | 183 | return $p->ID; |
| 184 | - }, $referencing_posts ); |
|
| 184 | + }, $referencing_posts); |
|
| 185 | 185 | /** |
| 186 | 186 | * @since 3.27.8 |
| 187 | 187 | * Filler posts are fetched using this util. |
| 188 | 188 | */ |
| 189 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | - $results = array_merge( $results, $filler_posts ); |
|
| 189 | + $filler_posts_util = new Filler_Posts_Util($current_post_id, $post_types); |
|
| 190 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 191 | + $filler_posts = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded); |
|
| 192 | + $results = array_merge($results, $filler_posts); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | // Apply filters after fillers are added |
| 196 | - foreach ( $results as $result_index => $result ) { |
|
| 197 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 196 | + foreach ($results as $result_index => $result) { |
|
| 197 | + $results[$result_index]['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id); |
|
| 198 | + $results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id ); |
|
| 201 | + $results = apply_filters('wl_navigator_results', $results, $navigator_id); |
|
| 202 | 202 | |
| 203 | 203 | return $results; |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | -function _wl_network_navigator_get_data( $request ) { |
|
| 206 | +function _wl_network_navigator_get_data($request) { |
|
| 207 | 207 | |
| 208 | 208 | // Limit the results (defaults to 4) |
| 209 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 209 | + $navigator_length = isset($request['limit']) ? intval($request['limit']) : 4; |
|
| 210 | + $navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0; |
|
| 211 | 211 | $navigator_id = $request['uniqid']; |
| 212 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 212 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 213 | 213 | |
| 214 | 214 | $entities = $request['entities']; |
| 215 | 215 | |
| 216 | 216 | // Post ID has to match an existing item |
| 217 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 217 | + if ( ! isset($entities) || empty($entities)) { |
|
| 218 | + wp_send_json_error('No valid entities provided'); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 221 | + $referencing_posts = _wl_network_navigator_get_results($entities, array( |
|
| 222 | 222 | 'ID', |
| 223 | 223 | 'post_title', |
| 224 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 224 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 225 | 225 | |
| 226 | 226 | // loop over them and take the first one which is not already in the $related_posts |
| 227 | 227 | $results = array(); |
| 228 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 228 | + foreach ($referencing_posts as $referencing_post) { |
|
| 229 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 230 | 230 | |
| 231 | 231 | /** |
| 232 | 232 | * Use the thumbnail. |
@@ -236,33 +236,33 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @since 3.19.3 We're using the medium size image. |
| 238 | 238 | */ |
| 239 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 239 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 240 | 240 | |
| 241 | 241 | $result = array( |
| 242 | 242 | 'post' => array( |
| 243 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 243 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 244 | 244 | 'title' => $referencing_post->post_title, |
| 245 | 245 | 'thumbnail' => $thumbnail, |
| 246 | 246 | ), |
| 247 | 247 | 'entity' => array( |
| 248 | 248 | 'label' => $serialized_entity['label'], |
| 249 | 249 | 'mainType' => $serialized_entity['mainType'], |
| 250 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 250 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 251 | 251 | ), |
| 252 | 252 | ); |
| 253 | 253 | |
| 254 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 254 | + $result['post'] = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 255 | + $result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 256 | 256 | |
| 257 | 257 | $results[] = $result; |
| 258 | 258 | |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | - if ( count( $results ) < $navigator_length ) { |
|
| 262 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 261 | + if (count($results) < $navigator_length) { |
|
| 262 | + $results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 265 | + $results = apply_filters('wl_network_navigator_results', $results, $navigator_id); |
|
| 266 | 266 | |
| 267 | 267 | return $results; |
| 268 | 268 | |
@@ -276,24 +276,24 @@ discard block |
||
| 276 | 276 | ) { |
| 277 | 277 | global $wpdb; |
| 278 | 278 | |
| 279 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 279 | + $select = implode(', ', array_map(function($item) { |
|
| 280 | 280 | return "p.$item AS $item"; |
| 281 | - }, (array) $fields ) ); |
|
| 281 | + }, (array) $fields)); |
|
| 282 | 282 | |
| 283 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 283 | + $order_by = implode(', ', array_map(function($item) { |
|
| 284 | 284 | return "p.$item"; |
| 285 | - }, (array) $order_by ) ); |
|
| 285 | + }, (array) $order_by)); |
|
| 286 | 286 | |
| 287 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | - if ( isset( $entity ) ) { |
|
| 287 | + $entities_in = implode(',', array_map(function($item) { |
|
| 288 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item)); |
|
| 289 | + if (isset($entity)) { |
|
| 290 | 290 | return $entity->ID; |
| 291 | 291 | } |
| 292 | - }, $entities ) ); |
|
| 292 | + }, $entities)); |
|
| 293 | 293 | |
| 294 | 294 | /** @noinspection SqlNoDataSourceInspection */ |
| 295 | 295 | return $wpdb->get_results( |
| 296 | - $wpdb->prepare( <<<EOF |
|
| 296 | + $wpdb->prepare(<<<EOF |
|
| 297 | 297 | SELECT %3\$s, p2.ID as entity_id |
| 298 | 298 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 299 | 299 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | LIMIT %1\$d |
| 320 | 320 | OFFSET %2\$d |
| 321 | 321 | EOF |
| 322 | - , $limit, $offset, $select, $order_by ) |
|
| 322 | + , $limit, $offset, $select, $order_by) |
|
| 323 | 323 | ); |
| 324 | 324 | |
| 325 | 325 | } |
@@ -334,12 +334,12 @@ discard block |
||
| 334 | 334 | |
| 335 | 335 | // Temporary blocking the Navigator. |
| 336 | 336 | $results = wl_shortcode_navigator_data(); |
| 337 | - wl_core_send_json( $results ); |
|
| 337 | + wl_core_send_json($results); |
|
| 338 | 338 | |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 342 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 341 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 342 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 343 | 343 | |
| 344 | 344 | /** |
| 345 | 345 | * wp-json call for the navigator widget |
@@ -347,13 +347,13 @@ discard block |
||
| 347 | 347 | function wl_shortcode_navigator_wp_json() { |
| 348 | 348 | |
| 349 | 349 | $results = wl_shortcode_navigator_data(); |
| 350 | - if ( ob_get_contents() ) { |
|
| 350 | + if (ob_get_contents()) { |
|
| 351 | 351 | ob_clean(); |
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | return array( |
| 355 | 355 | 'items' => array( |
| 356 | - array( 'values' => $results ), |
|
| 356 | + array('values' => $results), |
|
| 357 | 357 | ), |
| 358 | 358 | ); |
| 359 | 359 | |
@@ -362,23 +362,23 @@ discard block |
||
| 362 | 362 | /** |
| 363 | 363 | * Adding `rest_api_init` action for amp backend of navigator |
| 364 | 364 | */ |
| 365 | -add_action( 'rest_api_init', function () { |
|
| 366 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 365 | +add_action('rest_api_init', function() { |
|
| 366 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | 367 | 'methods' => 'GET', |
| 368 | 368 | 'permission_callback' => '__return_true', |
| 369 | 369 | 'callback' => 'wl_shortcode_navigator_wp_json' |
| 370 | - ) ); |
|
| 370 | + )); |
|
| 371 | 371 | } ); |
| 372 | 372 | |
| 373 | 373 | /** |
| 374 | 374 | * Adding `rest_api_init` action for backend of network navigator |
| 375 | 375 | */ |
| 376 | -add_action( 'rest_api_init', function () { |
|
| 377 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 376 | +add_action('rest_api_init', function() { |
|
| 377 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | 378 | 'methods' => 'GET', |
| 379 | 379 | 'callback' => 'wl_network_navigator_wp_json', |
| 380 | 380 | 'permission_callback' => '__return_true', |
| 381 | - ) ); |
|
| 381 | + )); |
|
| 382 | 382 | } ); |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -386,24 +386,24 @@ discard block |
||
| 386 | 386 | * |
| 387 | 387 | * @since 2.2.0 |
| 388 | 388 | */ |
| 389 | -add_action( 'plugins_loaded', function () { |
|
| 390 | - $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 391 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 389 | +add_action('plugins_loaded', function() { |
|
| 390 | + $action = array_key_exists('action', $_REQUEST) ? $_REQUEST['action'] : ''; |
|
| 391 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 392 | 392 | return; |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 398 | -}, 0 ); |
|
| 395 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 396 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 397 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 398 | +}, 0); |
|
| 399 | 399 | |
| 400 | -add_action( 'init', function () { |
|
| 401 | - $action = array_key_exists( 'action', $_REQUEST ) ? $_REQUEST['action'] : ''; |
|
| 402 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 400 | +add_action('init', function() { |
|
| 401 | + $action = array_key_exists('action', $_REQUEST) ? $_REQUEST['action'] : ''; |
|
| 402 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 403 | 403 | return; |
| 404 | 404 | } |
| 405 | 405 | |
| 406 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 408 | -}, 0 ); |
|
| 406 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 407 | + remove_action('init', 'gglcptch_init'); |
|
| 408 | +}, 0); |
|
| 409 | 409 | |
@@ -14,12 +14,12 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Same_Category_Filler_Posts extends Filler_Posts { |
| 16 | 16 | |
| 17 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 17 | + function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 18 | 18 | |
| 19 | - $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 20 | - $post_type = $this->alternate_post_type ?: get_post_types(); |
|
| 19 | + $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 20 | + $post_type = $this->alternate_post_type ?: get_post_types(); |
|
| 21 | 21 | |
| 22 | - return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => $post_type ) |
|
| 23 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 24 | - } |
|
| 22 | + return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => $post_type ) |
|
| 23 | + + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -14,12 +14,12 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Same_Category_Filler_Posts extends Filler_Posts { |
| 16 | 16 | |
| 17 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 17 | + function get_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 18 | 18 | |
| 19 | - $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 19 | + $current_post_categories = wp_get_post_categories($this->post_id); |
|
| 20 | 20 | $post_type = $this->alternate_post_type ?: get_post_types(); |
| 21 | 21 | |
| 22 | - return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => $post_type ) |
|
| 23 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 22 | + return get_posts(array('category__in' => $current_post_categories, 'post_type' => $post_type) |
|
| 23 | + + $this->get_posts_config($filler_count, $post_ids_to_be_excluded)); |
|
| 24 | 24 | } |
| 25 | 25 | } |
@@ -8,14 +8,14 @@ |
||
| 8 | 8 | class Same_Post_Type_Same_Category_Posts extends Filler_Posts { |
| 9 | 9 | |
| 10 | 10 | |
| 11 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 12 | - $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 13 | - $post_type = $this->alternate_post_type ?: get_post_type( $this->post_id ); |
|
| 11 | + function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 12 | + $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 13 | + $post_type = $this->alternate_post_type ?: get_post_type( $this->post_id ); |
|
| 14 | 14 | |
| 15 | - return get_posts( array( |
|
| 16 | - 'category__in' => $current_post_categories, |
|
| 17 | - 'post_type' => $post_type |
|
| 18 | - ) |
|
| 19 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 20 | - } |
|
| 15 | + return get_posts( array( |
|
| 16 | + 'category__in' => $current_post_categories, |
|
| 17 | + 'post_type' => $post_type |
|
| 18 | + ) |
|
| 19 | + + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -8,14 +8,14 @@ |
||
| 8 | 8 | class Same_Post_Type_Same_Category_Posts extends Filler_Posts { |
| 9 | 9 | |
| 10 | 10 | |
| 11 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 12 | - $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 13 | - $post_type = $this->alternate_post_type ?: get_post_type( $this->post_id ); |
|
| 11 | + function get_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 12 | + $current_post_categories = wp_get_post_categories($this->post_id); |
|
| 13 | + $post_type = $this->alternate_post_type ?: get_post_type($this->post_id); |
|
| 14 | 14 | |
| 15 | - return get_posts( array( |
|
| 15 | + return get_posts(array( |
|
| 16 | 16 | 'category__in' => $current_post_categories, |
| 17 | 17 | 'post_type' => $post_type |
| 18 | 18 | ) |
| 19 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 19 | + + $this->get_posts_config($filler_count, $post_ids_to_be_excluded)); |
|
| 20 | 20 | } |
| 21 | 21 | } |
@@ -7,15 +7,15 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Same_Post_Type_Filler_Posts extends Filler_Posts { |
| 9 | 9 | |
| 10 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 10 | + function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 11 | 11 | |
| 12 | - $post_type = $this->alternate_post_type ?: get_post_type( $this->post_id ); |
|
| 12 | + $post_type = $this->alternate_post_type ?: get_post_type( $this->post_id ); |
|
| 13 | 13 | |
| 14 | - if ( $post_type === 'entity' ) { |
|
| 15 | - $post_type = 'post'; |
|
| 16 | - } |
|
| 14 | + if ( $post_type === 'entity' ) { |
|
| 15 | + $post_type = 'post'; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - return get_posts( array( 'post_type' => $post_type ) |
|
| 19 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 20 | - } |
|
| 18 | + return get_posts( array( 'post_type' => $post_type ) |
|
| 19 | + + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -7,15 +7,15 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Same_Post_Type_Filler_Posts extends Filler_Posts { |
| 9 | 9 | |
| 10 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 10 | + function get_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 11 | 11 | |
| 12 | - $post_type = $this->alternate_post_type ?: get_post_type( $this->post_id ); |
|
| 12 | + $post_type = $this->alternate_post_type ?: get_post_type($this->post_id); |
|
| 13 | 13 | |
| 14 | - if ( $post_type === 'entity' ) { |
|
| 14 | + if ($post_type === 'entity') { |
|
| 15 | 15 | $post_type = 'post'; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - return get_posts( array( 'post_type' => $post_type ) |
|
| 19 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 18 | + return get_posts(array('post_type' => $post_type) |
|
| 19 | + + $this->get_posts_config($filler_count, $post_ids_to_be_excluded)); |
|
| 20 | 20 | } |
| 21 | 21 | } |
@@ -10,157 +10,157 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Filler_Posts_Util { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var array<Filler_Posts> |
|
| 15 | - */ |
|
| 16 | - private $sources = array(); |
|
| 17 | - |
|
| 18 | - public function __construct( $post_id, $alternate_post_type = null ) { |
|
| 19 | - |
|
| 20 | - $post_type = $alternate_post_type ?: get_post_type( $post_id ); |
|
| 21 | - |
|
| 22 | - if ( $post_type === 'post' || ( is_array( $post_type ) && in_array( 'post', $post_type ) ) ) { |
|
| 23 | - $this->sources = array( |
|
| 24 | - new Same_Category_Filler_Posts( $post_id ), |
|
| 25 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 26 | - ); |
|
| 27 | - } else if ( $post_type === 'product' || ( is_array( $post_type ) && in_array( 'product', $post_type ) ) ) { |
|
| 28 | - $this->sources = array( |
|
| 29 | - new Same_Post_Type_Same_Category_Posts( $post_id, 'product' ), |
|
| 30 | - new Same_Post_Type_Filler_Posts( $post_id, 'product' ), |
|
| 31 | - ); |
|
| 32 | - } else { |
|
| 33 | - $this->sources = array( |
|
| 34 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 35 | - ); |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param $posts array<\WP_Post> |
|
| 42 | - * |
|
| 43 | - * @return array<int> |
|
| 44 | - */ |
|
| 45 | - private function extract_post_ids( $posts ) { |
|
| 46 | - return array_map( function ( $post ) { |
|
| 47 | - /** |
|
| 48 | - * @var $post \WP_Post |
|
| 49 | - */ |
|
| 50 | - return $post->ID; |
|
| 51 | - }, $posts ); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 55 | - |
|
| 56 | - $filler_posts = array(); |
|
| 57 | - |
|
| 58 | - foreach ( $this->sources as $source ) { |
|
| 59 | - |
|
| 60 | - if ( $filler_count <= 0 ) { |
|
| 61 | - break; |
|
| 62 | - } |
|
| 63 | - /** |
|
| 64 | - * @var Filler_Posts $source |
|
| 65 | - */ |
|
| 66 | - $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
|
| 67 | - $source->filler_count = $filler_count; |
|
| 68 | - |
|
| 69 | - $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 70 | - $post_ids = $this->extract_post_ids( $posts ); |
|
| 71 | - |
|
| 72 | - // Update the post ids, filler posts and filler count |
|
| 73 | - $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 74 | - $filler_count = $filler_count - count( $posts ); |
|
| 75 | - $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 76 | - } |
|
| 77 | - $filler_posts = $this->add_additional_properties_to_filler_posts( $filler_posts ); |
|
| 78 | - |
|
| 79 | - return $filler_posts; |
|
| 80 | - |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @param $posts array<\WP_Post> |
|
| 85 | - * |
|
| 86 | - * @return array $posts array<\WP_Post> |
|
| 87 | - */ |
|
| 88 | - private function add_additional_properties_to_filler_posts( $posts ) { |
|
| 89 | - return array_map( function ( $post ) { |
|
| 90 | - $post->thumbnail = get_the_post_thumbnail_url( $post->ID, 'medium' ); |
|
| 91 | - $post->permalink = get_permalink( $post->ID ); |
|
| 92 | - $post->post_title = html_entity_decode( $post->post_title ); |
|
| 93 | - |
|
| 94 | - return $post; |
|
| 95 | - }, $posts ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Called by wordlift navigator, converts all the posts to response format. |
|
| 100 | - * |
|
| 101 | - * @param $filler_count |
|
| 102 | - * @param $post_ids_to_be_excluded |
|
| 103 | - * |
|
| 104 | - * @return array |
|
| 105 | - */ |
|
| 106 | - public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 107 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 108 | - // Add thumbnail and permalink to filler posts |
|
| 109 | - $filler_response = array(); |
|
| 110 | - foreach ( $filler_posts as $post_obj ) { |
|
| 111 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 112 | - $filler_response[] = array( |
|
| 113 | - 'post' => array( |
|
| 114 | - 'id' => $post_obj->ID, |
|
| 115 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 116 | - 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 117 | - 'title' => get_the_title( $post_obj->ID ), |
|
| 118 | - 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 119 | - ), |
|
| 120 | - 'entity' => array( |
|
| 121 | - 'id' => 0 |
|
| 122 | - ) |
|
| 123 | - ); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $filler_response; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Called by wordlift navigator, converts all the posts to response format. |
|
| 132 | - * |
|
| 133 | - * @param $filler_count |
|
| 134 | - * @param $post_ids_to_be_excluded |
|
| 135 | - * |
|
| 136 | - * @return array |
|
| 137 | - */ |
|
| 138 | - public function get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 139 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 140 | - // Add thumbnail and permalink to filler posts |
|
| 141 | - $filler_response = array(); |
|
| 142 | - foreach ( $filler_posts as $post_obj ) { |
|
| 143 | - $product = wc_get_product( $post_obj->ID ); |
|
| 144 | - $filler_response[] = array( |
|
| 145 | - 'product' => array( |
|
| 146 | - 'id' => $post_obj->ID, |
|
| 147 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 148 | - 'title' => $post_obj->post_title, |
|
| 149 | - 'thumbnail' => get_the_post_thumbnail_url( $post_obj->ID, 'medium' ), |
|
| 150 | - 'regular_price' => $product->get_regular_price(), |
|
| 151 | - 'sale_price' => $product->get_sale_price(), |
|
| 152 | - 'price' => $product->get_price(), |
|
| 153 | - 'currency_symbol' => get_woocommerce_currency_symbol(), |
|
| 154 | - 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 155 | - 'average_rating' => $product->get_average_rating(), |
|
| 156 | - 'rating_count' => $product->get_rating_count(), |
|
| 157 | - 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 158 | - ), |
|
| 159 | - 'entity' => array(), |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $filler_response; |
|
| 164 | - } |
|
| 13 | + /** |
|
| 14 | + * @var array<Filler_Posts> |
|
| 15 | + */ |
|
| 16 | + private $sources = array(); |
|
| 17 | + |
|
| 18 | + public function __construct( $post_id, $alternate_post_type = null ) { |
|
| 19 | + |
|
| 20 | + $post_type = $alternate_post_type ?: get_post_type( $post_id ); |
|
| 21 | + |
|
| 22 | + if ( $post_type === 'post' || ( is_array( $post_type ) && in_array( 'post', $post_type ) ) ) { |
|
| 23 | + $this->sources = array( |
|
| 24 | + new Same_Category_Filler_Posts( $post_id ), |
|
| 25 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 26 | + ); |
|
| 27 | + } else if ( $post_type === 'product' || ( is_array( $post_type ) && in_array( 'product', $post_type ) ) ) { |
|
| 28 | + $this->sources = array( |
|
| 29 | + new Same_Post_Type_Same_Category_Posts( $post_id, 'product' ), |
|
| 30 | + new Same_Post_Type_Filler_Posts( $post_id, 'product' ), |
|
| 31 | + ); |
|
| 32 | + } else { |
|
| 33 | + $this->sources = array( |
|
| 34 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 35 | + ); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param $posts array<\WP_Post> |
|
| 42 | + * |
|
| 43 | + * @return array<int> |
|
| 44 | + */ |
|
| 45 | + private function extract_post_ids( $posts ) { |
|
| 46 | + return array_map( function ( $post ) { |
|
| 47 | + /** |
|
| 48 | + * @var $post \WP_Post |
|
| 49 | + */ |
|
| 50 | + return $post->ID; |
|
| 51 | + }, $posts ); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 55 | + |
|
| 56 | + $filler_posts = array(); |
|
| 57 | + |
|
| 58 | + foreach ( $this->sources as $source ) { |
|
| 59 | + |
|
| 60 | + if ( $filler_count <= 0 ) { |
|
| 61 | + break; |
|
| 62 | + } |
|
| 63 | + /** |
|
| 64 | + * @var Filler_Posts $source |
|
| 65 | + */ |
|
| 66 | + $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
|
| 67 | + $source->filler_count = $filler_count; |
|
| 68 | + |
|
| 69 | + $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 70 | + $post_ids = $this->extract_post_ids( $posts ); |
|
| 71 | + |
|
| 72 | + // Update the post ids, filler posts and filler count |
|
| 73 | + $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 74 | + $filler_count = $filler_count - count( $posts ); |
|
| 75 | + $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 76 | + } |
|
| 77 | + $filler_posts = $this->add_additional_properties_to_filler_posts( $filler_posts ); |
|
| 78 | + |
|
| 79 | + return $filler_posts; |
|
| 80 | + |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @param $posts array<\WP_Post> |
|
| 85 | + * |
|
| 86 | + * @return array $posts array<\WP_Post> |
|
| 87 | + */ |
|
| 88 | + private function add_additional_properties_to_filler_posts( $posts ) { |
|
| 89 | + return array_map( function ( $post ) { |
|
| 90 | + $post->thumbnail = get_the_post_thumbnail_url( $post->ID, 'medium' ); |
|
| 91 | + $post->permalink = get_permalink( $post->ID ); |
|
| 92 | + $post->post_title = html_entity_decode( $post->post_title ); |
|
| 93 | + |
|
| 94 | + return $post; |
|
| 95 | + }, $posts ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Called by wordlift navigator, converts all the posts to response format. |
|
| 100 | + * |
|
| 101 | + * @param $filler_count |
|
| 102 | + * @param $post_ids_to_be_excluded |
|
| 103 | + * |
|
| 104 | + * @return array |
|
| 105 | + */ |
|
| 106 | + public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 107 | + $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 108 | + // Add thumbnail and permalink to filler posts |
|
| 109 | + $filler_response = array(); |
|
| 110 | + foreach ( $filler_posts as $post_obj ) { |
|
| 111 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 112 | + $filler_response[] = array( |
|
| 113 | + 'post' => array( |
|
| 114 | + 'id' => $post_obj->ID, |
|
| 115 | + 'permalink' => get_permalink( $post_obj->ID ), |
|
| 116 | + 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 117 | + 'title' => get_the_title( $post_obj->ID ), |
|
| 118 | + 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 119 | + ), |
|
| 120 | + 'entity' => array( |
|
| 121 | + 'id' => 0 |
|
| 122 | + ) |
|
| 123 | + ); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $filler_response; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Called by wordlift navigator, converts all the posts to response format. |
|
| 132 | + * |
|
| 133 | + * @param $filler_count |
|
| 134 | + * @param $post_ids_to_be_excluded |
|
| 135 | + * |
|
| 136 | + * @return array |
|
| 137 | + */ |
|
| 138 | + public function get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 139 | + $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 140 | + // Add thumbnail and permalink to filler posts |
|
| 141 | + $filler_response = array(); |
|
| 142 | + foreach ( $filler_posts as $post_obj ) { |
|
| 143 | + $product = wc_get_product( $post_obj->ID ); |
|
| 144 | + $filler_response[] = array( |
|
| 145 | + 'product' => array( |
|
| 146 | + 'id' => $post_obj->ID, |
|
| 147 | + 'permalink' => get_permalink( $post_obj->ID ), |
|
| 148 | + 'title' => $post_obj->post_title, |
|
| 149 | + 'thumbnail' => get_the_post_thumbnail_url( $post_obj->ID, 'medium' ), |
|
| 150 | + 'regular_price' => $product->get_regular_price(), |
|
| 151 | + 'sale_price' => $product->get_sale_price(), |
|
| 152 | + 'price' => $product->get_price(), |
|
| 153 | + 'currency_symbol' => get_woocommerce_currency_symbol(), |
|
| 154 | + 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 155 | + 'average_rating' => $product->get_average_rating(), |
|
| 156 | + 'rating_count' => $product->get_rating_count(), |
|
| 157 | + 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 158 | + ), |
|
| 159 | + 'entity' => array(), |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $filler_response; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | 166 | } |
@@ -15,23 +15,23 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | private $sources = array(); |
| 17 | 17 | |
| 18 | - public function __construct( $post_id, $alternate_post_type = null ) { |
|
| 18 | + public function __construct($post_id, $alternate_post_type = null) { |
|
| 19 | 19 | |
| 20 | - $post_type = $alternate_post_type ?: get_post_type( $post_id ); |
|
| 20 | + $post_type = $alternate_post_type ?: get_post_type($post_id); |
|
| 21 | 21 | |
| 22 | - if ( $post_type === 'post' || ( is_array( $post_type ) && in_array( 'post', $post_type ) ) ) { |
|
| 22 | + if ($post_type === 'post' || (is_array($post_type) && in_array('post', $post_type))) { |
|
| 23 | 23 | $this->sources = array( |
| 24 | - new Same_Category_Filler_Posts( $post_id ), |
|
| 25 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 24 | + new Same_Category_Filler_Posts($post_id), |
|
| 25 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 26 | 26 | ); |
| 27 | - } else if ( $post_type === 'product' || ( is_array( $post_type ) && in_array( 'product', $post_type ) ) ) { |
|
| 27 | + } else if ($post_type === 'product' || (is_array($post_type) && in_array('product', $post_type))) { |
|
| 28 | 28 | $this->sources = array( |
| 29 | - new Same_Post_Type_Same_Category_Posts( $post_id, 'product' ), |
|
| 30 | - new Same_Post_Type_Filler_Posts( $post_id, 'product' ), |
|
| 29 | + new Same_Post_Type_Same_Category_Posts($post_id, 'product'), |
|
| 30 | + new Same_Post_Type_Filler_Posts($post_id, 'product'), |
|
| 31 | 31 | ); |
| 32 | 32 | } else { |
| 33 | 33 | $this->sources = array( |
| 34 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 34 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 35 | 35 | ); |
| 36 | 36 | } |
| 37 | 37 | } |
@@ -42,22 +42,22 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @return array<int> |
| 44 | 44 | */ |
| 45 | - private function extract_post_ids( $posts ) { |
|
| 46 | - return array_map( function ( $post ) { |
|
| 45 | + private function extract_post_ids($posts) { |
|
| 46 | + return array_map(function($post) { |
|
| 47 | 47 | /** |
| 48 | 48 | * @var $post \WP_Post |
| 49 | 49 | */ |
| 50 | 50 | return $post->ID; |
| 51 | - }, $posts ); |
|
| 51 | + }, $posts); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 54 | + public function get_filler_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 55 | 55 | |
| 56 | 56 | $filler_posts = array(); |
| 57 | 57 | |
| 58 | - foreach ( $this->sources as $source ) { |
|
| 58 | + foreach ($this->sources as $source) { |
|
| 59 | 59 | |
| 60 | - if ( $filler_count <= 0 ) { |
|
| 60 | + if ($filler_count <= 0) { |
|
| 61 | 61 | break; |
| 62 | 62 | } |
| 63 | 63 | /** |
@@ -66,15 +66,15 @@ discard block |
||
| 66 | 66 | $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
| 67 | 67 | $source->filler_count = $filler_count; |
| 68 | 68 | |
| 69 | - $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 70 | - $post_ids = $this->extract_post_ids( $posts ); |
|
| 69 | + $posts = $source->get_posts($filler_count, $post_ids_to_be_excluded); |
|
| 70 | + $post_ids = $this->extract_post_ids($posts); |
|
| 71 | 71 | |
| 72 | 72 | // Update the post ids, filler posts and filler count |
| 73 | - $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 74 | - $filler_count = $filler_count - count( $posts ); |
|
| 75 | - $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 73 | + $post_ids_to_be_excluded = array_merge($post_ids_to_be_excluded, $post_ids); |
|
| 74 | + $filler_count = $filler_count - count($posts); |
|
| 75 | + $filler_posts = array_merge($filler_posts, $posts); |
|
| 76 | 76 | } |
| 77 | - $filler_posts = $this->add_additional_properties_to_filler_posts( $filler_posts ); |
|
| 77 | + $filler_posts = $this->add_additional_properties_to_filler_posts($filler_posts); |
|
| 78 | 78 | |
| 79 | 79 | return $filler_posts; |
| 80 | 80 | |
@@ -85,14 +85,14 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return array $posts array<\WP_Post> |
| 87 | 87 | */ |
| 88 | - private function add_additional_properties_to_filler_posts( $posts ) { |
|
| 89 | - return array_map( function ( $post ) { |
|
| 90 | - $post->thumbnail = get_the_post_thumbnail_url( $post->ID, 'medium' ); |
|
| 91 | - $post->permalink = get_permalink( $post->ID ); |
|
| 92 | - $post->post_title = html_entity_decode( $post->post_title ); |
|
| 88 | + private function add_additional_properties_to_filler_posts($posts) { |
|
| 89 | + return array_map(function($post) { |
|
| 90 | + $post->thumbnail = get_the_post_thumbnail_url($post->ID, 'medium'); |
|
| 91 | + $post->permalink = get_permalink($post->ID); |
|
| 92 | + $post->post_title = html_entity_decode($post->post_title); |
|
| 93 | 93 | |
| 94 | 94 | return $post; |
| 95 | - }, $posts ); |
|
| 95 | + }, $posts); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
@@ -103,19 +103,19 @@ discard block |
||
| 103 | 103 | * |
| 104 | 104 | * @return array |
| 105 | 105 | */ |
| 106 | - public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 107 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 106 | + public function get_filler_response($filler_count, $post_ids_to_be_excluded) { |
|
| 107 | + $filler_posts = $this->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 108 | 108 | // Add thumbnail and permalink to filler posts |
| 109 | 109 | $filler_response = array(); |
| 110 | - foreach ( $filler_posts as $post_obj ) { |
|
| 111 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 110 | + foreach ($filler_posts as $post_obj) { |
|
| 111 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 112 | 112 | $filler_response[] = array( |
| 113 | 113 | 'post' => array( |
| 114 | 114 | 'id' => $post_obj->ID, |
| 115 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 116 | - 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 117 | - 'title' => get_the_title( $post_obj->ID ), |
|
| 118 | - 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 115 | + 'permalink' => get_permalink($post_obj->ID), |
|
| 116 | + 'thumbnail' => ($thumbnail) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 117 | + 'title' => get_the_title($post_obj->ID), |
|
| 118 | + 'srcset' => Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET) |
|
| 119 | 119 | ), |
| 120 | 120 | 'entity' => array( |
| 121 | 121 | 'id' => 0 |
@@ -135,26 +135,26 @@ discard block |
||
| 135 | 135 | * |
| 136 | 136 | * @return array |
| 137 | 137 | */ |
| 138 | - public function get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 139 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 138 | + public function get_product_navigator_response($filler_count, $post_ids_to_be_excluded) { |
|
| 139 | + $filler_posts = $this->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 140 | 140 | // Add thumbnail and permalink to filler posts |
| 141 | 141 | $filler_response = array(); |
| 142 | - foreach ( $filler_posts as $post_obj ) { |
|
| 143 | - $product = wc_get_product( $post_obj->ID ); |
|
| 142 | + foreach ($filler_posts as $post_obj) { |
|
| 143 | + $product = wc_get_product($post_obj->ID); |
|
| 144 | 144 | $filler_response[] = array( |
| 145 | 145 | 'product' => array( |
| 146 | 146 | 'id' => $post_obj->ID, |
| 147 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 147 | + 'permalink' => get_permalink($post_obj->ID), |
|
| 148 | 148 | 'title' => $post_obj->post_title, |
| 149 | - 'thumbnail' => get_the_post_thumbnail_url( $post_obj->ID, 'medium' ), |
|
| 149 | + 'thumbnail' => get_the_post_thumbnail_url($post_obj->ID, 'medium'), |
|
| 150 | 150 | 'regular_price' => $product->get_regular_price(), |
| 151 | 151 | 'sale_price' => $product->get_sale_price(), |
| 152 | 152 | 'price' => $product->get_price(), |
| 153 | 153 | 'currency_symbol' => get_woocommerce_currency_symbol(), |
| 154 | - 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 154 | + 'discount_pc' => ($product->get_sale_price() && ($product->get_regular_price() > 0)) ? round(1 - ($product->get_sale_price() / $product->get_regular_price()), 2) * 100 : 0, |
|
| 155 | 155 | 'average_rating' => $product->get_average_rating(), |
| 156 | 156 | 'rating_count' => $product->get_rating_count(), |
| 157 | - 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 157 | + 'rating_html' => wc_get_rating_html($product->get_average_rating(), $product->get_rating_count()) |
|
| 158 | 158 | ), |
| 159 | 159 | 'entity' => array(), |
| 160 | 160 | ); |
@@ -14,213 +14,213 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Cached_Entity_Uri_Service extends Wordlift_Entity_Uri_Service { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The {@link Wordlift_Cache_Service} instance. |
|
| 19 | - * |
|
| 20 | - * @since 3.16.3 |
|
| 21 | - * @access private |
|
| 22 | - * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance. |
|
| 23 | - */ |
|
| 24 | - private $cache_service; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * A {@link Wordlift_Log_Service} instance. |
|
| 28 | - * |
|
| 29 | - * @since 3.16.3 |
|
| 30 | - * @access private |
|
| 31 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 32 | - */ |
|
| 33 | - private $log; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Create a {@link Wordlift_Cached_Entity_Uri_Service} instance. |
|
| 37 | - * |
|
| 38 | - * @param \Wordlift_Configuration_Service $configuration_service |
|
| 39 | - * @param \Wordlift_Cache_Service $cache_service |
|
| 40 | - * |
|
| 41 | - * @since 3.16.3 |
|
| 42 | - * |
|
| 43 | - */ |
|
| 44 | - public function __construct( $configuration_service, $cache_service ) { |
|
| 45 | - parent::__construct( $configuration_service ); |
|
| 46 | - |
|
| 47 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 48 | - |
|
| 49 | - // Add hooks for meta being added/modified/deleted. |
|
| 50 | - $this->cache_service = $cache_service; |
|
| 51 | - |
|
| 52 | - add_action( 'add_post_meta', array( |
|
| 53 | - $this, |
|
| 54 | - 'on_before_post_meta_add', |
|
| 55 | - ), 10, 3 ); |
|
| 56 | - add_action( 'update_post_meta', array( |
|
| 57 | - $this, |
|
| 58 | - 'on_before_post_meta_change', |
|
| 59 | - ), 10, 4 ); |
|
| 60 | - add_action( 'delete_post_meta', array( |
|
| 61 | - $this, |
|
| 62 | - 'on_before_post_meta_change', |
|
| 63 | - ), 10, 4 ); |
|
| 64 | - |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Preload the URIs. |
|
| 69 | - * |
|
| 70 | - * @param array $uris Preload an array of URIs. |
|
| 71 | - * |
|
| 72 | - * @since 3.16.3 |
|
| 73 | - * |
|
| 74 | - */ |
|
| 75 | - public function preload_uris( $uris ) { |
|
| 76 | - |
|
| 77 | - // Filter the URIs which aren't yet cached. |
|
| 78 | - $cache_service = $this->cache_service; |
|
| 79 | - $uris_to_cache = array_filter( (array) $uris, function ( $item ) use ( $cache_service ) { |
|
| 80 | - return ! $cache_service->has_cache( $item ); |
|
| 81 | - } ); |
|
| 82 | - |
|
| 83 | - // Preload the URIs. |
|
| 84 | - parent::preload_uris( $uris_to_cache ); |
|
| 85 | - |
|
| 86 | - // Store them in cache. |
|
| 87 | - if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
| 88 | - foreach ( $this->uri_to_post as $uri => $post ) { |
|
| 89 | - $this->set_cache( $uri, get_post( $post ) ); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Get the entity post for the specified URI. |
|
| 97 | - * |
|
| 98 | - * @param string $uri The URI. |
|
| 99 | - * |
|
| 100 | - * @return null|WP_Post The {@link WP_Post} or null if not found. |
|
| 101 | - * @since 3.16.3 |
|
| 102 | - * |
|
| 103 | - */ |
|
| 104 | - public function get_entity( $uri ) { |
|
| 105 | - |
|
| 106 | - $this->log->trace( "Getting entity for uri $uri..." ); |
|
| 107 | - |
|
| 108 | - // Get the cached post for the specified URI. |
|
| 109 | - $cache = $this->cache_service->get_cache( $uri ); |
|
| 110 | - |
|
| 111 | - // Return the cached data if valid. |
|
| 112 | - if ( false !== $cache && is_numeric( $cache ) ) { |
|
| 113 | - $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
| 114 | - |
|
| 115 | - return get_post( $cache ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // Get the actual result. |
|
| 119 | - $post = parent::get_entity( $uri ); |
|
| 120 | - |
|
| 121 | - // Cache the result, if a post is not found we cache -1. |
|
| 122 | - $this->set_cache( $uri, $post ); |
|
| 123 | - |
|
| 124 | - // Return the result. |
|
| 125 | - return $post; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Set the cached URI for the specified {@link WP_Post}. |
|
| 130 | - * |
|
| 131 | - * @param string $uri The URI. |
|
| 132 | - * @param WP_Post $post The post. |
|
| 133 | - * |
|
| 134 | - * @since 3.16.3 |
|
| 135 | - * |
|
| 136 | - */ |
|
| 137 | - private function set_cache( $uri, $post ) { |
|
| 138 | - // Cache the result, if a post is not found we cache -1. |
|
| 139 | - $this->cache_service->set_cache( $uri, ( null === $post ? - 1 : $post->ID ) ); |
|
| 140 | - |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Delete the cache for the specified URIs. |
|
| 145 | - * |
|
| 146 | - * @param array $uris An array of URIs. |
|
| 147 | - * |
|
| 148 | - * @since 3.16.3 |
|
| 149 | - * |
|
| 150 | - */ |
|
| 151 | - private function delete_cache( $uris ) { |
|
| 152 | - |
|
| 153 | - // Delete the cache for each URI. |
|
| 154 | - foreach ( $uris as $uri ) { |
|
| 155 | - // Delete the single cache file. |
|
| 156 | - $this->cache_service->delete_cache( $uri ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Before post meta change. |
|
| 163 | - * |
|
| 164 | - * When a post meta is going to be changed, we check if the `meta_key` is |
|
| 165 | - * either the `entity_url` or the `same_as` in which case we delete the cache |
|
| 166 | - * for all the associated URIs. |
|
| 167 | - * |
|
| 168 | - * @param int|array $meta_ids The {@link WP_Post} meta id(s). |
|
| 169 | - * @param int $post_id The {@link WP_Post} id. |
|
| 170 | - * @param string $meta_key The meta key. |
|
| 171 | - * @param mixed $meta_value The meta value(s). |
|
| 172 | - * |
|
| 173 | - * @since 3.16.3 |
|
| 174 | - * |
|
| 175 | - */ |
|
| 176 | - public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
| 177 | - |
|
| 178 | - // Bail out if we're not interested in the meta key. |
|
| 179 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 180 | - return; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 184 | - |
|
| 185 | - // The list of existing URIs, plus the list of URIs being deleted/updated. |
|
| 186 | - $old_value = get_post_meta( $post_id, $meta_key ); |
|
| 187 | - |
|
| 188 | - // We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects |
|
| 189 | - // because of filters. So if it's not we return an empty array. |
|
| 190 | - if ( ! is_array( $old_value ) ) { |
|
| 191 | - $old_value = array(); |
|
| 192 | - } |
|
| 193 | - $new_value = isset( $meta_value ) ? (array) $meta_value : array(); |
|
| 194 | - $uris = array_merge( $old_value, $new_value ); |
|
| 195 | - |
|
| 196 | - // Delete the cache for those URIs. |
|
| 197 | - $this->delete_cache( $uris ); |
|
| 198 | - |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Hook to meta add for a {@link WP_Post}, will cause the cache to |
|
| 203 | - * invalidate. |
|
| 204 | - * |
|
| 205 | - * @param int $post_id The {@link WP_Post} id. |
|
| 206 | - * @param string $meta_key The meta key. |
|
| 207 | - * @param mixed $meta_value The meta value(s). |
|
| 208 | - * |
|
| 209 | - * @since 3.16.3 |
|
| 210 | - * |
|
| 211 | - */ |
|
| 212 | - public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
| 213 | - |
|
| 214 | - // Bail out if we're not interested in the meta key. |
|
| 215 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 216 | - return; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 220 | - |
|
| 221 | - // Delete the cache for the URIs being added. |
|
| 222 | - $this->delete_cache( (array) $meta_value ); |
|
| 223 | - |
|
| 224 | - } |
|
| 17 | + /** |
|
| 18 | + * The {@link Wordlift_Cache_Service} instance. |
|
| 19 | + * |
|
| 20 | + * @since 3.16.3 |
|
| 21 | + * @access private |
|
| 22 | + * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance. |
|
| 23 | + */ |
|
| 24 | + private $cache_service; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * A {@link Wordlift_Log_Service} instance. |
|
| 28 | + * |
|
| 29 | + * @since 3.16.3 |
|
| 30 | + * @access private |
|
| 31 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 32 | + */ |
|
| 33 | + private $log; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Create a {@link Wordlift_Cached_Entity_Uri_Service} instance. |
|
| 37 | + * |
|
| 38 | + * @param \Wordlift_Configuration_Service $configuration_service |
|
| 39 | + * @param \Wordlift_Cache_Service $cache_service |
|
| 40 | + * |
|
| 41 | + * @since 3.16.3 |
|
| 42 | + * |
|
| 43 | + */ |
|
| 44 | + public function __construct( $configuration_service, $cache_service ) { |
|
| 45 | + parent::__construct( $configuration_service ); |
|
| 46 | + |
|
| 47 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 48 | + |
|
| 49 | + // Add hooks for meta being added/modified/deleted. |
|
| 50 | + $this->cache_service = $cache_service; |
|
| 51 | + |
|
| 52 | + add_action( 'add_post_meta', array( |
|
| 53 | + $this, |
|
| 54 | + 'on_before_post_meta_add', |
|
| 55 | + ), 10, 3 ); |
|
| 56 | + add_action( 'update_post_meta', array( |
|
| 57 | + $this, |
|
| 58 | + 'on_before_post_meta_change', |
|
| 59 | + ), 10, 4 ); |
|
| 60 | + add_action( 'delete_post_meta', array( |
|
| 61 | + $this, |
|
| 62 | + 'on_before_post_meta_change', |
|
| 63 | + ), 10, 4 ); |
|
| 64 | + |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Preload the URIs. |
|
| 69 | + * |
|
| 70 | + * @param array $uris Preload an array of URIs. |
|
| 71 | + * |
|
| 72 | + * @since 3.16.3 |
|
| 73 | + * |
|
| 74 | + */ |
|
| 75 | + public function preload_uris( $uris ) { |
|
| 76 | + |
|
| 77 | + // Filter the URIs which aren't yet cached. |
|
| 78 | + $cache_service = $this->cache_service; |
|
| 79 | + $uris_to_cache = array_filter( (array) $uris, function ( $item ) use ( $cache_service ) { |
|
| 80 | + return ! $cache_service->has_cache( $item ); |
|
| 81 | + } ); |
|
| 82 | + |
|
| 83 | + // Preload the URIs. |
|
| 84 | + parent::preload_uris( $uris_to_cache ); |
|
| 85 | + |
|
| 86 | + // Store them in cache. |
|
| 87 | + if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
| 88 | + foreach ( $this->uri_to_post as $uri => $post ) { |
|
| 89 | + $this->set_cache( $uri, get_post( $post ) ); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Get the entity post for the specified URI. |
|
| 97 | + * |
|
| 98 | + * @param string $uri The URI. |
|
| 99 | + * |
|
| 100 | + * @return null|WP_Post The {@link WP_Post} or null if not found. |
|
| 101 | + * @since 3.16.3 |
|
| 102 | + * |
|
| 103 | + */ |
|
| 104 | + public function get_entity( $uri ) { |
|
| 105 | + |
|
| 106 | + $this->log->trace( "Getting entity for uri $uri..." ); |
|
| 107 | + |
|
| 108 | + // Get the cached post for the specified URI. |
|
| 109 | + $cache = $this->cache_service->get_cache( $uri ); |
|
| 110 | + |
|
| 111 | + // Return the cached data if valid. |
|
| 112 | + if ( false !== $cache && is_numeric( $cache ) ) { |
|
| 113 | + $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
| 114 | + |
|
| 115 | + return get_post( $cache ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // Get the actual result. |
|
| 119 | + $post = parent::get_entity( $uri ); |
|
| 120 | + |
|
| 121 | + // Cache the result, if a post is not found we cache -1. |
|
| 122 | + $this->set_cache( $uri, $post ); |
|
| 123 | + |
|
| 124 | + // Return the result. |
|
| 125 | + return $post; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Set the cached URI for the specified {@link WP_Post}. |
|
| 130 | + * |
|
| 131 | + * @param string $uri The URI. |
|
| 132 | + * @param WP_Post $post The post. |
|
| 133 | + * |
|
| 134 | + * @since 3.16.3 |
|
| 135 | + * |
|
| 136 | + */ |
|
| 137 | + private function set_cache( $uri, $post ) { |
|
| 138 | + // Cache the result, if a post is not found we cache -1. |
|
| 139 | + $this->cache_service->set_cache( $uri, ( null === $post ? - 1 : $post->ID ) ); |
|
| 140 | + |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Delete the cache for the specified URIs. |
|
| 145 | + * |
|
| 146 | + * @param array $uris An array of URIs. |
|
| 147 | + * |
|
| 148 | + * @since 3.16.3 |
|
| 149 | + * |
|
| 150 | + */ |
|
| 151 | + private function delete_cache( $uris ) { |
|
| 152 | + |
|
| 153 | + // Delete the cache for each URI. |
|
| 154 | + foreach ( $uris as $uri ) { |
|
| 155 | + // Delete the single cache file. |
|
| 156 | + $this->cache_service->delete_cache( $uri ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Before post meta change. |
|
| 163 | + * |
|
| 164 | + * When a post meta is going to be changed, we check if the `meta_key` is |
|
| 165 | + * either the `entity_url` or the `same_as` in which case we delete the cache |
|
| 166 | + * for all the associated URIs. |
|
| 167 | + * |
|
| 168 | + * @param int|array $meta_ids The {@link WP_Post} meta id(s). |
|
| 169 | + * @param int $post_id The {@link WP_Post} id. |
|
| 170 | + * @param string $meta_key The meta key. |
|
| 171 | + * @param mixed $meta_value The meta value(s). |
|
| 172 | + * |
|
| 173 | + * @since 3.16.3 |
|
| 174 | + * |
|
| 175 | + */ |
|
| 176 | + public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
| 177 | + |
|
| 178 | + // Bail out if we're not interested in the meta key. |
|
| 179 | + if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 180 | + return; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 184 | + |
|
| 185 | + // The list of existing URIs, plus the list of URIs being deleted/updated. |
|
| 186 | + $old_value = get_post_meta( $post_id, $meta_key ); |
|
| 187 | + |
|
| 188 | + // We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects |
|
| 189 | + // because of filters. So if it's not we return an empty array. |
|
| 190 | + if ( ! is_array( $old_value ) ) { |
|
| 191 | + $old_value = array(); |
|
| 192 | + } |
|
| 193 | + $new_value = isset( $meta_value ) ? (array) $meta_value : array(); |
|
| 194 | + $uris = array_merge( $old_value, $new_value ); |
|
| 195 | + |
|
| 196 | + // Delete the cache for those URIs. |
|
| 197 | + $this->delete_cache( $uris ); |
|
| 198 | + |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Hook to meta add for a {@link WP_Post}, will cause the cache to |
|
| 203 | + * invalidate. |
|
| 204 | + * |
|
| 205 | + * @param int $post_id The {@link WP_Post} id. |
|
| 206 | + * @param string $meta_key The meta key. |
|
| 207 | + * @param mixed $meta_value The meta value(s). |
|
| 208 | + * |
|
| 209 | + * @since 3.16.3 |
|
| 210 | + * |
|
| 211 | + */ |
|
| 212 | + public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
| 213 | + |
|
| 214 | + // Bail out if we're not interested in the meta key. |
|
| 215 | + if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 216 | + return; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 220 | + |
|
| 221 | + // Delete the cache for the URIs being added. |
|
| 222 | + $this->delete_cache( (array) $meta_value ); |
|
| 223 | + |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | 226 | } |
@@ -41,26 +41,26 @@ discard block |
||
| 41 | 41 | * @since 3.16.3 |
| 42 | 42 | * |
| 43 | 43 | */ |
| 44 | - public function __construct( $configuration_service, $cache_service ) { |
|
| 45 | - parent::__construct( $configuration_service ); |
|
| 44 | + public function __construct($configuration_service, $cache_service) { |
|
| 45 | + parent::__construct($configuration_service); |
|
| 46 | 46 | |
| 47 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 47 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 48 | 48 | |
| 49 | 49 | // Add hooks for meta being added/modified/deleted. |
| 50 | 50 | $this->cache_service = $cache_service; |
| 51 | 51 | |
| 52 | - add_action( 'add_post_meta', array( |
|
| 52 | + add_action('add_post_meta', array( |
|
| 53 | 53 | $this, |
| 54 | 54 | 'on_before_post_meta_add', |
| 55 | - ), 10, 3 ); |
|
| 56 | - add_action( 'update_post_meta', array( |
|
| 55 | + ), 10, 3); |
|
| 56 | + add_action('update_post_meta', array( |
|
| 57 | 57 | $this, |
| 58 | 58 | 'on_before_post_meta_change', |
| 59 | - ), 10, 4 ); |
|
| 60 | - add_action( 'delete_post_meta', array( |
|
| 59 | + ), 10, 4); |
|
| 60 | + add_action('delete_post_meta', array( |
|
| 61 | 61 | $this, |
| 62 | 62 | 'on_before_post_meta_change', |
| 63 | - ), 10, 4 ); |
|
| 63 | + ), 10, 4); |
|
| 64 | 64 | |
| 65 | 65 | } |
| 66 | 66 | |
@@ -72,21 +72,21 @@ discard block |
||
| 72 | 72 | * @since 3.16.3 |
| 73 | 73 | * |
| 74 | 74 | */ |
| 75 | - public function preload_uris( $uris ) { |
|
| 75 | + public function preload_uris($uris) { |
|
| 76 | 76 | |
| 77 | 77 | // Filter the URIs which aren't yet cached. |
| 78 | 78 | $cache_service = $this->cache_service; |
| 79 | - $uris_to_cache = array_filter( (array) $uris, function ( $item ) use ( $cache_service ) { |
|
| 80 | - return ! $cache_service->has_cache( $item ); |
|
| 79 | + $uris_to_cache = array_filter((array) $uris, function($item) use ($cache_service) { |
|
| 80 | + return ! $cache_service->has_cache($item); |
|
| 81 | 81 | } ); |
| 82 | 82 | |
| 83 | 83 | // Preload the URIs. |
| 84 | - parent::preload_uris( $uris_to_cache ); |
|
| 84 | + parent::preload_uris($uris_to_cache); |
|
| 85 | 85 | |
| 86 | 86 | // Store them in cache. |
| 87 | - if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
| 88 | - foreach ( $this->uri_to_post as $uri => $post ) { |
|
| 89 | - $this->set_cache( $uri, get_post( $post ) ); |
|
| 87 | + if (is_array($this->uri_to_post) && ! empty($this->uri_to_post)) { |
|
| 88 | + foreach ($this->uri_to_post as $uri => $post) { |
|
| 89 | + $this->set_cache($uri, get_post($post)); |
|
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | |
@@ -101,25 +101,25 @@ discard block |
||
| 101 | 101 | * @since 3.16.3 |
| 102 | 102 | * |
| 103 | 103 | */ |
| 104 | - public function get_entity( $uri ) { |
|
| 104 | + public function get_entity($uri) { |
|
| 105 | 105 | |
| 106 | - $this->log->trace( "Getting entity for uri $uri..." ); |
|
| 106 | + $this->log->trace("Getting entity for uri $uri..."); |
|
| 107 | 107 | |
| 108 | 108 | // Get the cached post for the specified URI. |
| 109 | - $cache = $this->cache_service->get_cache( $uri ); |
|
| 109 | + $cache = $this->cache_service->get_cache($uri); |
|
| 110 | 110 | |
| 111 | 111 | // Return the cached data if valid. |
| 112 | - if ( false !== $cache && is_numeric( $cache ) ) { |
|
| 113 | - $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
| 112 | + if (false !== $cache && is_numeric($cache)) { |
|
| 113 | + $this->log->debug("Cached entity $cache for uri $uri found."); |
|
| 114 | 114 | |
| 115 | - return get_post( $cache ); |
|
| 115 | + return get_post($cache); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | // Get the actual result. |
| 119 | - $post = parent::get_entity( $uri ); |
|
| 119 | + $post = parent::get_entity($uri); |
|
| 120 | 120 | |
| 121 | 121 | // Cache the result, if a post is not found we cache -1. |
| 122 | - $this->set_cache( $uri, $post ); |
|
| 122 | + $this->set_cache($uri, $post); |
|
| 123 | 123 | |
| 124 | 124 | // Return the result. |
| 125 | 125 | return $post; |
@@ -134,9 +134,9 @@ discard block |
||
| 134 | 134 | * @since 3.16.3 |
| 135 | 135 | * |
| 136 | 136 | */ |
| 137 | - private function set_cache( $uri, $post ) { |
|
| 137 | + private function set_cache($uri, $post) { |
|
| 138 | 138 | // Cache the result, if a post is not found we cache -1. |
| 139 | - $this->cache_service->set_cache( $uri, ( null === $post ? - 1 : $post->ID ) ); |
|
| 139 | + $this->cache_service->set_cache($uri, (null === $post ? -1 : $post->ID)); |
|
| 140 | 140 | |
| 141 | 141 | } |
| 142 | 142 | |
@@ -148,12 +148,12 @@ discard block |
||
| 148 | 148 | * @since 3.16.3 |
| 149 | 149 | * |
| 150 | 150 | */ |
| 151 | - private function delete_cache( $uris ) { |
|
| 151 | + private function delete_cache($uris) { |
|
| 152 | 152 | |
| 153 | 153 | // Delete the cache for each URI. |
| 154 | - foreach ( $uris as $uri ) { |
|
| 154 | + foreach ($uris as $uri) { |
|
| 155 | 155 | // Delete the single cache file. |
| 156 | - $this->cache_service->delete_cache( $uri ); |
|
| 156 | + $this->cache_service->delete_cache($uri); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | } |
@@ -173,28 +173,28 @@ discard block |
||
| 173 | 173 | * @since 3.16.3 |
| 174 | 174 | * |
| 175 | 175 | */ |
| 176 | - public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
| 176 | + public function on_before_post_meta_change($meta_ids, $post_id, $meta_key, $meta_value) { |
|
| 177 | 177 | |
| 178 | 178 | // Bail out if we're not interested in the meta key. |
| 179 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 179 | + if (WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key) { |
|
| 180 | 180 | return; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 183 | + $this->log->trace("Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..."); |
|
| 184 | 184 | |
| 185 | 185 | // The list of existing URIs, plus the list of URIs being deleted/updated. |
| 186 | - $old_value = get_post_meta( $post_id, $meta_key ); |
|
| 186 | + $old_value = get_post_meta($post_id, $meta_key); |
|
| 187 | 187 | |
| 188 | 188 | // We expect an array here from the `get_post_meta` signature. However `get_post_meta` is prone to side effects |
| 189 | 189 | // because of filters. So if it's not we return an empty array. |
| 190 | - if ( ! is_array( $old_value ) ) { |
|
| 190 | + if ( ! is_array($old_value)) { |
|
| 191 | 191 | $old_value = array(); |
| 192 | 192 | } |
| 193 | - $new_value = isset( $meta_value ) ? (array) $meta_value : array(); |
|
| 194 | - $uris = array_merge( $old_value, $new_value ); |
|
| 193 | + $new_value = isset($meta_value) ? (array) $meta_value : array(); |
|
| 194 | + $uris = array_merge($old_value, $new_value); |
|
| 195 | 195 | |
| 196 | 196 | // Delete the cache for those URIs. |
| 197 | - $this->delete_cache( $uris ); |
|
| 197 | + $this->delete_cache($uris); |
|
| 198 | 198 | |
| 199 | 199 | } |
| 200 | 200 | |
@@ -209,17 +209,17 @@ discard block |
||
| 209 | 209 | * @since 3.16.3 |
| 210 | 210 | * |
| 211 | 211 | */ |
| 212 | - public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
| 212 | + public function on_before_post_meta_add($post_id, $meta_key, $meta_value) { |
|
| 213 | 213 | |
| 214 | 214 | // Bail out if we're not interested in the meta key. |
| 215 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 215 | + if (WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key) { |
|
| 216 | 216 | return; |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | - $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 219 | + $this->log->trace("Adding $meta_key for post $post_id ($meta_key), invalidating cache..."); |
|
| 220 | 220 | |
| 221 | 221 | // Delete the cache for the URIs being added. |
| 222 | - $this->delete_cache( (array) $meta_value ); |
|
| 222 | + $this->delete_cache((array) $meta_value); |
|
| 223 | 223 | |
| 224 | 224 | } |
| 225 | 225 | |