| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
|
| 7 |
/** |
| 8 |
* |
| 9 |
*/ |
| 10 |
final class CJTBlocksCouplingHookAttacher { |
| 11 |
|
| 12 |
|
| 13 |
const GROUP = 'cjt'; |
| 14 |
|
| 15 |
/** |
| 16 |
* put your comment there... |
| 17 |
* |
| 18 |
* @var mixed |
| 19 |
*/ |
| 20 |
public $callback; |
| 21 |
|
| 22 |
/** |
| 23 |
* put your comment there... |
| 24 |
* |
| 25 |
* @var mixed |
| 26 |
*/ |
| 27 |
public $filters; |
| 28 |
|
| 29 |
/** |
| 30 |
* put your comment there... |
| 31 |
* |
| 32 |
* @var mixed |
| 33 |
*/ |
| 34 |
private $filtersMap = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* put your comment there... |
| 38 |
* |
| 39 |
* @var mixed |
| 40 |
*/ |
| 41 |
public $groupMap = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* put your comment there... |
| 45 |
* |
| 46 |
* @var mixed |
| 47 |
*/ |
| 48 |
public $textMap = array(); |
| 49 |
|
| 50 |
/** |
| 51 |
* |
| 52 |
*/ |
| 53 |
public $typeHandler = null; |
| 54 |
|
| 55 |
/** |
| 56 |
* put your comment there... |
| 57 |
* |
| 58 |
* @param mixed $callback |
| 59 |
* @return CJTBlocksCouplingHookAttacher |
| 60 |
*/ |
| 61 |
public function __construct($callback) { |
| 62 |
|
| 63 |
$this->typeHandler = $this; |
| 64 |
$this->callback = $callback; |
| 65 |
|
| 66 |
// Initialize filters |
| 67 |
$this->filters = array( |
| 68 |
|
| 69 |
// Unattched hooks are attached (dynamically) to either Backend or Frontend by the coupling controller |
| 70 |
'unattached' => array( |
| 71 |
|
| 72 |
/* Database location map name */ /* Wordpress Hook name Part */ |
| 73 |
'header' => array( |
| 74 |
'name' => 'head', |
| 75 |
'handler' => $this->callback, |
| 76 |
'title' => cssJSToolbox::getText('Header'), |
| 77 |
'text' => cssJSToolbox::getText('Put Block code right before the HTML </header> tag'), |
| 78 |
'group' => self::GROUP, |
| 79 |
), |
| 80 |
'footer' => array( |
| 81 |
'name' => 'footer', |
| 82 |
'handler' => $this->callback, |
| 83 |
'title' => cssJSToolbox::getText('Footer'), |
| 84 |
'text' => cssJSToolbox::getText('Put Block code at the end of the page'), |
| 85 |
'group' => self::GROUP, |
| 86 |
), |
| 87 |
), |
| 88 |
); |
| 89 |
|
| 90 |
do_action(CJTPluggableHelper::ACTION_BLOCKS_COUPLING_INIT_ATTACHER, $this); |
| 91 |
|
| 92 |
// Generate filters map even if not bind done yet |
| 93 |
// so that filter names can be used elsewhere |
| 94 |
$this->generateMap(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Bind all obtained (through the map) filters to Wordpress hooks |
| 99 |
* |
| 100 |
*/ |
| 101 |
public function & bind() { |
| 102 |
|
| 103 |
foreach ($this->filtersMap as $hookFullName => $hooks) { |
| 104 |
|
| 105 |
foreach ($hooks as $hook) { |
| 106 |
|
| 107 |
add_filter($hookFullName, $hook['handler'], 30); |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Generate Hook names by adding the passed prefix to the hook name(s) |
| 117 |
* |
| 118 |
* @param mixed $hooks |
| 119 |
* @param mixed $prefix |
| 120 |
*/ |
| 121 |
public function genHooks($hooks, $prefix = '') { |
| 122 |
|
| 123 |
// Bind hooks strighatly |
| 124 |
foreach ($hooks as $locationName => $hook) { |
| 125 |
|
| 126 |
// Create filters map |
| 127 |
$hookFullName = "{$prefix}{$hook['name']}"; |
| 128 |
|
| 129 |
$hook['locationName'] = $locationName; |
| 130 |
$this->filtersMap[$hookFullName][$locationName] = $hook; |
| 131 |
|
| 132 |
// Generate localized text map |
| 133 |
$this->textMap[$hook['locationName']]['title'] = $hook['title']; |
| 134 |
$this->textMap[$hook['locationName']]['text'] = $hook['text']; |
| 135 |
|
| 136 |
// Make Group map |
| 137 |
$this->groupMap[$hook['group']][] = $hook['locationName']; |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Generate unattached hook names by adding either (wp_ or admin_ prefix to the hook name(s) |
| 144 |
* |
| 145 |
* @param mixed $filters |
| 146 |
* @return CJTBlocksCouplingHookAttacher |
| 147 |
*/ |
| 148 |
public function & genUnattachedHooks($filters) { |
| 149 |
|
| 150 |
$this->genHooks($filters, is_admin() ? 'admin_' : 'wp_'); |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Generate all filter names by calling the FILTER-TYPE-HANDLER method |
| 157 |
* |
| 158 |
*/ |
| 159 |
public function generateMap() { |
| 160 |
|
| 161 |
$typeHandler = $this->typeHandler; |
| 162 |
|
| 163 |
foreach ($this->filters as $typeName => $filters) { |
| 164 |
|
| 165 |
$typeName = ucfirst($typeName); |
| 166 |
|
| 167 |
$attacherMethodName = "gen{$typeName}Hooks"; |
| 168 |
|
| 169 |
$typeHandler->$attacherMethodName($filters); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* put your comment there... |
| 175 |
* |
| 176 |
*/ |
| 177 |
public function getFiltersList() { |
| 178 |
|
| 179 |
return $this->filtersMap; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* put your comment there... |
| 185 |
* |
| 186 |
* @param mixed $group |
| 187 |
*/ |
| 188 |
public function getGroupMap($group) { |
| 189 |
|
| 190 |
return $this->groupMap[$group]; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* put your comment there... |
| 195 |
* |
| 196 |
* @param mixed $filter |
| 197 |
*/ |
| 198 |
public function getHooksByFilterName($filterName) { |
| 199 |
|
| 200 |
return $this->filtersMap[$filterName]; |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* put your comment there... |
| 206 |
* |
| 207 |
* @param mixed $locationName |
| 208 |
*/ |
| 209 |
public function getHookText($locationName, $prop = 'text') { |
| 210 |
|
| 211 |
$text = isset($this->textMap[$locationName]) ? |
| 212 |
$this->textMap[$locationName][$prop] : |
| 213 |
''; |
| 214 |
|
| 215 |
return $text; |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|