@@ -9,80 +9,80 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Relation_Service extends Abstract_Relation_Service { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var Relation_Service_Interface[] |
|
| 14 | - */ |
|
| 15 | - private $delegates = array(); |
|
| 16 | - |
|
| 17 | - protected function __construct() { |
|
| 18 | - |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - private static $instance = null; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The singleton instance. |
|
| 25 | - * |
|
| 26 | - * @return Relation_Service_Interface |
|
| 27 | - */ |
|
| 28 | - public static function get_instance() { |
|
| 29 | - if ( ! isset( self::$instance ) ) { |
|
| 30 | - self::$instance = new self(); |
|
| 31 | - |
|
| 32 | - self::$instance->register_delegate( Relation_Instances_Relation_Service::get_instance() ); |
|
| 33 | - self::$instance->register_delegate( Post_Terms_Relation_Service::get_instance() ); |
|
| 34 | - self::$instance->register_delegate( Post_Content_Relation_Service::get_instance() ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - return self::$instance; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function register_delegate( $delegate ) { |
|
| 41 | - Assertions::is_a( $delegate, 'Wordlift\Relation\Relation_Service_Interface', 'A `delegate` must implement the `Wordlift\Relation\Relation_Service_Interface` interface.' ); |
|
| 42 | - |
|
| 43 | - $this->delegates[] = $delegate; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function add_relations( $content_id, $relations ) { |
|
| 47 | - Assertions::is_set( $relations, '`$relations` should be set to a `Relations` instance.' ); |
|
| 48 | - |
|
| 49 | - foreach ( $this->delegates as $delegate ) { |
|
| 50 | - $delegate->add_relations( $content_id, $relations ); |
|
| 51 | - } |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * This helper method is used to create relations from entity_uris. |
|
| 56 | - * Its only purpose as of now is to process the entity_uris emitted |
|
| 57 | - * from disambiguation widget. |
|
| 58 | - * |
|
| 59 | - * @param $subject Wordpress_Content_Id |
|
| 60 | - * @param $uris |
|
| 61 | - * |
|
| 62 | - * @return array<Relation> |
|
| 63 | - */ |
|
| 64 | - public static function get_relations_from_uris( $subject, $uris ) { |
|
| 65 | - |
|
| 66 | - $entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 67 | - |
|
| 68 | - return array_filter( |
|
| 69 | - array_map( |
|
| 70 | - function ( $uri ) use ( $entity_service, $subject ) { |
|
| 71 | - /** |
|
| 72 | - * @var $content Content|null |
|
| 73 | - */ |
|
| 74 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
| 75 | - $bag = $content->get_bag(); |
|
| 76 | - if ( $bag instanceof \WP_Post || $bag instanceof \WP_Term ) { |
|
| 77 | - $predicate = $bag instanceof \WP_Term ? WL_WHAT_RELATION : $entity_service->get_classification_scope_for( $content->get_id() ); |
|
| 78 | - return new Relation( $subject, new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ), $predicate ); |
|
| 79 | - } |
|
| 80 | - return false; |
|
| 81 | - }, |
|
| 82 | - $uris |
|
| 83 | - ) |
|
| 84 | - ); |
|
| 85 | - |
|
| 86 | - } |
|
| 12 | + /** |
|
| 13 | + * @var Relation_Service_Interface[] |
|
| 14 | + */ |
|
| 15 | + private $delegates = array(); |
|
| 16 | + |
|
| 17 | + protected function __construct() { |
|
| 18 | + |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + private static $instance = null; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The singleton instance. |
|
| 25 | + * |
|
| 26 | + * @return Relation_Service_Interface |
|
| 27 | + */ |
|
| 28 | + public static function get_instance() { |
|
| 29 | + if ( ! isset( self::$instance ) ) { |
|
| 30 | + self::$instance = new self(); |
|
| 31 | + |
|
| 32 | + self::$instance->register_delegate( Relation_Instances_Relation_Service::get_instance() ); |
|
| 33 | + self::$instance->register_delegate( Post_Terms_Relation_Service::get_instance() ); |
|
| 34 | + self::$instance->register_delegate( Post_Content_Relation_Service::get_instance() ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + return self::$instance; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function register_delegate( $delegate ) { |
|
| 41 | + Assertions::is_a( $delegate, 'Wordlift\Relation\Relation_Service_Interface', 'A `delegate` must implement the `Wordlift\Relation\Relation_Service_Interface` interface.' ); |
|
| 42 | + |
|
| 43 | + $this->delegates[] = $delegate; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function add_relations( $content_id, $relations ) { |
|
| 47 | + Assertions::is_set( $relations, '`$relations` should be set to a `Relations` instance.' ); |
|
| 48 | + |
|
| 49 | + foreach ( $this->delegates as $delegate ) { |
|
| 50 | + $delegate->add_relations( $content_id, $relations ); |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * This helper method is used to create relations from entity_uris. |
|
| 56 | + * Its only purpose as of now is to process the entity_uris emitted |
|
| 57 | + * from disambiguation widget. |
|
| 58 | + * |
|
| 59 | + * @param $subject Wordpress_Content_Id |
|
| 60 | + * @param $uris |
|
| 61 | + * |
|
| 62 | + * @return array<Relation> |
|
| 63 | + */ |
|
| 64 | + public static function get_relations_from_uris( $subject, $uris ) { |
|
| 65 | + |
|
| 66 | + $entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 67 | + |
|
| 68 | + return array_filter( |
|
| 69 | + array_map( |
|
| 70 | + function ( $uri ) use ( $entity_service, $subject ) { |
|
| 71 | + /** |
|
| 72 | + * @var $content Content|null |
|
| 73 | + */ |
|
| 74 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
| 75 | + $bag = $content->get_bag(); |
|
| 76 | + if ( $bag instanceof \WP_Post || $bag instanceof \WP_Term ) { |
|
| 77 | + $predicate = $bag instanceof \WP_Term ? WL_WHAT_RELATION : $entity_service->get_classification_scope_for( $content->get_id() ); |
|
| 78 | + return new Relation( $subject, new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ), $predicate ); |
|
| 79 | + } |
|
| 80 | + return false; |
|
| 81 | + }, |
|
| 82 | + $uris |
|
| 83 | + ) |
|
| 84 | + ); |
|
| 85 | + |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | 88 | } |
@@ -26,28 +26,28 @@ discard block |
||
| 26 | 26 | * @return Relation_Service_Interface |
| 27 | 27 | */ |
| 28 | 28 | public static function get_instance() { |
| 29 | - if ( ! isset( self::$instance ) ) { |
|
| 29 | + if ( ! isset(self::$instance)) { |
|
| 30 | 30 | self::$instance = new self(); |
| 31 | 31 | |
| 32 | - self::$instance->register_delegate( Relation_Instances_Relation_Service::get_instance() ); |
|
| 33 | - self::$instance->register_delegate( Post_Terms_Relation_Service::get_instance() ); |
|
| 34 | - self::$instance->register_delegate( Post_Content_Relation_Service::get_instance() ); |
|
| 32 | + self::$instance->register_delegate(Relation_Instances_Relation_Service::get_instance()); |
|
| 33 | + self::$instance->register_delegate(Post_Terms_Relation_Service::get_instance()); |
|
| 34 | + self::$instance->register_delegate(Post_Content_Relation_Service::get_instance()); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | return self::$instance; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - public function register_delegate( $delegate ) { |
|
| 41 | - Assertions::is_a( $delegate, 'Wordlift\Relation\Relation_Service_Interface', 'A `delegate` must implement the `Wordlift\Relation\Relation_Service_Interface` interface.' ); |
|
| 40 | + public function register_delegate($delegate) { |
|
| 41 | + Assertions::is_a($delegate, 'Wordlift\Relation\Relation_Service_Interface', 'A `delegate` must implement the `Wordlift\Relation\Relation_Service_Interface` interface.'); |
|
| 42 | 42 | |
| 43 | 43 | $this->delegates[] = $delegate; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - public function add_relations( $content_id, $relations ) { |
|
| 47 | - Assertions::is_set( $relations, '`$relations` should be set to a `Relations` instance.' ); |
|
| 46 | + public function add_relations($content_id, $relations) { |
|
| 47 | + Assertions::is_set($relations, '`$relations` should be set to a `Relations` instance.'); |
|
| 48 | 48 | |
| 49 | - foreach ( $this->delegates as $delegate ) { |
|
| 50 | - $delegate->add_relations( $content_id, $relations ); |
|
| 49 | + foreach ($this->delegates as $delegate) { |
|
| 50 | + $delegate->add_relations($content_id, $relations); |
|
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |
@@ -61,21 +61,21 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @return array<Relation> |
| 63 | 63 | */ |
| 64 | - public static function get_relations_from_uris( $subject, $uris ) { |
|
| 64 | + public static function get_relations_from_uris($subject, $uris) { |
|
| 65 | 65 | |
| 66 | 66 | $entity_service = \Wordlift_Entity_Service::get_instance(); |
| 67 | 67 | |
| 68 | 68 | return array_filter( |
| 69 | 69 | array_map( |
| 70 | - function ( $uri ) use ( $entity_service, $subject ) { |
|
| 70 | + function($uri) use ($entity_service, $subject) { |
|
| 71 | 71 | /** |
| 72 | 72 | * @var $content Content|null |
| 73 | 73 | */ |
| 74 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
| 74 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri); |
|
| 75 | 75 | $bag = $content->get_bag(); |
| 76 | - if ( $bag instanceof \WP_Post || $bag instanceof \WP_Term ) { |
|
| 77 | - $predicate = $bag instanceof \WP_Term ? WL_WHAT_RELATION : $entity_service->get_classification_scope_for( $content->get_id() ); |
|
| 78 | - return new Relation( $subject, new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ), $predicate ); |
|
| 76 | + if ($bag instanceof \WP_Post || $bag instanceof \WP_Term) { |
|
| 77 | + $predicate = $bag instanceof \WP_Term ? WL_WHAT_RELATION : $entity_service->get_classification_scope_for($content->get_id()); |
|
| 78 | + return new Relation($subject, new Wordpress_Content_Id($content->get_id(), $content->get_object_type_enum()), $predicate); |
|
| 79 | 79 | } |
| 80 | 80 | return false; |
| 81 | 81 | }, |
@@ -16,142 +16,142 @@ |
||
| 16 | 16 | use Wordlift\Content\Wordpress\Wordpress_Content_Id; |
| 17 | 17 | |
| 18 | 18 | class Relation implements Hashable_Interface, JsonSerializable { |
| 19 | - /** |
|
| 20 | - * @var Wordpress_Content_Id |
|
| 21 | - */ |
|
| 22 | - private $subject; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - private $predicate; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param Wordpress_Content_Id $subject |
|
| 31 | - * @param Wordpress_Content_Id $object |
|
| 32 | - * @param string $predicate |
|
| 33 | - */ |
|
| 34 | - public function __construct( $subject, $object, $predicate ) { |
|
| 35 | - $this->subject = $subject; |
|
| 36 | - $this->predicate = $predicate; |
|
| 37 | - $this->object = $object; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var Wordpress_Content_Id |
|
| 42 | - */ |
|
| 43 | - private $object; |
|
| 44 | - |
|
| 45 | - public static function from_json( $item ) { |
|
| 46 | - $subject = Wordpress_Content_Id::from_json( $item['subject'] ); |
|
| 47 | - $object = Wordpress_Content_Id::from_json( $item['object'] ); |
|
| 48 | - $predicate = $item['predicate']; |
|
| 49 | - |
|
| 50 | - return new self( $subject, $object, $predicate ); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @return Wordpress_Content_Id |
|
| 55 | - */ |
|
| 56 | - public function get_subject() { |
|
| 57 | - return $this->subject; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return string |
|
| 62 | - */ |
|
| 63 | - public function get_predicate() { |
|
| 64 | - return $this->predicate; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return Wordpress_Content_Id |
|
| 69 | - */ |
|
| 70 | - public function get_object() { |
|
| 71 | - return $this->object; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function hash() { |
|
| 75 | - // Define the hash algorithm for your object |
|
| 76 | - // Here's an example using the md5 hash function |
|
| 77 | - return md5( |
|
| 78 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
| 79 | - serialize( |
|
| 80 | - array( |
|
| 81 | - $this->get_subject(), |
|
| 82 | - $this->get_predicate(), |
|
| 83 | - $this->get_object(), |
|
| 84 | - ) |
|
| 85 | - ) |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function equals( Hashable_Interface $obj ) { |
|
| 90 | - return $this->hash() === $obj->hash(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @return string |
|
| 95 | - * @deprecated used for legacy compatibilty |
|
| 96 | - */ |
|
| 97 | - public function get_relation_type() { |
|
| 98 | - return $this->predicate; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return int |
|
| 103 | - * @deprecated used for legacy compatibility |
|
| 104 | - */ |
|
| 105 | - public function get_object_id() { |
|
| 106 | - return $this->object->get_id(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Subject type. |
|
| 111 | - * |
|
| 112 | - * @deprecated used for legacy compatibility |
|
| 113 | - */ |
|
| 114 | - public function get_subject_type() { |
|
| 115 | - return $this->subject->get_type(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Object type. |
|
| 120 | - * |
|
| 121 | - * @deprecated used for legacy compatibility |
|
| 122 | - */ |
|
| 123 | - public function get_object_type() { |
|
| 124 | - return $this->object->get_type(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public static function from_relation_instances( $instance ) { |
|
| 128 | - Assertions::is_set( $instance->subject_id ); |
|
| 129 | - Assertions::is_set( $instance->subject_type ); |
|
| 130 | - Assertions::is_set( $instance->predicate ); |
|
| 131 | - Assertions::is_set( $instance->object_id ); |
|
| 132 | - Assertions::is_set( $instance->object_type ); |
|
| 133 | - |
|
| 134 | - return new self( |
|
| 135 | - // Subject. |
|
| 136 | - new Wordpress_Content_Id( |
|
| 137 | - $instance->subject_id, |
|
| 138 | - $instance->subject_type |
|
| 139 | - ), |
|
| 140 | - // Object. |
|
| 141 | - new Wordpress_Content_Id( |
|
| 142 | - $instance->object_id, |
|
| 143 | - $instance->object_type |
|
| 144 | - ), |
|
| 145 | - // Predicate. |
|
| 146 | - $instance->predicate |
|
| 147 | - ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - public function jsonSerialize() { |
|
| 151 | - return array( |
|
| 152 | - 'subject' => $this->get_subject(), |
|
| 153 | - 'object' => $this->get_object(), |
|
| 154 | - 'predicate' => $this->get_predicate(), |
|
| 155 | - ); |
|
| 156 | - } |
|
| 19 | + /** |
|
| 20 | + * @var Wordpress_Content_Id |
|
| 21 | + */ |
|
| 22 | + private $subject; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + private $predicate; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param Wordpress_Content_Id $subject |
|
| 31 | + * @param Wordpress_Content_Id $object |
|
| 32 | + * @param string $predicate |
|
| 33 | + */ |
|
| 34 | + public function __construct( $subject, $object, $predicate ) { |
|
| 35 | + $this->subject = $subject; |
|
| 36 | + $this->predicate = $predicate; |
|
| 37 | + $this->object = $object; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var Wordpress_Content_Id |
|
| 42 | + */ |
|
| 43 | + private $object; |
|
| 44 | + |
|
| 45 | + public static function from_json( $item ) { |
|
| 46 | + $subject = Wordpress_Content_Id::from_json( $item['subject'] ); |
|
| 47 | + $object = Wordpress_Content_Id::from_json( $item['object'] ); |
|
| 48 | + $predicate = $item['predicate']; |
|
| 49 | + |
|
| 50 | + return new self( $subject, $object, $predicate ); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @return Wordpress_Content_Id |
|
| 55 | + */ |
|
| 56 | + public function get_subject() { |
|
| 57 | + return $this->subject; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return string |
|
| 62 | + */ |
|
| 63 | + public function get_predicate() { |
|
| 64 | + return $this->predicate; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return Wordpress_Content_Id |
|
| 69 | + */ |
|
| 70 | + public function get_object() { |
|
| 71 | + return $this->object; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function hash() { |
|
| 75 | + // Define the hash algorithm for your object |
|
| 76 | + // Here's an example using the md5 hash function |
|
| 77 | + return md5( |
|
| 78 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
| 79 | + serialize( |
|
| 80 | + array( |
|
| 81 | + $this->get_subject(), |
|
| 82 | + $this->get_predicate(), |
|
| 83 | + $this->get_object(), |
|
| 84 | + ) |
|
| 85 | + ) |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function equals( Hashable_Interface $obj ) { |
|
| 90 | + return $this->hash() === $obj->hash(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @return string |
|
| 95 | + * @deprecated used for legacy compatibilty |
|
| 96 | + */ |
|
| 97 | + public function get_relation_type() { |
|
| 98 | + return $this->predicate; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return int |
|
| 103 | + * @deprecated used for legacy compatibility |
|
| 104 | + */ |
|
| 105 | + public function get_object_id() { |
|
| 106 | + return $this->object->get_id(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Subject type. |
|
| 111 | + * |
|
| 112 | + * @deprecated used for legacy compatibility |
|
| 113 | + */ |
|
| 114 | + public function get_subject_type() { |
|
| 115 | + return $this->subject->get_type(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Object type. |
|
| 120 | + * |
|
| 121 | + * @deprecated used for legacy compatibility |
|
| 122 | + */ |
|
| 123 | + public function get_object_type() { |
|
| 124 | + return $this->object->get_type(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public static function from_relation_instances( $instance ) { |
|
| 128 | + Assertions::is_set( $instance->subject_id ); |
|
| 129 | + Assertions::is_set( $instance->subject_type ); |
|
| 130 | + Assertions::is_set( $instance->predicate ); |
|
| 131 | + Assertions::is_set( $instance->object_id ); |
|
| 132 | + Assertions::is_set( $instance->object_type ); |
|
| 133 | + |
|
| 134 | + return new self( |
|
| 135 | + // Subject. |
|
| 136 | + new Wordpress_Content_Id( |
|
| 137 | + $instance->subject_id, |
|
| 138 | + $instance->subject_type |
|
| 139 | + ), |
|
| 140 | + // Object. |
|
| 141 | + new Wordpress_Content_Id( |
|
| 142 | + $instance->object_id, |
|
| 143 | + $instance->object_type |
|
| 144 | + ), |
|
| 145 | + // Predicate. |
|
| 146 | + $instance->predicate |
|
| 147 | + ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + public function jsonSerialize() { |
|
| 151 | + return array( |
|
| 152 | + 'subject' => $this->get_subject(), |
|
| 153 | + 'object' => $this->get_object(), |
|
| 154 | + 'predicate' => $this->get_predicate(), |
|
| 155 | + ); |
|
| 156 | + } |
|
| 157 | 157 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @param Wordpress_Content_Id $object |
| 32 | 32 | * @param string $predicate |
| 33 | 33 | */ |
| 34 | - public function __construct( $subject, $object, $predicate ) { |
|
| 34 | + public function __construct($subject, $object, $predicate) { |
|
| 35 | 35 | $this->subject = $subject; |
| 36 | 36 | $this->predicate = $predicate; |
| 37 | 37 | $this->object = $object; |
@@ -42,12 +42,12 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | private $object; |
| 44 | 44 | |
| 45 | - public static function from_json( $item ) { |
|
| 46 | - $subject = Wordpress_Content_Id::from_json( $item['subject'] ); |
|
| 47 | - $object = Wordpress_Content_Id::from_json( $item['object'] ); |
|
| 45 | + public static function from_json($item) { |
|
| 46 | + $subject = Wordpress_Content_Id::from_json($item['subject']); |
|
| 47 | + $object = Wordpress_Content_Id::from_json($item['object']); |
|
| 48 | 48 | $predicate = $item['predicate']; |
| 49 | 49 | |
| 50 | - return new self( $subject, $object, $predicate ); |
|
| 50 | + return new self($subject, $object, $predicate); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | ); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - public function equals( Hashable_Interface $obj ) { |
|
| 89 | + public function equals(Hashable_Interface $obj) { |
|
| 90 | 90 | return $this->hash() === $obj->hash(); |
| 91 | 91 | } |
| 92 | 92 | |
@@ -124,12 +124,12 @@ discard block |
||
| 124 | 124 | return $this->object->get_type(); |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - public static function from_relation_instances( $instance ) { |
|
| 128 | - Assertions::is_set( $instance->subject_id ); |
|
| 129 | - Assertions::is_set( $instance->subject_type ); |
|
| 130 | - Assertions::is_set( $instance->predicate ); |
|
| 131 | - Assertions::is_set( $instance->object_id ); |
|
| 132 | - Assertions::is_set( $instance->object_type ); |
|
| 127 | + public static function from_relation_instances($instance) { |
|
| 128 | + Assertions::is_set($instance->subject_id); |
|
| 129 | + Assertions::is_set($instance->subject_type); |
|
| 130 | + Assertions::is_set($instance->predicate); |
|
| 131 | + Assertions::is_set($instance->object_id); |
|
| 132 | + Assertions::is_set($instance->object_type); |
|
| 133 | 133 | |
| 134 | 134 | return new self( |
| 135 | 135 | // Subject. |
@@ -6,49 +6,49 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class Wordlift_Install_3_32_0 extends Wordlift_Install { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * {@inheritdoc} |
|
| 11 | - */ |
|
| 12 | - protected static $version = '3.32.0'; |
|
| 13 | - |
|
| 14 | - public static function is_column_exists( $column_name ) { |
|
| 15 | - global $wpdb; |
|
| 16 | - |
|
| 17 | - return $wpdb->get_results( |
|
| 18 | - $wpdb->prepare( |
|
| 19 | - "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s", |
|
| 20 | - $column_name |
|
| 21 | - ) |
|
| 22 | - ); |
|
| 23 | - |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - public function install() { |
|
| 27 | - global $wpdb; |
|
| 28 | - // Allocate only 8 bytes, represent 2 ^ 8 values in signed form ( -128 to 127 ) |
|
| 29 | - // we default to 0 here, because we are going to represent Object_Type_Enum in |
|
| 30 | - // this field |
|
| 31 | - // const POST = 0; |
|
| 32 | - // const TERM = 1; |
|
| 33 | - // const HOMEPAGE = 2; |
|
| 34 | - // const USER = 3; |
|
| 35 | - // we add 0 as default since we want to add compat between old and new values. |
|
| 36 | - |
|
| 37 | - if ( ! self::is_column_exists( 'object_type' ) ) { |
|
| 38 | - // Add object_type column |
|
| 39 | - $wpdb->query( |
|
| 40 | - "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
| 9 | + /** |
|
| 10 | + * {@inheritdoc} |
|
| 11 | + */ |
|
| 12 | + protected static $version = '3.32.0'; |
|
| 13 | + |
|
| 14 | + public static function is_column_exists( $column_name ) { |
|
| 15 | + global $wpdb; |
|
| 16 | + |
|
| 17 | + return $wpdb->get_results( |
|
| 18 | + $wpdb->prepare( |
|
| 19 | + "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s", |
|
| 20 | + $column_name |
|
| 21 | + ) |
|
| 22 | + ); |
|
| 23 | + |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + public function install() { |
|
| 27 | + global $wpdb; |
|
| 28 | + // Allocate only 8 bytes, represent 2 ^ 8 values in signed form ( -128 to 127 ) |
|
| 29 | + // we default to 0 here, because we are going to represent Object_Type_Enum in |
|
| 30 | + // this field |
|
| 31 | + // const POST = 0; |
|
| 32 | + // const TERM = 1; |
|
| 33 | + // const HOMEPAGE = 2; |
|
| 34 | + // const USER = 3; |
|
| 35 | + // we add 0 as default since we want to add compat between old and new values. |
|
| 36 | + |
|
| 37 | + if ( ! self::is_column_exists( 'object_type' ) ) { |
|
| 38 | + // Add object_type column |
|
| 39 | + $wpdb->query( |
|
| 40 | + "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
| 41 | 41 | ADD object_type TINYINT DEFAULT 0;" |
| 42 | - ); |
|
| 43 | - } |
|
| 42 | + ); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - if ( ! self::is_column_exists( 'subject_type' ) ) { |
|
| 46 | - // Add subject_type column. |
|
| 47 | - $wpdb->query( |
|
| 48 | - "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
| 45 | + if ( ! self::is_column_exists( 'subject_type' ) ) { |
|
| 46 | + // Add subject_type column. |
|
| 47 | + $wpdb->query( |
|
| 48 | + "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
| 49 | 49 | ADD subject_type TINYINT DEFAULT 0;" |
| 50 | - ); |
|
| 51 | - } |
|
| 52 | - } |
|
| 50 | + ); |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | 54 | } |
@@ -23,61 +23,61 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | function wl_linked_data_save_post( $post_id ) { |
| 25 | 25 | |
| 26 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 27 | - |
|
| 28 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 29 | - |
|
| 30 | - // If it's not numeric exit from here. |
|
| 31 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 32 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 33 | - |
|
| 34 | - return; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - // Get the post type and check whether it supports the editor. |
|
| 38 | - // |
|
| 39 | - // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
| 40 | - $post_type = get_post_type( $post_id ); |
|
| 41 | - /** |
|
| 42 | - * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
| 43 | - * |
|
| 44 | - * @since 3.19.4 |
|
| 45 | - * |
|
| 46 | - * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 47 | - */ |
|
| 48 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 49 | - |
|
| 50 | - $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ); |
|
| 51 | - // Bail out if it's not an entity. |
|
| 52 | - if ( ! $is_editor_supported |
|
| 53 | - && ! $is_no_editor_analysis_enabled ) { |
|
| 54 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 55 | - |
|
| 56 | - return; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Only process valid post types |
|
| 61 | - * |
|
| 62 | - * @since 3.25.6 |
|
| 63 | - */ |
|
| 64 | - $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 65 | - |
|
| 66 | - // Bail out if it's not a valid entity. |
|
| 67 | - if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) { |
|
| 68 | - $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
| 69 | - |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - // Unhook this function so it doesn't loop infinitely. |
|
| 74 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 75 | - |
|
| 76 | - // raise the *wl_linked_data_save_post* event. |
|
| 77 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 78 | - |
|
| 79 | - // Re-hook this function. |
|
| 80 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 26 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 27 | + |
|
| 28 | + $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 29 | + |
|
| 30 | + // If it's not numeric exit from here. |
|
| 31 | + if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 32 | + $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 33 | + |
|
| 34 | + return; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + // Get the post type and check whether it supports the editor. |
|
| 38 | + // |
|
| 39 | + // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
| 40 | + $post_type = get_post_type( $post_id ); |
|
| 41 | + /** |
|
| 42 | + * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
| 43 | + * |
|
| 44 | + * @since 3.19.4 |
|
| 45 | + * |
|
| 46 | + * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 47 | + */ |
|
| 48 | + $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 49 | + |
|
| 50 | + $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ); |
|
| 51 | + // Bail out if it's not an entity. |
|
| 52 | + if ( ! $is_editor_supported |
|
| 53 | + && ! $is_no_editor_analysis_enabled ) { |
|
| 54 | + $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 55 | + |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Only process valid post types |
|
| 61 | + * |
|
| 62 | + * @since 3.25.6 |
|
| 63 | + */ |
|
| 64 | + $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 65 | + |
|
| 66 | + // Bail out if it's not a valid entity. |
|
| 67 | + if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) { |
|
| 68 | + $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
| 69 | + |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + // Unhook this function so it doesn't loop infinitely. |
|
| 74 | + remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 75 | + |
|
| 76 | + // raise the *wl_linked_data_save_post* event. |
|
| 77 | + do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 78 | + |
|
| 79 | + // Re-hook this function. |
|
| 80 | + add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | add_action( 'save_post', 'wl_linked_data_save_post' ); |
@@ -93,204 +93,204 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
| 95 | 95 | |
| 96 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 97 | - |
|
| 98 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 99 | - |
|
| 100 | - // Ignore auto-saves |
|
| 101 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 102 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 103 | - |
|
| 104 | - return; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // get the current post. |
|
| 108 | - $post = get_post( $post_id ); |
|
| 109 | - |
|
| 110 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 111 | - |
|
| 112 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 113 | - |
|
| 114 | - // Get the entity service instance. |
|
| 115 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 116 | - $uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
| 117 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 118 | - |
|
| 119 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 120 | - $entities_uri_mapping = array(); |
|
| 121 | - |
|
| 122 | - // Save all the selected internal entity uris to this variable. |
|
| 123 | - $internal_entity_uris = array(); |
|
| 124 | - |
|
| 125 | - // Save the entities coming with POST data. |
|
| 126 | - if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 127 | - $data = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 128 | - $entities_via_post = $data['wl_entities']; |
|
| 129 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 130 | - wl_write_log( wp_json_encode( $entities_via_post ) ); |
|
| 131 | - wl_write_log( ']' ); |
|
| 132 | - |
|
| 133 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 134 | - |
|
| 135 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) { |
|
| 136 | - $existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
| 137 | - if ( isset( $existing_entity ) ) { |
|
| 138 | - $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID ); |
|
| 139 | - // Type doesn't match, continue to create a new entity. |
|
| 140 | - if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) { |
|
| 141 | - $existing_entity = null; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } else { |
|
| 145 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 146 | - // 1. when $entity_uri is an internal uri |
|
| 147 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 148 | - $existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // Don't save the entities which are not found, but also local. |
|
| 152 | - if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) { |
|
| 153 | - $internal_entity_uris[] = $entity_uri; |
|
| 154 | - continue; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if ( ! isset( $existing_entity ) ) { |
|
| 158 | - // Update entity data with related post |
|
| 159 | - $entity['related_post_id'] = $post_id; |
|
| 160 | - // New entity, save it. |
|
| 161 | - $existing_entity = wl_save_entity( $entity ); |
|
| 162 | - } else { |
|
| 163 | - // Existing entity, update post status. |
|
| 164 | - if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) { |
|
| 165 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 166 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
| 167 | - ? $post->post_status : 'draft'; |
|
| 168 | - wl_update_post_status( $existing_entity->ID, $post_status ); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) ); |
|
| 173 | - |
|
| 174 | - $internal_entity_uris[] = $uri; |
|
| 175 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 176 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 177 | - |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Replace tmp uris in content post if needed |
|
| 182 | - $updated_post_content = $post->post_content; |
|
| 183 | - |
|
| 184 | - // Update the post content if we found mappings of new entities. |
|
| 185 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 186 | - // Save each entity and store the post id. |
|
| 187 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 188 | - if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
| 189 | - continue; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // Update the post content. |
|
| 196 | - /** |
|
| 197 | - * Note: wp_update_post do stripslashes before saving the |
|
| 198 | - * content, so add the slashes to prevent back slash getting |
|
| 199 | - * removed. |
|
| 200 | - */ |
|
| 201 | - wp_update_post( |
|
| 202 | - array( |
|
| 203 | - 'ID' => $post->ID, |
|
| 204 | - 'post_content' => addslashes( $updated_post_content ), |
|
| 205 | - ) |
|
| 206 | - ); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // Reset previously saved instances. |
|
| 210 | - wl_core_delete_relation_instances( $post_id ); |
|
| 211 | - |
|
| 212 | - $content_id = Wordpress_Content_Id::create_post( $post->ID ); |
|
| 213 | - $relations = Relation_Service::get_instance()->get_relations( $content_id ); |
|
| 214 | - |
|
| 215 | - if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) { |
|
| 216 | - $relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) ); |
|
| 217 | - } |
|
| 218 | - /** |
|
| 219 | - * Filter the relations, we dont want to create a relation |
|
| 220 | - * to uncategorized term, we are already filtering this on jsonld, |
|
| 221 | - * |
|
| 222 | - * @todo: do i need to move this to post-term-relation-service ? |
|
| 223 | - */ |
|
| 224 | - $filtered_relations = array_filter( |
|
| 225 | - $relations->toArray(), |
|
| 226 | - function ( $item ) { |
|
| 227 | - /** |
|
| 228 | - * @var $item Relation |
|
| 229 | - */ |
|
| 230 | - $object = $item->get_object(); |
|
| 231 | - |
|
| 232 | - return ! ( $object->get_type() === Object_Type_Enum::TERM |
|
| 233 | - && $object->get_id() === 1 ); |
|
| 234 | - } |
|
| 235 | - ); |
|
| 236 | - |
|
| 237 | - // Save relation instances |
|
| 238 | - /** @var Relation $relation */ |
|
| 239 | - foreach ( $filtered_relations as $relation ) { |
|
| 240 | - $subject = $relation->get_subject(); |
|
| 241 | - $object = $relation->get_object(); |
|
| 242 | - |
|
| 243 | - wl_core_add_relation_instance( |
|
| 244 | - // subject id. |
|
| 245 | - $subject->get_id(), |
|
| 246 | - // what, where, when, who |
|
| 247 | - $relation->get_predicate(), |
|
| 248 | - // object id. |
|
| 249 | - $object->get_id(), |
|
| 250 | - // Subject type. |
|
| 251 | - $subject->get_type(), |
|
| 252 | - // Object type. |
|
| 253 | - $object->get_type() |
|
| 254 | - ); |
|
| 255 | - |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 259 | - // Save post metadata if available |
|
| 260 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 261 | - |
|
| 262 | - $fields = array( |
|
| 263 | - Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 264 | - Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 265 | - ); |
|
| 266 | - |
|
| 267 | - // Unlink topic taxonomy terms |
|
| 268 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 269 | - |
|
| 270 | - foreach ( $fields as $field ) { |
|
| 271 | - |
|
| 272 | - // Delete current values |
|
| 273 | - delete_post_meta( $post->ID, $field ); |
|
| 274 | - // Retrieve the entity uri |
|
| 275 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 276 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 277 | - |
|
| 278 | - if ( empty( $uri ) ) { |
|
| 279 | - continue; |
|
| 280 | - } |
|
| 281 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 282 | - |
|
| 283 | - if ( $entity ) { |
|
| 284 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 285 | - // Set also the topic taxonomy |
|
| 286 | - if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) { |
|
| 287 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 96 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 97 | + |
|
| 98 | + $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 99 | + |
|
| 100 | + // Ignore auto-saves |
|
| 101 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 102 | + $log->trace( 'Doing autosave, skipping...' ); |
|
| 103 | + |
|
| 104 | + return; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // get the current post. |
|
| 108 | + $post = get_post( $post_id ); |
|
| 109 | + |
|
| 110 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 111 | + |
|
| 112 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 113 | + |
|
| 114 | + // Get the entity service instance. |
|
| 115 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 116 | + $uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
| 117 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 118 | + |
|
| 119 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 120 | + $entities_uri_mapping = array(); |
|
| 121 | + |
|
| 122 | + // Save all the selected internal entity uris to this variable. |
|
| 123 | + $internal_entity_uris = array(); |
|
| 124 | + |
|
| 125 | + // Save the entities coming with POST data. |
|
| 126 | + if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 127 | + $data = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 128 | + $entities_via_post = $data['wl_entities']; |
|
| 129 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 130 | + wl_write_log( wp_json_encode( $entities_via_post ) ); |
|
| 131 | + wl_write_log( ']' ); |
|
| 132 | + |
|
| 133 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 134 | + |
|
| 135 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) { |
|
| 136 | + $existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
| 137 | + if ( isset( $existing_entity ) ) { |
|
| 138 | + $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID ); |
|
| 139 | + // Type doesn't match, continue to create a new entity. |
|
| 140 | + if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) { |
|
| 141 | + $existing_entity = null; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } else { |
|
| 145 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 146 | + // 1. when $entity_uri is an internal uri |
|
| 147 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 148 | + $existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // Don't save the entities which are not found, but also local. |
|
| 152 | + if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) { |
|
| 153 | + $internal_entity_uris[] = $entity_uri; |
|
| 154 | + continue; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if ( ! isset( $existing_entity ) ) { |
|
| 158 | + // Update entity data with related post |
|
| 159 | + $entity['related_post_id'] = $post_id; |
|
| 160 | + // New entity, save it. |
|
| 161 | + $existing_entity = wl_save_entity( $entity ); |
|
| 162 | + } else { |
|
| 163 | + // Existing entity, update post status. |
|
| 164 | + if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) { |
|
| 165 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 166 | + $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
| 167 | + ? $post->post_status : 'draft'; |
|
| 168 | + wl_update_post_status( $existing_entity->ID, $post_status ); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) ); |
|
| 173 | + |
|
| 174 | + $internal_entity_uris[] = $uri; |
|
| 175 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 176 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 177 | + |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // Replace tmp uris in content post if needed |
|
| 182 | + $updated_post_content = $post->post_content; |
|
| 183 | + |
|
| 184 | + // Update the post content if we found mappings of new entities. |
|
| 185 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
| 186 | + // Save each entity and store the post id. |
|
| 187 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 188 | + if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
| 189 | + continue; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // Update the post content. |
|
| 196 | + /** |
|
| 197 | + * Note: wp_update_post do stripslashes before saving the |
|
| 198 | + * content, so add the slashes to prevent back slash getting |
|
| 199 | + * removed. |
|
| 200 | + */ |
|
| 201 | + wp_update_post( |
|
| 202 | + array( |
|
| 203 | + 'ID' => $post->ID, |
|
| 204 | + 'post_content' => addslashes( $updated_post_content ), |
|
| 205 | + ) |
|
| 206 | + ); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // Reset previously saved instances. |
|
| 210 | + wl_core_delete_relation_instances( $post_id ); |
|
| 211 | + |
|
| 212 | + $content_id = Wordpress_Content_Id::create_post( $post->ID ); |
|
| 213 | + $relations = Relation_Service::get_instance()->get_relations( $content_id ); |
|
| 214 | + |
|
| 215 | + if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) { |
|
| 216 | + $relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) ); |
|
| 217 | + } |
|
| 218 | + /** |
|
| 219 | + * Filter the relations, we dont want to create a relation |
|
| 220 | + * to uncategorized term, we are already filtering this on jsonld, |
|
| 221 | + * |
|
| 222 | + * @todo: do i need to move this to post-term-relation-service ? |
|
| 223 | + */ |
|
| 224 | + $filtered_relations = array_filter( |
|
| 225 | + $relations->toArray(), |
|
| 226 | + function ( $item ) { |
|
| 227 | + /** |
|
| 228 | + * @var $item Relation |
|
| 229 | + */ |
|
| 230 | + $object = $item->get_object(); |
|
| 231 | + |
|
| 232 | + return ! ( $object->get_type() === Object_Type_Enum::TERM |
|
| 233 | + && $object->get_id() === 1 ); |
|
| 234 | + } |
|
| 235 | + ); |
|
| 236 | + |
|
| 237 | + // Save relation instances |
|
| 238 | + /** @var Relation $relation */ |
|
| 239 | + foreach ( $filtered_relations as $relation ) { |
|
| 240 | + $subject = $relation->get_subject(); |
|
| 241 | + $object = $relation->get_object(); |
|
| 242 | + |
|
| 243 | + wl_core_add_relation_instance( |
|
| 244 | + // subject id. |
|
| 245 | + $subject->get_id(), |
|
| 246 | + // what, where, when, who |
|
| 247 | + $relation->get_predicate(), |
|
| 248 | + // object id. |
|
| 249 | + $object->get_id(), |
|
| 250 | + // Subject type. |
|
| 251 | + $subject->get_type(), |
|
| 252 | + // Object type. |
|
| 253 | + $object->get_type() |
|
| 254 | + ); |
|
| 255 | + |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 259 | + // Save post metadata if available |
|
| 260 | + $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 261 | + |
|
| 262 | + $fields = array( |
|
| 263 | + Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 264 | + Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 265 | + ); |
|
| 266 | + |
|
| 267 | + // Unlink topic taxonomy terms |
|
| 268 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 269 | + |
|
| 270 | + foreach ( $fields as $field ) { |
|
| 271 | + |
|
| 272 | + // Delete current values |
|
| 273 | + delete_post_meta( $post->ID, $field ); |
|
| 274 | + // Retrieve the entity uri |
|
| 275 | + $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 276 | + stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 277 | + |
|
| 278 | + if ( empty( $uri ) ) { |
|
| 279 | + continue; |
|
| 280 | + } |
|
| 281 | + $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 282 | + |
|
| 283 | + if ( $entity ) { |
|
| 284 | + add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 285 | + // Set also the topic taxonomy |
|
| 286 | + if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) { |
|
| 287 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -313,221 +313,221 @@ discard block |
||
| 313 | 313 | */ |
| 314 | 314 | function wl_save_entity( $entity_data ) { |
| 315 | 315 | |
| 316 | - // Required for REST API calls |
|
| 317 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 318 | - require_once ABSPATH . 'wp-admin/includes/image.php'; |
|
| 319 | - } |
|
| 316 | + // Required for REST API calls |
|
| 317 | + if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 318 | + require_once ABSPATH . 'wp-admin/includes/image.php'; |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 321 | + $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 322 | 322 | |
| 323 | - /* |
|
| 323 | + /* |
|
| 324 | 324 | * Data is coming from a $_POST, sanitize it. |
| 325 | 325 | * |
| 326 | 326 | * @since 3.19.4 |
| 327 | 327 | * |
| 328 | 328 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 329 | 329 | */ |
| 330 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 331 | - $type_uri = $entity_data['main_type']; |
|
| 332 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 333 | - $description = $entity_data['description']; |
|
| 334 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 335 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 336 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 337 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 338 | - // Get the synonyms. |
|
| 339 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 340 | - |
|
| 341 | - // Check whether an entity already exists with the provided URI. |
|
| 342 | - $uri = $entity_data['uri']; |
|
| 343 | - if ( isset( $uri ) ) { |
|
| 344 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 345 | - if ( isset( $post ) ) { |
|
| 346 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 347 | - |
|
| 348 | - return $post; |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - // Prepare properties of the new entity. |
|
| 353 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 354 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id ) |
|
| 355 | - ? get_post_status( $related_post_id ) : 'draft'; |
|
| 356 | - |
|
| 357 | - $params = array( |
|
| 358 | - // @@todo: we don't want an entity to be automatically published. |
|
| 359 | - 'post_status' => $post_status, |
|
| 360 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 361 | - 'post_title' => $label, |
|
| 362 | - 'post_content' => $description, |
|
| 363 | - 'post_excerpt' => '', |
|
| 364 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 365 | - // entity with a post_name already set, since this call is made only for |
|
| 366 | - // new entities. |
|
| 367 | - // |
|
| 368 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 369 | - 'post_name' => sanitize_title( $label ), |
|
| 370 | - ); |
|
| 371 | - |
|
| 372 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 373 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 374 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 375 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 376 | - // is created when saving a post. |
|
| 377 | - global $wpseo_metabox, $seo_ultimate; |
|
| 378 | - if ( isset( $wpseo_metabox ) ) { |
|
| 379 | - remove_action( |
|
| 380 | - 'wp_insert_post', |
|
| 381 | - array( |
|
| 382 | - $wpseo_metabox, |
|
| 383 | - 'save_postdata', |
|
| 384 | - ) |
|
| 385 | - ); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - if ( isset( $seo_ultimate ) ) { |
|
| 389 | - remove_action( |
|
| 390 | - 'save_post', |
|
| 391 | - array( |
|
| 392 | - $seo_ultimate, |
|
| 393 | - 'save_postmeta_box', |
|
| 394 | - ) |
|
| 395 | - ); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 399 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 400 | - // to the save_post and restore them after we saved the entity. |
|
| 401 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 402 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 403 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 404 | - global $wp_filter; |
|
| 405 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 406 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 407 | - |
|
| 408 | - $log->trace( 'Going to insert new post...' ); |
|
| 409 | - |
|
| 410 | - // create or update the post. |
|
| 411 | - $post_id = wp_insert_post( $params, true ); |
|
| 412 | - |
|
| 413 | - // Setting the alternative labels for this entity. |
|
| 414 | - Wordlift_Entity_Service::get_instance() |
|
| 415 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 416 | - |
|
| 417 | - // Restore all the existing filters. |
|
| 418 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 419 | - |
|
| 420 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 421 | - if ( isset( $wpseo_metabox ) ) { |
|
| 422 | - add_action( |
|
| 423 | - 'wp_insert_post', |
|
| 424 | - array( |
|
| 425 | - $wpseo_metabox, |
|
| 426 | - 'save_postdata', |
|
| 427 | - ) |
|
| 428 | - ); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 432 | - if ( isset( $seo_ultimate ) ) { |
|
| 433 | - add_action( |
|
| 434 | - 'save_post', |
|
| 435 | - array( |
|
| 436 | - $seo_ultimate, |
|
| 437 | - 'save_postmeta_box', |
|
| 438 | - ), |
|
| 439 | - 10, |
|
| 440 | - 2 |
|
| 441 | - ); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - // TODO: handle errors. |
|
| 445 | - if ( is_wp_error( $post_id ) ) { |
|
| 446 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 447 | - |
|
| 448 | - // inform an error occurred. |
|
| 449 | - return null; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 453 | - |
|
| 454 | - // Save the entity types. |
|
| 455 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 456 | - |
|
| 457 | - // Get a dataset URI for the entity. |
|
| 458 | - $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
| 459 | - |
|
| 460 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 461 | - if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) && |
|
| 462 | - $wl_uri !== $uri && |
|
| 463 | - // Only remote entities |
|
| 464 | - 0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) |
|
| 465 | - ) { |
|
| 466 | - array_push( $same_as, $uri ); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - // Save the sameAs data for the entity. |
|
| 470 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 471 | - |
|
| 472 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 473 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 474 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - // Call hooks. |
|
| 478 | - do_action( 'wl_save_entity', $post_id ); |
|
| 479 | - |
|
| 480 | - foreach ( $images as $image_remote_url ) { |
|
| 481 | - |
|
| 482 | - // Check if image is already present in local DB |
|
| 483 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 484 | - // Do nothing. |
|
| 485 | - continue; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 489 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 490 | - |
|
| 491 | - // Skip if an existing image is found. |
|
| 492 | - if ( null !== $existing_image ) { |
|
| 493 | - continue; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - // Save the image and get the local path. |
|
| 497 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 498 | - |
|
| 499 | - if ( false === $image || is_wp_error( $image ) ) { |
|
| 500 | - continue; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - // Get the local URL. |
|
| 504 | - $filename = $image['path']; |
|
| 505 | - $url = $image['url']; |
|
| 506 | - $content_type = $image['content_type']; |
|
| 507 | - |
|
| 508 | - $attachment = array( |
|
| 509 | - 'guid' => $url, |
|
| 510 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 511 | - 'post_title' => $label, |
|
| 512 | - // Set the title to the post title. |
|
| 513 | - 'post_content' => '', |
|
| 514 | - 'post_status' => 'inherit', |
|
| 515 | - 'post_mime_type' => $content_type, |
|
| 516 | - ); |
|
| 517 | - |
|
| 518 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 519 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 520 | - |
|
| 521 | - // Set the source URL for the image. |
|
| 522 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 523 | - |
|
| 524 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 525 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 526 | - |
|
| 527 | - // Set it as the featured image. |
|
| 528 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - // finally return the entity post. |
|
| 532 | - return get_post( $post_id ); |
|
| 330 | + $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 331 | + $type_uri = $entity_data['main_type']; |
|
| 332 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 333 | + $description = $entity_data['description']; |
|
| 334 | + $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 335 | + $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 336 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 337 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 338 | + // Get the synonyms. |
|
| 339 | + $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 340 | + |
|
| 341 | + // Check whether an entity already exists with the provided URI. |
|
| 342 | + $uri = $entity_data['uri']; |
|
| 343 | + if ( isset( $uri ) ) { |
|
| 344 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 345 | + if ( isset( $post ) ) { |
|
| 346 | + $log->debug( "Post already exists for URI $uri." ); |
|
| 347 | + |
|
| 348 | + return $post; |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + // Prepare properties of the new entity. |
|
| 353 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 354 | + $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id ) |
|
| 355 | + ? get_post_status( $related_post_id ) : 'draft'; |
|
| 356 | + |
|
| 357 | + $params = array( |
|
| 358 | + // @@todo: we don't want an entity to be automatically published. |
|
| 359 | + 'post_status' => $post_status, |
|
| 360 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 361 | + 'post_title' => $label, |
|
| 362 | + 'post_content' => $description, |
|
| 363 | + 'post_excerpt' => '', |
|
| 364 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 365 | + // entity with a post_name already set, since this call is made only for |
|
| 366 | + // new entities. |
|
| 367 | + // |
|
| 368 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 369 | + 'post_name' => sanitize_title( $label ), |
|
| 370 | + ); |
|
| 371 | + |
|
| 372 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 373 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 374 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 375 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 376 | + // is created when saving a post. |
|
| 377 | + global $wpseo_metabox, $seo_ultimate; |
|
| 378 | + if ( isset( $wpseo_metabox ) ) { |
|
| 379 | + remove_action( |
|
| 380 | + 'wp_insert_post', |
|
| 381 | + array( |
|
| 382 | + $wpseo_metabox, |
|
| 383 | + 'save_postdata', |
|
| 384 | + ) |
|
| 385 | + ); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + if ( isset( $seo_ultimate ) ) { |
|
| 389 | + remove_action( |
|
| 390 | + 'save_post', |
|
| 391 | + array( |
|
| 392 | + $seo_ultimate, |
|
| 393 | + 'save_postmeta_box', |
|
| 394 | + ) |
|
| 395 | + ); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 399 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 400 | + // to the save_post and restore them after we saved the entity. |
|
| 401 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 402 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 403 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 404 | + global $wp_filter; |
|
| 405 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 406 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 407 | + |
|
| 408 | + $log->trace( 'Going to insert new post...' ); |
|
| 409 | + |
|
| 410 | + // create or update the post. |
|
| 411 | + $post_id = wp_insert_post( $params, true ); |
|
| 412 | + |
|
| 413 | + // Setting the alternative labels for this entity. |
|
| 414 | + Wordlift_Entity_Service::get_instance() |
|
| 415 | + ->set_alternative_labels( $post_id, $synonyms ); |
|
| 416 | + |
|
| 417 | + // Restore all the existing filters. |
|
| 418 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 419 | + |
|
| 420 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 421 | + if ( isset( $wpseo_metabox ) ) { |
|
| 422 | + add_action( |
|
| 423 | + 'wp_insert_post', |
|
| 424 | + array( |
|
| 425 | + $wpseo_metabox, |
|
| 426 | + 'save_postdata', |
|
| 427 | + ) |
|
| 428 | + ); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 432 | + if ( isset( $seo_ultimate ) ) { |
|
| 433 | + add_action( |
|
| 434 | + 'save_post', |
|
| 435 | + array( |
|
| 436 | + $seo_ultimate, |
|
| 437 | + 'save_postmeta_box', |
|
| 438 | + ), |
|
| 439 | + 10, |
|
| 440 | + 2 |
|
| 441 | + ); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + // TODO: handle errors. |
|
| 445 | + if ( is_wp_error( $post_id ) ) { |
|
| 446 | + $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 447 | + |
|
| 448 | + // inform an error occurred. |
|
| 449 | + return null; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 453 | + |
|
| 454 | + // Save the entity types. |
|
| 455 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 456 | + |
|
| 457 | + // Get a dataset URI for the entity. |
|
| 458 | + $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
| 459 | + |
|
| 460 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 461 | + if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) && |
|
| 462 | + $wl_uri !== $uri && |
|
| 463 | + // Only remote entities |
|
| 464 | + 0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) |
|
| 465 | + ) { |
|
| 466 | + array_push( $same_as, $uri ); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + // Save the sameAs data for the entity. |
|
| 470 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 471 | + |
|
| 472 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 473 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 474 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + // Call hooks. |
|
| 478 | + do_action( 'wl_save_entity', $post_id ); |
|
| 479 | + |
|
| 480 | + foreach ( $images as $image_remote_url ) { |
|
| 481 | + |
|
| 482 | + // Check if image is already present in local DB |
|
| 483 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 484 | + // Do nothing. |
|
| 485 | + continue; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 489 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 490 | + |
|
| 491 | + // Skip if an existing image is found. |
|
| 492 | + if ( null !== $existing_image ) { |
|
| 493 | + continue; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + // Save the image and get the local path. |
|
| 497 | + $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 498 | + |
|
| 499 | + if ( false === $image || is_wp_error( $image ) ) { |
|
| 500 | + continue; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + // Get the local URL. |
|
| 504 | + $filename = $image['path']; |
|
| 505 | + $url = $image['url']; |
|
| 506 | + $content_type = $image['content_type']; |
|
| 507 | + |
|
| 508 | + $attachment = array( |
|
| 509 | + 'guid' => $url, |
|
| 510 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 511 | + 'post_title' => $label, |
|
| 512 | + // Set the title to the post title. |
|
| 513 | + 'post_content' => '', |
|
| 514 | + 'post_status' => 'inherit', |
|
| 515 | + 'post_mime_type' => $content_type, |
|
| 516 | + ); |
|
| 517 | + |
|
| 518 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 519 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 520 | + |
|
| 521 | + // Set the source URL for the image. |
|
| 522 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 523 | + |
|
| 524 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 525 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 526 | + |
|
| 527 | + // Set it as the featured image. |
|
| 528 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + // finally return the entity post. |
|
| 532 | + return get_post( $post_id ); |
|
| 533 | 533 | } |
@@ -21,15 +21,15 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * @since 3.0.0 |
| 23 | 23 | */ |
| 24 | -function wl_linked_data_save_post( $post_id ) { |
|
| 24 | +function wl_linked_data_save_post($post_id) { |
|
| 25 | 25 | |
| 26 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 26 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post'); |
|
| 27 | 27 | |
| 28 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 28 | + $log->trace("Saving post $post_id to Linked Data..."); |
|
| 29 | 29 | |
| 30 | 30 | // If it's not numeric exit from here. |
| 31 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 32 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 31 | + if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) { |
|
| 32 | + $log->debug("Skipping $post_id, because id is not numeric or is a post revision."); |
|
| 33 | 33 | |
| 34 | 34 | return; |
| 35 | 35 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | // Get the post type and check whether it supports the editor. |
| 38 | 38 | // |
| 39 | 39 | // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
| 40 | - $post_type = get_post_type( $post_id ); |
|
| 40 | + $post_type = get_post_type($post_id); |
|
| 41 | 41 | /** |
| 42 | 42 | * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
| 43 | 43 | * |
@@ -45,13 +45,13 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
| 47 | 47 | */ |
| 48 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 48 | + $is_editor_supported = wl_post_type_supports_editor($post_type); |
|
| 49 | 49 | |
| 50 | - $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ); |
|
| 50 | + $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id); |
|
| 51 | 51 | // Bail out if it's not an entity. |
| 52 | 52 | if ( ! $is_editor_supported |
| 53 | - && ! $is_no_editor_analysis_enabled ) { |
|
| 54 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 53 | + && ! $is_no_editor_analysis_enabled) { |
|
| 54 | + $log->debug("Skipping $post_id, because $post_type doesn't support the editor (content)."); |
|
| 55 | 55 | |
| 56 | 56 | return; |
| 57 | 57 | } |
@@ -64,23 +64,23 @@ discard block |
||
| 64 | 64 | $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 65 | 65 | |
| 66 | 66 | // Bail out if it's not a valid entity. |
| 67 | - if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) { |
|
| 68 | - $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
| 67 | + if ( ! in_array($post_type, $supported_types, true) && ! $is_no_editor_analysis_enabled) { |
|
| 68 | + $log->debug("Skipping $post_id, because $post_type is not a valid entity."); |
|
| 69 | 69 | |
| 70 | 70 | return; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // Unhook this function so it doesn't loop infinitely. |
| 74 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 74 | + remove_action('save_post', 'wl_linked_data_save_post'); |
|
| 75 | 75 | |
| 76 | 76 | // raise the *wl_linked_data_save_post* event. |
| 77 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 77 | + do_action('wl_linked_data_save_post', $post_id); |
|
| 78 | 78 | |
| 79 | 79 | // Re-hook this function. |
| 80 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 80 | + add_action('save_post', 'wl_linked_data_save_post'); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | -add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 83 | +add_action('save_post', 'wl_linked_data_save_post'); |
|
| 84 | 84 | |
| 85 | 85 | /** |
| 86 | 86 | * Save the post to the triple store. Also saves the entities locally and on the triple store. |
@@ -91,23 +91,23 @@ discard block |
||
| 91 | 91 | * |
| 92 | 92 | * @since 3.0.0 |
| 93 | 93 | */ |
| 94 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
| 94 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
| 95 | 95 | |
| 96 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 96 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities'); |
|
| 97 | 97 | |
| 98 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 98 | + $log->trace("Saving $post_id to Linked Data along with related entities..."); |
|
| 99 | 99 | |
| 100 | 100 | // Ignore auto-saves |
| 101 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 102 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 101 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 102 | + $log->trace('Doing autosave, skipping...'); |
|
| 103 | 103 | |
| 104 | 104 | return; |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | // get the current post. |
| 108 | - $post = get_post( $post_id ); |
|
| 108 | + $post = get_post($post_id); |
|
| 109 | 109 | |
| 110 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 110 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 111 | 111 | |
| 112 | 112 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
| 113 | 113 | |
@@ -123,21 +123,21 @@ discard block |
||
| 123 | 123 | $internal_entity_uris = array(); |
| 124 | 124 | |
| 125 | 125 | // Save the entities coming with POST data. |
| 126 | - if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 127 | - $data = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 126 | + if (isset($_POST['wl_entities'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 127 | + $data = filter_var_array($_POST, array('wl_entities' => array('flags' => FILTER_REQUIRE_ARRAY))); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 128 | 128 | $entities_via_post = $data['wl_entities']; |
| 129 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 130 | - wl_write_log( wp_json_encode( $entities_via_post ) ); |
|
| 131 | - wl_write_log( ']' ); |
|
| 129 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
| 130 | + wl_write_log(wp_json_encode($entities_via_post)); |
|
| 131 | + wl_write_log(']'); |
|
| 132 | 132 | |
| 133 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 133 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
| 134 | 134 | |
| 135 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) { |
|
| 136 | - $existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
| 137 | - if ( isset( $existing_entity ) ) { |
|
| 138 | - $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID ); |
|
| 135 | + if (preg_match('/^local-entity-.+/', $entity_uri)) { |
|
| 136 | + $existing_entity = get_page_by_title($entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types()); |
|
| 137 | + if (isset($existing_entity)) { |
|
| 138 | + $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get($existing_entity->ID); |
|
| 139 | 139 | // Type doesn't match, continue to create a new entity. |
| 140 | - if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) { |
|
| 140 | + if ( ! isset($existing_entity_type) || $existing_entity_type['css_class'] !== $entity['main_type']) { |
|
| 141 | 141 | $existing_entity = null; |
| 142 | 142 | } |
| 143 | 143 | } |
@@ -145,35 +145,35 @@ discard block |
||
| 145 | 145 | // Look if current entity uri matches an internal existing entity, meaning: |
| 146 | 146 | // 1. when $entity_uri is an internal uri |
| 147 | 147 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 148 | - $existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 148 | + $existing_entity = $entity_service->get_entity_post_by_uri($entity_uri); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | // Don't save the entities which are not found, but also local. |
| 152 | - if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) { |
|
| 152 | + if ( ! isset($existing_entity) && $uri_service->is_internal($entity_uri)) { |
|
| 153 | 153 | $internal_entity_uris[] = $entity_uri; |
| 154 | 154 | continue; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - if ( ! isset( $existing_entity ) ) { |
|
| 157 | + if ( ! isset($existing_entity)) { |
|
| 158 | 158 | // Update entity data with related post |
| 159 | 159 | $entity['related_post_id'] = $post_id; |
| 160 | 160 | // New entity, save it. |
| 161 | - $existing_entity = wl_save_entity( $entity ); |
|
| 161 | + $existing_entity = wl_save_entity($entity); |
|
| 162 | 162 | } else { |
| 163 | 163 | // Existing entity, update post status. |
| 164 | - if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) { |
|
| 164 | + if ($existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status) { |
|
| 165 | 165 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 166 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
| 166 | + $post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) |
|
| 167 | 167 | ? $post->post_status : 'draft'; |
| 168 | - wl_update_post_status( $existing_entity->ID, $post_status ); |
|
| 168 | + wl_update_post_status($existing_entity->ID, $post_status); |
|
| 169 | 169 | } |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) ); |
|
| 172 | + $uri = $content_service->get_entity_id(Wordpress_Content_Id::create_post($existing_entity->ID)); |
|
| 173 | 173 | |
| 174 | 174 | $internal_entity_uris[] = $uri; |
| 175 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 176 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 175 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 176 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 177 | 177 | |
| 178 | 178 | } |
| 179 | 179 | } |
@@ -182,14 +182,14 @@ discard block |
||
| 182 | 182 | $updated_post_content = $post->post_content; |
| 183 | 183 | |
| 184 | 184 | // Update the post content if we found mappings of new entities. |
| 185 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 185 | + if ( ! empty($entities_uri_mapping)) { |
|
| 186 | 186 | // Save each entity and store the post id. |
| 187 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 188 | - if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
| 187 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 188 | + if (1 !== preg_match('@^(https?://|local-entity-)@', $tmp_uri)) { |
|
| 189 | 189 | continue; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 192 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | // Update the post content. |
@@ -201,19 +201,19 @@ discard block |
||
| 201 | 201 | wp_update_post( |
| 202 | 202 | array( |
| 203 | 203 | 'ID' => $post->ID, |
| 204 | - 'post_content' => addslashes( $updated_post_content ), |
|
| 204 | + 'post_content' => addslashes($updated_post_content), |
|
| 205 | 205 | ) |
| 206 | 206 | ); |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | // Reset previously saved instances. |
| 210 | - wl_core_delete_relation_instances( $post_id ); |
|
| 210 | + wl_core_delete_relation_instances($post_id); |
|
| 211 | 211 | |
| 212 | - $content_id = Wordpress_Content_Id::create_post( $post->ID ); |
|
| 213 | - $relations = Relation_Service::get_instance()->get_relations( $content_id ); |
|
| 212 | + $content_id = Wordpress_Content_Id::create_post($post->ID); |
|
| 213 | + $relations = Relation_Service::get_instance()->get_relations($content_id); |
|
| 214 | 214 | |
| 215 | - if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) { |
|
| 216 | - $relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) ); |
|
| 215 | + if (No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id)) { |
|
| 216 | + $relations->add(...Relation_Service::get_relations_from_uris($content_id, $internal_entity_uris)); |
|
| 217 | 217 | } |
| 218 | 218 | /** |
| 219 | 219 | * Filter the relations, we dont want to create a relation |
@@ -223,20 +223,20 @@ discard block |
||
| 223 | 223 | */ |
| 224 | 224 | $filtered_relations = array_filter( |
| 225 | 225 | $relations->toArray(), |
| 226 | - function ( $item ) { |
|
| 226 | + function($item) { |
|
| 227 | 227 | /** |
| 228 | 228 | * @var $item Relation |
| 229 | 229 | */ |
| 230 | 230 | $object = $item->get_object(); |
| 231 | 231 | |
| 232 | - return ! ( $object->get_type() === Object_Type_Enum::TERM |
|
| 233 | - && $object->get_id() === 1 ); |
|
| 232 | + return ! ($object->get_type() === Object_Type_Enum::TERM |
|
| 233 | + && $object->get_id() === 1); |
|
| 234 | 234 | } |
| 235 | 235 | ); |
| 236 | 236 | |
| 237 | 237 | // Save relation instances |
| 238 | 238 | /** @var Relation $relation */ |
| 239 | - foreach ( $filtered_relations as $relation ) { |
|
| 239 | + foreach ($filtered_relations as $relation) { |
|
| 240 | 240 | $subject = $relation->get_subject(); |
| 241 | 241 | $object = $relation->get_object(); |
| 242 | 242 | |
@@ -255,9 +255,9 @@ discard block |
||
| 255 | 255 | |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 258 | + if (isset($_POST['wl_entities'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 259 | 259 | // Save post metadata if available |
| 260 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 260 | + $metadata_via_post = (isset($_POST['wl_metadata'])) ? filter_input_array(INPUT_POST, array('wl_metadata' => FILTER_DEFAULT)) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 261 | 261 | |
| 262 | 262 | $fields = array( |
| 263 | 263 | Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
@@ -265,35 +265,35 @@ discard block |
||
| 265 | 265 | ); |
| 266 | 266 | |
| 267 | 267 | // Unlink topic taxonomy terms |
| 268 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 268 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID); |
|
| 269 | 269 | |
| 270 | - foreach ( $fields as $field ) { |
|
| 270 | + foreach ($fields as $field) { |
|
| 271 | 271 | |
| 272 | 272 | // Delete current values |
| 273 | - delete_post_meta( $post->ID, $field ); |
|
| 273 | + delete_post_meta($post->ID, $field); |
|
| 274 | 274 | // Retrieve the entity uri |
| 275 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 276 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 275 | + $uri = (isset($metadata_via_post[$field])) ? |
|
| 276 | + stripslashes($metadata_via_post[$field]) : ''; |
|
| 277 | 277 | |
| 278 | - if ( empty( $uri ) ) { |
|
| 278 | + if (empty($uri)) { |
|
| 279 | 279 | continue; |
| 280 | 280 | } |
| 281 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 281 | + $entity = $entity_service->get_entity_post_by_uri($uri); |
|
| 282 | 282 | |
| 283 | - if ( $entity ) { |
|
| 284 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 283 | + if ($entity) { |
|
| 284 | + add_post_meta($post->ID, $field, $entity->ID, true); |
|
| 285 | 285 | // Set also the topic taxonomy |
| 286 | - if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) { |
|
| 287 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 286 | + if (Wordlift_Schema_Service::FIELD_TOPIC === $field) { |
|
| 287 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity); |
|
| 288 | 288 | } |
| 289 | 289 | } |
| 290 | 290 | } |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 293 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 296 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 297 | 297 | |
| 298 | 298 | /** |
| 299 | 299 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -311,14 +311,14 @@ discard block |
||
| 311 | 311 | * |
| 312 | 312 | * @return null|WP_Post A post instance or null in case of failure. |
| 313 | 313 | */ |
| 314 | -function wl_save_entity( $entity_data ) { |
|
| 314 | +function wl_save_entity($entity_data) { |
|
| 315 | 315 | |
| 316 | 316 | // Required for REST API calls |
| 317 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 318 | - require_once ABSPATH . 'wp-admin/includes/image.php'; |
|
| 317 | + if ( ! function_exists('wp_crop_image')) { |
|
| 318 | + require_once ABSPATH.'wp-admin/includes/image.php'; |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 321 | + $log = Wordlift_Log_Service::get_logger('wl_save_entity'); |
|
| 322 | 322 | |
| 323 | 323 | /* |
| 324 | 324 | * Data is coming from a $_POST, sanitize it. |
@@ -327,23 +327,23 @@ discard block |
||
| 327 | 327 | * |
| 328 | 328 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 329 | 329 | */ |
| 330 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 330 | + $label = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label'])); |
|
| 331 | 331 | $type_uri = $entity_data['main_type']; |
| 332 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 332 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 333 | 333 | $description = $entity_data['description']; |
| 334 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 335 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 336 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 337 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 334 | + $images = isset($entity_data['image']) ? (array) $entity_data['image'] : array(); |
|
| 335 | + $same_as = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array(); |
|
| 336 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 337 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 338 | 338 | // Get the synonyms. |
| 339 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 339 | + $synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array(); |
|
| 340 | 340 | |
| 341 | 341 | // Check whether an entity already exists with the provided URI. |
| 342 | 342 | $uri = $entity_data['uri']; |
| 343 | - if ( isset( $uri ) ) { |
|
| 344 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 345 | - if ( isset( $post ) ) { |
|
| 346 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 343 | + if (isset($uri)) { |
|
| 344 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri); |
|
| 345 | + if (isset($post)) { |
|
| 346 | + $log->debug("Post already exists for URI $uri."); |
|
| 347 | 347 | |
| 348 | 348 | return $post; |
| 349 | 349 | } |
@@ -351,8 +351,8 @@ discard block |
||
| 351 | 351 | |
| 352 | 352 | // Prepare properties of the new entity. |
| 353 | 353 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 354 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id ) |
|
| 355 | - ? get_post_status( $related_post_id ) : 'draft'; |
|
| 354 | + $post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) && is_numeric($related_post_id) |
|
| 355 | + ? get_post_status($related_post_id) : 'draft'; |
|
| 356 | 356 | |
| 357 | 357 | $params = array( |
| 358 | 358 | // @@todo: we don't want an entity to be automatically published. |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | // new entities. |
| 367 | 367 | // |
| 368 | 368 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
| 369 | - 'post_name' => sanitize_title( $label ), |
|
| 369 | + 'post_name' => sanitize_title($label), |
|
| 370 | 370 | ); |
| 371 | 371 | |
| 372 | 372 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 376 | 376 | // is created when saving a post. |
| 377 | 377 | global $wpseo_metabox, $seo_ultimate; |
| 378 | - if ( isset( $wpseo_metabox ) ) { |
|
| 378 | + if (isset($wpseo_metabox)) { |
|
| 379 | 379 | remove_action( |
| 380 | 380 | 'wp_insert_post', |
| 381 | 381 | array( |
@@ -385,7 +385,7 @@ discard block |
||
| 385 | 385 | ); |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | - if ( isset( $seo_ultimate ) ) { |
|
| 388 | + if (isset($seo_ultimate)) { |
|
| 389 | 389 | remove_action( |
| 390 | 390 | 'save_post', |
| 391 | 391 | array( |
@@ -402,23 +402,23 @@ discard block |
||
| 402 | 402 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
| 403 | 403 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
| 404 | 404 | global $wp_filter; |
| 405 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 406 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 405 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 406 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 407 | 407 | |
| 408 | - $log->trace( 'Going to insert new post...' ); |
|
| 408 | + $log->trace('Going to insert new post...'); |
|
| 409 | 409 | |
| 410 | 410 | // create or update the post. |
| 411 | - $post_id = wp_insert_post( $params, true ); |
|
| 411 | + $post_id = wp_insert_post($params, true); |
|
| 412 | 412 | |
| 413 | 413 | // Setting the alternative labels for this entity. |
| 414 | 414 | Wordlift_Entity_Service::get_instance() |
| 415 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 415 | + ->set_alternative_labels($post_id, $synonyms); |
|
| 416 | 416 | |
| 417 | 417 | // Restore all the existing filters. |
| 418 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 418 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
| 419 | 419 | |
| 420 | 420 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 421 | - if ( isset( $wpseo_metabox ) ) { |
|
| 421 | + if (isset($wpseo_metabox)) { |
|
| 422 | 422 | add_action( |
| 423 | 423 | 'wp_insert_post', |
| 424 | 424 | array( |
@@ -429,7 +429,7 @@ discard block |
||
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 432 | - if ( isset( $seo_ultimate ) ) { |
|
| 432 | + if (isset($seo_ultimate)) { |
|
| 433 | 433 | add_action( |
| 434 | 434 | 'save_post', |
| 435 | 435 | array( |
@@ -442,61 +442,61 @@ discard block |
||
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | // TODO: handle errors. |
| 445 | - if ( is_wp_error( $post_id ) ) { |
|
| 446 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 445 | + if (is_wp_error($post_id)) { |
|
| 446 | + $log->error('An error occurred: '.$post_id->get_error_message()); |
|
| 447 | 447 | |
| 448 | 448 | // inform an error occurred. |
| 449 | 449 | return null; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 452 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 453 | 453 | |
| 454 | 454 | // Save the entity types. |
| 455 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 455 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 456 | 456 | |
| 457 | 457 | // Get a dataset URI for the entity. |
| 458 | - $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
| 458 | + $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri($post_id); |
|
| 459 | 459 | |
| 460 | 460 | // Add the uri to the sameAs data if it's not a local URI. |
| 461 | - if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) && |
|
| 461 | + if (isset($uri) && preg_match('@https?://.*@', $uri) && |
|
| 462 | 462 | $wl_uri !== $uri && |
| 463 | 463 | // Only remote entities |
| 464 | - 0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) |
|
| 464 | + 0 !== strpos($uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri()) |
|
| 465 | 465 | ) { |
| 466 | - array_push( $same_as, $uri ); |
|
| 466 | + array_push($same_as, $uri); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | // Save the sameAs data for the entity. |
| 470 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 470 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 471 | 471 | |
| 472 | 472 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 473 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 474 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 473 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 474 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | // Call hooks. |
| 478 | - do_action( 'wl_save_entity', $post_id ); |
|
| 478 | + do_action('wl_save_entity', $post_id); |
|
| 479 | 479 | |
| 480 | - foreach ( $images as $image_remote_url ) { |
|
| 480 | + foreach ($images as $image_remote_url) { |
|
| 481 | 481 | |
| 482 | 482 | // Check if image is already present in local DB |
| 483 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 483 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 484 | 484 | // Do nothing. |
| 485 | 485 | continue; |
| 486 | 486 | } |
| 487 | 487 | |
| 488 | 488 | // Check if there is an existing attachment for this post ID and source URL. |
| 489 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 489 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 490 | 490 | |
| 491 | 491 | // Skip if an existing image is found. |
| 492 | - if ( null !== $existing_image ) { |
|
| 492 | + if (null !== $existing_image) { |
|
| 493 | 493 | continue; |
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | // Save the image and get the local path. |
| 497 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 497 | + $image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url); |
|
| 498 | 498 | |
| 499 | - if ( false === $image || is_wp_error( $image ) ) { |
|
| 499 | + if (false === $image || is_wp_error($image)) { |
|
| 500 | 500 | continue; |
| 501 | 501 | } |
| 502 | 502 | |
@@ -516,18 +516,18 @@ discard block |
||
| 516 | 516 | ); |
| 517 | 517 | |
| 518 | 518 | // Create the attachment in WordPress and generate the related metadata. |
| 519 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 519 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 520 | 520 | |
| 521 | 521 | // Set the source URL for the image. |
| 522 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 522 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 523 | 523 | |
| 524 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 525 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 524 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 525 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 526 | 526 | |
| 527 | 527 | // Set it as the featured image. |
| 528 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 528 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 529 | 529 | } |
| 530 | 530 | |
| 531 | 531 | // finally return the entity post. |
| 532 | - return get_post( $post_id ); |
|
| 532 | + return get_post($post_id); |
|
| 533 | 533 | } |
@@ -7,74 +7,74 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Jsonld { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var Content_Service $content_service |
|
| 12 | - */ |
|
| 13 | - private $content_service; |
|
| 10 | + /** |
|
| 11 | + * @var Content_Service $content_service |
|
| 12 | + */ |
|
| 13 | + private $content_service; |
|
| 14 | 14 | |
| 15 | - public function __construct( Content_Service $content_service ) { |
|
| 16 | - $this->content_service = $content_service; |
|
| 17 | - } |
|
| 15 | + public function __construct( Content_Service $content_service ) { |
|
| 16 | + $this->content_service = $content_service; |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - public function register_hooks() { |
|
| 20 | - add_action( 'wl_post_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
| 21 | - add_action( 'wl_entity_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
| 19 | + public function register_hooks() { |
|
| 20 | + add_action( 'wl_post_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
| 21 | + add_action( 'wl_entity_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
| 22 | 22 | |
| 23 | - } |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - public function post_jsonld( $jsonld, $post_id ) { |
|
| 25 | + public function post_jsonld( $jsonld, $post_id ) { |
|
| 26 | 26 | |
| 27 | - $mentions = array(); |
|
| 28 | - $term_ids = $this->get_term_ids( $post_id ); |
|
| 27 | + $mentions = array(); |
|
| 28 | + $term_ids = $this->get_term_ids( $post_id ); |
|
| 29 | 29 | |
| 30 | - foreach ( $term_ids as $term_id ) { |
|
| 31 | - $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_term( $term_id ) ); |
|
| 32 | - } |
|
| 30 | + foreach ( $term_ids as $term_id ) { |
|
| 31 | + $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_term( $term_id ) ); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - // Add also the post jsonld. |
|
| 35 | - $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_post( $post_id ) ); |
|
| 34 | + // Add also the post jsonld. |
|
| 35 | + $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_post( $post_id ) ); |
|
| 36 | 36 | |
| 37 | - $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
| 37 | + $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
| 38 | 38 | |
| 39 | - if ( count( $mentions ) > 0 ) { |
|
| 40 | - $jsonld['mentions'] = array_merge( $existing_mentions, $mentions ); |
|
| 41 | - } |
|
| 42 | - return $jsonld; |
|
| 43 | - } |
|
| 39 | + if ( count( $mentions ) > 0 ) { |
|
| 40 | + $jsonld['mentions'] = array_merge( $existing_mentions, $mentions ); |
|
| 41 | + } |
|
| 42 | + return $jsonld; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - private function get_term_ids( $post_id ) { |
|
| 46 | - global $wpdb; |
|
| 47 | - $result = $wpdb->get_col( |
|
| 48 | - $wpdb->prepare( |
|
| 49 | - "SELECT r.term_id from {$wpdb->prefix}term_relationships r |
|
| 45 | + private function get_term_ids( $post_id ) { |
|
| 46 | + global $wpdb; |
|
| 47 | + $result = $wpdb->get_col( |
|
| 48 | + $wpdb->prepare( |
|
| 49 | + "SELECT r.term_id from {$wpdb->prefix}term_relationships r |
|
| 50 | 50 | INNER JOIN {$wpdb->prefix}term_taxonomy tt ON r.term_taxonomy_id=tt.term_taxonomy_id WHERE r.object_id= %d", |
| 51 | - $post_id |
|
| 52 | - ) |
|
| 53 | - ); |
|
| 54 | - |
|
| 55 | - return is_array( $result ) ? $result : array(); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param $list array Adds the jsonld to the list if valid. |
|
| 60 | - * @param $content_id \Wordlift\Content\Wordpress\Wordpress_Content_Id |
|
| 61 | - * |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - private function add_about_jsonld( &$list, $content_id ) { |
|
| 65 | - $about_jsonld = $this->content_service->get_about_jsonld( |
|
| 66 | - $content_id |
|
| 67 | - ); |
|
| 68 | - $decoded_jsonld = json_decode( $about_jsonld, true ); |
|
| 69 | - |
|
| 70 | - if ( null === $decoded_jsonld ) { |
|
| 71 | - return; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $value = wp_is_numeric_array( $decoded_jsonld ) ? $decoded_jsonld : array( $decoded_jsonld ); |
|
| 75 | - |
|
| 76 | - $list = array_merge( $list, $value ); |
|
| 77 | - |
|
| 78 | - } |
|
| 51 | + $post_id |
|
| 52 | + ) |
|
| 53 | + ); |
|
| 54 | + |
|
| 55 | + return is_array( $result ) ? $result : array(); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param $list array Adds the jsonld to the list if valid. |
|
| 60 | + * @param $content_id \Wordlift\Content\Wordpress\Wordpress_Content_Id |
|
| 61 | + * |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + private function add_about_jsonld( &$list, $content_id ) { |
|
| 65 | + $about_jsonld = $this->content_service->get_about_jsonld( |
|
| 66 | + $content_id |
|
| 67 | + ); |
|
| 68 | + $decoded_jsonld = json_decode( $about_jsonld, true ); |
|
| 69 | + |
|
| 70 | + if ( null === $decoded_jsonld ) { |
|
| 71 | + return; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $value = wp_is_numeric_array( $decoded_jsonld ) ? $decoded_jsonld : array( $decoded_jsonld ); |
|
| 75 | + |
|
| 76 | + $list = array_merge( $list, $value ); |
|
| 77 | + |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | 80 | } |
@@ -12,37 +12,37 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | private $content_service; |
| 14 | 14 | |
| 15 | - public function __construct( Content_Service $content_service ) { |
|
| 15 | + public function __construct(Content_Service $content_service) { |
|
| 16 | 16 | $this->content_service = $content_service; |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public function register_hooks() { |
| 20 | - add_action( 'wl_post_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
| 21 | - add_action( 'wl_entity_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
| 20 | + add_action('wl_post_jsonld', array($this, 'post_jsonld'), 10, 2); |
|
| 21 | + add_action('wl_entity_jsonld', array($this, 'post_jsonld'), 10, 2); |
|
| 22 | 22 | |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - public function post_jsonld( $jsonld, $post_id ) { |
|
| 25 | + public function post_jsonld($jsonld, $post_id) { |
|
| 26 | 26 | |
| 27 | 27 | $mentions = array(); |
| 28 | - $term_ids = $this->get_term_ids( $post_id ); |
|
| 28 | + $term_ids = $this->get_term_ids($post_id); |
|
| 29 | 29 | |
| 30 | - foreach ( $term_ids as $term_id ) { |
|
| 31 | - $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_term( $term_id ) ); |
|
| 30 | + foreach ($term_ids as $term_id) { |
|
| 31 | + $this->add_about_jsonld($mentions, Wordpress_Content_Id::create_term($term_id)); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | // Add also the post jsonld. |
| 35 | - $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_post( $post_id ) ); |
|
| 35 | + $this->add_about_jsonld($mentions, Wordpress_Content_Id::create_post($post_id)); |
|
| 36 | 36 | |
| 37 | - $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
| 37 | + $existing_mentions = array_key_exists('mentions', $jsonld) ? $jsonld['mentions'] : array(); |
|
| 38 | 38 | |
| 39 | - if ( count( $mentions ) > 0 ) { |
|
| 40 | - $jsonld['mentions'] = array_merge( $existing_mentions, $mentions ); |
|
| 39 | + if (count($mentions) > 0) { |
|
| 40 | + $jsonld['mentions'] = array_merge($existing_mentions, $mentions); |
|
| 41 | 41 | } |
| 42 | 42 | return $jsonld; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - private function get_term_ids( $post_id ) { |
|
| 45 | + private function get_term_ids($post_id) { |
|
| 46 | 46 | global $wpdb; |
| 47 | 47 | $result = $wpdb->get_col( |
| 48 | 48 | $wpdb->prepare( |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | ) |
| 53 | 53 | ); |
| 54 | 54 | |
| 55 | - return is_array( $result ) ? $result : array(); |
|
| 55 | + return is_array($result) ? $result : array(); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
@@ -61,19 +61,19 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @return void |
| 63 | 63 | */ |
| 64 | - private function add_about_jsonld( &$list, $content_id ) { |
|
| 64 | + private function add_about_jsonld(&$list, $content_id) { |
|
| 65 | 65 | $about_jsonld = $this->content_service->get_about_jsonld( |
| 66 | 66 | $content_id |
| 67 | 67 | ); |
| 68 | - $decoded_jsonld = json_decode( $about_jsonld, true ); |
|
| 68 | + $decoded_jsonld = json_decode($about_jsonld, true); |
|
| 69 | 69 | |
| 70 | - if ( null === $decoded_jsonld ) { |
|
| 70 | + if (null === $decoded_jsonld) { |
|
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - $value = wp_is_numeric_array( $decoded_jsonld ) ? $decoded_jsonld : array( $decoded_jsonld ); |
|
| 74 | + $value = wp_is_numeric_array($decoded_jsonld) ? $decoded_jsonld : array($decoded_jsonld); |
|
| 75 | 75 | |
| 76 | - $list = array_merge( $list, $value ); |
|
| 76 | + $list = array_merge($list, $value); |
|
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | |
@@ -18,42 +18,42 @@ |
||
| 18 | 18 | |
| 19 | 19 | class No_Annotation_Strategy extends Singleton implements Occurrences { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var Relation_Service_Interface |
|
| 23 | - */ |
|
| 24 | - private $relation_service; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var Content_Service |
|
| 28 | - */ |
|
| 29 | - private $content_service; |
|
| 30 | - |
|
| 31 | - protected function __construct() { |
|
| 32 | - parent::__construct(); |
|
| 33 | - $this->relation_service = Relation_Service::get_instance(); |
|
| 34 | - $this->content_service = Wordpress_Content_Service::get_instance(); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
| 38 | - |
|
| 39 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 40 | - $relation_service = Relation_Service::get_instance(); |
|
| 41 | - $relations = $relation_service->get_relations( $content_id ); |
|
| 42 | - |
|
| 43 | - /** @var Relation $relation */ |
|
| 44 | - foreach ( $relations->toArray() as $relation ) { |
|
| 45 | - $object = $relation->get_object(); |
|
| 46 | - // @@todo is this working okey with term id? |
|
| 47 | - $entity_uri = $this->content_service->get_entity_id( $relation->get_object() ); |
|
| 48 | - if ( ! $entity_uri ) { |
|
| 49 | - continue; |
|
| 50 | - } |
|
| 51 | - $entity_data = wl_serialize_entity( $object->get_id() ); |
|
| 52 | - $entity_data['occurrences'] = array( 'placeholder-occurrence' ); |
|
| 53 | - $json->entities->{$entity_uri} = $entity_data; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - return $json; |
|
| 57 | - } |
|
| 21 | + /** |
|
| 22 | + * @var Relation_Service_Interface |
|
| 23 | + */ |
|
| 24 | + private $relation_service; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var Content_Service |
|
| 28 | + */ |
|
| 29 | + private $content_service; |
|
| 30 | + |
|
| 31 | + protected function __construct() { |
|
| 32 | + parent::__construct(); |
|
| 33 | + $this->relation_service = Relation_Service::get_instance(); |
|
| 34 | + $this->content_service = Wordpress_Content_Service::get_instance(); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
| 38 | + |
|
| 39 | + $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 40 | + $relation_service = Relation_Service::get_instance(); |
|
| 41 | + $relations = $relation_service->get_relations( $content_id ); |
|
| 42 | + |
|
| 43 | + /** @var Relation $relation */ |
|
| 44 | + foreach ( $relations->toArray() as $relation ) { |
|
| 45 | + $object = $relation->get_object(); |
|
| 46 | + // @@todo is this working okey with term id? |
|
| 47 | + $entity_uri = $this->content_service->get_entity_id( $relation->get_object() ); |
|
| 48 | + if ( ! $entity_uri ) { |
|
| 49 | + continue; |
|
| 50 | + } |
|
| 51 | + $entity_data = wl_serialize_entity( $object->get_id() ); |
|
| 52 | + $entity_data['occurrences'] = array( 'placeholder-occurrence' ); |
|
| 53 | + $json->entities->{$entity_uri} = $entity_data; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + return $json; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | 59 | } |
@@ -34,22 +34,22 @@ |
||
| 34 | 34 | $this->content_service = Wordpress_Content_Service::get_instance(); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
| 37 | + public function add_occurrences_to_entities($occurrences, $json, $post_id) { |
|
| 38 | 38 | |
| 39 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 39 | + $content_id = Wordpress_Content_Id::create_post($post_id); |
|
| 40 | 40 | $relation_service = Relation_Service::get_instance(); |
| 41 | - $relations = $relation_service->get_relations( $content_id ); |
|
| 41 | + $relations = $relation_service->get_relations($content_id); |
|
| 42 | 42 | |
| 43 | 43 | /** @var Relation $relation */ |
| 44 | - foreach ( $relations->toArray() as $relation ) { |
|
| 44 | + foreach ($relations->toArray() as $relation) { |
|
| 45 | 45 | $object = $relation->get_object(); |
| 46 | 46 | // @@todo is this working okey with term id? |
| 47 | - $entity_uri = $this->content_service->get_entity_id( $relation->get_object() ); |
|
| 48 | - if ( ! $entity_uri ) { |
|
| 47 | + $entity_uri = $this->content_service->get_entity_id($relation->get_object()); |
|
| 48 | + if ( ! $entity_uri) { |
|
| 49 | 49 | continue; |
| 50 | 50 | } |
| 51 | - $entity_data = wl_serialize_entity( $object->get_id() ); |
|
| 52 | - $entity_data['occurrences'] = array( 'placeholder-occurrence' ); |
|
| 51 | + $entity_data = wl_serialize_entity($object->get_id()); |
|
| 52 | + $entity_data['occurrences'] = array('placeholder-occurrence'); |
|
| 53 | 53 | $json->entities->{$entity_uri} = $entity_data; |
| 54 | 54 | } |
| 55 | 55 | |
@@ -20,273 +20,273 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Term_JsonLd_Adapter { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.20.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $entity_uri_service; |
|
| 31 | - |
|
| 32 | - private static $instance; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var Wordlift_Post_Converter |
|
| 36 | - */ |
|
| 37 | - private $post_id_to_jsonld_converter; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 41 | - * |
|
| 42 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 43 | - * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.20.0 |
|
| 46 | - */ |
|
| 47 | - public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 48 | - |
|
| 49 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 50 | - |
|
| 51 | - $this->entity_uri_service = $entity_uri_service; |
|
| 52 | - $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
|
| 53 | - |
|
| 54 | - self::$instance = $this; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public static function get_instance() { |
|
| 58 | - |
|
| 59 | - return self::$instance; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Adds carousel json ld data to term page if the conditions match |
|
| 64 | - * |
|
| 65 | - * @return array|boolean |
|
| 66 | - */ |
|
| 67 | - public function get_carousel_jsonld( $id = null ) { |
|
| 68 | - $posts = $this->get_posts( $id ); |
|
| 69 | - $post_jsonld = array(); |
|
| 70 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 71 | - // Bail out if no posts are present. |
|
| 72 | - return false; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - if ( $id !== null ) { |
|
| 76 | - $term = get_term( $id ); |
|
| 77 | - $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 78 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 79 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 80 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // More than 2 items are present, so construct the post_jsonld data |
|
| 85 | - $post_jsonld['@context'] = 'https://schema.org'; |
|
| 86 | - $post_jsonld['@type'] = 'ItemList'; |
|
| 87 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 88 | - $post_jsonld['itemListElement'] = array(); |
|
| 89 | - $position = 1; |
|
| 90 | - |
|
| 91 | - foreach ( $posts as $post_id ) { |
|
| 92 | - $result = array( |
|
| 93 | - '@type' => 'ListItem', |
|
| 94 | - 'position' => $position, |
|
| 95 | - /** |
|
| 96 | - * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 97 | - * |
|
| 98 | - * See https://developers.google.com/search/docs/data-types/carousel |
|
| 99 | - */ |
|
| 100 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 101 | - ); |
|
| 102 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 103 | - ++ $position; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return $post_jsonld; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - private function get_posts( $id ) { |
|
| 110 | - global $wp_query; |
|
| 111 | - |
|
| 112 | - if ( $wp_query->posts !== null ) { |
|
| 113 | - return array_map( |
|
| 114 | - function ( $post ) { |
|
| 115 | - return $post->ID; |
|
| 116 | - }, |
|
| 117 | - $wp_query->posts |
|
| 118 | - ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if ( $id === null ) { |
|
| 122 | - return null; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $term = get_term( $id ); |
|
| 126 | - |
|
| 127 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Hook to `wp_head` to print the JSON-LD. |
|
| 132 | - * |
|
| 133 | - * @since 3.20.0 |
|
| 134 | - */ |
|
| 135 | - public function wp_head() { |
|
| 136 | - $query_object = get_queried_object(); |
|
| 137 | - |
|
| 138 | - // Check if it is a term page. |
|
| 139 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 144 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 145 | - return; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $term_id = $query_object->term_id; |
|
| 149 | - |
|
| 150 | - $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 151 | - |
|
| 152 | - // Bail out if the JSON-LD is empty. |
|
| 153 | - if ( empty( $jsonld ) ) { |
|
| 154 | - return; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 158 | - |
|
| 159 | - $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 160 | - $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 161 | - |
|
| 162 | - echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
|
| 163 | - |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - public function get( $id, $context, $is_recursive_call = false ) { |
|
| 167 | - /** |
|
| 168 | - * Support for carousel rich snippet, get jsonld data present |
|
| 169 | - * for all the posts shown in the term page, and add the jsonld data |
|
| 170 | - * to list |
|
| 171 | - * |
|
| 172 | - * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 173 | - * |
|
| 174 | - * @since 3.26.0 |
|
| 175 | - */ |
|
| 176 | - $jsonld_array = array(); |
|
| 177 | - |
|
| 178 | - if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 179 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 180 | - if ( $carousel_data ) { |
|
| 181 | - $jsonld_array[] = $carousel_data; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 186 | - |
|
| 187 | - $result = array( |
|
| 188 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 189 | - 'references' => array(), |
|
| 190 | - ); |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @since 3.26.3 |
|
| 194 | - * Filter: wl_term_jsonld_array |
|
| 195 | - * @var $id int Term id |
|
| 196 | - * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 197 | - */ |
|
| 198 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 199 | - |
|
| 200 | - $references = array(); |
|
| 201 | - |
|
| 202 | - // Don't expand nested references, it will lead to an infinite loop. |
|
| 203 | - if ( ! $is_recursive_call ) { |
|
| 204 | - /** |
|
| 205 | - * @since 3.32.0 |
|
| 206 | - * Expand the references returned by this filter. |
|
| 207 | - */ |
|
| 208 | - $references = $this->expand_references( $arr['references'] ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 212 | - |
|
| 213 | - return $jsonld_array; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - private function get_term_url( $id ) { |
|
| 217 | - if ( null === $id ) { |
|
| 218 | - return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 222 | - if ( ! empty( $maybe_url ) ) { |
|
| 223 | - return $maybe_url; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - return get_term_link( $id ); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Return jsonld for entities bound to terms. |
|
| 231 | - * |
|
| 232 | - * @param int $term_id Term ID. |
|
| 233 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 234 | - * |
|
| 235 | - * @return array |
|
| 236 | - */ |
|
| 237 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 238 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 239 | - |
|
| 240 | - // The `_wl_entity_id` are URIs. |
|
| 241 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 242 | - $entity_uri_service = $this->entity_uri_service; |
|
| 243 | - |
|
| 244 | - $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
|
| 245 | - |
|
| 246 | - $local_entity_ids = array_filter( |
|
| 247 | - $entity_ids, |
|
| 248 | - function ( $uri ) use ( $entity_uri_service ) { |
|
| 249 | - return $entity_uri_service->is_internal( $uri ); |
|
| 250 | - } |
|
| 251 | - ); |
|
| 252 | - |
|
| 253 | - // Bail out if there are no entities. |
|
| 254 | - if ( empty( $local_entity_ids ) ) { |
|
| 255 | - return array(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 259 | - $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 260 | - // Reset the `url` to the term page. |
|
| 261 | - $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 262 | - |
|
| 263 | - return $entities_jsonld; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @param $references |
|
| 268 | - * |
|
| 269 | - * @return array |
|
| 270 | - */ |
|
| 271 | - private function expand_references( $references ) { |
|
| 272 | - if ( ! is_array( $references ) ) { |
|
| 273 | - return array(); |
|
| 274 | - } |
|
| 275 | - $references_jsonld = array(); |
|
| 276 | - // Expand the references. |
|
| 277 | - foreach ( $references as $reference ) { |
|
| 278 | - if ( $reference instanceof Term_Reference ) { |
|
| 279 | - // Second level references won't be expanded. |
|
| 280 | - $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 281 | - } elseif ( is_numeric( $reference ) ) { |
|
| 282 | - $ref_2 = array(); |
|
| 283 | - $ref_info_2 = array(); |
|
| 284 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - return $references_jsonld; |
|
| 289 | - |
|
| 290 | - } |
|
| 23 | + /** |
|
| 24 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.20.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $entity_uri_service; |
|
| 31 | + |
|
| 32 | + private static $instance; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var Wordlift_Post_Converter |
|
| 36 | + */ |
|
| 37 | + private $post_id_to_jsonld_converter; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 41 | + * |
|
| 42 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 43 | + * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.20.0 |
|
| 46 | + */ |
|
| 47 | + public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 48 | + |
|
| 49 | + add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 50 | + |
|
| 51 | + $this->entity_uri_service = $entity_uri_service; |
|
| 52 | + $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
|
| 53 | + |
|
| 54 | + self::$instance = $this; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public static function get_instance() { |
|
| 58 | + |
|
| 59 | + return self::$instance; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Adds carousel json ld data to term page if the conditions match |
|
| 64 | + * |
|
| 65 | + * @return array|boolean |
|
| 66 | + */ |
|
| 67 | + public function get_carousel_jsonld( $id = null ) { |
|
| 68 | + $posts = $this->get_posts( $id ); |
|
| 69 | + $post_jsonld = array(); |
|
| 70 | + if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 71 | + // Bail out if no posts are present. |
|
| 72 | + return false; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + if ( $id !== null ) { |
|
| 76 | + $term = get_term( $id ); |
|
| 77 | + $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 78 | + $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 79 | + if ( ! empty( $thumbnail_id ) ) { |
|
| 80 | + $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // More than 2 items are present, so construct the post_jsonld data |
|
| 85 | + $post_jsonld['@context'] = 'https://schema.org'; |
|
| 86 | + $post_jsonld['@type'] = 'ItemList'; |
|
| 87 | + $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 88 | + $post_jsonld['itemListElement'] = array(); |
|
| 89 | + $position = 1; |
|
| 90 | + |
|
| 91 | + foreach ( $posts as $post_id ) { |
|
| 92 | + $result = array( |
|
| 93 | + '@type' => 'ListItem', |
|
| 94 | + 'position' => $position, |
|
| 95 | + /** |
|
| 96 | + * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 97 | + * |
|
| 98 | + * See https://developers.google.com/search/docs/data-types/carousel |
|
| 99 | + */ |
|
| 100 | + 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 101 | + ); |
|
| 102 | + array_push( $post_jsonld['itemListElement'], $result ); |
|
| 103 | + ++ $position; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return $post_jsonld; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + private function get_posts( $id ) { |
|
| 110 | + global $wp_query; |
|
| 111 | + |
|
| 112 | + if ( $wp_query->posts !== null ) { |
|
| 113 | + return array_map( |
|
| 114 | + function ( $post ) { |
|
| 115 | + return $post->ID; |
|
| 116 | + }, |
|
| 117 | + $wp_query->posts |
|
| 118 | + ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if ( $id === null ) { |
|
| 122 | + return null; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $term = get_term( $id ); |
|
| 126 | + |
|
| 127 | + return get_objects_in_term( $id, $term->taxonomy ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Hook to `wp_head` to print the JSON-LD. |
|
| 132 | + * |
|
| 133 | + * @since 3.20.0 |
|
| 134 | + */ |
|
| 135 | + public function wp_head() { |
|
| 136 | + $query_object = get_queried_object(); |
|
| 137 | + |
|
| 138 | + // Check if it is a term page. |
|
| 139 | + if ( ! $query_object instanceof WP_Term ) { |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 144 | + if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 145 | + return; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $term_id = $query_object->term_id; |
|
| 149 | + |
|
| 150 | + $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 151 | + |
|
| 152 | + // Bail out if the JSON-LD is empty. |
|
| 153 | + if ( empty( $jsonld ) ) { |
|
| 154 | + return; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $jsonld_string = wp_json_encode( $jsonld ); |
|
| 158 | + |
|
| 159 | + $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 160 | + $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 161 | + |
|
| 162 | + echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
|
| 163 | + |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + public function get( $id, $context, $is_recursive_call = false ) { |
|
| 167 | + /** |
|
| 168 | + * Support for carousel rich snippet, get jsonld data present |
|
| 169 | + * for all the posts shown in the term page, and add the jsonld data |
|
| 170 | + * to list |
|
| 171 | + * |
|
| 172 | + * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 173 | + * |
|
| 174 | + * @since 3.26.0 |
|
| 175 | + */ |
|
| 176 | + $jsonld_array = array(); |
|
| 177 | + |
|
| 178 | + if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 179 | + $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 180 | + if ( $carousel_data ) { |
|
| 181 | + $jsonld_array[] = $carousel_data; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 186 | + |
|
| 187 | + $result = array( |
|
| 188 | + 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 189 | + 'references' => array(), |
|
| 190 | + ); |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @since 3.26.3 |
|
| 194 | + * Filter: wl_term_jsonld_array |
|
| 195 | + * @var $id int Term id |
|
| 196 | + * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 197 | + */ |
|
| 198 | + $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 199 | + |
|
| 200 | + $references = array(); |
|
| 201 | + |
|
| 202 | + // Don't expand nested references, it will lead to an infinite loop. |
|
| 203 | + if ( ! $is_recursive_call ) { |
|
| 204 | + /** |
|
| 205 | + * @since 3.32.0 |
|
| 206 | + * Expand the references returned by this filter. |
|
| 207 | + */ |
|
| 208 | + $references = $this->expand_references( $arr['references'] ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 212 | + |
|
| 213 | + return $jsonld_array; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + private function get_term_url( $id ) { |
|
| 217 | + if ( null === $id ) { |
|
| 218 | + return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 222 | + if ( ! empty( $maybe_url ) ) { |
|
| 223 | + return $maybe_url; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + return get_term_link( $id ); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Return jsonld for entities bound to terms. |
|
| 231 | + * |
|
| 232 | + * @param int $term_id Term ID. |
|
| 233 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 234 | + * |
|
| 235 | + * @return array |
|
| 236 | + */ |
|
| 237 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 238 | + private function get_entity_jsonld( $term_id, $context ) { |
|
| 239 | + |
|
| 240 | + // The `_wl_entity_id` are URIs. |
|
| 241 | + $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 242 | + $entity_uri_service = $this->entity_uri_service; |
|
| 243 | + |
|
| 244 | + $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
|
| 245 | + |
|
| 246 | + $local_entity_ids = array_filter( |
|
| 247 | + $entity_ids, |
|
| 248 | + function ( $uri ) use ( $entity_uri_service ) { |
|
| 249 | + return $entity_uri_service->is_internal( $uri ); |
|
| 250 | + } |
|
| 251 | + ); |
|
| 252 | + |
|
| 253 | + // Bail out if there are no entities. |
|
| 254 | + if ( empty( $local_entity_ids ) ) { |
|
| 255 | + return array(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 259 | + $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 260 | + // Reset the `url` to the term page. |
|
| 261 | + $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 262 | + |
|
| 263 | + return $entities_jsonld; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @param $references |
|
| 268 | + * |
|
| 269 | + * @return array |
|
| 270 | + */ |
|
| 271 | + private function expand_references( $references ) { |
|
| 272 | + if ( ! is_array( $references ) ) { |
|
| 273 | + return array(); |
|
| 274 | + } |
|
| 275 | + $references_jsonld = array(); |
|
| 276 | + // Expand the references. |
|
| 277 | + foreach ( $references as $reference ) { |
|
| 278 | + if ( $reference instanceof Term_Reference ) { |
|
| 279 | + // Second level references won't be expanded. |
|
| 280 | + $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 281 | + } elseif ( is_numeric( $reference ) ) { |
|
| 282 | + $ref_2 = array(); |
|
| 283 | + $ref_info_2 = array(); |
|
| 284 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + return $references_jsonld; |
|
| 289 | + |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | 292 | } |
@@ -44,9 +44,9 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @since 3.20.0 |
| 46 | 46 | */ |
| 47 | - public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 47 | + public function __construct($entity_uri_service, $post_id_to_jsonld_converter) { |
|
| 48 | 48 | |
| 49 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 49 | + add_action('wp_head', array($this, 'wp_head')); |
|
| 50 | 50 | |
| 51 | 51 | $this->entity_uri_service = $entity_uri_service; |
| 52 | 52 | $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
@@ -64,31 +64,31 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @return array|boolean |
| 66 | 66 | */ |
| 67 | - public function get_carousel_jsonld( $id = null ) { |
|
| 68 | - $posts = $this->get_posts( $id ); |
|
| 67 | + public function get_carousel_jsonld($id = null) { |
|
| 68 | + $posts = $this->get_posts($id); |
|
| 69 | 69 | $post_jsonld = array(); |
| 70 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 70 | + if ( ! is_array($posts) || count($posts) < 2) { |
|
| 71 | 71 | // Bail out if no posts are present. |
| 72 | 72 | return false; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - if ( $id !== null ) { |
|
| 76 | - $term = get_term( $id ); |
|
| 77 | - $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 78 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 79 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 80 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 75 | + if ($id !== null) { |
|
| 76 | + $term = get_term($id); |
|
| 77 | + $post_jsonld['description'] = wp_strip_all_tags(strip_shortcodes($term->description)); |
|
| 78 | + $thumbnail_id = get_term_meta($id, 'thumbnail_id', true); |
|
| 79 | + if ( ! empty($thumbnail_id)) { |
|
| 80 | + $post_jsonld['image'] = wp_get_attachment_url($thumbnail_id); |
|
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | // More than 2 items are present, so construct the post_jsonld data |
| 85 | 85 | $post_jsonld['@context'] = 'https://schema.org'; |
| 86 | 86 | $post_jsonld['@type'] = 'ItemList'; |
| 87 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 87 | + $post_jsonld['url'] = $this->get_term_url($id); |
|
| 88 | 88 | $post_jsonld['itemListElement'] = array(); |
| 89 | 89 | $position = 1; |
| 90 | 90 | |
| 91 | - foreach ( $posts as $post_id ) { |
|
| 91 | + foreach ($posts as $post_id) { |
|
| 92 | 92 | $result = array( |
| 93 | 93 | '@type' => 'ListItem', |
| 94 | 94 | 'position' => $position, |
@@ -97,34 +97,34 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * See https://developers.google.com/search/docs/data-types/carousel |
| 99 | 99 | */ |
| 100 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 100 | + 'url' => apply_filters('wl_carousel_post_list_item_url', get_permalink($post_id), $post_id), |
|
| 101 | 101 | ); |
| 102 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 103 | - ++ $position; |
|
| 102 | + array_push($post_jsonld['itemListElement'], $result); |
|
| 103 | + ++$position; |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | return $post_jsonld; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - private function get_posts( $id ) { |
|
| 109 | + private function get_posts($id) { |
|
| 110 | 110 | global $wp_query; |
| 111 | 111 | |
| 112 | - if ( $wp_query->posts !== null ) { |
|
| 112 | + if ($wp_query->posts !== null) { |
|
| 113 | 113 | return array_map( |
| 114 | - function ( $post ) { |
|
| 114 | + function($post) { |
|
| 115 | 115 | return $post->ID; |
| 116 | 116 | }, |
| 117 | 117 | $wp_query->posts |
| 118 | 118 | ); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - if ( $id === null ) { |
|
| 121 | + if ($id === null) { |
|
| 122 | 122 | return null; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - $term = get_term( $id ); |
|
| 125 | + $term = get_term($id); |
|
| 126 | 126 | |
| 127 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 127 | + return get_objects_in_term($id, $term->taxonomy); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -136,34 +136,34 @@ discard block |
||
| 136 | 136 | $query_object = get_queried_object(); |
| 137 | 137 | |
| 138 | 138 | // Check if it is a term page. |
| 139 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 139 | + if ( ! $query_object instanceof WP_Term) { |
|
| 140 | 140 | return; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | // Bail out if `wl_jsonld_enabled` isn't enabled. |
| 144 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 144 | + if ( ! apply_filters('wl_jsonld_enabled', true)) { |
|
| 145 | 145 | return; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | $term_id = $query_object->term_id; |
| 149 | 149 | |
| 150 | - $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 150 | + $jsonld = $this->get($term_id, Jsonld_Context_Enum::PAGE); |
|
| 151 | 151 | |
| 152 | 152 | // Bail out if the JSON-LD is empty. |
| 153 | - if ( empty( $jsonld ) ) { |
|
| 153 | + if (empty($jsonld)) { |
|
| 154 | 154 | return; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 157 | + $jsonld_string = wp_json_encode($jsonld); |
|
| 158 | 158 | |
| 159 | - $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 160 | - $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 159 | + $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">'.$jsonld_string.'</script>'; |
|
| 160 | + $jsonld_term_html_output = apply_filters('wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id); |
|
| 161 | 161 | |
| 162 | 162 | echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
| 163 | 163 | |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - public function get( $id, $context, $is_recursive_call = false ) { |
|
| 166 | + public function get($id, $context, $is_recursive_call = false) { |
|
| 167 | 167 | /** |
| 168 | 168 | * Support for carousel rich snippet, get jsonld data present |
| 169 | 169 | * for all the posts shown in the term page, and add the jsonld data |
@@ -175,17 +175,17 @@ discard block |
||
| 175 | 175 | */ |
| 176 | 176 | $jsonld_array = array(); |
| 177 | 177 | |
| 178 | - if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 179 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 180 | - if ( $carousel_data ) { |
|
| 178 | + if (Jsonld_Context_Enum::PAGE === $context) { |
|
| 179 | + $carousel_data = $this->get_carousel_jsonld($id); |
|
| 180 | + if ($carousel_data) { |
|
| 181 | 181 | $jsonld_array[] = $carousel_data; |
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 185 | + $entities_jsonld_array = $this->get_entity_jsonld($id, $context); |
|
| 186 | 186 | |
| 187 | 187 | $result = array( |
| 188 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 188 | + 'jsonld' => array_merge($jsonld_array, $entities_jsonld_array), |
|
| 189 | 189 | 'references' => array(), |
| 190 | 190 | ); |
| 191 | 191 | |
@@ -195,35 +195,35 @@ discard block |
||
| 195 | 195 | * @var $id int Term id |
| 196 | 196 | * @var $jsonld_array array An array containing jsonld for term and entities. |
| 197 | 197 | */ |
| 198 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 198 | + $arr = apply_filters('wl_term_jsonld_array', $result, $id); |
|
| 199 | 199 | |
| 200 | 200 | $references = array(); |
| 201 | 201 | |
| 202 | 202 | // Don't expand nested references, it will lead to an infinite loop. |
| 203 | - if ( ! $is_recursive_call ) { |
|
| 203 | + if ( ! $is_recursive_call) { |
|
| 204 | 204 | /** |
| 205 | 205 | * @since 3.32.0 |
| 206 | 206 | * Expand the references returned by this filter. |
| 207 | 207 | */ |
| 208 | - $references = $this->expand_references( $arr['references'] ); |
|
| 208 | + $references = $this->expand_references($arr['references']); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 211 | + $jsonld_array = array_merge($arr['jsonld'], $references); |
|
| 212 | 212 | |
| 213 | 213 | return $jsonld_array; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - private function get_term_url( $id ) { |
|
| 217 | - if ( null === $id ) { |
|
| 218 | - return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 216 | + private function get_term_url($id) { |
|
| 217 | + if (null === $id) { |
|
| 218 | + return isset($_SERVER['REQUEST_URI']) ? filter_var(wp_unslash($_SERVER['REQUEST_URI']), FILTER_SANITIZE_URL) : ''; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 222 | - if ( ! empty( $maybe_url ) ) { |
|
| 221 | + $maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true); |
|
| 222 | + if ( ! empty($maybe_url)) { |
|
| 223 | 223 | return $maybe_url; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - return get_term_link( $id ); |
|
| 226 | + return get_term_link($id); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | /** |
@@ -235,30 +235,30 @@ discard block |
||
| 235 | 235 | * @return array |
| 236 | 236 | */ |
| 237 | 237 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 238 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 238 | + private function get_entity_jsonld($term_id, $context) { |
|
| 239 | 239 | |
| 240 | 240 | // The `_wl_entity_id` are URIs. |
| 241 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 241 | + $entity_ids = get_term_meta($term_id, '_wl_entity_id'); |
|
| 242 | 242 | $entity_uri_service = $this->entity_uri_service; |
| 243 | 243 | |
| 244 | 244 | $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
| 245 | 245 | |
| 246 | 246 | $local_entity_ids = array_filter( |
| 247 | 247 | $entity_ids, |
| 248 | - function ( $uri ) use ( $entity_uri_service ) { |
|
| 249 | - return $entity_uri_service->is_internal( $uri ); |
|
| 248 | + function($uri) use ($entity_uri_service) { |
|
| 249 | + return $entity_uri_service->is_internal($uri); |
|
| 250 | 250 | } |
| 251 | 251 | ); |
| 252 | 252 | |
| 253 | 253 | // Bail out if there are no entities. |
| 254 | - if ( empty( $local_entity_ids ) ) { |
|
| 254 | + if (empty($local_entity_ids)) { |
|
| 255 | 255 | return array(); |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 259 | - $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 258 | + $post = $this->entity_uri_service->get_entity(array_shift($local_entity_ids)); |
|
| 259 | + $entities_jsonld = $wordlift_jsonld_service->get_jsonld(false, $post->ID); |
|
| 260 | 260 | // Reset the `url` to the term page. |
| 261 | - $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 261 | + $entities_jsonld[0]['url'] = get_term_link($term_id); |
|
| 262 | 262 | |
| 263 | 263 | return $entities_jsonld; |
| 264 | 264 | } |
@@ -268,20 +268,20 @@ discard block |
||
| 268 | 268 | * |
| 269 | 269 | * @return array |
| 270 | 270 | */ |
| 271 | - private function expand_references( $references ) { |
|
| 272 | - if ( ! is_array( $references ) ) { |
|
| 271 | + private function expand_references($references) { |
|
| 272 | + if ( ! is_array($references)) { |
|
| 273 | 273 | return array(); |
| 274 | 274 | } |
| 275 | 275 | $references_jsonld = array(); |
| 276 | 276 | // Expand the references. |
| 277 | - foreach ( $references as $reference ) { |
|
| 278 | - if ( $reference instanceof Term_Reference ) { |
|
| 277 | + foreach ($references as $reference) { |
|
| 278 | + if ($reference instanceof Term_Reference) { |
|
| 279 | 279 | // Second level references won't be expanded. |
| 280 | - $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 281 | - } elseif ( is_numeric( $reference ) ) { |
|
| 280 | + $references_jsonld[] = current($this->get($reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true)); |
|
| 281 | + } elseif (is_numeric($reference)) { |
|
| 282 | 282 | $ref_2 = array(); |
| 283 | 283 | $ref_info_2 = array(); |
| 284 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
| 284 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert($reference, $ref_2, $ref_info_2, new Relations()); |
|
| 285 | 285 | } |
| 286 | 286 | } |
| 287 | 287 | |
@@ -18,110 +18,110 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Property_Entity_Reference { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The URL associated with this entity reference. |
|
| 23 | - * |
|
| 24 | - * @since 3.8.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var string $url The URL associated with the entity reference. |
|
| 27 | - */ |
|
| 28 | - private $url; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The entity post id. |
|
| 32 | - * |
|
| 33 | - * @since 3.10.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var int $id The entity post id. |
|
| 36 | - */ |
|
| 37 | - private $id; |
|
| 38 | - |
|
| 39 | - private $required; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var int |
|
| 43 | - */ |
|
| 44 | - private $type; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Create a Wordlift_Property_Entity_Reference instance with the provided URL. |
|
| 48 | - * |
|
| 49 | - * @param string $entity_uri The URL. |
|
| 50 | - * @param int $id The entity post id. |
|
| 51 | - * @param bool $required Whether this property is always required in SD output, default false. |
|
| 52 | - * @param int $type Instance of Object_Enum_Type |
|
| 53 | - * |
|
| 54 | - * @since 3.8.0 |
|
| 55 | - */ |
|
| 56 | - public function __construct( $entity_uri, $id, $required = false, $type = Object_Type_Enum::POST ) { |
|
| 57 | - |
|
| 58 | - $this->url = $entity_uri; |
|
| 59 | - $this->id = $id; |
|
| 60 | - $this->required = $required; |
|
| 61 | - $this->type = $type; |
|
| 62 | - |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Return the type of the reference, one of the values in Object_Type_Enum |
|
| 67 | - * |
|
| 68 | - * @return int |
|
| 69 | - * @since 3.38.0 |
|
| 70 | - */ |
|
| 71 | - public function get_type() { |
|
| 72 | - return $this->type; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get the URL associated with this entity reference. |
|
| 77 | - * |
|
| 78 | - * @return string The URL associated with the entity reference. |
|
| 79 | - * @since 3.8.0 |
|
| 80 | - */ |
|
| 81 | - public function get_url() { |
|
| 82 | - |
|
| 83 | - return $this->url; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Get the entity id. |
|
| 88 | - * |
|
| 89 | - * @return int The entity id. |
|
| 90 | - * @since 3.10.0 |
|
| 91 | - */ |
|
| 92 | - public function get_id() { |
|
| 93 | - |
|
| 94 | - return $this->id; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Get the required flag for this {@link Wordlift_Property_Entity_Reference}. |
|
| 99 | - * |
|
| 100 | - * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this |
|
| 101 | - * property needs to be output to SD (JSON-LD). |
|
| 102 | - * |
|
| 103 | - * @since 3.27.1 |
|
| 104 | - */ |
|
| 105 | - public function get_required() { |
|
| 106 | - |
|
| 107 | - return $this->required; |
|
| 108 | - |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Set the required flag for this {@link Wordlift_Property_Entity_Reference}. |
|
| 113 | - * |
|
| 114 | - * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this |
|
| 115 | - * property needs to be output to SD (JSON-LD). |
|
| 116 | - * |
|
| 117 | - * @param bool $value |
|
| 118 | - * |
|
| 119 | - * @since 3.27.1 |
|
| 120 | - */ |
|
| 121 | - public function set_required( $value ) { |
|
| 122 | - |
|
| 123 | - $this->required = $value; |
|
| 124 | - |
|
| 125 | - } |
|
| 21 | + /** |
|
| 22 | + * The URL associated with this entity reference. |
|
| 23 | + * |
|
| 24 | + * @since 3.8.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var string $url The URL associated with the entity reference. |
|
| 27 | + */ |
|
| 28 | + private $url; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The entity post id. |
|
| 32 | + * |
|
| 33 | + * @since 3.10.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var int $id The entity post id. |
|
| 36 | + */ |
|
| 37 | + private $id; |
|
| 38 | + |
|
| 39 | + private $required; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var int |
|
| 43 | + */ |
|
| 44 | + private $type; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Create a Wordlift_Property_Entity_Reference instance with the provided URL. |
|
| 48 | + * |
|
| 49 | + * @param string $entity_uri The URL. |
|
| 50 | + * @param int $id The entity post id. |
|
| 51 | + * @param bool $required Whether this property is always required in SD output, default false. |
|
| 52 | + * @param int $type Instance of Object_Enum_Type |
|
| 53 | + * |
|
| 54 | + * @since 3.8.0 |
|
| 55 | + */ |
|
| 56 | + public function __construct( $entity_uri, $id, $required = false, $type = Object_Type_Enum::POST ) { |
|
| 57 | + |
|
| 58 | + $this->url = $entity_uri; |
|
| 59 | + $this->id = $id; |
|
| 60 | + $this->required = $required; |
|
| 61 | + $this->type = $type; |
|
| 62 | + |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Return the type of the reference, one of the values in Object_Type_Enum |
|
| 67 | + * |
|
| 68 | + * @return int |
|
| 69 | + * @since 3.38.0 |
|
| 70 | + */ |
|
| 71 | + public function get_type() { |
|
| 72 | + return $this->type; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get the URL associated with this entity reference. |
|
| 77 | + * |
|
| 78 | + * @return string The URL associated with the entity reference. |
|
| 79 | + * @since 3.8.0 |
|
| 80 | + */ |
|
| 81 | + public function get_url() { |
|
| 82 | + |
|
| 83 | + return $this->url; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Get the entity id. |
|
| 88 | + * |
|
| 89 | + * @return int The entity id. |
|
| 90 | + * @since 3.10.0 |
|
| 91 | + */ |
|
| 92 | + public function get_id() { |
|
| 93 | + |
|
| 94 | + return $this->id; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Get the required flag for this {@link Wordlift_Property_Entity_Reference}. |
|
| 99 | + * |
|
| 100 | + * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this |
|
| 101 | + * property needs to be output to SD (JSON-LD). |
|
| 102 | + * |
|
| 103 | + * @since 3.27.1 |
|
| 104 | + */ |
|
| 105 | + public function get_required() { |
|
| 106 | + |
|
| 107 | + return $this->required; |
|
| 108 | + |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Set the required flag for this {@link Wordlift_Property_Entity_Reference}. |
|
| 113 | + * |
|
| 114 | + * The required flag may tell converters or consumers (like {@link \Wordlift\Jsonld\Jsonld_Service} that this |
|
| 115 | + * property needs to be output to SD (JSON-LD). |
|
| 116 | + * |
|
| 117 | + * @param bool $value |
|
| 118 | + * |
|
| 119 | + * @since 3.27.1 |
|
| 120 | + */ |
|
| 121 | + public function set_required( $value ) { |
|
| 122 | + |
|
| 123 | + $this->required = $value; |
|
| 124 | + |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | 127 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @since 3.8.0 |
| 55 | 55 | */ |
| 56 | - public function __construct( $entity_uri, $id, $required = false, $type = Object_Type_Enum::POST ) { |
|
| 56 | + public function __construct($entity_uri, $id, $required = false, $type = Object_Type_Enum::POST) { |
|
| 57 | 57 | |
| 58 | 58 | $this->url = $entity_uri; |
| 59 | 59 | $this->id = $id; |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | * |
| 119 | 119 | * @since 3.27.1 |
| 120 | 120 | */ |
| 121 | - public function set_required( $value ) { |
|
| 121 | + public function set_required($value) { |
|
| 122 | 122 | |
| 123 | 123 | $this->required = $value; |
| 124 | 124 | |
@@ -11,111 +11,111 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Post_Jsonld { |
| 13 | 13 | |
| 14 | - public function enhance_post_jsonld() { |
|
| 15 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 16 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11 ); |
|
| 17 | - } |
|
| 14 | + public function enhance_post_jsonld() { |
|
| 15 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 16 | + add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11 ); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 19 | + public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 20 | 20 | |
| 21 | - $jsonld = $arr['jsonld']; |
|
| 22 | - $references = $arr['references']; |
|
| 21 | + $jsonld = $arr['jsonld']; |
|
| 22 | + $references = $arr['references']; |
|
| 23 | 23 | |
| 24 | - $this->add_mentions( $post_id, $jsonld ); |
|
| 24 | + $this->add_mentions( $post_id, $jsonld ); |
|
| 25 | 25 | |
| 26 | - return array( |
|
| 27 | - 'jsonld' => $jsonld, |
|
| 28 | - 'references' => $references, |
|
| 29 | - ); |
|
| 26 | + return array( |
|
| 27 | + 'jsonld' => $jsonld, |
|
| 28 | + 'references' => $references, |
|
| 29 | + ); |
|
| 30 | 30 | |
| 31 | - } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function add_mentions( $post_id, &$jsonld ) { |
|
| 33 | + public function add_mentions( $post_id, &$jsonld ) { |
|
| 34 | 34 | |
| 35 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 36 | - $terms = array(); |
|
| 35 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 36 | + $terms = array(); |
|
| 37 | 37 | |
| 38 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 39 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 40 | - if ( ! $taxonomy_terms ) { |
|
| 41 | - continue; |
|
| 42 | - } |
|
| 43 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 44 | - } |
|
| 38 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 39 | + $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 40 | + if ( ! $taxonomy_terms ) { |
|
| 41 | + continue; |
|
| 42 | + } |
|
| 43 | + $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - if ( ! $terms ) { |
|
| 47 | - return; |
|
| 48 | - } |
|
| 46 | + if ( ! $terms ) { |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - if ( ! array_key_exists( 'mentions', $jsonld ) && count( $terms ) > 0 ) { |
|
| 51 | - $jsonld['mentions'] = array(); |
|
| 52 | - } |
|
| 50 | + if ( ! array_key_exists( 'mentions', $jsonld ) && count( $terms ) > 0 ) { |
|
| 51 | + $jsonld['mentions'] = array(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - foreach ( $terms as $term ) { |
|
| 54 | + foreach ( $terms as $term ) { |
|
| 55 | 55 | |
| 56 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 56 | + $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 57 | 57 | |
| 58 | - if ( ! $is_matched ) { |
|
| 59 | - continue; |
|
| 60 | - } |
|
| 58 | + if ( ! $is_matched ) { |
|
| 59 | + continue; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
| 62 | + $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
| 63 | 63 | |
| 64 | - if ( count( $entities ) === 0 ) { |
|
| 65 | - continue; |
|
| 66 | - } |
|
| 64 | + if ( count( $entities ) === 0 ) { |
|
| 65 | + continue; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - $add_additional_attrs = self::add_additional_attrs( $term, $entities ); |
|
| 68 | + $add_additional_attrs = self::add_additional_attrs( $term, $entities ); |
|
| 69 | 69 | |
| 70 | - $jsonld['mentions'] = array_merge( $jsonld['mentions'], $add_additional_attrs ); |
|
| 71 | - } |
|
| 70 | + $jsonld['mentions'] = array_merge( $jsonld['mentions'], $add_additional_attrs ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @param $term \WP_Term |
|
| 77 | - * @param $entities |
|
| 78 | - * |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - public static function add_additional_attrs( $term, $entities ) { |
|
| 75 | + /** |
|
| 76 | + * @param $term \WP_Term |
|
| 77 | + * @param $entities |
|
| 78 | + * |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + public static function add_additional_attrs( $term, $entities ) { |
|
| 82 | 82 | |
| 83 | - return array_map( |
|
| 84 | - function ( $entity ) use ( $term ) { |
|
| 85 | - $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
| 86 | - if ( ! empty( $term->description ) ) { |
|
| 87 | - $entity['description'] = $term->description; |
|
| 88 | - } |
|
| 83 | + return array_map( |
|
| 84 | + function ( $entity ) use ( $term ) { |
|
| 85 | + $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
| 86 | + if ( ! empty( $term->description ) ) { |
|
| 87 | + $entity['description'] = $term->description; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - return $entity; |
|
| 90 | + return $entity; |
|
| 91 | 91 | |
| 92 | - }, |
|
| 93 | - $entities |
|
| 94 | - ); |
|
| 92 | + }, |
|
| 93 | + $entities |
|
| 94 | + ); |
|
| 95 | 95 | |
| 96 | - } |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - public function wl_after_get_jsonld( $jsonld ) { |
|
| 98 | + public function wl_after_get_jsonld( $jsonld ) { |
|
| 99 | 99 | |
| 100 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 101 | - return $jsonld; |
|
| 102 | - } |
|
| 100 | + if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 101 | + return $jsonld; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - foreach ( $jsonld as $key => $value ) { |
|
| 105 | - if ( 'Article' === $value['@type'] && isset( $value['image'] ) ) { |
|
| 106 | - $image = $value['image']; |
|
| 107 | - } |
|
| 108 | - if ( 'Recipe' === $value['@type'] && ! isset( $value['image'] ) ) { |
|
| 109 | - $index = $key; |
|
| 110 | - } |
|
| 111 | - } |
|
| 104 | + foreach ( $jsonld as $key => $value ) { |
|
| 105 | + if ( 'Article' === $value['@type'] && isset( $value['image'] ) ) { |
|
| 106 | + $image = $value['image']; |
|
| 107 | + } |
|
| 108 | + if ( 'Recipe' === $value['@type'] && ! isset( $value['image'] ) ) { |
|
| 109 | + $index = $key; |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - if ( isset( $index ) && ! empty( $image ) ) { |
|
| 114 | - $jsonld[ $index ]['image'] = $image; |
|
| 115 | - } |
|
| 113 | + if ( isset( $index ) && ! empty( $image ) ) { |
|
| 114 | + $jsonld[ $index ]['image'] = $image; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - return $jsonld; |
|
| 117 | + return $jsonld; |
|
| 118 | 118 | |
| 119 | - } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | } |
@@ -12,16 +12,16 @@ discard block |
||
| 12 | 12 | class Post_Jsonld { |
| 13 | 13 | |
| 14 | 14 | public function enhance_post_jsonld() { |
| 15 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 16 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11 ); |
|
| 15 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 16 | + add_filter('wl_after_get_jsonld', array($this, 'wl_after_get_jsonld'), 11); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 19 | + public function wl_post_jsonld_array($arr, $post_id) { |
|
| 20 | 20 | |
| 21 | 21 | $jsonld = $arr['jsonld']; |
| 22 | 22 | $references = $arr['references']; |
| 23 | 23 | |
| 24 | - $this->add_mentions( $post_id, $jsonld ); |
|
| 24 | + $this->add_mentions($post_id, $jsonld); |
|
| 25 | 25 | |
| 26 | 26 | return array( |
| 27 | 27 | 'jsonld' => $jsonld, |
@@ -30,44 +30,44 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public function add_mentions( $post_id, &$jsonld ) { |
|
| 33 | + public function add_mentions($post_id, &$jsonld) { |
|
| 34 | 34 | |
| 35 | 35 | $taxonomies = Terms_Compat::get_public_taxonomies(); |
| 36 | 36 | $terms = array(); |
| 37 | 37 | |
| 38 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 39 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 40 | - if ( ! $taxonomy_terms ) { |
|
| 38 | + foreach ($taxonomies as $taxonomy) { |
|
| 39 | + $taxonomy_terms = get_the_terms($post_id, $taxonomy); |
|
| 40 | + if ( ! $taxonomy_terms) { |
|
| 41 | 41 | continue; |
| 42 | 42 | } |
| 43 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 43 | + $terms = array_merge($taxonomy_terms, $terms); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - if ( ! $terms ) { |
|
| 46 | + if ( ! $terms) { |
|
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - if ( ! array_key_exists( 'mentions', $jsonld ) && count( $terms ) > 0 ) { |
|
| 50 | + if ( ! array_key_exists('mentions', $jsonld) && count($terms) > 0) { |
|
| 51 | 51 | $jsonld['mentions'] = array(); |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - foreach ( $terms as $term ) { |
|
| 54 | + foreach ($terms as $term) { |
|
| 55 | 55 | |
| 56 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 56 | + $is_matched = intval(get_term_meta($term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true)) === 1; |
|
| 57 | 57 | |
| 58 | - if ( ! $is_matched ) { |
|
| 58 | + if ( ! $is_matched) { |
|
| 59 | 59 | continue; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
| 62 | + $entities = Jsonld_Utils::get_matched_entities_for_term($term->term_id); |
|
| 63 | 63 | |
| 64 | - if ( count( $entities ) === 0 ) { |
|
| 64 | + if (count($entities) === 0) { |
|
| 65 | 65 | continue; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - $add_additional_attrs = self::add_additional_attrs( $term, $entities ); |
|
| 68 | + $add_additional_attrs = self::add_additional_attrs($term, $entities); |
|
| 69 | 69 | |
| 70 | - $jsonld['mentions'] = array_merge( $jsonld['mentions'], $add_additional_attrs ); |
|
| 70 | + $jsonld['mentions'] = array_merge($jsonld['mentions'], $add_additional_attrs); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | } |
@@ -78,12 +78,12 @@ discard block |
||
| 78 | 78 | * |
| 79 | 79 | * @return array |
| 80 | 80 | */ |
| 81 | - public static function add_additional_attrs( $term, $entities ) { |
|
| 81 | + public static function add_additional_attrs($term, $entities) { |
|
| 82 | 82 | |
| 83 | 83 | return array_map( |
| 84 | - function ( $entity ) use ( $term ) { |
|
| 85 | - $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
| 86 | - if ( ! empty( $term->description ) ) { |
|
| 84 | + function($entity) use ($term) { |
|
| 85 | + $entity['@id'] = get_term_link($term->term_id).'#id'; |
|
| 86 | + if ( ! empty($term->description)) { |
|
| 87 | 87 | $entity['description'] = $term->description; |
| 88 | 88 | } |
| 89 | 89 | |
@@ -95,23 +95,23 @@ discard block |
||
| 95 | 95 | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - public function wl_after_get_jsonld( $jsonld ) { |
|
| 98 | + public function wl_after_get_jsonld($jsonld) { |
|
| 99 | 99 | |
| 100 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 100 | + if ( ! is_array($jsonld) || count($jsonld) === 0) { |
|
| 101 | 101 | return $jsonld; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - foreach ( $jsonld as $key => $value ) { |
|
| 105 | - if ( 'Article' === $value['@type'] && isset( $value['image'] ) ) { |
|
| 104 | + foreach ($jsonld as $key => $value) { |
|
| 105 | + if ('Article' === $value['@type'] && isset($value['image'])) { |
|
| 106 | 106 | $image = $value['image']; |
| 107 | 107 | } |
| 108 | - if ( 'Recipe' === $value['@type'] && ! isset( $value['image'] ) ) { |
|
| 108 | + if ('Recipe' === $value['@type'] && ! isset($value['image'])) { |
|
| 109 | 109 | $index = $key; |
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - if ( isset( $index ) && ! empty( $image ) ) { |
|
| 114 | - $jsonld[ $index ]['image'] = $image; |
|
| 113 | + if (isset($index) && ! empty($image)) { |
|
| 114 | + $jsonld[$index]['image'] = $image; |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | return $jsonld; |