@@ -16,218 +16,218 @@ |
||
| 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'] = strip_tags( strip_shortcodes( $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 | - $query_object = get_queried_object(); |
|
| 135 | - |
|
| 136 | - // Check if it is a term page. |
|
| 137 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 138 | - return; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $term_id = $query_object->term_id; |
|
| 142 | - |
|
| 143 | - $jsonld = $this->get( $term_id ); |
|
| 144 | - |
|
| 145 | - // Bail out if the JSON-LD is empty. |
|
| 146 | - if ( empty( $jsonld ) || empty( $jsonld['jsonld'] ) ) { |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $jsonld_string = wp_json_encode( $jsonld['jsonld'] ); |
|
| 151 | - |
|
| 152 | - echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
|
| 153 | - |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - public function get( $id ) { |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Support for carousel rich snippet, get jsonld data present |
|
| 160 | - * for all the posts shown in the term page, and add the jsonld data |
|
| 161 | - * to list |
|
| 162 | - * |
|
| 163 | - * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 164 | - * |
|
| 165 | - * @since 3.26.0 |
|
| 166 | - */ |
|
| 167 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 168 | - $jsonld_array = array(); |
|
| 169 | - if ( $carousel_data ) { |
|
| 170 | - $jsonld_array[] = $carousel_data; |
|
| 171 | - } |
|
| 172 | - $entities_jsonld_array = $this->get_entity_jsonld( $id ); |
|
| 173 | - |
|
| 174 | - $result = array( |
|
| 175 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 176 | - 'references' => array() |
|
| 177 | - ); |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @since 3.26.3 |
|
| 181 | - * Filter: wl_term_jsonld_array |
|
| 182 | - * @var $id int Term id |
|
| 183 | - * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 184 | - */ |
|
| 185 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 186 | - |
|
| 187 | - return $arr['jsonld']; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - private function get_term_url( $id ) { |
|
| 191 | - |
|
| 192 | - if ( is_null( $id ) ) { |
|
| 193 | - return $_SERVER['REQUEST_URI']; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 197 | - if ( ! empty( $maybe_url ) ) { |
|
| 198 | - return $maybe_url; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return get_term_link( $id ); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Return jsonld for entities bound to terms. |
|
| 206 | - * |
|
| 207 | - * @param $id |
|
| 208 | - * |
|
| 209 | - * @return array |
|
| 210 | - */ |
|
| 211 | - private function get_entity_jsonld( $id ) { |
|
| 212 | - // The `_wl_entity_id` are URIs. |
|
| 213 | - $entity_ids = get_term_meta( $id, '_wl_entity_id' ); |
|
| 214 | - $entity_uri_service = $this->entity_uri_service; |
|
| 215 | - |
|
| 216 | - $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 217 | - return $entity_uri_service->is_internal( $uri ); |
|
| 218 | - } ); |
|
| 219 | - |
|
| 220 | - // Bail out if there are no entities. |
|
| 221 | - if ( empty( $local_entity_ids ) ) { |
|
| 222 | - return array(); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 226 | - $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 227 | - // Reset the `url` to the term page. |
|
| 228 | - $jsonld[0]['url'] = get_term_link( $id ); |
|
| 229 | - |
|
| 230 | - return $jsonld; |
|
| 231 | - } |
|
| 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'] = strip_tags( strip_shortcodes( $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 | + $query_object = get_queried_object(); |
|
| 135 | + |
|
| 136 | + // Check if it is a term page. |
|
| 137 | + if ( ! $query_object instanceof WP_Term ) { |
|
| 138 | + return; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $term_id = $query_object->term_id; |
|
| 142 | + |
|
| 143 | + $jsonld = $this->get( $term_id ); |
|
| 144 | + |
|
| 145 | + // Bail out if the JSON-LD is empty. |
|
| 146 | + if ( empty( $jsonld ) || empty( $jsonld['jsonld'] ) ) { |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $jsonld_string = wp_json_encode( $jsonld['jsonld'] ); |
|
| 151 | + |
|
| 152 | + echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
|
| 153 | + |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + public function get( $id ) { |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Support for carousel rich snippet, get jsonld data present |
|
| 160 | + * for all the posts shown in the term page, and add the jsonld data |
|
| 161 | + * to list |
|
| 162 | + * |
|
| 163 | + * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 164 | + * |
|
| 165 | + * @since 3.26.0 |
|
| 166 | + */ |
|
| 167 | + $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 168 | + $jsonld_array = array(); |
|
| 169 | + if ( $carousel_data ) { |
|
| 170 | + $jsonld_array[] = $carousel_data; |
|
| 171 | + } |
|
| 172 | + $entities_jsonld_array = $this->get_entity_jsonld( $id ); |
|
| 173 | + |
|
| 174 | + $result = array( |
|
| 175 | + 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 176 | + 'references' => array() |
|
| 177 | + ); |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @since 3.26.3 |
|
| 181 | + * Filter: wl_term_jsonld_array |
|
| 182 | + * @var $id int Term id |
|
| 183 | + * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 184 | + */ |
|
| 185 | + $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 186 | + |
|
| 187 | + return $arr['jsonld']; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + private function get_term_url( $id ) { |
|
| 191 | + |
|
| 192 | + if ( is_null( $id ) ) { |
|
| 193 | + return $_SERVER['REQUEST_URI']; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 197 | + if ( ! empty( $maybe_url ) ) { |
|
| 198 | + return $maybe_url; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return get_term_link( $id ); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Return jsonld for entities bound to terms. |
|
| 206 | + * |
|
| 207 | + * @param $id |
|
| 208 | + * |
|
| 209 | + * @return array |
|
| 210 | + */ |
|
| 211 | + private function get_entity_jsonld( $id ) { |
|
| 212 | + // The `_wl_entity_id` are URIs. |
|
| 213 | + $entity_ids = get_term_meta( $id, '_wl_entity_id' ); |
|
| 214 | + $entity_uri_service = $this->entity_uri_service; |
|
| 215 | + |
|
| 216 | + $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 217 | + return $entity_uri_service->is_internal( $uri ); |
|
| 218 | + } ); |
|
| 219 | + |
|
| 220 | + // Bail out if there are no entities. |
|
| 221 | + if ( empty( $local_entity_ids ) ) { |
|
| 222 | + return array(); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 226 | + $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 227 | + // Reset the `url` to the term page. |
|
| 228 | + $jsonld[0]['url'] = get_term_link( $id ); |
|
| 229 | + |
|
| 230 | + return $jsonld; |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | 233 | } |
@@ -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 ); |
|
| 78 | - $post_jsonld['description'] = strip_tags( strip_shortcodes( $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 ); |
|
| 76 | + if ( ! is_null($id)) { |
|
| 77 | + $term = get_term($id); |
|
| 78 | + $post_jsonld['description'] = strip_tags(strip_shortcodes($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 | 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 | /** |
@@ -134,26 +134,26 @@ discard block |
||
| 134 | 134 | $query_object = get_queried_object(); |
| 135 | 135 | |
| 136 | 136 | // Check if it is a term page. |
| 137 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 137 | + if ( ! $query_object instanceof WP_Term) { |
|
| 138 | 138 | return; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | $term_id = $query_object->term_id; |
| 142 | 142 | |
| 143 | - $jsonld = $this->get( $term_id ); |
|
| 143 | + $jsonld = $this->get($term_id); |
|
| 144 | 144 | |
| 145 | 145 | // Bail out if the JSON-LD is empty. |
| 146 | - if ( empty( $jsonld ) || empty( $jsonld['jsonld'] ) ) { |
|
| 146 | + if (empty($jsonld) || empty($jsonld['jsonld'])) { |
|
| 147 | 147 | return; |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - $jsonld_string = wp_json_encode( $jsonld['jsonld'] ); |
|
| 150 | + $jsonld_string = wp_json_encode($jsonld['jsonld']); |
|
| 151 | 151 | |
| 152 | 152 | echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
| 153 | 153 | |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - public function get( $id ) { |
|
| 156 | + public function get($id) { |
|
| 157 | 157 | |
| 158 | 158 | /** |
| 159 | 159 | * Support for carousel rich snippet, get jsonld data present |
@@ -164,15 +164,15 @@ discard block |
||
| 164 | 164 | * |
| 165 | 165 | * @since 3.26.0 |
| 166 | 166 | */ |
| 167 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 167 | + $carousel_data = $this->get_carousel_jsonld($id); |
|
| 168 | 168 | $jsonld_array = array(); |
| 169 | - if ( $carousel_data ) { |
|
| 169 | + if ($carousel_data) { |
|
| 170 | 170 | $jsonld_array[] = $carousel_data; |
| 171 | 171 | } |
| 172 | - $entities_jsonld_array = $this->get_entity_jsonld( $id ); |
|
| 172 | + $entities_jsonld_array = $this->get_entity_jsonld($id); |
|
| 173 | 173 | |
| 174 | 174 | $result = array( |
| 175 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 175 | + 'jsonld' => array_merge($jsonld_array, $entities_jsonld_array), |
|
| 176 | 176 | 'references' => array() |
| 177 | 177 | ); |
| 178 | 178 | |
@@ -182,23 +182,23 @@ discard block |
||
| 182 | 182 | * @var $id int Term id |
| 183 | 183 | * @var $jsonld_array array An array containing jsonld for term and entities. |
| 184 | 184 | */ |
| 185 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 185 | + $arr = apply_filters('wl_term_jsonld_array', $result, $id); |
|
| 186 | 186 | |
| 187 | 187 | return $arr['jsonld']; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - private function get_term_url( $id ) { |
|
| 190 | + private function get_term_url($id) { |
|
| 191 | 191 | |
| 192 | - if ( is_null( $id ) ) { |
|
| 192 | + if (is_null($id)) { |
|
| 193 | 193 | return $_SERVER['REQUEST_URI']; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 197 | - if ( ! empty( $maybe_url ) ) { |
|
| 196 | + $maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true); |
|
| 197 | + if ( ! empty($maybe_url)) { |
|
| 198 | 198 | return $maybe_url; |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - return get_term_link( $id ); |
|
| 201 | + return get_term_link($id); |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | /** |
@@ -208,24 +208,24 @@ discard block |
||
| 208 | 208 | * |
| 209 | 209 | * @return array |
| 210 | 210 | */ |
| 211 | - private function get_entity_jsonld( $id ) { |
|
| 211 | + private function get_entity_jsonld($id) { |
|
| 212 | 212 | // The `_wl_entity_id` are URIs. |
| 213 | - $entity_ids = get_term_meta( $id, '_wl_entity_id' ); |
|
| 213 | + $entity_ids = get_term_meta($id, '_wl_entity_id'); |
|
| 214 | 214 | $entity_uri_service = $this->entity_uri_service; |
| 215 | 215 | |
| 216 | - $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 217 | - return $entity_uri_service->is_internal( $uri ); |
|
| 216 | + $local_entity_ids = array_filter($entity_ids, function($uri) use ($entity_uri_service) { |
|
| 217 | + return $entity_uri_service->is_internal($uri); |
|
| 218 | 218 | } ); |
| 219 | 219 | |
| 220 | 220 | // Bail out if there are no entities. |
| 221 | - if ( empty( $local_entity_ids ) ) { |
|
| 221 | + if (empty($local_entity_ids)) { |
|
| 222 | 222 | return array(); |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 226 | - $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 225 | + $post = $this->entity_uri_service->get_entity(array_shift($local_entity_ids)); |
|
| 226 | + $jsonld = $this->jsonld_service->get_jsonld(false, $post->ID); |
|
| 227 | 227 | // Reset the `url` to the term page. |
| 228 | - $jsonld[0]['url'] = get_term_link( $id ); |
|
| 228 | + $jsonld[0]['url'] = get_term_link($id); |
|
| 229 | 229 | |
| 230 | 230 | return $jsonld; |
| 231 | 231 | } |