Completed
Push — develop ( 2774c1...957f96 )
by David
02:45
created
src/includes/class-wordlift-content-filter-service.php 2 patches
Indentation   +218 added lines, -218 removed lines patch added patch discarded remove patch
@@ -18,226 +18,226 @@
 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
-		$post = $this->entity_service->get_entity_post_by_uri( $uri );
133
-		if ( null === $post ) {
134
-
135
-			// If the entity post is not found return the label w/o the markup
136
-			// around it.
137
-			//
138
-			// See https://github.com/insideout10/wordlift-plugin/issues/461.
139
-			return $label;
140
-		}
141
-
142
-		$no_link = - 1 < strpos( $css_class, 'wl-no-link' );
143
-		$link    = - 1 < strpos( $css_class, 'wl-link' );
144
-
145
-		// Don't link if links are disabled and the entity is not link or the
146
-		// entity is do not link.
147
-		$dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link;
148
-
149
-		// Return the label if it's don't link.
150
-		if ( $dont_link ) {
151
-			return $label;
152
-		}
153
-
154
-		// Get the link.
155
-		$href = get_permalink( $post );
156
-
157
-		// Get an alternative title attribute.
158
-		$title_attribute = $this->get_title_attribute( $post->ID, $label );
159
-
160
-		// Return the link.
161
-		return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>";
162
-	}
163
-
164
-	/**
165
-	 * Get a `title` attribute with an alternative label for the link.
166
-	 *
167
-	 * If an alternative title isn't available an empty string is returned.
168
-	 *
169
-	 * @since 3.15.0
170
-	 *
171
-	 * @param int    $post_id The {@link WP_Post}'s id.
172
-	 * @param string $label   The main link label.
173
-	 *
174
-	 * @return string A `title` attribute with an alternative label or an empty
175
-	 *                string if none available.
176
-	 */
177
-	private function get_title_attribute( $post_id, $label ) {
178
-
179
-		// Get an alternative title.
180
-		$title = $this->get_link_title( $post_id, $label );
181
-		if ( ! empty( $title ) ) {
182
-			return 'title="' . esc_attr( $title ) . '"';
183
-		}
184
-
185
-		return '';
186
-	}
187
-
188
-	/**
189
-	 * Get a string to be used as a title attribute in links to a post
190
-	 *
191
-	 * @since 3.15.0
192
-	 *
193
-	 * @param int    $post_id      The post id of the post being linked.
194
-	 * @param string $ignore_label A label to ignore.
195
-	 *
196
-	 * @return string    The title to be used in the link. An empty string when
197
-	 *                    there is no alternative that is not the $ignore_label.
198
-	 */
199
-	function get_link_title( $post_id, $ignore_label ) {
200
-
201
-		// Get possible alternative labels we can select from.
202
-		$labels = $this->entity_service->get_alternative_labels( $post_id );
203
-
204
-		/*
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
+        $post = $this->entity_service->get_entity_post_by_uri( $uri );
133
+        if ( null === $post ) {
134
+
135
+            // If the entity post is not found return the label w/o the markup
136
+            // around it.
137
+            //
138
+            // See https://github.com/insideout10/wordlift-plugin/issues/461.
139
+            return $label;
140
+        }
141
+
142
+        $no_link = - 1 < strpos( $css_class, 'wl-no-link' );
143
+        $link    = - 1 < strpos( $css_class, 'wl-link' );
144
+
145
+        // Don't link if links are disabled and the entity is not link or the
146
+        // entity is do not link.
147
+        $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link;
148
+
149
+        // Return the label if it's don't link.
150
+        if ( $dont_link ) {
151
+            return $label;
152
+        }
153
+
154
+        // Get the link.
155
+        $href = get_permalink( $post );
156
+
157
+        // Get an alternative title attribute.
158
+        $title_attribute = $this->get_title_attribute( $post->ID, $label );
159
+
160
+        // Return the link.
161
+        return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>";
162
+    }
163
+
164
+    /**
165
+     * Get a `title` attribute with an alternative label for the link.
166
+     *
167
+     * If an alternative title isn't available an empty string is returned.
168
+     *
169
+     * @since 3.15.0
170
+     *
171
+     * @param int    $post_id The {@link WP_Post}'s id.
172
+     * @param string $label   The main link label.
173
+     *
174
+     * @return string A `title` attribute with an alternative label or an empty
175
+     *                string if none available.
176
+     */
177
+    private function get_title_attribute( $post_id, $label ) {
178
+
179
+        // Get an alternative title.
180
+        $title = $this->get_link_title( $post_id, $label );
181
+        if ( ! empty( $title ) ) {
182
+            return 'title="' . esc_attr( $title ) . '"';
183
+        }
184
+
185
+        return '';
186
+    }
187
+
188
+    /**
189
+     * Get a string to be used as a title attribute in links to a post
190
+     *
191
+     * @since 3.15.0
192
+     *
193
+     * @param int    $post_id      The post id of the post being linked.
194
+     * @param string $ignore_label A label to ignore.
195
+     *
196
+     * @return string    The title to be used in the link. An empty string when
197
+     *                    there is no alternative that is not the $ignore_label.
198
+     */
199
+    function get_link_title( $post_id, $ignore_label ) {
200
+
201
+        // Get possible alternative labels we can select from.
202
+        $labels = $this->entity_service->get_alternative_labels( $post_id );
203
+
204
+        /*
205 205
 		 * Since the original text might use an alternative label than the
206 206
 		 * Entity title, add the title itself which is not returned by the api.
207 207
 		 */
208
-		$labels[] = get_the_title( $post_id );
209
-
210
-		// Add some randomness to the label selection.
211
-		shuffle( $labels );
212
-
213
-		// Select the first label which is not to be ignored.
214
-		$title = '';
215
-		foreach ( $labels as $label ) {
216
-			if ( 0 !== strcasecmp( $label, $ignore_label ) ) {
217
-				$title = $label;
218
-				break;
219
-			}
220
-		}
221
-
222
-		return $title;
223
-	}
224
-
225
-	/**
226
-	 * Get the entity URIs (configured in the `itemid` attribute) contained in
227
-	 * the provided content.
228
-	 *
229
-	 * @since 3.14.2
230
-	 *
231
-	 * @param string $content The content.
232
-	 *
233
-	 * @return array An array of URIs.
234
-	 */
235
-	public function get_entity_uris( $content ) {
236
-
237
-		$matches = array();
238
-		preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches );
239
-
240
-		return array_unique( $matches[3] );
241
-	}
208
+        $labels[] = get_the_title( $post_id );
209
+
210
+        // Add some randomness to the label selection.
211
+        shuffle( $labels );
212
+
213
+        // Select the first label which is not to be ignored.
214
+        $title = '';
215
+        foreach ( $labels as $label ) {
216
+            if ( 0 !== strcasecmp( $label, $ignore_label ) ) {
217
+                $title = $label;
218
+                break;
219
+            }
220
+        }
221
+
222
+        return $title;
223
+    }
224
+
225
+    /**
226
+     * Get the entity URIs (configured in the `itemid` attribute) contained in
227
+     * the provided content.
228
+     *
229
+     * @since 3.14.2
230
+     *
231
+     * @param string $content The content.
232
+     *
233
+     * @return array An array of URIs.
234
+     */
235
+    public function get_entity_uris( $content ) {
236
+
237
+        $matches = array();
238
+        preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches );
239
+
240
+        return array_unique( $matches[3] );
241
+    }
242 242
 
