| 1 |
<?php |
| 2 |
/** |
| 3 |
* Field Visibility ↔ Form Builder bridge. |
| 4 |
* |
| 5 |
* Injects visibility / user-role / location attributes into field blocks and |
| 6 |
* publishes editor UI data. Keys already round-trip via optional_addon_properties. |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
// Loaded by wppb_fb_load_addon_integrations(); do not include directly. |
| 12 |
|
| 13 |
/** |
| 14 |
* Field types that get visibility attributes (labels from the legacy allowlist). |
| 15 |
* Repeater is excluded — see wppb_fv_fb_unsupported_field_types(). |
| 16 |
* |
| 17 |
* @return string[] |
| 18 |
*/ |
| 19 |
function wppb_fv_fb_supported_field_types() { |
| 20 |
if ( ! function_exists( 'wppb_in_field_visibility_get_extra_fields' ) ) return array(); |
| 21 |
|
| 22 |
return array_values( array_diff( |
| 23 |
array_values( wppb_in_field_visibility_get_extra_fields() ), |
| 24 |
wppb_fv_fb_unsupported_field_types() |
| 25 |
) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Match classic: no visibility UI on Repeater (legacy script overwrite of fields['Repeater']). |
| 30 |
* |
| 31 |
* @return string[] |
| 32 |
*/ |
| 33 |
function wppb_fv_fb_unsupported_field_types() { |
| 34 |
return apply_filters( 'wppb_fv_fb_unsupported_field_types', array( 'Repeater' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Strip visibility keys from Repeater sub-fields (classic unsupported; panel hidden via insideRepeater). |
| 39 |
*/ |
| 40 |
add_filter( 'wppb_fb_repeater_subfield_stripped_attrs', 'wppb_fv_fb_strip_subfield_attrs' ); |
| 41 |
function wppb_fv_fb_strip_subfield_attrs( $stripped ) { |
| 42 |
$stripped[] = 'visibility'; |
| 43 |
$stripped[] = 'user-role-visibility'; |
| 44 |
$stripped[] = 'location-visibility'; |
| 45 |
return $stripped; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Inject the three visibility attributes. Defaults match legacy WCK ("all"). |
| 50 |
*/ |
| 51 |
add_filter( 'wppb_fb_block_attributes', 'wppb_fv_fb_inject_block_attributes', 10, 3 ); |
| 52 |
function wppb_fv_fb_inject_block_attributes( $attributes, $field_type, $context ) { |
| 53 |
if ( ! in_array( $field_type, wppb_fv_fb_supported_field_types(), true ) ) { |
| 54 |
return $attributes; |
| 55 |
} |
| 56 |
if ( ! isset( $attributes['visibility'] ) ) { |
| 57 |
$attributes['visibility'] = array( 'type' => 'string', 'default' => 'all' ); |
| 58 |
} |
| 59 |
if ( ! isset( $attributes['user-role-visibility'] ) ) { |
| 60 |
$attributes['user-role-visibility'] = array( 'type' => 'string', 'default' => 'all' ); |
| 61 |
} |
| 62 |
if ( ! isset( $attributes['location-visibility'] ) ) { |
| 63 |
$attributes['location-visibility'] = array( 'type' => 'string', 'default' => 'all' ); |
| 64 |
} |
| 65 |
return $attributes; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Publish editor payloads for extraAttributes + FieldVisibilityPanel. |
| 70 |
*/ |
| 71 |
add_action( 'wppb_fb_enqueue_editor_assets_late', 'wppb_fv_fb_enqueue_editor_payload' ); |
| 72 |
function wppb_fv_fb_enqueue_editor_payload() { |
| 73 |
$supported_types = wppb_fv_fb_supported_field_types(); |
| 74 |
if ( empty( $supported_types ) ) return; |
| 75 |
|
| 76 |
$extra_attrs = array(); |
| 77 |
$attr_schema = array( |
| 78 |
'visibility' => array( 'type' => 'string', 'default' => 'all' ), |
| 79 |
'user-role-visibility' => array( 'type' => 'string', 'default' => 'all' ), |
| 80 |
'location-visibility' => array( 'type' => 'string', 'default' => 'all' ), |
| 81 |
); |
| 82 |
foreach ( $supported_types as $field_type ) { |
| 83 |
$extra_attrs[ $field_type ] = $attr_schema; |
| 84 |
} |
| 85 |
|
| 86 |
wppb_fb_extra_attributes_inline_script( $extra_attrs ); |
| 87 |
|
| 88 |
// Roles must come from the server — the editor has no other live source. |
| 89 |
global $wp_roles; |
| 90 |
$user_roles = array(); |
| 91 |
if ( isset( $wp_roles ) && ! empty( $wp_roles->roles ) ) { |
| 92 |
foreach ( $wp_roles->roles as $slug => $role ) { |
| 93 |
$user_roles[ $slug ] = stripslashes( $role['name'] ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$payload = array( |
| 98 |
'userRoles' => $user_roles, |
| 99 |
'locations' => array( |
| 100 |
'back_end' => __( 'WordPress Edit Profile Form (back-end)', 'profile-builder' ), |
| 101 |
'register' => __( 'Register Forms Front-End', 'profile-builder' ), |
| 102 |
'edit_profile' => __( 'Edit Profile Forms Front-End', 'profile-builder' ), |
| 103 |
), |
| 104 |
'visibilityOptions' => array( |
| 105 |
'all' => __( 'All', 'profile-builder' ), |
| 106 |
'admin_only' => __( 'Admin Only', 'profile-builder' ), |
| 107 |
'user_locked' => __( 'User Locked', 'profile-builder' ), |
| 108 |
), |
| 109 |
); |
| 110 |
|
| 111 |
wp_add_inline_script( |
| 112 |
'wppb-form-editor-bundle', |
| 113 |
'window.wppbFb = window.wppbFb || {}; window.wppbFb.fieldVisibility = ' . wp_json_encode( $payload ) . ';', |
| 114 |
'before' |
| 115 |
); |
| 116 |
} |
| 117 |
|