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 |
||
| 27 | class Give_Roles { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Class Constructor |
||
| 31 | * |
||
| 32 | * Set up the Give Roles Class. |
||
| 33 | * |
||
| 34 | * @since 1.0 |
||
| 35 | * @access public |
||
| 36 | */ |
||
| 37 | public function __construct() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Add Roles |
||
| 43 | * |
||
| 44 | * Add new shop roles with default WordPress capabilities. |
||
| 45 | * |
||
| 46 | * @since 1.0 |
||
| 47 | * @access public |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | public function add_roles() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Add Capabilities |
||
| 101 | * |
||
| 102 | * Add new shop-specific capabilities. |
||
| 103 | * |
||
| 104 | * @since 1.0 |
||
| 105 | * @access public |
||
| 106 | * |
||
| 107 | * @global WP_Roles $wp_roles |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function add_caps() { |
|
| 112 | global $wp_roles; |
||
| 113 | |||
| 114 | if ( class_exists( 'WP_Roles' ) ) { |
||
| 115 | if ( ! isset( $wp_roles ) ) { |
||
| 116 | $wp_roles = new WP_Roles(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | if ( is_object( $wp_roles ) ) { |
||
| 121 | $wp_roles->add_cap( 'give_manager', 'view_give_reports' ); |
||
| 122 | $wp_roles->add_cap( 'give_manager', 'view_give_sensitive_data' ); |
||
| 123 | $wp_roles->add_cap( 'give_manager', 'export_give_reports' ); |
||
| 124 | $wp_roles->add_cap( 'give_manager', 'manage_give_settings' ); |
||
| 125 | |||
| 126 | $wp_roles->add_cap( 'administrator', 'view_give_reports' ); |
||
| 127 | $wp_roles->add_cap( 'administrator', 'view_give_sensitive_data' ); |
||
| 128 | $wp_roles->add_cap( 'administrator', 'export_give_reports' ); |
||
| 129 | $wp_roles->add_cap( 'administrator', 'manage_give_settings' ); |
||
| 130 | |||
| 131 | // Add the main post type capabilities. |
||
| 132 | $capabilities = $this->get_core_caps(); |
||
| 133 | foreach ( $capabilities as $cap_group ) { |
||
| 134 | foreach ( $cap_group as $cap ) { |
||
| 135 | $wp_roles->add_cap( 'administrator', $cap ); |
||
| 136 | $wp_roles->add_cap( 'give_manager', $cap ); |
||
| 137 | $wp_roles->add_cap( 'give_worker', $cap ); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | $wp_roles->add_cap( 'give_accountant', 'edit_give_forms' ); |
||
| 142 | $wp_roles->add_cap( 'give_accountant', 'read_private_give_forms' ); |
||
| 143 | $wp_roles->add_cap( 'give_accountant', 'view_give_reports' ); |
||
| 144 | $wp_roles->add_cap( 'give_accountant', 'export_give_reports' ); |
||
| 145 | $wp_roles->add_cap( 'give_accountant', 'edit_give_payments' ); |
||
| 146 | |||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get Core Capabilities |
||
| 152 | * |
||
| 153 | * Retrieve core post type capabilities. |
||
| 154 | * |
||
| 155 | * @since 1.0 |
||
| 156 | * @access public |
||
| 157 | * |
||
| 158 | * @return array $capabilities Core post type capabilities. |
||
| 159 | */ |
||
| 160 | public function get_core_caps() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Meta Capabilities |
||
| 199 | * |
||
| 200 | * Map meta capabilities to primitive capabilities. |
||
| 201 | * |
||
| 202 | * @since 1.0 |
||
| 203 | * @access public |
||
| 204 | * |
||
| 205 | * @param array $caps Returns the user's actual capabilities. |
||
| 206 | * @param string $cap Capability name. |
||
| 207 | * @param int $user_id The user ID. |
||
| 208 | * @param array $args Adds the context to the cap. Typically the object ID. |
||
| 209 | * |
||
| 210 | * @return array $caps Meta capabilities. |
||
| 211 | */ |
||
| 212 | public function meta_caps( $caps, $cap, $user_id, $args ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Remove Capabilities |
||
| 240 | * |
||
| 241 | * Remove core post type capabilities (called on uninstall). |
||
| 242 | * |
||
| 243 | * @since 1.0 |
||
| 244 | * @access public |
||
| 245 | * |
||
| 246 | * @global WP_Roles $wp_roles |
||
| 247 | * |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | View Code Duplication | public function remove_caps() { |
|
| 292 | |||
| 293 | } |
||
| 294 |