Completed
Push — master ( 029c15...1ca2d3 )
by Zack
47:28 queued 36:10
created

GravityView_Settings::get_default_settings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 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 756.

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
 * GravityView Settings class (get/set/license validation) using the Gravity Forms App framework
5
 * @since 1.7.4 (Before, used the Redux Framework)
6
 */
7
class GravityView_Settings extends GFAddOn {
8
9
	/**
10
	 * @var string Version number of the Add-On
11
	 */
12
	protected $_version = GravityView_Plugin::version;
13
	/**
14
	 * @var string Gravity Forms minimum version requirement
15
	 */
16
	protected $_min_gravityforms_version = GV_MIN_GF_VERSION;
17
18
	/**
19
	 * @var string Title of the plugin to be used on the settings page, form settings and plugins page. Example: 'Gravity Forms MailChimp Add-On'
20
	 */
21
	protected $_title = 'GravityView';
22
23
	/**
24
	 * @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'
25
	 */
26
	protected  $_short_title = 'GravityView';
27
28
	/**
29
	 * @var string URL-friendly identifier used for form settings, add-on settings, text domain localization...
30
	 */
31
	protected $_slug = 'gravityview';
32
33
	/**
34
	 * @var string|array A string or an array of capabilities or roles that can uninstall the plugin
35
	 */
36
	protected $_capabilities_uninstall = 'gravityview_uninstall';
37
38
	/**
39
	 * @var string|array A string or an array of capabilities or roles that have access to the settings page
40
	 */
41
	protected $_capabilities_app_settings = 'gravityview_view_settings';
42
43
	/**
44
	 * @var string|array A string or an array of capabilities or roles that have access to the settings page
45
	 */
46
	protected $_capabilities_app_menu = 'gravityview_view_settings';
47
48
	/**
49
	 * @var string The hook suffix for the app menu
50
	 */
51
	public  $app_hook_suffix = 'gravityview';
52
53
	/**
54
	 * @var GV_License_Handler Process license validation
55
	 */
56
	private $License_Handler;
57
58
	/**
59
	 * @var GravityView_Settings
60
	 */
61
	private static $instance;
62
63
	/**
64
	 * We're not able to set the __construct() method to private because we're extending the GFAddon class, so
65
	 * we fake it. When called using `new GravityView_Settings`, it will return get_instance() instead. We pass
66
	 * 'get_instance' as a test string.
67
	 *
68
	 * @see get_instance()
69
	 *
70
	 * @param string $prevent_multiple_instances
71
	 */
72
	public function __construct( $prevent_multiple_instances = '' ) {
73
74
		if( $prevent_multiple_instances === 'get_instance' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
introduced by
Space after opening control structure is required
Loading history...
75
			return parent::__construct();
76
		}
77
78
		return self::get_instance();
79
	}
80
81
	/**
82
	 * @return GravityView_Settings
83
	 */
84
	public static function get_instance() {
85
86
		if( empty( self::$instance ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
87
			self::$instance = new self( 'get_instance' );
88
		}
89
90
		return self::$instance;
91
	}
92
93
	/**
94
	 * Prevent uninstall tab from being shown by returning false for the uninstall capability check. Otherwise:
95
	 * @inheritDoc
96
	 *
97
	 * @hack
98
	 *
99
	 * @param array|string $caps
100
	 *
101
	 * @return bool
102
	 */
103
	public function current_user_can_any( $caps ) {
104
105
		/**
106
		 * Prevent Gravity Forms from showing the uninstall tab on the settings page
107
		 * @hack
108
		 */
109
		if( $caps === $this->_capabilities_uninstall ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
110
			return false;
111
		}
112
113
		if( empty( $caps ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
114
			$caps = array( 'gravityview_full_access' );
115
		}
116
117
		return GVCommon::has_cap( $caps );
118
	}
119
120
	/**
121
	 * Run actions when initializing admin
122
	 *
123
	 * Triggers the license key notice
124
	 *
125
	 * @return void
126
	 */
127
	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...
128
129
		$this->_load_license_handler();
130
131
		$this->license_key_notice();
132
133
		add_filter( 'gform_addon_app_settings_menu_gravityview', array( $this, 'modify_app_settings_menu_title' ) );
134
135
		/** @since 1.7.6 */
136
		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...
137
138
		parent::init_admin();
139
	}
140
141
	/**
142
	 * Change the settings page header title to "GravityView"
143
	 *
144
	 * @param $setting_tabs
145
	 *
146
	 * @return array
147
	 */
148
	public function modify_app_settings_menu_title( $setting_tabs ) {
149
150
		$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...
151
152
		return $setting_tabs;
153
	}
154
155
	/**
156
	 * Load license handler in admin-ajax.php
157
	 */
158
	public function init_ajax() {
159
		$this->_load_license_handler();
160
	}
161
162
	/**
163
	 * Make sure the license handler is available
164
	 */
165
	private function _load_license_handler() {
166
167
		if( !empty( $this->License_Handler ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
168
			return;
169
		}
170
171
		require_once( GRAVITYVIEW_DIR . 'includes/class-gv-license-handler.php');
172
173
		$this->License_Handler = GV_License_Handler::get_instance( $this );
174
	}
175
176
	/**
177
	 * Display a notice if the plugin is inactive.
178
	 * @return void
179
	 */
180
	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...
181
182
		// Only show on GravityView pages
183
		if( ! gravityview_is_admin_page() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
184
			return;
185
		}
186
187
		$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...
188
		$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...
189
		$license_id = empty( $license_id ) ? 'license' : $license_id;
190
191
		$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...
192
193
		/**
194
		 * I wanted to remove the period from after the buttons in the string,
195
		 * but didn't want to mess up the translation strings for the translators.
196
		 */
197
		$message = mb_substr( $message, 0, mb_strlen( $message ) - 1 );
198
		$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...
199
		$status = '';
200
		$update_below = false;
201
		$primary_button_link = admin_url( 'edit.php?post_type=gravityview&amp;page=gravityview_settings' );
202
		switch ( $license_status ) {
203
			case 'invalid':
204
				$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...
205
				$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...
206
				break;
207
			case 'deactivated':
208
				$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...
209
				$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...
210
				break;
211
			/** @noinspection PhpMissingBreakStatementInspection */
212
			case '':
213
				$license_status = 'site_inactive';
214
				// break intentionally left blank
215
			case 'site_inactive':
216
				$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...
217
				$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...
218
				break;
219
		}
220
		$url = 'https://gravityview.co/pricing/?utm_source=admin_notice&utm_medium=admin&utm_content='.$license_status.'&utm_campaign=Admin%20Notice';
221
222
		// Show a different notice on settings page for inactive licenses (hide the buttons)
223
		if( $update_below && gravityview_is_admin_page( '', 'settings' ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
224
			$message = sprintf( $message, $status, '<div class="hidden">', '', '', '</div><a href="#" onclick="jQuery(\'#license_key\').focus(); return false;">' . $update_below . '</a>' );
225
		} else {
226
			$message = sprintf( $message, $status, "\n\n" . '<a href="' . $primary_button_link . '" class="button button-primary">', '</a>', '<a href="' . esc_url( $url ) . '" class="button button-secondary">', '</a>' );
227
		}
228
229
		if( !empty( $status ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
230
			GravityView_Admin_Notices::add_notice( array(
231
				'message' => $message,
232
				'class'	=> 'updated',
233
				'title' => $title,
234
				'cap' => 'gravityview_edit_settings',
235
				'dismiss' => sha1( $license_status.'_'.$license_id ),
236
			));
237
		}
238
	}
239
240
	/**
241
	 * Register styles in the app admin page
242
	 * @return array
243
	 */
244
	public function styles() {
245
246
		$styles = parent::styles();
247
248
		$styles[] = array(
249
			'handle'  => 'gravityview_settings',
250
			'src'     => plugins_url( 'assets/css/admin-settings.css', GRAVITYVIEW_FILE ),
251
			'version' => GravityView_Plugin::version,
252
			"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...
253
				'gaddon_form_settings_css'
254
			),
255
			'enqueue' => array(
256
				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...
257
					'app_settings'
258
				) ),
259
			)
260
		);
261
262
		return $styles;
263
	}
264
265
	/**
266
	 * Add global Settings page for Multisite
267
	 * @since 1.7.6
268
	 * @return void
269
	 */
270
	public function add_network_menu() {
271
		if( GravityView_Plugin::is_network_activated() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
272
			add_menu_page( __( 'Settings', 'gravityview' ), __( 'GravityView', 'gravityview' ), $this->_capabilities_app_settings, "{$this->_slug}_settings", array( $this, 'app_tab_page' ), 'none' );
273
		}
274
	}
275
276
	/**
277
	 * Add Settings link to GravityView menu
278
	 * @return void
279
	 */
280
	public function create_app_menu() {
281
282
		/**
283
		 * If not multisite, always show.
284
		 * If multisite and the plugin is network activated, show; we need to register the submenu page for the Network Admin settings to work.
285
		 * If multisite and not network admin, we don't want the settings to show.
286
		 * @since 1.7.6
287
		 */
288
		$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...
289
290
		/**
291
		 * Override whether to show the Settings menu on a per-blog basis.
292
		 * @since 1.7.6
293
		 * @param bool $hide_if_network_activated Default: true
294
		 */
295
		$show_submenu = apply_filters( 'gravityview/show-settings-menu', $show_submenu );
296
297
		if( $show_submenu ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
298
			add_submenu_page( 'edit.php?post_type=gravityview', __( 'Settings', 'gravityview' ), __( 'Settings', 'gravityview' ), $this->_capabilities_app_settings, $this->_slug . '_settings', array( $this, 'app_tab_page' ) );
299
		}
300
	}
301
302
	/**
303
	 * The Settings title
304
	 * @return string
305
	 */
306
	public function app_settings_title() {
307
		return null;
308
	}
309
310
	/**
311
	 * Prevent displaying of any icon
312
	 * @return string
313
	 */
314
	public function app_settings_icon() {
315
		return '<i></i>';
316
	}
317
318
	/**
319
	 * Make protected public
320
	 * @inheritDoc
321
	 * @access public
322
	 */
323
	public function get_app_setting( $setting_name ) {
324
325
		/**
326
		 * Backward compatibility with Redux
327
		 */
328
		if( $setting_name === 'license' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
introduced by
Space after opening control structure is required
Loading history...
329
			return array(
330
				'license' => parent::get_app_setting( 'license_key' ),
331
				'status' => parent::get_app_setting( 'license_key_status' ),
332
				'response' => parent::get_app_setting( 'license_key_response' ),
333
			);
334
		}
335
336
		return parent::get_app_setting( $setting_name );
337
	}
338
339
	/**
340
	 * Returns the currently saved plugin settings
341
	 *
342
	 * Different from GFAddon in two ways:
343
	 * 1. Makes protected method public
344
	 * 2. Use default settings if the original settings don't exist
345
	 *
346
	 * @access public
347
	 *
348
	 * @return array
349
	 */
350
	public function get_app_settings() {
351
		return get_option( 'gravityformsaddon_' . $this->_slug . '_app_settings', $this->get_default_settings() );
352
	}
353
354
355
	/**
356
	 * Updates app settings with the provided settings
357
	 *
358
	 * Same as the GVAddon, except it returns the value from update_option()
359
	 *
360
	 * @param array $settings - App settings to be saved
361
	 *
362
	 * @return boolean False if value was not updated and true if value was updated.
363
	 */
364
	public function update_app_settings( $settings ) {
365
		return update_option( 'gravityformsaddon_' . $this->_slug . '_app_settings', $settings );
366
	}
367
368
	/**
369
	 * Make protected public
370
	 * @inheritDoc
371
	 * @access public
372
	 */
373
	public function set_field_error( $field, $error_message = '' ) {
374
		parent::set_field_error( $field, $error_message );
375
	}
376
377
	/**
378
	 * Register the settings field for the EDD License field type
379
	 * @param array $field
380
	 * @param bool $echo Whether to echo the
381
	 *
382
	 * @return string
383
	 */
384
	protected function settings_edd_license( $field, $echo = true ) {
385
386
		$text = self::settings_text( $field, false );
387
388
		$activation = $this->License_Handler->settings_edd_license_activation( $field, false );
389
390
		$return = $text . $activation;
391
392
		if( $echo ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
393
			echo $return;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$return'
Loading history...
394
		}
395
396
		return $return;
397
	}
398
399
	/**
400
	 * Allow public access to the GV_License_Handler class
401
	 * @since 1.7.4
402
	 *
403
	 * @return GV_License_Handler
404
	 */
405
	public function get_license_handler() {
406
		return $this->License_Handler;
407
	}
408
409
	/***
410
	 * Renders the save button for settings pages
411
	 *
412
	 * @param array $field - Field array containing the configuration options of this field
413
	 * @param bool  $echo  = true - true to echo the output to the screen, false to simply return the contents as a string
414
	 *
415
	 * @return string The HTML
416
	 */
417
	public function settings_submit( $field, $echo = true ) {
418
419
		$field['type']  = ( isset($field['type']) && in_array( $field['type'], array('submit','reset','button') ) ) ? $field['type'] : 'submit';
0 ignored issues
show
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...
420
421
		$attributes    = $this->get_field_attributes( $field );
422
		$default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
423
		$value         = $this->get_setting( $field['name'], $default_value );
424
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
425
426
		$attributes['class'] = isset( $attributes['class'] ) ? esc_attr( $attributes['class'] ) : 'button-primary gfbutton';
427
		$name    = ( $field['name'] === 'gform-settings-save' ) ? $field['name'] : '_gaddon_setting_'.$field['name'];
428
429
		if ( empty( $value ) ) {
430
			$value = __( 'Update Settings', 'gravityview' );
431
		}
432
433
		$attributes = $this->get_field_attributes( $field );
434
435
		$html = '<input
436
                    type="' . $field['type'] . '"
437
                    name="' . esc_attr( $name ) . '"
438
                    value="' . $value . '" ' .
439
		        implode( ' ', $attributes ) .
440
		        ' />';
441
442
		if ( $echo ) {
443
			echo $html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$html'
Loading history...
444
		}
445
446
		return $html;
447
	}
448
449
	/**
450
	 * Allow customizing the Save field parameters
451
	 *
452
	 * @param array $field
453
	 * @param bool $echo
454
	 *
455
	 * @return string
456
	 */
457
	public function settings_save( $field, $echo = true ) {
458
		$field['type']  = 'submit';
459
		$field['name']  = 'gform-settings-save';
460
		$field['class'] = isset( $field['class'] ) ? $field['class'] : 'button-primary gfbutton';
461
462
		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...
463
			$field['value'] = __( 'Update Settings', 'gravityview' );
464
465
		$output = $this->settings_submit( $field, false );
466
467
		if( $echo ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
468
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
469
		}
470
471
		return $output;
472
	}
473
474
	/**
475
	 * The same as the parent, except added support for field descriptions
476
	 * @inheritDoc
477
	 * @param $field array
478
	 */
479
	public function single_setting_label( $field ) {
480
481
		parent::single_setting_label( $field );
482
483
		// Added by GravityView
484
		if ( isset( $field['description'] ) ) {
485
			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...
486
		}
487
488
	}
489
490
	/**
491
	 * Get the default settings for the plugin
492
	 *
493
	 * Merges previous settings created when using the Redux Framework
494
	 *
495
	 * @return array Settings with defaults set
496
	 */
497
	private function get_default_settings() {
498
499
		$defaults = array(
500
			// Set the default license in wp-config.php
501
			'license_key' => defined( 'GRAVITYVIEW_LICENSE_KEY' ) ? GRAVITYVIEW_LICENSE_KEY : '',
502
			'license_key_response' => '',
503
			'license_key_status' => '',
504
			'support-email' => get_bloginfo( 'admin_email' ),
505
			'no-conflict-mode' => '0',
506
			'support_port' => '1',
507
			'delete-on-uninstall' => '0',
508
		);
509
510
		return $defaults;
511
	}
512
513
	/**
514
	 * Check for the `gravityview_edit_settings` capability before saving plugin settings.
515
	 * Gravity Forms says you're able to edit if you're able to view settings. GravityView allows two different permissions.
516
	 *
517
	 * @since 1.15
518
	 * @return void
519
	 */
520
	public function maybe_save_app_settings() {
521
522
		if ( $this->is_save_postback() ) {
523
			if ( ! GVCommon::has_cap( 'gravityview_edit_settings' ) ) {
524
				$_POST = array(); // If you don't reset the $_POST array, it *looks* like the settings were changed, but they weren't
525
				GFCommon::add_error_message( __( 'You don\'t have the ability to edit plugin settings.', 'gravityview' ) );
526
				return;
527
			}
528
		}
529
530
		parent::maybe_save_app_settings();
531
	}
532
533
	/**
534
	 * When the settings are saved, make sure the license key matches the previously activated key
535
	 *
536
	 * @return array settings from parent::get_posted_settings(), with `license_key_response` and `license_key_status` potentially unset
537
	 */
538
	public function get_posted_settings() {
539
540
		$posted_settings = parent::get_posted_settings();
541
542
		$local_key = rgar( $posted_settings, 'license_key' );
543
		$response_key = rgars( $posted_settings, 'license_key_response/license_key' );
544
545
		// If the posted key doesn't match the activated/deactivated key (set using the Activate License button, AJAX response),
546
		// then we assume it's changed. If it's changed, unset the status and the previous response.
547
		if( $local_key !== $response_key ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
548
549
			unset( $posted_settings['license_key_response'] );
550
			unset( $posted_settings['license_key_status'] );
551
			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...
552
		}
553
554
		return $posted_settings;
555
	}
556
557
	/**
558
	 * Gets the required indicator
559
	 * Gets the markup of the required indicator symbol to highlight fields that are required
560
	 *
561
	 * @param $field - The field meta.
562
	 *
563
	 * @return string - Returns markup of the required indicator symbol
564
	 */
565
	public function get_required_indicator( $field ) {
566
		return '<span class="required" title="' . esc_attr__( 'Required', 'gravityview' ) . '">*</span>';
567
	}
568
569
	/**
570
	 * Specify the settings fields to be rendered on the plugin settings page
571
	 * @return array
572
	 */
573
	public function app_settings_fields() {
574
575
		$default_settings = $this->get_default_settings();
576
577
		$disabled_attribute = GVCommon::has_cap( 'gravityview_edit_settings' ) ? false : 'disabled';
578
579
		$fields = apply_filters( 'gravityview_settings_fields', array(
580
			array(
581
				'name'                => 'license_key',
582
				'required'               => true,
583
				'label'             => __( 'License Key', 'gravityview' ),
584
				'description'          => __( 'Enter the license key that was sent to you on purchase. This enables plugin updates &amp; support.', 'gravityview' ),
585
				'type'              => 'edd_license',
586
				'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...
587
				'default_value'           => $default_settings['license_key'],
588
				'class'             => ( '' == $this->get_app_setting( 'license_key' ) ) ? 'activate code regular-text edd-license-key' : 'deactivate code regular-text edd-license-key',
589
			),
590
			array(
591
				'name'       => 'license_key_response',
592
				'default_value'  => $default_settings['license_key_response'],
593
				'type'     => 'hidden',
594
			),
595
			array(
596
				'name'       => 'license_key_status',
597
				'default_value'  => $default_settings['license_key_status'],
598
				'type'     => 'hidden',
599
			),
600
			array(
601
				'name'       => 'support-email',
602
				'type'     => 'text',
603
				'validate' => 'email',
604
				'default_value'  => $default_settings['support-email'],
605
				'label'    => __( 'Support Email', 'gravityview' ),
606
				'description' => __( 'In order to provide responses to your support requests, please provide your email address.', 'gravityview' ),
607
				'class'    => 'code regular-text',
608
			),
609
			/**
610
			 * @since 1.15 Added Support Port support
611
			 */
612
			array(
613
				'name'         => 'support_port',
614
				'type'       => 'radio',
615
				'label'      => __( 'Show Support Port?', 'gravityview' ),
616
				'default_value'    => $default_settings['support_port'],
617
				'horizontal' => 1,
618
				'choices'    => array(
619
					array(
620
						'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...
621
						'value' => '1',
622
					),
623
					array(
624
						'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...
625
						'value' => '0',
626
					),
627
				),
628
				'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...
629
				'description'   => __( 'Show the Support Port on GravityView pages?', 'gravityview' ),
630
			),
631
			array(
632
				'name'         => 'no-conflict-mode',
633
				'type'       => 'radio',
634
				'label'      => __( 'No-Conflict Mode', 'gravityview' ),
635
				'default_value'    => $default_settings['no-conflict-mode'],
636
				'horizontal' => 1,
637
				'choices'    => array(
638
					array(
639
						'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...
640
						'value' => '1',
641
					),
642
					array(
643
						'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...
644
						'value' => '0',
645
					),
646
				),
647
				'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...
648
			),
649
			array(
650
				'name'       => 'delete-on-uninstall',
651
				'type'       => 'radio',
652
				'label'      => __( 'Remove Data on Delete?', 'gravityview' ),
653
				'default_value'    => $default_settings['delete-on-uninstall'],
654
				'horizontal' => 1,
655
				'choices'    => array(
656
					array(
657
						'label' => _x( 'Keep GravityView Data', 'Setting: what to do when uninstalling plugin', 'gravityview' ),
658
						'value' => '0',
659
						'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' ) ),
660
					),
661
					array(
662
						'label' => _x( 'Permanently Delete', 'Setting: what to do when uninstalling plugin', 'gravityview' ),
663
						'value' => 'delete',
664
					    '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' ) ),
665
					),
666
				),
667
				'description'   => sprintf( __( 'Should GravityView content and entry approval status be removed from the site when the GravityView plugin is deleted?', 'gravityview' ), __( 'Permanently Delete', 'gravityview' ) ),
668
			),
