Skip to content

Commit

Permalink
feat: campaigns listeners for the data events api (#2291)
Browse files Browse the repository at this point in the history
* feat: campaigns listeners for the data events api

* feat: add listeners file

* feat: add missing popup_id to listeners

* feat: add backward compatibility to popups events
  • Loading branch information
leogermani authored Feb 22, 2023
1 parent fd3011c commit ab407d4
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 1 deletion.
17 changes: 16 additions & 1 deletion assets/blocks/reader-registration/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ function render_block( $attrs, $content ) {
<p class="newspack-registration__description"><?php echo \wp_kses_post( $attrs['description'] ); ?></p>
<?php endif; ?>
<?php \wp_nonce_field( FORM_ACTION, FORM_ACTION ); ?>
<?php
/**
* Action to add custom fields before the form fields of the registration block.
*
* @param array $attrs Block attributes.
*/
do_action( 'newspack_registration_before_form_fields', $attrs );
?>
<div class="newspack-registration__form-content">
<?php
if ( ! empty( $lists ) ) {
Expand Down Expand Up @@ -404,15 +412,22 @@ function process_form() {
$metadata['current_page_url'] = home_url( add_query_arg( array(), \wp_get_referer() ) );
$metadata['registration_method'] = 'registration-block';

$popup_id = isset( $_REQUEST['newspack_popup_id'] ) ? (int) $_REQUEST['newspack_popup_id'] : false;
if ( $popup_id ) {
$metadata['popup_id'] = $popup_id;
$metadata['registration_method'] = 'registration-block-popup';
}

$user_id = Reader_Activation::register_reader( $email, '', true, $metadata );

/**
* Fires after a reader is registered through the Reader Registration Block.
*
* @param string $email Email address of the reader.
* @param int|false|\WP_Error $user_id The created user ID in case of registration, false if not created or a WP_Error object.
* @param int|false $popup_id The ID of the popup that triggered the registration, or false if not triggered by a popup.
*/
\do_action( 'newspack_reader_registration_form_processed', $email, $user_id );
\do_action( 'newspack_reader_registration_form_processed', $email, $user_id, $popup_id );

if ( \is_wp_error( $user_id ) ) {
return send_form_response( $user_id );
Expand Down
1 change: 1 addition & 0 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-webhooks.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-api.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/listeners.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-popups.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/ga4/class-ga4.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/class-mailchimp.php';
include_once NEWSPACK_ABSPATH . 'includes/class-api.php';
Expand Down
202 changes: 202 additions & 0 deletions includes/data-events/class-popups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
/**
* Newspack Data Events Popups helper.
*
* @package Newspack
*/

namespace Newspack\Data_Events;

use Newspack_Popups_Model;
use Newspack\Data_Events;
use WP_Error;

/**
* Class to register the Popups listeners.
*/
final class Popups {

/**
* Initialize the class by registering the listeners.
*
* @return void
*/
public static function init() {
Data_Events::register_listener(
'newspack_campaigns_after_campaign_render',
'campaign_interaction',
[ __CLASS__, 'campaign_rendered' ]
);

Data_Events::register_listener(
'newspack_reader_registration_form_processed',
'campaign_interaction',
[ __CLASS__, 'registration_submission' ]
);

Data_Events::register_listener(
'newspack_reader_registration_form_processed',
'campaign_interaction',
[ __CLASS__, 'registration_submission_with_status' ]
);

Data_Events::register_listener(
'newspack_newsletters_subscribe_form_processed',
'campaign_interaction',
[ __CLASS__, 'newsletter_submission' ]
);

Data_Events::register_listener(
'newspack_newsletters_subscribe_form_processed',
'campaign_interaction',
[ __CLASS__, 'newsletter_submission_with_status' ]
);
}

/**
* Extract the relevant data from a popup.
*
* @param int|array $popup The popup ID or object.
* @return array
*/
public static function get_popup_metadata( $popup ) {
if ( is_numeric( $popup ) ) {
$popup = Newspack_Popups_Model::retrieve_popup_by_id( $popup );
}
$data = [];
if ( ! $popup ) {
return $data;
}

$data['title'] = $popup['title'];

if ( isset( $popup['options'] ) ) {
$data['frequency'] = $popup['options']['frequency'] ?? '';
$data['placement'] = $popup['options']['placement'] ?? '';
}

$data['has_registration_block'] = has_block( 'newspack/reader-registration', $popup['content'] );
$data['has_donation_block'] = false; // TODO.
$data['has_newsletter_block'] = has_block( 'newspack-newsletters/subscribe', $popup['content'] );

return $data;
}

/**
* A listener for the moment when a campaign is rendered.
*
* @param array $popup The popup representation.
* @return ?array
*/
public static function campaign_rendered( $popup ) {
$popup_data = self::get_popup_metadata( $popup );
return array_merge(
$popup_data,
[
'action' => 'rendered',
]
);
}

/**
* A listener for the registration block form submission
*
* Will trigger the event with "form_submission" as action in all cases.
*
* @param string $email Email address of the reader.
* @param int|false|\WP_Error $user_id The created user ID in case of registration, false if not created or a WP_Error object.
* @param int|false $popup_id The ID of the popup that triggered the registration, or false if not triggered by a popup.
* @return ?array
*/
public static function registration_submission( $email, $user_id, $popup_id ) {
if ( ! $popup_id ) {
return;
}
$popup_data = self::get_popup_metadata( $popup_id );
return array_merge(
$popup_data,
[
'action' => 'form_submission',
]
);
}

/**
* A listener for the registration block form submission
*
* Will trigger the event with "form_submission_success" or "form_submission_failure" as action.
*
* @param string $email Email address of the reader.
* @param int|false|\WP_Error $user_id The created user ID in case of registration, false if not created or a WP_Error object.
* @param int|false $popup_id The ID of the popup that triggered the registration, or false if not triggered by a popup.
* @return ?array
*/
public static function registration_submission_with_status( $email, $user_id, $popup_id ) {
if ( ! $popup_id ) {
return;
}
$action = 'form_submission_success';
if ( ! $user_id || \is_wp_error( $user_id ) ) {
$action = 'form_submission_failure';
}
$popup_data = self::get_popup_metadata( $popup_id );
return array_merge(
$popup_data,
[
'action' => $action,
]
);
}

/**
* A listener for the registration block form submission
*
* Will trigger the event with "form_submission" as action in all cases.
*
* @param string $email Email address of the reader.
* @param array|WP_Error $result Contact data if it was added, or error otherwise.
* @param int|false $popup_id The ID of the popup that triggered the registration, or false if not triggered by a popup.
* @return ?array
*/
public static function newsletter_submission( $email, $result, $popup_id = false ) {
if ( ! $popup_id ) {
return;
}
$popup_data = self::get_popup_metadata( $popup_id );
return array_merge(
$popup_data,
[
'action' => 'form_submission',
]
);
}

/**
* A listener for the registration block form submission
*
* Will trigger the event with "form_submission_success" or "form_submission_failure" as action.
*
* @param string $email Email address of the reader.
* @param array|WP_Error $result Contact data if it was added, or error otherwise.
* @param int|false $popup_id The ID of the popup that triggered the registration, or false if not triggered by a popup.
* @return ?array
*/
public static function newsletter_submission_with_status( $email, $result, $popup_id = false ) {
if ( ! $popup_id ) {
return;
}
$action = 'form_submission_success';
if ( ! $result || \is_wp_error( $result ) ) {
$action = 'form_submission_failure';
}
$popup_data = self::get_popup_metadata( $popup_id );
return array_merge(
$popup_data,
[
'action' => $action,
]
);
}

}
Popups::init();

0 comments on commit ab407d4

Please sign in to comment.