PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / theme-builder / REST / ThemeBuilderApi.php

ThemeBuilderApi.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/theme-builder/REST/ThemeBuilderApi.php

85 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\ThemeBuilder\REST;
4
5 use Templately\Admin\Roles;
6 use Templately\API\API;
7 use Templately\Modules\ThemeBuilder\Types\BaseTemplate;
8 use WP_Error;
9 use WP_REST_Request;
10
11 class ThemeBuilderApi extends API {
12 public function permission_check( WP_REST_Request $request ) {
13 if ( $request->get_route() === '/templately/v1/create-template' ) {
14 if ( ! wp_verify_nonce( $request->get_param( 'nonce' ), 'templately_nonce' ) ) {
15 return false;
16 }
17
18 // `templately_library` is site-structure control, so its capability map
19 // resolves every write primitive to `edit_templately_builder`. The old
20 // `edit_posts` bar let the Author role create, publish and condition a
21 // site-wide header/footer/archive template with no admin review.
22 if ( ! Roles::can_manage_builder() ) {
23 return new WP_Error(
24 'rest_forbidden',
25 __( 'Sorry, you are not allowed to create templates.', 'templately' ),
26 [ 'status' => rest_authorization_required_code() ]
27 );
28 }
29
30 return true;
31 }
32
33 return parent::permission_check( $request );
34 }
35
36 public function register_routes() {
37 $this->post( 'create-template', [ $this, 'create' ], [
38 'platform' => [
39 'required' => true,
40 'validate_callback' => function ( $param ) {
41 return is_string( $param ) && ( $param == 'elementor' || 'gutenberg' == $param );
42 }
43 ],
44 'type' => [
45 'required' => true,
46 'validate_callback' => function ( $param ) {
47 return is_string( $param ) && array_key_exists( $param, templately()->theme_builder::$templates_manager->get_template_types() );
48 }
49 ],
50 'title' => [
51 'required' => true,
52 ]
53 ] );
54 }
55
56 public function create( WP_REST_Request $request ) {
57 $platform = $request->get_param( 'platform' );
58 $template_type = $request->get_param( 'type' );
59 $title = $request->get_param( 'title' );
60
61 if ( empty( $title ) ) {
62 $title = '';
63 }
64
65 $post_data = [
66 'post_title' => $title,
67 'post_status' => 'publish',
68 'post_type' => 'templately_library'
69 ];
70
71 /**
72 * @var BaseTemplate $template
73 */
74 $template = templately()->theme_builder::$templates_manager->create( $template_type, $post_data, [ 'platform' => $platform ] );
75
76 if ( is_wp_error( $template ) ) {
77 /**
78 * @var WP_Error $template ;
79 */
80 return $this->error( 'failed_template_creation', sprintf( __( 'Something went wrong. %s', 'templately' ), $template->get_error_message() ) );
81 }
82
83 return $this->success( $template->get_edit_url() );
84 }
85 }