@@ -315,7 +315,7 @@ |
||
| 315 | 315 | |
| 316 | 316 | /** |
| 317 | 317 | * @param $post |
| 318 | - * @param $delimiter |
|
| 318 | + * @param string $delimiter |
|
| 319 | 319 | * @param $limit |
| 320 | 320 | * |
| 321 | 321 | * @param $post_types |
@@ -17,117 +17,117 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Faceted_Search_Shortcode extends Wordlift_Shortcode { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - const SHORTCODE = 'wl_faceted_search'; |
|
| 24 | - |
|
| 25 | - public function __construct() { |
|
| 26 | - parent::__construct(); |
|
| 27 | - $this->register_block_type(); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - */ |
|
| 33 | - public function render( $atts ) { |
|
| 34 | - |
|
| 35 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | - : $this->web_shortcode( $atts ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - private function register_block_type() { |
|
| 40 | - |
|
| 41 | - $scope = $this; |
|
| 42 | - |
|
| 43 | - add_action( 'init', function () use ( $scope ) { |
|
| 44 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 45 | - // Gutenberg is not active. |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - register_block_type( 'wordlift/faceted-search', array( |
|
| 50 | - 'editor_script' => 'wl-block-editor', |
|
| 51 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 52 | - $attr_code = ''; |
|
| 53 | - foreach ( $attributes as $key => $value ) { |
|
| 54 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 58 | - }, |
|
| 59 | - |
|
| 60 | - 'attributes' => $this->get_block_attributes(), |
|
| 61 | - ) ); |
|
| 62 | - } ); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 67 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 68 | - * |
|
| 69 | - * @param array $atts Shortcode attributes. |
|
| 70 | - * |
|
| 71 | - * @return array $shortcode_atts |
|
| 72 | - * @since 3.20.0 |
|
| 73 | - * |
|
| 74 | - */ |
|
| 75 | - private function make_shortcode_atts( $atts ) { |
|
| 76 | - |
|
| 77 | - // Extract attributes and set default values. |
|
| 78 | - $shortcode_atts = shortcode_atts( array( |
|
| 79 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | - 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 81 | - 'post_id' => '', |
|
| 82 | - 'template_id' => '', |
|
| 83 | - 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 84 | - 'post_types' => '', |
|
| 85 | - ), $atts ); |
|
| 86 | - |
|
| 87 | - return $shortcode_atts; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 92 | - * |
|
| 93 | - * @param array $atts Shortcode attributes. |
|
| 94 | - * |
|
| 95 | - * @return string Shortcode HTML for web |
|
| 96 | - * @since 3.20.0 |
|
| 97 | - * |
|
| 98 | - */ |
|
| 99 | - private function web_shortcode( $atts ) { |
|
| 100 | - |
|
| 101 | - // attributes extraction and boolean filtering |
|
| 102 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 103 | - |
|
| 104 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 105 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 110 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 111 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 112 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 113 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 114 | - |
|
| 115 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 116 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 117 | - $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 118 | - $rest_url = esc_attr( $rest_url ); |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - // avoid building the widget when no valid $rest_url |
|
| 122 | - if ( ! $rest_url ) { |
|
| 123 | - return; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 127 | - $json_faceted_id = wp_json_encode( $faceted_id ); |
|
| 128 | - $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 129 | - |
|
| 130 | - return <<<HTML |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + const SHORTCODE = 'wl_faceted_search'; |
|
| 24 | + |
|
| 25 | + public function __construct() { |
|
| 26 | + parent::__construct(); |
|
| 27 | + $this->register_block_type(); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + */ |
|
| 33 | + public function render( $atts ) { |
|
| 34 | + |
|
| 35 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | + : $this->web_shortcode( $atts ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + private function register_block_type() { |
|
| 40 | + |
|
| 41 | + $scope = $this; |
|
| 42 | + |
|
| 43 | + add_action( 'init', function () use ( $scope ) { |
|
| 44 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 45 | + // Gutenberg is not active. |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + register_block_type( 'wordlift/faceted-search', array( |
|
| 50 | + 'editor_script' => 'wl-block-editor', |
|
| 51 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 52 | + $attr_code = ''; |
|
| 53 | + foreach ( $attributes as $key => $value ) { |
|
| 54 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 58 | + }, |
|
| 59 | + |
|
| 60 | + 'attributes' => $this->get_block_attributes(), |
|
| 61 | + ) ); |
|
| 62 | + } ); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 67 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 68 | + * |
|
| 69 | + * @param array $atts Shortcode attributes. |
|
| 70 | + * |
|
| 71 | + * @return array $shortcode_atts |
|
| 72 | + * @since 3.20.0 |
|
| 73 | + * |
|
| 74 | + */ |
|
| 75 | + private function make_shortcode_atts( $atts ) { |
|
| 76 | + |
|
| 77 | + // Extract attributes and set default values. |
|
| 78 | + $shortcode_atts = shortcode_atts( array( |
|
| 79 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | + 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 81 | + 'post_id' => '', |
|
| 82 | + 'template_id' => '', |
|
| 83 | + 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 84 | + 'post_types' => '', |
|
| 85 | + ), $atts ); |
|
| 86 | + |
|
| 87 | + return $shortcode_atts; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 92 | + * |
|
| 93 | + * @param array $atts Shortcode attributes. |
|
| 94 | + * |
|
| 95 | + * @return string Shortcode HTML for web |
|
| 96 | + * @since 3.20.0 |
|
| 97 | + * |
|
| 98 | + */ |
|
| 99 | + private function web_shortcode( $atts ) { |
|
| 100 | + |
|
| 101 | + // attributes extraction and boolean filtering |
|
| 102 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 103 | + |
|
| 104 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 105 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 110 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 111 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 112 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 113 | + $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 114 | + |
|
| 115 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 116 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 117 | + $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 118 | + $rest_url = esc_attr( $rest_url ); |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + // avoid building the widget when no valid $rest_url |
|
| 122 | + if ( ! $rest_url ) { |
|
| 123 | + return; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 127 | + $json_faceted_id = wp_json_encode( $faceted_id ); |
|
| 128 | + $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 129 | + |
|
| 130 | + return <<<HTML |
|
| 131 | 131 | <!-- Faceted {$faceted_id} --> |
| 132 | 132 | <script type="application/javascript"> |
| 133 | 133 | window.wlFaceteds = window.wlFaceteds || []; wlFaceteds.push({$json_faceted_id}); |
@@ -140,58 +140,58 @@ discard block |
||
| 140 | 140 | data-template-url="{$template_url}"></div> |
| 141 | 141 | <!-- /Faceted {$faceted_id} --> |
| 142 | 142 | HTML; |
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 147 | - * |
|
| 148 | - * @param array $atts Shortcode attributes. |
|
| 149 | - * |
|
| 150 | - * @return string Shortcode HTML for amp |
|
| 151 | - * @since 3.20.0 |
|
| 152 | - * |
|
| 153 | - */ |
|
| 154 | - private function amp_shortcode( $atts ) { |
|
| 155 | - |
|
| 156 | - // attributes extraction and boolean filtering |
|
| 157 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 158 | - |
|
| 159 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 160 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 161 | - return; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 165 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 166 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 167 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 168 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 169 | - |
|
| 170 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 171 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 172 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 173 | - 'amp', |
|
| 174 | - 'post_id' => $post->ID, |
|
| 175 | - 'limit' => $limit |
|
| 176 | - ) ) ) : false; |
|
| 177 | - |
|
| 178 | - $rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false; |
|
| 179 | - |
|
| 180 | - // avoid building the widget when no valid $rest_url |
|
| 181 | - if ( ! $rest_url ) { |
|
| 182 | - return; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 186 | - // This is a hackish way, but this works for http and https URLs |
|
| 187 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 188 | - |
|
| 189 | - if ( empty( $template_id ) ) { |
|
| 190 | - $template_id = "template-" . $faceted_id; |
|
| 191 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return <<<HTML |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 147 | + * |
|
| 148 | + * @param array $atts Shortcode attributes. |
|
| 149 | + * |
|
| 150 | + * @return string Shortcode HTML for amp |
|
| 151 | + * @since 3.20.0 |
|
| 152 | + * |
|
| 153 | + */ |
|
| 154 | + private function amp_shortcode( $atts ) { |
|
| 155 | + |
|
| 156 | + // attributes extraction and boolean filtering |
|
| 157 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 158 | + |
|
| 159 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 160 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 161 | + return; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 165 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 166 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 167 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 168 | + $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 169 | + |
|
| 170 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 171 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 172 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 173 | + 'amp', |
|
| 174 | + 'post_id' => $post->ID, |
|
| 175 | + 'limit' => $limit |
|
| 176 | + ) ) ) : false; |
|
| 177 | + |
|
| 178 | + $rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false; |
|
| 179 | + |
|
| 180 | + // avoid building the widget when no valid $rest_url |
|
| 181 | + if ( ! $rest_url ) { |
|
| 182 | + return; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 186 | + // This is a hackish way, but this works for http and https URLs |
|
| 187 | + $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 188 | + |
|
| 189 | + if ( empty( $template_id ) ) { |
|
| 190 | + $template_id = "template-" . $faceted_id; |
|
| 191 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return <<<HTML |
|
| 195 | 195 | <div id="{$faceted_id}" class="wl-amp-faceted"> |
| 196 | 196 | <h2 class="wl-headline">{$title}</h2> |
| 197 | 197 | <amp-state id="referencedPosts"> |
@@ -274,62 +274,62 @@ discard block |
||
| 274 | 274 | </section> |
| 275 | 275 | </div> |
| 276 | 276 | HTML; |
| 277 | - } |
|
| 278 | - |
|
| 279 | - public function get_block_attributes() { |
|
| 280 | - return array( |
|
| 281 | - 'title' => array( |
|
| 282 | - 'type' => 'string', |
|
| 283 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 284 | - ), |
|
| 285 | - 'template_id' => array( |
|
| 286 | - 'type' => 'string', |
|
| 287 | - 'default' => '', |
|
| 288 | - ), |
|
| 289 | - 'post_id' => array( |
|
| 290 | - 'type' => 'number', |
|
| 291 | - 'default' => '', |
|
| 292 | - ), |
|
| 293 | - 'uniqid' => array( |
|
| 294 | - 'type' => 'string', |
|
| 295 | - 'default' => '', |
|
| 296 | - ), |
|
| 297 | - 'limit' => array( |
|
| 298 | - 'type' => 'number', |
|
| 299 | - 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 300 | - ), |
|
| 301 | - 'preview' => array( |
|
| 302 | - 'type' => 'boolean', |
|
| 303 | - 'default' => false, |
|
| 304 | - ), |
|
| 305 | - 'preview_src' => array( |
|
| 306 | - 'type' => 'string', |
|
| 307 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 308 | - ), |
|
| 309 | - 'post_types' => array( |
|
| 310 | - 'type' => 'string', |
|
| 311 | - 'default' => '', |
|
| 312 | - ) |
|
| 313 | - ); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * @param $post |
|
| 318 | - * @param $delimiter |
|
| 319 | - * @param $limit |
|
| 320 | - * |
|
| 321 | - * @param $post_types |
|
| 322 | - * |
|
| 323 | - * @return bool|string |
|
| 324 | - */ |
|
| 325 | - public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 326 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 327 | - 'post_id' => $post->ID, |
|
| 328 | - 'limit' => $limit, |
|
| 329 | - 'post_types' => $post_types |
|
| 330 | - ) ) ) : false; |
|
| 331 | - |
|
| 332 | - return $rest_url; |
|
| 333 | - } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + public function get_block_attributes() { |
|
| 280 | + return array( |
|
| 281 | + 'title' => array( |
|
| 282 | + 'type' => 'string', |
|
| 283 | + 'default' => __( 'Related articles', 'wordlift' ), |
|
| 284 | + ), |
|
| 285 | + 'template_id' => array( |
|
| 286 | + 'type' => 'string', |
|
| 287 | + 'default' => '', |
|
| 288 | + ), |
|
| 289 | + 'post_id' => array( |
|
| 290 | + 'type' => 'number', |
|
| 291 | + 'default' => '', |
|
| 292 | + ), |
|
| 293 | + 'uniqid' => array( |
|
| 294 | + 'type' => 'string', |
|
| 295 | + 'default' => '', |
|
| 296 | + ), |
|
| 297 | + 'limit' => array( |
|
| 298 | + 'type' => 'number', |
|
| 299 | + 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 300 | + ), |
|
| 301 | + 'preview' => array( |
|
| 302 | + 'type' => 'boolean', |
|
| 303 | + 'default' => false, |
|
| 304 | + ), |
|
| 305 | + 'preview_src' => array( |
|
| 306 | + 'type' => 'string', |
|
| 307 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 308 | + ), |
|
| 309 | + 'post_types' => array( |
|
| 310 | + 'type' => 'string', |
|
| 311 | + 'default' => '', |
|
| 312 | + ) |
|
| 313 | + ); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * @param $post |
|
| 318 | + * @param $delimiter |
|
| 319 | + * @param $limit |
|
| 320 | + * |
|
| 321 | + * @param $post_types |
|
| 322 | + * |
|
| 323 | + * @return bool|string |
|
| 324 | + */ |
|
| 325 | + public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 326 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 327 | + 'post_id' => $post->ID, |
|
| 328 | + 'limit' => $limit, |
|
| 329 | + 'post_types' => $post_types |
|
| 330 | + ) ) ) : false; |
|
| 331 | + |
|
| 332 | + return $rest_url; |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | 335 | } |
@@ -30,35 +30,35 @@ discard block |
||
| 30 | 30 | /** |
| 31 | 31 | * {@inheritdoc} |
| 32 | 32 | */ |
| 33 | - public function render( $atts ) { |
|
| 33 | + public function render($atts) { |
|
| 34 | 34 | |
| 35 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | - : $this->web_shortcode( $atts ); |
|
| 35 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode($atts) |
|
| 36 | + : $this->web_shortcode($atts); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | private function register_block_type() { |
| 40 | 40 | |
| 41 | 41 | $scope = $this; |
| 42 | 42 | |
| 43 | - add_action( 'init', function () use ( $scope ) { |
|
| 44 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 43 | + add_action('init', function() use ($scope) { |
|
| 44 | + if ( ! function_exists('register_block_type')) { |
|
| 45 | 45 | // Gutenberg is not active. |
| 46 | 46 | return; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - register_block_type( 'wordlift/faceted-search', array( |
|
| 49 | + register_block_type('wordlift/faceted-search', array( |
|
| 50 | 50 | 'editor_script' => 'wl-block-editor', |
| 51 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 51 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 52 | 52 | $attr_code = ''; |
| 53 | - foreach ( $attributes as $key => $value ) { |
|
| 54 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 53 | + foreach ($attributes as $key => $value) { |
|
| 54 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 57 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 58 | 58 | }, |
| 59 | 59 | |
| 60 | 60 | 'attributes' => $this->get_block_attributes(), |
| 61 | - ) ); |
|
| 61 | + )); |
|
| 62 | 62 | } ); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -72,17 +72,17 @@ discard block |
||
| 72 | 72 | * @since 3.20.0 |
| 73 | 73 | * |
| 74 | 74 | */ |
| 75 | - private function make_shortcode_atts( $atts ) { |
|
| 75 | + private function make_shortcode_atts($atts) { |
|
| 76 | 76 | |
| 77 | 77 | // Extract attributes and set default values. |
| 78 | - $shortcode_atts = shortcode_atts( array( |
|
| 79 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | - 'limit' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 78 | + $shortcode_atts = shortcode_atts(array( |
|
| 79 | + 'title' => __('Related articles', 'wordlift'), |
|
| 80 | + 'limit' => apply_filters('wl_faceted_search_default_limit', 10), |
|
| 81 | 81 | 'post_id' => '', |
| 82 | 82 | 'template_id' => '', |
| 83 | - 'uniqid' => uniqid( 'wl-faceted-widget-' ), |
|
| 83 | + 'uniqid' => uniqid('wl-faceted-widget-'), |
|
| 84 | 84 | 'post_types' => '', |
| 85 | - ), $atts ); |
|
| 85 | + ), $atts); |
|
| 86 | 86 | |
| 87 | 87 | return $shortcode_atts; |
| 88 | 88 | } |
@@ -96,36 +96,36 @@ discard block |
||
| 96 | 96 | * @since 3.20.0 |
| 97 | 97 | * |
| 98 | 98 | */ |
| 99 | - private function web_shortcode( $atts ) { |
|
| 99 | + private function web_shortcode($atts) { |
|
| 100 | 100 | |
| 101 | 101 | // attributes extraction and boolean filtering |
| 102 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 102 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 103 | 103 | |
| 104 | 104 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 105 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 105 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 106 | 106 | return; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 110 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 111 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 112 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 113 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 109 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 110 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 111 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 112 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 113 | + $faceted_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-'); |
|
| 114 | 114 | |
| 115 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 116 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 117 | - $rest_url = $this->get_rest_url( $post, $delimiter, $limit, $shortcode_atts['post_types'] ); |
|
| 118 | - $rest_url = esc_attr( $rest_url ); |
|
| 115 | + $permalink_structure = get_option('permalink_structure'); |
|
| 116 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 117 | + $rest_url = $this->get_rest_url($post, $delimiter, $limit, $shortcode_atts['post_types']); |
|
| 118 | + $rest_url = esc_attr($rest_url); |
|
| 119 | 119 | |
| 120 | 120 | |
| 121 | 121 | // avoid building the widget when no valid $rest_url |
| 122 | - if ( ! $rest_url ) { |
|
| 122 | + if ( ! $rest_url) { |
|
| 123 | 123 | return; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 127 | - $json_faceted_id = wp_json_encode( $faceted_id ); |
|
| 128 | - $template_url = get_rest_url( null, '/wordlift/v1/faceted-search/template' ); |
|
| 126 | + wp_enqueue_script('wordlift-cloud'); |
|
| 127 | + $json_faceted_id = wp_json_encode($faceted_id); |
|
| 128 | + $template_url = get_rest_url(null, '/wordlift/v1/faceted-search/template'); |
|
| 129 | 129 | |
| 130 | 130 | return <<<HTML |
| 131 | 131 | <!-- Faceted {$faceted_id} --> |
@@ -151,44 +151,44 @@ discard block |
||
| 151 | 151 | * @since 3.20.0 |
| 152 | 152 | * |
| 153 | 153 | */ |
| 154 | - private function amp_shortcode( $atts ) { |
|
| 154 | + private function amp_shortcode($atts) { |
|
| 155 | 155 | |
| 156 | 156 | // attributes extraction and boolean filtering |
| 157 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 157 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 158 | 158 | |
| 159 | 159 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 160 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 160 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 161 | 161 | return; |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 165 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 166 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 167 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 168 | - $faceted_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-faceted-widget-' ); |
|
| 164 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 165 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 166 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 167 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 168 | + $faceted_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-faceted-widget-'); |
|
| 169 | 169 | |
| 170 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 171 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 172 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 170 | + $permalink_structure = get_option('permalink_structure'); |
|
| 171 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 172 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query(array( |
|
| 173 | 173 | 'amp', |
| 174 | 174 | 'post_id' => $post->ID, |
| 175 | 175 | 'limit' => $limit |
| 176 | - ) ) ) : false; |
|
| 176 | + ))) : false; |
|
| 177 | 177 | |
| 178 | - $rest_url = $post ? rest_url( sprintf( "wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit ) ) : false; |
|
| 178 | + $rest_url = $post ? rest_url(sprintf("wordlift/v1/faceted-search?amp&post_id=%s&limit=%s", $post->ID, $limit)) : false; |
|
| 179 | 179 | |
| 180 | 180 | // avoid building the widget when no valid $rest_url |
| 181 | - if ( ! $rest_url ) { |
|
| 181 | + if ( ! $rest_url) { |
|
| 182 | 182 | return; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 186 | 186 | // This is a hackish way, but this works for http and https URLs |
| 187 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 187 | + $rest_url = str_replace(array('http:', 'https:'), '', $rest_url); |
|
| 188 | 188 | |
| 189 | - if ( empty( $template_id ) ) { |
|
| 190 | - $template_id = "template-" . $faceted_id; |
|
| 191 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 189 | + if (empty($template_id)) { |
|
| 190 | + $template_id = "template-".$faceted_id; |
|
| 191 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css'); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | return <<<HTML |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | return array( |
| 281 | 281 | 'title' => array( |
| 282 | 282 | 'type' => 'string', |
| 283 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 283 | + 'default' => __('Related articles', 'wordlift'), |
|
| 284 | 284 | ), |
| 285 | 285 | 'template_id' => array( |
| 286 | 286 | 'type' => 'string', |
@@ -296,7 +296,7 @@ discard block |
||
| 296 | 296 | ), |
| 297 | 297 | 'limit' => array( |
| 298 | 298 | 'type' => 'number', |
| 299 | - 'default' => apply_filters( 'wl_faceted_search_default_limit', 10 ), |
|
| 299 | + 'default' => apply_filters('wl_faceted_search_default_limit', 10), |
|
| 300 | 300 | ), |
| 301 | 301 | 'preview' => array( |
| 302 | 302 | 'type' => 'boolean', |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | ), |
| 305 | 305 | 'preview_src' => array( |
| 306 | 306 | 'type' => 'string', |
| 307 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 307 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/faceted-search.png', |
|
| 308 | 308 | ), |
| 309 | 309 | 'post_types' => array( |
| 310 | 310 | 'type' => 'string', |
@@ -322,12 +322,12 @@ discard block |
||
| 322 | 322 | * |
| 323 | 323 | * @return bool|string |
| 324 | 324 | */ |
| 325 | - public function get_rest_url( $post, $delimiter, $limit, $post_types ) { |
|
| 326 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faceted-search' . $delimiter . build_query( array( |
|
| 325 | + public function get_rest_url($post, $delimiter, $limit, $post_types) { |
|
| 326 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faceted-search'.$delimiter.build_query(array( |
|
| 327 | 327 | 'post_id' => $post->ID, |
| 328 | 328 | 'limit' => $limit, |
| 329 | 329 | 'post_types' => $post_types |
| 330 | - ) ) ) : false; |
|
| 330 | + ))) : false; |
|
| 331 | 331 | |
| 332 | 332 | return $rest_url; |
| 333 | 333 | } |
@@ -8,7 +8,6 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | use Wordlift\Cache\Ttl_Cache; |
| 11 | -use Wordlift\Widgets\Navigator\Filler_Posts; |
|
| 12 | 11 | use Wordlift\Widgets\Navigator\Filler_Posts\Filler_Posts_Util; |
| 13 | 12 | use Wordlift\Widgets\Navigator\Navigator_Data; |
| 14 | 13 | |
@@ -19,29 +19,29 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | function wl_shortcode_navigator_data() { |
| 21 | 21 | |
| 22 | - // Create the cache key. |
|
| 23 | - $cache_key_params = $_REQUEST; |
|
| 24 | - unset( $cache_key_params['uniqid'] ); |
|
| 25 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 22 | + // Create the cache key. |
|
| 23 | + $cache_key_params = $_REQUEST; |
|
| 24 | + unset( $cache_key_params['uniqid'] ); |
|
| 25 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 26 | 26 | |
| 27 | - // Create the TTL cache and try to get the results. |
|
| 28 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | - $cache_results = $cache->get( $cache_key ); |
|
| 27 | + // Create the TTL cache and try to get the results. |
|
| 28 | + $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | + $cache_results = $cache->get( $cache_key ); |
|
| 30 | 30 | |
| 31 | - if ( isset( $cache_results ) ) { |
|
| 32 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 31 | + if ( isset( $cache_results ) ) { |
|
| 32 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 33 | 33 | |
| 34 | - return $cache_results; |
|
| 35 | - } |
|
| 34 | + return $cache_results; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 37 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 38 | 38 | |
| 39 | - $results = _wl_navigator_get_data(); |
|
| 39 | + $results = _wl_navigator_get_data(); |
|
| 40 | 40 | |
| 41 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 42 | - $cache->put( $cache_key, $results ); |
|
| 41 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 42 | + $cache->put( $cache_key, $results ); |
|
| 43 | 43 | |
| 44 | - return $results; |
|
| 44 | + return $results; |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
@@ -56,234 +56,234 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | function wl_network_navigator_wp_json( $request ) { |
| 58 | 58 | |
| 59 | - // Create the cache key. |
|
| 60 | - $cache_key_params = $_REQUEST; |
|
| 61 | - unset( $cache_key_params['uniqid'] ); |
|
| 62 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 59 | + // Create the cache key. |
|
| 60 | + $cache_key_params = $_REQUEST; |
|
| 61 | + unset( $cache_key_params['uniqid'] ); |
|
| 62 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 63 | 63 | |
| 64 | - // Create the TTL cache and try to get the results. |
|
| 65 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 66 | - $cache_results = $cache->get( $cache_key ); |
|
| 64 | + // Create the TTL cache and try to get the results. |
|
| 65 | + $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 66 | + $cache_results = $cache->get( $cache_key ); |
|
| 67 | 67 | |
| 68 | - if ( isset( $cache_results ) ) { |
|
| 69 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 68 | + if ( isset( $cache_results ) ) { |
|
| 69 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 70 | 70 | |
| 71 | - return $cache_results; |
|
| 72 | - } |
|
| 71 | + return $cache_results; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 74 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 75 | 75 | |
| 76 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 76 | + $results = _wl_network_navigator_get_data( $request ); |
|
| 77 | 77 | |
| 78 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 79 | - $cache->put( $cache_key, $results ); |
|
| 78 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 79 | + $cache->put( $cache_key, $results ); |
|
| 80 | 80 | |
| 81 | - return $results; |
|
| 81 | + return $results; |
|
| 82 | 82 | |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | function _wl_navigator_get_data() { |
| 86 | 86 | |
| 87 | - // Post ID must be defined |
|
| 88 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 89 | - wp_send_json_error( 'No post_id given' ); |
|
| 90 | - |
|
| 91 | - return array(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // Post ID must be defined |
|
| 95 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 96 | - wp_send_json_error( 'No uniqid given' ); |
|
| 97 | - |
|
| 98 | - return array(); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // Limit the results (defaults to 4) |
|
| 102 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 103 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 104 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 105 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 106 | - $post_types = explode( ',', $post_types ); |
|
| 107 | - $existing_post_types = get_post_types(); |
|
| 108 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 109 | - $current_post_id = $_GET['post_id']; |
|
| 110 | - $current_post = get_post( $current_post_id ); |
|
| 111 | - |
|
| 112 | - $navigator_id = $_GET['uniqid']; |
|
| 113 | - |
|
| 114 | - // Post ID has to match an existing item |
|
| 115 | - if ( null === $current_post ) { |
|
| 116 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 117 | - |
|
| 118 | - return array(); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // Determine navigator type and call respective _get_results |
|
| 122 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 123 | - |
|
| 124 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 125 | - 'ID', |
|
| 126 | - 'post_title', |
|
| 127 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 128 | - } else { |
|
| 129 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 130 | - 'ID', |
|
| 131 | - 'post_title', |
|
| 132 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 136 | - $results = array(); |
|
| 137 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 138 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Use the thumbnail. |
|
| 142 | - * |
|
| 143 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 144 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 145 | - * |
|
| 146 | - * @since 3.19.3 We're using the medium size image. |
|
| 147 | - */ |
|
| 148 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 149 | - |
|
| 150 | - $result = array( |
|
| 151 | - 'post' => array( |
|
| 152 | - 'id' => $referencing_post->ID, |
|
| 153 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 154 | - 'title' => $referencing_post->post_title, |
|
| 155 | - 'thumbnail' => $thumbnail, |
|
| 156 | - ), |
|
| 157 | - 'entity' => array( |
|
| 158 | - 'id' => $referencing_post->entity_id, |
|
| 159 | - 'label' => $serialized_entity['label'], |
|
| 160 | - 'mainType' => $serialized_entity['mainType'], |
|
| 161 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 162 | - ), |
|
| 163 | - ); |
|
| 164 | - |
|
| 165 | - $results[] = $result; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - if ( count( $results ) < $navigator_length ) { |
|
| 170 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Add filler posts if needed |
|
| 174 | - $filler_count = $navigator_length - count( $results ); |
|
| 175 | - if ( $filler_count > 0 ) { |
|
| 176 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 177 | - return $p->ID; |
|
| 178 | - }, $referencing_posts ); |
|
| 179 | - /** |
|
| 180 | - * @since 3.27.8 |
|
| 181 | - * Filler posts are fetched using this util. |
|
| 182 | - */ |
|
| 183 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 184 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 185 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 186 | - $results = array_merge( $filler_posts, $results ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - // Apply filters after fillers are added |
|
| 190 | - foreach ( $results as $result_index => $result ) { |
|
| 191 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 192 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return $results; |
|
| 87 | + // Post ID must be defined |
|
| 88 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 89 | + wp_send_json_error( 'No post_id given' ); |
|
| 90 | + |
|
| 91 | + return array(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // Post ID must be defined |
|
| 95 | + if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 96 | + wp_send_json_error( 'No uniqid given' ); |
|
| 97 | + |
|
| 98 | + return array(); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // Limit the results (defaults to 4) |
|
| 102 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 103 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 104 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 105 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 106 | + $post_types = explode( ',', $post_types ); |
|
| 107 | + $existing_post_types = get_post_types(); |
|
| 108 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 109 | + $current_post_id = $_GET['post_id']; |
|
| 110 | + $current_post = get_post( $current_post_id ); |
|
| 111 | + |
|
| 112 | + $navigator_id = $_GET['uniqid']; |
|
| 113 | + |
|
| 114 | + // Post ID has to match an existing item |
|
| 115 | + if ( null === $current_post ) { |
|
| 116 | + wp_send_json_error( 'No valid post_id given' ); |
|
| 117 | + |
|
| 118 | + return array(); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // Determine navigator type and call respective _get_results |
|
| 122 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 123 | + |
|
| 124 | + $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 125 | + 'ID', |
|
| 126 | + 'post_title', |
|
| 127 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 128 | + } else { |
|
| 129 | + $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 130 | + 'ID', |
|
| 131 | + 'post_title', |
|
| 132 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 136 | + $results = array(); |
|
| 137 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 138 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Use the thumbnail. |
|
| 142 | + * |
|
| 143 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 144 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 145 | + * |
|
| 146 | + * @since 3.19.3 We're using the medium size image. |
|
| 147 | + */ |
|
| 148 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 149 | + |
|
| 150 | + $result = array( |
|
| 151 | + 'post' => array( |
|
| 152 | + 'id' => $referencing_post->ID, |
|
| 153 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 154 | + 'title' => $referencing_post->post_title, |
|
| 155 | + 'thumbnail' => $thumbnail, |
|
| 156 | + ), |
|
| 157 | + 'entity' => array( |
|
| 158 | + 'id' => $referencing_post->entity_id, |
|
| 159 | + 'label' => $serialized_entity['label'], |
|
| 160 | + 'mainType' => $serialized_entity['mainType'], |
|
| 161 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 162 | + ), |
|
| 163 | + ); |
|
| 164 | + |
|
| 165 | + $results[] = $result; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + if ( count( $results ) < $navigator_length ) { |
|
| 170 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // Add filler posts if needed |
|
| 174 | + $filler_count = $navigator_length - count( $results ); |
|
| 175 | + if ( $filler_count > 0 ) { |
|
| 176 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 177 | + return $p->ID; |
|
| 178 | + }, $referencing_posts ); |
|
| 179 | + /** |
|
| 180 | + * @since 3.27.8 |
|
| 181 | + * Filler posts are fetched using this util. |
|
| 182 | + */ |
|
| 183 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 184 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 185 | + $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 186 | + $results = array_merge( $filler_posts, $results ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + // Apply filters after fillers are added |
|
| 190 | + foreach ( $results as $result_index => $result ) { |
|
| 191 | + $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 192 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return $results; |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | function _wl_network_navigator_get_data( $request ) { |
| 199 | 199 | |
| 200 | - // Limit the results (defaults to 4) |
|
| 201 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 202 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 203 | - $navigator_id = $request['uniqid']; |
|
| 204 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 205 | - |
|
| 206 | - $entities = $request['entities']; |
|
| 207 | - |
|
| 208 | - // Post ID has to match an existing item |
|
| 209 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 210 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 214 | - 'ID', |
|
| 215 | - 'post_title', |
|
| 216 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 217 | - |
|
| 218 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 219 | - $results = array(); |
|
| 220 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 221 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Use the thumbnail. |
|
| 225 | - * |
|
| 226 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 227 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 228 | - * |
|
| 229 | - * @since 3.19.3 We're using the medium size image. |
|
| 230 | - */ |
|
| 231 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 232 | - |
|
| 233 | - $result = array( |
|
| 234 | - 'post' => array( |
|
| 235 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 236 | - 'title' => $referencing_post->post_title, |
|
| 237 | - 'thumbnail' => $thumbnail, |
|
| 238 | - ), |
|
| 239 | - 'entity' => array( |
|
| 240 | - 'label' => $serialized_entity['label'], |
|
| 241 | - 'mainType' => $serialized_entity['mainType'], |
|
| 242 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 243 | - ), |
|
| 244 | - ); |
|
| 245 | - |
|
| 246 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 247 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 248 | - |
|
| 249 | - $results[] = $result; |
|
| 250 | - |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - if ( count( $results ) < $navigator_length ) { |
|
| 254 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - return $results; |
|
| 200 | + // Limit the results (defaults to 4) |
|
| 201 | + $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 202 | + $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 203 | + $navigator_id = $request['uniqid']; |
|
| 204 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 205 | + |
|
| 206 | + $entities = $request['entities']; |
|
| 207 | + |
|
| 208 | + // Post ID has to match an existing item |
|
| 209 | + if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 210 | + wp_send_json_error( 'No valid entities provided' ); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 214 | + 'ID', |
|
| 215 | + 'post_title', |
|
| 216 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 217 | + |
|
| 218 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 219 | + $results = array(); |
|
| 220 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 221 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Use the thumbnail. |
|
| 225 | + * |
|
| 226 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 227 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 228 | + * |
|
| 229 | + * @since 3.19.3 We're using the medium size image. |
|
| 230 | + */ |
|
| 231 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 232 | + |
|
| 233 | + $result = array( |
|
| 234 | + 'post' => array( |
|
| 235 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 236 | + 'title' => $referencing_post->post_title, |
|
| 237 | + 'thumbnail' => $thumbnail, |
|
| 238 | + ), |
|
| 239 | + 'entity' => array( |
|
| 240 | + 'label' => $serialized_entity['label'], |
|
| 241 | + 'mainType' => $serialized_entity['mainType'], |
|
| 242 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 243 | + ), |
|
| 244 | + ); |
|
| 245 | + |
|
| 246 | + $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 247 | + $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 248 | + |
|
| 249 | + $results[] = $result; |
|
| 250 | + |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + if ( count( $results ) < $navigator_length ) { |
|
| 254 | + $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + return $results; |
|
| 258 | 258 | |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | function _wl_network_navigator_get_results( |
| 262 | - $entities, $fields = array( |
|
| 263 | - 'ID', |
|
| 264 | - 'post_title', |
|
| 262 | + $entities, $fields = array( |
|
| 263 | + 'ID', |
|
| 264 | + 'post_title', |
|
| 265 | 265 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 266 | 266 | ) { |
| 267 | - global $wpdb; |
|
| 268 | - |
|
| 269 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 270 | - return "p.$item AS $item"; |
|
| 271 | - }, (array) $fields ) ); |
|
| 272 | - |
|
| 273 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 274 | - return "p.$item"; |
|
| 275 | - }, (array) $order_by ) ); |
|
| 276 | - |
|
| 277 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 278 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 279 | - if ( isset( $entity ) ) { |
|
| 280 | - return $entity->ID; |
|
| 281 | - } |
|
| 282 | - }, $entities ) ); |
|
| 283 | - |
|
| 284 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 285 | - return $wpdb->get_results( |
|
| 286 | - $wpdb->prepare( <<<EOF |
|
| 267 | + global $wpdb; |
|
| 268 | + |
|
| 269 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 270 | + return "p.$item AS $item"; |
|
| 271 | + }, (array) $fields ) ); |
|
| 272 | + |
|
| 273 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 274 | + return "p.$item"; |
|
| 275 | + }, (array) $order_by ) ); |
|
| 276 | + |
|
| 277 | + $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 278 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 279 | + if ( isset( $entity ) ) { |
|
| 280 | + return $entity->ID; |
|
| 281 | + } |
|
| 282 | + }, $entities ) ); |
|
| 283 | + |
|
| 284 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 285 | + return $wpdb->get_results( |
|
| 286 | + $wpdb->prepare( <<<EOF |
|
| 287 | 287 | SELECT %3\$s, p2.ID as entity_id |
| 288 | 288 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 289 | 289 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -309,8 +309,8 @@ discard block |
||
| 309 | 309 | LIMIT %1\$d |
| 310 | 310 | OFFSET %2\$d |
| 311 | 311 | EOF |
| 312 | - , $limit, $offset, $select, $order_by ) |
|
| 313 | - ); |
|
| 312 | + , $limit, $offset, $select, $order_by ) |
|
| 313 | + ); |
|
| 314 | 314 | |
| 315 | 315 | } |
| 316 | 316 | |
@@ -322,9 +322,9 @@ discard block |
||
| 322 | 322 | */ |
| 323 | 323 | function wl_shortcode_navigator_ajax() { |
| 324 | 324 | |
| 325 | - // Temporary blocking the Navigator. |
|
| 326 | - $results = wl_shortcode_navigator_data(); |
|
| 327 | - wl_core_send_json( $results ); |
|
| 325 | + // Temporary blocking the Navigator. |
|
| 326 | + $results = wl_shortcode_navigator_data(); |
|
| 327 | + wl_core_send_json( $results ); |
|
| 328 | 328 | |
| 329 | 329 | } |
| 330 | 330 | |
@@ -336,16 +336,16 @@ discard block |
||
| 336 | 336 | */ |
| 337 | 337 | function wl_shortcode_navigator_wp_json() { |
| 338 | 338 | |
| 339 | - $results = wl_shortcode_navigator_data(); |
|
| 340 | - if ( ob_get_contents() ) { |
|
| 341 | - ob_clean(); |
|
| 342 | - } |
|
| 339 | + $results = wl_shortcode_navigator_data(); |
|
| 340 | + if ( ob_get_contents() ) { |
|
| 341 | + ob_clean(); |
|
| 342 | + } |
|
| 343 | 343 | |
| 344 | - return array( |
|
| 345 | - 'items' => array( |
|
| 346 | - array( 'values' => $results ), |
|
| 347 | - ), |
|
| 348 | - ); |
|
| 344 | + return array( |
|
| 345 | + 'items' => array( |
|
| 346 | + array( 'values' => $results ), |
|
| 347 | + ), |
|
| 348 | + ); |
|
| 349 | 349 | |
| 350 | 350 | } |
| 351 | 351 | |
@@ -353,22 +353,22 @@ discard block |
||
| 353 | 353 | * Adding `rest_api_init` action for amp backend of navigator |
| 354 | 354 | */ |
| 355 | 355 | add_action( 'rest_api_init', function () { |
| 356 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 357 | - 'methods' => 'GET', |
|
| 358 | - 'permission_callback' => '__return_true', |
|
| 359 | - 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 360 | - ) ); |
|
| 356 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 357 | + 'methods' => 'GET', |
|
| 358 | + 'permission_callback' => '__return_true', |
|
| 359 | + 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 360 | + ) ); |
|
| 361 | 361 | } ); |
| 362 | 362 | |
| 363 | 363 | /** |
| 364 | 364 | * Adding `rest_api_init` action for backend of network navigator |
| 365 | 365 | */ |
| 366 | 366 | add_action( 'rest_api_init', function () { |
| 367 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 368 | - 'methods' => 'GET', |
|
| 369 | - 'callback' => 'wl_network_navigator_wp_json', |
|
| 370 | - 'permission_callback' => '__return_true', |
|
| 371 | - ) ); |
|
| 367 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 368 | + 'methods' => 'GET', |
|
| 369 | + 'callback' => 'wl_network_navigator_wp_json', |
|
| 370 | + 'permission_callback' => '__return_true', |
|
| 371 | + ) ); |
|
| 372 | 372 | } ); |
| 373 | 373 | |
| 374 | 374 | /** |
@@ -378,22 +378,22 @@ discard block |
||
| 378 | 378 | */ |
| 379 | 379 | add_action( 'plugins_loaded', function () { |
| 380 | 380 | |
| 381 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 382 | - return; |
|
| 383 | - } |
|
| 381 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 382 | + return; |
|
| 383 | + } |
|
| 384 | 384 | |
| 385 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 386 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 387 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 385 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 386 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 387 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 388 | 388 | }, 0 ); |
| 389 | 389 | |
| 390 | 390 | add_action( 'init', function () { |
| 391 | 391 | |
| 392 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 393 | - return; |
|
| 394 | - } |
|
| 392 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 393 | + return; |
|
| 394 | + } |
|
| 395 | 395 | |
| 396 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 397 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 396 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 397 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 398 | 398 | }, 0 ); |
| 399 | 399 | |
@@ -21,25 +21,25 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | // Create the cache key. |
| 23 | 23 | $cache_key_params = $_REQUEST; |
| 24 | - unset( $cache_key_params['uniqid'] ); |
|
| 25 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 24 | + unset($cache_key_params['uniqid']); |
|
| 25 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 26 | 26 | |
| 27 | 27 | // Create the TTL cache and try to get the results. |
| 28 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + $cache = new Ttl_Cache("navigator", 8 * 60 * 60); // 8 hours. |
|
| 29 | + $cache_results = $cache->get($cache_key); |
|
| 30 | 30 | |
| 31 | - if ( isset( $cache_results ) ) { |
|
| 32 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 31 | + if (isset($cache_results)) { |
|
| 32 | + header('X-WordLift-Cache: HIT'); |
|
| 33 | 33 | |
| 34 | 34 | return $cache_results; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 37 | + header('X-WordLift-Cache: MISS'); |
|
| 38 | 38 | |
| 39 | 39 | $results = _wl_navigator_get_data(); |
| 40 | 40 | |
| 41 | 41 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 42 | - $cache->put( $cache_key, $results ); |
|
| 42 | + $cache->put($cache_key, $results); |
|
| 43 | 43 | |
| 44 | 44 | return $results; |
| 45 | 45 | } |
@@ -54,29 +54,29 @@ discard block |
||
| 54 | 54 | * @since 3.22.6 |
| 55 | 55 | * |
| 56 | 56 | */ |
| 57 | -function wl_network_navigator_wp_json( $request ) { |
|
| 57 | +function wl_network_navigator_wp_json($request) { |
|
| 58 | 58 | |
| 59 | 59 | // Create the cache key. |
| 60 | 60 | $cache_key_params = $_REQUEST; |
| 61 | - unset( $cache_key_params['uniqid'] ); |
|
| 62 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 61 | + unset($cache_key_params['uniqid']); |
|
| 62 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 63 | 63 | |
| 64 | 64 | // Create the TTL cache and try to get the results. |
| 65 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 66 | - $cache_results = $cache->get( $cache_key ); |
|
| 65 | + $cache = new Ttl_Cache("network-navigator", 8 * 60 * 60); // 8 hours. |
|
| 66 | + $cache_results = $cache->get($cache_key); |
|
| 67 | 67 | |
| 68 | - if ( isset( $cache_results ) ) { |
|
| 69 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 68 | + if (isset($cache_results)) { |
|
| 69 | + header('X-WordLift-Cache: HIT'); |
|
| 70 | 70 | |
| 71 | 71 | return $cache_results; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 74 | + header('X-WordLift-Cache: MISS'); |
|
| 75 | 75 | |
| 76 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 76 | + $results = _wl_network_navigator_get_data($request); |
|
| 77 | 77 | |
| 78 | 78 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 79 | - $cache->put( $cache_key, $results ); |
|
| 79 | + $cache->put($cache_key, $results); |
|
| 80 | 80 | |
| 81 | 81 | return $results; |
| 82 | 82 | |
@@ -85,57 +85,57 @@ discard block |
||
| 85 | 85 | function _wl_navigator_get_data() { |
| 86 | 86 | |
| 87 | 87 | // Post ID must be defined |
| 88 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 89 | - wp_send_json_error( 'No post_id given' ); |
|
| 88 | + if ( ! isset($_GET['post_id'])) { |
|
| 89 | + wp_send_json_error('No post_id given'); |
|
| 90 | 90 | |
| 91 | 91 | return array(); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | // Post ID must be defined |
| 95 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 96 | - wp_send_json_error( 'No uniqid given' ); |
|
| 95 | + if ( ! isset($_GET['uniqid'])) { |
|
| 96 | + wp_send_json_error('No uniqid given'); |
|
| 97 | 97 | |
| 98 | 98 | return array(); |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Limit the results (defaults to 4) |
| 102 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 103 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 104 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 105 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 106 | - $post_types = explode( ',', $post_types ); |
|
| 102 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 103 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 104 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 105 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 106 | + $post_types = explode(',', $post_types); |
|
| 107 | 107 | $existing_post_types = get_post_types(); |
| 108 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 108 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 109 | 109 | $current_post_id = $_GET['post_id']; |
| 110 | - $current_post = get_post( $current_post_id ); |
|
| 110 | + $current_post = get_post($current_post_id); |
|
| 111 | 111 | |
| 112 | 112 | $navigator_id = $_GET['uniqid']; |
| 113 | 113 | |
| 114 | 114 | // Post ID has to match an existing item |
| 115 | - if ( null === $current_post ) { |
|
| 116 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 115 | + if (null === $current_post) { |
|
| 116 | + wp_send_json_error('No valid post_id given'); |
|
| 117 | 117 | |
| 118 | 118 | return array(); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | // Determine navigator type and call respective _get_results |
| 122 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 122 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 123 | 123 | |
| 124 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 124 | + $referencing_posts = Navigator_Data::entity_navigator_get_results($current_post_id, array( |
|
| 125 | 125 | 'ID', |
| 126 | 126 | 'post_title', |
| 127 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 127 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 128 | 128 | } else { |
| 129 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 129 | + $referencing_posts = Navigator_Data::post_navigator_get_results($current_post_id, array( |
|
| 130 | 130 | 'ID', |
| 131 | 131 | 'post_title', |
| 132 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 132 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | // loop over them and take the first one which is not already in the $related_posts |
| 136 | 136 | $results = array(); |
| 137 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 138 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 137 | + foreach ($referencing_posts as $referencing_post) { |
|
| 138 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | 141 | * Use the thumbnail. |
@@ -145,12 +145,12 @@ discard block |
||
| 145 | 145 | * |
| 146 | 146 | * @since 3.19.3 We're using the medium size image. |
| 147 | 147 | */ |
| 148 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 148 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 149 | 149 | |
| 150 | 150 | $result = array( |
| 151 | 151 | 'post' => array( |
| 152 | 152 | 'id' => $referencing_post->ID, |
| 153 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 153 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 154 | 154 | 'title' => $referencing_post->post_title, |
| 155 | 155 | 'thumbnail' => $thumbnail, |
| 156 | 156 | ), |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | 'id' => $referencing_post->entity_id, |
| 159 | 159 | 'label' => $serialized_entity['label'], |
| 160 | 160 | 'mainType' => $serialized_entity['mainType'], |
| 161 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 161 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 162 | 162 | ), |
| 163 | 163 | ); |
| 164 | 164 | |
@@ -166,59 +166,59 @@ discard block |
||
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | |
| 169 | - if ( count( $results ) < $navigator_length ) { |
|
| 170 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 169 | + if (count($results) < $navigator_length) { |
|
| 170 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Add filler posts if needed |
| 174 | - $filler_count = $navigator_length - count( $results ); |
|
| 175 | - if ( $filler_count > 0 ) { |
|
| 176 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 174 | + $filler_count = $navigator_length - count($results); |
|
| 175 | + if ($filler_count > 0) { |
|
| 176 | + $referencing_post_ids = array_map(function($p) { |
|
| 177 | 177 | return $p->ID; |
| 178 | - }, $referencing_posts ); |
|
| 178 | + }, $referencing_posts); |
|
| 179 | 179 | /** |
| 180 | 180 | * @since 3.27.8 |
| 181 | 181 | * Filler posts are fetched using this util. |
| 182 | 182 | */ |
| 183 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 184 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 185 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 186 | - $results = array_merge( $filler_posts, $results ); |
|
| 183 | + $filler_posts_util = new Filler_Posts_Util($current_post_id); |
|
| 184 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 185 | + $filler_posts = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded); |
|
| 186 | + $results = array_merge($filler_posts, $results); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | // Apply filters after fillers are added |
| 190 | - foreach ( $results as $result_index => $result ) { |
|
| 191 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 192 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 190 | + foreach ($results as $result_index => $result) { |
|
| 191 | + $results[$result_index]['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id); |
|
| 192 | + $results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | return $results; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | -function _wl_network_navigator_get_data( $request ) { |
|
| 198 | +function _wl_network_navigator_get_data($request) { |
|
| 199 | 199 | |
| 200 | 200 | // Limit the results (defaults to 4) |
| 201 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 202 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 201 | + $navigator_length = isset($request['limit']) ? intval($request['limit']) : 4; |
|
| 202 | + $navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0; |
|
| 203 | 203 | $navigator_id = $request['uniqid']; |
| 204 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 204 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 205 | 205 | |
| 206 | 206 | $entities = $request['entities']; |
| 207 | 207 | |
| 208 | 208 | // Post ID has to match an existing item |
| 209 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 210 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 209 | + if ( ! isset($entities) || empty($entities)) { |
|
| 210 | + wp_send_json_error('No valid entities provided'); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 213 | + $referencing_posts = _wl_network_navigator_get_results($entities, array( |
|
| 214 | 214 | 'ID', |
| 215 | 215 | 'post_title', |
| 216 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 216 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 217 | 217 | |
| 218 | 218 | // loop over them and take the first one which is not already in the $related_posts |
| 219 | 219 | $results = array(); |
| 220 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 221 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 220 | + foreach ($referencing_posts as $referencing_post) { |
|
| 221 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 222 | 222 | |
| 223 | 223 | /** |
| 224 | 224 | * Use the thumbnail. |
@@ -228,30 +228,30 @@ discard block |
||
| 228 | 228 | * |
| 229 | 229 | * @since 3.19.3 We're using the medium size image. |
| 230 | 230 | */ |
| 231 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 231 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 232 | 232 | |
| 233 | 233 | $result = array( |
| 234 | 234 | 'post' => array( |
| 235 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 235 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 236 | 236 | 'title' => $referencing_post->post_title, |
| 237 | 237 | 'thumbnail' => $thumbnail, |
| 238 | 238 | ), |
| 239 | 239 | 'entity' => array( |
| 240 | 240 | 'label' => $serialized_entity['label'], |
| 241 | 241 | 'mainType' => $serialized_entity['mainType'], |
| 242 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 242 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 243 | 243 | ), |
| 244 | 244 | ); |
| 245 | 245 | |
| 246 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 247 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 246 | + $result['post'] = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 247 | + $result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 248 | 248 | |
| 249 | 249 | $results[] = $result; |
| 250 | 250 | |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - if ( count( $results ) < $navigator_length ) { |
|
| 254 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 253 | + if (count($results) < $navigator_length) { |
|
| 254 | + $results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | return $results; |
@@ -266,24 +266,24 @@ discard block |
||
| 266 | 266 | ) { |
| 267 | 267 | global $wpdb; |
| 268 | 268 | |
| 269 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 269 | + $select = implode(', ', array_map(function($item) { |
|
| 270 | 270 | return "p.$item AS $item"; |
| 271 | - }, (array) $fields ) ); |
|
| 271 | + }, (array) $fields)); |
|
| 272 | 272 | |
| 273 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 273 | + $order_by = implode(', ', array_map(function($item) { |
|
| 274 | 274 | return "p.$item"; |
| 275 | - }, (array) $order_by ) ); |
|
| 275 | + }, (array) $order_by)); |
|
| 276 | 276 | |
| 277 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 278 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 279 | - if ( isset( $entity ) ) { |
|
| 277 | + $entities_in = implode(',', array_map(function($item) { |
|
| 278 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item)); |
|
| 279 | + if (isset($entity)) { |
|
| 280 | 280 | return $entity->ID; |
| 281 | 281 | } |
| 282 | - }, $entities ) ); |
|
| 282 | + }, $entities)); |
|
| 283 | 283 | |
| 284 | 284 | /** @noinspection SqlNoDataSourceInspection */ |
| 285 | 285 | return $wpdb->get_results( |
| 286 | - $wpdb->prepare( <<<EOF |
|
| 286 | + $wpdb->prepare(<<<EOF |
|
| 287 | 287 | SELECT %3\$s, p2.ID as entity_id |
| 288 | 288 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 289 | 289 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | LIMIT %1\$d |
| 310 | 310 | OFFSET %2\$d |
| 311 | 311 | EOF |
| 312 | - , $limit, $offset, $select, $order_by ) |
|
| 312 | + , $limit, $offset, $select, $order_by) |
|
| 313 | 313 | ); |
| 314 | 314 | |
| 315 | 315 | } |
@@ -324,12 +324,12 @@ discard block |
||
| 324 | 324 | |
| 325 | 325 | // Temporary blocking the Navigator. |
| 326 | 326 | $results = wl_shortcode_navigator_data(); |
| 327 | - wl_core_send_json( $results ); |
|
| 327 | + wl_core_send_json($results); |
|
| 328 | 328 | |
| 329 | 329 | } |
| 330 | 330 | |
| 331 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 332 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 331 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 332 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 333 | 333 | |
| 334 | 334 | /** |
| 335 | 335 | * wp-json call for the navigator widget |
@@ -337,13 +337,13 @@ discard block |
||
| 337 | 337 | function wl_shortcode_navigator_wp_json() { |
| 338 | 338 | |
| 339 | 339 | $results = wl_shortcode_navigator_data(); |
| 340 | - if ( ob_get_contents() ) { |
|
| 340 | + if (ob_get_contents()) { |
|
| 341 | 341 | ob_clean(); |
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | return array( |
| 345 | 345 | 'items' => array( |
| 346 | - array( 'values' => $results ), |
|
| 346 | + array('values' => $results), |
|
| 347 | 347 | ), |
| 348 | 348 | ); |
| 349 | 349 | |
@@ -352,23 +352,23 @@ discard block |
||
| 352 | 352 | /** |
| 353 | 353 | * Adding `rest_api_init` action for amp backend of navigator |
| 354 | 354 | */ |
| 355 | -add_action( 'rest_api_init', function () { |
|
| 356 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 355 | +add_action('rest_api_init', function() { |
|
| 356 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 357 | 357 | 'methods' => 'GET', |
| 358 | 358 | 'permission_callback' => '__return_true', |
| 359 | 359 | 'callback' => 'wl_shortcode_navigator_wp_json' |
| 360 | - ) ); |
|
| 360 | + )); |
|
| 361 | 361 | } ); |
| 362 | 362 | |
| 363 | 363 | /** |
| 364 | 364 | * Adding `rest_api_init` action for backend of network navigator |
| 365 | 365 | */ |
| 366 | -add_action( 'rest_api_init', function () { |
|
| 367 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 366 | +add_action('rest_api_init', function() { |
|
| 367 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 368 | 368 | 'methods' => 'GET', |
| 369 | 369 | 'callback' => 'wl_network_navigator_wp_json', |
| 370 | 370 | 'permission_callback' => '__return_true', |
| 371 | - ) ); |
|
| 371 | + )); |
|
| 372 | 372 | } ); |
| 373 | 373 | |
| 374 | 374 | /** |
@@ -376,24 +376,24 @@ discard block |
||
| 376 | 376 | * |
| 377 | 377 | * @since 2.2.0 |
| 378 | 378 | */ |
| 379 | -add_action( 'plugins_loaded', function () { |
|
| 379 | +add_action('plugins_loaded', function() { |
|
| 380 | 380 | |
| 381 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 381 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action']) { |
|
| 382 | 382 | return; |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 386 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 387 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 388 | -}, 0 ); |
|
| 385 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 386 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 387 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 388 | +}, 0); |
|
| 389 | 389 | |
| 390 | -add_action( 'init', function () { |
|
| 390 | +add_action('init', function() { |
|
| 391 | 391 | |
| 392 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action'] ) { |
|
| 392 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $_REQUEST['action']) { |
|
| 393 | 393 | return; |
| 394 | 394 | } |
| 395 | 395 | |
| 396 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 397 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 398 | -}, 0 ); |
|
| 396 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 397 | + remove_action('init', 'gglcptch_init'); |
|
| 398 | +}, 0); |
|
| 399 | 399 | |
@@ -19,30 +19,30 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | function wl_shortcode_faceted_search( $request ) { |
| 21 | 21 | |
| 22 | - // Create the cache key. |
|
| 23 | - $cache_key = array( |
|
| 24 | - 'request_params' => $_GET, |
|
| 25 | - ); |
|
| 22 | + // Create the cache key. |
|
| 23 | + $cache_key = array( |
|
| 24 | + 'request_params' => $_GET, |
|
| 25 | + ); |
|
| 26 | 26 | |
| 27 | - // Create the TTL cache and try to get the results. |
|
| 28 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | - $cache_results = $cache->get( $cache_key ); |
|
| 27 | + // Create the TTL cache and try to get the results. |
|
| 28 | + $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | + $cache_results = $cache->get( $cache_key ); |
|
| 30 | 30 | |
| 31 | - if ( isset( $cache_results ) ) { |
|
| 32 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 33 | - wl_core_send_json( $cache_results ); |
|
| 31 | + if ( isset( $cache_results ) ) { |
|
| 32 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 33 | + wl_core_send_json( $cache_results ); |
|
| 34 | 34 | |
| 35 | - return; |
|
| 36 | - } |
|
| 35 | + return; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 38 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 39 | 39 | |
| 40 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 40 | + $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 41 | 41 | |
| 42 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 43 | - $cache->put( $cache_key, $results ); |
|
| 42 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 43 | + $cache->put( $cache_key, $results ); |
|
| 44 | 44 | |
| 45 | - wl_core_send_json( $results ); |
|
| 45 | + wl_core_send_json( $results ); |
|
| 46 | 46 | |
| 47 | 47 | } |
| 48 | 48 | |
@@ -53,150 +53,150 @@ discard block |
||
| 53 | 53 | * @since 3.26.0 |
| 54 | 54 | */ |
| 55 | 55 | function wl_shortcode_faceted_search_origin( $request ) { |
| 56 | - // Post ID must be defined. |
|
| 57 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 58 | - wp_die( 'No post_id given' ); |
|
| 59 | - |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 64 | - $current_post = get_post( $current_post_id ); |
|
| 65 | - $faceted_id = $_GET['uniqid']; |
|
| 66 | - |
|
| 67 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 68 | - $post_types = explode( ',', $post_types ); |
|
| 69 | - $existing_post_types = get_post_types(); |
|
| 70 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - // Post ID has to match an existing item. |
|
| 74 | - if ( null === $current_post ) { |
|
| 75 | - wp_die( 'No valid post_id given' ); |
|
| 76 | - |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // If the current post is an entity, |
|
| 81 | - // the current post is used as main entity. |
|
| 82 | - // Otherwise, current post related entities are used. |
|
| 83 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 84 | - |
|
| 85 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 86 | - array( $current_post->ID ) : |
|
| 87 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 88 | - |
|
| 89 | - // If there are no entities we cannot render the widget. |
|
| 90 | - if ( 0 === count( $entity_ids ) ) { |
|
| 91 | - /** |
|
| 92 | - * If this function is not called from ajax |
|
| 93 | - * then this should not throw an error. |
|
| 94 | - * Note: Used in scripbox longtail project on json endpoint. |
|
| 95 | - */ |
|
| 96 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 97 | - wp_die( 'No entities available' ); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 103 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 108 | - * The ordering should be descending by date on default. |
|
| 109 | - */ |
|
| 110 | - $order_by = 'DESC'; |
|
| 111 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 112 | - $order_by = (string) $_GET['sort']; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 117 | - $entity_ids, |
|
| 118 | - '*', |
|
| 119 | - null, |
|
| 120 | - 'publish', |
|
| 121 | - array( $current_post_id ), |
|
| 122 | - $limit, |
|
| 123 | - null, |
|
| 124 | - $order_by, |
|
| 125 | - $post_types |
|
| 126 | - ); |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 130 | - return $p->ID; |
|
| 131 | - }, $referencing_posts ); |
|
| 132 | - |
|
| 133 | - $post_results = array(); |
|
| 134 | - $entity_results = array(); |
|
| 135 | - |
|
| 136 | - // Populate $post_results |
|
| 137 | - |
|
| 138 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 139 | - $referencing_posts : |
|
| 140 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 141 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 142 | - '*', |
|
| 143 | - null, |
|
| 144 | - null, |
|
| 145 | - array(), |
|
| 146 | - null, |
|
| 147 | - $referencing_post_ids |
|
| 148 | - ); |
|
| 149 | - |
|
| 150 | - if ( $filtered_posts ) { |
|
| 151 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Use the thumbnail. |
|
| 155 | - * |
|
| 156 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 157 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 158 | - * |
|
| 159 | - * @since 3.19.3 We're using the medium size image. |
|
| 160 | - */ |
|
| 161 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 162 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 163 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 164 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 165 | - |
|
| 166 | - $result = $post_obj; |
|
| 167 | - $post_results[] = $result; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // Add filler posts if needed |
|
| 172 | - |
|
| 173 | - $filler_count = $limit - count( $post_results ); |
|
| 174 | - if ( $filler_count > 0 ) { |
|
| 175 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 176 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 177 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 178 | - |
|
| 179 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 180 | - } |
|
| 181 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 182 | - return $post->ID; |
|
| 183 | - }, $post_results ); |
|
| 184 | - |
|
| 185 | - // Populate $entity_results |
|
| 186 | - |
|
| 187 | - global $wpdb; |
|
| 188 | - |
|
| 189 | - // Retrieve Wordlift relation instances table name. |
|
| 190 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 191 | - |
|
| 192 | - /* |
|
| 56 | + // Post ID must be defined. |
|
| 57 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 58 | + wp_die( 'No post_id given' ); |
|
| 59 | + |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 64 | + $current_post = get_post( $current_post_id ); |
|
| 65 | + $faceted_id = $_GET['uniqid']; |
|
| 66 | + |
|
| 67 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 68 | + $post_types = explode( ',', $post_types ); |
|
| 69 | + $existing_post_types = get_post_types(); |
|
| 70 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + // Post ID has to match an existing item. |
|
| 74 | + if ( null === $current_post ) { |
|
| 75 | + wp_die( 'No valid post_id given' ); |
|
| 76 | + |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // If the current post is an entity, |
|
| 81 | + // the current post is used as main entity. |
|
| 82 | + // Otherwise, current post related entities are used. |
|
| 83 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 84 | + |
|
| 85 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 86 | + array( $current_post->ID ) : |
|
| 87 | + $entity_service->get_related_entities( $current_post->ID ); |
|
| 88 | + |
|
| 89 | + // If there are no entities we cannot render the widget. |
|
| 90 | + if ( 0 === count( $entity_ids ) ) { |
|
| 91 | + /** |
|
| 92 | + * If this function is not called from ajax |
|
| 93 | + * then this should not throw an error. |
|
| 94 | + * Note: Used in scripbox longtail project on json endpoint. |
|
| 95 | + */ |
|
| 96 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 97 | + wp_die( 'No entities available' ); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 103 | + $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 108 | + * The ordering should be descending by date on default. |
|
| 109 | + */ |
|
| 110 | + $order_by = 'DESC'; |
|
| 111 | + if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 112 | + $order_by = (string) $_GET['sort']; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 117 | + $entity_ids, |
|
| 118 | + '*', |
|
| 119 | + null, |
|
| 120 | + 'publish', |
|
| 121 | + array( $current_post_id ), |
|
| 122 | + $limit, |
|
| 123 | + null, |
|
| 124 | + $order_by, |
|
| 125 | + $post_types |
|
| 126 | + ); |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 130 | + return $p->ID; |
|
| 131 | + }, $referencing_posts ); |
|
| 132 | + |
|
| 133 | + $post_results = array(); |
|
| 134 | + $entity_results = array(); |
|
| 135 | + |
|
| 136 | + // Populate $post_results |
|
| 137 | + |
|
| 138 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 139 | + $referencing_posts : |
|
| 140 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 141 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 142 | + '*', |
|
| 143 | + null, |
|
| 144 | + null, |
|
| 145 | + array(), |
|
| 146 | + null, |
|
| 147 | + $referencing_post_ids |
|
| 148 | + ); |
|
| 149 | + |
|
| 150 | + if ( $filtered_posts ) { |
|
| 151 | + foreach ( $filtered_posts as $post_obj ) { |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Use the thumbnail. |
|
| 155 | + * |
|
| 156 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 157 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 158 | + * |
|
| 159 | + * @since 3.19.3 We're using the medium size image. |
|
| 160 | + */ |
|
| 161 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 162 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 163 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 164 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 165 | + |
|
| 166 | + $result = $post_obj; |
|
| 167 | + $post_results[] = $result; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // Add filler posts if needed |
|
| 172 | + |
|
| 173 | + $filler_count = $limit - count( $post_results ); |
|
| 174 | + if ( $filler_count > 0 ) { |
|
| 175 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 176 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 177 | + $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 178 | + |
|
| 179 | + $post_results = array_merge( $post_results, $filler_posts ); |
|
| 180 | + } |
|
| 181 | + $referencing_post_ids = array_map( function ( $post ) { |
|
| 182 | + return $post->ID; |
|
| 183 | + }, $post_results ); |
|
| 184 | + |
|
| 185 | + // Populate $entity_results |
|
| 186 | + |
|
| 187 | + global $wpdb; |
|
| 188 | + |
|
| 189 | + // Retrieve Wordlift relation instances table name. |
|
| 190 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 191 | + |
|
| 192 | + /* |
|
| 193 | 193 | * Make sure we have some referenced post, otherwise the IN parts of |
| 194 | 194 | * the SQL will produce an SQL error. |
| 195 | 195 | */ |
| 196 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 197 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 196 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 197 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 198 | 198 | |
| 199 | - $query = " |
|
| 199 | + $query = " |
|
| 200 | 200 | SELECT |
| 201 | 201 | object_id AS ID, |
| 202 | 202 | count( object_id ) AS counter |
@@ -208,60 +208,60 @@ discard block |
||
| 208 | 208 | LIMIT $limit; |
| 209 | 209 | "; |
| 210 | 210 | |
| 211 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 211 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 212 | 212 | |
| 213 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 213 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 214 | 214 | |
| 215 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 215 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 216 | 216 | |
| 217 | - foreach ( $entities as $obj ) { |
|
| 217 | + foreach ( $entities as $obj ) { |
|
| 218 | 218 | |
| 219 | - $entity = get_post( $obj->ID ); |
|
| 219 | + $entity = get_post( $obj->ID ); |
|
| 220 | 220 | |
| 221 | - // Ensure only valid and published entities are returned. |
|
| 222 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 221 | + // Ensure only valid and published entities are returned. |
|
| 222 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 223 | 223 | |
| 224 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 225 | - $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 226 | - $serialized_entity['counter'] = $obj->counter; |
|
| 227 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 228 | - $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 229 | - $obj->ID, |
|
| 230 | - 'ids', |
|
| 231 | - null, |
|
| 232 | - null, |
|
| 233 | - array(), |
|
| 234 | - null, |
|
| 235 | - $referencing_post_ids |
|
| 236 | - ); |
|
| 237 | - $entity_results[] = $serialized_entity; |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - } |
|
| 224 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 225 | + $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 226 | + $serialized_entity['counter'] = $obj->counter; |
|
| 227 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 228 | + $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 229 | + $obj->ID, |
|
| 230 | + 'ids', |
|
| 231 | + null, |
|
| 232 | + null, |
|
| 233 | + array(), |
|
| 234 | + null, |
|
| 235 | + $referencing_post_ids |
|
| 236 | + ); |
|
| 237 | + $entity_results[] = $serialized_entity; |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 243 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 242 | + $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 243 | + $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 244 | 244 | |
| 245 | - return array( |
|
| 246 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 247 | - 'entities' => $entity_results |
|
| 248 | - ); |
|
| 245 | + return array( |
|
| 246 | + 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 247 | + 'entities' => $entity_results |
|
| 248 | + ); |
|
| 249 | 249 | |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
| 253 | 253 | |
| 254 | - $title = get_the_title( $post_id ); |
|
| 254 | + $title = get_the_title( $post_id ); |
|
| 255 | 255 | |
| 256 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 257 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 256 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 257 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 258 | 258 | |
| 259 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 260 | - $title = $alternative_labels[0]; |
|
| 261 | - } |
|
| 262 | - } |
|
| 259 | + if ( count( $alternative_labels ) > 0 ) { |
|
| 260 | + $title = $alternative_labels[0]; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - return remove_accents( $title ); |
|
| 264 | + return remove_accents( $title ); |
|
| 265 | 265 | |
| 266 | 266 | } |
| 267 | 267 | |
@@ -269,9 +269,9 @@ discard block |
||
| 269 | 269 | * Adding `rest_api_init` action for network faceted-search |
| 270 | 270 | */ |
| 271 | 271 | add_action( 'rest_api_init', function () { |
| 272 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 273 | - 'methods' => 'GET', |
|
| 274 | - 'callback' => 'wl_shortcode_faceted_search', |
|
| 275 | - 'permission_callback' => '__return_true', |
|
| 276 | - ) ); |
|
| 272 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 273 | + 'methods' => 'GET', |
|
| 274 | + 'callback' => 'wl_shortcode_faceted_search', |
|
| 275 | + 'permission_callback' => '__return_true', |
|
| 276 | + ) ); |
|
| 277 | 277 | } ); |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | * @since 3.21.4 fix the cache key by also using the request body. |
| 18 | 18 | * @since 3.21.2 add a caching layer. |
| 19 | 19 | */ |
| 20 | -function wl_shortcode_faceted_search( $request ) { |
|
| 20 | +function wl_shortcode_faceted_search($request) { |
|
| 21 | 21 | |
| 22 | 22 | // Create the cache key. |
| 23 | 23 | $cache_key = array( |
@@ -25,24 +25,24 @@ discard block |
||
| 25 | 25 | ); |
| 26 | 26 | |
| 27 | 27 | // Create the TTL cache and try to get the results. |
| 28 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 29 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + $cache = new Ttl_Cache("faceted-search", 8 * 60 * 60); // 8 hours. |
|
| 29 | + $cache_results = $cache->get($cache_key); |
|
| 30 | 30 | |
| 31 | - if ( isset( $cache_results ) ) { |
|
| 32 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 33 | - wl_core_send_json( $cache_results ); |
|
| 31 | + if (isset($cache_results)) { |
|
| 32 | + header('X-WordLift-Cache: HIT'); |
|
| 33 | + wl_core_send_json($cache_results); |
|
| 34 | 34 | |
| 35 | 35 | return; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 38 | + header('X-WordLift-Cache: MISS'); |
|
| 39 | 39 | |
| 40 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 40 | + $results = wl_shortcode_faceted_search_origin($request); |
|
| 41 | 41 | |
| 42 | 42 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 43 | - $cache->put( $cache_key, $results ); |
|
| 43 | + $cache->put($cache_key, $results); |
|
| 44 | 44 | |
| 45 | - wl_core_send_json( $results ); |
|
| 45 | + wl_core_send_json($results); |
|
| 46 | 46 | |
| 47 | 47 | } |
| 48 | 48 | |
@@ -52,27 +52,27 @@ discard block |
||
| 52 | 52 | * @return array $results |
| 53 | 53 | * @since 3.26.0 |
| 54 | 54 | */ |
| 55 | -function wl_shortcode_faceted_search_origin( $request ) { |
|
| 55 | +function wl_shortcode_faceted_search_origin($request) { |
|
| 56 | 56 | // Post ID must be defined. |
| 57 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 58 | - wp_die( 'No post_id given' ); |
|
| 57 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 58 | + wp_die('No post_id given'); |
|
| 59 | 59 | |
| 60 | 60 | return; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 64 | - $current_post = get_post( $current_post_id ); |
|
| 64 | + $current_post = get_post($current_post_id); |
|
| 65 | 65 | $faceted_id = $_GET['uniqid']; |
| 66 | 66 | |
| 67 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 68 | - $post_types = explode( ',', $post_types ); |
|
| 67 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 68 | + $post_types = explode(',', $post_types); |
|
| 69 | 69 | $existing_post_types = get_post_types(); |
| 70 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 70 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 71 | 71 | |
| 72 | 72 | |
| 73 | 73 | // Post ID has to match an existing item. |
| 74 | - if ( null === $current_post ) { |
|
| 75 | - wp_die( 'No valid post_id given' ); |
|
| 74 | + if (null === $current_post) { |
|
| 75 | + wp_die('No valid post_id given'); |
|
| 76 | 76 | |
| 77 | 77 | return; |
| 78 | 78 | } |
@@ -82,25 +82,24 @@ discard block |
||
| 82 | 82 | // Otherwise, current post related entities are used. |
| 83 | 83 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 84 | 84 | |
| 85 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 86 | - array( $current_post->ID ) : |
|
| 87 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 85 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 86 | + array($current_post->ID) : $entity_service->get_related_entities($current_post->ID); |
|
| 88 | 87 | |
| 89 | 88 | // If there are no entities we cannot render the widget. |
| 90 | - if ( 0 === count( $entity_ids ) ) { |
|
| 89 | + if (0 === count($entity_ids)) { |
|
| 91 | 90 | /** |
| 92 | 91 | * If this function is not called from ajax |
| 93 | 92 | * then this should not throw an error. |
| 94 | 93 | * Note: Used in scripbox longtail project on json endpoint. |
| 95 | 94 | */ |
| 96 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 97 | - wp_die( 'No entities available' ); |
|
| 95 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 96 | + wp_die('No entities available'); |
|
| 98 | 97 | } |
| 99 | 98 | |
| 100 | 99 | } |
| 101 | 100 | |
| 102 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 103 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 101 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 102 | + $amp = (isset($_GET['amp'])) ? true : false; |
|
| 104 | 103 | |
| 105 | 104 | |
| 106 | 105 | /** |
@@ -108,7 +107,7 @@ discard block |
||
| 108 | 107 | * The ordering should be descending by date on default. |
| 109 | 108 | */ |
| 110 | 109 | $order_by = 'DESC'; |
| 111 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 110 | + if (isset($_GET['sort']) && is_string($_GET['sort'])) { |
|
| 112 | 111 | $order_by = (string) $_GET['sort']; |
| 113 | 112 | } |
| 114 | 113 | |
@@ -118,7 +117,7 @@ discard block |
||
| 118 | 117 | '*', |
| 119 | 118 | null, |
| 120 | 119 | 'publish', |
| 121 | - array( $current_post_id ), |
|
| 120 | + array($current_post_id), |
|
| 122 | 121 | $limit, |
| 123 | 122 | null, |
| 124 | 123 | $order_by, |
@@ -126,19 +125,18 @@ discard block |
||
| 126 | 125 | ); |
| 127 | 126 | |
| 128 | 127 | |
| 129 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 128 | + $referencing_post_ids = array_map(function($p) { |
|
| 130 | 129 | return $p->ID; |
| 131 | - }, $referencing_posts ); |
|
| 130 | + }, $referencing_posts); |
|
| 132 | 131 | |
| 133 | 132 | $post_results = array(); |
| 134 | 133 | $entity_results = array(); |
| 135 | 134 | |
| 136 | 135 | // Populate $post_results |
| 137 | 136 | |
| 138 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 139 | - $referencing_posts : |
|
| 140 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 141 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 137 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 138 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 139 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 142 | 140 | '*', |
| 143 | 141 | null, |
| 144 | 142 | null, |
@@ -147,8 +145,8 @@ discard block |
||
| 147 | 145 | $referencing_post_ids |
| 148 | 146 | ); |
| 149 | 147 | |
| 150 | - if ( $filtered_posts ) { |
|
| 151 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 148 | + if ($filtered_posts) { |
|
| 149 | + foreach ($filtered_posts as $post_obj) { |
|
| 152 | 150 | |
| 153 | 151 | /** |
| 154 | 152 | * Use the thumbnail. |
@@ -158,10 +156,10 @@ discard block |
||
| 158 | 156 | * |
| 159 | 157 | * @since 3.19.3 We're using the medium size image. |
| 160 | 158 | */ |
| 161 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 162 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 159 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 160 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 163 | 161 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 164 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 162 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
| 165 | 163 | |
| 166 | 164 | $result = $post_obj; |
| 167 | 165 | $post_results[] = $result; |
@@ -170,17 +168,17 @@ discard block |
||
| 170 | 168 | |
| 171 | 169 | // Add filler posts if needed |
| 172 | 170 | |
| 173 | - $filler_count = $limit - count( $post_results ); |
|
| 174 | - if ( $filler_count > 0 ) { |
|
| 175 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 176 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 177 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 171 | + $filler_count = $limit - count($post_results); |
|
| 172 | + if ($filler_count > 0) { |
|
| 173 | + $filler_posts_util = new Filler_Posts_Util($current_post_id); |
|
| 174 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 175 | + $filler_posts = $filler_posts_util->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 178 | 176 | |
| 179 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 177 | + $post_results = array_merge($post_results, $filler_posts); |
|
| 180 | 178 | } |
| 181 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 179 | + $referencing_post_ids = array_map(function($post) { |
|
| 182 | 180 | return $post->ID; |
| 183 | - }, $post_results ); |
|
| 181 | + }, $post_results); |
|
| 184 | 182 | |
| 185 | 183 | // Populate $entity_results |
| 186 | 184 | |
@@ -193,8 +191,8 @@ discard block |
||
| 193 | 191 | * Make sure we have some referenced post, otherwise the IN parts of |
| 194 | 192 | * the SQL will produce an SQL error. |
| 195 | 193 | */ |
| 196 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 197 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 194 | + if ( ! empty($referencing_post_ids)) { |
|
| 195 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 198 | 196 | |
| 199 | 197 | $query = " |
| 200 | 198 | SELECT |
@@ -208,21 +206,21 @@ discard block |
||
| 208 | 206 | LIMIT $limit; |
| 209 | 207 | "; |
| 210 | 208 | |
| 211 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 209 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 212 | 210 | |
| 213 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 211 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 214 | 212 | |
| 215 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 213 | + wl_write_log('Entities found '.count($entities)); |
|
| 216 | 214 | |
| 217 | - foreach ( $entities as $obj ) { |
|
| 215 | + foreach ($entities as $obj) { |
|
| 218 | 216 | |
| 219 | - $entity = get_post( $obj->ID ); |
|
| 217 | + $entity = get_post($obj->ID); |
|
| 220 | 218 | |
| 221 | 219 | // Ensure only valid and published entities are returned. |
| 222 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 220 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 223 | 221 | |
| 224 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 225 | - $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title( $obj->ID ); |
|
| 222 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 223 | + $serialized_entity['label'] = wl_shortcode_faceted_search_get_the_title($obj->ID); |
|
| 226 | 224 | $serialized_entity['counter'] = $obj->counter; |
| 227 | 225 | $serialized_entity['createdAt'] = $entity->post_date; |
| 228 | 226 | $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -234,44 +232,44 @@ discard block |
||
| 234 | 232 | null, |
| 235 | 233 | $referencing_post_ids |
| 236 | 234 | ); |
| 237 | - $entity_results[] = $serialized_entity; |
|
| 235 | + $entity_results[] = $serialized_entity; |
|
| 238 | 236 | } |
| 239 | 237 | } |
| 240 | 238 | } |
| 241 | 239 | |
| 242 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 243 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 240 | + $post_results = apply_filters('wl_faceted_data_posts', $post_results, $faceted_id); |
|
| 241 | + $entity_results = apply_filters('wl_faceted_data_entities', $entity_results, $faceted_id); |
|
| 244 | 242 | |
| 245 | 243 | return array( |
| 246 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 244 | + 'posts' => $amp ? array(array('values' => $post_results)) : $post_results, |
|
| 247 | 245 | 'entities' => $entity_results |
| 248 | 246 | ); |
| 249 | 247 | |
| 250 | 248 | } |
| 251 | 249 | |
| 252 | -function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
|
| 250 | +function wl_shortcode_faceted_search_get_the_title($post_id) { |
|
| 253 | 251 | |
| 254 | - $title = get_the_title( $post_id ); |
|
| 252 | + $title = get_the_title($post_id); |
|
| 255 | 253 | |
| 256 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 257 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 254 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
| 255 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
| 258 | 256 | |
| 259 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 257 | + if (count($alternative_labels) > 0) { |
|
| 260 | 258 | $title = $alternative_labels[0]; |
| 261 | 259 | } |
| 262 | 260 | } |
| 263 | 261 | |
| 264 | - return remove_accents( $title ); |
|
| 262 | + return remove_accents($title); |
|
| 265 | 263 | |
| 266 | 264 | } |
| 267 | 265 | |
| 268 | 266 | /** |
| 269 | 267 | * Adding `rest_api_init` action for network faceted-search |
| 270 | 268 | */ |
| 271 | -add_action( 'rest_api_init', function () { |
|
| 272 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 269 | +add_action('rest_api_init', function() { |
|
| 270 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 273 | 271 | 'methods' => 'GET', |
| 274 | 272 | 'callback' => 'wl_shortcode_faceted_search', |
| 275 | 273 | 'permission_callback' => '__return_true', |
| 276 | - ) ); |
|
| 274 | + )); |
|
| 277 | 275 | } ); |
@@ -14,11 +14,11 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Same_Category_Filler_Posts extends Filler_Posts { |
| 16 | 16 | |
| 17 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 17 | + function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 18 | 18 | |
| 19 | - $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 19 | + $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 20 | 20 | |
| 21 | - return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => get_post_types() ) |
|
| 22 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 23 | - } |
|
| 21 | + return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => get_post_types() ) |
|
| 22 | + + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 23 | + } |
|
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -14,11 +14,11 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Same_Category_Filler_Posts extends Filler_Posts { |
| 16 | 16 | |
| 17 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 17 | + function get_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 18 | 18 | |
| 19 | - $current_post_categories = wp_get_post_categories( $this->post_id ); |
|
| 19 | + $current_post_categories = wp_get_post_categories($this->post_id); |
|
| 20 | 20 | |
| 21 | - return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => get_post_types() ) |
|
| 22 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 21 | + return get_posts(array('category__in' => $current_post_categories, 'post_type' => get_post_types()) |
|
| 22 | + + $this->get_posts_config($filler_count, $post_ids_to_be_excluded)); |
|
| 23 | 23 | } |
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -7,17 +7,17 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Same_Post_Type_Filler_Posts extends Filler_Posts { |
| 9 | 9 | |
| 10 | - public function __construct( $post_id ) { |
|
| 11 | - parent::__construct( $post_id ); |
|
| 12 | - } |
|
| 10 | + public function __construct( $post_id ) { |
|
| 11 | + parent::__construct( $post_id ); |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 15 | - $post_type = get_post_type( $this->post_id ); |
|
| 16 | - if ( $post_type === 'entity' ) { |
|
| 17 | - $post_type = 'post'; |
|
| 18 | - } |
|
| 14 | + function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 15 | + $post_type = get_post_type( $this->post_id ); |
|
| 16 | + if ( $post_type === 'entity' ) { |
|
| 17 | + $post_type = 'post'; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - return get_posts( array( 'post_type' => $post_type ) |
|
| 21 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 22 | - } |
|
| 20 | + return get_posts( array( 'post_type' => $post_type ) |
|
| 21 | + + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 22 | + } |
|
| 23 | 23 | } |
| 24 | 24 | \ No newline at end of file |
@@ -7,17 +7,17 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Same_Post_Type_Filler_Posts extends Filler_Posts { |
| 9 | 9 | |
| 10 | - public function __construct( $post_id ) { |
|
| 11 | - parent::__construct( $post_id ); |
|
| 10 | + public function __construct($post_id) { |
|
| 11 | + parent::__construct($post_id); |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | - function get_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 15 | - $post_type = get_post_type( $this->post_id ); |
|
| 16 | - if ( $post_type === 'entity' ) { |
|
| 14 | + function get_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 15 | + $post_type = get_post_type($this->post_id); |
|
| 16 | + if ($post_type === 'entity') { |
|
| 17 | 17 | $post_type = 'post'; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - return get_posts( array( 'post_type' => $post_type ) |
|
| 21 | - + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) ); |
|
| 20 | + return get_posts(array('post_type' => $post_type) |
|
| 21 | + + $this->get_posts_config($filler_count, $post_ids_to_be_excluded)); |
|
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | \ No newline at end of file |
@@ -7,54 +7,54 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | abstract class Filler_Posts { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var int |
|
| 12 | - */ |
|
| 13 | - public $filler_count; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @var array<int> |
|
| 17 | - */ |
|
| 18 | - public $post_ids_to_be_excluded; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var $post_id int |
|
| 22 | - */ |
|
| 23 | - protected $post_id; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Filler_Posts constructor. |
|
| 27 | - * |
|
| 28 | - * @param $post_id |
|
| 29 | - */ |
|
| 30 | - public function __construct( $post_id ) { |
|
| 31 | - |
|
| 32 | - $this->post_id = $post_id; |
|
| 33 | - |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - protected function get_posts_config($filler_count, $post_ids_to_be_excluded) { |
|
| 37 | - |
|
| 38 | - return array( |
|
| 39 | - 'meta_query' => array( |
|
| 40 | - array( |
|
| 41 | - 'key' => '_thumbnail_id' |
|
| 42 | - ) |
|
| 43 | - ), |
|
| 44 | - 'numberposts' => $filler_count, |
|
| 45 | - 'post__not_in' => $post_ids_to_be_excluded, |
|
| 46 | - 'ignore_sticky_posts' => 1 |
|
| 47 | - ); |
|
| 48 | - |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param $filler_count |
|
| 53 | - * @param $post_ids_to_be_excluded |
|
| 54 | - * |
|
| 55 | - * @return array<\WP_Post> |
|
| 56 | - */ |
|
| 57 | - abstract function get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 10 | + /** |
|
| 11 | + * @var int |
|
| 12 | + */ |
|
| 13 | + public $filler_count; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @var array<int> |
|
| 17 | + */ |
|
| 18 | + public $post_ids_to_be_excluded; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var $post_id int |
|
| 22 | + */ |
|
| 23 | + protected $post_id; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Filler_Posts constructor. |
|
| 27 | + * |
|
| 28 | + * @param $post_id |
|
| 29 | + */ |
|
| 30 | + public function __construct( $post_id ) { |
|
| 31 | + |
|
| 32 | + $this->post_id = $post_id; |
|
| 33 | + |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + protected function get_posts_config($filler_count, $post_ids_to_be_excluded) { |
|
| 37 | + |
|
| 38 | + return array( |
|
| 39 | + 'meta_query' => array( |
|
| 40 | + array( |
|
| 41 | + 'key' => '_thumbnail_id' |
|
| 42 | + ) |
|
| 43 | + ), |
|
| 44 | + 'numberposts' => $filler_count, |
|
| 45 | + 'post__not_in' => $post_ids_to_be_excluded, |
|
| 46 | + 'ignore_sticky_posts' => 1 |
|
| 47 | + ); |
|
| 48 | + |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param $filler_count |
|
| 53 | + * @param $post_ids_to_be_excluded |
|
| 54 | + * |
|
| 55 | + * @return array<\WP_Post> |
|
| 56 | + */ |
|
| 57 | + abstract function get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 58 | 58 | |
| 59 | 59 | |
| 60 | 60 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @param $post_id |
| 29 | 29 | */ |
| 30 | - public function __construct( $post_id ) { |
|
| 30 | + public function __construct($post_id) { |
|
| 31 | 31 | |
| 32 | 32 | $this->post_id = $post_id; |
| 33 | 33 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | * |
| 55 | 55 | * @return array<\WP_Post> |
| 56 | 56 | */ |
| 57 | - abstract function get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 57 | + abstract function get_posts($filler_count, $post_ids_to_be_excluded); |
|
| 58 | 58 | |
| 59 | 59 | |
| 60 | 60 | } |
@@ -7,100 +7,100 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Filler_Posts_Util { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var array<Filler_Posts> |
|
| 12 | - */ |
|
| 13 | - private $sources = array(); |
|
| 14 | - |
|
| 15 | - public function __construct( $post_id ) { |
|
| 16 | - |
|
| 17 | - $post_type = get_post_type( $post_id ); |
|
| 18 | - |
|
| 19 | - if ( $post_type === 'post' ) { |
|
| 20 | - $this->sources = array( |
|
| 21 | - new Same_Category_Filler_Posts( $post_id ), |
|
| 22 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 23 | - ); |
|
| 24 | - } else { |
|
| 25 | - $this->sources = array( |
|
| 26 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 27 | - ); |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @param $posts array<\WP_Post> |
|
| 34 | - * |
|
| 35 | - * @return array<int> |
|
| 36 | - */ |
|
| 37 | - private function extract_post_ids( $posts ) { |
|
| 38 | - return array_map( function ( $post ) { |
|
| 39 | - /** |
|
| 40 | - * @var $post \WP_Post |
|
| 41 | - */ |
|
| 42 | - return $post->ID; |
|
| 43 | - }, $posts ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 47 | - |
|
| 48 | - $filler_posts = array(); |
|
| 49 | - |
|
| 50 | - foreach ( $this->sources as $source ) { |
|
| 51 | - |
|
| 52 | - if ( $filler_count <= 0 ) { |
|
| 53 | - break; |
|
| 54 | - } |
|
| 55 | - /** |
|
| 56 | - * @var Filler_Posts $source |
|
| 57 | - */ |
|
| 58 | - $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
|
| 59 | - $source->filler_count = $filler_count; |
|
| 60 | - |
|
| 61 | - $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 62 | - $post_ids = $this->extract_post_ids( $posts ); |
|
| 63 | - |
|
| 64 | - // Update the post ids, filler posts and filler count |
|
| 65 | - $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 66 | - $filler_count = $filler_count - count( $posts ); |
|
| 67 | - $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - return $filler_posts; |
|
| 73 | - |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Called by wordlift navigator, converts all the posts to response format. |
|
| 78 | - * @param $filler_count |
|
| 79 | - * @param $post_ids_to_be_excluded |
|
| 80 | - * |
|
| 81 | - * @return array |
|
| 82 | - */ |
|
| 83 | - public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 84 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 85 | - // Add thumbnail and permalink to filler posts |
|
| 86 | - $filler_response = array(); |
|
| 87 | - foreach ( $filler_posts as $post_obj ) { |
|
| 88 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 89 | - $filler_response[] = array( |
|
| 90 | - 'post' => array( |
|
| 91 | - 'id' => $post_obj->ID, |
|
| 92 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 93 | - 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 94 | - 'title' => get_the_title( $post_obj->ID ) |
|
| 95 | - ), |
|
| 96 | - 'entity' => array( |
|
| 97 | - 'id' => 0 |
|
| 98 | - ) |
|
| 99 | - ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - return $filler_response; |
|
| 103 | - } |
|
| 10 | + /** |
|
| 11 | + * @var array<Filler_Posts> |
|
| 12 | + */ |
|
| 13 | + private $sources = array(); |
|
| 14 | + |
|
| 15 | + public function __construct( $post_id ) { |
|
| 16 | + |
|
| 17 | + $post_type = get_post_type( $post_id ); |
|
| 18 | + |
|
| 19 | + if ( $post_type === 'post' ) { |
|
| 20 | + $this->sources = array( |
|
| 21 | + new Same_Category_Filler_Posts( $post_id ), |
|
| 22 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 23 | + ); |
|
| 24 | + } else { |
|
| 25 | + $this->sources = array( |
|
| 26 | + new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 27 | + ); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @param $posts array<\WP_Post> |
|
| 34 | + * |
|
| 35 | + * @return array<int> |
|
| 36 | + */ |
|
| 37 | + private function extract_post_ids( $posts ) { |
|
| 38 | + return array_map( function ( $post ) { |
|
| 39 | + /** |
|
| 40 | + * @var $post \WP_Post |
|
| 41 | + */ |
|
| 42 | + return $post->ID; |
|
| 43 | + }, $posts ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 47 | + |
|
| 48 | + $filler_posts = array(); |
|
| 49 | + |
|
| 50 | + foreach ( $this->sources as $source ) { |
|
| 51 | + |
|
| 52 | + if ( $filler_count <= 0 ) { |
|
| 53 | + break; |
|
| 54 | + } |
|
| 55 | + /** |
|
| 56 | + * @var Filler_Posts $source |
|
| 57 | + */ |
|
| 58 | + $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
|
| 59 | + $source->filler_count = $filler_count; |
|
| 60 | + |
|
| 61 | + $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 62 | + $post_ids = $this->extract_post_ids( $posts ); |
|
| 63 | + |
|
| 64 | + // Update the post ids, filler posts and filler count |
|
| 65 | + $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 66 | + $filler_count = $filler_count - count( $posts ); |
|
| 67 | + $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + return $filler_posts; |
|
| 73 | + |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Called by wordlift navigator, converts all the posts to response format. |
|
| 78 | + * @param $filler_count |
|
| 79 | + * @param $post_ids_to_be_excluded |
|
| 80 | + * |
|
| 81 | + * @return array |
|
| 82 | + */ |
|
| 83 | + public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 84 | + $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 85 | + // Add thumbnail and permalink to filler posts |
|
| 86 | + $filler_response = array(); |
|
| 87 | + foreach ( $filler_posts as $post_obj ) { |
|
| 88 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 89 | + $filler_response[] = array( |
|
| 90 | + 'post' => array( |
|
| 91 | + 'id' => $post_obj->ID, |
|
| 92 | + 'permalink' => get_permalink( $post_obj->ID ), |
|
| 93 | + 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 94 | + 'title' => get_the_title( $post_obj->ID ) |
|
| 95 | + ), |
|
| 96 | + 'entity' => array( |
|
| 97 | + 'id' => 0 |
|
| 98 | + ) |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + return $filler_response; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | |
| 106 | 106 | } |
@@ -12,18 +12,18 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | private $sources = array(); |
| 14 | 14 | |
| 15 | - public function __construct( $post_id ) { |
|
| 15 | + public function __construct($post_id) { |
|
| 16 | 16 | |
| 17 | - $post_type = get_post_type( $post_id ); |
|
| 17 | + $post_type = get_post_type($post_id); |
|
| 18 | 18 | |
| 19 | - if ( $post_type === 'post' ) { |
|
| 19 | + if ($post_type === 'post') { |
|
| 20 | 20 | $this->sources = array( |
| 21 | - new Same_Category_Filler_Posts( $post_id ), |
|
| 22 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 21 | + new Same_Category_Filler_Posts($post_id), |
|
| 22 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 23 | 23 | ); |
| 24 | 24 | } else { |
| 25 | 25 | $this->sources = array( |
| 26 | - new Same_Post_Type_Filler_Posts( $post_id ), |
|
| 26 | + new Same_Post_Type_Filler_Posts($post_id), |
|
| 27 | 27 | ); |
| 28 | 28 | } |
| 29 | 29 | } |
@@ -34,22 +34,22 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @return array<int> |
| 36 | 36 | */ |
| 37 | - private function extract_post_ids( $posts ) { |
|
| 38 | - return array_map( function ( $post ) { |
|
| 37 | + private function extract_post_ids($posts) { |
|
| 38 | + return array_map(function($post) { |
|
| 39 | 39 | /** |
| 40 | 40 | * @var $post \WP_Post |
| 41 | 41 | */ |
| 42 | 42 | return $post->ID; |
| 43 | - }, $posts ); |
|
| 43 | + }, $posts); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - public function get_filler_posts( $filler_count, $post_ids_to_be_excluded ) { |
|
| 46 | + public function get_filler_posts($filler_count, $post_ids_to_be_excluded) { |
|
| 47 | 47 | |
| 48 | 48 | $filler_posts = array(); |
| 49 | 49 | |
| 50 | - foreach ( $this->sources as $source ) { |
|
| 50 | + foreach ($this->sources as $source) { |
|
| 51 | 51 | |
| 52 | - if ( $filler_count <= 0 ) { |
|
| 52 | + if ($filler_count <= 0) { |
|
| 53 | 53 | break; |
| 54 | 54 | } |
| 55 | 55 | /** |
@@ -58,13 +58,13 @@ discard block |
||
| 58 | 58 | $source->post_ids_to_be_excluded = $post_ids_to_be_excluded; |
| 59 | 59 | $source->filler_count = $filler_count; |
| 60 | 60 | |
| 61 | - $posts = $source->get_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 62 | - $post_ids = $this->extract_post_ids( $posts ); |
|
| 61 | + $posts = $source->get_posts($filler_count, $post_ids_to_be_excluded); |
|
| 62 | + $post_ids = $this->extract_post_ids($posts); |
|
| 63 | 63 | |
| 64 | 64 | // Update the post ids, filler posts and filler count |
| 65 | - $post_ids_to_be_excluded = array_merge( $post_ids_to_be_excluded, $post_ids ); |
|
| 66 | - $filler_count = $filler_count - count( $posts ); |
|
| 67 | - $filler_posts = array_merge( $filler_posts, $posts ); |
|
| 65 | + $post_ids_to_be_excluded = array_merge($post_ids_to_be_excluded, $post_ids); |
|
| 66 | + $filler_count = $filler_count - count($posts); |
|
| 67 | + $filler_posts = array_merge($filler_posts, $posts); |
|
| 68 | 68 | |
| 69 | 69 | |
| 70 | 70 | } |
@@ -80,18 +80,18 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @return array |
| 82 | 82 | */ |
| 83 | - public function get_filler_response( $filler_count, $post_ids_to_be_excluded ) { |
|
| 84 | - $filler_posts = $this->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 83 | + public function get_filler_response($filler_count, $post_ids_to_be_excluded) { |
|
| 84 | + $filler_posts = $this->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 85 | 85 | // Add thumbnail and permalink to filler posts |
| 86 | 86 | $filler_response = array(); |
| 87 | - foreach ( $filler_posts as $post_obj ) { |
|
| 88 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 87 | + foreach ($filler_posts as $post_obj) { |
|
| 88 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 89 | 89 | $filler_response[] = array( |
| 90 | 90 | 'post' => array( |
| 91 | 91 | 'id' => $post_obj->ID, |
| 92 | - 'permalink' => get_permalink( $post_obj->ID ), |
|
| 93 | - 'thumbnail' => ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 94 | - 'title' => get_the_title( $post_obj->ID ) |
|
| 92 | + 'permalink' => get_permalink($post_obj->ID), |
|
| 93 | + 'thumbnail' => ($thumbnail) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH, |
|
| 94 | + 'title' => get_the_title($post_obj->ID) |
|
| 95 | 95 | ), |
| 96 | 96 | 'entity' => array( |
| 97 | 97 | 'id' => 0 |
@@ -7,39 +7,39 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Navigator_Data { |
| 9 | 9 | |
| 10 | - public static function get_post_types_as_string( $post_types ) { |
|
| 11 | - if ( $post_types === array() ) { |
|
| 12 | - $post_types = get_post_types(); |
|
| 13 | - } |
|
| 14 | - $post_types = array_map( function ( $post_type ) { |
|
| 15 | - return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | - }, $post_types ); |
|
| 17 | - |
|
| 18 | - return implode( ',', $post_types ); |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - public static function post_navigator_get_results( |
|
| 22 | - $post_id, $fields = array( |
|
| 23 | - 'ID', |
|
| 24 | - 'post_title', |
|
| 25 | - ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 26 | - ) { |
|
| 27 | - |
|
| 28 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 29 | - global $wpdb; |
|
| 30 | - |
|
| 31 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 32 | - return "p.$item AS $item"; |
|
| 33 | - }, (array) $fields ) ); |
|
| 34 | - |
|
| 35 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 36 | - return "p.$item"; |
|
| 37 | - }, (array) $order_by ) ); |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 41 | - return $wpdb->get_results( |
|
| 42 | - $wpdb->prepare( <<<EOF |
|
| 10 | + public static function get_post_types_as_string( $post_types ) { |
|
| 11 | + if ( $post_types === array() ) { |
|
| 12 | + $post_types = get_post_types(); |
|
| 13 | + } |
|
| 14 | + $post_types = array_map( function ( $post_type ) { |
|
| 15 | + return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | + }, $post_types ); |
|
| 17 | + |
|
| 18 | + return implode( ',', $post_types ); |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + public static function post_navigator_get_results( |
|
| 22 | + $post_id, $fields = array( |
|
| 23 | + 'ID', |
|
| 24 | + 'post_title', |
|
| 25 | + ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 26 | + ) { |
|
| 27 | + |
|
| 28 | + $post_types = self::get_post_types_as_string( $post_types ); |
|
| 29 | + global $wpdb; |
|
| 30 | + |
|
| 31 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 32 | + return "p.$item AS $item"; |
|
| 33 | + }, (array) $fields ) ); |
|
| 34 | + |
|
| 35 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 36 | + return "p.$item"; |
|
| 37 | + }, (array) $order_by ) ); |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 41 | + return $wpdb->get_results( |
|
| 42 | + $wpdb->prepare( <<<EOF |
|
| 43 | 43 | SELECT %4\$s, p2.ID as entity_id |
| 44 | 44 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 45 | 45 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -73,33 +73,33 @@ discard block |
||
| 73 | 73 | LIMIT %2\$d |
| 74 | 74 | OFFSET %3\$d |
| 75 | 75 | EOF |
| 76 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 77 | - ); |
|
| 76 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 77 | + ); |
|
| 78 | 78 | |
| 79 | - } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - public static function entity_navigator_get_results( |
|
| 83 | - $post_id, $fields = array( |
|
| 84 | - 'ID', |
|
| 85 | - 'post_title', |
|
| 86 | - ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 87 | - ) { |
|
| 88 | - global $wpdb; |
|
| 82 | + public static function entity_navigator_get_results( |
|
| 83 | + $post_id, $fields = array( |
|
| 84 | + 'ID', |
|
| 85 | + 'post_title', |
|
| 86 | + ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 87 | + ) { |
|
| 88 | + global $wpdb; |
|
| 89 | 89 | |
| 90 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 91 | - return "p.$item AS $item"; |
|
| 92 | - }, (array) $fields ) ); |
|
| 90 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 91 | + return "p.$item AS $item"; |
|
| 92 | + }, (array) $fields ) ); |
|
| 93 | 93 | |
| 94 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 95 | - return "p.$item"; |
|
| 96 | - }, (array) $order_by ) ); |
|
| 94 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 95 | + return "p.$item"; |
|
| 96 | + }, (array) $order_by ) ); |
|
| 97 | 97 | |
| 98 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 98 | + $post_types = self::get_post_types_as_string( $post_types ); |
|
| 99 | 99 | |
| 100 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 101 | - return $wpdb->get_results( |
|
| 102 | - $wpdb->prepare( <<<EOF |
|
| 100 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 101 | + return $wpdb->get_results( |
|
| 102 | + $wpdb->prepare( <<<EOF |
|
| 103 | 103 | SELECT %4\$s, p2.ID as entity_id |
| 104 | 104 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 105 | 105 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -130,9 +130,9 @@ discard block |
||
| 130 | 130 | LIMIT %2\$d |
| 131 | 131 | OFFSET %3\$d |
| 132 | 132 | EOF |
| 133 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 134 | - ); |
|
| 135 | - } |
|
| 133 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 134 | + ); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | 137 | |
| 138 | 138 | } |
@@ -7,15 +7,15 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Navigator_Data { |
| 9 | 9 | |
| 10 | - public static function get_post_types_as_string( $post_types ) { |
|
| 11 | - if ( $post_types === array() ) { |
|
| 10 | + public static function get_post_types_as_string($post_types) { |
|
| 11 | + if ($post_types === array()) { |
|
| 12 | 12 | $post_types = get_post_types(); |
| 13 | 13 | } |
| 14 | - $post_types = array_map( function ( $post_type ) { |
|
| 15 | - return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | - }, $post_types ); |
|
| 14 | + $post_types = array_map(function($post_type) { |
|
| 15 | + return "'".esc_sql($post_type)."'"; |
|
| 16 | + }, $post_types); |
|
| 17 | 17 | |
| 18 | - return implode( ',', $post_types ); |
|
| 18 | + return implode(',', $post_types); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public static function post_navigator_get_results( |
@@ -25,21 +25,21 @@ discard block |
||
| 25 | 25 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
| 26 | 26 | ) { |
| 27 | 27 | |
| 28 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 28 | + $post_types = self::get_post_types_as_string($post_types); |
|
| 29 | 29 | global $wpdb; |
| 30 | 30 | |
| 31 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 31 | + $select = implode(', ', array_map(function($item) { |
|
| 32 | 32 | return "p.$item AS $item"; |
| 33 | - }, (array) $fields ) ); |
|
| 33 | + }, (array) $fields)); |
|
| 34 | 34 | |
| 35 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 35 | + $order_by = implode(', ', array_map(function($item) { |
|
| 36 | 36 | return "p.$item"; |
| 37 | - }, (array) $order_by ) ); |
|
| 37 | + }, (array) $order_by)); |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | 40 | /** @noinspection SqlNoDataSourceInspection */ |
| 41 | 41 | return $wpdb->get_results( |
| 42 | - $wpdb->prepare( <<<EOF |
|
| 42 | + $wpdb->prepare(<<<EOF |
|
| 43 | 43 | SELECT %4\$s, p2.ID as entity_id |
| 44 | 44 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 45 | 45 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | LIMIT %2\$d |
| 74 | 74 | OFFSET %3\$d |
| 75 | 75 | EOF |
| 76 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 76 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 77 | 77 | ); |
| 78 | 78 | |
| 79 | 79 | } |
@@ -87,19 +87,19 @@ discard block |
||
| 87 | 87 | ) { |
| 88 | 88 | global $wpdb; |
| 89 | 89 | |
| 90 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 90 | + $select = implode(', ', array_map(function($item) { |
|
| 91 | 91 | return "p.$item AS $item"; |
| 92 | - }, (array) $fields ) ); |
|
| 92 | + }, (array) $fields)); |
|
| 93 | 93 | |
| 94 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 94 | + $order_by = implode(', ', array_map(function($item) { |
|
| 95 | 95 | return "p.$item"; |
| 96 | - }, (array) $order_by ) ); |
|
| 96 | + }, (array) $order_by)); |
|
| 97 | 97 | |
| 98 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 98 | + $post_types = self::get_post_types_as_string($post_types); |
|
| 99 | 99 | |
| 100 | 100 | /** @noinspection SqlNoDataSourceInspection */ |
| 101 | 101 | return $wpdb->get_results( |
| 102 | - $wpdb->prepare( <<<EOF |
|
| 102 | + $wpdb->prepare(<<<EOF |
|
| 103 | 103 | SELECT %4\$s, p2.ID as entity_id |
| 104 | 104 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 105 | 105 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | LIMIT %2\$d |
| 131 | 131 | OFFSET %3\$d |
| 132 | 132 | EOF |
| 133 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 133 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 134 | 134 | ); |
| 135 | 135 | } |
| 136 | 136 | |
@@ -18,405 +18,405 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Relation_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The singleton instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Relation_Service $instance The singleton instance. |
|
| 27 | - */ |
|
| 28 | - private static $instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The relation table name in MySQL, set during instantiation. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var string $relation_table The relation table name. |
|
| 36 | - */ |
|
| 37 | - private $relation_table; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * A {@link Wordlift_Log_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.3 |
|
| 43 | - * |
|
| 44 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private static $log; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Create a {@link Wordlift_Relation_Service} instance. |
|
| 50 | - * |
|
| 51 | - * @since 3.15.0 |
|
| 52 | - */ |
|
| 53 | - public function __construct() { |
|
| 54 | - |
|
| 55 | - self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | - |
|
| 57 | - global $wpdb; |
|
| 58 | - |
|
| 59 | - // The relations table. |
|
| 60 | - $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 61 | - |
|
| 62 | - self::$instance = $this; |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the singleton instance. |
|
| 68 | - * |
|
| 69 | - * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
|
| 70 | - * singleton instance. |
|
| 71 | - * @since 3.15.0 |
|
| 72 | - * @access public |
|
| 73 | - */ |
|
| 74 | - public static function get_instance() { |
|
| 75 | - |
|
| 76 | - return self::$instance; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 81 | - * |
|
| 82 | - * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 83 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 84 | - * '*' to return all fields, by default '*'. |
|
| 85 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 86 | - * @param null|string $status The status, by default all. |
|
| 87 | - * @param array $excludes An array of ids to exclude from the results. |
|
| 88 | - * @param null|int $limit The maximum number of results, by default |
|
| 89 | - * no limit. |
|
| 90 | - * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 91 | - * |
|
| 92 | - * @param null | string $order_by |
|
| 93 | - * |
|
| 94 | - * @param array $post_types |
|
| 95 | - * |
|
| 96 | - * @return array|object|null Database query results |
|
| 97 | - * @since 3.15.0 |
|
| 98 | - */ |
|
| 99 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array() ) { |
|
| 100 | - global $wpdb; |
|
| 101 | - |
|
| 102 | - // The output fields. |
|
| 103 | - $actual_fields = self::fields( $fields ); |
|
| 104 | - |
|
| 105 | - self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 106 | - |
|
| 107 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 108 | - |
|
| 109 | - // If there are no related objects, return an empty array. |
|
| 110 | - if ( empty( $objects ) ) { |
|
| 111 | - self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 112 | - |
|
| 113 | - return array(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 117 | - |
|
| 118 | - $sql = |
|
| 119 | - " |
|
| 21 | + /** |
|
| 22 | + * The singleton instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Relation_Service $instance The singleton instance. |
|
| 27 | + */ |
|
| 28 | + private static $instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The relation table name in MySQL, set during instantiation. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var string $relation_table The relation table name. |
|
| 36 | + */ |
|
| 37 | + private $relation_table; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * A {@link Wordlift_Log_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.3 |
|
| 43 | + * |
|
| 44 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private static $log; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Create a {@link Wordlift_Relation_Service} instance. |
|
| 50 | + * |
|
| 51 | + * @since 3.15.0 |
|
| 52 | + */ |
|
| 53 | + public function __construct() { |
|
| 54 | + |
|
| 55 | + self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | + |
|
| 57 | + global $wpdb; |
|
| 58 | + |
|
| 59 | + // The relations table. |
|
| 60 | + $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 61 | + |
|
| 62 | + self::$instance = $this; |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the singleton instance. |
|
| 68 | + * |
|
| 69 | + * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
|
| 70 | + * singleton instance. |
|
| 71 | + * @since 3.15.0 |
|
| 72 | + * @access public |
|
| 73 | + */ |
|
| 74 | + public static function get_instance() { |
|
| 75 | + |
|
| 76 | + return self::$instance; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 81 | + * |
|
| 82 | + * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 83 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 84 | + * '*' to return all fields, by default '*'. |
|
| 85 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 86 | + * @param null|string $status The status, by default all. |
|
| 87 | + * @param array $excludes An array of ids to exclude from the results. |
|
| 88 | + * @param null|int $limit The maximum number of results, by default |
|
| 89 | + * no limit. |
|
| 90 | + * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 91 | + * |
|
| 92 | + * @param null | string $order_by |
|
| 93 | + * |
|
| 94 | + * @param array $post_types |
|
| 95 | + * |
|
| 96 | + * @return array|object|null Database query results |
|
| 97 | + * @since 3.15.0 |
|
| 98 | + */ |
|
| 99 | + public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array() ) { |
|
| 100 | + global $wpdb; |
|
| 101 | + |
|
| 102 | + // The output fields. |
|
| 103 | + $actual_fields = self::fields( $fields ); |
|
| 104 | + |
|
| 105 | + self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 106 | + |
|
| 107 | + $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 108 | + |
|
| 109 | + // If there are no related objects, return an empty array. |
|
| 110 | + if ( empty( $objects ) ) { |
|
| 111 | + self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 112 | + |
|
| 113 | + return array(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 117 | + |
|
| 118 | + $sql = |
|
| 119 | + " |
|
| 120 | 120 | SELECT DISTINCT p.$actual_fields |
| 121 | 121 | FROM {$this->relation_table} r |
| 122 | 122 | INNER JOIN $wpdb->posts p |
| 123 | 123 | ON p.id = r.subject_id |
| 124 | 124 | " |
| 125 | - // Add the status clause. |
|
| 126 | - . self::and_status( $status ) |
|
| 127 | - . self::inner_join_is_article() |
|
| 128 | - . self::where_object_id( $objects ) |
|
| 129 | - // Since `object_id` can be an article ID we need to exclude it from |
|
| 130 | - // the results. |
|
| 131 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 132 | - . self::and_article_in( $include ) |
|
| 133 | - . self::and_post_type_in( $post_types ) |
|
| 134 | - . self::and_predicate( $predicate ) |
|
| 135 | - . self::order_by( $order_by ) |
|
| 136 | - . self::limit( $limit ); |
|
| 137 | - |
|
| 138 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * The `post_type IN` clause. |
|
| 143 | - * |
|
| 144 | - * @param array $post_types If the post type is not provided then the valid |
|
| 145 | - * entity post types are used. |
|
| 146 | - * |
|
| 147 | - * @return string The `post_type IN` clause. |
|
| 148 | - * @since 3.15.3 |
|
| 149 | - */ |
|
| 150 | - private static function and_post_type_in( $post_types = array() ) { |
|
| 151 | - |
|
| 152 | - if ( $post_types === array() ) { |
|
| 153 | - $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return " AND p.post_type IN ( '" |
|
| 157 | - . implode( |
|
| 158 | - "','", |
|
| 159 | - array_map( 'esc_sql', $post_types ) |
|
| 160 | - ) |
|
| 161 | - . "' )"; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Add the limit clause if specified. |
|
| 166 | - * |
|
| 167 | - * @param null|int $limit The maximum number of results. |
|
| 168 | - * |
|
| 169 | - * @return string The limit clause (empty if no limit has been specified). |
|
| 170 | - * @since 3.15.0 |
|
| 171 | - * |
|
| 172 | - */ |
|
| 173 | - private static function limit( $limit = null ) { |
|
| 174 | - |
|
| 175 | - if ( null === $limit ) { |
|
| 176 | - return ''; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return "LIMIT $limit"; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @param $order_by string | null |
|
| 184 | - * |
|
| 185 | - * @return string |
|
| 186 | - */ |
|
| 187 | - private static function order_by( $order_by ) { |
|
| 188 | - if ( ! $order_by ) { |
|
| 189 | - return ''; |
|
| 190 | - } |
|
| 191 | - $order_by = (string) $order_by; |
|
| 192 | - $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 193 | - |
|
| 194 | - if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 195 | - return " ORDER BY p.post_modified ${order_by} "; |
|
| 196 | - } else { |
|
| 197 | - return ' ORDER BY p.post_modified DESC '; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 203 | - * or get the entities if it's a post). |
|
| 204 | - * |
|
| 205 | - * @param int|array $object_id An array of posts/entities' ids. |
|
| 206 | - * |
|
| 207 | - * @return array An array of entities' ids. |
|
| 208 | - * @since 3.15.0 |
|
| 209 | - * |
|
| 210 | - */ |
|
| 211 | - private function article_id_to_entity_id( $object_id ) { |
|
| 212 | - |
|
| 213 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 214 | - |
|
| 215 | - $relation_service = $this; |
|
| 216 | - |
|
| 217 | - return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 218 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 219 | - return array_merge( $carry, (array) $item ); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 223 | - }, array() ); |
|
| 224 | - |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Add the WHERE clause. |
|
| 229 | - * |
|
| 230 | - * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 231 | - * |
|
| 232 | - * @return string The WHERE clause. |
|
| 233 | - * @since 3.15.0 |
|
| 234 | - * |
|
| 235 | - */ |
|
| 236 | - private static function where_object_id( $object_id ) { |
|
| 237 | - |
|
| 238 | - if ( empty( $object_id ) ) { |
|
| 239 | - // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 240 | - |
|
| 241 | - return ' WHERE 1 = 1'; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Add the exclude clause. |
|
| 249 | - * |
|
| 250 | - * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 251 | - * |
|
| 252 | - * @return string The exclude clause. |
|
| 253 | - * @since 3.15.0 |
|
| 254 | - * |
|
| 255 | - */ |
|
| 256 | - private static function and_article_not_in( $exclude ) { |
|
| 257 | - |
|
| 258 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Add the include clause. |
|
| 263 | - * |
|
| 264 | - * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 265 | - * |
|
| 266 | - * @return string An empty string if $include is null otherwise the include |
|
| 267 | - * clause. |
|
| 268 | - * @since 3.15.0 |
|
| 269 | - * |
|
| 270 | - */ |
|
| 271 | - private static function and_article_in( $include = null ) { |
|
| 272 | - |
|
| 273 | - if ( null === $include ) { |
|
| 274 | - return ''; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 282 | - * |
|
| 283 | - * @param int $object_id The object {@link WP_Post}'s id. |
|
| 284 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 285 | - * '*' to return all fields, by default '*'. |
|
| 286 | - * @param null|string $status The status, by default all. |
|
| 287 | - * |
|
| 288 | - * @return array|object|null Database query results |
|
| 289 | - * @since 3.15.0 |
|
| 290 | - * |
|
| 291 | - */ |
|
| 292 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 293 | - global $wpdb; |
|
| 294 | - |
|
| 295 | - // The output fields. |
|
| 296 | - $actual_fields = self::fields( $fields ); |
|
| 297 | - |
|
| 298 | - $sql = $wpdb->prepare( |
|
| 299 | - " |
|
| 125 | + // Add the status clause. |
|
| 126 | + . self::and_status( $status ) |
|
| 127 | + . self::inner_join_is_article() |
|
| 128 | + . self::where_object_id( $objects ) |
|
| 129 | + // Since `object_id` can be an article ID we need to exclude it from |
|
| 130 | + // the results. |
|
| 131 | + . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 132 | + . self::and_article_in( $include ) |
|
| 133 | + . self::and_post_type_in( $post_types ) |
|
| 134 | + . self::and_predicate( $predicate ) |
|
| 135 | + . self::order_by( $order_by ) |
|
| 136 | + . self::limit( $limit ); |
|
| 137 | + |
|
| 138 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * The `post_type IN` clause. |
|
| 143 | + * |
|
| 144 | + * @param array $post_types If the post type is not provided then the valid |
|
| 145 | + * entity post types are used. |
|
| 146 | + * |
|
| 147 | + * @return string The `post_type IN` clause. |
|
| 148 | + * @since 3.15.3 |
|
| 149 | + */ |
|
| 150 | + private static function and_post_type_in( $post_types = array() ) { |
|
| 151 | + |
|
| 152 | + if ( $post_types === array() ) { |
|
| 153 | + $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return " AND p.post_type IN ( '" |
|
| 157 | + . implode( |
|
| 158 | + "','", |
|
| 159 | + array_map( 'esc_sql', $post_types ) |
|
| 160 | + ) |
|
| 161 | + . "' )"; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Add the limit clause if specified. |
|
| 166 | + * |
|
| 167 | + * @param null|int $limit The maximum number of results. |
|
| 168 | + * |
|
| 169 | + * @return string The limit clause (empty if no limit has been specified). |
|
| 170 | + * @since 3.15.0 |
|
| 171 | + * |
|
| 172 | + */ |
|
| 173 | + private static function limit( $limit = null ) { |
|
| 174 | + |
|
| 175 | + if ( null === $limit ) { |
|
| 176 | + return ''; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return "LIMIT $limit"; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @param $order_by string | null |
|
| 184 | + * |
|
| 185 | + * @return string |
|
| 186 | + */ |
|
| 187 | + private static function order_by( $order_by ) { |
|
| 188 | + if ( ! $order_by ) { |
|
| 189 | + return ''; |
|
| 190 | + } |
|
| 191 | + $order_by = (string) $order_by; |
|
| 192 | + $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 193 | + |
|
| 194 | + if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 195 | + return " ORDER BY p.post_modified ${order_by} "; |
|
| 196 | + } else { |
|
| 197 | + return ' ORDER BY p.post_modified DESC '; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 203 | + * or get the entities if it's a post). |
|
| 204 | + * |
|
| 205 | + * @param int|array $object_id An array of posts/entities' ids. |
|
| 206 | + * |
|
| 207 | + * @return array An array of entities' ids. |
|
| 208 | + * @since 3.15.0 |
|
| 209 | + * |
|
| 210 | + */ |
|
| 211 | + private function article_id_to_entity_id( $object_id ) { |
|
| 212 | + |
|
| 213 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 214 | + |
|
| 215 | + $relation_service = $this; |
|
| 216 | + |
|
| 217 | + return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 218 | + if ( $entity_service->is_entity( $item ) ) { |
|
| 219 | + return array_merge( $carry, (array) $item ); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 223 | + }, array() ); |
|
| 224 | + |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Add the WHERE clause. |
|
| 229 | + * |
|
| 230 | + * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 231 | + * |
|
| 232 | + * @return string The WHERE clause. |
|
| 233 | + * @since 3.15.0 |
|
| 234 | + * |
|
| 235 | + */ |
|
| 236 | + private static function where_object_id( $object_id ) { |
|
| 237 | + |
|
| 238 | + if ( empty( $object_id ) ) { |
|
| 239 | + // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 240 | + |
|
| 241 | + return ' WHERE 1 = 1'; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Add the exclude clause. |
|
| 249 | + * |
|
| 250 | + * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 251 | + * |
|
| 252 | + * @return string The exclude clause. |
|
| 253 | + * @since 3.15.0 |
|
| 254 | + * |
|
| 255 | + */ |
|
| 256 | + private static function and_article_not_in( $exclude ) { |
|
| 257 | + |
|
| 258 | + return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Add the include clause. |
|
| 263 | + * |
|
| 264 | + * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 265 | + * |
|
| 266 | + * @return string An empty string if $include is null otherwise the include |
|
| 267 | + * clause. |
|
| 268 | + * @since 3.15.0 |
|
| 269 | + * |
|
| 270 | + */ |
|
| 271 | + private static function and_article_in( $include = null ) { |
|
| 272 | + |
|
| 273 | + if ( null === $include ) { |
|
| 274 | + return ''; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 282 | + * |
|
| 283 | + * @param int $object_id The object {@link WP_Post}'s id. |
|
| 284 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 285 | + * '*' to return all fields, by default '*'. |
|
| 286 | + * @param null|string $status The status, by default all. |
|
| 287 | + * |
|
| 288 | + * @return array|object|null Database query results |
|
| 289 | + * @since 3.15.0 |
|
| 290 | + * |
|
| 291 | + */ |
|
| 292 | + public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 293 | + global $wpdb; |
|
| 294 | + |
|
| 295 | + // The output fields. |
|
| 296 | + $actual_fields = self::fields( $fields ); |
|
| 297 | + |
|
| 298 | + $sql = $wpdb->prepare( |
|
| 299 | + " |
|
| 300 | 300 | SELECT p.$actual_fields |
| 301 | 301 | FROM {$this->relation_table} r |
| 302 | 302 | INNER JOIN $wpdb->posts p |
| 303 | 303 | ON p.id = r.subject_id |
| 304 | 304 | " |
| 305 | - // Add the status clause. |
|
| 306 | - . self::and_status( $status ) |
|
| 307 | - . self::inner_join_is_not_article() |
|
| 308 | - . " WHERE r.object_id = %d " |
|
| 309 | - . self::and_post_type_in() |
|
| 310 | - , |
|
| 311 | - $object_id |
|
| 312 | - ); |
|
| 313 | - |
|
| 314 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Get the entities referenced by the specified {@link WP_Post}. |
|
| 319 | - * |
|
| 320 | - * @param int $subject_id The {@link WP_Post}'s id. |
|
| 321 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 322 | - * '*' to return all fields, by default '*'. |
|
| 323 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 324 | - * @param null|string $status The status, by default all. |
|
| 325 | - * |
|
| 326 | - * @return array|object|null Database query results |
|
| 327 | - * @since 3.15.0 |
|
| 328 | - * |
|
| 329 | - */ |
|
| 330 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 331 | - global $wpdb; |
|
| 332 | - |
|
| 333 | - // The output fields. |
|
| 334 | - $actual_fields = self::fields( $fields ); |
|
| 335 | - |
|
| 336 | - $sql = $wpdb->prepare( |
|
| 337 | - " |
|
| 305 | + // Add the status clause. |
|
| 306 | + . self::and_status( $status ) |
|
| 307 | + . self::inner_join_is_not_article() |
|
| 308 | + . " WHERE r.object_id = %d " |
|
| 309 | + . self::and_post_type_in() |
|
| 310 | + , |
|
| 311 | + $object_id |
|
| 312 | + ); |
|
| 313 | + |
|
| 314 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Get the entities referenced by the specified {@link WP_Post}. |
|
| 319 | + * |
|
| 320 | + * @param int $subject_id The {@link WP_Post}'s id. |
|
| 321 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 322 | + * '*' to return all fields, by default '*'. |
|
| 323 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 324 | + * @param null|string $status The status, by default all. |
|
| 325 | + * |
|
| 326 | + * @return array|object|null Database query results |
|
| 327 | + * @since 3.15.0 |
|
| 328 | + * |
|
| 329 | + */ |
|
| 330 | + public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 331 | + global $wpdb; |
|
| 332 | + |
|
| 333 | + // The output fields. |
|
| 334 | + $actual_fields = self::fields( $fields ); |
|
| 335 | + |
|
| 336 | + $sql = $wpdb->prepare( |
|
| 337 | + " |
|
| 338 | 338 | SELECT p.$actual_fields |
| 339 | 339 | FROM {$this->relation_table} r |
| 340 | 340 | INNER JOIN $wpdb->posts p |
| 341 | 341 | ON p.id = r.object_id |
| 342 | 342 | " |
| 343 | - // Add the status clause. |
|
| 344 | - . self::and_status( $status ) |
|
| 345 | - . self::inner_join_is_not_article() |
|
| 346 | - . " WHERE r.subject_id = %d " |
|
| 347 | - . self::and_post_type_in() |
|
| 348 | - . self::and_predicate( $predicate ) |
|
| 349 | - , |
|
| 350 | - $subject_id |
|
| 351 | - ); |
|
| 352 | - |
|
| 353 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Add the `post_status` clause. |
|
| 358 | - * |
|
| 359 | - * @param null|string|array $status The status values. |
|
| 360 | - * |
|
| 361 | - * @return string An empty string if $status is null, otherwise the status clause. |
|
| 362 | - * @since 3.15.0 |
|
| 363 | - * |
|
| 364 | - */ |
|
| 365 | - private static function and_status( $status = null ) { |
|
| 366 | - |
|
| 367 | - if ( null === $status ) { |
|
| 368 | - return ''; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Add the `predicate` clause. |
|
| 376 | - * |
|
| 377 | - * @param null|string|array $predicate An array of predicates. |
|
| 378 | - * |
|
| 379 | - * @return string An empty string if $predicate is null otherwise the predicate |
|
| 380 | - * clause. |
|
| 381 | - * @since 3.15.0 |
|
| 382 | - * |
|
| 383 | - */ |
|
| 384 | - private static function and_predicate( $predicate = null ) { |
|
| 385 | - |
|
| 386 | - if ( null === $predicate ) { |
|
| 387 | - return ''; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * The select fields. |
|
| 395 | - * |
|
| 396 | - * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 397 | - * |
|
| 398 | - * @return string The `id` field if `ids` otherwise `*`. |
|
| 399 | - * @since 3.15.0 |
|
| 400 | - * |
|
| 401 | - */ |
|
| 402 | - private static function fields( $fields = '*' ) { |
|
| 403 | - |
|
| 404 | - // The output fields. |
|
| 405 | - return 'ids' === $fields ? 'id' : '*'; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * The inner join clause for articles. |
|
| 410 | - * |
|
| 411 | - * @return string The articles inner join clause. |
|
| 412 | - * @since 3.15.0 |
|
| 413 | - * |
|
| 414 | - */ |
|
| 415 | - private static function inner_join_is_article() { |
|
| 416 | - global $wpdb; |
|
| 417 | - |
|
| 418 | - return $wpdb->prepare( |
|
| 419 | - " |
|
| 343 | + // Add the status clause. |
|
| 344 | + . self::and_status( $status ) |
|
| 345 | + . self::inner_join_is_not_article() |
|
| 346 | + . " WHERE r.subject_id = %d " |
|
| 347 | + . self::and_post_type_in() |
|
| 348 | + . self::and_predicate( $predicate ) |
|
| 349 | + , |
|
| 350 | + $subject_id |
|
| 351 | + ); |
|
| 352 | + |
|
| 353 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Add the `post_status` clause. |
|
| 358 | + * |
|
| 359 | + * @param null|string|array $status The status values. |
|
| 360 | + * |
|
| 361 | + * @return string An empty string if $status is null, otherwise the status clause. |
|
| 362 | + * @since 3.15.0 |
|
| 363 | + * |
|
| 364 | + */ |
|
| 365 | + private static function and_status( $status = null ) { |
|
| 366 | + |
|
| 367 | + if ( null === $status ) { |
|
| 368 | + return ''; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Add the `predicate` clause. |
|
| 376 | + * |
|
| 377 | + * @param null|string|array $predicate An array of predicates. |
|
| 378 | + * |
|
| 379 | + * @return string An empty string if $predicate is null otherwise the predicate |
|
| 380 | + * clause. |
|
| 381 | + * @since 3.15.0 |
|
| 382 | + * |
|
| 383 | + */ |
|
| 384 | + private static function and_predicate( $predicate = null ) { |
|
| 385 | + |
|
| 386 | + if ( null === $predicate ) { |
|
| 387 | + return ''; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * The select fields. |
|
| 395 | + * |
|
| 396 | + * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 397 | + * |
|
| 398 | + * @return string The `id` field if `ids` otherwise `*`. |
|
| 399 | + * @since 3.15.0 |
|
| 400 | + * |
|
| 401 | + */ |
|
| 402 | + private static function fields( $fields = '*' ) { |
|
| 403 | + |
|
| 404 | + // The output fields. |
|
| 405 | + return 'ids' === $fields ? 'id' : '*'; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * The inner join clause for articles. |
|
| 410 | + * |
|
| 411 | + * @return string The articles inner join clause. |
|
| 412 | + * @since 3.15.0 |
|
| 413 | + * |
|
| 414 | + */ |
|
| 415 | + private static function inner_join_is_article() { |
|
| 416 | + global $wpdb; |
|
| 417 | + |
|
| 418 | + return $wpdb->prepare( |
|
| 419 | + " |
|
| 420 | 420 | INNER JOIN $wpdb->term_relationships tr |
| 421 | 421 | ON p.id = tr.object_id |
| 422 | 422 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -426,23 +426,23 @@ discard block |
||
| 426 | 426 | ON t.term_id = tt.term_id |
| 427 | 427 | AND t.slug = %s |
| 428 | 428 | ", |
| 429 | - 'wl_entity_type', |
|
| 430 | - 'article' |
|
| 431 | - ); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * The inner join clause for non-articles. |
|
| 436 | - * |
|
| 437 | - * @return string The non-articles inner join clause. |
|
| 438 | - * @since 3.15.0 |
|
| 439 | - * |
|
| 440 | - */ |
|
| 441 | - private static function inner_join_is_not_article() { |
|
| 442 | - global $wpdb; |
|
| 443 | - |
|
| 444 | - return $wpdb->prepare( |
|
| 445 | - " |
|
| 429 | + 'wl_entity_type', |
|
| 430 | + 'article' |
|
| 431 | + ); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * The inner join clause for non-articles. |
|
| 436 | + * |
|
| 437 | + * @return string The non-articles inner join clause. |
|
| 438 | + * @since 3.15.0 |
|
| 439 | + * |
|
| 440 | + */ |
|
| 441 | + private static function inner_join_is_not_article() { |
|
| 442 | + global $wpdb; |
|
| 443 | + |
|
| 444 | + return $wpdb->prepare( |
|
| 445 | + " |
|
| 446 | 446 | INNER JOIN $wpdb->term_relationships tr |
| 447 | 447 | ON p.id = tr.object_id |
| 448 | 448 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -452,29 +452,29 @@ discard block |
||
| 452 | 452 | ON t.term_id = tt.term_id |
| 453 | 453 | AND NOT t.slug = %s |
| 454 | 454 | ", |
| 455 | - 'wl_entity_type', |
|
| 456 | - 'article' |
|
| 457 | - ); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * Find all the subject IDs and their referenced/related object IDs. The |
|
| 462 | - * object IDs are returned as comma separated IDs in the `object_ids` key. |
|
| 463 | - * |
|
| 464 | - * @return mixed Database query results |
|
| 465 | - * @since 3.18.0 |
|
| 466 | - */ |
|
| 467 | - public function find_all_grouped_by_subject_id() { |
|
| 468 | - global $wpdb; |
|
| 469 | - |
|
| 470 | - return $wpdb->get_results( |
|
| 471 | - " |
|
| 455 | + 'wl_entity_type', |
|
| 456 | + 'article' |
|
| 457 | + ); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * Find all the subject IDs and their referenced/related object IDs. The |
|
| 462 | + * object IDs are returned as comma separated IDs in the `object_ids` key. |
|
| 463 | + * |
|
| 464 | + * @return mixed Database query results |
|
| 465 | + * @since 3.18.0 |
|
| 466 | + */ |
|
| 467 | + public function find_all_grouped_by_subject_id() { |
|
| 468 | + global $wpdb; |
|
| 469 | + |
|
| 470 | + return $wpdb->get_results( |
|
| 471 | + " |
|
| 472 | 472 | SELECT subject_id, GROUP_CONCAT( DISTINCT object_id ORDER BY object_id SEPARATOR ',' ) AS object_ids |
| 473 | 473 | FROM $this->relation_table |
| 474 | 474 | GROUP BY subject_id |
| 475 | 475 | " |
| 476 | - ); |
|
| 476 | + ); |
|
| 477 | 477 | |
| 478 | - } |
|
| 478 | + } |
|
| 479 | 479 | |
| 480 | 480 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function __construct() { |
| 54 | 54 | |
| 55 | - self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 55 | + self::$log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 56 | 56 | |
| 57 | 57 | global $wpdb; |
| 58 | 58 | |
@@ -96,24 +96,24 @@ discard block |
||
| 96 | 96 | * @return array|object|null Database query results |
| 97 | 97 | * @since 3.15.0 |
| 98 | 98 | */ |
| 99 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array() ) { |
|
| 99 | + public function get_article_subjects($object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array()) { |
|
| 100 | 100 | global $wpdb; |
| 101 | 101 | |
| 102 | 102 | // The output fields. |
| 103 | - $actual_fields = self::fields( $fields ); |
|
| 103 | + $actual_fields = self::fields($fields); |
|
| 104 | 104 | |
| 105 | - self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 105 | + self::$log->trace('Getting article subjects for object '.implode(', ', (array) $object_id).'...'); |
|
| 106 | 106 | |
| 107 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 107 | + $objects = $this->article_id_to_entity_id($object_id); |
|
| 108 | 108 | |
| 109 | 109 | // If there are no related objects, return an empty array. |
| 110 | - if ( empty( $objects ) ) { |
|
| 111 | - self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 110 | + if (empty($objects)) { |
|
| 111 | + self::$log->debug('No entities found for object '.implode(', ', (array) $object_id).'.'); |
|
| 112 | 112 | |
| 113 | 113 | return array(); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 116 | + self::$log->debug(count($objects).' entity id(s) found for object '.implode(', ', (array) $object_id).'.'); |
|
| 117 | 117 | |
| 118 | 118 | $sql = |
| 119 | 119 | " |
@@ -123,19 +123,19 @@ discard block |
||
| 123 | 123 | ON p.id = r.subject_id |
| 124 | 124 | " |
| 125 | 125 | // Add the status clause. |
| 126 | - . self::and_status( $status ) |
|
| 126 | + . self::and_status($status) |
|
| 127 | 127 | . self::inner_join_is_article() |
| 128 | - . self::where_object_id( $objects ) |
|
| 128 | + . self::where_object_id($objects) |
|
| 129 | 129 | // Since `object_id` can be an article ID we need to exclude it from |
| 130 | 130 | // the results. |
| 131 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 132 | - . self::and_article_in( $include ) |
|
| 133 | - . self::and_post_type_in( $post_types ) |
|
| 134 | - . self::and_predicate( $predicate ) |
|
| 135 | - . self::order_by( $order_by ) |
|
| 136 | - . self::limit( $limit ); |
|
| 137 | - |
|
| 138 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 131 | + . self::and_article_not_in(array_merge($excludes, (array) $object_id)) |
|
| 132 | + . self::and_article_in($include) |
|
| 133 | + . self::and_post_type_in($post_types) |
|
| 134 | + . self::and_predicate($predicate) |
|
| 135 | + . self::order_by($order_by) |
|
| 136 | + . self::limit($limit); |
|
| 137 | + |
|
| 138 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | /** |
@@ -147,16 +147,16 @@ discard block |
||
| 147 | 147 | * @return string The `post_type IN` clause. |
| 148 | 148 | * @since 3.15.3 |
| 149 | 149 | */ |
| 150 | - private static function and_post_type_in( $post_types = array() ) { |
|
| 150 | + private static function and_post_type_in($post_types = array()) { |
|
| 151 | 151 | |
| 152 | - if ( $post_types === array() ) { |
|
| 152 | + if ($post_types === array()) { |
|
| 153 | 153 | $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | return " AND p.post_type IN ( '" |
| 157 | 157 | . implode( |
| 158 | 158 | "','", |
| 159 | - array_map( 'esc_sql', $post_types ) |
|
| 159 | + array_map('esc_sql', $post_types) |
|
| 160 | 160 | ) |
| 161 | 161 | . "' )"; |
| 162 | 162 | } |
@@ -170,9 +170,9 @@ discard block |
||
| 170 | 170 | * @since 3.15.0 |
| 171 | 171 | * |
| 172 | 172 | */ |
| 173 | - private static function limit( $limit = null ) { |
|
| 173 | + private static function limit($limit = null) { |
|
| 174 | 174 | |
| 175 | - if ( null === $limit ) { |
|
| 175 | + if (null === $limit) { |
|
| 176 | 176 | return ''; |
| 177 | 177 | } |
| 178 | 178 | |
@@ -184,14 +184,14 @@ discard block |
||
| 184 | 184 | * |
| 185 | 185 | * @return string |
| 186 | 186 | */ |
| 187 | - private static function order_by( $order_by ) { |
|
| 188 | - if ( ! $order_by ) { |
|
| 187 | + private static function order_by($order_by) { |
|
| 188 | + if ( ! $order_by) { |
|
| 189 | 189 | return ''; |
| 190 | 190 | } |
| 191 | 191 | $order_by = (string) $order_by; |
| 192 | - $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 192 | + $order_by_clauses = array('DESC', 'ASC'); |
|
| 193 | 193 | |
| 194 | - if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 194 | + if (in_array($order_by, $order_by_clauses, true)) { |
|
| 195 | 195 | return " ORDER BY p.post_modified ${order_by} "; |
| 196 | 196 | } else { |
| 197 | 197 | return ' ORDER BY p.post_modified DESC '; |
@@ -208,19 +208,19 @@ discard block |
||
| 208 | 208 | * @since 3.15.0 |
| 209 | 209 | * |
| 210 | 210 | */ |
| 211 | - private function article_id_to_entity_id( $object_id ) { |
|
| 211 | + private function article_id_to_entity_id($object_id) { |
|
| 212 | 212 | |
| 213 | 213 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 214 | 214 | |
| 215 | 215 | $relation_service = $this; |
| 216 | 216 | |
| 217 | - return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 218 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 219 | - return array_merge( $carry, (array) $item ); |
|
| 217 | + return array_reduce((array) $object_id, function($carry, $item) use ($entity_service, $relation_service) { |
|
| 218 | + if ($entity_service->is_entity($item)) { |
|
| 219 | + return array_merge($carry, (array) $item); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 223 | - }, array() ); |
|
| 222 | + return array_merge($carry, $relation_service->get_objects($item, 'ids')); |
|
| 223 | + }, array()); |
|
| 224 | 224 | |
| 225 | 225 | } |
| 226 | 226 | |
@@ -233,15 +233,15 @@ discard block |
||
| 233 | 233 | * @since 3.15.0 |
| 234 | 234 | * |
| 235 | 235 | */ |
| 236 | - private static function where_object_id( $object_id ) { |
|
| 236 | + private static function where_object_id($object_id) { |
|
| 237 | 237 | |
| 238 | - if ( empty( $object_id ) ) { |
|
| 238 | + if (empty($object_id)) { |
|
| 239 | 239 | // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
| 240 | 240 | |
| 241 | 241 | return ' WHERE 1 = 1'; |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 244 | + return ' WHERE r.object_id IN ( '.implode(',', wp_parse_id_list((array) $object_id)).' )'; |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -253,9 +253,9 @@ discard block |
||
| 253 | 253 | * @since 3.15.0 |
| 254 | 254 | * |
| 255 | 255 | */ |
| 256 | - private static function and_article_not_in( $exclude ) { |
|
| 256 | + private static function and_article_not_in($exclude) { |
|
| 257 | 257 | |
| 258 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 258 | + return ' AND NOT p.ID IN ( '.implode(',', wp_parse_id_list((array) $exclude)).' )'; |
|
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | /** |
@@ -268,13 +268,13 @@ discard block |
||
| 268 | 268 | * @since 3.15.0 |
| 269 | 269 | * |
| 270 | 270 | */ |
| 271 | - private static function and_article_in( $include = null ) { |
|
| 271 | + private static function and_article_in($include = null) { |
|
| 272 | 272 | |
| 273 | - if ( null === $include ) { |
|
| 273 | + if (null === $include) { |
|
| 274 | 274 | return ''; |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 277 | + return ' AND p.ID IN ( '.implode(',', wp_parse_id_list((array) $include)).' )'; |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | /** |
@@ -289,11 +289,11 @@ discard block |
||
| 289 | 289 | * @since 3.15.0 |
| 290 | 290 | * |
| 291 | 291 | */ |
| 292 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 292 | + public function get_non_article_subjects($object_id, $fields = '*', $status = null) { |
|
| 293 | 293 | global $wpdb; |
| 294 | 294 | |
| 295 | 295 | // The output fields. |
| 296 | - $actual_fields = self::fields( $fields ); |
|
| 296 | + $actual_fields = self::fields($fields); |
|
| 297 | 297 | |
| 298 | 298 | $sql = $wpdb->prepare( |
| 299 | 299 | " |
@@ -303,7 +303,7 @@ discard block |
||
| 303 | 303 | ON p.id = r.subject_id |
| 304 | 304 | " |
| 305 | 305 | // Add the status clause. |
| 306 | - . self::and_status( $status ) |
|
| 306 | + . self::and_status($status) |
|
| 307 | 307 | . self::inner_join_is_not_article() |
| 308 | 308 | . " WHERE r.object_id = %d " |
| 309 | 309 | . self::and_post_type_in() |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | $object_id |
| 312 | 312 | ); |
| 313 | 313 | |
| 314 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 314 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | /** |
@@ -327,11 +327,11 @@ discard block |
||
| 327 | 327 | * @since 3.15.0 |
| 328 | 328 | * |
| 329 | 329 | */ |
| 330 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 330 | + public function get_objects($subject_id, $fields = '*', $predicate = null, $status = null) { |
|
| 331 | 331 | global $wpdb; |
| 332 | 332 | |
| 333 | 333 | // The output fields. |
| 334 | - $actual_fields = self::fields( $fields ); |
|
| 334 | + $actual_fields = self::fields($fields); |
|
| 335 | 335 | |
| 336 | 336 | $sql = $wpdb->prepare( |
| 337 | 337 | " |
@@ -341,16 +341,16 @@ discard block |
||
| 341 | 341 | ON p.id = r.object_id |
| 342 | 342 | " |
| 343 | 343 | // Add the status clause. |
| 344 | - . self::and_status( $status ) |
|
| 344 | + . self::and_status($status) |
|
| 345 | 345 | . self::inner_join_is_not_article() |
| 346 | 346 | . " WHERE r.subject_id = %d " |
| 347 | 347 | . self::and_post_type_in() |
| 348 | - . self::and_predicate( $predicate ) |
|
| 348 | + . self::and_predicate($predicate) |
|
| 349 | 349 | , |
| 350 | 350 | $subject_id |
| 351 | 351 | ); |
| 352 | 352 | |
| 353 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 353 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | /** |
@@ -362,13 +362,13 @@ discard block |
||
| 362 | 362 | * @since 3.15.0 |
| 363 | 363 | * |
| 364 | 364 | */ |
| 365 | - private static function and_status( $status = null ) { |
|
| 365 | + private static function and_status($status = null) { |
|
| 366 | 366 | |
| 367 | - if ( null === $status ) { |
|
| 367 | + if (null === $status) { |
|
| 368 | 368 | return ''; |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 371 | + return " AND p.post_status IN ('".implode("', '", array_map('esc_sql', (array) $status))."')"; |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | /** |
@@ -381,13 +381,13 @@ discard block |
||
| 381 | 381 | * @since 3.15.0 |
| 382 | 382 | * |
| 383 | 383 | */ |
| 384 | - private static function and_predicate( $predicate = null ) { |
|
| 384 | + private static function and_predicate($predicate = null) { |
|
| 385 | 385 | |
| 386 | - if ( null === $predicate ) { |
|
| 386 | + if (null === $predicate) { |
|
| 387 | 387 | return ''; |
| 388 | 388 | } |
| 389 | 389 | |
| 390 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 390 | + return " AND r.predicate IN ('".implode("', '", array_map('esc_sql', (array) $predicate))."')"; |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | * @since 3.15.0 |
| 400 | 400 | * |
| 401 | 401 | */ |
| 402 | - private static function fields( $fields = '*' ) { |
|
| 402 | + private static function fields($fields = '*') { |
|
| 403 | 403 | |
| 404 | 404 | // The output fields. |
| 405 | 405 | return 'ids' === $fields ? 'id' : '*'; |