Completed
Push — develop ( c88545...500ee6 )
by Zack
15:52 queued 04:57
created

GravityView_Settings::getSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
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();
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
80
		}
81
82
		return self::get_instance();
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
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
		if( empty( $caps ) ) {
110
			$caps = array( 'gravityview_full_access' );
111
		}
112
113
		return GVCommon::has_cap( $caps );
114
	}
115
116
	public function uninstall_warning_message() {
117
118
		$heading = esc_html__( 'If you delete then re-install GravityView, it will be like installing GravityView for the first time.', 'gravityview' );
119
		$message = esc_html__( 'Delete all Views, GravityView entry approval status, GravityView-generated entry notes (including approval and entry creator changes), and GravityView plugin settings.', 'gravityview' );
120
121
		return sprintf( '<h4>%s</h4><p>%s</p>', $heading, $message );
122
	}
123
124
	public function uninstall() {
125
126
		include_once( GRAVITYVIEW_DIR . 'includes/class-gravityview-uninstall.php' );
127
128
		$uninstaller = new GravityView_Uninstall();
129
130
		$uninstaller->fire_everything();
131
132
		/**
133
         * Set the path so that Gravity Forms can de-activate GravityView
134
         * @see GFAddOn::uninstall_addon
135
         * @uses deactivate_plugins()
136
         */
137
		$this->_path = GRAVITYVIEW_FILE;
138
139
		return true;
140
	}
141
142
	/**
143
     * Get an array of reasons why the plugin might be uninstalled
144
     *
145
     * @since 1.17.5
146
     *
147
	 * @return array Array of reasons with the label and followup questions for each uninstall reason
148
	 */
149
	private function get_uninstall_reasons() {
150
151
		$reasons = array(
152
			'will-continue' => array(
153
                'label' => esc_html__( 'I am going to continue using GravityView', 'gravityview' ),
154
            ),
155
			'no-longer-need' => array(
156
                'label' => esc_html__( 'I no longer need GravityView', 'gravityview' ),
157
            ),
158
			'doesnt-work' => array(
159
                'label' => esc_html__( 'The plugin doesn\'t work', 'gravityview' ),
160
            ),
161
			'found-other' => array(
162
                'label' => esc_html__( 'I found a better plugin', 'gravityview' ),
163
                'followup' => esc_attr__('What plugin you are using, and why?', '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...
164
            ),
165
			'other' => array(
166
                'label' => esc_html__( 'Other', 'gravityview' ),
167
            ),
168
		);
169
170
		shuffle( $reasons );
171
172
		return $reasons;
173
    }
174
175
	/**
176
     * Display a feedback form when the plugin is uninstalled
177
     *
178
     * @since 1.17.5
179
     *
180
	 * @return string HTML of the uninstallation form
181
	 */
182
	public function uninstall_form() {
183
		ob_start();
184
185
		$user = wp_get_current_user();
186
		?>
187
    <style>
188
        #gv-reason-details {
189
            min-height: 100px;
190
        }
191
        .number-scale label {
192
            border: 1px solid #cccccc;
193
            padding: .5em .75em;
194
            margin: .1em;
195
        }
196
        #gv-uninstall-thanks p {
197
            font-size: 1.2em;
198
        }
199
        .scale-description ul {
200
            margin-bottom: 0;
201
            padding-bottom: 0;
202
        }
203
        .scale-description p.description {
204
            margin-top: 0!important;
205
            padding-top: 0!important;
206
        }
207
        .gv-form-field-wrapper {
208
            margin-top: 30px;
209
        }
210
    </style>
211
212
    <div class="gv-uninstall-form-wrapper" style="font-size: 110%; padding: 15px 0;">
213
        <script>
