Merged in feature/from-pantheon (pull request #16)

code from pantheon

* code from pantheon
This commit is contained in:
Tony Volpe
2024-01-10 17:03:02 +00:00
parent 054b4fffc9
commit 4eb982d7a8
16492 changed files with 3475854 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
<?php
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
add_filter( 'upgrader_post_install', 'imagify_sync_theme_plugin_files_on_update', IMAGIFY_INT_MAX, 3 );
/**
* Sync files right after a theme or plugin has been updated.
*
* @since 1.7
* @author Grégory Viguier
*
* @param bool $response Installation response.
* @param array $hook_extra Extra arguments passed to hooked filters.
* @param array $result Installation result data.
* @return bool
*/
function imagify_sync_theme_plugin_files_on_update( $response, $hook_extra, $result ) {
global $wpdb;
if ( ( empty( $hook_extra['plugin'] ) && empty( $hook_extra['theme'] ) ) || empty( $result['destination'] ) ) {
return $response;
}
$folders_to_sync = get_site_transient( 'imagify_themes_plugins_to_sync' );
$folders_to_sync = is_array( $folders_to_sync ) ? $folders_to_sync : array();
$folders_to_sync[] = $result['destination'];
set_site_transient( 'imagify_themes_plugins_to_sync', $folders_to_sync, DAY_IN_SECONDS );
return $response;
}
add_action( 'admin_init', 'imagify_sync_theme_plugin_files_after_update' );
/**
* Sync files after some themes or plugins have been updated.
*
* @since 1.7.1.2
* @author Grégory Viguier
*/
function imagify_sync_theme_plugin_files_after_update() {
global $wpdb;
$folders_to_sync = get_site_transient( 'imagify_themes_plugins_to_sync' );
if ( ! $folders_to_sync || ! is_array( $folders_to_sync ) ) {
return;
}
delete_site_transient( 'imagify_themes_plugins_to_sync' );
$folders_db = Imagify_Folders_DB::get_instance();
$files_db = Imagify_Files_DB::get_instance();
if ( ! $folders_db->can_operate() || ! $files_db->can_operate() ) {
return;
}
foreach ( $folders_to_sync as $folder_path ) {
$folder_path = trailingslashit( $folder_path );
if ( Imagify_Files_Scan::is_path_forbidden( $folder_path ) ) {
// This theme or plugin must not be optimized.
continue;
}
// Get the related folder.
$placeholder = Imagify_Files_Scan::add_placeholder( $folder_path );
$folder = Imagify_Folders_DB::get_instance()->get_in( 'path', $placeholder );
if ( ! $folder ) {
// This theme or plugin is not in the database.
continue;
}
// Sync the folder files.
Imagify_Custom_Folders::synchronize_files_from_folders( array(
$folder['folder_id'] => array(
'folder_id' => $folder['folder_id'],
'path' => $placeholder,
'active' => $folder['active'],
'folder_path' => $folder_path,
),
) );
}
}

View File

@@ -0,0 +1,98 @@
<?php
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
add_filter( 'attachment_fields_to_edit', '_imagify_attachment_fields_to_edit', IMAGIFY_INT_MAX, 2 );
/**
* Add "Imagify" column in the Media Uploader
*
* @since 1.2
* @author Jonathan Buttigieg
*
* @param array $form_fields An array of attachment form fields.
* @param object $post The WP_Post attachment object.
* @return array
*/
function _imagify_attachment_fields_to_edit( $form_fields, $post ) {
global $pagenow;
if ( 'post.php' === $pagenow ) {
return $form_fields;
}
if ( ! imagify_get_context( 'wp' )->current_user_can( 'manual-optimize', $post->ID ) ) {
return $form_fields;
}
$process = imagify_get_optimization_process( $post->ID, 'wp' );
$form_fields['imagify'] = array(
'label' => 'Imagify',
'input' => 'html',
'html' => get_imagify_media_column_content( $process ),
'show_in_edit' => true,
'show_in_modal' => true,
);
return $form_fields;
}
add_filter( 'media_row_actions', '_imagify_add_actions_to_media_list_row', IMAGIFY_INT_MAX, 2 );
/**
* Add "Compare Original VS Optimized" link to the media row action
*
* @since 1.4.3
* @author Geoffrey Crofte
* @param array $actions An array of action links for each attachment.
* Default 'Edit', 'Delete Permanently', 'View'.
* @param object $post WP_Post object for the current attachment.
* @return array
*/
function _imagify_add_actions_to_media_list_row( $actions, $post ) {
if ( ! imagify_get_context( 'wp' )->current_user_can( 'manual-optimize', $post->ID ) ) {
return $actions;
}
$process = imagify_get_optimization_process( $post->ID, 'wp' );
if ( ! $process->is_valid() ) {
return $actions;
}
$media = $process->get_media();
// If this media is not an image, do nothing.
if ( ! $media->is_supported() || ! $media->is_image() ) {
return $actions;
}
$data = $process->get_data();
// If Imagify license not valid, or image is not optimized, do nothing.
if ( ! Imagify_Requirements::is_api_key_valid() || ! $data->is_optimized() ) {
return $actions;
}
// If no backup, do nothing.
if ( ! $media->has_backup() ) {
return $actions;
}
$dimensions = $media->get_dimensions();
// If full image is too small. See get_imagify_localize_script_translations().
if ( $dimensions['width'] < 360 ) {
return $actions;
}
// Else, add action link for comparison (JS triggered).
$actions['imagify-compare'] = Imagify_Views::get_instance()->get_template( 'button/compare-images', [
'url' => get_edit_post_link( $media->get_id() ) . '#imagify-compare',
'backup_url' => $media->get_backup_url(),
'original_url' => $media->get_fullsize_url(),
'media_id' => $media->get_id(),
'width' => $dimensions['width'],
'height' => $dimensions['height'],
] );
return $actions;
}

View File

@@ -0,0 +1,89 @@
<?php
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
add_action( 'attachment_submitbox_misc_actions', '_imagify_attachment_submitbox_misc_actions', IMAGIFY_INT_MAX );
/**
* Add a "Optimize It" button or the Imagify optimization data in the attachment submit area.
*
* @since 1.0
*/
function _imagify_attachment_submitbox_misc_actions() {
global $post;
if ( ! imagify_get_context( 'wp' )->current_user_can( 'manual-optimize', $post->ID ) ) {
return;
}
$process = imagify_get_optimization_process( $post->ID, 'wp' );
if ( ! $process->is_valid() ) {
return;
}
$media = $process->get_media();
if ( ! $media->is_supported() ) {
return;
}
if ( ! $media->has_required_media_data() ) {
return;
}
$data = $process->get_data();
$views = Imagify_Views::get_instance();
if ( ! Imagify_Requirements::is_api_key_valid() && ! $data->is_optimized() ) {
?>
<div class="misc-pub-section misc-pub-imagify"><h4><?php esc_html_e( 'Imagify', 'imagify' ); ?></h4></div>
<div class="misc-pub-section misc-pub-imagify">
<?php esc_html_e( 'Invalid API key', 'imagify' ); ?>
<br/>
<a href="<?php echo esc_url( get_imagify_admin_url() ); ?>">
<?php esc_html_e( 'Check your Settings', 'imagify' ); ?>
</a>
</div>
<?php
} else {
$is_locked = $process->is_locked();
if ( $is_locked ) {
switch ( $is_locked ) {
case 'optimizing':
$lock_label = __( 'Optimizing...', 'imagify' );
break;
case 'restoring':
$lock_label = __( 'Restoring...', 'imagify' );
break;
default:
$lock_label = __( 'Processing...', 'imagify' );
}
?>
<div class="misc-pub-section misc-pub-imagify">
<?php $views->print_template( 'button/processing', [ 'label' => $lock_label ] ); ?>
</div>
<?php
} elseif ( $data->is_optimized() || $data->is_already_optimized() || $data->is_error() ) {
?>
<div class="misc-pub-section misc-pub-imagify"><h4><?php esc_html_e( 'Imagify', 'imagify' ); ?></h4></div>
<div class="misc-pub-section misc-pub-imagify imagify-data-item">
<?php echo get_imagify_attachment_optimization_text( $process ); ?>
</div>
<?php
} else {
$url = get_imagify_admin_url( 'optimize', array( 'attachment_id' => $post->ID ) );
?>
<div class="misc-pub-section misc-pub-imagify">
<a class="button-primary" href="<?php echo esc_url( $url ); ?>"><?php esc_html_e( 'Optimize', 'imagify' ); ?></a>
</div>
<?php
}
}
if ( $media->has_backup() && $data->is_optimized() ) {
?>
<input id="imagify-full-original" type="hidden" value="<?php echo esc_url( $media->get_backup_url() ); ?>">
<input id="imagify-full-original-size" type="hidden" value="<?php echo esc_attr( $data->get_original_size( true, 0 ) ); ?>">
<?php
}
}

View File

@@ -0,0 +1,331 @@
<?php
defined( 'ABSPATH' ) || die( 'Cheatin&#8217; uh?' );
/**
* Tell WP what to do when admin is loaded aka upgrader.
*
* @since 1.0
*/
function _imagify_upgrader() {
// Back-compat' with previous version of the upgrader.
imagify_upgrader_upgrade();
// Version stored on the network.
$network_version = Imagify_Options::get_instance()->get( 'version' );
// Version stored at the site level.
$site_version = Imagify_Data::get_instance()->get( 'version' );
if ( ! $network_version ) {
// First install (network).
/**
* Triggered on Imagify first install (network).
*
* @since 1.7
* @author Grégory Viguier
*/
do_action( 'imagify_first_network_install' );
} elseif ( IMAGIFY_VERSION !== $network_version ) {
// Already installed but got updated (network).
/**
* Triggered on Imagify upgrade (network).
*
* @since 1.7
* @author Grégory Viguier
*
* @param string $network_version Previous version stored on the network.
* @param string $site_version Previous version stored on site level.
*/
do_action( 'imagify_network_upgrade', $network_version, $site_version );
}
// If any upgrade has been done, we flush and update version.
if ( did_action( 'imagify_first_network_install' ) || did_action( 'imagify_network_upgrade' ) ) {
Imagify_Options::get_instance()->set( 'version', IMAGIFY_VERSION );
}
if ( ! $site_version ) {
// First install (site level).
/**
* Triggered on Imagify first install (site level).
*
* @since 1.0
*/
do_action( 'imagify_first_install' );
} elseif ( IMAGIFY_VERSION !== $site_version ) {
// Already installed but got updated (site level).
/**
* Triggered on Imagify upgrade (site level).
*
* @since 1.0
* @since 1.7 $network_version replaces the "new version" (which can easily be grabbed with the constant).
*
* @param string $network_version Previous version stored on the network.
* @param string $site_version Previous version stored on site level.
*/
do_action( 'imagify_upgrade', $network_version, $site_version );
}
// If any upgrade has been done, we flush and update version.
if ( did_action( 'imagify_first_install' ) || did_action( 'imagify_upgrade' ) ) {
Imagify_Data::get_instance()->set( 'version', IMAGIFY_VERSION );
}
}
add_action( 'admin_init', '_imagify_upgrader' );
/**
* Upgrade the upgrader:
* Imagify 1.7 splits "network version" and "site version". Since the "site version" didn't exist before 1.7, we need to provide a version based on the "network version".
*
* @since 1.7
* @author Grégory Viguier
*/
function imagify_upgrader_upgrade() {
global $wpdb;
// Version stored on the network.
$network_version = Imagify_Options::get_instance()->get( 'version' );
if ( ! $network_version ) {
// Really first install.
return;
}
// Version stored at the site level.
$site_version = Imagify_Data::get_instance()->get( 'version' );
if ( $site_version ) {
// This site's upgrader is already upgraded.
return;
}
if ( ! is_multisite() ) {
// Not a multisite, so both versions must have the same value.
Imagify_Data::get_instance()->set( 'version', $network_version );
return;
}
$sites = get_site_option( 'imagify_old_version' );
if ( IMAGIFY_VERSION !== $network_version && ! $sites ) {
// The network is not up-to-date yet: store the site IDs that must be updated.
$network_id = function_exists( 'get_current_network_id' ) ? get_current_network_id() : $wpdb->siteid;
$sites = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d AND archived = 0 AND deleted = 0", $network_id ) );
$sites = array_map( 'absint', $sites );
$sites = array_filter( $sites );
if ( ! $sites ) {
// Uh?
return;
}
// We store the old network version and the site Ids: those sites will need to be upgraded from this version.
$sites['version'] = $network_version;
add_site_option( 'imagify_old_version', $sites );
}
if ( empty( $sites['version'] ) ) {
// WTF.
delete_site_option( 'imagify_old_version' );
return;
}
$network_version = $sites['version'];
unset( $sites['version'] );
$sites = array_flip( $sites );
$site_id = get_current_blog_id();
if ( ! isset( $sites[ $site_id ] ) ) {
// This site is already upgraded.
return;
}
unset( $sites[ $site_id ] );
if ( ! $sites ) {
// We're done, all the sites have been upgraded.
delete_site_option( 'imagify_old_version' );
} else {
// Some sites still need to be upgraded.
$sites = array_flip( $sites );
$sites['version'] = $network_version;
update_site_option( 'imagify_old_version', $sites );
}
Imagify_Data::get_instance()->set( 'version', $network_version );
}
/**
* Keeps this function up to date at each version.
*
* @since 1.0
*/
function _imagify_first_install() {
// Set a transient to know when we will have to display a notice to ask the user to rate the plugin.
set_site_transient( 'imagify_seen_rating_notice', true, DAY_IN_SECONDS * 3 );
}
add_action( 'imagify_first_network_install', '_imagify_first_install' );
/**
* What to do when Imagify is updated, depending on versions.
*
* @since 1.0
* @since 1.7 $network_version replaces the "new version" (which can easily be grabbed with the constant).
*
* @param string $network_version Previous version stored on the network.
* @param string $site_version Previous version stored on site level.
*/
function _imagify_new_upgrade( $network_version, $site_version ) {
global $wpdb;
$options = Imagify_Options::get_instance();
// 1.2
if ( version_compare( $site_version, '1.2' ) < 0 ) {
// Update all already optimized images status from 'error' to 'already_optimized'.
$query = new WP_Query( array(
'is_imagify' => true,
'post_type' => 'attachment',
'post_status' => imagify_get_post_statuses(),
'post_mime_type' => imagify_get_mime_types(),
'meta_key' => '_imagify_status',
'meta_value' => 'error',
'posts_per_page' => -1,
'update_post_term_cache' => false,
'no_found_rows' => true,
'fields' => 'ids',
) );
if ( $query->posts ) {
foreach ( (array) $query->posts as $id ) {
$data = get_post_meta( $id, '_imagify_data', true );
$error = ! empty( $data['sizes']['full']['error'] ) ? $data['sizes']['full']['error'] : '';
if ( false !== strpos( $error, 'This image is already compressed' ) ) {
update_post_meta( $id, '_imagify_status', 'already_optimized' );
}
}
}
// Auto-activate the Admin Bar option.
$options->set( 'admin_bar_menu', 1 );
}
// 1.3.2
if ( version_compare( $site_version, '1.3.2' ) < 0 ) {
// Update all already optimized images status from 'error' to 'already_optimized'.
$query = new WP_Query( array(
'is_imagify' => true,
'post_type' => 'attachment',
'post_status' => imagify_get_post_statuses(),
'post_mime_type' => imagify_get_mime_types(),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_imagify_data',
'compare' => 'EXISTS',
),
array(
'key' => '_imagify_optimization_level',
'compare' => 'NOT EXISTS',
),
),
'posts_per_page' => -1,
'update_post_term_cache' => false,
'no_found_rows' => true,
'fields' => 'ids',
) );
if ( $query->posts ) {
foreach ( (array) $query->posts as $id ) {
$data = get_post_meta( $id, '_imagify_data', true );
$stats = isset( $data['stats'] ) ? $data['stats'] : [];
if ( isset( $stats['aggressive'] ) ) {
update_post_meta( $id, '_imagify_optimization_level', (int) $stats['aggressive'] );
}
}
}
}
// 1.4.5
if ( version_compare( $site_version, '1.4.5' ) < 0 ) {
// Delete all transients used for async optimization.
$wpdb->query( $wpdb->prepare( "DELETE from $wpdb->options WHERE option_name LIKE %s", Imagify_DB::esc_like( '_transient_imagify-async-in-progress-' ) . '%' ) );
}
// 1.7
if ( version_compare( $site_version, '1.7' ) < 0 ) {
// Migrate data.
Imagify_Cron_Library_Size::get_instance()->do_event();
if ( ! imagify_is_active_for_network() ) {
// Make sure the settings are autoloaded.
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET `autoload` = 'yes' WHERE `autoload` != 'yes' AND option_name = %s", $options->get_option_name() ) );
}
// Rename the option that stores the NGG table version. Since the table is also updated in 1.7, let's simply delete the option.
delete_option( $wpdb->prefix . 'ngg_imagify_data_db_version' );
}
// 1.8.1
if ( version_compare( $site_version, '1.8.1' ) < 0 ) {
// Custom folders: replace `{{ABSPATH}}/` by `{{ROOT}}/`.
$filesystem = imagify_get_filesystem();
$replacement = '{{ROOT}}/';
if ( $filesystem->has_wp_its_own_directory() ) {
$replacement .= preg_replace( '@^' . preg_quote( $filesystem->get_site_root(), '@' ) . '@', '', $filesystem->get_abspath() );
}
$replacement = Imagify_DB::esc_like( $replacement );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->base_prefix}imagify_files SET path = REPLACE( path, '{{ABSPATH}}/', %s ) WHERE path LIKE %s", $replacement, '{{ABSPATH}}/%' ) );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->base_prefix}imagify_folders SET path = REPLACE( path, '{{ABSPATH}}/', %s ) WHERE path LIKE %s", $replacement, '{{ABSPATH}}/%' ) );
}
// 1.8.2
if ( version_compare( $site_version, '1.8.2' ) < 0 ) {
Imagify_Options::get_instance()->set( 'partner_links', 1 );
}
// 1.9.6
if ( version_compare( $site_version, '1.9.6' ) < 0 ) {
\Imagify\Stats\OptimizedMediaWithoutWebp::get_instance()->clear_cache();
}
// 1.9.11
if ( version_compare( $site_version, '1.9.11' ) < 0 ) {
imagify_secure_custom_directories();
}
if ( version_compare( $site_version, '2.0' ) < 0 ) {
Imagify_Options::get_instance()->set( 'optimization_level', 2 );
}
}
add_action( 'imagify_upgrade', '_imagify_new_upgrade', 10, 2 );
/**
* Scan imagify directories and add `index.php` files where missing.
*
* @since 1.9.11
*
* @return void
*/
function imagify_secure_custom_directories() {
$filesystem = imagify_get_filesystem();
Imagify_Custom_Folders::add_indexes();
$conf_dir = $filesystem->get_site_root() . 'conf';
Imagify_Custom_Folders::add_indexes( $conf_dir );
$backup_dir = get_imagify_backup_dir_path();
Imagify_Custom_Folders::add_indexes( $backup_dir );
}
add_action( 'imagify_activation', 'imagify_secure_custom_directories' );

