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