GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-support-port.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @since 1.15
5  */
7 
8  /**
9  * @var string The name of the User Meta option used to store whether a user wants to see the Support Port
10  * @since 1.15
11  */
12  const user_pref_name = 'gravityview_support_port';
13 
14  public function __construct() {
15  $this->add_hooks();
16  }
17 
18  /**
19  * @since 1.15
20  */
21  private function add_hooks() {
22  add_action( 'personal_options', array( $this, 'user_field' ) );
23  add_action( 'personal_options_update', array( $this, 'update_user_meta_value' ) );
24  add_action( 'edit_user_profile_update', array( $this, 'update_user_meta_value' ) );
25  add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_script' ), 1000 );
26  }
27 
28  /**
29  * Enqueue Support Port script if user has it enabled and we're on a GravityView plugin page
30  *
31  * @uses gravityview_is_admin_page()
32  * @uses wp_enqueue_script()
33  * @since 1.15
34  *
35  * @return void
36  */
37  public static function maybe_enqueue_script( $hook ) {
38  global $pagenow;
39 
40  // Don't show if not GravityView page, or if we're on the Widgets page
41  if ( ! gravityview_is_admin_page( $hook ) || $pagenow === 'widgets.php' ) {
42  return;
43  }
44 
45  /**
46  * @filter `gravityview/support_port/display` Whether to display Support Port
47  * @since 1.15
48  * @param boolean $display_beacon Default: `true`
49  */
50  $display_support_port = apply_filters( 'gravityview/support_port/display', self::show_for_user() );
51 
52  if ( empty( $display_support_port ) ) {
53  do_action( 'gravityview_log_debug', __METHOD__ . ' - Not showing Support Port' );
54 
55  return;
56  }
57 
58  $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
59 
60  wp_enqueue_script( 'gravityview-support', plugins_url( 'assets/js/support' . $script_debug . '.js', GRAVITYVIEW_FILE ), array(), GravityView_Plugin::version, true );
61 
62  self::_localize_script();
63  }
64 
65  /**
66  * Localize the Support Port script
67  *
68  * @uses wp_localize_script()
69  * @since 1.15
70  * @return void
71  */
72  private static function _localize_script() {
73 
74  $translation = array(
75  'agentLabel' => __( 'GravityView Support', 'gravityview' ),
76  'searchLabel' => __( 'Search GravityView Docs', 'gravityview' ),
77  'searchErrorLabel' => __( 'Your search timed out. Please double-check your internet connection and try again.', 'gravityview' ),
78  'noResultsLabel' => _x( 'No results found for', 'a support form search has returned empty for the following word', 'gravityview' ),
79  'contactLabel' => __( 'Contact Support', 'gravityview' ),
80  'attachFileLabel' => __( 'Attach a screenshot or file', 'gravityview' ),
81  'attachFileError' => __( 'The maximum file size is 10 MB', 'gravityview' ),
82  'nameLabel' => __( 'Your Name', 'gravityview' ),
83  'nameError' => __( 'Please enter your name', 'gravityview' ),
84  'emailLabel' => __( 'Email address', 'gravityview' ),
85  'emailError' => __( 'Please enter a valid email address', 'gravityview' ),
86  'subjectLabel' => __( 'Subject', 'gravityview' ),
87  'subjectError' => _x( 'Please enter a subject', 'Error shown when submitting support request and there is no subject provided', 'gravityview' ),
88  'messageLabel' => __( 'How can we help you?', 'gravityview' ),
89  'messageError' => _x( 'Please enter a message', 'Error shown when submitting support request and there is no message provided', 'gravityview' ),
90  'contactSuccessLabel' => __( 'Message sent!', 'gravityview' ),
91  'contactSuccessDescription' => __( 'Thanks for reaching out! Someone from the GravityView team will get back to you soon.', 'gravityview' ),
92  );
93 
94  $response = GravityView_Settings::getSetting( 'license_key_response' );
95 
96  $response = wp_parse_args( $response, array(
97  'license' => '',
98  'message' => '',
99  'license_key' => '',
100  'license_limit' => '',
101  'expires' => '',
102  'activations_left' => '',
103  'site_count' => '',
104  'payment_id' => '',
105  'customer_name' => '',
106  'customer_email' => '',
107  ) );
108 
109  // This is just HTML we don't need.
110  unset( $response['message'] );
111 
112  switch ( intval( $response['license_limit'] ) ) {
113  case 1:
114  $package = 'Sol';
115  break;
116  case 100:
117  $package = 'Galactic';
118  break;
119  case 3:
120  $package = 'Interstellar';
121  break;
122  default:
123  $package = sprintf( '%d-Site License', $response['license_limit'] );
124  }
125 
126  $data = array(
127  'email' => GravityView_Settings::getSetting( 'support-email' ),
128  'name' => $response['customer_name'],
129  'Valid License?' => ucwords( $response['license'] ),
130  'License Key' => $response['license_key'],
131  'License Level' => $package,
132  'Site Admin Email' => get_bloginfo( 'admin_email' ),
133  'Support Email' => GravityView_Settings::getSetting( 'support-email' ),
134  'License Limit' => $response['license_limit'],
135  'Site Count' => $response['site_count'],
136  'License Expires' => $response['expires'],
137  'Activations Left' => $response['activations_left'],
138  'Payment ID' => $response['payment_id'],
139  'Payment Name' => $response['customer_name'],
140  'Payment Email' => $response['customer_email'],
141  'WordPress Version' => get_bloginfo( 'version', 'display' ),
142  'PHP Version' => phpversion(),
143  'GravityView Version' => GravityView_Plugin::version,
144  'Gravity Forms Version' => GFForms::$version,
145  'Plugins & Extensions' => GV_License_Handler::get_related_plugins_and_extensions(),
146  );
147 
148  $localization_data = array(
149  'contactEnabled' => (int)GVCommon::has_cap( 'gravityview_contact_support' ),
150  'data' => $data,
151  'translation' => $translation,
152  );
153 
154  wp_localize_script( 'gravityview-support', 'gvSupport', $localization_data );
155 
156  unset( $localization_data, $data, $translation, $response, $package );
157  }
158 
159  /**
160  * Check whether to show Support for a user
161  *
162  * If the user doesn't have the `gravityview_support_port` capability, returns false; then
163  * If global setting is "hide", returns false; then
164  * If user preference is not set, return global setting; then
165  * If user preference is set, return that setting.
166  *
167  * @since 1.15
168  * @since 1.17.5 Changed behavior to respect global setting
169  *
170  * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
171  *
172  * @return bool Whether to show GravityView support port
173  */
174  static public function show_for_user( $user = 0 ) {
175 
176  if ( ! GVCommon::has_cap( 'gravityview_support_port' ) ) {
177  return false;
178  }
179 
180  $global_setting = GravityView_Settings::getSetting( 'support_port' );
181 
182  if ( empty( $global_setting ) ) {
183  return false;
184  }
185 
186  // Get the per-user Support Port setting
187  $user_pref = get_user_option( self::user_pref_name, $user );
188 
189  // Not configured; default to global setting (which is true at this point)
190  if ( false === $user_pref ) {
191  $user_pref = $global_setting;
192  }
193 
194  return ! empty( $user_pref );
195  }
196 
197 
198  /**
199  * Update User Profile preferences for GravityView Support
200  *
201  * @since 1.5
202  *
203  * @param int $user_id
204  *
205  * @return void
206  */
207  public function update_user_meta_value( $user_id ) {
208  if ( current_user_can( 'edit_user', $user_id ) && isset( $_POST[ self::user_pref_name ] ) ) {
209  update_user_meta( $user_id, self::user_pref_name, intval( $_POST[ self::user_pref_name ] ) );
210  }
211  }
212 
213  /**
214  * Modify User Profile
215  *
216  * Modifies the output of profile.php to add GravityView Support preference
217  *
218  * @since 1.15
219  * @since 1.17.5 Only show if global setting is active
220  *
221  * @param WP_User $user Current user info
222  *
223  * @return void
224  */
225  public function user_field( $user ) {
226 
227  $global_setting = GravityView_Settings::getSetting( 'support_port' );
228 
229  if ( empty( $global_setting ) ) {
230  return;
231  }
232 
233  /**
234  * @filter `gravityview/support_port/show_profile_setting` Should the "GravityView Support Port" setting be shown on user profiles?
235  * @todo use GVCommon::has_cap() after merge
236  * @since 1.15
237  *
238  * @param boolean $allow_profile_setting Default: `true`, if the user has the `gravityview_support_port` capability, which defaults to true for Contributors and higher
239  * @param WP_User $user Current user object
240  */
241  $allow_profile_setting = apply_filters( 'gravityview/support_port/show_profile_setting', current_user_can( 'gravityview_support_port' ), $user );
242 
243  if ( $allow_profile_setting && current_user_can( 'edit_user', $user->ID ) ) {
244  ?>
245  <table class="form-table">
246  <tbody>
247  <tr class="user-gravityview-support-button-wrap">
248  <th scope="row"><?php
249  /* translators: "Support Port" can be translated as "Support Portal" or "Support Window" */
250  _e( 'GravityView Support Port', 'gravityview' );
251  ?></th>
252  <td>
253  <fieldset>
254  <legend class="screen-reader-text"><span><?php
255  /* translators: "Support Port" can be translated as "Support Portal" or "Support Window" */
256  _e( 'GravityView Support Port', 'gravityview' );
257  ?></span></legend>
258  <label>
259  <input name="<?php echo esc_attr( self::user_pref_name ); ?>" type="hidden" value="0"/>
260  <input name="<?php echo esc_attr( self::user_pref_name ); ?>" type="checkbox" value="1" <?php checked( self::show_for_user( $user->ID ) ); ?> />
261  <?php esc_html_e( 'Show GravityView Support Port when on a GravityView-related page', 'gravityview' ); ?>
262  </label>
263  </fieldset>
264  </td>
265  </tr>
266  </tbody>
267  </table>
268  <?php }
269  }
270 }
271 
update_user_meta_value($user_id)
Update User Profile preferences for GravityView Support.
static _localize_script()
Localize the Support Port script.
static has_cap($caps= '', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
const GRAVITYVIEW_FILE(!defined( 'ABSPATH'))
Plugin Name: GravityView Plugin URI: https://gravityview.co Description: The best, easiest way to display Gravity Forms entries on your website.
Definition: gravityview.php:26
user_field($user)
Modify User Profile.
gravityview_is_admin_page($hook= '', $page=NULL)
Alias for GravityView_Admin::is_admin_page()
static getSetting($key)
Get the setting for GravityView by name.
static get_related_plugins_and_extensions($implode= '< br/>')
Get active GravityView Extensions and Gravity Forms Add-ons to help debug issues. ...
static show_for_user($user=0)
Check whether to show Support for a user.
static maybe_enqueue_script($hook)
Enqueue Support Port script if user has it enabled and we&#39;re on a GravityView plugin page...