| 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 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get a list with ignored shortcodes. |
| 33 |
* |
| 34 |
* @return string[] List with ignored shortcodes |
| 35 |
*/ |
| 36 |
public function get_ignored() { |
| 37 |
/** |
| 38 |
* Filter the ignored shortcodes list. |
| 39 |
* |
| 40 |
* @since 1.6.0 |
| 41 |
* |
| 42 |
* @param string[] $ignored_shortcodes Current list of ignored shortcodes |
| 43 |
*/ |
| 44 |
$this->ignored_shortcodes = \apply_filters( 'embed_privacy_ignored_shortcodes', $this->ignored_shortcodes ); |
| 45 |
|
| 46 |
return $this->ignored_shortcodes; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Display an Opt-out shortcode. |
| 51 |
* |
| 52 |
* @param array|string $attributes Shortcode attributes |
| 53 |
* @return string The shortcode output |
| 54 |
*/ |
| 55 |
public static function opt_out( $attributes ) { |
| 56 |
$attributes = \shortcode_atts( [ |
| 57 |
'headline' => \__( 'Embed providers', 'embed-privacy' ), |
| 58 |
'show_all' => 0, |
| 59 |
'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' ), |
| 60 |
], $attributes ); |
| 61 |
$cookie = Embed_Privacy::get_instance()->get_cookie(); |
| 62 |
$embed_providers = Providers::get_instance()->get_list(); |
| 63 |
$enabled_providers = \array_keys( (array) $cookie ); |
| 64 |
$is_javascript_detection = \get_option( 'embed_privacy_javascript_detection' ) === 'yes'; |
| 65 |
|
| 66 |
if ( empty( $embed_providers ) ) { |
| 67 |
return ''; |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! $is_javascript_detection && ! $attributes['show_all'] && ! $enabled_providers ) { |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
$headline = '<h3>' . \esc_html( $attributes['headline'] ) . '</h3>' . \PHP_EOL; |
| 75 |
|
| 76 |
/** |
| 77 |
* Filter the opt-out headline. |
| 78 |
* |
| 79 |
* @param string $headline Current headline HTML |
| 80 |
* @param array $attributes Shortcode attributes |
| 81 |
*/ |
| 82 |
$headline = \apply_filters( 'embed_privacy_opt_out_headline', $headline, $attributes ); |
| 83 |
|
| 84 |
/** |
| 85 |
* Filter the opt-out subline. |
| 86 |
* |
| 87 |
* @param string $subline Current subline HTML |
| 88 |
* @param array $attributes Shortcode attributes |
| 89 |
*/ |
| 90 |
$subline = \apply_filters( 'embed_privacy_opt_out_subline', '<p>' . \esc_html( $attributes['subline'] ) . '</p>' . \PHP_EOL, $attributes ); |
| 91 |
|
| 92 |
$output = '<div class="embed-privacy-opt-out" data-show-all="' . ( $attributes['show_all'] ? 1 : 0 ) . '">' . \PHP_EOL . $headline . $subline; |
| 93 |
|
| 94 |
foreach ( $embed_providers as $provider ) { |
| 95 |
if ( $is_javascript_detection ) { |
| 96 |
$is_checked = false; |
| 97 |
} |
| 98 |
else if ( $attributes['show_all'] ) { |
| 99 |
$is_checked = \in_array( $provider->get_name(), $enabled_providers, true ); |
| 100 |
} |
| 101 |
else { |
| 102 |
$is_checked = true; |
| 103 |
} |
| 104 |
|
| 105 |
$is_hidden = ! $is_javascript_detection && ! $attributes['show_all'] && ! \in_array( $provider->get_name(), $enabled_providers, true ); |
| 106 |
$microtime = \str_replace( '.', '', \microtime( true ) ); |
| 107 |
$output .= '<span class="embed-privacy-provider' . ( $is_hidden ? ' is-hidden' : '' ) . '">' . \PHP_EOL; |
| 108 |
$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() ) . '">'; |
| 109 |
$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() ) . '"> '; |
| 110 |
$output .= \sprintf( |
| 111 |
/* translators: embed provider title */ |
| 112 |
\esc_html__( 'Load all embeds from %s', 'embed-privacy' ), |
| 113 |
\esc_html( $provider->get_title() ) |
| 114 |
); |
| 115 |
$output .= '</label><br>' . \PHP_EOL; |
| 116 |
$output .= '</span>' . \PHP_EOL; |
| 117 |
} |
| 118 |
|
| 119 |
$output .= '</div>' . \PHP_EOL; |
| 120 |
|
| 121 |
return $output; |
| 122 |
} |
| 123 |
} |
| 124 |
|