| 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 |
$assets = (array) \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 |
$data = ''; |
| 156 |
|
| 157 |
if ( \is_string( $asset['data'] ) ) { |
| 158 |
$data = \html_entity_decode( $asset['data'], \ENT_QUOTES, 'UTF-8' ); |
| 159 |
} |
| 160 |
else { |
| 161 |
$data = []; |
| 162 |
|
| 163 |
foreach ( (array) $asset['data'] as $key => $value ) { |
| 164 |
if ( ! \is_scalar( $value ) ) { |
| 165 |
continue; |
| 166 |
} |
| 167 |
|
| 168 |
$data[ $key ] = \html_entity_decode( (string) $value, \ENT_QUOTES, 'UTF-8' ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$output = '<script>var ' . \esc_js( $asset['object_name'] ) . ' = ' . \wp_json_encode( $data ) . ';</script>' . \PHP_EOL . $output; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return $output; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Get the thumbnail asset data. |
| 181 |
* |
| 182 |
* @return string[] Thumbnail asset data |
| 183 |
*/ |
| 184 |
public function get_thumbnail() { |
| 185 |
return $this->thumbnail; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Set the background image asset data. |
| 190 |
*/ |
| 191 |
private function set_background() { |
| 192 |
$background_id = \get_post_meta( $this->provider->get_post_object()->ID, 'background_image', true ); |
| 193 |
|
| 194 |
if ( $background_id ) { |
| 195 |
$this->background['path'] = \get_attached_file( $background_id ); |
| 196 |
$this->background['url'] = \wp_get_attachment_url( $background_id ); |
| 197 |
$this->background['version'] = ''; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Filter the path to the background image. |
| 202 |
* |
| 203 |
* @param string $background_path The current background path |
| 204 |
* @param string $provider The current embed provider in lowercase |
| 205 |
*/ |
| 206 |
$this->background['path'] = \apply_filters( "embed_privacy_background_path_{$this->provider}", $this->background['path'], $this->provider ); |
| 207 |
|
| 208 |
/** |
| 209 |
* Filter the URL to the background image. |
| 210 |
* |
| 211 |
* @param string $background_url The current background URL |
| 212 |
* @param string $provider The current embed provider in lowercase |
| 213 |
*/ |
| 214 |
$this->background['url'] = \apply_filters( "embed_privacy_background_url_{$this->provider}", $this->background['url'], $this->provider ); |
| 215 |
|
| 216 |
if ( ! empty( $this->background['path'] ) && \file_exists( $this->background['path'] ) ) { |
| 217 |
$this->background['version'] = $this->is_debug_mode ? \filemtime( $this->background['path'] ) : \EMBED_PRIVACY_VERSION; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Set the logo asset data. |
| 223 |
*/ |
| 224 |
private function set_logo() { |
| 225 |
if ( \file_exists( \EPI_EMBED_PRIVACY_BASE . 'assets/images/embed-' . $this->provider . '.png' ) ) { |
| 226 |
$this->logo['path'] = \EPI_EMBED_PRIVACY_BASE . 'assets/images/embed-' . $this->provider . '.png'; |
| 227 |
$this->logo['url'] = \EPI_EMBED_PRIVACY_URL . 'assets/images/embed-' . $this->provider . '.png'; |
| 228 |
$this->logo['version'] = ''; |
| 229 |
} |
| 230 |
|
| 231 |
if ( $this->provider->get_post_object() instanceof WP_Post ) { |
| 232 |
$thumbnail_id = \get_post_thumbnail_id( $this->provider->get_post_object() ); |
| 233 |
|
| 234 |
if ( $thumbnail_id ) { |
| 235 |
$this->logo['path'] = \get_attached_file( $thumbnail_id ); |
| 236 |
$this->logo['url'] = \get_the_post_thumbnail_url( $this->provider->get_post_object()->ID ); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Filter the path to the logo. |
| 242 |
* |
| 243 |
* @param string $logo_path The current logo path |
| 244 |
* @param string $provider The current embed provider in lowercase |
| 245 |
*/ |
| 246 |
$this->logo['path'] = \apply_filters( "embed_privacy_logo_path_{$this->provider}", $this->logo['path'], $this->provider ); |
| 247 |
|
| 248 |
/** |
| 249 |
* Filter the URL to the logo. |
| 250 |
* |
| 251 |
* @param string $logo_url The current logo URL |
| 252 |
* @param string $provider The current embed provider in lowercase |
| 253 |
*/ |
| 254 |
$this->logo['url'] = \apply_filters( "embed_privacy_logo_url_{$this->provider}", $this->logo['url'], $this->provider ); |
| 255 |
|
| 256 |
if ( ! empty( $this->logo['path'] ) && \file_exists( $this->logo['path'] ) ) { |
| 257 |
$this->logo['version'] = $this->is_debug_mode ? \filemtime( $this->logo['path'] ) : \EMBED_PRIVACY_VERSION; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Set the thumbnail image asset data. |
| 263 |
* |
| 264 |
* @param array $attributes Embed attributes |
| 265 |
*/ |
| 266 |
private function set_thumbnail( $attributes ) { |
| 267 |
if ( empty( $attributes['embed_url'] ) || ! \get_option( 'embed_privacy_download_thumbnails' ) ) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
$thumbnail = Embed_Privacy::get_instance()->thumbnail->get_data( \get_post(), $attributes['embed_url'] ); |
| 272 |
$this->thumbnail = [ |
| 273 |
'path' => $thumbnail['thumbnail_path'], |
| 274 |
'url' => $thumbnail['thumbnail_url'], |
| 275 |
'version' => '', |
| 276 |
]; |
| 277 |
|
| 278 |
/** |
| 279 |
* Filter the path to the thumbnail. |
| 280 |
* |
| 281 |
* @param string $thumbnail_path The current thumbnail path |
| 282 |
* @param string $provider The current embed provider in lowercase |
| 283 |
*/ |
| 284 |
$this->thumbnail['path'] = \apply_filters( "embed_privacy_thumbnail_path_{$this->provider}", $this->thumbnail['path'], $this->provider ); |
| 285 |
|
| 286 |
/** |
| 287 |
* Filter the URL to the thumbnail. |
| 288 |
* |
| 289 |
* @param string $thumbnail_url The current thumbnail URL |
| 290 |
* @param string $provider The current embed provider in lowercase |
| 291 |
*/ |
| 292 |
$this->thumbnail['url'] = \apply_filters( "embed_privacy_thumbnail_url_{$this->provider}", $this->thumbnail['url'], $this->provider ); |
| 293 |
|
| 294 |
if ( ! empty( $this->thumbnail['path'] ) && \file_exists( $this->thumbnail['path'] ) ) { |
| 295 |
$this->thumbnail['version'] = $this->is_debug_mode ? \filemtime( $this->thumbnail['path'] ) : \EMBED_PRIVACY_VERSION; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|