| 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 |
]); |
| 82 |
} |
| 83 |
|
| 84 |
private function register_page_settings_meta( string $post_type ): void { |
| 85 |
register_meta( 'post', '_elementor_page_settings', [ |
| 86 |
'single' => true, |
| 87 |
'object_subtype' => $post_type, |
| 88 |
'type' => 'object', |
| 89 |
'show_in_rest' => [ |
| 90 |
'schema' => [ |
| 91 |
'title' => 'Elementor page settings', |
| 92 |
'description' => 'Elementor page level settings', |
| 93 |
'type' => 'object', |
| 94 |
'properties' => [ |
| 95 |
'hide_title' => [ |
| 96 |
'type' => 'string', |
| 97 |
'enum' => [ 'yes', 'no' ], |
| 98 |
'default' => '', |
| 99 |
], |
| 100 |
], |
| 101 |
'default' => '{}', |
| 102 |
'additionalProperties' => true, |
| 103 |
'context' => [ 'edit' ], |
| 104 |
], |
| 105 |
], |
| 106 |
'auth_callback' => [ $this, 'check_edit_permission' ], |
| 107 |
]); |
| 108 |
} |
| 109 |
|
| 110 |
private function register_conditions_meta( string $post_type ): void { |
| 111 |
register_meta( 'post', '_elementor_conditions', [ |
| 112 |
'object_subtype' => $post_type, |
| 113 |
'type' => 'object', |
| 114 |
'title' => 'Elementor conditions', |
| 115 |
'description' => 'Elementor conditions', |
| 116 |
'single' => true, |
| 117 |
'show_in_rest' => [ |
| 118 |
'schema' => [ |
| 119 |
'description' => 'Elementor conditions', |
| 120 |
'type' => 'array', |
| 121 |
'additionalProperties' => true, |
| 122 |
'default' => [], |
| 123 |
'context' => [ 'edit' ], |
| 124 |
], |
| 125 |
], |
| 126 |
'auth_callback' => [ $this, 'check_edit_permission' ], |
| 127 |
]); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Check if current user has permission to edit the specific post with elementor |
| 132 |
* |
| 133 |
* @param bool $allowed Whether the user can add the post meta. Default false. |
| 134 |
* @param string $meta_key The meta key. |
| 135 |
* @param int $post_id Post ID. |
| 136 |
* @return bool |
| 137 |
* @since 3.27.0 |
| 138 |
*/ |
| 139 |
public function check_edit_permission( bool $allowed, string $meta_key, int $post_id ): bool { |
| 140 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 141 |
|
| 142 |
return $document && $document->is_editable_by_current_user(); |
| 143 |
} |
| 144 |
} |
| 145 |
|