Overview

Namespaces

  • Varunsridharan
    • WordPress

Classes

  • Varunsridharan\WordPress\Plugin_Version_Management
  • 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: 
<?php

namespace Varunsridharan\WordPress;

if ( ! class_exists( '\Varunsridharan\WordPress\Plugin_Version_Management' ) ) {
    /**
     * Class Plugin_Version_Management
     *
     * @package Varunsridharan\WordPress
     * @author Varun Sridharan <varunsridharan23@gmail.com>
     * @since 1.0
     */
    class Plugin_Version_Management {
        /**
         * Default Database Key.
         *
         * @var string
         */
        private $db_key = '_vs_wp_plugin_upgrader';

        /**
         * Data From DB.
         *
         * @var array
         */
        protected $db = false;

        /**
         * Stores Plugin Slug.
         *
         * @var bool
         */
        protected $slug = false;

        /**
         * Stores New Plugin Version.
         *
         * @var bool
         */
        protected $new_v = false;

        /**
         * Stores Version & Its Callback
         *
         * @example  array('1.0' => 'plugin_v1_upgrade')
         * @var array
         */
        protected $versions = array();

        /**
         * @var array
         */
        protected $callbacks = array();

        /**
         * Custom Option Name To Save Information in DB.
         *
         * @var string
         */
        protected $option_name = '';

        /**
         * Set True To Save Upgrade Log.
         *
         * @var bool
         */
        protected $logs = false;

        /**
         * Plugin_Version_Management constructor.
         *
         * @param array $config
         * @param array $versions
         */
        public function __construct( $config = array(), $versions = array() ) {
            $args              = wp_parse_args( $config, array(
                'slug'        => false,
                'version'     => false,
                'logs'        => true,
                'option_name' => true,
            ) );
            $this->slug        = $args['slug'];
            $this->new_v       = $args['version'];
            $this->option_name = ( empty( $args['option_name'] ) ) ? true : $args['option_name'];
            $this->logs        = $args['logs'];
            $this->versions    = array_keys( $versions );
            $this->callbacks   = $versions;
            sort( $this->versions );
            $this->get_db_values();
        }

        /**
         * Returns Current Plugin's Version.
         *
         * @return bool|mixed
         */
        public function version() {
            return ( isset( $this->db['version'] ) ) ? $this->db['version'] : false;
        }

        /**
         * Returns Plugin's Activation Logs.
         *
         * @return bool|mixed
         */
        public function logs() {
            return ( isset( $this->db['logs'] ) ) ? $this->db['logs'] : false;
        }

        /**
         * Retrives Saved Values From DB.
         */
        protected function get_db_values() {
            if ( empty( $this->db ) ) {
                $option   = ( true === $this->option_name ) ? $this->db_key : $this->option_name;
                $this->db = get_option( $option, false );
                $this->db = ( ! is_array( $this->db ) ) ? array() : $this->db;

                if ( true === $this->option_name ) {
                    $this->db = ( isset( $this->db[ $this->slug ] ) ) ? $this->db[ $this->slug ] : array();
                }

                $this->db = wp_parse_args( $this->db, array(
                    'version' => false,
                    'logs'    => false,
                ) );
            }
        }

        /**
         * Triggers Version Upgrade Callback.
         *
         * @return $this
         */
        public function run() {
            if ( ! empty( $this->slug ) && ! empty( $this->new_v ) && $this->new_v !== $this->version() ) {
                $done = false;
                if ( ! empty( $this->version() ) ) {
                    foreach ( $this->versions as $ver ) {
                        if ( $this->version() === $ver ) {
                            continue;
                        }

                        if ( version_compare( $ver, $this->version(), '>=' ) && version_compare( $ver, $this->new_v, '<=' ) ) {
                            $done = call_user_func_array( $this->callbacks[ $ver ], array( $this->version(), $ver ) );
                            if ( ! $done ) {
                                break;
                            }
                        }
                    }
                } elseif ( empty( $this->version() ) && isset( $this->callbacks[ $this->new_v ] ) && is_callable( $this->callbacks[ $this->new_v ] ) ) {
                    $done = call_user_func_array( $this->callbacks[ $this->new_v ], array( '', $this->new_v ) );
                }
                if ( true === $done || ! isset( $this->callbacks[ $this->new_v ] ) ) {
                    $this->update_log();
                }
            }
            return $this;
        }

        /**
         * Updates Log Informations.
         */
        protected function update_log() {
            $logs = ( $this->logs ) ? $this->logs() : false;
            $data = array( 'version' => $this->new_v );
            if ( true === $this->logs ) {
                $logs                 = ( ! is_array( $logs ) ) ? array() : $logs;
                $logs[ $this->new_v ] = array(
                    'user_id' => get_current_user_id(),
                    'time'    => current_time( 'timestamp' ),
                    'from'    => $this->version(),
                );
            }
            $data['logs'] = $logs;
            return $this->save_db_values( $data );
        }

        /**
         * Updated Database With new Values.
         *
         * @param $data
         *
         * @return $this
         */
        protected function save_db_values( $data ) {
            $option_name = ( true === $this->option_name ) ? $this->db_key : $this->option_name;
            $stored_data = get_option( $option_name, false );
            $stored_data = ( ! is_array( $stored_data ) ) ? array() : $stored_data;

            if ( true === $this->option_name ) {
                $stored_data[ $this->slug ] = $data;
            } else {
                $stored_data = wp_parse_args( $data, $stored_data );
            }
            update_option( $option_name, $stored_data, false );
            return $this;

        }
    }
}
API documentation generated by ApiGen