@@ -10,94 +10,94 @@ |
||
10 | 10 | |
11 | 11 | class MainController extends Controller |
12 | 12 | { |
13 | - /** |
|
14 | - * @return void |
|
15 | - * @action init |
|
16 | - */ |
|
17 | - public function registerPostType() |
|
18 | - { |
|
19 | - if (!glsr()->hasPermission()) { |
|
20 | - return; |
|
21 | - } |
|
22 | - $command = new RegisterPostType([ |
|
23 | - 'capabilities' => ['create_posts' => 'create_'.Application::POST_TYPE], |
|
24 | - 'columns' => [ |
|
25 | - 'title' => '', |
|
26 | - 'category' => '', |
|
27 | - 'assigned_to' => __('Assigned To', 'site-reviews'), |
|
28 | - 'reviewer' => __('Author', 'site-reviews'), |
|
29 | - 'review_type' => __('Type', 'site-reviews'), |
|
30 | - 'rating' => __('Rating', 'site-reviews'), |
|
31 | - 'pinned' => __('Pinned', 'site-reviews'), |
|
32 | - 'date' => '', |
|
33 | - ], |
|
34 | - 'menu_icon' => 'dashicons-star-half', |
|
35 | - 'menu_name' => glsr()->name, |
|
36 | - 'map_meta_cap' => true, |
|
37 | - 'plural' => __('Reviews', 'site-reviews'), |
|
38 | - 'post_type' => Application::POST_TYPE, |
|
39 | - 'rest_controller_class' => RestReviewController::class, |
|
40 | - 'show_in_rest' => true, |
|
41 | - 'single' => __('Review', 'site-reviews'), |
|
42 | - ]); |
|
43 | - $this->execute($command); |
|
44 | - } |
|
13 | + /** |
|
14 | + * @return void |
|
15 | + * @action init |
|
16 | + */ |
|
17 | + public function registerPostType() |
|
18 | + { |
|
19 | + if (!glsr()->hasPermission()) { |
|
20 | + return; |
|
21 | + } |
|
22 | + $command = new RegisterPostType([ |
|
23 | + 'capabilities' => ['create_posts' => 'create_'.Application::POST_TYPE], |
|
24 | + 'columns' => [ |
|
25 | + 'title' => '', |
|
26 | + 'category' => '', |
|
27 | + 'assigned_to' => __('Assigned To', 'site-reviews'), |
|
28 | + 'reviewer' => __('Author', 'site-reviews'), |
|
29 | + 'review_type' => __('Type', 'site-reviews'), |
|
30 | + 'rating' => __('Rating', 'site-reviews'), |
|
31 | + 'pinned' => __('Pinned', 'site-reviews'), |
|
32 | + 'date' => '', |
|
33 | + ], |
|
34 | + 'menu_icon' => 'dashicons-star-half', |
|
35 | + 'menu_name' => glsr()->name, |
|
36 | + 'map_meta_cap' => true, |
|
37 | + 'plural' => __('Reviews', 'site-reviews'), |
|
38 | + 'post_type' => Application::POST_TYPE, |
|
39 | + 'rest_controller_class' => RestReviewController::class, |
|
40 | + 'show_in_rest' => true, |
|
41 | + 'single' => __('Review', 'site-reviews'), |
|
42 | + ]); |
|
43 | + $this->execute($command); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @return void |
|
48 | - * @action init |
|
49 | - */ |
|
50 | - public function registerShortcodes() |
|
51 | - { |
|
52 | - $command = new RegisterShortcodes([ |
|
53 | - 'site_reviews', |
|
54 | - 'site_reviews_form', |
|
55 | - 'site_reviews_summary', |
|
56 | - ]); |
|
57 | - $this->execute($command); |
|
58 | - } |
|
46 | + /** |
|
47 | + * @return void |
|
48 | + * @action init |
|
49 | + */ |
|
50 | + public function registerShortcodes() |
|
51 | + { |
|
52 | + $command = new RegisterShortcodes([ |
|
53 | + 'site_reviews', |
|
54 | + 'site_reviews_form', |
|
55 | + 'site_reviews_summary', |
|
56 | + ]); |
|
57 | + $this->execute($command); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @return void |
|
62 | - * @action init |
|
63 | - */ |
|
64 | - public function registerTaxonomy() |
|
65 | - { |
|
66 | - $command = new RegisterTaxonomy([ |
|
67 | - 'hierarchical' => true, |
|
68 | - 'meta_box_cb' => [glsr(EditorController::class), 'renderTaxonomyMetabox'], |
|
69 | - 'public' => false, |
|
70 | - 'rest_controller_class' => RestCategoryController::class, |
|
71 | - 'show_admin_column' => true, |
|
72 | - 'show_in_rest' => true, |
|
73 | - 'show_ui' => true, |
|
74 | - ]); |
|
75 | - $this->execute($command); |
|
76 | - } |
|
60 | + /** |
|
61 | + * @return void |
|
62 | + * @action init |
|
63 | + */ |
|
64 | + public function registerTaxonomy() |
|
65 | + { |
|
66 | + $command = new RegisterTaxonomy([ |
|
67 | + 'hierarchical' => true, |
|
68 | + 'meta_box_cb' => [glsr(EditorController::class), 'renderTaxonomyMetabox'], |
|
69 | + 'public' => false, |
|
70 | + 'rest_controller_class' => RestCategoryController::class, |
|
71 | + 'show_admin_column' => true, |
|
72 | + 'show_in_rest' => true, |
|
73 | + 'show_ui' => true, |
|
74 | + ]); |
|
75 | + $this->execute($command); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return void |
|
80 | - * @action widgets_init |
|
81 | - */ |
|
82 | - public function registerWidgets() |
|
83 | - { |
|
84 | - $command = new RegisterWidgets([ |
|
85 | - 'site-reviews' => [ |
|
86 | - 'class' => 'glsr-widget glsr-widget-site-reviews', |
|
87 | - 'description' => __('Site Reviews: Display your recent reviews.', 'site-reviews'), |
|
88 | - 'title' => __('Recent Reviews', 'site-reviews'), |
|
89 | - ], |
|
90 | - 'site-reviews-form' => [ |
|
91 | - 'class' => 'glsr-widget glsr-widget-site-reviews-form', |
|
92 | - 'description' => __('Site Reviews: Display a form to submit reviews.', 'site-reviews'), |
|
93 | - 'title' => __('Submit a Review', 'site-reviews'), |
|
94 | - ], |
|
95 | - 'site-reviews-summary' => [ |
|
96 | - 'class' => 'glsr-widget glsr-widget-site-reviews-summary', |
|
97 | - 'description' => __('Site Reviews: Display a summary of your reviews.', 'site-reviews'), |
|
98 | - 'title' => __('Summary of Reviews', 'site-reviews'), |
|
99 | - ], |
|
100 | - ]); |
|
101 | - $this->execute($command); |
|
102 | - } |
|
78 | + /** |
|
79 | + * @return void |
|
80 | + * @action widgets_init |
|
81 | + */ |
|
82 | + public function registerWidgets() |
|
83 | + { |
|
84 | + $command = new RegisterWidgets([ |
|
85 | + 'site-reviews' => [ |
|
86 | + 'class' => 'glsr-widget glsr-widget-site-reviews', |
|
87 | + 'description' => __('Site Reviews: Display your recent reviews.', 'site-reviews'), |
|
88 | + 'title' => __('Recent Reviews', 'site-reviews'), |
|
89 | + ], |
|
90 | + 'site-reviews-form' => [ |
|
91 | + 'class' => 'glsr-widget glsr-widget-site-reviews-form', |
|
92 | + 'description' => __('Site Reviews: Display a form to submit reviews.', 'site-reviews'), |
|
93 | + 'title' => __('Submit a Review', 'site-reviews'), |
|
94 | + ], |
|
95 | + 'site-reviews-summary' => [ |
|
96 | + 'class' => 'glsr-widget glsr-widget-site-reviews-summary', |
|
97 | + 'description' => __('Site Reviews: Display a summary of your reviews.', 'site-reviews'), |
|
98 | + 'title' => __('Summary of Reviews', 'site-reviews'), |
|
99 | + ], |
|
100 | + ]); |
|
101 | + $this->execute($command); |
|
102 | + } |
|
103 | 103 | } |
@@ -18,11 +18,11 @@ |
||
18 | 18 | defined('WPINC') || die; |
19 | 19 | |
20 | 20 | if (!class_exists('GL_Plugin_Check_v3')) { |
21 | - require_once __DIR__.'/activate.php'; |
|
21 | + require_once __DIR__.'/activate.php'; |
|
22 | 22 | } |
23 | 23 | $check = new GL_Plugin_Check_v3(__FILE__); |
24 | 24 | if (!$check->canProceed()) { |
25 | - return; |
|
25 | + return; |
|
26 | 26 | } |
27 | 27 | require_once __DIR__.'/autoload.php'; |
28 | 28 | require_once __DIR__.'/compatibility.php'; |
@@ -16,204 +16,204 @@ |
||
16 | 16 | |
17 | 17 | class ReviewManager |
18 | 18 | { |
19 | - /** |
|
20 | - * @return false|Review |
|
21 | - */ |
|
22 | - public function create(CreateReview $command) |
|
23 | - { |
|
24 | - $reviewValues = glsr(CreateReviewDefaults::class)->restrict((array) $command); |
|
25 | - $reviewValues = apply_filters('site-reviews/create/review-values', $reviewValues, $command); |
|
26 | - $reviewValues = glsr(Helper::class)->prefixArrayKeys($reviewValues); |
|
27 | - unset($reviewValues['json']); // @todo remove the need for this |
|
28 | - $postValues = [ |
|
29 | - 'comment_status' => 'closed', |
|
30 | - 'meta_input' => $reviewValues, |
|
31 | - 'ping_status' => 'closed', |
|
32 | - 'post_content' => $reviewValues['_content'], |
|
33 | - 'post_date' => $reviewValues['_date'], |
|
34 | - 'post_date_gmt' => get_gmt_from_date($reviewValues['_date']), |
|
35 | - 'post_name' => $reviewValues['_review_type'].'-'.$reviewValues['_review_id'], |
|
36 | - 'post_status' => $this->getNewPostStatus($reviewValues, $command->blacklisted), |
|
37 | - 'post_title' => $reviewValues['_title'], |
|
38 | - 'post_type' => Application::POST_TYPE, |
|
39 | - ]; |
|
40 | - $postId = wp_insert_post($postValues, true); |
|
41 | - if (is_wp_error($postId)) { |
|
42 | - glsr_log()->error($postId->get_error_message())->debug($postValues); |
|
43 | - return false; |
|
44 | - } |
|
45 | - $this->setTerms($postId, $command->category); |
|
46 | - $review = $this->single(get_post($postId)); |
|
47 | - do_action('site-reviews/review/created', $review, $command); |
|
48 | - return $review; |
|
49 | - } |
|
19 | + /** |
|
20 | + * @return false|Review |
|
21 | + */ |
|
22 | + public function create(CreateReview $command) |
|
23 | + { |
|
24 | + $reviewValues = glsr(CreateReviewDefaults::class)->restrict((array) $command); |
|
25 | + $reviewValues = apply_filters('site-reviews/create/review-values', $reviewValues, $command); |
|
26 | + $reviewValues = glsr(Helper::class)->prefixArrayKeys($reviewValues); |
|
27 | + unset($reviewValues['json']); // @todo remove the need for this |
|
28 | + $postValues = [ |
|
29 | + 'comment_status' => 'closed', |
|
30 | + 'meta_input' => $reviewValues, |
|
31 | + 'ping_status' => 'closed', |
|
32 | + 'post_content' => $reviewValues['_content'], |
|
33 | + 'post_date' => $reviewValues['_date'], |
|
34 | + 'post_date_gmt' => get_gmt_from_date($reviewValues['_date']), |
|
35 | + 'post_name' => $reviewValues['_review_type'].'-'.$reviewValues['_review_id'], |
|
36 | + 'post_status' => $this->getNewPostStatus($reviewValues, $command->blacklisted), |
|
37 | + 'post_title' => $reviewValues['_title'], |
|
38 | + 'post_type' => Application::POST_TYPE, |
|
39 | + ]; |
|
40 | + $postId = wp_insert_post($postValues, true); |
|
41 | + if (is_wp_error($postId)) { |
|
42 | + glsr_log()->error($postId->get_error_message())->debug($postValues); |
|
43 | + return false; |
|
44 | + } |
|
45 | + $this->setTerms($postId, $command->category); |
|
46 | + $review = $this->single(get_post($postId)); |
|
47 | + do_action('site-reviews/review/created', $review, $command); |
|
48 | + return $review; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param string $metaReviewId |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function delete($metaReviewId) |
|
56 | - { |
|
57 | - if ($postId = $this->getPostId($metaReviewId)) { |
|
58 | - wp_delete_post($postId, true); |
|
59 | - } |
|
60 | - } |
|
51 | + /** |
|
52 | + * @param string $metaReviewId |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function delete($metaReviewId) |
|
56 | + { |
|
57 | + if ($postId = $this->getPostId($metaReviewId)) { |
|
58 | + wp_delete_post($postId, true); |
|
59 | + } |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return object |
|
64 | - */ |
|
65 | - public function get(array $args = []) |
|
66 | - { |
|
67 | - $args = glsr(ReviewsDefaults::class)->merge($args); |
|
68 | - $metaQuery = glsr(QueryBuilder::class)->buildQuery( |
|
69 | - ['assigned_to', 'email', 'ip_address', 'type', 'rating'], |
|
70 | - $args |
|
71 | - ); |
|
72 | - $taxQuery = glsr(QueryBuilder::class)->buildQuery( |
|
73 | - ['category'], |
|
74 | - ['category' => $this->normalizeTermIds($args['category'])] |
|
75 | - ); |
|
76 | - $paged = glsr(QueryBuilder::class)->getPaged( |
|
77 | - wp_validate_boolean($args['pagination']) |
|
78 | - ); |
|
79 | - $parameters = [ |
|
80 | - 'meta_key' => '_pinned', |
|
81 | - 'meta_query' => $metaQuery, |
|
82 | - 'offset' => $args['offset'], |
|
83 | - 'order' => $args['order'], |
|
84 | - 'orderby' => 'meta_value '.$args['orderby'], |
|
85 | - 'paged' => glsr_get($args, 'paged', $paged), |
|
86 | - 'post__in' => $args['post__in'], |
|
87 | - 'post__not_in' => $args['post__not_in'], |
|
88 | - 'post_status' => 'publish', |
|
89 | - 'post_type' => Application::POST_TYPE, |
|
90 | - 'posts_per_page' => $args['count'], |
|
91 | - 'tax_query' => $taxQuery, |
|
92 | - ]; |
|
93 | - $parameters = apply_filters('site-reviews/get/reviews/query', $parameters, $args); |
|
94 | - $query = new WP_Query($parameters); |
|
95 | - $results = array_map([$this, 'single'], $query->posts); |
|
96 | - $reviews = new Reviews($results, $query->max_num_pages, $args); |
|
97 | - return apply_filters('site-reviews/get/reviews', $reviews, $query); |
|
98 | - } |
|
62 | + /** |
|
63 | + * @return object |
|
64 | + */ |
|
65 | + public function get(array $args = []) |
|
66 | + { |
|
67 | + $args = glsr(ReviewsDefaults::class)->merge($args); |
|
68 | + $metaQuery = glsr(QueryBuilder::class)->buildQuery( |
|
69 | + ['assigned_to', 'email', 'ip_address', 'type', 'rating'], |
|
70 | + $args |
|
71 | + ); |
|
72 | + $taxQuery = glsr(QueryBuilder::class)->buildQuery( |
|
73 | + ['category'], |
|
74 | + ['category' => $this->normalizeTermIds($args['category'])] |
|
75 | + ); |
|
76 | + $paged = glsr(QueryBuilder::class)->getPaged( |
|
77 | + wp_validate_boolean($args['pagination']) |
|
78 | + ); |
|
79 | + $parameters = [ |
|
80 | + 'meta_key' => '_pinned', |
|
81 | + 'meta_query' => $metaQuery, |
|
82 | + 'offset' => $args['offset'], |
|
83 | + 'order' => $args['order'], |
|
84 | + 'orderby' => 'meta_value '.$args['orderby'], |
|
85 | + 'paged' => glsr_get($args, 'paged', $paged), |
|
86 | + 'post__in' => $args['post__in'], |
|
87 | + 'post__not_in' => $args['post__not_in'], |
|
88 | + 'post_status' => 'publish', |
|
89 | + 'post_type' => Application::POST_TYPE, |
|
90 | + 'posts_per_page' => $args['count'], |
|
91 | + 'tax_query' => $taxQuery, |
|
92 | + ]; |
|
93 | + $parameters = apply_filters('site-reviews/get/reviews/query', $parameters, $args); |
|
94 | + $query = new WP_Query($parameters); |
|
95 | + $results = array_map([$this, 'single'], $query->posts); |
|
96 | + $reviews = new Reviews($results, $query->max_num_pages, $args); |
|
97 | + return apply_filters('site-reviews/get/reviews', $reviews, $query); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @param string $metaReviewId |
|
102 | - * @return int |
|
103 | - */ |
|
104 | - public function getPostId($metaReviewId) |
|
105 | - { |
|
106 | - return glsr(SqlQueries::class)->getPostIdFromReviewId($metaReviewId); |
|
107 | - } |
|
100 | + /** |
|
101 | + * @param string $metaReviewId |
|
102 | + * @return int |
|
103 | + */ |
|
104 | + public function getPostId($metaReviewId) |
|
105 | + { |
|
106 | + return glsr(SqlQueries::class)->getPostIdFromReviewId($metaReviewId); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @return array |
|
111 | - */ |
|
112 | - public function getRatingCounts(array $args = []) |
|
113 | - { |
|
114 | - $args = glsr(SiteReviewsSummaryDefaults::class)->filter($args); |
|
115 | - $counts = glsr(CountsManager::class)->get([ |
|
116 | - 'post_ids' => glsr(Helper::class)->convertStringToArray($args['assigned_to']), |
|
117 | - 'term_ids' => $this->normalizeTermIds($args['category']), |
|
118 | - 'type' => $args['type'], |
|
119 | - ]); |
|
120 | - return glsr(CountsManager::class)->flatten($counts, [ |
|
121 | - 'min' => $args['rating'], |
|
122 | - ]); |
|
123 | - } |
|
109 | + /** |
|
110 | + * @return array |
|
111 | + */ |
|
112 | + public function getRatingCounts(array $args = []) |
|
113 | + { |
|
114 | + $args = glsr(SiteReviewsSummaryDefaults::class)->filter($args); |
|
115 | + $counts = glsr(CountsManager::class)->get([ |
|
116 | + 'post_ids' => glsr(Helper::class)->convertStringToArray($args['assigned_to']), |
|
117 | + 'term_ids' => $this->normalizeTermIds($args['category']), |
|
118 | + 'type' => $args['type'], |
|
119 | + ]); |
|
120 | + return glsr(CountsManager::class)->flatten($counts, [ |
|
121 | + 'min' => $args['rating'], |
|
122 | + ]); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * @param string $commaSeparatedTermIds |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - public function normalizeTermIds($commaSeparatedTermIds) |
|
130 | - { |
|
131 | - $termIds = glsr_array_column($this->normalizeTerms($commaSeparatedTermIds), 'term_id'); |
|
132 | - return array_unique(array_map('intval', $termIds)); |
|
133 | - } |
|
125 | + /** |
|
126 | + * @param string $commaSeparatedTermIds |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + public function normalizeTermIds($commaSeparatedTermIds) |
|
130 | + { |
|
131 | + $termIds = glsr_array_column($this->normalizeTerms($commaSeparatedTermIds), 'term_id'); |
|
132 | + return array_unique(array_map('intval', $termIds)); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * @param string $commaSeparatedTermIds |
|
137 | - * @return array |
|
138 | - */ |
|
139 | - public function normalizeTerms($commaSeparatedTermIds) |
|
140 | - { |
|
141 | - $terms = []; |
|
142 | - $termIds = glsr(Helper::class)->convertStringToArray($commaSeparatedTermIds); |
|
143 | - foreach ($termIds as $termId) { |
|
144 | - if (is_numeric($termId)) { |
|
145 | - $termId = intval($termId); |
|
146 | - } |
|
147 | - $term = term_exists($termId, Application::TAXONOMY); |
|
148 | - if (!isset($term['term_id'])) { |
|
149 | - continue; |
|
150 | - } |
|
151 | - $terms[] = $term; |
|
152 | - } |
|
153 | - return $terms; |
|
154 | - } |
|
135 | + /** |
|
136 | + * @param string $commaSeparatedTermIds |
|
137 | + * @return array |
|
138 | + */ |
|
139 | + public function normalizeTerms($commaSeparatedTermIds) |
|
140 | + { |
|
141 | + $terms = []; |
|
142 | + $termIds = glsr(Helper::class)->convertStringToArray($commaSeparatedTermIds); |
|
143 | + foreach ($termIds as $termId) { |
|
144 | + if (is_numeric($termId)) { |
|
145 | + $termId = intval($termId); |
|
146 | + } |
|
147 | + $term = term_exists($termId, Application::TAXONOMY); |
|
148 | + if (!isset($term['term_id'])) { |
|
149 | + continue; |
|
150 | + } |
|
151 | + $terms[] = $term; |
|
152 | + } |
|
153 | + return $terms; |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * @param int $postId |
|
158 | - * @return void |
|
159 | - */ |
|
160 | - public function revert($postId) |
|
161 | - { |
|
162 | - if (Application::POST_TYPE != get_post_field('post_type', $postId)) { |
|
163 | - return; |
|
164 | - } |
|
165 | - delete_post_meta($postId, '_edit_last'); |
|
166 | - $result = wp_update_post([ |
|
167 | - 'ID' => $postId, |
|
168 | - 'post_content' => glsr(Database::class)->get($postId, 'content'), |
|
169 | - 'post_date' => glsr(Database::class)->get($postId, 'date'), |
|
170 | - 'post_title' => glsr(Database::class)->get($postId, 'title'), |
|
171 | - ]); |
|
172 | - if (is_wp_error($result)) { |
|
173 | - glsr_log()->error($result->get_error_message()); |
|
174 | - return; |
|
175 | - } |
|
176 | - do_action('site-reviews/review/reverted', glsr_get_review($postId)); |
|
177 | - } |
|
156 | + /** |
|
157 | + * @param int $postId |
|
158 | + * @return void |
|
159 | + */ |
|
160 | + public function revert($postId) |
|
161 | + { |
|
162 | + if (Application::POST_TYPE != get_post_field('post_type', $postId)) { |
|
163 | + return; |
|
164 | + } |
|
165 | + delete_post_meta($postId, '_edit_last'); |
|
166 | + $result = wp_update_post([ |
|
167 | + 'ID' => $postId, |
|
168 | + 'post_content' => glsr(Database::class)->get($postId, 'content'), |
|
169 | + 'post_date' => glsr(Database::class)->get($postId, 'date'), |
|
170 | + 'post_title' => glsr(Database::class)->get($postId, 'title'), |
|
171 | + ]); |
|
172 | + if (is_wp_error($result)) { |
|
173 | + glsr_log()->error($result->get_error_message()); |
|
174 | + return; |
|
175 | + } |
|
176 | + do_action('site-reviews/review/reverted', glsr_get_review($postId)); |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * @return Review |
|
181 | - */ |
|
182 | - public function single(WP_Post $post) |
|
183 | - { |
|
184 | - if (Application::POST_TYPE != $post->post_type) { |
|
185 | - $post = new WP_Post((object) []); |
|
186 | - } |
|
187 | - $review = new Review($post); |
|
188 | - return apply_filters('site-reviews/get/review', $review, $post); |
|
189 | - } |
|
179 | + /** |
|
180 | + * @return Review |
|
181 | + */ |
|
182 | + public function single(WP_Post $post) |
|
183 | + { |
|
184 | + if (Application::POST_TYPE != $post->post_type) { |
|
185 | + $post = new WP_Post((object) []); |
|
186 | + } |
|
187 | + $review = new Review($post); |
|
188 | + return apply_filters('site-reviews/get/review', $review, $post); |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * @param bool $isBlacklisted |
|
193 | - * @return string |
|
194 | - */ |
|
195 | - protected function getNewPostStatus(array $reviewValues, $isBlacklisted) |
|
196 | - { |
|
197 | - $requireApproval = glsr(OptionManager::class)->getBool('settings.general.require.approval'); |
|
198 | - return 'local' == $reviewValues['_review_type'] && ($requireApproval || $isBlacklisted) |
|
199 | - ? 'pending' |
|
200 | - : 'publish'; |
|
201 | - } |
|
191 | + /** |
|
192 | + * @param bool $isBlacklisted |
|
193 | + * @return string |
|
194 | + */ |
|
195 | + protected function getNewPostStatus(array $reviewValues, $isBlacklisted) |
|
196 | + { |
|
197 | + $requireApproval = glsr(OptionManager::class)->getBool('settings.general.require.approval'); |
|
198 | + return 'local' == $reviewValues['_review_type'] && ($requireApproval || $isBlacklisted) |
|
199 | + ? 'pending' |
|
200 | + : 'publish'; |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * @param int $postId |
|
205 | - * @param string $termIds |
|
206 | - * @return void |
|
207 | - */ |
|
208 | - protected function setTerms($postId, $termIds) |
|
209 | - { |
|
210 | - $termIds = $this->normalizeTermIds($termIds); |
|
211 | - if (empty($termIds)) { |
|
212 | - return; |
|
213 | - } |
|
214 | - $termTaxonomyIds = wp_set_object_terms($postId, $termIds, Application::TAXONOMY); |
|
215 | - if (is_wp_error($termTaxonomyIds)) { |
|
216 | - glsr_log()->error($termTaxonomyIds->get_error_message()); |
|
217 | - } |
|
218 | - } |
|
203 | + /** |
|
204 | + * @param int $postId |
|
205 | + * @param string $termIds |
|
206 | + * @return void |
|
207 | + */ |
|
208 | + protected function setTerms($postId, $termIds) |
|
209 | + { |
|
210 | + $termIds = $this->normalizeTermIds($termIds); |
|
211 | + if (empty($termIds)) { |
|
212 | + return; |
|
213 | + } |
|
214 | + $termTaxonomyIds = wp_set_object_terms($postId, $termIds, Application::TAXONOMY); |
|
215 | + if (is_wp_error($termTaxonomyIds)) { |
|
216 | + glsr_log()->error($termTaxonomyIds->get_error_message()); |
|
217 | + } |
|
218 | + } |
|
219 | 219 | } |
@@ -6,162 +6,162 @@ |
||
6 | 6 | |
7 | 7 | class Router |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - protected $unguardedActions = []; |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + protected $unguardedActions = []; |
|
13 | 13 | |
14 | - public function __construct() |
|
15 | - { |
|
16 | - $this->unguardedActions = apply_filters('site-reviews/router/unguarded-actions', [ |
|
17 | - 'dismiss-notice', |
|
18 | - 'fetch-paged-reviews', |
|
19 | - ]); |
|
20 | - } |
|
14 | + public function __construct() |
|
15 | + { |
|
16 | + $this->unguardedActions = apply_filters('site-reviews/router/unguarded-actions', [ |
|
17 | + 'dismiss-notice', |
|
18 | + 'fetch-paged-reviews', |
|
19 | + ]); |
|
20 | + } |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return void |
|
24 | - */ |
|
25 | - public function routeAdminPostRequest() |
|
26 | - { |
|
27 | - $request = $this->getRequest(); |
|
28 | - if (!$this->isValidPostRequest($request)) { |
|
29 | - return; |
|
30 | - } |
|
31 | - check_admin_referer($request['_action']); |
|
32 | - $this->routeRequest('admin', $request['_action'], $request); |
|
33 | - } |
|
22 | + /** |
|
23 | + * @return void |
|
24 | + */ |
|
25 | + public function routeAdminPostRequest() |
|
26 | + { |
|
27 | + $request = $this->getRequest(); |
|
28 | + if (!$this->isValidPostRequest($request)) { |
|
29 | + return; |
|
30 | + } |
|
31 | + check_admin_referer($request['_action']); |
|
32 | + $this->routeRequest('admin', $request['_action'], $request); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - public function routeAjaxRequest() |
|
39 | - { |
|
40 | - $request = $this->getRequest(); |
|
41 | - $this->checkAjaxRequest($request); |
|
42 | - $this->checkAjaxNonce($request); |
|
43 | - $this->routeRequest('ajax', $request['_action'], $request); |
|
44 | - wp_die(); |
|
45 | - } |
|
35 | + /** |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + public function routeAjaxRequest() |
|
39 | + { |
|
40 | + $request = $this->getRequest(); |
|
41 | + $this->checkAjaxRequest($request); |
|
42 | + $this->checkAjaxNonce($request); |
|
43 | + $this->routeRequest('ajax', $request['_action'], $request); |
|
44 | + wp_die(); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @return void |
|
49 | - */ |
|
50 | - public function routePublicPostRequest() |
|
51 | - { |
|
52 | - if (is_admin()) { |
|
53 | - return; |
|
54 | - } |
|
55 | - $request = $this->getRequest(); |
|
56 | - if (!$this->isValidPostRequest($request)) { |
|
57 | - return; |
|
58 | - } |
|
59 | - if (!$this->isValidPublicNonce($request)) { |
|
60 | - return; |
|
61 | - } |
|
62 | - $this->routeRequest('public', $request['_action'], $request); |
|
63 | - } |
|
47 | + /** |
|
48 | + * @return void |
|
49 | + */ |
|
50 | + public function routePublicPostRequest() |
|
51 | + { |
|
52 | + if (is_admin()) { |
|
53 | + return; |
|
54 | + } |
|
55 | + $request = $this->getRequest(); |
|
56 | + if (!$this->isValidPostRequest($request)) { |
|
57 | + return; |
|
58 | + } |
|
59 | + if (!$this->isValidPublicNonce($request)) { |
|
60 | + return; |
|
61 | + } |
|
62 | + $this->routeRequest('public', $request['_action'], $request); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return void |
|
67 | - */ |
|
68 | - protected function checkAjaxNonce(array $request) |
|
69 | - { |
|
70 | - if (!is_user_logged_in() || in_array(glsr_get($request, '_action'), $this->unguardedActions)) { |
|
71 | - return; |
|
72 | - } |
|
73 | - if (!isset($request['_nonce'])) { |
|
74 | - $this->sendAjaxError('request is missing a nonce', $request); |
|
75 | - } |
|
76 | - if (!wp_verify_nonce($request['_nonce'], $request['_action'])) { |
|
77 | - $this->sendAjaxError('request failed the nonce check', $request, 403); |
|
78 | - } |
|
79 | - } |
|
65 | + /** |
|
66 | + * @return void |
|
67 | + */ |
|
68 | + protected function checkAjaxNonce(array $request) |
|
69 | + { |
|
70 | + if (!is_user_logged_in() || in_array(glsr_get($request, '_action'), $this->unguardedActions)) { |
|
71 | + return; |
|
72 | + } |
|
73 | + if (!isset($request['_nonce'])) { |
|
74 | + $this->sendAjaxError('request is missing a nonce', $request); |
|
75 | + } |
|
76 | + if (!wp_verify_nonce($request['_nonce'], $request['_action'])) { |
|
77 | + $this->sendAjaxError('request failed the nonce check', $request, 403); |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @return void |
|
83 | - */ |
|
84 | - protected function checkAjaxRequest(array $request) |
|
85 | - { |
|
86 | - if (!isset($request['_action'])) { |
|
87 | - $this->sendAjaxError('request must include an action', $request); |
|
88 | - } |
|
89 | - if (empty($request['_ajax_request'])) { |
|
90 | - $this->sendAjaxError('request is invalid', $request); |
|
91 | - } |
|
92 | - } |
|
81 | + /** |
|
82 | + * @return void |
|
83 | + */ |
|
84 | + protected function checkAjaxRequest(array $request) |
|
85 | + { |
|
86 | + if (!isset($request['_action'])) { |
|
87 | + $this->sendAjaxError('request must include an action', $request); |
|
88 | + } |
|
89 | + if (empty($request['_ajax_request'])) { |
|
90 | + $this->sendAjaxError('request is invalid', $request); |
|
91 | + } |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * All ajax requests in the plugin are triggered by a single action hook: glsr_action, |
|
96 | - * while each ajax route is determined by $_POST[request][_action]. |
|
97 | - * @return array |
|
98 | - */ |
|
99 | - protected function getRequest() |
|
100 | - { |
|
101 | - $request = glsr(Helper::class)->filterInputArray(Application::ID); |
|
102 | - if (glsr(Helper::class)->filterInput('action') == Application::PREFIX.'action') { |
|
103 | - $request['_ajax_request'] = true; |
|
104 | - } |
|
105 | - if ('submit-review' == glsr(Helper::class)->filterInput('_action', $request)) { |
|
106 | - $request['_recaptcha-token'] = glsr(Helper::class)->filterInput('g-recaptcha-response'); |
|
107 | - } |
|
108 | - return $request; |
|
109 | - } |
|
94 | + /** |
|
95 | + * All ajax requests in the plugin are triggered by a single action hook: glsr_action, |
|
96 | + * while each ajax route is determined by $_POST[request][_action]. |
|
97 | + * @return array |
|
98 | + */ |
|
99 | + protected function getRequest() |
|
100 | + { |
|
101 | + $request = glsr(Helper::class)->filterInputArray(Application::ID); |
|
102 | + if (glsr(Helper::class)->filterInput('action') == Application::PREFIX.'action') { |
|
103 | + $request['_ajax_request'] = true; |
|
104 | + } |
|
105 | + if ('submit-review' == glsr(Helper::class)->filterInput('_action', $request)) { |
|
106 | + $request['_recaptcha-token'] = glsr(Helper::class)->filterInput('g-recaptcha-response'); |
|
107 | + } |
|
108 | + return $request; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * @return bool |
|
113 | - */ |
|
114 | - protected function isValidPostRequest(array $request = []) |
|
115 | - { |
|
116 | - return !empty($request['_action']) && empty($request['_ajax_request']); |
|
117 | - } |
|
111 | + /** |
|
112 | + * @return bool |
|
113 | + */ |
|
114 | + protected function isValidPostRequest(array $request = []) |
|
115 | + { |
|
116 | + return !empty($request['_action']) && empty($request['_ajax_request']); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - protected function isValidPublicNonce(array $request) |
|
123 | - { |
|
124 | - if (is_user_logged_in() && !wp_verify_nonce($request['_nonce'], $request['_action'])) { |
|
125 | - glsr_log()->error('nonce check failed for public request')->debug($request); |
|
126 | - return false; |
|
127 | - } |
|
128 | - return true; |
|
129 | - } |
|
119 | + /** |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + protected function isValidPublicNonce(array $request) |
|
123 | + { |
|
124 | + if (is_user_logged_in() && !wp_verify_nonce($request['_nonce'], $request['_action'])) { |
|
125 | + glsr_log()->error('nonce check failed for public request')->debug($request); |
|
126 | + return false; |
|
127 | + } |
|
128 | + return true; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @param string $type |
|
133 | - * @param string $action |
|
134 | - * @return void |
|
135 | - */ |
|
136 | - protected function routeRequest($type, $action, array $request = []) |
|
137 | - { |
|
138 | - $actionHook = 'site-reviews/route/'.$type.'/request'; |
|
139 | - $controller = glsr(glsr(Helper::class)->buildClassName($type.'-controller', 'Controllers')); |
|
140 | - $method = glsr(Helper::class)->buildMethodName($action, 'router'); |
|
141 | - $request = apply_filters('site-reviews/route/request', $request, $action, $type); |
|
142 | - do_action($actionHook, $action, $request); |
|
143 | - if (is_callable([$controller, $method])) { |
|
144 | - call_user_func([$controller, $method], $request); |
|
145 | - return; |
|
146 | - } |
|
147 | - if (0 === did_action($actionHook)) { |
|
148 | - glsr_log('Unknown '.$type.' router request: '.$action); |
|
149 | - } |
|
150 | - } |
|
131 | + /** |
|
132 | + * @param string $type |
|
133 | + * @param string $action |
|
134 | + * @return void |
|
135 | + */ |
|
136 | + protected function routeRequest($type, $action, array $request = []) |
|
137 | + { |
|
138 | + $actionHook = 'site-reviews/route/'.$type.'/request'; |
|
139 | + $controller = glsr(glsr(Helper::class)->buildClassName($type.'-controller', 'Controllers')); |
|
140 | + $method = glsr(Helper::class)->buildMethodName($action, 'router'); |
|
141 | + $request = apply_filters('site-reviews/route/request', $request, $action, $type); |
|
142 | + do_action($actionHook, $action, $request); |
|
143 | + if (is_callable([$controller, $method])) { |
|
144 | + call_user_func([$controller, $method], $request); |
|
145 | + return; |
|
146 | + } |
|
147 | + if (0 === did_action($actionHook)) { |
|
148 | + glsr_log('Unknown '.$type.' router request: '.$action); |
|
149 | + } |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @param string $error |
|
154 | - * @param int $statusCode |
|
155 | - * @return void |
|
156 | - */ |
|
157 | - protected function sendAjaxError($error, array $request, $statusCode = 400) |
|
158 | - { |
|
159 | - glsr_log()->error($error)->debug($request); |
|
160 | - glsr(Notice::class)->addError(__('There was an error (try reloading the page).', 'site-reviews').' <code>'.$error.'</code>'); |
|
161 | - wp_send_json_error([ |
|
162 | - 'message' => __('The form could not be submitted. Please notify the site administrator.', 'site-reviews'), |
|
163 | - 'notices' => glsr(Notice::class)->get(), |
|
164 | - 'error' => $error, |
|
165 | - ]); |
|
166 | - } |
|
152 | + /** |
|
153 | + * @param string $error |
|
154 | + * @param int $statusCode |
|
155 | + * @return void |
|
156 | + */ |
|
157 | + protected function sendAjaxError($error, array $request, $statusCode = 400) |
|
158 | + { |
|
159 | + glsr_log()->error($error)->debug($request); |
|
160 | + glsr(Notice::class)->addError(__('There was an error (try reloading the page).', 'site-reviews').' <code>'.$error.'</code>'); |
|
161 | + wp_send_json_error([ |
|
162 | + 'message' => __('The form could not be submitted. Please notify the site administrator.', 'site-reviews'), |
|
163 | + 'notices' => glsr(Notice::class)->get(), |
|
164 | + 'error' => $error, |
|
165 | + ]); |
|
166 | + } |
|
167 | 167 | } |
@@ -9,128 +9,128 @@ |
||
9 | 9 | |
10 | 10 | class Helper |
11 | 11 | { |
12 | - use Arr; |
|
13 | - use Str; |
|
12 | + use Arr; |
|
13 | + use Str; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param string $name |
|
17 | - * @param string $path |
|
18 | - * @return string |
|
19 | - */ |
|
20 | - public function buildClassName($name, $path = '') |
|
21 | - { |
|
22 | - $className = $this->camelCase($name); |
|
23 | - $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\'); |
|
24 | - return !empty($path) |
|
25 | - ? __NAMESPACE__.'\\'.$path.'\\'.$className |
|
26 | - : $className; |
|
27 | - } |
|
15 | + /** |
|
16 | + * @param string $name |
|
17 | + * @param string $path |
|
18 | + * @return string |
|
19 | + */ |
|
20 | + public function buildClassName($name, $path = '') |
|
21 | + { |
|
22 | + $className = $this->camelCase($name); |
|
23 | + $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\'); |
|
24 | + return !empty($path) |
|
25 | + ? __NAMESPACE__.'\\'.$path.'\\'.$className |
|
26 | + : $className; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @param string $name |
|
31 | - * @param string $prefix |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - public function buildMethodName($name, $prefix = '') |
|
35 | - { |
|
36 | - return lcfirst($prefix.$this->buildClassName($name)); |
|
37 | - } |
|
29 | + /** |
|
30 | + * @param string $name |
|
31 | + * @param string $prefix |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + public function buildMethodName($name, $prefix = '') |
|
35 | + { |
|
36 | + return lcfirst($prefix.$this->buildClassName($name)); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param string $name |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function buildPropertyName($name) |
|
44 | - { |
|
45 | - return lcfirst($this->buildClassName($name)); |
|
46 | - } |
|
39 | + /** |
|
40 | + * @param string $name |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function buildPropertyName($name) |
|
44 | + { |
|
45 | + return lcfirst($this->buildClassName($name)); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param string $cast |
|
50 | - * @param mixed $value |
|
51 | - * @return mixed |
|
52 | - */ |
|
53 | - public function castTo($cast = '', $value) |
|
54 | - { |
|
55 | - switch ($cast) { |
|
56 | - case 'array': |
|
57 | - return (array) $value; |
|
58 | - case 'bool': |
|
59 | - case 'boolean': |
|
60 | - return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
61 | - case 'float': |
|
62 | - return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND); |
|
63 | - case 'int': |
|
64 | - case 'integer': |
|
65 | - return (int) filter_var($value, FILTER_VALIDATE_INT); |
|
66 | - case 'object': |
|
67 | - return (object) (array) $value; |
|
68 | - case 'str': |
|
69 | - case 'string': |
|
70 | - if (is_object($value) && in_array('__toString', get_class_methods($value))) { |
|
71 | - return (string) $value->__toString(); |
|
72 | - } |
|
73 | - if (is_array($value) || is_object($value)) { |
|
74 | - return serialize($value); |
|
75 | - } |
|
76 | - return (string) $value; |
|
77 | - default: |
|
78 | - return $value; |
|
79 | - } |
|
80 | - } |
|
48 | + /** |
|
49 | + * @param string $cast |
|
50 | + * @param mixed $value |
|
51 | + * @return mixed |
|
52 | + */ |
|
53 | + public function castTo($cast = '', $value) |
|
54 | + { |
|
55 | + switch ($cast) { |
|
56 | + case 'array': |
|
57 | + return (array) $value; |
|
58 | + case 'bool': |
|
59 | + case 'boolean': |
|
60 | + return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
61 | + case 'float': |
|
62 | + return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND); |
|
63 | + case 'int': |
|
64 | + case 'integer': |
|
65 | + return (int) filter_var($value, FILTER_VALIDATE_INT); |
|
66 | + case 'object': |
|
67 | + return (object) (array) $value; |
|
68 | + case 'str': |
|
69 | + case 'string': |
|
70 | + if (is_object($value) && in_array('__toString', get_class_methods($value))) { |
|
71 | + return (string) $value->__toString(); |
|
72 | + } |
|
73 | + if (is_array($value) || is_object($value)) { |
|
74 | + return serialize($value); |
|
75 | + } |
|
76 | + return (string) $value; |
|
77 | + default: |
|
78 | + return $value; |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @param string $key |
|
84 | - * @return mixed |
|
85 | - */ |
|
86 | - public function filterInput($key, array $request = []) |
|
87 | - { |
|
88 | - if (isset($request[$key])) { |
|
89 | - return $request[$key]; |
|
90 | - } |
|
91 | - $variable = filter_input(INPUT_POST, $key); |
|
92 | - if (is_null($variable) && isset($_POST[$key])) { |
|
93 | - $variable = $_POST[$key]; |
|
94 | - } |
|
95 | - return $variable; |
|
96 | - } |
|
82 | + /** |
|
83 | + * @param string $key |
|
84 | + * @return mixed |
|
85 | + */ |
|
86 | + public function filterInput($key, array $request = []) |
|
87 | + { |
|
88 | + if (isset($request[$key])) { |
|
89 | + return $request[$key]; |
|
90 | + } |
|
91 | + $variable = filter_input(INPUT_POST, $key); |
|
92 | + if (is_null($variable) && isset($_POST[$key])) { |
|
93 | + $variable = $_POST[$key]; |
|
94 | + } |
|
95 | + return $variable; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @param string $key |
|
100 | - * @return array |
|
101 | - */ |
|
102 | - public function filterInputArray($key) |
|
103 | - { |
|
104 | - $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
105 | - if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
|
106 | - $variable = $_POST[$key]; |
|
107 | - } |
|
108 | - return (array) $variable; |
|
109 | - } |
|
98 | + /** |
|
99 | + * @param string $key |
|
100 | + * @return array |
|
101 | + */ |
|
102 | + public function filterInputArray($key) |
|
103 | + { |
|
104 | + $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
105 | + if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
|
106 | + $variable = $_POST[$key]; |
|
107 | + } |
|
108 | + return (array) $variable; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public function getIpAddress() |
|
115 | - { |
|
116 | - $cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
|
117 | - $ipv6 = defined('AF_INET6') |
|
118 | - ? $cloudflareIps['v6'] |
|
119 | - : []; |
|
120 | - $whitelist = apply_filters('site-reviews/whip/whitelist', [ |
|
121 | - Whip::CLOUDFLARE_HEADERS => [ |
|
122 | - Whip::IPV4 => $cloudflareIps['v4'], |
|
123 | - Whip::IPV6 => $ipv6, |
|
124 | - ], |
|
125 | - Whip::CUSTOM_HEADERS => [ |
|
126 | - Whip::IPV4 => ['127.0.0.1'], |
|
127 | - Whip::IPV6 => ['::1'], |
|
128 | - ], |
|
129 | - ]); |
|
130 | - $methods = Whip::CUSTOM_HEADERS | Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR; |
|
131 | - $methods = apply_filters('site-reviews/whip/methods', $methods); |
|
132 | - $whip = new Whip($methods, $whitelist); |
|
133 | - do_action_ref_array('site-reviews/whip', [$whip]); |
|
134 | - return (string) $whip->getValidIpAddress(); |
|
135 | - } |
|
111 | + /** |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public function getIpAddress() |
|
115 | + { |
|
116 | + $cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
|
117 | + $ipv6 = defined('AF_INET6') |
|
118 | + ? $cloudflareIps['v6'] |
|
119 | + : []; |
|
120 | + $whitelist = apply_filters('site-reviews/whip/whitelist', [ |
|
121 | + Whip::CLOUDFLARE_HEADERS => [ |
|
122 | + Whip::IPV4 => $cloudflareIps['v4'], |
|
123 | + Whip::IPV6 => $ipv6, |
|
124 | + ], |
|
125 | + Whip::CUSTOM_HEADERS => [ |
|
126 | + Whip::IPV4 => ['127.0.0.1'], |
|
127 | + Whip::IPV6 => ['::1'], |
|
128 | + ], |
|
129 | + ]); |
|
130 | + $methods = Whip::CUSTOM_HEADERS | Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR; |
|
131 | + $methods = apply_filters('site-reviews/whip/methods', $methods); |
|
132 | + $whip = new Whip($methods, $whitelist); |
|
133 | + do_action_ref_array('site-reviews/whip', [$whip]); |
|
134 | + return (string) $whip->getValidIpAddress(); |
|
135 | + } |
|
136 | 136 | } |
@@ -7,308 +7,308 @@ |
||
7 | 7 | |
8 | 8 | final class Application extends Container |
9 | 9 | { |
10 | - const CAPABILITY = 'edit_others_posts'; |
|
11 | - const CRON_EVENT = 'site-reviews/schedule/session/purge'; |
|
12 | - const ID = 'site-reviews'; |
|
13 | - const PAGED_QUERY_VAR = 'reviews-page'; |
|
14 | - const POST_TYPE = 'site-review'; |
|
15 | - const PREFIX = 'glsr_'; |
|
16 | - const TAXONOMY = 'site-review-category'; |
|
10 | + const CAPABILITY = 'edit_others_posts'; |
|
11 | + const CRON_EVENT = 'site-reviews/schedule/session/purge'; |
|
12 | + const ID = 'site-reviews'; |
|
13 | + const PAGED_QUERY_VAR = 'reviews-page'; |
|
14 | + const POST_TYPE = 'site-review'; |
|
15 | + const PREFIX = 'glsr_'; |
|
16 | + const TAXONOMY = 'site-review-category'; |
|
17 | 17 | |
18 | - public $defaults; |
|
19 | - public $deprecated = []; |
|
20 | - public $file; |
|
21 | - public $languages; |
|
22 | - public $mceShortcodes = []; //defined elsewhere |
|
23 | - public $name; |
|
24 | - public $reviewTypes; |
|
25 | - public $schemas = []; //defined elsewhere |
|
26 | - public $version; |
|
18 | + public $defaults; |
|
19 | + public $deprecated = []; |
|
20 | + public $file; |
|
21 | + public $languages; |
|
22 | + public $mceShortcodes = []; //defined elsewhere |
|
23 | + public $name; |
|
24 | + public $reviewTypes; |
|
25 | + public $schemas = []; //defined elsewhere |
|
26 | + public $version; |
|
27 | 27 | |
28 | - public function __construct() |
|
29 | - { |
|
30 | - static::$instance = $this; |
|
31 | - $this->file = realpath(trailingslashit(dirname(__DIR__)).static::ID.'.php'); |
|
32 | - $plugin = get_file_data($this->file, [ |
|
33 | - 'languages' => 'Domain Path', |
|
34 | - 'name' => 'Plugin Name', |
|
35 | - 'version' => 'Version', |
|
36 | - ], 'plugin'); |
|
37 | - array_walk($plugin, function ($value, $key) { |
|
38 | - $this->$key = $value; |
|
39 | - }); |
|
40 | - } |
|
28 | + public function __construct() |
|
29 | + { |
|
30 | + static::$instance = $this; |
|
31 | + $this->file = realpath(trailingslashit(dirname(__DIR__)).static::ID.'.php'); |
|
32 | + $plugin = get_file_data($this->file, [ |
|
33 | + 'languages' => 'Domain Path', |
|
34 | + 'name' => 'Plugin Name', |
|
35 | + 'version' => 'Version', |
|
36 | + ], 'plugin'); |
|
37 | + array_walk($plugin, function ($value, $key) { |
|
38 | + $this->$key = $value; |
|
39 | + }); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * @return void |
|
44 | - */ |
|
45 | - public function activate() |
|
46 | - { |
|
47 | - $this->make(DefaultsManager::class)->set(); |
|
48 | - $this->scheduleCronJob(); |
|
49 | - $this->upgrade(); |
|
50 | - } |
|
42 | + /** |
|
43 | + * @return void |
|
44 | + */ |
|
45 | + public function activate() |
|
46 | + { |
|
47 | + $this->make(DefaultsManager::class)->set(); |
|
48 | + $this->scheduleCronJob(); |
|
49 | + $this->upgrade(); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function catchFatalError() |
|
56 | - { |
|
57 | - $error = error_get_last(); |
|
58 | - if (E_ERROR !== $error['type'] || false === strpos($error['message'], $this->path())) { |
|
59 | - return; |
|
60 | - } |
|
61 | - glsr_log()->error($error['message']); |
|
62 | - } |
|
52 | + /** |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function catchFatalError() |
|
56 | + { |
|
57 | + $error = error_get_last(); |
|
58 | + if (E_ERROR !== $error['type'] || false === strpos($error['message'], $this->path())) { |
|
59 | + return; |
|
60 | + } |
|
61 | + glsr_log()->error($error['message']); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @param string $name |
|
66 | - * @return array |
|
67 | - */ |
|
68 | - public function config($name) |
|
69 | - { |
|
70 | - $configFile = $this->path('config/'.$name.'.php'); |
|
71 | - $config = file_exists($configFile) |
|
72 | - ? include $configFile |
|
73 | - : []; |
|
74 | - return apply_filters('site-reviews/config/'.$name, $config); |
|
75 | - } |
|
64 | + /** |
|
65 | + * @param string $name |
|
66 | + * @return array |
|
67 | + */ |
|
68 | + public function config($name) |
|
69 | + { |
|
70 | + $configFile = $this->path('config/'.$name.'.php'); |
|
71 | + $config = file_exists($configFile) |
|
72 | + ? include $configFile |
|
73 | + : []; |
|
74 | + return apply_filters('site-reviews/config/'.$name, $config); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @param string $property |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function constant($property, $className = 'static') |
|
82 | - { |
|
83 | - $constant = $className.'::'.$property; |
|
84 | - return defined($constant) |
|
85 | - ? apply_filters('site-reviews/const/'.$property, constant($constant)) |
|
86 | - : ''; |
|
87 | - } |
|
77 | + /** |
|
78 | + * @param string $property |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function constant($property, $className = 'static') |
|
82 | + { |
|
83 | + $constant = $className.'::'.$property; |
|
84 | + return defined($constant) |
|
85 | + ? apply_filters('site-reviews/const/'.$property, constant($constant)) |
|
86 | + : ''; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * @return void |
|
91 | - */ |
|
92 | - public function deactivate() |
|
93 | - { |
|
94 | - $this->unscheduleCronJob(); |
|
95 | - } |
|
89 | + /** |
|
90 | + * @return void |
|
91 | + */ |
|
92 | + public function deactivate() |
|
93 | + { |
|
94 | + $this->unscheduleCronJob(); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @param string $view |
|
99 | - * @return void|string |
|
100 | - */ |
|
101 | - public function file($view) |
|
102 | - { |
|
103 | - $view.= '.php'; |
|
104 | - $filePaths = []; |
|
105 | - if (glsr(Helper::class)->startsWith('templates/', $view)) { |
|
106 | - $filePaths[] = $this->themePath(glsr(Helper::class)->removePrefix('templates/', $view)); |
|
107 | - } |
|
108 | - $filePaths[] = $this->path($view); |
|
109 | - $filePaths[] = $this->path('views/'.$view); |
|
110 | - foreach ($filePaths as $file) { |
|
111 | - if (!file_exists($file)) { |
|
112 | - continue; |
|
113 | - } |
|
114 | - return $file; |
|
115 | - } |
|
116 | - } |
|
97 | + /** |
|
98 | + * @param string $view |
|
99 | + * @return void|string |
|
100 | + */ |
|
101 | + public function file($view) |
|
102 | + { |
|
103 | + $view.= '.php'; |
|
104 | + $filePaths = []; |
|
105 | + if (glsr(Helper::class)->startsWith('templates/', $view)) { |
|
106 | + $filePaths[] = $this->themePath(glsr(Helper::class)->removePrefix('templates/', $view)); |
|
107 | + } |
|
108 | + $filePaths[] = $this->path($view); |
|
109 | + $filePaths[] = $this->path('views/'.$view); |
|
110 | + foreach ($filePaths as $file) { |
|
111 | + if (!file_exists($file)) { |
|
112 | + continue; |
|
113 | + } |
|
114 | + return $file; |
|
115 | + } |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - public function getDefaults() |
|
122 | - { |
|
123 | - if (empty($this->defaults)) { |
|
124 | - $this->defaults = $this->make(DefaultsManager::class)->get(); |
|
125 | - $this->upgrade(); |
|
126 | - } |
|
127 | - return apply_filters('site-reviews/get/defaults', $this->defaults); |
|
128 | - } |
|
118 | + /** |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + public function getDefaults() |
|
122 | + { |
|
123 | + if (empty($this->defaults)) { |
|
124 | + $this->defaults = $this->make(DefaultsManager::class)->get(); |
|
125 | + $this->upgrade(); |
|
126 | + } |
|
127 | + return apply_filters('site-reviews/get/defaults', $this->defaults); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function getPermission($page = '') |
|
134 | - { |
|
135 | - $permissions = [ |
|
136 | - 'addons' => 'install_plugins', |
|
137 | - 'settings' => 'manage_options', |
|
138 | - // 'welcome' => 'activate_plugins', |
|
139 | - ]; |
|
140 | - return glsr_get($permissions, $page, $this->constant('CAPABILITY')); |
|
141 | - } |
|
130 | + /** |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function getPermission($page = '') |
|
134 | + { |
|
135 | + $permissions = [ |
|
136 | + 'addons' => 'install_plugins', |
|
137 | + 'settings' => 'manage_options', |
|
138 | + // 'welcome' => 'activate_plugins', |
|
139 | + ]; |
|
140 | + return glsr_get($permissions, $page, $this->constant('CAPABILITY')); |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * @return bool |
|
145 | - */ |
|
146 | - public function hasPermission($page = '') |
|
147 | - { |
|
148 | - $isAdmin = $this->isAdmin(); |
|
149 | - return !$isAdmin || ($isAdmin && current_user_can($this->getPermission($page))); |
|
150 | - } |
|
143 | + /** |
|
144 | + * @return bool |
|
145 | + */ |
|
146 | + public function hasPermission($page = '') |
|
147 | + { |
|
148 | + $isAdmin = $this->isAdmin(); |
|
149 | + return !$isAdmin || ($isAdmin && current_user_can($this->getPermission($page))); |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @return void |
|
154 | - */ |
|
155 | - public function init() |
|
156 | - { |
|
157 | - $this->make(Actions::class)->run(); |
|
158 | - $this->make(Filters::class)->run(); |
|
159 | - } |
|
152 | + /** |
|
153 | + * @return void |
|
154 | + */ |
|
155 | + public function init() |
|
156 | + { |
|
157 | + $this->make(Actions::class)->run(); |
|
158 | + $this->make(Filters::class)->run(); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * @return bool |
|
163 | - */ |
|
164 | - public function isAdmin() |
|
165 | - { |
|
166 | - return is_admin() && !wp_doing_ajax(); |
|
167 | - } |
|
161 | + /** |
|
162 | + * @return bool |
|
163 | + */ |
|
164 | + public function isAdmin() |
|
165 | + { |
|
166 | + return is_admin() && !wp_doing_ajax(); |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * @param string $file |
|
171 | - * @return string |
|
172 | - */ |
|
173 | - public function path($file = '', $realpath = true) |
|
174 | - { |
|
175 | - $path = plugin_dir_path($this->file); |
|
176 | - if (!$realpath) { |
|
177 | - $path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
|
178 | - } |
|
179 | - $path = trailingslashit($path).ltrim(trim($file), '/'); |
|
180 | - return apply_filters('site-reviews/path', $path, $file); |
|
181 | - } |
|
169 | + /** |
|
170 | + * @param string $file |
|
171 | + * @return string |
|
172 | + */ |
|
173 | + public function path($file = '', $realpath = true) |
|
174 | + { |
|
175 | + $path = plugin_dir_path($this->file); |
|
176 | + if (!$realpath) { |
|
177 | + $path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file)); |
|
178 | + } |
|
179 | + $path = trailingslashit($path).ltrim(trim($file), '/'); |
|
180 | + return apply_filters('site-reviews/path', $path, $file); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * @return void |
|
185 | - */ |
|
186 | - public function registerAddons() |
|
187 | - { |
|
188 | - do_action('site-reviews/addon/register', $this); |
|
189 | - } |
|
183 | + /** |
|
184 | + * @return void |
|
185 | + */ |
|
186 | + public function registerAddons() |
|
187 | + { |
|
188 | + do_action('site-reviews/addon/register', $this); |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * @return void |
|
193 | - */ |
|
194 | - public function registerLanguages() |
|
195 | - { |
|
196 | - load_plugin_textdomain(static::ID, false, |
|
197 | - trailingslashit(plugin_basename($this->path()).'/'.$this->languages) |
|
198 | - ); |
|
199 | - } |
|
191 | + /** |
|
192 | + * @return void |
|
193 | + */ |
|
194 | + public function registerLanguages() |
|
195 | + { |
|
196 | + load_plugin_textdomain(static::ID, false, |
|
197 | + trailingslashit(plugin_basename($this->path()).'/'.$this->languages) |
|
198 | + ); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * @return void |
|
203 | - */ |
|
204 | - public function registerReviewTypes() |
|
205 | - { |
|
206 | - $types = apply_filters('site-reviews/addon/types', []); |
|
207 | - $this->reviewTypes = wp_parse_args($types, [ |
|
208 | - 'local' => __('Local', 'site-reviews'), |
|
209 | - ]); |
|
210 | - } |
|
201 | + /** |
|
202 | + * @return void |
|
203 | + */ |
|
204 | + public function registerReviewTypes() |
|
205 | + { |
|
206 | + $types = apply_filters('site-reviews/addon/types', []); |
|
207 | + $this->reviewTypes = wp_parse_args($types, [ |
|
208 | + 'local' => __('Local', 'site-reviews'), |
|
209 | + ]); |
|
210 | + } |
|
211 | 211 | |
212 | - /** |
|
213 | - * @param string $view |
|
214 | - * @return void |
|
215 | - */ |
|
216 | - public function render($view, array $data = []) |
|
217 | - { |
|
218 | - $view = apply_filters('site-reviews/render/view', $view, $data); |
|
219 | - $file = apply_filters('site-reviews/views/file', $this->file($view), $view, $data); |
|
220 | - if (!file_exists($file)) { |
|
221 | - glsr_log()->error('File not found: '.$file); |
|
222 | - return; |
|
223 | - } |
|
224 | - $data = apply_filters('site-reviews/views/data', $data, $view); |
|
225 | - extract($data); |
|
226 | - include $file; |
|
227 | - } |
|
212 | + /** |
|
213 | + * @param string $view |
|
214 | + * @return void |
|
215 | + */ |
|
216 | + public function render($view, array $data = []) |
|
217 | + { |
|
218 | + $view = apply_filters('site-reviews/render/view', $view, $data); |
|
219 | + $file = apply_filters('site-reviews/views/file', $this->file($view), $view, $data); |
|
220 | + if (!file_exists($file)) { |
|
221 | + glsr_log()->error('File not found: '.$file); |
|
222 | + return; |
|
223 | + } |
|
224 | + $data = apply_filters('site-reviews/views/data', $data, $view); |
|
225 | + extract($data); |
|
226 | + include $file; |
|
227 | + } |
|
228 | 228 | |
229 | - /** |
|
230 | - * @return void |
|
231 | - */ |
|
232 | - public function scheduleCronJob() |
|
233 | - { |
|
234 | - if (wp_next_scheduled(static::CRON_EVENT)) { |
|
235 | - return; |
|
236 | - } |
|
237 | - wp_schedule_event(time(), 'twicedaily', static::CRON_EVENT); |
|
238 | - } |
|
229 | + /** |
|
230 | + * @return void |
|
231 | + */ |
|
232 | + public function scheduleCronJob() |
|
233 | + { |
|
234 | + if (wp_next_scheduled(static::CRON_EVENT)) { |
|
235 | + return; |
|
236 | + } |
|
237 | + wp_schedule_event(time(), 'twicedaily', static::CRON_EVENT); |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * @param string $file |
|
242 | - * @return string |
|
243 | - */ |
|
244 | - public function themePath($file = '') |
|
245 | - { |
|
246 | - return get_stylesheet_directory().'/'.static::ID.'/'.ltrim(trim($file), '/'); |
|
247 | - } |
|
240 | + /** |
|
241 | + * @param string $file |
|
242 | + * @return string |
|
243 | + */ |
|
244 | + public function themePath($file = '') |
|
245 | + { |
|
246 | + return get_stylesheet_directory().'/'.static::ID.'/'.ltrim(trim($file), '/'); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * @return void |
|
251 | - */ |
|
252 | - public function unscheduleCronJob() |
|
253 | - { |
|
254 | - wp_unschedule_event(intval(wp_next_scheduled(static::CRON_EVENT)), static::CRON_EVENT); |
|
255 | - } |
|
249 | + /** |
|
250 | + * @return void |
|
251 | + */ |
|
252 | + public function unscheduleCronJob() |
|
253 | + { |
|
254 | + wp_unschedule_event(intval(wp_next_scheduled(static::CRON_EVENT)), static::CRON_EVENT); |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * @return void |
|
259 | - */ |
|
260 | - public function upgrade() |
|
261 | - { |
|
262 | - $this->make(Upgrader::class)->run(); |
|
263 | - } |
|
257 | + /** |
|
258 | + * @return void |
|
259 | + */ |
|
260 | + public function upgrade() |
|
261 | + { |
|
262 | + $this->make(Upgrader::class)->run(); |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * @param mixed $upgrader |
|
267 | - * @return void |
|
268 | - * @action upgrader_process_complete |
|
269 | - */ |
|
270 | - public function upgraded($upgrader, array $data) |
|
271 | - { |
|
272 | - if (array_key_exists('plugins', $data) |
|
273 | - && in_array(plugin_basename($this->file), $data['plugins']) |
|
274 | - && 'update' === $data['action'] |
|
275 | - && 'plugin' === $data['type'] |
|
276 | - ) { |
|
277 | - $this->upgrade(); |
|
278 | - } |
|
279 | - } |
|
265 | + /** |
|
266 | + * @param mixed $upgrader |
|
267 | + * @return void |
|
268 | + * @action upgrader_process_complete |
|
269 | + */ |
|
270 | + public function upgraded($upgrader, array $data) |
|
271 | + { |
|
272 | + if (array_key_exists('plugins', $data) |
|
273 | + && in_array(plugin_basename($this->file), $data['plugins']) |
|
274 | + && 'update' === $data['action'] |
|
275 | + && 'plugin' === $data['type'] |
|
276 | + ) { |
|
277 | + $this->upgrade(); |
|
278 | + } |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * @param string $path |
|
283 | - * @return string |
|
284 | - */ |
|
285 | - public function url($path = '') |
|
286 | - { |
|
287 | - $url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
|
288 | - return apply_filters('site-reviews/url', $url, $path); |
|
289 | - } |
|
281 | + /** |
|
282 | + * @param string $path |
|
283 | + * @return string |
|
284 | + */ |
|
285 | + public function url($path = '') |
|
286 | + { |
|
287 | + $url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/')); |
|
288 | + return apply_filters('site-reviews/url', $url, $path); |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * @param string $versionLevel |
|
293 | - * @return string |
|
294 | - */ |
|
295 | - public function version($versionLevel = '') |
|
296 | - { |
|
297 | - $pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i'; |
|
298 | - preg_match($pattern, $this->version, $matches); |
|
299 | - switch ($versionLevel) { |
|
300 | - case 'major': |
|
301 | - $version = glsr_get($matches, 1); |
|
302 | - break; |
|
303 | - case 'minor': |
|
304 | - $version = glsr_get($matches, 1).glsr_get($matches, 2); |
|
305 | - break; |
|
306 | - case 'patch': |
|
307 | - $version = glsr_get($matches, 1).glsr_get($matches, 2).glsr_get($matches, 3); |
|
308 | - break; |
|
309 | - } |
|
310 | - return empty($version) |
|
311 | - ? $this->version |
|
312 | - : $version; |
|
313 | - } |
|
291 | + /** |
|
292 | + * @param string $versionLevel |
|
293 | + * @return string |
|
294 | + */ |
|
295 | + public function version($versionLevel = '') |
|
296 | + { |
|
297 | + $pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i'; |
|
298 | + preg_match($pattern, $this->version, $matches); |
|
299 | + switch ($versionLevel) { |
|
300 | + case 'major': |
|
301 | + $version = glsr_get($matches, 1); |
|
302 | + break; |
|
303 | + case 'minor': |
|
304 | + $version = glsr_get($matches, 1).glsr_get($matches, 2); |
|
305 | + break; |
|
306 | + case 'patch': |
|
307 | + $version = glsr_get($matches, 1).glsr_get($matches, 2).glsr_get($matches, 3); |
|
308 | + break; |
|
309 | + } |
|
310 | + return empty($version) |
|
311 | + ? $this->version |
|
312 | + : $version; |
|
313 | + } |
|
314 | 314 | } |
@@ -13,189 +13,189 @@ |
||
13 | 13 | |
14 | 14 | class MenuController extends Controller |
15 | 15 | { |
16 | - /** |
|
17 | - * @return void |
|
18 | - * @action admin_menu |
|
19 | - */ |
|
20 | - public function registerMenuCount() |
|
21 | - { |
|
22 | - global $menu, $typenow; |
|
23 | - foreach ($menu as $key => $value) { |
|
24 | - if (!isset($value[2]) || $value[2] != 'edit.php?post_type='.Application::POST_TYPE) { |
|
25 | - continue; |
|
26 | - } |
|
27 | - $postCount = wp_count_posts(Application::POST_TYPE); |
|
28 | - $pendingCount = glsr(Builder::class)->span(number_format_i18n($postCount->pending), [ |
|
29 | - 'class' => 'unapproved-count', |
|
30 | - ]); |
|
31 | - $awaitingModeration = glsr(Builder::class)->span($pendingCount, [ |
|
32 | - 'class' => 'awaiting-mod count-'.$postCount->pending, |
|
33 | - ]); |
|
34 | - $menu[$key][0].= ' '.$awaitingModeration; |
|
35 | - if (Application::POST_TYPE === $typenow) { |
|
36 | - $menu[$key][4].= ' current'; |
|
37 | - } |
|
38 | - break; |
|
39 | - } |
|
40 | - } |
|
16 | + /** |
|
17 | + * @return void |
|
18 | + * @action admin_menu |
|
19 | + */ |
|
20 | + public function registerMenuCount() |
|
21 | + { |
|
22 | + global $menu, $typenow; |
|
23 | + foreach ($menu as $key => $value) { |
|
24 | + if (!isset($value[2]) || $value[2] != 'edit.php?post_type='.Application::POST_TYPE) { |
|
25 | + continue; |
|
26 | + } |
|
27 | + $postCount = wp_count_posts(Application::POST_TYPE); |
|
28 | + $pendingCount = glsr(Builder::class)->span(number_format_i18n($postCount->pending), [ |
|
29 | + 'class' => 'unapproved-count', |
|
30 | + ]); |
|
31 | + $awaitingModeration = glsr(Builder::class)->span($pendingCount, [ |
|
32 | + 'class' => 'awaiting-mod count-'.$postCount->pending, |
|
33 | + ]); |
|
34 | + $menu[$key][0].= ' '.$awaitingModeration; |
|
35 | + if (Application::POST_TYPE === $typenow) { |
|
36 | + $menu[$key][4].= ' current'; |
|
37 | + } |
|
38 | + break; |
|
39 | + } |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * @return void |
|
44 | - * @action admin_menu |
|
45 | - */ |
|
46 | - public function registerSubMenus() |
|
47 | - { |
|
48 | - $pages = $this->parseWithFilter('submenu/pages', [ |
|
49 | - 'settings' => __('Settings', 'site-reviews'), |
|
50 | - 'tools' => __('Tools', 'site-reviews'), |
|
51 | - 'addons' => __('Add-ons', 'site-reviews'), |
|
52 | - 'documentation' => __('Documentation', 'site-reviews'), |
|
53 | - ]); |
|
54 | - foreach ($pages as $slug => $title) { |
|
55 | - $method = glsr(Helper::class)->buildMethodName('render-'.$slug.'-menu'); |
|
56 | - $callback = apply_filters('site-reviews/addon/submenu/callback', [$this, $method], $slug); |
|
57 | - if (!is_callable($callback)) { |
|
58 | - continue; |
|
59 | - } |
|
60 | - add_submenu_page('edit.php?post_type='.Application::POST_TYPE, $title, $title, glsr()->getPermission($slug), $slug, $callback); |
|
61 | - } |
|
62 | - } |
|
42 | + /** |
|
43 | + * @return void |
|
44 | + * @action admin_menu |
|
45 | + */ |
|
46 | + public function registerSubMenus() |
|
47 | + { |
|
48 | + $pages = $this->parseWithFilter('submenu/pages', [ |
|
49 | + 'settings' => __('Settings', 'site-reviews'), |
|
50 | + 'tools' => __('Tools', 'site-reviews'), |
|
51 | + 'addons' => __('Add-ons', 'site-reviews'), |
|
52 | + 'documentation' => __('Documentation', 'site-reviews'), |
|
53 | + ]); |
|
54 | + foreach ($pages as $slug => $title) { |
|
55 | + $method = glsr(Helper::class)->buildMethodName('render-'.$slug.'-menu'); |
|
56 | + $callback = apply_filters('site-reviews/addon/submenu/callback', [$this, $method], $slug); |
|
57 | + if (!is_callable($callback)) { |
|
58 | + continue; |
|
59 | + } |
|
60 | + add_submenu_page('edit.php?post_type='.Application::POST_TYPE, $title, $title, glsr()->getPermission($slug), $slug, $callback); |
|
61 | + } |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return void |
|
66 | - * @see $this->registerSubMenus() |
|
67 | - * @callback add_submenu_page |
|
68 | - */ |
|
69 | - public function renderAddonsMenu() |
|
70 | - { |
|
71 | - $this->renderPage('addons', [ |
|
72 | - 'template' => glsr(Template::class), |
|
73 | - ]); |
|
74 | - } |
|
64 | + /** |
|
65 | + * @return void |
|
66 | + * @see $this->registerSubMenus() |
|
67 | + * @callback add_submenu_page |
|
68 | + */ |
|
69 | + public function renderAddonsMenu() |
|
70 | + { |
|
71 | + $this->renderPage('addons', [ |
|
72 | + 'template' => glsr(Template::class), |
|
73 | + ]); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @return void |
|
78 | - * @see $this->registerSubMenus() |
|
79 | - * @callback add_submenu_page |
|
80 | - */ |
|
81 | - public function renderDocumentationMenu() |
|
82 | - { |
|
83 | - $tabs = $this->parseWithFilter('documentation/tabs', [ |
|
84 | - 'support' => __('Support', 'site-reviews'), |
|
85 | - 'faq' => __('FAQ', 'site-reviews'), |
|
86 | - 'shortcodes' => __('Shortcodes', 'site-reviews'), |
|
87 | - 'hooks' => __('Hooks', 'site-reviews'), |
|
88 | - 'functions' => __('Functions', 'site-reviews'), |
|
89 | - 'addons' => __('Addons', 'site-reviews'), |
|
90 | - ]); |
|
91 | - $addons = apply_filters('site-reviews/addon/documentation', []); |
|
92 | - ksort($addons); |
|
93 | - if (empty($addons)) { |
|
94 | - unset($tabs['addons']); |
|
95 | - } |
|
96 | - $this->renderPage('documentation', [ |
|
97 | - 'addons' => $addons, |
|
98 | - 'tabs' => $tabs, |
|
99 | - ]); |
|
100 | - } |
|
76 | + /** |
|
77 | + * @return void |
|
78 | + * @see $this->registerSubMenus() |
|
79 | + * @callback add_submenu_page |
|
80 | + */ |
|
81 | + public function renderDocumentationMenu() |
|
82 | + { |
|
83 | + $tabs = $this->parseWithFilter('documentation/tabs', [ |
|
84 | + 'support' => __('Support', 'site-reviews'), |
|
85 | + 'faq' => __('FAQ', 'site-reviews'), |
|
86 | + 'shortcodes' => __('Shortcodes', 'site-reviews'), |
|
87 | + 'hooks' => __('Hooks', 'site-reviews'), |
|
88 | + 'functions' => __('Functions', 'site-reviews'), |
|
89 | + 'addons' => __('Addons', 'site-reviews'), |
|
90 | + ]); |
|
91 | + $addons = apply_filters('site-reviews/addon/documentation', []); |
|
92 | + ksort($addons); |
|
93 | + if (empty($addons)) { |
|
94 | + unset($tabs['addons']); |
|
95 | + } |
|
96 | + $this->renderPage('documentation', [ |
|
97 | + 'addons' => $addons, |
|
98 | + 'tabs' => $tabs, |
|
99 | + ]); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * @return void |
|
104 | - * @see $this->registerSubMenus() |
|
105 | - * @callback add_submenu_page |
|
106 | - */ |
|
107 | - public function renderSettingsMenu() |
|
108 | - { |
|
109 | - $tabs = $this->parseWithFilter('settings/tabs', [ |
|
110 | - 'general' => __('General', 'site-reviews'), |
|
111 | - 'reviews' => __('Reviews', 'site-reviews'), |
|
112 | - 'submissions' => __('Submissions', 'site-reviews'), |
|
113 | - 'schema' => __('Schema', 'site-reviews'), |
|
114 | - 'translations' => __('Translations', 'site-reviews'), |
|
115 | - 'addons' => __('Addons', 'site-reviews'), |
|
116 | - 'licenses' => __('Licenses', 'site-reviews'), |
|
117 | - ]); |
|
118 | - if (empty(glsr(Helper::class)->dataGet(glsr()->defaults, 'settings.addons'))) { |
|
119 | - unset($tabs['addons']); |
|
120 | - } |
|
121 | - if (empty(glsr(Helper::class)->dataGet(glsr()->defaults, 'settings.licenses'))) { |
|
122 | - unset($tabs['licenses']); |
|
123 | - } |
|
124 | - $this->renderPage('settings', [ |
|
125 | - 'settings' => glsr(Settings::class), |
|
126 | - 'tabs' => $tabs, |
|
127 | - ]); |
|
128 | - } |
|
102 | + /** |
|
103 | + * @return void |
|
104 | + * @see $this->registerSubMenus() |
|
105 | + * @callback add_submenu_page |
|
106 | + */ |
|
107 | + public function renderSettingsMenu() |
|
108 | + { |
|
109 | + $tabs = $this->parseWithFilter('settings/tabs', [ |
|
110 | + 'general' => __('General', 'site-reviews'), |
|
111 | + 'reviews' => __('Reviews', 'site-reviews'), |
|
112 | + 'submissions' => __('Submissions', 'site-reviews'), |
|
113 | + 'schema' => __('Schema', 'site-reviews'), |
|
114 | + 'translations' => __('Translations', 'site-reviews'), |
|
115 | + 'addons' => __('Addons', 'site-reviews'), |
|
116 | + 'licenses' => __('Licenses', 'site-reviews'), |
|
117 | + ]); |
|
118 | + if (empty(glsr(Helper::class)->dataGet(glsr()->defaults, 'settings.addons'))) { |
|
119 | + unset($tabs['addons']); |
|
120 | + } |
|
121 | + if (empty(glsr(Helper::class)->dataGet(glsr()->defaults, 'settings.licenses'))) { |
|
122 | + unset($tabs['licenses']); |
|
123 | + } |
|
124 | + $this->renderPage('settings', [ |
|
125 | + 'settings' => glsr(Settings::class), |
|
126 | + 'tabs' => $tabs, |
|
127 | + ]); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @return void |
|
132 | - * @see $this->registerSubMenus() |
|
133 | - * @callback add_submenu_page |
|
134 | - */ |
|
135 | - public function renderToolsMenu() |
|
136 | - { |
|
137 | - $tabs = $this->parseWithFilter('tools/tabs', [ |
|
138 | - 'general' => __('General', 'site-reviews'), |
|
139 | - 'sync' => __('Sync Reviews', 'site-reviews'), |
|
140 | - 'console' => __('Console', 'site-reviews'), |
|
141 | - 'system-info' => __('System Info', 'site-reviews'), |
|
142 | - ]); |
|
143 | - if (!apply_filters('site-reviews/addon/sync/enable', false)) { |
|
144 | - unset($tabs['sync']); |
|
145 | - } |
|
146 | - $this->renderPage('tools', [ |
|
147 | - 'data' => [ |
|
148 | - 'context' => [ |
|
149 | - 'base_url' => admin_url('edit.php?post_type='.Application::POST_TYPE), |
|
150 | - 'console' => strval(glsr(Console::class)), |
|
151 | - 'id' => Application::ID, |
|
152 | - 'system' => strval(glsr(System::class)), |
|
153 | - ], |
|
154 | - 'services' => apply_filters('site-reviews/addon/sync/services', []), |
|
155 | - ], |
|
156 | - 'tabs' => $tabs, |
|
157 | - 'template' => glsr(Template::class), |
|
158 | - ]); |
|
159 | - } |
|
130 | + /** |
|
131 | + * @return void |
|
132 | + * @see $this->registerSubMenus() |
|
133 | + * @callback add_submenu_page |
|
134 | + */ |
|
135 | + public function renderToolsMenu() |
|
136 | + { |
|
137 | + $tabs = $this->parseWithFilter('tools/tabs', [ |
|
138 | + 'general' => __('General', 'site-reviews'), |
|
139 | + 'sync' => __('Sync Reviews', 'site-reviews'), |
|
140 | + 'console' => __('Console', 'site-reviews'), |
|
141 | + 'system-info' => __('System Info', 'site-reviews'), |
|
142 | + ]); |
|
143 | + if (!apply_filters('site-reviews/addon/sync/enable', false)) { |
|
144 | + unset($tabs['sync']); |
|
145 | + } |
|
146 | + $this->renderPage('tools', [ |
|
147 | + 'data' => [ |
|
148 | + 'context' => [ |
|
149 | + 'base_url' => admin_url('edit.php?post_type='.Application::POST_TYPE), |
|
150 | + 'console' => strval(glsr(Console::class)), |
|
151 | + 'id' => Application::ID, |
|
152 | + 'system' => strval(glsr(System::class)), |
|
153 | + ], |
|
154 | + 'services' => apply_filters('site-reviews/addon/sync/services', []), |
|
155 | + ], |
|
156 | + 'tabs' => $tabs, |
|
157 | + 'template' => glsr(Template::class), |
|
158 | + ]); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * @return void |
|
163 | - * @action admin_init |
|
164 | - */ |
|
165 | - public function setCustomPermissions() |
|
166 | - { |
|
167 | - foreach (wp_roles()->roles as $role => $value) { |
|
168 | - wp_roles()->remove_cap($role, 'create_'.Application::POST_TYPE); |
|
169 | - } |
|
170 | - } |
|
161 | + /** |
|
162 | + * @return void |
|
163 | + * @action admin_init |
|
164 | + */ |
|
165 | + public function setCustomPermissions() |
|
166 | + { |
|
167 | + foreach (wp_roles()->roles as $role => $value) { |
|
168 | + wp_roles()->remove_cap($role, 'create_'.Application::POST_TYPE); |
|
169 | + } |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * @return string |
|
174 | - */ |
|
175 | - protected function getNotices() |
|
176 | - { |
|
177 | - return glsr(Builder::class)->div(glsr(Notice::class)->get(), [ |
|
178 | - 'id' => 'glsr-notices', |
|
179 | - ]); |
|
180 | - } |
|
172 | + /** |
|
173 | + * @return string |
|
174 | + */ |
|
175 | + protected function getNotices() |
|
176 | + { |
|
177 | + return glsr(Builder::class)->div(glsr(Notice::class)->get(), [ |
|
178 | + 'id' => 'glsr-notices', |
|
179 | + ]); |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * @param string $hookSuffix |
|
184 | - * @return array |
|
185 | - */ |
|
186 | - protected function parseWithFilter($hookSuffix, array $args = []) |
|
187 | - { |
|
188 | - return apply_filters('site-reviews/addon/'.$hookSuffix, $args); |
|
189 | - } |
|
182 | + /** |
|
183 | + * @param string $hookSuffix |
|
184 | + * @return array |
|
185 | + */ |
|
186 | + protected function parseWithFilter($hookSuffix, array $args = []) |
|
187 | + { |
|
188 | + return apply_filters('site-reviews/addon/'.$hookSuffix, $args); |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * @param string $page |
|
193 | - * @return void |
|
194 | - */ |
|
195 | - protected function renderPage($page, array $data = []) |
|
196 | - { |
|
197 | - $data['http_referer'] = (string) wp_get_referer(); |
|
198 | - $data['notices'] = $this->getNotices(); |
|
199 | - glsr()->render('pages/'.$page.'/index', $data); |
|
200 | - } |
|
191 | + /** |
|
192 | + * @param string $page |
|
193 | + * @return void |
|
194 | + */ |
|
195 | + protected function renderPage($page, array $data = []) |
|
196 | + { |
|
197 | + $data['http_referer'] = (string) wp_get_referer(); |
|
198 | + $data['notices'] = $this->getNotices(); |
|
199 | + glsr()->render('pages/'.$page.'/index', $data); |
|
200 | + } |
|
201 | 201 | } |
@@ -12,163 +12,163 @@ |
||
12 | 12 | |
13 | 13 | class AjaxController extends Controller |
14 | 14 | { |
15 | - /** |
|
16 | - * @return void |
|
17 | - */ |
|
18 | - public function routerChangeStatus(array $request) |
|
19 | - { |
|
20 | - wp_send_json_success($this->execute(new ChangeStatus($request))); |
|
21 | - } |
|
15 | + /** |
|
16 | + * @return void |
|
17 | + */ |
|
18 | + public function routerChangeStatus(array $request) |
|
19 | + { |
|
20 | + wp_send_json_success($this->execute(new ChangeStatus($request))); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * @return void |
|
25 | - */ |
|
26 | - public function routerClearConsole() |
|
27 | - { |
|
28 | - glsr(AdminController::class)->routerClearConsole(); |
|
29 | - wp_send_json_success([ |
|
30 | - 'console' => glsr(Console::class)->get(), |
|
31 | - 'notices' => glsr(Notice::class)->get(), |
|
32 | - ]); |
|
33 | - } |
|
23 | + /** |
|
24 | + * @return void |
|
25 | + */ |
|
26 | + public function routerClearConsole() |
|
27 | + { |
|
28 | + glsr(AdminController::class)->routerClearConsole(); |
|
29 | + wp_send_json_success([ |
|
30 | + 'console' => glsr(Console::class)->get(), |
|
31 | + 'notices' => glsr(Notice::class)->get(), |
|
32 | + ]); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - public function routerCountReviews() |
|
39 | - { |
|
40 | - glsr(AdminController::class)->routerCountReviews(); |
|
41 | - wp_send_json_success([ |
|
42 | - 'notices' => glsr(Notice::class)->get(), |
|
43 | - ]); |
|
44 | - } |
|
35 | + /** |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + public function routerCountReviews() |
|
39 | + { |
|
40 | + glsr(AdminController::class)->routerCountReviews(); |
|
41 | + wp_send_json_success([ |
|
42 | + 'notices' => glsr(Notice::class)->get(), |
|
43 | + ]); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function routerDismissNotice(array $request) |
|
50 | - { |
|
51 | - glsr(NoticeController::class)->routerDismissNotice($request); |
|
52 | - wp_send_json_success(); |
|
53 | - } |
|
46 | + /** |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function routerDismissNotice(array $request) |
|
50 | + { |
|
51 | + glsr(NoticeController::class)->routerDismissNotice($request); |
|
52 | + wp_send_json_success(); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function routerMceShortcode(array $request) |
|
59 | - { |
|
60 | - $shortcode = $request['shortcode']; |
|
61 | - $response = false; |
|
62 | - if (array_key_exists($shortcode, glsr()->mceShortcodes)) { |
|
63 | - $data = glsr()->mceShortcodes[$shortcode]; |
|
64 | - if (!empty($data['errors'])) { |
|
65 | - $data['btn_okay'] = [esc_html__('Okay', 'site-reviews')]; |
|
66 | - } |
|
67 | - $response = [ |
|
68 | - 'body' => $data['fields'], |
|
69 | - 'close' => $data['btn_close'], |
|
70 | - 'ok' => $data['btn_okay'], |
|
71 | - 'shortcode' => $shortcode, |
|
72 | - 'title' => $data['title'], |
|
73 | - ]; |
|
74 | - } |
|
75 | - wp_send_json_success($response); |
|
76 | - } |
|
55 | + /** |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function routerMceShortcode(array $request) |
|
59 | + { |
|
60 | + $shortcode = $request['shortcode']; |
|
61 | + $response = false; |
|
62 | + if (array_key_exists($shortcode, glsr()->mceShortcodes)) { |
|
63 | + $data = glsr()->mceShortcodes[$shortcode]; |
|
64 | + if (!empty($data['errors'])) { |
|
65 | + $data['btn_okay'] = [esc_html__('Okay', 'site-reviews')]; |
|
66 | + } |
|
67 | + $response = [ |
|
68 | + 'body' => $data['fields'], |
|
69 | + 'close' => $data['btn_close'], |
|
70 | + 'ok' => $data['btn_okay'], |
|
71 | + 'shortcode' => $shortcode, |
|
72 | + 'title' => $data['title'], |
|
73 | + ]; |
|
74 | + } |
|
75 | + wp_send_json_success($response); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return void |
|
80 | - */ |
|
81 | - public function routerFetchConsole() |
|
82 | - { |
|
83 | - glsr(AdminController::class)->routerFetchConsole(); |
|
84 | - wp_send_json_success([ |
|
85 | - 'console' => glsr(Console::class)->get(), |
|
86 | - 'notices' => glsr(Notice::class)->get(), |
|
87 | - ]); |
|
88 | - } |
|
78 | + /** |
|
79 | + * @return void |
|
80 | + */ |
|
81 | + public function routerFetchConsole() |
|
82 | + { |
|
83 | + glsr(AdminController::class)->routerFetchConsole(); |
|
84 | + wp_send_json_success([ |
|
85 | + 'console' => glsr(Console::class)->get(), |
|
86 | + 'notices' => glsr(Notice::class)->get(), |
|
87 | + ]); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @return void |
|
92 | - */ |
|
93 | - public function routerSearchPosts(array $request) |
|
94 | - { |
|
95 | - $results = glsr(Database::class)->searchPosts($request['search']); |
|
96 | - wp_send_json_success([ |
|
97 | - 'empty' => '<div>'.__('Nothing found.', 'site-reviews').'</div>', |
|
98 | - 'items' => $results, |
|
99 | - ]); |
|
100 | - } |
|
90 | + /** |
|
91 | + * @return void |
|
92 | + */ |
|
93 | + public function routerSearchPosts(array $request) |
|
94 | + { |
|
95 | + $results = glsr(Database::class)->searchPosts($request['search']); |
|
96 | + wp_send_json_success([ |
|
97 | + 'empty' => '<div>'.__('Nothing found.', 'site-reviews').'</div>', |
|
98 | + 'items' => $results, |
|
99 | + ]); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * @return void |
|
104 | - */ |
|
105 | - public function routerSearchTranslations(array $request) |
|
106 | - { |
|
107 | - if (empty($request['exclude'])) { |
|
108 | - $request['exclude'] = []; |
|
109 | - } |
|
110 | - $results = glsr(Translation::class) |
|
111 | - ->search($request['search']) |
|
112 | - ->exclude() |
|
113 | - ->exclude($request['exclude']) |
|
114 | - ->renderResults(); |
|
115 | - wp_send_json_success([ |
|
116 | - 'empty' => '<div>'.__('Nothing found.', 'site-reviews').'</div>', |
|
117 | - 'items' => $results, |
|
118 | - ]); |
|
119 | - } |
|
102 | + /** |
|
103 | + * @return void |
|
104 | + */ |
|
105 | + public function routerSearchTranslations(array $request) |
|
106 | + { |
|
107 | + if (empty($request['exclude'])) { |
|
108 | + $request['exclude'] = []; |
|
109 | + } |
|
110 | + $results = glsr(Translation::class) |
|
111 | + ->search($request['search']) |
|
112 | + ->exclude() |
|
113 | + ->exclude($request['exclude']) |
|
114 | + ->renderResults(); |
|
115 | + wp_send_json_success([ |
|
116 | + 'empty' => '<div>'.__('Nothing found.', 'site-reviews').'</div>', |
|
117 | + 'items' => $results, |
|
118 | + ]); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - public function routerSubmitReview(array $request) |
|
125 | - { |
|
126 | - $command = glsr(PublicController::class)->routerSubmitReview($request); |
|
127 | - $redirect = trim(strval(get_post_meta($command->post_id, 'redirect_to', true))); |
|
128 | - $redirect = apply_filters('site-reviews/review/redirect', $redirect, $command); |
|
129 | - $data = [ |
|
130 | - 'errors' => glsr()->sessionGet($command->form_id.'errors', false), |
|
131 | - 'message' => glsr()->sessionGet($command->form_id.'message', ''), |
|
132 | - 'recaptcha' => glsr()->sessionGet($command->form_id.'recaptcha', false), |
|
133 | - 'redirect' => $redirect, |
|
134 | - ]; |
|
135 | - if (false === $data['errors']) { |
|
136 | - glsr()->sessionClear(); |
|
137 | - wp_send_json_success($data); |
|
138 | - } |
|
139 | - wp_send_json_error($data); |
|
140 | - } |
|
121 | + /** |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + public function routerSubmitReview(array $request) |
|
125 | + { |
|
126 | + $command = glsr(PublicController::class)->routerSubmitReview($request); |
|
127 | + $redirect = trim(strval(get_post_meta($command->post_id, 'redirect_to', true))); |
|
128 | + $redirect = apply_filters('site-reviews/review/redirect', $redirect, $command); |
|
129 | + $data = [ |
|
130 | + 'errors' => glsr()->sessionGet($command->form_id.'errors', false), |
|
131 | + 'message' => glsr()->sessionGet($command->form_id.'message', ''), |
|
132 | + 'recaptcha' => glsr()->sessionGet($command->form_id.'recaptcha', false), |
|
133 | + 'redirect' => $redirect, |
|
134 | + ]; |
|
135 | + if (false === $data['errors']) { |
|
136 | + glsr()->sessionClear(); |
|
137 | + wp_send_json_success($data); |
|
138 | + } |
|
139 | + wp_send_json_error($data); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * @return void |
|
144 | - */ |
|
145 | - public function routerFetchPagedReviews(array $request) |
|
146 | - { |
|
147 | - $urlQuery = []; |
|
148 | - parse_str(parse_url(glsr_get($request, 'url'), PHP_URL_QUERY), $urlQuery); |
|
149 | - $args = [ |
|
150 | - 'paged' => glsr_get($urlQuery, glsr()->constant('PAGED_QUERY_VAR'), 1), |
|
151 | - 'pagedUrl' => home_url(parse_url(glsr_get($request, 'url'), PHP_URL_PATH)), |
|
152 | - 'pagination' => 'ajax', |
|
153 | - 'schema' => false, |
|
154 | - ]; |
|
155 | - $atts = (array) json_decode(glsr_get($request, 'atts')); |
|
156 | - $html = glsr(SiteReviews::class)->build(wp_parse_args($args, $atts)); |
|
157 | - return wp_send_json_success([ |
|
158 | - 'pagination' => $html->getPagination(), |
|
159 | - 'reviews' => $html->getReviews(), |
|
160 | - ]); |
|
161 | - } |
|
142 | + /** |
|
143 | + * @return void |
|
144 | + */ |
|
145 | + public function routerFetchPagedReviews(array $request) |
|
146 | + { |
|
147 | + $urlQuery = []; |
|
148 | + parse_str(parse_url(glsr_get($request, 'url'), PHP_URL_QUERY), $urlQuery); |
|
149 | + $args = [ |
|
150 | + 'paged' => glsr_get($urlQuery, glsr()->constant('PAGED_QUERY_VAR'), 1), |
|
151 | + 'pagedUrl' => home_url(parse_url(glsr_get($request, 'url'), PHP_URL_PATH)), |
|
152 | + 'pagination' => 'ajax', |
|
153 | + 'schema' => false, |
|
154 | + ]; |
|
155 | + $atts = (array) json_decode(glsr_get($request, 'atts')); |
|
156 | + $html = glsr(SiteReviews::class)->build(wp_parse_args($args, $atts)); |
|
157 | + return wp_send_json_success([ |
|
158 | + 'pagination' => $html->getPagination(), |
|
159 | + 'reviews' => $html->getReviews(), |
|
160 | + ]); |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public function routerTogglePinned(array $request) |
|
167 | - { |
|
168 | - $isPinned = $this->execute(new TogglePinned($request)); |
|
169 | - wp_send_json_success([ |
|
170 | - 'notices' => glsr(Notice::class)->get(), |
|
171 | - 'pinned' => $isPinned, |
|
172 | - ]); |
|
173 | - } |
|
163 | + /** |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public function routerTogglePinned(array $request) |
|
167 | + { |
|
168 | + $isPinned = $this->execute(new TogglePinned($request)); |
|
169 | + wp_send_json_success([ |
|
170 | + 'notices' => glsr(Notice::class)->get(), |
|
171 | + 'pinned' => $isPinned, |
|
172 | + ]); |
|
173 | + } |
|
174 | 174 | } |
@@ -8,112 +8,112 @@ |
||
8 | 8 | |
9 | 9 | class Notice |
10 | 10 | { |
11 | - /** |
|
12 | - * @param string $type |
|
13 | - * @param string|array|WP_Error $message |
|
14 | - * @return void |
|
15 | - */ |
|
16 | - public function add($type, $message, array $args = []) |
|
17 | - { |
|
18 | - if (empty(array_filter([$message, $type]))) { |
|
19 | - return; |
|
20 | - } |
|
21 | - $args['message'] = $message; |
|
22 | - $args['type'] = $type; |
|
23 | - add_settings_error(Application::ID, '', json_encode($this->normalize($args))); |
|
24 | - } |
|
11 | + /** |
|
12 | + * @param string $type |
|
13 | + * @param string|array|WP_Error $message |
|
14 | + * @return void |
|
15 | + */ |
|
16 | + public function add($type, $message, array $args = []) |
|
17 | + { |
|
18 | + if (empty(array_filter([$message, $type]))) { |
|
19 | + return; |
|
20 | + } |
|
21 | + $args['message'] = $message; |
|
22 | + $args['type'] = $type; |
|
23 | + add_settings_error(Application::ID, '', json_encode($this->normalize($args))); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @param string|array|WP_Error $message |
|
28 | - * @return void |
|
29 | - */ |
|
30 | - public function addError($message, array $args = []) |
|
31 | - { |
|
32 | - $this->add('error', $message, $args); |
|
33 | - } |
|
26 | + /** |
|
27 | + * @param string|array|WP_Error $message |
|
28 | + * @return void |
|
29 | + */ |
|
30 | + public function addError($message, array $args = []) |
|
31 | + { |
|
32 | + $this->add('error', $message, $args); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param string|array|WP_Error $message |
|
37 | - * @return void |
|
38 | - */ |
|
39 | - public function addSuccess($message, array $args = []) |
|
40 | - { |
|
41 | - $this->add('success', $message, $args); |
|
42 | - } |
|
35 | + /** |
|
36 | + * @param string|array|WP_Error $message |
|
37 | + * @return void |
|
38 | + */ |
|
39 | + public function addSuccess($message, array $args = []) |
|
40 | + { |
|
41 | + $this->add('success', $message, $args); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param string|array|WP_Error $message |
|
46 | - * @return void |
|
47 | - */ |
|
48 | - public function addWarning($message, array $args = []) |
|
49 | - { |
|
50 | - $this->add('warning', $message, $args); |
|
51 | - } |
|
44 | + /** |
|
45 | + * @param string|array|WP_Error $message |
|
46 | + * @return void |
|
47 | + */ |
|
48 | + public function addWarning($message, array $args = []) |
|
49 | + { |
|
50 | + $this->add('warning', $message, $args); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return static |
|
55 | - */ |
|
56 | - public function clear() |
|
57 | - { |
|
58 | - global $wp_settings_errors; |
|
59 | - $wp_settings_errors = []; |
|
60 | - delete_transient('settings_errors'); |
|
61 | - return $this; |
|
62 | - } |
|
53 | + /** |
|
54 | + * @return static |
|
55 | + */ |
|
56 | + public function clear() |
|
57 | + { |
|
58 | + global $wp_settings_errors; |
|
59 | + $wp_settings_errors = []; |
|
60 | + delete_transient('settings_errors'); |
|
61 | + return $this; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function get() |
|
68 | - { |
|
69 | - $notices = array_map('unserialize', |
|
70 | - array_unique(array_map('serialize', get_settings_errors(Application::ID))) |
|
71 | - ); |
|
72 | - $notices = array_reduce($notices, function ($carry, $notice) { |
|
73 | - return $carry.$this->buildNotice(json_decode($notice['message'], true)); |
|
74 | - }); |
|
75 | - return apply_filters('site-reviews/notices', $notices); |
|
76 | - } |
|
64 | + /** |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function get() |
|
68 | + { |
|
69 | + $notices = array_map('unserialize', |
|
70 | + array_unique(array_map('serialize', get_settings_errors(Application::ID))) |
|
71 | + ); |
|
72 | + $notices = array_reduce($notices, function ($carry, $notice) { |
|
73 | + return $carry.$this->buildNotice(json_decode($notice['message'], true)); |
|
74 | + }); |
|
75 | + return apply_filters('site-reviews/notices', $notices); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - protected function buildNotice(array $args) |
|
82 | - { |
|
83 | - $messages = array_reduce($args['messages'], function ($carry, $message) { |
|
84 | - return $carry.glsr(Builder::class)->p($message); |
|
85 | - }); |
|
86 | - $class = 'notice notice-'.$args['type']; |
|
87 | - if ($args['inline']) { |
|
88 | - $class.= ' inline'; |
|
89 | - } |
|
90 | - if ($args['dismissible']) { |
|
91 | - $class.= ' is-dismissible'; |
|
92 | - } |
|
93 | - return glsr(Builder::class)->div($messages, [ |
|
94 | - 'class' => $class, |
|
95 | - ]); |
|
96 | - } |
|
78 | + /** |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + protected function buildNotice(array $args) |
|
82 | + { |
|
83 | + $messages = array_reduce($args['messages'], function ($carry, $message) { |
|
84 | + return $carry.glsr(Builder::class)->p($message); |
|
85 | + }); |
|
86 | + $class = 'notice notice-'.$args['type']; |
|
87 | + if ($args['inline']) { |
|
88 | + $class.= ' inline'; |
|
89 | + } |
|
90 | + if ($args['dismissible']) { |
|
91 | + $class.= ' is-dismissible'; |
|
92 | + } |
|
93 | + return glsr(Builder::class)->div($messages, [ |
|
94 | + 'class' => $class, |
|
95 | + ]); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @return array |
|
100 | - */ |
|
101 | - protected function normalize(array $args) |
|
102 | - { |
|
103 | - $defaults = [ |
|
104 | - 'dismissible' => true, |
|
105 | - 'inline' => true, |
|
106 | - 'message' => '', |
|
107 | - 'type' => '', |
|
108 | - ]; |
|
109 | - $args = shortcode_atts($defaults, $args); |
|
110 | - if (!in_array($args['type'], ['error', 'warning', 'success'])) { |
|
111 | - $args['type'] = 'success'; |
|
112 | - } |
|
113 | - $args['messages'] = is_wp_error($args['message']) |
|
114 | - ? (array) $args['message']->get_error_message() |
|
115 | - : (array) $args['message']; |
|
116 | - unset($args['message']); |
|
117 | - return $args; |
|
118 | - } |
|
98 | + /** |
|
99 | + * @return array |
|
100 | + */ |
|
101 | + protected function normalize(array $args) |
|
102 | + { |
|
103 | + $defaults = [ |
|
104 | + 'dismissible' => true, |
|
105 | + 'inline' => true, |
|
106 | + 'message' => '', |
|
107 | + 'type' => '', |
|
108 | + ]; |
|
109 | + $args = shortcode_atts($defaults, $args); |
|
110 | + if (!in_array($args['type'], ['error', 'warning', 'success'])) { |
|
111 | + $args['type'] = 'success'; |
|
112 | + } |
|
113 | + $args['messages'] = is_wp_error($args['message']) |
|
114 | + ? (array) $args['message']->get_error_message() |
|
115 | + : (array) $args['message']; |
|
116 | + unset($args['message']); |
|
117 | + return $args; |
|
118 | + } |
|
119 | 119 | } |