diff --git a/admin/settings.php b/admin/settings.php index d2f1939..3d60041 100644 --- a/admin/settings.php +++ b/admin/settings.php @@ -484,7 +484,7 @@ function wpa_superstar_settings_page() {
- +
@@ -758,7 +758,7 @@ function wpa_superstar_settings_page() { -
+
+

-
+
-
+
+

-
+
-
+
+

-
- +
+
Configure workflow automation settings to enhance your content management process.

'; +} + +// Add Auto Upload Images setting +function wpa_superstar_auto_upload_images_callback() { + $options = get_option('wpa_superstar_workflow_options', array( + 'auto_upload_images' => false + )); + ?> + +

+ Import images that have external URLs into your Media Library when saving. + Consider disabling during large data imports with many external image URLs. +

+ ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) + modified: admin/settings.php modified: git_status_output.txt -Untracked files: - (use "git add ..." to include in what will be committed) - git_status_temp.txt - no changes added to commit (use "git add" and/or "git commit -a") diff --git a/git_status_temp.txt b/git_status_temp.txt index b1efbe9..d165ab7 100644 --- a/git_status_temp.txt +++ b/git_status_temp.txt @@ -2,10 +2,8 @@ On branch main Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) + modified: admin/settings.php modified: git_status_output.txt - -Untracked files: - (use "git add ..." to include in what will be committed) - git_status_temp.txt + modified: git_status_temp.txt no changes added to commit (use "git add" and/or "git commit -a") diff --git a/includes/class-wpa-superstar-auto-upload.php b/includes/class-wpa-superstar-auto-upload.php new file mode 100644 index 0000000..22c90ab --- /dev/null +++ b/includes/class-wpa-superstar-auto-upload.php @@ -0,0 +1,126 @@ + false)); + if (!$options['auto_upload_images']) { + return $content; + } + + // Regular expression to find image URLs + $pattern = '/]+src=[\'"]([^\'"]+)[\'"][^>]*>/i'; + + return preg_replace_callback($pattern, array($this, 'process_image_url'), $content); + } + + /** + * Process individual image URL + * + * @param array $matches Regex matches + * @return string Updated img tag + */ + private function process_image_url($matches) { + if (empty($matches[1])) { + return $matches[0]; + } + + $url = $matches[1]; + + // Skip if already a local URL + if ($this->is_local_url($url)) { + return $matches[0]; + } + + try { + $local_url = $this->upload_image($url); + if ($local_url) { + return str_replace($url, $local_url, $matches[0]); + } + } catch (Exception $e) { + do_action('wpa_superstar_image_upload_error', $url, $e->getMessage()); + } + + return $matches[0]; + } + + /** + * Check if URL is local + * + * @param string $url URL to check + * @return boolean + */ + private function is_local_url($url) { + $site_url = parse_url(get_site_url(), PHP_URL_HOST); + $image_host = parse_url($url, PHP_URL_HOST); + return $site_url === $image_host; + } + + /** + * Upload external image to media library + * + * @param string $url External image URL + * @return string|false Local URL on success, false on failure + */ + private function upload_image($url) { + // Get file info + $file_array = array(); + $file_array['name'] = basename($url); + + // Download file to temp location + $file_array['tmp_name'] = download_url($url); + + if (is_wp_error($file_array['tmp_name'])) { + throw new Exception('Failed to download image: ' . $file_array['tmp_name']->get_error_message()); + } + + // Check file type + $wp_filetype = wp_check_filetype_and_ext($file_array['tmp_name'], $file_array['name']); + if (!$wp_filetype['type']) { + unlink($file_array['tmp_name']); + throw new Exception('Invalid file type'); + } + + // Upload the file + $attachment_id = media_handle_sideload($file_array, 0); + + if (is_wp_error($attachment_id)) { + throw new Exception('Failed to upload image: ' . $attachment_id->get_error_message()); + } + + return wp_get_attachment_url($attachment_id); + } + + /** + * Log errors to WordPress debug log + * + * @param string $url URL that failed + * @param string $error Error message + */ + public function log_error($url, $error) { + error_log(sprintf( + '[WPA Superstar] Auto Upload Images Error - URL: %s, Error: %s', + $url, + $error + )); + } +} \ No newline at end of file diff --git a/wpa-superstar-plugin.php b/wpa-superstar-plugin.php index d9d5bf8..369b746 100644 --- a/wpa-superstar-plugin.php +++ b/wpa-superstar-plugin.php @@ -30,6 +30,7 @@ register_activation_hook( __FILE__, 'wpa_superstar_activate' ); // Load core functionality require_once plugin_dir_path( __FILE__ ) . 'includes/speed-functions.php'; +require_once plugin_dir_path(__FILE__) . 'includes/class-wpa-superstar-auto-upload.php'; // Load admin UI if ( is_admin() ) { @@ -86,4 +87,7 @@ function wpa_superstar_lazy_load_assets() { ); } // Hook the function to appropriate WordPress action -add_action( 'wp_enqueue_scripts', 'wpa_superstar_lazy_load_assets' ); \ No newline at end of file +add_action( 'wp_enqueue_scripts', 'wpa_superstar_lazy_load_assets' ); + +// Initialize classes +$wpa_superstar_auto_upload = new WPA_Superstar_Auto_Upload(); \ No newline at end of file