PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.1
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Builder / ThemeBuilder.php

ThemeBuilder.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.7.1, at includes/Builder/ThemeBuilder.php

250 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Builder;
4
5 use Elementor\Core\Base\Document;
6 use Templately\Builder\Managers\ConditionManager;
7 use Templately\Builder\Managers\LocationManager;
8 use Templately\Builder\Managers\TemplateManager;
9 use Templately\Builder\Managers\ThemeCompatibility;
10 use Templately\Builder\Types\ThemeTemplate;
11 use Templately\Utils\Base;
12 use Templately\Utils\Views;
13
14 class ThemeBuilder extends Base {
15 /**
16 * @var Source
17 */
18 public static $source;
19
20 /**
21 * @var ConditionManager
22 */
23 public static $conditions_manager;
24
25 /**
26 * @var TemplateManager
27 */
28 public static $templates_manager;
29
30 /**
31 * @var LocationManager
32 */
33 public static $location_manager;
34
35 /**
36 * @var ThemeCompatibility
37 */
38 public static $theme_compatibility;
39
40 /**
41 * @var PageTemplates
42 */
43 public static $page_template_module;
44
45 public static $views;
46
47 public function __construct() {
48 self::$views = Views::get_instance( TEMPLATELY_VIEWS_ABSPATH );
49
50 add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ], 1 );
51 add_action( 'wp', function () {
52 new TemplateLoader( $this, self::$views );
53 } );
54 // Unconditional (admin + front end): the editor's admin page bootstrap
55 // (post.php?action=elementor) never fires `wp`, so this can't be gated
56 // behind the TemplateLoader instantiation above. See TemplateLoader::
57 // register_preview_product_hooks() for why two render pathways are covered.
58 TemplateLoader::register_preview_product_hooks();
59
60 add_action( 'init', [ $this, 'source_register' ] );
61 add_action( 'wp_ajax_templately_create_template', [ $this, 'create_template' ] );
62
63 add_filter( 'elementor/document/config', [$this, 'elementor_document_config'], 10, 2 );
64 add_filter( 'elementor/documents/get/post_id', [$this, 'elementor_documents_get_post_id'] );
65 add_action( 'elementor/widgets/register', [ $this, 'register_elements' ] );
66
67 add_filter( 'the_content', [$this, 'filter_content'], 10 ,1);
68
69 self::$theme_compatibility = new ThemeCompatibility();
70 self::$location_manager = new LocationManager( $this );
71 self::$page_template_module = new PageTemplates();
72 }
73
74 public function pre_get_posts( $query ) {
75 if ( ! is_admin() || ! $query->is_main_query() || empty( $query->query['post_type'] ) || $query->query['post_type'] !== 'templately_library' ) {
76 return;
77 }
78
79 $template_types = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : '';
80
81 if ( ! empty( $template_types ) ) {
82 $query->set( 'meta_query', [
83 [
84 'key' => Source::TYPE_META_KEY,
85 'value' => $template_types,
86 ]
87 ] );
88 }
89 }
90
91 public function source_register() {
92 self::$templates_manager = new TemplateManager( $this );
93 self::$conditions_manager = new ConditionManager( $this );
94 self::$source = new Source( $this );
95 register_rest_field('templately_library','templately_type', [
96 'get_callback' => [ $this, 'get_rest_template_type' ],
97 ]);
98 }
99
100 public function get_rest_template_type() {
101 return get_post_meta( get_the_ID(), '_templately_template_type', true );
102 }
103
104 public function create_template() {
105 check_admin_referer( 'templately_create_template' );
106
107 // Check user capability
108 if (!current_user_can('delete_posts')) {
109 wp_send_json_error(['message' => 'Insufficient permissions']);
110 wp_die();
111 }
112
113 $_type = sanitize_text_field( wp_unslash( $_POST['template_type'] ) );
114
115 $_meta = [];
116
117 if ( isset( $_POST['meta'] ) && is_array( $_POST['meta'] ) ) {
118 $_meta = array_map( 'sanitize_text_field', wp_unslash( $_POST['meta'] ) );
119 }
120
121 $_post_data = [
122 'post_type' => 'templately_library',
123 'post_title' => sanitize_text_field( wp_unslash( $_POST['title'] ) )
124 ];
125
126 $template = self::$templates_manager->create( $_type, $_post_data, $_meta );
127
128 if ( is_wp_error( $template ) ) {
129 wp_die( $template );
130 }
131
132 wp_redirect( $template->get_edit_url() );
133 die;
134 }
135
136 public function get_public_post_types(): array {
137 $post_type_args = [
138 'show_in_nav_menus' => true,
139 ];
140
141 $_post_types = get_post_types( $post_type_args, 'objects' );
142
143 $post_types = [];
144
145 foreach ( $_post_types as $post_type => $object ) {
146 $post_types[ $post_type ] = $object->label;
147 }
148
149 return $post_types;
150 }
151
152 public function get_template( $template_id ) {
153 $template = self::$templates_manager->get( $template_id );
154
155 if ( ! empty( $template ) && ! $template instanceof ThemeTemplate ) {
156 $template = null;
157 }
158
159 return $template;
160 }
161
162 /**
163 * Save additional data for a specific post.
164 *
165 * This function filters the data by adding additional configuration.
166 * External developers can use this function to add custom configuration for different posts.
167 * It checks if the post type is not 'templately_library', then it adds a new configuration
168 * 'theme-post-content' to the data.
169 *
170 * @param array $data The original data that needs to be saved.
171 * @param int $post_id The ID of the post for which the data is being saved.
172 *
173 * @return array The modified data with the additional configuration.
174 */
175 public function elementor_document_config( $data, $post_id ) {
176 if ( Source::CPT === get_post_type( $post_id ) ) {
177 $data['panel'] = [
178 'widgets_settings' => [
179 'theme-post-content' => [
180 'show_in_panel' => true,
181 ],
182 ],
183 'elements_categories' => [
184 'theme-elements-archive' => [
185 'title' => esc_html__( 'Archive', 'elementor-pro' ),
186 ],
187 ],
188 ];
189
190 $conditions = self::$conditions_manager->get_conditions_for_display( $post_id );
191
192 $data['templately_builder']['conditions'] = $conditions;
193 $data['templately_builder']['post_type'] = get_post_type( $post_id );
194 }
195
196 return $data;
197 }
198
199 public function elementor_documents_get_post_id( $post_id ) {
200 if ( Source::CPT === get_post_type( $post_id ) ) {
201 add_filter( 'get_post_metadata', [ $this, 'modify_template_type' ], 10, 4 );
202 }
203
204 return $post_id;
205 }
206
207 public function modify_template_type( $value, $object_id, $meta_key, $single ) {
208 $_value = $value;
209 if ( empty($value) && Document::TYPE_META_KEY === $meta_key && Source::CPT === get_post_type( $object_id ) ) {
210 remove_filter( 'get_post_metadata', [ $this, 'modify_template_type' ] );
211 $value = get_post_meta( $object_id, Source::TYPE_META_KEY, $single );
212 $arr = [
213 'post' => 'single-post',
214 'page' => 'single-page',
215 'error' => 'error-404',
216 'product_single' => 'product',
217 'product_archive' => 'product-archive',
218 'course_single' => 'single-post',
219 'course_archive' => 'archive',
220 ];
221 $key = $single ? $value : (isset($value[0]) ? $value[0] : '');
222 $value = isset( $arr[ $key ] ) ? $arr[ $key ] : $value;
223 if(in_array($value, ['header', 'footer'])){
224 return $_value;
225 }
226 }
227
228 return $value;
229 }
230
231 public function register_elements( $widgets_manager ) {
232 $widgets_manager->register( new \Templately\Builder\Widgets\Post_Title() );
233 $widgets_manager->register( new \Templately\Builder\Widgets\Post_Content() );
234 $widgets_manager->register( new \Templately\Builder\Widgets\Featured_Image() );
235 $widgets_manager->register( new \Templately\Builder\Widgets\Site_Logo() );
236 }
237
238 public function filter_content( $content ) {
239 if(is_singular()){
240 global $post;
241 if(!empty($post) && $post->post_type === 'templately_library'){
242 if(in_array(get_post_meta($post->ID, '_templately_template_type', true), ['header', 'footer'])){
243 return '';
244 }
245 }
246 }
247 return $content;
248 }
249
250 }