| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Templately\Core\Platform; |
| 3 | 3 | |
| 4 | 4 | use Templately\API\Import; |
| 5 | -use Templately\Core\Importer\Utils\GutenbergHelper; | |
| 5 | +use Templately\Core\Importer\Utils\Utils; | |
| 6 | 6 | use Templately\Core\Platform; |
| 7 | 7 | use Templately\Core\Module; |
| 8 | 8 | use Templately\Utils\Helper; |
| 9 | 9 | |
| @@ -46,8 +46,9 @@ | ||
| 46 | 46 | */ |
| 47 | 47 | public function hooks(){ |
| 48 | 48 | add_action( 'enqueue_block_editor_assets', [ $this, 'scripts' ] ); |
| 49 | 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 | |
| 50 | 51 | } |
| 51 | 52 | |
| 52 | 53 | /** |
| 53 | 54 | * Assets Enqueueing |
| @@ -108,9 +109,9 @@ | ||
| 108 | 109 | * @since 2.0.0 |
| 109 | 110 | * |
| 110 | 111 | * @return array|WP_Error array on success, WP_Error on failure. |
| 111 | 112 | */ |
| 112 | - public function create_page( $id, $title, $importer = null ){ | |
| 113 | + public function create_page( $id, $title, $importer = null, $settings = [] ){ | |
| 113 | 114 | $post_data = $inserted_ID = $importer->get_content( $id, 'gutenberg' ); |
| 114 | 115 | |
| 115 | 116 | if( is_wp_error( $inserted_ID ) ) { |
| 116 | 117 | return $inserted_ID; |
| @@ -115,8 +116,13 @@ | ||
| 115 | 116 | if( is_wp_error( $inserted_ID ) ) { |
| 116 | 117 | return $inserted_ID; |
| 117 | 118 | } |
| 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 | + | |
| 119 | 125 | if ( ! empty( $inserted_ID['content'] ) ) { |
| 120 | 126 | $inserted_ID = wp_insert_post( array ( |
| 121 | 127 | 'post_status' => 'draft', |
| 122 | 128 | 'post_type' => 'page', |
| @@ -134,9 +140,9 @@ | ||
| 134 | 140 | ); |
| 135 | 141 | } |
| 136 | 142 | |
| 137 | 143 | if($inserted_ID){ |
| 138 | - $post_data = $this->process_images($post_data, $inserted_ID); | |
| 144 | + $post_data['content'] = Utils::import_and_replace_attachments($post_data['content'], $inserted_ID); | |
| 139 | 145 | |
| 140 | 146 | // Update the post content with the processed images |
| 141 | 147 | $updated_post = array( |
| 142 | 148 | 'ID' => $inserted_ID, |
| @@ -159,41 +165,29 @@ | ||
| 159 | 165 | * @param int $postId |
| 160 | 166 | * @return array |
| 161 | 167 | */ |
| 162 | 168 | public function insert($data, $postId = 0) { |
| 163 | - $data = $this->process_images($data, $postId); | |
| 169 | + $data['content'] = Utils::import_and_replace_attachments($data['content'], $postId); | |
| 164 | 170 | return $data; |
| 165 | 171 | } |
| 166 | 172 | |
| 167 | 173 | /** |
| 168 | - * Inserts a template into the Gutenberg editor. | |
| 169 | - * | |
| 170 | - * @param mixed $data | |
| 171 | - * @param int $postId | |
| 172 | - * @return array | |
| 174 | + * AJAX handler to update the option `templately-gutenberg-hide-buttons` | |
| 173 | 175 | */ |
| 174 | - public function process_images($data, $postId = 0) { | |
| 175 | - // Instantiate GutenbergHelper | |
| 176 | - $helper = new GutenbergHelper(); | |
| 176 | + public function update_gutenberg_hide_buttons() { | |
| 177 | + // Check nonce for security | |
| 178 | + check_ajax_referer( 'templately_nonce', 'nonce' ); | |
| 177 | 179 | |
| 178 | - // Organize URLs from the content | |
| 179 | - $organizedUrls = $helper->parse_images($data['content']); | |
| 180 | + // Get the new value from the AJAX request | |
| 181 | + $hide_buttons = isset($_GET['hide_buttons']) ? sanitize_text_field($_GET['hide_buttons']) : ''; | |
| 180 | 182 | |
| 181 | - // Define template settings | |
| 182 | - $template_settings = [ | |
| 183 | - 'post_id' => $postId, | |
| 184 | - '__attachments' => $organizedUrls, | |
| 185 | - ]; | |
| 183 | + // Update the option | |
| 184 | + update_option('templately-gutenberg-hide-buttons', $hide_buttons); | |
| 186 | 185 | |
| 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; | |
| 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 | + ]); | |
| 198 | 192 | } |
| 199 | 193 | } |