| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 |
|
| 5 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 6 |
use RadiusTheme\SB\Helpers\Fns; |
| 7 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 8 |
|
| 9 |
// Do not allow directly accessing this file. |
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit( 'This script cannot be accessed directly.' ); |
| 12 |
} |
| 13 |
/** |
| 14 |
* Default Template Switch. |
| 15 |
*/ |
| 16 |
class CreateTemplate { |
| 17 |
/** |
| 18 |
* Singleton Trait. |
| 19 |
*/ |
| 20 |
use SingletonTrait; |
| 21 |
/** |
| 22 |
* Construct function |
| 23 |
*/ |
| 24 |
private function __construct() { |
| 25 |
add_action( 'wp_ajax_rtsb_builder_create_template', [ $this, 'response' ] ); |
| 26 |
} |
| 27 |
/** |
| 28 |
* Create template |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public static function response() { |
| 33 |
$page_type = isset( $_POST['page_type'] ) ? sanitize_text_field( wp_unslash( $_POST['page_type'] ) ) : null; |
| 34 |
$page_id = isset( $_POST['page_id'] ) ? absint( wp_unslash( $_POST['page_id'] ) ) : null; |
| 35 |
$page_name = isset( $_POST['page_name'] ) ? sanitize_text_field( wp_unslash( $_POST['page_name'] ) ) : null; |
| 36 |
$edit_with = isset( $_POST['template_edit_with'] ) ? sanitize_text_field( wp_unslash( $_POST['template_edit_with'] ) ) : null; |
| 37 |
$default_template = isset( $_POST['default_template'] ) ? sanitize_text_field( wp_unslash( $_POST['default_template'] ) ) : null; |
| 38 |
$import_layout = isset( $_POST['import_default_layout'] ) ? sanitize_text_field( wp_unslash( $_POST['import_default_layout'] ) ) : null; |
| 39 |
$url = '#'; |
| 40 |
|
| 41 |
if ( ! Fns::verify_nonce() || ! $page_type ) { |
| 42 |
$return = [ |
| 43 |
'success' => false, |
| 44 |
'post_id' => $page_id, |
| 45 |
]; |
| 46 |
wp_send_json( $return ); |
| 47 |
} |
| 48 |
$option_name = BuilderFns::option_name( $page_type ); |
| 49 |
$post_data = [ |
| 50 |
'ID' => $page_id, |
| 51 |
'post_title' => $page_name, |
| 52 |
'meta_input' => [ |
| 53 |
BuilderFns::template_type_meta_key() => $page_type, |
| 54 |
], |
| 55 |
]; |
| 56 |
if ( 'elementor' == $edit_with ) { |
| 57 |
$post_data['meta_input']['_elementor_edit_mode'] = 'builder'; |
| 58 |
} elseif ( 'gutenberg' == $edit_with ) { |
| 59 |
$post_data['meta_input']['_elementor_edit_mode'] = ''; |
| 60 |
} |
| 61 |
if ( $page_id ) { |
| 62 |
$page_id = wp_update_post( $post_data ); |
| 63 |
$new_page = false; |
| 64 |
} else { |
| 65 |
unset( $post_data['ID'] ); |
| 66 |
$post_data['post_type'] = BuilderFns::$post_type_tb; |
| 67 |
$post_data['post_status'] = 'publish'; |
| 68 |
$page_id = wp_insert_post( $post_data ); |
| 69 |
$new_page = true; |
| 70 |
if ( 'elementor' == $edit_with ) { |
| 71 |
update_post_meta( $page_id, '_wp_page_template', 'elementor_header_footer' ); |
| 72 |
} |
| 73 |
} |
| 74 |
$edit_by = ''; |
| 75 |
if ( $page_id ) { |
| 76 |
$edit_by = Fns::page_edit_with( $page_id ); |
| 77 |
if ( 'default_template' === $default_template ) { |
| 78 |
update_option( $option_name, $page_id ); |
| 79 |
} else { |
| 80 |
update_option( $option_name, '' ); |
| 81 |
} |
| 82 |
$action = 'edit'; |
| 83 |
if ( 'elementor' === $edit_by ) { |
| 84 |
$action = 'elementor'; |
| 85 |
} |
| 86 |
$url = add_query_arg( |
| 87 |
[ |
| 88 |
'post' => $page_id, |
| 89 |
'action' => $action, |
| 90 |
], |
| 91 |
admin_url( 'post.php' ) |
| 92 |
); |
| 93 |
$filepath = rtsb()->plugin_path() . $import_layout; |
| 94 |
// TODO:: Need use Elementor importer. In future Version. |
| 95 |
if ( ! empty( $import_layout ) && file_exists( $filepath ) ) { |
| 96 |
$json_value = wp_json_file_decode( $filepath, [ 'associative' => true ] ); |
| 97 |
if ( ! empty( $json_value['content'] ) ) { |
| 98 |
$content = wp_slash( wp_json_encode( $json_value['content'] ) ); |
| 99 |
update_post_meta( $page_id, '_elementor_data', $content ); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$return = [ |
| 105 |
'success' => true, |
| 106 |
'post_id' => $page_id, |
| 107 |
'post_edit_url' => $url, |
| 108 |
'new_page' => $new_page, |
| 109 |
|
| 110 |
]; |
| 111 |
if ( $edit_by ) { |
| 112 |
$return['edit_btn_text'] = esc_html__( sprintf( 'Edit with %s', $edit_by ), 'shopbuilder' ); |
| 113 |
} |
| 114 |
wp_send_json( $return ); |
| 115 |
wp_die(); |
| 116 |
} |
| 117 |
} |
| 118 |
|