@@ -15,87 +15,87 @@ |
||
| 15 | 15 | |
| 16 | 16 | class Post_Jsonld { |
| 17 | 17 | |
| 18 | - public function init() { |
|
| 19 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 10, 2 ); |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function wl_post_jsonld_array( $data, $post_id ) { |
|
| 23 | - |
|
| 24 | - $term_references = $this->get_term_references( $post_id ); |
|
| 25 | - |
|
| 26 | - if ( ! $term_references ) { |
|
| 27 | - return $data; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - $references = $data['references']; |
|
| 31 | - $jsonld = $data['jsonld']; |
|
| 32 | - |
|
| 33 | - $jsonld['mentions'] = $this->append_term_mentions( $jsonld, $term_references ); |
|
| 34 | - |
|
| 35 | - return array( |
|
| 36 | - 'jsonld' => $jsonld, |
|
| 37 | - 'references' => array_merge( $references, $term_references ) |
|
| 38 | - ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param $post_id |
|
| 43 | - * |
|
| 44 | - * @return array Returns a list of term references, Returns empty array if none found. |
|
| 45 | - */ |
|
| 46 | - private function get_term_references( $post_id ) { |
|
| 47 | - |
|
| 48 | - /** @var WP_Taxonomy[] $taxonomies_for_post */ |
|
| 49 | - $taxonomies_for_post = get_object_taxonomies( get_post_type( $post_id ), 'objects' ); |
|
| 50 | - |
|
| 51 | - // now we need to collect all terms attached to this post. |
|
| 52 | - $terms = array(); |
|
| 53 | - |
|
| 54 | - foreach ( $taxonomies_for_post as $taxonomy ) { |
|
| 55 | - // Please note that `$taxonomy->publicly_queryable is only WP 4.7+ |
|
| 56 | - if ( 'wl_entity_type' === $taxonomy->name || ! $taxonomy->public ) { |
|
| 57 | - continue; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy->name ); |
|
| 61 | - if ( is_array( $taxonomy_terms ) ) { |
|
| 62 | - $terms = array_merge( $terms, $taxonomy_terms ); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // Convert everything to the Term Reference. |
|
| 67 | - return array_filter( array_map( function ( $term ) { |
|
| 68 | - /** |
|
| 69 | - * @var WP_Term $term |
|
| 70 | - */ |
|
| 71 | - if ( Wordpress_Term_Content_Legacy_Service::get_instance() |
|
| 72 | - ->get_entity_id( Wordpress_Content_Id::create_term( $term->term_id ) ) |
|
| 73 | - ) { |
|
| 74 | - return new Term_Reference( $term->term_id ); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - return false; |
|
| 78 | - }, $terms ) ); |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param $jsonld array |
|
| 84 | - * @param $term_references array<Term_Reference> |
|
| 85 | - */ |
|
| 86 | - private function append_term_mentions( $jsonld, $term_references ) { |
|
| 87 | - |
|
| 88 | - $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - $term_mentions = array_map( function ( $term_reference ) { |
|
| 92 | - return array( |
|
| 93 | - '@id' => Wordpress_Term_Content_Legacy_Service::get_instance() |
|
| 94 | - ->get_entity_id( Wordpress_Content_Id::create_term( $term_reference->get_id() ) ) |
|
| 95 | - ); |
|
| 96 | - }, $term_references ); |
|
| 97 | - |
|
| 98 | - return array_merge( $existing_mentions, $term_mentions ); |
|
| 99 | - } |
|
| 18 | + public function init() { |
|
| 19 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 10, 2 ); |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function wl_post_jsonld_array( $data, $post_id ) { |
|
| 23 | + |
|
| 24 | + $term_references = $this->get_term_references( $post_id ); |
|
| 25 | + |
|
| 26 | + if ( ! $term_references ) { |
|
| 27 | + return $data; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + $references = $data['references']; |
|
| 31 | + $jsonld = $data['jsonld']; |
|
| 32 | + |
|
| 33 | + $jsonld['mentions'] = $this->append_term_mentions( $jsonld, $term_references ); |
|
| 34 | + |
|
| 35 | + return array( |
|
| 36 | + 'jsonld' => $jsonld, |
|
| 37 | + 'references' => array_merge( $references, $term_references ) |
|
| 38 | + ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param $post_id |
|
| 43 | + * |
|
| 44 | + * @return array Returns a list of term references, Returns empty array if none found. |
|
| 45 | + */ |
|
| 46 | + private function get_term_references( $post_id ) { |
|
| 47 | + |
|
| 48 | + /** @var WP_Taxonomy[] $taxonomies_for_post */ |
|
| 49 | + $taxonomies_for_post = get_object_taxonomies( get_post_type( $post_id ), 'objects' ); |
|
| 50 | + |
|
| 51 | + // now we need to collect all terms attached to this post. |
|
| 52 | + $terms = array(); |
|
| 53 | + |
|
| 54 | + foreach ( $taxonomies_for_post as $taxonomy ) { |
|
| 55 | + // Please note that `$taxonomy->publicly_queryable is only WP 4.7+ |
|
| 56 | + if ( 'wl_entity_type' === $taxonomy->name || ! $taxonomy->public ) { |
|
| 57 | + continue; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $taxonomy_terms = get_the_terms( $post_id, $taxonomy->name ); |
|
| 61 | + if ( is_array( $taxonomy_terms ) ) { |
|
| 62 | + $terms = array_merge( $terms, $taxonomy_terms ); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // Convert everything to the Term Reference. |
|
| 67 | + return array_filter( array_map( function ( $term ) { |
|
| 68 | + /** |
|
| 69 | + * @var WP_Term $term |
|
| 70 | + */ |
|
| 71 | + if ( Wordpress_Term_Content_Legacy_Service::get_instance() |
|
| 72 | + ->get_entity_id( Wordpress_Content_Id::create_term( $term->term_id ) ) |
|
| 73 | + ) { |
|
| 74 | + return new Term_Reference( $term->term_id ); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + return false; |
|
| 78 | + }, $terms ) ); |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param $jsonld array |
|
| 84 | + * @param $term_references array<Term_Reference> |
|
| 85 | + */ |
|
| 86 | + private function append_term_mentions( $jsonld, $term_references ) { |
|
| 87 | + |
|
| 88 | + $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + $term_mentions = array_map( function ( $term_reference ) { |
|
| 92 | + return array( |
|
| 93 | + '@id' => Wordpress_Term_Content_Legacy_Service::get_instance() |
|
| 94 | + ->get_entity_id( Wordpress_Content_Id::create_term( $term_reference->get_id() ) ) |
|
| 95 | + ); |
|
| 96 | + }, $term_references ); |
|
| 97 | + |
|
| 98 | + return array_merge( $existing_mentions, $term_mentions ); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | 101 | } |
| 102 | 102 | \ No newline at end of file |
@@ -16,25 +16,25 @@ discard block |
||
| 16 | 16 | class Post_Jsonld { |
| 17 | 17 | |
| 18 | 18 | public function init() { |
| 19 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 10, 2 ); |
|
| 19 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 10, 2); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | - public function wl_post_jsonld_array( $data, $post_id ) { |
|
| 22 | + public function wl_post_jsonld_array($data, $post_id) { |
|
| 23 | 23 | |
| 24 | - $term_references = $this->get_term_references( $post_id ); |
|
| 24 | + $term_references = $this->get_term_references($post_id); |
|
| 25 | 25 | |
| 26 | - if ( ! $term_references ) { |
|
| 26 | + if ( ! $term_references) { |
|
| 27 | 27 | return $data; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | $references = $data['references']; |
| 31 | 31 | $jsonld = $data['jsonld']; |
| 32 | 32 | |
| 33 | - $jsonld['mentions'] = $this->append_term_mentions( $jsonld, $term_references ); |
|
| 33 | + $jsonld['mentions'] = $this->append_term_mentions($jsonld, $term_references); |
|
| 34 | 34 | |
| 35 | 35 | return array( |
| 36 | 36 | 'jsonld' => $jsonld, |
| 37 | - 'references' => array_merge( $references, $term_references ) |
|
| 37 | + 'references' => array_merge($references, $term_references) |
|
| 38 | 38 | ); |
| 39 | 39 | } |
| 40 | 40 | |
@@ -43,39 +43,39 @@ discard block |
||
| 43 | 43 | * |
| 44 | 44 | * @return array Returns a list of term references, Returns empty array if none found. |
| 45 | 45 | */ |
| 46 | - private function get_term_references( $post_id ) { |
|
| 46 | + private function get_term_references($post_id) { |
|
| 47 | 47 | |
| 48 | 48 | /** @var WP_Taxonomy[] $taxonomies_for_post */ |
| 49 | - $taxonomies_for_post = get_object_taxonomies( get_post_type( $post_id ), 'objects' ); |
|
| 49 | + $taxonomies_for_post = get_object_taxonomies(get_post_type($post_id), 'objects'); |
|
| 50 | 50 | |
| 51 | 51 | // now we need to collect all terms attached to this post. |
| 52 | 52 | $terms = array(); |
| 53 | 53 | |
| 54 | - foreach ( $taxonomies_for_post as $taxonomy ) { |
|
| 54 | + foreach ($taxonomies_for_post as $taxonomy) { |
|
| 55 | 55 | // Please note that `$taxonomy->publicly_queryable is only WP 4.7+ |
| 56 | - if ( 'wl_entity_type' === $taxonomy->name || ! $taxonomy->public ) { |
|
| 56 | + if ('wl_entity_type' === $taxonomy->name || ! $taxonomy->public) { |
|
| 57 | 57 | continue; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy->name ); |
|
| 61 | - if ( is_array( $taxonomy_terms ) ) { |
|
| 62 | - $terms = array_merge( $terms, $taxonomy_terms ); |
|
| 60 | + $taxonomy_terms = get_the_terms($post_id, $taxonomy->name); |
|
| 61 | + if (is_array($taxonomy_terms)) { |
|
| 62 | + $terms = array_merge($terms, $taxonomy_terms); |
|
| 63 | 63 | } |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Convert everything to the Term Reference. |
| 67 | - return array_filter( array_map( function ( $term ) { |
|
| 67 | + return array_filter(array_map(function($term) { |
|
| 68 | 68 | /** |
| 69 | 69 | * @var WP_Term $term |
| 70 | 70 | */ |
| 71 | - if ( Wordpress_Term_Content_Legacy_Service::get_instance() |
|
| 72 | - ->get_entity_id( Wordpress_Content_Id::create_term( $term->term_id ) ) |
|
| 71 | + if (Wordpress_Term_Content_Legacy_Service::get_instance() |
|
| 72 | + ->get_entity_id(Wordpress_Content_Id::create_term($term->term_id)) |
|
| 73 | 73 | ) { |
| 74 | - return new Term_Reference( $term->term_id ); |
|
| 74 | + return new Term_Reference($term->term_id); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | return false; |
| 78 | - }, $terms ) ); |
|
| 78 | + }, $terms)); |
|
| 79 | 79 | |
| 80 | 80 | } |
| 81 | 81 | |
@@ -83,19 +83,19 @@ discard block |
||
| 83 | 83 | * @param $jsonld array |
| 84 | 84 | * @param $term_references array<Term_Reference> |
| 85 | 85 | */ |
| 86 | - private function append_term_mentions( $jsonld, $term_references ) { |
|
| 86 | + private function append_term_mentions($jsonld, $term_references) { |
|
| 87 | 87 | |
| 88 | - $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
| 88 | + $existing_mentions = array_key_exists('mentions', $jsonld) ? $jsonld['mentions'] : array(); |
|
| 89 | 89 | |
| 90 | 90 | |
| 91 | - $term_mentions = array_map( function ( $term_reference ) { |
|
| 91 | + $term_mentions = array_map(function($term_reference) { |
|
| 92 | 92 | return array( |
| 93 | 93 | '@id' => Wordpress_Term_Content_Legacy_Service::get_instance() |
| 94 | - ->get_entity_id( Wordpress_Content_Id::create_term( $term_reference->get_id() ) ) |
|
| 94 | + ->get_entity_id(Wordpress_Content_Id::create_term($term_reference->get_id())) |
|
| 95 | 95 | ); |
| 96 | - }, $term_references ); |
|
| 96 | + }, $term_references); |
|
| 97 | 97 | |
| 98 | - return array_merge( $existing_mentions, $term_mentions ); |
|
| 98 | + return array_merge($existing_mentions, $term_mentions); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | } |
| 102 | 102 | \ No newline at end of file |