GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-settings-plugin.php
Go to the documentation of this file.
1 <?php
2 
3 namespace GV;
4 
9 
10 /**
11  * Ex-GF Addon Settings class that's been stripped of GF functionality.
12  * Serves as a wrapper for Foundation Settings while also providing old getter/setter methods for backward compatibility.
13  */
15  const SETTINGS_PLUGIN_ID = 'gravityview';
16 
17  /**
18  * Foundation Settings framework instance.
19  *
20  * @since 2.16
21  *
22  * @var SettingsFramework|GravityKitFoundation::settings
23  */
25 
26  /**
27  * Cached settings.
28  *
29  * @since 2.16.2
30  *
31  * @var array
32  */
33  private $_plugin_settings = [];
34 
35  public function __construct() {
36  // GravityKitFoundation may not yet be available when this class is instantiated, so let's temporarily use the Settings framework from Foundation that's included with GravityView and then possibly replace it with the latest version.
37  $this->_foundation_settings = SettingsFramework::get_instance();
38 
39  add_action( 'gk/foundation/initialized', function () {
40  $this->_foundation_settings = GravityKitFoundation::settings();
41  } );
42 
43  add_filter( 'gk/foundation/settings/data/plugins', [ $this, 'add_settings' ] );
44  }
45 
46  /**
47  * Returns a setting.
48  *
49  * @param string $setting_name The setting key.
50  *
51  * @return mixed The setting or null
52  * @deprecated Use \GV\Plugin_Settings::get()
53  */
54  public function get_app_setting( $setting_name ) {
55  return $this->get( $setting_name );
56  }
57 
58  /**
59  * Returns setting by its name.
60  *
61  * @param string $key The setting key.
62  * @param string $default (optional) A default if not found.
63  *
64  * @return mixed The setting value.
65  */
66  public function get( $key, $default = null ) {
67  return Arr::get( $this->all(), $key, $default );
68  }
69 
70  /**
71  * Returns setting by its name.
72  *
73  * @param string $key Option key to fetch
74  *
75  * @return mixed
76  * @deprecated Use gravityview()->plugin->settings->get()
77  */
78  static public function getSetting( $key ) {
79  if ( gravityview()->plugin->settings instanceof Plugin_Settings ) {
80  return gravityview()->plugin->settings->get( $key );
81  }
82  }
83 
84  /**
85  * Returns all settings.
86  *
87  * @return array The settings.
88  * @deprecated Use \GV\Plugin_Settings::all() or \GV\Plugin_Settings::get()
89  */
90  public function get_app_settings() {
91  return $this->all();
92  }
93 
94  /**
95  * Returns all settings.
96  *
97  * @return array The settings.
98  */
99  public function all() {
100  if ( ! empty( $this->_plugin_settings ) ) {
101  return $this->_plugin_settings;
102  }
103 
104  $this->_plugin_settings = $this->_foundation_settings->get_plugin_settings( self::SETTINGS_PLUGIN_ID );
105 
106  // Migrate legacy settings
107  if ( empty( $this->_plugin_settings ) ) {
108  $this->_plugin_settings = $this->migrate_legacy_settings();
109  }
110 
111  $this->_plugin_settings = wp_parse_args( $this->_plugin_settings, $this->defaults() );
112 
113  return $this->_plugin_settings;
114  }
115 
116  /**
117  * Migrates GravityView <2.16 settings to the new settings framework.
118  * Some of those settings are now part of the GravityKit general settings, and some are part of the GravityView plugin settings.
119  *
120  * @return array The settings.
121  */
122  public function migrate_legacy_settings() {
123  $option_name = 'gravityformsaddon_' . self::SETTINGS_PLUGIN_ID . '_app_settings';
124 
125  // Legacy check (@see https://github.com/gravityview/GravityView/blob/3719151f3752ebca56b5ec70bd4064effcb7094a/future/includes/class-gv-settings-addon.php#L939)
126  $site_has_settings = ( ! is_multisite() ) || is_main_site() || ( ! gravityview()->plugin->is_network_activated() ) || ( is_network_admin() && gravityview()->plugin->is_network_activated() );
127 
128  if ( $site_has_settings ) {
129  $legacy_settings = get_option( $option_name, array() );
130  } else {
131  $legacy_settings = get_blog_option( get_main_site_id(), $option_name );
132  }
133 
134  if ( empty( $legacy_settings ) ) {
135  return [];
136  }
137 
138  // Migrate legacy GravityView settings that are still part of GravityView.
139  $plugin_settings = [
140  'rest_api' => (int) Arr::get( $legacy_settings, 'rest_api' ),
141  ];
142 
143  $this->_foundation_settings->save_plugin_settings( self::SETTINGS_PLUGIN_ID, $plugin_settings );
144 
145  // Migrate legacy GravityView settings that are now part of the GravityKit general settings.
146  $gk_settings_id = class_exists( 'GravityKitFoundation' ) ? GravityKitFoundation::ID : FoundationCore::ID;
147 
148  // If there are no settings configured, this would typically return an array of default settings.
149  // However, because GV\Plugin_Settings is used before Foundation initializes and configures default settings, this will return an empty array indicating that migration should proceed.
150  $gk_settings = $this->_foundation_settings->get_plugin_settings( $gk_settings_id );
151 
152  if ( empty( $gk_settings ) ) {
153  $gk_settings = [
154  'support_email' => Arr::get( $legacy_settings, 'support-email' ),
155  'support_port' => Arr::get( $legacy_settings, 'support_port' ),
156  'powered_by' => (int) Arr::get( $legacy_settings, 'powered_by' ),
157  'affiliate_id' => Arr::get( $legacy_settings, 'affiliate_id' ),
158  'beta' => (int) Arr::get( $legacy_settings, 'beta' ),
159  'support_email' => Arr::get( $legacy_settings, 'support-email' ),
160  'support_port' => (int) Arr::get( $legacy_settings, 'support_port' ),
161  'no_conflict_mode' => (int) Arr::get( $legacy_settings, 'no-conflict-mode' ),
162  ];
163 
164  $this->_foundation_settings->save_plugin_settings( $gk_settings_id, $gk_settings );
165  }
166 
167  return $plugin_settings;
168  }
169 
170  /**
171  * Returns a GravityKit general setting.
172  *
173  * @param string $setting Setting value.
174  * @param mixed|null $default (optional) A default value if setting is not set. Defaults to null.
175  *
176  * @return mixed
177  */
178  public function get_gravitykit_setting( $setting, $default = null ) {
179  $gk_settings_id = class_exists( 'GravityKitFoundation' ) ? GravityKitFoundation::ID : FoundationCore::ID;
180 
181  return $this->_foundation_settings->get_plugin_setting( $gk_settings_id, $setting, $default );
182  }
183 
184  /**
185  * Returns default settings.
186  *
187  * @return array The defaults.
188  */
189  public function defaults() {
190  $defaults = [
191  'rest_api' => 0,
192  'public_entry_moderation' => 0,
193  ];
194 
195  /**
196  * @filter `gravityview/settings/default` Filter default global settings.
197  *
198  * @param [in,out] array The defaults.
199  */
200  return apply_filters( 'gravityview/settings/defaults', $defaults );
201  }
202 
203  /**
204  * Adds GravityView settings to Foundation.
205  *
206  * @since 2.16
207  *
208  * @param array $plugins_data Plugins data
209  *
210  * @return array $plugins_data
211  */
212  public function add_settings( $plugins_data ) {
213  /**
214  * Override whether to show GravityView settings.
215  *
216  * @since 1.7.6
217  *
218  * @param bool $show_settings Default: true
219  */
220  $show_settings = apply_filters( 'gravityview/show-settings-menu', true );
221 
222  if ( ! $show_settings ) {
223  return $plugins_data;
224  }
225 
226  $default_settings = $this->defaults();
227 
228  $settings = [
229  'id' => self::SETTINGS_PLUGIN_ID,
230  'title' => 'GravityView',
231  'defaults' => $default_settings,
232  'icon' => 'data:image/svg+xml,%3Csvg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg"%3E%3Crect width="80" height="80" rx="8" fill="%23FF1B67"/%3E%3Cg clip-path="url(%23clip0_16_27)"%3E%3Cpath fill-rule="evenodd" clip-rule="evenodd" d="M58.4105 54.6666H52.8824V56.4999C52.8824 59.5375 50.3902 61.9999 47.3544 61.9999H32.6134C29.5375 61.9999 27.0853 59.5375 27.0853 56.4999V29H21.5572C20.5144 29 19.7148 29.8207 19.7148 30.8333V49.1666C19.7148 50.1792 20.5144 51 21.5572 51H22.4788C22.9629 51 23.3997 51.4104 23.3997 51.9167V53.75C23.3997 54.2562 22.9629 54.6666 22.4788 54.6666H21.5572C18.4786 54.6666 16.0292 52.2042 16.0292 49.1666V30.8333C16.0292 27.7957 18.4786 25.3333 21.5572 25.3333H27.0853V23.5C27.0853 20.4624 29.5375 18 32.6134 18H47.3544C50.3902 18 52.8824 20.4624 52.8824 23.5V51H58.4105C59.4132 51 60.2529 50.1792 60.2529 49.1666V30.8333C60.2529 29.8207 59.4132 29 58.4105 29H57.4889C56.9647 29 56.5673 28.5896 56.5673 28.0833V26.2499C56.5673 25.7437 56.9647 25.3333 57.4889 25.3333H58.4105C61.449 25.3333 63.9378 27.7957 63.9378 30.8333V49.1666C63.9378 52.2042 61.449 54.6666 58.4105 54.6666ZM49.1968 23.5C49.1968 22.4874 48.3544 21.6667 47.3544 21.6667H32.6134C31.5733 21.6667 30.7709 22.4874 30.7709 23.5V56.4999C30.7709 57.5125 31.5733 58.3333 32.6134 58.3333H47.3544C48.3544 58.3333 49.1968 57.5125 49.1968 56.4999V23.5Z" fill="white"/%3E%3C/g%3E%3Cdefs%3E%3CclipPath id="clip0_16_27"%3E%3Crect width="48" height="44" fill="white" transform="translate(16 18)"/%3E%3C/clipPath%3E%3C/defs%3E%3C/svg%3E%0A',
233  'sections' => [
234  [
235  'title' => esc_html__( 'General', 'gk-gravityview' ),
236  'settings' => [
237  [
238  'id' => 'rest_api',
239  'type' => 'checkbox',
240  'title' => esc_html__( 'REST API', 'gk-gravityview' ),
241  'description' => esc_html__( 'Enable View and Entry access via the REST API? Regular per-View restrictions apply (private, password protected, etc.).', 'gk-gravityview' ) . ' ' . esc_html__( 'If you are unsure, disable this setting.', 'gk-gravityview' ),
242  'value' => $this->get( 'rest_api', $default_settings['rest_api'] ),
243  ],
244  ],
245  ],
246  [
247  'title' => esc_html__( 'Permissions', 'gk-gravityview' ),
248  'settings' => [
249  [
250  'id' => 'public_entry_moderation',
251  'type' => 'checkbox',
252  'title' => esc_html__( 'Enable Public Entry Moderation', 'gk-gravityview' ),
253  'description' => strtr(
254  // translators: Do not translate the words inside the {} curly brackets; they are replaced.
255  __( 'If enabled, adding {public} to {link}entry moderation merge tags{/link} will allow logged-out users to approve or reject entries. If disabled, all entry moderation actions require the user to be logged-in and have the ability to edit the entry.', 'gk-gravityview' ),
256  array(
257  '{public}' => '<code style="font-size: .9em">:public</code>',
258  '{link}' => '<a href="https://docs.gravitykit.com/article/904-entry-moderation-merge-tags" target="_blank" rel="noopener noreferrer">',
259  '{/link}' => '<span class="screen-reader-text"> ' . esc_html__( '(This link opens in a new window.)', 'gk-gravityview' ) . '</span></a>',
260  )
261  ),
262  'value' => $this->get( 'public_entry_moderation', $default_settings['public_entry_moderation'] ),
263  ],
264  ],
265  ],
266  ],
267  ];
268 
269  return array_merge( (array) $plugins_data, [ self::SETTINGS_PLUGIN_ID => $settings ] );
270  }
271 
272  /**
273  * Updates app settings with the provided settings.
274  *
275  * Same as the GFAddon, except it returns the value from update_option()
276  *
277  * @param array $settings - App settings to be saved
278  *
279  * @return boolean False if value was not updated and true if value was updated.
280  * @deprecated Use \GV\Settings::set() or \GV\Settings::update()
281  */
282  public function update_app_settings( $settings ) {
283  return $this->update( $settings );
284  }
285 
286  /**
287  * Sets a subset of settings.
288  *
289  * @param array|string An array of settings to update, or string (key) and $value to update one setting.
290  * @param mixed $value A value if $settings is string (key). Default: null.
291  */
292  public function set( $settings, $value = null ) {
293  if ( is_string( $settings ) ) {
294  $settings = [ $settings => $value ];
295  }
296 
297  $settings = wp_parse_args( $settings, $this->all() );
298 
299  return $this->_foundation_settings->save_plugin_settings( self::SETTINGS_PLUGIN_ID, $settings );
300  }
301 
302  /**
303  * Updates settings.
304  *
305  * @param array $settings The settings array.
306  *
307  * @return boolean False if value was not updated and true if value was updated.
308  */
309  public function update( $settings ) {
310  $result = $this->_foundation_settings->save_plugin_settings( self::SETTINGS_PLUGIN_ID, $settings );
311 
312  if ( ! $result ) {
313  return false;
314  }
315 
316  $this->_plugin_settings = $this->_foundation_settings->get_plugin_settings( self::SETTINGS_PLUGIN_ID );
317 
318  return true;
319  }
320 
321  /**
322  * Check for the `gravityview_edit_settings` capability before saving plugin settings.
323  * Gravity Forms says you're able to edit if you're able to view settings. GravityView allows two different permissions.
324  *
325  * @since 1.15
326  * @return void
327  */
328  public function maybe_save_app_settings() {
329  if ( $this->is_save_postback() ) {
330  if ( ! \GVCommon::has_cap( 'gravityview_edit_settings' ) ) {
331  $_POST = []; // If you don't reset the $_POST array, it *looks* like the settings were changed, but they weren't
332  \GFCommon::add_error_message( __( 'You don\'t have the ability to edit plugin settings.', 'gk-gravityview' ) );
333 
334  return;
335  }
336  }
337  }
338 
339  /*
340  * @TODO Reimplement elsewhere.
341  */
342  private function _uninstall_warning_message() {
343  $heading = esc_html__( 'If you delete then re-install GravityView, it will be like installing GravityView for the first time.', 'gk-gravityview' );
344  $message = esc_html__( 'Delete all Views, GravityView entry approval status, GravityView-generated entry notes (including approval and entry creator changes), and GravityView plugin settings.', 'gk-gravityview' );
345 
346  return sprintf( '<h4>%s</h4><p>%s</p>', $heading, $message );
347  }
348 
349  /**
350  * @TODO Reimplement elsewhere.
351  */
352  private function _get_uninstall_reasons() {
353  $reasons = [
354  'will-continue' => [
355  'label' => esc_html__( 'I am going to continue using GravityView', 'gk-gravityview' ),
356  ],
357  'no-longer-need' => [
358  'label' => esc_html__( 'I no longer need GravityView', 'gk-gravityview' ),
359  ],
360  'doesnt-work' => [
361  'label' => esc_html__( 'The plugin doesn\'t work', 'gk-gravityview' ),
362  ],
363  'found-other' => [
364  'label' => esc_html__( 'I found a better plugin', 'gk-gravityview' ),
365  'followup' => esc_attr__( 'What plugin you are using, and why?', 'gk-gravityview' ),
366  ],
367  'other' => [
368  'label' => esc_html__( 'Other', 'gk-gravityview' ),
369  ],
370  ];
371 
372  shuffle( $reasons );
373 
374  return $reasons;
375  }
376 
377  /**
378  * @TODO Reimplement elsewhere.
379  */
380  private function _uninstall_form() {
381  ob_start();
382 
383  $user = wp_get_current_user();
384  ?>
385  <style>
386  #gv-reason-details {
387  min-height: 100px;
388  }
389 
390  .number-scale label {
391  border: 1px solid #cccccc;
392  padding: .5em .75em;
393  margin: .1em;
394  }
395 
396  #gv-uninstall-thanks p {
397  font-size: 1.2em;
398  }
399 
400  .scale-description ul {
401  margin-bottom: 0;
402  padding-bottom: 0;
403  }
404 
405  .scale-description p.description {
406  margin-top: 0 !important;
407  padding-top: 0 !important;
408  }
409 
410  .gv-form-field-wrapper {
411  margin-top: 30px;
412  }
413  </style>
414 
415  <?php
416  if ( gravityview()->plugin->is_GF_25() ) {
417  $uninstall_title = esc_html__( 'Uninstall GravityView', 'gk-gravityview' );
418 
419  echo <<<HTML
420 <div class="gform-settings-panel">
421  <header class="gform-settings-panel__header">
422  <h4 class="gform-settings-panel__title">{$uninstall_title}</h4>
423  </header>
424  <div class="gform-settings-panel__content" style="padding: 0 1rem 1.25rem">
425 
426 HTML;
427  } else {
428  echo '<div class="gv-uninstall-form-wrapper" style="font-size: 110%; padding: 15px 0;">';
429  }
430  ?>
431  <script>
432  jQuery( function ( $ ) {
433  $( '#gv-uninstall-feedback' ).on( 'change', function ( e ) {
434 
435  if ( !$( e.target ).is( ':input' ) ) {
436  return;
437  }
438  var $textarea = $( '.gv-followup' ).find( 'textarea' );
439  var followup_text = $( e.target ).attr( 'data-followup' );
440  if ( !followup_text ) {
441  followup_text = $textarea.attr( 'data-default' );
442  }
443 
444  $textarea.attr( 'placeholder', followup_text );
445 
446  } ).on( 'submit', function ( e ) {
447  e.preventDefault();
448 
449  $.post( $( this ).attr( 'action' ), $( this ).serialize() )
450  .done( function ( data ) {
451  if ( 'success' !== data.status ) {
452  gv_feedback_append_error_message();
453  } else {
454  $( '#gv-uninstall-thanks' ).fadeIn();
455  }
456  } )
457  .fail( function ( data ) {
458  gv_feedback_append_error_message();
459  } )
460  .always( function () {
461  $( e.target ).remove();
462  } );
463 
464  return false;
465  } );
466 
467  function gv_feedback_append_error_message() {
468  $( '#gv-uninstall-thanks' ).append( '<div class="notice error">' + <?php echo json_encode( esc_html( __( 'There was an error sharing your feedback. Sorry! Please email us at [email protected]', 'gk-gravityview' ) ) ) ?> +'</div>' );
469  }
470  } );
471  </script>
472 
473  <form id="gv-uninstall-feedback" method="post" action="https://hooks.zapier.com/hooks/catch/28670/6haevn/">
474  <h2><?php esc_html_e( 'Why did you uninstall GravityView?', 'gk-gravityview' ); ?></h2>
475  <ul>
476  <?php
477  $reasons = $this->get_uninstall_reasons();
478  foreach ( $reasons as $reason ) {
479  printf( '<li><label><input name="reason" type="radio" value="other" data-followup="%s"> %s</label></li>', Arr::get( $reason, 'followup' ), Arr::get( $reason, 'label' ) );
480  }
481  ?>
482  </ul>
483  <div class="gv-followup widefat">
484  <p><strong><label for="gv-reason-details"><?php esc_html_e( 'Comments', 'gk-gravityview' ); ?></label></strong></p>
485  <textarea id="gv-reason-details" name="reason_details" data-default="<?php esc_attr_e( 'Please share your thoughts about GravityView', 'gk-gravityview' ) ?>" placeholder="<?php esc_attr_e( 'Please share your thoughts about GravityView', 'gk-gravityview' ); ?>" class="large-text"></textarea>
486  </div>
487  <div class="scale-description">
488  <p><strong><?php esc_html_e( 'How likely are you to recommend GravityView?', 'gk-gravityview' ); ?></strong></p>
489  <ul class="inline">
490  <?php
491  $i = 0;
492  while ( $i < 11 ) {
493  echo '<li class="inline number-scale"><label><input name="likely_to_refer" id="likely_to_refer_' . $i . '" value="' . $i . '" type="radio"> ' . $i . '</label></li>';
494  $i++;
495  }
496  ?>
497  </ul>
498  <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)', 'gk-gravityview' ), '<label for="likely_to_refer_0"><code>0</code></label>', '<label for="likely_to_refer_10"><code>10</code></label>' ); ?></p>
499  </div>
500 
501  <div class="gv-form-field-wrapper">
502  <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', 'gk-gravityview' ); ?></label>
503  </div>
504 
505  <div class="submit">
506  <input type="hidden" name="siteurl" value="<?php echo esc_url( get_bloginfo( 'url' ) ); ?>" />
507  <input type="hidden" name="email" value="<?php echo esc_attr( $user->user_email ); ?>" />
508  <input type="hidden" name="display_name" value="<?php echo esc_attr( $user->display_name ); ?>" />
509  <input type="submit" value="<?php esc_html_e( 'Send Us Your Feedback', 'gk-gravityview' ); ?>" class="button button-primary primary button-hero" />
510  </div>
511  </form>
512 
513  <div id="gv-uninstall-thanks" class="<?php echo ( gravityview()->plugin->is_GF_25() ) ? 'notice-large' : 'notice notice-large notice-updated below-h2'; ?>" style="display:none;">
514  <h3 class="notice-title"><?php esc_html_e( 'Thank you for using GravityView!', 'gk-gravityview' ); ?></h3>
515  <p><?php echo gravityview_get_floaty(); ?>
516  <?php echo make_clickable( esc_html__( 'Your feedback helps us improve GravityView. If you have any questions or comments, email us: [email protected]', 'gk-gravityview' ) ); ?>
517  </p>
518  <div class="wp-clearfix"></div>
519  </div>
520  </div>
521  <?php
522  if ( gravityview()->plugin->is_GF_25() ) {
523  echo '</div>';
524  }
525 
526  $form = ob_get_clean();
527 
528  return $form;
529  }
530 }
get_app_setting( $setting_name)
Returns a setting.
gravityview_get_floaty( $height=87, $css_class=null)
Get an image of our intrepid explorer friend.
static getSetting( $key)
Returns setting by its name.
get_app_settings()
Returns all settings.
if(empty( $value)) $user
_uninstall_form()
Reimplement elsewhere.
if(gravityview() ->plugin->is_GF_25()) $form
migrate_legacy_settings()
Migrates GravityView <2.16 settings to the new settings framework.
update_app_settings( $settings)
Updates app settings with the provided settings.
update( $settings)
Updates settings.
_get_uninstall_reasons()
Reimplement elsewhere.
scale description p description
all()
Returns all settings.
get_gravitykit_setting( $setting, $default=null)
Returns a GravityKit general setting.
defaults()
Returns default settings.
scale description ul
maybe_save_app_settings()
Check for the gravityview_edit_settings capability before saving plugin settings. ...
gv form field wrapper
gravityview()
The main GravityView wrapper function.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
Ex-GF Addon Settings class that&#39;s been stripped of GF functionality.
add_settings( $plugins_data)
Adds GravityView settings to Foundation.