GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
view/block.php
Go to the documentation of this file.
1 <?php
2 
4 
7 use GFFormDisplay;
8 
9 class View {
10  /**
11  * Modifies block meta.
12  *
13  * This method is called by class-gv-gutenberg.php before registering the block.
14  *
15  * @since $ver$
16  *
17  * @param array $block_meta
18  *
19  * @return array
20  */
21  public function modify_block_meta( $block_meta ) {
22  return [
23  'title' => __( 'GravityView View', 'gk-gravityview' ),
24  'render_callback' => [ $this, 'render' ],
25  'localization' => [
26  'previewImage' => untrailingslashit( plugin_dir_url( __FILE__ ) ) . '/preview.svg'
27  ]
28  ];
29  }
30 
31  /**
32  * Renders [gravityview] shortcode.
33  *
34  * @since $ver$
35  *
36  * @param array $block_attributes
37  *
38  * @return string $output
39  */
40  static function render( $block_attributes = [] ) {
41  $block_to_shortcode_attributes_map = [
42  'viewId' => 'id',
43  'postId' => 'post_id',
44  'pageSize' => 'page_size',
45  'sortField' => 'sort_field',
46  'sortDirection' => 'sort_direction',
47  'searchField' => 'search_field',
48  'searchValue' => 'search_value',
49  'searchOperator' => 'search_operator',
50  'startDate' => 'start_date',
51  'endDate' => 'end_date',
52  'classValue' => 'class',
53  'offset' => 'offset',
54  'singleTitle' => 'single_title',
55  'backLinkLabel' => 'back_link_label',
56  ];
57 
58  if ( isset( $block_attributes['searchOperator'] ) && empty( $block_attributes['searchValue'] ) ) {
59  unset( $block_attributes['searchOperator'] );
60  }
61 
62  $shortcode_attributes = [];
63 
64  foreach ( $block_attributes as $attribute => $value ) {
65  $value = esc_attr( sanitize_text_field( $value ) );
66 
67  if ( isset( $block_to_shortcode_attributes_map[ $attribute ] ) && ! empty( $value ) ) {
68  $shortcode_attributes[] = sprintf(
69  '%s="%s"',
70  $block_to_shortcode_attributes_map[ $attribute ],
71  str_replace( '"', '\"', $value )
72  );
73  }
74  }
75 
76  $shortcode = sprintf( '[gravityview %s]', implode( ' ', $shortcode_attributes ) );
77 
78  if ( Arr::get( $block_attributes, 'previewAsShortcode' ) ) {
79  return json_encode( [ 'content' => $shortcode, 'script' => '', 'styles' => '' ] );
80  }
81 
82  // Gravity Forms outputs JS not wrapped in <script> tags that's then displayed in the block preview.
83  // One of the situations where this happens is when a chained select field is added to the View search bar.
84  if ( class_exists( 'GFFormDisplay' ) && defined( 'REST_REQUEST' ) && REST_REQUEST ) {
85  $hooks_js_printed = GFFormDisplay::$hooks_js_printed;
86 
87  GFFormDisplay::$hooks_js_printed = true;
88 
89  $rendered_shortcode = Blocks::render_shortcode( $shortcode );
90 
91  GFFormDisplay::$hooks_js_printed = $hooks_js_printed;
92  } else {
93  $rendered_shortcode = Blocks::render_shortcode( $shortcode );
94  }
95 
96  if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
97  return $rendered_shortcode['content'];
98  }
99 
100  return json_encode( $rendered_shortcode );
101  }
102 }
static render( $block_attributes=[])
Renders [gravityview] shortcode.
Definition: view/block.php:40
static get( $array, $key, $default=null)
{}
Definition: Arr.php:99
modify_block_meta( $block_meta)
Modifies block meta.
Definition: view/block.php:21
static render_shortcode( $shortcode)
Renders shortcode and returns rendered content along with newly enqueued scripts and styles...