PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.91.6
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.91.6
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Support / MediaUploader.php

MediaUploader.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.91.6, at vendor/wpfluent/framework/src/WPFluent/Support/MediaUploader.php

102 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 FluentBoards\Framework\Support;
4
5 use RuntimeException;
6 use InvalidArgumentException;
7
8 class MediaUploader
9 {
10 /**
11 * upload a local file array (e.g. from $_FILES) or a remote file URL.
12 *
13 * @param array|string $resource File array from $_FILES or remote file URL
14 * @param int|null $postId Optional post ID to attach the media to
15 * @param string|null $filename Optional file name for remote URLs
16 *
17 * @return array
18 * @throws InvalidArgumentException
19 * @throws RuntimeException
20 * @see https://developer.wordpress.org/reference/functions/media_handle_sideload/
21 */
22 public static function upload($resource, $postId = 0, $filename = null)
23 {
24 [$fileArray, $tmp] = static::prepareMedia($resource, $filename);
25
26 $attachmentId = media_handle_sideload($fileArray, $postId);
27
28 if ($tmp && file_exists($tmp)) {
29 @unlink($tmp);
30 }
31
32 if (is_wp_error($attachmentId)) {
33 throw new RuntimeException(
34 'Failed to sideload media: ' . $attachmentId->get_error_message()
35 );
36 }
37
38 return [
39 'success' => true,
40 'id' => $attachmentId,
41 'url' => wp_get_attachment_url($attachmentId),
42 ];
43 }
44
45 /**
46 * Prepare the file array to upload.
47 *
48 * @param string|array $resource url or $_FILES
49 * @param string|null $filename
50 * @return array
51 */
52 protected static function prepareMedia($resource, $filename = null)
53 {
54 static::includeMediaFunctions();
55
56 if (is_array($resource)) {
57 if (empty($resource['tmp_name'])) {
58 throw new InvalidArgumentException('Invalid file array: tmp_name is required.');
59 }
60
61 return [[
62 'name' => $resource['name'] ?? 'file',
63 'tmp_name' => $resource['tmp_name'],
64 'type' => $resource['type'] ?? null,
65 'error' => $resource['error'] ?? 0,
66 'size' => $resource['size_in_bytes'] ?? $resource['size'] ?? null,
67 ], null];
68 }
69
70 if (is_string($resource) && filter_var($resource, FILTER_VALIDATE_URL)) {
71 $tmp = download_url($resource);
72
73 if (is_wp_error($tmp)) {
74 throw new RuntimeException(
75 'Failed to download remote file: ' . $tmp->get_error_message()
76 );
77 }
78
79 return [[
80 'tmp_name' => $tmp,
81 'name' => $filename ?: basename(
82 parse_url($resource, PHP_URL_PATH)
83 ),
84 ], $tmp];
85 }
86
87 throw new InvalidArgumentException('Invalid file array or URL provided.');
88 }
89
90 /**
91 * Ensure required WordPress media functions are loaded.
92 */
93 protected static function includeMediaFunctions()
94 {
95 if (!function_exists('media_handle_sideload')) {
96 require_once ABSPATH . 'wp-admin/includes/image.php';
97 require_once ABSPATH . 'wp-admin/includes/file.php';
98 require_once ABSPATH . 'wp-admin/includes/media.php';
99 }
100 }
101 }
102