From 909ab6fadb9a2e7612061f77030978218fd531c4 Mon Sep 17 00:00:00 2001 From: svfcode Date: Sun, 29 Dec 2024 11:00:13 +0300 Subject: [PATCH] Upd. Code. Refactoring to compatible with psalm L3. --- .../FormDecorator/FormDecoratorSettings.php | 37 +- .../PluginSettingsPage/SettingsField.php | 573 +++++++++++------- psalm_l3.xml | 2 - 3 files changed, 372 insertions(+), 240 deletions(-) diff --git a/lib/Cleantalk/ApbctWP/FormDecorator/FormDecoratorSettings.php b/lib/Cleantalk/ApbctWP/FormDecorator/FormDecoratorSettings.php index d452faf5e..d9e071c55 100644 --- a/lib/Cleantalk/ApbctWP/FormDecorator/FormDecoratorSettings.php +++ b/lib/Cleantalk/ApbctWP/FormDecorator/FormDecoratorSettings.php @@ -15,27 +15,36 @@ public static function filterSelectorParams($params, $disabled, $popup) { $params['nest_label_and_description_after_select'] = true; - $select = ''; + $data = array( + 'name' => isset($params['name']) ? $params['name'] : '', + 'type' => isset($params['type']) ? $params['type'] : '', + 'multiple' => isset($params['multiple']) && $params['multiple'], + 'size' => isset($params['multiple'], $params['options']) && $params['multiple'] ? 'size="' . count($params['options']) . '"' : '', + 'multiple_attr' => isset($params['multiple']) && $params['multiple'] ? ' multiple="multiple"' : '', + 'required_attr' => isset($params['required']) && $params['required'] ? ' required="required"' : '', + 'disabled' => $disabled, + ); + + $select_layout = 'params['name']}'" - . ($this->value == '1' ? ' checked' : '') - . $this->disabled_string - . ($this->params['required'] ? ' required="required"' : '') - . ($this->params['childrens'] ? ' apbct_children="' . $this->children_string . '"' : '') - . ' onchange="' - . ($this->params['childrens'] ? ' apbctSettingsDependencies(\'' . $this->children_string . '\');' : '') - . ($this->params['hide'] ? ' apbctShowHideElem(\'' . $this->hide_string . '\');' : '') - . '"' - . ' />' - . '' - . $this->description_popup; - //HANDLE LINK - $href = '' . __('CleanTalk Affiliate Program are here', 'cleantalk-spam-protect') . ''; - $this->params['description'] = str_replace('{CT_AFFILIATE_TERMS}', $href, $this->params['description']); - $out .= '
' - . $this->params['description'] - . '
'; + if (isset($this->params['description'])) { + $href = '' . __('CleanTalk Affiliate Program are here', 'cleantalk-spam-protect') . ''; + $this->params['description'] = str_replace('{CT_AFFILIATE_TERMS}', $href, $this->params['description']); + } + + $data = [ + 'name' => isset($this->params['name']) ? $this->params['name'] : '', + 'type' => isset($this->params['type']) ? $this->params['type'] : '', + 'value' => $this->value, + 'disabled' => $this->disabled_string, + 'required' => isset($this->params['required']) && $this->params['required'] ? 'required="required"' : '', + 'childrens' => isset($this->params['childrens']) && $this->params['childrens'] ? ' onchange="apbctSettingsDependencies(\'' . $this->children_string . '\')"' : '', + 'childrens_string' => isset($this->params['childrens']) && $this->params['childrens'] ? ' apbct_children="' . $this->children_string . '"' : '', + 'title' => isset($this->params['title']) ? $this->params['title'] : '', + 'popup_description' => isset($this->description_popup) ? $this->description_popup : '', + 'description' => isset($this->params['description']) ? $this->params['description'] : '', + 'checked' => $this->value == '1' ? ' checked' : '', + 'hide' => isset($this->params['hide']) && $this->params['hide'] ? ' apbctShowHideElem(\'' . $this->hide_string . '\');' : '', + ]; + + $layout = ' + + {{popup_description}} +
{{description}}
'; + + $out = $layout; + foreach ($data as $key => $value) { + $out = str_replace('{{' . $key . '}}', $value, $out); + } + return $out; } @@ -182,40 +224,61 @@ private function getInputCheckBox() */ private function getInputRadio() { + $data = [ + 'title' => isset($this->params['title']) ? $this->params['title'] : '', + 'type' => isset($this->params['type']) ? $this->params['type'] : '', + 'popup_description' => isset($this->description_popup) ? $this->description_popup : '', + ]; + $out = isset($this->params['title']) - ? '

' . $this->params['title'] . $this->description_popup . '

' + ? '

{{title}} {{popup_description}}

' : ''; - //ESC NEED - $out .= '
'; + + $out .= '
'; $out .= '
'; - foreach ( $this->params['options'] as $option ) { - //ESC NEED - $out .= 'params['name']}'" - . " id='apbct_setting_{$this->params['name']}__{$option['label']}'" - . ' name="cleantalk_settings[' . $this->params['name'] . ']"' - . ' value="' . $option['val'] . '"' - . $this->disabled_string - . ($this->params['childrens'] - ? ' onchange="apbctSettingsDependencies(\'' . $this->children_string . '\', ' . $option['childrens_enable'] . ')"' - : '' - ) - . ($this->value == $option['val'] ? ' checked' : '') - . ($this->params['required'] ? ' required="required"' : '') - . ' />'; - //ESC NEED - $out .= ''; - $out .= '     '; + + if (isset($this->params['options'])) { + foreach ( $this->params['options'] as $option ) { + $option_data = [ + 'type' => isset($this->params['type']) ? $this->params['type'] : '', + 'name' => isset($this->params['name']) ? $this->params['name'] : '', + 'name_id' => isset($this->params['name'], $option['label']) ? $this->params['name'] . '__' . $option['label'] : '', + 'value' => $option['val'], + 'disabled' => $this->disabled_string, + 'childrens' => isset($this->params['childrens'], $option['childrens_enable']) ? ' onchange="apbctSettingsDependencies(\'' . $this->children_string . '\', ' . $option['childrens_enable'] . ')"' : '', + 'required' => isset($this->params['required']) && $this->params['required'] ? 'required="required"' : '', + 'checked' => $this->value == $option['val'] ? ' checked' : '', + 'label' => isset($option['label']) ? $option['label'] : '', + ]; + + $option_layout = ' +      '; + + $option = $option_layout; + foreach ($option_data as $key => $value) { + $option = str_replace('{{' . $key . '}}', $value, $option); + } + + $out .= $option; + } } + $out .= '
'; - //ESC NEED + $out .= isset($this->params['description']) ? '
' . $this->params['description'] . '
' : ''; + $out .= '
'; - return $out; + + $output = $out; + foreach ($data as $key => $value) { + $output = str_replace('{{' . $key . '}}', $value, $output); + } + + return $output; } /** @@ -223,54 +286,74 @@ private function getInputRadio() */ private function getInputSelect() { - if ($this->params['name'] === 'comments__form_decoration_selector') { + if (isset($this->params['name']) && $this->params['name'] === 'comments__form_decoration_selector') { $this->params = FormDecoratorSettings::filterSelectorParams($this->params, $this->disabled_string, $this->description_popup); } - $title = ''; + $data = [ + 'name' => isset($this->params['name']) ? $this->params['name'] : '', + 'type' => isset($this->params['type']) ? $this->params['type'] : '', + 'title' => isset($this->params['title']) ? $this->params['title'] : '', + 'popup_description' => isset($this->description_popup) ? $this->description_popup : '', + 'description' => isset($this->params['description']) ? $this->params['description'] : '', + 'multiple' => isset($this->params['multiple']) ? '[]' : '', + 'multiple_bool' => isset($this->params['multiple']) ? 'multiple="multiple"' : '', + 'size' => isset($this->params['multiple'], $this->params['options']) ? ' size="' . count($this->params['options']) . '"' : '', + 'childrens' => isset($this->params['childrens']) ? ' onchange="apbctSettingsDependencies(\'' . $this->children_string . '\', jQuery(this).find(\'option:selected\').data(\'children_enable\'))"' : '', + 'disabled' => $this->disabled_string, + 'required' => isset($this->params['required']) && $this->params['required'] ? 'required="required"' : '', + ]; + + // title if (isset($this->params['custom_select_title'])) { $title = $this->params['custom_select_title']; - } elseif (isset($this->params['title'])) { - $title = '

