GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
entry-link/block.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class EntryLink {
9  /**
10  * Modifies block meta.
11  *
12  * This method is called by class-gv-gutenberg.php before registering the block.
13  *
14  * @since $ver$
15  *
16  * @param array $block_meta
17  *
18  * @return array
19  */
20  public function modify_block_meta( $block_meta ) {
21  return [
22  'title' => __( 'GravityView Entry Link', 'gk-gravityview' ),
23  'render_callback' => [ $this, 'render' ],
24  'localization' => [
25  'previewImage' => untrailingslashit( plugin_dir_url( __FILE__ ) ) . '/preview.svg'
26  ]
27  ];
28  }
29 
30  /**
31  * Renders [gv_entry_link] shortcode.
32  *
33  * @since $ver$
34  *
35  * @param array $block_attributes
36  *
37  * @return string $output
38  */
39  static function render( $block_attributes = [] ) {
40  $block_to_shortcode_attributes_map = [
41  'viewId' => 'view_id',
42  'entryId' => 'entry_id',
43  'action' => 'action',
44  'postId' => 'post_id',
45  'returnFormat' => 'return',
46  'linkAtts' => 'link_atts',
47  'fieldValues' => 'field_values',
48  'content' => 'content',
49  ];
50 
51  $shortcode_attributes = [];
52 
53  foreach ( $block_attributes as $attribute => $value ) {
54  $value = esc_attr( sanitize_text_field( $value ) );
55 
56  if ( isset( $block_to_shortcode_attributes_map[ $attribute ] ) && ! empty( $value ) ) {
57  $shortcode_attributes[] = sprintf(
58  '%s="%s"',
59  $block_to_shortcode_attributes_map[ $attribute ],
60  str_replace( '"', '\"', $value )
61  );
62  }
63  }
64 
65  if ( ! empty( $block_attributes['content'] ) ) {
66  $shortcode = sprintf(
67  '[gv_entry_link %s]%s[/gv_entry_link]',
68  implode( ' ', $shortcode_attributes ),
69  wp_kses_post( $block_attributes['content'] )
70  );
71  } else {
72  $shortcode = sprintf( '[gv_entry_link %s/]', implode( ' ', $shortcode_attributes ) );
73  }
74 
75  if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
76  add_filter( 'gravityview/entry_link/add_query_args', '__return_false' );
77  }
78 
79  if ( Arr::get( $block_attributes, 'previewAsShortcode' ) ) {
80  return $shortcode;
81  }
82 
83  $rendered_shortcode = Blocks::render_shortcode( $shortcode );
84 
85  return $rendered_shortcode['content'];
86  }
87 }
static get( $array, $key, $default=null)
{}
Definition: Arr.php:99
static render( $block_attributes=[])
Renders [gv_entry_link] shortcode.
modify_block_meta( $block_meta)
Modifies block meta.
static render_shortcode( $shortcode)
Renders shortcode and returns rendered content along with newly enqueued scripts and styles...