caching_code_to_add();
}else{
$this->remove_htaccess_code();
}
}
public static function getInstance() {
if ( null == self::$instance ) {
self::$instance = new self;
self::$plugin = Plugin::getInstance();
self::$instance->doHooks();
}
return self::$instance;
}
public function doHooks(){
add_action('wp_ajax_save_advanced_rules_options', array($this,'advanced_rules_options'));
add_action('wp_ajax_get_advanced_rules_options', array($this,'send_advanced_rules_options'));
add_action('wp_ajax_get_advancerule_selected_tab', array($this,'get_advancerule_selected_tab'));
}
public function get_advancerule_selected_tab(){
if (!is_user_logged_in() || !current_user_can('manage_options')) {
wp_send_json_error(['message' => 'Unauthorized access.'], 403);
return;
}
$tab = sanitize_text_field($_POST['tab']);
if($tab === 'undefined'){
$tab_value = get_option('smack_advance_tab');
if(empty($tab_value)){
$tab_name = 'cachequerystrings';
update_option('smack_advance_tab',$tab_name);
}else{
update_option('smack_advance_tab',$tab_value);
}
}else{
update_option('smack_advance_tab',$tab);
}
$tab_address = get_option('smack_advance_tab');
$result['tab'] = $tab_address;
$result['success'] = true;
echo wp_json_encode($result);
wp_die();
}
public function advanced_rules_options(){
if (!is_user_logged_in() || !current_user_can('manage_options')) {
wp_send_json_error(['message' => 'Unauthorized access.'], 403);
return;
}
if(isset($_POST)){
$never_cache_path = sanitize_text_field($_POST['excluded_page_paths']);
$never_cache_ids = sanitize_text_field($_POST['excluded_post_ids']);
$never_cache_slugs = sanitize_text_field($_POST['excluded_post_slugs']);
$cache_query_strings = sanitize_text_field($_POST['excluded_query_strings']);
$never_cache_cookies = sanitize_text_field($_POST['excluded_cookies']);
update_option('smack_never_cache_path',$never_cache_path);
update_option('smack_never_cache_ids',$never_cache_ids);
update_option('smack_never_cache_slugs',$never_cache_slugs);
update_option('smack_cache_query_strings',$cache_query_strings);
update_option('smack_never_cache_cookies',$never_cache_cookies);
}
$result['success'] = true;
ClearCache::get_setting();
echo wp_json_encode($result);
wp_die();
}
public function send_advanced_rules_options(){
if (!is_user_logged_in() || !current_user_can('manage_options')) {
wp_send_json_error(['message' => 'Unauthorized access.'], 403);
return;
}
$never_cache_path=get_option('smack_never_cache_path');
$never_cache_ids=get_option('smack_never_cache_ids');
$never_cache_slugs=get_option('smack_never_cache_slugs');
$cache_query_strings=get_option('smack_cache_query_strings');
$never_cache_cookies=get_option('smack_never_cache_cookies');
$result['excluded_page_paths']= !empty($never_cache_path) ? $never_cache_path : '';
$result['excluded_post_ids']= !empty($never_cache_ids) ? $never_cache_ids : '';
$result['excluded_post_slugs']= !empty($never_cache_slugs) ? $never_cache_slugs : '';
$result['excluded_query_strings']= !empty($cache_query_strings) ? $cache_query_strings : '';
$result['excluded_cookies']= !empty($never_cache_cookies) ? $never_cache_cookies : '';
$result['success'] = true;
echo wp_json_encode($result);
wp_die();
}
public function caching_code_to_add() {
$this->htaccess_file = wp_normalize_path( ABSPATH . '.htaccess' );
if ( file_exists( $this->htaccess_file ) ) {
if ( is_readable( $this->htaccess_file ) && is_writable( $this->htaccess_file ) ) {
$this->unique_string = 'LBROWSERCSTART';
$this->htaccess_code = file_get_contents( $this->htaccess_file );
$this->valid = false;
if ( strpos( $this->htaccess_code, $this->unique_string ) !== false ) {
$this->valid = true;
}
if ( ! $this->valid ) {
$this->htaccess_code = $this->htaccess_code . $this->code_to_add();
file_put_contents( $this->htaccess_file, $this->htaccess_code );
}
} else {
add_action( 'admin_notices', array( $this, 'no_htaccess_access_notice' ) );
}
} else {
add_action( 'admin_notices', array( $this, 'no_htaccess_notice' ) );
}
}
public function remove_htaccess_code() {
$this->htaccess_file = wp_normalize_path( ABSPATH . '.htaccess' );
if ( file_exists( $this->htaccess_file ) ) {
if ( is_readable( $this->htaccess_file ) && is_writable( $this->htaccess_file ) ) {
$this->unique_string = 'LBROWSERCSTART';
$this->htaccess_code = file_get_contents( $this->htaccess_file );
$this->valid = false;
if ( strpos( $this->htaccess_code, $this->unique_string ) !== false ) {
$this->valid = true;
}
if ( $this->valid ) {
// Code found, remove them.
$this->pattern = '/#\s?LBROWSERCSTART.*?LBROWSERCEND/s';
$this->htaccess_code = preg_replace( $this->pattern, '', $this->htaccess_code );
$this->htaccess_code = preg_replace( "/\n+/","\n", $this->htaccess_code );
file_put_contents( $this->htaccess_file, $this->htaccess_code );
// Bye Bye.
}
}
}
}
/**
* Codes to be add.
*/
public function code_to_add() {
$this->htaccess_code = "\n";
$this->htaccess_code .= '# LBROWSERCSTART Browser Caching' . "\n";
$this->htaccess_code .= '
'; $this->message .= __( 'Plugin Leverage Browser Caching: htaccess file not found. This plugin works only for Apache server. If you are using Apace server, please create it.', 'lbrowserc' ); $this->message .= '
'; $this->message .= __( 'Plugin Leverage Browser Caching: htaccess file is not readable or writable. Please change permission of htaccess file.', 'lbrowserc' ); $this->message .= '