Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions includes/blocks/class-convertkit-block-form-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,17 +511,27 @@ public function get_supports() {
*/
public function get_fields() {

// Get Kit Forms.
$forms = new ConvertKit_Resource_Forms( 'block_form_builder' );
$forms_options = array();
// Get Kit Forms. Non-legacy forms populate the sidebar dropdown;
// legacy forms are exposed separately as a fallback so the sidebar can
// keep displaying a previously-saved legacy form as the current
// selection without offering other legacy forms as new choices.
$forms = new ConvertKit_Resource_Forms( 'block_form_builder' );
$forms_options = array();
$forms_legacy_options = array();
if ( $forms->exist() ) {
foreach ( $forms->get() as $form ) {
// Legacy forms don't include a `format` key, so define them as inline.
$forms_options[ $form['id'] ] = sprintf(
$label = sprintf(
'%s [%s]',
sanitize_text_field( $form['name'] ),
// Legacy forms don't include a `format` key, so define them as inline.
( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' )
);

if ( ! empty( $form['format'] ) ) {
$forms_options[ $form['id'] ] = $label;
} else {
$forms_legacy_options[ $form['id'] ] = $label;
}
}
}

Expand Down Expand Up @@ -569,10 +579,11 @@ public function get_fields() {
),
),
'form_id' => array(
'label' => __( 'Form', 'convertkit' ),
'type' => 'select',
'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ),
'values' => $forms_options,
'label' => __( 'Form', 'convertkit' ),
'type' => 'select',
'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ),
'values' => $forms_options,
'legacy_values' => $forms_legacy_options,
),
'tag_id' => array(
'label' => __( 'Tag', 'convertkit' ),
Expand Down
30 changes: 21 additions & 9 deletions includes/blocks/class-convertkit-block-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,30 +262,42 @@ public function get_supports() {
*/
public function get_fields() {

// Get ConvertKit Forms.
// Get ConvertKit Forms. Non-legacy forms populate the sidebar dropdown;
// legacy forms are exposed separately as a fallback so the sidebar can
// keep displaying a previously-saved legacy form as the current
// selection without offering other legacy forms as new choices.
$forms = array();
$legacy_forms = array();
$convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
if ( $convertkit_forms->exist() ) {
foreach ( $convertkit_forms->get() as $form ) {
// Legacy forms don't include a `format` key, so define them as inline.
$forms[ absint( $form['id'] ) ] = sprintf(
$label = sprintf(
'%s [%s]',
sanitize_text_field( $form['name'] ),
// Legacy forms don't include a `format` key, so define them as inline.
( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' )
);

if ( ! empty( $form['format'] ) ) {
$forms[ absint( $form['id'] ) ] = $label;
} else {
$legacy_forms[ absint( $form['id'] ) ] = $label;
}
}
}

return array(
'form' => array(
'label' => __( 'Form', 'convertkit' ),
'type' => 'resource',
'resource' => 'forms',
'values' => $forms,
'data' => array(
'label' => __( 'Form', 'convertkit' ),
'type' => 'resource',
'resource' => 'forms',
'values' => $forms,
'legacy_values' => $legacy_forms,
'data' => array(
// Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format
// (modal, slide in, sticky bar) and output a message in the block editor for the preview to explain
// why some formats cannot be previewed.
// why some formats cannot be previewed. Includes legacy forms so the preview code can still find
// them when a saved block references a legacy form.
'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ),
),
),
Expand Down
71 changes: 69 additions & 2 deletions includes/class-convertkit-resource-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,42 @@ public function non_inline_exist() {

}

/**
* Returns all non-legacy forms (any v4 format: inline, modal, slide in
* or sticky bar) based on the sort order. Legacy forms lack a `format`
* key and are therefore excluded.
*
* @since 3.3.7
*
* @return bool|array
*/
public function get_non_legacy() {

// If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated
// addon), or a WordPress site updates this Plugin before other ConvertKit Plugins,
// get_by() won't be available and will cause an E_ERROR, crashing the site.
// @see https://wordpress.org/support/topic/error-1795/.
if ( ! method_exists( $this, 'get_by' ) ) { // @phpstan-ignore-line Older WordPress Libraries won't have this function.
return false;
}

return $this->get_by( 'format', array( 'inline', 'modal', 'slide in', 'sticky bar' ) );

}

/**
* Returns whether any non-legacy forms exist in the options table.
*
* @since 3.3.7
*
* @return bool
*/
public function non_legacy_exist() {

return (bool) $this->get_non_legacy();

}

/**
* Determines if the given Form ID is a legacy Form or Landing Page.
*
Expand Down Expand Up @@ -196,7 +232,7 @@ public function is_legacy( $id ) {
public function get_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) {

return $this->get_select_field(
$this->get(),
$this->get_forms_for_select_field( $selected_option ),
$name,
$id,
$css_classes,
Expand Down Expand Up @@ -224,7 +260,7 @@ public function get_select_field_all( $name, $id, $css_classes, $selected_option
public function output_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) {

$this->output_select_field(
$this->get(),
$this->get_forms_for_select_field( $selected_option ),
$name,
$id,
$css_classes,
Expand All @@ -236,6 +272,37 @@ public function output_select_field_all( $name, $id, $css_classes, $selected_opt

}

/**
* Returns the array of forms to display in an "all forms" dropdown: every
* non-legacy form, plus the currently-selected legacy form (if any) so
* existing saved legacy assignments continue to render as selected in the
* UI without exposing other legacy forms as new selection options.
*
* @since 3.3.7
*
* @param string|int $selected_option Currently-selected form ID.
* @return array
*/
private function get_forms_for_select_field( $selected_option ) {

$forms = $this->get_non_legacy();
if ( ! is_array( $forms ) ) {
$forms = array();
}

// If the currently-selected form is a legacy form, append it so the
// dropdown shows the saved value as selected.
if ( $selected_option && $this->is_legacy( $selected_option ) ) {
$legacy_form = $this->get_by_id( (int) $selected_option );
if ( $legacy_form ) {
$forms[] = $legacy_form;
}
}

return $forms;

}

/**
* Returns a <select> field populated with all non-inline forms, based on the given parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,29 @@ public function get_fields() {
$convertkit_tags = new ConvertKit_Resource_Tags( 'post_settings' );
$convertkit_products = new ConvertKit_Resource_Products( 'post_settings' );

// Get Forms.
$forms = array(
// Get Forms. Non-legacy forms populate the dropdown; legacy forms are
// exposed separately as a fallback so a previously-saved legacy form
// remains visible as the current selection without offering other
// legacy forms as new choices.
$forms = array(
'-1' => esc_html__( 'Default', 'convertkit' ),
'0' => esc_html__( 'None', 'convertkit' ),
);
$legacy_forms = array();
if ( $convertkit_forms->exist() ) {
foreach ( $convertkit_forms->get() as $form ) {
// Legacy forms don't include a `format` key, so define them as inline.
$forms[ absint( $form['id'] ) ] = sprintf(
$label = sprintf(
'%s [%s]',
sanitize_text_field( $form['name'] ),
// Legacy forms don't include a `format` key, so define them as inline.
( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' )
);

if ( ! empty( $form['format'] ) ) {
$forms[ absint( $form['id'] ) ] = $label;
} else {
$legacy_forms[ absint( $form['id'] ) ] = $label;
}
}
}

Expand All @@ -156,21 +166,29 @@ public function get_fields() {
}

// Get Products.
$restrict_content = array(
$restrict_content = array(
'0' => esc_html__( 'Do not restrict content to member-only', 'convertkit' ),
);
$restrict_content_legacy_forms = array();
if ( $convertkit_forms->exist() ) {
$restrict_content['forms'] = array(
'label' => esc_html__( 'Forms', 'convertkit' ),
'values' => array(),
);
foreach ( $convertkit_forms->get() as $form ) {
// Legacy forms don't include a `format` key, so define them as inline.
$restrict_content['forms']['values'][ 'form_' . absint( $form['id'] ) ] = sprintf(
$key = 'form_' . absint( $form['id'] );
$label = sprintf(
'%s [%s]',
sanitize_text_field( $form['name'] ),
// Legacy forms don't include a `format` key, so define them as inline.
( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' )
);

if ( ! empty( $form['format'] ) ) {
$restrict_content['forms']['values'][ $key ] = $label;
} else {
$restrict_content_legacy_forms[ $key ] = $label;
}
}
}
if ( $convertkit_tags->exist() ) {
Expand Down Expand Up @@ -218,6 +236,7 @@ public function get_fields() {
),
),
'values' => $forms,
'legacy_values' => $legacy_forms,
'resource_type' => 'forms',
),
'landing_page' => array(
Expand Down Expand Up @@ -267,6 +286,7 @@ public function get_fields() {
),
),
'values' => $restrict_content,
'legacy_values' => $restrict_content_legacy_forms,
'resource_type' => 'restrict_content',
),
);
Expand Down
Loading