PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0-beta1
Elementor Website Builder – more than just a page builder v4.2.0-beta1
4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 All 455 releases
elementor / modules / mcp / abilities / create-page-ability.php

create-page-ability.php in Elementor Website Builder – more than just a page builder 4.2.0-beta1, at modules/mcp/abilities/create-page-ability.php

138 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Mcp\Abilities;
4
5 use Elementor\Plugin;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 class Create_Page_Ability extends Abstract_Ability {
12
13 protected function get_ability_id(): string {
14 return 'elementor/create-page';
15 }
16
17 protected function get_definition(): Ability_Definition {
18 return new Ability_Definition(
19 __( 'Create Elementor Page', 'elementor' ),
20 __( 'Creates a new draft post or page and marks it as built with Elementor (blank canvas in the editor). Use when the user wants a new layout shell to design in Elementor. Returns the new post ID and edit URL.', 'elementor' ),
21 'elementor',
22 [
23 'type' => 'object',
24 'properties' => [
25 'id' => [ 'type' => 'integer' ],
26 'edit_url' => [ 'type' => 'string' ],
27 'status' => [ 'type' => 'string' ],
28 'type' => [ 'type' => 'string' ],
29 ],
30 ],
31 [
32 'annotations' => [
33 'readonly' => false,
34 'idempotent' => false,
35 'destructive' => false,
36 ],
37 ],
38 function () {
39 return current_user_can( 'edit_posts' );
40 },
41 [
42 'type' => 'object',
43 'properties' => [
44 'title' => [
45 'type' => 'string',
46 'description' => 'Optional title for the new post.',
47 ],
48 'post_type' => [
49 'type' => 'string',
50 'description' => 'Post type slug; must support Elementor. Defaults to page.',
51 'default' => 'page',
52 ],
53 ],
54 ]
55 );
56 }
57
58 public function execute( $input = [] ) {
59 $input = is_array( $input ) ? $input : [];
60
61 $post_type = ! empty( $input['post_type'] ) ? sanitize_key( $input['post_type'] ) : 'page';
62
63 $type_error = $this->validate_post_type( $post_type );
64 if ( $type_error ) {
65 return $type_error;
66 }
67
68 $permission_error = $this->check_create_permission( $post_type );
69 if ( $permission_error ) {
70 return $permission_error;
71 }
72
73 $title = isset( $input['title'] ) && is_string( $input['title'] ) ? $input['title'] : '';
74
75 return $this->create_post( $post_type, $title );
76 }
77
78 private function validate_post_type( string $post_type ): ?\WP_Error {
79 if ( ! post_type_exists( $post_type ) || ! post_type_supports( $post_type, 'elementor' ) ) {
80 return new \WP_Error(
81 'invalid_post_type',
82 __( 'This post type does not support Elementor.', 'elementor' ),
83 [ 'status' => \WP_Http::BAD_REQUEST ]
84 );
85 }
86
87 return null;
88 }
89
90 private function check_create_permission( string $post_type ): ?\WP_Error {
91 $post_type_object = get_post_type_object( $post_type );
92
93 if ( ! $post_type_object || ! current_user_can( $post_type_object->cap->create_posts ) ) {
94 return new \WP_Error(
95 'rest_cannot_create',
96 __( 'Sorry, you are not allowed to create posts of this type.', 'elementor' ),
97 [ 'status' => \WP_Http::FORBIDDEN ]
98 );
99 }
100
101 return null;
102 }
103
104 private function create_post( string $post_type, string $title ) {
105 $post_id = wp_insert_post(
106 [
107 'post_title' => $title ? $title : __( 'Elementor Draft', 'elementor' ),
108 'post_status' => 'draft',
109 'post_type' => $post_type,
110 ],
111 true
112 );
113
114 if ( is_wp_error( $post_id ) ) {
115 return $post_id;
116 }
117
118 $document = Plugin::$instance->documents->get( $post_id );
119
120 if ( ! $document ) {
121 return new \WP_Error(
122 'document_not_found',
123 __( 'Document could not be loaded.', 'elementor' ),
124 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
125 );
126 }
127
128 $document->set_is_built_with_elementor( true );
129
130 return [
131 'id' => (int) $post_id,
132 'edit_url' => $document->get_edit_url(),
133 'status' => get_post_status( $post_id ),
134 'type' => $post_type,
135 ];
136 }
137 }
138