PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.4.0
EmailKit – Email Customizer for WooCommerce & WP v1.4.0
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Api / UploadImage.php

UploadImage.php in EmailKit – Email Customizer for WooCommerce & WP 1.4.0, at includes/Admin/Api/UploadImage.php

74 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailKit\Admin\Api;
4
5 defined('ABSPATH') || exit;
6
7 class UploadImage
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', 'upload-image', 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 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
27 return [
28 'status' => 'fail',
29 'message' => ['Nonce mismatch.'],
30 ];
31 }
32
33 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
34 return [
35 'status' => 'fail',
36 'message' => ['Access denied.'],
37 ];
38 }
39
40 if (isset($_FILES['file'])) {
41 // Handle the uploaded file
42 require_once(ABSPATH . 'wp-admin/includes/image.php');
43 require_once(ABSPATH . 'wp-admin/includes/file.php');
44 require_once(ABSPATH . 'wp-admin/includes/media.php');
45
46 // Upload the file
47 $attachment_id = media_handle_upload('file', 0);
48
49 if (is_wp_error($attachment_id)) {
50 return array(
51 'status' => 'fail',
52 'message' => $attachment_id->get_error_message(),
53 );
54 }
55
56 // Get the file URL
57 $file_url = wp_get_attachment_url($attachment_id);
58
59 return array(
60 'status' => 'success',
61 'data' => array(
62 'attachmentId' => $attachment_id,
63 'fileUrl' => esc_url($file_url),
64 ),
65 'message' => 'Image uploaded successfully.',
66 );
67 } else {
68 return array(
69 'status' => 'fail',
70 'message' => 'No file uploaded.',
71 );
72 }
73 }
74 }