Complex classes like View 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 View, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class View implements \ArrayAccess { |
||
18 | |||
19 | /** |
||
20 | * @var \WP_Post The backing post instance. |
||
21 | */ |
||
22 | private $post; |
||
23 | |||
24 | /** |
||
25 | * @var \GV\View_Settings The settings. |
||
26 | * |
||
27 | * @api |
||
28 | * @since 2.0 |
||
29 | */ |
||
30 | public $settings; |
||
31 | |||
32 | /** |
||
33 | * @var \GV\Widget_Collection The widets attached here. |
||
34 | * |
||
35 | * @api |
||
36 | * @since 2.0 |
||
37 | */ |
||
38 | public $widgets; |
||
39 | |||
40 | /** |
||
41 | * @var \GV\GF_Form|\GV\Form The backing form for this view. |
||
42 | * |
||
43 | * Contains the form that is sourced for entries in this view. |
||
44 | * |
||
45 | * @api |
||
46 | * @since 2.0 |
||
47 | */ |
||
48 | public $form; |
||
49 | |||
50 | /** |
||
51 | * @var \GV\Field_Collection The fields for this view. |
||
52 | * |
||
53 | * Contains all the fields that are attached to this view. |
||
54 | * |
||
55 | * @api |
||
56 | * @since 2.0 |
||
57 | */ |
||
58 | public $fields; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | * |
||
63 | * Internal static cache for gets, and whatnot. |
||
64 | * This is not persistent, resets across requests. |
||
65 | |||
66 | * @internal |
||
67 | */ |
||
68 | private static $cache = array(); |
||
69 | |||
70 | /** |
||
71 | * @var \GV\Join[] The joins for all sources in this view. |
||
72 | * |
||
73 | * @api |
||
74 | * @since 2.0.1 |
||
75 | */ |
||
76 | public $joins = array(); |
||
77 | |||
78 | /** |
||
79 | * @var \GV\Field[][] The unions for all sources in this view. |
||
80 | * An array of fields grouped by form_id keyed by |
||
81 | * main field_id: |
||
82 | * |
||
83 | * array( |
||
84 | * $form_id => array( |
||
85 | * $field_id => $field, |
||
86 | * $field_id => $field, |
||
87 | * ) |
||
88 | * ) |
||
89 | * |
||
90 | * @api |
||
91 | * @since 2.2.2 |
||
92 | */ |
||
93 | public $unions = array(); |
||
94 | |||
95 | /** |
||
96 | * The constructor. |
||
97 | */ |
||
98 | 180 | public function __construct() { |
|
103 | |||
104 | /** |
||
105 | * Register the gravityview WordPress Custom Post Type. |
||
106 | * |
||
107 | * @internal |
||
108 | * @return void |
||
109 | */ |
||
110 | public static function register_post_type() { |
||
214 | |||
215 | /** |
||
216 | * Add extra rewrite endpoints. |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | 2 | public static function add_rewrite_endpoint() { |
|
221 | /** |
||
222 | * CSV. |
||
223 | */ |
||
224 | global $wp_rewrite; |
||
225 | |||
226 | $slug = apply_filters( 'gravityview_slug', 'view' ); |
||
227 | $rule = array( sprintf( '%s/([^/]+)/csv/?', $slug ), 'index.php?gravityview=$matches[1]&csv=1', 'top' ); |
||
228 | |||
229 | add_filter( 'query_vars', function( $query_vars ) { |
||
230 | 2 | $query_vars[] = 'csv'; |
|
231 | 2 | return $query_vars; |
|
232 | } ); |
||
233 | |||
234 | if ( ! isset( $wp_rewrite->extra_rules_top[ $rule[0] ] ) ) { |
||
235 | call_user_func_array( 'add_rewrite_rule', $rule ); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * A renderer filter for the View post type content. |
||
241 | * |
||
242 | * @param string $content Should be empty, as we don't store anything there. |
||
243 | * |
||
244 | * @return string $content The view content as output by the renderers. |
||
245 | */ |
||
246 | 12 | public static function content( $content ) { |
|
386 | |||
387 | /** |
||
388 | * Checks whether this view can be accessed or not. |
||
389 | * |
||
390 | * @param string[] $context The context we're asking for access from. |
||
391 | * Can any and as many of one of: |
||
392 | * edit An edit context. |
||
393 | * single A single context. |
||
394 | * cpt The custom post type single page acessed. |
||
395 | * shortcode Embedded as a shortcode. |
||
396 | * oembed Embedded as an oEmbed. |
||
397 | * rest A REST call. |
||
398 | * @param \GV\Request $request The request |
||
399 | * |
||
400 | * @return bool|\WP_Error An error if this View shouldn't be rendered here. |
||
401 | */ |
||
402 | 30 | public function can_render( $context = null, $request = null ) { |
|
494 | |||
495 | /** |
||
496 | * Get joins associated with a view |
||
497 | * |
||
498 | * @param \WP_Post $post GravityView CPT to get joins for |
||
499 | * |
||
500 | * @api |
||
501 | * @since 2.0.11 |
||
502 | * |
||
503 | * @return \GV\Join[] Array of \GV\Join instances |
||
504 | */ |
||
505 | 180 | public static function get_joins( $post ) { |
|
542 | |||
543 | /** |
||
544 | * Get joined forms associated with a view |
||
545 | * In no particular order. |
||
546 | * |
||
547 | * @since 2.0.11 |
||
548 | * |
||
549 | * @api |
||
550 | * @since 2.0 |
||
551 | * @param int $post_id ID of the View |
||
552 | * |
||
553 | * @return \GV\GF_Form[] Array of \GV\GF_Form instances |
||
554 | */ |
||
555 | 2 | public static function get_joined_forms( $post_id ) { |
|
596 | |||
597 | /** |
||
598 | * Get unions associated with a view |
||
599 | * |
||
600 | * @param \WP_Post $post GravityView CPT to get unions for |
||
601 | * |
||
602 | * @api |
||
603 | * @since 2.2.2 |
||
604 | * |
||
605 | * @return \GV\Field[][] Array of unions (see self::$unions) |
||
606 | */ |
||
607 | 180 | public static function get_unions( $post ) { |
|
652 | |||
653 | /** |
||
654 | * Construct a \GV\View instance from a \WP_Post. |
||
655 | * |
||
656 | * @param \WP_Post $post The \WP_Post instance to wrap. |
||
657 | * |
||
658 | * @api |
||
659 | * @since 2.0 |
||
660 | * @return \GV\View|null An instance around this \WP_Post if valid, null otherwise. |
||
661 | */ |
||
662 | 181 | public static function from_post( $post ) { |
|
765 | |||
766 | /** |
||
767 | * Flush the view cache. |
||
768 | * |
||
769 | * @param int $view_id The View to reset cache for. Optional. Default: resets everything. |
||
770 | * |
||
771 | * @internal |
||
772 | */ |
||
773 | 192 | public static function _flush_cache( $view_id = null ) { |
|
780 | |||
781 | /** |
||
782 | * Construct a \GV\View instance from a post ID. |
||
783 | * |
||
784 | * @param int|string $post_id The post ID. |
||
785 | * |
||
786 | * @api |
||
787 | * @since 2.0 |
||
788 | * @return \GV\View|null An instance around this \WP_Post or null if not found. |
||
789 | */ |
||
790 | 121 | public static function by_id( $post_id ) { |
|
796 | |||
797 | /** |
||
798 | * Determines if a view exists to begin with. |
||
799 | * |
||
800 | * @param int|\WP_Post|null $view The WordPress post ID, a \WP_Post object or null for global $post; |
||
801 | * |
||
802 | * @api |
||
803 | * @since 2.0 |
||
804 | * @return bool Whether the post exists or not. |
||
805 | */ |
||
806 | 25 | public static function exists( $view ) { |
|
809 | |||
810 | /** |
||
811 | * ArrayAccess compatibility layer with GravityView_View_Data::$views |
||
812 | * |
||
813 | * @internal |
||
814 | * @deprecated |
||
815 | * @since 2.0 |
||
816 | * @return bool Whether the offset exists or not, limited to GravityView_View_Data::$views element keys. |
||
817 | */ |
||
818 | 15 | public function offsetExists( $offset ) { |
|
822 | |||
823 | /** |
||
824 | * ArrayAccess compatibility layer with GravityView_View_Data::$views |
||
825 | * |
||
826 | * Maps the old keys to the new data; |
||
827 | * |
||
828 | * @internal |
||
829 | * @deprecated |
||
830 | * @since 2.0 |
||
831 | * |
||
832 | * @return mixed The value of the requested view data key limited to GravityView_View_Data::$views element keys. If offset not found, return null. |
||
833 | */ |
||
834 | 15 | public function offsetGet( $offset ) { |
|
860 | |||
861 | /** |
||
862 | * ArrayAccess compatibility layer with GravityView_View_Data::$views |
||
863 | * |
||
864 | * @internal |
||
865 | * @deprecated |
||
866 | * @since 2.0 |
||
867 | * |
||
868 | * @return void |
||
869 | */ |
||
870 | 1 | public function offsetSet( $offset, $value ) { |
|
873 | |||
874 | /** |
||
875 | * ArrayAccess compatibility layer with GravityView_View_Data::$views |
||
876 | * |
||
877 | * @internal |
||
878 | * @deprecated |
||
879 | * @since 2.0 |
||
880 | * @return void |
||
881 | */ |
||
882 | 1 | public function offsetUnset( $offset ) { |
|
885 | |||
886 | /** |
||
887 | * Be compatible with the old data object. |
||
888 | * |
||
889 | * Some external code expects an array (doing things like foreach on this, or array_keys) |
||
890 | * so let's return an array in the old format for such cases. Do not use unless using |
||
891 | * for back-compatibility. |
||
892 | * |
||
893 | * @internal |
||
894 | * @deprecated |
||
895 | * @since 2.0 |
||
896 | * @return array |
||
897 | */ |
||
898 | 28 | public function as_data() { |
|
910 | |||
911 | /** |
||
912 | * Retrieve the entries for the current view and request. |
||
913 | * |
||
914 | * @param \GV\Request The request. Unused for now. |
||
915 | * |
||
916 | * @return \GV\Entry_Collection The entries. |
||
917 | */ |
||
918 | 72 | public function get_entries( $request = null ) { |
|
919 | 72 | $entries = new \GV\Entry_Collection(); |
|
920 | 72 | if ( $this->form ) { |
|
921 | 72 | $parameters = $this->settings->as_atts(); |
|
922 | |||
923 | /** |
||
924 | * Remove multiple sorting before calling legacy filters. |
||
925 | * This allows us to fake it till we make it. |
||
926 | */ |
||
927 | 72 | if ( ! empty( $parameters['sort_field'] ) && is_array( $parameters['sort_field'] ) ) { |
|
928 | 2 | $has_multisort = true; |
|
929 | 2 | $parameters['sort_field'] = reset( $parameters['sort_field'] ); |
|
930 | 2 | if ( ! empty( $parameters['sort_direction'] ) && is_array( $parameters['sort_direction'] ) ) { |
|
931 | 2 | $parameters['sort_direction'] = reset( $parameters['sort_direction'] ); |
|
932 | } |
||
933 | } |
||
934 | |||
935 | /** |
||
936 | * @todo: Stop using _frontend and use something like $request->get_search_criteria() instead |
||
937 | */ |
||
938 | 72 | $parameters = \GravityView_frontend::get_view_entries_parameters( $parameters, $this->form->ID ); |
|
939 | |||
940 | 72 | $parameters['context_view_id'] = $this->ID; |
|
941 | 72 | $parameters = \GVCommon::calculate_get_entries_criteria( $parameters, $this->form->ID ); |
|
942 | |||
943 | 72 | if ( ! is_array( $parameters ) ) { |
|
944 | $parameters = array(); |
||
945 | } |
||
946 | |||
947 | 72 | if ( ! is_array( $parameters['search_criteria'] ) ) { |
|
948 | $parameters['search_criteria'] = array(); |
||
949 | } |
||
950 | |||
951 | 72 | if ( ( ! isset( $parameters['search_criteria']['field_filters'] ) ) || ( ! is_array( $parameters['search_criteria']['field_filters'] ) ) ) { |
|
952 | $parameters['search_criteria']['field_filters'] = array(); |
||
953 | } |
||
954 | |||
955 | 72 | if ( $request instanceof REST\Request ) { |
|
956 | 6 | $atts = $this->settings->as_atts(); |
|
957 | 6 | $paging_parameters = wp_parse_args( $request->get_paging(), array( |
|
958 | 6 | 'paging' => array( 'page_size' => $atts['page_size'] ), |
|
959 | ) ); |
||
960 | 6 | $parameters['paging'] = $paging_parameters['paging']; |
|
961 | } |
||
962 | |||
963 | 72 | $page = Utils::get( $parameters['paging'], 'current_page' ) ? |
|
964 | 72 | : ( ( ( $parameters['paging']['offset'] - $this->settings->get( 'offset' ) ) / \GV\Utils::get( $parameters, 'paging/page_size', 25 ) ) + 1 ); |
|
965 | |||
966 | /** |
||
967 | * Cleanup duplicate field_filter parameters to simplify the query. |
||
968 | */ |
||
969 | 72 | $unique_field_filters = array(); |
|
970 | 72 | foreach ( Utils::get( $parameters, 'search_criteria/field_filters', array() ) as $key => $filter ) { |
|
971 | 15 | if ( 'mode' === $key ) { |
|
972 | 14 | $unique_field_filters['mode'] = $filter; |
|
973 | 15 | } else if ( ! in_array( $filter, $unique_field_filters ) ) { |
|
974 | 15 | $unique_field_filters[] = $filter; |
|
975 | } |
||
976 | } |
||
977 | 72 | $parameters['search_criteria']['field_filters'] = $unique_field_filters; |
|
978 | |||
979 | 72 | if ( ! empty( $parameters['search_criteria']['field_filters'] ) ) { |
|
980 | 15 | gravityview()->log->notice( 'search_criteria/field_filters is not empty, third-party code may be using legacy search_criteria filters.' ); |
|
981 | } |
||
982 | |||
983 | 72 | if ( gravityview()->plugin->supports( Plugin::FEATURE_GFQUERY ) ) { |
|
984 | |||
985 | 72 | $query_class = $this->get_query_class(); |
|
986 | |||
987 | /** @var \GF_Query $query */ |
||
988 | 72 | $query = new $query_class( $this->form->ID, $parameters['search_criteria'], Utils::get( $parameters, 'sorting' ) ); |
|
989 | |||
990 | /** |
||
991 | * Apply multisort. |
||
992 | */ |
||
993 | 72 | if ( ! empty( $has_multisort ) ) { |
|
994 | 2 | $atts = $this->settings->as_atts(); |
|
995 | |||
996 | 2 | $view_setting_sort_field_ids = \GV\Utils::get( $atts, 'sort_field', array() ); |
|
997 | 2 | $view_setting_sort_directions = \GV\Utils::get( $atts, 'sort_direction', array() ); |
|
998 | |||
999 | 2 | $has_sort_query_param = ! empty( $_GET['sort'] ) && is_array( $_GET['sort'] ); |
|
1000 | |||
1001 | 2 | if( $has_sort_query_param ) { |
|
1002 | $has_sort_query_param = array_filter( array_values( $_GET['sort'] ) ); |
||
1003 | } |
||
1004 | |||
1005 | 2 | if ( $this->settings->get( 'sort_columns' ) && $has_sort_query_param ) { |
|
1006 | $sort_field_ids = array_keys( $_GET['sort'] ); |
||
1007 | $sort_directions = array_values( $_GET['sort'] ); |
||
1008 | } else { |
||
1009 | 2 | $sort_field_ids = $view_setting_sort_field_ids; |
|
1010 | 2 | $sort_directions = $view_setting_sort_directions; |
|
1011 | } |
||
1012 | |||
1013 | 2 | $skip_first = false; |
|
1014 | |||
1015 | 2 | foreach ( (array) $sort_field_ids as $key => $sort_field_id ) { |
|
1016 | |||
1017 | 2 | if ( ! $skip_first && ! $has_sort_query_param ) { |
|
1018 | 2 | $skip_first = true; // Skip the first one, it's already in the query |
|
1019 | 2 | continue; |
|
1020 | } |
||
1021 | |||
1022 | 1 | $sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $sort_field_id, $this->form->ID ); |
|
1023 | 1 | $sort_direction = strtoupper( \GV\Utils::get( $sort_directions, $key, 'ASC' ) ); |
|
1024 | |||
1025 | 1 | if ( ! empty( $sort_field_id ) ) { |
|
1026 | 1 | $order = new \GF_Query_Column( $sort_field_id, $this->form->ID ); |
|
1027 | 1 | if ( \GVCommon::is_field_numeric( $this->form->ID, $sort_field_id ) ) { |
|
1028 | $order = \GF_Query_Call::CAST( $order, defined( 'GF_Query::TYPE_DECIMAL' ) ? \GF_Query::TYPE_DECIMAL : \GF_Query::TYPE_SIGNED ); |
||
1029 | } |
||
1030 | |||
1031 | 1 | $query->order( $order, $sort_direction ); |
|
1032 | } |
||
1033 | } |
||
1034 | } |
||
1035 | |||
1036 | /** |
||
1037 | * Merge time subfield sorts. |
||
1038 | */ |
||
1039 | add_filter( 'gform_gf_query_sql', $gf_query_timesort_sql_callback = function( $sql ) use ( &$query ) { |
||
1040 | 72 | $q = $query->_introspect(); |
|
1041 | 72 | $orders = array(); |
|
1042 | |||
1043 | 72 | $merged_time = false; |
|
1044 | |||
1045 | 72 | foreach ( $q['order'] as $oid => $order ) { |
|
1046 | 72 | if ( $order[0] instanceof \GF_Query_Column ) { |
|
1047 | 71 | $column = $order[0]; |
|
1048 | 1 | } else if ( $order[0] instanceof \GF_Query_Call ) { |
|
1049 | 1 | if ( count( $order[0]->columns ) != 1 || ! $order[0]->columns[0] instanceof \GF_Query_Column ) { |
|
1050 | $orders[ $oid ] = $order; |
||
1051 | continue; // Need something that resembles a single sort |
||
1052 | } |
||
1053 | 1 | $column = $order[0]->columns[0]; |
|
1054 | } |
||
1055 | |||
1056 | 72 | if ( ( ! $field = \GFAPI::get_field( $column->source, $column->field_id ) ) || $field->type !== 'time' ) { |
|
1057 | 71 | $orders[ $oid ] = $order; |
|
1058 | 71 | continue; // Not a time field |
|
1059 | } |
||
1060 | |||
1061 | 1 | if ( ! class_exists( '\GV\Mocks\GF_Query_Call_TIMESORT' ) ) { |
|
1062 | 1 | require_once gravityview()->plugin->dir( 'future/_mocks.timesort.php' ); |
|
1063 | } |
||
1064 | |||
1065 | 1 | $orders[ $oid ] = array( |
|
1066 | 1 | new \GV\Mocks\GF_Query_Call_TIMESORT( 'timesort', array( $column, $sql ) ), |
|
1067 | 1 | $order[1] // Mock it! |
|
1068 | ); |
||
1069 | |||
1070 | 1 | $merged_time = true; |
|
1071 | } |
||
1072 | |||
1073 | 72 | if ( $merged_time ) { |
|
1074 | /** |
||
1075 | * ORDER again. |
||
1076 | */ |
||
1077 | 1 | if ( ! empty( $orders ) && $_orders = $query->_order_generate( $orders ) ) { |
|
1078 | 1 | $sql['order'] = 'ORDER BY ' . implode( ', ', $_orders ); |
|
1079 | } |
||
1080 | } |
||
1081 | |||
1082 | 72 | return $sql; |
|
1083 | 72 | } ); |
|
1084 | |||
1085 | 72 | $query->limit( $parameters['paging']['page_size'] ) |
|
1086 | 72 | ->offset( ( ( $page - 1 ) * $parameters['paging']['page_size'] ) + $this->settings->get( 'offset' ) ); |
|
1087 | |||
1088 | /** |
||
1089 | * Any joins? |
||
1090 | */ |
||
1091 | 72 | if ( gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) && count( $this->joins ) ) { |
|
1092 | |||
1093 | 12 | $is_admin_and_can_view = $this->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap( 'gravityview_moderate_entries', $this->ID ); |
|
1094 | |||
1095 | 12 | foreach ( $this->joins as $join ) { |
|
1096 | 12 | $query = $join->as_query_join( $query ); |
|
1097 | |||
1098 | 12 | if ( $this->settings->get( 'multiple_forms_disable_null_joins' ) ) { |
|
1099 | |||
1100 | // Disable NULL outputs |
||
1101 | $condition = new \GF_Query_Condition( |
||
1102 | new \GF_Query_Column( $join->join_on_column->ID, $join->join_on->ID ), |
||
1103 | \GF_Query_Condition::NEQ, |
||
1104 | new \GF_Query_Literal( '' ) |
||
1105 | ); |
||
1106 | |||
1107 | $query_parameters = $query->_introspect(); |
||
1108 | |||
1109 | $query->where( \GF_Query_Condition::_and( $query_parameters['where'], $condition ) ); |
||
1110 | } |
||
1111 | |||
1112 | /** |
||
1113 | * This is a temporary stub filter, until GF_Query supports NULL conditions. |
||
1114 | * Do not use! This filter will be removed. |
||
1115 | */ |
||
1116 | 12 | if ( defined( 'GF_Query_Condition::NULL' ) ) { |
|
1117 | 12 | $is_null_condition_native = true; |
|
1118 | } else { |
||
1119 | $is_null_condition_class = apply_filters( 'gravityview/query/is_null_condition', null ); |
||
1120 | $is_null_condition_native = false; |
||
1121 | } |
||
1122 | |||
1123 | // Filter to active entries only |
||
1124 | 12 | $condition = new \GF_Query_Condition( |
|
1125 | 12 | new \GF_Query_Column( 'status', $join->join_on->ID ), |
|
1126 | 12 | \GF_Query_Condition::EQ, |
|
1127 | 12 | new \GF_Query_Literal( 'active' ) |
|
1128 | ); |
||
1129 | |||
1130 | 12 | if ( $is_null_condition_native ) { |
|
1131 | 12 | $condition = \GF_Query_Condition::_or( $condition, new \GF_Query_Condition( |
|
1132 | 12 | new \GF_Query_Column( 'status', $join->join_on->ID ), |
|
1133 | 12 | \GF_Query_Condition::IS, |
|
1134 | 12 | \GF_Query_Condition::NULL |
|
1135 | ) ); |
||
1136 | } else if ( ! is_null( $is_null_condition_class ) ) { |
||
1137 | $condition = \GF_Query_Condition::_or( $condition, new $is_null_condition_class( |
||
1138 | new \GF_Query_Column( 'status', $join->join_on->ID ) |
||
1139 | ) ); |
||
1140 | } |
||
1141 | |||
1142 | 12 | $q = $query->_introspect(); |
|
1143 | 12 | $query->where( \GF_Query_Condition::_and( $q['where'], $condition ) ); |
|
1144 | |||
1145 | 12 | if ( $this->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) { |
|
1146 | |||
1147 | // Show only approved joined entries |
||
1148 | 1 | $condition = new \GF_Query_Condition( |
|
1149 | 1 | new \GF_Query_Column( \GravityView_Entry_Approval::meta_key, $join->join_on->ID ), |
|
1150 | 1 | \GF_Query_Condition::EQ, |
|
1151 | 1 | new \GF_Query_Literal( \GravityView_Entry_Approval_Status::APPROVED ) |
|
1152 | ); |
||
1153 | |||
1154 | 1 | if ( $is_null_condition_native ) { |
|
1155 | 1 | $condition = \GF_Query_Condition::_or( $condition, new \GF_Query_Condition( |
|
1156 | 1 | new \GF_Query_Column( \GravityView_Entry_Approval::meta_key, $join->join_on->ID ), |
|
1157 | 1 | \GF_Query_Condition::IS, |
|
1158 | 1 | \GF_Query_Condition::NULL |
|
1159 | ) ); |
||
1160 | } else if ( ! is_null( $is_null_condition_class ) ) { |
||
1161 | $condition = \GF_Query_Condition::_or( $condition, new $is_null_condition_class( |
||
1162 | new \GF_Query_Column( \GravityView_Entry_Approval::meta_key, $join->join_on->ID ) |
||
1163 | ) ); |
||
1164 | } |
||
1165 | |||
1166 | 1 | $query_parameters = $query->_introspect(); |
|
1167 | |||
1168 | 1 | $query->where( \GF_Query_Condition::_and( $query_parameters['where'], $condition ) ); |
|
1169 | } |
||
1170 | } |
||
1171 | |||
1172 | /** |
||
1173 | * Unions? |
||
1174 | */ |
||
1175 | 60 | } else if ( gravityview()->plugin->supports( Plugin::FEATURE_UNIONS ) && count( $this->unions ) ) { |
|
1176 | 1 | $query_parameters = $query->_introspect(); |
|
1177 | |||
1178 | 1 | $unions_sql = array(); |
|
1179 | |||
1180 | /** |
||
1181 | * @param \GF_Query_Condition $condition |
||
1182 | * @param array $fields |
||
1183 | * @param $recurse |
||
1184 | * |
||
1185 | * @return \GF_Query_Condition |
||
1186 | */ |
||
1187 | $where_union_substitute = function( $condition, $fields, $recurse ) { |
||
1188 | 1 | if ( $condition->expressions ) { |
|
1189 | 1 | $conditions = array(); |
|
1190 | |||
1191 | 1 | foreach ( $condition->expressions as $_condition ) { |
|
1192 | 1 | $conditions[] = $recurse( $_condition, $fields, $recurse ); |
|
1193 | } |
||
1194 | |||
1195 | 1 | return call_user_func_array( |
|
1196 | 1 | array( '\GF_Query_Condition', $condition->operator == 'AND' ? '_and' : '_or' ), |
|
1197 | 1 | $conditions |
|
1198 | ); |
||
1199 | } |
||
1200 | |||
1201 | 1 | if ( ! ( $condition->left && $condition->left instanceof \GF_Query_Column ) || ( ! $condition->left->is_entry_column() && ! $condition->left->is_meta_column() ) ) { |
|
1202 | 1 | return new \GF_Query_Condition( |
|
1203 | 1 | new \GF_Query_Column( $fields[ $condition->left->field_id ]->ID ), |
|
1204 | 1 | $condition->operator, |
|
1205 | 1 | $condition->right |
|
1206 | ); |
||
1207 | } |
||
1208 | |||
1209 | 1 | return $condition; |
|
1210 | 1 | }; |
|
1211 | |||
1212 | 1 | foreach ( $this->unions as $form_id => $fields ) { |
|
1213 | |||
1214 | // Build a new query for every unioned form |
||
1215 | 1 | $query_class = $this->get_query_class(); |
|
1216 | |||
1217 | /** @var \GF_Query|\GF_Patched_Query $q */ |
||
1218 | 1 | $q = new $query_class( $form_id ); |
|
1219 | |||
1220 | // Copy the WHERE clauses but substitute the field_ids to the respective ones |
||
1221 | 1 | $q->where( $where_union_substitute( $query_parameters['where'], $fields, $where_union_substitute ) ); |
|
1222 | |||
1223 | // Copy the ORDER clause and substitute the field_ids to the respective ones |
||
1224 | 1 | foreach ( $query_parameters['order'] as $order ) { |
|
1225 | 1 | list( $column, $_order ) = $order; |
|
1226 | |||
1227 | 1 | if ( $column && $column instanceof \GF_Query_Column ) { |
|
1228 | 1 | if ( ! $column->is_entry_column() && ! $column->is_meta_column() ) { |
|
1229 | 1 | $column = new \GF_Query_Column( $fields[ $column->field_id ]->ID ); |
|
1230 | } |
||
1231 | |||
1232 | 1 | $q->order( $column, $_order ); |
|
1233 | } |
||
1234 | } |
||
1235 | |||
1236 | add_filter( 'gform_gf_query_sql', $gf_query_sql_callback = function( $sql ) use ( &$unions_sql ) { |
||
1237 | // Remove SQL_CALC_FOUND_ROWS as it's not needed in UNION clauses |
||
1238 | 1 | $select = 'UNION ALL ' . str_replace( 'SQL_CALC_FOUND_ROWS ', '', $sql['select'] ); |
|
1239 | |||
1240 | // Record the SQL |
||
1241 | 1 | $unions_sql[] = array( |
|
1242 | // Remove columns, we'll rebuild them |
||
1243 | 1 | 'select' => preg_replace( '#DISTINCT (.*)#', 'DISTINCT ', $select ), |
|
1244 | 1 | 'from' => $sql['from'], |
|
1245 | 1 | 'join' => $sql['join'], |
|
1246 | 1 | 'where' => $sql['where'], |
|
1247 | // Remove order and limit |
||
1248 | ); |
||
1249 | |||
1250 | // Return empty query, no need to call the database |
||
1251 | 1 | return array(); |
|
1252 | 1 | } ); |
|
1253 | |||
1254 | 1 | do_action_ref_array( 'gravityview/view/query', array( &$q, $this, $request ) ); |
|
1255 | |||
1256 | 1 | $q->get(); // Launch |
|
1257 | |||
1258 | 1 | remove_filter( 'gform_gf_query_sql', $gf_query_sql_callback ); |
|
1259 | } |
||
1260 | |||
1261 | add_filter( 'gform_gf_query_sql', $gf_query_sql_callback = function( $sql ) use ( $unions_sql ) { |
||
1262 | // Remove SQL_CALC_FOUND_ROWS as it's not needed in UNION clauses |
||
1263 | 1 | $sql['select'] = str_replace( 'SQL_CALC_FOUND_ROWS ', '', $sql['select'] ); |
|
1264 | |||
1265 | // Remove columns, we'll rebuild them |
||
1266 | 1 | preg_match( '#DISTINCT (`[motc]\d+`.`.*?`)#', $sql['select'], $select_match ); |
|
1267 | 1 | $sql['select'] = preg_replace( '#DISTINCT (.*)#', 'DISTINCT ', $sql['select'] ); |
|
1268 | |||
1269 | 1 | $unions = array(); |
|
1270 | |||
1271 | // Transform selected columns to shared alias names |
||
1272 | $column_to_alias = function( $column ) { |
||
1273 | 1 | $column = str_replace( '`', '', $column ); |
|
1274 | 1 | return '`' . str_replace( '.', '_', $column ) . '`'; |
|
1275 | 1 | }; |
|
1276 | |||
1277 | // Add all the order columns into the selects, so we can order by the whole union group |
||
1278 | 1 | preg_match_all( '#(`[motc]\d+`.`.*?`)#', $sql['order'], $order_matches ); |
|
1279 | |||
1280 | $columns = array( |
||
1281 | 1 | sprintf( '%s AS %s', $select_match[1], $column_to_alias( $select_match[1] ) ) |
|
1282 | ); |
||
1283 | |||
1284 | 1 | foreach ( array_slice( $order_matches, 1 ) as $match ) { |
|
1285 | 1 | $columns[] = sprintf( '%s AS %s', $match[0], $column_to_alias( $match[0] ) ); |
|
1286 | |||
1287 | // Rewrite the order columns to the shared aliases |
||
1288 | 1 | $sql['order'] = str_replace( $match[0], $column_to_alias( $match[0] ), $sql['order'] ); |
|
1289 | } |
||
1290 | |||
1291 | 1 | $columns = array_unique( $columns ); |
|
1292 | |||
1293 | // Add the columns to every UNION |
||
1294 | 1 | foreach ( $unions_sql as $union_sql ) { |
|
1295 | 1 | $union_sql['select'] .= implode( ', ', $columns ); |
|
1296 | 1 | $unions []= implode( ' ', $union_sql ); |
|
1297 | } |
||
1298 | |||
1299 | // Add the columns to the main SELECT, but only grab the entry id column |
||
1300 | 1 | $sql['select'] = 'SELECT SQL_CALC_FOUND_ROWS t1_id FROM (' . $sql['select'] . implode( ', ', $columns ); |
|
1301 | 1 | $sql['order'] = implode( ' ', $unions ) . ') AS u ' . $sql['order']; |
|
1302 | |||
1303 | 1 | return $sql; |
|
1304 | 1 | } ); |
|
1305 | } |
||
1306 | |||
1307 | /** |
||
1308 | * @action `gravityview/view/query` Override the \GF_Query before the get() call. |
||
1309 | * @param \GF_Query $query The current query object reference |
||
1310 | * @param \GV\View $this The current view object |
||
1311 | * @param \GV\Request $request The request object |
||
1312 | */ |
||
1313 | 72 | do_action_ref_array( 'gravityview/view/query', array( &$query, $this, $request ) ); |
|
1314 | |||
1315 | 72 | gravityview()->log->debug( 'GF_Query parameters: ', array( 'data' => Utils::gf_query_debug( $query ) ) ); |
|
1316 | |||
1317 | /** |
||
1318 | * Map from Gravity Forms entries arrays to an Entry_Collection. |
||
1319 | */ |
||
1320 | 72 | if ( count( $this->joins ) ) { |
|
1321 | 12 | foreach ( $query->get() as $entry ) { |
|
1322 | 12 | $entries->add( |
|
1323 | 12 | Multi_Entry::from_entries( array_map( '\GV\GF_Entry::from_entry', $entry ) ) |
|
1324 | ); |
||
1325 | } |
||
1326 | } else { |
||
1327 | 60 | array_map( array( $entries, 'add' ), array_map( '\GV\GF_Entry::from_entry', $query->get() ) ); |
|
1328 | } |
||
1329 | |||
1330 | 72 | if ( isset( $gf_query_sql_callback ) ) { |
|
1331 | 1 | remove_action( 'gform_gf_query_sql', $gf_query_sql_callback ); |
|
1332 | } |
||
1333 | |||
1334 | 72 | if ( isset( $gf_query_timesort_sql_callback ) ) { |
|
1335 | 72 | remove_action( 'gform_gf_query_sql', $gf_query_timesort_sql_callback ); |
|
1336 | } |
||
1337 | |||
1338 | /** |
||
1339 | * Add total count callback. |
||
1340 | */ |
||
1341 | $entries->add_count_callback( function() use ( $query ) { |
||
1342 | 32 | return $query->total_found; |
|
1343 | 72 | } ); |
|
1344 | } else { |
||
1345 | $entries = $this->form->entries |
||
1346 | ->filter( \GV\GF_Entry_Filter::from_search_criteria( $parameters['search_criteria'] ) ) |
||
1347 | ->offset( $this->settings->get( 'offset' ) ) |
||
1348 | ->limit( $parameters['paging']['page_size'] ) |
||
1349 | ->page( $page ); |
||
1350 | |||
1351 | if ( ! empty( $parameters['sorting'] ) && is_array( $parameters['sorting'] && ! isset( $parameters['sorting']['key'] ) ) ) { |
||
1352 | // Pluck off multisort arrays |
||
1353 | $parameters['sorting'] = $parameters['sorting'][0]; |
||
1354 | } |
||
1355 | |||
1356 | if ( ! empty( $parameters['sorting'] ) && ! empty( $parameters['sorting']['key'] ) ) { |
||
1357 | $field = new \GV\Field(); |
||
1358 | $field->ID = $parameters['sorting']['key']; |
||
1359 | $direction = strtolower( $parameters['sorting']['direction'] ) == 'asc' ? \GV\Entry_Sort::ASC : \GV\Entry_Sort::DESC; |
||
1360 | $entries = $entries->sort( new \GV\Entry_Sort( $field, $direction ) ); |
||
1361 | } |
||
1362 | } |
||
1363 | } |
||
1364 | |||
1365 | /** |
||
1366 | * @filter `gravityview/view/entries` Modify the entry fetching filters, sorts, offsets, limits. |
||
1367 | * @param \GV\Entry_Collection $entries The entries for this view. |
||
1368 | * @param \GV\View $view The view. |
||
1369 | * @param \GV\Request $request The request. |
||
1370 | */ |
||
1371 | 72 | return apply_filters( 'gravityview/view/entries', $entries, $this, $request ); |
|
1372 | } |
||
1373 | |||
1374 | /** |
||
1375 | * Last chance to configure the output. |
||
1376 | * |
||
1377 | * Used for CSV output, for example. |
||
1378 | * |
||
1379 | * @return void |
||
1380 | */ |
||
1381 | 7 | public static function template_redirect() { |
|
1382 | /** |
||
1383 | * CSV output. |
||
1384 | */ |
||
1385 | 7 | if ( ! get_query_var( 'csv' ) ) { |
|
1386 | 1 | return; |
|
1387 | } |
||
1388 | |||
1389 | 7 | if ( ! $view = gravityview()->request->is_view() ) { |
|
1390 | 1 | return; |
|
1391 | } |
||
1392 | |||
1393 | 7 | if ( is_wp_error( $error = $view->can_render( array( 'csv' ) ) ) ) { |
|
1394 | 1 | gravityview()->log->error( 'Not rendering CSV: ' . $error->get_error_message() ); |
|
1395 | 1 | return; |
|
1396 | } |
||
1397 | |||
1398 | /** |
||
1399 | * Modify the name of the generated CSV file. Name will be sanitized using sanitize_file_name() before output. |
||
1400 | * @see sanitize_file_name() |
||
1401 | * @since 2.1 |
||
1402 | * @param string $filename File name used when downloading a CSV. Default is "{View title}.csv" |
||
1403 | * @param \GV\View $view Current View being rendered |
||
1404 | */ |
||
1405 | 7 | $filename = apply_filters( 'gravityview/output/csv/filename', get_the_title( $view->post ), $view ); |
|
1406 | |||
1407 | 7 | if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) { |
|
1408 | header( sprintf( 'Content-Disposition: attachment;filename="%s.csv"', sanitize_file_name( $filename ) ) ); |
||
1409 | header( 'Content-Transfer-Encoding: binary' ); |
||
1410 | header( 'Content-Type: text/csv' ); |
||
1411 | } |
||
1412 | |||
1413 | 7 | ob_start(); |
|
1414 | 7 | $csv = fopen( 'php://output', 'w' ); |
|
1415 | |||
1416 | /** |
||
1417 | * Add da' BOM if GF uses it |
||
1418 | * @see GFExport::start_export() |
||
1419 | */ |
||
1420 | 7 | if ( apply_filters( 'gform_include_bom_export_entries', true, $view->form ? $view->form->form : null ) ) { |
|
1421 | fputs( $csv, "\xef\xbb\xbf" ); |
||
1422 | } |
||
1423 | |||
1424 | 7 | if ( $view->settings->get( 'csv_nolimit' ) ) { |
|
1425 | 1 | $view->settings->update( array( 'page_size' => -1 ) ); |
|
1426 | } |
||
1427 | |||
1428 | 7 | $entries = $view->get_entries(); |
|
1429 | |||
1430 | 7 | $headers_done = false; |
|
1431 | 7 | $allowed = $headers = array(); |
|
1432 | |||
1433 | 7 | foreach ( $view->fields->by_position( "directory_*" )->by_visible( $view )->all() as $id => $field ) { |
|
1434 | 7 | $allowed[] = $field; |
|
1435 | } |
||
1436 | |||
1437 | 7 | $renderer = new Field_Renderer(); |
|
1438 | |||
1439 | 7 | foreach ( $entries->all() as $entry ) { |
|
1440 | |||
1441 | 7 | $return = array(); |
|
1442 | |||
1443 | /** |
||
1444 | * @filter `gravityview/csv/entry/fields` Whitelist more entry fields by ID that are output in CSV requests. |
||
1445 | * @param[in,out] array $allowed The allowed ones, default by_visible, by_position( "context_*" ), i.e. as set in the View. |
||
1446 | * @param \GV\View $view The view. |
||
1447 | * @param \GV\Entry $entry WordPress representation of the item. |
||
1448 | */ |
||
1449 | 7 | $allowed_field_ids = apply_filters( 'gravityview/csv/entry/fields', wp_list_pluck( $allowed, 'ID' ), $view, $entry ); |
|
1450 | |||
1451 | $allowed = array_filter( $allowed, function( $field ) use ( $allowed_field_ids ) { |
||
1452 | 7 | return in_array( $field->ID, $allowed_field_ids, true ); |
|
1453 | 7 | } ); |
|
1454 | |||
1455 | 7 | foreach ( array_diff( $allowed_field_ids, wp_list_pluck( $allowed, 'ID' ) ) as $field_id ) { |
|
1456 | $allowed[] = is_numeric( $field_id ) ? \GV\GF_Field::by_id( $view->form, $field_id ) : \GV\Internal_Field::by_id( $field_id ); |
||
1457 | } |
||
1458 | |||
1459 | 7 | foreach ( $allowed as $field ) { |
|
1460 | 7 | $source = is_numeric( $field->ID ) ? $view->form : new \GV\Internal_Source(); |
|
1461 | |||
1462 | 7 | $return[] = $renderer->render( $field, $view, $source, $entry, gravityview()->request, '\GV\Field_CSV_Template' ); |
|
1463 | |||
1464 | 7 | if ( ! $headers_done ) { |
|
1465 | 7 | $label = $field->get_label( $view, $source, $entry ); |
|
1466 | 7 | $headers[] = $label ? $label : $field->ID; |
|
1467 | } |
||
1468 | } |
||
1469 | |||
1470 | 7 | if ( ! $headers_done ) { |
|
1471 | 7 | $headers_done = fputcsv( $csv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), array_values( $headers ) ) ); |
|
1472 | } |
||
1473 | |||
1474 | 7 | fputcsv( $csv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), $return ) ); |
|
1475 | } |
||
1476 | |||
1477 | 7 | fflush( $csv ); |
|
1478 | |||
1479 | 7 | echo rtrim( ob_get_clean() ); |
|
1480 | |||
1481 | 7 | if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) { |
|
1482 | exit; |
||
1483 | } |
||
1484 | 7 | } |
|
1485 | |||
1486 | /** |
||
1487 | * Return the query class for this View. |
||
1488 | * |
||
1489 | * @return string The class name. |
||
1490 | */ |
||
1491 | 72 | public function get_query_class() { |
|
1500 | |||
1501 | /** |
||
1502 | * Restrict View access to specific capabilities. |
||
1503 | * |
||
1504 | * Hooked into `map_meta_cap` WordPress filter. |
||
1505 | * |
||
1506 | * @since develop |
||
1507 | * |
||
1508 | * @param $caps array The output capabilities. |
||
1509 | * @param $cap string The cap that is being checked. |
||
1510 | * @param $user_id int The User ID. |
||
1511 | * @param $args array Additional arguments to the capability. |
||
1512 | * |
||
1513 | * @return array The resulting capabilities. |
||
1514 | */ |
||
1515 | 56 | public static function restrict( $caps, $cap, $user_id, $args ) { |
|
1550 | |||
1551 | 180 | public function __get( $key ) { |
|
1558 | } |
||
1559 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.