| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Api; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
class TemplateData |
| 7 |
{ |
| 8 |
|
| 9 |
public $prefix = ''; |
| 10 |
public $param = ''; |
| 11 |
public $request = null; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
add_action('rest_api_init', function () { |
| 16 |
register_rest_route('emailkit/v1', 'template-data', array( |
| 17 |
'methods' => \WP_REST_Server::ALLMETHODS, |
| 18 |
'callback' => [$this, 'action'], |
| 19 |
'permission_callback' => '__return_true', |
| 20 |
)); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
public function action($request) |
| 25 |
{ |
| 26 |
|
| 27 |
if (!wp_verify_nonce($request->get_header( 'X-WP-Nonce' ), 'wp_rest')) { |
| 28 |
return [ |
| 29 |
'status' => 'fail', |
| 30 |
'message' => ['Nonce mismatch.'] |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
if (!is_user_logged_in() || !current_user_can( 'publish_posts' )) { |
| 35 |
return [ |
| 36 |
'status' => 'fail', |
| 37 |
'message' => ['Access denied.'] |
| 38 |
]; |
| 39 |
} |
| 40 |
$template = ''; |
| 41 |
$html = ''; |
| 42 |
$body = $request->get_body(); |
| 43 |
$req = json_decode($body); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
if(!empty($request->get_param( 'emailkit-editor-template' ) && trim($request->get_param( 'emailkit-editor-template' )) !== '')){ |
| 48 |
$template = file_get_contents($request->get_param( 'emailkit-editor-template' )); |
| 49 |
$html = file_get_contents(str_replace( "content.json", "content.html", $request->get_param( 'emailkit-editor-template' ))); |
| 50 |
} |
| 51 |
|
| 52 |
$subject = !empty($request->get_param( 'emailkit_template_title' ))? trim($request->get_param( 'emailkit_template_title' )) : null; |
| 53 |
|
| 54 |
$post_id = empty($req->postIdField) ? '' : $req->postIdField; |
| 55 |
|
| 56 |
if (empty($post_id)) { |
| 57 |
$subject = trim($request->get_param('emailkit_template_title')); |
| 58 |
$data = array( |
| 59 |
'post_type' => 'emailkit', |
| 60 |
'post_status' => 'publish', |
| 61 |
'post_author' => get_current_user_id(), |
| 62 |
'post_title' => $subject !== '' ? $subject : "New Template ".uniqid(), |
| 63 |
'meta_input' => array( |
| 64 |
'emailkit_template_content_html' => $html, |
| 65 |
'emailkit_template_content_object' => $template, |
| 66 |
'emailkit_email_type' => $request->get_param('emailkit_email_type'), |
| 67 |
'emailkit_template_type' => $request->get_param('emailkit_template_type'), |
| 68 |
'emailkit_template_status' => 'inactive', |
| 69 |
'emailkit_email_subject' => $req->subject??'', |
| 70 |
'emailkit_email_preheader' => $req->preheader??'', |
| 71 |
'emailkit_template_initial_content_object' => $template, |
| 72 |
) |
| 73 |
); |
| 74 |
|
| 75 |
if('active' == trim($request->get_param('emailkit_template_status'))){ |
| 76 |
$this->deactivateTemplateTypes(get_post_meta($post_id,'emailkit_template_type', true)); |
| 77 |
} |
| 78 |
$post_id = wp_insert_post($data); |
| 79 |
|
| 80 |
$message = esc_html__( 'Post created successfully.', 'emailkit' ); |
| 81 |
} |
| 82 |
|
| 83 |
return [ |
| 84 |
"status" => "success", |
| 85 |
"data" => [ |
| 86 |
"templateId" => $post_id, |
| 87 |
'builder_url' => (admin_url("post.php?post={$post_id}&action=emailkit-builder")), |
| 88 |
], |
| 89 |
"message" => [ |
| 90 |
$message, |
| 91 |
], |
| 92 |
]; |
| 93 |
} |
| 94 |
public function deactivateTemplateTypes($type) |
| 95 |
{ |
| 96 |
|
| 97 |
$query = array( |
| 98 |
'post_type' => 'emailkit', |
| 99 |
'relation' => 'AND', |
| 100 |
'meta_query' => array( |
| 101 |
array( |
| 102 |
'key' => 'emailkit_template_type', |
| 103 |
'value' => $type, |
| 104 |
'compare' => '==', |
| 105 |
), |
| 106 |
array( |
| 107 |
'key' => 'emailkit_template_status', |
| 108 |
'value' => 'active', |
| 109 |
'compare' => '==', |
| 110 |
), |
| 111 |
'fields' => 'ids' |
| 112 |
) |
| 113 |
); |
| 114 |
|
| 115 |
$data = new \WP_Query($query); |
| 116 |
if (isset($data)) { |
| 117 |
|
| 118 |
$postsIds = wp_list_pluck($data->posts, 'ID'); |
| 119 |
foreach ($postsIds as $id) { |
| 120 |
update_post_meta($id, 'emailkit_template_status', 'inactive'); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |