@@ -49,6 +49,11 @@ |
||
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | + /** |
|
| 53 | + * @param string $url |
|
| 54 | + * |
|
| 55 | + * @return integer|null |
|
| 56 | + */ |
|
| 52 | 57 | private function url_to_postid( $url ){ |
| 53 | 58 | // Try with url_to_postid |
| 54 | 59 | $post_id = url_to_postid( $url ); |
@@ -9,55 +9,55 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Wordlift_Context_Cards_Service { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - private $endpoint; |
|
| 16 | - |
|
| 17 | - function __construct() { |
|
| 18 | - |
|
| 19 | - $this->endpoint = '/jsonld'; |
|
| 20 | - |
|
| 21 | - // PHP 5.3 compatibility as `$this` cannot be used in closures. |
|
| 22 | - $that = $this; |
|
| 23 | - |
|
| 24 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 25 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 26 | - 'methods' => 'GET', |
|
| 27 | - 'callback' => array( $that, 'context_data' ), |
|
| 28 | - ) ); |
|
| 29 | - } ); |
|
| 30 | - |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - public function context_data( $request ) { |
|
| 34 | - |
|
| 35 | - $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 36 | - $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 37 | - $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 38 | - |
|
| 39 | - return $jsonld; |
|
| 40 | - |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function enqueue_scripts() { |
|
| 44 | - $show_context_cards = true; |
|
| 45 | - $show_context_cards = apply_filters( 'wl_show_context_cards', $show_context_cards ); |
|
| 46 | - if ( $show_context_cards ) { |
|
| 47 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 48 | - wp_add_inline_script( 'wordlift-cloud', "wordliftCloud.contextCards('a.wl-entity-page-link', '" . get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint . "')" ); |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - private function url_to_postid( $url ){ |
|
| 53 | - // Try with url_to_postid |
|
| 54 | - $post_id = url_to_postid( $url ); |
|
| 55 | - if ( $post_id == 0 ) { |
|
| 56 | - // Try with get_page_by_path |
|
| 57 | - $post = get_page_by_path( basename( untrailingslashit( $url ) ) , OBJECT, 'entity'); |
|
| 58 | - if($post) $post_id = $post->ID; |
|
| 59 | - } |
|
| 60 | - return $post_id; |
|
| 61 | - } |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + private $endpoint; |
|
| 16 | + |
|
| 17 | + function __construct() { |
|
| 18 | + |
|
| 19 | + $this->endpoint = '/jsonld'; |
|
| 20 | + |
|
| 21 | + // PHP 5.3 compatibility as `$this` cannot be used in closures. |
|
| 22 | + $that = $this; |
|
| 23 | + |
|
| 24 | + add_action( 'rest_api_init', function () use ( $that ) { |
|
| 25 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 26 | + 'methods' => 'GET', |
|
| 27 | + 'callback' => array( $that, 'context_data' ), |
|
| 28 | + ) ); |
|
| 29 | + } ); |
|
| 30 | + |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + public function context_data( $request ) { |
|
| 34 | + |
|
| 35 | + $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 36 | + $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 37 | + $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 38 | + |
|
| 39 | + return $jsonld; |
|
| 40 | + |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function enqueue_scripts() { |
|
| 44 | + $show_context_cards = true; |
|
| 45 | + $show_context_cards = apply_filters( 'wl_show_context_cards', $show_context_cards ); |
|
| 46 | + if ( $show_context_cards ) { |
|
| 47 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 48 | + wp_add_inline_script( 'wordlift-cloud', "wordliftCloud.contextCards('a.wl-entity-page-link', '" . get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint . "')" ); |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + private function url_to_postid( $url ){ |
|
| 53 | + // Try with url_to_postid |
|
| 54 | + $post_id = url_to_postid( $url ); |
|
| 55 | + if ( $post_id == 0 ) { |
|
| 56 | + // Try with get_page_by_path |
|
| 57 | + $post = get_page_by_path( basename( untrailingslashit( $url ) ) , OBJECT, 'entity'); |
|
| 58 | + if($post) $post_id = $post->ID; |
|
| 59 | + } |
|
| 60 | + return $post_id; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | 63 | } |
@@ -21,20 +21,20 @@ discard block |
||
| 21 | 21 | // PHP 5.3 compatibility as `$this` cannot be used in closures. |
| 22 | 22 | $that = $this; |
| 23 | 23 | |
| 24 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 25 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 24 | + add_action('rest_api_init', function() use ($that) { |
|
| 25 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 26 | 26 | 'methods' => 'GET', |
| 27 | - 'callback' => array( $that, 'context_data' ), |
|
| 28 | - ) ); |
|
| 27 | + 'callback' => array($that, 'context_data'), |
|
| 28 | + )); |
|
| 29 | 29 | } ); |
| 30 | 30 | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public function context_data( $request ) { |
|
| 33 | + public function context_data($request) { |
|
| 34 | 34 | |
| 35 | - $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 36 | - $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 37 | - $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 35 | + $entity_uri = urldecode($request->get_param('entity_url')); |
|
| 36 | + $entity_id = $this->url_to_postid($entity_uri); |
|
| 37 | + $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld(false, $entity_id); |
|
| 38 | 38 | |
| 39 | 39 | return $jsonld; |
| 40 | 40 | |
@@ -42,20 +42,20 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | public function enqueue_scripts() { |
| 44 | 44 | $show_context_cards = true; |
| 45 | - $show_context_cards = apply_filters( 'wl_show_context_cards', $show_context_cards ); |
|
| 46 | - if ( $show_context_cards ) { |
|
| 47 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 48 | - wp_add_inline_script( 'wordlift-cloud', "wordliftCloud.contextCards('a.wl-entity-page-link', '" . get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint . "')" ); |
|
| 45 | + $show_context_cards = apply_filters('wl_show_context_cards', $show_context_cards); |
|
| 46 | + if ($show_context_cards) { |
|
| 47 | + wp_enqueue_script('wordlift-cloud'); |
|
| 48 | + wp_add_inline_script('wordlift-cloud', "wordliftCloud.contextCards('a.wl-entity-page-link', '".get_rest_url().WL_REST_ROUTE_DEFAULT_NAMESPACE.$this->endpoint."')"); |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - private function url_to_postid( $url ){ |
|
| 52 | + private function url_to_postid($url) { |
|
| 53 | 53 | // Try with url_to_postid |
| 54 | - $post_id = url_to_postid( $url ); |
|
| 55 | - if ( $post_id == 0 ) { |
|
| 54 | + $post_id = url_to_postid($url); |
|
| 55 | + if ($post_id == 0) { |
|
| 56 | 56 | // Try with get_page_by_path |
| 57 | - $post = get_page_by_path( basename( untrailingslashit( $url ) ) , OBJECT, 'entity'); |
|
| 58 | - if($post) $post_id = $post->ID; |
|
| 57 | + $post = get_page_by_path(basename(untrailingslashit($url)), OBJECT, 'entity'); |
|
| 58 | + if ($post) $post_id = $post->ID; |
|
| 59 | 59 | } |
| 60 | 60 | return $post_id; |
| 61 | 61 | } |
@@ -55,7 +55,9 @@ |
||
| 55 | 55 | if ( $post_id == 0 ) { |
| 56 | 56 | // Try with get_page_by_path |
| 57 | 57 | $post = get_page_by_path( basename( untrailingslashit( $url ) ) , OBJECT, 'entity'); |
| 58 | - if($post) $post_id = $post->ID; |
|
| 58 | + if($post) { |
|
| 59 | + $post_id = $post->ID; |
|
| 60 | + } |
|
| 59 | 61 | } |
| 60 | 62 | return $post_id; |
| 61 | 63 | } |
@@ -14,117 +14,117 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wl_shortcode_navigator_data() { |
| 16 | 16 | |
| 17 | - // Post ID must be defined |
|
| 18 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 19 | - wp_die( 'No post_id given' ); |
|
| 17 | + // Post ID must be defined |
|
| 18 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 19 | + wp_die( 'No post_id given' ); |
|
| 20 | 20 | |
| 21 | - return; |
|
| 22 | - } |
|
| 21 | + return; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - // Limit the results (defaults to 4) |
|
| 25 | - $navigator_length = isset( $_GET['limit'] ) ? intval($_GET['limit']) : 4; |
|
| 26 | - $navigator_offset = isset( $_GET['offset'] ) ? intval($_GET['offset']) : 0; |
|
| 24 | + // Limit the results (defaults to 4) |
|
| 25 | + $navigator_length = isset( $_GET['limit'] ) ? intval($_GET['limit']) : 4; |
|
| 26 | + $navigator_offset = isset( $_GET['offset'] ) ? intval($_GET['offset']) : 0; |
|
| 27 | 27 | |
| 28 | - $current_post_id = $_GET['post_id']; |
|
| 29 | - $current_post = get_post( $current_post_id ); |
|
| 28 | + $current_post_id = $_GET['post_id']; |
|
| 29 | + $current_post = get_post( $current_post_id ); |
|
| 30 | 30 | |
| 31 | - $navigator_id = $_GET['uniqid']; |
|
| 31 | + $navigator_id = $_GET['uniqid']; |
|
| 32 | 32 | |
| 33 | - // Post ID has to match an existing item |
|
| 34 | - if ( null === $current_post ) { |
|
| 35 | - wp_die( 'No valid post_id given' ); |
|
| 33 | + // Post ID has to match an existing item |
|
| 34 | + if ( null === $current_post ) { |
|
| 35 | + wp_die( 'No valid post_id given' ); |
|
| 36 | 36 | |
| 37 | - return; |
|
| 38 | - } |
|
| 37 | + return; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - // prepare structures to memorize other related posts |
|
| 41 | - $results = array(); |
|
| 42 | - $blacklist_ids = array( $current_post_id ); |
|
| 43 | - $related_entities = array(); |
|
| 40 | + // prepare structures to memorize other related posts |
|
| 41 | + $results = array(); |
|
| 42 | + $blacklist_ids = array( $current_post_id ); |
|
| 43 | + $related_entities = array(); |
|
| 44 | 44 | |
| 45 | 45 | |
| 46 | - $relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 46 | + $relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 47 | 47 | |
| 48 | - // Get the related entities, ordering them by WHO, WHAT, WHERE, WHEN |
|
| 49 | - // TODO Replace with a single query if it is possible |
|
| 50 | - // We select in inverse order to give priority to less used entities |
|
| 51 | - foreach ( |
|
| 52 | - array( |
|
| 53 | - WL_WHEN_RELATION, |
|
| 54 | - WL_WHERE_RELATION, |
|
| 55 | - WL_WHAT_RELATION, |
|
| 56 | - WL_WHO_RELATION, |
|
| 57 | - ) as $predicate |
|
| 58 | - ) { |
|
| 48 | + // Get the related entities, ordering them by WHO, WHAT, WHERE, WHEN |
|
| 49 | + // TODO Replace with a single query if it is possible |
|
| 50 | + // We select in inverse order to give priority to less used entities |
|
| 51 | + foreach ( |
|
| 52 | + array( |
|
| 53 | + WL_WHEN_RELATION, |
|
| 54 | + WL_WHERE_RELATION, |
|
| 55 | + WL_WHAT_RELATION, |
|
| 56 | + WL_WHO_RELATION, |
|
| 57 | + ) as $predicate |
|
| 58 | + ) { |
|
| 59 | 59 | |
| 60 | - $related_entities = array_merge( $related_entities, |
|
| 61 | - $relation_service->get_objects( $current_post_id, '*', $predicate, 'publish' ) |
|
| 60 | + $related_entities = array_merge( $related_entities, |
|
| 61 | + $relation_service->get_objects( $current_post_id, '*', $predicate, 'publish' ) |
|
| 62 | 62 | // wl_core_get_related_entities( $current_post_id, array( |
| 63 | 63 | // 'predicate' => $predicate, |
| 64 | 64 | // 'status' => 'publish', |
| 65 | 65 | // ) |
| 66 | - ); |
|
| 66 | + ); |
|
| 67 | 67 | |
| 68 | - } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - foreach ( $related_entities as $related_entity ) { |
|
| 70 | + foreach ( $related_entities as $related_entity ) { |
|
| 71 | 71 | |
| 72 | - // take the id of posts referencing the entity |
|
| 73 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( $related_entity->ID, '*', null, 'publish' ); |
|
| 72 | + // take the id of posts referencing the entity |
|
| 73 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( $related_entity->ID, '*', null, 'publish' ); |
|
| 74 | 74 | |
| 75 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 76 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 75 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 76 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 77 | 77 | |
| 78 | - if ( ! in_array( $referencing_post->ID, $blacklist_ids ) ) { |
|
| 78 | + if ( ! in_array( $referencing_post->ID, $blacklist_ids ) ) { |
|
| 79 | 79 | |
| 80 | - $blacklist_ids[] = $referencing_post->ID; |
|
| 81 | - $serialized_entity = wl_serialize_entity( $related_entity ); |
|
| 80 | + $blacklist_ids[] = $referencing_post->ID; |
|
| 81 | + $serialized_entity = wl_serialize_entity( $related_entity ); |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Use the thumbnail. |
|
| 85 | - * |
|
| 86 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 87 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 88 | - * |
|
| 89 | - * @since 3.19.3 We're using the medium size image. |
|
| 90 | - */ |
|
| 91 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 83 | + /** |
|
| 84 | + * Use the thumbnail. |
|
| 85 | + * |
|
| 86 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 87 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 88 | + * |
|
| 89 | + * @since 3.19.3 We're using the medium size image. |
|
| 90 | + */ |
|
| 91 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 92 | 92 | |
| 93 | - if ( $thumbnail ) { |
|
| 93 | + if ( $thumbnail ) { |
|
| 94 | 94 | |
| 95 | - $result = array( |
|
| 96 | - 'post' => array( |
|
| 97 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 98 | - 'title' => $referencing_post->post_title, |
|
| 99 | - 'thumbnail' => $thumbnail, |
|
| 100 | - ), |
|
| 101 | - 'entity' => array( |
|
| 102 | - 'label' => $serialized_entity['label'], |
|
| 103 | - 'mainType' => $serialized_entity['mainType'], |
|
| 104 | - 'permalink' => get_permalink( $related_entity->ID ), |
|
| 105 | - ), |
|
| 106 | - ); |
|
| 95 | + $result = array( |
|
| 96 | + 'post' => array( |
|
| 97 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 98 | + 'title' => $referencing_post->post_title, |
|
| 99 | + 'thumbnail' => $thumbnail, |
|
| 100 | + ), |
|
| 101 | + 'entity' => array( |
|
| 102 | + 'label' => $serialized_entity['label'], |
|
| 103 | + 'mainType' => $serialized_entity['mainType'], |
|
| 104 | + 'permalink' => get_permalink( $related_entity->ID ), |
|
| 105 | + ), |
|
| 106 | + ); |
|
| 107 | 107 | |
| 108 | - $result['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id ); |
|
| 109 | - $result['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval($related_entity->ID), $navigator_id ); |
|
| 108 | + $result['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id ); |
|
| 109 | + $result['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval($related_entity->ID), $navigator_id ); |
|
| 110 | 110 | |
| 111 | - $results[] = $result; |
|
| 111 | + $results[] = $result; |
|
| 112 | 112 | |
| 113 | - // Be sure no more than 1 post for entity is returned |
|
| 114 | - break; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 113 | + // Be sure no more than 1 post for entity is returned |
|
| 114 | + break; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - $results = array_reverse( $results ); |
|
| 120 | + $results = array_reverse( $results ); |
|
| 121 | 121 | |
| 122 | - if(count($results) < ($navigator_offset + $navigator_length)){ |
|
| 123 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id ); |
|
| 124 | - } |
|
| 122 | + if(count($results) < ($navigator_offset + $navigator_length)){ |
|
| 123 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id ); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - // Return first 4 results in json accordingly to 4 columns layout |
|
| 127 | - return array_slice( $results, $navigator_offset, $navigator_length ); |
|
| 126 | + // Return first 4 results in json accordingly to 4 columns layout |
|
| 127 | + return array_slice( $results, $navigator_offset, $navigator_length ); |
|
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | |
@@ -135,8 +135,8 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | function wl_shortcode_navigator_ajax() { |
| 137 | 137 | |
| 138 | - $results = wl_shortcode_navigator_data(); |
|
| 139 | - wl_core_send_json($results); |
|
| 138 | + $results = wl_shortcode_navigator_data(); |
|
| 139 | + wl_core_send_json($results); |
|
| 140 | 140 | |
| 141 | 141 | } |
| 142 | 142 | |
@@ -148,15 +148,15 @@ discard block |
||
| 148 | 148 | */ |
| 149 | 149 | function wl_shortcode_navigator_wp_json() { |
| 150 | 150 | |
| 151 | - $results = wl_shortcode_navigator_data(); |
|
| 152 | - if ( ob_get_contents() ) { |
|
| 153 | - ob_clean(); |
|
| 154 | - } |
|
| 155 | - return array( |
|
| 156 | - 'items' => array( |
|
| 157 | - array('values' => $results) |
|
| 158 | - ) |
|
| 159 | - ); |
|
| 151 | + $results = wl_shortcode_navigator_data(); |
|
| 152 | + if ( ob_get_contents() ) { |
|
| 153 | + ob_clean(); |
|
| 154 | + } |
|
| 155 | + return array( |
|
| 156 | + 'items' => array( |
|
| 157 | + array('values' => $results) |
|
| 158 | + ) |
|
| 159 | + ); |
|
| 160 | 160 | |
| 161 | 161 | } |
| 162 | 162 | |
@@ -164,53 +164,53 @@ discard block |
||
| 164 | 164 | * Adding `rest_api_init` action for amp backend of navigator |
| 165 | 165 | */ |
| 166 | 166 | add_action( 'rest_api_init', function () { |
| 167 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 168 | - 'methods' => 'GET', |
|
| 169 | - 'callback' => 'wl_shortcode_navigator_wp_json', |
|
| 170 | - ) ); |
|
| 167 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 168 | + 'methods' => 'GET', |
|
| 169 | + 'callback' => 'wl_shortcode_navigator_wp_json', |
|
| 170 | + ) ); |
|
| 171 | 171 | } ); |
| 172 | 172 | |
| 173 | 173 | /** |
| 174 | 174 | * register_block_type for Gutenberg blocks |
| 175 | 175 | */ |
| 176 | 176 | add_action( 'init', function() { |
| 177 | - // Bail out if the `register_block_type` function isn't available. |
|
| 178 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 179 | - return; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - register_block_type('wordlift/navigator', array( |
|
| 183 | - 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 184 | - 'render_callback' => function($attributes){ |
|
| 185 | - $attr_code = ''; |
|
| 186 | - foreach ($attributes as $key => $value) { |
|
| 187 | - $attr_code .= $key.'="'.$value.'" '; |
|
| 188 | - } |
|
| 189 | - return '[wl_navigator '.$attr_code.']'; |
|
| 190 | - }, |
|
| 191 | - 'attributes' => array( |
|
| 192 | - 'title' => array( |
|
| 193 | - 'type' => 'string', |
|
| 194 | - 'default' => __( 'Related articles', 'wordlift' ) |
|
| 195 | - ), |
|
| 196 | - 'limit' => array( |
|
| 197 | - 'type' => 'number', |
|
| 198 | - 'default' => 4, |
|
| 199 | - ), |
|
| 200 | - 'template_id' => array( |
|
| 201 | - 'type' => 'string' |
|
| 202 | - ), |
|
| 203 | - 'post_id' => array( |
|
| 204 | - 'type' => 'number' |
|
| 205 | - ), |
|
| 206 | - 'offset' => array( |
|
| 207 | - 'type' => 'number', |
|
| 208 | - 'default' => 0, |
|
| 209 | - ), |
|
| 210 | - 'uniqid' => array( |
|
| 211 | - 'type' => 'string', |
|
| 212 | - 'default' => uniqid( 'wl-navigator-widget-' ), |
|
| 213 | - ) |
|
| 214 | - ) |
|
| 215 | - )); |
|
| 177 | + // Bail out if the `register_block_type` function isn't available. |
|
| 178 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 179 | + return; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + register_block_type('wordlift/navigator', array( |
|
| 183 | + 'editor_script' => 'wordlift-admin-edit-gutenberg', |
|
| 184 | + 'render_callback' => function($attributes){ |
|
| 185 | + $attr_code = ''; |
|
| 186 | + foreach ($attributes as $key => $value) { |
|
| 187 | + $attr_code .= $key.'="'.$value.'" '; |
|
| 188 | + } |
|
| 189 | + return '[wl_navigator '.$attr_code.']'; |
|
| 190 | + }, |
|
| 191 | + 'attributes' => array( |
|
| 192 | + 'title' => array( |
|
| 193 | + 'type' => 'string', |
|
| 194 | + 'default' => __( 'Related articles', 'wordlift' ) |
|
| 195 | + ), |
|
| 196 | + 'limit' => array( |
|
| 197 | + 'type' => 'number', |
|
| 198 | + 'default' => 4, |
|
| 199 | + ), |
|
| 200 | + 'template_id' => array( |
|
| 201 | + 'type' => 'string' |
|
| 202 | + ), |
|
| 203 | + 'post_id' => array( |
|
| 204 | + 'type' => 'number' |
|
| 205 | + ), |
|
| 206 | + 'offset' => array( |
|
| 207 | + 'type' => 'number', |
|
| 208 | + 'default' => 0, |
|
| 209 | + ), |
|
| 210 | + 'uniqid' => array( |
|
| 211 | + 'type' => 'string', |
|
| 212 | + 'default' => uniqid( 'wl-navigator-widget-' ), |
|
| 213 | + ) |
|
| 214 | + ) |
|
| 215 | + )); |
|
| 216 | 216 | } ); |
@@ -15,31 +15,31 @@ discard block |
||
| 15 | 15 | function wl_shortcode_navigator_data() { |
| 16 | 16 | |
| 17 | 17 | // Post ID must be defined |
| 18 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 19 | - wp_die( 'No post_id given' ); |
|
| 18 | + if ( ! isset($_GET['post_id'])) { |
|
| 19 | + wp_die('No post_id given'); |
|
| 20 | 20 | |
| 21 | 21 | return; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | // Limit the results (defaults to 4) |
| 25 | - $navigator_length = isset( $_GET['limit'] ) ? intval($_GET['limit']) : 4; |
|
| 26 | - $navigator_offset = isset( $_GET['offset'] ) ? intval($_GET['offset']) : 0; |
|
| 25 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 26 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 27 | 27 | |
| 28 | 28 | $current_post_id = $_GET['post_id']; |
| 29 | - $current_post = get_post( $current_post_id ); |
|
| 29 | + $current_post = get_post($current_post_id); |
|
| 30 | 30 | |
| 31 | 31 | $navigator_id = $_GET['uniqid']; |
| 32 | 32 | |
| 33 | 33 | // Post ID has to match an existing item |
| 34 | - if ( null === $current_post ) { |
|
| 35 | - wp_die( 'No valid post_id given' ); |
|
| 34 | + if (null === $current_post) { |
|
| 35 | + wp_die('No valid post_id given'); |
|
| 36 | 36 | |
| 37 | 37 | return; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | // prepare structures to memorize other related posts |
| 41 | 41 | $results = array(); |
| 42 | - $blacklist_ids = array( $current_post_id ); |
|
| 42 | + $blacklist_ids = array($current_post_id); |
|
| 43 | 43 | $related_entities = array(); |
| 44 | 44 | |
| 45 | 45 | |
@@ -57,8 +57,8 @@ discard block |
||
| 57 | 57 | ) as $predicate |
| 58 | 58 | ) { |
| 59 | 59 | |
| 60 | - $related_entities = array_merge( $related_entities, |
|
| 61 | - $relation_service->get_objects( $current_post_id, '*', $predicate, 'publish' ) |
|
| 60 | + $related_entities = array_merge($related_entities, |
|
| 61 | + $relation_service->get_objects($current_post_id, '*', $predicate, 'publish') |
|
| 62 | 62 | // wl_core_get_related_entities( $current_post_id, array( |
| 63 | 63 | // 'predicate' => $predicate, |
| 64 | 64 | // 'status' => 'publish', |
@@ -67,18 +67,18 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - foreach ( $related_entities as $related_entity ) { |
|
| 70 | + foreach ($related_entities as $related_entity) { |
|
| 71 | 71 | |
| 72 | 72 | // take the id of posts referencing the entity |
| 73 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( $related_entity->ID, '*', null, 'publish' ); |
|
| 73 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects($related_entity->ID, '*', null, 'publish'); |
|
| 74 | 74 | |
| 75 | 75 | // loop over them and take the first one which is not already in the $related_posts |
| 76 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 76 | + foreach ($referencing_posts as $referencing_post) { |
|
| 77 | 77 | |
| 78 | - if ( ! in_array( $referencing_post->ID, $blacklist_ids ) ) { |
|
| 78 | + if ( ! in_array($referencing_post->ID, $blacklist_ids)) { |
|
| 79 | 79 | |
| 80 | 80 | $blacklist_ids[] = $referencing_post->ID; |
| 81 | - $serialized_entity = wl_serialize_entity( $related_entity ); |
|
| 81 | + $serialized_entity = wl_serialize_entity($related_entity); |
|
| 82 | 82 | |
| 83 | 83 | /** |
| 84 | 84 | * Use the thumbnail. |
@@ -88,25 +88,25 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @since 3.19.3 We're using the medium size image. |
| 90 | 90 | */ |
| 91 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 91 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 92 | 92 | |
| 93 | - if ( $thumbnail ) { |
|
| 93 | + if ($thumbnail) { |
|
| 94 | 94 | |
| 95 | 95 | $result = array( |
| 96 | 96 | 'post' => array( |
| 97 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 97 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 98 | 98 | 'title' => $referencing_post->post_title, |
| 99 | 99 | 'thumbnail' => $thumbnail, |
| 100 | 100 | ), |
| 101 | 101 | 'entity' => array( |
| 102 | 102 | 'label' => $serialized_entity['label'], |
| 103 | 103 | 'mainType' => $serialized_entity['mainType'], |
| 104 | - 'permalink' => get_permalink( $related_entity->ID ), |
|
| 104 | + 'permalink' => get_permalink($related_entity->ID), |
|
| 105 | 105 | ), |
| 106 | 106 | ); |
| 107 | 107 | |
| 108 | - $result['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id ); |
|
| 109 | - $result['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval($related_entity->ID), $navigator_id ); |
|
| 108 | + $result['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 109 | + $result['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($related_entity->ID), $navigator_id); |
|
| 110 | 110 | |
| 111 | 111 | $results[] = $result; |
| 112 | 112 | |
@@ -117,14 +117,14 @@ discard block |
||
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - $results = array_reverse( $results ); |
|
| 120 | + $results = array_reverse($results); |
|
| 121 | 121 | |
| 122 | - if(count($results) < ($navigator_offset + $navigator_length)){ |
|
| 123 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id ); |
|
| 122 | + if (count($results) < ($navigator_offset + $navigator_length)) { |
|
| 123 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | // Return first 4 results in json accordingly to 4 columns layout |
| 127 | - return array_slice( $results, $navigator_offset, $navigator_length ); |
|
| 127 | + return array_slice($results, $navigator_offset, $navigator_length); |
|
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | |
@@ -140,8 +140,8 @@ discard block |
||
| 140 | 140 | |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 144 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 143 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 144 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 145 | 145 | |
| 146 | 146 | /** |
| 147 | 147 | * wp-json call for the navigator widget |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | function wl_shortcode_navigator_wp_json() { |
| 150 | 150 | |
| 151 | 151 | $results = wl_shortcode_navigator_data(); |
| 152 | - if ( ob_get_contents() ) { |
|
| 152 | + if (ob_get_contents()) { |
|
| 153 | 153 | ob_clean(); |
| 154 | 154 | } |
| 155 | 155 | return array( |
@@ -163,25 +163,25 @@ discard block |
||
| 163 | 163 | /** |
| 164 | 164 | * Adding `rest_api_init` action for amp backend of navigator |
| 165 | 165 | */ |
| 166 | -add_action( 'rest_api_init', function () { |
|
| 167 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 166 | +add_action('rest_api_init', function() { |
|
| 167 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 168 | 168 | 'methods' => 'GET', |
| 169 | 169 | 'callback' => 'wl_shortcode_navigator_wp_json', |
| 170 | - ) ); |
|
| 170 | + )); |
|
| 171 | 171 | } ); |
| 172 | 172 | |
| 173 | 173 | /** |
| 174 | 174 | * register_block_type for Gutenberg blocks |
| 175 | 175 | */ |
| 176 | -add_action( 'init', function() { |
|
| 176 | +add_action('init', function() { |
|
| 177 | 177 | // Bail out if the `register_block_type` function isn't available. |
| 178 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 178 | + if ( ! function_exists('register_block_type')) { |
|
| 179 | 179 | return; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | register_block_type('wordlift/navigator', array( |
| 183 | 183 | 'editor_script' => 'wordlift-admin-edit-gutenberg', |
| 184 | - 'render_callback' => function($attributes){ |
|
| 184 | + 'render_callback' => function($attributes) { |
|
| 185 | 185 | $attr_code = ''; |
| 186 | 186 | foreach ($attributes as $key => $value) { |
| 187 | 187 | $attr_code .= $key.'="'.$value.'" '; |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | 'attributes' => array( |
| 192 | 192 | 'title' => array( |
| 193 | 193 | 'type' => 'string', |
| 194 | - 'default' => __( 'Related articles', 'wordlift' ) |
|
| 194 | + 'default' => __('Related articles', 'wordlift') |
|
| 195 | 195 | ), |
| 196 | 196 | 'limit' => array( |
| 197 | 197 | 'type' => 'number', |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | ), |
| 210 | 210 | 'uniqid' => array( |
| 211 | 211 | 'type' => 'string', |
| 212 | - 'default' => uniqid( 'wl-navigator-widget-' ), |
|
| 212 | + 'default' => uniqid('wl-navigator-widget-'), |
|
| 213 | 213 | ) |
| 214 | 214 | ) |
| 215 | 215 | )); |
@@ -17,194 +17,194 @@ 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 | - /** |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - public function render( $atts ) { |
|
| 29 | - |
|
| 30 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 31 | - : $this->web_shortcode( $atts ); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 36 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 37 | - * |
|
| 38 | - * @since 3.20.0 |
|
| 39 | - * |
|
| 40 | - * @param array $atts Shortcode attributes. |
|
| 41 | - * |
|
| 42 | - * @return array $shortcode_atts |
|
| 43 | - */ |
|
| 44 | - private function make_shortcode_atts( $atts ) { |
|
| 45 | - |
|
| 46 | - // Extract attributes and set default values. |
|
| 47 | - $shortcode_atts = shortcode_atts( array( |
|
| 48 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 49 | - 'show_facets' => true, |
|
| 50 | - 'with_carousel' => true, |
|
| 51 | - 'squared_thumbs' => false, |
|
| 52 | - 'limit' => 20, |
|
| 53 | - ), $atts ); |
|
| 54 | - |
|
| 55 | - foreach ( |
|
| 56 | - array( |
|
| 57 | - 'show_facets', |
|
| 58 | - 'with_carousel', |
|
| 59 | - 'squared_thumbs', |
|
| 60 | - ) as $att |
|
| 61 | - ) { |
|
| 62 | - |
|
| 63 | - // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode |
|
| 64 | - $shortcode_atts[ $att ] = filter_var( |
|
| 65 | - $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 66 | - ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return $shortcode_atts; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 74 | - * |
|
| 75 | - * @since 3.20.0 |
|
| 76 | - * |
|
| 77 | - * @param array $atts Shortcode attributes. |
|
| 78 | - * |
|
| 79 | - * @return string Shortcode HTML for web |
|
| 80 | - */ |
|
| 81 | - private function web_shortcode( $atts ) { |
|
| 82 | - |
|
| 83 | - // attributes extraction and boolean filtering |
|
| 84 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 85 | - |
|
| 86 | - // If the current post is not an entity and has no related entities |
|
| 87 | - // than the shortcode cannot be rendered |
|
| 88 | - // TODO Add an alert visibile only for connected admin users. |
|
| 89 | - $current_post = get_post(); |
|
| 90 | - |
|
| 91 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 92 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 93 | - array( $current_post->ID ) : |
|
| 94 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 95 | - |
|
| 96 | - // Bail if there are no entity ids. |
|
| 97 | - if ( 0 === count( $entity_ids ) ) { |
|
| 98 | - return ''; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $div_id = 'wordlift-faceted-entity-search-widget'; |
|
| 102 | - |
|
| 103 | - // Enqueue common shortcode scripts. |
|
| 104 | - $this->enqueue_scripts(); |
|
| 105 | - |
|
| 106 | - // Enqueue shortcode specific scripts and styles. |
|
| 107 | - $deps = apply_filters( 'wl_include_font_awesome', true ) |
|
| 108 | - ? array( 'wordlift-font-awesome' ) |
|
| 109 | - : array(); |
|
| 110 | - wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css', $deps, Wordlift::get_instance()->get_version() ); |
|
| 111 | - wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.js' ); |
|
| 112 | - |
|
| 113 | - wp_localize_script( |
|
| 114 | - 'wordlift-faceted-search', |
|
| 115 | - 'wl_faceted_search_params', array( |
|
| 116 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 117 | - 'action' => 'wl_faceted_search', |
|
| 118 | - 'post_id' => $current_post->ID, |
|
| 119 | - 'entity_ids' => $entity_ids, |
|
| 120 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 121 | - 'div_id' => $div_id, |
|
| 122 | - 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
|
| 123 | - 'attrs' => $shortcode_atts, |
|
| 124 | - 'l10n' => array( |
|
| 125 | - 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 126 | - 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 127 | - 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 128 | - 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 129 | - ), |
|
| 130 | - ) |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 138 | - * |
|
| 139 | - * @since 3.20.0 |
|
| 140 | - * |
|
| 141 | - * @param array $atts Shortcode attributes. |
|
| 142 | - * |
|
| 143 | - * @return string Shortcode HTML for amp |
|
| 144 | - */ |
|
| 145 | - private function amp_shortcode( $atts ) { |
|
| 146 | - |
|
| 147 | - // attributes extraction and boolean filtering |
|
| 148 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 149 | - |
|
| 150 | - // If the current post is not an entity and has no related entities |
|
| 151 | - // than the shortcode cannot be rendered |
|
| 152 | - // TODO Add an alert visibile only for connected admin users. |
|
| 153 | - $current_post = get_post(); |
|
| 154 | - |
|
| 155 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 156 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 157 | - array( $current_post->ID ) : |
|
| 158 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 159 | - |
|
| 160 | - // Bail if there are no entity ids. |
|
| 161 | - if ( 0 === count( $entity_ids ) ) { |
|
| 162 | - return ''; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $div_id = 'wordlift-faceted-entity-search-widget'; |
|
| 166 | - |
|
| 167 | - // Enqueue amp specific styles |
|
| 168 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 169 | - |
|
| 170 | - $wp_json_base = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE; |
|
| 171 | - |
|
| 172 | - $query_posts = array( |
|
| 173 | - 'post_id' => $current_post->ID, |
|
| 174 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 175 | - 'type' => 'posts', |
|
| 176 | - ); |
|
| 177 | - |
|
| 178 | - $query_facets = array( |
|
| 179 | - 'post_id' => $current_post->ID, |
|
| 180 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 181 | - 'type' => 'facets', |
|
| 182 | - 'l10n' => array( |
|
| 183 | - 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 184 | - 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 185 | - 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 186 | - 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 187 | - ), |
|
| 188 | - ); |
|
| 189 | - |
|
| 190 | - if ( strpos( $wp_json_base, 'wp-json/' . WL_REST_ROUTE_DEFAULT_NAMESPACE ) ) { |
|
| 191 | - $delimiter = '?'; |
|
| 192 | - } else { |
|
| 193 | - $delimiter = '&'; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 197 | - // This is a hackish way, but this works for http and https URLs |
|
| 198 | - $wp_json_url_posts = str_replace( array( |
|
| 199 | - 'http:', |
|
| 200 | - 'https:', |
|
| 201 | - ), '', $wp_json_base ) . '/faceted-search' . $delimiter . http_build_query( $query_posts ); |
|
| 202 | - $wp_json_url_facets = str_replace( array( |
|
| 203 | - 'http:', |
|
| 204 | - 'https:', |
|
| 205 | - ), '', $wp_json_base ) . '/faceted-search' . $delimiter . http_build_query( $query_facets ); |
|
| 206 | - |
|
| 207 | - /* |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + const SHORTCODE = 'wl_faceted_search'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + public function render( $atts ) { |
|
| 29 | + |
|
| 30 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 31 | + : $this->web_shortcode( $atts ); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 36 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 37 | + * |
|
| 38 | + * @since 3.20.0 |
|
| 39 | + * |
|
| 40 | + * @param array $atts Shortcode attributes. |
|
| 41 | + * |
|
| 42 | + * @return array $shortcode_atts |
|
| 43 | + */ |
|
| 44 | + private function make_shortcode_atts( $atts ) { |
|
| 45 | + |
|
| 46 | + // Extract attributes and set default values. |
|
| 47 | + $shortcode_atts = shortcode_atts( array( |
|
| 48 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 49 | + 'show_facets' => true, |
|
| 50 | + 'with_carousel' => true, |
|
| 51 | + 'squared_thumbs' => false, |
|
| 52 | + 'limit' => 20, |
|
| 53 | + ), $atts ); |
|
| 54 | + |
|
| 55 | + foreach ( |
|
| 56 | + array( |
|
| 57 | + 'show_facets', |
|
| 58 | + 'with_carousel', |
|
| 59 | + 'squared_thumbs', |
|
| 60 | + ) as $att |
|
| 61 | + ) { |
|
| 62 | + |
|
| 63 | + // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode |
|
| 64 | + $shortcode_atts[ $att ] = filter_var( |
|
| 65 | + $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 66 | + ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return $shortcode_atts; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Function in charge of diplaying the [wl-faceted-search] in web mode. |
|
| 74 | + * |
|
| 75 | + * @since 3.20.0 |
|
| 76 | + * |
|
| 77 | + * @param array $atts Shortcode attributes. |
|
| 78 | + * |
|
| 79 | + * @return string Shortcode HTML for web |
|
| 80 | + */ |
|
| 81 | + private function web_shortcode( $atts ) { |
|
| 82 | + |
|
| 83 | + // attributes extraction and boolean filtering |
|
| 84 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 85 | + |
|
| 86 | + // If the current post is not an entity and has no related entities |
|
| 87 | + // than the shortcode cannot be rendered |
|
| 88 | + // TODO Add an alert visibile only for connected admin users. |
|
| 89 | + $current_post = get_post(); |
|
| 90 | + |
|
| 91 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 92 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 93 | + array( $current_post->ID ) : |
|
| 94 | + wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 95 | + |
|
| 96 | + // Bail if there are no entity ids. |
|
| 97 | + if ( 0 === count( $entity_ids ) ) { |
|
| 98 | + return ''; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $div_id = 'wordlift-faceted-entity-search-widget'; |
|
| 102 | + |
|
| 103 | + // Enqueue common shortcode scripts. |
|
| 104 | + $this->enqueue_scripts(); |
|
| 105 | + |
|
| 106 | + // Enqueue shortcode specific scripts and styles. |
|
| 107 | + $deps = apply_filters( 'wl_include_font_awesome', true ) |
|
| 108 | + ? array( 'wordlift-font-awesome' ) |
|
| 109 | + : array(); |
|
| 110 | + wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css', $deps, Wordlift::get_instance()->get_version() ); |
|
| 111 | + wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.js' ); |
|
| 112 | + |
|
| 113 | + wp_localize_script( |
|
| 114 | + 'wordlift-faceted-search', |
|
| 115 | + 'wl_faceted_search_params', array( |
|
| 116 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 117 | + 'action' => 'wl_faceted_search', |
|
| 118 | + 'post_id' => $current_post->ID, |
|
| 119 | + 'entity_ids' => $entity_ids, |
|
| 120 | + 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 121 | + 'div_id' => $div_id, |
|
| 122 | + 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
|
| 123 | + 'attrs' => $shortcode_atts, |
|
| 124 | + 'l10n' => array( |
|
| 125 | + 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 126 | + 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 127 | + 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 128 | + 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 129 | + ), |
|
| 130 | + ) |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 138 | + * |
|
| 139 | + * @since 3.20.0 |
|
| 140 | + * |
|
| 141 | + * @param array $atts Shortcode attributes. |
|
| 142 | + * |
|
| 143 | + * @return string Shortcode HTML for amp |
|
| 144 | + */ |
|
| 145 | + private function amp_shortcode( $atts ) { |
|
| 146 | + |
|
| 147 | + // attributes extraction and boolean filtering |
|
| 148 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 149 | + |
|
| 150 | + // If the current post is not an entity and has no related entities |
|
| 151 | + // than the shortcode cannot be rendered |
|
| 152 | + // TODO Add an alert visibile only for connected admin users. |
|
| 153 | + $current_post = get_post(); |
|
| 154 | + |
|
| 155 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 156 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 157 | + array( $current_post->ID ) : |
|
| 158 | + wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 159 | + |
|
| 160 | + // Bail if there are no entity ids. |
|
| 161 | + if ( 0 === count( $entity_ids ) ) { |
|
| 162 | + return ''; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $div_id = 'wordlift-faceted-entity-search-widget'; |
|
| 166 | + |
|
| 167 | + // Enqueue amp specific styles |
|
| 168 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 169 | + |
|
| 170 | + $wp_json_base = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE; |
|
| 171 | + |
|
| 172 | + $query_posts = array( |
|
| 173 | + 'post_id' => $current_post->ID, |
|
| 174 | + 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 175 | + 'type' => 'posts', |
|
| 176 | + ); |
|
| 177 | + |
|
| 178 | + $query_facets = array( |
|
| 179 | + 'post_id' => $current_post->ID, |
|
| 180 | + 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 181 | + 'type' => 'facets', |
|
| 182 | + 'l10n' => array( |
|
| 183 | + 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 184 | + 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 185 | + 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 186 | + 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 187 | + ), |
|
| 188 | + ); |
|
| 189 | + |
|
| 190 | + if ( strpos( $wp_json_base, 'wp-json/' . WL_REST_ROUTE_DEFAULT_NAMESPACE ) ) { |
|
| 191 | + $delimiter = '?'; |
|
| 192 | + } else { |
|
| 193 | + $delimiter = '&'; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 197 | + // This is a hackish way, but this works for http and https URLs |
|
| 198 | + $wp_json_url_posts = str_replace( array( |
|
| 199 | + 'http:', |
|
| 200 | + 'https:', |
|
| 201 | + ), '', $wp_json_base ) . '/faceted-search' . $delimiter . http_build_query( $query_posts ); |
|
| 202 | + $wp_json_url_facets = str_replace( array( |
|
| 203 | + 'http:', |
|
| 204 | + 'https:', |
|
| 205 | + ), '', $wp_json_base ) . '/faceted-search' . $delimiter . http_build_query( $query_facets ); |
|
| 206 | + |
|
| 207 | + /* |
|
| 208 | 208 | * Notes about amp code: |
| 209 | 209 | * |
| 210 | 210 | * amp-list src is the only way to do an xhr fetch so we need it for both facets and posts |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | * All JS code in amp has to be done using limited JS expressions `see https://www.ampproject.org/docs/reference/components/amp-bind#expressions` |
| 219 | 219 | */ |
| 220 | 220 | |
| 221 | - return <<<HTML |
|
| 221 | + return <<<HTML |
|
| 222 | 222 | <div id="{$div_id}" class="wl-amp-fs"> |
| 223 | 223 | <amp-state id="currentEntities"> |
| 224 | 224 | <script type="application/json"> |
@@ -307,6 +307,6 @@ discard block |
||
| 307 | 307 | </amp-list> |
| 308 | 308 | </div> |
| 309 | 309 | HTML; |
| 310 | - } |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | 312 | } |
@@ -25,10 +25,10 @@ discard block |
||
| 25 | 25 | /** |
| 26 | 26 | * {@inheritdoc} |
| 27 | 27 | */ |
| 28 | - public function render( $atts ) { |
|
| 28 | + public function render($atts) { |
|
| 29 | 29 | |
| 30 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 31 | - : $this->web_shortcode( $atts ); |
|
| 30 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode($atts) |
|
| 31 | + : $this->web_shortcode($atts); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | /** |
@@ -41,16 +41,16 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @return array $shortcode_atts |
| 43 | 43 | */ |
| 44 | - private function make_shortcode_atts( $atts ) { |
|
| 44 | + private function make_shortcode_atts($atts) { |
|
| 45 | 45 | |
| 46 | 46 | // Extract attributes and set default values. |
| 47 | - $shortcode_atts = shortcode_atts( array( |
|
| 48 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 47 | + $shortcode_atts = shortcode_atts(array( |
|
| 48 | + 'title' => __('Related articles', 'wordlift'), |
|
| 49 | 49 | 'show_facets' => true, |
| 50 | 50 | 'with_carousel' => true, |
| 51 | 51 | 'squared_thumbs' => false, |
| 52 | 52 | 'limit' => 20, |
| 53 | - ), $atts ); |
|
| 53 | + ), $atts); |
|
| 54 | 54 | |
| 55 | 55 | foreach ( |
| 56 | 56 | array( |
@@ -61,8 +61,8 @@ discard block |
||
| 61 | 61 | ) { |
| 62 | 62 | |
| 63 | 63 | // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode |
| 64 | - $shortcode_atts[ $att ] = filter_var( |
|
| 65 | - $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 64 | + $shortcode_atts[$att] = filter_var( |
|
| 65 | + $shortcode_atts[$att], FILTER_VALIDATE_BOOLEAN |
|
| 66 | 66 | ); |
| 67 | 67 | } |
| 68 | 68 | |
@@ -78,10 +78,10 @@ discard block |
||
| 78 | 78 | * |
| 79 | 79 | * @return string Shortcode HTML for web |
| 80 | 80 | */ |
| 81 | - private function web_shortcode( $atts ) { |
|
| 81 | + private function web_shortcode($atts) { |
|
| 82 | 82 | |
| 83 | 83 | // attributes extraction and boolean filtering |
| 84 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 84 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 85 | 85 | |
| 86 | 86 | // If the current post is not an entity and has no related entities |
| 87 | 87 | // than the shortcode cannot be rendered |
@@ -89,12 +89,11 @@ discard block |
||
| 89 | 89 | $current_post = get_post(); |
| 90 | 90 | |
| 91 | 91 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 92 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 93 | - array( $current_post->ID ) : |
|
| 94 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 92 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 93 | + array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID); |
|
| 95 | 94 | |
| 96 | 95 | // Bail if there are no entity ids. |
| 97 | - if ( 0 === count( $entity_ids ) ) { |
|
| 96 | + if (0 === count($entity_ids)) { |
|
| 98 | 97 | return ''; |
| 99 | 98 | } |
| 100 | 99 | |
@@ -104,33 +103,33 @@ discard block |
||
| 104 | 103 | $this->enqueue_scripts(); |
| 105 | 104 | |
| 106 | 105 | // Enqueue shortcode specific scripts and styles. |
| 107 | - $deps = apply_filters( 'wl_include_font_awesome', true ) |
|
| 108 | - ? array( 'wordlift-font-awesome' ) |
|
| 106 | + $deps = apply_filters('wl_include_font_awesome', true) |
|
| 107 | + ? array('wordlift-font-awesome') |
|
| 109 | 108 | : array(); |
| 110 | - wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css', $deps, Wordlift::get_instance()->get_version() ); |
|
| 111 | - wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.js' ); |
|
| 109 | + wp_enqueue_style('wordlift-faceted-search', dirname(plugin_dir_url(__FILE__)).'/css/wordlift-faceted-entity-search-widget.min.css', $deps, Wordlift::get_instance()->get_version()); |
|
| 110 | + wp_enqueue_script('wordlift-faceted-search', dirname(plugin_dir_url(__FILE__)).'/js/wordlift-faceted-entity-search-widget.js'); |
|
| 112 | 111 | |
| 113 | 112 | wp_localize_script( |
| 114 | 113 | 'wordlift-faceted-search', |
| 115 | 114 | 'wl_faceted_search_params', array( |
| 116 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 115 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 117 | 116 | 'action' => 'wl_faceted_search', |
| 118 | 117 | 'post_id' => $current_post->ID, |
| 119 | 118 | 'entity_ids' => $entity_ids, |
| 120 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 119 | + 'limit' => apply_filters('wl_faceted_search_limit', $shortcode_atts['limit']), |
|
| 121 | 120 | 'div_id' => $div_id, |
| 122 | 121 | 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
| 123 | 122 | 'attrs' => $shortcode_atts, |
| 124 | 123 | 'l10n' => array( |
| 125 | - 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 126 | - 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 127 | - 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 128 | - 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 124 | + 'what' => _x('What', 'Faceted Search Widget', 'wordlift'), |
|
| 125 | + 'who' => _x('Who', 'Faceted Search Widget', 'wordlift'), |
|
| 126 | + 'where' => _x('Where', 'Faceted Search Widget', 'wordlift'), |
|
| 127 | + 'when' => _x('When', 'Faceted Search Widget', 'wordlift'), |
|
| 129 | 128 | ), |
| 130 | 129 | ) |
| 131 | 130 | ); |
| 132 | 131 | |
| 133 | - return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 132 | + return '<div id="'.$div_id.'" style="width:100%"></div>'; |
|
| 134 | 133 | } |
| 135 | 134 | |
| 136 | 135 | /** |
@@ -142,10 +141,10 @@ discard block |
||
| 142 | 141 | * |
| 143 | 142 | * @return string Shortcode HTML for amp |
| 144 | 143 | */ |
| 145 | - private function amp_shortcode( $atts ) { |
|
| 144 | + private function amp_shortcode($atts) { |
|
| 146 | 145 | |
| 147 | 146 | // attributes extraction and boolean filtering |
| 148 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 147 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 149 | 148 | |
| 150 | 149 | // If the current post is not an entity and has no related entities |
| 151 | 150 | // than the shortcode cannot be rendered |
@@ -153,41 +152,40 @@ discard block |
||
| 153 | 152 | $current_post = get_post(); |
| 154 | 153 | |
| 155 | 154 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 156 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 157 | - array( $current_post->ID ) : |
|
| 158 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 155 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 156 | + array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID); |
|
| 159 | 157 | |
| 160 | 158 | // Bail if there are no entity ids. |
| 161 | - if ( 0 === count( $entity_ids ) ) { |
|
| 159 | + if (0 === count($entity_ids)) { |
|
| 162 | 160 | return ''; |
| 163 | 161 | } |
| 164 | 162 | |
| 165 | 163 | $div_id = 'wordlift-faceted-entity-search-widget'; |
| 166 | 164 | |
| 167 | 165 | // Enqueue amp specific styles |
| 168 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 166 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css'); |
|
| 169 | 167 | |
| 170 | - $wp_json_base = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE; |
|
| 168 | + $wp_json_base = get_rest_url().WL_REST_ROUTE_DEFAULT_NAMESPACE; |
|
| 171 | 169 | |
| 172 | 170 | $query_posts = array( |
| 173 | 171 | 'post_id' => $current_post->ID, |
| 174 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 172 | + 'limit' => apply_filters('wl_faceted_search_limit', $shortcode_atts['limit']), |
|
| 175 | 173 | 'type' => 'posts', |
| 176 | 174 | ); |
| 177 | 175 | |
| 178 | 176 | $query_facets = array( |
| 179 | 177 | 'post_id' => $current_post->ID, |
| 180 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 178 | + 'limit' => apply_filters('wl_faceted_search_limit', $shortcode_atts['limit']), |
|
| 181 | 179 | 'type' => 'facets', |
| 182 | 180 | 'l10n' => array( |
| 183 | - 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 184 | - 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 185 | - 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 186 | - 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 181 | + 'what' => _x('What', 'Faceted Search Widget', 'wordlift'), |
|
| 182 | + 'who' => _x('Who', 'Faceted Search Widget', 'wordlift'), |
|
| 183 | + 'where' => _x('Where', 'Faceted Search Widget', 'wordlift'), |
|
| 184 | + 'when' => _x('When', 'Faceted Search Widget', 'wordlift'), |
|
| 187 | 185 | ), |
| 188 | 186 | ); |
| 189 | 187 | |
| 190 | - if ( strpos( $wp_json_base, 'wp-json/' . WL_REST_ROUTE_DEFAULT_NAMESPACE ) ) { |
|
| 188 | + if (strpos($wp_json_base, 'wp-json/'.WL_REST_ROUTE_DEFAULT_NAMESPACE)) { |
|
| 191 | 189 | $delimiter = '?'; |
| 192 | 190 | } else { |
| 193 | 191 | $delimiter = '&'; |
@@ -195,14 +193,14 @@ discard block |
||
| 195 | 193 | |
| 196 | 194 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 197 | 195 | // This is a hackish way, but this works for http and https URLs |
| 198 | - $wp_json_url_posts = str_replace( array( |
|
| 196 | + $wp_json_url_posts = str_replace(array( |
|
| 199 | 197 | 'http:', |
| 200 | 198 | 'https:', |
| 201 | - ), '', $wp_json_base ) . '/faceted-search' . $delimiter . http_build_query( $query_posts ); |
|
| 202 | - $wp_json_url_facets = str_replace( array( |
|
| 199 | + ), '', $wp_json_base).'/faceted-search'.$delimiter.http_build_query($query_posts); |
|
| 200 | + $wp_json_url_facets = str_replace(array( |
|
| 203 | 201 | 'http:', |
| 204 | 202 | 'https:', |
| 205 | - ), '', $wp_json_base ) . '/faceted-search' . $delimiter . http_build_query( $query_facets ); |
|
| 203 | + ), '', $wp_json_base).'/faceted-search'.$delimiter.http_build_query($query_facets); |
|
| 206 | 204 | |
| 207 | 205 | /* |
| 208 | 206 | * Notes about amp code: |
@@ -17,137 +17,137 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Navigator_Shortcode extends Wordlift_Shortcode { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - const SHORTCODE = 'wl_navigator'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - public function render( $atts ) { |
|
| 29 | - |
|
| 30 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 31 | - : $this->web_shortcode( $atts ); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 36 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 37 | - * |
|
| 38 | - * @since 3.20.0 |
|
| 39 | - * |
|
| 40 | - * @param array $atts Shortcode attributes. |
|
| 41 | - * |
|
| 42 | - * @return array $shortcode_atts |
|
| 43 | - */ |
|
| 44 | - private function make_shortcode_atts( $atts ) { |
|
| 45 | - |
|
| 46 | - // Extract attributes and set default values. |
|
| 47 | - $shortcode_atts = shortcode_atts( array( |
|
| 48 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 49 | - 'limit' => 4, |
|
| 50 | - 'offset' => 0, |
|
| 51 | - 'template_id' => '', |
|
| 52 | - 'post_id' => '', |
|
| 53 | - 'uniqid' => uniqid( 'wl-navigator-widget-' ) |
|
| 54 | - ), $atts ); |
|
| 55 | - |
|
| 56 | - return $shortcode_atts; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Function in charge of diplaying the [wl-navigator] in web mode. |
|
| 61 | - * |
|
| 62 | - * @since 3.20.0 |
|
| 63 | - * |
|
| 64 | - * @param array $atts Shortcode attributes. |
|
| 65 | - * |
|
| 66 | - * @return string Shortcode HTML for web |
|
| 67 | - */ |
|
| 68 | - private function web_shortcode( $atts ) { |
|
| 69 | - |
|
| 70 | - // attributes extraction and boolean filtering |
|
| 71 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 72 | - |
|
| 73 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 74 | - if ( empty($shortcode_atts['post_id']) && !is_singular() ) { |
|
| 75 | - return; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $post = !empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 79 | - $navigator_id = $shortcode_atts['uniqid']; |
|
| 80 | - $rest_url = $post ? admin_url( sprintf('admin-ajax.php?action=wl_navigator&uniqid=%s&post_id=%s&limit=%s&offset=%s', $navigator_id, $post->ID, $shortcode_atts['limit'], $shortcode_atts['offset']) ) : false; |
|
| 81 | - |
|
| 82 | - // avoid building the widget when no valid $rest_url |
|
| 83 | - if ( !$rest_url ) { |
|
| 84 | - return; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 88 | - wp_add_inline_script( 'wordlift-cloud', "wordliftCloud.navigator('#".$navigator_id."')" ); |
|
| 89 | - |
|
| 90 | - return sprintf( |
|
| 91 | - '<div id="%s" class="%s" data-rest-url="%s" data-title="%s" data-template-id="%s" data-limit="%s"></div>', |
|
| 92 | - $navigator_id, |
|
| 93 | - 'wl-navigator', |
|
| 94 | - $rest_url, |
|
| 95 | - $shortcode_atts['title'], |
|
| 96 | - $shortcode_atts['template_id'], |
|
| 97 | - $shortcode_atts['limit'] |
|
| 98 | - ); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 103 | - * |
|
| 104 | - * @since 3.20.0 |
|
| 105 | - * |
|
| 106 | - * @param array $atts Shortcode attributes. |
|
| 107 | - * |
|
| 108 | - * @return string Shortcode HTML for amp |
|
| 109 | - */ |
|
| 110 | - private function amp_shortcode( $atts ) { |
|
| 111 | - |
|
| 112 | - // attributes extraction and boolean filtering |
|
| 113 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 114 | - |
|
| 115 | - // avoid building the widget when there is a list of posts. |
|
| 116 | - if ( ! is_singular() ) { |
|
| 117 | - return ''; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $current_post = get_post(); |
|
| 121 | - |
|
| 122 | - // Enqueue amp specific styles |
|
| 123 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 124 | - |
|
| 125 | - $post = !empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 126 | - $navigator_id = $shortcode_atts['uniqid']; |
|
| 127 | - |
|
| 128 | - $wp_json_base = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE; |
|
| 129 | - |
|
| 130 | - $navigator_query = array( |
|
| 131 | - 'uniqid' => $navigator_id, |
|
| 132 | - 'post_id' => $post->ID, |
|
| 133 | - 'limit' => $shortcode_atts['limit'], |
|
| 134 | - 'offset' => $shortcode_atts['offset'] |
|
| 135 | - ); |
|
| 136 | - |
|
| 137 | - if ( strpos( $wp_json_base, 'wp-json/' . WL_REST_ROUTE_DEFAULT_NAMESPACE ) ) { |
|
| 138 | - $delimiter = '?'; |
|
| 139 | - } else { |
|
| 140 | - $delimiter = '&'; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 144 | - // This is a hackish way, but this works for http and https URLs |
|
| 145 | - $wp_json_url_posts = str_replace( array( |
|
| 146 | - 'http:', |
|
| 147 | - 'https:', |
|
| 148 | - ), '', $wp_json_base ) . '/navigator' . $delimiter . http_build_query( $navigator_query ); |
|
| 149 | - |
|
| 150 | - return <<<HTML |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + const SHORTCODE = 'wl_navigator'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + public function render( $atts ) { |
|
| 29 | + |
|
| 30 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 31 | + : $this->web_shortcode( $atts ); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 36 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 37 | + * |
|
| 38 | + * @since 3.20.0 |
|
| 39 | + * |
|
| 40 | + * @param array $atts Shortcode attributes. |
|
| 41 | + * |
|
| 42 | + * @return array $shortcode_atts |
|
| 43 | + */ |
|
| 44 | + private function make_shortcode_atts( $atts ) { |
|
| 45 | + |
|
| 46 | + // Extract attributes and set default values. |
|
| 47 | + $shortcode_atts = shortcode_atts( array( |
|
| 48 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 49 | + 'limit' => 4, |
|
| 50 | + 'offset' => 0, |
|
| 51 | + 'template_id' => '', |
|
| 52 | + 'post_id' => '', |
|
| 53 | + 'uniqid' => uniqid( 'wl-navigator-widget-' ) |
|
| 54 | + ), $atts ); |
|
| 55 | + |
|
| 56 | + return $shortcode_atts; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Function in charge of diplaying the [wl-navigator] in web mode. |
|
| 61 | + * |
|
| 62 | + * @since 3.20.0 |
|
| 63 | + * |
|
| 64 | + * @param array $atts Shortcode attributes. |
|
| 65 | + * |
|
| 66 | + * @return string Shortcode HTML for web |
|
| 67 | + */ |
|
| 68 | + private function web_shortcode( $atts ) { |
|
| 69 | + |
|
| 70 | + // attributes extraction and boolean filtering |
|
| 71 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 72 | + |
|
| 73 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 74 | + if ( empty($shortcode_atts['post_id']) && !is_singular() ) { |
|
| 75 | + return; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $post = !empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 79 | + $navigator_id = $shortcode_atts['uniqid']; |
|
| 80 | + $rest_url = $post ? admin_url( sprintf('admin-ajax.php?action=wl_navigator&uniqid=%s&post_id=%s&limit=%s&offset=%s', $navigator_id, $post->ID, $shortcode_atts['limit'], $shortcode_atts['offset']) ) : false; |
|
| 81 | + |
|
| 82 | + // avoid building the widget when no valid $rest_url |
|
| 83 | + if ( !$rest_url ) { |
|
| 84 | + return; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 88 | + wp_add_inline_script( 'wordlift-cloud', "wordliftCloud.navigator('#".$navigator_id."')" ); |
|
| 89 | + |
|
| 90 | + return sprintf( |
|
| 91 | + '<div id="%s" class="%s" data-rest-url="%s" data-title="%s" data-template-id="%s" data-limit="%s"></div>', |
|
| 92 | + $navigator_id, |
|
| 93 | + 'wl-navigator', |
|
| 94 | + $rest_url, |
|
| 95 | + $shortcode_atts['title'], |
|
| 96 | + $shortcode_atts['template_id'], |
|
| 97 | + $shortcode_atts['limit'] |
|
| 98 | + ); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 103 | + * |
|
| 104 | + * @since 3.20.0 |
|
| 105 | + * |
|
| 106 | + * @param array $atts Shortcode attributes. |
|
| 107 | + * |
|
| 108 | + * @return string Shortcode HTML for amp |
|
| 109 | + */ |
|
| 110 | + private function amp_shortcode( $atts ) { |
|
| 111 | + |
|
| 112 | + // attributes extraction and boolean filtering |
|
| 113 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 114 | + |
|
| 115 | + // avoid building the widget when there is a list of posts. |
|
| 116 | + if ( ! is_singular() ) { |
|
| 117 | + return ''; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $current_post = get_post(); |
|
| 121 | + |
|
| 122 | + // Enqueue amp specific styles |
|
| 123 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 124 | + |
|
| 125 | + $post = !empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 126 | + $navigator_id = $shortcode_atts['uniqid']; |
|
| 127 | + |
|
| 128 | + $wp_json_base = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE; |
|
| 129 | + |
|
| 130 | + $navigator_query = array( |
|
| 131 | + 'uniqid' => $navigator_id, |
|
| 132 | + 'post_id' => $post->ID, |
|
| 133 | + 'limit' => $shortcode_atts['limit'], |
|
| 134 | + 'offset' => $shortcode_atts['offset'] |
|
| 135 | + ); |
|
| 136 | + |
|
| 137 | + if ( strpos( $wp_json_base, 'wp-json/' . WL_REST_ROUTE_DEFAULT_NAMESPACE ) ) { |
|
| 138 | + $delimiter = '?'; |
|
| 139 | + } else { |
|
| 140 | + $delimiter = '&'; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 144 | + // This is a hackish way, but this works for http and https URLs |
|
| 145 | + $wp_json_url_posts = str_replace( array( |
|
| 146 | + 'http:', |
|
| 147 | + 'https:', |
|
| 148 | + ), '', $wp_json_base ) . '/navigator' . $delimiter . http_build_query( $navigator_query ); |
|
| 149 | + |
|
| 150 | + return <<<HTML |
|
| 151 | 151 | <div id="{$navigator_id}" class="wl-navigator-widget"> |
| 152 | 152 | <h3 class="wl-headline">{$shortcode_atts['title']}</h3> |
| 153 | 153 | <amp-list |
@@ -204,6 +204,6 @@ discard block |
||
| 204 | 204 | </amp-list> |
| 205 | 205 | </div> |
| 206 | 206 | HTML; |
| 207 | - } |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | 209 | } |
@@ -25,10 +25,10 @@ discard block |
||
| 25 | 25 | /** |
| 26 | 26 | * {@inheritdoc} |
| 27 | 27 | */ |
| 28 | - public function render( $atts ) { |
|
| 28 | + public function render($atts) { |
|
| 29 | 29 | |
| 30 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 31 | - : $this->web_shortcode( $atts ); |
|
| 30 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode($atts) |
|
| 31 | + : $this->web_shortcode($atts); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | /** |
@@ -41,17 +41,17 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @return array $shortcode_atts |
| 43 | 43 | */ |
| 44 | - private function make_shortcode_atts( $atts ) { |
|
| 44 | + private function make_shortcode_atts($atts) { |
|
| 45 | 45 | |
| 46 | 46 | // Extract attributes and set default values. |
| 47 | - $shortcode_atts = shortcode_atts( array( |
|
| 48 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 47 | + $shortcode_atts = shortcode_atts(array( |
|
| 48 | + 'title' => __('Related articles', 'wordlift'), |
|
| 49 | 49 | 'limit' => 4, |
| 50 | 50 | 'offset' => 0, |
| 51 | 51 | 'template_id' => '', |
| 52 | 52 | 'post_id' => '', |
| 53 | - 'uniqid' => uniqid( 'wl-navigator-widget-' ) |
|
| 54 | - ), $atts ); |
|
| 53 | + 'uniqid' => uniqid('wl-navigator-widget-') |
|
| 54 | + ), $atts); |
|
| 55 | 55 | |
| 56 | 56 | return $shortcode_atts; |
| 57 | 57 | } |
@@ -65,27 +65,27 @@ discard block |
||
| 65 | 65 | * |
| 66 | 66 | * @return string Shortcode HTML for web |
| 67 | 67 | */ |
| 68 | - private function web_shortcode( $atts ) { |
|
| 68 | + private function web_shortcode($atts) { |
|
| 69 | 69 | |
| 70 | 70 | // attributes extraction and boolean filtering |
| 71 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 71 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 72 | 72 | |
| 73 | 73 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 74 | - if ( empty($shortcode_atts['post_id']) && !is_singular() ) { |
|
| 74 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - $post = !empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 78 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 79 | 79 | $navigator_id = $shortcode_atts['uniqid']; |
| 80 | - $rest_url = $post ? admin_url( sprintf('admin-ajax.php?action=wl_navigator&uniqid=%s&post_id=%s&limit=%s&offset=%s', $navigator_id, $post->ID, $shortcode_atts['limit'], $shortcode_atts['offset']) ) : false; |
|
| 80 | + $rest_url = $post ? admin_url(sprintf('admin-ajax.php?action=wl_navigator&uniqid=%s&post_id=%s&limit=%s&offset=%s', $navigator_id, $post->ID, $shortcode_atts['limit'], $shortcode_atts['offset'])) : false; |
|
| 81 | 81 | |
| 82 | 82 | // avoid building the widget when no valid $rest_url |
| 83 | - if ( !$rest_url ) { |
|
| 83 | + if ( ! $rest_url) { |
|
| 84 | 84 | return; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 88 | - wp_add_inline_script( 'wordlift-cloud', "wordliftCloud.navigator('#".$navigator_id."')" ); |
|
| 87 | + wp_enqueue_script('wordlift-cloud'); |
|
| 88 | + wp_add_inline_script('wordlift-cloud', "wordliftCloud.navigator('#".$navigator_id."')"); |
|
| 89 | 89 | |
| 90 | 90 | return sprintf( |
| 91 | 91 | '<div id="%s" class="%s" data-rest-url="%s" data-title="%s" data-template-id="%s" data-limit="%s"></div>', |
@@ -107,25 +107,25 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return string Shortcode HTML for amp |
| 109 | 109 | */ |
| 110 | - private function amp_shortcode( $atts ) { |
|
| 110 | + private function amp_shortcode($atts) { |
|
| 111 | 111 | |
| 112 | 112 | // attributes extraction and boolean filtering |
| 113 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 113 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 114 | 114 | |
| 115 | 115 | // avoid building the widget when there is a list of posts. |
| 116 | - if ( ! is_singular() ) { |
|
| 116 | + if ( ! is_singular()) { |
|
| 117 | 117 | return ''; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | $current_post = get_post(); |
| 121 | 121 | |
| 122 | 122 | // Enqueue amp specific styles |
| 123 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 123 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css'); |
|
| 124 | 124 | |
| 125 | - $post = !empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 125 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 126 | 126 | $navigator_id = $shortcode_atts['uniqid']; |
| 127 | 127 | |
| 128 | - $wp_json_base = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE; |
|
| 128 | + $wp_json_base = get_rest_url().WL_REST_ROUTE_DEFAULT_NAMESPACE; |
|
| 129 | 129 | |
| 130 | 130 | $navigator_query = array( |
| 131 | 131 | 'uniqid' => $navigator_id, |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | 'offset' => $shortcode_atts['offset'] |
| 135 | 135 | ); |
| 136 | 136 | |
| 137 | - if ( strpos( $wp_json_base, 'wp-json/' . WL_REST_ROUTE_DEFAULT_NAMESPACE ) ) { |
|
| 137 | + if (strpos($wp_json_base, 'wp-json/'.WL_REST_ROUTE_DEFAULT_NAMESPACE)) { |
|
| 138 | 138 | $delimiter = '?'; |
| 139 | 139 | } else { |
| 140 | 140 | $delimiter = '&'; |
@@ -142,10 +142,10 @@ discard block |
||
| 142 | 142 | |
| 143 | 143 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 144 | 144 | // This is a hackish way, but this works for http and https URLs |
| 145 | - $wp_json_url_posts = str_replace( array( |
|
| 145 | + $wp_json_url_posts = str_replace(array( |
|
| 146 | 146 | 'http:', |
| 147 | 147 | 'https:', |
| 148 | - ), '', $wp_json_base ) . '/navigator' . $delimiter . http_build_query( $navigator_query ); |
|
| 148 | + ), '', $wp_json_base).'/navigator'.$delimiter.http_build_query($navigator_query); |
|
| 149 | 149 | |
| 150 | 150 | return <<<HTML |
| 151 | 151 | <div id="{$navigator_id}" class="wl-navigator-widget"> |