Make WordPress Core

Opened 3 years ago

Last modified 9 days ago

#44098 new defect (bug)

Widget classes when custom widget class is namespaced

Reported by: rogerlos Owned by:
Milestone: 5.8 Priority: normal
Severity: normal Version: 5.4.2
Component: Widgets Keywords: has-patch dev-feedback needs-testing needs-unit-tests
Focuses: Cc:


Widgets output from a namespaced class have a class added to the before_widget argument in a way which is potentially confusing and messy to work with.

The before_widget value within the arguments array sent to the widget() method within WP_Widget looks something like this when using a namespaced custom widget class:

<aside class="widget \myplugin\My_Widget" id="mywidgetname_widget-1">'

Given my understanding of WP's "normal" escaping of attributes, I would have expected myplugin-My_Widget, mywidgetname myplugin-My_Widget or maybe mypluginMy_Widget.

I believe that technically the two escape characters make the WP output the equivalent to the latter, but it's a bit messy and potentially confusing to folks looking to style the output, or find the aside using JS.

Attachments (1)

bug.png (46.1 KB) - added by Mte90 10 months ago.
Whan happens when the widget is a class initialized by a namespace without id_base defined

Download all attachments as: .zip

Change History (8)

#1 @welcher
2 years ago

  • Keywords reporter-feedback added

@rogerlos thanks for the ticket! Can you share the code you are using to register the widget? My local tests are not getting the same results.

#2 @Mte90
10 months ago

  • Keywords needs-patch added; reporter-feedback removed
  • Version set to 5.4.2

I can confirm the issue.
This happens when you don't define the id_base that is automatically generated.

// https://developer.wordpress.org/reference/classes/wp_widget/
// line 163
 $this->id_base         = empty( $id_base ) ? preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) ) : strtolower( $id_base );

So it use the class to generate it but if it is a namespace generated namespace\sub\sub\class doesn't do anything creating this issues.

10 months ago

Whan happens when the widget is a class initialized by a namespace without id_base defined

This ticket was mentioned in PR #435 on WordPress/wordpress-develop by Mte90.

10 months ago

  • Keywords has-patch added; needs-patch removed

As https://core.trac.wordpress.org/ticket/44098 this happens when the widget is initialized without defining the id_base that is auto generated. The problem is that doesn't handle namespaces so this can create warning by php because the preg_match include a string with a backslash \.

Trac ticket:

#4 @Mte90
10 months ago

  • Keywords dev-feedback needs-testing needs-unit-tests added

I did a first patch version, I added a test but require some review.

Last edited 10 months ago by Mte90 (previous) (diff)

#5 @prbot
10 months ago

Mte90 commented on PR #435:

This should fix the linting but for the test failing I don't know how I can mock a fake namespace and class.

#6 @SergeyBiryukov
2 months ago

  • Milestone changed from Awaiting Review to 5.8

This ticket was mentioned in Slack in #core by mte90. View the logs.

9 days ago

Note: See TracTickets for help on using tickets.