| @@ -1,81 +1,81 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | -* | |
| 3 | +* | |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | |
| 7 | 7 | /** |
| 8 | -* | |
| 8 | +* | |
| 9 | 9 | */ |
| 10 | 10 | final class CJTBlocksCouplingHookAttacher { |
| 11 | - | |
| 12 | - | |
| 11 | + | |
| 12 | + | |
| 13 | 13 | const GROUP = 'cjt'; |
| 14 | - | |
| 14 | + | |
| 15 | 15 | /** |
| 16 | 16 | * put your comment there... |
| 17 | - * | |
| 17 | + * | |
| 18 | 18 | * @var mixed |
| 19 | 19 | */ |
| 20 | 20 | public $callback; |
| 21 | - | |
| 21 | + | |
| 22 | 22 | /** |
| 23 | 23 | * put your comment there... |
| 24 | - * | |
| 24 | + * | |
| 25 | 25 | * @var mixed |
| 26 | 26 | */ |
| 27 | 27 | public $filters; |
| 28 | - | |
| 28 | + | |
| 29 | 29 | /** |
| 30 | 30 | * put your comment there... |
| 31 | - * | |
| 31 | + * | |
| 32 | 32 | * @var mixed |
| 33 | 33 | */ |
| 34 | 34 | private $filtersMap = array(); |
| 35 | - | |
| 35 | + | |
| 36 | 36 | /** |
| 37 | 37 | * put your comment there... |
| 38 | - * | |
| 38 | + * | |
| 39 | 39 | * @var mixed |
| 40 | 40 | */ |
| 41 | 41 | public $groupMap = array(); |
| 42 | - | |
| 42 | + | |
| 43 | 43 | /** |
| 44 | 44 | * put your comment there... |
| 45 | - * | |
| 45 | + * | |
| 46 | 46 | * @var mixed |
| 47 | 47 | */ |
| 48 | 48 | public $textMap = array(); |
| 49 | - | |
| 49 | + | |
| 50 | 50 | /** |
| 51 | - * | |
| 51 | + * | |
| 52 | 52 | */ |
| 53 | 53 | public $typeHandler = null; |
| 54 | - | |
| 54 | + | |
| 55 | 55 | /** |
| 56 | 56 | * put your comment there... |
| 57 | - * | |
| 57 | + * | |
| 58 | 58 | * @param mixed $callback |
| 59 | 59 | * @return CJTBlocksCouplingHookAttacher |
| 60 | 60 | */ |
| 61 | 61 | public function __construct($callback) { |
| 62 | - | |
| 62 | + | |
| 63 | 63 | $this->typeHandler = $this; |
| 64 | 64 | $this->callback = $callback; |
| 65 | - | |
| 65 | + | |
| 66 | 66 | // Initialize filters |
| 67 | 67 | $this->filters = array( |
| 68 | - | |
| 68 | + | |
| 69 | 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 */ | |
| 70 | + 'unattached' => array( | |
| 71 | + | |
| 72 | + /* Database location map name */ /* Wordpress Hook name Part */ | |
| 73 | 73 | 'header' => array( |
| 74 | 74 | 'name' => 'head', |
| 75 | 75 | 'handler' => $this->callback, |
| 76 | 76 | 'title' => cssJSToolbox::getText('Header'), |
| 77 | - 'text' => cssJSToolbox::getText('Put Block code right before the HTML </header> tag'), | |
| 77 | + 'text' => cssJSToolbox::getText('Puts your code right before the HTML </header> tag'), | |
| 78 | 78 | 'group' => self::GROUP, |
| 79 | 79 | ), |
| 80 | 80 | 'footer' => array( |
| 81 | 81 | 'name' => 'footer', |
| @@ -80,139 +80,208 @@ | ||
| 80 | 80 | 'footer' => array( |
| 81 | 81 | 'name' => 'footer', |
| 82 | 82 | 'handler' => $this->callback, |
| 83 | 83 | 'title' => cssJSToolbox::getText('Footer'), |
| 84 | - 'text' => cssJSToolbox::getText('Put Block code at the end of the page'), | |
| 84 | + 'text' => cssJSToolbox::getText('Puts your code at the end of the page'), | |
| 85 | 85 | 'group' => self::GROUP, |
| 86 | 86 | ), |
| 87 | 87 | ), |
| 88 | 88 | ); |
| 89 | - | |
| 89 | + | |
| 90 | + if ( ! class_exists( 'CJTPlus' ) ) { | |
| 91 | + $this->filters['unattached'] = array_merge( $this->filters['unattached'], [ | |
| 92 | + 'wp_body_open' => array( | |
| 93 | + 'name' => 'wp_body_open', | |
| 94 | + 'handler' => '', | |
| 95 | + 'title' => cssJSToolbox::getText('Body Open'), | |
| 96 | + 'text' => cssJSToolbox::getText('Puts your code right after the HTML tag'), | |
| 97 | + 'group' => self::GROUP, | |
| 98 | + 'disabled' => true, | |
| 99 | + ), | |
| 100 | + 'before_the_content' => array( | |
| 101 | + 'name' => 'the_content', | |
| 102 | + 'handler' => '', | |
| 103 | + 'title' => cssJSToolbox::getText('Before Post Content'), | |
| 104 | + 'text' => cssJSToolbox::getText('Puts your code before the post content'), | |
| 105 | + 'group' => self::GROUP, | |
| 106 | + 'disabled' => true, | |
| 107 | + ), | |
| 108 | + 'after_the_content' => array( | |
| 109 | + 'name' => 'the_content', | |
| 110 | + 'handler' => '', | |
| 111 | + 'title' => cssJSToolbox::getText('After Post Content'), | |
| 112 | + 'text' => cssJSToolbox::getText('Puts your code after the post content'), | |
| 113 | + 'group' => self::GROUP, | |
| 114 | + 'disabled' => true, | |
| 115 | + ), | |
| 116 | + 'before_the_excerpt' => array( | |
| 117 | + 'name' => 'the_excerpt', | |
| 118 | + 'handler' => '', | |
| 119 | + 'title' => cssJSToolbox::getText('Before Post Excerpt'), | |
| 120 | + 'text' => cssJSToolbox::getText('Puts your code just before the post excerpt'), | |
| 121 | + 'group' => self::GROUP, | |
| 122 | + 'disabled' => true, | |
| 123 | + ), | |
| 124 | + 'after_the_excerpt' => array( | |
| 125 | + 'name' => 'the_excerpt', | |
| 126 | + 'handler' => '', | |
| 127 | + 'title' => cssJSToolbox::getText('After Post Excerpt'), | |
| 128 | + 'text' => cssJSToolbox::getText('Puts your code after the post excerpt'), | |
| 129 | + 'group' => self::GROUP, | |
| 130 | + 'disabled' => true, | |
| 131 | + ), | |
| 132 | + 'loop_start' => array( | |
| 133 | + 'name' => 'loop_start', | |
| 134 | + 'handler' => '', | |
| 135 | + 'title' => cssJSToolbox::getText('Before Posts List'), | |
| 136 | + 'text' => cssJSToolbox::getText('Puts your code right before the posts list, or before the post when the posts query result is a single post'), | |
| 137 | + 'group' => self::GROUP, | |
| 138 | + 'disabled' => true, | |
| 139 | + ), | |
| 140 | + 'loop_end' => array( | |
| 141 | + 'name' => 'loop_end', | |
| 142 | + 'handler' => '', | |
| 143 | + 'title' => cssJSToolbox::getText('After Posts List'), | |
| 144 | + 'text' => cssJSToolbox::getText('Puts your code right after the posts list, or after the post when the posts query result is a single post'), | |
| 145 | + 'group' => self::GROUP, | |
| 146 | + 'disabled' => true, | |
| 147 | + ), | |
| 148 | + 'the_post' => array( | |
| 149 | + 'name' => 'the_post', | |
| 150 | + 'handler' => '', | |
| 151 | + 'title' => cssJSToolbox::getText('Before Post'), | |
| 152 | + 'text' => cssJSToolbox::getText('Puts your code right before the post'), | |
| 153 | + 'group' => self::GROUP, | |
| 154 | + 'disabled' => true, | |
| 155 | + ), | |
| 156 | + ] ); | |
| 157 | + } | |
| 158 | + | |
| 90 | 159 | do_action(CJTPluggableHelper::ACTION_BLOCKS_COUPLING_INIT_ATTACHER, $this); |
| 91 | - | |
| 160 | + | |
| 92 | 161 | // Generate filters map even if not bind done yet |
| 93 | 162 | // so that filter names can be used elsewhere |
| 94 | 163 | $this->generateMap(); |
| 95 | 164 | } |
| 96 | - | |
| 165 | + | |
| 97 | 166 | /** |
| 98 | 167 | * Bind all obtained (through the map) filters to Wordpress hooks |
| 99 | - * | |
| 168 | + * | |
| 100 | 169 | */ |
| 101 | 170 | public function & bind() { |
| 102 | - | |
| 171 | + | |
| 103 | 172 | foreach ($this->filtersMap as $hookFullName => $hooks) { |
| 104 | - | |
| 173 | + | |
| 105 | 174 | foreach ($hooks as $hook) { |
| 106 | - | |
| 175 | + | |
| 107 | 176 | add_filter($hookFullName, $hook['handler'], 30); |
| 108 | 177 | } |
| 109 | 178 | |
| 110 | 179 | } |
| 111 | - | |
| 180 | + | |
| 112 | 181 | return $this; |
| 113 | 182 | } |
| 114 | - | |
| 183 | + | |
| 115 | 184 | /** |
| 116 | 185 | * Generate Hook names by adding the passed prefix to the hook name(s) |
| 117 | - * | |
| 186 | + * | |
| 118 | 187 | * @param mixed $hooks |
| 119 | 188 | * @param mixed $prefix |
| 120 | 189 | */ |
| 121 | 190 | public function genHooks($hooks, $prefix = '') { |
| 122 | - | |
| 123 | - // Bind hooks strighatly | |
| 191 | + | |
| 192 | + // Bind hooks strighatly | |
| 124 | 193 | foreach ($hooks as $locationName => $hook) { |
| 125 | - | |
| 194 | + | |
| 126 | 195 | // Create filters map |
| 127 | 196 | $hookFullName = "{$prefix}{$hook['name']}"; |
| 128 | - | |
| 197 | + | |
| 129 | 198 | $hook['locationName'] = $locationName; |
| 130 | 199 | $this->filtersMap[$hookFullName][$locationName] = $hook; |
| 131 | - | |
| 200 | + | |
| 132 | 201 | // Generate localized text map |
| 133 | 202 | $this->textMap[$hook['locationName']]['title'] = $hook['title']; |
| 134 | 203 | $this->textMap[$hook['locationName']]['text'] = $hook['text']; |
| 135 | - | |
| 204 | + | |
| 136 | 205 | // Make Group map |
| 137 | 206 | $this->groupMap[$hook['group']][] = $hook['locationName']; |
| 138 | 207 | } |
| 139 | - | |
| 208 | + | |
| 140 | 209 | } |
| 141 | - | |
| 210 | + | |
| 142 | 211 | /** |
| 143 | 212 | * Generate unattached hook names by adding either (wp_ or admin_ prefix to the hook name(s) |
| 144 | - * | |
| 213 | + * | |
| 145 | 214 | * @param mixed $filters |
| 146 | 215 | * @return CJTBlocksCouplingHookAttacher |
| 147 | 216 | */ |
| 148 | 217 | public function & genUnattachedHooks($filters) { |
| 149 | - | |
| 218 | + | |
| 150 | 219 | $this->genHooks($filters, is_admin() ? 'admin_' : 'wp_'); |
| 151 | - | |
| 220 | + | |
| 152 | 221 | return $this; |
| 153 | 222 | } |
| 154 | - | |
| 223 | + | |
| 155 | 224 | /** |
| 156 | 225 | * Generate all filter names by calling the FILTER-TYPE-HANDLER method |
| 157 | - * | |
| 226 | + * | |
| 158 | 227 | */ |
| 159 | 228 | public function generateMap() { |
| 160 | - | |
| 229 | + | |
| 161 | 230 | $typeHandler = $this->typeHandler; |
| 162 | - | |
| 231 | + | |
| 163 | 232 | foreach ($this->filters as $typeName => $filters) { |
| 164 | - | |
| 233 | + | |
| 165 | 234 | $typeName = ucfirst($typeName); |
| 166 | - | |
| 235 | + | |
| 167 | 236 | $attacherMethodName = "gen{$typeName}Hooks"; |
| 168 | - | |
| 237 | + | |
| 169 | 238 | $typeHandler->$attacherMethodName($filters); |
| 170 | 239 | } |
| 171 | 240 | } |
| 172 | - | |
| 241 | + | |
| 173 | 242 | /** |
| 174 | 243 | * put your comment there... |
| 175 | - * | |
| 244 | + * | |
| 176 | 245 | */ |
| 177 | 246 | public function getFiltersList() { |
| 178 | - | |
| 247 | + | |
| 179 | 248 | return $this->filtersMap; |
| 180 | - | |
| 249 | + | |
| 181 | 250 | } |
| 182 | - | |
| 251 | + | |
| 183 | 252 | /** |
| 184 | 253 | * put your comment there... |
| 185 | - * | |
| 254 | + * | |
| 186 | 255 | * @param mixed $group |
| 187 | 256 | */ |
| 188 | 257 | public function getGroupMap($group) { |
| 189 | - | |
| 258 | + | |
| 190 | 259 | return $this->groupMap[$group]; |
| 191 | 260 | } |
| 192 | - | |
| 261 | + | |
| 193 | 262 | /** |
| 194 | 263 | * put your comment there... |
| 195 | - * | |
| 264 | + * | |
| 196 | 265 | * @param mixed $filter |
| 197 | 266 | */ |
| 198 | 267 | public function getHooksByFilterName($filterName) { |
| 199 | - | |
| 268 | + | |
| 200 | 269 | return $this->filtersMap[$filterName]; |
| 201 | - | |
| 270 | + | |
| 202 | 271 | } |
| 203 | - | |
| 272 | + | |
| 204 | 273 | /** |
| 205 | 274 | * put your comment there... |
| 206 | - * | |
| 275 | + * | |
| 207 | 276 | * @param mixed $locationName |
| 208 | 277 | */ |
| 209 | 278 | public function getHookText($locationName, $prop = 'text') { |
| 210 | - | |
| 279 | + | |
| 211 | 280 | $text = isset($this->textMap[$locationName]) ? |
| 212 | 281 | $this->textMap[$locationName][$prop] : |
| 213 | 282 | ''; |
| 214 | - | |
| 283 | + | |
| 215 | 284 | return $text; |
| 216 | 285 | } |
| 217 | - | |
| 286 | + | |
| 218 | 287 | } |