Completed
Push — master ( 022e44...5d3455 )
by Zack
08:11 queued 31s
created

GravityView_Settings::get_app_setting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.4285
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
			case 'invalid':
208
				$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...
209
				$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...
210
				break;
211
			case 'deactivated':
212
				$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...
213
				$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...
214
				break;
215
			/** @noinspection PhpMissingBreakStatementInspection */
216
			case '':
217
				$license_status = 'site_inactive';
218
				// break intentionally left blank
219
			case 'site_inactive':
220
				$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...
221
				$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...
222
				break;
223
		}
224
		$url = 'https://gravityview.co/pricing/?utm_source=admin_notice&utm_medium=admin&utm_content='.$license_status.'&utm_campaign=Admin%20Notice';
225
226
		// Show a different notice on settings page for inactive licenses (hide the buttons)
227
		if( $update_below && gravityview_is_admin_page( '', 'settings' ) ) {
228
			$message = sprintf( $message, $status, '<div class="hidden">', '', '', '</div><a href="#" onclick="jQuery(\'#license_key\').focus(); return false;">' . $update_below . '</a>' );
229
		} else {
230
			$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>' );
231
		}
232
233
		if( !empty( $status ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
234
			GravityView_Admin_Notices::add_notice( array(
235
				'message' => $message,
236
				'class'	=> 'updated',
237
				'title' => $title,
238
				'cap' => 'gravityview_edit_settings',
239
				'dismiss' => sha1( $license_status.'_'.$license_id ),
240
			));
241
		}
242
	}
243
244
	/**
245
	 * Register styles in the app admin page
246
	 * @return array
247
	 */
248
	public function styles() {
249
250
		$styles = parent::styles();
251
252
		$styles[] = array(
253
			'handle'  => 'gravityview_settings',
254
			'src'     => plugins_url( 'assets/css/admin-settings.css', GRAVITYVIEW_FILE ),
255
			'version' => GravityView_Plugin::version,
256
			"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...
257
				'gaddon_form_settings_css'
258
			),
259
			'enqueue' => array(
260
				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...
261
					'app_settings'
262
				) ),
263
			)
264
		);
265
266
		return $styles;
267
	}
268
269
	/**
270
	 * Add global Settings page for Multisite
271
	 * @since 1.7.6
272
	 * @return void
273
	 */
274
	public function add_network_menu() {
275
		if( GravityView_Plugin::is_network_activated() ) {
276
			add_menu_page( __( 'Settings', 'gravityview' ), __( 'GravityView', 'gravityview' ), $this->_capabilities_app_settings, "{$this->_slug}_settings", array( $this, 'app_tab_page' ), 'none' );
277
		}
278
	}
279
280
	/**
281
	 * Add Settings link to GravityView menu
282
	 * @return void
283
	 */
284
	public function create_app_menu() {
285
286
		/**
287
		 * If not multisite, always show.
288
		 * If multisite and the plugin is network activated, show; we need to register the submenu page for the Network Admin settings to work.
289
		 * If multisite and not network admin, we don't want the settings to show.
290
		 * @since 1.7.6
291
		 */
292
		$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...
293
294
		/**
295
		 * Override whether to show the Settings menu on a per-blog basis.
296
		 * @since 1.7.6
297
		 * @param bool $hide_if_network_activated Default: true
298
		 */
299
		$show_submenu = apply_filters( 'gravityview/show-settings-menu', $show_submenu );
300
301
		if( $show_submenu ) {
302
			add_submenu_page( 'edit.php?post_type=gravityview', __( 'Settings', 'gravityview' ), __( 'Settings', 'gravityview' ), $this->_capabilities_app_settings, $this->_slug . '_settings', array( $this, 'app_tab_page' ) );
303
		}
304
	}
305
306
	/**
307
	 * The Settings title
308
	 * @return string
309
	 */
310
	public function app_settings_title() {
311
		return null;
312
	}
313
314
	/**
315
	 * Prevent displaying of any icon
316
	 * @return string
317
	 */
318
	public function app_settings_icon() {
319
		return '<i></i>';
320
	}
321
322
	/**
323
	 * Make protected public
324
	 * @inheritDoc
325
	 * @access public
326
	 */
327
	public function get_app_setting( $setting_name ) {
328
329
		/**
330
		 * Backward compatibility with Redux
331
		 */
332
		if( $setting_name === 'license' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
333
			return array(
334
				'license' => parent::get_app_setting( 'license_key' ),
335
				'status' => parent::get_app_setting( 'license_key_status' ),
336
				'response' => parent::get_app_setting( 'license_key_response' ),
337
			);
338
		}
339
340
		return parent::get_app_setting( $setting_name );
341
	}
