Complex classes like GravityView_frontend 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_frontend, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class GravityView_frontend { |
||
16 | |||
17 | /** |
||
18 | * Regex strings that are used to determine whether the current request is a GravityView search or not. |
||
19 | * @see GravityView_frontend::is_searching() |
||
20 | * @since 1.7.4.1 |
||
21 | * @var array |
||
22 | */ |
||
23 | private static $search_parameters = array( 'gv_search', 'gv_start', 'gv_end', 'gv_id', 'gv_by', 'filter_*' ); |
||
24 | |||
25 | /** |
||
26 | * Is the currently viewed post a `gravityview` post type? |
||
27 | * @var boolean |
||
28 | */ |
||
29 | var $is_gravityview_post_type = false; |
||
30 | |||
31 | /** |
||
32 | * Does the current post have a `[gravityview]` shortcode? |
||
33 | * @var boolean |
||
34 | */ |
||
35 | var $post_has_shortcode = false; |
||
36 | |||
37 | /** |
||
38 | * The Post ID of the currently viewed post. Not necessarily GV |
||
39 | * @var int |
||
40 | */ |
||
41 | var $post_id = null; |
||
42 | |||
43 | /** |
||
44 | * Are we currently viewing a single entry? |
||
45 | * If so, the int value of the entry ID. Otherwise, false. |
||
46 | * @var int|boolean |
||
47 | */ |
||
48 | var $single_entry = false; |
||
49 | |||
50 | /** |
||
51 | * If we are viewing a single entry, the entry data |
||
52 | * @var array|false |
||
53 | */ |
||
54 | var $entry = false; |
||
55 | |||
56 | /** |
||
57 | * When displaying the single entry we should always know to which View it belongs (the context is everything!) |
||
58 | * @var null |
||
59 | */ |
||
60 | var $context_view_id = null; |
||
61 | |||
62 | /** |
||
63 | * The View is showing search results |
||
64 | * @since 1.5.4 |
||
65 | * @var boolean |
||
66 | */ |
||
67 | var $is_search = false; |
||
68 | |||
69 | /** |
||
70 | * The view data parsed from the $post |
||
71 | * |
||
72 | * @see GravityView_View_Data::__construct() |
||
73 | * @var GravityView_View_Data |
||
74 | */ |
||
75 | var $gv_output_data = null; |
||
76 | |||
77 | /** |
||
78 | * @var GravityView_frontend |
||
79 | */ |
||
80 | static $instance; |
||
81 | |||
82 | /** |
||
83 | * Class constructor, enforce Singleton pattern |
||
84 | */ |
||
85 | private function __construct() {} |
||
86 | |||
87 | 23 | private function initialize() { |
|
103 | |||
104 | /** |
||
105 | * Get the one true instantiated self |
||
106 | * @return GravityView_frontend |
||
107 | */ |
||
108 | 30 | public static function getInstance() { |
|
117 | |||
118 | /** |
||
119 | * @return GravityView_View_Data |
||
120 | */ |
||
121 | 22 | public function getGvOutputData() { |
|
122 | 22 | return $this->gv_output_data; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param GravityView_View_Data $gv_output_data |
||
127 | */ |
||
128 | 22 | public function setGvOutputData( $gv_output_data ) { |
|
129 | 22 | $this->gv_output_data = $gv_output_data; |
|
130 | 22 | } |
|
131 | |||
132 | /** |
||
133 | * @return boolean |
||
134 | */ |
||
135 | 22 | public function isSearch() { |
|
136 | 22 | return $this->is_search; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param boolean $is_search |
||
141 | */ |
||
142 | 22 | public function setIsSearch( $is_search ) { |
|
143 | 22 | $this->is_search = $is_search; |
|
144 | 22 | } |
|
145 | |||
146 | /** |
||
147 | * @return bool|int |
||
148 | */ |
||
149 | 30 | public function getSingleEntry() { |
|
152 | |||
153 | /** |
||
154 | * Sets the single entry ID and also the entry |
||
155 | * @param bool|int|string $single_entry |
||
156 | */ |
||
157 | 22 | public function setSingleEntry( $single_entry ) { |
|
158 | |||
159 | 22 | $this->single_entry = $single_entry; |
|
160 | |||
161 | 22 | } |
|
162 | |||
163 | /** |
||
164 | * @return array |
||
165 | */ |
||
166 | 23 | public function getEntry() { |
|
167 | 23 | return $this->entry; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Set the current entry |
||
172 | * @param array|int $entry Entry array or entry slug or ID |
||
173 | */ |
||
174 | 22 | public function setEntry( $entry ) { |
|
175 | |||
176 | 22 | if ( ! is_array( $entry ) ) { |
|
177 | 22 | $entry = GVCommon::get_entry( $entry ); |
|
178 | } |
||
179 | |||
180 | 22 | $this->entry = $entry; |
|
181 | 22 | } |
|
182 | |||
183 | /** |
||
184 | * @return int |
||
185 | */ |
||
186 | 22 | public function getPostId() { |
|
187 | 22 | return $this->post_id; |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param int $post_id |
||
192 | */ |
||
193 | 22 | public function setPostId( $post_id ) { |
|
194 | 22 | $this->post_id = $post_id; |
|
195 | 22 | } |
|
196 | |||
197 | /** |
||
198 | * @return boolean |
||
199 | */ |
||
200 | 23 | public function isPostHasShortcode() { |
|
203 | |||
204 | /** |
||
205 | * @param boolean $post_has_shortcode |
||
206 | */ |
||
207 | 22 | public function setPostHasShortcode( $post_has_shortcode ) { |
|
208 | 22 | $this->post_has_shortcode = $post_has_shortcode; |
|
209 | 22 | } |
|
210 | |||
211 | /** |
||
212 | * @return boolean |
||
213 | */ |
||
214 | 23 | public function isGravityviewPostType() { |
|
217 | |||
218 | /** |
||
219 | * @param boolean $is_gravityview_post_type |
||
220 | */ |
||
221 | 22 | public function setIsGravityviewPostType( $is_gravityview_post_type ) { |
|
222 | 22 | $this->is_gravityview_post_type = $is_gravityview_post_type; |
|
223 | 22 | } |
|
224 | |||
225 | /** |
||
226 | * Set the context view ID used when page contains multiple embedded views or displaying the single entry view |
||
227 | * |
||
228 | * |
||
229 | * |
||
230 | * @param null $view_id |
||
231 | */ |
||
232 | 1 | public function set_context_view_id( $view_id = null ) { |
|
253 | |||
254 | /** |
||
255 | * Returns the the view_id context when page contains multiple embedded views or displaying single entry view |
||
256 | * |
||
257 | * @since 1.5.4 |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | 26 | public function get_context_view_id() { |
|
264 | |||
265 | /** |
||
266 | * Allow GravityView entry endpoints on the front page of a site |
||
267 | * |
||
268 | * @link https://core.trac.wordpress.org/ticket/23867 Fixes this core issue |
||
269 | * @link https://wordpress.org/plugins/cpt-on-front-page/ Code is based on this |
||
270 | * |
||
271 | * @since 1.17.3 |
||
272 | * |
||
273 | * @param WP_Query &$query (passed by reference) |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | 71 | public function parse_query_fix_frontpage( &$query ) { |
|
331 | |||
332 | /** |
||
333 | * Read the $post and process the View data inside |
||
334 | * @param array $wp Passed in the `wp` hook. Not used. |
||
335 | * @return void |
||
336 | */ |
||
337 | public function parse_content( $wp = array() ) { |
||
363 | |||
364 | /** |
||
365 | * Set the entry |
||
366 | */ |
||
367 | function set_entry_data() { |
||
372 | |||
373 | /** |
||
374 | * Checks if the current View is presenting search results |
||
375 | * |
||
376 | * @since 1.5.4 |
||
377 | * |
||
378 | * @return boolean True: Yes, it's a search; False: No, not a search. |
||
379 | */ |
||
380 | function is_searching() { |
||
422 | |||
423 | /** |
||
424 | * Filter the title for the single entry view |
||
425 | * |
||
426 | * |
||
427 | * @param string $title current title |
||
428 | * @param int $passed_post_id Post ID |
||
429 | * @return string (modified) title |
||
430 | */ |
||
431 | 1 | public function single_entry_title( $title, $passed_post_id = null ) { |
|
432 | 1 | global $post; |
|
433 | |||
434 | // If this is the directory view, return. |
||
435 | 1 | if ( ! $this->getSingleEntry() ) { |
|
436 | 1 | return $title; |
|
437 | } |
||
438 | |||
439 | 1 | $entry = $this->getEntry(); |
|
440 | |||
441 | /** |
||
442 | * @filter `gravityview/single/title/out_loop` Apply the Single Entry Title filter outside the WordPress loop? |
||
443 | * @param boolean $in_the_loop Whether to apply the filter to the menu title and the meta tag <title> - outside the loop |
||
444 | * @param array $entry Current entry |
||
445 | */ |
||
446 | 1 | $apply_outside_loop = apply_filters( 'gravityview/single/title/out_loop' , in_the_loop(), $entry ); |
|
447 | |||
448 | 1 | if ( ! $apply_outside_loop ) { |
|
449 | 1 | return $title; |
|
450 | } |
||
451 | |||
452 | // User reported WooCommerce doesn't pass two args. |
||
453 | if ( empty( $passed_post_id ) ) { |
||
454 | return $title; |
||
455 | } |
||
456 | |||
457 | // Don't modify the title for anything other than the current view/post. |
||
458 | // This is true for embedded shortcodes and Views. |
||
459 | if ( is_object( $post ) && (int) $post->ID !== (int) $passed_post_id ) { |
||
460 | return $title; |
||
461 | } |
||
462 | |||
463 | $context_view_id = $this->get_context_view_id(); |
||
464 | |||
465 | $multiple_views = $this->getGvOutputData()->has_multiple_views(); |
||
466 | |||
467 | if ( $multiple_views && ! empty( $context_view_id ) ) { |
||
468 | $view_meta = $this->getGvOutputData()->get_view( $context_view_id ); |
||
469 | } else { |
||
470 | foreach ( $this->getGvOutputData()->get_views() as $view_id => $view_data ) { |
||
471 | if ( intval( $view_data['form_id'] ) === intval( $entry['form_id'] ) ) { |
||
472 | $view_meta = $view_data; |
||
473 | break; |
||
474 | } |
||
475 | } |
||
476 | } |
||
477 | |||
478 | /** Deprecated stuff in the future. See the branch above. */ |
||
479 | if ( ! empty( $view_meta['atts']['single_title'] ) ) { |
||
480 | |||
481 | $title = $view_meta['atts']['single_title']; |
||
482 | |||
483 | // We are allowing HTML in the fields, so no escaping the output |
||
484 | $title = GravityView_API::replace_variables( $title, $view_meta['form'], $entry ); |
||
485 | |||
486 | $title = do_shortcode( $title ); |
||
487 | } |
||
488 | |||
489 | |||
490 | return $title; |
||
491 | } |
||
492 | |||
493 | |||
494 | /** |
||
495 | * In case View post is called directly, insert the view in the post content |
||
496 | * |
||
497 | * @deprecated Use \GV\View::content() instead. |
||
498 | * |
||
499 | * @access public |
||
500 | * @static |
||
501 | * @param mixed $content |
||
502 | * @return string Add the View output into View CPT content |
||
503 | */ |
||
504 | 2 | public function insert_view_in_content( $content ) { |
|
508 | |||
509 | /** |
||
510 | * Disable comments on GravityView post types |
||
511 | * @param boolean $open existing status |
||
512 | * @param int $post_id Post ID |
||
513 | * @return boolean |
||
514 | */ |
||
515 | public function comments_open( $open, $post_id ) { |
||
531 | |||
532 | /** |
||
533 | * Display a warning when a View has not been configured |
||
534 | * |
||
535 | * @since 1.19.2 |
||
536 | * |
||
537 | * @param int $view_id The ID of the View currently being displayed |
||
538 | * |
||
539 | * @return void |
||
540 | */ |
||
541 | 2 | public function context_not_configured_warning( $view_id = 0 ) { |
|
579 | |||
580 | |||
581 | /** |
||
582 | * Core function to render a View based on a set of arguments |
||
583 | * |
||
584 | * @access public |
||
585 | * @static |
||
586 | * @param array $passed_args { |
||
587 | * |
||
588 | * Settings for rendering the View |
||
589 | * |
||
590 | * @type int $id View id |
||
591 | * @type int $page_size Number of entries to show per page |
||
592 | * @type string $sort_field Form field id to sort |
||
593 | * @type string $sort_direction Sorting direction ('ASC', 'DESC', or 'RAND') |
||
594 | * @type string $start_date - Ymd |
||
595 | * @type string $end_date - Ymd |
||
596 | * @type string $class - assign a html class to the view |
||
597 | * @type string $offset (optional) - This is the start point in the current data set (0 index based). |
||
598 | * } |
||
599 | * |
||
600 | * @deprecated Use \GV\View_Renderer |
||
601 | * |
||
602 | * @return string|null HTML output of a View, NULL if View isn't found |
||
603 | */ |
||
604 | 1 | public function render_view( $passed_args ) { |
|
629 | |||
630 | /** |
||
631 | * Process the start and end dates for a view - overrides values defined in shortcode (if needed) |
||
632 | * |
||
633 | * The `start_date` and `end_date` keys need to be in a format processable by GFFormsModel::get_date_range_where(), |
||
634 | * which uses \DateTime() format. |
||
635 | * |
||
636 | * You can set the `start_date` or `end_date` to any value allowed by {@link http://www.php.net//manual/en/function.strtotime.php strtotime()}, |
||
637 | * including strings like "now" or "-1 year" or "-3 days". |
||
638 | * |
||
639 | * @see GFFormsModel::get_date_range_where |
||
640 | * |
||
641 | * @param array $args View settings |
||
642 | * @param array $search_criteria Search being performed, if any |
||
643 | * @return array Modified `$search_criteria` array |
||
644 | */ |
||
645 | 21 | public static function process_search_dates( $args, $search_criteria = array() ) { |
|
711 | |||
712 | |||
713 | /** |
||
714 | * Process the approved only search criteria according to the View settings |
||
715 | * |
||
716 | * @param array $args View settings |
||
717 | * @param array $search_criteria Search being performed, if any |
||
718 | * @return array Modified `$search_criteria` array |
||
719 | */ |
||
720 | 21 | public static function process_search_only_approved( $args, $search_criteria ) { |
|
742 | |||
743 | |||
744 | /** |
||
745 | * Check if a certain entry is approved. |
||
746 | * |
||
747 | * If we pass the View settings ($args) it will check the 'show_only_approved' setting before |
||
748 | * checking the entry approved field, returning true if show_only_approved = false. |
||
749 | * |
||
750 | * @since 1.7 |
||
751 | * @since 1.18 Converted check to use GravityView_Entry_Approval_Status::is_approved |
||
752 | * |
||
753 | * @uses GravityView_Entry_Approval_Status::is_approved |
||
754 | * |
||
755 | * @param array $entry Entry object |
||
756 | * @param array $args View settings (optional) |
||
757 | * |
||
758 | * @return bool |
||
759 | */ |
||
760 | public static function is_entry_approved( $entry, $args = array() ) { |
||
777 | |||
778 | /** |
||
779 | * Parse search criteria for a entries search. |
||
780 | * |
||
781 | * array( |
||
782 | * 'search_field' => 1, // ID of the field |
||
783 | * 'search_value' => '', // Value of the field to search |
||
784 | * 'search_operator' => 'contains', // 'is', 'isnot', '>', '<', 'contains' |
||
785 | * 'show_only_approved' => 0 or 1 // Boolean |
||
786 | * ) |
||
787 | * |
||
788 | * @param array $args Array of args |
||
789 | * @param int $form_id Gravity Forms form ID |
||
790 | * @return array Array of search parameters, formatted in Gravity Forms mode, using `status` key set to "active" by default, `field_filters` array with `key`, `value` and `operator` keys. |
||
791 | */ |
||
792 | 21 | public static function get_search_criteria( $args, $form_id ) { |
|
850 | |||
851 | |||
852 | |||
853 | /** |
||
854 | * Core function to calculate View multi entries (directory) based on a set of arguments ($args): |
||
855 | * $id - View id |
||
856 | * $page_size - Page |
||
857 | * $sort_field - form field id to sort |
||
858 | * $sort_direction - ASC / DESC |
||
859 | * $start_date - Ymd |
||
860 | * $end_date - Ymd |
||
861 | * $class - assign a html class to the view |
||
862 | * $offset (optional) - This is the start point in the current data set (0 index based). |
||
863 | * |
||
864 | * |
||
865 | * |
||
866 | * @uses gravityview_get_entries() |
||
867 | * @access public |
||
868 | * @param array $args\n |
||
869 | * - $id - View id |
||
870 | * - $page_size - Page |
||
871 | * - $sort_field - form field id to sort |
||
872 | * - $sort_direction - ASC / DESC |
||
873 | * - $start_date - Ymd |
||
874 | * - $end_date - Ymd |
||
875 | * - $class - assign a html class to the view |
||
876 | * - $offset (optional) - This is the start point in the current data set (0 index based). |
||
877 | * @param int $form_id Gravity Forms Form ID |
||
878 | * @return array Associative array with `count`, `entries`, and `paging` keys. `count` has the total entries count, `entries` is an array with Gravity Forms full entry data, `paging` is an array with `offset` and `page_size` keys |
||
879 | */ |
||
880 | 1 | public static function get_view_entries( $args, $form_id ) { |
|
917 | |||
918 | /** |
||
919 | * Get an array of search parameters formatted as Gravity Forms requires |
||
920 | * |
||
921 | * Results are filtered by `gravityview_get_entries` and `gravityview_get_entries_{View ID}` filters |
||
922 | * |
||
923 | * @uses GravityView_frontend::get_search_criteria |
||
924 | * @uses GravityView_frontend::get_search_criteria_paging |
||
925 | * |
||
926 | * @since 1.20 |
||
927 | * |
||
928 | * @see \GV\View_Settings::defaults For $args options |
||
929 | * |
||
930 | * @param array $args Array of View settings, as structured in \GV\View_Settings::defaults |
||
931 | * @param int $form_id Gravity Forms form ID to search |
||
932 | * |
||
933 | * @return array With `search_criteria`, `sorting`, `paging`, `cache` keys |
||
934 | */ |
||
935 | 20 | public static function get_view_entries_parameters( $args = array(), $form_id = 0 ) { |
|
990 | |||
991 | /** |
||
992 | * Get the paging array for the View |
||
993 | * |
||
994 | * @since 1.19.5 |
||
995 | * |
||
996 | * @param $args |
||
997 | * @param int $form_id |
||
998 | */ |
||
999 | 20 | public static function get_search_criteria_paging( $args ) { |
|
1031 | |||
1032 | /** |
||
1033 | * Updates the View sorting criteria |
||
1034 | * |
||
1035 | * @since 1.7 |
||
1036 | * |
||
1037 | * @param array $args View settings. Required to have `sort_field` and `sort_direction` keys |
||
1038 | * @param int $form_id The ID of the form used to sort |
||
1039 | * @return array $sorting Array with `key`, `direction` and `is_numeric` keys |
||
1040 | */ |
||
1041 | 20 | public static function updateViewSorting( $args, $form_id ) { |
|
1088 | |||
1089 | /** |
||
1090 | * Override sorting per field |
||
1091 | * |
||
1092 | * Currently only modifies sorting ID when sorting by the full name. Sorts by first name. |
||
1093 | * Use the `gravityview/sorting/full-name` filter to override. |
||
1094 | * |
||
1095 | * @todo Filter from GravityView_Field |
||
1096 | * @since 1.7.4 |
||
1097 | * @internal Hi developer! Although this is public, don't call this method; we're going to replace it. |
||
1098 | * |
||
1099 | * @param int|string $sort_field_id Field used for sorting (`id` or `1.2`) |
||
1100 | * @param int $form_id GF Form ID |
||
1101 | * |
||
1102 | * @return string Possibly modified sorting ID |
||
1103 | */ |
||
1104 | 20 | public static function _override_sorting_id_by_field_type( $sort_field_id, $form_id ) { |
|
1192 | |||
1193 | /** |
||
1194 | * Verify if user requested a single entry view |
||
1195 | * @return boolean|string false if not, single entry slug if true |
||
1196 | */ |
||
1197 | 7 | public static function is_single_entry() { |
|
1217 | |||
1218 | |||
1219 | /** |
||
1220 | * Register styles and scripts |
||
1221 | * |
||
1222 | * @access public |
||
1223 | * @return void |
||
1224 | */ |
||
1225 | 1 | public function add_scripts_and_styles() { |
|
1313 | |||
1314 | /** |
||
1315 | * Handle enqueuing the `gravityview_default_style` stylesheet |
||
1316 | * |
||
1317 | * @since 1.17 |
||
1318 | * |
||
1319 | * @param array $css_dependencies Dependencies for the `gravityview_default_style` stylesheet |
||
1320 | * |
||
1321 | * @return void |
||
1322 | */ |
||
1323 | private function enqueue_default_style( $css_dependencies = array() ) { |
||
1340 | |||
1341 | /** |
||
1342 | * Add template extra style if exists |
||
1343 | * @param string $template_id |
||
1344 | */ |
||
1345 | public static function add_style( $template_id ) { |
||
1357 | |||
1358 | |||
1359 | /** |
||
1360 | * Inject the sorting links on the table columns |
||
1361 | * |
||
1362 | * Callback function for hook 'gravityview/template/field_label' |
||
1363 | * @see GravityView_API::field_label() (in includes/class-api.php) |
||
1364 | * |
||
1365 | * @since 1.7 |
||
1366 | * |
||
1367 | * @param string $label Field label |
||
1368 | * @param array $field Field settings |
||
1369 | * @param array $form Form object |
||
1370 | * |
||
1371 | * @return string Field Label |
||
1372 | */ |
||
1373 | public function add_columns_sort_links( $label = '', $field, $form ) { |
||
1416 | |||
1417 | /** |
||
1418 | * Checks if field (column) is sortable |
||
1419 | * |
||
1420 | * @param string $field Field settings |
||
1421 | * @param array $form Gravity Forms form array |
||
1422 | * |
||
1423 | * @since 1.7 |
||
1424 | * |
||
1425 | * @return bool True: Yes, field is sortable; False: not sortable |
||
1426 | */ |
||
1427 | public function is_field_sortable( $field_id = '', $form = array() ) { |
||
1457 | |||
1458 | } |
||
1459 | |||
1464 |
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.