PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.0.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.0.0
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Core / Importer / Runners / GutenbergContent.php

GutenbergContent.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.0.0, at includes/Core/Importer/Runners/GutenbergContent.php

107 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Core\Importer\Runners;
4
5 use Templately\Builder\PageTemplates;
6 use Templately\Core\Importer\Utils\Utils;
7 use WP_Error;
8
9 class GutenbergContent extends BaseRunner {
10
11 public function get_name(): string {
12 return 'content';
13 }
14
15 public function get_label(): string {
16 return __( 'Block Editor Content', 'templately' );
17 }
18
19 public function should_run( $data, $imported_data = [] ): bool {
20 return $this->platform == 'gutenberg' && ! empty( $this->manifest['content'] );
21 }
22
23 public function should_log(): bool {
24 return true;
25 }
26
27 public function get_action(): string {
28 return 'eventLog';
29 }
30
31 public function log_message(): string {
32 return __( 'Importing Gutenberg Templates (Pages, Posts etc)', 'templately' );
33 }
34
35 public function import( $data, $imported_data ): array {
36 $results = [];
37 $contents = $this->manifest['content'];
38 $path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR;
39
40 $processed = 0;
41 $total = array_reduce($contents, function($carry, $item) {
42 return $carry + count($item);
43 }, 0);
44
45 foreach ( $contents as $type => $posts ) {
46 foreach ( $posts as $id => $settings ) {
47 $import = $this->import_page_content( $id, $type, $path, $settings );
48
49 if ( ! $import ) {
50 $results[ $type ]['failed'][ $id ] = $import;
51 } else {
52 Utils::import_page_settings( $import, $settings );
53 $results[ $type ]['succeed'][ $id ] = $import;
54 }
55
56 // Broadcast Log
57 $processed += 1;
58 $progress = floor( ( 100 * $processed ) / $total );
59 $this->log( $progress, null, 'eventLog' );
60 }
61 }
62
63 return [ 'content' => $results ];
64 }
65
66 /**
67 * @param $id
68 * @param $type
69 * @param $path
70 * @param $settings
71 *
72 * @return false|int|void|WP_Error
73 */
74 private function import_page_content( $id, $type, $path, $settings ) {
75 try {
76 $json_content = Utils::read_json_file( $path . '/' . $type . '/' . $id . '.json' );
77 if ( ! empty( $json_content ) ) {
78
79 /**
80 * TODO:
81 *
82 * We can check if there is any data for settings.
83 * if yes: ignore content from insert.
84 *
85 * Process the content while finalizing.
86 */
87
88 $post_data = [
89 'post_title' => $json_content['title'] ?? ucfirst( $type ) . ' - (by Templately)',
90 'post_status' => 'publish',
91 'post_type' => $type,
92 'post_content' => wp_slash( $json_content['content'] ),
93 'page_template' => PageTemplates::TEMPLATE_HEADER_FOOTER
94 ];
95 $inserted = wp_insert_post( $post_data );
96
97 if ( is_wp_error( $inserted ) ) {
98 return false;
99 }
100
101 return $inserted;
102 }
103 } catch ( \Exception $e ) {
104 return false;
105 }
106 }
107 }