342
343
	/**
344
	 * Returns the currently saved plugin settings
345
	 *
346
	 * Different from GFAddon in two ways:
347
	 * 1. Makes protected method public
348
	 * 2. Use default settings if the original settings don't exist
349
	 *
350
	 * @access public
351
	 *
352
	 * @return array
353
	 */
354
	public function get_app_settings() {
355
		return get_option( 'gravityformsaddon_' . $this->_slug . '_app_settings', $this->get_default_settings() );
356
	}
357
358
359
	/**
360
	 * Updates app settings with the provided settings
361
	 *
362
	 * Same as the GVAddon, except it returns the value from update_option()
363
	 *
364
	 * @param array $settings - App settings to be saved
365
	 *
366
	 * @return boolean False if value was not updated and true if value was updated.
367
	 */
368
	public function update_app_settings( $settings ) {
369
		return update_option( 'gravityformsaddon_' . $this->_slug . '_app_settings', $settings );
370
	}
371
372
	/**
373
	 * Make protected public
374
	 * @inheritDoc
375
	 * @access public
376
	 */
377
	public function set_field_error( $field, $error_message = '' ) {
378
		parent::set_field_error( $field, $error_message );
379
	}
380
381
	/**
382
	 * Register the settings field for the EDD License field type
383
	 * @param array $field
384
	 * @param bool $echo Whether to echo the
385
	 *
386
	 * @return string
387
	 */
388
	protected function settings_edd_license( $field, $echo = true ) {
389
390
		$text = self::settings_text( $field, false );
391
392
		$activation = $this->License_Handler->settings_edd_license_activation( $field, false );
393
394
		$return = $text . $activation;
395
396
		if( $echo ) {
397
			echo $return;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$return'
Loading history...
398
		}
399
400
		return $return;
401
	}
402
403
	/**
404
	 * Allow public access to the GV_License_Handler class
405
	 * @since 1.7.4
406
	 *
407
	 * @return GV_License_Handler
408
	 */
409
	public function get_license_handler() {
410
		return $this->License_Handler;
411
	}
412
413
	/***
414
	 * Renders the save button for settings pages
415
	 *
416
	 * @param array $field - Field array containing the configuration options of this field
417
	 * @param bool  $echo  = true - true to echo the output to the screen, false to simply return the contents as a string
418
	 *
419
	 * @return string The HTML
420
	 */
421
	public function settings_submit( $field, $echo = true ) {
422
423
		$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...
424
425
		$attributes    = $this->get_field_attributes( $field );
426
		$default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
427
		$value         = $this->get_setting( $field['name'], $default_value );
428
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
429
430
		$attributes['class'] = isset( $attributes['class'] ) ? esc_attr( $attributes['class'] ) : 'button-primary gfbutton';
431
		$name    = ( $field['name'] === 'gform-settings-save' ) ? $field['name'] : '_gaddon_setting_'.$field['name'];
432
433
		if ( empty( $value ) ) {
434
			$value = __( 'Update Settings', 'gravityview' );
435
		}
436
437
		$attributes = $this->get_field_attributes( $field );
438
439
		$html = '<input
440
                    type="' . $field['type'] . '"
441
                    name="' . esc_attr( $name ) . '"
442
                    value="' . $value . '" ' .
443
		        implode( ' ', $attributes ) .
444
		        ' />';
445
446
		if ( $echo ) {
447
			echo $html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$html'
Loading history...
448
		}
449
450
		return $html;
451
	}
452
453
	/**
454
	 * Allow customizing the Save field parameters
455
	 *
456
	 * @param array $field
457
	 * @param bool $echo
458
	 *
459
	 * @return string
460
	 */
461
	public function settings_save( $field, $echo = true ) {
462
		$field['type']  = 'submit';
463
		$field['name']  = 'gform-settings-save';
464
		$field['class'] = isset( $field['class'] ) ? $field['class'] : 'button-primary gfbutton';
465
466
		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...
467
			$field['value'] = __( 'Update Settings', 'gravityview' );
468
469
		$output = $this->settings_submit( $field, false );
470
471
		if( $echo ) {
472
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
473
		}
474
475
		return $output;
476
	}
477
478
	/**
479
	 * The same as the parent, except added support for field descriptions
480
	 * @inheritDoc
481
	 * @param $field array
482
	 */
483
	public function single_setting_label( $field ) {
484
485
		parent::single_setting_label( $field );
486
487
		// Added by GravityView
488
		if ( isset( $field['description'] ) ) {
489
			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...
490
		}
491
492
	}
