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 / wp-rest / classes / elementor-post-meta.php

elementor-post-meta.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/wp-rest/classes/elementor-post-meta.php

174 lines 4.8 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\WpRest\Classes;
4
5 use Elementor\Plugin;
6 use Elementor\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Elementor_Post_Meta {
13
14 public function register(): void {
15 $post_types = get_post_types_by_support( 'elementor' );
16
17 foreach ( $post_types as $post_type ) {
18 $this->register_edit_mode_meta( $post_type );
19 $this->register_template_type_meta( $post_type );
20 $this->register_elementor_data_meta( $post_type );
21 $this->register_page_settings_meta( $post_type );
22
23 if ( Utils::has_pro() ) {
24 $this->register_conditions_meta( $post_type );
25 }
26 }
27 }
28
29 private function register_edit_mode_meta( string $post_type ): void {
30 register_meta( 'post', '_elementor_edit_mode', [
31 'single' => true,
32 'object_subtype' => $post_type,
33 'show_in_rest' => [
34 'schema' => [
35 'title' => 'Elementor edit mode',
36 'description' => 'Elementor edit mode, `builder` is required for Elementor editing',
37 'type' => 'string',
38 'enum' => [ '', 'builder' ],
39 'default' => '',
40 'context' => [ 'edit' ],
41 ],
42 ],
43 'auth_callback' => [ $this, 'check_edit_permission' ],
44 ]);
45 }
46
47 private function register_template_type_meta( string $post_type ): void {
48 $document_types = Plugin::$instance->documents->get_document_types();
49
50 register_meta( 'post', '_elementor_template_type', [
51 'single' => true,
52 'object_subtype' => $post_type,
53 'show_in_rest' => [
54 'schema' => [
55 'title' => 'Elementor template type',
56 'description' => 'Elementor document type',
57 'type' => 'string',
58 'enum' => array_merge( array_keys( $document_types ), [ '' ] ),
59 'default' => '',
60 'context' => [ 'edit' ],
61 ],
62 ],
63 'auth_callback' => [ $this, 'check_edit_permission' ],
64 ]);
65 }
66
67 private function register_elementor_data_meta( string $post_type ): void {
68 register_meta( 'post', '_elementor_data', [
69 'single' => true,
70 'object_subtype' => $post_type,
71 'show_in_rest' => [
72 'schema' => [
73 'title' => 'Elementor data',
74 'description' => 'Elementor JSON as a string',
75 'type' => 'string',
76 'default' => '',
77 'context' => [ 'edit' ],
78 ],
79 ],
80 'auth_callback' => [ $this, 'check_edit_permission' ],
81 'sanitize_callback' => [ $this, 'sanitize_elementor_data' ],
82 ]);
83 }
84
85 public function sanitize_elementor_data( $value ) {
86 if ( current_user_can( 'unfiltered_html' ) ) {
87 return $value;
88 }
89
90 if ( ! is_string( $value ) ) {
91 return Utils::kses_post_deep( $value );
92 }
93
94 $elementor_data = json_decode( $value, true );
95
96 if ( JSON_ERROR_NONE !== json_last_error() || empty( $elementor_data ) ) {
97 // Not valid Elementor JSON (or empty): treat the whole string as HTML to be safe.
98 return wp_kses_post( $value );
99 }
100
101 return wp_json_encode( Utils::kses_post_deep( $elementor_data ) );
102 }
103
104 private function register_page_settings_meta( string $post_type ): void {
105 register_meta( 'post', '_elementor_page_settings', [
106 'single' => true,
107 'object_subtype' => $post_type,
108 'type' => 'object',
109 'show_in_rest' => [
110 'schema' => [
111 'title' => 'Elementor page settings',
112 'description' => 'Elementor page level settings',
113 'type' => 'object',
114 'properties' => [
115 'hide_title' => [
116 'type' => 'string',
117 'enum' => [ 'yes', 'no' ],
118 'default' => '',
119 ],
120 ],
121 'default' => '{}',
122 'additionalProperties' => true,
123 'context' => [ 'edit' ],
124 ],
125 ],
126 'auth_callback' => [ $this, 'check_edit_permission' ],
127 'sanitize_callback' => [ $this, 'sanitize_page_settings' ],
128 ]);
129 }
130
131 public function sanitize_page_settings( $value ) {
132 if ( current_user_can( 'unfiltered_html' ) ) {
133 return $value;
134 }
135
136 return Utils::kses_post_deep( $value );
137 }
138
139 private function register_conditions_meta( string $post_type ): void {
140 register_meta( 'post', '_elementor_conditions', [
141 'object_subtype' => $post_type,
142 'type' => 'object',
143 'title' => 'Elementor conditions',
144 'description' => 'Elementor conditions',
145 'single' => true,
146 'show_in_rest' => [
147 'schema' => [
148 'description' => 'Elementor conditions',
149 'type' => 'array',
150 'additionalProperties' => true,
151 'default' => [],
152 'context' => [ 'edit' ],
153 ],
154 ],
155 'auth_callback' => [ $this, 'check_edit_permission' ],
156 ]);
157 }
158
159 /**
160 * Check if current user has permission to edit the specific post with elementor
161 *
162 * @param bool $allowed Whether the user can add the post meta. Default false.
163 * @param string $meta_key The meta key.
164 * @param int $post_id Post ID.
165 * @return bool
166 * @since 3.27.0
167 */
168 public function check_edit_permission( bool $allowed, string $meta_key, int $post_id ): bool {
169 $document = Plugin::$instance->documents->get( $post_id );
170
171 return $document && $document->is_editable_by_current_user();
172 }
173 }
174