Skip to content

Commit

Permalink
Merged PR 55056: Initialise active tab on submit
Browse files Browse the repository at this point in the history
## What's being changed

We have moved the initialization of the page tabs to the render function and added a new options register hook.

## Why it's being changed

The page initialize was being called on every page load, even if it was not part of the DD plugin.

## How to review / test this change

1. Ensure the plugin is not registering a page tab on every load, only the page intended.
2. Ensure the page save function works as expected.

Related work items: #258420
  • Loading branch information
pvpcookie committed Jun 7, 2024
1 parent 390dfa0 commit 5c76dcd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
35 changes: 18 additions & 17 deletions admin/class-dotdigital-wordpress-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,9 @@ public function render_tab_links() {
* @return void
*/
public function add_plugin_page_tabs() {
if ( ! $this->is_dd_wp_settings_page() ) {
return;
}
foreach ( $this->available_page_tabs as $page_tab ) {
try {
$tab = new $page_tab();
if ( $this->is_current_tab( $tab ) ) {
$tab->initialise();
}
$this->page_tabs[ $tab->get_slug() ] = $tab;
} catch ( \Exception $e ) {
error_log( $e );
Expand All @@ -139,13 +133,30 @@ public function add_plugin_page_tabs() {
}

/**
* Render the active tab for the settings page.
* Ensure the form has been properly registered on submission.
*
* @return void
*/
public function add_plugin_page_submission_initialisation() {
global $pagenow;

if ( 'options.php' === $pagenow && isset( $_POST['option_page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$option_page = sanitize_text_field( wp_unslash( $_POST['option_page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $this->page_tabs[ $option_page ] ) ) {
$this->page_tabs[ $option_page ]->initialise();
}
}
}

/**
* Render and initialise the active tab for the settings page.
*
* @return void
*/
public function render_active_tab() {
foreach ( $this->page_tabs as $page_tab ) {
if ( $this->is_current_tab( $page_tab ) ) {
$page_tab->initialise();
$page_tab->render();
}
}
Expand Down Expand Up @@ -246,16 +257,6 @@ private static function dotdigital_item_sort_desc( object $a, object $b ) {
return $a_name > $b_name ? -1 : 1;
}

/**
* Check if current page is ?page=dotdigital-for-wordpress-settings
*
* @return bool
*/
private function is_dd_wp_settings_page() {
return isset( $_GET['page'] ) &&
strpos( sanitize_text_field( wp_unslash( $_GET['page'] ) ), Dotdigital_WordPress_Settings_Admin::URL_SLUG ) !== false;
}

/**
* Check if current tab is in query string
*
Expand Down
5 changes: 3 additions & 2 deletions includes/class-dotdigital-wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ private function define_admin_hooks() {
$plugin_admin = new Dotdigital_WordPress_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
$this->loader->add_action( 'admin_init', $plugin_admin, 'add_plugin_page_submission_initialisation', 1 );
$this->loader->add_action( 'admin_init', $plugin_admin, 'add_plugin_admin_page_actions' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_page_tabs' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menus' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_page_actions', 5 );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_page_tabs', 5 );
}

/**
Expand Down

0 comments on commit 5c76dcd

Please sign in to comment.