PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev3
Elementor Website Builder – more than just a page builder v3.32.0-dev3
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
← All changes | modules/components/components-repository.php +16 -151 4.0.93.32.0-dev3 View file →
@@ -1,12 +1,11 @@
1 1 <?php
2 2
3 3 namespace Elementor\Modules\Components;
4 4
5 -use Elementor\Core\Utils\Collection;
6 5 use Elementor\Modules\Components\Documents\Component as Component_Document;
7 6 use Elementor\Plugin;
8 -use Elementor\Core\Base\Document;
7 +use Elementor\Modules\Components\Components_REST_API;
9 8
10 9 if ( ! defined( 'ABSPATH' ) ) {
11 10 exit; // Exit if accessed directly.
12 11 }
@@ -16,13 +15,13 @@
16 15 public static function make(): Components_Repository {
17 16 return new self();
18 17 }
19 18
20 - public function all(): Collection {
21 - // Components count is limited to 100, if we increase this number, we need to iterate the posts in batches.
19 + public function all() {
20 + // Components count is limited to 50, if we increase this number, we need to iterate the posts in batches.
22 21 $posts = get_posts( [
23 22 'post_type' => Component_Document::TYPE,
24 - 'post_status' => 'any',
23 + 'post_status' => 'publish',
25 24 'posts_per_page' => Components_REST_API::MAX_COMPONENTS,
26 25 ] );
27 26
28 27 $components = [];
@@ -27,62 +26,38 @@
27 26
28 27 $components = [];
29 28
30 29 foreach ( $posts as $post ) {
31 - $component = $this->get( $post->ID );
30 + $doc = Plugin::$instance->documents->get( $post->ID );
32 31
33 - if ( ! $component ) {
32 + if ( ! $doc ) {
34 33 continue;
35 34 }
36 35
37 36 $components[] = [
38 - 'id' => $component->get_main_id(),
39 - 'title' => $component->get_post()->post_title,
40 - 'uid' => $component->get_component_uid(),
41 - 'is_archived' => $component->get_is_archived(),
42 - 'styles' => $this->extract_styles( $component->get_elements_data() ),
37 + 'id' => $doc->get_main_id(),
38 + 'name' => $doc->get_post()->post_title,
39 + 'styles' => $this->extract_styles( $doc->get_elements_data() ),
43 40 ];
44 41 }
45 42
46 - return Collection::make( $components );
43 + return Components::make( $components );
47 44 }
48 45
49 - public function get( $id, bool $include_autosave = true ) {
50 - $doc = $include_autosave
51 - ? Plugin::$instance->documents->get_doc_or_auto_save( $id, get_current_user_id() )
52 - : Plugin::$instance->documents->get( $id );
53 -
54 - if ( ! $doc instanceof Component_Document ) {
55 - return null;
56 - }
57 -
58 - return $doc;
59 - }
60 -
61 - public function create( string $title, array $content, string $status, string $uid, array $settings = [] ) {
46 + public function create( string $name, array $content ) {
62 47 $document = Plugin::$instance->documents->create(
63 48 Component_Document::get_type(),
64 49 [
65 - 'post_title' => $title,
66 - 'post_status' => $status,
67 - ],
68 - [
69 - Component_Document::COMPONENT_UID_META_KEY => $uid,
50 + 'post_title' => $name,
51 + 'post_status' => 'publish',
70 52 ]
71 53 );
72 54
73 - try {
74 - $saved = $document->save( [
75 - 'elements' => $content,
76 - 'settings' => $settings,
77 - ] );
78 - } catch ( \Exception $e ) {
79 - $document->force_delete();
80 - throw $e;
81 - }
55 + $saved = $document->save( [
56 + 'elements' => $content,
57 + ] );
82 58
83 59 if ( ! $saved ) {
84 - $document->force_delete();
85 60 throw new \Exception( 'Failed to create component' );
86 61 }
87 62
88 63 return $document->get_main_id();
@@ -98,116 +73,6 @@
98 73 }
99 74 }
100 75
101 76 return $styles;
102 - }
103 -
104 - public function archive( array $ids, string $status ) {
105 - $failed_ids = [];
106 - $success_ids = [];
107 -
108 - foreach ( $ids as $id ) {
109 - try {
110 - $component = $this->get_component_for_edit( $id, $status );
111 -
112 - if ( ! $component ) {
113 - $failed_ids[] = $id;
114 - continue;
115 - }
116 -
117 - $component->archive();
118 - $success_ids[] = $id;
119 - } catch ( \Exception $e ) {
120 - $failed_ids[] = $id;
121 - }
122 - }
123 -
124 - return [
125 - 'failedIds' => $failed_ids,
126 - 'successIds' => $success_ids,
127 - ];
128 - }
129 -
130 - public function update_title( int $component_id, string $title, string $status ): bool {
131 - $component = $this->get_component_for_edit( $component_id, $status );
132 -
133 - if ( ! $component ) {
134 - return false;
135 - }
136 -
137 - return $component->update_title( $title );
138 - }
139 -
140 - /**
141 - * Get the component for edit.
142 - *
143 - * @param int $component_id The component ID.
144 - * @param string $target_status The target status, means the status the component should be saved as.
145 - * @return ?Component_Document The component document for edit.
146 - *
147 - * If target status is an autosave / draft:
148 - * - If the component main document is autosave / draft, it will return the main document.
149 - * - If the component main document is published, it will create a new autosave document and return it.
150 -
151 - * If target status is publish:
152 - * - Will return the main document. If it's an autosave, it will be published later by the publish_component method.
153 - */
154 - private function get_component_for_edit( int $component_id, string $target_status ): ?Component_Document {
155 - $component = $this->get( $component_id );
156 -
157 - if ( ! $component ) {
158 - return null;
159 - }
160 -
161 - $autosave_statuses = [ Document::STATUS_AUTOSAVE, Document::STATUS_DRAFT ];
162 - $autosave_exists = $component->is_autosave();
163 - $should_create_autosave = in_array( $target_status, $autosave_statuses, true ) && ! $autosave_exists;
164 -
165 - if ( ! $should_create_autosave ) {
166 - return $component;
167 - }
168 -
169 - // Create a new autosave document, based on the published version.
170 - return $component->get_autosave( 0, true );
171 - }
172 -
173 - public function publish_component( Component_Document $component ): bool {
174 - try {
175 - $main_id = $component->get_main_id();
176 - $main_component = $this->get( $main_id, false );
177 -
178 - $autosave = $main_component->get_newer_autosave();
179 -
180 - if ( $autosave ) {
181 - $success = $this->copy_autosave_data_to_main_component_document_and_publish( $autosave, $main_component, $main_id );
182 - } else {
183 - $success = $main_component->update_status( Document::STATUS_PUBLISH );
184 - }
185 -
186 - if ( ! $success ) {
187 - throw new \Exception( 'Failed to publish component' );
188 - }
189 - } catch ( \Exception $e ) {
190 - return false;
191 - }
192 -
193 - return true;
194 - }
195 -
196 - private function copy_autosave_data_to_main_component_document_and_publish( Component_Document $autosave, Component_Document $main_component_document, int $main_id ): bool {
197 - $autosave_id = $autosave->get_post()->ID;
198 -
199 - // Copy component custom meta keys from the autosave to the main component.
200 - Plugin::$instance->db->copy_elementor_meta( $autosave_id, $main_id, Component_Document::COMPONENT_CUSTOM_META_KEYS );
201 -
202 - $autosave_elements = $autosave->get_elements_data();
203 - $autosave_title = $autosave->get_post()->post_title;
204 -
205 - return $main_component_document->save( [
206 - 'elements' => $autosave_elements,
207 - 'settings' => [
208 - 'post_status' => Document::STATUS_PUBLISH,
209 - 'post_title' => $autosave_title,
210 - ],
211 - ] );
212 77 }
213 78 }