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

142 lines 3.4 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 // Return commerce.js URL.
78 return $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/commerce.js';
79
80 }
81
82 /**
83 * Returns the HTML button markup for the given Product ID.
84 *
85 * @since 2.0.0
86 *
87 * @param int $id Product ID.
88 * @param string $button_text Button Text.
89 * @param array $css_classes CSS classes to apply to link (typically included when using Gutenberg).
90 * @param array $css_styles CSS inline styles to apply to link (typically included when using Gutenberg).
91 * @param bool $return_as_span If true, returns a <span> instead of <a>. Useful for the block editor so that the element is interactible.
92 * @return WP_Error|string Button HTML
93 */
94 public function get_html( $id, $button_text, $css_classes = array(), $css_styles = array(), $return_as_span = false ) {
95
96 // Cast ID to integer.
97 $id = absint( $id );
98
99 // Bail if the resources are a WP_Error.
100 if ( is_wp_error( $this->resources ) ) {
101 return $this->resources;
102 }
103
104 // Bail if the resource doesn't exist.
105 if ( ! isset( $this->resources[ $id ] ) ) {
106 return new WP_Error(
107 'convertkit_resource_products_get_html',
108 sprintf(
109 /* translators: ConvertKit Product ID */
110 __( 'ConvertKit Product ID %s does not exist on ConvertKit.', 'convertkit' ),
111 $id
112 )
113 );
114 }
115
116 // Build button HTML.
117 $html = '<div class="convertkit-product">';
118
119 if ( $return_as_span ) {
120 $html .= '<span';
121 } else {
122 $html .= '<a href="' . $this->resources[ $id ]['url'] . '"';
123 }
124
125 $html .= ' class="wp-block-button__link ' . esc_attr( implode( ' ', $css_classes ) ) . '" style="' . implode( ';', $css_styles ) . '" data-commerce>';
126 $html .= esc_html( $button_text );
127
128 if ( $return_as_span ) {
129 $html .= '</span>';
130 } else {
131 $html .= '</a>';
132 }
133
134 $html .= '</div>';
135
136 // Return.
137 return $html;
138
139 }
140
141 }
142