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

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

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