| 1 |
<?php |
| 2 |
namespace Templately\Core\Platform; |
| 3 |
|
| 4 |
use Templately\API\Import; |
| 5 |
use Templately\Core\Importer\Utils\Utils; |
| 6 |
use Templately\Core\Platform; |
| 7 |
use Templately\Core\Module; |
| 8 |
use Templately\Utils\Helper; |
| 9 |
|
| 10 |
use WP_Error; |
| 11 |
use function get_permalink; |
| 12 |
use function get_edit_post_link; |
| 13 |
use function wp_insert_post; |
| 14 |
use function wp_slash; |
| 15 |
use function wp_unslash; |
| 16 |
use function json_decode; |
| 17 |
|
| 18 |
class Gutenberg extends Platform { |
| 19 |
/** |
| 20 |
* Platform ID |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $id = 'gutenberg'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Is gutenberg is active or not |
| 27 |
* @var boolean |
| 28 |
*/ |
| 29 |
public $is_gutenberg_active = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* Initializing the platform and add it to module. |
| 33 |
*/ |
| 34 |
public function __construct(){ |
| 35 |
Module::get_instance()->add( (object) [ |
| 36 |
'id' => $this->id, |
| 37 |
'object' => $this |
| 38 |
]); |
| 39 |
|
| 40 |
$this->hooks(); |
| 41 |
} |
| 42 |
|
| 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 |
} |
| 52 |
|
| 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'] ); |
| 61 |
templately()->assets->enqueue( 'templately-gutenberg', 'js/gutenberg.js' ); |
| 62 |
templately()->admin->scripts( 'gutenberg' ); |
| 63 |
} |
| 64 |
|
| 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()); |
| 77 |
|
| 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 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Determine Active UI Theme |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public function ui_theme(){ |
| 100 |
return 'light'; |
| 101 |
} |
| 102 |
|
| 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' ); |
| 116 |
|
| 117 |
if( is_wp_error( $inserted_ID ) ) { |
| 118 |
return $inserted_ID; |
| 119 |
} |
| 120 |
|
| 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 |
} |
| 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 |
if ( is_wp_error( $inserted_ID ) ) { |
| 136 |
return Helper::error( |
| 137 |
'import_failed', |
| 138 |
$inserted_ID->get_error_message(), |
| 139 |
'import/page', |
| 140 |
$inserted_ID->get_error_code() |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 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 |
return [ |
| 156 |
'post_id' => $inserted_ID, |
| 157 |
'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ), |
| 158 |
'visit' => get_permalink( $inserted_ID ) |
| 159 |
]; |
| 160 |
} |
| 161 |
|
| 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 |
} |
| 194 |
} |