| @@ -1,9 +1,8 @@ | ||
| 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 | 8 | |
| @@ -15,23 +14,23 @@ | ||
| 15 | 14 | use function wp_unslash; |
| 16 | 15 | use function json_decode; |
| 17 | 16 | |
| 18 | 17 | class Gutenberg extends Platform { |
| 19 | - /** | |
| 20 | - * Platform ID | |
| 21 | - * @var string | |
| 22 | - */ | |
| 18 | + /** | |
| 19 | + * Platform ID | |
| 20 | + * @var string | |
| 21 | + */ | |
| 23 | 22 | private $id = 'gutenberg'; |
| 24 | 23 | |
| 25 | - /** | |
| 26 | - * Is gutenberg is active or not | |
| 27 | - * @var boolean | |
| 28 | - */ | |
| 29 | - public $is_gutenberg_active = false; | |
| 24 | + /** | |
| 25 | + * Is gutenberg is active or not | |
| 26 | + * @var boolean | |
| 27 | + */ | |
| 28 | + public $is_gutenberg_active = false; | |
| 30 | 29 | |
| 31 | - /** | |
| 32 | - * Initializing the platform and add it to module. | |
| 33 | - */ | |
| 30 | + /** | |
| 31 | + * Initializing the platform and add it to module. | |
| 32 | + */ | |
| 34 | 33 | public function __construct(){ |
| 35 | 34 | Module::get_instance()->add( (object) [ |
| 36 | 35 | 'id' => $this->id, |
| 37 | 36 | 'object' => $this |
| @@ -36,103 +35,95 @@ | ||
| 36 | 35 | 'id' => $this->id, |
| 37 | 36 | 'object' => $this |
| 38 | 37 | ]); |
| 39 | 38 | |
| 40 | - $this->hooks(); | |
| 39 | + $this->hooks(); | |
| 41 | 40 | } |
| 42 | 41 | |
| 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 | - } | |
| 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 | + } | |
| 52 | 50 | |
| 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-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' ); | |
| 61 | 58 | templately()->assets->enqueue( 'templately-gutenberg', 'js/gutenberg.js' ); |
| 62 | - templately()->admin->scripts( 'gutenberg' ); | |
| 59 | + templately()->admin->scripts( 'gutenberg' ); | |
| 63 | 60 | } |
| 64 | 61 | |
| 65 | - /** | |
| 66 | - * Templately Button and Wrapper for Gutenberg | |
| 67 | - * | |
| 68 | - * @since 2.0.0 | |
| 69 | - * | |
| 70 | - * @return void | |
| 71 | - */ | |
| 72 | - public function print_admin_js_template() { | |
| 73 | - if ( ! $this->is_gutenberg_active ) { | |
| 74 | - return; | |
| 75 | - } | |
| 76 | - $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 | + } | |
| 77 | 73 | |
| 78 | - ?> | |
| 79 | - <div id="templately-gutenberg"></div> | |
| 80 | - <script id="templately-gutenberg-button-switch-mode" type="text/html"> | |
| 81 | - <div id="templately-gutenberg-buttons"> | |
| 82 | - <button id="templately-gutenberg-button" type="button" class="button button-primary button-large gutenberg-add-templately-button"> | |
| 83 | - <i class="templately-icon" aria-hidden="true"></i> | |
| 84 | - <?php echo esc_html__( 'Templately', 'templately' ); ?> | |
| 85 | - </button> | |
| 86 | - <button id="templately-cloud-push" type="button" class="button button-primary button-large"> | |
| 87 | - <i class="templately-cloud-icon" aria-hidden="true"></i> | |
| 88 | - <?php echo sprintf( __( 'Save %s in Templately', 'templately' ), $post_type ); ?> | |
| 89 | - </button> | |
| 90 | - </div> | |
| 91 | - </script> | |
| 92 | - <?php | |
| 93 | - } | |
| 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 | + } | |
| 94 | 90 | |
| 95 | - /** | |
| 96 | - * Determine Active UI Theme | |
| 97 | - * @return string | |
| 98 | - */ | |
| 99 | - public function ui_theme(){ | |
| 100 | - return 'light'; | |
| 101 | - } | |
| 91 | + /** | |
| 92 | + * Determine Active UI Theme | |
| 93 | + * @return string | |
| 94 | + */ | |
| 95 | + public function ui_theme(){ | |
| 96 | + return 'light'; | |
| 97 | + } | |
| 102 | 98 | |
| 103 | - /** | |
| 104 | - * Creating a gutenberg page | |
| 105 | - * | |
| 106 | - * @param integer $id | |
| 107 | - * @param string $title | |
| 108 | - * @param Import $importer | |
| 109 | - * | |
| 110 | - * @since 2.0.0 | |
| 111 | - * | |
| 112 | - * @return array|WP_Error array on success, WP_Error on failure. | |
| 113 | - */ | |
| 114 | - public function create_page( $id, $title, $importer = null, $settings = [] ){ | |
| 115 | - $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' ); | |
| 116 | 112 | |
| 117 | 113 | if( is_wp_error( $inserted_ID ) ) { |
| 118 | 114 | return $inserted_ID; |
| 119 | 115 | } |
| 120 | 116 | |
| 121 | - if ( ! empty( $settings ) && ! empty( $inserted_ID['content'] ) && is_string( $inserted_ID['content'] ) ) { | |
| 122 | - $inserted_ID['content'] = \Templately\Core\Importer\Utils\GutenbergSettingsMerger::merge( $inserted_ID['content'], $settings ); | |
| 123 | - $post_data['content'] = $inserted_ID['content']; | |
| 124 | - } | |
| 117 | + if ( ! empty( $inserted_ID['content'] ) ) { | |
| 118 | + $inserted_ID = wp_insert_post( array ( | |
| 119 | + 'post_status' => 'draft', | |
| 120 | + 'post_type' => 'page', | |
| 121 | + 'post_title' => $title, | |
| 122 | + 'post_content' => wp_slash($inserted_ID['content']), | |
| 123 | + ) ); | |
| 124 | + } | |
| 125 | 125 | |
| 126 | - if ( ! empty( $inserted_ID['content'] ) ) { | |
| 127 | - $inserted_ID = wp_insert_post( array ( | |
| 128 | - 'post_status' => 'draft', | |
| 129 | - 'post_type' => 'page', | |
| 130 | - 'post_title' => $title, | |
| 131 | - 'post_content' => wp_slash($inserted_ID['content']), | |
| 132 | - ) ); | |
| 133 | - } | |
| 134 | - | |
| 135 | 126 | if ( is_wp_error( $inserted_ID ) ) { |
| 136 | 127 | return Helper::error( |
| 137 | 128 | 'import_failed', |
| 138 | 129 | $inserted_ID->get_error_message(), |
| @@ -140,19 +131,8 @@ | ||
| 140 | 131 | $inserted_ID->get_error_code() |
| 141 | 132 | ); |
| 142 | 133 | } |
| 143 | 134 | |
| 144 | - if($inserted_ID){ | |
| 145 | - $post_data['content'] = Utils::import_and_replace_attachments($post_data['content'], $inserted_ID); | |
| 146 | - | |
| 147 | - // Update the post content with the processed images | |
| 148 | - $updated_post = array( | |
| 149 | - 'ID' => $inserted_ID, | |
| 150 | - 'post_content' => wp_slash($post_data['content']), | |
| 151 | - ); | |
| 152 | - wp_update_post($updated_post); | |
| 153 | - } | |
| 154 | - | |
| 155 | 135 | return [ |
| 156 | 136 | 'post_id' => $inserted_ID, |
| 157 | 137 | 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ), |
| 158 | 138 | 'visit' => get_permalink( $inserted_ID ) |
| @@ -158,37 +138,14 @@ | ||
| 158 | 138 | 'visit' => get_permalink( $inserted_ID ) |
| 159 | 139 | ]; |
| 160 | 140 | } |
| 161 | 141 | |
| 162 | - /** | |
| 163 | - * Inserts a template into the Gutenberg editor. | |
| 164 | - * | |
| 165 | - * @param mixed $data | |
| 166 | - * @param int $postId | |
| 167 | - * @return array | |
| 168 | - */ | |
| 169 | - public function insert($data, $postId = 0) { | |
| 170 | - $data['content'] = Utils::import_and_replace_attachments($data['content'], $postId); | |
| 171 | - return $data; | |
| 172 | - } | |
| 173 | - | |
| 174 | - /** | |
| 175 | - * AJAX handler to update the option `templately-gutenberg-hide-buttons` | |
| 176 | - */ | |
| 177 | - public function update_gutenberg_hide_buttons() { | |
| 178 | - // Check nonce for security | |
| 179 | - check_ajax_referer( 'templately_nonce', 'nonce' ); | |
| 180 | - | |
| 181 | - // Get the new value from the AJAX request | |
| 182 | - $hide_buttons = isset($_GET['hide_buttons']) ? sanitize_text_field($_GET['hide_buttons']) : ''; | |
| 183 | - | |
| 184 | - // Update the option | |
| 185 | - update_option('templately-gutenberg-hide-buttons', $hide_buttons); | |
| 186 | - | |
| 187 | - $hide_buttons = get_option('templately-gutenberg-hide-buttons', 'no'); | |
| 188 | - $hide_buttons = $hide_buttons === 'yes' ? 'yes' : 'no'; | |
| 189 | - // Send a response back to the JavaScript | |
| 190 | - wp_send_json_success([ | |
| 191 | - 'hide_buttons' => $hide_buttons, | |
| 192 | - ]); | |
| 193 | - } | |
| 142 | + /** | |
| 143 | + * Inserting Template in Gutenberg Editor | |
| 144 | + * | |
| 145 | + * @param mixed $data | |
| 146 | + * @return array | |
| 147 | + */ | |
| 148 | + public function insert( $data ){ | |
| 149 | + return $data; | |
| 150 | + } | |
| 194 | 151 | } |