| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\FloatingButtons\Classes\Action; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use Elementor\Modules\FloatingButtons\Classes\Conditions\Conditions_Cache; |
| 10 |
use Elementor\Modules\FloatingButtons\Documents\Floating_Buttons; |
| 11 |
use Elementor\Modules\FloatingButtons\Module; |
| 12 |
|
| 13 |
class Action_Handler { |
| 14 |
|
| 15 |
protected string $action; |
| 16 |
protected array $menu_args; |
| 17 |
protected Conditions_Cache $conditions_cache; |
| 18 |
|
| 19 |
public function __construct( string $action, array $menu_args ) { |
| 20 |
$this->action = $action; |
| 21 |
$this->menu_args = $menu_args; |
| 22 |
$this->conditions_cache = new Conditions_Cache(); |
| 23 |
} |
| 24 |
|
| 25 |
public function process_action() { |
| 26 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
switch ( $this->action ) { |
| 31 |
case 'remove_from_entire_site': |
| 32 |
$this->handle_remove_from_entire_site(); |
| 33 |
break; |
| 34 |
case 'set_as_entire_site': |
| 35 |
$this->handle_set_as_entire_site(); |
| 36 |
break; |
| 37 |
default: |
| 38 |
break; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
private function handle_remove_from_entire_site(): void { |
| 43 |
$post_id = filter_input( INPUT_GET, 'post', FILTER_VALIDATE_INT ); |
| 44 |
check_admin_referer( 'remove_from_entire_site_' . $post_id ); |
| 45 |
delete_post_meta( $post_id, '_elementor_conditions' ); |
| 46 |
$this->conditions_cache->remove_from_cache( $post_id ); |
| 47 |
|
| 48 |
wp_redirect( $this->menu_args['menu_slug'] ); |
| 49 |
exit; |
| 50 |
} |
| 51 |
|
| 52 |
private function handle_set_as_entire_site(): void { |
| 53 |
$post_id = filter_input( INPUT_GET, 'post', FILTER_VALIDATE_INT ); |
| 54 |
check_admin_referer( 'set_as_entire_site_' . $post_id ); |
| 55 |
|
| 56 |
$posts = $this->get_published_floating_elements( $post_id ); |
| 57 |
|
| 58 |
foreach ( $posts as $post_id_to_delete ) { |
| 59 |
delete_post_meta( $post_id_to_delete, '_elementor_conditions' ); |
| 60 |
$this->conditions_cache->remove_from_cache( $post_id_to_delete ); |
| 61 |
} |
| 62 |
|
| 63 |
update_post_meta( $post_id, '_elementor_conditions', [ 'include/general' ] ); |
| 64 |
$this->conditions_cache->add_to_cache( $post_id ); |
| 65 |
|
| 66 |
wp_redirect( $this->menu_args['menu_slug'] ); |
| 67 |
exit; |
| 68 |
} |
| 69 |
|
| 70 |
private function get_published_floating_elements( int $post_id ): array { |
| 71 |
return get_posts( [ |
| 72 |
'post_type' => Module::CPT_FLOATING_BUTTONS, |
| 73 |
'posts_per_page' => -1, |
| 74 |
'post_status' => 'publish', |
| 75 |
'fields' => 'ids', |
| 76 |
'no_found_rows' => true, |
| 77 |
'update_post_term_cache' => false, |
| 78 |
'update_post_meta_cache' => false, |
| 79 |
'meta_query' => Floating_Buttons::get_meta_query_for_floating_buttons( |
| 80 |
Floating_Buttons::get_floating_element_type( $post_id ) |
| 81 |
), |
| 82 |
] ); |
| 83 |
} |
| 84 |
} |
| 85 |
|