| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gutenberg Manager class |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! class_exists( 'LoftLoader_Gutenberg_Any_Page' ) ) { |
| 7 |
class LoftLoader_Gutenberg_Any_Page { |
| 8 |
/** |
| 9 |
* Array of post meta name list |
| 10 |
*/ |
| 11 |
public $page_meta_list = array( |
| 12 |
'loftloader_page_shortcode' => 'string' |
| 13 |
); |
| 14 |
/** |
| 15 |
* Construct function |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_action( 'rest_api_init', array( $this, 'api_update_post_meta' ) ); |
| 19 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); |
| 20 |
$this->register_metas(); |
| 21 |
} |
| 22 |
/** |
| 23 |
* Enqueue editor assets |
| 24 |
*/ |
| 25 |
public function enqueue_editor_assets() { |
| 26 |
switch ( $this->get_current_post_type() ) { |
| 27 |
case 'page': |
| 28 |
wp_enqueue_script( |
| 29 |
'loftloader-gutenberg-any-page-script', |
| 30 |
LOFTLOADER_URI. 'inc/any-page/gutenberg/plugin.js', |
| 31 |
array( 'wp-blocks', 'wp-element', 'wp-i18n' ), |
| 32 |
LOFTLOADER_ASSET_VERSION, |
| 33 |
true |
| 34 |
); |
| 35 |
break; |
| 36 |
} |
| 37 |
} |
| 38 |
/** |
| 39 |
* Register metas for gutenberg |
| 40 |
*/ |
| 41 |
public function register_metas() { |
| 42 |
foreach( $this->page_meta_list as $id => $type ) { |
| 43 |
register_meta( 'post', $id, array( |
| 44 |
'auth_callback' => array( $this, 'permission_check' ), |
| 45 |
'show_in_rest' => true, |
| 46 |
'single' => true, |
| 47 |
'type' => $type |
| 48 |
) ); |
| 49 |
} |
| 50 |
} |
| 51 |
/** |
| 52 |
* Check permission for meta registration |
| 53 |
*/ |
| 54 |
public function permission_check( $arg1, $meta_name, $post_id ) { |
| 55 |
return current_user_can( 'edit_post', $post_id ); |
| 56 |
} |
| 57 |
/** |
| 58 |
* Update post metas from gutenberg |
| 59 |
*/ |
| 60 |
public function api_update_post_meta() { |
| 61 |
register_rest_route( |
| 62 |
'loftloader/v2', '/update-meta', array( |
| 63 |
'methods' => 'POST', |
| 64 |
'callback' => array( $this, 'update_callback' ), |
| 65 |
'args' => array( |
| 66 |
'id' => array( |
| 67 |
'sanitize_callback' => 'absint', |
| 68 |
) |
| 69 |
) |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
/** |
| 74 |
* Hello Gutenberg REST API Callback for Gutenberg |
| 75 |
*/ |
| 76 |
function update_callback( $data ) { |
| 77 |
$post_id = $data['id']; |
| 78 |
switch ( get_post_type( $post_id ) ) { |
| 79 |
case 'page': |
| 80 |
$meta_names = array_keys( $this->page_meta_list ); |
| 81 |
break; |
| 82 |
default: |
| 83 |
$meta_names = array(); |
| 84 |
} |
| 85 |
foreach( $meta_names as $meta_name ) { |
| 86 |
update_post_meta( $post_id, $meta_name, $data[ $meta_name ] ); |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
/** |
| 91 |
* Get current post type |
| 92 |
* @return mix post type string or boolean false |
| 93 |
*/ |
| 94 |
protected function get_current_post_type() { |
| 95 |
global $post; |
| 96 |
if ( is_admin() && ! empty( $post ) && ! empty( $post->post_type ) ) { |
| 97 |
return $post->post_type; |
| 98 |
} else { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
function loftloader_init_gutenberg_any_page() { |
| 104 |
new LoftLoader_Gutenberg_Any_Page(); |
| 105 |
} |
| 106 |
add_action( 'init', 'loftloader_init_gutenberg_any_page' ); |
| 107 |
} |