@@ -8,137 +8,137 @@ |
||
8 | 8 | */ |
9 | 9 | class GL_Plugin_Check_v3 |
10 | 10 | { |
11 | - const MIN_PHP_VERSION = '5.6.0'; |
|
12 | - const MIN_WORDPRESS_VERSION = '4.7.0'; |
|
11 | + const MIN_PHP_VERSION = '5.6.0'; |
|
12 | + const MIN_WORDPRESS_VERSION = '4.7.0'; |
|
13 | 13 | |
14 | - /** |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $file; |
|
14 | + /** |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $file; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected $versions; |
|
19 | + /** |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected $versions; |
|
23 | 23 | |
24 | - /** |
|
25 | - * @param string $file |
|
26 | - */ |
|
27 | - public function __construct($file, array $versions = array()) |
|
28 | - { |
|
29 | - $this->file = realpath($file); |
|
30 | - $this->versions = wp_parse_args($versions, array( |
|
31 | - 'php' => static::MIN_PHP_VERSION, |
|
32 | - 'wordpress' => static::MIN_WORDPRESS_VERSION, |
|
33 | - )); |
|
34 | - } |
|
24 | + /** |
|
25 | + * @param string $file |
|
26 | + */ |
|
27 | + public function __construct($file, array $versions = array()) |
|
28 | + { |
|
29 | + $this->file = realpath($file); |
|
30 | + $this->versions = wp_parse_args($versions, array( |
|
31 | + 'php' => static::MIN_PHP_VERSION, |
|
32 | + 'wordpress' => static::MIN_WORDPRESS_VERSION, |
|
33 | + )); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public function canProceed() |
|
40 | - { |
|
41 | - if ($this->isValid()) { |
|
42 | - return true; |
|
43 | - } |
|
44 | - add_action('activated_plugin', array($this, 'deactivate')); |
|
45 | - add_action('admin_notices', array($this, 'deactivate')); |
|
46 | - return false; |
|
47 | - } |
|
36 | + /** |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public function canProceed() |
|
40 | + { |
|
41 | + if ($this->isValid()) { |
|
42 | + return true; |
|
43 | + } |
|
44 | + add_action('activated_plugin', array($this, 'deactivate')); |
|
45 | + add_action('admin_notices', array($this, 'deactivate')); |
|
46 | + return false; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @return bool |
|
51 | - */ |
|
52 | - public function isPhpValid() |
|
53 | - { |
|
54 | - return !version_compare(PHP_VERSION, $this->versions['php'], '<'); |
|
55 | - } |
|
49 | + /** |
|
50 | + * @return bool |
|
51 | + */ |
|
52 | + public function isPhpValid() |
|
53 | + { |
|
54 | + return !version_compare(PHP_VERSION, $this->versions['php'], '<'); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return bool |
|
59 | - */ |
|
60 | - public function isValid() |
|
61 | - { |
|
62 | - return $this->isPhpValid() && $this->isWpValid(); |
|
63 | - } |
|
57 | + /** |
|
58 | + * @return bool |
|
59 | + */ |
|
60 | + public function isValid() |
|
61 | + { |
|
62 | + return $this->isPhpValid() && $this->isWpValid(); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function isWpValid() |
|
69 | - { |
|
70 | - global $wp_version; |
|
71 | - return !version_compare($wp_version, $this->versions['wordpress'], '<'); |
|
72 | - } |
|
65 | + /** |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function isWpValid() |
|
69 | + { |
|
70 | + global $wp_version; |
|
71 | + return !version_compare($wp_version, $this->versions['wordpress'], '<'); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @param string $plugin |
|
76 | - * @return void |
|
77 | - */ |
|
78 | - public function deactivate($plugin) |
|
79 | - { |
|
80 | - if ($this->isValid()) { |
|
81 | - return; |
|
82 | - } |
|
83 | - $pluginSlug = plugin_basename($this->file); |
|
84 | - if ($plugin == $pluginSlug) { |
|
85 | - $this->redirect(); //exit |
|
86 | - } |
|
87 | - $pluginData = get_file_data($this->file, array('name' => 'Plugin Name'), 'plugin'); |
|
88 | - deactivate_plugins($pluginSlug); |
|
89 | - $this->printNotice($pluginData['name']); |
|
90 | - } |
|
74 | + /** |
|
75 | + * @param string $plugin |
|
76 | + * @return void |
|
77 | + */ |
|
78 | + public function deactivate($plugin) |
|
79 | + { |
|
80 | + if ($this->isValid()) { |
|
81 | + return; |
|
82 | + } |
|
83 | + $pluginSlug = plugin_basename($this->file); |
|
84 | + if ($plugin == $pluginSlug) { |
|
85 | + $this->redirect(); //exit |
|
86 | + } |
|
87 | + $pluginData = get_file_data($this->file, array('name' => 'Plugin Name'), 'plugin'); |
|
88 | + deactivate_plugins($pluginSlug); |
|
89 | + $this->printNotice($pluginData['name']); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @return array |
|
94 | - */ |
|
95 | - protected function getMessages() |
|
96 | - { |
|
97 | - return array( |
|
98 | - __('The %s plugin was deactivated.', 'site-reviews'), |
|
99 | - __('This plugin requires %s or greater in order to work properly.', 'site-reviews'), |
|
100 | - __('Please contact your hosting provider or server administrator to upgrade the version of PHP on your server (your server is running PHP version %s), or try to find an alternative plugin.', 'site-reviews'), |
|
101 | - __('PHP version', 'site-reviews'), |
|
102 | - __('WordPress version', 'site-reviews'), |
|
103 | - __('Update WordPress', 'site-reviews'), |
|
104 | - __('You can use the %s plugin to restore %s to the previous version.', 'site-reviews'), |
|
105 | - ); |
|
106 | - } |
|
92 | + /** |
|
93 | + * @return array |
|
94 | + */ |
|
95 | + protected function getMessages() |
|
96 | + { |
|
97 | + return array( |
|
98 | + __('The %s plugin was deactivated.', 'site-reviews'), |
|
99 | + __('This plugin requires %s or greater in order to work properly.', 'site-reviews'), |
|
100 | + __('Please contact your hosting provider or server administrator to upgrade the version of PHP on your server (your server is running PHP version %s), or try to find an alternative plugin.', 'site-reviews'), |
|
101 | + __('PHP version', 'site-reviews'), |
|
102 | + __('WordPress version', 'site-reviews'), |
|
103 | + __('Update WordPress', 'site-reviews'), |
|
104 | + __('You can use the %s plugin to restore %s to the previous version.', 'site-reviews'), |
|
105 | + ); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * @param string $pluginName |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - protected function printNotice($pluginName) |
|
113 | - { |
|
114 | - $noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>'; |
|
115 | - $messages = $this->getMessages(); |
|
116 | - $rollbackMessage = sprintf('<strong>'.$messages[6].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/">WP Rollback</a>', $pluginName); |
|
117 | - if (!$this->isPhpValid()) { |
|
118 | - printf($noticeTemplate, |
|
119 | - sprintf($messages[0], $pluginName), |
|
120 | - sprintf($messages[1], $messages[3].' '.$this->versions['php']), |
|
121 | - sprintf($messages[2], PHP_VERSION).'</p><p>'.$rollbackMessage |
|
122 | - ); |
|
123 | - } elseif (!$this->isWpValid()) { |
|
124 | - printf($noticeTemplate, |
|
125 | - sprintf($messages[0], $pluginName), |
|
126 | - sprintf($messages[1], $messages[4].' '.$this->versions['wordpress']), |
|
127 | - $rollbackMessage.'</p><p>'.sprintf('<a href="%s">%s</a>', admin_url('update-core.php'), $messages[5]) |
|
128 | - ); |
|
129 | - } |
|
130 | - } |
|
108 | + /** |
|
109 | + * @param string $pluginName |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + protected function printNotice($pluginName) |
|
113 | + { |
|
114 | + $noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>'; |
|
115 | + $messages = $this->getMessages(); |
|
116 | + $rollbackMessage = sprintf('<strong>'.$messages[6].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/">WP Rollback</a>', $pluginName); |
|
117 | + if (!$this->isPhpValid()) { |
|
118 | + printf($noticeTemplate, |
|
119 | + sprintf($messages[0], $pluginName), |
|
120 | + sprintf($messages[1], $messages[3].' '.$this->versions['php']), |
|
121 | + sprintf($messages[2], PHP_VERSION).'</p><p>'.$rollbackMessage |
|
122 | + ); |
|
123 | + } elseif (!$this->isWpValid()) { |
|
124 | + printf($noticeTemplate, |
|
125 | + sprintf($messages[0], $pluginName), |
|
126 | + sprintf($messages[1], $messages[4].' '.$this->versions['wordpress']), |
|
127 | + $rollbackMessage.'</p><p>'.sprintf('<a href="%s">%s</a>', admin_url('update-core.php'), $messages[5]) |
|
128 | + ); |
|
129 | + } |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * @return void |
|
134 | - */ |
|
135 | - protected function redirect() |
|
136 | - { |
|
137 | - wp_safe_redirect(self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s', |
|
138 | - filter_input(INPUT_GET, 'plugin_status'), |
|
139 | - filter_input(INPUT_GET, 'paged'), |
|
140 | - filter_input(INPUT_GET, 's') |
|
141 | - ))); |
|
142 | - exit; |
|
143 | - } |
|
132 | + /** |
|
133 | + * @return void |
|
134 | + */ |
|
135 | + protected function redirect() |
|
136 | + { |
|
137 | + wp_safe_redirect(self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s', |
|
138 | + filter_input(INPUT_GET, 'plugin_status'), |
|
139 | + filter_input(INPUT_GET, 'paged'), |
|
140 | + filter_input(INPUT_GET, 's') |
|
141 | + ))); |
|
142 | + exit; |
|
143 | + } |
|
144 | 144 | } |
@@ -9,239 +9,239 @@ |
||
9 | 9 | |
10 | 10 | class Settings |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array |
|
14 | - */ |
|
15 | - public $settings; |
|
16 | - |
|
17 | - /** |
|
18 | - * @param string $id |
|
19 | - * @return string |
|
20 | - */ |
|
21 | - public function buildFields($id) |
|
22 | - { |
|
23 | - $this->settings = glsr(DefaultsManager::class)->settings(); |
|
24 | - $method = glsr(Helper::class)->buildMethodName($id, 'getTemplateDataFor'); |
|
25 | - $data = !method_exists($this, $method) |
|
26 | - ? $this->getTemplateData($id) |
|
27 | - : $this->$method($id); |
|
28 | - return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - protected function getFieldDefault(array $field) |
|
35 | - { |
|
36 | - return glsr_get($field, 'default'); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - protected function getFieldNameForDependsOn($path) |
|
43 | - { |
|
44 | - $fieldName = glsr(Helper::class)->convertPathToName($path, OptionManager::databaseKey()); |
|
45 | - return $this->isMultiDependency($path) |
|
46 | - ? $fieldName.'[]' |
|
47 | - : $fieldName; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - protected function getSettingFields($path) |
|
54 | - { |
|
55 | - return array_filter($this->settings, function ($key) use ($path) { |
|
56 | - return glsr(Helper::class)->startsWith($path, $key); |
|
57 | - }, ARRAY_FILTER_USE_KEY); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - protected function getSettingRows(array $fields) |
|
64 | - { |
|
65 | - $rows = ''; |
|
66 | - foreach ($fields as $name => $field) { |
|
67 | - $field = wp_parse_args($field, [ |
|
68 | - 'is_setting' => true, |
|
69 | - 'name' => $name, |
|
70 | - ]); |
|
71 | - $rows.= new Field($this->normalize($field)); |
|
72 | - } |
|
73 | - return $rows; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param string $id |
|
78 | - * @return array |
|
79 | - */ |
|
80 | - protected function getTemplateData($id) |
|
81 | - { |
|
82 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
83 | - return [ |
|
84 | - 'context' => [ |
|
85 | - 'rows' => $this->getSettingRows($fields), |
|
86 | - ], |
|
87 | - ]; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $id |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - protected function getTemplateDataForAddons($id) |
|
95 | - { |
|
96 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
97 | - $settings = glsr(Helper::class)->convertDotNotationArray($fields); |
|
98 | - $settingKeys = array_keys($settings['settings']['addons']); |
|
99 | - $results = []; |
|
100 | - foreach ($settingKeys as $key) { |
|
101 | - $addonFields = array_filter($fields, function ($path) use ($key) { |
|
102 | - return glsr(Helper::class)->startsWith('settings.addons.'.$key, $path); |
|
103 | - }, ARRAY_FILTER_USE_KEY); |
|
104 | - $results[$key] = $this->getSettingRows($addonFields); |
|
105 | - } |
|
106 | - ksort($results); |
|
107 | - return [ |
|
108 | - 'settings' => $results, |
|
109 | - ]; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param string $id |
|
114 | - * @return array |
|
115 | - */ |
|
116 | - protected function getTemplateDataForLicenses($id) |
|
117 | - { |
|
118 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
119 | - ksort($fields); |
|
120 | - return [ |
|
121 | - 'context' => [ |
|
122 | - 'rows' => $this->getSettingRows($fields), |
|
123 | - ], |
|
124 | - ]; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - protected function getTemplateDataForTranslations() |
|
131 | - { |
|
132 | - $translations = glsr(Translation::class)->renderAll(); |
|
133 | - $class = empty($translations) |
|
134 | - ? 'glsr-hidden' |
|
135 | - : ''; |
|
136 | - return [ |
|
137 | - 'context' => [ |
|
138 | - 'class' => $class, |
|
139 | - 'database_key' => OptionManager::databaseKey(), |
|
140 | - 'translations' => $translations, |
|
141 | - ], |
|
142 | - ]; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param string $path |
|
147 | - * @param string|array $expectedValue |
|
148 | - * @return bool |
|
149 | - */ |
|
150 | - protected function isFieldHidden($path, $expectedValue) |
|
151 | - { |
|
152 | - $optionValue = glsr(OptionManager::class)->get( |
|
153 | - $path, |
|
154 | - glsr(Helper::class)->dataGet(glsr()->defaults, $path) |
|
155 | - ); |
|
156 | - if (is_array($expectedValue)) { |
|
157 | - return is_array($optionValue) |
|
158 | - ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
159 | - : !in_array($optionValue, $expectedValue); |
|
160 | - } |
|
161 | - return $optionValue != $expectedValue; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - protected function isMultiDependency($path) |
|
168 | - { |
|
169 | - if (isset($this->settings[$path])) { |
|
170 | - $field = $this->settings[$path]; |
|
171 | - return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
172 | - || !empty($field['multiple']); |
|
173 | - } |
|
174 | - return false; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - protected function normalize(array $field) |
|
181 | - { |
|
182 | - $field = $this->normalizeDependsOn($field); |
|
183 | - $field = $this->normalizeLabelAndLegend($field); |
|
184 | - $field = $this->normalizeValue($field); |
|
185 | - return $field; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @return array |
|
190 | - */ |
|
191 | - protected function normalizeDependsOn(array $field) |
|
192 | - { |
|
193 | - if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
194 | - $isFieldHidden = false; |
|
195 | - $conditions = []; |
|
196 | - foreach ($field['depends_on'] as $path => $value) { |
|
197 | - $conditions[] = [ |
|
198 | - 'name' => $this->getFieldNameForDependsOn($path), |
|
199 | - 'value' => $value, |
|
200 | - ]; |
|
201 | - if ($this->isFieldHidden($path, $value)) { |
|
202 | - $isFieldHidden = true; |
|
203 | - } |
|
204 | - } |
|
205 | - $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
206 | - $field['is_hidden'] = $isFieldHidden; |
|
207 | - } |
|
208 | - return $field; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @return array |
|
213 | - */ |
|
214 | - protected function normalizeLabelAndLegend(array $field) |
|
215 | - { |
|
216 | - if (!empty($field['label'])) { |
|
217 | - $field['legend'] = $field['label']; |
|
218 | - unset($field['label']); |
|
219 | - } else { |
|
220 | - $field['is_valid'] = false; |
|
221 | - glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
222 | - } |
|
223 | - return $field; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @return array |
|
228 | - */ |
|
229 | - protected function normalizeValue(array $field) |
|
230 | - { |
|
231 | - if (!isset($field['value'])) { |
|
232 | - $field['value'] = glsr(OptionManager::class)->get( |
|
233 | - $field['name'], |
|
234 | - $this->getFieldDefault($field) |
|
235 | - ); |
|
236 | - } |
|
237 | - return $field; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @return string |
|
242 | - */ |
|
243 | - protected function normalizeSettingPath($path) |
|
244 | - { |
|
245 | - return glsr(Helper::class)->prefixString(rtrim($path, '.'), 'settings.'); |
|
246 | - } |
|
12 | + /** |
|
13 | + * @var array |
|
14 | + */ |
|
15 | + public $settings; |
|
16 | + |
|
17 | + /** |
|
18 | + * @param string $id |
|
19 | + * @return string |
|
20 | + */ |
|
21 | + public function buildFields($id) |
|
22 | + { |
|
23 | + $this->settings = glsr(DefaultsManager::class)->settings(); |
|
24 | + $method = glsr(Helper::class)->buildMethodName($id, 'getTemplateDataFor'); |
|
25 | + $data = !method_exists($this, $method) |
|
26 | + ? $this->getTemplateData($id) |
|
27 | + : $this->$method($id); |
|
28 | + return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + protected function getFieldDefault(array $field) |
|
35 | + { |
|
36 | + return glsr_get($field, 'default'); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + protected function getFieldNameForDependsOn($path) |
|
43 | + { |
|
44 | + $fieldName = glsr(Helper::class)->convertPathToName($path, OptionManager::databaseKey()); |
|
45 | + return $this->isMultiDependency($path) |
|
46 | + ? $fieldName.'[]' |
|
47 | + : $fieldName; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + protected function getSettingFields($path) |
|
54 | + { |
|
55 | + return array_filter($this->settings, function ($key) use ($path) { |
|
56 | + return glsr(Helper::class)->startsWith($path, $key); |
|
57 | + }, ARRAY_FILTER_USE_KEY); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + protected function getSettingRows(array $fields) |
|
64 | + { |
|
65 | + $rows = ''; |
|
66 | + foreach ($fields as $name => $field) { |
|
67 | + $field = wp_parse_args($field, [ |
|
68 | + 'is_setting' => true, |
|
69 | + 'name' => $name, |
|
70 | + ]); |
|
71 | + $rows.= new Field($this->normalize($field)); |
|
72 | + } |
|
73 | + return $rows; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param string $id |
|
78 | + * @return array |
|
79 | + */ |
|
80 | + protected function getTemplateData($id) |
|
81 | + { |
|
82 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
83 | + return [ |
|
84 | + 'context' => [ |
|
85 | + 'rows' => $this->getSettingRows($fields), |
|
86 | + ], |
|
87 | + ]; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $id |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + protected function getTemplateDataForAddons($id) |
|
95 | + { |
|
96 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
97 | + $settings = glsr(Helper::class)->convertDotNotationArray($fields); |
|
98 | + $settingKeys = array_keys($settings['settings']['addons']); |
|
99 | + $results = []; |
|
100 | + foreach ($settingKeys as $key) { |
|
101 | + $addonFields = array_filter($fields, function ($path) use ($key) { |
|
102 | + return glsr(Helper::class)->startsWith('settings.addons.'.$key, $path); |
|
103 | + }, ARRAY_FILTER_USE_KEY); |
|
104 | + $results[$key] = $this->getSettingRows($addonFields); |
|
105 | + } |
|
106 | + ksort($results); |
|
107 | + return [ |
|
108 | + 'settings' => $results, |
|
109 | + ]; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param string $id |
|
114 | + * @return array |
|
115 | + */ |
|
116 | + protected function getTemplateDataForLicenses($id) |
|
117 | + { |
|
118 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
119 | + ksort($fields); |
|
120 | + return [ |
|
121 | + 'context' => [ |
|
122 | + 'rows' => $this->getSettingRows($fields), |
|
123 | + ], |
|
124 | + ]; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + protected function getTemplateDataForTranslations() |
|
131 | + { |
|
132 | + $translations = glsr(Translation::class)->renderAll(); |
|
133 | + $class = empty($translations) |
|
134 | + ? 'glsr-hidden' |
|
135 | + : ''; |
|
136 | + return [ |
|
137 | + 'context' => [ |
|
138 | + 'class' => $class, |
|
139 | + 'database_key' => OptionManager::databaseKey(), |
|
140 | + 'translations' => $translations, |
|
141 | + ], |
|
142 | + ]; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param string $path |
|
147 | + * @param string|array $expectedValue |
|
148 | + * @return bool |
|
149 | + */ |
|
150 | + protected function isFieldHidden($path, $expectedValue) |
|
151 | + { |
|
152 | + $optionValue = glsr(OptionManager::class)->get( |
|
153 | + $path, |
|
154 | + glsr(Helper::class)->dataGet(glsr()->defaults, $path) |
|
155 | + ); |
|
156 | + if (is_array($expectedValue)) { |
|
157 | + return is_array($optionValue) |
|
158 | + ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
159 | + : !in_array($optionValue, $expectedValue); |
|
160 | + } |
|
161 | + return $optionValue != $expectedValue; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + protected function isMultiDependency($path) |
|
168 | + { |
|
169 | + if (isset($this->settings[$path])) { |
|
170 | + $field = $this->settings[$path]; |
|
171 | + return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
172 | + || !empty($field['multiple']); |
|
173 | + } |
|
174 | + return false; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + protected function normalize(array $field) |
|
181 | + { |
|
182 | + $field = $this->normalizeDependsOn($field); |
|
183 | + $field = $this->normalizeLabelAndLegend($field); |
|
184 | + $field = $this->normalizeValue($field); |
|
185 | + return $field; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @return array |
|
190 | + */ |
|
191 | + protected function normalizeDependsOn(array $field) |
|
192 | + { |
|
193 | + if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
194 | + $isFieldHidden = false; |
|
195 | + $conditions = []; |
|
196 | + foreach ($field['depends_on'] as $path => $value) { |
|
197 | + $conditions[] = [ |
|
198 | + 'name' => $this->getFieldNameForDependsOn($path), |
|
199 | + 'value' => $value, |
|
200 | + ]; |
|
201 | + if ($this->isFieldHidden($path, $value)) { |
|
202 | + $isFieldHidden = true; |
|
203 | + } |
|
204 | + } |
|
205 | + $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
206 | + $field['is_hidden'] = $isFieldHidden; |
|
207 | + } |
|
208 | + return $field; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @return array |
|
213 | + */ |
|
214 | + protected function normalizeLabelAndLegend(array $field) |
|
215 | + { |
|
216 | + if (!empty($field['label'])) { |
|
217 | + $field['legend'] = $field['label']; |
|
218 | + unset($field['label']); |
|
219 | + } else { |
|
220 | + $field['is_valid'] = false; |
|
221 | + glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
222 | + } |
|
223 | + return $field; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @return array |
|
228 | + */ |
|
229 | + protected function normalizeValue(array $field) |
|
230 | + { |
|
231 | + if (!isset($field['value'])) { |
|
232 | + $field['value'] = glsr(OptionManager::class)->get( |
|
233 | + $field['name'], |
|
234 | + $this->getFieldDefault($field) |
|
235 | + ); |
|
236 | + } |
|
237 | + return $field; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @return string |
|
242 | + */ |
|
243 | + protected function normalizeSettingPath($path) |
|
244 | + { |
|
245 | + return glsr(Helper::class)->prefixString(rtrim($path, '.'), 'settings.'); |
|
246 | + } |
|
247 | 247 | } |
@@ -18,346 +18,346 @@ |
||
18 | 18 | */ |
19 | 19 | class Builder |
20 | 20 | { |
21 | - const INPUT_TYPES = [ |
|
22 | - 'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', |
|
23 | - 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', |
|
24 | - 'url', 'week', |
|
25 | - ]; |
|
26 | - |
|
27 | - const TAGS_FORM = [ |
|
28 | - 'input', 'select', 'textarea', |
|
29 | - ]; |
|
30 | - |
|
31 | - const TAGS_SINGLE = [ |
|
32 | - 'img', |
|
33 | - ]; |
|
34 | - |
|
35 | - const TAGS_STRUCTURE = [ |
|
36 | - 'div', 'form', 'nav', 'ol', 'section', 'ul', |
|
37 | - ]; |
|
38 | - |
|
39 | - const TAGS_TEXT = [ |
|
40 | - 'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', |
|
41 | - 'small', 'span', |
|
42 | - ]; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var array |
|
46 | - */ |
|
47 | - public $args = []; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var bool |
|
51 | - */ |
|
52 | - public $render = false; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - public $tag; |
|
58 | - |
|
59 | - /** |
|
60 | - * @param string $method |
|
61 | - * @param array $args |
|
62 | - * @return string|void |
|
63 | - */ |
|
64 | - public function __call($method, $args) |
|
65 | - { |
|
66 | - $instance = new static(); |
|
67 | - $instance->setTagFromMethod($method); |
|
68 | - call_user_func_array([$instance, 'normalize'], $args += ['', '']); |
|
69 | - $tags = array_merge(static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT); |
|
70 | - do_action_ref_array('site-reviews/builder', [$instance]); |
|
71 | - $generatedTag = in_array($instance->tag, $tags) |
|
72 | - ? $instance->buildTag() |
|
73 | - : $instance->buildCustomField(); |
|
74 | - $generatedTag = apply_filters('site-reviews/builder/result', $generatedTag, $instance); |
|
75 | - if (!$this->render) { |
|
76 | - return $generatedTag; |
|
77 | - } |
|
78 | - echo $generatedTag; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @param string $property |
|
83 | - * @param mixed $value |
|
84 | - * @return void |
|
85 | - */ |
|
86 | - public function __set($property, $value) |
|
87 | - { |
|
88 | - $properties = [ |
|
89 | - 'args' => 'is_array', |
|
90 | - 'render' => 'is_bool', |
|
91 | - 'tag' => 'is_string', |
|
92 | - ]; |
|
93 | - if (!isset($properties[$property]) |
|
94 | - || empty(array_filter([$value], $properties[$property])) |
|
95 | - ) { |
|
96 | - return; |
|
97 | - } |
|
98 | - $this->$property = $value; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @return void|string |
|
103 | - */ |
|
104 | - public function getClosingTag() |
|
105 | - { |
|
106 | - if (empty($this->tag)) { |
|
107 | - return; |
|
108 | - } |
|
109 | - return '</'.$this->tag.'>'; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return void|string |
|
114 | - */ |
|
115 | - public function getOpeningTag() |
|
116 | - { |
|
117 | - if (empty($this->tag)) { |
|
118 | - return; |
|
119 | - } |
|
120 | - $attributes = glsr(Attributes::class)->{$this->tag}($this->args)->toString(); |
|
121 | - return '<'.trim($this->tag.' '.$attributes).'>'; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @return void|string |
|
126 | - */ |
|
127 | - public function getTag() |
|
128 | - { |
|
129 | - if (in_array($this->tag, static::TAGS_SINGLE)) { |
|
130 | - return $this->getOpeningTag(); |
|
131 | - } |
|
132 | - if (!in_array($this->tag, static::TAGS_FORM)) { |
|
133 | - return $this->buildDefaultTag(); |
|
134 | - } |
|
135 | - return call_user_func([$this, 'buildForm'.ucfirst($this->tag)]).$this->buildFieldDescription(); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public function raw(array $field) |
|
142 | - { |
|
143 | - unset($field['label']); |
|
144 | - return $this->{$field['type']}($field); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @return string|void |
|
149 | - */ |
|
150 | - protected function buildCustomField() |
|
151 | - { |
|
152 | - $className = $this->getCustomFieldClassName(); |
|
153 | - if (class_exists($className)) { |
|
154 | - return (new $className($this))->build(); |
|
155 | - } |
|
156 | - glsr_log()->error('Field missing: '.$className); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @return string|void |
|
161 | - */ |
|
162 | - protected function buildDefaultTag($text = '') |
|
163 | - { |
|
164 | - if (empty($text)) { |
|
165 | - $text = $this->args['text']; |
|
166 | - } |
|
167 | - return $this->getOpeningTag().$text.$this->getClosingTag(); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @return string|void |
|
172 | - */ |
|
173 | - protected function buildFieldDescription() |
|
174 | - { |
|
175 | - if (empty($this->args['description'])) { |
|
176 | - return; |
|
177 | - } |
|
178 | - if ($this->args['is_widget']) { |
|
179 | - return $this->small($this->args['description']); |
|
180 | - } |
|
181 | - return $this->p($this->args['description'], ['class' => 'description']); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * @return string|void |
|
186 | - */ |
|
187 | - protected function buildFormInput() |
|
188 | - { |
|
189 | - if (!in_array($this->args['type'], ['checkbox', 'radio'])) { |
|
190 | - if (isset($this->args['multiple'])) { |
|
191 | - $this->args['name'].= '[]'; |
|
192 | - } |
|
193 | - return $this->buildFormLabel().$this->getOpeningTag(); |
|
194 | - } |
|
195 | - return empty($this->args['options']) |
|
196 | - ? $this->buildFormInputChoice() |
|
197 | - : $this->buildFormInputMultiChoice(); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * @return string|void |
|
202 | - */ |
|
203 | - protected function buildFormInputChoice() |
|
204 | - { |
|
205 | - if (!empty($this->args['text'])) { |
|
206 | - $this->args['label'] = $this->args['text']; |
|
207 | - } |
|
208 | - if (!$this->args['is_public']) { |
|
209 | - return $this->buildFormLabel([ |
|
210 | - 'class' => 'glsr-'.$this->args['type'].'-label', |
|
211 | - 'text' => $this->getOpeningTag().' '.$this->args['label'].'<span></span>', |
|
212 | - ]); |
|
213 | - } |
|
214 | - return $this->getOpeningTag().$this->buildFormLabel([ |
|
215 | - 'class' => 'glsr-'.$this->args['type'].'-label', |
|
216 | - 'text' => $this->args['label'].'<span></span>', |
|
217 | - ]); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @return string|void |
|
222 | - */ |
|
223 | - protected function buildFormInputMultiChoice() |
|
224 | - { |
|
225 | - if ('checkbox' == $this->args['type']) { |
|
226 | - $this->args['name'].= '[]'; |
|
227 | - } |
|
228 | - $index = 0; |
|
229 | - $options = array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$index) { |
|
230 | - return $carry.$this->li($this->{$this->args['type']}([ |
|
231 | - 'checked' => in_array($key, (array) $this->args['value']), |
|
232 | - 'id' => $this->args['id'].'-'.$index++, |
|
233 | - 'name' => $this->args['name'], |
|
234 | - 'text' => $this->args['options'][$key], |
|
235 | - 'value' => $key, |
|
236 | - ])); |
|
237 | - }); |
|
238 | - return $this->ul($options, [ |
|
239 | - 'class' => $this->args['class'], |
|
240 | - 'id' => $this->args['id'], |
|
241 | - ]); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return void|string |
|
246 | - */ |
|
247 | - protected function buildFormLabel(array $customArgs = []) |
|
248 | - { |
|
249 | - if (empty($this->args['label']) || 'hidden' == $this->args['type']) { |
|
250 | - return; |
|
251 | - } |
|
252 | - return $this->label(wp_parse_args($customArgs, [ |
|
253 | - 'for' => $this->args['id'], |
|
254 | - 'is_public' => $this->args['is_public'], |
|
255 | - 'text' => $this->args['label'], |
|
256 | - 'type' => $this->args['type'], |
|
257 | - ])); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * @return string|void |
|
262 | - */ |
|
263 | - protected function buildFormSelect() |
|
264 | - { |
|
265 | - return $this->buildFormLabel().$this->buildDefaultTag($this->buildFormSelectOptions()); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * @return string|void |
|
270 | - */ |
|
271 | - protected function buildFormSelectOptions() |
|
272 | - { |
|
273 | - return array_reduce(array_keys($this->args['options']), function ($carry, $key) { |
|
274 | - return $carry.$this->option([ |
|
275 | - 'selected' => $this->args['value'] == $key, |
|
276 | - 'text' => $this->args['options'][$key], |
|
277 | - 'value' => $key, |
|
278 | - ]); |
|
279 | - }); |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * @return string|void |
|
284 | - */ |
|
285 | - protected function buildFormTextarea() |
|
286 | - { |
|
287 | - return $this->buildFormLabel().$this->buildDefaultTag($this->args['value']); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * @return string|void |
|
292 | - */ |
|
293 | - protected function buildTag() |
|
294 | - { |
|
295 | - $this->mergeArgsWithRequiredDefaults(); |
|
296 | - return $this->getTag(); |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - protected function getCustomFieldClassName() |
|
303 | - { |
|
304 | - $classname = glsr(Helper::class)->buildClassName($this->tag, __NAMESPACE__.'\Fields'); |
|
305 | - return apply_filters('site-reviews/builder/field/'.$this->tag, $classname); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * @return void |
|
310 | - */ |
|
311 | - protected function mergeArgsWithRequiredDefaults() |
|
312 | - { |
|
313 | - $className = $this->getCustomFieldClassName(); |
|
314 | - if (class_exists($className)) { |
|
315 | - $this->args = $className::merge($this->args); |
|
316 | - } |
|
317 | - $this->args = glsr(BuilderDefaults::class)->merge($this->args); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * @param string|array ...$params |
|
322 | - * @return void |
|
323 | - */ |
|
324 | - protected function normalize(...$params) |
|
325 | - { |
|
326 | - if (is_string($params[0]) || is_numeric($params[0])) { |
|
327 | - $this->setNameOrTextAttributeForTag($params[0]); |
|
328 | - } |
|
329 | - if (is_array($params[0])) { |
|
330 | - $this->args += $params[0]; |
|
331 | - } elseif (is_array($params[1])) { |
|
332 | - $this->args += $params[1]; |
|
333 | - } |
|
334 | - if (!isset($this->args['is_public'])) { |
|
335 | - $this->args['is_public'] = false; |
|
336 | - } |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * @param string $value |
|
341 | - * @return void |
|
342 | - */ |
|
343 | - protected function setNameOrTextAttributeForTag($value) |
|
344 | - { |
|
345 | - $attribute = in_array($this->tag, static::TAGS_FORM) |
|
346 | - ? 'name' |
|
347 | - : 'text'; |
|
348 | - $this->args[$attribute] = $value; |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * @param string $method |
|
353 | - * @return void |
|
354 | - */ |
|
355 | - protected function setTagFromMethod($method) |
|
356 | - { |
|
357 | - $this->tag = strtolower($method); |
|
358 | - if (in_array($this->tag, static::INPUT_TYPES)) { |
|
359 | - $this->args['type'] = $this->tag; |
|
360 | - $this->tag = 'input'; |
|
361 | - } |
|
362 | - } |
|
21 | + const INPUT_TYPES = [ |
|
22 | + 'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', |
|
23 | + 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', |
|
24 | + 'url', 'week', |
|
25 | + ]; |
|
26 | + |
|
27 | + const TAGS_FORM = [ |
|
28 | + 'input', 'select', 'textarea', |
|
29 | + ]; |
|
30 | + |
|
31 | + const TAGS_SINGLE = [ |
|
32 | + 'img', |
|
33 | + ]; |
|
34 | + |
|
35 | + const TAGS_STRUCTURE = [ |
|
36 | + 'div', 'form', 'nav', 'ol', 'section', 'ul', |
|
37 | + ]; |
|
38 | + |
|
39 | + const TAGS_TEXT = [ |
|
40 | + 'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', |
|
41 | + 'small', 'span', |
|
42 | + ]; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var array |
|
46 | + */ |
|
47 | + public $args = []; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | + public $render = false; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + public $tag; |
|
58 | + |
|
59 | + /** |
|
60 | + * @param string $method |
|
61 | + * @param array $args |
|
62 | + * @return string|void |
|
63 | + */ |
|
64 | + public function __call($method, $args) |
|
65 | + { |
|
66 | + $instance = new static(); |
|
67 | + $instance->setTagFromMethod($method); |
|
68 | + call_user_func_array([$instance, 'normalize'], $args += ['', '']); |
|
69 | + $tags = array_merge(static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT); |
|
70 | + do_action_ref_array('site-reviews/builder', [$instance]); |
|
71 | + $generatedTag = in_array($instance->tag, $tags) |
|
72 | + ? $instance->buildTag() |
|
73 | + : $instance->buildCustomField(); |
|
74 | + $generatedTag = apply_filters('site-reviews/builder/result', $generatedTag, $instance); |
|
75 | + if (!$this->render) { |
|
76 | + return $generatedTag; |
|
77 | + } |
|
78 | + echo $generatedTag; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @param string $property |
|
83 | + * @param mixed $value |
|
84 | + * @return void |
|
85 | + */ |
|
86 | + public function __set($property, $value) |
|
87 | + { |
|
88 | + $properties = [ |
|
89 | + 'args' => 'is_array', |
|
90 | + 'render' => 'is_bool', |
|
91 | + 'tag' => 'is_string', |
|
92 | + ]; |
|
93 | + if (!isset($properties[$property]) |
|
94 | + || empty(array_filter([$value], $properties[$property])) |
|
95 | + ) { |
|
96 | + return; |
|
97 | + } |
|
98 | + $this->$property = $value; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @return void|string |
|
103 | + */ |
|
104 | + public function getClosingTag() |
|
105 | + { |
|
106 | + if (empty($this->tag)) { |
|
107 | + return; |
|
108 | + } |
|
109 | + return '</'.$this->tag.'>'; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return void|string |
|
114 | + */ |
|
115 | + public function getOpeningTag() |
|
116 | + { |
|
117 | + if (empty($this->tag)) { |
|
118 | + return; |
|
119 | + } |
|
120 | + $attributes = glsr(Attributes::class)->{$this->tag}($this->args)->toString(); |
|
121 | + return '<'.trim($this->tag.' '.$attributes).'>'; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @return void|string |
|
126 | + */ |
|
127 | + public function getTag() |
|
128 | + { |
|
129 | + if (in_array($this->tag, static::TAGS_SINGLE)) { |
|
130 | + return $this->getOpeningTag(); |
|
131 | + } |
|
132 | + if (!in_array($this->tag, static::TAGS_FORM)) { |
|
133 | + return $this->buildDefaultTag(); |
|
134 | + } |
|
135 | + return call_user_func([$this, 'buildForm'.ucfirst($this->tag)]).$this->buildFieldDescription(); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public function raw(array $field) |
|
142 | + { |
|
143 | + unset($field['label']); |
|
144 | + return $this->{$field['type']}($field); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @return string|void |
|
149 | + */ |
|
150 | + protected function buildCustomField() |
|
151 | + { |
|
152 | + $className = $this->getCustomFieldClassName(); |
|
153 | + if (class_exists($className)) { |
|
154 | + return (new $className($this))->build(); |
|
155 | + } |
|
156 | + glsr_log()->error('Field missing: '.$className); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @return string|void |
|
161 | + */ |
|
162 | + protected function buildDefaultTag($text = '') |
|
163 | + { |
|
164 | + if (empty($text)) { |
|
165 | + $text = $this->args['text']; |
|
166 | + } |
|
167 | + return $this->getOpeningTag().$text.$this->getClosingTag(); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @return string|void |
|
172 | + */ |
|
173 | + protected function buildFieldDescription() |
|
174 | + { |
|
175 | + if (empty($this->args['description'])) { |
|
176 | + return; |
|
177 | + } |
|
178 | + if ($this->args['is_widget']) { |
|
179 | + return $this->small($this->args['description']); |
|
180 | + } |
|
181 | + return $this->p($this->args['description'], ['class' => 'description']); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * @return string|void |
|
186 | + */ |
|
187 | + protected function buildFormInput() |
|
188 | + { |
|
189 | + if (!in_array($this->args['type'], ['checkbox', 'radio'])) { |
|
190 | + if (isset($this->args['multiple'])) { |
|
191 | + $this->args['name'].= '[]'; |
|
192 | + } |
|
193 | + return $this->buildFormLabel().$this->getOpeningTag(); |
|
194 | + } |
|
195 | + return empty($this->args['options']) |
|
196 | + ? $this->buildFormInputChoice() |
|
197 | + : $this->buildFormInputMultiChoice(); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * @return string|void |
|
202 | + */ |
|
203 | + protected function buildFormInputChoice() |
|
204 | + { |
|
205 | + if (!empty($this->args['text'])) { |
|
206 | + $this->args['label'] = $this->args['text']; |
|
207 | + } |
|
208 | + if (!$this->args['is_public']) { |
|
209 | + return $this->buildFormLabel([ |
|
210 | + 'class' => 'glsr-'.$this->args['type'].'-label', |
|
211 | + 'text' => $this->getOpeningTag().' '.$this->args['label'].'<span></span>', |
|
212 | + ]); |
|
213 | + } |
|
214 | + return $this->getOpeningTag().$this->buildFormLabel([ |
|
215 | + 'class' => 'glsr-'.$this->args['type'].'-label', |
|
216 | + 'text' => $this->args['label'].'<span></span>', |
|
217 | + ]); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @return string|void |
|
222 | + */ |
|
223 | + protected function buildFormInputMultiChoice() |
|
224 | + { |
|
225 | + if ('checkbox' == $this->args['type']) { |
|
226 | + $this->args['name'].= '[]'; |
|
227 | + } |
|
228 | + $index = 0; |
|
229 | + $options = array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$index) { |
|
230 | + return $carry.$this->li($this->{$this->args['type']}([ |
|
231 | + 'checked' => in_array($key, (array) $this->args['value']), |
|
232 | + 'id' => $this->args['id'].'-'.$index++, |
|
233 | + 'name' => $this->args['name'], |
|
234 | + 'text' => $this->args['options'][$key], |
|
235 | + 'value' => $key, |
|
236 | + ])); |
|
237 | + }); |
|
238 | + return $this->ul($options, [ |
|
239 | + 'class' => $this->args['class'], |
|
240 | + 'id' => $this->args['id'], |
|
241 | + ]); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return void|string |
|
246 | + */ |
|
247 | + protected function buildFormLabel(array $customArgs = []) |
|
248 | + { |
|
249 | + if (empty($this->args['label']) || 'hidden' == $this->args['type']) { |
|
250 | + return; |
|
251 | + } |
|
252 | + return $this->label(wp_parse_args($customArgs, [ |
|
253 | + 'for' => $this->args['id'], |
|
254 | + 'is_public' => $this->args['is_public'], |
|
255 | + 'text' => $this->args['label'], |
|
256 | + 'type' => $this->args['type'], |
|
257 | + ])); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * @return string|void |
|
262 | + */ |
|
263 | + protected function buildFormSelect() |
|
264 | + { |
|
265 | + return $this->buildFormLabel().$this->buildDefaultTag($this->buildFormSelectOptions()); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * @return string|void |
|
270 | + */ |
|
271 | + protected function buildFormSelectOptions() |
|
272 | + { |
|
273 | + return array_reduce(array_keys($this->args['options']), function ($carry, $key) { |
|
274 | + return $carry.$this->option([ |
|
275 | + 'selected' => $this->args['value'] == $key, |
|
276 | + 'text' => $this->args['options'][$key], |
|
277 | + 'value' => $key, |
|
278 | + ]); |
|
279 | + }); |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * @return string|void |
|
284 | + */ |
|
285 | + protected function buildFormTextarea() |
|
286 | + { |
|
287 | + return $this->buildFormLabel().$this->buildDefaultTag($this->args['value']); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * @return string|void |
|
292 | + */ |
|
293 | + protected function buildTag() |
|
294 | + { |
|
295 | + $this->mergeArgsWithRequiredDefaults(); |
|
296 | + return $this->getTag(); |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + protected function getCustomFieldClassName() |
|
303 | + { |
|
304 | + $classname = glsr(Helper::class)->buildClassName($this->tag, __NAMESPACE__.'\Fields'); |
|
305 | + return apply_filters('site-reviews/builder/field/'.$this->tag, $classname); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * @return void |
|
310 | + */ |
|
311 | + protected function mergeArgsWithRequiredDefaults() |
|
312 | + { |
|
313 | + $className = $this->getCustomFieldClassName(); |
|
314 | + if (class_exists($className)) { |
|
315 | + $this->args = $className::merge($this->args); |
|
316 | + } |
|
317 | + $this->args = glsr(BuilderDefaults::class)->merge($this->args); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * @param string|array ...$params |
|
322 | + * @return void |
|
323 | + */ |
|
324 | + protected function normalize(...$params) |
|
325 | + { |
|
326 | + if (is_string($params[0]) || is_numeric($params[0])) { |
|
327 | + $this->setNameOrTextAttributeForTag($params[0]); |
|
328 | + } |
|
329 | + if (is_array($params[0])) { |
|
330 | + $this->args += $params[0]; |
|
331 | + } elseif (is_array($params[1])) { |
|
332 | + $this->args += $params[1]; |
|
333 | + } |
|
334 | + if (!isset($this->args['is_public'])) { |
|
335 | + $this->args['is_public'] = false; |
|
336 | + } |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * @param string $value |
|
341 | + * @return void |
|
342 | + */ |
|
343 | + protected function setNameOrTextAttributeForTag($value) |
|
344 | + { |
|
345 | + $attribute = in_array($this->tag, static::TAGS_FORM) |
|
346 | + ? 'name' |
|
347 | + : 'text'; |
|
348 | + $this->args[$attribute] = $value; |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * @param string $method |
|
353 | + * @return void |
|
354 | + */ |
|
355 | + protected function setTagFromMethod($method) |
|
356 | + { |
|
357 | + $this->tag = strtolower($method); |
|
358 | + if (in_array($this->tag, static::INPUT_TYPES)) { |
|
359 | + $this->args['type'] = $this->tag; |
|
360 | + $this->tag = 'input'; |
|
361 | + } |
|
362 | + } |
|
363 | 363 | } |
@@ -13,297 +13,297 @@ |
||
13 | 13 | |
14 | 14 | class SiteReviewsForm |
15 | 15 | { |
16 | - /** |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - protected $args; |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + protected $args; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $errors; |
|
21 | + /** |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $errors; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $message; |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $message; |
|
30 | 30 | |
31 | - /** |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - protected $required; |
|
31 | + /** |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + protected $required; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - protected $values; |
|
36 | + /** |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + protected $values; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @return void|string |
|
43 | - */ |
|
44 | - public function build(array $args = []) |
|
45 | - { |
|
46 | - $this->args = $args; |
|
47 | - if (!is_user_logged_in() && glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
48 | - return $this->buildLoginRegister(); |
|
49 | - } |
|
50 | - $this->errors = glsr(Session::class)->get($args['id'].'errors', [], true); |
|
51 | - $this->message = glsr(Session::class)->get($args['id'].'message', '', true); |
|
52 | - $this->required = glsr(OptionManager::class)->get('settings.submissions.required', []); |
|
53 | - $this->values = glsr(Session::class)->get($args['id'].'values', [], true); |
|
54 | - $fields = array_reduce($this->getFields(), function ($carry, $field) { |
|
55 | - return $carry.$field; |
|
56 | - }); |
|
57 | - return glsr(Template::class)->build('templates/reviews-form', [ |
|
58 | - 'args' => $args, |
|
59 | - 'context' => [ |
|
60 | - 'class' => $this->getClass(), |
|
61 | - 'fields' => $fields, |
|
62 | - 'id' => $this->args['id'], |
|
63 | - 'response' => $this->buildResponse(), |
|
64 | - 'submit_button' => $this->buildSubmitButton().$this->buildRecaptcha(), |
|
65 | - ], |
|
66 | - ]); |
|
67 | - } |
|
41 | + /** |
|
42 | + * @return void|string |
|
43 | + */ |
|
44 | + public function build(array $args = []) |
|
45 | + { |
|
46 | + $this->args = $args; |
|
47 | + if (!is_user_logged_in() && glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
48 | + return $this->buildLoginRegister(); |
|
49 | + } |
|
50 | + $this->errors = glsr(Session::class)->get($args['id'].'errors', [], true); |
|
51 | + $this->message = glsr(Session::class)->get($args['id'].'message', '', true); |
|
52 | + $this->required = glsr(OptionManager::class)->get('settings.submissions.required', []); |
|
53 | + $this->values = glsr(Session::class)->get($args['id'].'values', [], true); |
|
54 | + $fields = array_reduce($this->getFields(), function ($carry, $field) { |
|
55 | + return $carry.$field; |
|
56 | + }); |
|
57 | + return glsr(Template::class)->build('templates/reviews-form', [ |
|
58 | + 'args' => $args, |
|
59 | + 'context' => [ |
|
60 | + 'class' => $this->getClass(), |
|
61 | + 'fields' => $fields, |
|
62 | + 'id' => $this->args['id'], |
|
63 | + 'response' => $this->buildResponse(), |
|
64 | + 'submit_button' => $this->buildSubmitButton().$this->buildRecaptcha(), |
|
65 | + ], |
|
66 | + ]); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - protected function buildLoginRegister() |
|
73 | - { |
|
74 | - return glsr(Template::class)->build('templates/login-register', [ |
|
75 | - 'context' => [ |
|
76 | - 'text' => trim($this->getLoginText().' '.$this->getRegisterText()), |
|
77 | - ], |
|
78 | - ]); |
|
79 | - } |
|
69 | + /** |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + protected function buildLoginRegister() |
|
73 | + { |
|
74 | + return glsr(Template::class)->build('templates/login-register', [ |
|
75 | + 'context' => [ |
|
76 | + 'text' => trim($this->getLoginText().' '.$this->getRegisterText()), |
|
77 | + ], |
|
78 | + ]); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @return void|string |
|
83 | - */ |
|
84 | - protected function buildRecaptcha() |
|
85 | - { |
|
86 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
87 | - return; |
|
88 | - } |
|
89 | - return glsr(Builder::class)->div([ |
|
90 | - 'class' => 'glsr-recaptcha-holder', |
|
91 | - 'data-badge' => glsr(OptionManager::class)->get('settings.submissions.recaptcha.position'), |
|
92 | - 'data-sitekey' => sanitize_text_field(glsr(OptionManager::class)->get('settings.submissions.recaptcha.key')), |
|
93 | - 'data-size' => 'invisible', |
|
94 | - ]); |
|
95 | - } |
|
81 | + /** |
|
82 | + * @return void|string |
|
83 | + */ |
|
84 | + protected function buildRecaptcha() |
|
85 | + { |
|
86 | + if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
87 | + return; |
|
88 | + } |
|
89 | + return glsr(Builder::class)->div([ |
|
90 | + 'class' => 'glsr-recaptcha-holder', |
|
91 | + 'data-badge' => glsr(OptionManager::class)->get('settings.submissions.recaptcha.position'), |
|
92 | + 'data-sitekey' => sanitize_text_field(glsr(OptionManager::class)->get('settings.submissions.recaptcha.key')), |
|
93 | + 'data-size' => 'invisible', |
|
94 | + ]); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - protected function buildResponse() |
|
101 | - { |
|
102 | - $classes = !empty($this->errors) |
|
103 | - ? glsr(StyleValidationDefaults::class)->defaults()['message_error_class'] |
|
104 | - : ''; |
|
105 | - return glsr(Template::class)->build('templates/form/response', [ |
|
106 | - 'context' => [ |
|
107 | - 'class' => $classes, |
|
108 | - 'message' => wpautop($this->message), |
|
109 | - ], |
|
110 | - 'has_errors' => !empty($this->errors), |
|
111 | - ]); |
|
112 | - } |
|
97 | + /** |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + protected function buildResponse() |
|
101 | + { |
|
102 | + $classes = !empty($this->errors) |
|
103 | + ? glsr(StyleValidationDefaults::class)->defaults()['message_error_class'] |
|
104 | + : ''; |
|
105 | + return glsr(Template::class)->build('templates/form/response', [ |
|
106 | + 'context' => [ |
|
107 | + 'class' => $classes, |
|
108 | + 'message' => wpautop($this->message), |
|
109 | + ], |
|
110 | + 'has_errors' => !empty($this->errors), |
|
111 | + ]); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - protected function buildSubmitButton() |
|
118 | - { |
|
119 | - return glsr(Template::class)->build('templates/form/submit-button', [ |
|
120 | - 'context' => [ |
|
121 | - 'text' => __('Submit your review', 'site-reviews'), |
|
122 | - ], |
|
123 | - ]); |
|
124 | - } |
|
114 | + /** |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + protected function buildSubmitButton() |
|
118 | + { |
|
119 | + return glsr(Template::class)->build('templates/form/submit-button', [ |
|
120 | + 'context' => [ |
|
121 | + 'text' => __('Submit your review', 'site-reviews'), |
|
122 | + ], |
|
123 | + ]); |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * @return string |
|
128 | - */ |
|
129 | - protected function getClass() |
|
130 | - { |
|
131 | - return trim('glsr-form glsr-'.glsr(Style::class)->get().' '.$this->args['class']); |
|
132 | - } |
|
126 | + /** |
|
127 | + * @return string |
|
128 | + */ |
|
129 | + protected function getClass() |
|
130 | + { |
|
131 | + return trim('glsr-form glsr-'.glsr(Style::class)->get().' '.$this->args['class']); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @return array |
|
136 | - */ |
|
137 | - protected function getFields() |
|
138 | - { |
|
139 | - $hiddenFields = $this->getHiddenFields(); |
|
140 | - $hiddenFields[] = $this->getHoneypotField(); |
|
141 | - $fields = $this->normalizeFields(glsr(Form::class)->getFields('submission-form')); |
|
142 | - $paths = array_map(function ($obj) { |
|
143 | - return $obj->field['path']; |
|
144 | - }, $hiddenFields); |
|
145 | - foreach ($fields as $field) { |
|
146 | - $index = array_search($field->field['path'], $paths); |
|
147 | - if (false === $index) { |
|
148 | - continue; |
|
149 | - } |
|
150 | - unset($hiddenFields[$index]); |
|
151 | - } |
|
152 | - return array_merge($hiddenFields, $fields); |
|
153 | - } |
|
134 | + /** |
|
135 | + * @return array |
|
136 | + */ |
|
137 | + protected function getFields() |
|
138 | + { |
|
139 | + $hiddenFields = $this->getHiddenFields(); |
|
140 | + $hiddenFields[] = $this->getHoneypotField(); |
|
141 | + $fields = $this->normalizeFields(glsr(Form::class)->getFields('submission-form')); |
|
142 | + $paths = array_map(function ($obj) { |
|
143 | + return $obj->field['path']; |
|
144 | + }, $hiddenFields); |
|
145 | + foreach ($fields as $field) { |
|
146 | + $index = array_search($field->field['path'], $paths); |
|
147 | + if (false === $index) { |
|
148 | + continue; |
|
149 | + } |
|
150 | + unset($hiddenFields[$index]); |
|
151 | + } |
|
152 | + return array_merge($hiddenFields, $fields); |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * @return string |
|
157 | - */ |
|
158 | - protected function getLoginText() |
|
159 | - { |
|
160 | - $loginLink = glsr(Builder::class)->a([ |
|
161 | - 'href' => wp_login_url(strval(get_permalink())), |
|
162 | - 'text' => __('logged in', 'site-reviews'), |
|
163 | - ]); |
|
164 | - return sprintf(__('You must be %s to submit a review.', 'site-reviews'), $loginLink); |
|
165 | - } |
|
155 | + /** |
|
156 | + * @return string |
|
157 | + */ |
|
158 | + protected function getLoginText() |
|
159 | + { |
|
160 | + $loginLink = glsr(Builder::class)->a([ |
|
161 | + 'href' => wp_login_url(strval(get_permalink())), |
|
162 | + 'text' => __('logged in', 'site-reviews'), |
|
163 | + ]); |
|
164 | + return sprintf(__('You must be %s to submit a review.', 'site-reviews'), $loginLink); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @return void|string |
|
169 | - */ |
|
170 | - protected function getRegisterText() |
|
171 | - { |
|
172 | - if (!get_option('users_can_register') || !glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
173 | - return; |
|
174 | - } |
|
175 | - $registerLink = glsr(Builder::class)->a([ |
|
176 | - 'href' => wp_registration_url(), |
|
177 | - 'text' => __('register', 'site-reviews'), |
|
178 | - ]); |
|
179 | - return sprintf(__('You may also %s for an account.', 'site-reviews'), $registerLink); |
|
180 | - } |
|
167 | + /** |
|
168 | + * @return void|string |
|
169 | + */ |
|
170 | + protected function getRegisterText() |
|
171 | + { |
|
172 | + if (!get_option('users_can_register') || !glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
173 | + return; |
|
174 | + } |
|
175 | + $registerLink = glsr(Builder::class)->a([ |
|
176 | + 'href' => wp_registration_url(), |
|
177 | + 'text' => __('register', 'site-reviews'), |
|
178 | + ]); |
|
179 | + return sprintf(__('You may also %s for an account.', 'site-reviews'), $registerLink); |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * @return array |
|
184 | - */ |
|
185 | - protected function getHiddenFields() |
|
186 | - { |
|
187 | - $fields = [[ |
|
188 | - 'name' => '_action', |
|
189 | - 'value' => 'submit-review', |
|
190 | - ], [ |
|
191 | - 'name' => '_counter', |
|
192 | - ], [ |
|
193 | - 'name' => '_nonce', |
|
194 | - 'value' => wp_create_nonce('submit-review'), |
|
195 | - ], [ |
|
196 | - 'name' => '_post_id', |
|
197 | - 'value' => get_the_ID(), |
|
198 | - ], [ |
|
199 | - 'name' => '_referer', |
|
200 | - 'value' => wp_unslash(filter_input(INPUT_SERVER, 'REQUEST_URI')), |
|
201 | - ], [ |
|
202 | - 'name' => 'assign_to', |
|
203 | - 'value' => $this->args['assign_to'], |
|
204 | - ], [ |
|
205 | - 'name' => 'category', |
|
206 | - 'value' => $this->args['category'], |
|
207 | - ], [ |
|
208 | - 'name' => 'excluded', |
|
209 | - 'value' => $this->args['hide'], |
|
210 | - ], [ |
|
211 | - 'name' => 'form_id', |
|
212 | - 'value' => $this->args['id'], |
|
213 | - ]]; |
|
214 | - return array_map(function ($field) { |
|
215 | - return new Field(wp_parse_args($field, ['type' => 'hidden'])); |
|
216 | - }, $fields); |
|
217 | - } |
|
182 | + /** |
|
183 | + * @return array |
|
184 | + */ |
|
185 | + protected function getHiddenFields() |
|
186 | + { |
|
187 | + $fields = [[ |
|
188 | + 'name' => '_action', |
|
189 | + 'value' => 'submit-review', |
|
190 | + ], [ |
|
191 | + 'name' => '_counter', |
|
192 | + ], [ |
|
193 | + 'name' => '_nonce', |
|
194 | + 'value' => wp_create_nonce('submit-review'), |
|
195 | + ], [ |
|
196 | + 'name' => '_post_id', |
|
197 | + 'value' => get_the_ID(), |
|
198 | + ], [ |
|
199 | + 'name' => '_referer', |
|
200 | + 'value' => wp_unslash(filter_input(INPUT_SERVER, 'REQUEST_URI')), |
|
201 | + ], [ |
|
202 | + 'name' => 'assign_to', |
|
203 | + 'value' => $this->args['assign_to'], |
|
204 | + ], [ |
|
205 | + 'name' => 'category', |
|
206 | + 'value' => $this->args['category'], |
|
207 | + ], [ |
|
208 | + 'name' => 'excluded', |
|
209 | + 'value' => $this->args['hide'], |
|
210 | + ], [ |
|
211 | + 'name' => 'form_id', |
|
212 | + 'value' => $this->args['id'], |
|
213 | + ]]; |
|
214 | + return array_map(function ($field) { |
|
215 | + return new Field(wp_parse_args($field, ['type' => 'hidden'])); |
|
216 | + }, $fields); |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * @return Field |
|
221 | - */ |
|
222 | - protected function getHoneypotField() |
|
223 | - { |
|
224 | - return new Field([ |
|
225 | - 'name' => 'gotcha', |
|
226 | - 'type' => 'honeypot', |
|
227 | - ]); |
|
228 | - } |
|
219 | + /** |
|
220 | + * @return Field |
|
221 | + */ |
|
222 | + protected function getHoneypotField() |
|
223 | + { |
|
224 | + return new Field([ |
|
225 | + 'name' => 'gotcha', |
|
226 | + 'type' => 'honeypot', |
|
227 | + ]); |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * @return void |
|
232 | - */ |
|
233 | - protected function normalizeFieldId(Field &$field) |
|
234 | - { |
|
235 | - if (empty($this->args['id']) || empty($field->field['id'])) { |
|
236 | - return; |
|
237 | - } |
|
238 | - $field->field['id'].= '-'.$this->args['id']; |
|
239 | - } |
|
230 | + /** |
|
231 | + * @return void |
|
232 | + */ |
|
233 | + protected function normalizeFieldId(Field &$field) |
|
234 | + { |
|
235 | + if (empty($this->args['id']) || empty($field->field['id'])) { |
|
236 | + return; |
|
237 | + } |
|
238 | + $field->field['id'].= '-'.$this->args['id']; |
|
239 | + } |
|
240 | 240 | |
241 | - /** |
|
242 | - * @return void |
|
243 | - */ |
|
244 | - protected function normalizeFieldClass(Field &$field) |
|
245 | - { |
|
246 | - if (!isset($field->field['class'])) { |
|
247 | - $field->field['class'] = ''; |
|
248 | - } |
|
249 | - $field->field['class'] = trim($field->field['class'].' glsr-field-control'); |
|
250 | - } |
|
241 | + /** |
|
242 | + * @return void |
|
243 | + */ |
|
244 | + protected function normalizeFieldClass(Field &$field) |
|
245 | + { |
|
246 | + if (!isset($field->field['class'])) { |
|
247 | + $field->field['class'] = ''; |
|
248 | + } |
|
249 | + $field->field['class'] = trim($field->field['class'].' glsr-field-control'); |
|
250 | + } |
|
251 | 251 | |
252 | - /** |
|
253 | - * @return void |
|
254 | - */ |
|
255 | - protected function normalizeFieldErrors(Field &$field) |
|
256 | - { |
|
257 | - if (!array_key_exists($field->field['path'], $this->errors)) { |
|
258 | - return; |
|
259 | - } |
|
260 | - $field->field['errors'] = $this->errors[$field->field['path']]; |
|
261 | - } |
|
252 | + /** |
|
253 | + * @return void |
|
254 | + */ |
|
255 | + protected function normalizeFieldErrors(Field &$field) |
|
256 | + { |
|
257 | + if (!array_key_exists($field->field['path'], $this->errors)) { |
|
258 | + return; |
|
259 | + } |
|
260 | + $field->field['errors'] = $this->errors[$field->field['path']]; |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * @return void |
|
265 | - */ |
|
266 | - protected function normalizeFieldRequired(Field &$field) |
|
267 | - { |
|
268 | - if (!in_array($field->field['path'], $this->required)) { |
|
269 | - return; |
|
270 | - } |
|
271 | - $field->field['required'] = true; |
|
272 | - } |
|
263 | + /** |
|
264 | + * @return void |
|
265 | + */ |
|
266 | + protected function normalizeFieldRequired(Field &$field) |
|
267 | + { |
|
268 | + if (!in_array($field->field['path'], $this->required)) { |
|
269 | + return; |
|
270 | + } |
|
271 | + $field->field['required'] = true; |
|
272 | + } |
|
273 | 273 | |
274 | - /** |
|
275 | - * @return array |
|
276 | - */ |
|
277 | - protected function normalizeFields($fields) |
|
278 | - { |
|
279 | - $normalizedFields = []; |
|
280 | - foreach ($fields as $field) { |
|
281 | - if (in_array($field->field['path'], $this->args['hide'])) { |
|
282 | - continue; |
|
283 | - } |
|
284 | - $field->field['is_public'] = true; |
|
285 | - $this->normalizeFieldClass($field); |
|
286 | - $this->normalizeFieldErrors($field); |
|
287 | - $this->normalizeFieldRequired($field); |
|
288 | - $this->normalizeFieldValue($field); |
|
289 | - $this->normalizeFieldId($field); |
|
290 | - $normalizedFields[] = $field; |
|
291 | - } |
|
292 | - return $normalizedFields; |
|
293 | - } |
|
274 | + /** |
|
275 | + * @return array |
|
276 | + */ |
|
277 | + protected function normalizeFields($fields) |
|
278 | + { |
|
279 | + $normalizedFields = []; |
|
280 | + foreach ($fields as $field) { |
|
281 | + if (in_array($field->field['path'], $this->args['hide'])) { |
|
282 | + continue; |
|
283 | + } |
|
284 | + $field->field['is_public'] = true; |
|
285 | + $this->normalizeFieldClass($field); |
|
286 | + $this->normalizeFieldErrors($field); |
|
287 | + $this->normalizeFieldRequired($field); |
|
288 | + $this->normalizeFieldValue($field); |
|
289 | + $this->normalizeFieldId($field); |
|
290 | + $normalizedFields[] = $field; |
|
291 | + } |
|
292 | + return $normalizedFields; |
|
293 | + } |
|
294 | 294 | |
295 | - /** |
|
296 | - * @return void |
|
297 | - */ |
|
298 | - protected function normalizeFieldValue(Field &$field) |
|
299 | - { |
|
300 | - if (!array_key_exists($field->field['path'], $this->values)) { |
|
301 | - return; |
|
302 | - } |
|
303 | - if (in_array($field->field['type'], ['radio', 'checkbox'])) { |
|
304 | - $field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']]; |
|
305 | - } else { |
|
306 | - $field->field['value'] = $this->values[$field->field['path']]; |
|
307 | - } |
|
308 | - } |
|
295 | + /** |
|
296 | + * @return void |
|
297 | + */ |
|
298 | + protected function normalizeFieldValue(Field &$field) |
|
299 | + { |
|
300 | + if (!array_key_exists($field->field['path'], $this->values)) { |
|
301 | + return; |
|
302 | + } |
|
303 | + if (in_array($field->field['type'], ['radio', 'checkbox'])) { |
|
304 | + $field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']]; |
|
305 | + } else { |
|
306 | + $field->field['value'] = $this->values[$field->field['path']]; |
|
307 | + } |
|
308 | + } |
|
309 | 309 | } |
@@ -19,376 +19,376 @@ |
||
19 | 19 | |
20 | 20 | class SiteReviews |
21 | 21 | { |
22 | - /** |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - public $args; |
|
22 | + /** |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + public $args; |
|
26 | 26 | |
27 | - /** |
|
28 | - * @var Review |
|
29 | - */ |
|
30 | - public $current; |
|
27 | + /** |
|
28 | + * @var Review |
|
29 | + */ |
|
30 | + public $current; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - public $options; |
|
32 | + /** |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + public $options; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var Reviews |
|
39 | - */ |
|
40 | - protected $reviews; |
|
37 | + /** |
|
38 | + * @var Reviews |
|
39 | + */ |
|
40 | + protected $reviews; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param Reviews|null $reviews |
|
44 | - * @return ReviewsHtml |
|
45 | - */ |
|
46 | - public function build(array $args = [], $reviews = null) |
|
47 | - { |
|
48 | - $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
49 | - $this->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
50 | - $this->reviews = $reviews instanceof Reviews |
|
51 | - ? $reviews |
|
52 | - : glsr(ReviewManager::class)->get($args); |
|
53 | - $this->generateSchema(); |
|
54 | - return $this->buildReviews(); |
|
55 | - } |
|
42 | + /** |
|
43 | + * @param Reviews|null $reviews |
|
44 | + * @return ReviewsHtml |
|
45 | + */ |
|
46 | + public function build(array $args = [], $reviews = null) |
|
47 | + { |
|
48 | + $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
49 | + $this->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
50 | + $this->reviews = $reviews instanceof Reviews |
|
51 | + ? $reviews |
|
52 | + : glsr(ReviewManager::class)->get($args); |
|
53 | + $this->generateSchema(); |
|
54 | + return $this->buildReviews(); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return ReviewHtml |
|
59 | - */ |
|
60 | - public function buildReview(Review $review) |
|
61 | - { |
|
62 | - $review = apply_filters('site-reviews/review/build/before', $review); |
|
63 | - $this->current = $review; |
|
64 | - $renderedFields = []; |
|
65 | - foreach ($review as $key => $value) { |
|
66 | - $method = glsr(Helper::class)->buildMethodName($key, 'buildOption'); |
|
67 | - $field = method_exists($this, $method) |
|
68 | - ? $this->$method($key, $value) |
|
69 | - : apply_filters('site-reviews/review/build/'.$key, false, $value, $this, $review); |
|
70 | - if (false === $field) { |
|
71 | - continue; |
|
72 | - } |
|
73 | - $renderedFields[$key] = $field; |
|
74 | - } |
|
75 | - $this->wrap($renderedFields, $review); |
|
76 | - $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review); |
|
77 | - $this->current = null; |
|
78 | - return new ReviewHtml($review, (array) $renderedFields); |
|
79 | - } |
|
57 | + /** |
|
58 | + * @return ReviewHtml |
|
59 | + */ |
|
60 | + public function buildReview(Review $review) |
|
61 | + { |
|
62 | + $review = apply_filters('site-reviews/review/build/before', $review); |
|
63 | + $this->current = $review; |
|
64 | + $renderedFields = []; |
|
65 | + foreach ($review as $key => $value) { |
|
66 | + $method = glsr(Helper::class)->buildMethodName($key, 'buildOption'); |
|
67 | + $field = method_exists($this, $method) |
|
68 | + ? $this->$method($key, $value) |
|
69 | + : apply_filters('site-reviews/review/build/'.$key, false, $value, $this, $review); |
|
70 | + if (false === $field) { |
|
71 | + continue; |
|
72 | + } |
|
73 | + $renderedFields[$key] = $field; |
|
74 | + } |
|
75 | + $this->wrap($renderedFields, $review); |
|
76 | + $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review); |
|
77 | + $this->current = null; |
|
78 | + return new ReviewHtml($review, (array) $renderedFields); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @return ReviewsHtml |
|
83 | - */ |
|
84 | - public function buildReviews() |
|
85 | - { |
|
86 | - $renderedReviews = []; |
|
87 | - foreach ($this->reviews as $index => $review) { |
|
88 | - $renderedReviews[] = $this->buildReview($review); |
|
89 | - } |
|
90 | - return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
91 | - } |
|
81 | + /** |
|
82 | + * @return ReviewsHtml |
|
83 | + */ |
|
84 | + public function buildReviews() |
|
85 | + { |
|
86 | + $renderedReviews = []; |
|
87 | + foreach ($this->reviews as $index => $review) { |
|
88 | + $renderedReviews[] = $this->buildReview($review); |
|
89 | + } |
|
90 | + return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * @return void |
|
95 | - */ |
|
96 | - public function generateSchema() |
|
97 | - { |
|
98 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
99 | - return; |
|
100 | - } |
|
101 | - glsr(Schema::class)->store( |
|
102 | - glsr(Schema::class)->build($this->args) |
|
103 | - ); |
|
104 | - } |
|
93 | + /** |
|
94 | + * @return void |
|
95 | + */ |
|
96 | + public function generateSchema() |
|
97 | + { |
|
98 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
99 | + return; |
|
100 | + } |
|
101 | + glsr(Schema::class)->store( |
|
102 | + glsr(Schema::class)->build($this->args) |
|
103 | + ); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @param string $key |
|
108 | - * @param string $path |
|
109 | - * @return bool |
|
110 | - */ |
|
111 | - public function isHidden($key, $path = '') |
|
112 | - { |
|
113 | - $isOptionEnabled = !empty($path) |
|
114 | - ? $this->isOptionEnabled($path) |
|
115 | - : true; |
|
116 | - return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
117 | - } |
|
106 | + /** |
|
107 | + * @param string $key |
|
108 | + * @param string $path |
|
109 | + * @return bool |
|
110 | + */ |
|
111 | + public function isHidden($key, $path = '') |
|
112 | + { |
|
113 | + $isOptionEnabled = !empty($path) |
|
114 | + ? $this->isOptionEnabled($path) |
|
115 | + : true; |
|
116 | + return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * @param string $key |
|
121 | - * @param string $value |
|
122 | - * @return void|string |
|
123 | - */ |
|
124 | - protected function buildOptionAssignedTo($key, $value) |
|
125 | - { |
|
126 | - if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
127 | - return; |
|
128 | - } |
|
129 | - $post = glsr(Polylang::class)->getPost($value); |
|
130 | - if (!($post instanceof WP_Post)) { |
|
131 | - return; |
|
132 | - } |
|
133 | - $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
134 | - 'href' => get_the_permalink($post->ID), |
|
135 | - ]); |
|
136 | - $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
137 | - return '<span>'.$assignedTo.'</span>'; |
|
138 | - } |
|
119 | + /** |
|
120 | + * @param string $key |
|
121 | + * @param string $value |
|
122 | + * @return void|string |
|
123 | + */ |
|
124 | + protected function buildOptionAssignedTo($key, $value) |
|
125 | + { |
|
126 | + if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
127 | + return; |
|
128 | + } |
|
129 | + $post = glsr(Polylang::class)->getPost($value); |
|
130 | + if (!($post instanceof WP_Post)) { |
|
131 | + return; |
|
132 | + } |
|
133 | + $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
134 | + 'href' => get_the_permalink($post->ID), |
|
135 | + ]); |
|
136 | + $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
137 | + return '<span>'.$assignedTo.'</span>'; |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * @param string $key |
|
142 | - * @param string $value |
|
143 | - * @return void|string |
|
144 | - */ |
|
145 | - protected function buildOptionAuthor($key, $value) |
|
146 | - { |
|
147 | - if (!$this->isHidden($key)) { |
|
148 | - return '<span>'.$value.'</span>'; |
|
149 | - } |
|
150 | - } |
|
140 | + /** |
|
141 | + * @param string $key |
|
142 | + * @param string $value |
|
143 | + * @return void|string |
|
144 | + */ |
|
145 | + protected function buildOptionAuthor($key, $value) |
|
146 | + { |
|
147 | + if (!$this->isHidden($key)) { |
|
148 | + return '<span>'.$value.'</span>'; |
|
149 | + } |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @param string $key |
|
154 | - * @param string $value |
|
155 | - * @return void|string |
|
156 | - */ |
|
157 | - protected function buildOptionAvatar($key, $value) |
|
158 | - { |
|
159 | - if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
160 | - return; |
|
161 | - } |
|
162 | - $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
163 | - return glsr(Builder::class)->img([ |
|
164 | - 'height' => $size, |
|
165 | - 'src' => $this->generateAvatar($value), |
|
166 | - 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
167 | - 'width' => $size, |
|
168 | - ]); |
|
169 | - } |
|
152 | + /** |
|
153 | + * @param string $key |
|
154 | + * @param string $value |
|
155 | + * @return void|string |
|
156 | + */ |
|
157 | + protected function buildOptionAvatar($key, $value) |
|
158 | + { |
|
159 | + if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
160 | + return; |
|
161 | + } |
|
162 | + $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
163 | + return glsr(Builder::class)->img([ |
|
164 | + 'height' => $size, |
|
165 | + 'src' => $this->generateAvatar($value), |
|
166 | + 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
167 | + 'width' => $size, |
|
168 | + ]); |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * @param string $key |
|
173 | - * @param string $value |
|
174 | - * @return void|string |
|
175 | - */ |
|
176 | - protected function buildOptionContent($key, $value) |
|
177 | - { |
|
178 | - $text = $this->normalizeText($value); |
|
179 | - if (!$this->isHiddenOrEmpty($key, $text)) { |
|
180 | - return '<p>'.$text.'</p>'; |
|
181 | - } |
|
182 | - } |
|
171 | + /** |
|
172 | + * @param string $key |
|
173 | + * @param string $value |
|
174 | + * @return void|string |
|
175 | + */ |
|
176 | + protected function buildOptionContent($key, $value) |
|
177 | + { |
|
178 | + $text = $this->normalizeText($value); |
|
179 | + if (!$this->isHiddenOrEmpty($key, $text)) { |
|
180 | + return '<p>'.$text.'</p>'; |
|
181 | + } |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * @param string $key |
|
186 | - * @param string $value |
|
187 | - * @return void|string |
|
188 | - */ |
|
189 | - protected function buildOptionDate($key, $value) |
|
190 | - { |
|
191 | - if ($this->isHidden($key)) { |
|
192 | - return; |
|
193 | - } |
|
194 | - $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
195 | - if ('relative' == $dateFormat) { |
|
196 | - $date = glsr(Date::class)->relative($value); |
|
197 | - } else { |
|
198 | - $format = 'custom' == $dateFormat |
|
199 | - ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
200 | - : (string) get_option('date_format'); |
|
201 | - $date = date_i18n($format, strtotime($value)); |
|
202 | - } |
|
203 | - return '<span>'.$date.'</span>'; |
|
204 | - } |
|
184 | + /** |
|
185 | + * @param string $key |
|
186 | + * @param string $value |
|
187 | + * @return void|string |
|
188 | + */ |
|
189 | + protected function buildOptionDate($key, $value) |
|
190 | + { |
|
191 | + if ($this->isHidden($key)) { |
|
192 | + return; |
|
193 | + } |
|
194 | + $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
195 | + if ('relative' == $dateFormat) { |
|
196 | + $date = glsr(Date::class)->relative($value); |
|
197 | + } else { |
|
198 | + $format = 'custom' == $dateFormat |
|
199 | + ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
200 | + : (string) get_option('date_format'); |
|
201 | + $date = date_i18n($format, strtotime($value)); |
|
202 | + } |
|
203 | + return '<span>'.$date.'</span>'; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * @param string $key |
|
208 | - * @param string $value |
|
209 | - * @return void|string |
|
210 | - */ |
|
211 | - protected function buildOptionRating($key, $value) |
|
212 | - { |
|
213 | - if (!$this->isHiddenOrEmpty($key, $value)) { |
|
214 | - return glsr_star_rating($value); |
|
215 | - } |
|
216 | - } |
|
206 | + /** |
|
207 | + * @param string $key |
|
208 | + * @param string $value |
|
209 | + * @return void|string |
|
210 | + */ |
|
211 | + protected function buildOptionRating($key, $value) |
|
212 | + { |
|
213 | + if (!$this->isHiddenOrEmpty($key, $value)) { |
|
214 | + return glsr_star_rating($value); |
|
215 | + } |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * @param string $key |
|
220 | - * @param string $value |
|
221 | - * @return void|string |
|
222 | - */ |
|
223 | - protected function buildOptionResponse($key, $value) |
|
224 | - { |
|
225 | - if ($this->isHiddenOrEmpty($key, $value)) { |
|
226 | - return; |
|
227 | - } |
|
228 | - $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
229 | - $text = $this->normalizeText($value); |
|
230 | - $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
231 | - $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
232 | - $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
233 | - return $response.$background; |
|
234 | - } |
|
218 | + /** |
|
219 | + * @param string $key |
|
220 | + * @param string $value |
|
221 | + * @return void|string |
|
222 | + */ |
|
223 | + protected function buildOptionResponse($key, $value) |
|
224 | + { |
|
225 | + if ($this->isHiddenOrEmpty($key, $value)) { |
|
226 | + return; |
|
227 | + } |
|
228 | + $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
229 | + $text = $this->normalizeText($value); |
|
230 | + $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
231 | + $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
232 | + $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
233 | + return $response.$background; |
|
234 | + } |
|
235 | 235 | |
236 | - /** |
|
237 | - * @param string $key |
|
238 | - * @param string $value |
|
239 | - * @return void|string |
|
240 | - */ |
|
241 | - protected function buildOptionTitle($key, $value) |
|
242 | - { |
|
243 | - if ($this->isHidden($key)) { |
|
244 | - return; |
|
245 | - } |
|
246 | - if (empty($value)) { |
|
247 | - $value = __('No Title', 'site-reviews'); |
|
248 | - } |
|
249 | - return '<h3>'.$value.'</h3>'; |
|
250 | - } |
|
236 | + /** |
|
237 | + * @param string $key |
|
238 | + * @param string $value |
|
239 | + * @return void|string |
|
240 | + */ |
|
241 | + protected function buildOptionTitle($key, $value) |
|
242 | + { |
|
243 | + if ($this->isHidden($key)) { |
|
244 | + return; |
|
245 | + } |
|
246 | + if (empty($value)) { |
|
247 | + $value = __('No Title', 'site-reviews'); |
|
248 | + } |
|
249 | + return '<h3>'.$value.'</h3>'; |
|
250 | + } |
|
251 | 251 | |
252 | - /** |
|
253 | - * @param string $avatarUrl |
|
254 | - * @return string |
|
255 | - */ |
|
256 | - protected function generateAvatar($avatarUrl) |
|
257 | - { |
|
258 | - if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
259 | - return $avatarUrl; |
|
260 | - } |
|
261 | - $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
262 | - if (empty($authorIdOrEmail)) { |
|
263 | - $authorIdOrEmail = $this->current->email; |
|
264 | - } |
|
265 | - if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
266 | - return $newAvatar; |
|
267 | - } |
|
268 | - return $avatarUrl; |
|
269 | - } |
|
252 | + /** |
|
253 | + * @param string $avatarUrl |
|
254 | + * @return string |
|
255 | + */ |
|
256 | + protected function generateAvatar($avatarUrl) |
|
257 | + { |
|
258 | + if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
259 | + return $avatarUrl; |
|
260 | + } |
|
261 | + $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
262 | + if (empty($authorIdOrEmail)) { |
|
263 | + $authorIdOrEmail = $this->current->email; |
|
264 | + } |
|
265 | + if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
266 | + return $newAvatar; |
|
267 | + } |
|
268 | + return $avatarUrl; |
|
269 | + } |
|
270 | 270 | |
271 | - /** |
|
272 | - * @param string $text |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - protected function getExcerpt($text) |
|
276 | - { |
|
277 | - $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
278 | - $split = extension_loaded('intl') |
|
279 | - ? $this->getExcerptIntlSplit($text, $limit) |
|
280 | - : $this->getExcerptSplit($text, $limit); |
|
281 | - $hiddenText = substr($text, $split); |
|
282 | - if (!empty($hiddenText)) { |
|
283 | - $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
284 | - 'class' => 'glsr-hidden glsr-hidden-text', |
|
285 | - 'data-show-less' => __('Show less', 'site-reviews'), |
|
286 | - 'data-show-more' => __('Show more', 'site-reviews'), |
|
287 | - ]); |
|
288 | - $text = ltrim(substr($text, 0, $split)).$showMore; |
|
289 | - } |
|
290 | - return $text; |
|
291 | - } |
|
271 | + /** |
|
272 | + * @param string $text |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + protected function getExcerpt($text) |
|
276 | + { |
|
277 | + $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
278 | + $split = extension_loaded('intl') |
|
279 | + ? $this->getExcerptIntlSplit($text, $limit) |
|
280 | + : $this->getExcerptSplit($text, $limit); |
|
281 | + $hiddenText = substr($text, $split); |
|
282 | + if (!empty($hiddenText)) { |
|
283 | + $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
284 | + 'class' => 'glsr-hidden glsr-hidden-text', |
|
285 | + 'data-show-less' => __('Show less', 'site-reviews'), |
|
286 | + 'data-show-more' => __('Show more', 'site-reviews'), |
|
287 | + ]); |
|
288 | + $text = ltrim(substr($text, 0, $split)).$showMore; |
|
289 | + } |
|
290 | + return $text; |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * @param string $text |
|
295 | - * @param int $limit |
|
296 | - * @return int |
|
297 | - */ |
|
298 | - protected function getExcerptIntlSplit($text, $limit) |
|
299 | - { |
|
300 | - $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
301 | - $words->setText($text); |
|
302 | - $count = 0; |
|
303 | - foreach ($words as $offset) { |
|
304 | - if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
305 | - continue; |
|
306 | - } |
|
307 | - ++$count; |
|
308 | - if ($count != $limit) { |
|
309 | - continue; |
|
310 | - } |
|
311 | - return $offset; |
|
312 | - } |
|
313 | - return strlen($text); |
|
314 | - } |
|
293 | + /** |
|
294 | + * @param string $text |
|
295 | + * @param int $limit |
|
296 | + * @return int |
|
297 | + */ |
|
298 | + protected function getExcerptIntlSplit($text, $limit) |
|
299 | + { |
|
300 | + $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
301 | + $words->setText($text); |
|
302 | + $count = 0; |
|
303 | + foreach ($words as $offset) { |
|
304 | + if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
305 | + continue; |
|
306 | + } |
|
307 | + ++$count; |
|
308 | + if ($count != $limit) { |
|
309 | + continue; |
|
310 | + } |
|
311 | + return $offset; |
|
312 | + } |
|
313 | + return strlen($text); |
|
314 | + } |
|
315 | 315 | |
316 | - /** |
|
317 | - * @param string $text |
|
318 | - * @param int $limit |
|
319 | - * @return int |
|
320 | - */ |
|
321 | - protected function getExcerptSplit($text, $limit) |
|
322 | - { |
|
323 | - if (str_word_count($text, 0) > $limit) { |
|
324 | - $words = array_keys(str_word_count($text, 2)); |
|
325 | - return $words[$limit]; |
|
326 | - } |
|
327 | - return strlen($text); |
|
328 | - } |
|
316 | + /** |
|
317 | + * @param string $text |
|
318 | + * @param int $limit |
|
319 | + * @return int |
|
320 | + */ |
|
321 | + protected function getExcerptSplit($text, $limit) |
|
322 | + { |
|
323 | + if (str_word_count($text, 0) > $limit) { |
|
324 | + $words = array_keys(str_word_count($text, 2)); |
|
325 | + return $words[$limit]; |
|
326 | + } |
|
327 | + return strlen($text); |
|
328 | + } |
|
329 | 329 | |
330 | - /** |
|
331 | - * @param string $path |
|
332 | - * @param mixed $fallback |
|
333 | - * @return mixed |
|
334 | - */ |
|
335 | - protected function getOption($path, $fallback = '') |
|
336 | - { |
|
337 | - if (array_key_exists($path, $this->options)) { |
|
338 | - return $this->options[$path]; |
|
339 | - } |
|
340 | - return $fallback; |
|
341 | - } |
|
330 | + /** |
|
331 | + * @param string $path |
|
332 | + * @param mixed $fallback |
|
333 | + * @return mixed |
|
334 | + */ |
|
335 | + protected function getOption($path, $fallback = '') |
|
336 | + { |
|
337 | + if (array_key_exists($path, $this->options)) { |
|
338 | + return $this->options[$path]; |
|
339 | + } |
|
340 | + return $fallback; |
|
341 | + } |
|
342 | 342 | |
343 | - /** |
|
344 | - * @param string $key |
|
345 | - * @param string $value |
|
346 | - * @return bool |
|
347 | - */ |
|
348 | - protected function isHiddenOrEmpty($key, $value) |
|
349 | - { |
|
350 | - return $this->isHidden($key) || empty($value); |
|
351 | - } |
|
343 | + /** |
|
344 | + * @param string $key |
|
345 | + * @param string $value |
|
346 | + * @return bool |
|
347 | + */ |
|
348 | + protected function isHiddenOrEmpty($key, $value) |
|
349 | + { |
|
350 | + return $this->isHidden($key) || empty($value); |
|
351 | + } |
|
352 | 352 | |
353 | - /** |
|
354 | - * @param string $path |
|
355 | - * @return bool |
|
356 | - */ |
|
357 | - protected function isOptionEnabled($path) |
|
358 | - { |
|
359 | - return 'yes' == $this->getOption($path); |
|
360 | - } |
|
353 | + /** |
|
354 | + * @param string $path |
|
355 | + * @return bool |
|
356 | + */ |
|
357 | + protected function isOptionEnabled($path) |
|
358 | + { |
|
359 | + return 'yes' == $this->getOption($path); |
|
360 | + } |
|
361 | 361 | |
362 | - /** |
|
363 | - * @param string $text |
|
364 | - * @return string |
|
365 | - */ |
|
366 | - protected function normalizeText($text) |
|
367 | - { |
|
368 | - $text = wp_kses($text, wp_kses_allowed_html()); |
|
369 | - $text = convert_smilies(strip_shortcodes($text)); |
|
370 | - $text = str_replace(']]>', ']]>', $text); |
|
371 | - $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
372 | - if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
373 | - $text = $this->getExcerpt($text); |
|
374 | - } |
|
375 | - return wptexturize(nl2br($text)); |
|
376 | - } |
|
362 | + /** |
|
363 | + * @param string $text |
|
364 | + * @return string |
|
365 | + */ |
|
366 | + protected function normalizeText($text) |
|
367 | + { |
|
368 | + $text = wp_kses($text, wp_kses_allowed_html()); |
|
369 | + $text = convert_smilies(strip_shortcodes($text)); |
|
370 | + $text = str_replace(']]>', ']]>', $text); |
|
371 | + $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
372 | + if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
373 | + $text = $this->getExcerpt($text); |
|
374 | + } |
|
375 | + return wptexturize(nl2br($text)); |
|
376 | + } |
|
377 | 377 | |
378 | - /** |
|
379 | - * @return void |
|
380 | - */ |
|
381 | - protected function wrap(array &$renderedFields, Review $review) |
|
382 | - { |
|
383 | - $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review); |
|
384 | - array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
385 | - $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
386 | - if (empty($value)) { |
|
387 | - return; |
|
388 | - } |
|
389 | - $value = glsr(Builder::class)->div($value, [ |
|
390 | - 'class' => 'glsr-review-'.$key, |
|
391 | - ]); |
|
392 | - }); |
|
393 | - } |
|
378 | + /** |
|
379 | + * @return void |
|
380 | + */ |
|
381 | + protected function wrap(array &$renderedFields, Review $review) |
|
382 | + { |
|
383 | + $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review); |
|
384 | + array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
385 | + $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
386 | + if (empty($value)) { |
|
387 | + return; |
|
388 | + } |
|
389 | + $value = glsr(Builder::class)->div($value, [ |
|
390 | + 'class' => 'glsr-review-'.$key, |
|
391 | + ]); |
|
392 | + }); |
|
393 | + } |
|
394 | 394 | } |
@@ -7,135 +7,135 @@ |
||
7 | 7 | |
8 | 8 | class ReviewsHtml extends ArrayObject |
9 | 9 | { |
10 | - /** |
|
11 | - * @var array |
|
12 | - */ |
|
13 | - public $args; |
|
10 | + /** |
|
11 | + * @var array |
|
12 | + */ |
|
13 | + public $args; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @var int |
|
17 | - */ |
|
18 | - public $max_num_pages; |
|
15 | + /** |
|
16 | + * @var int |
|
17 | + */ |
|
18 | + public $max_num_pages; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - public $pagination; |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + public $pagination; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - public $reviews; |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + public $reviews; |
|
29 | 29 | |
30 | - public function __construct(array $reviews, $maxPageCount, array $args) |
|
31 | - { |
|
32 | - $this->args = $args; |
|
33 | - $this->max_num_pages = $maxPageCount; |
|
34 | - $this->reviews = $reviews; |
|
35 | - $this->pagination = $this->buildPagination(); |
|
36 | - parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
37 | - } |
|
30 | + public function __construct(array $reviews, $maxPageCount, array $args) |
|
31 | + { |
|
32 | + $this->args = $args; |
|
33 | + $this->max_num_pages = $maxPageCount; |
|
34 | + $this->reviews = $reviews; |
|
35 | + $this->pagination = $this->buildPagination(); |
|
36 | + parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public function __toString() |
|
43 | - { |
|
44 | - return glsr(Template::class)->build('templates/reviews', [ |
|
45 | - 'args' => $this->args, |
|
46 | - 'context' => [ |
|
47 | - 'assigned_to' => $this->args['assigned_to'], |
|
48 | - 'category' => $this->args['category'], |
|
49 | - 'class' => $this->getClass(), |
|
50 | - 'id' => $this->args['id'], |
|
51 | - 'pagination' => $this->getPagination(), |
|
52 | - 'reviews' => $this->getReviews(), |
|
53 | - ], |
|
54 | - ]); |
|
55 | - } |
|
39 | + /** |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public function __toString() |
|
43 | + { |
|
44 | + return glsr(Template::class)->build('templates/reviews', [ |
|
45 | + 'args' => $this->args, |
|
46 | + 'context' => [ |
|
47 | + 'assigned_to' => $this->args['assigned_to'], |
|
48 | + 'category' => $this->args['category'], |
|
49 | + 'class' => $this->getClass(), |
|
50 | + 'id' => $this->args['id'], |
|
51 | + 'pagination' => $this->getPagination(), |
|
52 | + 'reviews' => $this->getReviews(), |
|
53 | + ], |
|
54 | + ]); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return string |
|
59 | - */ |
|
60 | - public function getPagination() |
|
61 | - { |
|
62 | - return wp_validate_boolean($this->args['pagination']) |
|
63 | - ? $this->pagination |
|
64 | - : ''; |
|
65 | - } |
|
57 | + /** |
|
58 | + * @return string |
|
59 | + */ |
|
60 | + public function getPagination() |
|
61 | + { |
|
62 | + return wp_validate_boolean($this->args['pagination']) |
|
63 | + ? $this->pagination |
|
64 | + : ''; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @return string |
|
69 | - */ |
|
70 | - public function getReviews() |
|
71 | - { |
|
72 | - $html = empty($this->reviews) |
|
73 | - ? $this->getReviewsFallback() |
|
74 | - : implode(PHP_EOL, $this->reviews); |
|
75 | - $wrapper = '<div class="glsr-reviews">%s</div>'; |
|
76 | - $wrapper = apply_filters('site-reviews/reviews/reviews-wrapper', $wrapper); |
|
77 | - return sprintf($wrapper, $html); |
|
78 | - } |
|
67 | + /** |
|
68 | + * @return string |
|
69 | + */ |
|
70 | + public function getReviews() |
|
71 | + { |
|
72 | + $html = empty($this->reviews) |
|
73 | + ? $this->getReviewsFallback() |
|
74 | + : implode(PHP_EOL, $this->reviews); |
|
75 | + $wrapper = '<div class="glsr-reviews">%s</div>'; |
|
76 | + $wrapper = apply_filters('site-reviews/reviews/reviews-wrapper', $wrapper); |
|
77 | + return sprintf($wrapper, $html); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @param mixed $key |
|
82 | - * @return mixed |
|
83 | - */ |
|
84 | - public function offsetGet($key) |
|
85 | - { |
|
86 | - if ('navigation' == $key) { |
|
87 | - glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.'; |
|
88 | - return $this->pagination; |
|
89 | - } |
|
90 | - if (property_exists($this, $key)) { |
|
91 | - return $this->{$key}; |
|
92 | - } |
|
93 | - return array_key_exists($key, $this->reviews) |
|
94 | - ? $this->reviews[$key] |
|
95 | - : null; |
|
96 | - } |
|
80 | + /** |
|
81 | + * @param mixed $key |
|
82 | + * @return mixed |
|
83 | + */ |
|
84 | + public function offsetGet($key) |
|
85 | + { |
|
86 | + if ('navigation' == $key) { |
|
87 | + glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.'; |
|
88 | + return $this->pagination; |
|
89 | + } |
|
90 | + if (property_exists($this, $key)) { |
|
91 | + return $this->{$key}; |
|
92 | + } |
|
93 | + return array_key_exists($key, $this->reviews) |
|
94 | + ? $this->reviews[$key] |
|
95 | + : null; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - protected function buildPagination() |
|
102 | - { |
|
103 | - $html = glsr(Partial::class)->build('pagination', [ |
|
104 | - 'baseUrl' => glsr_get($this->args, 'pagedUrl'), |
|
105 | - 'current' => glsr_get($this->args, 'paged'), |
|
106 | - 'total' => $this->max_num_pages, |
|
107 | - ]); |
|
108 | - $html.= sprintf('<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json']); |
|
109 | - $wrapper = '<div class="glsr-pagination">%s</div>'; |
|
110 | - $wrapper = apply_filters('site-reviews/reviews/pagination-wrapper', $wrapper); |
|
111 | - return sprintf($wrapper, $html); |
|
112 | - } |
|
98 | + /** |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + protected function buildPagination() |
|
102 | + { |
|
103 | + $html = glsr(Partial::class)->build('pagination', [ |
|
104 | + 'baseUrl' => glsr_get($this->args, 'pagedUrl'), |
|
105 | + 'current' => glsr_get($this->args, 'paged'), |
|
106 | + 'total' => $this->max_num_pages, |
|
107 | + ]); |
|
108 | + $html.= sprintf('<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json']); |
|
109 | + $wrapper = '<div class="glsr-pagination">%s</div>'; |
|
110 | + $wrapper = apply_filters('site-reviews/reviews/pagination-wrapper', $wrapper); |
|
111 | + return sprintf($wrapper, $html); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - protected function getClass() |
|
118 | - { |
|
119 | - $defaults = [ |
|
120 | - 'glsr-default', |
|
121 | - ]; |
|
122 | - if ('ajax' == $this->args['pagination']) { |
|
123 | - $defaults[] = 'glsr-ajax-pagination'; |
|
124 | - } |
|
125 | - $classes = explode(' ', $this->args['class']); |
|
126 | - $classes = array_unique(array_merge($defaults, array_filter($classes))); |
|
127 | - return implode(' ', $classes); |
|
128 | - } |
|
114 | + /** |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + protected function getClass() |
|
118 | + { |
|
119 | + $defaults = [ |
|
120 | + 'glsr-default', |
|
121 | + ]; |
|
122 | + if ('ajax' == $this->args['pagination']) { |
|
123 | + $defaults[] = 'glsr-ajax-pagination'; |
|
124 | + } |
|
125 | + $classes = explode(' ', $this->args['class']); |
|
126 | + $classes = array_unique(array_merge($defaults, array_filter($classes))); |
|
127 | + return implode(' ', $classes); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - protected function getReviewsFallback() |
|
134 | - { |
|
135 | - if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) { |
|
136 | - $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews'); |
|
137 | - } |
|
138 | - $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>'; |
|
139 | - return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args); |
|
140 | - } |
|
130 | + /** |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + protected function getReviewsFallback() |
|
134 | + { |
|
135 | + if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) { |
|
136 | + $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews'); |
|
137 | + } |
|
138 | + $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>'; |
|
139 | + return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args); |
|
140 | + } |
|
141 | 141 | } |
@@ -8,114 +8,114 @@ |
||
8 | 8 | |
9 | 9 | class Notice |
10 | 10 | { |
11 | - /** |
|
12 | - * @param string $type |
|
13 | - * @param string|array|WP_Error $message |
|
14 | - * @return void |
|
15 | - */ |
|
16 | - public function add($type, $message, array $args = []) |
|
17 | - { |
|
18 | - if (empty(array_filter([$message, $type]))) { |
|
19 | - return; |
|
20 | - } |
|
21 | - $args['message'] = $message; |
|
22 | - $args['type'] = $type; |
|
23 | - add_settings_error(Application::ID, '', json_encode($this->normalize($args))); |
|
24 | - } |
|
11 | + /** |
|
12 | + * @param string $type |
|
13 | + * @param string|array|WP_Error $message |
|
14 | + * @return void |
|
15 | + */ |
|
16 | + public function add($type, $message, array $args = []) |
|
17 | + { |
|
18 | + if (empty(array_filter([$message, $type]))) { |
|
19 | + return; |
|
20 | + } |
|
21 | + $args['message'] = $message; |
|
22 | + $args['type'] = $type; |
|
23 | + add_settings_error(Application::ID, '', json_encode($this->normalize($args))); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @param string|array|WP_Error $message |
|
28 | - * @return void |
|
29 | - */ |
|
30 | - public function addError($message, array $args = []) |
|
31 | - { |
|
32 | - $this->add('error', $message, $args); |
|
33 | - } |
|
26 | + /** |
|
27 | + * @param string|array|WP_Error $message |
|
28 | + * @return void |
|
29 | + */ |
|
30 | + public function addError($message, array $args = []) |
|
31 | + { |
|
32 | + $this->add('error', $message, $args); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param string|array|WP_Error $message |
|
37 | - * @return void |
|
38 | - */ |
|
39 | - public function addSuccess($message, array $args = []) |
|
40 | - { |
|
41 | - $this->add('success', $message, $args); |
|
42 | - } |
|
35 | + /** |
|
36 | + * @param string|array|WP_Error $message |
|
37 | + * @return void |
|
38 | + */ |
|
39 | + public function addSuccess($message, array $args = []) |
|
40 | + { |
|
41 | + $this->add('success', $message, $args); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param string|array|WP_Error $message |
|
46 | - * @return void |
|
47 | - */ |
|
48 | - public function addWarning($message, array $args = []) |
|
49 | - { |
|
50 | - $this->add('warning', $message, $args); |
|
51 | - } |
|
44 | + /** |
|
45 | + * @param string|array|WP_Error $message |
|
46 | + * @return void |
|
47 | + */ |
|
48 | + public function addWarning($message, array $args = []) |
|
49 | + { |
|
50 | + $this->add('warning', $message, $args); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return static |
|
55 | - */ |
|
56 | - public function clear() |
|
57 | - { |
|
58 | - global $wp_settings_errors; |
|
59 | - $wp_settings_errors = []; |
|
60 | - delete_transient('settings_errors'); |
|
61 | - return $this; |
|
62 | - } |
|
53 | + /** |
|
54 | + * @return static |
|
55 | + */ |
|
56 | + public function clear() |
|
57 | + { |
|
58 | + global $wp_settings_errors; |
|
59 | + $wp_settings_errors = []; |
|
60 | + delete_transient('settings_errors'); |
|
61 | + return $this; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function get() |
|
68 | - { |
|
69 | - $notices = array_map('unserialize', |
|
70 | - array_unique(array_map('serialize', get_settings_errors(Application::ID))) |
|
71 | - ); |
|
72 | - if (empty($notices)) { |
|
73 | - return; |
|
74 | - } |
|
75 | - return array_reduce($notices, function ($carry, $notice) { |
|
76 | - return $carry.$this->buildNotice(json_decode($notice['message'], true)); |
|
77 | - }); |
|
78 | - } |
|
64 | + /** |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function get() |
|
68 | + { |
|
69 | + $notices = array_map('unserialize', |
|
70 | + array_unique(array_map('serialize', get_settings_errors(Application::ID))) |
|
71 | + ); |
|
72 | + if (empty($notices)) { |
|
73 | + return; |
|
74 | + } |
|
75 | + return array_reduce($notices, function ($carry, $notice) { |
|
76 | + return $carry.$this->buildNotice(json_decode($notice['message'], true)); |
|
77 | + }); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - protected function buildNotice(array $args) |
|
84 | - { |
|
85 | - $messages = array_reduce($args['messages'], function ($carry, $message) { |
|
86 | - return $carry.glsr(Builder::class)->p($message); |
|
87 | - }); |
|
88 | - $class = 'notice notice-'.$args['type']; |
|
89 | - if ($args['inline']) { |
|
90 | - $class.= ' inline'; |
|
91 | - } |
|
92 | - if ($args['dismissible']) { |
|
93 | - $class.= ' is-dismissible'; |
|
94 | - } |
|
95 | - return glsr(Builder::class)->div($messages, [ |
|
96 | - 'class' => $class, |
|
97 | - ]); |
|
98 | - } |
|
80 | + /** |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + protected function buildNotice(array $args) |
|
84 | + { |
|
85 | + $messages = array_reduce($args['messages'], function ($carry, $message) { |
|
86 | + return $carry.glsr(Builder::class)->p($message); |
|
87 | + }); |
|
88 | + $class = 'notice notice-'.$args['type']; |
|
89 | + if ($args['inline']) { |
|
90 | + $class.= ' inline'; |
|
91 | + } |
|
92 | + if ($args['dismissible']) { |
|
93 | + $class.= ' is-dismissible'; |
|
94 | + } |
|
95 | + return glsr(Builder::class)->div($messages, [ |
|
96 | + 'class' => $class, |
|
97 | + ]); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @return array |
|
102 | - */ |
|
103 | - protected function normalize(array $args) |
|
104 | - { |
|
105 | - $defaults = [ |
|
106 | - 'dismissible' => true, |
|
107 | - 'inline' => true, |
|
108 | - 'message' => '', |
|
109 | - 'type' => '', |
|
110 | - ]; |
|
111 | - $args = shortcode_atts($defaults, $args); |
|
112 | - if (!in_array($args['type'], ['error', 'warning', 'success'])) { |
|
113 | - $args['type'] = 'success'; |
|
114 | - } |
|
115 | - $args['messages'] = is_wp_error($args['message']) |
|
116 | - ? (array) $args['message']->get_error_message() |
|
117 | - : (array) $args['message']; |
|
118 | - unset($args['message']); |
|
119 | - return $args; |
|
120 | - } |
|
100 | + /** |
|
101 | + * @return array |
|
102 | + */ |
|
103 | + protected function normalize(array $args) |
|
104 | + { |
|
105 | + $defaults = [ |
|
106 | + 'dismissible' => true, |
|
107 | + 'inline' => true, |
|
108 | + 'message' => '', |
|
109 | + 'type' => '', |
|
110 | + ]; |
|
111 | + $args = shortcode_atts($defaults, $args); |
|
112 | + if (!in_array($args['type'], ['error', 'warning', 'success'])) { |
|
113 | + $args['type'] = 'success'; |
|
114 | + } |
|
115 | + $args['messages'] = is_wp_error($args['message']) |
|
116 | + ? (array) $args['message']->get_error_message() |
|
117 | + : (array) $args['message']; |
|
118 | + unset($args['message']); |
|
119 | + return $args; |
|
120 | + } |
|
121 | 121 | } |
@@ -10,216 +10,216 @@ |
||
10 | 10 | */ |
11 | 11 | class Session |
12 | 12 | { |
13 | - const DELIMITER = '__'; |
|
14 | - |
|
15 | - const SESSION_COOKIE = '_glsr_session'; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var int |
|
19 | - */ |
|
20 | - protected $expiryTimestamp; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var int |
|
24 | - */ |
|
25 | - protected $expiryTimestampReset; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $sessionData; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - protected $sessionId; |
|
36 | - |
|
37 | - public function __construct() |
|
38 | - { |
|
39 | - $cookieId = filter_input(INPUT_COOKIE, static::SESSION_COOKIE); |
|
40 | - if (false !== strpos($cookieId, static::DELIMITER)) { |
|
41 | - $cookie = explode(static::DELIMITER, stripslashes($cookieId)); |
|
42 | - $this->sessionId = preg_replace('/[^A-Za-z0-9_]/', '', $cookie[0]); |
|
43 | - $this->expiryTimestamp = absint($cookie[1]); |
|
44 | - $this->expiryTimestampReset = absint($cookie[2]); |
|
45 | - if (time() > $this->expiryTimestampReset) { |
|
46 | - $this->setCookieExpiration(); |
|
47 | - } |
|
48 | - } else { |
|
49 | - $this->sessionId = $this->generateSessionId(); |
|
50 | - $this->setCookieExpiration(); |
|
51 | - } |
|
52 | - $this->getSessionData(); |
|
53 | - $this->setCookie(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - public function clear() |
|
60 | - { |
|
61 | - $this->setCookieExpiration(); |
|
62 | - $this->regenerateSessionId('and delete session!'); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @return int|false |
|
67 | - */ |
|
68 | - public function deleteAllSessions() |
|
69 | - { |
|
70 | - return glsr(SqlQueries::class)->deleteAllSessions(static::SESSION_COOKIE); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @param int $limit |
|
75 | - * @return void |
|
76 | - */ |
|
77 | - public function deleteExpiredSessions($limit = 1000) |
|
78 | - { |
|
79 | - if ($expiredSessions = implode("','", $this->getExpiredSessions($limit))) { |
|
80 | - glsr(SqlQueries::class)->deleteExpiredSessions($expiredSessions); |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string $key |
|
86 | - * @param string|array $fallback |
|
87 | - * @param bool|string $unset |
|
88 | - * @return string|array |
|
89 | - */ |
|
90 | - public function get($key, $fallback = '', $unset = false) |
|
91 | - { |
|
92 | - $key = sanitize_key($key); |
|
93 | - $value = isset($this->sessionData[$key]) |
|
94 | - ? maybe_unserialize($this->sessionData[$key]) |
|
95 | - : $fallback; |
|
96 | - if (isset($this->sessionData[$key]) && $unset) { |
|
97 | - unset($this->sessionData[$key]); |
|
98 | - $this->updateSession(); |
|
99 | - } |
|
100 | - return $value; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param string $key |
|
105 | - * @param mixed $value |
|
106 | - * @return mixed |
|
107 | - */ |
|
108 | - public function set($key, $value) |
|
109 | - { |
|
110 | - $key = sanitize_key($key); |
|
111 | - $this->sessionData[$key] = maybe_serialize($value); |
|
112 | - $this->updateSession(); |
|
113 | - return $this->sessionData[$key]; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @return void |
|
118 | - */ |
|
119 | - protected function createSession() |
|
120 | - { |
|
121 | - add_option($this->getSessionId(), $this->sessionData, '', false); |
|
122 | - add_option($this->getSessionId('expires'), $this->expiryTimestamp, '', false); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @return void |
|
127 | - */ |
|
128 | - protected function deleteSession() |
|
129 | - { |
|
130 | - delete_option($this->getSessionId()); |
|
131 | - delete_option($this->getSessionId('expires')); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - protected function generateSessionId() |
|
138 | - { |
|
139 | - return md5((new PasswordHash(8, false))->get_random_bytes(32)); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param int $limit |
|
144 | - * @return array |
|
145 | - */ |
|
146 | - protected function getExpiredSessions($limit) |
|
147 | - { |
|
148 | - $expiredSessions = []; |
|
149 | - $sessions = glsr(SqlQueries::class)->getExpiredSessions(static::SESSION_COOKIE, absint($limit)); |
|
150 | - if (!empty($sessions)) { |
|
151 | - $now = time(); |
|
152 | - foreach ($sessions as $session) { |
|
153 | - if ($now <= $session->expiration) { |
|
154 | - continue; |
|
155 | - } |
|
156 | - $expiredSessions[] = $session->name; |
|
157 | - $expiredSessions[] = str_replace('_expires_', '_', $session->name); |
|
158 | - } |
|
159 | - } |
|
160 | - return $expiredSessions; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @param string $separator |
|
165 | - * @return string |
|
166 | - */ |
|
167 | - protected function getSessionId($separator = '') |
|
168 | - { |
|
169 | - return implode('_', array_filter([static::SESSION_COOKIE, $separator, $this->sessionId])); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @return array |
|
174 | - */ |
|
175 | - protected function getSessionData() |
|
176 | - { |
|
177 | - return $this->sessionData = (array) get_option($this->getSessionId(), []); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @param bool|string $deleteOld |
|
182 | - * @return void |
|
183 | - */ |
|
184 | - protected function regenerateSessionId($deleteOld = false) |
|
185 | - { |
|
186 | - if ($deleteOld) { |
|
187 | - $this->deleteSession(); |
|
188 | - } |
|
189 | - $this->sessionId = $this->generateSessionId(); |
|
190 | - $this->setCookie(); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @return void |
|
195 | - */ |
|
196 | - protected function setCookie() |
|
197 | - { |
|
198 | - if (headers_sent()) { |
|
199 | - return; |
|
200 | - } |
|
201 | - $cookie = $this->sessionId.static::DELIMITER.$this->expiryTimestamp.static::DELIMITER.$this->expiryTimestampReset; |
|
202 | - $cookiePath = preg_replace('|https?://[^/]+|i', '', trailingslashit((string) get_option('home'))); |
|
203 | - setcookie(static::SESSION_COOKIE, $cookie, $this->expiryTimestamp, $cookiePath); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @return void |
|
208 | - */ |
|
209 | - protected function setCookieExpiration() |
|
210 | - { |
|
211 | - $this->expiryTimestampReset = time() + (24 * 60); // 24 minutes |
|
212 | - $this->expiryTimestamp = time() + (30 * 60); // 30 minutes |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @return void |
|
217 | - */ |
|
218 | - protected function updateSession() |
|
219 | - { |
|
220 | - if (false === get_option($this->getSessionId())) { |
|
221 | - return $this->createSession(); |
|
222 | - } |
|
223 | - update_option($this->getSessionId(), $this->sessionData, false); |
|
224 | - } |
|
13 | + const DELIMITER = '__'; |
|
14 | + |
|
15 | + const SESSION_COOKIE = '_glsr_session'; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var int |
|
19 | + */ |
|
20 | + protected $expiryTimestamp; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var int |
|
24 | + */ |
|
25 | + protected $expiryTimestampReset; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $sessionData; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + protected $sessionId; |
|
36 | + |
|
37 | + public function __construct() |
|
38 | + { |
|
39 | + $cookieId = filter_input(INPUT_COOKIE, static::SESSION_COOKIE); |
|
40 | + if (false !== strpos($cookieId, static::DELIMITER)) { |
|
41 | + $cookie = explode(static::DELIMITER, stripslashes($cookieId)); |
|
42 | + $this->sessionId = preg_replace('/[^A-Za-z0-9_]/', '', $cookie[0]); |
|
43 | + $this->expiryTimestamp = absint($cookie[1]); |
|
44 | + $this->expiryTimestampReset = absint($cookie[2]); |
|
45 | + if (time() > $this->expiryTimestampReset) { |
|
46 | + $this->setCookieExpiration(); |
|
47 | + } |
|
48 | + } else { |
|
49 | + $this->sessionId = $this->generateSessionId(); |
|
50 | + $this->setCookieExpiration(); |
|
51 | + } |
|
52 | + $this->getSessionData(); |
|
53 | + $this->setCookie(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + public function clear() |
|
60 | + { |
|
61 | + $this->setCookieExpiration(); |
|
62 | + $this->regenerateSessionId('and delete session!'); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @return int|false |
|
67 | + */ |
|
68 | + public function deleteAllSessions() |
|
69 | + { |
|
70 | + return glsr(SqlQueries::class)->deleteAllSessions(static::SESSION_COOKIE); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @param int $limit |
|
75 | + * @return void |
|
76 | + */ |
|
77 | + public function deleteExpiredSessions($limit = 1000) |
|
78 | + { |
|
79 | + if ($expiredSessions = implode("','", $this->getExpiredSessions($limit))) { |
|
80 | + glsr(SqlQueries::class)->deleteExpiredSessions($expiredSessions); |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string $key |
|
86 | + * @param string|array $fallback |
|
87 | + * @param bool|string $unset |
|
88 | + * @return string|array |
|
89 | + */ |
|
90 | + public function get($key, $fallback = '', $unset = false) |
|
91 | + { |
|
92 | + $key = sanitize_key($key); |
|
93 | + $value = isset($this->sessionData[$key]) |
|
94 | + ? maybe_unserialize($this->sessionData[$key]) |
|
95 | + : $fallback; |
|
96 | + if (isset($this->sessionData[$key]) && $unset) { |
|
97 | + unset($this->sessionData[$key]); |
|
98 | + $this->updateSession(); |
|
99 | + } |
|
100 | + return $value; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param string $key |
|
105 | + * @param mixed $value |
|
106 | + * @return mixed |
|
107 | + */ |
|
108 | + public function set($key, $value) |
|
109 | + { |
|
110 | + $key = sanitize_key($key); |
|
111 | + $this->sessionData[$key] = maybe_serialize($value); |
|
112 | + $this->updateSession(); |
|
113 | + return $this->sessionData[$key]; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @return void |
|
118 | + */ |
|
119 | + protected function createSession() |
|
120 | + { |
|
121 | + add_option($this->getSessionId(), $this->sessionData, '', false); |
|
122 | + add_option($this->getSessionId('expires'), $this->expiryTimestamp, '', false); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @return void |
|
127 | + */ |
|
128 | + protected function deleteSession() |
|
129 | + { |
|
130 | + delete_option($this->getSessionId()); |
|
131 | + delete_option($this->getSessionId('expires')); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + protected function generateSessionId() |
|
138 | + { |
|
139 | + return md5((new PasswordHash(8, false))->get_random_bytes(32)); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param int $limit |
|
144 | + * @return array |
|
145 | + */ |
|
146 | + protected function getExpiredSessions($limit) |
|
147 | + { |
|
148 | + $expiredSessions = []; |
|
149 | + $sessions = glsr(SqlQueries::class)->getExpiredSessions(static::SESSION_COOKIE, absint($limit)); |
|
150 | + if (!empty($sessions)) { |
|
151 | + $now = time(); |
|
152 | + foreach ($sessions as $session) { |
|
153 | + if ($now <= $session->expiration) { |
|
154 | + continue; |
|
155 | + } |
|
156 | + $expiredSessions[] = $session->name; |
|
157 | + $expiredSessions[] = str_replace('_expires_', '_', $session->name); |
|
158 | + } |
|
159 | + } |
|
160 | + return $expiredSessions; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @param string $separator |
|
165 | + * @return string |
|
166 | + */ |
|
167 | + protected function getSessionId($separator = '') |
|
168 | + { |
|
169 | + return implode('_', array_filter([static::SESSION_COOKIE, $separator, $this->sessionId])); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @return array |
|
174 | + */ |
|
175 | + protected function getSessionData() |
|
176 | + { |
|
177 | + return $this->sessionData = (array) get_option($this->getSessionId(), []); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @param bool|string $deleteOld |
|
182 | + * @return void |
|
183 | + */ |
|
184 | + protected function regenerateSessionId($deleteOld = false) |
|
185 | + { |
|
186 | + if ($deleteOld) { |
|
187 | + $this->deleteSession(); |
|
188 | + } |
|
189 | + $this->sessionId = $this->generateSessionId(); |
|
190 | + $this->setCookie(); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @return void |
|
195 | + */ |
|
196 | + protected function setCookie() |
|
197 | + { |
|
198 | + if (headers_sent()) { |
|
199 | + return; |
|
200 | + } |
|
201 | + $cookie = $this->sessionId.static::DELIMITER.$this->expiryTimestamp.static::DELIMITER.$this->expiryTimestampReset; |
|
202 | + $cookiePath = preg_replace('|https?://[^/]+|i', '', trailingslashit((string) get_option('home'))); |
|
203 | + setcookie(static::SESSION_COOKIE, $cookie, $this->expiryTimestamp, $cookiePath); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @return void |
|
208 | + */ |
|
209 | + protected function setCookieExpiration() |
|
210 | + { |
|
211 | + $this->expiryTimestampReset = time() + (24 * 60); // 24 minutes |
|
212 | + $this->expiryTimestamp = time() + (30 * 60); // 30 minutes |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @return void |
|
217 | + */ |
|
218 | + protected function updateSession() |
|
219 | + { |
|
220 | + if (false === get_option($this->getSessionId())) { |
|
221 | + return $this->createSession(); |
|
222 | + } |
|
223 | + update_option($this->getSessionId(), $this->sessionData, false); |
|
224 | + } |
|
225 | 225 | } |
@@ -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 | } |