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