Make Administrative Fields Visible on Edit

Make administrative fields visible when editing via Entry Blocks. Includes support for Nested Forms.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gpeb-make-administrative-fields-visible-on-edit.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 * Gravity Perks // Entry Blocks // Make Administrative Fields Visible on Edit
 * https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
 *
 * Make administrative fields visible when editing via Entry Blocks. Includes support for Nested Forms.
 */
class GPEB_Editable_Admin_Fields {

	private static $instance;

	public static function get_instance() {

		if ( ! self::$instance ) {
			self::$instance = new self;
		}

		return self::$instance;
	}

	private function __construct() {

		add_filter( 'gform_pre_render', array( $this, 'set_field_visbility_on_edit' ) );
		add_filter( 'gpnf_init_script_args', array( $this, 'add_gpep_context_for_gpnf_ajax_requests' ) );

	}

	public function set_field_visbility_on_edit( $form ) {

		if ( ! $this->is_edit_entry_context( $form['id'] ) ) {
			return $form;
		}

		/**
		 * Allow conditional control over whether admin fields should be made visible.
		 *
		 * @param bool  $should_make_visible Default true.
		 * @param array $form
		 */
		$should_make_visible = apply_filters( 'gpeb_eaf_visibility', true, $form );

		if ( ! $should_make_visible ) {
			return $form;
		}

		foreach ( $form['fields'] as &$field ) {
			if ( $field->visibility === 'administrative' ) {
				$field->visibility = 'visible';
			}
		}

		return $form;
	}

	public function add_gpep_context_for_gpnf_ajax_requests( $args ) {
		$payload    = array();
		$block_uuid = $this->get_edit_block_uuid( $args['formId'] );
		if ( $block_uuid ) {
			$payload['uuid']     = $block_uuid;
			$payload['entry_id'] = $this->get_edit_block_entry( $args['formId'] );
			$payload['nonce']    = wp_create_nonce( $this->get_edit_block_nonce_action( $payload['uuid'], $payload['entry_id'] ) );
		}
		$args['ajaxContext']['gpebEditEntry'] = $payload;
		return $args;
	}

	public function is_edit_entry_context( $form_id ) {

		$block_uuid = $this->get_edit_block_uuid( $form_id );
		if ( $block_uuid ) {
			return true;
		}

		if ( ! defined( 'DOING_AJAX' ) ) {
			return false;
		}

		$action = rgpost( 'action' );
		if ( ! in_array( $action, array( 'gpnf_edit_entry', 'gpnf_refresh_markup' ) ) ) {
			return false;
		}

		$payload = rgars( $_REQUEST, 'gpnf_context/gpebEditEntry' );
		if ( ! $payload || ! wp_verify_nonce( $payload['nonce'], $this->get_edit_block_nonce_action( $payload['uuid'], $payload['entry_id'] ) ) ) {
			return false;
		}

		// Additional security not required for adding new child entries.
		if ( rgpost( 'action' ) === 'gpnf_refresh_markup' ) {
			return true;
		}

		$child_entry  = GFAPI::get_entry( gp_nested_forms()->get_posted_entry_id() );
		$parent_entry = GFAPI::get_entry( rgar( $child_entry, 'gpnf_entry_parent' ) );
		if ( $parent_entry['id'] == $payload['entry_id'] ) {
			return true;
		}

		return false;
	}

	public function get_edit_queryer( $form_id ) {
		if ( method_exists( 'GP_Entry_Blocks\GF_Queryer', 'attach_to_current_block' ) ) {
			$gpeb_queryer = GP_Entry_Blocks\GF_Queryer::attach_to_current_block();
			if ( $gpeb_queryer && $gpeb_queryer->is_edit_entry() && $gpeb_queryer->form_id == $form_id ) {
				return $gpeb_queryer;
			}
		}
		return false;
	}

	public function get_edit_block_uuid( $form_id ) {
		$gpeb_queryer = $this->get_edit_queryer( $form_id );
		if ( $gpeb_queryer ) {
			return $gpeb_queryer->block_context['gp-entry-blocks/uuid'];
		}
	}

	public function get_edit_block_entry( $form_id ) {
		$gpeb_queryer = $this->get_edit_queryer( $form_id );
		if ( $gpeb_queryer ) {
			return $gpeb_queryer->entry['id'];
		}
	}

	public function get_edit_block_nonce_action( $block_uuid, $entry_id ) {
		return implode( '/', array( 'gpeb_edit_entry', $block_uuid, $entry_id ) );
	}

}

function gpeb_editable_admin_fields() {
	return GPEB_Editable_Admin_Fields::get_instance();
}

gpeb_editable_admin_fields();

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.