Complex classes like GravityView_API 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_API, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class GravityView_API { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Fetch Field Label |
||
| 18 | * |
||
| 19 | * @access public |
||
| 20 | * @static |
||
| 21 | * @param array $field GravityView field array |
||
| 22 | * @param array $entry Gravity Forms entry array |
||
| 23 | * @param boolean $force_show_label Whether to always show the label, regardless of field settings |
||
| 24 | * @return string |
||
| 25 | */ |
||
| 26 | public static function field_label( $field, $entry = array(), $force_show_label = false ) { |
||
| 27 | $gravityview_view = GravityView_View::getInstance(); |
||
| 28 | |||
| 29 | $form = $gravityview_view->getForm(); |
||
| 30 | |||
| 31 | $label = ''; |
||
| 32 | |||
| 33 | if( !empty( $field['show_label'] ) || $force_show_label ) { |
||
|
|
|||
| 34 | |||
| 35 | $label = $field['label']; |
||
| 36 | |||
| 37 | // Support Gravity Forms 1.9+ |
||
| 38 | if( class_exists( 'GF_Field' ) ) { |
||
| 39 | |||
| 40 | $field_object = RGFormsModel::get_field( $form, $field['id'] ); |
||
| 41 | |||
| 42 | if( $field_object ) { |
||
| 43 | |||
| 44 | $input = GFFormsModel::get_input( $field_object, $field['id'] ); |
||
| 45 | |||
| 46 | // This is a complex field, with labels on a per-input basis |
||
| 47 | if( $input ) { |
||
| 48 | |||
| 49 | // Does the input have a custom label on a per-input basis? Otherwise, default label. |
||
| 50 | $label = ! empty( $input['customLabel'] ) ? $input['customLabel'] : $input['label']; |
||
| 51 | |||
| 52 | } else { |
||
| 53 | |||
| 54 | // This is a field with one label |
||
| 55 | $label = $field_object->get_field_label( true, $field['label'] ); |
||
| 56 | |||
| 57 | } |
||
|
1 ignored issue
–
show
|
|||
| 58 | |||
| 59 | } |
||
|
1 ignored issue
–
show
|
|||
| 60 | |||
| 61 | } |
||
| 62 | |||
| 63 | // Use Gravity Forms label by default, but if a custom label is defined in GV, use it. |
||
| 64 | if ( !empty( $field['custom_label'] ) ) { |
||
| 65 | |||
| 66 | $label = self::replace_variables( $field['custom_label'], $form, $entry ); |
||
| 67 | |||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @filter `gravityview_render_after_label` Append content to a field label |
||
| 72 | * @param[in,out] string $appended_content Content you can add after a label. Empty by default. |
||
| 73 | * @param[in] array $field GravityView field array |
||
| 74 | */ |
||
| 75 | $label .= apply_filters( 'gravityview_render_after_label', '', $field ); |
||
| 76 | |||
| 77 | } // End $field['show_label'] |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @filter `gravityview/template/field_label` Modify field label output |
||
| 81 | * @since 1.7 |
||
| 82 | * @param[in,out] string $label Field label HTML |
||
| 83 | * @param[in] array $field GravityView field array |
||
| 84 | * @param[in] array $form Gravity Forms form array |
||
| 85 | * @param[in] array $entry Gravity Forms entry array |
||
| 86 | */ |
||
| 87 | $label = apply_filters( 'gravityview/template/field_label', $label, $field, $form, $entry ); |
||
| 88 | |||
| 89 | return $label; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Alias for GravityView_Merge_Tags::replace_variables() |
||
| 94 | * |
||
| 95 | * @see GravityView_Merge_Tags::replace_variables() Moved in 1.8.4 |
||
| 96 | * |
||
| 97 | * @param string $text Text to replace variables in |
||
| 98 | * @param array $form GF Form array |
||
| 99 | * @param array $entry GF Entry array |
||
| 100 | * @return string Text with variables maybe replaced |
||
| 101 | */ |
||
| 102 | public static function replace_variables( $text, $form = array(), $entry = array() ) { |
||
| 103 | return GravityView_Merge_Tags::replace_variables( $text, $form, $entry ); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get column width from the field setting |
||
| 108 | * |
||
| 109 | * @since 1.9 |
||
| 110 | * |
||
| 111 | * @param array $field Array of settings for the field |
||
| 112 | * @param string $format Format for width. "%" (default) will return |
||
| 113 | * |
||
| 114 | * @return string|null If not empty, string in $format format. Otherwise, null. |
||
| 115 | */ |
||
| 116 | public static function field_width( $field, $format = '%d%%' ) { |
||
| 117 | |||
| 118 | $width = NULL; |
||
| 119 | |||
| 120 | if( !empty( $field['width'] ) ) { |
||
| 121 | $width = absint( $field['width'] ); |
||
| 122 | |||
| 123 | // If using percentages, limit to 100% |
||
| 124 | if( '%d%%' === $format && $width > 100 ) { |
||
| 125 | $width = 100; |
||
| 126 | } |
||
| 127 | |||
| 128 | $width = sprintf( $format, $width ); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $width; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Fetch Field class |
||
| 136 | * |
||
| 137 | * @access public |
||
| 138 | * @static |
||
| 139 | * @param mixed $field |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public static function field_class( $field, $form = NULL, $entry = NULL ) { |
||
| 143 | $gravityview_view = GravityView_View::getInstance(); |
||
| 144 | |||
| 145 | $classes = array(); |
||
| 146 | |||
| 147 | if( !empty( $field['custom_class'] ) ) { |
||
| 148 | |||
| 149 | $custom_class = $field['custom_class']; |
||
| 150 | |||
| 151 | if( !empty( $entry ) ) { |
||
| 152 | |||
| 153 | // We want the merge tag to be formatted as a class. The merge tag may be |
||
| 154 | // replaced by a multiple-word value that should be output as a single class. |
||
| 155 | // "Office Manager" will be formatted as `.OfficeManager`, not `.Office` and `.Manager` |
||
| 156 | add_filter('gform_merge_tag_filter', 'sanitize_html_class'); |
||
| 157 | |||
| 158 | $custom_class = self::replace_variables( $custom_class, $form, $entry); |
||
| 159 | |||
| 160 | // And then we want life to return to normal |
||
| 161 | remove_filter('gform_merge_tag_filter', 'sanitize_html_class'); |
||
| 162 | } |
||
| 163 | |||
| 164 | // And now we want the spaces to be handled nicely. |
||
| 165 | $classes[] = gravityview_sanitize_html_class( $custom_class ); |
||
| 166 | |||
| 167 | } |
||
| 168 | |||
| 169 | if(!empty($field['id'])) { |
||
| 170 | if( !empty( $form ) && !empty( $form['id'] ) ) { |
||
| 171 | $form_id = '-'.$form['id']; |
||
| 172 | } else { |
||
| 173 | $form_id = $gravityview_view->getFormId() ? '-'. $gravityview_view->getFormId() : ''; |
||
| 174 | } |
||
| 175 | |||
| 176 | $classes[] = 'gv-field'.$form_id.'-'.$field['id']; |
||
| 177 | } |
||
| 178 | |||
| 179 | return esc_attr(implode(' ', $classes)); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Fetch Field HTML ID |
||
| 184 | * |
||
| 185 | * @since 1.11 |
||
| 186 | * |
||
| 187 | * @access public |
||
| 188 | * @static |
||
| 189 | * @param array $field GravityView field array passed to gravityview_field_output() |
||
| 190 | * @param array $form Gravity Forms form array, if set. |
||
| 191 | * @param array $entry Gravity Forms entry array |
||
| 192 | * @return string Sanitized unique HTML `id` attribute for the field |
||
| 193 | */ |
||
| 194 | public static function field_html_attr_id( $field, $form = array(), $entry = array() ) { |
||
| 195 | $gravityview_view = GravityView_View::getInstance(); |
||
| 196 | $id = $field['id']; |
||
| 197 | |||
| 198 | if ( ! empty( $id ) ) { |
||
| 199 | if ( ! empty( $form ) && ! empty( $form['id'] ) ) { |
||
| 200 | $form_id = '-' . $form['id']; |
||
| 201 | } else { |
||
| 202 | $form_id = $gravityview_view->getFormId() ? '-' . $gravityview_view->getFormId() : ''; |
||
| 203 | } |
||
| 204 | |||
| 205 | $id = 'gv-field' . $form_id . '-' . $field['id']; |
||
| 206 | } |
||
| 207 | |||
| 208 | return esc_attr( $id ); |
||
| 209 | } |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Given an entry and a form field id, calculate the entry value for that field. |
||
| 214 | * |
||
| 215 | * @access public |
||
| 216 | * @param array $entry |
||
| 217 | * @param array $field |
||
| 218 | * @return null|string |
||
| 219 | */ |
||
| 220 | public static function field_value( $entry, $field_settings, $format = 'html' ) { |
||
| 221 | |||
| 222 | if( empty( $entry['form_id'] ) || empty( $field_settings['id'] ) ) { |
||
| 223 | return NULL; |
||
| 224 | } |
||
| 225 | |||
| 226 | $gravityview_view = GravityView_View::getInstance(); |
||
| 227 | |||
| 228 | $field_id = $field_settings['id']; |
||
| 229 | $form = $gravityview_view->getForm(); |
||
| 230 | $field = gravityview_get_field( $form, $field_id ); |
||
| 231 | |||
| 232 | if( $field && is_numeric( $field_id ) ) { |
||
| 233 | // Used as file name of field template in GV. |
||
| 234 | // Don't use RGFormsModel::get_input_type( $field ); we don't care if it's a radio input; we want to know it's a 'quiz' field |
||
| 235 | $field_type = $field->type; |
||
| 236 | $value = RGFormsModel::get_lead_field_value( $entry, $field ); |
||
| 237 | } else { |
||
| 238 | $field = GravityView_Fields::get_associated_field( $field_id ); |
||
| 239 | $field_type = $field_id; // Used as file name of field template in GV |
||
| 240 | } |
||
| 241 | |||
| 242 | // If a Gravity Forms Field is found, get the field display |
||
| 243 | if( $field ) { |
||
| 244 | |||
| 245 | // Prevent any PHP warnings that may be generated |
||
| 246 | ob_start(); |
||
| 247 | |||
| 248 | $display_value = GFCommon::get_lead_field_display( $field, $value, $entry["currency"], false, $format ); |
||
| 249 | |||
| 250 | if ( $errors = ob_get_clean() ) { |
||
| 251 | do_action( 'gravityview_log_error', 'GravityView_API[field_value] Errors when calling GFCommon::get_lead_field_display()', $errors ); |
||
| 252 | } |
||
| 253 | |||
| 254 | $display_value = apply_filters( "gform_entry_field_value", $display_value, $field, $entry, $form ); |
||
| 255 | |||
| 256 | // prevent the use of merge_tags for non-admin fields |
||
| 257 | if( !empty( $field->adminOnly ) ) { |
||
| 258 | $display_value = self::replace_variables( $display_value, $form, $entry ); |
||
| 259 | } |
||
| 260 | } else { |
||
| 261 | $value = $display_value = rgar( $entry, $field_id ); |
||
| 262 | $display_value = $value; |
||
| 263 | } |
||
| 264 | |||
| 265 | // Check whether the field exists in /includes/fields/{$field_type}.php |
||
| 266 | // This can be overridden by user template files. |
||
| 267 | $field_path = $gravityview_view->locate_template("fields/{$field_type}.php"); |
||
| 268 | |||
| 269 | // Set the field data to be available in the templates |
||
| 270 | $gravityview_view->setCurrentField( array( |
||
| 271 | 'form' => $form, |
||
| 272 | 'field_id' => $field_id, |
||
| 273 | 'field' => $field, |
||
| 274 | 'field_settings' => $field_settings, |
||
| 275 | 'value' => $value, |
||
| 276 | 'display_value' => $display_value, |
||
| 277 | 'format' => $format, |
||
| 278 | 'entry' => $entry, |
||
| 279 | 'field_type' => $field_type, /** {@since 1.6} */ |
||
| 280 | 'field_path' => $field_path, /** {@since 1.16} */ |
||
| 281 | )); |
||
| 282 | |||
| 283 | if( ! empty( $field_path ) ) { |
||
| 284 | |||
| 285 | do_action( 'gravityview_log_debug', sprintf('[field_value] Rendering %s', $field_path ) ); |
||
| 286 | |||
| 287 | ob_start(); |
||
| 288 | |||
| 289 | load_template( $field_path, false ); |
||
| 290 | |||
| 291 | $output = ob_get_clean(); |
||
| 292 | |||
| 293 | } else { |
||
| 294 | |||
| 295 | // Backup; the field template doesn't exist. |
||
| 296 | $output = $display_value; |
||
| 297 | |||
| 298 | } |
||
| 299 | |||
| 300 | // Get the field settings again so that the field template can override the settings |
||
| 301 | $field_settings = $gravityview_view->getCurrentField('field_settings'); |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @filter `gravityview_field_entry_value_{$field_type}_pre_link` Modify the field value output for a field type before Show As Link setting is applied. Example: `gravityview_field_entry_value_number_pre_link` |
||
| 305 | * @since 1.16 |
||
| 306 | * @param string $output HTML value output |
||
| 307 | * @param array $entry The GF entry array |
||
| 308 | * @param array $field_settings Settings for the particular GV field |
||
| 309 | * @param array $field Field array, as fetched from GravityView_View::getCurrentField() |
||
| 310 | */ |
||
| 311 | $output = apply_filters( 'gravityview_field_entry_value_' . $field_type . '_pre_link', $output, $entry, $field_settings, $gravityview_view->getCurrentField() ); |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Link to the single entry by wrapping the output in an anchor tag |
||
| 315 | * |
||
| 316 | * Fields can override this by modifying the field data variable inside the field. See /templates/fields/post_image.php for an example. |
||
| 317 | * |
||
| 318 | */ |
||
| 319 | if( !empty( $field_settings['show_as_link'] ) && ! gv_empty( $output, false, false ) ) { |
||
| 320 | |||
| 321 | $link_atts = empty( $field_settings['new_window'] ) ? array() : array( 'target' => '_blank' ); |
||
| 322 | |||
| 323 | $output = self::entry_link_html( $entry, $output, $link_atts, $field_settings ); |
||
| 324 | |||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @filter `gravityview_field_entry_value_{$field_type}` Modify the field value output for a field type. Example: `gravityview_field_entry_value_number` |
||
| 329 | * @since 1.6 |
||
| 330 | * @param string $output HTML value output |
||
| 331 | * @param array $entry The GF entry array |
||
| 332 | * @param array $field_settings Settings for the particular GV field |
||
| 333 | * @param array $field Current field being displayed |
||
| 334 | */ |
||
| 335 | $output = apply_filters( 'gravityview_field_entry_value_'.$field_type, $output, $entry, $field_settings, $gravityview_view->getCurrentField() ); |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @filter `gravityview_field_entry_value` Modify the field value output for all field types |
||
| 339 | * @param string $output HTML value output |
||
| 340 | * @param array $entry The GF entry array |
||
| 341 | * @param array $field_settings Settings for the particular GV field |
||
| 342 | * @param array $field_data {@since 1.6} |
||
| 343 | */ |
||
| 344 | $output = apply_filters( 'gravityview_field_entry_value', $output, $entry, $field_settings, $gravityview_view->getCurrentField() ); |
||
| 345 | |||
| 346 | return $output; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Generate an anchor tag that links to an entry. |
||
| 351 | * |
||
| 352 | * @since 1.6 |
||
| 353 | * @see GVCommon::get_link_html() |
||
| 354 | * |
||
| 355 | * @param string $anchor_text The text or HTML inside the link |
||
| 356 | * @param array $entry Gravity Forms entry array |
||
| 357 | * @param array|string $passed_tag_atts Attributes to be added to the anchor tag, such as `title` or `rel`. |
||
| 358 | * @param array $field_settings Array of field settings. Optional, but passed to the `gravityview_field_entry_link` filter |
||
| 359 | * |
||
| 360 | * @return string|null Returns HTML for an anchor link. Null if $entry isn't defined or is missing an ID. |
||
| 361 | */ |
||
| 362 | public static function entry_link_html( $entry = array(), $anchor_text = '', $passed_tag_atts = array(), $field_settings = array() ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get the "No Results" text depending on whether there were results. |
||
| 391 | * @param boolean $wpautop Apply wpautop() to the output? |
||
| 392 | * @return string HTML of "no results" text |
||
| 393 | */ |
||
| 394 | public static function no_results($wpautop = true) { |
||
| 395 | $gravityview_view = GravityView_View::getInstance(); |
||
| 396 | |||
| 397 | $is_search = false; |
||
| 398 | |||
| 399 | if( $gravityview_view && ( $gravityview_view->curr_start || $gravityview_view->curr_end || $gravityview_view->curr_search ) ) { |
||
| 400 | $is_search = true; |
||
| 401 | } |
||
| 402 | |||
| 403 | if($is_search) { |
||
| 404 | $output = __('This search returned no results.', 'gravityview'); |
||
| 405 | } else { |
||
| 406 | $output = __('No entries match your request.', 'gravityview'); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @filter `gravitview_no_entries_text` Modify the text displayed when there are no entries. |
||
| 411 | * @param string $output The existing "No Entries" text |
||
| 412 | * @param boolean $is_search Is the current page a search result, or just a multiple entries screen? |
||
| 413 | */ |
||
| 414 | $output = apply_filters( 'gravitview_no_entries_text', $output, $is_search); |
||
| 415 | |||
| 416 | return $wpautop ? wpautop($output) : $output; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Generate a link to the Directory view |
||
| 421 | * |
||
| 422 | * Uses `wp_cache_get` and `wp_cache_get` (since 1.3) to speed up repeated requests to get permalink, which improves load time. Since we may be doing this hundreds of times per request, it adds up! |
||
| 423 | * |
||
| 424 | * @param int $post_id Post ID |
||
| 425 | * @param boolean $add_query_args Add pagination and sorting arguments |
||
| 426 | * @return string Permalink to multiple entries view |
||
| 427 | */ |
||
| 428 | public static function directory_link( $post_id = NULL, $add_query_args = true ) { |
||
| 429 | global $post; |
||
| 430 | |||
| 431 | $gravityview_view = GravityView_View::getInstance(); |
||
| 432 | |||
| 433 | if( empty( $post_id ) ) { |
||
| 434 | |||
| 435 | $post_id = false; |
||
| 436 | |||
| 437 | // DataTables passes the Post ID |
||
| 438 | if( defined('DOING_AJAX') && DOING_AJAX ) { |
||
| 439 | |||
| 440 | $post_id = isset( $_POST['post_id'] ) ? (int)$_POST['post_id'] : false; |
||
| 441 | |||
| 442 | } else { |
||
| 443 | |||
| 444 | // The Post ID has been passed via the shortcode |
||
| 445 | if( !empty( $gravityview_view ) && $gravityview_view->getPostId() ) { |
||
| 446 | |||
| 447 | $post_id = $gravityview_view->getPostId(); |
||
| 448 | |||
| 449 | } else { |
||
| 450 | |||
| 451 | // This is a GravityView post type |
||
| 452 | if( GravityView_frontend::getInstance()->isGravityviewPostType() ) { |
||
| 453 | |||
| 454 | $post_id = isset( $gravityview_view ) ? $gravityview_view->getViewId() : $post->ID; |
||
| 455 | |||
| 456 | } else { |
||
| 457 | |||
| 458 | // This is an embedded GravityView; use the embedded post's ID as the base. |
||
| 459 | if( GravityView_frontend::getInstance()->isPostHasShortcode() && is_a( $post, 'WP_Post' ) ) { |
||
| 460 | |||
| 461 | $post_id = $post->ID; |
||
| 462 | |||
| 463 | } elseif( $gravityview_view->getViewId() ) { |
||
| 464 | |||
| 465 | // The GravityView has been embedded in a widget or in a template, and |
||
| 466 | // is not in the current content. Thus, we defer to the View's own ID. |
||
| 467 | $post_id = $gravityview_view->getViewId(); |
||
| 468 | |||
| 469 | } |
||
|
1 ignored issue
–
show
|
|||
| 470 | |||
| 471 | } |
||
|
1 ignored issue
–
show
|
|||
| 472 | |||
| 473 | } |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | // No post ID, get outta here. |
||
| 478 | if( empty( $post_id ) ) { |
||
| 479 | return NULL; |
||
| 480 | } |
||
| 481 | |||
| 482 | // If we've saved the permalink in memory, use it |
||
| 483 | // @since 1.3 |
||
| 484 | $link = wp_cache_get( 'gv_directory_link_'.$post_id ); |
||
| 485 | |||
| 486 | if( empty( $link ) ) { |
||
| 487 | |||
| 488 | $link = get_permalink( $post_id ); |
||
| 489 | |||
| 490 | // If not yet saved, cache the permalink. |
||
| 491 | // @since 1.3 |
||
| 492 | wp_cache_set( 'gv_directory_link_'.$post_id, $link ); |
||
| 493 | |||
| 494 | } |
||
| 495 | |||
| 496 | // Deal with returning to proper pagination for embedded views |
||
| 497 | if( $link && $add_query_args ) { |
||
| 498 | |||
| 499 | $args = array(); |
||
| 500 | |||
| 501 | if( $pagenum = rgget('pagenum') ) { |
||
| 502 | $args['pagenum'] = intval( $pagenum ); |
||
| 503 | } |
||
| 504 | |||
| 505 | if( $sort = rgget('sort') ) { |
||
| 506 | $args['sort'] = $sort; |
||
| 507 | $args['dir'] = rgget('dir'); |
||
| 508 | } |
||
| 509 | |||
| 510 | $link = add_query_arg( $args, $link ); |
||
| 511 | } |
||
| 512 | |||
| 513 | return $link; |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Calculate an *unique* hash for an entry based on the entry ID |
||
| 518 | * |
||
| 519 | * This allows you to be more discrete as to the number of the entry - if you don't want users to know that you have made a certain number of sales, for example, or that their entry in the giveaway is entry #3. |
||
| 520 | * |
||
| 521 | * The hashed value MUST be unique, otherwise multiple entries will share the same URL, which leads to obvious problems. |
||
| 522 | * |
||
| 523 | * @param int|string $id Entry ID to generate the hash for. |
||
| 524 | * @param array $entry Entry data passed to provide additional information when generating the hash. Optional - don't rely on it being available. |
||
| 525 | * @return string Hashed unique value for entry |
||
| 526 | */ |
||
| 527 | private static function get_custom_entry_slug( $id, $entry = array() ) { |
||
| 528 | |||
| 529 | // Generate an unique hash to use as the default value |
||
| 530 | $slug = substr( wp_hash( $id, 'gravityview'.$id ), 0, 8 ); |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @filter `gravityview_entry_slug` Modify the unique hash ID generated, if you want to improve usability or change the format. This will allow for custom URLs, such as `{entryid}-{first-name}` or even, if unique, `{first-name}-{last-name}` |
||
| 534 | * @param string $hash Existing hash generated by GravityView |
||
| 535 | * @param string $id The entry ID |
||
| 536 | * @param array $entry Entry data array. May be empty. |
||
| 537 | */ |
||
| 538 | $slug = apply_filters( 'gravityview_entry_slug', $slug, $id, $entry ); |
||
| 539 | |||
| 540 | // Make sure we have something - use the original ID as backup. |
||
| 541 | if( empty( $slug ) ) { |
||
| 542 | $slug = $id; |
||
| 543 | } |
||
| 544 | |||
| 545 | return sanitize_title( $slug ); |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get the entry slug for the entry. By default, it is the entry ID. |
||
| 550 | * |
||
| 551 | * |
||
| 552 | * @see gravityview_get_entry() |
||
| 553 | * @uses GravityView_API::get_custom_entry_slug() If using custom slug, gets the custom slug value |
||
| 554 | * @since 1.4 |
||
| 555 | * @param int|string $id_or_string ID of the entry, or custom slug string |
||
| 556 | * @param array $entry Gravity Forms Entry array, optional. Used only to provide data to customize the `gravityview_entry_slug` filter |
||
| 557 | * @return string Unique slug ID, passed through `sanitize_title()` |
||
| 558 | */ |
||
| 559 | public static function get_entry_slug( $id_or_string, $entry = array() ) { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Default: use the entry ID as the unique identifier |
||
| 563 | */ |
||
| 564 | $slug = $id_or_string; |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @filter `gravityview_custom_entry_slug` Whether to enable and use custom entry slugs. |
||
| 568 | * @param boolean True: Allow for slugs based on entry values. False: always use entry IDs (default) |
||
| 569 | */ |
||
| 570 | $custom = apply_filters('gravityview_custom_entry_slug', false ); |
||
| 571 | |||
| 572 | // If we're using custom slug... |
||
| 573 | if ( $custom ) { |
||
| 574 | |||
| 575 | // Get the entry hash |
||
| 576 | $hash = self::get_custom_entry_slug( $id_or_string, $entry ); |
||
| 577 | |||
| 578 | // See if the entry already has a hash set |
||
| 579 | $value = gform_get_meta( $id_or_string, 'gravityview_unique_id' ); |
||
| 580 | |||
| 581 | // If it does have a hash set, and the hash is expected, use it. |
||
| 582 | // This check allows users to change the hash structure using the |
||
| 583 | // gravityview_entry_hash filter and have the old hashes expire. |
||
| 584 | if( empty( $value ) || $value !== $hash ) { |
||
| 585 | |||
| 586 | gform_update_meta( $id_or_string, 'gravityview_unique_id', $hash ); |
||
| 587 | |||
| 588 | } |
||
| 589 | |||
| 590 | $slug = $hash; |
||
| 591 | |||
| 592 | unset( $value, $hash ); |
||
| 593 | } |
||
| 594 | |||
| 595 | return sanitize_title( $slug ); |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * If using the entry custom slug feature, make sure the new entries have the custom slug created and saved as meta |
||
| 600 | * |
||
| 601 | * Triggered by add_action( 'gform_entry_created', array( 'GravityView_API', 'entry_create_custom_slug' ), 10, 2 ); |
||
| 602 | * |
||
| 603 | * @param $entry array Gravity Forms entry object |
||
| 604 | * @param $form array Gravity Forms form object |
||
| 605 | */ |
||
| 606 | public static function entry_create_custom_slug( $entry, $form ) { |
||
| 607 | /** |
||
| 608 | * @filter `gravityview_custom_entry_slug` On entry creation, check if we are using the custom entry slug feature and update the meta |
||
| 609 | * @param boolean $custom Should we process the custom entry slug? |
||
| 610 | */ |
||
| 611 | $custom = apply_filters( 'gravityview_custom_entry_slug', false ); |
||
| 612 | if( $custom ) { |
||
| 613 | // create the gravityview_unique_id and save it |
||
| 614 | |||
| 615 | // Get the entry hash |
||
| 616 | $hash = self::get_custom_entry_slug( $entry['id'], $entry ); |
||
| 617 | gform_update_meta( $entry['id'], 'gravityview_unique_id', $hash ); |
||
| 618 | |||
| 619 | } |
||
| 620 | } |
||
| 621 | |||
| 622 | |||
| 623 | |||
| 624 | |||
| 625 | /** |
||
| 626 | * return href for single entry |
||
| 627 | * @param array|int $entry Entry array or entry ID |
||
| 628 | * @param int|null $post_id If wanting to define the parent post, pass a post ID |
||
| 629 | * @param boolean $add_directory_args True: Add args to help return to directory; False: only include args required to get to entry {@since 1.7.3} |
||
| 630 | * @return string Link to the entry with the directory parent slug |
||
| 631 | */ |
||
| 632 | public static function entry_link( $entry, $post_id = NULL, $add_directory_args = true ) { |
||
| 707 | |||
| 708 | |||
| 709 | } |
||
| 710 | |||
| 711 | |||
| 712 | // inside loop functions |
||
| 713 | |||
| 714 | function gv_label( $field, $entry = NULL ) { |
||
| 715 | return GravityView_API::field_label( $field, $entry ); |
||
| 716 | } |
||
| 717 | |||
| 718 | function gv_class( $field, $form = NULL, $entry = array() ) { |
||
| 1270 |