arguments
3 years ago
arrayable
3 years ago
filters
3 years ago
http
3 years ago
macro-constants
3 years ago
post
3 years ago
repository
3 years ago
resources
3 years ago
security
3 years ago
theme
3 years ago
attributes-trait.php
3 years ago
base-attributes-trait.php
3 years ago
builder-helper.php
3 years ago
compatibility.php
3 years ago
date-tools.php
3 years ago
gallery.php
3 years ago
get-icon-trait.php
3 years ago
get-template-trait.php
3 years ago
html-attributes-trait.php
3 years ago
instance-trait.php
3 years ago
macros-parser.php
3 years ago
regexp-tools.php
3 years ago
tools.php
3 years ago
builder-helper.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Manager; |
| 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 | $classes = array( |
| 50 | 'jet-form-builder__field-template', |
| 51 | 'jet-listing-dynamic-post-' . $object_id, |
| 52 | ); |
| 53 | |
| 54 | if ( $checked ) { |
| 55 | $classes[] = 'jet-form-builder__field-template--checked'; |
| 56 | } |
| 57 | |
| 58 | $wp_query->queried_object = $object; |
| 59 | jet_engine()->listings->data->set_current_object( $object ); |
| 60 | |
| 61 | jet_engine()->frontend->set_listing( $listing_id ); |
| 62 | |
| 63 | ob_start(); |
| 64 | $content = jet_engine()->frontend->get_listing_item( $object ); |
| 65 | |
| 66 | $content .= ob_get_clean(); |
| 67 | |
| 68 | wp_enqueue_script( Manager::LISTING_OPTIONS_HANDLE ); |
| 69 | |
| 70 | $result = sprintf( |
| 71 | '<div class="%3$s" data-value="%1$d">%2$s</div>', |
| 72 | esc_attr( $object_id ), |
| 73 | apply_filters( 'jet-form-builder/custom-template-content', $content, $object_id, $listing_id ), |
| 74 | join( ' ', $classes ) |
| 75 | ); |
| 76 | |
| 77 | $wp_query->queried_object = $default_object; |
| 78 | jet_engine()->listings->data->set_current_object( $wp_query->queried_object ); |
| 79 | |
| 80 | return $result; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param string $html_id |
| 85 | * @param string $for |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | public static function get_unique_id( string $html_id, string $for = 'input' ): string { |
| 90 | if ( ! array_key_exists( $html_id, self::$html_ids ) ) { |
| 91 | self::$html_ids[ $html_id ] = array(); |
| 92 | } |
| 93 | |
| 94 | if ( ! isset( self::$html_ids[ $html_id ][ $for ] ) ) { |
| 95 | self::$html_ids[ $html_id ][ $for ] = 0; |
| 96 | |
| 97 | return $html_id; |
| 98 | } |
| 99 | |
| 100 | return sprintf( '%1$s_%2$d', $html_id, ++ self::$html_ids[ $html_id ][ $for ] ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @since 3.0.3 |
| 105 | * |
| 106 | * @see https://github.com/Crocoblock/issues-tracker/issues/2636 |
| 107 | */ |
| 108 | public static function enqueue_global_styles() { |
| 109 | wp_enqueue_global_styles(); |
| 110 | wp_enqueue_style( 'wp-block-library' ); |
| 111 | wp_enqueue_style( 'jet-form-builder-frontend' ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @since 3.0.4 |
| 116 | * |
| 117 | * @param string|int $form_id |
| 118 | */ |
| 119 | public static function enqueue_style_form( $form_id ) { |
| 120 | if ( self::is_enqueued_form_style( $form_id ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | $form_id = absint( $form_id ); |
| 125 | |
| 126 | $result = wp_add_inline_style( |
| 127 | 'jet-form-builder-frontend', |
| 128 | Plugin::instance()->post_type->maybe_get_jet_sm_ready_styles( $form_id ) |
| 129 | ); |
| 130 | |
| 131 | if ( $result ) { |
| 132 | self::$styled_form_ids[ $form_id ] = 1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @since 3.0.4 |
| 138 | * |
| 139 | * @param string|int $form_id |
| 140 | */ |
| 141 | public static function is_enqueued_form_style( $form_id ): bool { |
| 142 | $form_id = absint( $form_id ); |
| 143 | |
| 144 | return isset( self::$styled_form_ids[ $form_id ] ); |
| 145 | } |
| 146 | } |
| 147 |