214
            jQuery(function( $ ) {
215
                $('#gv-uninstall-feedback').on( 'change', function( e ) {
216
217
                    if( ! $( e.target ).is(':input') ) {
218
                        return;
219
                    }
220
                    var $textarea = $('.gv-followup').find('textarea');
221
                    var followup_text = $( e.target ).attr( 'data-followup' );
222
                    if( ! followup_text ) {
223
                        followup_text = $textarea.attr('data-default');
224
                    }
225
226
                    $textarea.attr( 'placeholder', followup_text );
227
228
                }).on( 'submit', function( e ) {
229
                    e.preventDefault();
230
231
                    $.post( $( this ).attr( 'action' ), $( this ).serialize() )
232
                        .done( function( data ) {
233
                            if( 'success' !== data.status ) {
234
                                gv_feedback_append_error_message();
235
                            } else {
236
                                $( '#gv-uninstall-thanks' ).fadeIn();
237
                            }
238
                        })
239
                        .fail( function( data ) {
240
                            gv_feedback_append_error_message();
241
                        })
242
                        .always( function() {
243
                            $( e.target ).remove();
244
                        });
245
246
                    return false;
247
                });
248
249
                function gv_feedback_append_error_message() {
250
                    $('#gv-uninstall-thanks').append('<div class="notice error"><?php echo esc_js( __('There was an error sharing your feedback. Sorry! Please email us at [email protected]', 'gravityview' ) ) ?></div>');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
251
                }
252
            });
253
        </script>
254
255
        <form id="gv-uninstall-feedback" method="post" action="https://hooks.zapier.com/hooks/catch/28670/6haevn/">
256
            <h2><?php esc_html_e( 'Why did you uninstall GravityView?', 'gravityview' ); ?></h2>
257
            <ul>
258
				<?php
259
                $reasons = $this->get_uninstall_reasons();
260
				foreach ( $reasons as $reason ) {
261
					printf( '<li><label><input name="reason" type="radio" value="other" data-followup="%s"> %s</label></li>', rgar( $reason, 'followup' ), rgar( $reason, 'label' ) );
262
				}
263
				?>
264
            </ul>
265
            <div class="gv-followup widefat">
266
                <p><strong><label for="gv-reason-details"><?php esc_html_e( 'Comments', 'gravityview' ); ?></label></strong></p>
267
                <textarea id="gv-reason-details" name="reason_details" data-default="<?php esc_attr_e('Please share your thoughts about GravityView', 'gravityview') ?>" placeholder="<?php esc_attr_e('Please share your thoughts about GravityView', 'gravityview'); ?>" class="large-text"></textarea>
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...
268
            </div>
269
            <div class="scale-description">
270
                <p><strong><?php esc_html_e('How likely are you to recommend GravityView?', 'gravityview' ); ?></strong></p>
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
271
                <ul class="inline">
272
					<?php
273
					$i = 0;
274
					while( $i < 11 ) {
275
						echo '<li class="inline number-scale"><label><input name="likely_to_refer" id="likely_to_refer_'.$i.'" value="'.$i.'" type="radio"> '.$i.'</label></li>';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$i'
Loading history...
276
						$i++;
277
					}
278
					?>
279
                </ul>
280
                <p class="description"><?php printf( esc_html_x( '%s ("Not at all likely") to %s ("Extremely likely")', 'A scale from 0 (bad) to 10 (good)', 'gravityview' ), '<label for="likely_to_refer_0"><code>0</code></label>', '<label for="likely_to_refer_10"><code>10</code></label>' ); ?></p>
281
            </div>
282
283
            <div class="gv-form-field-wrapper">
284
                <label><input type="checkbox" class="checkbox" name="follow_up_with_me" value="1" /> <?php esc_html_e('Please follow up with me about my feedback', 'gravityview'); ?></label>
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...
285
            </div>
286
287
            <div class="submit">
288
                <input type="hidden" name="siteurl" value="<?php echo esc_url( get_bloginfo( 'siteurl' ) ); ?>" />
