Completed
Push — develop ( 7592b8...3eb7c5 )
by Zack
14:44
created

GravityView_Field_Entry_Approval::field_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
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 7 and the first side effect is on line 244.

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
/**
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';
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...
10
11
	var $is_searchable = true;
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...
12
13
	public $search_operators = array( 'is', 'isnot' );
14
15
	var $is_sortable = true;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_sortable.

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...
16
17
	var $is_numeric = true;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_numeric.

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