| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Elementor\Rest; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use Cookiez\Modules\Elementor\Documents\Template_Defaults\Cookie_Consent_Template_Defaults; |
| 10 |
use Cookiez\Modules\Elementor\Documents\Template_Defaults\Preferences_Banner_Template_Defaults; |
| 11 |
use Cookiez\Modules\Elementor\Classes\Route_Base; |
| 12 |
use WP_Query; |
| 13 |
use WP_REST_Request; |
| 14 |
use WP_REST_Response; |
| 15 |
|
| 16 |
class Create_Template extends Route_Base { |
| 17 |
|
| 18 |
private const DOCUMENT_TYPE_COOKIE_CONSENT = 'cookie-consent'; |
| 19 |
private const DOCUMENT_TYPE_PREFERENCES_BANNER = 'preferences-banner'; |
| 20 |
private const ELEMENTOR_LIBRARY_CPT = 'elementor_library'; |
| 21 |
private const ELEMENTOR_TYPE_META_KEY = '_elementor_template_type'; |
| 22 |
private const ELEMENTOR_TAXONOMY_TYPE_SLUG = 'elementor_library_type'; |
| 23 |
|
| 24 |
private const ALLOWED_TEMPLATE_TYPES = [ |
| 25 |
self::DOCUMENT_TYPE_COOKIE_CONSENT, |
| 26 |
self::DOCUMENT_TYPE_PREFERENCES_BANNER, |
| 27 |
]; |
| 28 |
|
| 29 |
public string $path = 'create-template'; |
| 30 |
|
| 31 |
public function get_methods(): array { |
| 32 |
return [ 'POST' ]; |
| 33 |
} |
| 34 |
|
| 35 |
public function get_name(): string { |
| 36 |
return 'create-template'; |
| 37 |
} |
| 38 |
|
| 39 |
public function POST( WP_REST_Request $request ): WP_REST_Response { |
| 40 |
$template_type = $this->get_requested_template_type( $request ); |
| 41 |
|
| 42 |
$template_ids = $this->find_template_ids( $template_type ); |
| 43 |
$template_count = count( $template_ids ); |
| 44 |
|
| 45 |
if ( 1 < $template_count ) { |
| 46 |
return $this->respond_success_json( [ |
| 47 |
'editUrl' => $this->get_templates_admin_url( $template_type ), |
| 48 |
] ); |
| 49 |
} |
| 50 |
|
| 51 |
if ( 1 === $template_count ) { |
| 52 |
$post_id = $template_ids[0]; |
| 53 |
$this->set_template_defaults( $template_type, $post_id ); |
| 54 |
|
| 55 |
return $this->respond_success_json( [ |
| 56 |
'editUrl' => $this->get_elementor_edit_url( $post_id ), |
| 57 |
] ); |
| 58 |
} |
| 59 |
|
| 60 |
$post_id = $this->create_template( $template_type ); |
| 61 |
|
| 62 |
if ( is_wp_error( $post_id ) ) { |
| 63 |
return $this->respond_error_json( [ |
| 64 |
'message' => $post_id->get_error_message(), |
| 65 |
'code' => 'template_creation_failed', |
| 66 |
], 500 ); |
| 67 |
} |
| 68 |
|
| 69 |
$this->set_template_defaults( $template_type, $post_id ); |
| 70 |
|
| 71 |
return $this->respond_success_json( [ |
| 72 |
'editUrl' => $this->get_elementor_edit_url( $post_id ), |
| 73 |
] ); |
| 74 |
} |
| 75 |
|
| 76 |
private function get_requested_template_type( WP_REST_Request $request ): string { |
| 77 |
$raw = $request->get_param( 'template_type' ); |
| 78 |
if ( is_string( $raw ) && in_array( $raw, self::ALLOWED_TEMPLATE_TYPES, true ) ) { |
| 79 |
return $raw; |
| 80 |
} |
| 81 |
return self::DOCUMENT_TYPE_COOKIE_CONSENT; |
| 82 |
} |
| 83 |
|
| 84 |
private function set_template_defaults( string $template_type, int $post_id ): void { |
| 85 |
if ( self::DOCUMENT_TYPE_PREFERENCES_BANNER === $template_type ) { |
| 86 |
Preferences_Banner_Template_Defaults::set_default_template_data( $post_id ); |
| 87 |
return; |
| 88 |
} |
| 89 |
Cookie_Consent_Template_Defaults::set_default_template_data( $post_id ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @return int[] |
| 94 |
*/ |
| 95 |
private function find_template_ids( string $template_type ): array { |
| 96 |
$query = new WP_Query( [ |
| 97 |
'post_type' => self::ELEMENTOR_LIBRARY_CPT, |
| 98 |
'posts_per_page' => 2, |
| 99 |
'post_status' => 'any', |
| 100 |
'fields' => 'ids', |
| 101 |
'no_found_rows' => true, |
| 102 |
'update_post_term_cache' => false, |
| 103 |
'meta_query' => [ |
| 104 |
[ |
| 105 |
'key' => self::ELEMENTOR_TYPE_META_KEY, |
| 106 |
'value' => $template_type, |
| 107 |
], |
| 108 |
], |
| 109 |
] ); |
| 110 |
|
| 111 |
return array_map( 'intval', $query->get_posts() ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @return int|\WP_Error |
| 116 |
*/ |
| 117 |
private function create_template( string $template_type ) { |
| 118 |
$post_id = wp_insert_post( [ |
| 119 |
'post_title' => $this->get_template_post_title( $template_type ), |
| 120 |
'post_status' => 'publish', |
| 121 |
'post_type' => self::ELEMENTOR_LIBRARY_CPT, |
| 122 |
] ); |
| 123 |
|
| 124 |
if ( is_wp_error( $post_id ) ) { |
| 125 |
return $post_id; |
| 126 |
} |
| 127 |
|
| 128 |
update_post_meta( $post_id, self::ELEMENTOR_TYPE_META_KEY, $template_type ); |
| 129 |
|
| 130 |
wp_set_object_terms( $post_id, $template_type, self::ELEMENTOR_TAXONOMY_TYPE_SLUG ); |
| 131 |
|
| 132 |
update_post_meta( $post_id, '_elementor_edit_mode', 'builder' ); |
| 133 |
|
| 134 |
return $post_id; |
| 135 |
} |
| 136 |
|
| 137 |
private function get_template_post_title( string $template_type ): string { |
| 138 |
if ( self::DOCUMENT_TYPE_PREFERENCES_BANNER === $template_type ) { |
| 139 |
return esc_html__( 'Preferences Banner', 'cookiez' ); |
| 140 |
} |
| 141 |
return esc_html__( 'Cookie Consent', 'cookiez' ); |
| 142 |
} |
| 143 |
|
| 144 |
private function get_elementor_edit_url( int $post_id ): string { |
| 145 |
return admin_url( "post.php?post={$post_id}&action=elementor" ); |
| 146 |
} |
| 147 |
|
| 148 |
private function get_templates_admin_url( string $template_type ): string { |
| 149 |
return admin_url( 'edit.php?post_type=elementor_library&tabs_group=popup&elementor_library_type=' . rawurlencode( $template_type ) ); |
| 150 |
} |
| 151 |
} |
| 152 |
|