289
                <input type="hidden" name="email" value="<?php echo esc_attr( $user->user_email ); ?>" />
290
                <input type="hidden" name="display_name" value="<?php echo esc_attr( $user->display_name ); ?>" />
291
                <input type="submit" value="<?php esc_html_e( 'Send Us Your Feedback', 'gravityview' ); ?>" class="button button-primary button-hero" />
292
            </div>
293
        </form>
294
295
        <div id="gv-uninstall-thanks" class="notice notice-large notice-updated below-h2" style="display:none;">
296
            <h3 class="notice-title"><?php esc_html_e( 'Thank you for using GravityView!', 'gravityview' ); ?></h3>
297
            <p><?php echo gravityview_get_floaty(); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'gravityview_get_floaty'
Loading history...
298
				<?php echo make_clickable( esc_html__('Your feedback helps us improve GravityView. If you have any questions or comments, email us: [email protected]', 'gravityview' ) ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'make_clickable'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
299
            </p>
300
            <div class="wp-clearfix"></div>
301
        </div>
302
    </div>
303
		<?php
304
		$form = ob_get_clean();
305
306
		return $form;
307
	}
308
309
310
	public function app_settings_uninstall_tab() {
311
312
		if ( $this->maybe_uninstall() ) {
313
314
			parent::app_settings_uninstall_tab();
315
316
			return;
317
		}
318
319
		if ( ! ( $this->current_user_can_any( $this->_capabilities_uninstall ) && ( ! function_exists( 'is_multisite' ) || ! is_multisite() || is_super_admin() ) ) ) {
320
			return;
321
		}
322
323
		?>
324
		<script>
325
			jQuery(document).on('click', 'a[rel="gv-uninstall-wrapper"]', function( e ) {
326
				e.preventDefault();
327
				jQuery( '#gv-uninstall-wrapper' ).slideToggle();
328
			});
329
		</script>
330
331
		<a rel="gv-uninstall-wrapper" href="#gv-uninstall-wrapper" class="button button-large alignright button-danger">Uninstall GravityView</a>
332
333
		<div id="gv-uninstall-wrapper">
334
			<form action="" method="post">
335
				<?php wp_nonce_field( 'uninstall', 'gf_addon_uninstall' ) ?>
336
				<div class="delete-alert alert_red">
337
338
					<h3>
339
						<i class="fa fa-exclamation-triangle gf_invalid"></i> <?php esc_html_e( 'Delete all GravityView content and settings', 'gravityview' ); ?>
340
					</h3>
341
342
					<div class="gf_delete_notice">
343
						<?php echo $this->uninstall_warning_message() ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
344
					</div>
345
346
					<?php
347
					echo '<input type="submit" name="uninstall" value="' . sprintf( esc_attr__( 'Uninstall %s', 'gravityforms' ), $this->get_short_title() ) . '" class="button button-hero" onclick="return confirm(\'' . esc_js( $this->uninstall_confirm_message() ) . '\');" onkeypress="return confirm(\'' . esc_js( $this->uninstall_confirm_message() ) . '\');"/>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
348
					?>
349
350
				</div>
351
			</form>
352
		</div>
353
	<?php
354
	}
355
356
	/**
357
	 * Run actions when initializing admin
358
	 *
359
	 * Triggers the license key notice
360
	 *
361
	 * @return void
362
	 */
363
	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...
364
365
		$this->_load_license_handler();
366
367
		$this->license_key_notice();
368
369
		add_filter( 'gform_addon_app_settings_menu_gravityview', array( $this, 'modify_app_settings_menu_title' ) );
370
371
		/** @since 1.7.6 */
372
		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...
373
374
		parent::init_admin();
375
	}
376
377
	/**
378
	 * Change the settings page header title to "GravityView"
379
	 *
380
	 * @param $setting_tabs
381
	 *
382
	 * @return array
383
	 */
