Completed
Pull Request — develop (#1555)
by Zack
23:16 queued 03:19
created

Renderer::maybe_print_configuration_notice()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 12
nop 1
dl 0
loc 35
ccs 0
cts 0
cp 0
crap 42
rs 8.7377
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
	 * Print entry approval notice.
28
	 *
29
	 * @param \GV\Template_Context $gravityview The $gravityview template object.
30
	 *
31
	 * @return void
32 35
	 */
33 35
	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 35
		self::maybe_print_configuration_notice( $gravityview );
43 35
44
		self::maybe_print_entry_approval_notice( $gravityview );
45
	}
46 35
47 35
	/**
48
	 * Print notice warning admins that "Show only approved" is enabled
49
	 *
50 35
	 * @since 2.9.5
51
	 *
52 35
	 * @param \GV\Template_Context $gravityview The $gravityview template object.
53 35
	 *
54
	 * @return void
55 35
	 */
56 35
	private static function maybe_print_entry_approval_notice( $gravityview ) {
57
58
		if ( $gravityview->entries->count() ) {
59
			return;
60 35
		}
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', false ) ) {
68 35
			return;
69
		}
70 35
71
		$current_user  = wp_get_current_user();
72 35
		$user_meta_key = '_gv_dismissed_entry_approval_notice' . $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...
73 1
74
		if ( isset( $_GET['gv-dismiss'] ) && wp_verify_nonce( $_GET['gv-dismiss'], 'dismiss' ) ) {
75 1
			add_user_meta( $current_user->ID, $user_meta_key, 1 ); // Prevent user from seeing this again for this View
76 1
77 1
			return;
78
		}
79 1
80
		// The user has already dismissed the notice
81 1
		if ( get_user_meta( $current_user->ID, $user_meta_key, true ) ) {
82
			return;
83
		}
84
85
		$form = $gravityview->view->form;
86
87
		if ( ! $form ) {
88 35
			return;
89
		}
90
91
		$count = \GFAPI::count_entries( $gravityview->view->form->ID, array(
92 35
			'status'        => 'active',
93 10
			'field_filters' => array(
94 10
				array(
95 10
					'key'      => 'is_approved',
96
					'operator' => 'isnot',
97 27
					'value'    => \GravityView_Entry_Approval_Status::APPROVED,
98 27
				),
99 27
			),
100
		) );
101
102 35
		$notice_title = _n(
103 35
			esc_html__( 'There is an unapproved entry that is not being shown.', 'gravityview' ),
104 35
			esc_html__( 'There are %s unapproved entries that are not being shown.', 'gravityview' ),
105 28
			$count
106
		);
107
108 7
		$float_dir = is_rtl() ? 'left' : 'right';
109 7
		$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' ) );
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...
110 7
111 7
		$message_strings = array(
112
			'<h3>' . sprintf( $notice_title, number_format_i18n( $count ) ) . $hide_link . '</h3>',
113 7
			esc_html__( 'The "Show only approved entries" setting is enabled, so only entries that have been approved are displayed.', 'gravityview' ),
114 7
			sprintf( '<a href="%s">%s</a>', 'https://docs.gravityview.co/article/490-entry-approval-gravity-forms', esc_html__( 'Learn about entry approval.', 'gravityview' ) ),
115
			"\n\n",
116 7
			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>' ),
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...
117 7
			"\n\n",
118
			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 ) ) ),
119
			"\n\n",
120
			esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' ),
121
		);
122
123
		$notice = wpautop( implode( ' ', $message_strings ) );
124
125
		echo \GVCommon::generate_notice( $notice, '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...
126
	}
127
128
	/**
129
	 * Check empty configuration.
130
	 *
131
	 * @since 2.9.5
132
	 *
133
	 * @param \GV\Template_Context $gravityview The $gravityview template object.
134
	 *
135
	 * @return void
136
	 */
137
	private static function maybe_print_configuration_notice( $gravityview ) {
138
139
		switch ( true ) {
140
			case ( $gravityview->request->is_edit_entry() ):
141
				$tab = __( 'Edit Entry', 'gravityview' );
142
				$context = 'edit';
143
				break;
144
			case ( $gravityview->request->is_entry( $gravityview->view->form ? $gravityview->view->form->ID : 0 ) ):
145
				$tab = __( 'Single Entry', 'gravityview' );
146
				$context = 'single';
147
				break;
148
			default:
149
				$tab = __( 'Multiple Entries', 'gravityview' );
150
				$context = 'directory';
151
				break;
152
		}
153
154
		$cls = $gravityview->template;
155
		$slug = property_exists( $cls, '_configuration_slug' ) ? $cls::$_configuration_slug : $cls::$slug;
156
157
		// If the zone has been configured, don't display notice.
158
		if ( $gravityview->fields->by_position( sprintf( '%s_%s-*', $context, $slug ) )->by_visible( $gravityview->view )->count() ) {
159
			return;
160
		}
161
162
		$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 );
163
		$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...
164
		$action_text = sprintf( esc_html__( 'Add fields to %s', 'gravityview' ), $tab );
165
		$message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
166
167
		$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 ) ) );
168
		$output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message );
169
170
		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...
171
	}
172
173
	/**
174
	 * Print reserved slug warnings, if they exist.
175
	 *
176
	 * @since 2.9.5
177
	 *
178
	 * @param Template_Context $gravityview The $gravityview template object.
179
	 *
180
	 * @return void
181
	 */
182
	private static function maybe_print_reserved_slugs_notice( $gravityview ) {
183
		global $wp;
184
		global $wp_rewrite;
185
186
		$reserved_slugs = array(
187
			$wp_rewrite->search_base,
188
			apply_filters( 'gravityview_directory_endpoint', 'entry' ),
189
		);
190
191
		$post_types = get_post_types();
192
193
		foreach( $post_types as $post_type ) {
194
			$post_type_rewrite = get_post_type_object( $post_type )->rewrite;
195
196
			if ( $slug = \GV\Utils::get( $post_type_rewrite, 'slug' ) ) {
197
				$reserved_slugs[] = $slug;
198
			}
199
		}
200
201
		unset( $post_types, $post_type_rewrite );
202
203
		/**
204
		 * @filter `gravityview/rewrite/reserved_slugs` Modify the reserved embed slugs that trigger a warning.
205
		 * @since 2.5
206
		 * @param[in,out] array $reserved_slugs An array of strings, reserved slugs.
207
		 * @param \GV\Template_Context $gravityview The context.
208
		 */
209
		$reserved_slugs = apply_filters( 'gravityview/rewrite/reserved_slugs', $reserved_slugs, $gravityview );
210
211
		$reserved_slugs = array_map( 'strtolower', $reserved_slugs );
212
213
		if ( ! in_array( strtolower( $wp->request ), $reserved_slugs, true ) ) {
214
			return;
215
		}
216
217
		gravityview()->log->error( '{slug} page URL is reserved.', array( 'slug' => $wp->request ) );
218
219
		$title   = esc_html__( 'GravityView will not work correctly on this page because of the URL Slug.', 'gravityview' );
220
		$message = __( 'Please <a href="%s">read this article</a> for more information.', 'gravityview' );
221
		$message .= ' ' . esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
222
223
		$output = sprintf( '<h3>%s</h3><p>%s</p>', $title, sprintf( $message, 'https://docs.gravityview.co/article/659-reserved-urls' ) );
224
225
		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...
226
	}
227
228
	/**
229
	 * Warn about legacy template being used.
230
	 *
231
	 * Generate a callback that shows which legacy template was at fault.
232
	 * Used in gravityview_before.
233
	 *
234
	 * @param \GV\View $view The view we're looking at.
235
	 * @param string $path The path of the offending template.
236
	 *
237
	 * @return \Callable A closure used in the filter.
238
	 */
239
	public function legacy_template_warning( $view, $path ) {
240
		return function() use ( $view, $path ) {
241
			// Do not panic for now...
242
		};
243
	}
244
}
245