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