| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExport\Runners\Revert; |
| 4 |
|
| 5 |
use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils; |
| 6 |
use Elementor\Plugin; |
| 7 |
|
| 8 |
class Elementor_Content extends Revert_Runner_Base { |
| 9 |
private $show_page_on_front; |
| 10 |
|
| 11 |
private $page_on_front_id; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
$this->init_page_on_front_data(); |
| 15 |
} |
| 16 |
|
| 17 |
public static function get_name() : string { |
| 18 |
return 'elementor-content'; |
| 19 |
} |
| 20 |
|
| 21 |
public function should_revert( array $data ) : bool { |
| 22 |
return ( |
| 23 |
isset( $data['runners'] ) && |
| 24 |
array_key_exists( static::get_name(), $data['runners'] ) |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function revert( array $data ) { |
| 29 |
$elementor_post_types = ImportExportUtils::get_elementor_post_types(); |
| 30 |
|
| 31 |
$query_args = [ |
| 32 |
'post_type' => $elementor_post_types, |
| 33 |
'post_status' => 'any', |
| 34 |
'posts_per_page' => -1, |
| 35 |
'meta_query' => [ |
| 36 |
[ |
| 37 |
'key' => static::META_KEY_ELEMENTOR_EDIT_MODE, |
| 38 |
'compare' => 'EXISTS', |
| 39 |
], |
| 40 |
[ |
| 41 |
'key' => static::META_KEY_ELEMENTOR_IMPORT_SESSION_ID, |
| 42 |
'value' => $data['session_id'], |
| 43 |
], |
| 44 |
], |
| 45 |
]; |
| 46 |
|
| 47 |
$query = new \WP_Query( $query_args ); |
| 48 |
|
| 49 |
foreach ( $query->posts as $post ) { |
| 50 |
$post_type_document = Plugin::$instance->documents->get( $post->ID ); |
| 51 |
$post_type_document->delete(); |
| 52 |
|
| 53 |
// Deleting the post will reset the show_on_front option. We need to set it to false, |
| 54 |
// so we can set it back to what it was. |
| 55 |
if ( $post->ID === $this->page_on_front_id ) { |
| 56 |
$this->show_page_on_front = false; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
$this->restore_page_on_front( $data ); |
| 61 |
} |
| 62 |
|
| 63 |
private function init_page_on_front_data() { |
| 64 |
$this->show_page_on_front = 'page' === get_option( 'show_on_front' ); |
| 65 |
|
| 66 |
if ( $this->show_page_on_front ) { |
| 67 |
$this->page_on_front_id = (int) get_option( 'page_on_front' ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
private function restore_page_on_front( $data ) { |
| 72 |
if ( empty( $data['runners'][ static::get_name() ]['page_on_front'] ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$page_on_front = $data['runners'][ static::get_name() ]['page_on_front']; |
| 77 |
|
| 78 |
$document = Plugin::$instance->documents->get( $page_on_front ); |
| 79 |
|
| 80 |
if ( ! $document ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$this->set_page_on_front( $document->get_main_id() ); |
| 85 |
} |
| 86 |
|
| 87 |
private function set_page_on_front( $page_id ) { |
| 88 |
update_option( 'page_on_front', $page_id ); |
| 89 |
|
| 90 |
if ( ! $this->show_page_on_front ) { |
| 91 |
update_option( 'show_on_front', 'page' ); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|