| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
class Powered_Cache_Admin_Actions { |
| 9 |
|
| 10 |
/** |
| 11 |
* Save settings to database and file |
| 12 |
* |
| 13 |
* @since 1.0 |
| 14 |
*/ |
| 15 |
public static function update_settings() { |
| 16 |
global $powered_cache_options, $powered_cache_fs; |
| 17 |
|
| 18 |
|
| 19 |
if ( ! defined( 'POWERED_CACHE_SAVING_OPTIONS' ) ) { |
| 20 |
define( 'POWERED_CACHE_SAVING_OPTIONS', true ); |
| 21 |
} |
| 22 |
|
| 23 |
$old_options = $powered_cache_options; |
| 24 |
|
| 25 |
$new_options = array(); |
| 26 |
$_post = ( isset( $_POST['powered_cache_settings'] ) ? $_POST['powered_cache_settings'] : array() ); |
| 27 |
|
| 28 |
if ( ! isset( $_REQUEST['section'] ) ) { |
| 29 |
$_REQUEST['section'] = 'basic-options'; |
| 30 |
} |
| 31 |
|
| 32 |
switch ( $_REQUEST['section'] ) { |
| 33 |
case 'basic-options': |
| 34 |
default: |
| 35 |
$new_options['enable_page_caching'] = ( isset( $_post['enable_page_caching'] ) && 1 == $_post['enable_page_caching'] ? true : false ); |
| 36 |
$new_options['object_cache'] = ( isset( $_post['object_cache'] ) ? sanitize_text_field( $_post['object_cache'] ) : 'off' ); |
| 37 |
$new_options['cache_mobile'] = ( isset( $_post['cache_mobile'] ) && 1 == $_post['cache_mobile'] ? true : false ); |
| 38 |
$new_options['cache_mobile_separate_file'] = ( ( isset( $_post['cache_mobile'] ) && 1 == $_post['cache_mobile'] ) && ( isset( $_post['cache_mobile_separate_file'] ) && 1 == $_post['cache_mobile_separate_file'] ) ? true : false ); |
| 39 |
$new_options['loggedin_user_cache'] = ( isset( $_post['loggedin_user_cache'] ) && 1 == $_post['loggedin_user_cache'] ? true : false ); |
| 40 |
$new_options['ssl_cache'] = ( isset( $_post['ssl_cache'] ) && 1 == $_post['ssl_cache'] ? true : false ); |
| 41 |
$new_options['gzip_compression'] = ( isset( $_post['gzip_compression'] ) && 1 == $_post['gzip_compression'] ? true : false ); |
| 42 |
|
| 43 |
/** |
| 44 |
* Calculate timeout with given interval, saving as minutes. |
| 45 |
* |
| 46 |
* @since 1.1 |
| 47 |
*/ |
| 48 |
$cache_timeout = ( isset( $_post['cache_timeout'] ) ? intval( $_post['cache_timeout'] ) : 1440 ); |
| 49 |
|
| 50 |
if ( $cache_timeout > 0 && isset( $_post['cache_timeout_interval'] ) ) { |
| 51 |
switch ( $_post['cache_timeout_interval'] ) { |
| 52 |
case 'DAY': |
| 53 |
$cache_timeout = $cache_timeout * 1440; |
| 54 |
break; |
| 55 |
case 'HOUR': |
| 56 |
$cache_timeout = $cache_timeout * 60; |
| 57 |
break; |
| 58 |
case 'MINUTE': |
| 59 |
default: |
| 60 |
$cache_timeout = $cache_timeout * 1; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$new_options['cache_timeout'] = $cache_timeout; |
| 65 |
|
| 66 |
break; |
| 67 |
case 'advanced-options': |
| 68 |
$new_options['remove_query_string'] = ( isset( $_post['remove_query_string'] ) && 1 == $_post['remove_query_string'] ? true : false ); |
| 69 |
$new_options['rejected_user_agents'] = ( strlen( trim( $_post['rejected_user_agents'] ) ) > 0 ? wp_kses_post( $_post['rejected_user_agents'] ) : '' ); |
| 70 |
$new_options['rejected_cookies'] = ( strlen( trim( $_post['rejected_cookies'] ) ) > 0 ? wp_kses_post( $_post['rejected_cookies'] ) : '' ); |
| 71 |
$new_options['rejected_uri'] = ( strlen( trim( $_post['rejected_uri'] ) ) > 0 ? wp_kses_post( $_post['rejected_uri'] ) : '' ); |
| 72 |
$new_options['accepted_query_strings'] = ( strlen( trim( $_post['accepted_query_strings'] ) ) > 0 ? wp_kses_post( $_post['accepted_query_strings'] ) : '' ); |
| 73 |
$new_options['purge_additional_pages'] = ( strlen( trim( $_post['purge_additional_pages'] ) ) > 0 ? wp_kses_post( $_post['purge_additional_pages'] ) : '' ); |
| 74 |
break; |
| 75 |
case 'cdn': |
| 76 |
$new_options['cdn_status'] = ( isset( $_post['cdn_status'] ) && 1 == $_post['cdn_status'] ? true : false ); |
| 77 |
$new_options['cdn_ssl_disable'] = ( isset( $_post['cdn_ssl_disable'] ) && 1 == $_post['cdn_ssl_disable'] ? true : false ); |
| 78 |
|
| 79 |
// prepare hostname + zone pair |
| 80 |
if ( ! empty( $_post['cdn_hostname'] ) && is_array( $_post['cdn_hostname'] ) ) { |
| 81 |
foreach ( $_post['cdn_hostname'] as $cdn_key => $hostname ) { |
| 82 |
if ( ! empty( $hostname ) && isset( $_post['cdn_zone'][ $cdn_key ] ) && ! empty( $_post['cdn_zone'][ $cdn_key ] ) ) { |
| 83 |
$new_options['cdn_hostname'][ $cdn_key ] = esc_url_raw( $hostname ); |
| 84 |
$new_options['cdn_zone'][ $cdn_key ] = sanitize_text_field( $_post['cdn_zone'][ $cdn_key ] ); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$new_options['cdn_rejected_files'] = ( strlen( trim( $_post['cdn_rejected_files'] ) ) > 0 ? wp_kses_post( $_post['cdn_rejected_files'] ) : '' ); |
| 90 |
|
| 91 |
break; |
| 92 |
case 'extensions': |
| 93 |
$extension = sanitize_text_field( $_REQUEST['extension'] ); |
| 94 |
|
| 95 |
if ( ! empty( $extension ) && isset( $_REQUEST['status'] ) ) { |
| 96 |
if ( 'activate' === $_REQUEST['status'] ) { |
| 97 |
Powered_Cache_Extensions::factory()->activate( $extension ); |
| 98 |
|
| 99 |
$msg = __( 'Extension activated', 'powered-cache' ); |
| 100 |
Powered_Cache_Admin_Helper::set_flash_message( $msg ); |
| 101 |
} elseif ( 'deactivate' === $_REQUEST['status'] ) { |
| 102 |
Powered_Cache_Extensions::factory()->deactivate( $extension ); |
| 103 |
|
| 104 |
$msg = __( 'Extension deactivated', 'powered-cache' ); |
| 105 |
Powered_Cache_Admin_Helper::set_flash_message( $msg ); |
| 106 |
} |
| 107 |
|
| 108 |
self::exit_with_redirect(); |
| 109 |
} |
| 110 |
|
| 111 |
break; |
| 112 |
/** |
| 113 |
* Handle misc options |
| 114 |
* Some of misc actions might work as GET request.So we don't need to handle here. |
| 115 |
* |
| 116 |
* @since 1.0 |
| 117 |
*/ |
| 118 |
case 'misc': |
| 119 |
if ( isset( $_POST['do-import'] ) && ! empty( $_POST['do-import'] ) ) { |
| 120 |
if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 121 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 122 |
} |
| 123 |
$uploadedfile = $_FILES['powered_cache_import']; |
| 124 |
|
| 125 |
$import_file = wp_handle_upload( $uploadedfile, array( 'action' => 'powered_cache_update_settings', 'mimes' => array( 'txt' => 'text/plain' ) ) ); |
| 126 |
|
| 127 |
if ( $import_file && ! isset( $import_file['error'] ) ) { |
| 128 |
$imported_options = $powered_cache_fs->get_contents( $import_file['file'] ); |
| 129 |
$imported_options = unserialize( $imported_options ); |
| 130 |
$imported_options['cache_location'] = powered_cache_get_cache_dir(); |
| 131 |
$powered_cache_fs->delete( $import_file['file'] ); |
| 132 |
update_option( 'powered_cache_settings', $imported_options ); |
| 133 |
$new_options = get_option( 'powered_cache_settings' ); |
| 134 |
} else { |
| 135 |
Powered_Cache_Admin_Helper::set_flash_message( $import_file['error'], 'error' ); |
| 136 |
} |
| 137 |
|
| 138 |
} else { |
| 139 |
$new_options['show_cache_message'] = ( isset( $_post['show_cache_message'] ) && 1 == $_post['show_cache_message'] ? true : false ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if ( powered_cache_save_settings( $old_options, $new_options ) ) { |
| 144 |
$msg = __( 'Options updated', 'powered-cache' ); |
| 145 |
Powered_Cache_Admin_Helper::set_flash_message( $msg ); |
| 146 |
|
| 147 |
self::exit_with_redirect(); |
| 148 |
} |
| 149 |
|
| 150 |
// false message |
| 151 |
$msg = __( 'Something went wrong, we could not save settings to file. Please make sure the configuration file writable.', 'powered-cache' ); |
| 152 |
Powered_Cache_Admin_Helper::set_flash_message( $msg, 'error' ); |
| 153 |
|
| 154 |
self::exit_with_redirect(); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Reset settings |
| 160 |
* |
| 161 |
* @since 1.0 |
| 162 |
*/ |
| 163 |
public static function reset_settings() { |
| 164 |
// purge cache stuff |
| 165 |
powered_cache_clean_site_cache_dir(); |
| 166 |
delete_option( 'powered_cache_settings' ); |
| 167 |
|
| 168 |
global $powered_cache_options; |
| 169 |
$powered_cache_options = array(); |
| 170 |
|
| 171 |
$msg = __( 'All settings cleaned!', 'powered-cache' ); |
| 172 |
Powered_Cache_Admin_Helper::set_flash_message( $msg ); |
| 173 |
|
| 174 |
self::exit_with_redirect(); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Exports settings |
| 179 |
* |
| 180 |
* @since 1.0 |
| 181 |
*/ |
| 182 |
public static function export_settings() { |
| 183 |
$filename = sprintf( 'powered-cache-settings-%s-%s.txt', date( 'Y-m-d' ), uniqid() ); |
| 184 |
$options = serialize( get_option( 'powered_cache_settings' ) ); |
| 185 |
nocache_headers(); |
| 186 |
@header( 'Content-Type: text/plain' ); |
| 187 |
@header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); |
| 188 |
@header( 'Content-Transfer-Encoding: binary' ); |
| 189 |
@header( 'Content-Length: ' . strlen( $options ) ); |
| 190 |
@header( 'Connection: close' ); |
| 191 |
echo $options; |
| 192 |
exit; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* We use redirect after action occurred |
| 198 |
* |
| 199 |
* @since 1.0 |
| 200 |
*/ |
| 201 |
public static function exit_with_redirect() { |
| 202 |
if ( isset( $_REQUEST['wp_http_referer'] ) ) { |
| 203 |
wp_redirect( $_REQUEST['wp_http_referer'] ); |
| 204 |
} else { |
| 205 |
wp_redirect( admin_url( 'admin.php?page=powered-cache' ) ); |
| 206 |
} |
| 207 |
exit; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
} |