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 if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
||
24 | class EE_Brewing_Regular extends EE_BASE { |
||
25 | |||
26 | /** |
||
27 | * EE_Brewing_Regular constructor. |
||
28 | */ |
||
29 | public function __construct() { |
||
30 | if ( defined( 'EE_CAFF_PATH' ) ) { |
||
31 | // activation |
||
32 | add_action( 'AHEE__EEH_Activation__initialize_db_content', array( $this, 'initialize_caf_db_content' ) ); |
||
33 | // load caff init |
||
34 | add_action( 'AHEE__EE_System__set_hooks_for_core', array( $this, 'caffeinated_init' ) ); |
||
35 | // remove the "powered by" credit link from receipts and invoices |
||
36 | add_filter( 'FHEE_EE_Html_messenger__add_powered_by_credit_link_to_receipt_and_invoice', '__return_false' ); |
||
37 | // add caffeinated modules |
||
38 | add_filter( |
||
39 | 'FHEE__EE_Config__register_modules__modules_to_register', |
||
40 | array( $this, 'caffeinated_modules_to_register' ) |
||
41 | ); |
||
42 | // load caff scripts |
||
43 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_caffeinated_scripts' ), 10 ); |
||
44 | add_filter( 'FHEE__EE_Registry__load_helper__helper_paths', array( $this, 'caf_helper_paths' ), 10 ); |
||
45 | add_filter( |
||
46 | 'FHEE__EE_Payment_Method_Manager__register_payment_methods__payment_methods_to_register', |
||
47 | array( $this, 'caf_payment_methods' ) |
||
48 | ); |
||
49 | // caffeinated constructed |
||
50 | do_action( 'AHEE__EE_Brewing_Regular__construct__complete' ); |
||
51 | //seeing how this is caf, which isn't put on WordPress.org, we can have affiliate links without a disclaimer |
||
52 | add_filter( 'FHEE__ee_show_affiliate_links', '__return_false' ); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | |||
57 | |||
58 | /** |
||
59 | * callback for the FHEE__EE_Registry__load_helper__helper_paths filter to add the caffeinated paths |
||
60 | * |
||
61 | * @param array $paths original helper paths array |
||
62 | * @return array new array of paths |
||
63 | */ |
||
64 | public function caf_helper_paths( $paths ) { |
||
65 | $paths[] = EE_CAF_CORE . 'helpers' . DS; |
||
66 | return $paths; |
||
67 | } |
||
68 | |||
69 | |||
70 | |||
71 | /** |
||
72 | * Upon brand-new activation, if this is a new activation of CAF, we want to add |
||
73 | * some global prices that will show off EE4's capabilities. However, if they're upgrading |
||
74 | * from 3.1, or simply EE4.x decaf, we assume they don't want us to suddenly introduce these extra prices. |
||
75 | * This action should only be called when EE 4.x.0.P is initially activated. |
||
76 | * Right now the only CAF content are these global prices. If there's more in the future, then |
||
77 | * we should probably create a caf file to contain it all instead just a function like this. |
||
78 | * Right now, we ASSUME the only price types in the system are default ones |
||
79 | * |
||
80 | * @global wpdb $wpdb |
||
81 | */ |
||
82 | function initialize_caf_db_content() { |
||
83 | global $wpdb; |
||
84 | //use same method of getting creator id as the version introducing the change |
||
85 | $default_creator_id = apply_filters( 'FHEE__EE_DMS_Core_4_5_0__get_default_creator_id', get_current_user_id() ); |
||
86 | $price_type_table = $wpdb->prefix . "esp_price_type"; |
||
87 | $price_table = $wpdb->prefix . "esp_price"; |
||
88 | if ( EEH_Activation::table_exists( $price_type_table ) ) { |
||
89 | $SQL = 'SELECT COUNT(PRT_ID) FROM ' . $price_type_table . ' WHERE PBT_ID=4';//include trashed price types |
||
90 | $tax_price_type_count = $wpdb->get_var( $SQL ); |
||
91 | if ( $tax_price_type_count <= 1 ) { |
||
92 | $wpdb->insert( |
||
93 | $price_type_table, |
||
94 | array( |
||
95 | 'PRT_name' => __( "Regional Tax", "event_espresso" ), |
||
96 | 'PBT_ID' => 4, |
||
97 | 'PRT_is_percent' => true, |
||
98 | 'PRT_order' => 60, |
||
99 | 'PRT_deleted' => false, |
||
100 | 'PRT_wp_user' => $default_creator_id, |
||
101 | ), |
||
102 | array( |
||
103 | '%s',//PRT_name |
||
104 | '%d',//PBT_id |
||
105 | '%d',//PRT_is_percent |
||
106 | '%d',//PRT_order |
||
107 | '%d',//PRT_deleted |
||
108 | '%d', //PRT_wp_user |
||
109 | ) |
||
110 | ); |
||
111 | //federal tax |
||
112 | $result = $wpdb->insert( |
||
113 | $price_type_table, |
||
114 | array( |
||
115 | 'PRT_name' => __( "Federal Tax", "event_espresso" ), |
||
116 | 'PBT_ID' => 4, |
||
117 | 'PRT_is_percent' => true, |
||
118 | 'PRT_order' => 70, |
||
119 | 'PRT_deleted' => false, |
||
120 | 'PRT_wp_user' => $default_creator_id, |
||
121 | ), |
||
122 | array( |
||
123 | '%s',//PRT_name |
||
124 | '%d',//PBT_id |
||
125 | '%d',//PRT_is_percent |
||
126 | '%d',//PRT_order |
||
127 | '%d',//PRT_deleted |
||
128 | '%d' //PRT_wp_user |
||
129 | ) |
||
130 | ); |
||
131 | if ( $result ) { |
||
132 | $wpdb->insert( |
||
133 | $price_table, |
||
134 | array( |
||
135 | 'PRT_ID' => $wpdb->insert_id, |
||
136 | 'PRC_amount' => 15.00, |
||
137 | 'PRC_name' => __( "Sales Tax", "event_espresso" ), |
||
138 | 'PRC_desc' => '', |
||
139 | 'PRC_is_default' => true, |
||
140 | 'PRC_overrides' => null, |
||
141 | 'PRC_deleted' => false, |
||
142 | 'PRC_order' => 50, |
||
143 | 'PRC_parent' => null, |
||
144 | 'PRC_wp_user' => $default_creator_id, |
||
145 | ), |
||
146 | array( |
||
147 | '%d',//PRT_id |
||
148 | '%f',//PRC_amount |
||
149 | '%s',//PRC_name |
||
150 | '%s',//PRC_desc |
||
151 | '%d',//PRC_is_default |
||
152 | '%d',//PRC_overrides |
||
153 | '%d',//PRC_deleted |
||
154 | '%d',//PRC_order |
||
155 | '%d',//PRC_parent |
||
156 | '%d' //PRC_wp_user |
||
157 | ) |
||
158 | ); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * caffeinated_modules_to_register |
||
168 | * |
||
169 | * @access public |
||
170 | * @param array $modules_to_register |
||
171 | * @return array |
||
172 | */ |
||
173 | public function caffeinated_modules_to_register( $modules_to_register = array() ) { |
||
174 | if ( is_readable( EE_CAFF_PATH . 'modules' ) ) { |
||
175 | $caffeinated_modules_to_register = glob( EE_CAFF_PATH . 'modules' . DS . '*', GLOB_ONLYDIR ); |
||
176 | if ( is_array( $caffeinated_modules_to_register ) && ! empty( $caffeinated_modules_to_register ) ) { |
||
177 | $modules_to_register = array_merge( $modules_to_register, $caffeinated_modules_to_register ); |
||
178 | } |
||
179 | } |
||
180 | return $modules_to_register; |
||
181 | } |
||
182 | |||
183 | |||
184 | |||
185 | public function caffeinated_init() { |
||
186 | // EE_Register_CPTs hooks |
||
187 | add_filter( 'FHEE__EE_Register_CPTs__get_taxonomies__taxonomies', array( $this, 'filter_taxonomies' ), 10 ); |
||
188 | add_filter( 'FHEE__EE_Register_CPTs__get_CPTs__cpts', array( $this, 'filter_cpts' ), 10 ); |
||
189 | add_filter( 'FHEE__EE_Admin__get_extra_nav_menu_pages_items', array( $this, 'nav_metabox_items' ), 10 ); |
||
190 | EE_Registry::instance()->load_file( EE_CAFF_PATH, 'EE_Caf_Messages', 'class', array(), false ); |
||
191 | // caffeinated_init__complete hook |
||
192 | do_action( 'AHEE__EE_Brewing_Regular__caffeinated_init__complete' ); |
||
193 | } |
||
194 | |||
195 | |||
196 | |||
197 | public function enqueue_caffeinated_scripts() { |
||
198 | // sound of crickets... |
||
199 | } |
||
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * callbacks below here |
||
205 | * |
||
206 | * @param array $taxonomy_array |
||
207 | * @return array |
||
208 | */ |
||
209 | public function filter_taxonomies( array $taxonomy_array ) { |
||
213 | |||
214 | |||
215 | |||
216 | /** |
||
217 | * @param array $cpt_array |
||
218 | * @return mixed |
||
219 | */ |
||
220 | public function filter_cpts( array $cpt_array ) { |
||
224 | |||
225 | |||
226 | |||
227 | /** |
||
228 | * @param array $menuitems |
||
229 | * @return array |
||
230 | */ |
||
231 | View Code Duplication | public function nav_metabox_items( array $menuitems ) { |
|
239 | |||
240 | |||
241 | |||
242 | /** |
||
243 | * Adds the payment methods in {event-espresso-core}/caffeinated/payment_methods |
||
244 | * |
||
245 | * @param array $payment_method_paths |
||
246 | * @return array values are folder paths to payment method folders |
||
247 | */ |
||
248 | public function caf_payment_methods( $payment_method_paths ) { |
||
253 | } |
||
254 | |||
258 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.