@@ -18,224 +18,224 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Term_JsonLd_Adapter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.20.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private $entity_uri_service; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The {@link Wordlift_Jsonld_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.20.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $jsonld_service; |
|
| 38 | - |
|
| 39 | - private static $instance; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 43 | - * |
|
| 44 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 45 | - * @param \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
| 46 | - * |
|
| 47 | - * @since 3.20.0 |
|
| 48 | - * |
|
| 49 | - */ |
|
| 50 | - public function __construct( $entity_uri_service, $jsonld_service ) { |
|
| 51 | - |
|
| 52 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 53 | - |
|
| 54 | - $this->entity_uri_service = $entity_uri_service; |
|
| 55 | - $this->jsonld_service = $jsonld_service; |
|
| 56 | - |
|
| 57 | - self::$instance = $this; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public static function get_instance() { |
|
| 61 | - |
|
| 62 | - return self::$instance; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Adds carousel json ld data to term page if the conditions match |
|
| 67 | - * |
|
| 68 | - * @return array|boolean |
|
| 69 | - */ |
|
| 70 | - public function get_carousel_jsonld( $id = null ) { |
|
| 71 | - $posts = $this->get_posts( $id ); |
|
| 72 | - $post_jsonld = array(); |
|
| 73 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 74 | - // Bail out if no posts are present. |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if ( ! is_null( $id ) ) { |
|
| 79 | - $term = get_term( $id ); |
|
| 80 | - $post_jsonld['description'] = strip_tags( strip_shortcodes( $term->description ) ); |
|
| 81 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 82 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 83 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // More than 2 items are present, so construct the post_jsonld data |
|
| 88 | - $post_jsonld['@context'] = 'https://schema.org'; |
|
| 89 | - $post_jsonld['@type'] = 'ItemList'; |
|
| 90 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 91 | - $post_jsonld['itemListElement'] = array(); |
|
| 92 | - $position = 1; |
|
| 93 | - |
|
| 94 | - foreach ( $posts as $post_id ) { |
|
| 95 | - $result = array( |
|
| 96 | - '@type' => 'ListItem', |
|
| 97 | - 'position' => $position, |
|
| 98 | - /** |
|
| 99 | - * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 100 | - * |
|
| 101 | - * See https://developers.google.com/search/docs/data-types/carousel |
|
| 102 | - */ |
|
| 103 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 104 | - ); |
|
| 105 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 106 | - $position += 1; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return $post_jsonld; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - private function get_posts( $id ) { |
|
| 113 | - global $wp_query; |
|
| 114 | - |
|
| 115 | - if ( ! is_null( $wp_query->posts ) ) { |
|
| 116 | - return array_map( function ( $post ) { |
|
| 117 | - return $post->ID; |
|
| 118 | - }, $wp_query->posts ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if ( is_null( $id ) ) { |
|
| 122 | - return null; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $term = get_term( $id ); |
|
| 126 | - |
|
| 127 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Hook to `wp_head` to print the JSON-LD. |
|
| 132 | - * |
|
| 133 | - * @since 3.20.0 |
|
| 134 | - */ |
|
| 135 | - public function wp_head() { |
|
| 136 | - $query_object = get_queried_object(); |
|
| 137 | - |
|
| 138 | - // Check if it is a term page. |
|
| 139 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 144 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 145 | - return; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $term_id = $query_object->term_id; |
|
| 149 | - |
|
| 150 | - $jsonld = $this->get( $term_id ); |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - // Bail out if the JSON-LD is empty. |
|
| 154 | - if ( empty( $jsonld ) ) { |
|
| 155 | - return; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 159 | - |
|
| 160 | - echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
|
| 161 | - |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - public function get( $id ) { |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Support for carousel rich snippet, get jsonld data present |
|
| 168 | - * for all the posts shown in the term page, and add the jsonld data |
|
| 169 | - * to list |
|
| 170 | - * |
|
| 171 | - * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 172 | - * |
|
| 173 | - * @since 3.26.0 |
|
| 174 | - */ |
|
| 175 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 176 | - $jsonld_array = array(); |
|
| 177 | - if ( $carousel_data ) { |
|
| 178 | - $jsonld_array[] = $carousel_data; |
|
| 179 | - } |
|
| 180 | - $context = empty( $carousel_data ) ? Jsonld_Context_Enum::PAGE : Jsonld_Context_Enum::CAROUSEL; |
|
| 181 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 182 | - |
|
| 183 | - $result = array( |
|
| 184 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 185 | - 'references' => array() |
|
| 186 | - ); |
|
| 187 | - /** |
|
| 188 | - * @since 3.26.3 |
|
| 189 | - * Filter: wl_term_jsonld_array |
|
| 190 | - * @var $id int Term id |
|
| 191 | - * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 192 | - */ |
|
| 193 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 194 | - return $arr['jsonld']; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - private function get_term_url( $id ) { |
|
| 198 | - |
|
| 199 | - if ( is_null( $id ) ) { |
|
| 200 | - return $_SERVER['REQUEST_URI']; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 204 | - if ( ! empty( $maybe_url ) ) { |
|
| 205 | - return $maybe_url; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - return get_term_link( $id ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Return jsonld for entities bound to terms. |
|
| 213 | - * |
|
| 214 | - * @param int $term_id Term ID. |
|
| 215 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 216 | - * |
|
| 217 | - * @return array |
|
| 218 | - */ |
|
| 219 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 220 | - // The `_wl_entity_id` are URIs. |
|
| 221 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 222 | - $entity_uri_service = $this->entity_uri_service; |
|
| 223 | - |
|
| 224 | - $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 225 | - return $entity_uri_service->is_internal( $uri ); |
|
| 226 | - } ); |
|
| 227 | - |
|
| 228 | - // Bail out if there are no entities. |
|
| 229 | - if ( empty( $local_entity_ids ) ) { |
|
| 230 | - return array(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 234 | - $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID, $context ); |
|
| 235 | - // Reset the `url` to the term page. |
|
| 236 | - $jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 237 | - |
|
| 238 | - return $jsonld; |
|
| 239 | - } |
|
| 21 | + /** |
|
| 22 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.20.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private $entity_uri_service; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The {@link Wordlift_Jsonld_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.20.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $jsonld_service; |
|
| 38 | + |
|
| 39 | + private static $instance; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 43 | + * |
|
| 44 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 45 | + * @param \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
| 46 | + * |
|
| 47 | + * @since 3.20.0 |
|
| 48 | + * |
|
| 49 | + */ |
|
| 50 | + public function __construct( $entity_uri_service, $jsonld_service ) { |
|
| 51 | + |
|
| 52 | + add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 53 | + |
|
| 54 | + $this->entity_uri_service = $entity_uri_service; |
|
| 55 | + $this->jsonld_service = $jsonld_service; |
|
| 56 | + |
|
| 57 | + self::$instance = $this; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public static function get_instance() { |
|
| 61 | + |
|
| 62 | + return self::$instance; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Adds carousel json ld data to term page if the conditions match |
|
| 67 | + * |
|
| 68 | + * @return array|boolean |
|
| 69 | + */ |
|
| 70 | + public function get_carousel_jsonld( $id = null ) { |
|
| 71 | + $posts = $this->get_posts( $id ); |
|
| 72 | + $post_jsonld = array(); |
|
| 73 | + if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 74 | + // Bail out if no posts are present. |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if ( ! is_null( $id ) ) { |
|
| 79 | + $term = get_term( $id ); |
|
| 80 | + $post_jsonld['description'] = strip_tags( strip_shortcodes( $term->description ) ); |
|
| 81 | + $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 82 | + if ( ! empty( $thumbnail_id ) ) { |
|
| 83 | + $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // More than 2 items are present, so construct the post_jsonld data |
|
| 88 | + $post_jsonld['@context'] = 'https://schema.org'; |
|
| 89 | + $post_jsonld['@type'] = 'ItemList'; |
|
| 90 | + $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 91 | + $post_jsonld['itemListElement'] = array(); |
|
| 92 | + $position = 1; |
|
| 93 | + |
|
| 94 | + foreach ( $posts as $post_id ) { |
|
| 95 | + $result = array( |
|
| 96 | + '@type' => 'ListItem', |
|
| 97 | + 'position' => $position, |
|
| 98 | + /** |
|
| 99 | + * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 100 | + * |
|
| 101 | + * See https://developers.google.com/search/docs/data-types/carousel |
|
| 102 | + */ |
|
| 103 | + 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 104 | + ); |
|
| 105 | + array_push( $post_jsonld['itemListElement'], $result ); |
|
| 106 | + $position += 1; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return $post_jsonld; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + private function get_posts( $id ) { |
|
| 113 | + global $wp_query; |
|
| 114 | + |
|
| 115 | + if ( ! is_null( $wp_query->posts ) ) { |
|
| 116 | + return array_map( function ( $post ) { |
|
| 117 | + return $post->ID; |
|
| 118 | + }, $wp_query->posts ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if ( is_null( $id ) ) { |
|
| 122 | + return null; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $term = get_term( $id ); |
|
| 126 | + |
|
| 127 | + return get_objects_in_term( $id, $term->taxonomy ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Hook to `wp_head` to print the JSON-LD. |
|
| 132 | + * |
|
| 133 | + * @since 3.20.0 |
|
| 134 | + */ |
|
| 135 | + public function wp_head() { |
|
| 136 | + $query_object = get_queried_object(); |
|
| 137 | + |
|
| 138 | + // Check if it is a term page. |
|
| 139 | + if ( ! $query_object instanceof WP_Term ) { |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 144 | + if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 145 | + return; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $term_id = $query_object->term_id; |
|
| 149 | + |
|
| 150 | + $jsonld = $this->get( $term_id ); |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + // Bail out if the JSON-LD is empty. |
|
| 154 | + if ( empty( $jsonld ) ) { |
|
| 155 | + return; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $jsonld_string = wp_json_encode( $jsonld ); |
|
| 159 | + |
|
| 160 | + echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
|
| 161 | + |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + public function get( $id ) { |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Support for carousel rich snippet, get jsonld data present |
|
| 168 | + * for all the posts shown in the term page, and add the jsonld data |
|
| 169 | + * to list |
|
| 170 | + * |
|
| 171 | + * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 172 | + * |
|
| 173 | + * @since 3.26.0 |
|
| 174 | + */ |
|
| 175 | + $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 176 | + $jsonld_array = array(); |
|
| 177 | + if ( $carousel_data ) { |
|
| 178 | + $jsonld_array[] = $carousel_data; |
|
| 179 | + } |
|
| 180 | + $context = empty( $carousel_data ) ? Jsonld_Context_Enum::PAGE : Jsonld_Context_Enum::CAROUSEL; |
|
| 181 | + $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 182 | + |
|
| 183 | + $result = array( |
|
| 184 | + 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 185 | + 'references' => array() |
|
| 186 | + ); |
|
| 187 | + /** |
|
| 188 | + * @since 3.26.3 |
|
| 189 | + * Filter: wl_term_jsonld_array |
|
| 190 | + * @var $id int Term id |
|
| 191 | + * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 192 | + */ |
|
| 193 | + $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 194 | + return $arr['jsonld']; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + private function get_term_url( $id ) { |
|
| 198 | + |
|
| 199 | + if ( is_null( $id ) ) { |
|
| 200 | + return $_SERVER['REQUEST_URI']; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 204 | + if ( ! empty( $maybe_url ) ) { |
|
| 205 | + return $maybe_url; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + return get_term_link( $id ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Return jsonld for entities bound to terms. |
|
| 213 | + * |
|
| 214 | + * @param int $term_id Term ID. |
|
| 215 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 216 | + * |
|
| 217 | + * @return array |
|
| 218 | + */ |
|
| 219 | + private function get_entity_jsonld( $term_id, $context ) { |
|
| 220 | + // The `_wl_entity_id` are URIs. |
|
| 221 | + $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 222 | + $entity_uri_service = $this->entity_uri_service; |
|
| 223 | + |
|
| 224 | + $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 225 | + return $entity_uri_service->is_internal( $uri ); |
|
| 226 | + } ); |
|
| 227 | + |
|
| 228 | + // Bail out if there are no entities. |
|
| 229 | + if ( empty( $local_entity_ids ) ) { |
|
| 230 | + return array(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 234 | + $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID, $context ); |
|
| 235 | + // Reset the `url` to the term page. |
|
| 236 | + $jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 237 | + |
|
| 238 | + return $jsonld; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | 241 | } |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | * @since 3.20.0 |
| 48 | 48 | * |
| 49 | 49 | */ |
| 50 | - public function __construct( $entity_uri_service, $jsonld_service ) { |
|
| 50 | + public function __construct($entity_uri_service, $jsonld_service) { |
|
| 51 | 51 | |
| 52 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 52 | + add_action('wp_head', array($this, 'wp_head')); |
|
| 53 | 53 | |
| 54 | 54 | $this->entity_uri_service = $entity_uri_service; |
| 55 | 55 | $this->jsonld_service = $jsonld_service; |
@@ -67,31 +67,31 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return array|boolean |
| 69 | 69 | */ |
| 70 | - public function get_carousel_jsonld( $id = null ) { |
|
| 71 | - $posts = $this->get_posts( $id ); |
|
| 70 | + public function get_carousel_jsonld($id = null) { |
|
| 71 | + $posts = $this->get_posts($id); |
|
| 72 | 72 | $post_jsonld = array(); |
| 73 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 73 | + if ( ! is_array($posts) || count($posts) < 2) { |
|
| 74 | 74 | // Bail out if no posts are present. |
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if ( ! is_null( $id ) ) { |
|
| 79 | - $term = get_term( $id ); |
|
| 80 | - $post_jsonld['description'] = strip_tags( strip_shortcodes( $term->description ) ); |
|
| 81 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 82 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 83 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 78 | + if ( ! is_null($id)) { |
|
| 79 | + $term = get_term($id); |
|
| 80 | + $post_jsonld['description'] = strip_tags(strip_shortcodes($term->description)); |
|
| 81 | + $thumbnail_id = get_term_meta($id, 'thumbnail_id', true); |
|
| 82 | + if ( ! empty($thumbnail_id)) { |
|
| 83 | + $post_jsonld['image'] = wp_get_attachment_url($thumbnail_id); |
|
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // More than 2 items are present, so construct the post_jsonld data |
| 88 | 88 | $post_jsonld['@context'] = 'https://schema.org'; |
| 89 | 89 | $post_jsonld['@type'] = 'ItemList'; |
| 90 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 90 | + $post_jsonld['url'] = $this->get_term_url($id); |
|
| 91 | 91 | $post_jsonld['itemListElement'] = array(); |
| 92 | 92 | $position = 1; |
| 93 | 93 | |
| 94 | - foreach ( $posts as $post_id ) { |
|
| 94 | + foreach ($posts as $post_id) { |
|
| 95 | 95 | $result = array( |
| 96 | 96 | '@type' => 'ListItem', |
| 97 | 97 | 'position' => $position, |
@@ -100,31 +100,31 @@ discard block |
||
| 100 | 100 | * |
| 101 | 101 | * See https://developers.google.com/search/docs/data-types/carousel |
| 102 | 102 | */ |
| 103 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 103 | + 'url' => apply_filters('wl_carousel_post_list_item_url', get_permalink($post_id), $post_id), |
|
| 104 | 104 | ); |
| 105 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 105 | + array_push($post_jsonld['itemListElement'], $result); |
|
| 106 | 106 | $position += 1; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | return $post_jsonld; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | - private function get_posts( $id ) { |
|
| 112 | + private function get_posts($id) { |
|
| 113 | 113 | global $wp_query; |
| 114 | 114 | |
| 115 | - if ( ! is_null( $wp_query->posts ) ) { |
|
| 116 | - return array_map( function ( $post ) { |
|
| 115 | + if ( ! is_null($wp_query->posts)) { |
|
| 116 | + return array_map(function($post) { |
|
| 117 | 117 | return $post->ID; |
| 118 | - }, $wp_query->posts ); |
|
| 118 | + }, $wp_query->posts); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - if ( is_null( $id ) ) { |
|
| 121 | + if (is_null($id)) { |
|
| 122 | 122 | return null; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - $term = get_term( $id ); |
|
| 125 | + $term = get_term($id); |
|
| 126 | 126 | |
| 127 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 127 | + return get_objects_in_term($id, $term->taxonomy); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -136,32 +136,32 @@ discard block |
||
| 136 | 136 | $query_object = get_queried_object(); |
| 137 | 137 | |
| 138 | 138 | // Check if it is a term page. |
| 139 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 139 | + if ( ! $query_object instanceof WP_Term) { |
|
| 140 | 140 | return; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | // Bail out if `wl_jsonld_enabled` isn't enabled. |
| 144 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 144 | + if ( ! apply_filters('wl_jsonld_enabled', true)) { |
|
| 145 | 145 | return; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | $term_id = $query_object->term_id; |
| 149 | 149 | |
| 150 | - $jsonld = $this->get( $term_id ); |
|
| 150 | + $jsonld = $this->get($term_id); |
|
| 151 | 151 | |
| 152 | 152 | |
| 153 | 153 | // Bail out if the JSON-LD is empty. |
| 154 | - if ( empty( $jsonld ) ) { |
|
| 154 | + if (empty($jsonld)) { |
|
| 155 | 155 | return; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 158 | + $jsonld_string = wp_json_encode($jsonld); |
|
| 159 | 159 | |
| 160 | 160 | echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
| 161 | 161 | |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | - public function get( $id ) { |
|
| 164 | + public function get($id) { |
|
| 165 | 165 | |
| 166 | 166 | /** |
| 167 | 167 | * Support for carousel rich snippet, get jsonld data present |
@@ -172,16 +172,16 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @since 3.26.0 |
| 174 | 174 | */ |
| 175 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 175 | + $carousel_data = $this->get_carousel_jsonld($id); |
|
| 176 | 176 | $jsonld_array = array(); |
| 177 | - if ( $carousel_data ) { |
|
| 177 | + if ($carousel_data) { |
|
| 178 | 178 | $jsonld_array[] = $carousel_data; |
| 179 | 179 | } |
| 180 | - $context = empty( $carousel_data ) ? Jsonld_Context_Enum::PAGE : Jsonld_Context_Enum::CAROUSEL; |
|
| 181 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 180 | + $context = empty($carousel_data) ? Jsonld_Context_Enum::PAGE : Jsonld_Context_Enum::CAROUSEL; |
|
| 181 | + $entities_jsonld_array = $this->get_entity_jsonld($id, $context); |
|
| 182 | 182 | |
| 183 | 183 | $result = array( |
| 184 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 184 | + 'jsonld' => array_merge($jsonld_array, $entities_jsonld_array), |
|
| 185 | 185 | 'references' => array() |
| 186 | 186 | ); |
| 187 | 187 | /** |
@@ -190,22 +190,22 @@ discard block |
||
| 190 | 190 | * @var $id int Term id |
| 191 | 191 | * @var $jsonld_array array An array containing jsonld for term and entities. |
| 192 | 192 | */ |
| 193 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 193 | + $arr = apply_filters('wl_term_jsonld_array', $result, $id); |
|
| 194 | 194 | return $arr['jsonld']; |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - private function get_term_url( $id ) { |
|
| 197 | + private function get_term_url($id) { |
|
| 198 | 198 | |
| 199 | - if ( is_null( $id ) ) { |
|
| 199 | + if (is_null($id)) { |
|
| 200 | 200 | return $_SERVER['REQUEST_URI']; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 204 | - if ( ! empty( $maybe_url ) ) { |
|
| 203 | + $maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true); |
|
| 204 | + if ( ! empty($maybe_url)) { |
|
| 205 | 205 | return $maybe_url; |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | - return get_term_link( $id ); |
|
| 208 | + return get_term_link($id); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | /** |
@@ -216,24 +216,24 @@ discard block |
||
| 216 | 216 | * |
| 217 | 217 | * @return array |
| 218 | 218 | */ |
| 219 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 219 | + private function get_entity_jsonld($term_id, $context) { |
|
| 220 | 220 | // The `_wl_entity_id` are URIs. |
| 221 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 221 | + $entity_ids = get_term_meta($term_id, '_wl_entity_id'); |
|
| 222 | 222 | $entity_uri_service = $this->entity_uri_service; |
| 223 | 223 | |
| 224 | - $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 225 | - return $entity_uri_service->is_internal( $uri ); |
|
| 224 | + $local_entity_ids = array_filter($entity_ids, function($uri) use ($entity_uri_service) { |
|
| 225 | + return $entity_uri_service->is_internal($uri); |
|
| 226 | 226 | } ); |
| 227 | 227 | |
| 228 | 228 | // Bail out if there are no entities. |
| 229 | - if ( empty( $local_entity_ids ) ) { |
|
| 229 | + if (empty($local_entity_ids)) { |
|
| 230 | 230 | return array(); |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 234 | - $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID, $context ); |
|
| 233 | + $post = $this->entity_uri_service->get_entity(array_shift($local_entity_ids)); |
|
| 234 | + $jsonld = $this->jsonld_service->get_jsonld(false, $post->ID, $context); |
|
| 235 | 235 | // Reset the `url` to the term page. |
| 236 | - $jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 236 | + $jsonld[0]['url'] = get_term_link($term_id); |
|
| 237 | 237 | |
| 238 | 238 | return $jsonld; |
| 239 | 239 | } |