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