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