Completed
Push — develop ( 936044...d510c0 )
by Zack
16:20
created

GravityView_Shortcode   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 57.8%

Importance

Changes 0
Metric Value
dl 0
loc 164
ccs 37
cts 64
cp 0.578
rs 10
c 0
b 0
f 0
wmc 25
lcom 0
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add_hooks() 0 2 1
A shortcode() 0 18 3
C parse_and_sanitize_atts() 0 64 13
B get_view_detail() 0 32 7
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 21 and the first side effect is on line 186.

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
/**
3
 * [gravityview] Shortcode class
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    Katz Web Services, Inc.
8
 * @link      http://gravityview.co
9
 * @copyright Copyright 2015, Katz Web Services, Inc.
10
 *
11
 * @since 1.13
12
 */
13
14
/**
15
 * Handle the [gravityview] shortcode
16
 *
17
 * @since 1.13
18
 * @deprecated since 2.0.3
19
 * @see \GV\Shortcodes\gravityview
20
 */
21
class GravityView_Shortcode {
22
23
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
		$this->add_hooks();
0 ignored issues
show
Unused Code introduced by
The call to the method GravityView_Shortcode::add_hooks() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
25
	}
26
27
	private function add_hooks() {
28
	}
29
30
	/**
31
	 * Callback function for add_shortcode()
32
	 *
33
	 * @since 1.13
34
	 *
35
	 * @access public
36
	 * @static
37
	 * @param mixed $passed_atts
38
	 * @param string|null $content Content passed inside the shortcode
39
	 * @return null|string If admin, null. Otherwise, output of $this->render_view()
40
	 */
41 1
	function shortcode( $passed_atts, $content = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
43
		// Don't process when saving post.
44 1
		if ( is_admin() ) {
45
			return null;
46
		}
47
48 1
		gravityview()->log->debug( '$passed_atts: ', array( 'data' => $passed_atts ) );
49
50
		// Get details about the current View
51 1
		if( !empty( $passed_atts['detail'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
52 1
			return $this->get_view_detail( $passed_atts['detail'] );
53
		}
54
55 1
		$atts = $this->parse_and_sanitize_atts( $passed_atts );
56
57 1
		return GravityView_frontend::getInstance()->render_view( $atts );
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_frontend::render_view() has been deprecated with message: Use \GV\View_Renderer

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
	}
59
60
	/**
61
	 * Validate attributes passed to the [gravityview] shortcode. Supports {get} Merge Tags values.
62
	 *
63
	 * Attributes passed to the shortcode are compared to registered attributes {@see \GV\View_Settings::defaults}
64
	 * Only attributes that are defined will be allowed through.
65
	 *
66
	 * Then, {get} merge tags are replaced with their $_GET values, if passed
67
	 *
68
	 * Then, attributes are sanitized based on the type of setting (number, checkbox, select, radio, text)
69
	 *
70
	 * @since 1.15.1
71
	 *
72
	 * @see \GV\View_Settings::defaults() Only attributes defined in default() are valid to be passed via the shortcode
73
	 *
74
	 * @param array $passed_atts Attribute pairs defined to render the View
75
	 *
76
	 * @return array Valid and sanitized attribute pairs
77
	 */
78 1
	private function parse_and_sanitize_atts( $passed_atts ) {
79
80 1
		$defaults = \GV\View_Settings::defaults( true );
81
82 1
		$supported_atts = array_fill_keys( array_keys( $defaults ), '' );
83
84
		// Whittle down the attributes to only valid pairs
85 1
		$filtered_atts = shortcode_atts( $supported_atts, $passed_atts, 'gravityview' );
86
87
		// Only keep the passed attributes after making sure that they're valid pairs
88 1
		$filtered_atts = array_intersect_key( (array) $passed_atts, $filtered_atts );
89
90 1
		$atts = array();
91
92 1
		foreach( $filtered_atts as $key => $passed_value ) {
93
94
			// Allow using GravityView merge tags in shortcode attributes, like {get} and {created_by}
95 1
			$passed_value = GravityView_Merge_Tags::replace_variables( $passed_value );
96
97 1
			switch( $defaults[ $key ]['type'] ) {
98
99
				/**
100
				 * Make sure number fields are numeric.
101
				 * Also, convert mixed number strings to numbers
102
				 * @see http://php.net/manual/en/function.is-numeric.php#107326
103
				 */
104 1
				case 'number':
105 1
					if( is_numeric( $passed_value ) ) {
106 1
						$atts[ $key ] = ( $passed_value + 0 );
107
					}
108 1
					break;
109
110
				/** @since 2.1 */
111 1
				case 'operator':
112 1
					if( GFFormsModel::is_valid_operator( $passed_value ) ) {
113 1
						$atts[ $key ] = $passed_value;
114
					}
115 1
					break;
116
117
				// Checkboxes should be 1 or 0
118 1
				case 'checkbox':
119 1
					$atts[ $key ] = gv_empty( $passed_value, true, false ) ? 0 : 1;
120 1
					break;
121
122
				/**
123
				 * Only allow values that are defined in the settings
124
				 */
125 1
				case 'select':
126 1
				case 'radio':
127 1
					$options = isset( $defaults[ $key ]['choices'] ) ? $defaults[ $key ]['choices'] : $defaults[ $key ]['options'];
128 1
					if( in_array( $passed_value, array_keys( $options ) ) ) {
129 1
						$atts[ $key ] = $passed_value;
130
					}
131 1
					break;
132
133 1
				case 'text':
134
				default:
135 1
					$atts[ $key ] = $passed_value;
136 1
					break;
137
			}
138
		}
139
140 1
		return $atts;
141
	}
142
143
	/**
144
	 * Display details for the current View
145
	 *
146
	 * @since 1.13
147
	 *
148
	 * @param string $detail The information requested about the current View. Accepts `total_entries`, `first_entry` (entry #), `last_entry` (entry #), and `page_size`
149
	 *
150
	 * @return string Detail information
151
	 */
152
	private function get_view_detail( $detail = '' ) {
153
154
		$gravityview_view = GravityView_View::getInstance();
155
		$return = '';
156
157
		switch( $detail ) {
158
			case 'total_entries':
159
				$return = number_format_i18n( $gravityview_view->getTotalEntries() );
160
				break;
161
			case 'first_entry':
162
				$paging = $gravityview_view->getPaginationCounts();
163
				$return = empty( $paging ) ? '' : number_format_i18n( \GV\Utils::get( $paging, 'first', 0 ) );
164
				break;
165
			case 'last_entry':
166
				$paging = $gravityview_view->getPaginationCounts();
167
				$return = empty( $paging ) ? '' : number_format_i18n( \GV\Utils::get( $paging, 'last', 0 ) );
168
				break;
169
			case 'page_size':
170
				$paging = $gravityview_view->getPaging();
171
				$return = number_format_i18n( \GV\Utils::get( $paging, 'page_size', 0 ) );
172
				break;
173
		}
174
175
		/**
176
		 * @filter `gravityview/shortcode/detail/{$detail}` Filter the detail output returned from `[gravityview detail="$detail"]`
177
		 * @since 1.13
178
		 * @param string $return Existing output
179
		 */
180
		$return = apply_filters( 'gravityview/shortcode/detail/' . $detail, $return );
181
182
		return $return;
183
	}
184
}
185
186
new GravityView_Shortcode;
0 ignored issues
show
Deprecated Code introduced by
The class GravityView_Shortcode has been deprecated with message: since 2.0.3

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
Unused Code introduced by
The call to the method GravityView_Shortcode::__construct() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...