Completed
Push — develop ( 6d1660...ace222 )
by Zack
16:17
created

Renderer::maybe_print_notices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 12
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 9.8666
c 0
b 0
f 0
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 102
	public function __construct() {
19 102
		if ( ! has_action( 'gravityview/template/before', array( __CLASS__, 'maybe_print_notices' ) ) ) {
20 31
			add_action( 'gravityview/template/before', array( __CLASS__, 'maybe_print_notices' ) );
21
		}
22 102
	}
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 35
	public static function maybe_print_notices( $gravityview = null ) {
33 35
		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
		self::maybe_print_reserved_slugs_notice( $gravityview );
40
41
		self::maybe_print_configuration_notice( $gravityview );
42 35
43 35
	}
44
45
	/**
46 35
	 * Check empty configuration.
47 35
	 *
48
	 * @since 2.9.5
49
	 *
50 35
	 * @param \GV\Template_Context $gravityview The $gravityview template object.
51
	 *
52 35
	 * @return void
53 35
	 */
54
	private static function maybe_print_configuration_notice( $gravityview ) {
55 35
56 35
		switch ( true ) {
57
			case ( $gravityview->request->is_edit_entry() ):
58
				$tab = __( 'Edit Entry', 'gravityview' );
59
				$context = 'edit';
60 35
				break;
61
			case ( $gravityview->request->is_entry( $gravityview->view->form ? $gravityview->view->form->ID : 0 ) ):
62
				$tab = __( 'Single Entry', 'gravityview' );
63
				$context = 'single';
64
				break;
65
			default:
66
				$tab = __( 'Multiple Entries', 'gravityview' );
67
				$context = 'directory';
68 35
				break;
69
		}
70 35
71
		$cls = $gravityview->template;
72 35
		$slug = property_exists( $cls, '_configuration_slug' ) ? $cls::$_configuration_slug : $cls::$slug;
73 1
74
		// If the zone has been configured, don't display notice.
75 1
		if ( $gravityview->fields->by_position( sprintf( '%s_%s-*', $context, $slug ) )->by_visible( $gravityview->view )->count() ) {
76 1
			return;
77 1
		}
78
79 1
		$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 );
80
		$edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $gravityview->view->ID, $context ) );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81 1
		$action_text = sprintf( esc_html__( 'Add fields to %s', 'gravityview' ), $tab );
82
		$message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
83
84
		$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 ) ) );
85
		$output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message );
86
87
		echo \GVCommon::generate_notice( $output . $image, 'gv-warning warning', 'edit_gravityview', $gravityview->view->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88 35
	}
89
90
	/**
91
	 * Print reserved slug warnings, if they exist.
92 35
	 *
93 10
	 * @since 2.9.5
94 10
	 *
95 10
	 * @param Template_Context $gravityview The $gravityview template object.
96
	 *
97 27
	 * @return void
98 27
	 */
99 27
	private static function maybe_print_reserved_slugs_notice( $gravityview ) {
100
		global $wp;
101
		global $wp_rewrite;
102 35
103 35
		$reserved_slugs = array(
104 35
			$wp_rewrite->search_base,
105 28
			apply_filters( 'gravityview_directory_endpoint', 'entry' ),
106
		);
107
108 7
		$post_types = get_post_types();
109 7
110 7
		foreach( $post_types as $post_type ) {
111 7
			$post_type_rewrite = get_post_type_object( $post_type )->rewrite;
112
113 7
			if ( $slug = \GV\Utils::get( $post_type_rewrite, 'slug' ) ) {
114 7
				$reserved_slugs[] = $slug;
115
			}
116 7
		}
117 7
118
		unset( $post_types, $post_type_rewrite );
119
120
		/**
121
		 * @filter `gravityview/rewrite/reserved_slugs` Modify the reserved embed slugs that trigger a warning.
122
		 * @since 2.5
123
		 * @param[in,out] array $reserved_slugs An array of strings, reserved slugs.
124
		 * @param \GV\Template_Context $gravityview The context.
125
		 */
126
		$reserved_slugs = apply_filters( 'gravityview/rewrite/reserved_slugs', $reserved_slugs, $gravityview );
127
128
		$reserved_slugs = array_map( 'strtolower', $reserved_slugs );
129
130
		if ( ! in_array( strtolower( $wp->request ), $reserved_slugs, true ) ) {
131
			return;
132
		}
133
134
		gravityview()->log->error( '{slug} page URL is reserved.', array( 'slug' => $wp->request ) );
135
136
		$title   = esc_html__( 'GravityView will not work correctly on this page because of the URL Slug.', 'gravityview' );
137
		$message = __( 'Please <a href="%s">read this article</a> for more information.', 'gravityview' );
138
		$message .= ' ' . esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
139
140
		$output = sprintf( '<h3>%s</h3><p>%s</p>', $title, sprintf( $message, 'https://docs.gravityview.co/article/659-reserved-urls' ) );
141
142
		echo \GVCommon::generate_notice( $output, 'gv-error error', 'edit_gravityview', $gravityview->view->ID );
0 ignored issues
show
Documentation introduced by
The property ID does not exist on object<GV\View>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
143
	}
144
145
	/**
146
	 * Warn about legacy template being used.
147
	 *
148
	 * Generate a callback that shows which legacy template was at fault.
149
	 * Used in gravityview_before.
150
	 *
151
	 * @param \GV\View $view The view we're looking at.
152
	 * @param string $path The path of the offending template.
153
	 *
154
	 * @return \Callable A closure used in the filter.
155
	 */
156
	public function legacy_template_warning( $view, $path ) {
157
		return function() use ( $view, $path ) {
158
			// Do not panic for now...
159
		};
160
	}
161
}
162