@@ -8,97 +8,97 @@ |
||
8 | 8 | |
9 | 9 | class TaxonomyController |
10 | 10 | { |
11 | - /** |
|
12 | - * @return void |
|
13 | - * @action Application::TAXONOMY._add_form_fields |
|
14 | - * @action Application::TAXONOMY._edit_form |
|
15 | - */ |
|
16 | - public function disableParents() |
|
17 | - { |
|
18 | - global $wp_taxonomies; |
|
19 | - $wp_taxonomies[Application::TAXONOMY]->hierarchical = false; |
|
20 | - } |
|
11 | + /** |
|
12 | + * @return void |
|
13 | + * @action Application::TAXONOMY._add_form_fields |
|
14 | + * @action Application::TAXONOMY._edit_form |
|
15 | + */ |
|
16 | + public function disableParents() |
|
17 | + { |
|
18 | + global $wp_taxonomies; |
|
19 | + $wp_taxonomies[Application::TAXONOMY]->hierarchical = false; |
|
20 | + } |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return void |
|
24 | - * @action Application::TAXONOMY._term_edit_form_top |
|
25 | - * @action Application::TAXONOMY._term_new_form_tag |
|
26 | - */ |
|
27 | - public function enableParents() |
|
28 | - { |
|
29 | - global $wp_taxonomies; |
|
30 | - $wp_taxonomies[Application::TAXONOMY]->hierarchical = true; |
|
31 | - } |
|
22 | + /** |
|
23 | + * @return void |
|
24 | + * @action Application::TAXONOMY._term_edit_form_top |
|
25 | + * @action Application::TAXONOMY._term_new_form_tag |
|
26 | + */ |
|
27 | + public function enableParents() |
|
28 | + { |
|
29 | + global $wp_taxonomies; |
|
30 | + $wp_taxonomies[Application::TAXONOMY]->hierarchical = true; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @return void |
|
35 | - * @action restrict_manage_posts |
|
36 | - */ |
|
37 | - public function renderTaxonomyFilter() |
|
38 | - { |
|
39 | - if (!is_object_in_taxonomy(glsr_current_screen()->post_type, Application::TAXONOMY)) { |
|
40 | - return; |
|
41 | - } |
|
42 | - echo glsr(Builder::class)->label(__('Filter by category', 'site-reviews'), [ |
|
43 | - 'class' => 'screen-reader-text', |
|
44 | - 'for' => Application::TAXONOMY, |
|
45 | - ]); |
|
46 | - wp_dropdown_categories([ |
|
47 | - 'depth' => 3, |
|
48 | - 'hide_empty' => true, |
|
49 | - 'hide_if_empty' => true, |
|
50 | - 'hierarchical' => true, |
|
51 | - 'name' => Application::TAXONOMY, |
|
52 | - 'orderby' => 'name', |
|
53 | - 'selected' => $this->getSelected(), |
|
54 | - 'show_count' => false, |
|
55 | - 'show_option_all' => $this->getShowOptionAll(), |
|
56 | - 'taxonomy' => Application::TAXONOMY, |
|
57 | - 'value_field' => 'slug', |
|
58 | - ]); |
|
59 | - } |
|
33 | + /** |
|
34 | + * @return void |
|
35 | + * @action restrict_manage_posts |
|
36 | + */ |
|
37 | + public function renderTaxonomyFilter() |
|
38 | + { |
|
39 | + if (!is_object_in_taxonomy(glsr_current_screen()->post_type, Application::TAXONOMY)) { |
|
40 | + return; |
|
41 | + } |
|
42 | + echo glsr(Builder::class)->label(__('Filter by category', 'site-reviews'), [ |
|
43 | + 'class' => 'screen-reader-text', |
|
44 | + 'for' => Application::TAXONOMY, |
|
45 | + ]); |
|
46 | + wp_dropdown_categories([ |
|
47 | + 'depth' => 3, |
|
48 | + 'hide_empty' => true, |
|
49 | + 'hide_if_empty' => true, |
|
50 | + 'hierarchical' => true, |
|
51 | + 'name' => Application::TAXONOMY, |
|
52 | + 'orderby' => 'name', |
|
53 | + 'selected' => $this->getSelected(), |
|
54 | + 'show_count' => false, |
|
55 | + 'show_option_all' => $this->getShowOptionAll(), |
|
56 | + 'taxonomy' => Application::TAXONOMY, |
|
57 | + 'value_field' => 'slug', |
|
58 | + ]); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param int $postId |
|
63 | - * @param array $terms |
|
64 | - * @param array $newTTIds |
|
65 | - * @param string $taxonomy |
|
66 | - * @param bool $append |
|
67 | - * @param array $oldTTIds |
|
68 | - * @return void |
|
69 | - * @action set_object_terms |
|
70 | - */ |
|
71 | - public function restrictTermSelection($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds) |
|
72 | - { |
|
73 | - if (Application::TAXONOMY != $taxonomy || count($newTTIds) <= 1) { |
|
74 | - return; |
|
75 | - } |
|
76 | - $diff = array_diff($newTTIds, $oldTTIds); |
|
77 | - if (empty($newTerm = array_shift($diff))) { |
|
78 | - $newTerm = array_shift($newTTIds); |
|
79 | - } |
|
80 | - if ($newTerm) { |
|
81 | - wp_set_object_terms($postId, intval($newTerm), $taxonomy); |
|
82 | - } |
|
83 | - } |
|
61 | + /** |
|
62 | + * @param int $postId |
|
63 | + * @param array $terms |
|
64 | + * @param array $newTTIds |
|
65 | + * @param string $taxonomy |
|
66 | + * @param bool $append |
|
67 | + * @param array $oldTTIds |
|
68 | + * @return void |
|
69 | + * @action set_object_terms |
|
70 | + */ |
|
71 | + public function restrictTermSelection($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds) |
|
72 | + { |
|
73 | + if (Application::TAXONOMY != $taxonomy || count($newTTIds) <= 1) { |
|
74 | + return; |
|
75 | + } |
|
76 | + $diff = array_diff($newTTIds, $oldTTIds); |
|
77 | + if (empty($newTerm = array_shift($diff))) { |
|
78 | + $newTerm = array_shift($newTTIds); |
|
79 | + } |
|
80 | + if ($newTerm) { |
|
81 | + wp_set_object_terms($postId, intval($newTerm), $taxonomy); |
|
82 | + } |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - protected function getSelected() |
|
89 | - { |
|
90 | - global $wp_query; |
|
91 | - return Arr::get($wp_query->query, Application::TAXONOMY); |
|
92 | - } |
|
85 | + /** |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + protected function getSelected() |
|
89 | + { |
|
90 | + global $wp_query; |
|
91 | + return Arr::get($wp_query->query, Application::TAXONOMY); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return string |
|
96 | - */ |
|
97 | - protected function getShowOptionAll() |
|
98 | - { |
|
99 | - $taxonomy = get_taxonomy(Application::TAXONOMY); |
|
100 | - return $taxonomy |
|
101 | - ? ucfirst(strtolower($taxonomy->labels->all_items)) |
|
102 | - : ''; |
|
103 | - } |
|
94 | + /** |
|
95 | + * @return string |
|
96 | + */ |
|
97 | + protected function getShowOptionAll() |
|
98 | + { |
|
99 | + $taxonomy = get_taxonomy(Application::TAXONOMY); |
|
100 | + return $taxonomy |
|
101 | + ? ucfirst(strtolower($taxonomy->labels->all_items)) |
|
102 | + : ''; |
|
103 | + } |
|
104 | 104 | } |
@@ -13,139 +13,139 @@ |
||
13 | 13 | |
14 | 14 | class ReviewController extends Controller |
15 | 15 | { |
16 | - /** |
|
17 | - * @param int $postId |
|
18 | - * @param array $terms |
|
19 | - * @param array $newTTIds |
|
20 | - * @param string $taxonomy |
|
21 | - * @param bool $append |
|
22 | - * @param array $oldTTIds |
|
23 | - * @return void |
|
24 | - * @action set_object_terms |
|
25 | - */ |
|
26 | - public function onAfterChangeCategory($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds) |
|
27 | - { |
|
28 | - sort($newTTIds); |
|
29 | - sort($oldTTIds); |
|
30 | - if ($newTTIds === $oldTTIds || !$this->isReviewPostId($postId)) { |
|
31 | - return; |
|
32 | - } |
|
33 | - $review = glsr_get_review($postId); |
|
34 | - $ignoredIds = array_intersect($oldTTIds, $newTTIds); |
|
35 | - $decreasedIds = array_diff($oldTTIds, $ignoredIds); |
|
36 | - $increasedIds = array_diff($newTTIds, $ignoredIds); |
|
37 | - if ($review->term_ids = glsr(Database::class)->getTermIds($decreasedIds, 'term_taxonomy_id')) { |
|
38 | - glsr(CountsManager::class)->decreaseTermCounts($review); |
|
39 | - } |
|
40 | - if ($review->term_ids = glsr(Database::class)->getTermIds($increasedIds, 'term_taxonomy_id')) { |
|
41 | - glsr(CountsManager::class)->increaseTermCounts($review); |
|
42 | - } |
|
43 | - } |
|
16 | + /** |
|
17 | + * @param int $postId |
|
18 | + * @param array $terms |
|
19 | + * @param array $newTTIds |
|
20 | + * @param string $taxonomy |
|
21 | + * @param bool $append |
|
22 | + * @param array $oldTTIds |
|
23 | + * @return void |
|
24 | + * @action set_object_terms |
|
25 | + */ |
|
26 | + public function onAfterChangeCategory($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds) |
|
27 | + { |
|
28 | + sort($newTTIds); |
|
29 | + sort($oldTTIds); |
|
30 | + if ($newTTIds === $oldTTIds || !$this->isReviewPostId($postId)) { |
|
31 | + return; |
|
32 | + } |
|
33 | + $review = glsr_get_review($postId); |
|
34 | + $ignoredIds = array_intersect($oldTTIds, $newTTIds); |
|
35 | + $decreasedIds = array_diff($oldTTIds, $ignoredIds); |
|
36 | + $increasedIds = array_diff($newTTIds, $ignoredIds); |
|
37 | + if ($review->term_ids = glsr(Database::class)->getTermIds($decreasedIds, 'term_taxonomy_id')) { |
|
38 | + glsr(CountsManager::class)->decreaseTermCounts($review); |
|
39 | + } |
|
40 | + if ($review->term_ids = glsr(Database::class)->getTermIds($increasedIds, 'term_taxonomy_id')) { |
|
41 | + glsr(CountsManager::class)->increaseTermCounts($review); |
|
42 | + } |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $oldStatus |
|
47 | - * @param string $newStatus |
|
48 | - * @param WP_Post $post |
|
49 | - * @return void |
|
50 | - * @action transition_post_status |
|
51 | - */ |
|
52 | - public function onAfterChangeStatus($newStatus, $oldStatus, $post) |
|
53 | - { |
|
54 | - if (Application::POST_TYPE != Arr::get($post, 'post_type') || in_array($oldStatus, ['new', $newStatus])) { |
|
55 | - return; |
|
56 | - } |
|
57 | - $review = glsr_get_review($post); |
|
58 | - if ('publish' == $post->post_status) { |
|
59 | - glsr(CountsManager::class)->increase($review); |
|
60 | - } else { |
|
61 | - glsr(CountsManager::class)->decrease($review); |
|
62 | - } |
|
63 | - } |
|
45 | + /** |
|
46 | + * @param string $oldStatus |
|
47 | + * @param string $newStatus |
|
48 | + * @param WP_Post $post |
|
49 | + * @return void |
|
50 | + * @action transition_post_status |
|
51 | + */ |
|
52 | + public function onAfterChangeStatus($newStatus, $oldStatus, $post) |
|
53 | + { |
|
54 | + if (Application::POST_TYPE != Arr::get($post, 'post_type') || in_array($oldStatus, ['new', $newStatus])) { |
|
55 | + return; |
|
56 | + } |
|
57 | + $review = glsr_get_review($post); |
|
58 | + if ('publish' == $post->post_status) { |
|
59 | + glsr(CountsManager::class)->increase($review); |
|
60 | + } else { |
|
61 | + glsr(CountsManager::class)->decrease($review); |
|
62 | + } |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return void |
|
67 | - * @action site-reviews/review/created |
|
68 | - */ |
|
69 | - public function onAfterCreate(Review $review) |
|
70 | - { |
|
71 | - if ('publish' !== $review->status) { |
|
72 | - return; |
|
73 | - } |
|
74 | - glsr(CountsManager::class)->increaseCounts($review); |
|
75 | - glsr(CountsManager::class)->increasePostCounts($review); |
|
76 | - } |
|
65 | + /** |
|
66 | + * @return void |
|
67 | + * @action site-reviews/review/created |
|
68 | + */ |
|
69 | + public function onAfterCreate(Review $review) |
|
70 | + { |
|
71 | + if ('publish' !== $review->status) { |
|
72 | + return; |
|
73 | + } |
|
74 | + glsr(CountsManager::class)->increaseCounts($review); |
|
75 | + glsr(CountsManager::class)->increasePostCounts($review); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param int $postId |
|
80 | - * @return void |
|
81 | - * @action before_delete_post |
|
82 | - */ |
|
83 | - public function onBeforeDelete($postId) |
|
84 | - { |
|
85 | - if (!$this->isReviewPostId($postId)) { |
|
86 | - return; |
|
87 | - } |
|
88 | - $review = glsr_get_review($postId); |
|
89 | - if ('trash' !== $review->status) { // do not run for trashed posts |
|
90 | - glsr(CountsManager::class)->decrease($review); |
|
91 | - } |
|
92 | - } |
|
78 | + /** |
|
79 | + * @param int $postId |
|
80 | + * @return void |
|
81 | + * @action before_delete_post |
|
82 | + */ |
|
83 | + public function onBeforeDelete($postId) |
|
84 | + { |
|
85 | + if (!$this->isReviewPostId($postId)) { |
|
86 | + return; |
|
87 | + } |
|
88 | + $review = glsr_get_review($postId); |
|
89 | + if ('trash' !== $review->status) { // do not run for trashed posts |
|
90 | + glsr(CountsManager::class)->decrease($review); |
|
91 | + } |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @param int $metaId |
|
96 | - * @param int $postId |
|
97 | - * @param string $metaKey |
|
98 | - * @param mixed $metaValue |
|
99 | - * @return void |
|
100 | - * @action update_postmeta |
|
101 | - */ |
|
102 | - public function onBeforeUpdate($metaId, $postId, $metaKey, $metaValue) |
|
103 | - { |
|
104 | - if (!$this->isReviewPostId($postId)) { |
|
105 | - return; |
|
106 | - } |
|
107 | - $metaKey = Str::removePrefix('_', $metaKey); |
|
108 | - if (!in_array($metaKey, ['assigned_to', 'rating', 'review_type'])) { |
|
109 | - return; |
|
110 | - } |
|
111 | - $review = glsr_get_review($postId); |
|
112 | - if ($review->$metaKey == $metaValue) { |
|
113 | - return; |
|
114 | - } |
|
115 | - $method = Helper::buildMethodName($metaKey, 'onBeforeChange'); |
|
116 | - call_user_func([$this, $method], $review, $metaValue); |
|
117 | - } |
|
94 | + /** |
|
95 | + * @param int $metaId |
|
96 | + * @param int $postId |
|
97 | + * @param string $metaKey |
|
98 | + * @param mixed $metaValue |
|
99 | + * @return void |
|
100 | + * @action update_postmeta |
|
101 | + */ |
|
102 | + public function onBeforeUpdate($metaId, $postId, $metaKey, $metaValue) |
|
103 | + { |
|
104 | + if (!$this->isReviewPostId($postId)) { |
|
105 | + return; |
|
106 | + } |
|
107 | + $metaKey = Str::removePrefix('_', $metaKey); |
|
108 | + if (!in_array($metaKey, ['assigned_to', 'rating', 'review_type'])) { |
|
109 | + return; |
|
110 | + } |
|
111 | + $review = glsr_get_review($postId); |
|
112 | + if ($review->$metaKey == $metaValue) { |
|
113 | + return; |
|
114 | + } |
|
115 | + $method = Helper::buildMethodName($metaKey, 'onBeforeChange'); |
|
116 | + call_user_func([$this, $method], $review, $metaValue); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * @param string|int $assignedTo |
|
121 | - * @return void |
|
122 | - */ |
|
123 | - public function onBeforeChangeAssignedTo(Review $review, $assignedTo) |
|
124 | - { |
|
125 | - glsr(CountsManager::class)->decreasePostCounts($review); |
|
126 | - $review->assigned_to = $assignedTo; |
|
127 | - glsr(CountsManager::class)->increasePostCounts($review); |
|
128 | - } |
|
119 | + /** |
|
120 | + * @param string|int $assignedTo |
|
121 | + * @return void |
|
122 | + */ |
|
123 | + public function onBeforeChangeAssignedTo(Review $review, $assignedTo) |
|
124 | + { |
|
125 | + glsr(CountsManager::class)->decreasePostCounts($review); |
|
126 | + $review->assigned_to = $assignedTo; |
|
127 | + glsr(CountsManager::class)->increasePostCounts($review); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @param string|int $rating |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - public function onBeforeChangeRating(Review $review, $rating) |
|
135 | - { |
|
136 | - glsr(CountsManager::class)->decrease($review); |
|
137 | - $review->rating = $rating; |
|
138 | - glsr(CountsManager::class)->increase($review); |
|
139 | - } |
|
130 | + /** |
|
131 | + * @param string|int $rating |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + public function onBeforeChangeRating(Review $review, $rating) |
|
135 | + { |
|
136 | + glsr(CountsManager::class)->decrease($review); |
|
137 | + $review->rating = $rating; |
|
138 | + glsr(CountsManager::class)->increase($review); |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * @param string $reviewType |
|
143 | - * @return void |
|
144 | - */ |
|
145 | - public function onBeforeChangeReviewType(Review $review, $reviewType) |
|
146 | - { |
|
147 | - glsr(CountsManager::class)->decrease($review); |
|
148 | - $review->review_type = $reviewType; |
|
149 | - glsr(CountsManager::class)->increase($review); |
|
150 | - } |
|
141 | + /** |
|
142 | + * @param string $reviewType |
|
143 | + * @return void |
|
144 | + */ |
|
145 | + public function onBeforeChangeReviewType(Review $review, $reviewType) |
|
146 | + { |
|
147 | + glsr(CountsManager::class)->decrease($review); |
|
148 | + $review->review_type = $reviewType; |
|
149 | + glsr(CountsManager::class)->increase($review); |
|
150 | + } |
|
151 | 151 | } |
@@ -16,222 +16,222 @@ |
||
16 | 16 | |
17 | 17 | class AdminController extends Controller |
18 | 18 | { |
19 | - /** |
|
20 | - * @return void |
|
21 | - * @action admin_enqueue_scripts |
|
22 | - */ |
|
23 | - public function enqueueAssets() |
|
24 | - { |
|
25 | - $command = new EnqueueAdminAssets([ |
|
26 | - 'pointers' => [[ |
|
27 | - 'content' => __('You can pin exceptional reviews so that they are always shown first.', 'site-reviews'), |
|
28 | - 'id' => 'glsr-pointer-pinned', |
|
29 | - 'position' => [ |
|
30 | - 'edge' => 'right', |
|
31 | - 'align' => 'middle', |
|
32 | - ], |
|
33 | - 'screen' => Application::POST_TYPE, |
|
34 | - 'target' => '#misc-pub-pinned', |
|
35 | - 'title' => __('Pin Your Reviews', 'site-reviews'), |
|
36 | - ]], |
|
37 | - ]); |
|
38 | - $this->execute($command); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @return array |
|
43 | - * @filter plugin_action_links_site-reviews/site-reviews.php |
|
44 | - */ |
|
45 | - public function filterActionLinks(array $links) |
|
46 | - { |
|
47 | - $links['documentation'] = glsr(Builder::class)->a(__('Help', 'site-reviews'), [ |
|
48 | - 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=documentation'), |
|
49 | - ]); |
|
50 | - $links['settings'] = glsr(Builder::class)->a(__('Settings', 'site-reviews'), [ |
|
51 | - 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=settings'), |
|
52 | - ]); |
|
53 | - return $links; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @param array $capabilities |
|
58 | - * @param string $capability |
|
59 | - * @return array |
|
60 | - * @filter map_meta_cap |
|
61 | - */ |
|
62 | - public function filterCreateCapability($capabilities, $capability) |
|
63 | - { |
|
64 | - if ($capability == 'create_'.Application::POST_TYPE) { |
|
65 | - $capabilities[] = 'do_not_allow'; |
|
66 | - } |
|
67 | - return $capabilities; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param array $items |
|
72 | - * @return array |
|
73 | - * @filter dashboard_glance_items |
|
74 | - */ |
|
75 | - public function filterDashboardGlanceItems($items) |
|
76 | - { |
|
77 | - $postCount = wp_count_posts(Application::POST_TYPE); |
|
78 | - if (empty($postCount->publish)) { |
|
79 | - return $items; |
|
80 | - } |
|
81 | - $text = _n('%s Review', '%s Reviews', $postCount->publish, 'site-reviews'); |
|
82 | - $text = sprintf($text, number_format_i18n($postCount->publish)); |
|
83 | - $items = Arr::consolidateArray($items); |
|
84 | - $items[] = current_user_can(get_post_type_object(Application::POST_TYPE)->cap->edit_posts) |
|
85 | - ? glsr(Builder::class)->a($text, [ |
|
86 | - 'class' => 'glsr-review-count', |
|
87 | - 'href' => 'edit.php?post_type='.Application::POST_TYPE, |
|
88 | - ]) |
|
89 | - : glsr(Builder::class)->span($text, [ |
|
90 | - 'class' => 'glsr-review-count', |
|
91 | - ]); |
|
92 | - return $items; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param array $plugins |
|
97 | - * @return array |
|
98 | - * @filter mce_external_plugins |
|
99 | - */ |
|
100 | - public function filterTinymcePlugins($plugins) |
|
101 | - { |
|
102 | - if (current_user_can('edit_posts') || current_user_can('edit_pages')) { |
|
103 | - $plugins = Arr::consolidateArray($plugins); |
|
104 | - $plugins['glsr_shortcode'] = glsr()->url('assets/scripts/mce-plugin.js'); |
|
105 | - } |
|
106 | - return $plugins; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return void |
|
111 | - * @action admin_init |
|
112 | - */ |
|
113 | - public function registerTinymcePopups() |
|
114 | - { |
|
115 | - $command = new RegisterTinymcePopups([ |
|
116 | - 'site_reviews' => esc_html__('Recent Reviews', 'site-reviews'), |
|
117 | - 'site_reviews_form' => esc_html__('Submit a Review', 'site-reviews'), |
|
118 | - 'site_reviews_summary' => esc_html__('Summary of Reviews', 'site-reviews'), |
|
119 | - ]); |
|
120 | - $this->execute($command); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * @param string $editorId |
|
125 | - * @return void|null |
|
126 | - * @action media_buttons |
|
127 | - */ |
|
128 | - public function renderTinymceButton($editorId) |
|
129 | - { |
|
130 | - $allowedEditors = apply_filters('site-reviews/tinymce/editor-ids', ['content'], $editorId); |
|
131 | - if ('post' != glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) { |
|
132 | - return; |
|
133 | - } |
|
134 | - $shortcodes = []; |
|
135 | - foreach (glsr()->mceShortcodes as $shortcode => $values) { |
|
136 | - $shortcodes[$shortcode] = $values; |
|
137 | - } |
|
138 | - if (empty($shortcodes)) { |
|
139 | - return; |
|
140 | - } |
|
141 | - glsr()->render('partials/editor/tinymce', [ |
|
142 | - 'shortcodes' => $shortcodes, |
|
143 | - ]); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - public function routerClearConsole() |
|
150 | - { |
|
151 | - glsr(Console::class)->clear(); |
|
152 | - glsr(Notice::class)->addSuccess(__('Console cleared.', 'site-reviews')); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * @return void |
|
157 | - */ |
|
158 | - public function routerFetchConsole() |
|
159 | - { |
|
160 | - glsr(Notice::class)->addSuccess(__('Console reloaded.', 'site-reviews')); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @param bool $showNotice |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - public function routerCountReviews($showNotice = true) |
|
168 | - { |
|
169 | - glsr(CountsManager::class)->countAll(); |
|
170 | - glsr(OptionManager::class)->set('last_review_count', current_time('timestamp')); |
|
171 | - if ($showNotice) { |
|
172 | - glsr(Notice::class)->clear()->addSuccess(__('Recalculated rating counts.', 'site-reviews')); |
|
173 | - } |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * @return void |
|
178 | - */ |
|
179 | - public function routerDownloadConsole() |
|
180 | - { |
|
181 | - $this->download(Application::ID.'-console.txt', glsr(Console::class)->get()); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * @return void |
|
186 | - */ |
|
187 | - public function routerDownloadSystemInfo() |
|
188 | - { |
|
189 | - $this->download(Application::ID.'-system-info.txt', glsr(System::class)->get()); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @return void |
|
194 | - */ |
|
195 | - public function routerExportSettings() |
|
196 | - { |
|
197 | - $this->download(Application::ID.'-settings.json', glsr(OptionManager::class)->json()); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - public function routerImportSettings() |
|
204 | - { |
|
205 | - $file = $_FILES['import-file']; |
|
206 | - if (UPLOAD_ERR_OK !== $file['error']) { |
|
207 | - return glsr(Notice::class)->addError($this->getUploadError($file['error'])); |
|
208 | - } |
|
209 | - if ('application/json' !== $file['type'] || !Str::endsWith('.json', $file['name'])) { |
|
210 | - return glsr(Notice::class)->addError(__('Please use a valid Site Reviews settings file.', 'site-reviews')); |
|
211 | - } |
|
212 | - $settings = json_decode(file_get_contents($file['tmp_name']), true); |
|
213 | - if (empty($settings)) { |
|
214 | - return glsr(Notice::class)->addWarning(__('There were no settings found to import.', 'site-reviews')); |
|
215 | - } |
|
216 | - glsr(OptionManager::class)->set(glsr(OptionManager::class)->normalize($settings)); |
|
217 | - glsr(Notice::class)->addSuccess(__('Settings imported.', 'site-reviews')); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @param int $errorCode |
|
222 | - * @return string |
|
223 | - */ |
|
224 | - protected function getUploadError($errorCode) |
|
225 | - { |
|
226 | - $errors = [ |
|
227 | - UPLOAD_ERR_INI_SIZE => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews'), |
|
228 | - UPLOAD_ERR_FORM_SIZE => __('The uploaded file is too big.', 'site-reviews'), |
|
229 | - UPLOAD_ERR_PARTIAL => __('The uploaded file was only partially uploaded.', 'site-reviews'), |
|
230 | - UPLOAD_ERR_NO_FILE => __('No file was uploaded.', 'site-reviews'), |
|
231 | - UPLOAD_ERR_NO_TMP_DIR => __('Missing a temporary folder.', 'site-reviews'), |
|
232 | - UPLOAD_ERR_CANT_WRITE => __('Failed to write file to disk.', 'site-reviews'), |
|
233 | - UPLOAD_ERR_EXTENSION => __('A PHP extension stopped the file upload.', 'site-reviews'), |
|
234 | - ]; |
|
235 | - return Arr::get($errors, $errorCode, __('Unknown upload error.', 'site-reviews')); |
|
236 | - } |
|
19 | + /** |
|
20 | + * @return void |
|
21 | + * @action admin_enqueue_scripts |
|
22 | + */ |
|
23 | + public function enqueueAssets() |
|
24 | + { |
|
25 | + $command = new EnqueueAdminAssets([ |
|
26 | + 'pointers' => [[ |
|
27 | + 'content' => __('You can pin exceptional reviews so that they are always shown first.', 'site-reviews'), |
|
28 | + 'id' => 'glsr-pointer-pinned', |
|
29 | + 'position' => [ |
|
30 | + 'edge' => 'right', |
|
31 | + 'align' => 'middle', |
|
32 | + ], |
|
33 | + 'screen' => Application::POST_TYPE, |
|
34 | + 'target' => '#misc-pub-pinned', |
|
35 | + 'title' => __('Pin Your Reviews', 'site-reviews'), |
|
36 | + ]], |
|
37 | + ]); |
|
38 | + $this->execute($command); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @return array |
|
43 | + * @filter plugin_action_links_site-reviews/site-reviews.php |
|
44 | + */ |
|
45 | + public function filterActionLinks(array $links) |
|
46 | + { |
|
47 | + $links['documentation'] = glsr(Builder::class)->a(__('Help', 'site-reviews'), [ |
|
48 | + 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=documentation'), |
|
49 | + ]); |
|
50 | + $links['settings'] = glsr(Builder::class)->a(__('Settings', 'site-reviews'), [ |
|
51 | + 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=settings'), |
|
52 | + ]); |
|
53 | + return $links; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @param array $capabilities |
|
58 | + * @param string $capability |
|
59 | + * @return array |
|
60 | + * @filter map_meta_cap |
|
61 | + */ |
|
62 | + public function filterCreateCapability($capabilities, $capability) |
|
63 | + { |
|
64 | + if ($capability == 'create_'.Application::POST_TYPE) { |
|
65 | + $capabilities[] = 'do_not_allow'; |
|
66 | + } |
|
67 | + return $capabilities; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param array $items |
|
72 | + * @return array |
|
73 | + * @filter dashboard_glance_items |
|
74 | + */ |
|
75 | + public function filterDashboardGlanceItems($items) |
|
76 | + { |
|
77 | + $postCount = wp_count_posts(Application::POST_TYPE); |
|
78 | + if (empty($postCount->publish)) { |
|
79 | + return $items; |
|
80 | + } |
|
81 | + $text = _n('%s Review', '%s Reviews', $postCount->publish, 'site-reviews'); |
|
82 | + $text = sprintf($text, number_format_i18n($postCount->publish)); |
|
83 | + $items = Arr::consolidateArray($items); |
|
84 | + $items[] = current_user_can(get_post_type_object(Application::POST_TYPE)->cap->edit_posts) |
|
85 | + ? glsr(Builder::class)->a($text, [ |
|
86 | + 'class' => 'glsr-review-count', |
|
87 | + 'href' => 'edit.php?post_type='.Application::POST_TYPE, |
|
88 | + ]) |
|
89 | + : glsr(Builder::class)->span($text, [ |
|
90 | + 'class' => 'glsr-review-count', |
|
91 | + ]); |
|
92 | + return $items; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param array $plugins |
|
97 | + * @return array |
|
98 | + * @filter mce_external_plugins |
|
99 | + */ |
|
100 | + public function filterTinymcePlugins($plugins) |
|
101 | + { |
|
102 | + if (current_user_can('edit_posts') || current_user_can('edit_pages')) { |
|
103 | + $plugins = Arr::consolidateArray($plugins); |
|
104 | + $plugins['glsr_shortcode'] = glsr()->url('assets/scripts/mce-plugin.js'); |
|
105 | + } |
|
106 | + return $plugins; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return void |
|
111 | + * @action admin_init |
|
112 | + */ |
|
113 | + public function registerTinymcePopups() |
|
114 | + { |
|
115 | + $command = new RegisterTinymcePopups([ |
|
116 | + 'site_reviews' => esc_html__('Recent Reviews', 'site-reviews'), |
|
117 | + 'site_reviews_form' => esc_html__('Submit a Review', 'site-reviews'), |
|
118 | + 'site_reviews_summary' => esc_html__('Summary of Reviews', 'site-reviews'), |
|
119 | + ]); |
|
120 | + $this->execute($command); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * @param string $editorId |
|
125 | + * @return void|null |
|
126 | + * @action media_buttons |
|
127 | + */ |
|
128 | + public function renderTinymceButton($editorId) |
|
129 | + { |
|
130 | + $allowedEditors = apply_filters('site-reviews/tinymce/editor-ids', ['content'], $editorId); |
|
131 | + if ('post' != glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) { |
|
132 | + return; |
|
133 | + } |
|
134 | + $shortcodes = []; |
|
135 | + foreach (glsr()->mceShortcodes as $shortcode => $values) { |
|
136 | + $shortcodes[$shortcode] = $values; |
|
137 | + } |
|
138 | + if (empty($shortcodes)) { |
|
139 | + return; |
|
140 | + } |
|
141 | + glsr()->render('partials/editor/tinymce', [ |
|
142 | + 'shortcodes' => $shortcodes, |
|
143 | + ]); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + public function routerClearConsole() |
|
150 | + { |
|
151 | + glsr(Console::class)->clear(); |
|
152 | + glsr(Notice::class)->addSuccess(__('Console cleared.', 'site-reviews')); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * @return void |
|
157 | + */ |
|
158 | + public function routerFetchConsole() |
|
159 | + { |
|
160 | + glsr(Notice::class)->addSuccess(__('Console reloaded.', 'site-reviews')); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @param bool $showNotice |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + public function routerCountReviews($showNotice = true) |
|
168 | + { |
|
169 | + glsr(CountsManager::class)->countAll(); |
|
170 | + glsr(OptionManager::class)->set('last_review_count', current_time('timestamp')); |
|
171 | + if ($showNotice) { |
|
172 | + glsr(Notice::class)->clear()->addSuccess(__('Recalculated rating counts.', 'site-reviews')); |
|
173 | + } |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * @return void |
|
178 | + */ |
|
179 | + public function routerDownloadConsole() |
|
180 | + { |
|
181 | + $this->download(Application::ID.'-console.txt', glsr(Console::class)->get()); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * @return void |
|
186 | + */ |
|
187 | + public function routerDownloadSystemInfo() |
|
188 | + { |
|
189 | + $this->download(Application::ID.'-system-info.txt', glsr(System::class)->get()); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @return void |
|
194 | + */ |
|
195 | + public function routerExportSettings() |
|
196 | + { |
|
197 | + $this->download(Application::ID.'-settings.json', glsr(OptionManager::class)->json()); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + public function routerImportSettings() |
|
204 | + { |
|
205 | + $file = $_FILES['import-file']; |
|
206 | + if (UPLOAD_ERR_OK !== $file['error']) { |
|
207 | + return glsr(Notice::class)->addError($this->getUploadError($file['error'])); |
|
208 | + } |
|
209 | + if ('application/json' !== $file['type'] || !Str::endsWith('.json', $file['name'])) { |
|
210 | + return glsr(Notice::class)->addError(__('Please use a valid Site Reviews settings file.', 'site-reviews')); |
|
211 | + } |
|
212 | + $settings = json_decode(file_get_contents($file['tmp_name']), true); |
|
213 | + if (empty($settings)) { |
|
214 | + return glsr(Notice::class)->addWarning(__('There were no settings found to import.', 'site-reviews')); |
|
215 | + } |
|
216 | + glsr(OptionManager::class)->set(glsr(OptionManager::class)->normalize($settings)); |
|
217 | + glsr(Notice::class)->addSuccess(__('Settings imported.', 'site-reviews')); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @param int $errorCode |
|
222 | + * @return string |
|
223 | + */ |
|
224 | + protected function getUploadError($errorCode) |
|
225 | + { |
|
226 | + $errors = [ |
|
227 | + UPLOAD_ERR_INI_SIZE => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews'), |
|
228 | + UPLOAD_ERR_FORM_SIZE => __('The uploaded file is too big.', 'site-reviews'), |
|
229 | + UPLOAD_ERR_PARTIAL => __('The uploaded file was only partially uploaded.', 'site-reviews'), |
|
230 | + UPLOAD_ERR_NO_FILE => __('No file was uploaded.', 'site-reviews'), |
|
231 | + UPLOAD_ERR_NO_TMP_DIR => __('Missing a temporary folder.', 'site-reviews'), |
|
232 | + UPLOAD_ERR_CANT_WRITE => __('Failed to write file to disk.', 'site-reviews'), |
|
233 | + UPLOAD_ERR_EXTENSION => __('A PHP extension stopped the file upload.', 'site-reviews'), |
|
234 | + ]; |
|
235 | + return Arr::get($errors, $errorCode, __('Unknown upload error.', 'site-reviews')); |
|
236 | + } |
|
237 | 237 | } |
@@ -8,103 +8,103 @@ |
||
8 | 8 | |
9 | 9 | class ReviewLimits |
10 | 10 | { |
11 | - protected $request; |
|
11 | + protected $request; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @return array |
|
15 | - * @filter site-reviews/get/reviews/query |
|
16 | - */ |
|
17 | - public function filterReviewsQuery(array $parameters, array $args) |
|
18 | - { |
|
19 | - if ($authorId = get_current_user_id()) { |
|
20 | - $parameters['author'] = $authorId; |
|
21 | - } |
|
22 | - $parameters['post_status'] = ['pending', 'publish']; |
|
23 | - return apply_filters('site-reviews/reviews-limits/query', $parameters, $args); |
|
24 | - } |
|
13 | + /** |
|
14 | + * @return array |
|
15 | + * @filter site-reviews/get/reviews/query |
|
16 | + */ |
|
17 | + public function filterReviewsQuery(array $parameters, array $args) |
|
18 | + { |
|
19 | + if ($authorId = get_current_user_id()) { |
|
20 | + $parameters['author'] = $authorId; |
|
21 | + } |
|
22 | + $parameters['post_status'] = ['pending', 'publish']; |
|
23 | + return apply_filters('site-reviews/reviews-limits/query', $parameters, $args); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @return bool |
|
28 | - */ |
|
29 | - public function hasReachedLimit(array $request = []) |
|
30 | - { |
|
31 | - $this->request = $request; |
|
32 | - $method = Helper::buildMethodName( |
|
33 | - glsr(OptionManager::class)->get('settings.submissions.limit'), 'validateBy' |
|
34 | - ); |
|
35 | - return method_exists($this, $method) |
|
36 | - ? !call_user_func([$this, $method]) |
|
37 | - : false; |
|
38 | - } |
|
26 | + /** |
|
27 | + * @return bool |
|
28 | + */ |
|
29 | + public function hasReachedLimit(array $request = []) |
|
30 | + { |
|
31 | + $this->request = $request; |
|
32 | + $method = Helper::buildMethodName( |
|
33 | + glsr(OptionManager::class)->get('settings.submissions.limit'), 'validateBy' |
|
34 | + ); |
|
35 | + return method_exists($this, $method) |
|
36 | + ? !call_user_func([$this, $method]) |
|
37 | + : false; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @param string $value |
|
42 | - * @param string $whitelist |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function isWhitelisted($value, $whitelist) |
|
46 | - { |
|
47 | - if (empty($whitelist)) { |
|
48 | - return false; |
|
49 | - } |
|
50 | - return in_array($value, array_filter(explode("\n", $whitelist), 'trim')); |
|
51 | - } |
|
40 | + /** |
|
41 | + * @param string $value |
|
42 | + * @param string $whitelist |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function isWhitelisted($value, $whitelist) |
|
46 | + { |
|
47 | + if (empty($whitelist)) { |
|
48 | + return false; |
|
49 | + } |
|
50 | + return in_array($value, array_filter(explode("\n", $whitelist), 'trim')); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @param string $whitelistName |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - protected function getWhitelist($whitelistName) |
|
58 | - { |
|
59 | - return glsr(OptionManager::class)->get('settings.submissions.limit_whitelist.'.$whitelistName); |
|
60 | - } |
|
53 | + /** |
|
54 | + * @param string $whitelistName |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + protected function getWhitelist($whitelistName) |
|
58 | + { |
|
59 | + return glsr(OptionManager::class)->get('settings.submissions.limit_whitelist.'.$whitelistName); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return bool |
|
64 | - */ |
|
65 | - protected function validate($key, $value, $addMetaQuery = true) |
|
66 | - { |
|
67 | - if ($this->isWhitelisted($value, $this->getWhitelist($key))) { |
|
68 | - return true; |
|
69 | - } |
|
70 | - add_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5, 2); |
|
71 | - $args = ['assigned_to' => Arr::get($this->request, 'assign_to')]; |
|
72 | - if ($addMetaQuery) { |
|
73 | - $args[$key] = $value; |
|
74 | - } |
|
75 | - $reviews = glsr_get_reviews($args); |
|
76 | - remove_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5); |
|
77 | - return 0 === count($reviews); |
|
78 | - } |
|
62 | + /** |
|
63 | + * @return bool |
|
64 | + */ |
|
65 | + protected function validate($key, $value, $addMetaQuery = true) |
|
66 | + { |
|
67 | + if ($this->isWhitelisted($value, $this->getWhitelist($key))) { |
|
68 | + return true; |
|
69 | + } |
|
70 | + add_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5, 2); |
|
71 | + $args = ['assigned_to' => Arr::get($this->request, 'assign_to')]; |
|
72 | + if ($addMetaQuery) { |
|
73 | + $args[$key] = $value; |
|
74 | + } |
|
75 | + $reviews = glsr_get_reviews($args); |
|
76 | + remove_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5); |
|
77 | + return 0 === count($reviews); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @return bool |
|
82 | - */ |
|
83 | - protected function validateByEmail() |
|
84 | - { |
|
85 | - glsr_log()->debug('Email is: '.Arr::get($this->request, 'email')); |
|
86 | - return $this->validate('email', Arr::get($this->request, 'email')); |
|
87 | - } |
|
80 | + /** |
|
81 | + * @return bool |
|
82 | + */ |
|
83 | + protected function validateByEmail() |
|
84 | + { |
|
85 | + glsr_log()->debug('Email is: '.Arr::get($this->request, 'email')); |
|
86 | + return $this->validate('email', Arr::get($this->request, 'email')); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * @return bool |
|
91 | - */ |
|
92 | - protected function validateByIpAddress() |
|
93 | - { |
|
94 | - glsr_log()->debug('IP Address is: '.Arr::get($this->request, 'ip_address')); |
|
95 | - return $this->validate('ip_address', Arr::get($this->request, 'ip_address')); |
|
96 | - } |
|
89 | + /** |
|
90 | + * @return bool |
|
91 | + */ |
|
92 | + protected function validateByIpAddress() |
|
93 | + { |
|
94 | + glsr_log()->debug('IP Address is: '.Arr::get($this->request, 'ip_address')); |
|
95 | + return $this->validate('ip_address', Arr::get($this->request, 'ip_address')); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @return bool |
|
100 | - */ |
|
101 | - protected function validateByUsername() |
|
102 | - { |
|
103 | - $user = wp_get_current_user(); |
|
104 | - if (!$user->exists()) { |
|
105 | - return true; |
|
106 | - } |
|
107 | - glsr_log()->debug('Username is: '.$user->user_login); |
|
108 | - return $this->validate('username', $user->user_login, false); |
|
109 | - } |
|
98 | + /** |
|
99 | + * @return bool |
|
100 | + */ |
|
101 | + protected function validateByUsername() |
|
102 | + { |
|
103 | + $user = wp_get_current_user(); |
|
104 | + if (!$user->exists()) { |
|
105 | + return true; |
|
106 | + } |
|
107 | + glsr_log()->debug('Username is: '.$user->user_login); |
|
108 | + return $this->validate('username', $user->user_login, false); |
|
109 | + } |
|
110 | 110 | } |
@@ -10,143 +10,143 @@ |
||
10 | 10 | |
11 | 11 | class Rebusify |
12 | 12 | { |
13 | - const API_URL = 'https://www.rebusify.com/api/rbs/'; |
|
14 | - const WEB_URL = 'https://rebusify.com/plans?ref=105'; |
|
13 | + const API_URL = 'https://www.rebusify.com/api/rbs/'; |
|
14 | + const WEB_URL = 'https://rebusify.com/plans?ref=105'; |
|
15 | 15 | |
16 | - public $message; |
|
17 | - public $response; |
|
18 | - public $success; |
|
16 | + public $message; |
|
17 | + public $response; |
|
18 | + public $success; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @return mixed |
|
22 | - */ |
|
23 | - public function __get($key) |
|
24 | - { |
|
25 | - return property_exists($this, $key) |
|
26 | - ? $this->$key |
|
27 | - : Arr::get($this->response, $key, null); |
|
28 | - } |
|
20 | + /** |
|
21 | + * @return mixed |
|
22 | + */ |
|
23 | + public function __get($key) |
|
24 | + { |
|
25 | + return property_exists($this, $key) |
|
26 | + ? $this->$key |
|
27 | + : Arr::get($this->response, $key, null); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return self |
|
32 | - */ |
|
33 | - public function activateKey($apiKey = '', $email = '') |
|
34 | - { |
|
35 | - $this->send('api_key_activation.php', [ |
|
36 | - 'body' => [ |
|
37 | - 'apikey' => $apiKey ?: 0, |
|
38 | - 'domain' => get_home_url(), |
|
39 | - 'email' => $email ?: 0, |
|
40 | - ], |
|
41 | - ]); |
|
42 | - return $this; |
|
43 | - } |
|
30 | + /** |
|
31 | + * @return self |
|
32 | + */ |
|
33 | + public function activateKey($apiKey = '', $email = '') |
|
34 | + { |
|
35 | + $this->send('api_key_activation.php', [ |
|
36 | + 'body' => [ |
|
37 | + 'apikey' => $apiKey ?: 0, |
|
38 | + 'domain' => get_home_url(), |
|
39 | + 'email' => $email ?: 0, |
|
40 | + ], |
|
41 | + ]); |
|
42 | + return $this; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return self |
|
47 | - */ |
|
48 | - public function reset() |
|
49 | - { |
|
50 | - $this->message = ''; |
|
51 | - $this->response = []; |
|
52 | - $this->success = false; |
|
53 | - return $this; |
|
54 | - } |
|
45 | + /** |
|
46 | + * @return self |
|
47 | + */ |
|
48 | + public function reset() |
|
49 | + { |
|
50 | + $this->message = ''; |
|
51 | + $this->response = []; |
|
52 | + $this->success = false; |
|
53 | + return $this; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return self |
|
58 | - */ |
|
59 | - public function sendReview(Review $review) |
|
60 | - { |
|
61 | - $this->send('index.php', [ |
|
62 | - 'body' => $this->getBodyForReview($review), |
|
63 | - 'timeout' => 120, |
|
64 | - ]); |
|
65 | - return $this; |
|
66 | - } |
|
56 | + /** |
|
57 | + * @return self |
|
58 | + */ |
|
59 | + public function sendReview(Review $review) |
|
60 | + { |
|
61 | + $this->send('index.php', [ |
|
62 | + 'body' => $this->getBodyForReview($review), |
|
63 | + 'timeout' => 120, |
|
64 | + ]); |
|
65 | + return $this; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return self |
|
70 | - */ |
|
71 | - public function sendReviewResponse(Review $review) |
|
72 | - { |
|
73 | - $this->send('fetch_customer_reply.php', [ |
|
74 | - 'body' => $this->getBodyForResponse($review), |
|
75 | - ]); |
|
76 | - return $this; |
|
77 | - } |
|
68 | + /** |
|
69 | + * @return self |
|
70 | + */ |
|
71 | + public function sendReviewResponse(Review $review) |
|
72 | + { |
|
73 | + $this->send('fetch_customer_reply.php', [ |
|
74 | + 'body' => $this->getBodyForResponse($review), |
|
75 | + ]); |
|
76 | + return $this; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - protected function getBodyForResponse(Review $review) |
|
83 | - { |
|
84 | - $rebusifyResponse = [ |
|
85 | - 'reply' => Str::truncate($review->response, 300), |
|
86 | - 'review_id' => glsr(Database::class)->get($review->ID, 'rebusify'), // this is the rebusify review ID |
|
87 | - 'review_transaction_id' => $review->review_id, |
|
88 | - 'type' => 'M', |
|
89 | - ]; |
|
90 | - return apply_filters('site-reviews/rebusify/response', $rebusifyResponse, $review); |
|
91 | - } |
|
79 | + /** |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + protected function getBodyForResponse(Review $review) |
|
83 | + { |
|
84 | + $rebusifyResponse = [ |
|
85 | + 'reply' => Str::truncate($review->response, 300), |
|
86 | + 'review_id' => glsr(Database::class)->get($review->ID, 'rebusify'), // this is the rebusify review ID |
|
87 | + 'review_transaction_id' => $review->review_id, |
|
88 | + 'type' => 'M', |
|
89 | + ]; |
|
90 | + return apply_filters('site-reviews/rebusify/response', $rebusifyResponse, $review); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected function getBodyForReview(Review $review) |
|
97 | - { |
|
98 | - $rebusifyReview = [ |
|
99 | - 'domain' => get_home_url(), |
|
100 | - 'firstname' => Str::truncate(Str::convertName($review->author, 'first'), 25), |
|
101 | - 'rate' => $review->rating, |
|
102 | - 'review_transaction_id' => $review->review_id, |
|
103 | - 'reviews' => Str::truncate($review->content, 280), |
|
104 | - 'title' => Str::truncate($review->title, 35), |
|
105 | - 'transaction' => Application::ID, // woocommerce field, not needed for Site Reviews |
|
106 | - ]; |
|
107 | - return apply_filters('site-reviews/rebusify/review', $rebusifyReview, $review); |
|
108 | - } |
|
93 | + /** |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected function getBodyForReview(Review $review) |
|
97 | + { |
|
98 | + $rebusifyReview = [ |
|
99 | + 'domain' => get_home_url(), |
|
100 | + 'firstname' => Str::truncate(Str::convertName($review->author, 'first'), 25), |
|
101 | + 'rate' => $review->rating, |
|
102 | + 'review_transaction_id' => $review->review_id, |
|
103 | + 'reviews' => Str::truncate($review->content, 280), |
|
104 | + 'title' => Str::truncate($review->title, 35), |
|
105 | + 'transaction' => Application::ID, // woocommerce field, not needed for Site Reviews |
|
106 | + ]; |
|
107 | + return apply_filters('site-reviews/rebusify/review', $rebusifyReview, $review); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * @param \WP_Error|array $response |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - protected function handleResponse($response) |
|
115 | - { |
|
116 | - if (is_wp_error($response)) { |
|
117 | - $this->message = $response->get_error_message(); |
|
118 | - } else { |
|
119 | - $responseBody = wp_remote_retrieve_body($response); |
|
120 | - $responseCode = wp_remote_retrieve_response_code($response); |
|
121 | - $responseData = (array) json_decode($responseBody, true); |
|
122 | - $this->response = array_shift($responseData); |
|
123 | - $this->message = Arr::get($this->response, 'msg'); |
|
124 | - $this->success = 'success' === Arr::get($this->response, 'result') || 'yes' === Arr::get($this->response, 'success'); // @todo remove this ugly hack! |
|
125 | - if (200 !== $responseCode) { |
|
126 | - glsr_log()->error('Bad response code ['.$responseCode.']'); |
|
127 | - } |
|
128 | - if (!$this->success) { |
|
129 | - glsr_log()->error($this->message); |
|
130 | - } |
|
131 | - } |
|
132 | - } |
|
110 | + /** |
|
111 | + * @param \WP_Error|array $response |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + protected function handleResponse($response) |
|
115 | + { |
|
116 | + if (is_wp_error($response)) { |
|
117 | + $this->message = $response->get_error_message(); |
|
118 | + } else { |
|
119 | + $responseBody = wp_remote_retrieve_body($response); |
|
120 | + $responseCode = wp_remote_retrieve_response_code($response); |
|
121 | + $responseData = (array) json_decode($responseBody, true); |
|
122 | + $this->response = array_shift($responseData); |
|
123 | + $this->message = Arr::get($this->response, 'msg'); |
|
124 | + $this->success = 'success' === Arr::get($this->response, 'result') || 'yes' === Arr::get($this->response, 'success'); // @todo remove this ugly hack! |
|
125 | + if (200 !== $responseCode) { |
|
126 | + glsr_log()->error('Bad response code ['.$responseCode.']'); |
|
127 | + } |
|
128 | + if (!$this->success) { |
|
129 | + glsr_log()->error($this->message); |
|
130 | + } |
|
131 | + } |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @param string $endpoint |
|
136 | - * @return void |
|
137 | - */ |
|
138 | - protected function send($endpoint, array $args = []) |
|
139 | - { |
|
140 | - $args = wp_parse_args($args, [ |
|
141 | - 'body' => null, |
|
142 | - 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], |
|
143 | - 'redirection' => 5, |
|
144 | - 'sslverify' => false, |
|
145 | - 'timeout' => 5, |
|
146 | - ]); |
|
147 | - $this->reset(); |
|
148 | - $this->handleResponse( |
|
149 | - wp_remote_post(trailingslashit(static::API_URL).$endpoint, $args) |
|
150 | - ); |
|
151 | - } |
|
134 | + /** |
|
135 | + * @param string $endpoint |
|
136 | + * @return void |
|
137 | + */ |
|
138 | + protected function send($endpoint, array $args = []) |
|
139 | + { |
|
140 | + $args = wp_parse_args($args, [ |
|
141 | + 'body' => null, |
|
142 | + 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], |
|
143 | + 'redirection' => 5, |
|
144 | + 'sslverify' => false, |
|
145 | + 'timeout' => 5, |
|
146 | + ]); |
|
147 | + $this->reset(); |
|
148 | + $this->handleResponse( |
|
149 | + wp_remote_post(trailingslashit(static::API_URL).$endpoint, $args) |
|
150 | + ); |
|
151 | + } |
|
152 | 152 | } |
@@ -13,248 +13,248 @@ |
||
13 | 13 | |
14 | 14 | abstract class BaseType implements ArrayAccess, JsonSerializable, Type |
15 | 15 | { |
16 | - /** |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $allowed = []; |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $allowed = []; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - public $parents = []; |
|
21 | + /** |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + public $parents = []; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $properties = []; |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $properties = []; |
|
30 | 30 | |
31 | - /** |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $type; |
|
31 | + /** |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $type; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param string $method |
|
38 | - * @return static |
|
39 | - */ |
|
40 | - public function __call($method, array $arguments) |
|
41 | - { |
|
42 | - return $this->setProperty($method, Arr::get($arguments, 0)); |
|
43 | - } |
|
36 | + /** |
|
37 | + * @param string $method |
|
38 | + * @return static |
|
39 | + */ |
|
40 | + public function __call($method, array $arguments) |
|
41 | + { |
|
42 | + return $this->setProperty($method, Arr::get($arguments, 0)); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $type |
|
47 | - */ |
|
48 | - public function __construct($type = null) |
|
49 | - { |
|
50 | - $this->type = !is_string($type) |
|
51 | - ? (new ReflectionClass($this))->getShortName() |
|
52 | - : $type; |
|
53 | - $this->setAllowedProperties(); |
|
54 | - } |
|
45 | + /** |
|
46 | + * @param string $type |
|
47 | + */ |
|
48 | + public function __construct($type = null) |
|
49 | + { |
|
50 | + $this->type = !is_string($type) |
|
51 | + ? (new ReflectionClass($this))->getShortName() |
|
52 | + : $type; |
|
53 | + $this->setAllowedProperties(); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function __toString() |
|
60 | - { |
|
61 | - return $this->toScript(); |
|
62 | - } |
|
56 | + /** |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function __toString() |
|
60 | + { |
|
61 | + return $this->toScript(); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return static |
|
66 | - */ |
|
67 | - public function addProperties(array $properties) |
|
68 | - { |
|
69 | - foreach ($properties as $property => $value) { |
|
70 | - $this->setProperty($property, $value); |
|
71 | - } |
|
72 | - return $this; |
|
73 | - } |
|
64 | + /** |
|
65 | + * @return static |
|
66 | + */ |
|
67 | + public function addProperties(array $properties) |
|
68 | + { |
|
69 | + foreach ($properties as $property => $value) { |
|
70 | + $this->setProperty($property, $value); |
|
71 | + } |
|
72 | + return $this; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function getContext() |
|
79 | - { |
|
80 | - return 'https://schema.org'; |
|
81 | - } |
|
75 | + /** |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function getContext() |
|
79 | + { |
|
80 | + return 'https://schema.org'; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @return array |
|
85 | - */ |
|
86 | - public function getProperties() |
|
87 | - { |
|
88 | - return $this->properties; |
|
89 | - } |
|
83 | + /** |
|
84 | + * @return array |
|
85 | + */ |
|
86 | + public function getProperties() |
|
87 | + { |
|
88 | + return $this->properties; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * @param string $property |
|
93 | - * @param mixed $default |
|
94 | - * @return mixed |
|
95 | - */ |
|
96 | - public function getProperty($property, $default = null) |
|
97 | - { |
|
98 | - return Arr::get($this->properties, $property, $default); |
|
99 | - } |
|
91 | + /** |
|
92 | + * @param string $property |
|
93 | + * @param mixed $default |
|
94 | + * @return mixed |
|
95 | + */ |
|
96 | + public function getProperty($property, $default = null) |
|
97 | + { |
|
98 | + return Arr::get($this->properties, $property, $default); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - public function getType() |
|
105 | - { |
|
106 | - return $this->type; |
|
107 | - } |
|
101 | + /** |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + public function getType() |
|
105 | + { |
|
106 | + return $this->type; |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @param bool $condition |
|
111 | - * @param mixed $callback |
|
112 | - * @return static |
|
113 | - */ |
|
114 | - public function doIf($condition, $callback) |
|
115 | - { |
|
116 | - if ($condition) { |
|
117 | - $callback($this); |
|
118 | - } |
|
119 | - return $this; |
|
120 | - } |
|
109 | + /** |
|
110 | + * @param bool $condition |
|
111 | + * @param mixed $callback |
|
112 | + * @return static |
|
113 | + */ |
|
114 | + public function doIf($condition, $callback) |
|
115 | + { |
|
116 | + if ($condition) { |
|
117 | + $callback($this); |
|
118 | + } |
|
119 | + return $this; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - public function jsonSerialize() |
|
126 | - { |
|
127 | - return $this->toArray(); |
|
128 | - } |
|
122 | + /** |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + public function jsonSerialize() |
|
126 | + { |
|
127 | + return $this->toArray(); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @param mixed $offset |
|
132 | - * @return bool |
|
133 | - */ |
|
134 | - public function offsetExists($offset) |
|
135 | - { |
|
136 | - return array_key_exists($offset, $this->properties); |
|
137 | - } |
|
130 | + /** |
|
131 | + * @param mixed $offset |
|
132 | + * @return bool |
|
133 | + */ |
|
134 | + public function offsetExists($offset) |
|
135 | + { |
|
136 | + return array_key_exists($offset, $this->properties); |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * @param string $offset |
|
141 | - * @return mixed |
|
142 | - */ |
|
143 | - public function offsetGet($offset) |
|
144 | - { |
|
145 | - return $this->getProperty($offset); |
|
146 | - } |
|
139 | + /** |
|
140 | + * @param string $offset |
|
141 | + * @return mixed |
|
142 | + */ |
|
143 | + public function offsetGet($offset) |
|
144 | + { |
|
145 | + return $this->getProperty($offset); |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * @param string $offset |
|
150 | - * @param mixed $value |
|
151 | - * @return void |
|
152 | - */ |
|
153 | - public function offsetSet($offset, $value) |
|
154 | - { |
|
155 | - $this->setProperty($offset, $value); |
|
156 | - } |
|
148 | + /** |
|
149 | + * @param string $offset |
|
150 | + * @param mixed $value |
|
151 | + * @return void |
|
152 | + */ |
|
153 | + public function offsetSet($offset, $value) |
|
154 | + { |
|
155 | + $this->setProperty($offset, $value); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @param string $offset |
|
160 | - * @return void |
|
161 | - */ |
|
162 | - public function offsetUnset($offset) |
|
163 | - { |
|
164 | - unset($this->properties[$offset]); |
|
165 | - } |
|
158 | + /** |
|
159 | + * @param string $offset |
|
160 | + * @return void |
|
161 | + */ |
|
162 | + public function offsetUnset($offset) |
|
163 | + { |
|
164 | + unset($this->properties[$offset]); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @param string $property |
|
169 | - * @param mixed $value |
|
170 | - * @return static |
|
171 | - */ |
|
172 | - public function setProperty($property, $value) |
|
173 | - { |
|
174 | - if (!in_array($property, $this->allowed) |
|
175 | - && 'UnknownType' != (new ReflectionClass($this))->getShortName()) { |
|
176 | - glsr_log()->warning($this->getType().' does not allow the "'.$property.'" property'); |
|
177 | - return $this; |
|
178 | - } |
|
179 | - $this->properties[$property] = $value; |
|
180 | - return $this; |
|
181 | - } |
|
167 | + /** |
|
168 | + * @param string $property |
|
169 | + * @param mixed $value |
|
170 | + * @return static |
|
171 | + */ |
|
172 | + public function setProperty($property, $value) |
|
173 | + { |
|
174 | + if (!in_array($property, $this->allowed) |
|
175 | + && 'UnknownType' != (new ReflectionClass($this))->getShortName()) { |
|
176 | + glsr_log()->warning($this->getType().' does not allow the "'.$property.'" property'); |
|
177 | + return $this; |
|
178 | + } |
|
179 | + $this->properties[$property] = $value; |
|
180 | + return $this; |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * @return array |
|
185 | - */ |
|
186 | - public function toArray() |
|
187 | - { |
|
188 | - return [ |
|
189 | - '@context' => $this->getContext(), |
|
190 | - '@type' => $this->getType(), |
|
191 | - ] + $this->serializeProperty($this->getProperties()); |
|
192 | - } |
|
183 | + /** |
|
184 | + * @return array |
|
185 | + */ |
|
186 | + public function toArray() |
|
187 | + { |
|
188 | + return [ |
|
189 | + '@context' => $this->getContext(), |
|
190 | + '@type' => $this->getType(), |
|
191 | + ] + $this->serializeProperty($this->getProperties()); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * @return string |
|
196 | - */ |
|
197 | - public function toScript() |
|
198 | - { |
|
199 | - return sprintf('<script type="application/ld+json">%s</script>', |
|
200 | - json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
201 | - ); |
|
202 | - } |
|
194 | + /** |
|
195 | + * @return string |
|
196 | + */ |
|
197 | + public function toScript() |
|
198 | + { |
|
199 | + return sprintf('<script type="application/ld+json">%s</script>', |
|
200 | + json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
201 | + ); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * @param array|null $parents |
|
206 | - * @return array |
|
207 | - */ |
|
208 | - protected function getParents($parents = null) |
|
209 | - { |
|
210 | - if (!isset($parents)) { |
|
211 | - $parents = $this->parents; |
|
212 | - } |
|
213 | - $newParents = $parents; |
|
214 | - foreach ($parents as $parent) { |
|
215 | - $parentClass = Helper::buildClassName($parent, __NAMESPACE__); |
|
216 | - if (!class_exists($parentClass)) { |
|
217 | - continue; |
|
218 | - } |
|
219 | - $newParents = array_merge($newParents, $this->getParents((new $parentClass())->parents)); |
|
220 | - } |
|
221 | - return array_values(array_unique($newParents)); |
|
222 | - } |
|
204 | + /** |
|
205 | + * @param array|null $parents |
|
206 | + * @return array |
|
207 | + */ |
|
208 | + protected function getParents($parents = null) |
|
209 | + { |
|
210 | + if (!isset($parents)) { |
|
211 | + $parents = $this->parents; |
|
212 | + } |
|
213 | + $newParents = $parents; |
|
214 | + foreach ($parents as $parent) { |
|
215 | + $parentClass = Helper::buildClassName($parent, __NAMESPACE__); |
|
216 | + if (!class_exists($parentClass)) { |
|
217 | + continue; |
|
218 | + } |
|
219 | + $newParents = array_merge($newParents, $this->getParents((new $parentClass())->parents)); |
|
220 | + } |
|
221 | + return array_values(array_unique($newParents)); |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
225 | - * @return void |
|
226 | - */ |
|
227 | - protected function setAllowedProperties() |
|
228 | - { |
|
229 | - $parents = $this->getParents(); |
|
230 | - foreach ($parents as $parent) { |
|
231 | - $parentClass = Helper::buildClassName($parent, __NAMESPACE__); |
|
232 | - if (!class_exists($parentClass)) { |
|
233 | - continue; |
|
234 | - } |
|
235 | - $this->allowed = array_values(array_unique(array_merge((new $parentClass())->allowed, $this->allowed))); |
|
236 | - } |
|
237 | - } |
|
224 | + /** |
|
225 | + * @return void |
|
226 | + */ |
|
227 | + protected function setAllowedProperties() |
|
228 | + { |
|
229 | + $parents = $this->getParents(); |
|
230 | + foreach ($parents as $parent) { |
|
231 | + $parentClass = Helper::buildClassName($parent, __NAMESPACE__); |
|
232 | + if (!class_exists($parentClass)) { |
|
233 | + continue; |
|
234 | + } |
|
235 | + $this->allowed = array_values(array_unique(array_merge((new $parentClass())->allowed, $this->allowed))); |
|
236 | + } |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * @param mixed $property |
|
241 | - * @return array|string |
|
242 | - */ |
|
243 | - protected function serializeProperty($property) |
|
244 | - { |
|
245 | - if (is_array($property)) { |
|
246 | - return array_map([$this, 'serializeProperty'], $property); |
|
247 | - } |
|
248 | - if ($property instanceof Type) { |
|
249 | - $property = $property->toArray(); |
|
250 | - unset($property['@context']); |
|
251 | - } |
|
252 | - if ($property instanceof DateTimeInterface) { |
|
253 | - $property = $property->format(DateTime::ATOM); |
|
254 | - } |
|
255 | - if (is_object($property)) { |
|
256 | - throw new InvalidProperty(); |
|
257 | - } |
|
258 | - return $property; |
|
259 | - } |
|
239 | + /** |
|
240 | + * @param mixed $property |
|
241 | + * @return array|string |
|
242 | + */ |
|
243 | + protected function serializeProperty($property) |
|
244 | + { |
|
245 | + if (is_array($property)) { |
|
246 | + return array_map([$this, 'serializeProperty'], $property); |
|
247 | + } |
|
248 | + if ($property instanceof Type) { |
|
249 | + $property = $property->toArray(); |
|
250 | + unset($property['@context']); |
|
251 | + } |
|
252 | + if ($property instanceof DateTimeInterface) { |
|
253 | + $property = $property->format(DateTime::ATOM); |
|
254 | + } |
|
255 | + if (is_object($property)) { |
|
256 | + throw new InvalidProperty(); |
|
257 | + } |
|
258 | + return $property; |
|
259 | + } |
|
260 | 260 | } |
@@ -11,239 +11,239 @@ |
||
11 | 11 | |
12 | 12 | class Settings |
13 | 13 | { |
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - public $settings; |
|
18 | - |
|
19 | - /** |
|
20 | - * @param string $id |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - public function buildFields($id) |
|
24 | - { |
|
25 | - $this->settings = glsr(DefaultsManager::class)->settings(); |
|
26 | - $method = Helper::buildMethodName($id, 'getTemplateDataFor'); |
|
27 | - $data = !method_exists($this, $method) |
|
28 | - ? $this->getTemplateData($id) |
|
29 | - : $this->$method($id); |
|
30 | - return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - protected function getFieldDefault(array $field) |
|
37 | - { |
|
38 | - return Arr::get($field, 'default'); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @return string |
|
43 | - */ |
|
44 | - protected function getFieldNameForDependsOn($path) |
|
45 | - { |
|
46 | - $fieldName = Str::convertPathToName($path, OptionManager::databaseKey()); |
|
47 | - return $this->isMultiDependency($path) |
|
48 | - ? $fieldName.'[]' |
|
49 | - : $fieldName; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - protected function getSettingFields($path) |
|
56 | - { |
|
57 | - return array_filter($this->settings, function ($key) use ($path) { |
|
58 | - return Str::startsWith($path, $key); |
|
59 | - }, ARRAY_FILTER_USE_KEY); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - protected function getSettingRows(array $fields) |
|
66 | - { |
|
67 | - $rows = ''; |
|
68 | - foreach ($fields as $name => $field) { |
|
69 | - $field = wp_parse_args($field, [ |
|
70 | - 'is_setting' => true, |
|
71 | - 'name' => $name, |
|
72 | - ]); |
|
73 | - $rows.= new Field($this->normalize($field)); |
|
74 | - } |
|
75 | - return $rows; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param string $id |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - protected function getTemplateData($id) |
|
83 | - { |
|
84 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
85 | - return [ |
|
86 | - 'context' => [ |
|
87 | - 'rows' => $this->getSettingRows($fields), |
|
88 | - ], |
|
89 | - ]; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param string $id |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected function getTemplateDataForAddons($id) |
|
97 | - { |
|
98 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
99 | - $settings = Arr::convertDotNotationArray($fields); |
|
100 | - $settingKeys = array_keys($settings['settings']['addons']); |
|
101 | - $results = []; |
|
102 | - foreach ($settingKeys as $key) { |
|
103 | - $addonFields = array_filter($fields, function ($path) use ($key) { |
|
104 | - return Str::startsWith('settings.addons.'.$key, $path); |
|
105 | - }, ARRAY_FILTER_USE_KEY); |
|
106 | - $results[$key] = $this->getSettingRows($addonFields); |
|
107 | - } |
|
108 | - ksort($results); |
|
109 | - return [ |
|
110 | - 'settings' => $results, |
|
111 | - ]; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param string $id |
|
116 | - * @return array |
|
117 | - */ |
|
118 | - protected function getTemplateDataForLicenses($id) |
|
119 | - { |
|
120 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
121 | - ksort($fields); |
|
122 | - return [ |
|
123 | - 'context' => [ |
|
124 | - 'rows' => $this->getSettingRows($fields), |
|
125 | - ], |
|
126 | - ]; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - protected function getTemplateDataForTranslations() |
|
133 | - { |
|
134 | - $translations = glsr(Translation::class)->renderAll(); |
|
135 | - $class = empty($translations) |
|
136 | - ? 'glsr-hidden' |
|
137 | - : ''; |
|
138 | - return [ |
|
139 | - 'context' => [ |
|
140 | - 'class' => $class, |
|
141 | - 'database_key' => OptionManager::databaseKey(), |
|
142 | - 'translations' => $translations, |
|
143 | - ], |
|
144 | - ]; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $path |
|
149 | - * @param string|array $expectedValue |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - protected function isFieldHidden($path, $expectedValue) |
|
153 | - { |
|
154 | - $optionValue = glsr(OptionManager::class)->get( |
|
155 | - $path, |
|
156 | - Arr::get(glsr()->defaults, $path) |
|
157 | - ); |
|
158 | - if (is_array($expectedValue)) { |
|
159 | - return is_array($optionValue) |
|
160 | - ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
161 | - : !in_array($optionValue, $expectedValue); |
|
162 | - } |
|
163 | - return $optionValue != $expectedValue; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - protected function isMultiDependency($path) |
|
170 | - { |
|
171 | - if (isset($this->settings[$path])) { |
|
172 | - $field = $this->settings[$path]; |
|
173 | - return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
174 | - || !empty($field['multiple']); |
|
175 | - } |
|
176 | - return false; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - protected function normalize(array $field) |
|
183 | - { |
|
184 | - $field = $this->normalizeDependsOn($field); |
|
185 | - $field = $this->normalizeLabelAndLegend($field); |
|
186 | - $field = $this->normalizeValue($field); |
|
187 | - return $field; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @return array |
|
192 | - */ |
|
193 | - protected function normalizeDependsOn(array $field) |
|
194 | - { |
|
195 | - if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
196 | - $isFieldHidden = false; |
|
197 | - $conditions = []; |
|
198 | - foreach ($field['depends_on'] as $path => $value) { |
|
199 | - $conditions[] = [ |
|
200 | - 'name' => $this->getFieldNameForDependsOn($path), |
|
201 | - 'value' => $value, |
|
202 | - ]; |
|
203 | - if ($this->isFieldHidden($path, $value)) { |
|
204 | - $isFieldHidden = true; |
|
205 | - } |
|
206 | - } |
|
207 | - $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
208 | - $field['is_hidden'] = $isFieldHidden; |
|
209 | - } |
|
210 | - return $field; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * @return array |
|
215 | - */ |
|
216 | - protected function normalizeLabelAndLegend(array $field) |
|
217 | - { |
|
218 | - if (!empty($field['label'])) { |
|
219 | - $field['legend'] = $field['label']; |
|
220 | - unset($field['label']); |
|
221 | - } else { |
|
222 | - $field['is_valid'] = false; |
|
223 | - glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
224 | - } |
|
225 | - return $field; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @return array |
|
230 | - */ |
|
231 | - protected function normalizeValue(array $field) |
|
232 | - { |
|
233 | - if (!isset($field['value'])) { |
|
234 | - $field['value'] = glsr(OptionManager::class)->get( |
|
235 | - $field['name'], |
|
236 | - $this->getFieldDefault($field) |
|
237 | - ); |
|
238 | - } |
|
239 | - return $field; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @return string |
|
244 | - */ |
|
245 | - protected function normalizeSettingPath($path) |
|
246 | - { |
|
247 | - return Str::prefix('settings.', rtrim($path, '.')); |
|
248 | - } |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + public $settings; |
|
18 | + |
|
19 | + /** |
|
20 | + * @param string $id |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + public function buildFields($id) |
|
24 | + { |
|
25 | + $this->settings = glsr(DefaultsManager::class)->settings(); |
|
26 | + $method = Helper::buildMethodName($id, 'getTemplateDataFor'); |
|
27 | + $data = !method_exists($this, $method) |
|
28 | + ? $this->getTemplateData($id) |
|
29 | + : $this->$method($id); |
|
30 | + return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + protected function getFieldDefault(array $field) |
|
37 | + { |
|
38 | + return Arr::get($field, 'default'); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @return string |
|
43 | + */ |
|
44 | + protected function getFieldNameForDependsOn($path) |
|
45 | + { |
|
46 | + $fieldName = Str::convertPathToName($path, OptionManager::databaseKey()); |
|
47 | + return $this->isMultiDependency($path) |
|
48 | + ? $fieldName.'[]' |
|
49 | + : $fieldName; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + protected function getSettingFields($path) |
|
56 | + { |
|
57 | + return array_filter($this->settings, function ($key) use ($path) { |
|
58 | + return Str::startsWith($path, $key); |
|
59 | + }, ARRAY_FILTER_USE_KEY); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + protected function getSettingRows(array $fields) |
|
66 | + { |
|
67 | + $rows = ''; |
|
68 | + foreach ($fields as $name => $field) { |
|
69 | + $field = wp_parse_args($field, [ |
|
70 | + 'is_setting' => true, |
|
71 | + 'name' => $name, |
|
72 | + ]); |
|
73 | + $rows.= new Field($this->normalize($field)); |
|
74 | + } |
|
75 | + return $rows; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param string $id |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + protected function getTemplateData($id) |
|
83 | + { |
|
84 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
85 | + return [ |
|
86 | + 'context' => [ |
|
87 | + 'rows' => $this->getSettingRows($fields), |
|
88 | + ], |
|
89 | + ]; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param string $id |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected function getTemplateDataForAddons($id) |
|
97 | + { |
|
98 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
99 | + $settings = Arr::convertDotNotationArray($fields); |
|
100 | + $settingKeys = array_keys($settings['settings']['addons']); |
|
101 | + $results = []; |
|
102 | + foreach ($settingKeys as $key) { |
|
103 | + $addonFields = array_filter($fields, function ($path) use ($key) { |
|
104 | + return Str::startsWith('settings.addons.'.$key, $path); |
|
105 | + }, ARRAY_FILTER_USE_KEY); |
|
106 | + $results[$key] = $this->getSettingRows($addonFields); |
|
107 | + } |
|
108 | + ksort($results); |
|
109 | + return [ |
|
110 | + 'settings' => $results, |
|
111 | + ]; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param string $id |
|
116 | + * @return array |
|
117 | + */ |
|
118 | + protected function getTemplateDataForLicenses($id) |
|
119 | + { |
|
120 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
121 | + ksort($fields); |
|
122 | + return [ |
|
123 | + 'context' => [ |
|
124 | + 'rows' => $this->getSettingRows($fields), |
|
125 | + ], |
|
126 | + ]; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + protected function getTemplateDataForTranslations() |
|
133 | + { |
|
134 | + $translations = glsr(Translation::class)->renderAll(); |
|
135 | + $class = empty($translations) |
|
136 | + ? 'glsr-hidden' |
|
137 | + : ''; |
|
138 | + return [ |
|
139 | + 'context' => [ |
|
140 | + 'class' => $class, |
|
141 | + 'database_key' => OptionManager::databaseKey(), |
|
142 | + 'translations' => $translations, |
|
143 | + ], |
|
144 | + ]; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $path |
|
149 | + * @param string|array $expectedValue |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + protected function isFieldHidden($path, $expectedValue) |
|
153 | + { |
|
154 | + $optionValue = glsr(OptionManager::class)->get( |
|
155 | + $path, |
|
156 | + Arr::get(glsr()->defaults, $path) |
|
157 | + ); |
|
158 | + if (is_array($expectedValue)) { |
|
159 | + return is_array($optionValue) |
|
160 | + ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
161 | + : !in_array($optionValue, $expectedValue); |
|
162 | + } |
|
163 | + return $optionValue != $expectedValue; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + protected function isMultiDependency($path) |
|
170 | + { |
|
171 | + if (isset($this->settings[$path])) { |
|
172 | + $field = $this->settings[$path]; |
|
173 | + return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
174 | + || !empty($field['multiple']); |
|
175 | + } |
|
176 | + return false; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + protected function normalize(array $field) |
|
183 | + { |
|
184 | + $field = $this->normalizeDependsOn($field); |
|
185 | + $field = $this->normalizeLabelAndLegend($field); |
|
186 | + $field = $this->normalizeValue($field); |
|
187 | + return $field; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @return array |
|
192 | + */ |
|
193 | + protected function normalizeDependsOn(array $field) |
|
194 | + { |
|
195 | + if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
196 | + $isFieldHidden = false; |
|
197 | + $conditions = []; |
|
198 | + foreach ($field['depends_on'] as $path => $value) { |
|
199 | + $conditions[] = [ |
|
200 | + 'name' => $this->getFieldNameForDependsOn($path), |
|
201 | + 'value' => $value, |
|
202 | + ]; |
|
203 | + if ($this->isFieldHidden($path, $value)) { |
|
204 | + $isFieldHidden = true; |
|
205 | + } |
|
206 | + } |
|
207 | + $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
208 | + $field['is_hidden'] = $isFieldHidden; |
|
209 | + } |
|
210 | + return $field; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * @return array |
|
215 | + */ |
|
216 | + protected function normalizeLabelAndLegend(array $field) |
|
217 | + { |
|
218 | + if (!empty($field['label'])) { |
|
219 | + $field['legend'] = $field['label']; |
|
220 | + unset($field['label']); |
|
221 | + } else { |
|
222 | + $field['is_valid'] = false; |
|
223 | + glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
224 | + } |
|
225 | + return $field; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @return array |
|
230 | + */ |
|
231 | + protected function normalizeValue(array $field) |
|
232 | + { |
|
233 | + if (!isset($field['value'])) { |
|
234 | + $field['value'] = glsr(OptionManager::class)->get( |
|
235 | + $field['name'], |
|
236 | + $this->getFieldDefault($field) |
|
237 | + ); |
|
238 | + } |
|
239 | + return $field; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @return string |
|
244 | + */ |
|
245 | + protected function normalizeSettingPath($path) |
|
246 | + { |
|
247 | + return Str::prefix('settings.', rtrim($path, '.')); |
|
248 | + } |
|
249 | 249 | } |
@@ -7,73 +7,73 @@ |
||
7 | 7 | |
8 | 8 | abstract class Field |
9 | 9 | { |
10 | - /** |
|
11 | - * @var Builder |
|
12 | - */ |
|
13 | - protected $builder; |
|
10 | + /** |
|
11 | + * @var Builder |
|
12 | + */ |
|
13 | + protected $builder; |
|
14 | 14 | |
15 | - public function __construct(Builder $builder) |
|
16 | - { |
|
17 | - $this->builder = $builder; |
|
18 | - } |
|
15 | + public function __construct(Builder $builder) |
|
16 | + { |
|
17 | + $this->builder = $builder; |
|
18 | + } |
|
19 | 19 | |
20 | - /** |
|
21 | - * @return string|void |
|
22 | - */ |
|
23 | - public function build() |
|
24 | - { |
|
25 | - glsr_log()->error('Build method is not implemented for '.get_class($this)); |
|
26 | - } |
|
20 | + /** |
|
21 | + * @return string|void |
|
22 | + */ |
|
23 | + public function build() |
|
24 | + { |
|
25 | + glsr_log()->error('Build method is not implemented for '.get_class($this)); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public static function defaults() |
|
32 | - { |
|
33 | - return []; |
|
34 | - } |
|
28 | + /** |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public static function defaults() |
|
32 | + { |
|
33 | + return []; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public static function merge(array $args) |
|
40 | - { |
|
41 | - $merged = array_merge( |
|
42 | - wp_parse_args($args, static::defaults()), |
|
43 | - static::required() |
|
44 | - ); |
|
45 | - $merged['class'] = implode(' ', static::mergedAttribute('class', ' ', $args)); |
|
46 | - $merged['style'] = implode(';', static::mergedAttribute('style', ';', $args)); |
|
47 | - return $merged; |
|
48 | - } |
|
36 | + /** |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public static function merge(array $args) |
|
40 | + { |
|
41 | + $merged = array_merge( |
|
42 | + wp_parse_args($args, static::defaults()), |
|
43 | + static::required() |
|
44 | + ); |
|
45 | + $merged['class'] = implode(' ', static::mergedAttribute('class', ' ', $args)); |
|
46 | + $merged['style'] = implode(';', static::mergedAttribute('style', ';', $args)); |
|
47 | + return $merged; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param string $delimiter |
|
52 | - * @param string $key |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - public static function mergedAttribute($key, $delimiter, array $args) |
|
56 | - { |
|
57 | - return array_filter(array_merge( |
|
58 | - explode($delimiter, Arr::get($args, $key)), |
|
59 | - explode($delimiter, Arr::get(static::defaults(), $key)), |
|
60 | - explode($delimiter, Arr::get(static::required(), $key)) |
|
61 | - )); |
|
62 | - } |
|
50 | + /** |
|
51 | + * @param string $delimiter |
|
52 | + * @param string $key |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + public static function mergedAttribute($key, $delimiter, array $args) |
|
56 | + { |
|
57 | + return array_filter(array_merge( |
|
58 | + explode($delimiter, Arr::get($args, $key)), |
|
59 | + explode($delimiter, Arr::get(static::defaults(), $key)), |
|
60 | + explode($delimiter, Arr::get(static::required(), $key)) |
|
61 | + )); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public static function required() |
|
68 | - { |
|
69 | - return []; |
|
70 | - } |
|
64 | + /** |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public static function required() |
|
68 | + { |
|
69 | + return []; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - protected function mergeFieldArgs() |
|
76 | - { |
|
77 | - $this->builder->args = static::merge($this->builder->args); |
|
78 | - } |
|
72 | + /** |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + protected function mergeFieldArgs() |
|
76 | + { |
|
77 | + $this->builder->args = static::merge($this->builder->args); |
|
78 | + } |
|
79 | 79 | } |
@@ -7,78 +7,78 @@ |
||
7 | 7 | |
8 | 8 | class Template |
9 | 9 | { |
10 | - /** |
|
11 | - * @param string $templatePath |
|
12 | - * @return void|string |
|
13 | - */ |
|
14 | - public function build($templatePath, array $data = []) |
|
15 | - { |
|
16 | - $data = $this->normalize($data); |
|
17 | - ob_start(); |
|
18 | - glsr()->render($templatePath, $data); |
|
19 | - $template = ob_get_clean(); |
|
20 | - $path = Str::removePrefix('templates/', $templatePath); |
|
21 | - $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
22 | - $template = $this->interpolate($template, $data, $path); |
|
23 | - $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
24 | - $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
25 | - return $template; |
|
26 | - } |
|
10 | + /** |
|
11 | + * @param string $templatePath |
|
12 | + * @return void|string |
|
13 | + */ |
|
14 | + public function build($templatePath, array $data = []) |
|
15 | + { |
|
16 | + $data = $this->normalize($data); |
|
17 | + ob_start(); |
|
18 | + glsr()->render($templatePath, $data); |
|
19 | + $template = ob_get_clean(); |
|
20 | + $path = Str::removePrefix('templates/', $templatePath); |
|
21 | + $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
22 | + $template = $this->interpolate($template, $data, $path); |
|
23 | + $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
24 | + $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
25 | + return $template; |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Interpolate context values into template placeholders. |
|
30 | - * @param string $template |
|
31 | - * @param string $templatePath |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - public function interpolate($template, array $data = [], $templatePath) |
|
35 | - { |
|
36 | - $context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
37 | - $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data); |
|
38 | - foreach ($context as $key => $value) { |
|
39 | - $template = strtr( |
|
40 | - $template, |
|
41 | - array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
42 | - ); |
|
43 | - } |
|
44 | - return trim($template); |
|
45 | - } |
|
28 | + /** |
|
29 | + * Interpolate context values into template placeholders. |
|
30 | + * @param string $template |
|
31 | + * @param string $templatePath |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + public function interpolate($template, array $data = [], $templatePath) |
|
35 | + { |
|
36 | + $context = $this->normalizeContext(Arr::get($data, 'context', [])); |
|
37 | + $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data); |
|
38 | + foreach ($context as $key => $value) { |
|
39 | + $template = strtr( |
|
40 | + $template, |
|
41 | + array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
42 | + ); |
|
43 | + } |
|
44 | + return trim($template); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param string $templatePath |
|
49 | - * @return void|string |
|
50 | - */ |
|
51 | - public function render($templatePath, array $data = []) |
|
52 | - { |
|
53 | - echo $this->build($templatePath, $data); |
|
54 | - } |
|
47 | + /** |
|
48 | + * @param string $templatePath |
|
49 | + * @return void|string |
|
50 | + */ |
|
51 | + public function render($templatePath, array $data = []) |
|
52 | + { |
|
53 | + echo $this->build($templatePath, $data); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return array |
|
58 | - */ |
|
59 | - protected function normalize(array $data) |
|
60 | - { |
|
61 | - $arrayKeys = ['context', 'globals']; |
|
62 | - $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
63 | - foreach ($arrayKeys as $key) { |
|
64 | - if (is_array($data[$key])) { |
|
65 | - continue; |
|
66 | - } |
|
67 | - $data[$key] = []; |
|
68 | - } |
|
69 | - return $data; |
|
70 | - } |
|
56 | + /** |
|
57 | + * @return array |
|
58 | + */ |
|
59 | + protected function normalize(array $data) |
|
60 | + { |
|
61 | + $arrayKeys = ['context', 'globals']; |
|
62 | + $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
63 | + foreach ($arrayKeys as $key) { |
|
64 | + if (is_array($data[$key])) { |
|
65 | + continue; |
|
66 | + } |
|
67 | + $data[$key] = []; |
|
68 | + } |
|
69 | + return $data; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - protected function normalizeContext(array $context) |
|
76 | - { |
|
77 | - $context = array_filter($context, function ($value) { |
|
78 | - return !is_array($value) && !is_object($value); |
|
79 | - }); |
|
80 | - return array_map(function ($value) { |
|
81 | - return (string) $value; |
|
82 | - }, $context); |
|
83 | - } |
|
72 | + /** |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + protected function normalizeContext(array $context) |
|
76 | + { |
|
77 | + $context = array_filter($context, function ($value) { |
|
78 | + return !is_array($value) && !is_object($value); |
|
79 | + }); |
|
80 | + return array_map(function ($value) { |
|
81 | + return (string) $value; |
|
82 | + }, $context); |
|
83 | + } |
|
84 | 84 | } |