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
|
|
|
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'); |
|
|
|
|
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 ) { |
|
|
|
|
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'), |
|
|
|
|
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'), |
|
|
|
|
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; |
|
|
|
|
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 ) { |
|
|
|
|
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' |
|
|
|
|
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 ); |
|
|
|
|
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' |
|
|
|
|
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
$search_criteria['paging']['page_size'] = $context->field->page_size ? : 10; |
|
|
|
|
177
|
|
|
|
178
|
|
|
return $search_criteria; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function gf_query_filter( &$query, $view, $request ) { |
|
|
|
|
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
|
|
|
|
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.