GravityView  2.17
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  * @var string The name of the User Meta option used to store whether a user wants to see the Support Port
9  * @since 1.15
10  * @since 2.16 Renamed from `user_pref_name` to `USER_PREF_NAME`
11  */
12  const USER_PREF_NAME = 'gravityview_support_port';
13 
14  /**
15  * @var string Help Scout beacon key
16  *
17  * @since 2.16 Previous constant `beacon_key` was renamed to `HASH_KEY`
18  */
19  const HS_BEACON_KEY = 'b4f6255a-91bc-436c-a5a2-4cca051ad00f';
20 
21  /**
22  * @var string The hash key used to generate secure message history
23  *
24  * @since 2.16
25  */
26  const HASH_KEY = 'lCXlwbQR707kipR+J0MCqcxrhGOHjGF0ldD6yNbGM0w=';
27 
28 
29  public function __construct() {
30  $this->add_hooks();
31  }
32 
33  /**
34  * @since 1.15
35  */
36  private function add_hooks() {
37  add_action( 'personal_options', [ $this, 'user_field' ] );
38  add_action( 'personal_options_update', [ $this, 'update_user_meta_value' ] );
39  add_action( 'edit_user_profile_update', [ $this, 'update_user_meta_value' ] );
40  add_filter( 'gk/foundation/integrations/helpscout/display', [ $this, 'maybe_display_helpscout_beacon' ] );
41  add_filter( 'gravityview/tooltips/tooltip', [ $this, 'maybe_add_article_to_tooltip' ], 10, 6 );
42  }
43 
44  /**
45  * Modify tooltips to add Beacon article
46  *
47  * @since 2.8.1
48  *
49  * @param string $tooltip HTML of original tooltip
50  * @param array $article Optional. Details about support doc article connected to the tooltip. {
51  * @type string $id Unique ID of article for Beacon API
52  * @type string $url URL of support doc article
53  * @type string $type Type of Beacon element to open. {@see https://developer.helpscout.com/beacon-2/web/javascript-api/#beaconarticle}
54  * }
55  * @param string $url
56  * @param string $atts
57  * @param string $css_class
58  * @param string $anchor_text
59  * @param string $link_text
60  *
61  * @return string If no article information exists, original tooltip. Otherwise, modified!
62  */
63  public function maybe_add_article_to_tooltip( $tooltip = '', $article = [], $url = '', $atts = '', $css_class = '', $anchor_text = '' ) {
64  if ( empty( $article['id'] ) ) {
65  return $tooltip;
66  }
67 
68  static $show_support_port;
69 
70  if ( ! isset( $show_support_port ) ) {
71  $show_support_port = self::show_for_user();
72  }
73 
74  if ( ! $show_support_port ) {
75  return $tooltip;
76  }
77 
78  $css_class .= ' gv_tooltip';
79 
80  if ( ! empty( $article['type'] ) ) {
81  $atts = sprintf( 'data-beacon-article-%s="%s"', $article['type'], $article['id'] );
82  } else {
83  $atts = sprintf( 'data-beacon-article="%s"', $article['id'] );
84  }
85 
86  $url = \GV\Utils::get( $article, 'url', '#' );
87  $anchor_text .= '<p class="description" style="font-size: 15px; text-align: center;"><strong>' . sprintf( esc_html__( 'Click %s icon for additional information.', 'gk-gravityview' ), '<i class=\'fa fa-question-circle\'></i>' ) . '</strong></p>';
88  $link_text = esc_html__( 'Learn More', 'gk-gravityview' );
89 
90  return sprintf( '<a href="%s" %s class="%s" title="%s" role="button">%s</a>',
91  esc_url( $url ),
92  $atts,
93  $css_class,
94  esc_attr( $anchor_text ),
96  );
97  }
98 
99  /**
100  * Conditionally displays Help Scout beacon on certain pages
101  *
102  * @since 2.16
103  *
104  * @param bool $display
105  *
106  * @return bool
107  */
108  public function maybe_display_helpscout_beacon( $display ) {
109  global $post;
110 
111  if ( ! is_admin() || 'gravityview' !== get_post_type( $post ) ) {
112  return $display;
113  }
114 
115  /**
116  * @filter `gravityview/support_port/display` Whether to display Support Port
117  * @since 1.15
118  * @param boolean $display_support_port Default: `true`
119  */
120  $display_support_port = apply_filters( 'gravityview/support_port/display', self::show_for_user() );
121 
122  if ( empty( $display_support_port ) ) {
123  gravityview()->log->debug( 'Not showing Support Port' );
124 
125  return false;
126  }
127 
128  add_filter( 'gk/foundation/integrations/helpscout/configuration', function ( $configuration ) {
129  $arr_helpers = GravityKitFoundation::helpers()->array;
130 
131  $arr_helpers->set( $configuration, 'init', self::HS_BEACON_KEY );
132  $arr_helpers->set( $configuration, 'identify.signature', hash_hmac( 'sha256', $arr_helpers->get( $configuration, 'identify.email', '' ), self::HASH_KEY ) );
133 
134  /**
135  * @filter `gravityview/support_port/localization_data` Filter data passed to the Support Port, before localize_script is run
136  * @since 2.0
137  * @since 2.16 Removed `contactEnabled`, `translation` and `data` keys
138  *
139  * @param array $configuration {
140  *
141  * @type array $suggest Article IDs to recommend to the user (per page in the admin)
142  * }
143  */
144  $localized_data = apply_filters( 'gravityview/support_port/localization_data', [
145  'suggest' => $arr_helpers->get( $configuration, 'suggest', [] ),
146  ] );
147 
148  $arr_helpers->set( $configuration, 'suggest', $localized_data['suggest'] );
149 
150  return $configuration;
151  } );
152 
153  return true;
154  }
155 
156  /**
157  * Check whether to show Support for a user
158  *
159  * If the user doesn't have the `gravityview_support_port` capability, returns false; then
160  * If global setting is "hide", returns false; then
161  * If user preference is not set, return global setting; then
162  * If user preference is set, return that setting.
163  *
164  * @since 1.15
165  * @since 1.17.5 Changed behavior to respect global setting
166  *
167  * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
168  *
169  * @return bool Whether to show GravityView support port
170  */
171  static public function show_for_user( $user = 0 ) {
172  if ( ! GVCommon::has_cap( 'gravityview_support_port' ) ) {
173  return false;
174  }
175 
176  $global_setting = GravityKitFoundation::settings()->get_plugin_setting( GravityKitFoundation::ID, 'support_port' );
177 
178  if ( empty( $global_setting ) ) {
179  return false;
180  }
181 
182  // Get the per-user Support Port setting
183  $user_pref = get_user_option( self::USER_PREF_NAME, $user );
184 
185  // Not configured; default to global setting (which is true at this point)
186  if ( false === $user_pref ) {
187  $user_pref = $global_setting;
188  }
189 
190  return ! empty( $user_pref );
191  }
192 
193 
194  /**
195  * Update User Profile preferences for GravityView Support
196  *
197  * @since 1.5
198  *
199  * @param int $user_id
200  *
201  * @return void
202  */
203  public function update_user_meta_value( $user_id ) {
204  if ( current_user_can( 'edit_user', $user_id ) && isset( $_POST[ self::USER_PREF_NAME ] ) ) {
205  update_user_meta( $user_id, self::USER_PREF_NAME, intval( $_POST[ self::USER_PREF_NAME ] ) );
206  }
207  }
208 
209  /**
210  * Modify User Profile
211  *
212  * Modifies the output of profile.php to add GravityView Support preference
213  *
214  * @since 1.15
215  * @since 1.17.5 Only show if global setting is active
216  *
217  * @param WP_User $user Current user info
218  *
219  * @return void
220  */
221  public function user_field( $user ) {
222  $global_setting = GravityKitFoundation::settings()->get_plugin_setting( GravityKitFoundation::ID, 'support_port' );
223 
224  if ( empty( $global_setting ) ) {
225  return;
226  }
227 
228  /**
229  * @filter `gravityview/support_port/show_profile_setting` Should the "GravityView Support Port" setting be shown on user profiles?
230  * @since 1.15
231  * @param boolean $allow_profile_setting Default: `true`, if the user has the `gravityview_support_port` capability, which defaults to true for Contributors and higher
232  * @param WP_User $user Current user object
233  */
234  $allow_profile_setting = apply_filters( 'gravityview/support_port/show_profile_setting', GVCommon::has_cap( 'gravityview_support_port' ), $user );
235 
236  if ( $allow_profile_setting && current_user_can( 'edit_user', $user->ID ) ) {
237  ?>
238  <table class="form-table">
239  <tbody>
240  <tr class="user-gravityview-support-button-wrap">
241  <th scope="row"><?php
242  /* translators: "Support Port" can be translated as "Support Portal" or "Support Window" */
243  esc_html_e( 'GravityView Support Port', 'gk-gravityview' );
244  ?></th>
245  <td>
246  <fieldset>
247  <legend class="screen-reader-text"><span><?php
248  /* translators: "Support Port" can be translated as "Support Portal" or "Support Window" */
249  esc_html_e( 'GravityView Support Port', 'gk-gravityview' );
250  ?></span></legend>
251  <label>
252  <input name="<?php echo esc_attr( self::USER_PREF_NAME ); ?>" type="hidden" value="0"/>
253  <input name="<?php echo esc_attr( self::USER_PREF_NAME ); ?>" type="checkbox" value="1" <?php checked( self::show_for_user( $user->ID ) ); ?> />
254  <?php esc_html_e( 'Show GravityView Support Port when on a GravityView-related page', 'gk-gravityview' ); ?>
255  </label>
256  </fieldset>
257  </td>
258  </tr>
259  </tbody>
260  </table>
261  <?php }
262  }
263 }
264 
$url
Definition: post_image.php:25
if(empty( $value)) $user
update_user_meta_value( $user_id)
Update User Profile preferences for GravityView Support.
global $post
Definition: delete-entry.php:7
static show_for_user( $user=0)
Check whether to show Support for a user.
maybe_display_helpscout_beacon( $display)
Conditionally displays Help Scout beacon on certain pages.
user_field( $user)
Modify User Profile.
maybe_add_article_to_tooltip( $tooltip='', $article=[], $url='', $atts='', $css_class='', $anchor_text='')
Modify tooltips to add Beacon article.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()