' . $this->params['title'] . $this->description_popup . '

'; + } else { + $title = '

{{title}} {{popup_description}}

'; } - $description = ''; + // description if (isset($this->params['custom_select_description'])) { $description = $this->params['custom_select_description']; - } elseif (isset($this->params['description'])) { - $description = '
' . $this->params['description'] . '
'; + } else { + $description = '
{{description}}
'; } + // select if (isset($this->params['custom_select_element'])) { $select = $this->params['custom_select_element']; } else { - $select = 'params['name']}'" - . ' name="cleantalk_settings[' . $this->params['name'] . ']' . ($this->params['multiple'] ? '[]"' : '"') - . ($this->params['multiple'] ? ' size="' . count($this->params['options']) . '""' : '') - . ($this->params['multiple'] ? ' multiple="multiple"' : '') - . ($this->params['childrens'] - ? ' onchange="apbctSettingsDependencies(\'' . $this->children_string . '\', jQuery(this).find(\'option:selected\').data(\'children_enable\'))"' - : '' - ) - . $this->disabled_string - . ($this->params['required'] ? ' required="required"' : '') - . ' >'; + $select = ''; @@ -281,7 +364,12 @@ private function getInputSelect() $output = $title . $description . $select; } - return $output; + $out = $output; + foreach ($data as $key => $value) { + $out = str_replace('{{' . $key . '}}', $value, $out); + } + + return $out; } /** @@ -289,24 +377,33 @@ private function getInputSelect() */ private function getInputText() { - $out = 'params['name']}'" - . ' value="' . $this->value . '" ' - . (isset($this->params['placeholder']) ? ' placeholder="' . $this->params['placeholder'] : '') . '" ' - . $this->disabled_string - . ($this->params['required'] ? ' required="required"' : '') - . ($this->params['childrens'] ? ' onchange="apbctSettingsDependencies(\'' . $this->children_string . '\')"' : '') - . ' />' - . ' ' - . ''; - $out .= '
' - . $this->params['description'] - . '
'; + $data = [ + 'name' => isset($this->params['name']) ? $this->params['name'] : '', + 'type' => isset($this->params['type']) ? $this->params['type'] : '', + 'value' => $this->value, + 'placeholder' => isset($this->params['placeholder']) ? 'placeholder="' . $this->params['placeholder'] . '"' : '', + 'disabled' => $this->disabled_string, + 'required' => isset($this->params['required']) && $this->params['required'] ? 'required="required"' : '', + 'childrens' => isset($this->params['childrens']) ? 'onchange="apbctSettingsDependencies(\'' . $this->children_string . '\')"' : '', + 'title' => isset($this->params['title']) ? $this->params['title'] : '', + 'popup_description' => isset($this->description_popup) ? $this->description_popup : '', + 'description' => isset($this->params['description']) ? $this->params['description'] : '', + ]; + + $layout = ' +   + +
{{description}}
'; + + $out = $layout; + foreach ($data as $key => $value) { + $out = str_replace('{{' . $key . '}}', $value, $out); + } + return $out; } @@ -315,24 +412,34 @@ private function getInputText() */ private function getAffiliateShortCode() { - $out = 'params['name']}'" - . ' value="[cleantalk_affiliate_link]" ' - . "readonly" //hardcode for this shortcode - . $this->disabled_string - . ($this->params['required'] ? ' required="required"' : '') - . ($this->params['childrens'] ? ' onchange="apbctSettingsDependencies(\'' . $this->children_string . '\')"' : '') - . ' />' - . ' ' - . ''; - $out .= '
' - . $this->params['description'] - . '
'; + $data = [ + 'name' => isset($this->params['name']) ? $this->params['name'] : '', + 'type' => isset($this->params['type']) ? $this->params['type'] : '', + 'value' => '[cleantalk_affiliate_link]', + 'title' => isset($this->params['title']) ? $this->params['title'] : '', + 'popup_description' => isset($this->description_popup) ? $this->description_popup : '', + 'description' => isset($this->params['description']) ? $this->params['description'] : '', + 'disabled' => $this->disabled_string, + 'required' => isset($this->params['required']) && $this->params['required'] ? 'required="required"' : '', + 'childrens' => isset($this->params['childrens']) ? 'onchange="apbctSettingsDependencies(\'' . $this->children_string . '\')"' : '', + ]; + + $layout = ' +   + +
{{description}}
'; + + $out = $layout; + foreach ($data as $key => $value) { + $out = str_replace('{{' . $key . '}}', $value, $out); + } + return $out; } @@ -341,24 +448,35 @@ private function getAffiliateShortCode() */ private function getInputTextarea() { - $out = isset($this->params['title']) - ? '

