GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-is-starred.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-is-starred.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
9 
10  var $name = 'is_starred';
11 
12  var $is_searchable = true;
13 
14  var $search_operators = array( 'is', 'isnot' );
15 
16  var $group = 'meta';
17 
18  var $contexts = array( 'single', 'multiple', 'export' );
19 
20  var $icon = 'dashicons-star-filled';
21 
22  private static $has_star_field = false;
23 
24  /**
25  * GravityView_Field_Is_Starred constructor.
26  */
27  public function __construct() {
28 
29  $this->label = esc_html__( 'Entry Star', 'gk-gravityview' );
30  $this->default_search_label = __( 'Is Starred', 'gk-gravityview' );
31  $this->description = esc_html__( 'Display the entry\'s "star" status.', 'gk-gravityview' );
32 
33 
34  $this->add_hooks();
35 
36  parent::__construct();
37  }
38 
39  private function add_hooks() {
40  /** @see \GV\Field::get_value_filters */
41  add_filter( "gravityview/field/{$this->name}/output", array( $this, 'get_content' ), 4, 2 );
42  add_action( 'gravityview/template/after', array( $this, 'print_script'), 10, 1 );
43  add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 );
44  }
45 
46  /**
47  * Add this field to the default fields in the GV field picker
48  *
49  * @param array $entry_default_fields Array of fields shown by default
50  * @param string|array $form form_ID or form object
51  * @param string $zone Either 'single', 'directory', 'header', 'footer'
52  *
53  * @return array
54  */
55  function add_default_field( $entry_default_fields = array(), $form = array(), $zone = '' ) {
56 
57  if( 'edit' !== $zone ) {
58  $entry_default_fields[ $this->name ] = array(
59  'label' => $this->label,
60  'desc' => $this->description,
61  'type' => $this->name,
62  );
63  }
64 
65  return $entry_default_fields;
66  }
67 
68  /**
69  * Show the star image
70  *
71  * @since 2.0
72  *
73  * @param string $output HTML value output
74  * @param \GV\Field_Template $template The field template being rendered
75  *
76  * @return string Image of the star
77  */
78  public function get_content( $output, $template ) {
79  $entry = $template->entry;
80 
81  $star_url = GFCommon::get_base_url() .'/images/star' . intval( $entry['is_starred'] ) .'.png';
82 
83  $entry_id = '';
84 
85  if ( GravityView_Roles_Capabilities::has_cap( 'gravityview_edit_entries' ) ) {
86  $entry_id = "data-entry-id='{$entry->ID}'";
87  }
88 
89  // if( $show_as_star )
90  $output = '<img class="gv-star-image" '.$entry_id.' data-is_starred="'. intval( $entry['is_starred'] ) .'" src="'. esc_attr( $star_url ) .'" />';
91 
92  self::$has_star_field = true;
93 
94  return $output;
95  }
96 
97  /**
98  * Add JS to the bottom of the View if there is a star field and user has `gravityview_edit_entries` cap
99  *
100  * @param \GV\Template_Context $context The template context
101  * @since 2.0
102  *
103  * @return void
104  */
105  public function print_script( $context ) {
106 
107  if( ! self::$has_star_field ) {
108  return;
109  }
110 
111  if ( ! GravityView_Roles_Capabilities::has_cap( 'gravityview_edit_entries' ) ) {
112  return;
113  }
114 
115  ?>
116 <style>
117  .gv-star-image[data-entry-id] {
118  cursor: pointer;
119  }
120 </style>
121 <script>
122  jQuery( document ).ready( function ( $ ) {
123  $('[class*=is_starred] img.gv-star-image[data-entry-id]').on('click', function() {
124 
125  var is_starred = $(this).data('is_starred'),
126  update = ( is_starred ? 0 : 1 ),
127  entry_id = $(this).data('entry-id'),
128  $star = $( this );
129 
130  $.ajax({
131  type: "POST",
132  url: "<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>",
133  data: {
134  action: 'rg_update_lead_property',
135  rg_update_lead_property: '<?php echo wp_create_nonce( 'rg_update_lead_property' ) ?>',
136  lead_id: entry_id,
137  name: 'is_starred',
138  value: update
139  }
140  })
141  .done(function() {
142  $star
143  .attr('src', $star.attr('src').replace( "star" + is_starred + ".png", "star" + update + ".png" ) )
144  .data( 'is_starred', update );
145  })
146  .fail(function() {
147  alert(<?php echo json_encode( __( 'There was an error updating the entry.', 'gk-gravityview' ) ); ?>);
148  });
149  });
150  });
151 </script>
152 <?php
153  }
154 
155 }
156 
Modify field settings by extending this class.
if(! isset( $gravityview)||empty( $gravityview->template)) $template
The entry loop for the list output.
static has_cap( $caps_to_check='', $object_id=null, $user_id=null)
Check whether the current user has a capability.
__construct()
GravityView_Field_Is_Starred constructor.
add_default_field( $entry_default_fields=array(), $form=array(), $zone='')
Add this field to the default fields in the GV field picker.
print_script( $context)
Add JS to the bottom of the View if there is a star field and user has gravityview_edit_entries cap...
if(gravityview() ->plugin->is_GF_25()) $form
scale description p description
get_content( $output, $template)
Show the star image.
$entry
Definition: notes.php:27