Skip to content

Commit

Permalink
Merge pull request #566 from ConvertKit/site-wide-non-inline-form-set…
Browse files Browse the repository at this point in the history
…ting

Add Global Default Non-Inline Form Setting
  • Loading branch information
n7studios authored Oct 23, 2023
2 parents 970266c + e9e44ab commit 912d1d2
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
'acceptance/integrations/woocommerce',
'acceptance/products',
'acceptance/restrict-content',
'acceptance/tags',
'acceptance/tags',
]

# Steps to install, configure and run tests
Expand Down
66 changes: 66 additions & 0 deletions admin/section/class-convertkit-settings-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ public function register_fields() {
);
}

add_settings_field(
'non_inline_form',
__( 'Default Non-Inline Form (Global)', 'convertkit' ),
array( $this, 'non_inline_form_callback' ),
$this->settings_key,
$this->name,
array(
'label_for' => 'non_inline_form',
)
);

add_settings_field(
'debug',
__( 'Debug', 'convertkit' ),
Expand Down Expand Up @@ -473,6 +484,61 @@ public function custom_post_types_callback( $args ) {

}


/**
* Renders the input for the Non-inline Form setting.
*
* @since 2.2.3
*
* @param array $args Field arguments.
*/
public function non_inline_form_callback( $args ) {

// Bail if no non-inline Forms exist.
if ( ! $this->forms->non_inline_exist() ) {
esc_html_e( 'No non-inline Forms exist in ConvertKit.', 'convertkit' );
echo '<br /><a href="' . esc_url( convertkit_get_new_form_url() ) . '" target="_blank">' . esc_html__( 'Click here to create your first modal, slide in or sticky bar form', 'convertkit' ) . '</a>';
return;
}

// Build array of select options.
$options = array(
'' => esc_html__( 'None', 'convertkit' ),
);
foreach ( $this->forms->get_non_inline() as $form ) {
$options[ esc_attr( $form['id'] ) ] = esc_html( $form['name'] );
}

// Build description with preview link.
$preview_url = WP_ConvertKit()->get_class( 'preview_output' )->get_preview_form_home_url();
$description = sprintf(
'%s %s %s',
esc_html__( 'Select a modal, slide in or sticky bar form to automatically display site wide.', 'convertkit' ),
'<a href="' . esc_url( $preview_url ) . '" id="convertkit-preview-non-inline-form" target="_blank">' . esc_html__( 'Click here', 'convertkit' ) . '</a>',
esc_html__( 'to preview how this will display.', 'convertkit' )
);

// Build field.
$select_field = $this->get_select_field(
'non_inline_form',
$this->settings->get_non_inline_form(),
$options,
$description,
array(
'convertkit-select2',
'convertkit-preview-output-link',
),
array(
'data-target' => '#convertkit-preview-non-inline-form',
'data-link' => esc_url( $preview_url ) . '&convertkit_form_id=',
)
);

// Output field.
echo '<div class="convertkit-select2-container">' . $select_field . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput

}

/**
* Renders the input for the Debug setting.
*
Expand Down
46 changes: 46 additions & 0 deletions includes/class-convertkit-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function __construct() {

add_action( 'init', array( $this, 'get_subscriber_id_from_request' ), 1 );
add_action( 'template_redirect', array( $this, 'output_form' ) );
add_action( 'template_redirect', array( $this, 'output_global_non_inline_form' ) );
add_action( 'template_redirect', array( $this, 'page_takeover' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_filter( 'the_content', array( $this, 'append_form_to_content' ) );
Expand Down Expand Up @@ -410,6 +411,51 @@ public function get_subscriber_id_from_request() {

}

/**
* Outputs a non-inline form if defined in the Plugin's settings >
* Default Non-Inline Form (Global) setting.
*
* @since 2.3.3
*/
public function output_global_non_inline_form() {

// Get Settings, if they have not yet been loaded.
if ( ! $this->settings ) {
$this->settings = new ConvertKit_Settings();
}

// Bail if no non-inline form setting is specified.
if ( ! $this->settings->has_non_inline_form() ) {
return;
}

// Get form.
$convertkit_forms = new ConvertKit_Resource_Forms();
$form = $convertkit_forms->get_by_id( (int) $this->settings->get_non_inline_form() );

// Bail if the Form doesn't exist (this shouldn't happen, but you never know).
if ( ! $form ) {
return;
}

// Add the form to the scripts array so it is included in the output.
add_filter(
'convertkit_output_scripts_footer',
function ( $scripts ) use ( $form ) {

$scripts[] = array(
'async' => true,
'data-uid' => $form['uid'],
'src' => $form['embed_js'],
);

return $scripts;

}
);

}

/**
* Outputs any JS <script> tags registered with the convertkit_output_scripts_footer
* filter
Expand Down
84 changes: 83 additions & 1 deletion includes/class-convertkit-preview-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ class ConvertKit_Preview_Output {
*/
public function __construct() {

// Preview inline forms.
add_filter( 'convertkit_output_append_form_to_content_form_id', array( $this, 'preview_form' ), 99999 );

// Preview non-inline forms.
add_filter( 'template_redirect', array( $this, 'preview_non_inline_form' ) );

// Append edit link to inline forms.
add_filter( 'convertkit_block_form_render', array( $this, 'maybe_append_edit_form_link_to_form_block' ), 10, 3 );
add_filter( 'convertkit_frontend_append_form', array( $this, 'maybe_append_edit_form_link_to_form' ), 10, 4 );

}


/**
* Changes the form to display for the given Post ID if the request is
* from a logged in user who has clicked a preview link in the Plugin Setup Wizard.
Expand Down Expand Up @@ -66,6 +71,64 @@ 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
* and is viewing the home page.
*
* @since 2.3.3
*/
public function preview_non_inline_form() {

// Bail if not on the home page.
if ( ! is_home() ) {
return;
}

// Bail if the user isn't logged in.
if ( ! is_user_logged_in() ) {
return;
}

// Bail if no nonce field exists.
if ( ! isset( $_REQUEST['convertkit-preview-form-nonce'] ) ) {
return;
}

// Bail if the nonce verification fails.
if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['convertkit-preview-form-nonce'] ) ), 'convertkit-preview-form' ) ) {
return;
}

