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