PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Http / Controllers / AppControllers / AppController.php

AppController.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Http/Controllers/AppControllers/AppController.php

101 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }