[wp-trac] [WordPress Trac] #38923: Introduce `WP_Action_Handler` to clean up admin action mess

WordPress Trac noreply at wordpress.org
Thu Nov 24 01:08:36 UTC 2016


#38923: Introduce `WP_Action_Handler` to clean up admin action mess
----------------------------+-----------------------------
 Reporter:  flixos90        |      Owner:
     Type:  enhancement     |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Administration  |    Version:
 Severity:  normal          |   Keywords:  needs-patch
  Focuses:                  |
----------------------------+-----------------------------
 We all know that several parts of the admin code are messy in a way. After
 we had to take several looks at these files during today's multisite chat,
 I thought we need to do something about it.

 My proposal for now is to introduce a new base class `WP_Action_Handler`.
 This abstract class would lay a foundation to handle any kind of actions
 on any admin screen. Currently there's a (sometimes huge) switch clause at
 the beginning of almost every page. By removing these clauses entirely and
 handling the logic in a dedicated class for this instead, we get the
 following benefits:

 * easier readable code
 * less huge files
 * possibility to add unit tests for each of the switch cases
 * possibility to add unit tests the entire handling of an action
 * easy addition of any custom actions in plugins, beyond just bulk actions
 (which have become easier to implement thanks to 4.7)

 I have been experimenting with such a class over the past few hours and
 will upload a first patch below. Some notes:
 * The class usage is almost similar to all the list table classes: There's
 a base class, and then there's (or rather there will be) one class (which
 extends the base class) for each screen that could benefit from that.
 * Many of the actions have similarities: They mostly use
 `check_admin_referer()` at the beginning, followed by some capability and
 other permission checks to verify the user is allowed to perform the
 action. Therefore the base class should be able to handle nonces on its
 own. The capability checks should be separated from the actual action
 logic for better organization and easier testing.
 * Many of the actions redirect back to the admin page, with a GET
 parameter denoting a notification message to show to the user.
 `WP_Action_Handler` should also be capable of managing such messages to
 make that easier as well. This will furthermore allow to remove a bunch of
 code from the admin screen files where they "bootstrap" their supported
 messages.

 More notes will follow on the initial patch.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/38923>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list