@@ -18,319 +18,319 @@ |
||
| 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 | - /** |
|
| 232 | - * Allow 3rd parties to add additional attributes to the anchor link. |
|
| 233 | - * |
|
| 234 | - * @since 3.26.0 |
|
| 235 | - */ |
|
| 236 | - $default_attributes = array( |
|
| 237 | - 'id' => implode( ';', array_merge( |
|
| 238 | - (array) $this->entity_service->get_uri( $post->ID ), |
|
| 239 | - get_post_meta( $post->ID, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 240 | - ) ) |
|
| 241 | - ); |
|
| 242 | - $attributes = apply_filters( 'wl_anchor_data_attributes', $default_attributes, $post->ID ); |
|
| 243 | - $attributes_html = ''; |
|
| 244 | - foreach ( $attributes as $key => $value ) { |
|
| 245 | - $attributes_html .= ' data-' . esc_html( $key ) . '="' . esc_attr( $value ) . '" '; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Return the link. |
|
| 249 | - return "<a class='wl-entity-page-link' $title_attribute href='$href' $attributes_html>$label</a>"; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Get a `title` attribute with an alternative label for the link. |
|
| 254 | - * |
|
| 255 | - * If an alternative title isn't available an empty string is returned. |
|
| 256 | - * |
|
| 257 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 258 | - * @param string $label The main link label. |
|
| 259 | - * |
|
| 260 | - * @return string A `title` attribute with an alternative label or an empty |
|
| 261 | - * string if none available. |
|
| 262 | - * @since 3.15.0 |
|
| 263 | - * |
|
| 264 | - */ |
|
| 265 | - private function get_title_attribute( $post_id, $label ) { |
|
| 266 | - |
|
| 267 | - // Get an alternative title. |
|
| 268 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 269 | - if ( ! empty( $title ) ) { |
|
| 270 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - return ''; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Get a string to be used as a title attribute in links to a post |
|
| 278 | - * |
|
| 279 | - * @param int $post_id The post id of the post being linked. |
|
| 280 | - * @param string $ignore_label A label to ignore. |
|
| 281 | - * |
|
| 282 | - * @return string The title to be used in the link. An empty string when |
|
| 283 | - * there is no alternative that is not the $ignore_label. |
|
| 284 | - * @since 3.15.0 |
|
| 285 | - * |
|
| 286 | - */ |
|
| 287 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 288 | - |
|
| 289 | - // Get possible alternative labels we can select from. |
|
| 290 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 291 | - |
|
| 292 | - /* |
|
| 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 | + /** |
|
| 232 | + * Allow 3rd parties to add additional attributes to the anchor link. |
|
| 233 | + * |
|
| 234 | + * @since 3.26.0 |
|
| 235 | + */ |
|
| 236 | + $default_attributes = array( |
|
| 237 | + 'id' => implode( ';', array_merge( |
|
| 238 | + (array) $this->entity_service->get_uri( $post->ID ), |
|
| 239 | + get_post_meta( $post->ID, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 240 | + ) ) |
|
| 241 | + ); |
|
| 242 | + $attributes = apply_filters( 'wl_anchor_data_attributes', $default_attributes, $post->ID ); |
|
| 243 | + $attributes_html = ''; |
|
| 244 | + foreach ( $attributes as $key => $value ) { |
|
| 245 | + $attributes_html .= ' data-' . esc_html( $key ) . '="' . esc_attr( $value ) . '" '; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Return the link. |
|
| 249 | + return "<a class='wl-entity-page-link' $title_attribute href='$href' $attributes_html>$label</a>"; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Get a `title` attribute with an alternative label for the link. |
|
| 254 | + * |
|
| 255 | + * If an alternative title isn't available an empty string is returned. |
|
| 256 | + * |
|
| 257 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 258 | + * @param string $label The main link label. |
|
| 259 | + * |
|
| 260 | + * @return string A `title` attribute with an alternative label or an empty |
|
| 261 | + * string if none available. |
|
| 262 | + * @since 3.15.0 |
|
| 263 | + * |
|
| 264 | + */ |
|
| 265 | + private function get_title_attribute( $post_id, $label ) { |
|
| 266 | + |
|
| 267 | + // Get an alternative title. |
|
| 268 | + $title = $this->get_link_title( $post_id, $label ); |
|
| 269 | + if ( ! empty( $title ) ) { |
|
| 270 | + return 'title="' . esc_attr( $title ) . '"'; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + return ''; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Get a string to be used as a title attribute in links to a post |
|
| 278 | + * |
|
| 279 | + * @param int $post_id The post id of the post being linked. |
|
| 280 | + * @param string $ignore_label A label to ignore. |
|
| 281 | + * |
|
| 282 | + * @return string The title to be used in the link. An empty string when |
|
| 283 | + * there is no alternative that is not the $ignore_label. |
|
| 284 | + * @since 3.15.0 |
|
| 285 | + * |
|
| 286 | + */ |
|
| 287 | + function get_link_title( $post_id, $ignore_label ) { |
|
| 288 | + |
|
| 289 | + // Get possible alternative labels we can select from. |
|
| 290 | + $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 291 | + |
|
| 292 | + /* |
|
| 293 | 293 | * Since the original text might use an alternative label than the |
| 294 | 294 | * Entity title, add the title itself which is not returned by the api. |
| 295 | 295 | */ |
| 296 | - $labels[] = get_the_title( $post_id ); |
|
| 297 | - |
|
| 298 | - // Add some randomness to the label selection. |
|
| 299 | - shuffle( $labels ); |
|
| 300 | - |
|
| 301 | - // Select the first label which is not to be ignored. |
|
| 302 | - $title = ''; |
|
| 303 | - foreach ( $labels as $label ) { |
|
| 304 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 305 | - $title = $label; |
|
| 306 | - break; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return $title; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 315 | - * the provided content. |
|
| 316 | - * |
|
| 317 | - * @param string $content The content. |
|
| 318 | - * |
|
| 319 | - * @return array An array of URIs. |
|
| 320 | - * @since 3.14.2 |
|
| 321 | - * |
|
| 322 | - */ |
|
| 323 | - public function get_entity_uris( $content ) { |
|
| 324 | - |
|
| 325 | - $matches = array(); |
|
| 326 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 327 | - |
|
| 328 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 329 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
| 330 | - // is not sequential. |
|
| 331 | - // |
|
| 332 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 333 | - return array_values( array_unique( $matches[3] ) ); |
|
| 334 | - } |
|
| 296 | + $labels[] = get_the_title( $post_id ); |
|
| 297 | + |
|
| 298 | + // Add some randomness to the label selection. |
|
| 299 | + shuffle( $labels ); |
|
| 300 | + |
|
| 301 | + // Select the first label which is not to be ignored. |
|
| 302 | + $title = ''; |
|
| 303 | + foreach ( $labels as $label ) { |
|
| 304 | + if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 305 | + $title = $label; |
|
| 306 | + break; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return $title; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 315 | + * the provided content. |
|
| 316 | + * |
|
| 317 | + * @param string $content The content. |
|
| 318 | + * |
|
| 319 | + * @return array An array of URIs. |
|
| 320 | + * @since 3.14.2 |
|
| 321 | + * |
|
| 322 | + */ |
|
| 323 | + public function get_entity_uris( $content ) { |
|
| 324 | + |
|
| 325 | + $matches = array(); |
|
| 326 | + preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 327 | + |
|
| 328 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 329 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
| 330 | + // is not sequential. |
|
| 331 | + // |
|
| 332 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 333 | + return array_values( array_unique( $matches[3] ) ); |
|
| 334 | + } |
|
| 335 | 335 | |
| 336 | 336 | } |
@@ -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 | /** |
| 232 | 232 | * Allow 3rd parties to add additional attributes to the anchor link. |
@@ -234,15 +234,15 @@ discard block |
||
| 234 | 234 | * @since 3.26.0 |
| 235 | 235 | */ |
| 236 | 236 | $default_attributes = array( |
| 237 | - 'id' => implode( ';', array_merge( |
|
| 238 | - (array) $this->entity_service->get_uri( $post->ID ), |
|
| 239 | - get_post_meta( $post->ID, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 240 | - ) ) |
|
| 237 | + 'id' => implode(';', array_merge( |
|
| 238 | + (array) $this->entity_service->get_uri($post->ID), |
|
| 239 | + get_post_meta($post->ID, Wordlift_Schema_Service::FIELD_SAME_AS) |
|
| 240 | + )) |
|
| 241 | 241 | ); |
| 242 | - $attributes = apply_filters( 'wl_anchor_data_attributes', $default_attributes, $post->ID ); |
|
| 242 | + $attributes = apply_filters('wl_anchor_data_attributes', $default_attributes, $post->ID); |
|
| 243 | 243 | $attributes_html = ''; |
| 244 | - foreach ( $attributes as $key => $value ) { |
|
| 245 | - $attributes_html .= ' data-' . esc_html( $key ) . '="' . esc_attr( $value ) . '" '; |
|
| 244 | + foreach ($attributes as $key => $value) { |
|
| 245 | + $attributes_html .= ' data-'.esc_html($key).'="'.esc_attr($value).'" '; |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | // Return the link. |
@@ -262,12 +262,12 @@ discard block |
||
| 262 | 262 | * @since 3.15.0 |
| 263 | 263 | * |
| 264 | 264 | */ |
| 265 | - private function get_title_attribute( $post_id, $label ) { |
|
| 265 | + private function get_title_attribute($post_id, $label) { |
|
| 266 | 266 | |
| 267 | 267 | // Get an alternative title. |
| 268 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 269 | - if ( ! empty( $title ) ) { |
|
| 270 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 268 | + $title = $this->get_link_title($post_id, $label); |
|
| 269 | + if ( ! empty($title)) { |
|
| 270 | + return 'title="'.esc_attr($title).'"'; |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | return ''; |
@@ -284,24 +284,24 @@ discard block |
||
| 284 | 284 | * @since 3.15.0 |
| 285 | 285 | * |
| 286 | 286 | */ |
| 287 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 287 | + function get_link_title($post_id, $ignore_label) { |
|
| 288 | 288 | |
| 289 | 289 | // Get possible alternative labels we can select from. |
| 290 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 290 | + $labels = $this->entity_service->get_alternative_labels($post_id); |
|
| 291 | 291 | |
| 292 | 292 | /* |
| 293 | 293 | * Since the original text might use an alternative label than the |
| 294 | 294 | * Entity title, add the title itself which is not returned by the api. |
| 295 | 295 | */ |
| 296 | - $labels[] = get_the_title( $post_id ); |
|
| 296 | + $labels[] = get_the_title($post_id); |
|
| 297 | 297 | |
| 298 | 298 | // Add some randomness to the label selection. |
| 299 | - shuffle( $labels ); |
|
| 299 | + shuffle($labels); |
|
| 300 | 300 | |
| 301 | 301 | // Select the first label which is not to be ignored. |
| 302 | 302 | $title = ''; |
| 303 | - foreach ( $labels as $label ) { |
|
| 304 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 303 | + foreach ($labels as $label) { |
|
| 304 | + if (0 !== strcasecmp($label, $ignore_label)) { |
|
| 305 | 305 | $title = $label; |
| 306 | 306 | break; |
| 307 | 307 | } |
@@ -320,17 +320,17 @@ discard block |
||
| 320 | 320 | * @since 3.14.2 |
| 321 | 321 | * |
| 322 | 322 | */ |
| 323 | - public function get_entity_uris( $content ) { |
|
| 323 | + public function get_entity_uris($content) { |
|
| 324 | 324 | |
| 325 | 325 | $matches = array(); |
| 326 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 326 | + preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches); |
|
| 327 | 327 | |
| 328 | 328 | // We need to use `array_values` here in order to avoid further `json_encode` |
| 329 | 329 | // to turn it into an object (since if the 3rd match isn't found the index |
| 330 | 330 | // is not sequential. |
| 331 | 331 | // |
| 332 | 332 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
| 333 | - return array_values( array_unique( $matches[3] ) ); |
|
| 333 | + return array_values(array_unique($matches[3])); |
|
| 334 | 334 | } |
| 335 | 335 | |
| 336 | 336 | } |