PluginProbe
Embed Privacy / 1.11.2
Embed Privacy v1.11.2
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 1.11.2, at inc/embed/class-provider.php

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