| @@ -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\GutenbergHelper; | |
| 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,95 +35,94 @@ | ||
| 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 | - } | |
| 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 | + } | |
| 51 | 50 | |
| 52 | - /** | |
| 53 | - * Assets Enqueueing | |
| 54 | - * @return void | |
| 55 | - */ | |
| 56 | - public function scripts(){ | |
| 57 | - $this->is_gutenberg_active = true; | |
| 58 | - templately()->assets->enqueue( 'templately-gutenberg', 'css/gutenberg.css' ); | |
| 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' ); | |
| 59 | 58 | templately()->assets->enqueue( 'templately-gutenberg', 'js/gutenberg.js' ); |
| 60 | - templately()->admin->scripts( 'gutenberg' ); | |
| 59 | + templately()->admin->scripts( 'gutenberg' ); | |
| 61 | 60 | } |
| 62 | 61 | |
| 63 | - /** | |
| 64 | - * Templately Button and Wrapper for Gutenberg | |
| 65 | - * | |
| 66 | - * @since 2.0.0 | |
| 67 | - * | |
| 68 | - * @return void | |
| 69 | - */ | |
| 70 | - public function print_admin_js_template() { | |
| 71 | - if ( ! $this->is_gutenberg_active ) { | |
| 72 | - return; | |
| 73 | - } | |
| 74 | - $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 | + } | |
| 75 | 73 | |
| 76 | - ?> | |
| 77 | - <div id="templately-gutenberg"></div> | |
| 78 | - <script id="templately-gutenberg-button-switch-mode" type="text/html"> | |
| 79 | - <div id="templately-gutenberg-buttons"> | |
| 80 | - <button id="templately-gutenberg-button" type="button" class="button button-primary button-large gutenberg-add-templately-button"> | |
| 81 | - <i class="templately-icon" aria-hidden="true"></i> | |
| 82 | - <?php echo esc_html__( 'Templately', 'templately' ); ?> | |
| 83 | - </button> | |
| 84 | - <button id="templately-cloud-push" type="button" class="button button-primary button-large"> | |
| 85 | - <i class="templately-cloud-icon" aria-hidden="true"></i> | |
| 86 | - <?php echo sprintf( __( 'Save %s in Templately', 'templately' ), $post_type ); ?> | |
| 87 | - </button> | |
| 88 | - </div> | |
| 89 | - </script> | |
| 90 | - <?php | |
| 91 | - } | |
| 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 | + } | |
| 92 | 90 | |
| 93 | - /** | |
| 94 | - * Determine Active UI Theme | |
| 95 | - * @return string | |
| 96 | - */ | |
| 97 | - public function ui_theme(){ | |
| 98 | - return 'light'; | |
| 99 | - } | |
| 91 | + /** | |
| 92 | + * Determine Active UI Theme | |
| 93 | + * @return string | |
| 94 | + */ | |
| 95 | + public function ui_theme(){ | |
| 96 | + return 'light'; | |
| 97 | + } | |
| 100 | 98 | |
| 101 | - /** | |
| 102 | - * Creating a gutenberg page | |
| 103 | - * | |
| 104 | - * @param integer $id | |
| 105 | - * @param string $title | |
| 106 | - * @param Import $importer | |
| 107 | - * | |
| 108 | - * @since 2.0.0 | |
| 109 | - * | |
| 110 | - * @return array|WP_Error array on success, WP_Error on failure. | |
| 111 | - */ | |
| 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 | + */ | |
| 112 | 110 | public function create_page( $id, $title, $importer = null ){ |
| 113 | - $post_data = $inserted_ID = $importer->get_content( $id, 'gutenberg' ); | |
| 111 | + $inserted_ID = $importer->get_content( $id, 'gutenberg' ); | |
| 114 | 112 | |
| 115 | 113 | if( is_wp_error( $inserted_ID ) ) { |
| 116 | 114 | return $inserted_ID; |
| 117 | 115 | } |
| 118 | 116 | |
| 119 | - if ( ! empty( $inserted_ID['content'] ) ) { | |
| 120 | - $inserted_ID = wp_insert_post( array ( | |
| 121 | - 'post_status' => 'draft', | |
| 122 | - 'post_type' => 'page', | |
| 123 | - 'post_title' => $title, | |
| 124 | - 'post_content' => wp_slash($inserted_ID['content']), | |
| 125 | - ) ); | |
| 126 | - } | |
| 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 | + } | |
| 127 | 125 | |
| 128 | 126 | if ( is_wp_error( $inserted_ID ) ) { |
| 129 | 127 | return Helper::error( |
| 130 | 128 | 'import_failed', |
| @@ -133,19 +131,8 @@ | ||
| 133 | 131 | $inserted_ID->get_error_code() |
| 134 | 132 | ); |
| 135 | 133 | } |
| 136 | 134 | |
| 137 | - if($inserted_ID){ | |
| 138 | - $post_data = $this->process_images($post_data, $inserted_ID); | |
| 139 | - | |
| 140 | - // Update the post content with the processed images | |
| 141 | - $updated_post = array( | |
| 142 | - 'ID' => $inserted_ID, | |
| 143 | - 'post_content' => wp_slash($post_data['content']), | |
| 144 | - ); | |
| 145 | - wp_update_post($updated_post); | |
| 146 | - } | |
| 147 | - | |
| 148 | 135 | return [ |
| 149 | 136 | 'post_id' => $inserted_ID, |
| 150 | 137 | 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ), |
| 151 | 138 | 'visit' => get_permalink( $inserted_ID ) |
| @@ -151,49 +138,14 @@ | ||
| 151 | 138 | 'visit' => get_permalink( $inserted_ID ) |
| 152 | 139 | ]; |
| 153 | 140 | } |
| 154 | 141 | |
| 155 | - /** | |
| 156 | - * Inserts a template into the Gutenberg editor. | |
| 157 | - * | |
| 158 | - * @param mixed $data | |
| 159 | - * @param int $postId | |
| 160 | - * @return array | |
| 161 | - */ | |
| 162 | - public function insert($data, $postId = 0) { | |
| 163 | - $data = $this->process_images($data, $postId); | |
| 164 | - return $data; | |
| 165 | - } | |
| 166 | - | |
| 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(); | |
| 177 | - | |
| 178 | - // Organize URLs from the content | |
| 179 | - $organizedUrls = $helper->parse_images($data['content']); | |
| 180 | - | |
| 181 | - // Define template settings | |
| 182 | - $template_settings = [ | |
| 183 | - 'post_id' => $postId, | |
| 184 | - '__attachments' => $organizedUrls, | |
| 185 | - ]; | |
| 186 | - | |
| 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; | |
| 198 | - } | |
| 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 | + } | |
| 199 | 151 | } |