Completed
Push — develop ( 636298...36bdeb )
by Gennady
16:37
created

GravityView_Field_Other_Entries::get_entries()   B

Complexity

Conditions 8
Paths 24

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 24
nop 1
dl 0
loc 47
ccs 0
cts 0
cp 0
crap 72
rs 7.9119
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 187.

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
 * @file class-gravityview-field-other-entries.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 * @since 1.7.2
7
 */
8
9
/**
10
 * A field that displays other entries by the entry_creator for the same View in a list format
11
 *
12
 * @since 1.7.2
13
 */
14
class GravityView_Field_Other_Entries extends GravityView_Field {
15
16
	var $name = 'other_entries';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
17
18
	var $is_searchable = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_searchable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
19
20
	var $contexts = array( 'multiple', 'single' );
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $contexts.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
21
22
	var $group = 'gravityview';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $group.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
23
24
	private $context;
25
26
	public function __construct() {
27
		$this->label = esc_html__( 'Other Entries', 'gravityview' );
28
		$this->description = esc_html__('Display other entries created by the entry creator.', 'gravityview');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
29
		parent::__construct();
30
	}
31
32
	/**
33
	 * @inheritDoc
34
	 * @since 1.7.2
35
	 */
36
	function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
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...
37
38
		if( 'edit' === $context ) {
39
			return $field_options;
40
		}
41
42
		// No "Link to single entry"; all the items will be links to entries!
43
		unset( $field_options['show_as_link'] );
44
45
		$new_options = array();
46
47
		$new_options['link_format'] = array(
48
			'type'  => 'text',
49
			'label' => __( 'Entry link text (required)', 'gravityview' ),
50
			'value' => __('Entry #{entry_id}', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
51
			'merge_tags' => 'force',
52
		);
53
54
		$new_options['after_link'] = array(
55
			'type'  => 'textarea',
56
			'label' => __( 'Text or HTML to display after the link (optional)', 'gravityview' ),
57
			'desc'  => __('This content will be displayed below each entry link.', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
58
			'value' => '',
59
			'merge_tags' => 'force',
60
			'class' => 'widefat code',
61
		);
62
63
		$new_options['page_size'] = array(
64
			'type'  => 'number',
65
			'label' => __( 'Entries to Display', 'gravityview' ),
66
			'desc'  => __( 'What is the maximum number of entries that should be shown?', 'gravityview' ),
67
			'value' => '10',
68
			'merge_tags' => false,
69
		);
70
71
		$new_options['no_entries_hide'] = array(
72
			'type'  => 'checkbox',
73
			'label' => __( 'Hide if no entries', 'gravityview' ),
74
			'desc'  => __( 'Don\'t display this field if the entry creator has no other entries', 'gravityview' ),
75
			'value' => false,
76
		);
77
78
		$new_options['no_entries_text'] = array(
79
			'type'  => 'text',
80
			'label' => __( 'No Entries Text', 'gravityview' ),
81
			'desc'  => __( 'The text that is shown if the entry creator has no other entries (and "Hide if no entries" is disabled).', 'gravityview' ),
82
			'value' => __( 'This user has no other entries.', 'gravityview' ),
83
		);
84
85
		return $new_options + $field_options;
86
	}
87
88
	/**
89
	 * Retrieve the other entries based on the current View and entry.
90
	 *
91
	 * @param \GV\Template_Context $context The context that contains the View and the Entry.
92
	 *
93
	 * @return \GV\Entry[] The entries.
94
	 */
95
	public function get_entries( $context ) {
96
		add_action( 'gravityview/view/query', array( $this, 'gf_query_filter' ), 10, 3 );
97
		add_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ), 10, 3 );
98
99
		// Exclude widiget modifiers altogether
100
		global $wp_filter;
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...
101
		$filters = array(
102
			'gravityview_fe_search_criteria',
103
			'gravityview_search_criteria',
104
			'gravityview/view/query',
105
		);
106
		$removed = $remove = array();
107
		foreach ( $filters as $filter ) {
108
			foreach ( $wp_filter[ $filter ] as $priority => $callbacks ) {
109
				foreach ( $callbacks as $id => $callback ) {
110
					if ( ! is_array( $callback['function'] ) ) {
111
						continue;
112
					}
113
					if ( $callback['function'][0] instanceof \GV\Widget ) {
114
						$remove[] = array( $filter, $priority, $id );
115
					}
116
				}
117
			}
118
		}
119
120
		foreach ( $remove as $r ) {
121
			list( $filter, $priority, $id ) = $r;
122
			$removed[] = array( $filter, $priority, $id, $wp_filter[ $filter ]->callbacks[ $priority ][ $id ] );
123
			unset( $wp_filter[ $filter ]->callbacks[ $priority ][ $id ] );
124
		}
125
126
		$this->context = $context;
127
128
		$entries = $context->view->get_entries()->all();
129
130
		foreach ( $removed as $r ) {
131
			list( $filter, $priority, $id, $function ) = $r;
132
			$wp_filter[ $filter ]->callbacks[ $priority ][ $id ] = $function;
133
		}
134
135
		remove_action( 'gravityview/view/query', array( $this, 'gf_query_filter' ) );
136
		remove_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ) );
137
138
		$this->context = null;
139
140
		return $entries;
141
	}
142
143
	public function filter_entries( $search_criteria, $form_id = null, $args = array(), $force_search_criteria = false ) {
0 ignored issues
show
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...
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 $force_search_criteria 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...
144
		$context = $this->context;
145
146
		$created_by = $context->entry['created_by'];
147
148
		/** Filter entries by approved and created_by. */
149
		$search_criteria['field_filters'][] = array(
150
			'key' => 'created_by',
151
			'value' => $created_by,
152
			'operator' => 'is'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
153
		);
154
155
		/**
156
		 * @filter `gravityview/field/other_entries/criteria` Modify the search parameters before the entries are fetched.
157
		 *
158
		 * @since 1.11
159
		 *
160
		 * @param array $criteria Gravity Forms search criteria array, as used by GVCommon::get_entries()
161
		 * @param array $view_settings Associative array of settings with plugin defaults used if not set by the View
162
		 * @param int $form_id The Gravity Forms ID
163
		 * @since 2.0
164
		 * @param \GV\Template_Context $gravityview The context
165
		 */
166
		$criteria = apply_filters( 'gravityview/field/other_entries/criteria', $search_criteria, $context->view->settings->as_atts(), $context->view->form->ID, $context );
0 ignored issues
show
Deprecated Code introduced by
The method GV\View_Settings::as_atts() has been deprecated.

This method has been deprecated.

Loading history...
167
168
		/** Force mode all and filter out our own entry. */
169
		$search_criteria['field_filters']['mode'] = 'all';
170
		$search_criteria['field_filters'][] = array(
171
			'key' => 'id',
172
			'value' => $context->entry->ID,
173
			'operator' => 'isnot'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
174
		);
175
176
		$search_criteria['paging']['page_size'] = $context->field->page_size ? : 10;
0 ignored issues
show
Documentation introduced by
The property page_size does not exist on object<GV\Field>. 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...
177
178
		return $search_criteria;
179
	}
180
181
	public function gf_query_filter( &$query, $view, $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $query 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 $view 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 $request 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...
182
		// @todo One day, we can implement in GF_Query as well...
183
		// this would allow us to keep on using nested conditionals and not force 'all'
184
	}
185
}
186
187
new GravityView_Field_Other_Entries;
188