PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.4
Elementor Website Builder – more than just a page builder v3.0.4
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 / core / settings / page / manager.php

manager.php in Elementor Website Builder – more than just a page builder 3.0.4, at core/settings/page/manager.php

344 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Settings\Page;
3
4 use Elementor\Core\Files\CSS\Base;
5 use Elementor\Core\Files\CSS\Post;
6 use Elementor\Core\Files\CSS\Post_Preview;
7 use Elementor\Core\Settings\Base\CSS_Manager;
8 use Elementor\Core\Utils\Exceptions;
9 use Elementor\Core\Settings\Base\Model as BaseModel;
10 use Elementor\DB;
11 use Elementor\Plugin;
12 use Elementor\Utils;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 /**
19 * Elementor page settings manager.
20 *
21 * Elementor page settings manager handler class is responsible for registering
22 * and managing Elementor page settings managers.
23 *
24 * @since 1.6.0
25 */
26 class Manager extends CSS_Manager {
27
28 /**
29 * Meta key for the page settings.
30 */
31 const META_KEY = '_elementor_page_settings';
32
33 /**
34 * Get manager name.
35 *
36 * Retrieve page settings manager name.
37 *
38 * @since 1.6.0
39 * @access public
40 *
41 * @return string Manager name.
42 */
43 public function get_name() {
44 return 'page';
45 }
46
47 /**
48 * Get model for config.
49 *
50 * Retrieve the model for settings configuration.
51 *
52 * @since 1.6.0
53 * @access public
54 *
55 * @return BaseModel The model object.
56 */
57 public function get_model_for_config() {
58 if ( ! is_singular() && ! Plugin::$instance->editor->is_edit_mode() ) {
59 return null;
60 }
61
62 if ( Plugin::$instance->editor->is_edit_mode() ) {
63 $post_id = Plugin::$instance->editor->get_post_id();
64 $document = Plugin::$instance->documents->get_doc_or_auto_save( $post_id );
65 } else {
66 $post_id = get_the_ID();
67 $document = Plugin::$instance->documents->get_doc_for_frontend( $post_id );
68 }
69
70 if ( ! $document ) {
71 return null;
72 }
73
74 $model = $this->get_model( $document->get_post()->ID );
75
76 if ( $document->is_autosave() ) {
77 $model->set_settings( 'post_status', $document->get_main_post()->post_status );
78 }
79
80 return $model;
81 }
82
83 /**
84 * Ajax before saving settings.
85 *
86 * Validate the data before saving it and updating the data in the database.
87 *
88 * @since 1.6.0
89 * @access public
90 *
91 * @param array $data Post data.
92 * @param int $id Post ID.
93 *
94 * @throws \Exception If invalid post returned using the `$id`.
95 * @throws \Exception If current user don't have permissions to edit the post.
96 */
97 public function ajax_before_save_settings( array $data, $id ) {
98 $post = get_post( $id );
99
100 if ( empty( $post ) ) {
101 throw new \Exception( 'Invalid post.', Exceptions::NOT_FOUND );
102 }
103
104 if ( ! current_user_can( 'edit_post', $id ) ) {
105 throw new \Exception( 'Access denied.', Exceptions::FORBIDDEN );
106 }
107
108 // Avoid save empty post title.
109 if ( ! empty( $data['post_title'] ) ) {
110 $post->post_title = $data['post_title'];
111 }
112
113 if ( isset( $data['post_excerpt'] ) && post_type_supports( $post->post_type, 'excerpt' ) ) {
114 $post->post_excerpt = $data['post_excerpt'];
115 }
116
117 if ( isset( $data['post_status'] ) ) {
118 $this->save_post_status( $id, $data['post_status'] );
119 unset( $post->post_status );
120 }
121
122 wp_update_post( $post );
123
124 // Check updated status
125 if ( DB::STATUS_PUBLISH === get_post_status( $id ) ) {
126 $autosave = wp_get_post_autosave( $post->ID );
127 if ( $autosave ) {
128 wp_delete_post_revision( $autosave->ID );
129 }
130 }
131
132 if ( isset( $data['post_featured_image'] ) && post_type_supports( $post->post_type, 'thumbnail' ) ) {
133 if ( empty( $data['post_featured_image']['id'] ) ) {
134 delete_post_thumbnail( $post->ID );
135 } else {
136 set_post_thumbnail( $post->ID, $data['post_featured_image']['id'] );
137 }
138 }
139
140 if ( Utils::is_cpt_custom_templates_supported() ) {
141 $template = get_metadata( 'post', $post->ID, '_wp_page_template', true );
142
143 if ( isset( $data['template'] ) ) {
144 $template = $data['template'];
145 }
146
147 if ( empty( $template ) ) {
148 $template = 'default';
149 }
150
151 // Use `update_metadata` in order to save also for revisions.
152 update_metadata( 'post', $post->ID, '_wp_page_template', $template );
153 }
154 }
155
156 /**
157 * @inheritDoc
158 *
159 * Override parent because the page setting moved to document.settings.
160 */
161 protected function print_editor_template_content( $name ) {
162 ?>
163 <#
164 const tabs = elementor.config.document.settings.tabs;
165
166 if ( Object.values( tabs ).length > 1 ) { #>
167 <div class="elementor-panel-navigation">
168 <# _.each( tabs, function( tabTitle, tabSlug ) { #>
169 <div class="elementor-component-tab elementor-panel-navigation-tab elementor-tab-control-{{ tabSlug }}" data-tab="{{ tabSlug }}">
170 <a href="#">{{{ tabTitle }}}</a>
171 </div>
172 <# } ); #>
173 </div>
174 <# } #>
175 <div id="elementor-panel-<?php echo $name; ?>-settings-controls"></div>
176 <?php
177 }
178
179 /**
180 * Save settings to DB.
181 *
182 * Save page settings to the database, as post meta data.
183 *
184 * @since 1.6.0
185 * @access protected
186 *
187 * @param array $settings Settings.
188 * @param int $id Post ID.
189 */
190 protected function save_settings_to_db( array $settings, $id ) {
191 // Use update/delete_metadata in order to handle also revisions.
192 if ( ! empty( $settings ) ) {
193 // Use `wp_slash` in order to avoid the unslashing during the `update_post_meta`.
194 update_metadata( 'post', $id, self::META_KEY, wp_slash( $settings ) );
195 } else {
196 delete_metadata( 'post', $id, self::META_KEY );
197 }
198 }
199
200 /**
201 * Get CSS file for update.
202 *
203 * Retrieve the CSS file before updating it.
204 *
205 * This method overrides the parent method to disallow updating CSS files for pages.
206 *
207 * @since 1.6.0
208 * @access protected
209 *
210 * @param int $id Post ID.
211 *
212 * @return false Disallow The updating CSS files for pages.
213 */
214 protected function get_css_file_for_update( $id ) {
215 return false;
216 }
217
218 /**
219 * Get saved settings.
220 *
221 * Retrieve the saved settings from the post meta.
222 *
223 * @since 1.6.0
224 * @access protected
225 *
226 * @param int $id Post ID.
227 *
228 * @return array Saved settings.
229 */
230 protected function get_saved_settings( $id ) {
231 $settings = get_post_meta( $id, self::META_KEY, true );
232
233 if ( ! $settings ) {
234 $settings = [];
235 }
236
237 if ( Utils::is_cpt_custom_templates_supported() ) {
238 $saved_template = get_post_meta( $id, '_wp_page_template', true );
239
240 if ( $saved_template ) {
241 $settings['template'] = $saved_template;
242 }
243 }
244
245 return $settings;
246 }
247
248 /**
249 * Get CSS file name.
250 *
251 * Retrieve CSS file name for the page settings manager.
252 *
253 * @since 1.6.0
254 * @access protected
255 *
256 * @return string CSS file name.
257 */
258 protected function get_css_file_name() {
259 return 'post';
260 }
261
262 /**
263 * Get model for CSS file.
264 *
265 * Retrieve the model for the CSS file.
266 *
267 * @since 1.6.0
268 * @access protected
269 *
270 * @param Base $css_file The requested CSS file.
271 *
272 * @return BaseModel The model object.
273 */
274 protected function get_model_for_css_file( Base $css_file ) {
275 if ( ! $css_file instanceof Post ) {
276 return null;
277 }
278
279 $post_id = $css_file->get_post_id();
280
281 if ( $css_file instanceof Post_Preview ) {
282 $autosave = Utils::get_post_autosave( $post_id );
283 if ( $autosave ) {
284 $post_id = $autosave->ID;
285 }
286 }
287
288 return $this->get_model( $post_id );
289 }
290
291 /**
292 * Get special settings names.
293 *
294 * Retrieve the names of the special settings that are not saved as regular
295 * settings. Those settings have a separate saving process.
296 *
297 * @since 1.6.0
298 * @access protected
299 *
300 * @return array Special settings names.
301 */
302 protected function get_special_settings_names() {
303 return [
304 'id',
305 'post_title',
306 'post_status',
307 'template',
308 'post_excerpt',
309 'post_featured_image',
310 ];
311 }
312
313 /**
314 * @since 2.0.0
315 * @access public
316 *
317 * @param $post_id
318 * @param $status
319 */
320 public function save_post_status( $post_id, $status ) {
321 $parent_id = wp_is_post_revision( $post_id );
322
323 if ( $parent_id ) {
324 // Don't update revisions post-status
325 return;
326 }
327
328 $parent_id = $post_id;
329
330 $post = get_post( $parent_id );
331
332 $allowed_post_statuses = get_post_statuses();
333
334 if ( isset( $allowed_post_statuses[ $status ] ) ) {
335 $post_type_object = get_post_type_object( $post->post_type );
336 if ( 'publish' !== $status || current_user_can( $post_type_object->cap->publish_posts ) ) {
337 $post->post_status = $status;
338 }
339 }
340
341 wp_update_post( $post );
342 }
343 }
344