493
494
	/**
495
	 * Get the default settings for the plugin
496
	 *
497
	 * Merges previous settings created when using the Redux Framework
498
	 *
499
	 * @return array Settings with defaults set
500
	 */
501
	private function get_default_settings() {
502
503
		$defaults = array(
504
			// Set the default license in wp-config.php
505
			'license_key' => defined( 'GRAVITYVIEW_LICENSE_KEY' ) ? GRAVITYVIEW_LICENSE_KEY : '',
506
			'license_key_response' => '',
507
			'license_key_status' => '',
508
			'support-email' => get_bloginfo( 'admin_email' ),
509
			'no-conflict-mode' => '0',
510
			'support_port' => '1',
511
			'delete-on-uninstall' => '0',
512
		);
513
514
		return $defaults;
515
	}
516
517
	/**
518
	 * Check for the `gravityview_edit_settings` capability before saving plugin settings.
519
	 * Gravity Forms says you're able to edit if you're able to view settings. GravityView allows two different permissions.
520
	 *
521
	 * @since 1.15
522
	 * @return void
523
	 */
524
	public function maybe_save_app_settings() {
525
526
		if ( $this->is_save_postback() ) {
527
			if ( ! GVCommon::has_cap( 'gravityview_edit_settings' ) ) {
528
				$_POST = array(); // If you don't reset the $_POST array, it *looks* like the settings were changed, but they weren't
529
				GFCommon::add_error_message( __( 'You don\'t have the ability to edit plugin settings.', 'gravityview' ) );
530
				return;
531
			}
532
		}
533
534
		parent::maybe_save_app_settings();
535
	}
536
537
	/**
538
	 * When the settings are saved, make sure the license key matches the previously activated key
539
	 *
540
	 * @return array settings from parent::get_posted_settings(), with `license_key_response` and `license_key_status` potentially unset
541
	 */
542
	public function get_posted_settings() {
543
544
		$posted_settings = parent::get_posted_settings();
545
546
		$local_key = rgar( $posted_settings, 'license_key' );
547
		$response_key = rgars( $posted_settings, 'license_key_response/license_key' );
548
549
		// If the posted key doesn't match the activated/deactivated key (set using the Activate License button, AJAX response),
550
		// then we assume it's changed. If it's changed, unset the status and the previous response.
551
		if( $local_key !== $response_key ) {
552
553
			unset( $posted_settings['license_key_response'] );
554
			unset( $posted_settings['license_key_status'] );
555
			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...
556
		}
557
558
		return $posted_settings;
559
	}
560
561
	/**
562
	 * Gets the required indicator
563
	 * Gets the markup of the required indicator symbol to highlight fields that are required
564
	 *
565
	 * @param $field - The field meta.
566
	 *
567
	 * @return string - Returns markup of the required indicator symbol
568
	 */
569
	public function get_required_indicator( $field ) {
570
		return '<span class="required" title="' . esc_attr__( 'Required', 'gravityview' ) . '">*</span>';
571
	}
572
573
	/**
574
	 * Specify the settings fields to be rendered on the plugin settings page
575
	 * @return array
576
	 */
