PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.0.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.0.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-products.php

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

202 lines 5.0 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 // Call parent initialization function.
54 parent::init();
55
56 }
57
58 /**
59 * Returns the commerce.js URL based on the account's ConvertKit Domain.
60 *
61 * @since 2.0.0
62 *
63 * @return bool|string false (if no products) | URL.
64 */
65 public function get_commerce_js_url() {
66
67 // Bail if no Products exist in this resource.
68 if ( ! $this->exist() ) {
69 return false;
70 }
71
72 // Fetch the first Product.
73 $products = $this->get();
74 $product = reset( $products );
75
76 // Parse the URL.
77 $parsed_url = wp_parse_url( $product['url'] );
78
79 // Bail if parsing the URL failed.
80 if ( ! $parsed_url ) {
81 return false;
82 }
83
84 // Bail if the scheme and host could not be obtained from the URL.
85 if ( ! array_key_exists( 'scheme', $parsed_url ) || ! array_key_exists( 'host', $parsed_url ) ) {
86 return false;
87 }
88
89 // Return commerce.js URL.
90 return $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/commerce.js';
91
92 }
93
94 /**
95 * Returns the HTML button markup for the given Product ID.
96 *
97 * @since 2.0.0
98 *
99 * @param int $id Product ID.
100 * @param string $button_text Button Text.
101 * @param bool|array $options {
102 * Optional. An array of settings.
103 *
104 * @type bool $disable_modal If true, the button's link will open in a new browser window/tab, instead of a modal
105 * @type bool|string $discount_code Discount Code to include.
106 * @type array $css_classes CSS classes to apply to link (typically included when using Gutenberg).
107 * @type array $css_styles CSS inline styles to apply to link (typically included when using Gutenberg).
108 * @type bool $return_as_span If true, returns a <span> instead of <a>. Useful for the block editor so that the element is interactible.
109 * }
110 * @return WP_Error|string Button HTML
111 */
112 public function get_html( $id, $button_text, $options = false ) {
113
114 // Define default options for the button.
115 $defaults = array(
116 'disable_modal' => false,
117 'discount_code' => false,
118 'checkout' => false,
119 'css_classes' => array(),
120 'css_styles' => array(),
121 'return_as_span' => false,
122 );
123
124 // If option are supplied, merge with defaults.
125 $options = ( ! $options ? $defaults : array_merge( $defaults, $options ) );
126
127 // Cast ID to integer.
128 $id = absint( $id );
129
130 // Bail if the resources are a WP_Error.
131 if ( is_wp_error( $this->resources ) ) {
132 return $this->resources;
133 }
134
135 // Bail if the resource doesn't exist.
136 if ( ! isset( $this->resources[ $id ] ) ) {
137 return new WP_Error(
138 'convertkit_resource_products_get_html',
139 sprintf(
140 /* translators: Kit Product ID */
141 __( 'Kit Product ID %s does not exist on Kit.', 'convertkit' ),
142 $id
143 )
144 );
145 }
146
147 // Build product URL.
148 $product_url = $this->resources[ $id ]['url'];
149
150 // If a discount code is specified, add it to the URL now.
151 if ( $options['discount_code'] ) {
152 $product_url = add_query_arg(
153 array(
154 'promo' => $options['discount_code'],
155 ),
156 $product_url
157 );
158 }
159
160 // If the URL should directly load the checkout step, add it to the URL now.
161 if ( $options['checkout'] ) {
162 $product_url = add_query_arg(
163 array(
164 'step' => 'checkout',
165 ),
166 $product_url
167 );
168 }
169
170 // Build button HTML.
171 $html = '<div class="convertkit-product">';
172
173 if ( $options['return_as_span'] ) {
174 $html .= '<span';
175 } else {
176 $html .= '<a href="' . esc_url( $product_url ) . '"';
177 }
178
179 $html .= sprintf(
180 ' class="%s" style="%s"%s>',
181 implode( ' ', map_deep( $options['css_classes'], 'sanitize_html_class' ) ),
182 implode( ';', map_deep( $options['css_styles'], 'esc_attr' ) ),
183 ( ! $options['disable_modal'] ? ' data-commerce' : '' )
184 );
185
186 $html .= esc_html( $button_text );
187
188 if ( $options['return_as_span'] ) {
189 $html .= '</span>';
190 } else {
191 $html .= '</a>';
192 }
193
194 $html .= '</div>';
195
196 // Return.
197 return $html;
198
199 }
200
201 }
202