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