GravityView  2.10.1
The best, easiest way to display Gravity Forms entries on your website.
class-frontend-views.php
Go to the documentation of this file.
1 <?php
2 /**
3  * GravityView Frontend functions
4  *
5  * @package GravityView
6  * @license GPL2+
7  * @author GravityView <[email protected]>
8  * @link http://gravityview.co
9  * @copyright Copyright 2014, Katz Web Services, Inc.
10  *
11  * @since 1.0.0
12  */
13 
14 
16 
17  /**
18  * Regex strings that are used to determine whether the current request is a GravityView search or not.
19  * @see GravityView_frontend::is_searching()
20  * @since 1.7.4.1
21  * @var array
22  */
23  private static $search_parameters = array( 'gv_search', 'gv_start', 'gv_end', 'gv_id', 'gv_by', 'filter_*' );
24 
25  /**
26  * Is the currently viewed post a `gravityview` post type?
27  * @var boolean
28  */
30 
31  /**
32  * Does the current post have a `[gravityview]` shortcode?
33  * @var boolean
34  */
35  var $post_has_shortcode = false;
36 
37  /**
38  * The Post ID of the currently viewed post. Not necessarily GV
39  * @var int
40  */
41  var $post_id = null;
42 
43  /**
44  * Are we currently viewing a single entry?
45  * If so, the int value of the entry ID. Otherwise, false.
46  * @var int|boolean
47  */
48  var $single_entry = false;
49 
50  /**
51  * If we are viewing a single entry, the entry data
52  * @var array|false
53  */
54  var $entry = false;
55 
56  /**
57  * When displaying the single entry we should always know to which View it belongs (the context is everything!)
58  * @var null
59  */
60  var $context_view_id = null;
61 
62  /**
63  * The View is showing search results
64  * @since 1.5.4
65  * @var boolean
66  */
67  var $is_search = false;
68 
69  /**
70  * The view data parsed from the $post
71  *
72  * @see GravityView_View_Data::__construct()
73  * @var GravityView_View_Data
74  */
75  var $gv_output_data = null;
76 
77  /**
78  * @var GravityView_frontend
79  */
80  static $instance;
81 
82  /**
83  * Class constructor, enforce Singleton pattern
84  */
85  private function __construct() {}
86 
87  private function initialize() {
88  add_action( 'wp', array( $this, 'parse_content'), 11 );
89  add_filter( 'parse_query', array( $this, 'parse_query_fix_frontpage' ), 10 );
90  add_action( 'template_redirect', array( $this, 'set_entry_data'), 1 );
91 
92  // Enqueue scripts and styles after GravityView_Template::register_styles()
93  add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts_and_styles' ), 20 );
94 
95  // Enqueue and print styles in the footer. Added 1 priorty so stuff gets printed at 10 priority.
96  add_action( 'wp_print_footer_scripts', array( $this, 'add_scripts_and_styles' ), 1 );
97 
98  add_filter( 'the_title', array( $this, 'single_entry_title' ), 1, 2 );
99  add_filter( 'comments_open', array( $this, 'comments_open' ), 10, 2 );
100 
101  add_action( 'gravityview_after', array( $this, 'context_not_configured_warning' ) );
102  }
103 
104  /**
105  * Get the one true instantiated self
106  * @return GravityView_frontend
107  */
108  public static function getInstance() {
109 
110  if ( empty( self::$instance ) ) {
111  self::$instance = new self;
112  self::$instance->initialize();
113  }
114 
115  return self::$instance;
116  }
117 
118  /**
119  * @return GravityView_View_Data
120  */
121  public function getGvOutputData() {
122  return $this->gv_output_data;
123  }
124 
125  /**
126  * @param \GravityView_View_Data $gv_output_data
127  */
128  public function setGvOutputData( $gv_output_data ) {
129  $this->gv_output_data = $gv_output_data;
130  }
131 
132  /**
133  * @return boolean
134  */
135  public function isSearch() {
136  return $this->is_search;
137  }
138 
139  /**
140  * @param boolean $is_search
141  */
142  public function setIsSearch( $is_search ) {
143  $this->is_search = $is_search;
144  }
145 
146  /**
147  * @return bool|int
148  */
149  public function getSingleEntry() {
150  return $this->single_entry;
151  }
152 
153  /**
154  * Sets the single entry ID and also the entry
155  * @param bool|int|string $single_entry
156  */
157  public function setSingleEntry( $single_entry ) {
158 
159  $this->single_entry = $single_entry;
160 
161  }
162 
163  /**
164  * @return array
165  */
166  public function getEntry() {
167  return $this->entry;
168  }
169 
170  /**
171  * Set the current entry
172  * @param array|int $entry Entry array or entry slug or ID
173  */
174  public function setEntry( $entry ) {
175 
176  if ( ! is_array( $entry ) ) {
177  $entry = GVCommon::get_entry( $entry );
178  }
179 
180  $this->entry = $entry;
181  }
182 
183  /**
184  * @return int
185  */
186  public function getPostId() {
187  return $this->post_id;
188  }
189 
190  /**
191  * @param int $post_id
192  */
193  public function setPostId( $post_id ) {
194  $this->post_id = $post_id;
195  }
196 
197  /**
198  * @return boolean
199  */
200  public function isPostHasShortcode() {
202  }
203 
204  /**
205  * @param boolean $post_has_shortcode
206  */
207  public function setPostHasShortcode( $post_has_shortcode ) {
208  $this->post_has_shortcode = $post_has_shortcode;
209  }
210 
211  /**
212  * @return boolean
213  */
214  public function isGravityviewPostType() {
216  }
217 
218  /**
219  * @param boolean $is_gravityview_post_type
220  */
221  public function setIsGravityviewPostType( $is_gravityview_post_type ) {
222  $this->is_gravityview_post_type = $is_gravityview_post_type;
223  }
224 
225  /**
226  * Set the context view ID used when page contains multiple embedded views or displaying the single entry view
227  *
228  *
229  *
230  * @param null $view_id
231  */
232  public function set_context_view_id( $view_id = null ) {
233  $multiple_views = $this->getGvOutputData() && $this->getGvOutputData()->has_multiple_views();
234 
235  if ( ! empty( $view_id ) ) {
236 
237  $this->context_view_id = (int) $view_id;
238 
239  } elseif ( isset( $_GET['gvid'] ) && $multiple_views ) {
240  /**
241  * used on a has_multiple_views context
242  * @see GravityView_API::entry_link
243  */
244  $this->context_view_id = (int) $_GET['gvid'];
245 
246  } elseif ( ! $multiple_views ) {
247  $array_keys = array_keys( $this->getGvOutputData()->get_views() );
248  $this->context_view_id = (int) array_pop( $array_keys );
249  unset( $array_keys );
250  }
251 
252  }
253 
254  /**
255  * Returns the the view_id context when page contains multiple embedded views or displaying single entry view
256  *
257  * @since 1.5.4
258  *
259  * @return int|null
260  */
261  public function get_context_view_id() {
262  return $this->context_view_id;
263  }
264 
265  /**
266  * Allow GravityView entry endpoints on the front page of a site
267  *
268  * @link https://core.trac.wordpress.org/ticket/23867 Fixes this core issue
269  * @link https://wordpress.org/plugins/cpt-on-front-page/ Code is based on this
270  *
271  * @since 1.17.3
272  *
273  * @param WP_Query &$query (passed by reference)
274  *
275  * @return void
276  */
277  public function parse_query_fix_frontpage( &$query ) {
278  global $wp_rewrite;
279 
280  $is_front_page = ( $query->is_home || $query->is_page );
281  $show_on_front = ( 'page' === get_option('show_on_front') );
282  $front_page_id = get_option('page_on_front');
283 
284  if ( $is_front_page && $show_on_front && $front_page_id ) {
285 
286  // Force to be an array, potentially a query string ( entry=16 )
287  $_query = wp_parse_args( $query->query );
288 
289  // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
290  if ( isset( $_query['pagename'] ) && '' === $_query['pagename'] ) {
291  unset( $_query['pagename'] );
292  }
293 
294  // this is where will break from core wordpress
295  /** @internal Don't use this filter; it will be unnecessary soon - it's just a patch for specific use case */
296  $ignore = apply_filters( 'gravityview/internal/ignored_endpoints', array( 'preview', 'page', 'paged', 'cpage' ), $query );
297  $endpoints = \GV\Utils::get( $wp_rewrite, 'endpoints' );
298  foreach ( (array) $endpoints as $endpoint ) {
299  $ignore[] = $endpoint[1];
300  }
301  unset( $endpoints );
302 
303  // Modify the query if:
304  // - We're on the "Page on front" page (which we are), and:
305  // - The query is empty OR
306  // - The query includes keys that are associated with registered endpoints. `entry`, for example.
307  if ( empty( $_query ) || ! array_diff( array_keys( $_query ), $ignore ) ) {
308 
309  $qv =& $query->query_vars;
310 
311  // Prevent redirect when on the single entry endpoint
312  if( self::is_single_entry() ) {
313  add_filter( 'redirect_canonical', '__return_false' );
314  }
315 
316  $query->is_page = true;
317  $query->is_home = false;
318  $qv['page_id'] = $front_page_id;
319 
320  // Correct <!--nextpage--> for page_on_front
321  if ( ! empty( $qv['paged'] ) ) {
322  $qv['page'] = $qv['paged'];
323  unset( $qv['paged'] );
324  }
325  }
326 
327  // reset the is_singular flag after our updated code above
328  $query->is_singular = $query->is_single || $query->is_page || $query->is_attachment;
329  }
330  }
331 
332  /**
333  * Read the $post and process the View data inside
334  * @param array $wp Passed in the `wp` hook. Not used.
335  * @return void
336  */
337  public function parse_content( $wp = array() ) {
338  global $post;
339 
340  // If in admin and NOT AJAX request, get outta here.
341  if ( gravityview()->request->is_admin() ) {
342  return;
343  }
344 
345  $is_GV_post_type = 'gravityview' === get_post_type( $post );
346 
347  // Calculate requested Views
348  $post_content = ! empty( $post->post_content ) ? $post->post_content : null;
349 
350  if ( ! $is_GV_post_type && function_exists( 'parse_blocks' ) && preg_match_all( '/"ref":\d+/', $post_content ) ) {
351  $blocks = parse_blocks( $post_content );
352 
353  foreach ( $blocks as $block ) {
354  if ( empty( $block['attrs']['ref'] ) ) {
355  continue;
356  }
357 
358  $block_post = get_post( $block['attrs']['ref'] );
359 
360  if ( $block_post ) {
361  $post_content .= $block_post->post_content;
362  }
363  }
364 
365  $this->setGvOutputData( GravityView_View_Data::getInstance( $post_content ) );
366  } else {
368  }
369 
370  // !important: we need to run this before getting single entry (to kick the advanced filter)
371  $this->set_context_view_id();
372 
373  $this->setIsGravityviewPostType( $is_GV_post_type );
374 
375  $post_id = $this->getPostId() ? $this->getPostId() : ( isset( $post ) ? $post->ID : null );
376  $this->setPostId( $post_id );
377  $post_has_shortcode = $post_content ? gravityview_has_shortcode_r( $post_content, 'gravityview' ) : false;
378  $this->setPostHasShortcode( $this->isGravityviewPostType() ? null : ! empty( $post_has_shortcode ) );
379 
380  // check if the View is showing search results (only for multiple entries View)
381  $this->setIsSearch( $this->is_searching() );
382 
383  unset( $entry, $post_id, $post_has_shortcode );
384  }
385 
386  /**
387  * Set the entry
388  */
389  function set_entry_data() {
390  $entry_id = self::is_single_entry();
391  $this->setSingleEntry( $entry_id );
392  $this->setEntry( $entry_id );
393  }
394 
395  /**
396  * Checks if the current View is presenting search results
397  *
398  * @since 1.5.4
399  *
400  * @return boolean True: Yes, it's a search; False: No, not a search.
401  */
402  function is_searching() {
403 
404  // It's a single entry, not search
405  if ( $this->getSingleEntry() ) {
406  return false;
407  }
408 
410 
411  if( 'post' === $search_method ) {
412  $get = $_POST;
413  } else {
414  $get = $_GET;
415  }
416 
417  // No $_GET parameters
418  if ( empty( $get ) || ! is_array( $get ) ) {
419  return false;
420  }
421 
422  // Remove empty values
423  $get = array_filter( $get );
424 
425  // If the $_GET parameters are empty, it's no search.
426  if ( empty( $get ) ) {
427  return false;
428  }
429 
430  $search_keys = array_keys( $get );
431 
432  $search_match = implode( '|', self::$search_parameters );
433 
434  foreach ( $search_keys as $search_key ) {
435 
436  // Analyze the search key $_GET parameter and see if it matches known GV args
437  if ( preg_match( '/(' . $search_match . ')/i', $search_key ) ) {
438  return true;
439  }
440  }
441 
442  return false;
443  }
444 
445  /**
446  * Filter the title for the single entry view
447  *
448  * @param string $passed_title Current title
449  * @param int $passed_post_id Post ID
450  * @return string (modified) title
451  */
452  public function single_entry_title( $passed_title, $passed_post_id = null ) {
453  global $post;
454 
455  // Since this is a public method, it can be called outside of the plugin. Don't assume things have been loaded properly.
456  if ( ! class_exists( '\GV\Entry' ) ) {
457  return $passed_title;
458  }
459 
460  $gventry = gravityview()->request->is_entry();
461 
462  // If this is the directory view, return.
463  if( ! $gventry ) {
464  return $passed_title;
465  }
466 
467  $entry = $gventry->as_entry();
468 
469  /**
470  * @filter `gravityview/single/title/out_loop` Apply the Single Entry Title filter outside the WordPress loop?
471  * @param boolean $in_the_loop Whether to apply the filter to the menu title and the meta tag <title> - outside the loop
472  * @param array $entry Current entry
473  */
474  $apply_outside_loop = apply_filters( 'gravityview/single/title/out_loop', in_the_loop(), $entry );
475 
476  if ( ! $apply_outside_loop ) {
477  return $passed_title;
478  }
479 
480  // WooCommerce doesn't $post_id
481  if ( empty( $passed_post_id ) ) {
482  return $passed_title;
483  }
484 
485  // Don't modify the title for anything other than the current view/post.
486  // This is true for embedded shortcodes and Views.
487  if ( is_object( $post ) && (int) $post->ID !== (int) $passed_post_id ) {
488  return $passed_title;
489  }
490 
491  $view = gravityview()->request->is_view();
492 
493  if( $view ) {
494  return $this->_get_single_entry_title( $view, $entry, $passed_title );
495  }
496 
497  $_gvid = \GV\Utils::_GET( 'gvid', null );
498 
499  // $_GET['gvid'] is set; we know what View to render
500  if ( $_gvid ) {
501 
502  $view = \GV\View::by_id( $_gvid );
503 
504  return $this->_get_single_entry_title( $view, $entry, $passed_title );
505  }
506 
507  global $post;
508 
509  if ( ! $post ) {
510  return $passed_title;
511  }
512 
513  $view_collection = \GV\View_Collection::from_post( $post );
514 
515  // We have multiple Views, but no gvid...this isn't valid security
516  if( 1 < $view_collection->count() ) {
517  return $passed_title;
518  }
519 
520  return $this->_get_single_entry_title( $view_collection->first(), $entry, $passed_title );
521  }
522 
523  /**
524  * Returns the single entry title for a View with variables replaced and shortcodes parsed
525  *
526  * @since 2.7.2
527  *
528  * @param \GV\View|null $view
529  * @param array $entry
530  * @param string $passed_title
531  *
532  * @return string
533  */
534  private function _get_single_entry_title( $view, $entry = array(), $passed_title = '' ) {
535 
536  if ( ! $view ) {
537  return $passed_title;
538  }
539 
540  /**
541  * @filter `gravityview/single/title/check_entry_display` Override whether to check entry display rules against filters
542  * @internal This might change in the future! Don't rely on it.
543  * @since 2.7.2
544  * @param bool $check_entry_display Check whether the entry is visible for the current View configuration. Default: true.
545  * @param array $entry Gravity Forms entry array
546  * @param \GV\View $view The View
547  */
548  $check_entry_display = apply_filters( 'gravityview/single/title/check_entry_display', true, $entry, $view );
549 
550  if( $check_entry_display ) {
551 
552  $check_display = GVCommon::check_entry_display( $entry, $view );
553 
554  if( is_wp_error( $check_display ) ) {
555  return $passed_title;
556  }
557  }
558 
559  $title = $view->settings->get( 'single_title', $passed_title );
560 
561  $form = GVCommon::get_form( $entry['form_id'] );
562 
563  // We are allowing HTML in the fields, so no escaping the output
565 
566  $title = do_shortcode( $title );
567 
568  return $title;
569  }
570 
571 
572  /**
573  * In case View post is called directly, insert the view in the post content
574  *
575  * @deprecated Use \GV\View::content() instead.
576  *
577  * @static
578  * @param mixed $content
579  * @return string Add the View output into View CPT content
580  */
581  public function insert_view_in_content( $content ) {
582  gravityview()->log->notice( '\GravityView_frontend::insert_view_in_content is deprecated. Use \GV\View::content()' );
583  return \GV\View::content( $content );
584  }
585 
586  /**
587  * Disable comments on GravityView post types
588  * @param boolean $open existing status
589  * @param int $post_id Post ID
590  * @return boolean
591  */
592  public function comments_open( $open, $post_id ) {
593 
594  if ( $this->isGravityviewPostType() ) {
595  $open = false;
596  }
597 
598  /**
599  * @filter `gravityview/comments_open` Whether to set comments to open or closed.
600  * @since 1.5.4
601  * @param boolean $open Open or closed status
602  * @param int $post_id Post ID to set comment status for
603  */
604  $open = apply_filters( 'gravityview/comments_open', $open, $post_id );
605 
606  return $open;
607  }
608 
609  /**
610  * Display a warning when a View has not been configured
611  *
612  * @since 1.19.2
613  *
614  * @param int $view_id The ID of the View currently being displayed
615  *
616  * @return void
617  */
618  public function context_not_configured_warning( $view_id = 0 ) {
619 
620  if ( ! class_exists( 'GravityView_View' ) ) {
621  return;
622  }
623 
624  $fields = GravityView_View::getInstance()->getContextFields();
625 
626  if ( ! empty( $fields ) ) {
627  return;
628  }
629 
630  $context = GravityView_View::getInstance()->getContext();
631 
632  switch( $context ) {
633  case 'directory':
634  $tab = __( 'Multiple Entries', 'gravityview' );
635  break;
636  case 'edit':
637  $tab = __( 'Edit Entry', 'gravityview' );
638  break;
639  case 'single':
640  default:
641  $tab = __( 'Single Entry', 'gravityview' );
642  break;
643  }
644 
645 
646  $title = sprintf( esc_html_x('The %s layout has not been configured.', 'Displayed when a View is not configured. %s is replaced by the tab label', 'gravityview' ), $tab );
647  $edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $view_id, $context ) );
648  $action_text = sprintf( esc_html__('Add fields to %s', 'gravityview' ), $tab );
649  $message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
650 
651  $image = sprintf( '<img alt="%s" src="%s" style="margin-top: 10px;" />', $tab, esc_url(plugins_url( sprintf( 'assets/images/tab-%s.png', $context ), GRAVITYVIEW_FILE ) ) );
652  $output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message );
653 
654  echo GVCommon::generate_notice( $output . $image, 'gv-error error', 'edit_gravityview', $view_id );
655  }
656 
657 
658  /**
659  * Core function to render a View based on a set of arguments
660  *
661  * @static
662  * @param array $passed_args {
663  *
664  * Settings for rendering the View
665  *
666  * @type int $id View id
667  * @type int $page_size Number of entries to show per page
668  * @type string $sort_field Form field id to sort
669  * @type string $sort_direction Sorting direction ('ASC', 'DESC', or 'RAND')
670  * @type string $start_date - Ymd
671  * @type string $end_date - Ymd
672  * @type string $class - assign a html class to the view
673  * @type string $offset (optional) - This is the start point in the current data set (0 index based).
674  * }
675  *
676  * @deprecated Use \GV\View_Renderer
677  *
678  * @return string|null HTML output of a View, NULL if View isn't found
679  */
680  public function render_view( $passed_args ) {
681  gravityview()->log->notice( '\GravityView_frontend::render_view is deprecated. Use \GV\View_Renderer etc.' );
682 
683  /**
684  * We can use a shortcode here, since it's pretty much the same.
685  *
686  * But we do need to check embed permissions, since shortcodes don't do this.
687  */
688 
689  if ( ! $view = gravityview()->views->get( $passed_args ) ) {
690  return null;
691  }
692 
693  $view->settings->update( $passed_args );
694 
695  $direct_access = apply_filters( 'gravityview_direct_access', true, $view->ID );
696  $embed_only = $view->settings->get( 'embed_only' );
697 
698  if( ! $direct_access || ( $embed_only && ! GVCommon::has_cap( 'read_private_gravityviews' ) ) ) {
699  return __( 'You are not allowed to view this content.', 'gravityview' );
700  }
701 
702  $shortcode = new \GV\Shortcodes\gravityview();
703  return $shortcode->callback( $passed_args );
704  }
705 
706  /**
707  * Process the start and end dates for a view - overrides values defined in shortcode (if needed)
708  *
709  * The `start_date` and `end_date` keys need to be in a format processable by GFFormsModel::get_date_range_where(),
710  * which uses \DateTime() format.
711  *
712  * You can set the `start_date` or `end_date` to any value allowed by {@link http://www.php.net//manual/en/function.strtotime.php strtotime()},
713  * including strings like "now" or "-1 year" or "-3 days".
714  *
715  * @see GFFormsModel::get_date_range_where
716  *
717  * @param array $args View settings
718  * @param array $search_criteria Search being performed, if any
719  * @return array Modified `$search_criteria` array
720  */
721  public static function process_search_dates( $args, $search_criteria = array() ) {
722 
723  $return_search_criteria = $search_criteria;
724 
725  foreach ( array( 'start_date', 'end_date' ) as $key ) {
726 
727 
728  // Is the start date or end date set in the view or shortcode?
729  // If so, we want to make sure that the search doesn't go outside the bounds defined.
730  if ( ! empty( $args[ $key ] ) ) {
731 
732  // Get a timestamp and see if it's a valid date format
733  $date = strtotime( $args[ $key ], GFCommon::get_local_timestamp() );
734 
735  // The date was invalid
736  if ( empty( $date ) ) {
737  gravityview()->log->error( ' Invalid {key} date format: {format}', array( 'key' => $key, 'format' => $args[ $key ] ) );
738  continue;
739  }
740 
741  // The format that Gravity Forms expects for start_date and day-specific (not hour/second-specific) end_date
742  $datetime_format = 'Y-m-d H:i:s';
743  $search_is_outside_view_bounds = false;
744 
745  if( ! empty( $search_criteria[ $key ] ) ) {
746 
747  $search_date = strtotime( $search_criteria[ $key ], GFCommon::get_local_timestamp() );
748 
749  // The search is for entries before the start date defined by the settings
750  switch ( $key ) {
751  case 'end_date':
752  /**
753  * If the end date is formatted as 'Y-m-d', it should be formatted without hours and seconds
754  * so that Gravity Forms can convert the day to 23:59:59 the previous day.
755  *
756  * If it's a relative date ("now" or "-1 day"), then it should use the precise date format
757  *
758  * @see GFFormsModel::get_date_range_where
759  */
760  $datetime_format = gravityview_is_valid_datetime( $args[ $key ] ) ? 'Y-m-d' : $datetime_format;
761  $search_is_outside_view_bounds = ( $search_date > $date );
762  break;
763  case 'start_date':
764  $search_is_outside_view_bounds = ( $search_date < $date );
765  break;
766  }
767  }
768 
769  // If there is no search being performed, or if there is a search being performed that's outside the bounds
770  if ( empty( $search_criteria[ $key ] ) || $search_is_outside_view_bounds ) {
771 
772  // Then we override the search and re-set the start date
773  $return_search_criteria[ $key ] = date_i18n( $datetime_format , $date, true );
774  }
775  }
776  }
777 
778  if( isset( $return_search_criteria['start_date'] ) && isset( $return_search_criteria['end_date'] ) ) {
779  // The start date is AFTER the end date. This will result in no results, but let's not force the issue.
780  if ( strtotime( $return_search_criteria['start_date'] ) > strtotime( $return_search_criteria['end_date'] ) ) {
781  gravityview()->log->error( 'Invalid search: the start date is after the end date.', array( 'data' => $return_search_criteria ) );
782  }
783  }
784 
785  return $return_search_criteria;
786  }
787 
788 
789  /**
790  * Process the approved only search criteria according to the View settings
791  *
792  * @param array $args View settings
793  * @param array $search_criteria Search being performed, if any
794  * @return array Modified `$search_criteria` array
795  */
796  public static function process_search_only_approved( $args, $search_criteria ) {
797 
798  /** @since 1.19 */
799  if( ! empty( $args['admin_show_all_statuses'] ) && GVCommon::has_cap('gravityview_moderate_entries') ) {
800  gravityview()->log->debug( 'User can moderate entries; showing all approval statuses' );
801  return $search_criteria;
802  }
803 
804  if ( ! empty( $args['show_only_approved'] ) ) {
805 
806  $search_criteria['field_filters'][] = array(
809  );
810 
811  $search_criteria['field_filters']['mode'] = 'all'; // force all the criterias to be met
812 
813  gravityview()->log->debug( '[process_search_only_approved] Search Criteria if show only approved: ', array( 'data' => $search_criteria ) );
814  }
815 
816  return $search_criteria;
817  }
818 
819 
820  /**
821  * Check if a certain entry is approved.
822  *
823  * If we pass the View settings ($args) it will check the 'show_only_approved' setting before
824  * checking the entry approved field, returning true if show_only_approved = false.
825  *
826  * @since 1.7
827  * @since 1.18 Converted check to use GravityView_Entry_Approval_Status::is_approved
828  *
829  * @uses GravityView_Entry_Approval_Status::is_approved
830  *
831  * @param array $entry Entry object
832  * @param array $args View settings (optional)
833  *
834  * @return bool
835  */
836  public static function is_entry_approved( $entry, $args = array() ) {
837 
838  if ( empty( $entry['id'] ) || ( array_key_exists( 'show_only_approved', $args ) && ! $args['show_only_approved'] ) ) {
839  // is implicitly approved if entry is null or View settings doesn't require to check for approval
840  return true;
841  }
842 
843  /** @since 1.19 */
844  if( ! empty( $args['admin_show_all_statuses'] ) && GVCommon::has_cap('gravityview_moderate_entries') ) {
845  gravityview()->log->debug( 'User can moderate entries, so entry is approved for viewing' );
846  return true;
847  }
848 
849  $is_approved = gform_get_meta( $entry['id'], GravityView_Entry_Approval::meta_key );
850 
851  return GravityView_Entry_Approval_Status::is_approved( $is_approved );
852  }
853 
854  /**
855  * Parse search criteria for a entries search.
856  *
857  * array(
858  * 'search_field' => 1, // ID of the field
859  * 'search_value' => '', // Value of the field to search
860  * 'search_operator' => 'contains', // 'is', 'isnot', '>', '<', 'contains'
861  * 'show_only_approved' => 0 or 1 // Boolean
862  * )
863  *
864  * @param array $args Array of args
865  * @param int $form_id Gravity Forms form ID
866  * @return array Array of search parameters, formatted in Gravity Forms mode, using `status` key set to "active" by default, `field_filters` array with `key`, `value` and `operator` keys.
867  */
868  public static function get_search_criteria( $args, $form_id ) {
869  /**
870  * Compatibility with filters hooking in `gravityview_search_criteria` instead of `gravityview_fe_search_criteria`.
871  */
872  $criteria = apply_filters( 'gravityview_search_criteria', array(), array( $form_id ), \GV\Utils::get( $args, 'id' ) );
873  $search_criteria = isset( $criteria['search_criteria'] ) ? $criteria['search_criteria'] : array( 'field_filters' => array() );
874 
875  /**
876  * @filter `gravityview_fe_search_criteria` Modify the search criteria
877  * @see GravityView_Widget_Search::filter_entries Adds the default search criteria
878  * @param array $search_criteria Empty `field_filters` key
879  * @param int $form_id ID of the Gravity Forms form that is being searched
880  * @param array $args The View settings.
881  */
882  $search_criteria = apply_filters( 'gravityview_fe_search_criteria', $search_criteria, $form_id, $args );
883 
884  if ( ! is_array( $search_criteria ) ) {
885  return array();
886  }
887 
888  $original_search_criteria = $search_criteria;
889 
890  gravityview()->log->debug( '[get_search_criteria] Search Criteria after hook gravityview_fe_search_criteria: ', array( 'data' =>$search_criteria ) );
891 
892  // implicity search
893  if ( ! empty( $args['search_value'] ) ) {
894 
895  // Search operator options. Options: `is` or `contains`
896  $operator = ! empty( $args['search_operator'] ) && in_array( $args['search_operator'], array( 'is', 'isnot', '>', '<', 'contains' ) ) ? $args['search_operator'] : 'contains';
897 
898  $search_criteria['field_filters'][] = array(
899  'key' => \GV\Utils::_GET( 'search_field', \GV\Utils::get( $args, 'search_field' ) ), // The field ID to search
900  'value' => _wp_specialchars( $args['search_value'] ), // The value to search. Encode ampersands but not quotes.
901  'operator' => $operator,
902  );
903 
904  // Lock search mode to "all" with implicit presearch filter.
905  $search_criteria['field_filters']['mode'] = 'all';
906  }
907 
908  if( $search_criteria !== $original_search_criteria ) {
909  gravityview()->log->debug( '[get_search_criteria] Search Criteria after implicity search: ', array( 'data' => $search_criteria ) );
910  }
911 
912  // Handle setting date range
913  $search_criteria = self::process_search_dates( $args, $search_criteria );
914 
915  if( $search_criteria !== $original_search_criteria ) {
916  gravityview()->log->debug( '[get_search_criteria] Search Criteria after date params: ', array( 'data' => $search_criteria ) );
917  }
918 
919  // remove not approved entries
920  $search_criteria = self::process_search_only_approved( $args, $search_criteria );
921 
922  /**
923  * @filter `gravityview_status` Modify entry status requirements to be included in search results.
924  * @param string $status Default: `active`. Accepts all Gravity Forms entry statuses, including `spam` and `trash`
925  */
926  $search_criteria['status'] = apply_filters( 'gravityview_status', 'active', $args );
927 
928  return $search_criteria;
929  }
930 
931 
932 
933  /**
934  * Core function to calculate View multi entries (directory) based on a set of arguments ($args):
935  * $id - View id
936  * $page_size - Page
937  * $sort_field - form field id to sort
938  * $sort_direction - ASC / DESC
939  * $start_date - Ymd
940  * $end_date - Ymd
941  * $class - assign a html class to the view
942  * $offset (optional) - This is the start point in the current data set (0 index based).
943  *
944  *
945  *
946  * @uses gravityview_get_entries()
947  * @param array $args\n
948  * - $id - View id
949  * - $page_size - Page
950  * - $sort_field - form field id to sort
951  * - $sort_direction - ASC / DESC
952  * - $start_date - Ymd
953  * - $end_date - Ymd
954  * - $class - assign a html class to the view
955  * - $offset (optional) - This is the start point in the current data set (0 index based).
956  * @param int $form_id Gravity Forms Form ID
957  * @return array Associative array with `count`, `entries`, and `paging` keys. `count` has the total entries count, `entries` is an array with Gravity Forms full entry data, `paging` is an array with `offset` and `page_size` keys
958  */
959  public static function get_view_entries( $args, $form_id ) {
960 
961  gravityview()->log->debug( '[get_view_entries] init' );
962  // start filters and sorting
963 
964  $parameters = self::get_view_entries_parameters( $args, $form_id );
965 
966  $count = 0; // Must be defined so that gravityview_get_entries can use by reference
967 
968  // fetch entries
969  list( $entries, $paging, $count ) =
971 
972  gravityview()->log->debug( 'Get Entries. Found: {count} entries', array( 'count' => $count, 'data' => $entries ) );
973 
974  /**
975  * @filter `gravityview_view_entries` Filter the entries output to the View
976  * @deprecated since 1.5.2
977  * @param array $args View settings associative array
978  * @var array
979  */
980  $entries = apply_filters( 'gravityview_view_entries', $entries, $args );
981 
982  $return = array(
983  'count' => $count,
984  'entries' => $entries,
985  'paging' => $paging,
986  );
987 
988  /**
989  * @filter `gravityview/view/entries` Filter the entries output to the View
990  * @param array $criteria associative array containing count, entries & paging
991  * @param array $args View settings associative array
992  * @since 1.5.2
993  */
994  return apply_filters( 'gravityview/view/entries', $return, $args );
995  }
996 
997  /**
998  * Get an array of search parameters formatted as Gravity Forms requires
999  *
1000  * Results are filtered by `gravityview_get_entries` and `gravityview_get_entries_{View ID}` filters
1001  *
1002  * @uses GravityView_frontend::get_search_criteria
1003  * @uses GravityView_frontend::get_search_criteria_paging
1004  *
1005  * @since 1.20
1006  *
1007  * @see \GV\View_Settings::defaults For $args options
1008  *
1009  * @param array $args Array of View settings, as structured in \GV\View_Settings::defaults
1010  * @param int $form_id Gravity Forms form ID to search
1011  *
1012  * @return array With `search_criteria`, `sorting`, `paging`, `cache` keys
1013  */
1014  public static function get_view_entries_parameters( $args = array(), $form_id = 0 ) {
1015 
1016 
1017  if ( ! is_array( $args ) || ! is_numeric( $form_id ) ) {
1018 
1019  gravityview()->log->error( 'Passed args are not an array or the form ID is not numeric' );
1020 
1021  return array();
1022  }
1023 
1024  $form_id = intval( $form_id );
1025 
1026  /**
1027  * Process search parameters
1028  * @var array
1029  */
1030  $search_criteria = self::get_search_criteria( $args, $form_id );
1031 
1032  $paging = self::get_search_criteria_paging( $args );
1033 
1034  $parameters = array(
1035  'search_criteria' => $search_criteria,
1036  'sorting' => self::updateViewSorting( $args, $form_id ),
1037  'paging' => $paging,
1038  'cache' => isset( $args['cache'] ) ? $args['cache'] : true,
1039  );
1040 
1041  /**
1042  * @filter `gravityview_get_entries` Filter get entries criteria
1043  * @param array $parameters Array with `search_criteria`, `sorting` and `paging` keys.
1044  * @param array $args View configuration args. {
1045  * @type int $id View id
1046  * @type int $page_size Number of entries to show per page
1047  * @type string $sort_field Form field id to sort
1048  * @type string $sort_direction Sorting direction ('ASC', 'DESC', or 'RAND')
1049  * @type string $start_date - Ymd
1050  * @type string $end_date - Ymd
1051  * @type string $class - assign a html class to the view
1052  * @type string $offset (optional) - This is the start point in the current data set (0 index based).
1053  * }
1054  * @param int $form_id ID of Gravity Forms form
1055  */
1056  $parameters = apply_filters( 'gravityview_get_entries', $parameters, $args, $form_id );
1057 
1058  /**
1059  * @filter `gravityview_get_entries_{View ID}` Filter get entries criteria
1060  * @param array $parameters Array with `search_criteria`, `sorting` and `paging` keys.
1061  * @param array $args View configuration args.
1062  */
1063  $parameters = apply_filters( 'gravityview_get_entries_'.\GV\Utils::get( $args, 'id' ), $parameters, $args, $form_id );
1064 
1065  gravityview()->log->debug( '$parameters passed to gravityview_get_entries(): ', array( 'data' => $parameters ) );
1066 
1067  return $parameters;
1068  }
1069 
1070  /**
1071  * Get the paging array for the View
1072  *
1073  * @since 1.19.5
1074  *
1075  * @param $args
1076  * @param int $form_id
1077  */
1078  public static function get_search_criteria_paging( $args ) {
1079 
1080  /**
1081  * @filter `gravityview_default_page_size` The default number of entries displayed in a View
1082  * @since 1.1.6
1083  * @param int $default_page_size Default: 25
1084  */
1085  $default_page_size = apply_filters( 'gravityview_default_page_size', 25 );
1086 
1087  // Paging & offset
1088  $page_size = ! empty( $args['page_size'] ) ? intval( $args['page_size'] ) : $default_page_size;
1089 
1090  if ( -1 === $page_size ) {
1091  $page_size = PHP_INT_MAX;
1092  }
1093 
1094  $curr_page = empty( $_GET['pagenum'] ) ? 1 : intval( $_GET['pagenum'] );
1095  $offset = ( $curr_page - 1 ) * $page_size;
1096 
1097  if ( ! empty( $args['offset'] ) ) {
1098  $offset += intval( $args['offset'] );
1099  }
1100 
1101  $paging = array(
1102  'offset' => $offset,
1103  'page_size' => $page_size,
1104  );
1105 
1106  gravityview()->log->debug( 'Paging: ', array( 'data' => $paging ) );
1107 
1108  return $paging;
1109  }
1110 
1111  /**
1112  * Updates the View sorting criteria
1113  *
1114  * @since 1.7
1115  *
1116  * @param array $args View settings. Required to have `sort_field` and `sort_direction` keys
1117  * @param int $form_id The ID of the form used to sort
1118  * @return array $sorting Array with `key`, `direction` and `is_numeric` keys
1119  */
1120  public static function updateViewSorting( $args, $form_id ) {
1121  $sorting = array();
1122 
1123  $has_values = isset( $_GET['sort'] );
1124 
1125  if ( $has_values && is_array( $_GET['sort'] ) ) {
1126  $sorts = array_keys( $_GET['sort'] );
1127  $dirs = array_values( $_GET['sort'] );
1128 
1129  if ( $has_values = array_filter( $dirs ) ) {
1130  $sort_field_id = end( $sorts );
1131  $sort_direction = end( $dirs );
1132  }
1133  }
1134 
1135  if ( ! isset( $sort_field_id ) ) {
1136  $sort_field_id = isset( $_GET['sort'] ) ? $_GET['sort'] : \GV\Utils::get( $args, 'sort_field' );
1137  }
1138 
1139  if ( ! isset( $sort_direction ) ) {
1140  $sort_direction = isset( $_GET['dir'] ) ? $_GET['dir'] : \GV\Utils::get( $args, 'sort_direction' );
1141  }
1142 
1143  if ( is_array( $sort_field_id ) ) {
1144  $sort_field_id = array_pop( $sort_field_id );
1145  }
1146 
1147  if ( is_array( $sort_direction ) ) {
1148  $sort_direction = array_pop( $sort_direction );
1149  }
1150 
1151  if ( ! empty( $sort_field_id ) ) {
1152  if ( is_array( $sort_field_id ) ) {
1153  $sort_direction = array_values( $sort_field_id );
1154  $sort_field_id = array_keys( $sort_field_id );
1155 
1156  $sort_field_id = reset( $sort_field_id );
1157  $sort_direction = reset( $sort_direction );
1158  }
1159 
1160  $sort_field_id = self::_override_sorting_id_by_field_type( $sort_field_id, $form_id );
1161  $sorting = array(
1162  'key' => $sort_field_id,
1163  'direction' => strtolower( $sort_direction ),
1164  'is_numeric' => GVCommon::is_field_numeric( $form_id, $sort_field_id )
1165  );
1166 
1167  if ( 'RAND' === $sort_direction ) {
1168 
1169  $form = GFAPI::get_form( $form_id );
1170 
1171  // Get the first GF_Field field ID, set as the key for entry randomization
1172  if ( ! empty( $form['fields'] ) ) {
1173 
1174  /** @var GF_Field $field */
1175  foreach ( $form['fields'] as $field ) {
1176  if ( ! is_a( $field, 'GF_Field' ) ) {
1177  continue;
1178  }
1179 
1180  $sorting = array(
1181  'key' => $field->id,
1182  'is_numeric' => false,
1183  'direction' => 'RAND',
1184  );
1185 
1186  break;
1187  }
1188  }
1189  }
1190  }
1191 
1192  GravityView_View::getInstance()->setSorting( $sorting );
1193 
1194  gravityview()->log->debug( '[updateViewSorting] Sort Criteria : ', array( 'data' => $sorting ) );
1195 
1196  return $sorting;
1197 
1198  }
1199 
1200  /**
1201  * Override sorting per field
1202  *
1203  * Currently only modifies sorting ID when sorting by the full name. Sorts by first name.
1204  * Use the `gravityview/sorting/full-name` filter to override.
1205  *
1206  * @todo Filter from GravityView_Field
1207  * @since 1.7.4
1208  * @internal Hi developer! Although this is public, don't call this method; we're going to replace it.
1209  *
1210  * @param int|string|array $sort_field_id Field used for sorting (`id` or `1.2`), or an array for multisorts
1211  * @param int $form_id GF Form ID
1212  *
1213  * @return string|array Possibly modified sorting ID. Array if $sort_field_id is passed as array.
1214  */
1215  public static function _override_sorting_id_by_field_type( $sort_field_id, $form_id ) {
1216 
1217  if ( is_array( $sort_field_id ) ) {
1218  $modified_ids = array();
1219  foreach ( $sort_field_id as $_sort_field_id ) {
1220  $modified_ids []= self::_override_sorting_id_by_field_type( $_sort_field_id, $form_id );
1221  }
1222  return $modified_ids;
1223  }
1224 
1226 
1227  $sort_field = GFFormsModel::get_field( $form, $sort_field_id );
1228 
1229  if( ! $sort_field ) {
1230  return $sort_field_id;
1231  }
1232 
1233  switch ( $sort_field['type'] ) {
1234 
1235  case 'address':
1236  // Sorting by full address
1237  if ( floatval( $sort_field_id ) === floor( $sort_field_id ) ) {
1238 
1239  /**
1240  * Override how to sort when sorting address
1241  *
1242  * @since 1.8
1243  *
1244  * @param string $address_part `street`, `street2`, `city`, `state`, `zip`, or `country` (default: `city`)
1245  * @param string $sort_field_id Field used for sorting
1246  * @param int $form_id GF Form ID
1247  */
1248  $address_part = apply_filters( 'gravityview/sorting/address', 'city', $sort_field_id, $form_id );
1249 
1250  switch( strtolower( $address_part ) ){
1251  case 'street':
1252  $sort_field_id .= '.1';
1253  break;
1254  case 'street2':
1255  $sort_field_id .= '.2';
1256  break;
1257  default:
1258  case 'city':
1259  $sort_field_id .= '.3';
1260  break;
1261  case 'state':
1262  $sort_field_id .= '.4';
1263  break;
1264  case 'zip':
1265  $sort_field_id .= '.5';
1266  break;
1267  case 'country':
1268  $sort_field_id .= '.6';
1269  break;
1270  }
1271 
1272  }
1273  break;
1274  case 'name':
1275  // Sorting by full name, not first, last, etc.
1276  if ( floatval( $sort_field_id ) === floor( $sort_field_id ) ) {
1277  /**
1278  * @filter `gravityview/sorting/full-name` Override how to sort when sorting full name.
1279  * @since 1.7.4
1280  * @param[in,out] string $name_part Sort by `first` or `last` (default: `first`)
1281  * @param[in] string $sort_field_id Field used for sorting
1282  * @param[in] int $form_id GF Form ID
1283  */
1284  $name_part = apply_filters( 'gravityview/sorting/full-name', 'first', $sort_field_id, $form_id );
1285 
1286  if ( 'last' === strtolower( $name_part ) ) {
1287  $sort_field_id .= '.6';
1288  } else {
1289  $sort_field_id .= '.3';
1290  }
1291  }
1292  break;
1293  case 'list':
1294  $sort_field_id = false;
1295  break;
1296  case 'time':
1297 
1298  /**
1299  * @filter `gravityview/sorting/time` Override how to sort when sorting time
1300  * @see GravityView_Field_Time
1301  * @since 1.14
1302  * @param[in,out] string $name_part Field used for sorting
1303  * @param[in] int $form_id GF Form ID
1304  */
1305  $sort_field_id = apply_filters( 'gravityview/sorting/time', $sort_field_id, $form_id );
1306  break;
1307  }
1308 
1309  return $sort_field_id;
1310  }
1311 
1312  /**
1313  * Verify if user requested a single entry view
1314  * @since 2.3.3 Added return null
1315  * @return boolean|string|null false if not, single entry slug if true, null if \GV\Entry doesn't exist yet
1316  */
1317  public static function is_single_entry() {
1318 
1319  // Since this is a public method, it can be called outside of the plugin. Don't assume things have been loaded properly.
1320  if ( ! class_exists( '\GV\Entry' ) ) {
1321 
1322  // Not using gravityview()->log->error(), since that may not exist yet either!
1323  do_action( 'gravityview_log_error', '\GV\Entry not defined yet. Backtrace: ' . wp_debug_backtrace_summary() );
1324 
1325  return null;
1326  }
1327 
1328  $var_name = \GV\Entry::get_endpoint_name();
1329 
1330  $single_entry = get_query_var( $var_name );
1331 
1332  /**
1333  * Modify the entry that is being displayed.
1334  *
1335  * @internal Should only be used by things like the oEmbed functionality.
1336  * @since 1.6
1337  */
1338  $single_entry = apply_filters( 'gravityview/is_single_entry', $single_entry );
1339 
1340  if ( empty( $single_entry ) ){
1341  return false;
1342  } else {
1343  return $single_entry;
1344  }
1345  }
1346 
1347 
1348  /**
1349  * Register styles and scripts
1350  *
1351  * @return void
1352  */
1353  public function add_scripts_and_styles() {
1354  global $post, $posts;
1355  // enqueue template specific styles
1356  if ( $this->getGvOutputData() ) {
1357 
1358  $views = $this->getGvOutputData()->get_views();
1359 
1360  foreach ( $views as $view_id => $data ) {
1361  $view = \GV\View::by_id( $data['id'] );
1362  $view_id = $view->ID;
1363  $template_id = gravityview_get_template_id( $view->ID );
1364  $data = $view->as_data();
1365 
1366  /**
1367  * Don't enqueue the scripts or styles if it's not going to be displayed.
1368  * @since 1.15
1369  */
1370  if( is_user_logged_in() && false === GVCommon::has_cap( 'read_gravityview', $view_id ) ) {
1371  continue;
1372  }
1373 
1374  // By default, no thickbox
1375  $js_dependencies = array( 'jquery', 'gravityview-jquery-cookie' );
1376  $css_dependencies = array();
1377 
1378  $lightbox = $view->settings->get( 'lightbox' );
1379 
1380  // If the thickbox is enqueued, add dependencies
1381  if ( $lightbox ) {
1382 
1383  global $wp_filter;
1384 
1385  /**
1386  * @filter `gravity_view_lightbox_script` Override the lightbox script to enqueue. Default: `thickbox`
1387  * @param string $script_slug If you want to use a different lightbox script, return the name of it here.
1388  * @deprecated 2.5.1 Naming. See `gravityview_lightbox_script` instead.
1389  */
1390  $js_dependency = apply_filters_deprecated( 'gravity_view_lightbox_script', array( 'thickbox' ), '2.5.1', 'gravityview_lightbox_script' );
1391 
1392  /**
1393  * @filter `gravityview_lightbox_script` Override the lightbox script to enqueue. Default: `thickbox`
1394  * @since 2.5.1
1395  * @param string $script_slug If you want to use a different lightbox script, return the name of it here.
1396  * @param \GV\View The View.
1397  */
1398  $js_dependency = apply_filters( 'gravityview_lightbox_script', $js_dependency, $view );
1399  $js_dependencies[] = $js_dependency;
1400 
1401  /**
1402  * @filter `gravity_view_lightbox_style` Modify the lightbox CSS slug. Default: `thickbox`
1403  * @param string $script_slug If you want to use a different lightbox script, return the name of its CSS file here.
1404  * @deprecated 2.5.1 Naming. See `gravityview_lightbox_style` instead.
1405  */
1406  $css_dependency = apply_filters_deprecated( 'gravity_view_lightbox_style', array( 'thickbox' ), '2.5.1', 'gravityview_lightbox_style' );
1407 
1408  /**
1409  * @filter `gravityview_lightbox_script` Override the lightbox script to enqueue. Default: `thickbox`
1410  * @since 2.5.1
1411  * @param string $script_slug If you want to use a different lightbox script, return the name of it here.
1412  * @param \GV\View The View.
1413  */
1414  $css_dependency = apply_filters( 'gravityview_lightbox_style', $css_dependency, $view );
1415  $css_dependencies[] = $css_dependency;
1416  }
1417 
1418  /**
1419  * If the form has checkbox fields, enqueue dashicons
1420  * @see https://github.com/katzwebservices/GravityView/issues/536
1421  * @since 1.15
1422  */
1423  if( gravityview_view_has_single_checkbox_or_radio( $data['form'], $data['fields'] ) ) {
1424  $css_dependencies[] = 'dashicons';
1425  }
1426 
1427  wp_register_script( 'gravityview-jquery-cookie', plugins_url( 'assets/lib/jquery.cookie/jquery.cookie.min.js', GRAVITYVIEW_FILE ), array( 'jquery' ), GravityView_Plugin::version, true );
1428 
1429  $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
1430 
1431  wp_register_script( 'gravityview-fe-view', plugins_url( 'assets/js/fe-views' . $script_debug . '.js', GRAVITYVIEW_FILE ), apply_filters( 'gravityview_js_dependencies', $js_dependencies ) , GravityView_Plugin::version, true );
1432 
1433  wp_enqueue_script( 'gravityview-fe-view' );
1434 
1435  if ( ! empty( $data['atts']['sort_columns'] ) ) {
1436  wp_enqueue_style( 'gravityview_font', plugins_url( 'assets/css/font.css', GRAVITYVIEW_FILE ), $css_dependencies, GravityView_Plugin::version, 'all' );
1437  }
1438 
1439  $this->enqueue_default_style( $css_dependencies );
1440 
1441  self::add_style( $template_id );
1442  }
1443 
1444  if ( 'wp_print_footer_scripts' === current_filter() ) {
1445 
1446  $js_localization = array(
1447  'cookiepath' => COOKIEPATH,
1448  'clear' => _x( 'Clear', 'Clear all data from the form', 'gravityview' ),
1449  'reset' => _x( 'Reset', 'Reset the search form to the state that existed on page load', 'gravityview' ),
1450  );
1451 
1452  /**
1453  * @filter `gravityview_js_localization` Modify the array passed to wp_localize_script()
1454  * @param array $js_localization The data padded to the Javascript file
1455  * @param array $views Array of View data arrays with View settings
1456  */
1457  $js_localization = apply_filters( 'gravityview_js_localization', $js_localization, $views );
1458 
1459  wp_localize_script( 'gravityview-fe-view', 'gvGlobals', $js_localization );
1460  }
1461  }
1462  }
1463 
1464  /**
1465  * Handle enqueuing the `gravityview_default_style` stylesheet
1466  *
1467  * @since 1.17
1468  *
1469  * @param array $css_dependencies Dependencies for the `gravityview_default_style` stylesheet
1470  *
1471  * @return void
1472  */
1473  private function enqueue_default_style( $css_dependencies = array() ) {
1474 
1475  /**
1476  * @filter `gravityview_use_legacy_search_css` Should GravityView use the legacy Search Bar stylesheet (from before Version 1.17)?
1477  * @since 1.17
1478  * @param bool $use_legacy_search_style If true, loads `gv-legacy-search(-rtl).css`. If false, loads `gv-default-styles(-rtl).css`. `-rtl` is added on RTL websites. Default: `false`
1479  */
1480  $use_legacy_search_style = apply_filters( 'gravityview_use_legacy_search_style', false );
1481 
1482  $rtl = is_rtl() ? '-rtl' : '';
1483 
1484  $css_file_base = $use_legacy_search_style ? 'gv-legacy-search' : 'gv-default-styles';
1485 
1486  $path = gravityview_css_url( $css_file_base . $rtl . '.css' );
1487 
1488  wp_enqueue_style( 'gravityview_default_style', $path, $css_dependencies, GravityView_Plugin::version, 'all' );
1489  }
1490 
1491  /**
1492  * Add template extra style if exists
1493  * @param string $template_id
1494  */
1495  public static function add_style( $template_id ) {
1496 
1497  if ( ! empty( $template_id ) && wp_style_is( 'gravityview_style_' . $template_id, 'registered' ) ) {
1498  gravityview()->log->debug( 'Adding extra template style for {template_id}', array( 'template_id' => $template_id ) );
1499  wp_enqueue_style( 'gravityview_style_' . $template_id );
1500  } elseif ( empty( $template_id ) ) {
1501  gravityview()->log->error( 'Cannot add template style; template_id is empty' );
1502  } else {
1503  gravityview()->log->error( 'Cannot add template style; {template_id} is not registered', array( 'template_id' => 'gravityview_style_' . $template_id ) );
1504  }
1505 
1506  }
1507 
1508 
1509  /**
1510  * Inject the sorting links on the table columns
1511  *
1512  * Callback function for hook 'gravityview/template/field_label'
1513  * @see GravityView_API::field_label() (in includes/class-api.php)
1514  *
1515  * @since 1.7
1516  *
1517  * @param string $label Field label
1518  * @param array $field Field settings
1519  * @param array $form Form object
1520  *
1521  * @return string Field Label
1522  */
1523  public function add_columns_sort_links( $label = '', $field, $form ) {
1524 
1525  /**
1526  * Not a table-based template; don't add sort icons
1527  * @since 1.12
1528  */
1529  if( ! preg_match( '/table/ism', GravityView_View::getInstance()->getTemplatePartSlug() ) ) {
1530  return $label;
1531  }
1532 
1533  if ( ! $this->is_field_sortable( $field['id'], $form ) ) {
1534  return $label;
1535  }
1536 
1537  $sorting = GravityView_View::getInstance()->getSorting();
1538 
1539  $class = 'gv-sort';
1540 
1541  $sort_field_id = self::_override_sorting_id_by_field_type( $field['id'], $form['id'] );
1542 
1543  $sort_args = array(
1544  'sort' => $field['id'],
1545  'dir' => 'asc',
1546  );
1547 
1548  if ( ! empty( $sorting['key'] ) && (string) $sort_field_id === (string) $sorting['key'] ) {
1549  //toggle sorting direction.
1550  if ( 'asc' === $sorting['direction'] ) {
1551  $sort_args['dir'] = 'desc';
1552  $class .= ' gv-icon-sort-desc';
1553  } else {
1554  $sort_args['dir'] = 'asc';
1555  $class .= ' gv-icon-sort-asc';
1556  }
1557  } else {
1558  $class .= ' gv-icon-caret-up-down';
1559  }
1560 
1561  $url = add_query_arg( $sort_args, remove_query_arg( array('pagenum') ) );
1562 
1563  return '<a href="'. esc_url_raw( $url ) .'" class="'. $class .'" ></a>&nbsp;'. $label;
1564 
1565  }
1566 
1567  /**
1568  * Checks if field (column) is sortable
1569  *
1570  * @param string $field Field settings
1571  * @param array $form Gravity Forms form array
1572  *
1573  * @since 1.7
1574  *
1575  * @return bool True: Yes, field is sortable; False: not sortable
1576  */
1577  public function is_field_sortable( $field_id = '', $form = array() ) {
1578 
1579  $field_type = $field_id;
1580 
1581  if( is_numeric( $field_id ) ) {
1582  $field = GFFormsModel::get_field( $form, $field_id );
1583  $field_type = $field ? $field->type : $field_id;
1584  }
1585 
1586  $not_sortable = array(
1587  'edit_link',
1588  'delete_link',
1589  );
1590 
1591  /**
1592  * @filter `gravityview/sortable/field_blacklist` Modify what fields should never be sortable.
1593  * @since 1.7
1594  * @param[in,out] array $not_sortable Array of field types that aren't sortable
1595  * @param string $field_type Field type to check whether the field is sortable
1596  * @param array $form Gravity Forms form
1597  */
1598  $not_sortable = apply_filters( 'gravityview/sortable/field_blacklist', $not_sortable, $field_type, $form );
1599 
1600  if ( in_array( $field_type, $not_sortable ) ) {
1601  return false;
1602  }
1603 
1604  return apply_filters( "gravityview/sortable/formfield_{$form['id']}_{$field_id}", apply_filters( "gravityview/sortable/field_{$field_id}", true, $form ) );
1605 
1606  }
1607 
1608 }
1609 
1611 
1612 
1613 
$url
Definition: post_image.php:25
$image
Definition: post_image.php:98
static _GET( $name, $default=null)
Grab a value from the _GET superglobal or default.
context_not_configured_warning( $view_id=0)
Display a warning when a View has not been configured.
single_entry_title( $passed_title, $passed_post_id=null)
Filter the title for the single entry view.
static getInstance( $passed_post=NULL)
static _override_sorting_id_by_field_type( $sort_field_id, $form_id)
Override sorting per field.
static getInstance( $passed_post=NULL)
Definition: class-data.php:122
setIsGravityviewPostType( $is_gravityview_post_type)
__construct()
Class constructor, enforce Singleton pattern.
set_context_view_id( $view_id=null)
Set the context view ID used when page contains multiple embedded views or displaying the single entr...
static process_search_only_approved( $args, $search_criteria)
Process the approved only search criteria according to the View settings.
gravityview_get_form( $form_id)
Returns the form object for a given Form ID.
$class
static generate_notice( $notice, $class='', $cap='', $object_id=null)
Display updated/error notice.
render_view( $passed_args)
Core function to render a View based on a set of arguments.
$entries
$search_method
static from_post(\WP_Post $post)
Get a list of objects inside the supplied .
gravityview()
Definition: _stubs.php:26
global $post
Definition: delete-entry.php:7
get( $key, $default=null)
Retrieve a setting.
static is_entry_approved( $entry, $args=array())
Check if a certain entry is approved.
gravityview_css_url( $css_file='', $dir_path='')
Functions that don&#39;t require GravityView or Gravity Forms API access but are used in the plugin to ex...
if(gravityview() ->plugin->is_GF_25()) $form
setSingleEntry( $single_entry)
Sets the single entry ID and also the entry.
GravityView_frontend_get_view_entries( $args, $form_id, $parameters, $count)
Definition: _mocks.php:69
$criteria['paging']
Modify the search parameters before the entries are fetched.
comments_open( $open, $post_id)
Disable comments on GravityView post types.
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
is_field_sortable( $field_id='', $form=array())
Checks if field (column) is sortable.
static get_search_criteria( $args, $form_id)
Parse search criteria for a entries search.
static check_entry_display( $entry, $view=null)
Checks if a certain entry is valid according to the View search filters (specially the Adv Filters) ...
static get_endpoint_name()
Return the endpoint name for a single Entry.
const APPROVED
static get_search_criteria_paging( $args)
Get the paging array for the View.
static is_field_numeric( $form=null, $field='')
Checks if the field type is a &#39;numeric&#39; field type (e.g.
setEntry( $entry)
Set the current entry.
static by_id( $post_id)
Construct a instance from a post ID.
enqueue_default_style( $css_dependencies=array())
Handle enqueuing the gravityview_default_style stylesheet.
gravityview_has_shortcode_r( $content, $tag='gravityview')
Placeholder until the recursive has_shortcode() patch is merged.
setPostHasShortcode( $post_has_shortcode)
insert_view_in_content( $content)
In case View post is called directly, insert the view in the post content.
setGvOutputData( $gv_output_data)
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:31
static is_single_entry()
Verify if user requested a single entry view.
$field_id
Definition: time.php:17
static process_search_dates( $args, $search_criteria=array())
Process the start and end dates for a view - overrides values defined in shortcode (if needed) ...
static replace_variables( $text, $form=array(), $entry=array(), $url_encode=false, $esc_html=true, $nl2br=true, $format='html', $aux_data=array())
Alias for GravityView_Merge_Tags::replace_variables()
Definition: class-api.php:118
if(empty( $created_by)) $form_id
is_searching()
Checks if the current View is presenting search results.
add_scripts_and_styles()
Register styles and scripts.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
parse_query_fix_frontpage(&$query)
Allow GravityView entry endpoints on the front page of a site.
gravityview_get_template_id( $post_id)
Get the template ID (list, table, datatables, map) for a View.
static is_approved( $status)
static add_style( $template_id)
Add template extra style if exists.
static get_entry( $entry_slug, $force_allow_ids=false, $check_entry_display=true, $view=null)
Return a single entry object.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
_get_single_entry_title( $view, $entry=array(), $passed_title='')
Returns the single entry title for a View with variables replaced and shortcodes parsed.
parse_content( $wp=array())
Read the $post and process the View data inside.
static get_form( $form_id)
Returns the form object for a given Form ID.
const meta_key
get_context_view_id()
Returns the the view_id context when page contains multiple embedded views or displaying single entry...
$field
Definition: gquiz_grade.php:11
add_columns_sort_links( $label='', $field, $form)
Inject the sorting links on the table columns.
set_entry_data()
Set the entry.
$title
static getInstance()
Get the one true instantiated self.