diff --git a/includes/class-convertkit-preview-output.php b/includes/class-convertkit-preview-output.php index 5eb80465..685985be 100644 --- a/includes/class-convertkit-preview-output.php +++ b/includes/class-convertkit-preview-output.php @@ -72,7 +72,7 @@ public function preview_form( $form_id ) { } /** - * Adds a non-inline form for display if the request is from a logged in user who has clicked a preview link + * Adds non-inline form(s) for display if the request is from a logged in user who has clicked a preview link * and is viewing the home page. * * @since 2.3.3 @@ -99,33 +99,40 @@ public function preview_non_inline_form() { return; } - // Determine the form to preview. - $preview_form_id = (int) ( isset( $_REQUEST['convertkit_form_id'] ) ? sanitize_text_field( $_REQUEST['convertkit_form_id'] ) : 0 ); + // Bail if no form(s) to preview. + if ( ! isset( $_REQUEST['convertkit_form_id'] ) ) { + return; + } - // Get form. + // Determine form ID(s) to preview. $convertkit_forms = new ConvertKit_Resource_Forms(); - $form = $convertkit_forms->get_by_id( $preview_form_id ); + $preview_form_ids = explode( ',', sanitize_text_field( $_REQUEST['convertkit_form_id'] ) ); - // Bail if the Form doesn't exist (this shouldn't happen, but you never know). - if ( ! $form ) { - return; - } + foreach ( $preview_form_ids as $preview_form_id ) { + // Get form. + $form = $convertkit_forms->get_by_id( (int) $preview_form_id ); - // Add the form to the scripts array so it is included in the preview. - add_filter( - 'convertkit_output_scripts_footer', - function ( $scripts ) use ( $form ) { + // Bail if the Form doesn't exist (this shouldn't happen, but you never know). + if ( ! $form ) { + continue; + } - $scripts[] = array( - 'async' => true, - 'data-uid' => $form['uid'], - 'src' => $form['embed_js'], - ); + // Add the form to the scripts array so it is included in the preview. + add_filter( + 'convertkit_output_scripts_footer', + function ( $scripts ) use ( $form ) { - return $scripts; + $scripts[] = array( + 'async' => true, + 'data-uid' => $form['uid'], + 'src' => $form['embed_js'], + ); - } - ); + return $scripts; + + } + ); + } } diff --git a/includes/class-convertkit-resource-forms.php b/includes/class-convertkit-resource-forms.php index 3fbfcdbc..84466858 100644 --- a/includes/class-convertkit-resource-forms.php +++ b/includes/class-convertkit-resource-forms.php @@ -157,7 +157,7 @@ public function get_select_field_all( $name, $id, $css_classes, $selected_option * @param string $name Name. * @param string $id ID. * @param bool|array $css_classes attributes. * @param bool|string|array $description Description. @@ -305,7 +305,7 @@ private function get_multi_select_field( $forms, $name, $id, $css_classes, $sele $html .= sprintf( '', esc_attr( $value ), - ( in_array( $value, $selected_options, false ) ? ' selected' : '' ), + ( in_array( $value, $selected_options, true ) ? ' selected' : '' ), esc_attr( $label ) ); } @@ -318,7 +318,7 @@ private function get_multi_select_field( $forms, $name, $id, $css_classes, $sele $html .= sprintf( '', esc_attr( $form['id'] ), - ( in_array( $form['id'], $selected_options, false ) ? ' selected' : '' ), + ( in_array( $form['id'], $selected_options, true ) ? ' selected' : '' ), esc_attr( $form['name'] ), ( ! empty( $form['format'] ) ? esc_attr( $form['format'] ) : 'inline' ) ); diff --git a/includes/class-convertkit-settings.php b/includes/class-convertkit-settings.php index f008cac5..e3017bbd 100644 --- a/includes/class-convertkit-settings.php +++ b/includes/class-convertkit-settings.php @@ -364,10 +364,11 @@ public function get_non_inline_form() { // 2.6.8 and earlier stored a single Form ID in a string. if ( is_string( $this->settings['non_inline_form'] ) ) { - return array( $this->settings['non_inline_form'] ); + return array( (int) $this->settings['non_inline_form'] ); } - return $this->settings['non_inline_form']; + // Cast values to integers and return. + return array_map( 'intval', $this->settings['non_inline_form'] ); } @@ -383,13 +384,13 @@ public function has_non_inline_form() { // 2.6.8 and earlier stored a single Form ID in a string. if ( is_string( $this->settings['non_inline_form'] ) ) { if ( ! empty( $this->settings['non_inline_form'] ) ) { - return array( $this->settings['non_inline_form'] ); + return true; } return false; } - return ( count( $this->settings['non_inline_form'] ) ); + return ( count( $this->settings['non_inline_form'] ) > 0 ? true : false ); } diff --git a/tests/_support/Helper/Acceptance/Select2.php b/tests/_support/Helper/Acceptance/Select2.php index 8560a5fe..f349d0d8 100644 --- a/tests/_support/Helper/Acceptance/Select2.php +++ b/tests/_support/Helper/Acceptance/Select2.php @@ -29,4 +29,23 @@ public function fillSelect2Field($I, $container, $value, $ariaAttributeName = 'a $I->waitForElementVisible('ul#select2-' . $fieldName . '-results li.select2-results__option--highlighted'); $I->pressKey('.select2-search__field[' . $ariaAttributeName . '="select2-' . $fieldName . '-results"]', \Facebook\WebDriver\WebDriverKeys::ENTER); } + + /** + * Helper method to enter text into a jQuery Select2 multiple selection field, selecting the option that appears. + * + * @since 2.6.9 + * + * @param AcceptanceTester $I Tester. + * @param string $container Field CSS Class / ID. + * @param string $value Field Value. + * @param string $ariaAttributeName Aria Attribute Name (aria-controls|aria-owns). + */ + public function fillSelect2MultipleField($I, $container, $value, $ariaAttributeName = 'aria-describedby') + { + $fieldID = $I->grabAttributeFrom($container, 'id'); + $fieldName = str_replace('-container', '', str_replace('select2-', '', $fieldID)); + $I->fillField('.select2-search__field[' . $ariaAttributeName . '="select2-' . $fieldName . '-container"]', $value); + $I->waitForElementVisible('ul#select2-' . $fieldName . '-results li.select2-results__option--highlighted'); + $I->pressKey('.select2-search__field[' . $ariaAttributeName . '="select2-' . $fieldName . '-container"]', \Facebook\WebDriver\WebDriverKeys::ENTER); + } } diff --git a/tests/acceptance/general/PluginSettingsGeneralCest.php b/tests/acceptance/general/PluginSettingsGeneralCest.php index 38da9c8b..c1a9e456 100644 --- a/tests/acceptance/general/PluginSettingsGeneralCest.php +++ b/tests/acceptance/general/PluginSettingsGeneralCest.php @@ -281,8 +281,38 @@ public function testChangeDefaultFormSettingAndPreviewFormLinks(AcceptanceTester // Close newly opened tab. $I->closeTab(); - // Select a non-inline form for the Default non-inline form setting. - $I->fillSelect2Field($I, '#select2-_wp_convertkit_settings_non_inline_form-container', $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_NAME']); + // Click the Save Changes button. + $I->click('Save Changes'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Check the value of the fields match the inputs provided. + $I->seeInField('_wp_convertkit_settings[page_form]', $_ENV['CONVERTKIT_API_FORM_NAME']); + $I->seeInField('_wp_convertkit_settings[page_form_position]', 'Before Page content'); + $I->seeInField('_wp_convertkit_settings[post_form]', $_ENV['CONVERTKIT_API_FORM_NAME']); + $I->seeInField('_wp_convertkit_settings[post_form_position]', 'After Post content'); + } + + /** + * Test that no PHP errors or notices are displayed on the Plugin's Setting screen, + * when the Default Forms (Site Wide) setting is changed, and that the preview links + * work when one or more Default Forms are chosen. + * + * @since 2.6.9 + * + * @param AcceptanceTester $I Tester. + */ + public function testChangeDefaultSiteWideFormsSettingAndPreviewFormLinks(AcceptanceTester $I) + { + // Setup Plugin, without defining default Forms. + $I->setupConvertKitPluginNoDefaultForms($I); + + // Go to the Plugin's Settings Screen. + $I->loadConvertKitSettingsGeneralScreen($I); + + // Select the Sticky Bar Form for the Site Wide option. + $I->fillSelect2MultipleField($I, '#select2-_wp_convertkit_settings_non_inline_form-container', $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_NAME']); // Open preview. $I->click('a#convertkit-preview-non-inline-form'); @@ -291,13 +321,36 @@ public function testChangeDefaultFormSettingAndPreviewFormLinks(AcceptanceTester // Switch to newly opened tab. $I->switchToNextTab(); - // Confirm that one ConvertKit Form is output in the DOM. + // Confirm that the preview is the Home Page. + $I->seeElementInDOM('body.home'); + + // Confirm that one Kit Form is output in the DOM. // This confirms that there is only one script on the page for this form, which renders the form. $I->seeNumberOfElementsInDOM('form[data-sv-form="' . $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'] . '"]', 1); // Close newly opened tab. $I->closeTab(); + // Select a second Modal Form for the Site Wide option. + $I->fillSelect2MultipleField($I, '#select2-_wp_convertkit_settings_non_inline_form-container', $_ENV['CONVERTKIT_API_FORM_FORMAT_MODAL_NAME']); + + // Open preview. + $I->click('a#convertkit-preview-non-inline-form'); + $I->wait(2); // Required, otherwise switchToNextTab fails. + + // Switch to newly opened tab. + $I->switchToNextTab(); + + // Confirm that the preview is the Home Page. + $I->seeElementInDOM('body.home'); + + // Confirm that two Kit Forms are output in the DOM. + $I->seeNumberOfElementsInDOM('form[data-sv-form="' . $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'] . '"]', 1); + $I->seeNumberOfElementsInDOM('form[data-sv-form="' . $_ENV['CONVERTKIT_API_FORM_FORMAT_MODAL_ID'] . '"]', 1); + + // Close newly opened tab. + $I->closeTab(); + // Click the Save Changes button. $I->click('Save Changes'); @@ -305,11 +358,15 @@ public function testChangeDefaultFormSettingAndPreviewFormLinks(AcceptanceTester $I->checkNoWarningsAndNoticesOnScreen($I); // Check the value of the fields match the inputs provided. - $I->seeInField('_wp_convertkit_settings[page_form]', $_ENV['CONVERTKIT_API_FORM_NAME']); - $I->seeInField('_wp_convertkit_settings[page_form_position]', 'Before Page content'); - $I->seeInField('_wp_convertkit_settings[post_form]', $_ENV['CONVERTKIT_API_FORM_NAME']); - $I->seeInField('_wp_convertkit_settings[post_form_position]', 'After Post content'); - $I->seeInField('_wp_convertkit_settings[non_inline_form]', $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_NAME']); + $I->seeInFormFields( + 'form', + [ + '_wp_convertkit_settings[non_inline_form][]' => [ + $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_NAME'], + $_ENV['CONVERTKIT_API_FORM_FORMAT_MODAL_NAME'], + ], + ] + ); } /**