| @@ -11,9 +11,9 @@ | ||
| 11 | 11 | * ConvertKit Products data stored locally from the API. |
| 12 | 12 | * |
| 13 | 13 | * @since 2.0.0 |
| 14 | 14 | */ |
| 15 | -class ConvertKit_Resource_Products extends ConvertKit_Resource { | |
| 15 | +class ConvertKit_Resource_Products extends ConvertKit_Resource_V4 { | |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Holds the Settings Key that stores site wide ConvertKit settings |
| 19 | 19 | * |
| @@ -36,21 +36,24 @@ | ||
| 36 | 36 | * @param bool|string $context Context. |
| 37 | 37 | */ |
| 38 | 38 | public function __construct( $context = false ) { |
| 39 | 39 | |
| 40 | - // Initialize the API if the API Key and Secret have been defined in the Plugin Settings. | |
| 40 | + // Initialize the API if the Access Token has been defined in the Plugin Settings. | |
| 41 | 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(), | |
| 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(), | |
| 46 | 48 | $settings->debug_enabled(), |
| 47 | 49 | $context |
| 48 | 50 | ); |
| 49 | 51 | } |
| 50 | 52 | |
| 51 | - // Call parent initialization function. | |
| 52 | - parent::init(); | |
| 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 ); | |
| 53 | 56 | |
| 54 | 57 | } |
| 55 | 58 | |
| 56 | 59 | /** |
| @@ -93,17 +96,36 @@ | ||
| 93 | 96 | * Returns the HTML button markup for the given Product ID. |
| 94 | 97 | * |
| 95 | 98 | * @since 2.0.0 |
| 96 | 99 | * |
| 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 | |
| 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 | |
| 103 | 112 | */ |
| 104 | - public function get_html( $id, $button_text, $css_classes = array(), $css_styles = array(), $return_as_span = false ) { | |
| 113 | + public function get_html( $id, $button_text, $options = false ) { | |
| 105 | 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 | + | |
| 106 | 128 | // Cast ID to integer. |
| 107 | 129 | $id = absint( $id ); |
| 108 | 130 | |
| 109 | 131 | // Bail if the resources are a WP_Error. |
| @@ -115,28 +137,57 @@ | ||
| 115 | 137 | if ( ! isset( $this->resources[ $id ] ) ) { |
| 116 | 138 | return new WP_Error( |
| 117 | 139 | 'convertkit_resource_products_get_html', |
| 118 | 140 | sprintf( |
| 119 | - /* translators: ConvertKit Product ID */ | |
| 120 | - __( 'ConvertKit Product ID %s does not exist on ConvertKit.', 'convertkit' ), | |
| 141 | + /* translators: Kit Product ID */ | |
| 142 | + __( 'Kit Product ID %s does not exist on Kit.', 'convertkit' ), | |
| 121 | 143 | $id |
| 122 | 144 | ) |
| 123 | 145 | ); |
| 124 | 146 | } |
| 125 | 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 | + | |
| 126 | 171 | // Build button HTML. |
| 127 | 172 | $html = '<div class="convertkit-product">'; |
| 128 | 173 | |
| 129 | - if ( $return_as_span ) { | |
| 174 | + if ( $options['return_as_span'] ) { | |
| 130 | 175 | $html .= '<span'; |
| 131 | 176 | } else { |
| 132 | - $html .= '<a href="' . esc_url( $this->resources[ $id ]['url'] ) . '"'; | |
| 177 | + $html .= '<a href="' . esc_url( $product_url ) . '"'; | |
| 133 | 178 | } |
| 134 | 179 | |
| 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>'; | |
| 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 | + | |
| 136 | 187 | $html .= esc_html( $button_text ); |
| 137 | 188 | |
| 138 | - if ( $return_as_span ) { | |
| 189 | + if ( $options['return_as_span'] ) { | |
| 139 | 190 | $html .= '</span>'; |
| 140 | 191 | } else { |
| 141 | 192 | $html .= '</a>'; |
| 142 | 193 | } |