GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
TrustedLogin.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @license GPL-2.0-or-later
4  *
5  * Modified by gravityview on 13-January-2023 using Strauss.
6  * @see https://github.com/BrianHenryIE/strauss
7  */
8 
10 
14 use GravityKit\GravityView\Foundation\ThirdParty\TrustedLogin\Admin as TrustedLoginAdmin;
15 use GravityKit\GravityView\Foundation\ThirdParty\TrustedLogin\Logging as TrustedLoginLogging;
16 use GravityKit\GravityView\Foundation\ThirdParty\TrustedLogin\Config as TrustedLoginConfig;
17 use GravityKit\GravityView\Foundation\ThirdParty\TrustedLogin\Client as TrustedLoginClient;
20 use Exception;
21 
22 class TrustedLogin {
23  const ID = 'gk_foundation_trustedlogin';
24 
25  const TL_API_KEY = '3b3dc46c0714cc8e';
26 
27  /**
28  * @since 1.0.0
29  *
30  * @var string Access capabilities.
31  */
32  private $_capability = 'manage_options';
33 
34  /**
35  * @since 1.0.0
36  *
37  * @var TrustedLoginClient TL Client class instance.
38  */
40 
41  /**
42  * @since 1.0.0
43  *
44  * @var TrustedLogin Class instance.
45  */
46  private static $_instance;
47 
48  /**
49  * Class constructor.
50  *
51  * @since 1.0.0
52  *
53  * @return void
54  */
55  private function __construct() {
56  try {
57  $this->_trustedlogin_client = new TrustedLoginClient(
58  new TrustedLoginConfig( $this->get_config() )
59  );
60  } catch ( Exception $e ) {
61  LoggerFramework::get_instance()->error( 'Unable to initialize TrustedLogin client: ' . $e->getMessage() );
62 
63  return;
64  }
65 
66  try {
67  $this->add_gk_submenu_item();
68  } catch ( Exception $e ) {
69  LoggerFramework::get_instance()->error( 'Unable to add TrustedLogin to the Foundation menu: ' . $e->getMessage() );
70 
71  return;
72  }
73 
74  add_action( 'trustedlogin/' . self::ID . '/logging/log', [ $this, 'log' ], 10, 4 );
75  add_filter( 'gk/foundation/integrations/helpscout/configuration', [ $this, 'add_tl_key_to_helpscout_beacon' ] );
76  }
77 
78  /**
79  * Returns class instance.
80  *
81  * @since 1.0.0
82  *
83  * @return TrustedLogin
84  */
85  public static function get_instance() {
86  if ( ! self::$_instance ) {
87  self::$_instance = new self();
88  }
89 
90  return self::$_instance;
91  }
92 
93  /**
94  * Adds Settings submenu to the GravityKit top-level admin menu.
95  *
96  * @since 1.0.0
97  *
98  * @throws Exception TrustedLoginConfig throws an exception when the config object is empty (do not apply to us),
99  *
100  * @return void
101  */
102  public function add_gk_submenu_item() {
103  $tl_config = new TrustedLoginConfig( $this->get_config() );
104  $tl_admin = new TrustedLoginAdmin( $tl_config, new TrustedLoginLogging( $tl_config ) );
105 
106  $page_title = $menu_title = esc_html__( 'Grant Support Access', 'gk-gravityview' );
107 
109  'page_title' => $page_title,
110  'menu_title' => $menu_title,
111  'capability' => $this->_capability,
112  'id' => self::ID,
113  'callback' => [ $tl_admin, 'print_auth_screen' ],
114  'order' => 1,
115  ], 'bottom' );
116  }
117 
118  /**
119  * Returns TrustedLogin configuration.
120  *
121  * @since 1.0.0
122  *
123  * @return array
124  */
125  public function get_config() {
126  /**
127  * @filter `gk/foundation/integrations/trustedlogin/capabilities` Modifies the capabilities added/removed by TL.
128  *
129  * @since 1.0.0
130  *
131  * @param array $capabilities
132  */
133  $capabilities = apply_filters( 'gk/foundation/integrations/trustedlogin/capabilities', [
134  'add' => [
135  'gravityview_full_access' => esc_html__( 'We need access to Views to provide great support.', 'gk-gravityview' ),
136  'gform_full_access' => esc_html__( 'We will need to see and edit the forms, entries, and Gravity Forms settings to debug issues.', 'gk-gravityview' ),
137  'install_plugins' => esc_html__( 'We may need to manage plugins in order to debug conflicts on your site and add related GravityView functionality.', 'gk-gravityview' ),
138  'update_plugins' => '',
139  'deactivate_plugins' => '',
140  'activate_plugins' => '',
141  ],
142  'remove' => [
143  'manage_woocommerce' => strtr(
144  esc_html_x( "We don't need to see your [plugin] details to provide support (if [plugin] is enabled).", 'Placeholders inside [] are not to be translated.', 'gk-gravityview' ),
145  [ 'plugin' => 'WooCommerce' ]
146  ),
147  'view_shop_reports' => strtr(
148  esc_html_x( "We don't need to see your [plugin] details to provide support (if [plugin] is enabled).", 'Placeholders inside [] are not to be translated.', 'gk-gravityview' ),
149  [ 'plugin' => 'Easy Digital Downloads' ]
150  ),
151  ],
152  ] );
153 
154  $config = [
155  'auth' => [
156  'api_key' => self::TL_API_KEY,
157  ],
158  'menu' => [
159  'slug' => false, // Prevent TL from adding a menu item; we'll do it manually in the add_gk_submenu_item() method.
160  ],
161  'role' => 'administrator',
162  'caps' => $capabilities,
163  'logging' => [
164  'enabled' => true,
165  'threshold' => 'warning',
166  ],
167  'vendor' => [
168  'namespace' => self::ID,
169  'title' => 'GravityKit',
170  'email' => 'support+{hash}@gravitykit.com',
171  'website' => 'https://www.gravitykit.com',
172  'support_url' => 'https://www.gravitykit.com/support/',
173  'display_name' => 'GravityKit Support',
174  'logo_url' => CoreHelpers::get_assets_url( 'gravitykit-logo.svg' ),
175  ],
176  'register_assets' => true,
177  'paths' => [
178  'css' => CoreHelpers::get_assets_url( 'trustedlogin/trustedlogin.css' ),
179  ],
180  'webhook_url' => 'https://hooks.zapier.com/hooks/catch/28670/bbyi3l4',
181  ];
182 
183  $license_manager = LicenseManager::get_instance();
184 
185  foreach ( $license_manager->get_licenses_data() as $license_data ) {
186  if ( Arr::get( $license_data, 'products' ) && ! $license_manager->is_expired_license( Arr::get( $license_data, 'expiry' ) ) ) {
187  Arr::set( $config, 'auth.license_key', Arr::get( $license_data, 'key' ));
188 
189  break;
190  }
191  }
192 
193  return $config;
194  }
195 
196  /**
197  * Overrides TL's internal logging with Foundation's logging.
198  *
199  * @internal Once we require PHP 7.1, this will be a private method, and we'll use Closure::fromCallable().
200  *
201  * @since 1.0.0
202  *
203  * @param string $message Message to log.
204  * @param string $method Method where the log was called.
205  * @param string $level PSR-3 log level {@see https://www.php-fig.org/psr/psr-3/#5-psrlogloglevel}.
206  * @param \WP_Error|\Exception|mixed $data (optional) Error data. Ignored if $message is WP_Error.
207  *
208  * @return void
209  */
210  public function log( $message, $method = '', $level = 'debug', $data = [] ) {
211  LoggerFramework::get_instance()->{$level}( $message );
212  }
213 
214  /**
215  * Updates Help Scout beacon with TL access key.
216  *
217  * @since 1.0.0
218  *
219  * @param array $configuration
220  *
221  * @return array
222  */
223  public function add_tl_key_to_helpscout_beacon( $configuration ) {
224  Arr::set( $configuration, 'identify.tl_access_key', $this->_trustedlogin_client->get_access_key() );
225 
226  return $configuration;
227  }
228 }
static set(&$array, $key, $value)
{}
Definition: Arr.php:225
static add_submenu_item( $submenu, $position='top')
Adds a submenu to the GravityKit top-level menu in WP admin.
Definition: AdminMenu.php:233
static get( $array, $key, $default=null)
{}
Definition: Arr.php:99
add_tl_key_to_helpscout_beacon( $configuration)
Updates Help Scout beacon with TL access key.
add_gk_submenu_item()
Adds Settings submenu to the GravityKit top-level admin menu.
get_config()
Returns TrustedLogin configuration.
log( $message, $method='', $level='debug', $data=[])
Overrides TL&#39;s internal logging with Foundation&#39;s logging.