Completed
Push — master ( cd320e...b9ab02 )
by Zack
15s
created

GravityView_Shortcode   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 55%

Importance

Changes 0
Metric Value
dl 0
loc 157
ccs 33
cts 60
cp 0.55
rs 10
c 0
b 0
f 0
wmc 24
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 57 12
C 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 20 and the first side effect is on line 178.

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, see \GV\Shortcodes\gravityview
19
 */
20
class GravityView_Shortcode {
21
22
	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...
23
		$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...
24
	}
25
26
	private function add_hooks() {
27
	}
28
29
	/**
30
	 * Callback function for add_shortcode()
31
	 *
32
	 * @since 1.13
33
	 *
34
	 * @access public
35
	 * @static
36
	 * @param mixed $passed_atts
37
	 * @param string|null $content Content passed inside the shortcode
38
	 * @return null|string If admin, null. Otherwise, output of $this->render_view()
39
	 */
40 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...
41
42
		// Don't process when saving post.
43 1
		if ( is_admin() ) {
44
			return null;
45
		}
46
47 1
		gravityview()->log->debug( '$passed_atts: ', array( 'data' => $passed_atts ) );
48
49
		// Get details about the current View
50 1
		if( !empty( $passed_atts['detail'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
51 1
			return $this->get_view_detail( $passed_atts['detail'] );
52
		}
53
54 1
		$atts = $this->parse_and_sanitize_atts( $passed_atts );
55
56 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...
57
	}
58
59
	/**
60
	 * Validate attributes passed to the [gravityview] shortcode. Supports {get} Merge Tags values.
61
	 *
62
	 * Attributes passed to the shortcode are compared to registered attributes {@see \GV\View_Settings::defaults}
63
	 * Only attributes that are defined will be allowed through.
64
	 *
65
	 * Then, {get} merge tags are replaced with their $_GET values, if passed
66
	 *
67
	 * Then, attributes are sanitized based on the type of setting (number, checkbox, select, radio, text)
68
	 *
69
	 * @since 1.15.1
70
	 *
71
	 * @see \GV\View_Settings::defaults() Only attributes defined in default() are valid to be passed via the shortcode
72
	 *
73
	 * @param array $passed_atts Attribute pairs defined to render the View
74
	 *
75
	 * @return array Valid and sanitized attribute pairs
76
	 */
77 1
	private function parse_and_sanitize_atts( $passed_atts ) {
78
79 1
		$defaults = \GV\View_Settings::defaults( true );
80
81 1
		$supported_atts = array_fill_keys( array_keys( $defaults ), '' );
82
83
		// Whittle down the attributes to only valid pairs
84 1
		$filtered_atts = shortcode_atts( $supported_atts, $passed_atts, 'gravityview' );
85
86
		// Only keep the passed attributes after making sure that they're valid pairs
87 1
		$filtered_atts = function_exists( 'array_intersect_key' ) ? array_intersect_key( (array) $passed_atts, $filtered_atts ) : $filtered_atts;
88
89 1
		$atts = array();
90
91 1
		foreach( $filtered_atts as $key => $passed_value ) {
92
93
			// Allow using GravityView merge tags in shortcode attributes, like {get} and {created_by}
94 1
			$passed_value = GravityView_Merge_Tags::replace_variables( $passed_value );
95
96 1
			switch( $defaults[ $key ]['type'] ) {
97
98
				/**
99
				 * Make sure number fields are numeric.
100
				 * Also, convert mixed number strings to numbers
101
				 * @see http://php.net/manual/en/function.is-numeric.php#107326
102
				 */
103 1
				case 'number':
104 1
					if( is_numeric( $passed_value ) ) {
105 1
						$atts[ $key ] = ( $passed_value + 0 );
106
					}
107 1
					break;
108
109
				// Checkboxes should be 1 or 0
110 1
				case 'checkbox':
111 1
					$atts[ $key ] = gv_empty( $passed_value, true, false ) ? 0 : 1;
112 1
					break;
113
114
				/**
115
				 * Only allow values that are defined in the settings
116
				 */
117 1
				case 'select':
118 1
				case 'radio':
119 1
					$options = isset( $defaults[ $key ]['choices'] ) ? $defaults[ $key ]['choices'] : $defaults[ $key ]['options'];
120 1
					if( in_array( $passed_value, array_keys( $options ) ) ) {
121 1
						$atts[ $key ] = $passed_value;
122
					}
123 1
					break;
124
125 1
				case 'text':
126
				default:
127 1
					$atts[ $key ] = $passed_value;
128 1
					break;
129
			}
130
		}
131
132 1
		return $atts;
133
	}
134
135
	/**
136
	 * Display details for the current View
137
	 *
138
	 * @since 1.13
139
	 *
140
	 * @param string $detail The information requested about the current View. Accepts `total_entries`, `first_entry` (entry #), `last_entry` (entry #), and `page_size`
141
	 *
142
	 * @return string Detail information
143
	 */
144
	private function get_view_detail( $detail = '' ) {
145
146
		$gravityview_view = GravityView_View::getInstance();
147
		$return = '';
148
149
		switch( $detail ) {
150
			case 'total_entries':
151
				$return = number_format_i18n( $gravityview_view->getTotalEntries() );
152
				break;
153
			case 'first_entry':
154
				$paging = $gravityview_view->getPaginationCounts();
155
				$return = empty( $paging ) ? '' : number_format_i18n( \GV\Utils::get( $paging, 'first', 0 ) );
156
				break;
157
			case 'last_entry':
158
				$paging = $gravityview_view->getPaginationCounts();
159
				$return = empty( $paging ) ? '' : number_format_i18n( \GV\Utils::get( $paging, 'last', 0 ) );
160
				break;
161
			case 'page_size':
162
				$paging = $gravityview_view->getPaging();
163
				$return = number_format_i18n( \GV\Utils::get( $paging, 'page_size', 0 ) );
164
				break;
165
		}
166
167
		/**
168
		 * @filter `gravityview/shortcode/detail/{$detail}` Filter the detail output returned from `[gravityview detail="$detail"]`
169
		 * @since 1.13
170
		 * @param string $return Existing output
171
		 */
172
		$return = apply_filters( 'gravityview/shortcode/detail/' . $detail, $return );
173
174
		return $return;
175
	}
176
}
177
178
new GravityView_Shortcode;
0 ignored issues
show
Deprecated Code introduced by
The class GravityView_Shortcode has been deprecated with message: since 2.0.3, see \GV\Shortcodes\gravityview

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...