@@ -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'] = $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 ) ) { |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 151 | - |
|
| 152 | - echo "<script type=\"application/ld+json\">$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 | - |
|
| 175 | - $result = array( |
|
| 176 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 177 | - 'references' => array() |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @since 3.26.3 |
|
| 182 | - * Filter: wl_term_jsonld_array |
|
| 183 | - * @var $id int Term id |
|
| 184 | - * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 185 | - */ |
|
| 186 | - return apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 187 | - |
|
| 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'] = $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 ) ) { |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $jsonld_string = wp_json_encode( $jsonld ); |
|
| 151 | + |
|
| 152 | + echo "<script type=\"application/ld+json\">$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 | + |
|
| 175 | + $result = array( |
|
| 176 | + 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 177 | + 'references' => array() |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @since 3.26.3 |
|
| 182 | + * Filter: wl_term_jsonld_array |
|
| 183 | + * @var $id int Term id |
|
| 184 | + * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 185 | + */ |
|
| 186 | + return apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 187 | + |
|
| 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 ); |
|
| 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 | /** |
@@ -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 ) ) { |
|
| 146 | + if (empty($jsonld)) { |
|
| 147 | 147 | return; |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 150 | + $jsonld_string = wp_json_encode($jsonld); |
|
| 151 | 151 | |
| 152 | 152 | echo "<script type=\"application/ld+json\">$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,16 +164,16 @@ 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 | |
| 175 | 175 | $result = array( |
| 176 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 176 | + 'jsonld' => array_merge($jsonld_array, $entities_jsonld_array), |
|
| 177 | 177 | 'references' => array() |
| 178 | 178 | ); |
| 179 | 179 | |
@@ -183,22 +183,22 @@ discard block |
||
| 183 | 183 | * @var $id int Term id |
| 184 | 184 | * @var $jsonld_array array An array containing jsonld for term and entities. |
| 185 | 185 | */ |
| 186 | - return apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 186 | + return apply_filters('wl_term_jsonld_array', $result, $id); |
|
| 187 | 187 | |
| 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 | } |
@@ -18,192 +18,192 @@ |
||
| 18 | 18 | * @since 3.25.0 |
| 19 | 19 | */ |
| 20 | 20 | class Jsonld_Converter { |
| 21 | - /** |
|
| 22 | - * Enumerations for the field types. |
|
| 23 | - */ |
|
| 24 | - const FIELD_TYPE_TEXT_FIELD = 'text'; |
|
| 25 | - const FIELD_TYPE_CUSTOM_FIELD = 'custom_field'; |
|
| 26 | - const FIELD_TYPE_ACF = 'acf'; |
|
| 27 | - /** |
|
| 28 | - * The {@link Mappings_Validator} instance to test. |
|
| 29 | - * |
|
| 30 | - * @since 3.25.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var Mappings_Validator $validator The {@link Mappings_Validator} instance. |
|
| 33 | - */ |
|
| 34 | - private $validator; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 38 | - * |
|
| 39 | - * @since 3.25.0 |
|
| 40 | - * @access private |
|
| 41 | - * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 42 | - */ |
|
| 43 | - private $transform_functions_registry; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Initialize all dependencies required. |
|
| 47 | - * |
|
| 48 | - * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
|
| 49 | - * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
|
| 50 | - */ |
|
| 51 | - public function __construct( $validator, $transform_functions_registry ) { |
|
| 52 | - |
|
| 53 | - $this->validator = $validator; |
|
| 54 | - $this->transform_functions_registry = $transform_functions_registry; |
|
| 55 | - |
|
| 56 | - // Hook to refactor the JSON-LD. |
|
| 57 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 58 | - add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 59 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`. |
|
| 64 | - * |
|
| 65 | - * Receive the JSON-LD and the references in the array along with the post ID and transform them according to |
|
| 66 | - * the configuration. |
|
| 67 | - * |
|
| 68 | - * @param array $value { |
|
| 69 | - * The array containing the JSON-LD and the references. |
|
| 70 | - * |
|
| 71 | - * @type array $jsonld The JSON-LD array. |
|
| 72 | - * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 73 | - * } |
|
| 74 | - * |
|
| 75 | - * @param int $post_id The post ID. |
|
| 76 | - * |
|
| 77 | - * @return array An array with the updated JSON-LD and references. |
|
| 78 | - */ |
|
| 79 | - public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 80 | - |
|
| 81 | - $jsonld = $value['jsonld']; |
|
| 82 | - $references = $value['references']; |
|
| 83 | - |
|
| 84 | - return array( |
|
| 85 | - 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 86 | - 'references' => $references, |
|
| 87 | - ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Returns JSON-LD data after applying transformation functions. |
|
| 92 | - * |
|
| 93 | - * @param array $jsonld The JSON-LD structure. |
|
| 94 | - * @param int $post_id The {@link WP_Post} id. |
|
| 95 | - * @param array $references An array of post references. |
|
| 96 | - * |
|
| 97 | - * @return array the new refactored array structure. |
|
| 98 | - * @since 3.25.0 |
|
| 99 | - */ |
|
| 100 | - private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 101 | - |
|
| 102 | - // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
|
| 103 | - // instance may be reused afterwards. |
|
| 104 | - |
|
| 105 | - $properties = $this->validator->validate( $post_id ); |
|
| 106 | - $nested_properties = array(); |
|
| 107 | - |
|
| 108 | - foreach ( $properties as $property ) { |
|
| 109 | - // If the property has the character '/' in the property name then it is a nested property. |
|
| 110 | - if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 111 | - $nested_properties[] = $property; |
|
| 112 | - continue; |
|
| 113 | - } |
|
| 114 | - $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 115 | - if ( false !== $property_transformed_data ) { |
|
| 116 | - $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 121 | - |
|
| 122 | - return $jsonld; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Get the property data by applying the transformation function |
|
| 127 | - * |
|
| 128 | - * @param $property |
|
| 129 | - * @param $jsonld |
|
| 130 | - * @param $post_id |
|
| 131 | - * @param $references |
|
| 132 | - * |
|
| 133 | - * @return array|bool|null |
|
| 134 | - */ |
|
| 135 | - public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 136 | - $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 137 | - $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property ); |
|
| 138 | - if ( null !== $transform_instance ) { |
|
| 139 | - $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 140 | - if ( null !== $transform_data ) { |
|
| 141 | - return $this->make_single( $transform_data ); |
|
| 142 | - } |
|
| 143 | - } else { |
|
| 144 | - return $this->make_single( $data ); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return false; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Process all the nested properties. |
|
| 152 | - * |
|
| 153 | - * @param $nested_properties array |
|
| 154 | - * @param $jsonld array |
|
| 155 | - * |
|
| 156 | - * @return array |
|
| 157 | - */ |
|
| 158 | - public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 159 | - foreach ( $nested_properties as $property ) { |
|
| 160 | - $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 161 | - if ( false === $property_data ) { |
|
| 162 | - // No need to create nested levels. |
|
| 163 | - continue; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $keys = explode( '/', $property['property_name'] ); |
|
| 167 | - // end is the last level of the nested property. |
|
| 168 | - $end = array_pop( $keys ); |
|
| 169 | - $current_property_pointer = &$jsonld; |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Once we find all the nested levels from the property name |
|
| 173 | - * loop through it and create associative array if the levels |
|
| 174 | - * didnt exist. |
|
| 175 | - */ |
|
| 176 | - while ( count( $keys ) > 0 ) { |
|
| 177 | - $key = array_shift( $keys ); |
|
| 178 | - if ( $key === "" ) { |
|
| 179 | - continue; |
|
| 180 | - } |
|
| 181 | - if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 182 | - $current_property_pointer[ $key ] = array(); |
|
| 183 | - } |
|
| 184 | - // We are setting the pointer to the current key, so that at the end |
|
| 185 | - // we can add the data at last level. |
|
| 186 | - $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 187 | - } |
|
| 188 | - $current_property_pointer[ $end ] = $property_data; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return $jsonld; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - private function make_single( $value ) { |
|
| 195 | - |
|
| 196 | - $values = (array) $value; |
|
| 197 | - |
|
| 198 | - if ( empty( $values ) ) { |
|
| 199 | - return false; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 203 | - return current( $values ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return $values; |
|
| 207 | - } |
|
| 21 | + /** |
|
| 22 | + * Enumerations for the field types. |
|
| 23 | + */ |
|
| 24 | + const FIELD_TYPE_TEXT_FIELD = 'text'; |
|
| 25 | + const FIELD_TYPE_CUSTOM_FIELD = 'custom_field'; |
|
| 26 | + const FIELD_TYPE_ACF = 'acf'; |
|
| 27 | + /** |
|
| 28 | + * The {@link Mappings_Validator} instance to test. |
|
| 29 | + * |
|
| 30 | + * @since 3.25.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var Mappings_Validator $validator The {@link Mappings_Validator} instance. |
|
| 33 | + */ |
|
| 34 | + private $validator; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 38 | + * |
|
| 39 | + * @since 3.25.0 |
|
| 40 | + * @access private |
|
| 41 | + * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 42 | + */ |
|
| 43 | + private $transform_functions_registry; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Initialize all dependencies required. |
|
| 47 | + * |
|
| 48 | + * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
|
| 49 | + * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
|
| 50 | + */ |
|
| 51 | + public function __construct( $validator, $transform_functions_registry ) { |
|
| 52 | + |
|
| 53 | + $this->validator = $validator; |
|
| 54 | + $this->transform_functions_registry = $transform_functions_registry; |
|
| 55 | + |
|
| 56 | + // Hook to refactor the JSON-LD. |
|
| 57 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 58 | + add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 59 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`. |
|
| 64 | + * |
|
| 65 | + * Receive the JSON-LD and the references in the array along with the post ID and transform them according to |
|
| 66 | + * the configuration. |
|
| 67 | + * |
|
| 68 | + * @param array $value { |
|
| 69 | + * The array containing the JSON-LD and the references. |
|
| 70 | + * |
|
| 71 | + * @type array $jsonld The JSON-LD array. |
|
| 72 | + * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 73 | + * } |
|
| 74 | + * |
|
| 75 | + * @param int $post_id The post ID. |
|
| 76 | + * |
|
| 77 | + * @return array An array with the updated JSON-LD and references. |
|
| 78 | + */ |
|
| 79 | + public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 80 | + |
|
| 81 | + $jsonld = $value['jsonld']; |
|
| 82 | + $references = $value['references']; |
|
| 83 | + |
|
| 84 | + return array( |
|
| 85 | + 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 86 | + 'references' => $references, |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Returns JSON-LD data after applying transformation functions. |
|
| 92 | + * |
|
| 93 | + * @param array $jsonld The JSON-LD structure. |
|
| 94 | + * @param int $post_id The {@link WP_Post} id. |
|
| 95 | + * @param array $references An array of post references. |
|
| 96 | + * |
|
| 97 | + * @return array the new refactored array structure. |
|
| 98 | + * @since 3.25.0 |
|
| 99 | + */ |
|
| 100 | + private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 101 | + |
|
| 102 | + // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
|
| 103 | + // instance may be reused afterwards. |
|
| 104 | + |
|
| 105 | + $properties = $this->validator->validate( $post_id ); |
|
| 106 | + $nested_properties = array(); |
|
| 107 | + |
|
| 108 | + foreach ( $properties as $property ) { |
|
| 109 | + // If the property has the character '/' in the property name then it is a nested property. |
|
| 110 | + if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 111 | + $nested_properties[] = $property; |
|
| 112 | + continue; |
|
| 113 | + } |
|
| 114 | + $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 115 | + if ( false !== $property_transformed_data ) { |
|
| 116 | + $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 121 | + |
|
| 122 | + return $jsonld; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Get the property data by applying the transformation function |
|
| 127 | + * |
|
| 128 | + * @param $property |
|
| 129 | + * @param $jsonld |
|
| 130 | + * @param $post_id |
|
| 131 | + * @param $references |
|
| 132 | + * |
|
| 133 | + * @return array|bool|null |
|
| 134 | + */ |
|
| 135 | + public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 136 | + $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 137 | + $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property ); |
|
| 138 | + if ( null !== $transform_instance ) { |
|
| 139 | + $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 140 | + if ( null !== $transform_data ) { |
|
| 141 | + return $this->make_single( $transform_data ); |
|
| 142 | + } |
|
| 143 | + } else { |
|
| 144 | + return $this->make_single( $data ); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return false; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Process all the nested properties. |
|
| 152 | + * |
|
| 153 | + * @param $nested_properties array |
|
| 154 | + * @param $jsonld array |
|
| 155 | + * |
|
| 156 | + * @return array |
|
| 157 | + */ |
|
| 158 | + public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 159 | + foreach ( $nested_properties as $property ) { |
|
| 160 | + $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 161 | + if ( false === $property_data ) { |
|
| 162 | + // No need to create nested levels. |
|
| 163 | + continue; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $keys = explode( '/', $property['property_name'] ); |
|
| 167 | + // end is the last level of the nested property. |
|
| 168 | + $end = array_pop( $keys ); |
|
| 169 | + $current_property_pointer = &$jsonld; |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Once we find all the nested levels from the property name |
|
| 173 | + * loop through it and create associative array if the levels |
|
| 174 | + * didnt exist. |
|
| 175 | + */ |
|
| 176 | + while ( count( $keys ) > 0 ) { |
|
| 177 | + $key = array_shift( $keys ); |
|
| 178 | + if ( $key === "" ) { |
|
| 179 | + continue; |
|
| 180 | + } |
|
| 181 | + if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 182 | + $current_property_pointer[ $key ] = array(); |
|
| 183 | + } |
|
| 184 | + // We are setting the pointer to the current key, so that at the end |
|
| 185 | + // we can add the data at last level. |
|
| 186 | + $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 187 | + } |
|
| 188 | + $current_property_pointer[ $end ] = $property_data; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return $jsonld; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + private function make_single( $value ) { |
|
| 195 | + |
|
| 196 | + $values = (array) $value; |
|
| 197 | + |
|
| 198 | + if ( empty( $values ) ) { |
|
| 199 | + return false; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 203 | + return current( $values ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return $values; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | 209 | } |
@@ -48,15 +48,15 @@ discard block |
||
| 48 | 48 | * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
| 49 | 49 | * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
| 50 | 50 | */ |
| 51 | - public function __construct( $validator, $transform_functions_registry ) { |
|
| 51 | + public function __construct($validator, $transform_functions_registry) { |
|
| 52 | 52 | |
| 53 | 53 | $this->validator = $validator; |
| 54 | 54 | $this->transform_functions_registry = $transform_functions_registry; |
| 55 | 55 | |
| 56 | 56 | // Hook to refactor the JSON-LD. |
| 57 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 58 | - add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 3 ); |
|
| 59 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 57 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 58 | + add_filter('wl_entity_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 3); |
|
| 59 | + add_filter('wl_term_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | /** |
@@ -76,13 +76,13 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @return array An array with the updated JSON-LD and references. |
| 78 | 78 | */ |
| 79 | - public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 79 | + public function wl_post_jsonld_array($value, $post_id) { |
|
| 80 | 80 | |
| 81 | 81 | $jsonld = $value['jsonld']; |
| 82 | 82 | $references = $value['references']; |
| 83 | 83 | |
| 84 | 84 | return array( |
| 85 | - 'jsonld' => $this->wl_post_jsonld( $jsonld, $post_id, $references ), |
|
| 85 | + 'jsonld' => $this->wl_post_jsonld($jsonld, $post_id, $references), |
|
| 86 | 86 | 'references' => $references, |
| 87 | 87 | ); |
| 88 | 88 | } |
@@ -97,27 +97,27 @@ discard block |
||
| 97 | 97 | * @return array the new refactored array structure. |
| 98 | 98 | * @since 3.25.0 |
| 99 | 99 | */ |
| 100 | - private function wl_post_jsonld( $jsonld, $post_id, &$references ) { |
|
| 100 | + private function wl_post_jsonld($jsonld, $post_id, &$references) { |
|
| 101 | 101 | |
| 102 | 102 | // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
| 103 | 103 | // instance may be reused afterwards. |
| 104 | 104 | |
| 105 | - $properties = $this->validator->validate( $post_id ); |
|
| 105 | + $properties = $this->validator->validate($post_id); |
|
| 106 | 106 | $nested_properties = array(); |
| 107 | 107 | |
| 108 | - foreach ( $properties as $property ) { |
|
| 108 | + foreach ($properties as $property) { |
|
| 109 | 109 | // If the property has the character '/' in the property name then it is a nested property. |
| 110 | - if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 110 | + if (strpos($property['property_name'], '/') !== false) { |
|
| 111 | 111 | $nested_properties[] = $property; |
| 112 | 112 | continue; |
| 113 | 113 | } |
| 114 | - $property_transformed_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 115 | - if ( false !== $property_transformed_data ) { |
|
| 116 | - $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 114 | + $property_transformed_data = $this->get_property_data($property, $jsonld, $post_id, $references); |
|
| 115 | + if (false !== $property_transformed_data) { |
|
| 116 | + $jsonld[$property['property_name']] = $property_transformed_data; |
|
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $post_id, $references ); |
|
| 120 | + $jsonld = $this->process_nested_properties($nested_properties, $jsonld, $post_id, $references); |
|
| 121 | 121 | |
| 122 | 122 | return $jsonld; |
| 123 | 123 | } |
@@ -132,16 +132,16 @@ discard block |
||
| 132 | 132 | * |
| 133 | 133 | * @return array|bool|null |
| 134 | 134 | */ |
| 135 | - public function get_property_data( $property, $jsonld, $post_id, &$references ) { |
|
| 136 | - $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 137 | - $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property ); |
|
| 138 | - if ( null !== $transform_instance ) { |
|
| 139 | - $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 140 | - if ( null !== $transform_data ) { |
|
| 141 | - return $this->make_single( $transform_data ); |
|
| 135 | + public function get_property_data($property, $jsonld, $post_id, &$references) { |
|
| 136 | + $transform_instance = $this->transform_functions_registry->get_transform_function($property['transform_function']); |
|
| 137 | + $data = Data_Source_Factory::get_instance()->get_data($post_id, $property); |
|
| 138 | + if (null !== $transform_instance) { |
|
| 139 | + $transform_data = $transform_instance->transform_data($data, $jsonld, $references, $post_id); |
|
| 140 | + if (null !== $transform_data) { |
|
| 141 | + return $this->make_single($transform_data); |
|
| 142 | 142 | } |
| 143 | 143 | } else { |
| 144 | - return $this->make_single( $data ); |
|
| 144 | + return $this->make_single($data); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | return false; |
@@ -155,17 +155,17 @@ discard block |
||
| 155 | 155 | * |
| 156 | 156 | * @return array |
| 157 | 157 | */ |
| 158 | - public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references ) { |
|
| 159 | - foreach ( $nested_properties as $property ) { |
|
| 160 | - $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references ); |
|
| 161 | - if ( false === $property_data ) { |
|
| 158 | + public function process_nested_properties($nested_properties, $jsonld, $post_id, &$references) { |
|
| 159 | + foreach ($nested_properties as $property) { |
|
| 160 | + $property_data = $this->get_property_data($property, $jsonld, $post_id, $references); |
|
| 161 | + if (false === $property_data) { |
|
| 162 | 162 | // No need to create nested levels. |
| 163 | 163 | continue; |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - $keys = explode( '/', $property['property_name'] ); |
|
| 166 | + $keys = explode('/', $property['property_name']); |
|
| 167 | 167 | // end is the last level of the nested property. |
| 168 | - $end = array_pop( $keys ); |
|
| 168 | + $end = array_pop($keys); |
|
| 169 | 169 | $current_property_pointer = &$jsonld; |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -173,34 +173,34 @@ discard block |
||
| 173 | 173 | * loop through it and create associative array if the levels |
| 174 | 174 | * didnt exist. |
| 175 | 175 | */ |
| 176 | - while ( count( $keys ) > 0 ) { |
|
| 177 | - $key = array_shift( $keys ); |
|
| 178 | - if ( $key === "" ) { |
|
| 176 | + while (count($keys) > 0) { |
|
| 177 | + $key = array_shift($keys); |
|
| 178 | + if ($key === "") { |
|
| 179 | 179 | continue; |
| 180 | 180 | } |
| 181 | - if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 182 | - $current_property_pointer[ $key ] = array(); |
|
| 181 | + if ( ! array_key_exists($key, $current_property_pointer)) { |
|
| 182 | + $current_property_pointer[$key] = array(); |
|
| 183 | 183 | } |
| 184 | 184 | // We are setting the pointer to the current key, so that at the end |
| 185 | 185 | // we can add the data at last level. |
| 186 | - $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 186 | + $current_property_pointer = &$current_property_pointer[$key]; |
|
| 187 | 187 | } |
| 188 | - $current_property_pointer[ $end ] = $property_data; |
|
| 188 | + $current_property_pointer[$end] = $property_data; |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | return $jsonld; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | - private function make_single( $value ) { |
|
| 194 | + private function make_single($value) { |
|
| 195 | 195 | |
| 196 | 196 | $values = (array) $value; |
| 197 | 197 | |
| 198 | - if ( empty( $values ) ) { |
|
| 198 | + if (empty($values)) { |
|
| 199 | 199 | return false; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 203 | - return current( $values ); |
|
| 202 | + if (1 === count($values) && 0 === key($values)) { |
|
| 203 | + return current($values); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | return $values; |