| @@ -1,0 +1,94 @@ | ||
| 1 | +<?php | |
| 2 | +namespace NotificationX\Extensions\PressBar; | |
| 3 | + | |
| 4 | +use Elementor\TemplateLibrary\Source_Local; | |
| 5 | +use Elementor\TemplateLibrary\Classes\Images; | |
| 6 | +use Elementor\Core\Settings\Page\Model; | |
| 7 | +use Elementor\Plugin as ElementorPlugin; | |
| 8 | + | |
| 9 | +class Importer extends Source_Local { | |
| 10 | + public function get_template_content( $args ){ | |
| 11 | + $output = json_decode( file_get_contents( __DIR__ . "/jsons/" . $args[ 'theme' ] . '.json' ), true ); | |
| 12 | + return $output; | |
| 13 | + } | |
| 14 | + /** | |
| 15 | + * Get template data. | |
| 16 | + * | |
| 17 | + * @inheritDoc | |
| 18 | + * | |
| 19 | + * @param array $args Custom template arguments. | |
| 20 | + * @param string $context Optional. The context. Default is `display`. | |
| 21 | + * | |
| 22 | + * @return array Remote Template data. | |
| 23 | + */ | |
| 24 | + public function get_data( array $args, $context = 'display' ) { | |
| 25 | + $data = $this->get_template_content( $args ); | |
| 26 | + if ( is_wp_error( $data ) ) { | |
| 27 | + return $data; | |
| 28 | + } | |
| 29 | + ElementorPlugin::$instance->editor->set_edit_mode( true ); | |
| 30 | + | |
| 31 | + $data['content'] = $this->replace_elements_ids( $data['content'] ); | |
| 32 | + $data['content'] = $this->process_export_import_content( $data['content'], 'on_import' ); | |
| 33 | + | |
| 34 | + $post_id = isset( $args['editor_post_id'] ) ? $args['editor_post_id'] : false; | |
| 35 | + if( $post_id ) { | |
| 36 | + $document = ElementorPlugin::$instance->documents->get( $post_id ); | |
| 37 | + if ( $document ) { | |
| 38 | + $data['content'] = $document->get_elements_raw_data( $data['content'], true ); | |
| 39 | + } | |
| 40 | + } | |
| 41 | + return $data; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public function create_nx( $args ){ | |
| 45 | + $template_data = $this->get_data( $args ); | |
| 46 | + | |
| 47 | + $page_settings = []; | |
| 48 | + | |
| 49 | + if ( ! empty( $template_data['page_settings'] ) ) { | |
| 50 | + $page = new Model( [ | |
| 51 | + 'id' => 0, | |
| 52 | + 'settings' => $template_data['page_settings'], | |
| 53 | + ] ); | |
| 54 | + | |
| 55 | + $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' ); | |
| 56 | + | |
| 57 | + if ( ! empty( $page_settings_data['settings'] ) ) { | |
| 58 | + $page_settings = $page_settings_data['settings']; | |
| 59 | + } | |
| 60 | + } | |
| 61 | + | |
| 62 | + $defaults = [ | |
| 63 | + 'post_title' => isset( $template_data['page_title'] ) ? $template_data['page_title'] : 'NxBar: ' . $template_data['title'], | |
| 64 | + 'page_settings' => $page_settings, | |
| 65 | + 'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending', | |
| 66 | + ]; | |
| 67 | + | |
| 68 | + $template_data = wp_parse_args( $template_data, $defaults ); | |
| 69 | + | |
| 70 | + $document = ElementorPlugin::$instance->documents->create( | |
| 71 | + $template_data['type'], | |
| 72 | + [ | |
| 73 | + 'post_title' => $template_data['post_title'], | |
| 74 | + 'post_status' => $template_data['status'], | |
| 75 | + 'post_type' => 'nx_bar', | |
| 76 | + ] | |
| 77 | + ); | |
| 78 | + | |
| 79 | + if ( is_wp_error( $document ) ) { | |
| 80 | + /** | |
| 81 | + * @var \WP_Error $document | |
| 82 | + */ | |
| 83 | + return $document; | |
| 84 | + } | |
| 85 | + | |
| 86 | + $document->save( [ | |
| 87 | + 'elements' => $template_data['content'], | |
| 88 | + 'settings' => $page_settings, | |
| 89 | + ] ); | |
| 90 | + | |
| 91 | + return $document->get_main_id(); | |
| 92 | + } | |
| 93 | + | |
| 94 | +} | |