384
	public function modify_app_settings_menu_title( $setting_tabs ) {
385
386
		$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...
387
388
		return $setting_tabs;
389
	}
390
391
	/**
392
	 * Load license handler in admin-ajax.php
393
	 */
394
	public function init_ajax() {
395
		$this->_load_license_handler();
396
	}
397
398
	/**
399
	 * Make sure the license handler is available
400
	 */
401
	private function _load_license_handler() {
402
403
		if( !empty( $this->License_Handler ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
404
			return;
405
		}
406
407
		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...
408
409
		$this->License_Handler = GV_License_Handler::get_instance( $this );
410
	}
411
412
	/**
413
	 * Display a notice if the plugin is inactive.
414
	 * @return void
415
	 */
416
	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...
417
418
		// Only show on GravityView pages
419
		if( ! gravityview_is_admin_page() ) {
420
			return;
421
		}
422
423
		$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...
424
		$license_key = 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...
425
		if( '' === $license_key ) {
426
			$license_status = 'inactive';
427
        }
428
		$license_id = empty( $license_key ) ? 'license' : $license_key;
429
430
		$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...
431
432
		/**
433
		 * I wanted to remove the period from after the buttons in the string,
434
		 * but didn't want to mess up the translation strings for the translators.
435
		 */
436
		$message = mb_substr( $message, 0, mb_strlen( $message ) - 1 );
437
		$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...
438
		$status = '';
439
		$update_below = false;
440
		$primary_button_link = admin_url( 'edit.php?post_type=gravityview&amp;page=gravityview_settings' );
441
		switch ( $license_status ) {
442
			/** @since 1.17 */
443
			case 'expired':
444
				$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...
445
				$status = 'expired';
446
				$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...
447
				break;
448
			case 'invalid':
449
				$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...
450
				$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...
451
				break;
452
			case 'deactivated':
453
				$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...
454
				$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...
455
				break;
456
			/** @noinspection PhpMissingBreakStatementInspection */
457
			case '':
458
				$license_status = 'site_inactive';
459
				// break intentionally left blank
460
			case 'inactive':
461
			case 'site_inactive':
462
				$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...
463
				$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...
464
				break;
465
		}
466
		$url = 'https://gravityview.co/pricing/?utm_source=admin_notice&utm_medium=admin&utm_content='.$license_status.'&utm_campaign=Admin%20Notice';
467
468
		// Show a different notice on settings page for inactive licenses (hide the buttons)
469
		if( $update_below && gravityview_is_admin_page( '', 'settings' ) ) {
470
			$message = sprintf( $message, $status, '<div class="hidden">', '', '', '</div><a href="#" onclick="jQuery(\'#license_key\').focus(); return false;">' . $update_below . '</a>' );
471
		} else {
472
			$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>' );
473
		}
474
475
		if( !empty( $status ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
476
			GravityView_Admin_Notices::add_notice( array(
477
				'message' => $message,
478
				'class'	=> 'updated',
479
				'title' => $title,
480
				'cap' => 'gravityview_edit_settings',
481
				'dismiss' => sha1( $license_status.'_'.$license_id ),
482
			));
483
		}
484
	}
485
486
	/**
487
	 * Register styles in the app admin page
488
	 * @return array
489
	 */
490
	public function styles() {
491
492
		$styles = parent::styles();
493
494
		$styles[] = array(
495
			'handle'  => 'gravityview_settings',
496
			'src'     => plugins_url( 'assets/css/admin-settings.css', GRAVITYVIEW_FILE ),
497
			'version' => GravityView_Plugin::version,
498
			"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...
499
				'gaddon_form_settings_css'
500
			),
501
			'enqueue' => array(
502
				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...
503
					'app_settings'
504
				) ),
505
			)
506
		);
507
508
		return $styles;
509
	}
510
511
	/**
512
	 * Add global Settings page for Multisite
513
	 * @since 1.7.6
514
	 * @return void
515
	 */
