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/ia cleanup settings #3641

Draft
wants to merge 6 commits into
base: epic/ia
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/tracking/class-twitter-pixel.php';
include_once NEWSPACK_ABSPATH . 'includes/revisions-control/class-revisions-control.php';
include_once NEWSPACK_ABSPATH . 'includes/authors/class-authors-custom-fields.php';
include_once NEWSPACK_ABSPATH . 'includes/class-syndication.php';

include_once NEWSPACK_ABSPATH . 'includes/starter_content/class-starter-content-provider.php';
include_once NEWSPACK_ABSPATH . 'includes/starter_content/class-starter-content-generated.php';
Expand All @@ -140,6 +141,7 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/wizards/newspack/class-newspack-dashboard.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/newspack/class-newspack-settings.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/newspack/class-custom-events-section.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/newspack/class-syndication-section.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/newspack/class-seo-wizard.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/newspack/class-pixels-section.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/newspack/class-recirculation-section.php';
Expand All @@ -166,7 +168,6 @@ private function includes() {
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';
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-engagement-wizard.php';
include_once NEWSPACK_ABSPATH . 'includes/wizards/class-site-design-wizard.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Newspack's Settings
* Newspack's Syndication Settings
*
* @package Newspack
*/
Expand All @@ -9,54 +9,48 @@

defined( 'ABSPATH' ) || exit;

require_once NEWSPACK_ABSPATH . '/includes/wizards/class-wizard.php';

/**
* Settings.
* Syndication
*/
class Settings extends Wizard {
const SETTINGS_OPTION_NAME = 'newspack_settings';

const MODULE_ENABLED_PREFIX = 'module_enabled_';

class Syndication {
/**
* The slug of this wizard.
* The name of the option that stores the settings.
*
* @TODO: Consider a more relevant option name e.g. 'newspack_syndication_settings'.
*
* @var string
*/
protected $slug = 'newspack-settings-wizard';
const OPTION_NAME = 'newspack_settings';

/**
* The capability required to access this wizard.
* The prefix for the module enabled settings.
*
* @var string
*/
protected $capability = 'manage_options';
const MODULE_ENABLED_PREFIX = 'module_enabled_';

/**
* Constructor.
*/
public function __construct() {
parent::__construct();
add_action( 'rest_api_init', [ $this, 'register_api_endpoints' ] );
$this->hidden = true;
}

/**
* Get all settings.
*/
private static function get_settings() {
public static function get_settings() {
$default_settings = [
self::MODULE_ENABLED_PREFIX . 'rss' => false,
self::MODULE_ENABLED_PREFIX . 'media-partners' => false,
];
return wp_parse_args( get_option( self::SETTINGS_OPTION_NAME ), $default_settings );
return wp_parse_args( get_option( self::OPTION_NAME ), $default_settings );
}

/**
* Get the list of available optional modules.
*/
private static function get_available_optional_modules() {
public static function get_available_optional_modules() {
return [ 'rss' ];
}

Expand All @@ -73,7 +67,7 @@ public static function api_get_settings() {
* @param string $module_name Name of the module.
*/
public static function is_optional_module_active( $module_name ) {
$settings = self::api_get_settings();
$settings = self::get_settings();
$setting_name = self::MODULE_ENABLED_PREFIX . $module_name;
if ( isset( $settings[ $setting_name ] ) ) {
return $settings[ $setting_name ];
Expand All @@ -100,7 +94,7 @@ private static function update_setting( $key, $value ) {
$settings = self::get_settings();
if ( isset( $settings[ $key ] ) ) {
$settings[ $key ] = $value;
update_option( self::SETTINGS_OPTION_NAME, $settings );
update_option( self::OPTION_NAME, $settings );
}
return $settings;
}
Expand All @@ -117,45 +111,8 @@ public static function api_update_settings( $request ) {
$setting_name = self::MODULE_ENABLED_PREFIX . $module_name;
$settings[ $setting_name ] = $request->get_param( $setting_name );
}
update_option( self::SETTINGS_OPTION_NAME, $settings );
return self::api_get_settings();
}

/**
* Register the endpoints needed for the wizard screens.
*/
public function register_api_endpoints() {
register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/' . $this->slug,
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'api_get_settings' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
]
);

$required_args = array_reduce(
self::get_available_optional_modules(),
function( $acc, $module_name ) {
$acc[ self::MODULE_ENABLED_PREFIX . $module_name ] = [
'required' => true,
'sanitize_callback' => 'rest_sanitize_boolean',
];
return $acc;
},
[]
);
register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/' . $this->slug,
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ $this, 'api_update_settings' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
'args' => $required_args,
]
);
update_option( self::OPTION_NAME, $settings );
return self::get_settings();
}

/**
Expand All @@ -164,7 +121,7 @@ function( $acc, $module_name ) {
* @return string The wizard name.
*/
public function get_name() {
return \esc_html__( 'Settings', 'newspack' );
return esc_html__( 'Settings', 'newspack' );
}

/**
Expand All @@ -173,7 +130,7 @@ public function get_name() {
* @return string The wizard description.
*/
public function get_description() {
return \esc_html__( 'Configure settings.', 'newspack' );
return esc_html__( 'Configure settings.', 'newspack' );
}

/**
Expand Down
3 changes: 1 addition & 2 deletions includes/class-wizards.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ class Wizards {
public static function init() {
self::$wizards = [
'site-design' => new Site_Design_Wizard(),
'syndication' => new Syndication_Wizard(),
'analytics' => new Analytics_Wizard(),
'components-demo' => new Components_Demo(),
'seo' => new SEO_Wizard(),
'health-check' => new Health_Check_Wizard(),
'engagement' => new Engagement_Wizard(),
'connections' => new Connections_Wizard(),
'settings' => new Settings(),
// v2 Information Architecture.
'newspack-dashboard' => new Newspack_Dashboard(),
'setup' => new Setup_Wizard(),
Expand All @@ -47,6 +45,7 @@ public static function init() {
'custom-events' => 'Newspack\Wizards\Newspack\Custom_Events_Section',
'social-pixels' => 'Newspack\Wizards\Newspack\Pixels_Section',
'recirculation' => 'Newspack\Wizards\Newspack\Recirculation_Section',
'syndication' => 'Newspack\Wizards\Newspack\Syndication_Section',
],
]
),
Expand Down
2 changes: 1 addition & 1 deletion includes/optional-modules/class-media-partners.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Media_Partners {
* Initialize everything.
*/
public static function init() {
if ( ! Settings::is_optional_module_active( 'media-partners' ) ) {
if ( ! Syndication::is_optional_module_active( 'media-partners' ) ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion includes/optional-modules/class-rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RSS {
* Initialise.
*/
public static function init() {
if ( ! Settings::is_optional_module_active( 'rss' ) ) {
if ( ! Syndication::is_optional_module_active( 'rss' ) ) {
return;
}

Expand Down
75 changes: 0 additions & 75 deletions includes/wizards/class-syndication-wizard.php

This file was deleted.

74 changes: 74 additions & 0 deletions includes/wizards/newspack/class-syndication-section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Syndication Section Object.
*
* @package Newspack
*/

namespace Newspack\Wizards\Newspack;

/**
* WordPress dependencies
*/

use Newspack\Syndication;
use WP_REST_Server;

/**
* Internal dependencies
*/
use Newspack\Wizards\Wizard_Section;

/**
* Syndication Section Object.
*
* @package Newspack\Wizards\Newspack
*/
class Syndication_Section extends Wizard_Section {

/**
* Containing wizard slug.
*
* @var string
*/
protected $wizard_slug = 'newspack-settings';

/**
* Register Wizard Section specific endpoints.
*
* @return void
*/
public function register_rest_routes() {
register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/' . $this->wizard_slug . '/syndication',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ Syndication::class, 'api_get_settings' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
]
);

$required_args = array_reduce(
Syndication::get_available_optional_modules(),
function( $acc, $module_name ) {
$acc[ Syndication::MODULE_ENABLED_PREFIX . $module_name ] = [
'required' => true,
'sanitize_callback' => 'rest_sanitize_boolean',
];
return $acc;
},
[]
);
register_rest_route(
NEWSPACK_API_NAMESPACE,
'/wizard/' . $this->wizard_slug . '/syndication',
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ Syndication::class, 'api_update_settings' ],
'permission_callback' => [ $this, 'api_permissions_check' ],
'args' => $required_args,
]
);
}
}
Loading