| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Core\Kits; |
| 3 | 3 | |
| 4 | +use Elementor\Core\Base\Document; | |
| 4 | 5 | use Elementor\Core\Kits\Controls\Repeater; |
| 5 | 6 | use Elementor\Core\Kits\Documents\Tabs\Global_Colors; |
| 6 | 7 | use Elementor\Core\Kits\Documents\Tabs\Global_Typography; |
| 7 | 8 | use Elementor\Plugin; |
| @@ -22,30 +23,51 @@ | ||
| 22 | 23 | |
| 23 | 24 | const E_HASH_COMMAND_OPEN_SITE_SETTINGS = 'e:run:panel/global/open'; |
| 24 | 25 | |
| 25 | 26 | public function get_active_id() { |
| 26 | - $id = get_option( self::OPTION_ACTIVE ); | |
| 27 | + return get_option( self::OPTION_ACTIVE ); | |
| 28 | + } | |
| 27 | 29 | |
| 28 | - $kit_document = Plugin::$instance->documents->get( $id ); | |
| 30 | + public function get_active_kit() { | |
| 31 | + $kit = Plugin::$instance->documents->get( $this->get_active_id() ); | |
| 29 | 32 | |
| 30 | - if ( ! $kit_document || ! $kit_document instanceof Kit || 'trash' === $kit_document->get_main_post()->post_status ) { | |
| 31 | - $id = $this->create_default(); | |
| 32 | - update_option( self::OPTION_ACTIVE, $id ); | |
| 33 | + if ( ! $this->is_valid_kit( $kit ) ) { | |
| 34 | + return $this->get_empty_kit_instance(); | |
| 33 | 35 | } |
| 34 | 36 | |
| 35 | - return $id; | |
| 37 | + return $kit; | |
| 36 | 38 | } |
| 37 | 39 | |
| 38 | - public function get_active_kit() { | |
| 39 | - $id = $this->get_active_id(); | |
| 40 | + public function get_active_kit_for_frontend() { | |
| 41 | + $kit = Plugin::$instance->documents->get_doc_for_frontend( $this->get_active_id() ); | |
| 40 | 42 | |
| 41 | - return Plugin::$instance->documents->get( $id ); | |
| 43 | + if ( ! $this->is_valid_kit( $kit ) ) { | |
| 44 | + return $this->get_empty_kit_instance(); | |
| 45 | + } | |
| 46 | + | |
| 47 | + return $kit; | |
| 42 | 48 | } |
| 43 | 49 | |
| 44 | - public function get_active_kit_for_frontend() { | |
| 45 | - $id = $this->get_active_id(); | |
| 50 | + /** | |
| 51 | + * @param $kit | |
| 52 | + * | |
| 53 | + * @return bool | |
| 54 | + */ | |
| 55 | + private function is_valid_kit( $kit ) { | |
| 56 | + return $kit && $kit instanceof Kit && 'trash' !== $kit->get_main_post()->post_status; | |
| 57 | + } | |
| 46 | 58 | |
| 47 | - return Plugin::$instance->documents->get_doc_for_frontend( $id ); | |
| 59 | + /** | |
| 60 | + * Returns an empty kit for situation when there is no kit in the site. | |
| 61 | + * | |
| 62 | + * @return Kit | |
| 63 | + * @throws \Exception | |
| 64 | + */ | |
| 65 | + private function get_empty_kit_instance() { | |
| 66 | + return new Kit( [ | |
| 67 | + 'settings' => [], | |
| 68 | + 'post_id' => 0, | |
| 69 | + ] ); | |
| 48 | 70 | } |
| 49 | 71 | |
| 50 | 72 | /** |
| 51 | 73 | * Checks if specific post is a kit. |
| @@ -96,10 +118,40 @@ | ||
| 96 | 118 | |
| 97 | 119 | return $kit->get_id(); |
| 98 | 120 | } |
| 99 | 121 | |
| 100 | - private function create_default() { | |
| 101 | - return $this->create( [ 'post_title' => __( 'Default Kit', 'elementor' ) ] ); | |
| 122 | + public function create_default() { | |
| 123 | + return $this->create( [ | |
| 124 | + 'post_title' => __( 'Default Kit', 'elementor' ), | |
| 125 | + ] ); | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Create a default kit if needed. | |
| 130 | + * | |
| 131 | + * This action runs on activation hook, all the Plugin components do not exists and | |
| 132 | + * the Document manager and Kits manager instances cannot be used. | |
| 133 | + * | |
| 134 | + * @return int|void|\WP_Error | |
| 135 | + */ | |
| 136 | + public static function create_default_kit() { | |
| 137 | + if ( get_option( self::OPTION_ACTIVE ) ) { | |
| 138 | + return; | |
| 139 | + } | |
| 140 | + | |
| 141 | + $id = wp_insert_post( [ | |
| 142 | + 'post_title' => __( 'Default Kit', 'elementor' ), | |
| 143 | + 'post_type' => Source_Local::CPT, | |
| 144 | + 'post_status' => 'publish', | |
| 145 | + 'meta_input' => [ | |
| 146 | + '_elementor_edit_mode' => 'builder', | |
| 147 | + Document::TYPE_META_KEY => 'kit', | |
| 148 | + ], | |
| 149 | + ] ); | |
| 150 | + | |
| 151 | + update_option( self::OPTION_ACTIVE, $id ); | |
| 152 | + | |
| 153 | + return $id; | |
| 102 | 154 | } |
| 103 | 155 | |
| 104 | 156 | /** |
| 105 | 157 | * @param Documents_Manager $documents_manager |