@@ -9,7 +9,7 @@ |
||
| 9 | 9 | </div> |
| 10 | 10 | |
| 11 | 11 | <?php if (empty($response)) { |
| 12 | - return; |
|
| 12 | + return; |
|
| 13 | 13 | } ?> |
| 14 | 14 | |
| 15 | 15 | <div class="postbox glsr-response-postbox"> |
@@ -15,14 +15,20 @@ |
||
| 15 | 15 | <span class="glsr-addon-link button button-secondary" disabled> |
| 16 | 16 | <?= __('Installed', 'site-reviews'); ?> |
| 17 | 17 | </span> |
| 18 | - <?php else: ?> |
|
| 19 | - <a href="<?= wp_nonce_url(self_admin_url('plugins.php?action=activate&plugin='.$plugin), 'activate-plugin_'.$plugin); ?>" class="glsr-addon-link button button-secondary"> |
|
| 18 | + <?php else { |
|
| 19 | + : ?> |
|
| 20 | + <a href="<?= wp_nonce_url(self_admin_url('plugins.php?action=activate&plugin='.$plugin), 'activate-plugin_'.$plugin); |
|
| 21 | +} |
|
| 22 | +?>" class="glsr-addon-link button button-secondary"> |
|
| 20 | 23 | <?= __('Activate', 'site-reviews'); ?> |
| 21 | 24 | </a> |
| 22 | 25 | <?php endif; ?> |
| 23 | - <?php else: ?> |
|
| 26 | + <?php else { |
|
| 27 | + : ?> |
|
| 24 | 28 | <a href="{{ link }}" class="glsr-addon-link glsr-external button button-secondary"> |
| 25 | - <?= __('More Info', 'site-reviews'); ?> |
|
| 29 | + <?= __('More Info', 'site-reviews'); |
|
| 30 | +} |
|
| 31 | +?> |
|
| 26 | 32 | </a> |
| 27 | 33 | <?php endif; ?> |
| 28 | 34 | </div> |
@@ -9,24 +9,24 @@ |
||
| 9 | 9 | <p><?= __('Add-ons extend the functionality of Site Reviews.', 'site-reviews'); ?></p> |
| 10 | 10 | <div class="glsr-addons wp-clearfix"> |
| 11 | 11 | <?php |
| 12 | - $template->render('partials/addons/addon', [ |
|
| 13 | - 'context' => [ |
|
| 14 | - 'description' => __('Allow your visitors to submit multiple images with their reviews.', 'site-reviews'), |
|
| 15 | - 'link' => 'https://niftyplugins.com/addons/site-reviews-images/', |
|
| 16 | - 'slug' => 'images', |
|
| 17 | - 'title' => 'Images', |
|
| 18 | - ], |
|
| 19 | - 'plugin' => 'site-reviews-images/site-reviews-images.php', |
|
| 20 | - ]); |
|
| 21 | - $template->render('partials/addons/addon', [ |
|
| 22 | - 'context' => [ |
|
| 23 | - 'description' => __('Sync your Tripadvisor reviews to your website and manage them with Site Reviews.', 'site-reviews'), |
|
| 24 | - 'link' => 'https://niftyplugins.com/addons/site-reviews-tripadvisor/', |
|
| 25 | - 'slug' => 'tripadvisor', |
|
| 26 | - 'title' => 'Tripadvisor Reviews', |
|
| 27 | - ], |
|
| 28 | - 'plugin' => 'site-reviews-tripadvisor/site-reviews-tripadvisor.php', |
|
| 29 | - ]); |
|
| 30 | - ?> |
|
| 12 | + $template->render('partials/addons/addon', [ |
|
| 13 | + 'context' => [ |
|
| 14 | + 'description' => __('Allow your visitors to submit multiple images with their reviews.', 'site-reviews'), |
|
| 15 | + 'link' => 'https://niftyplugins.com/addons/site-reviews-images/', |
|
| 16 | + 'slug' => 'images', |
|
| 17 | + 'title' => 'Images', |
|
| 18 | + ], |
|
| 19 | + 'plugin' => 'site-reviews-images/site-reviews-images.php', |
|
| 20 | + ]); |
|
| 21 | + $template->render('partials/addons/addon', [ |
|
| 22 | + 'context' => [ |
|
| 23 | + 'description' => __('Sync your Tripadvisor reviews to your website and manage them with Site Reviews.', 'site-reviews'), |
|
| 24 | + 'link' => 'https://niftyplugins.com/addons/site-reviews-tripadvisor/', |
|
| 25 | + 'slug' => 'tripadvisor', |
|
| 26 | + 'title' => 'Tripadvisor Reviews', |
|
| 27 | + ], |
|
| 28 | + 'plugin' => 'site-reviews-tripadvisor/site-reviews-tripadvisor.php', |
|
| 29 | + ]); |
|
| 30 | + ?> |
|
| 31 | 31 | </div> |
| 32 | 32 | </div> |
@@ -9,138 +9,138 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Slack |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - public $endpoint; |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + public $endpoint; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var array |
|
| 19 | - */ |
|
| 20 | - public $notification; |
|
| 17 | + /** |
|
| 18 | + * @var array |
|
| 19 | + */ |
|
| 20 | + public $notification; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var Review |
|
| 24 | - */ |
|
| 25 | - public $review; |
|
| 22 | + /** |
|
| 23 | + * @var Review |
|
| 24 | + */ |
|
| 25 | + public $review; |
|
| 26 | 26 | |
| 27 | - public function __construct() |
|
| 28 | - { |
|
| 29 | - $this->endpoint = glsr(OptionManager::class)->get('settings.general.notification_slack'); |
|
| 30 | - } |
|
| 27 | + public function __construct() |
|
| 28 | + { |
|
| 29 | + $this->endpoint = glsr(OptionManager::class)->get('settings.general.notification_slack'); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @return Slack |
|
| 34 | - */ |
|
| 35 | - public function compose(Review $review, array $notification) |
|
| 36 | - { |
|
| 37 | - if (empty($this->endpoint)) { |
|
| 38 | - return $this; |
|
| 39 | - } |
|
| 40 | - $args = shortcode_atts(glsr(SlackDefaults::class)->defaults(), $notification); |
|
| 41 | - $this->review = $review; |
|
| 42 | - $notification = [ |
|
| 43 | - 'icon_url' => $args['icon_url'], |
|
| 44 | - 'username' => $args['username'], |
|
| 45 | - 'attachments' => [[ |
|
| 46 | - 'actions' => $this->buildAction($args), |
|
| 47 | - 'pretext' => $args['pretext'], |
|
| 48 | - 'color' => $args['color'], |
|
| 49 | - 'fallback' => $args['fallback'], |
|
| 50 | - 'fields' => $this->buildFields(), |
|
| 51 | - ]], |
|
| 52 | - ]; |
|
| 53 | - $this->notification = apply_filters('site-reviews/slack/compose', $notification, $this); |
|
| 54 | - return $this; |
|
| 55 | - } |
|
| 32 | + /** |
|
| 33 | + * @return Slack |
|
| 34 | + */ |
|
| 35 | + public function compose(Review $review, array $notification) |
|
| 36 | + { |
|
| 37 | + if (empty($this->endpoint)) { |
|
| 38 | + return $this; |
|
| 39 | + } |
|
| 40 | + $args = shortcode_atts(glsr(SlackDefaults::class)->defaults(), $notification); |
|
| 41 | + $this->review = $review; |
|
| 42 | + $notification = [ |
|
| 43 | + 'icon_url' => $args['icon_url'], |
|
| 44 | + 'username' => $args['username'], |
|
| 45 | + 'attachments' => [[ |
|
| 46 | + 'actions' => $this->buildAction($args), |
|
| 47 | + 'pretext' => $args['pretext'], |
|
| 48 | + 'color' => $args['color'], |
|
| 49 | + 'fallback' => $args['fallback'], |
|
| 50 | + 'fields' => $this->buildFields(), |
|
| 51 | + ]], |
|
| 52 | + ]; |
|
| 53 | + $this->notification = apply_filters('site-reviews/slack/compose', $notification, $this); |
|
| 54 | + return $this; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return WP_Error|array |
|
| 59 | - */ |
|
| 60 | - public function send() |
|
| 61 | - { |
|
| 62 | - if (empty($this->endpoint)) { |
|
| 63 | - return new WP_Error('slack', 'Slack notification was not sent: missing endpoint'); |
|
| 64 | - } |
|
| 65 | - return wp_remote_post($this->endpoint, [ |
|
| 66 | - 'blocking' => false, |
|
| 67 | - 'body' => json_encode($this->notification), |
|
| 68 | - 'headers' => ['Content-Type' => 'application/json'], |
|
| 69 | - 'httpversion' => '1.0', |
|
| 70 | - 'method' => 'POST', |
|
| 71 | - 'redirection' => 5, |
|
| 72 | - 'sslverify' => false, |
|
| 73 | - 'timeout' => 45, |
|
| 74 | - ]); |
|
| 75 | - } |
|
| 57 | + /** |
|
| 58 | + * @return WP_Error|array |
|
| 59 | + */ |
|
| 60 | + public function send() |
|
| 61 | + { |
|
| 62 | + if (empty($this->endpoint)) { |
|
| 63 | + return new WP_Error('slack', 'Slack notification was not sent: missing endpoint'); |
|
| 64 | + } |
|
| 65 | + return wp_remote_post($this->endpoint, [ |
|
| 66 | + 'blocking' => false, |
|
| 67 | + 'body' => json_encode($this->notification), |
|
| 68 | + 'headers' => ['Content-Type' => 'application/json'], |
|
| 69 | + 'httpversion' => '1.0', |
|
| 70 | + 'method' => 'POST', |
|
| 71 | + 'redirection' => 5, |
|
| 72 | + 'sslverify' => false, |
|
| 73 | + 'timeout' => 45, |
|
| 74 | + ]); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * @return array |
|
| 79 | - */ |
|
| 80 | - protected function buildAction(array $args) |
|
| 81 | - { |
|
| 82 | - return [[ |
|
| 83 | - 'text' => $args['button_text'], |
|
| 84 | - 'type' => 'button', |
|
| 85 | - 'url' => $args['button_url'], |
|
| 86 | - ]]; |
|
| 87 | - } |
|
| 77 | + /** |
|
| 78 | + * @return array |
|
| 79 | + */ |
|
| 80 | + protected function buildAction(array $args) |
|
| 81 | + { |
|
| 82 | + return [[ |
|
| 83 | + 'text' => $args['button_text'], |
|
| 84 | + 'type' => 'button', |
|
| 85 | + 'url' => $args['button_url'], |
|
| 86 | + ]]; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @return array |
|
| 91 | - */ |
|
| 92 | - protected function buildAuthorField() |
|
| 93 | - { |
|
| 94 | - $email = !empty($this->review->email) |
|
| 95 | - ? '<'.$this->review->email.'>' |
|
| 96 | - : ''; |
|
| 97 | - $author = trim(rtrim($this->review->author).' '.$email); |
|
| 98 | - return ['value' => implode(' - ', array_filter([$author, $this->review->ip_address]))]; |
|
| 99 | - } |
|
| 89 | + /** |
|
| 90 | + * @return array |
|
| 91 | + */ |
|
| 92 | + protected function buildAuthorField() |
|
| 93 | + { |
|
| 94 | + $email = !empty($this->review->email) |
|
| 95 | + ? '<'.$this->review->email.'>' |
|
| 96 | + : ''; |
|
| 97 | + $author = trim(rtrim($this->review->author).' '.$email); |
|
| 98 | + return ['value' => implode(' - ', array_filter([$author, $this->review->ip_address]))]; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * @return array |
|
| 103 | - */ |
|
| 104 | - protected function buildContentField() |
|
| 105 | - { |
|
| 106 | - return !empty($this->review->content) |
|
| 107 | - ? ['value' => $this->review->content] |
|
| 108 | - : []; |
|
| 109 | - } |
|
| 101 | + /** |
|
| 102 | + * @return array |
|
| 103 | + */ |
|
| 104 | + protected function buildContentField() |
|
| 105 | + { |
|
| 106 | + return !empty($this->review->content) |
|
| 107 | + ? ['value' => $this->review->content] |
|
| 108 | + : []; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * @return array |
|
| 113 | - */ |
|
| 114 | - protected function buildFields() |
|
| 115 | - { |
|
| 116 | - $fields = [ |
|
| 117 | - $this->buildStarsField(), |
|
| 118 | - $this->buildTitleField(), |
|
| 119 | - $this->buildContentField(), |
|
| 120 | - $this->buildAuthorField(), |
|
| 121 | - ]; |
|
| 122 | - return array_filter($fields); |
|
| 123 | - } |
|
| 111 | + /** |
|
| 112 | + * @return array |
|
| 113 | + */ |
|
| 114 | + protected function buildFields() |
|
| 115 | + { |
|
| 116 | + $fields = [ |
|
| 117 | + $this->buildStarsField(), |
|
| 118 | + $this->buildTitleField(), |
|
| 119 | + $this->buildContentField(), |
|
| 120 | + $this->buildAuthorField(), |
|
| 121 | + ]; |
|
| 122 | + return array_filter($fields); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - protected function buildStarsField() |
|
| 129 | - { |
|
| 130 | - $solidStars = str_repeat('★', $this->review->rating); |
|
| 131 | - $emptyStars = str_repeat('☆', max(0, glsr()->constant('MAX_RATING', Rating::class) - $this->review->rating)); |
|
| 132 | - $stars = $solidStars.$emptyStars; |
|
| 133 | - $stars = apply_filters('site-reviews/slack/stars', $stars, $this->review->rating, glsr()->constant('MAX_RATING', Rating::class)); |
|
| 134 | - return ['title' => $stars]; |
|
| 135 | - } |
|
| 125 | + /** |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + protected function buildStarsField() |
|
| 129 | + { |
|
| 130 | + $solidStars = str_repeat('★', $this->review->rating); |
|
| 131 | + $emptyStars = str_repeat('☆', max(0, glsr()->constant('MAX_RATING', Rating::class) - $this->review->rating)); |
|
| 132 | + $stars = $solidStars.$emptyStars; |
|
| 133 | + $stars = apply_filters('site-reviews/slack/stars', $stars, $this->review->rating, glsr()->constant('MAX_RATING', Rating::class)); |
|
| 134 | + return ['title' => $stars]; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * @return array |
|
| 139 | - */ |
|
| 140 | - protected function buildTitleField() |
|
| 141 | - { |
|
| 142 | - return !empty($this->review->title) |
|
| 143 | - ? ['title' => $this->review->title] |
|
| 144 | - : []; |
|
| 145 | - } |
|
| 137 | + /** |
|
| 138 | + * @return array |
|
| 139 | + */ |
|
| 140 | + protected function buildTitleField() |
|
| 141 | + { |
|
| 142 | + return !empty($this->review->title) |
|
| 143 | + ? ['title' => $this->review->title] |
|
| 144 | + : []; |
|
| 145 | + } |
|
| 146 | 146 | } |
@@ -216,7 +216,8 @@ |
||
| 216 | 216 | if (!empty($field['label'])) { |
| 217 | 217 | $field['legend'] = $field['label']; |
| 218 | 218 | unset($field['label']); |
| 219 | - } else { |
|
| 219 | + } |
|
| 220 | + else { |
|
| 220 | 221 | $field['is_valid'] = false; |
| 221 | 222 | glsr_log()->warning('Setting field is missing a label')->debug($field); |
| 222 | 223 | } |
@@ -11,239 +11,239 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Settings |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - public $settings; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @param string $id |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function buildFields($id) |
|
| 24 | - { |
|
| 25 | - $this->settings = glsr(DefaultsManager::class)->settings(); |
|
| 26 | - $method = Helper::buildMethodName($id, 'getTemplateDataFor'); |
|
| 27 | - $data = !method_exists($this, $method) |
|
| 28 | - ? $this->getTemplateData($id) |
|
| 29 | - : $this->$method($id); |
|
| 30 | - return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @return string |
|
| 35 | - */ |
|
| 36 | - protected function getFieldDefault(array $field) |
|
| 37 | - { |
|
| 38 | - return Arr::get($field, 'default'); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - protected function getFieldNameForDependsOn($path) |
|
| 45 | - { |
|
| 46 | - $fieldName = Str::convertPathToName($path, OptionManager::databaseKey()); |
|
| 47 | - return $this->isMultiDependency($path) |
|
| 48 | - ? $fieldName.'[]' |
|
| 49 | - : $fieldName; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - protected function getSettingFields($path) |
|
| 56 | - { |
|
| 57 | - return array_filter($this->settings, function ($key) use ($path) { |
|
| 58 | - return Str::startsWith($path, $key); |
|
| 59 | - }, ARRAY_FILTER_USE_KEY); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - protected function getSettingRows(array $fields) |
|
| 66 | - { |
|
| 67 | - $rows = ''; |
|
| 68 | - foreach ($fields as $name => $field) { |
|
| 69 | - $field = wp_parse_args($field, [ |
|
| 70 | - 'is_setting' => true, |
|
| 71 | - 'name' => $name, |
|
| 72 | - ]); |
|
| 73 | - $rows.= new Field($this->normalize($field)); |
|
| 74 | - } |
|
| 75 | - return $rows; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param string $id |
|
| 80 | - * @return array |
|
| 81 | - */ |
|
| 82 | - protected function getTemplateData($id) |
|
| 83 | - { |
|
| 84 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
| 85 | - return [ |
|
| 86 | - 'context' => [ |
|
| 87 | - 'rows' => $this->getSettingRows($fields), |
|
| 88 | - ], |
|
| 89 | - ]; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param string $id |
|
| 94 | - * @return array |
|
| 95 | - */ |
|
| 96 | - protected function getTemplateDataForAddons($id) |
|
| 97 | - { |
|
| 98 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
| 99 | - $settings = Arr::convertDotNotationArray($fields); |
|
| 100 | - $settingKeys = array_keys($settings['settings']['addons']); |
|
| 101 | - $results = []; |
|
| 102 | - foreach ($settingKeys as $key) { |
|
| 103 | - $addonFields = array_filter($fields, function ($path) use ($key) { |
|
| 104 | - return Str::startsWith('settings.addons.'.$key, $path); |
|
| 105 | - }, ARRAY_FILTER_USE_KEY); |
|
| 106 | - $results[$key] = $this->getSettingRows($addonFields); |
|
| 107 | - } |
|
| 108 | - ksort($results); |
|
| 109 | - return [ |
|
| 110 | - 'settings' => $results, |
|
| 111 | - ]; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @param string $id |
|
| 116 | - * @return array |
|
| 117 | - */ |
|
| 118 | - protected function getTemplateDataForLicenses($id) |
|
| 119 | - { |
|
| 120 | - $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
| 121 | - ksort($fields); |
|
| 122 | - return [ |
|
| 123 | - 'context' => [ |
|
| 124 | - 'rows' => $this->getSettingRows($fields), |
|
| 125 | - ], |
|
| 126 | - ]; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - protected function getTemplateDataForTranslations() |
|
| 133 | - { |
|
| 134 | - $translations = glsr(Translation::class)->renderAll(); |
|
| 135 | - $class = empty($translations) |
|
| 136 | - ? 'glsr-hidden' |
|
| 137 | - : ''; |
|
| 138 | - return [ |
|
| 139 | - 'context' => [ |
|
| 140 | - 'class' => $class, |
|
| 141 | - 'database_key' => OptionManager::databaseKey(), |
|
| 142 | - 'translations' => $translations, |
|
| 143 | - ], |
|
| 144 | - ]; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @param string $path |
|
| 149 | - * @param string|array $expectedValue |
|
| 150 | - * @return bool |
|
| 151 | - */ |
|
| 152 | - protected function isFieldHidden($path, $expectedValue) |
|
| 153 | - { |
|
| 154 | - $optionValue = glsr(OptionManager::class)->get( |
|
| 155 | - $path, |
|
| 156 | - Arr::get(glsr()->defaults, $path) |
|
| 157 | - ); |
|
| 158 | - if (is_array($expectedValue)) { |
|
| 159 | - return is_array($optionValue) |
|
| 160 | - ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
| 161 | - : !in_array($optionValue, $expectedValue); |
|
| 162 | - } |
|
| 163 | - return $optionValue != $expectedValue; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @return bool |
|
| 168 | - */ |
|
| 169 | - protected function isMultiDependency($path) |
|
| 170 | - { |
|
| 171 | - if (isset($this->settings[$path])) { |
|
| 172 | - $field = $this->settings[$path]; |
|
| 173 | - return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
| 174 | - || !empty($field['multiple']); |
|
| 175 | - } |
|
| 176 | - return false; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @return array |
|
| 181 | - */ |
|
| 182 | - protected function normalize(array $field) |
|
| 183 | - { |
|
| 184 | - $field = $this->normalizeDependsOn($field); |
|
| 185 | - $field = $this->normalizeLabelAndLegend($field); |
|
| 186 | - $field = $this->normalizeValue($field); |
|
| 187 | - return $field; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @return array |
|
| 192 | - */ |
|
| 193 | - protected function normalizeDependsOn(array $field) |
|
| 194 | - { |
|
| 195 | - if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
| 196 | - $isFieldHidden = false; |
|
| 197 | - $conditions = []; |
|
| 198 | - foreach ($field['depends_on'] as $path => $value) { |
|
| 199 | - $conditions[] = [ |
|
| 200 | - 'name' => $this->getFieldNameForDependsOn($path), |
|
| 201 | - 'value' => $value, |
|
| 202 | - ]; |
|
| 203 | - if ($this->isFieldHidden($path, $value)) { |
|
| 204 | - $isFieldHidden = true; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
| 208 | - $field['is_hidden'] = $isFieldHidden; |
|
| 209 | - } |
|
| 210 | - return $field; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @return array |
|
| 215 | - */ |
|
| 216 | - protected function normalizeLabelAndLegend(array $field) |
|
| 217 | - { |
|
| 218 | - if (!empty($field['label'])) { |
|
| 219 | - $field['legend'] = $field['label']; |
|
| 220 | - unset($field['label']); |
|
| 221 | - } else { |
|
| 222 | - $field['is_valid'] = false; |
|
| 223 | - glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
| 224 | - } |
|
| 225 | - return $field; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * @return array |
|
| 230 | - */ |
|
| 231 | - protected function normalizeValue(array $field) |
|
| 232 | - { |
|
| 233 | - if (!isset($field['value'])) { |
|
| 234 | - $field['value'] = glsr(OptionManager::class)->get( |
|
| 235 | - $field['name'], |
|
| 236 | - $this->getFieldDefault($field) |
|
| 237 | - ); |
|
| 238 | - } |
|
| 239 | - return $field; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * @return string |
|
| 244 | - */ |
|
| 245 | - protected function normalizeSettingPath($path) |
|
| 246 | - { |
|
| 247 | - return Str::prefix('settings.', rtrim($path, '.')); |
|
| 248 | - } |
|
| 14 | + /** |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + public $settings; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @param string $id |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function buildFields($id) |
|
| 24 | + { |
|
| 25 | + $this->settings = glsr(DefaultsManager::class)->settings(); |
|
| 26 | + $method = Helper::buildMethodName($id, 'getTemplateDataFor'); |
|
| 27 | + $data = !method_exists($this, $method) |
|
| 28 | + ? $this->getTemplateData($id) |
|
| 29 | + : $this->$method($id); |
|
| 30 | + return glsr(Template::class)->build('pages/settings/'.$id, $data); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @return string |
|
| 35 | + */ |
|
| 36 | + protected function getFieldDefault(array $field) |
|
| 37 | + { |
|
| 38 | + return Arr::get($field, 'default'); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + protected function getFieldNameForDependsOn($path) |
|
| 45 | + { |
|
| 46 | + $fieldName = Str::convertPathToName($path, OptionManager::databaseKey()); |
|
| 47 | + return $this->isMultiDependency($path) |
|
| 48 | + ? $fieldName.'[]' |
|
| 49 | + : $fieldName; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + protected function getSettingFields($path) |
|
| 56 | + { |
|
| 57 | + return array_filter($this->settings, function ($key) use ($path) { |
|
| 58 | + return Str::startsWith($path, $key); |
|
| 59 | + }, ARRAY_FILTER_USE_KEY); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + protected function getSettingRows(array $fields) |
|
| 66 | + { |
|
| 67 | + $rows = ''; |
|
| 68 | + foreach ($fields as $name => $field) { |
|
| 69 | + $field = wp_parse_args($field, [ |
|
| 70 | + 'is_setting' => true, |
|
| 71 | + 'name' => $name, |
|
| 72 | + ]); |
|
| 73 | + $rows.= new Field($this->normalize($field)); |
|
| 74 | + } |
|
| 75 | + return $rows; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param string $id |
|
| 80 | + * @return array |
|
| 81 | + */ |
|
| 82 | + protected function getTemplateData($id) |
|
| 83 | + { |
|
| 84 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
| 85 | + return [ |
|
| 86 | + 'context' => [ |
|
| 87 | + 'rows' => $this->getSettingRows($fields), |
|
| 88 | + ], |
|
| 89 | + ]; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param string $id |
|
| 94 | + * @return array |
|
| 95 | + */ |
|
| 96 | + protected function getTemplateDataForAddons($id) |
|
| 97 | + { |
|
| 98 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
| 99 | + $settings = Arr::convertDotNotationArray($fields); |
|
| 100 | + $settingKeys = array_keys($settings['settings']['addons']); |
|
| 101 | + $results = []; |
|
| 102 | + foreach ($settingKeys as $key) { |
|
| 103 | + $addonFields = array_filter($fields, function ($path) use ($key) { |
|
| 104 | + return Str::startsWith('settings.addons.'.$key, $path); |
|
| 105 | + }, ARRAY_FILTER_USE_KEY); |
|
| 106 | + $results[$key] = $this->getSettingRows($addonFields); |
|
| 107 | + } |
|
| 108 | + ksort($results); |
|
| 109 | + return [ |
|
| 110 | + 'settings' => $results, |
|
| 111 | + ]; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @param string $id |
|
| 116 | + * @return array |
|
| 117 | + */ |
|
| 118 | + protected function getTemplateDataForLicenses($id) |
|
| 119 | + { |
|
| 120 | + $fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
|
| 121 | + ksort($fields); |
|
| 122 | + return [ |
|
| 123 | + 'context' => [ |
|
| 124 | + 'rows' => $this->getSettingRows($fields), |
|
| 125 | + ], |
|
| 126 | + ]; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + protected function getTemplateDataForTranslations() |
|
| 133 | + { |
|
| 134 | + $translations = glsr(Translation::class)->renderAll(); |
|
| 135 | + $class = empty($translations) |
|
| 136 | + ? 'glsr-hidden' |
|
| 137 | + : ''; |
|
| 138 | + return [ |
|
| 139 | + 'context' => [ |
|
| 140 | + 'class' => $class, |
|
| 141 | + 'database_key' => OptionManager::databaseKey(), |
|
| 142 | + 'translations' => $translations, |
|
| 143 | + ], |
|
| 144 | + ]; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @param string $path |
|
| 149 | + * @param string|array $expectedValue |
|
| 150 | + * @return bool |
|
| 151 | + */ |
|
| 152 | + protected function isFieldHidden($path, $expectedValue) |
|
| 153 | + { |
|
| 154 | + $optionValue = glsr(OptionManager::class)->get( |
|
| 155 | + $path, |
|
| 156 | + Arr::get(glsr()->defaults, $path) |
|
| 157 | + ); |
|
| 158 | + if (is_array($expectedValue)) { |
|
| 159 | + return is_array($optionValue) |
|
| 160 | + ? 0 === count(array_intersect($optionValue, $expectedValue)) |
|
| 161 | + : !in_array($optionValue, $expectedValue); |
|
| 162 | + } |
|
| 163 | + return $optionValue != $expectedValue; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @return bool |
|
| 168 | + */ |
|
| 169 | + protected function isMultiDependency($path) |
|
| 170 | + { |
|
| 171 | + if (isset($this->settings[$path])) { |
|
| 172 | + $field = $this->settings[$path]; |
|
| 173 | + return ('checkbox' == $field['type'] && !empty($field['options'])) |
|
| 174 | + || !empty($field['multiple']); |
|
| 175 | + } |
|
| 176 | + return false; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @return array |
|
| 181 | + */ |
|
| 182 | + protected function normalize(array $field) |
|
| 183 | + { |
|
| 184 | + $field = $this->normalizeDependsOn($field); |
|
| 185 | + $field = $this->normalizeLabelAndLegend($field); |
|
| 186 | + $field = $this->normalizeValue($field); |
|
| 187 | + return $field; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @return array |
|
| 192 | + */ |
|
| 193 | + protected function normalizeDependsOn(array $field) |
|
| 194 | + { |
|
| 195 | + if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
|
| 196 | + $isFieldHidden = false; |
|
| 197 | + $conditions = []; |
|
| 198 | + foreach ($field['depends_on'] as $path => $value) { |
|
| 199 | + $conditions[] = [ |
|
| 200 | + 'name' => $this->getFieldNameForDependsOn($path), |
|
| 201 | + 'value' => $value, |
|
| 202 | + ]; |
|
| 203 | + if ($this->isFieldHidden($path, $value)) { |
|
| 204 | + $isFieldHidden = true; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + $field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
|
| 208 | + $field['is_hidden'] = $isFieldHidden; |
|
| 209 | + } |
|
| 210 | + return $field; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @return array |
|
| 215 | + */ |
|
| 216 | + protected function normalizeLabelAndLegend(array $field) |
|
| 217 | + { |
|
| 218 | + if (!empty($field['label'])) { |
|
| 219 | + $field['legend'] = $field['label']; |
|
| 220 | + unset($field['label']); |
|
| 221 | + } else { |
|
| 222 | + $field['is_valid'] = false; |
|
| 223 | + glsr_log()->warning('Setting field is missing a label')->debug($field); |
|
| 224 | + } |
|
| 225 | + return $field; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * @return array |
|
| 230 | + */ |
|
| 231 | + protected function normalizeValue(array $field) |
|
| 232 | + { |
|
| 233 | + if (!isset($field['value'])) { |
|
| 234 | + $field['value'] = glsr(OptionManager::class)->get( |
|
| 235 | + $field['name'], |
|
| 236 | + $this->getFieldDefault($field) |
|
| 237 | + ); |
|
| 238 | + } |
|
| 239 | + return $field; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * @return string |
|
| 244 | + */ |
|
| 245 | + protected function normalizeSettingPath($path) |
|
| 246 | + { |
|
| 247 | + return Str::prefix('settings.', rtrim($path, '.')); |
|
| 248 | + } |
|
| 249 | 249 | } |
@@ -328,7 +328,8 @@ |
||
| 328 | 328 | } |
| 329 | 329 | if (is_array($params[0])) { |
| 330 | 330 | $this->args += $params[0]; |
| 331 | - } elseif (is_array($params[1])) { |
|
| 331 | + } |
|
| 332 | + elseif (is_array($params[1])) { |
|
| 332 | 333 | $this->args += $params[1]; |
| 333 | 334 | } |
| 334 | 335 | if (!isset($this->args['is_public'])) { |
@@ -18,346 +18,346 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Builder |
| 20 | 20 | { |
| 21 | - const INPUT_TYPES = [ |
|
| 22 | - 'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', |
|
| 23 | - 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', |
|
| 24 | - 'url', 'week', |
|
| 25 | - ]; |
|
| 26 | - |
|
| 27 | - const TAGS_FORM = [ |
|
| 28 | - 'input', 'select', 'textarea', |
|
| 29 | - ]; |
|
| 30 | - |
|
| 31 | - const TAGS_SINGLE = [ |
|
| 32 | - 'img', |
|
| 33 | - ]; |
|
| 34 | - |
|
| 35 | - const TAGS_STRUCTURE = [ |
|
| 36 | - 'div', 'form', 'nav', 'ol', 'section', 'ul', |
|
| 37 | - ]; |
|
| 38 | - |
|
| 39 | - const TAGS_TEXT = [ |
|
| 40 | - 'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', |
|
| 41 | - 'small', 'span', |
|
| 42 | - ]; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - public $args = []; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var bool |
|
| 51 | - */ |
|
| 52 | - public $render = false; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - public $tag; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param string $method |
|
| 61 | - * @param array $args |
|
| 62 | - * @return string|void |
|
| 63 | - */ |
|
| 64 | - public function __call($method, $args) |
|
| 65 | - { |
|
| 66 | - $instance = new static(); |
|
| 67 | - $instance->setTagFromMethod($method); |
|
| 68 | - call_user_func_array([$instance, 'normalize'], $args += ['', '']); |
|
| 69 | - $tags = array_merge(static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT); |
|
| 70 | - do_action_ref_array('site-reviews/builder', [$instance]); |
|
| 71 | - $generatedTag = in_array($instance->tag, $tags) |
|
| 72 | - ? $instance->buildTag() |
|
| 73 | - : $instance->buildCustomField(); |
|
| 74 | - $generatedTag = apply_filters('site-reviews/builder/result', $generatedTag, $instance); |
|
| 75 | - if (!$this->render) { |
|
| 76 | - return $generatedTag; |
|
| 77 | - } |
|
| 78 | - echo $generatedTag; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param string $property |
|
| 83 | - * @param mixed $value |
|
| 84 | - * @return void |
|
| 85 | - */ |
|
| 86 | - public function __set($property, $value) |
|
| 87 | - { |
|
| 88 | - $properties = [ |
|
| 89 | - 'args' => 'is_array', |
|
| 90 | - 'render' => 'is_bool', |
|
| 91 | - 'tag' => 'is_string', |
|
| 92 | - ]; |
|
| 93 | - if (!isset($properties[$property]) |
|
| 94 | - || empty(array_filter([$value], $properties[$property])) |
|
| 95 | - ) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - $this->$property = $value; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return void|string |
|
| 103 | - */ |
|
| 104 | - public function getClosingTag() |
|
| 105 | - { |
|
| 106 | - if (empty($this->tag)) { |
|
| 107 | - return; |
|
| 108 | - } |
|
| 109 | - return '</'.$this->tag.'>'; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @return void|string |
|
| 114 | - */ |
|
| 115 | - public function getOpeningTag() |
|
| 116 | - { |
|
| 117 | - if (empty($this->tag)) { |
|
| 118 | - return; |
|
| 119 | - } |
|
| 120 | - $attributes = glsr(Attributes::class)->{$this->tag}($this->args)->toString(); |
|
| 121 | - return '<'.trim($this->tag.' '.$attributes).'>'; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @return void|string |
|
| 126 | - */ |
|
| 127 | - public function getTag() |
|
| 128 | - { |
|
| 129 | - if (in_array($this->tag, static::TAGS_SINGLE)) { |
|
| 130 | - return $this->getOpeningTag(); |
|
| 131 | - } |
|
| 132 | - if (!in_array($this->tag, static::TAGS_FORM)) { |
|
| 133 | - return $this->buildDefaultTag(); |
|
| 134 | - } |
|
| 135 | - return call_user_func([$this, 'buildForm'.ucfirst($this->tag)]).$this->buildFieldDescription(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @return string |
|
| 140 | - */ |
|
| 141 | - public function raw(array $field) |
|
| 142 | - { |
|
| 143 | - unset($field['label']); |
|
| 144 | - return $this->{$field['type']}($field); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @return string|void |
|
| 149 | - */ |
|
| 150 | - protected function buildCustomField() |
|
| 151 | - { |
|
| 152 | - $className = $this->getCustomFieldClassName(); |
|
| 153 | - if (class_exists($className)) { |
|
| 154 | - return (new $className($this))->build(); |
|
| 155 | - } |
|
| 156 | - glsr_log()->error('Field missing: '.$className); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return string|void |
|
| 161 | - */ |
|
| 162 | - protected function buildDefaultTag($text = '') |
|
| 163 | - { |
|
| 164 | - if (empty($text)) { |
|
| 165 | - $text = $this->args['text']; |
|
| 166 | - } |
|
| 167 | - return $this->getOpeningTag().$text.$this->getClosingTag(); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @return string|void |
|
| 172 | - */ |
|
| 173 | - protected function buildFieldDescription() |
|
| 174 | - { |
|
| 175 | - if (empty($this->args['description'])) { |
|
| 176 | - return; |
|
| 177 | - } |
|
| 178 | - if ($this->args['is_widget']) { |
|
| 179 | - return $this->small($this->args['description']); |
|
| 180 | - } |
|
| 181 | - return $this->p($this->args['description'], ['class' => 'description']); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @return string|void |
|
| 186 | - */ |
|
| 187 | - protected function buildFormInput() |
|
| 188 | - { |
|
| 189 | - if (!in_array($this->args['type'], ['checkbox', 'radio'])) { |
|
| 190 | - if (isset($this->args['multiple'])) { |
|
| 191 | - $this->args['name'].= '[]'; |
|
| 192 | - } |
|
| 193 | - return $this->buildFormLabel().$this->getOpeningTag(); |
|
| 194 | - } |
|
| 195 | - return empty($this->args['options']) |
|
| 196 | - ? $this->buildFormInputChoice() |
|
| 197 | - : $this->buildFormInputMultiChoice(); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @return string|void |
|
| 202 | - */ |
|
| 203 | - protected function buildFormInputChoice() |
|
| 204 | - { |
|
| 205 | - if (!empty($this->args['text'])) { |
|
| 206 | - $this->args['label'] = $this->args['text']; |
|
| 207 | - } |
|
| 208 | - if (!$this->args['is_public']) { |
|
| 209 | - return $this->buildFormLabel([ |
|
| 210 | - 'class' => 'glsr-'.$this->args['type'].'-label', |
|
| 211 | - 'text' => $this->getOpeningTag().' '.$this->args['label'].'<span></span>', |
|
| 212 | - ]); |
|
| 213 | - } |
|
| 214 | - return $this->getOpeningTag().$this->buildFormLabel([ |
|
| 215 | - 'class' => 'glsr-'.$this->args['type'].'-label', |
|
| 216 | - 'text' => $this->args['label'].'<span></span>', |
|
| 217 | - ]); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @return string|void |
|
| 222 | - */ |
|
| 223 | - protected function buildFormInputMultiChoice() |
|
| 224 | - { |
|
| 225 | - if ('checkbox' == $this->args['type']) { |
|
| 226 | - $this->args['name'].= '[]'; |
|
| 227 | - } |
|
| 228 | - $index = 0; |
|
| 229 | - $options = array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$index) { |
|
| 230 | - return $carry.$this->li($this->{$this->args['type']}([ |
|
| 231 | - 'checked' => in_array($key, (array) $this->args['value']), |
|
| 232 | - 'id' => $this->args['id'].'-'.$index++, |
|
| 233 | - 'name' => $this->args['name'], |
|
| 234 | - 'text' => $this->args['options'][$key], |
|
| 235 | - 'value' => $key, |
|
| 236 | - ])); |
|
| 237 | - }); |
|
| 238 | - return $this->ul($options, [ |
|
| 239 | - 'class' => $this->args['class'], |
|
| 240 | - 'id' => $this->args['id'], |
|
| 241 | - ]); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * @return void|string |
|
| 246 | - */ |
|
| 247 | - protected function buildFormLabel(array $customArgs = []) |
|
| 248 | - { |
|
| 249 | - if (empty($this->args['label']) || 'hidden' == $this->args['type']) { |
|
| 250 | - return; |
|
| 251 | - } |
|
| 252 | - return $this->label(wp_parse_args($customArgs, [ |
|
| 253 | - 'for' => $this->args['id'], |
|
| 254 | - 'is_public' => $this->args['is_public'], |
|
| 255 | - 'text' => $this->args['label'], |
|
| 256 | - 'type' => $this->args['type'], |
|
| 257 | - ])); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @return string|void |
|
| 262 | - */ |
|
| 263 | - protected function buildFormSelect() |
|
| 264 | - { |
|
| 265 | - return $this->buildFormLabel().$this->buildDefaultTag($this->buildFormSelectOptions()); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @return string|void |
|
| 270 | - */ |
|
| 271 | - protected function buildFormSelectOptions() |
|
| 272 | - { |
|
| 273 | - return array_reduce(array_keys($this->args['options']), function ($carry, $key) { |
|
| 274 | - return $carry.$this->option([ |
|
| 275 | - 'selected' => $this->args['value'] == $key, |
|
| 276 | - 'text' => $this->args['options'][$key], |
|
| 277 | - 'value' => $key, |
|
| 278 | - ]); |
|
| 279 | - }); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * @return string|void |
|
| 284 | - */ |
|
| 285 | - protected function buildFormTextarea() |
|
| 286 | - { |
|
| 287 | - return $this->buildFormLabel().$this->buildDefaultTag($this->args['value']); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @return string|void |
|
| 292 | - */ |
|
| 293 | - protected function buildTag() |
|
| 294 | - { |
|
| 295 | - $this->mergeArgsWithRequiredDefaults(); |
|
| 296 | - return $this->getTag(); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * @return string |
|
| 301 | - */ |
|
| 302 | - protected function getCustomFieldClassName() |
|
| 303 | - { |
|
| 304 | - $classname = Helper::buildClassName($this->tag, __NAMESPACE__.'\Fields'); |
|
| 305 | - return apply_filters('site-reviews/builder/field/'.$this->tag, $classname); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * @return void |
|
| 310 | - */ |
|
| 311 | - protected function mergeArgsWithRequiredDefaults() |
|
| 312 | - { |
|
| 313 | - $className = $this->getCustomFieldClassName(); |
|
| 314 | - if (class_exists($className)) { |
|
| 315 | - $this->args = $className::merge($this->args); |
|
| 316 | - } |
|
| 317 | - $this->args = glsr(BuilderDefaults::class)->merge($this->args); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * @param string|array ...$params |
|
| 322 | - * @return void |
|
| 323 | - */ |
|
| 324 | - protected function normalize(...$params) |
|
| 325 | - { |
|
| 326 | - if (is_string($params[0]) || is_numeric($params[0])) { |
|
| 327 | - $this->setNameOrTextAttributeForTag($params[0]); |
|
| 328 | - } |
|
| 329 | - if (is_array($params[0])) { |
|
| 330 | - $this->args += $params[0]; |
|
| 331 | - } elseif (is_array($params[1])) { |
|
| 332 | - $this->args += $params[1]; |
|
| 333 | - } |
|
| 334 | - if (!isset($this->args['is_public'])) { |
|
| 335 | - $this->args['is_public'] = false; |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * @param string $value |
|
| 341 | - * @return void |
|
| 342 | - */ |
|
| 343 | - protected function setNameOrTextAttributeForTag($value) |
|
| 344 | - { |
|
| 345 | - $attribute = in_array($this->tag, static::TAGS_FORM) |
|
| 346 | - ? 'name' |
|
| 347 | - : 'text'; |
|
| 348 | - $this->args[$attribute] = $value; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * @param string $method |
|
| 353 | - * @return void |
|
| 354 | - */ |
|
| 355 | - protected function setTagFromMethod($method) |
|
| 356 | - { |
|
| 357 | - $this->tag = strtolower($method); |
|
| 358 | - if (in_array($this->tag, static::INPUT_TYPES)) { |
|
| 359 | - $this->args['type'] = $this->tag; |
|
| 360 | - $this->tag = 'input'; |
|
| 361 | - } |
|
| 362 | - } |
|
| 21 | + const INPUT_TYPES = [ |
|
| 22 | + 'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', |
|
| 23 | + 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', |
|
| 24 | + 'url', 'week', |
|
| 25 | + ]; |
|
| 26 | + |
|
| 27 | + const TAGS_FORM = [ |
|
| 28 | + 'input', 'select', 'textarea', |
|
| 29 | + ]; |
|
| 30 | + |
|
| 31 | + const TAGS_SINGLE = [ |
|
| 32 | + 'img', |
|
| 33 | + ]; |
|
| 34 | + |
|
| 35 | + const TAGS_STRUCTURE = [ |
|
| 36 | + 'div', 'form', 'nav', 'ol', 'section', 'ul', |
|
| 37 | + ]; |
|
| 38 | + |
|
| 39 | + const TAGS_TEXT = [ |
|
| 40 | + 'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', |
|
| 41 | + 'small', 'span', |
|
| 42 | + ]; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + public $args = []; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var bool |
|
| 51 | + */ |
|
| 52 | + public $render = false; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + public $tag; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param string $method |
|
| 61 | + * @param array $args |
|
| 62 | + * @return string|void |
|
| 63 | + */ |
|
| 64 | + public function __call($method, $args) |
|
| 65 | + { |
|
| 66 | + $instance = new static(); |
|
| 67 | + $instance->setTagFromMethod($method); |
|
| 68 | + call_user_func_array([$instance, 'normalize'], $args += ['', '']); |
|
| 69 | + $tags = array_merge(static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT); |
|
| 70 | + do_action_ref_array('site-reviews/builder', [$instance]); |
|
| 71 | + $generatedTag = in_array($instance->tag, $tags) |
|
| 72 | + ? $instance->buildTag() |
|
| 73 | + : $instance->buildCustomField(); |
|
| 74 | + $generatedTag = apply_filters('site-reviews/builder/result', $generatedTag, $instance); |
|
| 75 | + if (!$this->render) { |
|
| 76 | + return $generatedTag; |
|
| 77 | + } |
|
| 78 | + echo $generatedTag; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param string $property |
|
| 83 | + * @param mixed $value |
|
| 84 | + * @return void |
|
| 85 | + */ |
|
| 86 | + public function __set($property, $value) |
|
| 87 | + { |
|
| 88 | + $properties = [ |
|
| 89 | + 'args' => 'is_array', |
|
| 90 | + 'render' => 'is_bool', |
|
| 91 | + 'tag' => 'is_string', |
|
| 92 | + ]; |
|
| 93 | + if (!isset($properties[$property]) |
|
| 94 | + || empty(array_filter([$value], $properties[$property])) |
|
| 95 | + ) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + $this->$property = $value; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return void|string |
|
| 103 | + */ |
|
| 104 | + public function getClosingTag() |
|
| 105 | + { |
|
| 106 | + if (empty($this->tag)) { |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | + return '</'.$this->tag.'>'; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @return void|string |
|
| 114 | + */ |
|
| 115 | + public function getOpeningTag() |
|
| 116 | + { |
|
| 117 | + if (empty($this->tag)) { |
|
| 118 | + return; |
|
| 119 | + } |
|
| 120 | + $attributes = glsr(Attributes::class)->{$this->tag}($this->args)->toString(); |
|
| 121 | + return '<'.trim($this->tag.' '.$attributes).'>'; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @return void|string |
|
| 126 | + */ |
|
| 127 | + public function getTag() |
|
| 128 | + { |
|
| 129 | + if (in_array($this->tag, static::TAGS_SINGLE)) { |
|
| 130 | + return $this->getOpeningTag(); |
|
| 131 | + } |
|
| 132 | + if (!in_array($this->tag, static::TAGS_FORM)) { |
|
| 133 | + return $this->buildDefaultTag(); |
|
| 134 | + } |
|
| 135 | + return call_user_func([$this, 'buildForm'.ucfirst($this->tag)]).$this->buildFieldDescription(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @return string |
|
| 140 | + */ |
|
| 141 | + public function raw(array $field) |
|
| 142 | + { |
|
| 143 | + unset($field['label']); |
|
| 144 | + return $this->{$field['type']}($field); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @return string|void |
|
| 149 | + */ |
|
| 150 | + protected function buildCustomField() |
|
| 151 | + { |
|
| 152 | + $className = $this->getCustomFieldClassName(); |
|
| 153 | + if (class_exists($className)) { |
|
| 154 | + return (new $className($this))->build(); |
|
| 155 | + } |
|
| 156 | + glsr_log()->error('Field missing: '.$className); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return string|void |
|
| 161 | + */ |
|
| 162 | + protected function buildDefaultTag($text = '') |
|
| 163 | + { |
|
| 164 | + if (empty($text)) { |
|
| 165 | + $text = $this->args['text']; |
|
| 166 | + } |
|
| 167 | + return $this->getOpeningTag().$text.$this->getClosingTag(); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @return string|void |
|
| 172 | + */ |
|
| 173 | + protected function buildFieldDescription() |
|
| 174 | + { |
|
| 175 | + if (empty($this->args['description'])) { |
|
| 176 | + return; |
|
| 177 | + } |
|
| 178 | + if ($this->args['is_widget']) { |
|
| 179 | + return $this->small($this->args['description']); |
|
| 180 | + } |
|
| 181 | + return $this->p($this->args['description'], ['class' => 'description']); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @return string|void |
|
| 186 | + */ |
|
| 187 | + protected function buildFormInput() |
|
| 188 | + { |
|
| 189 | + if (!in_array($this->args['type'], ['checkbox', 'radio'])) { |
|
| 190 | + if (isset($this->args['multiple'])) { |
|
| 191 | + $this->args['name'].= '[]'; |
|
| 192 | + } |
|
| 193 | + return $this->buildFormLabel().$this->getOpeningTag(); |
|
| 194 | + } |
|
| 195 | + return empty($this->args['options']) |
|
| 196 | + ? $this->buildFormInputChoice() |
|
| 197 | + : $this->buildFormInputMultiChoice(); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @return string|void |
|
| 202 | + */ |
|
| 203 | + protected function buildFormInputChoice() |
|
| 204 | + { |
|
| 205 | + if (!empty($this->args['text'])) { |
|
| 206 | + $this->args['label'] = $this->args['text']; |
|
| 207 | + } |
|
| 208 | + if (!$this->args['is_public']) { |
|
| 209 | + return $this->buildFormLabel([ |
|
| 210 | + 'class' => 'glsr-'.$this->args['type'].'-label', |
|
| 211 | + 'text' => $this->getOpeningTag().' '.$this->args['label'].'<span></span>', |
|
| 212 | + ]); |
|
| 213 | + } |
|
| 214 | + return $this->getOpeningTag().$this->buildFormLabel([ |
|
| 215 | + 'class' => 'glsr-'.$this->args['type'].'-label', |
|
| 216 | + 'text' => $this->args['label'].'<span></span>', |
|
| 217 | + ]); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @return string|void |
|
| 222 | + */ |
|
| 223 | + protected function buildFormInputMultiChoice() |
|
| 224 | + { |
|
| 225 | + if ('checkbox' == $this->args['type']) { |
|
| 226 | + $this->args['name'].= '[]'; |
|
| 227 | + } |
|
| 228 | + $index = 0; |
|
| 229 | + $options = array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$index) { |
|
| 230 | + return $carry.$this->li($this->{$this->args['type']}([ |
|
| 231 | + 'checked' => in_array($key, (array) $this->args['value']), |
|
| 232 | + 'id' => $this->args['id'].'-'.$index++, |
|
| 233 | + 'name' => $this->args['name'], |
|
| 234 | + 'text' => $this->args['options'][$key], |
|
| 235 | + 'value' => $key, |
|
| 236 | + ])); |
|
| 237 | + }); |
|
| 238 | + return $this->ul($options, [ |
|
| 239 | + 'class' => $this->args['class'], |
|
| 240 | + 'id' => $this->args['id'], |
|
| 241 | + ]); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * @return void|string |
|
| 246 | + */ |
|
| 247 | + protected function buildFormLabel(array $customArgs = []) |
|
| 248 | + { |
|
| 249 | + if (empty($this->args['label']) || 'hidden' == $this->args['type']) { |
|
| 250 | + return; |
|
| 251 | + } |
|
| 252 | + return $this->label(wp_parse_args($customArgs, [ |
|
| 253 | + 'for' => $this->args['id'], |
|
| 254 | + 'is_public' => $this->args['is_public'], |
|
| 255 | + 'text' => $this->args['label'], |
|
| 256 | + 'type' => $this->args['type'], |
|
| 257 | + ])); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @return string|void |
|
| 262 | + */ |
|
| 263 | + protected function buildFormSelect() |
|
| 264 | + { |
|
| 265 | + return $this->buildFormLabel().$this->buildDefaultTag($this->buildFormSelectOptions()); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @return string|void |
|
| 270 | + */ |
|
| 271 | + protected function buildFormSelectOptions() |
|
| 272 | + { |
|
| 273 | + return array_reduce(array_keys($this->args['options']), function ($carry, $key) { |
|
| 274 | + return $carry.$this->option([ |
|
| 275 | + 'selected' => $this->args['value'] == $key, |
|
| 276 | + 'text' => $this->args['options'][$key], |
|
| 277 | + 'value' => $key, |
|
| 278 | + ]); |
|
| 279 | + }); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * @return string|void |
|
| 284 | + */ |
|
| 285 | + protected function buildFormTextarea() |
|
| 286 | + { |
|
| 287 | + return $this->buildFormLabel().$this->buildDefaultTag($this->args['value']); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @return string|void |
|
| 292 | + */ |
|
| 293 | + protected function buildTag() |
|
| 294 | + { |
|
| 295 | + $this->mergeArgsWithRequiredDefaults(); |
|
| 296 | + return $this->getTag(); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * @return string |
|
| 301 | + */ |
|
| 302 | + protected function getCustomFieldClassName() |
|
| 303 | + { |
|
| 304 | + $classname = Helper::buildClassName($this->tag, __NAMESPACE__.'\Fields'); |
|
| 305 | + return apply_filters('site-reviews/builder/field/'.$this->tag, $classname); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * @return void |
|
| 310 | + */ |
|
| 311 | + protected function mergeArgsWithRequiredDefaults() |
|
| 312 | + { |
|
| 313 | + $className = $this->getCustomFieldClassName(); |
|
| 314 | + if (class_exists($className)) { |
|
| 315 | + $this->args = $className::merge($this->args); |
|
| 316 | + } |
|
| 317 | + $this->args = glsr(BuilderDefaults::class)->merge($this->args); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * @param string|array ...$params |
|
| 322 | + * @return void |
|
| 323 | + */ |
|
| 324 | + protected function normalize(...$params) |
|
| 325 | + { |
|
| 326 | + if (is_string($params[0]) || is_numeric($params[0])) { |
|
| 327 | + $this->setNameOrTextAttributeForTag($params[0]); |
|
| 328 | + } |
|
| 329 | + if (is_array($params[0])) { |
|
| 330 | + $this->args += $params[0]; |
|
| 331 | + } elseif (is_array($params[1])) { |
|
| 332 | + $this->args += $params[1]; |
|
| 333 | + } |
|
| 334 | + if (!isset($this->args['is_public'])) { |
|
| 335 | + $this->args['is_public'] = false; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * @param string $value |
|
| 341 | + * @return void |
|
| 342 | + */ |
|
| 343 | + protected function setNameOrTextAttributeForTag($value) |
|
| 344 | + { |
|
| 345 | + $attribute = in_array($this->tag, static::TAGS_FORM) |
|
| 346 | + ? 'name' |
|
| 347 | + : 'text'; |
|
| 348 | + $this->args[$attribute] = $value; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * @param string $method |
|
| 353 | + * @return void |
|
| 354 | + */ |
|
| 355 | + protected function setTagFromMethod($method) |
|
| 356 | + { |
|
| 357 | + $this->tag = strtolower($method); |
|
| 358 | + if (in_array($this->tag, static::INPUT_TYPES)) { |
|
| 359 | + $this->args['type'] = $this->tag; |
|
| 360 | + $this->tag = 'input'; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | 363 | } |
@@ -4,13 +4,13 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Hidden extends Field |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return array |
|
| 9 | - */ |
|
| 10 | - public static function required() |
|
| 11 | - { |
|
| 12 | - return [ |
|
| 13 | - 'is_raw' => true, |
|
| 14 | - ]; |
|
| 15 | - } |
|
| 7 | + /** |
|
| 8 | + * @return array |
|
| 9 | + */ |
|
| 10 | + public static function required() |
|
| 11 | + { |
|
| 12 | + return [ |
|
| 13 | + 'is_raw' => true, |
|
| 14 | + ]; |
|
| 15 | + } |
|
| 16 | 16 | } |
@@ -4,33 +4,33 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Code extends Field |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return string|void |
|
| 9 | - */ |
|
| 10 | - public function build() |
|
| 11 | - { |
|
| 12 | - $this->builder->tag = 'textarea'; |
|
| 13 | - $this->mergeFieldArgs(); |
|
| 14 | - return $this->builder->getTag(); |
|
| 15 | - } |
|
| 7 | + /** |
|
| 8 | + * @return string|void |
|
| 9 | + */ |
|
| 10 | + public function build() |
|
| 11 | + { |
|
| 12 | + $this->builder->tag = 'textarea'; |
|
| 13 | + $this->mergeFieldArgs(); |
|
| 14 | + return $this->builder->getTag(); |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @return array |
|
| 19 | - */ |
|
| 20 | - public static function defaults() |
|
| 21 | - { |
|
| 22 | - return [ |
|
| 23 | - 'class' => 'large-text code', |
|
| 24 | - ]; |
|
| 25 | - } |
|
| 17 | + /** |
|
| 18 | + * @return array |
|
| 19 | + */ |
|
| 20 | + public static function defaults() |
|
| 21 | + { |
|
| 22 | + return [ |
|
| 23 | + 'class' => 'large-text code', |
|
| 24 | + ]; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @return array |
|
| 29 | - */ |
|
| 30 | - public static function required() |
|
| 31 | - { |
|
| 32 | - return [ |
|
| 33 | - 'type' => 'textarea', |
|
| 34 | - ]; |
|
| 35 | - } |
|
| 27 | + /** |
|
| 28 | + * @return array |
|
| 29 | + */ |
|
| 30 | + public static function required() |
|
| 31 | + { |
|
| 32 | + return [ |
|
| 33 | + 'type' => 'textarea', |
|
| 34 | + ]; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -4,13 +4,13 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Text 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 | } |