@@ -14,152 +14,152 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Graph { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * A single item in jsonld ( associative array ) |
|
| 19 | - * |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - private $main_jsonld; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var array<Content_Id> |
|
| 26 | - */ |
|
| 27 | - private $referenced_content_ids = array(); |
|
| 28 | - /** |
|
| 29 | - * @var \Wordlift_Post_Converter |
|
| 30 | - */ |
|
| 31 | - private $post_converter; |
|
| 32 | - /** |
|
| 33 | - * @var \Wordlift_Term_JsonLd_Adapter |
|
| 34 | - */ |
|
| 35 | - private $term_converter; |
|
| 36 | - |
|
| 37 | - public function __construct( $main_jsonld, $post_converter, $term_converter ) { |
|
| 38 | - $this->main_jsonld = $main_jsonld; |
|
| 39 | - $this->post_converter = $post_converter; |
|
| 40 | - $this->term_converter = $term_converter; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function set_main_jsonld( $main_jsonld ) { |
|
| 44 | - $this->main_jsonld = $main_jsonld; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function get_main_jsonld() { |
|
| 48 | - return $this->main_jsonld; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param $references array<int> |
|
| 53 | - * |
|
| 54 | - * @return Graph |
|
| 55 | - */ |
|
| 56 | - public function add_references( $refs ) { |
|
| 57 | - Assertions::is_array( $refs ); |
|
| 58 | - |
|
| 59 | - foreach ( $refs as $ref ) { |
|
| 60 | - $this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref ); |
|
| 61 | - } |
|
| 62 | - return $this; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param $reference_infos array |
|
| 67 | - * Structure: [ |
|
| 68 | - * [ |
|
| 69 | - * 'reference' => \Wordlift_Property_Entity_Reference, |
|
| 70 | - * ], |
|
| 71 | - * // more array items ... |
|
| 72 | - * ] |
|
| 73 | - * |
|
| 74 | - * @return Graph |
|
| 75 | - */ |
|
| 76 | - public function add_required_reference_infos( $references_infos ) { |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @var $required_references array<\Wordlift_Property_Entity_Reference> |
|
| 80 | - */ |
|
| 81 | - $required_references = array_filter( |
|
| 82 | - $references_infos, |
|
| 83 | - function ( $item ) { |
|
| 84 | - return isset( $item['reference'] ) && |
|
| 85 | - // Check that the reference is required |
|
| 86 | - $item['reference']->get_required(); |
|
| 87 | - } |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - foreach ( $required_references as $required_reference ) { |
|
| 91 | - $this->referenced_content_ids[] = new Wordpress_Content_Id( |
|
| 92 | - $required_reference->get_id(), |
|
| 93 | - $required_reference->get_type() |
|
| 94 | - ); |
|
| 95 | - } |
|
| 96 | - return $this; |
|
| 97 | - |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @param $relations Relations |
|
| 102 | - * |
|
| 103 | - * @return Graph |
|
| 104 | - */ |
|
| 105 | - public function add_relations( $relations ) { |
|
| 106 | - |
|
| 107 | - foreach ( $relations->toArray() as $relation ) { |
|
| 108 | - |
|
| 109 | - $this->referenced_content_ids[] = $relation->get_object(); |
|
| 110 | - |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $this; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param $content_id Wordpress_Content_Id |
|
| 118 | - * @param $context int |
|
| 119 | - * @return array|bool |
|
| 120 | - */ |
|
| 121 | - private function expand( $content_id, $context ) { |
|
| 122 | - $object_id = $content_id->get_id(); |
|
| 123 | - $object_type = $content_id->get_type(); |
|
| 124 | - |
|
| 125 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 126 | - $references = array(); |
|
| 127 | - $reference_info = array(); |
|
| 128 | - $relations = new Relations(); |
|
| 129 | - return $this->post_converter->convert( $object_id, $references, $reference_info, $relations ); |
|
| 130 | - } elseif ( $object_type === Object_Type_Enum::TERM ) { |
|
| 131 | - // Skip the Uncategorized term. |
|
| 132 | - if ( 1 === $object_id ) { |
|
| 133 | - return false; |
|
| 134 | - } |
|
| 135 | - return current( $this->term_converter->get( $object_id, $context ) ); |
|
| 136 | - } else { |
|
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param $context int Instance of Jsonld_Context_Enum |
|
| 143 | - * |
|
| 144 | - * @return array |
|
| 145 | - */ |
|
| 146 | - public function render( $context ) { |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * This is possible because the toString() method of |
|
| 150 | - * Wordpress_Content_Id is used to get the unique value. |
|
| 151 | - */ |
|
| 152 | - $unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING ); |
|
| 153 | - |
|
| 154 | - $result = array( $this->main_jsonld ); |
|
| 155 | - |
|
| 156 | - foreach ( $unique_content_ids as $unique_content_id ) { |
|
| 157 | - $result[] = $this->expand( $unique_content_id, $context ); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // Filter out the false and empty results. |
|
| 161 | - return array_filter( $result ); |
|
| 162 | - |
|
| 163 | - } |
|
| 17 | + /** |
|
| 18 | + * A single item in jsonld ( associative array ) |
|
| 19 | + * |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + private $main_jsonld; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var array<Content_Id> |
|
| 26 | + */ |
|
| 27 | + private $referenced_content_ids = array(); |
|
| 28 | + /** |
|
| 29 | + * @var \Wordlift_Post_Converter |
|
| 30 | + */ |
|
| 31 | + private $post_converter; |
|
| 32 | + /** |
|
| 33 | + * @var \Wordlift_Term_JsonLd_Adapter |
|
| 34 | + */ |
|
| 35 | + private $term_converter; |
|
| 36 | + |
|
| 37 | + public function __construct( $main_jsonld, $post_converter, $term_converter ) { |
|
| 38 | + $this->main_jsonld = $main_jsonld; |
|
| 39 | + $this->post_converter = $post_converter; |
|
| 40 | + $this->term_converter = $term_converter; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function set_main_jsonld( $main_jsonld ) { |
|
| 44 | + $this->main_jsonld = $main_jsonld; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function get_main_jsonld() { |
|
| 48 | + return $this->main_jsonld; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param $references array<int> |
|
| 53 | + * |
|
| 54 | + * @return Graph |
|
| 55 | + */ |
|
| 56 | + public function add_references( $refs ) { |
|
| 57 | + Assertions::is_array( $refs ); |
|
| 58 | + |
|
| 59 | + foreach ( $refs as $ref ) { |
|
| 60 | + $this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref ); |
|
| 61 | + } |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param $reference_infos array |
|
| 67 | + * Structure: [ |
|
| 68 | + * [ |
|
| 69 | + * 'reference' => \Wordlift_Property_Entity_Reference, |
|
| 70 | + * ], |
|
| 71 | + * // more array items ... |
|
| 72 | + * ] |
|
| 73 | + * |
|
| 74 | + * @return Graph |
|
| 75 | + */ |
|
| 76 | + public function add_required_reference_infos( $references_infos ) { |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @var $required_references array<\Wordlift_Property_Entity_Reference> |
|
| 80 | + */ |
|
| 81 | + $required_references = array_filter( |
|
| 82 | + $references_infos, |
|
| 83 | + function ( $item ) { |
|
| 84 | + return isset( $item['reference'] ) && |
|
| 85 | + // Check that the reference is required |
|
| 86 | + $item['reference']->get_required(); |
|
| 87 | + } |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + foreach ( $required_references as $required_reference ) { |
|
| 91 | + $this->referenced_content_ids[] = new Wordpress_Content_Id( |
|
| 92 | + $required_reference->get_id(), |
|
| 93 | + $required_reference->get_type() |
|
| 94 | + ); |
|
| 95 | + } |
|
| 96 | + return $this; |
|
| 97 | + |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @param $relations Relations |
|
| 102 | + * |
|
| 103 | + * @return Graph |
|
| 104 | + */ |
|
| 105 | + public function add_relations( $relations ) { |
|
| 106 | + |
|
| 107 | + foreach ( $relations->toArray() as $relation ) { |
|
| 108 | + |
|
| 109 | + $this->referenced_content_ids[] = $relation->get_object(); |
|
| 110 | + |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $this; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param $content_id Wordpress_Content_Id |
|
| 118 | + * @param $context int |
|
| 119 | + * @return array|bool |
|
| 120 | + */ |
|
| 121 | + private function expand( $content_id, $context ) { |
|
| 122 | + $object_id = $content_id->get_id(); |
|
| 123 | + $object_type = $content_id->get_type(); |
|
| 124 | + |
|
| 125 | + if ( $object_type === Object_Type_Enum::POST ) { |
|
| 126 | + $references = array(); |
|
| 127 | + $reference_info = array(); |
|
| 128 | + $relations = new Relations(); |
|
| 129 | + return $this->post_converter->convert( $object_id, $references, $reference_info, $relations ); |
|
| 130 | + } elseif ( $object_type === Object_Type_Enum::TERM ) { |
|
| 131 | + // Skip the Uncategorized term. |
|
| 132 | + if ( 1 === $object_id ) { |
|
| 133 | + return false; |
|
| 134 | + } |
|
| 135 | + return current( $this->term_converter->get( $object_id, $context ) ); |
|
| 136 | + } else { |
|
| 137 | + return false; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param $context int Instance of Jsonld_Context_Enum |
|
| 143 | + * |
|
| 144 | + * @return array |
|
| 145 | + */ |
|
| 146 | + public function render( $context ) { |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * This is possible because the toString() method of |
|
| 150 | + * Wordpress_Content_Id is used to get the unique value. |
|
| 151 | + */ |
|
| 152 | + $unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING ); |
|
| 153 | + |
|
| 154 | + $result = array( $this->main_jsonld ); |
|
| 155 | + |
|
| 156 | + foreach ( $unique_content_ids as $unique_content_id ) { |
|
| 157 | + $result[] = $this->expand( $unique_content_id, $context ); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // Filter out the false and empty results. |
|
| 161 | + return array_filter( $result ); |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | 165 | } |
@@ -34,13 +34,13 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | private $term_converter; |
| 36 | 36 | |
| 37 | - public function __construct( $main_jsonld, $post_converter, $term_converter ) { |
|
| 37 | + public function __construct($main_jsonld, $post_converter, $term_converter) { |
|
| 38 | 38 | $this->main_jsonld = $main_jsonld; |
| 39 | 39 | $this->post_converter = $post_converter; |
| 40 | 40 | $this->term_converter = $term_converter; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function set_main_jsonld( $main_jsonld ) { |
|
| 43 | + public function set_main_jsonld($main_jsonld) { |
|
| 44 | 44 | $this->main_jsonld = $main_jsonld; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -53,11 +53,11 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @return Graph |
| 55 | 55 | */ |
| 56 | - public function add_references( $refs ) { |
|
| 57 | - Assertions::is_array( $refs ); |
|
| 56 | + public function add_references($refs) { |
|
| 57 | + Assertions::is_array($refs); |
|
| 58 | 58 | |
| 59 | - foreach ( $refs as $ref ) { |
|
| 60 | - $this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref ); |
|
| 59 | + foreach ($refs as $ref) { |
|
| 60 | + $this->referenced_content_ids[] = Wordpress_Content_Id::create_post($ref); |
|
| 61 | 61 | } |
| 62 | 62 | return $this; |
| 63 | 63 | } |
@@ -73,21 +73,21 @@ discard block |
||
| 73 | 73 | * |
| 74 | 74 | * @return Graph |
| 75 | 75 | */ |
| 76 | - public function add_required_reference_infos( $references_infos ) { |
|
| 76 | + public function add_required_reference_infos($references_infos) { |
|
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * @var $required_references array<\Wordlift_Property_Entity_Reference> |
| 80 | 80 | */ |
| 81 | 81 | $required_references = array_filter( |
| 82 | 82 | $references_infos, |
| 83 | - function ( $item ) { |
|
| 84 | - return isset( $item['reference'] ) && |
|
| 83 | + function($item) { |
|
| 84 | + return isset($item['reference']) && |
|
| 85 | 85 | // Check that the reference is required |
| 86 | 86 | $item['reference']->get_required(); |
| 87 | 87 | } |
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | - foreach ( $required_references as $required_reference ) { |
|
| 90 | + foreach ($required_references as $required_reference) { |
|
| 91 | 91 | $this->referenced_content_ids[] = new Wordpress_Content_Id( |
| 92 | 92 | $required_reference->get_id(), |
| 93 | 93 | $required_reference->get_type() |
@@ -102,9 +102,9 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | * @return Graph |
| 104 | 104 | */ |
| 105 | - public function add_relations( $relations ) { |
|
| 105 | + public function add_relations($relations) { |
|
| 106 | 106 | |
| 107 | - foreach ( $relations->toArray() as $relation ) { |
|
| 107 | + foreach ($relations->toArray() as $relation) { |
|
| 108 | 108 | |
| 109 | 109 | $this->referenced_content_ids[] = $relation->get_object(); |
| 110 | 110 | |
@@ -118,21 +118,21 @@ discard block |
||
| 118 | 118 | * @param $context int |
| 119 | 119 | * @return array|bool |
| 120 | 120 | */ |
| 121 | - private function expand( $content_id, $context ) { |
|
| 121 | + private function expand($content_id, $context) { |
|
| 122 | 122 | $object_id = $content_id->get_id(); |
| 123 | 123 | $object_type = $content_id->get_type(); |
| 124 | 124 | |
| 125 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 125 | + if ($object_type === Object_Type_Enum::POST) { |
|
| 126 | 126 | $references = array(); |
| 127 | 127 | $reference_info = array(); |
| 128 | 128 | $relations = new Relations(); |
| 129 | - return $this->post_converter->convert( $object_id, $references, $reference_info, $relations ); |
|
| 130 | - } elseif ( $object_type === Object_Type_Enum::TERM ) { |
|
| 129 | + return $this->post_converter->convert($object_id, $references, $reference_info, $relations); |
|
| 130 | + } elseif ($object_type === Object_Type_Enum::TERM) { |
|
| 131 | 131 | // Skip the Uncategorized term. |
| 132 | - if ( 1 === $object_id ) { |
|
| 132 | + if (1 === $object_id) { |
|
| 133 | 133 | return false; |
| 134 | 134 | } |
| 135 | - return current( $this->term_converter->get( $object_id, $context ) ); |
|
| 135 | + return current($this->term_converter->get($object_id, $context)); |
|
| 136 | 136 | } else { |
| 137 | 137 | return false; |
| 138 | 138 | } |
@@ -143,22 +143,22 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return array |
| 145 | 145 | */ |
| 146 | - public function render( $context ) { |
|
| 146 | + public function render($context) { |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * This is possible because the toString() method of |
| 150 | 150 | * Wordpress_Content_Id is used to get the unique value. |
| 151 | 151 | */ |
| 152 | - $unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING ); |
|
| 152 | + $unique_content_ids = array_unique($this->referenced_content_ids, SORT_STRING); |
|
| 153 | 153 | |
| 154 | - $result = array( $this->main_jsonld ); |
|
| 154 | + $result = array($this->main_jsonld); |
|
| 155 | 155 | |
| 156 | - foreach ( $unique_content_ids as $unique_content_id ) { |
|
| 157 | - $result[] = $this->expand( $unique_content_id, $context ); |
|
| 156 | + foreach ($unique_content_ids as $unique_content_id) { |
|
| 157 | + $result[] = $this->expand($unique_content_id, $context); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Filter out the false and empty results. |
| 161 | - return array_filter( $result ); |
|
| 161 | + return array_filter($result); |
|
| 162 | 162 | |
| 163 | 163 | } |
| 164 | 164 | |
@@ -24,122 +24,122 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * The JSON-LD context. |
|
| 29 | - * |
|
| 30 | - * @since 3.10.0 |
|
| 31 | - */ |
|
| 32 | - const CONTEXT = 'http://schema.org'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 36 | - * |
|
| 37 | - * @since 3.10.0 |
|
| 38 | - * @access protected |
|
| 39 | - * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 40 | - */ |
|
| 41 | - protected $entity_type_service; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * A {@link Wordlift_User_Service} instance. |
|
| 45 | - * |
|
| 46 | - * @since 3.10.0 |
|
| 47 | - * @access private |
|
| 48 | - * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 49 | - */ |
|
| 50 | - protected $user_service; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * A {@link Wordlift_Attachment_Service} instance. |
|
| 54 | - * |
|
| 55 | - * @since 3.10.0 |
|
| 56 | - * @access private |
|
| 57 | - * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 58 | - */ |
|
| 59 | - protected $attachment_service; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var Wordlift_Property_Getter |
|
| 63 | - */ |
|
| 64 | - private $property_getter; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var Relation_Service_Interface |
|
| 68 | - */ |
|
| 69 | - private $relation_service; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 73 | - * |
|
| 74 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 75 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 76 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 77 | - * @param \Wordlift_Property_Getter $property_getter |
|
| 78 | - * |
|
| 79 | - * @since 3.10.0 |
|
| 80 | - */ |
|
| 81 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
| 82 | - $this->entity_type_service = $entity_type_service; |
|
| 83 | - $this->user_service = $user_service; |
|
| 84 | - $this->attachment_service = $attachment_service; |
|
| 85 | - $this->property_getter = $property_getter; |
|
| 86 | - |
|
| 87 | - $this->relation_service = Relation_Service::get_instance(); |
|
| 88 | - |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 93 | - * found while processing the post is set in the $references array. |
|
| 94 | - * |
|
| 95 | - * @param int $post_id The post id. |
|
| 96 | - * @param array $references An array of entity references. |
|
| 97 | - * @param array $relations |
|
| 98 | - * |
|
| 99 | - * @return array A JSON-LD array. |
|
| 100 | - * @since 3.10.0 |
|
| 101 | - */ |
|
| 102 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 103 | - public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) { |
|
| 104 | - |
|
| 105 | - // Get the post instance. |
|
| 106 | - $post = get_post( $post_id ); |
|
| 107 | - if ( null === $post ) { |
|
| 108 | - // Post not found. |
|
| 109 | - return null; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // Get the post URI @id. |
|
| 113 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
| 114 | - if ( $id === null ) { |
|
| 115 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /* |
|
| 27 | + /** |
|
| 28 | + * The JSON-LD context. |
|
| 29 | + * |
|
| 30 | + * @since 3.10.0 |
|
| 31 | + */ |
|
| 32 | + const CONTEXT = 'http://schema.org'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 36 | + * |
|
| 37 | + * @since 3.10.0 |
|
| 38 | + * @access protected |
|
| 39 | + * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 40 | + */ |
|
| 41 | + protected $entity_type_service; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * A {@link Wordlift_User_Service} instance. |
|
| 45 | + * |
|
| 46 | + * @since 3.10.0 |
|
| 47 | + * @access private |
|
| 48 | + * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 49 | + */ |
|
| 50 | + protected $user_service; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * A {@link Wordlift_Attachment_Service} instance. |
|
| 54 | + * |
|
| 55 | + * @since 3.10.0 |
|
| 56 | + * @access private |
|
| 57 | + * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 58 | + */ |
|
| 59 | + protected $attachment_service; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var Wordlift_Property_Getter |
|
| 63 | + */ |
|
| 64 | + private $property_getter; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var Relation_Service_Interface |
|
| 68 | + */ |
|
| 69 | + private $relation_service; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 73 | + * |
|
| 74 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 75 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 76 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 77 | + * @param \Wordlift_Property_Getter $property_getter |
|
| 78 | + * |
|
| 79 | + * @since 3.10.0 |
|
| 80 | + */ |
|
| 81 | + public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
| 82 | + $this->entity_type_service = $entity_type_service; |
|
| 83 | + $this->user_service = $user_service; |
|
| 84 | + $this->attachment_service = $attachment_service; |
|
| 85 | + $this->property_getter = $property_getter; |
|
| 86 | + |
|
| 87 | + $this->relation_service = Relation_Service::get_instance(); |
|
| 88 | + |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 93 | + * found while processing the post is set in the $references array. |
|
| 94 | + * |
|
| 95 | + * @param int $post_id The post id. |
|
| 96 | + * @param array $references An array of entity references. |
|
| 97 | + * @param array $relations |
|
| 98 | + * |
|
| 99 | + * @return array A JSON-LD array. |
|
| 100 | + * @since 3.10.0 |
|
| 101 | + */ |
|
| 102 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 103 | + public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) { |
|
| 104 | + |
|
| 105 | + // Get the post instance. |
|
| 106 | + $post = get_post( $post_id ); |
|
| 107 | + if ( null === $post ) { |
|
| 108 | + // Post not found. |
|
| 109 | + return null; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // Get the post URI @id. |
|
| 113 | + $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
| 114 | + if ( $id === null ) { |
|
| 115 | + $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /* |
|
| 119 | 119 | * The `types` variable holds one or more entity types. The `type` variable isn't used anymore. |
| 120 | 120 | * |
| 121 | 121 | * @since 3.20.0 We support more than one entity type. |
| 122 | 122 | * |
| 123 | 123 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 124 | 124 | */ |
| 125 | - // Get the entity @type. We consider `post` BlogPostings. |
|
| 126 | - // $type = $this->entity_type_service->get( $post_id ); |
|
| 127 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 128 | - $type = self::make_one( $types ); |
|
| 129 | - |
|
| 130 | - // Prepare the response. |
|
| 131 | - $jsonld = array( |
|
| 132 | - '@context' => self::CONTEXT, |
|
| 133 | - '@id' => $id, |
|
| 134 | - '@type' => $type, |
|
| 135 | - ); |
|
| 136 | - |
|
| 137 | - if ( post_type_supports( $post->post_type, 'excerpt' ) ) { |
|
| 138 | - $jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 142 | - /* |
|
| 125 | + // Get the entity @type. We consider `post` BlogPostings. |
|
| 126 | + // $type = $this->entity_type_service->get( $post_id ); |
|
| 127 | + $types = $this->entity_type_service->get_names( $post_id ); |
|
| 128 | + $type = self::make_one( $types ); |
|
| 129 | + |
|
| 130 | + // Prepare the response. |
|
| 131 | + $jsonld = array( |
|
| 132 | + '@context' => self::CONTEXT, |
|
| 133 | + '@id' => $id, |
|
| 134 | + '@type' => $type, |
|
| 135 | + ); |
|
| 136 | + |
|
| 137 | + if ( post_type_supports( $post->post_type, 'excerpt' ) ) { |
|
| 138 | + $jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 142 | + /* |
|
| 143 | 143 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 144 | 144 | * because the content is written elsewhere. |
| 145 | 145 | * |
@@ -147,336 +147,336 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | * @since 3.20.0 |
| 149 | 149 | */ |
| 150 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 151 | - if ( ! empty( $post_content ) || in_array( 'Product', (array) $type, true ) ) { |
|
| 152 | - // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 153 | - // main entity for the specified URL. It might be however that the |
|
| 154 | - // post/page is actually about another specific entity. How WL deals |
|
| 155 | - // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 156 | - // |
|
| 157 | - // See http://schema.org/mainEntityOfPage |
|
| 158 | - // |
|
| 159 | - // No need to specify `'@type' => 'WebPage'. |
|
| 160 | - // |
|
| 161 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 162 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
| 166 | - * |
|
| 167 | - * @since 3.27.7 |
|
| 168 | - */ |
|
| 169 | - if ( in_array( |
|
| 170 | - $type, |
|
| 171 | - array( |
|
| 172 | - 'Occupation', |
|
| 173 | - 'OccupationAggregationByEmployer', |
|
| 174 | - ), |
|
| 175 | - true |
|
| 176 | - ) ) { |
|
| 177 | - $jsonld['mainEntityOfPage'] = array( |
|
| 178 | - '@type' => 'WebPage', |
|
| 179 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
| 180 | - ); |
|
| 181 | - } |
|
| 182 | - }; |
|
| 183 | - |
|
| 184 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 185 | - |
|
| 186 | - // Naveen: Get the entities referenced by this post and set it to the `references` |
|
| 187 | - // array so that the caller can do further processing, such as printing out |
|
| 188 | - // more of those references. |
|
| 189 | - // |
|
| 190 | - // David: you can't set this to the `references`, because `references` it's an array of post |
|
| 191 | - // IDs and client are using this. As of 3.42.1 (2023-04-20) I am refactoring this to use a |
|
| 192 | - // new variable $relations. |
|
| 193 | - // ** @var Object_Relation_Service $object_relation_service */ |
|
| 194 | - // $object_relation_service = Object_Relation_Service::get_instance(); |
|
| 195 | - // $references_without_locations = $object_relation_service->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 196 | - |
|
| 197 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 198 | - // Populate the relations. |
|
| 199 | - $this->relation_service->add_relations( $content_id, $relations ); |
|
| 200 | - // We copy the relations to the legacy references array. Please note that the references |
|
| 201 | - // array contains only post IDs, while Relations may contain other content types (e.g. |
|
| 202 | - // terms). |
|
| 203 | - $this->add_relations_to_references( $relations, $references ); |
|
| 204 | - |
|
| 205 | - return $jsonld; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Copies the Post relations to referenced post IDs (legacy). |
|
| 210 | - * |
|
| 211 | - * @param Relations $relations |
|
| 212 | - */ |
|
| 213 | - private function add_relations_to_references( $relations, &$references ) { |
|
| 214 | - if ( ! is_a( $relations, 'Wordlift\Relation\Relations' ) ) { |
|
| 215 | - return; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - foreach ( $relations->toArray() as $relation ) { |
|
| 219 | - $object = $relation->get_object(); |
|
| 220 | - $object_id = $object->get_id(); |
|
| 221 | - if ( $object->get_type() === Object_Type_Enum::POST && ! in_array( $object_id, $references, true ) ) { |
|
| 222 | - $references[] = $object_id; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // private function add_location_reference_objects() { |
|
| 228 | - // array_reduce( |
|
| 229 | - // $references_without_locations, |
|
| 230 | - // function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 231 | - // ** |
|
| 232 | - // * @var $reference Reference |
|
| 233 | - // */ |
|
| 234 | - // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 235 | - // if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 236 | - // && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 237 | - // && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 238 | - // |
|
| 239 | - // return $carry; |
|
| 240 | - // } |
|
| 241 | - // $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 242 | - // $post_location_references = array_map( |
|
| 243 | - // function ( $post_id ) { |
|
| 244 | - // return new Post_Reference( $post_id ); |
|
| 245 | - // }, |
|
| 246 | - // $post_location_ids |
|
| 247 | - // ); |
|
| 248 | - // |
|
| 249 | - // return array_merge( $carry, $post_location_references ); |
|
| 250 | - // }, |
|
| 251 | - // array() |
|
| 252 | - // ); |
|
| 253 | - // } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 257 | - * part since it is set with the '@context'. |
|
| 258 | - * |
|
| 259 | - * @param string $value The property value. |
|
| 260 | - * |
|
| 261 | - * @return string The property value without the context. |
|
| 262 | - * @since 3.10.0 |
|
| 263 | - */ |
|
| 264 | - public function relative_to_context( $value ) { |
|
| 265 | - return ! is_array( $value ) && 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Set the images, by looking for embedded images, for images loaded via the |
|
| 270 | - * gallery and for the featured image. |
|
| 271 | - * |
|
| 272 | - * Uses the cache service to store the results of this function for a day. |
|
| 273 | - * |
|
| 274 | - * @param $attachment_service Wordlift_Attachment_Service |
|
| 275 | - * @param WP_Post $post The target {@link WP_Post}. |
|
| 276 | - * @param array $jsonld The JSON-LD array. |
|
| 277 | - * |
|
| 278 | - * @since 3.10.0 |
|
| 279 | - */ |
|
| 280 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 281 | - |
|
| 282 | - // Prepare the attachment ids array. |
|
| 283 | - $ids = array(); |
|
| 284 | - |
|
| 285 | - // Set the thumbnail id as first attachment id, if any. |
|
| 286 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 287 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 288 | - $ids[] = $thumbnail_id; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - // For the time being the following is being removed since the query |
|
| 292 | - // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 293 | - // |
|
| 294 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 295 | - // |
|
| 296 | - // Get the embeds, removing existing ids. |
|
| 297 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 298 | - if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
| 299 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 300 | - } else { |
|
| 301 | - $embeds = array(); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // Get the gallery, removing existing ids. |
|
| 305 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 306 | - |
|
| 307 | - // Map the attachment ids to images' data structured for schema.org use. |
|
| 308 | - $images_with_sizes = array_filter( |
|
| 309 | - array_reduce( |
|
| 310 | - array_merge( $ids, $embeds, $gallery ), |
|
| 311 | - function ( $carry, $item ) { |
|
| 312 | - /* |
|
| 150 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 151 | + if ( ! empty( $post_content ) || in_array( 'Product', (array) $type, true ) ) { |
|
| 152 | + // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 153 | + // main entity for the specified URL. It might be however that the |
|
| 154 | + // post/page is actually about another specific entity. How WL deals |
|
| 155 | + // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 156 | + // |
|
| 157 | + // See http://schema.org/mainEntityOfPage |
|
| 158 | + // |
|
| 159 | + // No need to specify `'@type' => 'WebPage'. |
|
| 160 | + // |
|
| 161 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 162 | + $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
| 166 | + * |
|
| 167 | + * @since 3.27.7 |
|
| 168 | + */ |
|
| 169 | + if ( in_array( |
|
| 170 | + $type, |
|
| 171 | + array( |
|
| 172 | + 'Occupation', |
|
| 173 | + 'OccupationAggregationByEmployer', |
|
| 174 | + ), |
|
| 175 | + true |
|
| 176 | + ) ) { |
|
| 177 | + $jsonld['mainEntityOfPage'] = array( |
|
| 178 | + '@type' => 'WebPage', |
|
| 179 | + 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
| 180 | + ); |
|
| 181 | + } |
|
| 182 | + }; |
|
| 183 | + |
|
| 184 | + $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 185 | + |
|
| 186 | + // Naveen: Get the entities referenced by this post and set it to the `references` |
|
| 187 | + // array so that the caller can do further processing, such as printing out |
|
| 188 | + // more of those references. |
|
| 189 | + // |
|
| 190 | + // David: you can't set this to the `references`, because `references` it's an array of post |
|
| 191 | + // IDs and client are using this. As of 3.42.1 (2023-04-20) I am refactoring this to use a |
|
| 192 | + // new variable $relations. |
|
| 193 | + // ** @var Object_Relation_Service $object_relation_service */ |
|
| 194 | + // $object_relation_service = Object_Relation_Service::get_instance(); |
|
| 195 | + // $references_without_locations = $object_relation_service->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 196 | + |
|
| 197 | + $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 198 | + // Populate the relations. |
|
| 199 | + $this->relation_service->add_relations( $content_id, $relations ); |
|
| 200 | + // We copy the relations to the legacy references array. Please note that the references |
|
| 201 | + // array contains only post IDs, while Relations may contain other content types (e.g. |
|
| 202 | + // terms). |
|
| 203 | + $this->add_relations_to_references( $relations, $references ); |
|
| 204 | + |
|
| 205 | + return $jsonld; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Copies the Post relations to referenced post IDs (legacy). |
|
| 210 | + * |
|
| 211 | + * @param Relations $relations |
|
| 212 | + */ |
|
| 213 | + private function add_relations_to_references( $relations, &$references ) { |
|
| 214 | + if ( ! is_a( $relations, 'Wordlift\Relation\Relations' ) ) { |
|
| 215 | + return; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + foreach ( $relations->toArray() as $relation ) { |
|
| 219 | + $object = $relation->get_object(); |
|
| 220 | + $object_id = $object->get_id(); |
|
| 221 | + if ( $object->get_type() === Object_Type_Enum::POST && ! in_array( $object_id, $references, true ) ) { |
|
| 222 | + $references[] = $object_id; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // private function add_location_reference_objects() { |
|
| 228 | + // array_reduce( |
|
| 229 | + // $references_without_locations, |
|
| 230 | + // function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 231 | + // ** |
|
| 232 | + // * @var $reference Reference |
|
| 233 | + // */ |
|
| 234 | + // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 235 | + // if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 236 | + // && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 237 | + // && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 238 | + // |
|
| 239 | + // return $carry; |
|
| 240 | + // } |
|
| 241 | + // $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 242 | + // $post_location_references = array_map( |
|
| 243 | + // function ( $post_id ) { |
|
| 244 | + // return new Post_Reference( $post_id ); |
|
| 245 | + // }, |
|
| 246 | + // $post_location_ids |
|
| 247 | + // ); |
|
| 248 | + // |
|
| 249 | + // return array_merge( $carry, $post_location_references ); |
|
| 250 | + // }, |
|
| 251 | + // array() |
|
| 252 | + // ); |
|
| 253 | + // } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 257 | + * part since it is set with the '@context'. |
|
| 258 | + * |
|
| 259 | + * @param string $value The property value. |
|
| 260 | + * |
|
| 261 | + * @return string The property value without the context. |
|
| 262 | + * @since 3.10.0 |
|
| 263 | + */ |
|
| 264 | + public function relative_to_context( $value ) { |
|
| 265 | + return ! is_array( $value ) && 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Set the images, by looking for embedded images, for images loaded via the |
|
| 270 | + * gallery and for the featured image. |
|
| 271 | + * |
|
| 272 | + * Uses the cache service to store the results of this function for a day. |
|
| 273 | + * |
|
| 274 | + * @param $attachment_service Wordlift_Attachment_Service |
|
| 275 | + * @param WP_Post $post The target {@link WP_Post}. |
|
| 276 | + * @param array $jsonld The JSON-LD array. |
|
| 277 | + * |
|
| 278 | + * @since 3.10.0 |
|
| 279 | + */ |
|
| 280 | + public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 281 | + |
|
| 282 | + // Prepare the attachment ids array. |
|
| 283 | + $ids = array(); |
|
| 284 | + |
|
| 285 | + // Set the thumbnail id as first attachment id, if any. |
|
| 286 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 287 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 288 | + $ids[] = $thumbnail_id; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + // For the time being the following is being removed since the query |
|
| 292 | + // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 293 | + // |
|
| 294 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 295 | + // |
|
| 296 | + // Get the embeds, removing existing ids. |
|
| 297 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 298 | + if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
| 299 | + $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 300 | + } else { |
|
| 301 | + $embeds = array(); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // Get the gallery, removing existing ids. |
|
| 305 | + $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 306 | + |
|
| 307 | + // Map the attachment ids to images' data structured for schema.org use. |
|
| 308 | + $images_with_sizes = array_filter( |
|
| 309 | + array_reduce( |
|
| 310 | + array_merge( $ids, $embeds, $gallery ), |
|
| 311 | + function ( $carry, $item ) { |
|
| 312 | + /* |
|
| 313 | 313 | * @todo: we're not sure that we're getting attachment data here, we |
| 314 | 314 | * should filter `false`s. |
| 315 | 315 | */ |
| 316 | 316 | |
| 317 | - $sources = array_merge( |
|
| 318 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 319 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 320 | - ); |
|
| 321 | - |
|
| 322 | - $sources_with_image = array_filter( |
|
| 323 | - $sources, |
|
| 324 | - function ( $source ) { |
|
| 325 | - return ! empty( $source[0] ); |
|
| 326 | - } |
|
| 327 | - ); |
|
| 328 | - |
|
| 329 | - // Get the attachment data. |
|
| 330 | - // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 331 | - |
|
| 332 | - // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 333 | - |
|
| 334 | - // Bail if image is not found. |
|
| 335 | - // In some cases, you can delete the image from the database |
|
| 336 | - // or from uploads dir, but the image id still exists as featured image |
|
| 337 | - // or in [gallery] shortcode. |
|
| 338 | - // if ( empty( $attachment[0] ) ) { |
|
| 339 | - if ( empty( $sources_with_image ) ) { |
|
| 340 | - return $carry; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - // Merge the arrays. |
|
| 344 | - return array_merge( |
|
| 345 | - $carry, |
|
| 346 | - $sources_with_image |
|
| 347 | - ); |
|
| 348 | - }, |
|
| 349 | - // Initial array. |
|
| 350 | - |
|
| 351 | - array() |
|
| 352 | - ) |
|
| 353 | - ); |
|
| 354 | - |
|
| 355 | - // Refactor data as per schema.org specifications. |
|
| 356 | - $images = array_map( |
|
| 357 | - function ( $attachment ) { |
|
| 358 | - return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 359 | - array( |
|
| 360 | - '@type' => 'ImageObject', |
|
| 361 | - 'url' => $attachment[0], |
|
| 362 | - ), |
|
| 363 | - $attachment |
|
| 364 | - ); |
|
| 365 | - }, |
|
| 366 | - $images_with_sizes |
|
| 367 | - ); |
|
| 368 | - |
|
| 369 | - // Add images if present. |
|
| 370 | - if ( 0 < count( $images ) ) { |
|
| 371 | - $jsonld['image'] = $images; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * If the provided array of values contains only one value, then one single |
|
| 378 | - * value is returned, otherwise the original array is returned. |
|
| 379 | - * |
|
| 380 | - * @param array $value An array of values. |
|
| 381 | - * |
|
| 382 | - * @return mixed|array A single value or the original array. |
|
| 383 | - * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 384 | - * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 385 | - * @since 3.8.0 |
|
| 386 | - * @access private |
|
| 387 | - */ |
|
| 388 | - protected static function make_one( $value ) { |
|
| 389 | - |
|
| 390 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Process the provided array by adding the width / height if the values |
|
| 395 | - * are available and are greater than 0. |
|
| 396 | - * |
|
| 397 | - * @param array $image The `ImageObject` array. |
|
| 398 | - * @param array $attachment The attachment array. |
|
| 399 | - * |
|
| 400 | - * @return array The enriched `ImageObject` array. |
|
| 401 | - * @since 3.14.0 |
|
| 402 | - */ |
|
| 403 | - public static function set_image_size( $image, $attachment ) { |
|
| 404 | - |
|
| 405 | - // If you specify a "width" or "height" value you should leave out |
|
| 406 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 407 | - // |
|
| 408 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 409 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 410 | - $image['width'] = $attachment[1]; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 414 | - $image['height'] = $attachment[2]; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - return $image; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property |
|
| 422 | - * for the post entity type. |
|
| 423 | - * |
|
| 424 | - * @param array $jsonld The JSON-LD array. |
|
| 425 | - * @param array $fields The entity types field array. |
|
| 426 | - * @param WP_Post $post The target {@link WP_Post} instance. |
|
| 427 | - * @param array $references The references array. |
|
| 428 | - * |
|
| 429 | - * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
|
| 430 | - * the {@link Wordlift_Schema_Service} class. |
|
| 431 | - */ |
|
| 432 | - protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
| 433 | - |
|
| 434 | - // Set a reference to use in closures. |
|
| 435 | - $converter = $this; |
|
| 436 | - |
|
| 437 | - // Try each field on the entity. |
|
| 438 | - foreach ( $fields as $key => $value ) { |
|
| 439 | - |
|
| 440 | - // Get the predicate. |
|
| 441 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
| 442 | - |
|
| 443 | - // Get the value, the property service will get the right extractor |
|
| 444 | - // for that property. |
|
| 445 | - $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
| 446 | - |
|
| 447 | - if ( empty( $value ) ) { |
|
| 448 | - continue; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - // Map the value to the property name. |
|
| 452 | - // If we got an array with just one value, we return that one value. |
|
| 453 | - // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
| 454 | - $jsonld[ $name ] = self::make_one( |
|
| 455 | - array_map( |
|
| 456 | - function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
| 457 | - |
|
| 458 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 459 | - |
|
| 460 | - $url = $item->get_url(); |
|
| 461 | - |
|
| 462 | - // The refactored converters require the entity id. |
|
| 463 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 464 | - $references[] = $item->to_reference(); |
|
| 465 | - |
|
| 466 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 467 | - $references_infos[] = array( 'reference' => $item ); |
|
| 468 | - |
|
| 469 | - return array( '@id' => $url ); |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - return $converter->relative_to_context( $item ); |
|
| 473 | - }, |
|
| 474 | - $value |
|
| 475 | - ) |
|
| 476 | - ); |
|
| 477 | - |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - } |
|
| 317 | + $sources = array_merge( |
|
| 318 | + Wordlift_Image_Service::get_sources( $item ), |
|
| 319 | + array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 320 | + ); |
|
| 321 | + |
|
| 322 | + $sources_with_image = array_filter( |
|
| 323 | + $sources, |
|
| 324 | + function ( $source ) { |
|
| 325 | + return ! empty( $source[0] ); |
|
| 326 | + } |
|
| 327 | + ); |
|
| 328 | + |
|
| 329 | + // Get the attachment data. |
|
| 330 | + // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 331 | + |
|
| 332 | + // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 333 | + |
|
| 334 | + // Bail if image is not found. |
|
| 335 | + // In some cases, you can delete the image from the database |
|
| 336 | + // or from uploads dir, but the image id still exists as featured image |
|
| 337 | + // or in [gallery] shortcode. |
|
| 338 | + // if ( empty( $attachment[0] ) ) { |
|
| 339 | + if ( empty( $sources_with_image ) ) { |
|
| 340 | + return $carry; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + // Merge the arrays. |
|
| 344 | + return array_merge( |
|
| 345 | + $carry, |
|
| 346 | + $sources_with_image |
|
| 347 | + ); |
|
| 348 | + }, |
|
| 349 | + // Initial array. |
|
| 350 | + |
|
| 351 | + array() |
|
| 352 | + ) |
|
| 353 | + ); |
|
| 354 | + |
|
| 355 | + // Refactor data as per schema.org specifications. |
|
| 356 | + $images = array_map( |
|
| 357 | + function ( $attachment ) { |
|
| 358 | + return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 359 | + array( |
|
| 360 | + '@type' => 'ImageObject', |
|
| 361 | + 'url' => $attachment[0], |
|
| 362 | + ), |
|
| 363 | + $attachment |
|
| 364 | + ); |
|
| 365 | + }, |
|
| 366 | + $images_with_sizes |
|
| 367 | + ); |
|
| 368 | + |
|
| 369 | + // Add images if present. |
|
| 370 | + if ( 0 < count( $images ) ) { |
|
| 371 | + $jsonld['image'] = $images; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * If the provided array of values contains only one value, then one single |
|
| 378 | + * value is returned, otherwise the original array is returned. |
|
| 379 | + * |
|
| 380 | + * @param array $value An array of values. |
|
| 381 | + * |
|
| 382 | + * @return mixed|array A single value or the original array. |
|
| 383 | + * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 384 | + * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 385 | + * @since 3.8.0 |
|
| 386 | + * @access private |
|
| 387 | + */ |
|
| 388 | + protected static function make_one( $value ) { |
|
| 389 | + |
|
| 390 | + return 1 === count( $value ) ? $value[0] : $value; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Process the provided array by adding the width / height if the values |
|
| 395 | + * are available and are greater than 0. |
|
| 396 | + * |
|
| 397 | + * @param array $image The `ImageObject` array. |
|
| 398 | + * @param array $attachment The attachment array. |
|
| 399 | + * |
|
| 400 | + * @return array The enriched `ImageObject` array. |
|
| 401 | + * @since 3.14.0 |
|
| 402 | + */ |
|
| 403 | + public static function set_image_size( $image, $attachment ) { |
|
| 404 | + |
|
| 405 | + // If you specify a "width" or "height" value you should leave out |
|
| 406 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 407 | + // |
|
| 408 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 409 | + if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 410 | + $image['width'] = $attachment[1]; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 414 | + $image['height'] = $attachment[2]; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + return $image; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property |
|
| 422 | + * for the post entity type. |
|
| 423 | + * |
|
| 424 | + * @param array $jsonld The JSON-LD array. |
|
| 425 | + * @param array $fields The entity types field array. |
|
| 426 | + * @param WP_Post $post The target {@link WP_Post} instance. |
|
| 427 | + * @param array $references The references array. |
|
| 428 | + * |
|
| 429 | + * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
|
| 430 | + * the {@link Wordlift_Schema_Service} class. |
|
| 431 | + */ |
|
| 432 | + protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
| 433 | + |
|
| 434 | + // Set a reference to use in closures. |
|
| 435 | + $converter = $this; |
|
| 436 | + |
|
| 437 | + // Try each field on the entity. |
|
| 438 | + foreach ( $fields as $key => $value ) { |
|
| 439 | + |
|
| 440 | + // Get the predicate. |
|
| 441 | + $name = $this->relative_to_context( $value['predicate'] ); |
|
| 442 | + |
|
| 443 | + // Get the value, the property service will get the right extractor |
|
| 444 | + // for that property. |
|
| 445 | + $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
| 446 | + |
|
| 447 | + if ( empty( $value ) ) { |
|
| 448 | + continue; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + // Map the value to the property name. |
|
| 452 | + // If we got an array with just one value, we return that one value. |
|
| 453 | + // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
| 454 | + $jsonld[ $name ] = self::make_one( |
|
| 455 | + array_map( |
|
| 456 | + function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
| 457 | + |
|
| 458 | + if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 459 | + |
|
| 460 | + $url = $item->get_url(); |
|
| 461 | + |
|
| 462 | + // The refactored converters require the entity id. |
|
| 463 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 464 | + $references[] = $item->to_reference(); |
|
| 465 | + |
|
| 466 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
|
| 467 | + $references_infos[] = array( 'reference' => $item ); |
|
| 468 | + |
|
| 469 | + return array( '@id' => $url ); |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + return $converter->relative_to_context( $item ); |
|
| 473 | + }, |
|
| 474 | + $value |
|
| 475 | + ) |
|
| 476 | + ); |
|
| 477 | + |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + } |
|
| 481 | 481 | |
| 482 | 482 | } |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | * |
| 79 | 79 | * @since 3.10.0 |
| 80 | 80 | */ |
| 81 | - public function __construct( $entity_type_service, $user_service, $attachment_service, $property_getter ) { |
|
| 81 | + public function __construct($entity_type_service, $user_service, $attachment_service, $property_getter) { |
|
| 82 | 82 | $this->entity_type_service = $entity_type_service; |
| 83 | 83 | $this->user_service = $user_service; |
| 84 | 84 | $this->attachment_service = $attachment_service; |
@@ -100,19 +100,19 @@ discard block |
||
| 100 | 100 | * @since 3.10.0 |
| 101 | 101 | */ |
| 102 | 102 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 103 | - public function convert( $post_id, &$references = array(), &$references_infos = array(), $relations = null ) { |
|
| 103 | + public function convert($post_id, &$references = array(), &$references_infos = array(), $relations = null) { |
|
| 104 | 104 | |
| 105 | 105 | // Get the post instance. |
| 106 | - $post = get_post( $post_id ); |
|
| 107 | - if ( null === $post ) { |
|
| 106 | + $post = get_post($post_id); |
|
| 107 | + if (null === $post) { |
|
| 108 | 108 | // Post not found. |
| 109 | 109 | return null; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // Get the post URI @id. |
| 113 | - $id = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID ); |
|
| 114 | - if ( $id === null ) { |
|
| 115 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 113 | + $id = Wordlift_Entity_Service::get_instance()->get_uri($post->ID); |
|
| 114 | + if ($id === null) { |
|
| 115 | + $id = 'get_uri returned null, dataset is '.wl_configuration_get_redlink_dataset_uri(); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /* |
@@ -124,8 +124,8 @@ discard block |
||
| 124 | 124 | */ |
| 125 | 125 | // Get the entity @type. We consider `post` BlogPostings. |
| 126 | 126 | // $type = $this->entity_type_service->get( $post_id ); |
| 127 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 128 | - $type = self::make_one( $types ); |
|
| 127 | + $types = $this->entity_type_service->get_names($post_id); |
|
| 128 | + $type = self::make_one($types); |
|
| 129 | 129 | |
| 130 | 130 | // Prepare the response. |
| 131 | 131 | $jsonld = array( |
@@ -134,8 +134,8 @@ discard block |
||
| 134 | 134 | '@type' => $type, |
| 135 | 135 | ); |
| 136 | 136 | |
| 137 | - if ( post_type_supports( $post->post_type, 'excerpt' ) ) { |
|
| 138 | - $jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ); |
|
| 137 | + if (post_type_supports($post->post_type, 'excerpt')) { |
|
| 138 | + $jsonld['description'] = Wordlift_Post_Excerpt_Helper::get_text_excerpt($post); |
|
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | // Set the `mainEntityOfPage` property if the post has some contents. |
@@ -147,8 +147,8 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | * @since 3.20.0 |
| 149 | 149 | */ |
| 150 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 151 | - if ( ! empty( $post_content ) || in_array( 'Product', (array) $type, true ) ) { |
|
| 150 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 151 | + if ( ! empty($post_content) || in_array('Product', (array) $type, true)) { |
|
| 152 | 152 | // We're setting the `mainEntityOfPage` to signal which one is the |
| 153 | 153 | // main entity for the specified URL. It might be however that the |
| 154 | 154 | // post/page is actually about another specific entity. How WL deals |
@@ -159,29 +159,29 @@ discard block |
||
| 159 | 159 | // No need to specify `'@type' => 'WebPage'. |
| 160 | 160 | // |
| 161 | 161 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
| 162 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 162 | + $jsonld['mainEntityOfPage'] = get_the_permalink($post->ID); |
|
| 163 | 163 | |
| 164 | 164 | /** |
| 165 | 165 | * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
| 166 | 166 | * |
| 167 | 167 | * @since 3.27.7 |
| 168 | 168 | */ |
| 169 | - if ( in_array( |
|
| 169 | + if (in_array( |
|
| 170 | 170 | $type, |
| 171 | 171 | array( |
| 172 | 172 | 'Occupation', |
| 173 | 173 | 'OccupationAggregationByEmployer', |
| 174 | 174 | ), |
| 175 | 175 | true |
| 176 | - ) ) { |
|
| 176 | + )) { |
|
| 177 | 177 | $jsonld['mainEntityOfPage'] = array( |
| 178 | 178 | '@type' => 'WebPage', |
| 179 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ), |
|
| 179 | + 'lastReviewed' => get_post_time('Y-m-d\TH:i:sP', true, $post, false), |
|
| 180 | 180 | ); |
| 181 | 181 | } |
| 182 | 182 | }; |
| 183 | 183 | |
| 184 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 184 | + $this->set_images($this->attachment_service, $post, $jsonld); |
|
| 185 | 185 | |
| 186 | 186 | // Naveen: Get the entities referenced by this post and set it to the `references` |
| 187 | 187 | // array so that the caller can do further processing, such as printing out |
@@ -194,13 +194,13 @@ discard block |
||
| 194 | 194 | // $object_relation_service = Object_Relation_Service::get_instance(); |
| 195 | 195 | // $references_without_locations = $object_relation_service->get_references( $post_id, Object_Type_Enum::POST ); |
| 196 | 196 | |
| 197 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 197 | + $content_id = Wordpress_Content_Id::create_post($post_id); |
|
| 198 | 198 | // Populate the relations. |
| 199 | - $this->relation_service->add_relations( $content_id, $relations ); |
|
| 199 | + $this->relation_service->add_relations($content_id, $relations); |
|
| 200 | 200 | // We copy the relations to the legacy references array. Please note that the references |
| 201 | 201 | // array contains only post IDs, while Relations may contain other content types (e.g. |
| 202 | 202 | // terms). |
| 203 | - $this->add_relations_to_references( $relations, $references ); |
|
| 203 | + $this->add_relations_to_references($relations, $references); |
|
| 204 | 204 | |
| 205 | 205 | return $jsonld; |
| 206 | 206 | } |
@@ -210,15 +210,15 @@ discard block |
||
| 210 | 210 | * |
| 211 | 211 | * @param Relations $relations |
| 212 | 212 | */ |
| 213 | - private function add_relations_to_references( $relations, &$references ) { |
|
| 214 | - if ( ! is_a( $relations, 'Wordlift\Relation\Relations' ) ) { |
|
| 213 | + private function add_relations_to_references($relations, &$references) { |
|
| 214 | + if ( ! is_a($relations, 'Wordlift\Relation\Relations')) { |
|
| 215 | 215 | return; |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | - foreach ( $relations->toArray() as $relation ) { |
|
| 218 | + foreach ($relations->toArray() as $relation) { |
|
| 219 | 219 | $object = $relation->get_object(); |
| 220 | 220 | $object_id = $object->get_id(); |
| 221 | - if ( $object->get_type() === Object_Type_Enum::POST && ! in_array( $object_id, $references, true ) ) { |
|
| 221 | + if ($object->get_type() === Object_Type_Enum::POST && ! in_array($object_id, $references, true)) { |
|
| 222 | 222 | $references[] = $object_id; |
| 223 | 223 | } |
| 224 | 224 | } |
@@ -261,8 +261,8 @@ discard block |
||
| 261 | 261 | * @return string The property value without the context. |
| 262 | 262 | * @since 3.10.0 |
| 263 | 263 | */ |
| 264 | - public function relative_to_context( $value ) { |
|
| 265 | - return ! is_array( $value ) && 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 264 | + public function relative_to_context($value) { |
|
| 265 | + return ! is_array($value) && 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value; |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | /** |
@@ -277,14 +277,14 @@ discard block |
||
| 277 | 277 | * |
| 278 | 278 | * @since 3.10.0 |
| 279 | 279 | */ |
| 280 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 280 | + public static function set_images($attachment_service, $post, &$jsonld) { |
|
| 281 | 281 | |
| 282 | 282 | // Prepare the attachment ids array. |
| 283 | 283 | $ids = array(); |
| 284 | 284 | |
| 285 | 285 | // Set the thumbnail id as first attachment id, if any. |
| 286 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 287 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 286 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
| 287 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id) { |
|
| 288 | 288 | $ids[] = $thumbnail_id; |
| 289 | 289 | } |
| 290 | 290 | |
@@ -295,34 +295,34 @@ discard block |
||
| 295 | 295 | // |
| 296 | 296 | // Get the embeds, removing existing ids. |
| 297 | 297 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 298 | - if ( apply_filters( 'wl_feature__enable__image-embeds', false ) ) { |
|
| 299 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 298 | + if (apply_filters('wl_feature__enable__image-embeds', false)) { |
|
| 299 | + $embeds = array_diff($attachment_service->get_image_embeds($post->post_content), $ids); |
|
| 300 | 300 | } else { |
| 301 | 301 | $embeds = array(); |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | // Get the gallery, removing existing ids. |
| 305 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 305 | + $gallery = array_diff($attachment_service->get_gallery($post), $ids, $embeds); |
|
| 306 | 306 | |
| 307 | 307 | // Map the attachment ids to images' data structured for schema.org use. |
| 308 | 308 | $images_with_sizes = array_filter( |
| 309 | 309 | array_reduce( |
| 310 | - array_merge( $ids, $embeds, $gallery ), |
|
| 311 | - function ( $carry, $item ) { |
|
| 310 | + array_merge($ids, $embeds, $gallery), |
|
| 311 | + function($carry, $item) { |
|
| 312 | 312 | /* |
| 313 | 313 | * @todo: we're not sure that we're getting attachment data here, we |
| 314 | 314 | * should filter `false`s. |
| 315 | 315 | */ |
| 316 | 316 | |
| 317 | 317 | $sources = array_merge( |
| 318 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 319 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 318 | + Wordlift_Image_Service::get_sources($item), |
|
| 319 | + array(wp_get_attachment_image_src($item, 'full')) |
|
| 320 | 320 | ); |
| 321 | 321 | |
| 322 | 322 | $sources_with_image = array_filter( |
| 323 | 323 | $sources, |
| 324 | - function ( $source ) { |
|
| 325 | - return ! empty( $source[0] ); |
|
| 324 | + function($source) { |
|
| 325 | + return ! empty($source[0]); |
|
| 326 | 326 | } |
| 327 | 327 | ); |
| 328 | 328 | |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | // or from uploads dir, but the image id still exists as featured image |
| 337 | 337 | // or in [gallery] shortcode. |
| 338 | 338 | // if ( empty( $attachment[0] ) ) { |
| 339 | - if ( empty( $sources_with_image ) ) { |
|
| 339 | + if (empty($sources_with_image)) { |
|
| 340 | 340 | return $carry; |
| 341 | 341 | } |
| 342 | 342 | |
@@ -354,7 +354,7 @@ discard block |
||
| 354 | 354 | |
| 355 | 355 | // Refactor data as per schema.org specifications. |
| 356 | 356 | $images = array_map( |
| 357 | - function ( $attachment ) { |
|
| 357 | + function($attachment) { |
|
| 358 | 358 | return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
| 359 | 359 | array( |
| 360 | 360 | '@type' => 'ImageObject', |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | ); |
| 368 | 368 | |
| 369 | 369 | // Add images if present. |
| 370 | - if ( 0 < count( $images ) ) { |
|
| 370 | + if (0 < count($images)) { |
|
| 371 | 371 | $jsonld['image'] = $images; |
| 372 | 372 | } |
| 373 | 373 | |
@@ -385,9 +385,9 @@ discard block |
||
| 385 | 385 | * @since 3.8.0 |
| 386 | 386 | * @access private |
| 387 | 387 | */ |
| 388 | - protected static function make_one( $value ) { |
|
| 388 | + protected static function make_one($value) { |
|
| 389 | 389 | |
| 390 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 390 | + return 1 === count($value) ? $value[0] : $value; |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -400,17 +400,17 @@ discard block |
||
| 400 | 400 | * @return array The enriched `ImageObject` array. |
| 401 | 401 | * @since 3.14.0 |
| 402 | 402 | */ |
| 403 | - public static function set_image_size( $image, $attachment ) { |
|
| 403 | + public static function set_image_size($image, $attachment) { |
|
| 404 | 404 | |
| 405 | 405 | // If you specify a "width" or "height" value you should leave out |
| 406 | 406 | // 'px'. For example: "width":"4608px" should be "width":"4608". |
| 407 | 407 | // |
| 408 | 408 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
| 409 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 409 | + if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) { |
|
| 410 | 410 | $image['width'] = $attachment[1]; |
| 411 | 411 | } |
| 412 | 412 | |
| 413 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 413 | + if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) { |
|
| 414 | 414 | $image['height'] = $attachment[2]; |
| 415 | 415 | } |
| 416 | 416 | |
@@ -429,33 +429,33 @@ discard block |
||
| 429 | 429 | * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in |
| 430 | 430 | * the {@link Wordlift_Schema_Service} class. |
| 431 | 431 | */ |
| 432 | - protected function process_type_custom_fields( &$jsonld, $fields, $post, &$references, &$references_infos ) { |
|
| 432 | + protected function process_type_custom_fields(&$jsonld, $fields, $post, &$references, &$references_infos) { |
|
| 433 | 433 | |
| 434 | 434 | // Set a reference to use in closures. |
| 435 | 435 | $converter = $this; |
| 436 | 436 | |
| 437 | 437 | // Try each field on the entity. |
| 438 | - foreach ( $fields as $key => $value ) { |
|
| 438 | + foreach ($fields as $key => $value) { |
|
| 439 | 439 | |
| 440 | 440 | // Get the predicate. |
| 441 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
| 441 | + $name = $this->relative_to_context($value['predicate']); |
|
| 442 | 442 | |
| 443 | 443 | // Get the value, the property service will get the right extractor |
| 444 | 444 | // for that property. |
| 445 | - $value = $this->property_getter->get( $post->ID, $key, Object_Type_Enum::POST ); |
|
| 445 | + $value = $this->property_getter->get($post->ID, $key, Object_Type_Enum::POST); |
|
| 446 | 446 | |
| 447 | - if ( empty( $value ) ) { |
|
| 447 | + if (empty($value)) { |
|
| 448 | 448 | continue; |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | // Map the value to the property name. |
| 452 | 452 | // If we got an array with just one value, we return that one value. |
| 453 | 453 | // If we got a Wordlift_Property_Entity_Reference we get the URL. |
| 454 | - $jsonld[ $name ] = self::make_one( |
|
| 454 | + $jsonld[$name] = self::make_one( |
|
| 455 | 455 | array_map( |
| 456 | - function ( $item ) use ( $converter, &$references, &$references_infos ) { |
|
| 456 | + function($item) use ($converter, &$references, &$references_infos) { |
|
| 457 | 457 | |
| 458 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 458 | + if ($item instanceof Wordlift_Property_Entity_Reference) { |
|
| 459 | 459 | |
| 460 | 460 | $url = $item->get_url(); |
| 461 | 461 | |
@@ -464,12 +464,12 @@ discard block |
||
| 464 | 464 | $references[] = $item->to_reference(); |
| 465 | 465 | |
| 466 | 466 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable |
| 467 | - $references_infos[] = array( 'reference' => $item ); |
|
| 467 | + $references_infos[] = array('reference' => $item); |
|
| 468 | 468 | |
| 469 | - return array( '@id' => $url ); |
|
| 469 | + return array('@id' => $url); |
|
| 470 | 470 | } |
| 471 | 471 | |
| 472 | - return $converter->relative_to_context( $item ); |
|
| 472 | + return $converter->relative_to_context($item); |
|
| 473 | 473 | }, |
| 474 | 474 | $value |
| 475 | 475 | ) |
@@ -20,521 +20,521 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Jsonld_Service { |
| 22 | 22 | |
| 23 | - private static $creative_work_types = array( |
|
| 24 | - 'AmpStory', |
|
| 25 | - 'ArchiveComponent', |
|
| 26 | - 'Article', |
|
| 27 | - 'Atlas', |
|
| 28 | - 'Blog', |
|
| 29 | - 'Book', |
|
| 30 | - 'Chapter', |
|
| 31 | - 'Claim', |
|
| 32 | - 'Clip', |
|
| 33 | - 'Code', |
|
| 34 | - 'Collection', |
|
| 35 | - 'ComicStory', |
|
| 36 | - 'Comment', |
|
| 37 | - 'Conversation', |
|
| 38 | - 'Course', |
|
| 39 | - 'CreativeWork', |
|
| 40 | - 'CreativeWorkSeason', |
|
| 41 | - 'CreativeWorkSeries', |
|
| 42 | - 'DataCatalog', |
|
| 43 | - 'Dataset', |
|
| 44 | - 'DefinedTermSet', |
|
| 45 | - 'Diet', |
|
| 46 | - 'DigitalDocument', |
|
| 47 | - 'Drawing', |
|
| 48 | - 'EducationalOccupationalCredential', |
|
| 49 | - 'Episode', |
|
| 50 | - 'ExercisePlan', |
|
| 51 | - 'Game', |
|
| 52 | - 'Guide', |
|
| 53 | - 'HowTo', |
|
| 54 | - 'HowToDirection', |
|
| 55 | - 'HowToSection', |
|
| 56 | - 'HowToStep', |
|
| 57 | - 'HowToTip', |
|
| 58 | - 'HyperToc', |
|
| 59 | - 'HyperTocEntry', |
|
| 60 | - 'LearningResource', |
|
| 61 | - 'Legislation', |
|
| 62 | - 'Manuscript', |
|
| 63 | - 'Map', |
|
| 64 | - 'MathSolver', |
|
| 65 | - 'MediaObject', |
|
| 66 | - 'Menu', |
|
| 67 | - 'MenuSection', |
|
| 68 | - 'Message', |
|
| 69 | - 'Movie', |
|
| 70 | - 'MusicComposition', |
|
| 71 | - 'MusicPlaylist', |
|
| 72 | - 'MusicRecording', |
|
| 73 | - 'Painting', |
|
| 74 | - 'Photograph', |
|
| 75 | - 'Play', |
|
| 76 | - 'Poster', |
|
| 77 | - 'PublicationIssue', |
|
| 78 | - 'PublicationVolume', |
|
| 79 | - 'Quotation', |
|
| 80 | - 'Review', |
|
| 81 | - 'Sculpture', |
|
| 82 | - 'Season', |
|
| 83 | - 'SheetMusic', |
|
| 84 | - 'ShortStory', |
|
| 85 | - 'SoftwareApplication', |
|
| 86 | - 'SoftwareSourceCode', |
|
| 87 | - 'SpecialAnnouncement', |
|
| 88 | - 'Thesis', |
|
| 89 | - 'TvSeason', |
|
| 90 | - 'TvSeries', |
|
| 91 | - 'VisualArtwork', |
|
| 92 | - 'WebContent', |
|
| 93 | - 'WebPage', |
|
| 94 | - 'WebPageElement', |
|
| 95 | - 'WebSite', |
|
| 96 | - 'AdvertiserContentArticle', |
|
| 97 | - 'NewsArticle', |
|
| 98 | - 'Report', |
|
| 99 | - 'SatiricalArticle', |
|
| 100 | - 'ScholarlyArticle', |
|
| 101 | - 'SocialMediaPosting', |
|
| 102 | - 'TechArticle', |
|
| 103 | - 'AnalysisNewsArticle', |
|
| 104 | - 'AskPublicNewsArticle', |
|
| 105 | - 'BackgroundNewsArticle', |
|
| 106 | - 'OpinionNewsArticle', |
|
| 107 | - 'ReportageNewsArticle', |
|
| 108 | - 'ReviewNewsArticle', |
|
| 109 | - 'MedicalScholarlyArticle', |
|
| 110 | - 'BlogPosting', |
|
| 111 | - 'DiscussionForumPosting', |
|
| 112 | - 'LiveBlogPosting', |
|
| 113 | - 'ApiReference', |
|
| 114 | - 'Audiobook', |
|
| 115 | - 'MovieClip', |
|
| 116 | - 'RadioClip', |
|
| 117 | - 'TvClip', |
|
| 118 | - 'VideoGameClip', |
|
| 119 | - 'ProductCollection', |
|
| 120 | - 'ComicCoverArt', |
|
| 121 | - 'Answer', |
|
| 122 | - 'CorrectionComment', |
|
| 123 | - 'Question', |
|
| 124 | - 'PodcastSeason', |
|
| 125 | - 'RadioSeason', |
|
| 126 | - 'TvSeason', |
|
| 127 | - 'BookSeries', |
|
| 128 | - 'MovieSeries', |
|
| 129 | - 'Periodical', |
|
| 130 | - 'PodcastSeries', |
|
| 131 | - 'RadioSeries', |
|
| 132 | - 'TvSeries', |
|
| 133 | - 'VideoGameSeries', |
|
| 134 | - 'ComicSeries', |
|
| 135 | - 'Newspaper', |
|
| 136 | - 'DataFeed', |
|
| 137 | - 'CompleteDataFeed', |
|
| 138 | - 'CategoryCodeSet', |
|
| 139 | - 'NoteDigitalDocument', |
|
| 140 | - 'PresentationDigitalDocument', |
|
| 141 | - 'SpreadsheetDigitalDocument', |
|
| 142 | - 'TextDigitalDocument', |
|
| 143 | - 'PodcastEpisode', |
|
| 144 | - 'RadioEpisode', |
|
| 145 | - 'TvEpisode', |
|
| 146 | - 'VideoGame', |
|
| 147 | - 'Recipe', |
|
| 148 | - 'Course', |
|
| 149 | - 'Quiz', |
|
| 150 | - 'LegislationObject', |
|
| 151 | - 'AudioObject', |
|
| 152 | - 'DModel', |
|
| 153 | - 'DataDownload', |
|
| 154 | - 'ImageObject', |
|
| 155 | - 'LegislationObject', |
|
| 156 | - 'MusicVideoObject', |
|
| 157 | - 'VideoObject', |
|
| 158 | - 'Audiobook', |
|
| 159 | - 'Barcode', |
|
| 160 | - 'EmailMessage', |
|
| 161 | - 'MusicAlbum', |
|
| 162 | - 'MusicRelease', |
|
| 163 | - 'ComicIssue', |
|
| 164 | - 'ClaimReview', |
|
| 165 | - 'CriticReview', |
|
| 166 | - 'EmployerReview', |
|
| 167 | - 'MediaReview', |
|
| 168 | - 'Recommendation', |
|
| 169 | - 'UserReview', |
|
| 170 | - 'ReviewNewsArticle', |
|
| 171 | - 'MobileApplication', |
|
| 172 | - 'VideoGame', |
|
| 173 | - 'WebApplication', |
|
| 174 | - 'CoverArt', |
|
| 175 | - 'ComicCoverArt', |
|
| 176 | - 'HealthTopicContent', |
|
| 177 | - 'AboutPage', |
|
| 178 | - 'CheckoutPage', |
|
| 179 | - 'CollectionPage', |
|
| 180 | - 'ContactPage', |
|
| 181 | - 'FaqPage', |
|
| 182 | - 'ItemPage', |
|
| 183 | - 'MedicalWebPage', |
|
| 184 | - 'ProfilePage', |
|
| 185 | - 'QaPage', |
|
| 186 | - 'RealEstateListing', |
|
| 187 | - 'SearchResultsPage', |
|
| 188 | - 'MediaGallery', |
|
| 189 | - 'ImageGallery', |
|
| 190 | - 'VideoGallery', |
|
| 191 | - 'SiteNavigationElement', |
|
| 192 | - 'Table', |
|
| 193 | - 'WpAdBlock', |
|
| 194 | - 'WpFooter', |
|
| 195 | - 'WpHeader', |
|
| 196 | - 'WpSideBar', |
|
| 197 | - ); |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 201 | - * |
|
| 202 | - * @since 3.8.0 |
|
| 203 | - * @access private |
|
| 204 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 205 | - */ |
|
| 206 | - private $entity_service; |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 210 | - * |
|
| 211 | - * @since 3.32.0 |
|
| 212 | - * @access private |
|
| 213 | - * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 214 | - */ |
|
| 215 | - private $term_jsonld_adapter; |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * A {@link Wordlift_Post_Converter} instance. |
|
| 219 | - * |
|
| 220 | - * @since 3.8.0 |
|
| 221 | - * @access private |
|
| 222 | - * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 223 | - */ |
|
| 224 | - private $converter; |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 228 | - * |
|
| 229 | - * @since 3.14.0 |
|
| 230 | - * @access private |
|
| 231 | - * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 232 | - */ |
|
| 233 | - private $website_converter; |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * The singleton instance for the JSON-LD service. |
|
| 237 | - * |
|
| 238 | - * @since 3.15.1 |
|
| 239 | - * |
|
| 240 | - * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 241 | - */ |
|
| 242 | - private static $instance; |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * @var Wordlift_Entity_Type_Service|null |
|
| 246 | - */ |
|
| 247 | - private $entity_type_service; |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Create a JSON-LD service. |
|
| 251 | - * |
|
| 252 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 253 | - * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 254 | - * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 255 | - * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
| 256 | - * |
|
| 257 | - * @since 3.8.0 |
|
| 258 | - */ |
|
| 259 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
| 260 | - |
|
| 261 | - $this->entity_service = $entity_service; |
|
| 262 | - $this->converter = $converter; |
|
| 263 | - $this->website_converter = $website_converter; |
|
| 264 | - $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
| 265 | - $this->entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
| 266 | - self::$instance = $this; |
|
| 267 | - |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Get the singleton instance for the JSON-LD service. |
|
| 272 | - * |
|
| 273 | - * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 274 | - * @since 3.15.1 |
|
| 275 | - */ |
|
| 276 | - public static function get_instance() { |
|
| 277 | - |
|
| 278 | - return self::$instance; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 283 | - * |
|
| 284 | - * @since 3.8.0 |
|
| 285 | - */ |
|
| 286 | - public function get() { |
|
| 287 | - // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 288 | - // |
|
| 289 | - // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 290 | - // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 291 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 292 | - @ob_clean(); |
|
| 293 | - |
|
| 294 | - // Get the parameter from the request. |
|
| 295 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 296 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 297 | - |
|
| 298 | - // Send the generated JSON-LD. |
|
| 299 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 300 | - |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 305 | - * |
|
| 306 | - * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 307 | - * then print and die. |
|
| 308 | - * |
|
| 309 | - * @since 3.18.5 |
|
| 310 | - */ |
|
| 311 | - private function send_jsonld( $response ) { |
|
| 312 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 313 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 314 | - echo wp_json_encode( $response ); |
|
| 315 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 316 | - wp_die(); |
|
| 317 | - } else { |
|
| 318 | - die; |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Get the JSON-LD. |
|
| 324 | - * |
|
| 325 | - * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 326 | - * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 327 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 328 | - * |
|
| 329 | - * @return array A JSON-LD structure. |
|
| 330 | - * @since 3.15.1 |
|
| 331 | - */ |
|
| 332 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 333 | - // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 334 | - // e-mails with a low apdex score. |
|
| 335 | - // |
|
| 336 | - // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 337 | - Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 338 | - |
|
| 339 | - // Switch to Website converter if is home page. |
|
| 340 | - if ( $is_homepage ) { |
|
| 341 | - /** |
|
| 342 | - * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 343 | - * |
|
| 344 | - * @since 3.14.0 |
|
| 345 | - * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 346 | - */ |
|
| 347 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 348 | - return array(); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - // Set a reference to the website_converter. |
|
| 352 | - $website_converter = $this->website_converter; |
|
| 353 | - |
|
| 354 | - // Send JSON-LD. |
|
| 355 | - return $website_converter->create_schema(); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - // If no id has been provided return an empty array. |
|
| 359 | - if ( ! isset( $post_id ) ) { |
|
| 360 | - return array(); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // An array of references which is captured when converting an URI to a |
|
| 364 | - // json which we gather to further expand our json-ld. |
|
| 365 | - $references = array(); |
|
| 366 | - $references_infos = array(); |
|
| 367 | - |
|
| 368 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 369 | - $entity_to_jsonld_converter = $this->converter; |
|
| 370 | - |
|
| 371 | - $relations = new Relations(); |
|
| 372 | - $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
| 373 | - |
|
| 374 | - $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
| 375 | - |
|
| 376 | - $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
| 377 | - // Add `about`/`mentions` only for `CreativeWork` and descendants. |
|
| 378 | - if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
| 379 | - |
|
| 380 | - foreach ( $relations->toArray() as $relation ) { |
|
| 381 | - $object = $relation->get_object(); |
|
| 382 | - $object_id = $object->get_id(); |
|
| 383 | - $object_type = $object->get_type(); |
|
| 384 | - |
|
| 385 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 386 | - $post_status = get_post_status( $object_id ); |
|
| 387 | - |
|
| 388 | - // Do not add unpublished posts. |
|
| 389 | - if ( 'publish' !== $post_status ) { |
|
| 390 | - continue; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - // Do not add Articles. |
|
| 394 | - if ( $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Article' ) ) { |
|
| 395 | - continue; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - $references_2 = array(); |
|
| 399 | - $reference_infos_2 = array(); |
|
| 400 | - $relations_2 = new Relations(); |
|
| 401 | - $jsonld[] = $entity_to_jsonld_converter->convert( $object_id, $references_2, $reference_infos_2, $relations_2 ); |
|
| 402 | - $this->add_location( $jsonld, $object ); |
|
| 403 | - } elseif ( $object_type === Object_Type_Enum::TERM && 1 !== $object_id ) { // Skip the Uncategorized term. |
|
| 404 | - $jsonld[] = $this->term_jsonld_adapter->get( $object_id, $context ); |
|
| 405 | - } else { |
|
| 406 | - continue; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - // Add the `mentions`/`about` prop. |
|
| 410 | - $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
| 411 | - } |
|
| 412 | - $graph->set_main_jsonld( $jsonld ); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - $jsonld_arr = $graph->add_references( $references ) |
|
| 416 | - ->add_relations( $relations ) |
|
| 417 | - ->add_required_reference_infos( $references_infos ) |
|
| 418 | - ->render( $context ); |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Filter name: wl_after_get_jsonld |
|
| 422 | - * |
|
| 423 | - * @return array |
|
| 424 | - * @since 3.27.2 |
|
| 425 | - * @var $jsonld array The final jsonld before outputting to page. |
|
| 426 | - * @var $post_id int The post id for which the jsonld is generated. |
|
| 427 | - */ |
|
| 428 | - $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
| 429 | - |
|
| 430 | - return $jsonld_arr; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - private function add_location( &$jsonld, $content_id ) { |
|
| 434 | - /* |
|
| 23 | + private static $creative_work_types = array( |
|
| 24 | + 'AmpStory', |
|
| 25 | + 'ArchiveComponent', |
|
| 26 | + 'Article', |
|
| 27 | + 'Atlas', |
|
| 28 | + 'Blog', |
|
| 29 | + 'Book', |
|
| 30 | + 'Chapter', |
|
| 31 | + 'Claim', |
|
| 32 | + 'Clip', |
|
| 33 | + 'Code', |
|
| 34 | + 'Collection', |
|
| 35 | + 'ComicStory', |
|
| 36 | + 'Comment', |
|
| 37 | + 'Conversation', |
|
| 38 | + 'Course', |
|
| 39 | + 'CreativeWork', |
|
| 40 | + 'CreativeWorkSeason', |
|
| 41 | + 'CreativeWorkSeries', |
|
| 42 | + 'DataCatalog', |
|
| 43 | + 'Dataset', |
|
| 44 | + 'DefinedTermSet', |
|
| 45 | + 'Diet', |
|
| 46 | + 'DigitalDocument', |
|
| 47 | + 'Drawing', |
|
| 48 | + 'EducationalOccupationalCredential', |
|
| 49 | + 'Episode', |
|
| 50 | + 'ExercisePlan', |
|
| 51 | + 'Game', |
|
| 52 | + 'Guide', |
|
| 53 | + 'HowTo', |
|
| 54 | + 'HowToDirection', |
|
| 55 | + 'HowToSection', |
|
| 56 | + 'HowToStep', |
|
| 57 | + 'HowToTip', |
|
| 58 | + 'HyperToc', |
|
| 59 | + 'HyperTocEntry', |
|
| 60 | + 'LearningResource', |
|
| 61 | + 'Legislation', |
|
| 62 | + 'Manuscript', |
|
| 63 | + 'Map', |
|
| 64 | + 'MathSolver', |
|
| 65 | + 'MediaObject', |
|
| 66 | + 'Menu', |
|
| 67 | + 'MenuSection', |
|
| 68 | + 'Message', |
|
| 69 | + 'Movie', |
|
| 70 | + 'MusicComposition', |
|
| 71 | + 'MusicPlaylist', |
|
| 72 | + 'MusicRecording', |
|
| 73 | + 'Painting', |
|
| 74 | + 'Photograph', |
|
| 75 | + 'Play', |
|
| 76 | + 'Poster', |
|
| 77 | + 'PublicationIssue', |
|
| 78 | + 'PublicationVolume', |
|
| 79 | + 'Quotation', |
|
| 80 | + 'Review', |
|
| 81 | + 'Sculpture', |
|
| 82 | + 'Season', |
|
| 83 | + 'SheetMusic', |
|
| 84 | + 'ShortStory', |
|
| 85 | + 'SoftwareApplication', |
|
| 86 | + 'SoftwareSourceCode', |
|
| 87 | + 'SpecialAnnouncement', |
|
| 88 | + 'Thesis', |
|
| 89 | + 'TvSeason', |
|
| 90 | + 'TvSeries', |
|
| 91 | + 'VisualArtwork', |
|
| 92 | + 'WebContent', |
|
| 93 | + 'WebPage', |
|
| 94 | + 'WebPageElement', |
|
| 95 | + 'WebSite', |
|
| 96 | + 'AdvertiserContentArticle', |
|
| 97 | + 'NewsArticle', |
|
| 98 | + 'Report', |
|
| 99 | + 'SatiricalArticle', |
|
| 100 | + 'ScholarlyArticle', |
|
| 101 | + 'SocialMediaPosting', |
|
| 102 | + 'TechArticle', |
|
| 103 | + 'AnalysisNewsArticle', |
|
| 104 | + 'AskPublicNewsArticle', |
|
| 105 | + 'BackgroundNewsArticle', |
|
| 106 | + 'OpinionNewsArticle', |
|
| 107 | + 'ReportageNewsArticle', |
|
| 108 | + 'ReviewNewsArticle', |
|
| 109 | + 'MedicalScholarlyArticle', |
|
| 110 | + 'BlogPosting', |
|
| 111 | + 'DiscussionForumPosting', |
|
| 112 | + 'LiveBlogPosting', |
|
| 113 | + 'ApiReference', |
|
| 114 | + 'Audiobook', |
|
| 115 | + 'MovieClip', |
|
| 116 | + 'RadioClip', |
|
| 117 | + 'TvClip', |
|
| 118 | + 'VideoGameClip', |
|
| 119 | + 'ProductCollection', |
|
| 120 | + 'ComicCoverArt', |
|
| 121 | + 'Answer', |
|
| 122 | + 'CorrectionComment', |
|
| 123 | + 'Question', |
|
| 124 | + 'PodcastSeason', |
|
| 125 | + 'RadioSeason', |
|
| 126 | + 'TvSeason', |
|
| 127 | + 'BookSeries', |
|
| 128 | + 'MovieSeries', |
|
| 129 | + 'Periodical', |
|
| 130 | + 'PodcastSeries', |
|
| 131 | + 'RadioSeries', |
|
| 132 | + 'TvSeries', |
|
| 133 | + 'VideoGameSeries', |
|
| 134 | + 'ComicSeries', |
|
| 135 | + 'Newspaper', |
|
| 136 | + 'DataFeed', |
|
| 137 | + 'CompleteDataFeed', |
|
| 138 | + 'CategoryCodeSet', |
|
| 139 | + 'NoteDigitalDocument', |
|
| 140 | + 'PresentationDigitalDocument', |
|
| 141 | + 'SpreadsheetDigitalDocument', |
|
| 142 | + 'TextDigitalDocument', |
|
| 143 | + 'PodcastEpisode', |
|
| 144 | + 'RadioEpisode', |
|
| 145 | + 'TvEpisode', |
|
| 146 | + 'VideoGame', |
|
| 147 | + 'Recipe', |
|
| 148 | + 'Course', |
|
| 149 | + 'Quiz', |
|
| 150 | + 'LegislationObject', |
|
| 151 | + 'AudioObject', |
|
| 152 | + 'DModel', |
|
| 153 | + 'DataDownload', |
|
| 154 | + 'ImageObject', |
|
| 155 | + 'LegislationObject', |
|
| 156 | + 'MusicVideoObject', |
|
| 157 | + 'VideoObject', |
|
| 158 | + 'Audiobook', |
|
| 159 | + 'Barcode', |
|
| 160 | + 'EmailMessage', |
|
| 161 | + 'MusicAlbum', |
|
| 162 | + 'MusicRelease', |
|
| 163 | + 'ComicIssue', |
|
| 164 | + 'ClaimReview', |
|
| 165 | + 'CriticReview', |
|
| 166 | + 'EmployerReview', |
|
| 167 | + 'MediaReview', |
|
| 168 | + 'Recommendation', |
|
| 169 | + 'UserReview', |
|
| 170 | + 'ReviewNewsArticle', |
|
| 171 | + 'MobileApplication', |
|
| 172 | + 'VideoGame', |
|
| 173 | + 'WebApplication', |
|
| 174 | + 'CoverArt', |
|
| 175 | + 'ComicCoverArt', |
|
| 176 | + 'HealthTopicContent', |
|
| 177 | + 'AboutPage', |
|
| 178 | + 'CheckoutPage', |
|
| 179 | + 'CollectionPage', |
|
| 180 | + 'ContactPage', |
|
| 181 | + 'FaqPage', |
|
| 182 | + 'ItemPage', |
|
| 183 | + 'MedicalWebPage', |
|
| 184 | + 'ProfilePage', |
|
| 185 | + 'QaPage', |
|
| 186 | + 'RealEstateListing', |
|
| 187 | + 'SearchResultsPage', |
|
| 188 | + 'MediaGallery', |
|
| 189 | + 'ImageGallery', |
|
| 190 | + 'VideoGallery', |
|
| 191 | + 'SiteNavigationElement', |
|
| 192 | + 'Table', |
|
| 193 | + 'WpAdBlock', |
|
| 194 | + 'WpFooter', |
|
| 195 | + 'WpHeader', |
|
| 196 | + 'WpSideBar', |
|
| 197 | + ); |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 201 | + * |
|
| 202 | + * @since 3.8.0 |
|
| 203 | + * @access private |
|
| 204 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 205 | + */ |
|
| 206 | + private $entity_service; |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 210 | + * |
|
| 211 | + * @since 3.32.0 |
|
| 212 | + * @access private |
|
| 213 | + * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 214 | + */ |
|
| 215 | + private $term_jsonld_adapter; |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * A {@link Wordlift_Post_Converter} instance. |
|
| 219 | + * |
|
| 220 | + * @since 3.8.0 |
|
| 221 | + * @access private |
|
| 222 | + * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 223 | + */ |
|
| 224 | + private $converter; |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 228 | + * |
|
| 229 | + * @since 3.14.0 |
|
| 230 | + * @access private |
|
| 231 | + * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 232 | + */ |
|
| 233 | + private $website_converter; |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * The singleton instance for the JSON-LD service. |
|
| 237 | + * |
|
| 238 | + * @since 3.15.1 |
|
| 239 | + * |
|
| 240 | + * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 241 | + */ |
|
| 242 | + private static $instance; |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * @var Wordlift_Entity_Type_Service|null |
|
| 246 | + */ |
|
| 247 | + private $entity_type_service; |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Create a JSON-LD service. |
|
| 251 | + * |
|
| 252 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 253 | + * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 254 | + * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 255 | + * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
| 256 | + * |
|
| 257 | + * @since 3.8.0 |
|
| 258 | + */ |
|
| 259 | + public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
| 260 | + |
|
| 261 | + $this->entity_service = $entity_service; |
|
| 262 | + $this->converter = $converter; |
|
| 263 | + $this->website_converter = $website_converter; |
|
| 264 | + $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
| 265 | + $this->entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
| 266 | + self::$instance = $this; |
|
| 267 | + |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Get the singleton instance for the JSON-LD service. |
|
| 272 | + * |
|
| 273 | + * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 274 | + * @since 3.15.1 |
|
| 275 | + */ |
|
| 276 | + public static function get_instance() { |
|
| 277 | + |
|
| 278 | + return self::$instance; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 283 | + * |
|
| 284 | + * @since 3.8.0 |
|
| 285 | + */ |
|
| 286 | + public function get() { |
|
| 287 | + // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 288 | + // |
|
| 289 | + // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 290 | + // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 291 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 292 | + @ob_clean(); |
|
| 293 | + |
|
| 294 | + // Get the parameter from the request. |
|
| 295 | + $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 296 | + $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 297 | + |
|
| 298 | + // Send the generated JSON-LD. |
|
| 299 | + $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 300 | + |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 305 | + * |
|
| 306 | + * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 307 | + * then print and die. |
|
| 308 | + * |
|
| 309 | + * @since 3.18.5 |
|
| 310 | + */ |
|
| 311 | + private function send_jsonld( $response ) { |
|
| 312 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 313 | + @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 314 | + echo wp_json_encode( $response ); |
|
| 315 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 316 | + wp_die(); |
|
| 317 | + } else { |
|
| 318 | + die; |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Get the JSON-LD. |
|
| 324 | + * |
|
| 325 | + * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 326 | + * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 327 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 328 | + * |
|
| 329 | + * @return array A JSON-LD structure. |
|
| 330 | + * @since 3.15.1 |
|
| 331 | + */ |
|
| 332 | + public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 333 | + // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 334 | + // e-mails with a low apdex score. |
|
| 335 | + // |
|
| 336 | + // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 337 | + Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 338 | + |
|
| 339 | + // Switch to Website converter if is home page. |
|
| 340 | + if ( $is_homepage ) { |
|
| 341 | + /** |
|
| 342 | + * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 343 | + * |
|
| 344 | + * @since 3.14.0 |
|
| 345 | + * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 346 | + */ |
|
| 347 | + if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 348 | + return array(); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + // Set a reference to the website_converter. |
|
| 352 | + $website_converter = $this->website_converter; |
|
| 353 | + |
|
| 354 | + // Send JSON-LD. |
|
| 355 | + return $website_converter->create_schema(); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + // If no id has been provided return an empty array. |
|
| 359 | + if ( ! isset( $post_id ) ) { |
|
| 360 | + return array(); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // An array of references which is captured when converting an URI to a |
|
| 364 | + // json which we gather to further expand our json-ld. |
|
| 365 | + $references = array(); |
|
| 366 | + $references_infos = array(); |
|
| 367 | + |
|
| 368 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 369 | + $entity_to_jsonld_converter = $this->converter; |
|
| 370 | + |
|
| 371 | + $relations = new Relations(); |
|
| 372 | + $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
| 373 | + |
|
| 374 | + $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
| 375 | + |
|
| 376 | + $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
| 377 | + // Add `about`/`mentions` only for `CreativeWork` and descendants. |
|
| 378 | + if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
| 379 | + |
|
| 380 | + foreach ( $relations->toArray() as $relation ) { |
|
| 381 | + $object = $relation->get_object(); |
|
| 382 | + $object_id = $object->get_id(); |
|
| 383 | + $object_type = $object->get_type(); |
|
| 384 | + |
|
| 385 | + if ( $object_type === Object_Type_Enum::POST ) { |
|
| 386 | + $post_status = get_post_status( $object_id ); |
|
| 387 | + |
|
| 388 | + // Do not add unpublished posts. |
|
| 389 | + if ( 'publish' !== $post_status ) { |
|
| 390 | + continue; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + // Do not add Articles. |
|
| 394 | + if ( $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Article' ) ) { |
|
| 395 | + continue; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + $references_2 = array(); |
|
| 399 | + $reference_infos_2 = array(); |
|
| 400 | + $relations_2 = new Relations(); |
|
| 401 | + $jsonld[] = $entity_to_jsonld_converter->convert( $object_id, $references_2, $reference_infos_2, $relations_2 ); |
|
| 402 | + $this->add_location( $jsonld, $object ); |
|
| 403 | + } elseif ( $object_type === Object_Type_Enum::TERM && 1 !== $object_id ) { // Skip the Uncategorized term. |
|
| 404 | + $jsonld[] = $this->term_jsonld_adapter->get( $object_id, $context ); |
|
| 405 | + } else { |
|
| 406 | + continue; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + // Add the `mentions`/`about` prop. |
|
| 410 | + $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
| 411 | + } |
|
| 412 | + $graph->set_main_jsonld( $jsonld ); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + $jsonld_arr = $graph->add_references( $references ) |
|
| 416 | + ->add_relations( $relations ) |
|
| 417 | + ->add_required_reference_infos( $references_infos ) |
|
| 418 | + ->render( $context ); |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Filter name: wl_after_get_jsonld |
|
| 422 | + * |
|
| 423 | + * @return array |
|
| 424 | + * @since 3.27.2 |
|
| 425 | + * @var $jsonld array The final jsonld before outputting to page. |
|
| 426 | + * @var $post_id int The post id for which the jsonld is generated. |
|
| 427 | + */ |
|
| 428 | + $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
| 429 | + |
|
| 430 | + return $jsonld_arr; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + private function add_location( &$jsonld, $content_id ) { |
|
| 434 | + /* |
|
| 435 | 435 | * Add the locations to the references. |
| 436 | 436 | * |
| 437 | 437 | * @since 3.19.5 |
| 438 | 438 | * |
| 439 | 439 | * @see https://github.com/insideout10/wordlift-plugin/issues/858. |
| 440 | 440 | */ |
| 441 | - // We do support only post targets here, because of the Entity_Type_Service limitation. |
|
| 442 | - if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
| 443 | - return; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - $object_id = $content_id->get_id(); |
|
| 447 | - if ( $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Action' ) |
|
| 448 | - || $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Event' ) |
|
| 449 | - || $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Organization' ) ) { |
|
| 450 | - |
|
| 451 | - $post_ids = get_post_meta( $object_id, Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 452 | - foreach ( $post_ids as $post_id ) { |
|
| 453 | - $references = array(); |
|
| 454 | - $reference_infos = array(); |
|
| 455 | - $relations = new Relations(); |
|
| 456 | - $jsonld[] = $this->converter->convert( $post_id, $references, $reference_infos, $relations ); |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Write the JSON-LD in the head. |
|
| 463 | - * |
|
| 464 | - * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 465 | - * document head, using the `wp_head` hook. |
|
| 466 | - * |
|
| 467 | - * @since 3.18.5 |
|
| 468 | - */ |
|
| 469 | - public function wp_head() { |
|
| 470 | - |
|
| 471 | - // Determine whether this is the home page or whether we're displaying a single post. |
|
| 472 | - $is_homepage = is_home() || is_front_page(); |
|
| 473 | - $post_id = is_singular() ? get_the_ID() : null; |
|
| 474 | - |
|
| 475 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 476 | - ?> |
|
| 441 | + // We do support only post targets here, because of the Entity_Type_Service limitation. |
|
| 442 | + if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
| 443 | + return; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + $object_id = $content_id->get_id(); |
|
| 447 | + if ( $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Action' ) |
|
| 448 | + || $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Event' ) |
|
| 449 | + || $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Organization' ) ) { |
|
| 450 | + |
|
| 451 | + $post_ids = get_post_meta( $object_id, Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 452 | + foreach ( $post_ids as $post_id ) { |
|
| 453 | + $references = array(); |
|
| 454 | + $reference_infos = array(); |
|
| 455 | + $relations = new Relations(); |
|
| 456 | + $jsonld[] = $this->converter->convert( $post_id, $references, $reference_infos, $relations ); |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Write the JSON-LD in the head. |
|
| 463 | + * |
|
| 464 | + * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 465 | + * document head, using the `wp_head` hook. |
|
| 466 | + * |
|
| 467 | + * @since 3.18.5 |
|
| 468 | + */ |
|
| 469 | + public function wp_head() { |
|
| 470 | + |
|
| 471 | + // Determine whether this is the home page or whether we're displaying a single post. |
|
| 472 | + $is_homepage = is_home() || is_front_page(); |
|
| 473 | + $post_id = is_singular() ? get_the_ID() : null; |
|
| 474 | + |
|
| 475 | + $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 476 | + ?> |
|
| 477 | 477 | <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
| 478 | 478 | <?php |
| 479 | - } |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * @param array $jsonld |
|
| 483 | - * @param Relation $relation |
|
| 484 | - * |
|
| 485 | - * @return void |
|
| 486 | - */ |
|
| 487 | - private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
| 488 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 489 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 490 | - |
|
| 491 | - $object = $relation->get_object(); |
|
| 492 | - $entity_uri = $content_service->get_entity_id( $object ); |
|
| 493 | - $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
| 494 | - |
|
| 495 | - $escaped_labels = array_map( |
|
| 496 | - function ( $value ) { |
|
| 497 | - return preg_quote( $value, '/' ); |
|
| 498 | - }, |
|
| 499 | - $labels |
|
| 500 | - ); |
|
| 501 | - |
|
| 502 | - $matches = false; |
|
| 503 | - |
|
| 504 | - // When the title is empty, then we shouldn't yield a match to about section. |
|
| 505 | - if ( array_filter( $escaped_labels ) ) { |
|
| 506 | - // Check if the labels match any part of the title. |
|
| 507 | - $post = get_post( $post_id ); |
|
| 508 | - $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - if ( $entity_uri ) { |
|
| 512 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 513 | - $property_name = $matches ? 'about' : 'mentions'; |
|
| 514 | - $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
| 515 | - $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Check if the labels match any part of the title. |
|
| 522 | - * |
|
| 523 | - * @param $labels array The labels to check. |
|
| 524 | - * @param $title string The title to check. |
|
| 525 | - * |
|
| 526 | - * @return boolean |
|
| 527 | - */ |
|
| 528 | - public function check_title_match( $labels, $title ) { |
|
| 529 | - |
|
| 530 | - // If the title is empty, then we shouldn't yield a match to about section. |
|
| 531 | - if ( empty( $title ) ) { |
|
| 532 | - return false; |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - // Check if the labels match any part of the title. |
|
| 536 | - return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
| 537 | - |
|
| 538 | - } |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * @param array $jsonld |
|
| 483 | + * @param Relation $relation |
|
| 484 | + * |
|
| 485 | + * @return void |
|
| 486 | + */ |
|
| 487 | + private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
| 488 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 489 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 490 | + |
|
| 491 | + $object = $relation->get_object(); |
|
| 492 | + $entity_uri = $content_service->get_entity_id( $object ); |
|
| 493 | + $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
| 494 | + |
|
| 495 | + $escaped_labels = array_map( |
|
| 496 | + function ( $value ) { |
|
| 497 | + return preg_quote( $value, '/' ); |
|
| 498 | + }, |
|
| 499 | + $labels |
|
| 500 | + ); |
|
| 501 | + |
|
| 502 | + $matches = false; |
|
| 503 | + |
|
| 504 | + // When the title is empty, then we shouldn't yield a match to about section. |
|
| 505 | + if ( array_filter( $escaped_labels ) ) { |
|
| 506 | + // Check if the labels match any part of the title. |
|
| 507 | + $post = get_post( $post_id ); |
|
| 508 | + $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + if ( $entity_uri ) { |
|
| 512 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 513 | + $property_name = $matches ? 'about' : 'mentions'; |
|
| 514 | + $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
| 515 | + $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Check if the labels match any part of the title. |
|
| 522 | + * |
|
| 523 | + * @param $labels array The labels to check. |
|
| 524 | + * @param $title string The title to check. |
|
| 525 | + * |
|
| 526 | + * @return boolean |
|
| 527 | + */ |
|
| 528 | + public function check_title_match( $labels, $title ) { |
|
| 529 | + |
|
| 530 | + // If the title is empty, then we shouldn't yield a match to about section. |
|
| 531 | + if ( empty( $title ) ) { |
|
| 532 | + return false; |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + // Check if the labels match any part of the title. |
|
| 536 | + return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
| 537 | + |
|
| 538 | + } |
|
| 539 | 539 | |
| 540 | 540 | } |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | * |
| 257 | 257 | * @since 3.8.0 |
| 258 | 258 | */ |
| 259 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
| 259 | + public function __construct($entity_service, $converter, $website_converter, $term_jsonld_adapter) { |
|
| 260 | 260 | |
| 261 | 261 | $this->entity_service = $entity_service; |
| 262 | 262 | $this->converter = $converter; |
@@ -292,11 +292,11 @@ discard block |
||
| 292 | 292 | @ob_clean(); |
| 293 | 293 | |
| 294 | 294 | // Get the parameter from the request. |
| 295 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 296 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 295 | + $is_homepage = isset($_REQUEST['homepage']); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 296 | + $post_id = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 297 | 297 | |
| 298 | 298 | // Send the generated JSON-LD. |
| 299 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 299 | + $this->send_jsonld($this->get_jsonld($is_homepage, $post_id)); |
|
| 300 | 300 | |
| 301 | 301 | } |
| 302 | 302 | |
@@ -308,11 +308,11 @@ discard block |
||
| 308 | 308 | * |
| 309 | 309 | * @since 3.18.5 |
| 310 | 310 | */ |
| 311 | - private function send_jsonld( $response ) { |
|
| 311 | + private function send_jsonld($response) { |
|
| 312 | 312 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 313 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 314 | - echo wp_json_encode( $response ); |
|
| 315 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 313 | + @header('Content-Type: application/ld+json; charset='.get_option('blog_charset')); |
|
| 314 | + echo wp_json_encode($response); |
|
| 315 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 316 | 316 | wp_die(); |
| 317 | 317 | } else { |
| 318 | 318 | die; |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | * @return array A JSON-LD structure. |
| 330 | 330 | * @since 3.15.1 |
| 331 | 331 | */ |
| 332 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 332 | + public function get_jsonld($is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN) { |
|
| 333 | 333 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
| 334 | 334 | // e-mails with a low apdex score. |
| 335 | 335 | // |
@@ -337,14 +337,14 @@ discard block |
||
| 337 | 337 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
| 338 | 338 | |
| 339 | 339 | // Switch to Website converter if is home page. |
| 340 | - if ( $is_homepage ) { |
|
| 340 | + if ($is_homepage) { |
|
| 341 | 341 | /** |
| 342 | 342 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
| 343 | 343 | * |
| 344 | 344 | * @since 3.14.0 |
| 345 | 345 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
| 346 | 346 | */ |
| 347 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 347 | + if (apply_filters('wordlift_disable_website_json_ld', false)) { |
|
| 348 | 348 | return array(); |
| 349 | 349 | } |
| 350 | 350 | |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | // If no id has been provided return an empty array. |
| 359 | - if ( ! isset( $post_id ) ) { |
|
| 359 | + if ( ! isset($post_id)) { |
|
| 360 | 360 | return array(); |
| 361 | 361 | } |
| 362 | 362 | |
@@ -369,53 +369,53 @@ discard block |
||
| 369 | 369 | $entity_to_jsonld_converter = $this->converter; |
| 370 | 370 | |
| 371 | 371 | $relations = new Relations(); |
| 372 | - $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
| 372 | + $jsonld = $entity_to_jsonld_converter->convert($post_id, $references, $references_infos, $relations); |
|
| 373 | 373 | |
| 374 | - $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
| 374 | + $graph = new Graph($jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance()); |
|
| 375 | 375 | |
| 376 | - $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
| 376 | + $schema_type = is_array($jsonld['@type']) ? $jsonld['@type'] : array($jsonld['@type']); |
|
| 377 | 377 | // Add `about`/`mentions` only for `CreativeWork` and descendants. |
| 378 | - if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
| 378 | + if (array_intersect($schema_type, self::$creative_work_types)) { |
|
| 379 | 379 | |
| 380 | - foreach ( $relations->toArray() as $relation ) { |
|
| 380 | + foreach ($relations->toArray() as $relation) { |
|
| 381 | 381 | $object = $relation->get_object(); |
| 382 | 382 | $object_id = $object->get_id(); |
| 383 | 383 | $object_type = $object->get_type(); |
| 384 | 384 | |
| 385 | - if ( $object_type === Object_Type_Enum::POST ) { |
|
| 386 | - $post_status = get_post_status( $object_id ); |
|
| 385 | + if ($object_type === Object_Type_Enum::POST) { |
|
| 386 | + $post_status = get_post_status($object_id); |
|
| 387 | 387 | |
| 388 | 388 | // Do not add unpublished posts. |
| 389 | - if ( 'publish' !== $post_status ) { |
|
| 389 | + if ('publish' !== $post_status) { |
|
| 390 | 390 | continue; |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | // Do not add Articles. |
| 394 | - if ( $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Article' ) ) { |
|
| 394 | + if ($this->entity_type_service->has_entity_type($object_id, 'http://schema.org/Article')) { |
|
| 395 | 395 | continue; |
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 398 | $references_2 = array(); |
| 399 | 399 | $reference_infos_2 = array(); |
| 400 | 400 | $relations_2 = new Relations(); |
| 401 | - $jsonld[] = $entity_to_jsonld_converter->convert( $object_id, $references_2, $reference_infos_2, $relations_2 ); |
|
| 402 | - $this->add_location( $jsonld, $object ); |
|
| 403 | - } elseif ( $object_type === Object_Type_Enum::TERM && 1 !== $object_id ) { // Skip the Uncategorized term. |
|
| 404 | - $jsonld[] = $this->term_jsonld_adapter->get( $object_id, $context ); |
|
| 401 | + $jsonld[] = $entity_to_jsonld_converter->convert($object_id, $references_2, $reference_infos_2, $relations_2); |
|
| 402 | + $this->add_location($jsonld, $object); |
|
| 403 | + } elseif ($object_type === Object_Type_Enum::TERM && 1 !== $object_id) { // Skip the Uncategorized term. |
|
| 404 | + $jsonld[] = $this->term_jsonld_adapter->get($object_id, $context); |
|
| 405 | 405 | } else { |
| 406 | 406 | continue; |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | // Add the `mentions`/`about` prop. |
| 410 | - $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
| 410 | + $this->add_mention_or_about($jsonld, $post_id, $relation); |
|
| 411 | 411 | } |
| 412 | - $graph->set_main_jsonld( $jsonld ); |
|
| 412 | + $graph->set_main_jsonld($jsonld); |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | - $jsonld_arr = $graph->add_references( $references ) |
|
| 416 | - ->add_relations( $relations ) |
|
| 417 | - ->add_required_reference_infos( $references_infos ) |
|
| 418 | - ->render( $context ); |
|
| 415 | + $jsonld_arr = $graph->add_references($references) |
|
| 416 | + ->add_relations($relations) |
|
| 417 | + ->add_required_reference_infos($references_infos) |
|
| 418 | + ->render($context); |
|
| 419 | 419 | |
| 420 | 420 | /** |
| 421 | 421 | * Filter name: wl_after_get_jsonld |
@@ -425,12 +425,12 @@ discard block |
||
| 425 | 425 | * @var $jsonld array The final jsonld before outputting to page. |
| 426 | 426 | * @var $post_id int The post id for which the jsonld is generated. |
| 427 | 427 | */ |
| 428 | - $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
| 428 | + $jsonld_arr = apply_filters('wl_after_get_jsonld', $jsonld_arr, $post_id, $context); |
|
| 429 | 429 | |
| 430 | 430 | return $jsonld_arr; |
| 431 | 431 | } |
| 432 | 432 | |
| 433 | - private function add_location( &$jsonld, $content_id ) { |
|
| 433 | + private function add_location(&$jsonld, $content_id) { |
|
| 434 | 434 | /* |
| 435 | 435 | * Add the locations to the references. |
| 436 | 436 | * |
@@ -439,21 +439,21 @@ discard block |
||
| 439 | 439 | * @see https://github.com/insideout10/wordlift-plugin/issues/858. |
| 440 | 440 | */ |
| 441 | 441 | // We do support only post targets here, because of the Entity_Type_Service limitation. |
| 442 | - if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
| 442 | + if ($content_id->get_type() !== Object_Type_Enum::POST) { |
|
| 443 | 443 | return; |
| 444 | 444 | } |
| 445 | 445 | |
| 446 | 446 | $object_id = $content_id->get_id(); |
| 447 | - if ( $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Action' ) |
|
| 448 | - || $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Event' ) |
|
| 449 | - || $this->entity_type_service->has_entity_type( $object_id, 'http://schema.org/Organization' ) ) { |
|
| 447 | + if ($this->entity_type_service->has_entity_type($object_id, 'http://schema.org/Action') |
|
| 448 | + || $this->entity_type_service->has_entity_type($object_id, 'http://schema.org/Event') |
|
| 449 | + || $this->entity_type_service->has_entity_type($object_id, 'http://schema.org/Organization')) { |
|
| 450 | 450 | |
| 451 | - $post_ids = get_post_meta( $object_id, Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 452 | - foreach ( $post_ids as $post_id ) { |
|
| 451 | + $post_ids = get_post_meta($object_id, Wordlift_Schema_Service::FIELD_LOCATION); |
|
| 452 | + foreach ($post_ids as $post_id) { |
|
| 453 | 453 | $references = array(); |
| 454 | 454 | $reference_infos = array(); |
| 455 | 455 | $relations = new Relations(); |
| 456 | - $jsonld[] = $this->converter->convert( $post_id, $references, $reference_infos, $relations ); |
|
| 456 | + $jsonld[] = $this->converter->convert($post_id, $references, $reference_infos, $relations); |
|
| 457 | 457 | } |
| 458 | 458 | } |
| 459 | 459 | } |
@@ -472,9 +472,9 @@ discard block |
||
| 472 | 472 | $is_homepage = is_home() || is_front_page(); |
| 473 | 473 | $post_id = is_singular() ? get_the_ID() : null; |
| 474 | 474 | |
| 475 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 475 | + $jsonld = wp_json_encode($this->get_jsonld($is_homepage, $post_id, Jsonld_Context_Enum::PAGE)); |
|
| 476 | 476 | ?> |
| 477 | - <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
|
| 477 | + <script type="application/ld+json"><?php echo esc_html($jsonld); ?></script> |
|
| 478 | 478 | <?php |
| 479 | 479 | } |
| 480 | 480 | |
@@ -484,17 +484,17 @@ discard block |
||
| 484 | 484 | * |
| 485 | 485 | * @return void |
| 486 | 486 | */ |
| 487 | - private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
| 487 | + private function add_mention_or_about(&$jsonld, $post_id, $relation) { |
|
| 488 | 488 | $content_service = Wordpress_Content_Service::get_instance(); |
| 489 | 489 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 490 | 490 | |
| 491 | 491 | $object = $relation->get_object(); |
| 492 | - $entity_uri = $content_service->get_entity_id( $object ); |
|
| 493 | - $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
| 492 | + $entity_uri = $content_service->get_entity_id($object); |
|
| 493 | + $labels = $entity_service->get_labels($object->get_id(), $object->get_type()); |
|
| 494 | 494 | |
| 495 | 495 | $escaped_labels = array_map( |
| 496 | - function ( $value ) { |
|
| 497 | - return preg_quote( $value, '/' ); |
|
| 496 | + function($value) { |
|
| 497 | + return preg_quote($value, '/'); |
|
| 498 | 498 | }, |
| 499 | 499 | $labels |
| 500 | 500 | ); |
@@ -502,17 +502,17 @@ discard block |
||
| 502 | 502 | $matches = false; |
| 503 | 503 | |
| 504 | 504 | // When the title is empty, then we shouldn't yield a match to about section. |
| 505 | - if ( array_filter( $escaped_labels ) ) { |
|
| 505 | + if (array_filter($escaped_labels)) { |
|
| 506 | 506 | // Check if the labels match any part of the title. |
| 507 | - $post = get_post( $post_id ); |
|
| 508 | - $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
| 507 | + $post = get_post($post_id); |
|
| 508 | + $matches = $this->check_title_match($escaped_labels, $post->post_title); |
|
| 509 | 509 | } |
| 510 | 510 | |
| 511 | - if ( $entity_uri ) { |
|
| 511 | + if ($entity_uri) { |
|
| 512 | 512 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
| 513 | 513 | $property_name = $matches ? 'about' : 'mentions'; |
| 514 | - $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
| 515 | - $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
| 514 | + $jsonld[$property_name] = isset($jsonld[$property_name]) ? (array) $jsonld[$property_name] : array(); |
|
| 515 | + $jsonld[$property_name][] = array('@id' => $entity_uri); |
|
| 516 | 516 | } |
| 517 | 517 | |
| 518 | 518 | } |
@@ -525,15 +525,15 @@ discard block |
||
| 525 | 525 | * |
| 526 | 526 | * @return boolean |
| 527 | 527 | */ |
| 528 | - public function check_title_match( $labels, $title ) { |
|
| 528 | + public function check_title_match($labels, $title) { |
|
| 529 | 529 | |
| 530 | 530 | // If the title is empty, then we shouldn't yield a match to about section. |
| 531 | - if ( empty( $title ) ) { |
|
| 531 | + if (empty($title)) { |
|
| 532 | 532 | return false; |
| 533 | 533 | } |
| 534 | 534 | |
| 535 | 535 | // Check if the labels match any part of the title. |
| 536 | - return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
| 536 | + return 1 === preg_match('/\b('.implode('|', $labels).')\b/iu', $title); |
|
| 537 | 537 | |
| 538 | 538 | } |
| 539 | 539 | |
@@ -16,21 +16,21 @@ |
||
| 16 | 16 | |
| 17 | 17 | interface Relation_Service_Interface { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Get the relations for the provided {@link Wordpress_Content_Id} |
|
| 21 | - * |
|
| 22 | - * @param Wordpress_Content_Id $content_id |
|
| 23 | - * |
|
| 24 | - * @return Relations_Interface |
|
| 25 | - */ |
|
| 26 | - public function get_relations( $content_id ); |
|
| 19 | + /** |
|
| 20 | + * Get the relations for the provided {@link Wordpress_Content_Id} |
|
| 21 | + * |
|
| 22 | + * @param Wordpress_Content_Id $content_id |
|
| 23 | + * |
|
| 24 | + * @return Relations_Interface |
|
| 25 | + */ |
|
| 26 | + public function get_relations( $content_id ); |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Add the relations for the provided {@link Wordpress_Content_Id} to the provided {@link Relations_Interface} |
|
| 30 | - * |
|
| 31 | - * @param Wordpress_Content_Id $content_id |
|
| 32 | - * @param Relations $relations |
|
| 33 | - */ |
|
| 34 | - public function add_relations( $content_id, $relations ); |
|
| 28 | + /** |
|
| 29 | + * Add the relations for the provided {@link Wordpress_Content_Id} to the provided {@link Relations_Interface} |
|
| 30 | + * |
|
| 31 | + * @param Wordpress_Content_Id $content_id |
|
| 32 | + * @param Relations $relations |
|
| 33 | + */ |
|
| 34 | + public function add_relations( $content_id, $relations ); |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | * |
| 24 | 24 | * @return Relations_Interface |
| 25 | 25 | */ |
| 26 | - public function get_relations( $content_id ); |
|
| 26 | + public function get_relations($content_id); |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * Add the relations for the provided {@link Wordpress_Content_Id} to the provided {@link Relations_Interface} |
@@ -31,6 +31,6 @@ discard block |
||
| 31 | 31 | * @param Wordpress_Content_Id $content_id |
| 32 | 32 | * @param Relations $relations |
| 33 | 33 | */ |
| 34 | - public function add_relations( $content_id, $relations ); |
|
| 34 | + public function add_relations($content_id, $relations); |
|
| 35 | 35 | |
| 36 | 36 | } |