GravityView  2.1.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  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 \GV\Admin_Request::is_admin()
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()->request->is_admin( $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  gravityview()->log->debug( '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()->plugin->settings->get( '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  'price_id' => '0',
108  ) );
109 
110  // This is just HTML we don't need.
111  unset( $response['message'] );
112 
113  switch ( intval( $response['price_id'] ) ) {
114  default:
115  case 1:
116  $package = 'Core';
117  break;
118  case 2:
119  $package = 'Extensions';
120  break;
121  case 3:
122  $package = 'All Access';
123  break;
124  case 4:
125  $package = 'Lifetime';
126  break;
127  }
128 
129  $data = array(
130  'email' => gravityview()->plugin->settings->get( 'support-email' ),
131  'name' => $response['customer_name'],
132  'Valid License?' => ucwords( $response['license'] ),
133  'License Key' => $response['license_key'],
134  'License Level' => $package,
135  'Site Admin Email' => get_bloginfo( 'admin_email' ),
136  'Support Email' => gravityview()->plugin->settings->get( 'support-email' ),
137  'License Limit' => $response['license_limit'],
138  'Site Count' => $response['site_count'],
139  'License Expires' => $response['expires'],
140  'Activations Left' => $response['activations_left'],
141  'Payment ID' => $response['payment_id'],
142  'Payment Name' => $response['customer_name'],
143  'Payment Email' => $response['customer_email'],
144  'WordPress Version' => get_bloginfo( 'version', 'display' ),
145  'PHP Version' => phpversion(),
146  'GravityView Version' => \GV\Plugin::$version,
147  'Gravity Forms Version' => GFForms::$version,
149  );
150 
151  $localization_data = array(
152  'contactEnabled' => (int)GVCommon::has_cap( 'gravityview_contact_support' ),
153  'data' => $data,
154  'translation' => $translation,
155  'suggest' => array(),
156  );
157 
158  /**
159  * @filter `gravityview/support_port/localization_data` Filter data passed to the Support Port, before localize_script is run
160  * @since 2.0
161  * @param array $localization_data {
162  * @type int $contactEnabled Can the user contact support?
163  * @type array $data Support/license info
164  * @type array $translation i18n strings
165  * @type array $suggest Article IDs to recommend to the user (per page in the admin
166  * }
167  */
168  $localization_data = apply_filters( 'gravityview/support_port/localization_data', $localization_data );
169 
170  wp_localize_script( 'gravityview-support', 'gvSupport', $localization_data );
171 
172  unset( $localization_data, $data, $translation, $response, $package );
173  }
174 
175  /**
176  * Check whether to show Support for a user
177  *
178  * If the user doesn't have the `gravityview_support_port` capability, returns false; then
179  * If global setting is "hide", returns false; then
180  * If user preference is not set, return global setting; then
181  * If user preference is set, return that setting.
182  *
183  * @since 1.15
184  * @since 1.17.5 Changed behavior to respect global setting
185  *
186  * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
187  *
188  * @return bool Whether to show GravityView support port
189  */
190  static public function show_for_user( $user = 0 ) {
191 
192  if ( ! GVCommon::has_cap( 'gravityview_support_port' ) ) {
193  return false;
194  }
195 
196  $global_setting = gravityview()->plugin->settings->get( 'support_port' );
197 
198  if ( empty( $global_setting ) ) {
199  return false;
200  }
201 
202  // Get the per-user Support Port setting
203  $user_pref = get_user_option( self::user_pref_name, $user );
204 
205  // Not configured; default to global setting (which is true at this point)
206  if ( false === $user_pref ) {
207  $user_pref = $global_setting;
208  }
209 
210  return ! empty( $user_pref );
211  }
212 
213 
214  /**
215  * Update User Profile preferences for GravityView Support
216  *
217  * @since 1.5
218  *
219  * @param int $user_id
220  *
221  * @return void
222  */
223  public function update_user_meta_value( $user_id ) {
224  if ( current_user_can( 'edit_user', $user_id ) && isset( $_POST[ self::user_pref_name ] ) ) {
225  update_user_meta( $user_id, self::user_pref_name, intval( $_POST[ self::user_pref_name ] ) );
226  }
227  }
228 
229  /**
230  * Modify User Profile
231  *
232  * Modifies the output of profile.php to add GravityView Support preference
233  *
234  * @since 1.15
235  * @since 1.17.5 Only show if global setting is active
236  *
237  * @param WP_User $user Current user info
238  *
239  * @return void
240  */
241  public function user_field( $user ) {
242 
243  $global_setting = gravityview()->plugin->settings->get( 'support_port' );
244 
245  if ( empty( $global_setting ) ) {
246  return;
247  }
248 
249  /**
250  * @filter `gravityview/support_port/show_profile_setting` Should the "GravityView Support Port" setting be shown on user profiles?
251  * @since 1.15
252  * @param boolean $allow_profile_setting Default: `true`, if the user has the `gravityview_support_port` capability, which defaults to true for Contributors and higher
253  * @param WP_User $user Current user object
254  */
255  $allow_profile_setting = apply_filters( 'gravityview/support_port/show_profile_setting', GVCommon::has_cap( 'gravityview_support_port' ), $user );
256 
257  if ( $allow_profile_setting && current_user_can( 'edit_user', $user->ID ) ) {
258  ?>
259  <table class="form-table">
260  <tbody>
261  <tr class="user-gravityview-support-button-wrap">
262  <th scope="row"><?php
263  /* translators: "Support Port" can be translated as "Support Portal" or "Support Window" */
264  _e( 'GravityView Support Port', 'gravityview' );
265  ?></th>
266  <td>
267  <fieldset>
268  <legend class="screen-reader-text"><span><?php
269  /* translators: "Support Port" can be translated as "Support Portal" or "Support Window" */
270  _e( 'GravityView Support Port', 'gravityview' );
271  ?></span></legend>
272  <label>
273  <input name="<?php echo esc_attr( self::user_pref_name ); ?>" type="hidden" value="0"/>
274  <input name="<?php echo esc_attr( self::user_pref_name ); ?>" type="checkbox" value="1" <?php checked( self::show_for_user( $user->ID ) ); ?> />
275  <?php esc_html_e( 'Show GravityView Support Port when on a GravityView-related page', 'gravityview' ); ?>
276  </label>
277  </fieldset>
278  </td>
279  </tr>
280  </tbody>
281  </table>
282  <?php }
283  }
284 }
285 
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 get_related_plugins_and_extensions( $implode='< br/>')
Get active GravityView Extensions and Gravity Forms Add-ons to help debug issues. ...
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 has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()