PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
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 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / mcp / abilities / create-page-ability.php

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

148 lines 3.9 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\Modules\Mcp\Abilities\Utils\Document_Mutation_Links;
6 use Elementor\Plugin;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class Create_Page_Ability extends Abstract_Ability {
13
14 protected function get_ability_id(): string {
15 return 'elementor/create-page';
16 }
17
18 public function is_exposed_via_proxy(): bool {
19 return false;
20 }
21
22 protected function get_definition(): Ability_Definition {
23 return new Ability_Definition(
24 __( 'Create Elementor Page', 'elementor' ),
25 __( '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' ),
26 'elementor',
27 [
28 'type' => 'object',
29 'properties' => [
30 'id' => [ 'type' => 'integer' ],
31 'edit_url' => [ 'type' => 'string' ],
32 'status' => [ 'type' => 'string' ],
33 'type' => [ 'type' => 'string' ],
34 'preview_url' => Document_Mutation_Links::preview_schema_property(),
35 'llm_instructions' => Document_Mutation_Links::llm_instructions_schema_property(),
36 ],
37 ],
38 [
39 'annotations' => [
40 'readonly' => false,
41 'idempotent' => false,
42 'destructive' => false,
43 ],
44 ],
45 function () {
46 return current_user_can( 'edit_posts' );
47 },
48 [
49 'type' => 'object',
50 'properties' => [
51 'title' => [
52 'type' => 'string',
53 'description' => 'Optional title for the new post.',
54 ],
55 'post_type' => [
56 'type' => 'string',
57 'description' => 'Post type slug; must support Elementor. Defaults to page.',
58 'default' => 'page',
59 ],
60 ],
61 ]
62 );
63 }
64
65 public function execute( $input = [] ) {
66 $input = is_array( $input ) ? $input : [];
67
68 $post_type = ! empty( $input['post_type'] ) ? sanitize_key( $input['post_type'] ) : 'page';
69
70 $type_error = $this->validate_post_type( $post_type );
71 if ( $type_error ) {
72 return $type_error;
73 }
74
75 $permission_error = $this->check_create_permission( $post_type );
76 if ( $permission_error ) {
77 return $permission_error;
78 }
79
80 $title = isset( $input['title'] ) && is_string( $input['title'] ) ? $input['title'] : '';
81
82 return $this->create_post( $post_type, $title );
83 }
84
85 private function validate_post_type( string $post_type ): ?\WP_Error {
86 if ( ! post_type_exists( $post_type ) || ! post_type_supports( $post_type, 'elementor' ) ) {
87 return new \WP_Error(
88 'invalid_post_type',
89 __( 'This post type does not support Elementor.', 'elementor' ),
90 [ 'status' => \WP_Http::BAD_REQUEST ]
91 );
92 }
93
94 return null;
95 }
96
97 private function check_create_permission( string $post_type ): ?\WP_Error {
98 $post_type_object = get_post_type_object( $post_type );
99
100 if ( ! $post_type_object || ! current_user_can( $post_type_object->cap->create_posts ) ) {
101 return new \WP_Error(
102 'rest_cannot_create',
103 __( 'Sorry, you are not allowed to create posts of this type.', 'elementor' ),
104 [ 'status' => \WP_Http::FORBIDDEN ]
105 );
106 }
107
108 return null;
109 }
110
111 private function create_post( string $post_type, string $title ) {
112 $post_id = wp_insert_post(
113 [
114 'post_title' => $title ? $title : __( 'Elementor Draft', 'elementor' ),
115 'post_status' => 'draft',
116 'post_type' => $post_type,
117 ],
118 true
119 );
120
121 if ( is_wp_error( $post_id ) ) {
122 return $post_id;
123 }
124
125 $document = Plugin::$instance->documents->get( $post_id );
126
127 if ( ! $document ) {
128 return new \WP_Error(
129 'document_not_found',
130 __( 'Document could not be loaded.', 'elementor' ),
131 [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ]
132 );
133 }
134
135 $document->set_is_built_with_elementor( true );
136
137 return [
138 'id' => (int) $post_id,
139 'edit_url' => $document->get_edit_url(),
140 'status' => get_post_status( $post_id ),
141 'type' => $post_type,
142 ] + Document_Mutation_Links::for_document(
143 $document,
144 __( 'Page created.', 'elementor' )
145 );
146 }
147 }
148