@@ -8,13 +8,13 @@ |
||
| 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 ); |
|
| 11 | + function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 12 | + $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 13 | 13 | |
| 14 | - return get_posts( array( |
|
| 15 | - 'category__in' => $current_post_categories, |
|
| 16 | - 'post_type' => get_post_type( $this->post_id ) |
|
| 17 | - ) |
|
| 18 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 19 | - } |
|
| 14 | + return get_posts( array( |
|
| 15 | + 'category__in' => $current_post_categories, |
|
| 16 | + 'post_type' => get_post_type( $this->post_id ) |
|
| 17 | + ) |
|
| 18 | + + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 19 | + } |
|
| 20 | 20 | } |
@@ -8,13 +8,13 @@ |
||
| 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 ); |
|
| 11 | + function get_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 12 | + $current_post_categories = wp_get_post_categories($this->post_id); |
|
| 13 | 13 | |
| 14 | - return get_posts( array( |
|
| 14 | + return get_posts(array( |
|
| 15 | 15 | 'category__in' => $current_post_categories, |
| 16 | - 'post_type' => get_post_type( $this->post_id ) |
|
| 16 | + 'post_type' => get_post_type($this->post_id) |
|
| 17 | 17 | ) |
| 18 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 18 | + + $this->get_posts_config($filler_count, $post_ids_to_be_excluded)); |
|
| 19 | 19 | } |
| 20 | 20 | } |
@@ -4,203 +4,203 @@ 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 | - 'label' => $serialized_entity['label'], |
|
| 105 | - 'mainType' => $serialized_entity['mainType'], |
|
| 106 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 107 | - ), |
|
| 108 | - ); |
|
| 109 | - |
|
| 110 | - $result['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 111 | - $result['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 112 | - |
|
| 113 | - $results[] = $result; |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if ( count( $results ) < $navigator_length ) { |
|
| 118 | - $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - // Add filler posts if needed |
|
| 123 | - $filler_count = $navigator_length - count( $results ); |
|
| 124 | - if ( $filler_count > 0 ) { |
|
| 125 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 126 | - return $p->ID; |
|
| 127 | - }, $referencing_posts ); |
|
| 128 | - /** |
|
| 129 | - * @since 3.28.0 |
|
| 130 | - * Filler posts are fetched using this util. |
|
| 131 | - */ |
|
| 132 | - $filler_posts_util = new Filler_Posts_Util( $post_id ); |
|
| 133 | - $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 134 | - $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 135 | - $results = array_merge( $results, $filler_posts ); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - return $amp ? array( |
|
| 140 | - 'items' => array( |
|
| 141 | - array( 'values' => $results ), |
|
| 142 | - ), |
|
| 143 | - ) : $results; |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 148 | - |
|
| 149 | - $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 150 | - |
|
| 151 | - $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 152 | - |
|
| 153 | - $directly_referencing_posts = get_posts( array( |
|
| 154 | - 'meta_query' => array( |
|
| 155 | - array( |
|
| 156 | - 'key' => '_thumbnail_id' |
|
| 157 | - ), |
|
| 158 | - array( |
|
| 159 | - 'key' => '_stock_status', |
|
| 160 | - 'value' => 'instock' |
|
| 161 | - ) |
|
| 162 | - ), |
|
| 163 | - 'post__in' => $post__in, |
|
| 164 | - 'post_type' => 'product', |
|
| 165 | - 'ignore_sticky_posts' => 1 |
|
| 166 | - ) ); |
|
| 167 | - |
|
| 168 | - $results = array(); |
|
| 169 | - |
|
| 170 | - foreach ( $directly_referencing_posts as $post ) { |
|
| 171 | - $result = new stdClass(); |
|
| 172 | - $result->ID = $post->ID; |
|
| 173 | - $result->post_title = $post->post_title; |
|
| 174 | - $result->entity_id = $post->ID; |
|
| 175 | - $results[] = $result; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return $results; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - private function get_entity_results( |
|
| 182 | - $post_id, |
|
| 183 | - $fields = array( |
|
| 184 | - 'ID', |
|
| 185 | - 'post_title', |
|
| 186 | - ), |
|
| 187 | - $order_by = 'ID DESC', |
|
| 188 | - $limit = 10, |
|
| 189 | - $offset = 0 |
|
| 190 | - ) { |
|
| 191 | - global $wpdb; |
|
| 192 | - |
|
| 193 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 194 | - return "p.$item AS $item"; |
|
| 195 | - }, (array) $fields ) ); |
|
| 196 | - |
|
| 197 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 198 | - return "p.$item"; |
|
| 199 | - }, (array) $order_by ) ); |
|
| 200 | - |
|
| 201 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 202 | - return $wpdb->get_results( |
|
| 203 | - $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 | + 'label' => $serialized_entity['label'], |
|
| 105 | + 'mainType' => $serialized_entity['mainType'], |
|
| 106 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 107 | + ), |
|
| 108 | + ); |
|
| 109 | + |
|
| 110 | + $result['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 111 | + $result['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 112 | + |
|
| 113 | + $results[] = $result; |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if ( count( $results ) < $navigator_length ) { |
|
| 118 | + $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + // Add filler posts if needed |
|
| 123 | + $filler_count = $navigator_length - count( $results ); |
|
| 124 | + if ( $filler_count > 0 ) { |
|
| 125 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 126 | + return $p->ID; |
|
| 127 | + }, $referencing_posts ); |
|
| 128 | + /** |
|
| 129 | + * @since 3.28.0 |
|
| 130 | + * Filler posts are fetched using this util. |
|
| 131 | + */ |
|
| 132 | + $filler_posts_util = new Filler_Posts_Util( $post_id ); |
|
| 133 | + $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 134 | + $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 135 | + $results = array_merge( $results, $filler_posts ); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + return $amp ? array( |
|
| 140 | + 'items' => array( |
|
| 141 | + array( 'values' => $results ), |
|
| 142 | + ), |
|
| 143 | + ) : $results; |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 148 | + |
|
| 149 | + $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 150 | + |
|
| 151 | + $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 152 | + |
|
| 153 | + $directly_referencing_posts = get_posts( array( |
|
| 154 | + 'meta_query' => array( |
|
| 155 | + array( |
|
| 156 | + 'key' => '_thumbnail_id' |
|
| 157 | + ), |
|
| 158 | + array( |
|
| 159 | + 'key' => '_stock_status', |
|
| 160 | + 'value' => 'instock' |
|
| 161 | + ) |
|
| 162 | + ), |
|
| 163 | + 'post__in' => $post__in, |
|
| 164 | + 'post_type' => 'product', |
|
| 165 | + 'ignore_sticky_posts' => 1 |
|
| 166 | + ) ); |
|
| 167 | + |
|
| 168 | + $results = array(); |
|
| 169 | + |
|
| 170 | + foreach ( $directly_referencing_posts as $post ) { |
|
| 171 | + $result = new stdClass(); |
|
| 172 | + $result->ID = $post->ID; |
|
| 173 | + $result->post_title = $post->post_title; |
|
| 174 | + $result->entity_id = $post->ID; |
|
| 175 | + $results[] = $result; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return $results; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + private function get_entity_results( |
|
| 182 | + $post_id, |
|
| 183 | + $fields = array( |
|
| 184 | + 'ID', |
|
| 185 | + 'post_title', |
|
| 186 | + ), |
|
| 187 | + $order_by = 'ID DESC', |
|
| 188 | + $limit = 10, |
|
| 189 | + $offset = 0 |
|
| 190 | + ) { |
|
| 191 | + global $wpdb; |
|
| 192 | + |
|
| 193 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 194 | + return "p.$item AS $item"; |
|
| 195 | + }, (array) $fields ) ); |
|
| 196 | + |
|
| 197 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 198 | + return "p.$item"; |
|
| 199 | + }, (array) $order_by ) ); |
|
| 200 | + |
|
| 201 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 202 | + return $wpdb->get_results( |
|
| 203 | + $wpdb->prepare( <<<EOF |
|
| 204 | 204 | SELECT %4\$s, p2.ID as entity_id |
| 205 | 205 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 206 | 206 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -233,33 +233,33 @@ discard block |
||
| 233 | 233 | LIMIT %2\$d |
| 234 | 234 | OFFSET %3\$d |
| 235 | 235 | EOF |
| 236 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 237 | - ); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - private function get_post_results( |
|
| 241 | - $post_id, |
|
| 242 | - $fields = array( |
|
| 243 | - 'ID', |
|
| 244 | - 'post_title', |
|
| 245 | - ), |
|
| 246 | - $order_by = 'ID DESC', |
|
| 247 | - $limit = 10, |
|
| 248 | - $offset = 0 |
|
| 249 | - ) { |
|
| 250 | - global $wpdb; |
|
| 251 | - |
|
| 252 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 253 | - return "p.$item AS $item"; |
|
| 254 | - }, (array) $fields ) ); |
|
| 255 | - |
|
| 256 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 257 | - return "p.$item"; |
|
| 258 | - }, (array) $order_by ) ); |
|
| 259 | - |
|
| 260 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 261 | - return $wpdb->get_results( |
|
| 262 | - $wpdb->prepare( <<<EOF |
|
| 236 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 237 | + ); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + private function get_post_results( |
|
| 241 | + $post_id, |
|
| 242 | + $fields = array( |
|
| 243 | + 'ID', |
|
| 244 | + 'post_title', |
|
| 245 | + ), |
|
| 246 | + $order_by = 'ID DESC', |
|
| 247 | + $limit = 10, |
|
| 248 | + $offset = 0 |
|
| 249 | + ) { |
|
| 250 | + global $wpdb; |
|
| 251 | + |
|
| 252 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 253 | + return "p.$item AS $item"; |
|
| 254 | + }, (array) $fields ) ); |
|
| 255 | + |
|
| 256 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 257 | + return "p.$item"; |
|
| 258 | + }, (array) $order_by ) ); |
|
| 259 | + |
|
| 260 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 261 | + return $wpdb->get_results( |
|
| 262 | + $wpdb->prepare( <<<EOF |
|
| 263 | 263 | SELECT %4\$s, p2.ID as entity_id |
| 264 | 264 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 265 | 265 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -295,8 +295,8 @@ discard block |
||
| 295 | 295 | LIMIT %2\$d |
| 296 | 296 | OFFSET %3\$d |
| 297 | 297 | EOF |
| 298 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 299 | - ); |
|
| 300 | - } |
|
| 298 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 299 | + ); |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | 302 | } |
@@ -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,106 +51,106 @@ 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 | 'label' => $serialized_entity['label'], |
| 105 | 105 | 'mainType' => $serialized_entity['mainType'], |
| 106 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 106 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 107 | 107 | ), |
| 108 | 108 | ); |
| 109 | 109 | |
| 110 | - $result['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 111 | - $result['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 110 | + $result['product'] = apply_filters('wl_products_navigator_data_post', $result['product'], intval($referencing_post->ID), $navigator_id); |
|
| 111 | + $result['entity'] = apply_filters('wl_products_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 112 | 112 | |
| 113 | 113 | $results[] = $result; |
| 114 | 114 | |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - if ( count( $results ) < $navigator_length ) { |
|
| 118 | - $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 117 | + if (count($results) < $navigator_length) { |
|
| 118 | + $results = apply_filters('wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | |
| 122 | 122 | // Add filler posts if needed |
| 123 | - $filler_count = $navigator_length - count( $results ); |
|
| 124 | - if ( $filler_count > 0 ) { |
|
| 125 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 123 | + $filler_count = $navigator_length - count($results); |
|
| 124 | + if ($filler_count > 0) { |
|
| 125 | + $referencing_post_ids = array_map(function($p) { |
|
| 126 | 126 | return $p->ID; |
| 127 | - }, $referencing_posts ); |
|
| 127 | + }, $referencing_posts); |
|
| 128 | 128 | /** |
| 129 | 129 | * @since 3.28.0 |
| 130 | 130 | * Filler posts are fetched using this util. |
| 131 | 131 | */ |
| 132 | - $filler_posts_util = new Filler_Posts_Util( $post_id ); |
|
| 133 | - $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids ); |
|
| 134 | - $filler_posts = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 135 | - $results = array_merge( $results, $filler_posts ); |
|
| 132 | + $filler_posts_util = new Filler_Posts_Util($post_id); |
|
| 133 | + $post_ids_to_be_excluded = array_merge(array($post_id), $referencing_post_ids); |
|
| 134 | + $filler_posts = $filler_posts_util->get_product_navigator_response($filler_count, $post_ids_to_be_excluded); |
|
| 135 | + $results = array_merge($results, $filler_posts); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | |
| 139 | 139 | return $amp ? array( |
| 140 | 140 | 'items' => array( |
| 141 | - array( 'values' => $results ), |
|
| 141 | + array('values' => $results), |
|
| 142 | 142 | ), |
| 143 | 143 | ) : $results; |
| 144 | 144 | |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) { |
|
| 147 | + private function get_directly_referencing_posts($post_id, $referencing_post_ids) { |
|
| 148 | 148 | |
| 149 | - $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id ); |
|
| 149 | + $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities($post_id); |
|
| 150 | 150 | |
| 151 | - $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids ); |
|
| 151 | + $post__in = array_diff($directly_referencing_post_ids, $referencing_post_ids); |
|
| 152 | 152 | |
| 153 | - $directly_referencing_posts = get_posts( array( |
|
| 153 | + $directly_referencing_posts = get_posts(array( |
|
| 154 | 154 | 'meta_query' => array( |
| 155 | 155 | array( |
| 156 | 156 | 'key' => '_thumbnail_id' |
@@ -163,11 +163,11 @@ discard block |
||
| 163 | 163 | 'post__in' => $post__in, |
| 164 | 164 | 'post_type' => 'product', |
| 165 | 165 | 'ignore_sticky_posts' => 1 |
| 166 | - ) ); |
|
| 166 | + )); |
|
| 167 | 167 | |
| 168 | 168 | $results = array(); |
| 169 | 169 | |
| 170 | - foreach ( $directly_referencing_posts as $post ) { |
|
| 170 | + foreach ($directly_referencing_posts as $post) { |
|
| 171 | 171 | $result = new stdClass(); |
| 172 | 172 | $result->ID = $post->ID; |
| 173 | 173 | $result->post_title = $post->post_title; |
@@ -190,17 +190,17 @@ discard block |
||
| 190 | 190 | ) { |
| 191 | 191 | global $wpdb; |
| 192 | 192 | |
| 193 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 193 | + $select = implode(', ', array_map(function($item) { |
|
| 194 | 194 | return "p.$item AS $item"; |
| 195 | - }, (array) $fields ) ); |
|
| 195 | + }, (array) $fields)); |
|
| 196 | 196 | |
| 197 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 197 | + $order_by = implode(', ', array_map(function($item) { |
|
| 198 | 198 | return "p.$item"; |
| 199 | - }, (array) $order_by ) ); |
|
| 199 | + }, (array) $order_by)); |
|
| 200 | 200 | |
| 201 | 201 | /** @noinspection SqlNoDataSourceInspection */ |
| 202 | 202 | return $wpdb->get_results( |
| 203 | - $wpdb->prepare( <<<EOF |
|
| 203 | + $wpdb->prepare(<<<EOF |
|
| 204 | 204 | SELECT %4\$s, p2.ID as entity_id |
| 205 | 205 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 206 | 206 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | LIMIT %2\$d |
| 234 | 234 | OFFSET %3\$d |
| 235 | 235 | EOF |
| 236 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 236 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 237 | 237 | ); |
| 238 | 238 | } |
| 239 | 239 | |
@@ -249,17 +249,17 @@ discard block |
||
| 249 | 249 | ) { |
| 250 | 250 | global $wpdb; |
| 251 | 251 | |
| 252 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 252 | + $select = implode(', ', array_map(function($item) { |
|
| 253 | 253 | return "p.$item AS $item"; |
| 254 | - }, (array) $fields ) ); |
|
| 254 | + }, (array) $fields)); |
|
| 255 | 255 | |
| 256 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 256 | + $order_by = implode(', ', array_map(function($item) { |
|
| 257 | 257 | return "p.$item"; |
| 258 | - }, (array) $order_by ) ); |
|
| 258 | + }, (array) $order_by)); |
|
| 259 | 259 | |
| 260 | 260 | /** @noinspection SqlNoDataSourceInspection */ |
| 261 | 261 | return $wpdb->get_results( |
| 262 | - $wpdb->prepare( <<<EOF |
|
| 262 | + $wpdb->prepare(<<<EOF |
|
| 263 | 263 | SELECT %4\$s, p2.ID as entity_id |
| 264 | 264 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 265 | 265 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | LIMIT %2\$d |
| 296 | 296 | OFFSET %3\$d |
| 297 | 297 | EOF |
| 298 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 298 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 299 | 299 | ); |
| 300 | 300 | } |
| 301 | 301 | |
@@ -9,156 +9,156 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Filler_Posts_Util { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var array<Filler_Posts> |
|
| 14 | - */ |
|
| 15 | - private $sources = array(); |
|
| 16 | - |
|
| 17 | - public function __construct( $post_id ) { |
|
| 18 | - |
|
| 19 | - $post_type = get_post_type( $post_id ); |
|
| 20 | - |
|
| 21 | - if ( $post_type === 'post' ) { |
|
| 22 | - $this->sources = array( |
|
| 23 | - new Same_Category_Filler_Posts( $post_id ), |
|
| 24 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 25 | - ); |
|
| 26 | - } else if ( $post_type === 'product' ) { |
|
| 27 | - $this->sources = array( |
|
| 28 | - new Same_Post_Type_Same_Category_Posts( $post_id ), |
|
| 29 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 30 | - ); |
|
| 31 | - } else { |
|
| 32 | - $this->sources = array( |
|
| 33 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 34 | - ); |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param $posts array<\WP_Post> |
|
| 41 | - * |
|
| 42 | - * @return array<int> |
|
| 43 | - */ |
|
| 44 | - private function extract_post_ids( $posts ) { |
|
| 45 | - return array_map( function ( $post ) { |
|
| 46 | - /** |
|
| 47 | - * @var $post \WP_Post |
|
| 48 | - */ |
|
| 49 | - return $post->ID; |
|
| 50 | - }, $posts ); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 54 | - |
|
| 55 | - $filler_posts = array(); |
|
| 56 | - |
|
| 57 | - foreach ( $this->sources as $source ) { |
|
| 58 | - |
|
| 59 | - if ( $filler_count <= 0 ) { |
|
| 60 | - break; |
|
| 61 | - } |
|
| 62 | - /** |
|
| 63 | - * @var Filler_Posts $source |
|
| 64 | - */ |
|
| 65 | - $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
|
| 66 | - $source->filler_count = $filler_count; |
|
| 67 | - |
|
| 68 | - $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 69 | - $post_ids = $this->extract_post_ids( $posts ); |
|
| 70 | - |
|
| 71 | - // Update the post ids, filler posts and filler count |
|
| 72 | - $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 73 | - $filler_count = $filler_count - count( $posts ); |
|
| 74 | - $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 75 | - } |
|
| 76 | - $filler_posts = $this->add_additional_properties_to_filler_posts( $filler_posts ); |
|
| 77 | - |
|
| 78 | - return $filler_posts; |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param $posts array<\WP_Post> |
|
| 84 | - * |
|
| 85 | - * @return array $posts array<\WP_Post> |
|
| 86 | - */ |
|
| 87 | - private function add_additional_properties_to_filler_posts( $posts ) { |
|
| 88 | - return array_map( function ( $post ) { |
|
| 89 | - $post->thumbnail = get_the_post_thumbnail_url( $post->ID, 'medium' ); |
|
| 90 | - $post->permalink = get_permalink( $post->ID ); |
|
| 91 | - |
|
| 92 | - return $post; |
|
| 93 | - }, $posts ); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Called by wordlift navigator, converts all the posts to response format. |
|
| 98 | - * |
|
| 99 | - * @param $filler_count |
|
| 100 | - * @param $post_ids_to_be_excluded |
|
| 101 | - * |
|
| 102 | - * @return array |
|
| 103 | - */ |
|
| 104 | - public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 105 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 106 | - // Add thumbnail and permalink to filler posts |
|
| 107 | - $filler_response = array(); |
|
| 108 | - foreach ( $filler_posts as $post_obj ) { |
|
| 109 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 110 | - $filler_response[] = array( |
|
| 111 | - 'post' => array( |
|
| 112 | - 'id' => $post_obj->ID, |
|
| 113 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 114 | - 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 115 | - 'title' => get_the_title( $post_obj->ID ), |
|
| 116 | - 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 117 | - ), |
|
| 118 | - 'entity' => array( |
|
| 119 | - 'id' => 0 |
|
| 120 | - ) |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $filler_response; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Called by wordlift navigator, converts all the posts to response format. |
|
| 130 | - * |
|
| 131 | - * @param $filler_count |
|
| 132 | - * @param $post_ids_to_be_excluded |
|
| 133 | - * |
|
| 134 | - * @return array |
|
| 135 | - */ |
|
| 136 | - public function get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 137 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 138 | - // Add thumbnail and permalink to filler posts |
|
| 139 | - $filler_response = array(); |
|
| 140 | - foreach ( $filler_posts as $post_obj ) { |
|
| 141 | - $product = wc_get_product( $post_obj->ID ); |
|
| 142 | - $filler_response[] = array( |
|
| 143 | - 'product' => array( |
|
| 144 | - 'id' => $post_obj->ID, |
|
| 145 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 146 | - 'title' => $post_obj->post_title, |
|
| 147 | - 'thumbnail' => get_the_post_thumbnail_url( $post_obj->ID, 'medium' ), |
|
| 148 | - 'regular_price' => $product->get_regular_price(), |
|
| 149 | - 'sale_price' => $product->get_sale_price(), |
|
| 150 | - 'price' => $product->get_price(), |
|
| 151 | - 'currency_symbol' => get_woocommerce_currency_symbol(), |
|
| 152 | - 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 153 | - 'average_rating' => $product->get_average_rating(), |
|
| 154 | - 'rating_count' => $product->get_rating_count(), |
|
| 155 | - 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 156 | - ), |
|
| 157 | - 'entity' => array(), |
|
| 158 | - ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - return $filler_response; |
|
| 162 | - } |
|
| 12 | + /** |
|
| 13 | + * @var array<Filler_Posts> |
|
| 14 | + */ |
|
| 15 | + private $sources = array(); |
|
| 16 | + |
|
| 17 | + public function __construct( $post_id ) { |
|
| 18 | + |
|
| 19 | + $post_type = get_post_type( $post_id ); |
|
| 20 | + |
|
| 21 | + if ( $post_type === 'post' ) { |
|
| 22 | + $this->sources = array( |
|
| 23 | + new Same_Category_Filler_Posts( $post_id ), |
|
| 24 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 25 | + ); |
|
| 26 | + } else if ( $post_type === 'product' ) { |
|
| 27 | + $this->sources = array( |
|
| 28 | + new Same_Post_Type_Same_Category_Posts( $post_id ), |
|
| 29 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 30 | + ); |
|
| 31 | + } else { |
|
| 32 | + $this->sources = array( |
|
| 33 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 34 | + ); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param $posts array<\WP_Post> |
|
| 41 | + * |
|
| 42 | + * @return array<int> |
|
| 43 | + */ |
|
| 44 | + private function extract_post_ids( $posts ) { |
|
| 45 | + return array_map( function ( $post ) { |
|
| 46 | + /** |
|
| 47 | + * @var $post \WP_Post |
|
| 48 | + */ |
|
| 49 | + return $post->ID; |
|
| 50 | + }, $posts ); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 54 | + |
|
| 55 | + $filler_posts = array(); |
|
| 56 | + |
|
| 57 | + foreach ( $this->sources as $source ) { |
|
| 58 | + |
|
| 59 | + if ( $filler_count <= 0 ) { |
|
| 60 | + break; |
|
| 61 | + } |
|
| 62 | + /** |
|
| 63 | + * @var Filler_Posts $source |
|
| 64 | + */ |
|
| 65 | + $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
|
| 66 | + $source->filler_count = $filler_count; |
|
| 67 | + |
|
| 68 | + $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 69 | + $post_ids = $this->extract_post_ids( $posts ); |
|
| 70 | + |
|
| 71 | + // Update the post ids, filler posts and filler count |
|
| 72 | + $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 73 | + $filler_count = $filler_count - count( $posts ); |
|
| 74 | + $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 75 | + } |
|
| 76 | + $filler_posts = $this->add_additional_properties_to_filler_posts( $filler_posts ); |
|
| 77 | + |
|
| 78 | + return $filler_posts; |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param $posts array<\WP_Post> |
|
| 84 | + * |
|
| 85 | + * @return array $posts array<\WP_Post> |
|
| 86 | + */ |
|
| 87 | + private function add_additional_properties_to_filler_posts( $posts ) { |
|
| 88 | + return array_map( function ( $post ) { |
|
| 89 | + $post->thumbnail = get_the_post_thumbnail_url( $post->ID, 'medium' ); |
|
| 90 | + $post->permalink = get_permalink( $post->ID ); |
|
| 91 | + |
|
| 92 | + return $post; |
|
| 93 | + }, $posts ); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Called by wordlift navigator, converts all the posts to response format. |
|
| 98 | + * |
|
| 99 | + * @param $filler_count |
|
| 100 | + * @param $post_ids_to_be_excluded |
|
| 101 | + * |
|
| 102 | + * @return array |
|
| 103 | + */ |
|
| 104 | + public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 105 | + $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 106 | + // Add thumbnail and permalink to filler posts |
|
| 107 | + $filler_response = array(); |
|
| 108 | + foreach ( $filler_posts as $post_obj ) { |
|
| 109 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 110 | + $filler_response[] = array( |
|
| 111 | + 'post' => array( |
|
| 112 | + 'id' => $post_obj->ID, |
|
| 113 | + 'permalink' => get_permalink( $post_obj->ID ), |
|
| 114 | + 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 115 | + 'title' => get_the_title( $post_obj->ID ), |
|
| 116 | + 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 117 | + ), |
|
| 118 | + 'entity' => array( |
|
| 119 | + 'id' => 0 |
|
| 120 | + ) |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $filler_response; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Called by wordlift navigator, converts all the posts to response format. |
|
| 130 | + * |
|
| 131 | + * @param $filler_count |
|
| 132 | + * @param $post_ids_to_be_excluded |
|
| 133 | + * |
|
| 134 | + * @return array |
|
| 135 | + */ |
|
| 136 | + public function get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 137 | + $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 138 | + // Add thumbnail and permalink to filler posts |
|
| 139 | + $filler_response = array(); |
|
| 140 | + foreach ( $filler_posts as $post_obj ) { |
|
| 141 | + $product = wc_get_product( $post_obj->ID ); |
|
| 142 | + $filler_response[] = array( |
|
| 143 | + 'product' => array( |
|
| 144 | + 'id' => $post_obj->ID, |
|
| 145 | + 'permalink' => get_permalink( $post_obj->ID ), |
|
| 146 | + 'title' => $post_obj->post_title, |
|
| 147 | + 'thumbnail' => get_the_post_thumbnail_url( $post_obj->ID, 'medium' ), |
|
| 148 | + 'regular_price' => $product->get_regular_price(), |
|
| 149 | + 'sale_price' => $product->get_sale_price(), |
|
| 150 | + 'price' => $product->get_price(), |
|
| 151 | + 'currency_symbol' => get_woocommerce_currency_symbol(), |
|
| 152 | + 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 153 | + 'average_rating' => $product->get_average_rating(), |
|
| 154 | + 'rating_count' => $product->get_rating_count(), |
|
| 155 | + 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 156 | + ), |
|
| 157 | + 'entity' => array(), |
|
| 158 | + ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + return $filler_response; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | 164 | } |
@@ -14,23 +14,23 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | private $sources = array(); |
| 16 | 16 | |
| 17 | - public function __construct( $post_id ) { |
|
| 17 | + public function __construct($post_id) { |
|
| 18 | 18 | |
| 19 | - $post_type = get_post_type( $post_id ); |
|
| 19 | + $post_type = get_post_type($post_id); |
|
| 20 | 20 | |
| 21 | - if ( $post_type === 'post' ) { |
|
| 21 | + if ($post_type === 'post') { |
|
| 22 | 22 | $this->sources = array( |
| 23 | - new Same_Category_Filler_Posts( $post_id ), |
|
| 24 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 23 | + new Same_Category_Filler_Posts($post_id), |
|
| 24 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 25 | 25 | ); |
| 26 | - } else if ( $post_type === 'product' ) { |
|
| 26 | + } else if ($post_type === 'product') { |
|
| 27 | 27 | $this->sources = array( |
| 28 | - new Same_Post_Type_Same_Category_Posts( $post_id ), |
|
| 29 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 28 | + new Same_Post_Type_Same_Category_Posts($post_id), |
|
| 29 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 30 | 30 | ); |
| 31 | 31 | } else { |
| 32 | 32 | $this->sources = array( |
| 33 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 33 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 34 | 34 | ); |
| 35 | 35 | } |
| 36 | 36 | } |
@@ -41,22 +41,22 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @return array<int> |
| 43 | 43 | */ |
| 44 | - private function extract_post_ids( $posts ) { |
|
| 45 | - return array_map( function ( $post ) { |
|
| 44 | + private function extract_post_ids($posts) { |
|
| 45 | + return array_map(function($post) { |
|
| 46 | 46 | /** |
| 47 | 47 | * @var $post \WP_Post |
| 48 | 48 | */ |
| 49 | 49 | return $post->ID; |
| 50 | - }, $posts ); |
|
| 50 | + }, $posts); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 53 | + public function get_filler_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 54 | 54 | |
| 55 | 55 | $filler_posts = array(); |
| 56 | 56 | |
| 57 | - foreach ( $this->sources as $source ) { |
|
| 57 | + foreach ($this->sources as $source) { |
|
| 58 | 58 | |
| 59 | - if ( $filler_count <= 0 ) { |
|
| 59 | + if ($filler_count <= 0) { |
|
| 60 | 60 | break; |
| 61 | 61 | } |
| 62 | 62 | /** |
@@ -65,15 +65,15 @@ discard block |
||
| 65 | 65 | $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
| 66 | 66 | $source->filler_count = $filler_count; |
| 67 | 67 | |
| 68 | - $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 69 | - $post_ids = $this->extract_post_ids( $posts ); |
|
| 68 | + $posts = $source->get_posts($filler_count, $post_ids_to_be_excluded); |
|
| 69 | + $post_ids = $this->extract_post_ids($posts); |
|
| 70 | 70 | |
| 71 | 71 | // Update the post ids, filler posts and filler count |
| 72 | - $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 73 | - $filler_count = $filler_count - count( $posts ); |
|
| 74 | - $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 72 | + $post_ids_to_be_excluded = array_merge($post_ids_to_be_excluded, $post_ids); |
|
| 73 | + $filler_count = $filler_count - count($posts); |
|
| 74 | + $filler_posts = array_merge($filler_posts, $posts); |
|
| 75 | 75 | } |
| 76 | - $filler_posts = $this->add_additional_properties_to_filler_posts( $filler_posts ); |
|
| 76 | + $filler_posts = $this->add_additional_properties_to_filler_posts($filler_posts); |
|
| 77 | 77 | |
| 78 | 78 | return $filler_posts; |
| 79 | 79 | |
@@ -84,13 +84,13 @@ discard block |
||
| 84 | 84 | * |
| 85 | 85 | * @return array $posts array<\WP_Post> |
| 86 | 86 | */ |
| 87 | - private function add_additional_properties_to_filler_posts( $posts ) { |
|
| 88 | - return array_map( function ( $post ) { |
|
| 89 | - $post->thumbnail = get_the_post_thumbnail_url( $post->ID, 'medium' ); |
|
| 90 | - $post->permalink = get_permalink( $post->ID ); |
|
| 87 | + private function add_additional_properties_to_filler_posts($posts) { |
|
| 88 | + return array_map(function($post) { |
|
| 89 | + $post->thumbnail = get_the_post_thumbnail_url($post->ID, 'medium'); |
|
| 90 | + $post->permalink = get_permalink($post->ID); |
|
| 91 | 91 | |
| 92 | 92 | return $post; |
| 93 | - }, $posts ); |
|
| 93 | + }, $posts); |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | /** |
@@ -101,19 +101,19 @@ discard block |
||
| 101 | 101 | * |
| 102 | 102 | * @return array |
| 103 | 103 | */ |
| 104 | - public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 105 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 104 | + public function get_filler_response($filler_count, $post_ids_to_be_excluded) { |
|
| 105 | + $filler_posts = $this->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 106 | 106 | // Add thumbnail and permalink to filler posts |
| 107 | 107 | $filler_response = array(); |
| 108 | - foreach ( $filler_posts as $post_obj ) { |
|
| 109 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 108 | + foreach ($filler_posts as $post_obj) { |
|
| 109 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 110 | 110 | $filler_response[] = array( |
| 111 | 111 | 'post' => array( |
| 112 | 112 | 'id' => $post_obj->ID, |
| 113 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 114 | - 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 115 | - 'title' => get_the_title( $post_obj->ID ), |
|
| 116 | - 'srcset' => Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 113 | + 'permalink' => get_permalink($post_obj->ID), |
|
| 114 | + 'thumbnail' => ($thumbnail) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 115 | + 'title' => get_the_title($post_obj->ID), |
|
| 116 | + 'srcset' => Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::NAVIGATOR_WIDGET) |
|
| 117 | 117 | ), |
| 118 | 118 | 'entity' => array( |
| 119 | 119 | 'id' => 0 |
@@ -133,26 +133,26 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @return array |
| 135 | 135 | */ |
| 136 | - public function get_product_navigator_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 137 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 136 | + public function get_product_navigator_response($filler_count, $post_ids_to_be_excluded) { |
|
| 137 | + $filler_posts = $this->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 138 | 138 | // Add thumbnail and permalink to filler posts |
| 139 | 139 | $filler_response = array(); |
| 140 | - foreach ( $filler_posts as $post_obj ) { |
|
| 141 | - $product = wc_get_product( $post_obj->ID ); |
|
| 140 | + foreach ($filler_posts as $post_obj) { |
|
| 141 | + $product = wc_get_product($post_obj->ID); |
|
| 142 | 142 | $filler_response[] = array( |
| 143 | 143 | 'product' => array( |
| 144 | 144 | 'id' => $post_obj->ID, |
| 145 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 145 | + 'permalink' => get_permalink($post_obj->ID), |
|
| 146 | 146 | 'title' => $post_obj->post_title, |
| 147 | - 'thumbnail' => get_the_post_thumbnail_url( $post_obj->ID, 'medium' ), |
|
| 147 | + 'thumbnail' => get_the_post_thumbnail_url($post_obj->ID, 'medium'), |
|
| 148 | 148 | 'regular_price' => $product->get_regular_price(), |
| 149 | 149 | 'sale_price' => $product->get_sale_price(), |
| 150 | 150 | 'price' => $product->get_price(), |
| 151 | 151 | 'currency_symbol' => get_woocommerce_currency_symbol(), |
| 152 | - 'discount_pc' => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
| 152 | + 'discount_pc' => ($product->get_sale_price() && ($product->get_regular_price() > 0)) ? round(1 - ($product->get_sale_price() / $product->get_regular_price()), 2) * 100 : 0, |
|
| 153 | 153 | 'average_rating' => $product->get_average_rating(), |
| 154 | 154 | 'rating_count' => $product->get_rating_count(), |
| 155 | - 'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
| 155 | + 'rating_html' => wc_get_rating_html($product->get_average_rating(), $product->get_rating_count()) |
|
| 156 | 156 | ), |
| 157 | 157 | 'entity' => array(), |
| 158 | 158 | ); |