PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.9.7.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.9.7.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 2.3.3 All 194 releases
convertkit / includes / class-convertkit-resource.php

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

157 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Resource class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Abstract class defining variables and functions for a ConvertKit API Resource
11 * (forms, landing pages, tags).
12 *
13 * @since 1.9.6
14 */
15 class ConvertKit_Resource {
16
17 /**
18 * Holds the Settings Key that stores site wide ConvertKit settings
19 *
20 * @var string
21 */
22 public $settings_name = '';
23
24 /**
25 * The type of resource
26 *
27 * @var string
28 */
29 public $type = '';
30
31 /**
32 * Holds the resources from the ConvertKit API
33 *
34 * @var WP_Error|array
35 */
36 public $resources = array();
37
38 /**
39 * Constructor. Populate the resources array of e.g. forms, landing pages or tags.
40 *
41 * @since 1.9.6
42 */
43 public function __construct() {
44
45 // Get resources from options.
46 $resources = get_option( $this->settings_name );
47
48 // If resources exist in the options table, use them.
49 if ( is_array( $resources ) ) {
50 $this->resources = $resources;
51 } else {
52 // No options exist in the options table. Fetch them from the API, storing
53 // them in the options table.
54 $this->resources = $this->refresh();
55 }
56
57 }
58
59 /**
60 * Returns all resources.
61 *
62 * @since 1.9.6
63 *
64 * @return array
65 */
66 public function get() {
67
68 return $this->resources;
69
70 }
71
72 /**
73 * Returns whether any resources exist in the options table.
74 *
75 * @since 1.9.6
76 *
77 * @return bool
78 */
79 public function exist() {
80
81 if ( $this->resources === false ) { // @phpstan-ignore-line.
82 return false;
83 }
84
85 if ( is_wp_error( $this->resources ) ) {
86 return false;
87 }
88
89 if ( is_null( $this->resources ) ) {
90 return false;
91 }
92
93 return ( count( $this->resources ) ? true : false );
94
95 }
96
97 /**
98 * Fetches resources (forms, landing pages or tags) from the API, storing them in the options table.
99 *
100 * @since 1.9.6
101 *
102 * @return bool|WP_Error|array
103 */
104 public function refresh() {
105
106 // Bail if the API Key and Secret hasn't been defined in the Plugin Settings.
107 $settings = new ConvertKit_Settings();
108 if ( ! $settings->has_api_key_and_secret() ) {
109 return false;
110 }
111
112 // Initialize the API.
113 $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() );
114
115 // Fetch resources.
116 switch ( $this->type ) {
117 case 'forms':
118 $results = $api->get_forms();
119 break;
120
121 case 'landing_pages':
122 $results = $api->get_landing_pages();
123 break;
124
125 case 'tags':
126 $results = $api->get_tags();
127 break;
128
129 default:
130 $results = new WP_Error(
131 'convertkit_resource_refresh_error',
132 sprintf(
133 /* translators: Resource Type */
134 __( 'Resource type %s is not supported in ConvertKit_Resource class.', 'convertkit' ),
135 $this->type
136 )
137 );
138 break;
139 }
140
141 // Bail if an error occured.
142 if ( is_wp_error( $results ) ) {
143 return $results;
144 }
145
146 // Update options table data.
147 update_option( $this->settings_name, $results );
148
149 // Store in resource class.
150 $this->resources = $results;
151
152 return $results;
153
154 }
155
156 }
157