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 / handler / class-shortcode.php

class-shortcode.php in Embed Privacy 1.11.2, at inc/handler/class-shortcode.php

132 lines 4.2 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\handler;
3
4 use epiphyt\Embed_Privacy\data\Providers;
5 use epiphyt\Embed_Privacy\Embed_Privacy;
6
7 /**
8 * Shortcode handler.
9 *
10 * @author Epiphyt
11 * @license GPL2
12 * @package epiphyt\Embed_Privacy
13 * @since 1.10.0
14 */
15 final class Shortcode {
16 /**
17 * @var string[] List of ignored shortcodes
18 */
19 private $ignored_shortcodes = [
20 'embed_privacy_opt_out',
21 'grw',
22 ];
23
24 /**
25 * Initialize functionality.
26 */
27 public function init() {
28 \add_shortcode( 'embed_privacy_opt_out', [ self::class, 'opt_out' ] );
29 \add_filter( 'the_content', [ $this, 'print_assets_for_shortcode' ] );
30 }
31
32 /**
33 * Get a list with ignored shortcodes.
34 *
35 * @return string[] List with ignored shortcodes
36 */
37 public function get_ignored() {
38 /**
39 * Filter the ignored shortcodes list.
40 *
41 * @since 1.6.0
42 *
43 * @param string[] $ignored_shortcodes Current list of ignored shortcodes
44 */
45 $this->ignored_shortcodes = \apply_filters( 'embed_privacy_ignored_shortcodes', $this->ignored_shortcodes );
46
47 return $this->ignored_shortcodes;
48 }
49
50 /**
51 * Display an Opt-out shortcode.
52 *
53 * @param array|string $attributes Shortcode attributes
54 * @return string The shortcode output
55 */
56 public static function opt_out( $attributes ) {
57 $attributes = \shortcode_atts( [
58 'headline' => \__( 'Embed providers', 'embed-privacy' ),
59 'show_all' => 0,
60 'subline' => \__( 'Enable or disable embed providers globally. By enabling a provider, its embedded content will be displayed directly on every page without asking you anymore.', 'embed-privacy' ),
61 ], $attributes );
62 $cookie = Embed_Privacy::get_instance()->get_cookie();
63 $embed_providers = Providers::get_instance()->get_list();
64 $enabled_providers = \array_keys( (array) $cookie );
65
66 if ( empty( $embed_providers ) ) {
67 return '';
68 }
69
70 $headline = '<h3>' . \esc_html( $attributes['headline'] ) . '</h3>' . \PHP_EOL;
71
72 /**
73 * Filter the opt-out headline.
74 *
75 * @param string $headline Current headline HTML
76 * @param array $attributes Shortcode attributes
77 */
78 $headline = \apply_filters( 'embed_privacy_opt_out_headline', $headline, $attributes );
79
80 /**
81 * Filter the opt-out subline.
82 *
83 * @param string $subline Current subline HTML
84 * @param array $attributes Shortcode attributes
85 */
86 $subline = \apply_filters( 'embed_privacy_opt_out_subline', '<p>' . \esc_html( $attributes['subline'] ) . '</p>' . \PHP_EOL, $attributes );
87
88 $output = '<div class="embed-privacy-opt-out" data-show-all="' . ( $attributes['show_all'] ? 1 : 0 ) . '">' . \PHP_EOL . $headline . $subline;
89
90 foreach ( $embed_providers as $provider ) {
91 $is_checked = false;
92
93 if ( $attributes['show_all'] ) {
94 $is_checked = \in_array( $provider->get_name(), $enabled_providers, true );
95 }
96
97 $is_hidden = ! $attributes['show_all'] && ! \in_array( $provider->get_name(), $enabled_providers, true );
98 $microtime = \str_replace( '.', '', \microtime( true ) );
99 $output .= '<span class="embed-privacy-provider' . ( $is_hidden ? ' is-hidden' : '' ) . '">' . \PHP_EOL;
100 $output .= '<label class="embed-privacy-opt-out-label" for="embed-privacy-provider-' . \esc_attr( $provider->get_name() ) . '-' . $microtime . '" data-embed-provider="' . \esc_attr( $provider->get_name() ) . '">';
101 $output .= '<input type="checkbox" id="embed-privacy-provider-' . \esc_attr( $provider->get_name() ) . '-' . $microtime . '" ' . \checked( $is_checked, true, false ) . ' class="embed-privacy-opt-out-input" data-embed-provider="' . \esc_attr( $provider->get_name() ) . '"> ';
102 $output .= \sprintf(
103 /* translators: embed provider title */
104 \esc_html__( 'Load all embeds from %s', 'embed-privacy' ),
105 \esc_html( $provider->get_title() )
106 );
107 $output .= '</label><br>' . \PHP_EOL;
108 $output .= '</span>' . \PHP_EOL;
109 }
110
111 $output .= '</div>' . \PHP_EOL;
112
113 return $output;
114 }
115
116 /**
117 * Print Embed Privacy assets if page contains the shortcode.
118 *
119 * @since 1.11.1
120 *
121 * @param string $content Current page content
122 * @return string Current page content
123 */
124 public function print_assets_for_shortcode( $content ) {
125 if ( \has_shortcode( $content, 'embed_privacy_opt_out' ) ) {
126 Embed_Privacy::get_instance()->frontend->print_assets();
127 }
128
129 return $content;
130 }
131 }
132