PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.9
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.9
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.1.9, at includes/Builder/ThemeBuilder.php

245 lines 6.9 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
55 add_action( 'init', [ $this, 'source_register' ] );
56 add_action( 'wp_ajax_templately_create_template', [ $this, 'create_template' ] );
57
58 add_filter( 'elementor/document/config', [$this, 'elementor_document_config'], 10, 2 );
59 add_filter( 'elementor/documents/get/post_id', [$this, 'elementor_documents_get_post_id'] );
60 add_action( 'elementor/widgets/register', [ $this, 'register_elements' ] );
61
62 add_filter( 'the_content', [$this, 'filter_content'], 10 ,1);
63
64 self::$theme_compatibility = new ThemeCompatibility();
65 self::$location_manager = new LocationManager( $this );
66 self::$page_template_module = new PageTemplates();
67 }
68
69 public function pre_get_posts( $query ) {
70 if ( ! is_admin() || ! $query->is_main_query() || empty( $query->query['post_type'] ) || $query->query['post_type'] !== 'templately_library' ) {
71 return;
72 }
73
74 $template_types = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : '';
75
76 if ( ! empty( $template_types ) ) {
77 $query->set( 'meta_query', [
78 [
79 'key' => Source::TYPE_META_KEY,
80 'value' => $template_types,
81 ]
82 ] );
83 }
84 }
85
86 public function source_register() {
87 self::$templates_manager = new TemplateManager( $this );
88 self::$conditions_manager = new ConditionManager( $this );
89 self::$source = new Source( $this );
90 register_rest_field('templately_library','templately_type', [
91 'get_callback' => [ $this, 'get_rest_template_type' ],
92 ]);
93 }
94
95 public function get_rest_template_type() {
96 return get_post_meta( get_the_ID(), '_templately_template_type', true );
97 }
98
99 public function create_template() {
100 check_admin_referer( 'templately_create_template' );
101
102 // Check user capability
103 if (!current_user_can('delete_posts')) {
104 wp_send_json_error(['message' => 'Insufficient permissions']);
105 wp_die();
106 }
107
108 $_type = sanitize_text_field( wp_unslash( $_POST['template_type'] ) );
109
110 $_meta = [];
111
112 if ( isset( $_POST['meta'] ) && is_array( $_POST['meta'] ) ) {
113 $_meta = array_map( 'sanitize_text_field', wp_unslash( $_POST['meta'] ) );
114 }
115
116 $_post_data = [
117 'post_type' => 'templately_library',
118 'post_title' => sanitize_text_field( wp_unslash( $_POST['title'] ) )
119 ];
120
121 $template = self::$templates_manager->create( $_type, $_post_data, $_meta );
122
123 if ( is_wp_error( $template ) ) {
124 wp_die( $template );
125 }
126
127 wp_redirect( $template->get_edit_url() );
128 die;
129 }
130
131 public function get_public_post_types(): array {
132 $post_type_args = [
133 'show_in_nav_menus' => true,
134 ];
135
136 $_post_types = get_post_types( $post_type_args, 'objects' );
137
138 $post_types = [];
139
140 foreach ( $_post_types as $post_type => $object ) {
141 $post_types[ $post_type ] = $object->label;
142 }
143
144 return $post_types;
145 }
146
147 public function get_template( $template_id ) {
148 $template = self::$templates_manager->get( $template_id );
149
150 if ( ! empty( $template ) && ! $template instanceof ThemeTemplate ) {
151 $template = null;
152 }
153
154 return $template;
155 }
156
157 /**
158 * Save additional data for a specific post.
159 *
160 * This function filters the data by adding additional configuration.
161 * External developers can use this function to add custom configuration for different posts.
162 * It checks if the post type is not 'templately_library', then it adds a new configuration
163 * 'theme-post-content' to the data.
164 *
165 * @param array $data The original data that needs to be saved.
166 * @param int $post_id The ID of the post for which the data is being saved.
167 *
168 * @return array The modified data with the additional configuration.
169 */
170 public function elementor_document_config( $data, $post_id ) {
171 if ( Source::CPT === get_post_type( $post_id ) ) {
172 $data['panel'] = [
173 'widgets_settings' => [
174 'theme-post-content' => [
175 'show_in_panel' => true,
176 ],
177 ],
178 'elements_categories' => [
179 'theme-elements-archive' => [
180 'title' => esc_html__( 'Archive', 'elementor-pro' ),
181 ],
182 ],
183 ];
184
185 $conditions = self::$conditions_manager->get_conditions_for_display( $post_id );
186
187 $data['templately_builder']['conditions'] = $conditions;
188 $data['templately_builder']['post_type'] = get_post_type( $post_id );
189 }
190
191 return $data;
192 }
193
194 public function elementor_documents_get_post_id( $post_id ) {
195 if ( Source::CPT === get_post_type( $post_id ) ) {
196 add_filter( 'get_post_metadata', [ $this, 'modify_template_type' ], 10, 4 );
197 }
198
199 return $post_id;
200 }
201
202 public function modify_template_type( $value, $object_id, $meta_key, $single ) {
203 $_value = $value;
204 if ( empty($value) && Document::TYPE_META_KEY === $meta_key && Source::CPT === get_post_type( $object_id ) ) {
205 remove_filter( 'get_post_metadata', [ $this, 'modify_template_type' ] );
206 $value = get_post_meta( $object_id, Source::TYPE_META_KEY, $single );
207 $arr = [
208 'post' => 'single-post',
209 'page' => 'single-page',
210 'error' => 'error-404',
211 'product_single' => 'product',
212 'product_archive' => 'product-archive',
213 'course_single' => 'single-post',
214 'course_archive' => 'archive',
215 ];
216 $key = $single ? $value : (isset($value[0]) ? $value[0] : '');
217 $value = isset( $arr[ $key ] ) ? $arr[ $key ] : $value;
218 if(in_array($value, ['header', 'footer'])){
219 return $_value;
220 }
221 }
222
223 return $value;
224 }
225
226 public function register_elements( $widgets_manager ) {
227 $widgets_manager->register( new \Templately\Builder\Widgets\Post_Title() );
228 $widgets_manager->register( new \Templately\Builder\Widgets\Post_Content() );
229 $widgets_manager->register( new \Templately\Builder\Widgets\Featured_Image() );
230 $widgets_manager->register( new \Templately\Builder\Widgets\Site_Logo() );
231 }
232
233 public function filter_content( $content ) {
234 if(is_singular()){
235 global $post;
236 if(!empty($post) && $post->post_type === 'templately_library'){
237 if(in_array(get_post_meta($post->ID, '_templately_template_type', true), ['header', 'footer'])){
238 return '';
239 }
240 }
241 }
242 return $content;
243 }
244
245 }