Skip to content
Merged
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
16 changes: 16 additions & 0 deletions admin/class-convertkit-admin-refresh-resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,23 @@ public function refresh_resources( $request ) {
case 'forms':
$forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' );
$results = $forms->refresh();

// Only return non-legacy forms.
if ( ! is_wp_error( $results ) ) {
$non_legacy = $forms->get_non_legacy();
$results = is_array( $non_legacy ) ? $non_legacy : array();
}
break;

case 'landing_pages':
$landing_pages = new ConvertKit_Resource_Landing_Pages( 'user_refresh_resource' );
$results = $landing_pages->refresh();

// Only return non-legacy landing pages.
if ( ! is_wp_error( $results ) ) {
$non_legacy = $landing_pages->get_non_legacy();
$results = is_array( $non_legacy ) ? $non_legacy : array();
}
break;

case 'tags':
Expand Down Expand Up @@ -114,6 +126,10 @@ public function refresh_resources( $request ) {
return rest_ensure_response( $results_forms );
}

// Only return non-legacy forms.
$non_legacy_forms = $forms->get_non_legacy();
$results_forms = is_array( $non_legacy_forms ) ? $non_legacy_forms : array();

// Fetch Tags.
$tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' );
$results_tags = $tags->refresh();
Expand Down
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 @@ -517,17 +517,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 @@ -575,10 +585,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
78 changes: 78 additions & 0 deletions includes/class-convertkit-resource-landing-pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,84 @@ public function __construct( $context = 'landing_pages' ) {

}

/**
* Returns all non-legacy landing pages (v4 pages with an `embed_url`
* property). Legacy landing pages have a `url` property instead and are
* excluded here.
*
* @since 3.3.7
*
* @return bool|array
*/
public function get_non_legacy() {

$resources = $this->get();
if ( ! $resources ) {
return false;
}

$non_legacy = array();
foreach ( $resources as $id => $landing_page ) {
if ( isset( $landing_page['url'] ) ) {
continue;
}
$non_legacy[ $id ] = $landing_page;
}

return $non_legacy;

}

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

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

}

/**
* Determines whether the given identifier refers to a legacy landing page.
*
* Handles both storage shapes: a URL string (as saved by Plugin versions
* < 1.9.6 which stored the landing page URL directly on the post), and a
* numeric ID that resolves to a resource entry containing a `url` key.
*
* @since 3.3.7
*
* @param int|string $id_or_url Landing Page ID or URL.
* @return bool
*/
public function is_legacy( $id_or_url ) {

// Bail if no value.
if ( ! $id_or_url ) {
return false;
}

// If the value is a URL string, it's a legacy landing page as stored
// by Plugin versions < 1.9.6.
if ( is_string( $id_or_url ) && strstr( $id_or_url, 'http' ) ) {
return true;
}

// Otherwise resolve the ID against the cached resources.
$landing_page = $this->get_by_id( (int) $id_or_url );

if ( ! $landing_page ) {
return false;
}

// Legacy landing pages carry a `url` key; v4 pages carry `embed_url`.
return isset( $landing_page['url'] );

}

/**
* Returns the HTML/JS markup for the given Landing Page ID
*
Expand Down
Loading