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