php [瞬态API设置] #PHP #WordPress

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [瞬态API设置] #PHP #WordPress相关的知识,希望对你有一定的参考价值。

<?php
/**
 *
 * Transients API settings
 *
 */

namespace THEME_NAME;

class Transients
{
    /**
     * Constructor
     */
    public function __construct()
    {
        // Register action/filter callbacks
        add_action('admin_init', [$this, 'action_admin_init']);
    }

    /**--------------------------------------------------------------------------------
     * Action callbacks
     * --------------------------------------------------------------------------------*/
    /**
     * action: 'admin_init'
     * @return [type] [description]
     */
    public function action_admin_init()
    {
      	// Delete All cache when accessing management screen
        $this->delete_transient();
    }

    /**--------------------------------------------------------------------------------
     * Filter callbacks
     * --------------------------------------------------------------------------------*/

    /**--------------------------------------------------------------------------------
     * Utility methods
     * --------------------------------------------------------------------------------*/
    /**
     * Delete cache
     */
    public function delete_transient()
    {
        $is_logged_in = is_user_logged_in();

        if ((!defined('DOING_AJAX'))) {
            foreach (THEME_TRANSIENT_KEYS as $key) {
                delete_transient($key);
                if ($is_logged_in) {
                    delete_transient(str_replace(THEME_TRANSIENT_ADMIN_SUFFIX, '', $key));
                } else {
                    delete_transient($key . THEME_TRANSIENT_ADMIN_SUFFIX);
                }
            }
        }
    }
}
<?php
/**
 *
 * Constant setting
 *
 */

/**
 * Set suffix so that key changes at login and not
 */
const THEME_TRANSIENT_ADMIN_SUFFIX = '_admin';
$transient_key_suffix = is_user_logged_in() ? THEME_TRANSIENT_ADMIN_SUFFIX : '';

/**
 * Transients Keys
 */
const THEME_TRANSIENT_KEYS = [
  'post_query' => 'theme_post_query'.$transient_key_suffix,
];
<?php
/**
 *
 * functions and definitions
 *
 */

require_once 'inc/constant.php';
require_once 'inc/Transients.php';

new THEME_NAME\Transients();

以上是关于php [瞬态API设置] #PHP #WordPress的主要内容,如果未能解决你的问题,请参考以下文章

php WP瞬态 - 完全瞬态代码

php WordPress瞬态缓存

php 针对缓存插件处理插件瞬态。

php 显示保存为瞬态的导航菜单的功能

php ACF:Ukázkapráce的瞬态

php Swagger设置api键