devinsays/portfolio-post-type — pull request #36 by GaryJones (2014-01-05 21:00:19+00:00)
Title: Move adding of taxonomy term body classes to new PHP class.

The existing `add_body_classes()` method in the registration class was always in the wrong place - it's nothing to do with registration, and it doesn't need to be available in the admin. It needed to be moved out of there.

The new class started off as `Portfolio_Post_Type_Public` to mirror the existing `Portfolio_Post_Type_Admin` class, but for the front-end of a site. It contained the `init()` method containing the `add_filter()` call, and the original `add_body_classes()` method.

However, the `add_body_classes()` method could be broken down several times, so that each method really focused on doing only one task. I could break it down into a method for getting a single HTML class given a taxonomy and term slug, a method that got all of the terms given a taxonomy and optional post ID including handling any instance of `WP_Error`, a method that looped through to build the HTML classes array for all taxonomies, and a method that acted as the filter callback to merge that in with the supplied classes.

These methods now open themselves to easier unit testing, since they each take a finite set of arguments, and return an array or string without too much logic used during the processing. This breakdown and simplification enabled the class to be abstracted out from being a class in this Portfolio plugin, to something more generic that this plugin happens to use.