| 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 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Filter the path to the background image. |
| 180 |
* |
| 181 |
* @param string $background_path The current background path |
| 182 |
* @param string $provider The current embed provider in lowercase |
| 183 |
*/ |
| 184 |
$this->background['path'] = \apply_filters( "embed_privacy_background_path_{$this->provider}", $this->background['path'], $this->provider ); |
| 185 |
|
| 186 |
/** |
| 187 |
* Filter the URL to the background image. |
| 188 |
* |
| 189 |
* @param string $background_url The current background URL |
| 190 |
* @param string $provider The current embed provider in lowercase |
| 191 |
*/ |
| 192 |
$this->background['url'] = \apply_filters( "embed_privacy_background_url_{$this->provider}", $this->background['url'], $this->provider ); |
| 193 |
|
| 194 |
if ( ! empty( $this->background['path'] ) && \file_exists( $this->background['path'] ) ) { |
| 195 |
$this->background['version'] = $this->is_debug_mode ? \filemtime( $this->background['path'] ) : \EMBED_PRIVACY_VERSION; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Set the logo asset data. |
| 201 |
*/ |
| 202 |
private function set_logo() { |
| 203 |
if ( \file_exists( \EPI_EMBED_PRIVACY_BASE . 'assets/images/embed-' . $this->provider . '.png' ) ) { |
| 204 |
$this->logo['path'] = \EPI_EMBED_PRIVACY_BASE . 'assets/images/embed-' . $this->provider . '.png'; |
| 205 |
$this->logo['url'] = \EPI_EMBED_PRIVACY_URL . 'assets/images/embed-' . $this->provider . '.png'; |
| 206 |
} |
| 207 |
|
| 208 |
if ( $this->embed_post instanceof WP_Post ) { |
| 209 |
$thumbnail_id = \get_post_thumbnail_id( $this->embed_post ); |
| 210 |
|
| 211 |
if ( $thumbnail_id ) { |
| 212 |
$this->logo['path'] = \get_attached_file( $thumbnail_id ); |
| 213 |
$this->logo['url'] = \get_the_post_thumbnail_url( $this->embed_post->ID ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Filter the path to the logo. |
| 219 |
* |
| 220 |
* @param string $logo_path The current logo path |
| 221 |
* @param string $provider The current embed provider in lowercase |
| 222 |
*/ |
| 223 |
$this->logo['path'] = \apply_filters( "embed_privacy_logo_path_{$this->provider}", $this->logo['path'], $this->provider ); |
| 224 |
|
| 225 |
/** |
| 226 |
* Filter the URL to the logo. |
| 227 |
* |
| 228 |
* @param string $logo_url The current logo URL |
| 229 |
* @param string $provider The current embed provider in lowercase |
| 230 |
*/ |
| 231 |
$this->logo['url'] = \apply_filters( "embed_privacy_logo_url_{$this->provider}", $this->logo['url'], $this->provider ); |
| 232 |
|
| 233 |
if ( ! empty( $this->logo['path'] ) && \file_exists( $this->logo['path'] ) ) { |
| 234 |
$this->logo['version'] = $this->is_debug_mode ? \filemtime( $this->logo['path'] ) : \EMBED_PRIVACY_VERSION; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Set the thumbnail image asset data. |
| 240 |
* |
| 241 |
* @param array $attributes Embed attributes |
| 242 |
*/ |
| 243 |
private function set_thumbnail( $attributes ) { |
| 244 |
if ( empty( $attributes['embed_url'] ) || ! \get_option( 'embed_privacy_download_thumbnails' ) ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
$thumbnail = Embed_Privacy::get_instance()->thumbnail->get_data( \get_post(), $attributes['embed_url'] ); |
| 249 |
$this->thumbnail = [ |
| 250 |
'path' => $thumbnail['thumbnail_path'], |
| 251 |
'url' => $thumbnail['thumbnail_url'], |
| 252 |
]; |
| 253 |
|
| 254 |
/** |
| 255 |
* Filter the path to the thumbnail. |
| 256 |
* |
| 257 |
* @param string $thumbnail_path The current thumbnail path |
| 258 |
* @param string $provider The current embed provider in lowercase |
| 259 |
*/ |
| 260 |
$this->thumbnail['path'] = \apply_filters( "embed_privacy_thumbnail_path_{$this->provider}", $this->thumbnail['path'], $this->provider ); |
| 261 |
|
| 262 |
/** |
| 263 |
* Filter the URL to the thumbnail. |
| 264 |
* |
| 265 |
* @param string $thumbnail_url The current thumbnail URL |
| 266 |
* @param string $provider The current embed provider in lowercase |
| 267 |
*/ |
| 268 |
$this->thumbnail['url'] = \apply_filters( "embed_privacy_thumbnail_url_{$this->provider}", $this->thumbnail['url'], $this->provider ); |
| 269 |
|
| 270 |
if ( ! empty( $this->thumbnail['path'] ) && \file_exists( $this->thumbnail['path'] ) ) { |
| 271 |
$this->thumbnail['version'] = $this->is_debug_mode ? \filemtime( $this->thumbnail['path'] ) : \EMBED_PRIVACY_VERSION; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|