Skip to content

Commit

Permalink
Merge pull request #420 from Automattic/master
Browse files Browse the repository at this point in the history
Release Jan 28
  • Loading branch information
dkoo authored Jan 28, 2021
2 parents 90d8225 + a3be6ca commit 9ddc220
Show file tree
Hide file tree
Showing 10 changed files with 1,052 additions and 551 deletions.
32 changes: 24 additions & 8 deletions api/campaigns/class-campaign-data-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ public static function is_donor( $client_data ) {
return ! empty( $client_data['donations'] );
}

/**
* Compare page referrer to a list of referrers.
*
* @param string $page_referrer_url Referrer to compare to.
* @param string $referrers_list_string Comma-separated referrer domains list.
*/
public static function does_referrer_match( $page_referrer_url, $referrers_list_string ) {
$referer_domain = parse_url( $page_referrer_url, PHP_URL_HOST ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$referer_domain_root = implode( '.', array_slice( explode( '.', $referer_domain ), -2 ) );
$referrer_matches = in_array(
$referer_domain_root,
array_map( 'trim', explode( ',', $referrers_list_string ) )
);
return $referrer_matches;
}

/**
* Is client a subscriber?
*
Expand Down Expand Up @@ -185,18 +201,18 @@ function ( $read_counts, $read_post ) {
if ( empty( $page_referrer_url ) ) {
$should_display = false;
}
$referer_domain = parse_url( $page_referrer_url, PHP_URL_HOST ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
// Handle the 'www' prefix – assume `www.example.com` and `example.com` referrers are the same.
$referer_domain_alternative = strpos( $referer_domain, 'www.' ) === 0 ? substr( $referer_domain, 4 ) : "www.$referer_domain";
$referrer_matches = array_intersect(
[ $referer_domain, $referer_domain_alternative ],
array_map( 'trim', explode( ',', $campaign_segment->referrers ) )
);
if ( empty( $referrer_matches ) ) {
if ( empty( self::does_referrer_match( $page_referrer_url, $campaign_segment->referrers ) ) ) {
$should_display = false;
}
}

/**
* By referrer domain - negative.
*/
if ( ! empty( $campaign_segment->referrers_not ) && ! empty( self::does_referrer_match( $page_referrer_url, $campaign_segment->referrers_not ) ) ) {
$should_display = false;
}

/**
* By most read category.
*/
Expand Down
3 changes: 3 additions & 0 deletions api/segmentation/class-segmentation-client-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function report_client_data( $request ) {
// Add a donation to client.
$donation = $this->get_request_param( 'donation', $request );
if ( $donation ) {
if ( 'string' === $donation ) {
$donation = json_decode( $donation );
}
$client_data_update['donations'][] = $donation;
}

Expand Down
25 changes: 21 additions & 4 deletions includes/class-newspack-popups-segmentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,13 @@ public static function insert_amp_analytics() {
],
];

$client_data_endpoint = self::get_client_data_endpoint();
$amp_analytics_config['requests'] = [
'event' => esc_url( $client_data_endpoint ),
];

// Handle Mailchimp URL parameters.
if ( isset( $_GET['mc_cid'], $_GET['mc_eid'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$endpoint = self::get_client_data_endpoint();
$amp_analytics_config['requests'] = [
'event' => esc_url( $endpoint ),
];
$amp_analytics_config['triggers']['trackMailchimpData'] = [
'on' => 'ini-load',
'request' => 'event',
Expand All @@ -245,6 +246,22 @@ public static function insert_amp_analytics() {
];
}

$donor_landing_page = Newspack_Popups_Settings::donor_landing_page();
if ( $donor_landing_page && intval( $donor_landing_page ) === get_queried_object_id() ) {
$amp_analytics_config['triggers']['reportDonor'] = [
'on' => 'ini-load',
'request' => 'event',
'extraUrlParams' => [
'donation' => wp_json_encode(
[
'offsite_has_donated' => true,
]
),
'client_id' => 'CLIENT_ID(' . esc_attr( self::NEWSPACK_SEGMENTATION_CID_NAME ) . ')',
],
];
}

?>
<amp-analytics>
<script type="application/json">
Expand Down
46 changes: 44 additions & 2 deletions includes/class-newspack-popups-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ public static function get_setting( $key ) {
* Return all settings.
*/
public static function get_settings() {
$donor_landing_options = [];
$donor_landing_options_query = new \WP_Query(
[
'post_type' => [ 'page' ],
'post_status' => [ 'publish' ],
'post_parent' => 0,
'posts_per_page' => 100,
]
);

if ( $donor_landing_options_query->have_posts() ) {
foreach ( $donor_landing_options_query->posts as $page ) {
$donor_landing_options[] = [
'label' => $page->post_title,
'value' => $page->ID,
];
}
}

return [
[
'key' => 'suppress_newsletter_campaigns',
Expand All @@ -99,7 +118,7 @@ public static function get_settings() {
),
],
[
'key' => 'newspack_newsletters_non_interative_mode',
'key' => 'newspack_popups_non_interative_mode',
'value' => self::is_non_interactive(),
'label' => __(
'Enable non-interactive mode.',
Expand All @@ -110,6 +129,21 @@ public static function get_settings() {
'newspack-popups'
),
],
[
'key' => 'newspack_popups_donor_landing_page',
'value' => self::donor_landing_page(),
'label' => __(
'Donor landing page',
'newspack-popups'
),
'help' => __(
"Set a page on your site as a donation landing page. Once a reader views this page, they will be considered a donor. This is helpful if you're using an off-site donation platform but still want to target donors as an audience segment.",
'newspack-popups'
),
'type' => 'select',
'options' => $donor_landing_options,
'no_option_text' => __( '-- None --', 'newspack-popups' ),
],
[
'key' => 'all_segments',
'value' => array_reduce(
Expand All @@ -132,7 +166,15 @@ function( $acc, $item ) {
* Is the non-interactive setting on?
*/
public static function is_non_interactive() {
return get_option( 'newspack_newsletters_non_interative_mode', false );
// Handle legacy option name.
return get_option( 'newspack_newsletters_non_interative_mode', false ) || get_option( 'newspack_popups_non_interative_mode', false );
}

/**
* Donor landing page.
*/
public static function donor_landing_page() {
return get_option( 'newspack_popups_donor_landing_page', '' );
}

/**
Expand Down
Loading

0 comments on commit 9ddc220

Please sign in to comment.