GravityView  2.9.2
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  *
28  * @param \GV\Template_Context $gravityview The $gravityview template object.
29  *
30  * @return void
31  */
32  public static function maybe_print_notices( $gravityview = null ) {
33  if ( ! $gravityview instanceof \GV\Template_Context ) {
34  /** Call the legacy code. */
35  \GravityView_frontend::getInstance()->context_not_configured_warning( gravityview_get_view_id() );
36  return;
37  }
38 
39  /**
40  * Check reserved slugs.
41  */
42  global $wp;
43  global $wp_rewrite;
44 
45  $reserved_slugs = array(
46  $wp_rewrite->search_base,
47  apply_filters( 'gravityview_directory_endpoint', 'entry' ),
48  );
49 
50  $post_types = get_post_types();
51 
52  foreach( $post_types as $post_type ) {
53  $post_type_rewrite = get_post_type_object( $post_type )->rewrite;
54 
55  if ( $slug = \GV\Utils::get( $post_type_rewrite, 'slug' ) ) {
56  $reserved_slugs[] = $slug;
57  }
58  }
59 
60  unset( $post_types, $post_type_rewrite );
61 
62  /**
63  * @filter `gravityview/rewrite/reserved_slugs` Modify the reserved embed slugs that trigger a warning.
64  * @since 2.5
65  * @param[in,out] array $reserved_slugs An array of strings, reserved slugs.
66  * @param \GV\Template_Context $gravityview The context.
67  */
68  $reserved_slugs = apply_filters( 'gravityview/rewrite/reserved_slugs', $reserved_slugs, $gravityview );
69 
70  $reserved_slugs = array_map( 'strtolower', $reserved_slugs );
71 
72  if ( in_array( strtolower( $wp->request ), $reserved_slugs, true ) ) {
73  gravityview()->log->error( '{slug} page URL is reserved.', array( 'slug' => $wp->request ) );
74 
75  $title = esc_html__( 'GravityView will not work correctly on this page because of the URL Slug.', 'gravityview' );
76  $message = __( 'Please <a href="%s">read this article</a> for more information.', 'gravityview' );
77  $message .= ' ' . esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
78 
79  $output = sprintf( '<h3>%s</h3><p>%s</p>', $title, sprintf( $message, 'https://docs.gravityview.co/article/659-reserved-urls' ) );
80 
81  echo \GVCommon::generate_notice( $output, 'gv-error error', 'edit_gravityview', $gravityview->view->ID );
82  }
83 
84  /**
85  * Check empty configuration.
86  */
87  switch ( true ) {
88  case ( $gravityview->request->is_edit_entry() ):
89  $tab = __( 'Edit Entry', 'gravityview' );
90  $context = 'edit';
91  break;
92  case ( $gravityview->request->is_entry( $gravityview->view->form ? $gravityview->view->form->ID : 0 ) ):
93  $tab = __( 'Single Entry', 'gravityview' );
94  $context = 'single';
95  break;
96  default:
97  $tab = __( 'Multiple Entries', 'gravityview' );
98  $context = 'directory';
99  break;
100  }
101 
102  $cls = $gravityview->template;
103  $slug = property_exists( $cls, '_configuration_slug' ) ? $cls::$_configuration_slug : $cls::$slug;
104  if ( $gravityview->fields->by_position( sprintf( '%s_%s-*', $context, $slug ) )->by_visible( $gravityview->view )->count() ) {
105  return;
106  }
107 
108  $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 );
109  $edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $gravityview->view->ID, $context ) );
110  $action_text = sprintf( esc_html__( 'Add fields to %s', 'gravityview' ), $tab );
111  $message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
112 
113  $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 ) ) );
114  $output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message );
115 
116  echo \GVCommon::generate_notice( $output . $image, 'gv-error error', 'edit_gravityview', $gravityview->view->ID );
117  }
118 
119  /**
120  * Warn about legacy template being used.
121  *
122  * Generate a callback that shows which legacy template was at fault.
123  * Used in gravityview_before.
124  *
125  * @param \GV\View $view The view we're looking at.
126  * @param string $path The path of the offending template.
127  *
128  * @return \Callable A closure used in the filter.
129  */
130  public function legacy_template_warning( $view, $path ) {
131  return function() use ( $view, $path ) {
132  // Do not panic for now...
133  };
134  }
135 }
gravityview_get_view_id()
Get the current View ID being rendered.
Definition: class-api.php:1228
$image
Definition: post_image.php:98
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.
static maybe_print_notices( $gravityview=null)
Print unconfigured notices to admins.
__construct()
Initialization.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:31
$title
static getInstance()
Get the one true instantiated self.