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

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