PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.1
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 2.4.1, at includes/class-convertkit-resource-products.php

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