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

152 lines 3.7 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 array $css_classes CSS classes to apply to link (typically included when using Gutenberg).
100 * @param array $css_styles CSS inline styles to apply to link (typically included when using Gutenberg).
101 * @param bool $return_as_span If true, returns a <span> instead of <a>. Useful for the block editor so that the element is interactible.
102 * @return WP_Error|string Button HTML
103 */
104 public function get_html( $id, $button_text, $css_classes = array(), $css_styles = array(), $return_as_span = false ) {
105
106 // Cast ID to integer.
107 $id = absint( $id );
108
109 // Bail if the resources are a WP_Error.
110 if ( is_wp_error( $this->resources ) ) {
111 return $this->resources;
112 }
113
114 // Bail if the resource doesn't exist.
115 if ( ! isset( $this->resources[ $id ] ) ) {
116 return new WP_Error(
117 'convertkit_resource_products_get_html',
118 sprintf(
119 /* translators: ConvertKit Product ID */
120 __( 'ConvertKit Product ID %s does not exist on ConvertKit.', 'convertkit' ),
121 $id
122 )
123 );
124 }
125
126 // Build button HTML.
127 $html = '<div class="convertkit-product">';
128
129 if ( $return_as_span ) {
130 $html .= '<span';
131 } else {
132 $html .= '<a href="' . esc_url( $this->resources[ $id ]['url'] ) . '"';
133 }
134
135 $html .= ' class="wp-block-button__link ' . implode( ' ', map_deep( $css_classes, 'sanitize_html_class' ) ) . '" style="' . implode( ';', map_deep( $css_styles, 'esc_attr' ) ) . '" data-commerce>';
136 $html .= esc_html( $button_text );
137
138 if ( $return_as_span ) {
139 $html .= '</span>';
140 } else {
141 $html .= '</a>';
142 }
143
144 $html .= '</div>';
145
146 // Return.
147 return $html;
148
149 }
150
151 }
152