| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Components\Documents; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Document; |
| 5 |
use Elementor\Core\Utils\Api\Parse_Result; |
| 6 |
use Elementor\Modules\Components\OverridableProps\Component_Overridable_Props_Parser; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly |
| 10 |
} |
| 11 |
|
| 12 |
class Component extends Document { |
| 13 |
const TYPE = 'elementor_component'; |
| 14 |
const COMPONENT_UID_META_KEY = '_elementor_component_uid'; |
| 15 |
const OVERRIDABLE_PROPS_META_KEY = '_elementor_component_overridable_props'; |
| 16 |
const ARCHIVED_META_KEY = '_elementor_component_is_archived'; |
| 17 |
const ARCHIVED_AT_META_KEY = '_elementor_component_archived_at'; |
| 18 |
const COMPONENT_CUSTOM_META_KEYS = [ |
| 19 |
self::COMPONENT_UID_META_KEY, |
| 20 |
self::OVERRIDABLE_PROPS_META_KEY, |
| 21 |
self::ARCHIVED_META_KEY, |
| 22 |
self::ARCHIVED_AT_META_KEY, |
| 23 |
]; |
| 24 |
|
| 25 |
public static function get_properties() { |
| 26 |
$properties = parent::get_properties(); |
| 27 |
|
| 28 |
$properties['cpt'] = [ self::TYPE ]; |
| 29 |
|
| 30 |
return $properties; |
| 31 |
} |
| 32 |
|
| 33 |
public static function get_type() { |
| 34 |
return self::TYPE; |
| 35 |
} |
| 36 |
|
| 37 |
public static function get_title() { |
| 38 |
return esc_html__( 'Component', 'elementor' ); |
| 39 |
} |
| 40 |
|
| 41 |
public static function get_plural_title() { |
| 42 |
return esc_html__( 'Components', 'elementor' ); |
| 43 |
} |
| 44 |
|
| 45 |
public static function get_labels(): array { |
| 46 |
$plural_label = static::get_plural_title(); |
| 47 |
$singular_label = static::get_title(); |
| 48 |
|
| 49 |
$labels = [ |
| 50 |
'name' => $plural_label, |
| 51 |
'singular_name' => $singular_label, |
| 52 |
]; |
| 53 |
|
| 54 |
return $labels; |
| 55 |
} |
| 56 |
|
| 57 |
public static function get_supported_features(): array { |
| 58 |
return [ |
| 59 |
'title', |
| 60 |
'author', |
| 61 |
'thumbnail', |
| 62 |
'custom-fields', |
| 63 |
'revisions', |
| 64 |
'elementor', |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
public function get_component_uid() { |
| 69 |
return $this->get_meta( self::COMPONENT_UID_META_KEY ); |
| 70 |
} |
| 71 |
|
| 72 |
public function get_overridable_props(): Component_Overridable_Props { |
| 73 |
$meta = $this->get_json_meta( self::OVERRIDABLE_PROPS_META_KEY ); |
| 74 |
|
| 75 |
return Component_Overridable_Props::make( $meta ?? [] ); |
| 76 |
} |
| 77 |
|
| 78 |
public function archive() { |
| 79 |
try { |
| 80 |
$this->update_json_meta( self::ARCHIVED_META_KEY, [ |
| 81 |
'is_archived' => true, |
| 82 |
'archived_at' => time(), |
| 83 |
] ); |
| 84 |
} catch ( \Exception $e ) { |
| 85 |
throw new \Exception( 'Failed to archive component: ' . esc_html( $e->getMessage() ) ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public function get_is_archived(): bool { |
| 90 |
$archived_meta = $this->get_json_meta( self::ARCHIVED_META_KEY ); |
| 91 |
|
| 92 |
return $archived_meta['is_archived'] ?? false; |
| 93 |
} |
| 94 |
|
| 95 |
public function update_overridable_props( $data ): Parse_Result { |
| 96 |
$parser = Component_Overridable_Props_Parser::make(); |
| 97 |
|
| 98 |
$result = $parser->parse( $data ); |
| 99 |
|
| 100 |
if ( ! $result->is_valid() ) { |
| 101 |
return $result; |
| 102 |
} |
| 103 |
|
| 104 |
$sanitized_data = $result->unwrap(); |
| 105 |
|
| 106 |
$this->update_json_meta( self::OVERRIDABLE_PROPS_META_KEY, $sanitized_data ); |
| 107 |
|
| 108 |
return $result; |
| 109 |
} |
| 110 |
|
| 111 |
public function update_title( string $title ): bool { |
| 112 |
$sanitized_title = sanitize_text_field( $title ); |
| 113 |
|
| 114 |
if ( empty( $sanitized_title ) ) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
return $this->update_post_field( 'post_title', $sanitized_title ); |
| 119 |
} |
| 120 |
|
| 121 |
public function update_status( string $status ): bool { |
| 122 |
if ( ! in_array( $status, [ Document::STATUS_PUBLISH, Document::STATUS_DRAFT, Document::STATUS_AUTOSAVE ], true ) ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
return $this->update_post_field( 'post_status', $status ); |
| 127 |
} |
| 128 |
|
| 129 |
private function update_post_field( string $field, $value ): bool { |
| 130 |
$result = wp_update_post( [ |
| 131 |
'ID' => $this->post->ID, |
| 132 |
$field => $value, |
| 133 |
] ); |
| 134 |
|
| 135 |
$success = ! is_wp_error( $result ) && $result > 0; |
| 136 |
|
| 137 |
if ( $success ) { |
| 138 |
$this->refresh_post(); |
| 139 |
} |
| 140 |
|
| 141 |
return $success; |
| 142 |
} |
| 143 |
} |
| 144 |
|