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

355 lines 8.4 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\Base\Document;
5 use Elementor\Core\Files\CSS\Base;
6 use Elementor\Core\Files\CSS\Post;
7 use Elementor\Core\Files\CSS\Post_Preview;
8 use Elementor\Core\Settings\Base\CSS_Manager;
9 use Elementor\Core\Utils\Exceptions;
10 use Elementor\Core\Settings\Base\Model as BaseModel;
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 ( ! Utils::is_wp_cli() && ! 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['menu_order'] ) && is_post_type_hierarchical( $post->post_type ) ) {
118 $post->menu_order = $data['menu_order'];
119 }
120
121 if ( isset( $data['post_status'] ) ) {
122 $this->save_post_status( $id, $data['post_status'] );
123 unset( $post->post_status );
124 }
125
126 if ( isset( $data['comment_status'] ) && post_type_supports( $post->post_type, 'comments' ) ) {
127 $post->comment_status = $data['comment_status'];
128 }
129
130 wp_update_post( $post );
131
132 // Check updated status
133 if ( Document::STATUS_PUBLISH === get_post_status( $id ) ) {
134 $autosave = wp_get_post_autosave( $post->ID );
135 if ( $autosave ) {
136 wp_delete_post_revision( $autosave->ID );
137 }
138 }
139
140 if ( isset( $data['post_featured_image'] ) && post_type_supports( $post->post_type, 'thumbnail' ) ) {
141 if ( empty( $data['post_featured_image']['id'] ) ) {
142 delete_post_thumbnail( $post->ID );
143 } else {
144 set_post_thumbnail( $post->ID, $data['post_featured_image']['id'] );
145 }
146 }
147
148 if ( Utils::is_cpt_custom_templates_supported() ) {
149 $template = get_metadata( 'post', $post->ID, '_wp_page_template', true );
150
151 if ( isset( $data['template'] ) ) {
152 $template = $data['template'];
153 }
154
155 if ( empty( $template ) ) {
156 $template = 'default';
157 }
158
159 // Use `update_metadata` in order to save also for revisions.
160 update_metadata( 'post', $post->ID, '_wp_page_template', $template );
161 }
162 }
163
164 /**
165 * @inheritDoc
166 *
167 * Override parent because the page setting moved to document.settings.
168 */
169 protected function print_editor_template_content( $name ) {
170 ?>
171 <#
172 const tabs = elementor.config.document.settings.tabs;
173
174 if ( Object.values( tabs ).length > 1 ) { #>
175 <div class="elementor-panel-navigation">
176 <# _.each( tabs, function( tabTitle, tabSlug ) {
177 $e.bc.ensureTab( 'panel/page-settings', tabSlug ); #>
178 <button class="elementor-component-tab elementor-panel-navigation-tab elementor-tab-control-{{ tabSlug }}" data-tab="{{ tabSlug }}">
179 <span>{{{ tabTitle }}}</span>
180 </button>
181 <# } ); #>
182 </div>
183 <# } #>
184 <div id="elementor-panel-<?php echo esc_attr( $name ); ?>-settings-controls"></div>
185 <?php
186 }
187
188 /**
189 * Save settings to DB.
190 *
191 * Save page settings to the database, as post meta data.
192 *
193 * @since 1.6.0
194 * @access protected
195 *
196 * @param array $settings Settings.
197 * @param int $id Post ID.
198 */
199 protected function save_settings_to_db( array $settings, $id ) {
200 // Use update/delete_metadata in order to handle also revisions.
201 if ( ! empty( $settings ) ) {
202 // Use `wp_slash` in order to avoid the unslashing during the `update_post_meta`.
203 update_metadata( 'post', $id, self::META_KEY, wp_slash( $settings ) );
204 } else {
205 delete_metadata( 'post', $id, self::META_KEY );
206 }
207 }
208
209 /**
210 * Get CSS file for update.
211 *
212 * Retrieve the CSS file before updating it.
213 *
214 * This method overrides the parent method to disallow updating CSS files for pages.
215 *
216 * @since 1.6.0
217 * @access protected
218 *
219 * @param int $id Post ID.
220 *
221 * @return false Disallow The updating CSS files for pages.
222 */
223 protected function get_css_file_for_update( $id ) {
224 return false;
225 }
226
227 /**
228 * Get saved settings.
229 *
230 * Retrieve the saved settings from the post meta.
231 *
232 * @since 1.6.0
233 * @access protected
234 *
235 * @param int $id Post ID.
236 *
237 * @return array Saved settings.
238 */
239 protected function get_saved_settings( $id ) {
240 $settings = get_post_meta( $id, self::META_KEY, true );
241
242 if ( ! $settings ) {
243 $settings = [];
244 }
245
246 if ( Utils::is_cpt_custom_templates_supported() ) {
247 $saved_template = get_post_meta( $id, '_wp_page_template', true );
248
249 if ( $saved_template ) {
250 $settings['template'] = $saved_template;
251 }
252 }
253
254 return $settings;
255 }
256
257 /**
258 * Get CSS file name.
259 *
260 * Retrieve CSS file name for the page settings manager.
261 *
262 * @since 1.6.0
263 * @access protected
264 *
265 * @return string CSS file name.
266 */
267 protected function get_css_file_name() {
268 return 'post';
269 }
270
271 /**
272 * Get model for CSS file.
273 *
274 * Retrieve the model for the CSS file.
275 *
276 * @since 1.6.0
277 * @access protected
278 *
279 * @param Base $css_file The requested CSS file.
280 *
281 * @return BaseModel The model object.
282 */
283 protected function get_model_for_css_file( Base $css_file ) {
284 if ( ! $css_file instanceof Post ) {
285 return null;
286 }
287
288 $post_id = $css_file->get_post_id();
289
290 if ( $css_file instanceof Post_Preview ) {
291 $autosave = Utils::get_post_autosave( $post_id );
292 if ( $autosave ) {
293 $post_id = $autosave->ID;
294 }
295 }
296
297 return $this->get_model( $post_id );
298 }
299
300 /**
301 * Get special settings names.
302 *
303 * Retrieve the names of the special settings that are not saved as regular
304 * settings. Those settings have a separate saving process.
305 *
306 * @since 1.6.0
307 * @access protected
308 *
309 * @return array Special settings names.
310 */
311 protected function get_special_settings_names() {
312 return [
313 'id',
314 'post_title',
315 'post_status',
316 'template',
317 'post_excerpt',
318 'post_featured_image',
319 'menu_order',
320 'comment_status',
321 ];
322 }
323
324 /**
325 * @since 2.0.0
326 * @access public
327 *
328 * @param $post_id
329 * @param $status
330 */
331 public function save_post_status( $post_id, $status ) {
332 $parent_id = wp_is_post_revision( $post_id );
333
334 if ( $parent_id ) {
335 // Don't update revisions post-status
336 return;
337 }
338
339 $parent_id = $post_id;
340
341 $post = get_post( $parent_id );
342
343 $allowed_post_statuses = get_post_statuses();
344
345 if ( isset( $allowed_post_statuses[ $status ] ) ) {
346 $post_type_object = get_post_type_object( $post->post_type );
347 if ( 'publish' !== $status || current_user_can( $post_type_object->cap->publish_posts ) ) {
348 $post->post_status = $status;
349 }
350 }
351
352 wp_update_post( $post );
353 }
354 }
355