| 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 ( $checked ) { |
| 61 |
$classes[] = 'jet-form-builder__field-template--checked'; |
| 62 |
} |
| 63 |
|
| 64 |
if ( in_array( $object, array( \WP_User::class, \WP_Post::class, \WP_Term::class ), true ) ) { |
| 65 |
$wp_query->queried_object = $object; |
| 66 |
} |
| 67 |
jet_engine()->listings->data->set_current_object( $object ); |
| 68 |
|
| 69 |
jet_engine()->frontend->set_listing( $listing_id ); |
| 70 |
|
| 71 |
ob_start(); |
| 72 |
$content = jet_engine()->frontend->get_listing_item( $object ); |
| 73 |
|
| 74 |
$content .= ob_get_clean(); |
| 75 |
|
| 76 |
wp_enqueue_script( Module::LISTING_OPTIONS_HANDLE ); |
| 77 |
|
| 78 |
$result = apply_filters( |
| 79 |
'jet-form-builder/custom-template-content', |
| 80 |
sprintf( |
| 81 |
'<div class="%3$s" data-value="%1$d">%2$s</div>', |
| 82 |
esc_attr( $object_id ), |
| 83 |
$content, |
| 84 |
join( ' ', $classes ) |
| 85 |
), |
| 86 |
$object_id, |
| 87 |
$listing_id |
| 88 |
); |
| 89 |
|
| 90 |
if ( in_array( $object, array( \WP_User::class, \WP_Post::class, \WP_Term::class ), true ) ) { |
| 91 |
$wp_query->queried_object = $default_object; |
| 92 |
} |
| 93 |
|
| 94 |
jet_engine()->listings->data->set_current_object( $wp_query->queried_object ); |
| 95 |
|
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param string $html_id |
| 101 |
* @param string $for_html |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
public static function get_unique_id( string $html_id, string $for_html = 'input' ): string { |
| 106 |
if ( ! array_key_exists( $html_id, self::$html_ids ) ) { |
| 107 |
self::$html_ids[ $html_id ] = array(); |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! isset( self::$html_ids[ $html_id ][ $for_html ] ) ) { |
| 111 |
self::$html_ids[ $html_id ][ $for_html ] = 0; |
| 112 |
|
| 113 |
return $html_id; |
| 114 |
} |
| 115 |
|
| 116 |
return sprintf( '%1$s_%2$d', $html_id, ++ self::$html_ids[ $html_id ][ $for_html ] ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @since 3.1.0 Add support for WP 5.6 |
| 121 |
* @since 3.0.3 |
| 122 |
* |
| 123 |
* @see https://github.com/Crocoblock/issues-tracker/issues/2636 |
| 124 |
*/ |
| 125 |
public static function enqueue_global_styles() { |
| 126 |
// support for WP 5.6 |
| 127 |
if ( function_exists( 'wp_enqueue_global_styles' ) ) { |
| 128 |
wp_enqueue_global_styles(); |
| 129 |
} |
| 130 |
wp_enqueue_style( 'wp-block-library' ); |
| 131 |
wp_enqueue_style( 'jet-form-builder-frontend' ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @param string|int $form_id |
| 136 |
* |
| 137 |
* @since 3.0.4 |
| 138 |
*/ |
| 139 |
public static function enqueue_style_form( $form_id ) { |
| 140 |
if ( self::is_enqueued_form_style( $form_id ) ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
$form_id = absint( $form_id ); |
| 145 |
|
| 146 |
$result = wp_add_inline_style( |
| 147 |
'jet-form-builder-frontend', |
| 148 |
Plugin::instance()->post_type->maybe_get_jet_sm_ready_styles( $form_id ) |
| 149 |
); |
| 150 |
|
| 151 |
do_action( 'jet-form-builder/enqueue-style' ); |
| 152 |
|
| 153 |
if ( $result ) { |
| 154 |
self::$styled_form_ids[ $form_id ] = 1; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @param string|int $form_id |
| 160 |
* |
| 161 |
* @since 3.0.4 |
| 162 |
*/ |
| 163 |
public static function is_enqueued_form_style( $form_id ): bool { |
| 164 |
$form_id = absint( $form_id ); |
| 165 |
|
| 166 |
return isset( self::$styled_form_ids[ $form_id ] ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @param array<array<string, string>> $attributes |
| 171 |
* |
| 172 |
* @return string |
| 173 |
* @since 3.1.0 |
| 174 |
*/ |
| 175 |
public static function attrs( array $attributes ): string { |
| 176 |
return implode( |
| 177 |
' ', |
| 178 |
self::filter_attrs( $attributes ) |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @param array<array<string, string>> $attributes |
| 184 |
* |
| 185 |
* @return array |
| 186 |
*/ |
| 187 |
public static function filter_attrs( array $attributes ): array { |
| 188 |
$attributes_stack = array(); |
| 189 |
|
| 190 |
foreach ( $attributes as list( $name, $value ) ) { |
| 191 |
if ( Tools::is_empty( $value ) ) { |
| 192 |
continue; |
| 193 |
} |
| 194 |
$attributes_stack[] = sprintf( '%1$s="%2$s"', $name, $value ); |
| 195 |
} |
| 196 |
|
| 197 |
return $attributes_stack; |
| 198 |
} |
| 199 |
} |
| 200 |
|