1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Add custom options for address fields |
5
|
|
|
* @since 1.19 |
6
|
|
|
*/ |
7
|
|
|
class GravityView_Field_Entry_Approval extends GravityView_Field { |
8
|
|
|
|
9
|
|
|
var $name = 'entry_approval'; |
|
|
|
|
10
|
|
|
|
11
|
|
|
var $is_searchable = true; |
|
|
|
|
12
|
|
|
|
13
|
|
|
var $is_sortable = true; |
|
|
|
|
14
|
|
|
|
15
|
|
|
var $is_numeric = true; |
|
|
|
|
16
|
|
|
|
17
|
|
|
var $group = 'gravityview'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
var $contexts = array( 'single', 'multiple' ); |
|
|
|
|
20
|
|
|
|
21
|
|
|
public function __construct() { |
22
|
|
|
|
23
|
|
|
$this->label = esc_attr__( 'Approve Entries', 'gravityview' ); |
24
|
|
|
|
25
|
|
|
$this->description = esc_attr__( 'Approve and reject entries from the View.', 'gravityview' ); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$this->add_hooks(); |
28
|
|
|
|
29
|
|
|
parent::__construct(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Remove unused settings for the approval field |
34
|
|
|
* |
35
|
|
|
* @since 1.19 |
36
|
|
|
* |
37
|
|
|
* @param array $field_options |
38
|
|
|
* @param string $template_id |
39
|
|
|
* @param string $field_id |
40
|
|
|
* @param string $context |
41
|
|
|
* @param string $input_type |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
function field_options( $field_options, $template_id = '', $field_id = '', $context = '', $input_type = '' ) { |
|
|
|
|
46
|
|
|
|
47
|
|
|
unset( $field_options['only_loggedin'] ); |
48
|
|
|
|
49
|
|
|
unset( $field_options['new_window'] ); |
50
|
|
|
|
51
|
|
|
unset( $field_options['show_as_link'] ); |
52
|
|
|
|
53
|
|
|
return $field_options; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add filters and actions for the field |
58
|
|
|
* |
59
|
|
|
* @since 1.19 |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
private function add_hooks() { |
64
|
|
|
|
65
|
|
|
add_filter( 'gravityview_entry_default_fields', array( $this, 'filter_gravityview_entry_default_field' ), 10, 3 ); |
66
|
|
|
|
67
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts_and_styles' ) ); |
68
|
|
|
|
69
|
|
|
add_action( 'gravityview/field/approval/load_scripts', array( $this, 'enqueue_and_localize_script' ) ); |
70
|
|
|
|
71
|
|
|
add_action( 'gravityview_datatables_scripts_styles', array( $this, 'enqueue_and_localize_script' ) ); |
72
|
|
|
|
73
|
|
|
add_filter( 'gravityview_get_entries', array( $this, 'modify_search_parameters' ), 1000 ); |
74
|
|
|
|
75
|
|
|
add_filter( 'gravityview/field_output/html', array( $this, 'maybe_prevent_field_render' ), 10, 2 ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @filter `gravityview/template/field_label` Modify field label output |
80
|
|
|
* |
81
|
|
|
* @since 1.19 |
82
|
|
|
* |
83
|
|
|
* @param string $html Existing HTML output |
84
|
|
|
* @param array $args Arguments passed to the function |
85
|
|
|
* |
86
|
|
|
* @return string Empty string if user doesn't have the `gravityview_moderate_entries` cap; field HTML otherwise |
87
|
|
|
*/ |
88
|
|
|
public function maybe_prevent_field_render( $html, $args ) { |
89
|
|
|
|
90
|
|
|
// If the field is `entry_approval` type but the user doesn't have the moderate entries cap, don't render. |
91
|
|
|
if( $this->name === rgar( $args['field'], 'id' ) && ! GVCommon::has_cap('gravityview_moderate_entries') ) { |
|
|
|
|
92
|
|
|
return ''; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $html; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Modify search to use `is_approved` meta key to sort, instead of `entry_approval` |
100
|
|
|
* |
101
|
|
|
* @param array $parameters Search parameters used to generate GF search |
102
|
|
|
* |
103
|
|
|
* @return array Same parameters, but if sorting by `entry_approval`, changed to `is_approved` |
104
|
|
|
*/ |
105
|
|
|
public function modify_search_parameters( $parameters ) { |
106
|
|
|
|
107
|
|
|
if( $this->name === rgars( $parameters, 'sorting/key' ) ) { |
108
|
|
|
$parameters['sorting']['key'] = 'is_approved'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $parameters; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Register the field approval script and style |
116
|
|
|
* |
117
|
|
|
* @since 1.19 |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
function register_scripts_and_styles() { |
|
|
|
|
122
|
|
|
$script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
123
|
|
|
|
124
|
|
|
wp_register_script( 'gravityview-field-approval', GRAVITYVIEW_URL . 'assets/js/field-approval'.$script_debug.'.js', array( 'jquery' ), GravityView_Plugin::version, true ); |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Override CSS file by placing in your theme's /gravityview/css/ sub-directory. |
128
|
|
|
*/ |
129
|
|
|
$style_path = GravityView_View::getInstance()->locate_template( 'css/field-approval.css', false ); |
130
|
|
|
|
131
|
|
|
$style_url = plugins_url( 'css/field-approval.css', trailingslashit( dirname( $style_path ) ) ); |
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @filter `gravityview/field/approval/css_url` URL to the Approval field CSS file. |
135
|
|
|
* @since 1.19 |
136
|
|
|
* @param string $style_url Override to use your own CSS file, or return empty string to disable loading. |
137
|
|
|
*/ |
138
|
|
|
$style_url = apply_filters( 'gravityview/field/approval/css_url', $style_url ); |
139
|
|
|
|
140
|
|
|
if( ! empty( $style_url ) ) { |
141
|
|
|
wp_register_style( 'gravityview-field-approval', $style_url, array( 'dashicons' ), GravityView_Plugin::version, 'screen' ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
unset( $style_path, $style_url ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Register the field approval script and output the localized text JS variables |
149
|
|
|
* @since 1.19 |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
public function enqueue_and_localize_script() { |
153
|
|
|
|
154
|
|
|
// The script is already registered and enqueued |
155
|
|
|
if( wp_script_is( 'gravityview-field-approval', 'enqueued' ) ) { |
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
wp_enqueue_style( 'gravityview-field-approval' ); |
160
|
|
|
|
161
|
|
|
wp_enqueue_script( 'gravityview-field-approval' ); |
162
|
|
|
|
163
|
|
|
wp_localize_script( 'gravityview-field-approval', 'gvApproval', array( |
164
|
|
|
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
165
|
|
|
'nonce' => wp_create_nonce('gravityview_entry_approval'), |
|
|
|
|
166
|
|
|
'status' => GravityView_Entry_Approval_Status::get_all(), |
167
|
|
|
)); |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add Fields to the field list |
173
|
|
|
* |
174
|
|
|
* @since 1.19 |
175
|
|
|
* |
176
|
|
|
* @param array $entry_default_fields Array of fields shown by default |
177
|
|
|
* @param string|array $form form_ID or form object |
178
|
|
|
* @param string $context Either 'single', 'directory', 'header', 'footer' |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
public function filter_gravityview_entry_default_field( $entry_default_fields, $form, $context ) { |
|
|
|
|
183
|
|
|
|
184
|
|
|
if ( ! isset( $entry_default_fields["{$this->name}"] ) ) { |
|
|
|
|
185
|
|
|
$entry_default_fields["{$this->name}"] = array( |
|
|
|
|
186
|
|
|
'label' => $this->label, |
187
|
|
|
'desc' => $this->description, |
188
|
|
|
'type' => $this->name, |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $entry_default_fields; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get the anchor text for a link, based on the current status |
197
|
|
|
* |
198
|
|
|
* @since 1.19 |
199
|
|
|
* @uses GravityView_Entry_Approval_Status::get_string() |
200
|
|
|
* |
201
|
|
|
* @param string $approved_status Status string or key |
202
|
|
|
* |
203
|
|
|
* @return false|string False if string doesn't exist, otherwise the "label" for the status |
204
|
|
|
*/ |
205
|
|
|
public static function get_anchor_text( $approved_status = '' ) { |
206
|
|
|
return GravityView_Entry_Approval_Status::get_string( $approved_status, 'label' ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the title attribute for a link, based on the current status |
211
|
|
|
* |
212
|
|
|
* @since 1.19 |
213
|
|
|
* @uses GravityView_Entry_Approval_Status::get_string() |
214
|
|
|
* |
215
|
|
|
* @param int|string $approved_status Status string or key |
216
|
|
|
* |
217
|
|
|
* @return false|string |
218
|
|
|
*/ |
219
|
|
|
public static function get_title_attr( $approved_status ) { |
220
|
|
|
return GravityView_Entry_Approval_Status::get_string( $approved_status, 'title' ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get the CSS class for a link, based on the current status |
225
|
|
|
* |
226
|
|
|
* @param int|string $approved_status Status string or key |
227
|
|
|
* |
228
|
|
|
* @return string CSS class, sanitized using esc_attr() |
229
|
|
|
*/ |
230
|
|
|
public static function get_css_class( $approved_status ) { |
231
|
|
|
|
232
|
|
|
$approved_key = GravityView_Entry_Approval_Status::get_key( $approved_status ); |
233
|
|
|
|
234
|
|
|
return esc_attr( "gv-approval-{$approved_key}" ); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
new GravityView_Field_Entry_Approval; |
239
|
|
|
|
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.