@@ -11,6 +11,6 @@ |
||
11 | 11 | */ |
12 | 12 | class Upgrader |
13 | 13 | { |
14 | - public function run() |
|
15 | - {} |
|
14 | + public function run() |
|
15 | + {} |
|
16 | 16 | } |
@@ -8,47 +8,47 @@ discard block |
||
8 | 8 | |
9 | 9 | class SqlQueries |
10 | 10 | { |
11 | - protected $db; |
|
12 | - protected $postType; |
|
11 | + protected $db; |
|
12 | + protected $postType; |
|
13 | 13 | |
14 | - public function __construct() |
|
15 | - { |
|
16 | - global $wpdb; |
|
17 | - $this->db = $wpdb; |
|
18 | - $this->postType = Application::POST_TYPE; |
|
19 | - } |
|
14 | + public function __construct() |
|
15 | + { |
|
16 | + global $wpdb; |
|
17 | + $this->db = $wpdb; |
|
18 | + $this->postType = Application::POST_TYPE; |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * @return bool |
|
23 | - */ |
|
24 | - public function deletePostCountMetaKeys() |
|
25 | - { |
|
26 | - $this->db->query(" |
|
21 | + /** |
|
22 | + * @return bool |
|
23 | + */ |
|
24 | + public function deletePostCountMetaKeys() |
|
25 | + { |
|
26 | + $this->db->query(" |
|
27 | 27 | DELETE |
28 | 28 | FROM {$this->db->postmeta} |
29 | 29 | WHERE meta_key LIKE '_glsr_%' |
30 | 30 | "); |
31 | - } |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @return bool |
|
35 | - */ |
|
36 | - public function deleteTermCountMetaKeys() |
|
37 | - { |
|
38 | - $this->db->query(" |
|
33 | + /** |
|
34 | + * @return bool |
|
35 | + */ |
|
36 | + public function deleteTermCountMetaKeys() |
|
37 | + { |
|
38 | + $this->db->query(" |
|
39 | 39 | DELETE |
40 | 40 | FROM {$this->db->termmeta} |
41 | 41 | WHERE meta_key LIKE '_glsr_%' |
42 | 42 | "); |
43 | - } |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $metaReviewId |
|
47 | - * @return int |
|
48 | - */ |
|
49 | - public function getPostIdFromReviewId($metaReviewId) |
|
50 | - { |
|
51 | - $postId = $this->db->get_var(" |
|
45 | + /** |
|
46 | + * @param string $metaReviewId |
|
47 | + * @return int |
|
48 | + */ |
|
49 | + public function getPostIdFromReviewId($metaReviewId) |
|
50 | + { |
|
51 | + $postId = $this->db->get_var(" |
|
52 | 52 | SELECT p.ID |
53 | 53 | FROM {$this->db->posts} AS p |
54 | 54 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -56,17 +56,17 @@ discard block |
||
56 | 56 | AND m.meta_key = '_review_id' |
57 | 57 | AND m.meta_value = '{$metaReviewId}' |
58 | 58 | "); |
59 | - return intval($postId); |
|
60 | - } |
|
59 | + return intval($postId); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param int $lastPostId |
|
64 | - * @param int $limit |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public function getReviewCounts(array $args, $lastPostId = 0, $limit = 500) |
|
68 | - { |
|
69 | - return (array) $this->db->get_results(" |
|
62 | + /** |
|
63 | + * @param int $lastPostId |
|
64 | + * @param int $limit |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public function getReviewCounts(array $args, $lastPostId = 0, $limit = 500) |
|
68 | + { |
|
69 | + return (array) $this->db->get_results(" |
|
70 | 70 | SELECT DISTINCT p.ID, m1.meta_value AS rating, m2.meta_value AS type |
71 | 71 | FROM {$this->db->posts} AS p |
72 | 72 | INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id |
@@ -81,17 +81,17 @@ discard block |
||
81 | 81 | ORDER By p.ID ASC |
82 | 82 | LIMIT {$limit} |
83 | 83 | "); |
84 | - } |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @todo remove this? |
|
88 | - * @param string $metaKey |
|
89 | - * @return array |
|
90 | - */ |
|
91 | - public function getReviewCountsFor($metaKey) |
|
92 | - { |
|
93 | - $metaKey = Str::prefix('_', $metaKey); |
|
94 | - return (array) $this->db->get_results(" |
|
86 | + /** |
|
87 | + * @todo remove this? |
|
88 | + * @param string $metaKey |
|
89 | + * @return array |
|
90 | + */ |
|
91 | + public function getReviewCountsFor($metaKey) |
|
92 | + { |
|
93 | + $metaKey = Str::prefix('_', $metaKey); |
|
94 | + return (array) $this->db->get_results(" |
|
95 | 95 | SELECT DISTINCT m.meta_value AS name, COUNT(*) num_posts |
96 | 96 | FROM {$this->db->posts} AS p |
97 | 97 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -99,16 +99,16 @@ discard block |
||
99 | 99 | AND m.meta_key = '{$metaKey}' |
100 | 100 | GROUP BY name |
101 | 101 | "); |
102 | - } |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @todo remove this? |
|
106 | - * @param string $reviewType |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function getReviewIdsByType($reviewType) |
|
110 | - { |
|
111 | - $results = $this->db->get_col(" |
|
104 | + /** |
|
105 | + * @todo remove this? |
|
106 | + * @param string $reviewType |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function getReviewIdsByType($reviewType) |
|
110 | + { |
|
111 | + $results = $this->db->get_col(" |
|
112 | 112 | SELECT DISTINCT m1.meta_value AS review_id |
113 | 113 | FROM {$this->db->posts} AS p |
114 | 114 | INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id |
@@ -118,20 +118,20 @@ discard block |
||
118 | 118 | AND m2.meta_key = '_review_type' |
119 | 119 | AND m2.meta_value = '{$reviewType}' |
120 | 120 | "); |
121 | - return array_keys(array_flip($results)); |
|
122 | - } |
|
121 | + return array_keys(array_flip($results)); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * @param int $greaterThanId |
|
126 | - * @param int $limit |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - public function getReviewRatingsFromIds(array $postIds, $greaterThanId = 0, $limit = 100) |
|
130 | - { |
|
131 | - sort($postIds); |
|
132 | - $postIds = array_slice($postIds, intval(array_search($greaterThanId, $postIds)), $limit); |
|
133 | - $postIds = implode(',', $postIds); |
|
134 | - return (array) $this->db->get_results(" |
|
124 | + /** |
|
125 | + * @param int $greaterThanId |
|
126 | + * @param int $limit |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + public function getReviewRatingsFromIds(array $postIds, $greaterThanId = 0, $limit = 100) |
|
130 | + { |
|
131 | + sort($postIds); |
|
132 | + $postIds = array_slice($postIds, intval(array_search($greaterThanId, $postIds)), $limit); |
|
133 | + $postIds = implode(',', $postIds); |
|
134 | + return (array) $this->db->get_results(" |
|
135 | 135 | SELECT p.ID, m.meta_value AS rating |
136 | 136 | FROM {$this->db->posts} AS p |
137 | 137 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -144,20 +144,20 @@ discard block |
||
144 | 144 | ORDER By p.ID ASC |
145 | 145 | LIMIT {$limit} |
146 | 146 | "); |
147 | - } |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * @param string $key |
|
151 | - * @param string $status |
|
152 | - * @return array |
|
153 | - */ |
|
154 | - public function getReviewsMeta($key, $status = 'publish') |
|
155 | - { |
|
156 | - $postStatusQuery = 'all' != $status && !empty($status) |
|
157 | - ? "AND p.post_status = '{$status}'" |
|
158 | - : ''; |
|
159 | - $key = Str::prefix('_', $key); |
|
160 | - $values = $this->db->get_col(" |
|
149 | + /** |
|
150 | + * @param string $key |
|
151 | + * @param string $status |
|
152 | + * @return array |
|
153 | + */ |
|
154 | + public function getReviewsMeta($key, $status = 'publish') |
|
155 | + { |
|
156 | + $postStatusQuery = 'all' != $status && !empty($status) |
|
157 | + ? "AND p.post_status = '{$status}'" |
|
158 | + : ''; |
|
159 | + $key = Str::prefix('_', $key); |
|
160 | + $values = $this->db->get_col(" |
|
161 | 161 | SELECT DISTINCT m.meta_value |
162 | 162 | FROM {$this->db->postmeta} m |
163 | 163 | LEFT JOIN {$this->db->posts} p ON p.ID = m.post_id |
@@ -168,42 +168,42 @@ discard block |
||
168 | 168 | GROUP BY p.ID -- remove duplicate meta_value entries |
169 | 169 | ORDER BY m.meta_id ASC -- sort by oldest meta_value |
170 | 170 | "); |
171 | - sort($values); |
|
172 | - return $values; |
|
173 | - } |
|
171 | + sort($values); |
|
172 | + return $values; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * @param string $and |
|
177 | - * @return string |
|
178 | - */ |
|
179 | - protected function getAndForCounts(array $args, $and = '') |
|
180 | - { |
|
181 | - $postIds = implode(',', array_filter(Arr::get($args, 'post_ids', []))); |
|
182 | - $termIds = implode(',', array_filter(Arr::get($args, 'term_ids', []))); |
|
183 | - if (!empty($args['type'])) { |
|
184 | - $and.= "AND m2.meta_value = '{$args['type']}' "; |
|
185 | - } |
|
186 | - if ($postIds) { |
|
187 | - $and.= "AND m3.meta_key = '_assigned_to' AND m3.meta_value IN ({$postIds}) "; |
|
188 | - } |
|
189 | - if ($termIds) { |
|
190 | - $and.= "AND tr.term_taxonomy_id IN ({$termIds}) "; |
|
191 | - } |
|
192 | - return apply_filters('site-reviews/query/and-for-counts', $and); |
|
193 | - } |
|
175 | + /** |
|
176 | + * @param string $and |
|
177 | + * @return string |
|
178 | + */ |
|
179 | + protected function getAndForCounts(array $args, $and = '') |
|
180 | + { |
|
181 | + $postIds = implode(',', array_filter(Arr::get($args, 'post_ids', []))); |
|
182 | + $termIds = implode(',', array_filter(Arr::get($args, 'term_ids', []))); |
|
183 | + if (!empty($args['type'])) { |
|
184 | + $and.= "AND m2.meta_value = '{$args['type']}' "; |
|
185 | + } |
|
186 | + if ($postIds) { |
|
187 | + $and.= "AND m3.meta_key = '_assigned_to' AND m3.meta_value IN ({$postIds}) "; |
|
188 | + } |
|
189 | + if ($termIds) { |
|
190 | + $and.= "AND tr.term_taxonomy_id IN ({$termIds}) "; |
|
191 | + } |
|
192 | + return apply_filters('site-reviews/query/and-for-counts', $and); |
|
193 | + } |
|
194 | 194 | |
195 | - /** |
|
196 | - * @param string $innerJoin |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - protected function getInnerJoinForCounts(array $args, $innerJoin = '') |
|
200 | - { |
|
201 | - if (!empty(Arr::get($args, 'post_ids'))) { |
|
202 | - $innerJoin.= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id "; |
|
203 | - } |
|
204 | - if (!empty(Arr::get($args, 'term_ids'))) { |
|
205 | - $innerJoin.= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id "; |
|
206 | - } |
|
207 | - return apply_filters('site-reviews/query/inner-join-for-counts', $innerJoin); |
|
208 | - } |
|
195 | + /** |
|
196 | + * @param string $innerJoin |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + protected function getInnerJoinForCounts(array $args, $innerJoin = '') |
|
200 | + { |
|
201 | + if (!empty(Arr::get($args, 'post_ids'))) { |
|
202 | + $innerJoin.= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id "; |
|
203 | + } |
|
204 | + if (!empty(Arr::get($args, 'term_ids'))) { |
|
205 | + $innerJoin.= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id "; |
|
206 | + } |
|
207 | + return apply_filters('site-reviews/query/inner-join-for-counts', $innerJoin); |
|
208 | + } |
|
209 | 209 | } |
@@ -9,93 +9,93 @@ |
||
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 | - glsr(SqlQueries::class)->deleteTermCountMetaKeys(); |
|
94 | - $terms = glsr(Database::class)->getTerms([ |
|
95 | - 'fields' => 'all', |
|
96 | - ]); |
|
97 | - foreach ($terms as $term) { |
|
98 | - $this->update($term->term_id, $this->build($term->term_taxonomy_id)); |
|
99 | - } |
|
100 | - } |
|
88 | + /** |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function updateAll() |
|
92 | + { |
|
93 | + glsr(SqlQueries::class)->deleteTermCountMetaKeys(); |
|
94 | + $terms = glsr(Database::class)->getTerms([ |
|
95 | + 'fields' => 'all', |
|
96 | + ]); |
|
97 | + foreach ($terms as $term) { |
|
98 | + $this->update($term->term_id, $this->build($term->term_taxonomy_id)); |
|
99 | + } |
|
100 | + } |
|
101 | 101 | } |
@@ -7,85 +7,85 @@ |
||
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 | - glsr(SqlQueries::class)->deletePostCountMetaKeys(); |
|
86 | - $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
87 | - foreach ($postIds as $postId) { |
|
88 | - $this->update($postId, $this->build($postId)); |
|
89 | - } |
|
90 | - } |
|
80 | + /** |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function updateAll() |
|
84 | + { |
|
85 | + glsr(SqlQueries::class)->deletePostCountMetaKeys(); |
|
86 | + $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
87 | + foreach ($postIds as $postId) { |
|
88 | + $this->update($postId, $this->build($postId)); |
|
89 | + } |
|
90 | + } |
|
91 | 91 | } |
@@ -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 | } |