@@ -15,190 +15,190 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 20 | - * |
|
| 21 | - * @since 3.10.0 |
|
| 22 | - * @access private |
|
| 23 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 24 | - */ |
|
| 25 | - private $configuration_service; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A {@link Wordlift_Log_Service} instance. |
|
| 29 | - * |
|
| 30 | - * @since 3.10.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 33 | - */ |
|
| 34 | - private $log; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 38 | - * |
|
| 39 | - * @since 3.10.0 |
|
| 40 | - * |
|
| 41 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 42 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 43 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 44 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 45 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 46 | - */ |
|
| 47 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 48 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 49 | - |
|
| 50 | - $this->configuration_service = $configuration_service; |
|
| 51 | - |
|
| 52 | - // Set a reference to the logger. |
|
| 53 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 58 | - * found while processing the post is set in the $references array. |
|
| 59 | - * |
|
| 60 | - * @since 3.10.0 |
|
| 61 | - * |
|
| 62 | - * |
|
| 63 | - * @param int $post_id The post id. |
|
| 64 | - * @param array $references An array of entity references. |
|
| 65 | - * |
|
| 66 | - * @return array A JSON-LD array. |
|
| 67 | - */ |
|
| 68 | - public function convert( $post_id, &$references = array() ) { |
|
| 69 | - |
|
| 70 | - // Get the post instance. |
|
| 71 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 72 | - // Post not found. |
|
| 73 | - return null; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 77 | - $jsonld = parent::convert( $post_id, $references ); |
|
| 78 | - |
|
| 79 | - // Get the entity name. |
|
| 80 | - $jsonld['headline'] = $post->post_title; |
|
| 81 | - |
|
| 82 | - // Get the author. |
|
| 83 | - $author = get_the_author_meta( 'display_name', $post->post_author ); |
|
| 84 | - $author_id = $this->user_service->get_uri( $post->post_author ); |
|
| 85 | - |
|
| 86 | - $jsonld['author'] = array( |
|
| 87 | - '@type' => 'Person', |
|
| 88 | - '@id' => $author_id, |
|
| 89 | - 'name' => $author, |
|
| 90 | - ); |
|
| 91 | - |
|
| 92 | - // Set the published and modified dates. |
|
| 93 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 94 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 95 | - |
|
| 96 | - // Set the publisher. |
|
| 97 | - $this->set_publisher( $jsonld ); |
|
| 98 | - |
|
| 99 | - // Process the references if any. |
|
| 100 | - if ( 0 < sizeof( $references ) ) { |
|
| 101 | - |
|
| 102 | - // Prepare the `about` and `mentions` array. |
|
| 103 | - $about = $mentions = array(); |
|
| 104 | - |
|
| 105 | - // If the entity is in the title, then it should be an `about`. |
|
| 106 | - foreach ( $references as $reference ) { |
|
| 107 | - |
|
| 108 | - // Get the entity labels. |
|
| 109 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 110 | - |
|
| 111 | - // Get the entity URI. |
|
| 112 | - $item = array( '@id' => $this->entity_service->get_uri( $reference ) ); |
|
| 113 | - |
|
| 114 | - // Check if the labels match any part of the title. |
|
| 115 | - $matches = 1 === preg_match( '/' . implode( '|', $labels ) . '/', $post->post_title ); |
|
| 116 | - |
|
| 117 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 118 | - if ( $matches ) { |
|
| 119 | - $about[] = $item; |
|
| 120 | - } else { |
|
| 121 | - $mentions[] = $item; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // If we have abouts, assign them to the JSON-LD. |
|
| 126 | - if ( 0 < sizeof( $about ) ) { |
|
| 127 | - $jsonld['about'] = $about; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // If we have mentions, assign them to the JSON-LD. |
|
| 131 | - if ( 0 < sizeof( $mentions ) ) { |
|
| 132 | - $jsonld['mentions'] = $mentions; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return $jsonld; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Enrich the provided params array with publisher data, if available. |
|
| 142 | - * |
|
| 143 | - * @since 3.10.0 |
|
| 144 | - * |
|
| 145 | - * @param array $params The parameters array. |
|
| 146 | - */ |
|
| 147 | - private function set_publisher( &$params ) { |
|
| 148 | - |
|
| 149 | - // If the publisher id isn't set don't do anything. |
|
| 150 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 151 | - return; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // Get the post instance. |
|
| 155 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 156 | - // Publisher not found. |
|
| 157 | - return; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // Get the item id |
|
| 161 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 162 | - |
|
| 163 | - // Get the type. |
|
| 164 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 165 | - |
|
| 166 | - // Get the name. |
|
| 167 | - $name = $post->post_title; |
|
| 168 | - |
|
| 169 | - // Set the publisher data. |
|
| 170 | - $params['publisher'] = array( |
|
| 171 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 172 | - '@id' => $id, |
|
| 173 | - 'name' => $name, |
|
| 174 | - ); |
|
| 175 | - |
|
| 176 | - // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 177 | - // support the logo property. |
|
| 178 | - // |
|
| 179 | - // See http://schema.org/logo |
|
| 180 | - if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 181 | - return; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // Get the logo, WP < 4.4 way: only post ID accepted here. |
|
| 185 | - if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) { |
|
| 186 | - return; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - // Get the image URL. |
|
| 190 | - if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, 'full' ) ) { |
|
| 191 | - return; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // Copy over some useful properties. |
|
| 195 | - // |
|
| 196 | - // See https://developers.google.com/search/docs/data-types/articles |
|
| 197 | - $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 198 | - $params['publisher']['logo']['url'] = $attachment[0]; |
|
| 199 | - $params['publisher']['logo']['width'] = $attachment[1] . 'px'; |
|
| 200 | - $params['publisher']['logo']['height'] = $attachment[2] . 'px'; |
|
| 201 | - |
|
| 202 | - } |
|
| 18 | + /** |
|
| 19 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 20 | + * |
|
| 21 | + * @since 3.10.0 |
|
| 22 | + * @access private |
|
| 23 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 24 | + */ |
|
| 25 | + private $configuration_service; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A {@link Wordlift_Log_Service} instance. |
|
| 29 | + * |
|
| 30 | + * @since 3.10.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 33 | + */ |
|
| 34 | + private $log; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 38 | + * |
|
| 39 | + * @since 3.10.0 |
|
| 40 | + * |
|
| 41 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 42 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 43 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 44 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 45 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 46 | + */ |
|
| 47 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 48 | + parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 49 | + |
|
| 50 | + $this->configuration_service = $configuration_service; |
|
| 51 | + |
|
| 52 | + // Set a reference to the logger. |
|
| 53 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 58 | + * found while processing the post is set in the $references array. |
|
| 59 | + * |
|
| 60 | + * @since 3.10.0 |
|
| 61 | + * |
|
| 62 | + * |
|
| 63 | + * @param int $post_id The post id. |
|
| 64 | + * @param array $references An array of entity references. |
|
| 65 | + * |
|
| 66 | + * @return array A JSON-LD array. |
|
| 67 | + */ |
|
| 68 | + public function convert( $post_id, &$references = array() ) { |
|
| 69 | + |
|
| 70 | + // Get the post instance. |
|
| 71 | + if ( null === $post = get_post( $post_id ) ) { |
|
| 72 | + // Post not found. |
|
| 73 | + return null; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 77 | + $jsonld = parent::convert( $post_id, $references ); |
|
| 78 | + |
|
| 79 | + // Get the entity name. |
|
| 80 | + $jsonld['headline'] = $post->post_title; |
|
| 81 | + |
|
| 82 | + // Get the author. |
|
| 83 | + $author = get_the_author_meta( 'display_name', $post->post_author ); |
|
| 84 | + $author_id = $this->user_service->get_uri( $post->post_author ); |
|
| 85 | + |
|
| 86 | + $jsonld['author'] = array( |
|
| 87 | + '@type' => 'Person', |
|
| 88 | + '@id' => $author_id, |
|
| 89 | + 'name' => $author, |
|
| 90 | + ); |
|
| 91 | + |
|
| 92 | + // Set the published and modified dates. |
|
| 93 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 94 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 95 | + |
|
| 96 | + // Set the publisher. |
|
| 97 | + $this->set_publisher( $jsonld ); |
|
| 98 | + |
|
| 99 | + // Process the references if any. |
|
| 100 | + if ( 0 < sizeof( $references ) ) { |
|
| 101 | + |
|
| 102 | + // Prepare the `about` and `mentions` array. |
|
| 103 | + $about = $mentions = array(); |
|
| 104 | + |
|
| 105 | + // If the entity is in the title, then it should be an `about`. |
|
| 106 | + foreach ( $references as $reference ) { |
|
| 107 | + |
|
| 108 | + // Get the entity labels. |
|
| 109 | + $labels = $this->entity_service->get_labels( $reference ); |
|
| 110 | + |
|
| 111 | + // Get the entity URI. |
|
| 112 | + $item = array( '@id' => $this->entity_service->get_uri( $reference ) ); |
|
| 113 | + |
|
| 114 | + // Check if the labels match any part of the title. |
|
| 115 | + $matches = 1 === preg_match( '/' . implode( '|', $labels ) . '/', $post->post_title ); |
|
| 116 | + |
|
| 117 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 118 | + if ( $matches ) { |
|
| 119 | + $about[] = $item; |
|
| 120 | + } else { |
|
| 121 | + $mentions[] = $item; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // If we have abouts, assign them to the JSON-LD. |
|
| 126 | + if ( 0 < sizeof( $about ) ) { |
|
| 127 | + $jsonld['about'] = $about; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // If we have mentions, assign them to the JSON-LD. |
|
| 131 | + if ( 0 < sizeof( $mentions ) ) { |
|
| 132 | + $jsonld['mentions'] = $mentions; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return $jsonld; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Enrich the provided params array with publisher data, if available. |
|
| 142 | + * |
|
| 143 | + * @since 3.10.0 |
|
| 144 | + * |
|
| 145 | + * @param array $params The parameters array. |
|
| 146 | + */ |
|
| 147 | + private function set_publisher( &$params ) { |
|
| 148 | + |
|
| 149 | + // If the publisher id isn't set don't do anything. |
|
| 150 | + if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 151 | + return; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // Get the post instance. |
|
| 155 | + if ( null === $post = get_post( $publisher_id ) ) { |
|
| 156 | + // Publisher not found. |
|
| 157 | + return; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // Get the item id |
|
| 161 | + $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 162 | + |
|
| 163 | + // Get the type. |
|
| 164 | + $type = $this->entity_type_service->get( $publisher_id ); |
|
| 165 | + |
|
| 166 | + // Get the name. |
|
| 167 | + $name = $post->post_title; |
|
| 168 | + |
|
| 169 | + // Set the publisher data. |
|
| 170 | + $params['publisher'] = array( |
|
| 171 | + '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 172 | + '@id' => $id, |
|
| 173 | + 'name' => $name, |
|
| 174 | + ); |
|
| 175 | + |
|
| 176 | + // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 177 | + // support the logo property. |
|
| 178 | + // |
|
| 179 | + // See http://schema.org/logo |
|
| 180 | + if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 181 | + return; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // Get the logo, WP < 4.4 way: only post ID accepted here. |
|
| 185 | + if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) { |
|
| 186 | + return; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + // Get the image URL. |
|
| 190 | + if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, 'full' ) ) { |
|
| 191 | + return; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // Copy over some useful properties. |
|
| 195 | + // |
|
| 196 | + // See https://developers.google.com/search/docs/data-types/articles |
|
| 197 | + $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 198 | + $params['publisher']['logo']['url'] = $attachment[0]; |
|
| 199 | + $params['publisher']['logo']['width'] = $attachment[1] . 'px'; |
|
| 200 | + $params['publisher']['logo']['height'] = $attachment[2] . 'px'; |
|
| 201 | + |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | 204 | } |
@@ -44,13 +44,13 @@ discard block |
||
| 44 | 44 | * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
| 45 | 45 | * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
| 46 | 46 | */ |
| 47 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 48 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 47 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service) { |
|
| 48 | + parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service); |
|
| 49 | 49 | |
| 50 | 50 | $this->configuration_service = $configuration_service; |
| 51 | 51 | |
| 52 | 52 | // Set a reference to the logger. |
| 53 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 53 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter'); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
@@ -65,23 +65,23 @@ discard block |
||
| 65 | 65 | * |
| 66 | 66 | * @return array A JSON-LD array. |
| 67 | 67 | */ |
| 68 | - public function convert( $post_id, &$references = array() ) { |
|
| 68 | + public function convert($post_id, &$references = array()) { |
|
| 69 | 69 | |
| 70 | 70 | // Get the post instance. |
| 71 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 71 | + if (null === $post = get_post($post_id)) { |
|
| 72 | 72 | // Post not found. |
| 73 | 73 | return null; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 77 | - $jsonld = parent::convert( $post_id, $references ); |
|
| 77 | + $jsonld = parent::convert($post_id, $references); |
|
| 78 | 78 | |
| 79 | 79 | // Get the entity name. |
| 80 | 80 | $jsonld['headline'] = $post->post_title; |
| 81 | 81 | |
| 82 | 82 | // Get the author. |
| 83 | - $author = get_the_author_meta( 'display_name', $post->post_author ); |
|
| 84 | - $author_id = $this->user_service->get_uri( $post->post_author ); |
|
| 83 | + $author = get_the_author_meta('display_name', $post->post_author); |
|
| 84 | + $author_id = $this->user_service->get_uri($post->post_author); |
|
| 85 | 85 | |
| 86 | 86 | $jsonld['author'] = array( |
| 87 | 87 | '@type' => 'Person', |
@@ -90,32 +90,32 @@ discard block |
||
| 90 | 90 | ); |
| 91 | 91 | |
| 92 | 92 | // Set the published and modified dates. |
| 93 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 94 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 93 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 94 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 95 | 95 | |
| 96 | 96 | // Set the publisher. |
| 97 | - $this->set_publisher( $jsonld ); |
|
| 97 | + $this->set_publisher($jsonld); |
|
| 98 | 98 | |
| 99 | 99 | // Process the references if any. |
| 100 | - if ( 0 < sizeof( $references ) ) { |
|
| 100 | + if (0 < sizeof($references)) { |
|
| 101 | 101 | |
| 102 | 102 | // Prepare the `about` and `mentions` array. |
| 103 | 103 | $about = $mentions = array(); |
| 104 | 104 | |
| 105 | 105 | // If the entity is in the title, then it should be an `about`. |
| 106 | - foreach ( $references as $reference ) { |
|
| 106 | + foreach ($references as $reference) { |
|
| 107 | 107 | |
| 108 | 108 | // Get the entity labels. |
| 109 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 109 | + $labels = $this->entity_service->get_labels($reference); |
|
| 110 | 110 | |
| 111 | 111 | // Get the entity URI. |
| 112 | - $item = array( '@id' => $this->entity_service->get_uri( $reference ) ); |
|
| 112 | + $item = array('@id' => $this->entity_service->get_uri($reference)); |
|
| 113 | 113 | |
| 114 | 114 | // Check if the labels match any part of the title. |
| 115 | - $matches = 1 === preg_match( '/' . implode( '|', $labels ) . '/', $post->post_title ); |
|
| 115 | + $matches = 1 === preg_match('/'.implode('|', $labels).'/', $post->post_title); |
|
| 116 | 116 | |
| 117 | 117 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
| 118 | - if ( $matches ) { |
|
| 118 | + if ($matches) { |
|
| 119 | 119 | $about[] = $item; |
| 120 | 120 | } else { |
| 121 | 121 | $mentions[] = $item; |
@@ -123,12 +123,12 @@ discard block |
||
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // If we have abouts, assign them to the JSON-LD. |
| 126 | - if ( 0 < sizeof( $about ) ) { |
|
| 126 | + if (0 < sizeof($about)) { |
|
| 127 | 127 | $jsonld['about'] = $about; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // If we have mentions, assign them to the JSON-LD. |
| 131 | - if ( 0 < sizeof( $mentions ) ) { |
|
| 131 | + if (0 < sizeof($mentions)) { |
|
| 132 | 132 | $jsonld['mentions'] = $mentions; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -144,31 +144,31 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @param array $params The parameters array. |
| 146 | 146 | */ |
| 147 | - private function set_publisher( &$params ) { |
|
| 147 | + private function set_publisher(&$params) { |
|
| 148 | 148 | |
| 149 | 149 | // If the publisher id isn't set don't do anything. |
| 150 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 150 | + if (null === $publisher_id = $this->configuration_service->get_publisher_id()) { |
|
| 151 | 151 | return; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | // Get the post instance. |
| 155 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 155 | + if (null === $post = get_post($publisher_id)) { |
|
| 156 | 156 | // Publisher not found. |
| 157 | 157 | return; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Get the item id |
| 161 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 161 | + $id = $this->entity_service->get_uri($publisher_id); |
|
| 162 | 162 | |
| 163 | 163 | // Get the type. |
| 164 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 164 | + $type = $this->entity_type_service->get($publisher_id); |
|
| 165 | 165 | |
| 166 | 166 | // Get the name. |
| 167 | 167 | $name = $post->post_title; |
| 168 | 168 | |
| 169 | 169 | // Set the publisher data. |
| 170 | 170 | $params['publisher'] = array( |
| 171 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 171 | + '@type' => $this->relative_to_context($type['uri']), |
|
| 172 | 172 | '@id' => $id, |
| 173 | 173 | 'name' => $name, |
| 174 | 174 | ); |
@@ -177,17 +177,17 @@ discard block |
||
| 177 | 177 | // support the logo property. |
| 178 | 178 | // |
| 179 | 179 | // See http://schema.org/logo |
| 180 | - if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 180 | + if ('http://schema.org/Organization' !== $type['uri']) { |
|
| 181 | 181 | return; |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | // Get the logo, WP < 4.4 way: only post ID accepted here. |
| 185 | - if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) { |
|
| 185 | + if ('' === $thumbnail_id = get_post_thumbnail_id($post->ID)) { |
|
| 186 | 186 | return; |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | // Get the image URL. |
| 190 | - if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, 'full' ) ) { |
|
| 190 | + if (false === $attachment = wp_get_attachment_image_src($thumbnail_id, 'full')) { |
|
| 191 | 191 | return; |
| 192 | 192 | } |
| 193 | 193 | |
@@ -196,8 +196,8 @@ discard block |
||
| 196 | 196 | // See https://developers.google.com/search/docs/data-types/articles |
| 197 | 197 | $params['publisher']['logo']['@type'] = 'ImageObject'; |
| 198 | 198 | $params['publisher']['logo']['url'] = $attachment[0]; |
| 199 | - $params['publisher']['logo']['width'] = $attachment[1] . 'px'; |
|
| 200 | - $params['publisher']['logo']['height'] = $attachment[2] . 'px'; |
|
| 199 | + $params['publisher']['logo']['width'] = $attachment[1].'px'; |
|
| 200 | + $params['publisher']['logo']['height'] = $attachment[2].'px'; |
|
| 201 | 201 | |
| 202 | 202 | } |
| 203 | 203 | |
@@ -7,486 +7,486 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Entity_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The Log service. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - * @access private |
|
| 15 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The UI service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | - */ |
|
| 26 | - private $ui_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The entity post type name. |
|
| 30 | - * |
|
| 31 | - * @since 3.1.0 |
|
| 32 | - */ |
|
| 33 | - const TYPE_NAME = 'entity'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The alternative label meta key. |
|
| 37 | - * |
|
| 38 | - * @since 3.2.0 |
|
| 39 | - */ |
|
| 40 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The alternative label input template. |
|
| 44 | - * |
|
| 45 | - * @since 3.2.0 |
|
| 46 | - */ |
|
| 47 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 48 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The UI service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | + */ |
|
| 26 | + private $ui_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The entity post type name. |
|
| 30 | + * |
|
| 31 | + * @since 3.1.0 |
|
| 32 | + */ |
|
| 33 | + const TYPE_NAME = 'entity'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The alternative label meta key. |
|
| 37 | + * |
|
| 38 | + * @since 3.2.0 |
|
| 39 | + */ |
|
| 40 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The alternative label input template. |
|
| 44 | + * |
|
| 45 | + * @since 3.2.0 |
|
| 46 | + */ |
|
| 47 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 48 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 49 | 49 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 50 | 50 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 51 | 51 | <button class="button wl-delete-button">%s</button> |
| 52 | 52 | </div>'; |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * A singleton instance of the Entity service. |
|
| 56 | - * |
|
| 57 | - * @since 3.2.0 |
|
| 58 | - * @access private |
|
| 59 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 60 | - */ |
|
| 61 | - private static $instance; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Create a Wordlift_Entity_Service instance. |
|
| 65 | - * |
|
| 66 | - * @since 3.2.0 |
|
| 67 | - * |
|
| 68 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 69 | - */ |
|
| 70 | - public function __construct( $ui_service ) { |
|
| 71 | - |
|
| 72 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 73 | - |
|
| 74 | - // Set the UI service. |
|
| 75 | - $this->ui_service = $ui_service; |
|
| 76 | - |
|
| 77 | - // Set the singleton instance. |
|
| 78 | - self::$instance = $this; |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get the singleton instance of the Entity service. |
|
| 84 | - * |
|
| 85 | - * @since 3.2.0 |
|
| 86 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 87 | - */ |
|
| 88 | - public static function get_instance() { |
|
| 89 | - |
|
| 90 | - return self::$instance; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Determines whether a post is an entity or not. |
|
| 95 | - * |
|
| 96 | - * @since 3.1.0 |
|
| 97 | - * |
|
| 98 | - * @param int $post_id A post id. |
|
| 99 | - * |
|
| 100 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 101 | - */ |
|
| 102 | - public function is_entity( $post_id ) { |
|
| 103 | - |
|
| 104 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the proper classification scope for a given entity post |
|
| 109 | - * |
|
| 110 | - * @since 3.5.0 |
|
| 111 | - * |
|
| 112 | - * @param integer $post_id An entity post id. |
|
| 113 | - * |
|
| 114 | - * @return string Returns an uri. |
|
| 115 | - */ |
|
| 116 | - public function get_classification_scope_for( $post_id ) { |
|
| 117 | - |
|
| 118 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 119 | - return null; |
|
| 120 | - } |
|
| 121 | - // Retrieve the entity type |
|
| 122 | - $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 124 | - // Retrieve classification boxes configuration |
|
| 125 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | - foreach ( $classification_boxes as $cb ) { |
|
| 127 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 128 | - return $cb['id']; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // or null |
|
| 133 | - return null; |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - public function is_used( $post_id ) { |
|
| 139 | - |
|
| 140 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 141 | - return null; |
|
| 142 | - } |
|
| 143 | - // Retrieve the post |
|
| 144 | - $entity = get_post( $post_id ); |
|
| 145 | - |
|
| 146 | - global $wpdb; |
|
| 147 | - // Retrieve Wordlift relation instances table name |
|
| 148 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 149 | - |
|
| 150 | - // Check is it's referenced / related to another post / entity |
|
| 151 | - $stmt = $wpdb->prepare( |
|
| 152 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 153 | - $entity->ID |
|
| 154 | - ); |
|
| 155 | - |
|
| 156 | - // Perform the query |
|
| 157 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 158 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 159 | - if ( 0 < $relation_instances ) { |
|
| 160 | - return true; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Check if the entity uri is used as meta_value |
|
| 164 | - $stmt = $wpdb->prepare( |
|
| 165 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 166 | - $entity->ID, |
|
| 167 | - wl_get_entity_uri( $entity->ID ) |
|
| 168 | - ); |
|
| 169 | - // Perform the query |
|
| 170 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 171 | - |
|
| 172 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 173 | - if ( 0 < $meta_instances ) { |
|
| 174 | - return true; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // If we are here, it means the current entity is not used at the moment |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Determines whether a given uri is an internal uri or not. |
|
| 183 | - * |
|
| 184 | - * @since 3.3.2 |
|
| 185 | - * |
|
| 186 | - * @param int $uri An uri. |
|
| 187 | - * |
|
| 188 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 189 | - */ |
|
| 190 | - public function is_internal_uri( $uri ) { |
|
| 191 | - |
|
| 192 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 197 | - * |
|
| 198 | - * @since 3.2.0 |
|
| 199 | - * |
|
| 200 | - * @param string $uri The entity URI. |
|
| 201 | - * |
|
| 202 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 203 | - */ |
|
| 204 | - public function get_entity_post_by_uri( $uri ) { |
|
| 205 | - |
|
| 206 | - // Check if we've been provided with a value otherwise return null. |
|
| 207 | - if ( empty( $uri ) ) { |
|
| 208 | - return null; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $query_args = array( |
|
| 212 | - 'posts_per_page' => 1, |
|
| 213 | - 'post_status' => 'any', |
|
| 214 | - 'post_type' => self::TYPE_NAME, |
|
| 215 | - 'meta_query' => array( |
|
| 216 | - array( |
|
| 217 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 218 | - 'value' => $uri, |
|
| 219 | - 'compare' => '=', |
|
| 220 | - ), |
|
| 221 | - ), |
|
| 222 | - ); |
|
| 223 | - |
|
| 224 | - // Only if the current uri is not an internal uri, entity search is |
|
| 225 | - // performed also looking at sameAs values. |
|
| 226 | - // |
|
| 227 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 228 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 229 | - |
|
| 230 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 231 | - $query_args['meta_query'][] = array( |
|
| 232 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 233 | - 'value' => $uri, |
|
| 234 | - 'compare' => '=', |
|
| 235 | - ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $query = new WP_Query( $query_args ); |
|
| 239 | - |
|
| 240 | - // Get the matching entity posts. |
|
| 241 | - $posts = $query->get_posts(); |
|
| 242 | - |
|
| 243 | - // Return null if no post is found. |
|
| 244 | - if ( 0 === count( $posts ) ) { |
|
| 245 | - return null; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Return the found post. |
|
| 249 | - return $posts[0]; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 254 | - * we check that the post we're saving is the current post. |
|
| 255 | - * |
|
| 256 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 257 | - * |
|
| 258 | - * @since 3.2.0 |
|
| 259 | - * |
|
| 260 | - * @param int $post_id Post ID. |
|
| 261 | - * @param WP_Post $post Post object. |
|
| 262 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 263 | - */ |
|
| 264 | - public function save_post( $post_id, $post, $update ) { |
|
| 265 | - |
|
| 266 | - // Avoid doing anything if post is autosave or a revision. |
|
| 267 | - |
|
| 268 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 269 | - return; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // We're setting the alternative label that have been provided via the UI |
|
| 273 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 274 | - // programmatically by some other function: we need to check therefore if |
|
| 275 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 276 | - // |
|
| 277 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 278 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 279 | - return; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - // Get the alt labels from the request (or empty array). |
|
| 283 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 284 | - |
|
| 285 | - // Set the alternative labels. |
|
| 286 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 287 | - |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Set the alternative labels. |
|
| 292 | - * |
|
| 293 | - * @since 3.2.0 |
|
| 294 | - * |
|
| 295 | - * @param int $post_id The post id. |
|
| 296 | - * @param array $alt_labels An array of labels. |
|
| 297 | - */ |
|
| 298 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 299 | - |
|
| 300 | - // Force $alt_labels to be an array |
|
| 301 | - if ( ! is_array( $alt_labels ) ) { |
|
| 302 | - $alt_labels = array( $alt_labels ); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 306 | - |
|
| 307 | - // Delete all the existing alternate labels. |
|
| 308 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 309 | - |
|
| 310 | - // Set the alternative labels. |
|
| 311 | - foreach ( $alt_labels as $alt_label ) { |
|
| 312 | - if ( ! empty( $alt_label ) ) { |
|
| 313 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 314 | - } |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Retrieve the alternate labels. |
|
| 321 | - * |
|
| 322 | - * @since 3.2.0 |
|
| 323 | - * |
|
| 324 | - * @param int $post_id Post id. |
|
| 325 | - * |
|
| 326 | - * @return mixed An array of alternative labels. |
|
| 327 | - */ |
|
| 328 | - public function get_alternative_labels( $post_id ) { |
|
| 329 | - |
|
| 330 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 335 | - * |
|
| 336 | - * @since 3.12.0 |
|
| 337 | - * |
|
| 338 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 339 | - * |
|
| 340 | - * @return array An array with the entity title and labels. |
|
| 341 | - */ |
|
| 342 | - public function get_labels( $post_id ) { |
|
| 343 | - |
|
| 344 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 349 | - * |
|
| 350 | - * @since 3.2.0 |
|
| 351 | - * |
|
| 352 | - * @param WP_Post $post Post object. |
|
| 353 | - */ |
|
| 354 | - public function edit_form_before_permalink( $post ) { |
|
| 355 | - |
|
| 356 | - // If it's not an entity, return. |
|
| 357 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 358 | - return; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - // Print the input template. |
|
| 362 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 363 | - |
|
| 364 | - // Print all the currently set alternative labels. |
|
| 365 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 366 | - |
|
| 367 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 368 | - |
|
| 369 | - }; |
|
| 370 | - |
|
| 371 | - // Print the button. |
|
| 372 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 373 | - |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Get the URI for the entity with the specified post id. |
|
| 378 | - * |
|
| 379 | - * @since 3.6.0 |
|
| 380 | - * |
|
| 381 | - * @param int $post_id The entity post id. |
|
| 382 | - * |
|
| 383 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 384 | - */ |
|
| 385 | - public function get_uri( $post_id ) { |
|
| 386 | - |
|
| 387 | - // If a null is given, nothing to do |
|
| 388 | - if ( null == $post_id ) { |
|
| 389 | - return null; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 393 | - |
|
| 394 | - // If the dataset uri is not properly configured, null is returned |
|
| 395 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 396 | - return null; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - // Set the URI if it isn't set yet. |
|
| 400 | - $post_status = get_post_status( $post_id ); |
|
| 401 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 402 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 403 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - return $uri; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Get the alternative label input HTML code. |
|
| 412 | - * |
|
| 413 | - * @since 3.2.0 |
|
| 414 | - * |
|
| 415 | - * @param string $value The input value. |
|
| 416 | - * |
|
| 417 | - * @return string The input HTML code. |
|
| 418 | - */ |
|
| 419 | - private function get_alternative_label_input( $value = '' ) { |
|
| 420 | - |
|
| 421 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Get the number of entity posts published in this blog. |
|
| 426 | - * |
|
| 427 | - * @since 3.6.0 |
|
| 428 | - * |
|
| 429 | - * @return int The number of published entity posts. |
|
| 430 | - */ |
|
| 431 | - public function count() { |
|
| 432 | - |
|
| 433 | - $count = wp_count_posts( self::TYPE_NAME ); |
|
| 434 | - |
|
| 435 | - return $count->publish; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * Create a new entity. |
|
| 440 | - * |
|
| 441 | - * @since 3.9.0 |
|
| 442 | - * |
|
| 443 | - * @param string $name The entity name. |
|
| 444 | - * @param string $type_uri The entity's type URI. |
|
| 445 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 446 | - * @param string $status The post status, by default 'publish'. |
|
| 447 | - * |
|
| 448 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 449 | - */ |
|
| 450 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 451 | - |
|
| 452 | - // Create an entity for the publisher. |
|
| 453 | - $post_id = wp_insert_post( array( |
|
| 454 | - 'post_type' => self::TYPE_NAME, |
|
| 455 | - 'post_title' => $name, |
|
| 456 | - 'post_status' => $status, |
|
| 457 | - 'post_content' => '', |
|
| 458 | - ) ); |
|
| 459 | - |
|
| 460 | - // Return the error if any. |
|
| 461 | - if ( is_wp_error( $post_id ) ) { |
|
| 462 | - return $post_id; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - // Set the entity logo. |
|
| 466 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 467 | - set_post_thumbnail( $post_id, $logo ); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - // Set the entity type. |
|
| 471 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 472 | - |
|
| 473 | - return $post_id; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Get the entities related to the one with the specified id. By default only |
|
| 478 | - * published entities will be returned. |
|
| 479 | - * |
|
| 480 | - * @since 3.10.0 |
|
| 481 | - * |
|
| 482 | - * @param int $id The post id. |
|
| 483 | - * @param string $post_status The target post status (default = publish). |
|
| 484 | - * |
|
| 485 | - * @return array An array of post ids. |
|
| 486 | - */ |
|
| 487 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 488 | - |
|
| 489 | - return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 490 | - } |
|
| 54 | + /** |
|
| 55 | + * A singleton instance of the Entity service. |
|
| 56 | + * |
|
| 57 | + * @since 3.2.0 |
|
| 58 | + * @access private |
|
| 59 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 60 | + */ |
|
| 61 | + private static $instance; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Create a Wordlift_Entity_Service instance. |
|
| 65 | + * |
|
| 66 | + * @since 3.2.0 |
|
| 67 | + * |
|
| 68 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 69 | + */ |
|
| 70 | + public function __construct( $ui_service ) { |
|
| 71 | + |
|
| 72 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 73 | + |
|
| 74 | + // Set the UI service. |
|
| 75 | + $this->ui_service = $ui_service; |
|
| 76 | + |
|
| 77 | + // Set the singleton instance. |
|
| 78 | + self::$instance = $this; |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get the singleton instance of the Entity service. |
|
| 84 | + * |
|
| 85 | + * @since 3.2.0 |
|
| 86 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 87 | + */ |
|
| 88 | + public static function get_instance() { |
|
| 89 | + |
|
| 90 | + return self::$instance; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Determines whether a post is an entity or not. |
|
| 95 | + * |
|
| 96 | + * @since 3.1.0 |
|
| 97 | + * |
|
| 98 | + * @param int $post_id A post id. |
|
| 99 | + * |
|
| 100 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 101 | + */ |
|
| 102 | + public function is_entity( $post_id ) { |
|
| 103 | + |
|
| 104 | + return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the proper classification scope for a given entity post |
|
| 109 | + * |
|
| 110 | + * @since 3.5.0 |
|
| 111 | + * |
|
| 112 | + * @param integer $post_id An entity post id. |
|
| 113 | + * |
|
| 114 | + * @return string Returns an uri. |
|
| 115 | + */ |
|
| 116 | + public function get_classification_scope_for( $post_id ) { |
|
| 117 | + |
|
| 118 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 119 | + return null; |
|
| 120 | + } |
|
| 121 | + // Retrieve the entity type |
|
| 122 | + $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 124 | + // Retrieve classification boxes configuration |
|
| 125 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | + foreach ( $classification_boxes as $cb ) { |
|
| 127 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 128 | + return $cb['id']; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // or null |
|
| 133 | + return null; |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + public function is_used( $post_id ) { |
|
| 139 | + |
|
| 140 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 141 | + return null; |
|
| 142 | + } |
|
| 143 | + // Retrieve the post |
|
| 144 | + $entity = get_post( $post_id ); |
|
| 145 | + |
|
| 146 | + global $wpdb; |
|
| 147 | + // Retrieve Wordlift relation instances table name |
|
| 148 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 149 | + |
|
| 150 | + // Check is it's referenced / related to another post / entity |
|
| 151 | + $stmt = $wpdb->prepare( |
|
| 152 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 153 | + $entity->ID |
|
| 154 | + ); |
|
| 155 | + |
|
| 156 | + // Perform the query |
|
| 157 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 158 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 159 | + if ( 0 < $relation_instances ) { |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Check if the entity uri is used as meta_value |
|
| 164 | + $stmt = $wpdb->prepare( |
|
| 165 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 166 | + $entity->ID, |
|
| 167 | + wl_get_entity_uri( $entity->ID ) |
|
| 168 | + ); |
|
| 169 | + // Perform the query |
|
| 170 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 171 | + |
|
| 172 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 173 | + if ( 0 < $meta_instances ) { |
|
| 174 | + return true; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // If we are here, it means the current entity is not used at the moment |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Determines whether a given uri is an internal uri or not. |
|
| 183 | + * |
|
| 184 | + * @since 3.3.2 |
|
| 185 | + * |
|
| 186 | + * @param int $uri An uri. |
|
| 187 | + * |
|
| 188 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 189 | + */ |
|
| 190 | + public function is_internal_uri( $uri ) { |
|
| 191 | + |
|
| 192 | + return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 197 | + * |
|
| 198 | + * @since 3.2.0 |
|
| 199 | + * |
|
| 200 | + * @param string $uri The entity URI. |
|
| 201 | + * |
|
| 202 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 203 | + */ |
|
| 204 | + public function get_entity_post_by_uri( $uri ) { |
|
| 205 | + |
|
| 206 | + // Check if we've been provided with a value otherwise return null. |
|
| 207 | + if ( empty( $uri ) ) { |
|
| 208 | + return null; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $query_args = array( |
|
| 212 | + 'posts_per_page' => 1, |
|
| 213 | + 'post_status' => 'any', |
|
| 214 | + 'post_type' => self::TYPE_NAME, |
|
| 215 | + 'meta_query' => array( |
|
| 216 | + array( |
|
| 217 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 218 | + 'value' => $uri, |
|
| 219 | + 'compare' => '=', |
|
| 220 | + ), |
|
| 221 | + ), |
|
| 222 | + ); |
|
| 223 | + |
|
| 224 | + // Only if the current uri is not an internal uri, entity search is |
|
| 225 | + // performed also looking at sameAs values. |
|
| 226 | + // |
|
| 227 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 228 | + if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 229 | + |
|
| 230 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 231 | + $query_args['meta_query'][] = array( |
|
| 232 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 233 | + 'value' => $uri, |
|
| 234 | + 'compare' => '=', |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $query = new WP_Query( $query_args ); |
|
| 239 | + |
|
| 240 | + // Get the matching entity posts. |
|
| 241 | + $posts = $query->get_posts(); |
|
| 242 | + |
|
| 243 | + // Return null if no post is found. |
|
| 244 | + if ( 0 === count( $posts ) ) { |
|
| 245 | + return null; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Return the found post. |
|
| 249 | + return $posts[0]; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 254 | + * we check that the post we're saving is the current post. |
|
| 255 | + * |
|
| 256 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 257 | + * |
|
| 258 | + * @since 3.2.0 |
|
| 259 | + * |
|
| 260 | + * @param int $post_id Post ID. |
|
| 261 | + * @param WP_Post $post Post object. |
|
| 262 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 263 | + */ |
|
| 264 | + public function save_post( $post_id, $post, $update ) { |
|
| 265 | + |
|
| 266 | + // Avoid doing anything if post is autosave or a revision. |
|
| 267 | + |
|
| 268 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 269 | + return; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // We're setting the alternative label that have been provided via the UI |
|
| 273 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 274 | + // programmatically by some other function: we need to check therefore if |
|
| 275 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 276 | + // |
|
| 277 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 278 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 279 | + return; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + // Get the alt labels from the request (or empty array). |
|
| 283 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 284 | + |
|
| 285 | + // Set the alternative labels. |
|
| 286 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 287 | + |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Set the alternative labels. |
|
| 292 | + * |
|
| 293 | + * @since 3.2.0 |
|
| 294 | + * |
|
| 295 | + * @param int $post_id The post id. |
|
| 296 | + * @param array $alt_labels An array of labels. |
|
| 297 | + */ |
|
| 298 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 299 | + |
|
| 300 | + // Force $alt_labels to be an array |
|
| 301 | + if ( ! is_array( $alt_labels ) ) { |
|
| 302 | + $alt_labels = array( $alt_labels ); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 306 | + |
|
| 307 | + // Delete all the existing alternate labels. |
|
| 308 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 309 | + |
|
| 310 | + // Set the alternative labels. |
|
| 311 | + foreach ( $alt_labels as $alt_label ) { |
|
| 312 | + if ( ! empty( $alt_label ) ) { |
|
| 313 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 314 | + } |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Retrieve the alternate labels. |
|
| 321 | + * |
|
| 322 | + * @since 3.2.0 |
|
| 323 | + * |
|
| 324 | + * @param int $post_id Post id. |
|
| 325 | + * |
|
| 326 | + * @return mixed An array of alternative labels. |
|
| 327 | + */ |
|
| 328 | + public function get_alternative_labels( $post_id ) { |
|
| 329 | + |
|
| 330 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 335 | + * |
|
| 336 | + * @since 3.12.0 |
|
| 337 | + * |
|
| 338 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 339 | + * |
|
| 340 | + * @return array An array with the entity title and labels. |
|
| 341 | + */ |
|
| 342 | + public function get_labels( $post_id ) { |
|
| 343 | + |
|
| 344 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 349 | + * |
|
| 350 | + * @since 3.2.0 |
|
| 351 | + * |
|
| 352 | + * @param WP_Post $post Post object. |
|
| 353 | + */ |
|
| 354 | + public function edit_form_before_permalink( $post ) { |
|
| 355 | + |
|
| 356 | + // If it's not an entity, return. |
|
| 357 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 358 | + return; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + // Print the input template. |
|
| 362 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 363 | + |
|
| 364 | + // Print all the currently set alternative labels. |
|
| 365 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 366 | + |
|
| 367 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 368 | + |
|
| 369 | + }; |
|
| 370 | + |
|
| 371 | + // Print the button. |
|
| 372 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 373 | + |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Get the URI for the entity with the specified post id. |
|
| 378 | + * |
|
| 379 | + * @since 3.6.0 |
|
| 380 | + * |
|
| 381 | + * @param int $post_id The entity post id. |
|
| 382 | + * |
|
| 383 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 384 | + */ |
|
| 385 | + public function get_uri( $post_id ) { |
|
| 386 | + |
|
| 387 | + // If a null is given, nothing to do |
|
| 388 | + if ( null == $post_id ) { |
|
| 389 | + return null; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 393 | + |
|
| 394 | + // If the dataset uri is not properly configured, null is returned |
|
| 395 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 396 | + return null; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + // Set the URI if it isn't set yet. |
|
| 400 | + $post_status = get_post_status( $post_id ); |
|
| 401 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 402 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 403 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + return $uri; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Get the alternative label input HTML code. |
|
| 412 | + * |
|
| 413 | + * @since 3.2.0 |
|
| 414 | + * |
|
| 415 | + * @param string $value The input value. |
|
| 416 | + * |
|
| 417 | + * @return string The input HTML code. |
|
| 418 | + */ |
|
| 419 | + private function get_alternative_label_input( $value = '' ) { |
|
| 420 | + |
|
| 421 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Get the number of entity posts published in this blog. |
|
| 426 | + * |
|
| 427 | + * @since 3.6.0 |
|
| 428 | + * |
|
| 429 | + * @return int The number of published entity posts. |
|
| 430 | + */ |
|
| 431 | + public function count() { |
|
| 432 | + |
|
| 433 | + $count = wp_count_posts( self::TYPE_NAME ); |
|
| 434 | + |
|
| 435 | + return $count->publish; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Create a new entity. |
|
| 440 | + * |
|
| 441 | + * @since 3.9.0 |
|
| 442 | + * |
|
| 443 | + * @param string $name The entity name. |
|
| 444 | + * @param string $type_uri The entity's type URI. |
|
| 445 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 446 | + * @param string $status The post status, by default 'publish'. |
|
| 447 | + * |
|
| 448 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 449 | + */ |
|
| 450 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 451 | + |
|
| 452 | + // Create an entity for the publisher. |
|
| 453 | + $post_id = wp_insert_post( array( |
|
| 454 | + 'post_type' => self::TYPE_NAME, |
|
| 455 | + 'post_title' => $name, |
|
| 456 | + 'post_status' => $status, |
|
| 457 | + 'post_content' => '', |
|
| 458 | + ) ); |
|
| 459 | + |
|
| 460 | + // Return the error if any. |
|
| 461 | + if ( is_wp_error( $post_id ) ) { |
|
| 462 | + return $post_id; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + // Set the entity logo. |
|
| 466 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 467 | + set_post_thumbnail( $post_id, $logo ); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + // Set the entity type. |
|
| 471 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 472 | + |
|
| 473 | + return $post_id; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Get the entities related to the one with the specified id. By default only |
|
| 478 | + * published entities will be returned. |
|
| 479 | + * |
|
| 480 | + * @since 3.10.0 |
|
| 481 | + * |
|
| 482 | + * @param int $id The post id. |
|
| 483 | + * @param string $post_status The target post status (default = publish). |
|
| 484 | + * |
|
| 485 | + * @return array An array of post ids. |
|
| 486 | + */ |
|
| 487 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 488 | + |
|
| 489 | + return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 490 | + } |
|
| 491 | 491 | |
| 492 | 492 | } |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 69 | 69 | */ |
| 70 | - public function __construct( $ui_service ) { |
|
| 70 | + public function __construct($ui_service) { |
|
| 71 | 71 | |
| 72 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 72 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 73 | 73 | |
| 74 | 74 | // Set the UI service. |
| 75 | 75 | $this->ui_service = $ui_service; |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | * |
| 100 | 100 | * @return bool Return true if the post is an entity otherwise false. |
| 101 | 101 | */ |
| 102 | - public function is_entity( $post_id ) { |
|
| 102 | + public function is_entity($post_id) { |
|
| 103 | 103 | |
| 104 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 104 | + return (self::TYPE_NAME === get_post_type($post_id)); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -113,18 +113,18 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return string Returns an uri. |
| 115 | 115 | */ |
| 116 | - public function get_classification_scope_for( $post_id ) { |
|
| 116 | + public function get_classification_scope_for($post_id) { |
|
| 117 | 117 | |
| 118 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 118 | + if (false === $this->is_entity($post_id)) { |
|
| 119 | 119 | return null; |
| 120 | 120 | } |
| 121 | 121 | // Retrieve the entity type |
| 122 | - $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 122 | + $entity_type_arr = wl_entity_type_taxonomy_get_type($post_id); |
|
| 123 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 124 | 124 | // Retrieve classification boxes configuration |
| 125 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | - foreach ( $classification_boxes as $cb ) { |
|
| 127 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 125 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 126 | + foreach ($classification_boxes as $cb) { |
|
| 127 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 128 | 128 | return $cb['id']; |
| 129 | 129 | } |
| 130 | 130 | } |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | |
| 138 | - public function is_used( $post_id ) { |
|
| 138 | + public function is_used($post_id) { |
|
| 139 | 139 | |
| 140 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 140 | + if (false === $this->is_entity($post_id)) { |
|
| 141 | 141 | return null; |
| 142 | 142 | } |
| 143 | 143 | // Retrieve the post |
| 144 | - $entity = get_post( $post_id ); |
|
| 144 | + $entity = get_post($post_id); |
|
| 145 | 145 | |
| 146 | 146 | global $wpdb; |
| 147 | 147 | // Retrieve Wordlift relation instances table name |
@@ -154,9 +154,9 @@ discard block |
||
| 154 | 154 | ); |
| 155 | 155 | |
| 156 | 156 | // Perform the query |
| 157 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 157 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 158 | 158 | // If there is at least one relation instance for the current entity, then it's used |
| 159 | - if ( 0 < $relation_instances ) { |
|
| 159 | + if (0 < $relation_instances) { |
|
| 160 | 160 | return true; |
| 161 | 161 | } |
| 162 | 162 | |
@@ -164,13 +164,13 @@ discard block |
||
| 164 | 164 | $stmt = $wpdb->prepare( |
| 165 | 165 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 166 | 166 | $entity->ID, |
| 167 | - wl_get_entity_uri( $entity->ID ) |
|
| 167 | + wl_get_entity_uri($entity->ID) |
|
| 168 | 168 | ); |
| 169 | 169 | // Perform the query |
| 170 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 170 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 171 | 171 | |
| 172 | 172 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 173 | - if ( 0 < $meta_instances ) { |
|
| 173 | + if (0 < $meta_instances) { |
|
| 174 | 174 | return true; |
| 175 | 175 | } |
| 176 | 176 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return true if the uri internal to the current dataset otherwise false. |
| 189 | 189 | */ |
| 190 | - public function is_internal_uri( $uri ) { |
|
| 190 | + public function is_internal_uri($uri) { |
|
| 191 | 191 | |
| 192 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 192 | + return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri())); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
@@ -201,10 +201,10 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 203 | 203 | */ |
| 204 | - public function get_entity_post_by_uri( $uri ) { |
|
| 204 | + public function get_entity_post_by_uri($uri) { |
|
| 205 | 205 | |
| 206 | 206 | // Check if we've been provided with a value otherwise return null. |
| 207 | - if ( empty( $uri ) ) { |
|
| 207 | + if (empty($uri)) { |
|
| 208 | 208 | return null; |
| 209 | 209 | } |
| 210 | 210 | |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | // performed also looking at sameAs values. |
| 226 | 226 | // |
| 227 | 227 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 228 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 228 | + if ( ! $this->is_internal_uri($uri)) { |
|
| 229 | 229 | |
| 230 | 230 | $query_args['meta_query']['relation'] = 'OR'; |
| 231 | 231 | $query_args['meta_query'][] = array( |
@@ -235,13 +235,13 @@ discard block |
||
| 235 | 235 | ); |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - $query = new WP_Query( $query_args ); |
|
| 238 | + $query = new WP_Query($query_args); |
|
| 239 | 239 | |
| 240 | 240 | // Get the matching entity posts. |
| 241 | 241 | $posts = $query->get_posts(); |
| 242 | 242 | |
| 243 | 243 | // Return null if no post is found. |
| 244 | - if ( 0 === count( $posts ) ) { |
|
| 244 | + if (0 === count($posts)) { |
|
| 245 | 245 | return null; |
| 246 | 246 | } |
| 247 | 247 | |
@@ -261,11 +261,11 @@ discard block |
||
| 261 | 261 | * @param WP_Post $post Post object. |
| 262 | 262 | * @param bool $update Whether this is an existing post being updated or not. |
| 263 | 263 | */ |
| 264 | - public function save_post( $post_id, $post, $update ) { |
|
| 264 | + public function save_post($post_id, $post, $update) { |
|
| 265 | 265 | |
| 266 | 266 | // Avoid doing anything if post is autosave or a revision. |
| 267 | 267 | |
| 268 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 268 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 269 | 269 | return; |
| 270 | 270 | } |
| 271 | 271 | |
@@ -275,15 +275,15 @@ discard block |
||
| 275 | 275 | // the $post_id in the save_post call matches the post id set in the request. |
| 276 | 276 | // |
| 277 | 277 | // If this is not the current post being saved or if it's not an entity, return. |
| 278 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 278 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 279 | 279 | return; |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | // Get the alt labels from the request (or empty array). |
| 283 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 283 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 284 | 284 | |
| 285 | 285 | // Set the alternative labels. |
| 286 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 286 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 287 | 287 | |
| 288 | 288 | } |
| 289 | 289 | |
@@ -295,22 +295,22 @@ discard block |
||
| 295 | 295 | * @param int $post_id The post id. |
| 296 | 296 | * @param array $alt_labels An array of labels. |
| 297 | 297 | */ |
| 298 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 298 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 299 | 299 | |
| 300 | 300 | // Force $alt_labels to be an array |
| 301 | - if ( ! is_array( $alt_labels ) ) { |
|
| 302 | - $alt_labels = array( $alt_labels ); |
|
| 301 | + if ( ! is_array($alt_labels)) { |
|
| 302 | + $alt_labels = array($alt_labels); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 305 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 306 | 306 | |
| 307 | 307 | // Delete all the existing alternate labels. |
| 308 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 308 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 309 | 309 | |
| 310 | 310 | // Set the alternative labels. |
| 311 | - foreach ( $alt_labels as $alt_label ) { |
|
| 312 | - if ( ! empty( $alt_label ) ) { |
|
| 313 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 311 | + foreach ($alt_labels as $alt_label) { |
|
| 312 | + if ( ! empty($alt_label)) { |
|
| 313 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 314 | 314 | } |
| 315 | 315 | } |
| 316 | 316 | |
@@ -325,9 +325,9 @@ discard block |
||
| 325 | 325 | * |
| 326 | 326 | * @return mixed An array of alternative labels. |
| 327 | 327 | */ |
| 328 | - public function get_alternative_labels( $post_id ) { |
|
| 328 | + public function get_alternative_labels($post_id) { |
|
| 329 | 329 | |
| 330 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 330 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | /** |
@@ -339,9 +339,9 @@ discard block |
||
| 339 | 339 | * |
| 340 | 340 | * @return array An array with the entity title and labels. |
| 341 | 341 | */ |
| 342 | - public function get_labels( $post_id ) { |
|
| 342 | + public function get_labels($post_id) { |
|
| 343 | 343 | |
| 344 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 344 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | /** |
@@ -351,25 +351,25 @@ discard block |
||
| 351 | 351 | * |
| 352 | 352 | * @param WP_Post $post Post object. |
| 353 | 353 | */ |
| 354 | - public function edit_form_before_permalink( $post ) { |
|
| 354 | + public function edit_form_before_permalink($post) { |
|
| 355 | 355 | |
| 356 | 356 | // If it's not an entity, return. |
| 357 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 357 | + if ( ! $this->is_entity($post->ID)) { |
|
| 358 | 358 | return; |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | // Print the input template. |
| 362 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 362 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 363 | 363 | |
| 364 | 364 | // Print all the currently set alternative labels. |
| 365 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 365 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 366 | 366 | |
| 367 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 367 | + echo $this->get_alternative_label_input($alt_label); |
|
| 368 | 368 | |
| 369 | 369 | }; |
| 370 | 370 | |
| 371 | 371 | // Print the button. |
| 372 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 372 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 373 | 373 | |
| 374 | 374 | } |
| 375 | 375 | |
@@ -382,25 +382,25 @@ discard block |
||
| 382 | 382 | * |
| 383 | 383 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 384 | 384 | */ |
| 385 | - public function get_uri( $post_id ) { |
|
| 385 | + public function get_uri($post_id) { |
|
| 386 | 386 | |
| 387 | 387 | // If a null is given, nothing to do |
| 388 | - if ( null == $post_id ) { |
|
| 388 | + if (null == $post_id) { |
|
| 389 | 389 | return null; |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 392 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 393 | 393 | |
| 394 | 394 | // If the dataset uri is not properly configured, null is returned |
| 395 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 395 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 396 | 396 | return null; |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | // Set the URI if it isn't set yet. |
| 400 | - $post_status = get_post_status( $post_id ); |
|
| 401 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 402 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 403 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 400 | + $post_status = get_post_status($post_id); |
|
| 401 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 402 | + $uri = wl_build_entity_uri($post_id); |
|
| 403 | + wl_set_entity_uri($post_id, $uri); |
|
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | return $uri; |
@@ -416,9 +416,9 @@ discard block |
||
| 416 | 416 | * |
| 417 | 417 | * @return string The input HTML code. |
| 418 | 418 | */ |
| 419 | - private function get_alternative_label_input( $value = '' ) { |
|
| 419 | + private function get_alternative_label_input($value = '') { |
|
| 420 | 420 | |
| 421 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 421 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | /** |
@@ -430,7 +430,7 @@ discard block |
||
| 430 | 430 | */ |
| 431 | 431 | public function count() { |
| 432 | 432 | |
| 433 | - $count = wp_count_posts( self::TYPE_NAME ); |
|
| 433 | + $count = wp_count_posts(self::TYPE_NAME); |
|
| 434 | 434 | |
| 435 | 435 | return $count->publish; |
| 436 | 436 | } |
@@ -447,28 +447,28 @@ discard block |
||
| 447 | 447 | * |
| 448 | 448 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 449 | 449 | */ |
| 450 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 450 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 451 | 451 | |
| 452 | 452 | // Create an entity for the publisher. |
| 453 | - $post_id = wp_insert_post( array( |
|
| 453 | + $post_id = wp_insert_post(array( |
|
| 454 | 454 | 'post_type' => self::TYPE_NAME, |
| 455 | 455 | 'post_title' => $name, |
| 456 | 456 | 'post_status' => $status, |
| 457 | 457 | 'post_content' => '', |
| 458 | - ) ); |
|
| 458 | + )); |
|
| 459 | 459 | |
| 460 | 460 | // Return the error if any. |
| 461 | - if ( is_wp_error( $post_id ) ) { |
|
| 461 | + if (is_wp_error($post_id)) { |
|
| 462 | 462 | return $post_id; |
| 463 | 463 | } |
| 464 | 464 | |
| 465 | 465 | // Set the entity logo. |
| 466 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 467 | - set_post_thumbnail( $post_id, $logo ); |
|
| 466 | + if ($logo && is_numeric($logo)) { |
|
| 467 | + set_post_thumbnail($post_id, $logo); |
|
| 468 | 468 | } |
| 469 | 469 | |
| 470 | 470 | // Set the entity type. |
| 471 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 471 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 472 | 472 | |
| 473 | 473 | return $post_id; |
| 474 | 474 | } |
@@ -484,9 +484,9 @@ discard block |
||
| 484 | 484 | * |
| 485 | 485 | * @return array An array of post ids. |
| 486 | 486 | */ |
| 487 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 487 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 488 | 488 | |
| 489 | - return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 489 | + return wl_core_inner_get_related_entities('post_ids', $id, null, $post_status); |
|
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | } |