Completed
Pull Request — develop (#1555)
by Zack
34:38 queued 14:33
created

Renderer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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