PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.8
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.8
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 / Platform / Gutenberg.php

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

199 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Templately\Core\Platform;
3
4 use Templately\API\Import;
5 use Templately\Core\Importer\Utils\GutenbergHelper;
6 use Templately\Core\Platform;
7 use Templately\Core\Module;
8 use Templately\Utils\Helper;
9
10 use WP_Error;
11 use function get_permalink;
12 use function get_edit_post_link;
13 use function wp_insert_post;
14 use function wp_slash;
15 use function wp_unslash;
16 use function json_decode;
17
18 class Gutenberg extends Platform {
19 /**
20 * Platform ID
21 * @var string
22 */
23 private $id = 'gutenberg';
24
25 /**
26 * Is gutenberg is active or not
27 * @var boolean
28 */
29 public $is_gutenberg_active = false;
30
31 /**
32 * Initializing the platform and add it to module.
33 */
34 public function __construct(){
35 Module::get_instance()->add( (object) [
36 'id' => $this->id,
37 'object' => $this
38 ]);
39
40 $this->hooks();
41 }
42
43 /**
44 * Initializing Hooks
45 * @return void
46 */
47 public function hooks(){
48 add_action( 'enqueue_block_editor_assets', [ $this, 'scripts' ] );
49 add_action( 'admin_footer', [ $this, 'print_admin_js_template' ] );
50 }
51
52 /**
53 * Assets Enqueueing
54 * @return void
55 */
56 public function scripts(){
57 $this->is_gutenberg_active = true;
58 templately()->assets->enqueue( 'templately-gutenberg', 'css/gutenberg.css' );
59 templately()->assets->enqueue( 'templately-gutenberg', 'js/gutenberg.js' );
60 templately()->admin->scripts( 'gutenberg' );
61 }
62
63 /**
64 * Templately Button and Wrapper for Gutenberg
65 *
66 * @since 2.0.0
67 *
68 * @return void
69 */
70 public function print_admin_js_template() {
71 if ( ! $this->is_gutenberg_active ) {
72 return;
73 }
74 $post_type = apply_filters( 'templately_cloud_push_post_type', get_post_type());
75
76 ?>
77 <div id="templately-gutenberg"></div>
78 <script id="templately-gutenberg-button-switch-mode" type="text/html">
79 <div id="templately-gutenberg-buttons">
80 <button id="templately-gutenberg-button" type="button" class="button button-primary button-large gutenberg-add-templately-button">
81 <i class="templately-icon" aria-hidden="true"></i>
82 <?php echo esc_html__( 'Templately', 'templately' ); ?>
83 </button>
84 <button id="templately-cloud-push" type="button" class="button button-primary button-large">
85 <i class="templately-cloud-icon" aria-hidden="true"></i>
86 <?php echo sprintf( __( 'Save %s in Templately', 'templately' ), $post_type ); ?>
87 </button>
88 </div>
89 </script>
90 <?php
91 }
92
93 /**
94 * Determine Active UI Theme
95 * @return string
96 */
97 public function ui_theme(){
98 return 'light';
99 }
100
101 /**
102 * Creating a gutenberg page
103 *
104 * @param integer $id
105 * @param string $title
106 * @param Import $importer
107 *
108 * @since 2.0.0
109 *
110 * @return array|WP_Error array on success, WP_Error on failure.
111 */
112 public function create_page( $id, $title, $importer = null ){
113 $post_data = $inserted_ID = $importer->get_content( $id, 'gutenberg' );
114
115 if( is_wp_error( $inserted_ID ) ) {
116 return $inserted_ID;
117 }
118
119 if ( ! empty( $inserted_ID['content'] ) ) {
120 $inserted_ID = wp_insert_post( array (
121 'post_status' => 'draft',
122 'post_type' => 'page',
123 'post_title' => $title,
124 'post_content' => wp_slash($inserted_ID['content']),
125 ) );
126 }
127
128 if ( is_wp_error( $inserted_ID ) ) {
129 return Helper::error(
130 'import_failed',
131 $inserted_ID->get_error_message(),
132 'import/page',
133 $inserted_ID->get_error_code()
134 );
135 }
136
137 if($inserted_ID){
138 $post_data = $this->process_images($post_data, $inserted_ID);
139
140 // Update the post content with the processed images
141 $updated_post = array(
142 'ID' => $inserted_ID,
143 'post_content' => wp_slash($post_data['content']),
144 );
145 wp_update_post($updated_post);
146 }
147
148 return [
149 'post_id' => $inserted_ID,
150 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ),
151 'visit' => get_permalink( $inserted_ID )
152 ];
153 }
154
155 /**
156 * Inserts a template into the Gutenberg editor.
157 *
158 * @param mixed $data
159 * @param int $postId
160 * @return array
161 */
162 public function insert($data, $postId = 0) {
163 $data = $this->process_images($data, $postId);
164 return $data;
165 }
166
167 /**
168 * Inserts a template into the Gutenberg editor.
169 *
170 * @param mixed $data
171 * @param int $postId
172 * @return array
173 */
174 public function process_images($data, $postId = 0) {
175 // Instantiate GutenbergHelper
176 $helper = new GutenbergHelper();
177
178 // Organize URLs from the content
179 $organizedUrls = $helper->parse_images($data['content']);
180
181 // Define template settings
182 $template_settings = [
183 'post_id' => $postId,
184 '__attachments' => $organizedUrls,
185 ];
186
187 // Map post IDs and disable logging
188 $helper->map_post_ids[$postId] = $postId;
189 $helper->shouldLog = false;
190
191 // Prepare the helper with the data and settings
192 $helper->prepare($data, $template_settings);
193
194 // Update the content in the data array
195 $data['content'] = wp_unslash($helper->get_content());
196
197 return $data;
198 }
199 }