PluginProbe
Embed Privacy / trunk
Embed Privacy vtrunk
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 trunk, at inc/handler/class-shortcode.php

143 lines 4.6 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 'headline_level' => 3,
60 'show_all' => 0,
61 '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' ),
62 ], $attributes );
63 $cookie = Embed_Privacy::get_instance()->get_cookie();
64 $embed_providers = Providers::get_instance()->get_list();
65 $enabled_providers = \array_keys( (array) $cookie );
66
67 if ( empty( $embed_providers ) ) {
68 return '';
69 }
70
71 $headline_id = \wp_unique_id( 'embed-privacy-opt-out-headline-' );
72 $headline_level = \min( 6, \max( 1, (int) $attributes['headline_level'] ) );
73 $headline = \sprintf(
74 '<h%1$d id="%2$s">%3$s</h%1$d>' . \PHP_EOL,
75 $headline_level,
76 \esc_attr( $headline_id ),
77 \esc_html( $attributes['headline'] )
78 );
79
80 /**
81 * Filter the opt-out headline.
82 *
83 * @since 1.13.1 Added the $headline_id parameter
84 *
85 * @param string $headline Current headline HTML
86 * @param array $attributes Shortcode attributes
87 * @param string $headline_id The ID the group of embed providers is labelled by
88 */
89 $headline = \apply_filters( 'embed_privacy_opt_out_headline', $headline, $attributes, $headline_id );
90
91 /**
92 * Filter the opt-out subline.
93 *
94 * @param string $subline Current subline HTML
95 * @param array $attributes Shortcode attributes
96 */
97 $subline = \apply_filters( 'embed_privacy_opt_out_subline', '<p>' . \esc_html( $attributes['subline'] ) . '</p>' . \PHP_EOL, $attributes );
98
99 $output = '<div class="embed-privacy-opt-out" data-show-all="' . ( $attributes['show_all'] ? 1 : 0 ) . '" role="group" aria-labelledby="' . \esc_attr( $headline_id ) . '">' . \PHP_EOL . $headline . $subline;
100
101 foreach ( $embed_providers as $provider ) {
102 $is_checked = false;
103
104 if ( $attributes['show_all'] ) {
105 $is_checked = \in_array( $provider->get_name(), $enabled_providers, true );
106 }
107
108 $is_hidden = ! $attributes['show_all'] && ! \in_array( $provider->get_name(), $enabled_providers, true );
109 $input_id = \wp_unique_id( 'embed-privacy-provider-' . $provider->get_name() . '-' );
110 $output .= '<span class="embed-privacy-provider' . ( $is_hidden ? ' is-hidden' : '' ) . '">' . \PHP_EOL;
111 $output .= '<label class="embed-privacy-opt-out-label" for="' . \esc_attr( $input_id ) . '" data-embed-provider="' . \esc_attr( $provider->get_name() ) . '">';
112 $output .= '<input type="checkbox" id="' . \esc_attr( $input_id ) . '" ' . \checked( $is_checked, true, false ) . ' class="embed-privacy-opt-out-input" data-embed-provider="' . \esc_attr( $provider->get_name() ) . '"> ';
113 $output .= \sprintf(
114 /* translators: embed provider title */
115 \esc_html__( 'Load all embeds from %s', 'embed-privacy' ),
116 \esc_html( $provider->get_title() )
117 );
118 $output .= '</label><br>' . \PHP_EOL;
119 $output .= '</span>' . \PHP_EOL;
120 }
121
122 $output .= '</div>' . \PHP_EOL;
123
124 return $output;
125 }
126
127 /**
128 * Print Embed Privacy assets if page contains the shortcode.
129 *
130 * @since 1.11.1
131 *
132 * @param string $content Current page content
133 * @return string Current page content
134 */
135 public function print_assets_for_shortcode( $content ) {
136 if ( \has_shortcode( $content, 'embed_privacy_opt_out' ) ) {
137 Embed_Privacy::get_instance()->frontend->print_assets();
138 }
139
140 return $content;
141 }
142 }
143