PluginProbe
PayPlug for WooCommerce (Official) / 2.9.0
PayPlug for WooCommerce (Official) v2.9.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Gateway / PayplugPermissions.php

PayplugPermissions.php in PayPlug for WooCommerce (Official) 2.9.0, at src/Gateway/PayplugPermissions.php

124 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Gateway;
4
5 // Exit if accessed directly
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10
11 use Payplug\Payplug;
12 use Payplug\Authentication;
13 use Payplug\Exception\PayplugException;
14
15 class PayplugPermissions {
16
17 const OPTION_NAME = 'payplug_permission';
18 const LIVE_MODE = 'use_live_mode';
19 const SAVE_CARD = 'can_save_cards';
20 const USE_ONEY = 'can_use_oney';
21
22
23 /**
24 * The current mode for the gateway.
25 *
26 * @var string
27 */
28 private $gateway_mode;
29
30 /**
31 * The current key for account permissions.
32 *
33 * @var string
34 */
35 private $current_key;
36
37 /**
38 * @var array
39 */
40 private $permissions;
41
42 /**
43 * PayplugPermissions constructor.
44 *
45 * @param PayplugGateway $gateway
46 */
47 public function __construct( PayplugGateway $gateway) {
48 $this->gateway_mode = $gateway->get_current_mode();
49 if (!isset($gateway->settings['payplug_live_key']) || !isset($gateway->settings['payplug_test_key'])) {
50 $this->current_key = '';
51 } else {
52 $this->current_key = "live" === $this->gateway_mode ?
53 $gateway->settings['payplug_live_key'] : $gateway->settings['payplug_test_key'];
54 }
55 $this->load_permissions();
56 }
57
58 /**
59 * Get all permissions.
60 *
61 * @return array
62 */
63 public function get_permissions() {
64 return $this->permissions;
65 }
66
67 /**
68 * Check if user has specific permission.
69 *
70 * @param string $user_can
71 *
72 * @return bool
73 */
74 public function has_permissions( $user_can ) {
75 if ( empty( $user_can ) ) {
76 return false;
77 }
78
79 return isset( $this->permissions[ $user_can ] ) && true === $this->permissions[ $user_can ];
80 }
81
82 /**
83 * Delete permissions for the current mode.
84 *
85 * @return bool
86 */
87 public function clear_permissions() {
88 return delete_transient( $this->get_key() );
89 }
90
91 /**
92 * Load permissions for the current mode.
93 */
94 protected function load_permissions() {
95 $payplug_permissions = get_transient( $this->get_key() );
96 if ( ! empty( $payplug_permissions ) ) {
97 $this->permissions = $payplug_permissions;
98
99 return true;
100 }
101
102 try {
103 $response = Authentication::getPermissions(new Payplug($this->current_key));
104 $this->permissions = ! empty( $response ) ? $response : [];
105 set_transient( $this->get_key(), $this->permissions, DAY_IN_SECONDS );
106
107 return true;
108 } catch ( PayplugException $e ) {
109 $this->permissions = [];
110 }
111
112 return false;
113 }
114
115 /**
116 * Build the key to retrieve the permissions.
117 *
118 * @return string
119 */
120 protected function get_key() {
121 return self::OPTION_NAME . '_' . $this->gateway_mode;
122 }
123 }
124