PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.5.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.5.3
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.5.3, at includes/Core/Platform/Gutenberg.php

193 lines 5.7 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\Utils;
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 add_action( 'wp_ajax_update_gutenberg_hide_buttons', [ $this, 'update_gutenberg_hide_buttons' ] ); // Register AJAX action
51 }
52
53 /**
54 * Assets Enqueueing
55 * @return void
56 */
57 public function scripts(){
58 $this->is_gutenberg_active = true;
59 templately()->assets->enqueue( 'templately-gutenberg', 'css/gutenberg.css' );
60 templately()->assets->enqueue( 'templately-gutenberg', 'js/gutenberg.js' );
61 templately()->admin->scripts( 'gutenberg' );
62 }
63
64 /**
65 * Templately Button and Wrapper for Gutenberg
66 *
67 * @since 2.0.0
68 *
69 * @return void
70 */
71 public function print_admin_js_template() {
72 if ( ! $this->is_gutenberg_active ) {
73 return;
74 }
75 $post_type = apply_filters( 'templately_cloud_push_post_type', get_post_type());
76
77 ?>
78 <div id="templately-gutenberg"></div>
79 <script id="templately-gutenberg-button-switch-mode" type="text/html">
80 <div id="templately-gutenberg-buttons">
81 <button id="templately-gutenberg-button" type="button" class="button button-primary button-large gutenberg-add-templately-button">
82 <i class="templately-icon" aria-hidden="true"></i>
83 <?php echo esc_html__( 'Templately', 'templately' ); ?>
84 </button>
85 <button id="templately-cloud-push" type="button" class="button button-primary button-large">
86 <i class="templately-cloud-icon" aria-hidden="true"></i>
87 <?php echo sprintf( __( 'Save %s in Templately', 'templately' ), $post_type ); ?>
88 </button>
89 </div>
90 </script>
91 <?php
92 }
93
94 /**
95 * Determine Active UI Theme
96 * @return string
97 */
98 public function ui_theme(){
99 return 'light';
100 }
101
102 /**
103 * Creating a gutenberg page
104 *
105 * @param integer $id
106 * @param string $title
107 * @param Import $importer
108 *
109 * @since 2.0.0
110 *
111 * @return array|WP_Error array on success, WP_Error on failure.
112 */
113 public function create_page( $id, $title, $importer = null, $settings = [] ){
114 $post_data = $inserted_ID = $importer->get_content( $id, 'gutenberg' );
115
116 if( is_wp_error( $inserted_ID ) ) {
117 return $inserted_ID;
118 }
119
120 if ( ! empty( $settings ) && ! empty( $inserted_ID['content'] ) && is_string( $inserted_ID['content'] ) ) {
121 $inserted_ID['content'] = \Templately\Core\Importer\Utils\GutenbergSettingsMerger::merge( $inserted_ID['content'], $settings );
122 $post_data['content'] = $inserted_ID['content'];
123 }
124
125 if ( ! empty( $inserted_ID['content'] ) ) {
126 $inserted_ID = wp_insert_post( array (
127 'post_status' => 'draft',
128 'post_type' => 'page',
129 'post_title' => $title,
130 'post_content' => wp_slash($inserted_ID['content']),
131 ) );
132 }
133
134 if ( is_wp_error( $inserted_ID ) ) {
135 return Helper::error(
136 'import_failed',
137 $inserted_ID->get_error_message(),
138 'import/page',
139 $inserted_ID->get_error_code()
140 );
141 }
142
143 if($inserted_ID){
144 $post_data['content'] = Utils::import_and_replace_attachments($post_data['content'], $inserted_ID);
145
146 // Update the post content with the processed images
147 $updated_post = array(
148 'ID' => $inserted_ID,
149 'post_content' => wp_slash($post_data['content']),
150 );
151 wp_update_post($updated_post);
152 }
153
154 return [
155 'post_id' => $inserted_ID,
156 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ),
157 'visit' => get_permalink( $inserted_ID )
158 ];
159 }
160
161 /**
162 * Inserts a template into the Gutenberg editor.
163 *
164 * @param mixed $data
165 * @param int $postId
166 * @return array
167 */
168 public function insert($data, $postId = 0) {
169 $data['content'] = Utils::import_and_replace_attachments($data['content'], $postId);
170 return $data;
171 }
172
173 /**
174 * AJAX handler to update the option `templately-gutenberg-hide-buttons`
175 */
176 public function update_gutenberg_hide_buttons() {
177 // Check nonce for security
178 check_ajax_referer( 'templately_nonce', 'nonce' );
179
180 // Get the new value from the AJAX request
181 $hide_buttons = isset($_GET['hide_buttons']) ? sanitize_text_field($_GET['hide_buttons']) : '';
182
183 // Update the option
184 update_option('templately-gutenberg-hide-buttons', $hide_buttons);
185
186 $hide_buttons = get_option('templately-gutenberg-hide-buttons', 'no');
187 $hide_buttons = $hide_buttons === 'yes' ? 'yes' : 'no';
188 // Send a response back to the JavaScript
189 wp_send_json_success([
190 'hide_buttons' => $hide_buttons,
191 ]);
192 }
193 }