Overview

Namespaces

  • Varunsridharan
    • WordPress

Classes

  • Varunsridharan\WordPress\Transient_Api
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 
<?php
/**
 * WordPress Transient API
 * This library provides developers to manage all their Transients with version management.
 *
 * @author    Varun Sridharan <varunsridharan23@gmail.com>
 * @copyright 2018 Varun Sridharan
 * @license   GPLV3 Or Greater
 */

namespace Varunsridharan\WordPress;

if ( ! class_exists( '\Varunsridharan\WordPress\Transient_Api' ) ) {
    /**
     * Class Transient_WP_Api
     *
     * @package Varunsridharan\WordPress
     * @author Varun Sridharan <varunsridharan23@gmail.com>
     * @since 1.0
     */
    class Transient_Api {
        /**
         * _instances
         *
         * @var array
         */
        protected static $_instances = array();

        /**
         * Stores All Options.
         *
         * @var array
         */
        protected $options = array();

        /**
         * Transient_Api constructor.
         *
         * @param array $options
         */
        public function __construct( $options = array() ) {
            $this->options = wp_parse_args( $options, array(
                // Transients
                'transient_version'     => 1.0,
                'transient_auto_delete' => false,
                'transient_surfix'      => '',
                'transient_prefix'      => '',
                // WP DB Options
                'option_auto_delete'    => false,
                'option_version'        => 1.0,
                'option_surfix'         => '',
                'option_prefix'         => '',
                // Global Config.
                'is_option'             => false,
            ) );
        }

        /**
         * @param      $key
         * @param bool $default
         *
         * @return bool|mixed
         */
        protected function option( $key, $default = false ) {
            return ( isset( $this->options[ $key ] ) ) ? $this->options[ $key ] : $default;
        }

        /**
         * Returns a Unique Key.
         *
         * @param string $key
         * @param bool   $is_option
         *
         * @return mixed
         */
        public function key( $key = '', $is_option = false ) {
            return $this->get_key( $this->validate_length( $key ), $is_option );
        }

        /**
         * Returns Key Based on Requirement.
         *
         * @param string $key
         * @param bool   $is_option
         *
         * @return string
         */
        public function get_key( $key = '', $is_option = false ) {
            if ( true === $is_option ) {
                return $this->option( 'option_prefix', '' ) . $key . $this->option( 'option_surfix', '' );
            }

            return $this->option( 'transient_prefix', '' ) . $key . $this->option( 'transient_surfix', '' );
        }

        /**
         * Validates Key Length if key lenght exceeds it will md5 or returns the orginal key
         *
         * @param string $key
         *
         * @return string
         */
        protected function validate_length( $key = '' ) {
            return ( $this->check_length( $key ) === false ) ? $this->validate_length( md5( $key ) ) : $key;
        }

        /**
         * Returns if key is in correct length
         * if Type set to option then it uses $this->option_limit for char limit
         * or uses $this->transient_limit for char limit
         *
         * @param string $key
         *
         * @return bool
         */
        protected function check_length( $key = '' ) {
            if ( true === $this->option( 'is_option' ) ) {
                return ( strlen( $this->get_key( $key ) ) > $this->option( 'option_limit' ) ) ? false : true;
            }

            return ( strlen( $this->get_key( $key ) ) > $this->option( 'transient_limit' ) ) ? false : true;
        }

        /**
         * Adds Given value to db using add_option
         *
         * @param string $key
         * @param mixed  $value
         * @param string $autoload
         *
         * @return bool
         */
        protected function wp_add_option( $key = '', $value = '', $autoload = 'no' ) {
            return \add_option( $key, $value, '', $autoload );
        }

        /**
         * Updates Given value to db using update_option
         *
         * @param string $key
         * @param mixed  $value
         * @param string $autoload
         *
         * @return bool
         */
        protected function wp_update_option( $key = '', $value = '', $autoload = 'no' ) {
            return \update_option( $key, $value, $autoload );
        }

        /**
         * Deletes a given option
         *
         * @param string $key
         *
         * @return bool
         */
        protected function wp_delete_option( $key = '' ) {
            return \delete_option( $key );
        }

        /**
         * Gets An Option From DB
         *
         * @param string $key
         * @param bool   $default
         *
         * @return mixed|void
         */
        protected function wp_get_option( $key = '', $default = false ) {
            return \get_option( $key, $default );
        }

        /**
         * Sets an transient using Transient API In WP
         *
         * @param     $transient
         * @param     $value
         * @param int $expiration
         *
         * @return bool
         */
        protected function wp_set_transient( $transient, $value, $expiration = 0 ) {
            return \set_transient( $transient, $value, $expiration );
        }

        /**
         * Gets an transient using Transient API In WP
         *
         * @param $transient
         *
         * @return mixed
         */
        protected function wp_get_transient( $transient ) {
            return \get_transient( $transient );
        }

        /**
         * Deletes an transient using Transient API In WP
         *
         * @param $transient
         *
         * @return bool
         */
        protected function wp_delete_transient( $transient ) {
            return \delete_transient( $transient );
        }

        /**
         * Returns Version Key.
         *
         * @param string $key
         *
         * @return string
         */
        protected function get_version_key( $key = '' ) {
            return $this->validate_length( $key . '-version' );
        }

        /**
         * Validates If Saved Version is same as in the class version.
         *
         * @param        $value
         * @param string $type
         *
         * @return bool|mixed
         */
        protected function validate_version( $value, $type = '' ) {
            if ( false === $value || empty( $value ) || is_null( $value ) ) {
                return false;
            }

            $key = ( 'option' === $type ) ? 'option_version' : 'transient_version';
            return version_compare( $this->option( $key ), $value, '=' );
        }

        /**
         * @param bool  $key
         * @param array $args
         *
         * @static
         * @return \Varunsridharan\WordPress\Transient_Api
         */
        public static function instance( $key = false, $args = array() ) {
            $key = ( false === $key ) ? static::class : $key;
            if ( ! isset( self::$_instances[ $key ] ) ) {
                self::$_instances[ $key ] = new static( $args );
            }
            return self::$_instances[ $key ];
        }

        /**
         * @param string $key
         * @param string $value
         * @param string $expiry
         *
         * @return mixed
         */
        public function force_set( $key = '', $value = '', $expiry = '' ) {
            if ( true === $this->option( 'is_option' ) ) {
                return $this->update_option( $key, $value, $expiry );
            }
            $this->delete_transient( $key );
            return $this->set_transient( $key, $value, $expiry );
        }

        /**
         * @param        $key
         * @param        $value
         * @param string $status
         *
         * @return bool
         */
        public function update_option( $key, $value, $status = '' ) {
            $key         = $this->key( $key, true );
            $version_key = $this->get_version_key( $key );
            $_status     = $this->wp_update_option( $key, $value, $status );
            $this->wp_update_option( $version_key, $this->option( 'option_version' ), $status );
            return $_status;
        }

        /**
         * @param $_key
         *
         * @return bool
         */
        public function delete_transient( $_key ) {
            $key         = $this->key( $_key, false );
            $version_key = $this->get_version_key( $key );
            if ( $this->wp_delete_transient( $key ) ) {
                return ( $this->wp_delete_transient( $version_key ) ) ? true : false;
            }
            return false;
        }

        /**
         * @param     $_key
         * @param     $value
         * @param int $expiry
         *
         * @return bool
         */
        public function set_transient( $_key, $value, $expiry = 0 ) {
            $key         = $this->key( $_key, false );
            $version_key = $this->get_version_key( $key );
            $_status     = $this->wp_set_transient( $key, $value, $expiry );
            $this->wp_set_transient( $version_key, $this->option( 'option_version' ), $expiry );
            return $_status;
        }

        /**
         * @param string $key
         * @param string $value
         * @param string $expiry
         *
         * @return bool
         */
        public function set( $key = '', $value = '', $expiry = '' ) {
            return ( true === $this->option( 'is_option' ) ) ? $this->set_option( $key, $value, $expiry ) : $this->set_transient( $key, $value, $expiry );
        }

        /**
         * @param        $_key
         * @param        $value
         * @param string $status
         *
         * @return bool
         */
        public function set_option( $_key, $value, $status = '' ) {
            $key         = $this->key( $_key, true );
            $version_key = $this->get_version_key( $key );
            $_status     = $this->wp_add_option( $key, $value, $status );
            $this->wp_add_option( $version_key, $this->option( 'option_version' ), $status );
            return $_status;
        }

        /**
         * @param string $key
         *
         * @return mixed
         */
        public function get( $key = '' ) {
            return ( true === $this->option( 'is_option' ) ) ? $this->get_option( $key ) : $this->get_transient( $key );
        }

        /**
         * @param $_key
         *
         * @return bool|mixed
         */
        public function get_option( $_key ) {
            $key         = $this->key( $_key, true );
            $version_key = $this->get_version_key( $key );
            $version     = $this->wp_get_option( $version_key, true );
            if ( $this->validate_version( $version, 'option' ) === false ) {
                $this->delete_version_issue( $_key );
                return false;
            }
            return $this->wp_get_option( $key );
        }

        /**
         * @param $_key
         *
         * @return bool|mixed
         */
        public function get_transient( $_key ) {
            $key         = $this->key( $_key, false );
            $version_key = $this->get_version_key( $key );
            $version     = $this->wp_get_transient( $version_key );
            if ( $this->validate_version( $version, 'transient' ) === false ) {
                $this->delete_version_issue( $_key, 'transient' );
                return false;
            }
            return $this->wp_get_transient( $key );
        }

        /**
         * @param $key
         *
         * @return bool|void
         */
        public function delete( $key ) {
            return ( true === $this->option( 'is_option' ) ) ? $this->delete_option( $key ) : $this->delete_transient( $key );
        }

        /**
         * @param $_key
         *
         * @return bool
         */
        public function delete_option( $_key ) {
            $key         = $this->key( $_key, true );
            $version_key = $this->get_version_key( $key );
            if ( $this->wp_delete_option( $key ) ) {
                return ( $this->wp_delete_option( $version_key ) ) ? true : false;
            }
            return false;
        }

        /**
         * @param        $key
         * @param string $value
         * @param string $expiry
         *
         * @return bool
         */
        public function update( $key, $value = '', $expiry = '' ) {
            return ( $this->option( 'is_option' ) ) ? $this->update_option( $key, $value, $expiry ) : true;
        }

        /**
         * Deletes if cache has any issues.
         *
         * @param        $key
         * @param string $type
         *
         * @return bool
         */
        protected function delete_version_issue( $key, $type = '' ) {
            if ( true === $this->option( 'option_auto_delete' ) && 'option' === $type ) {
                $this->delete_option( $key );
            }

            if ( true === $this->option( 'transient_auto_delete' ) ) {
                return $this->delete_transient( $key );
            }
            return false;
        }
    }
}
API documentation generated by ApiGen