| @@ -1,0 +1,282 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.layout | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Base class for rendering a display layout | |
| 16 | + * | |
| 17 | + * @since 10.1.18 | |
| 18 | + */ | |
| 19 | +class JLayoutBase | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Options object. | |
| 23 | + * | |
| 24 | + * @var JRegistry | |
| 25 | + */ | |
| 26 | + protected $options = null; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Data for the layout. | |
| 30 | + * | |
| 31 | + * @var array | |
| 32 | + */ | |
| 33 | + protected $data = array(); | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Debug information messages. | |
| 37 | + * | |
| 38 | + * @var array | |
| 39 | + */ | |
| 40 | + protected $debugMessages = array(); | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Set the options. | |
| 44 | + * | |
| 45 | + * @param mixed $options Array / JRegistry object with the options to load. | |
| 46 | + * | |
| 47 | + * @return self This object to support chaining. | |
| 48 | + */ | |
| 49 | + public function setOptions($options = null) | |
| 50 | + { | |
| 51 | + // registry received | |
| 52 | + if ($options instanceof JRegistry) | |
| 53 | + { | |
| 54 | + $this->options = $options; | |
| 55 | + } | |
| 56 | + // array received | |
| 57 | + else if (is_array($options)) | |
| 58 | + { | |
| 59 | + $this->options = new JRegistry($options); | |
| 60 | + } | |
| 61 | + else | |
| 62 | + { | |
| 63 | + $this->options = new JRegistry; | |
| 64 | + } | |
| 65 | + | |
| 66 | + return $this; | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Get the options. | |
| 71 | + * | |
| 72 | + * @return JRegistry Object with the options. | |
| 73 | + * | |
| 74 | + * @uses resetOptions() | |
| 75 | + */ | |
| 76 | + public function getOptions() | |
| 77 | + { | |
| 78 | + // Always return a JRegistry instance | |
| 79 | + if (!($this->options instanceof JRegistry)) | |
| 80 | + { | |
| 81 | + $this->resetOptions(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + return $this->options; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Function to empty all the options. | |
| 89 | + * | |
| 90 | + * @return self This object to support chaining. | |
| 91 | + * | |
| 92 | + * @uses resetOptions() | |
| 93 | + */ | |
| 94 | + public function resetOptions() | |
| 95 | + { | |
| 96 | + return $this->setOptions(null); | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Method to escape output. | |
| 101 | + * | |
| 102 | + * @param string $output The output to escape. | |
| 103 | + * | |
| 104 | + * @return string The escaped output. | |
| 105 | + */ | |
| 106 | + public function escape($output) | |
| 107 | + { | |
| 108 | + /** | |
| 109 | + * Attributes are now escaped by using the built-in WP function. | |
| 110 | + * | |
| 111 | + * @since 10.1.33 | |
| 112 | + */ | |
| 113 | + return esc_attr($output); | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Gets the debug messages array. | |
| 118 | + * | |
| 119 | + * @return array | |
| 120 | + */ | |
| 121 | + public function getDebugMessages() | |
| 122 | + { | |
| 123 | + return $this->debugMessages; | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Method to render the layout. | |
| 128 | + * | |
| 129 | + * @param array $displayData Array of properties available for use inside | |
| 130 | + * the layout file to build the displayed output. | |
| 131 | + * | |
| 132 | + * @return string The necessary HTML to display the layout. | |
| 133 | + */ | |
| 134 | + public function render($displayData) | |
| 135 | + { | |
| 136 | + // automatically merge any previously data set if $displayData is an array | |
| 137 | + if (is_array($displayData)) | |
| 138 | + { | |
| 139 | + $displayData = array_merge($this->data, $displayData); | |
| 140 | + } | |
| 141 | + | |
| 142 | + return ''; | |
| 143 | + } | |
| 144 | + | |
| 145 | + /** | |
| 146 | + * Renders the list of debug messages. | |
| 147 | + * | |
| 148 | + * @return string Output text/HTML code. | |
| 149 | + */ | |
| 150 | + public function renderDebugMessages() | |
| 151 | + { | |
| 152 | + return implode("\n", $this->debugMessages); | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Adds a debug message to the debug messages array. | |
| 157 | + * | |
| 158 | + * @param string $message Message to save. | |
| 159 | + * | |
| 160 | + * @return self This object to support chaining. | |
| 161 | + */ | |
| 162 | + public function addDebugMessage($message) | |
| 163 | + { | |
| 164 | + $this->debugMessages[] = $message; | |
| 165 | + | |
| 166 | + return $this; | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Clears the debug messages array. | |
| 171 | + * | |
| 172 | + * @return self This object to support chaining. | |
| 173 | + */ | |
| 174 | + public function clearDebugMessages() | |
| 175 | + { | |
| 176 | + $this->debugMessages = array(); | |
| 177 | + | |
| 178 | + return $this; | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Renders a layout with debug info. | |
| 183 | + * | |
| 184 | + * @param mixed $data Data passed to the layout. | |
| 185 | + * | |
| 186 | + * @return string The necessary HTML to display the layout. | |
| 187 | + * | |
| 188 | + * @uses setDebug() | |
| 189 | + * @uses render() | |
| 190 | + */ | |
| 191 | + public function debug($data = array()) | |
| 192 | + { | |
| 193 | + $this->setDebug(true); | |
| 194 | + | |
| 195 | + $output = $this->render($data); | |
| 196 | + | |
| 197 | + $this->setDebug(false); | |
| 198 | + | |
| 199 | + return $output; | |
| 200 | + } | |
| 201 | + | |
| 202 | + /** | |
| 203 | + * Method to get the value from the data array. | |
| 204 | + * | |
| 205 | + * @param string $key Key to search for in the data array. | |
| 206 | + * @param mixed $defaultValue Default value to return if the key is not set. | |
| 207 | + * | |
| 208 | + * @return mixed The data value if set, otherwise the default key. | |
| 209 | + */ | |
| 210 | + public function get($key, $defaultValue = null) | |
| 211 | + { | |
| 212 | + return isset($this->data[$key]) ? $this->data[$key] : $defaultValue; | |
| 213 | + } | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * Gets the data being rendered. | |
| 217 | + * | |
| 218 | + * @return array | |
| 219 | + */ | |
| 220 | + public function getData() | |
| 221 | + { | |
| 222 | + return $this->data; | |
| 223 | + } | |
| 224 | + | |
| 225 | + /** | |
| 226 | + * Check if debug mode is enabled. | |
| 227 | + * | |
| 228 | + * @return boolean True if enabled, false otherwise. | |
| 229 | + * | |
| 230 | + * @uses getOptions() | |
| 231 | + */ | |
| 232 | + public function isDebugEnabled() | |
| 233 | + { | |
| 234 | + return $this->getOptions()->get('debug', false) === true; | |
| 235 | + } | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * Method to set a value in the data array. | |
| 239 | + * Example: $layout->set('items', $items); | |
| 240 | + * | |
| 241 | + * @param string $key Key for the data array. | |
| 242 | + * @param mixed $value Value to assign to the key. | |
| 243 | + * | |
| 244 | + * @return self This object to support chaining. | |
| 245 | + */ | |
| 246 | + public function set($key, $value) | |
| 247 | + { | |
| 248 | + $this->data[(string) $key] = $value; | |
| 249 | + | |
| 250 | + return $this; | |
| 251 | + } | |
| 252 | + | |
| 253 | + /** | |
| 254 | + * Sets the the data passed the layout. | |
| 255 | + * | |
| 256 | + * @param array $data Array with the data for the layout. | |
| 257 | + * | |
| 258 | + * @return self This object to support chaining. | |
| 259 | + */ | |
| 260 | + public function setData(array $data) | |
| 261 | + { | |
| 262 | + $this->data = $data; | |
| 263 | + | |
| 264 | + return $this; | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Changes the debug mode. | |
| 269 | + * | |
| 270 | + * @param boolean $debug Enable or disable the debug flag. | |
| 271 | + * | |
| 272 | + * @return self This object to support chaining. | |
| 273 | + * | |
| 274 | + * @uses getOptions() | |
| 275 | + */ | |
| 276 | + public function setDebug($debug) | |
| 277 | + { | |
| 278 | + $this->getOptions()->set('debug', (boolean) $debug); | |
| 279 | + | |
| 280 | + return $this; | |
| 281 | + } | |
| 282 | +} | |