Completed
Pull Request — develop (#1572)
by Zack
19:45
created

GravityView_Field_Other_Entries::field_options()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 5.3159

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 6
dl 0
loc 59
ccs 2
cts 33
cp 0.0606
crap 5.3159
rs 8.8945
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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';
17
18
	var $is_searchable = false;
19
20
	var $contexts = array( 'multiple', 'single' );
21
22
	var $group = 'gravityview';
23
24
	var $icon = 'dashicons-admin-page';
25
26
	private $context;
27
28
	public function __construct() {
29
		$this->label = esc_html__( 'Other Entries', 'gravityview' );
30
		$this->description = esc_html__('Display other entries created by the entry creator.', 'gravityview');
31
		parent::__construct();
32
	}
33
34
	/**
35
	 * @inheritDoc
36
	 * @since 1.7.2
37
	 */
38
	public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
39
40
		if( 'edit' === $context ) {
41
			return $field_options;
42
		}
43
44
		// No "Link to single entry"; all the items will be links to entries!
45
		unset( $field_options['show_as_link'] );
46
47
		$new_options = array();
48
49
		$new_options['link_format'] = array(
50
			'type'  => 'text',
51
			'label' => __( 'Entry link text (required)', 'gravityview' ),
52
			'value' => __('Entry #{entry_id}', 'gravityview'),
53
			'merge_tags' => 'force',
54
			'group' => 'field',
55
		);
56
57
		$new_options['after_link'] = array(
58
			'type'  => 'textarea',
59
			'label' => __( 'Text or HTML to display after the link (optional)', 'gravityview' ),
60
			'desc'  => __('This content will be displayed below each entry link.', 'gravityview'),
61
			'value' => '',
62
			'merge_tags' => 'force',
63
			'class' => 'widefat code',
64
			'group' => 'field',
65
		);
66
67
		$new_options['page_size'] = array(
68
			'type'  => 'number',
69
			'label' => __( 'Entries to Display', 'gravityview' ),
70
			'desc'  => __( 'What is the maximum number of entries that should be shown?', 'gravityview' ) . ' ' . sprintf( _x( 'Set to %s for no maximum.', '%s replaced with a formatted 0', 'gravityview' ), '<code>0</code>' ),
71
			'value' => '10',
72
			'merge_tags' => false,
73
			'min'   => 0,
74
			'group' => 'field',
75
		);
76
77
		$new_options['no_entries_hide'] = array(
78
			'type'  => 'checkbox',
79
			'label' => __( 'Hide if no entries', 'gravityview' ),
80
			'desc'  => __( 'Don\'t display this field if the entry creator has no other entries', 'gravityview' ),
81
			'value' => false,
82
			'group' => 'visibility',
83
		);
84
85
		$new_options['no_entries_text'] = array(
86
			'type'  => 'text',
87
			'label' => __( 'No Entries Text', 'gravityview' ),
88
			'desc'  => __( 'The text that is shown if the entry creator has no other entries (and "Hide if no entries" is disabled).', 'gravityview' ),
89
			'value' => __( 'This user has no other entries.', 'gravityview' ),
90
			'class' => 'widefat',
91
			'requires' => 'no_entries_hide',
92
			'group' => 'visibility',
93
		);
94
95 2
		return $new_options + $field_options;
96 2
	}
97 2
98
	/**
99
	 * Retrieve the other entries based on the current View and entry.
100 2
	 *
101
	 * @param \GV\Template_Context $context The context that contains the View and the Entry.
102 2
	 *
103
	 * @return \GV\Entry[] The entries.
104
	 */
105
	public function get_entries( $context ) {
106 2
		add_action( 'gravityview/view/query', array( $this, 'gf_query_filter' ), 10, 3 );
107 2
		add_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ), 10, 3 );
108 2
109 2
		// Exclude widiget modifiers altogether
110 2
		global $wp_filter;
111
		$filters = array(
112
			'gravityview_fe_search_criteria',
113 2
			'gravityview_search_criteria',
114 2
			'gravityview/view/query',
115
		);
116
		$removed = $remove = array();
117
		foreach ( $filters as $filter ) {
118
			foreach ( $wp_filter[ $filter ] as $priority => $callbacks ) {
119
				foreach ( $callbacks as $id => $callback ) {
120 2
					if ( ! is_array( $callback['function'] ) ) {
121 2
						continue;
122 2
					}
123 2
					if ( $callback['function'][0] instanceof \GV\Widget ) {
124
						$remove[] = array( $filter, $priority, $id );
125
					}
126 2
				}
127
			}
128 2
		}
129
130 2
		foreach ( $remove as $r ) {
131 2
			list( $filter, $priority, $id ) = $r;
132 2
			$removed[] = array( $filter, $priority, $id, $wp_filter[ $filter ]->callbacks[ $priority ][ $id ] );
133
			unset( $wp_filter[ $filter ]->callbacks[ $priority ][ $id ] );
134
		}
135 2
136 2
		$this->context = $context;
137
138 2
		$entries = $context->view->get_entries()->all();
139
140 2
		foreach ( $removed as $r ) {
141
			list( $filter, $priority, $id, $function ) = $r;
142
			$wp_filter[ $filter ]->callbacks[ $priority ][ $id ] = $function;
143 2
		}
144 2
145
		remove_action( 'gravityview/view/query', array( $this, 'gf_query_filter' ) );
146 2
		remove_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ) );
147
148
		$this->context = null;
149 2
150 2
		return $entries;
151 2
	}
152 2
153
	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...
154
		$context = $this->context;
155
156
		$created_by = $context->entry['created_by'];
157
158
		/** Filter entries by approved and created_by. */
159
		$search_criteria['field_filters'][] = array(
160
			'key' => 'created_by',
161
			'value' => $created_by,
162
			'operator' => 'is'
163
		);
164
165
		/**
166 2
		 * @filter `gravityview/field/other_entries/criteria` Modify the search parameters before the entries are fetched.
167
		 *
168
		 * @since 1.11
169 2
		 *
170 2
		 * @param array $criteria Gravity Forms search criteria array, as used by GVCommon::get_entries()
171 2
		 * @param array $view_settings Associative array of settings with plugin defaults used if not set by the View
172 2
		 * @param int $form_id The Gravity Forms ID
173 2
		 * @since 2.0
174
		 * @param \GV\Template_Context $gravityview The context
175
		 */
176 2
		$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...
177
178 2
		/** Force mode all and filter out our own entry. */
179
		$search_criteria['field_filters']['mode'] = 'all';
180
		$search_criteria['field_filters'][] = array(
181 2
			'key' => 'id',
182
			'value' => $context->entry->ID,
183
			'operator' => 'isnot'
184 2
		);
185
186
		$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...
187
188
		return $search_criteria;
189
	}
190
191
	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...
192
		// @todo One day, we can implement in GF_Query as well...
193
		// this would allow us to keep on using nested conditionals and not force 'all'
194
	}
195
}
196
197
new GravityView_Field_Other_Entries;
198