PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.0-dev2
Elementor Website Builder – more than just a page builder v3.35.0-dev2
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 4.0.7 All 451 releases
elementor / modules / site-navigation / data / endpoints / add-new-post.php

add-new-post.php in Elementor Website Builder – more than just a page builder 3.35.0-dev2, at modules/site-navigation/data/endpoints/add-new-post.php

91 lines 2.4 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\SiteNavigation\Data\Endpoints;
4
5 use Elementor\Data\V2\Base\Endpoint;
6 use Elementor\Plugin;
7 use Elementor\User;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 class Add_New_Post extends Endpoint {
14
15 protected function register() {
16 $args = [
17 'post_type' => [
18 'description' => 'Post type to create',
19 'type' => 'string',
20 'required' => false,
21 'default' => 'post',
22 'sanitize_callback' => function ( $value ) {
23 return sanitize_text_field( $value );
24 },
25 'validate_callback' => 'rest_validate_request_arg',
26 ],
27 ];
28
29 $this->register_items_route( \WP_REST_Server::CREATABLE, $args );
30 }
31
32 public function get_name() {
33 return 'add-new-post';
34 }
35
36 public function get_format() {
37 return 'site-navigation/add-new-post';
38 }
39
40 public function create_items( $request ) {
41 $post_type = $request->get_param( 'post_type' );
42
43 if ( ! $this->validate_post_type( $post_type ) ) {
44 $sanitized_post_type = esc_html( str_replace( '%', '%%', $post_type ) );
45 return new \WP_Error( 400, sprintf( 'Post type %s does not exist.', $sanitized_post_type ), [ 'status' => 400 ] );
46 }
47
48 if ( ! User::is_current_user_can_edit_post_type( $post_type ) ) {
49 $sanitized_post_type = esc_html( str_replace( '%', '%%', $post_type ) );
50 return new \WP_Error( 401, sprintf( 'User dont have capability to create page of type - %s.', $sanitized_post_type ), [ 'status' => 401 ] );
51 }
52
53 // Temporary solution for the fact that documents creation not using the actual registered post types.
54 $post_type = $this->map_post_type( $post_type );
55
56 $document = Plugin::$instance->documents->create( $post_type );
57
58 if ( is_wp_error( $document ) ) {
59 $sanitized_post_type = esc_html( str_replace( '%', '%%', $post_type ) );
60 return new \WP_Error( 500, sprintf( 'Error while creating %s.', $sanitized_post_type ) );
61 }
62
63 return [
64 'id' => $document->get_main_id(),
65 'edit_url' => $document->get_edit_url(),
66 ];
67 }
68
69 private function validate_post_type( $post_type ): bool {
70 $post_types = get_post_types();
71
72 return in_array( $post_type, $post_types );
73 }
74
75 /**
76 * Map post type to Elementor document type.
77 *
78 * @param $post_type
79 *
80 * @return string
81 */
82 private function map_post_type( $post_type ): string {
83 $post_type_map = [
84 'page' => 'wp-page',
85 'post' => 'wp-post',
86 ];
87
88 return $post_type_map[ $post_type ] ?? $post_type;
89 }
90 }
91