516
	public function add_network_menu() {
517
		if( GravityView_Plugin::is_network_activated() ) {
518
			add_menu_page( __( 'Settings', 'gravityview' ), __( 'GravityView', 'gravityview' ), $this->_capabilities_app_settings, "{$this->_slug}_settings", array( $this, 'app_tab_page' ), 'none' );
519
		}
520
	}
521
522
	/**
523
	 * Add Settings link to GravityView menu
524
	 * @return void
525
	 */
526
	public function create_app_menu() {
527
528
		/**
529
		 * If not multisite, always show.
530
		 * If multisite and the plugin is network activated, show; we need to register the submenu page for the Network Admin settings to work.
531
		 * If multisite and not network admin, we don't want the settings to show.
532
		 * @since 1.7.6
533
		 */
534
		$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...
535
536
		/**
537
		 * Override whether to show the Settings menu on a per-blog basis.
538
		 * @since 1.7.6
539
		 * @param bool $hide_if_network_activated Default: true
540
		 */
541
		$show_submenu = apply_filters( 'gravityview/show-settings-menu', $show_submenu );
542
543
		if( $show_submenu ) {
544
			add_submenu_page( 'edit.php?post_type=gravityview', __( 'Settings', 'gravityview' ), __( 'Settings', 'gravityview' ), $this->_capabilities_app_settings, $this->_slug . '_settings', array( $this, 'app_tab_page' ) );
545
		}
546
	}
547
548
	/**
549
	 * The Settings title
550
	 * @return string
551
	 */
552
	public function app_settings_title() {
553
		return null;
554
	}
555
556
	/**
557
	 * Prevent displaying of any icon
558
	 * @return string
559
	 */
560
	public function app_settings_icon() {
561
		return '&nbsp;';
562
	}
563
564
	public function app_settings_tab() {
565
	    parent::app_settings_tab();
566
567
		if ( $this->maybe_uninstall() ) {
568
            echo $this->uninstall_form();
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
569
		}
570
    }
571
572
	/**
573
	 * Make protected public
574
	 * @inheritDoc
575
	 * @access public
576
	 */
577
	public function get_app_setting( $setting_name ) {
578
579
		/**
580
		 * Backward compatibility with Redux
581
		 */
582
		if( $setting_name === 'license' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
583
			return array(
584
				'license' => parent::get_app_setting( 'license_key' ),
585
				'status' => parent::get_app_setting( 'license_key_status' ),
586
				'response' => parent::get_app_setting( 'license_key_response' ),
587
			);
588
		}
589
590
		return parent::get_app_setting( $setting_name );
591
	}
592
593
	/**
594
	 * Returns the currently saved plugin settings
595
	 *
596
	 * Different from GFAddon in two ways:
597
	 * 1. Makes protected method public
598
	 * 2. Use default settings if the original settings don't exist
599
	 *
600
	 * @access public
601
	 *
602
	 * @return array
603
	 */
604
	public function get_app_settings() {
605
		return get_option( 'gravityformsaddon_' . $this->_slug . '_app_settings', $this->get_default_settings() );
606
	}
607
608
609
	/**
610
	 * Updates app settings with the provided settings
611
	 *
612
	 * Same as the GVAddon, except it returns the value from update_option()
613
	 *
614
	 * @param array $settings - App settings to be saved
615
	 *
616
	 * @return boolean False if value was not updated and true if value was updated.
617
	 */
618
	public function update_app_settings( $settings ) {
619
		return update_option( 'gravityformsaddon_' . $this->_slug . '_app_settings', $settings );
620
	}
621
622
	/**
623
	 * Make protected public
624
	 * @inheritDoc
625
	 * @access public
626
	 */
627
	public function set_field_error( $field, $error_message = '' ) {
628
		parent::set_field_error( $field, $error_message );
629
	}
