Completed
Push — develop ( fa5c7e...2774c1 )
by David
02:50
created
src/includes/class-wordlift-content-filter-service.php 2 patches
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -18,225 +18,225 @@
 block discarded – undo
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
-	/**
56
-	 * The {@link Wordlift_Content_Filter_Service} singleton instance.
57
-	 *
58
-	 * @since  3.14.2
59
-	 * @access private
60
-	 * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance.
61
-	 */
62
-	private static $instance;
63
-
64
-	/**
65
-	 * Create a {@link Wordlift_Content_Filter_Service} instance.
66
-	 *
67
-	 * @since 3.8.0
68
-	 *
69
-	 * @param \Wordlift_Entity_Service        $entity_service        The {@link Wordlift_Entity_Service} instance.
70
-	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
71
-	 */
72
-	public function __construct( $entity_service, $configuration_service ) {
73
-
74
-		$this->entity_service        = $entity_service;
75
-		$this->configuration_service = $configuration_service;
76
-
77
-		self::$instance = $this;
78
-
79
-	}
80
-
81
-	/**
82
-	 * Get the {@link Wordlift_Content_Filter_Service} singleton instance.
83
-	 *
84
-	 * @since 3.14.2
85
-	 * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance.
86
-	 */
87
-	public static function get_instance() {
88
-
89
-		return self::$instance;
90
-	}
91
-
92
-	/**
93
-	 * Mangle the content by adding links to the entity pages. This function is
94
-	 * hooked to the 'the_content' WP's filter.
95
-	 *
96
-	 * @since 3.8.0
97
-	 *
98
-	 * @param string $content The content being filtered.
99
-	 *
100
-	 * @return string The filtered content.
101
-	 */
102
-	public function the_content( $content ) {
103
-
104
-		// Preload the `link by default` setting.
105
-		$this->is_link_by_default = $this->configuration_service->is_link_by_default();
106
-
107
-		// Replace each match of the entity tag with the entity link. If an error
108
-		// occurs fail silently returning the original content.
109
-		return preg_replace_callback( self::PATTERN, array(
110
-			$this,
111
-			'link',
112
-		), $content ) ?: $content;
113
-	}
114
-
115
-	/**
116
-	 * Get the entity match and replace it with a page link.
117
-	 *
118
-	 * @since 3.8.0
119
-	 *
120
-	 * @param array $matches An array of matches.
121
-	 *
122
-	 * @return string The replaced text with the link to the entity page.
123
-	 */
124
-	private function link( $matches ) {
125
-
126
-		// Get the entity itemid URI and label.
127
-		$css_class = $matches[2];
128
-		$uri       = $matches[3];
129
-		$label     = $matches[4];
130
-
131
-		// Get the entity post by URI.
132
-		if ( null === ( $post = $this->entity_service->get_entity_post_by_uri( $uri ) ) ) {
133
-
134
-			// If the entity post is not found return the label w/o the markup
135
-			// around it.
136
-			//
137
-			// See https://github.com/insideout10/wordlift-plugin/issues/461
138
-			return $label;
139
-		}
140
-
141
-		$no_link = - 1 < strpos( $css_class, 'wl-no-link' );
142
-		$link    = - 1 < strpos( $css_class, 'wl-link' );
143
-
144
-		// Don't link if links are disabled and the entity is not link or the
145
-		// entity is do not link.
146
-		$dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link;
147
-
148
-		// Return the label if it's don't link.
149
-		if ( $dont_link ) {
150
-			return $label;
151
-		}
152
-
153
-		// Get the link.
154
-		$href = get_permalink( $post );
155
-
156
-		// Get an alternative title attribute.
157
-		$title_attribute = $this->get_title_attribute( $post->ID, $label );
158
-
159
-		// Return the link.
160
-		return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>";
161
-	}
162
-
163
-	/**
164
-	 * Get a `title` attribute with an alternative label for the link.
165
-	 *
166
-	 * If an alternative title isn't available an empty string is returned.
167
-	 *
168
-	 * @since 3.15.0
169
-	 *
170
-	 * @param int    $post_id The {@link WP_Post}'s id.
171
-	 * @param string $label   The main link label.
172
-	 *
173
-	 * @return string A `title` attribute with an alternative label or an empty
174
-	 *                string if none available.
175
-	 */
176
-	private function get_title_attribute( $post_id, $label ) {
177
-
178
-		// Get an alternative title.
179
-		$title = $this->get_link_title( $post_id, $label );
180
-		if ( ! empty( $title ) ) {
181
-			return 'title="' . esc_attr( $title ) . '"';
182
-		}
183
-
184
-		return '';
185
-	}
186
-
187
-	/**
188
-	 * Get a string to be used as a title attribute in links to a post
189
-	 *
190
-	 * @since 3.15.0
191
-	 *
192
-	 * @param int    $post_id      The post id of the post being linked.
193
-	 * @param string $ignore_label A label to ignore.
194
-	 *
195
-	 * @return string    The title to be used in the link. An empty string when
196
-	 *                    there is no alternative that is not the $ignore_label.
197
-	 */
198
-	function get_link_title( $post_id, $ignore_label ) {
199
-
200
-		// Get possible alternative labels we can select from
201
-		$labels = $this->entity_service->get_alternative_labels( $post_id );
202
-
203
-		/*
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
+    /**
56
+     * The {@link Wordlift_Content_Filter_Service} singleton instance.
57
+     *
58
+     * @since  3.14.2
59
+     * @access private
60
+     * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance.
61
+     */
62
+    private static $instance;
63
+
64
+    /**
65
+     * Create a {@link Wordlift_Content_Filter_Service} instance.
66
+     *
67
+     * @since 3.8.0
68
+     *
69
+     * @param \Wordlift_Entity_Service        $entity_service        The {@link Wordlift_Entity_Service} instance.
70
+     * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
71
+     */
72
+    public function __construct( $entity_service, $configuration_service ) {
73
+
74
+        $this->entity_service        = $entity_service;
75
+        $this->configuration_service = $configuration_service;
76
+
77
+        self::$instance = $this;
78
+
79
+    }
80
+
81
+    /**
82
+     * Get the {@link Wordlift_Content_Filter_Service} singleton instance.
83
+     *
84
+     * @since 3.14.2
85
+     * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance.
86
+     */
87
+    public static function get_instance() {
88
+
89
+        return self::$instance;
90
+    }
91
+
92
+    /**
93
+     * Mangle the content by adding links to the entity pages. This function is
94
+     * hooked to the 'the_content' WP's filter.
95
+     *
96
+     * @since 3.8.0
97
+     *
98
+     * @param string $content The content being filtered.
99
+     *
100
+     * @return string The filtered content.
101
+     */
102
+    public function the_content( $content ) {
103
+
104
+        // Preload the `link by default` setting.
105
+        $this->is_link_by_default = $this->configuration_service->is_link_by_default();
106
+
107
+        // Replace each match of the entity tag with the entity link. If an error
108
+        // occurs fail silently returning the original content.
109
+        return preg_replace_callback( self::PATTERN, array(
110
+            $this,
111
+            'link',
112
+        ), $content ) ?: $content;
113
+    }
114
+
115
+    /**
116
+     * Get the entity match and replace it with a page link.
117
+     *
118
+     * @since 3.8.0
119
+     *
120
+     * @param array $matches An array of matches.
121
+     *
122
+     * @return string The replaced text with the link to the entity page.
123
+     */
124
+    private function link( $matches ) {
125
+
126
+        // Get the entity itemid URI and label.
127
+        $css_class = $matches[2];
128
+        $uri       = $matches[3];
129
+        $label     = $matches[4];
130
+
131
+        // Get the entity post by URI.
132
+        if ( null === ( $post = $this->entity_service->get_entity_post_by_uri( $uri ) ) ) {
133
+
134
+            // If the entity post is not found return the label w/o the markup
135
+            // around it.
136
+            //
137
+            // See https://github.com/insideout10/wordlift-plugin/issues/461
138
+            return $label;
139
+        }
140
+
141
+        $no_link = - 1 < strpos( $css_class, 'wl-no-link' );
142
+        $link    = - 1 < strpos( $css_class, 'wl-link' );
143
+
144
+        // Don't link if links are disabled and the entity is not link or the
145
+        // entity is do not link.
146
+        $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link;
147
+
148
+        // Return the label if it's don't link.
149
+        if ( $dont_link ) {
150
+            return $label;
151
+        }
152
+
153
+        // Get the link.
154
+        $href = get_permalink( $post );
155
+
156
+        // Get an alternative title attribute.
157
+        $title_attribute = $this->get_title_attribute( $post->ID, $label );
158
+
159
+        // Return the link.
160
+        return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>";
161
+    }
162
+
163
+    /**
164
+     * Get a `title` attribute with an alternative label for the link.
165
+     *
166
+     * If an alternative title isn't available an empty string is returned.
167
+     *
168
+     * @since 3.15.0
169
+     *
170
+     * @param int    $post_id The {@link WP_Post}'s id.
171
+     * @param string $label   The main link label.
172
+     *
173
+     * @return string A `title` attribute with an alternative label or an empty
174
+     *                string if none available.
175
+     */
176
+    private function get_title_attribute( $post_id, $label ) {
177
+
178
+        // Get an alternative title.
179
+        $title = $this->get_link_title( $post_id, $label );
180
+        if ( ! empty( $title ) ) {
181
+            return 'title="' . esc_attr( $title ) . '"';
182
+        }
183
+
184
+        return '';
185
+    }
186
+
187
+    /**
188
+     * Get a string to be used as a title attribute in links to a post
189
+     *
190
+     * @since 3.15.0
191
+     *
192
+     * @param int    $post_id      The post id of the post being linked.
193
+     * @param string $ignore_label A label to ignore.
194
+     *
195
+     * @return string    The title to be used in the link. An empty string when
196
+     *                    there is no alternative that is not the $ignore_label.
197
+     */
198
+    function get_link_title( $post_id, $ignore_label ) {
199
+
200
+        // Get possible alternative labels we can select from
201
+        $labels = $this->entity_service->get_alternative_labels( $post_id );
202
+
203
+        /*
204 204
 		 * Since the original text might use an alternative label than the
205 205
 		 * Entity title, add the title itself which is not returned by the api.
206 206
 		 */
207
-		$labels[] = get_the_title( $post_id );
208
-
209
-		// Add some randomness to the label selection
210
-		shuffle( $labels );
211
-
212
-		// Select the first label which is not to be ignored.
213
-		$title = '';
214
-		foreach ( $labels as $label ) {
215
-			if ( strcasecmp( $label, $ignore_label ) != 0 ) {
216
-				$title = $label;
217
-				break;
218
-			}
219
-		}
220
-
221
-		return $title;
222
-	}
223
-
224
-	/**
225
-	 * Get the entity URIs (configured in the `itemid` attribute) contained in
226
-	 * the provided content.
227
-	 *
228
-	 * @since 3.14.2
229
-	 *
230
-	 * @param string $content The content.
231
-	 *
232
-	 * @return array An array of URIs.
233
-	 */
234
-	public function get_entity_uris( $content ) {
235
-
236
-		$matches = array();
237
-		preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches );
238
-
239
-		return array_unique( $matches[3] );
240
-	}
207
+        $labels[] = get_the_title( $post_id );
208
+
209
+        // Add some randomness to the label selection
210
+        shuffle( $labels );
211
+
212
+        // Select the first label which is not to be ignored.
213
+        $title = '';
214
+        foreach ( $labels as $label ) {
215
+            if ( strcasecmp( $label, $ignore_label ) != 0 ) {
216
+                $title = $label;
217
+                break;
218
+            }
219
+        }
220
+
221
+        return $title;
222
+    }
223
+
224
+    /**
225
+     * Get the entity URIs (configured in the `itemid` attribute) contained in
226
+     * the provided content.
227
+     *
228
+     * @since 3.14.2
229
+     *
230
+     * @param string $content The content.
231
+     *
232
+     * @return array An array of URIs.
233
+     */
234
+    public function get_entity_uris( $content ) {
235
+
236
+        $matches = array();
237
+        preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches );
238
+
239
+        return array_unique( $matches[3] );
240
+    }
241 241
 