' . $this->params['title'] . $this->description_popup . '

' - : ''; - //ESC NEED - $out .= '
' - . $this->params['description'] - . '
'; - //ESC NEED - $out .= '' - . ' '; - return $out; + $title_layout = '

{{title}} {{popup_description}}

'; + + $data = [ + 'title' => isset($this->params['title']) ? $this->params['title'] : '', + 'type' => isset($this->params['type']) ? $this->params['type'] : '', + 'popup_description' => $this->description_popup, + 'description' => isset($this->params['description']) ? $this->params['description'] : '', + 'name' => isset($this->params['name']) ? $this->params['name'] : '', + 'disabled' => $this->disabled_string, + 'required' => isset($this->params['required']) && $this->params['required'] ? 'required="required"' : '', + 'childrens' => isset($this->params['childrens']) ? 'onchange="apbctSettingsDependencies(\'' . $this->children_string . '\')" ' : '', + 'value' => $this->value, + ]; + + $layout = ''; + if (isset($this->params['title'])) { + $layout .= $title_layout; + } + + $layout .= '
{{description}}
+  '; + + foreach ($data as $key => $value) { + $layout = str_replace('{{' . $key . '}}', $value, $layout); + } + + return $layout; } /** @@ -366,25 +484,32 @@ private function getInputTextarea() */ private function getInputColor() { - $out = ''; - //ESC NEED - $out .= 'params['name']}'" - . ' value="' . $this->value . '" ' - . $this->disabled_string - . ($this->params['required'] ? ' required="required"' : '') - . ($this->params['childrens'] ? ' onchange="apbctSettingsDependencies(\'' . $this->children_string . '\')"' : '') - . ' />' - . ' ' - . ''; - $out .= '
' - . $this->params['description'] - . '
'; + $data = [ + 'name' => isset($this->params['name']) ? $this->params['name'] : '', + 'type' => isset($this->params['type']) ? $this->params['type'] : '', + 'value' => $this->value, + 'disabled' => $this->disabled_string, + 'required' => isset($this->params['required']) && $this->params['required'] ? 'required="required"' : '', + 'childrens' => isset($this->params['childrens']) ? 'onchange="apbctSettingsDependencies(\'' . $this->children_string . '\')" ' : '', + 'title' => isset($this->params['title']) ? $this->params['title'] : '', + 'popup_description' => isset($this->description_popup) ? $this->description_popup : '', + 'description' => isset($this->params['description']) ? $this->params['description'] : '', + ]; + + $layout = '   + +
{{description}}
'; + + $out = $layout; + foreach ($data as $key => $value) { + $value = is_array($value) ? implode(', ', $value) : (string) $value; + $out = str_replace('{{' . $key . '}}', $value, $out); + } + return $out; } } diff --git a/psalm_l3.xml b/psalm_l3.xml index 0bba2e443..b182e7a82 100644 --- a/psalm_l3.xml +++ b/psalm_l3.xml @@ -29,8 +29,6 @@ - -