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