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

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