Changeset 3371984
- Timestamp:
- 10/02/2025 08:19:08 PM (6 months ago)
- Location:
- extension-access-manager/trunk
- Files:
-
- 2 edited
-
extension-access-manager.php (modified) (3 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
extension-access-manager/trunk/extension-access-manager.php
r3317649 r3371984 3 3 Plugin Name: extension access manager 4 4 Description: A custom REST API interface for secure external image uploads and article posting. 5 Version: 1. 05 Version: 1.1 6 6 Author: Haider Mirza 7 7 License: GPLv2 or later … … 135 135 136 136 function exteacma_image_upload_handler($request) { 137 $img_url = $request->get_param('img'); 138 $base64 = $request->get_param('base64'); 139 $filename = sanitize_file_name($request->get_param('filename')); 137 $images = $request->get_param('images'); // ← array of URLs or base64 data 138 $results = []; 139 140 if (!is_array($images) || empty($images)) { 141 return new WP_Error('invalid_images', 'Images parameter must be a non-empty array.', ['status' => 400]); 142 } 140 143 141 144 require_once ABSPATH . 'wp-admin/includes/file.php'; … … 143 146 require_once ABSPATH . 'wp-admin/includes/image.php'; 144 147 145 if ($img_url && filter_var($img_url, FILTER_VALIDATE_URL)) { 146 $headers = wp_remote_head($img_url); 147 if (is_wp_error($headers)) { 148 return new WP_Error('no_headers', 'Could not get headers.', ['status' => 400]); 148 foreach ($images as $index => $image) { 149 $filename = sanitize_file_name($image['filename'] ?? 'image_' . time() . '_' . $index . '.jpg'); 150 $is_base64 = !empty($image['base64']); 151 $is_url = !empty($image['url']); 152 153 if ($is_base64) { 154 $decoded = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image['base64'])); 155 if (!$decoded) { 156 $results[] = ['index' => $index, 'success' => false, 'error' => 'Base64 decode failed']; 157 continue; 158 } 159 160 $tmp = wp_tempnam($filename); 161 if (!$tmp) { 162 $results[] = ['index' => $index, 'success' => false, 'error' => 'Temp file creation failed']; 163 continue; 164 } 165 166 global $wp_filesystem; 167 if (empty($wp_filesystem)) { 168 require_once ABSPATH . '/wp-admin/includes/file.php'; 169 WP_Filesystem(); 170 } 171 172 $written = $wp_filesystem->put_contents($tmp, $decoded, FS_CHMOD_FILE); 173 if (!$written) { 174 wp_delete_file($tmp); 175 $results[] = ['index' => $index, 'success' => false, 'error' => 'Write to temp failed']; 176 continue; 177 } 178 179 $file_array = ['name' => $filename, 'tmp_name' => $tmp]; 180 $id = media_handle_sideload($file_array, 0); 181 if (is_wp_error($id)) { 182 wp_delete_file($tmp); 183 $results[] = ['index' => $index, 'success' => false, 'error' => $id->get_error_message()]; 184 continue; 185 } 186 187 $results[] = ['index' => $index, 'success' => true, 'url' => wp_get_attachment_url($id)]; 149 188 } 150 189 151 $content_type = wp_remote_retrieve_header($headers, 'content-type'); 152 if (stripos($content_type, 'image/') !== 0) { 153 return new WP_Error('invalid_image', 'The URL does not point to an image.', ['status' => 400]); 190 elseif ($is_url) { 191 $url = $image['url']; 192 $tmp = exteacma_download_image($url); 193 if (is_wp_error($tmp)) { 194 $results[] = ['index' => $index, 'success' => false, 'error' => $tmp->get_error_message()]; 195 continue; 196 } 197 198 $file_array = ['name' => $filename, 'tmp_name' => $tmp]; 199 $id = media_handle_sideload($file_array, 0); 200 if (is_wp_error($id)) { 201 wp_delete_file($tmp); 202 $results[] = ['index' => $index, 'success' => false, 'error' => $id->get_error_message()]; 203 continue; 204 } 205 206 $results[] = ['index' => $index, 'success' => true, 'url' => wp_get_attachment_url($id)]; 154 207 } 155 208 156 $tmp = exteacma_download_image($img_url); 157 if (is_wp_error($tmp)) { 158 return $tmp; 209 else { 210 $results[] = ['index' => $index, 'success' => false, 'error' => 'Invalid image format']; 159 211 } 160 161 $name = basename(wp_parse_url($img_url, PHP_URL_PATH)); 162 $file_array = ['name' => $name, 'tmp_name' => $tmp]; 163 164 $id = media_handle_sideload($file_array, 0); 165 if (is_wp_error($id)) { 166 wp_delete_file($tmp); 167 return new WP_Error('upload_error', $id->get_error_message(), ['status' => 500]); 168 } 169 170 return ['success' => true, 'url' => wp_get_attachment_url($id)]; 171 } 172 173 if ($base64 && $filename) { 174 $decoded = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64)); 175 if (!$decoded) { 176 return new WP_Error('decode_error', 'Base64 decode failed', ['status' => 400]); 177 } 178 179 $tmp = wp_tempnam($filename); 180 if (!$tmp) { 181 return new WP_Error('temp_file_error', 'Could not create temp file.', ['status' => 500]); 182 } 183 184 global $wp_filesystem; 185 if (empty($wp_filesystem)) { 186 require_once ABSPATH . '/wp-admin/includes/file.php'; 187 WP_Filesystem(); 188 } 189 190 $written = $wp_filesystem->put_contents($tmp, $decoded, FS_CHMOD_FILE); 191 if (!$written) { 192 wp_delete_file($tmp); 193 return new WP_Error('write_failed', 'Failed to write image to temp file.', ['status' => 500]); 194 } 195 196 $file_array = ['name' => $filename, 'tmp_name' => $tmp]; 197 $id = media_handle_sideload($file_array, 0); 198 if (is_wp_error($id)) { 199 wp_delete_file($tmp); 200 return new WP_Error('upload_error', $id->get_error_message(), ['status' => 500]); 201 } 202 203 return ['success' => true, 'url' => wp_get_attachment_url($id)]; 204 } 205 206 return new WP_Error('invalid_request', 'Invalid image data.', ['status' => 400]); 207 } 212 } 213 214 return ['results' => $results]; 215 } 216 208 217 209 218 function exteacma_download_image($url) { -
extension-access-manager/trunk/readme.txt
r3317649 r3371984 5 5 Tested up to: 6.8 6 6 Requires PHP: 7.4 7 Stable tag: 1. 07 Stable tag: 1.1 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 43 43 44 44 == Changelog == 45 46 = 1.0 = 47 * Initial release with image upload and post creation support. 45 = 1.1 = 46 * Added REST route /import-categories to import scraped categories. 47 * Improved image handling and .webp/base64 upload fallback. 48 * Fixed minor bugs. 48 49 49 50 == Upgrade Notice == 50 51 51 = 1. 0=52 = 1.1 = 52 53 First release – stable and secure for basic REST integration. 53 54
Note: See TracChangeset
for help on using the changeset viewer.