PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / trunk
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites vtrunk
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / inc / layout / save-templates.php

save-templates.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites trunk, at inc/layout/save-templates.php

77 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }