Completed
Push — develop ( d6e54c...488d60 )
by Zack
17:10
created

Renderer::legacy_template_warning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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/after', array( __CLASS__, 'maybe_print_notices' ) ) ) {
20
			add_action( 'gravityview/template/after', array( __CLASS__, 'maybe_print_notices' ) );
21
		}
22
	}
23
24
	/**
25
	 * Print unconfigured notices to admins.
26
	 *
27
	 * @param \GV\Template_Context $gravityview The $gravityview template object.
28
	 *
29
	 * @return void
30
	 */
31
	public static function maybe_print_notices( $gravityview = null ) {
32
		if ( ! $gravityview instanceof \GV\Template_Context ) {
33
			/** Call the legacy code. */
34
			\GravityView_frontend::getInstance()->context_not_configured_warning( gravityview_get_view_id() );
35
			return;
36
		}
37
38
		switch ( true ) {
39
			case ( $gravityview->request->is_edit_entry() ):
40
				$tab = __( 'Edit Entry', 'gravityview' );
41
				$context = 'edit';
42
				break;
43
			case ( $gravityview->request->is_entry() ):
44
				$tab = __( 'Single Entry', 'gravityview' );
45
				$context = 'single';
46
				break;
47
			default:
48
				$tab = __( 'Multiple Entries', 'gravityview' );
49
				$context = 'directory';
50
				break;
51
		}
52
53
		$cls = $gravityview->template;
54
		$slug = property_exists( $cls, '_configuration_slug' ) ? $cls::$_configuration_slug : $cls::$slug;
55
		if ( $gravityview->fields->by_position( sprintf( '%s_%s-*', $context, $slug ) )->by_visible()->count() ) {
56
			return;
57
		}
58
		
59
		$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 );
60
		$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...
61
		$action_text = sprintf( esc_html__( 'Add fields to %s', 'gravityview' ), $tab );
62
		$message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' );
63
64
		$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 ) ) );
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
65
		$output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message );
66
67
		echo \GVCommon::generate_notice( $output . $image, '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...
introduced by
Expected next thing to be a escaping function, not '\'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$image'
Loading history...
68
	}
69
70
	/**
71
	 * Warn about legacy template being used.
72
	 *
73
	 * Generate a callback that shows which legacy template was at fault.
74
	 * Used in gravityview_before.
75
	 *
76
	 * @param \GV\View $view The view we're looking at.
77
	 * @param string $path The path of the offending template.
78
	 *
79
	 * @return \Callable A closure used in the filter.
80
	 */
81
	public function legacy_template_warning( $view, $path ) {
82
		return function() use ( $view, $path ) {
83
			// Do not panic for now...
84
		};
85
	}
86
}
87