| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\ImportExportCustomization\Runners\Import; |
| 3 |
|
| 4 |
use Elementor\Modules\FloatingButtons\Documents\Floating_Buttons; |
| 5 |
use Elementor\Modules\FloatingButtons\Module as FloatingButtonsModule; |
| 6 |
use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Floating_Elements extends Import_Runner_Base { |
| 13 |
|
| 14 |
const CONDITIONS_CACHE_META_KEY = 'elementor_pro_theme_builder_conditions'; |
| 15 |
|
| 16 |
private $posts_cache = []; |
| 17 |
|
| 18 |
public static function get_name(): string { |
| 19 |
return 'floating-elements'; |
| 20 |
} |
| 21 |
|
| 22 |
public function should_import( array $data ): bool { |
| 23 |
return ( |
| 24 |
isset( $data['include'] ) && |
| 25 |
in_array( 'content', $data['include'], true ) && |
| 26 |
! empty( $data['manifest']['content']['e-floating-buttons'] ) && |
| 27 |
! empty( $data['extracted_directory_path'] ) |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
public function import( array $data, array $imported_data ) { |
| 32 |
$post_type = 'e-floating-buttons'; |
| 33 |
$posts_settings = $data['manifest']['content'][ $post_type ]; |
| 34 |
$path = $data['extracted_directory_path'] . 'content/' . $post_type . '/'; |
| 35 |
$imported_floating_elements = $imported_data['content']['e-floating-buttons']['succeed'] ?? []; |
| 36 |
$imported_post_ids = []; |
| 37 |
|
| 38 |
foreach ( $posts_settings as $id => $post_settings ) { |
| 39 |
try { |
| 40 |
$imported_post_ids[] = $this->import_floating_element_metadata( |
| 41 |
$id, |
| 42 |
$path, |
| 43 |
$imported_floating_elements |
| 44 |
); |
| 45 |
} catch ( \Exception $e ) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$this->set_display_conditions_cache( $imported_post_ids ); |
| 51 |
|
| 52 |
return []; |
| 53 |
} |
| 54 |
|
| 55 |
private function set_display_conditions_cache( array $imported_post_ids ) { |
| 56 |
$conditions = get_option( self::CONDITIONS_CACHE_META_KEY, [] ); |
| 57 |
$conditions['floating_buttons'] = []; |
| 58 |
|
| 59 |
foreach ( $imported_post_ids as $imported_post_id ) { |
| 60 |
$conditions['floating_buttons'][ $imported_post_id ] = [ 'include/general' ]; |
| 61 |
} |
| 62 |
|
| 63 |
update_option( self::CONDITIONS_CACHE_META_KEY, $conditions ); |
| 64 |
} |
| 65 |
|
| 66 |
private function import_floating_element_metadata( $id, $path, $imported_floating_elements ) { |
| 67 |
$post_data = ImportExportUtils::read_json_file( $path . $id ); |
| 68 |
$widget_type = $post_data['content'][0]['elements'][0]['widgetType'] ?? ''; |
| 69 |
$floating_element_type = 'floating-buttons'; |
| 70 |
$imported_post_id = $imported_floating_elements[ $id ] ?? null; |
| 71 |
|
| 72 |
if ( ! $imported_post_id ) { |
| 73 |
throw new \Exception( |
| 74 |
sprintf( |
| 75 |
/* translators: %s: Floating element ID */ |
| 76 |
esc_html__( 'Imported post ID not found for floating element: %s', 'elementor' ), |
| 77 |
esc_html( $id ) |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
if ( str_starts_with( $widget_type, 'floating-bars' ) ) { |
| 83 |
$floating_element_type = 'floating-bars'; |
| 84 |
update_post_meta( |
| 85 |
$imported_post_id, |
| 86 |
FloatingButtonsModule::FLOATING_ELEMENTS_TYPE_META_KEY, |
| 87 |
$floating_element_type |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! isset( $this->posts_cache[ $floating_element_type ] ) ) { |
| 92 |
$this->posts_cache[ $floating_element_type ] = get_posts( [ |
| 93 |
'post_type' => FloatingButtonsModule::CPT_FLOATING_BUTTONS, |
| 94 |
'posts_per_page' => -1, |
| 95 |
'post_status' => 'publish', |
| 96 |
'fields' => 'ids', |
| 97 |
'no_found_rows' => true, |
| 98 |
'update_post_term_cache' => false, |
| 99 |
'update_post_meta_cache' => false, |
| 100 |
'meta_query' => Floating_Buttons::get_meta_query_for_floating_buttons( |
| 101 |
$floating_element_type |
| 102 |
), |
| 103 |
] ); |
| 104 |
} |
| 105 |
|
| 106 |
$posts = $this->posts_cache[ $floating_element_type ]; |
| 107 |
|
| 108 |
foreach ( $posts as $post_id ) { |
| 109 |
delete_post_meta( $post_id, '_elementor_conditions' ); |
| 110 |
} |
| 111 |
|
| 112 |
update_post_meta( |
| 113 |
$imported_post_id, |
| 114 |
'_elementor_conditions', |
| 115 |
[ 'include/general' ] |
| 116 |
); |
| 117 |
|
| 118 |
return $imported_post_id; |
| 119 |
} |
| 120 |
} |
| 121 |
|