File: /datos/www/www.colombia.co/public_html/wp-content/plugins/amp-cache/ampforwp-cache-admin.php
<?php
add_filter("redux/options/redux_builder_amp/sections", 'ampforwp_settings_to_cache');
add_filter("redux/options/redux_builder_amp/saved", 'ampforwp_settings_to_cache_save', 10, 2);
function ampforwp_settings_to_cache_save($redux_builder_amp, $this_transients_changed_values){
global $redux_builder_amp;
if($redux_builder_amp['AMPforWP_cache_mode']==1){
update_option('ampforwp_cache_mode','serverrewrite');
//Apache
if(ampforwp_getwebserver()=='apache' && !ampforwp_is_rewrite_ok()){
ampforwp_insert_htaccess(ampforwp_getrewriterule(false));
}
}else{
update_option('ampforwp_cache_mode','close');
}
/* compress mode option removed.
if($redux_builder_amp['AMPforWP_cache_compression']==true){
update_option("ampforwp_cache_compress",true);
}else{
}*/
update_option("ampforwp_cache_compress",false);
if($redux_builder_amp['AMPforWP_cache_mode_cache_Clear']==true){
$_POST['ampforwp_clearcache'] = array('all');
ampforwp_do_update_actions();
$redux_builder_amp['AMPforWP_cache_mode_cache_Clear'] = 0;
update_option("redux_builder_amp",$redux_builder_amp);
}
}
if ( ! function_exists( 'ampforwp_settings_to_cache' ) ) {
function ampforwp_settings_to_cache($sections){
$sections[] = array(
'title' => __( 'AMP Cache', 'accelerated-mobile-pages' ),
'icon' => 'el el-forward',
// 'desc' =>"AMP Cache",
'subsection' => false,
'id' => 'opt-go-amp-cache',
'fields' => amp_cache_fields(),
);
return $sections;
}
}
function amp_cache_fields(){
$contents[] = array(
'id' => 'AMPforWP_cache_mode',
'type' => 'switch',
'title' => __('AMP Cache', 'accelerated-mobile-pages'),
'subtitle' => __('Start the Caching Engine by enabling the button from the right side', 'accelerated-mobile-pages'),
'default' => 0,
'true' => 'Enabled',
'false' => 'Disabled',
'desc' => __( 'This feature make your website much faster in mobile', 'accelerated-mobile-pages' ),
);
/* removed compress mode from the plugin however for future reference Iam committing out the code.
$contents[] = array(
'id' => 'AMPforWP_cache_compression',
'type' => 'switch',
'title' => __('Compression Mode', 'accelerated-mobile-pages'),
'subtitle' => __('Increase the performance with compression mode', 'accelerated-mobile-pages'),
'default' => 0,
'true' => 'Enabled',
'false' => 'Disabled',
'desc' => __( '', 'accelerated-mobile-pages' ),
'required' => array('AMPforWP_cache_mode', '=' , '1'),
);*/
$contents[] = array(
'id' => 'AMPforWP_cache_time_mode',
'type' => 'switch',
'title' => __('Auto clear cache?', 'accelerated-mobile-pages'),
'default' => 0,
'true' => 'Enabled',
'false' => 'Disabled',
'required' => array('AMPforWP_cache_mode', '=' , '1'),
);
$contents[] = array(
'id' => 'auto-clear-cache-per-hours',
'title' => __('Set the Time Interval', 'accelerated-mobile-pages'),
'type' => 'text',
'default' => '12',
'class' => 'child_opt child_opt_arrow',
'desc' => 'Set the Time Interval in Hours only in numerics ( eg: 12 ).',
'required'=>array(
array('AMPforWP_cache_time_mode','=','1'),
)
);
$contents[] = array(
'id' => 'AMPforWP_home_cache',
'type' => 'switch',
'title' => __('Cache Homepage?', 'accelerated-mobile-pages'),
'default' => 1,
'true' => 'Enabled',
'false' => 'Disabled',
'tooltip-subtitle' => __('Enable/Disable Cache support on amp homepage', 'accelerated-mobile-pages'),
'required' => array('AMPforWP_cache_mode', '=' , '1'),
);
$contents[]= array(
'id' => 'AMPforWP_cache_mode_cache_Clear',
'type' => 'checkbox',
'required' => array('AMPforWP_cache_mode', '=' , '1'),
'title' => __('Want to Clear Cache?', 'accelerated-mobile-pages'),
'subtitle' => __('', 'accelerated-mobile-pages'),
'desc' => __('To Clear the Cache, select the above checkbox and then hit save.', 'accelerated-mobile-pages'),
'default' => '0'// 1 = on | 0 = off
);
return $contents;
}
function ampforwp_action_links($links,$pluginfile){
if($pluginfile == 'ampforwp-cache/ampforwp-cache.php'){
$link=array(
'<a href="'. get_admin_url(null, 'options-general.php?page=amp-cache') .'">'.__('Settings','ampforwp_cache').'</a>'
);
$links = array_merge($link, $links);
}
return $links;
}
add_filter('plugin_action_links', 'ampforwp_action_links',10,2);
function ampforwp_ssc_row_meta($links,$pluginfile){
if($pluginfile == 'ampforwp-cache/ampforwp-cache.php'){
$link=array(
'<a href="'. get_admin_url(null, 'options-general.php?page=amp-cache') .'">'.__('Settings','ampforwp_cache').'</a>'
);
$links = array_merge($links,$link);
}
return $links;
}
add_filter('plugin_row_meta','ampforwp_ssc_row_meta',10,2);
function ampforwp_is_rewrite_ok(){
global $ampforwpc;
if($ampforwpc->iscompress){
if(@fopen($ampforwpc->siteurl."/rewrite_ok.html","r")){
return true;
}
}else{
if(@fopen($ampforwpc->siteurl."/rewrite_ok.txt","r")){
return true;
}
}
return false;
}
function ampforwp_getwebserver(){
$software=strtolower($_SERVER["SERVER_SOFTWARE"]);
switch ($software){
case strstr($software,"nginx"):
return "nginx";
break;
case strstr($software,"apache"):
return "apache";
break;
case strstr($software,"iis"):
return "iis";
break;
default:
return "unknown";
}
}
function ampforwp_getwpinstallpath(){
global $ampforwpc;
return "/".substr($ampforwpc->wppath,strlen($ampforwpc->docroot));
}
function ampforwp_insert_htaccess($rule){
global $ampforwpc;
$raw_rules = file_get_contents($ampforwpc->wpabspath.".htaccess");
if (strpos($raw_rules, '#BEGIN ampforwpCache') !== false) {
return true;
}
$raw_rules = preg_replace('/#BEGIN\sampforwp\sCache(.*?)#End\sampforwp\sCache/si', "", $raw_rules);
$rules = $rule."\n".$raw_rules;
if(!file_put_contents($ampforwpc->wpabspath.".htaccess",$rules,LOCK_EX)) return false;
return true;
}
function ampforwp_getrewriterule($escape=true){
if(ampforwp_is_rewrite_ok()) return false;
$cachemod=get_option("ampforwp_cache_mode");
$compress=get_option("ampforwp_cache_compress");
$webscr=ampforwp_getwebserver();
$rules=false;
$httpdcompressflushreule="\n<IfModule mod_headers.c>\nAddType application/x-gzip .gz .tgz\nAddEncoding x-gzip .gz .tgz\n<IfModule mod_deflate.c>\n# Insert filters\nAddOutputFilterByType DEFLATE text/plain\nAddOutputFilterByType DEFLATE text/html\nAddOutputFilterByType DEFLATE text/xml\nAddOutputFilterByType DEFLATE text/css\nAddOutputFilterByType DEFLATE application/xml\nAddOutputFilterByType DEFLATE application/xhtml+xml\nAddOutputFilterByType DEFLATE application/rss+xml\nAddOutputFilterByType DEFLATE application/javascript\nAddOutputFilterByType DEFLATE application/x-javascript\nAddOutputFilterByType DEFLATE application/x-httpd-php\nAddOutputFilterByType DEFLATE application/x-httpd-fastphp\nAddOutputFilterByType DEFLATE image/svg+xml\n</IfModule>\n\n# Drop problematic browsers\nBrowserMatch ^Mozilla/4 gzip-only-text/html\nBrowserMatch ^Mozilla/4\.0[678] no-gzip\nBrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html\n\n# Make sure proxies donot deliver the wrong content\nHeader append Vary User-Agent env=!dont-vary\n\n\n<FilesMatch \"\.(html|txt)\.gz$\">\nheader set Content-Encoding gzip\nheader set Content-Type text/html\n</FilesMatch>\n</IfModule>\n#End ampforwp Cache\n";
$httpdrewriterule="#BEGIN ampforwp Cache\n#Must the First Rewrite Rule\n<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase {wp_install_dir}\nRewriteRule ^ampforwp-cache/ - [L]\n\nRewriteCond %{REQUEST_METHOD} !POST\nRewriteCond %{QUERY_STRING} !.*=.*\nRewriteCond %{DOCUMENT_ROOT}{wp_install_dir}ampforwp-cache/$1{iscompressed} -f\nRewriteRule ^(.*)$ {wp_install_dir}ampforwp-cache/$1{iscompressed} [L]\n\nRewriteCond %{REQUEST_METHOD} !POST\nRewriteCond %{QUERY_STRING} !.*=.*\nRewriteCond %{DOCUMENT_ROOT}{wp_install_dir}ampforwp-cache/$1/index.html{iscompressed} -f\nRewriteRule ^(.*)$ {wp_install_dir}ampforwp-cache/$1/index.html{iscompressed} [L]\n</IfModule>\n## EXPIRES CACHING ## \n<IfModule mod_expires.c>\nExpiresActive On\nExpiresByType image/jpg 'access 1 year'\nExpiresByType image/jpeg 'access 1 year'\nExpiresByType image/gif 'access 1 year'\nExpiresByType image/png 'access 1 year'\nExpiresByType text/css 'access 1 month'\nExpiresByType application/pdf 'access 1 month'\nExpiresByType application/javascript 'access 1 month'\nExpiresByType application/x-javascript 'access 1 month'\nExpiresByType application/x-shockwave-flash 'access 1 month'\nExpiresByType image/x-icon 'access 1 year'\nExpiresDefault 'access 2 days'\n</IfModule>\n## EXPIRES CACHING ##";
$nginxcompressstatic = "#ngx_http_gzip_static_module and ngx_http_gunzip_module Must Be Added To Nginx\n gzip_static always;\n gunzip on;\n";
$nginxrewriterule='#BEGIN ampforwp Cache
location {wp_install_dir} {
{wp_gzip_static_flush}
if (-f $request_filename) {
break;
}
if ($uri ~ {wp_install_dir}(.*)$){
set $wpuri $1;
set $sscfile $document_root{wp_install_dir}ampforwp-cache/$1;
}
set $ssc Y;
if ($query_string !~ .*=.*){
set $ssc "${ssc}Y";
}
if ($request_method != "POST"){
set $ssc "${ssc}Y";
}
if (-f $sscfile{iscompressed}){
set $ssc "${ssc}F";
}
if (-f $sscfile/index.html{iscompressed}){
set $ssc "${ssc}I";
}
if ($ssc = YYYF){
rewrite . {wp_install_dir}ampforwp-cache/$wpuri break;
}
if ($ssc = YYYI){
rewrite . {wp_install_dir}ampforwp-cache/$wpuri/index.html break;
}
if (!-e $request_filename){
rewrite . {wp_install_dir}index.php last;
}
}
#End ';
//Apache
if($cachemod == 'serverrewrite' && $webscr == 'apache' && $compress == true){
$httpdrewriterule=str_replace('{wp_install_dir}',ampforwp_getwpinstallpath(),$httpdrewriterule);
$httpdrewriterule=str_replace('{iscompressed}','.gz',$httpdrewriterule);
if($escape){
$rules=htmlentities($httpdrewriterule.$httpdcompressflushreule);
}else{
$rules=$httpdrewriterule.$httpdcompressflushreule;
}
}else if($cachemod == 'serverrewrite' && $webscr == 'apache' && $compress == false){
$httpdrewriterule=str_replace('{wp_install_dir}',ampforwp_getwpinstallpath(),$httpdrewriterule);
$httpdrewriterule=str_replace('{iscompressed}','',$httpdrewriterule);
if($escape){
$rules=htmlentities($httpdrewriterule.$httpdcompressflushreule);
}else{
$rules=$httpdrewriterule.$httpdcompressflushreule;
}
}
//Nginx
else if($cachemod == 'serverrewrite' && $webscr == 'nginx' && $compress == true){
$nginxrewriterule=str_replace('{wp_install_dir}',ampforwp_getwpinstallpath(),$nginxrewriterule);
$nginxrewriterule=str_replace('{iscompressed}','.gz',$nginxrewriterule);
$nginxrewriterule=str_replace('{wp_gzip_static_flush}',$nginxcompressstatic,$nginxrewriterule);
if($escape){
$rules=htmlentities($nginxrewriterule);
}else{
$rules=$nginxrewriterule;
}
}else if($cachemod == 'serverrewrite' && $webscr == 'nginx' && $compress == false){
$nginxrewriterule=str_replace('{wp_install_dir}',ampforwp_getwpinstallpath(),$nginxrewriterule);
$nginxrewriterule=str_replace('{iscompressed}','',$nginxrewriterule);
$nginxrewriterule=str_replace('{wp_gzip_static_flush}','',$nginxrewriterule);
if($escape){
$rules=htmlentities($nginxrewriterule);
}else{
$rules=$nginxrewriterule;
}
}else {
$rules = __('Your server type is not detected, Please visit ampforwp website for help.','ampforwp-cache');
}
return $rules;
}
function ampforwp_clear_post_cache($args){
global $ampforwpc,$wpdb;
if($args*1 > 0){
$link=get_permalink($args);
$ampforwpc->ampforwp_delete_cache($link);
}else if(strlen($args) > 2){
$postres=$wpdb->get_results("SELECT `ID` FROM `" . $wpdb->posts . "` WHERE post_title like '%".$args."%' LIMIT 0,1 ");
$link = get_permalink($postres[0]->ID);
$ampforwpc->ampforwp_delete_cache($link);
}
}
function ampforwp_clear_category_cache(){
global $ampforwpc,$wpdb;
$sql="SELECT $wpdb->terms.term_id, name FROM $wpdb->terms LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id WHERE $wpdb->term_taxonomy.taxonomy = 'category'";
$cateobj = $wpdb->get_results($sql);
foreach($cateobj as $cate){
$link=get_category_link($cate->term_id);
$wpssc->ampforwp_delete_cache($link);
}
}
function ampforwp_clear_tag_cache(){
global $ampforwpc;
$tagobj=get_tags();
foreach($tagobj as $tag){
$link=get_tag_link($tag->term_id);
$wpssc->ampforwp_delete_cache($link);
}
}
function ampforwp_clear_post_page_cache($type){
global $ampforwpc,$wpdb;
$postres=$wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type='".$type."' and post_status='publish'");
foreach($postres as $post){
$link=get_permalink($post->ID);
$wpssc->ampforwp_delete_cache($link);
}
}
function ampforwp_clear_home_cache(){
global $ampforwpc;
$ampforwpc->ampforwp_delete_cache($ampforwpc->siteurl."/index.html");
}
function ampforwp_do_update_actions(){
if(empty($_POST)) return;
if(!empty($_POST['ampforwp_cache_mode'])){
$ampforwp_cache_mode=trim($_POST['ampforwp_cache_mode']);
update_option('ampforwp_cache_mode',$ampforwp_cache_mode);
//No Cache Page: Possible answers
//home, single, page, category, tag, author, date, attachment
$ampforwp_cache_excet_arr= array('author', 'date', 'attachment');
$ampforwp_cache_excet = implode(',',$ampforwp_cache_excet_arr);
update_option('ampforwp_cache_excet',$ampforwp_cache_excet);
$ampforwp_cache_strict = false;
update_option('ampforwp_cache_strict',$ampforwp_cache_strict);
$ampforwp_cache_nocachesinglepage = str_replace("\r\n",",",'');/*$_POST['ampforwp_cache_nocachesinglepage']*/
update_option('ampforwp_cache_nocachesinglepage',$ampforwp_cache_nocachesinglepage);
//Apache
if(ampforwp_getwebserver()=='apache' && !ampforwp_is_rewrite_ok()){
ampforwp_insert_htaccess(ampforwp_getrewriterule(false));
}
}
if(!empty($_POST['ampforwp_cache_compress'])){
$ampforwp_cache_compress = false;
update_option('ampforwp_cache_compress',$ampforwp_cache_compress);
}
//if(!empty($_POST['update_cache_action_submit'])){
//possible answers
//publish_post, post_updated, trashed_post, publish_page, 'update_cache_action','comment_unapproved_to_approved', comment_approved_to_trash, comment_approved_to_spam
$update_cache_action_arr=array('publish_post','post_updated','trashed_post','publish_page');
$update_cache_action=implode(',',$update_cache_action_arr);
update_option('ampforwp_update_cache_action',$update_cache_action);
//}
if(!empty($_POST['ampforwp_clearcache'])){
$del_type_arr=$_POST['ampforwp_clearcache'];
if(in_array('home',$del_type_arr)){
ampforwp_clear_home_cache();
}
if(in_array('single',$del_type_arr)){
ampforwp_clear_post_page_cache("post");
}
if(in_array('page',$del_type_arr)){
ampforwp_clear_post_page_cache("page");
}
if(in_array('category',$del_type_arr)){
ampforwp_clear_category_cache();
}
if(in_array('tag',$del_type_arr)){
ampforwp_clear_tag_cache();
}
if(in_array('all',$del_type_arr)){
global $ampforwpc;
if($ampforwpc->cachemod=="serverrewrite"){
$ampforwpc->ampforwp_delete_cache($ampforwpc->siteurl."/");
}
}
}
if(!empty($_POST['clearpostpagecache'])){
$ids=explode(",",trim($_POST['clearpostpagecache']));
foreach($ids as $id){
ampforwp_clear_post_cache($id);
}
}
}