@@ -10,272 +10,272 @@ |
||
| 10 | 10 | |
| 11 | 11 | class Translation |
| 12 | 12 | { |
| 13 | - const SEARCH_THRESHOLD = 3; |
|
| 13 | + const SEARCH_THRESHOLD = 3; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $entries; |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $entries; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $results; |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $results; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Returns all saved custom translations with translation context. |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 29 | - public function all() |
|
| 30 | - { |
|
| 31 | - $translations = $this->translations(); |
|
| 32 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
| 33 | - array_walk($translations, function (&$entry) use ($entries) { |
|
| 34 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 35 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 36 | - : ''; |
|
| 37 | - }); |
|
| 38 | - return $translations; |
|
| 39 | - } |
|
| 25 | + /** |
|
| 26 | + * Returns all saved custom translations with translation context. |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | + public function all() |
|
| 30 | + { |
|
| 31 | + $translations = $this->translations(); |
|
| 32 | + $entries = $this->filter($translations, $this->entries())->results(); |
|
| 33 | + array_walk($translations, function (&$entry) use ($entries) { |
|
| 34 | + $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 35 | + ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 36 | + : ''; |
|
| 37 | + }); |
|
| 38 | + return $translations; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - public function entries() |
|
| 45 | - { |
|
| 46 | - if (!isset($this->entries)) { |
|
| 47 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 48 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 49 | - $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
| 50 | - $this->entries = $entries; |
|
| 51 | - } |
|
| 52 | - return $this->entries; |
|
| 53 | - } |
|
| 41 | + /** |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + public function entries() |
|
| 45 | + { |
|
| 46 | + if (!isset($this->entries)) { |
|
| 47 | + $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 48 | + $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 49 | + $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
| 50 | + $this->entries = $entries; |
|
| 51 | + } |
|
| 52 | + return $this->entries; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @param array|null $entriesToExclude |
|
| 57 | - * @param array|null $entries |
|
| 58 | - * @return static |
|
| 59 | - */ |
|
| 60 | - public function exclude($entriesToExclude = null, $entries = null) |
|
| 61 | - { |
|
| 62 | - return $this->filter($entriesToExclude, $entries, false); |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * @param array|null $entriesToExclude |
|
| 57 | + * @param array|null $entries |
|
| 58 | + * @return static |
|
| 59 | + */ |
|
| 60 | + public function exclude($entriesToExclude = null, $entries = null) |
|
| 61 | + { |
|
| 62 | + return $this->filter($entriesToExclude, $entries, false); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @param string $potFile |
|
| 67 | - * @return array |
|
| 68 | - */ |
|
| 69 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 70 | - { |
|
| 71 | - try { |
|
| 72 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 73 | - foreach ($potEntries as $key => $entry) { |
|
| 74 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 75 | - } |
|
| 76 | - } catch (Exception $e) { |
|
| 77 | - glsr_log()->error($e->getMessage()); |
|
| 78 | - } |
|
| 79 | - return $entries; |
|
| 80 | - } |
|
| 65 | + /** |
|
| 66 | + * @param string $potFile |
|
| 67 | + * @return array |
|
| 68 | + */ |
|
| 69 | + public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 70 | + { |
|
| 71 | + try { |
|
| 72 | + $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 73 | + foreach ($potEntries as $key => $entry) { |
|
| 74 | + $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 75 | + } |
|
| 76 | + } catch (Exception $e) { |
|
| 77 | + glsr_log()->error($e->getMessage()); |
|
| 78 | + } |
|
| 79 | + return $entries; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * @param array|null $filterWith |
|
| 84 | - * @param array|null $entries |
|
| 85 | - * @param bool $intersect |
|
| 86 | - * @return static |
|
| 87 | - */ |
|
| 88 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 89 | - { |
|
| 90 | - if (!is_array($entries)) { |
|
| 91 | - $entries = $this->results; |
|
| 92 | - } |
|
| 93 | - if (!is_array($filterWith)) { |
|
| 94 | - $filterWith = $this->translations(); |
|
| 95 | - } |
|
| 96 | - $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
| 97 | - $this->results = $intersect |
|
| 98 | - ? array_intersect_key($entries, $keys) |
|
| 99 | - : array_diff_key($entries, $keys); |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 82 | + /** |
|
| 83 | + * @param array|null $filterWith |
|
| 84 | + * @param array|null $entries |
|
| 85 | + * @param bool $intersect |
|
| 86 | + * @return static |
|
| 87 | + */ |
|
| 88 | + public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 89 | + { |
|
| 90 | + if (!is_array($entries)) { |
|
| 91 | + $entries = $this->results; |
|
| 92 | + } |
|
| 93 | + if (!is_array($filterWith)) { |
|
| 94 | + $filterWith = $this->translations(); |
|
| 95 | + } |
|
| 96 | + $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
| 97 | + $this->results = $intersect |
|
| 98 | + ? array_intersect_key($entries, $keys) |
|
| 99 | + : array_diff_key($entries, $keys); |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @param string $template |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function render($template, array $entry) |
|
| 108 | - { |
|
| 109 | - $data = array_combine( |
|
| 110 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 111 | - $entry |
|
| 112 | - ); |
|
| 113 | - $data['data.class'] = $data['data.error'] = ''; |
|
| 114 | - if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
| 115 | - $data['data.class'] = 'is-invalid'; |
|
| 116 | - $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
| 117 | - } |
|
| 118 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 119 | - 'context' => $data, |
|
| 120 | - ]); |
|
| 121 | - } |
|
| 103 | + /** |
|
| 104 | + * @param string $template |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function render($template, array $entry) |
|
| 108 | + { |
|
| 109 | + $data = array_combine( |
|
| 110 | + array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 111 | + $entry |
|
| 112 | + ); |
|
| 113 | + $data['data.class'] = $data['data.error'] = ''; |
|
| 114 | + if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
| 115 | + $data['data.class'] = 'is-invalid'; |
|
| 116 | + $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
| 117 | + } |
|
| 118 | + return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 119 | + 'context' => $data, |
|
| 120 | + ]); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Returns a rendered string of all saved custom translations with translation context. |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - public function renderAll() |
|
| 128 | - { |
|
| 129 | - $rendered = ''; |
|
| 130 | - foreach ($this->all() as $index => $entry) { |
|
| 131 | - $entry['index'] = $index; |
|
| 132 | - $entry['prefix'] = OptionManager::databaseKey(); |
|
| 133 | - $rendered .= $this->render($entry['type'], $entry); |
|
| 134 | - } |
|
| 135 | - return $rendered; |
|
| 136 | - } |
|
| 123 | + /** |
|
| 124 | + * Returns a rendered string of all saved custom translations with translation context. |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + public function renderAll() |
|
| 128 | + { |
|
| 129 | + $rendered = ''; |
|
| 130 | + foreach ($this->all() as $index => $entry) { |
|
| 131 | + $entry['index'] = $index; |
|
| 132 | + $entry['prefix'] = OptionManager::databaseKey(); |
|
| 133 | + $rendered .= $this->render($entry['type'], $entry); |
|
| 134 | + } |
|
| 135 | + return $rendered; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * @param bool $resetAfterRender |
|
| 140 | - * @return string |
|
| 141 | - */ |
|
| 142 | - public function renderResults($resetAfterRender = true) |
|
| 143 | - { |
|
| 144 | - $rendered = ''; |
|
| 145 | - foreach ($this->results as $id => $entry) { |
|
| 146 | - $data = [ |
|
| 147 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 148 | - 'id' => $id, |
|
| 149 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 150 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 151 | - ]; |
|
| 152 | - $text = !empty($data['p1']) |
|
| 153 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 154 | - : $data['s1']; |
|
| 155 | - $rendered .= $this->render('result', [ |
|
| 156 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 157 | - 'text' => wp_strip_all_tags($text), |
|
| 158 | - ]); |
|
| 159 | - } |
|
| 160 | - if ($resetAfterRender) { |
|
| 161 | - $this->reset(); |
|
| 162 | - } |
|
| 163 | - return $rendered; |
|
| 164 | - } |
|
| 138 | + /** |
|
| 139 | + * @param bool $resetAfterRender |
|
| 140 | + * @return string |
|
| 141 | + */ |
|
| 142 | + public function renderResults($resetAfterRender = true) |
|
| 143 | + { |
|
| 144 | + $rendered = ''; |
|
| 145 | + foreach ($this->results as $id => $entry) { |
|
| 146 | + $data = [ |
|
| 147 | + 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 148 | + 'id' => $id, |
|
| 149 | + 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 150 | + 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 151 | + ]; |
|
| 152 | + $text = !empty($data['p1']) |
|
| 153 | + ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 154 | + : $data['s1']; |
|
| 155 | + $rendered .= $this->render('result', [ |
|
| 156 | + 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 157 | + 'text' => wp_strip_all_tags($text), |
|
| 158 | + ]); |
|
| 159 | + } |
|
| 160 | + if ($resetAfterRender) { |
|
| 161 | + $this->reset(); |
|
| 162 | + } |
|
| 163 | + return $rendered; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * @return void |
|
| 168 | - */ |
|
| 169 | - public function reset() |
|
| 170 | - { |
|
| 171 | - $this->results = []; |
|
| 172 | - } |
|
| 166 | + /** |
|
| 167 | + * @return void |
|
| 168 | + */ |
|
| 169 | + public function reset() |
|
| 170 | + { |
|
| 171 | + $this->results = []; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * @return array |
|
| 176 | - */ |
|
| 177 | - public function results() |
|
| 178 | - { |
|
| 179 | - $results = $this->results; |
|
| 180 | - $this->reset(); |
|
| 181 | - return $results; |
|
| 182 | - } |
|
| 174 | + /** |
|
| 175 | + * @return array |
|
| 176 | + */ |
|
| 177 | + public function results() |
|
| 178 | + { |
|
| 179 | + $results = $this->results; |
|
| 180 | + $this->reset(); |
|
| 181 | + return $results; |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * @param string $needle |
|
| 186 | - * @return static |
|
| 187 | - */ |
|
| 188 | - public function search($needle = '') |
|
| 189 | - { |
|
| 190 | - $this->reset(); |
|
| 191 | - $needle = trim(strtolower($needle)); |
|
| 192 | - foreach ($this->entries() as $key => $entry) { |
|
| 193 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 194 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 195 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 196 | - if (in_array($needle, [$single, $plural])) { |
|
| 197 | - $this->results[$key] = $entry; |
|
| 198 | - } |
|
| 199 | - } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 200 | - $this->results[$key] = $entry; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - return $this; |
|
| 204 | - } |
|
| 184 | + /** |
|
| 185 | + * @param string $needle |
|
| 186 | + * @return static |
|
| 187 | + */ |
|
| 188 | + public function search($needle = '') |
|
| 189 | + { |
|
| 190 | + $this->reset(); |
|
| 191 | + $needle = trim(strtolower($needle)); |
|
| 192 | + foreach ($this->entries() as $key => $entry) { |
|
| 193 | + $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 194 | + $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 195 | + if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 196 | + if (in_array($needle, [$single, $plural])) { |
|
| 197 | + $this->results[$key] = $entry; |
|
| 198 | + } |
|
| 199 | + } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 200 | + $this->results[$key] = $entry; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + return $this; |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Store the translations to avoid unnecessary loops. |
|
| 208 | - * @return array |
|
| 209 | - */ |
|
| 210 | - public function translations() |
|
| 211 | - { |
|
| 212 | - static $translations; |
|
| 213 | - if (empty($translations)) { |
|
| 214 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
| 215 | - $translations = isset($settings['strings']) |
|
| 216 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
| 217 | - : []; |
|
| 218 | - } |
|
| 219 | - return $translations; |
|
| 220 | - } |
|
| 206 | + /** |
|
| 207 | + * Store the translations to avoid unnecessary loops. |
|
| 208 | + * @return array |
|
| 209 | + */ |
|
| 210 | + public function translations() |
|
| 211 | + { |
|
| 212 | + static $translations; |
|
| 213 | + if (empty($translations)) { |
|
| 214 | + $settings = glsr(OptionManager::class)->get('settings'); |
|
| 215 | + $translations = isset($settings['strings']) |
|
| 216 | + ? $this->normalizeSettings((array) $settings['strings']) |
|
| 217 | + : []; |
|
| 218 | + } |
|
| 219 | + return $translations; |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - /** |
|
| 223 | - * @param string $key |
|
| 224 | - * @return string |
|
| 225 | - */ |
|
| 226 | - protected function getEntryString(array $entry, $key) |
|
| 227 | - { |
|
| 228 | - return isset($entry[$key]) |
|
| 229 | - ? implode('', (array) $entry[$key]) |
|
| 230 | - : ''; |
|
| 231 | - } |
|
| 222 | + /** |
|
| 223 | + * @param string $key |
|
| 224 | + * @return string |
|
| 225 | + */ |
|
| 226 | + protected function getEntryString(array $entry, $key) |
|
| 227 | + { |
|
| 228 | + return isset($entry[$key]) |
|
| 229 | + ? implode('', (array) $entry[$key]) |
|
| 230 | + : ''; |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * @return array |
|
| 235 | - */ |
|
| 236 | - protected function normalize(array $entries) |
|
| 237 | - { |
|
| 238 | - $keys = [ |
|
| 239 | - 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
| 240 | - ]; |
|
| 241 | - array_walk($entries, function (&$entry) use ($keys) { |
|
| 242 | - foreach ($keys as $key) { |
|
| 243 | - try { |
|
| 244 | - $entry = $this->normalizeEntryString($entry, $key); |
|
| 245 | - } catch (\TypeError $error) { |
|
| 246 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 247 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - }); |
|
| 251 | - return $entries; |
|
| 252 | - } |
|
| 233 | + /** |
|
| 234 | + * @return array |
|
| 235 | + */ |
|
| 236 | + protected function normalize(array $entries) |
|
| 237 | + { |
|
| 238 | + $keys = [ |
|
| 239 | + 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
| 240 | + ]; |
|
| 241 | + array_walk($entries, function (&$entry) use ($keys) { |
|
| 242 | + foreach ($keys as $key) { |
|
| 243 | + try { |
|
| 244 | + $entry = $this->normalizeEntryString($entry, $key); |
|
| 245 | + } catch (\TypeError $error) { |
|
| 246 | + glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 247 | + glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + }); |
|
| 251 | + return $entries; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * @param string $key |
|
| 256 | - * @return array |
|
| 257 | - */ |
|
| 258 | - protected function normalizeEntryString(array $entry, $key) |
|
| 259 | - { |
|
| 260 | - if (isset($entry[$key])) { |
|
| 261 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
| 262 | - } |
|
| 263 | - return $entry; |
|
| 264 | - } |
|
| 254 | + /** |
|
| 255 | + * @param string $key |
|
| 256 | + * @return array |
|
| 257 | + */ |
|
| 258 | + protected function normalizeEntryString(array $entry, $key) |
|
| 259 | + { |
|
| 260 | + if (isset($entry[$key])) { |
|
| 261 | + $entry[$key] = $this->getEntryString($entry, $key); |
|
| 262 | + } |
|
| 263 | + return $entry; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * @return array |
|
| 268 | - */ |
|
| 269 | - protected function normalizeSettings(array $strings) |
|
| 270 | - { |
|
| 271 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 272 | - $strings = array_filter($strings, 'is_array'); |
|
| 273 | - foreach ($strings as &$string) { |
|
| 274 | - $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
| 275 | - $string = wp_parse_args($string, $defaultString); |
|
| 276 | - } |
|
| 277 | - return array_filter($strings, function ($string) { |
|
| 278 | - return !empty($string['id']); |
|
| 279 | - }); |
|
| 280 | - } |
|
| 266 | + /** |
|
| 267 | + * @return array |
|
| 268 | + */ |
|
| 269 | + protected function normalizeSettings(array $strings) |
|
| 270 | + { |
|
| 271 | + $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 272 | + $strings = array_filter($strings, 'is_array'); |
|
| 273 | + foreach ($strings as &$string) { |
|
| 274 | + $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
| 275 | + $string = wp_parse_args($string, $defaultString); |
|
| 276 | + } |
|
| 277 | + return array_filter($strings, function ($string) { |
|
| 278 | + return !empty($string['id']); |
|
| 279 | + }); |
|
| 280 | + } |
|
| 281 | 281 | } |
@@ -9,433 +9,433 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Console |
| 11 | 11 | { |
| 12 | - const DEBUG = 0; // Detailed debug information |
|
| 13 | - const INFO = 1; // Interesting events |
|
| 14 | - const NOTICE = 2; // Normal but significant events |
|
| 15 | - const WARNING = 4; // Exceptional occurrences that are not errors |
|
| 16 | - const ERROR = 8; // Runtime errors that do not require immediate action |
|
| 17 | - const CRITICAL = 16; // Critical conditions |
|
| 18 | - const ALERT = 32; // Action must be taken immediately |
|
| 19 | - const EMERGENCY = 64; // System is unusable |
|
| 20 | - |
|
| 21 | - protected $file; |
|
| 22 | - protected $log; |
|
| 23 | - protected $logOnceKey = 'glsr_log_once'; |
|
| 24 | - |
|
| 25 | - public function __construct() |
|
| 26 | - { |
|
| 27 | - $this->file = glsr()->path('console.log'); |
|
| 28 | - $this->log = file_exists($this->file) |
|
| 29 | - ? file_get_contents($this->file) |
|
| 30 | - : ''; |
|
| 31 | - $this->reset(); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public function __toString() |
|
| 38 | - { |
|
| 39 | - return $this->get(); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Action must be taken immediately |
|
| 44 | - * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. |
|
| 45 | - * @param mixed $message |
|
| 46 | - * @param array $context |
|
| 47 | - * @return static |
|
| 48 | - */ |
|
| 49 | - public function alert($message, array $context = []) |
|
| 50 | - { |
|
| 51 | - return $this->log(static::ALERT, $message, $context); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return void |
|
| 56 | - */ |
|
| 57 | - public function clear() |
|
| 58 | - { |
|
| 59 | - $this->log = ''; |
|
| 60 | - file_put_contents($this->file, $this->log); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Critical conditions |
|
| 65 | - * Example: Application component unavailable, unexpected exception. |
|
| 66 | - * @param mixed $message |
|
| 67 | - * @param array $context |
|
| 68 | - * @return static |
|
| 69 | - */ |
|
| 70 | - public function critical($message, array $context = []) |
|
| 71 | - { |
|
| 72 | - return $this->log(static::CRITICAL, $message, $context); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Detailed debug information. |
|
| 77 | - * @param mixed $message |
|
| 78 | - * @param array $context |
|
| 79 | - * @return static |
|
| 80 | - */ |
|
| 81 | - public function debug($message, array $context = []) |
|
| 82 | - { |
|
| 83 | - return $this->log(static::DEBUG, $message, $context); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * System is unusable. |
|
| 88 | - * @param mixed $message |
|
| 89 | - * @param array $context |
|
| 90 | - * @return static |
|
| 91 | - */ |
|
| 92 | - public function emergency($message, array $context = []) |
|
| 93 | - { |
|
| 94 | - return $this->log(static::EMERGENCY, $message, $context); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
| 99 | - * @param mixed $message |
|
| 100 | - * @param array $context |
|
| 101 | - * @return static |
|
| 102 | - */ |
|
| 103 | - public function error($message, array $context = []) |
|
| 104 | - { |
|
| 105 | - return $this->log(static::ERROR, $message, $context); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @return string |
|
| 110 | - */ |
|
| 111 | - public function get() |
|
| 112 | - { |
|
| 113 | - return empty($this->log) |
|
| 114 | - ? __('Console is empty', 'site-reviews') |
|
| 115 | - : $this->log; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return int |
|
| 120 | - */ |
|
| 121 | - public function getLevel() |
|
| 122 | - { |
|
| 123 | - return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return array |
|
| 128 | - */ |
|
| 129 | - public function getLevels() |
|
| 130 | - { |
|
| 131 | - $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
| 132 | - return array_map('strtolower', array_flip($constants)); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public function humanLevel() |
|
| 139 | - { |
|
| 140 | - $level = $this->getLevel(); |
|
| 141 | - return sprintf('%s (%d)', strtoupper(glsr_get($this->getLevels(), $level, 'unknown')), $level); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param string|null $valueIfEmpty |
|
| 146 | - * @return string |
|
| 147 | - */ |
|
| 148 | - public function humanSize($valueIfEmpty = null) |
|
| 149 | - { |
|
| 150 | - $bytes = $this->size(); |
|
| 151 | - if (empty($bytes) && is_string($valueIfEmpty)) { |
|
| 152 | - return $valueIfEmpty; |
|
| 153 | - } |
|
| 154 | - $exponent = floor(log(max($bytes, 1), 1024)); |
|
| 155 | - return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Interesting events |
|
| 160 | - * Example: User logs in, SQL logs. |
|
| 161 | - * @param mixed $message |
|
| 162 | - * @param array $context |
|
| 163 | - * @return static |
|
| 164 | - */ |
|
| 165 | - public function info($message, array $context = []) |
|
| 166 | - { |
|
| 167 | - return $this->log(static::INFO, $message, $context); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param int $level |
|
| 172 | - * @param mixed $message |
|
| 173 | - * @param array $context |
|
| 174 | - * @param string $backtraceLine |
|
| 175 | - * @return static |
|
| 176 | - */ |
|
| 177 | - public function log($level, $message, $context = [], $backtraceLine = '') |
|
| 178 | - { |
|
| 179 | - if (empty($backtraceLine)) { |
|
| 180 | - $backtraceLine = $this->getBacktraceLine(); |
|
| 181 | - } |
|
| 182 | - if ($this->canLogEntry($level, $backtraceLine)) { |
|
| 183 | - $levelName = glsr_get($this->getLevels(), $level); |
|
| 184 | - $context = glsr(Helper::class)->consolidateArray($context); |
|
| 185 | - $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
| 186 | - $message = $this->interpolate($message, $context); |
|
| 187 | - $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
| 188 | - file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
| 189 | - apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
| 190 | - $this->reset(); |
|
| 191 | - } |
|
| 192 | - return $this; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * @return void |
|
| 197 | - */ |
|
| 198 | - public function logOnce() |
|
| 199 | - { |
|
| 200 | - $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
| 201 | - $levels = $this->getLevels(); |
|
| 202 | - foreach ($once as $entry) { |
|
| 203 | - $levelName = glsr_get($entry, 'level'); |
|
| 204 | - if (!in_array($levelName, $levels)) { |
|
| 205 | - continue; |
|
| 206 | - } |
|
| 207 | - $level = glsr_get(array_flip($levels), $levelName); |
|
| 208 | - $message = glsr_get($entry, 'message'); |
|
| 209 | - $backtraceLine = glsr_get($entry, 'backtrace'); |
|
| 210 | - $this->log($level, $message, [], $backtraceLine); |
|
| 211 | - } |
|
| 212 | - glsr()->{$this->logOnceKey} = []; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Normal but significant events. |
|
| 217 | - * @param mixed $message |
|
| 218 | - * @param array $context |
|
| 219 | - * @return static |
|
| 220 | - */ |
|
| 221 | - public function notice($message, array $context = []) |
|
| 222 | - { |
|
| 223 | - return $this->log(static::NOTICE, $message, $context); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param string $levelName |
|
| 228 | - * @param string $handle |
|
| 229 | - * @param mixed $data |
|
| 230 | - * @return void |
|
| 231 | - */ |
|
| 232 | - public function once($levelName, $handle, $data) |
|
| 233 | - { |
|
| 234 | - $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
| 235 | - $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
| 236 | - return glsr_get($entry, 'level') == $levelName |
|
| 237 | - && glsr_get($entry, 'handle') == $handle; |
|
| 238 | - }); |
|
| 239 | - if (!empty($filtered)) { |
|
| 240 | - return; |
|
| 241 | - } |
|
| 242 | - $once[] = [ |
|
| 243 | - 'backtrace' => $this->getBacktraceLineFromData($data), |
|
| 244 | - 'handle' => $handle, |
|
| 245 | - 'level' => $levelName, |
|
| 246 | - 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
| 247 | - ]; |
|
| 248 | - glsr()->{$this->logOnceKey} = $once; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * @return int |
|
| 253 | - */ |
|
| 254 | - public function size() |
|
| 255 | - { |
|
| 256 | - return file_exists($this->file) |
|
| 257 | - ? filesize($this->file) |
|
| 258 | - : 0; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Exceptional occurrences that are not errors |
|
| 263 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. |
|
| 264 | - * @param mixed $message |
|
| 265 | - * @param array $context |
|
| 266 | - * @return static |
|
| 267 | - */ |
|
| 268 | - public function warning($message, array $context = []) |
|
| 269 | - { |
|
| 270 | - return $this->log(static::WARNING, $message, $context); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * @param array $backtrace |
|
| 275 | - * @param int $index |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - protected function buildBacktraceLine($backtrace, $index) |
|
| 279 | - { |
|
| 280 | - return sprintf('%s:%s', |
|
| 281 | - glsr_get($backtrace, $index.'.file'), // realpath |
|
| 282 | - glsr_get($backtrace, $index.'.line') |
|
| 283 | - ); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @param string $levelName |
|
| 288 | - * @param mixed $message |
|
| 289 | - * @param string $backtraceLine |
|
| 290 | - * @return string |
|
| 291 | - */ |
|
| 292 | - protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
| 293 | - { |
|
| 294 | - return sprintf('[%s] %s [%s] %s', |
|
| 295 | - current_time('mysql'), |
|
| 296 | - strtoupper($levelName), |
|
| 297 | - $backtraceLine, |
|
| 298 | - $message |
|
| 299 | - ); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @param int $level |
|
| 304 | - * @return bool |
|
| 305 | - */ |
|
| 306 | - protected function canLogEntry($level, $backtraceLine) |
|
| 307 | - { |
|
| 308 | - $levelExists = array_key_exists($level, $this->getLevels()); |
|
| 309 | - if (false === strpos($backtraceLine, glsr()->path())) { |
|
| 310 | - return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
| 311 | - } |
|
| 312 | - return $levelExists && $level >= $this->getLevel(); |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * @return void|string |
|
| 317 | - */ |
|
| 318 | - protected function getBacktraceLine() |
|
| 319 | - { |
|
| 320 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
| 321 | - $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
| 322 | - if (false !== $search) { |
|
| 323 | - $index = '{closure}' == glsr_get($backtrace, ($search + 2).'.function') |
|
| 324 | - ? $search + 4 |
|
| 325 | - : $search + 1; |
|
| 326 | - return $this->buildBacktraceLine($backtrace, $index); |
|
| 327 | - } |
|
| 328 | - return 'Unknown'; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * @param mixed $data |
|
| 333 | - * @return string |
|
| 334 | - */ |
|
| 335 | - protected function getBacktraceLineFromData($data) |
|
| 336 | - { |
|
| 337 | - $backtrace = $data instanceof Throwable |
|
| 338 | - ? $data->getTrace() |
|
| 339 | - : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
| 340 | - return $this->buildBacktraceLine($backtrace, 0); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * @param mixed $data |
|
| 345 | - * @return string |
|
| 346 | - */ |
|
| 347 | - protected function getMessageFromData($data) |
|
| 348 | - { |
|
| 349 | - return $data instanceof Throwable |
|
| 350 | - ? $this->normalizeThrowableMessage($data->getMessage()) |
|
| 351 | - : print_r($data, 1); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Interpolates context values into the message placeholders. |
|
| 356 | - * @param mixed $message |
|
| 357 | - * @param array $context |
|
| 358 | - * @return string |
|
| 359 | - */ |
|
| 360 | - protected function interpolate($message, $context = []) |
|
| 361 | - { |
|
| 362 | - if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
| 363 | - return print_r($message, true); |
|
| 364 | - } |
|
| 365 | - $replace = []; |
|
| 366 | - foreach ($context as $key => $value) { |
|
| 367 | - $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
| 368 | - } |
|
| 369 | - return strtr($message, $replace); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * @param mixed $value |
|
| 374 | - * @return bool |
|
| 375 | - */ |
|
| 376 | - protected function isObjectOrArray($value) |
|
| 377 | - { |
|
| 378 | - return is_object($value) || is_array($value); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * @param string $backtraceLine |
|
| 383 | - * @return string |
|
| 384 | - */ |
|
| 385 | - protected function normalizeBacktraceLine($backtraceLine) |
|
| 386 | - { |
|
| 387 | - $search = [ |
|
| 388 | - glsr()->path('plugin/'), |
|
| 389 | - glsr()->path('plugin/', false), |
|
| 390 | - trailingslashit(glsr()->path()), |
|
| 391 | - trailingslashit(glsr()->path('', false)), |
|
| 392 | - WP_CONTENT_DIR, |
|
| 393 | - ABSPATH, |
|
| 394 | - ]; |
|
| 395 | - return str_replace(array_unique($search), '', $backtraceLine); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * @param string $message |
|
| 400 | - * @return string |
|
| 401 | - */ |
|
| 402 | - protected function normalizeThrowableMessage($message) |
|
| 403 | - { |
|
| 404 | - $calledIn = strpos($message, ', called in'); |
|
| 405 | - return false !== $calledIn |
|
| 406 | - ? substr($message, 0, $calledIn) |
|
| 407 | - : $message; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * @param mixed $value |
|
| 412 | - * @return string |
|
| 413 | - */ |
|
| 414 | - protected function normalizeValue($value) |
|
| 415 | - { |
|
| 416 | - if ($value instanceof DateTime) { |
|
| 417 | - $value = $value->format('Y-m-d H:i:s'); |
|
| 418 | - } elseif ($this->isObjectOrArray($value)) { |
|
| 419 | - $value = json_encode($value); |
|
| 420 | - } |
|
| 421 | - return (string) $value; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * @return void |
|
| 426 | - */ |
|
| 427 | - protected function reset() |
|
| 428 | - { |
|
| 429 | - if ($this->size() <= pow(1024, 2) / 8) { |
|
| 430 | - return; |
|
| 431 | - } |
|
| 432 | - $this->clear(); |
|
| 433 | - file_put_contents( |
|
| 434 | - $this->file, |
|
| 435 | - $this->buildLogEntry( |
|
| 436 | - static::NOTICE, |
|
| 437 | - __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
| 438 | - ) |
|
| 439 | - ); |
|
| 440 | - } |
|
| 12 | + const DEBUG = 0; // Detailed debug information |
|
| 13 | + const INFO = 1; // Interesting events |
|
| 14 | + const NOTICE = 2; // Normal but significant events |
|
| 15 | + const WARNING = 4; // Exceptional occurrences that are not errors |
|
| 16 | + const ERROR = 8; // Runtime errors that do not require immediate action |
|
| 17 | + const CRITICAL = 16; // Critical conditions |
|
| 18 | + const ALERT = 32; // Action must be taken immediately |
|
| 19 | + const EMERGENCY = 64; // System is unusable |
|
| 20 | + |
|
| 21 | + protected $file; |
|
| 22 | + protected $log; |
|
| 23 | + protected $logOnceKey = 'glsr_log_once'; |
|
| 24 | + |
|
| 25 | + public function __construct() |
|
| 26 | + { |
|
| 27 | + $this->file = glsr()->path('console.log'); |
|
| 28 | + $this->log = file_exists($this->file) |
|
| 29 | + ? file_get_contents($this->file) |
|
| 30 | + : ''; |
|
| 31 | + $this->reset(); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public function __toString() |
|
| 38 | + { |
|
| 39 | + return $this->get(); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Action must be taken immediately |
|
| 44 | + * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. |
|
| 45 | + * @param mixed $message |
|
| 46 | + * @param array $context |
|
| 47 | + * @return static |
|
| 48 | + */ |
|
| 49 | + public function alert($message, array $context = []) |
|
| 50 | + { |
|
| 51 | + return $this->log(static::ALERT, $message, $context); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return void |
|
| 56 | + */ |
|
| 57 | + public function clear() |
|
| 58 | + { |
|
| 59 | + $this->log = ''; |
|
| 60 | + file_put_contents($this->file, $this->log); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Critical conditions |
|
| 65 | + * Example: Application component unavailable, unexpected exception. |
|
| 66 | + * @param mixed $message |
|
| 67 | + * @param array $context |
|
| 68 | + * @return static |
|
| 69 | + */ |
|
| 70 | + public function critical($message, array $context = []) |
|
| 71 | + { |
|
| 72 | + return $this->log(static::CRITICAL, $message, $context); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Detailed debug information. |
|
| 77 | + * @param mixed $message |
|
| 78 | + * @param array $context |
|
| 79 | + * @return static |
|
| 80 | + */ |
|
| 81 | + public function debug($message, array $context = []) |
|
| 82 | + { |
|
| 83 | + return $this->log(static::DEBUG, $message, $context); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * System is unusable. |
|
| 88 | + * @param mixed $message |
|
| 89 | + * @param array $context |
|
| 90 | + * @return static |
|
| 91 | + */ |
|
| 92 | + public function emergency($message, array $context = []) |
|
| 93 | + { |
|
| 94 | + return $this->log(static::EMERGENCY, $message, $context); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
| 99 | + * @param mixed $message |
|
| 100 | + * @param array $context |
|
| 101 | + * @return static |
|
| 102 | + */ |
|
| 103 | + public function error($message, array $context = []) |
|
| 104 | + { |
|
| 105 | + return $this->log(static::ERROR, $message, $context); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @return string |
|
| 110 | + */ |
|
| 111 | + public function get() |
|
| 112 | + { |
|
| 113 | + return empty($this->log) |
|
| 114 | + ? __('Console is empty', 'site-reviews') |
|
| 115 | + : $this->log; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return int |
|
| 120 | + */ |
|
| 121 | + public function getLevel() |
|
| 122 | + { |
|
| 123 | + return intval(apply_filters('site-reviews/console/level', static::INFO)); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return array |
|
| 128 | + */ |
|
| 129 | + public function getLevels() |
|
| 130 | + { |
|
| 131 | + $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
| 132 | + return array_map('strtolower', array_flip($constants)); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public function humanLevel() |
|
| 139 | + { |
|
| 140 | + $level = $this->getLevel(); |
|
| 141 | + return sprintf('%s (%d)', strtoupper(glsr_get($this->getLevels(), $level, 'unknown')), $level); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param string|null $valueIfEmpty |
|
| 146 | + * @return string |
|
| 147 | + */ |
|
| 148 | + public function humanSize($valueIfEmpty = null) |
|
| 149 | + { |
|
| 150 | + $bytes = $this->size(); |
|
| 151 | + if (empty($bytes) && is_string($valueIfEmpty)) { |
|
| 152 | + return $valueIfEmpty; |
|
| 153 | + } |
|
| 154 | + $exponent = floor(log(max($bytes, 1), 1024)); |
|
| 155 | + return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Interesting events |
|
| 160 | + * Example: User logs in, SQL logs. |
|
| 161 | + * @param mixed $message |
|
| 162 | + * @param array $context |
|
| 163 | + * @return static |
|
| 164 | + */ |
|
| 165 | + public function info($message, array $context = []) |
|
| 166 | + { |
|
| 167 | + return $this->log(static::INFO, $message, $context); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param int $level |
|
| 172 | + * @param mixed $message |
|
| 173 | + * @param array $context |
|
| 174 | + * @param string $backtraceLine |
|
| 175 | + * @return static |
|
| 176 | + */ |
|
| 177 | + public function log($level, $message, $context = [], $backtraceLine = '') |
|
| 178 | + { |
|
| 179 | + if (empty($backtraceLine)) { |
|
| 180 | + $backtraceLine = $this->getBacktraceLine(); |
|
| 181 | + } |
|
| 182 | + if ($this->canLogEntry($level, $backtraceLine)) { |
|
| 183 | + $levelName = glsr_get($this->getLevels(), $level); |
|
| 184 | + $context = glsr(Helper::class)->consolidateArray($context); |
|
| 185 | + $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
| 186 | + $message = $this->interpolate($message, $context); |
|
| 187 | + $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
| 188 | + file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
| 189 | + apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
| 190 | + $this->reset(); |
|
| 191 | + } |
|
| 192 | + return $this; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * @return void |
|
| 197 | + */ |
|
| 198 | + public function logOnce() |
|
| 199 | + { |
|
| 200 | + $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
| 201 | + $levels = $this->getLevels(); |
|
| 202 | + foreach ($once as $entry) { |
|
| 203 | + $levelName = glsr_get($entry, 'level'); |
|
| 204 | + if (!in_array($levelName, $levels)) { |
|
| 205 | + continue; |
|
| 206 | + } |
|
| 207 | + $level = glsr_get(array_flip($levels), $levelName); |
|
| 208 | + $message = glsr_get($entry, 'message'); |
|
| 209 | + $backtraceLine = glsr_get($entry, 'backtrace'); |
|
| 210 | + $this->log($level, $message, [], $backtraceLine); |
|
| 211 | + } |
|
| 212 | + glsr()->{$this->logOnceKey} = []; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Normal but significant events. |
|
| 217 | + * @param mixed $message |
|
| 218 | + * @param array $context |
|
| 219 | + * @return static |
|
| 220 | + */ |
|
| 221 | + public function notice($message, array $context = []) |
|
| 222 | + { |
|
| 223 | + return $this->log(static::NOTICE, $message, $context); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @param string $levelName |
|
| 228 | + * @param string $handle |
|
| 229 | + * @param mixed $data |
|
| 230 | + * @return void |
|
| 231 | + */ |
|
| 232 | + public function once($levelName, $handle, $data) |
|
| 233 | + { |
|
| 234 | + $once = glsr(Helper::class)->consolidateArray(glsr()->{$this->logOnceKey}); |
|
| 235 | + $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
| 236 | + return glsr_get($entry, 'level') == $levelName |
|
| 237 | + && glsr_get($entry, 'handle') == $handle; |
|
| 238 | + }); |
|
| 239 | + if (!empty($filtered)) { |
|
| 240 | + return; |
|
| 241 | + } |
|
| 242 | + $once[] = [ |
|
| 243 | + 'backtrace' => $this->getBacktraceLineFromData($data), |
|
| 244 | + 'handle' => $handle, |
|
| 245 | + 'level' => $levelName, |
|
| 246 | + 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
| 247 | + ]; |
|
| 248 | + glsr()->{$this->logOnceKey} = $once; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * @return int |
|
| 253 | + */ |
|
| 254 | + public function size() |
|
| 255 | + { |
|
| 256 | + return file_exists($this->file) |
|
| 257 | + ? filesize($this->file) |
|
| 258 | + : 0; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Exceptional occurrences that are not errors |
|
| 263 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. |
|
| 264 | + * @param mixed $message |
|
| 265 | + * @param array $context |
|
| 266 | + * @return static |
|
| 267 | + */ |
|
| 268 | + public function warning($message, array $context = []) |
|
| 269 | + { |
|
| 270 | + return $this->log(static::WARNING, $message, $context); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * @param array $backtrace |
|
| 275 | + * @param int $index |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + protected function buildBacktraceLine($backtrace, $index) |
|
| 279 | + { |
|
| 280 | + return sprintf('%s:%s', |
|
| 281 | + glsr_get($backtrace, $index.'.file'), // realpath |
|
| 282 | + glsr_get($backtrace, $index.'.line') |
|
| 283 | + ); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @param string $levelName |
|
| 288 | + * @param mixed $message |
|
| 289 | + * @param string $backtraceLine |
|
| 290 | + * @return string |
|
| 291 | + */ |
|
| 292 | + protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
| 293 | + { |
|
| 294 | + return sprintf('[%s] %s [%s] %s', |
|
| 295 | + current_time('mysql'), |
|
| 296 | + strtoupper($levelName), |
|
| 297 | + $backtraceLine, |
|
| 298 | + $message |
|
| 299 | + ); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @param int $level |
|
| 304 | + * @return bool |
|
| 305 | + */ |
|
| 306 | + protected function canLogEntry($level, $backtraceLine) |
|
| 307 | + { |
|
| 308 | + $levelExists = array_key_exists($level, $this->getLevels()); |
|
| 309 | + if (false === strpos($backtraceLine, glsr()->path())) { |
|
| 310 | + return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
| 311 | + } |
|
| 312 | + return $levelExists && $level >= $this->getLevel(); |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * @return void|string |
|
| 317 | + */ |
|
| 318 | + protected function getBacktraceLine() |
|
| 319 | + { |
|
| 320 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
| 321 | + $search = array_search('log', glsr_array_column($backtrace, 'function')); |
|
| 322 | + if (false !== $search) { |
|
| 323 | + $index = '{closure}' == glsr_get($backtrace, ($search + 2).'.function') |
|
| 324 | + ? $search + 4 |
|
| 325 | + : $search + 1; |
|
| 326 | + return $this->buildBacktraceLine($backtrace, $index); |
|
| 327 | + } |
|
| 328 | + return 'Unknown'; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * @param mixed $data |
|
| 333 | + * @return string |
|
| 334 | + */ |
|
| 335 | + protected function getBacktraceLineFromData($data) |
|
| 336 | + { |
|
| 337 | + $backtrace = $data instanceof Throwable |
|
| 338 | + ? $data->getTrace() |
|
| 339 | + : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
| 340 | + return $this->buildBacktraceLine($backtrace, 0); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * @param mixed $data |
|
| 345 | + * @return string |
|
| 346 | + */ |
|
| 347 | + protected function getMessageFromData($data) |
|
| 348 | + { |
|
| 349 | + return $data instanceof Throwable |
|
| 350 | + ? $this->normalizeThrowableMessage($data->getMessage()) |
|
| 351 | + : print_r($data, 1); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Interpolates context values into the message placeholders. |
|
| 356 | + * @param mixed $message |
|
| 357 | + * @param array $context |
|
| 358 | + * @return string |
|
| 359 | + */ |
|
| 360 | + protected function interpolate($message, $context = []) |
|
| 361 | + { |
|
| 362 | + if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
| 363 | + return print_r($message, true); |
|
| 364 | + } |
|
| 365 | + $replace = []; |
|
| 366 | + foreach ($context as $key => $value) { |
|
| 367 | + $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
| 368 | + } |
|
| 369 | + return strtr($message, $replace); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * @param mixed $value |
|
| 374 | + * @return bool |
|
| 375 | + */ |
|
| 376 | + protected function isObjectOrArray($value) |
|
| 377 | + { |
|
| 378 | + return is_object($value) || is_array($value); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * @param string $backtraceLine |
|
| 383 | + * @return string |
|
| 384 | + */ |
|
| 385 | + protected function normalizeBacktraceLine($backtraceLine) |
|
| 386 | + { |
|
| 387 | + $search = [ |
|
| 388 | + glsr()->path('plugin/'), |
|
| 389 | + glsr()->path('plugin/', false), |
|
| 390 | + trailingslashit(glsr()->path()), |
|
| 391 | + trailingslashit(glsr()->path('', false)), |
|
| 392 | + WP_CONTENT_DIR, |
|
| 393 | + ABSPATH, |
|
| 394 | + ]; |
|
| 395 | + return str_replace(array_unique($search), '', $backtraceLine); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * @param string $message |
|
| 400 | + * @return string |
|
| 401 | + */ |
|
| 402 | + protected function normalizeThrowableMessage($message) |
|
| 403 | + { |
|
| 404 | + $calledIn = strpos($message, ', called in'); |
|
| 405 | + return false !== $calledIn |
|
| 406 | + ? substr($message, 0, $calledIn) |
|
| 407 | + : $message; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * @param mixed $value |
|
| 412 | + * @return string |
|
| 413 | + */ |
|
| 414 | + protected function normalizeValue($value) |
|
| 415 | + { |
|
| 416 | + if ($value instanceof DateTime) { |
|
| 417 | + $value = $value->format('Y-m-d H:i:s'); |
|
| 418 | + } elseif ($this->isObjectOrArray($value)) { |
|
| 419 | + $value = json_encode($value); |
|
| 420 | + } |
|
| 421 | + return (string) $value; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * @return void |
|
| 426 | + */ |
|
| 427 | + protected function reset() |
|
| 428 | + { |
|
| 429 | + if ($this->size() <= pow(1024, 2) / 8) { |
|
| 430 | + return; |
|
| 431 | + } |
|
| 432 | + $this->clear(); |
|
| 433 | + file_put_contents( |
|
| 434 | + $this->file, |
|
| 435 | + $this->buildLogEntry( |
|
| 436 | + static::NOTICE, |
|
| 437 | + __('Console was automatically cleared (128 KB maximum size)', 'site-reviews') |
|
| 438 | + ) |
|
| 439 | + ); |
|
| 440 | + } |
|
| 441 | 441 | } |
@@ -19,12 +19,12 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Thing extends BaseType |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var array |
|
| 24 | - * @see http://schema.org/{property_name} |
|
| 25 | - */ |
|
| 26 | - public $allowed = [ |
|
| 27 | - 'additionalType', 'alternateName', 'description', 'disambiguatingDescription', 'identifier', |
|
| 28 | - 'image', 'mainEntityOfPage', 'name', 'potentialAction', 'sameAs', 'url', |
|
| 29 | - ]; |
|
| 22 | + /** |
|
| 23 | + * @var array |
|
| 24 | + * @see http://schema.org/{property_name} |
|
| 25 | + */ |
|
| 26 | + public $allowed = [ |
|
| 27 | + 'additionalType', 'alternateName', 'description', 'disambiguatingDescription', 'identifier', |
|
| 28 | + 'image', 'mainEntityOfPage', 'name', 'potentialAction', 'sameAs', 'url', |
|
| 29 | + ]; |
|
| 30 | 30 | } |
@@ -4,19 +4,19 @@ |
||
| 4 | 4 | |
| 5 | 5 | class UnknownType extends BaseType |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @var array |
|
| 9 | - * @see http://schema.org/{property_name} |
|
| 10 | - */ |
|
| 11 | - public $allowed = [ |
|
| 12 | - 'aggregateRating', |
|
| 13 | - ]; |
|
| 7 | + /** |
|
| 8 | + * @var array |
|
| 9 | + * @see http://schema.org/{property_name} |
|
| 10 | + */ |
|
| 11 | + public $allowed = [ |
|
| 12 | + 'aggregateRating', |
|
| 13 | + ]; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - * @see http://schema.org/{property_name} |
|
| 18 | - */ |
|
| 19 | - public $parents = [ |
|
| 20 | - 'Thing', |
|
| 21 | - ]; |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + * @see http://schema.org/{property_name} |
|
| 18 | + */ |
|
| 19 | + public $parents = [ |
|
| 20 | + 'Thing', |
|
| 21 | + ]; |
|
| 22 | 22 | } |
@@ -4,22 +4,22 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface Type |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Return an array representation of the type. If the array contains child types, |
|
| 9 | - * their context needs to be stripped if it's the same. |
|
| 10 | - * @return array |
|
| 11 | - */ |
|
| 12 | - public function toArray(); |
|
| 7 | + /** |
|
| 8 | + * Return an array representation of the type. If the array contains child types, |
|
| 9 | + * their context needs to be stripped if it's the same. |
|
| 10 | + * @return array |
|
| 11 | + */ |
|
| 12 | + public function toArray(); |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Create a json-ld script tag for this type, built from the data that `toArray` returns. |
|
| 16 | - * @return string |
|
| 17 | - */ |
|
| 18 | - public function toScript(); |
|
| 14 | + /** |
|
| 15 | + * Create a json-ld script tag for this type, built from the data that `toArray` returns. |
|
| 16 | + * @return string |
|
| 17 | + */ |
|
| 18 | + public function toScript(); |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Create a json-ld script tag for this type, built from the data that `toArray` returns. |
|
| 22 | - * @return string |
|
| 23 | - */ |
|
| 24 | - public function __toString(); |
|
| 20 | + /** |
|
| 21 | + * Create a json-ld script tag for this type, built from the data that `toArray` returns. |
|
| 22 | + * @return string |
|
| 23 | + */ |
|
| 24 | + public function __toString(); |
|
| 25 | 25 | } |
@@ -57,166 +57,166 @@ |
||
| 57 | 57 | */ |
| 58 | 58 | class Organization extends BaseType |
| 59 | 59 | { |
| 60 | - /** |
|
| 61 | - * The schema.org Actions mechanism benefited from extensive discussions across the Web |
|
| 62 | - * standards community around W3C, in particular from the [Hydra project](http://purl.org/hydra/) |
|
| 63 | - * community group. |
|
| 64 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_ActionCollabClass |
|
| 65 | - */ |
|
| 66 | - const ActionCollabClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_ActionCollabClass'; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * This element is based on the work of the Automotive Ontology Working Group, |
|
| 70 | - * see [www.automotive-ontology.org](http://www.automotive-ontology.org) for details. |
|
| 71 | - * Many class and property definitions are inspired by or based on abstracts from Wikipedia, |
|
| 72 | - * the free encyclopedia. |
|
| 73 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#Automotive_Ontology_Working_Group |
|
| 74 | - */ |
|
| 75 | - const AutomotiveOntologyWGClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#Automotive_Ontology_Working_Group'; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * The W3C [Schema Bib Extend](http://www.w3.org/community/schemabibex/) (BibEx) group led the |
|
| 79 | - * work to improve schema.org for bibliographic information, including terms for periodicals, |
|
| 80 | - * articles and multi-volume works. The design was inspired in places (e.g. [[pageStart]], |
|
| 81 | - * [[pageEnd]], [[pagination]]) by the [Bibliographic Ontology](http://bibliontology.com/), |
|
| 82 | - * 'bibo'. |
|
| 83 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_bibex |
|
| 84 | - */ |
|
| 85 | - const BibExTerm = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_bibex'; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * This class is based upon W3C DCAT work, and benefits from collaboration around the DCAT, ADMS |
|
| 89 | - * and VoID vocabularies. See http://www.w3.org/wiki/WebSchemas/Datasets for full details and |
|
| 90 | - * mappings. |
|
| 91 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_DatasetClass |
|
| 92 | - */ |
|
| 93 | - const DatasetClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_DatasetClass'; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * This element is based on the work of the Financial Industry Business Ontology project (see |
|
| 97 | - * [http://www.fibo.org/schema](http://www.fibo.org/schema) for details), in support of the W3C |
|
| 98 | - * Financial Industry Business Ontology Community Group |
|
| 99 | - * ([http://www.fibo.org/community](http://www.fibo.org/community)). Many class and property |
|
| 100 | - * definitions are inspired by or based on [http://www.fibo.org](http://www.fibo.org). |
|
| 101 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#FIBO |
|
| 102 | - */ |
|
| 103 | - const FIBO = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#FIBO'; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The implementation and use of Legal Entity Identifier (LEI) is supported by Global Legal |
|
| 107 | - * Entity Identifier Foundation [https://www.gleif.org](https://www.gleif.org). |
|
| 108 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#GLEIF |
|
| 109 | - */ |
|
| 110 | - const GLEIF = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#GLEIF'; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * This class is derived from the GoodRelations Vocabulary for E-Commerce, created by Martin |
|
| 114 | - * Hepp. GoodRelations is a data model for sharing e-commerce data on the Web that can be |
|
| 115 | - * expressed in a variety of syntaxes, including RDFa and HTML5 Microdata. More information |
|
| 116 | - * about GoodRelations can be found at |
|
| 117 | - * [http://purl.org/goodrelations/](http://purl.org/goodrelations/). |
|
| 118 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_GoodRelationsClass |
|
| 119 | - */ |
|
| 120 | - const GoodRelationsClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_GoodRelationsClass'; |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * This term [uses](http://blog.schema.org/2012/11/good-relations-and-schemaorg.html) |
|
| 124 | - * terminology from the GoodRelations Vocabulary for E-Commerce, created by Martin Hepp. |
|
| 125 | - * GoodRelations is a data model for sharing e-commerce data on the Web. More information about |
|
| 126 | - * GoodRelations can be found at |
|
| 127 | - * [http://purl.org/goodrelations/](http://purl.org/goodrelations/). |
|
| 128 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_GoodRelationsTerms |
|
| 129 | - */ |
|
| 130 | - const GoodRelationsTerms = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_GoodRelationsTerms'; |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * This element is based on work by the Web Applications for the Future Internet Lab, Institute |
|
| 134 | - * of Informatics and Telematics, Pisa, Italy. |
|
| 135 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#IIT-CNR.it |
|
| 136 | - */ |
|
| 137 | - const IITCNRit = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#IIT-CNR.it'; |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * This class is based on the work of the LRMI project, see lrmi.net for details. |
|
| 141 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_LRMIClass |
|
| 142 | - */ |
|
| 143 | - const LRMIClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_LRMIClass'; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * This vocabulary was improved through collaboration with the MusicBrainz project |
|
| 147 | - * ([www.musicbrainz.org](http://www.musicbrainz.org)), and is partially inspired by the |
|
| 148 | - * MusicBrainz and |
|
| 149 | - * [Music Ontology](http://musicontology.com/docs/getting-started.html) schemas. |
|
| 150 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#MBZ |
|
| 151 | - */ |
|
| 152 | - const MBZ = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#MBZ'; |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * This element is based on the STI Accommodation Ontology, see <a |
|
| 156 | - * href="http://ontologies.sti-innsbruck.at/acco/ns.html">http://ontologies.sti-innsbruck.at/acco/ns.html</a> |
|
| 157 | - * for details. |
|
| 158 | - * Many class and property definitions are inspired by or based on abstracts from Wikipedia, |
|
| 159 | - * the free encyclopedia. |
|
| 160 | - * @see https://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#STI_Accommodation_Ontology |
|
| 161 | - */ |
|
| 162 | - const STI_Accommodation_Ontology = 'https://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#STI_Accommodation_Ontology'; |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * The Question/Answer types were [based |
|
| 166 | - * on](https://www.w3.org/wiki/WebSchemas/QASchemaResearch) the Stack Overflow API. |
|
| 167 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_QAStackExchange |
|
| 168 | - */ |
|
| 169 | - const Stack_Exchange = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_QAStackExchange'; |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * This term and associated definitions draws upon the work of [The Trust |
|
| 173 | - * Project](http://thetrustproject.org/). |
|
| 174 | - * @see https://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#TP-draws |
|
| 175 | - */ |
|
| 176 | - const The_Trust_Project = 'https://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#TP-draws'; |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * This element is based on the work of the [Tourism Structured Web Data Community |
|
| 180 | - * Group](https://www.w3.org/community/tourismdata). |
|
| 181 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#Tourism |
|
| 182 | - */ |
|
| 183 | - const Tourism = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#Tourism'; |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * This class contains information contributed by |
|
| 187 | - * [http://wikidoc.org>WikiDoc](http://wikidoc.org>WikiDoc). |
|
| 188 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_WikiDoc |
|
| 189 | - */ |
|
| 190 | - const WikiDoc = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_WikiDoc'; |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * This class contains derivatives of IPTC rNews properties. rNews is a data model of publishing |
|
| 194 | - * metadata with serializations currently available for RDFa as well as HTML5 Microdata. More |
|
| 195 | - * information about the IPTC and rNews can be found at [rnews.org](http://rnews.org). |
|
| 196 | - * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews |
|
| 197 | - */ |
|
| 198 | - const rNews = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews'; |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @var array |
|
| 202 | - * @see http://schema.org/{property_name} |
|
| 203 | - */ |
|
| 204 | - public $allowed = [ |
|
| 205 | - 'address', 'aggregateRating', 'areaServed', 'award', 'awards', 'brand', 'contactPoint', |
|
| 206 | - 'contactPoints', 'department', 'dissolutionDate', 'duns', 'email', 'employee', 'employees', |
|
| 207 | - 'event', 'events', 'faxNumber', 'founder', 'founders', 'foundingDate', 'foundingLocation', |
|
| 208 | - 'funder', 'globalLocationNumber', 'hasOfferCatalog', 'hasPOS', 'isicV4', 'legalName', |
|
| 209 | - 'leiCode', 'location', 'logo', 'makesOffer', 'member', 'memberOf', 'members', 'naics', |
|
| 210 | - 'numberOfEmployees', 'offeredBy', 'owns', 'parentOrganization', 'publishingPrinciples', |
|
| 211 | - 'review', 'reviews', 'seeks', 'serviceArea', 'sponsor', 'subOrganization', 'taxID', |
|
| 212 | - 'telephone', 'vatID', |
|
| 213 | - ]; |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @var array |
|
| 217 | - * @see http://schema.org/{property_name} |
|
| 218 | - */ |
|
| 219 | - public $parents = [ |
|
| 220 | - 'Thing', |
|
| 221 | - ]; |
|
| 60 | + /** |
|
| 61 | + * The schema.org Actions mechanism benefited from extensive discussions across the Web |
|
| 62 | + * standards community around W3C, in particular from the [Hydra project](http://purl.org/hydra/) |
|
| 63 | + * community group. |
|
| 64 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_ActionCollabClass |
|
| 65 | + */ |
|
| 66 | + const ActionCollabClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_ActionCollabClass'; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * This element is based on the work of the Automotive Ontology Working Group, |
|
| 70 | + * see [www.automotive-ontology.org](http://www.automotive-ontology.org) for details. |
|
| 71 | + * Many class and property definitions are inspired by or based on abstracts from Wikipedia, |
|
| 72 | + * the free encyclopedia. |
|
| 73 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#Automotive_Ontology_Working_Group |
|
| 74 | + */ |
|
| 75 | + const AutomotiveOntologyWGClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#Automotive_Ontology_Working_Group'; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * The W3C [Schema Bib Extend](http://www.w3.org/community/schemabibex/) (BibEx) group led the |
|
| 79 | + * work to improve schema.org for bibliographic information, including terms for periodicals, |
|
| 80 | + * articles and multi-volume works. The design was inspired in places (e.g. [[pageStart]], |
|
| 81 | + * [[pageEnd]], [[pagination]]) by the [Bibliographic Ontology](http://bibliontology.com/), |
|
| 82 | + * 'bibo'. |
|
| 83 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_bibex |
|
| 84 | + */ |
|
| 85 | + const BibExTerm = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_bibex'; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * This class is based upon W3C DCAT work, and benefits from collaboration around the DCAT, ADMS |
|
| 89 | + * and VoID vocabularies. See http://www.w3.org/wiki/WebSchemas/Datasets for full details and |
|
| 90 | + * mappings. |
|
| 91 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_DatasetClass |
|
| 92 | + */ |
|
| 93 | + const DatasetClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_DatasetClass'; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * This element is based on the work of the Financial Industry Business Ontology project (see |
|
| 97 | + * [http://www.fibo.org/schema](http://www.fibo.org/schema) for details), in support of the W3C |
|
| 98 | + * Financial Industry Business Ontology Community Group |
|
| 99 | + * ([http://www.fibo.org/community](http://www.fibo.org/community)). Many class and property |
|
| 100 | + * definitions are inspired by or based on [http://www.fibo.org](http://www.fibo.org). |
|
| 101 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#FIBO |
|
| 102 | + */ |
|
| 103 | + const FIBO = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#FIBO'; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The implementation and use of Legal Entity Identifier (LEI) is supported by Global Legal |
|
| 107 | + * Entity Identifier Foundation [https://www.gleif.org](https://www.gleif.org). |
|
| 108 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#GLEIF |
|
| 109 | + */ |
|
| 110 | + const GLEIF = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#GLEIF'; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * This class is derived from the GoodRelations Vocabulary for E-Commerce, created by Martin |
|
| 114 | + * Hepp. GoodRelations is a data model for sharing e-commerce data on the Web that can be |
|
| 115 | + * expressed in a variety of syntaxes, including RDFa and HTML5 Microdata. More information |
|
| 116 | + * about GoodRelations can be found at |
|
| 117 | + * [http://purl.org/goodrelations/](http://purl.org/goodrelations/). |
|
| 118 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_GoodRelationsClass |
|
| 119 | + */ |
|
| 120 | + const GoodRelationsClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_GoodRelationsClass'; |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * This term [uses](http://blog.schema.org/2012/11/good-relations-and-schemaorg.html) |
|
| 124 | + * terminology from the GoodRelations Vocabulary for E-Commerce, created by Martin Hepp. |
|
| 125 | + * GoodRelations is a data model for sharing e-commerce data on the Web. More information about |
|
| 126 | + * GoodRelations can be found at |
|
| 127 | + * [http://purl.org/goodrelations/](http://purl.org/goodrelations/). |
|
| 128 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_GoodRelationsTerms |
|
| 129 | + */ |
|
| 130 | + const GoodRelationsTerms = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_GoodRelationsTerms'; |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * This element is based on work by the Web Applications for the Future Internet Lab, Institute |
|
| 134 | + * of Informatics and Telematics, Pisa, Italy. |
|
| 135 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#IIT-CNR.it |
|
| 136 | + */ |
|
| 137 | + const IITCNRit = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#IIT-CNR.it'; |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * This class is based on the work of the LRMI project, see lrmi.net for details. |
|
| 141 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_LRMIClass |
|
| 142 | + */ |
|
| 143 | + const LRMIClass = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_LRMIClass'; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * This vocabulary was improved through collaboration with the MusicBrainz project |
|
| 147 | + * ([www.musicbrainz.org](http://www.musicbrainz.org)), and is partially inspired by the |
|
| 148 | + * MusicBrainz and |
|
| 149 | + * [Music Ontology](http://musicontology.com/docs/getting-started.html) schemas. |
|
| 150 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#MBZ |
|
| 151 | + */ |
|
| 152 | + const MBZ = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#MBZ'; |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * This element is based on the STI Accommodation Ontology, see <a |
|
| 156 | + * href="http://ontologies.sti-innsbruck.at/acco/ns.html">http://ontologies.sti-innsbruck.at/acco/ns.html</a> |
|
| 157 | + * for details. |
|
| 158 | + * Many class and property definitions are inspired by or based on abstracts from Wikipedia, |
|
| 159 | + * the free encyclopedia. |
|
| 160 | + * @see https://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#STI_Accommodation_Ontology |
|
| 161 | + */ |
|
| 162 | + const STI_Accommodation_Ontology = 'https://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#STI_Accommodation_Ontology'; |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * The Question/Answer types were [based |
|
| 166 | + * on](https://www.w3.org/wiki/WebSchemas/QASchemaResearch) the Stack Overflow API. |
|
| 167 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_QAStackExchange |
|
| 168 | + */ |
|
| 169 | + const Stack_Exchange = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_QAStackExchange'; |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * This term and associated definitions draws upon the work of [The Trust |
|
| 173 | + * Project](http://thetrustproject.org/). |
|
| 174 | + * @see https://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#TP-draws |
|
| 175 | + */ |
|
| 176 | + const The_Trust_Project = 'https://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#TP-draws'; |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * This element is based on the work of the [Tourism Structured Web Data Community |
|
| 180 | + * Group](https://www.w3.org/community/tourismdata). |
|
| 181 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#Tourism |
|
| 182 | + */ |
|
| 183 | + const Tourism = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#Tourism'; |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * This class contains information contributed by |
|
| 187 | + * [http://wikidoc.org>WikiDoc](http://wikidoc.org>WikiDoc). |
|
| 188 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_WikiDoc |
|
| 189 | + */ |
|
| 190 | + const WikiDoc = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_WikiDoc'; |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * This class contains derivatives of IPTC rNews properties. rNews is a data model of publishing |
|
| 194 | + * metadata with serializations currently available for RDFa as well as HTML5 Microdata. More |
|
| 195 | + * information about the IPTC and rNews can be found at [rnews.org](http://rnews.org). |
|
| 196 | + * @see http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews |
|
| 197 | + */ |
|
| 198 | + const rNews = 'http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews'; |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @var array |
|
| 202 | + * @see http://schema.org/{property_name} |
|
| 203 | + */ |
|
| 204 | + public $allowed = [ |
|
| 205 | + 'address', 'aggregateRating', 'areaServed', 'award', 'awards', 'brand', 'contactPoint', |
|
| 206 | + 'contactPoints', 'department', 'dissolutionDate', 'duns', 'email', 'employee', 'employees', |
|
| 207 | + 'event', 'events', 'faxNumber', 'founder', 'founders', 'foundingDate', 'foundingLocation', |
|
| 208 | + 'funder', 'globalLocationNumber', 'hasOfferCatalog', 'hasPOS', 'isicV4', 'legalName', |
|
| 209 | + 'leiCode', 'location', 'logo', 'makesOffer', 'member', 'memberOf', 'members', 'naics', |
|
| 210 | + 'numberOfEmployees', 'offeredBy', 'owns', 'parentOrganization', 'publishingPrinciples', |
|
| 211 | + 'review', 'reviews', 'seeks', 'serviceArea', 'sponsor', 'subOrganization', 'taxID', |
|
| 212 | + 'telephone', 'vatID', |
|
| 213 | + ]; |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @var array |
|
| 217 | + * @see http://schema.org/{property_name} |
|
| 218 | + */ |
|
| 219 | + public $parents = [ |
|
| 220 | + 'Thing', |
|
| 221 | + ]; |
|
| 222 | 222 | } |
@@ -9,11 +9,11 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Intangible extends BaseType |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var array |
|
| 14 | - * @see http://schema.org/{property_name} |
|
| 15 | - */ |
|
| 16 | - public $parents = [ |
|
| 17 | - 'Thing', |
|
| 18 | - ]; |
|
| 12 | + /** |
|
| 13 | + * @var array |
|
| 14 | + * @see http://schema.org/{property_name} |
|
| 15 | + */ |
|
| 16 | + public $parents = [ |
|
| 17 | + 'Thing', |
|
| 18 | + ]; |
|
| 19 | 19 | } |
@@ -37,23 +37,23 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | class Place extends BaseType |
| 39 | 39 | { |
| 40 | - /** |
|
| 41 | - * @var array |
|
| 42 | - * @see http://schema.org/{property_name} |
|
| 43 | - */ |
|
| 44 | - public $allowed = [ |
|
| 45 | - 'additionalProperty', 'address', 'aggregateRating', 'amenityFeature', 'branchCode', |
|
| 46 | - 'containedIn', 'containedInPlace', 'containsPlace', 'event', 'events', 'faxNumber', 'geo', |
|
| 47 | - 'globalLocationNumber', 'hasMap', 'isAccessibleForFree', 'isicV4', 'logo', 'map', 'maps', |
|
| 48 | - 'maximumAttendeeCapacity', 'openingHoursSpecification', 'photo', 'photos', 'publicAccess', |
|
| 49 | - 'review', 'reviews', 'smokingAllowed', 'specialOpeningHoursSpecification', 'telephone', |
|
| 50 | - ]; |
|
| 40 | + /** |
|
| 41 | + * @var array |
|
| 42 | + * @see http://schema.org/{property_name} |
|
| 43 | + */ |
|
| 44 | + public $allowed = [ |
|
| 45 | + 'additionalProperty', 'address', 'aggregateRating', 'amenityFeature', 'branchCode', |
|
| 46 | + 'containedIn', 'containedInPlace', 'containsPlace', 'event', 'events', 'faxNumber', 'geo', |
|
| 47 | + 'globalLocationNumber', 'hasMap', 'isAccessibleForFree', 'isicV4', 'logo', 'map', 'maps', |
|
| 48 | + 'maximumAttendeeCapacity', 'openingHoursSpecification', 'photo', 'photos', 'publicAccess', |
|
| 49 | + 'review', 'reviews', 'smokingAllowed', 'specialOpeningHoursSpecification', 'telephone', |
|
| 50 | + ]; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @var array |
|
| 54 | - * @see http://schema.org/{property_name} |
|
| 55 | - */ |
|
| 56 | - public $parents = [ |
|
| 57 | - 'Thing', |
|
| 58 | - ]; |
|
| 52 | + /** |
|
| 53 | + * @var array |
|
| 54 | + * @see http://schema.org/{property_name} |
|
| 55 | + */ |
|
| 56 | + public $parents = [ |
|
| 57 | + 'Thing', |
|
| 58 | + ]; |
|
| 59 | 59 | } |
@@ -11,19 +11,19 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Review extends BaseType |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var array |
|
| 16 | - * @see http://schema.org/{property_name} |
|
| 17 | - */ |
|
| 18 | - public $allowed = [ |
|
| 19 | - 'itemReviewed', 'reviewBody', 'reviewRating', |
|
| 20 | - ]; |
|
| 14 | + /** |
|
| 15 | + * @var array |
|
| 16 | + * @see http://schema.org/{property_name} |
|
| 17 | + */ |
|
| 18 | + public $allowed = [ |
|
| 19 | + 'itemReviewed', 'reviewBody', 'reviewRating', |
|
| 20 | + ]; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var array |
|
| 24 | - * @see http://schema.org/{property_name} |
|
| 25 | - */ |
|
| 26 | - public $parents = [ |
|
| 27 | - 'CreativeWork', |
|
| 28 | - ]; |
|
| 22 | + /** |
|
| 23 | + * @var array |
|
| 24 | + * @see http://schema.org/{property_name} |
|
| 25 | + */ |
|
| 26 | + public $parents = [ |
|
| 27 | + 'CreativeWork', |
|
| 28 | + ]; |
|
| 29 | 29 | } |