GravityView  2.17
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::disable_show_only_approved_entries( $gravityview );
41 
42  self::maybe_print_reserved_slugs_notice( $gravityview );
43 
44  self::maybe_print_configuration_notice( $gravityview );
45 
46  self::maybe_print_entry_approval_notice( $gravityview );
47  }
48 
49 
50  /**
51  * Disable the "Show only approved entries" setting, hence displaying all entries on the View
52  *
53  * @since 2.14.3
54  *
55  * @param \GV\Template_Context $gravityview The $gravityview template object.
56  *
57  * @return void
58  */
59  private static function disable_show_only_approved_entries( $gravityview ) {
60 
61  if ( ! isset( $_GET['disable_setting'] ) || ! wp_verify_nonce( $_GET['gv-setting'], 'setting' ) ) {
62  return;
63  }
64 
65  $settings = $gravityview->view->settings->all();
66 
67  $settings['show_only_approved'] = 0;
68 
69  $updated = update_post_meta( $gravityview->view->ID, '_gravityview_template_settings', $settings );
70 
71  if ( ! $updated ) {
72  gravityview()->log->error( 'Could not update View settings => Show only approved' );
73  return;
74  }
75 
76  $redirect_url = home_url( remove_query_arg( array( 'disable_setting', 'gv-setting' ) ) );
77 
78  if ( wp_safe_redirect( $redirect_url ) ) {
79  exit();
80  }
81  }
82 
83  /**
84  * Print notice warning admins that "Show only approved" is enabled
85  *
86  * @since 2.9.5
87  *
88  * @param \GV\Template_Context $gravityview The $gravityview template object.
89  *
90  * @return void
91  */
92  private static function maybe_print_entry_approval_notice( $gravityview ) {
93 
94  if ( $gravityview->entries && $gravityview->entries->count() ) {
95  return;
96  }
97 
98  if ( $gravityview->request->is_search() ) {
99  return;
100  }
101 
102  // "Show Only Approved" is not enabled.
103  if ( ! $gravityview->view->settings->get( 'show_only_approved', 0 ) ) {
104  return;
105  }
106 
107  // If "Show all entries to administrators" is enabled, approval status isn't the issue.
108  if ( $gravityview->view->settings->get( 'admin_show_all_statuses', 0 ) ) {
109  return;
110  }
111 
112  // Don't show when no entries are being displayed due to "Hide View data until search is performed".
113  if ( $gravityview->view->settings->get( 'hide_until_searched', 0 ) ) {
114  return;
115  }
116 
117  $current_user = wp_get_current_user();
118 
119  $user_meta_key = '_gv_dismissed_entry_approval_notice_' . $gravityview->view->ID;
120 
121  $dismiss_nonce_name = 'gv-dismiss';
122  $dismiss_nonce_action = 'gv-dismiss-no-entries-' . $gravityview->view->ID;
123 
124  if ( isset( $_GET[ $dismiss_nonce_name ] ) && wp_verify_nonce( $_GET[ $dismiss_nonce_name ], $dismiss_nonce_action ) ) {
125  add_user_meta( $current_user->ID, $user_meta_key, 1 ); // Prevent user from seeing this again for this View
126  return;
127  }
128 
129  // The user has already dismissed the notice
130  if ( get_user_meta( $current_user->ID, $user_meta_key, true ) ) {
131  return;
132  }
133 
134  // No form is attached to this View for some reason; there are no entries to display.
135  if ( empty( $gravityview->view->form ) ) {
136  return;
137  }
138 
139  $count = \GFAPI::count_entries( $gravityview->view->form->ID, array(
140  'status' => 'active',
141  'field_filters' => array(
142  array(
143  'key' => 'is_approved',
144  'operator' => 'isnot',
146  ),
147  ),
148  ) );
149 
150  // There aren't any entries to show!
151  if ( empty( $count ) ) {
152  return;
153  }
154 
155  $message_template = <<<EOD
156 <style>
157 #{dom_id}-hide-notice {
158  float: {float_dir};
159  font-size: 1rem;
160  font-weight: normal;
161 }
162 #{dom_id} div {
163  margin: 1em 0;
164 }
165 #{dom_id} hr {
166  border: none;
167  border-bottom: 1px solid #ddd;
168  margin: 0 0 10px;
169 }
170 #{dom_id} .gv-notice-message {
171  margin: 1em 0;
172  padding: 0;
173  font-size: 1.2rem;
174  font-weight: normal;
175 }
176 #{dom_id} span.gv-notice-description {
177  display: block;
178  font-weight: normal;
179  font-style: italic;
180 }
181 #{dom_id} .gv-notice-admin-message {
182  display: block;
183  text-align:center;
184  clear: both;
185 }
186 #{dom_id} img {
187  display: block;
188  margin: 10px 20px;
189  max-width: 550px;
190  float: {float_dir};
191 }
192 #{dom_id} .dashicons-no-alt {
193  font-size: 1.2em;
194  height: 1.2em;
195  width: 1em;
196 }
197 #{dom_id} .dashicons-external {
198  font-size: .8em;
199  height: .8em;
200  width: .8em;
201  line-height: .8em;
202 }
203 </style>
204 <div id="{dom_id}">
205  <h3>{notice_title}<span id="{dom_id}-hide-notice"><a href="{hide_notice_link}" role="button">{hide_notice} <span class="dashicons dashicons-no-alt"></span></span></a></h3>
206 
207  <p class="gv-notice-message">{screenshot} {message} <a href="{learn_more_link}" rel="external" target="_blank">{learn_more} <span class="dashicons dashicons-external" title="{title_new_window}"></span></a></p>
208 
209  <hr />
210 
211  <div><a href="{disable_setting_link}">{disable_setting}</a> <span class="gv-notice-description">{disable_setting_description}</span></div>
212  <div><a href="{approve_entries_link}">{approve_entries}</a> <span class="gv-notice-description">{approve_entries_description}</span></div>
213 
214  <p class="gv-notice-admin-message"><em>{admin_message}</em></p>
215 </div>
216 EOD;
217 
218  $notice_title = _n(
219  esc_html__( 'There is an unapproved entry that is not being shown.', 'gk-gravityview' ),
220  sprintf( esc_html__( 'There are %s unapproved entries that are not being shown.', 'gk-gravityview' ), number_format_i18n( $count ) ),
221  $count
222  );
223 
224  $float_dir = is_rtl() ? 'left' : 'right';
225 
226  $dismiss_notice_link = wp_nonce_url( add_query_arg( array() ), $dismiss_nonce_action, $dismiss_nonce_name );
227 
228  $disable_setting_link = wp_nonce_url( add_query_arg( array(
229  'disable_setting' => 'show_only_approved_' . $gravityview->view->ID
230  ) ), 'setting', 'gv-setting' );
231 
232  $placeholders = array(
233  '{dom_id}' => sprintf( 'gv-notice-approve-entries-%d', $gravityview->view->ID ),
234  '{float_dir}' => $float_dir,
235  '{notice_title}' => esc_html( $notice_title ),
236  '{title_new_window}' => esc_attr__( 'This link opens in a new window.', 'gk-gravityview' ),
237  '{hide_notice}' => esc_html__( 'Hide this notice', 'gk-gravityview' ),
238  '{hide_notice_link}' => esc_url( $dismiss_notice_link ),
239  '{message}' => esc_html( wptexturize( __( 'The "Show only approved entries" setting is enabled, so only entries that have been approved are displayed.', 'gk-gravityview' ) ) ),
240  '{learn_more}' => esc_html__( 'Learn about entry approval.', 'gk-gravityview' ),
241  '{learn_more_link}' => 'https://docs.gravityview.co/article/490-entry-approval-gravity-forms',
242  '{disable_setting}' => esc_html( wptexturize( __( 'Disable the "Show only approved entries" setting for this View', 'gk-gravityview' ) ) ),
243  '{disable_setting_description}' => esc_html( wptexturize( __( 'Click to immediately disable the "Show only approved entries" setting. All entry statuses will be shown.', 'gk-gravityview' ) ) ),
244  '{disable_setting_link}' => esc_url( $disable_setting_link ),
245  '{approve_entries}' => esc_html__( 'Manage entry approval', 'gk-gravityview' ),
246  '{approve_entries_description}' => esc_html__( 'Go to the Gravity Forms entries screen to moderate entry approval.', 'gk-gravityview' ),
247  '{approve_entries_link}' => esc_url( admin_url( 'admin.php?page=gf_entries&id=' . $gravityview->view->form->ID ) ),
248  '{screenshot}' => sprintf( '<img alt="%s" src="%s" />', esc_attr__( 'Show only approved entries', 'gk-gravityview' ), esc_url( plugins_url( 'assets/images/screenshots/entry-approval.png', GRAVITYVIEW_FILE ) ) ),
249  '{admin_message}' => sprintf( esc_html__( 'Note: %s', 'gk-gravityview' ), esc_html__( 'You can only see this message because you are able to edit this View.', 'gk-gravityview' ) ),
250  );
251 
252  $notice = strtr( $message_template, $placeholders );
253 
254  // Needed for the external link and close icons
255  wp_print_styles( 'dashicons' );
256 
257  echo \GVCommon::generate_notice( $notice, 'warning', 'edit_gravityview', $gravityview->view->ID );
258  }
259 
260  /**
261  * Check empty configuration.
262  *
263  * @since 2.10
264  *
265  * @param \GV\Template_Context $gravityview The $gravityview template object.
266  *
267  * @return void
268  */
269  private static function maybe_print_configuration_notice( $gravityview ) {
270 
271  switch ( true ) {
272  case ( $gravityview->request->is_edit_entry() ):
273  $tab = esc_html__( 'Edit Entry', 'gk-gravityview' );
274  $context = 'edit';
275  break;
276  case ( $gravityview->request->is_entry( $gravityview->view->form ? $gravityview->view->form->ID : 0 ) ):
277  $tab = esc_html__( 'Single Entry', 'gk-gravityview' );
278  $context = 'single';
279  break;
280  default:
281  $tab = esc_html__( 'Multiple Entries', 'gk-gravityview' );
282  $context = 'directory';
283  break;
284  }
285 
286  $cls = $gravityview->template;
287  $slug = property_exists( $cls, '_configuration_slug' ) ? $cls::$_configuration_slug : $cls::$slug;
288 
289  // If the zone has been configured, don't display notice.
290  if ( $gravityview->fields->by_position( sprintf( '%s_%s-*', $context, $slug ) )->by_visible( $gravityview->view )->count() ) {
291  return;
292  }
293 
294  $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 );
295  $edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $gravityview->view->ID, $context ) );
296  $action_text = sprintf( esc_html__( 'Add fields to %s', 'gk-gravityview' ), $tab );
297  $message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gk-gravityview' );
298 
299  $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 ) ) );
300  $output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message );
301 
302  echo \GVCommon::generate_notice( $output . $image, 'gv-warning warning', 'edit_gravityview', $gravityview->view->ID );
303  }
304 
305  /**
306  * Print reserved slug warnings, if they exist.
307  *
308  * @since 2.9.5
309  *
310  * @param Template_Context $gravityview The $gravityview template object.
311  *
312  * @return void
313  */
314  private static function maybe_print_reserved_slugs_notice( $gravityview ) {
315  global $wp;
316  global $wp_rewrite;
317 
318  $reserved_slugs = array(
319  $wp_rewrite->search_base,
320  apply_filters( 'gravityview_directory_endpoint', 'entry' ),
321  );
322 
323  $post_types = get_post_types();
324 
325  foreach( $post_types as $post_type ) {
326  $post_type_rewrite = get_post_type_object( $post_type )->rewrite;
327 
328  if ( $slug = \GV\Utils::get( $post_type_rewrite, 'slug' ) ) {
329  $reserved_slugs[] = $slug;
330  }
331  }
332 
333  unset( $post_types, $post_type_rewrite );
334 
335  /**
336  * @filter `gravityview/rewrite/reserved_slugs` Modify the reserved embed slugs that trigger a warning.
337  * @since 2.5
338  * @param array $reserved_slugs An array of strings, reserved slugs.
339  * @param \GV\Template_Context $gravityview The context.
340  */
341  $reserved_slugs = apply_filters( 'gravityview/rewrite/reserved_slugs', $reserved_slugs, $gravityview );
342 
343  $reserved_slugs = array_map( 'strtolower', $reserved_slugs );
344 
345  if ( ! in_array( strtolower( $wp->request ), $reserved_slugs, true ) ) {
346  return;
347  }
348 
349  gravityview()->log->error( '{slug} page URL is reserved.', array( 'slug' => $wp->request ) );
350 
351  $title = esc_html__( 'GravityView will not work correctly on this page because of the URL Slug.', 'gk-gravityview' );
352  $message = __( 'Please <a href="%s">read this article</a> for more information.', 'gk-gravityview' );
353  $message .= ' ' . esc_html__( 'You can only see this message because you are able to edit this View.', 'gk-gravityview' );
354 
355  $output = sprintf( '<h3>%s</h3><p>%s</p>', $title, sprintf( $message, 'https://docs.gravityview.co/article/659-reserved-urls' ) );
356 
357  echo \GVCommon::generate_notice( $output, 'gv-error error', 'edit_gravityview', $gravityview->view->ID );
358  }
359 
360  /**
361  * Warn about legacy template being used.
362  *
363  * Generate a callback that shows which legacy template was at fault.
364  * Used in gravityview_before.
365  *
366  * @param \GV\View $view The view we're looking at.
367  * @param string $path The path of the offending template.
368  *
369  * @return \Callable A closure used in the filter.
370  */
371  public function legacy_template_warning( $view, $path ) {
372  return function() use ( $view, $path ) {
373  // Do not panic for now...
374  };
375  }
376 }
gravityview_get_view_id()
Get the current View ID being rendered.
Definition: class-api.php:1308
$image
Definition: post_image.php:98
static maybe_print_reserved_slugs_notice( $gravityview)
Print reserved slug warnings, if they exist.
static disable_show_only_approved_entries( $gravityview)
Disable the "Show only approved entries" setting, hence displaying all entries on the View...
legacy_template_warning( $view, $path)
Warn about legacy template being used.
If this file is called directly, abort.
If this file is called directly, abort.
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:40
static maybe_print_configuration_notice( $gravityview)
Check empty configuration.
gravityview()
The main GravityView wrapper function.
$title
static getInstance()
Get the one true instantiated self.