/**
 * Performs actions related to upgrading from version 0.2.08 because from that version and on major code and 
 * settings changes took place.
 * 
 * @author Nevma (info@nevma.gr)
 * 
 * @return void
 */
function adaptive_images_upgrade_action_upgraded_from_v0208()
{
    // Try to remove old htaccess entry.
    $htaccess = adaptive_images_plugin_get_htaccess_file_path();
    $htaccess_available = adaptive_images_plugin_is_htaccess_writeable();
    if ($htaccess_available) {
        $htaccess_old_contents = file_get_contents($htaccess);
        $htaccess_new_contents = preg_replace('/# Adaptive Images.*# END Adaptive Images\\n/s', '', $htaccess_old_contents);
        @file_put_contents($htaccess, $htaccess_new_contents);
    }
    // Try to remove old cache directory.
    $old_cache_path = realpath(dirname($_SERVER['SCRIPT_FILENAME']) . '/../wp-content/') . '/cache-ai/';
    adaptive_images_actions_rmdir_recursive($old_cache_path);
    // Try to remove old options.
    delete_option('wprxr_include_paths');
    delete_option('wprxr_ai_config');
}
/**
 * Deletes the contents of a directory recursively.
 * 
 * @author Nevma (info@nevma.gr)
 * 
 * @param string $dir The directory whose contents to delete recursively.
 * 
 * @return array An array with the totals of directories and files deleted so far.
 */
function adaptive_images_actions_rmdir_recursive($dir)
{
    // var_dump( $dir );
    // echo '<hr />';
    // $result = exec( 'rm -rfv ' . $dir, $output, $code );
    // var_dump( $result ); // ==> empty when not done, echoes "removed directory: `/foo/bar'" when done
    // echo '<hr />';
    // var_dump( $output ); // ==> empty when not done, holds lines of command output when done
    // echo '<hr />';
    // var_dump( $code );   // ==> 0 when command executed successfully, 1 when not
    // echo '<hr />';
    // return;
    // Keep count of recursively accessed files.
    static $total_files = 0;
    static $total_dirs = 0;
    static $total_size = 0;
    // Do not take into acount files and symbolic links.
    if (is_dir($dir) && !is_link($dir)) {
        $objects = scandir($dir);
        foreach ($objects as $object) {
            if ($object != "." && $object != "..") {
                $file = $dir . "/" . $object;
                if (filetype($file) == "dir") {
                    // Descend into directory children.
                    $total_dirs++;
                    adaptive_images_actions_rmdir_recursive($file);
                } else {
                    // Delete file.
                    $total_files++;
                    $total_size += filesize($file);
                    unlink($file);
                }
            }
        }
        reset($objects);
        rmdir($dir);
    }
    return array('files' => $total_files, 'size' => $total_size, 'dirs' => $total_dirs);
}
Esempio n. 3
0
<?php

/******************************************************************************************************************
 *                                                                                                                *
 *                                                                                                                *
 *      INCLUDED AS-IS WHEN THE PLUGIN IS UNINSTALLED                                                             *
 *      =============================================                                                             *
 *                                                                                                                *
 *      Nevma (info@nevma.gr)                                                                                     *
 *                                                                                                                *
 *                                                                                                                *
 ******************************************************************************************************************/
// Exit, if file is accessed directly.
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}
// Include important plugin functions.
require_once 'adaptive-images-plugin.php';
require_once 'adaptive-images-actions.php';
// Attempt to cleanup the cache.
$cache_path = adaptive_images_plugin_get_cahe_directory_path();
adaptive_images_actions_rmdir_recursive($cache_path);
// Cleanup possible leftover options from version 0.2.08.
delete_option('wprxr_include_paths');
delete_option('wprxr_ai_config');
// Cleanup current plugin options.
delete_option('adaptive-images');
/**
 * Takes care of the plugin settings page actions.
 * 
 * @author Nevma (info@nevma.gr)
 * 
 * @return void Nothing really!.
 */
function adaptive_images_admin_settings_actions()
{
    if (!isset($_GET['action'])) {
        return;
    }
    // Cleanup image cache action.
    if ($_GET['action'] == 'cleanup-image-cache' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-cleanup-image-cache')) {
        $cache_path = adaptive_images_plugin_get_cahe_directory_path();
        $result = adaptive_images_actions_rmdir_recursive($cache_path);
        add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Cleanup image cache <hr />' . '<p>Total files deleted from the adaptive images cache: ' . $result['files'] . '</p>' . '<p>Total directories deleted from the adaptive images cache: ' . $result['dirs'] . '</p>' . '<p>' . 'Total size deleted from the adaptive images cache: ' . adaptive_images_plugin_file_size_human($result['size']) . '</p>', 'updated');
    }
    // Calculate image cache size action.
    if ($_GET['action'] == 'calculate-cache-size' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-calculate-cache-size')) {
        $cache_path = adaptive_images_plugin_get_cahe_directory_path();
        $cache_size = adaptive_images_plugin_dir_size($cache_path);
        add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Calculate cache size <hr />' . '<p>Total files in the adaptive images cache: ' . $cache_size['files'] . '</p>' . '<p>Total directories in the adaptive images cache: ' . $cache_size['dirs'] . '</p>' . '<p>' . 'Total size of the adaptive images cache: ' . adaptive_images_plugin_file_size_human($cache_size['size']) . '</p>', 'updated');
    }
    // Print plugin info action.
    if ($_GET['action'] == 'print-debug-info' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-print-debug-info')) {
        add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Debug info <hr />' . adaptive_images_debug_general_info(FALSE), 'updated');
    }
    // Print system info action.
    if ($_GET['action'] == 'print-diagnostic-info' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-print-diagnostic-info')) {
        add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'System information <hr />' . adaptive_images_debug_diagnostic_info(FALSE), 'updated');
    }
}