@@ -18,302 +18,302 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Content_Filter_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The pattern to find entities in text. |
|
| 23 | - * |
|
| 24 | - * @since 3.8.0 |
|
| 25 | - */ |
|
| 26 | - const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.8.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.13.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $configuration_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The `link by default` setting. |
|
| 48 | - * |
|
| 49 | - * @since 3.13.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var bool True if link by default is enabled otherwise false. |
|
| 52 | - */ |
|
| 53 | - private $is_link_by_default; |
|
| 54 | - |
|
| 55 | - private $entity_post_ids_linked_from_post_content = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 59 | - * |
|
| 60 | - * @since 3.16.3 |
|
| 61 | - * @access private |
|
| 62 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 63 | - */ |
|
| 64 | - private $entity_uri_service; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * A {@link Wordlift_Log_Service} instance. |
|
| 68 | - * |
|
| 69 | - * @since 3.16.0 |
|
| 70 | - * |
|
| 71 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 72 | - */ |
|
| 73 | - private $log; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 77 | - * |
|
| 78 | - * @since 3.14.2 |
|
| 79 | - * @access private |
|
| 80 | - * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 81 | - */ |
|
| 82 | - private static $instance; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 86 | - * |
|
| 87 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 88 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 89 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 90 | - * |
|
| 91 | - * @since 3.8.0 |
|
| 92 | - * |
|
| 93 | - */ |
|
| 94 | - public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 95 | - |
|
| 96 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 97 | - |
|
| 98 | - $this->entity_service = $entity_service; |
|
| 99 | - $this->configuration_service = $configuration_service; |
|
| 100 | - $this->entity_uri_service = $entity_uri_service; |
|
| 101 | - |
|
| 102 | - self::$instance = $this; |
|
| 103 | - |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 108 | - * |
|
| 109 | - * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 110 | - * @since 3.14.2 |
|
| 111 | - */ |
|
| 112 | - public static function get_instance() { |
|
| 113 | - |
|
| 114 | - return self::$instance; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Mangle the content by adding links to the entity pages. This function is |
|
| 119 | - * hooked to the 'the_content' WP's filter. |
|
| 120 | - * |
|
| 121 | - * @param string $content The content being filtered. |
|
| 122 | - * |
|
| 123 | - * @return string The filtered content. |
|
| 124 | - * @since 3.8.0 |
|
| 125 | - * |
|
| 126 | - */ |
|
| 127 | - public function the_content( $content ) { |
|
| 128 | - |
|
| 129 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 130 | - |
|
| 131 | - // Links should be added only on the front end and not for RSS. |
|
| 132 | - if ( is_feed() ) { |
|
| 133 | - return $content; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Preload the `link by default` setting. |
|
| 137 | - $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 138 | - |
|
| 139 | - // Reset the array of of entity post ids linked from the post content. |
|
| 140 | - // This is used to avoid linking more the once the same post. |
|
| 141 | - $this->entity_post_ids_linked_from_post_content = array(); |
|
| 142 | - |
|
| 143 | - // Preload URIs. |
|
| 144 | - $matches = array(); |
|
| 145 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 146 | - |
|
| 147 | - // Bail out if there are no URIs. |
|
| 148 | - if ( empty( $matches[3] ) ) { |
|
| 149 | - return $content; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Preload the URIs. |
|
| 153 | - $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 154 | - |
|
| 155 | - // Replace each match of the entity tag with the entity link. If an error |
|
| 156 | - // occurs fail silently returning the original content. |
|
| 157 | - $result = preg_replace_callback( self::PATTERN, array( |
|
| 158 | - $this, |
|
| 159 | - 'link', |
|
| 160 | - ), $content ) ?: $content; |
|
| 161 | - |
|
| 162 | - $this->entity_uri_service->reset_uris(); |
|
| 163 | - |
|
| 164 | - return $result; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get the entity match and replace it with a page link. |
|
| 169 | - * |
|
| 170 | - * @param array $matches An array of matches. |
|
| 171 | - * |
|
| 172 | - * @return string The replaced text with the link to the entity page. |
|
| 173 | - * @since 3.8.0 |
|
| 174 | - * |
|
| 175 | - */ |
|
| 176 | - private function link( $matches ) { |
|
| 177 | - |
|
| 178 | - // Get the entity itemid URI and label. |
|
| 179 | - $css_class = $matches[2]; |
|
| 180 | - $uri = $matches[3]; |
|
| 181 | - $label = $matches[4]; |
|
| 182 | - |
|
| 183 | - // Get the entity post by URI. |
|
| 184 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 185 | - |
|
| 186 | - // @todo: revise the `test-content-filter-service.php` before switching |
|
| 187 | - // to the `entity_uri_service`. This is required, because the test injects |
|
| 188 | - // itself as `entity_service` to mock the requests to get a post by |
|
| 189 | - // entity uri. |
|
| 190 | - // |
|
| 191 | - // $post = $this->entity_uri_service->get_entity( $uri ); |
|
| 192 | - |
|
| 193 | - if ( null === $post ) { |
|
| 194 | - |
|
| 195 | - // If the entity post is not found return the label w/o the markup |
|
| 196 | - // around it. |
|
| 197 | - // |
|
| 198 | - // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 199 | - return $label; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ) |
|
| 203 | - // Do not link if already linked. |
|
| 204 | - || in_array( $post->ID, $this->entity_post_ids_linked_from_post_content ); |
|
| 205 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 206 | - |
|
| 207 | - // Don't link if links are disabled and the entity is not link or the |
|
| 208 | - // entity is do not link. |
|
| 209 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 210 | - |
|
| 211 | - // Return the label if it's don't link. |
|
| 212 | - if ( $dont_link ) { |
|
| 213 | - return $label; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - // Add the entity post id to the array of already linked entities, so that |
|
| 217 | - // only the first entity occurrence is linked. |
|
| 218 | - $this->entity_post_ids_linked_from_post_content[] = $post->ID; |
|
| 219 | - |
|
| 220 | - // Get the link. |
|
| 221 | - $href = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 222 | - |
|
| 223 | - // Bail out if the `$href` has been reset. |
|
| 224 | - if ( empty( $href ) ) { |
|
| 225 | - return $label; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // Get an alternative title attribute. |
|
| 229 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 230 | - |
|
| 231 | - // Return the link. |
|
| 232 | - return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Get a `title` attribute with an alternative label for the link. |
|
| 237 | - * |
|
| 238 | - * If an alternative title isn't available an empty string is returned. |
|
| 239 | - * |
|
| 240 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 241 | - * @param string $label The main link label. |
|
| 242 | - * |
|
| 243 | - * @return string A `title` attribute with an alternative label or an empty |
|
| 244 | - * string if none available. |
|
| 245 | - * @since 3.15.0 |
|
| 246 | - * |
|
| 247 | - */ |
|
| 248 | - private function get_title_attribute( $post_id, $label ) { |
|
| 249 | - |
|
| 250 | - // Get an alternative title. |
|
| 251 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 252 | - if ( ! empty( $title ) ) { |
|
| 253 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - return ''; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Get a string to be used as a title attribute in links to a post |
|
| 261 | - * |
|
| 262 | - * @param int $post_id The post id of the post being linked. |
|
| 263 | - * @param string $ignore_label A label to ignore. |
|
| 264 | - * |
|
| 265 | - * @return string The title to be used in the link. An empty string when |
|
| 266 | - * there is no alternative that is not the $ignore_label. |
|
| 267 | - * @since 3.15.0 |
|
| 268 | - * |
|
| 269 | - */ |
|
| 270 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 271 | - |
|
| 272 | - // Get possible alternative labels we can select from. |
|
| 273 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 274 | - |
|
| 275 | - /* |
|
| 21 | + /** |
|
| 22 | + * The pattern to find entities in text. |
|
| 23 | + * |
|
| 24 | + * @since 3.8.0 |
|
| 25 | + */ |
|
| 26 | + const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.8.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.13.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $configuration_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The `link by default` setting. |
|
| 48 | + * |
|
| 49 | + * @since 3.13.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var bool True if link by default is enabled otherwise false. |
|
| 52 | + */ |
|
| 53 | + private $is_link_by_default; |
|
| 54 | + |
|
| 55 | + private $entity_post_ids_linked_from_post_content = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 59 | + * |
|
| 60 | + * @since 3.16.3 |
|
| 61 | + * @access private |
|
| 62 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 63 | + */ |
|
| 64 | + private $entity_uri_service; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * A {@link Wordlift_Log_Service} instance. |
|
| 68 | + * |
|
| 69 | + * @since 3.16.0 |
|
| 70 | + * |
|
| 71 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 72 | + */ |
|
| 73 | + private $log; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 77 | + * |
|
| 78 | + * @since 3.14.2 |
|
| 79 | + * @access private |
|
| 80 | + * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 81 | + */ |
|
| 82 | + private static $instance; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 86 | + * |
|
| 87 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 88 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 89 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 90 | + * |
|
| 91 | + * @since 3.8.0 |
|
| 92 | + * |
|
| 93 | + */ |
|
| 94 | + public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 95 | + |
|
| 96 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 97 | + |
|
| 98 | + $this->entity_service = $entity_service; |
|
| 99 | + $this->configuration_service = $configuration_service; |
|
| 100 | + $this->entity_uri_service = $entity_uri_service; |
|
| 101 | + |
|
| 102 | + self::$instance = $this; |
|
| 103 | + |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 108 | + * |
|
| 109 | + * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 110 | + * @since 3.14.2 |
|
| 111 | + */ |
|
| 112 | + public static function get_instance() { |
|
| 113 | + |
|
| 114 | + return self::$instance; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Mangle the content by adding links to the entity pages. This function is |
|
| 119 | + * hooked to the 'the_content' WP's filter. |
|
| 120 | + * |
|
| 121 | + * @param string $content The content being filtered. |
|
| 122 | + * |
|
| 123 | + * @return string The filtered content. |
|
| 124 | + * @since 3.8.0 |
|
| 125 | + * |
|
| 126 | + */ |
|
| 127 | + public function the_content( $content ) { |
|
| 128 | + |
|
| 129 | + $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 130 | + |
|
| 131 | + // Links should be added only on the front end and not for RSS. |
|
| 132 | + if ( is_feed() ) { |
|
| 133 | + return $content; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Preload the `link by default` setting. |
|
| 137 | + $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 138 | + |
|
| 139 | + // Reset the array of of entity post ids linked from the post content. |
|
| 140 | + // This is used to avoid linking more the once the same post. |
|
| 141 | + $this->entity_post_ids_linked_from_post_content = array(); |
|
| 142 | + |
|
| 143 | + // Preload URIs. |
|
| 144 | + $matches = array(); |
|
| 145 | + preg_match_all( self::PATTERN, $content, $matches ); |
|
| 146 | + |
|
| 147 | + // Bail out if there are no URIs. |
|
| 148 | + if ( empty( $matches[3] ) ) { |
|
| 149 | + return $content; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Preload the URIs. |
|
| 153 | + $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 154 | + |
|
| 155 | + // Replace each match of the entity tag with the entity link. If an error |
|
| 156 | + // occurs fail silently returning the original content. |
|
| 157 | + $result = preg_replace_callback( self::PATTERN, array( |
|
| 158 | + $this, |
|
| 159 | + 'link', |
|
| 160 | + ), $content ) ?: $content; |
|
| 161 | + |
|
| 162 | + $this->entity_uri_service->reset_uris(); |
|
| 163 | + |
|
| 164 | + return $result; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get the entity match and replace it with a page link. |
|
| 169 | + * |
|
| 170 | + * @param array $matches An array of matches. |
|
| 171 | + * |
|
| 172 | + * @return string The replaced text with the link to the entity page. |
|
| 173 | + * @since 3.8.0 |
|
| 174 | + * |
|
| 175 | + */ |
|
| 176 | + private function link( $matches ) { |
|
| 177 | + |
|
| 178 | + // Get the entity itemid URI and label. |
|
| 179 | + $css_class = $matches[2]; |
|
| 180 | + $uri = $matches[3]; |
|
| 181 | + $label = $matches[4]; |
|
| 182 | + |
|
| 183 | + // Get the entity post by URI. |
|
| 184 | + $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 185 | + |
|
| 186 | + // @todo: revise the `test-content-filter-service.php` before switching |
|
| 187 | + // to the `entity_uri_service`. This is required, because the test injects |
|
| 188 | + // itself as `entity_service` to mock the requests to get a post by |
|
| 189 | + // entity uri. |
|
| 190 | + // |
|
| 191 | + // $post = $this->entity_uri_service->get_entity( $uri ); |
|
| 192 | + |
|
| 193 | + if ( null === $post ) { |
|
| 194 | + |
|
| 195 | + // If the entity post is not found return the label w/o the markup |
|
| 196 | + // around it. |
|
| 197 | + // |
|
| 198 | + // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 199 | + return $label; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $no_link = - 1 < strpos( $css_class, 'wl-no-link' ) |
|
| 203 | + // Do not link if already linked. |
|
| 204 | + || in_array( $post->ID, $this->entity_post_ids_linked_from_post_content ); |
|
| 205 | + $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 206 | + |
|
| 207 | + // Don't link if links are disabled and the entity is not link or the |
|
| 208 | + // entity is do not link. |
|
| 209 | + $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 210 | + |
|
| 211 | + // Return the label if it's don't link. |
|
| 212 | + if ( $dont_link ) { |
|
| 213 | + return $label; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + // Add the entity post id to the array of already linked entities, so that |
|
| 217 | + // only the first entity occurrence is linked. |
|
| 218 | + $this->entity_post_ids_linked_from_post_content[] = $post->ID; |
|
| 219 | + |
|
| 220 | + // Get the link. |
|
| 221 | + $href = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 222 | + |
|
| 223 | + // Bail out if the `$href` has been reset. |
|
| 224 | + if ( empty( $href ) ) { |
|
| 225 | + return $label; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + // Get an alternative title attribute. |
|
| 229 | + $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 230 | + |
|
| 231 | + // Return the link. |
|
| 232 | + return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Get a `title` attribute with an alternative label for the link. |
|
| 237 | + * |
|
| 238 | + * If an alternative title isn't available an empty string is returned. |
|
| 239 | + * |
|
| 240 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 241 | + * @param string $label The main link label. |
|
| 242 | + * |
|
| 243 | + * @return string A `title` attribute with an alternative label or an empty |
|
| 244 | + * string if none available. |
|
| 245 | + * @since 3.15.0 |
|
| 246 | + * |
|
| 247 | + */ |
|
| 248 | + private function get_title_attribute( $post_id, $label ) { |
|
| 249 | + |
|
| 250 | + // Get an alternative title. |
|
| 251 | + $title = $this->get_link_title( $post_id, $label ); |
|
| 252 | + if ( ! empty( $title ) ) { |
|
| 253 | + return 'title="' . esc_attr( $title ) . '"'; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + return ''; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Get a string to be used as a title attribute in links to a post |
|
| 261 | + * |
|
| 262 | + * @param int $post_id The post id of the post being linked. |
|
| 263 | + * @param string $ignore_label A label to ignore. |
|
| 264 | + * |
|
| 265 | + * @return string The title to be used in the link. An empty string when |
|
| 266 | + * there is no alternative that is not the $ignore_label. |
|
| 267 | + * @since 3.15.0 |
|
| 268 | + * |
|
| 269 | + */ |
|
| 270 | + function get_link_title( $post_id, $ignore_label ) { |
|
| 271 | + |
|
| 272 | + // Get possible alternative labels we can select from. |
|
| 273 | + $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 274 | + |
|
| 275 | + /* |
|
| 276 | 276 | * Since the original text might use an alternative label than the |
| 277 | 277 | * Entity title, add the title itself which is not returned by the api. |
| 278 | 278 | */ |
| 279 | - $labels[] = get_the_title( $post_id ); |
|
| 280 | - |
|
| 281 | - // Add some randomness to the label selection. |
|
| 282 | - shuffle( $labels ); |
|
| 283 | - |
|
| 284 | - // Select the first label which is not to be ignored. |
|
| 285 | - $title = ''; |
|
| 286 | - foreach ( $labels as $label ) { |
|
| 287 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 288 | - $title = $label; |
|
| 289 | - break; |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - return $title; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 298 | - * the provided content. |
|
| 299 | - * |
|
| 300 | - * @param string $content The content. |
|
| 301 | - * |
|
| 302 | - * @return array An array of URIs. |
|
| 303 | - * @since 3.14.2 |
|
| 304 | - * |
|
| 305 | - */ |
|
| 306 | - public function get_entity_uris( $content ) { |
|
| 307 | - |
|
| 308 | - $matches = array(); |
|
| 309 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 310 | - |
|
| 311 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 312 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
| 313 | - // is not sequential. |
|
| 314 | - // |
|
| 315 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 316 | - return array_values( array_unique( $matches[3] ) ); |
|
| 317 | - } |
|
| 279 | + $labels[] = get_the_title( $post_id ); |
|
| 280 | + |
|
| 281 | + // Add some randomness to the label selection. |
|
| 282 | + shuffle( $labels ); |
|
| 283 | + |
|
| 284 | + // Select the first label which is not to be ignored. |
|
| 285 | + $title = ''; |
|
| 286 | + foreach ( $labels as $label ) { |
|
| 287 | + if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 288 | + $title = $label; |
|
| 289 | + break; |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + return $title; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 298 | + * the provided content. |
|
| 299 | + * |
|
| 300 | + * @param string $content The content. |
|
| 301 | + * |
|
| 302 | + * @return array An array of URIs. |
|
| 303 | + * @since 3.14.2 |
|
| 304 | + * |
|
| 305 | + */ |
|
| 306 | + public function get_entity_uris( $content ) { |
|
| 307 | + |
|
| 308 | + $matches = array(); |
|
| 309 | + preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 310 | + |
|
| 311 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 312 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
| 313 | + // is not sequential. |
|
| 314 | + // |
|
| 315 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 316 | + return array_values( array_unique( $matches[3] ) ); |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | 319 | } |
@@ -91,9 +91,9 @@ discard block |
||
| 91 | 91 | * @since 3.8.0 |
| 92 | 92 | * |
| 93 | 93 | */ |
| 94 | - public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 94 | + public function __construct($entity_service, $configuration_service, $entity_uri_service) { |
|
| 95 | 95 | |
| 96 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 96 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 97 | 97 | |
| 98 | 98 | $this->entity_service = $entity_service; |
| 99 | 99 | $this->configuration_service = $configuration_service; |
@@ -124,12 +124,12 @@ discard block |
||
| 124 | 124 | * @since 3.8.0 |
| 125 | 125 | * |
| 126 | 126 | */ |
| 127 | - public function the_content( $content ) { |
|
| 127 | + public function the_content($content) { |
|
| 128 | 128 | |
| 129 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 129 | + $this->log->trace("Filtering content [ ".(is_singular() ? 'yes' : 'no')." ]..."); |
|
| 130 | 130 | |
| 131 | 131 | // Links should be added only on the front end and not for RSS. |
| 132 | - if ( is_feed() ) { |
|
| 132 | + if (is_feed()) { |
|
| 133 | 133 | return $content; |
| 134 | 134 | } |
| 135 | 135 | |
@@ -142,22 +142,22 @@ discard block |
||
| 142 | 142 | |
| 143 | 143 | // Preload URIs. |
| 144 | 144 | $matches = array(); |
| 145 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 145 | + preg_match_all(self::PATTERN, $content, $matches); |
|
| 146 | 146 | |
| 147 | 147 | // Bail out if there are no URIs. |
| 148 | - if ( empty( $matches[3] ) ) { |
|
| 148 | + if (empty($matches[3])) { |
|
| 149 | 149 | return $content; |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // Preload the URIs. |
| 153 | - $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 153 | + $this->entity_uri_service->preload_uris($matches[3]); |
|
| 154 | 154 | |
| 155 | 155 | // Replace each match of the entity tag with the entity link. If an error |
| 156 | 156 | // occurs fail silently returning the original content. |
| 157 | - $result = preg_replace_callback( self::PATTERN, array( |
|
| 157 | + $result = preg_replace_callback(self::PATTERN, array( |
|
| 158 | 158 | $this, |
| 159 | 159 | 'link', |
| 160 | - ), $content ) ?: $content; |
|
| 160 | + ), $content) ?: $content; |
|
| 161 | 161 | |
| 162 | 162 | $this->entity_uri_service->reset_uris(); |
| 163 | 163 | |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | * @since 3.8.0 |
| 174 | 174 | * |
| 175 | 175 | */ |
| 176 | - private function link( $matches ) { |
|
| 176 | + private function link($matches) { |
|
| 177 | 177 | |
| 178 | 178 | // Get the entity itemid URI and label. |
| 179 | 179 | $css_class = $matches[2]; |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | $label = $matches[4]; |
| 182 | 182 | |
| 183 | 183 | // Get the entity post by URI. |
| 184 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 184 | + $post = $this->entity_service->get_entity_post_by_uri($uri); |
|
| 185 | 185 | |
| 186 | 186 | // @todo: revise the `test-content-filter-service.php` before switching |
| 187 | 187 | // to the `entity_uri_service`. This is required, because the test injects |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | // |
| 191 | 191 | // $post = $this->entity_uri_service->get_entity( $uri ); |
| 192 | 192 | |
| 193 | - if ( null === $post ) { |
|
| 193 | + if (null === $post) { |
|
| 194 | 194 | |
| 195 | 195 | // If the entity post is not found return the label w/o the markup |
| 196 | 196 | // around it. |
@@ -199,17 +199,17 @@ discard block |
||
| 199 | 199 | return $label; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ) |
|
| 202 | + $no_link = - 1 < strpos($css_class, 'wl-no-link') |
|
| 203 | 203 | // Do not link if already linked. |
| 204 | - || in_array( $post->ID, $this->entity_post_ids_linked_from_post_content ); |
|
| 205 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 204 | + || in_array($post->ID, $this->entity_post_ids_linked_from_post_content); |
|
| 205 | + $link = - 1 < strpos($css_class, 'wl-link'); |
|
| 206 | 206 | |
| 207 | 207 | // Don't link if links are disabled and the entity is not link or the |
| 208 | 208 | // entity is do not link. |
| 209 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 209 | + $dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link; |
|
| 210 | 210 | |
| 211 | 211 | // Return the label if it's don't link. |
| 212 | - if ( $dont_link ) { |
|
| 212 | + if ($dont_link) { |
|
| 213 | 213 | return $label; |
| 214 | 214 | } |
| 215 | 215 | |
@@ -218,15 +218,15 @@ discard block |
||
| 218 | 218 | $this->entity_post_ids_linked_from_post_content[] = $post->ID; |
| 219 | 219 | |
| 220 | 220 | // Get the link. |
| 221 | - $href = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 221 | + $href = Wordlift_Post_Adapter::get_production_permalink($post->ID); |
|
| 222 | 222 | |
| 223 | 223 | // Bail out if the `$href` has been reset. |
| 224 | - if ( empty( $href ) ) { |
|
| 224 | + if (empty($href)) { |
|
| 225 | 225 | return $label; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | // Get an alternative title attribute. |
| 229 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 229 | + $title_attribute = $this->get_title_attribute($post->ID, $label); |
|
| 230 | 230 | |
| 231 | 231 | // Return the link. |
| 232 | 232 | return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
@@ -245,12 +245,12 @@ discard block |
||
| 245 | 245 | * @since 3.15.0 |
| 246 | 246 | * |
| 247 | 247 | */ |
| 248 | - private function get_title_attribute( $post_id, $label ) { |
|
| 248 | + private function get_title_attribute($post_id, $label) { |
|
| 249 | 249 | |
| 250 | 250 | // Get an alternative title. |
| 251 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 252 | - if ( ! empty( $title ) ) { |
|
| 253 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 251 | + $title = $this->get_link_title($post_id, $label); |
|
| 252 | + if ( ! empty($title)) { |
|
| 253 | + return 'title="'.esc_attr($title).'"'; |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | return ''; |
@@ -267,24 +267,24 @@ discard block |
||
| 267 | 267 | * @since 3.15.0 |
| 268 | 268 | * |
| 269 | 269 | */ |
| 270 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 270 | + function get_link_title($post_id, $ignore_label) { |
|
| 271 | 271 | |
| 272 | 272 | // Get possible alternative labels we can select from. |
| 273 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 273 | + $labels = $this->entity_service->get_alternative_labels($post_id); |
|
| 274 | 274 | |
| 275 | 275 | /* |
| 276 | 276 | * Since the original text might use an alternative label than the |
| 277 | 277 | * Entity title, add the title itself which is not returned by the api. |
| 278 | 278 | */ |
| 279 | - $labels[] = get_the_title( $post_id ); |
|
| 279 | + $labels[] = get_the_title($post_id); |
|
| 280 | 280 | |
| 281 | 281 | // Add some randomness to the label selection. |
| 282 | - shuffle( $labels ); |
|
| 282 | + shuffle($labels); |
|
| 283 | 283 | |
| 284 | 284 | // Select the first label which is not to be ignored. |
| 285 | 285 | $title = ''; |
| 286 | - foreach ( $labels as $label ) { |
|
| 287 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 286 | + foreach ($labels as $label) { |
|
| 287 | + if (0 !== strcasecmp($label, $ignore_label)) { |
|
| 288 | 288 | $title = $label; |
| 289 | 289 | break; |
| 290 | 290 | } |
@@ -303,17 +303,17 @@ discard block |
||
| 303 | 303 | * @since 3.14.2 |
| 304 | 304 | * |
| 305 | 305 | */ |
| 306 | - public function get_entity_uris( $content ) { |
|
| 306 | + public function get_entity_uris($content) { |
|
| 307 | 307 | |
| 308 | 308 | $matches = array(); |
| 309 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 309 | + preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches); |
|
| 310 | 310 | |
| 311 | 311 | // We need to use `array_values` here in order to avoid further `json_encode` |
| 312 | 312 | // to turn it into an object (since if the 3rd match isn't found the index |
| 313 | 313 | // is not sequential. |
| 314 | 314 | // |
| 315 | 315 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
| 316 | - return array_values( array_unique( $matches[3] ) ); |
|
| 316 | + return array_values(array_unique($matches[3])); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | } |
@@ -16,131 +16,131 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Push_Reference_Data_Command { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.18.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 25 | - */ |
|
| 26 | - private $relation_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.18.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Sparql_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.18.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $sparql_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.18.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 52 | - */ |
|
| 53 | - private $configuration_service; |
|
| 54 | - /** |
|
| 55 | - * @var Wordlift_Entity_Type_Service |
|
| 56 | - */ |
|
| 57 | - private $entity_type_service; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Wordlift_Push_Reference_Data_Command constructor. |
|
| 61 | - * |
|
| 62 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 63 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 64 | - * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 65 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 66 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 67 | - * |
|
| 68 | - * @since 3.18.0 |
|
| 69 | - * |
|
| 70 | - */ |
|
| 71 | - public function __construct( $relation_service, $entity_service, $sparql_service, $configuration_service, $entity_type_service ) { |
|
| 72 | - |
|
| 73 | - $this->relation_service = $relation_service; |
|
| 74 | - $this->entity_service = $entity_service; |
|
| 75 | - $this->sparql_service = $sparql_service; |
|
| 76 | - $this->configuration_service = $configuration_service; |
|
| 77 | - $this->entity_type_service = $entity_type_service; |
|
| 78 | - |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function __invoke( $args ) { |
|
| 82 | - |
|
| 83 | - $relations = $this->relation_service->find_all_grouped_by_subject_id(); |
|
| 84 | - $progress_bar = \WP_CLI\Utils\make_progress_bar( 'Processing...', count( $relations ) ); |
|
| 85 | - |
|
| 86 | - foreach ( $relations as $relation ) { |
|
| 87 | - $progress_bar->tick(); |
|
| 88 | - |
|
| 89 | - // Get the post. |
|
| 90 | - $post = get_post( $relation->subject_id ); |
|
| 91 | - |
|
| 92 | - // Bail out if the post isn't found. |
|
| 93 | - if ( null == $post ) { |
|
| 94 | - continue; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Bail out if it's an entity: we're only interested in articles |
|
| 98 | - // *referencing* entities. |
|
| 99 | - if ( $this->entity_service->is_entity( $post->ID ) ) { |
|
| 100 | - continue; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // Get the article URI. |
|
| 104 | - $uri = $this->entity_service->get_uri( $post->ID ); |
|
| 105 | - |
|
| 106 | - // Prepare the DELETE query to delete existing data. |
|
| 107 | - $query = self::get_delete_query( $uri ) |
|
| 108 | - . $this->get_insert_query( $post, $uri, explode( ',', $relation->object_ids ) ); |
|
| 109 | - |
|
| 110 | - $this->sparql_service->execute( $query, false ); |
|
| 111 | - |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $progress_bar->finish(); |
|
| 115 | - |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - private static function get_delete_query( $uri ) { |
|
| 119 | - |
|
| 120 | - return Wordlift_Query_Builder |
|
| 121 | - ::new_instance() |
|
| 122 | - ->delete()->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, '?o' ) |
|
| 123 | - ->build() |
|
| 124 | - . Wordlift_Query_Builder |
|
| 125 | - ::new_instance() |
|
| 126 | - ->delete()->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 127 | - ->build() |
|
| 128 | - . Wordlift_Query_Builder |
|
| 129 | - ::new_instance() |
|
| 130 | - ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, '?o' ) |
|
| 131 | - ->build() |
|
| 132 | - . Wordlift_Query_Builder |
|
| 133 | - ::new_instance() |
|
| 134 | - ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 135 | - ->build(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - private function get_insert_query( $post, $uri, $object_ids ) { |
|
| 139 | - |
|
| 140 | - $language_code = $this->configuration_service->get_language_code(); |
|
| 141 | - $type = $this->entity_type_service->get( $post->ID ); |
|
| 142 | - |
|
| 143 | - /* |
|
| 19 | + /** |
|
| 20 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.18.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 25 | + */ |
|
| 26 | + private $relation_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.18.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Sparql_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.18.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $sparql_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.18.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 52 | + */ |
|
| 53 | + private $configuration_service; |
|
| 54 | + /** |
|
| 55 | + * @var Wordlift_Entity_Type_Service |
|
| 56 | + */ |
|
| 57 | + private $entity_type_service; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Wordlift_Push_Reference_Data_Command constructor. |
|
| 61 | + * |
|
| 62 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 63 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 64 | + * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 65 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 66 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 67 | + * |
|
| 68 | + * @since 3.18.0 |
|
| 69 | + * |
|
| 70 | + */ |
|
| 71 | + public function __construct( $relation_service, $entity_service, $sparql_service, $configuration_service, $entity_type_service ) { |
|
| 72 | + |
|
| 73 | + $this->relation_service = $relation_service; |
|
| 74 | + $this->entity_service = $entity_service; |
|
| 75 | + $this->sparql_service = $sparql_service; |
|
| 76 | + $this->configuration_service = $configuration_service; |
|
| 77 | + $this->entity_type_service = $entity_type_service; |
|
| 78 | + |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function __invoke( $args ) { |
|
| 82 | + |
|
| 83 | + $relations = $this->relation_service->find_all_grouped_by_subject_id(); |
|
| 84 | + $progress_bar = \WP_CLI\Utils\make_progress_bar( 'Processing...', count( $relations ) ); |
|
| 85 | + |
|
| 86 | + foreach ( $relations as $relation ) { |
|
| 87 | + $progress_bar->tick(); |
|
| 88 | + |
|
| 89 | + // Get the post. |
|
| 90 | + $post = get_post( $relation->subject_id ); |
|
| 91 | + |
|
| 92 | + // Bail out if the post isn't found. |
|
| 93 | + if ( null == $post ) { |
|
| 94 | + continue; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Bail out if it's an entity: we're only interested in articles |
|
| 98 | + // *referencing* entities. |
|
| 99 | + if ( $this->entity_service->is_entity( $post->ID ) ) { |
|
| 100 | + continue; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // Get the article URI. |
|
| 104 | + $uri = $this->entity_service->get_uri( $post->ID ); |
|
| 105 | + |
|
| 106 | + // Prepare the DELETE query to delete existing data. |
|
| 107 | + $query = self::get_delete_query( $uri ) |
|
| 108 | + . $this->get_insert_query( $post, $uri, explode( ',', $relation->object_ids ) ); |
|
| 109 | + |
|
| 110 | + $this->sparql_service->execute( $query, false ); |
|
| 111 | + |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $progress_bar->finish(); |
|
| 115 | + |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + private static function get_delete_query( $uri ) { |
|
| 119 | + |
|
| 120 | + return Wordlift_Query_Builder |
|
| 121 | + ::new_instance() |
|
| 122 | + ->delete()->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, '?o' ) |
|
| 123 | + ->build() |
|
| 124 | + . Wordlift_Query_Builder |
|
| 125 | + ::new_instance() |
|
| 126 | + ->delete()->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 127 | + ->build() |
|
| 128 | + . Wordlift_Query_Builder |
|
| 129 | + ::new_instance() |
|
| 130 | + ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, '?o' ) |
|
| 131 | + ->build() |
|
| 132 | + . Wordlift_Query_Builder |
|
| 133 | + ::new_instance() |
|
| 134 | + ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 135 | + ->build(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + private function get_insert_query( $post, $uri, $object_ids ) { |
|
| 139 | + |
|
| 140 | + $language_code = $this->configuration_service->get_language_code(); |
|
| 141 | + $type = $this->entity_type_service->get( $post->ID ); |
|
| 142 | + |
|
| 143 | + /* |
|
| 144 | 144 | * When inserting the schema:url property in the triple store, we want to use the production |
| 145 | 145 | * URL, i.e. we must take into consideration that the current URL is a staging one and that |
| 146 | 146 | * 3rd parties may want to update the URL with a production one. |
@@ -149,24 +149,24 @@ discard block |
||
| 149 | 149 | * |
| 150 | 150 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 151 | 151 | */ |
| 152 | - $builder = Wordlift_Query_Builder |
|
| 153 | - ::new_instance() |
|
| 154 | - ->insert() |
|
| 155 | - ->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, $post->post_title, Wordlift_Query_Builder::OBJECT_VALUE, null, $language_code ) |
|
| 156 | - ->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, $type['uri'] ); |
|
| 157 | - |
|
| 158 | - $permalink = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 159 | - if ( ! empty( $permalink ) ) { |
|
| 160 | - $builder->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, $permalink ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $entity_service = $this->entity_service; |
|
| 164 | - array_walk( $object_ids, function ( $item ) use ( $entity_service, $builder, $uri ) { |
|
| 165 | - $object_uri = $entity_service->get_uri( $item ); |
|
| 166 | - $builder->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, $object_uri ); |
|
| 167 | - } ); |
|
| 168 | - |
|
| 169 | - return $builder->build(); |
|
| 170 | - } |
|
| 152 | + $builder = Wordlift_Query_Builder |
|
| 153 | + ::new_instance() |
|
| 154 | + ->insert() |
|
| 155 | + ->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, $post->post_title, Wordlift_Query_Builder::OBJECT_VALUE, null, $language_code ) |
|
| 156 | + ->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, $type['uri'] ); |
|
| 157 | + |
|
| 158 | + $permalink = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 159 | + if ( ! empty( $permalink ) ) { |
|
| 160 | + $builder->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, $permalink ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $entity_service = $this->entity_service; |
|
| 164 | + array_walk( $object_ids, function ( $item ) use ( $entity_service, $builder, $uri ) { |
|
| 165 | + $object_uri = $entity_service->get_uri( $item ); |
|
| 166 | + $builder->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, $object_uri ); |
|
| 167 | + } ); |
|
| 168 | + |
|
| 169 | + return $builder->build(); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | 172 | } |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | * @since 3.18.0 |
| 69 | 69 | * |
| 70 | 70 | */ |
| 71 | - public function __construct( $relation_service, $entity_service, $sparql_service, $configuration_service, $entity_type_service ) { |
|
| 71 | + public function __construct($relation_service, $entity_service, $sparql_service, $configuration_service, $entity_type_service) { |
|
| 72 | 72 | |
| 73 | 73 | $this->relation_service = $relation_service; |
| 74 | 74 | $this->entity_service = $entity_service; |
@@ -78,36 +78,36 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - public function __invoke( $args ) { |
|
| 81 | + public function __invoke($args) { |
|
| 82 | 82 | |
| 83 | 83 | $relations = $this->relation_service->find_all_grouped_by_subject_id(); |
| 84 | - $progress_bar = \WP_CLI\Utils\make_progress_bar( 'Processing...', count( $relations ) ); |
|
| 84 | + $progress_bar = \WP_CLI\Utils\make_progress_bar('Processing...', count($relations)); |
|
| 85 | 85 | |
| 86 | - foreach ( $relations as $relation ) { |
|
| 86 | + foreach ($relations as $relation) { |
|
| 87 | 87 | $progress_bar->tick(); |
| 88 | 88 | |
| 89 | 89 | // Get the post. |
| 90 | - $post = get_post( $relation->subject_id ); |
|
| 90 | + $post = get_post($relation->subject_id); |
|
| 91 | 91 | |
| 92 | 92 | // Bail out if the post isn't found. |
| 93 | - if ( null == $post ) { |
|
| 93 | + if (null == $post) { |
|
| 94 | 94 | continue; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | // Bail out if it's an entity: we're only interested in articles |
| 98 | 98 | // *referencing* entities. |
| 99 | - if ( $this->entity_service->is_entity( $post->ID ) ) { |
|
| 99 | + if ($this->entity_service->is_entity($post->ID)) { |
|
| 100 | 100 | continue; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | // Get the article URI. |
| 104 | - $uri = $this->entity_service->get_uri( $post->ID ); |
|
| 104 | + $uri = $this->entity_service->get_uri($post->ID); |
|
| 105 | 105 | |
| 106 | 106 | // Prepare the DELETE query to delete existing data. |
| 107 | - $query = self::get_delete_query( $uri ) |
|
| 108 | - . $this->get_insert_query( $post, $uri, explode( ',', $relation->object_ids ) ); |
|
| 107 | + $query = self::get_delete_query($uri) |
|
| 108 | + . $this->get_insert_query($post, $uri, explode(',', $relation->object_ids)); |
|
| 109 | 109 | |
| 110 | - $this->sparql_service->execute( $query, false ); |
|
| 110 | + $this->sparql_service->execute($query, false); |
|
| 111 | 111 | |
| 112 | 112 | } |
| 113 | 113 | |
@@ -115,30 +115,30 @@ discard block |
||
| 115 | 115 | |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - private static function get_delete_query( $uri ) { |
|
| 118 | + private static function get_delete_query($uri) { |
|
| 119 | 119 | |
| 120 | 120 | return Wordlift_Query_Builder |
| 121 | 121 | ::new_instance() |
| 122 | - ->delete()->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, '?o' ) |
|
| 122 | + ->delete()->statement($uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, '?o') |
|
| 123 | 123 | ->build() |
| 124 | 124 | . Wordlift_Query_Builder |
| 125 | 125 | ::new_instance() |
| 126 | - ->delete()->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 126 | + ->delete()->statement($uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o') |
|
| 127 | 127 | ->build() |
| 128 | 128 | . Wordlift_Query_Builder |
| 129 | 129 | ::new_instance() |
| 130 | - ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, '?o' ) |
|
| 130 | + ->delete()->statement($uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, '?o') |
|
| 131 | 131 | ->build() |
| 132 | 132 | . Wordlift_Query_Builder |
| 133 | 133 | ::new_instance() |
| 134 | - ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 134 | + ->delete()->statement($uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o') |
|
| 135 | 135 | ->build(); |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - private function get_insert_query( $post, $uri, $object_ids ) { |
|
| 138 | + private function get_insert_query($post, $uri, $object_ids) { |
|
| 139 | 139 | |
| 140 | 140 | $language_code = $this->configuration_service->get_language_code(); |
| 141 | - $type = $this->entity_type_service->get( $post->ID ); |
|
| 141 | + $type = $this->entity_type_service->get($post->ID); |
|
| 142 | 142 | |
| 143 | 143 | /* |
| 144 | 144 | * When inserting the schema:url property in the triple store, we want to use the production |
@@ -152,18 +152,18 @@ discard block |
||
| 152 | 152 | $builder = Wordlift_Query_Builder |
| 153 | 153 | ::new_instance() |
| 154 | 154 | ->insert() |
| 155 | - ->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, $post->post_title, Wordlift_Query_Builder::OBJECT_VALUE, null, $language_code ) |
|
| 156 | - ->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, $type['uri'] ); |
|
| 155 | + ->statement($uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, $post->post_title, Wordlift_Query_Builder::OBJECT_VALUE, null, $language_code) |
|
| 156 | + ->statement($uri, Wordlift_Query_Builder::RDFS_TYPE_URI, $type['uri']); |
|
| 157 | 157 | |
| 158 | - $permalink = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 159 | - if ( ! empty( $permalink ) ) { |
|
| 160 | - $builder->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, $permalink ); |
|
| 158 | + $permalink = Wordlift_Post_Adapter::get_production_permalink($post->ID); |
|
| 159 | + if ( ! empty($permalink)) { |
|
| 160 | + $builder->statement($uri, Wordlift_Query_Builder::SCHEMA_URL_URI, $permalink); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | $entity_service = $this->entity_service; |
| 164 | - array_walk( $object_ids, function ( $item ) use ( $entity_service, $builder, $uri ) { |
|
| 165 | - $object_uri = $entity_service->get_uri( $item ); |
|
| 166 | - $builder->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, $object_uri ); |
|
| 164 | + array_walk($object_ids, function($item) use ($entity_service, $builder, $uri) { |
|
| 165 | + $object_uri = $entity_service->get_uri($item); |
|
| 166 | + $builder->statement($uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, $object_uri); |
|
| 167 | 167 | } ); |
| 168 | 168 | |
| 169 | 169 | return $builder->build(); |