@@ -8,124 +8,124 @@ |
||
8 | 8 | |
9 | 9 | class NoticeController extends Controller |
10 | 10 | { |
11 | - const USER_META_KEY = '_glsr_notices'; |
|
11 | + const USER_META_KEY = '_glsr_notices'; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - protected $dismissValuesMap; |
|
13 | + /** |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + protected $dismissValuesMap; |
|
17 | 17 | |
18 | - public function __construct() |
|
19 | - { |
|
20 | - $this->dismissValuesMap = [ |
|
21 | - 'rebusify' => glsr()->version('major'), |
|
22 | - 'welcome' => glsr()->version('minor'), |
|
23 | - ]; |
|
24 | - } |
|
18 | + public function __construct() |
|
19 | + { |
|
20 | + $this->dismissValuesMap = [ |
|
21 | + 'rebusify' => glsr()->version('major'), |
|
22 | + 'welcome' => glsr()->version('minor'), |
|
23 | + ]; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @return void |
|
28 | - * @action admin_notices |
|
29 | - */ |
|
30 | - public function filterAdminNotices() |
|
31 | - { |
|
32 | - $screen = glsr_current_screen(); |
|
33 | - $this->renderWelcomeNotice($screen->post_type); |
|
34 | - $this->renderRebusifyNotice($screen->post_type); |
|
35 | - $this->renderAddonsNotice($screen->id); |
|
36 | - } |
|
26 | + /** |
|
27 | + * @return void |
|
28 | + * @action admin_notices |
|
29 | + */ |
|
30 | + public function filterAdminNotices() |
|
31 | + { |
|
32 | + $screen = glsr_current_screen(); |
|
33 | + $this->renderWelcomeNotice($screen->post_type); |
|
34 | + $this->renderRebusifyNotice($screen->post_type); |
|
35 | + $this->renderAddonsNotice($screen->id); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return void |
|
40 | - */ |
|
41 | - public function routerDismissNotice(array $request) |
|
42 | - { |
|
43 | - if ($key = Arr::get($request, 'notice')) { |
|
44 | - $this->dismissNotice($key); |
|
45 | - } |
|
46 | - } |
|
38 | + /** |
|
39 | + * @return void |
|
40 | + */ |
|
41 | + public function routerDismissNotice(array $request) |
|
42 | + { |
|
43 | + if ($key = Arr::get($request, 'notice')) { |
|
44 | + $this->dismissNotice($key); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param string $key |
|
50 | - * @return void |
|
51 | - */ |
|
52 | - protected function dismissNotice($key) |
|
53 | - { |
|
54 | - $this->setUserMeta($key, $this->getVersionFor($key)); |
|
55 | - } |
|
48 | + /** |
|
49 | + * @param string $key |
|
50 | + * @return void |
|
51 | + */ |
|
52 | + protected function dismissNotice($key) |
|
53 | + { |
|
54 | + $this->setUserMeta($key, $this->getVersionFor($key)); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param string $key |
|
59 | - * @param mixed $fallback |
|
60 | - * @return mixed |
|
61 | - */ |
|
62 | - protected function getUserMeta($key, $fallback) |
|
63 | - { |
|
64 | - $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true); |
|
65 | - return Arr::get($meta, $key, $fallback); |
|
66 | - } |
|
57 | + /** |
|
58 | + * @param string $key |
|
59 | + * @param mixed $fallback |
|
60 | + * @return mixed |
|
61 | + */ |
|
62 | + protected function getUserMeta($key, $fallback) |
|
63 | + { |
|
64 | + $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true); |
|
65 | + return Arr::get($meta, $key, $fallback); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @param string $noticeKey |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - protected function getVersionFor($noticeKey) |
|
73 | - { |
|
74 | - return Arr::get($this->dismissValuesMap, $noticeKey, glsr()->version('major')); |
|
75 | - } |
|
68 | + /** |
|
69 | + * @param string $noticeKey |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + protected function getVersionFor($noticeKey) |
|
73 | + { |
|
74 | + return Arr::get($this->dismissValuesMap, $noticeKey, glsr()->version('major')); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @param string $screenId |
|
79 | - * @return void |
|
80 | - */ |
|
81 | - protected function renderAddonsNotice($screenId) |
|
82 | - { |
|
83 | - if (Application::POST_TYPE.'_page_addons' == $screenId) { |
|
84 | - glsr()->render('partials/notices/addons'); |
|
85 | - } |
|
86 | - } |
|
77 | + /** |
|
78 | + * @param string $screenId |
|
79 | + * @return void |
|
80 | + */ |
|
81 | + protected function renderAddonsNotice($screenId) |
|
82 | + { |
|
83 | + if (Application::POST_TYPE.'_page_addons' == $screenId) { |
|
84 | + glsr()->render('partials/notices/addons'); |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @param string $screenPostType |
|
90 | - * @return void |
|
91 | - */ |
|
92 | - protected function renderRebusifyNotice($screenPostType) |
|
93 | - { |
|
94 | - if (Application::POST_TYPE == $screenPostType |
|
95 | - && version_compare($this->getVersionFor('rebusify'), $this->getUserMeta('rebusify', 0), '>') |
|
96 | - && !glsr(OptionManager::class)->getBool('settings.general.rebusify')) { |
|
97 | - glsr()->render('partials/notices/rebusify'); |
|
98 | - } |
|
99 | - } |
|
88 | + /** |
|
89 | + * @param string $screenPostType |
|
90 | + * @return void |
|
91 | + */ |
|
92 | + protected function renderRebusifyNotice($screenPostType) |
|
93 | + { |
|
94 | + if (Application::POST_TYPE == $screenPostType |
|
95 | + && version_compare($this->getVersionFor('rebusify'), $this->getUserMeta('rebusify', 0), '>') |
|
96 | + && !glsr(OptionManager::class)->getBool('settings.general.rebusify')) { |
|
97 | + glsr()->render('partials/notices/rebusify'); |
|
98 | + } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @param string $screenPostType |
|
103 | - * @return void |
|
104 | - */ |
|
105 | - protected function renderWelcomeNotice($screenPostType) |
|
106 | - { |
|
107 | - if (Application::POST_TYPE == $screenPostType |
|
108 | - && version_compare($this->getVersionFor('welcome'), $this->getUserMeta('welcome', 0), '>')) { |
|
109 | - $welcomeText = '0.0.0' == glsr(OptionManager::class)->get('version_upgraded_from') |
|
110 | - ? __('Thanks for installing Site Reviews %s, we hope you love it!', 'site-reviews') |
|
111 | - : __('Thanks for updating to Site Reviews %s, we hope you love the changes!', 'site-reviews'); |
|
112 | - glsr()->render('partials/notices/welcome', [ |
|
113 | - 'text' => sprintf($welcomeText, glsr()->version), |
|
114 | - ]); |
|
115 | - } |
|
116 | - } |
|
101 | + /** |
|
102 | + * @param string $screenPostType |
|
103 | + * @return void |
|
104 | + */ |
|
105 | + protected function renderWelcomeNotice($screenPostType) |
|
106 | + { |
|
107 | + if (Application::POST_TYPE == $screenPostType |
|
108 | + && version_compare($this->getVersionFor('welcome'), $this->getUserMeta('welcome', 0), '>')) { |
|
109 | + $welcomeText = '0.0.0' == glsr(OptionManager::class)->get('version_upgraded_from') |
|
110 | + ? __('Thanks for installing Site Reviews %s, we hope you love it!', 'site-reviews') |
|
111 | + : __('Thanks for updating to Site Reviews %s, we hope you love the changes!', 'site-reviews'); |
|
112 | + glsr()->render('partials/notices/welcome', [ |
|
113 | + 'text' => sprintf($welcomeText, glsr()->version), |
|
114 | + ]); |
|
115 | + } |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * @param string $key |
|
120 | - * @param mixed $fallback |
|
121 | - * @return mixed |
|
122 | - */ |
|
123 | - protected function setUserMeta($key, $value) |
|
124 | - { |
|
125 | - $userId = get_current_user_id(); |
|
126 | - $meta = (array) get_user_meta($userId, static::USER_META_KEY, true); |
|
127 | - $meta = array_filter(wp_parse_args($meta, [])); |
|
128 | - $meta[$key] = $value; |
|
129 | - update_user_meta($userId, static::USER_META_KEY, $meta); |
|
130 | - } |
|
118 | + /** |
|
119 | + * @param string $key |
|
120 | + * @param mixed $fallback |
|
121 | + * @return mixed |
|
122 | + */ |
|
123 | + protected function setUserMeta($key, $value) |
|
124 | + { |
|
125 | + $userId = get_current_user_id(); |
|
126 | + $meta = (array) get_user_meta($userId, static::USER_META_KEY, true); |
|
127 | + $meta = array_filter(wp_parse_args($meta, [])); |
|
128 | + $meta[$key] = $value; |
|
129 | + update_user_meta($userId, static::USER_META_KEY, $meta); |
|
130 | + } |
|
131 | 131 | } |
@@ -9,339 +9,339 @@ |
||
9 | 9 | |
10 | 10 | final class Application extends Container |
11 | 11 | { |
12 | - const CAPABILITY = 'edit_others_posts'; |
|
13 | - const CRON_EVENT = 'site-reviews/schedule/session/purge'; |
|
14 | - const ID = 'site-reviews'; |
|
15 | - const PAGED_QUERY_VAR = 'reviews-page'; |
|
16 | - const POST_TYPE = 'site-review'; |
|
17 | - const PREFIX = 'glsr_'; |
|
18 | - const TAXONOMY = 'site-review-category'; |
|
12 | + const CAPABILITY = 'edit_others_posts'; |
|
13 | + const CRON_EVENT = 'site-reviews/schedule/session/purge'; |
|
14 | + const ID = 'site-reviews'; |
|
15 | + const PAGED_QUERY_VAR = 'reviews-page'; |
|
16 | + const POST_TYPE = 'site-review'; |
|
17 | + const PREFIX = 'glsr_'; |
|
18 | + const TAXONOMY = 'site-review-category'; |
|
19 | 19 | |
20 | - public $addons = []; |
|
21 | - public $defaults; |
|
22 | - public $deprecated = []; |
|
23 | - public $file; |
|
24 | - public $languages; |
|
25 | - public $mceShortcodes = []; //defined elsewhere |
|
26 | - public $name; |
|
27 | - public $postTypeColumns = []; // defined elsewhere |
|
28 | - public $reviewTypes; |
|
29 | - public $schemas = []; //defined elsewhere |
|
30 | - public $version; |
|
20 | + public $addons = []; |
|
21 | + public $defaults; |
|
22 | + public $deprecated = []; |
|
23 | + public $file; |
|
24 | + public $languages; |
|
25 | + public $mceShortcodes = []; //defined elsewhere |
|
26 | + public $name; |
|
27 | + public $postTypeColumns = []; // defined elsewhere |
|
28 | + public $reviewTypes; |
|
29 | + public $schemas = []; //defined elsewhere |
|
30 | + public $version; |
|
31 | 31 | |
32 | - public function __construct() |
|
33 | - { |
|
34 | - static::$instance = $this; |
|
35 | - $this->file = realpath(trailingslashit(dirname(__DIR__)).static::ID.'.php'); |
|
36 | - $plugin = get_file_data($this->file, [ |
|
37 | - 'languages' => 'Domain Path', |
|
38 | - 'name' => 'Plugin Name', |
|
39 | - 'version' => 'Version', |
|
40 | - ], 'plugin'); |
|
41 | - array_walk($plugin, function ($value, $key) { |
|
42 | - $this->$key = $value; |
|
43 | - }); |
|
44 | - } |
|
32 | + public function __construct() |
|
33 | + { |
|
34 | + static::$instance = $this; |
|
35 | + $this->file = realpath(trailingslashit(dirname(__DIR__)).static::ID.'.php'); |
|
36 | + $plugin = get_file_data($this->file, [ |
|
37 | + 'languages' => 'Domain Path', |
|
38 | + 'name' => 'Plugin Name', |
|
39 | + 'version' => 'Version', |
|
40 | + ], 'plugin'); |
|
41 | + array_walk($plugin, function ($value, $key) { |
|
42 | + $this->$key = $value; |
|
43 | + }); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function activate() |
|
50 | - { |
|
51 | - $this->make(DefaultsManager::class)->set(); |
|
52 | - $this->scheduleCronJob(); |
|
53 | - $this->upgrade(); |
|
54 | - } |
|
46 | + /** |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function activate() |
|
50 | + { |
|
51 | + $this->make(DefaultsManager::class)->set(); |
|
52 | + $this->scheduleCronJob(); |
|
53 | + $this->upgrade(); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param string $view |
|
58 | - * @return string |
|
59 | - */ |
|
60 | - public function build($view, array $data = []) |
|
61 | - { |
|
62 | - ob_start(); |
|
63 | - $this->render($view, $data); |
|
64 | - return ob_get_clean(); |
|
65 | - } |
|
56 | + /** |
|
57 | + * @param string $view |
|
58 | + * @return string |
|
59 | + */ |
|
60 | + public function build($view, array $data = []) |
|
61 | + { |
|
62 | + ob_start(); |
|
63 | + $this->render($view, $data); |
|
64 | + return ob_get_clean(); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @return void |
|
69 | - */ |
|
70 | - public function catchFatalError() |
|
71 | - { |
|
72 | - $error = error_get_last(); |
|
73 | - if (E_ERROR !== $error['type'] || !Str::contains($error['message'], $this->path())) { |
|
74 | - return; |
|
75 | - } |
|
76 | - glsr_log()->error($error['message']); |
|
77 | - } |
|
67 | + /** |
|
68 | + * @return void |
|
69 | + */ |
|
70 | + public function catchFatalError() |
|
71 | + { |
|
72 | + $error = error_get_last(); |
|
73 | + if (E_ERROR !== $error['type'] || !Str::contains($error['message'], $this->path())) { |
|
74 | + return; |
|
75 | + } |
|
76 | + glsr_log()->error($error['message']); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @param string $name |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - public function config($name) |
|
84 | - { |
|
85 | - $configFile = $this->path('config/'.$name.'.php'); |
|
86 | - $config = file_exists($configFile) |
|
87 | - ? include $configFile |
|
88 | - : []; |
|
89 | - return apply_filters('site-reviews/config/'.$name, $config); |
|
90 | - } |
|
79 | + /** |
|
80 | + * @param string $name |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + public function config($name) |
|
84 | + { |
|
85 | + $configFile = $this->path('config/'.$name.'.php'); |
|
86 | + $config = file_exists($configFile) |
|
87 | + ? include $configFile |
|
88 | + : []; |
|
89 | + return apply_filters('site-reviews/config/'.$name, $config); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @param string $property |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - public function constant($property, $className = 'static') |
|
97 | - { |
|
98 | - $constant = $className.'::'.$property; |
|
99 | - return defined($constant) |
|
100 | - ? apply_filters('site-reviews/const/'.$property, constant($constant)) |
|
101 | - : ''; |
|
102 | - } |
|
92 | + /** |
|
93 | + * @param string $property |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + public function constant($property, $className = 'static') |
|
97 | + { |
|
98 | + $constant = $className.'::'.$property; |
|
99 | + return defined($constant) |
|
100 | + ? apply_filters('site-reviews/const/'.$property, constant($constant)) |
|
101 | + : ''; |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return void |
|
106 | - */ |
|
107 | - public function deactivate() |
|
108 | - { |
|
109 | - $this->unscheduleCronJob(); |
|
110 | - } |
|
104 | + /** |
|
105 | + * @return void |
|
106 | + */ |
|
107 | + public function deactivate() |
|
108 | + { |
|
109 | + $this->unscheduleCronJob(); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * @param string $view |
|
114 | - * @return void|string |
|
115 | - */ |
|
116 | - public function file($view) |
|
117 | - { |
|
118 | - $view.= '.php'; |
|
119 | - $filePaths = []; |
|
120 | - if (Str::startsWith('templates/', $view)) { |
|
121 | - $filePaths[] = $this->themePath(Str::removePrefix('templates/', $view)); |
|
122 | - } |
|
123 | - $filePaths[] = $this->path($view); |
|
124 | - $filePaths[] = $this->path('views/'.$view); |
|
125 | - foreach ($filePaths as $file) { |
|
126 | - if (!file_exists($file)) { |
|
127 | - continue; |
|
128 | - } |
|
129 | - return $file; |
|
130 | - } |
|
131 | - } |
|
112 | + /** |
|
113 | + * @param string $view |
|
114 | + * @return void|string |
|
115 | + */ |
|
116 | + public function file($view) |
|
117 | + { |
|
118 | + $view.= '.php'; |
|
119 | + $filePaths = []; |
|
120 | + if (Str::startsWith('templates/', $view)) { |
|
121 | + $filePaths[] = $this->themePath(Str::removePrefix('templates/', $view)); |
|
122 | + } |
|
123 | + $filePaths[] = $this->path($view); |
|
124 | + $filePaths[] = $this->path('views/'.$view); |
|
125 | + foreach ($filePaths as $file) { |
|
126 | + if (!file_exists($file)) { |
|
127 | + continue; |
|
128 | + } |
|
129 | + return $file; |
|
130 | + } |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - public function getDefaults() |
|
137 | - { |
|
138 | - if (empty($this->defaults)) { |
|
139 | - $this->defaults = $this->make(DefaultsManager::class)->get(); |
|
140 | - $this->upgrade(); |
|
141 | - } |
|
142 | - return apply_filters('site-reviews/get/defaults', $this->defaults); |
|
143 | - } |
|
133 | + /** |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + public function getDefaults() |
|
137 | + { |
|
138 | + if (empty($this->defaults)) { |
|
139 | + $this->defaults = $this->make(DefaultsManager::class)->get(); |
|
140 | + $this->upgrade(); |
|
141 | + } |
|
142 | + return apply_filters('site-reviews/get/defaults', $this->defaults); |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * @return string |
|
147 | - */ |
|
148 | - public function getPermission($page = '') |
|
149 | - { |
|
150 | - $permissions = [ |
|
151 | - 'addons' => 'install_plugins', |
|
152 | - 'settings' => 'manage_options', |
|
153 | - // 'welcome' => 'activate_plugins', |
|
154 | - ]; |
|
155 | - return Arr::get($permissions, $page, $this->constant('CAPABILITY')); |
|
156 | - } |
|
145 | + /** |
|
146 | + * @return string |
|
147 | + */ |
|
148 | + public function getPermission($page = '') |
|
149 | + { |
|
150 | + $permissions = [ |
|
151 | + 'addons' => 'install_plugins', |
|
152 | + 'settings' => 'manage_options', |
|
153 | + // 'welcome' => 'activate_plugins', |
|
154 | + ]; |
|
155 | + return Arr::get($permissions, $page, $this->constant('CAPABILITY')); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @return bool |
|
160 | - */ |
|
161 | - public function hasPermission($page = '') |
|
162 | - { |
|
163 | - $isAdmin = $this->isAdmin(); |
|
164 | - return !$isAdmin || ($isAdmin && current_user_can($this->getPermission($page))); |
|
165 | - } |
|
158 | + /** |
|
159 | + * @return bool |
|
160 | + */ |
|
161 | + public function hasPermission($page = '') |
|
162 | + { |
|
163 | + $isAdmin = $this->isAdmin(); |
|
164 | + return !$isAdmin || ($isAdmin && current_user_can($this->getPermission($page))); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @return void |
|
169 | - */ |
|
170 | - public function init() |
|
171 | - { |
|
172 | - $this->make(Actions::class)->run(); |
|
173 | - $this->make(Filters::class)->run(); |
|
174 | - } |
|
167 | + /** |
|
168 | + * @return void |
|
169 | + */ |
|
170 | + public function init() |
|
171 | + { |
|
172 | + $this->make(Actions::class)->run(); |
|
173 | + $this->make(Filters::class)->run(); |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * @return bool |
|
178 | - */ |
|
179 | - public function isAdmin() |
|
180 | - { |
|
181 | - return is_admin() && !wp_doing_ajax(); |
|
182 | - } |
|
176 | + /** |
|
177 | + * @return bool |
|
178 | + */ |
|
179 | + public function isAdmin() |
|
180 | + { |
|
181 | + return is_admin() && !wp_doing_ajax(); |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * @param string $file |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - public function path($file = '', $realpath = true) |
|
189 | - { |
|
190 | - $path = plugin_dir_path($this->file); |
|
191 | - if (!$realpath) { |
|
192 | - $path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
|
193 | - } |
|
194 | - $path = trailingslashit($path).ltrim(trim($file), '/'); |
|
195 | - return apply_filters('site-reviews/path', $path, $file); |
|
196 | - } |
|
184 | + /** |
|
185 | + * @param string $file |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + public function path($file = '', $realpath = true) |
|
189 | + { |
|
190 | + $path = plugin_dir_path($this->file); |
|
191 | + if (!$realpath) { |
|
192 | + $path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
|
193 | + } |
|
194 | + $path = trailingslashit($path).ltrim(trim($file), '/'); |
|
195 | + return apply_filters('site-reviews/path', $path, $file); |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * @param object $addon |
|
200 | - * @return void |
|
201 | - */ |
|
202 | - public function register($addon) |
|
203 | - { |
|
204 | - try { |
|
205 | - $reflection = new \ReflectionClass($addon); |
|
206 | - if ($id = $reflection->getConstant('ID')) { |
|
207 | - $this->addons[] = $id; |
|
208 | - $this->bind($id, $addon); |
|
209 | - $addon->init(); |
|
210 | - } |
|
211 | - } catch(\ReflectionException $e) { |
|
212 | - glsr_log()->error('Attempted to register an invalid addon.'); |
|
213 | - } |
|
214 | - } |
|
198 | + /** |
|
199 | + * @param object $addon |
|
200 | + * @return void |
|
201 | + */ |
|
202 | + public function register($addon) |
|
203 | + { |
|
204 | + try { |
|
205 | + $reflection = new \ReflectionClass($addon); |
|
206 | + if ($id = $reflection->getConstant('ID')) { |
|
207 | + $this->addons[] = $id; |
|
208 | + $this->bind($id, $addon); |
|
209 | + $addon->init(); |
|
210 | + } |
|
211 | + } catch(\ReflectionException $e) { |
|
212 | + glsr_log()->error('Attempted to register an invalid addon.'); |
|
213 | + } |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * @return void |
|
218 | - */ |
|
219 | - public function registerAddons() |
|
220 | - { |
|
221 | - do_action('site-reviews/addon/register', $this); |
|
222 | - } |
|
216 | + /** |
|
217 | + * @return void |
|
218 | + */ |
|
219 | + public function registerAddons() |
|
220 | + { |
|
221 | + do_action('site-reviews/addon/register', $this); |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
225 | - * @return void |
|
226 | - */ |
|
227 | - public function registerLanguages() |
|
228 | - { |
|
229 | - load_plugin_textdomain(static::ID, false, |
|
230 | - trailingslashit(plugin_basename($this->path()).'/'.$this->languages) |
|
231 | - ); |
|
232 | - } |
|
224 | + /** |
|
225 | + * @return void |
|
226 | + */ |
|
227 | + public function registerLanguages() |
|
228 | + { |
|
229 | + load_plugin_textdomain(static::ID, false, |
|
230 | + trailingslashit(plugin_basename($this->path()).'/'.$this->languages) |
|
231 | + ); |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - public function registerReviewTypes() |
|
238 | - { |
|
239 | - $types = apply_filters('site-reviews/addon/types', []); |
|
240 | - $this->reviewTypes = wp_parse_args($types, [ |
|
241 | - 'local' => __('Local', 'site-reviews'), |
|
242 | - ]); |
|
243 | - } |
|
234 | + /** |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + public function registerReviewTypes() |
|
238 | + { |
|
239 | + $types = apply_filters('site-reviews/addon/types', []); |
|
240 | + $this->reviewTypes = wp_parse_args($types, [ |
|
241 | + 'local' => __('Local', 'site-reviews'), |
|
242 | + ]); |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * @param string $view |
|
247 | - * @return void |
|
248 | - */ |
|
249 | - public function render($view, array $data = []) |
|
250 | - { |
|
251 | - $view = apply_filters('site-reviews/render/view', $view, $data); |
|
252 | - $file = apply_filters('site-reviews/views/file', $this->file($view), $view, $data); |
|
253 | - if (!file_exists($file)) { |
|
254 | - glsr_log()->error('File not found: '.$file); |
|
255 | - return; |
|
256 | - } |
|
257 | - $data = apply_filters('site-reviews/views/data', $data, $view); |
|
258 | - extract($data); |
|
259 | - include $file; |
|
260 | - } |
|
245 | + /** |
|
246 | + * @param string $view |
|
247 | + * @return void |
|
248 | + */ |
|
249 | + public function render($view, array $data = []) |
|
250 | + { |
|
251 | + $view = apply_filters('site-reviews/render/view', $view, $data); |
|
252 | + $file = apply_filters('site-reviews/views/file', $this->file($view), $view, $data); |
|
253 | + if (!file_exists($file)) { |
|
254 | + glsr_log()->error('File not found: '.$file); |
|
255 | + return; |
|
256 | + } |
|
257 | + $data = apply_filters('site-reviews/views/data', $data, $view); |
|
258 | + extract($data); |
|
259 | + include $file; |
|
260 | + } |
|
261 | 261 | |
262 | - /** |
|
263 | - * @return void |
|
264 | - */ |
|
265 | - public function scheduleCronJob() |
|
266 | - { |
|
267 | - if (wp_next_scheduled(static::CRON_EVENT)) { |
|
268 | - return; |
|
269 | - } |
|
270 | - wp_schedule_event(time(), 'twicedaily', static::CRON_EVENT); |
|
271 | - } |
|
262 | + /** |
|
263 | + * @return void |
|
264 | + */ |
|
265 | + public function scheduleCronJob() |
|
266 | + { |
|
267 | + if (wp_next_scheduled(static::CRON_EVENT)) { |
|
268 | + return; |
|
269 | + } |
|
270 | + wp_schedule_event(time(), 'twicedaily', static::CRON_EVENT); |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * @param string $file |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - public function themePath($file = '') |
|
278 | - { |
|
279 | - return get_stylesheet_directory().'/'.static::ID.'/'.ltrim(trim($file), '/'); |
|
280 | - } |
|
273 | + /** |
|
274 | + * @param string $file |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + public function themePath($file = '') |
|
278 | + { |
|
279 | + return get_stylesheet_directory().'/'.static::ID.'/'.ltrim(trim($file), '/'); |
|
280 | + } |
|
281 | 281 | |
282 | - /** |
|
283 | - * @return void |
|
284 | - */ |
|
285 | - public function unscheduleCronJob() |
|
286 | - { |
|
287 | - wp_unschedule_event(intval(wp_next_scheduled(static::CRON_EVENT)), static::CRON_EVENT); |
|
288 | - } |
|
282 | + /** |
|
283 | + * @return void |
|
284 | + */ |
|
285 | + public function unscheduleCronJob() |
|
286 | + { |
|
287 | + wp_unschedule_event(intval(wp_next_scheduled(static::CRON_EVENT)), static::CRON_EVENT); |
|
288 | + } |
|
289 | 289 | |
290 | - /** |
|
291 | - * @return void |
|
292 | - */ |
|
293 | - public function upgrade() |
|
294 | - { |
|
295 | - $this->make(Upgrader::class)->run(); |
|
296 | - } |
|
290 | + /** |
|
291 | + * @return void |
|
292 | + */ |
|
293 | + public function upgrade() |
|
294 | + { |
|
295 | + $this->make(Upgrader::class)->run(); |
|
296 | + } |
|
297 | 297 | |
298 | - /** |
|
299 | - * @param mixed $upgrader |
|
300 | - * @return void |
|
301 | - * @action upgrader_process_complete |
|
302 | - */ |
|
303 | - public function upgraded($upgrader, array $data) |
|
304 | - { |
|
305 | - if (array_key_exists('plugins', $data) |
|
306 | - && in_array(plugin_basename($this->file), $data['plugins']) |
|
307 | - && 'update' === $data['action'] |
|
308 | - && 'plugin' === $data['type'] |
|
309 | - ) { |
|
310 | - $this->upgrade(); |
|
311 | - } |
|
312 | - } |
|
298 | + /** |
|
299 | + * @param mixed $upgrader |
|
300 | + * @return void |
|
301 | + * @action upgrader_process_complete |
|
302 | + */ |
|
303 | + public function upgraded($upgrader, array $data) |
|
304 | + { |
|
305 | + if (array_key_exists('plugins', $data) |
|
306 | + && in_array(plugin_basename($this->file), $data['plugins']) |
|
307 | + && 'update' === $data['action'] |
|
308 | + && 'plugin' === $data['type'] |
|
309 | + ) { |
|
310 | + $this->upgrade(); |
|
311 | + } |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * @param string $path |
|
316 | - * @return string |
|
317 | - */ |
|
318 | - public function url($path = '') |
|
319 | - { |
|
320 | - $url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
|
321 | - return apply_filters('site-reviews/url', $url, $path); |
|
322 | - } |
|
314 | + /** |
|
315 | + * @param string $path |
|
316 | + * @return string |
|
317 | + */ |
|
318 | + public function url($path = '') |
|
319 | + { |
|
320 | + $url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
|
321 | + return apply_filters('site-reviews/url', $url, $path); |
|
322 | + } |
|
323 | 323 | |
324 | - /** |
|
325 | - * @param string $versionLevel |
|
326 | - * @return string |
|
327 | - */ |
|
328 | - public function version($versionLevel = '') |
|
329 | - { |
|
330 | - $pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i'; |
|
331 | - preg_match($pattern, $this->version, $matches); |
|
332 | - switch ($versionLevel) { |
|
333 | - case 'major': |
|
334 | - $version = Arr::get($matches, 1); |
|
335 | - break; |
|
336 | - case 'minor': |
|
337 | - $version = Arr::get($matches, 1).Arr::get($matches, 2); |
|
338 | - break; |
|
339 | - case 'patch': |
|
340 | - $version = Arr::get($matches, 1).Arr::get($matches, 2).Arr::get($matches, 3); |
|
341 | - break; |
|
342 | - } |
|
343 | - return empty($version) |
|
344 | - ? $this->version |
|
345 | - : $version; |
|
346 | - } |
|
324 | + /** |
|
325 | + * @param string $versionLevel |
|
326 | + * @return string |
|
327 | + */ |
|
328 | + public function version($versionLevel = '') |
|
329 | + { |
|
330 | + $pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i'; |
|
331 | + preg_match($pattern, $this->version, $matches); |
|
332 | + switch ($versionLevel) { |
|
333 | + case 'major': |
|
334 | + $version = Arr::get($matches, 1); |
|
335 | + break; |
|
336 | + case 'minor': |
|
337 | + $version = Arr::get($matches, 1).Arr::get($matches, 2); |
|
338 | + break; |
|
339 | + case 'patch': |
|
340 | + $version = Arr::get($matches, 1).Arr::get($matches, 2).Arr::get($matches, 3); |
|
341 | + break; |
|
342 | + } |
|
343 | + return empty($version) |
|
344 | + ? $this->version |
|
345 | + : $version; |
|
346 | + } |
|
347 | 347 | } |
@@ -11,227 +11,227 @@ |
||
11 | 11 | |
12 | 12 | abstract class Shortcode implements ShortcodeContract |
13 | 13 | { |
14 | - /** |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $partialName; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $shortcodeName; |
|
23 | - |
|
24 | - public function __construct() |
|
25 | - { |
|
26 | - $this->partialName = $this->getShortcodePartialName(); |
|
27 | - $this->shortcodeName = $this->getShortcodeName(); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * @param string|array $atts |
|
32 | - * @param string $type |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function build($atts, array $args = [], $type = 'shortcode') |
|
36 | - { |
|
37 | - $args = $this->normalizeArgs($args, $type); |
|
38 | - $atts = $this->normalizeAtts($atts, $type); |
|
39 | - $partial = glsr(Partial::class)->build($this->partialName, $atts); |
|
40 | - $title = !empty($atts['title']) |
|
41 | - ? $args['before_title'].$atts['title'].$args['after_title'] |
|
42 | - : ''; |
|
43 | - $debug = sprintf('<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json']); |
|
44 | - return $args['before_widget'].$title.$partial.$debug.$args['after_widget']; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @param string|array $atts |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function buildShortcode($atts = []) |
|
52 | - { |
|
53 | - return $this->build($atts); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @return array |
|
58 | - */ |
|
59 | - public function getDefaults($atts) |
|
60 | - { |
|
61 | - return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts)); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public function getHideOptions() |
|
68 | - { |
|
69 | - $options = $this->hideOptions(); |
|
70 | - return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - public function getShortcodeClassName($replace = '', $search = 'Shortcode') |
|
77 | - { |
|
78 | - return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @return string |
|
83 | - */ |
|
84 | - public function getShortcodeDefaultsClassName() |
|
85 | - { |
|
86 | - return Helper::buildClassName( |
|
87 | - $this->getShortcodeClassName('Defaults'), |
|
88 | - 'Defaults' |
|
89 | - ); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - public function getShortcodeName() |
|
96 | - { |
|
97 | - return Str::snakeCase($this->getShortcodeClassName()); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function getShortcodePartialName() |
|
104 | - { |
|
105 | - return Str::dashCase($this->getShortcodeClassName()); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @param array|string $args |
|
110 | - * @param string $type |
|
111 | - * @return array |
|
112 | - */ |
|
113 | - public function normalizeArgs($args, $type = 'shortcode') |
|
114 | - { |
|
115 | - $args = wp_parse_args($args, [ |
|
116 | - 'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">', |
|
117 | - 'after_widget' => '</div>', |
|
118 | - 'before_title' => '<h3 class="glsr-'.$type.'-title">', |
|
119 | - 'after_title' => '</h3>', |
|
120 | - ]); |
|
121 | - return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param array|string $atts |
|
126 | - * @param string $type |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - public function normalizeAtts($atts, $type = 'shortcode') |
|
130 | - { |
|
131 | - $atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName); |
|
132 | - $atts = $this->getDefaults($atts); |
|
133 | - array_walk($atts, function (&$value, $key) { |
|
134 | - $methodName = Helper::buildMethodName($key, 'normalize'); |
|
135 | - if (!method_exists($this, $methodName)) { |
|
136 | - return; |
|
137 | - } |
|
138 | - $value = $this->$methodName($value); |
|
139 | - }); |
|
140 | - return $atts; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @return array |
|
145 | - */ |
|
146 | - abstract protected function hideOptions(); |
|
147 | - |
|
148 | - /** |
|
149 | - * @param string $postId |
|
150 | - * @return int|string |
|
151 | - */ |
|
152 | - protected function normalizeAssignedTo($postId) |
|
153 | - { |
|
154 | - if ('parent_id' == $postId) { |
|
155 | - $postId = intval(wp_get_post_parent_id(intval(get_the_ID()))); |
|
156 | - } elseif ('post_id' == $postId) { |
|
157 | - $postId = intval(get_the_ID()); |
|
158 | - } |
|
159 | - return $postId; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param string $postId |
|
164 | - * @return int|string |
|
165 | - */ |
|
166 | - protected function normalizeAssignTo($postId) |
|
167 | - { |
|
168 | - return $this->normalizeAssignedTo($postId); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @param string|array $hide |
|
173 | - * @return array |
|
174 | - */ |
|
175 | - protected function normalizeHide($hide) |
|
176 | - { |
|
177 | - if (is_string($hide)) { |
|
178 | - $hide = explode(',', $hide); |
|
179 | - } |
|
180 | - $hideKeys = array_keys($this->getHideOptions()); |
|
181 | - return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) { |
|
182 | - return in_array($value, $hideKeys); |
|
183 | - }); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @param string $id |
|
188 | - * @return string |
|
189 | - */ |
|
190 | - protected function normalizeId($id) |
|
191 | - { |
|
192 | - return sanitize_title($id); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @param string $labels |
|
197 | - * @return array |
|
198 | - */ |
|
199 | - protected function normalizeLabels($labels) |
|
200 | - { |
|
201 | - $defaults = [ |
|
202 | - __('Excellent', 'site-reviews'), |
|
203 | - __('Very good', 'site-reviews'), |
|
204 | - __('Average', 'site-reviews'), |
|
205 | - __('Poor', 'site-reviews'), |
|
206 | - __('Terrible', 'site-reviews'), |
|
207 | - ]; |
|
208 | - $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class); |
|
209 | - $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
|
210 | - $labels = array_map('trim', explode(',', $labels)); |
|
211 | - foreach ($defaults as $i => $label) { |
|
212 | - if (empty($labels[$i])) { |
|
213 | - continue; |
|
214 | - } |
|
215 | - $defaults[$i] = $labels[$i]; |
|
216 | - } |
|
217 | - return array_combine(range($maxRating, 1), $defaults); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @param string $schema |
|
222 | - * @return bool |
|
223 | - */ |
|
224 | - protected function normalizeSchema($schema) |
|
225 | - { |
|
226 | - return wp_validate_boolean($schema); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * @param string $text |
|
231 | - * @return string |
|
232 | - */ |
|
233 | - protected function normalizeText($text) |
|
234 | - { |
|
235 | - return trim($text); |
|
236 | - } |
|
14 | + /** |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $partialName; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $shortcodeName; |
|
23 | + |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + $this->partialName = $this->getShortcodePartialName(); |
|
27 | + $this->shortcodeName = $this->getShortcodeName(); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * @param string|array $atts |
|
32 | + * @param string $type |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function build($atts, array $args = [], $type = 'shortcode') |
|
36 | + { |
|
37 | + $args = $this->normalizeArgs($args, $type); |
|
38 | + $atts = $this->normalizeAtts($atts, $type); |
|
39 | + $partial = glsr(Partial::class)->build($this->partialName, $atts); |
|
40 | + $title = !empty($atts['title']) |
|
41 | + ? $args['before_title'].$atts['title'].$args['after_title'] |
|
42 | + : ''; |
|
43 | + $debug = sprintf('<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json']); |
|
44 | + return $args['before_widget'].$title.$partial.$debug.$args['after_widget']; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @param string|array $atts |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function buildShortcode($atts = []) |
|
52 | + { |
|
53 | + return $this->build($atts); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @return array |
|
58 | + */ |
|
59 | + public function getDefaults($atts) |
|
60 | + { |
|
61 | + return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts)); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public function getHideOptions() |
|
68 | + { |
|
69 | + $options = $this->hideOptions(); |
|
70 | + return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + public function getShortcodeClassName($replace = '', $search = 'Shortcode') |
|
77 | + { |
|
78 | + return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @return string |
|
83 | + */ |
|
84 | + public function getShortcodeDefaultsClassName() |
|
85 | + { |
|
86 | + return Helper::buildClassName( |
|
87 | + $this->getShortcodeClassName('Defaults'), |
|
88 | + 'Defaults' |
|
89 | + ); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + public function getShortcodeName() |
|
96 | + { |
|
97 | + return Str::snakeCase($this->getShortcodeClassName()); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function getShortcodePartialName() |
|
104 | + { |
|
105 | + return Str::dashCase($this->getShortcodeClassName()); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @param array|string $args |
|
110 | + * @param string $type |
|
111 | + * @return array |
|
112 | + */ |
|
113 | + public function normalizeArgs($args, $type = 'shortcode') |
|
114 | + { |
|
115 | + $args = wp_parse_args($args, [ |
|
116 | + 'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">', |
|
117 | + 'after_widget' => '</div>', |
|
118 | + 'before_title' => '<h3 class="glsr-'.$type.'-title">', |
|
119 | + 'after_title' => '</h3>', |
|
120 | + ]); |
|
121 | + return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param array|string $atts |
|
126 | + * @param string $type |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + public function normalizeAtts($atts, $type = 'shortcode') |
|
130 | + { |
|
131 | + $atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName); |
|
132 | + $atts = $this->getDefaults($atts); |
|
133 | + array_walk($atts, function (&$value, $key) { |
|
134 | + $methodName = Helper::buildMethodName($key, 'normalize'); |
|
135 | + if (!method_exists($this, $methodName)) { |
|
136 | + return; |
|
137 | + } |
|
138 | + $value = $this->$methodName($value); |
|
139 | + }); |
|
140 | + return $atts; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @return array |
|
145 | + */ |
|
146 | + abstract protected function hideOptions(); |
|
147 | + |
|
148 | + /** |
|
149 | + * @param string $postId |
|
150 | + * @return int|string |
|
151 | + */ |
|
152 | + protected function normalizeAssignedTo($postId) |
|
153 | + { |
|
154 | + if ('parent_id' == $postId) { |
|
155 | + $postId = intval(wp_get_post_parent_id(intval(get_the_ID()))); |
|
156 | + } elseif ('post_id' == $postId) { |
|
157 | + $postId = intval(get_the_ID()); |
|
158 | + } |
|
159 | + return $postId; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param string $postId |
|
164 | + * @return int|string |
|
165 | + */ |
|
166 | + protected function normalizeAssignTo($postId) |
|
167 | + { |
|
168 | + return $this->normalizeAssignedTo($postId); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @param string|array $hide |
|
173 | + * @return array |
|
174 | + */ |
|
175 | + protected function normalizeHide($hide) |
|
176 | + { |
|
177 | + if (is_string($hide)) { |
|
178 | + $hide = explode(',', $hide); |
|
179 | + } |
|
180 | + $hideKeys = array_keys($this->getHideOptions()); |
|
181 | + return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) { |
|
182 | + return in_array($value, $hideKeys); |
|
183 | + }); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @param string $id |
|
188 | + * @return string |
|
189 | + */ |
|
190 | + protected function normalizeId($id) |
|
191 | + { |
|
192 | + return sanitize_title($id); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @param string $labels |
|
197 | + * @return array |
|
198 | + */ |
|
199 | + protected function normalizeLabels($labels) |
|
200 | + { |
|
201 | + $defaults = [ |
|
202 | + __('Excellent', 'site-reviews'), |
|
203 | + __('Very good', 'site-reviews'), |
|
204 | + __('Average', 'site-reviews'), |
|
205 | + __('Poor', 'site-reviews'), |
|
206 | + __('Terrible', 'site-reviews'), |
|
207 | + ]; |
|
208 | + $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class); |
|
209 | + $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
|
210 | + $labels = array_map('trim', explode(',', $labels)); |
|
211 | + foreach ($defaults as $i => $label) { |
|
212 | + if (empty($labels[$i])) { |
|
213 | + continue; |
|
214 | + } |
|
215 | + $defaults[$i] = $labels[$i]; |
|
216 | + } |
|
217 | + return array_combine(range($maxRating, 1), $defaults); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @param string $schema |
|
222 | + * @return bool |
|
223 | + */ |
|
224 | + protected function normalizeSchema($schema) |
|
225 | + { |
|
226 | + return wp_validate_boolean($schema); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * @param string $text |
|
231 | + * @return string |
|
232 | + */ |
|
233 | + protected function normalizeText($text) |
|
234 | + { |
|
235 | + return trim($text); |
|
236 | + } |
|
237 | 237 | } |
@@ -21,384 +21,384 @@ |
||
21 | 21 | |
22 | 22 | class SiteReviews |
23 | 23 | { |
24 | - /** |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - public $args; |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + public $args; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var Review |
|
31 | - */ |
|
32 | - public $current; |
|
29 | + /** |
|
30 | + * @var Review |
|
31 | + */ |
|
32 | + public $current; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - public $options; |
|
34 | + /** |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + public $options; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var Reviews |
|
41 | - */ |
|
42 | - protected $reviews; |
|
39 | + /** |
|
40 | + * @var Reviews |
|
41 | + */ |
|
42 | + protected $reviews; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param Reviews|null $reviews |
|
46 | - * @return ReviewsHtml |
|
47 | - */ |
|
48 | - public function build(array $args = [], $reviews = null) |
|
49 | - { |
|
50 | - $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
51 | - $this->options = Arr::flattenArray(glsr(OptionManager::class)->all()); |
|
52 | - $this->reviews = $reviews instanceof Reviews |
|
53 | - ? $reviews |
|
54 | - : glsr(ReviewManager::class)->get($this->args); |
|
55 | - $this->generateSchema(); |
|
56 | - return $this->buildReviews(); |
|
57 | - } |
|
44 | + /** |
|
45 | + * @param Reviews|null $reviews |
|
46 | + * @return ReviewsHtml |
|
47 | + */ |
|
48 | + public function build(array $args = [], $reviews = null) |
|
49 | + { |
|
50 | + $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
51 | + $this->options = Arr::flattenArray(glsr(OptionManager::class)->all()); |
|
52 | + $this->reviews = $reviews instanceof Reviews |
|
53 | + ? $reviews |
|
54 | + : glsr(ReviewManager::class)->get($this->args); |
|
55 | + $this->generateSchema(); |
|
56 | + return $this->buildReviews(); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @return ReviewHtml |
|
61 | - */ |
|
62 | - public function buildReview(Review $review) |
|
63 | - { |
|
64 | - $review = apply_filters('site-reviews/review/build/before', $review); |
|
65 | - $this->current = $review; |
|
66 | - $renderedFields = []; |
|
67 | - foreach ($review as $key => $value) { |
|
68 | - $method = Helper::buildMethodName($key, 'buildOption'); |
|
69 | - $field = method_exists($this, $method) |
|
70 | - ? $this->$method($key, $value) |
|
71 | - : false; |
|
72 | - $field = apply_filters('site-reviews/review/build/'.$key, $field, $value, $review, $this); |
|
73 | - if (false === $field) { |
|
74 | - continue; |
|
75 | - } |
|
76 | - $renderedFields[$key] = $field; |
|
77 | - } |
|
78 | - $this->wrap($renderedFields, $review); |
|
79 | - $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review, $this); |
|
80 | - $this->current = null; |
|
81 | - return new ReviewHtml($review, (array) $renderedFields); |
|
82 | - } |
|
59 | + /** |
|
60 | + * @return ReviewHtml |
|
61 | + */ |
|
62 | + public function buildReview(Review $review) |
|
63 | + { |
|
64 | + $review = apply_filters('site-reviews/review/build/before', $review); |
|
65 | + $this->current = $review; |
|
66 | + $renderedFields = []; |
|
67 | + foreach ($review as $key => $value) { |
|
68 | + $method = Helper::buildMethodName($key, 'buildOption'); |
|
69 | + $field = method_exists($this, $method) |
|
70 | + ? $this->$method($key, $value) |
|
71 | + : false; |
|
72 | + $field = apply_filters('site-reviews/review/build/'.$key, $field, $value, $review, $this); |
|
73 | + if (false === $field) { |
|
74 | + continue; |
|
75 | + } |
|
76 | + $renderedFields[$key] = $field; |
|
77 | + } |
|
78 | + $this->wrap($renderedFields, $review); |
|
79 | + $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review, $this); |
|
80 | + $this->current = null; |
|
81 | + return new ReviewHtml($review, (array) $renderedFields); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @return ReviewsHtml |
|
86 | - */ |
|
87 | - public function buildReviews() |
|
88 | - { |
|
89 | - $renderedReviews = []; |
|
90 | - foreach ($this->reviews as $index => $review) { |
|
91 | - $renderedReviews[] = $this->buildReview($review); |
|
92 | - } |
|
93 | - return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
94 | - } |
|
84 | + /** |
|
85 | + * @return ReviewsHtml |
|
86 | + */ |
|
87 | + public function buildReviews() |
|
88 | + { |
|
89 | + $renderedReviews = []; |
|
90 | + foreach ($this->reviews as $index => $review) { |
|
91 | + $renderedReviews[] = $this->buildReview($review); |
|
92 | + } |
|
93 | + return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - public function generateSchema() |
|
100 | - { |
|
101 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
102 | - return; |
|
103 | - } |
|
104 | - glsr(Schema::class)->store( |
|
105 | - glsr(Schema::class)->build($this->args) |
|
106 | - ); |
|
107 | - } |
|
96 | + /** |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + public function generateSchema() |
|
100 | + { |
|
101 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
102 | + return; |
|
103 | + } |
|
104 | + glsr(Schema::class)->store( |
|
105 | + glsr(Schema::class)->build($this->args) |
|
106 | + ); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @param string $text |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function getExcerpt($text) |
|
114 | - { |
|
115 | - $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
116 | - $split = extension_loaded('intl') |
|
117 | - ? $this->getExcerptIntlSplit($text, $limit) |
|
118 | - : $this->getExcerptSplit($text, $limit); |
|
119 | - $hiddenText = substr($text, $split); |
|
120 | - if (!empty($hiddenText)) { |
|
121 | - $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
122 | - 'class' => 'glsr-hidden glsr-hidden-text', |
|
123 | - 'data-show-less' => __('Show less', 'site-reviews'), |
|
124 | - 'data-show-more' => __('Show more', 'site-reviews'), |
|
125 | - ]); |
|
126 | - $text = ltrim(substr($text, 0, $split)).$showMore; |
|
127 | - } |
|
128 | - return $text; |
|
129 | - } |
|
109 | + /** |
|
110 | + * @param string $text |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function getExcerpt($text) |
|
114 | + { |
|
115 | + $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
116 | + $split = extension_loaded('intl') |
|
117 | + ? $this->getExcerptIntlSplit($text, $limit) |
|
118 | + : $this->getExcerptSplit($text, $limit); |
|
119 | + $hiddenText = substr($text, $split); |
|
120 | + if (!empty($hiddenText)) { |
|
121 | + $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
122 | + 'class' => 'glsr-hidden glsr-hidden-text', |
|
123 | + 'data-show-less' => __('Show less', 'site-reviews'), |
|
124 | + 'data-show-more' => __('Show more', 'site-reviews'), |
|
125 | + ]); |
|
126 | + $text = ltrim(substr($text, 0, $split)).$showMore; |
|
127 | + } |
|
128 | + return $text; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @param string $key |
|
133 | - * @param string $path |
|
134 | - * @return bool |
|
135 | - */ |
|
136 | - public function isHidden($key, $path = '') |
|
137 | - { |
|
138 | - $isOptionEnabled = !empty($path) |
|
139 | - ? $this->isOptionEnabled($path) |
|
140 | - : true; |
|
141 | - return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
142 | - } |
|
131 | + /** |
|
132 | + * @param string $key |
|
133 | + * @param string $path |
|
134 | + * @return bool |
|
135 | + */ |
|
136 | + public function isHidden($key, $path = '') |
|
137 | + { |
|
138 | + $isOptionEnabled = !empty($path) |
|
139 | + ? $this->isOptionEnabled($path) |
|
140 | + : true; |
|
141 | + return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * @param string $key |
|
146 | - * @param string $value |
|
147 | - * @return bool |
|
148 | - */ |
|
149 | - public function isHiddenOrEmpty($key, $value) |
|
150 | - { |
|
151 | - return $this->isHidden($key) || empty($value); |
|
152 | - } |
|
144 | + /** |
|
145 | + * @param string $key |
|
146 | + * @param string $value |
|
147 | + * @return bool |
|
148 | + */ |
|
149 | + public function isHiddenOrEmpty($key, $value) |
|
150 | + { |
|
151 | + return $this->isHidden($key) || empty($value); |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * @param string $text |
|
156 | - * @return string |
|
157 | - */ |
|
158 | - public function normalizeText($text) |
|
159 | - { |
|
160 | - $text = wp_kses($text, wp_kses_allowed_html()); |
|
161 | - $text = convert_smilies(strip_shortcodes($text)); |
|
162 | - $text = str_replace(']]>', ']]>', $text); |
|
163 | - $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
164 | - if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
165 | - $text = $this->getExcerpt($text); |
|
166 | - } |
|
167 | - return wptexturize(nl2br($text)); |
|
168 | - } |
|
154 | + /** |
|
155 | + * @param string $text |
|
156 | + * @return string |
|
157 | + */ |
|
158 | + public function normalizeText($text) |
|
159 | + { |
|
160 | + $text = wp_kses($text, wp_kses_allowed_html()); |
|
161 | + $text = convert_smilies(strip_shortcodes($text)); |
|
162 | + $text = str_replace(']]>', ']]>', $text); |
|
163 | + $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
164 | + if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
165 | + $text = $this->getExcerpt($text); |
|
166 | + } |
|
167 | + return wptexturize(nl2br($text)); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * @param string $key |
|
172 | - * @param string $value |
|
173 | - * @return void|string |
|
174 | - */ |
|
175 | - protected function buildOptionAssignedTo($key, $value) |
|
176 | - { |
|
177 | - if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
178 | - return; |
|
179 | - } |
|
180 | - $post = get_post(glsr(Multilingual::class)->getPostId($value)); |
|
181 | - if (empty($post->ID)) { |
|
182 | - return; |
|
183 | - } |
|
184 | - $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
185 | - 'href' => get_the_permalink($post->ID), |
|
186 | - ]); |
|
187 | - $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
188 | - return '<span>'.$assignedTo.'</span>'; |
|
189 | - } |
|
170 | + /** |
|
171 | + * @param string $key |
|
172 | + * @param string $value |
|
173 | + * @return void|string |
|
174 | + */ |
|
175 | + protected function buildOptionAssignedTo($key, $value) |
|
176 | + { |
|
177 | + if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
178 | + return; |
|
179 | + } |
|
180 | + $post = get_post(glsr(Multilingual::class)->getPostId($value)); |
|
181 | + if (empty($post->ID)) { |
|
182 | + return; |
|
183 | + } |
|
184 | + $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
185 | + 'href' => get_the_permalink($post->ID), |
|
186 | + ]); |
|
187 | + $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
188 | + return '<span>'.$assignedTo.'</span>'; |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * @param string $key |
|
193 | - * @param string $value |
|
194 | - * @return void|string |
|
195 | - */ |
|
196 | - protected function buildOptionAuthor($key, $value) |
|
197 | - { |
|
198 | - if (!$this->isHidden($key)) { |
|
199 | - $name = Str::convertName( |
|
200 | - $value, |
|
201 | - glsr_get_option('reviews.name.format'), |
|
202 | - glsr_get_option('reviews.name.initial') |
|
203 | - ); |
|
204 | - return '<span>'.$name.'</span>'; |
|
205 | - } |
|
206 | - } |
|
191 | + /** |
|
192 | + * @param string $key |
|
193 | + * @param string $value |
|
194 | + * @return void|string |
|
195 | + */ |
|
196 | + protected function buildOptionAuthor($key, $value) |
|
197 | + { |
|
198 | + if (!$this->isHidden($key)) { |
|
199 | + $name = Str::convertName( |
|
200 | + $value, |
|
201 | + glsr_get_option('reviews.name.format'), |
|
202 | + glsr_get_option('reviews.name.initial') |
|
203 | + ); |
|
204 | + return '<span>'.$name.'</span>'; |
|
205 | + } |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * @param string $key |
|
210 | - * @param string $value |
|
211 | - * @return void|string |
|
212 | - */ |
|
213 | - protected function buildOptionAvatar($key, $value) |
|
214 | - { |
|
215 | - if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
216 | - return; |
|
217 | - } |
|
218 | - $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
219 | - return glsr(Builder::class)->img([ |
|
220 | - 'height' => $size, |
|
221 | - 'src' => $this->generateAvatar($value), |
|
222 | - 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
223 | - 'width' => $size, |
|
224 | - ]); |
|
225 | - } |
|
208 | + /** |
|
209 | + * @param string $key |
|
210 | + * @param string $value |
|
211 | + * @return void|string |
|
212 | + */ |
|
213 | + protected function buildOptionAvatar($key, $value) |
|
214 | + { |
|
215 | + if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
216 | + return; |
|
217 | + } |
|
218 | + $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
219 | + return glsr(Builder::class)->img([ |
|
220 | + 'height' => $size, |
|
221 | + 'src' => $this->generateAvatar($value), |
|
222 | + 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
223 | + 'width' => $size, |
|
224 | + ]); |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * @param string $key |
|
229 | - * @param string $value |
|
230 | - * @return void|string |
|
231 | - */ |
|
232 | - protected function buildOptionContent($key, $value) |
|
233 | - { |
|
234 | - $text = $this->normalizeText($value); |
|
235 | - if (!$this->isHiddenOrEmpty($key, $text)) { |
|
236 | - return '<p>'.$text.'</p>'; |
|
237 | - } |
|
238 | - } |
|
227 | + /** |
|
228 | + * @param string $key |
|
229 | + * @param string $value |
|
230 | + * @return void|string |
|
231 | + */ |
|
232 | + protected function buildOptionContent($key, $value) |
|
233 | + { |
|
234 | + $text = $this->normalizeText($value); |
|
235 | + if (!$this->isHiddenOrEmpty($key, $text)) { |
|
236 | + return '<p>'.$text.'</p>'; |
|
237 | + } |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * @param string $key |
|
242 | - * @param string $value |
|
243 | - * @return void|string |
|
244 | - */ |
|
245 | - protected function buildOptionDate($key, $value) |
|
246 | - { |
|
247 | - if ($this->isHidden($key)) { |
|
248 | - return; |
|
249 | - } |
|
250 | - $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
251 | - if ('relative' == $dateFormat) { |
|
252 | - $date = glsr(Date::class)->relative($value); |
|
253 | - } else { |
|
254 | - $format = 'custom' == $dateFormat |
|
255 | - ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
256 | - : glsr(OptionManager::class)->getWP('date_format', 'F j, Y'); |
|
257 | - $date = date_i18n($format, strtotime($value)); |
|
258 | - } |
|
259 | - return '<span>'.$date.'</span>'; |
|
260 | - } |
|
240 | + /** |
|
241 | + * @param string $key |
|
242 | + * @param string $value |
|
243 | + * @return void|string |
|
244 | + */ |
|
245 | + protected function buildOptionDate($key, $value) |
|
246 | + { |
|
247 | + if ($this->isHidden($key)) { |
|
248 | + return; |
|
249 | + } |
|
250 | + $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
251 | + if ('relative' == $dateFormat) { |
|
252 | + $date = glsr(Date::class)->relative($value); |
|
253 | + } else { |
|
254 | + $format = 'custom' == $dateFormat |
|
255 | + ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
256 | + : glsr(OptionManager::class)->getWP('date_format', 'F j, Y'); |
|
257 | + $date = date_i18n($format, strtotime($value)); |
|
258 | + } |
|
259 | + return '<span>'.$date.'</span>'; |
|
260 | + } |
|
261 | 261 | |
262 | - /** |
|
263 | - * @param string $key |
|
264 | - * @param string $value |
|
265 | - * @return void|string |
|
266 | - */ |
|
267 | - protected function buildOptionRating($key, $value) |
|
268 | - { |
|
269 | - if (!$this->isHiddenOrEmpty($key, $value)) { |
|
270 | - return glsr_star_rating($value); |
|
271 | - } |
|
272 | - } |
|
262 | + /** |
|
263 | + * @param string $key |
|
264 | + * @param string $value |
|
265 | + * @return void|string |
|
266 | + */ |
|
267 | + protected function buildOptionRating($key, $value) |
|
268 | + { |
|
269 | + if (!$this->isHiddenOrEmpty($key, $value)) { |
|
270 | + return glsr_star_rating($value); |
|
271 | + } |
|
272 | + } |
|
273 | 273 | |
274 | - /** |
|
275 | - * @param string $key |
|
276 | - * @param string $value |
|
277 | - * @return void|string |
|
278 | - */ |
|
279 | - protected function buildOptionResponse($key, $value) |
|
280 | - { |
|
281 | - if ($this->isHiddenOrEmpty($key, $value)) { |
|
282 | - return; |
|
283 | - } |
|
284 | - $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
285 | - $text = $this->normalizeText($value); |
|
286 | - $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
287 | - $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
288 | - $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
289 | - return $response.$background; |
|
290 | - } |
|
274 | + /** |
|
275 | + * @param string $key |
|
276 | + * @param string $value |
|
277 | + * @return void|string |
|
278 | + */ |
|
279 | + protected function buildOptionResponse($key, $value) |
|
280 | + { |
|
281 | + if ($this->isHiddenOrEmpty($key, $value)) { |
|
282 | + return; |
|
283 | + } |
|
284 | + $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
285 | + $text = $this->normalizeText($value); |
|
286 | + $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
287 | + $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
288 | + $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
289 | + return $response.$background; |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * @param string $key |
|
294 | - * @param string $value |
|
295 | - * @return void|string |
|
296 | - */ |
|
297 | - protected function buildOptionTitle($key, $value) |
|
298 | - { |
|
299 | - if ($this->isHidden($key)) { |
|
300 | - return; |
|
301 | - } |
|
302 | - if (empty($value)) { |
|
303 | - $value = __('No Title', 'site-reviews'); |
|
304 | - } |
|
305 | - return '<h3>'.$value.'</h3>'; |
|
306 | - } |
|
292 | + /** |
|
293 | + * @param string $key |
|
294 | + * @param string $value |
|
295 | + * @return void|string |
|
296 | + */ |
|
297 | + protected function buildOptionTitle($key, $value) |
|
298 | + { |
|
299 | + if ($this->isHidden($key)) { |
|
300 | + return; |
|
301 | + } |
|
302 | + if (empty($value)) { |
|
303 | + $value = __('No Title', 'site-reviews'); |
|
304 | + } |
|
305 | + return '<h3>'.$value.'</h3>'; |
|
306 | + } |
|
307 | 307 | |
308 | - /** |
|
309 | - * @param string $avatarUrl |
|
310 | - * @return string |
|
311 | - */ |
|
312 | - protected function generateAvatar($avatarUrl) |
|
313 | - { |
|
314 | - if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
315 | - return $avatarUrl; |
|
316 | - } |
|
317 | - if ($this->current->user_id) { |
|
318 | - $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
319 | - } |
|
320 | - if (empty($authorIdOrEmail)) { |
|
321 | - $authorIdOrEmail = $this->current->email; |
|
322 | - } |
|
323 | - if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
324 | - return $newAvatar; |
|
325 | - } |
|
326 | - return $avatarUrl; |
|
327 | - } |
|
308 | + /** |
|
309 | + * @param string $avatarUrl |
|
310 | + * @return string |
|
311 | + */ |
|
312 | + protected function generateAvatar($avatarUrl) |
|
313 | + { |
|
314 | + if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
315 | + return $avatarUrl; |
|
316 | + } |
|
317 | + if ($this->current->user_id) { |
|
318 | + $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
319 | + } |
|
320 | + if (empty($authorIdOrEmail)) { |
|
321 | + $authorIdOrEmail = $this->current->email; |
|
322 | + } |
|
323 | + if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
324 | + return $newAvatar; |
|
325 | + } |
|
326 | + return $avatarUrl; |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * @param string $text |
|
331 | - * @param int $limit |
|
332 | - * @return int |
|
333 | - */ |
|
334 | - protected function getExcerptIntlSplit($text, $limit) |
|
335 | - { |
|
336 | - $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
337 | - $words->setText($text); |
|
338 | - $count = 0; |
|
339 | - foreach ($words as $offset) { |
|
340 | - if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
341 | - continue; |
|
342 | - } |
|
343 | - ++$count; |
|
344 | - if ($count != $limit) { |
|
345 | - continue; |
|
346 | - } |
|
347 | - return $offset; |
|
348 | - } |
|
349 | - return strlen($text); |
|
350 | - } |
|
329 | + /** |
|
330 | + * @param string $text |
|
331 | + * @param int $limit |
|
332 | + * @return int |
|
333 | + */ |
|
334 | + protected function getExcerptIntlSplit($text, $limit) |
|
335 | + { |
|
336 | + $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
337 | + $words->setText($text); |
|
338 | + $count = 0; |
|
339 | + foreach ($words as $offset) { |
|
340 | + if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
341 | + continue; |
|
342 | + } |
|
343 | + ++$count; |
|
344 | + if ($count != $limit) { |
|
345 | + continue; |
|
346 | + } |
|
347 | + return $offset; |
|
348 | + } |
|
349 | + return strlen($text); |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * @param string $text |
|
354 | - * @param int $limit |
|
355 | - * @return int |
|
356 | - */ |
|
357 | - protected function getExcerptSplit($text, $limit) |
|
358 | - { |
|
359 | - if (str_word_count($text, 0) > $limit) { |
|
360 | - $words = array_keys(str_word_count($text, 2)); |
|
361 | - return $words[$limit]; |
|
362 | - } |
|
363 | - return strlen($text); |
|
364 | - } |
|
352 | + /** |
|
353 | + * @param string $text |
|
354 | + * @param int $limit |
|
355 | + * @return int |
|
356 | + */ |
|
357 | + protected function getExcerptSplit($text, $limit) |
|
358 | + { |
|
359 | + if (str_word_count($text, 0) > $limit) { |
|
360 | + $words = array_keys(str_word_count($text, 2)); |
|
361 | + return $words[$limit]; |
|
362 | + } |
|
363 | + return strlen($text); |
|
364 | + } |
|
365 | 365 | |
366 | - /** |
|
367 | - * @param string $path |
|
368 | - * @param mixed $fallback |
|
369 | - * @return mixed |
|
370 | - */ |
|
371 | - protected function getOption($path, $fallback = '') |
|
372 | - { |
|
373 | - if (array_key_exists($path, $this->options)) { |
|
374 | - return $this->options[$path]; |
|
375 | - } |
|
376 | - return $fallback; |
|
377 | - } |
|
366 | + /** |
|
367 | + * @param string $path |
|
368 | + * @param mixed $fallback |
|
369 | + * @return mixed |
|
370 | + */ |
|
371 | + protected function getOption($path, $fallback = '') |
|
372 | + { |
|
373 | + if (array_key_exists($path, $this->options)) { |
|
374 | + return $this->options[$path]; |
|
375 | + } |
|
376 | + return $fallback; |
|
377 | + } |
|
378 | 378 | |
379 | - /** |
|
380 | - * @param string $path |
|
381 | - * @return bool |
|
382 | - */ |
|
383 | - protected function isOptionEnabled($path) |
|
384 | - { |
|
385 | - return 'yes' == $this->getOption($path); |
|
386 | - } |
|
379 | + /** |
|
380 | + * @param string $path |
|
381 | + * @return bool |
|
382 | + */ |
|
383 | + protected function isOptionEnabled($path) |
|
384 | + { |
|
385 | + return 'yes' == $this->getOption($path); |
|
386 | + } |
|
387 | 387 | |
388 | - /** |
|
389 | - * @return void |
|
390 | - */ |
|
391 | - protected function wrap(array &$renderedFields, Review $review) |
|
392 | - { |
|
393 | - $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review, $this); |
|
394 | - array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
395 | - $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
396 | - if (empty($value)) { |
|
397 | - return; |
|
398 | - } |
|
399 | - $value = glsr(Builder::class)->div($value, [ |
|
400 | - 'class' => 'glsr-review-'.$key, |
|
401 | - ]); |
|
402 | - }); |
|
403 | - } |
|
388 | + /** |
|
389 | + * @return void |
|
390 | + */ |
|
391 | + protected function wrap(array &$renderedFields, Review $review) |
|
392 | + { |
|
393 | + $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review, $this); |
|
394 | + array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
395 | + $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
396 | + if (empty($value)) { |
|
397 | + return; |
|
398 | + } |
|
399 | + $value = glsr(Builder::class)->div($value, [ |
|
400 | + 'class' => 'glsr-review-'.$key, |
|
401 | + ]); |
|
402 | + }); |
|
403 | + } |
|
404 | 404 | } |
@@ -7,115 +7,115 @@ |
||
7 | 7 | |
8 | 8 | class SiteReviewsFormBlock extends BlockGenerator |
9 | 9 | { |
10 | - /** |
|
11 | - * @return array |
|
12 | - */ |
|
13 | - public function attributes() |
|
14 | - { |
|
15 | - return [ |
|
16 | - 'assign_to' => [ |
|
17 | - 'default' => '', |
|
18 | - 'type' => 'string', |
|
19 | - ], |
|
20 | - 'category' => [ |
|
21 | - 'default' => '', |
|
22 | - 'type' => 'string', |
|
23 | - ], |
|
24 | - 'className' => [ |
|
25 | - 'default' => '', |
|
26 | - 'type' => 'string', |
|
27 | - ], |
|
28 | - 'hide' => [ |
|
29 | - 'default' => '', |
|
30 | - 'type' => 'string', |
|
31 | - ], |
|
32 | - 'id' => [ |
|
33 | - 'default' => '', |
|
34 | - 'type' => 'string', |
|
35 | - ], |
|
36 | - ]; |
|
37 | - } |
|
10 | + /** |
|
11 | + * @return array |
|
12 | + */ |
|
13 | + public function attributes() |
|
14 | + { |
|
15 | + return [ |
|
16 | + 'assign_to' => [ |
|
17 | + 'default' => '', |
|
18 | + 'type' => 'string', |
|
19 | + ], |
|
20 | + 'category' => [ |
|
21 | + 'default' => '', |
|
22 | + 'type' => 'string', |
|
23 | + ], |
|
24 | + 'className' => [ |
|
25 | + 'default' => '', |
|
26 | + 'type' => 'string', |
|
27 | + ], |
|
28 | + 'hide' => [ |
|
29 | + 'default' => '', |
|
30 | + 'type' => 'string', |
|
31 | + ], |
|
32 | + 'id' => [ |
|
33 | + 'default' => '', |
|
34 | + 'type' => 'string', |
|
35 | + ], |
|
36 | + ]; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public function render(array $attributes) |
|
43 | - { |
|
44 | - $attributes['class'] = $attributes['className']; |
|
45 | - $shortcode = glsr(Shortcode::class); |
|
46 | - if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
47 | - $this->filterFormFields(); |
|
48 | - $this->filterRatingField(); |
|
49 | - $this->filterShortcodeClass(); |
|
50 | - $this->filterSubmitButton(); |
|
51 | - if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
52 | - $this->filterInterpolation(); |
|
53 | - } |
|
54 | - } |
|
55 | - return $shortcode->buildShortcode($attributes); |
|
56 | - } |
|
39 | + /** |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public function render(array $attributes) |
|
43 | + { |
|
44 | + $attributes['class'] = $attributes['className']; |
|
45 | + $shortcode = glsr(Shortcode::class); |
|
46 | + if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
47 | + $this->filterFormFields(); |
|
48 | + $this->filterRatingField(); |
|
49 | + $this->filterShortcodeClass(); |
|
50 | + $this->filterSubmitButton(); |
|
51 | + if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
52 | + $this->filterInterpolation(); |
|
53 | + } |
|
54 | + } |
|
55 | + return $shortcode->buildShortcode($attributes); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - protected function filterFormFields() |
|
62 | - { |
|
63 | - add_filter('site-reviews/config/forms/submission-form', function (array $config) { |
|
64 | - array_walk($config, function (&$field) { |
|
65 | - $field['disabled'] = true; |
|
66 | - $field['tabindex'] = '-1'; |
|
67 | - }); |
|
68 | - return $config; |
|
69 | - }); |
|
70 | - } |
|
58 | + /** |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + protected function filterFormFields() |
|
62 | + { |
|
63 | + add_filter('site-reviews/config/forms/submission-form', function (array $config) { |
|
64 | + array_walk($config, function (&$field) { |
|
65 | + $field['disabled'] = true; |
|
66 | + $field['tabindex'] = '-1'; |
|
67 | + }); |
|
68 | + return $config; |
|
69 | + }); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - protected function filterInterpolation() |
|
76 | - { |
|
77 | - add_filter('site-reviews/interpolate/reviews-form', function ($context) { |
|
78 | - $context['class'] = 'glsr-default glsr-block-disabled'; |
|
79 | - $context['fields'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
80 | - $context['response'] = ''; |
|
81 | - $context['submit_button'] = ''; |
|
82 | - return $context; |
|
83 | - }); |
|
84 | - } |
|
72 | + /** |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + protected function filterInterpolation() |
|
76 | + { |
|
77 | + add_filter('site-reviews/interpolate/reviews-form', function ($context) { |
|
78 | + $context['class'] = 'glsr-default glsr-block-disabled'; |
|
79 | + $context['fields'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
80 | + $context['response'] = ''; |
|
81 | + $context['submit_button'] = ''; |
|
82 | + return $context; |
|
83 | + }); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - protected function filterRatingField() |
|
90 | - { |
|
91 | - add_filter('site-reviews/rendered/field', function ($html, $type, $args) { |
|
92 | - if ('rating' == $args['path']) { |
|
93 | - $stars = '<span class="glsr-stars">'; |
|
94 | - $stars.= str_repeat('<span class="glsr-star glsr-star-empty" aria-hidden="true"></span>', (int) glsr()->constant('MAX_RATING', Rating::class)); |
|
95 | - $stars.= '</span>'; |
|
96 | - $html = preg_replace('/(.*)(<select.*)(<\/select>)(.*)/', '$1'.$stars.'$4', $html); |
|
97 | - } |
|
98 | - return $html; |
|
99 | - }, 10, 3); |
|
100 | - } |
|
86 | + /** |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + protected function filterRatingField() |
|
90 | + { |
|
91 | + add_filter('site-reviews/rendered/field', function ($html, $type, $args) { |
|
92 | + if ('rating' == $args['path']) { |
|
93 | + $stars = '<span class="glsr-stars">'; |
|
94 | + $stars.= str_repeat('<span class="glsr-star glsr-star-empty" aria-hidden="true"></span>', (int) glsr()->constant('MAX_RATING', Rating::class)); |
|
95 | + $stars.= '</span>'; |
|
96 | + $html = preg_replace('/(.*)(<select.*)(<\/select>)(.*)/', '$1'.$stars.'$4', $html); |
|
97 | + } |
|
98 | + return $html; |
|
99 | + }, 10, 3); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * @return void |
|
104 | - */ |
|
105 | - protected function filterShortcodeClass() |
|
106 | - { |
|
107 | - add_filter('site-reviews/style', function () { |
|
108 | - return 'default'; |
|
109 | - }); |
|
110 | - } |
|
102 | + /** |
|
103 | + * @return void |
|
104 | + */ |
|
105 | + protected function filterShortcodeClass() |
|
106 | + { |
|
107 | + add_filter('site-reviews/style', function () { |
|
108 | + return 'default'; |
|
109 | + }); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * @return void |
|
114 | - */ |
|
115 | - protected function filterSubmitButton() |
|
116 | - { |
|
117 | - add_filter('site-reviews/rendered/template/form/submit-button', function ($template) { |
|
118 | - return str_replace('type="submit"', 'tabindex="-1"', $template); |
|
119 | - }); |
|
120 | - } |
|
112 | + /** |
|
113 | + * @return void |
|
114 | + */ |
|
115 | + protected function filterSubmitButton() |
|
116 | + { |
|
117 | + add_filter('site-reviews/rendered/template/form/submit-button', function ($template) { |
|
118 | + return str_replace('type="submit"', 'tabindex="-1"', $template); |
|
119 | + }); |
|
120 | + } |
|
121 | 121 | } |
@@ -10,177 +10,177 @@ |
||
10 | 10 | |
11 | 11 | class Columns |
12 | 12 | { |
13 | - /** |
|
14 | - * @param int $postId |
|
15 | - * @return void|string |
|
16 | - */ |
|
17 | - public function buildColumnAssignedTo($postId) |
|
18 | - { |
|
19 | - $assignedPost = glsr(Database::class)->getAssignedToPost($postId); |
|
20 | - if ($assignedPost instanceof WP_Post && 'publish' == $assignedPost->post_status) { |
|
21 | - return glsr(Builder::class)->a(get_the_title($assignedPost->ID), [ |
|
22 | - 'href' => (string) get_the_permalink($assignedPost->ID), |
|
23 | - ]); |
|
24 | - } |
|
25 | - } |
|
13 | + /** |
|
14 | + * @param int $postId |
|
15 | + * @return void|string |
|
16 | + */ |
|
17 | + public function buildColumnAssignedTo($postId) |
|
18 | + { |
|
19 | + $assignedPost = glsr(Database::class)->getAssignedToPost($postId); |
|
20 | + if ($assignedPost instanceof WP_Post && 'publish' == $assignedPost->post_status) { |
|
21 | + return glsr(Builder::class)->a(get_the_title($assignedPost->ID), [ |
|
22 | + 'href' => (string) get_the_permalink($assignedPost->ID), |
|
23 | + ]); |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @param int $postId |
|
29 | - * @return void|string |
|
30 | - */ |
|
31 | - public function buildColumnEmail($postId) |
|
32 | - { |
|
33 | - if ($email = glsr(Database::class)->get($postId, 'email')) { |
|
34 | - return $email; |
|
35 | - } |
|
36 | - } |
|
27 | + /** |
|
28 | + * @param int $postId |
|
29 | + * @return void|string |
|
30 | + */ |
|
31 | + public function buildColumnEmail($postId) |
|
32 | + { |
|
33 | + if ($email = glsr(Database::class)->get($postId, 'email')) { |
|
34 | + return $email; |
|
35 | + } |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param int $postId |
|
40 | - * @return void|string |
|
41 | - */ |
|
42 | - public function buildColumnIpAddress($postId) |
|
43 | - { |
|
44 | - if ($ipAddress = glsr(Database::class)->get($postId, 'ip_address')) { |
|
45 | - return $ipAddress; |
|
46 | - } |
|
47 | - } |
|
38 | + /** |
|
39 | + * @param int $postId |
|
40 | + * @return void|string |
|
41 | + */ |
|
42 | + public function buildColumnIpAddress($postId) |
|
43 | + { |
|
44 | + if ($ipAddress = glsr(Database::class)->get($postId, 'ip_address')) { |
|
45 | + return $ipAddress; |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param int $postId |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function buildColumnPinned($postId) |
|
54 | - { |
|
55 | - $pinned = glsr(Database::class)->get($postId, 'pinned') |
|
56 | - ? 'pinned ' |
|
57 | - : ''; |
|
58 | - return glsr(Builder::class)->i([ |
|
59 | - 'class' => $pinned.'dashicons dashicons-sticky', |
|
60 | - 'data-id' => $postId, |
|
61 | - ]); |
|
62 | - } |
|
49 | + /** |
|
50 | + * @param int $postId |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function buildColumnPinned($postId) |
|
54 | + { |
|
55 | + $pinned = glsr(Database::class)->get($postId, 'pinned') |
|
56 | + ? 'pinned ' |
|
57 | + : ''; |
|
58 | + return glsr(Builder::class)->i([ |
|
59 | + 'class' => $pinned.'dashicons dashicons-sticky', |
|
60 | + 'data-id' => $postId, |
|
61 | + ]); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @param int $postId |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function buildColumnResponse($postId) |
|
69 | - { |
|
70 | - return glsr(Database::class)->get($postId, 'response') |
|
71 | - ? __('Yes', 'site-reviews') |
|
72 | - : __('No', 'site-reviews'); |
|
73 | - } |
|
64 | + /** |
|
65 | + * @param int $postId |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function buildColumnResponse($postId) |
|
69 | + { |
|
70 | + return glsr(Database::class)->get($postId, 'response') |
|
71 | + ? __('Yes', 'site-reviews') |
|
72 | + : __('No', 'site-reviews'); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @param int $postId |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public function buildColumnReviewer($postId) |
|
80 | - { |
|
81 | - return strval(glsr(Database::class)->get($postId, 'author')); |
|
82 | - } |
|
75 | + /** |
|
76 | + * @param int $postId |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public function buildColumnReviewer($postId) |
|
80 | + { |
|
81 | + return strval(glsr(Database::class)->get($postId, 'author')); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @param int $postId |
|
86 | - * @param int|null $rating |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - public function buildColumnRating($postId) |
|
90 | - { |
|
91 | - return glsr_star_rating(intval(glsr(Database::class)->get($postId, 'rating'))); |
|
92 | - } |
|
84 | + /** |
|
85 | + * @param int $postId |
|
86 | + * @param int|null $rating |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + public function buildColumnRating($postId) |
|
90 | + { |
|
91 | + return glsr_star_rating(intval(glsr(Database::class)->get($postId, 'rating'))); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @param int $postId |
|
96 | - * @return string |
|
97 | - */ |
|
98 | - public function buildColumnReviewType($postId) |
|
99 | - { |
|
100 | - $type = glsr(Database::class)->get($postId, 'review_type'); |
|
101 | - return array_key_exists($type, glsr()->reviewTypes) |
|
102 | - ? glsr()->reviewTypes[$type] |
|
103 | - : __('Unsupported Type', 'site-reviews'); |
|
104 | - } |
|
94 | + /** |
|
95 | + * @param int $postId |
|
96 | + * @return string |
|
97 | + */ |
|
98 | + public function buildColumnReviewType($postId) |
|
99 | + { |
|
100 | + $type = glsr(Database::class)->get($postId, 'review_type'); |
|
101 | + return array_key_exists($type, glsr()->reviewTypes) |
|
102 | + ? glsr()->reviewTypes[$type] |
|
103 | + : __('Unsupported Type', 'site-reviews'); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @param string $postType |
|
108 | - * @return void |
|
109 | - */ |
|
110 | - public function renderFilters($postType) |
|
111 | - { |
|
112 | - if (Application::POST_TYPE !== $postType) { |
|
113 | - return; |
|
114 | - } |
|
115 | - if (!($status = filter_input(INPUT_GET, 'post_status'))) { |
|
116 | - $status = 'publish'; |
|
117 | - } |
|
118 | - $ratings = glsr(Database::class)->getReviewsMeta('rating', $status); |
|
119 | - $types = glsr(Database::class)->getReviewsMeta('review_type', $status); |
|
120 | - $this->renderFilterRatings($ratings); |
|
121 | - $this->renderFilterTypes($types); |
|
122 | - } |
|
106 | + /** |
|
107 | + * @param string $postType |
|
108 | + * @return void |
|
109 | + */ |
|
110 | + public function renderFilters($postType) |
|
111 | + { |
|
112 | + if (Application::POST_TYPE !== $postType) { |
|
113 | + return; |
|
114 | + } |
|
115 | + if (!($status = filter_input(INPUT_GET, 'post_status'))) { |
|
116 | + $status = 'publish'; |
|
117 | + } |
|
118 | + $ratings = glsr(Database::class)->getReviewsMeta('rating', $status); |
|
119 | + $types = glsr(Database::class)->getReviewsMeta('review_type', $status); |
|
120 | + $this->renderFilterRatings($ratings); |
|
121 | + $this->renderFilterTypes($types); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * @param string $column |
|
126 | - * @param int $postId |
|
127 | - * @return void |
|
128 | - */ |
|
129 | - public function renderValues($column, $postId) |
|
130 | - { |
|
131 | - $method = Helper::buildMethodName($column, 'buildColumn'); |
|
132 | - $value = method_exists($this, $method) |
|
133 | - ? call_user_func([$this, $method], $postId) |
|
134 | - : ''; |
|
135 | - $value = apply_filters('site-reviews/columns/'.$column, $value, $postId); |
|
136 | - if (0 !== $value && empty($value)) { |
|
137 | - $value = '—'; |
|
138 | - } |
|
139 | - echo $value; |
|
140 | - } |
|
124 | + /** |
|
125 | + * @param string $column |
|
126 | + * @param int $postId |
|
127 | + * @return void |
|
128 | + */ |
|
129 | + public function renderValues($column, $postId) |
|
130 | + { |
|
131 | + $method = Helper::buildMethodName($column, 'buildColumn'); |
|
132 | + $value = method_exists($this, $method) |
|
133 | + ? call_user_func([$this, $method], $postId) |
|
134 | + : ''; |
|
135 | + $value = apply_filters('site-reviews/columns/'.$column, $value, $postId); |
|
136 | + if (0 !== $value && empty($value)) { |
|
137 | + $value = '—'; |
|
138 | + } |
|
139 | + echo $value; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * @param array $ratings |
|
144 | - * @return void |
|
145 | - */ |
|
146 | - protected function renderFilterRatings($ratings) |
|
147 | - { |
|
148 | - if (empty($ratings)) { |
|
149 | - return; |
|
150 | - } |
|
151 | - $ratings = array_flip(array_reverse($ratings)); |
|
152 | - array_walk($ratings, function (&$value, $key) { |
|
153 | - $label = _n('%s star', '%s stars', $key, 'site-reviews'); |
|
154 | - $value = sprintf($label, $key); |
|
155 | - }); |
|
156 | - echo glsr(Builder::class)->label(__('Filter by rating', 'site-reviews'), [ |
|
157 | - 'class' => 'screen-reader-text', |
|
158 | - 'for' => 'rating', |
|
159 | - ]); |
|
160 | - echo glsr(Builder::class)->select([ |
|
161 | - 'name' => 'rating', |
|
162 | - 'options' => ['' => __('All ratings', 'site-reviews')] + $ratings, |
|
163 | - 'value' => filter_input(INPUT_GET, 'rating'), |
|
164 | - ]); |
|
165 | - } |
|
142 | + /** |
|
143 | + * @param array $ratings |
|
144 | + * @return void |
|
145 | + */ |
|
146 | + protected function renderFilterRatings($ratings) |
|
147 | + { |
|
148 | + if (empty($ratings)) { |
|
149 | + return; |
|
150 | + } |
|
151 | + $ratings = array_flip(array_reverse($ratings)); |
|
152 | + array_walk($ratings, function (&$value, $key) { |
|
153 | + $label = _n('%s star', '%s stars', $key, 'site-reviews'); |
|
154 | + $value = sprintf($label, $key); |
|
155 | + }); |
|
156 | + echo glsr(Builder::class)->label(__('Filter by rating', 'site-reviews'), [ |
|
157 | + 'class' => 'screen-reader-text', |
|
158 | + 'for' => 'rating', |
|
159 | + ]); |
|
160 | + echo glsr(Builder::class)->select([ |
|
161 | + 'name' => 'rating', |
|
162 | + 'options' => ['' => __('All ratings', 'site-reviews')] + $ratings, |
|
163 | + 'value' => filter_input(INPUT_GET, 'rating'), |
|
164 | + ]); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @param array $types |
|
169 | - * @return void |
|
170 | - */ |
|
171 | - protected function renderFilterTypes($types) |
|
172 | - { |
|
173 | - if (count(glsr()->reviewTypes) < 2) { |
|
174 | - return; |
|
175 | - } |
|
176 | - echo glsr(Builder::class)->label(__('Filter by type', 'site-reviews'), [ |
|
177 | - 'class' => 'screen-reader-text', |
|
178 | - 'for' => 'review_type', |
|
179 | - ]); |
|
180 | - echo glsr(Builder::class)->select([ |
|
181 | - 'name' => 'review_type', |
|
182 | - 'options' => ['' => __('All types', 'site-reviews')] + glsr()->reviewTypes, |
|
183 | - 'value' => filter_input(INPUT_GET, 'review_type'), |
|
184 | - ]); |
|
185 | - } |
|
167 | + /** |
|
168 | + * @param array $types |
|
169 | + * @return void |
|
170 | + */ |
|
171 | + protected function renderFilterTypes($types) |
|
172 | + { |
|
173 | + if (count(glsr()->reviewTypes) < 2) { |
|
174 | + return; |
|
175 | + } |
|
176 | + echo glsr(Builder::class)->label(__('Filter by type', 'site-reviews'), [ |
|
177 | + 'class' => 'screen-reader-text', |
|
178 | + 'for' => 'review_type', |
|
179 | + ]); |
|
180 | + echo glsr(Builder::class)->select([ |
|
181 | + 'name' => 'review_type', |
|
182 | + 'options' => ['' => __('All types', 'site-reviews')] + glsr()->reviewTypes, |
|
183 | + 'value' => filter_input(INPUT_GET, 'review_type'), |
|
184 | + ]); |
|
185 | + } |
|
186 | 186 | } |
@@ -7,266 +7,266 @@ |
||
7 | 7 | |
8 | 8 | class Updater |
9 | 9 | { |
10 | - /** |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - protected $apiUrl; |
|
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $data; |
|
18 | - /** |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected $plugin; |
|
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected $transientName; |
|
10 | + /** |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + protected $apiUrl; |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $data; |
|
18 | + /** |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected $plugin; |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected $transientName; |
|
26 | 26 | |
27 | - /** |
|
28 | - * @param string $apiUrl |
|
29 | - * @param string $file |
|
30 | - */ |
|
31 | - public function __construct($apiUrl, $file, array $data = []) |
|
32 | - { |
|
33 | - if (!function_exists('get_plugin_data')) { |
|
34 | - require_once ABSPATH.WPINC.'/plugin.php'; |
|
35 | - } |
|
36 | - $this->apiUrl = trailingslashit(apply_filters('site-reviews/addon/api-url', $apiUrl)); |
|
37 | - $this->data = wp_parse_args($data, get_plugin_data($file)); |
|
38 | - $this->plugin = plugin_basename($file); |
|
39 | - $this->transientName = Application::PREFIX.md5(Arr::get($data, 'TextDomain')); |
|
40 | - } |
|
27 | + /** |
|
28 | + * @param string $apiUrl |
|
29 | + * @param string $file |
|
30 | + */ |
|
31 | + public function __construct($apiUrl, $file, array $data = []) |
|
32 | + { |
|
33 | + if (!function_exists('get_plugin_data')) { |
|
34 | + require_once ABSPATH.WPINC.'/plugin.php'; |
|
35 | + } |
|
36 | + $this->apiUrl = trailingslashit(apply_filters('site-reviews/addon/api-url', $apiUrl)); |
|
37 | + $this->data = wp_parse_args($data, get_plugin_data($file)); |
|
38 | + $this->plugin = plugin_basename($file); |
|
39 | + $this->transientName = Application::PREFIX.md5(Arr::get($data, 'TextDomain')); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * @return object |
|
44 | - */ |
|
45 | - public function activateLicense(array $data = []) |
|
46 | - { |
|
47 | - return $this->request('activate_license', $data); |
|
48 | - } |
|
42 | + /** |
|
43 | + * @return object |
|
44 | + */ |
|
45 | + public function activateLicense(array $data = []) |
|
46 | + { |
|
47 | + return $this->request('activate_license', $data); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @return object |
|
52 | - */ |
|
53 | - public function checkLicense(array $data = []) |
|
54 | - { |
|
55 | - $response = $this->request('check_license', $data); |
|
56 | - if ('valid' === Arr::get($response, 'license')) { |
|
57 | - $this->getPluginUpdate(true); |
|
58 | - } |
|
59 | - return $response; |
|
60 | - } |
|
50 | + /** |
|
51 | + * @return object |
|
52 | + */ |
|
53 | + public function checkLicense(array $data = []) |
|
54 | + { |
|
55 | + $response = $this->request('check_license', $data); |
|
56 | + if ('valid' === Arr::get($response, 'license')) { |
|
57 | + $this->getPluginUpdate(true); |
|
58 | + } |
|
59 | + return $response; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return object |
|
64 | - */ |
|
65 | - public function deactivateLicense(array $data = []) |
|
66 | - { |
|
67 | - return $this->request('deactivate_license', $data); |
|
68 | - } |
|
62 | + /** |
|
63 | + * @return object |
|
64 | + */ |
|
65 | + public function deactivateLicense(array $data = []) |
|
66 | + { |
|
67 | + return $this->request('deactivate_license', $data); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * @param false|object|array $result |
|
72 | - * @param string $action |
|
73 | - * @param object $args |
|
74 | - * @return mixed |
|
75 | - */ |
|
76 | - public function filterPluginUpdateDetails($result, $action, $args) |
|
77 | - { |
|
78 | - if ('plugin_information' != $action |
|
79 | - || Arr::get($this->data, 'TextDomain') != Arr::get($args, 'slug')) { |
|
80 | - return $result; |
|
81 | - } |
|
82 | - if ($updateInfo = $this->getPluginUpdate()) { |
|
83 | - return $this->modifyUpdateDetails($updateInfo); |
|
84 | - } |
|
85 | - return $result; |
|
86 | - } |
|
70 | + /** |
|
71 | + * @param false|object|array $result |
|
72 | + * @param string $action |
|
73 | + * @param object $args |
|
74 | + * @return mixed |
|
75 | + */ |
|
76 | + public function filterPluginUpdateDetails($result, $action, $args) |
|
77 | + { |
|
78 | + if ('plugin_information' != $action |
|
79 | + || Arr::get($this->data, 'TextDomain') != Arr::get($args, 'slug')) { |
|
80 | + return $result; |
|
81 | + } |
|
82 | + if ($updateInfo = $this->getPluginUpdate()) { |
|
83 | + return $this->modifyUpdateDetails($updateInfo); |
|
84 | + } |
|
85 | + return $result; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @param object $transient |
|
90 | - * @return object |
|
91 | - */ |
|
92 | - public function filterPluginUpdates($transient) |
|
93 | - { |
|
94 | - if ($updateInfo = $this->getPluginUpdate()) { |
|
95 | - return $this->modifyPluginUpdates($transient, $updateInfo); |
|
96 | - } |
|
97 | - return $transient; |
|
98 | - } |
|
88 | + /** |
|
89 | + * @param object $transient |
|
90 | + * @return object |
|
91 | + */ |
|
92 | + public function filterPluginUpdates($transient) |
|
93 | + { |
|
94 | + if ($updateInfo = $this->getPluginUpdate()) { |
|
95 | + return $this->modifyPluginUpdates($transient, $updateInfo); |
|
96 | + } |
|
97 | + return $transient; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @return object |
|
102 | - */ |
|
103 | - public function getVersion(array $data = []) |
|
104 | - { |
|
105 | - return $this->request('get_version', $data); |
|
106 | - } |
|
100 | + /** |
|
101 | + * @return object |
|
102 | + */ |
|
103 | + public function getVersion(array $data = []) |
|
104 | + { |
|
105 | + return $this->request('get_version', $data); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function init() |
|
112 | - { |
|
113 | - if ($this->apiUrl === trailingslashit(home_url())) { |
|
114 | - return; |
|
115 | - } |
|
116 | - add_filter('plugins_api', [$this, 'filterPluginUpdateDetails'], 10, 3); |
|
117 | - add_filter('pre_set_site_transient_update_plugins', [$this, 'filterPluginUpdates'], 999); |
|
118 | - add_action('load-update-core.php', [$this, 'onForceUpdateCheck'], 9); |
|
119 | - add_action('in_plugin_update_message-'.$this->plugin, [$this, 'renderLicenseMissingLink']); |
|
120 | - } |
|
108 | + /** |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function init() |
|
112 | + { |
|
113 | + if ($this->apiUrl === trailingslashit(home_url())) { |
|
114 | + return; |
|
115 | + } |
|
116 | + add_filter('plugins_api', [$this, 'filterPluginUpdateDetails'], 10, 3); |
|
117 | + add_filter('pre_set_site_transient_update_plugins', [$this, 'filterPluginUpdates'], 999); |
|
118 | + add_action('load-update-core.php', [$this, 'onForceUpdateCheck'], 9); |
|
119 | + add_action('in_plugin_update_message-'.$this->plugin, [$this, 'renderLicenseMissingLink']); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @return bool |
|
124 | - */ |
|
125 | - public function isLicenseValid() |
|
126 | - { |
|
127 | - $result = $this->checkLicense(); |
|
128 | - return 'valid' === Arr::get($result, 'license'); |
|
129 | - } |
|
122 | + /** |
|
123 | + * @return bool |
|
124 | + */ |
|
125 | + public function isLicenseValid() |
|
126 | + { |
|
127 | + $result = $this->checkLicense(); |
|
128 | + return 'valid' === Arr::get($result, 'license'); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - public function onForceUpdateCheck() |
|
135 | - { |
|
136 | - if (!filter_input(INPUT_GET, 'force-check')) { |
|
137 | - return; |
|
138 | - } |
|
139 | - foreach (glsr()->addons as $addon) { |
|
140 | - try { |
|
141 | - glsr($addon)->updater->getPluginUpdate(true); |
|
142 | - } catch (\Exception $e) { |
|
143 | - glsr_log()->error($e->getMessage()); |
|
144 | - } |
|
145 | - } |
|
146 | - } |
|
131 | + /** |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + public function onForceUpdateCheck() |
|
135 | + { |
|
136 | + if (!filter_input(INPUT_GET, 'force-check')) { |
|
137 | + return; |
|
138 | + } |
|
139 | + foreach (glsr()->addons as $addon) { |
|
140 | + try { |
|
141 | + glsr($addon)->updater->getPluginUpdate(true); |
|
142 | + } catch (\Exception $e) { |
|
143 | + glsr_log()->error($e->getMessage()); |
|
144 | + } |
|
145 | + } |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * @return void |
|
150 | - */ |
|
151 | - public function renderLicenseMissingLink() |
|
152 | - { |
|
153 | - if (!$this->isLicenseValid()) { |
|
154 | - glsr()->render('partials/addons/license-missing'); |
|
155 | - } |
|
156 | - } |
|
148 | + /** |
|
149 | + * @return void |
|
150 | + */ |
|
151 | + public function renderLicenseMissingLink() |
|
152 | + { |
|
153 | + if (!$this->isLicenseValid()) { |
|
154 | + glsr()->render('partials/addons/license-missing'); |
|
155 | + } |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @return false|object |
|
160 | - */ |
|
161 | - protected function getCachedVersion() |
|
162 | - { |
|
163 | - return get_transient($this->transientName); |
|
164 | - } |
|
158 | + /** |
|
159 | + * @return false|object |
|
160 | + */ |
|
161 | + protected function getCachedVersion() |
|
162 | + { |
|
163 | + return get_transient($this->transientName); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @param bool $force |
|
168 | - * @return false|object |
|
169 | - */ |
|
170 | - protected function getPluginUpdate($force = false) |
|
171 | - { |
|
172 | - $version = $this->getCachedVersion(); |
|
173 | - if (false === $version || $force) { |
|
174 | - $version = $this->getVersion(); |
|
175 | - $this->setCachedVersion($version); |
|
176 | - } |
|
177 | - if (isset($version->error)) { |
|
178 | - glsr_log()->error($version->error); |
|
179 | - return false; |
|
180 | - } |
|
181 | - return $version; |
|
182 | - } |
|
166 | + /** |
|
167 | + * @param bool $force |
|
168 | + * @return false|object |
|
169 | + */ |
|
170 | + protected function getPluginUpdate($force = false) |
|
171 | + { |
|
172 | + $version = $this->getCachedVersion(); |
|
173 | + if (false === $version || $force) { |
|
174 | + $version = $this->getVersion(); |
|
175 | + $this->setCachedVersion($version); |
|
176 | + } |
|
177 | + if (isset($version->error)) { |
|
178 | + glsr_log()->error($version->error); |
|
179 | + return false; |
|
180 | + } |
|
181 | + return $version; |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * @param object $transient |
|
186 | - * @param object $updateInfo |
|
187 | - * @return object |
|
188 | - */ |
|
189 | - protected function modifyPluginUpdates($transient, $updateInfo) |
|
190 | - { |
|
191 | - $updateInfo->id = Application::ID.'/'.Arr::get($this->data, 'TextDomain'); |
|
192 | - $updateInfo->plugin = $this->plugin; |
|
193 | - $updateInfo->requires_php = Arr::get($this->data, 'RequiresPHP'); |
|
194 | - $updateInfo->tested = Arr::get($this->data, 'testedTo'); |
|
195 | - $transient->checked[$this->plugin] = Arr::get($this->data, 'Version'); |
|
196 | - $transient->last_checked = time(); |
|
197 | - if (version_compare($updateInfo->new_version, Arr::get($this->data, 'Version'), '>')) { |
|
198 | - unset($transient->no_update[$this->plugin]); |
|
199 | - $updateInfo->update = true; |
|
200 | - $transient->response[$this->plugin] = $updateInfo; |
|
201 | - } else { |
|
202 | - unset($transient->response[$this->plugin]); |
|
203 | - $transient->no_update[$this->plugin] = $updateInfo; |
|
204 | - } |
|
205 | - return $transient; |
|
206 | - } |
|
184 | + /** |
|
185 | + * @param object $transient |
|
186 | + * @param object $updateInfo |
|
187 | + * @return object |
|
188 | + */ |
|
189 | + protected function modifyPluginUpdates($transient, $updateInfo) |
|
190 | + { |
|
191 | + $updateInfo->id = Application::ID.'/'.Arr::get($this->data, 'TextDomain'); |
|
192 | + $updateInfo->plugin = $this->plugin; |
|
193 | + $updateInfo->requires_php = Arr::get($this->data, 'RequiresPHP'); |
|
194 | + $updateInfo->tested = Arr::get($this->data, 'testedTo'); |
|
195 | + $transient->checked[$this->plugin] = Arr::get($this->data, 'Version'); |
|
196 | + $transient->last_checked = time(); |
|
197 | + if (version_compare($updateInfo->new_version, Arr::get($this->data, 'Version'), '>')) { |
|
198 | + unset($transient->no_update[$this->plugin]); |
|
199 | + $updateInfo->update = true; |
|
200 | + $transient->response[$this->plugin] = $updateInfo; |
|
201 | + } else { |
|
202 | + unset($transient->response[$this->plugin]); |
|
203 | + $transient->no_update[$this->plugin] = $updateInfo; |
|
204 | + } |
|
205 | + return $transient; |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * @param object $updateInfo |
|
210 | - * @return object |
|
211 | - */ |
|
212 | - protected function modifyUpdateDetails($updateInfo) |
|
213 | - { |
|
214 | - $updateInfo->author = Arr::get($this->data, 'Author'); |
|
215 | - $updateInfo->author_profile = Arr::get($this->data, 'AuthorURI'); |
|
216 | - $updateInfo->requires = Arr::get($this->data, 'RequiresWP'); |
|
217 | - $updateInfo->requires_php = Arr::get($this->data, 'RequiresPHP'); |
|
218 | - $updateInfo->tested = Arr::get($this->data, 'testedTo'); |
|
219 | - $updateInfo->version = $updateInfo->new_version; |
|
220 | - return $updateInfo; |
|
221 | - } |
|
208 | + /** |
|
209 | + * @param object $updateInfo |
|
210 | + * @return object |
|
211 | + */ |
|
212 | + protected function modifyUpdateDetails($updateInfo) |
|
213 | + { |
|
214 | + $updateInfo->author = Arr::get($this->data, 'Author'); |
|
215 | + $updateInfo->author_profile = Arr::get($this->data, 'AuthorURI'); |
|
216 | + $updateInfo->requires = Arr::get($this->data, 'RequiresWP'); |
|
217 | + $updateInfo->requires_php = Arr::get($this->data, 'RequiresPHP'); |
|
218 | + $updateInfo->tested = Arr::get($this->data, 'testedTo'); |
|
219 | + $updateInfo->version = $updateInfo->new_version; |
|
220 | + return $updateInfo; |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * @param \WP_Error|array $response |
|
225 | - * @return object |
|
226 | - */ |
|
227 | - protected function normalizeResponse($response) |
|
228 | - { |
|
229 | - $body = wp_remote_retrieve_body($response); |
|
230 | - if ($data = json_decode($body)) { |
|
231 | - $data = array_map('maybe_unserialize', (array) $data); |
|
232 | - return (object) $data; |
|
233 | - } |
|
234 | - $error = is_wp_error($response) |
|
235 | - ? $response->get_error_message() |
|
236 | - : 'Update server not responding ('.Arr::get($this->data, 'TextDomain').')'; |
|
237 | - return (object) ['error' => $error]; |
|
238 | - } |
|
223 | + /** |
|
224 | + * @param \WP_Error|array $response |
|
225 | + * @return object |
|
226 | + */ |
|
227 | + protected function normalizeResponse($response) |
|
228 | + { |
|
229 | + $body = wp_remote_retrieve_body($response); |
|
230 | + if ($data = json_decode($body)) { |
|
231 | + $data = array_map('maybe_unserialize', (array) $data); |
|
232 | + return (object) $data; |
|
233 | + } |
|
234 | + $error = is_wp_error($response) |
|
235 | + ? $response->get_error_message() |
|
236 | + : 'Update server not responding ('.Arr::get($this->data, 'TextDomain').')'; |
|
237 | + return (object) ['error' => $error]; |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * @param string $action activate_license|check_license|deactivate_license|get_version |
|
242 | - * @return object |
|
243 | - */ |
|
244 | - protected function request($action, array $data = []) |
|
245 | - { |
|
246 | - $data = wp_parse_args($data, $this->data); |
|
247 | - $response = wp_remote_post($this->apiUrl, [ |
|
248 | - 'body' => [ |
|
249 | - 'edd_action' => $action, |
|
250 | - 'item_id' => Arr::get($data, 'item_id'), |
|
251 | - 'item_name' => Arr::get($data, 'Name'), |
|
252 | - 'license' => Arr::get($data, 'license'), |
|
253 | - 'slug' => Arr::get($data, 'TextDomain'), |
|
254 | - 'url' => home_url(), |
|
255 | - ], |
|
256 | - 'sslverify' => apply_filters('site-reviews/sslverify/post', false), |
|
257 | - 'timeout' => 15, |
|
258 | - ]); |
|
259 | - return $this->normalizeResponse($response); |
|
260 | - } |
|
240 | + /** |
|
241 | + * @param string $action activate_license|check_license|deactivate_license|get_version |
|
242 | + * @return object |
|
243 | + */ |
|
244 | + protected function request($action, array $data = []) |
|
245 | + { |
|
246 | + $data = wp_parse_args($data, $this->data); |
|
247 | + $response = wp_remote_post($this->apiUrl, [ |
|
248 | + 'body' => [ |
|
249 | + 'edd_action' => $action, |
|
250 | + 'item_id' => Arr::get($data, 'item_id'), |
|
251 | + 'item_name' => Arr::get($data, 'Name'), |
|
252 | + 'license' => Arr::get($data, 'license'), |
|
253 | + 'slug' => Arr::get($data, 'TextDomain'), |
|
254 | + 'url' => home_url(), |
|
255 | + ], |
|
256 | + 'sslverify' => apply_filters('site-reviews/sslverify/post', false), |
|
257 | + 'timeout' => 15, |
|
258 | + ]); |
|
259 | + return $this->normalizeResponse($response); |
|
260 | + } |
|
261 | 261 | |
262 | - /** |
|
263 | - * @param object $version |
|
264 | - * @return void |
|
265 | - */ |
|
266 | - protected function setCachedVersion($version) |
|
267 | - { |
|
268 | - if (!isset($version->error)) { |
|
269 | - set_transient($this->transientName, $version, 3 * HOUR_IN_SECONDS); |
|
270 | - } |
|
271 | - } |
|
262 | + /** |
|
263 | + * @param object $version |
|
264 | + * @return void |
|
265 | + */ |
|
266 | + protected function setCachedVersion($version) |
|
267 | + { |
|
268 | + if (!isset($version->error)) { |
|
269 | + set_transient($this->transientName, $version, 3 * HOUR_IN_SECONDS); |
|
270 | + } |
|
271 | + } |
|
272 | 272 | } |
@@ -9,92 +9,92 @@ |
||
9 | 9 | |
10 | 10 | class TermCountsManager |
11 | 11 | { |
12 | - /** |
|
13 | - * @var CountsManager |
|
14 | - */ |
|
15 | - protected $manager; |
|
12 | + /** |
|
13 | + * @var CountsManager |
|
14 | + */ |
|
15 | + protected $manager; |
|
16 | 16 | |
17 | - public function __construct() |
|
18 | - { |
|
19 | - $this->manager = glsr(CountsManager::class); |
|
20 | - } |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | + $this->manager = glsr(CountsManager::class); |
|
20 | + } |
|
21 | 21 | |
22 | - /** |
|
23 | - * @param int $termTaxonomyId |
|
24 | - * @return array |
|
25 | - */ |
|
26 | - public function build($termTaxonomyId) |
|
27 | - { |
|
28 | - return $this->manager->buildCounts([ |
|
29 | - 'term_ids' => [$termTaxonomyId], |
|
30 | - ]); |
|
31 | - } |
|
22 | + /** |
|
23 | + * @param int $termTaxonomyId |
|
24 | + * @return array |
|
25 | + */ |
|
26 | + public function build($termTaxonomyId) |
|
27 | + { |
|
28 | + return $this->manager->buildCounts([ |
|
29 | + 'term_ids' => [$termTaxonomyId], |
|
30 | + ]); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - public function decrease(Review $review) |
|
37 | - { |
|
38 | - foreach ($review->term_ids as $termId) { |
|
39 | - if (empty($counts = $this->get($termId))) { |
|
40 | - continue; |
|
41 | - } |
|
42 | - $this->update($termId, |
|
43 | - $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
44 | - ); |
|
45 | - } |
|
46 | - } |
|
33 | + /** |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + public function decrease(Review $review) |
|
37 | + { |
|
38 | + foreach ($review->term_ids as $termId) { |
|
39 | + if (empty($counts = $this->get($termId))) { |
|
40 | + continue; |
|
41 | + } |
|
42 | + $this->update($termId, |
|
43 | + $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
44 | + ); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param int $termId |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - public function get($termId) |
|
53 | - { |
|
54 | - return array_filter((array) get_term_meta($termId, CountsManager::META_COUNT, true)); |
|
55 | - } |
|
48 | + /** |
|
49 | + * @param int $termId |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + public function get($termId) |
|
53 | + { |
|
54 | + return array_filter((array) get_term_meta($termId, CountsManager::META_COUNT, true)); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function increase(Review $review) |
|
61 | - { |
|
62 | - $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
63 | - foreach ($terms as $term) { |
|
64 | - $counts = $this->get($term['term_id']); |
|
65 | - $counts = empty($counts) |
|
66 | - ? $this->build($term['term_taxonomy_id']) |
|
67 | - : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
68 | - $this->update($term['term_id'], $counts); |
|
69 | - } |
|
70 | - } |
|
57 | + /** |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function increase(Review $review) |
|
61 | + { |
|
62 | + $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
63 | + foreach ($terms as $term) { |
|
64 | + $counts = $this->get($term['term_id']); |
|
65 | + $counts = empty($counts) |
|
66 | + ? $this->build($term['term_taxonomy_id']) |
|
67 | + : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
68 | + $this->update($term['term_id'], $counts); |
|
69 | + } |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @param int $termId |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - public function update($termId, array $reviewCounts) |
|
77 | - { |
|
78 | - $term = get_term($termId, Application::TAXONOMY); |
|
79 | - if (!isset($term->term_id)) { |
|
80 | - return; |
|
81 | - } |
|
82 | - $ratingCounts = $this->manager->flatten($reviewCounts); |
|
83 | - update_term_meta($termId, CountsManager::META_COUNT, $reviewCounts); |
|
84 | - update_term_meta($termId, CountsManager::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
85 | - update_term_meta($termId, CountsManager::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
86 | - } |
|
72 | + /** |
|
73 | + * @param int $termId |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + public function update($termId, array $reviewCounts) |
|
77 | + { |
|
78 | + $term = get_term($termId, Application::TAXONOMY); |
|
79 | + if (!isset($term->term_id)) { |
|
80 | + return; |
|
81 | + } |
|
82 | + $ratingCounts = $this->manager->flatten($reviewCounts); |
|
83 | + update_term_meta($termId, CountsManager::META_COUNT, $reviewCounts); |
|
84 | + update_term_meta($termId, CountsManager::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
85 | + update_term_meta($termId, CountsManager::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - public function updateAll() |
|
92 | - { |
|
93 | - $terms = glsr(Database::class)->getTerms([ |
|
94 | - 'fields' => 'all', |
|
95 | - ]); |
|
96 | - foreach ($terms as $term) { |
|
97 | - $this->update($term->term_id, $this->build($term->term_taxonomy_id)); |
|
98 | - } |
|
99 | - } |
|
88 | + /** |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function updateAll() |
|
92 | + { |
|
93 | + $terms = glsr(Database::class)->getTerms([ |
|
94 | + 'fields' => 'all', |
|
95 | + ]); |
|
96 | + foreach ($terms as $term) { |
|
97 | + $this->update($term->term_id, $this->build($term->term_taxonomy_id)); |
|
98 | + } |
|
99 | + } |
|
100 | 100 | } |
@@ -7,84 +7,84 @@ |
||
7 | 7 | |
8 | 8 | class PostCountsManager |
9 | 9 | { |
10 | - /** |
|
11 | - * @var CountsManager |
|
12 | - */ |
|
13 | - protected $manager; |
|
10 | + /** |
|
11 | + * @var CountsManager |
|
12 | + */ |
|
13 | + protected $manager; |
|
14 | 14 | |
15 | - public function __construct() |
|
16 | - { |
|
17 | - $this->manager = glsr(CountsManager::class); |
|
18 | - } |
|
15 | + public function __construct() |
|
16 | + { |
|
17 | + $this->manager = glsr(CountsManager::class); |
|
18 | + } |
|
19 | 19 | |
20 | - /** |
|
21 | - * @param int $postId |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function build($postId) |
|
25 | - { |
|
26 | - return $this->manager->buildCounts([ |
|
27 | - 'post_ids' => [$postId], |
|
28 | - ]); |
|
29 | - } |
|
20 | + /** |
|
21 | + * @param int $postId |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function build($postId) |
|
25 | + { |
|
26 | + return $this->manager->buildCounts([ |
|
27 | + 'post_ids' => [$postId], |
|
28 | + ]); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @return void |
|
33 | - */ |
|
34 | - public function decrease(Review $review) |
|
35 | - { |
|
36 | - if (empty($counts = $this->get($review->assigned_to))) { |
|
37 | - return; |
|
38 | - } |
|
39 | - $this->update($review->assigned_to, |
|
40 | - $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
41 | - ); |
|
42 | - } |
|
31 | + /** |
|
32 | + * @return void |
|
33 | + */ |
|
34 | + public function decrease(Review $review) |
|
35 | + { |
|
36 | + if (empty($counts = $this->get($review->assigned_to))) { |
|
37 | + return; |
|
38 | + } |
|
39 | + $this->update($review->assigned_to, |
|
40 | + $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
41 | + ); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param int $postId |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function get($postId) |
|
49 | - { |
|
50 | - return array_filter((array) get_post_meta($postId, CountsManager::META_COUNT, true)); |
|
51 | - } |
|
44 | + /** |
|
45 | + * @param int $postId |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function get($postId) |
|
49 | + { |
|
50 | + return array_filter((array) get_post_meta($postId, CountsManager::META_COUNT, true)); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function increase(Review $review) |
|
57 | - { |
|
58 | - if (!(get_post($review->assigned_to) instanceof \WP_Post)) { |
|
59 | - return; |
|
60 | - } |
|
61 | - $counts = $this->get($review->assigned_to); |
|
62 | - $counts = empty($counts) |
|
63 | - ? $this->build($review->assigned_to) |
|
64 | - : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
65 | - $this->update($review->assigned_to, $counts); |
|
66 | - } |
|
53 | + /** |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function increase(Review $review) |
|
57 | + { |
|
58 | + if (!(get_post($review->assigned_to) instanceof \WP_Post)) { |
|
59 | + return; |
|
60 | + } |
|
61 | + $counts = $this->get($review->assigned_to); |
|
62 | + $counts = empty($counts) |
|
63 | + ? $this->build($review->assigned_to) |
|
64 | + : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
65 | + $this->update($review->assigned_to, $counts); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @param int $postId |
|
70 | - * @return void |
|
71 | - */ |
|
72 | - public function update($postId, array $reviewCounts) |
|
73 | - { |
|
74 | - $ratingCounts = $this->manager->flatten($reviewCounts); |
|
75 | - update_post_meta($postId, CountsManager::META_COUNT, $reviewCounts); |
|
76 | - update_post_meta($postId, CountsManager::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
77 | - update_post_meta($postId, CountsManager::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
78 | - } |
|
68 | + /** |
|
69 | + * @param int $postId |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + public function update($postId, array $reviewCounts) |
|
73 | + { |
|
74 | + $ratingCounts = $this->manager->flatten($reviewCounts); |
|
75 | + update_post_meta($postId, CountsManager::META_COUNT, $reviewCounts); |
|
76 | + update_post_meta($postId, CountsManager::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
77 | + update_post_meta($postId, CountsManager::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function updateAll() |
|
84 | - { |
|
85 | - $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
86 | - foreach ($postIds as $postId) { |
|
87 | - $this->update($postId, $this->build($postId)); |
|
88 | - } |
|
89 | - } |
|
80 | + /** |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function updateAll() |
|
84 | + { |
|
85 | + $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
86 | + foreach ($postIds as $postId) { |
|
87 | + $this->update($postId, $this->build($postId)); |
|
88 | + } |
|
89 | + } |
|
90 | 90 | } |