Add Workflow tab with Auto Upload Images feature: - Add new Workflow settings tab - Implement auto image upload functionality - Add error logging and handling - Add file type validation
This commit is contained in:
@ -484,7 +484,7 @@ function wpa_superstar_settings_page() {
|
|||||||
|
|
||||||
<div class="wpa-settings-container">
|
<div class="wpa-settings-container">
|
||||||
<div class="wpa-superstar-nav">
|
<div class="wpa-superstar-nav">
|
||||||
<h2 class="nav-tab-wrapper">
|
<h2 class="nav-tab-wrapper">
|
||||||
<a href="?page=wpa-superstar&tab=general" class="nav-tab <?php echo $active_tab == 'general' ? 'nav-tab-active' : ''; ?>">
|
<a href="?page=wpa-superstar&tab=general" class="nav-tab <?php echo $active_tab == 'general' ? 'nav-tab-active' : ''; ?>">
|
||||||
<?php esc_html_e('General', 'wpa-superstar'); ?>
|
<?php esc_html_e('General', 'wpa-superstar'); ?>
|
||||||
</a>
|
</a>
|
||||||
@ -497,7 +497,7 @@ function wpa_superstar_settings_page() {
|
|||||||
<a href="?page=wpa-superstar&tab=theme" class="nav-tab <?php echo $active_tab == 'theme' ? 'nav-tab-active' : ''; ?>">
|
<a href="?page=wpa-superstar&tab=theme" class="nav-tab <?php echo $active_tab == 'theme' ? 'nav-tab-active' : ''; ?>">
|
||||||
<?php esc_html_e('Theme', 'wpa-superstar'); ?>
|
<?php esc_html_e('Theme', 'wpa-superstar'); ?>
|
||||||
</a>
|
</a>
|
||||||
</h2>
|
</h2>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="wpa-settings-content">
|
<div class="wpa-settings-content">
|
||||||
@ -758,7 +758,7 @@ function wpa_superstar_settings_page() {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<?php elseif ($active_tab == 'general'): ?>
|
<?php elseif ($active_tab == 'general'): ?>
|
||||||
<div class="wpa-superstar-toggle">
|
<div class="wpa-superstar-toggle">
|
||||||
<label for="wpa_superstar_lazy_load">
|
<label for="wpa_superstar_lazy_load">
|
||||||
<div class="wpa-toggle-switch">
|
<div class="wpa-toggle-switch">
|
||||||
<input type="checkbox"
|
<input type="checkbox"
|
||||||
@ -770,14 +770,14 @@ function wpa_superstar_settings_page() {
|
|||||||
<span class="wpa-toggle-slider"></span>
|
<span class="wpa-toggle-slider"></span>
|
||||||
</div>
|
</div>
|
||||||
<?php esc_html_e('Enable lazy loading for images', 'wpa-superstar'); ?>
|
<?php esc_html_e('Enable lazy loading for images', 'wpa-superstar'); ?>
|
||||||
</label>
|
</label>
|
||||||
<p class="description">
|
<p class="description">
|
||||||
<?php esc_html_e('Improves page load time by loading images only when they enter the viewport.', 'wpa-superstar'); ?>
|
<?php esc_html_e('Improves page load time by loading images only when they enter the viewport.', 'wpa-superstar'); ?>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<?php elseif ($active_tab == 'advanced'): ?>
|
<?php elseif ($active_tab == 'advanced'): ?>
|
||||||
<div class="wpa-superstar-toggle">
|
<div class="wpa-superstar-toggle">
|
||||||
<label for="wpa_superstar_minify_css">
|
<label for="wpa_superstar_minify_css">
|
||||||
<div class="wpa-toggle-switch">
|
<div class="wpa-toggle-switch">
|
||||||
<input type="checkbox"
|
<input type="checkbox"
|
||||||
@ -789,13 +789,13 @@ function wpa_superstar_settings_page() {
|
|||||||
<span class="wpa-toggle-slider"></span>
|
<span class="wpa-toggle-slider"></span>
|
||||||
</div>
|
</div>
|
||||||
<?php esc_html_e('Enable CSS minification', 'wpa-superstar'); ?>
|
<?php esc_html_e('Enable CSS minification', 'wpa-superstar'); ?>
|
||||||
</label>
|
</label>
|
||||||
<p class="description">
|
<p class="description">
|
||||||
<?php esc_html_e('Minifies CSS files to reduce file size and improve load times.', 'wpa-superstar'); ?>
|
<?php esc_html_e('Minifies CSS files to reduce file size and improve load times.', 'wpa-superstar'); ?>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="wpa-superstar-toggle">
|
<div class="wpa-superstar-toggle">
|
||||||
<label for="wpa_superstar_minify_js">
|
<label for="wpa_superstar_minify_js">
|
||||||
<div class="wpa-toggle-switch">
|
<div class="wpa-toggle-switch">
|
||||||
<input type="checkbox"
|
<input type="checkbox"
|
||||||
@ -807,14 +807,63 @@ function wpa_superstar_settings_page() {
|
|||||||
<span class="wpa-toggle-slider"></span>
|
<span class="wpa-toggle-slider"></span>
|
||||||
</div>
|
</div>
|
||||||
<?php esc_html_e('Enable JS minification', 'wpa-superstar'); ?>
|
<?php esc_html_e('Enable JS minification', 'wpa-superstar'); ?>
|
||||||
</label>
|
</label>
|
||||||
<p class="description">
|
<p class="description">
|
||||||
<?php esc_html_e('Minifies JavaScript files to reduce file size and improve load times.', 'wpa-superstar'); ?>
|
<?php esc_html_e('Minifies JavaScript files to reduce file size and improve load times.', 'wpa-superstar'); ?>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register Workflow settings
|
||||||
|
function wpa_superstar_workflow_section_callback() {
|
||||||
|
echo '<p>Configure workflow automation settings to enhance your content management process.</p>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add Auto Upload Images setting
|
||||||
|
function wpa_superstar_auto_upload_images_callback() {
|
||||||
|
$options = get_option('wpa_superstar_workflow_options', array(
|
||||||
|
'auto_upload_images' => false
|
||||||
|
));
|
||||||
|
?>
|
||||||
|
<label class="switch">
|
||||||
|
<input type="checkbox" name="wpa_superstar_workflow_options[auto_upload_images]"
|
||||||
|
<?php checked($options['auto_upload_images'], true); ?>>
|
||||||
|
<span class="slider round"></span>
|
||||||
|
</label>
|
||||||
|
<p class="description">
|
||||||
|
Import images that have external URLs into your Media Library when saving.
|
||||||
|
Consider disabling during large data imports with many external image URLs.
|
||||||
|
</p>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add Workflow section
|
||||||
|
function wpa_superstar_workflow_section() {
|
||||||
|
// Register Workflow settings
|
||||||
|
register_setting('wpa_superstar_workflow', 'wpa_superstar_workflow_options');
|
||||||
|
|
||||||
|
// Add Workflow section
|
||||||
|
add_settings_section(
|
||||||
|
'wpa_superstar_workflow_section',
|
||||||
|
'Workflow Settings',
|
||||||
|
'wpa_superstar_workflow_section_callback',
|
||||||
|
'wpa_superstar_workflow'
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add Auto Upload Images setting
|
||||||
|
add_settings_field(
|
||||||
|
'auto_upload_images',
|
||||||
|
'Enable Auto Upload Images',
|
||||||
|
'wpa_superstar_auto_upload_images_callback',
|
||||||
|
'wpa_superstar_workflow',
|
||||||
|
'wpa_superstar_workflow_section'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add Workflow section
|
||||||
|
add_action('admin_init', 'wpa_superstar_workflow_section');
|
@ -2,10 +2,7 @@ On branch main
|
|||||||
Changes not staged for commit:
|
Changes not staged for commit:
|
||||||
(use "git add <file>..." to update what will be committed)
|
(use "git add <file>..." to update what will be committed)
|
||||||
(use "git restore <file>..." to discard changes in working directory)
|
(use "git restore <file>..." to discard changes in working directory)
|
||||||
|
modified: admin/settings.php
|
||||||
modified: git_status_output.txt
|
modified: git_status_output.txt
|
||||||
|
|
||||||
Untracked files:
|
|
||||||
(use "git add <file>..." to include in what will be committed)
|
|
||||||
git_status_temp.txt
|
|
||||||
|
|
||||||
no changes added to commit (use "git add" and/or "git commit -a")
|
no changes added to commit (use "git add" and/or "git commit -a")
|
||||||
|
@ -2,10 +2,8 @@ On branch main
|
|||||||
Changes not staged for commit:
|
Changes not staged for commit:
|
||||||
(use "git add <file>..." to update what will be committed)
|
(use "git add <file>..." to update what will be committed)
|
||||||
(use "git restore <file>..." to discard changes in working directory)
|
(use "git restore <file>..." to discard changes in working directory)
|
||||||
|
modified: admin/settings.php
|
||||||
modified: git_status_output.txt
|
modified: git_status_output.txt
|
||||||
|
modified: git_status_temp.txt
|
||||||
Untracked files:
|
|
||||||
(use "git add <file>..." to include in what will be committed)
|
|
||||||
git_status_temp.txt
|
|
||||||
|
|
||||||
no changes added to commit (use "git add" and/or "git commit -a")
|
no changes added to commit (use "git add" and/or "git commit -a")
|
||||||
|
126
includes/class-wpa-superstar-auto-upload.php
Normal file
126
includes/class-wpa-superstar-auto-upload.php
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Auto Upload Images functionality
|
||||||
|
*
|
||||||
|
* @package WPA_Superstar
|
||||||
|
*/
|
||||||
|
|
||||||
|
class WPA_Superstar_Auto_Upload {
|
||||||
|
/**
|
||||||
|
* Initialize the class
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
add_filter('content_save_pre', array($this, 'process_content'), 10, 1);
|
||||||
|
add_action('wpa_superstar_image_upload_error', array($this, 'log_error'), 10, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process content for external images
|
||||||
|
*
|
||||||
|
* @param string $content The post content
|
||||||
|
* @return string Modified content with local image URLs
|
||||||
|
*/
|
||||||
|
public function process_content($content) {
|
||||||
|
// Check if auto upload is enabled
|
||||||
|
$options = get_option('wpa_superstar_workflow_options', array('auto_upload_images' => false));
|
||||||
|
if (!$options['auto_upload_images']) {
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regular expression to find image URLs
|
||||||
|
$pattern = '/<img[^>]+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
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
@ -30,6 +30,7 @@ register_activation_hook( __FILE__, 'wpa_superstar_activate' );
|
|||||||
|
|
||||||
// Load core functionality
|
// Load core functionality
|
||||||
require_once plugin_dir_path( __FILE__ ) . 'includes/speed-functions.php';
|
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
|
// Load admin UI
|
||||||
if ( is_admin() ) {
|
if ( is_admin() ) {
|
||||||
@ -87,3 +88,6 @@ function wpa_superstar_lazy_load_assets() {
|
|||||||
}
|
}
|
||||||
// Hook the function to appropriate WordPress action
|
// Hook the function to appropriate WordPress action
|
||||||
add_action( 'wp_enqueue_scripts', 'wpa_superstar_lazy_load_assets' );
|
add_action( 'wp_enqueue_scripts', 'wpa_superstar_lazy_load_assets' );
|
||||||
|
|
||||||
|
// Initialize classes
|
||||||
|
$wpa_superstar_auto_upload = new WPA_Superstar_Auto_Upload();
|
Reference in New Issue
Block a user