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
91 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Manager; |
| 7 | |
| 8 | if ( ! defined( 'WPINC' ) ) { |
| 9 | die; |
| 10 | } |
| 11 | |
| 12 | class Builder_Helper { |
| 13 | |
| 14 | private static $html_ids = array(); |
| 15 | |
| 16 | /** |
| 17 | * @param $object_id |
| 18 | * @param $args |
| 19 | * @param false $checked |
| 20 | * |
| 21 | * @return string |
| 22 | */ |
| 23 | public function get_custom_template( $object_id, $args, $checked = false ) { |
| 24 | if ( ! Compatibility::has_jet_engine() ) { |
| 25 | return __( 'Please install JetEngine', 'jet-form-builder' ) . '<br>'; |
| 26 | } |
| 27 | |
| 28 | $listing_id = ! empty( $args['custom_item_template_id'] ) ? $args['custom_item_template_id'] : false; |
| 29 | $listing_id = absint( $listing_id ); |
| 30 | |
| 31 | if ( ! $listing_id ) { |
| 32 | return __( 'Please select template', 'jet-form-builder' ) . '<br>'; |
| 33 | } |
| 34 | |
| 35 | global $wp_query; |
| 36 | $default_object = $wp_query->queried_object; |
| 37 | |
| 38 | $options_from = ! empty( $args['field_options_from'] ) ? $args['field_options_from'] : 'posts'; |
| 39 | |
| 40 | if ( 'terms' === $options_from ) { |
| 41 | $object = get_term( $object_id ); |
| 42 | } else { |
| 43 | $object = get_post( $object_id ); |
| 44 | } |
| 45 | |
| 46 | $classes = array( |
| 47 | 'jet-form-builder__field-template', |
| 48 | 'jet-listing-dynamic-post-' . $object_id, |
| 49 | ); |
| 50 | |
| 51 | if ( $checked ) { |
| 52 | $classes[] = 'jet-form-builder__field-template--checked'; |
| 53 | } |
| 54 | |
| 55 | $wp_query->queried_object = $object; |
| 56 | jet_engine()->listings->data->set_current_object( $object ); |
| 57 | |
| 58 | jet_engine()->frontend->set_listing( $listing_id ); |
| 59 | |
| 60 | ob_start(); |
| 61 | $content = jet_engine()->frontend->get_listing_item( $object ); |
| 62 | |
| 63 | $content .= ob_get_clean(); |
| 64 | |
| 65 | wp_enqueue_script( Manager::LISTING_OPTIONS_HANDLE ); |
| 66 | |
| 67 | $result = sprintf( |
| 68 | '<div class="%3$s" data-value="%1$d">%2$s</div>', |
| 69 | esc_attr( $object_id ), |
| 70 | apply_filters( 'jet-form-builder/custom-template-content', $content, $object_id, $listing_id ), |
| 71 | join( ' ', $classes ) |
| 72 | ); |
| 73 | |
| 74 | $wp_query->queried_object = $default_object; |
| 75 | jet_engine()->listings->data->set_current_object( $wp_query->queried_object ); |
| 76 | |
| 77 | return $result; |
| 78 | } |
| 79 | |
| 80 | public static function get_unique_id( string $html_id ): string { |
| 81 | if ( ! array_key_exists( $html_id, self::$html_ids ) ) { |
| 82 | self::$html_ids[ $html_id ] = 0; |
| 83 | |
| 84 | return $html_id; |
| 85 | } |
| 86 | |
| 87 | return sprintf( '%1$s_%2$d', $html_id, ++ self::$html_ids[ $html_id ] ); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 |