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:
| 1 | <?php |
||
| 11 | class Helper { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Get a value formatted for end-users |
||
| 15 | * |
||
| 16 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 17 | * @param string $container_type Container type to search in |
||
| 18 | * @param string $container_id |
||
| 19 | * @param string $field_name Field name |
||
| 20 | * @return mixed |
||
| 21 | */ |
||
| 22 | public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get a value formatted for end-users |
||
| 44 | * |
||
| 45 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 46 | * @param string $container_type Container type to search in |
||
| 47 | * @param string $container_id |
||
| 48 | * @param string $field_name Field name |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | public static function get_value( $object_id, $container_type, $container_id, $field_name ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Set value for a field |
||
| 64 | * |
||
| 65 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 66 | * @param string $container_type Container type to search in |
||
| 67 | * @param string $container_id |
||
| 68 | * @param string $field_name Field name |
||
| 69 | * @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
||
| 70 | */ |
||
| 71 | public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Shorthand for get_post_meta(). |
||
| 85 | * Uses the ID of the current post in the loop. |
||
| 86 | * |
||
| 87 | * @param string $name Custom field name. |
||
| 88 | * @param string $container_id |
||
| 89 | * @return mixed Meta value. |
||
| 90 | */ |
||
| 91 | public static function get_the_post_meta( $name, $container_id = '' ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get post meta field for a post. |
||
| 97 | * |
||
| 98 | * @param int $id Post ID. |
||
| 99 | * @param string $name Custom field name. |
||
| 100 | * @param string $container_id |
||
| 101 | * @return mixed Meta value. |
||
| 102 | */ |
||
| 103 | public static function get_post_meta( $id, $name, $container_id = '' ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Set post meta field for a post. |
||
| 109 | * |
||
| 110 | * @param int $id Post ID |
||
| 111 | * @param string $name Custom field name |
||
| 112 | * @param array $value |
||
| 113 | * @param string $container_id |
||
| 114 | */ |
||
| 115 | public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get theme option field value. |
||
| 121 | * |
||
| 122 | * @param string $name Custom field name |
||
| 123 | * @param string $container_id |
||
| 124 | * @return mixed Option value |
||
| 125 | */ |
||
| 126 | public static function get_theme_option( $name, $container_id = '' ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set theme option field value. |
||
| 132 | * |
||
| 133 | * @param string $name Field name |
||
| 134 | * @param array $value |
||
| 135 | * @param string $container_id |
||
| 136 | */ |
||
| 137 | public static function set_theme_option( $name, $value, $container_id = '' ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get term meta field for a term. |
||
| 143 | * |
||
| 144 | * @param int $id Term ID. |
||
| 145 | * @param string $name Custom field name. |
||
| 146 | * @param string $container_id |
||
| 147 | * @return mixed Meta value. |
||
| 148 | */ |
||
| 149 | public static function get_term_meta( $id, $name, $container_id = '' ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set term meta field for a term. |
||
| 155 | * |
||
| 156 | * @param int $id Term ID |
||
| 157 | * @param string $name Field name |
||
| 158 | * @param array $value |
||
| 159 | * @param string $container_id |
||
| 160 | */ |
||
| 161 | public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get user meta field for a user. |
||
| 167 | * |
||
| 168 | * @param int $id User ID. |
||
| 169 | * @param string $name Custom field name. |
||
| 170 | * @param string $container_id |
||
| 171 | * @return mixed Meta value. |
||
| 172 | */ |
||
| 173 | public static function get_user_meta( $id, $name, $container_id = '' ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set user meta field for a user. |
||
| 179 | * |
||
| 180 | * @param int $id User ID |
||
| 181 | * @param string $name Field name |
||
| 182 | * @param array $value |
||
| 183 | * @param string $container_id |
||
| 184 | */ |
||
| 185 | public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get comment meta field for a comment. |
||
| 191 | * |
||
| 192 | * @param int $id Comment ID. |
||
| 193 | * @param string $name Custom field name. |
||
| 194 | * @param string $container_id |
||
| 195 | * @return mixed Meta value. |
||
| 196 | */ |
||
| 197 | public static function get_comment_meta( $id, $name, $container_id = '' ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set comment meta field for a comment. |
||
| 203 | * |
||
| 204 | * @param int $id Comment ID |
||
| 205 | * @param string $name Field name |
||
| 206 | * @param array $value |
||
| 207 | * @param string $container_id |
||
| 208 | */ |
||
| 209 | public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get nav menu item meta field for a nav menu item. |
||
| 215 | * |
||
| 216 | * @param int $id Nav menu item ID. |
||
| 217 | * @param string $name Custom field name. |
||
| 218 | * @param string $container_id |
||
| 219 | * @return mixed Meta value. |
||
| 220 | */ |
||
| 221 | public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set nav menu item meta field for a nav menu item. |
||
| 227 | * |
||
| 228 | * @param int $id Nav menu item ID |
||
| 229 | * @param string $name Field name |
||
| 230 | * @param array $value |
||
| 231 | * @param string $container_id |
||
| 232 | */ |
||
| 233 | public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Recursive sorting function by array key. |
||
| 239 | * |
||
| 240 | * @param array &$array The input array. |
||
| 241 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
| 242 | * @return boolean |
||
| 243 | */ |
||
| 244 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
| 257 | * |
||
| 258 | * @param array $array |
||
| 259 | * @param array<string> $allowed_relations |
||
| 260 | * @param string $relation_key |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Normalize a label by updating case, stripping common prefixes etc. |
||
| 282 | * |
||
| 283 | * @param string $label |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | View Code Duplication | public static function normalize_label( $label ) { |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Normalize a type string representing an object type |
||
| 301 | * |
||
| 302 | * @param string $type |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | View Code Duplication | public static function normalize_type( $type ) { |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Convert a string representing an object type to a fully qualified class name |
||
| 315 | * |
||
| 316 | * @param string $type |
||
| 317 | * @param string $namespace |
||
| 318 | * @param string $class_suffix |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Convert a string representing an object type to a fully qualified class name |
||
| 337 | * |
||
| 338 | * @param string $class |
||
| 339 | * @param string $class_suffix |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get an array of sanitized html classes |
||
| 357 | * |
||
| 358 | * @param string|array<string> $classes |
||
| 359 | * @return array<string> |
||
| 360 | */ |
||
| 361 | public static function sanitize_classes( $classes ) { |
||
| 368 | } |
||
| 369 |