@@ -6,54 +6,54 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Blacklist |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @return bool |
|
| 11 | - */ |
|
| 12 | - public function isBlacklisted(array $review) |
|
| 13 | - { |
|
| 14 | - $target = implode("\n", array_filter([ |
|
| 15 | - $review['name'], |
|
| 16 | - $review['content'], |
|
| 17 | - $review['email'], |
|
| 18 | - $review['ip_address'], |
|
| 19 | - $review['title'], |
|
| 20 | - ])); |
|
| 21 | - return (bool) apply_filters('site-reviews/blacklist/is-blacklisted', |
|
| 22 | - $this->check($target), |
|
| 23 | - $review, |
|
| 24 | - $target |
|
| 25 | - ); |
|
| 26 | - } |
|
| 9 | + /** |
|
| 10 | + * @return bool |
|
| 11 | + */ |
|
| 12 | + public function isBlacklisted(array $review) |
|
| 13 | + { |
|
| 14 | + $target = implode("\n", array_filter([ |
|
| 15 | + $review['name'], |
|
| 16 | + $review['content'], |
|
| 17 | + $review['email'], |
|
| 18 | + $review['ip_address'], |
|
| 19 | + $review['title'], |
|
| 20 | + ])); |
|
| 21 | + return (bool) apply_filters('site-reviews/blacklist/is-blacklisted', |
|
| 22 | + $this->check($target), |
|
| 23 | + $review, |
|
| 24 | + $target |
|
| 25 | + ); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @param string $target |
|
| 30 | - * @return bool |
|
| 31 | - */ |
|
| 32 | - protected function check($target) |
|
| 33 | - { |
|
| 34 | - $blacklist = $this->getBlacklist(); |
|
| 35 | - if (empty($blacklist)) { |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 38 | - $lines = explode("\n", $blacklist); |
|
| 39 | - foreach ((array) $lines as $line) { |
|
| 40 | - $line = trim($line); |
|
| 41 | - if (empty($line) || 256 < strlen($line)) { |
|
| 42 | - continue; |
|
| 43 | - } |
|
| 44 | - $pattern = sprintf('#%s#i', preg_quote($line, '#')); |
|
| 45 | - if (preg_match($pattern, $target)) { |
|
| 46 | - return true; |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - return false; |
|
| 50 | - } |
|
| 28 | + /** |
|
| 29 | + * @param string $target |
|
| 30 | + * @return bool |
|
| 31 | + */ |
|
| 32 | + protected function check($target) |
|
| 33 | + { |
|
| 34 | + $blacklist = $this->getBlacklist(); |
|
| 35 | + if (empty($blacklist)) { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | + $lines = explode("\n", $blacklist); |
|
| 39 | + foreach ((array) $lines as $line) { |
|
| 40 | + $line = trim($line); |
|
| 41 | + if (empty($line) || 256 < strlen($line)) { |
|
| 42 | + continue; |
|
| 43 | + } |
|
| 44 | + $pattern = sprintf('#%s#i', preg_quote($line, '#')); |
|
| 45 | + if (preg_match($pattern, $target)) { |
|
| 46 | + return true; |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + return false; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - protected function getBlacklist() |
|
| 53 | - { |
|
| 54 | - $option = glsr(OptionManager::class)->get('settings.submissions.blacklist.integration'); |
|
| 55 | - return $option == 'comments' |
|
| 56 | - ? trim(glsr(OptionManager::class)->getWP('blacklist_keys')) |
|
| 57 | - : trim(glsr(OptionManager::class)->get('settings.submissions.blacklist.entries')); |
|
| 58 | - } |
|
| 52 | + protected function getBlacklist() |
|
| 53 | + { |
|
| 54 | + $option = glsr(OptionManager::class)->get('settings.submissions.blacklist.integration'); |
|
| 55 | + return $option == 'comments' |
|
| 56 | + ? trim(glsr(OptionManager::class)->getWP('blacklist_keys')) |
|
| 57 | + : trim(glsr(OptionManager::class)->get('settings.submissions.blacklist.entries')); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -8,164 +8,164 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Notification |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var bool |
|
| 13 | - */ |
|
| 14 | - protected $email; |
|
| 11 | + /** |
|
| 12 | + * @var bool |
|
| 13 | + */ |
|
| 14 | + protected $email; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var Review |
|
| 18 | - */ |
|
| 19 | - protected $review; |
|
| 16 | + /** |
|
| 17 | + * @var Review |
|
| 18 | + */ |
|
| 19 | + protected $review; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var bool |
|
| 23 | - */ |
|
| 24 | - protected $slack; |
|
| 21 | + /** |
|
| 22 | + * @var bool |
|
| 23 | + */ |
|
| 24 | + protected $slack; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - protected $types; |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + protected $types; |
|
| 30 | 30 | |
| 31 | - public function __construct() |
|
| 32 | - { |
|
| 33 | - $types = glsr(OptionManager::class)->get('settings.general.notifications', []); |
|
| 34 | - $this->email = count(array_intersect(['admin', 'custom'], $types)) > 0; |
|
| 35 | - $this->slack = in_array('slack', $types); |
|
| 36 | - $this->types = $types; |
|
| 37 | - } |
|
| 31 | + public function __construct() |
|
| 32 | + { |
|
| 33 | + $types = glsr(OptionManager::class)->get('settings.general.notifications', []); |
|
| 34 | + $this->email = count(array_intersect(['admin', 'custom'], $types)) > 0; |
|
| 35 | + $this->slack = in_array('slack', $types); |
|
| 36 | + $this->types = $types; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @return void |
|
| 41 | - */ |
|
| 42 | - public function send(Review $review) |
|
| 43 | - { |
|
| 44 | - if (empty($this->types)) { |
|
| 45 | - return; |
|
| 46 | - } |
|
| 47 | - $this->review = $review; |
|
| 48 | - $args = [ |
|
| 49 | - 'link' => $this->getLink(), |
|
| 50 | - 'title' => $this->getTitle(), |
|
| 51 | - ]; |
|
| 52 | - $this->sendToEmail($args); |
|
| 53 | - $this->sendToSlack($args); |
|
| 54 | - } |
|
| 39 | + /** |
|
| 40 | + * @return void |
|
| 41 | + */ |
|
| 42 | + public function send(Review $review) |
|
| 43 | + { |
|
| 44 | + if (empty($this->types)) { |
|
| 45 | + return; |
|
| 46 | + } |
|
| 47 | + $this->review = $review; |
|
| 48 | + $args = [ |
|
| 49 | + 'link' => $this->getLink(), |
|
| 50 | + 'title' => $this->getTitle(), |
|
| 51 | + ]; |
|
| 52 | + $this->sendToEmail($args); |
|
| 53 | + $this->sendToSlack($args); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @return Email |
|
| 58 | - */ |
|
| 59 | - protected function buildEmail(array $args) |
|
| 60 | - { |
|
| 61 | - return glsr(Email::class)->compose([ |
|
| 62 | - 'to' => $this->getEmailAddresses(), |
|
| 63 | - 'subject' => $args['title'], |
|
| 64 | - 'template' => 'email-notification', |
|
| 65 | - 'template-tags' => [ |
|
| 66 | - 'review_author' => $this->review->author ?: __('Anonymous', 'site-reviews'), |
|
| 67 | - 'review_content' => $this->review->content, |
|
| 68 | - 'review_email' => $this->review->email, |
|
| 69 | - 'review_ip' => $this->review->ip_address, |
|
| 70 | - 'review_link' => sprintf('<a href="%1$s">%1$s</a>', $args['link']), |
|
| 71 | - 'review_rating' => $this->review->rating, |
|
| 72 | - 'review_title' => $this->review->title, |
|
| 73 | - ], |
|
| 74 | - ]); |
|
| 75 | - } |
|
| 56 | + /** |
|
| 57 | + * @return Email |
|
| 58 | + */ |
|
| 59 | + protected function buildEmail(array $args) |
|
| 60 | + { |
|
| 61 | + return glsr(Email::class)->compose([ |
|
| 62 | + 'to' => $this->getEmailAddresses(), |
|
| 63 | + 'subject' => $args['title'], |
|
| 64 | + 'template' => 'email-notification', |
|
| 65 | + 'template-tags' => [ |
|
| 66 | + 'review_author' => $this->review->author ?: __('Anonymous', 'site-reviews'), |
|
| 67 | + 'review_content' => $this->review->content, |
|
| 68 | + 'review_email' => $this->review->email, |
|
| 69 | + 'review_ip' => $this->review->ip_address, |
|
| 70 | + 'review_link' => sprintf('<a href="%1$s">%1$s</a>', $args['link']), |
|
| 71 | + 'review_rating' => $this->review->rating, |
|
| 72 | + 'review_title' => $this->review->title, |
|
| 73 | + ], |
|
| 74 | + ]); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * @return Slack |
|
| 79 | - */ |
|
| 80 | - protected function buildSlackNotification(array $args) |
|
| 81 | - { |
|
| 82 | - return glsr(Slack::class)->compose($this->review, [ |
|
| 83 | - 'button_url' => $args['link'], |
|
| 84 | - 'fallback' => $this->buildEmail($args)->read('plaintext'), |
|
| 85 | - 'pretext' => $args['title'], |
|
| 86 | - ]); |
|
| 87 | - } |
|
| 77 | + /** |
|
| 78 | + * @return Slack |
|
| 79 | + */ |
|
| 80 | + protected function buildSlackNotification(array $args) |
|
| 81 | + { |
|
| 82 | + return glsr(Slack::class)->compose($this->review, [ |
|
| 83 | + 'button_url' => $args['link'], |
|
| 84 | + 'fallback' => $this->buildEmail($args)->read('plaintext'), |
|
| 85 | + 'pretext' => $args['title'], |
|
| 86 | + ]); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @return array |
|
| 91 | - */ |
|
| 92 | - protected function getEmailAddresses() |
|
| 93 | - { |
|
| 94 | - $emails = []; |
|
| 95 | - if (in_array('admin', $this->types)) { |
|
| 96 | - $emails[] = glsr(OptionManager::class)->getWP('admin_email'); |
|
| 97 | - } |
|
| 98 | - if (in_array('author', $this->types)) { |
|
| 99 | - $assignedPost = get_post(intval($this->review->assigned_to)); |
|
| 100 | - if ($assignedPost instanceof WP_Post) { |
|
| 101 | - $this->email = true; |
|
| 102 | - $emails[] = get_the_author_meta('user_email', intval($assignedPost->post_author)); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - if (in_array('custom', $this->types)) { |
|
| 106 | - $customEmails = glsr(OptionManager::class)->get('settings.general.notification_email'); |
|
| 107 | - $customEmails = str_replace([' ', ',', ';'], ',', $customEmails); |
|
| 108 | - $customEmails = explode(',', $customEmails); |
|
| 109 | - $emails = array_merge($emails, $customEmails); |
|
| 110 | - } |
|
| 111 | - $emails = array_filter(array_keys(array_flip($emails))); |
|
| 112 | - return apply_filters('site-reviews/notification/emails', $emails, $this->review); |
|
| 113 | - } |
|
| 89 | + /** |
|
| 90 | + * @return array |
|
| 91 | + */ |
|
| 92 | + protected function getEmailAddresses() |
|
| 93 | + { |
|
| 94 | + $emails = []; |
|
| 95 | + if (in_array('admin', $this->types)) { |
|
| 96 | + $emails[] = glsr(OptionManager::class)->getWP('admin_email'); |
|
| 97 | + } |
|
| 98 | + if (in_array('author', $this->types)) { |
|
| 99 | + $assignedPost = get_post(intval($this->review->assigned_to)); |
|
| 100 | + if ($assignedPost instanceof WP_Post) { |
|
| 101 | + $this->email = true; |
|
| 102 | + $emails[] = get_the_author_meta('user_email', intval($assignedPost->post_author)); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + if (in_array('custom', $this->types)) { |
|
| 106 | + $customEmails = glsr(OptionManager::class)->get('settings.general.notification_email'); |
|
| 107 | + $customEmails = str_replace([' ', ',', ';'], ',', $customEmails); |
|
| 108 | + $customEmails = explode(',', $customEmails); |
|
| 109 | + $emails = array_merge($emails, $customEmails); |
|
| 110 | + } |
|
| 111 | + $emails = array_filter(array_keys(array_flip($emails))); |
|
| 112 | + return apply_filters('site-reviews/notification/emails', $emails, $this->review); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - protected function getLink() |
|
| 119 | - { |
|
| 120 | - return admin_url('post.php?post='.$this->review->ID.'&action=edit'); |
|
| 121 | - } |
|
| 115 | + /** |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + protected function getLink() |
|
| 119 | + { |
|
| 120 | + return admin_url('post.php?post='.$this->review->ID.'&action=edit'); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * @return string |
|
| 125 | - */ |
|
| 126 | - protected function getTitle() |
|
| 127 | - { |
|
| 128 | - $assignedTitle = get_the_title(intval($this->review->assigned_to)); |
|
| 129 | - $title = _nx( |
|
| 130 | - 'New %s-star review', |
|
| 131 | - 'New %s-star review of: %s', |
|
| 132 | - intval(empty($assignedTitle)), |
|
| 133 | - 'This string differs depending on whether or not the review has been assigned to a post.', |
|
| 134 | - 'site-reviews' |
|
| 135 | - ); |
|
| 136 | - $title = sprintf('[%s] %s', |
|
| 137 | - wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES), |
|
| 138 | - sprintf($title, $this->review->rating, $assignedTitle) |
|
| 139 | - ); |
|
| 140 | - return apply_filters('site-reviews/notification/title', $title, $this->review); |
|
| 141 | - } |
|
| 123 | + /** |
|
| 124 | + * @return string |
|
| 125 | + */ |
|
| 126 | + protected function getTitle() |
|
| 127 | + { |
|
| 128 | + $assignedTitle = get_the_title(intval($this->review->assigned_to)); |
|
| 129 | + $title = _nx( |
|
| 130 | + 'New %s-star review', |
|
| 131 | + 'New %s-star review of: %s', |
|
| 132 | + intval(empty($assignedTitle)), |
|
| 133 | + 'This string differs depending on whether or not the review has been assigned to a post.', |
|
| 134 | + 'site-reviews' |
|
| 135 | + ); |
|
| 136 | + $title = sprintf('[%s] %s', |
|
| 137 | + wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES), |
|
| 138 | + sprintf($title, $this->review->rating, $assignedTitle) |
|
| 139 | + ); |
|
| 140 | + return apply_filters('site-reviews/notification/title', $title, $this->review); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * @return void |
|
| 145 | - */ |
|
| 146 | - protected function sendToEmail(array $args) |
|
| 147 | - { |
|
| 148 | - $email = $this->buildEmail($args); |
|
| 149 | - if (empty($email->to)) { |
|
| 150 | - glsr_log()->error('Email notification was not sent (missing email address)'); |
|
| 151 | - return; |
|
| 152 | - } |
|
| 153 | - $email->send(); |
|
| 154 | - } |
|
| 143 | + /** |
|
| 144 | + * @return void |
|
| 145 | + */ |
|
| 146 | + protected function sendToEmail(array $args) |
|
| 147 | + { |
|
| 148 | + $email = $this->buildEmail($args); |
|
| 149 | + if (empty($email->to)) { |
|
| 150 | + glsr_log()->error('Email notification was not sent (missing email address)'); |
|
| 151 | + return; |
|
| 152 | + } |
|
| 153 | + $email->send(); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * @return void |
|
| 158 | - */ |
|
| 159 | - protected function sendToSlack(array $args) |
|
| 160 | - { |
|
| 161 | - if (!$this->slack) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - $notification = $this->buildSlackNotification($args); |
|
| 165 | - $result = $notification->send(); |
|
| 166 | - if (is_wp_error($result)) { |
|
| 167 | - $notification->review = null; |
|
| 168 | - glsr_log()->error($result->get_error_message())->debug($notification); |
|
| 169 | - } |
|
| 170 | - } |
|
| 156 | + /** |
|
| 157 | + * @return void |
|
| 158 | + */ |
|
| 159 | + protected function sendToSlack(array $args) |
|
| 160 | + { |
|
| 161 | + if (!$this->slack) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + $notification = $this->buildSlackNotification($args); |
|
| 165 | + $result = $notification->send(); |
|
| 166 | + if (is_wp_error($result)) { |
|
| 167 | + $notification->review = null; |
|
| 168 | + glsr_log()->error($result->get_error_message())->debug($notification); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | 171 | } |
@@ -7,84 +7,84 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Customization |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @return array |
|
| 12 | - */ |
|
| 13 | - public function filterEditorSettings(array $settings) |
|
| 14 | - { |
|
| 15 | - if ($this->isReviewEditable()) { |
|
| 16 | - $settings = [ |
|
| 17 | - 'media_buttons' => false, |
|
| 18 | - 'quicktags' => false, |
|
| 19 | - 'textarea_rows' => 12, |
|
| 20 | - 'tinymce' => false, |
|
| 21 | - ]; |
|
| 22 | - } |
|
| 23 | - return $settings; |
|
| 24 | - } |
|
| 10 | + /** |
|
| 11 | + * @return array |
|
| 12 | + */ |
|
| 13 | + public function filterEditorSettings(array $settings) |
|
| 14 | + { |
|
| 15 | + if ($this->isReviewEditable()) { |
|
| 16 | + $settings = [ |
|
| 17 | + 'media_buttons' => false, |
|
| 18 | + 'quicktags' => false, |
|
| 19 | + 'textarea_rows' => 12, |
|
| 20 | + 'tinymce' => false, |
|
| 21 | + ]; |
|
| 22 | + } |
|
| 23 | + return $settings; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @param string $html |
|
| 28 | - * @return string |
|
| 29 | - */ |
|
| 30 | - public function filterEditorTextarea($html) |
|
| 31 | - { |
|
| 32 | - if ($this->isReviewEditable()) { |
|
| 33 | - $html = str_replace('<textarea', '<div id="ed_toolbar"></div><textarea', $html); |
|
| 34 | - } |
|
| 35 | - return $html; |
|
| 36 | - } |
|
| 26 | + /** |
|
| 27 | + * @param string $html |
|
| 28 | + * @return string |
|
| 29 | + */ |
|
| 30 | + public function filterEditorTextarea($html) |
|
| 31 | + { |
|
| 32 | + if ($this->isReviewEditable()) { |
|
| 33 | + $html = str_replace('<textarea', '<div id="ed_toolbar"></div><textarea', $html); |
|
| 34 | + } |
|
| 35 | + return $html; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @return void |
|
| 40 | - */ |
|
| 41 | - public function removeAutosave() |
|
| 42 | - { |
|
| 43 | - if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
| 44 | - return; |
|
| 45 | - } |
|
| 46 | - wp_deregister_script('autosave'); |
|
| 47 | - } |
|
| 38 | + /** |
|
| 39 | + * @return void |
|
| 40 | + */ |
|
| 41 | + public function removeAutosave() |
|
| 42 | + { |
|
| 43 | + if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
| 44 | + return; |
|
| 45 | + } |
|
| 46 | + wp_deregister_script('autosave'); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @return void |
|
| 51 | - */ |
|
| 52 | - public function removeMetaBoxes() |
|
| 53 | - { |
|
| 54 | - remove_meta_box('slugdiv', Application::POST_TYPE, 'advanced'); |
|
| 55 | - } |
|
| 49 | + /** |
|
| 50 | + * @return void |
|
| 51 | + */ |
|
| 52 | + public function removeMetaBoxes() |
|
| 53 | + { |
|
| 54 | + remove_meta_box('slugdiv', Application::POST_TYPE, 'advanced'); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function removePostTypeSupport() |
|
| 61 | - { |
|
| 62 | - if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
| 63 | - return; |
|
| 64 | - } |
|
| 65 | - remove_post_type_support(Application::POST_TYPE, 'title'); |
|
| 66 | - remove_post_type_support(Application::POST_TYPE, 'editor'); |
|
| 67 | - } |
|
| 57 | + /** |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function removePostTypeSupport() |
|
| 61 | + { |
|
| 62 | + if (!$this->isReviewEditor() || $this->isReviewEditable()) { |
|
| 63 | + return; |
|
| 64 | + } |
|
| 65 | + remove_post_type_support(Application::POST_TYPE, 'title'); |
|
| 66 | + remove_post_type_support(Application::POST_TYPE, 'editor'); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @return bool |
|
| 71 | - */ |
|
| 72 | - protected function isReviewEditable() |
|
| 73 | - { |
|
| 74 | - $postId = intval(filter_input(INPUT_GET, 'post')); |
|
| 75 | - return $postId > 0 |
|
| 76 | - && 'local' == glsr(Database::class)->get($postId, 'review_type') |
|
| 77 | - && $this->isReviewEditor(); |
|
| 78 | - } |
|
| 69 | + /** |
|
| 70 | + * @return bool |
|
| 71 | + */ |
|
| 72 | + protected function isReviewEditable() |
|
| 73 | + { |
|
| 74 | + $postId = intval(filter_input(INPUT_GET, 'post')); |
|
| 75 | + return $postId > 0 |
|
| 76 | + && 'local' == glsr(Database::class)->get($postId, 'review_type') |
|
| 77 | + && $this->isReviewEditor(); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @return bool |
|
| 82 | - */ |
|
| 83 | - protected function isReviewEditor() |
|
| 84 | - { |
|
| 85 | - $screen = glsr_current_screen(); |
|
| 86 | - return 'post' == $screen->base |
|
| 87 | - && Application::POST_TYPE == $screen->id |
|
| 88 | - && Application::POST_TYPE == $screen->post_type; |
|
| 89 | - } |
|
| 80 | + /** |
|
| 81 | + * @return bool |
|
| 82 | + */ |
|
| 83 | + protected function isReviewEditor() |
|
| 84 | + { |
|
| 85 | + $screen = glsr_current_screen(); |
|
| 86 | + return 'post' == $screen->base |
|
| 87 | + && Application::POST_TYPE == $screen->id |
|
| 88 | + && Application::POST_TYPE == $screen->post_type; |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -7,40 +7,40 @@ |
||
| 7 | 7 | |
| 8 | 8 | class EmailDefaults extends Defaults |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @return array |
|
| 12 | - */ |
|
| 13 | - protected function defaults() |
|
| 14 | - { |
|
| 15 | - return [ |
|
| 16 | - 'after' => '', |
|
| 17 | - 'attachments' => [], |
|
| 18 | - 'bcc' => '', |
|
| 19 | - 'before' => '', |
|
| 20 | - 'cc' => '', |
|
| 21 | - 'from' => $this->getFromName().' <'.$this->getFromEmail().'>', |
|
| 22 | - 'message' => '', |
|
| 23 | - 'reply-to' => '', |
|
| 24 | - 'subject' => '', |
|
| 25 | - 'template' => '', |
|
| 26 | - 'template-tags' => [], |
|
| 27 | - 'to' => '', |
|
| 28 | - ]; |
|
| 29 | - } |
|
| 10 | + /** |
|
| 11 | + * @return array |
|
| 12 | + */ |
|
| 13 | + protected function defaults() |
|
| 14 | + { |
|
| 15 | + return [ |
|
| 16 | + 'after' => '', |
|
| 17 | + 'attachments' => [], |
|
| 18 | + 'bcc' => '', |
|
| 19 | + 'before' => '', |
|
| 20 | + 'cc' => '', |
|
| 21 | + 'from' => $this->getFromName().' <'.$this->getFromEmail().'>', |
|
| 22 | + 'message' => '', |
|
| 23 | + 'reply-to' => '', |
|
| 24 | + 'subject' => '', |
|
| 25 | + 'template' => '', |
|
| 26 | + 'template-tags' => [], |
|
| 27 | + 'to' => '', |
|
| 28 | + ]; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @return string |
|
| 33 | - */ |
|
| 34 | - protected function getFromEmail() |
|
| 35 | - { |
|
| 36 | - return glsr(OptionManager::class)->getWP('admin_email'); |
|
| 37 | - } |
|
| 31 | + /** |
|
| 32 | + * @return string |
|
| 33 | + */ |
|
| 34 | + protected function getFromEmail() |
|
| 35 | + { |
|
| 36 | + return glsr(OptionManager::class)->getWP('admin_email'); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - protected function getFromName() |
|
| 43 | - { |
|
| 44 | - return wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES); |
|
| 45 | - } |
|
| 39 | + /** |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + protected function getFromName() |
|
| 43 | + { |
|
| 44 | + return wp_specialchars_decode(glsr(OptionManager::class)->getWP('blogname'), ENT_QUOTES); |
|
| 45 | + } |
|
| 46 | 46 | } |
@@ -8,112 +8,112 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Notice |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @param string $type |
|
| 13 | - * @param string|array|WP_Error $message |
|
| 14 | - * @return void |
|
| 15 | - */ |
|
| 16 | - public function add($type, $message, array $args = []) |
|
| 17 | - { |
|
| 18 | - if (empty(array_filter([$message, $type]))) { |
|
| 19 | - return; |
|
| 20 | - } |
|
| 21 | - $args['message'] = $message; |
|
| 22 | - $args['type'] = $type; |
|
| 23 | - add_settings_error(Application::ID, '', json_encode($this->normalize($args))); |
|
| 24 | - } |
|
| 11 | + /** |
|
| 12 | + * @param string $type |
|
| 13 | + * @param string|array|WP_Error $message |
|
| 14 | + * @return void |
|
| 15 | + */ |
|
| 16 | + public function add($type, $message, array $args = []) |
|
| 17 | + { |
|
| 18 | + if (empty(array_filter([$message, $type]))) { |
|
| 19 | + return; |
|
| 20 | + } |
|
| 21 | + $args['message'] = $message; |
|
| 22 | + $args['type'] = $type; |
|
| 23 | + add_settings_error(Application::ID, '', json_encode($this->normalize($args))); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @param string|array|WP_Error $message |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - public function addError($message, array $args = []) |
|
| 31 | - { |
|
| 32 | - $this->add('error', $message, $args); |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * @param string|array|WP_Error $message |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + public function addError($message, array $args = []) |
|
| 31 | + { |
|
| 32 | + $this->add('error', $message, $args); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @param string|array|WP_Error $message |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 39 | - public function addSuccess($message, array $args = []) |
|
| 40 | - { |
|
| 41 | - $this->add('success', $message, $args); |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * @param string|array|WP_Error $message |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | + public function addSuccess($message, array $args = []) |
|
| 40 | + { |
|
| 41 | + $this->add('success', $message, $args); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param string|array|WP_Error $message |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - public function addWarning($message, array $args = []) |
|
| 49 | - { |
|
| 50 | - $this->add('warning', $message, $args); |
|
| 51 | - } |
|
| 44 | + /** |
|
| 45 | + * @param string|array|WP_Error $message |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + public function addWarning($message, array $args = []) |
|
| 49 | + { |
|
| 50 | + $this->add('warning', $message, $args); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return static |
|
| 55 | - */ |
|
| 56 | - public function clear() |
|
| 57 | - { |
|
| 58 | - global $wp_settings_errors; |
|
| 59 | - $wp_settings_errors = []; |
|
| 60 | - delete_transient('settings_errors'); |
|
| 61 | - return $this; |
|
| 62 | - } |
|
| 53 | + /** |
|
| 54 | + * @return static |
|
| 55 | + */ |
|
| 56 | + public function clear() |
|
| 57 | + { |
|
| 58 | + global $wp_settings_errors; |
|
| 59 | + $wp_settings_errors = []; |
|
| 60 | + delete_transient('settings_errors'); |
|
| 61 | + return $this; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @return string |
|
| 66 | - */ |
|
| 67 | - public function get() |
|
| 68 | - { |
|
| 69 | - $notices = array_map('unserialize', |
|
| 70 | - array_unique(array_map('serialize', get_settings_errors(Application::ID))) |
|
| 71 | - ); |
|
| 72 | - $notices = array_reduce($notices, function ($carry, $notice) { |
|
| 73 | - return $carry.$this->buildNotice(json_decode($notice['message'], true)); |
|
| 74 | - }); |
|
| 75 | - return apply_filters('site-reviews/notices', $notices); |
|
| 76 | - } |
|
| 64 | + /** |
|
| 65 | + * @return string |
|
| 66 | + */ |
|
| 67 | + public function get() |
|
| 68 | + { |
|
| 69 | + $notices = array_map('unserialize', |
|
| 70 | + array_unique(array_map('serialize', get_settings_errors(Application::ID))) |
|
| 71 | + ); |
|
| 72 | + $notices = array_reduce($notices, function ($carry, $notice) { |
|
| 73 | + return $carry.$this->buildNotice(json_decode($notice['message'], true)); |
|
| 74 | + }); |
|
| 75 | + return apply_filters('site-reviews/notices', $notices); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - protected function buildNotice(array $args) |
|
| 82 | - { |
|
| 83 | - $messages = array_reduce($args['messages'], function ($carry, $message) { |
|
| 84 | - return $carry.glsr(Builder::class)->p($message); |
|
| 85 | - }); |
|
| 86 | - $class = 'notice notice-'.$args['type']; |
|
| 87 | - if ($args['inline']) { |
|
| 88 | - $class.= ' inline'; |
|
| 89 | - } |
|
| 90 | - if ($args['dismissible']) { |
|
| 91 | - $class.= ' is-dismissible'; |
|
| 92 | - } |
|
| 93 | - return glsr(Builder::class)->div($messages, [ |
|
| 94 | - 'class' => $class, |
|
| 95 | - ]); |
|
| 96 | - } |
|
| 78 | + /** |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + protected function buildNotice(array $args) |
|
| 82 | + { |
|
| 83 | + $messages = array_reduce($args['messages'], function ($carry, $message) { |
|
| 84 | + return $carry.glsr(Builder::class)->p($message); |
|
| 85 | + }); |
|
| 86 | + $class = 'notice notice-'.$args['type']; |
|
| 87 | + if ($args['inline']) { |
|
| 88 | + $class.= ' inline'; |
|
| 89 | + } |
|
| 90 | + if ($args['dismissible']) { |
|
| 91 | + $class.= ' is-dismissible'; |
|
| 92 | + } |
|
| 93 | + return glsr(Builder::class)->div($messages, [ |
|
| 94 | + 'class' => $class, |
|
| 95 | + ]); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * @return array |
|
| 100 | - */ |
|
| 101 | - protected function normalize(array $args) |
|
| 102 | - { |
|
| 103 | - $defaults = [ |
|
| 104 | - 'dismissible' => true, |
|
| 105 | - 'inline' => true, |
|
| 106 | - 'message' => '', |
|
| 107 | - 'type' => '', |
|
| 108 | - ]; |
|
| 109 | - $args = shortcode_atts($defaults, $args); |
|
| 110 | - if (!in_array($args['type'], ['error', 'warning', 'success'])) { |
|
| 111 | - $args['type'] = 'success'; |
|
| 112 | - } |
|
| 113 | - $args['messages'] = is_wp_error($args['message']) |
|
| 114 | - ? (array) $args['message']->get_error_message() |
|
| 115 | - : (array) $args['message']; |
|
| 116 | - unset($args['message']); |
|
| 117 | - return $args; |
|
| 118 | - } |
|
| 98 | + /** |
|
| 99 | + * @return array |
|
| 100 | + */ |
|
| 101 | + protected function normalize(array $args) |
|
| 102 | + { |
|
| 103 | + $defaults = [ |
|
| 104 | + 'dismissible' => true, |
|
| 105 | + 'inline' => true, |
|
| 106 | + 'message' => '', |
|
| 107 | + 'type' => '', |
|
| 108 | + ]; |
|
| 109 | + $args = shortcode_atts($defaults, $args); |
|
| 110 | + if (!in_array($args['type'], ['error', 'warning', 'success'])) { |
|
| 111 | + $args['type'] = 'success'; |
|
| 112 | + } |
|
| 113 | + $args['messages'] = is_wp_error($args['message']) |
|
| 114 | + ? (array) $args['message']->get_error_message() |
|
| 115 | + : (array) $args['message']; |
|
| 116 | + unset($args['message']); |
|
| 117 | + return $args; |
|
| 118 | + } |
|
| 119 | 119 | } |
@@ -4,13 +4,13 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Password extends Field |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return array |
|
| 9 | - */ |
|
| 10 | - public static function defaults() |
|
| 11 | - { |
|
| 12 | - return [ |
|
| 13 | - 'class' => 'regular-text', |
|
| 14 | - ]; |
|
| 15 | - } |
|
| 7 | + /** |
|
| 8 | + * @return array |
|
| 9 | + */ |
|
| 10 | + public static function defaults() |
|
| 11 | + { |
|
| 12 | + return [ |
|
| 13 | + 'class' => 'regular-text', |
|
| 14 | + ]; |
|
| 15 | + } |
|
| 16 | 16 | } |
@@ -9,241 +9,241 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Field |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - public $field; |
|
| 16 | - |
|
| 17 | - public function __construct(array $field = []) |
|
| 18 | - { |
|
| 19 | - $this->field = wp_parse_args($field, [ |
|
| 20 | - 'errors' => false, |
|
| 21 | - 'is_hidden' => false, |
|
| 22 | - 'is_multi' => false, |
|
| 23 | - 'is_public' => false, |
|
| 24 | - 'is_raw' => false, |
|
| 25 | - 'is_setting' => false, |
|
| 26 | - 'is_valid' => true, |
|
| 27 | - 'is_widget' => false, |
|
| 28 | - 'path' => '', |
|
| 29 | - ]); |
|
| 30 | - $this->normalize(); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @return string |
|
| 35 | - */ |
|
| 36 | - public function __toString() |
|
| 37 | - { |
|
| 38 | - return (string) $this->build(); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @return void|string |
|
| 43 | - */ |
|
| 44 | - public function build() |
|
| 45 | - { |
|
| 46 | - if (!$this->field['is_valid']) { |
|
| 47 | - return; |
|
| 48 | - } |
|
| 49 | - if ($this->field['is_raw']) { |
|
| 50 | - return glsr(Builder::class)->{$this->field['type']}($this->field); |
|
| 51 | - } |
|
| 52 | - if (!$this->field['is_setting']) { |
|
| 53 | - return $this->buildField(); |
|
| 54 | - } |
|
| 55 | - if (!$this->field['is_multi']) { |
|
| 56 | - return $this->buildSettingField(); |
|
| 57 | - } |
|
| 58 | - return $this->buildSettingMultiField(); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - public function render() |
|
| 65 | - { |
|
| 66 | - echo $this->build(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - protected function buildField() |
|
| 73 | - { |
|
| 74 | - $field = glsr(Template::class)->build('templates/form/field_'.$this->field['type'], [ |
|
| 75 | - 'context' => [ |
|
| 76 | - 'class' => $this->getFieldClass(), |
|
| 77 | - 'errors' => $this->getFieldErrors(), |
|
| 78 | - 'field' => glsr(Builder::class)->raw($this->field), |
|
| 79 | - 'label' => glsr(Builder::class)->label([ |
|
| 80 | - 'class' => 'glsr-'.$this->field['type'].'-label', |
|
| 81 | - 'for' => $this->field['id'], |
|
| 82 | - 'is_public' => $this->field['is_public'], |
|
| 83 | - 'text' => $this->field['label'].'<span></span>', |
|
| 84 | - 'type' => $this->field['type'], |
|
| 85 | - ]), |
|
| 86 | - ], |
|
| 87 | - 'field' => $this->field, |
|
| 88 | - ]); |
|
| 89 | - return apply_filters('site-reviews/rendered/field', $field, $this->field['type'], $this->field); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @return string |
|
| 94 | - */ |
|
| 95 | - protected function buildSettingField() |
|
| 96 | - { |
|
| 97 | - return glsr(Template::class)->build('partials/form/table-row', [ |
|
| 98 | - 'context' => [ |
|
| 99 | - 'class' => $this->getFieldClass(), |
|
| 100 | - 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
| 101 | - 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
| 102 | - ], |
|
| 103 | - 'field' => $this->field, |
|
| 104 | - ]); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @return string |
|
| 109 | - */ |
|
| 110 | - protected function buildSettingMultiField() |
|
| 111 | - { |
|
| 112 | - $dependsOn = $this->getFieldDependsOn(); |
|
| 113 | - unset($this->field['data-depends']); |
|
| 114 | - return glsr(Template::class)->build('partials/form/table-row-multiple', [ |
|
| 115 | - 'context' => [ |
|
| 116 | - 'class' => $this->getFieldClass(), |
|
| 117 | - 'depends_on' => $dependsOn, |
|
| 118 | - 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
| 119 | - 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
| 120 | - 'legend' => $this->field['legend'], |
|
| 121 | - ], |
|
| 122 | - 'field' => $this->field, |
|
| 123 | - ]); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return string |
|
| 128 | - */ |
|
| 129 | - protected function getFieldClass() |
|
| 130 | - { |
|
| 131 | - $classes = []; |
|
| 132 | - if (!empty($this->field['errors'])) { |
|
| 133 | - $classes[] = 'glsr-has-error'; |
|
| 134 | - } |
|
| 135 | - if ($this->field['is_hidden']) { |
|
| 136 | - $classes[] = 'hidden'; |
|
| 137 | - } |
|
| 138 | - if (!empty($this->field['required'])) { |
|
| 139 | - $classes[] = 'glsr-required'; |
|
| 140 | - } |
|
| 141 | - $classes = apply_filters('site-reviews/rendered/field/classes', $classes, $this->field); |
|
| 142 | - return implode(' ', $classes); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return string |
|
| 147 | - */ |
|
| 148 | - protected function getFieldDependsOn() |
|
| 149 | - { |
|
| 150 | - return !empty($this->field['data-depends']) |
|
| 151 | - ? $this->field['data-depends'] |
|
| 152 | - : ''; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @return void|string |
|
| 157 | - */ |
|
| 158 | - protected function getFieldErrors() |
|
| 159 | - { |
|
| 160 | - if (empty($this->field['errors']) || !is_array($this->field['errors'])) { |
|
| 161 | - return; |
|
| 162 | - } |
|
| 163 | - $errors = array_reduce($this->field['errors'], function ($carry, $error) { |
|
| 164 | - return $carry.glsr(Builder::class)->span($error, ['class' => 'glsr-field-error']); |
|
| 165 | - }); |
|
| 166 | - return glsr(Template::class)->build('templates/form/field-errors', [ |
|
| 167 | - 'context' => [ |
|
| 168 | - 'errors' => $errors, |
|
| 169 | - ], |
|
| 170 | - 'field' => $this->field, |
|
| 171 | - ]); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @return string |
|
| 176 | - */ |
|
| 177 | - protected function getFieldPrefix() |
|
| 178 | - { |
|
| 179 | - return $this->field['is_setting'] |
|
| 180 | - ? OptionManager::databaseKey() |
|
| 181 | - : Application::ID; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @return bool |
|
| 186 | - */ |
|
| 187 | - protected function isFieldValid() |
|
| 188 | - { |
|
| 189 | - $missingValues = []; |
|
| 190 | - $requiredValues = [ |
|
| 191 | - 'name', 'type', |
|
| 192 | - ]; |
|
| 193 | - foreach ($requiredValues as $value) { |
|
| 194 | - if (isset($this->field[$value])) { |
|
| 195 | - continue; |
|
| 196 | - } |
|
| 197 | - $missingValues[] = $value; |
|
| 198 | - $this->field['is_valid'] = false; |
|
| 199 | - } |
|
| 200 | - if (!empty($missingValues)) { |
|
| 201 | - glsr_log() |
|
| 202 | - ->warning('Field is missing: '.implode(', ', $missingValues)) |
|
| 203 | - ->debug($this->field); |
|
| 204 | - } |
|
| 205 | - return $this->field['is_valid']; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @return void |
|
| 210 | - */ |
|
| 211 | - protected function normalize() |
|
| 212 | - { |
|
| 213 | - if (!$this->isFieldValid()) { |
|
| 214 | - return; |
|
| 215 | - } |
|
| 216 | - $this->field['path'] = $this->field['name']; |
|
| 217 | - $className = Helper::buildClassName($this->field['type'], __NAMESPACE__.'\Fields'); |
|
| 218 | - if (class_exists($className)) { |
|
| 219 | - $this->field = $className::merge($this->field); |
|
| 220 | - } |
|
| 221 | - $this->normalizeFieldId(); |
|
| 222 | - $this->normalizeFieldName(); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @return void |
|
| 227 | - */ |
|
| 228 | - protected function normalizeFieldId() |
|
| 229 | - { |
|
| 230 | - if (isset($this->field['id']) || $this->field['is_raw']) { |
|
| 231 | - return; |
|
| 232 | - } |
|
| 233 | - $this->field['id'] = Str::convertPathToId( |
|
| 234 | - $this->field['path'], |
|
| 235 | - $this->getFieldPrefix() |
|
| 236 | - ); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @return void |
|
| 241 | - */ |
|
| 242 | - protected function normalizeFieldName() |
|
| 243 | - { |
|
| 244 | - $this->field['name'] = Str::convertPathToName( |
|
| 245 | - $this->field['path'], |
|
| 246 | - $this->getFieldPrefix() |
|
| 247 | - ); |
|
| 248 | - } |
|
| 12 | + /** |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + public $field; |
|
| 16 | + |
|
| 17 | + public function __construct(array $field = []) |
|
| 18 | + { |
|
| 19 | + $this->field = wp_parse_args($field, [ |
|
| 20 | + 'errors' => false, |
|
| 21 | + 'is_hidden' => false, |
|
| 22 | + 'is_multi' => false, |
|
| 23 | + 'is_public' => false, |
|
| 24 | + 'is_raw' => false, |
|
| 25 | + 'is_setting' => false, |
|
| 26 | + 'is_valid' => true, |
|
| 27 | + 'is_widget' => false, |
|
| 28 | + 'path' => '', |
|
| 29 | + ]); |
|
| 30 | + $this->normalize(); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @return string |
|
| 35 | + */ |
|
| 36 | + public function __toString() |
|
| 37 | + { |
|
| 38 | + return (string) $this->build(); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @return void|string |
|
| 43 | + */ |
|
| 44 | + public function build() |
|
| 45 | + { |
|
| 46 | + if (!$this->field['is_valid']) { |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | + if ($this->field['is_raw']) { |
|
| 50 | + return glsr(Builder::class)->{$this->field['type']}($this->field); |
|
| 51 | + } |
|
| 52 | + if (!$this->field['is_setting']) { |
|
| 53 | + return $this->buildField(); |
|
| 54 | + } |
|
| 55 | + if (!$this->field['is_multi']) { |
|
| 56 | + return $this->buildSettingField(); |
|
| 57 | + } |
|
| 58 | + return $this->buildSettingMultiField(); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + public function render() |
|
| 65 | + { |
|
| 66 | + echo $this->build(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + protected function buildField() |
|
| 73 | + { |
|
| 74 | + $field = glsr(Template::class)->build('templates/form/field_'.$this->field['type'], [ |
|
| 75 | + 'context' => [ |
|
| 76 | + 'class' => $this->getFieldClass(), |
|
| 77 | + 'errors' => $this->getFieldErrors(), |
|
| 78 | + 'field' => glsr(Builder::class)->raw($this->field), |
|
| 79 | + 'label' => glsr(Builder::class)->label([ |
|
| 80 | + 'class' => 'glsr-'.$this->field['type'].'-label', |
|
| 81 | + 'for' => $this->field['id'], |
|
| 82 | + 'is_public' => $this->field['is_public'], |
|
| 83 | + 'text' => $this->field['label'].'<span></span>', |
|
| 84 | + 'type' => $this->field['type'], |
|
| 85 | + ]), |
|
| 86 | + ], |
|
| 87 | + 'field' => $this->field, |
|
| 88 | + ]); |
|
| 89 | + return apply_filters('site-reviews/rendered/field', $field, $this->field['type'], $this->field); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @return string |
|
| 94 | + */ |
|
| 95 | + protected function buildSettingField() |
|
| 96 | + { |
|
| 97 | + return glsr(Template::class)->build('partials/form/table-row', [ |
|
| 98 | + 'context' => [ |
|
| 99 | + 'class' => $this->getFieldClass(), |
|
| 100 | + 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
| 101 | + 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
| 102 | + ], |
|
| 103 | + 'field' => $this->field, |
|
| 104 | + ]); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @return string |
|
| 109 | + */ |
|
| 110 | + protected function buildSettingMultiField() |
|
| 111 | + { |
|
| 112 | + $dependsOn = $this->getFieldDependsOn(); |
|
| 113 | + unset($this->field['data-depends']); |
|
| 114 | + return glsr(Template::class)->build('partials/form/table-row-multiple', [ |
|
| 115 | + 'context' => [ |
|
| 116 | + 'class' => $this->getFieldClass(), |
|
| 117 | + 'depends_on' => $dependsOn, |
|
| 118 | + 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
| 119 | + 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
| 120 | + 'legend' => $this->field['legend'], |
|
| 121 | + ], |
|
| 122 | + 'field' => $this->field, |
|
| 123 | + ]); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return string |
|
| 128 | + */ |
|
| 129 | + protected function getFieldClass() |
|
| 130 | + { |
|
| 131 | + $classes = []; |
|
| 132 | + if (!empty($this->field['errors'])) { |
|
| 133 | + $classes[] = 'glsr-has-error'; |
|
| 134 | + } |
|
| 135 | + if ($this->field['is_hidden']) { |
|
| 136 | + $classes[] = 'hidden'; |
|
| 137 | + } |
|
| 138 | + if (!empty($this->field['required'])) { |
|
| 139 | + $classes[] = 'glsr-required'; |
|
| 140 | + } |
|
| 141 | + $classes = apply_filters('site-reviews/rendered/field/classes', $classes, $this->field); |
|
| 142 | + return implode(' ', $classes); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return string |
|
| 147 | + */ |
|
| 148 | + protected function getFieldDependsOn() |
|
| 149 | + { |
|
| 150 | + return !empty($this->field['data-depends']) |
|
| 151 | + ? $this->field['data-depends'] |
|
| 152 | + : ''; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @return void|string |
|
| 157 | + */ |
|
| 158 | + protected function getFieldErrors() |
|
| 159 | + { |
|
| 160 | + if (empty($this->field['errors']) || !is_array($this->field['errors'])) { |
|
| 161 | + return; |
|
| 162 | + } |
|
| 163 | + $errors = array_reduce($this->field['errors'], function ($carry, $error) { |
|
| 164 | + return $carry.glsr(Builder::class)->span($error, ['class' => 'glsr-field-error']); |
|
| 165 | + }); |
|
| 166 | + return glsr(Template::class)->build('templates/form/field-errors', [ |
|
| 167 | + 'context' => [ |
|
| 168 | + 'errors' => $errors, |
|
| 169 | + ], |
|
| 170 | + 'field' => $this->field, |
|
| 171 | + ]); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @return string |
|
| 176 | + */ |
|
| 177 | + protected function getFieldPrefix() |
|
| 178 | + { |
|
| 179 | + return $this->field['is_setting'] |
|
| 180 | + ? OptionManager::databaseKey() |
|
| 181 | + : Application::ID; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @return bool |
|
| 186 | + */ |
|
| 187 | + protected function isFieldValid() |
|
| 188 | + { |
|
| 189 | + $missingValues = []; |
|
| 190 | + $requiredValues = [ |
|
| 191 | + 'name', 'type', |
|
| 192 | + ]; |
|
| 193 | + foreach ($requiredValues as $value) { |
|
| 194 | + if (isset($this->field[$value])) { |
|
| 195 | + continue; |
|
| 196 | + } |
|
| 197 | + $missingValues[] = $value; |
|
| 198 | + $this->field['is_valid'] = false; |
|
| 199 | + } |
|
| 200 | + if (!empty($missingValues)) { |
|
| 201 | + glsr_log() |
|
| 202 | + ->warning('Field is missing: '.implode(', ', $missingValues)) |
|
| 203 | + ->debug($this->field); |
|
| 204 | + } |
|
| 205 | + return $this->field['is_valid']; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @return void |
|
| 210 | + */ |
|
| 211 | + protected function normalize() |
|
| 212 | + { |
|
| 213 | + if (!$this->isFieldValid()) { |
|
| 214 | + return; |
|
| 215 | + } |
|
| 216 | + $this->field['path'] = $this->field['name']; |
|
| 217 | + $className = Helper::buildClassName($this->field['type'], __NAMESPACE__.'\Fields'); |
|
| 218 | + if (class_exists($className)) { |
|
| 219 | + $this->field = $className::merge($this->field); |
|
| 220 | + } |
|
| 221 | + $this->normalizeFieldId(); |
|
| 222 | + $this->normalizeFieldName(); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @return void |
|
| 227 | + */ |
|
| 228 | + protected function normalizeFieldId() |
|
| 229 | + { |
|
| 230 | + if (isset($this->field['id']) || $this->field['is_raw']) { |
|
| 231 | + return; |
|
| 232 | + } |
|
| 233 | + $this->field['id'] = Str::convertPathToId( |
|
| 234 | + $this->field['path'], |
|
| 235 | + $this->getFieldPrefix() |
|
| 236 | + ); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @return void |
|
| 241 | + */ |
|
| 242 | + protected function normalizeFieldName() |
|
| 243 | + { |
|
| 244 | + $this->field['name'] = Str::convertPathToName( |
|
| 245 | + $this->field['path'], |
|
| 246 | + $this->getFieldPrefix() |
|
| 247 | + ); |
|
| 248 | + } |
|
| 249 | 249 | } |
@@ -6,285 +6,285 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Attributes |
| 8 | 8 | { |
| 9 | - const ATTRIBUTES_A = [ |
|
| 10 | - 'download', 'href', 'hreflang', 'ping', 'referrerpolicy', 'rel', 'target', 'type', |
|
| 11 | - ]; |
|
| 9 | + const ATTRIBUTES_A = [ |
|
| 10 | + 'download', 'href', 'hreflang', 'ping', 'referrerpolicy', 'rel', 'target', 'type', |
|
| 11 | + ]; |
|
| 12 | 12 | |
| 13 | - const ATTRIBUTES_BUTTON = [ |
|
| 14 | - 'autofocus', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
|
| 15 | - 'formnovalidate', 'formtarget', 'name', 'type', 'value', |
|
| 16 | - ]; |
|
| 13 | + const ATTRIBUTES_BUTTON = [ |
|
| 14 | + 'autofocus', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
|
| 15 | + 'formnovalidate', 'formtarget', 'name', 'type', 'value', |
|
| 16 | + ]; |
|
| 17 | 17 | |
| 18 | - const ATTRIBUTES_FORM = [ |
|
| 19 | - 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', 'method', |
|
| 20 | - 'name', 'novalidate', 'target', |
|
| 21 | - ]; |
|
| 18 | + const ATTRIBUTES_FORM = [ |
|
| 19 | + 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', 'method', |
|
| 20 | + 'name', 'novalidate', 'target', |
|
| 21 | + ]; |
|
| 22 | 22 | |
| 23 | - const ATTRIBUTES_IMG = [ |
|
| 24 | - 'alt', 'crossorigin', 'decoding', 'height', 'ismap', 'referrerpolicy', 'sizes', 'src', |
|
| 25 | - 'srcset', 'width', 'usemap', |
|
| 26 | - ]; |
|
| 23 | + const ATTRIBUTES_IMG = [ |
|
| 24 | + 'alt', 'crossorigin', 'decoding', 'height', 'ismap', 'referrerpolicy', 'sizes', 'src', |
|
| 25 | + 'srcset', 'width', 'usemap', |
|
| 26 | + ]; |
|
| 27 | 27 | |
| 28 | - const ATTRIBUTES_INPUT = [ |
|
| 29 | - 'accept', 'autocomplete', 'autocorrect', 'autofocus', 'capture', 'checked', 'disabled', |
|
| 30 | - 'form', 'formaction', 'formenctype', 'formmethod', 'formnovalidate', 'formtarget', 'height', |
|
| 31 | - 'incremental', 'inputmode', 'list', 'max', 'maxlength', 'min', 'minlength', 'multiple', |
|
| 32 | - 'name', 'pattern', 'placeholder', 'readonly', 'results', 'required', 'selectionDirection', |
|
| 33 | - 'selectionEnd', 'selectionStart', 'size', 'spellcheck', 'src', 'step', 'tabindex', 'type', |
|
| 34 | - 'value', 'webkitdirectory', 'width', |
|
| 35 | - ]; |
|
| 28 | + const ATTRIBUTES_INPUT = [ |
|
| 29 | + 'accept', 'autocomplete', 'autocorrect', 'autofocus', 'capture', 'checked', 'disabled', |
|
| 30 | + 'form', 'formaction', 'formenctype', 'formmethod', 'formnovalidate', 'formtarget', 'height', |
|
| 31 | + 'incremental', 'inputmode', 'list', 'max', 'maxlength', 'min', 'minlength', 'multiple', |
|
| 32 | + 'name', 'pattern', 'placeholder', 'readonly', 'results', 'required', 'selectionDirection', |
|
| 33 | + 'selectionEnd', 'selectionStart', 'size', 'spellcheck', 'src', 'step', 'tabindex', 'type', |
|
| 34 | + 'value', 'webkitdirectory', 'width', |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - const ATTRIBUTES_LABEL = [ |
|
| 38 | - 'for', |
|
| 39 | - ]; |
|
| 37 | + const ATTRIBUTES_LABEL = [ |
|
| 38 | + 'for', |
|
| 39 | + ]; |
|
| 40 | 40 | |
| 41 | - const ATTRIBUTES_OPTION = [ |
|
| 42 | - 'disabled', 'label', 'selected', 'value', |
|
| 43 | - ]; |
|
| 41 | + const ATTRIBUTES_OPTION = [ |
|
| 42 | + 'disabled', 'label', 'selected', 'value', |
|
| 43 | + ]; |
|
| 44 | 44 | |
| 45 | - const ATTRIBUTES_SELECT = [ |
|
| 46 | - 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
|
| 47 | - ]; |
|
| 45 | + const ATTRIBUTES_SELECT = [ |
|
| 46 | + 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
|
| 47 | + ]; |
|
| 48 | 48 | |
| 49 | - const ATTRIBUTES_TEXTAREA = [ |
|
| 50 | - 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
|
| 51 | - 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', 'spellcheck', 'wrap', |
|
| 52 | - ]; |
|
| 49 | + const ATTRIBUTES_TEXTAREA = [ |
|
| 50 | + 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
|
| 51 | + 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', 'spellcheck', 'wrap', |
|
| 52 | + ]; |
|
| 53 | 53 | |
| 54 | - const BOOLEAN_ATTRIBUTES = [ |
|
| 55 | - 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
|
| 56 | - 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
|
| 57 | - 'webkitdirectory', |
|
| 58 | - ]; |
|
| 54 | + const BOOLEAN_ATTRIBUTES = [ |
|
| 55 | + 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
|
| 56 | + 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
|
| 57 | + 'webkitdirectory', |
|
| 58 | + ]; |
|
| 59 | 59 | |
| 60 | - const GLOBAL_ATTRIBUTES = [ |
|
| 61 | - 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
|
| 62 | - 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
|
| 63 | - ]; |
|
| 60 | + const GLOBAL_ATTRIBUTES = [ |
|
| 61 | + 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
|
| 62 | + 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
|
| 63 | + ]; |
|
| 64 | 64 | |
| 65 | - const GLOBAL_WILDCARD_ATTRIBUTES = [ |
|
| 66 | - 'aria-', 'data-', 'item', 'on', |
|
| 67 | - ]; |
|
| 65 | + const GLOBAL_WILDCARD_ATTRIBUTES = [ |
|
| 66 | + 'aria-', 'data-', 'item', 'on', |
|
| 67 | + ]; |
|
| 68 | 68 | |
| 69 | - const INPUT_TYPES = [ |
|
| 70 | - 'button', 'checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', |
|
| 71 | - 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', |
|
| 72 | - 'time', 'url', 'week', |
|
| 73 | - ]; |
|
| 69 | + const INPUT_TYPES = [ |
|
| 70 | + 'button', 'checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', |
|
| 71 | + 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', |
|
| 72 | + 'time', 'url', 'week', |
|
| 73 | + ]; |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @var array |
|
| 77 | - */ |
|
| 78 | - protected $attributes = []; |
|
| 75 | + /** |
|
| 76 | + * @var array |
|
| 77 | + */ |
|
| 78 | + protected $attributes = []; |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @param string $method |
|
| 82 | - * @param array $args |
|
| 83 | - * @return static |
|
| 84 | - */ |
|
| 85 | - public function __call($method, $args) |
|
| 86 | - { |
|
| 87 | - $args += [[], false]; |
|
| 88 | - $constant = 'static::ATTRIBUTES_'.strtoupper($method); |
|
| 89 | - $allowedAttributeKeys = defined($constant) |
|
| 90 | - ? constant($constant) |
|
| 91 | - : []; |
|
| 92 | - $this->normalize((array) $args[0], $allowedAttributeKeys); |
|
| 93 | - $this->normalizeInputType($method); |
|
| 94 | - return $this; |
|
| 95 | - } |
|
| 80 | + /** |
|
| 81 | + * @param string $method |
|
| 82 | + * @param array $args |
|
| 83 | + * @return static |
|
| 84 | + */ |
|
| 85 | + public function __call($method, $args) |
|
| 86 | + { |
|
| 87 | + $args += [[], false]; |
|
| 88 | + $constant = 'static::ATTRIBUTES_'.strtoupper($method); |
|
| 89 | + $allowedAttributeKeys = defined($constant) |
|
| 90 | + ? constant($constant) |
|
| 91 | + : []; |
|
| 92 | + $this->normalize((array) $args[0], $allowedAttributeKeys); |
|
| 93 | + $this->normalizeInputType($method); |
|
| 94 | + return $this; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - public function toArray() |
|
| 101 | - { |
|
| 102 | - return $this->attributes; |
|
| 103 | - } |
|
| 97 | + /** |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + public function toArray() |
|
| 101 | + { |
|
| 102 | + return $this->attributes; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public function toString() |
|
| 109 | - { |
|
| 110 | - $attributes = []; |
|
| 111 | - foreach ($this->attributes as $attribute => $value) { |
|
| 112 | - $quote = $this->getQuoteChar($attribute); |
|
| 113 | - $attributes[] = in_array($attribute, static::BOOLEAN_ATTRIBUTES) |
|
| 114 | - ? $attribute |
|
| 115 | - : $attribute.'='.$quote.implode(',', (array) $value).$quote; |
|
| 116 | - } |
|
| 117 | - return implode(' ', $attributes); |
|
| 118 | - } |
|
| 105 | + /** |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public function toString() |
|
| 109 | + { |
|
| 110 | + $attributes = []; |
|
| 111 | + foreach ($this->attributes as $attribute => $value) { |
|
| 112 | + $quote = $this->getQuoteChar($attribute); |
|
| 113 | + $attributes[] = in_array($attribute, static::BOOLEAN_ATTRIBUTES) |
|
| 114 | + ? $attribute |
|
| 115 | + : $attribute.'='.$quote.implode(',', (array) $value).$quote; |
|
| 116 | + } |
|
| 117 | + return implode(' ', $attributes); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * @return array |
|
| 122 | - */ |
|
| 123 | - protected function filterAttributes(array $allowedAttributeKeys) |
|
| 124 | - { |
|
| 125 | - return array_intersect_key($this->attributes, array_flip($allowedAttributeKeys)); |
|
| 126 | - } |
|
| 120 | + /** |
|
| 121 | + * @return array |
|
| 122 | + */ |
|
| 123 | + protected function filterAttributes(array $allowedAttributeKeys) |
|
| 124 | + { |
|
| 125 | + return array_intersect_key($this->attributes, array_flip($allowedAttributeKeys)); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * @return array |
|
| 130 | - */ |
|
| 131 | - protected function filterGlobalAttributes() |
|
| 132 | - { |
|
| 133 | - $globalAttributes = $this->filterAttributes(static::GLOBAL_ATTRIBUTES); |
|
| 134 | - $wildcards = []; |
|
| 135 | - foreach (static::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
| 136 | - $newWildcards = array_filter($this->attributes, function ($key) use ($wildcard) { |
|
| 137 | - return Str::startsWith($wildcard, $key); |
|
| 138 | - }, ARRAY_FILTER_USE_KEY); |
|
| 139 | - $wildcards = array_merge($wildcards, $newWildcards); |
|
| 140 | - } |
|
| 141 | - return array_merge($globalAttributes, $wildcards); |
|
| 142 | - } |
|
| 128 | + /** |
|
| 129 | + * @return array |
|
| 130 | + */ |
|
| 131 | + protected function filterGlobalAttributes() |
|
| 132 | + { |
|
| 133 | + $globalAttributes = $this->filterAttributes(static::GLOBAL_ATTRIBUTES); |
|
| 134 | + $wildcards = []; |
|
| 135 | + foreach (static::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
| 136 | + $newWildcards = array_filter($this->attributes, function ($key) use ($wildcard) { |
|
| 137 | + return Str::startsWith($wildcard, $key); |
|
| 138 | + }, ARRAY_FILTER_USE_KEY); |
|
| 139 | + $wildcards = array_merge($wildcards, $newWildcards); |
|
| 140 | + } |
|
| 141 | + return array_merge($globalAttributes, $wildcards); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * @return array |
|
| 146 | - */ |
|
| 147 | - protected function getPermanentAttributes() |
|
| 148 | - { |
|
| 149 | - $permanentAttributes = []; |
|
| 150 | - if (array_key_exists('value', $this->attributes)) { |
|
| 151 | - $permanentAttributes['value'] = $this->attributes['value']; |
|
| 152 | - } |
|
| 153 | - return $permanentAttributes; |
|
| 154 | - } |
|
| 144 | + /** |
|
| 145 | + * @return array |
|
| 146 | + */ |
|
| 147 | + protected function getPermanentAttributes() |
|
| 148 | + { |
|
| 149 | + $permanentAttributes = []; |
|
| 150 | + if (array_key_exists('value', $this->attributes)) { |
|
| 151 | + $permanentAttributes['value'] = $this->attributes['value']; |
|
| 152 | + } |
|
| 153 | + return $permanentAttributes; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * @param string $attribute |
|
| 158 | - * @return string |
|
| 159 | - */ |
|
| 160 | - protected function getQuoteChar($attribute) |
|
| 161 | - { |
|
| 162 | - return Str::startsWith('data-', $attribute) |
|
| 163 | - ? '\'' |
|
| 164 | - : '"'; |
|
| 165 | - } |
|
| 156 | + /** |
|
| 157 | + * @param string $attribute |
|
| 158 | + * @return string |
|
| 159 | + */ |
|
| 160 | + protected function getQuoteChar($attribute) |
|
| 161 | + { |
|
| 162 | + return Str::startsWith('data-', $attribute) |
|
| 163 | + ? '\'' |
|
| 164 | + : '"'; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param string $key |
|
| 169 | - * @param mixed $value |
|
| 170 | - * @return bool |
|
| 171 | - */ |
|
| 172 | - protected function isAttributeKeyNumeric($key, $value) |
|
| 173 | - { |
|
| 174 | - return is_string($value) |
|
| 175 | - && is_numeric($key) |
|
| 176 | - && !array_key_exists($value, $this->attributes); |
|
| 177 | - } |
|
| 167 | + /** |
|
| 168 | + * @param string $key |
|
| 169 | + * @param mixed $value |
|
| 170 | + * @return bool |
|
| 171 | + */ |
|
| 172 | + protected function isAttributeKeyNumeric($key, $value) |
|
| 173 | + { |
|
| 174 | + return is_string($value) |
|
| 175 | + && is_numeric($key) |
|
| 176 | + && !array_key_exists($value, $this->attributes); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * @return void |
|
| 181 | - */ |
|
| 182 | - protected function normalize(array $args, array $allowedAttributeKeys) |
|
| 183 | - { |
|
| 184 | - $this->attributes = array_change_key_case($args, CASE_LOWER); |
|
| 185 | - $this->normalizeBooleanAttributes(); |
|
| 186 | - $this->normalizeDataAttributes(); |
|
| 187 | - $this->normalizeStringAttributes(); |
|
| 188 | - $this->removeEmptyAttributes(); |
|
| 189 | - $this->removeIndexedAttributes(); |
|
| 190 | - $this->attributes = array_merge( |
|
| 191 | - $this->filterGlobalAttributes(), |
|
| 192 | - $this->filterAttributes($allowedAttributeKeys) |
|
| 193 | - ); |
|
| 194 | - } |
|
| 179 | + /** |
|
| 180 | + * @return void |
|
| 181 | + */ |
|
| 182 | + protected function normalize(array $args, array $allowedAttributeKeys) |
|
| 183 | + { |
|
| 184 | + $this->attributes = array_change_key_case($args, CASE_LOWER); |
|
| 185 | + $this->normalizeBooleanAttributes(); |
|
| 186 | + $this->normalizeDataAttributes(); |
|
| 187 | + $this->normalizeStringAttributes(); |
|
| 188 | + $this->removeEmptyAttributes(); |
|
| 189 | + $this->removeIndexedAttributes(); |
|
| 190 | + $this->attributes = array_merge( |
|
| 191 | + $this->filterGlobalAttributes(), |
|
| 192 | + $this->filterAttributes($allowedAttributeKeys) |
|
| 193 | + ); |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * @return void |
|
| 198 | - */ |
|
| 199 | - protected function normalizeBooleanAttributes() |
|
| 200 | - { |
|
| 201 | - foreach ($this->attributes as $key => $value) { |
|
| 202 | - if ($this->isAttributeKeyNumeric($key, $value)) { |
|
| 203 | - $key = $value; |
|
| 204 | - $value = true; |
|
| 205 | - } |
|
| 206 | - if (!in_array($key, static::BOOLEAN_ATTRIBUTES)) { |
|
| 207 | - continue; |
|
| 208 | - } |
|
| 209 | - $this->attributes[$key] = wp_validate_boolean($value); |
|
| 210 | - } |
|
| 211 | - } |
|
| 196 | + /** |
|
| 197 | + * @return void |
|
| 198 | + */ |
|
| 199 | + protected function normalizeBooleanAttributes() |
|
| 200 | + { |
|
| 201 | + foreach ($this->attributes as $key => $value) { |
|
| 202 | + if ($this->isAttributeKeyNumeric($key, $value)) { |
|
| 203 | + $key = $value; |
|
| 204 | + $value = true; |
|
| 205 | + } |
|
| 206 | + if (!in_array($key, static::BOOLEAN_ATTRIBUTES)) { |
|
| 207 | + continue; |
|
| 208 | + } |
|
| 209 | + $this->attributes[$key] = wp_validate_boolean($value); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * @return void |
|
| 215 | - */ |
|
| 216 | - protected function normalizeDataAttributes() |
|
| 217 | - { |
|
| 218 | - foreach ($this->attributes as $key => $value) { |
|
| 219 | - if ($this->isAttributeKeyNumeric($key, $value)) { |
|
| 220 | - $key = $value; |
|
| 221 | - $value = ''; |
|
| 222 | - } |
|
| 223 | - if (!Str::startsWith('data-', $key)) { |
|
| 224 | - continue; |
|
| 225 | - } |
|
| 226 | - if (is_array($value)) { |
|
| 227 | - $value = json_encode($value, JSON_HEX_APOS | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
| 228 | - } |
|
| 229 | - $this->attributes[$key] = $value; |
|
| 230 | - } |
|
| 231 | - } |
|
| 213 | + /** |
|
| 214 | + * @return void |
|
| 215 | + */ |
|
| 216 | + protected function normalizeDataAttributes() |
|
| 217 | + { |
|
| 218 | + foreach ($this->attributes as $key => $value) { |
|
| 219 | + if ($this->isAttributeKeyNumeric($key, $value)) { |
|
| 220 | + $key = $value; |
|
| 221 | + $value = ''; |
|
| 222 | + } |
|
| 223 | + if (!Str::startsWith('data-', $key)) { |
|
| 224 | + continue; |
|
| 225 | + } |
|
| 226 | + if (is_array($value)) { |
|
| 227 | + $value = json_encode($value, JSON_HEX_APOS | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
| 228 | + } |
|
| 229 | + $this->attributes[$key] = $value; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * @return void |
|
| 235 | - */ |
|
| 236 | - protected function normalizeStringAttributes() |
|
| 237 | - { |
|
| 238 | - foreach ($this->attributes as $key => $value) { |
|
| 239 | - if (!is_string($value)) { |
|
| 240 | - continue; |
|
| 241 | - } |
|
| 242 | - $this->attributes[$key] = trim($value); |
|
| 243 | - } |
|
| 244 | - } |
|
| 233 | + /** |
|
| 234 | + * @return void |
|
| 235 | + */ |
|
| 236 | + protected function normalizeStringAttributes() |
|
| 237 | + { |
|
| 238 | + foreach ($this->attributes as $key => $value) { |
|
| 239 | + if (!is_string($value)) { |
|
| 240 | + continue; |
|
| 241 | + } |
|
| 242 | + $this->attributes[$key] = trim($value); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * @param string $method |
|
| 248 | - * @return void |
|
| 249 | - */ |
|
| 250 | - protected function normalizeInputType($method) |
|
| 251 | - { |
|
| 252 | - if ('input' != $method) { |
|
| 253 | - return; |
|
| 254 | - } |
|
| 255 | - $attributes = wp_parse_args($this->attributes, ['type' => '']); |
|
| 256 | - if (!in_array($attributes['type'], static::INPUT_TYPES)) { |
|
| 257 | - $this->attributes['type'] = 'text'; |
|
| 258 | - } |
|
| 259 | - } |
|
| 246 | + /** |
|
| 247 | + * @param string $method |
|
| 248 | + * @return void |
|
| 249 | + */ |
|
| 250 | + protected function normalizeInputType($method) |
|
| 251 | + { |
|
| 252 | + if ('input' != $method) { |
|
| 253 | + return; |
|
| 254 | + } |
|
| 255 | + $attributes = wp_parse_args($this->attributes, ['type' => '']); |
|
| 256 | + if (!in_array($attributes['type'], static::INPUT_TYPES)) { |
|
| 257 | + $this->attributes['type'] = 'text'; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * @return void |
|
| 263 | - */ |
|
| 264 | - protected function removeEmptyAttributes() |
|
| 265 | - { |
|
| 266 | - $attributes = $this->attributes; |
|
| 267 | - $permanentAttributes = $this->getPermanentAttributes(); |
|
| 268 | - foreach ($this->attributes as $key => $value) { |
|
| 269 | - if (in_array($key, static::BOOLEAN_ATTRIBUTES) && !$value) { |
|
| 270 | - unset($attributes[$key]); |
|
| 271 | - } |
|
| 272 | - if (Str::startsWith('data-', $key)) { |
|
| 273 | - $permanentAttributes[$key] = $value; |
|
| 274 | - unset($attributes[$key]); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - $this->attributes = array_merge(array_filter($attributes), $permanentAttributes); |
|
| 278 | - } |
|
| 261 | + /** |
|
| 262 | + * @return void |
|
| 263 | + */ |
|
| 264 | + protected function removeEmptyAttributes() |
|
| 265 | + { |
|
| 266 | + $attributes = $this->attributes; |
|
| 267 | + $permanentAttributes = $this->getPermanentAttributes(); |
|
| 268 | + foreach ($this->attributes as $key => $value) { |
|
| 269 | + if (in_array($key, static::BOOLEAN_ATTRIBUTES) && !$value) { |
|
| 270 | + unset($attributes[$key]); |
|
| 271 | + } |
|
| 272 | + if (Str::startsWith('data-', $key)) { |
|
| 273 | + $permanentAttributes[$key] = $value; |
|
| 274 | + unset($attributes[$key]); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + $this->attributes = array_merge(array_filter($attributes), $permanentAttributes); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - /** |
|
| 281 | - * @return void |
|
| 282 | - */ |
|
| 283 | - protected function removeIndexedAttributes() |
|
| 284 | - { |
|
| 285 | - $this->attributes = array_diff_key( |
|
| 286 | - $this->attributes, |
|
| 287 | - array_filter($this->attributes, 'is_numeric', ARRAY_FILTER_USE_KEY) |
|
| 288 | - ); |
|
| 289 | - } |
|
| 280 | + /** |
|
| 281 | + * @return void |
|
| 282 | + */ |
|
| 283 | + protected function removeIndexedAttributes() |
|
| 284 | + { |
|
| 285 | + $this->attributes = array_diff_key( |
|
| 286 | + $this->attributes, |
|
| 287 | + array_filter($this->attributes, 'is_numeric', ARRAY_FILTER_USE_KEY) |
|
| 288 | + ); |
|
| 289 | + } |
|
| 290 | 290 | } |
@@ -6,30 +6,30 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Partial |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @param string $partialPath |
|
| 11 | - * @return string |
|
| 12 | - */ |
|
| 13 | - public function build($partialPath, array $args = []) |
|
| 14 | - { |
|
| 15 | - $className = Helper::buildClassName($partialPath, 'Modules\Html\Partials'); |
|
| 16 | - if (!class_exists($className)) { |
|
| 17 | - glsr_log()->error('Partial missing: '.$className); |
|
| 18 | - return; |
|
| 19 | - } |
|
| 20 | - $args = apply_filters('site-reviews/partial/args/'.$partialPath, $args); |
|
| 21 | - $partial = glsr($className)->build($args); |
|
| 22 | - $partial = apply_filters('site-reviews/rendered/partial', $partial, $partialPath, $args); |
|
| 23 | - $partial = apply_filters('site-reviews/rendered/partial/'.$partialPath, $partial, $args); |
|
| 24 | - return $partial; |
|
| 25 | - } |
|
| 9 | + /** |
|
| 10 | + * @param string $partialPath |
|
| 11 | + * @return string |
|
| 12 | + */ |
|
| 13 | + public function build($partialPath, array $args = []) |
|
| 14 | + { |
|
| 15 | + $className = Helper::buildClassName($partialPath, 'Modules\Html\Partials'); |
|
| 16 | + if (!class_exists($className)) { |
|
| 17 | + glsr_log()->error('Partial missing: '.$className); |
|
| 18 | + return; |
|
| 19 | + } |
|
| 20 | + $args = apply_filters('site-reviews/partial/args/'.$partialPath, $args); |
|
| 21 | + $partial = glsr($className)->build($args); |
|
| 22 | + $partial = apply_filters('site-reviews/rendered/partial', $partial, $partialPath, $args); |
|
| 23 | + $partial = apply_filters('site-reviews/rendered/partial/'.$partialPath, $partial, $args); |
|
| 24 | + return $partial; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @param string $partialPath |
|
| 29 | - * @return void |
|
| 30 | - */ |
|
| 31 | - public function render($partialPath, array $args = []) |
|
| 32 | - { |
|
| 33 | - echo $this->build($partialPath, $args); |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * @param string $partialPath |
|
| 29 | + * @return void |
|
| 30 | + */ |
|
| 31 | + public function render($partialPath, array $args = []) |
|
| 32 | + { |
|
| 33 | + echo $this->build($partialPath, $args); |
|
| 34 | + } |
|
| 35 | 35 | } |