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

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