imsanity_ms ) ) {
$wpdb->imsanity_ms = $wpdb->get_blog_prefix( 0 ) . 'imsanity';
}
// Register the plugin settings menu.
add_action( 'admin_menu', 'imsanity_create_menu' );
add_action( 'network_admin_menu', 'imsanity_register_network' );
add_filter( 'plugin_action_links_' . IMSANITY_PLUGIN_FILE_REL, 'imsanity_settings_link' );
add_filter( 'network_admin_plugin_action_links_' . IMSANITY_PLUGIN_FILE_REL, 'imsanity_settings_link' );
add_action( 'admin_enqueue_scripts', 'imsanity_queue_script' );
add_action( 'admin_init', 'imsanity_register_settings' );
add_filter( 'big_image_size_threshold', '__return_false' );
register_activation_hook( IMSANITY_PLUGIN_FILE_REL, 'imsanity_maybe_created_custom_table' );
// settings cache.
$_imsanity_multisite_settings = null;
/**
* Create the settings menu item in the WordPress admin navigation and
* link it to the plugin settings page
*/
function imsanity_create_menu() {
$permissions = apply_filters( 'imsanity_admin_permissions', 'manage_options' );
// Create new menu for site configuration.
add_options_page(
esc_html__( 'Imsanity Plugin Settings', 'imsanity' ), // Page Title.
esc_html__( 'Imsanity', 'imsanity' ), // Menu Title.
$permissions, // Required permissions.
'imsanity-options', // Slug.
'imsanity_settings_page' // Function to call.
);
}
/**
* Register the network settings page
*/
function imsanity_register_network() {
if ( ! function_exists( 'is_plugin_active_for_network' ) && is_multisite() ) {
// Need to include the plugin library for the is_plugin_active function.
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( is_multisite() ) {
$permissions = apply_filters( 'imsanity_superadmin_permissions', 'manage_network_options' );
add_submenu_page(
'settings.php',
esc_html__( 'Imsanity Network Settings', 'imsanity' ),
esc_html__( 'Imsanity', 'imsanity' ),
$permissions,
'imsanity-options',
'imsanity_network_settings'
);
}
}
/**
* Get the settings link, based on whether we are in a multi-site network admin or not.
*
* @return string The URL for the settings page.
*/
function imsanity_get_settings_link() {
if ( is_multisite() && is_network_admin() ) {
return network_admin_url( 'settings.php?page=imsanity-options' );
}
return admin_url( 'options-general.php?page=imsanity-options' );
}
/**
* Settings link that appears on the plugins overview page
*
* @param array $links The plugin action links.
* @return array The action links, with a settings link pre-pended.
*/
function imsanity_settings_link( $links ) {
if ( ! is_array( $links ) ) {
$links = array();
}
if ( is_multisite() && is_network_admin() ) {
$settings_link = '' . esc_html__( 'Settings', 'imsanity' ) . ' ';
} else {
$settings_link = '' . esc_html__( 'Settings', 'imsanity' ) . ' ';
}
array_unshift( $links, $settings_link );
return $links;
}
/**
* Queues up the AJAX script and any localized JS vars we need.
*
* @param string $hook The hook name for the current page.
*/
function imsanity_queue_script( $hook ) {
// Make sure we are being called from the settings page.
if ( strpos( $hook, 'settings_page_imsanity' ) !== 0 && 'upload.php' !== $hook ) {
return;
}
if ( ! empty( $_REQUEST['imsanity_reset'] ) && ! empty( $_REQUEST['imsanity_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['imsanity_wpnonce'] ), 'imsanity-bulk-reset' ) ) {
update_option( 'imsanity_resume_id', 0, false );
}
$resume_id = (int) get_option( 'imsanity_resume_id' );
$loading_image = plugins_url( '/images/ajax-loader.gif', __FILE__ );
// Register the scripts that are used by the bulk resizer.
wp_enqueue_script( 'imsanity_script', plugins_url( '/scripts/imsanity.js', __FILE__ ), array( 'jquery' ), IMSANITY_VERSION );
wp_localize_script(
'imsanity_script',
'imsanity_vars',
array(
'_wpnonce' => wp_create_nonce( 'imsanity-bulk' ),
'resize_all_prompt' => esc_html__( 'You are about to resize all your existing images. Please be sure your site is backed up before proceeding. Do you wish to continue?', 'imsanity' ),
'resizing_complete' => esc_html__( 'Resizing Complete', 'imsanity' ) . ' - ' . esc_html__( 'Leave a Review', 'imsanity' ) . ' ',
'resize_selected' => esc_html__( 'Resize Selected Images', 'imsanity' ),
'resizing' => '
' . esc_html__( 'Please wait...', 'imsanity' ) . "
",
'removal_failed' => esc_html__( 'Removal Failed', 'imsanity' ),
'removal_succeeded' => esc_html__( 'Removal Complete', 'imsanity' ),
'operation_stopped' => esc_html__( 'Resizing stopped, reload page to resume.', 'imsanity' ),
'image' => esc_html__( 'Image', 'imsanity' ),
'invalid_response' => esc_html__( 'Received an invalid response, please check for errors in the Developer Tools console of your browser.', 'imsanity' ),
'none_found' => esc_html__( 'There are no images that need to be resized.', 'imsanity' ),
'resume_id' => $resume_id,
)
);
add_action( 'admin_notices', 'imsanity_missing_gd_admin_notice' );
add_action( 'network_admin_notices', 'imsanity_missing_gd_admin_notice' );
add_action( 'admin_print_scripts', 'imsanity_settings_css' );
}
/**
* Return true if the multi-site settings table exists
*
* @return bool True if the Imsanity table exists.
*/
function imsanity_multisite_table_exists() {
global $wpdb;
return $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->imsanity_ms'" ) === $wpdb->imsanity_ms;
}
/**
* Checks the schema version for the Imsanity table.
*
* @return string The version identifier for the schema.
*/
function imsanity_multisite_table_schema_version() {
// If the table doesn't exist then there is no schema to report.
if ( ! imsanity_multisite_table_exists() ) {
return '0';
}
global $wpdb;
$version = $wpdb->get_var( "SELECT data FROM $wpdb->imsanity_ms WHERE setting = 'schema'" );
if ( ! $version ) {
$version = '1.0'; // This is a legacy version 1.0 installation.
}
return $version;
}
/**
* Returns the default network settings in the case where they are not
* defined in the database, or multi-site is not enabled.
*
* @return stdClass
*/
function imsanity_get_default_multisite_settings() {
$data = new stdClass();
if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) {
imsanity_init();
}
$data->imsanity_override_site = false;
$data->imsanity_max_height = IMSANITY_DEFAULT_MAX_HEIGHT;
$data->imsanity_max_width = IMSANITY_DEFAULT_MAX_WIDTH;
$data->imsanity_max_height_library = IMSANITY_DEFAULT_MAX_HEIGHT;
$data->imsanity_max_width_library = IMSANITY_DEFAULT_MAX_WIDTH;
$data->imsanity_max_height_other = IMSANITY_DEFAULT_MAX_HEIGHT;
$data->imsanity_max_width_other = IMSANITY_DEFAULT_MAX_WIDTH;
$data->imsanity_bmp_to_jpg = IMSANITY_DEFAULT_BMP_TO_JPG;
$data->imsanity_png_to_jpg = IMSANITY_DEFAULT_PNG_TO_JPG;
$data->imsanity_quality = IMSANITY_DEFAULT_QUALITY;
$data->imsanity_avif_quality = IMSANITY_DEFAULT_AVIF_QUALITY;
$data->imsanity_webp_quality = IMSANITY_DEFAULT_WEBP_QUALITY;
$data->imsanity_delete_originals = false;
return $data;
}
/**
* On activation create the multisite database table if necessary. this is
* called when the plugin is activated as well as when it is automatically
* updated.
*/
function imsanity_maybe_created_custom_table() {
// If not a multi-site no need to do any custom table lookups.
if ( ! function_exists( 'is_multisite' ) || ( ! is_multisite() ) ) {
return;
}
global $wpdb;
$schema = imsanity_multisite_table_schema_version();
if ( '0' === $schema ) {
// This is an initial database setup.
$sql = 'CREATE TABLE IF NOT EXISTS ' . $wpdb->imsanity_ms . ' (
setting varchar(55),
data text NOT NULL,
PRIMARY KEY (setting)
);';
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
// Add the rows to the database.
$data = imsanity_get_default_multisite_settings();
$wpdb->insert(
$wpdb->imsanity_ms,
array(
'setting' => 'multisite',
'data' => maybe_serialize( $data ),
)
);
$wpdb->insert(
$wpdb->imsanity_ms,
array(
'setting' => 'schema',
'data' => IMSANITY_SCHEMA_VERSION,
)
);
}
if ( IMSANITY_SCHEMA_VERSION !== $schema ) {
// This is a schema update. for the moment there is only one schema update available, from 1.0 to 1.1.
if ( '1.0' === $schema ) {
// Update from version 1.0 to 1.1.
$wpdb->insert(
$wpdb->imsanity_ms,
array(
'setting' => 'schema',
'data' => IMSANITY_SCHEMA_VERSION,
)
);
$wpdb->query( "ALTER TABLE $wpdb->imsanity_ms CHANGE COLUMN data data TEXT NOT NULL;" );
} else {
// @todo we don't have this yet
$wpdb->update(
$wpdb->imsanity_ms,
array( 'data' => IMSANITY_SCHEMA_VERSION ),
array( 'setting' => 'schema' )
);
}
}
}
/**
* Display the form for the multi-site settings page.
*/
function imsanity_network_settings() {
$settings = imsanity_get_multisite_settings(); ?>
imsanity_override_site = isset( $_POST['imsanity_override_site'] ) ? (bool) $_POST['imsanity_override_site'] : false;
$data->imsanity_max_height = isset( $_POST['imsanity_max_height'] ) ? (int) $_POST['imsanity_max_height'] : 0;
$data->imsanity_max_width = isset( $_POST['imsanity_max_width'] ) ? (int) $_POST['imsanity_max_width'] : 0;
$data->imsanity_max_height_library = isset( $_POST['imsanity_max_height_library'] ) ? (int) $_POST['imsanity_max_height_library'] : 0;
$data->imsanity_max_width_library = isset( $_POST['imsanity_max_width_library'] ) ? (int) $_POST['imsanity_max_width_library'] : 0;
$data->imsanity_max_height_other = isset( $_POST['imsanity_max_height_other'] ) ? (int) $_POST['imsanity_max_height_other'] : 0;
$data->imsanity_max_width_other = isset( $_POST['imsanity_max_width_other'] ) ? (int) $_POST['imsanity_max_width_other'] : 0;
$data->imsanity_bmp_to_jpg = ! empty( $_POST['imsanity_bmp_to_jpg'] );
$data->imsanity_png_to_jpg = ! empty( $_POST['imsanity_png_to_jpg'] );
$data->imsanity_quality = isset( $_POST['imsanity_quality'] ) ? imsanity_jpg_quality( intval( $_POST['imsanity_quality'] ) ) : 82;
$data->imsanity_avif_quality = isset( $_POST['imsanity_avif_quality'] ) ? imsanity_avif_quality( intval( $_POST['imsanity_avif_quality'] ) ) : 86;
$data->imsanity_webp_quality = isset( $_POST['imsanity_webp_quality'] ) ? imsanity_webp_quality( intval( $_POST['imsanity_webp_quality'] ) ) : 86;
$data->imsanity_delete_originals = ! empty( $_POST['imsanity_delete_originals'] );
$wpdb->update(
$wpdb->imsanity_ms,
array( 'data' => maybe_serialize( $data ) ),
array( 'setting' => 'multisite' )
);
// Clear the cache.
$_imsanity_multisite_settings = null;
add_action( 'network_admin_notices', 'imsanity_network_settings_saved' );
}
/**
* Display a message to inform the user the multi-site setting have been saved.
*/
function imsanity_network_settings_saved() {
echo "" . esc_html__( 'Imsanity network settings saved.', 'imsanity' ) . '
';
}
/**
* Return the multi-site settings as a standard class. If the settings are not
* defined in the database or multi-site is not enabled then the default settings
* are returned. This is cached so it only loads once per page load, unless
* imsanity_network_settings_update is called.
*
* @return stdClass
*/
function imsanity_get_multisite_settings() {
global $_imsanity_multisite_settings;
$result = null;
if ( ! $_imsanity_multisite_settings ) {
if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) {
imsanity_init();
}
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
global $wpdb;
$result = $wpdb->get_var( "SELECT data FROM $wpdb->imsanity_ms WHERE setting = 'multisite'" );
}
// if there's no results, return the defaults instead.
$_imsanity_multisite_settings = $result
? unserialize( $result )
: imsanity_get_default_multisite_settings();
// this is for backwards compatibility.
if ( ! isset( $_imsanity_multisite_settings->imsanity_max_height_library ) ) {
$_imsanity_multisite_settings->imsanity_max_height_library = $_imsanity_multisite_settings->imsanity_max_height;
$_imsanity_multisite_settings->imsanity_max_width_library = $_imsanity_multisite_settings->imsanity_max_width;
$_imsanity_multisite_settings->imsanity_max_height_other = $_imsanity_multisite_settings->imsanity_max_height;
$_imsanity_multisite_settings->imsanity_max_width_other = $_imsanity_multisite_settings->imsanity_max_width;
}
$_imsanity_multisite_settings->imsanity_override_site = ! empty( $_imsanity_multisite_settings->imsanity_override_site ) ? '1' : '0';
$_imsanity_multisite_settings->imsanity_bmp_to_jpg = ! empty( $_imsanity_multisite_settings->imsanity_bmp_to_jpg ) ? true : false;
$_imsanity_multisite_settings->imsanity_png_to_jpg = ! empty( $_imsanity_multisite_settings->imsanity_png_to_jpg ) ? true : false;
if ( ! property_exists( $_imsanity_multisite_settings, 'imsanity_delete_originals' ) ) {
$_imsanity_multisite_settings->imsanity_delete_originals = false;
}
if ( ! property_exists( $_imsanity_multisite_settings, 'imsanity_avif_quality' ) ) {
$_imsanity_multisite_settings->imsanity_avif_quality = IMSANITY_DEFAULT_AVIF_QUALITY;
}
if ( ! property_exists( $_imsanity_multisite_settings, 'imsanity_webp_quality' ) ) {
$_imsanity_multisite_settings->imsanity_webp_quality = IMSANITY_DEFAULT_WEBP_QUALITY;
}
}
return $_imsanity_multisite_settings;
}
/**
* Gets the option setting for the given key, first checking to see if it has been
* set globally for multi-site. Otherwise checking the site options.
*
* @param string $key The name of the option to retrieve.
* @param string $ifnull Value to use if the requested option returns null.
*/
function imsanity_get_option( $key, $ifnull ) {
$result = null;
$settings = imsanity_get_multisite_settings();
if ( $settings->imsanity_override_site ) {
$result = $settings->$key;
if ( is_null( $result ) ) {
$result = $ifnull;
}
} else {
$result = get_option( $key, $ifnull );
}
return $result;
}
/**
* Run upgrade check for new version.
*/
function imsanity_upgrade() {
if ( is_network_admin() ) {
return;
}
if ( -1 === version_compare( get_option( 'imsanity_version' ), IMSANITY_VERSION ) ) {
if ( wp_doing_ajax() ) {
return;
}
imsanity_set_defaults();
update_option( 'imsanity_version', IMSANITY_VERSION );
}
}
/**
* Set default options on multi-site.
*/
function imsanity_set_defaults() {
$settings = imsanity_get_multisite_settings();
add_option( 'imsanity_max_width', $settings->imsanity_max_width, '', false );
add_option( 'imsanity_max_height', $settings->imsanity_max_height, '', false );
add_option( 'imsanity_max_width_library', $settings->imsanity_max_width_library, '', false );
add_option( 'imsanity_max_height_library', $settings->imsanity_max_height_library, '', false );
add_option( 'imsanity_max_width_other', $settings->imsanity_max_width_other, '', false );
add_option( 'imsanity_max_height_other', $settings->imsanity_max_height_other, '', false );
add_option( 'imsanity_bmp_to_jpg', $settings->imsanity_bmp_to_jpg, '', false );
add_option( 'imsanity_png_to_jpg', $settings->imsanity_png_to_jpg, '', false );
add_option( 'imsanity_quality', $settings->imsanity_quality, '', false );
add_option( 'imsanity_avif_quality', $settings->imsanity_avif_quality, '', false );
add_option( 'imsanity_webp_quality', $settings->imsanity_webp_quality, '', false );
add_option( 'imsanity_delete_originals', $settings->imsanity_delete_originals, '', false );
$autoload_options = array(
'imsanity_version' => true,
'imsanity_max_width' => false,
'imsanity_max_height' => false,
'imsanity_max_width_library' => false,
'imsanity_max_height_library' => false,
'imsanity_max_width_other' => false,
'imsanity_max_height_other' => false,
'imsanity_bmp_to_jpg' => false,
'imsanity_png_to_jpg' => false,
'imsanity_quality' => false,
'imsanity_avif_quality' => false,
'imsanity_webp_quality' => false,
'imsanity_delete_originals' => false,
);
wp_set_option_autoload_values( $autoload_options );
}
/**
* Register the configuration settings that the plugin will use
*/
function imsanity_register_settings() {
imsanity_upgrade();
// We only want to update if the form has been submitted.
// Verification is done inside the imsanity_network_settings_update() function.
if ( isset( $_POST['update_imsanity_settings'] ) && is_multisite() && is_network_admin() ) { // phpcs:ignore WordPress.Security.NonceVerification
imsanity_network_settings_update();
}
// Register our settings.
register_setting( 'imsanity-settings-group', 'imsanity_max_height', 'intval' );
register_setting( 'imsanity-settings-group', 'imsanity_max_width', 'intval' );
register_setting( 'imsanity-settings-group', 'imsanity_max_height_library', 'intval' );
register_setting( 'imsanity-settings-group', 'imsanity_max_width_library', 'intval' );
register_setting( 'imsanity-settings-group', 'imsanity_max_height_other', 'intval' );
register_setting( 'imsanity-settings-group', 'imsanity_max_width_other', 'intval' );
register_setting( 'imsanity-settings-group', 'imsanity_bmp_to_jpg', 'boolval' );
register_setting( 'imsanity-settings-group', 'imsanity_png_to_jpg', 'boolval' );
register_setting( 'imsanity-settings-group', 'imsanity_quality', 'imsanity_jpg_quality' );
register_setting( 'imsanity-settings-group', 'imsanity_avif_quality', 'imsanity_avif_quality' );
register_setting( 'imsanity-settings-group', 'imsanity_webp_quality', 'imsanity_webp_quality' );
register_setting( 'imsanity-settings-group', 'imsanity_delete_originals', 'boolval' );
}
/**
* Set the quality based on the mime type of the image being resized.
*
* @param int $quality The quality currently set.
* @param string $mime_type The mime type of the image being resized.
* @return int The (potentially) adjusted quality level.
*/
function imsanity_editor_quality( $quality, $mime_type = '' ) {
if ( 'image/avif' === $mime_type ) {
$new_quality = imsanity_avif_quality();
} elseif ( 'image/webp' === $mime_type ) {
$new_quality = imsanity_webp_quality();
} elseif ( 'image/jpeg' === $mime_type ) {
$new_quality = imsanity_jpg_quality();
}
if ( ! empty( $new_quality ) && $new_quality > 0 && $new_quality <= 92 ) {
return $new_quality;
}
return $quality;
}
/**
* Validate and return the JPG quality setting.
*
* @param int $quality The JPG quality currently set.
* @return int The (potentially) adjusted quality level.
*/
function imsanity_jpg_quality( $quality = null ) {
if ( is_null( $quality ) ) {
$quality = get_option( 'imsanity_quality' );
}
if ( preg_match( '/^(100|[1-9][0-9]?)$/', $quality ) ) {
return (int) $quality;
} else {
if ( ! defined( 'IMSANITY_DEFAULT_QUALITY' ) ) {
imsanity_init();
}
return IMSANITY_DEFAULT_QUALITY;
}
}
/**
* Validate and return the AVIF quality setting.
*
* @param int $quality The AVIF quality currently set.
* @return int The (potentially) adjusted quality level.
*/
function imsanity_avif_quality( $quality = null ) {
if ( is_null( $quality ) ) {
$quality = get_option( 'imsanity_avif_quality' );
}
if ( preg_match( '/^(100|[1-9][0-9]?)$/', $quality ) ) {
return (int) $quality;
} else {
if ( ! defined( 'IMSANITY_DEFAULT_AVIF_QUALITY' ) ) {
imsanity_init();
}
return IMSANITY_DEFAULT_AVIF_QUALITY;
}
}
/**
* Validate and return the WebP quality setting.
*
* @param int $quality The WebP quality currently set.
* @return int The (potentially) adjusted quality level.
*/
function imsanity_webp_quality( $quality = null ) {
if ( is_null( $quality ) ) {
$quality = get_option( 'imsanity_webp_quality' );
}
if ( preg_match( '/^(100|[1-9][0-9]?)$/', $quality ) ) {
return (int) $quality;
} else {
if ( ! defined( 'IMSANITY_DEFAULT_WEBP_QUALITY' ) ) {
imsanity_init();
}
return IMSANITY_DEFAULT_WEBP_QUALITY;
}
}
/**
* Check default WP threshold and adjust to comply with normal Imsanity behavior.
*
* @param int $size The default WP scaling size, or whatever has been filtered by other plugins.
* @param array $imagesize {
* Indexed array of the image width and height in pixels.
*
* @type int $0 The image width.
* @type int $1 The image height.
* }
* @param string $file Full path to the uploaded image file.
* @return int The proper size to use for scaling originals.
*/
function imsanity_adjust_default_threshold( $size, $imagesize = array(), $file = '' ) {
if ( false !== strpos( $file, 'noresize' ) ) {
return false;
}
if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) {
imsanity_init();
}
$max_size = max(
imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH ),
imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT ),
imsanity_get_option( 'imsanity_max_width_library', IMSANITY_DEFAULT_MAX_WIDTH ),
imsanity_get_option( 'imsanity_max_height_library', IMSANITY_DEFAULT_MAX_HEIGHT ),
imsanity_get_option( 'imsanity_max_width_other', IMSANITY_DEFAULT_MAX_WIDTH ),
imsanity_get_option( 'imsanity_max_height_other', IMSANITY_DEFAULT_MAX_HEIGHT ),
(int) $size
);
return $max_size;
}
/**
* Helper function to render css styles for the settings forms
* for both site and network settings page
*/
function imsanity_settings_css() {
?>
|
|
imsanity_override_site ) {
imsanity_settings_page_notice();
} else {
imsanity_settings_page_form();
}
?>
' . esc_html__( 'List View in the Media Library', 'imsanity' ) . ''
);
?>
';
}
/**
* Multi-user config file exists so display a notice
*/
function imsanity_settings_page_notice() {
?>
" . esc_html__( 'The GD extension is not enabled in PHP, Imsanity may not function correctly. Enable GD or contact your web host for assistance.', 'imsanity' ) . '
';
}
/**
* Render the site settings form.
*/
function imsanity_settings_page_form() {
if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) {
imsanity_init();
}
?>