577
	public function app_settings_fields() {
578
579
		$default_settings = $this->get_default_settings();
580
581
		$disabled_attribute = GVCommon::has_cap( 'gravityview_edit_settings' ) ? false : 'disabled';
582
583
		$fields = apply_filters( 'gravityview_settings_fields', array(
584
			array(
585
				'name'                => 'license_key',
586
				'required'               => true,
587
				'label'             => __( 'License Key', 'gravityview' ),
588
				'description'          => __( 'Enter the license key that was sent to you on purchase. This enables plugin updates &amp; support.', 'gravityview' ),
589
				'type'              => 'edd_license',
590
				'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...
591
				'default_value'           => $default_settings['license_key'],
592
				'class'             => ( '' == $this->get_app_setting( 'license_key' ) ) ? 'activate code regular-text edd-license-key' : 'deactivate code regular-text edd-license-key',
593
			),
594
			array(
595
				'name'       => 'license_key_response',
596
				'default_value'  => $default_settings['license_key_response'],
597
				'type'     => 'hidden',
598
			),
599
			array(
600
				'name'       => 'license_key_status',
601
				'default_value'  => $default_settings['license_key_status'],
602
				'type'     => 'hidden',
603
			),
604
			array(
605
				'name'       => 'support-email',
606
				'type'     => 'text',
607
				'validate' => 'email',
608
				'default_value'  => $default_settings['support-email'],
609
				'label'    => __( 'Support Email', 'gravityview' ),
610
				'description' => __( 'In order to provide responses to your support requests, please provide your email address.', 'gravityview' ),
611
				'class'    => 'code regular-text',
612
			),
613
			/**
614
			 * @since 1.15 Added Support Port support
615
			 */
616
			array(
617
				'name'         => 'support_port',
618
				'type'       => 'radio',
619
				'label'      => __( 'Show Support Port?', 'gravityview' ),
620
				'default_value'    => $default_settings['support_port'],
621
				'horizontal' => 1,
622
				'choices'    => array(
623
					array(
624
						'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...
625
						'value' => '1',
626
					),
627
					array(
628
						'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...
629
						'value' => '0',
630
					),
631
				),
632
				'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...
633
				'description'   => __( 'Show the Support Port on GravityView pages?', 'gravityview' ),
634
			),
635
			array(
636
				'name'         => 'no-conflict-mode',
637
				'type'       => 'radio',
638
				'label'      => __( 'No-Conflict Mode', 'gravityview' ),
639
				'default_value'    => $default_settings['no-conflict-mode'],
640
				'horizontal' => 1,
641
				'choices'    => array(
642
					array(
643
						'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...
644
						'value' => '1',
645
					),
646
					array(
647
						'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...
648
						'value' => '0',
649
					),
650
				),
651
				'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...
652
			),
653
			array(
654
				'name'       => 'delete-on-uninstall',
655
				'type'       => 'radio',
656
				'label'      => __( 'Remove Data on Delete?', 'gravityview' ),
657
				'default_value'    => $default_settings['delete-on-uninstall'],
658
				'horizontal' => 1,
659
				'choices'    => array(
660
					array(
661
						'label' => _x( 'Keep GravityView Data', 'Setting: what to do when uninstalling plugin', 'gravityview' ),
662
						'value' => '0',
663
						'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' ) ),
664
					),
665
					array(
666
						'label' => _x( 'Permanently Delete', 'Setting: what to do when uninstalling plugin', 'gravityview' ),
667
						'value' => 'delete',
668
					    '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' ) ),
669
					),
670
				),
671
				'description'   => sprintf( __( 'Should GravityView content and entry approval status be removed from the site when the GravityView plugin is deleted?', 'gravityview' ), __( 'Permanently Delete', 'gravityview' ) ),
672
			),
673
674
		) );
675
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
676
677
678
		/**
679
		 * Redux backward compatibility
680
		 * @since 1.7.4
681
		 */
682
		foreach ( $fields as &$field ) {
683
			$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...
684
			$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...
685
			$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...
686
			$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...
687
688
			if( $disabled_attribute ) {
689
				$field['disabled']  = $disabled_attribute;
690
			}
691
		}
692
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
693
694
        $sections = array(
695
            array(
696
                '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...
697
                'fields'      => $fields,
698
            )
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
699
        );
700
701
        // custom 'update settings' button
702
        $button = array(
703
            'class' => 'button button-primary button-hero',
704
            'type'     => 'save',
705
        );
706
707
		if( $disabled_attribute ) {
708
			$button['disabled'] = $disabled_attribute;
709
		}
710
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
711
712
        /**
713
         * @filter `gravityview/settings/extension/sections` Modify the GravityView settings page
714
         * Extensions can tap in here to insert their own section and settings.
715
         * <code>
716
         *   $sections[] = array(
717
         *      'title' => __( 'GravityView My Extension Settings', 'gravityview' ),
718
         *      'fields' => $settings,
719
         *   );
720
         * </code>
721
         * @param array $extension_settings Empty array, ready for extension settings!
722
         */
723
        $extension_sections = apply_filters( 'gravityview/settings/extension/sections', array() );
724
725
		// If there are extensions, add a section for them
726
		if ( ! empty( $extension_sections ) ) {
727
728
			if( $disabled_attribute ) {
729
				foreach ( $extension_sections as &$section ) {
730
					foreach ( $section['fields'] as &$field ) {
731
						$field['disabled'] = $disabled_attribute;
732
					}
733
				}
734
			}
735
736
            $k = count( $extension_sections ) - 1 ;
737
            $extension_sections[ $k ]['fields'][] = $button;
738
			$sections = array_merge( $sections, $extension_sections );
739
		} else {
740
            // add the 'update settings' button to the general section
741
            $sections[0]['fields'][] = $button;
742
        }
743
744
		return $sections;
745
	}
746
747
	/**
748
	 * Get the setting for GravityView by name
749
	 *
750
	 * @param  string $key     Option key to fetch
751
	 *
752
	 * @return mixed
753
	 */
754
	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...
755
		return self::get_instance()->get_app_setting( $key );
756
	}
757
758
}
759
760
GravityView_Settings::get_instance();