| 1 |
<?php |
| 2 |
namespace NotificationX\Extensions\ExitIntent; |
| 3 |
|
| 4 |
use Elementor\TemplateLibrary\Source_Local; |
| 5 |
use Elementor\Core\Settings\Page\Model; |
| 6 |
use Elementor\Plugin as ElementorPlugin; |
| 7 |
|
| 8 |
/** |
| 9 |
* Elementor importer for Exit Intent Popup seed templates. |
| 10 |
* |
| 11 |
* Mirrors NotificationX\Extensions\PressBar\Importer but writes to the |
| 12 |
* `nx_exit_intent` post type and reads from this extension's own jsons/ |
| 13 |
* folder. See docs/features/exit-intent/elementor/02-importer.md. |
| 14 |
*/ |
| 15 |
class Importer extends Source_Local { |
| 16 |
|
| 17 |
public function get_template_content( $args ) { |
| 18 |
$path = __DIR__ . '/jsons/' . $args['theme'] . '.json'; |
| 19 |
if ( ! file_exists( $path ) ) { |
| 20 |
return new \WP_Error( 'nx_exit_intent_template_missing', sprintf( 'Seed template not found: %s', $args['theme'] ) ); |
| 21 |
} |
| 22 |
return json_decode( file_get_contents( $path ), true ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get template data ready for Elementor import. |
| 27 |
* |
| 28 |
* @inheritDoc |
| 29 |
* @param array $args |
| 30 |
* @param string $context |
| 31 |
* @return array|\WP_Error |
| 32 |
*/ |
| 33 |
public function get_data( array $args, $context = 'display' ) { |
| 34 |
$data = $this->get_template_content( $args ); |
| 35 |
if ( is_wp_error( $data ) ) { |
| 36 |
return $data; |
| 37 |
} |
| 38 |
ElementorPlugin::$instance->editor->set_edit_mode( true ); |
| 39 |
|
| 40 |
$data['content'] = $this->replace_elements_ids( $data['content'] ); |
| 41 |
$data['content'] = $this->process_export_import_content( $data['content'], 'on_import' ); |
| 42 |
|
| 43 |
$post_id = isset( $args['editor_post_id'] ) ? $args['editor_post_id'] : false; |
| 44 |
if ( $post_id ) { |
| 45 |
$document = ElementorPlugin::$instance->documents->get( $post_id ); |
| 46 |
if ( $document ) { |
| 47 |
$data['content'] = $document->get_elements_raw_data( $data['content'], true ); |
| 48 |
} |
| 49 |
} |
| 50 |
return $data; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Create a new `nx_exit_intent` Elementor document from a seed theme. |
| 55 |
* |
| 56 |
* @param array $args { theme: string, post_title?: string } |
| 57 |
* @return int|\WP_Error New post ID on success. |
| 58 |
*/ |
| 59 |
public function create_nx( $args ) { |
| 60 |
$template_data = $this->get_data( $args ); |
| 61 |
if ( is_wp_error( $template_data ) ) { |
| 62 |
return $template_data; |
| 63 |
} |
| 64 |
|
| 65 |
$page_settings = []; |
| 66 |
if ( ! empty( $template_data['page_settings'] ) ) { |
| 67 |
$page = new Model( [ |
| 68 |
'id' => 0, |
| 69 |
'settings' => $template_data['page_settings'], |
| 70 |
] ); |
| 71 |
$page_settings_data = $this->process_element_export_import_content( $page, 'on_import' ); |
| 72 |
if ( ! empty( $page_settings_data['settings'] ) ) { |
| 73 |
$page_settings = $page_settings_data['settings']; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
$defaults = [ |
| 78 |
'post_title' => isset( $template_data['page_title'] ) |
| 79 |
? $template_data['page_title'] |
| 80 |
: 'NxExit: ' . $template_data['title'], |
| 81 |
'page_settings' => $page_settings, |
| 82 |
'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending', |
| 83 |
]; |
| 84 |
|
| 85 |
$template_data = wp_parse_args( $template_data, $defaults ); |
| 86 |
|
| 87 |
$document = ElementorPlugin::$instance->documents->create( |
| 88 |
$template_data['type'], |
| 89 |
[ |
| 90 |
'post_title' => $template_data['post_title'], |
| 91 |
'post_status' => $template_data['status'], |
| 92 |
'post_type' => 'nx_exit_intent', |
| 93 |
] |
| 94 |
); |
| 95 |
|
| 96 |
if ( is_wp_error( $document ) ) { |
| 97 |
return $document; |
| 98 |
} |
| 99 |
|
| 100 |
$document->save( [ |
| 101 |
'elements' => $template_data['content'], |
| 102 |
'settings' => $page_settings, |
| 103 |
] ); |
| 104 |
|
| 105 |
return $document->get_main_id(); |
| 106 |
} |
| 107 |
} |
| 108 |
|