PluginProbe
Elementor Website Builder – more than just a page builder / 3.34.2
Elementor Website Builder – more than just a page builder v3.34.2
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
elementor / modules / components / components-repository.php

components-repository.php in Elementor Website Builder – more than just a page builder 3.34.2, at modules/components/components-repository.php

94 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Components;
4
5 use Elementor\Core\Utils\Collection;
6 use Elementor\Modules\Components\Documents\Component;
7 use Elementor\Modules\Components\Documents\Component as Component_Document;
8 use Elementor\Plugin;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 class Components_Repository {
15
16 public static function make(): Components_Repository {
17 return new self();
18 }
19
20 public function all() {
21 // Components count is limited to 50, if we increase this number, we need to iterate the posts in batches.
22 $posts = get_posts( [
23 'post_type' => Component_Document::TYPE,
24 'post_status' => 'any',
25 'posts_per_page' => Components_REST_API::MAX_COMPONENTS,
26 ] );
27
28 $components = [];
29
30 foreach ( $posts as $post ) {
31 $doc = Plugin::$instance->documents->get( $post->ID );
32
33 if ( ! $doc || ! $doc instanceof Component_Document ) {
34 continue;
35 }
36
37 $components[] = [
38 'id' => $doc->get_main_id(),
39 'title' => $doc->get_post()->post_title,
40 'uid' => $doc->get_component_uid(),
41 'styles' => $this->extract_styles( $doc->get_elements_data() ),
42 ];
43 }
44
45 return Collection::make( $components );
46 }
47
48 public function get( $id ) {
49 $doc = Plugin::$instance->documents->get( $id );
50
51 if ( ! $doc instanceof Component ) {
52 return null;
53 }
54
55 return $doc;
56 }
57
58 public function create( string $title, array $content, string $status, string $uid ) {
59 $document = Plugin::$instance->documents->create(
60 Component_Document::get_type(),
61 [
62 'post_title' => $title,
63 'post_status' => $status,
64 ],
65 [
66 Component_Document::COMPONENT_UID_META_KEY => $uid,
67 ]
68 );
69
70 $saved = $document->save( [
71 'elements' => $content,
72 ] );
73
74 if ( ! $saved ) {
75 throw new \Exception( 'Failed to create component' );
76 }
77
78 return $document->get_main_id();
79 }
80 private function extract_styles( array $elements, array $styles = [] ) {
81 foreach ( $elements as $element ) {
82 if ( isset( $element['styles'] ) ) {
83 $styles = array_merge( $styles, $element['styles'] );
84 }
85
86 if ( isset( $element['elements'] ) ) {
87 $styles = $this->extract_styles( $element['elements'], $styles );
88 }
89 }
90
91 return $styles;
92 }
93 }
94