630
631
	/**
632
	 * Register the settings field for the EDD License field type
633
	 * @param array $field
634
	 * @param bool $echo Whether to echo the
635
	 *
636
	 * @return string
637
	 */
638
	protected function settings_edd_license( $field, $echo = true ) {
639
640
		$text = self::settings_text( $field, false );
641
642
		$activation = $this->License_Handler->settings_edd_license_activation( $field, false );
643
644
		$return = $text . $activation;
645
646
		if( $echo ) {
647
			echo $return;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$return'
Loading history...
648
		}
649
650
		return $return;
651
	}
652
653
	/**
654
	 * Allow public access to the GV_License_Handler class
655
	 * @since 1.7.4
656
	 *
657
	 * @return GV_License_Handler
658
	 */
659
	public function get_license_handler() {
660
		return $this->License_Handler;
661
	}
662
663
	/***
664
	 * Renders the save button for settings pages
665
	 *
666
	 * @param array $field - Field array containing the configuration options of this field
667
	 * @param bool  $echo  = true - true to echo the output to the screen, false to simply return the contents as a string
668
	 *
669
	 * @return string The HTML
670
	 */
671
	public function settings_submit( $field, $echo = true ) {
672
673
		$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...
674
675
		$attributes    = $this->get_field_attributes( $field );
676
		$default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
677
		$value         = $this->get_setting( $field['name'], $default_value );
678
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
679
680
		$attributes['class'] = isset( $attributes['class'] ) ? esc_attr( $attributes['class'] ) : 'button-primary gfbutton';
681
		$name    = ( $field['name'] === 'gform-settings-save' ) ? $field['name'] : '_gaddon_setting_'.$field['name'];
682
683
		if ( empty( $value ) ) {
684
			$value = __( 'Update Settings', 'gravityview' );
685
		}
686
687
		$attributes = $this->get_field_attributes( $field );
688
689
		$html = '<input
690
                    type="' . $field['type'] . '"
691
                    name="' . esc_attr( $name ) . '"
692
                    value="' . $value . '" ' .
693
		        implode( ' ', $attributes ) .
694
		        ' />';
695
696
		if ( $echo ) {
697
			echo $html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$html'
Loading history...
698
		}
699
700
		return $html;
701
	}
702
703
	/**
704
	 * Allow customizing the Save field parameters
705
	 *
706
	 * @param array $field
707
	 * @param bool $echo
708
	 *
709
	 * @return string
710
	 */
711
	public function settings_save( $field, $echo = true ) {
712
		$field['type']  = 'submit';
713
		$field['name']  = 'gform-settings-save';
714
		$field['class'] = isset( $field['class'] ) ? $field['class'] : 'button-primary gfbutton';
715
716
		if ( ! rgar( $field, 'value' ) ) {
717
			$field['value'] = __( 'Update Settings', 'gravityview' );
718
		}
719
720
		$output = $this->settings_submit( $field, false );
721
722
		ob_start();
723
		$this->app_settings_uninstall_tab();
724
		$output .= ob_get_clean();
725
726
		if( $echo ) {
727
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
728
		}
729
730
		return $output;
731
	}
732
733
734
	/**
735
	 * The same as the parent, except added support for field descriptions
736
	 * @inheritDoc
737
	 * @param $field array
738
	 */
739
	public function single_setting_label( $field ) {
740
741
		parent::single_setting_label( $field );
742
743
		// Added by GravityView
744
		if ( isset( $field['description'] ) ) {
745
			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...
746
		}
747
748
	}
749
750
	/**
751
	 * Get the default settings for the plugin
752
	 *
753
	 * Merges previous settings created when using the Redux Framework
754
	 *
755
	 * @return array Settings with defaults set
756
	 */
