| 1 |
<?php |
| 2 |
namespace Templately\Core\Platform; |
| 3 |
|
| 4 |
use Templately\API\Import; |
| 5 |
use Templately\Core\Platform; |
| 6 |
use Templately\Core\Module; |
| 7 |
use Templately\Utils\Helper; |
| 8 |
|
| 9 |
use WP_Error; |
| 10 |
use function get_permalink; |
| 11 |
use function get_edit_post_link; |
| 12 |
use function wp_insert_post; |
| 13 |
use function wp_slash; |
| 14 |
use function wp_unslash; |
| 15 |
use function json_decode; |
| 16 |
|
| 17 |
class Gutenberg extends Platform { |
| 18 |
/** |
| 19 |
* Platform ID |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $id = 'gutenberg'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Is gutenberg is active or not |
| 26 |
* @var boolean |
| 27 |
*/ |
| 28 |
public $is_gutenberg_active = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initializing the platform and add it to module. |
| 32 |
*/ |
| 33 |
public function __construct(){ |
| 34 |
Module::get_instance()->add( (object) [ |
| 35 |
'id' => $this->id, |
| 36 |
'object' => $this |
| 37 |
]); |
| 38 |
|
| 39 |
$this->hooks(); |
| 40 |
} |
| 41 |
|
| 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 |
} |
| 50 |
|
| 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' ); |
| 58 |
templately()->assets->enqueue( 'templately-gutenberg', 'js/gutenberg.js' ); |
| 59 |
templately()->admin->scripts( 'gutenberg' ); |
| 60 |
} |
| 61 |
|
| 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 |
} |
| 73 |
|
| 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 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Determine Active UI Theme |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public function ui_theme(){ |
| 96 |
return 'light'; |
| 97 |
} |
| 98 |
|
| 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' ); |
| 112 |
|
| 113 |
if( is_wp_error( $inserted_ID ) ) { |
| 114 |
return $inserted_ID; |
| 115 |
} |
| 116 |
|
| 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 |
|
| 126 |
if ( is_wp_error( $inserted_ID ) ) { |
| 127 |
return Helper::error( |
| 128 |
'import_failed', |
| 129 |
$inserted_ID->get_error_message(), |
| 130 |
'import/page', |
| 131 |
$inserted_ID->get_error_code() |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
return [ |
| 136 |
'post_id' => $inserted_ID, |
| 137 |
'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ), |
| 138 |
'visit' => get_permalink( $inserted_ID ) |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 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 |
} |
| 151 |
} |