PluginProbe
Embed Privacy / 1.10.3
Embed Privacy v1.10.3
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-widget.php

class-widget.php in Embed Privacy 1.10.3, at inc/handler/class-widget.php

129 lines 3.8 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\Replacer;
5
6 /**
7 * Widget handler.
8 *
9 * @author Epiphyt
10 * @license GPL2
11 * @package epiphyt\Embed_Privacy
12 * @since 1.10.0
13 */
14 final class Widget {
15 /**
16 * @var string The alternative option name of this filter
17 */
18 public $alt_option_name = '';
19
20 /**
21 * @var string The ID of this filter
22 */
23 public $id = '';
24
25 /**
26 * @var string The ID base of this filter
27 */
28 public $id_base = 'embed_privacy_widget_output_filter';
29
30 /**
31 * @var string The name of this filter
32 */
33 public $name = 'Embed Privacy';
34
35 /**
36 * @var string The option name of this filter
37 */
38 public $option_name = 'widget_embed_privacy_widget_output_filter';
39
40 /**
41 * @var bool The updated value of this filter
42 */
43 public $updated = false;
44
45 /**
46 * @var array The widget options of this filter
47 */
48 public $widget_options = [];
49
50 /**
51 * @var \epiphyt\Embed_Privacy\handler\Widget
52 */
53 private static $instance;
54
55 /**
56 * Initialize functionality.
57 */
58 public static function init() {
59 \add_filter( 'dynamic_sidebar_params', [ self::class, 'filter_dynamic_sidebar_params' ], \PHP_INT_MAX );
60 \add_filter( 'embed_privacy_widget_output', [ Replacer::class, 'replace_embeds' ] );
61 }
62
63 /**
64 * Return the single instance of this class.
65 *
66 * @return \epiphyt\Embed_Privacy\handler\Widget The single instance of this class
67 */
68 public static function get_instance() {
69 if ( self::$instance === null ) {
70 self::$instance = new self();
71 }
72
73 return self::$instance;
74 }
75
76 /**
77 * Execute the widget's original callback function, filtering its output.
78 */
79 public static function display_widget() {
80 global $wp_registered_widgets;
81 $original_callback_params = \func_get_args();
82
83 $widget_id = isset( $original_callback_params[0]['widget_id'] ) ? $original_callback_params[0]['widget_id'] : null;
84 $original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
85
86 $wp_registered_widgets[ $widget_id ]['callback'] = $original_callback; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
87
88 $sidebar_id = isset( $original_callback_params[0]['id'] ) ? $original_callback_params[0]['id'] : null;
89
90 if ( \is_callable( $original_callback ) ) {
91 \ob_start();
92 \call_user_func_array( $original_callback, $original_callback_params ); // phpcs:ignore NeutronStandard.Functions.DisallowCallUserFunc.CallUserFunc
93 $widget_output = \ob_get_clean();
94
95 /**
96 * Filter the widget's output.
97 *
98 * @since 1.1.0
99 *
100 * @param string $widget_output The widget's output
101 * @param string $widget_id The widget's full ID
102 * @param string $sidebar_id The current sidebar ID
103 */
104 echo \apply_filters( 'embed_privacy_widget_output', $widget_output, $widget_id, $sidebar_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
105 }
106 }
107
108 /**
109 * Replace the widget's display callback with the Dynamic Sidebar Params display callback, storing the original callback for use later.
110 * The $sidebar_params variable is not modified; it is only used to get the current widget's ID.
111 *
112 * @param array $sidebar_params The sidebar parameters
113 * @return array The sidebar parameters
114 */
115 public static function filter_dynamic_sidebar_params( $sidebar_params ) {
116 if ( \is_admin() ) {
117 return $sidebar_params;
118 }
119
120 global $wp_registered_widgets;
121 $current_widget_id = $sidebar_params[0]['widget_id'];
122
123 $wp_registered_widgets[ $current_widget_id ]['original_callback'] = $wp_registered_widgets[ $current_widget_id ]['callback']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
124 $wp_registered_widgets[ $current_widget_id ]['callback'] = [ self::class, 'display_widget' ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
125
126 return $sidebar_params;
127 }
128 }
129