757
	private function get_default_settings() {
758
759
		$defaults = array(
760
			// Set the default license in wp-config.php
761
			'license_key' => defined( 'GRAVITYVIEW_LICENSE_KEY' ) ? GRAVITYVIEW_LICENSE_KEY : '',
762
			'license_key_response' => '',
763
			'license_key_status' => '',
764
			'support-email' => get_bloginfo( 'admin_email' ),
765
			'no-conflict-mode' => '1',
766
			'support_port' => '1',
767
			'flexbox_search' => '1',
768
		);
769
770
		return $defaults;
771
	}
772
773
	/**
774
	 * Check for the `gravityview_edit_settings` capability before saving plugin settings.
775
	 * Gravity Forms says you're able to edit if you're able to view settings. GravityView allows two different permissions.
776
	 *
777
	 * @since 1.15
778
	 * @return void
779
	 */
780
	public function maybe_save_app_settings() {
781
782
		if ( $this->is_save_postback() ) {
783
			if ( ! GVCommon::has_cap( 'gravityview_edit_settings' ) ) {
784
				$_POST = array(); // If you don't reset the $_POST array, it *looks* like the settings were changed, but they weren't
785
				GFCommon::add_error_message( __( 'You don\'t have the ability to edit plugin settings.', 'gravityview' ) );
786
				return;
787
			}
788
		}
789
790
		parent::maybe_save_app_settings();
791
	}
792
793
	/**
794
	 * When the settings are saved, make sure the license key matches the previously activated key
795
	 *
796
	 * @return array settings from parent::get_posted_settings(), with `license_key_response` and `license_key_status` potentially unset
797
	 */
798
	public function get_posted_settings() {
799
800
		$posted_settings = parent::get_posted_settings();
801
802
		$local_key = rgar( $posted_settings, 'license_key' );
803
		$response_key = rgars( $posted_settings, 'license_key_response/license_key' );
804
805
		// If the posted key doesn't match the activated/deactivated key (set using the Activate License button, AJAX response),
806
		// then we assume it's changed. If it's changed, unset the status and the previous response.
807
		if( $local_key !== $response_key ) {
808
809
			unset( $posted_settings['license_key_response'] );
810
			unset( $posted_settings['license_key_status'] );
811
			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...
812
		}
813
814
		return $posted_settings;
815
	}
816
817
	/**
818
	 * Gets the required indicator
819
	 * Gets the markup of the required indicator symbol to highlight fields that are required
820
	 *
821
	 * @param $field - The field meta.
822
	 *
823
	 * @return string - Returns markup of the required indicator symbol
824
	 */
825
	public function get_required_indicator( $field ) {
826
		return '<span class="required" title="' . esc_attr__( 'Required', 'gravityview' ) . '">*</span>';
827
	}
828
829
	/**
830
	 * Specify the settings fields to be rendered on the plugin settings page
831
	 * @return array
832
	 */
