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