Cmb2: Multiple update messages when multiple instances of option pages

Created on 14 Nov 2017  路  4Comments  路  Source: CMB2/CMB2

Expected Behavior:

On updating an option page to get a single 'Settings Updated' message, even when multiple plugins use CMB2 Options pages.

Actual Behavior:

Depending on how many plugins are loaded with CMB2 option pages you can get multiple 'Settings Updated' message

Steps to reproduce (I have confirmed I can reproduce this issue on the trunk branch):

  1. create a 2 plugins with different option pages
  2. activate just one / activate two
  3. trace code with debugger

CMB2 Field Registration Code:

Not applicable

Proposed fix:

cmb2/includes/CMB2_Options_hookup.php
line 109 onwards

if ( ! empty( $_GET['settings-updated'] ) ) {
            if ( 'true' === $_GET['settings-updated'] ) {
                add_settings_error( "{$this->option_key}-notices", '', __( 'Settings updated.', 'cmb2' ), 'updated' );
            } else {
                add_settings_error( "{$this->option_key}-notices", '', __( 'Nothing to update.', 'cmb2' ), 'notice-warning' );
            }
        }

checks settings-updates for every option page, so if multiple options pages this is true multiple times.
Adding a check against the page stops this e.g.

if ( ( ! empty( $_GET['settings-updated'] ) ) && ($this->option_key == $_GET['page'] ) ){
            if ( 'true' === $_GET['settings-updated'] ) {
                add_settings_error( "{$this->option_key}-notices", '', __( 'Settings updated.', 'cmb2' ), 'updated' );
            } else {
                add_settings_error( "{$this->option_key}-notices", '', __( 'Nothing to update.', 'cmb2' ), 'notice-warning' );
            }
        }

Pull request

I haven't yet, but happy to create a pull request if this makes sense

bug

All 4 comments

Good catch. I have noticed this too, but haven't had the time to dig in. Your suggestion looks like a good one. I'll dig into it.

The problem is slightly worse than explained, as any options page will show double notices, not just CMB2 as the if statement catches them all. I'm running my code in two of my plugins - so far so good.

If I'm reading the new code correctly, the updated message now can be overridden by a callback? If so that is neat, as I was trying hard to achieve that too, as often a setting change performs a function ( like clearing data ) on update / save.

Yes, that is now possible! See the example functions file for a demonstration: https://github.com/CMB2/CMB2/blob/trunk/example-functions.php#L687-L720

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mathetos picture mathetos  路  8Comments

GaryJones picture GaryJones  路  3Comments

jquimera picture jquimera  路  8Comments

jflagarde picture jflagarde  路  6Comments

gareth-gillman picture gareth-gillman  路  6Comments