PluginProbe ʕ •ᴥ•ʔ
ShopPress – Shop Builder for Elementor and WooCommerce / trunk
ShopPress – Shop Builder for Elementor and WooCommerce vtrunk
shop-press / Elementor / template-library / TemplateSourceContent.php
shop-press / Elementor / template-library Last commit date
assets 2 years ago TemplateSourceContent.php 4 months ago TemplatesLib.php 2 weeks ago index.php 2 years ago
TemplateSourceContent.php
232 lines
1 <?php
2 /**
3 * Template Library.
4 *
5 * @package ShopPress
6 */
7
8 namespace ShopPress\Elementor\TemplateLibrary;
9
10 use Elementor\Plugin;
11 use Elementor\TemplateLibrary\Source_Base;
12
13 /**
14 * Custom source.
15 */
16 class TemplateSourceContent extends Source_Base {
17 /**
18 * Get remote template ID.
19 *
20 * Retrieve the remote template ID.
21 *
22 * @since 1.2.0
23 * @access public
24 *
25 * @return string The remote template ID.
26 */
27 public function get_id() {
28 return 'product_loop';
29 }
30
31 /**
32 * Get remote template title.
33 *
34 * Retrieve the remote template title.
35 *
36 * @since 1.2.0
37 * @access public
38 *
39 * @return string The remote template title.
40 */
41 public function get_title() {
42 return 'Content';
43 }
44
45 /**
46 * Register remote template data.
47 *
48 * Used to register custom template data like a post type, a taxonomy or any
49 * other data.
50 *
51 * @since 1.2.0
52 * @access public
53 */
54 public function register_data() {}
55
56 /**
57 * Get remote templates.
58 *
59 * Retrieve remote templates from webnus servers.
60 *
61 * @since 1.2.0
62 * @access public
63 *
64 * @param array $args Optional. Nou used in remote source.
65 *
66 * @return array Remote templates.
67 */
68 public function get_items( $args = array() ) {
69 $library_data = TemplatesLib::get_library_data();
70
71 $templates = array();
72
73 if ( ! empty( $library_data ) ) {
74 foreach ( $library_data as $template_data ) {
75 $templates[] = $this->prepare_template( $template_data );
76 }
77 }
78
79 return $templates;
80 }
81
82 /**
83 * Get remote template.
84 *
85 * Retrieve a single remote template from webnus servers.
86 *
87 * @since 1.2.0
88 * @access public
89 *
90 * @param int $template_id The template ID.
91 *
92 * @return array Remote template.
93 */
94 public function get_item( $template_id ) {
95 $templates = $this->get_items();
96
97 return $templates[ $template_id ];
98 }
99
100 /**
101 * Save remote template.
102 *
103 * Remote template from webnus servers cannot be saved on the
104 * database as they are retrieved from remote servers.
105 *
106 * @since 1.2.0
107 * @access public
108 *
109 * @param array $template_data Remote template data.
110 *
111 * @return \WP_Error
112 */
113 public function save_item( $template_data ) {
114 return new \WP_Error( 'invalid_request', 'Cannot save template to a remote source' );
115 }
116
117 /**
118 * Update remote template.
119 *
120 * Remote template from webnus servers cannot be updated on the
121 * database as they are retrieved from remote servers.
122 *
123 * @since 1.2.0
124 * @access public
125 *
126 * @param array $new_data New template data.
127 *
128 * @return \WP_Error
129 */
130 public function update_item( $new_data ) {
131 return new \WP_Error( 'invalid_request', 'Cannot update template to a remote source' );
132 }
133
134 /**
135 * Delete remote template.
136 *
137 * Remote template from webnus servers cannot be deleted from the
138 * database as they are retrieved from remote servers.
139 *
140 * @since 1.2.0
141 * @access public
142 *
143 * @param int $template_id The template ID.
144 *
145 * @return \WP_Error
146 */
147 public function delete_template( $template_id ) {
148 return new \WP_Error( 'invalid_request', 'Cannot delete template from a remote source' );
149 }
150
151 /**
152 * Export remote template.
153 *
154 * Remote template from webnus servers cannot be exported from the
155 * database as they are retrieved from remote servers.
156 *
157 * @since 1.2.0
158 * @access public
159 *
160 * @param int $template_id The template ID.
161 *
162 * @return \WP_Error
163 */
164 public function export_template( $template_id ) {
165 return new \WP_Error( 'invalid_request', 'Cannot export template from a remote source' );
166 }
167
168 /**
169 * Get remote template data.
170 *
171 * Retrieve the data of a single remote template from webnus servers.
172 *
173 * @since 1.2.0
174 * @access public
175 *
176 * @param array $args Custom template arguments.
177 * @param string $context Optional. The context. Default is `display`.
178 *
179 * @return array|\WP_Error Remote Template data.
180 */
181 public function get_data( array $args, $context = 'display' ) {
182 $data = TemplatesLib::shoppress_get_template_data( $args['template_id'] );
183
184 if ( is_wp_error( $data ) ) {
185 return $data;
186 }
187
188 $data = (array) $data;
189
190 $data['content'] = $this->replace_elements_ids( $data['content'] );
191 $data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );
192
193 $post_id = $args['editor_post_id'];
194 $document = Plugin::$instance->documents->get( $post_id );
195 if ( $document ) {
196 $data['content'] = $document->get_elements_raw_data( $data['content'], true );
197 }
198
199 return $data;
200 }
201
202 /**
203 * Prepare template.
204 *
205 * Prepare template data.
206 *
207 * @since 1.2.0
208 * @access private
209 *
210 * @param array $template_data Collection of template data.
211 * @return array Collection of template data.
212 */
213 private function prepare_template( array $template_data ) {
214 $favorite_templates = $this->get_user_meta( 'favorites' );
215
216 return array(
217 'template_id' => $template_data['id'],
218 'source' => $this->get_id(),
219 'type' => $template_data['type'],
220 'subtype' => $template_data['subtype'],
221 'title' => $template_data['title'],
222 'thumbnail' => $template_data['thumbnail'],
223 'date' => $template_data['tmpl_created'],
224 'author' => $template_data['author'],
225 'tags' => $template_data['tags'],
226 'isPro' => $template_data['is_pro'],
227 'url' => $template_data['url'],
228 'favorite' => ! empty( $favorite_templates[ $template_data['id'] ] ),
229 );
230 }
231 }
232