GravityView  2.10.1
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  /**
15  * @var string The hash key used to generate secure message history
16  * @since 2.2.5
17  */
18  const beacon_key = 'lCXlwbQR707kipR+J0MCqcxrhGOHjGF0ldD6yNbGM0w=';
19 
20  public function __construct() {
21  $this->add_hooks();
22  }
23 
24  /**
25  * @since 1.15
26  */
27  private function add_hooks() {
28  add_action( 'personal_options', array( $this, 'user_field' ) );
29  add_action( 'personal_options_update', array( $this, 'update_user_meta_value' ) );
30  add_action( 'edit_user_profile_update', array( $this, 'update_user_meta_value' ) );
31  add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_script' ), 1000 );
32  add_filter( 'gravityview/tooltips/tooltip', array( $this, 'maybe_add_article_to_tooltip' ), 10, 6 );
33  }
34 
35  /**
36  * Modify tooltips to add Beacon article
37  *
38  * @since 2.8.1
39  *
40  * @param string $tooltip HTML of original tooltip
41  * @param array $article Optional. Details about support doc article connected to the tooltip. {
42  * @type string $id Unique ID of article for Beacon API
43  * @type string $url URL of support doc article
44  * @type string $type Type of Beacon element to open. {@see https://developer.helpscout.com/beacon-2/web/javascript-api/#beaconarticle}
45  * }
46  * @param string $url
47  * @param string $atts
48  * @param string $css_class
49  * @param string $anchor_text
50  * @param string $link_text
51  *
52  * @return string If no article information exists, original tooltip. Otherwise, modified!
53  */
54  public function maybe_add_article_to_tooltip( $tooltip = '', $article = array(), $url = '', $atts = '', $css_class = '', $anchor_text = '' ) {
55 
56  if ( empty( $article['id'] ) ) {
57  return $tooltip;
58  }
59 
60  static $show_support_port;
61 
62  if ( ! isset( $show_support_port ) ) {
63  $show_support_port = self::show_for_user();
64  }
65 
66  if ( ! $show_support_port ) {
67  return $tooltip;
68  }
69 
70  $css_class .= ' gv_tooltip';
71 
72  if ( ! empty( $article['type'] ) ) {
73  $atts = sprintf( 'data-beacon-article-%s="%s"', $article['type'], $article['id'] );
74  } else {
75  $atts = sprintf( 'data-beacon-article="%s"', $article['id'] );
76  }
77 
78  $url = \GV\Utils::get( $article, 'url', '#' );
79  $anchor_text .= '<p class="description" style="font-size: 15px; text-align: center;"><strong>' . sprintf( esc_html__( 'Click %s icon for additional information.', 'gravityview' ), '<i class=\'fa fa-question-circle\'></i>' ) . '</strong></p>';
80  $link_text = esc_html__( 'Learn More', 'gravityview' );
81 
82  return sprintf( '<a href="%s" %s class="%s" title="%s" role="button">%s</a>',
83  esc_url( $url ),
84  $atts,
85  $css_class,
86  esc_attr( $anchor_text ),
88  );
89  }
90 
91  /**
92  * Enqueue Support Port script if user has it enabled and we're on a GravityView plugin page
93  *
94  * @uses \GV\Admin_Request::is_admin()
95  * @uses wp_enqueue_script()
96  * @since 1.15
97  *
98  * @return void
99  */
100  public static function maybe_enqueue_script( $hook ) {
101  global $pagenow;
102 
103  // Don't show if not GravityView page, or if we're on the Widgets page
104  if ( ! gravityview()->request->is_admin( $hook ) || $pagenow === 'widgets.php' ) {
105  return;
106  }
107 
108  /**
109  * @filter `gravityview/support_port/display` Whether to display Support Port
110  * @since 1.15
111  * @param boolean $display_beacon Default: `true`
112  */
113  $display_support_port = apply_filters( 'gravityview/support_port/display', self::show_for_user() );
114 
115  if ( empty( $display_support_port ) ) {
116  gravityview()->log->debug( 'Not showing Support Port' );
117 
118  return;
119  }
120 
121  $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
122 
123  wp_enqueue_script( 'gravityview-support', plugins_url( 'assets/js/support' . $script_debug . '.js', GRAVITYVIEW_FILE ), array(), \GV\Plugin::$version, true );
124 
125  self::_localize_script();
126  }
127 
128  /**
129  * Localize the Support Port script
130  *
131  * @uses wp_localize_script()
132  * @since 1.15
133  * @return void
134  */
135  private static function _localize_script() {
136 
137  $translation = array(
138  'agentLabel' => __( 'GravityView Support', 'gravityview' ),
139  'searchLabel' => __( 'Search GravityView Docs', 'gravityview' ),
140  'docsSearchErrorText' => __( 'Your search timed out. Please double-check your internet connection and try again.', 'gravityview' ),
141  'noResultsLabel' => _x( 'No results found for', 'a support form search has returned empty for the following word', 'gravityview' ),
142  'contactLabel' => __( 'Contact Support', 'gravityview' ),
143  'attachAFile' => __( 'Attach a screenshot or file', 'gravityview' ),
144  'attachmentSizeErrorText' => __( 'The maximum file size is 10 MB', 'gravityview' ),
145  'nameLabel' => __( 'Your Name', 'gravityview' ),
146  'nameError' => __( 'Please enter your name', 'gravityview' ),
147  'emailLabel' => __( 'Email address', 'gravityview' ),
148  'emailValidationLabel' => __( 'Please enter a valid email address', 'gravityview' ),
149  'subjectLabel' => __( 'Subject', 'gravityview' ),
150  'subjectError' => _x( 'Please enter a subject', 'Error shown when submitting support request and there is no subject provided', 'gravityview' ),
151  'messageLabel' => __( 'How can we help you?', 'gravityview' ),
152  'messageError' => _x( 'Please enter a message', 'Error shown when submitting support request and there is no message provided', 'gravityview' ),
153  'weAreOnIt' => __( 'Message sent!', 'gravityview' ),
154  'contactSuccessDescription' => __( 'Thanks for reaching out! Someone from the GravityView team will get back to you soon.', 'gravityview' ),
155  'needHelp' => __( 'Need Help?', 'gravityview' ),
156  );
157 
158  $response = gravityview()->plugin->settings->get( 'license_key_response' );
159 
160  $response = wp_parse_args( $response, array(
161  'license' => '',
162  'message' => '',
163  'license_key' => '',
164  'license_limit' => '',
165  'expires' => '',
166  'activations_left' => '',
167  'site_count' => '',
168  'payment_id' => '',
169  'customer_name' => '',
170  'customer_email' => '',
171  'price_id' => '0',
172  ) );
173 
174  // This is just HTML we don't need.
175  unset( $response['message'] );
176 
177  switch ( intval( $response['price_id'] ) ) {
178  default:
179  case 1:
180  $package = 'Core';
181  break;
182  case 2:
183  $package = 'Extensions';
184  break;
185  case 3:
186  $package = 'All Access';
187  break;
188  case 4:
189  $package = 'Lifetime';
190  break;
191  }
192 
193  $current_user = wp_get_current_user();
194  $no_conflict_mode = gravityview()->plugin->settings->get( 'no-conflict-mode' );
195 
196  // Prevent any PHP warnings
197  ob_start();
198 
199  $data = array(
200  'email' => $current_user->user_email,
201  'name' => mb_substr( $current_user->display_name, 0, 80 ),
202  'signature' => hash_hmac( 'sha256', $current_user->user_email, self::beacon_key ),
203  'affiliate_id' => gravityview()->plugin->settings->get( 'affiliate_id' ),
204  'is_super_admin' => is_super_admin(),
205  'License Key' => $response['license_key'] . ' (' . ucwords( $response['license'] ) . ')',
206  'License Level' => $package,
207  'Alt Emails' => sprintf( "Admin: %s, GV Support: %s", get_bloginfo( 'admin_email' ), gravityview()->plugin->settings->get( 'support-email' ) ),
208  'Payment Details' => $response['customer_name'] . ' ' . $response['customer_email'],
209  'WordPress Version' => get_bloginfo( 'version', 'display' ),
210  'PHP Version' => phpversion() . ' on ' . esc_html( $_SERVER['SERVER_SOFTWARE'] ),
211  'No-Conflict Mode' => empty( $no_conflict_mode ) ? 'Disabled' : 'Enabled',
212  'GravityView Version' => \GV\Plugin::$version,
213  'Gravity Forms Version' => GFForms::$version,
214  );
215 
216  // End prevent any PHP warnings
217  ob_get_clean();
218 
219  // Help Scout length limit is 200 characters
220  foreach( $data as $key => $value ) {
221  if ( ! is_string( $value ) ) {
222  continue;
223  }
224  $data[ $key ] = mb_substr( $value, 0, 200 );
225  }
226 
227  $localization_data = array(
228  'contactEnabled' => (int)GVCommon::has_cap( 'gravityview_contact_support' ),
229  'data' => $data,
230  'translation' => $translation,
231  'suggest' => array(),
232  );
233 
234  /**
235  * @filter `gravityview/support_port/localization_data` Filter data passed to the Support Port, before localize_script is run
236  * @since 2.0
237  * @param array $localization_data {
238  * @type int $contactEnabled Can the user contact support?
239  * @type array $data Support/license info
240  * @type array $translation i18n strings
241  * @type array $suggest Article IDs to recommend to the user (per page in the admin
242  * }
243  */
244  $localization_data = apply_filters( 'gravityview/support_port/localization_data', $localization_data );
245 
246  wp_localize_script( 'gravityview-support', 'gvSupport', $localization_data );
247 
248  unset( $localization_data, $data, $translation, $response, $package );
249  }
250 
251  /**
252  * Check whether to show Support for a user
253  *
254  * If the user doesn't have the `gravityview_support_port` capability, returns false; then
255  * If global setting is "hide", returns false; then
256  * If user preference is not set, return global setting; then
257  * If user preference is set, return that setting.
258  *
259  * @since 1.15
260  * @since 1.17.5 Changed behavior to respect global setting
261  *
262  * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
263  *
264  * @return bool Whether to show GravityView support port
265  */
266  static public function show_for_user( $user = 0 ) {
267 
268  if ( ! GVCommon::has_cap( 'gravityview_support_port' ) ) {
269  return false;
270  }
271 
272  $global_setting = gravityview()->plugin->settings->get( 'support_port' );
273 
274  if ( empty( $global_setting ) ) {
275  return false;
276  }
277 
278  // Get the per-user Support Port setting
279  $user_pref = get_user_option( self::user_pref_name, $user );
280 
281  // Not configured; default to global setting (which is true at this point)
282  if ( false === $user_pref ) {
283  $user_pref = $global_setting;
284  }
285 
286  return ! empty( $user_pref );
287  }
288 
289 
290  /**
291  * Update User Profile preferences for GravityView Support
292  *
293  * @since 1.5
294  *
295  * @param int $user_id
296  *
297  * @return void
298  */
299  public function update_user_meta_value( $user_id ) {
300  if ( current_user_can( 'edit_user', $user_id ) && isset( $_POST[ self::user_pref_name ] ) ) {
301  update_user_meta( $user_id, self::user_pref_name, intval( $_POST[ self::user_pref_name ] ) );
302  }
303  }
304 
305  /**
306  * Modify User Profile
307  *
308  * Modifies the output of profile.php to add GravityView Support preference
309  *
310  * @since 1.15
311  * @since 1.17.5 Only show if global setting is active
312  *
313  * @param WP_User $user Current user info
314  *
315  * @return void
316  */
317  public function user_field( $user ) {
318 
319  $global_setting = gravityview()->plugin->settings->get( 'support_port' );
320 
321  if ( empty( $global_setting ) ) {
322  return;
323  }
324 
325  /**
326  * @filter `gravityview/support_port/show_profile_setting` Should the "GravityView Support Port" setting be shown on user profiles?
327  * @since 1.15
328  * @param boolean $allow_profile_setting Default: `true`, if the user has the `gravityview_support_port` capability, which defaults to true for Contributors and higher
329  * @param WP_User $user Current user object
330  */
331  $allow_profile_setting = apply_filters( 'gravityview/support_port/show_profile_setting', GVCommon::has_cap( 'gravityview_support_port' ), $user );
332 
333  if ( $allow_profile_setting && current_user_can( 'edit_user', $user->ID ) ) {
334  ?>
335  <table class="form-table">
336  <tbody>
337  <tr class="user-gravityview-support-button-wrap">
338  <th scope="row"><?php
339  /* translators: "Support Port" can be translated as "Support Portal" or "Support Window" */
340  _e( 'GravityView Support Port', 'gravityview' );
341  ?></th>
342  <td>
343  <fieldset>
344  <legend class="screen-reader-text"><span><?php
345  /* translators: "Support Port" can be translated as "Support Portal" or "Support Window" */
346  _e( 'GravityView Support Port', 'gravityview' );
347  ?></span></legend>
348  <label>
349  <input name="<?php echo esc_attr( self::user_pref_name ); ?>" type="hidden" value="0"/>
350  <input name="<?php echo esc_attr( self::user_pref_name ); ?>" type="checkbox" value="1" <?php checked( self::show_for_user( $user->ID ) ); ?> />
351  <?php esc_html_e( 'Show GravityView Support Port when on a GravityView-related page', 'gravityview' ); ?>
352  </label>
353  </fieldset>
354  </td>
355  </tr>
356  </tbody>
357  </table>
358  <?php }
359  }
360 }
361 
$url
Definition: post_image.php:25
if(empty( $value)) $user
static _localize_script()
Localize the Support Port script.
update_user_meta_value( $user_id)
Update User Profile preferences for GravityView Support.
gravityview()
Definition: _stubs.php:26
static show_for_user( $user=0)
Check whether to show Support for a user.
static $version
static maybe_enqueue_script( $hook)
Enqueue Support Port script if user has it enabled and we&#39;re on a GravityView plugin page...
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:31
user_field( $user)
Modify User Profile.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
maybe_add_article_to_tooltip( $tooltip='', $article=array(), $url='', $atts='', $css_class='', $anchor_text='')
Modify tooltips to add Beacon article.