| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\ImportExportCustomization\Processes; |
| 3 |
|
| 4 |
use Elementor\App\Modules\ImportExportCustomization\Module; |
| 5 |
use Elementor\App\Modules\ImportExportCustomization\Runners\Revert\Elementor_Content; |
| 6 |
use Elementor\App\Modules\ImportExportCustomization\Runners\Revert\Revert_Runner_Base; |
| 7 |
use Elementor\App\Modules\ImportExportCustomization\Runners\Revert\Plugins; |
| 8 |
use Elementor\App\Modules\ImportExportCustomization\Runners\Revert\Site_Settings; |
| 9 |
use Elementor\App\Modules\ImportExportCustomization\Runners\Revert\Taxonomies; |
| 10 |
use Elementor\App\Modules\ImportExportCustomization\Runners\Revert\Templates; |
| 11 |
use Elementor\App\Modules\ImportExportCustomization\Runners\Revert\Wp_Content; |
| 12 |
use Elementor\App\Modules\ImportExportCustomization\Utils; |
| 13 |
|
| 14 |
class Revert { |
| 15 |
|
| 16 |
/** |
| 17 |
* @var Revert_Runner_Base[] |
| 18 |
*/ |
| 19 |
protected $runners = []; |
| 20 |
|
| 21 |
private $import_sessions; |
| 22 |
|
| 23 |
private $revert_sessions; |
| 24 |
|
| 25 |
public function __construct() { |
| 26 |
$this->import_sessions = self::get_import_sessions(); |
| 27 |
$this->revert_sessions = self::get_revert_sessions(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register a runner. |
| 32 |
* |
| 33 |
* @param Revert_Runner_Base $runner_instance |
| 34 |
*/ |
| 35 |
public function register( Revert_Runner_Base $runner_instance ) { |
| 36 |
$this->runners[ $runner_instance::get_name() ] = $runner_instance; |
| 37 |
} |
| 38 |
|
| 39 |
public function register_default_runners() { |
| 40 |
$this->register( new Site_Settings() ); |
| 41 |
$this->register( new Plugins() ); |
| 42 |
$this->register( new Templates() ); |
| 43 |
$this->register( new Taxonomies() ); |
| 44 |
$this->register( new Elementor_Content() ); |
| 45 |
$this->register( new Wp_Content() ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Execute the revert process. |
| 50 |
* |
| 51 |
* @throws \Exception If no revert runners have been specified. |
| 52 |
*/ |
| 53 |
public function run() { |
| 54 |
if ( empty( $this->runners ) ) { |
| 55 |
throw new \Exception( 'Couldn’t execute the revert process because no revert runners have been specified. Try again by specifying revert runners.' ); |
| 56 |
} |
| 57 |
|
| 58 |
$import_session = $this->get_last_import_session(); |
| 59 |
|
| 60 |
if ( empty( $import_session ) ) { |
| 61 |
throw new \Exception( 'Couldn’t execute the revert process because there are no import sessions to revert.' ); |
| 62 |
} |
| 63 |
|
| 64 |
// fallback if the import session failed and doesn't have the runners metadata |
| 65 |
if ( ! isset( $import_session['runners'] ) && isset( $import_session['instance_data'] ) ) { |
| 66 |
$import_session['runners'] = $import_session['instance_data']['runners_import_metadata'] ?? []; |
| 67 |
} |
| 68 |
|
| 69 |
foreach ( $this->runners as $runner ) { |
| 70 |
if ( $runner->should_revert( $import_session ) ) { |
| 71 |
$runner->revert( $import_session ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
$this->revert_attachments( $import_session ); |
| 76 |
|
| 77 |
$this->delete_last_import_data(); |
| 78 |
} |
| 79 |
|
| 80 |
public static function get_import_sessions() { |
| 81 |
$import_sessions = Utils::get_import_sessions(); |
| 82 |
|
| 83 |
if ( ! $import_sessions ) { |
| 84 |
return []; |
| 85 |
} |
| 86 |
|
| 87 |
usort( $import_sessions, function( $a, $b ) { |
| 88 |
return strcmp( $a['start_timestamp'], $b['start_timestamp'] ); |
| 89 |
} ); |
| 90 |
|
| 91 |
return $import_sessions; |
| 92 |
} |
| 93 |
|
| 94 |
public static function get_revert_sessions() { |
| 95 |
$revert_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_REVERT_SESSIONS ); |
| 96 |
|
| 97 |
if ( ! $revert_sessions ) { |
| 98 |
return []; |
| 99 |
} |
| 100 |
|
| 101 |
return $revert_sessions; |
| 102 |
} |
| 103 |
|
| 104 |
public function get_last_import_session() { |
| 105 |
$import_sessions = $this->import_sessions; |
| 106 |
|
| 107 |
if ( empty( $import_sessions ) ) { |
| 108 |
return []; |
| 109 |
} |
| 110 |
|
| 111 |
return end( $import_sessions ); |
| 112 |
} |
| 113 |
|
| 114 |
public function get_penultimate_import_session() { |
| 115 |
$sessions_data = $this->import_sessions; |
| 116 |
$penultimate_element_value = []; |
| 117 |
|
| 118 |
if ( empty( $sessions_data ) ) { |
| 119 |
return []; |
| 120 |
} |
| 121 |
|
| 122 |
end( $sessions_data ); |
| 123 |
|
| 124 |
prev( $sessions_data ); |
| 125 |
|
| 126 |
if ( ! is_null( key( $sessions_data ) ) ) { |
| 127 |
$penultimate_element_value = current( $sessions_data ); |
| 128 |
} |
| 129 |
|
| 130 |
return $penultimate_element_value; |
| 131 |
} |
| 132 |
|
| 133 |
private function delete_last_import_data() { |
| 134 |
$import_sessions = $this->import_sessions; |
| 135 |
$revert_sessions = $this->revert_sessions; |
| 136 |
|
| 137 |
$reverted_session = array_pop( $import_sessions ); |
| 138 |
|
| 139 |
$revert_sessions[] = [ |
| 140 |
'session_id' => $reverted_session['session_id'], |
| 141 |
'kit_title' => $reverted_session['kit_title'], |
| 142 |
'kit_name' => $reverted_session['kit_name'], |
| 143 |
'kit_thumbnail' => $reverted_session['kit_thumbnail'], |
| 144 |
'source' => $reverted_session['kit_source'], |
| 145 |
'user_id' => get_current_user_id(), |
| 146 |
'import_timestamp' => $reverted_session['start_timestamp'], |
| 147 |
'revert_timestamp' => current_time( 'timestamp' ), |
| 148 |
]; |
| 149 |
|
| 150 |
update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false ); |
| 151 |
update_option( Module::OPTION_KEY_ELEMENTOR_REVERT_SESSIONS, $revert_sessions, false ); |
| 152 |
|
| 153 |
$this->import_sessions = $import_sessions; |
| 154 |
$this->revert_sessions = $revert_sessions; |
| 155 |
} |
| 156 |
|
| 157 |
private function revert_attachments( $data ) { |
| 158 |
$query_args = [ |
| 159 |
'post_type' => 'attachment', |
| 160 |
'post_status' => 'any', |
| 161 |
'posts_per_page' => -1, |
| 162 |
'meta_query' => [ |
| 163 |
[ |
| 164 |
'key' => Module::META_KEY_ELEMENTOR_IMPORT_SESSION_ID, |
| 165 |
'value' => $data['session_id'], |
| 166 |
], |
| 167 |
], |
| 168 |
]; |
| 169 |
|
| 170 |
$query = new \WP_Query( $query_args ); |
| 171 |
|
| 172 |
foreach ( $query->posts as $post ) { |
| 173 |
wp_delete_attachment( $post->ID, true ); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|