PluginProbe
Embed Privacy / 1.11.0
Embed Privacy v1.11.0
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.0, at inc/embed/class-template.php

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