- Added image optimization with WebP conversion
- Updated Dashboard UI with card layout
- Fixed duplicate domain/email detection
- Increased default items per page to 25
- Added campaign weekend reduction factor field
- Added ember animation effect in footer
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
136 lines
No EOL
4 KiB
PHP
136 lines
No EOL
4 KiB
PHP
<?php
|
|
|
|
/* Add Dashicons in WordPress Front-end */
|
|
add_action('wp_enqueue_scripts', 'load_dashicons_front_end');
|
|
function load_dashicons_front_end()
|
|
{
|
|
wp_enqueue_style('dashicons');
|
|
}
|
|
|
|
/**
|
|
* Convert uploaded images to WebP with adjustable image compression quality
|
|
*/
|
|
|
|
// Register the settings on the Media Settings page
|
|
function add_optimize_images_settings()
|
|
{
|
|
register_setting('media', 'optimize_images');
|
|
register_setting('media', 'image_compression_quality');
|
|
|
|
add_settings_field(
|
|
'optimize_images',
|
|
__('Optimize Images', 'textdomain'),
|
|
'display_optimize_images_checkbox',
|
|
'media',
|
|
'default'
|
|
);
|
|
|
|
add_settings_field(
|
|
'image_compression_quality',
|
|
__('Image Compression Quality', 'textdomain'),
|
|
'display_image_compression_quality_input',
|
|
'media',
|
|
'default'
|
|
);
|
|
}
|
|
|
|
function display_optimize_images_checkbox()
|
|
{
|
|
$option = get_option('optimize_images');
|
|
?>
|
|
<input type="checkbox" name="optimize_images" value="1" <?php checked(1, $option, true); ?> />
|
|
<label for="optimize_images"><?php esc_html_e('Convert images to WebP format upon upload.', 'textdomain'); ?></label>
|
|
<?php
|
|
}
|
|
|
|
function display_image_compression_quality_input()
|
|
{
|
|
$quality = get_option('image_compression_quality', 80);
|
|
?>
|
|
<input type="number" name="image_compression_quality"
|
|
value="<?php echo esc_attr($quality); ?>" min="1" max="100" />
|
|
<label for="image_compression_quality"><?php esc_html_e('Set compression quality for WebP images (1-100).', 'textdomain'); ?></label>
|
|
<?php
|
|
}
|
|
|
|
add_action('admin_init', 'add_optimize_images_settings');
|
|
|
|
function convert_images_to_webp($file)
|
|
{
|
|
$optimize_images = get_option('optimize_images');
|
|
if (!$optimize_images) {
|
|
return $file;
|
|
}
|
|
|
|
if (!class_exists('Imagick')) {
|
|
return $file;
|
|
}
|
|
|
|
$supported_types = ['image/jpeg', 'image/jpg', 'image/png'];
|
|
|
|
if (!in_array($file['type'], $supported_types)) {
|
|
return $file;
|
|
}
|
|
|
|
$wp_upload_dir = wp_upload_dir();
|
|
$original_file_path = $file['file'];
|
|
$file_name = pathinfo($original_file_path, PATHINFO_FILENAME);
|
|
$webp_file_path = $wp_upload_dir['path'] . '/' . $file_name . '.webp';
|
|
|
|
if (pathinfo($original_file_path, PATHINFO_EXTENSION) === 'webp') {
|
|
return $file;
|
|
}
|
|
|
|
$compression_quality = get_option('image_compression_quality', 75);
|
|
|
|
try {
|
|
$image = new Imagick($original_file_path);
|
|
|
|
$hasTransparency = $image->getImageAlphaChannel();
|
|
if ($hasTransparency) {
|
|
return $file;
|
|
}
|
|
|
|
$image->setImageFormat('webp');
|
|
$image->setImageCompressionQuality($compression_quality);
|
|
$image->stripImage();
|
|
$image->writeImage($webp_file_path);
|
|
$image->clear();
|
|
$image->destroy();
|
|
} catch (Exception $e) {
|
|
return $file;
|
|
}
|
|
|
|
$backup_dir = $wp_upload_dir['path'] . '/backup';
|
|
if (!file_exists($backup_dir)) {
|
|
mkdir($backup_dir, 0755, true);
|
|
}
|
|
|
|
$backup_file_path = $backup_dir . '/' . basename($original_file_path);
|
|
rename($original_file_path, $backup_file_path);
|
|
|
|
$metadata = wp_generate_attachment_metadata(attachment_url_to_postid($file['url']), $webp_file_path);
|
|
wp_update_attachment_metadata(attachment_url_to_postid($file['url']), $metadata);
|
|
|
|
return [
|
|
'file' => $webp_file_path,
|
|
'url' => $wp_upload_dir['url'] . '/' . basename($webp_file_path),
|
|
'type' => 'image/webp',
|
|
];
|
|
}
|
|
|
|
add_filter('wp_handle_upload', 'convert_images_to_webp');
|
|
|
|
|
|
|
|
|
|
// function rl_before_update( $upgrader_object, $options ) {
|
|
// log_to_file("rl_before_update - Options: ", $options);
|
|
// }
|
|
|
|
// function rl_after_update( $upgrader_object, $options ) {
|
|
// log_to_file("rl_after_update - Options: ", $options);
|
|
// }
|
|
|
|
// add_action( 'upgrader_pre_install', 'rl_before_update', 10, 2 );
|
|
// add_action( 'upgrader_process_complete', 'rl_after_update', 10, 2 );
|