Skip to content

Commit 0f0121e

Browse files
authored
gpeb-make-administrative-fields-visible-on-edit.php: Added support for securely allowing administrative fields to be edited for child entries in a Nested Form field when editing a parent entry via Entry Blocks. (#676)
1 parent 7bf3599 commit 0f0121e

File tree

1 file changed

+108
-12
lines changed

1 file changed

+108
-12
lines changed

gp-entry-blocks/gpeb-make-administrative-fields-visible-on-edit.php

Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,122 @@
33
* Gravity Perks // Entry Blocks // Make Administrative Fields Visible on Edit
44
* https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
55
*
6-
* Make administrative fields visible when editing via Entry Blocks.
6+
* Make administrative fields visible when editing via Entry Blocks. Includes support for Nested Forms.
77
*/
8-
add_filter( 'gform_pre_render', 'gpeb_set_field_visbility_on_edit' );
9-
add_filter( 'gform_pre_process', 'gpeb_set_field_visbility_on_edit' );
8+
class GPEB_Editable_Admin_Fields {
109

11-
function gpeb_set_field_visbility_on_edit( $form ) {
10+
private static $instance;
1211

13-
$is_block = (bool) rgpost( 'gpeb_entry_id' );
14-
if ( ! $is_block ) {
15-
$is_block = class_exists( 'WP_Block_Supports' ) && rgar( WP_Block_Supports::$block_to_render, 'blockName' ) === 'gp-entry-blocks/edit-form';
16-
if ( ! $is_block ) {
12+
public static function get_instance() {
13+
14+
if ( ! self::$instance ) {
15+
self::$instance = new self;
16+
}
17+
18+
return self::$instance;
19+
}
20+
21+
private function __construct() {
22+
23+
add_filter( 'gform_pre_render', array( $this, 'set_field_visbility_on_edit' ) );
24+
add_filter( 'gpnf_init_script_args', array( $this, 'add_gpep_context_for_gpnf_ajax_requests' ) );
25+
26+
}
27+
28+
public function set_field_visbility_on_edit( $form ) {
29+
30+
if ( ! $this->is_edit_entry_context( $form['id'] ) ) {
1731
return $form;
1832
}
33+
34+
foreach ( $form['fields'] as &$field ) {
35+
if ( $field->visibility === 'administrative' ) {
36+
$field->visibility = 'visible';
37+
}
38+
}
39+
40+
return $form;
41+
}
42+
43+
public function add_gpep_context_for_gpnf_ajax_requests( $args ) {
44+
$payload = array();
45+
$block_uuid = $this->get_edit_block_uuid( $args['formId'] );
46+
if ( $block_uuid ) {
47+
$payload['uuid'] = $block_uuid;
48+
$payload['entry_id'] = $this->get_edit_block_entry( $args['formId'] );
49+
$payload['nonce'] = wp_create_nonce( $this->get_edit_block_nonce_action( $payload['uuid'], $payload['entry_id'] ) );
50+
}
51+
$args['ajaxContext']['gpebEditEntry'] = $payload;
52+
return $args;
53+
}
54+
55+
public function is_edit_entry_context( $form_id ) {
56+
57+
$block_uuid = $this->get_edit_block_uuid( $form_id );
58+
if ( $block_uuid ) {
59+
return true;
60+
}
61+
62+
if ( ! defined( 'DOING_AJAX' ) ) {
63+
return false;
64+
}
65+
66+
$action = rgpost( 'action' );
67+
if ( ! in_array( $action, array( 'gpnf_edit_entry', 'gpnf_refresh_markup' ) ) ) {
68+
return false;
69+
}
70+
71+
$payload = rgars( $_REQUEST, 'gpnf_context/gpebEditEntry' );
72+
if ( ! $payload || ! wp_verify_nonce( $payload['nonce'], $this->get_edit_block_nonce_action( $payload['uuid'], $payload['entry_id'] ) ) ) {
73+
return false;
74+
}
75+
76+
// Additional security not required for adding new child entries.
77+
if ( rgpost( 'action' ) === 'gpnf_refresh_markup' ) {
78+
return true;
79+
}
80+
81+
$child_entry = GFAPI::get_entry( gp_nested_forms()->get_posted_entry_id() );
82+
$parent_entry = GFAPI::get_entry( rgar( $child_entry, 'gpnf_entry_parent' ) );
83+
if ( $parent_entry['id'] == $payload['entry_id'] ) {
84+
return true;
85+
}
86+
87+
return false;
88+
}
89+
90+
public function get_edit_queryer( $form_id ) {
91+
if ( method_exists( 'GP_Entry_Blocks\GF_Queryer', 'attach_to_current_block' ) ) {
92+
$gpeb_queryer = GP_Entry_Blocks\GF_Queryer::attach_to_current_block();
93+
if ( $gpeb_queryer && $gpeb_queryer->is_edit_entry() && $gpeb_queryer->form_id == $form_id ) {
94+
return $gpeb_queryer;
95+
}
96+
}
97+
return false;
98+
}
99+
100+
public function get_edit_block_uuid( $form_id ) {
101+
$gpeb_queryer = $this->get_edit_queryer( $form_id );
102+
if ( $gpeb_queryer ) {
103+
return $gpeb_queryer->block_context['gp-entry-blocks/uuid'];
104+
}
19105
}
20106

21-
foreach ( $form['fields'] as &$field ) {
22-
if ( $field->visibility === 'administrative' ) {
23-
$field->visibility = 'visible';
107+
public function get_edit_block_entry( $form_id ) {
108+
$gpeb_queryer = $this->get_edit_queryer( $form_id );
109+
if ( $gpeb_queryer ) {
110+
return $gpeb_queryer->entry['id'];
24111
}
25112
}
26113

27-
return $form;
114+
public function get_edit_block_nonce_action( $block_uuid, $entry_id ) {
115+
return implode( '/', array( 'gpeb_edit_entry', $block_uuid, $entry_id ) );
116+
}
117+
28118
}
119+
120+
function gpeb_editable_admin_fields() {
121+
return GPEB_Editable_Admin_Fields::get_instance();
122+
}
123+
124+
gpeb_editable_admin_fields();

0 commit comments

Comments
 (0)