PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
jetformbuilder / includes / classes / builder-helper.php

builder-helper.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.6, at includes/classes/builder-helper.php

204 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace Jet_Form_Builder\Classes;
5
6 use Jet_Form_Builder\Blocks\Module;
7 use Jet_Form_Builder\Plugin;
8
9 if ( ! defined( 'WPINC' ) ) {
10 die;
11 }
12
13 class Builder_Helper {
14
15 private static $html_ids = array();
16
17 private static $styled_form_ids = array();
18
19 /**
20 * @param $object_id
21 * @param $args
22 * @param false $checked
23 *
24 * @return string
25 */
26 public function get_custom_template( $object_id, $args, $checked = false ) {
27 if ( ! Compatibility::has_jet_engine() ) {
28 return __( 'Please install JetEngine', 'jet-form-builder' ) . '<br>';
29 }
30
31 $listing_id = ! empty( $args['custom_item_template_id'] ) ? $args['custom_item_template_id'] : false;
32 $listing_id = absint( $listing_id );
33
34 if ( ! $listing_id ) {
35 return __( 'Please select template', 'jet-form-builder' ) . '<br>';
36 }
37
38 global $wp_query;
39 $default_object = $wp_query->queried_object;
40
41 $options_from = ! empty( $args['field_options_from'] ) ? $args['field_options_from'] : 'posts';
42
43 if ( 'terms' === $options_from ) {
44 $object = get_term( $object_id );
45 } else {
46 $object = get_post( $object_id );
47 }
48
49 $object = apply_filters( 'jet-form-builder/custom-template-object', $object, $object_id, $args );
50
51 if ( ! is_object( $object ) ) {
52 return '';
53 }
54
55 $classes = array(
56 'jet-form-builder__field-template',
57 'jet-listing-dynamic-post-' . $object_id,
58 );
59
60 if ( 'twig' === jet_engine()->listings->data->get_listing_type( $listing_id ) ) {
61 $classes[] = 'jet-listing-grid--' . $listing_id;
62 }
63
64 if ( $checked ) {
65 $classes[] = 'jet-form-builder__field-template--checked';
66 }
67
68 if ( in_array( $object, array( \WP_User::class, \WP_Post::class, \WP_Term::class ), true ) ) {
69 $wp_query->queried_object = $object;
70 }
71 jet_engine()->listings->data->set_current_object( $object );
72
73 jet_engine()->frontend->set_listing( $listing_id );
74
75 ob_start();
76 $content = jet_engine()->frontend->get_listing_item( $object );
77
78 $content .= ob_get_clean();
79
80 wp_enqueue_script( Module::LISTING_OPTIONS_HANDLE );
81
82 $result = apply_filters(
83 'jet-form-builder/custom-template-content',
84 sprintf(
85 '<div class="%3$s" data-value="%1$d">%2$s</div>',
86 esc_attr( $object_id ),
87 $content,
88 join( ' ', $classes )
89 ),
90 $object_id,
91 $listing_id
92 );
93
94 if ( in_array( $object, array( \WP_User::class, \WP_Post::class, \WP_Term::class ), true ) ) {
95 $wp_query->queried_object = $default_object;
96 }
97
98 jet_engine()->listings->data->set_current_object( $wp_query->queried_object );
99
100 return $result;
101 }
102
103 /**
104 * @param string $html_id
105 * @param string $for_html
106 *
107 * @return string
108 */
109 public static function get_unique_id( string $html_id, string $for_html = 'input' ): string {
110 if ( ! array_key_exists( $html_id, self::$html_ids ) ) {
111 self::$html_ids[ $html_id ] = array();
112 }
113
114 if ( ! isset( self::$html_ids[ $html_id ][ $for_html ] ) ) {
115 self::$html_ids[ $html_id ][ $for_html ] = 0;
116
117 return $html_id;
118 }
119
120 return sprintf( '%1$s_%2$d', $html_id, ++ self::$html_ids[ $html_id ][ $for_html ] );
121 }
122
123 /**
124 * @since 3.1.0 Add support for WP 5.6
125 * @since 3.0.3
126 *
127 * @see https://github.com/Crocoblock/issues-tracker/issues/2636
128 */
129 public static function enqueue_global_styles() {
130 // support for WP 5.6
131 if ( function_exists( 'wp_enqueue_global_styles' ) ) {
132 wp_enqueue_global_styles();
133 }
134 wp_enqueue_style( 'wp-block-library' );
135 wp_enqueue_style( 'jet-form-builder-frontend' );
136 }
137
138 /**
139 * @param string|int $form_id
140 *
141 * @since 3.0.4
142 */
143 public static function enqueue_style_form( $form_id ) {
144 if ( self::is_enqueued_form_style( $form_id ) ) {
145 return;
146 }
147
148 $form_id = absint( $form_id );
149
150 $result = wp_add_inline_style(
151 'jet-form-builder-frontend',
152 Plugin::instance()->post_type->maybe_get_jet_sm_ready_styles( $form_id )
153 );
154
155 do_action( 'jet-form-builder/enqueue-style' );
156
157 if ( $result ) {
158 self::$styled_form_ids[ $form_id ] = 1;
159 }
160 }
161
162 /**
163 * @param string|int $form_id
164 *
165 * @since 3.0.4
166 */
167 public static function is_enqueued_form_style( $form_id ): bool {
168 $form_id = absint( $form_id );
169
170 return isset( self::$styled_form_ids[ $form_id ] );
171 }
172
173 /**
174 * @param array<array<string, string>> $attributes
175 *
176 * @return string
177 * @since 3.1.0
178 */
179 public static function attrs( array $attributes ): string {
180 return implode(
181 ' ',
182 self::filter_attrs( $attributes )
183 );
184 }
185
186 /**
187 * @param array<array<string, string>> $attributes
188 *
189 * @return array
190 */
191 public static function filter_attrs( array $attributes ): array {
192 $attributes_stack = array();
193
194 foreach ( $attributes as list( $name, $value ) ) {
195 if ( Tools::is_empty( $value ) ) {
196 continue;
197 }
198 $attributes_stack[] = sprintf( '%1$s="%2$s"', $name, $value );
199 }
200
201 return $attributes_stack;
202 }
203 }
204