PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.2.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.2.0
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 / Managers / TemplateManager.php

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

172 lines 4.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\Managers;
4
5 use Templately\Builder\PageTemplates;
6 use Templately\Builder\Source;
7 use Templately\Builder\Types\Archive;
8 use Templately\Builder\Types\BaseTemplate;
9 use Templately\Builder\Types\CourseArchive;
10 use Templately\Builder\Types\CourseSingle;
11 use Templately\Builder\Types\Error;
12 use Templately\Builder\Types\Footer;
13 use Templately\Builder\Types\Header;
14 use Templately\Builder\Types\Page;
15 use Templately\Builder\Types\PageSingle;
16 use Templately\Builder\Types\Post;
17 use Templately\Builder\Types\ProductArchive;
18 use Templately\Builder\Types\ProductSingle;
19 use Templately\Builder\Types\Single;
20 use WP_Error;
21
22 class TemplateManager {
23 private $types = [];
24 private $documents = [];
25
26 public function __construct( $builder ) {
27 $this->register_types();
28 }
29
30 public function register_types() {
31 $types = [
32 'header' => Header::class,
33 'footer' => Footer::class,
34 'single' => Single::class,
35 'archive' => Archive::class,
36 'post' => Post::class,
37 'page' => Page::class,
38 'error' => Error::class,
39
40 // WooCommerce Template Types
41 'product_single' => ProductSingle::class,
42 'product_archive' => ProductArchive::class,
43
44 'page_single' => PageSingle::class,
45
46 // LMS (LearnDash)
47 'course_archive' => CourseArchive::class,
48 'course_single' => CourseSingle::class,
49 ];
50
51 $this->types = $types;
52 }
53
54 /**
55 * @param $type
56 * @param $post_data
57 * @param array $meta
58 *
59 * @return BaseTemplate|WP_Error
60 */
61 public function create( $type, $post_data, array $meta = [] ) {
62 /**
63 * @var BaseTemplate $class ;
64 */
65 $class = $this->get_template_type( $type );
66
67 $should_update_title = false;
68
69 if ( empty( $post_data['post_title'] ) ) {
70 $post_data['post_title'] = ucwords( $type ) . ' Template';
71
72 $should_update_title = true;
73 }
74
75 $meta = wp_parse_args( $meta, [
76 Source::TYPE_META_KEY => $type,
77 Source::PLATFORM_META_KEY => $meta['platform'] ?? 'gutenberg'
78 ] );
79
80 if ( isset( $meta['platform'] ) ) {
81 unset( $meta['platform'] );
82 }
83
84 if ( $meta[ Source::PLATFORM_META_KEY ] == 'elementor' ) {
85 $meta['_wp_page_template'] = 'elementor_header_footer';
86 } elseif ( $meta[ Source::PLATFORM_META_KEY ] == 'gutenberg' ) {
87 $meta['_wp_page_template'] = PageTemplates::TEMPLATE_HEADER_FOOTER;
88 }
89
90 $post_data['meta_input'] = $meta;
91
92 $post_type = $class::get_property( 'cpt' );
93 if ( ! empty( $post_type ) && empty( $post_data['post_type'] ) ) {
94 $post_data['post_type'] = $post_type;
95 }
96
97 $post_id = wp_insert_post( $post_data );
98
99 if ( is_wp_error( $post_id ) ) {
100 return $post_id;
101 }
102
103 if ( $should_update_title ) {
104 $post_data['ID'] = $post_id;
105 $post_data['post_title'] .= " #{$post_id} (by Templately)";
106 unset( $post_data['meta_input'] );
107
108 wp_update_post( $post_data );
109 }
110
111 return new $class( [
112 'post_id' => $post_id
113 ] );
114 }
115
116 private function get_template_type( $type, $default = 'post' ): string {
117 if ( isset( $this->types[ $type ] ) ) {
118 return $this->types[ $type ];
119 }
120
121 return $this->types[ $default ];
122 }
123
124 /**
125 *
126 * @param int $post_id
127 * @param bool $from_cache
128 *
129 * @return ThemeTemplate||false
130 */
131 public function get( int $post_id, $from_cache = true ) {
132 $this->register_types();
133
134 $post_id = absint( $post_id );
135
136 if ( ! $post_id || ! get_post( $post_id ) ) {
137 return false;
138 }
139
140 if ( ! $from_cache || ! isset( $this->documents[ $post_id ] ) ) {
141 $doc_type = $this->get_template_type_by_id( $post_id );
142 $doc_type_class = $this->get_template_type( $doc_type );
143
144 $this->documents[ $post_id ] = new $doc_type_class( [
145 'post_id' => $post_id,
146 ] );
147 }
148
149 return $this->documents[ $post_id ];
150 }
151
152 private function get_template_type_by_id( int $post_id ) {
153 // Auto-save inherits from the original post.
154 if ( wp_is_post_autosave( $post_id ) ) {
155 $post_id = wp_get_post_parent_id( $post_id );
156 }
157
158 $template_type = get_post_meta( $post_id, Source::TYPE_META_KEY, true );
159
160 if ( $template_type && isset( $this->types[ $template_type ] ) ) {
161 return $template_type;
162 }
163
164 return 'post';
165 }
166
167 public function get_template_types(): array {
168 $this->register_types();
169
170 return $this->types;
171 }
172 }