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

gvfield   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 14

Importance

Changes 0
Metric Value
dl 0
loc 165
rs 10
c 0
b 0
f 0
wmc 27
lcom 0
cbo 14

1 Method

Rating   Name   Duplication   Size   Complexity  
D callback() 0 150 27
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 12 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\Shortcodes;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The [gvfield] shortcode.
11
 */
12
class gvfield extends \GV\Shortcode {
0 ignored issues
show
Coding Style introduced by
Class name "gvfield" is not in camel caps format
Loading history...
13
	/**
14
	 * {@inheritDoc}
15
	 */
16
	public $name = 'gvfield';
17
18
	/**
19
	 * Process and output the [gvfield] shortcode.
20
	 *
21
	 * @param array $atts The attributes passed.
22
	 * @param string $content The content inside the shortcode.
23
	 *
24
	 * @return string The output.
25
	 */
26
	public function callback( $atts, $content = null ) {
27
		$request = gravityview()->request;
28
29
		if ( $request->is_admin() ) {
30
			return apply_filters( 'gravityview/shortcodes/gvfield/output', '', null, null, $atts );
31
		}
32
33
		$atts = wp_parse_args( $atts, array(
34
			'view_id' => null,
35
			'entry_id' => null,
36
			'field_id' => null,
37
		) );
38
39
		/**
40
		 * @filter `gravityview/shortcodes/gvfield/atts` Filter the [gvfield] shortcode attributes.
41
		 * @param array $atts The initial attributes.
42
		 * @since 2.0
43
		 */
44
		$atts = apply_filters( 'gravityview/shortcodes/gvfield/atts', $atts );
45
46
		if ( ! $view = \GV\View::by_id( $atts['view_id'] ) ) {
47
			gravityview()->log->error( 'View #{view_id} not found', array( 'view_id' => $atts['view_id'] ) );
48
			return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, null, null, $atts );
49
		}
50
51
		switch( $atts['entry_id'] ):
52
			case 'last':
53
				if ( gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) {
54
					/**
55
					 * @todo Remove once we refactor the use of get_view_entries_parameters.
56
					 *
57
					 * Since we're using \GF_Query shorthand initialization we have to reverse the order parameters here.
58
					 */
59
					add_filter( 'gravityview_get_entries', $filter = function( $parameters, $args, $form_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
Unused Code introduced by
The parameter $form_id 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...
60
						if ( ! empty( $parameters['sorting'] ) ) {
61
							/**
62
							 * Reverse existing sorts.
63
							 */
64
							$sort = &$parameters['sorting'];
65
							$sort['direction'] = $sort['direction'] == 'RAND' ? : ( $sort['direction'] == 'ASC' ? 'DESC' : 'ASC' );
66
						} else {
67
							/**
68
							 * Otherwise, sort by date_created.
69
							 */
70
							$parameters['sorting'] = array(
71
								'key' => 'id',
72
								'direction' => 'ASC',
73
								'is_numeric' => true
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
74
							);
75
						}
76
						return $parameters;
77
					}, 10, 3 );
78
					$entries = $view->get_entries( null );
79
					remove_filter( 'gravityview_get_entries', $filter );
80
				} else {
81
					$entries = $view->get_entries( null );
82
83
					/** If a sort already exists, reverse it. */
84
					if ( $sort = end( $entries->sorts ) ) {
85
						$entries = $entries->sort( new \GV\Entry_Sort( $sort->field, $sort->direction == \GV\Entry_Sort::RAND ? : ( $sort->direction == \GV\Entry_Sort::ASC ? \GV\Entry_Sort::DESC : \GV\Entry_Sort::ASC ) ), $sort->mode );
0 ignored issues
show
Unused Code introduced by
The call to Entry_Collection::sort() has too many arguments starting with $sort->mode.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86
					} else {
87
						/** Otherwise, sort by date_created */
88
						$entries = $entries->sort( new \GV\Entry_Sort( \GV\Internal_Field::by_id( 'id' ), \GV\Entry_Sort::ASC ), \GV\Entry_Sort::NUMERIC );
0 ignored issues
show
Unused Code introduced by
The call to Entry_Collection::sort() has too many arguments starting with \GV\Entry_Sort::NUMERIC.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
89
					}
90
				}
91
92
				if ( ! $entry = $entries->first() ) {
93
					return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts );
94
				}
95
				break;
96
			case 'first':
97
				if ( ! $entry = $view->get_entries( null )->first() ) {
98
					return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts );
99
				}
100
				break;
101
			default:
102
				if ( ! $entry = \GV\GF_Entry::by_id( $atts['entry_id'] ) ) {
103
					gravityview()->log->error( 'Entry #{entry_id} not found', array( 'view_id' => $atts['view_id'] ) );
104
					return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts );
105
				}
106
		endswitch;
107
108
		$field = is_numeric( $atts['field_id'] ) ? \GV\GF_Field::by_id( $view->form, $atts['field_id'] ) : \GV\Internal_Field::by_id( $atts['field_id'] );
109
110
		if ( ! $field ) {
111
			gravityview()->log->error( 'Field #{field_id} not found', array( 'view_id' => $atts['field_id'] ) );
112
			return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $field, $atts );
113
		}
114
115
		if ( $view->form->ID != $entry['form_id'] ) {
116
			gravityview()->log->error( 'Entry does not belong to view (form mismatch)' );
117
			return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
118
		}
119
120
		if ( post_password_required( $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...
121
			gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $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...
122
			return apply_filters( 'gravityview/shortcodes/gvfield/output', get_the_password_form( $view->ID ), $view, $entry, $atts );
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...
123
		}
124
125
		if ( ! $view->form  ) {
126
			gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $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...
127
128
			/**
129
			 * This View has no data source. There's nothing to show really.
130
			 * ...apart from a nice message if the user can do anything about it.
131
			 */
132
			if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_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...
133
				$return = __( sprintf( 'This View is not configured properly. Start by <a href="%s">selecting a form</a>.', esc_url( get_edit_post_link( $view->ID, false ) ) ), '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...
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
134
				return apply_filters( 'gravityview/shortcodes/gvfield/output', $return, $view, $entry, $atts );
135
			}
136
137
			return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
138
		}
139
140
		/** Private, pending, draft, etc. */
141
		$public_states = get_post_stati( array( 'public' => true ) );
142
		if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) {
0 ignored issues
show
Documentation introduced by
The property post_status 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...
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...
143
			gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $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...
144
			return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
145
		}
146
147
		/** Unapproved entries. */
148
		if ( $entry['status'] != 'active' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
149
			gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) );
150
			return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
151
		}
152
153
		if ( $view->settings->get( 'show_only_approved' ) ) {
154
			if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) )  ) {
155
				gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) );
156
				return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts );
157
			}
158
		}
159
160
		$field->update_configuration( $atts );
161
162
		$renderer = new \GV\Field_Renderer();
163
		$output = $renderer->render( $field, $view, is_numeric( $field->ID ) ? $view->form : new \GV\Internal_Source(), $entry, gravityview()->request );
164
165
		/**
166
		 * @filter `gravityview/shortcodes/gvfield/output` Filter the [gvfield] output.
167
		 * @param string $output The output.
168
		 * @param \GV\View|null $view The View detected or null.
169
		 * @param \GV\Entry|null $entry The Entry or null.
170
		 * @param \GV\Field|null $field The Field or null.
171
		 *
172
		 * @since 2.0
173
		 */
174
		return apply_filters( 'gravityview/shortcodes/gvfield/output', $output, $view, $entry, $field, $atts );
175
	}
176
}
177