@@ -10,114 +10,114 @@ |
||
10 | 10 | |
11 | 11 | class NoticeController extends Controller |
12 | 12 | { |
13 | - const USER_META_KEY = '_glsr_notices'; |
|
13 | + const USER_META_KEY = '_glsr_notices'; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - protected $dismissValuesMap; |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + protected $dismissValuesMap; |
|
19 | 19 | |
20 | - public function __construct() |
|
21 | - { |
|
22 | - $this->dismissValuesMap = [ |
|
23 | - 'trustalyze' => glsr()->version('major'), |
|
24 | - 'welcome' => glsr()->version('minor'), |
|
25 | - ]; |
|
26 | - } |
|
20 | + public function __construct() |
|
21 | + { |
|
22 | + $this->dismissValuesMap = [ |
|
23 | + 'trustalyze' => glsr()->version('major'), |
|
24 | + 'welcome' => glsr()->version('minor'), |
|
25 | + ]; |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return void |
|
30 | - * @action admin_notices |
|
31 | - */ |
|
32 | - public function filterAdminNotices() |
|
33 | - { |
|
34 | - $screen = glsr_current_screen(); |
|
35 | - $this->renderWelcomeNotice($screen->post_type); |
|
36 | - $this->renderTrustalyzeNotice($screen->post_type); |
|
37 | - } |
|
28 | + /** |
|
29 | + * @return void |
|
30 | + * @action admin_notices |
|
31 | + */ |
|
32 | + public function filterAdminNotices() |
|
33 | + { |
|
34 | + $screen = glsr_current_screen(); |
|
35 | + $this->renderWelcomeNotice($screen->post_type); |
|
36 | + $this->renderTrustalyzeNotice($screen->post_type); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - public function routerDismissNotice(array $request) |
|
43 | - { |
|
44 | - if ($key = Arr::get($request, 'notice')) { |
|
45 | - $this->dismissNotice($key); |
|
46 | - } |
|
47 | - } |
|
39 | + /** |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + public function routerDismissNotice(array $request) |
|
43 | + { |
|
44 | + if ($key = Arr::get($request, 'notice')) { |
|
45 | + $this->dismissNotice($key); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param string $key |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - protected function dismissNotice($key) |
|
54 | - { |
|
55 | - $this->setUserMeta($key, $this->getVersionFor($key)); |
|
56 | - } |
|
49 | + /** |
|
50 | + * @param string $key |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + protected function dismissNotice($key) |
|
54 | + { |
|
55 | + $this->setUserMeta($key, $this->getVersionFor($key)); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param string $key |
|
60 | - * @param mixed $fallback |
|
61 | - * @return mixed |
|
62 | - */ |
|
63 | - protected function getUserMeta($key, $fallback) |
|
64 | - { |
|
65 | - $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true); |
|
66 | - return Arr::get($meta, $key, $fallback); |
|
67 | - } |
|
58 | + /** |
|
59 | + * @param string $key |
|
60 | + * @param mixed $fallback |
|
61 | + * @return mixed |
|
62 | + */ |
|
63 | + protected function getUserMeta($key, $fallback) |
|
64 | + { |
|
65 | + $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true); |
|
66 | + return Arr::get($meta, $key, $fallback); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @param string $noticeKey |
|
71 | - * @return string |
|
72 | - */ |
|
73 | - protected function getVersionFor($noticeKey) |
|
74 | - { |
|
75 | - return Arr::get($this->dismissValuesMap, $noticeKey, glsr()->version('major')); |
|
76 | - } |
|
69 | + /** |
|
70 | + * @param string $noticeKey |
|
71 | + * @return string |
|
72 | + */ |
|
73 | + protected function getVersionFor($noticeKey) |
|
74 | + { |
|
75 | + return Arr::get($this->dismissValuesMap, $noticeKey, glsr()->version('major')); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string $screenPostType |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - protected function renderTrustalyzeNotice($screenPostType) |
|
83 | - { |
|
84 | - if (Application::POST_TYPE == $screenPostType |
|
85 | - && Helper::isGreaterThan($this->getVersionFor('trustalyze'), $this->getUserMeta('trustalyze', 0)) |
|
86 | - && !glsr(OptionManager::class)->getBool('settings.general.trustalyze') |
|
87 | - && glsr()->can('manage_options')) { |
|
88 | - glsr()->render('partials/notices/trustalyze'); |
|
89 | - } |
|
90 | - } |
|
78 | + /** |
|
79 | + * @param string $screenPostType |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + protected function renderTrustalyzeNotice($screenPostType) |
|
83 | + { |
|
84 | + if (Application::POST_TYPE == $screenPostType |
|
85 | + && Helper::isGreaterThan($this->getVersionFor('trustalyze'), $this->getUserMeta('trustalyze', 0)) |
|
86 | + && !glsr(OptionManager::class)->getBool('settings.general.trustalyze') |
|
87 | + && glsr()->can('manage_options')) { |
|
88 | + glsr()->render('partials/notices/trustalyze'); |
|
89 | + } |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @param string $screenPostType |
|
94 | - * @return void |
|
95 | - */ |
|
96 | - protected function renderWelcomeNotice($screenPostType) |
|
97 | - { |
|
98 | - if (Application::POST_TYPE == $screenPostType |
|
99 | - && Helper::isGreaterThan($this->getVersionFor('welcome'), $this->getUserMeta('welcome', 0)) |
|
100 | - && glsr()->can('edit_others_posts')) { |
|
101 | - $welcomeText = '0.0.0' == glsr(OptionManager::class)->get('version_upgraded_from') |
|
102 | - ? __('Thanks for installing Site Reviews v%s, we hope you love it!', 'site-reviews') |
|
103 | - : __('Thanks for updating to Site Reviews v%s, we hope you love the changes!', 'site-reviews'); |
|
104 | - glsr()->render('partials/notices/welcome', [ |
|
105 | - 'text' => sprintf($welcomeText, glsr()->version), |
|
106 | - ]); |
|
107 | - } |
|
108 | - } |
|
92 | + /** |
|
93 | + * @param string $screenPostType |
|
94 | + * @return void |
|
95 | + */ |
|
96 | + protected function renderWelcomeNotice($screenPostType) |
|
97 | + { |
|
98 | + if (Application::POST_TYPE == $screenPostType |
|
99 | + && Helper::isGreaterThan($this->getVersionFor('welcome'), $this->getUserMeta('welcome', 0)) |
|
100 | + && glsr()->can('edit_others_posts')) { |
|
101 | + $welcomeText = '0.0.0' == glsr(OptionManager::class)->get('version_upgraded_from') |
|
102 | + ? __('Thanks for installing Site Reviews v%s, we hope you love it!', 'site-reviews') |
|
103 | + : __('Thanks for updating to Site Reviews v%s, we hope you love the changes!', 'site-reviews'); |
|
104 | + glsr()->render('partials/notices/welcome', [ |
|
105 | + 'text' => sprintf($welcomeText, glsr()->version), |
|
106 | + ]); |
|
107 | + } |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * @param string $key |
|
112 | - * @param mixed $fallback |
|
113 | - * @return mixed |
|
114 | - */ |
|
115 | - protected function setUserMeta($key, $value) |
|
116 | - { |
|
117 | - $userId = get_current_user_id(); |
|
118 | - $meta = (array) get_user_meta($userId, static::USER_META_KEY, true); |
|
119 | - $meta = array_filter(wp_parse_args($meta, [])); |
|
120 | - $meta[$key] = $value; |
|
121 | - update_user_meta($userId, static::USER_META_KEY, $meta); |
|
122 | - } |
|
110 | + /** |
|
111 | + * @param string $key |
|
112 | + * @param mixed $fallback |
|
113 | + * @return mixed |
|
114 | + */ |
|
115 | + protected function setUserMeta($key, $value) |
|
116 | + { |
|
117 | + $userId = get_current_user_id(); |
|
118 | + $meta = (array) get_user_meta($userId, static::USER_META_KEY, true); |
|
119 | + $meta = array_filter(wp_parse_args($meta, [])); |
|
120 | + $meta[$key] = $value; |
|
121 | + update_user_meta($userId, static::USER_META_KEY, $meta); |
|
122 | + } |
|
123 | 123 | } |
@@ -10,143 +10,143 @@ |
||
10 | 10 | |
11 | 11 | class Trustalyze |
12 | 12 | { |
13 | - const API_URL = 'https://www.trustalyze.com/api/rbs/'; |
|
14 | - const WEB_URL = 'https://trustalyze.com/plans?ref=105'; |
|
13 | + const API_URL = 'https://www.trustalyze.com/api/rbs/'; |
|
14 | + const WEB_URL = 'https://trustalyze.com/plans?ref=105'; |
|
15 | 15 | |
16 | - public $message; |
|
17 | - public $response; |
|
18 | - public $success; |
|
16 | + public $message; |
|
17 | + public $response; |
|
18 | + public $success; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @return mixed |
|
22 | - */ |
|
23 | - public function __get($key) |
|
24 | - { |
|
25 | - return property_exists($this, $key) |
|
26 | - ? $this->$key |
|
27 | - : Arr::get($this->response, $key, null); |
|
28 | - } |
|
20 | + /** |
|
21 | + * @return mixed |
|
22 | + */ |
|
23 | + public function __get($key) |
|
24 | + { |
|
25 | + return property_exists($this, $key) |
|
26 | + ? $this->$key |
|
27 | + : Arr::get($this->response, $key, null); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return self |
|
32 | - */ |
|
33 | - public function activateKey($apiKey = '', $email = '') |
|
34 | - { |
|
35 | - $this->send('api_key_activation.php', [ |
|
36 | - 'body' => [ |
|
37 | - 'apikey' => $apiKey ?: 0, |
|
38 | - 'domain' => get_home_url(), |
|
39 | - 'email' => $email ?: 0, |
|
40 | - ], |
|
41 | - ]); |
|
42 | - return $this; |
|
43 | - } |
|
30 | + /** |
|
31 | + * @return self |
|
32 | + */ |
|
33 | + public function activateKey($apiKey = '', $email = '') |
|
34 | + { |
|
35 | + $this->send('api_key_activation.php', [ |
|
36 | + 'body' => [ |
|
37 | + 'apikey' => $apiKey ?: 0, |
|
38 | + 'domain' => get_home_url(), |
|
39 | + 'email' => $email ?: 0, |
|
40 | + ], |
|
41 | + ]); |
|
42 | + return $this; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return self |
|
47 | - */ |
|
48 | - public function reset() |
|
49 | - { |
|
50 | - $this->message = ''; |
|
51 | - $this->response = []; |
|
52 | - $this->success = false; |
|
53 | - return $this; |
|
54 | - } |
|
45 | + /** |
|
46 | + * @return self |
|
47 | + */ |
|
48 | + public function reset() |
|
49 | + { |
|
50 | + $this->message = ''; |
|
51 | + $this->response = []; |
|
52 | + $this->success = false; |
|
53 | + return $this; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return self |
|
58 | - */ |
|
59 | - public function sendReview(Review $review) |
|
60 | - { |
|
61 | - $this->send('index.php', [ |
|
62 | - 'body' => $this->getBodyForReview($review), |
|
63 | - 'timeout' => 120, |
|
64 | - ]); |
|
65 | - return $this; |
|
66 | - } |
|
56 | + /** |
|
57 | + * @return self |
|
58 | + */ |
|
59 | + public function sendReview(Review $review) |
|
60 | + { |
|
61 | + $this->send('index.php', [ |
|
62 | + 'body' => $this->getBodyForReview($review), |
|
63 | + 'timeout' => 120, |
|
64 | + ]); |
|
65 | + return $this; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return self |
|
70 | - */ |
|
71 | - public function sendReviewResponse(Review $review) |
|
72 | - { |
|
73 | - $this->send('fetch_customer_reply.php', [ |
|
74 | - 'body' => $this->getBodyForResponse($review), |
|
75 | - ]); |
|
76 | - return $this; |
|
77 | - } |
|
68 | + /** |
|
69 | + * @return self |
|
70 | + */ |
|
71 | + public function sendReviewResponse(Review $review) |
|
72 | + { |
|
73 | + $this->send('fetch_customer_reply.php', [ |
|
74 | + 'body' => $this->getBodyForResponse($review), |
|
75 | + ]); |
|
76 | + return $this; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - protected function getBodyForResponse(Review $review) |
|
83 | - { |
|
84 | - $trustalyzeResponse = [ |
|
85 | - 'reply' => Str::truncate($review->response, 300), |
|
86 | - 'review_id' => glsr(Database::class)->get($review->ID, 'trustalyze'), // this is the trustalyze review ID |
|
87 | - 'review_transaction_id' => $review->review_id, |
|
88 | - 'type' => 'M', |
|
89 | - ]; |
|
90 | - return apply_filters('site-reviews/trustalyze/response', $trustalyzeResponse, $review); |
|
91 | - } |
|
79 | + /** |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + protected function getBodyForResponse(Review $review) |
|
83 | + { |
|
84 | + $trustalyzeResponse = [ |
|
85 | + 'reply' => Str::truncate($review->response, 300), |
|
86 | + 'review_id' => glsr(Database::class)->get($review->ID, 'trustalyze'), // this is the trustalyze review ID |
|
87 | + 'review_transaction_id' => $review->review_id, |
|
88 | + 'type' => 'M', |
|
89 | + ]; |
|
90 | + return apply_filters('site-reviews/trustalyze/response', $trustalyzeResponse, $review); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected function getBodyForReview(Review $review) |
|
97 | - { |
|
98 | - $trustalyzeReview = [ |
|
99 | - 'domain' => get_home_url(), |
|
100 | - 'firstname' => Str::truncate(Str::convertName($review->author, 'first'), 25), |
|
101 | - 'rate' => $review->rating, |
|
102 | - 'review_transaction_id' => $review->review_id, |
|
103 | - 'reviews' => Str::truncate($review->content, 280), |
|
104 | - 'title' => Str::truncate($review->title, 35), |
|
105 | - 'transaction' => Application::ID, // woocommerce field, not needed for Site Reviews |
|
106 | - ]; |
|
107 | - return apply_filters('site-reviews/trustalyze/review', $trustalyzeReview, $review); |
|
108 | - } |
|
93 | + /** |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected function getBodyForReview(Review $review) |
|
97 | + { |
|
98 | + $trustalyzeReview = [ |
|
99 | + 'domain' => get_home_url(), |
|
100 | + 'firstname' => Str::truncate(Str::convertName($review->author, 'first'), 25), |
|
101 | + 'rate' => $review->rating, |
|
102 | + 'review_transaction_id' => $review->review_id, |
|
103 | + 'reviews' => Str::truncate($review->content, 280), |
|
104 | + 'title' => Str::truncate($review->title, 35), |
|
105 | + 'transaction' => Application::ID, // woocommerce field, not needed for Site Reviews |
|
106 | + ]; |
|
107 | + return apply_filters('site-reviews/trustalyze/review', $trustalyzeReview, $review); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * @param \WP_Error|array $response |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - protected function handleResponse($response) |
|
115 | - { |
|
116 | - if (is_wp_error($response)) { |
|
117 | - $this->message = $response->get_error_message(); |
|
118 | - } else { |
|
119 | - $responseBody = wp_remote_retrieve_body($response); |
|
120 | - $responseCode = wp_remote_retrieve_response_code($response); |
|
121 | - $responseData = (array) json_decode($responseBody, true); |
|
122 | - $this->response = array_shift($responseData); |
|
123 | - $this->message = Arr::get($this->response, 'msg'); |
|
124 | - $this->success = 'success' === Arr::get($this->response, 'result') || 'yes' === Arr::get($this->response, 'success'); // @todo remove this ugly hack! |
|
125 | - if (200 !== $responseCode) { |
|
126 | - glsr_log()->error('Bad response code ['.$responseCode.']'); |
|
127 | - } |
|
128 | - if (!$this->success) { |
|
129 | - glsr_log()->error($this->message); |
|
130 | - } |
|
131 | - } |
|
132 | - } |
|
110 | + /** |
|
111 | + * @param \WP_Error|array $response |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + protected function handleResponse($response) |
|
115 | + { |
|
116 | + if (is_wp_error($response)) { |
|
117 | + $this->message = $response->get_error_message(); |
|
118 | + } else { |
|
119 | + $responseBody = wp_remote_retrieve_body($response); |
|
120 | + $responseCode = wp_remote_retrieve_response_code($response); |
|
121 | + $responseData = (array) json_decode($responseBody, true); |
|
122 | + $this->response = array_shift($responseData); |
|
123 | + $this->message = Arr::get($this->response, 'msg'); |
|
124 | + $this->success = 'success' === Arr::get($this->response, 'result') || 'yes' === Arr::get($this->response, 'success'); // @todo remove this ugly hack! |
|
125 | + if (200 !== $responseCode) { |
|
126 | + glsr_log()->error('Bad response code ['.$responseCode.']'); |
|
127 | + } |
|
128 | + if (!$this->success) { |
|
129 | + glsr_log()->error($this->message); |
|
130 | + } |
|
131 | + } |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @param string $endpoint |
|
136 | - * @return void |
|
137 | - */ |
|
138 | - protected function send($endpoint, array $args = []) |
|
139 | - { |
|
140 | - $args = wp_parse_args($args, [ |
|
141 | - 'body' => null, |
|
142 | - 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], |
|
143 | - 'redirection' => 5, |
|
144 | - 'sslverify' => false, |
|
145 | - 'timeout' => 5, |
|
146 | - ]); |
|
147 | - $this->reset(); |
|
148 | - $this->handleResponse( |
|
149 | - wp_remote_post(trailingslashit(static::API_URL).$endpoint, $args) |
|
150 | - ); |
|
151 | - } |
|
134 | + /** |
|
135 | + * @param string $endpoint |
|
136 | + * @return void |
|
137 | + */ |
|
138 | + protected function send($endpoint, array $args = []) |
|
139 | + { |
|
140 | + $args = wp_parse_args($args, [ |
|
141 | + 'body' => null, |
|
142 | + 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], |
|
143 | + 'redirection' => 5, |
|
144 | + 'sslverify' => false, |
|
145 | + 'timeout' => 5, |
|
146 | + ]); |
|
147 | + $this->reset(); |
|
148 | + $this->handleResponse( |
|
149 | + wp_remote_post(trailingslashit(static::API_URL).$endpoint, $args) |
|
150 | + ); |
|
151 | + } |
|
152 | 152 | } |
@@ -8,66 +8,66 @@ |
||
8 | 8 | |
9 | 9 | class Migrate_4_5_0 |
10 | 10 | { |
11 | - /** |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function migrateOptions() |
|
15 | - { |
|
16 | - $isAccountVerified = glsr(OptionManager::class)->getWP('_glsr_rebusify', false); |
|
17 | - update_option('_glsr_trustalyze', $isAccountVerified); |
|
18 | - delete_option('_glsr_rebusify'); |
|
19 | - } |
|
11 | + /** |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function migrateOptions() |
|
15 | + { |
|
16 | + $isAccountVerified = glsr(OptionManager::class)->getWP('_glsr_rebusify', false); |
|
17 | + update_option('_glsr_trustalyze', $isAccountVerified); |
|
18 | + delete_option('_glsr_rebusify'); |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * @return void |
|
23 | - */ |
|
24 | - public function migrateSettings() |
|
25 | - { |
|
26 | - if ($settings = get_option(OptionManager::databaseKey(4))) { |
|
27 | - $settings = Arr::set($settings, 'settings.general.trustalyze', |
|
28 | - Arr::get($settings, 'settings.general.rebusify') |
|
29 | - ); |
|
30 | - $settings = Arr::set($settings, 'settings.general.trustalyze_email', |
|
31 | - Arr::get($settings, 'settings.general.rebusify_email') |
|
32 | - ); |
|
33 | - $settings = Arr::set($settings, 'settings.general.trustalyze_serial', |
|
34 | - Arr::get($settings, 'settings.general.rebusify_serial') |
|
35 | - ); |
|
36 | - unset($settings['settings']['general']['rebusify']); |
|
37 | - unset($settings['settings']['general']['rebusify_email']); |
|
38 | - unset($settings['settings']['general']['rebusify_serial']); |
|
39 | - update_option(OptionManager::databaseKey(4), $settings); |
|
40 | - } |
|
41 | - } |
|
21 | + /** |
|
22 | + * @return void |
|
23 | + */ |
|
24 | + public function migrateSettings() |
|
25 | + { |
|
26 | + if ($settings = get_option(OptionManager::databaseKey(4))) { |
|
27 | + $settings = Arr::set($settings, 'settings.general.trustalyze', |
|
28 | + Arr::get($settings, 'settings.general.rebusify') |
|
29 | + ); |
|
30 | + $settings = Arr::set($settings, 'settings.general.trustalyze_email', |
|
31 | + Arr::get($settings, 'settings.general.rebusify_email') |
|
32 | + ); |
|
33 | + $settings = Arr::set($settings, 'settings.general.trustalyze_serial', |
|
34 | + Arr::get($settings, 'settings.general.rebusify_serial') |
|
35 | + ); |
|
36 | + unset($settings['settings']['general']['rebusify']); |
|
37 | + unset($settings['settings']['general']['rebusify_email']); |
|
38 | + unset($settings['settings']['general']['rebusify_serial']); |
|
39 | + update_option(OptionManager::databaseKey(4), $settings); |
|
40 | + } |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - public function migrateUserMeta() |
|
47 | - { |
|
48 | - $metaKey = NoticeController::USER_META_KEY; |
|
49 | - $userIds = get_users([ |
|
50 | - 'fields' => 'ID', |
|
51 | - 'meta_compare' => 'EXISTS', |
|
52 | - 'meta_key' => $metaKey, |
|
53 | - ]); |
|
54 | - foreach ($userIds as $userId) { |
|
55 | - $meta = (array) get_user_meta($userId, $metaKey, true); |
|
56 | - if (array_key_exists('rebusify', $meta)) { |
|
57 | - $meta['trustalyze'] = $meta['rebusify']; |
|
58 | - unset($meta['rebusify']); |
|
59 | - update_user_meta($userId, $metaKey, $meta); |
|
60 | - } |
|
61 | - } |
|
62 | - } |
|
43 | + /** |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + public function migrateUserMeta() |
|
47 | + { |
|
48 | + $metaKey = NoticeController::USER_META_KEY; |
|
49 | + $userIds = get_users([ |
|
50 | + 'fields' => 'ID', |
|
51 | + 'meta_compare' => 'EXISTS', |
|
52 | + 'meta_key' => $metaKey, |
|
53 | + ]); |
|
54 | + foreach ($userIds as $userId) { |
|
55 | + $meta = (array) get_user_meta($userId, $metaKey, true); |
|
56 | + if (array_key_exists('rebusify', $meta)) { |
|
57 | + $meta['trustalyze'] = $meta['rebusify']; |
|
58 | + unset($meta['rebusify']); |
|
59 | + update_user_meta($userId, $metaKey, $meta); |
|
60 | + } |
|
61 | + } |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function run() |
|
68 | - { |
|
69 | - $this->migrateOptions(); |
|
70 | - $this->migrateSettings(); |
|
71 | - $this->migrateUserMeta(); |
|
72 | - } |
|
64 | + /** |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function run() |
|
68 | + { |
|
69 | + $this->migrateOptions(); |
|
70 | + $this->migrateSettings(); |
|
71 | + $this->migrateUserMeta(); |
|
72 | + } |
|
73 | 73 | } |
@@ -11,6 +11,6 @@ |
||
11 | 11 | */ |
12 | 12 | class Upgrader |
13 | 13 | { |
14 | - public function run() |
|
15 | - {} |
|
14 | + public function run() |
|
15 | + {} |
|
16 | 16 | } |
@@ -4,15 +4,15 @@ |
||
4 | 4 | |
5 | 5 | class SiteReviewsFormShortcode extends Shortcode |
6 | 6 | { |
7 | - protected function hideOptions() |
|
8 | - { |
|
9 | - return [ |
|
10 | - 'rating' => __('Hide the rating field', 'site-reviews'), |
|
11 | - 'title' => __('Hide the title field', 'site-reviews'), |
|
12 | - 'content' => __('Hide the review field', 'site-reviews'), |
|
13 | - 'name' => __('Hide the name field', 'site-reviews'), |
|
14 | - 'email' => __('Hide the email field', 'site-reviews'), |
|
15 | - 'terms' => __('Hide the terms field', 'site-reviews'), |
|
16 | - ]; |
|
17 | - } |
|
7 | + protected function hideOptions() |
|
8 | + { |
|
9 | + return [ |
|
10 | + 'rating' => __('Hide the rating field', 'site-reviews'), |
|
11 | + 'title' => __('Hide the title field', 'site-reviews'), |
|
12 | + 'content' => __('Hide the review field', 'site-reviews'), |
|
13 | + 'name' => __('Hide the name field', 'site-reviews'), |
|
14 | + 'email' => __('Hide the email field', 'site-reviews'), |
|
15 | + 'terms' => __('Hide the terms field', 'site-reviews'), |
|
16 | + ]; |
|
17 | + } |
|
18 | 18 | } |
@@ -12,238 +12,238 @@ |
||
12 | 12 | |
13 | 13 | abstract class Shortcode implements ShortcodeContract |
14 | 14 | { |
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - protected $partialName; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $shortcodeName; |
|
24 | - |
|
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->partialName = $this->getShortcodePartialName(); |
|
28 | - $this->shortcodeName = $this->getShortcodeName(); |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @param string|array $atts |
|
33 | - * @param string $type |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - public function build($atts, array $args = [], $type = 'shortcode') |
|
37 | - { |
|
38 | - $args = $this->normalizeArgs($args, $type); |
|
39 | - $atts = $this->normalizeAtts($atts, $type); |
|
40 | - $partial = glsr(Partial::class)->build($this->partialName, $atts); |
|
41 | - $title = !empty($atts['title']) |
|
42 | - ? $args['before_title'].$atts['title'].$args['after_title'] |
|
43 | - : ''; |
|
44 | - $debug = sprintf('<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json']); |
|
45 | - return $args['before_widget'].$title.$partial.$debug.$args['after_widget']; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @param string|array $atts |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function buildShortcode($atts = []) |
|
53 | - { |
|
54 | - return $this->build($atts); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - public function getDefaults($atts) |
|
61 | - { |
|
62 | - return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts)); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @return array |
|
67 | - */ |
|
68 | - public function getHideOptions() |
|
69 | - { |
|
70 | - $options = $this->hideOptions(); |
|
71 | - return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function getShortcodeClassName($replace = '', $search = 'Shortcode') |
|
78 | - { |
|
79 | - return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function getShortcodeDefaultsClassName() |
|
86 | - { |
|
87 | - return Helper::buildClassName( |
|
88 | - $this->getShortcodeClassName('Defaults'), |
|
89 | - 'Defaults' |
|
90 | - ); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - public function getShortcodeName() |
|
97 | - { |
|
98 | - return Str::snakeCase($this->getShortcodeClassName()); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - public function getShortcodePartialName() |
|
105 | - { |
|
106 | - return Str::dashCase($this->getShortcodeClassName()); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @param array|string $args |
|
111 | - * @param string $type |
|
112 | - * @return array |
|
113 | - */ |
|
114 | - public function normalizeArgs($args, $type = 'shortcode') |
|
115 | - { |
|
116 | - $args = wp_parse_args($args, [ |
|
117 | - 'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">', |
|
118 | - 'after_widget' => '</div>', |
|
119 | - 'before_title' => '<h3 class="glsr-'.$type.'-title">', |
|
120 | - 'after_title' => '</h3>', |
|
121 | - ]); |
|
122 | - return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param array|string $atts |
|
127 | - * @param string $type |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function normalizeAtts($atts, $type = 'shortcode') |
|
131 | - { |
|
132 | - $atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName); |
|
133 | - $atts = $this->getDefaults($atts); |
|
134 | - array_walk($atts, function (&$value, $key) { |
|
135 | - $methodName = Helper::buildMethodName($key, 'normalize'); |
|
136 | - if (!method_exists($this, $methodName)) { |
|
137 | - return; |
|
138 | - } |
|
139 | - $value = $this->$methodName($value); |
|
140 | - }); |
|
141 | - $this->setId($atts); |
|
142 | - return $atts; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return array |
|
147 | - */ |
|
148 | - abstract protected function hideOptions(); |
|
149 | - |
|
150 | - /** |
|
151 | - * @param string $postId |
|
152 | - * @return int|string |
|
153 | - */ |
|
154 | - protected function normalizeAssignedTo($postId) |
|
155 | - { |
|
156 | - if ('parent_id' == $postId) { |
|
157 | - $postId = intval(wp_get_post_parent_id(intval(get_the_ID()))); |
|
158 | - } elseif ('post_id' == $postId) { |
|
159 | - $postId = intval(get_the_ID()); |
|
160 | - } |
|
161 | - return $postId; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $postId |
|
166 | - * @return int|string |
|
167 | - */ |
|
168 | - protected function normalizeAssignTo($postId) |
|
169 | - { |
|
170 | - return $this->normalizeAssignedTo($postId); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param string|array $hide |
|
175 | - * @return array |
|
176 | - */ |
|
177 | - protected function normalizeHide($hide) |
|
178 | - { |
|
179 | - if (is_string($hide)) { |
|
180 | - $hide = explode(',', $hide); |
|
181 | - } |
|
182 | - $hideKeys = array_keys($this->getHideOptions()); |
|
183 | - return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) { |
|
184 | - return in_array($value, $hideKeys); |
|
185 | - }); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @param string $id |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - protected function normalizeId($id) |
|
193 | - { |
|
194 | - return sanitize_title($id); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param string $labels |
|
199 | - * @return array |
|
200 | - */ |
|
201 | - protected function normalizeLabels($labels) |
|
202 | - { |
|
203 | - $defaults = [ |
|
204 | - __('Excellent', 'site-reviews'), |
|
205 | - __('Very good', 'site-reviews'), |
|
206 | - __('Average', 'site-reviews'), |
|
207 | - __('Poor', 'site-reviews'), |
|
208 | - __('Terrible', 'site-reviews'), |
|
209 | - ]; |
|
210 | - $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class); |
|
211 | - $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
|
212 | - $labels = array_map('trim', explode(',', $labels)); |
|
213 | - foreach ($defaults as $i => $label) { |
|
214 | - if (empty($labels[$i])) { |
|
215 | - continue; |
|
216 | - } |
|
217 | - $defaults[$i] = $labels[$i]; |
|
218 | - } |
|
219 | - return array_combine(range($maxRating, 1), $defaults); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @param string $schema |
|
224 | - * @return bool |
|
225 | - */ |
|
226 | - protected function normalizeSchema($schema) |
|
227 | - { |
|
228 | - return wp_validate_boolean($schema); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @param string $text |
|
233 | - * @return string |
|
234 | - */ |
|
235 | - protected function normalizeText($text) |
|
236 | - { |
|
237 | - return trim($text); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @return void |
|
242 | - */ |
|
243 | - protected function setId(array &$atts) |
|
244 | - { |
|
245 | - if (empty($atts['id'])) { |
|
246 | - $atts['id'] = Application::PREFIX.substr(md5(serialize($atts)), 0, 8); |
|
247 | - } |
|
248 | - } |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + protected $partialName; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $shortcodeName; |
|
24 | + |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->partialName = $this->getShortcodePartialName(); |
|
28 | + $this->shortcodeName = $this->getShortcodeName(); |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @param string|array $atts |
|
33 | + * @param string $type |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + public function build($atts, array $args = [], $type = 'shortcode') |
|
37 | + { |
|
38 | + $args = $this->normalizeArgs($args, $type); |
|
39 | + $atts = $this->normalizeAtts($atts, $type); |
|
40 | + $partial = glsr(Partial::class)->build($this->partialName, $atts); |
|
41 | + $title = !empty($atts['title']) |
|
42 | + ? $args['before_title'].$atts['title'].$args['after_title'] |
|
43 | + : ''; |
|
44 | + $debug = sprintf('<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json']); |
|
45 | + return $args['before_widget'].$title.$partial.$debug.$args['after_widget']; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @param string|array $atts |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function buildShortcode($atts = []) |
|
53 | + { |
|
54 | + return $this->build($atts); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + public function getDefaults($atts) |
|
61 | + { |
|
62 | + return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts)); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @return array |
|
67 | + */ |
|
68 | + public function getHideOptions() |
|
69 | + { |
|
70 | + $options = $this->hideOptions(); |
|
71 | + return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function getShortcodeClassName($replace = '', $search = 'Shortcode') |
|
78 | + { |
|
79 | + return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function getShortcodeDefaultsClassName() |
|
86 | + { |
|
87 | + return Helper::buildClassName( |
|
88 | + $this->getShortcodeClassName('Defaults'), |
|
89 | + 'Defaults' |
|
90 | + ); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + public function getShortcodeName() |
|
97 | + { |
|
98 | + return Str::snakeCase($this->getShortcodeClassName()); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + public function getShortcodePartialName() |
|
105 | + { |
|
106 | + return Str::dashCase($this->getShortcodeClassName()); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @param array|string $args |
|
111 | + * @param string $type |
|
112 | + * @return array |
|
113 | + */ |
|
114 | + public function normalizeArgs($args, $type = 'shortcode') |
|
115 | + { |
|
116 | + $args = wp_parse_args($args, [ |
|
117 | + 'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">', |
|
118 | + 'after_widget' => '</div>', |
|
119 | + 'before_title' => '<h3 class="glsr-'.$type.'-title">', |
|
120 | + 'after_title' => '</h3>', |
|
121 | + ]); |
|
122 | + return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param array|string $atts |
|
127 | + * @param string $type |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function normalizeAtts($atts, $type = 'shortcode') |
|
131 | + { |
|
132 | + $atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName); |
|
133 | + $atts = $this->getDefaults($atts); |
|
134 | + array_walk($atts, function (&$value, $key) { |
|
135 | + $methodName = Helper::buildMethodName($key, 'normalize'); |
|
136 | + if (!method_exists($this, $methodName)) { |
|
137 | + return; |
|
138 | + } |
|
139 | + $value = $this->$methodName($value); |
|
140 | + }); |
|
141 | + $this->setId($atts); |
|
142 | + return $atts; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return array |
|
147 | + */ |
|
148 | + abstract protected function hideOptions(); |
|
149 | + |
|
150 | + /** |
|
151 | + * @param string $postId |
|
152 | + * @return int|string |
|
153 | + */ |
|
154 | + protected function normalizeAssignedTo($postId) |
|
155 | + { |
|
156 | + if ('parent_id' == $postId) { |
|
157 | + $postId = intval(wp_get_post_parent_id(intval(get_the_ID()))); |
|
158 | + } elseif ('post_id' == $postId) { |
|
159 | + $postId = intval(get_the_ID()); |
|
160 | + } |
|
161 | + return $postId; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $postId |
|
166 | + * @return int|string |
|
167 | + */ |
|
168 | + protected function normalizeAssignTo($postId) |
|
169 | + { |
|
170 | + return $this->normalizeAssignedTo($postId); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param string|array $hide |
|
175 | + * @return array |
|
176 | + */ |
|
177 | + protected function normalizeHide($hide) |
|
178 | + { |
|
179 | + if (is_string($hide)) { |
|
180 | + $hide = explode(',', $hide); |
|
181 | + } |
|
182 | + $hideKeys = array_keys($this->getHideOptions()); |
|
183 | + return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) { |
|
184 | + return in_array($value, $hideKeys); |
|
185 | + }); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @param string $id |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + protected function normalizeId($id) |
|
193 | + { |
|
194 | + return sanitize_title($id); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param string $labels |
|
199 | + * @return array |
|
200 | + */ |
|
201 | + protected function normalizeLabels($labels) |
|
202 | + { |
|
203 | + $defaults = [ |
|
204 | + __('Excellent', 'site-reviews'), |
|
205 | + __('Very good', 'site-reviews'), |
|
206 | + __('Average', 'site-reviews'), |
|
207 | + __('Poor', 'site-reviews'), |
|
208 | + __('Terrible', 'site-reviews'), |
|
209 | + ]; |
|
210 | + $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class); |
|
211 | + $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
|
212 | + $labels = array_map('trim', explode(',', $labels)); |
|
213 | + foreach ($defaults as $i => $label) { |
|
214 | + if (empty($labels[$i])) { |
|
215 | + continue; |
|
216 | + } |
|
217 | + $defaults[$i] = $labels[$i]; |
|
218 | + } |
|
219 | + return array_combine(range($maxRating, 1), $defaults); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @param string $schema |
|
224 | + * @return bool |
|
225 | + */ |
|
226 | + protected function normalizeSchema($schema) |
|
227 | + { |
|
228 | + return wp_validate_boolean($schema); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @param string $text |
|
233 | + * @return string |
|
234 | + */ |
|
235 | + protected function normalizeText($text) |
|
236 | + { |
|
237 | + return trim($text); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @return void |
|
242 | + */ |
|
243 | + protected function setId(array &$atts) |
|
244 | + { |
|
245 | + if (empty($atts['id'])) { |
|
246 | + $atts['id'] = Application::PREFIX.substr(md5(serialize($atts)), 0, 8); |
|
247 | + } |
|
248 | + } |
|
249 | 249 | } |
@@ -10,197 +10,197 @@ |
||
10 | 10 | |
11 | 11 | class SiteReviewsSummary |
12 | 12 | { |
13 | - /** |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - protected $args; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var float |
|
20 | - */ |
|
21 | - protected $averageRating; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $ratingCounts; |
|
27 | - |
|
28 | - /** |
|
29 | - * @return void|string |
|
30 | - */ |
|
31 | - public function build(array $args = []) |
|
32 | - { |
|
33 | - $this->args = $args; |
|
34 | - $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
35 | - if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
36 | - return; |
|
37 | - } |
|
38 | - $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
39 | - $this->generateSchema(); |
|
40 | - return glsr(Template::class)->build('templates/reviews-summary', [ |
|
41 | - 'context' => [ |
|
42 | - 'assigned_to' => $this->args['assigned_to'], |
|
43 | - 'category' => $this->args['category'], |
|
44 | - 'class' => $this->getClass(), |
|
45 | - 'id' => $this->args['id'], |
|
46 | - 'percentages' => $this->buildPercentage(), |
|
47 | - 'rating' => $this->buildRating(), |
|
48 | - 'stars' => $this->buildStars(), |
|
49 | - 'text' => $this->buildText(), |
|
50 | - ], |
|
51 | - ]); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @return void|string |
|
56 | - */ |
|
57 | - protected function buildPercentage() |
|
58 | - { |
|
59 | - if ($this->isHidden('bars')) { |
|
60 | - return; |
|
61 | - } |
|
62 | - $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
63 | - $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
64 | - $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
65 | - $background = $this->buildPercentageBackground($percentages[$level]); |
|
66 | - $count = apply_filters('site-reviews/summary/counts', |
|
67 | - $percentages[$level], |
|
68 | - $this->ratingCounts[$level] |
|
69 | - ); |
|
70 | - $percent = $this->buildPercentageCount($count); |
|
71 | - $value = $label.$background.$percent; |
|
72 | - $value = apply_filters('site-reviews/summary/wrap/bar', $value, $this->args, [ |
|
73 | - 'percent' => wp_strip_all_tags($count, true), |
|
74 | - 'rating' => $level, |
|
75 | - ]); |
|
76 | - return $carry.glsr(Builder::class)->div($value, [ |
|
77 | - 'class' => 'glsr-bar', |
|
78 | - ]); |
|
79 | - }); |
|
80 | - return $this->wrap('percentage', $bars); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @param string $percent |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - protected function buildPercentageBackground($percent) |
|
88 | - { |
|
89 | - $backgroundPercent = glsr(Builder::class)->span([ |
|
90 | - 'class' => 'glsr-bar-background-percent', |
|
91 | - 'style' => 'width:'.$percent, |
|
92 | - ]); |
|
93 | - return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param string $count |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - protected function buildPercentageCount($count) |
|
101 | - { |
|
102 | - return '<span class="glsr-bar-percent">'.$count.'</span>'; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $label |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - protected function buildPercentageLabel($label) |
|
110 | - { |
|
111 | - return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return void|string |
|
116 | - */ |
|
117 | - protected function buildRating() |
|
118 | - { |
|
119 | - if ($this->isHidden('rating')) { |
|
120 | - return; |
|
121 | - } |
|
122 | - return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @return void|string |
|
127 | - */ |
|
128 | - protected function buildStars() |
|
129 | - { |
|
130 | - if ($this->isHidden('stars')) { |
|
131 | - return; |
|
132 | - } |
|
133 | - $stars = glsr_star_rating($this->averageRating); |
|
134 | - return $this->wrap('stars', $stars); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return void|string |
|
139 | - */ |
|
140 | - protected function buildText() |
|
141 | - { |
|
142 | - if ($this->isHidden('summary')) { |
|
143 | - return; |
|
144 | - } |
|
145 | - $count = intval(array_sum($this->ratingCounts)); |
|
146 | - if (empty($this->args['text'])) { |
|
147 | - // @todo document this change |
|
148 | - $this->args['text'] = _nx( |
|
149 | - '{rating} out of {max} stars (based on {num} review)', |
|
150 | - '{rating} out of {max} stars (based on {num} reviews)', |
|
151 | - $count, |
|
152 | - 'Do not translate {rating}, {max}, and {num}, they are template tags.', |
|
153 | - 'site-reviews' |
|
154 | - ); |
|
155 | - } |
|
156 | - $summary = str_replace( |
|
157 | - ['{rating}', '{max}', '{num}'], |
|
158 | - [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
159 | - $this->args['text'] |
|
160 | - ); |
|
161 | - return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - protected function generateSchema() |
|
168 | - { |
|
169 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
170 | - return; |
|
171 | - } |
|
172 | - glsr(Schema::class)->store( |
|
173 | - glsr(Schema::class)->buildSummary($this->args) |
|
174 | - ); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - protected function getClass() |
|
181 | - { |
|
182 | - return trim('glsr-summary glsr-default '.$this->args['class']); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @param string $key |
|
187 | - * @return bool |
|
188 | - */ |
|
189 | - protected function isHidden($key) |
|
190 | - { |
|
191 | - return in_array($key, $this->args['hide']); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @param string $key |
|
196 | - * @param string $value |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - protected function wrap($key, $value) |
|
200 | - { |
|
201 | - $value = apply_filters('site-reviews/summary/wrap/'.$key, $value, $this->args); |
|
202 | - return glsr(Builder::class)->div($value, [ |
|
203 | - 'class' => 'glsr-summary-'.$key, |
|
204 | - ]); |
|
205 | - } |
|
13 | + /** |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + protected $args; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var float |
|
20 | + */ |
|
21 | + protected $averageRating; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $ratingCounts; |
|
27 | + |
|
28 | + /** |
|
29 | + * @return void|string |
|
30 | + */ |
|
31 | + public function build(array $args = []) |
|
32 | + { |
|
33 | + $this->args = $args; |
|
34 | + $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
35 | + if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
36 | + return; |
|
37 | + } |
|
38 | + $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
39 | + $this->generateSchema(); |
|
40 | + return glsr(Template::class)->build('templates/reviews-summary', [ |
|
41 | + 'context' => [ |
|
42 | + 'assigned_to' => $this->args['assigned_to'], |
|
43 | + 'category' => $this->args['category'], |
|
44 | + 'class' => $this->getClass(), |
|
45 | + 'id' => $this->args['id'], |
|
46 | + 'percentages' => $this->buildPercentage(), |
|
47 | + 'rating' => $this->buildRating(), |
|
48 | + 'stars' => $this->buildStars(), |
|
49 | + 'text' => $this->buildText(), |
|
50 | + ], |
|
51 | + ]); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @return void|string |
|
56 | + */ |
|
57 | + protected function buildPercentage() |
|
58 | + { |
|
59 | + if ($this->isHidden('bars')) { |
|
60 | + return; |
|
61 | + } |
|
62 | + $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
63 | + $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
64 | + $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
65 | + $background = $this->buildPercentageBackground($percentages[$level]); |
|
66 | + $count = apply_filters('site-reviews/summary/counts', |
|
67 | + $percentages[$level], |
|
68 | + $this->ratingCounts[$level] |
|
69 | + ); |
|
70 | + $percent = $this->buildPercentageCount($count); |
|
71 | + $value = $label.$background.$percent; |
|
72 | + $value = apply_filters('site-reviews/summary/wrap/bar', $value, $this->args, [ |
|
73 | + 'percent' => wp_strip_all_tags($count, true), |
|
74 | + 'rating' => $level, |
|
75 | + ]); |
|
76 | + return $carry.glsr(Builder::class)->div($value, [ |
|
77 | + 'class' => 'glsr-bar', |
|
78 | + ]); |
|
79 | + }); |
|
80 | + return $this->wrap('percentage', $bars); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @param string $percent |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + protected function buildPercentageBackground($percent) |
|
88 | + { |
|
89 | + $backgroundPercent = glsr(Builder::class)->span([ |
|
90 | + 'class' => 'glsr-bar-background-percent', |
|
91 | + 'style' => 'width:'.$percent, |
|
92 | + ]); |
|
93 | + return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param string $count |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + protected function buildPercentageCount($count) |
|
101 | + { |
|
102 | + return '<span class="glsr-bar-percent">'.$count.'</span>'; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $label |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + protected function buildPercentageLabel($label) |
|
110 | + { |
|
111 | + return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return void|string |
|
116 | + */ |
|
117 | + protected function buildRating() |
|
118 | + { |
|
119 | + if ($this->isHidden('rating')) { |
|
120 | + return; |
|
121 | + } |
|
122 | + return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @return void|string |
|
127 | + */ |
|
128 | + protected function buildStars() |
|
129 | + { |
|
130 | + if ($this->isHidden('stars')) { |
|
131 | + return; |
|
132 | + } |
|
133 | + $stars = glsr_star_rating($this->averageRating); |
|
134 | + return $this->wrap('stars', $stars); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return void|string |
|
139 | + */ |
|
140 | + protected function buildText() |
|
141 | + { |
|
142 | + if ($this->isHidden('summary')) { |
|
143 | + return; |
|
144 | + } |
|
145 | + $count = intval(array_sum($this->ratingCounts)); |
|
146 | + if (empty($this->args['text'])) { |
|
147 | + // @todo document this change |
|
148 | + $this->args['text'] = _nx( |
|
149 | + '{rating} out of {max} stars (based on {num} review)', |
|
150 | + '{rating} out of {max} stars (based on {num} reviews)', |
|
151 | + $count, |
|
152 | + 'Do not translate {rating}, {max}, and {num}, they are template tags.', |
|
153 | + 'site-reviews' |
|
154 | + ); |
|
155 | + } |
|
156 | + $summary = str_replace( |
|
157 | + ['{rating}', '{max}', '{num}'], |
|
158 | + [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
159 | + $this->args['text'] |
|
160 | + ); |
|
161 | + return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + protected function generateSchema() |
|
168 | + { |
|
169 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
170 | + return; |
|
171 | + } |
|
172 | + glsr(Schema::class)->store( |
|
173 | + glsr(Schema::class)->buildSummary($this->args) |
|
174 | + ); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + protected function getClass() |
|
181 | + { |
|
182 | + return trim('glsr-summary glsr-default '.$this->args['class']); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @param string $key |
|
187 | + * @return bool |
|
188 | + */ |
|
189 | + protected function isHidden($key) |
|
190 | + { |
|
191 | + return in_array($key, $this->args['hide']); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @param string $key |
|
196 | + * @param string $value |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + protected function wrap($key, $value) |
|
200 | + { |
|
201 | + $value = apply_filters('site-reviews/summary/wrap/'.$key, $value, $this->args); |
|
202 | + return glsr(Builder::class)->div($value, [ |
|
203 | + 'class' => 'glsr-summary-'.$key, |
|
204 | + ]); |
|
205 | + } |
|
206 | 206 | } |
@@ -7,86 +7,86 @@ |
||
7 | 7 | |
8 | 8 | class Template |
9 | 9 | { |
10 | - /** |
|
11 | - * @param string $templatePath |
|
12 | - * @return void|string |
|
13 | - */ |
|
14 | - public function build($templatePath, array $data = []) |
|
15 | - { |
|
16 | - $data = $this->normalize($data); |
|
17 | - $path = Str::removePrefix('templates/', $templatePath); |
|
18 | - $template = glsr()->build($templatePath, $data); |
|
19 | - $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
20 | - $template = $this->interpolate($template, $data, $path); |
|
21 | - $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
22 | - $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
23 | - return $template; |
|
24 | - } |
|
10 | + /** |
|
11 | + * @param string $templatePath |
|
12 | + * @return void|string |
|
13 | + */ |
|
14 | + public function build($templatePath, array $data = []) |
|
15 | + { |
|
16 | + $data = $this->normalize($data); |
|
17 | + $path = Str::removePrefix('templates/', $templatePath); |
|
18 | + $template = glsr()->build($templatePath, $data); |
|
19 | + $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
20 | + $template = $this->interpolate($template, $data, $path); |
|
21 | + $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
22 | + $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
23 | + return $template; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Interpolate context values into template placeholders. |
|
28 | - * @param string $template |
|
29 | - * @param string $templatePath |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - public function interpolate($template, array $data = [], $templatePath) |
|
33 | - { |
|
34 | - $context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
35 | - $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data); |
|
36 | - return $this->interpolateContext($template, $context); |
|
37 | - } |
|
26 | + /** |
|
27 | + * Interpolate context values into template placeholders. |
|
28 | + * @param string $template |
|
29 | + * @param string $templatePath |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + public function interpolate($template, array $data = [], $templatePath) |
|
33 | + { |
|
34 | + $context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
35 | + $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data); |
|
36 | + return $this->interpolateContext($template, $context); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Interpolate context values into template placeholders. |
|
41 | - * @param string $text |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - public function interpolateContext($text, array $context = []) |
|
45 | - { |
|
46 | - foreach ($context as $key => $value) { |
|
47 | - $text = strtr( |
|
48 | - $text, |
|
49 | - array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
50 | - ); |
|
51 | - } |
|
52 | - return trim($text); |
|
53 | - } |
|
39 | + /** |
|
40 | + * Interpolate context values into template placeholders. |
|
41 | + * @param string $text |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + public function interpolateContext($text, array $context = []) |
|
45 | + { |
|
46 | + foreach ($context as $key => $value) { |
|
47 | + $text = strtr( |
|
48 | + $text, |
|
49 | + array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
50 | + ); |
|
51 | + } |
|
52 | + return trim($text); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param string $templatePath |
|
57 | - * @return void|string |
|
58 | - */ |
|
59 | - public function render($templatePath, array $data = []) |
|
60 | - { |
|
61 | - echo $this->build($templatePath, $data); |
|
62 | - } |
|
55 | + /** |
|
56 | + * @param string $templatePath |
|
57 | + * @return void|string |
|
58 | + */ |
|
59 | + public function render($templatePath, array $data = []) |
|
60 | + { |
|
61 | + echo $this->build($templatePath, $data); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - protected function normalize(array $data) |
|
68 | - { |
|
69 | - $arrayKeys = ['context', 'globals']; |
|
70 | - $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
71 | - foreach ($arrayKeys as $key) { |
|
72 | - if (is_array($data[$key])) { |
|
73 | - continue; |
|
74 | - } |
|
75 | - $data[$key] = []; |
|
76 | - } |
|
77 | - return $data; |
|
78 | - } |
|
64 | + /** |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + protected function normalize(array $data) |
|
68 | + { |
|
69 | + $arrayKeys = ['context', 'globals']; |
|
70 | + $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
71 | + foreach ($arrayKeys as $key) { |
|
72 | + if (is_array($data[$key])) { |
|
73 | + continue; |
|
74 | + } |
|
75 | + $data[$key] = []; |
|
76 | + } |
|
77 | + return $data; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - protected function normalizeContext(array $context) |
|
84 | - { |
|
85 | - $context = array_filter($context, function ($value) { |
|
86 | - return !is_array($value) && !is_object($value); |
|
87 | - }); |
|
88 | - return array_map(function ($value) { |
|
89 | - return (string) $value; |
|
90 | - }, $context); |
|
91 | - } |
|
80 | + /** |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + protected function normalizeContext(array $context) |
|
84 | + { |
|
85 | + $context = array_filter($context, function ($value) { |
|
86 | + return !is_array($value) && !is_object($value); |
|
87 | + }); |
|
88 | + return array_map(function ($value) { |
|
89 | + return (string) $value; |
|
90 | + }, $context); |
|
91 | + } |
|
92 | 92 | } |
@@ -4,215 +4,215 @@ |
||
4 | 4 | |
5 | 5 | class Arr |
6 | 6 | { |
7 | - /** |
|
8 | - * @return bool |
|
9 | - */ |
|
10 | - public static function compareArrays(array $arr1, array $arr2) |
|
11 | - { |
|
12 | - sort($arr1); |
|
13 | - sort($arr2); |
|
14 | - return $arr1 == $arr2; |
|
15 | - } |
|
16 | - |
|
17 | - /** |
|
18 | - * @param mixed $array |
|
19 | - * @return array |
|
20 | - */ |
|
21 | - public static function consolidateArray($array) |
|
22 | - { |
|
23 | - return is_array($array) || is_object($array) |
|
24 | - ? (array) $array |
|
25 | - : []; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public static function convertDotNotationArray(array $array) |
|
32 | - { |
|
33 | - $results = []; |
|
34 | - foreach ($array as $path => $value) { |
|
35 | - $results = static::set($results, $path, $value); |
|
36 | - } |
|
37 | - return $results; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @param string $string |
|
42 | - * @param mixed $callback |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public static function convertStringToArray($string, $callback = null) |
|
46 | - { |
|
47 | - $array = array_map('trim', explode(',', $string)); |
|
48 | - return $callback |
|
49 | - ? array_filter($array, $callback) |
|
50 | - : array_filter($array); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param bool $flattenValue |
|
55 | - * @param string $prefix |
|
56 | - * @return array |
|
57 | - */ |
|
58 | - public static function flattenArray(array $array, $flattenValue = false, $prefix = '') |
|
59 | - { |
|
60 | - $result = []; |
|
61 | - foreach ($array as $key => $value) { |
|
62 | - $newKey = ltrim($prefix.'.'.$key, '.'); |
|
63 | - if (static::isIndexedFlatArray($value)) { |
|
64 | - if ($flattenValue) { |
|
65 | - $value = '['.implode(', ', $value).']'; |
|
66 | - } |
|
67 | - } elseif (is_array($value)) { |
|
68 | - $result = array_merge($result, static::flattenArray($value, $flattenValue, $newKey)); |
|
69 | - continue; |
|
70 | - } |
|
71 | - $result[$newKey] = $value; |
|
72 | - } |
|
73 | - return $result; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Get a value from an array of values using a dot-notation path as reference. |
|
78 | - * @param mixed $data |
|
79 | - * @param string $path |
|
80 | - * @param mixed $fallback |
|
81 | - * @return mixed |
|
82 | - */ |
|
83 | - public static function get($data, $path = '', $fallback = '') |
|
84 | - { |
|
85 | - $data = static::consolidateArray($data); |
|
86 | - $keys = explode('.', $path); |
|
87 | - foreach ($keys as $key) { |
|
88 | - if (!isset($data[$key])) { |
|
89 | - return $fallback; |
|
90 | - } |
|
91 | - $data = $data[$key]; |
|
92 | - } |
|
93 | - return $data; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param string $key |
|
98 | - * @return array |
|
99 | - */ |
|
100 | - public static function insertAfter($key, array $array, array $insert) |
|
101 | - { |
|
102 | - return static::insertInArray($array, $insert, $key, 'after'); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $key |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public static function insertBefore($key, array $array, array $insert) |
|
110 | - { |
|
111 | - return static::insertInArray($array, $insert, $key, 'before'); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param string $key |
|
116 | - * @param string $position |
|
117 | - * @return array |
|
118 | - */ |
|
119 | - public static function insertInArray(array $array, array $insert, $key, $position = 'before') |
|
120 | - { |
|
121 | - $keyPosition = intval(array_search($key, array_keys($array))); |
|
122 | - if ('after' == $position) { |
|
123 | - ++$keyPosition; |
|
124 | - } |
|
125 | - if (false !== $keyPosition) { |
|
126 | - $result = array_slice($array, 0, $keyPosition); |
|
127 | - $result = array_merge($result, $insert); |
|
128 | - return array_merge($result, array_slice($array, $keyPosition)); |
|
129 | - } |
|
130 | - return array_merge($array, $insert); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @param mixed $array |
|
135 | - * @return bool |
|
136 | - */ |
|
137 | - public static function isIndexedFlatArray($array) |
|
138 | - { |
|
139 | - if (!is_array($array) || array_filter($array, 'is_array')) { |
|
140 | - return false; |
|
141 | - } |
|
142 | - return wp_is_numeric_array($array); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param bool $prefixed |
|
147 | - * @return array |
|
148 | - */ |
|
149 | - public static function prefixArrayKeys(array $values, $prefixed = true) |
|
150 | - { |
|
151 | - $trim = '_'; |
|
152 | - $prefix = $prefixed |
|
153 | - ? $trim |
|
154 | - : ''; |
|
155 | - $prefixed = []; |
|
156 | - foreach ($values as $key => $value) { |
|
157 | - $key = trim($key); |
|
158 | - if (0 === strpos($key, $trim)) { |
|
159 | - $key = substr($key, strlen($trim)); |
|
160 | - } |
|
161 | - $prefixed[$prefix.$key] = $value; |
|
162 | - } |
|
163 | - return $prefixed; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return array |
|
168 | - */ |
|
169 | - public static function removeEmptyArrayValues(array $array) |
|
170 | - { |
|
171 | - $result = []; |
|
172 | - foreach ($array as $key => $value) { |
|
173 | - if (!$value) { |
|
174 | - continue; |
|
175 | - } |
|
176 | - $result[$key] = is_array($value) |
|
177 | - ? static::removeEmptyArrayValues($value) |
|
178 | - : $value; |
|
179 | - } |
|
180 | - return $result; |
|
181 | - } |
|
182 | - |
|
183 | - |
|
184 | - /** |
|
185 | - * Set a value to an array of values using a dot-notation path as reference. |
|
186 | - * @param string $path |
|
187 | - * @param mixed $value |
|
188 | - * @return array |
|
189 | - */ |
|
190 | - public static function set(array $data, $path, $value) |
|
191 | - { |
|
192 | - $token = strtok($path, '.'); |
|
193 | - $ref = &$data; |
|
194 | - while (false !== $token) { |
|
195 | - $ref = static::consolidateArray($ref); |
|
196 | - $ref = &$ref[$token]; |
|
197 | - $token = strtok('.'); |
|
198 | - } |
|
199 | - $ref = $value; |
|
200 | - return $data; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @return array |
|
205 | - */ |
|
206 | - public static function unique(array $values) |
|
207 | - { |
|
208 | - return array_filter(array_unique($values)); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @return array |
|
213 | - */ |
|
214 | - public static function unprefixArrayKeys(array $values) |
|
215 | - { |
|
216 | - return static::prefixArrayKeys($values, false); |
|
217 | - } |
|
7 | + /** |
|
8 | + * @return bool |
|
9 | + */ |
|
10 | + public static function compareArrays(array $arr1, array $arr2) |
|
11 | + { |
|
12 | + sort($arr1); |
|
13 | + sort($arr2); |
|
14 | + return $arr1 == $arr2; |
|
15 | + } |
|
16 | + |
|
17 | + /** |
|
18 | + * @param mixed $array |
|
19 | + * @return array |
|
20 | + */ |
|
21 | + public static function consolidateArray($array) |
|
22 | + { |
|
23 | + return is_array($array) || is_object($array) |
|
24 | + ? (array) $array |
|
25 | + : []; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public static function convertDotNotationArray(array $array) |
|
32 | + { |
|
33 | + $results = []; |
|
34 | + foreach ($array as $path => $value) { |
|
35 | + $results = static::set($results, $path, $value); |
|
36 | + } |
|
37 | + return $results; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @param string $string |
|
42 | + * @param mixed $callback |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public static function convertStringToArray($string, $callback = null) |
|
46 | + { |
|
47 | + $array = array_map('trim', explode(',', $string)); |
|
48 | + return $callback |
|
49 | + ? array_filter($array, $callback) |
|
50 | + : array_filter($array); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param bool $flattenValue |
|
55 | + * @param string $prefix |
|
56 | + * @return array |
|
57 | + */ |
|
58 | + public static function flattenArray(array $array, $flattenValue = false, $prefix = '') |
|
59 | + { |
|
60 | + $result = []; |
|
61 | + foreach ($array as $key => $value) { |
|
62 | + $newKey = ltrim($prefix.'.'.$key, '.'); |
|
63 | + if (static::isIndexedFlatArray($value)) { |
|
64 | + if ($flattenValue) { |
|
65 | + $value = '['.implode(', ', $value).']'; |
|
66 | + } |
|
67 | + } elseif (is_array($value)) { |
|
68 | + $result = array_merge($result, static::flattenArray($value, $flattenValue, $newKey)); |
|
69 | + continue; |
|
70 | + } |
|
71 | + $result[$newKey] = $value; |
|
72 | + } |
|
73 | + return $result; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Get a value from an array of values using a dot-notation path as reference. |
|
78 | + * @param mixed $data |
|
79 | + * @param string $path |
|
80 | + * @param mixed $fallback |
|
81 | + * @return mixed |
|
82 | + */ |
|
83 | + public static function get($data, $path = '', $fallback = '') |
|
84 | + { |
|
85 | + $data = static::consolidateArray($data); |
|
86 | + $keys = explode('.', $path); |
|
87 | + foreach ($keys as $key) { |
|
88 | + if (!isset($data[$key])) { |
|
89 | + return $fallback; |
|
90 | + } |
|
91 | + $data = $data[$key]; |
|
92 | + } |
|
93 | + return $data; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param string $key |
|
98 | + * @return array |
|
99 | + */ |
|
100 | + public static function insertAfter($key, array $array, array $insert) |
|
101 | + { |
|
102 | + return static::insertInArray($array, $insert, $key, 'after'); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $key |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public static function insertBefore($key, array $array, array $insert) |
|
110 | + { |
|
111 | + return static::insertInArray($array, $insert, $key, 'before'); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param string $key |
|
116 | + * @param string $position |
|
117 | + * @return array |
|
118 | + */ |
|
119 | + public static function insertInArray(array $array, array $insert, $key, $position = 'before') |
|
120 | + { |
|
121 | + $keyPosition = intval(array_search($key, array_keys($array))); |
|
122 | + if ('after' == $position) { |
|
123 | + ++$keyPosition; |
|
124 | + } |
|
125 | + if (false !== $keyPosition) { |
|
126 | + $result = array_slice($array, 0, $keyPosition); |
|
127 | + $result = array_merge($result, $insert); |
|
128 | + return array_merge($result, array_slice($array, $keyPosition)); |
|
129 | + } |
|
130 | + return array_merge($array, $insert); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @param mixed $array |
|
135 | + * @return bool |
|
136 | + */ |
|
137 | + public static function isIndexedFlatArray($array) |
|
138 | + { |
|
139 | + if (!is_array($array) || array_filter($array, 'is_array')) { |
|
140 | + return false; |
|
141 | + } |
|
142 | + return wp_is_numeric_array($array); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param bool $prefixed |
|
147 | + * @return array |
|
148 | + */ |
|
149 | + public static function prefixArrayKeys(array $values, $prefixed = true) |
|
150 | + { |
|
151 | + $trim = '_'; |
|
152 | + $prefix = $prefixed |
|
153 | + ? $trim |
|
154 | + : ''; |
|
155 | + $prefixed = []; |
|
156 | + foreach ($values as $key => $value) { |
|
157 | + $key = trim($key); |
|
158 | + if (0 === strpos($key, $trim)) { |
|
159 | + $key = substr($key, strlen($trim)); |
|
160 | + } |
|
161 | + $prefixed[$prefix.$key] = $value; |
|
162 | + } |
|
163 | + return $prefixed; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return array |
|
168 | + */ |
|
169 | + public static function removeEmptyArrayValues(array $array) |
|
170 | + { |
|
171 | + $result = []; |
|
172 | + foreach ($array as $key => $value) { |
|
173 | + if (!$value) { |
|
174 | + continue; |
|
175 | + } |
|
176 | + $result[$key] = is_array($value) |
|
177 | + ? static::removeEmptyArrayValues($value) |
|
178 | + : $value; |
|
179 | + } |
|
180 | + return $result; |
|
181 | + } |
|
182 | + |
|
183 | + |
|
184 | + /** |
|
185 | + * Set a value to an array of values using a dot-notation path as reference. |
|
186 | + * @param string $path |
|
187 | + * @param mixed $value |
|
188 | + * @return array |
|
189 | + */ |
|
190 | + public static function set(array $data, $path, $value) |
|
191 | + { |
|
192 | + $token = strtok($path, '.'); |
|
193 | + $ref = &$data; |
|
194 | + while (false !== $token) { |
|
195 | + $ref = static::consolidateArray($ref); |
|
196 | + $ref = &$ref[$token]; |
|
197 | + $token = strtok('.'); |
|
198 | + } |
|
199 | + $ref = $value; |
|
200 | + return $data; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @return array |
|
205 | + */ |
|
206 | + public static function unique(array $values) |
|
207 | + { |
|
208 | + return array_filter(array_unique($values)); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @return array |
|
213 | + */ |
|
214 | + public static function unprefixArrayKeys(array $values) |
|
215 | + { |
|
216 | + return static::prefixArrayKeys($values, false); |
|
217 | + } |
|
218 | 218 | } |