243 243
 }
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 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,8 +129,8 @@  discard block
 block discarded – undo
129 129
 		$label     = $matches[4];
130 130
 
131 131
 		// Get the entity post by URI.
132
-		$post = $this->entity_service->get_entity_post_by_uri( $uri );
133
-		if ( null === $post ) {
132
+		$post = $this->entity_service->get_entity_post_by_uri($uri);
133
+		if (null === $post) {
134 134
 
135 135
 			// If the entity post is not found return the label w/o the markup
136 136
 			// around it.
@@ -139,23 +139,23 @@  discard block
 block discarded – undo
139 139
 			return $label;
140 140
 		}
141 141
 
142
-		$no_link = - 1 < strpos( $css_class, 'wl-no-link' );
143
-		$link    = - 1 < strpos( $css_class, 'wl-link' );
142
+		$no_link = - 1 < strpos($css_class, 'wl-no-link');
143
+		$link    = - 1 < strpos($css_class, 'wl-link');
144 144
 
145 145
 		// Don't link if links are disabled and the entity is not link or the
146 146
 		// entity is do not link.
147
-		$dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link;
147
+		$dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link;
148 148
 
149 149
 		// Return the label if it's don't link.
150
-		if ( $dont_link ) {
150
+		if ($dont_link) {
151 151
 			return $label;
152 152
 		}
153 153
 
154 154
 		// Get the link.
155
-		$href = get_permalink( $post );
155
+		$href = get_permalink($post);
156 156
 
157 157
 		// Get an alternative title attribute.
158
-		$title_attribute = $this->get_title_attribute( $post->ID, $label );
158
+		$title_attribute = $this->get_title_attribute($post->ID, $label);
159 159
 
160 160
 		// Return the link.
161 161
 		return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>";
@@ -174,12 +174,12 @@  discard block
 block discarded – undo
174 174
 	 * @return string A `title` attribute with an alternative label or an empty
175 175
 	 *                string if none available.
176 176
 	 */
177
-	private function get_title_attribute( $post_id, $label ) {
177
+	private function get_title_attribute($post_id, $label) {
178 178
 
179 179
 		// Get an alternative title.
180
-		$title = $this->get_link_title( $post_id, $label );
181
-		if ( ! empty( $title ) ) {
182
-			return 'title="' . esc_attr( $title ) . '"';
180
+		$title = $this->get_link_title($post_id, $label);
181
+		if ( ! empty($title)) {
182
+			return 'title="'.esc_attr($title).'"';
183 183
 		}
184 184
 
185 185
 		return '';
@@ -196,24 +196,24 @@  discard block
 block discarded – undo
196 196
 	 * @return string    The title to be used in the link. An empty string when
197 197
 	 *                    there is no alternative that is not the $ignore_label.
198 198
 	 */
199
-	function get_link_title( $post_id, $ignore_label ) {
199
+	function get_link_title($post_id, $ignore_label) {
200 200
 
201 201
 		// Get possible alternative labels we can select from.
202
-		$labels = $this->entity_service->get_alternative_labels( $post_id );
202
+		$labels = $this->entity_service->get_alternative_labels($post_id);
203 203
 
204 204
 		/*
205 205
 		 * Since the original text might use an alternative label than the
206 206
 		 * Entity title, add the title itself which is not returned by the api.
207 207
 		 */
208
-		$labels[] = get_the_title( $post_id );
208
+		$labels[] = get_the_title($post_id);
209 209
 
210 210
 		// Add some randomness to the label selection.
211
-		shuffle( $labels );
211
+		shuffle($labels);
212 212
 
213 213
 		// Select the first label which is not to be ignored.
214 214
 		$title = '';
215
-		foreach ( $labels as $label ) {
216
-			if ( 0 !== strcasecmp( $label, $ignore_label ) ) {
215
+		foreach ($labels as $label) {
216
+			if (0 !== strcasecmp($label, $ignore_label)) {
217 217
 				$title = $label;
218 218
 				break;
219 219
 			}
@@ -232,12 +232,12 @@  discard block
 block discarded – undo
232 232
 	 *
233 233
 	 * @return array An array of URIs.
234 234
 	 */
235
-	public function get_entity_uris( $content ) {
235
+	public function get_entity_uris($content) {
236 236
 
237 237
 		$matches = array();
238
-		preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches );
238
+		preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches);
239 239
 
240
-		return array_unique( $matches[3] );
240
+		return array_unique($matches[3]);
241 241
 	}
242 242
 
243 243
 }
Please login to merge, or discard this patch.