669
670
		) );
671
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
672
673
674
		/**
675
		 * Redux backward compatibility
676
		 * @since 1.7.4
677
		 */
678
		foreach ( $fields as &$field ) {
679
			$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...
680
			$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...
681
			$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...
682
			$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...
683
684
			if( $disabled_attribute ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
685
				$field['disabled']  = $disabled_attribute;
686
			}
687
		}
688
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
689
690
        $sections = array(
691
            array(
692
                '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...
693
                'fields'      => $fields,
694
            )
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
695
        );
696
697
        // custom 'update settings' button
698
        $button = array(
699
            'class' => 'button button-primary button-hero',
700
            'type'     => 'save',
701
        );
702
703
		if( $disabled_attribute ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
704
			$button['disabled'] = $disabled_attribute;
705
		}
706
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
707
708
        /**
709
         * @filter `gravityview/settings/extension/sections` Modify the GravityView settings page
710
         * Extensions can tap in here to insert their own section and settings.
711
         * <code>
712
         *   $sections[] = array(
713
         *      'title' => __( 'GravityView My Extension Settings', 'gravityview' ),
714
         *      'fields' => $settings,
715
         *   );
716
         * </code>
717
         * @param array $extension_settings Empty array, ready for extension settings!
718
         */
719
        $extension_sections = apply_filters( 'gravityview/settings/extension/sections', array() );
720
721
		// If there are extensions, add a section for them
722
		if ( ! empty( $extension_sections ) ) {
723
724
			if( $disabled_attribute ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
725
				foreach ( $extension_sections as &$section ) {
726
					foreach ( $section['fields'] as &$field ) {
727
						$field['disabled'] = $disabled_attribute;
728
					}
729
				}
730
			}
731
732
            $k = count( $extension_sections ) - 1 ;
733
            $extension_sections[ $k ]['fields'][] = $button;
734
			$sections = array_merge( $sections, $extension_sections );
735
		} else {
736
            // add the 'update settings' button to the general section
737
            $sections[0]['fields'][] = $button;
738
        }
739
740
		return $sections;
741
	}
742
743
	/**
744
	 * Get the setting for GravityView by name
745
	 *
746
	 * @param  string $key     Option key to fetch
747
	 *
748
	 * @return mixed
749
	 */
750
	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...
751
		return self::get_instance()->get_app_setting( $key );
752
	}
753
754
}
755
756
GravityView_Settings::get_instance();