GravityView  2.10.1
The best, easiest way to display Gravity Forms entries on your website.
class-gv-renderer.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The \GV\Renderer class.
11  *
12  * The base for all renderers.
13  */
14 class Renderer {
15  /**
16  * Initialization.
17  */
18  public function __construct() {
19  if ( ! has_action( 'gravityview/template/before', array( __CLASS__, 'maybe_print_notices' ) ) ) {
20  add_action( 'gravityview/template/before', array( __CLASS__, 'maybe_print_notices' ) );
21  }
22  }
23 
24  /**
25  * Print unconfigured notices to admins.
26  * Print reserved slug warnings.
27  * Print entry approval notice.
28  *
29  * @param \GV\Template_Context $gravityview The $gravityview template object.
30  *
31  * @return void
32  */
33  public static function maybe_print_notices( $gravityview = null ) {
34  if ( ! $gravityview instanceof \GV\Template_Context ) {
35  /** Call the legacy code. */
36  \GravityView_frontend::getInstance()->context_not_configured_warning( gravityview_get_view_id() );
37  return;
38  }
39 
40  self::maybe_print_reserved_slugs_notice( $gravityview );
41 
42  self::maybe_print_configuration_notice( $gravityview );
43 
44  self::maybe_print_entry_approval_notice( $gravityview );
45  }
46 
47  /**
48  * Print notice warning admins that "Show only approved" is enabled
49  *
50  * @since 2.9.5
51  *
52  * @param \GV\Template_Context $gravityview The $gravityview template object.
53  *
54  * @return void
55  */
56  private static function maybe_print_entry_approval_notice( $gravityview ) {
57 
58  if ( $gravityview->entries && $gravityview->entries->count() ) {
59  return;
60  }
61 
62  if ( $gravityview->request->is_search() ) {
63  return;
64  }
65 
66  // "Show Only Approved" is not enabled.
67  if ( ! $gravityview->view->settings->get( 'show_only_approved', 0 ) ) {
68  return;
69  }
70 
71  // If "Show all entries to administrators" is enabled, approval status isn't the issue.
72  if ( $gravityview->view->settings->get( 'admin_show_all_statuses', 0 ) ) {
73  return;
74  }
75 
76  // Don't show when no entries are being displayed due to "Hide View data until search is performed".
77  if ( $gravityview->view->settings->get( 'hide_until_searched', 0 ) ) {
78  return;
79  }
80 
81  $current_user = wp_get_current_user();
82  $user_meta_key = '_gv_dismissed_entry_approval_notice' . $gravityview->view->ID;
83 
84  if ( isset( $_GET['gv-dismiss'] ) && wp_verify_nonce( $_GET['gv-dismiss'], 'dismiss' ) ) {
85  add_user_meta( $current_user->ID, $user_meta_key, 1 ); // Prevent user from seeing this again for this View
86  return;
87  }
88 
89  // The user has already dismissed the notice
90  if ( get_user_meta( $current_user->ID, $user_meta_key, true ) ) {
91  return;
92  }
93 
94  $form = $gravityview->view->form;
95 
96  if ( ! $form ) {
97  return;
98  }
99 
100  $count = \GFAPI::count_entries( $gravityview->view->form->ID, array(
101  'status' => 'active',
102  'field_filters' => array(
103  array(
104  'key' => 'is_approved',
105  'operator' => 'isnot',
107  ),
108  ),
109  ) );
110 
111  // There aren't any entries to show!
112  if ( empty( $count ) ) {
113  return;
114  }
115 
116  $notice_title = _n(
117  esc_html__( 'There is an unapproved entry that is not being shown.', 'gravityview' ),
118  esc_html__( 'There are %s unapproved entries that are not being shown.', 'gravityview' ),
119  $count
120  );
121 
122  $float_dir = is_rtl() ? 'left' : 'right';
123  $hide_link = sprintf( '<a href="%s" style="float: ' . $float_dir . '; font-size: 1rem" role="button">%s</a>', esc_url( wp_nonce_url( add_query_arg( array( 'notice' => 'no_entries_' . $gravityview->view->ID ) ), 'dismiss', 'gv-dismiss' ) ), esc_html__( 'Hide this notice', 'gravityview' ) );
124 
125  $message_strings = array(
126  '<h3>' . sprintf( $notice_title, number_format_i18n( $count ) ) . $hide_link . '</h3>',
127  esc_html__( 'The "Show only approved entries" setting is enabled, so only entries that have been approved are displayed.', 'gravityview' ),
128  sprintf( '<a href="%s">%s</a>', 'https://docs.gravityview.co/article/490-entry-approval-gravity-forms', esc_html__( 'Learn about entry approval.', 'gravityview' ) ),
129  "\n\n",
130  sprintf( esc_html_x( '%sEdit the View settings%s or %sApprove entries%s', 'Replacements are HTML links', 'gravityview' ), '<a href="' . esc_url( get_edit_post_link( $gravityview->view->ID, false ) ) . '" style="font-weight: bold;">', '</a>', '<a href="' . esc_url( admin_url( 'admin.php?page=gf_entries&id=' . $gravityview->view->form->ID ) ) . '" style="font-weight: bold;">', '</a>' ),
131  "\n\n",
132  sprintf( '<img alt="%s" src="%s" style="padding: 10px 0; max-width: 550px;" />', esc_html__( 'Show only approved entries', 'gravityview' ), esc_url( plugins_url( 'assets/images/screenshots/entry-approval.png', GRAVITYVIEW_FILE ) ) ),
133  "\n\n",
134  esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' ),
135  );
136 
137  $notice = wpautop( implode( ' ', $message_strings ) );
138 
139  echo \GVCommon::generate_notice( $notice, 'warning', 'edit_gravityview', $gravityview->view->ID );
140  }
141 
142  /**
143  * Check empty configuration.
144  *
145  * @since 2.10
146  *
147  * @param \GV\Template_Context $gravityview The $gravityview template object.
148  *
149  * @return void
150  */
151  private static function maybe_print_configuration_notice( $gravityview ) {
152 
153  switch ( true ) {
154  case ( $gravityview->request->is_edit_entry() ):
155  $tab = __( 'Edit Entry', 'gravityview' );
156  $context = 'edit';
157  break;
158  case ( $gravityview->request->is_entry( $gravityview->view->form ? $gravityview->view->form->ID : 0 ) ):
159  $tab = __( 'Single Entry', 'gravityview' );
160  $context = 'single';
161  break;
162  default:
163  $tab = __( 'Multiple Entries', 'gravityview' );
164  $context = 'directory';
165  break;
166  }
167 
168  $cls = $gravityview->template;
169  $slug = property_exists( $cls, '_configuration_slug' ) ? $cls::$_configuration_slug : $cls::$slug;
170 
171  // If the zone has been configured, don't display notice.
172  if ( $gravityview->fields->by_position( sprintf( '%s_%s-*', $context, $slug ) )->by_visible( $gravityview->view )->count() ) {
173  return;
174  }
175 
176  $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 );
177  $edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $gravityview->view->ID, $context ) );
178  $action_text = sprintf( esc_html__( 'Add fields to %s', 'gravityview' ), $tab );
179  $message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
180 
181  $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 ) ) );
182  $output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message );
183 
184  echo \GVCommon::generate_notice( $output . $image, 'gv-warning warning', 'edit_gravityview', $gravityview->view->ID );
185  }
186 
187  /**
188  * Print reserved slug warnings, if they exist.
189  *
190  * @since 2.9.5
191  *
192  * @param Template_Context $gravityview The $gravityview template object.
193  *
194  * @return void
195  */
196  private static function maybe_print_reserved_slugs_notice( $gravityview ) {
197  global $wp;
198  global $wp_rewrite;
199 
200  $reserved_slugs = array(
201  $wp_rewrite->search_base,
202  apply_filters( 'gravityview_directory_endpoint', 'entry' ),
203  );
204 
205  $post_types = get_post_types();
206 
207  foreach( $post_types as $post_type ) {
208  $post_type_rewrite = get_post_type_object( $post_type )->rewrite;
209 
210  if ( $slug = \GV\Utils::get( $post_type_rewrite, 'slug' ) ) {
211  $reserved_slugs[] = $slug;
212  }
213  }
214 
215  unset( $post_types, $post_type_rewrite );
216 
217  /**
218  * @filter `gravityview/rewrite/reserved_slugs` Modify the reserved embed slugs that trigger a warning.
219  * @since 2.5
220  * @param[in,out] array $reserved_slugs An array of strings, reserved slugs.
221  * @param \GV\Template_Context $gravityview The context.
222  */
223  $reserved_slugs = apply_filters( 'gravityview/rewrite/reserved_slugs', $reserved_slugs, $gravityview );
224 
225  $reserved_slugs = array_map( 'strtolower', $reserved_slugs );
226 
227  if ( ! in_array( strtolower( $wp->request ), $reserved_slugs, true ) ) {
228  return;
229  }
230 
231  gravityview()->log->error( '{slug} page URL is reserved.', array( 'slug' => $wp->request ) );
232 
233  $title = esc_html__( 'GravityView will not work correctly on this page because of the URL Slug.', 'gravityview' );
234  $message = __( 'Please <a href="%s">read this article</a> for more information.', 'gravityview' );
235  $message .= ' ' . esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
236 
237  $output = sprintf( '<h3>%s</h3><p>%s</p>', $title, sprintf( $message, 'https://docs.gravityview.co/article/659-reserved-urls' ) );
238 
239  echo \GVCommon::generate_notice( $output, 'gv-error error', 'edit_gravityview', $gravityview->view->ID );
240  }
241 
242  /**
243  * Warn about legacy template being used.
244  *
245  * Generate a callback that shows which legacy template was at fault.
246  * Used in gravityview_before.
247  *
248  * @param \GV\View $view The view we're looking at.
249  * @param string $path The path of the offending template.
250  *
251  * @return \Callable A closure used in the filter.
252  */
253  public function legacy_template_warning( $view, $path ) {
254  return function() use ( $view, $path ) {
255  // Do not panic for now...
256  };
257  }
258 }
gravityview_get_view_id()
Get the current View ID being rendered.
Definition: class-api.php:1296
$image
Definition: post_image.php:98
static maybe_print_reserved_slugs_notice( $gravityview)
Print reserved slug warnings, if they exist.
legacy_template_warning( $view, $path)
Warn about legacy template being used.
If this file is called directly, abort.
gravityview()
Definition: _stubs.php:26
If this file is called directly, abort.
get( $key, $default=null)
Retrieve a setting.
if(gravityview() ->plugin->is_GF_25()) $form
static maybe_print_notices( $gravityview=null)
Print unconfigured notices to admins.
const APPROVED
static maybe_print_entry_approval_notice( $gravityview)
Print notice warning admins that "Show only approved" is enabled.
__construct()
Initialization.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:31
static maybe_print_configuration_notice( $gravityview)
Check empty configuration.
$title
static getInstance()
Get the one true instantiated self.