| 1 |
<?php namespace Datafeedr\Api\Wuwei\Templates; |
| 2 |
|
| 3 |
/** |
| 4 |
* Abstract Class Template. |
| 5 |
* |
| 6 |
* This is the abstract class for Template_Public() and Template_Admin(). |
| 7 |
* Handles the $data array and $name of path to template file. |
| 8 |
* |
| 9 |
* @since 2.0.0 |
| 10 |
*/ |
| 11 |
abstract class Template { |
| 12 |
|
| 13 |
/** |
| 14 |
* The relative path to template file (file extension optional). |
| 15 |
* |
| 16 |
* @since 2.0.0 |
| 17 |
* @access public |
| 18 |
* @var string $name Name of path to template file. Example: 'product/price'. |
| 19 |
*/ |
| 20 |
public $name; |
| 21 |
|
| 22 |
/** |
| 23 |
* The data to use in the template. |
| 24 |
* |
| 25 |
* @since 2.0.0 |
| 26 |
* @access public |
| 27 |
* @var array $data Optional. Array of data for the template to use. |
| 28 |
*/ |
| 29 |
public $data; |
| 30 |
|
| 31 |
/** |
| 32 |
* Template Constructor. |
| 33 |
* |
| 34 |
* This sets our $name and $data properties. |
| 35 |
* |
| 36 |
* @since 2.0.0 |
| 37 |
* |
| 38 |
* @param string $name Relative path to template file. |
| 39 |
* @param array $data Optional. Array of data to use in the template file. |
| 40 |
*/ |
| 41 |
public function __construct( $name, $data = array() ) { |
| 42 |
$this->set_name( $name ); |
| 43 |
$this->set_data( $data ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Forces our child classes to have this function as this class relies on the get_file() |
| 48 |
* method to return the full path to the template file. |
| 49 |
* |
| 50 |
* Returns the full path to the template file we will use. |
| 51 |
* |
| 52 |
* @since 2.0.0 |
| 53 |
* |
| 54 |
* @return string Full path to the template file. |
| 55 |
*/ |
| 56 |
abstract protected function get_file(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Forces our child classes to have this function as this function returns the |
| 60 |
* array of data we will use in our themes. |
| 61 |
* |
| 62 |
* We needed to make this abstract because our admin template class does not |
| 63 |
* run the $data array through apply_filters() whereas the public template file does. |
| 64 |
* |
| 65 |
* @since 2.0.0 |
| 66 |
* |
| 67 |
* @return array Array of $data that will be extracted for use in the template file. |
| 68 |
*/ |
| 69 |
abstract protected function get_data(); |
| 70 |
|
| 71 |
/** |
| 72 |
* Sets the file name. |
| 73 |
* |
| 74 |
* Given a $name like /product/partials/price, this function will: |
| 75 |
* |
| 76 |
* - strip the leading & trailing forward slashes |
| 77 |
* - replace all forward slashes with DIRECTORY_SEPARATOR |
| 78 |
* - If the $name does not have an extension, will append ".php" to the $name; |
| 79 |
* |
| 80 |
* @since 2.0.0 |
| 81 |
* |
| 82 |
* @param string $name The template file name. Example: 'product/partials/price |
| 83 |
*/ |
| 84 |
private function set_name( $name ) { |
| 85 |
$name = trim( $name, '/' ); |
| 86 |
$name = str_replace( '/', DIRECTORY_SEPARATOR, $name ); |
| 87 |
$path = pathinfo( $name ); |
| 88 |
|
| 89 |
$this->name = ( ! isset( $path['extension'] ) || empty( $path['extension'] ) ) ? $name . '.php' : $name; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Sets the $this->data property. |
| 94 |
* |
| 95 |
* If $data['attributes'] is not set, then it also sets that array item to an |
| 96 |
* empty array to avoid "PHP Notice: Undefined variable: attributes" errors in |
| 97 |
* the generic templates or anywhere $attributes is used in a template file. |
| 98 |
* |
| 99 |
* @since 2.0.0 |
| 100 |
* |
| 101 |
* @param array $data An array of data to be used in the template file. |
| 102 |
*/ |
| 103 |
private function set_data( $data ) { |
| 104 |
if ( ! isset( $data['attributes'] ) ) { |
| 105 |
$data['attributes'] = array(); |
| 106 |
} |
| 107 |
|
| 108 |
$this->data = $data; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* This inserts another template file into a parent template file. |
| 113 |
* |
| 114 |
* This basically acts as an include() call in a template file. However, |
| 115 |
* this passes false to render() so that the results of this insert() call are not |
| 116 |
* buffered. Our parent template already handles that. No need to buffer the output twice. |
| 117 |
* |
| 118 |
* Usage: <?php $this->insert( 'products/product', array( 'product' => $product ) ); ?> |
| 119 |
* |
| 120 |
* @since 2.0.0 |
| 121 |
* |
| 122 |
* @param string $name The name of the relative template file. |
| 123 |
* @param array $data Optional. An array of data to use in the template file. Will be appended to original $data array. |
| 124 |
*/ |
| 125 |
public function insert( $name, $data = array() ) { |
| 126 |
$this->set_name( $name ); |
| 127 |
$this->data = array_merge( $this->get_data(), $data ); |
| 128 |
$this->render( false ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns formatted list of attributes for an HTML element. |
| 133 |
* |
| 134 |
* $attrs should be formatted like: |
| 135 |
* |
| 136 |
* array( |
| 137 |
* 'href' => 'http://google.com', |
| 138 |
* 'target' => '_blank', |
| 139 |
* 'class' => 'btn btn-sm', |
| 140 |
* ) |
| 141 |
* |
| 142 |
* @since 2.0.0 |
| 143 |
* |
| 144 |
* @param array $attrs Optional. Array of attributes. |
| 145 |
* |
| 146 |
* @return string HTML. |
| 147 |
*/ |
| 148 |
public function attributes( $attrs = array() ) { |
| 149 |
$attributes = ''; |
| 150 |
$format = '%s="%s" '; |
| 151 |
foreach ( $attrs as $k => $v ) { |
| 152 |
$attributes .= sprintf( $format, $k, esc_attr( $v ) ); |
| 153 |
} |
| 154 |
|
| 155 |
return trim( $attributes ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Includes the proper template file and extracts $data for use from |
| 160 |
* within the template file. |
| 161 |
* |
| 162 |
* @since 2.0.0 |
| 163 |
* |
| 164 |
* @param boolean $buffer Optional. Whether or not to buffer the output. Default: true. |
| 165 |
* |
| 166 |
* @return string|null Return error message if file is missing, or null if we did the include(). |
| 167 |
*/ |
| 168 |
public function render( $buffer = true ) { |
| 169 |
|
| 170 |
$file = $this->get_file(); |
| 171 |
|
| 172 |
if ( ! is_file( $file ) ) { |
| 173 |
return 'Template file ' . $file . ' does not exist.'; |
| 174 |
} |
| 175 |
|
| 176 |
$data = $this->get_data(); |
| 177 |
|
| 178 |
extract( $data ); |
| 179 |
|
| 180 |
if ( $buffer ) { |
| 181 |
ob_start(); |
| 182 |
} |
| 183 |
|
| 184 |
include( $file ); |
| 185 |
|
| 186 |
if ( $buffer ) { |
| 187 |
return ob_get_clean(); |
| 188 |
} |
| 189 |
|
| 190 |
return null; |
| 191 |
} |
| 192 |
} |
| 193 |
|