Skip to content

Commit

Permalink
fix: fallback if no newspack plugin (#1179)
Browse files Browse the repository at this point in the history
* fix: allow prompts to be displayed without segmentation if no Newspack Plugin

* refactor: avoid use of negative variable name

* fix: newspack_popups_view.segments is not an array

* fix: hide segments taxonomy

---------

Co-authored-by: Leo Germani <[email protected]>
Co-authored-by: dkoo <[email protected]>
  • Loading branch information
3 people authored Aug 15, 2023
1 parent af8f6b7 commit 380765a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 63 deletions.
32 changes: 18 additions & 14 deletions includes/class-newspack-popups-inserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,23 +658,27 @@ public static function enqueue_scripts() {
true
);

$segments = Newspack_Popups_Segmentation::get_segments( false );

// Gather segments for all prompts to be displayed.
foreach ( $segments as $segment ) {
if ( ! empty( $segment ) && ! empty( $segment['criteria'] ) && ! isset( self::$segments[ $segment['id'] ] ) ) {
self::$segments[ $segment['id'] ] = [
'criteria' => $segment['criteria'],
'priority' => $segment['priority'],
];
}
}

$script_data = [
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
'segments' => self::$segments,
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
];

if ( Newspack_Popups::$segmentation_enabled ) {
$segments = Newspack_Popups_Segmentation::get_segments( false );

// Gather segments for all prompts to be displayed.
foreach ( $segments as $segment ) {
if ( ! empty( $segment ) && ! empty( $segment['criteria'] ) && ! isset( self::$segments[ $segment['id'] ] ) ) {
self::$segments[ $segment['id'] ] = [
'criteria' => $segment['criteria'],
'priority' => $segment['priority'],
];
}
}

$script_data['segments'] = self::$segments;

}

\wp_localize_script( $script_handle, 'newspack_popups_view', $script_data );
\wp_enqueue_script( $script_handle );
}
Expand Down
16 changes: 15 additions & 1 deletion includes/class-newspack-popups.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ final class Newspack_Popups {
*/
protected static $instance = null;

/**
* Whether the segmentation features are enabled
*
* @var bool
*/
public static $segmentation_enabled;

/**
* Main Newspack Ads Instance.
* Ensures only one instance of Newspack Ads is loaded or can be loaded.
Expand All @@ -69,6 +76,12 @@ public static function instance() {
* Constructor.
*/
public function __construct() {

// Segmentation requires the main Newspack plugin.
self::$segmentation_enabled = class_exists( '\Newspack\Reader_Data' );

add_action( 'admin_init', [ __CLASS__, 'create_lightweight_api_config' ] );
add_action( 'admin_notices', [ __CLASS__, 'api_config_missing_notice' ] );
add_action( 'cli_init', [ __CLASS__, 'register_cli_commands' ] );

add_action( 'init', [ __CLASS__, 'register_cpt' ] );
Expand Down Expand Up @@ -667,6 +680,7 @@ public static function enqueue_block_editor_assets() {
)
),
'preview_query_keys' => self::PREVIEW_QUERY_KEYS,
'segmentation_enabled' => self::$segmentation_enabled,
]
);
\wp_enqueue_style(
Expand Down Expand Up @@ -1213,7 +1227,7 @@ public static function migrate_user_data() {
if (
! is_user_logged_in() ||
get_user_meta( get_current_user_id(), 'newspack_popups_reader_data_migrated', true ) ||
! class_exists( 'Newspack\Reader_Data' )
! self::$segmentation_enabled
) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions includes/class-newspack-segments-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public static function register_segments_taxonomy() {
'description' => __( 'Segments for popups', 'newspack-popups' ),
'hierarchical' => true, // just to get the checkbox UI.
'public' => false,
'show_ui' => true,
'show_in_rest' => true,
'show_ui' => Newspack_Popups::$segmentation_enabled,
'show_in_rest' => Newspack_Popups::$segmentation_enabled,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
Expand Down
24 changes: 13 additions & 11 deletions src/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,19 @@ registerPlugin( 'newspack-popups', {
icon: null,
} );

registerPlugin( 'newspack-popups-frequency', {
render: () => (
<PluginDocumentSettingPanel
name="-frequency-panel"
title={ __( 'Frequency', 'newspack-popups' ) }
>
<FrequencySidebarWithData />
</PluginDocumentSettingPanel>
),
icon: null,
} );
if ( window?.newspack_popups_data?.segmentation_enabled ) {
registerPlugin( 'newspack-popups-frequency', {
render: () => (
<PluginDocumentSettingPanel
name="-frequency-panel"
title={ __( 'Frequency', 'newspack-popups' ) }
>
<FrequencySidebarWithData />
</PluginDocumentSettingPanel>
),
icon: null,
} );
}

registerPlugin( 'newspack-popups-colors', {
render: () => (
Expand Down
21 changes: 16 additions & 5 deletions src/view/segmentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import {
* Match reader to segments.
*/
export const handleSegmentation = prompts => {
window.newspackRAS = window.newspackRAS || [];
window.newspackRAS.push( ras => {
const maybeDisplayPrompts = ( ras = null ) => {
const segments = newspack_popups_view?.segments || {};
const matchingSegment = getBestPrioritySegment( segments );
debug( 'matchingSegment', matchingSegment );
// Log a pageview for frequency counts.
logPageview( ras );
if ( ras ) {
logPageview( ras );
}
let overlayDisplayed;

prompts.forEach( prompt => {
Expand Down Expand Up @@ -54,7 +55,9 @@ export const handleSegmentation = prompts => {
prompt.classList.remove( 'hidden' );

// Log a "prompt_seen" activity when the prompt becomes visible.
handleSeen( prompt, ras );
if ( ras ) {
handleSeen( prompt, ras );
}
};
if ( isOverlay ) {
const scroll = prompt.getAttribute( 'data-scroll' );
Expand All @@ -77,5 +80,13 @@ export const handleSegmentation = prompts => {
// Debug logging for prompt display.
debug( promptId, shouldDisplay );
} );
} );
};

// If no segments to handle.
if ( ! newspack_popups_view.segments ) {
maybeDisplayPrompts();
} else {
window.newspackRAS = window.newspackRAS || [];
window.newspackRAS.push( maybeDisplayPrompts );
}
};
63 changes: 33 additions & 30 deletions src/view/utils/segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,48 @@ export const shouldPromptBeDisplayed = ( prompt, matchingSegment, ras, override
return override;
}

// By frequency.
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const [ start, between, max, reset ] = prompt.getAttribute( 'data-frequency' ).split( ',' );
const pageviews = ras.store.get( 'pageviews' );
if ( pageviews[ reset ] ) {
const views = pageviews[ reset ].count || 0;
if ( ras ) {
// By frequency.
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const [ start, between, max, reset ] = prompt.getAttribute( 'data-frequency' ).split( ',' );
const pageviews = ras.store.get( 'pageviews' );
if ( pageviews[ reset ] ) {
const views = pageviews[ reset ].count || 0;

// If reader hasn't amassed enough pageviews yet.
if ( views <= parseInt( start ) ) {
return false;
}
// If reader hasn't amassed enough pageviews yet.
if ( views <= parseInt( start ) ) {
return false;
}

// If not displaying every pageview.
if ( 0 < between ) {
const viewsAfterStart = Math.max( 0, views - ( parseInt( start ) + 1 ) );
if ( 0 < viewsAfterStart % ( parseInt( between ) + 1 ) ) {
// If not displaying every pageview.
if ( 0 < between ) {
const viewsAfterStart = Math.max( 0, views - ( parseInt( start ) + 1 ) );
if ( 0 < viewsAfterStart % ( parseInt( between ) + 1 ) ) {
return false;
}
}

// If there's a max frequency.
const promptId = getRawId( prompt.getAttribute( 'id' ) );
const seenEvents = ( ras.getActivities( 'prompt_seen' ) || [] ).filter( activity => {
return (
activity.data?.prompt_id === promptId &&
periods[ reset ] > Date.now() - activity.timestamp
);
} );
if ( 0 < parseInt( max ) && seenEvents.length >= parseInt( max ) ) {
return false;
}
}

// If there's a max frequency.
const promptId = getRawId( prompt.getAttribute( 'id' ) );
const seenEvents = ( ras.getActivities( 'prompt_seen' ) || [] ).filter( activity => {
return (
activity.data?.prompt_id === promptId && periods[ reset ] > Date.now() - activity.timestamp
);
} );
if ( 0 < parseInt( max ) && seenEvents.length >= parseInt( max ) ) {
// By assigned segments.
const assignedSegments = prompt.getAttribute( 'data-segments' )
? prompt.getAttribute( 'data-segments' ).split( ',' )
: null;
if ( assignedSegments && 0 > assignedSegments.indexOf( matchingSegment ) ) {
return false;
}
}

// By assigned segments.
const assignedSegments = prompt.getAttribute( 'data-segments' )
? prompt.getAttribute( 'data-segments' ).split( ',' )
: null;
if ( assignedSegments && 0 > assignedSegments.indexOf( matchingSegment ) ) {
return false;
}

return true;
};

0 comments on commit 380765a

Please sign in to comment.