PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 All 195 releases
convertkit / includes / class-convertkit-resource-account.php

class-convertkit-resource-account.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages trunk, at includes/class-convertkit-resource-account.php

174 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Account class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Reads Kit Account data from the options table, and refreshes
11 * Kit Account data from the API.
12 *
13 * @since 3.4.0
14 */
15 class ConvertKit_Resource_Account extends ConvertKit_Resource_V4 {
16
17 /**
18 * Holds the Settings Key that stores account data
19 *
20 * @since 3.4.0
21 *
22 * @var string
23 */
24 public $settings_name = 'convertkit_account';
25
26 /**
27 * The type of resource
28 *
29 * @since 3.4.0
30 *
31 * @var string
32 */
33 public $type = 'account';
34
35 /**
36 * Constructor.
37 *
38 * @since 1.9.8.4
39 *
40 * @param bool|string $context Context.
41 */
42 public function __construct( $context = false ) {
43
44 // Initialize the API if the Access Token has been defined in the Plugin Settings.
45 $settings = new ConvertKit_Settings();
46 if ( $settings->has_access_and_refresh_token() ) {
47 $this->api = new ConvertKit_API_V4(
48 CONVERTKIT_OAUTH_CLIENT_ID,
49 CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
50 $settings->get_access_token(),
51 $settings->get_refresh_token(),
52 $settings->debug_enabled(),
53 $context
54 );
55 }
56
57 // Get last query time and existing resources.
58 $this->last_queried = get_option( $this->settings_name . '_last_queried' );
59 $this->resources = get_option( $this->settings_name );
60
61 }
62
63 /**
64 * Fetches the account data from the API, storing them in the options table
65 * with a last queried timestamp.
66 *
67 * If the refresh results in a 401, removes the access and refresh tokens from the settings.
68 *
69 * @since 3.4.0
70 *
71 * @return WP_Error|array
72 */
73 public function refresh() {
74
75 // Query API for account details.
76 $results = $this->api->get_account();
77
78 // Define and store the last query time now.
79 // This prevents multiple calls to refresh() when the above returns a 401 error.
80 $this->last_queried = time();
81 update_option( $this->settings_name . '_last_queried', $this->last_queried );
82
83 // If an error occurred, maybe delete credentials from the Plugin's settings
84 // if the error is a 401 unauthorized.
85 if ( is_wp_error( $results ) ) {
86 convertkit_maybe_delete_credentials( $results, CONVERTKIT_OAUTH_CLIENT_ID );
87 return $results;
88 }
89
90 // Store resources in the options table.
91 // We don't use WordPress' Transients API (i.e. auto expiring options), because they're prone to being
92 // flushed by some third party "optimization" Plugins. They're also not guaranteed to remain in the options
93 // table for the amount of time specified; any expiry is a maximum, not a minimum.
94 // We don't want to keep querying the ConvertKit API for a list of e.g. forms, tags that rarely change as
95 // a result of transients not being honored, so storing them as options with a separate, persistent expiry
96 // value is more reliable here.
97 update_option( $this->settings_name, $results );
98
99 // Store resources in class variable.
100 $this->resources = $results;
101
102 /**
103 * Perform any actions immediately after the resource has been refreshed.
104 *
105 * @since 3.4.0
106 *
107 * @param array $results Resources
108 */
109 do_action( 'convertkit_resource_refreshed_' . $this->type, $results );
110
111 // Return resources.
112 return $this->get();
113
114 }
115
116 /**
117 * Overrides the parent method to return account data, as there's no sorting required.
118 *
119 * @since 3.4.0
120 *
121 * @return bool|array
122 */
123 public function get() {
124
125 return get_option( $this->settings_name );
126
127 }
128
129 /**
130 * Returns the cached plan_type string.
131 *
132 * @since 3.4.0
133 *
134 * @return bool|string
135 */
136 public function get_plan_type() {
137
138 // Get account details from cache.
139 $account = $this->get();
140
141 // If no account details are found, or the plan type is not set, return false.
142 if ( ! $account || ! isset( $account['account']['plan_type'] ) ) {
143 return false;
144 }
145
146 // Return the plan type.
147 return (string) $account['account']['plan_type'];
148
149 }
150
151 /**
152 * Returns whether the cached plan is a paid Kit plan.
153 *
154 * @since 3.4.0
155 *
156 * @return bool
157 */
158 public function is_paid_plan() {
159
160 // Get the plan type from the account details.
161 $plan_type = $this->get_plan_type();
162
163 // If no plan type is found, return false.
164 if ( ! $plan_type ) {
165 return false;
166 }
167
168 // Return true if the plan type is not free, false otherwise.
169 return $plan_type !== 'free';
170
171 }
172
173 }
174