PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / form-builder / integrations / progress-bar.php

progress-bar.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.3, at form-builder/integrations/progress-bar.php

202 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Progress Bar ↔ Form Builder bridge.
4 *
5 * Virtual REST keys (`wppb_fb_progress_bar_*`) mirror nested
6 * `wppb_*_progress_bar_settings`; the canvas block is injected from those settings.
7 * Whole bridge is skipped when the add-on is inactive.
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) exit;
11
12 // Loaded by wppb_fb_load_addon_integrations(); do not include directly.
13
14 // Keep the Progress Bar structural block insertable while the add-on is active.
15 add_filter( 'wppb_fb_enabled_structural_blocks', function ( $blocks ) {
16 $blocks[] = 'profile-builder/progress-bar';
17 return $blocks;
18 } );
19
20 /**
21 * Maps virtual flat REST meta keys to legacy nested progress-bar settings keys.
22 */
23 function wppb_pb_fb_setting_map() {
24 return array(
25 'wppb_fb_progress_bar_enable' => 'enable-progress-bar',
26 'wppb_fb_progress_bar_calculation_mode' => 'calculation-mode',
27 'wppb_fb_progress_bar_display_options' => 'display-options',
28 'wppb_fb_progress_bar_position' => 'position',
29 );
30 }
31
32 /**
33 * Legacy progress-bar settings meta key for a post type.
34 */
35 function wppb_pb_fb_meta_key( $post_type ) {
36 return $post_type === 'wppb-rf-cpt' ? 'wppb_rf_progress_bar_settings' : 'wppb_epf_progress_bar_settings';
37 }
38
39 /**
40 * Allowed values per virtual key, default first. Front end compares literally — unknown values must not land in the row.
41 */
42 function wppb_pb_fb_allowed_setting_values() {
43 return array(
44 'wppb_fb_progress_bar_enable' => array( 'No', 'Yes' ),
45 'wppb_fb_progress_bar_calculation_mode' => array( 'All Fields', 'Required Fields' ),
46 'wppb_fb_progress_bar_display_options' => array( 'Bar Only', 'Bar and Percentage Label' ),
47 'wppb_fb_progress_bar_position' => array( 'Top of Form', 'Bottom of Form', 'Both' ),
48 );
49 }
50
51 /**
52 * Fold unknown values down to the key's default (mirror writes unverified into legacy storage).
53 */
54 function wppb_pb_fb_sanitize_setting( $value, $meta_key ) {
55 $allowed = wppb_pb_fb_allowed_setting_values();
56 if ( ! isset( $allowed[ $meta_key ] ) ) return '';
57
58 return in_array( $value, $allowed[ $meta_key ], true ) ? $value : $allowed[ $meta_key ][0];
59 }
60
61 add_action( 'init', 'wppb_pb_fb_register_virtual_meta' );
62 function wppb_pb_fb_register_virtual_meta() {
63 foreach ( array( 'wppb-rf-cpt', 'wppb-epf-cpt' ) as $post_type ) {
64 foreach ( array_keys( wppb_pb_fb_setting_map() ) as $key ) {
65 // Enum-sanitized + manage_options write gate (same as wppb_fb_register_virtual_meta).
66 register_post_meta( $post_type, $key, array(
67 'show_in_rest' => true,
68 'single' => true,
69 'type' => 'string',
70 'sanitize_callback' => 'wppb_pb_fb_sanitize_setting',
71 'auth_callback' => 'wppb_fb_virtual_meta_auth',
72 ) );
73 }
74 }
75 }
76
77 /**
78 * Fold flat progress-bar meta into `wppb_*_progress_bar_settings` and delete the flat row.
79 */
80 add_action( 'added_post_meta', 'wppb_pb_fb_mirror_flat_to_nested', 10, 4 );
81 add_action( 'updated_post_meta', 'wppb_pb_fb_mirror_flat_to_nested', 10, 4 );
82 function wppb_pb_fb_mirror_flat_to_nested( $meta_id, $post_id, $meta_key, $meta_value ) {
83 $post_type = get_post_type( $post_id );
84 if ( ! in_array( $post_type, array( 'wppb-rf-cpt', 'wppb-epf-cpt' ), true ) ) return;
85
86 $map = wppb_pb_fb_setting_map();
87 if ( ! isset( $map[ $meta_key ] ) ) return;
88
89 $nested_key = $map[ $meta_key ];
90 $storage_key = wppb_pb_fb_meta_key( $post_type );
91 $settings = get_post_meta( $post_id, $storage_key, true );
92 if ( ! is_array( $settings ) || empty( $settings[0] ) || ! is_array( $settings[0] ) ) {
93 $settings = array( 0 => array() );
94 }
95
96 $settings[0][ $nested_key ] = $meta_value;
97 update_post_meta( $post_id, $storage_key, $settings );
98
99 delete_post_meta( $post_id, $meta_key );
100 }
101
102 /**
103 * Surface nested progress-bar settings as flat virtual meta on REST read (priority 11, after form-builder).
104 */
105 add_filter( 'rest_prepare_wppb-rf-cpt', 'wppb_pb_fb_inject_settings_into_response', 11, 3 );
106 add_filter( 'rest_prepare_wppb-epf-cpt', 'wppb_pb_fb_inject_settings_into_response', 11, 3 );
107 function wppb_pb_fb_inject_settings_into_response( $response, $post, $request ) {
108 if ( ! $response instanceof WP_REST_Response ) return $response;
109 $data = $response->get_data();
110 if ( ! isset( $data['meta'] ) || ! is_array( $data['meta'] ) ) {
111 $data['meta'] = array();
112 }
113
114 $post_id = (int) $post->ID;
115 $pb = get_post_meta( $post_id, wppb_pb_fb_meta_key( $post->post_type ), true );
116 $nested = is_array( $pb ) && isset( $pb[0] ) && is_array( $pb[0] ) ? $pb[0] : array();
117
118 foreach ( wppb_pb_fb_setting_map() as $flat => $nested_key ) {
119 if ( isset( $nested[ $nested_key ] ) ) {
120 $data['meta'][ $flat ] = $nested[ $nested_key ];
121 }
122 }
123
124 $response->set_data( $data );
125 return $response;
126 }
127
128 /**
129 * Inject locked progress-bar preview blocks from settings.
130 * Priority 20 — after MSF inserts step breaks so per-step bars can mirror the front end.
131 * Skip leading/trailing breaks as boundaries (same as isStepBoundary() in stepBoundaries.js).
132 */
133 add_filter( 'wppb_fb_form_blocks', 'wppb_pb_fb_inject_progress_bar_blocks', 20, 3 );
134 function wppb_pb_fb_inject_progress_bar_blocks( $blocks, $post_id, $post_type ) {
135 if ( ! in_array( $post_type, array( 'wppb-rf-cpt', 'wppb-epf-cpt' ), true ) ) return $blocks;
136
137 $pb_settings = get_post_meta( $post_id, wppb_pb_fb_meta_key( $post_type ), true );
138 $row = is_array( $pb_settings ) && isset( $pb_settings[0] ) ? $pb_settings[0] : array();
139 if ( ( $row['enable-progress-bar'] ?? 'No' ) !== 'Yes' ) return $blocks;
140
141 $position = $row['position'] ?? 'Top of Form';
142 $want_top = in_array( $position, array( 'Top of Form', 'Both' ), true );
143 $want_bottom = in_array( $position, array( 'Bottom of Form', 'Both' ), true );
144
145 $make_block = function ( $context ) {
146 return array(
147 'blockName' => 'profile-builder/progress-bar',
148 'attrs' => array(
149 'position-context' => $context,
150 'lock' => array( 'move' => true, 'remove' => true ),
151 ),
152 'innerBlocks' => array(),
153 'innerHTML' => '',
154 'innerContent' => array(),
155 );
156 };
157
158 $is_break = function ( $b ) {
159 return is_array( $b ) && ( $b['blockName'] ?? '' ) === 'profile-builder/msf-step-break';
160 };
161
162 $out = array();
163 $blocks = array_values( $blocks );
164 $last = count( $blocks ) - 1;
165
166 if ( $want_top ) $out[] = $make_block( 'top' );
167
168 foreach ( $blocks as $i => $block ) {
169 $is_boundary = $is_break( $block ) && $i !== 0 && $i !== $last;
170
171 if ( $is_boundary ) {
172 if ( $want_bottom ) $out[] = $make_block( 'bottom' );
173 $out[] = $block;
174 if ( $want_top ) $out[] = $make_block( 'top' );
175 } else {
176 $out[] = $block;
177 }
178 }
179
180 if ( $want_bottom ) $out[] = $make_block( 'bottom' );
181
182 return $out;
183 }
184
185 /**
186 * Surface an `active` flag so the Progress Bar panel only runs when the add-on is on.
187 */
188 add_action( 'enqueue_block_editor_assets', 'wppb_pb_fb_enqueue_inline_bridge', 11 );
189 function wppb_pb_fb_enqueue_inline_bridge() {
190 $post_type = get_post_type();
191 if ( ! in_array( $post_type, array( 'wppb-rf-cpt', 'wppb-epf-cpt' ), true ) ) return;
192 if ( ! wp_script_is( 'wppb-form-editor-bundle', 'registered' ) ) return;
193
194 wp_add_inline_script(
195 'wppb-form-editor-bundle',
196 'window.wppbFb = window.wppbFb || {}; window.wppbFb.progressBar = ' . wp_json_encode( array(
197 'active' => true,
198 ) ) . ';',
199 'before'
200 );
201 }
202