From 65dd87ac94214c9bf5928c3f0f4bf6460b1cc156 Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Thu, 14 Nov 2024 16:29:05 +0700 Subject: [PATCH 01/16] 54309 fix loading script --- plugin/Form.php | 12 + ...criptLoaderGenericConfigurationDefault.php | 325 ++++++------------ plugin/Template.php | 28 ++ 3 files changed, 139 insertions(+), 226 deletions(-) diff --git a/plugin/Form.php b/plugin/Form.php index e9e2ce7a1..3a3a3040d 100644 --- a/plugin/Form.php +++ b/plugin/Form.php @@ -645,6 +645,18 @@ public function getFormId() } + /** + * + * @return string + * + */ + + public function getFormType() + { + return esc_html($this->getDataFormConfiguration()->getFormType()); + } + + /** * * @return bool diff --git a/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php b/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php index fee60b663..26def7a4e 100644 --- a/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php +++ b/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php @@ -30,6 +30,7 @@ use onOffice\WPlugin\Record\RecordManagerReadForm; use onOffice\WPlugin\Form; use DI\ContainerBuilder; +use onOffice\WPlugin\Template; /** * @@ -70,8 +71,6 @@ public function getScriptLoaderGenericConfiguration(): array new IncludeFileModel($style, 'onoffice-forms', plugins_url('/css/onoffice-forms.css', $pluginPath)), new IncludeFileModel($style, 'select2', plugins_url('/vendor/select2/select2/dist/css/select2.min.css', $pluginPath)) ]; - //ensure to load 3rd party styles on detail page before onOffice style - $values = array_merge($values, $this->renderStyleForEstateDetailPage($pluginPath, $style)); $styleVersion = $this->getOnOfficeStyleVersion(); $onOfficeStyleUri = $this->getStyleUriByVersion($styleVersion); $values []= (new IncludeFileModel($style, $styleVersion, $onOfficeStyleUri)); @@ -79,163 +78,111 @@ public function getScriptLoaderGenericConfiguration(): array $values []= (new IncludeFileModel($script, 'onoffice-favorites', plugins_url('/dist/favorites.min.js', $pluginPath))) ->setDependencies(['jquery']); } - $values = $this->addAddressStyle($values, $style, $pluginPath); - return array_merge($values, $this->addScripts($pluginPath, $script, $style, $defer, $async)); + return $values; } /** - * @param string $pluginPath - * @param string $script - * @param string $style - * @param string $defer - * @param string $async - * @return array + * @param string $formType + * @param bool $recaptcha */ - private function addScripts(string $pluginPath, string $script, string $style, string $defer, string $async): array + public function addFormScripts(string $formType, bool $recaptcha) { - $scripts = []; - $pageContent = get_the_content(); - - $shortcode = $this->getShortcodeByPostMeta(); - - if (empty($pageContent)) { - return $scripts; - } - - $pageContent = str_replace('\u0022', '"', $pageContent); - if ($this->isAddressListPage($pageContent) || !empty($shortcode['address'])) { - $scripts = $this->renderScriptForAddressListPage($scripts, $pluginPath, $script); - } - - if ($this->isEstateListPage($pageContent) || !empty($shortcode['estate'])) { - $scripts = $this->renderScriptForEstateListPage($scripts, $pluginPath, $script, $async); - } - - if ($this->isDetailEstatePage($pageContent) || !empty($shortcode['detail'])) { - $scripts = $this->renderScriptForEstateDetailPage($scripts, $pluginPath, $script, $defer); - } - - $shortcodeFormForDetailPage = !empty(get_option('onoffice-default-view')) ? get_option('onoffice-default-view')->getShortCodeForm() : ''; - if ($this->isFormPage($pageContent) || (is_array($shortcode) && !empty($shortcode['form'])) - || ($this->isDetailEstatePage($pageContent) && ! empty($shortcodeFormForDetailPage)) - || ($this->isDetailAddressPage($pageContent) && ! empty($shortcodeFormForDetailPage))) { - $pageContent = ($this->isFormPage($pageContent) || $this->isDetailEstatePage($pageContent) || $this->isDetailAddressPage($pageContent)) ? $pageContent : $shortcode['form']; - $scripts = $this->renderScriptForFormPage($scripts, $pluginPath, $script, $pageContent, $async, $shortcodeFormForDetailPage); - } - - return $scripts; + $pluginPath = ONOFFICE_PLUGIN_DIR.'/index.php'; + $scriptType = IncludeFileModel::TYPE_SCRIPT; + $async = IncludeFileModel::LOAD_ASYNC; + + $scripts = $this->renderScriptForForm([], $pluginPath, $scriptType, $async, $formType, $recaptcha); + $this->enqueueScripts($scripts); } /** - * @return array + * @param string $key */ - private function getShortcodeByPostMeta(): array + + public function addEstateScripts(string $key) { - $metaKeys = get_post_meta(get_the_ID(), '', true); + $pluginPath = ONOFFICE_PLUGIN_DIR.'/index.php'; + $scriptType = IncludeFileModel::TYPE_SCRIPT; + $async = IncludeFileModel::LOAD_ASYNC; + $style = IncludeFileModel::TYPE_STYLE; + $scripts = []; + $styles = []; - if (!is_array($metaKeys) || empty($metaKeys)) { - return []; + if ($key === Template::KEY_ESTATELIST) { + $scripts = $this->renderScriptForEstateListPage([], $pluginPath, $scriptType, $async); } - - $filteredMetaKeys = []; - array_walk($metaKeys, function($metaValueArray, $key) use (&$filteredMetaKeys) { - $metaValue = ''; - if (isset($metaValueArray[0])) { - $metaValue = str_replace('\u0022', '"', $metaValueArray[0]); - } - if ($this->isEstateListPage($metaValue)) { - $filteredMetaKeys['estate'] = $metaValue; - } elseif ($this->isAddressListPage($metaValue)) { - $filteredMetaKeys['address'] = $metaValue; - } elseif ($this->isDetailEstatePage($metaValue) || $this->isDetailAddressPage($metaValue)) { - $filteredMetaKeys['detail'] = $metaValue; - } elseif ($this->isFormPage($metaValue)) { - $filteredMetaKeys['form'][] = $metaValue; - } - }); - - return $filteredMetaKeys; + if ($key === Template::KEY_ESTATEDETAIL) { + $scripts = $this->renderScriptForEstateDetailPage([], $pluginPath, $scriptType, $async); + $styles = $this->renderStyleForEstateDetailPage($pluginPath, $style); + } + $this->enqueueScripts($scripts); + $this->enqueueStyles($styles); } /** - * @param string $content - * @return bool + * @param string $key */ - private function isEstateListPage(string $content): bool - { - return ($this->matchesShortcode($content, self::ESTATE_TAG, 'view', '[^"]*') && - !$this->matchesShortcode($content, self::ESTATE_TAG, 'view', 'detail')) || - ($this->matchesShortcode($content, self::ESTATE_TAG, 'units', '[^"]*') && - !$this->matchesShortcode($content, self::ESTATE_TAG, 'view', 'detail')); - } - /** - * @param string $content - * @return bool - */ - private function isDetailEstatePage(string $content): bool + public function addAddressScripts(string $key) { - return $this->matchesShortcode($content, self::ESTATE_TAG, 'view', 'detail'); - } - /** - * @param string $content - * @return bool - */ - private function isAddressListPage(string $content): bool - { - return ($this->matchesShortcode($content, self::ADDRESS_TAG, 'view', '[^"]*') && - !$this->matchesShortcode($content, self::ADDRESS_TAG, 'view', 'detail')) || - ($this->matchesShortcode($content, self::ADDRESS_TAG, 'units', '[^"]*') && - !$this->matchesShortcode($content, self::ADDRESS_TAG, 'view', 'detail')); + $pluginPath = ONOFFICE_PLUGIN_DIR.'/index.php'; + $scriptType = IncludeFileModel::TYPE_SCRIPT; + $defer = IncludeFileModel::LOAD_DEFER; + $style = IncludeFileModel::TYPE_STYLE; + $styles = []; + + if ($key === Template::KEY_ADDRESSLIST) { + $scripts = $this->renderScriptForAddressListPage([], $pluginPath, $scriptType); + } + if ($key === Template::KEY_ADDRESSDETAIL) { + $scripts = $this->renderScriptForAddressDetailPage([], $pluginPath, $scriptType, $defer); + $styles = $this->renderStyleForAddressDetail($style, $pluginPath); + } + $this->enqueueScripts($scripts); + $this->enqueueStyles($styles); } - + /** - * @param string $content - * @return bool + * @param array $scripts + * @return void */ - private function isDetailAddressPage(string $content): bool + private function enqueueScripts(array $scripts) { - return $this->matchesShortcode($content, self::ADDRESS_TAG, 'view', 'detail'); + foreach ($scripts as $script) { + if (!wp_script_is($script->getIdentifier())) { + wp_enqueue_script($script->getIdentifier(), $script->getFilePath(), $script->getDependencies()); + } + } } - + /** - * @param string $content - * @return bool + * @param array $styles + * @return void */ - private function isFormPage(string $content): bool + private function enqueueStyles(array $styles) { - return $this->matchesShortcode($content, self::FORM_TAG, 'form', '[^"]*'); + foreach ($styles as $style) { + if (!wp_style_is($style->getIdentifier())) { + wp_enqueue_style($style->getIdentifier(), $style->getFilePath(), $style->getDependencies()); + } + } } - /** - * @param string $content - * @param string $tag - * @param string $attribute - * @param string $valuePattern - * @return bool - */ - private function matchesShortcode(string $content, string $tag, string $attribute, string $valuePattern): bool - { - $pattern = '/\[' . $tag . '\s+.*?' . $attribute . '="' . $valuePattern . '".*?\]/'; - return (bool) preg_match($pattern, $content); - } + /** * @param array $scripts * @param string $pluginPath * @param string $script - * @param mixed $pageContent * @param string $async - * @param string $shortcodeFormForDetailPage + * @param string $formType + * @param bool $recaptcha * @return array */ - - private function renderScriptForFormPage(array $scripts, string $pluginPath, string $script, $pageContent, string $async, string $shortcodeFormForDetailPage = ''): array + private function renderScriptForForm(array $scripts, string $pluginPath, string $script, string $async, string $formType, bool $recaptcha): array { - $forms = $this->getFormsByPageContent($pageContent, $shortcodeFormForDetailPage); - $scripts[] = (new IncludeFileModel($script, 'onoffice-custom-select', plugins_url('/dist/onoffice-custom-select.min.js', $pluginPath))) ->setDependencies(['jquery']) ->setLoadInFooter(true); @@ -259,110 +206,40 @@ private function renderScriptForFormPage(array $scripts, string $pluginPath, str $this->localizeApplyThousandSeparatorScript(); } - if ($this->checkFormType($forms, [Form::TYPE_APPLICANT_SEARCH])) { + if ($formType === Form::TYPE_APPLICANT_SEARCH) { $scripts[] = (new IncludeFileModel($script, 'onoffice-form-preview', plugins_url('/dist/onoffice-form-preview.min.js', $pluginPath))) ->setDependencies(['jquery']) ->setLoadInFooter(true); $this->localizeFormPreviewScript(); } - if (get_option('onoffice-settings-honeypot') == true && $this->checkFormType($forms, [Form::TYPE_CONTACT, Form::TYPE_OWNER, Form::TYPE_INTEREST])) { - $scripts[] = (new IncludeFileModel($script, 'onoffice-honeypot', plugins_url('/dist/onoffice-honeypot.min.js', $pluginPath))) - ->setDependencies(['jquery']) - ->setLoadInFooter(true); - } - - if (get_option('onoffice-settings-captcha-sitekey') !== '' && $this->checkCaptchaActive($forms) && - $this->checkFormType($forms, [Form::TYPE_CONTACT, Form::TYPE_OWNER, Form::TYPE_INTEREST])) { - $scripts[] = (new IncludeFileModel($script, 'onoffice-captchacontrol', plugins_url('/dist/onoffice-captchacontrol.min.js', $pluginPath))) - ->setDependencies(['jquery']) - ->setLoadInFooter(false); + if (in_array($formType, [Form::TYPE_CONTACT, Form::TYPE_OWNER, Form::TYPE_INTEREST])) { + if (get_option('onoffice-settings-honeypot')) { + $scripts[] = (new IncludeFileModel($script, 'onoffice-honeypot', plugins_url('/dist/onoffice-honeypot.min.js', $pluginPath))) + ->setDependencies(['jquery']) + ->setLoadInFooter(true); + } + if (get_option('onoffice-settings-captcha-sitekey') !== '' && $recaptcha) { + $scripts[] = (new IncludeFileModel($script, 'onoffice-captchacontrol', plugins_url('/dist/onoffice-captchacontrol.min.js', $pluginPath))) + ->setDependencies(['jquery']) + ->setLoadInFooter(false); + } } return $scripts; } /** - * @param array $forms - * @param array $typeForm - * @return bool - */ - - private function checkFormType(array $forms, array $typeForm): bool - { - return !empty(array_filter($forms, function($form) use ($typeForm) { - return in_array($form->form_type, $typeForm); - })); - } - - /** - * @param array $forms - * - * @return bool - */ - private function checkCaptchaActive(array $forms): bool - { - return !empty(array_filter($forms, function($form) { - return (bool) $form->captcha; - })); - } - - /** - * @param mixed $pageContent - * @param string $shortcodeFormForDetailPage - * @return array - */ - - private function getFormsByPageContent($pageContent, string $shortcodeFormForDetailPage): array - { - if (is_array($pageContent)) { - $pageContent = implode(',', $pageContent); - } - - $pContainerBuilder = new ContainerBuilder; - $pContainerBuilder->addDefinitions(ONOFFICE_DI_CONFIG_PATH); - $pContainer = $pContainerBuilder->build(); - - $names = array_map(function ($item) { - return "'" . esc_sql($item) . "'"; - }, $this->extractFormNames($pageContent)); - $names = implode(',', $names); - - if ($this->isDetailEstatePage($pageContent) || $this->isDetailAddressPage($pageContent)) { - $names = "'" . esc_sql($shortcodeFormForDetailPage) . "'"; - } - - $pRecordManagerReadForm = $pContainer->get(RecordManagerReadForm::class); - $pRecordManagerReadForm->addColumn('form_type'); - $pRecordManagerReadForm->addColumn('captcha'); - $pRecordManagerReadForm->addWhere("`name` IN(" . $names . ")"); - - return $pRecordManagerReadForm->getRecords(); - } - - /** - * @param string $pageContent + * @param string $style + * @param string $pluginPath * @return array */ - - private function extractFormNames(string $pageContent): array + private function renderStyleForAddressDetail(string $style, string $pluginPath): array { - preg_match_all('/\[oo_form form="([^"]+)"\]/', $pageContent, $matches); - return $matches[1]; - } - - /** - * @param array $values - * @param string $style - * @param string $pluginPath - * @return array - */ - private function addAddressStyle(array $values, string $style, string $pluginPath) { - $pageContent = get_the_content(); - if ($this->isDetailAddressPage($pageContent)) { - $values []= (new IncludeFileModel($style, 'onoffice-address-detail', plugins_url('/css/onoffice-address-detail.css', $pluginPath))); - } - return $values; + $styles = [ + new IncludeFileModel($style, 'onoffice-address-detail', plugins_url('/css/onoffice-address-detail.css', $pluginPath)) + ]; + return $styles; } /** @@ -385,6 +262,7 @@ private function renderScriptForAddressDetailPage(array $scripts, string $plugin return $scripts; } + /** * @param array $scripts * @param string $pluginPath @@ -407,26 +285,19 @@ private function renderScriptForEstateDetailPage(array $scripts, string $pluginP } /** - * @param array $styles * @param string $pluginPath * @param string $style * @return array */ - private function renderStyleForEstateDetailPage(string $pluginPath, string $style): array { - $styles = []; - $pageContent = get_the_content(); - if (empty($pageContent)) { - return $styles; - } - $shortcode = $this->getShortcodeByPostMeta(); - $pageContent = str_replace('\u0022', '"', $pageContent); - if ($this->isDetailEstatePage($pageContent) || !empty($shortcode['detail'])) { - $styles[] = new IncludeFileModel($style, 'slick', plugins_url('/third_party/slick/slick.css', $pluginPath)); - $styles[] = new IncludeFileModel($style, 'slick-theme', plugins_url('/third_party/slick/slick-theme.css', $pluginPath)); - } - return $styles; - } + private function renderStyleForEstateDetailPage(string $pluginPath, string $style): array + { + $styles = [ + new IncludeFileModel($style, 'slick', plugins_url('/third_party/slick/slick.css', $pluginPath)), + new IncludeFileModel($style, 'slick-theme', plugins_url('/third_party/slick/slick-theme.css', $pluginPath)) + ]; + return $styles; + } /** * @param array $scripts @@ -437,9 +308,11 @@ private function renderStyleForEstateDetailPage(string $pluginPath, string $styl private function renderScriptForAddressListPage(array $scripts, string $pluginPath, string $script): array { - $scripts[] = (new IncludeFileModel($script, 'onoffice-custom-select', plugins_url('/dist/onoffice-custom-select.min.js', $pluginPath))) + $scripts = [ + (new IncludeFileModel($script, 'onoffice-custom-select', plugins_url('/dist/onoffice-custom-select.min.js', $pluginPath))) ->setDependencies(['jquery']) - ->setLoadInFooter(true); + ->setLoadInFooter(true), + ]; return $scripts; } diff --git a/plugin/Template.php b/plugin/Template.php index f87b1148a..07006793d 100644 --- a/plugin/Template.php +++ b/plugin/Template.php @@ -31,6 +31,7 @@ use onOffice\WPlugin\Utility\__String; use RuntimeException; use const WP_PLUGIN_DIR; +use onOffice\WPlugin\ScriptLoader\ScriptLoaderGenericConfigurationDefault; /** * @@ -47,6 +48,12 @@ class Template /** */ const KEY_ADDRESSLIST = 'addresslist'; + /** */ + const KEY_ESTATEDETAIL = 'estatedetail'; + + /** */ + const KEY_ADDRESSDETAIL = 'addressdetail'; + /** @var string */ private $_templateName = ''; @@ -112,6 +119,27 @@ private static function getIncludeContents(array $templateData, $templatePath, C $pTemplateCallback = $pContainer->get(TemplateCallbackBuilder::class); $generateSortDropDown = $pTemplateCallback->buildCallbackListSortDropDown($pEstates); $getListName = $pTemplateCallback->buildCallbackEstateListName($pEstates); + $scriptLoader = $pContainer->get(ScriptLoaderGenericConfigurationDefault::class); + + if (!empty($pEstates)) { + if ($pEstates instanceof EstateList && !($pEstates instanceof EstateDetail)) { + $scriptLoader->addEstateScripts(self::KEY_ESTATELIST); + } elseif ($pEstates instanceof EstateDetail) { + $scriptLoader->addEstateScripts(self::KEY_ESTATEDETAIL); + } + } + + if (!empty($pForm)) { + $scriptLoader->addFormScripts($pForm->getFormType(), $pForm->needsReCaptcha()); + } + + if (!empty($pAddressList)) { + if ($pAddressList instanceof AddressList && !($pAddressList instanceof AddressDetail)) { + $scriptLoader->addAddressScripts(self::KEY_ADDRESSLIST); + } elseif ($pAddressList instanceof AddressDetail) { + $scriptLoader->addAddressScripts(self::KEY_ADDRESSDETAIL); + } + } unset($templateData, $pTemplateCallback, $pContainer); ob_start(); From b617c4a01a3c416d9fe8f84d010263023f51f6d7 Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Fri, 15 Nov 2024 14:37:24 +0700 Subject: [PATCH 02/16] 54309 refactor code --- plugin/Template.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugin/Template.php b/plugin/Template.php index 07006793d..01c4a882c 100644 --- a/plugin/Template.php +++ b/plugin/Template.php @@ -32,6 +32,8 @@ use RuntimeException; use const WP_PLUGIN_DIR; use onOffice\WPlugin\ScriptLoader\ScriptLoaderGenericConfigurationDefault; +use onOffice\WPlugin\DataView\DataDetailView; +use onOffice\WPlugin\DataView\DataListView; /** * @@ -122,9 +124,9 @@ private static function getIncludeContents(array $templateData, $templatePath, C $scriptLoader = $pContainer->get(ScriptLoaderGenericConfigurationDefault::class); if (!empty($pEstates)) { - if ($pEstates instanceof EstateList && !($pEstates instanceof EstateDetail)) { + if ($pEstates->getDataView() instanceof DataListView) { $scriptLoader->addEstateScripts(self::KEY_ESTATELIST); - } elseif ($pEstates instanceof EstateDetail) { + } elseif ($pEstates->getDataView() instanceof DataDetailView) { $scriptLoader->addEstateScripts(self::KEY_ESTATEDETAIL); } } From 2f666f300b54d2969a57b4c35371908039eb0fd6 Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Fri, 15 Nov 2024 14:52:19 +0700 Subject: [PATCH 03/16] 54309 refactor code --- .../ScriptLoaderGenericConfigurationDefault.php | 2 +- plugin/Template.php | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php b/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php index 26def7a4e..48ea25210 100644 --- a/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php +++ b/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php @@ -131,13 +131,13 @@ public function addAddressScripts(string $key) $scriptType = IncludeFileModel::TYPE_SCRIPT; $defer = IncludeFileModel::LOAD_DEFER; $style = IncludeFileModel::TYPE_STYLE; + $scripts = []; $styles = []; if ($key === Template::KEY_ADDRESSLIST) { $scripts = $this->renderScriptForAddressListPage([], $pluginPath, $scriptType); } if ($key === Template::KEY_ADDRESSDETAIL) { - $scripts = $this->renderScriptForAddressDetailPage([], $pluginPath, $scriptType, $defer); $styles = $this->renderStyleForAddressDetail($style, $pluginPath); } $this->enqueueScripts($scripts); diff --git a/plugin/Template.php b/plugin/Template.php index 01c4a882c..5cd808db9 100644 --- a/plugin/Template.php +++ b/plugin/Template.php @@ -34,6 +34,8 @@ use onOffice\WPlugin\ScriptLoader\ScriptLoaderGenericConfigurationDefault; use onOffice\WPlugin\DataView\DataDetailView; use onOffice\WPlugin\DataView\DataListView; +use onOffice\WPlugin\DataView\DataListViewAddress; +use onOffice\WPlugin\DataView\DataAddressDetailView; /** * @@ -136,9 +138,9 @@ private static function getIncludeContents(array $templateData, $templatePath, C } if (!empty($pAddressList)) { - if ($pAddressList instanceof AddressList && !($pAddressList instanceof AddressDetail)) { + if ($pAddressList->getDataViewAddress() instanceof DataListViewAddress) { $scriptLoader->addAddressScripts(self::KEY_ADDRESSLIST); - } elseif ($pAddressList instanceof AddressDetail) { + } elseif ($pAddressList->getDataViewAddress() instanceof DataAddressDetailView) { $scriptLoader->addAddressScripts(self::KEY_ADDRESSDETAIL); } } From c8e42bcc6b4e5e758bf169323ad7826e1ce8c7a3 Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Mon, 18 Nov 2024 10:05:38 +0700 Subject: [PATCH 04/16] 54309 refactor code --- ...criptLoaderGenericConfigurationDefault.php | 56 +++++++++---------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php b/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php index 48ea25210..658043fc1 100644 --- a/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php +++ b/plugin/ScriptLoader/ScriptLoaderGenericConfigurationDefault.php @@ -153,6 +153,7 @@ private function enqueueScripts(array $scripts) foreach ($scripts as $script) { if (!wp_script_is($script->getIdentifier())) { wp_enqueue_script($script->getIdentifier(), $script->getFilePath(), $script->getDependencies()); + $this->localizeScript($script->getIdentifier()); } } } @@ -170,8 +171,6 @@ private function enqueueStyles(array $styles) } } - - /** * @param array $scripts * @param string $pluginPath @@ -203,14 +202,18 @@ private function renderScriptForForm(array $scripts, string $pluginPath, string $scripts[] = (new IncludeFileModel($script, 'onoffice-apply-thousand-separator', plugins_url('dist/onoffice-apply-thousand-separator.min.js', $pluginPath))) ->setDependencies(['jquery']) ->setLoadInFooter(true); - $this->localizeApplyThousandSeparatorScript(); + } + + if (get_option('onoffice-settings-captcha-sitekey') !== '' && $recaptcha) { + $scripts[] = (new IncludeFileModel($script, 'onoffice-captchacontrol', plugins_url('/dist/onoffice-captchacontrol.min.js', $pluginPath))) + ->setDependencies(['jquery']) + ->setLoadInFooter(false); } if ($formType === Form::TYPE_APPLICANT_SEARCH) { $scripts[] = (new IncludeFileModel($script, 'onoffice-form-preview', plugins_url('/dist/onoffice-form-preview.min.js', $pluginPath))) ->setDependencies(['jquery']) ->setLoadInFooter(true); - $this->localizeFormPreviewScript(); } if (in_array($formType, [Form::TYPE_CONTACT, Form::TYPE_OWNER, Form::TYPE_INTEREST])) { @@ -219,11 +222,6 @@ private function renderScriptForForm(array $scripts, string $pluginPath, string ->setDependencies(['jquery']) ->setLoadInFooter(true); } - if (get_option('onoffice-settings-captcha-sitekey') !== '' && $recaptcha) { - $scripts[] = (new IncludeFileModel($script, 'onoffice-captchacontrol', plugins_url('/dist/onoffice-captchacontrol.min.js', $pluginPath))) - ->setDependencies(['jquery']) - ->setLoadInFooter(false); - } } return $scripts; @@ -348,25 +346,31 @@ private function renderScriptForEstateListPage(array $scripts, string $pluginPat ->setDependencies(['jquery']) ->setLoadInFooter(true); - $this->localizeFormPreviewScript(); - $this->localizeApplyThousandSeparatorScript(); - return $scripts; } /** + * @param string $scriptIdentifier * @return void */ - private function localizeFormPreviewScript() + private function localizeScript(string $scriptIdentifier) { - wp_localize_script('onoffice-form-preview', 'onoffice_form_preview_strings', [ - 'amount_none' => __('0 matches', 'onoffice-for-wp-websites'), - 'amount_one' => __('Show exact match', 'onoffice-for-wp-websites'), - /* translators: %s is the amount of results */ - 'amount_other' => __('Show %s matches', 'onoffice-for-wp-websites'), - 'nonce_estate' => wp_create_nonce('onoffice-estate-preview'), - 'nonce_applicant_search' => wp_create_nonce('onoffice-applicant-search-preview'), - ]); + switch ($scriptIdentifier) { + case 'onoffice-form-preview': + wp_localize_script('onoffice-form-preview', 'onoffice_form_preview_strings', [ + 'amount_none' => __('0 matches', 'onoffice-for-wp-websites'), + 'amount_one' => __('Show exact match', 'onoffice-for-wp-websites'), + 'amount_other' => __('Show %s matches', 'onoffice-for-wp-websites'), + 'nonce_estate' => wp_create_nonce('onoffice-estate-preview'), + 'nonce_applicant_search' => wp_create_nonce('onoffice-applicant-search-preview'), + ]); + break; + case 'onoffice-apply-thousand-separator': + wp_localize_script('onoffice-apply-thousand-separator', 'onoffice_apply_thousand_separator', [ + 'thousand_separator_format' => get_option('onoffice-settings-thousand-separator') + ]); + break; + } } /** @@ -437,14 +441,4 @@ public function getOnOfficeStyleVersion() } return $onofficeCssStyleVersion; } - - /** - * @return void - */ - private function localizeApplyThousandSeparatorScript() - { - wp_localize_script('onoffice-apply-thousand-separator', 'onoffice_apply_thousand_separator', [ - 'thousand_separator_format' => get_option('onoffice-settings-thousand-separator') - ]); - } } From 1a4ce0ae37b52d5f31047df6ad471ed84aa0995b Mon Sep 17 00:00:00 2001 From: "dai.trinh" Date: Mon, 18 Nov 2024 10:21:17 +0700 Subject: [PATCH 05/16] 54309 refactor code --- dist/onoffice-captchacontrol.min.js | 2 +- js/onoffice-captchacontrol.js | 3 +++ .../ScriptLoader/ScriptLoaderGenericConfigurationDefault.php | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dist/onoffice-captchacontrol.min.js b/dist/onoffice-captchacontrol.min.js index 8e3efc513..a560bc048 100644 --- a/dist/onoffice-captchacontrol.min.js +++ b/dist/onoffice-captchacontrol.min.js @@ -1 +1 @@ -var onOffice=onOffice||{};var CaptchaCallback=function(){const recaptchaElements=document.querySelectorAll(".g-recaptcha");recaptchaElements.forEach((function(element){const sitekey=element.getAttribute("data-sitekey");const size=element.getAttribute("data-size");const callback=element.getAttribute("data-callback");const grecaptchaId=grecaptcha.render(element,{sitekey:sitekey,size:size,callback:callback});element.setAttribute("data-grecaptcha-id",grecaptchaId)}))};(function(){onOffice.captchaControl=function(formElement,submitButtonElement){submitButtonElement.onclick=function(event){const recaptcha=formElement.querySelector(".g-recaptcha");const grecaptchaId=recaptcha.getAttribute("data-grecaptcha-id");event.preventDefault();if(!formElement.checkValidity()&&!_isMSIE()){formElement.reportValidity()}else{if(window.grecaptcha&&typeof window.grecaptcha.execute==="function"){window.grecaptcha.execute(grecaptchaId)}}}};var _isMSIE=function(){var userAgent=window.navigator.userAgent;var iePosition=userAgent.indexOf("MSIE ");return iePosition>0||!!navigator.userAgent.match(/Trident.*rv\:11\./)}})();jQuery(document).ready((function($){function addRecaptchaScript(){if(!$("#recaptcha-script").length){$("