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.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-products.php

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

203 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Products Resource class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Reads ConvertKit Products from the options table, and refreshes
11 * ConvertKit Products data stored locally from the API.
12 *
13 * @since 2.0.0
14 */
15 class ConvertKit_Resource_Products extends ConvertKit_Resource_V4 {
16
17 /**
18 * Holds the Settings Key that stores site wide ConvertKit settings
19 *
20 * @var string
21 */
22 public $settings_name = 'convertkit_products';
23
24 /**
25 * The type of resource
26 *
27 * @var string
28 */
29 public $type = 'products';
30
31 /**
32 * Constructor.
33 *
34 * @since 2.0.0
35 *
36 * @param bool|string $context Context.
37 */
38 public function __construct( $context = false ) {
39
40 // Initialize the API if the Access Token has been defined in the Plugin Settings.
41 $settings = new ConvertKit_Settings();
42 if ( $settings->has_access_and_refresh_token() ) {
43 $this->api = new ConvertKit_API_V4(
44 CONVERTKIT_OAUTH_CLIENT_ID,
45 CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
46 $settings->get_access_token(),
47 $settings->get_refresh_token(),
48 $settings->debug_enabled(),
49 $context
50 );
51 }
52
53 // Get last query time and existing resources.
54 $this->last_queried = get_option( $this->settings_name . '_last_queried' );
55 $this->resources = get_option( $this->settings_name );
56
57 }
58
59 /**
60 * Returns the commerce.js URL based on the account's ConvertKit Domain.
61 *
62 * @since 2.0.0
63 *
64 * @return bool|string false (if no products) | URL.
65 */
66 public function get_commerce_js_url() {
67
68 // Bail if no Products exist in this resource.
69 if ( ! $this->exist() ) {
70 return false;
71 }
72
73 // Fetch the first Product.
74 $products = $this->get();
75 $product = reset( $products );
76
77 // Parse the URL.
78 $parsed_url = wp_parse_url( $product['url'] );
79
80 // Bail if parsing the URL failed.
81 if ( ! $parsed_url ) {
82 return false;
83 }
84
85 // Bail if the scheme and host could not be obtained from the URL.
86 if ( ! array_key_exists( 'scheme', $parsed_url ) || ! array_key_exists( 'host', $parsed_url ) ) {
87 return false;
88 }
89
90 // Return commerce.js URL.
91 return $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/commerce.js';
92
93 }
94
95 /**
96 * Returns the HTML button markup for the given Product ID.
97 *
98 * @since 2.0.0
99 *
100 * @param int $id Product ID.
101 * @param string $button_text Button Text.
102 * @param bool|array $options {
103 * Optional. An array of settings.
104 *
105 * @type bool $disable_modal If true, the button's link will open in a new browser window/tab, instead of a modal
106 * @type bool|string $discount_code Discount Code to include.
107 * @type array $css_classes CSS classes to apply to link (typically included when using Gutenberg).
108 * @type array $css_styles CSS inline styles to apply to link (typically included when using Gutenberg).
109 * @type bool $return_as_span If true, returns a <span> instead of <a>. Useful for the block editor so that the element is interactible.
110 * }
111 * @return WP_Error|string Button HTML
112 */
113 public function get_html( $id, $button_text, $options = false ) {
114
115 // Define default options for the button.
116 $defaults = array(
117 'disable_modal' => false,
118 'discount_code' => false,
119 'checkout' => false,
120 'css_classes' => array(),
121 'css_styles' => array(),
122 'return_as_span' => false,
123 );
124
125 // If option are supplied, merge with defaults.
126 $options = ( ! $options ? $defaults : array_merge( $defaults, $options ) );
127
128 // Cast ID to integer.
129 $id = absint( $id );
130
131 // Bail if the resources are a WP_Error.
132 if ( is_wp_error( $this->resources ) ) {
133 return $this->resources;
134 }
135
136 // Bail if the resource doesn't exist.
137 if ( ! isset( $this->resources[ $id ] ) ) {
138 return new WP_Error(
139 'convertkit_resource_products_get_html',
140 sprintf(
141 /* translators: Kit Product ID */
142 __( 'Kit Product ID %s does not exist on Kit.', 'convertkit' ),
143 $id
144 )
145 );
146 }
147
148 // Build product URL.
149 $product_url = $this->resources[ $id ]['url'];
150
151 // If a discount code is specified, add it to the URL now.
152 if ( $options['discount_code'] ) {
153 $product_url = add_query_arg(
154 array(
155 'promo' => $options['discount_code'],
156 ),
157 $product_url
158 );
159 }
160
161 // If the URL should directly load the checkout step, add it to the URL now.
162 if ( $options['checkout'] ) {
163 $product_url = add_query_arg(
164 array(
165 'step' => 'checkout',
166 ),
167 $product_url
168 );
169 }
170
171 // Build button HTML.
172 $html = '<div class="convertkit-product">';
173
174 if ( $options['return_as_span'] ) {
175 $html .= '<span';
176 } else {
177 $html .= '<a href="' . esc_url( $product_url ) . '"';
178 }
179
180 $html .= sprintf(
181 ' class="%s" style="%s"%s>',
182 implode( ' ', map_deep( $options['css_classes'], 'sanitize_html_class' ) ),
183 implode( ';', map_deep( $options['css_styles'], 'esc_attr' ) ),
184 ( ! $options['disable_modal'] ? ' data-commerce' : '' )
185 );
186
187 $html .= esc_html( $button_text );
188
189 if ( $options['return_as_span'] ) {
190 $html .= '</span>';
191 } else {
192 $html .= '</a>';
193 }
194
195 $html .= '</div>';
196
197 // Return.
198 return $html;
199
200 }
201
202 }
203