@@ -12,173 +12,173 @@ |
||
12 | 12 | |
13 | 13 | class Review implements \ArrayAccess |
14 | 14 | { |
15 | - public $assigned_to; |
|
16 | - public $author; |
|
17 | - public $avatar; |
|
18 | - public $content; |
|
19 | - public $custom; |
|
20 | - public $date; |
|
21 | - public $email; |
|
22 | - public $ID; |
|
23 | - public $ip_address; |
|
24 | - public $modified; |
|
25 | - public $pinned; |
|
26 | - public $rating; |
|
27 | - public $response; |
|
28 | - public $review_id; |
|
29 | - public $review_type; |
|
30 | - public $status; |
|
31 | - public $term_ids; |
|
32 | - public $title; |
|
33 | - public $url; |
|
34 | - public $user_id; |
|
15 | + public $assigned_to; |
|
16 | + public $author; |
|
17 | + public $avatar; |
|
18 | + public $content; |
|
19 | + public $custom; |
|
20 | + public $date; |
|
21 | + public $email; |
|
22 | + public $ID; |
|
23 | + public $ip_address; |
|
24 | + public $modified; |
|
25 | + public $pinned; |
|
26 | + public $rating; |
|
27 | + public $response; |
|
28 | + public $review_id; |
|
29 | + public $review_type; |
|
30 | + public $status; |
|
31 | + public $term_ids; |
|
32 | + public $title; |
|
33 | + public $url; |
|
34 | + public $user_id; |
|
35 | 35 | |
36 | - public function __construct(WP_Post $post) |
|
37 | - { |
|
38 | - if (Application::POST_TYPE != $post->post_type) { |
|
39 | - return; |
|
40 | - } |
|
41 | - $this->content = $post->post_content; |
|
42 | - $this->date = $post->post_date; |
|
43 | - $this->ID = intval($post->ID); |
|
44 | - $this->status = $post->post_status; |
|
45 | - $this->title = $post->post_title; |
|
46 | - $this->user_id = intval($post->post_author); |
|
47 | - $this->setProperties($post); |
|
48 | - $this->setTermIds($post); |
|
49 | - } |
|
36 | + public function __construct(WP_Post $post) |
|
37 | + { |
|
38 | + if (Application::POST_TYPE != $post->post_type) { |
|
39 | + return; |
|
40 | + } |
|
41 | + $this->content = $post->post_content; |
|
42 | + $this->date = $post->post_date; |
|
43 | + $this->ID = intval($post->ID); |
|
44 | + $this->status = $post->post_status; |
|
45 | + $this->title = $post->post_title; |
|
46 | + $this->user_id = intval($post->post_author); |
|
47 | + $this->setProperties($post); |
|
48 | + $this->setTermIds($post); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @return mixed |
|
53 | - */ |
|
54 | - public function __get($key) |
|
55 | - { |
|
56 | - return $this->offsetGet($key); |
|
57 | - } |
|
51 | + /** |
|
52 | + * @return mixed |
|
53 | + */ |
|
54 | + public function __get($key) |
|
55 | + { |
|
56 | + return $this->offsetGet($key); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @return string |
|
61 | - */ |
|
62 | - public function __toString() |
|
63 | - { |
|
64 | - return (string) $this->build(); |
|
65 | - } |
|
59 | + /** |
|
60 | + * @return string |
|
61 | + */ |
|
62 | + public function __toString() |
|
63 | + { |
|
64 | + return (string) $this->build(); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @return ReviewHtml |
|
69 | - */ |
|
70 | - public function build(array $args = []) |
|
71 | - { |
|
72 | - if (empty($this->ID)) { |
|
73 | - return new ReviewHtml($this); |
|
74 | - } |
|
75 | - $partial = glsr(SiteReviewsPartial::class); |
|
76 | - $partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
77 | - $partial->options = Arr::flattenArray(glsr(OptionManager::class)->all()); |
|
78 | - return $partial->buildReview($this); |
|
79 | - } |
|
67 | + /** |
|
68 | + * @return ReviewHtml |
|
69 | + */ |
|
70 | + public function build(array $args = []) |
|
71 | + { |
|
72 | + if (empty($this->ID)) { |
|
73 | + return new ReviewHtml($this); |
|
74 | + } |
|
75 | + $partial = glsr(SiteReviewsPartial::class); |
|
76 | + $partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
77 | + $partial->options = Arr::flattenArray(glsr(OptionManager::class)->all()); |
|
78 | + return $partial->buildReview($this); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param mixed $key |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - public function offsetExists($key) |
|
86 | - { |
|
87 | - return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
|
88 | - } |
|
81 | + /** |
|
82 | + * @param mixed $key |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + public function offsetExists($key) |
|
86 | + { |
|
87 | + return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @param mixed $key |
|
92 | - * @return mixed |
|
93 | - */ |
|
94 | - public function offsetGet($key) |
|
95 | - { |
|
96 | - return property_exists($this, $key) |
|
97 | - ? $this->$key |
|
98 | - : Arr::get($this->custom, $key, null); |
|
99 | - } |
|
90 | + /** |
|
91 | + * @param mixed $key |
|
92 | + * @return mixed |
|
93 | + */ |
|
94 | + public function offsetGet($key) |
|
95 | + { |
|
96 | + return property_exists($this, $key) |
|
97 | + ? $this->$key |
|
98 | + : Arr::get($this->custom, $key, null); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @param mixed $key |
|
103 | - * @param mixed $value |
|
104 | - * @return void |
|
105 | - */ |
|
106 | - public function offsetSet($key, $value) |
|
107 | - { |
|
108 | - if (property_exists($this, $key)) { |
|
109 | - $this->$key = $value; |
|
110 | - return; |
|
111 | - } |
|
112 | - if (!is_array($this->custom)) { |
|
113 | - $this->custom = array_filter((array) $this->custom); |
|
114 | - } |
|
115 | - $this->custom[$key] = $value; |
|
116 | - } |
|
101 | + /** |
|
102 | + * @param mixed $key |
|
103 | + * @param mixed $value |
|
104 | + * @return void |
|
105 | + */ |
|
106 | + public function offsetSet($key, $value) |
|
107 | + { |
|
108 | + if (property_exists($this, $key)) { |
|
109 | + $this->$key = $value; |
|
110 | + return; |
|
111 | + } |
|
112 | + if (!is_array($this->custom)) { |
|
113 | + $this->custom = array_filter((array) $this->custom); |
|
114 | + } |
|
115 | + $this->custom[$key] = $value; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * @param mixed $key |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function offsetUnset($key) |
|
123 | - { |
|
124 | - $this->offsetSet($key, null); |
|
125 | - } |
|
118 | + /** |
|
119 | + * @param mixed $key |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function offsetUnset($key) |
|
123 | + { |
|
124 | + $this->offsetSet($key, null); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * @return void |
|
129 | - */ |
|
130 | - public function render() |
|
131 | - { |
|
132 | - echo $this->build(); |
|
133 | - } |
|
127 | + /** |
|
128 | + * @return void |
|
129 | + */ |
|
130 | + public function render() |
|
131 | + { |
|
132 | + echo $this->build(); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * @return bool |
|
137 | - */ |
|
138 | - protected function isModified(array $properties) |
|
139 | - { |
|
140 | - return $this->date != $properties['date'] |
|
141 | - || $this->content != $properties['content'] |
|
142 | - || $this->title != $properties['title']; |
|
143 | - } |
|
135 | + /** |
|
136 | + * @return bool |
|
137 | + */ |
|
138 | + protected function isModified(array $properties) |
|
139 | + { |
|
140 | + return $this->date != $properties['date'] |
|
141 | + || $this->content != $properties['content'] |
|
142 | + || $this->title != $properties['title']; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * @return void |
|
147 | - */ |
|
148 | - protected function setProperties(WP_Post $post) |
|
149 | - { |
|
150 | - $defaults = [ |
|
151 | - 'author' => __('Anonymous', 'site-reviews'), |
|
152 | - 'date' => '', |
|
153 | - 'review_id' => '', |
|
154 | - 'review_type' => 'local', |
|
155 | - ]; |
|
156 | - $meta = array_filter( |
|
157 | - array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
|
158 | - 'strlen' |
|
159 | - ); |
|
160 | - $meta = array_merge($defaults, Arr::unprefixArrayKeys($meta)); |
|
161 | - $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
|
162 | - $this->modified = $this->isModified($properties); |
|
163 | - array_walk($properties, function ($value, $key) { |
|
164 | - if (!property_exists($this, $key) || isset($this->$key)) { |
|
165 | - return; |
|
166 | - } |
|
167 | - $this->$key = maybe_unserialize($value); |
|
168 | - }); |
|
169 | - } |
|
145 | + /** |
|
146 | + * @return void |
|
147 | + */ |
|
148 | + protected function setProperties(WP_Post $post) |
|
149 | + { |
|
150 | + $defaults = [ |
|
151 | + 'author' => __('Anonymous', 'site-reviews'), |
|
152 | + 'date' => '', |
|
153 | + 'review_id' => '', |
|
154 | + 'review_type' => 'local', |
|
155 | + ]; |
|
156 | + $meta = array_filter( |
|
157 | + array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
|
158 | + 'strlen' |
|
159 | + ); |
|
160 | + $meta = array_merge($defaults, Arr::unprefixArrayKeys($meta)); |
|
161 | + $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
|
162 | + $this->modified = $this->isModified($properties); |
|
163 | + array_walk($properties, function ($value, $key) { |
|
164 | + if (!property_exists($this, $key) || isset($this->$key)) { |
|
165 | + return; |
|
166 | + } |
|
167 | + $this->$key = maybe_unserialize($value); |
|
168 | + }); |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * @return void |
|
173 | - */ |
|
174 | - protected function setTermIds(WP_Post $post) |
|
175 | - { |
|
176 | - $this->term_ids = []; |
|
177 | - if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
|
178 | - return; |
|
179 | - } |
|
180 | - foreach ($terms as $term) { |
|
181 | - $this->term_ids[] = $term->term_id; |
|
182 | - } |
|
183 | - } |
|
171 | + /** |
|
172 | + * @return void |
|
173 | + */ |
|
174 | + protected function setTermIds(WP_Post $post) |
|
175 | + { |
|
176 | + $this->term_ids = []; |
|
177 | + if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
|
178 | + return; |
|
179 | + } |
|
180 | + foreach ($terms as $term) { |
|
181 | + $this->term_ids[] = $term->term_id; |
|
182 | + } |
|
183 | + } |
|
184 | 184 | } |
@@ -4,84 +4,84 @@ |
||
4 | 4 | |
5 | 5 | class SiteReviewsPopup extends TinymcePopupGenerator |
6 | 6 | { |
7 | - /** |
|
8 | - * @return array |
|
9 | - */ |
|
10 | - public function fields() |
|
11 | - { |
|
12 | - return [[ |
|
13 | - 'html' => sprintf('<p class="strong">%s</p>', esc_html__('All settings are optional.', 'site-reviews')), |
|
14 | - 'minWidth' => 320, |
|
15 | - 'type' => 'container', |
|
16 | - ], [ |
|
17 | - 'label' => esc_html__('Title', 'site-reviews'), |
|
18 | - 'name' => 'title', |
|
19 | - 'tooltip' => __('Enter a custom shortcode heading.', 'site-reviews'), |
|
20 | - 'type' => 'textbox', |
|
21 | - ], [ |
|
22 | - 'label' => esc_html__('Display', 'site-reviews'), |
|
23 | - 'maxLength' => 5, |
|
24 | - 'name' => 'display', |
|
25 | - 'size' => 3, |
|
26 | - 'text' => '10', |
|
27 | - 'tooltip' => __('How many reviews would you like to display (default: 10)?', 'site-reviews'), |
|
28 | - 'type' => 'textbox', |
|
29 | - ], [ |
|
30 | - 'label' => esc_html__('Rating', 'site-reviews'), |
|
31 | - 'name' => 'rating', |
|
32 | - 'options' => [ |
|
33 | - '5' => esc_html(sprintf(_n('%s star', '%s stars', 5, 'site-reviews'), 5)), |
|
34 | - '4' => esc_html(sprintf(_n('%s star', '%s stars', 4, 'site-reviews'), 4)), |
|
35 | - '3' => esc_html(sprintf(_n('%s star', '%s stars', 3, 'site-reviews'), 3)), |
|
36 | - '2' => esc_html(sprintf(_n('%s star', '%s stars', 2, 'site-reviews'), 2)), |
|
37 | - '1' => esc_html(sprintf(_n('%s star', '%s stars', 1, 'site-reviews'), 1)), |
|
38 | - '0' => esc_html(__('Unrated', 'site-reviews')), |
|
39 | - ], |
|
40 | - 'tooltip' => __('What is the minimum rating to display (default: 1 star)?', 'site-reviews'), |
|
41 | - 'type' => 'listbox', |
|
42 | - ], [ |
|
43 | - 'label' => esc_html__('Pagination', 'site-reviews'), |
|
44 | - 'name' => 'pagination', |
|
45 | - 'options' => [ |
|
46 | - 'true' => esc_html__('Enable', 'site-reviews'), |
|
47 | - 'ajax' => esc_html__('Enable (using ajax)', 'site-reviews'), |
|
48 | - 'false' => esc_html__('Disable', 'site-reviews'), |
|
49 | - ], |
|
50 | - 'tooltip' => __('When using pagination this shortcode can only be used once on a page. (default: disable)', 'site-reviews'), |
|
51 | - 'type' => 'listbox', |
|
52 | - ], |
|
53 | - $this->getTypes(__('Which type of review would you like to display?', 'site-reviews')), |
|
54 | - $this->getCategories(__('Limit reviews to this category.', 'site-reviews')), |
|
55 | - [ |
|
56 | - 'label' => esc_html__('Assigned To', 'site-reviews'), |
|
57 | - 'name' => 'assigned_to', |
|
58 | - 'tooltip' => __('Limit reviews to those assigned to this post ID (separate multiple IDs with a comma). You can also enter "post_id" to use the ID of the current page, or "parent_id" to use the ID of the parent page.', 'site-reviews'), |
|
59 | - 'type' => 'textbox', |
|
60 | - ], [ |
|
61 | - 'label' => esc_html__('Schema', 'site-reviews'), |
|
62 | - 'name' => 'schema', |
|
63 | - 'options' => [ |
|
64 | - 'true' => esc_html__('Enable rich snippets', 'site-reviews'), |
|
65 | - 'false' => esc_html__('Disable rich snippets', 'site-reviews'), |
|
66 | - ], |
|
67 | - 'tooltip' => __('Rich snippets are disabled by default.', 'site-reviews'), |
|
68 | - 'type' => 'listbox', |
|
69 | - ], [ |
|
70 | - 'label' => esc_html__('Classes', 'site-reviews'), |
|
71 | - 'name' => 'class', |
|
72 | - 'tooltip' => __('Add custom CSS classes to the shortcode.', 'site-reviews'), |
|
73 | - 'type' => 'textbox', |
|
74 | - ], [ |
|
75 | - 'columns' => 2, |
|
76 | - 'items' => $this->getHideOptions(), |
|
77 | - 'label' => esc_html__('Hide', 'site-reviews'), |
|
78 | - 'layout' => 'grid', |
|
79 | - 'spacing' => 5, |
|
80 | - 'type' => 'container', |
|
81 | - ], [ |
|
82 | - 'hidden' => true, |
|
83 | - 'name' => 'id', |
|
84 | - 'type' => 'textbox', |
|
85 | - ], ]; |
|
86 | - } |
|
7 | + /** |
|
8 | + * @return array |
|
9 | + */ |
|
10 | + public function fields() |
|
11 | + { |
|
12 | + return [[ |
|
13 | + 'html' => sprintf('<p class="strong">%s</p>', esc_html__('All settings are optional.', 'site-reviews')), |
|
14 | + 'minWidth' => 320, |
|
15 | + 'type' => 'container', |
|
16 | + ], [ |
|
17 | + 'label' => esc_html__('Title', 'site-reviews'), |
|
18 | + 'name' => 'title', |
|
19 | + 'tooltip' => __('Enter a custom shortcode heading.', 'site-reviews'), |
|
20 | + 'type' => 'textbox', |
|
21 | + ], [ |
|
22 | + 'label' => esc_html__('Display', 'site-reviews'), |
|
23 | + 'maxLength' => 5, |
|
24 | + 'name' => 'display', |
|
25 | + 'size' => 3, |
|
26 | + 'text' => '10', |
|
27 | + 'tooltip' => __('How many reviews would you like to display (default: 10)?', 'site-reviews'), |
|
28 | + 'type' => 'textbox', |
|
29 | + ], [ |
|
30 | + 'label' => esc_html__('Rating', 'site-reviews'), |
|
31 | + 'name' => 'rating', |
|
32 | + 'options' => [ |
|
33 | + '5' => esc_html(sprintf(_n('%s star', '%s stars', 5, 'site-reviews'), 5)), |
|
34 | + '4' => esc_html(sprintf(_n('%s star', '%s stars', 4, 'site-reviews'), 4)), |
|
35 | + '3' => esc_html(sprintf(_n('%s star', '%s stars', 3, 'site-reviews'), 3)), |
|
36 | + '2' => esc_html(sprintf(_n('%s star', '%s stars', 2, 'site-reviews'), 2)), |
|
37 | + '1' => esc_html(sprintf(_n('%s star', '%s stars', 1, 'site-reviews'), 1)), |
|
38 | + '0' => esc_html(__('Unrated', 'site-reviews')), |
|
39 | + ], |
|
40 | + 'tooltip' => __('What is the minimum rating to display (default: 1 star)?', 'site-reviews'), |
|
41 | + 'type' => 'listbox', |
|
42 | + ], [ |
|
43 | + 'label' => esc_html__('Pagination', 'site-reviews'), |
|
44 | + 'name' => 'pagination', |
|
45 | + 'options' => [ |
|
46 | + 'true' => esc_html__('Enable', 'site-reviews'), |
|
47 | + 'ajax' => esc_html__('Enable (using ajax)', 'site-reviews'), |
|
48 | + 'false' => esc_html__('Disable', 'site-reviews'), |
|
49 | + ], |
|
50 | + 'tooltip' => __('When using pagination this shortcode can only be used once on a page. (default: disable)', 'site-reviews'), |
|
51 | + 'type' => 'listbox', |
|
52 | + ], |
|
53 | + $this->getTypes(__('Which type of review would you like to display?', 'site-reviews')), |
|
54 | + $this->getCategories(__('Limit reviews to this category.', 'site-reviews')), |
|
55 | + [ |
|
56 | + 'label' => esc_html__('Assigned To', 'site-reviews'), |
|
57 | + 'name' => 'assigned_to', |
|
58 | + 'tooltip' => __('Limit reviews to those assigned to this post ID (separate multiple IDs with a comma). You can also enter "post_id" to use the ID of the current page, or "parent_id" to use the ID of the parent page.', 'site-reviews'), |
|
59 | + 'type' => 'textbox', |
|
60 | + ], [ |
|
61 | + 'label' => esc_html__('Schema', 'site-reviews'), |
|
62 | + 'name' => 'schema', |
|
63 | + 'options' => [ |
|
64 | + 'true' => esc_html__('Enable rich snippets', 'site-reviews'), |
|
65 | + 'false' => esc_html__('Disable rich snippets', 'site-reviews'), |
|
66 | + ], |
|
67 | + 'tooltip' => __('Rich snippets are disabled by default.', 'site-reviews'), |
|
68 | + 'type' => 'listbox', |
|
69 | + ], [ |
|
70 | + 'label' => esc_html__('Classes', 'site-reviews'), |
|
71 | + 'name' => 'class', |
|
72 | + 'tooltip' => __('Add custom CSS classes to the shortcode.', 'site-reviews'), |
|
73 | + 'type' => 'textbox', |
|
74 | + ], [ |
|
75 | + 'columns' => 2, |
|
76 | + 'items' => $this->getHideOptions(), |
|
77 | + 'label' => esc_html__('Hide', 'site-reviews'), |
|
78 | + 'layout' => 'grid', |
|
79 | + 'spacing' => 5, |
|
80 | + 'type' => 'container', |
|
81 | + ], [ |
|
82 | + 'hidden' => true, |
|
83 | + 'name' => 'id', |
|
84 | + 'type' => 'textbox', |
|
85 | + ], ]; |
|
86 | + } |
|
87 | 87 | } |
@@ -8,97 +8,97 @@ |
||
8 | 8 | |
9 | 9 | class WelcomeController extends Controller |
10 | 10 | { |
11 | - /** |
|
12 | - * @return array |
|
13 | - * @filter plugin_action_links_site-reviews/site-reviews.php |
|
14 | - */ |
|
15 | - public function filterActionLinks(array $links) |
|
16 | - { |
|
17 | - $links['welcome'] = glsr(Builder::class)->a(__('About', 'site-reviews'), [ |
|
18 | - 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=welcome'), |
|
19 | - ]); |
|
20 | - return $links; |
|
21 | - } |
|
11 | + /** |
|
12 | + * @return array |
|
13 | + * @filter plugin_action_links_site-reviews/site-reviews.php |
|
14 | + */ |
|
15 | + public function filterActionLinks(array $links) |
|
16 | + { |
|
17 | + $links['welcome'] = glsr(Builder::class)->a(__('About', 'site-reviews'), [ |
|
18 | + 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=welcome'), |
|
19 | + ]); |
|
20 | + return $links; |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * @return string |
|
25 | - * @filter admin_title |
|
26 | - */ |
|
27 | - public function filterAdminTitle($title) |
|
28 | - { |
|
29 | - return Application::POST_TYPE.'_page_welcome' == glsr_current_screen()->id |
|
30 | - ? sprintf(__('Welcome to %s — WordPress', 'site-reviews'), glsr()->name) |
|
31 | - : $title; |
|
32 | - } |
|
23 | + /** |
|
24 | + * @return string |
|
25 | + * @filter admin_title |
|
26 | + */ |
|
27 | + public function filterAdminTitle($title) |
|
28 | + { |
|
29 | + return Application::POST_TYPE.'_page_welcome' == glsr_current_screen()->id |
|
30 | + ? sprintf(__('Welcome to %s — WordPress', 'site-reviews'), glsr()->name) |
|
31 | + : $title; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * @param string $text |
|
36 | - * @return string |
|
37 | - * @filter admin_footer_text |
|
38 | - */ |
|
39 | - public function filterFooterText($text) |
|
40 | - { |
|
41 | - if (Application::POST_TYPE.'_page_welcome' != glsr_current_screen()->id) { |
|
42 | - return $text; |
|
43 | - } |
|
44 | - $url = 'https://wordpress.org/support/view/plugin-reviews/site-reviews?filter=5#new-post'; |
|
45 | - return wp_kses_post(sprintf( |
|
46 | - __('Please rate %s on %s and help us spread the word. Thank you so much!', 'site-reviews'), |
|
47 | - '<strong>'.glsr()->name.'</strong> <a href="'.$url.'" target="_blank">★★★★★</a>', |
|
48 | - '<a href="'.$url.'" target="_blank">wordpress.org</a>' |
|
49 | - )); |
|
50 | - } |
|
34 | + /** |
|
35 | + * @param string $text |
|
36 | + * @return string |
|
37 | + * @filter admin_footer_text |
|
38 | + */ |
|
39 | + public function filterFooterText($text) |
|
40 | + { |
|
41 | + if (Application::POST_TYPE.'_page_welcome' != glsr_current_screen()->id) { |
|
42 | + return $text; |
|
43 | + } |
|
44 | + $url = 'https://wordpress.org/support/view/plugin-reviews/site-reviews?filter=5#new-post'; |
|
45 | + return wp_kses_post(sprintf( |
|
46 | + __('Please rate %s on %s and help us spread the word. Thank you so much!', 'site-reviews'), |
|
47 | + '<strong>'.glsr()->name.'</strong> <a href="'.$url.'" target="_blank">★★★★★</a>', |
|
48 | + '<a href="'.$url.'" target="_blank">wordpress.org</a>' |
|
49 | + )); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param string $plugin |
|
54 | - * @param bool $isNetworkActivation |
|
55 | - * @return void |
|
56 | - * @action activated_plugin |
|
57 | - */ |
|
58 | - public function redirectOnActivation($plugin, $isNetworkActivation) |
|
59 | - { |
|
60 | - if (!$isNetworkActivation |
|
61 | - && 'cli' !== php_sapi_name() |
|
62 | - && $plugin === plugin_basename(glsr()->file)) { |
|
63 | - wp_safe_redirect(admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=welcome')); |
|
64 | - exit; |
|
65 | - } |
|
66 | - } |
|
52 | + /** |
|
53 | + * @param string $plugin |
|
54 | + * @param bool $isNetworkActivation |
|
55 | + * @return void |
|
56 | + * @action activated_plugin |
|
57 | + */ |
|
58 | + public function redirectOnActivation($plugin, $isNetworkActivation) |
|
59 | + { |
|
60 | + if (!$isNetworkActivation |
|
61 | + && 'cli' !== php_sapi_name() |
|
62 | + && $plugin === plugin_basename(glsr()->file)) { |
|
63 | + wp_safe_redirect(admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=welcome')); |
|
64 | + exit; |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return void |
|
70 | - * @action admin_menu |
|
71 | - */ |
|
72 | - public function registerPage() |
|
73 | - { |
|
74 | - add_submenu_page('edit.php?post_type='.Application::POST_TYPE, |
|
75 | - sprintf(__('Welcome to %s', 'site-reviews'), glsr()->name), |
|
76 | - glsr()->name, |
|
77 | - glsr()->getPermission('welcome'), |
|
78 | - 'welcome', |
|
79 | - [$this, 'renderPage'] |
|
80 | - ); |
|
81 | - remove_submenu_page('edit.php?post_type='.Application::POST_TYPE, 'welcome'); |
|
82 | - } |
|
68 | + /** |
|
69 | + * @return void |
|
70 | + * @action admin_menu |
|
71 | + */ |
|
72 | + public function registerPage() |
|
73 | + { |
|
74 | + add_submenu_page('edit.php?post_type='.Application::POST_TYPE, |
|
75 | + sprintf(__('Welcome to %s', 'site-reviews'), glsr()->name), |
|
76 | + glsr()->name, |
|
77 | + glsr()->getPermission('welcome'), |
|
78 | + 'welcome', |
|
79 | + [$this, 'renderPage'] |
|
80 | + ); |
|
81 | + remove_submenu_page('edit.php?post_type='.Application::POST_TYPE, 'welcome'); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @return void |
|
86 | - * @see $this->registerPage() |
|
87 | - * @callback add_submenu_page |
|
88 | - */ |
|
89 | - public function renderPage() |
|
90 | - { |
|
91 | - $tabs = apply_filters('site-reviews/addon/welcome/tabs', [ |
|
92 | - 'getting-started' => __('Getting Started', 'site-reviews'), |
|
93 | - 'whatsnew' => __('What\'s New', 'site-reviews'), |
|
94 | - 'upgrade-guide' => __('Upgrade Guide', 'site-reviews'), |
|
95 | - 'support' => __('Support', 'site-reviews'), |
|
96 | - ]); |
|
97 | - glsr()->render('pages/welcome/index', [ |
|
98 | - 'data' => ['context' => []], |
|
99 | - 'http_referer' => (string) wp_get_referer(), |
|
100 | - 'tabs' => $tabs, |
|
101 | - 'template' => glsr(Template::class), |
|
102 | - ]); |
|
103 | - } |
|
84 | + /** |
|
85 | + * @return void |
|
86 | + * @see $this->registerPage() |
|
87 | + * @callback add_submenu_page |
|
88 | + */ |
|
89 | + public function renderPage() |
|
90 | + { |
|
91 | + $tabs = apply_filters('site-reviews/addon/welcome/tabs', [ |
|
92 | + 'getting-started' => __('Getting Started', 'site-reviews'), |
|
93 | + 'whatsnew' => __('What\'s New', 'site-reviews'), |
|
94 | + 'upgrade-guide' => __('Upgrade Guide', 'site-reviews'), |
|
95 | + 'support' => __('Support', 'site-reviews'), |
|
96 | + ]); |
|
97 | + glsr()->render('pages/welcome/index', [ |
|
98 | + 'data' => ['context' => []], |
|
99 | + 'http_referer' => (string) wp_get_referer(), |
|
100 | + 'tabs' => $tabs, |
|
101 | + 'template' => glsr(Template::class), |
|
102 | + ]); |
|
103 | + } |
|
104 | 104 | } |
@@ -6,41 +6,41 @@ |
||
6 | 6 | |
7 | 7 | class SiteReviewsDefaults extends Defaults |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - protected $guarded = [ |
|
13 | - 'fallback', |
|
14 | - 'title', |
|
15 | - ]; |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + protected $guarded = [ |
|
13 | + 'fallback', |
|
14 | + 'title', |
|
15 | + ]; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - protected $mapped = [ |
|
21 | - 'count' => 'display', // @deprecated since v4.1.0 |
|
22 | - 'per_page' => 'display', |
|
23 | - ]; |
|
17 | + /** |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + protected $mapped = [ |
|
21 | + 'count' => 'display', // @deprecated since v4.1.0 |
|
22 | + 'per_page' => 'display', |
|
23 | + ]; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @return array |
|
27 | - */ |
|
28 | - protected function defaults() |
|
29 | - { |
|
30 | - return [ |
|
31 | - 'assigned_to' => '', |
|
32 | - 'category' => '', |
|
33 | - 'class' => '', |
|
34 | - 'display' => 5, |
|
35 | - 'fallback' => '', |
|
36 | - 'hide' => [], |
|
37 | - 'id' => '', |
|
38 | - 'offset' => '', |
|
39 | - 'pagination' => false, |
|
40 | - 'rating' => 0, |
|
41 | - 'schema' => false, |
|
42 | - 'title' => '', |
|
43 | - 'type' => 'local', |
|
44 | - ]; |
|
45 | - } |
|
25 | + /** |
|
26 | + * @return array |
|
27 | + */ |
|
28 | + protected function defaults() |
|
29 | + { |
|
30 | + return [ |
|
31 | + 'assigned_to' => '', |
|
32 | + 'category' => '', |
|
33 | + 'class' => '', |
|
34 | + 'display' => 5, |
|
35 | + 'fallback' => '', |
|
36 | + 'hide' => [], |
|
37 | + 'id' => '', |
|
38 | + 'offset' => '', |
|
39 | + 'pagination' => false, |
|
40 | + 'rating' => 0, |
|
41 | + 'schema' => false, |
|
42 | + 'title' => '', |
|
43 | + 'type' => 'local', |
|
44 | + ]; |
|
45 | + } |
|
46 | 46 | } |
@@ -6,31 +6,31 @@ |
||
6 | 6 | |
7 | 7 | class ReviewsDefaults extends Defaults |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - protected $mapped = [ |
|
13 | - 'count' => 'per_page', // @deprecated since v4.1.0 |
|
14 | - 'display' => 'per_page', |
|
15 | - ]; |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + protected $mapped = [ |
|
13 | + 'count' => 'per_page', // @deprecated since v4.1.0 |
|
14 | + 'display' => 'per_page', |
|
15 | + ]; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return array |
|
19 | - */ |
|
20 | - protected function defaults() |
|
21 | - { |
|
22 | - return [ |
|
23 | - 'assigned_to' => '', |
|
24 | - 'category' => '', |
|
25 | - 'offset' => '', |
|
26 | - 'order' => 'DESC', |
|
27 | - 'orderby' => 'date', |
|
28 | - 'pagination' => false, |
|
29 | - 'per_page' => 10, |
|
30 | - 'post__in' => [], |
|
31 | - 'post__not_in' => [], |
|
32 | - 'rating' => '', |
|
33 | - 'type' => '', |
|
34 | - ]; |
|
35 | - } |
|
17 | + /** |
|
18 | + * @return array |
|
19 | + */ |
|
20 | + protected function defaults() |
|
21 | + { |
|
22 | + return [ |
|
23 | + 'assigned_to' => '', |
|
24 | + 'category' => '', |
|
25 | + 'offset' => '', |
|
26 | + 'order' => 'DESC', |
|
27 | + 'orderby' => 'date', |
|
28 | + 'pagination' => false, |
|
29 | + 'per_page' => 10, |
|
30 | + 'post__in' => [], |
|
31 | + 'post__not_in' => [], |
|
32 | + 'rating' => '', |
|
33 | + 'type' => '', |
|
34 | + ]; |
|
35 | + } |
|
36 | 36 | } |
@@ -8,137 +8,137 @@ |
||
8 | 8 | |
9 | 9 | abstract class DefaultsAbstract |
10 | 10 | { |
11 | - /** |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected $callable = [ |
|
15 | - 'defaults', 'filter', 'merge', 'restrict', 'unguarded', |
|
16 | - ]; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var array |
|
20 | - */ |
|
21 | - protected $guarded = []; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $mapped = []; |
|
27 | - |
|
28 | - /** |
|
29 | - * @param string $name |
|
30 | - * @return void|array |
|
31 | - */ |
|
32 | - public function __call($name, array $args = []) |
|
33 | - { |
|
34 | - if (!method_exists($this, $name) || !in_array($name, $this->callable)) { |
|
35 | - return; |
|
36 | - } |
|
37 | - $args[0] = $this->mapKeys(Arr::get($args, 0, [])); |
|
38 | - $defaults = call_user_func_array([$this, $name], $args); |
|
39 | - $hookName = (new ReflectionClass($this))->getShortName(); |
|
40 | - $hookName = str_replace('Defaults', '', $hookName); |
|
41 | - $hookName = Str::dashCase($hookName); |
|
42 | - return apply_filters('site-reviews/defaults/'.$hookName, $defaults, $name); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - abstract protected function defaults(); |
|
49 | - |
|
50 | - /** |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - protected function filter(array $values = []) |
|
54 | - { |
|
55 | - return $this->normalize($this->merge(array_filter($values)), $values); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - protected function filteredJson(array $values = []) |
|
62 | - { |
|
63 | - $defaults = $this->flattenArray( |
|
64 | - array_diff_key($this->defaults(), array_flip($this->guarded)) |
|
65 | - ); |
|
66 | - $values = $this->flattenArray( |
|
67 | - shortcode_atts($defaults, $values) |
|
68 | - ); |
|
69 | - $filtered = array_filter(array_diff_assoc($values, $defaults), function ($value) { |
|
70 | - return !$this->isEmpty($value); |
|
71 | - }); |
|
72 | - return json_encode($filtered, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @return array |
|
77 | - */ |
|
78 | - protected function flattenArray(array $values) |
|
79 | - { |
|
80 | - array_walk($values, function (&$value) { |
|
81 | - if (!is_array($value)) { |
|
82 | - return; |
|
83 | - } |
|
84 | - $value = implode(',', $value); |
|
85 | - }); |
|
86 | - return $values; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param mixed $var |
|
91 | - * @return bool |
|
92 | - */ |
|
93 | - protected function isEmpty($var) |
|
94 | - { |
|
95 | - return !is_numeric($var) && !is_bool($var) && empty($var); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @return array |
|
100 | - */ |
|
101 | - protected function mapKeys(array $args) |
|
102 | - { |
|
103 | - foreach ($this->mapped as $old => $new) { |
|
104 | - if (array_key_exists($old, $args)) { |
|
105 | - $args[$new] = $args[$old]; |
|
106 | - unset($args[$old]); |
|
107 | - } |
|
108 | - } |
|
109 | - return $args; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return array |
|
114 | - */ |
|
115 | - protected function merge(array $values = []) |
|
116 | - { |
|
117 | - return $this->normalize(wp_parse_args($values, $this->defaults()), $values); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return array |
|
122 | - */ |
|
123 | - protected function normalize(array $values, array $originalValues) |
|
124 | - { |
|
125 | - $values['json'] = $this->filteredJson($originalValues); |
|
126 | - return $values; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - protected function restrict(array $values = []) |
|
133 | - { |
|
134 | - return $this->normalize(shortcode_atts($this->defaults(), $values), $values); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - protected function unguarded() |
|
141 | - { |
|
142 | - return array_diff_key($this->defaults(), array_flip($this->guarded)); |
|
143 | - } |
|
11 | + /** |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected $callable = [ |
|
15 | + 'defaults', 'filter', 'merge', 'restrict', 'unguarded', |
|
16 | + ]; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var array |
|
20 | + */ |
|
21 | + protected $guarded = []; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $mapped = []; |
|
27 | + |
|
28 | + /** |
|
29 | + * @param string $name |
|
30 | + * @return void|array |
|
31 | + */ |
|
32 | + public function __call($name, array $args = []) |
|
33 | + { |
|
34 | + if (!method_exists($this, $name) || !in_array($name, $this->callable)) { |
|
35 | + return; |
|
36 | + } |
|
37 | + $args[0] = $this->mapKeys(Arr::get($args, 0, [])); |
|
38 | + $defaults = call_user_func_array([$this, $name], $args); |
|
39 | + $hookName = (new ReflectionClass($this))->getShortName(); |
|
40 | + $hookName = str_replace('Defaults', '', $hookName); |
|
41 | + $hookName = Str::dashCase($hookName); |
|
42 | + return apply_filters('site-reviews/defaults/'.$hookName, $defaults, $name); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + abstract protected function defaults(); |
|
49 | + |
|
50 | + /** |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + protected function filter(array $values = []) |
|
54 | + { |
|
55 | + return $this->normalize($this->merge(array_filter($values)), $values); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + protected function filteredJson(array $values = []) |
|
62 | + { |
|
63 | + $defaults = $this->flattenArray( |
|
64 | + array_diff_key($this->defaults(), array_flip($this->guarded)) |
|
65 | + ); |
|
66 | + $values = $this->flattenArray( |
|
67 | + shortcode_atts($defaults, $values) |
|
68 | + ); |
|
69 | + $filtered = array_filter(array_diff_assoc($values, $defaults), function ($value) { |
|
70 | + return !$this->isEmpty($value); |
|
71 | + }); |
|
72 | + return json_encode($filtered, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @return array |
|
77 | + */ |
|
78 | + protected function flattenArray(array $values) |
|
79 | + { |
|
80 | + array_walk($values, function (&$value) { |
|
81 | + if (!is_array($value)) { |
|
82 | + return; |
|
83 | + } |
|
84 | + $value = implode(',', $value); |
|
85 | + }); |
|
86 | + return $values; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param mixed $var |
|
91 | + * @return bool |
|
92 | + */ |
|
93 | + protected function isEmpty($var) |
|
94 | + { |
|
95 | + return !is_numeric($var) && !is_bool($var) && empty($var); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @return array |
|
100 | + */ |
|
101 | + protected function mapKeys(array $args) |
|
102 | + { |
|
103 | + foreach ($this->mapped as $old => $new) { |
|
104 | + if (array_key_exists($old, $args)) { |
|
105 | + $args[$new] = $args[$old]; |
|
106 | + unset($args[$old]); |
|
107 | + } |
|
108 | + } |
|
109 | + return $args; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return array |
|
114 | + */ |
|
115 | + protected function merge(array $values = []) |
|
116 | + { |
|
117 | + return $this->normalize(wp_parse_args($values, $this->defaults()), $values); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return array |
|
122 | + */ |
|
123 | + protected function normalize(array $values, array $originalValues) |
|
124 | + { |
|
125 | + $values['json'] = $this->filteredJson($originalValues); |
|
126 | + return $values; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + protected function restrict(array $values = []) |
|
133 | + { |
|
134 | + return $this->normalize(shortcode_atts($this->defaults(), $values), $values); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + protected function unguarded() |
|
141 | + { |
|
142 | + return array_diff_key($this->defaults(), array_flip($this->guarded)); |
|
143 | + } |
|
144 | 144 | } |
@@ -11,226 +11,226 @@ |
||
11 | 11 | |
12 | 12 | abstract class Container |
13 | 13 | { |
14 | - const PROTECTED_PROPERTIES = [ |
|
15 | - 'instance', |
|
16 | - 'services', |
|
17 | - 'session', |
|
18 | - 'storage', |
|
19 | - ]; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var static |
|
23 | - */ |
|
24 | - protected static $instance; |
|
25 | - |
|
26 | - /** |
|
27 | - * The container's bound services. |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $services = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - protected $session = []; |
|
36 | - |
|
37 | - /** |
|
38 | - * The container's storage items. |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - protected $storage = []; |
|
42 | - |
|
43 | - /** |
|
44 | - * @return static |
|
45 | - */ |
|
46 | - public static function load() |
|
47 | - { |
|
48 | - if (empty(static::$instance)) { |
|
49 | - static::$instance = new static(); |
|
50 | - } |
|
51 | - return static::$instance; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @param string $property |
|
56 | - * @return mixed |
|
57 | - */ |
|
58 | - public function __get($property) |
|
59 | - { |
|
60 | - if (property_exists($this, $property) && !in_array($property, static::PROTECTED_PROPERTIES)) { |
|
61 | - return $this->$property; |
|
62 | - } |
|
63 | - $constant = 'static::'.strtoupper(Str::snakeCase($property)); |
|
64 | - if (defined($constant)) { |
|
65 | - return constant($constant); |
|
66 | - } |
|
67 | - return Arr::get($this->storage, $property, null); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string $property |
|
72 | - * @param string $value |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function __set($property, $value) |
|
76 | - { |
|
77 | - if (!property_exists($this, $property) || in_array($property, static::PROTECTED_PROPERTIES)) { |
|
78 | - $this->storage[$property] = $value; |
|
79 | - } elseif (!isset($this->$property)) { |
|
80 | - $this->$property = $value; |
|
81 | - } else { |
|
82 | - throw new Exception(sprintf('The "%s" property cannot be changed once set.', $property)); |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Bind a service to the container. |
|
88 | - * @param string $alias |
|
89 | - * @param mixed $concrete |
|
90 | - * @return mixed |
|
91 | - */ |
|
92 | - public function bind($alias, $concrete) |
|
93 | - { |
|
94 | - $this->services[$alias] = $concrete; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Request a service from the container. |
|
99 | - * @param mixed $abstract |
|
100 | - * @return mixed |
|
101 | - */ |
|
102 | - public function make($abstract) |
|
103 | - { |
|
104 | - if (!isset($this->services[$abstract])) { |
|
105 | - $abstract = $this->addNamespace($abstract); |
|
106 | - } |
|
107 | - if (isset($this->services[$abstract])) { |
|
108 | - $abstract = $this->services[$abstract]; |
|
109 | - } |
|
110 | - if (is_callable($abstract)) { |
|
111 | - return call_user_func_array($abstract, [$this]); |
|
112 | - } |
|
113 | - if (is_object($abstract)) { |
|
114 | - return $abstract; |
|
115 | - } |
|
116 | - return $this->resolve($abstract); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function sessionClear() |
|
123 | - { |
|
124 | - $this->session = []; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @return mixed |
|
129 | - */ |
|
130 | - public function sessionGet($key, $fallback = '') |
|
131 | - { |
|
132 | - $value = Arr::get($this->session, $key, $fallback); |
|
133 | - unset($this->session[$key]); |
|
134 | - return $value; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return void |
|
139 | - */ |
|
140 | - public function sessionSet($key, $value) |
|
141 | - { |
|
142 | - $this->session[$key] = $value; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Bind a singleton instance to the container. |
|
147 | - * @param string $alias |
|
148 | - * @param callable|string|null $binding |
|
149 | - * @return void |
|
150 | - */ |
|
151 | - public function singleton($alias, $binding) |
|
152 | - { |
|
153 | - $this->bind($alias, $this->make($binding)); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Prefix the current namespace to the abstract if absent. |
|
158 | - * @param string $abstract |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - protected function addNamespace($abstract) |
|
162 | - { |
|
163 | - if (!Str::contains($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
164 | - $abstract = __NAMESPACE__.'\\'.$abstract; |
|
165 | - } |
|
166 | - return $abstract; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Resolve a service from the container. |
|
171 | - * @param mixed $concrete |
|
172 | - * @return mixed |
|
173 | - * @throws Exception |
|
174 | - */ |
|
175 | - protected function resolve($concrete) |
|
176 | - { |
|
177 | - if ($concrete instanceof Closure) { |
|
178 | - return $concrete($this); |
|
179 | - } |
|
180 | - $reflector = new ReflectionClass($concrete); |
|
181 | - if (!$reflector->isInstantiable()) { |
|
182 | - throw new Exception('Target ['.$concrete.'] is not instantiable.'); |
|
183 | - } |
|
184 | - $constructor = $reflector->getConstructor(); |
|
185 | - if (empty($constructor)) { |
|
186 | - return new $concrete(); |
|
187 | - } |
|
188 | - return $reflector->newInstanceArgs( |
|
189 | - $this->resolveDependencies($constructor->getParameters()) |
|
190 | - ); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Resolve a class based dependency from the container. |
|
195 | - * @return mixed |
|
196 | - * @throws Exception |
|
197 | - */ |
|
198 | - protected function resolveClass(ReflectionParameter $parameter) |
|
199 | - { |
|
200 | - try { |
|
201 | - return $this->make($parameter->getClass()->name); |
|
202 | - } catch (Exception $error) { |
|
203 | - if ($parameter->isOptional()) { |
|
204 | - return $parameter->getDefaultValue(); |
|
205 | - } |
|
206 | - throw $error; |
|
207 | - } |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Resolve all of the dependencies from the ReflectionParameters. |
|
212 | - * @return array |
|
213 | - */ |
|
214 | - protected function resolveDependencies(array $dependencies) |
|
215 | - { |
|
216 | - $results = []; |
|
217 | - foreach ($dependencies as $dependency) { |
|
218 | - $results[] = !is_null($class = $dependency->getClass()) |
|
219 | - ? $this->resolveClass($dependency) |
|
220 | - : $this->resolveDependency($dependency); |
|
221 | - } |
|
222 | - return $results; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Resolve a single ReflectionParameter dependency. |
|
227 | - * @return array|null |
|
228 | - */ |
|
229 | - protected function resolveDependency(ReflectionParameter $parameter) |
|
230 | - { |
|
231 | - if ($parameter->isArray() && $parameter->isDefaultValueAvailable()) { |
|
232 | - return $parameter->getDefaultValue(); |
|
233 | - } |
|
234 | - return null; |
|
235 | - } |
|
14 | + const PROTECTED_PROPERTIES = [ |
|
15 | + 'instance', |
|
16 | + 'services', |
|
17 | + 'session', |
|
18 | + 'storage', |
|
19 | + ]; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var static |
|
23 | + */ |
|
24 | + protected static $instance; |
|
25 | + |
|
26 | + /** |
|
27 | + * The container's bound services. |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $services = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + protected $session = []; |
|
36 | + |
|
37 | + /** |
|
38 | + * The container's storage items. |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + protected $storage = []; |
|
42 | + |
|
43 | + /** |
|
44 | + * @return static |
|
45 | + */ |
|
46 | + public static function load() |
|
47 | + { |
|
48 | + if (empty(static::$instance)) { |
|
49 | + static::$instance = new static(); |
|
50 | + } |
|
51 | + return static::$instance; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @param string $property |
|
56 | + * @return mixed |
|
57 | + */ |
|
58 | + public function __get($property) |
|
59 | + { |
|
60 | + if (property_exists($this, $property) && !in_array($property, static::PROTECTED_PROPERTIES)) { |
|
61 | + return $this->$property; |
|
62 | + } |
|
63 | + $constant = 'static::'.strtoupper(Str::snakeCase($property)); |
|
64 | + if (defined($constant)) { |
|
65 | + return constant($constant); |
|
66 | + } |
|
67 | + return Arr::get($this->storage, $property, null); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string $property |
|
72 | + * @param string $value |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function __set($property, $value) |
|
76 | + { |
|
77 | + if (!property_exists($this, $property) || in_array($property, static::PROTECTED_PROPERTIES)) { |
|
78 | + $this->storage[$property] = $value; |
|
79 | + } elseif (!isset($this->$property)) { |
|
80 | + $this->$property = $value; |
|
81 | + } else { |
|
82 | + throw new Exception(sprintf('The "%s" property cannot be changed once set.', $property)); |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Bind a service to the container. |
|
88 | + * @param string $alias |
|
89 | + * @param mixed $concrete |
|
90 | + * @return mixed |
|
91 | + */ |
|
92 | + public function bind($alias, $concrete) |
|
93 | + { |
|
94 | + $this->services[$alias] = $concrete; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Request a service from the container. |
|
99 | + * @param mixed $abstract |
|
100 | + * @return mixed |
|
101 | + */ |
|
102 | + public function make($abstract) |
|
103 | + { |
|
104 | + if (!isset($this->services[$abstract])) { |
|
105 | + $abstract = $this->addNamespace($abstract); |
|
106 | + } |
|
107 | + if (isset($this->services[$abstract])) { |
|
108 | + $abstract = $this->services[$abstract]; |
|
109 | + } |
|
110 | + if (is_callable($abstract)) { |
|
111 | + return call_user_func_array($abstract, [$this]); |
|
112 | + } |
|
113 | + if (is_object($abstract)) { |
|
114 | + return $abstract; |
|
115 | + } |
|
116 | + return $this->resolve($abstract); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function sessionClear() |
|
123 | + { |
|
124 | + $this->session = []; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @return mixed |
|
129 | + */ |
|
130 | + public function sessionGet($key, $fallback = '') |
|
131 | + { |
|
132 | + $value = Arr::get($this->session, $key, $fallback); |
|
133 | + unset($this->session[$key]); |
|
134 | + return $value; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return void |
|
139 | + */ |
|
140 | + public function sessionSet($key, $value) |
|
141 | + { |
|
142 | + $this->session[$key] = $value; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Bind a singleton instance to the container. |
|
147 | + * @param string $alias |
|
148 | + * @param callable|string|null $binding |
|
149 | + * @return void |
|
150 | + */ |
|
151 | + public function singleton($alias, $binding) |
|
152 | + { |
|
153 | + $this->bind($alias, $this->make($binding)); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Prefix the current namespace to the abstract if absent. |
|
158 | + * @param string $abstract |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + protected function addNamespace($abstract) |
|
162 | + { |
|
163 | + if (!Str::contains($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
164 | + $abstract = __NAMESPACE__.'\\'.$abstract; |
|
165 | + } |
|
166 | + return $abstract; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Resolve a service from the container. |
|
171 | + * @param mixed $concrete |
|
172 | + * @return mixed |
|
173 | + * @throws Exception |
|
174 | + */ |
|
175 | + protected function resolve($concrete) |
|
176 | + { |
|
177 | + if ($concrete instanceof Closure) { |
|
178 | + return $concrete($this); |
|
179 | + } |
|
180 | + $reflector = new ReflectionClass($concrete); |
|
181 | + if (!$reflector->isInstantiable()) { |
|
182 | + throw new Exception('Target ['.$concrete.'] is not instantiable.'); |
|
183 | + } |
|
184 | + $constructor = $reflector->getConstructor(); |
|
185 | + if (empty($constructor)) { |
|
186 | + return new $concrete(); |
|
187 | + } |
|
188 | + return $reflector->newInstanceArgs( |
|
189 | + $this->resolveDependencies($constructor->getParameters()) |
|
190 | + ); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Resolve a class based dependency from the container. |
|
195 | + * @return mixed |
|
196 | + * @throws Exception |
|
197 | + */ |
|
198 | + protected function resolveClass(ReflectionParameter $parameter) |
|
199 | + { |
|
200 | + try { |
|
201 | + return $this->make($parameter->getClass()->name); |
|
202 | + } catch (Exception $error) { |
|
203 | + if ($parameter->isOptional()) { |
|
204 | + return $parameter->getDefaultValue(); |
|
205 | + } |
|
206 | + throw $error; |
|
207 | + } |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Resolve all of the dependencies from the ReflectionParameters. |
|
212 | + * @return array |
|
213 | + */ |
|
214 | + protected function resolveDependencies(array $dependencies) |
|
215 | + { |
|
216 | + $results = []; |
|
217 | + foreach ($dependencies as $dependency) { |
|
218 | + $results[] = !is_null($class = $dependency->getClass()) |
|
219 | + ? $this->resolveClass($dependency) |
|
220 | + : $this->resolveDependency($dependency); |
|
221 | + } |
|
222 | + return $results; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Resolve a single ReflectionParameter dependency. |
|
227 | + * @return array|null |
|
228 | + */ |
|
229 | + protected function resolveDependency(ReflectionParameter $parameter) |
|
230 | + { |
|
231 | + if ($parameter->isArray() && $parameter->isDefaultValueAvailable()) { |
|
232 | + return $parameter->getDefaultValue(); |
|
233 | + } |
|
234 | + return null; |
|
235 | + } |
|
236 | 236 | } |
@@ -11,272 +11,272 @@ |
||
11 | 11 | |
12 | 12 | class Translation |
13 | 13 | { |
14 | - const SEARCH_THRESHOLD = 3; |
|
14 | + const SEARCH_THRESHOLD = 3; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - protected $entries; |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + protected $entries; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $results; |
|
21 | + /** |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $results; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns all saved custom translations with translation context. |
|
28 | - * @return array |
|
29 | - */ |
|
30 | - public function all() |
|
31 | - { |
|
32 | - $translations = $this->translations(); |
|
33 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
34 | - array_walk($translations, function (&$entry) use ($entries) { |
|
35 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
36 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
37 | - : ''; |
|
38 | - }); |
|
39 | - return $translations; |
|
40 | - } |
|
26 | + /** |
|
27 | + * Returns all saved custom translations with translation context. |
|
28 | + * @return array |
|
29 | + */ |
|
30 | + public function all() |
|
31 | + { |
|
32 | + $translations = $this->translations(); |
|
33 | + $entries = $this->filter($translations, $this->entries())->results(); |
|
34 | + array_walk($translations, function (&$entry) use ($entries) { |
|
35 | + $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
36 | + ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
37 | + : ''; |
|
38 | + }); |
|
39 | + return $translations; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public function entries() |
|
46 | - { |
|
47 | - if (!isset($this->entries)) { |
|
48 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
49 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
50 | - $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
51 | - $this->entries = $entries; |
|
52 | - } |
|
53 | - return $this->entries; |
|
54 | - } |
|
42 | + /** |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public function entries() |
|
46 | + { |
|
47 | + if (!isset($this->entries)) { |
|
48 | + $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
49 | + $entries = $this->extractEntriesFromPotFile($potFile); |
|
50 | + $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
51 | + $this->entries = $entries; |
|
52 | + } |
|
53 | + return $this->entries; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param array|null $entriesToExclude |
|
58 | - * @param array|null $entries |
|
59 | - * @return static |
|
60 | - */ |
|
61 | - public function exclude($entriesToExclude = null, $entries = null) |
|
62 | - { |
|
63 | - return $this->filter($entriesToExclude, $entries, false); |
|
64 | - } |
|
56 | + /** |
|
57 | + * @param array|null $entriesToExclude |
|
58 | + * @param array|null $entries |
|
59 | + * @return static |
|
60 | + */ |
|
61 | + public function exclude($entriesToExclude = null, $entries = null) |
|
62 | + { |
|
63 | + return $this->filter($entriesToExclude, $entries, false); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @param string $potFile |
|
68 | - * @return array |
|
69 | - */ |
|
70 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
71 | - { |
|
72 | - try { |
|
73 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
74 | - foreach ($potEntries as $key => $entry) { |
|
75 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
76 | - } |
|
77 | - } catch (Exception $e) { |
|
78 | - glsr_log()->error($e->getMessage()); |
|
79 | - } |
|
80 | - return $entries; |
|
81 | - } |
|
66 | + /** |
|
67 | + * @param string $potFile |
|
68 | + * @return array |
|
69 | + */ |
|
70 | + public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
71 | + { |
|
72 | + try { |
|
73 | + $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
74 | + foreach ($potEntries as $key => $entry) { |
|
75 | + $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
76 | + } |
|
77 | + } catch (Exception $e) { |
|
78 | + glsr_log()->error($e->getMessage()); |
|
79 | + } |
|
80 | + return $entries; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @param array|null $filterWith |
|
85 | - * @param array|null $entries |
|
86 | - * @param bool $intersect |
|
87 | - * @return static |
|
88 | - */ |
|
89 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
90 | - { |
|
91 | - if (!is_array($entries)) { |
|
92 | - $entries = $this->results; |
|
93 | - } |
|
94 | - if (!is_array($filterWith)) { |
|
95 | - $filterWith = $this->translations(); |
|
96 | - } |
|
97 | - $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
98 | - $this->results = $intersect |
|
99 | - ? array_intersect_key($entries, $keys) |
|
100 | - : array_diff_key($entries, $keys); |
|
101 | - return $this; |
|
102 | - } |
|
83 | + /** |
|
84 | + * @param array|null $filterWith |
|
85 | + * @param array|null $entries |
|
86 | + * @param bool $intersect |
|
87 | + * @return static |
|
88 | + */ |
|
89 | + public function filter($filterWith = null, $entries = null, $intersect = true) |
|
90 | + { |
|
91 | + if (!is_array($entries)) { |
|
92 | + $entries = $this->results; |
|
93 | + } |
|
94 | + if (!is_array($filterWith)) { |
|
95 | + $filterWith = $this->translations(); |
|
96 | + } |
|
97 | + $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
98 | + $this->results = $intersect |
|
99 | + ? array_intersect_key($entries, $keys) |
|
100 | + : array_diff_key($entries, $keys); |
|
101 | + return $this; |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @param string $template |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - public function render($template, array $entry) |
|
109 | - { |
|
110 | - $data = array_combine( |
|
111 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
112 | - $entry |
|
113 | - ); |
|
114 | - $data['data.class'] = $data['data.error'] = ''; |
|
115 | - if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
116 | - $data['data.class'] = 'is-invalid'; |
|
117 | - $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
118 | - } |
|
119 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
120 | - 'context' => array_map('esc_html', $data), |
|
121 | - ]); |
|
122 | - } |
|
104 | + /** |
|
105 | + * @param string $template |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + public function render($template, array $entry) |
|
109 | + { |
|
110 | + $data = array_combine( |
|
111 | + array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
112 | + $entry |
|
113 | + ); |
|
114 | + $data['data.class'] = $data['data.error'] = ''; |
|
115 | + if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
116 | + $data['data.class'] = 'is-invalid'; |
|
117 | + $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
118 | + } |
|
119 | + return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
120 | + 'context' => array_map('esc_html', $data), |
|
121 | + ]); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * Returns a rendered string of all saved custom translations with translation context. |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - public function renderAll() |
|
129 | - { |
|
130 | - $rendered = ''; |
|
131 | - foreach ($this->all() as $index => $entry) { |
|
132 | - $entry['index'] = $index; |
|
133 | - $entry['prefix'] = OptionManager::databaseKey(); |
|
134 | - $rendered.= $this->render($entry['type'], $entry); |
|
135 | - } |
|
136 | - return $rendered; |
|
137 | - } |
|
124 | + /** |
|
125 | + * Returns a rendered string of all saved custom translations with translation context. |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + public function renderAll() |
|
129 | + { |
|
130 | + $rendered = ''; |
|
131 | + foreach ($this->all() as $index => $entry) { |
|
132 | + $entry['index'] = $index; |
|
133 | + $entry['prefix'] = OptionManager::databaseKey(); |
|
134 | + $rendered.= $this->render($entry['type'], $entry); |
|
135 | + } |
|
136 | + return $rendered; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * @param bool $resetAfterRender |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - public function renderResults($resetAfterRender = true) |
|
144 | - { |
|
145 | - $rendered = ''; |
|
146 | - foreach ($this->results as $id => $entry) { |
|
147 | - $data = [ |
|
148 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
149 | - 'id' => $id, |
|
150 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
151 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
152 | - ]; |
|
153 | - $text = !empty($data['p1']) |
|
154 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
155 | - : $data['s1']; |
|
156 | - $rendered.= $this->render('result', [ |
|
157 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
158 | - 'text' => wp_strip_all_tags($text), |
|
159 | - ]); |
|
160 | - } |
|
161 | - if ($resetAfterRender) { |
|
162 | - $this->reset(); |
|
163 | - } |
|
164 | - return $rendered; |
|
165 | - } |
|
139 | + /** |
|
140 | + * @param bool $resetAfterRender |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + public function renderResults($resetAfterRender = true) |
|
144 | + { |
|
145 | + $rendered = ''; |
|
146 | + foreach ($this->results as $id => $entry) { |
|
147 | + $data = [ |
|
148 | + 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
149 | + 'id' => $id, |
|
150 | + 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
151 | + 's1' => $this->getEntryString($entry, 'msgid'), |
|
152 | + ]; |
|
153 | + $text = !empty($data['p1']) |
|
154 | + ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
155 | + : $data['s1']; |
|
156 | + $rendered.= $this->render('result', [ |
|
157 | + 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
158 | + 'text' => wp_strip_all_tags($text), |
|
159 | + ]); |
|
160 | + } |
|
161 | + if ($resetAfterRender) { |
|
162 | + $this->reset(); |
|
163 | + } |
|
164 | + return $rendered; |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * @return void |
|
169 | - */ |
|
170 | - public function reset() |
|
171 | - { |
|
172 | - $this->results = []; |
|
173 | - } |
|
167 | + /** |
|
168 | + * @return void |
|
169 | + */ |
|
170 | + public function reset() |
|
171 | + { |
|
172 | + $this->results = []; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * @return array |
|
177 | - */ |
|
178 | - public function results() |
|
179 | - { |
|
180 | - $results = $this->results; |
|
181 | - $this->reset(); |
|
182 | - return $results; |
|
183 | - } |
|
175 | + /** |
|
176 | + * @return array |
|
177 | + */ |
|
178 | + public function results() |
|
179 | + { |
|
180 | + $results = $this->results; |
|
181 | + $this->reset(); |
|
182 | + return $results; |
|
183 | + } |
|
184 | 184 | |
185 | - /** |
|
186 | - * @param string $needle |
|
187 | - * @return static |
|
188 | - */ |
|
189 | - public function search($needle = '') |
|
190 | - { |
|
191 | - $this->reset(); |
|
192 | - $needle = trim(strtolower($needle)); |
|
193 | - foreach ($this->entries() as $key => $entry) { |
|
194 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
195 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
196 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
197 | - if (in_array($needle, [$single, $plural])) { |
|
198 | - $this->results[$key] = $entry; |
|
199 | - } |
|
200 | - } elseif (Str::contains(sprintf('%s %s', $single, $plural), $needle)) { |
|
201 | - $this->results[$key] = $entry; |
|
202 | - } |
|
203 | - } |
|
204 | - return $this; |
|
205 | - } |
|
185 | + /** |
|
186 | + * @param string $needle |
|
187 | + * @return static |
|
188 | + */ |
|
189 | + public function search($needle = '') |
|
190 | + { |
|
191 | + $this->reset(); |
|
192 | + $needle = trim(strtolower($needle)); |
|
193 | + foreach ($this->entries() as $key => $entry) { |
|
194 | + $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
195 | + $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
196 | + if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
197 | + if (in_array($needle, [$single, $plural])) { |
|
198 | + $this->results[$key] = $entry; |
|
199 | + } |
|
200 | + } elseif (Str::contains(sprintf('%s %s', $single, $plural), $needle)) { |
|
201 | + $this->results[$key] = $entry; |
|
202 | + } |
|
203 | + } |
|
204 | + return $this; |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Store the translations to avoid unnecessary loops. |
|
209 | - * @return array |
|
210 | - */ |
|
211 | - public function translations() |
|
212 | - { |
|
213 | - static $translations; |
|
214 | - if (empty($translations)) { |
|
215 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
216 | - $translations = isset($settings['strings']) |
|
217 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
218 | - : []; |
|
219 | - } |
|
220 | - return $translations; |
|
221 | - } |
|
207 | + /** |
|
208 | + * Store the translations to avoid unnecessary loops. |
|
209 | + * @return array |
|
210 | + */ |
|
211 | + public function translations() |
|
212 | + { |
|
213 | + static $translations; |
|
214 | + if (empty($translations)) { |
|
215 | + $settings = glsr(OptionManager::class)->get('settings'); |
|
216 | + $translations = isset($settings['strings']) |
|
217 | + ? $this->normalizeSettings((array) $settings['strings']) |
|
218 | + : []; |
|
219 | + } |
|
220 | + return $translations; |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * @param string $key |
|
225 | - * @return string |
|
226 | - */ |
|
227 | - protected function getEntryString(array $entry, $key) |
|
228 | - { |
|
229 | - return isset($entry[$key]) |
|
230 | - ? implode('', (array) $entry[$key]) |
|
231 | - : ''; |
|
232 | - } |
|
223 | + /** |
|
224 | + * @param string $key |
|
225 | + * @return string |
|
226 | + */ |
|
227 | + protected function getEntryString(array $entry, $key) |
|
228 | + { |
|
229 | + return isset($entry[$key]) |
|
230 | + ? implode('', (array) $entry[$key]) |
|
231 | + : ''; |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * @return array |
|
236 | - */ |
|
237 | - protected function normalize(array $entries) |
|
238 | - { |
|
239 | - $keys = [ |
|
240 | - 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
241 | - ]; |
|
242 | - array_walk($entries, function (&$entry) use ($keys) { |
|
243 | - foreach ($keys as $key) { |
|
244 | - try { |
|
245 | - $entry = $this->normalizeEntryString($entry, $key); |
|
246 | - } catch (\TypeError $error) { |
|
247 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
248 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
249 | - } |
|
250 | - } |
|
251 | - }); |
|
252 | - return $entries; |
|
253 | - } |
|
234 | + /** |
|
235 | + * @return array |
|
236 | + */ |
|
237 | + protected function normalize(array $entries) |
|
238 | + { |
|
239 | + $keys = [ |
|
240 | + 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
241 | + ]; |
|
242 | + array_walk($entries, function (&$entry) use ($keys) { |
|
243 | + foreach ($keys as $key) { |
|
244 | + try { |
|
245 | + $entry = $this->normalizeEntryString($entry, $key); |
|
246 | + } catch (\TypeError $error) { |
|
247 | + glsr_log()->once('error', 'Translation/normalize', $error); |
|
248 | + glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
249 | + } |
|
250 | + } |
|
251 | + }); |
|
252 | + return $entries; |
|
253 | + } |
|
254 | 254 | |
255 | - /** |
|
256 | - * @param string $key |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - protected function normalizeEntryString(array $entry, $key) |
|
260 | - { |
|
261 | - if (isset($entry[$key])) { |
|
262 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
263 | - } |
|
264 | - return $entry; |
|
265 | - } |
|
255 | + /** |
|
256 | + * @param string $key |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + protected function normalizeEntryString(array $entry, $key) |
|
260 | + { |
|
261 | + if (isset($entry[$key])) { |
|
262 | + $entry[$key] = $this->getEntryString($entry, $key); |
|
263 | + } |
|
264 | + return $entry; |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * @return array |
|
269 | - */ |
|
270 | - protected function normalizeSettings(array $strings) |
|
271 | - { |
|
272 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
273 | - $strings = array_filter($strings, 'is_array'); |
|
274 | - foreach ($strings as &$string) { |
|
275 | - $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
276 | - $string = wp_parse_args($string, $defaultString); |
|
277 | - } |
|
278 | - return array_filter($strings, function ($string) { |
|
279 | - return !empty($string['id']); |
|
280 | - }); |
|
281 | - } |
|
267 | + /** |
|
268 | + * @return array |
|
269 | + */ |
|
270 | + protected function normalizeSettings(array $strings) |
|
271 | + { |
|
272 | + $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
273 | + $strings = array_filter($strings, 'is_array'); |
|
274 | + foreach ($strings as &$string) { |
|
275 | + $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
276 | + $string = wp_parse_args($string, $defaultString); |
|
277 | + } |
|
278 | + return array_filter($strings, function ($string) { |
|
279 | + return !empty($string['id']); |
|
280 | + }); |
|
281 | + } |
|
282 | 282 | } |
@@ -12,302 +12,302 @@ |
||
12 | 12 | */ |
13 | 13 | class Validator |
14 | 14 | { |
15 | - use ValidationRules; |
|
15 | + use ValidationRules; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - public $errors = []; |
|
17 | + /** |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + public $errors = []; |
|
21 | 21 | |
22 | - /** |
|
23 | - * The data under validation. |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $data = []; |
|
22 | + /** |
|
23 | + * The data under validation. |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $data = []; |
|
27 | 27 | |
28 | - /** |
|
29 | - * The failed validation rules. |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $failedRules = []; |
|
28 | + /** |
|
29 | + * The failed validation rules. |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $failedRules = []; |
|
33 | 33 | |
34 | - /** |
|
35 | - * The rules to be applied to the data. |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $rules = []; |
|
34 | + /** |
|
35 | + * The rules to be applied to the data. |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $rules = []; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The size related validation rules. |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - protected $sizeRules = [ |
|
45 | - 'Between', 'Max', 'Min', |
|
46 | - ]; |
|
40 | + /** |
|
41 | + * The size related validation rules. |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + protected $sizeRules = [ |
|
45 | + 'Between', 'Max', 'Min', |
|
46 | + ]; |
|
47 | 47 | |
48 | - /** |
|
49 | - * The validation rules that imply the field is required. |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $implicitRules = [ |
|
53 | - 'Required', |
|
54 | - ]; |
|
48 | + /** |
|
49 | + * The validation rules that imply the field is required. |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $implicitRules = [ |
|
53 | + 'Required', |
|
54 | + ]; |
|
55 | 55 | |
56 | - /** |
|
57 | - * The numeric related validation rules. |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - protected $numericRules = [ |
|
61 | - 'Number', |
|
62 | - ]; |
|
56 | + /** |
|
57 | + * The numeric related validation rules. |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + protected $numericRules = [ |
|
61 | + 'Number', |
|
62 | + ]; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Run the validator's rules against its data. |
|
66 | - * @param mixed $data |
|
67 | - * @return array |
|
68 | - */ |
|
69 | - public function validate($data, array $rules = []) |
|
70 | - { |
|
71 | - $this->normalizeData($data); |
|
72 | - $this->setRules($rules); |
|
73 | - foreach ($this->rules as $attribute => $rules) { |
|
74 | - foreach ($rules as $rule) { |
|
75 | - $this->validateAttribute($attribute, $rule); |
|
76 | - if ($this->shouldStopValidating($attribute)) { |
|
77 | - break; |
|
78 | - } |
|
79 | - } |
|
80 | - } |
|
81 | - return $this->errors; |
|
82 | - } |
|
64 | + /** |
|
65 | + * Run the validator's rules against its data. |
|
66 | + * @param mixed $data |
|
67 | + * @return array |
|
68 | + */ |
|
69 | + public function validate($data, array $rules = []) |
|
70 | + { |
|
71 | + $this->normalizeData($data); |
|
72 | + $this->setRules($rules); |
|
73 | + foreach ($this->rules as $attribute => $rules) { |
|
74 | + foreach ($rules as $rule) { |
|
75 | + $this->validateAttribute($attribute, $rule); |
|
76 | + if ($this->shouldStopValidating($attribute)) { |
|
77 | + break; |
|
78 | + } |
|
79 | + } |
|
80 | + } |
|
81 | + return $this->errors; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Validate a given attribute against a rule. |
|
86 | - * @param string $attribute |
|
87 | - * @param string $rule |
|
88 | - * @return void |
|
89 | - * @throws BadMethodCallException |
|
90 | - */ |
|
91 | - public function validateAttribute($attribute, $rule) |
|
92 | - { |
|
93 | - list($rule, $parameters) = $this->parseRule($rule); |
|
94 | - if ('' == $rule) { |
|
95 | - return; |
|
96 | - } |
|
97 | - $value = $this->getValue($attribute); |
|
98 | - if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
100 | - } |
|
101 | - if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | - $this->addFailure($attribute, $rule, $parameters); |
|
103 | - } |
|
104 | - } |
|
84 | + /** |
|
85 | + * Validate a given attribute against a rule. |
|
86 | + * @param string $attribute |
|
87 | + * @param string $rule |
|
88 | + * @return void |
|
89 | + * @throws BadMethodCallException |
|
90 | + */ |
|
91 | + public function validateAttribute($attribute, $rule) |
|
92 | + { |
|
93 | + list($rule, $parameters) = $this->parseRule($rule); |
|
94 | + if ('' == $rule) { |
|
95 | + return; |
|
96 | + } |
|
97 | + $value = $this->getValue($attribute); |
|
98 | + if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | + throw new BadMethodCallException("Method [$method] does not exist."); |
|
100 | + } |
|
101 | + if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | + $this->addFailure($attribute, $rule, $parameters); |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Add an error message to the validator's collection of errors. |
|
108 | - * @param string $attribute |
|
109 | - * @param string $rule |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - protected function addError($attribute, $rule, array $parameters) |
|
113 | - { |
|
114 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
115 | - $this->errors[$attribute][] = $message; |
|
116 | - } |
|
106 | + /** |
|
107 | + * Add an error message to the validator's collection of errors. |
|
108 | + * @param string $attribute |
|
109 | + * @param string $rule |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + protected function addError($attribute, $rule, array $parameters) |
|
113 | + { |
|
114 | + $message = $this->getMessage($attribute, $rule, $parameters); |
|
115 | + $this->errors[$attribute][] = $message; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Add a failed rule and error message to the collection. |
|
120 | - * @param string $attribute |
|
121 | - * @param string $rule |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - protected function addFailure($attribute, $rule, array $parameters) |
|
125 | - { |
|
126 | - $this->addError($attribute, $rule, $parameters); |
|
127 | - $this->failedRules[$attribute][$rule] = $parameters; |
|
128 | - } |
|
118 | + /** |
|
119 | + * Add a failed rule and error message to the collection. |
|
120 | + * @param string $attribute |
|
121 | + * @param string $rule |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + protected function addFailure($attribute, $rule, array $parameters) |
|
125 | + { |
|
126 | + $this->addError($attribute, $rule, $parameters); |
|
127 | + $this->failedRules[$attribute][$rule] = $parameters; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Get the data type of the given attribute. |
|
132 | - * @param string $attribute |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function getAttributeType($attribute) |
|
136 | - { |
|
137 | - return !$this->hasRule($attribute, $this->numericRules) |
|
138 | - ? 'length' |
|
139 | - : ''; |
|
140 | - } |
|
130 | + /** |
|
131 | + * Get the data type of the given attribute. |
|
132 | + * @param string $attribute |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function getAttributeType($attribute) |
|
136 | + { |
|
137 | + return !$this->hasRule($attribute, $this->numericRules) |
|
138 | + ? 'length' |
|
139 | + : ''; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Get the validation message for an attribute and rule. |
|
144 | - * @param string $attribute |
|
145 | - * @param string $rule |
|
146 | - * @return string|null |
|
147 | - */ |
|
148 | - protected function getMessage($attribute, $rule, array $parameters) |
|
149 | - { |
|
150 | - if (in_array($rule, $this->sizeRules)) { |
|
151 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
152 | - } |
|
153 | - $lowerRule = Str::snakeCase($rule); |
|
154 | - return $this->translator($lowerRule, $parameters); |
|
155 | - } |
|
142 | + /** |
|
143 | + * Get the validation message for an attribute and rule. |
|
144 | + * @param string $attribute |
|
145 | + * @param string $rule |
|
146 | + * @return string|null |
|
147 | + */ |
|
148 | + protected function getMessage($attribute, $rule, array $parameters) |
|
149 | + { |
|
150 | + if (in_array($rule, $this->sizeRules)) { |
|
151 | + return $this->getSizeMessage($attribute, $rule, $parameters); |
|
152 | + } |
|
153 | + $lowerRule = Str::snakeCase($rule); |
|
154 | + return $this->translator($lowerRule, $parameters); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * Get a rule and its parameters for a given attribute. |
|
159 | - * @param string $attribute |
|
160 | - * @param string|array $rules |
|
161 | - * @return array|null |
|
162 | - */ |
|
163 | - protected function getRule($attribute, $rules) |
|
164 | - { |
|
165 | - if (!array_key_exists($attribute, $this->rules)) { |
|
166 | - return; |
|
167 | - } |
|
168 | - $rules = (array) $rules; |
|
169 | - foreach ($this->rules[$attribute] as $rule) { |
|
170 | - list($rule, $parameters) = $this->parseRule($rule); |
|
171 | - if (in_array($rule, $rules)) { |
|
172 | - return [$rule, $parameters]; |
|
173 | - } |
|
174 | - } |
|
175 | - } |
|
157 | + /** |
|
158 | + * Get a rule and its parameters for a given attribute. |
|
159 | + * @param string $attribute |
|
160 | + * @param string|array $rules |
|
161 | + * @return array|null |
|
162 | + */ |
|
163 | + protected function getRule($attribute, $rules) |
|
164 | + { |
|
165 | + if (!array_key_exists($attribute, $this->rules)) { |
|
166 | + return; |
|
167 | + } |
|
168 | + $rules = (array) $rules; |
|
169 | + foreach ($this->rules[$attribute] as $rule) { |
|
170 | + list($rule, $parameters) = $this->parseRule($rule); |
|
171 | + if (in_array($rule, $rules)) { |
|
172 | + return [$rule, $parameters]; |
|
173 | + } |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get the size of an attribute. |
|
179 | - * @param string $attribute |
|
180 | - * @param mixed $value |
|
181 | - * @return mixed |
|
182 | - */ |
|
183 | - protected function getSize($attribute, $value) |
|
184 | - { |
|
185 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | - if (is_numeric($value) && $hasNumeric) { |
|
187 | - return $value; |
|
188 | - } elseif (is_array($value)) { |
|
189 | - return count($value); |
|
190 | - } |
|
191 | - return function_exists('mb_strlen') |
|
192 | - ? mb_strlen($value) |
|
193 | - : strlen($value); |
|
194 | - } |
|
177 | + /** |
|
178 | + * Get the size of an attribute. |
|
179 | + * @param string $attribute |
|
180 | + * @param mixed $value |
|
181 | + * @return mixed |
|
182 | + */ |
|
183 | + protected function getSize($attribute, $value) |
|
184 | + { |
|
185 | + $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | + if (is_numeric($value) && $hasNumeric) { |
|
187 | + return $value; |
|
188 | + } elseif (is_array($value)) { |
|
189 | + return count($value); |
|
190 | + } |
|
191 | + return function_exists('mb_strlen') |
|
192 | + ? mb_strlen($value) |
|
193 | + : strlen($value); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Get the proper error message for an attribute and size rule. |
|
198 | - * @param string $attribute |
|
199 | - * @param string $rule |
|
200 | - * @return string|null |
|
201 | - */ |
|
202 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
203 | - { |
|
204 | - $type = $this->getAttributeType($attribute); |
|
205 | - $lowerRule = Str::snakeCase($rule.$type); |
|
206 | - return $this->translator($lowerRule, $parameters); |
|
207 | - } |
|
196 | + /** |
|
197 | + * Get the proper error message for an attribute and size rule. |
|
198 | + * @param string $attribute |
|
199 | + * @param string $rule |
|
200 | + * @return string|null |
|
201 | + */ |
|
202 | + protected function getSizeMessage($attribute, $rule, array $parameters) |
|
203 | + { |
|
204 | + $type = $this->getAttributeType($attribute); |
|
205 | + $lowerRule = Str::snakeCase($rule.$type); |
|
206 | + return $this->translator($lowerRule, $parameters); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Get the value of a given attribute. |
|
211 | - * @param string $attribute |
|
212 | - * @return mixed |
|
213 | - */ |
|
214 | - protected function getValue($attribute) |
|
215 | - { |
|
216 | - if (isset($this->data[$attribute])) { |
|
217 | - return $this->data[$attribute]; |
|
218 | - } |
|
219 | - } |
|
209 | + /** |
|
210 | + * Get the value of a given attribute. |
|
211 | + * @param string $attribute |
|
212 | + * @return mixed |
|
213 | + */ |
|
214 | + protected function getValue($attribute) |
|
215 | + { |
|
216 | + if (isset($this->data[$attribute])) { |
|
217 | + return $this->data[$attribute]; |
|
218 | + } |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Determine if the given attribute has a rule in the given set. |
|
223 | - * @param string $attribute |
|
224 | - * @param string|array $rules |
|
225 | - * @return bool |
|
226 | - */ |
|
227 | - protected function hasRule($attribute, $rules) |
|
228 | - { |
|
229 | - return !is_null($this->getRule($attribute, $rules)); |
|
230 | - } |
|
221 | + /** |
|
222 | + * Determine if the given attribute has a rule in the given set. |
|
223 | + * @param string $attribute |
|
224 | + * @param string|array $rules |
|
225 | + * @return bool |
|
226 | + */ |
|
227 | + protected function hasRule($attribute, $rules) |
|
228 | + { |
|
229 | + return !is_null($this->getRule($attribute, $rules)); |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Normalize the provided data to an array. |
|
234 | - * @param mixed $data |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - protected function normalizeData($data) |
|
238 | - { |
|
239 | - $this->data = is_object($data) |
|
240 | - ? get_object_vars($data) |
|
241 | - : $data; |
|
242 | - } |
|
232 | + /** |
|
233 | + * Normalize the provided data to an array. |
|
234 | + * @param mixed $data |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + protected function normalizeData($data) |
|
238 | + { |
|
239 | + $this->data = is_object($data) |
|
240 | + ? get_object_vars($data) |
|
241 | + : $data; |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * Parse a parameter list. |
|
246 | - * @param string $rule |
|
247 | - * @param string $parameter |
|
248 | - * @return array |
|
249 | - */ |
|
250 | - protected function parseParameters($rule, $parameter) |
|
251 | - { |
|
252 | - return 'regex' == strtolower($rule) |
|
253 | - ? [$parameter] |
|
254 | - : str_getcsv($parameter); |
|
255 | - } |
|
244 | + /** |
|
245 | + * Parse a parameter list. |
|
246 | + * @param string $rule |
|
247 | + * @param string $parameter |
|
248 | + * @return array |
|
249 | + */ |
|
250 | + protected function parseParameters($rule, $parameter) |
|
251 | + { |
|
252 | + return 'regex' == strtolower($rule) |
|
253 | + ? [$parameter] |
|
254 | + : str_getcsv($parameter); |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Extract the rule name and parameters from a rule. |
|
259 | - * @param string $rule |
|
260 | - * @return array |
|
261 | - */ |
|
262 | - protected function parseRule($rule) |
|
263 | - { |
|
264 | - $parameters = []; |
|
265 | - if (Str::contains($rule, ':')) { |
|
266 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | - $parameters = $this->parseParameters($rule, $parameter); |
|
268 | - } |
|
269 | - $rule = Str::camelCase($rule); |
|
270 | - return [$rule, $parameters]; |
|
271 | - } |
|
257 | + /** |
|
258 | + * Extract the rule name and parameters from a rule. |
|
259 | + * @param string $rule |
|
260 | + * @return array |
|
261 | + */ |
|
262 | + protected function parseRule($rule) |
|
263 | + { |
|
264 | + $parameters = []; |
|
265 | + if (Str::contains($rule, ':')) { |
|
266 | + list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | + $parameters = $this->parseParameters($rule, $parameter); |
|
268 | + } |
|
269 | + $rule = Str::camelCase($rule); |
|
270 | + return [$rule, $parameters]; |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Set the validation rules. |
|
275 | - * @return void |
|
276 | - */ |
|
277 | - protected function setRules(array $rules) |
|
278 | - { |
|
279 | - foreach ($rules as $key => $rule) { |
|
280 | - $rules[$key] = is_string($rule) |
|
281 | - ? explode('|', $rule) |
|
282 | - : $rule; |
|
283 | - } |
|
284 | - $this->rules = $rules; |
|
285 | - } |
|
273 | + /** |
|
274 | + * Set the validation rules. |
|
275 | + * @return void |
|
276 | + */ |
|
277 | + protected function setRules(array $rules) |
|
278 | + { |
|
279 | + foreach ($rules as $key => $rule) { |
|
280 | + $rules[$key] = is_string($rule) |
|
281 | + ? explode('|', $rule) |
|
282 | + : $rule; |
|
283 | + } |
|
284 | + $this->rules = $rules; |
|
285 | + } |
|
286 | 286 | |
287 | - /** |
|
288 | - * Check if we should stop further validations on a given attribute. |
|
289 | - * @param string $attribute |
|
290 | - * @return bool |
|
291 | - */ |
|
292 | - protected function shouldStopValidating($attribute) |
|
293 | - { |
|
294 | - return $this->hasRule($attribute, $this->implicitRules) |
|
295 | - && isset($this->failedRules[$attribute]) |
|
296 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
297 | - } |
|
287 | + /** |
|
288 | + * Check if we should stop further validations on a given attribute. |
|
289 | + * @param string $attribute |
|
290 | + * @return bool |
|
291 | + */ |
|
292 | + protected function shouldStopValidating($attribute) |
|
293 | + { |
|
294 | + return $this->hasRule($attribute, $this->implicitRules) |
|
295 | + && isset($this->failedRules[$attribute]) |
|
296 | + && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Returns a translated message for the attribute. |
|
301 | - * @param string $key |
|
302 | - * @param string $attribute |
|
303 | - * @return void|string |
|
304 | - */ |
|
305 | - protected function translator($key, array $parameters) |
|
306 | - { |
|
307 | - $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | - if (isset($strings[$key])) { |
|
309 | - return $this->replace($strings[$key], $parameters); |
|
310 | - } |
|
311 | - return 'error'; |
|
312 | - } |
|
299 | + /** |
|
300 | + * Returns a translated message for the attribute. |
|
301 | + * @param string $key |
|
302 | + * @param string $attribute |
|
303 | + * @return void|string |
|
304 | + */ |
|
305 | + protected function translator($key, array $parameters) |
|
306 | + { |
|
307 | + $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | + if (isset($strings[$key])) { |
|
309 | + return $this->replace($strings[$key], $parameters); |
|
310 | + } |
|
311 | + return 'error'; |
|
312 | + } |
|
313 | 313 | } |