Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ras): allow skipping prerequisites; avoid dead end in onboarding #3738

Merged
merged 6 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion includes/cli/class-ras.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static function cli_setup_ras() {
WP_CLI::error( __( 'Newspack Campaigns plugin not found.', 'newspack-plugin' ) );
}


if ( ! class_exists( '\Newspack_Newsletters_Subscription' ) ) {
WP_CLI::error( __( 'Newspack Newsletters plugin not found.', 'newspack-plugin' ) );
}
Expand Down
115 changes: 103 additions & 12 deletions includes/reader-activation/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,15 @@ public static function update_setting( $key, $value ) {
* Activate RAS features and publish RAS prompts + segments.
*/
public static function activate() {
if ( ! method_exists( '\Newspack_Popups_Presets', 'activate_ras_presets' ) ) {
if ( ! method_exists( 'Newspack_Popups_Presets', 'activate_ras_presets' ) ) {
return new \WP_Error( 'newspack_reader_activation_missing_dependencies', __( 'Newspack Campaigns plugin is required to activate Reader Activation features.', 'newspack-plugin' ) );
}

return \Newspack_Popups_Presets::activate_ras_presets();
$activated = \Newspack_Popups_Presets::activate_ras_presets();
if ( $activated ) {
self::skip( 'ras_campaign', false );
}
return $activated;
}

/**
Expand Down Expand Up @@ -674,12 +678,23 @@ public static function get_reader_revenue_required_plugins() {
/**
* Are the Legal Pages settings configured?
* Allows for blank values.
*
* @param bool $skip Whether to skip the check.
*
* @return bool
*/
public static function is_terms_configured() {
public static function is_terms_configured( $skip = false ) {
$terms_text = \get_option( self::OPTIONS_PREFIX . 'terms_text', false );
$terms_url = \get_option( self::OPTIONS_PREFIX . 'terms_url', false );
$is_valid = is_string( $terms_text ) && is_string( $terms_url );
if ( $skip ) {
return $is_valid || self::is_skipped( 'terms_conditions' );
}
if ( $is_valid ) {
self::skip( 'terms_conditions', false );
}

return is_string( $terms_text ) && is_string( $terms_url );
return $is_valid;
}

/**
Expand All @@ -693,26 +708,97 @@ public static function is_transactional_email_configured() {
return ! empty( $sender_name ) && ! empty( $sender_email ) && ! empty( $contact_email_address );
}

/**
* Is reCAPTCHA enabled?
*
* @param bool $skip Whether to skip the check.
*
* @return bool
*/
public static function is_recaptcha_enabled( $skip = false ) {
$is_valid = method_exists( '\Newspack\Recaptcha', 'can_use_captcha' ) && \Newspack\Recaptcha::can_use_captcha();
if ( $skip ) {
return $is_valid || self::is_skipped( 'recaptcha' );
}
if ( $is_valid ) {
self::skip( 'recaptcha', false );
}
return $is_valid;
}

/**
* Is the RAS campaign configured?
*
* TODO: Make this dynamic once the third UI screen to generate the prompts is built.
* @param bool $skip Whether to skip the check.
*
* @return bool
*/
public static function is_ras_campaign_configured() {
return self::is_enabled() || get_option( Audience_Wizard::SKIP_CAMPAIGN_SETUP_OPTION, '' ) === '1';
public static function is_ras_campaign_configured( $skip = false ) {
$is_valid = class_exists( 'Newspack_Popups_Presets' ) && get_option( \Newspack_Popups_Presets::NEWSPACK_POPUPS_RAS_LAST_UPDATED, false );
if ( $skip ) {
return $is_valid || self::is_skipped( 'ras_campaign' );
}
return $is_valid;
}

/**
* Are all prerequisites for Reader Activation complete?
*
* @return bool
*/
public static function is_ras_ready_to_configure() {
return self::is_terms_configured() && self::is_esp_configured() && self::is_transactional_email_configured() && method_exists( '\Newspack\Recaptcha', 'can_use_captcha' ) && \Newspack\Recaptcha::can_use_captcha() && self::is_woocommerce_active();
$is_ready = self::is_terms_configured( true ) && self::is_esp_configured() && self::is_transactional_email_configured() && self::is_recaptcha_enabled( true ) && self::is_woocommerce_active();

// If all requirements are met or skipped, and RAS isn't yet enabled, enable it.
if ( $is_ready && self::is_ras_campaign_configured( true ) && ! self::is_enabled() ) {
self::update_setting( 'enabled', true );
}
return $is_ready;
}

/**
* Has the given prerequisite been skipped?
*
* @param string $prerequisite The prerequisite to check.
*
* @return bool
*/
public static function is_skipped( $prerequisite ) {
// Legacy option name compabitility.
$legacy_is_skipped = false;
if ( 'ras_campaign' === $prerequisite ) {
$legacy_is_skipped = get_option( Audience_Wizard::SKIP_CAMPAIGN_SETUP_OPTION, false ) === '1';
}

return boolval( get_option( self::OPTIONS_PREFIX . $prerequisite . '_skipped', $legacy_is_skipped ) );
}

/**
* Skip or unskip the given prerequisite.
*
* @param string $prerequisite The prerequisite to skip.
* @param bool $skip If true, skip the prerequisite. If false, unskip it.
*
* @return bool True if updated, false if not.
*/
public static function skip( $prerequisite, $skip = true ) {
$updated = $skip ? update_option( self::OPTIONS_PREFIX . $prerequisite . '_skipped', '1' ) : delete_option( self::OPTIONS_PREFIX . $prerequisite . '_skipped' );

// Legacy option name compabitility.
if ( 'ras_campaign' === $prerequisite && ! $skip && ! $updated ) {
$updated = delete_option( Audience_Wizard::SKIP_CAMPAIGN_SETUP_OPTION );
}

// If all requirements are met or skipped, and RAS isn't yet enabled, enable it.
if ( $skip && self::is_ras_ready_to_configure() && self::is_ras_campaign_configured( true ) && ! self::is_enabled() ) {
self::update_setting( 'enabled', true );
}

return $updated;
}

/**
* Get the status of the prerequisites for enabling reader activation.
* TODO: Finalize the list of prerequisites and all copy.
* TODO: Establish schema for input fields to be shown in expandable cards.
*
* @return array Array of prerequisites to complete.
*/
Expand All @@ -734,6 +820,8 @@ public static function get_prerequisites_status() {
'description' => __( 'URL to the page containing the privacy policy or terms of service.', 'newspack-plugin' ),
],
],
'skippable' => true,
'is_skipped' => self::is_skipped( 'terms_conditions' ),
],
'esp' => [
'active' => self::is_esp_configured(),
Expand Down Expand Up @@ -768,13 +856,15 @@ public static function get_prerequisites_status() {
],
],
'recaptcha' => [
'active' => method_exists( '\Newspack\Recaptcha', 'can_use_captcha' ) && \Newspack\Recaptcha::can_use_captcha(),
'active' => self::is_recaptcha_enabled(),
'label' => __( 'reCAPTCHA', 'newspack-plugin' ),
'description' => __( 'Connecting to a Google reCAPTCHA account enables enhanced anti-spam for all Newspack sign-up blocks.', 'newspack-plugin' ),
'instructions' => __( 'Enable reCAPTCHA and enter your account credentials.', 'newspack-plugin' ),
'help_url' => 'https://help.newspack.com/engagement/audience-management-system/',
'href' => \admin_url( '/admin.php?page=newspack-settings&scrollTo=newspack-settings-recaptcha' ),
'action_text' => __( 'reCAPTCHA settings' ),
'skippable' => true,
'is_skipped' => self::is_skipped( 'recaptcha' ),
],
'reader_revenue' => [
'active' => self::is_reader_revenue_ready(),
Expand All @@ -788,7 +878,6 @@ public static function get_prerequisites_status() {
],
'ras_campaign' => [
'active' => self::is_ras_campaign_configured(),
'is_skipped' => get_option( Audience_Wizard::SKIP_CAMPAIGN_SETUP_OPTION, '' ) === '1',
'plugins' => [
'newspack-popups' => class_exists( '\Newspack_Popups_Model' ),
],
Expand All @@ -799,6 +888,8 @@ public static function get_prerequisites_status() {
'action_enabled' => self::is_ras_ready_to_configure(),
'action_text' => __( 'Audience Management campaign', 'newspack-plugin' ),
'disabled_text' => __( 'Waiting for all settings to be ready', 'newspack-plugin' ),
'skippable' => true,
'is_skipped' => self::is_skipped( 'ras_campaign' ),
],
];

Expand Down
48 changes: 36 additions & 12 deletions includes/wizards/audience/class-audience-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function enqueue_scripts_and_styles() {
$data['preview_archive'] = $newspack_popups->preview_archive();
}

$data['is_skipped_campaign_setup'] = get_option( static::SKIP_CAMPAIGN_SETUP_OPTION, '' );
$data['is_skipped_campaign_setup'] = Reader_Activation::is_skipped( 'ras_campaign' );

wp_enqueue_script( 'newspack-wizards' );

Expand Down Expand Up @@ -185,20 +185,19 @@ public function register_api_endpoints() {
);
register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/' . $this->slug . '/audience-management/skip-campaign',
'/wizard/' . $this->slug . '/audience-management/skip',
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => function( $request ) {
$skip = $request->get_param( 'skip' );
$skip_campaign_setup = update_option( static::SKIP_CAMPAIGN_SETUP_OPTION, $skip );
return rest_ensure_response(
[
'skipped' => $skip,
'updated' => $skip_campaign_setup,
]
);
},
'callback' => [ $this, 'api_skip_prerequisite' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
'args' => [
'prerequisite' => [
'sanitize_callback' => 'sanitize_text_field',
],
'skip' => [
'sanitize_callback' => 'Newspack\newspack_string_to_bool',
],
],
]
);
register_rest_route(
Expand Down Expand Up @@ -428,6 +427,7 @@ public function api_update_reader_activation_settings( $request ) {
[
'config' => Reader_Activation::get_settings(),
'prerequisites_status' => Reader_Activation::get_prerequisites_status(),
'memberships' => self::get_memberships_settings(),
'can_esp_sync' => Reader_Activation\ESP_Sync::can_esp_sync( true ),
]
);
Expand Down Expand Up @@ -492,6 +492,30 @@ public function api_activate_reader_activation( WP_REST_Request $request ) {
return rest_ensure_response( $response );
}

/**
* Activate reader activation and publish RAS prompts/segments.
*
* @param WP_REST_Request $request WP Rest Request object.
* @return WP_REST_Response
*/
public function api_skip_prerequisite( WP_REST_Request $request ) {
$preqrequisite = $request->get_param( 'prerequisite' );
$skip = $request->get_param( 'skip' );
$skip_campaign_setup = Reader_Activation::skip( $preqrequisite, $skip );
if ( ! $skip_campaign_setup ) {
return new WP_REST_Response( [ 'message' => __( 'Error skipping prerequisite', 'newspack-plugin' ) ], 400 );
}

return rest_ensure_response(
[
'config' => Reader_Activation::get_settings(),
'prerequisites_status' => Reader_Activation::get_prerequisites_status(),
'memberships' => self::get_memberships_settings(),
'can_esp_sync' => Reader_Activation\ESP_Sync::can_esp_sync( true ),
]
);
}

/**
* Get content gating settings.
*
Expand Down
Loading