balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-redux-instances.php (2755B)


      1 <?php
      2 /**
      3  * Redux Framework Instance Container Class
      4  * Automatically captures and stores all instances
      5  * of ReduxFramework at instantiation.
      6  *
      7  * @package     Redux_Framework/Classes
      8  * @subpackage  Core
      9  */
     10 
     11 defined( 'ABSPATH' ) || exit;
     12 
     13 if ( ! class_exists( 'Redux_Instances', false ) ) {
     14 
     15 	/**
     16 	 * Class Redux_Instances
     17 	 */
     18 	class Redux_Instances {
     19 
     20 		/**
     21 		 * ReduxFramework instances
     22 		 *
     23 		 * @var ReduxFramework[]
     24 		 */
     25 		private static $instances;
     26 
     27 		/**
     28 		 * Get Instance
     29 		 * Get Redux_Instances instance
     30 		 * OR an instance of ReduxFramework by [opt_name]
     31 		 *
     32 		 * @param  string|false $opt_name the defined opt_name.
     33 		 *
     34 		 * @return ReduxFramework class instance
     35 		 */
     36 		public static function get_instance( $opt_name = false ) {
     37 
     38 			if ( $opt_name && ! empty( self::$instances[ $opt_name ] ) ) {
     39 				return self::$instances[ $opt_name ];
     40 			}
     41 
     42 			return new self();
     43 		}
     44 
     45 		/**
     46 		 * Shim for old get_redux_instance method.
     47 		 *
     48 		 * @param  string|false $opt_name the defined opt_name.
     49 		 *
     50 		 * @return ReduxFramework class instance
     51 		 */
     52 		public static function get_redux_instance( $opt_name = '' ) {
     53 			return self::get_instance( $opt_name );
     54 		}
     55 
     56 		/**
     57 		 * Get all instantiated ReduxFramework instances (so far)
     58 		 *
     59 		 * @return [type] [description]
     60 		 */
     61 		public static function get_all_instances(): ?array {
     62 			return self::$instances;
     63 		}
     64 
     65 		/**
     66 		 * Redux_Instances constructor.
     67 		 *
     68 		 * @param mixed $redux_framework Is object.
     69 		 */
     70 		public function __construct( $redux_framework = false ) {
     71 			if ( false !== $redux_framework ) {
     72 				$this->store( $redux_framework );
     73 			} else {
     74 				add_action( 'redux/construct', array( $this, 'store' ), 5, 1 );
     75 			}
     76 		}
     77 
     78 		/**
     79 		 * Action hook callback.
     80 		 *
     81 		 * @param object $redux_framework Pointer.
     82 		 */
     83 		public function store( $redux_framework ) {
     84 			if ( $redux_framework instanceof ReduxFramework ) {
     85 				$key                     = $redux_framework->args['opt_name'];
     86 				self::$instances[ $key ] = $redux_framework;
     87 			}
     88 		}
     89 	}
     90 }
     91 
     92 if ( ! class_exists( 'ReduxFrameworkInstances' ) ) {
     93 	class_alias( 'Redux_Instances', 'ReduxFrameworkInstances' );
     94 }
     95 
     96 if ( ! function_exists( 'get_redux_instance' ) ) {
     97 	/**
     98 	 * Shim function that some theme oddly used.
     99 	 *
    100 	 * @param  string|false $opt_name the defined opt_name.
    101 	 *
    102 	 * @return ReduxFramework class instance
    103 	 */
    104 	function get_redux_instance( $opt_name ) {
    105 		return Redux_Instances::get_instance( $opt_name );
    106 	}
    107 }
    108 
    109 if ( ! function_exists( 'get_all_redux_instances' ) ) {
    110 	/**
    111 	 * Fetch all instances of ReduxFramework
    112 	 * as an associative array.
    113 	 *
    114 	 * @return array        format ['opt_name' => $ReduxFramework]
    115 	 */
    116 	function get_all_redux_instances(): ?array {
    117 		return Redux_Instances::get_all_instances();
    118 	}
    119 }