@@ -8,55 +8,55 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | abstract class Wordlift_Shortcode { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * The shortcode, set by extending classes. |
|
| 13 | - */ |
|
| 14 | - const SHORTCODE = null; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * Create a shortcode instance by registering the shortcode with the render |
|
| 18 | - * function. |
|
| 19 | - * |
|
| 20 | - * @since 3.5.4 |
|
| 21 | - */ |
|
| 22 | - public function __construct() { |
|
| 23 | - |
|
| 24 | - add_shortcode( static::SHORTCODE, array( $this, 'render' ) ); |
|
| 25 | - |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Render the shortcode. |
|
| 30 | - * |
|
| 31 | - * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 32 | - * |
|
| 33 | - * @return string The output html code. |
|
| 34 | - * @since 3.5.4 |
|
| 35 | - */ |
|
| 36 | - abstract public function render( $atts ); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Enqueue scripts. Called by the shortcode implementations in their render |
|
| 40 | - * method. |
|
| 41 | - * |
|
| 42 | - * @since 3.5.4 |
|
| 43 | - */ |
|
| 44 | - protected function enqueue_scripts() { |
|
| 45 | - |
|
| 46 | - wp_enqueue_script( 'angularjs', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular.min.js', array(), '1.3.11', false ); |
|
| 47 | - wp_enqueue_script( 'angularjs-touch', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular-touch.min.js', array( 'angularjs' ), '1.3.11', false ); |
|
| 48 | - wp_enqueue_script( |
|
| 49 | - 'wordlift-ui', |
|
| 50 | - dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', |
|
| 51 | - array( |
|
| 52 | - 'jquery', |
|
| 53 | - 'angularjs', |
|
| 54 | - 'angularjs-touch', |
|
| 55 | - ), |
|
| 56 | - WORDLIFT_VERSION, |
|
| 57 | - false |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - } |
|
| 11 | + /** |
|
| 12 | + * The shortcode, set by extending classes. |
|
| 13 | + */ |
|
| 14 | + const SHORTCODE = null; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * Create a shortcode instance by registering the shortcode with the render |
|
| 18 | + * function. |
|
| 19 | + * |
|
| 20 | + * @since 3.5.4 |
|
| 21 | + */ |
|
| 22 | + public function __construct() { |
|
| 23 | + |
|
| 24 | + add_shortcode( static::SHORTCODE, array( $this, 'render' ) ); |
|
| 25 | + |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Render the shortcode. |
|
| 30 | + * |
|
| 31 | + * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 32 | + * |
|
| 33 | + * @return string The output html code. |
|
| 34 | + * @since 3.5.4 |
|
| 35 | + */ |
|
| 36 | + abstract public function render( $atts ); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Enqueue scripts. Called by the shortcode implementations in their render |
|
| 40 | + * method. |
|
| 41 | + * |
|
| 42 | + * @since 3.5.4 |
|
| 43 | + */ |
|
| 44 | + protected function enqueue_scripts() { |
|
| 45 | + |
|
| 46 | + wp_enqueue_script( 'angularjs', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular.min.js', array(), '1.3.11', false ); |
|
| 47 | + wp_enqueue_script( 'angularjs-touch', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular-touch.min.js', array( 'angularjs' ), '1.3.11', false ); |
|
| 48 | + wp_enqueue_script( |
|
| 49 | + 'wordlift-ui', |
|
| 50 | + dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', |
|
| 51 | + array( |
|
| 52 | + 'jquery', |
|
| 53 | + 'angularjs', |
|
| 54 | + 'angularjs-touch', |
|
| 55 | + ), |
|
| 56 | + WORDLIFT_VERSION, |
|
| 57 | + false |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function __construct() { |
| 23 | 23 | |
| 24 | - add_shortcode( static::SHORTCODE, array( $this, 'render' ) ); |
|
| 24 | + add_shortcode(static::SHORTCODE, array($this, 'render')); |
|
| 25 | 25 | |
| 26 | 26 | } |
| 27 | 27 | |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | * @return string The output html code. |
| 34 | 34 | * @since 3.5.4 |
| 35 | 35 | */ |
| 36 | - abstract public function render( $atts ); |
|
| 36 | + abstract public function render($atts); |
|
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * Enqueue scripts. Called by the shortcode implementations in their render |
@@ -43,11 +43,11 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | protected function enqueue_scripts() { |
| 45 | 45 | |
| 46 | - wp_enqueue_script( 'angularjs', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular.min.js', array(), '1.3.11', false ); |
|
| 47 | - wp_enqueue_script( 'angularjs-touch', plugin_dir_url( __DIR__ ) . '/js/3rdparty/angular-touch.min.js', array( 'angularjs' ), '1.3.11', false ); |
|
| 46 | + wp_enqueue_script('angularjs', plugin_dir_url(__DIR__).'/js/3rdparty/angular.min.js', array(), '1.3.11', false); |
|
| 47 | + wp_enqueue_script('angularjs-touch', plugin_dir_url(__DIR__).'/js/3rdparty/angular-touch.min.js', array('angularjs'), '1.3.11', false); |
|
| 48 | 48 | wp_enqueue_script( |
| 49 | 49 | 'wordlift-ui', |
| 50 | - dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', |
|
| 50 | + dirname(plugin_dir_url(__FILE__)).'/js/wordlift-ui'.( ! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG ? '.min' : '').'.js', |
|
| 51 | 51 | array( |
| 52 | 52 | 'jquery', |
| 53 | 53 | 'angularjs', |
@@ -20,45 +20,45 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_shortcode_faceted_search( $request ) { |
| 22 | 22 | |
| 23 | - // Filter the allowed parameters for caching. |
|
| 24 | - $cache_params = array_intersect_key( |
|
| 25 | - $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 26 | - array( |
|
| 27 | - 'post_id' => 1, |
|
| 28 | - 'post_types' => 1, |
|
| 29 | - 'limit' => 1, |
|
| 30 | - 'amp' => 1, |
|
| 31 | - 'sort' => 1, |
|
| 32 | - ) |
|
| 33 | - ); |
|
| 23 | + // Filter the allowed parameters for caching. |
|
| 24 | + $cache_params = array_intersect_key( |
|
| 25 | + $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 26 | + array( |
|
| 27 | + 'post_id' => 1, |
|
| 28 | + 'post_types' => 1, |
|
| 29 | + 'limit' => 1, |
|
| 30 | + 'amp' => 1, |
|
| 31 | + 'sort' => 1, |
|
| 32 | + ) |
|
| 33 | + ); |
|
| 34 | 34 | |
| 35 | - // Create the cache key. |
|
| 36 | - $cache_key = array( |
|
| 37 | - 'request_params' => $cache_params, |
|
| 38 | - ); |
|
| 35 | + // Create the cache key. |
|
| 36 | + $cache_key = array( |
|
| 37 | + 'request_params' => $cache_params, |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - // Create the TTL cache and try to get the results. |
|
| 41 | - $cache = new Ttl_Cache( 'faceted-search', 8 * 60 * 60 ); // 8 hours. |
|
| 42 | - $cache_results = $cache->get( $cache_key ); |
|
| 40 | + // Create the TTL cache and try to get the results. |
|
| 41 | + $cache = new Ttl_Cache( 'faceted-search', 8 * 60 * 60 ); // 8 hours. |
|
| 42 | + $cache_results = $cache->get( $cache_key ); |
|
| 43 | 43 | |
| 44 | - // So that the endpoint can be used remotely |
|
| 45 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 44 | + // So that the endpoint can be used remotely |
|
| 45 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 46 | 46 | |
| 47 | - if ( isset( $cache_results ) ) { |
|
| 48 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 49 | - wl_core_send_json( $cache_results ); |
|
| 47 | + if ( isset( $cache_results ) ) { |
|
| 48 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 49 | + wl_core_send_json( $cache_results ); |
|
| 50 | 50 | |
| 51 | - return; |
|
| 52 | - } |
|
| 51 | + return; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 54 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 55 | 55 | |
| 56 | - $results = wl_shortcode_faceted_search_origin( $request->get_query_params() ); |
|
| 56 | + $results = wl_shortcode_faceted_search_origin( $request->get_query_params() ); |
|
| 57 | 57 | |
| 58 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 59 | - $cache->put( $cache_key, $results ); |
|
| 58 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 59 | + $cache->put( $cache_key, $results ); |
|
| 60 | 60 | |
| 61 | - wl_core_send_json( $results ); |
|
| 61 | + wl_core_send_json( $results ); |
|
| 62 | 62 | |
| 63 | 63 | } |
| 64 | 64 | |
@@ -70,141 +70,141 @@ discard block |
||
| 70 | 70 | */ |
| 71 | 71 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 72 | 72 | function wl_shortcode_faceted_search_origin( $request ) { |
| 73 | - // Post ID must be defined. |
|
| 74 | - if ( ! isset( $request['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 75 | - wp_die( 'No post_id given' ); |
|
| 76 | - |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $current_post_id = (int) $request['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 81 | - $current_post = get_post( $current_post_id ); |
|
| 82 | - $faceted_id = isset( $request['uniqid'] ) ? sanitize_text_field( wp_unslash( (string) $request['uniqid'] ) ) : ''; |
|
| 83 | - |
|
| 84 | - $post_types = isset( $request['post_types'] ) ? sanitize_text_field( wp_unslash( (string) $request['post_types'] ) ) : ''; |
|
| 85 | - $post_types = explode( ',', $post_types ); |
|
| 86 | - $existing_post_types = get_post_types(); |
|
| 87 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 88 | - |
|
| 89 | - // Post ID has to match an existing item. |
|
| 90 | - if ( null === $current_post ) { |
|
| 91 | - wp_die( 'No valid post_id given' ); |
|
| 92 | - |
|
| 93 | - return; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // If the current post is an entity, |
|
| 97 | - // the current post is used as main entity. |
|
| 98 | - // Otherwise, current post related entities are used. |
|
| 99 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 100 | - |
|
| 101 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 102 | - array( $current_post->ID ) : |
|
| 103 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 104 | - |
|
| 105 | - // If there are no entities we cannot render the widget. |
|
| 106 | - if ( 0 === count( $entity_ids ) ) { |
|
| 107 | - /** |
|
| 108 | - * If this function is not called from ajax |
|
| 109 | - * then this should not throw an error. |
|
| 110 | - * Note: Used in scripbox longtail project on json endpoint. |
|
| 111 | - */ |
|
| 112 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 113 | - wp_die( 'No entities available' ); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - // phpcs:ignore Standard.Category.SniffName.ErrorCode |
|
| 118 | - $limit = ( isset( $request['limit'] ) ) ? (int) $request['limit'] : 4; |
|
| 119 | - $amp = isset( $request['amp'] ); |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 123 | - * The ordering should be descending by date on default. |
|
| 124 | - */ |
|
| 125 | - $order_by = 'DESC'; |
|
| 126 | - if ( isset( $request['sort'] ) && is_string( $request['sort'] ) ) { |
|
| 127 | - $order_by = sanitize_sql_orderby( wp_unslash( (string) $request['sort'] ) ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 131 | - $entity_ids, |
|
| 132 | - '*', |
|
| 133 | - null, |
|
| 134 | - 'publish', |
|
| 135 | - array( $current_post_id ), |
|
| 136 | - $limit, |
|
| 137 | - null, |
|
| 138 | - $order_by, |
|
| 139 | - $post_types |
|
| 140 | - ); |
|
| 141 | - |
|
| 142 | - $referencing_post_ids = array_map( |
|
| 143 | - function ( $p ) { |
|
| 144 | - return $p->ID; |
|
| 145 | - }, |
|
| 146 | - $referencing_posts |
|
| 147 | - ); |
|
| 148 | - |
|
| 149 | - $post_results = array(); |
|
| 150 | - $entity_results = array(); |
|
| 151 | - |
|
| 152 | - // Populate $post_results |
|
| 153 | - |
|
| 154 | - if ( $referencing_posts ) { |
|
| 155 | - foreach ( $referencing_posts as $post_obj ) { |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Use the thumbnail. |
|
| 159 | - * |
|
| 160 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 161 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 162 | - * |
|
| 163 | - * @since 3.19.3 We're using the medium size image. |
|
| 164 | - */ |
|
| 165 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 166 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 167 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 168 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 169 | - $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 170 | - $post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
| 171 | - $result = $post_obj; |
|
| 172 | - $post_results[] = $result; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Add filler posts if needed |
|
| 177 | - |
|
| 178 | - $filler_count = $limit - count( $post_results ); |
|
| 179 | - if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
| 180 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 181 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 182 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 183 | - |
|
| 184 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 185 | - } |
|
| 186 | - $referencing_post_ids = array_map( |
|
| 187 | - function ( $post ) { |
|
| 188 | - return $post->ID; |
|
| 189 | - }, |
|
| 190 | - $post_results |
|
| 191 | - ); |
|
| 192 | - |
|
| 193 | - // Populate $entity_results |
|
| 194 | - |
|
| 195 | - global $wpdb; |
|
| 196 | - |
|
| 197 | - // Retrieve Wordlift relation instances table name. |
|
| 198 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 199 | - |
|
| 200 | - /* |
|
| 73 | + // Post ID must be defined. |
|
| 74 | + if ( ! isset( $request['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 75 | + wp_die( 'No post_id given' ); |
|
| 76 | + |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $current_post_id = (int) $request['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 81 | + $current_post = get_post( $current_post_id ); |
|
| 82 | + $faceted_id = isset( $request['uniqid'] ) ? sanitize_text_field( wp_unslash( (string) $request['uniqid'] ) ) : ''; |
|
| 83 | + |
|
| 84 | + $post_types = isset( $request['post_types'] ) ? sanitize_text_field( wp_unslash( (string) $request['post_types'] ) ) : ''; |
|
| 85 | + $post_types = explode( ',', $post_types ); |
|
| 86 | + $existing_post_types = get_post_types(); |
|
| 87 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 88 | + |
|
| 89 | + // Post ID has to match an existing item. |
|
| 90 | + if ( null === $current_post ) { |
|
| 91 | + wp_die( 'No valid post_id given' ); |
|
| 92 | + |
|
| 93 | + return; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // If the current post is an entity, |
|
| 97 | + // the current post is used as main entity. |
|
| 98 | + // Otherwise, current post related entities are used. |
|
| 99 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 100 | + |
|
| 101 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 102 | + array( $current_post->ID ) : |
|
| 103 | + $entity_service->get_related_entities( $current_post->ID ); |
|
| 104 | + |
|
| 105 | + // If there are no entities we cannot render the widget. |
|
| 106 | + if ( 0 === count( $entity_ids ) ) { |
|
| 107 | + /** |
|
| 108 | + * If this function is not called from ajax |
|
| 109 | + * then this should not throw an error. |
|
| 110 | + * Note: Used in scripbox longtail project on json endpoint. |
|
| 111 | + */ |
|
| 112 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 113 | + wp_die( 'No entities available' ); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + // phpcs:ignore Standard.Category.SniffName.ErrorCode |
|
| 118 | + $limit = ( isset( $request['limit'] ) ) ? (int) $request['limit'] : 4; |
|
| 119 | + $amp = isset( $request['amp'] ); |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 123 | + * The ordering should be descending by date on default. |
|
| 124 | + */ |
|
| 125 | + $order_by = 'DESC'; |
|
| 126 | + if ( isset( $request['sort'] ) && is_string( $request['sort'] ) ) { |
|
| 127 | + $order_by = sanitize_sql_orderby( wp_unslash( (string) $request['sort'] ) ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 131 | + $entity_ids, |
|
| 132 | + '*', |
|
| 133 | + null, |
|
| 134 | + 'publish', |
|
| 135 | + array( $current_post_id ), |
|
| 136 | + $limit, |
|
| 137 | + null, |
|
| 138 | + $order_by, |
|
| 139 | + $post_types |
|
| 140 | + ); |
|
| 141 | + |
|
| 142 | + $referencing_post_ids = array_map( |
|
| 143 | + function ( $p ) { |
|
| 144 | + return $p->ID; |
|
| 145 | + }, |
|
| 146 | + $referencing_posts |
|
| 147 | + ); |
|
| 148 | + |
|
| 149 | + $post_results = array(); |
|
| 150 | + $entity_results = array(); |
|
| 151 | + |
|
| 152 | + // Populate $post_results |
|
| 153 | + |
|
| 154 | + if ( $referencing_posts ) { |
|
| 155 | + foreach ( $referencing_posts as $post_obj ) { |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Use the thumbnail. |
|
| 159 | + * |
|
| 160 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 161 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 162 | + * |
|
| 163 | + * @since 3.19.3 We're using the medium size image. |
|
| 164 | + */ |
|
| 165 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 166 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 167 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 168 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 169 | + $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 170 | + $post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
| 171 | + $result = $post_obj; |
|
| 172 | + $post_results[] = $result; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Add filler posts if needed |
|
| 177 | + |
|
| 178 | + $filler_count = $limit - count( $post_results ); |
|
| 179 | + if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
| 180 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 181 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 182 | + $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 183 | + |
|
| 184 | + $post_results = array_merge( $post_results, $filler_posts ); |
|
| 185 | + } |
|
| 186 | + $referencing_post_ids = array_map( |
|
| 187 | + function ( $post ) { |
|
| 188 | + return $post->ID; |
|
| 189 | + }, |
|
| 190 | + $post_results |
|
| 191 | + ); |
|
| 192 | + |
|
| 193 | + // Populate $entity_results |
|
| 194 | + |
|
| 195 | + global $wpdb; |
|
| 196 | + |
|
| 197 | + // Retrieve Wordlift relation instances table name. |
|
| 198 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 199 | + |
|
| 200 | + /* |
|
| 201 | 201 | * Make sure we have some referenced post, otherwise the IN parts of |
| 202 | 202 | * the SQL will produce an SQL error. |
| 203 | 203 | */ |
| 204 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 205 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 204 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 205 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 206 | 206 | |
| 207 | - $query = " |
|
| 207 | + $query = " |
|
| 208 | 208 | SELECT |
| 209 | 209 | object_id AS ID, |
| 210 | 210 | count( object_id ) AS counter |
@@ -216,71 +216,71 @@ discard block |
||
| 216 | 216 | LIMIT $limit; |
| 217 | 217 | "; |
| 218 | 218 | |
| 219 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 219 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 220 | 220 | |
| 221 | - $entities = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 221 | + $entities = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 222 | 222 | |
| 223 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 223 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 224 | 224 | |
| 225 | - foreach ( $entities as $obj ) { |
|
| 225 | + foreach ( $entities as $obj ) { |
|
| 226 | 226 | |
| 227 | - $entity = get_post( $obj->ID ); |
|
| 227 | + $entity = get_post( $obj->ID ); |
|
| 228 | 228 | |
| 229 | - // Ensure only valid and published entities are returned. |
|
| 230 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 229 | + // Ensure only valid and published entities are returned. |
|
| 230 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 231 | 231 | |
| 232 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 233 | - $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
| 234 | - $serialized_entity['counter'] = $obj->counter; |
|
| 235 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 236 | - $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 237 | - $obj->ID, |
|
| 238 | - 'ids', |
|
| 239 | - null, |
|
| 240 | - null, |
|
| 241 | - array(), |
|
| 242 | - null, |
|
| 243 | - $referencing_post_ids |
|
| 244 | - ); |
|
| 245 | - $entity_results[] = $serialized_entity; |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - } |
|
| 232 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 233 | + $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
| 234 | + $serialized_entity['counter'] = $obj->counter; |
|
| 235 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 236 | + $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 237 | + $obj->ID, |
|
| 238 | + 'ids', |
|
| 239 | + null, |
|
| 240 | + null, |
|
| 241 | + array(), |
|
| 242 | + null, |
|
| 243 | + $referencing_post_ids |
|
| 244 | + ); |
|
| 245 | + $entity_results[] = $serialized_entity; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 250 | + $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 251 | 251 | |
| 252 | - // Add srcset attribute. |
|
| 253 | - $post_results = array_map( |
|
| 254 | - function ( $post ) { |
|
| 255 | - $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 252 | + // Add srcset attribute. |
|
| 253 | + $post_results = array_map( |
|
| 254 | + function ( $post ) { |
|
| 255 | + $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 256 | 256 | |
| 257 | - return $post; |
|
| 258 | - }, |
|
| 259 | - $post_results |
|
| 260 | - ); |
|
| 257 | + return $post; |
|
| 258 | + }, |
|
| 259 | + $post_results |
|
| 260 | + ); |
|
| 261 | 261 | |
| 262 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 262 | + $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 263 | 263 | |
| 264 | - return array( |
|
| 265 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 266 | - 'entities' => $entity_results, |
|
| 267 | - ); |
|
| 264 | + return array( |
|
| 265 | + 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 266 | + 'entities' => $entity_results, |
|
| 267 | + ); |
|
| 268 | 268 | |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
| 272 | 272 | |
| 273 | - $title = wp_strip_all_tags( get_the_title( $post_id ) ); |
|
| 273 | + $title = wp_strip_all_tags( get_the_title( $post_id ) ); |
|
| 274 | 274 | |
| 275 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 276 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 275 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 276 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 277 | 277 | |
| 278 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 279 | - $title = $alternative_labels[0]; |
|
| 280 | - } |
|
| 281 | - } |
|
| 278 | + if ( count( $alternative_labels ) > 0 ) { |
|
| 279 | + $title = $alternative_labels[0]; |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - return remove_accents( $title ); |
|
| 283 | + return remove_accents( $title ); |
|
| 284 | 284 | |
| 285 | 285 | } |
| 286 | 286 | |
@@ -288,16 +288,16 @@ discard block |
||
| 288 | 288 | * Adding `rest_api_init` action for network faceted-search |
| 289 | 289 | */ |
| 290 | 290 | add_action( |
| 291 | - 'rest_api_init', |
|
| 292 | - function () { |
|
| 293 | - register_rest_route( |
|
| 294 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 295 | - '/faceted-search', |
|
| 296 | - array( |
|
| 297 | - 'methods' => 'GET', |
|
| 298 | - 'callback' => 'wl_shortcode_faceted_search', |
|
| 299 | - 'permission_callback' => '__return_true', |
|
| 300 | - ) |
|
| 301 | - ); |
|
| 302 | - } |
|
| 291 | + 'rest_api_init', |
|
| 292 | + function () { |
|
| 293 | + register_rest_route( |
|
| 294 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 295 | + '/faceted-search', |
|
| 296 | + array( |
|
| 297 | + 'methods' => 'GET', |
|
| 298 | + 'callback' => 'wl_shortcode_faceted_search', |
|
| 299 | + 'permission_callback' => '__return_true', |
|
| 300 | + ) |
|
| 301 | + ); |
|
| 302 | + } |
|
| 303 | 303 | ); |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | * @since 3.21.4 fix the cache key by also using the request body. |
| 19 | 19 | * @since 3.21.2 add a caching layer. |
| 20 | 20 | */ |
| 21 | -function wl_shortcode_faceted_search( $request ) { |
|
| 21 | +function wl_shortcode_faceted_search($request) { |
|
| 22 | 22 | |
| 23 | 23 | // Filter the allowed parameters for caching. |
| 24 | 24 | $cache_params = array_intersect_key( |
@@ -38,27 +38,27 @@ discard block |
||
| 38 | 38 | ); |
| 39 | 39 | |
| 40 | 40 | // Create the TTL cache and try to get the results. |
| 41 | - $cache = new Ttl_Cache( 'faceted-search', 8 * 60 * 60 ); // 8 hours. |
|
| 42 | - $cache_results = $cache->get( $cache_key ); |
|
| 41 | + $cache = new Ttl_Cache('faceted-search', 8 * 60 * 60); // 8 hours. |
|
| 42 | + $cache_results = $cache->get($cache_key); |
|
| 43 | 43 | |
| 44 | 44 | // So that the endpoint can be used remotely |
| 45 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 45 | + header('Access-Control-Allow-Origin: *'); |
|
| 46 | 46 | |
| 47 | - if ( isset( $cache_results ) ) { |
|
| 48 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 49 | - wl_core_send_json( $cache_results ); |
|
| 47 | + if (isset($cache_results)) { |
|
| 48 | + header('X-WordLift-Cache: HIT'); |
|
| 49 | + wl_core_send_json($cache_results); |
|
| 50 | 50 | |
| 51 | 51 | return; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 54 | + header('X-WordLift-Cache: MISS'); |
|
| 55 | 55 | |
| 56 | - $results = wl_shortcode_faceted_search_origin( $request->get_query_params() ); |
|
| 56 | + $results = wl_shortcode_faceted_search_origin($request->get_query_params()); |
|
| 57 | 57 | |
| 58 | 58 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 59 | - $cache->put( $cache_key, $results ); |
|
| 59 | + $cache->put($cache_key, $results); |
|
| 60 | 60 | |
| 61 | - wl_core_send_json( $results ); |
|
| 61 | + wl_core_send_json($results); |
|
| 62 | 62 | |
| 63 | 63 | } |
| 64 | 64 | |
@@ -69,26 +69,26 @@ discard block |
||
| 69 | 69 | * @since 3.26.0 |
| 70 | 70 | */ |
| 71 | 71 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 72 | -function wl_shortcode_faceted_search_origin( $request ) { |
|
| 72 | +function wl_shortcode_faceted_search_origin($request) { |
|
| 73 | 73 | // Post ID must be defined. |
| 74 | - if ( ! isset( $request['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 75 | - wp_die( 'No post_id given' ); |
|
| 74 | + if ( ! isset($request['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 75 | + wp_die('No post_id given'); |
|
| 76 | 76 | |
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | $current_post_id = (int) $request['post_id']; // WPCS: input var ok; CSRF ok. |
| 81 | - $current_post = get_post( $current_post_id ); |
|
| 82 | - $faceted_id = isset( $request['uniqid'] ) ? sanitize_text_field( wp_unslash( (string) $request['uniqid'] ) ) : ''; |
|
| 81 | + $current_post = get_post($current_post_id); |
|
| 82 | + $faceted_id = isset($request['uniqid']) ? sanitize_text_field(wp_unslash((string) $request['uniqid'])) : ''; |
|
| 83 | 83 | |
| 84 | - $post_types = isset( $request['post_types'] ) ? sanitize_text_field( wp_unslash( (string) $request['post_types'] ) ) : ''; |
|
| 85 | - $post_types = explode( ',', $post_types ); |
|
| 84 | + $post_types = isset($request['post_types']) ? sanitize_text_field(wp_unslash((string) $request['post_types'])) : ''; |
|
| 85 | + $post_types = explode(',', $post_types); |
|
| 86 | 86 | $existing_post_types = get_post_types(); |
| 87 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 87 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 88 | 88 | |
| 89 | 89 | // Post ID has to match an existing item. |
| 90 | - if ( null === $current_post ) { |
|
| 91 | - wp_die( 'No valid post_id given' ); |
|
| 90 | + if (null === $current_post) { |
|
| 91 | + wp_die('No valid post_id given'); |
|
| 92 | 92 | |
| 93 | 93 | return; |
| 94 | 94 | } |
@@ -98,33 +98,32 @@ discard block |
||
| 98 | 98 | // Otherwise, current post related entities are used. |
| 99 | 99 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 100 | 100 | |
| 101 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 102 | - array( $current_post->ID ) : |
|
| 103 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 101 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 102 | + array($current_post->ID) : $entity_service->get_related_entities($current_post->ID); |
|
| 104 | 103 | |
| 105 | 104 | // If there are no entities we cannot render the widget. |
| 106 | - if ( 0 === count( $entity_ids ) ) { |
|
| 105 | + if (0 === count($entity_ids)) { |
|
| 107 | 106 | /** |
| 108 | 107 | * If this function is not called from ajax |
| 109 | 108 | * then this should not throw an error. |
| 110 | 109 | * Note: Used in scripbox longtail project on json endpoint. |
| 111 | 110 | */ |
| 112 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 113 | - wp_die( 'No entities available' ); |
|
| 111 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 112 | + wp_die('No entities available'); |
|
| 114 | 113 | } |
| 115 | 114 | } |
| 116 | 115 | |
| 117 | 116 | // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 118 | - $limit = ( isset( $request['limit'] ) ) ? (int) $request['limit'] : 4; |
|
| 119 | - $amp = isset( $request['amp'] ); |
|
| 117 | + $limit = (isset($request['limit'])) ? (int) $request['limit'] : 4; |
|
| 118 | + $amp = isset($request['amp']); |
|
| 120 | 119 | |
| 121 | 120 | /** |
| 122 | 121 | * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
| 123 | 122 | * The ordering should be descending by date on default. |
| 124 | 123 | */ |
| 125 | 124 | $order_by = 'DESC'; |
| 126 | - if ( isset( $request['sort'] ) && is_string( $request['sort'] ) ) { |
|
| 127 | - $order_by = sanitize_sql_orderby( wp_unslash( (string) $request['sort'] ) ); |
|
| 125 | + if (isset($request['sort']) && is_string($request['sort'])) { |
|
| 126 | + $order_by = sanitize_sql_orderby(wp_unslash((string) $request['sort'])); |
|
| 128 | 127 | } |
| 129 | 128 | |
| 130 | 129 | $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -132,7 +131,7 @@ discard block |
||
| 132 | 131 | '*', |
| 133 | 132 | null, |
| 134 | 133 | 'publish', |
| 135 | - array( $current_post_id ), |
|
| 134 | + array($current_post_id), |
|
| 136 | 135 | $limit, |
| 137 | 136 | null, |
| 138 | 137 | $order_by, |
@@ -140,7 +139,7 @@ discard block |
||
| 140 | 139 | ); |
| 141 | 140 | |
| 142 | 141 | $referencing_post_ids = array_map( |
| 143 | - function ( $p ) { |
|
| 142 | + function($p) { |
|
| 144 | 143 | return $p->ID; |
| 145 | 144 | }, |
| 146 | 145 | $referencing_posts |
@@ -151,8 +150,8 @@ discard block |
||
| 151 | 150 | |
| 152 | 151 | // Populate $post_results |
| 153 | 152 | |
| 154 | - if ( $referencing_posts ) { |
|
| 155 | - foreach ( $referencing_posts as $post_obj ) { |
|
| 153 | + if ($referencing_posts) { |
|
| 154 | + foreach ($referencing_posts as $post_obj) { |
|
| 156 | 155 | |
| 157 | 156 | /** |
| 158 | 157 | * Use the thumbnail. |
@@ -162,12 +161,12 @@ discard block |
||
| 162 | 161 | * |
| 163 | 162 | * @since 3.19.3 We're using the medium size image. |
| 164 | 163 | */ |
| 165 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 166 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 164 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 165 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 167 | 166 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 168 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 169 | - $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 170 | - $post_obj->post_title = wp_strip_all_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
| 167 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
| 168 | + $post_obj->srcset = Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
| 169 | + $post_obj->post_title = wp_strip_all_tags(html_entity_decode($post_obj->post_title, ENT_QUOTES, 'UTF-8')); |
|
| 171 | 170 | $result = $post_obj; |
| 172 | 171 | $post_results[] = $result; |
| 173 | 172 | } |
@@ -175,16 +174,16 @@ discard block |
||
| 175 | 174 | |
| 176 | 175 | // Add filler posts if needed |
| 177 | 176 | |
| 178 | - $filler_count = $limit - count( $post_results ); |
|
| 179 | - if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
| 180 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 181 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 182 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 177 | + $filler_count = $limit - count($post_results); |
|
| 178 | + if (0 < apply_filters('wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids)) { |
|
| 179 | + $filler_posts_util = new Filler_Posts_Util($current_post_id); |
|
| 180 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 181 | + $filler_posts = $filler_posts_util->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 183 | 182 | |
| 184 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 183 | + $post_results = array_merge($post_results, $filler_posts); |
|
| 185 | 184 | } |
| 186 | 185 | $referencing_post_ids = array_map( |
| 187 | - function ( $post ) { |
|
| 186 | + function($post) { |
|
| 188 | 187 | return $post->ID; |
| 189 | 188 | }, |
| 190 | 189 | $post_results |
@@ -201,8 +200,8 @@ discard block |
||
| 201 | 200 | * Make sure we have some referenced post, otherwise the IN parts of |
| 202 | 201 | * the SQL will produce an SQL error. |
| 203 | 202 | */ |
| 204 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 205 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 203 | + if ( ! empty($referencing_post_ids)) { |
|
| 204 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 206 | 205 | |
| 207 | 206 | $query = " |
| 208 | 207 | SELECT |
@@ -216,21 +215,21 @@ discard block |
||
| 216 | 215 | LIMIT $limit; |
| 217 | 216 | "; |
| 218 | 217 | |
| 219 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 218 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 220 | 219 | |
| 221 | - $entities = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 220 | + $entities = $wpdb->get_results($query, OBJECT); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 222 | 221 | |
| 223 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 222 | + wl_write_log('Entities found '.count($entities)); |
|
| 224 | 223 | |
| 225 | - foreach ( $entities as $obj ) { |
|
| 224 | + foreach ($entities as $obj) { |
|
| 226 | 225 | |
| 227 | - $entity = get_post( $obj->ID ); |
|
| 226 | + $entity = get_post($obj->ID); |
|
| 228 | 227 | |
| 229 | 228 | // Ensure only valid and published entities are returned. |
| 230 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 229 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 231 | 230 | |
| 232 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 233 | - $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
| 231 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 232 | + $serialized_entity['label'] = html_entity_decode(wl_shortcode_faceted_search_get_the_title($obj->ID), ENT_QUOTES, 'UTF-8'); |
|
| 234 | 233 | $serialized_entity['counter'] = $obj->counter; |
| 235 | 234 | $serialized_entity['createdAt'] = $entity->post_date; |
| 236 | 235 | $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -242,45 +241,45 @@ discard block |
||
| 242 | 241 | null, |
| 243 | 242 | $referencing_post_ids |
| 244 | 243 | ); |
| 245 | - $entity_results[] = $serialized_entity; |
|
| 244 | + $entity_results[] = $serialized_entity; |
|
| 246 | 245 | } |
| 247 | 246 | } |
| 248 | 247 | } |
| 249 | 248 | |
| 250 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 249 | + $post_results = apply_filters('wl_faceted_data_posts', $post_results, $faceted_id); |
|
| 251 | 250 | |
| 252 | 251 | // Add srcset attribute. |
| 253 | 252 | $post_results = array_map( |
| 254 | - function ( $post ) { |
|
| 255 | - $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 253 | + function($post) { |
|
| 254 | + $post->srcset = Srcset_Util::get_srcset($post->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
| 256 | 255 | |
| 257 | 256 | return $post; |
| 258 | 257 | }, |
| 259 | 258 | $post_results |
| 260 | 259 | ); |
| 261 | 260 | |
| 262 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 261 | + $entity_results = apply_filters('wl_faceted_data_entities', $entity_results, $faceted_id); |
|
| 263 | 262 | |
| 264 | 263 | return array( |
| 265 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 264 | + 'posts' => $amp ? array(array('values' => $post_results)) : $post_results, |
|
| 266 | 265 | 'entities' => $entity_results, |
| 267 | 266 | ); |
| 268 | 267 | |
| 269 | 268 | } |
| 270 | 269 | |
| 271 | -function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
|
| 270 | +function wl_shortcode_faceted_search_get_the_title($post_id) { |
|
| 272 | 271 | |
| 273 | - $title = wp_strip_all_tags( get_the_title( $post_id ) ); |
|
| 272 | + $title = wp_strip_all_tags(get_the_title($post_id)); |
|
| 274 | 273 | |
| 275 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 276 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 274 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
| 275 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
| 277 | 276 | |
| 278 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 277 | + if (count($alternative_labels) > 0) { |
|
| 279 | 278 | $title = $alternative_labels[0]; |
| 280 | 279 | } |
| 281 | 280 | } |
| 282 | 281 | |
| 283 | - return remove_accents( $title ); |
|
| 282 | + return remove_accents($title); |
|
| 284 | 283 | |
| 285 | 284 | } |
| 286 | 285 | |
@@ -289,7 +288,7 @@ discard block |
||
| 289 | 288 | */ |
| 290 | 289 | add_action( |
| 291 | 290 | 'rest_api_init', |
| 292 | - function () { |
|
| 291 | + function() { |
|
| 293 | 292 | register_rest_route( |
| 294 | 293 | WL_REST_ROUTE_DEFAULT_NAMESPACE, |
| 295 | 294 | '/faceted-search', |
@@ -19,176 +19,176 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | function wl_shortcode_navigator_data() { |
| 21 | 21 | |
| 22 | - // Create the cache key. |
|
| 23 | - $cache_params = array_intersect_key( |
|
| 24 | - $_REQUEST, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 25 | - array( |
|
| 26 | - 'limit' => 1, |
|
| 27 | - 'offset' => 1, |
|
| 28 | - 'sort' => 1, |
|
| 29 | - 'post_types' => 1, |
|
| 30 | - 'post_id' => 1, |
|
| 31 | - ) |
|
| 32 | - ); |
|
| 22 | + // Create the cache key. |
|
| 23 | + $cache_params = array_intersect_key( |
|
| 24 | + $_REQUEST, // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 25 | + array( |
|
| 26 | + 'limit' => 1, |
|
| 27 | + 'offset' => 1, |
|
| 28 | + 'sort' => 1, |
|
| 29 | + 'post_types' => 1, |
|
| 30 | + 'post_id' => 1, |
|
| 31 | + ) |
|
| 32 | + ); |
|
| 33 | 33 | |
| 34 | - $cache_key = array( 'request_params' => $cache_params ); |
|
| 34 | + $cache_key = array( 'request_params' => $cache_params ); |
|
| 35 | 35 | |
| 36 | - // Create the TTL cache and try to get the results. |
|
| 37 | - $cache = new Ttl_Cache( 'navigator', 8 * 60 * 60 ); // 8 hours. |
|
| 38 | - $cache_results = $cache->get( $cache_key ); |
|
| 36 | + // Create the TTL cache and try to get the results. |
|
| 37 | + $cache = new Ttl_Cache( 'navigator', 8 * 60 * 60 ); // 8 hours. |
|
| 38 | + $cache_results = $cache->get( $cache_key ); |
|
| 39 | 39 | |
| 40 | - // So that the endpoint can be used remotely |
|
| 41 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 40 | + // So that the endpoint can be used remotely |
|
| 41 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 42 | 42 | |
| 43 | - if ( isset( $cache_results ) ) { |
|
| 44 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 43 | + if ( isset( $cache_results ) ) { |
|
| 44 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 45 | 45 | |
| 46 | - return $cache_results; |
|
| 47 | - } |
|
| 46 | + return $cache_results; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 49 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 50 | 50 | |
| 51 | - $results = _wl_navigator_get_data(); |
|
| 51 | + $results = _wl_navigator_get_data(); |
|
| 52 | 52 | |
| 53 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 54 | - $cache->put( $cache_key, $results ); |
|
| 53 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 54 | + $cache->put( $cache_key, $results ); |
|
| 55 | 55 | |
| 56 | - return $results; |
|
| 56 | + return $results; |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | function _wl_navigator_get_data() { |
| 60 | 60 | |
| 61 | - check_ajax_referer( 'wl_navigator' ); |
|
| 62 | - |
|
| 63 | - // Post ID must be defined |
|
| 64 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 65 | - wp_send_json_error( 'No post_id given' ); |
|
| 66 | - |
|
| 67 | - return array(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // Post ID must be defined |
|
| 71 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 72 | - wp_send_json_error( 'No uniqid given' ); |
|
| 73 | - |
|
| 74 | - return array(); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // Limit the results (defaults to 4) |
|
| 78 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 79 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 80 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( wp_unslash( $_GET['sort'] ) ) : 'ID DESC'; |
|
| 81 | - $post_types = isset( $_GET['post_types'] ) ? sanitize_text_field( wp_unslash( $_GET['post_types'] ) ) : ''; |
|
| 82 | - $post_types = explode( ',', $post_types ); |
|
| 83 | - $existing_post_types = get_post_types(); |
|
| 84 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 85 | - $current_post_id = (int) $_GET['post_id']; |
|
| 86 | - $current_post = get_post( $current_post_id ); |
|
| 87 | - |
|
| 88 | - $navigator_id = sanitize_text_field( wp_unslash( $_GET['uniqid'] ) ); |
|
| 89 | - |
|
| 90 | - // Post ID has to match an existing item |
|
| 91 | - if ( null === $current_post ) { |
|
| 92 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 93 | - |
|
| 94 | - return array(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Determine navigator type and call respective _get_results |
|
| 98 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 99 | - |
|
| 100 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( |
|
| 101 | - $current_post_id, |
|
| 102 | - array( |
|
| 103 | - 'ID', |
|
| 104 | - 'post_title', |
|
| 105 | - ), |
|
| 106 | - $order_by, |
|
| 107 | - $navigator_length, |
|
| 108 | - $navigator_offset, |
|
| 109 | - $post_types |
|
| 110 | - ); |
|
| 111 | - } else { |
|
| 112 | - $referencing_posts = Navigator_Data::post_navigator_get_results( |
|
| 113 | - $current_post_id, |
|
| 114 | - array( |
|
| 115 | - 'ID', |
|
| 116 | - 'post_title', |
|
| 117 | - ), |
|
| 118 | - $order_by, |
|
| 119 | - $navigator_length, |
|
| 120 | - $navigator_offset, |
|
| 121 | - $post_types |
|
| 122 | - ); |
|
| 123 | - |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 127 | - $results = array(); |
|
| 128 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 129 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Use the thumbnail. |
|
| 133 | - * |
|
| 134 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 135 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 136 | - * |
|
| 137 | - * @since 3.19.3 We're using the medium size image. |
|
| 138 | - */ |
|
| 139 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 140 | - |
|
| 141 | - $result = array( |
|
| 142 | - 'post' => array( |
|
| 143 | - 'id' => $referencing_post->ID, |
|
| 144 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 145 | - 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 146 | - 'thumbnail' => $thumbnail, |
|
| 147 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ), |
|
| 148 | - ), |
|
| 149 | - 'entity' => array( |
|
| 150 | - 'id' => $referencing_post->entity_id, |
|
| 151 | - 'label' => $serialized_entity['label'], |
|
| 152 | - 'mainType' => $serialized_entity['mainType'], |
|
| 153 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 154 | - ), |
|
| 155 | - ); |
|
| 156 | - |
|
| 157 | - $results[] = $result; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - if ( count( $results ) < $navigator_length ) { |
|
| 161 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // Add filler posts if needed |
|
| 165 | - $filler_count = $navigator_length - count( $results ); |
|
| 166 | - if ( $filler_count > 0 ) { |
|
| 167 | - $referencing_post_ids = array_map( |
|
| 168 | - function ( $p ) { |
|
| 169 | - return $p->ID; |
|
| 170 | - }, |
|
| 171 | - $referencing_posts |
|
| 172 | - ); |
|
| 173 | - /** |
|
| 174 | - * @since 3.27.8 |
|
| 175 | - * Filler posts are fetched using this util. |
|
| 176 | - */ |
|
| 177 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 178 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 179 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 180 | - $results = array_merge( $results, $filler_posts ); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // Apply filters after fillers are added |
|
| 184 | - foreach ( $results as $result_index => $result ) { |
|
| 185 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 186 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
| 190 | - |
|
| 191 | - return $results; |
|
| 61 | + check_ajax_referer( 'wl_navigator' ); |
|
| 62 | + |
|
| 63 | + // Post ID must be defined |
|
| 64 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 65 | + wp_send_json_error( 'No post_id given' ); |
|
| 66 | + |
|
| 67 | + return array(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // Post ID must be defined |
|
| 71 | + if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 72 | + wp_send_json_error( 'No uniqid given' ); |
|
| 73 | + |
|
| 74 | + return array(); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // Limit the results (defaults to 4) |
|
| 78 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 79 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 80 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( wp_unslash( $_GET['sort'] ) ) : 'ID DESC'; |
|
| 81 | + $post_types = isset( $_GET['post_types'] ) ? sanitize_text_field( wp_unslash( $_GET['post_types'] ) ) : ''; |
|
| 82 | + $post_types = explode( ',', $post_types ); |
|
| 83 | + $existing_post_types = get_post_types(); |
|
| 84 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 85 | + $current_post_id = (int) $_GET['post_id']; |
|
| 86 | + $current_post = get_post( $current_post_id ); |
|
| 87 | + |
|
| 88 | + $navigator_id = sanitize_text_field( wp_unslash( $_GET['uniqid'] ) ); |
|
| 89 | + |
|
| 90 | + // Post ID has to match an existing item |
|
| 91 | + if ( null === $current_post ) { |
|
| 92 | + wp_send_json_error( 'No valid post_id given' ); |
|
| 93 | + |
|
| 94 | + return array(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Determine navigator type and call respective _get_results |
|
| 98 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 99 | + |
|
| 100 | + $referencing_posts = Navigator_Data::entity_navigator_get_results( |
|
| 101 | + $current_post_id, |
|
| 102 | + array( |
|
| 103 | + 'ID', |
|
| 104 | + 'post_title', |
|
| 105 | + ), |
|
| 106 | + $order_by, |
|
| 107 | + $navigator_length, |
|
| 108 | + $navigator_offset, |
|
| 109 | + $post_types |
|
| 110 | + ); |
|
| 111 | + } else { |
|
| 112 | + $referencing_posts = Navigator_Data::post_navigator_get_results( |
|
| 113 | + $current_post_id, |
|
| 114 | + array( |
|
| 115 | + 'ID', |
|
| 116 | + 'post_title', |
|
| 117 | + ), |
|
| 118 | + $order_by, |
|
| 119 | + $navigator_length, |
|
| 120 | + $navigator_offset, |
|
| 121 | + $post_types |
|
| 122 | + ); |
|
| 123 | + |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 127 | + $results = array(); |
|
| 128 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 129 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Use the thumbnail. |
|
| 133 | + * |
|
| 134 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 135 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 136 | + * |
|
| 137 | + * @since 3.19.3 We're using the medium size image. |
|
| 138 | + */ |
|
| 139 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 140 | + |
|
| 141 | + $result = array( |
|
| 142 | + 'post' => array( |
|
| 143 | + 'id' => $referencing_post->ID, |
|
| 144 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 145 | + 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 146 | + 'thumbnail' => $thumbnail, |
|
| 147 | + 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ), |
|
| 148 | + ), |
|
| 149 | + 'entity' => array( |
|
| 150 | + 'id' => $referencing_post->entity_id, |
|
| 151 | + 'label' => $serialized_entity['label'], |
|
| 152 | + 'mainType' => $serialized_entity['mainType'], |
|
| 153 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 154 | + ), |
|
| 155 | + ); |
|
| 156 | + |
|
| 157 | + $results[] = $result; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + if ( count( $results ) < $navigator_length ) { |
|
| 161 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // Add filler posts if needed |
|
| 165 | + $filler_count = $navigator_length - count( $results ); |
|
| 166 | + if ( $filler_count > 0 ) { |
|
| 167 | + $referencing_post_ids = array_map( |
|
| 168 | + function ( $p ) { |
|
| 169 | + return $p->ID; |
|
| 170 | + }, |
|
| 171 | + $referencing_posts |
|
| 172 | + ); |
|
| 173 | + /** |
|
| 174 | + * @since 3.27.8 |
|
| 175 | + * Filler posts are fetched using this util. |
|
| 176 | + */ |
|
| 177 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 178 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 179 | + $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 180 | + $results = array_merge( $results, $filler_posts ); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // Apply filters after fillers are added |
|
| 184 | + foreach ( $results as $result_index => $result ) { |
|
| 185 | + $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 186 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
| 190 | + |
|
| 191 | + return $results; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -198,9 +198,9 @@ discard block |
||
| 198 | 198 | */ |
| 199 | 199 | function wl_shortcode_navigator_ajax() { |
| 200 | 200 | |
| 201 | - // Temporary blocking the Navigator. |
|
| 202 | - $results = wl_shortcode_navigator_data(); |
|
| 203 | - wl_core_send_json( $results ); |
|
| 201 | + // Temporary blocking the Navigator. |
|
| 202 | + $results = wl_shortcode_navigator_data(); |
|
| 203 | + wl_core_send_json( $results ); |
|
| 204 | 204 | |
| 205 | 205 | } |
| 206 | 206 | |
@@ -212,16 +212,16 @@ discard block |
||
| 212 | 212 | */ |
| 213 | 213 | function wl_shortcode_navigator_wp_json() { |
| 214 | 214 | |
| 215 | - $results = wl_shortcode_navigator_data(); |
|
| 216 | - if ( ob_get_contents() ) { |
|
| 217 | - ob_clean(); |
|
| 218 | - } |
|
| 215 | + $results = wl_shortcode_navigator_data(); |
|
| 216 | + if ( ob_get_contents() ) { |
|
| 217 | + ob_clean(); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - return array( |
|
| 221 | - 'items' => array( |
|
| 222 | - array( 'values' => $results ), |
|
| 223 | - ), |
|
| 224 | - ); |
|
| 220 | + return array( |
|
| 221 | + 'items' => array( |
|
| 222 | + array( 'values' => $results ), |
|
| 223 | + ), |
|
| 224 | + ); |
|
| 225 | 225 | |
| 226 | 226 | } |
| 227 | 227 | |
@@ -229,18 +229,18 @@ discard block |
||
| 229 | 229 | * Adding `rest_api_init` action for amp backend of navigator |
| 230 | 230 | */ |
| 231 | 231 | add_action( |
| 232 | - 'rest_api_init', |
|
| 233 | - function () { |
|
| 234 | - register_rest_route( |
|
| 235 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 236 | - '/navigator', |
|
| 237 | - array( |
|
| 238 | - 'methods' => 'GET', |
|
| 239 | - 'permission_callback' => '__return_true', |
|
| 240 | - 'callback' => 'wl_shortcode_navigator_wp_json', |
|
| 241 | - ) |
|
| 242 | - ); |
|
| 243 | - } |
|
| 232 | + 'rest_api_init', |
|
| 233 | + function () { |
|
| 234 | + register_rest_route( |
|
| 235 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 236 | + '/navigator', |
|
| 237 | + array( |
|
| 238 | + 'methods' => 'GET', |
|
| 239 | + 'permission_callback' => '__return_true', |
|
| 240 | + 'callback' => 'wl_shortcode_navigator_wp_json', |
|
| 241 | + ) |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | 244 | ); |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -249,31 +249,31 @@ discard block |
||
| 249 | 249 | * @since 2.2.0 |
| 250 | 250 | */ |
| 251 | 251 | add_action( |
| 252 | - 'plugins_loaded', |
|
| 253 | - function () { |
|
| 254 | - $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 255 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 256 | - return; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 260 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 261 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 262 | - }, |
|
| 263 | - 0 |
|
| 252 | + 'plugins_loaded', |
|
| 253 | + function () { |
|
| 254 | + $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 255 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 256 | + return; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 260 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 261 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 262 | + }, |
|
| 263 | + 0 |
|
| 264 | 264 | ); |
| 265 | 265 | |
| 266 | 266 | add_action( |
| 267 | - 'init', |
|
| 268 | - function () { |
|
| 269 | - $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 270 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 271 | - return; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 275 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 276 | - }, |
|
| 277 | - 0 |
|
| 267 | + 'init', |
|
| 268 | + function () { |
|
| 269 | + $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 270 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 271 | + return; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 275 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 276 | + }, |
|
| 277 | + 0 |
|
| 278 | 278 | ); |
| 279 | 279 | |
@@ -31,71 +31,71 @@ discard block |
||
| 31 | 31 | ) |
| 32 | 32 | ); |
| 33 | 33 | |
| 34 | - $cache_key = array( 'request_params' => $cache_params ); |
|
| 34 | + $cache_key = array('request_params' => $cache_params); |
|
| 35 | 35 | |
| 36 | 36 | // Create the TTL cache and try to get the results. |
| 37 | - $cache = new Ttl_Cache( 'navigator', 8 * 60 * 60 ); // 8 hours. |
|
| 38 | - $cache_results = $cache->get( $cache_key ); |
|
| 37 | + $cache = new Ttl_Cache('navigator', 8 * 60 * 60); // 8 hours. |
|
| 38 | + $cache_results = $cache->get($cache_key); |
|
| 39 | 39 | |
| 40 | 40 | // So that the endpoint can be used remotely |
| 41 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 41 | + header('Access-Control-Allow-Origin: *'); |
|
| 42 | 42 | |
| 43 | - if ( isset( $cache_results ) ) { |
|
| 44 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 43 | + if (isset($cache_results)) { |
|
| 44 | + header('X-WordLift-Cache: HIT'); |
|
| 45 | 45 | |
| 46 | 46 | return $cache_results; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 49 | + header('X-WordLift-Cache: MISS'); |
|
| 50 | 50 | |
| 51 | 51 | $results = _wl_navigator_get_data(); |
| 52 | 52 | |
| 53 | 53 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 54 | - $cache->put( $cache_key, $results ); |
|
| 54 | + $cache->put($cache_key, $results); |
|
| 55 | 55 | |
| 56 | 56 | return $results; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | function _wl_navigator_get_data() { |
| 60 | 60 | |
| 61 | - check_ajax_referer( 'wl_navigator' ); |
|
| 61 | + check_ajax_referer('wl_navigator'); |
|
| 62 | 62 | |
| 63 | 63 | // Post ID must be defined |
| 64 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 65 | - wp_send_json_error( 'No post_id given' ); |
|
| 64 | + if ( ! isset($_GET['post_id'])) { |
|
| 65 | + wp_send_json_error('No post_id given'); |
|
| 66 | 66 | |
| 67 | 67 | return array(); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // Post ID must be defined |
| 71 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 72 | - wp_send_json_error( 'No uniqid given' ); |
|
| 71 | + if ( ! isset($_GET['uniqid'])) { |
|
| 72 | + wp_send_json_error('No uniqid given'); |
|
| 73 | 73 | |
| 74 | 74 | return array(); |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | // Limit the results (defaults to 4) |
| 78 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 79 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 80 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( wp_unslash( $_GET['sort'] ) ) : 'ID DESC'; |
|
| 81 | - $post_types = isset( $_GET['post_types'] ) ? sanitize_text_field( wp_unslash( $_GET['post_types'] ) ) : ''; |
|
| 82 | - $post_types = explode( ',', $post_types ); |
|
| 78 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 79 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 80 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby(wp_unslash($_GET['sort'])) : 'ID DESC'; |
|
| 81 | + $post_types = isset($_GET['post_types']) ? sanitize_text_field(wp_unslash($_GET['post_types'])) : ''; |
|
| 82 | + $post_types = explode(',', $post_types); |
|
| 83 | 83 | $existing_post_types = get_post_types(); |
| 84 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 84 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 85 | 85 | $current_post_id = (int) $_GET['post_id']; |
| 86 | - $current_post = get_post( $current_post_id ); |
|
| 86 | + $current_post = get_post($current_post_id); |
|
| 87 | 87 | |
| 88 | - $navigator_id = sanitize_text_field( wp_unslash( $_GET['uniqid'] ) ); |
|
| 88 | + $navigator_id = sanitize_text_field(wp_unslash($_GET['uniqid'])); |
|
| 89 | 89 | |
| 90 | 90 | // Post ID has to match an existing item |
| 91 | - if ( null === $current_post ) { |
|
| 92 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 91 | + if (null === $current_post) { |
|
| 92 | + wp_send_json_error('No valid post_id given'); |
|
| 93 | 93 | |
| 94 | 94 | return array(); |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | // Determine navigator type and call respective _get_results |
| 98 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 98 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 99 | 99 | |
| 100 | 100 | $referencing_posts = Navigator_Data::entity_navigator_get_results( |
| 101 | 101 | $current_post_id, |
@@ -125,8 +125,8 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | // loop over them and take the first one which is not already in the $related_posts |
| 127 | 127 | $results = array(); |
| 128 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 129 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 128 | + foreach ($referencing_posts as $referencing_post) { |
|
| 129 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | 132 | * Use the thumbnail. |
@@ -136,36 +136,36 @@ discard block |
||
| 136 | 136 | * |
| 137 | 137 | * @since 3.19.3 We're using the medium size image. |
| 138 | 138 | */ |
| 139 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 139 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 140 | 140 | |
| 141 | 141 | $result = array( |
| 142 | 142 | 'post' => array( |
| 143 | 143 | 'id' => $referencing_post->ID, |
| 144 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 145 | - 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 144 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 145 | + 'title' => html_entity_decode($referencing_post->post_title, ENT_QUOTES, 'UTF-8'), |
|
| 146 | 146 | 'thumbnail' => $thumbnail, |
| 147 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ), |
|
| 147 | + 'srcset' => Srcset_Util::get_srcset($referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET), |
|
| 148 | 148 | ), |
| 149 | 149 | 'entity' => array( |
| 150 | 150 | 'id' => $referencing_post->entity_id, |
| 151 | 151 | 'label' => $serialized_entity['label'], |
| 152 | 152 | 'mainType' => $serialized_entity['mainType'], |
| 153 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 153 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 154 | 154 | ), |
| 155 | 155 | ); |
| 156 | 156 | |
| 157 | 157 | $results[] = $result; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - if ( count( $results ) < $navigator_length ) { |
|
| 161 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 160 | + if (count($results) < $navigator_length) { |
|
| 161 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | // Add filler posts if needed |
| 165 | - $filler_count = $navigator_length - count( $results ); |
|
| 166 | - if ( $filler_count > 0 ) { |
|
| 165 | + $filler_count = $navigator_length - count($results); |
|
| 166 | + if ($filler_count > 0) { |
|
| 167 | 167 | $referencing_post_ids = array_map( |
| 168 | - function ( $p ) { |
|
| 168 | + function($p) { |
|
| 169 | 169 | return $p->ID; |
| 170 | 170 | }, |
| 171 | 171 | $referencing_posts |
@@ -174,19 +174,19 @@ discard block |
||
| 174 | 174 | * @since 3.27.8 |
| 175 | 175 | * Filler posts are fetched using this util. |
| 176 | 176 | */ |
| 177 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 178 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 179 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 180 | - $results = array_merge( $results, $filler_posts ); |
|
| 177 | + $filler_posts_util = new Filler_Posts_Util($current_post_id, $post_types); |
|
| 178 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 179 | + $filler_posts = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded); |
|
| 180 | + $results = array_merge($results, $filler_posts); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | // Apply filters after fillers are added |
| 184 | - foreach ( $results as $result_index => $result ) { |
|
| 185 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 186 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 184 | + foreach ($results as $result_index => $result) { |
|
| 185 | + $results[$result_index]['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id); |
|
| 186 | + $results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id, $current_post_id ); |
|
| 189 | + $results = apply_filters('wl_navigator_results', $results, $navigator_id, $current_post_id); |
|
| 190 | 190 | |
| 191 | 191 | return $results; |
| 192 | 192 | } |
@@ -200,12 +200,12 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | // Temporary blocking the Navigator. |
| 202 | 202 | $results = wl_shortcode_navigator_data(); |
| 203 | - wl_core_send_json( $results ); |
|
| 203 | + wl_core_send_json($results); |
|
| 204 | 204 | |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 208 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 207 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 208 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 209 | 209 | |
| 210 | 210 | /** |
| 211 | 211 | * wp-json call for the navigator widget |
@@ -213,13 +213,13 @@ discard block |
||
| 213 | 213 | function wl_shortcode_navigator_wp_json() { |
| 214 | 214 | |
| 215 | 215 | $results = wl_shortcode_navigator_data(); |
| 216 | - if ( ob_get_contents() ) { |
|
| 216 | + if (ob_get_contents()) { |
|
| 217 | 217 | ob_clean(); |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | return array( |
| 221 | 221 | 'items' => array( |
| 222 | - array( 'values' => $results ), |
|
| 222 | + array('values' => $results), |
|
| 223 | 223 | ), |
| 224 | 224 | ); |
| 225 | 225 | |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | */ |
| 231 | 231 | add_action( |
| 232 | 232 | 'rest_api_init', |
| 233 | - function () { |
|
| 233 | + function() { |
|
| 234 | 234 | register_rest_route( |
| 235 | 235 | WL_REST_ROUTE_DEFAULT_NAMESPACE, |
| 236 | 236 | '/navigator', |
@@ -250,29 +250,29 @@ discard block |
||
| 250 | 250 | */ |
| 251 | 251 | add_action( |
| 252 | 252 | 'plugins_loaded', |
| 253 | - function () { |
|
| 254 | - $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 255 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 253 | + function() { |
|
| 254 | + $action = array_key_exists('action', $_REQUEST) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 255 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 256 | 256 | return; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 260 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 261 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 259 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 260 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 261 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 262 | 262 | }, |
| 263 | 263 | 0 |
| 264 | 264 | ); |
| 265 | 265 | |
| 266 | 266 | add_action( |
| 267 | 267 | 'init', |
| 268 | - function () { |
|
| 269 | - $action = array_key_exists( 'action', $_REQUEST ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['action'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 270 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 268 | + function() { |
|
| 269 | + $action = array_key_exists('action', $_REQUEST) ? sanitize_text_field(wp_unslash((string) $_REQUEST['action'])) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 270 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 271 | 271 | return; |
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 275 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 274 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 275 | + remove_action('init', 'gglcptch_init'); |
|
| 276 | 276 | }, |
| 277 | 277 | 0 |
| 278 | 278 | ); |