diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 78dfe52d2..d719d0506 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -64,7 +64,7 @@ jobs: 'acceptance/integrations/woocommerce', 'acceptance/products', 'acceptance/restrict-content', - 'acceptance/tags', + 'acceptance/tags', ] # Steps to install, configure and run tests diff --git a/admin/section/class-convertkit-settings-general.php b/admin/section/class-convertkit-settings-general.php index dd7bf7c10..c23567fb0 100644 --- a/admin/section/class-convertkit-settings-general.php +++ b/admin/section/class-convertkit-settings-general.php @@ -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' ), @@ -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 '
' . esc_html__( 'Click here to create your first modal, slide in or sticky bar form', 'convertkit' ) . ''; + 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' ), + '' . esc_html__( 'Click here', 'convertkit' ) . '', + 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 '
' . $select_field . '
'; // phpcs:ignore WordPress.Security.EscapeOutput + + } + /** * Renders the input for the Debug setting. * diff --git a/includes/class-convertkit-output.php b/includes/class-convertkit-output.php index 2549c42da..c3ea5a3b8 100644 --- a/includes/class-convertkit-output.php +++ b/includes/class-convertkit-output.php @@ -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' ) ); @@ -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