WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#37469 closed defect (bug) (invalid)

WP_Post_Type class causes fatal error for themes/plugins that cast post objects as stdClass

Reported by: modernnerd Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.6
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

In 4.6beta1 and onwards, the new WP_Post_Type class is causing a fatal error in the Genesis theme framework on all admin pages if the site has an active custom post type:

[26-Jul-2016 12:00:47 UTC] PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Genesis_Admin_CPT_Archive_Settings::__construct() must be an instance of stdClass, instance of WP_Post_Type given, called in /Users/nickcernis/www/trunk/wp-content/themes/genesis/lib/admin/menu.php on line 122 and defined in /Users/nickcernis/www/trunk/wp-content/themes/genesis/lib/admin/cpt-archive-settings.php:38
Stack trace:
#0 /Users/nickcernis/www/trunk/wp-content/themes/genesis/lib/admin/menu.php(122): Genesis_Admin_CPT_Archive_Settings->__construct(Object(WP_Post_Type))
#1 /Users/nickcernis/www/trunk/wp-includes/plugin.php(524): genesis_add_cpt_archive_page('')
#2 /Users/nickcernis/www/trunk/wp-admin/includes/menu.php(149): do_action('admin_menu', '')
#3 /Users/nickcernis/www/trunk/wp-admin/menu.php(282): require_once('/Users/nickcern...')
#4 /Users/nickcernis/www/trunk/wp-admin/admin.php(138): require('/Users/nickcern...')
#5 /Users/nickcernis/www/trunk/wp-admin/plugins.php(10): require_once('/Users/nickcern...')
#6 {main}
  thrown in /Users/nickcernis/www/trunk/wp-content/themes/genesis/lib/admin/cpt-archive-settings.php on line 38

This is because get_post_type_object now returns an instance of WP_Post_Type rather than the stdClass that Genesis previously expected. From genesis/lib/admin/cpt-archive-settings.php line 38:

public function __construct( stdClass $post_type ) {

At present, this will cause all sites running Genesis that have a custom post type active to white screen on admin pages when WordPress 4.6 is released.

Steps to reproduce:

<ol>
<li>Install https://wordpress.org/plugins/wordpress-beta-tester/ and update to the latest nightly.
<li>Activate Genesis or the Genesis Sample child theme.
<li>Add some CPT boilerplate to functions.php: http://d.pr/n/LSYM+
<li>Visit any admin page, which produces the fatal error.
</ol>

I am happy to share a copy of Genesis for testing purposes with any contributor that would like to help to review this.

Reporting because — although this can be remedied in Genesis by not casting $post_type as an stdClass — this may also affect other themes and plugins that cast the type and do not expect that type to change. Am unsure if it is the responsibility of plugin/theme authors or WP core to fix this, so all advice is appreciated.

Change History (2)

#1 @ocean90
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hello @modernnerd, thanks for your report.

That part is actually type hinting, not a type cast.

stdClass isn't really an useful type hint. Type hinting should only be used for "real" classes. Closing as a wontfix because we already had such changes in the past, for example WP_Term [34997], WP_Comment [33891], or WP_Site [36393].

I suggest to contact the author of the theme and inform them about https://make.wordpress.org/core/2016/07/13/wp_post_type-in-4-6/.

#2 @modernnerd
4 years ago

Many thanks for the correction and quick update, @ocean90 – will pass this on.

Note: See TracTickets for help on using tickets.