[wp-trac] [WordPress Trac] #20523: Disable autoloader when using class_exists()

WordPress Trac noreply at wordpress.org
Mon Sep 21 23:02:11 UTC 2015


#20523: Disable autoloader when using class_exists()
----------------------------------------+-----------------------------
 Reporter:  michael.heuberger           |       Owner:  wonderboymusic
     Type:  enhancement                 |      Status:  closed
 Priority:  normal                      |   Milestone:  4.4
Component:  Bootstrap/Load              |     Version:  3.3.1
 Severity:  normal                      |  Resolution:  fixed
 Keywords:  needs-patch good-first-bug  |     Focuses:
----------------------------------------+-----------------------------

Comment (by westonruter):

 @wonderboymusic This change to `wp_ajax_add_menu_item` broke a plugin. We
 had defined an autoloader for our plugin namespace and then added a filter
 like:

 {{{#!php
 add_filter( 'wp_edit_nav_menu_walker', function () {
     return __NAMESPACE__ . '\Walker_Nav_Menu_Custom_Fields';
 } );
 }}}

 Since the `class_exists()` now prevents autoloading, it then caused the
 class to not get loaded and the add-menu-item Ajax call would fail
 returning with `0` due to:

 {{{#!php
 if ( ! class_exists( $walker_class_name, false ) )
     wp_die( 0 );
 }}}

 Now, it's easy enough to change our plugin to explicitly `require_once`
 the class file in the filter:

 {{{#!php
 add_filter( 'wp_edit_nav_menu_walker', function () {
     require_once( __DIR__ ' . '/class-walker-nav-menu-custom-fields.php'
 );
     return __NAMESPACE__ . '\Walker_Nav_Menu_Custom_Fields';
 } );
 }}}

 But plugins that use autoloaders aren't going to expect to have to do
 this.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/20523#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list