Textpattern | PHP Cross Reference | Content Management Systems |
1 <?php 2 3 /* 4 * Textpattern Content Management System 5 * https://textpattern.com/ 6 * 7 * Copyright (C) 2020 The Textpattern Development Team 8 * 9 * This file is part of Textpattern. 10 * 11 * Textpattern is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License 13 * as published by the Free Software Foundation, version 2. 14 * 15 * Textpattern is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with Textpattern. If not, see <https://www.gnu.org/licenses/>. 22 */ 23 24 namespace Textpattern\Textfilter; 25 26 /** 27 * A registry of Textfilters interfaces those to the core. 28 * 29 * @since 4.6.0 30 * @package Textfilter 31 */ 32 33 class Registry implements \ArrayAccess, \IteratorAggregate, \Textpattern\Container\ReusableInterface 34 { 35 /** 36 * An array of filters. 37 * 38 * @var array 39 */ 40 41 protected $filters; 42 43 /** 44 * Stores an array of filter titles. 45 * 46 * @var array 47 */ 48 49 protected $titles; 50 51 /** 52 * Constructor. 53 * 54 * Creates core Textfilters according to a preference and registers all 55 * available filters with the core. 56 * 57 * This method triggers 'textfilter.register' callback 58 * event. 59 */ 60 61 public function __construct() 62 { 63 if ($filters = get_pref('admin_textfilter_classes')) { 64 foreach (do_list_unique($filters) as $filter) { 65 $filter[0] == '\\' or $filter = __NAMESPACE__.'\\'.$filter; 66 67 if (class_exists($filter)) { 68 new $filter; 69 } 70 } 71 } else { 72 new Plain(); 73 new Nl2Br(); 74 new Textile(); 75 } 76 77 $this->filters = array(); 78 callback_event('textfilter', 'register', 0, $this); 79 } 80 81 /** 82 * Gets an array map of filter keys vs. titles. 83 * 84 * @return array Map of 'key' => 'title' for all Textfilters 85 */ 86 87 public function getMap() 88 { 89 if ($this->titles === null) { 90 $this->titles = array(); 91 92 foreach ($this as $filter) { 93 $this->titles[$filter->getKey()] = $filter->title; 94 } 95 } 96 97 return $this->titles; 98 } 99 100 /** 101 * Filter raw input text by calling one of our known Textfilters by its key. 102 * 103 * Invokes the 'textfilter.filter' pre- and post-callbacks. 104 * 105 * @param string $key The Textfilter's key 106 * @param string $thing Raw input text 107 * @param array $context Filter context ('options' => array, 'field' => string, 'data' => mixed) 108 * @return string Filtered output text 109 * @throws Exception 110 */ 111 112 public function filter($key, $thing, $context) 113 { 114 // Preprocessing, anyone? 115 callback_event_ref('textfilter', 'filter', 0, $thing, $context); 116 117 if (isset($this[$key])) { 118 $thing = $this[$key]->filter($thing, $context['options']); 119 } else { 120 throw new \Exception(gTxt('invalid_argument', array('{name}' => 'key'))); 121 } 122 123 // Postprocessing, anyone? 124 callback_event_ref('textfilter', 'filter', 1, $thing, $context); 125 126 return $thing; 127 } 128 129 /** 130 * Get help URL for a certain Textfilter. 131 * 132 * @param string $key The Textfilter's key 133 * @return string URL endpoint for human-readable help 134 */ 135 136 public function getHelp($key) 137 { 138 if (isset($this[$key])) { 139 return $this[$key]->getHelp(); 140 } 141 142 return ''; 143 } 144 145 /** 146 * ArrayAccess interface to our set of filters. 147 * 148 * @param string $key 149 * @param string $filter 150 * @see ArrayAccess 151 */ 152 153 public function offsetSet($key, $filter) 154 { 155 if ($key === null) { 156 $key = $filter->getKey(); 157 } 158 159 $this->filters[$key] = $filter; 160 } 161 162 /** 163 * Returns the value at specified offset. 164 * 165 * @param string $key 166 * @return string The value 167 * @see ArrayAccess 168 */ 169 170 public function offsetGet($key) 171 { 172 if ($this->offsetExists($key)) { 173 return $this->filters[$key]; 174 } 175 176 return null; 177 } 178 179 /** 180 * Whether an offset exists. 181 * 182 * @param string $key 183 * @return bool 184 * @see ArrayAccess 185 */ 186 187 public function offsetExists($key) 188 { 189 return isset($this->filters[$key]); 190 } 191 192 /** 193 * Offset to unset. 194 * 195 * @param string $key 196 * @see ArrayAccess 197 */ 198 199 public function offsetUnset($key) 200 { 201 unset($this->filters[$key]); 202 } 203 204 /** 205 * IteratorAggregate interface. 206 * 207 * @return ArrayIterator 208 * @see IteratorAggregate 209 */ 210 211 public function getIterator() 212 { 213 return new \ArrayIterator($this->filters); 214 } 215 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title