GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-approval.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Add custom options for address fields
5  * @since 1.19
6  */
8 
9  var $name = 'entry_approval';
10 
11  var $is_searchable = true;
12 
13  public $search_operators = array( 'is', 'isnot' );
14 
15  var $is_sortable = true;
16 
17  var $is_numeric = true;
18 
19  var $group = 'gravityview';
20 
21  var $contexts = array( 'single', 'multiple' );
22 
23  var $icon = 'dashicons-yes-alt';
24 
25  public function __construct() {
26 
27  $this->label = esc_attr__( 'Approve Entries', 'gk-gravityview' );
28 
29  $this->description = esc_attr__( 'Approve and reject entries from the View.', 'gk-gravityview' );
30 
31  $this->add_hooks();
32 
33  parent::__construct();
34  }
35 
36  /**
37  * Remove unused settings for the approval field
38  *
39  * @since 1.19
40  *
41  * @param array $field_options
42  * @param string $template_id
43  * @param string $field_id
44  * @param string $context
45  * @param string $input_type
46  *
47  * @return array
48  */
49  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
50 
51  unset( $field_options['only_loggedin'] );
52 
53  unset( $field_options['new_window'] );
54 
55  unset( $field_options['show_as_link'] );
56 
57  return $field_options;
58  }
59 
60  /**
61  * Add filters and actions for the field
62  *
63  * @since 1.19
64  *
65  * @return void
66  */
67  private function add_hooks() {
68 
69  add_filter( 'gravityview_entry_default_fields', array( $this, 'filter_gravityview_entry_default_field' ), 10, 3 );
70 
71  add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts_and_styles' ) );
72 
73  // Make sure scripts are registered for FSE themes
74  add_action( 'gravityview/template/before', array( $this, 'register_scripts_and_styles' ) );
75 
76  add_action( 'gravityview/field/approval/load_scripts', array( $this, 'enqueue_and_localize_script' ) );
77 
78  add_action( 'gravityview_datatables_scripts_styles', array( $this, 'enqueue_and_localize_script' ) );
79 
80  add_filter( 'gravityview_get_entries', array( $this, 'modify_search_parameters' ), 1000 );
81 
82  add_filter( 'gravityview/field_output/html', array( $this, 'maybe_prevent_field_render' ), 10, 2 );
83 
84  add_filter( 'gravityview/field/is_visible', array( $this, 'maybe_not_visible' ), 10, 2 );
85  }
86 
87  /**
88  * @filter `gravityview/template/field_label` Modify field label output
89  *
90  * @since 1.19
91  *
92  * @param string $html Existing HTML output
93  * @param array $args Arguments passed to the function
94  *
95  * @return string Empty string if user doesn't have the `gravityview_moderate_entries` cap; field HTML otherwise
96  */
97  public function maybe_prevent_field_render( $html, $args ) {
98 
99  // If the field is `entry_approval` type but the user doesn't have the moderate entries cap, don't render.
100  if( $this->name === \GV\Utils::get( $args['field'], 'id' ) && ! GVCommon::has_cap('gravityview_moderate_entries') ) {
101  return '';
102  }
103 
104  return $html;
105  }
106 
107  /**
108  * Do not show this field if `gravityview_moderate_entries` capability is absent.
109  *
110  * @return boolean Whether this field is visible or not.
111  */
112  public function maybe_not_visible( $visible, $field ) {
113  if ( $this->name !== $field->ID ) {
114  return $visible;
115  }
116 
117  return GVCommon::has_cap( 'gravityview_moderate_entries' );
118  }
119 
120  /**
121  * Modify search to use `is_approved` meta key to sort, instead of `entry_approval`
122  *
123  * @param array $parameters Search parameters used to generate GF search
124  *
125  * @return array Same parameters, but if sorting by `entry_approval`, changed to `is_approved`
126  */
127  public function modify_search_parameters( $parameters ) {
128 
129  if ( $this->name === \GV\Utils::get( $parameters, 'sorting/key' ) ) {
130  $parameters['sorting']['key'] = 'is_approved';
131  }
132 
133  return $parameters;
134  }
135 
136  /**
137  * Register the field approval script and style
138  *
139  * @since 1.19
140  *
141  * @return void
142  */
144 
145  if ( wp_script_is( 'gravityview-field-approval' ) ) {
146  return;
147  }
148 
149  $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
150 
151  wp_register_script( 'gravityview-field-approval', GRAVITYVIEW_URL . 'assets/js/field-approval'.$script_debug.'.js', array( 'jquery' ), GV_PLUGIN_VERSION, true );
152 
153  wp_register_script( 'gravityview-field-approval-popper', GRAVITYVIEW_URL . 'assets/lib/tippy/popper.min.js', array(), GV_PLUGIN_VERSION, true );
154  wp_register_script( 'gravityview-field-approval-tippy', GRAVITYVIEW_URL . 'assets/lib/tippy/tippy.min.js', array(), GV_PLUGIN_VERSION, true );
155  wp_register_style( 'gravityview-field-approval-tippy', GRAVITYVIEW_URL . 'assets/lib/tippy/tippy.css', array(), GV_PLUGIN_VERSION, 'screen' );
156 
157  $style_path = GRAVITYVIEW_DIR . 'templates/css/field-approval.css';
158 
159  if( class_exists( 'GravityView_View' ) ) {
160  /**
161  * Override CSS file by placing in your theme's /gravityview/css/ sub-directory.
162  */
163  $style_path = GravityView_View::getInstance()->locate_template( 'css/field-approval.css', false );
164  }
165 
166  $style_url = plugins_url( 'css/field-approval.css', trailingslashit( dirname( $style_path ) ) );
167 
168  /**
169  * @filter `gravityview/field/approval/css_url` URL to the Approval field CSS file.
170  * @since 1.19
171  * @param string $style_url Override to use your own CSS file, or return empty string to disable loading.
172  */
173  $style_url = apply_filters( 'gravityview/field/approval/css_url', $style_url );
174 
175  if( ! empty( $style_url ) ) {
176  wp_register_style( 'gravityview-field-approval', $style_url, array( 'dashicons' ), GV_PLUGIN_VERSION, 'screen' );
177  }
178 
179  unset( $style_path, $style_url );
180  }
181 
182  /**
183  * Register the field approval script and output the localized text JS variables
184  * @since 1.19
185  * @return void
186  */
187  public function enqueue_and_localize_script() {
188 
189  // The script is already registered and enqueued
190  if( wp_script_is( 'gravityview-field-approval', 'enqueued' ) ) {
191  return;
192  }
193 
194  wp_enqueue_style( 'gravityview-field-approval' );
195 
196  wp_enqueue_script( 'gravityview-field-approval' );
197  wp_enqueue_script( 'gravityview-field-approval-tippy' );
198  wp_enqueue_script( 'gravityview-field-approval-popper' );
199  wp_enqueue_style( 'gravityview-field-approval-tippy' );
200 
201  wp_localize_script( 'gravityview-field-approval', 'gvApproval', array(
202  'ajaxurl' => admin_url( 'admin-ajax.php' ),
203  'nonce' => wp_create_nonce('gravityview_entry_approval'),
205  'status_popover_template' => GravityView_Entry_Approval::get_popover_template(),
206  'status_popover_placement' => GravityView_Entry_Approval::get_popover_placement(),
207  ));
208 
209  }
210 
211  /**
212  * Add Fields to the field list
213  *
214  * @since 1.19
215  *
216  * @param array $entry_default_fields Array of fields shown by default
217  * @param string|array $form form_ID or form object
218  * @param string $context Either 'single', 'directory', 'header', 'footer'
219  *
220  * @return array
221  */
222  public function filter_gravityview_entry_default_field( $entry_default_fields, $form, $context ) {
223 
224  if ( ! isset( $entry_default_fields["{$this->name}"] ) && 'edit' !== $context ) {
225  $entry_default_fields["{$this->name}"] = array(
226  'label' => $this->label,
227  'desc' => $this->description,
228  'type' => $this->name,
229  );
230  }
231 
232  return $entry_default_fields;
233  }
234 
235  /**
236  * Get the anchor text for a link, based on the current status
237  *
238  * @since 1.19
239  * @uses GravityView_Entry_Approval_Status::get_string()
240  *
241  * @param string $approved_status Status string or key
242  *
243  * @return false|string False if string doesn't exist, otherwise the "label" for the status
244  */
245  public static function get_anchor_text( $approved_status = '' ) {
246  return GravityView_Entry_Approval_Status::get_string( $approved_status, 'label' );
247  }
248 
249  /**
250  * Get the title attribute for a link, based on the current status
251  *
252  * @since 1.19
253  * @uses GravityView_Entry_Approval_Status::get_string()
254  *
255  * @param int|string $approved_status Status string or key
256  *
257  * @return false|string
258  */
259  public static function get_title_attr( $approved_status ) {
260  return GravityView_Entry_Approval_Status::get_string( $approved_status, 'title' );
261  }
262 
263  /**
264  * Get the CSS class for a link, based on the current status
265  *
266  * @param int|string $approved_status Status string or key
267  *
268  * @return string CSS class, sanitized using esc_attr()
269  */
270  public static function get_css_class( $approved_status ) {
271 
272  $approved_key = GravityView_Entry_Approval_Status::get_key( $approved_status );
273 
274  return esc_attr( "gv-approval-{$approved_key}" );
275  }
276 }
277 
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
Definition: gravityview.php:49
Modify field settings by extending this class.
const GV_PLUGIN_VERSION(! GravityKit\GravityView\Foundation\meets_min_php_version_requirement(__FILE__, '7.2.0'))
Constants.
Definition: gravityview.php:34
enqueue_and_localize_script()
Register the field approval script and output the localized text JS variables.
static getInstance( $passed_post=NULL)
new GravityView_Field_Entry_Approval
$name
$contexts
const GRAVITYVIEW_URL
The URL to this file, with trailing slash.
Definition: gravityview.php:45
static get_title_attr( $approved_status)
Get the title attribute for a link, based on the current status.
$icon
static get_popover_placement()
Where should the popover be placed?
__construct()
modify_search_parameters( $parameters)
Modify search to use is_approved meta key to sort, instead of entry_approval
$is_sortable
static get_css_class( $approved_status)
Get the CSS class for a link, based on the current status.
$group
static get_key( $value)
Get the status key for a value.
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
Remove unused settings for the approval field.
if(gravityview() ->plugin->is_GF_25()) $form
$is_searchable
scale description p description
$search_operators
add_hooks()
Add filters and actions for the field.
register_scripts_and_styles()
Register the field approval script and style.
static get_anchor_text( $approved_status='')
Get the anchor text for a link, based on the current status.
maybe_not_visible( $visible, $field)
Do not show this field if gravityview_moderate_entries capability is absent.
Add custom options for address fields.
$is_numeric
filter_gravityview_entry_default_field( $entry_default_fields, $form, $context)
Add Fields to the field list.
static get_string( $value_or_key, $string_key='')
Get the label for a specific approval value.
if(empty( $created_by)) $form_id
static get_popover_template()
Get HTML template for a popover used to display approval statuses.
static get_all()
Return array of status options.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22
maybe_prevent_field_render( $html, $args)