Complex classes like GravityView_Field_Notes 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_Field_Notes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class GravityView_Field_Notes extends GravityView_Field { |
||
18 | |||
19 | /** |
||
20 | * @var string Current __FILE__ |
||
21 | * @since 1.17 |
||
22 | */ |
||
23 | static $file; |
||
24 | |||
25 | /** |
||
26 | * @var string plugin_dir_path() of the current field file |
||
27 | * @since 1.17 |
||
28 | */ |
||
29 | static $path; |
||
30 | |||
31 | /** |
||
32 | * @var bool Are we doing an AJAX request? |
||
33 | * @since 1.17 |
||
34 | */ |
||
35 | private $doing_ajax = false; |
||
36 | |||
37 | /** |
||
38 | * The name of the GravityView field type |
||
39 | * @var string |
||
40 | */ |
||
41 | var $name = 'notes'; |
||
42 | |||
43 | function __construct() { |
||
54 | |||
55 | /** |
||
56 | * Add AJAX hooks, [gv_note_add] shortcode, and template loading paths |
||
57 | * |
||
58 | * @since 1.17 |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | private function add_hooks() { |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Add Entry Notes to the Add Field picker in Edit View |
||
86 | * |
||
87 | * @see GravityView_Admin_Views::get_entry_default_fields() |
||
88 | * |
||
89 | * @since 1.17 |
||
90 | * |
||
91 | * @param array $entry_default_fields Fields configured to show in the picker |
||
92 | * @param array $form Gravity Forms form array |
||
93 | * @param string $zone Current context: `directory`, `single`, `edit` |
||
94 | * |
||
95 | * @return array Fields array with notes added, if in Multiple Entries or Single Entry context |
||
96 | */ |
||
97 | public function add_entry_default_field( $entry_default_fields, $form, $zone ) { |
||
109 | |||
110 | /** |
||
111 | * Register scripts and styles used by the Notes field |
||
112 | * |
||
113 | * @since 1.17 |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function register_scripts() { |
||
118 | $css_file = gravityview_css_url( 'entry-notes.css', GravityView_Field_Notes::$path . 'assets/css/' ); |
||
119 | wp_register_style( 'gravityview-notes', $css_file, array(), GravityView_Plugin::version ); |
||
120 | wp_register_script( 'gravityview-notes', plugins_url( '/assets/js/entry-notes.js', GravityView_Field_Notes::$file ), array( 'jquery' ), GravityView_Plugin::version, true ); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Enqueue, localize field scripts and styles |
||
125 | * |
||
126 | * @since 1.17 |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | public function enqueue_scripts() { |
||
131 | global $wp_actions; |
||
132 | |||
133 | if( ! wp_script_is( 'gravityview-notes', 'enqueued' ) ) { |
||
134 | wp_enqueue_style( 'gravityview-notes' ); |
||
135 | wp_enqueue_script( 'gravityview-notes' ); |
||
136 | } |
||
137 | |||
138 | if( ! wp_script_is( 'gravityview-notes', 'done' ) ) { |
||
139 | |||
140 | $strings = self::strings(); |
||
141 | |||
142 | wp_localize_script( 'gravityview-notes', 'GVNotes', array( |
||
143 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
||
144 | 'text' => array( |
||
145 | 'processing' => $strings['processing'], |
||
146 | 'delete_confirm' => $strings['delete-confirm'], |
||
147 | 'note_added' => $strings['added-note'], |
||
148 | 'error_invalid' => $strings['error-invalid'], |
||
149 | 'error_empty_note' => $strings['error-empty-note'], |
||
150 | ), |
||
151 | ) ); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Verify permissions, check if $_POST is set and as expected. If so, use process_add_note |
||
157 | * |
||
158 | * @since 1.17 |
||
159 | * |
||
160 | * @see process_add_note |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | function maybe_add_note() { |
||
184 | |||
185 | /** |
||
186 | * Handle adding a note. |
||
187 | * |
||
188 | * Verify the request. If valid, add the note. If AJAX request, send response JSON. |
||
189 | * |
||
190 | * @since 1.17 |
||
191 | * |
||
192 | * @var array $data { |
||
193 | * @type string $action "gv_note_add" |
||
194 | * @type string $entry-slug Entry slug or ID to add note to |
||
195 | * @type string $gv_note_add Nonce with action "gv_note_add_{entry slug}" and name "gv_note_add" |
||
196 | * @type string $_wp_http_referer Relative URL to submitting page ('/view/example/entry/123/') |
||
197 | * @type string $gv-note-content Note content |
||
198 | * @type string $add_note Submit button value ('Add Note') |
||
199 | * } |
||
200 | * |
||
201 | * @return void |
||
202 | */ |
||
203 | private function process_add_note( $data ) { |
||
265 | |||
266 | /** |
||
267 | * Possibly delete notes, if request is proper. |
||
268 | * |
||
269 | * Verify permissions. Check expected $_POST. Parse args, then send to process_delete_notes |
||
270 | * |
||
271 | * @since 1.17 |
||
272 | * |
||
273 | * @see process_delete_notes |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | function maybe_delete_notes() { |
||
302 | |||
303 | /** |
||
304 | * Handle deleting notes |
||
305 | * |
||
306 | * @var array $data { |
||
307 | * @type string $action "gv_delete_notes" |
||
308 | * @type string $entry-slug Entry slug or ID to add note to |
||
309 | * @type string $gv_delete_notes Nonce with action "gv_delete_notes_{entry slug}" and name "gv_delete_notes" |
||
310 | * @type string $_wp_http_referer Relative URL to submitting page ('/view/example/entry/123/') |
||
311 | * @type int[] $note Array of Note IDs to be deleted |
||
312 | * } |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | function process_delete_notes( $data ) { |
||
342 | |||
343 | /** |
||
344 | * Include this extension templates path |
||
345 | * |
||
346 | * @since 1.17 |
||
347 | * |
||
348 | * @param array $file_paths List of template paths ordered |
||
349 | * |
||
350 | * @return array File paths with `./` and `./partials/` paths added |
||
351 | */ |
||
352 | public function add_template_path( $file_paths ) { |
||
359 | |||
360 | public function field_options( $field_options, $template_id, $field_id, $context, $input_type ) { |
||
394 | |||
395 | /** |
||
396 | * Get strings used by the Entry Notes field |
||
397 | * |
||
398 | * Use `gravityview/field/notes/strings` filter to modify the strings |
||
399 | * |
||
400 | * @since 1.17 |
||
401 | * |
||
402 | * @param string $key If set, return the string with the key of $key |
||
403 | * |
||
404 | * @return array|string Array of strings with keys and values. If $key is set, returns string. If missing $strings[ $key ], empty string. |
||
405 | */ |
||
406 | static public function strings( $key = '' ) { |
||
407 | |||
408 | $strings = array( |
||
409 | 'add-note' => __( 'Add Note', 'gravityview' ), |
||
410 | 'added-note' => __( 'Note added.', 'gravityview' ), |
||
411 | 'content-label' => __( 'Note Content', 'gravityview' ), |
||
412 | 'delete' => __( 'Delete', 'gravityview' ), |
||
413 | 'delete-confirm' => __( 'Are you sure you want to delete the selected notes?', 'gravityview' ), |
||
414 | 'caption' => __( 'Notes for this entry', 'gravityview' ), |
||
415 | 'toggle-notes' => __( 'Toggle all notes', 'gravityview' ), |
||
416 | 'no-notes' => __( 'There are no notes.', 'gravityview' ), |
||
417 | 'processing' => __( 'Processing…', 'gravityview' ), |
||
418 | 'other-email' => __( 'Other email address', 'gravityview' ), |
||
419 | 'email-label' => __( 'Email address', 'gravityview' ), |
||
420 | 'email-placeholder' => _x('[email protected]', 'Example email address used as a placeholder', 'gravityview'), |
||
421 | 'subject-label' => __( 'Subject', 'gravityview' ), |
||
422 | 'subject' => __( 'Email subject', 'gravityview' ), |
||
423 | 'default-email-subject' => __( 'New entry note', 'gravityview' ), |
||
424 | 'also-email' => __( 'Also email this note to', 'gravityview' ), |
||
425 | 'error-add-note' => __( 'There was an error adding the note.', 'gravityview' ), |
||
426 | 'error-invalid' => __( 'The request was invalid. Refresh the page and try again.', 'gravityview' ), |
||
427 | 'error-empty-note' => _x( 'Note cannot be blank.', 'Message to display when submitting a note without content.', 'gravityview' ), |
||
428 | 'error-cap-delete' => __( 'You don\'t have the ability to delete notes.', 'gravityview' ), |
||
429 | 'error-cap-add' => __( 'You don\'t have the ability to add notes.', 'gravityview' ), |
||
430 | ); |
||
431 | |||
432 | /** |
||
433 | * @filter `gravityview/field/notes/strings` Modify the text used in the Entry Notes field. Sanitized by `esc_html` after return. |
||
434 | * @since 1.17 |
||
435 | * @param array $strings Text in key => value pairs |
||
436 | */ |
||
437 | $strings = gv_map_deep( apply_filters( 'gravityview/field/notes/strings', $strings ), 'esc_html' ); |
||
438 | |||
439 | if( $key ) { |
||
440 | return isset( $strings[ $key ] ) ? $strings[ $key ] : ''; |
||
441 | } |
||
442 | |||
443 | return $strings; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Generate HTML output for a single note |
||
448 | * |
||
449 | * @since 1.17 |
||
450 | * |
||
451 | * @param object $note Note object with id, user_id, date_created, value, note_type, user_name, user_email vars |
||
452 | * @param bool $show_delete Whether to show the bulk delete inputs |
||
453 | * |
||
454 | * @return string HTML |
||
455 | */ |
||
456 | static public function display_note( $note, $show_delete = false ) { |
||
513 | |||
514 | /** |
||
515 | * Add a note. |
||
516 | * |
||
517 | * @since 1.17 |
||
518 | * |
||
519 | * @see GravityView_Entry_Notes::add_note This method is mostly a wrapper |
||
520 | * |
||
521 | * @param array $entry |
||
522 | * @param array $data Note details array |
||
523 | * |
||
524 | * @return int|WP_Error |
||
525 | */ |
||
526 | private function add_note( $entry, $data ) { |
||
541 | |||
542 | /** |
||
543 | * Get the Add Note form HTML |
||
544 | * |
||
545 | * @todo Allow passing entry_id as a shortcode parameter to set entry from shortcode |
||
546 | * |
||
547 | * @since 1.17 |
||
548 | * |
||
549 | * @return string HTML of the Add Note form, or empty string if the user doesn't have the `gravityview_add_entry_notes` cap |
||
550 | */ |
||
551 | public static function get_add_note_part() { |
||
582 | |||
583 | /** |
||
584 | * Get array of emails addresses from the stored entry |
||
585 | * |
||
586 | * @since 1.17 |
||
587 | * |
||
588 | * @return array Array of email addresses connected to the entry |
||
589 | */ |
||
590 | private static function get_note_emails_array() { |
||
617 | |||
618 | /** |
||
619 | * Generate a HTML dropdown of email values based on email fields from the current form |
||
620 | * |
||
621 | * @uses get_note_emails_array |
||
622 | * |
||
623 | * @since 1.17 |
||
624 | * |
||
625 | * @param int|string $entry_slug Current entry unique ID |
||
626 | * |
||
627 | * @return string HTML output |
||
628 | */ |
||
629 | private static function get_note_email_fields( $entry_slug = '' ) { |
||
682 | |||
683 | /** |
||
684 | * If note has an email to send, and the user has the right caps, send it |
||
685 | * |
||
686 | * @since 1.17 |
||
687 | * |
||
688 | * @param false|object $note If note was created, object. Otherwise, false. |
||
689 | * @param array $entry Entry data |
||
690 | * @param array $data $_POST data |
||
691 | * |
||
692 | * @return void Tap in to Gravity Forms' `gform_after_email` action if you want a return result from sending the email. |
||
693 | */ |
||
694 | private function maybe_send_entry_notes( $note = false, $entry, $data ) { |
||
765 | } |
||
766 | |||
768 |
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.