Complex classes like GravityView_Edit_Entry_Render 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_Edit_Entry_Render, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class GravityView_Edit_Entry_Render { |
||
17 | |||
18 | /** |
||
19 | * @var GravityView_Edit_Entry |
||
20 | */ |
||
21 | protected $loader; |
||
22 | |||
23 | /** |
||
24 | * @var string String used to generate unique nonce for the entry/form/view combination. Allows access to edit page. |
||
25 | */ |
||
26 | static $nonce_key; |
||
27 | |||
28 | /** |
||
29 | * @since 1.9 |
||
30 | * @var string String used for check valid edit entry form submission. Allows saving edit form values. |
||
31 | */ |
||
32 | private static $nonce_field = 'is_gv_edit_entry'; |
||
33 | |||
34 | /** |
||
35 | * @since 1.9 |
||
36 | * @var bool Whether to allow save and continue functionality |
||
37 | */ |
||
38 | private static $supports_save_and_continue = false; |
||
39 | |||
40 | /** |
||
41 | * Gravity Forms entry array |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | public $entry; |
||
46 | |||
47 | /** |
||
48 | * Gravity Forms entry array (it won't get changed during this class lifecycle) |
||
49 | * @since 1.17.2 |
||
50 | * @var array |
||
51 | */ |
||
52 | private static $original_entry = array(); |
||
53 | |||
54 | /** |
||
55 | * Gravity Forms form array (GravityView modifies the content through this class lifecycle) |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | public $form; |
||
60 | |||
61 | /** |
||
62 | * Gravity Forms form array (it won't get changed during this class lifecycle) |
||
63 | * @since 1.16.2.1 |
||
64 | * @var array |
||
65 | */ |
||
66 | private static $original_form; |
||
67 | |||
68 | /** |
||
69 | * Gravity Forms form array after the form validation process |
||
70 | * @since 1.13 |
||
71 | * @var array |
||
72 | */ |
||
73 | public $form_after_validation = null; |
||
74 | |||
75 | /** |
||
76 | * Hold an array of GF field objects that have calculation rules |
||
77 | * @var array |
||
78 | */ |
||
79 | public $fields_with_calculation = array(); |
||
80 | |||
81 | /** |
||
82 | * Gravity Forms form id |
||
83 | * |
||
84 | * @var int |
||
85 | */ |
||
86 | public $form_id; |
||
87 | |||
88 | /** |
||
89 | * ID of the current view |
||
90 | * |
||
91 | * @var int |
||
92 | */ |
||
93 | public $view_id; |
||
94 | |||
95 | /** |
||
96 | * Updated entry is valid (GF Validation object) |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | public $is_valid = NULL; |
||
101 | |||
102 | 3 | function __construct( GravityView_Edit_Entry $loader ) { |
|
105 | |||
106 | 3 | function load() { |
|
134 | |||
135 | /** |
||
136 | * Don't show any forms embedded on a page when GravityView is in Edit Entry mode |
||
137 | * |
||
138 | * Adds a `__return_empty_string` filter on the Gravity Forms shortcode on the `wp_head` action |
||
139 | * And then removes it on the `wp_footer` action |
||
140 | * |
||
141 | * @since 1.16.1 |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | 1 | public function prevent_render_form() { |
|
154 | |||
155 | /** |
||
156 | * Because we're mimicking being a front-end Gravity Forms form while using a Gravity Forms |
||
157 | * backend form, we need to prevent them from saving twice. |
||
158 | * @return void |
||
159 | */ |
||
160 | public function prevent_maybe_process_form() { |
||
171 | |||
172 | /** |
||
173 | * Is the current page an Edit Entry page? |
||
174 | * @return boolean |
||
175 | */ |
||
176 | 4 | public function is_edit_entry() { |
|
182 | |||
183 | /** |
||
184 | * Is the current page an Edit Entry page? |
||
185 | * @since 1.9 |
||
186 | * @return boolean |
||
187 | */ |
||
188 | 3 | public function is_edit_entry_submission() { |
|
191 | |||
192 | /** |
||
193 | * When Edit entry view is requested setup the vars |
||
194 | */ |
||
195 | 3 | private function setup_vars() { |
|
210 | |||
211 | |||
212 | /** |
||
213 | * Load required files and trigger edit flow |
||
214 | * |
||
215 | * Run when the is_edit_entry returns true. |
||
216 | * |
||
217 | * @param GravityView_View_Data $gv_data GravityView Data object |
||
218 | * @return void |
||
219 | */ |
||
220 | 4 | public function init( $gv_data ) { |
|
247 | |||
248 | |||
249 | /** |
||
250 | * Force Gravity Forms to output scripts as if it were in the admin |
||
251 | * @return void |
||
252 | */ |
||
253 | 3 | private function print_scripts() { |
|
263 | |||
264 | |||
265 | /** |
||
266 | * Process edit entry form save |
||
267 | */ |
||
268 | 4 | private function process_save() { |
|
347 | |||
348 | /** |
||
349 | * Delete the value of fields hidden by conditional logic when the entry is edited |
||
350 | * |
||
351 | * @uses GFFormsModel::update_lead_field_value() |
||
352 | * |
||
353 | * @since 1.17.4 |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | 3 | private function unset_hidden_field_values() { |
|
358 | 3 | global $wpdb; |
|
359 | |||
360 | /** |
||
361 | * @filter `gravityview/edit_entry/unset_hidden_field_values` Whether to delete values of fields hidden by conditional logic |
||
362 | * @since 1.22.2 |
||
363 | * @param bool $unset_hidden_field_values Default: true |
||
364 | * @param GravityView_Edit_Entry_Render $this This object |
||
365 | */ |
||
366 | 3 | $unset_hidden_field_values = apply_filters( 'gravityview/edit_entry/unset_hidden_field_values', true, $this ); |
|
367 | |||
368 | 3 | if( ! $unset_hidden_field_values ) { |
|
369 | return; |
||
370 | } |
||
371 | |||
372 | 3 | if ( version_compare( GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) && method_exists( 'GFFormsModel', 'get_entry_meta_table_name' ) ) { |
|
373 | $entry_meta_table = GFFormsModel::get_entry_meta_table_name(); |
||
374 | $current_fields = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $entry_meta_table WHERE entry_id=%d", $this->entry['id'] ) ); |
||
375 | } else { |
||
376 | 3 | $lead_detail_table = GFFormsModel::get_lead_details_table_name(); |
|
377 | 3 | $current_fields = $wpdb->get_results( $wpdb->prepare( "SELECT id, field_number FROM $lead_detail_table WHERE lead_id=%d", $this->entry['id'] ) ); |
|
378 | } |
||
379 | |||
380 | 3 | foreach ( $this->entry as $input_id => $field_value ) { |
|
381 | |||
382 | 3 | $field = RGFormsModel::get_field( $this->form, $input_id ); |
|
383 | |||
384 | // Reset fields that are hidden |
||
385 | // Don't pass $entry as fourth parameter; force using $_POST values to calculate conditional logic |
||
386 | 3 | if ( GFFormsModel::is_field_hidden( $this->form, $field, array(), NULL ) ) { |
|
387 | |||
388 | // List fields are stored as empty arrays when empty |
||
389 | $empty_value = $this->is_field_json_encoded( $field ) ? '[]' : ''; |
||
390 | |||
391 | $lead_detail_id = GFFormsModel::get_lead_detail_id( $current_fields, $input_id ); |
||
392 | |||
393 | GFFormsModel::update_lead_field_value( $this->form, $this->entry, $field, $lead_detail_id, $input_id, $empty_value ); |
||
394 | |||
395 | // Prevent the $_POST values of hidden fields from being used as default values when rendering the form |
||
396 | // after submission |
||
397 | $post_input_id = 'input_' . str_replace( '.', '_', $input_id ); |
||
398 | 3 | $_POST[ $post_input_id ] = ''; |
|
399 | } |
||
400 | } |
||
401 | 3 | } |
|
402 | |||
403 | /** |
||
404 | * Have GF handle file uploads |
||
405 | * |
||
406 | * Copy of code from GFFormDisplay::process_form() |
||
407 | * |
||
408 | * @param int $form_id |
||
409 | */ |
||
410 | 3 | private function process_save_process_files( $form_id ) { |
|
426 | |||
427 | /** |
||
428 | * Make sure the fileuploads are not overwritten if no such request was done. |
||
429 | * |
||
430 | * TO ONLY BE USED INTERNALLY; DO NOT DEVELOP ON; MAY BE REMOVED AT ANY TIME. |
||
431 | * |
||
432 | * @since 1.20.1 |
||
433 | * |
||
434 | * @param string $value Field value |
||
435 | * @param array $entry GF entry array |
||
436 | * @param GF_Field_FileUpload $field |
||
437 | * @param array $form GF form array |
||
438 | * @param string $input_id ID of the input being saved |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | 3 | public function save_field_value( $value = '', $entry = array(), $field = null, $form = array(), $input_id = '' ) { |
|
465 | |||
466 | /** |
||
467 | * Remove max_files validation (done on gravityforms.js) to avoid conflicts with GravityView |
||
468 | * Late validation done on self::custom_validation |
||
469 | * |
||
470 | * @param $plupload_init array Plupload settings |
||
471 | * @param $form_id |
||
472 | * @param $instance |
||
473 | * @return mixed |
||
474 | */ |
||
475 | 1 | public function modify_fileupload_settings( $plupload_init, $form_id, $instance ) { |
|
484 | |||
485 | |||
486 | /** |
||
487 | * Set visibility to visible and convert field input key to string |
||
488 | * @return array $form |
||
489 | */ |
||
490 | 3 | private function form_prepare_for_save() { |
|
517 | |||
518 | 3 | private function update_calculation_fields() { |
|
571 | |||
572 | /** |
||
573 | * Handle updating the Post Image field |
||
574 | * |
||
575 | * Sets a new Featured Image if configured in Gravity Forms; otherwise uploads/updates media |
||
576 | * |
||
577 | * @since 1.17 |
||
578 | * |
||
579 | * @uses GFFormsModel::media_handle_upload |
||
580 | * @uses set_post_thumbnail |
||
581 | * |
||
582 | * @param array $form GF Form array |
||
583 | * @param GF_Field $field GF Field |
||
584 | * @param string $field_id Numeric ID of the field |
||
585 | * @param string $value |
||
586 | * @param array $entry GF Entry currently being edited |
||
587 | * @param int $post_id ID of the Post being edited |
||
588 | * |
||
589 | * @return mixed|string |
||
590 | */ |
||
591 | 1 | private function update_post_image( $form, $field, $field_id, $value, $entry, $post_id ) { |
|
592 | |||
593 | 1 | $input_name = 'input_' . $field_id; |
|
594 | |||
595 | 1 | if ( !empty( $_FILES[ $input_name ]['name'] ) ) { |
|
596 | |||
597 | // We have a new image |
||
598 | |||
599 | $value = RGFormsModel::prepare_value( $form, $field, $value, $input_name, $entry['id'] ); |
||
600 | |||
601 | $ary = ! empty( $value ) ? explode( '|:|', $value ) : array(); |
||
602 | $ary = stripslashes_deep( $ary ); |
||
603 | $img_url = rgar( $ary, 0 ); |
||
604 | |||
605 | $img_title = count( $ary ) > 1 ? $ary[1] : ''; |
||
606 | $img_caption = count( $ary ) > 2 ? $ary[2] : ''; |
||
607 | $img_description = count( $ary ) > 3 ? $ary[3] : ''; |
||
608 | |||
609 | $image_meta = array( |
||
610 | 'post_excerpt' => $img_caption, |
||
611 | 'post_content' => $img_description, |
||
612 | ); |
||
613 | |||
614 | //adding title only if it is not empty. It will default to the file name if it is not in the array |
||
615 | if ( ! empty( $img_title ) ) { |
||
616 | $image_meta['post_title'] = $img_title; |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * todo: As soon as \GFFormsModel::media_handle_upload becomes a public method, move this call to \GFFormsModel::media_handle_upload and remove the hack from this class. |
||
621 | * Note: the method became public in GF 1.9.17.7, but we don't require that version yet. |
||
622 | */ |
||
623 | $media_id = GravityView_GFFormsModel::media_handle_upload( $img_url, $post_id, $image_meta ); |
||
624 | |||
625 | // is this field set as featured image? |
||
626 | if ( $media_id && $field->postFeaturedImage ) { |
||
627 | set_post_thumbnail( $post_id, $media_id ); |
||
628 | } |
||
629 | |||
630 | 1 | } elseif ( !empty( $_POST[ $input_name ] ) && is_array( $value ) ) { |
|
631 | |||
632 | 1 | $img_url = rgpost( $input_name ); |
|
633 | 1 | $img_title = rgpost( $input_name . '_1' ); |
|
634 | 1 | $img_caption = rgpost( $input_name . '_4' ); |
|
635 | 1 | $img_description = rgpost( $input_name . '_7' ); |
|
636 | |||
637 | 1 | $value = ! empty( $img_url ) ? $img_url . "|:|" . $img_title . "|:|" . $img_caption . "|:|" . $img_description : ''; |
|
638 | |||
639 | 1 | if ( $field->postFeaturedImage ) { |
|
640 | |||
641 | $image_meta = array( |
||
642 | 1 | 'ID' => get_post_thumbnail_id( $post_id ), |
|
643 | 1 | 'post_title' => $img_title, |
|
644 | 1 | 'post_excerpt' => $img_caption, |
|
645 | 1 | 'post_content' => $img_description, |
|
646 | ); |
||
647 | |||
648 | // update image title, caption or description |
||
649 | 1 | wp_update_post( $image_meta ); |
|
650 | } |
||
651 | } else { |
||
652 | |||
653 | // if we get here, image was removed or not set. |
||
654 | $value = ''; |
||
655 | |||
656 | if ( $field->postFeaturedImage ) { |
||
657 | delete_post_thumbnail( $post_id ); |
||
658 | } |
||
659 | } |
||
660 | |||
661 | 1 | return $value; |
|
662 | } |
||
663 | |||
664 | /** |
||
665 | * Loop through the fields being edited and if they include Post fields, update the Entry's post object |
||
666 | * |
||
667 | * @param array $form Gravity Forms form |
||
668 | * |
||
669 | * @return void |
||
670 | */ |
||
671 | 3 | private function maybe_update_post_fields( $form ) { |
|
672 | |||
673 | 3 | if( empty( $this->entry['post_id'] ) ) { |
|
674 | 2 | do_action( 'gravityview_log_debug', __METHOD__ . ': This entry has no post fields. Continuing...' ); |
|
675 | 2 | return; |
|
676 | } |
||
677 | |||
678 | 1 | $post_id = $this->entry['post_id']; |
|
679 | |||
680 | // Security check |
||
681 | 1 | if( false === GVCommon::has_cap( 'edit_post', $post_id ) ) { |
|
682 | do_action( 'gravityview_log_error', 'The current user does not have the ability to edit Post #'.$post_id ); |
||
683 | return; |
||
684 | } |
||
685 | |||
686 | 1 | $update_entry = false; |
|
687 | |||
688 | 1 | $updated_post = $original_post = get_post( $post_id ); |
|
689 | |||
690 | 1 | foreach ( $this->entry as $field_id => $value ) { |
|
691 | |||
692 | 1 | $field = RGFormsModel::get_field( $form, $field_id ); |
|
693 | |||
694 | 1 | if( ! $field ) { |
|
695 | 1 | continue; |
|
696 | } |
||
697 | |||
698 | 1 | if( GFCommon::is_post_field( $field ) && 'post_category' !== $field->type ) { |
|
699 | |||
700 | // Get the value of the field, including $_POSTed value |
||
701 | 1 | $value = RGFormsModel::get_field_value( $field ); |
|
702 | |||
703 | // Use temporary entry variable, to make values available to fill_post_template() and update_post_image() |
||
704 | 1 | $entry_tmp = $this->entry; |
|
705 | 1 | $entry_tmp["{$field_id}"] = $value; |
|
706 | |||
707 | 1 | switch( $field->type ) { |
|
708 | |||
709 | 1 | case 'post_title': |
|
710 | $post_title = $value; |
||
711 | if( rgar( $form, 'postTitleTemplateEnabled' ) ) { |
||
712 | $post_title = $this->fill_post_template( $form['postTitleTemplate'], $form, $entry_tmp ); |
||
713 | } |
||
714 | $updated_post->post_title = $post_title; |
||
715 | $updated_post->post_name = $post_title; |
||
716 | unset( $post_title ); |
||
717 | break; |
||
718 | |||
719 | 1 | case 'post_content': |
|
720 | $post_content = $value; |
||
721 | if( rgar( $form, 'postContentTemplateEnabled' ) ) { |
||
722 | $post_content = $this->fill_post_template( $form['postContentTemplate'], $form, $entry_tmp, true ); |
||
723 | } |
||
724 | $updated_post->post_content = $post_content; |
||
725 | unset( $post_content ); |
||
726 | break; |
||
727 | 1 | case 'post_excerpt': |
|
728 | $updated_post->post_excerpt = $value; |
||
729 | break; |
||
730 | 1 | case 'post_tags': |
|
731 | wp_set_post_tags( $post_id, $value, false ); |
||
732 | break; |
||
733 | 1 | case 'post_category': |
|
734 | break; |
||
735 | 1 | case 'post_custom_field': |
|
736 | if ( is_array( $value ) && ( floatval( $field_id ) !== floatval( $field->id ) ) ) { |
||
737 | $value = $value[ $field_id ]; |
||
738 | } |
||
739 | |||
740 | if( ! empty( $field->customFieldTemplateEnabled ) ) { |
||
741 | $value = $this->fill_post_template( $field->customFieldTemplate, $form, $entry_tmp, true ); |
||
742 | } |
||
743 | |||
744 | if ( $this->is_field_json_encoded( $field ) && ! is_string( $value ) ) { |
||
745 | $value = function_exists('wp_json_encode') ? wp_json_encode( $value ) : json_encode( $value ); |
||
746 | } |
||
747 | |||
748 | update_post_meta( $post_id, $field->postCustomFieldName, $value ); |
||
749 | break; |
||
750 | |||
751 | 1 | case 'post_image': |
|
752 | 1 | $value = $this->update_post_image( $form, $field, $field_id, $value, $this->entry, $post_id ); |
|
753 | 1 | break; |
|
754 | |||
755 | } |
||
756 | |||
757 | // update entry after |
||
758 | 1 | $this->entry["{$field_id}"] = $value; |
|
759 | |||
760 | 1 | $update_entry = true; |
|
761 | |||
762 | 1 | unset( $entry_tmp ); |
|
763 | } |
||
764 | |||
765 | } |
||
766 | |||
767 | 1 | if( $update_entry ) { |
|
768 | |||
769 | 1 | $return_entry = GFAPI::update_entry( $this->entry ); |
|
770 | |||
771 | 1 | if( is_wp_error( $return_entry ) ) { |
|
772 | do_action( 'gravityview_log_error', 'Updating the entry post fields failed', array( '$this->entry' => $this->entry, '$return_entry' => $return_entry ) ); |
||
773 | } else { |
||
774 | 1 | do_action( 'gravityview_log_debug', 'Updating the entry post fields for post #'.$post_id.' succeeded' ); |
|
775 | } |
||
776 | |||
777 | } |
||
778 | |||
779 | 1 | $return_post = wp_update_post( $updated_post, true ); |
|
780 | |||
781 | 1 | if( is_wp_error( $return_post ) ) { |
|
782 | $return_post->add_data( $updated_post, '$updated_post' ); |
||
783 | do_action( 'gravityview_log_error', 'Updating the post content failed', compact( 'updated_post', 'return_post' ) ); |
||
784 | } else { |
||
785 | 1 | do_action( 'gravityview_log_debug', 'Updating the post content for post #'.$post_id.' succeeded', $updated_post ); |
|
786 | } |
||
787 | 1 | } |
|
788 | |||
789 | /** |
||
790 | * Is the field stored in a JSON-encoded manner? |
||
791 | * |
||
792 | * @param GF_Field $field |
||
793 | * |
||
794 | * @return bool True: stored in DB json_encode()'d; False: not encoded |
||
795 | */ |
||
796 | private function is_field_json_encoded( $field ) { |
||
813 | |||
814 | /** |
||
815 | * Convert a field content template into prepared output |
||
816 | * |
||
817 | * @uses GravityView_GFFormsModel::get_post_field_images() |
||
818 | * |
||
819 | * @since 1.17 |
||
820 | * |
||
821 | * @param string $template The content template for the field |
||
822 | * @param array $form Gravity Forms form |
||
823 | * @param bool $do_shortcode Whether to process shortcode inside content. In GF, only run on Custom Field and Post Content fields |
||
824 | * |
||
825 | * @return string |
||
826 | */ |
||
827 | private function fill_post_template( $template, $form, $entry, $do_shortcode = false ) { |
||
846 | |||
847 | |||
848 | /** |
||
849 | * Perform actions normally performed after updating a lead |
||
850 | * |
||
851 | * @since 1.8 |
||
852 | * |
||
853 | * @see GFEntryDetail::lead_detail_page() |
||
854 | * |
||
855 | * @return void |
||
856 | */ |
||
857 | 3 | private function after_update() { |
|
858 | |||
859 | 3 | do_action( 'gform_after_update_entry', $this->form, $this->entry['id'], self::$original_entry ); |
|
860 | 3 | do_action( "gform_after_update_entry_{$this->form['id']}", $this->form, $this->entry['id'], self::$original_entry ); |
|
861 | |||
862 | // Re-define the entry now that we've updated it. |
||
863 | 3 | $entry = RGFormsModel::get_lead( $this->entry['id'] ); |
|
864 | |||
865 | 3 | $entry = GFFormsModel::set_entry_meta( $entry, $this->form ); |
|
866 | |||
867 | 3 | if ( version_compare( GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '<' ) ) { |
|
868 | // We need to clear the cache because Gravity Forms caches the field values, which |
||
869 | // we have just updated. |
||
870 | 3 | foreach ($this->form['fields'] as $key => $field) { |
|
871 | 3 | GFFormsModel::refresh_lead_field_value( $entry['id'], $field->id ); |
|
872 | } |
||
873 | } |
||
874 | |||
875 | 3 | $this->entry = $entry; |
|
876 | 3 | } |
|
877 | |||
878 | |||
879 | /** |
||
880 | * Display the Edit Entry form |
||
881 | * |
||
882 | * @return void |
||
883 | */ |
||
884 | 3 | public function edit_entry_form() { |
|
885 | |||
886 | ?> |
||
887 | |||
888 | <div class="gv-edit-entry-wrapper"><?php |
||
889 | |||
890 | 3 | $javascript = gravityview_ob_include( GravityView_Edit_Entry::$file .'/partials/inline-javascript.php', $this ); |
|
891 | |||
892 | /** |
||
893 | * Fixes weird wpautop() issue |
||
894 | * @see https://github.com/katzwebservices/GravityView/issues/451 |
||
895 | */ |
||
896 | 3 | echo gravityview_strip_whitespace( $javascript ); |
|
897 | |||
898 | ?><h2 class="gv-edit-entry-title"> |
||
899 | <span><?php |
||
900 | |||
901 | /** |
||
902 | * @filter `gravityview_edit_entry_title` Modify the edit entry title |
||
903 | * @param string $edit_entry_title Modify the "Edit Entry" title |
||
904 | * @param GravityView_Edit_Entry_Render $this This object |
||
905 | */ |
||
906 | 3 | $edit_entry_title = apply_filters('gravityview_edit_entry_title', __('Edit Entry', 'gravityview'), $this ); |
|
907 | |||
908 | 3 | echo esc_attr( $edit_entry_title ); |
|
909 | ?></span> |
||
910 | </h2> |
||
911 | |||
912 | <?php $this->maybe_print_message(); ?> |
||
913 | |||
914 | <?php // The ID of the form needs to be `gform_{form_id}` for the pluploader ?> |
||
915 | |||
916 | <form method="post" id="gform_<?php echo $this->form_id; ?>" enctype="multipart/form-data"> |
||
917 | |||
918 | <?php |
||
919 | |||
920 | 3 | wp_nonce_field( self::$nonce_key, self::$nonce_key ); |
|
921 | |||
922 | 3 | wp_nonce_field( self::$nonce_field, self::$nonce_field, false ); |
|
923 | |||
924 | // Print the actual form HTML |
||
925 | 3 | $this->render_edit_form(); |
|
926 | |||
927 | ?> |
||
928 | 3 | </form> |
|
929 | |||
930 | <script> |
||
931 | gform.addFilter('gform_reset_pre_conditional_logic_field_action', function ( reset, formId, targetId, defaultValues, isInit ) { |
||
932 | return false; |
||
933 | }); |
||
934 | </script> |
||
935 | |||
936 | </div> |
||
937 | |||
938 | <?php |
||
939 | 3 | } |
|
940 | |||
941 | /** |
||
942 | * Display success or error message if the form has been submitted |
||
943 | * |
||
944 | * @uses GVCommon::generate_notice |
||
945 | * |
||
946 | * @since 1.16.2.2 |
||
947 | * |
||
948 | * @return void |
||
949 | */ |
||
950 | 3 | private function maybe_print_message() { |
|
951 | |||
952 | 3 | if( rgpost('action') === 'update' ) { |
|
953 | |||
954 | $back_link = esc_url( remove_query_arg( array( 'page', 'view', 'edit' ) ) ); |
||
955 | |||
956 | if( ! $this->is_valid ){ |
||
957 | |||
958 | // Keeping this compatible with Gravity Forms. |
||
959 | $validation_message = "<div class='validation_error'>" . __('There was a problem with your submission.', 'gravityview') . " " . __('Errors have been highlighted below.', 'gravityview') . "</div>"; |
||
960 | $message = apply_filters("gform_validation_message_{$this->form['id']}", apply_filters("gform_validation_message", $validation_message, $this->form), $this->form); |
||
961 | |||
962 | echo GVCommon::generate_notice( $message , 'gv-error' ); |
||
963 | |||
964 | } else { |
||
965 | $entry_updated_message = sprintf( esc_attr__('Entry Updated. %sReturn to Entry%s', 'gravityview'), '<a href="'. $back_link .'">', '</a>' ); |
||
966 | |||
967 | /** |
||
968 | * @filter `gravityview/edit_entry/success` Modify the edit entry success message (including the anchor link) |
||
969 | * @since 1.5.4 |
||
970 | * @param string $entry_updated_message Existing message |
||
971 | * @param int $view_id View ID |
||
972 | * @param array $entry Gravity Forms entry array |
||
973 | * @param string $back_link URL to return to the original entry. @since 1.6 |
||
974 | */ |
||
975 | $message = apply_filters( 'gravityview/edit_entry/success', $entry_updated_message , $this->view_id, $this->entry, $back_link ); |
||
976 | |||
977 | echo GVCommon::generate_notice( $message ); |
||
978 | } |
||
979 | |||
980 | } |
||
981 | 3 | } |
|
982 | |||
983 | /** |
||
984 | * Display the Edit Entry form in the original Gravity Forms format |
||
985 | * |
||
986 | * @since 1.9 |
||
987 | * |
||
988 | * @return void |
||
989 | */ |
||
990 | 3 | private function render_edit_form() { |
|
991 | |||
992 | /** |
||
993 | * @action `gravityview/edit-entry/render/before` Before rendering the Edit Entry form |
||
994 | * @since 1.17 |
||
995 | * @param GravityView_Edit_Entry_Render $this |
||
996 | */ |
||
997 | 3 | do_action( 'gravityview/edit-entry/render/before', $this ); |
|
998 | |||
999 | 3 | add_filter( 'gform_pre_render', array( $this, 'filter_modify_form_fields'), 5000, 3 ); |
|
1000 | 3 | add_filter( 'gform_submit_button', array( $this, 'render_form_buttons') ); |
|
1001 | 3 | add_filter( 'gform_disable_view_counter', '__return_true' ); |
|
1002 | |||
1003 | 3 | add_filter( 'gform_field_input', array( $this, 'verify_user_can_edit_post' ), 5, 5 ); |
|
1004 | 3 | add_filter( 'gform_field_input', array( $this, 'modify_edit_field_input' ), 10, 5 ); |
|
1005 | |||
1006 | // We need to remove the fake $_GET['page'] arg to avoid rendering form as if in admin. |
||
1007 | 3 | unset( $_GET['page'] ); |
|
1008 | |||
1009 | // TODO: Verify multiple-page forms |
||
1010 | |||
1011 | 3 | ob_start(); // Prevent PHP warnings possibly caused by prefilling list fields for conditional logic |
|
1012 | |||
1013 | 3 | $html = GFFormDisplay::get_form( $this->form['id'], false, false, true, $this->entry ); |
|
1014 | |||
1015 | 3 | ob_get_clean(); |
|
1016 | |||
1017 | 3 | remove_filter( 'gform_pre_render', array( $this, 'filter_modify_form_fields' ), 5000 ); |
|
1018 | 3 | remove_filter( 'gform_submit_button', array( $this, 'render_form_buttons' ) ); |
|
1019 | 3 | remove_filter( 'gform_disable_view_counter', '__return_true' ); |
|
1020 | 3 | remove_filter( 'gform_field_input', array( $this, 'verify_user_can_edit_post' ), 5 ); |
|
1021 | 3 | remove_filter( 'gform_field_input', array( $this, 'modify_edit_field_input' ), 10 ); |
|
1022 | |||
1023 | 3 | echo $html; |
|
1024 | |||
1025 | /** |
||
1026 | * @action `gravityview/edit-entry/render/after` After rendering the Edit Entry form |
||
1027 | * @since 1.17 |
||
1028 | * @param GravityView_Edit_Entry_Render $this |
||
1029 | */ |
||
1030 | 3 | do_action( 'gravityview/edit-entry/render/after', $this ); |
|
1031 | 3 | } |
|
1032 | |||
1033 | /** |
||
1034 | * Display the Update/Cancel/Delete buttons for the Edit Entry form |
||
1035 | * @since 1.8 |
||
1036 | * @return string |
||
1037 | */ |
||
1038 | 3 | public function render_form_buttons() { |
|
1041 | |||
1042 | |||
1043 | /** |
||
1044 | * Modify the form fields that are shown when using GFFormDisplay::get_form() |
||
1045 | * |
||
1046 | * By default, all fields will be shown. We only want the Edit Tab configured fields to be shown. |
||
1047 | * |
||
1048 | * @param array $form |
||
1049 | * @param boolean $ajax Whether in AJAX mode |
||
1050 | * @param array|string $field_values Passed parameters to the form |
||
1051 | * |
||
1052 | * @since 1.9 |
||
1053 | * |
||
1054 | * @return array Modified form array |
||
1055 | */ |
||
1056 | 3 | public function filter_modify_form_fields( $form, $ajax = false, $field_values = '' ) { |
|
1076 | |||
1077 | /** |
||
1078 | * When displaying a field, check if it's a Post Field, and if so, make sure the post exists and current user has edit rights. |
||
1079 | * |
||
1080 | * @since 1.16.2.2 |
||
1081 | * |
||
1082 | * @param string $field_content Always empty. Returning not-empty overrides the input. |
||
1083 | * @param GF_Field $field |
||
1084 | * @param string|array $value If array, it's a field with multiple inputs. If string, single input. |
||
1085 | * @param int $lead_id Lead ID. Always 0 for the `gform_field_input` filter. |
||
1086 | * @param int $form_id Form ID |
||
1087 | * |
||
1088 | * @return string If error, the error message. If no error, blank string (modify_edit_field_input() runs next) |
||
1089 | */ |
||
1090 | 3 | public function verify_user_can_edit_post( $field_content = '', $field, $value, $lead_id = 0, $form_id ) { |
|
1120 | |||
1121 | /** |
||
1122 | * |
||
1123 | * Fill-in the saved values into the form inputs |
||
1124 | * |
||
1125 | * @param string $field_content Always empty. Returning not-empty overrides the input. |
||
1126 | * @param GF_Field $field |
||
1127 | * @param string|array $value If array, it's a field with multiple inputs. If string, single input. |
||
1128 | * @param int $lead_id Lead ID. Always 0 for the `gform_field_input` filter. |
||
1129 | * @param int $form_id Form ID |
||
1130 | * |
||
1131 | * @return mixed |
||
1132 | */ |
||
1133 | 3 | public function modify_edit_field_input( $field_content = '', $field, $value, $lead_id = 0, $form_id ) { |
|
1175 | |||
1176 | /** |
||
1177 | * Modify the value for the current field input |
||
1178 | * |
||
1179 | * @param GF_Field $field |
||
1180 | * |
||
1181 | * @return array|mixed|string |
||
1182 | */ |
||
1183 | 3 | private function get_field_value( $field ) { |
|
1263 | |||
1264 | |||
1265 | // ---- Entry validation |
||
1266 | |||
1267 | /** |
||
1268 | * Add field keys that Gravity Forms expects. |
||
1269 | * |
||
1270 | * @see GFFormDisplay::validate() |
||
1271 | * @param array $form GF Form |
||
1272 | * @return array Modified GF Form |
||
1273 | */ |
||
1274 | 3 | public function gform_pre_validation( $form ) { |
|
1366 | |||
1367 | |||
1368 | /** |
||
1369 | * Process validation for a edit entry submission |
||
1370 | * |
||
1371 | * Sets the `is_valid` object var |
||
1372 | * |
||
1373 | * @return void |
||
1374 | */ |
||
1375 | 4 | private function validate() { |
|
1417 | |||
1418 | /** |
||
1419 | * Make validation work for User Registration feeds. |
||
1420 | * |
||
1421 | * The default validation does a bit too much for our liking. |
||
1422 | * Email, username validation should work. Called on the `gform_validation` filter. |
||
1423 | * |
||
1424 | * @param array $validation_results The validation results. |
||
1425 | * @return array The modified validation results. |
||
1426 | */ |
||
1427 | 3 | public function user_registration_validation( $validation_results ) { |
|
1462 | |||
1463 | /** |
||
1464 | * Make validation work for Edit Entry |
||
1465 | * |
||
1466 | * Because we're calling the GFFormDisplay::validate() in an unusual way (as a front-end |
||
1467 | * form pretending to be a back-end form), validate() doesn't know we _can't_ edit post |
||
1468 | * fields. This goes through all the fields and if they're an invalid post field, we |
||
1469 | * set them as valid. If there are still issues, we'll return false. |
||
1470 | * |
||
1471 | * @param [type] $validation_results [description] |
||
1472 | * @return [type] [description] |
||
1473 | */ |
||
1474 | 4 | public function custom_validation( $validation_results ) { |
|
1598 | |||
1599 | |||
1600 | /** |
||
1601 | * TODO: This seems to be hacky... we should remove it. Entry is set when updating the form using setup_vars()! |
||
1602 | * Get the current entry and set it if it's not yet set. |
||
1603 | * @return array Gravity Forms entry array |
||
1604 | */ |
||
1605 | 3 | public function get_entry() { |
|
1614 | |||
1615 | |||
1616 | |||
1617 | // --- Filters |
||
1618 | |||
1619 | /** |
||
1620 | * Get the Edit Entry fields as configured in the View |
||
1621 | * |
||
1622 | * @since 1.8 |
||
1623 | * |
||
1624 | * @param int $view_id |
||
1625 | * |
||
1626 | * @return array Array of fields that are configured in the Edit tab in the Admin |
||
1627 | */ |
||
1628 | 4 | private function get_configured_edit_fields( $form, $view_id ) { |
|
1662 | |||
1663 | |||
1664 | /** |
||
1665 | * Filter area fields based on specified conditions |
||
1666 | * - This filter removes the fields that have calculation configured |
||
1667 | * |
||
1668 | * @uses GravityView_Edit_Entry::user_can_edit_field() Check caps |
||
1669 | * @access private |
||
1670 | * @param GF_Field[] $fields |
||
1671 | * @param array $configured_fields |
||
1672 | * @since 1.5 |
||
1673 | * @return array $fields |
||
1674 | */ |
||
1675 | 3 | private function filter_fields( $fields, $configured_fields ) { |
|
1723 | |||
1724 | /** |
||
1725 | * Override GF Form field properties with the ones defined on the View |
||
1726 | * @param GF_Field $field GF Form field object |
||
1727 | * @param array $field_setting GV field options |
||
1728 | * @since 1.5 |
||
1729 | * @return array|GF_Field |
||
1730 | */ |
||
1731 | private function merge_field_properties( $field, $field_setting ) { |
||
1754 | |||
1755 | /** |
||
1756 | * Remove fields that shouldn't be visible based on the Gravity Forms visibility field property |
||
1757 | * |
||
1758 | * @since 1.9.1 |
||
1759 | * |
||
1760 | * @param array|GF_Field[] $fields Gravity Forms form fields |
||
1761 | * @param array|null $edit_fields Fields for the Edit Entry tab configured in the View Configuration |
||
1762 | * @param array $form GF Form array |
||
1763 | * @param int $view_id View ID |
||
1764 | * |
||
1765 | * @return array Possibly modified form array |
||
1766 | */ |
||
1767 | 3 | private function filter_admin_only_fields( $fields = array(), $edit_fields = null, $form = array(), $view_id = 0 ) { |
|
1834 | |||
1835 | // --- Conditional Logic |
||
1836 | |||
1837 | /** |
||
1838 | * Conditional logic isn't designed to work with forms that already have content. When switching input values, |
||
1839 | * the dependent fields will be blank. |
||
1840 | * |
||
1841 | * Note: This is because GF populates a JavaScript variable with the input values. This is tough to filter at the input level; |
||
1842 | * via the `gform_field_value` filter; it requires lots of legwork. Doing it at the form level is easier. |
||
1843 | * |
||
1844 | * @since 1.17.4 |
||
1845 | * |
||
1846 | * @param array $form Gravity Forms array object |
||
1847 | * |
||
1848 | * @return array $form, modified to fix conditional |
||
1849 | */ |
||
1850 | 3 | function prefill_conditional_logic( $form ) { |
|
1898 | |||
1899 | /** |
||
1900 | * Remove the conditional logic rules from the form button and the form fields, if needed. |
||
1901 | * |
||
1902 | * @todo Merge with caller method |
||
1903 | * @since 1.9 |
||
1904 | * |
||
1905 | * @param array $form Gravity Forms form |
||
1906 | * @return array Modified form, if not using Conditional Logic |
||
1907 | */ |
||
1908 | 3 | private function filter_conditional_logic( $form ) { |
|
1932 | |||
1933 | /** |
||
1934 | * Disable the Gravity Forms conditional logic script and features on the Edit Entry screen |
||
1935 | * |
||
1936 | * @since 1.9 |
||
1937 | * |
||
1938 | * @param $has_conditional_logic |
||
1939 | * @param $form |
||
1940 | * @return mixed |
||
1941 | */ |
||
1942 | 3 | public function manage_conditional_logic( $has_conditional_logic, $form ) { |
|
1951 | |||
1952 | |||
1953 | // --- User checks and nonces |
||
1954 | |||
1955 | /** |
||
1956 | * Check if the user can edit the entry |
||
1957 | * |
||
1958 | * - Is the nonce valid? |
||
1959 | * - Does the user have the right caps for the entry |
||
1960 | * - Is the entry in the trash? |
||
1961 | * |
||
1962 | * @todo Move to GVCommon |
||
1963 | * |
||
1964 | * @param boolean $echo Show error messages in the form? |
||
1965 | * @return boolean True: can edit form. False: nope. |
||
1966 | */ |
||
1967 | 4 | private function user_can_edit_entry( $echo = false ) { |
|
2031 | |||
2032 | |||
2033 | /** |
||
2034 | * Check whether a field is editable by the current user, and optionally display an error message |
||
2035 | * @uses GravityView_Edit_Entry->check_user_cap_edit_field() Check user capabilities |
||
2036 | * @param array $field Field or field settings array |
||
2037 | * @param boolean $echo Whether to show error message telling user they aren't allowed |
||
2038 | * @return boolean True: user can edit the current field; False: nope, they can't. |
||
2039 | */ |
||
2040 | private function user_can_edit_field( $field, $echo = false ) { |
||
2062 | |||
2063 | |||
2064 | /** |
||
2065 | * checks if user has permissions to edit a specific field |
||
2066 | * |
||
2067 | * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_field for maximum security!! |
||
2068 | * |
||
2069 | * @param [type] $field [description] |
||
2070 | * @return bool |
||
2071 | */ |
||
2072 | private function check_user_cap_edit_field( $field ) { |
||
2088 | |||
2089 | |||
2090 | /** |
||
2091 | * Is the current nonce valid for editing the entry? |
||
2092 | * @return boolean |
||
2093 | */ |
||
2094 | 3 | public function verify_nonce() { |
|
2120 | |||
2121 | |||
2122 | /** |
||
2123 | * Multiselect in GF 2.2 became a json_encoded value. Fix it. |
||
2124 | * |
||
2125 | * As a hack for now we'll implode it back. |
||
2126 | */ |
||
2127 | public function fix_multiselect_value_serialization( $field_value, $field, $_this ) { |
||
2140 | |||
2141 | |||
2142 | |||
2143 | } //end class |
||
2144 |
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.