242 242
 }
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 	 * @param \Wordlift_Entity_Service        $entity_service        The {@link Wordlift_Entity_Service} instance.
70 70
 	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
71 71
 	 */
72
-	public function __construct( $entity_service, $configuration_service ) {
72
+	public function __construct($entity_service, $configuration_service) {
73 73
 
74 74
 		$this->entity_service        = $entity_service;
75 75
 		$this->configuration_service = $configuration_service;
@@ -99,17 +99,17 @@  discard block
 block discarded – undo
99 99
 	 *
100 100
 	 * @return string The filtered content.
101 101
 	 */
102
-	public function the_content( $content ) {
102
+	public function the_content($content) {
103 103
 
104 104
 		// Preload the `link by default` setting.
105 105
 		$this->is_link_by_default = $this->configuration_service->is_link_by_default();
106 106
 
107 107
 		// Replace each match of the entity tag with the entity link. If an error
108 108
 		// occurs fail silently returning the original content.
109
-		return preg_replace_callback( self::PATTERN, array(
109
+		return preg_replace_callback(self::PATTERN, array(
110 110
 			$this,
111 111
 			'link',
112
-		), $content ) ?: $content;
112
+		), $content) ?: $content;
113 113
 	}
114 114
 
115 115
 	/**
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
 	 *
122 122
 	 * @return string The replaced text with the link to the entity page.
123 123
 	 */
124
-	private function link( $matches ) {
124
+	private function link($matches) {
125 125
 
126 126
 		// Get the entity itemid URI and label.
127 127
 		$css_class = $matches[2];
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
 		$label     = $matches[4];
130 130
 
131 131
 		// Get the entity post by URI.
132
-		if ( null === ( $post = $this->entity_service->get_entity_post_by_uri( $uri ) ) ) {
132
+		if (null === ($post = $this->entity_service->get_entity_post_by_uri($uri))) {
133 133
 
134 134
 			// If the entity post is not found return the label w/o the markup
135 135
 			// around it.
@@ -138,23 +138,23 @@  discard block
 block discarded – undo
138 138
 			return $label;
139 139
 		}
140 140
 
141
-		$no_link = - 1 < strpos( $css_class, 'wl-no-link' );
142
-		$link    = - 1 < strpos( $css_class, 'wl-link' );
141
+		$no_link = - 1 < strpos($css_class, 'wl-no-link');
142
+		$link    = - 1 < strpos($css_class, 'wl-link');
143 143
 
144 144
 		// Don't link if links are disabled and the entity is not link or the
145 145
 		// entity is do not link.
146
-		$dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link;
146
+		$dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link;
147 147
 
148 148
 		// Return the label if it's don't link.
149
-		if ( $dont_link ) {
149
+		if ($dont_link) {
150 150
 			return $label;
151 151
 		}
152 152
 
153 153
 		// Get the link.
154
-		$href = get_permalink( $post );
154
+		$href = get_permalink($post);
155 155
 
156 156
 		// Get an alternative title attribute.
157
-		$title_attribute = $this->get_title_attribute( $post->ID, $label );
157
+		$title_attribute = $this->get_title_attribute($post->ID, $label);
158 158
 
159 159
 		// Return the link.
160 160
 		return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>";
@@ -173,12 +173,12 @@  discard block
 block discarded – undo
173 173
 	 * @return string A `title` attribute with an alternative label or an empty
174 174
 	 *                string if none available.
175 175
 	 */
176
-	private function get_title_attribute( $post_id, $label ) {
176
+	private function get_title_attribute($post_id, $label) {
177 177
 
178 178
 		// Get an alternative title.
179
-		$title = $this->get_link_title( $post_id, $label );
180
-		if ( ! empty( $title ) ) {
181
-			return 'title="' . esc_attr( $title ) . '"';
179
+		$title = $this->get_link_title($post_id, $label);
180
+		if ( ! empty($title)) {
181
+			return 'title="'.esc_attr($title).'"';
182 182
 		}
183 183
 
184 184
 		return '';
@@ -195,24 +195,24 @@  discard block
 block discarded – undo
195 195
 	 * @return string    The title to be used in the link. An empty string when
196 196
 	 *                    there is no alternative that is not the $ignore_label.
197 197
 	 */
198
-	function get_link_title( $post_id, $ignore_label ) {
198
+	function get_link_title($post_id, $ignore_label) {
199 199
 
200 200
 		// Get possible alternative labels we can select from
201
-		$labels = $this->entity_service->get_alternative_labels( $post_id );
201
+		$labels = $this->entity_service->get_alternative_labels($post_id);
202 202
 
203 203
 		/*
204 204
 		 * Since the original text might use an alternative label than the
205 205
 		 * Entity title, add the title itself which is not returned by the api.
206 206
 		 */
207
-		$labels[] = get_the_title( $post_id );
207
+		$labels[] = get_the_title($post_id);
208 208
 
209 209
 		// Add some randomness to the label selection
210
-		shuffle( $labels );
210
+		shuffle($labels);
211 211
 
212 212
 		// Select the first label which is not to be ignored.
213 213
 		$title = '';
214
-		foreach ( $labels as $label ) {
215
-			if ( strcasecmp( $label, $ignore_label ) != 0 ) {
214
+		foreach ($labels as $label) {
215
+			if (strcasecmp($label, $ignore_label) != 0) {
216 216
 				$title = $label;
217 217
 				break;
218 218
 			}
@@ -231,12 +231,12 @@  discard block
 block discarded – undo
231 231
 	 *
232 232
 	 * @return array An array of URIs.
233 233
 	 */
234
-	public function get_entity_uris( $content ) {
234
+	public function get_entity_uris($content) {
235 235
 
236 236
 		$matches = array();
237
-		preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches );
237
+		preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches);
238 238
 
239
-		return array_unique( $matches[3] );
239
+		return array_unique($matches[3]);
240 240
 	}
241 241
 
242 242
 }
Please login to merge, or discard this patch.