Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_CPT_Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EE_CPT_Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
||
| 31 | abstract class EE_CPT_Base extends EE_Soft_Delete_Base_Class { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * This is a property for holding cached feature images on CPT objects. Cache's are set on the first "feature_image()" method call. Each key in the array corresponds to the requested size. |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $_feature_image = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var WP_Post the WP_Post that corresponds with this CPT model object |
||
| 41 | */ |
||
| 42 | protected $_wp_post; |
||
| 43 | |||
| 44 | |||
| 45 | abstract public function wp_user(); |
||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Returns the WP post associated with this CPT model object. If this CPT is saved, fetches it |
||
| 51 | * from the DB. Otherwise, create an unsaved WP_POst object. Caches the post internally. |
||
| 52 | * @return WP_Post |
||
| 53 | */ |
||
| 54 | public function wp_post(){ |
||
| 85 | |||
| 86 | /** |
||
| 87 | * When fetching a new value for a post field that uses the global $post for rendering, |
||
| 88 | * set the global $post temporarily to be this model object; and afterwards restore it |
||
| 89 | * @param string $fieldname |
||
| 90 | * @param bool $pretty |
||
| 91 | * @param string $extra_cache_ref |
||
| 92 | * @return mixed |
||
| 93 | */ |
||
| 94 | protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Adds to the specified event category. If it category doesn't exist, creates it. |
||
| 119 | * @param string $category_name |
||
| 120 | * @param string $category_description optional |
||
| 121 | * @param int $parent_term_taxonomy_id optional |
||
| 122 | * @return EE_Term_Taxonomy |
||
| 123 | */ |
||
| 124 | function add_event_category( $category_name, $category_description = NULL, $parent_term_taxonomy_id = NULL ) { |
||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Removes the event category by specified name from being related ot this event |
||
| 132 | * @param string $category_name |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | function remove_event_category( $category_name ) { |
||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * Removes the relation to the specified term taxonomy, and maintains the |
||
| 143 | * data integrity of the term taxonomy provided |
||
| 144 | * @param EE_Term_Taxonomy $term_taxonomy |
||
| 145 | * @return EE_Base_Class the relation was removed from |
||
| 146 | */ |
||
| 147 | function remove_relation_to_term_taxonomy( $term_taxonomy ) { |
||
| 148 | View Code Duplication | if ( !$term_taxonomy ) { |
|
| 149 | EE_Error::add_error( sprintf( __( "No Term_Taxonomy provided which to remove from model object of type %s and id %d", "event_espresso" ), get_class( $this ), $this->ID() ), __FILE__, __FUNCTION__, __LINE__ ); |
||
| 150 | return NULL; |
||
| 151 | } |
||
| 152 | $term_taxonomy->set_count( $term_taxonomy->count() - 1 ); |
||
| 153 | $term_taxonomy->save(); |
||
| 154 | return $this->_remove_relation_to( $term_taxonomy, 'Term_Taxonomy' ); |
||
| 155 | } |
||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * The main purpose of this method is to return the post type for the model object |
||
| 161 | * |
||
| 162 | * @access public |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function post_type() { |
||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * The main purpose of this method is to return the parent for the model object |
||
| 173 | * |
||
| 174 | * @access public |
||
| 175 | * @return int |
||
| 176 | */ |
||
| 177 | public function parent() { |
||
| 180 | |||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * return the _status property |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function status() { |
||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $status |
||
| 195 | */ |
||
| 196 | public function set_status( $status ) { |
||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * This calls the equivalent model method for retrieving the feature image which in turn is a wrapper for WordPress' get_the_post_thumbnail() function. |
||
| 204 | * |
||
| 205 | * @link http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail |
||
| 206 | * @access protected |
||
| 207 | * @param string|array $size (optional) Image size. Defaults to 'post-thumbnail' but can also be a 2-item array representing width and height in pixels (i.e. array(32,32) ). |
||
| 208 | * @param string|array $attr Optional. Query string or array of attributes. |
||
| 209 | * @return string HTML image element |
||
| 210 | */ |
||
| 211 | protected function _get_feature_image( $size, $attr ) { |
||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * See _get_feature_image. Returns the HTML to display a featured image |
||
| 223 | * @param string $size |
||
| 224 | * @param string|array $attr |
||
| 225 | * @return string of html |
||
| 226 | */ |
||
| 227 | public function feature_image( $size = 'thumbnail', $attr = '' ) { |
||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * This uses the wp "wp_get_attachment_image_src()" function to return the feature image for the current class using the given size params. |
||
| 235 | * @param string|array $size can either be a string: 'thumbnail', 'medium', 'large', 'full' OR 2-item array representing width and height in pixels eg. array(32,32). |
||
| 236 | * @return string|boolean the url of the image or false if not found |
||
| 237 | */ |
||
| 238 | public function feature_image_url( $size = 'thumbnail' ) { |
||
| 242 | |||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * This is a method for restoring this_obj using details from the given $revision_id |
||
| 247 | * |
||
| 248 | * @param int $revision_id ID of the revision we're getting data from |
||
| 249 | * @param array $related_obj_names if included this will be used to restore for related obj |
||
| 250 | * if not included then we just do restore on the meta. |
||
| 251 | * We will accept an array of related_obj_names for restoration here. |
||
| 252 | * @param array $where_query You can optionally include an array of key=>value pairs |
||
| 253 | * that allow you to further constrict the relation to being added. |
||
| 254 | * However, keep in mind that the columns (keys) given |
||
| 255 | * must match a column on the JOIN table and currently |
||
| 256 | * only the HABTM models accept these additional conditions. |
||
| 257 | * Also remember that if an exact match isn't found for these extra cols/val pairs, |
||
| 258 | * then a NEW row is created in the join table. |
||
| 259 | * This array is INDEXED by RELATED OBJ NAME (so it corresponds with the obj_names sent); |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function restore_revision( $revision_id, $related_obj_names = array(), $where_query = array() ) { |
||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Wrapper for get_post_meta, http://codex.wordpress.org/Function_Reference/get_post_meta |
||
| 301 | * @param string $meta_key |
||
| 302 | * @param boolean $single |
||
| 303 | * @return mixed <ul><li>If only $id is set it will return all meta values in an associative array.</li> |
||
| 304 | * <li>If $single is set to false, or left blank, the function returns an array containing all values of the specified key.</li> |
||
| 305 | * <li>If $single is set to true, the function returns the first value of the specified key (not in an array</li></ul> |
||
| 306 | */ |
||
| 307 | public function get_post_meta( $meta_key = NULL, $single = FALSE ) { |
||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Wrapper for update_post_meta, http://codex.wordpress.org/Function_Reference/update_post_meta |
||
| 315 | * @param string $meta_key |
||
| 316 | * @param mixed $meta_value |
||
| 317 | * @param mixed $prev_value |
||
| 318 | * @return mixed Returns meta_id if the meta doesn't exist, otherwise returns true on success and false on failure. NOTE: If the meta_value passed to this function is the same as the value that is already in the database, this function returns false. |
||
| 319 | */ |
||
| 320 | public function update_post_meta( $meta_key, $meta_value, $prev_value = NULL ) { |
||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Wrapper for add_post_meta, http://codex.wordpress.org/Function_Reference/add_post_meta |
||
| 331 | * @param mixed $meta_key |
||
| 332 | * @param mixed $meta_value |
||
| 333 | * @param bool $unique . If postmeta for this $meta_key already exists, whether to add an additional item or not |
||
| 334 | * @return boolean Boolean true, except if the $unique argument was set to true and a custom field with the given key already exists, in which case false is returned. |
||
| 335 | */ |
||
| 336 | public function add_post_meta( $meta_key, $meta_value, $unique = FALSE ) { |
||
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * Wrapper for delete_post_meta, http://codex.wordpress.org/Function_Reference/delete_post_meta |
||
| 347 | * |
||
| 348 | * @param mixed $meta_key |
||
| 349 | * @param mixed $meta_value |
||
| 350 | * @return boolean False for failure. True for success. |
||
| 351 | */ |
||
| 352 | public function delete_post_meta( $meta_key, $meta_value = '' ) { |
||
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Gets the URL for viewing this event on the front-end |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function get_permalink() { |
||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Gets all the term-taxonomies for this CPT |
||
| 375 | * @param array $query_params |
||
| 376 | * @return EE_Term_Taxonomy |
||
| 377 | */ |
||
| 378 | public function term_taxonomies( $query_params = array() ) { |
||
| 381 | |||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | public function get_custom_post_statuses() { |
||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * @return mixed |
||
| 395 | */ |
||
| 396 | public function get_all_post_statuses() { |
||
| 399 | |||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * Don't serialize the WP Post. That's just duplicate data and we want to avoid recursion |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function __sleep() |
||
| 412 | } |
||
| 413 |