// Determine the form to preview.
$preview_form_id = (int) ( isset( $_REQUEST['convertkit_form_id'] ) ? sanitize_text_field( $_REQUEST['convertkit_form_id'] ) : 0 );

// Get form.
$convertkit_forms = new ConvertKit_Resource_Forms();
$form = $convertkit_forms->get_by_id( $preview_form_id );

// Bail if the Form doesn't exist (this shouldn't happen, but you never know).
if ( ! $form ) {
return;
}

// Add the form to the scripts array so it is included in the preview.
add_filter(
'convertkit_output_scripts_footer',
function ( $scripts ) use ( $form ) {

$scripts[] = array(
'async' => true,
'data-uid' => $form['uid'],
'src' => $form['embed_js'],
);

return $scripts;

}
);

}

/**
* Returns the URL for the most recent published Post based on the supplied Post Type,
* with a preview form nonce included in the URL.
Expand Down Expand Up @@ -95,6 +158,25 @@ public function get_preview_form_url( $post_type = 'post' ) {

}

/**
* Returns the URL for the home page, with a preview form nonce included in the URL.
*
* @since 2.3.3
*
* @return string
*/
public function get_preview_form_home_url() {

// Return preview URL.
return add_query_arg(
array(
'convertkit-preview-form-nonce' => $this->get_preview_form_nonce(),
),
get_home_url()
);

}

/**
* Appends an "Edit form in ConvertKit" link to the given ConvertKit Form block,
* if the request is to preview a Page and the logged in WordPress user can
Expand Down
42 changes: 37 additions & 5 deletions includes/class-convertkit-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,37 @@ public function has_default_form( $post_type ) {

}

/**
* Returns the Global non-inline Form Plugin setting.
*
* @since 2.3.3
*
* @return string|int Non-inline Form (blank string|form id)
*/
public function get_non_inline_form() {

// Return blank string if no inline form is specified.
if ( ! $this->has_non_inline_form() ) {
return '';
}

return $this->settings['non_inline_form'];

}

/**
* Returns whether the Global non-inline Form has been set in the Plugin settings.
*
* @since 2.3.3
*
* @return bool Global non-inline Form setting specified in Plugin Settings.
*/
public function has_non_inline_form() {

return ( ! empty( $this->settings['non_inline_form'] ) ? true : false );

}

/**
* Returns whether debugging is enabled in the Plugin settings.
*
Expand Down Expand Up @@ -252,11 +283,12 @@ public function css_disabled() {
public function get_defaults() {

$defaults = array(
'api_key' => '', // string.
'api_secret' => '', // string.
'debug' => '', // blank|on.
'no_scripts' => '', // blank|on.
'no_css' => '', // blank|on.
'api_key' => '', // string.
'api_secret' => '', // string.
'non_inline_form' => '', // string.
'debug' => '', // blank|on.
'no_scripts' => '', // blank|on.
'no_css' => '', // blank|on.
);

// Add Post Type Default Forms.
Expand Down
4 changes: 4 additions & 0 deletions tests/acceptance/general/ActivateDeactivatePluginCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@ public function testPluginActivationAndDeactivationWithOtherPlugins(AcceptanceTe

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Deactivate Plugins.
$I->deactivateConvertKitPlugin($I);
$I->deactivateThirdPartyPlugin($I, 'convertkit-for-woocommerce');
}
}
Loading

0 comments on commit 912d1d2

Please sign in to comment.