Completed
Push — develop ( 744f01...bc21cb )
by Zack
17:24
created

Entry_Template::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 9.9332
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 23 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
 * Load up the Gamajo Template Loader.
11
 *
12
 * @see https://github.com/GaryJones/Gamajo-Template-Loader
13
 */
14
if ( ! class_exists( '\GV\Gamajo_Template_Loader' ) ) {
15
	require gravityview()->plugin->dir( 'future/lib/class-gamajo-template-loader.php' );
16
}
17
18
/**
19
 * The Entry Template class .
20
 *
21
 * Renders a \GV\Entry using a \GV\Entry_Renderer.
22
 */
23
abstract class Entry_Template extends Template {
24
	/**
25
	 * Prefix for filter names.
26
	 * @var string
27
	 */
28
	protected $filter_prefix = 'gravityview/template/entries';
29
30
	/**
31
	 * Directory name where custom templates for this plugin should be found in the theme.
32
	 * @var string
33
	 */
34
	protected $theme_template_directory = 'gravityview/entries/';
35
36
	/**
37
	 * Directory name where the default templates for this plugin are found.
38
	 * @var string
39
	 */
40
	protected $plugin_template_directory = 'templates/entries/';
41
42
	/**
43
	 * @var \GV\Entry The entry that need to be rendered.
44
	 */
45
	public $entry;
46
47
	/**
48
	 * @var \GV\View The view context.
49
	 */
50
	public $view;
51
52
	/**
53
	 * @var \GV\Request The request context.
54
	 */
55
	public $request;
56
57
	/**
58
	 * @var string The template slug to be loaded (like "table", "list")
59
	 */
60
	public static $slug;
61
62
	/**
63
	 * Initializer.
64
	 *
65
	 * @param \GV\View $view The View connected to this template.
66
	 * @param \GV\Entry_Collection $entries A collection of entries for this view.
0 ignored issues
show
Bug introduced by
There is no parameter named $entries. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
67
	 * @param \GV\Request $request The request context.
68
	 */
69 12
	public function __construct( Entry $entry, View $view, Request $request ) {
70 12
		$this->entry = $entry;
71 12
		$this->view = $view;
72 12
		$this->request = $request;
73
74
		/** Add granular overrides. */
75 12
		add_filter( $this->filter_prefix . '_get_template_part', array( $this, 'add_id_specific_templates' ), 10, 3 );
76
77 12
		parent::__construct();
78 12
	}
79
80 2
	public function __destruct() {
81 2
		remove_filter( $this->filter_prefix . '_get_template_part', array( $this, 'add_id_specific_templates' ) );
82 2
	}
83
84
	/**
85
	 * Enable granular template overrides based on current post, view, form, etc.
86
	 *
87
	 * The loading order is:
88
	 *
89
	 * - post-[ID of post or page where view is embedded]-view-[View ID]-entry-[Entry ID]-table-footer.php
90
	 * - post-[ID of post or page where view is embedded]-entry-[Entry ID]-table-footer.php
91
	 * - post-[ID of post or page where view is embedded]-view-[View ID]-table-footer.php
92
	 * - post-[ID of post or page where view is embedded]-table-footer.php
93
	 * - view-[View ID]-entry-[Entry ID]-table-footer.php
94
	 * - form-[Form ID]-entry-[Entry ID]-table-footer.php
95
	 * - view-[View ID]-table-footer.php
96
	 * - form-[Form ID]-table-footer.php
97
	 * - entry-[Entry ID]-table-footer.php
98
	 * - table-footer.php
99
	 *
100
	 * @see  Gamajo_Template_Loader::get_template_file_names() Where the filter is
101
	 * @param array $templates Existing list of templates.
102
	 * @param string $slug      Name of the template base, example: `table`, `list`, `datatables`, `map`
103
	 * @param string $name      Name of the template part, example: `body`, `footer`, `head`, `single`
104
	 *
105
	 * @return array $templates Modified template array, merged with existing $templates values
106
	 */
107 12
	public function add_id_specific_templates( $templates, $slug, $name ) {
108
109 12
		$specifics = array();
110
111 12
		list( $slug_dir, $slug_name ) = self::split_slug( $slug, $name );
112
113 12
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
114
115 12
		if ( ! $this->request->is_view() && $post ) {
116 1
			$specifics []= sprintf( '%spost-%d-view-%d-entry-%d-%s.php', $slug_dir, $post->ID, $this->view->ID, $this->entry->ID, $slug_name );
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 1 space before "="; 0 found
Loading history...
117 1
			$specifics []= sprintf( '%spost-%d-entry-%d-%s.php', $slug_dir, $post->ID, $this->entry->ID, $slug_name );
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
118 1
			$specifics []= sprintf( '%spost-%d-view-%d-%s.php', $slug_dir, $post->ID, $this->view->ID, $slug_name );
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 1 space before "="; 0 found
Loading history...
119 1
			$specifics []= sprintf( '%spost-%d-%s.php', $slug_dir, $post->ID, $slug_name );
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
120
		}
121
122 12
		$specifics []= sprintf( '%sview-%d-entry-%d-%s.php', $slug_dir, $this->view->ID, $this->entry->ID, $slug_name );
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 1 space before "="; 0 found
Loading history...
123 12
		$specifics []= sprintf( '%sform-%d-entry-%d-%s.php', $slug_dir, $this->view->form->ID, $this->entry->ID, $slug_name );
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
124 12
		$specifics []= sprintf( '%sview-%d-%s.php', $slug_dir, $this->view->ID, $slug_name );
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 1 space before "="; 0 found
Loading history...
125 12
		$specifics []= sprintf( '%sform-%d-%s.php', $slug_dir, $this->view->form->ID, $slug_name );
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
126
127 12
		$specifics []= sprintf( '%sentry-%d-%s.php', $slug_dir, $this->entry->ID, $slug_name );
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
128
129 12
		return array_merge( $specifics, $templates );
130
	}
131
132
	/**
133
	 * Output some HTML.
134
	 *
135
	 * @return void
136
	 */
137 14
	public function render() {
138 14
		$context = Template_Context::from_template( $this );
139
140
		/**
141
		 * Make various pieces of data available to the template
142
		 *  under the $gravityview scoped variable.
143
		 *
144
		 * @filter `gravityview/template/entry/context`
145
		 * @param \GV\Template_Context $context The context for this template.
146
		 * @param \GV\Entry_Template $template The current template.
147
		 * @since 2.0
148
		 */
149 14
		$this->push_template_data( apply_filters( 'gravityview/template/entry/context', $context, $this ), 'gravityview' );
150
151
		/** Load the template. */
152 14
		$this->get_template_part( static::$slug );
153 14
		$this->pop_template_data( 'gravityview' );
154 14
	}
155
156
	/**
157
	 * Fetch the back link label for an entry context.
158
	 *
159
	 * @param boolean $do_replace Whether to perform merge tag replacements, etc.
160
	 *
161
	 * @see `gravityview/template/links/back/label` filter
162
	 *
163
	 * @return string The link label
164
	 */
165 10
	public function get_back_label( $do_replace = true ) {
166 10
		if ( ! $this->view ) {
167
			return '';
168
		}
169
170 10
		$back_link_label = $this->view->settings->get( 'back_link_label', null );
171
172 10
		if ( $do_replace ) {
173 10
			$back_link_label = \GravityView_API::replace_variables( $back_link_label, Utils::get( $this->view, 'form/form' ), $this->entry->as_entry() );
174 10
			return do_shortcode( $back_link_label );
175
		}
176
		return $back_link_label;
177
	}
178
}
179
180
/** Load implementations. */
181
require gravityview()->plugin->dir( 'future/includes/class-gv-template-entry-table.php' );
182
require gravityview()->plugin->dir( 'future/includes/class-gv-template-entry-list.php' );
183
require gravityview()->plugin->dir( 'future/includes/class-gv-template-entry-legacy.php' );
184