| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Tag Groups |
| 5 |
* @author Christoph Amthor |
| 6 |
* @copyright 2019 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 7 |
* @license GPL-3.0+ |
| 8 |
* @since |
| 9 |
*/ |
| 10 |
|
| 11 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, Squiz.Scope.MethodScope.Missing, PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 12 |
if (! class_exists('TagGroups_View')) { |
| 13 |
/** |
| 14 |
* General handling of views |
| 15 |
* |
| 16 |
* @since |
| 17 |
*/ |
| 18 |
class TagGroups_View |
| 19 |
{ |
| 20 |
/** |
| 21 |
* identifier of this view |
| 22 |
* |
| 23 |
* @var string |
| 24 |
* @since |
| 25 |
*/ |
| 26 |
private $view_slug; |
| 27 |
/** |
| 28 |
* full path of the file providing the view |
| 29 |
* |
| 30 |
* @var string |
| 31 |
* @since |
| 32 |
*/ |
| 33 |
private $view; |
| 34 |
/** |
| 35 |
* array of variables to be made available to the view (key is the variable name) |
| 36 |
* |
| 37 |
* @var array |
| 38 |
* @since |
| 39 |
*/ |
| 40 |
private $vars; |
| 41 |
/** |
| 42 |
* name of the view slug that can be used in a filter identificator |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
private $view_slug_filter; |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructor: checks if view exists |
| 50 |
* |
| 51 |
* @param string $view identifier of the view |
| 52 |
* @return object $this |
| 53 |
* @since |
| 54 |
*/ |
| 55 |
public function __construct($view_slug) |
| 56 |
{ |
| 57 |
|
| 58 |
$this->view_slug = $view_slug; |
| 59 |
$path = TAG_GROUPS_PLUGIN_ABSOLUTE_PATH . "/views/" . $this->view_slug . ".view.php"; |
| 60 |
if (file_exists($path)) { |
| 61 |
$this->view = $path; |
| 62 |
} else { |
| 63 |
TagGroups_Error::log('[Tag Groups] View ' . $path . ' not found'); |
| 64 |
} |
| 65 |
|
| 66 |
$this->view_slug_filter = str_replace('/', '-', $this->view_slug); |
| 67 |
$this->vars = array(); |
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* renders the view |
| 74 |
* |
| 75 |
* @param void |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function render() |
| 79 |
{ |
| 80 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Views contain trusted HTML/JS from plugin files |
| 81 |
echo $this->return_html(); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* returns the view |
| 87 |
* |
| 88 |
* @param void |
| 89 |
* @return string $html |
| 90 |
*/ |
| 91 |
public function return_html() |
| 92 |
{ |
| 93 |
|
| 94 |
if (empty($this->view)) { |
| 95 |
return ''; |
| 96 |
} |
| 97 |
|
| 98 |
$this->do_filter_atts(); |
| 99 |
extract($this->vars, EXTR_SKIP); |
| 100 |
ob_start(); |
| 101 |
include $this->view; |
| 102 |
$html = ob_get_clean(); |
| 103 |
return $this->do_filter_html($html); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* Option to modify the attributes used in the templates |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
private function do_filter_atts() |
| 113 |
{ |
| 114 |
|
| 115 |
/** |
| 116 |
* Filters the attributes before rendering |
| 117 |
* |
| 118 |
* @param array $this->vars |
| 119 |
* @param string $this->view_slug_filter |
| 120 |
* @return array must contain same fields and data types as $this->vars |
| 121 |
*/ |
| 122 |
$this->vars = apply_filters('tag_groups_view_atts', $this->vars, $this->view_slug_filter); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Option to customize the output |
| 128 |
* |
| 129 |
* @param string $html |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
private function do_filter_html($html) |
| 133 |
{ |
| 134 |
|
| 135 |
/** |
| 136 |
* Filters the final output of the view |
| 137 |
* |
| 138 |
* @param string $html |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
return apply_filters('tag_groups_view-' . $this->view_slug_filter, $html); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* General setter for $this->vars, accepting an array of key and values or one pair of key and value |
| 147 |
* |
| 148 |
* @param array|string $variable_name_or_array |
| 149 |
* @param mixed@null $data |
| 150 |
* @return object $this |
| 151 |
*/ |
| 152 |
public function set($variable_name_or_array, $data = null) |
| 153 |
{ |
| 154 |
|
| 155 |
if (is_string($variable_name_or_array)) { |
| 156 |
$this->set_view_var($variable_name_or_array, $data); |
| 157 |
} elseif (is_array($variable_name_or_array)) { |
| 158 |
foreach ($variable_name_or_array as $key => $value) { |
| 159 |
$this->set_view_var($key, $value); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $this; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Setter for $this->vars |
| 169 |
* |
| 170 |
* @param string $key |
| 171 |
* @param mixed $value |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
private function set_view_var($key, $value) |
| 175 |
{ |
| 176 |
|
| 177 |
$this->vars[ $key ] = $value; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
} |
| 183 |
|