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-assets.php

class-assets.php in Embed Privacy 1.11.2, at inc/embed/class-assets.php

294 lines 8.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 epiphyt\Embed_Privacy\Embed_Privacy;
6 use WP_Post;
7
8 /**
9 * Assets of an embed.
10 *
11 * @author Epiphyt
12 * @license GPL2
13 * @package epiphyt\Embed_Privacy
14 * @since 1.10.0
15 */
16 final class Assets {
17 /**
18 * @var string[] Background image asset data
19 */
20 private $background = [
21 'path' => null,
22 'url' => null,
23 'version' => null,
24 ];
25
26 /**
27 * @var bool Whether debug mode is enabled
28 */
29 private $is_debug_mode = false;
30
31 /**
32 * @var string[] Logo asset data
33 */
34 private $logo = [
35 'path' => null,
36 'url' => null,
37 'version' => null,
38 ];
39
40 /**
41 * @var \epiphyt\Embed_Privacy\embed\Provider Provider object
42 */
43 private $provider;
44
45 /**
46 * @var string[] Thumbnail image asset data
47 */
48 private $thumbnail = [
49 'path' => null,
50 'url' => null,
51 'version' => null,
52 ];
53
54 /**
55 * Construct the object.
56 *
57 * @since 1.11.0 Deprecated second parameter
58 * @since 1.11.0 First parameter must be a provider object
59 *
60 * @param string|\epiphyt\Embed_Privacy\embed\Provider $provider Provider object
61 * @param null $deprecated Deprecated parameter
62 * @param array $attributes Additional embed attributes
63 */
64 public function __construct( $provider, $deprecated = null, $attributes = [] ) {
65 if ( \is_string( $provider ) ) {
66 \_doing_it_wrong(
67 __METHOD__,
68 \sprintf(
69 /* translators: parameter name */
70 \esc_html__( 'Passing a string as parameter %s is deprecated.', 'embed-privacy' ),
71 '$provider'
72 ),
73 '1.11.0'
74 );
75
76 $provider = Providers::get_instance()->get_by_name( $provider );
77 }
78
79 if ( $deprecated !== null ) {
80 \_deprecated_argument( __METHOD__, '1.11.0' );
81 }
82
83 $this->is_debug_mode = \defined( 'WP_DEBUG' ) && \WP_DEBUG || \defined( 'SCRIPT_DEBUG' ) && \SCRIPT_DEBUG;
84 $this->provider = $provider;
85
86 if ( $this->provider->get_post_object() instanceof WP_Post ) {
87 $this->set_background();
88 }
89
90 $this->set_logo();
91 $this->set_thumbnail( $attributes );
92 }
93
94 /**
95 * Get the background image asset data.
96 *
97 * @return string[] Background image asset data
98 */
99 public function get_background() {
100 return $this->background;
101 }
102
103 /**
104 * Get the logo asset data.
105 *
106 * @return string[] Logo asset data
107 */
108 public function get_logo() {
109 return $this->logo;
110 }
111
112 /**
113 * Get static assets.
114 *
115 * @param array $assets List of assets
116 * @param string $provider Provider name
117 * @return string Static assets as HTML
118 */
119 public static function get_static( $assets, $provider = '' ) {
120 $output = '';
121
122 if ( ! empty( $provider ) ) {
123 /**
124 * Filter the additional assets of an embed provider.
125 *
126 * @since 1.4.5
127 *
128 * @param array $assets List of embed assets
129 * @param string $provider The current embed provider in lowercase
130 */
131 $args['assets'] = \apply_filters( "embed_privacy_assets_{$provider}", $assets, $provider );
132 }
133
134 if ( empty( $assets ) ) {
135 return $output;
136 }
137
138 foreach ( \array_reverse( $assets ) as $asset ) {
139 if ( empty( $asset['type'] ) ) {
140 continue;
141 }
142
143 if ( $asset['type'] === 'script' ) {
144 if ( empty( $asset['handle'] ) || empty( $asset['src'] ) ) {
145 continue;
146 }
147
148 $output = '<script src="' . \esc_url( $asset['src'] ) . ( ! empty( $asset['version'] ) ? '?ver=' . \esc_attr( \rawurlencode( $asset['version'] ) ) : '' ) . '" id="' . \esc_attr( $asset['handle'] ) . '"></script>' . \PHP_EOL . $output; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
149 }
150 else if ( $asset['type'] === 'inline' ) {
151 if ( empty( $asset['data'] ) || empty( $asset['object_name'] ) ) {
152 continue;
153 }
154
155 if ( \is_string( $asset['data'] ) ) {
156 $data = \html_entity_decode( $asset['data'], \ENT_QUOTES, 'UTF-8' );
157 }
158 else {
159 foreach ( (array) $asset['data'] as $key => $value ) {
160 if ( ! \is_scalar( $value ) ) {
161 continue;
162 }
163
164 $data[ $key ] = \html_entity_decode( (string) $value, \ENT_QUOTES, 'UTF-8' );
165 }
166 }
167 $output = '<script>var ' . \esc_js( $asset['object_name'] ) . ' = ' . \wp_json_encode( $data ) . ';</script>' . \PHP_EOL . $output;
168 }
169 }
170
171 return $output;
172 }
173
174 /**
175 * Get the thumbnail asset data.
176 *
177 * @return string[] Thumbnail asset data
178 */
179 public function get_thumbnail() {
180 return $this->thumbnail;
181 }
182
183 /**
184 * Set the background image asset data.
185 */
186 private function set_background() {
187 $background_id = \get_post_meta( $this->provider->get_post_object()->ID, 'background_image', true );
188
189 if ( $background_id ) {
190 $this->background['path'] = \get_attached_file( $background_id );
191 $this->background['url'] = \wp_get_attachment_url( $background_id );
192 $this->background['version'] = '';
193 }
194
195 /**
196 * Filter the path to the background image.
197 *
198 * @param string $background_path The current background path
199 * @param string $provider The current embed provider in lowercase
200 */
201 $this->background['path'] = \apply_filters( "embed_privacy_background_path_{$this->provider}", $this->background['path'], $this->provider );
202
203 /**
204 * Filter the URL to the background image.
205 *
206 * @param string $background_url The current background URL
207 * @param string $provider The current embed provider in lowercase
208 */
209 $this->background['url'] = \apply_filters( "embed_privacy_background_url_{$this->provider}", $this->background['url'], $this->provider );
210
211 if ( ! empty( $this->background['path'] ) && \file_exists( $this->background['path'] ) ) {
212 $this->background['version'] = $this->is_debug_mode ? \filemtime( $this->background['path'] ) : \EMBED_PRIVACY_VERSION;
213 }
214 }
215
216 /**
217 * Set the logo asset data.
218 */
219 private function set_logo() {
220 if ( \file_exists( \EPI_EMBED_PRIVACY_BASE . 'assets/images/embed-' . $this->provider . '.png' ) ) {
221 $this->logo['path'] = \EPI_EMBED_PRIVACY_BASE . 'assets/images/embed-' . $this->provider . '.png';
222 $this->logo['url'] = \EPI_EMBED_PRIVACY_URL . 'assets/images/embed-' . $this->provider . '.png';
223 $this->logo['version'] = '';
224 }
225
226 if ( $this->provider->get_post_object() instanceof WP_Post ) {
227 $thumbnail_id = \get_post_thumbnail_id( $this->provider->get_post_object() );
228
229 if ( $thumbnail_id ) {
230 $this->logo['path'] = \get_attached_file( $thumbnail_id );
231 $this->logo['url'] = \get_the_post_thumbnail_url( $this->provider->get_post_object()->ID );
232 }
233 }
234
235 /**
236 * Filter the path to the logo.
237 *
238 * @param string $logo_path The current logo path
239 * @param string $provider The current embed provider in lowercase
240 */
241 $this->logo['path'] = \apply_filters( "embed_privacy_logo_path_{$this->provider}", $this->logo['path'], $this->provider );
242
243 /**
244 * Filter the URL to the logo.
245 *
246 * @param string $logo_url The current logo URL
247 * @param string $provider The current embed provider in lowercase
248 */
249 $this->logo['url'] = \apply_filters( "embed_privacy_logo_url_{$this->provider}", $this->logo['url'], $this->provider );
250
251 if ( ! empty( $this->logo['path'] ) && \file_exists( $this->logo['path'] ) ) {
252 $this->logo['version'] = $this->is_debug_mode ? \filemtime( $this->logo['path'] ) : \EMBED_PRIVACY_VERSION;
253 }
254 }
255
256 /**
257 * Set the thumbnail image asset data.
258 *
259 * @param array $attributes Embed attributes
260 */
261 private function set_thumbnail( $attributes ) {
262 if ( empty( $attributes['embed_url'] ) || ! \get_option( 'embed_privacy_download_thumbnails' ) ) {
263 return;
264 }
265
266 $thumbnail = Embed_Privacy::get_instance()->thumbnail->get_data( \get_post(), $attributes['embed_url'] );
267 $this->thumbnail = [
268 'path' => $thumbnail['thumbnail_path'],
269 'url' => $thumbnail['thumbnail_url'],
270 'version' => '',
271 ];
272
273 /**
274 * Filter the path to the thumbnail.
275 *
276 * @param string $thumbnail_path The current thumbnail path
277 * @param string $provider The current embed provider in lowercase
278 */
279 $this->thumbnail['path'] = \apply_filters( "embed_privacy_thumbnail_path_{$this->provider}", $this->thumbnail['path'], $this->provider );
280
281 /**
282 * Filter the URL to the thumbnail.
283 *
284 * @param string $thumbnail_url The current thumbnail URL
285 * @param string $provider The current embed provider in lowercase
286 */
287 $this->thumbnail['url'] = \apply_filters( "embed_privacy_thumbnail_url_{$this->provider}", $this->thumbnail['url'], $this->provider );
288
289 if ( ! empty( $this->thumbnail['path'] ) && \file_exists( $this->thumbnail['path'] ) ) {
290 $this->thumbnail['version'] = $this->is_debug_mode ? \filemtime( $this->thumbnail['path'] ) : \EMBED_PRIVACY_VERSION;
291 }
292 }
293 }
294