| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikWP - Libraries |
| 4 |
* @subpackage adapter.toolbar |
| 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 |
JLoader::import('adapter.toolbar.button.base'); |
| 15 |
|
| 16 |
/** |
| 17 |
* Plugin main toolbar handler. |
| 18 |
* |
| 19 |
* @since 10.0 |
| 20 |
*/ |
| 21 |
class JToolbar |
| 22 |
{ |
| 23 |
/** |
| 24 |
* The singleton toolbar instance. |
| 25 |
* |
| 26 |
* @var JToolbar |
| 27 |
*/ |
| 28 |
protected static $instance = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* The toolbar ID. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $id; |
| 36 |
|
| 37 |
/** |
| 38 |
* The toolbar title. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
protected $title; |
| 43 |
|
| 44 |
/** |
| 45 |
* A list of buttons. |
| 46 |
* |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
protected $bar; |
| 50 |
|
| 51 |
/** |
| 52 |
* Class constructor. |
| 53 |
*/ |
| 54 |
protected function __construct($id) |
| 55 |
{ |
| 56 |
// this method is not accessible |
| 57 |
|
| 58 |
$this->id = empty($id) ? uniqid() : $id; |
| 59 |
$this->bar = array(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Class cloner. |
| 64 |
*/ |
| 65 |
protected function __clone() |
| 66 |
{ |
| 67 |
// this method is not accessible |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Proxy to access the JToolbar singleton. |
| 72 |
* |
| 73 |
* @return self The singleton. |
| 74 |
*/ |
| 75 |
public static function getInstance($id = 'jtoolbar') |
| 76 |
{ |
| 77 |
if (!isset(static::$instance[$id])) |
| 78 |
{ |
| 79 |
static::$instance[$id] = new static($id); |
| 80 |
} |
| 81 |
|
| 82 |
return static::$instance[$id]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* The toolbar title. |
| 87 |
* |
| 88 |
* @param string $title The title. |
| 89 |
* |
| 90 |
* @return self This object to support chaining. |
| 91 |
*/ |
| 92 |
public function setTitle($title) |
| 93 |
{ |
| 94 |
$this->title = (string) $title; |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns the toolbar title. |
| 101 |
* If missing, it will be used the default WP page title. |
| 102 |
* |
| 103 |
* @return string The title. |
| 104 |
*/ |
| 105 |
public function getTitle() |
| 106 |
{ |
| 107 |
if (empty($this->title)) |
| 108 |
{ |
| 109 |
if (JFactory::getApplication()->isAdmin()) |
| 110 |
{ |
| 111 |
return get_admin_page_title(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return $this->title; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Checks if the title has been set. |
| 120 |
* |
| 121 |
* @return boolean True if set, otherwise false. |
| 122 |
*/ |
| 123 |
public function hasTitle() |
| 124 |
{ |
| 125 |
return !empty($this->title); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns the toolbar buttons. |
| 130 |
* |
| 131 |
* @return array The buttons in the bar. |
| 132 |
*/ |
| 133 |
public function getButtons() |
| 134 |
{ |
| 135 |
return $this->bar; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Checks if the toolbar contains any buttons. |
| 140 |
* |
| 141 |
* @return boolean True if set, otherwise false. |
| 142 |
*/ |
| 143 |
public function hasButtons() |
| 144 |
{ |
| 145 |
return (bool) count($this->bar); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Appends a button to the toolbar. |
| 150 |
* This method accept an undefined number of arguments. |
| 151 |
* |
| 152 |
* @return self This object to support chaining. |
| 153 |
* |
| 154 |
* @uses createButton() |
| 155 |
*/ |
| 156 |
public function appendButton() |
| 157 |
{ |
| 158 |
$args = func_get_args(); |
| 159 |
$btn = $this->createButton($args); |
| 160 |
|
| 161 |
if ($btn) |
| 162 |
{ |
| 163 |
array_push($this->bar, $btn); |
| 164 |
} |
| 165 |
|
| 166 |
return $this; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Adds a button at the beginning of the toolbar. |
| 171 |
* This method accept an undefined number of arguments. |
| 172 |
* |
| 173 |
* @return self This object to support chaining. |
| 174 |
* |
| 175 |
* @uses createButton() |
| 176 |
*/ |
| 177 |
public function prependButton() |
| 178 |
{ |
| 179 |
$args = func_get_args(); |
| 180 |
$btn = $this->createButton($args); |
| 181 |
|
| 182 |
if ($btn) |
| 183 |
{ |
| 184 |
array_unshift($this->bar, $btn); |
| 185 |
} |
| 186 |
|
| 187 |
return $this; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Removes a button from the toolbar. |
| 192 |
* |
| 193 |
* @param mixed $btn Either the position of the button or the |
| 194 |
* button reference to delete. |
| 195 |
* |
| 196 |
* @return mixed The deleted button on success, false otherwise. |
| 197 |
* |
| 198 |
* @since 10.1.36 |
| 199 |
*/ |
| 200 |
public function removeButton($btn) |
| 201 |
{ |
| 202 |
if (!is_scalar($btn)) |
| 203 |
{ |
| 204 |
// button instance specified, search it insider the bar |
| 205 |
$btn = array_search($btn, $this->bar); |
| 206 |
|
| 207 |
// make sure the button exists |
| 208 |
if ($btn === false) |
| 209 |
{ |
| 210 |
return false; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
// detach button from array |
| 215 |
$deleted = array_splice($this->bar, (int) $btn, 1); |
| 216 |
|
| 217 |
if (!$deleted) |
| 218 |
{ |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
return $deleted[0]; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Creates a button depending on the specified arguments. |
| 227 |
* |
| 228 |
* @param array $args The button options. |
| 229 |
* |
| 230 |
* @return mixed The new button on success, otherwise null. |
| 231 |
*/ |
| 232 |
protected function createButton(array $args = array()) |
| 233 |
{ |
| 234 |
if (!count($args)) |
| 235 |
{ |
| 236 |
return null; |
| 237 |
} |
| 238 |
|
| 239 |
return JToolbarButtonBase::getInstance($args); |
| 240 |
} |
| 241 |
} |
| 242 |
|