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

195 lines 5.2 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
61 self::$theme_compatibility = new ThemeCompatibility();
62 self::$location_manager = new LocationManager( $this );
63 self::$page_template_module = new PageTemplates();
64 }
65
66 public function pre_get_posts( $query ) {
67 if ( ! is_admin() || ! $query->is_main_query() || empty( $query->query['post_type'] ) || $query->query['post_type'] !== 'templately_library' ) {
68 return;
69 }
70
71 $template_types = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : '';
72
73 if ( ! empty( $template_types ) ) {
74 $query->set( 'meta_query', [
75 [
76 'key' => Source::TYPE_META_KEY,
77 'value' => $template_types,
78 ]
79 ] );
80 }
81 }
82
83 public function source_register() {
84 self::$templates_manager = new TemplateManager( $this );
85 self::$conditions_manager = new ConditionManager( $this );
86 self::$source = new Source( $this );
87 }
88
89 public function create_template() {
90 check_admin_referer( 'templately_create_template' );
91
92 $_type = sanitize_text_field( wp_unslash( $_POST['template_type'] ) );
93
94 $_meta = [];
95
96 if ( isset( $_POST['meta'] ) && is_array( $_POST['meta'] ) ) {
97 $_meta = array_map( 'sanitize_text_field', wp_unslash( $_POST['meta'] ) );
98 }
99
100 $_post_data = [
101 'post_type' => 'templately_library',
102 'post_title' => sanitize_text_field( wp_unslash( $_POST['title'] ) )
103 ];
104
105 $template = self::$templates_manager->create( $_type, $_post_data, $_meta );
106
107 if ( is_wp_error( $template ) ) {
108 wp_die( $template );
109 }
110
111 wp_redirect( $template->get_edit_url() );
112 die;
113 }
114
115 public function get_public_post_types(): array {
116 $post_type_args = [
117 'show_in_nav_menus' => true,
118 ];
119
120 $_post_types = get_post_types( $post_type_args, 'objects' );
121
122 $post_types = [];
123
124 foreach ( $_post_types as $post_type => $object ) {
125 $post_types[ $post_type ] = $object->label;
126 }
127
128 return $post_types;
129 }
130
131 public function get_template( $template_id ) {
132 $template = self::$templates_manager->get( $template_id );
133
134 if ( ! empty( $template ) && ! $template instanceof ThemeTemplate ) {
135 $template = null;
136 }
137
138 return $template;
139 }
140
141 /**
142 * Save additional data for a specific post.
143 *
144 * This function filters the data by adding additional configuration.
145 * External developers can use this function to add custom configuration for different posts.
146 * It checks if the post type is not 'templately_library', then it adds a new configuration
147 * 'theme-post-content' to the data.
148 *
149 * @param array $data The original data that needs to be saved.
150 * @param int $post_id The ID of the post for which the data is being saved.
151 *
152 * @return array The modified data with the additional configuration.
153 */
154 public function elementor_document_config( $data, $post_id ) {
155 if ( Source::CPT === get_post_type( $post_id ) ) {
156 $data['panel'] = [
157 'widgets_settings' => [
158 'theme-post-content' => [
159 'show_in_panel' => true,
160 ],
161 ],
162 'elements_categories' => [
163 'theme-elements-archive' => [
164 'title' => esc_html__( 'Archive', 'elementor-pro' ),
165 ],
166 ],
167 ];
168
169 $conditions = self::$conditions_manager->get_conditions_for_display( $post_id );
170
171 $data['templately_builder']['conditions'] = $conditions;
172 $data['templately_builder']['post_type'] = get_post_type( $post_id );
173 }
174
175 return $data;
176 }
177
178 public function elementor_documents_get_post_id( $post_id ) {
179 if ( Source::CPT === get_post_type( $post_id ) ) {
180 add_filter( 'get_post_metadata', [ $this, 'modify_template_type' ], 10, 4 );
181 }
182
183 return $post_id;
184 }
185
186 public function modify_template_type( $value, $object_id, $meta_key, $single ) {
187 if ( Document::TYPE_META_KEY === $meta_key && Source::CPT === get_post_type( $object_id ) ) {
188 remove_filter( 'get_post_metadata', [ $this, 'modify_template_type' ] );
189 $value = get_post_meta( $object_id, Source::TYPE_META_KEY, $single );
190 }
191
192 return $value;
193 }
194
195 }