Make WordPress Core

Opened 6 years ago

Last modified 19 months ago

#42455 new enhancement

WordPress Class methods and Single Responsibility (recent posts widgets)

Reported by: viewup's profile viewup Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.8
Component: Widgets Keywords: 2nd-opinion
Focuses: docs, performance Cc:

Description

WordPress uses PHP Classes a lot and this is great for extending and improving. The problem is, many class methods does a lot of things at once and this doesn't help extending at all.

For instance, I'm trying to extend the Recent Posts Widget (WP_Widget_Recent_Posts).

The plugin has it's internal settings and logic, query posts and apply filters.

I want to change it's render method, but just that. Don't want to mess with the plugin logic.

The problem is, the render method (widget method) does a lot of things instead of focusing only on rendering. This forces me to copy every logical actions and reproduce them on my extending class.

This could be solved by just splitting the plugin logic and rendering in separated functions (an function to get the posts and filters, separated from the widget function).

This would improve a lot the WordPress extending by plugins and themes. If we apply the single responsibility principle on WordPress classes and functions, plugins wouldn't need to have much more code.

Another point: this enhancement wouldn't impact old plugins/themes if the functions signatures keep the same.

Change History (2)

#1 @kitchin
19 months ago

  • Component changed from Plugins to Widgets
  • Keywords 2nd-opinion added
  • Version changed from 4.9 to 4.9.8

Makes sense. The code before rendering is only 23 lines, but could be split to a helper function or short-circuit action.

I can't change the Version to trunk (UI bug? Permissions?), but diffing 4.9 to trunk shows only one effective change:
$format = apply_filters( 'navigation_widgets_format', $format );
and the related html5 rendering. The filter is used in 9 widgets.

#2 @kitchin
19 months ago

Since the formatting changes sometimes, such as #54054, overriding would be nice. Better than a js hack.

Note: See TracTickets for help on using tickets.