| @@ -1,13 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Templately\Core\Platform; |
| 3 | 3 | |
| 4 | 4 | use Templately\API\Import; |
| 5 | -use Templately\Core\Importer\Utils\Utils; | |
| 5 | +use Templately\Core\Importer\Utils\GutenbergHelper; | |
| 6 | 6 | use Templately\Core\Platform; |
| 7 | 7 | use Templately\Core\Module; |
| 8 | 8 | use Templately\Utils\Helper; |
| 9 | -use Templately\Utils\Options; | |
| 10 | 9 | |
| 11 | 10 | use WP_Error; |
| 12 | 11 | use function get_permalink; |
| 13 | 12 | use function get_edit_post_link; |
| @@ -13,9 +12,8 @@ | ||
| 13 | 12 | use function get_edit_post_link; |
| 14 | 13 | use function wp_insert_post; |
| 15 | 14 | use function wp_slash; |
| 16 | 15 | use function wp_unslash; |
| 17 | -use function wp_update_post; | |
| 18 | 16 | use function json_decode; |
| 19 | 17 | |
| 20 | 18 | class Gutenberg extends Platform { |
| 21 | 19 | /** |
| @@ -48,9 +46,8 @@ | ||
| 48 | 46 | */ |
| 49 | 47 | public function hooks(){ |
| 50 | 48 | add_action( 'enqueue_block_editor_assets', [ $this, 'scripts' ] ); |
| 51 | 49 | add_action( 'admin_footer', [ $this, 'print_admin_js_template' ] ); |
| 52 | - add_action( 'wp_ajax_update_gutenberg_hide_buttons', [ $this, 'update_gutenberg_hide_buttons' ] ); // Register AJAX action | |
| 53 | 50 | } |
| 54 | 51 | |
| 55 | 52 | /** |
| 56 | 53 | * Assets Enqueueing |
| @@ -58,9 +55,8 @@ | ||
| 58 | 55 | */ |
| 59 | 56 | public function scripts(){ |
| 60 | 57 | $this->is_gutenberg_active = true; |
| 61 | 58 | templately()->assets->enqueue( 'templately-gutenberg', 'css/gutenberg.css' ); |
| 62 | - templately()->assets->enqueue( 'templately-tailwind', 'css/tailwind.css', ['templately-gutenberg'] ); | |
| 63 | 59 | templately()->assets->enqueue( 'templately-gutenberg', 'js/gutenberg.js' ); |
| 64 | 60 | templately()->admin->scripts( 'gutenberg' ); |
| 65 | 61 | } |
| 66 | 62 | |
| @@ -112,9 +108,9 @@ | ||
| 112 | 108 | * @since 2.0.0 |
| 113 | 109 | * |
| 114 | 110 | * @return array|WP_Error array on success, WP_Error on failure. |
| 115 | 111 | */ |
| 116 | - public function create_page( $id, $title, $importer = null, $settings = [] ){ | |
| 112 | + public function create_page( $id, $title, $importer = null ){ | |
| 117 | 113 | $post_data = $inserted_ID = $importer->get_content( $id, 'gutenberg' ); |
| 118 | 114 | |
| 119 | 115 | if( is_wp_error( $inserted_ID ) ) { |
| 120 | 116 | return $inserted_ID; |
| @@ -119,13 +115,8 @@ | ||
| 119 | 115 | if( is_wp_error( $inserted_ID ) ) { |
| 120 | 116 | return $inserted_ID; |
| 121 | 117 | } |
| 122 | 118 | |
| 123 | - if ( ! empty( $settings ) && ! empty( $inserted_ID['content'] ) && is_string( $inserted_ID['content'] ) ) { | |
| 124 | - $inserted_ID['content'] = \Templately\Core\Importer\Utils\GutenbergSettingsMerger::merge( $inserted_ID['content'], $settings ); | |
| 125 | - $post_data['content'] = $inserted_ID['content']; | |
| 126 | - } | |
| 127 | - | |
| 128 | 119 | if ( ! empty( $inserted_ID['content'] ) ) { |
| 129 | 120 | $inserted_ID = wp_insert_post( array ( |
| 130 | 121 | 'post_status' => 'draft', |
| 131 | 122 | 'post_type' => 'page', |
| @@ -143,9 +134,9 @@ | ||
| 143 | 134 | ); |
| 144 | 135 | } |
| 145 | 136 | |
| 146 | 137 | if($inserted_ID){ |
| 147 | - $post_data['content'] = Utils::import_and_replace_attachments($post_data['content'], $inserted_ID); | |
| 138 | + $post_data = $this->process_images($post_data, $inserted_ID); | |
| 148 | 139 | |
| 149 | 140 | // Update the post content with the processed images |
| 150 | 141 | $updated_post = array( |
| 151 | 142 | 'ID' => $inserted_ID, |
| @@ -168,101 +159,41 @@ | ||
| 168 | 159 | * @param int $postId |
| 169 | 160 | * @return array |
| 170 | 161 | */ |
| 171 | 162 | public function insert($data, $postId = 0) { |
| 172 | - $data['content'] = Utils::import_and_replace_attachments($data['content'], $postId); | |
| 163 | + $data = $this->process_images($data, $postId); | |
| 173 | 164 | return $data; |
| 174 | 165 | } |
| 175 | 166 | |
| 176 | - /** | |
| 177 | - * Import template to Templately Library | |
| 178 | - * | |
| 179 | - * @param integer $id | |
| 180 | - * @param Import $importer | |
| 181 | - * @param array $settings | |
| 182 | - * | |
| 183 | - * @return array|WP_Error array on success, WP_Error on failure. | |
| 184 | - */ | |
| 185 | - public function import_in_library( $id, $importer = null, $settings = [], $template_type = '', $item_type = '' ) { | |
| 186 | - $template_data = $importer->get_content( $id, 'gutenberg', 'remote' ); | |
| 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(); | |
| 187 | 177 | |
| 188 | - if ( is_wp_error( $template_data ) ) { | |
| 189 | - return $template_data; | |
| 190 | - } | |
| 178 | + // Organize URLs from the content | |
| 179 | + $organizedUrls = $helper->parse_images($data['content']); | |
| 191 | 180 | |
| 192 | - if ( ! empty( $settings ) && ! empty( $template_data['content'] ) && is_string( $template_data['content'] ) ) { | |
| 193 | - $template_data['content'] = \Templately\Core\Importer\Utils\GutenbergSettingsMerger::merge( $template_data['content'], $settings ); | |
| 194 | - } | |
| 181 | + // Define template settings | |
| 182 | + $template_settings = [ | |
| 183 | + 'post_id' => $postId, | |
| 184 | + '__attachments' => $organizedUrls, | |
| 185 | + ]; | |
| 195 | 186 | |
| 196 | - // Handle attachment replacements before saving | |
| 197 | - $template_data['content'] = Utils::import_and_replace_attachments( $template_data['content'], 0 ); | |
| 187 | + // Map post IDs and disable logging | |
| 188 | + $helper->map_post_ids[$postId] = $postId; | |
| 189 | + $helper->shouldLog = false; | |
| 198 | 190 | |
| 199 | - // Frontend-provided template_type (from item details API) takes precedence over | |
| 200 | - // whatever the fetched content JSON reports. | |
| 201 | - if ( ! empty( $template_type ) ) { | |
| 202 | - $template_data['template_type'] = $template_type; | |
| 203 | - } | |
| 191 | + // Prepare the helper with the data and settings | |
| 192 | + $helper->prepare($data, $template_settings); | |
| 204 | 193 | |
| 205 | - // Resolve the correct Builder Type using template_type before passing to the importer. | |
| 206 | - try { | |
| 207 | - $type = static::resolve_library_type( $template_data ); | |
| 208 | - } catch ( \InvalidArgumentException $e ) { | |
| 209 | - return Helper::error( 'unsupported_template_type', $e->getMessage(), 'import', 400 ); | |
| 210 | - } | |
| 194 | + // Update the content in the data array | |
| 195 | + $data['content'] = wp_unslash($helper->get_content()); | |
| 211 | 196 | |
| 212 | - $factory = new \Templately\Builder\Factory\TemplateFactory( 'gutenberg' ); | |
| 213 | - | |
| 214 | - $template = $factory->create( $type, [ | |
| 215 | - 'post_title' => $template_data['title'], | |
| 216 | - 'post_status' => 'publish', | |
| 217 | - 'post_type' => 'templately_library', | |
| 218 | - ] ); | |
| 219 | - | |
| 220 | - if ( is_wp_error( $template ) ) { | |
| 221 | - return $template; | |
| 222 | - } | |
| 223 | - | |
| 224 | - $template->import( array_merge( $template_data, [ | |
| 225 | - 'import_settings' => [ | |
| 226 | - 'title' => $template_data['title'], | |
| 227 | - 'type' => $type, | |
| 228 | - ] | |
| 229 | - ] ) ); | |
| 230 | - | |
| 231 | - $post_id = $template->get_main_id(); | |
| 232 | - | |
| 233 | - // Companion content for archive/fluent types — same side effects (Shop page, | |
| 234 | - // page_for_posts, LearnDash courses slug, fluent single-page template) the FSI | |
| 235 | - // Templates runner performs when importing these template types. | |
| 236 | - Utils::create_companion_content( $type, $template_data['title'] ?? '', 'gutenberg' ); | |
| 237 | - | |
| 238 | - // Mark user as having imported a template | |
| 239 | - Options::get_instance()->set( 'has_imported_template', true ); | |
| 240 | - | |
| 241 | - return [ | |
| 242 | - 'post_id' => $post_id, | |
| 243 | - 'edit_link' => get_edit_post_link( $post_id, 'internal' ), | |
| 244 | - 'visit' => get_permalink( $post_id ), | |
| 245 | - ]; | |
| 246 | - } | |
| 247 | - | |
| 248 | - /** | |
| 249 | - * AJAX handler to update the option `templately-gutenberg-hide-buttons` | |
| 250 | - */ | |
| 251 | - public function update_gutenberg_hide_buttons() { | |
| 252 | - // Check nonce for security | |
| 253 | - check_ajax_referer( 'templately_nonce', 'nonce' ); | |
| 254 | - | |
| 255 | - // Get the new value from the AJAX request | |
| 256 | - $hide_buttons = isset($_GET['hide_buttons']) ? sanitize_text_field($_GET['hide_buttons']) : ''; | |
| 257 | - | |
| 258 | - // Update the option | |
| 259 | - update_option('templately-gutenberg-hide-buttons', $hide_buttons); | |
| 260 | - | |
| 261 | - $hide_buttons = get_option('templately-gutenberg-hide-buttons', 'no'); | |
| 262 | - $hide_buttons = $hide_buttons === 'yes' ? 'yes' : 'no'; | |
| 263 | - // Send a response back to the JavaScript | |
| 264 | - wp_send_json_success([ | |
| 265 | - 'hide_buttons' => $hide_buttons, | |
| 266 | - ]); | |
| 197 | + return $data; | |
| 267 | 198 | } |
| 268 | 199 | } |