Complex classes like GVCommon 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 GVCommon, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class GVCommon { |
||
20 | |||
21 | /** |
||
22 | * Returns the form object for a given Form ID. |
||
23 | * |
||
24 | * @access public |
||
25 | * @param mixed $form_id |
||
26 | * @return mixed False: no form ID specified or Gravity Forms isn't active. Array: Form returned from Gravity Forms |
||
27 | */ |
||
28 | public static function get_form( $form_id ) { |
||
44 | |||
45 | /** |
||
46 | * Alias of GravityView_Roles_Capabilities::has_cap() |
||
47 | * |
||
48 | * @since 1.15 |
||
49 | * |
||
50 | * @see GravityView_Roles_Capabilities::has_cap() |
||
51 | * |
||
52 | * @param string|array $caps Single capability or array of capabilities |
||
53 | * @param int $object_id (optional) Parameter can be used to check for capabilities against a specific object, such as a post or user |
||
54 | * @param int|null $user_id (optional) Check the capabilities for a user who is not necessarily the currently logged-in user |
||
55 | * |
||
56 | * @return bool True: user has at least one passed capability; False: user does not have any defined capabilities |
||
57 | */ |
||
58 | public static function has_cap( $caps = '', $object_id = null, $user_id = null ) { |
||
61 | |||
62 | /** |
||
63 | * Return a Gravity Forms field array, whether using GF 1.9 or not |
||
64 | * |
||
65 | * @since 1.7 |
||
66 | * |
||
67 | * @param array|GF_Fields $field Gravity Forms field or array |
||
68 | * @return array Array version of $field |
||
69 | */ |
||
70 | public static function get_field_array( $field ) { |
||
71 | |||
72 | if ( class_exists( 'GF_Fields' ) ) { |
||
73 | |||
74 | $field_object = GF_Fields::create( $field ); |
||
75 | |||
76 | // Convert the field object in 1.9 to an array for backward compatibility |
||
77 | $field_array = get_object_vars( $field_object ); |
||
78 | |||
79 | } else { |
||
80 | $field_array = $field; |
||
81 | } |
||
82 | |||
83 | return $field_array; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Get all existing Views |
||
88 | * |
||
89 | * @since 1.5.4 |
||
90 | * @since TODO Added $args array |
||
91 | * |
||
92 | * @param array $args Pass custom array of args, formatted as if for `get_posts()` |
||
93 | * |
||
94 | * @return array Array of Views as `WP_Post`. Empty array if none found. |
||
95 | */ |
||
96 | public static function get_all_views( $args = array() ) { |
||
97 | |||
98 | $default_params = array( |
||
99 | 'post_type' => 'gravityview', |
||
100 | 'posts_per_page' => -1, |
||
101 | 'post_status' => 'publish', |
||
102 | ); |
||
103 | |||
104 | $params = wp_parse_args( $args, $default_params ); |
||
105 | |||
106 | /** |
||
107 | * @filter `gravityview/get_all_views/params` Modify the parameters sent to get all views. |
||
108 | * @param[in,out] array $params Array of parameters to pass to `get_posts()` |
||
109 | */ |
||
110 | $views_params = apply_filters( 'gravityview/get_all_views/params', $params ); |
||
111 | |||
112 | $views = get_posts( $views_params ); |
||
113 | |||
114 | return $views; |
||
115 | } |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Get the form array for an entry based only on the entry ID |
||
120 | * @param int|string $entry_slug Entry slug |
||
121 | * @return array Gravity Forms form array |
||
122 | */ |
||
123 | public static function get_form_from_entry_id( $entry_slug ) { |
||
124 | |||
125 | $entry = self::get_entry( $entry_slug, true ); |
||
126 | |||
127 | $form = self::get_form( $entry['form_id'] ); |
||
128 | |||
129 | return $form; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Check whether a form has product fields |
||
134 | * |
||
135 | * @since 1.16 |
||
136 | * |
||
137 | * @param array $form Gravity Forms form array |
||
138 | * |
||
139 | * @return bool|GF_Field[] |
||
140 | */ |
||
141 | public static function has_product_field( $form = array() ) { |
||
142 | |||
143 | $product_fields = apply_filters( 'gform_product_field_types', array( 'option', 'quantity', 'product', 'total', 'shipping', 'calculation', 'price' ) ); |
||
144 | |||
145 | $fields = GFAPI::get_fields_by_type( $form, $product_fields ); |
||
146 | |||
147 | return empty( $fields ) ? false : $fields; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Get the entry ID from the entry slug, which may or may not be the entry ID |
||
152 | * |
||
153 | * @since 1.5.2 |
||
154 | * @param string $slug The entry slug, as returned by GravityView_API::get_entry_slug() |
||
155 | * @return int|null The entry ID, if exists; `NULL` if not |
||
156 | */ |
||
157 | public static function get_entry_id_from_slug( $slug ) { |
||
158 | global $wpdb; |
||
159 | |||
160 | $search_criteria = array( |
||
161 | 'field_filters' => array( |
||
162 | array( |
||
163 | 'key' => 'gravityview_unique_id', // Search the meta values |
||
164 | 'value' => $slug, |
||
165 | 'operator' => 'is', |
||
166 | 'type' => 'meta', |
||
167 | ), |
||
168 | ) |
||
169 | ); |
||
170 | |||
171 | // Limit to one for speed |
||
172 | $paging = array( |
||
173 | 'page_size' => 1, |
||
174 | ); |
||
175 | |||
176 | $results = GFAPI::get_entries( 0, $search_criteria, null, $paging ); |
||
177 | |||
178 | $result = ( ! empty( $results ) && ! empty( $results[0]['id'] ) ) ? $results[0]['id'] : null; |
||
179 | |||
180 | return $result; |
||
181 | } |
||
182 | |||
183 | |||
184 | /** |
||
185 | * Returns the list of available forms |
||
186 | * |
||
187 | * @access public |
||
188 | * @param mixed $form_id |
||
189 | * @return array Empty array if GFAPI isn't available or no forms. Otherwise, associative array with id, title keys |
||
190 | */ |
||
191 | public static function get_forms() { |
||
192 | $forms = array(); |
||
193 | if ( class_exists( 'GFAPI' ) ) { |
||
194 | $gf_forms = GFAPI::get_forms(); |
||
195 | foreach ( $gf_forms as $form ) { |
||
196 | $forms[] = array( |
||
197 | 'id' => $form['id'], |
||
198 | 'title' => $form['title'], |
||
199 | ); |
||
200 | } |
||
201 | } |
||
202 | return $forms; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Return array of fields' id and label, for a given Form ID |
||
207 | * |
||
208 | * @access public |
||
209 | * @param string|array $form_id (default: '') or $form object |
||
210 | * @return array |
||
211 | */ |
||
212 | public static function get_form_fields( $form = '', $add_default_properties = false, $include_parent_field = true ) { |
||
213 | |||
214 | if ( ! is_array( $form ) ) { |
||
215 | $form = self::get_form( $form ); |
||
216 | } |
||
217 | |||
218 | $fields = array(); |
||
219 | $has_product_fields = false; |
||
220 | $has_post_fields = false; |
||
221 | $has_quiz_fields = false; |
||
222 | $has_poll_fields = false; |
||
223 | |||
224 | // If GF_Field exists, we're using GF 1.9+, where add_default_properties has been deprecated. |
||
225 | if ( false === class_exists( 'GF_Field' ) && $add_default_properties ) { |
||
226 | $form = RGFormsModel::add_default_properties( $form ); |
||
227 | } |
||
228 | |||
229 | if ( $form ) { |
||
230 | foreach ( $form['fields'] as $field ) { |
||
231 | if ( $include_parent_field || empty( $field['inputs'] ) ) { |
||
232 | $fields[ $field['id'] ] = array( |
||
233 | 'label' => rgar( $field, 'label' ), |
||
234 | 'parent' => null, |
||
235 | 'type' => rgar( $field, 'type' ), |
||
236 | 'adminLabel' => rgar( $field, 'adminLabel' ), |
||
237 | 'adminOnly' => rgar( $field, 'adminOnly' ), |
||
238 | ); |
||
239 | } |
||
240 | |||
241 | if ( $add_default_properties && ! empty( $field['inputs'] ) ) { |
||
242 | foreach ( $field['inputs'] as $input ) { |
||
243 | /** |
||
244 | * @hack |
||
245 | * In case of email/email confirmation, the input for email has the same id as the parent field |
||
246 | */ |
||
247 | if( 'email' == rgar( $field, 'type' ) && false === strpos( $input['id'], '.' ) ) { |
||
248 | continue; |
||
249 | } |
||
250 | $fields[ (string)$input['id'] ] = array( |
||
251 | 'label' => rgar( $input, 'label' ), |
||
252 | 'customLabel' => rgar( $input, 'customLabel' ), |
||
253 | 'parent' => $field, |
||
254 | 'type' => rgar( $field, 'type' ), |
||
255 | 'adminLabel' => rgar( $field, 'adminLabel' ), |
||
256 | 'adminOnly' => rgar( $field, 'adminOnly' ), |
||
257 | ); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** @since 1.14 */ |
||
262 | if( 'list' === $field['type'] && !empty( $field['enableColumns'] ) ) { |
||
263 | |||
264 | foreach ( (array)$field['choices'] as $key => $input ) { |
||
265 | |||
266 | $input_id = sprintf( '%d.%d', $field['id'], $key ); // {field_id}.{column_key} |
||
267 | |||
268 | $fields[ $input_id ] = array( |
||
269 | 'label' => rgar( $input, 'text' ), |
||
270 | 'customLabel' => '', |
||
271 | 'parent' => $field, |
||
272 | 'type' => rgar( $field, 'type' ), |
||
273 | 'adminLabel' => rgar( $field, 'adminLabel' ), |
||
274 | 'adminOnly' => rgar( $field, 'adminOnly' ), |
||
275 | ); |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @since 1.8 |
||
281 | */ |
||
282 | if( 'quiz' === $field['type'] ) { |
||
283 | $has_quiz_fields = true; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @since 1.8 |
||
288 | */ |
||
289 | if( 'poll' === $field['type'] ) { |
||
290 | $has_poll_fields = true; |
||
291 | } |
||
292 | |||
293 | if( GFCommon::is_product_field( $field['type'] ) ){ |
||
294 | $has_product_fields = true; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @hack Version 1.9 |
||
299 | */ |
||
300 | $field_for_is_post_field = class_exists( 'GF_Fields' ) ? (object) $field : (array) $field; |
||
301 | |||
302 | if ( GFCommon::is_post_field( $field_for_is_post_field ) ) { |
||
303 | $has_post_fields = true; |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @since 1.7 |
||
310 | */ |
||
311 | if ( $has_post_fields ) { |
||
312 | $fields['post_id'] = array( |
||
313 | 'label' => __( 'Post ID', 'gravityview' ), |
||
314 | 'type' => 'post_id', |
||
315 | ); |
||
316 | } |
||
317 | |||
318 | if ( $has_product_fields ) { |
||
319 | |||
320 | $payment_fields = GravityView_Fields::get_all( 'pricing' ); |
||
321 | |||
322 | foreach ( $payment_fields as $payment_field ) { |
||
323 | if( isset( $fields["{$payment_field->name}"] ) ) { |
||
324 | continue; |
||
325 | } |
||
326 | $fields["{$payment_field->name}"] = array( |
||
327 | 'label' => $payment_field->label, |
||
328 | 'desc' => $payment_field->description, |
||
329 | 'type' => $payment_field->name, |
||
330 | ); |
||
331 | } |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @since 1.8 |
||
336 | */ |
||
337 | if( $has_quiz_fields ) { |
||
338 | |||
339 | $fields['gquiz_score'] = array( |
||
340 | 'label' => __( 'Quiz Score Total', 'gravityview' ), |
||
341 | 'type' => 'quiz_score', |
||
342 | 'desc' => __( 'Displays the number of correct Quiz answers the user submitted.', 'gravityview' ), |
||
343 | ); |
||
344 | $fields['gquiz_percent'] = array( |
||
345 | 'label' => __( 'Quiz Percentage Grade', 'gravityview' ), |
||
346 | 'type' => 'quiz_percent', |
||
347 | 'desc' => __( 'Displays the percentage of correct Quiz answers the user submitted.', 'gravityview' ), |
||
348 | ); |
||
349 | $fields['gquiz_grade'] = array( |
||
350 | 'label' => __( 'Quiz Letter Grade', 'gravityview' ), |
||
351 | 'type' => 'quiz_grade', |
||
352 | 'desc' => __( 'Displays the Grade the user achieved based on Letter Grading configured in the Quiz Settings.', 'gravityview' ), |
||
353 | ); |
||
354 | $fields['gquiz_is_pass'] = array( |
||
355 | 'label' => __( 'Quiz Pass/Fail', 'gravityview' ), |
||
356 | 'type' => 'quiz_is_pass', |
||
357 | 'desc' => __( 'Displays either Passed or Failed based on the Pass/Fail settings configured in the Quiz Settings.', 'gravityview' ), |
||
358 | ); |
||
359 | } |
||
360 | |||
361 | return $fields; |
||
362 | |||
363 | } |
||
364 | |||
365 | /** |
||
366 | * get extra fields from entry meta |
||
367 | * @param string $form_id (default: '') |
||
368 | * @return array |
||
369 | */ |
||
370 | public static function get_entry_meta( $form_id, $only_default_column = true ) { |
||
384 | |||
385 | |||
386 | /** |
||
387 | * Wrapper for the Gravity Forms GFFormsModel::search_lead_ids() method |
||
388 | * |
||
389 | * @see GFEntryList::leads_page() |
||
390 | * @param int $form_id ID of the Gravity Forms form |
||
391 | * @since 1.1.6 |
||
392 | * @return array|void Array of entry IDs. Void if Gravity Forms isn't active. |
||
393 | */ |
||
394 | public static function get_entry_ids( $form_id, $search_criteria = array() ) { |
||
402 | |||
403 | /** |
||
404 | * Calculates the Search Criteria used on the self::get_entries / self::get_entry methods |
||
405 | * |
||
406 | * @since 1.7.4 |
||
407 | * |
||
408 | * @param array $passed_criteria array Input Criteria (search_criteria, sorting, paging) |
||
409 | * @param array $form_ids array Gravity Forms form IDs |
||
410 | * @return array |
||
411 | */ |
||
412 | public static function calculate_get_entries_criteria( $passed_criteria = array(), $form_ids = array() ) { |
||
495 | |||
496 | |||
497 | /** |
||
498 | * Retrieve entries given search, sort, paging criteria |
||
499 | * |
||
500 | * @see GFAPI::get_entries() |
||
501 | * @see GFFormsModel::get_field_filters_where() |
||
502 | * @access public |
||
503 | * @param int|array $form_ids The ID of the form or an array IDs of the Forms. Zero for all forms. |
||
504 | * @param mixed $passed_criteria (default: null) |
||
505 | * @param mixed &$total Optional. An output parameter containing the total number of entries. Pass a non-null value to generate the total count. (default: null) |
||
506 | * @return mixed False: Error fetching entries. Array: Multi-dimensional array of Gravity Forms entry arrays |
||
507 | */ |
||
508 | public static function get_entries( $form_ids = null, $passed_criteria = null, &$total = null ) { |
||
584 | |||
585 | |||
586 | /** |
||
587 | * Return a single entry object |
||
588 | * |
||
589 | * Since 1.4, supports custom entry slugs. The way that GravityView fetches an entry based on the custom slug is by searching `gravityview_unique_id` meta. The `$entry_slug` is fetched by getting the current query var set by `is_single_entry()` |
||
590 | * |
||
591 | * @access public |
||
592 | * @param string|int $entry_slug Either entry ID or entry slug string |
||
593 | * @param boolean $force_allow_ids Force the get_entry() method to allow passed entry IDs, even if the `gravityview_custom_entry_slug_allow_id` filter returns false. |
||
594 | * @param boolean $check_entry_display Check whether the entry is visible for the current View configuration. Default: true. {@since 1.14} |
||
595 | * @return array|boolean |
||
596 | */ |
||
597 | public static function get_entry( $entry_slug, $force_allow_ids = false, $check_entry_display = true ) { |
||
658 | |||
659 | /** |
||
660 | * Wrapper for the GFFormsModel::matches_operation() method that adds additional comparisons, including: |
||
661 | * 'equals', 'greater_than_or_is', 'greater_than_or_equals', 'less_than_or_is', 'less_than_or_equals', |
||
662 | * and 'not_contains' |
||
663 | * |
||
664 | * @since 1.13 You can define context, which displays/hides based on what's being displayed (single, multiple, edit) |
||
665 | * |
||
666 | * @see http://docs.gravityview.co/article/252-gvlogic-shortcode |
||
667 | * @uses GFFormsModel::matches_operation |
||
668 | * @since 1.7.5 |
||
669 | * |
||
670 | * @param string $val1 Left side of comparison |
||
671 | * @param string $val2 Right side of comparison |
||
672 | * @param string $operation Type of comparison |
||
673 | * |
||
674 | * @return bool True: matches, false: not matches |
||
675 | */ |
||
676 | public static function matches_operation( $val1, $val2, $operation ) { |
||
725 | |||
726 | /** |
||
727 | * |
||
728 | * Checks if a certain entry is valid according to the View search filters (specially the Adv Filters) |
||
729 | * |
||
730 | * @see GFFormsModel::is_value_match() |
||
731 | * |
||
732 | * @since 1.7.4 |
||
733 | * @todo Return WP_Error instead of boolean |
||
734 | * |
||
735 | * @param array $entry Gravity Forms Entry object |
||
736 | * @return bool|array Returns 'false' if entry is not valid according to the view search filters (Adv Filter) |
||
737 | */ |
||
738 | public static function check_entry_display( $entry ) { |
||
831 | |||
832 | |||
833 | /** |
||
834 | * Allow formatting date and time based on GravityView standards |
||
835 | * |
||
836 | * @since 1.16 |
||
837 | * |
||
838 | * @see GVCommon_Test::test_format_date for examples |
||
839 | * |
||
840 | * @param string $date_string The date as stored by Gravity Forms (`Y-m-d h:i:s` GMT) |
||
841 | * @param string|array $args Array or string of settings for output parsed by `wp_parse_args()`; Can use `raw=1` or `array('raw' => true)` \n |
||
842 | * - `raw` Un-formatted date string in original `Y-m-d h:i:s` format |
||
843 | * - `timestamp` Integer timestamp returned by GFCommon::get_local_timestamp() |
||
844 | * - `diff` "%s ago" format, unless other `format` is defined |
||
845 | * - `human` Set $is_human parameter to true for `GFCommon::format_date()`. Shows `diff` within 24 hours or date after. Format based on blog setting, unless `format` is defined. |
||
846 | * - `time` Include time in the `GFCommon::format_date()` output |
||
847 | * - `format` Define your own date format, or `diff` format |
||
848 | * |
||
849 | * @return int|null|string Formatted date based on the original date |
||
850 | */ |
||
851 | public static function format_date( $date_string = '', $args = array() ) { |
||
898 | |||
899 | /** |
||
900 | * Retrieve the label of a given field id (for a specific form) |
||
901 | * |
||
902 | * @access public |
||
903 | * @param array $form |
||
904 | * @param string $field_id |
||
905 | * @return string |
||
906 | */ |
||
907 | public static function get_field_label( $form = array(), $field_id = '' ) { |
||
917 | |||
918 | |||
919 | /** |
||
920 | * Returns the field details array of a specific form given the field id |
||
921 | * |
||
922 | * Alias of GFFormsModel::get_field |
||
923 | * |
||
924 | * @uses GFFormsModel::get_field |
||
925 | * @see GFFormsModel::get_field |
||
926 | * @access public |
||
927 | * @param array $form |
||
928 | * @param string|int $field_id |
||
929 | * @return array|null Array: Gravity Forms field array; NULL: Gravity Forms GFFormsModel does not exist |
||
930 | */ |
||
931 | public static function get_field( $form, $field_id ) { |
||
938 | |||
939 | |||
940 | /** |
||
941 | * Check whether the post is GravityView |
||
942 | * |
||
943 | * - Check post type. Is it `gravityview`? |
||
944 | * - Check shortcode |
||
945 | * |
||
946 | * @param WP_Post $post WordPress post object |
||
947 | * @return boolean True: yep, GravityView; No: not! |
||
948 | */ |
||
949 | public static function has_gravityview_shortcode( $post = null ) { |
||
961 | |||
962 | |||
963 | /** |
||
964 | * Placeholder until the recursive has_shortcode() patch is merged |
||
965 | * @see https://core.trac.wordpress.org/ticket/26343#comment:10 |
||
966 | * @param string $content Content to check whether there's a shortcode |
||
967 | * @param string $tag Current shortcode tag |
||
968 | */ |
||
969 | public static function has_shortcode_r( $content, $tag = 'gravityview' ) { |
||
998 | |||
999 | |||
1000 | |||
1001 | /** |
||
1002 | * Get the views for a particular form |
||
1003 | * |
||
1004 | * @since 1.15.2 Add $args array and limit posts_per_page to 500 |
||
1005 | * |
||
1006 | * @uses get_posts() |
||
1007 | * |
||
1008 | * @param int $form_id Gravity Forms form ID |
||
1009 | * @param array $args Pass args sent to get_posts() |
||
1010 | * |
||
1011 | * @return array Array with view details, as returned by get_posts() |
||
1012 | */ |
||
1013 | public static function get_connected_views( $form_id, $args = array() ) { |
||
1028 | |||
1029 | /** |
||
1030 | * Get the Gravity Forms form ID connected to a View |
||
1031 | * |
||
1032 | * @param int $view_id The ID of the View to get the connected form of |
||
1033 | * |
||
1034 | * @return string ID of the connected Form, if exists. Empty string if not. |
||
1035 | */ |
||
1036 | public static function get_meta_form_id( $view_id ) { |
||
1039 | |||
1040 | /** |
||
1041 | * Get the template ID (`list`, `table`, `datatables`, `map`) for a View |
||
1042 | * |
||
1043 | * @see GravityView_Template::template_id |
||
1044 | * |
||
1045 | * @param int $view_id The ID of the View to get the layout of |
||
1046 | * |
||
1047 | * @return string GravityView_Template::template_id value. Empty string if not. |
||
1048 | */ |
||
1049 | public static function get_meta_template_id( $view_id ) { |
||
1052 | |||
1053 | |||
1054 | /** |
||
1055 | * Get all the settings for a View |
||
1056 | * |
||
1057 | * @uses GravityView_View_Data::get_default_args() Parses the settings with the plugin defaults as backups. |
||
1058 | * @param int $post_id View ID |
||
1059 | * @return array Associative array of settings with plugin defaults used if not set by the View |
||
1060 | */ |
||
1061 | public static function get_template_settings( $post_id ) { |
||
1076 | |||
1077 | /** |
||
1078 | * Get the setting for a View |
||
1079 | * |
||
1080 | * If the setting isn't set by the View, it returns the plugin default. |
||
1081 | * |
||
1082 | * @param int $post_id View ID |
||
1083 | * @param string $key Key for the setting |
||
1084 | * @return mixed|null Setting value, or NULL if not set. |
||
1085 | */ |
||
1086 | public static function get_template_setting( $post_id, $key ) { |
||
1096 | |||
1097 | /** |
||
1098 | * Get the field configuration for the View |
||
1099 | * |
||
1100 | * array( |
||
1101 | * |
||
1102 | * [other zones] |
||
1103 | * |
||
1104 | * 'directory_list-title' => array( |
||
1105 | * |
||
1106 | * [other fields] |
||
1107 | * |
||
1108 | * '5372653f25d44' => array( |
||
1109 | * 'id' => string '9' (length=1) |
||
1110 | * 'label' => string 'Screenshots' (length=11) |
||
1111 | * 'show_label' => string '1' (length=1) |
||
1112 | * 'custom_label' => string '' (length=0) |
||
1113 | * 'custom_class' => string 'gv-gallery' (length=10) |
||
1114 | * 'only_loggedin' => string '0' (length=1) |
||
1115 | * 'only_loggedin_cap' => string 'read' (length=4) |
||
1116 | * ) |
||
1117 | * |
||
1118 | * [other fields] |
||
1119 | * ) |
||
1120 | * |
||
1121 | * [other zones] |
||
1122 | * ) |
||
1123 | * |
||
1124 | * @param int $post_id View ID |
||
1125 | * @return array Multi-array of fields with first level being the field zones. See code comment. |
||
1126 | */ |
||
1127 | public static function get_directory_fields( $post_id ) { |
||
1130 | |||
1131 | |||
1132 | /** |
||
1133 | * Render dropdown (select) with the list of sortable fields from a form ID |
||
1134 | * |
||
1135 | * @access public |
||
1136 | * @param int $formid Form ID |
||
1137 | * @return string html |
||
1138 | */ |
||
1139 | public static function get_sortable_fields( $formid, $current = '' ) { |
||
1163 | |||
1164 | /** |
||
1165 | * |
||
1166 | * @param int $formid Gravity Forms form ID |
||
1167 | * @param array $blacklist Field types to exclude |
||
1168 | * |
||
1169 | * @since TODO |
||
1170 | * |
||
1171 | * @todo Get all fields, check if sortable dynamically |
||
1172 | * |
||
1173 | * @return array |
||
1174 | */ |
||
1175 | public static function get_sortable_fields_array( $formid, $blacklist = array( 'list', 'textarea' ) ) { |
||
1209 | |||
1210 | /** |
||
1211 | * Returns the GF Form field type for a certain field(id) of a form |
||
1212 | * @param object $form Gravity Forms form |
||
1213 | * @param mixed $field_id Field ID or Field array |
||
1214 | * @return string field type |
||
1215 | */ |
||
1216 | public static function get_field_type( $form = null, $field_id = '' ) { |
||
1227 | |||
1228 | |||
1229 | /** |
||
1230 | * Checks if the field type is a 'numeric' field type (e.g. to be used when sorting) |
||
1231 | * @param int|array $form form ID or form array |
||
1232 | * @param int|array $field field key or field array |
||
1233 | * @return boolean |
||
1234 | */ |
||
1235 | public static function is_field_numeric( $form = null, $field = '' ) { |
||
1266 | |||
1267 | /** |
||
1268 | * Encrypt content using Javascript so that it's hidden when JS is disabled. |
||
1269 | * |
||
1270 | * This is mostly used to hide email addresses from scraper bots. |
||
1271 | * |
||
1272 | * @param string $content Content to encrypt |
||
1273 | * @param string $message Message shown if Javascript is disabled |
||
1274 | * |
||
1275 | * @see https://github.com/jnicol/standalone-phpenkoder StandalonePHPEnkoder on Github |
||
1276 | * |
||
1277 | * @since 1.7 |
||
1278 | * |
||
1279 | * @return string Content, encrypted |
||
1280 | */ |
||
1281 | public static function js_encrypt( $content, $message = '' ) { |
||
1308 | |||
1309 | /** |
||
1310 | * |
||
1311 | * Do the same than parse_str without max_input_vars limitation: |
||
1312 | * Parses $string as if it were the query string passed via a URL and sets variables in the current scope. |
||
1313 | * @param $string array string to parse (not altered like in the original parse_str(), use the second parameter!) |
||
1314 | * @param $result array If the second parameter is present, variables are stored in this variable as array elements |
||
1315 | * @return bool true or false if $string is an empty string |
||
1316 | * @since 1.5.3 |
||
1317 | * |
||
1318 | * @author rubo77 at https://gist.github.com/rubo77/6821632 |
||
1319 | **/ |
||
1320 | public static function gv_parse_str( $string, &$result ) { |
||
1343 | |||
1344 | |||
1345 | /** |
||
1346 | * Generate an HTML anchor tag with a list of supported attributes |
||
1347 | * |
||
1348 | * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a Supported attributes defined here |
||
1349 | * @uses esc_url_raw() to sanitize $href |
||
1350 | * @uses esc_attr() to sanitize $atts |
||
1351 | * |
||
1352 | * @since 1.6 |
||
1353 | * |
||
1354 | * @param string $href URL of the link. Sanitized using `esc_url_raw()` |
||
1355 | * @param string $anchor_text The text or HTML inside the anchor. This is not sanitized in the function. |
||
1356 | * @param array|string $atts Attributes to be added to the anchor tag. Parsed by `wp_parse_args()`, sanitized using `esc_attr()` |
||
1357 | * |
||
1358 | * @return string HTML output of anchor link. If empty $href, returns NULL |
||
1359 | */ |
||
1360 | public static function get_link_html( $href = '', $anchor_text = '', $atts = array() ) { |
||
1424 | |||
1425 | /** |
||
1426 | * array_merge_recursive does indeed merge arrays, but it converts values with duplicate |
||
1427 | * keys to arrays rather than overwriting the value in the first array with the duplicate |
||
1428 | * value in the second array, as array_merge does. |
||
1429 | * |
||
1430 | * @see http://php.net/manual/en/function.array-merge-recursive.php |
||
1431 | * |
||
1432 | * @since 1.5.3 |
||
1433 | * @param array $array1 |
||
1434 | * @param array $array2 |
||
1435 | * @return array |
||
1436 | * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk> |
||
1437 | * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com> |
||
1438 | */ |
||
1439 | public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) { |
||
1452 | |||
1453 | /** |
||
1454 | * Get WordPress users with reasonable limits set |
||
1455 | * |
||
1456 | * @param string $context Where are we using this information (e.g. change_entry_creator, search_widget ..) |
||
1457 | * @param array $args Arguments to modify the user query. See get_users() {@since 1.14} |
||
1458 | * @return array Array of WP_User objects. |
||
1459 | */ |
||
1460 | public static function get_users( $context = 'change_entry_creator', $args = array() ) { |
||
1481 | |||
1482 | |||
1483 | /** |
||
1484 | * Display updated/error notice |
||
1485 | * |
||
1486 | * @param string $notice text/HTML of notice |
||
1487 | * @param string $class CSS class for notice (`updated` or `error`) |
||
1488 | * |
||
1489 | * @return string |
||
1490 | */ |
||
1491 | public static function generate_notice( $notice, $class = '' ) { |
||
1494 | |||
1495 | |||
1496 | |||
1497 | |||
1498 | } //end class |
||
1499 | |||
1517 |
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.