imath / wp-statuses

WordPress plugin to ease Custom Post Statuses integration
GNU General Public License v2.0
161 stars 27 forks source link

switch_to_locale() is causing the plugin to crash #89

Closed bonny closed 5 months ago

bonny commented 5 months ago

If a plugin calls switch_to_locale():

add_action(
    'wp_loaded',
    function() {
        switch_to_locale( 'de_DE' );
    }
);

then WP Statuses will crash

Fatal error: Uncaught Error: Call to undefined method stdClass::is_builtin()

Reason:

Currently the function wp_statuses_register that converts the status objects to instances of WP_Statuses_Core_Status is called at init:

add_action( 'init', 'wp_statuses_register', 1000 );

If a plugin then somewhere calles the WP function switch_to_locale() the status objects are recreated by WordPress, due to the function create_initial_post_types being called again from hook change_locale: add_action( 'change_locale', 'create_initial_post_types' );

Possible solution is to run wp_status_register also on the change_locale hook (just as create_initial_post_types does):

add_action( 'change_locale', 'wp_statuses_register', 10 );

imath commented 5 months ago

Hi @bonny

Thanks for your report, can you put the code into a PR so that I can merge it?

bonny commented 5 months ago

Hi @bonny

Thanks for your report, can you put the code into a PR so that I can merge it?

Sure, I will try to send a PR today.