PluginProbe
Embed Privacy / trunk
Embed Privacy vtrunk
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / embed / class-provider.php

class-provider.php in Embed Privacy trunk, at inc/embed/class-provider.php

387 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace epiphyt\Embed_Privacy\embed;
3
4 use epiphyt\Embed_Privacy\data\Providers;
5 use epiphyt\Embed_Privacy\data\Replacer;
6 use WP_Post;
7
8 /**
9 * Embed provider representation.
10 *
11 * @author Epiphyt
12 * @license GPL2
13 * @package epiphyt\Embed_Privacy
14 * @since 1.10.0
15 */
16 final class Provider {
17 /**
18 * @var int|null Background image ID
19 */
20 private $background_image_id = null;
21
22 /**
23 * @var string Name of a content item
24 */
25 private $content_name = '';
26
27 /**
28 * @var string The description
29 */
30 private $description = '';
31
32 /**
33 * @var bool Whether the provider is disabled
34 */
35 private $disabled = false;
36
37 /**
38 * @var string Provider name
39 */
40 private $name = '';
41
42 /**
43 * @var \WP_Post|null Provider post object
44 */
45 private $post_object = null;
46
47 /**
48 * @var string Regular expression pattern
49 */
50 private $pattern = '';
51
52 /**
53 * @since 1.12.0
54 * @var string Regular expression pattern
55 */
56 private $pattern_extended = '';
57
58 /**
59 * @var string Privacy policy URL
60 */
61 private $privacy_policy_url = '';
62
63 /**
64 * @var bool Whether the provider is a system provider
65 */
66 private $system = false;
67
68 /**
69 * @var int|null Thumbnail ID
70 */
71 private $thumbnail_id = null;
72
73 /**
74 * @var string Title
75 */
76 private $title = '';
77
78 /**
79 * @var bool Whether the current provider is unknown
80 */
81 private $unknown = false;
82
83 /**
84 * Provider constructor
85 *
86 * @param \WP_Post $provider_object Provider post object
87 */
88 public function __construct( $provider_object = null ) {
89 $this->set_post_object( $provider_object );
90
91 if ( $provider_object instanceof WP_Post ) {
92 $this->set_name( Providers::sanitize_name( $provider_object->post_name ) );
93 $this->set_title( $provider_object->post_title );
94 $this->set_is_system( \get_post_meta( $provider_object->ID, 'is_system', true ) );
95 $this->set_is_disabled( Providers::is_disabled( $provider_object ) );
96 $this->set_pattern( \get_post_meta( $provider_object->ID, 'regex_default', true ) );
97 $this->set_description( $provider_object->post_content );
98 $this->set_privacy_policy_url( \get_post_meta( $provider_object->ID, 'privacy_policy_url', true ) );
99 $this->set_background_image_id( \get_post_meta( $provider_object->ID, 'background_image', true ) );
100 $this->set_thumbnail_id( \get_post_thumbnail_id( $provider_object ) );
101 $this->set_content_name( \get_post_meta( $provider_object->ID, 'content_item_name', true ) );
102 $this->extend_pattern();
103 }
104 else {
105 $this->set_is_unknown( true );
106 }
107 }
108
109 /**
110 * String representation of the provider.
111 *
112 * @since 1.11.0
113 *
114 * @return string Provider name
115 */
116 public function __toString() {
117 return $this->get_name();
118 }
119
120 /**
121 * Extend the pattern.
122 *
123 * @since 1.12.0
124 */
125 private function extend_pattern() {
126 $this->pattern_extended = Replacer::extend_pattern( $this->pattern, $this );
127 }
128
129 /**
130 * Get the background image ID.
131 *
132 * @return int|null Background image ID or null
133 */
134 public function get_background_image_id() {
135 return $this->background_image_id;
136 }
137
138 /**
139 * Get the name of a content item.
140 *
141 * @return string The content name
142 */
143 public function get_content_name() {
144 return $this->content_name;
145 }
146
147 /**
148 * Get the description.
149 *
150 * @return string The description
151 */
152 public function get_description() {
153 return $this->description;
154 }
155
156 /**
157 * Get the name.
158 *
159 * @return string Provider name
160 */
161 public function get_name() {
162 return $this->name;
163 }
164
165 /**
166 * Get the pattern.
167 *
168 * @param string $type Type of the pattern, 'default' or 'extended'
169 * @return string Regular expression pattern
170 */
171 public function get_pattern( $type = 'default' ) {
172 if ( $type === 'extended' ) {
173 return $this->pattern_extended;
174 }
175
176 return $this->pattern;
177 }
178
179 /**
180 * Get the post object.
181 *
182 * @return \WP_Post|null Post object or null
183 */
184 public function get_post_object() {
185 return $this->post_object;
186 }
187
188 /**
189 * Get the privacy policy URL.
190 *
191 * @return string Privacy policy URL
192 */
193 public function get_privacy_policy_url() {
194 return $this->privacy_policy_url;
195 }
196
197 /**
198 * Get the thumbnail ID.
199 *
200 * @return int|null Thumbnail ID or null
201 */
202 public function get_thumbnail_id() {
203 return $this->thumbnail_id;
204 }
205
206 /**
207 * Get the title.
208 *
209 * @return string Title
210 */
211 public function get_title() {
212 return $this->title;
213 }
214
215 /**
216 * Set the background image ID.
217 *
218 * @param int|null $background_image_id Background image ID or null
219 */
220 public function set_background_image_id( $background_image_id ) {
221 $this->background_image_id = $background_image_id;
222 }
223
224 /**
225 * Whether the provider has a certain name.
226 *
227 * @param string $name Name to check
228 * @return bool Whether the provider has the name to check
229 */
230 public function is( $name ) {
231 return $name === $this->name || $name === $this->title;
232 }
233
234 /**
235 * Whether the provider is disabled or not.
236 *
237 * @return bool Whether the provider is disabled
238 */
239 public function is_disabled() {
240 return $this->disabled;
241 }
242
243 /**
244 * Whether the provider is a system provider or not.
245 *
246 * @return bool Whether the provider is a system provider
247 */
248 public function is_system() {
249 return $this->system;
250 }
251
252 /**
253 * Whether the provider is unknown or not.
254 *
255 * @return bool Whether the provider is unknown
256 */
257 public function is_unknown() {
258 return $this->unknown;
259 }
260
261 /**
262 * Whether the provider is matching the current content.
263 *
264 * @param string $content Content to check
265 * @param string $pattern Optional alternative pattern
266 * @return bool Whether the provider is matching the current content
267 */
268 public function is_matching( $content, $pattern = '' ) {
269 $used_pattern = $pattern ?: $this->pattern;
270
271 return (bool) ! empty( $used_pattern ) && \preg_match( $used_pattern, $content );
272 }
273
274 /**
275 * Set the content item name.
276 *
277 * @param string $content_name Content name
278 */
279 public function set_content_name( $content_name ) {
280 $this->content_name = $content_name;
281 }
282
283 /**
284 * Set the description.
285 *
286 * @param string $description Description
287 */
288 public function set_description( $description ) {
289 $this->description = $description;
290 }
291
292 /**
293 * Set the disabled state.
294 *
295 * @param bool $disabled Whether this provider is disabled
296 */
297 public function set_is_disabled( $disabled ) {
298 $this->disabled = $disabled;
299 }
300
301 /**
302 * Set the system state.
303 *
304 * @param bool $system Whether this provider is a system provider
305 */
306 public function set_is_system( $system ) {
307 $this->system = (bool) $system;
308 }
309
310 /**
311 * Set the unknown state.
312 *
313 * @param bool $unknown Whether this provider is unknown
314 */
315 public function set_is_unknown( $unknown ) {
316 $this->unknown = (bool) $unknown;
317 }
318
319 /**
320 * Set the name.
321 *
322 * @param string $name Name
323 */
324 public function set_name( $name ) {
325 /**
326 * Filter the embed provider name.
327 *
328 * @since 1.10.0
329 *
330 * @param string $name Embed provider name
331 * @param string $provider Embed provider
332 */
333 $name = \apply_filters( 'embed_privacy_provider_name', $name, $this );
334
335 $this->name = $name;
336 }
337
338 /**
339 * Set the pattern.
340 *
341 * @param string $pattern Regular expression pattern
342 */
343 public function set_pattern( $pattern ) {
344 $this->pattern = \trim( $pattern, '/' );
345
346 if ( ! empty( $this->pattern ) ) {
347 $this->pattern = '/' . $this->pattern . '/';
348 }
349 }
350
351 /**
352 * Set the post object.
353 *
354 * @param string $post_object Post object
355 */
356 public function set_post_object( $post_object ) {
357 $this->post_object = $post_object;
358 }
359
360 /**
361 * Set the privacy policy URL.
362 *
363 * @param string $privacy_policy_url URL to the privacy policy
364 */
365 public function set_privacy_policy_url( $privacy_policy_url ) {
366 $this->privacy_policy_url = $privacy_policy_url;
367 }
368
369 /**
370 * Set the thumbnail_id.
371 *
372 * @param int|null $thumbnail_id Thumbnail ID or null
373 */
374 public function set_thumbnail_id( $thumbnail_id ) {
375 $this->thumbnail_id = $thumbnail_id;
376 }
377
378 /**
379 * Set the title.
380 *
381 * @param string $title Title
382 */
383 public function set_title( $title ) {
384 $this->title = $title;
385 }
386 }
387