Complex classes like GravityView_Entry_Approval 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 GravityView_Entry_Approval, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class GravityView_Entry_Approval { |
||
24 | |||
25 | /** |
||
26 | * @var string Key used to store approval status in the Gravity Forms entry meta table |
||
27 | */ |
||
28 | const meta_key = 'is_approved'; |
||
29 | |||
30 | public function __construct() { |
||
33 | |||
34 | /** |
||
35 | * Add actions and filters related to entry approval |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | private function add_hooks() { |
||
51 | |||
52 | /** |
||
53 | * Get the approval status for an entry |
||
54 | * |
||
55 | * @since 1.18 |
||
56 | * @uses GVCommon::get_entry_id() Accepts entry slug or entry ID |
||
57 | * |
||
58 | * @param array|int|string $entry Entry array, entry slug, or entry ID |
||
59 | * @param string $value_or_label "value" or "label" (default: "label") |
||
60 | * |
||
61 | * @return bool|string Return the label or value of entry approval |
||
62 | */ |
||
63 | 1 | public static function get_entry_status( $entry, $value_or_label = 'label' ) { |
|
64 | |||
65 | 1 | $entry_id = is_array( $entry ) ? $entry['id'] : GVCommon::get_entry_id( $entry, true ); |
|
66 | |||
67 | 1 | $status = gform_get_meta( $entry_id, self::meta_key ); |
|
68 | |||
69 | 1 | $status = GravityView_Entry_Approval_Status::maybe_convert_status( $status ); |
|
70 | |||
71 | 1 | if( 'value' === $value_or_label ) { |
|
72 | 1 | return $status; |
|
73 | } |
||
74 | |||
75 | return GravityView_Entry_Approval_Status::get_label( $status ); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Approve/Disapprove entries using the × or ✓ icons in the GF Entries screen |
||
80 | * |
||
81 | * @uses wp_send_json_error() |
||
82 | * @uses wp_send_json_success() |
||
83 | * |
||
84 | * Expects a $_POST request with the following $_POST keys and values: |
||
85 | * |
||
86 | * @global array $_POST { |
||
87 | * @type int $form_id ID of the form connected to the entry being updated |
||
88 | * @type string|int $entry_slug The ID or slug of the entry being updated |
||
89 | * @type string $approved The value of the entry approval status {@see GravityView_Entry_Approval_Status::is_valid() } |
||
90 | * } |
||
91 | * |
||
92 | * @return void Prints result using wp_send_json_success() and wp_send_json_error() |
||
93 | */ |
||
94 | public function ajax_update_approved() { |
||
163 | |||
164 | /** |
||
165 | * Update the is_approved meta whenever the entry is submitted (and it contains a User Opt-in field) |
||
166 | * |
||
167 | * @since 1.16.6 |
||
168 | * |
||
169 | * @param $entry array Gravity Forms entry object |
||
170 | * @param $form array Gravity Forms form object |
||
171 | */ |
||
172 | public function after_submission( $entry, $form ) { |
||
175 | |||
176 | /** |
||
177 | * Update the is_approved meta whenever the entry is updated |
||
178 | * |
||
179 | * @since 1.7.6.1 Was previously named `update_approved_meta` |
||
180 | * |
||
181 | * @param array $form Gravity Forms form array |
||
182 | * @param int $entry_id ID of the Gravity Forms entry |
||
183 | * @return void |
||
184 | */ |
||
185 | 3 | public function after_update_entry_update_approved_meta( $form, $entry_id = NULL ) { |
|
186 | |||
187 | 3 | $approved_column = self::get_approved_column( $form['id'] ); |
|
188 | |||
189 | /** |
||
190 | * If the form doesn't contain the approve field, don't assume anything. |
||
191 | */ |
||
192 | 3 | if( empty( $approved_column ) ) { |
|
193 | 3 | return; |
|
194 | } |
||
195 | |||
196 | $entry = GFAPI::get_entry( $entry_id ); |
||
197 | |||
198 | // If the checkbox is blank, it's disapproved, regardless of the label |
||
199 | if ( '' === \GV\Utils::get( $entry, $approved_column ) ) { |
||
200 | $value = GravityView_Entry_Approval_Status::DISAPPROVED; |
||
201 | } else { |
||
202 | // If the checkbox is not blank, it's approved |
||
203 | $value = GravityView_Entry_Approval_Status::APPROVED; |
||
204 | } |
||
205 | |||
206 | self::update_approved_meta( $entry_id, $value, $form['id'] ); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Process a bulk of entries to update the approve field/property |
||
211 | * |
||
212 | * @since 1.18 Moved to GravityView_Entry_Approval |
||
213 | * @since 1.18 Made public |
||
214 | * |
||
215 | * @access public |
||
216 | * @static |
||
217 | * @param array|boolean $entries If array, array of entry IDs that are to be updated. If true: update all entries. |
||
218 | * @param int $approved Approved status. If `0`: unapproved, if not empty, `Approved` |
||
219 | * @param int $form_id The Gravity Forms Form ID |
||
220 | * @return boolean|null True: successfully updated all entries. False: there was an error updating at least one entry. NULL: an error occurred (see log) |
||
221 | */ |
||
222 | 1 | public static function update_bulk( $entries = array(), $approved, $form_id ) { |
|
254 | |||
255 | /** |
||
256 | * update_approved function. |
||
257 | * |
||
258 | * @since 1.18 Moved to GravityView_Entry_Approval class |
||
259 | * |
||
260 | * @access public |
||
261 | * @static |
||
262 | * @param int $entry_id (default: 0) |
||
263 | * @param int $approved (default: 2) |
||
264 | * @param int $form_id (default: 0) |
||
265 | * @param int $approvedcolumn (default: 0) |
||
266 | * |
||
267 | * @return boolean True: It worked; False: it failed |
||
268 | */ |
||
269 | 1 | public static function update_approved( $entry_id = 0, $approved = 2, $form_id = 0, $approvedcolumn = 0 ) { |
|
321 | |||
322 | /** |
||
323 | * Add a note when an entry is approved |
||
324 | * |
||
325 | * @see GravityView_Entry_Approval::update_approved |
||
326 | * |
||
327 | * @since 1.18 |
||
328 | * |
||
329 | * @param int $entry_id Gravity Forms entry ID |
||
330 | * @param int $approved Approval status |
||
331 | * |
||
332 | * @return false|int|WP_Error Note ID if successful; WP_Error if error when adding note, FALSE if note not updated because of `gravityview/approve_entries/add-note` filter or `GravityView_Entry_Notes` class not existing |
||
333 | */ |
||
334 | 1 | private static function add_approval_status_updated_note( $entry_id, $approved = 0 ) { |
|
335 | 1 | $note = ''; |
|
336 | |||
337 | 1 | switch ( $approved ) { |
|
338 | case GravityView_Entry_Approval_Status::APPROVED: |
||
339 | 1 | $note = __( 'Approved the Entry for GravityView', 'gravityview' ); |
|
340 | 1 | break; |
|
341 | case GravityView_Entry_Approval_Status::UNAPPROVED: |
||
342 | $note = __( 'Reset Entry approval for GravityView', 'gravityview' ); |
||
343 | break; |
||
344 | case GravityView_Entry_Approval_Status::DISAPPROVED: |
||
345 | $note = __( 'Disapproved the Entry for GravityView', 'gravityview' ); |
||
346 | break; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * @filter `gravityview/approve_entries/add-note` Add a note when the entry has been approved or disapproved? |
||
351 | * @since 1.16.3 |
||
352 | * @param bool $add_note True: Yep, add that note! False: Do not, under any circumstances, add that note! |
||
353 | */ |
||
354 | 1 | $add_note = apply_filters( 'gravityview/approve_entries/add-note', true ); |
|
355 | |||
356 | 1 | $note_id = false; |
|
357 | |||
358 | 1 | if( $add_note && class_exists( 'GravityView_Entry_Notes' ) ) { |
|
359 | |||
360 | 1 | $current_user = wp_get_current_user(); |
|
361 | |||
362 | 1 | $note_id = GravityView_Entry_Notes::add_note( $entry_id, $current_user->ID, $current_user->display_name, $note ); |
|
363 | } |
||
364 | |||
365 | 1 | return $note_id; |
|
366 | } |
||
367 | |||
368 | /** |
||
369 | * Update the Approve/Disapproved field value |
||
370 | * |
||
371 | * @param int $entry_id ID of the Gravity Forms entry |
||
372 | * @param string $status String whether entry is approved or not. `0` for not approved, `Approved` for approved. |
||
373 | * @param int $form_id ID of the form of the entry being updated. Improves query performance. |
||
374 | * @param string $approvedcolumn Gravity Forms Field ID |
||
375 | * |
||
376 | * @return true|WP_Error |
||
377 | */ |
||
378 | private static function update_approved_column( $entry_id = 0, $status = '0', $form_id = 0, $approvedcolumn = 0 ) { |
||
419 | |||
420 | /** |
||
421 | * Get the value for the approved field checkbox |
||
422 | * |
||
423 | * When approving a field via the entry meta, use the correct value for the new approved column input |
||
424 | * |
||
425 | * @since 1.19 |
||
426 | * |
||
427 | * @param array|int $form Form ID or form array |
||
428 | * @param string $approved_column Approved column field ID |
||
429 | * |
||
430 | * @return string|null |
||
431 | */ |
||
432 | private static function get_approved_column_input_label( $form, $approved_column ) { |
||
449 | |||
450 | /** |
||
451 | * Update the `is_approved` entry meta value |
||
452 | * |
||
453 | * @since 1.7.6.1 `after_update_entry_update_approved_meta` was previously to be named `update_approved_meta` |
||
454 | * @since 1.17.1 Added $form_id parameter |
||
455 | * |
||
456 | * @param int $entry_id ID of the Gravity Forms entry |
||
457 | * @param string $status String whether entry is approved or not. `0` for not approved, `Approved` for approved. |
||
458 | * @param int $form_id ID of the form of the entry being updated. Improves query performance. |
||
459 | * |
||
460 | * @return void |
||
461 | */ |
||
462 | private static function update_approved_meta( $entry_id, $status, $form_id = 0 ) { |
||
503 | |||
504 | /** |
||
505 | * Calculate the approve field.input id |
||
506 | * |
||
507 | * @access public |
||
508 | * @static |
||
509 | * @param mixed $form GF Form or Form ID |
||
510 | * @return false|null|string Returns the input ID of the approved field. Returns NULL if no approved fields were found. Returns false if $form_id wasn't set. |
||
511 | */ |
||
512 | 3 | static public function get_approved_column( $form ) { |
|
552 | |||
553 | } |
||
554 | |||
555 | new GravityView_Entry_Approval; |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.