| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\embed; |
| 3 |
|
| 4 |
use epiphyt\Embed_Privacy\data\Providers; |
| 5 |
use epiphyt\Embed_Privacy\Embed_Privacy; |
| 6 |
|
| 7 |
/** |
| 8 |
* Embed Privacy template related functionality. |
| 9 |
* |
| 10 |
* @author Epiphyt |
| 11 |
* @license GPL2 |
| 12 |
* @package epiphyt\Embed_Privacy |
| 13 |
* @since 1.10.0 |
| 14 |
*/ |
| 15 |
final class Template { |
| 16 |
/** |
| 17 |
* Get an overlay template. |
| 18 |
* |
| 19 |
* @param \epiphyt\Embed_Privacy\embed\Provider|string $provider The embed provider |
| 20 |
* @param string $output The output before replacing it |
| 21 |
* @param array $attributes Additional attributes |
| 22 |
* @return string The overlay template |
| 23 |
*/ |
| 24 |
public static function get( $provider, $output, $attributes = [] ) { |
| 25 |
if ( \is_string( $provider ) ) { |
| 26 |
\_doing_it_wrong( |
| 27 |
__METHOD__, |
| 28 |
\sprintf( |
| 29 |
/* translators: alternative method */ |
| 30 |
\esc_html__( 'Providing a string as parameter %1$s is deprecated. Use an object of type %2$s instead.', 'embed-privacy' ), |
| 31 |
'$provider', |
| 32 |
'epiphyt\Embed_Privacy\embed\Provider' |
| 33 |
), |
| 34 |
'1.10.0' |
| 35 |
); |
| 36 |
|
| 37 |
$provider = Providers::get_instance()->get_by_name( Providers::sanitize_name( $provider ) ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Filter the overlay arguments. |
| 42 |
* |
| 43 |
* @deprecated 1.10.0 Use embed_privacy_template_arguments instead |
| 44 |
* @since 1.9.0 |
| 45 |
* |
| 46 |
* @param array $attributes Template arguments |
| 47 |
* @param string $provider The embed provider |
| 48 |
* @param string $provider_name The embed provider without spaces and in lowercase |
| 49 |
* @param string $output The output before replacing it |
| 50 |
*/ |
| 51 |
$attributes = (array) \apply_filters_deprecated( |
| 52 |
'embed_privacy_overlay_args', |
| 53 |
[ |
| 54 |
$attributes, |
| 55 |
$provider, |
| 56 |
$provider->get_name(), |
| 57 |
$output, |
| 58 |
], |
| 59 |
'1.10.0', |
| 60 |
'embed_privacy_template_arguments' |
| 61 |
); |
| 62 |
|
| 63 |
/** |
| 64 |
* Filter the template attributes. |
| 65 |
* |
| 66 |
* @since 1.10.0 |
| 67 |
* |
| 68 |
* @param array $attributes Template attributes |
| 69 |
* @param string $provider The embed provider |
| 70 |
* @param string $output The output before replacing it |
| 71 |
*/ |
| 72 |
$attributes = (array) \apply_filters( 'embed_privacy_template_attributes', $attributes, $provider, $output ); |
| 73 |
|
| 74 |
if ( ! empty( $attributes['post_id'] ) ) { |
| 75 |
$embed_post = \get_post( $attributes['post_id'] ); |
| 76 |
|
| 77 |
if ( Providers::is_disabled( $embed_post ) ) { |
| 78 |
return $output; |
| 79 |
} |
| 80 |
} |
| 81 |
else if ( ! empty( $attributes['provider'] ) ) { |
| 82 |
if ( $attributes['provider'] instanceof Provider && $attributes['provider']->is_disabled() ) { |
| 83 |
return $output; |
| 84 |
} |
| 85 |
} |
| 86 |
else { |
| 87 |
$embed_post = null; |
| 88 |
} |
| 89 |
|
| 90 |
if ( $provider->is( 'youtube' ) ) { |
| 91 |
$output = \str_replace( 'youtube.com', 'youtube-nocookie.com', $output ); |
| 92 |
} |
| 93 |
|
| 94 |
$embed_class = 'embed-' . ( ! empty( $provider->get_name() ) ? $provider->get_name() : 'default' ); |
| 95 |
$embed_classes = $embed_class; |
| 96 |
$style = new Style( $provider->get_name(), $embed_post, $attributes ); |
| 97 |
|
| 98 |
if ( ! empty( $attributes['align'] ) ) { |
| 99 |
$embed_classes .= ' align' . $attributes['align']; |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! empty( $attributes['assets'] ) ) { |
| 103 |
$output = Assets::get_static( $attributes['assets'], $provider->get_name() ) . $output; |
| 104 |
} |
| 105 |
|
| 106 |
$embed_md5 = \md5( $output . \wp_generate_uuid4() ); |
| 107 |
$checkbox_id = 'embed-privacy-store-' . $provider->get_name() . '-' . $embed_md5; |
| 108 |
|
| 109 |
/** |
| 110 |
* Fires before the template output is generated. |
| 111 |
* |
| 112 |
* @since 1.10.0 |
| 113 |
* |
| 114 |
* @param \epiphyt\Embed_Privacy\embed\Provider $provider The embed provider |
| 115 |
* @param \epiphyt\Embed_Privacy\embed\Style $style The overlay style object |
| 116 |
* @param array $attributes Additional attributes |
| 117 |
*/ |
| 118 |
\do_action( 'embed_privacy_before_template_output', $provider, $style, $attributes ); |
| 119 |
|
| 120 |
\ob_start(); |
| 121 |
?> |
| 122 |
<p> |
| 123 |
<?php |
| 124 |
if ( ! empty( $provider->get_name() ) ) { |
| 125 |
if ( $embed_post || ! empty( $provider->get_description() ) ) { |
| 126 |
$allowed_tags = [ |
| 127 |
'a' => [ |
| 128 |
'href', |
| 129 |
'target', |
| 130 |
], |
| 131 |
]; |
| 132 |
|
| 133 |
if ( $embed_post ) { |
| 134 |
echo $embed_post->post_content . \PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 135 |
$privacy_policy = \get_post_meta( $embed_post->ID, 'privacy_policy_url', true ); |
| 136 |
} |
| 137 |
else { |
| 138 |
echo $provider->get_description() . \PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 139 |
$privacy_policy = $provider->get_privacy_policy_url(); |
| 140 |
} |
| 141 |
|
| 142 |
if ( $privacy_policy ) { |
| 143 |
?> |
| 144 |
<br> |
| 145 |
<?php |
| 146 |
/* translators: 1: embed provider, 2: opening <a> tag to the privacy policy, 3: closing </a> */ |
| 147 |
\printf( \wp_kses( \__( 'Learn more in %1$s’s %2$sprivacy policy%3$s.', 'embed-privacy' ), $allowed_tags ), \esc_html( $provider->get_title() ), '<a href="' . \esc_url( $privacy_policy ) . '" target="_blank">', '</a>' ); |
| 148 |
} |
| 149 |
} |
| 150 |
else { |
| 151 |
/* translators: embed provider */ |
| 152 |
\printf( \esc_html__( 'Click here to display content from %s', 'embed-privacy' ), \esc_html( $provider->get_title() ) ); |
| 153 |
} |
| 154 |
} |
| 155 |
else { |
| 156 |
\esc_html_e( 'Click here to display content from an external service.', 'embed-privacy' ); |
| 157 |
} |
| 158 |
?> |
| 159 |
</p> |
| 160 |
<p class="embed-privacy-input-wrapper"> |
| 161 |
<input id="<?php echo \esc_attr( $checkbox_id ); ?>" type="checkbox" value="1" class="embed-privacy-input" data-embed-provider="<?php echo \esc_attr( $provider->get_name() ); ?>"> |
| 162 |
<label for="<?php echo \esc_attr( $checkbox_id ); ?>" class="embed-privacy-label" data-embed-provider="<?php echo \esc_attr( $provider->get_name() ); ?>"> |
| 163 |
<?php |
| 164 |
/* translators: the embed provider */ |
| 165 |
\printf( \esc_html__( 'Always display content from %s', 'embed-privacy' ), \esc_html( $provider->get_title() ) ); |
| 166 |
?> |
| 167 |
</label> |
| 168 |
</p> |
| 169 |
<?php |
| 170 |
$content = \ob_get_clean(); |
| 171 |
|
| 172 |
/** |
| 173 |
* Filter the content of the embed overlay. |
| 174 |
* |
| 175 |
* @deprecated 1.10.0 Use embed_privacy_template_content instead |
| 176 |
* |
| 177 |
* @param string $content The content |
| 178 |
* @param string $provider The embed provider of this embed |
| 179 |
*/ |
| 180 |
$content = \apply_filters_deprecated( |
| 181 |
'embed_privacy_content', |
| 182 |
[ |
| 183 |
$content, |
| 184 |
$provider->get_title(), |
| 185 |
], |
| 186 |
'1.10.0', |
| 187 |
'embed_privacy_template_content' |
| 188 |
); |
| 189 |
|
| 190 |
/** |
| 191 |
* Filter the content of the embed overlay. |
| 192 |
* |
| 193 |
* @since 1.10.0 |
| 194 |
* |
| 195 |
* @param string $content The content |
| 196 |
* @param string $provider The embed provider of this embed |
| 197 |
*/ |
| 198 |
$content = \apply_filters( 'embed_privacy_template_content', $content, $provider ); |
| 199 |
|
| 200 |
\ob_start(); |
| 201 |
|
| 202 |
$footer_content = ''; |
| 203 |
|
| 204 |
if ( ! empty( $attributes['embed_url'] ) ) { |
| 205 |
$footer_content = '<div class="embed-privacy-footer">'; |
| 206 |
|
| 207 |
if ( ! \get_option( 'embed_privacy_disable_link' ) ) { |
| 208 |
$footer_content .= '<span class="embed-privacy-url"><a href="' . \esc_url( $attributes['embed_url'] ) . '">'; |
| 209 |
$footer_content .= \sprintf( |
| 210 |
/* translators: content name or 'content' */ |
| 211 |
\esc_html__( 'Open "%s" directly', 'embed-privacy' ), |
| 212 |
! empty( $attributes['embed_title'] ) ? $attributes['embed_title'] : \__( 'content', 'embed-privacy' ) |
| 213 |
); |
| 214 |
$footer_content .= '</a></span>'; |
| 215 |
} |
| 216 |
|
| 217 |
$footer_content .= '</div>' . \PHP_EOL; |
| 218 |
|
| 219 |
/** |
| 220 |
* Filter the overlay footer. |
| 221 |
* |
| 222 |
* @param string $footer_content The footer content |
| 223 |
*/ |
| 224 |
$footer_content = \apply_filters( 'embed_privacy_overlay_footer', $footer_content ); |
| 225 |
} |
| 226 |
|
| 227 |
$container_style = $style->get( 'container' ); |
| 228 |
$logo_style = $style->get( 'logo' ); |
| 229 |
?> |
| 230 |
<div class="embed-privacy-container is-disabled <?php echo \esc_attr( $embed_classes ); ?>" data-embed-id="oembed_<?php echo \esc_attr( $embed_md5 ); ?>" data-embed-provider="<?php echo \esc_attr( $provider->get_name() ); ?>"<?php echo ! empty( $container_style ) ? ' style="' . \esc_attr( $container_style ) . '"' : ''; ?>> |
| 231 |
<?php |
| 232 |
/* translators: embed provider */ |
| 233 |
$button_text = \sprintf( \__( 'Display content from %s', 'embed-privacy' ), \esc_html( $provider->get_title() ) ); |
| 234 |
|
| 235 |
if ( ! empty( $attributes['embed_title'] ) ) { |
| 236 |
/* translators: 1: embed title, 2: embed provider */ |
| 237 |
$button_text = \sprintf( \__( 'Display "%1$s" from %2$s', 'embed-privacy' ), $attributes['embed_title'], \esc_html( $provider->get_title() ) ); |
| 238 |
} |
| 239 |
?> |
| 240 |
<button class="embed-privacy-enable screen-reader-text"><?php echo \esc_html( $button_text ); ?></button> |
| 241 |
|
| 242 |
<div class="embed-privacy-overlay"> |
| 243 |
<div class="embed-privacy-inner"> |
| 244 |
<?php |
| 245 |
echo ! empty( $logo_style ) ? '<div class="embed-privacy-logo" style="' . \esc_attr( $logo_style ) . '"></div>' . \PHP_EOL : ''; |
| 246 |
echo $content . \PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 247 |
?> |
| 248 |
</div> |
| 249 |
|
| 250 |
<?php echo $footer_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 251 |
</div> |
| 252 |
|
| 253 |
<div class="embed-privacy-content"> |
| 254 |
<script>var _oembed_<?php echo $embed_md5; ?> = '<?php echo \addslashes( \wp_json_encode( [ 'embed' => \htmlentities( \preg_replace( '/\s+/S', ' ', $output ) ) ] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>';</script> |
| 255 |
</div> |
| 256 |
</div> |
| 257 |
<?php |
| 258 |
$markup = \ob_get_clean(); |
| 259 |
|
| 260 |
/** |
| 261 |
* Filter the complete markup of the embed. |
| 262 |
* |
| 263 |
* @deprecated 1.10.0 Use embed_privacy_template_markup instead |
| 264 |
* |
| 265 |
* @param string $markup The markup |
| 266 |
* @param string $provider_name The embed provider name of this embed |
| 267 |
*/ |
| 268 |
$markup = \apply_filters_deprecated( |
| 269 |
'embed_privacy_markup', |
| 270 |
[ |
| 271 |
$markup, |
| 272 |
$provider->get_title(), |
| 273 |
], |
| 274 |
'1.10.0', |
| 275 |
'embed_privacy_template_markup' |
| 276 |
); |
| 277 |
|
| 278 |
/** |
| 279 |
* Filter the complete markup of the embed. |
| 280 |
* |
| 281 |
* @since 1.10.0 |
| 282 |
* |
| 283 |
* @param string $markup The markup |
| 284 |
* @param \epiphyt\Embed_Privacy\embed\Provider $provider The embed provider of this embed |
| 285 |
*/ |
| 286 |
$markup = \apply_filters( 'embed_privacy_template_markup', $markup, $provider ); |
| 287 |
|
| 288 |
Embed_Privacy::get_instance()->has_embed = true; |
| 289 |
|
| 290 |
if ( ! empty( $attributes['strip_newlines'] ) ) { |
| 291 |
$markup = \str_replace( \PHP_EOL, '', $markup ); |
| 292 |
} |
| 293 |
|
| 294 |
return $markup; |
| 295 |
} |
| 296 |
} |
| 297 |
|