| 1 |
<?php |
| 2 |
add_action( 'rest_api_init', 'blockspare_save_templates_rest_controller'); |
| 3 |
function blockspare_save_templates_rest_controller() { |
| 4 |
register_rest_route('blockspare-save-templates/v1', '/save_templates', array( |
| 5 |
'methods'=>\WP_REST_Server::EDITABLE, |
| 6 |
'callback' => 'blockspare_save_templates_callback', |
| 7 |
'permission_callback' => function ($request) { |
| 8 |
$nonce = $request->get_header( 'X-WP-Nonce' ); |
| 9 |
if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) && !current_user_can( 'edit_posts' )) { |
| 10 |
return new WP_Error( 'rest_forbidden', 'Validation Failed', array( 'status' => 200 ) ); |
| 11 |
} |
| 12 |
|
| 13 |
return true; |
| 14 |
}, |
| 15 |
)); |
| 16 |
} |
| 17 |
|
| 18 |
if(!function_exists('blockspare_custom_post_type')){ |
| 19 |
function blockspare_save_templates_callback(\WP_REST_Request $request) { |
| 20 |
$params = $request->get_params(); |
| 21 |
$post_content = $params['data']; |
| 22 |
$post_title = sanitize_text_field($params['title']); |
| 23 |
$category = $params['category']; |
| 24 |
|
| 25 |
$new_post = array( |
| 26 |
'post_content' => $post_content, |
| 27 |
'post_title' => $post_title, |
| 28 |
'post_type' => 'bs_templates', |
| 29 |
'post_status' => 'publish', // You can set to 'draft' if you don't want to publish immediately |
| 30 |
); |
| 31 |
|
| 32 |
// Insert the post into the database |
| 33 |
$post_id = wp_insert_post($new_post); |
| 34 |
|
| 35 |
// Check if the post was successfully inserted |
| 36 |
if ($post_id) { |
| 37 |
//update_post_meta($post_id, 'bs_template_category',$category); |
| 38 |
$notice='success'; |
| 39 |
} else { |
| 40 |
$notice='success'; |
| 41 |
} |
| 42 |
return new WP_REST_Response($notice, 200); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
// Register Custom Post Type |
| 47 |
function blockspare_custom_post_type() { |
| 48 |
|
| 49 |
$labels = array( |
| 50 |
'name' => _x( 'My Templates', 'Post Type General Name', 'blockspare' ), |
| 51 |
'singular_name' => _x( 'My Templates', 'Post Type Singular Name', 'blockspare' ), |
| 52 |
'menu_name' => __( 'My Templates', 'blockspare' ), |
| 53 |
'parent_item_colon' => __( 'My Parent Item:', 'blockspare' ), |
| 54 |
|
| 55 |
); |
| 56 |
$args = array( |
| 57 |
'label' => __( 'My Templates', 'blockspare' ), |
| 58 |
'description' => __( 'Tempaltes created by users', 'blockspare' ), |
| 59 |
'labels' => $labels, |
| 60 |
'supports' => array( 'title', 'editor', 'thumbnail','blockeditor' ), |
| 61 |
'hierarchical' => false, |
| 62 |
'public' => true, |
| 63 |
'show_ui' => true, |
| 64 |
'show_in_menu' => false, |
| 65 |
'show_in_rest' =>true, |
| 66 |
'show_in_admin_bar' => false, |
| 67 |
'show_in_nav_menus' => false, |
| 68 |
'can_export' => false, |
| 69 |
'has_archive' => false, // Set to false to exclude from archives |
| 70 |
'exclude_from_search' => false, |
| 71 |
|
| 72 |
); |
| 73 |
register_post_type( 'bs_templates', $args ); |
| 74 |
|
| 75 |
} |
| 76 |
add_action( 'init', 'blockspare_custom_post_type',1 ); |
| 77 |
} |