Skip to content

Commit

Permalink
feat(ia): newsletters (#3490)
Browse files Browse the repository at this point in the history
Co-authored-by: Miguel Peixe <[email protected]>
  • Loading branch information
ronchambers and miguelpeixe authored Nov 1, 2024
1 parent d2dc24b commit 8ad5986
Show file tree
Hide file tree
Showing 20 changed files with 985 additions and 357 deletions.
9 changes: 6 additions & 3 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private function includes() {

include_once NEWSPACK_ABSPATH . 'includes/wizards/class-setup-wizard.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-components-demo.php';

include_once NEWSPACK_ABSPATH . 'includes/wizards/traits/trait-wizards-admin-header.php';

// Newspack Wizards and Sections.
Expand All @@ -140,14 +140,17 @@ private function includes() {

include_once NEWSPACK_ABSPATH . 'includes/wizards/class-setup-wizard.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-components-demo.php';
// Advertising Wizard.

// Advertising Wizard.
include_once NEWSPACK_ABSPATH . 'includes/wizards/advertising/class-advertising-display-ads.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/advertising/class-advertising-sponsors.php';

// Network Wizard.
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-network-wizard.php';

// Newsletters Wizard.
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-newsletters-wizard.php';

/* Unified Wizards */
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-settings.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-analytics-wizard.php';
Expand Down
45 changes: 41 additions & 4 deletions includes/class-wizards.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,25 @@ public static function init() {
'settings' => new Settings(),
// v2 Information Architecture.
'newspack-dashboard' => new Newspack_Dashboard(),
'newspack-settings' => new Newspack_Settings(
'newspack-settings' => new Newspack_Settings(
[
'sections' => [
'custom-events' => 'Newspack\Wizards\Newspack\Custom_Events_Section',
'social-pixels' => 'Newspack\Wizards\Newspack\Pixels_Section',
'recirculation' => 'Newspack\Wizards\Newspack\Recirculation_Section',
],
]
]
),
'advertising-display-ads' => new Advertising_Display_Ads(),
'advertising-sponsors' => new Advertising_Sponsors(),
'network' => new Network_Wizard(),
'newsletters' => new Newsletters_Wizard(),
];

// Not needed in $wizards[] since it's just for Admin Headers, not full react pages.
new Network_Wizard();
// Allow custom menu order.
add_filter( 'custom_menu_order', '__return_true' );
// Fix menu order for wizards with parent menu items.
add_filter( 'menu_order', [ __CLASS__, 'menu_order' ], 11 );
}

/**
Expand Down Expand Up @@ -133,5 +137,38 @@ public static function is_completed( $wizard_slug ) {

return false;
}

/**
* Update menu order for wizards with parent menu items.
*
* @param array $menu_order The current menu order.
*
* @return array The updated menu order.
*/
public static function menu_order( $menu_order ) {
$index = array_search( 'newspack-dashboard', $menu_order, true );
if ( false === $index ) {
return $menu_order;
}
$ordered_wizards = [];
foreach ( self::$wizards as $slug => $wizard ) {
if ( ! empty( $wizard->parent_menu ) && ! empty( $wizard->menu_order ) ) {
$ordered_wizards[ $wizard->menu_order ] = $wizard->parent_menu;
}
}
if ( empty( $ordered_wizards ) ) {
return $menu_order;
}
ksort( $ordered_wizards );
foreach ( array_reverse( $ordered_wizards ) as $menu_item ) {
$key = array_search( $menu_item, $menu_order, true );
if ( false === $key ) {
continue;
}
array_splice( $menu_order, $key, 1 );
array_splice( $menu_order, $index + 1, 0, $menu_item );
}
return $menu_order;
}
}
Wizards::init();
17 changes: 15 additions & 2 deletions includes/wizards/advertising/class-advertising-display-ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ class Advertising_Display_Ads extends Wizard {
),
);

/**
* The parent menu item name.
*
* @var string
*/
public $parent_menu = 'advertising-display-ads';

/**
* Order relative to the Newspack Dashboard menu item.
*
* @var int
*/
public $menu_order = 4;

