| @@ -1,77 +1,79 @@ | ||
| 1 | 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 | - )); | |
| 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 | + )); | |
| 16 | 17 | } |
| 17 | 18 | |
| 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); | |
| 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 | 43 | } |
| 44 | + return new WP_REST_Response($notice, 200); | |
| 45 | + } | |
| 44 | 46 | |
| 45 | 47 | |
| 46 | - // Register Custom Post Type | |
| 47 | - function blockspare_custom_post_type() { | |
| 48 | + // Register Custom Post Type | |
| 49 | + function blockspare_custom_post_type() | |
| 50 | + { | |
| 48 | 51 | |
| 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' ), | |
| 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'), | |
| 54 | 57 | |
| 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 ); | |
| 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 | 74 | |
| 75 | - } | |
| 76 | - add_action( 'init', 'blockspare_custom_post_type',1 ); | |
| 77 | -} | |
| 75 | + ); | |
| 76 | + register_post_type('bs_templates', $args); | |
| 77 | + } | |
| 78 | + add_action('init', 'blockspare_custom_post_type', 1); | |
| 79 | +} | |