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

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

188 lines 5.4 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 ){
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( $inserted_ID['content'] ) ) {
121 $inserted_ID = wp_insert_post( array (
122 'post_status' => 'draft',
123 'post_type' => 'page',
124 'post_title' => $title,
125 'post_content' => wp_slash($inserted_ID['content']),
126 ) );
127 }
128
129 if ( is_wp_error( $inserted_ID ) ) {
130 return Helper::error(
131 'import_failed',
132 $inserted_ID->get_error_message(),
133 'import/page',
134 $inserted_ID->get_error_code()
135 );
136 }
137
138 if($inserted_ID){
139 $post_data['content'] = Utils::import_and_replace_attachments($post_data['content'], $inserted_ID);
140
141 // Update the post content with the processed images
142 $updated_post = array(
143 'ID' => $inserted_ID,
144 'post_content' => wp_slash($post_data['content']),
145 );
146 wp_update_post($updated_post);
147 }
148
149 return [
150 'post_id' => $inserted_ID,
151 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ),
152 'visit' => get_permalink( $inserted_ID )
153 ];
154 }
155
156 /**
157 * Inserts a template into the Gutenberg editor.
158 *
159 * @param mixed $data
160 * @param int $postId
161 * @return array
162 */
163 public function insert($data, $postId = 0) {
164 $data['content'] = Utils::import_and_replace_attachments($data['content'], $postId);
165 return $data;
166 }
167
168 /**
169 * AJAX handler to update the option `templately-gutenberg-hide-buttons`
170 */
171 public function update_gutenberg_hide_buttons() {
172 // Check nonce for security
173 check_ajax_referer( 'templately_nonce', 'nonce' );
174
175 // Get the new value from the AJAX request
176 $hide_buttons = isset($_GET['hide_buttons']) ? sanitize_text_field($_GET['hide_buttons']) : '';
177
178 // Update the option
179 update_option('templately-gutenberg-hide-buttons', $hide_buttons);
180
181 $hide_buttons = get_option('templately-gutenberg-hide-buttons', 'no');
182 $hide_buttons = $hide_buttons === 'yes' ? 'yes' : 'no';
183 // Send a response back to the JavaScript
184 wp_send_json_success([
185 'hide_buttons' => $hide_buttons,
186 ]);
187 }
188 }