PluginProbe
Elementor Website Builder – more than just a page builder / 3.24.0-dev2
Elementor Website Builder – more than just a page builder v3.24.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.24.0-dev2, at modules/site-navigation/data/endpoints/add-new-post.php

88 lines 2.1 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 return new \WP_Error( 400, sprintf( 'Post type %s does not exist.', $post_type ), [ 'status' => 400 ] );
45 }
46
47 if ( ! User::is_current_user_can_edit_post_type( $post_type ) ) {
48 return new \WP_Error( 401, sprintf( 'User dont have capability to create page of type - %s.', $post_type ), [ 'status' => 401 ] );
49 }
50
51 // Temporary solution for the fact that documents creation not using the actual registered post types.
52 $post_type = $this->map_post_type( $post_type );
53
54 $document = Plugin::$instance->documents->create( $post_type );
55
56 if ( is_wp_error( $document ) ) {
57 return new \WP_Error( 500, sprintf( 'Error while creating %s.', $post_type ) );
58 }
59
60 return [
61 'id' => $document->get_main_id(),
62 'edit_url' => $document->get_edit_url(),
63 ];
64 }
65
66 private function validate_post_type( $post_type ): bool {
67 $post_types = get_post_types();
68
69 return in_array( $post_type, $post_types );
70 }
71
72 /**
73 * Map post type to Elementor document type.
74 *
75 * @param $post_type
76 *
77 * @return string
78 */
79 private function map_post_type( $post_type ): string {
80 $post_type_map = [
81 'page' => 'wp-page',
82 'post' => 'wp-post',
83 ];
84
85 return $post_type_map[ $post_type ] ?? $post_type;
86 }
87 }
88