Completed
Push — master ( ebef7a...94d09a )
by Zack
20:55 queued 16:56
created

GravityView_Settings::add_network_menu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
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 11 and the first side effect is on line 4.

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
if( ! class_exists('GFAddOn') ) {
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...
4
	return;
5
}
6
7
/**
8
 * GravityView Settings class (get/set/license validation) using the Gravity Forms App framework
9
 * @since 1.7.4 (Before, used the Redux Framework)
10
 */
11
class GravityView_Settings extends GFAddOn {
12
13
	/**
14
	 * @var string Version number of the Add-On
15
	 */
16
	protected $_version = GravityView_Plugin::version;
17
	/**
18
	 * @var string Gravity Forms minimum version requirement
19
	 */
20
	protected $_min_gravityforms_version = GV_MIN_GF_VERSION;
21
22
	/**
23
	 * @var string Title of the plugin to be used on the settings page, form settings and plugins page. Example: 'Gravity Forms MailChimp Add-On'
24
	 */
25
	protected $_title = 'GravityView';
26
27
	/**
28
	 * @var string Short version of the plugin title to be used on menus and other places where a less verbose string is useful. Example: 'MailChimp'
29
	 */
30
	protected  $_short_title = 'GravityView';
31
32
	/**
33
	 * @var string URL-friendly identifier used for form settings, add-on settings, text domain localization...
34
	 */
35
	protected $_slug = 'gravityview';
36
37
	/**
38
	 * @var string|array A string or an array of capabilities or roles that can uninstall the plugin
39
	 */
40
	protected $_capabilities_uninstall = 'gravityview_uninstall';
41
42
	/**
43
	 * @var string|array A string or an array of capabilities or roles that have access to the settings page
44
	 */
45
	protected $_capabilities_app_settings = 'gravityview_view_settings';
46
47
	/**
48
	 * @var string|array A string or an array of capabilities or roles that have access to the settings page
49
	 */
50
	protected $_capabilities_app_menu = 'gravityview_view_settings';
51
52
	/**
53
	 * @var string The hook suffix for the app menu
54
	 */
55
	public  $app_hook_suffix = 'gravityview';
56
57
	/**
58
	 * @var GV_License_Handler Process license validation
59
	 */
60
	private $License_Handler;
61
62
	/**
63
	 * @var GravityView_Settings
64
	 */
65
	private static $instance;
66
67
	/**
68
	 * We're not able to set the __construct() method to private because we're extending the GFAddon class, so
69
	 * we fake it. When called using `new GravityView_Settings`, it will return get_instance() instead. We pass
70
	 * 'get_instance' as a test string.
71
	 *
72
	 * @see get_instance()
73
	 *
74
	 * @param string $prevent_multiple_instances
75
	 */
76
	public function __construct( $prevent_multiple_instances = '' ) {
77
78
		if( $prevent_multiple_instances === 'get_instance' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
79
			return parent::__construct();
80
		}
81
82
		return self::get_instance();
83
	}
84
85
	/**
86
	 * @return GravityView_Settings
87
	 */
88
	public static function get_instance() {
89
90
		if( empty( self::$instance ) ) {
91
			self::$instance = new self( 'get_instance' );
92
		}
93
94
		return self::$instance;
95
	}
96
97
	/**
98
	 * Prevent uninstall tab from being shown by returning false for the uninstall capability check. Otherwise:
99
	 * @inheritDoc
100
	 *
101
	 * @hack
102
	 *
103
	 * @param array|string $caps
104
	 *
105
	 * @return bool
106
	 */
107
	public function current_user_can_any( $caps ) {
108
109
		/**
110
		 * Prevent Gravity Forms from showing the uninstall tab on the settings page
111
		 * @hack
112
		 */
113
		if( $caps === $this->_capabilities_uninstall ) {
114
			return false;
115
		}
116
117
		if( empty( $caps ) ) {
118
			$caps = array( 'gravityview_full_access' );
119
		}
120
121
		return GVCommon::has_cap( $caps );
122
	}
123
124
	/**
125
	 * Run actions when initializing admin
126
	 *
127
	 * Triggers the license key notice
128
	 *
129
	 * @return void
130
	 */
131
	function init_admin() {
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...
132
133
		$this->_load_license_handler();
134
135
		$this->license_key_notice();
136
137
		add_filter( 'gform_addon_app_settings_menu_gravityview', array( $this, 'modify_app_settings_menu_title' ) );
138
139
		/** @since 1.7.6 */
140
		add_action('network_admin_menu', array( $this, 'add_network_menu' ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
141
142
		parent::init_admin();
143
	}
144
145
	/**
146
	 * Change the settings page header title to "GravityView"
147
	 *
148
	 * @param $setting_tabs
149
	 *
150
	 * @return array
151
	 */
152
	public function modify_app_settings_menu_title( $setting_tabs ) {
153
154
		$setting_tabs[0]['label'] = __( 'GravityView Settings', 'gravityview');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
155
156
		return $setting_tabs;
157
	}
158
159
	/**
160
	 * Load license handler in admin-ajax.php
161
	 */
162
	public function init_ajax() {
163
		$this->_load_license_handler();
164
	}
165
166
	/**
167
	 * Make sure the license handler is available
168
	 */
169
	private function _load_license_handler() {
170
171
		if( !empty( $this->License_Handler ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
172
			return;
173
		}
174
175
		require_once( GRAVITYVIEW_DIR . 'includes/class-gv-license-handler.php');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
176
177
		$this->License_Handler = GV_License_Handler::get_instance( $this );
178
	}
179
180
	/**
181
	 * Display a notice if the plugin is inactive.
182
	 * @return void
183
	 */
184
	function license_key_notice() {
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...
185
186
		// Only show on GravityView pages
187
		if( ! gravityview_is_admin_page() ) {
188
			return;
189
		}
190
191
		$license_status = self::getSetting('license_key_status');
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...
192
		$license_id = self::getSetting('license_key');
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...
193
		$license_id = empty( $license_id ) ? 'license' : $license_id;
194
195
		$message = esc_html__('Your GravityView license %s. This means you&rsquo;re missing out on updates and support! %sActivate your license%s or %sget a license here%s.', '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...
196
197
		/**
198
		 * I wanted to remove the period from after the buttons in the string,
199
		 * but didn't want to mess up the translation strings for the translators.
200
		 */
201
		$message = mb_substr( $message, 0, mb_strlen( $message ) - 1 );
202
		$title = __('Inactive License', '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...
203
		$status = '';
204
		$update_below = false;
205
		$primary_button_link = admin_url( 'edit.php?post_type=gravityview&amp;page=gravityview_settings' );
206
		switch ( $license_status ) {
207
			/** @since 1.17 */
208
			case 'expired':
209
				$title = __('Expired License', '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...
210
				$status = 'expired';
211
				$message = $this->get_license_handler()->strings( 'expired', self::getSetting('license_key_response') );
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...
212
				break;
213
			case 'invalid':
214
				$title = __('Invalid License', '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...
215
				$status = __('is invalid', '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...
216
				break;
217
			case 'deactivated':
218
				$status = __('is inactive', '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...
219
				$update_below = __('Activate your license key below.', '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...
220
				break;
221
			/** @noinspection PhpMissingBreakStatementInspection */
222
			case '':
223
				$license_status = 'site_inactive';
224
				// break intentionally left blank
225
			case 'inactive':
226
			case 'site_inactive':
227
				$status = __('has not been activated', '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...
228
				$update_below = __('Activate your license key below.', '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...
229
				break;
230
		}
231
		$url = 'https://gravityview.co/pricing/?utm_source=admin_notice&utm_medium=admin&utm_content='.$license_status.'&utm_campaign=Admin%20Notice';
232
233
		// Show a different notice on settings page for inactive licenses (hide the buttons)
234
		if( $update_below && gravityview_is_admin_page( '', 'settings' ) ) {
235
			$message = sprintf( $message, $status, '<div class="hidden">', '', '', '</div><a href="#" onclick="jQuery(\'#license_key\').focus(); return false;">' . $update_below . '</a>' );
236
		} else {
237
			$message = sprintf( $message, $status, "\n\n" . '<a href="' . esc_url( $primary_button_link ) . '" class="button button-primary">', '</a>', '<a href="' . esc_url( $url ) . '" class="button button-secondary">', '</a>' );
238
		}
239
240
		if( !empty( $status ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
241
			GravityView_Admin_Notices::add_notice( array(
242
				'message' => $message,
243
				'class'	=> 'updated',
244
				'title' => $title,
245
				'cap' => 'gravityview_edit_settings',
246
				'dismiss' => sha1( $license_status.'_'.$license_id ),
247
			));
248
		}
249
	}
250
251
	/**
252
	 * Register styles in the app admin page
253
	 * @return array
254
	 */
255
	public function styles() {
256
257
		$styles = parent::styles();
258
259
		$styles[] = array(
260
			'handle'  => 'gravityview_settings',
261
			'src'     => plugins_url( 'assets/css/admin-settings.css', GRAVITYVIEW_FILE ),
262
			'version' => GravityView_Plugin::version,
263
			"deps" => array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal deps does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
264
				'gaddon_form_settings_css'
265
			),
266
			'enqueue' => array(
267
				array( 'admin_page' => array(
0 ignored issues
show
introduced by
The first index in a multi-value array must be on a new line
Loading history...
introduced by
The first value in a multi-value array must be on a new line
Loading history...
268
					'app_settings'
269
				) ),
270
			)
271
		);
272
273
		return $styles;
274
	}
275
276
	/**
277
	 * Add global Settings page for Multisite
278
	 * @since 1.7.6
279
	 * @return void
280
	 */
281
	public function add_network_menu() {
282
		if( GravityView_Plugin::is_network_activated() ) {
283
			add_menu_page( __( 'Settings', 'gravityview' ), __( 'GravityView', 'gravityview' ), $this->_capabilities_app_settings, "{$this->_slug}_settings", array( $this, 'app_tab_page' ), 'none' );
284
		}
285
	}
286
287
	/**
288
	 * Add Settings link to GravityView menu
289
	 * @return void
290
	 */
291
	public function create_app_menu() {
292
293
		/**
294
		 * If not multisite, always show.
295
		 * If multisite and the plugin is network activated, show; we need to register the submenu page for the Network Admin settings to work.
296
		 * If multisite and not network admin, we don't want the settings to show.
297
		 * @since 1.7.6
298
		 */
299
		$show_submenu = !is_multisite() ||  is_main_site() || !GravityView_Plugin::is_network_activated() || ( is_network_admin() && GravityView_Plugin::is_network_activated() );
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
300
301
		/**
302
		 * Override whether to show the Settings menu on a per-blog basis.
303
		 * @since 1.7.6
304
		 * @param bool $hide_if_network_activated Default: true
305
		 */
306
		$show_submenu = apply_filters( 'gravityview/show-settings-menu', $show_submenu );
307
308
		if( $show_submenu ) {
309
			add_submenu_page( 'edit.php?post_type=gravityview', __( 'Settings', 'gravityview' ), __( 'Settings', 'gravityview' ), $this->_capabilities_app_settings, $this->_slug . '_settings', array( $this, 'app_tab_page' ) );
310
		}
311
	}
312
313
	/**
314
	 * The Settings title
315
	 * @return string
316
	 */
317
	public function app_settings_title() {
318
		return null;
319
	}
320
321
	/**
322
	 * Prevent displaying of any icon
323
	 * @return string
324
	 */
325
	public function app_settings_icon() {
326
		return '<i></i>';
327
	}
328
329
	/**
330
	 * Make protected public
331
	 * @inheritDoc
332
	 * @access public
333
	 */
334
	public function get_app_setting( $setting_name ) {
335
336
		/**
337
		 * Backward compatibility with Redux
338
		 */
339
		if( $setting_name === 'license' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
340
			return array(
341
				'license' => parent::get_app_setting( 'license_key' ),
342
				'status' => parent::get_app_setting( 'license_key_status' ),
343
				'response' => parent::get_app_setting( 'license_key_response' ),
344
			);
345
		}
346
347
		return parent::get_app_setting( $setting_name );
348
	}
349
350
	/**
351
	 * Returns the currently saved plugin settings
352
	 *
353
	 * Different from GFAddon in two ways:
354
	 * 1. Makes protected method public
355
	 * 2. Use default settings if the original settings don't exist
356
	 *
357
	 * @access public
358
	 *
359
	 * @return array
360
	 */
361
	public function get_app_settings() {
362
		return get_option( 'gravityformsaddon_' . $this->_slug . '_app_settings', $this->get_default_settings() );
363
	}
364
365
366
	/**
367
	 * Updates app settings with the provided settings
368
	 *
369
	 * Same as the GVAddon, except it returns the value from update_option()
370
	 *
371
	 * @param array $settings - App settings to be saved
372
	 *
373
	 * @return boolean False if value was not updated and true if value was updated.
374
	 */
375
	public function update_app_settings( $settings ) {
376
		return update_option( 'gravityformsaddon_' . $this->_slug . '_app_settings', $settings );
377
	}
378
379
	/**
380
	 * Make protected public
381
	 * @inheritDoc
382
	 * @access public
383
	 */
384
	public function set_field_error( $field, $error_message = '' ) {
385
		parent::set_field_error( $field, $error_message );
386
	}
387
388
	/**
389
	 * Register the settings field for the EDD License field type
390
	 * @param array $field
391
	 * @param bool $echo Whether to echo the
392
	 *
393
	 * @return string
394
	 */
395
	protected function settings_edd_license( $field, $echo = true ) {
396
397
		$text = self::settings_text( $field, false );
398
399
		$activation = $this->License_Handler->settings_edd_license_activation( $field, false );
400
401
		$return = $text . $activation;
402
403
		if( $echo ) {
404
			echo $return;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$return'
Loading history...
405
		}
406
407
		return $return;
408
	}
409
410
	/**
411
	 * Allow public access to the GV_License_Handler class
412
	 * @since 1.7.4
413
	 *
414
	 * @return GV_License_Handler
415
	 */
416
	public function get_license_handler() {
417
		return $this->License_Handler;
418
	}
419
420
	/***
421
	 * Renders the save button for settings pages
422
	 *
423
	 * @param array $field - Field array containing the configuration options of this field
424
	 * @param bool  $echo  = true - true to echo the output to the screen, false to simply return the contents as a string
425
	 *
426
	 * @return string The HTML
427
	 */
428
	public function settings_submit( $field, $echo = true ) {
429
430
		$field['type']  = ( isset($field['type']) && in_array( $field['type'], array('submit','reset','button') ) ) ? $field['type'] : 'submit';
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...
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
Expected 1 space between comma and "'reset'"; 0 found
Loading history...
introduced by
Expected 1 space between comma and "'button'"; 0 found
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
431
432
		$attributes    = $this->get_field_attributes( $field );
433
		$default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
434
		$value         = $this->get_setting( $field['name'], $default_value );
435
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
436
437
		$attributes['class'] = isset( $attributes['class'] ) ? esc_attr( $attributes['class'] ) : 'button-primary gfbutton';
438
		$name    = ( $field['name'] === 'gform-settings-save' ) ? $field['name'] : '_gaddon_setting_'.$field['name'];
439
440
		if ( empty( $value ) ) {
441
			$value = __( 'Update Settings', 'gravityview' );
442
		}
443
444
		$attributes = $this->get_field_attributes( $field );
445
446
		$html = '<input
447
                    type="' . $field['type'] . '"
448
                    name="' . esc_attr( $name ) . '"
449
                    value="' . $value . '" ' .
450
		        implode( ' ', $attributes ) .
451
		        ' />';
452
453
		if ( $echo ) {
454
			echo $html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$html'
Loading history...
455
		}
456
457
		return $html;
458
	}
459
460
	/**
461
	 * Allow customizing the Save field parameters
462
	 *
463
	 * @param array $field
464
	 * @param bool $echo
465
	 *
466
	 * @return string
467
	 */
468
	public function settings_save( $field, $echo = true ) {
469
		$field['type']  = 'submit';
470
		$field['name']  = 'gform-settings-save';
471
		$field['class'] = isset( $field['class'] ) ? $field['class'] : 'button-primary gfbutton';
472
473
		if ( ! rgar( $field, 'value' ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
474
			$field['value'] = __( 'Update Settings', 'gravityview' );
475
476
		$output = $this->settings_submit( $field, false );
477
478
		if( $echo ) {
479
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
480
		}
481
482
		return $output;
483
	}
484
485
	/**
486
	 * The same as the parent, except added support for field descriptions
487
	 * @inheritDoc
488
	 * @param $field array
489
	 */
490
	public function single_setting_label( $field ) {
491
492
		parent::single_setting_label( $field );
493
494
		// Added by GravityView
495
		if ( isset( $field['description'] ) ) {
496
			echo '<span class="description">'. $field['description'] .'</span>';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$field'
Loading history...
497
		}
498
499
	}
500
501
	/**
502
	 * Get the default settings for the plugin
503
	 *
504
	 * Merges previous settings created when using the Redux Framework
505
	 *
506
	 * @return array Settings with defaults set
507
	 */
508
	private function get_default_settings() {
509
510
		$defaults = array(
511
			// Set the default license in wp-config.php
512
			'license_key' => defined( 'GRAVITYVIEW_LICENSE_KEY' ) ? GRAVITYVIEW_LICENSE_KEY : '',
513
			'license_key_response' => '',
514
			'license_key_status' => '',
515
			'support-email' => get_bloginfo( 'admin_email' ),
516
			'no-conflict-mode' => '0',
517
			'support_port' => '1',
518
			'delete-on-uninstall' => '0',
519
			'flexbox_search' => '1',
520
		);
521
522
		return $defaults;
523
	}
524
525
	/**
526
	 * Check for the `gravityview_edit_settings` capability before saving plugin settings.
527
	 * Gravity Forms says you're able to edit if you're able to view settings. GravityView allows two different permissions.
528
	 *
529
	 * @since 1.15
530
	 * @return void
531
	 */
532
	public function maybe_save_app_settings() {
533
534
		if ( $this->is_save_postback() ) {
535
			if ( ! GVCommon::has_cap( 'gravityview_edit_settings' ) ) {
536
				$_POST = array(); // If you don't reset the $_POST array, it *looks* like the settings were changed, but they weren't
537
				GFCommon::add_error_message( __( 'You don\'t have the ability to edit plugin settings.', 'gravityview' ) );
538
				return;
539
			}
540
		}
541
542
		parent::maybe_save_app_settings();
543
	}
544
545
	/**
546
	 * When the settings are saved, make sure the license key matches the previously activated key
547
	 *
548
	 * @return array settings from parent::get_posted_settings(), with `license_key_response` and `license_key_status` potentially unset
549
	 */
550
	public function get_posted_settings() {
551
552
		$posted_settings = parent::get_posted_settings();
553
554
		$local_key = rgar( $posted_settings, 'license_key' );
555
		$response_key = rgars( $posted_settings, 'license_key_response/license_key' );
556
557
		// If the posted key doesn't match the activated/deactivated key (set using the Activate License button, AJAX response),
558
		// then we assume it's changed. If it's changed, unset the status and the previous response.
559
		if( $local_key !== $response_key ) {
560
561
			unset( $posted_settings['license_key_response'] );
562
			unset( $posted_settings['license_key_status'] );
563
			GFCommon::add_error_message( __('The license key you entered has been saved, but not activated. Please activate the license.', 'gravityview' ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
564
		}
565
566
		return $posted_settings;
567
	}
568
569
	/**
570
	 * Gets the required indicator
571
	 * Gets the markup of the required indicator symbol to highlight fields that are required
572
	 *
573
	 * @param $field - The field meta.
574
	 *
575
	 * @return string - Returns markup of the required indicator symbol
576
	 */
577
	public function get_required_indicator( $field ) {
578
		return '<span class="required" title="' . esc_attr__( 'Required', 'gravityview' ) . '">*</span>';
579
	}
580
581
	/**
582
	 * Specify the settings fields to be rendered on the plugin settings page
583
	 * @return array
584
	 */
585
	public function app_settings_fields() {
586
587
		$default_settings = $this->get_default_settings();
588
589
		$disabled_attribute = GVCommon::has_cap( 'gravityview_edit_settings' ) ? false : 'disabled';
590
591
		$fields = apply_filters( 'gravityview_settings_fields', array(
592
			array(
593
				'name'                => 'license_key',
594
				'required'               => true,
595
				'label'             => __( 'License Key', 'gravityview' ),
596
				'description'          => __( 'Enter the license key that was sent to you on purchase. This enables plugin updates &amp; support.', 'gravityview' ) . $this->get_license_handler()->license_details( $this->get_app_setting( 'license_key_response' ) ),
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
597
				'type'              => 'edd_license',
598
				'data-pending-text' => __('Verifying license&hellip;', '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...
599
				'default_value'           => $default_settings['license_key'],
600
				'class'             => ( '' == $this->get_app_setting( 'license_key' ) ) ? 'activate code regular-text edd-license-key' : 'deactivate code regular-text edd-license-key',
601
			),
602
			array(
603
				'name'       => 'license_key_response',
604
				'default_value'  => $default_settings['license_key_response'],
605
				'type'     => 'hidden',
606
			),
607
			array(
608
				'name'       => 'license_key_status',
609
				'default_value'  => $default_settings['license_key_status'],
610
				'type'     => 'hidden',
611
			),
612
			array(
613
				'name'       => 'support-email',
614
				'type'     => 'text',
615
				'validate' => 'email',
616
				'default_value'  => $default_settings['support-email'],
617
				'label'    => __( 'Support Email', 'gravityview' ),
618
				'description' => __( 'In order to provide responses to your support requests, please provide your email address.', 'gravityview' ),
619
				'class'    => 'code regular-text',
620
			),
621
			/**
622
			 * @since 1.15 Added Support Port support
623
			 */
624
			array(
625
				'name'         => 'support_port',
626
				'type'       => 'radio',
627
				'label'      => __( 'Show Support Port?', 'gravityview' ),
628
				'default_value'    => $default_settings['support_port'],
629
				'horizontal' => 1,
630
				'choices'    => array(
631
					array(
632
						'label' => _x('Show', 'Setting: Show or Hide', '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...
633
						'value' => '1',
634
					),
635
					array(
636
						'label' => _x('Hide', 'Setting: Show or Hide', '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...
637
						'value' => '0',
638
					),
639
				),
640
				'tooltip' => '<p><img src="' . esc_url_raw( plugins_url('assets/images/screenshots/beacon.png', GRAVITYVIEW_FILE ) ) . '" alt="' . esc_attr__( 'The Support Port looks like this.', 'gravityview' ) . '" class="alignright" style="max-width:40px; margin:.5em;" />' . esc_html__('The Support Port provides quick access to how-to articles and tutorials. For administrators, it also makes it easy to contact support.', 'gravityview') . '</p>',
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...
641
				'description'   => __( 'Show the Support Port on GravityView pages?', 'gravityview' ),
642
			),
643
			array(
644
				'name'         => 'no-conflict-mode',
645
				'type'       => 'radio',
646
				'label'      => __( 'No-Conflict Mode', 'gravityview' ),
647
				'default_value'    => $default_settings['no-conflict-mode'],
648
				'horizontal' => 1,
649
				'choices'    => array(
650
					array(
651
						'label' => _x('On', 'Setting: On or off', '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...
652
						'value' => '1',
653
					),
654
					array(
655
						'label' => _x('Off', 'Setting: On or off', '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...
656
						'value' => '0',
657
					),
658
				),
659
				'description'   => __( 'Set this to ON to prevent extraneous scripts and styles from being printed on GravityView admin pages, reducing conflicts with other plugins and themes.', 'gravityview' ) . ' ' . __('If your Edit View tabs are ugly, enable this setting.', 'gravityview'),
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
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...
660
			),
661
			array(
662
				'name'       => 'delete-on-uninstall',
663
				'type'       => 'radio',
664
				'label'      => __( 'Remove Data on Delete?', 'gravityview' ),
665
				'default_value'    => $default_settings['delete-on-uninstall'],
666
				'horizontal' => 1,
667
				'choices'    => array(
668
					array(
669
						'label' => _x( 'Keep GravityView Data', 'Setting: what to do when uninstalling plugin', 'gravityview' ),
670
						'value' => '0',
671
						'tooltip' => sprintf( '<h6>%s</h6><p>%s</p>', __( 'Keep GravityView content and settings', 'gravityview' ), __( 'If you delete then re-install the plugin, all GravityView data will be kept. Views, settings, etc. will be untouched.', 'gravityview' ) ),
672
					),
673
					array(
674
						'label' => _x( 'Permanently Delete', 'Setting: what to do when uninstalling plugin', 'gravityview' ),
675
						'value' => 'delete',
676
					    'tooltip' => sprintf( '<h6>%s</h6><p><span class="howto">%s</span></p><p>%s</p>', __( 'Delete all GravityView content and settings', 'gravityview' ), __( 'If you delete then re-install GravityView, it will be like installing GravityView for the first time.', 'gravityview' ), __( 'When GravityView is uninstalled and deleted, delete all Views, GravityView entry approvals, GravityView-generated entry notes (including approval and entry creator changes), and GravityView plugin settings. No Gravity Forms data will be touched.', 'gravityview' ) ),
677
					),
678
				),
679
				'description'   => sprintf( __( 'Should GravityView content and entry approval status be removed from the site when the GravityView plugin is deleted?', 'gravityview' ), __( 'Permanently Delete', 'gravityview' ) ),
680
			),
681
682
		) );
683
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
684
685
686
		/**
687
		 * Redux backward compatibility
688
		 * @since 1.7.4
689
		 */
690
		foreach ( $fields as &$field ) {
691
			$field['name']          = isset( $field['name'] ) ? $field['name'] : rgget('id', $field );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
692
			$field['label']         = isset( $field['label'] ) ? $field['label'] : rgget('title', $field );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
693
			$field['default_value'] = isset( $field['default_value'] ) ? $field['default_value'] : rgget('default', $field );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
694
			$field['description']   = isset( $field['description'] ) ? $field['description'] : rgget('subtitle', $field );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
695
696
			if( $disabled_attribute ) {
697
				$field['disabled']  = $disabled_attribute;
698
			}
699
		}
700
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
701
702
        $sections = array(
703
            array(
704
                'description' =>      sprintf( '<span class="version-info description">%s</span>', sprintf( __('You are running GravityView version %s', 'gravityview'), GravityView_Plugin::version ) ),
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 6 found
Loading history...
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...
705
                'fields'      => $fields,
706
            )
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
707
        );
708
709
        // custom 'update settings' button
710
        $button = array(
711
            'class' => 'button button-primary button-hero',
712
            'type'     => 'save',
713
        );
714
715
		if( $disabled_attribute ) {
716
			$button['disabled'] = $disabled_attribute;
717
		}
718
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
719
720
        /**
721
         * @filter `gravityview/settings/extension/sections` Modify the GravityView settings page
722
         * Extensions can tap in here to insert their own section and settings.
723
         * <code>
724
         *   $sections[] = array(
725
         *      'title' => __( 'GravityView My Extension Settings', 'gravityview' ),
726
         *      'fields' => $settings,
727
         *   );
728
         * </code>
729
         * @param array $extension_settings Empty array, ready for extension settings!
730
         */
731
        $extension_sections = apply_filters( 'gravityview/settings/extension/sections', array() );
732
733
		// If there are extensions, add a section for them
734
		if ( ! empty( $extension_sections ) ) {
735
736
			if( $disabled_attribute ) {
737
				foreach ( $extension_sections as &$section ) {
738
					foreach ( $section['fields'] as &$field ) {
739
						$field['disabled'] = $disabled_attribute;
740
					}
741
				}
742
			}
743
744
            $k = count( $extension_sections ) - 1 ;
745
            $extension_sections[ $k ]['fields'][] = $button;
746
			$sections = array_merge( $sections, $extension_sections );
747
		} else {
748
            // add the 'update settings' button to the general section
749
            $sections[0]['fields'][] = $button;
750
        }
751
752
		return $sections;
753
	}
754
755
	/**
756
	 * Get the setting for GravityView by name
757
	 *
758
	 * @param  string $key     Option key to fetch
759
	 *
760
	 * @return mixed
761
	 */
762
	static public function getSetting( $key ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
Coding Style introduced by
The function name getSetting is in camel caps, but expected get_setting instead as per the coding standard.
Loading history...
763
		return self::get_instance()->get_app_setting( $key );
764
	}
765
766
}
767
768
GravityView_Settings::get_instance();