| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers\AppControllers; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\App\Http\Controllers\Controller; |
| 7 |
use FluentCart\App\Services\Translations\TransStrings; |
| 8 |
use FluentCart\App\Vite; |
| 9 |
use FluentCart\Framework\Http\Request\File; |
| 10 |
use FluentCart\Framework\Http\Request\Request; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
|
| 13 |
class AppController extends Controller |
| 14 |
{ |
| 15 |
public function init(Request $request): \WP_REST_Response |
| 16 |
{ |
| 17 |
return $this->sendSuccess([ |
| 18 |
'rest' => Helper::getRestInfo(), |
| 19 |
'asset_url' => Vite::getAssetUrl(), |
| 20 |
'trans' => TransStrings::getStrings(), |
| 21 |
'shop' => Helper::shopConfig(), |
| 22 |
]); |
| 23 |
} |
| 24 |
|
| 25 |
public function attachments(): \WP_REST_Response |
| 26 |
{ |
| 27 |
$query_images_args = array( |
| 28 |
'post_type' => 'attachment', |
| 29 |
'post_mime_type' => 'image', |
| 30 |
'post_status' => 'inherit', |
| 31 |
'posts_per_page' => -1, |
| 32 |
); |
| 33 |
|
| 34 |
$query_images = new \WP_Query($query_images_args); |
| 35 |
|
| 36 |
$image_list = []; |
| 37 |
foreach ($query_images->posts as $image) { |
| 38 |
$image_title = get_the_title($image->ID); |
| 39 |
$image_url = wp_get_attachment_url($image->ID); |
| 40 |
$image_list[] = array( |
| 41 |
'id' => $image->ID, |
| 42 |
'title' => $image_title, |
| 43 |
'url' => $image_url, |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
if (count($image_list)) { |
| 48 |
return $this->sendSuccess( |
| 49 |
[ |
| 50 |
'attachments' => $image_list |
| 51 |
] |
| 52 |
); |
| 53 |
} |
| 54 |
return $this->sendError( |
| 55 |
[ |
| 56 |
'message' => __('No Images Found', 'fluent-cart') |
| 57 |
] |
| 58 |
); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
public function uploadAttachments(Request $request) |
| 63 |
{ |
| 64 |
|
| 65 |
|
| 66 |
foreach ($request->files() as $file) { |
| 67 |
if ($file instanceof File) { |
| 68 |
$fileArray = $file->toArray(); |
| 69 |
$wp_filetype = wp_check_filetype_and_ext($fileArray['tmp_name'], $fileArray['name']); |
| 70 |
|
| 71 |
if (wp_match_mime_types('image', $wp_filetype['type'])) { |
| 72 |
|
| 73 |
if (!function_exists('media_handle_upload')) { |
| 74 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 75 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 76 |
require_once(ABSPATH . 'wp-admin/includes/media.php'); |
| 77 |
} |
| 78 |
|
| 79 |
$attachment_id = media_handle_upload('file', 0, []); |
| 80 |
|
| 81 |
if ($attachment_id instanceof \WP_Error) { |
| 82 |
return $this->sendError([ |
| 83 |
'error' => $attachment_id->get_error_messages() |
| 84 |
]); |
| 85 |
} |
| 86 |
return Arr::only( |
| 87 |
wp_prepare_attachment_for_js($attachment_id), |
| 88 |
['id', 'title', 'url'] |
| 89 |
); |
| 90 |
} |
| 91 |
return $this->sendError([ |
| 92 |
'error' => __('Error Uploading File', 'fluent-cart') |
| 93 |
]); |
| 94 |
|
| 95 |
|
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return $this->sendError(__('No File Attached', 'fluent-cart')); |
| 100 |
} |
| 101 |
} |