/**
* Constructor.
*/
Expand Down Expand Up @@ -546,8 +560,7 @@ public function add_page() {
$this->capability,
$this->slug,
array( $this, 'render_wizard' ),
$icon,
3.5
$icon
);
add_submenu_page(
$this->slug,
Expand Down
27 changes: 10 additions & 17 deletions includes/wizards/advertising/class-advertising-sponsors.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ class Advertising_Sponsors extends Wizard {

/**
* Newspack Sponsors CPT name.
*
*
* @var string
*/
const CPT_NAME = 'newspack_spnsrs_cpt';

/**
* Sponsors CPT list path.
*
*
* @var string
*/
const URL = 'edit.php?post_type=newspack_spnsrs_cpt';

/**
/**
* Advertising Page path.
*
*
* @var string
*/
const PARENT_URL = 'admin.php?page=advertising-display-ads';
Expand All @@ -47,13 +47,6 @@ class Advertising_Sponsors extends Wizard {
*/
protected $capability = 'manage_options';

/**
* High menu priority since we need core registrations to exist before we can modify them.
*
* @var int
*/
protected $menu_priority = 99;

/**
* Advertising_Sponsors Constructor.
*/
Expand Down Expand Up @@ -83,8 +76,8 @@ public function __construct() {
'textContent' => esc_html__( 'Settings', 'newspack-plugin' ),
'href' => admin_url( static::URL . '&page=newspack-sponsors-settings-admin' ),
],
],
'title' => $this->get_name(),
],
'title' => $this->get_name(),
]
);
}
Expand Down Expand Up @@ -174,19 +167,19 @@ public function update_sponsors_cpt_args( $args, $post_type ) {
* Parent file filter. Used to determine active menu items.
*
* @param string $parent_file Parent file to be overridden.
* @return string
* @return string
*/
public function parent_file( $parent_file ) {
global $pagenow, $typenow;

if ( in_array( $pagenow, [ 'post.php', 'post-new.php' ] ) && $typenow === static::CPT_NAME ) {
return 'advertising-display-ads';
}

if ( isset( $_GET['page'] ) && $_GET['page'] === 'newspack-sponsors-settings-admin' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return static::PARENT_URL;
}

return $parent_file;
}

Expand All @@ -200,7 +193,7 @@ public function submenu_file( $submenu_file ) {
if ( isset( $_GET['page'] ) && $_GET['page'] === 'newspack-sponsors-settings-admin' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return static::URL;
}

return $submenu_file;
}
}
7 changes: 0 additions & 7 deletions includes/wizards/class-components-demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ class Components_Demo extends Wizard {
*/
protected $capability = 'manage_options';

/**
* Priority setting for ordering admin submenu items.
*
* @var int.
*/
protected $menu_priority = 100;

/**
* Whether the wizard should be displayed in the Newspack submenu.
*
Expand Down
79 changes: 0 additions & 79 deletions includes/wizards/class-engagement-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function __construct() {
parent::__construct();
add_action( 'rest_api_init', [ $this, 'register_api_endpoints' ] );
add_filter( 'jetpack_relatedposts_filter_date_range', [ $this, 'restrict_age_of_related_posts' ] );
add_filter( 'newspack_newsletters_settings_url', [ $this, 'newsletters_settings_url' ] );
}

/**
Expand Down Expand Up @@ -131,33 +130,6 @@ public function register_api_endpoints() {
'permission_callback' => [ $this, 'api_permissions_check' ],
]
);
register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/' . $this->slug . '/newsletters',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'api_get_newsletters_settings' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
]
);
register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/' . $this->slug . '/newsletters',
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ $this, 'api_update_newsletters_settings' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
]
);
register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/' . $this->slug . '/newsletters/lists',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'api_get_newsletters_lists' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
]
);

$meta_pixel = new Meta_Pixel();
register_rest_route(
Expand Down Expand Up @@ -305,57 +277,6 @@ public function api_activate_reader_activation( WP_REST_Request $request ) {
return rest_ensure_response( $response );
}

/**
* Get lists of configured ESP.
*/
public static function api_get_newsletters_lists() {
$newsletters_configuration_manager = Configuration_Managers::configuration_manager_class_for_plugin_slug( 'newspack-newsletters' );
return $newsletters_configuration_manager->get_lists();
}

/**
* Get Newspack Newsletters setttings.
*
* @return object with the info.
*/
private static function get_newsletters_settings() {
$newsletters_configuration_manager = Configuration_Managers::configuration_manager_class_for_plugin_slug( 'newspack-newsletters' );
$settings = array_reduce(
$newsletters_configuration_manager->get_settings(),
function ( $acc, $value ) {
$acc[ $value['key'] ] = $value;
return $acc;
},
[]
);
return [
'configured' => $newsletters_configuration_manager->is_configured(),
'settings' => $settings,
];
}

/**
* Get Newspack Newsletters setttings API response.
*
* @return WP_REST_Response with the info.
*/
public function api_get_newsletters_settings() {
return rest_ensure_response( self::get_newsletters_settings() );
}

/**
* Get Newspack Newsletters setttings.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response with the info.
*/
public function api_update_newsletters_settings( $request ) {
$args = $request->get_params();
$newsletters_configuration_manager = Configuration_Managers::configuration_manager_class_for_plugin_slug( 'newspack-newsletters' );
$newsletters_configuration_manager->update_settings( $args );
return $this->api_get_newsletters_settings();
}

/**
* Update the Related Posts Max Age setting.
*
Expand Down
Loading

0 comments on commit 8ad5986

Please sign in to comment.