module.php
54 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Elementor\Modules\ContentSanitizer; |
| 4 | |
| 5 | use Elementor\Core\Base\Module as BaseModule; |
| 6 | use Elementor\Plugin; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | class Module extends BaseModule { |
| 13 | |
| 14 | const WIDGET_TO_SANITIZE = 'heading'; |
| 15 | |
| 16 | public function __construct() { |
| 17 | parent::__construct(); |
| 18 | |
| 19 | add_filter( 'elementor/document/save/data', [ $this, 'sanitize_content' ], 10, 2 ); |
| 20 | } |
| 21 | |
| 22 | public function get_name() { |
| 23 | return 'content-sanitizer'; |
| 24 | } |
| 25 | |
| 26 | public function sanitize_content( $data, $document ) : array { |
| 27 | if ( current_user_can( 'manage_options' ) || empty( $data['elements'] ) ) { |
| 28 | return $data; |
| 29 | } |
| 30 | |
| 31 | if ( ! $this->is_widget_present( $data ) ) { |
| 32 | return $data; |
| 33 | } |
| 34 | |
| 35 | return Plugin::$instance->db->iterate_data( $data, function ( $element ) { |
| 36 | if ( $this->is_target_widget( $element ) ) { |
| 37 | $element['settings']['title'] = Plugin::$instance->widgets_manager->get_widget_types( self::WIDGET_TO_SANITIZE )->sanitize( $element['settings']['title'] ); |
| 38 | } |
| 39 | |
| 40 | return $element; |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | private function is_target_widget( $element ) { |
| 45 | return self::WIDGET_TO_SANITIZE === $element['widgetType']; |
| 46 | } |
| 47 | |
| 48 | private function is_widget_present( array $elements ): bool { |
| 49 | $json = wp_json_encode( $elements ); |
| 50 | |
| 51 | return false !== strpos( $json, '"widgetType":"' . self::WIDGET_TO_SANITIZE . '"' ); |
| 52 | } |
| 53 | } |
| 54 |