833
	public function app_settings_fields() {
834
835
		$default_settings = $this->get_default_settings();
836
837
		$disabled_attribute = GVCommon::has_cap( 'gravityview_edit_settings' ) ? false : 'disabled';
838
839
		$fields = apply_filters( 'gravityview_settings_fields', array(
840
			array(
841
				'name'                => 'license_key',
842
				'required'               => true,
843
				'label'             => __( 'License Key', 'gravityview' ),
844
				'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...
845
				'type'              => 'edd_license',
846
				'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...
847
				'default_value'           => $default_settings['license_key'],
848
				'class'             => ( '' == $this->get_app_setting( 'license_key' ) ) ? 'activate code regular-text edd-license-key' : 'deactivate code regular-text edd-license-key',
849
			),
850
			array(
851
				'name'       => 'license_key_response',
852
				'default_value'  => $default_settings['license_key_response'],
853
				'type'     => 'hidden',
854
			),
855
			array(
856
				'name'       => 'license_key_status',
857
				'default_value'  => $default_settings['license_key_status'],
858
				'type'     => 'hidden',
859
			),
860
			array(
861
				'name'       => 'support-email',
862
				'type'     => 'text',
863
				'validate' => 'email',
864
				'default_value'  => $default_settings['support-email'],
865
				'label'    => __( 'Support Email', 'gravityview' ),
866
				'description' => __( 'In order to provide responses to your support requests, please provide your email address.', 'gravityview' ),
867
				'class'    => 'code regular-text',
868
			),
869
			/**
870
			 * @since 1.15 Added Support Port support
871
			 */
872
			array(
873
				'name'         => 'support_port',
874
				'type'       => 'radio',
875
				'label'      => __( 'Show Support Port?', 'gravityview' ),
876
				'default_value'    => $default_settings['support_port'],
877
				'horizontal' => 1,
878
				'choices'    => array(
879
					array(
880
						'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...
881
						'value' => '1',
882
					),
883
					array(
884
						'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...
885
						'value' => '0',
886
					),
887
				),
888
				'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...
889
				'description'   => __( 'Show the Support Port on GravityView pages?', 'gravityview' ),
890
			),
891
			array(
892
				'name'         => 'no-conflict-mode',
893
				'type'       => 'radio',
894
				'label'      => __( 'No-Conflict Mode', 'gravityview' ),
895
				'default_value'    => $default_settings['no-conflict-mode'],
896
				'horizontal' => 1,
897
				'choices'    => array(
898
					array(
899
						'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...
900
						'value' => '1',
901
					),
902
					array(
903
						'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...
904
						'value' => '0',
905
					),
906
				),
907
				'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...
908
			),
909
		) );
910
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
911
912
913
		/**
914
		 * Redux backward compatibility
915
		 * @since 1.7.4
916
		 */
917
		foreach ( $fields as &$field ) {
918
			$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...
919
			$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...
920
			$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...
921
			$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...
922
923
			if( $disabled_attribute ) {
924
				$field['disabled']  = $disabled_attribute;
925
			}
926
		}
927
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
928
929
        $sections = array(
930
            array(
931
                '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...
932
                'fields'      => $fields,
933
            )
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
934
        );
935
936
        // custom 'update settings' button
937
        $button = array(
938
            'class' => 'button button-primary button-hero',
939
            'type'     => 'save',
940
        );
941
942
		if( $disabled_attribute ) {
943
			$button['disabled'] = $disabled_attribute;
944
		}
945
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
946
947
        /**
948
         * @filter `gravityview/settings/extension/sections` Modify the GravityView settings page
949
         * Extensions can tap in here to insert their own section and settings.
950
         * <code>
951
         *   $sections[] = array(
952
         *      'title' => __( 'GravityView My Extension Settings', 'gravityview' ),
953
         *      'fields' => $settings,
954
         *   );
955
         * </code>
956
         * @param array $extension_settings Empty array, ready for extension settings!
957
         */
958
        $extension_sections = apply_filters( 'gravityview/settings/extension/sections', array() );
959
960
		// If there are extensions, add a section for them
961
		if ( ! empty( $extension_sections ) ) {
962
963
			if( $disabled_attribute ) {
964
				foreach ( $extension_sections as &$section ) {
965
					foreach ( $section['fields'] as &$field ) {
966
						$field['disabled'] = $disabled_attribute;
967
					}
968
				}
969
			}
970
971
            $k = count( $extension_sections ) - 1 ;
972
            $extension_sections[ $k ]['fields'][] = $button;
973
			$sections = array_merge( $sections, $extension_sections );
974
		} else {
975
            // add the 'update settings' button to the general section
976
            $sections[0]['fields'][] = $button;
977
        }
978
979
		return $sections;
980
	}
981
982
	/**
983
	 * Get the setting for GravityView by name
984
	 *
985
	 * @param  string $key     Option key to fetch
986
	 *
987
	 * @return mixed
988
	 */
989
	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...
990
		return self::get_instance()->get_app_setting( $key );
991
	}
992
993
}
994
995
GravityView_Settings::get_instance();