Plugin Directory

Changeset 2048076


Ignore:
Timestamp:
03/11/2019 10:34:34 AM (7 years ago)
Author:
grfstudio
Message:

Finishing previous commit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-admin-cache/trunk/index.php

    r2046678 r2048076  
    11<?php
     2
    23/*
    3 Plugin Name: WP Admin Cache
    4 Plugin URI: https://www.wpadmincache.com
    5 Description: The first cache plugin for WordPress admin area
    6 Version: 0.1.1
    7 Author: Grf Studio
    8 Author URI: https://www.grfstudio.com
    9 Text Domain: wp-admin-cache
    10 Domain Path: /languages/
    11 License:
    12 */
     4  Plugin Name: WP Admin Cache
     5  Plugin URI: https://www.wpadmincache.com
     6  Description: The first cache plugin for WordPress admin area
     7  Version: 0.1.2
     8  Author: Grf Studio
     9  Author URI: https://www.grfstudio.com
     10  Text Domain: wp-admin-cache
     11  Domain Path: /languages/
     12  License:
     13 */
    1314
    14 if ( !function_exists( 'add_action' ) ) {
    15     exit;
     15if (!function_exists('add_action')) {
     16    exit;
    1617}
    1718
    1819class AdminCache {
    1920
    20     private $settings;
    21     private $beginStarted=false;
    22     private $currentCaching='';
     21    private $settings;
     22    private $beginStarted = false;
     23    private $currentCaching = '';
    2324
    24     function __construct() {
    25         if(!is_admin())return;
    26         $this->settings = json_decode(get_option('wp_admin_cache_settings'), true);     
    27         add_action('admin_menu', array($this, 'init'));
    28         add_action('admin_print_footer_scripts', array($this, 'writeScripts'));
    29         if($this->settings['enabled']){
    30             $this->begin();
    31             $this->autoPurgeCache();       
    32         }
    33     }
     25    function __construct() {
     26        if (!is_admin()) return;
     27        $this->settings = json_decode(get_option('wp_admin_cache_settings'), true);
     28        add_action('admin_menu', array($this, 'init'));
     29        add_action('admin_print_footer_scripts', array($this, 'writeScripts'));
     30        if ($this->settings['enabled']) {
     31            $this->begin();
     32            $this->autoPurgeCache();
     33        }
     34    }
    3435
    35     function init(){
    36         add_options_page( 'WP Admin Cache', 'WP Admin Cache', 'manage_options', 'wp-admin-cache', array($this,'options_page') );   
    37         wp_enqueue_script( 'wp-admin-cache-script', plugin_dir_url( __FILE__ ).'index.js' );
    38         wp_enqueue_style('wp-admin-cache-style', plugin_dir_url( __FILE__ ).'index.css');
    39         $session=wp_get_session_token();
    40         if(!isset($_COOKIE['wp-admin-cache-session']) || $_COOKIE['wp-admin-cache-session']!=$session)setcookie('wp-admin-cache-session', $session, 0, admin_url());
    41     }
     36    function init() {
     37        add_options_page('WP Admin Cache', 'WP Admin Cache', 'manage_options', 'wp-admin-cache', array($this, 'options_page'));
     38        wp_enqueue_script('wp-admin-cache-script', plugin_dir_url(__FILE__) . 'index.js');
     39        wp_enqueue_style('wp-admin-cache-style', plugin_dir_url(__FILE__) . 'index.css');
     40        $session = wp_get_session_token();
     41        if (!isset($_COOKIE['wp-admin-cache-session']) || $_COOKIE['wp-admin-cache-session'] != $session) setcookie('wp-admin-cache-session', $session, 0, admin_url());
     42    }
    4243
    43     function writeScripts(){
    44         echo '<script>';
    45         foreach($this->getEnabledUrls() as $url)echo 'wp_cache_prefetch("'.$url.'"); ';
    46         echo '</script>';
    47     }
     44    function writeScripts() {
     45        echo '<script>';
     46        foreach ($this->getEnabledUrls() as $url) echo 'wp_cache_prefetch("' . $url . '"); ';
     47        echo '</script>';
     48    }
    4849
    49     function movePluginAtTop()
    50     {
    51         $path = str_replace( WP_PLUGIN_DIR . '/', '', __FILE__ );
    52         if ( $plugins = get_option( 'active_plugins' ) ) {
    53             if ( $key = array_search( $path, $plugins ) ) {
    54                 array_splice( $plugins, $key, 1 );
    55                 array_unshift( $plugins, $path );
    56                 update_option( 'active_plugins', $plugins );
    57             }
    58         }
    59     }
     50    function movePluginAtTop() {
     51        $path = str_replace(WP_PLUGIN_DIR . '/', '', __FILE__);
     52        if ($plugins = get_option('active_plugins')) {
     53            if ($key = array_search($path, $plugins)) {
     54                array_splice($plugins, $key, 1);
     55                array_unshift($plugins, $path);
     56                update_option('active_plugins', $plugins);
     57            }
     58        }
     59    }
    6060
    61     function options_page(){
    62         include_once 'settings.php';
    63     }
     61    function options_page() {
     62        include_once 'settings.php';
     63    }
    6464
    65     function getToken(){
    66         if(isset($_COOKIE['wp-admin-cache-session']))return $_COOKIE['wp-admin-cache-session'];
    67         return '';
    68     }
     65    function getToken() {
     66        if (isset($_COOKIE['wp-admin-cache-session'])) return $_COOKIE['wp-admin-cache-session'];
     67        return '';
     68    }
    6969
    70     function autoPurgeCache(){
    71         add_action('activated_plugin', array($this, 'purgeCache'));
    72         add_action('deactivated_plugin', array($this, 'purgeCache'));           
    73         add_action('wp_insert_post', array($this, 'purgeCache'));
    74         add_filter('widget_update_callback', array($this, 'widget_update_callback'),10,3);
    75     }
     70    function autoPurgeCache() {
     71        add_action('activated_plugin', array($this, 'purgeCache'));
     72        add_action('deactivated_plugin', array($this, 'purgeCache'));
     73        add_action('wp_insert_post', array($this, 'purgeCache'));
     74        add_filter('widget_update_callback', array($this, 'widget_update_callback'), 10, 3);
     75        add_action('upgrader_process_complete', array($this, 'upgrader_callback'), 10, 2);
     76    }
    7677
    77     function purgeCache(){
    78         foreach($this->getEnabledUrls() as $url){
    79             delete_transient('wp-admin-cached-'.$this->getToken().$url);
    80         }
    81     }
     78    function purgeCache() {
     79        foreach ($this->getEnabledUrls() as $url) {
     80            delete_transient('wp-admin-cached-' . $this->getToken() . $url);
     81        }
     82    }
    8283
    83     function widget_update_callback( $array ) {
    84         $this->purgeCache();
    85         return $array;
    86     }
     84    function widget_update_callback($array) {
     85        $this->purgeCache();
     86        return $array;
     87    }
    8788
    88     function begin() {
    89         if($this->beginStarted)return;
    90         $token=$this->getToken();
    91         if($token=='')return;
    92         $this->beginStarted=true;
    93         $currentPage= add_query_arg( NULL, NULL );
    94         $currentPage=explode('/',$currentPage);
    95         $currentPage=$currentPage[count($currentPage)-1];
    96         if (in_array($currentPage,$this->getEnabledUrls())) {
    97             $tName='wp-admin-cached-'.$token.$currentPage;
    98             $content = get_transient($tName);
    99             if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    100                 $this->purgeCache();
    101                 return;
    102             }
    103             if ($content === false) {
    104                 $this->currentCaching=$tName;
    105                 ob_start(array($this,'end'));
    106             }else{
    107                 if(isset($this->settings['show-label']) && $this->settings['show-label']=='1'){
    108                     echo str_replace('</body>','<div class="wp-admin-cache-label">cached page</div><!--wp-admin-cached--></body>',$content);                   
    109                 }else{
    110                     echo str_replace('</body>','<!--wp-admin-cached--></body>',$content);
    111                 }
    112                 die();
    113             }
    114         }
    115     }
     89    function upgrader_callback($upgrader_object, $options) {
     90        $this->purgeCache();
     91    }
    11692
    117     function end($content){
    118         $duration=$this->settings['duration'];
    119         if($duration=='')$duration='5';
    120         set_transient($this->currentCaching, $content, 60*$duration);
    121         return $content;
    122     }
     93    function begin() {
     94        if ($this->beginStarted) return;
     95        $token = $this->getToken();
     96        if ($token == '') return;
     97        $this->beginStarted = true;
     98        $currentPage = add_query_arg(NULL, NULL);
     99        $currentPage = explode('/', $currentPage);
     100        $currentPage = $currentPage[count($currentPage) - 1];
     101        if (in_array($currentPage, $this->getEnabledUrls())) {
     102            $tName = 'wp-admin-cached-' . $token . $currentPage;
     103            $content = get_transient($tName);
     104            if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     105                $this->purgeCache();
     106                return;
     107            }
     108            if ($content === false) {
     109                $this->currentCaching = $tName;
     110                ob_start(array($this, 'end'));
     111            } else {
     112                if (isset($this->settings['show-label']) && $this->settings['show-label'] == '1') {
     113                    echo str_replace('</body>', '<div class="wp-admin-cache-label">cached page</div><!--wp-admin-cached--></body>', $content);
     114                } else {
     115                    echo str_replace('</body>', '<!--wp-admin-cached--></body>', $content);
     116                }
     117                die();
     118            }
     119        }
     120    }
    123121
    124     function getPageHooks(){
    125         global $admin_page_hooks;
    126         $a=array();
    127         foreach($admin_page_hooks as $key=>$value)
    128         {
    129             if(strpos($key,'.php')!==false && strpos($key,'/')===false) array_push($a,$key);
    130         }
    131         $args=array('show_ui'=>true);
    132         foreach(get_post_types($args) as $type)
    133         {
    134             $url='edit.php?post_type='.$type;
    135             if(!in_array($url,$a))array_push($a,$url);
    136         }
    137         array_push($a,'widgets.php');
    138         sort($a);
    139         return $a;
    140     }
     122    function end($content) {
     123        $duration = $this->settings['duration'];
     124        if ($duration == '') $duration = '5';
     125        set_transient($this->currentCaching, $content, 60 * $duration);
     126        return $content;
     127    }
    141128
    142     function getEnabledUrls(){
    143         $urls=$this->settings['enabled-urls'];
    144         return $urls;
    145     }
     129    function getPageHooks() {
     130        global $admin_page_hooks;
     131        $a = array();
     132        foreach ($admin_page_hooks as $key => $value) {
     133            if (strpos($key, '.php') !== false && strpos($key, '/') === false) array_push($a, $key);
     134        }
     135        $args = array('show_ui' => true);
     136        foreach (get_post_types($args) as $type) {
     137            $url = 'edit.php?post_type=' . $type;
     138            if (!in_array($url, $a)) array_push($a, $url);
     139        }
     140        array_push($a, 'widgets.php');
     141        sort($a);
     142        return $a;
     143    }
     144
     145    function getEnabledUrls() {
     146        $urls = $this->settings['enabled-urls'];
     147        return $urls;
     148    }
    146149
    147150}
Note: See TracChangeset for help on using the changeset viewer.