| 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 |
|
| 85 |
if ( $widget_id === null || ! isset( $wp_registered_widgets[ $widget_id ] ) ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$original_callback = $wp_registered_widgets[ $widget_id ]['original_callback']; |
| 90 |
|
| 91 |
$wp_registered_widgets[ $widget_id ]['callback'] = $original_callback; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 92 |
|
| 93 |
$sidebar_id = isset( $original_callback_params[0]['id'] ) ? $original_callback_params[0]['id'] : null; |
| 94 |
|
| 95 |
if ( \is_callable( $original_callback ) ) { |
| 96 |
\ob_start(); |
| 97 |
\call_user_func_array( $original_callback, $original_callback_params ); // phpcs:ignore NeutronStandard.Functions.DisallowCallUserFunc.CallUserFunc |
| 98 |
$widget_output = \ob_get_clean(); |
| 99 |
|
| 100 |
/** |
| 101 |
* Filter the widget's output. |
| 102 |
* |
| 103 |
* @since 1.1.0 |
| 104 |
* |
| 105 |
* @param string $widget_output The widget's output |
| 106 |
* @param string $widget_id The widget's full ID |
| 107 |
* @param string $sidebar_id The current sidebar ID |
| 108 |
*/ |
| 109 |
$widget_output = \apply_filters( 'embed_privacy_widget_output', $widget_output, $widget_id, $sidebar_id ); |
| 110 |
|
| 111 |
echo \is_string( $widget_output ) ? $widget_output : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Replace the widget's display callback with the Dynamic Sidebar Params display callback, storing the original callback for use later. |
| 117 |
* The $sidebar_params variable is not modified; it is only used to get the current widget's ID. |
| 118 |
* |
| 119 |
* @param array $sidebar_params The sidebar parameters |
| 120 |
* @return array The sidebar parameters |
| 121 |
*/ |
| 122 |
public static function filter_dynamic_sidebar_params( $sidebar_params ) { |
| 123 |
if ( \is_admin() ) { |
| 124 |
return $sidebar_params; |
| 125 |
} |
| 126 |
|
| 127 |
global $wp_registered_widgets; |
| 128 |
$current_widget_id = $sidebar_params[0]['widget_id']; |
| 129 |
|
| 130 |
$wp_registered_widgets[ $current_widget_id ]['original_callback'] = $wp_registered_widgets[ $current_widget_id ]['callback']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 131 |
$wp_registered_widgets[ $current_widget_id ]['callback'] = [ self::class, 'display_widget' ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 132 |
|
| 133 |
return $sidebar_params; |
| 134 |
} |
| 135 |
} |
| 136 |
|