Created
February 17, 2020 08:17
-
-
Save MaximeCulea/031223e105ecb15cb7cea18f806c580a to your computer and use it in GitHub Desktop.
Handle variables when working with template parts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php class MC_Template_Part_Var { | |
/** | |
* @author Maxime Culea | |
* @var MC_Template_Part_Var | |
*/ | |
public static $instance; | |
/** | |
* Vars to store | |
* | |
* @author Maxime Culea | |
* @var array | |
*/ | |
private $vars = array(); | |
/** | |
* Private | |
*/ | |
private function __construct() {} | |
/** | |
* @return MC_Template_Part_Var | |
*/ | |
public static function get_instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new MC_Template_Part_Var(); | |
} | |
return self::$instance; | |
} | |
/** | |
* @param $slug | |
* @param $key | |
* @param string $value | |
* | |
* @return bool | |
*/ | |
public function add_var( $slug, $key, $value = '' ) { | |
$this->vars[ $slug ][ $key ] = $value; | |
return true; | |
} | |
/** | |
* @param string $slug | |
* @param string $key | |
* | |
* @return mixed | |
*/ | |
public function get_var( $slug = '', $key = '' ) { | |
return isset( $this->vars[ $slug ][ $key ] ) ? $this->vars[ $slug ][ $key ] : null; | |
} | |
/** | |
* @param string $slug | |
* | |
* @return mixed | |
*/ | |
public function get_vars( $slug = '' ) { | |
return isset( $this->vars[ $slug ] ) ? $this->vars[ $slug ] : null; | |
} | |
} | |
/** | |
* @param $slug | |
* @param $key | |
* @param string $value | |
* | |
* @use MC_Template_Part_Var | |
*/ | |
function mc_add_template_var( $slug, $key, $value = '' ) { | |
MC_Template_Part_Var::get_instance()->add_var( $slug, $key, $value ); | |
} | |
/** | |
* Add vars to a template | |
* | |
* @param $slug | |
* @param array $data : associative array of key => value | |
* | |
* @author Maxime Culea | |
* | |
*/ | |
function mc_add_template_vars( $slug, array $data ) { | |
if ( empty( $data ) ) { | |
return; | |
} | |
if ( count( $data ) == 1 ) { | |
MC_Template_Part_Var::get_instance()->add_var( $slug, $data['key'], $data['value'] ); | |
} | |
foreach ( $data as $key => $value ) { | |
MC_Template_Part_Var::get_instance()->add_var( $slug, $key, $value ); | |
} | |
} | |
/** | |
* @param $slug | |
* @param $key | |
* | |
* @use MC_Template_Part_Var | |
* | |
* @return string | |
*/ | |
function mc_get_template_var( $slug, $key ) { | |
return MC_Template_Part_Var::get_instance()->get_var( $slug, $key ); | |
} | |
/** | |
* Get the templates vars | |
* | |
* @param $slug | |
* @param $key_or_keys : single key or array of keys | |
* | |
* @use MC_Template_Part_Var | |
* | |
* @author Maxime Culea | |
* | |
* @return array | |
*/ | |
function mc_get_template_vars( $slug, $key_or_keys ) { | |
if ( empty( $key_or_keys ) ) { | |
return array(); | |
} | |
if ( ! is_array( $key_or_keys ) ) { | |
return array( $key_or_keys => MC_Template_Part_Var::get_instance()->get_var( $slug, $key_or_keys ) ); | |
} | |
$values = []; | |
foreach ( $key_or_keys as $key ) { | |
$values[ $key ] = MC_Template_Part_Var::get_instance()->get_var( $slug, $key ); | |
} | |
return $values; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment