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

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

326 lines 10.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
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 ( ! $provider instanceof 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_attributes 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_attributes'
61 );
62
63 /**
64 * Filter the template attributes.
65 *
66 * @since 1.10.0
67 *
68 * @param array $attributes Template attributes
69 * @param \epiphyt\Embed_privacy\embed\Provider $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 = $provider->get_post_object();
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, null, $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 %2$s%1$s’s privacy 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 .= self::get_embed_link_markup( $attributes, $provider );
209 }
210
211 $footer_content .= '</div>' . \PHP_EOL;
212
213 /**
214 * Filter the overlay footer.
215 *
216 * @param string $footer_content The footer content
217 */
218 $footer_content = \apply_filters( 'embed_privacy_overlay_footer', $footer_content );
219 }
220
221 $container_style = $style->get( 'container' );
222 $logo_style = $style->get( 'logo' );
223 ?>
224 <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 ) . '"' : ''; ?>>
225 <?php
226 /* translators: embed provider */
227 $button_text = \sprintf( \__( 'Display content from %s', 'embed-privacy' ), \esc_html( $provider->get_title() ) );
228
229 if ( ! empty( $attributes['embed_title'] ) ) {
230 /* translators: 1: embed title, 2: embed provider */
231 $button_text = \sprintf( \__( 'Display "%1$s" from %2$s', 'embed-privacy' ), $attributes['embed_title'], \esc_html( $provider->get_title() ) );
232 }
233 ?>
234 <button type="button" class="embed-privacy-enable screen-reader-text"><?php echo \esc_html( $button_text ); ?></button>
235
236 <div class="embed-privacy-overlay">
237 <div class="embed-privacy-inner">
238 <?php
239 echo ! empty( $logo_style ) ? '<div class="embed-privacy-logo" style="' . \esc_attr( $logo_style ) . '"></div>' . \PHP_EOL : '';
240 echo $content . \PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
241 ?>
242 </div>
243
244 <?php echo $footer_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
245 </div>
246
247 <div class="embed-privacy-content">
248 <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>
249 </div>
250 </div>
251 <?php
252 $markup = \ob_get_clean();
253
254 /**
255 * Filter the complete markup of the embed.
256 *
257 * @deprecated 1.10.0 Use embed_privacy_template_markup instead
258 *
259 * @param string $markup The markup
260 * @param string $provider_name The embed provider name of this embed
261 */
262 $markup = \apply_filters_deprecated(
263 'embed_privacy_markup',
264 [
265 $markup,
266 $provider->get_title(),
267 ],
268 '1.10.0',
269 'embed_privacy_template_markup'
270 );
271
272 /**
273 * Filter the complete markup of the embed.
274 *
275 * @since 1.10.0
276 * @since 1.11.2 Added $attributes parameter
277 *
278 * @param string $markup The markup
279 * @param \epiphyt\Embed_Privacy\embed\Provider $provider The embed provider of this embed
280 * @param mixed[] $attributes Embed attributes
281 */
282 $markup = \apply_filters( 'embed_privacy_template_markup', $markup, $provider, $attributes );
283
284 Embed_Privacy::get_instance()->has_embed = true;
285
286 if ( ! empty( $attributes['strip_newlines'] ) ) {
287 $markup = \str_replace( \PHP_EOL, '', $markup );
288 }
289
290 return $markup;
291 }
292
293 /**
294 * Get the embed link markup.
295 *
296 * @param mixed[] $attributes Embed attributes
297 * @param \epiphyt\Embed_Privacy\embed\Provider $provider The embed provider of this embed
298 * @return string Embed link markup
299 */
300 private static function get_embed_link_markup( $attributes, $provider ) {
301 if ( ! empty( $attributes['embed_title'] ) ) {
302 $footer_link_title = \sprintf(
303 /* translators: content name */
304 \esc_html__( 'Open "%s" directly', 'embed-privacy' ),
305 $attributes['embed_title']
306 );
307 }
308 else if ( ! empty( $provider->get_content_name() ) ) {
309 $footer_link_title = \sprintf(
310 /* translators: content name */
311 \esc_html__( 'Open %s directly', 'embed-privacy' ),
312 $provider->get_content_name()
313 );
314 }
315 else {
316 $footer_link_title = \esc_html__( 'Open content directly', 'embed-privacy' );
317 }
318
319 return \sprintf(
320 '<span class="embed-privacy-url"><a href="%1$s">%2$s</a></span>',
321 \esc_url( $attributes['embed_url'] ),
322 $footer_link_title
323 );
324 }
325 }
326