# shopbuilder/1.1.1/app/Controllers/Admin/Ajax/CreateTemplate.php

ShopBuilder – WooCommerce Builder For Elementor, version 1.1.1. 126 lines.

- Page: https://pluginprobe.com/plugins/shopbuilder/1.1.1/code/app/Controllers/Admin/Ajax/CreateTemplate.php
- Raw: https://pluginprobe.com/plugins/shopbuilder/1.1.1/raw/app/Controllers/Admin/Ajax/CreateTemplate.php
- Modified: 2023-05-25T10:51:40+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/shopbuilder/1.1.1/code/app/Controllers/Admin/Ajax/CreateTemplate.php#L10-L20`.

```php
<?php

namespace RadiusTheme\SB\Controllers\Admin\Ajax;

use RadiusTheme\SB\Helpers\BuilderFns;
use RadiusTheme\SB\Helpers\Fns;
use RadiusTheme\SB\Traits\SingletonTrait;

// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
	exit( 'This script cannot be accessed directly.' );
}
/**
 * Default Template Switch.
 */
class CreateTemplate {
	/**
	 * Singleton Trait.
	 */
	use SingletonTrait;
	/**
	 * Construct function
	 */
	private function __construct() {
		add_action( 'wp_ajax_rtsb_builder_create_template', [ $this, 'response' ] );
	}
	/**
	 * Create template
	 *
	 * @return void
	 */
	public static function response() {
		$page_type        = isset( $_POST['page_type'] ) ? sanitize_text_field( wp_unslash( $_POST['page_type'] ) ) : null;
		$page_id          = isset( $_POST['page_id'] ) ? absint( wp_unslash( $_POST['page_id'] ) ) : null;
		$page_name        = isset( $_POST['page_name'] ) ? sanitize_text_field( wp_unslash( $_POST['page_name'] ) ) : null;
		$edit_with        = isset( $_POST['template_edit_with'] ) ? sanitize_text_field( wp_unslash( $_POST['template_edit_with'] ) ) : null;
		$default_template = isset( $_POST['default_template'] ) ? sanitize_text_field( wp_unslash( $_POST['default_template'] ) ) : null;
		$import_layout    = isset( $_POST['import_default_layout'] ) ? sanitize_text_field( wp_unslash( $_POST['import_default_layout'] ) ) : null;
		$product_id    = isset( $_POST['display_product_id'] ) ? absint(  $_POST['display_product_id']  ) : null;

		$url              = '#';

		if ( ! Fns::verify_nonce() || ! $page_type ) {
			$return = [
				'success' => false,
				'post_id' => $page_id,
			];
			wp_send_json( $return );
		}
		$option_name = BuilderFns::option_name( $page_type );
		$post_data   = [
			'ID'         => $page_id,
			'post_title' => $page_name,
			'meta_input' => [
				BuilderFns::template_type_meta_key() => $page_type,
			],
		];
		if ( 'elementor' == $edit_with ) {
			$post_data['meta_input']['_elementor_edit_mode'] = 'builder';
		} elseif ( 'gutenberg' == $edit_with ) {
			$post_data['meta_input']['_elementor_edit_mode'] = '';
		}

		if ( $page_id ) {
			$page_id  = wp_update_post( $post_data );
			$new_page = false;
		} else {
			unset( $post_data['ID'] );
			$post_data['post_type']   = BuilderFns::$post_type_tb;
			$post_data['post_status'] = 'publish';
			$page_id                  = wp_insert_post( $post_data );
			$new_page                 = true;
			if ( 'elementor' == $edit_with ) {
				update_post_meta( $page_id, '_wp_page_template', 'elementor_header_footer' );
			}
		}

		$edit_by = '';
		if ( $page_id ) {
			if( 'product' == $page_type ){
				update_post_meta( $product_id, BuilderFns::$product_template_meta, $page_id );
				update_post_meta( $page_id, BuilderFns::$product_template_meta, $product_id );
			}
			$edit_by = Fns::page_edit_with( $page_id );
			if ( 'default_template' === $default_template ) {
				update_option( $option_name, $page_id );
			} else {
				update_option( $option_name, '' );
			}
			$action = 'edit';
			if ( 'elementor' === $edit_by ) {
				$action = 'elementor';
			}
			$url      = add_query_arg(
				[
					'post'   => $page_id,
					'action' => $action,
				],
				admin_url( 'post.php' )
			);
			$filepath = rtsb()->plugin_path() . $import_layout;
            // TODO:: Need use Elementor importer. In future Version.
			if ( ! empty( $import_layout ) && file_exists( $filepath ) ) {
				$json_value = wp_json_file_decode( $filepath, [ 'associative' => true ] );
				if ( ! empty( $json_value['content'] ) ) {
					$content = wp_slash( wp_json_encode( $json_value['content'] ) );
					update_post_meta( $page_id, '_elementor_data', $content );
				}
			}
		}

		$return = [
			'success'       => true,
			'post_id'       => $page_id,
			'post_edit_url' => $url,
			'new_page'      => $new_page,

		];
		if ( $edit_by ) {
			$return['edit_btn_text'] = esc_html__( sprintf( 'Edit with %s', $edit_by ), 'shopbuilder' );
		}
		wp_send_json( $return );
		wp_die();
	}
}

```