View File

@@ -0,0 +1,131 @@
<?php
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
add_filter( 'manage_media_columns', '_imagify_manage_media_columns' );
/**
* Add "Imagify" column in upload.php.
*
* @since 1.0
* @author Jonathan Buttigieg
*
* @param array $columns An array of columns displayed in the Media list table.
* @return array
*/
function _imagify_manage_media_columns( $columns ) {
if ( imagify_get_context( 'wp' )->current_user_can( 'optimize' ) ) {
$columns['imagify_optimized_file'] = __( 'Imagify', 'imagify' );
}
return $columns;
}
add_action( 'manage_media_custom_column', '_imagify_manage_media_custom_column', 10, 2 );
/**
* Add content to the "Imagify" columns in upload.php.
*
* @since 1.0
* @author Jonathan Buttigieg
*
* @param string $column_name Name of the custom column.
* @param int $attachment_id Attachment ID.
*/
function _imagify_manage_media_custom_column( $column_name, $attachment_id ) {
if ( 'imagify_optimized_file' !== $column_name ) {
return;
}
$process = imagify_get_optimization_process( $attachment_id, 'wp' );
echo get_imagify_media_column_content( $process );
}
add_action( 'restrict_manage_posts', '_imagify_attachments_filter_dropdown' );
/**
* Adds a dropdown that allows filtering on the attachments Imagify status.
*
* @since 1.0
* @author Jonathan Buttigieg
*/
function _imagify_attachments_filter_dropdown() {
if ( ! Imagify_Views::get_instance()->is_wp_library_page() ) {
return;
}
$optimized = imagify_count_optimized_attachments();
$unoptimized = imagify_count_unoptimized_attachments();
$errors = imagify_count_error_attachments();
$status = isset( $_GET['imagify-status'] ) ? wp_unslash( $_GET['imagify-status'] ) : 0; // WPCS: CSRF ok.
$options = array(
'optimized' => _x( 'Optimized', 'Media Files', 'imagify' ),
'unoptimized' => _x( 'Unoptimized', 'Media Files', 'imagify' ),
'errors' => _x( 'Errors', 'Media Files', 'imagify' ),
);
echo '<label class="screen-reader-text" for="filter-by-optimization-status">' . __( 'Filter by status', 'imagify' ) . '</label>';
echo '<select id="filter-by-optimization-status" name="imagify-status">';
echo '<option value="0" selected="selected">' . __( 'All Media Files', 'imagify' ) . '</option>';
foreach ( $options as $value => $label ) {
echo '<option value="' . $value . '" ' . selected( $status, $value, false ) . '>' . $label . ' (' . ${$value} . ')</option>';
}
echo '</select>&nbsp;';
}
add_filter( 'request', '_imagify_sort_attachments_by_status' );
/**
* Modify the query based on the imagify-status variable in $_GET.
*
* @since 1.0
* @author Jonathan Buttigieg
*
* @param array $vars The array of requested query variables.
* @return array
*/
function _imagify_sort_attachments_by_status( $vars ) {
if ( empty( $_GET['imagify-status'] ) || ! Imagify_Views::get_instance()->is_wp_library_page() ) { // WPCS: CSRF ok.
return $vars;
}
$status = wp_unslash( $_GET['imagify-status'] ); // WPCS: CSRF ok.
$meta_key = '_imagify_status';
$meta_compare = '=';
$relation = array();
switch ( $status ) {
case 'unoptimized':
$meta_key = '_imagify_data';
$meta_compare = 'NOT EXISTS';
break;
case 'optimized':
$status = 'success';
$relation = array(
'key' => $meta_key,
'value' => 'already_optimized',
'compare' => $meta_compare,
);
break;
case 'errors':
$status = 'error';
break;
default:
return $vars;
}
$vars = array_merge( $vars, array(
'meta_query' => array(
'relation' => 'or',
array(
'key' => $meta_key,
'value' => $status,
'compare' => $meta_compare,
),
$relation,
),
) );
if ( ! key_exists( 'post_mime_type', $vars ) ) {
$vars['post_mime_type'] = imagify_get_mime_types();
}
return $vars;
}