| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Product Button Block class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit Product Button Block for Gutenberg and Shortcode. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Block_Product extends ConvertKit_Block { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
* |
| 20 |
* @since 1.9.8.5 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
// Register this as a shortcode in the ConvertKit Plugin. |
| 25 |
add_filter( 'convertkit_shortcodes', array( $this, 'register' ) ); |
| 26 |
|
| 27 |
// Register this as a Gutenberg block in the ConvertKit Plugin. |
| 28 |
add_filter( 'convertkit_blocks', array( $this, 'register' ) ); |
| 29 |
|
| 30 |
// Enqueue scripts and styles for this Gutenberg Block in the editor view. |
| 31 |
add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts_editor' ) ); |
| 32 |
|
| 33 |
// Enqueue scripts and styles for this Gutenberg Block in the editor and frontend views. |
| 34 |
add_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend', array( $this, 'enqueue_scripts' ) ); |
| 35 |
add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) ); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Enqueues scripts for this Gutenberg Block in the editor view. |
| 41 |
* |
| 42 |
* @since 1.9.8.5 |
| 43 |
*/ |
| 44 |
public function enqueue_scripts_editor() { |
| 45 |
|
| 46 |
wp_enqueue_script( 'convertkit-gutenberg-block-product', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-product.js', array( 'convertkit-gutenberg' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Enqueues scripts for this Gutenberg Block in the editor and frontend views. |
| 52 |
* |
| 53 |
* @since 1.9.8.5 |
| 54 |
*/ |
| 55 |
public function enqueue_scripts() { |
| 56 |
|
| 57 |
// Get URL for commerce.js from Products. |
| 58 |
$convertkit_products = new ConvertKit_Resource_Products(); |
| 59 |
$commerce_js_url = $convertkit_products->get_commerce_js_url(); |
| 60 |
|
| 61 |
// Bail if the commerce.js URL could not be fetched, as this means there are no Products. |
| 62 |
if ( ! $commerce_js_url ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// Enqueue. |
| 67 |
wp_enqueue_script( 'convertkit-commerce', $commerce_js_url, array(), false, true ); // phpcs:ignore |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Enqueues styles for this Gutenberg Block in the editor and frontend views. |
| 73 |
* |
| 74 |
* @since 1.9.8.5 |
| 75 |
*/ |
| 76 |
public function enqueue_styles() { |
| 77 |
|
| 78 |
wp_enqueue_style( 'convertkit-gutenberg-block-product-frontend', CONVERTKIT_PLUGIN_URL . '/resources/frontend/css/product.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns this block's programmatic name, excluding the convertkit- prefix. |
| 84 |
* |
| 85 |
* @since 1.9.8.5 |
| 86 |
*/ |
| 87 |
public function get_name() { |
| 88 |
|
| 89 |
/** |
| 90 |
* This will register as: |
| 91 |
* - a shortcode, with the name [convertkit_product]. |
| 92 |
* - a Gutenberg block, with the name convertkit/product. |
| 93 |
*/ |
| 94 |
return 'product'; |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns this block's Title, Icon, Categories, Keywords and properties. |
| 100 |
* |
| 101 |
* @since 1.9.8.5 |
| 102 |
*/ |
| 103 |
public function get_overview() { |
| 104 |
|
| 105 |
return array( |
| 106 |
'title' => __( 'ConvertKit Product', 'convertkit' ), |
| 107 |
'description' => __( 'Displays a button to purchase a ConvertKit product.', 'convertkit' ), |
| 108 |
'icon' => 'resources/backend/images/block-icon-product.png', |
| 109 |
'category' => 'convertkit', |
| 110 |
'keywords' => array( |
| 111 |
__( 'ConvertKit', 'convertkit' ), |
| 112 |
__( 'Product', 'convertkit' ), |
| 113 |
), |
| 114 |
|
| 115 |
// Function to call when rendering as a block or a shortcode on the frontend web site. |
| 116 |
'render_callback' => array( $this, 'render' ), |
| 117 |
|
| 118 |
// Shortcode: TinyMCE / QuickTags Modal Width and Height. |
| 119 |
'modal' => array( |
| 120 |
'width' => 500, |
| 121 |
'height' => 290, |
| 122 |
), |
| 123 |
|
| 124 |
// Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals. |
| 125 |
'shortcode_include_closing_tag' => false, |
| 126 |
|
| 127 |
// Gutenberg: Block Icon in Editor. |
| 128 |
'gutenberg_icon' => file_get_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-product.svg' ), /* phpcs:ignore */ |
| 129 |
|
| 130 |
// Gutenberg: Example image showing how this block looks when choosing it in Gutenberg. |
| 131 |
'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . '/resources/backend/images/block-example-product.png', |
| 132 |
|
| 133 |
// Gutenberg: Help description, displayed when no settings defined for a newly added Block. |
| 134 |
'gutenberg_help_description' => __( 'Select a Product using the Product option in the Gutenberg sidebar.', 'convertkit' ), |
| 135 |
|
| 136 |
// Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor. |
| 137 |
// If not defined, render_callback above will be used. |
| 138 |
'gutenberg_preview_render_callback' => 'convertKitGutenbergProductBlockRenderPreview', |
| 139 |
); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns this block's Attributes |
| 145 |
* |
| 146 |
* @since 1.9.8.5 |
| 147 |
*/ |
| 148 |
public function get_attributes() { |
| 149 |
|
| 150 |
return array( |
| 151 |
// Block attributes. |
| 152 |
'product' => array( |
| 153 |
'type' => 'string', |
| 154 |
'default' => $this->get_default_value( 'product' ), |
| 155 |
), |
| 156 |
'text' => array( |
| 157 |
'type' => 'string', |
| 158 |
'default' => $this->get_default_value( 'text' ), |
| 159 |
), |
| 160 |
|
| 161 |
// The below are built in Gutenberg attributes registered in get_supports(). |
| 162 |
|
| 163 |
// Color. |
| 164 |
'backgroundColor' => array( |
| 165 |
'type' => 'string', |
| 166 |
), |
| 167 |
'textColor' => array( |
| 168 |
'type' => 'string', |
| 169 |
), |
| 170 |
|
| 171 |
// Typography. |
| 172 |
'fontSize' => array( |
| 173 |
'type' => 'string', |
| 174 |
), |
| 175 |
|
| 176 |
// Spacing/Dimensions > Padding. |
| 177 |
'style' => array( |
| 178 |
'type' => 'object', |
| 179 |
'visualizers' => array( |
| 180 |
'type' => 'object', |
| 181 |
'padding' => array( |
| 182 |
'type' => 'object', |
| 183 |
'top' => array( |
| 184 |
'type' => 'boolean', |
| 185 |
), |
| 186 |
'bottom' => array( |
| 187 |
'type' => 'boolean', |
| 188 |
), |
| 189 |
'left' => array( |
| 190 |
'type' => 'boolean', |
| 191 |
), |
| 192 |
'right' => array( |
| 193 |
'type' => 'boolean', |
| 194 |
), |
| 195 |
), |
| 196 |
), |
| 197 |
), |
| 198 |
|
| 199 |
// Always required for Gutenberg. |
| 200 |
'is_gutenberg_example' => array( |
| 201 |
'type' => 'boolean', |
| 202 |
'default' => false, |
| 203 |
), |
| 204 |
); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Returns this block's supported built-in Attributes. |
| 210 |
* |
| 211 |
* @since 1.9.8.5 |
| 212 |
* |
| 213 |
* @return array Supports |
| 214 |
*/ |
| 215 |
public function get_supports() { |
| 216 |
|
| 217 |
return array( |
| 218 |
'className' => true, |
| 219 |
'color' => array( |
| 220 |
'background' => true, |
| 221 |
'text' => true, |
| 222 |
), |
| 223 |
'typography' => array( |
| 224 |
'fontSize' => true, |
| 225 |
), |
| 226 |
'spacing' => array( |
| 227 |
'padding' => array( |
| 228 |
'horizontal', |
| 229 |
'vertical', |
| 230 |
), |
| 231 |
), |
| 232 |
); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns this block's Fields |
| 238 |
* |
| 239 |
* @since 1.9.8.5 |
| 240 |
* |
| 241 |
* @return bool|array |
| 242 |
*/ |
| 243 |
public function get_fields() { |
| 244 |
|
| 245 |
// Bail if the request is not for the WordPress Administration or frontend editor. |
| 246 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
// Get ConvertKit Products. |
| 251 |
$products = array(); |
| 252 |
$convertkit_products = new ConvertKit_Resource_Products(); |
| 253 |
if ( $convertkit_products->exist() ) { |
| 254 |
foreach ( $convertkit_products->get() as $product ) { |
| 255 |
$products[ absint( $product['id'] ) ] = sanitize_text_field( $product['name'] ); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// Get Settings. |
| 260 |
$settings = new ConvertKit_Settings(); |
| 261 |
|
| 262 |
// Gutenberg's built-in fields (such as styling, padding etc) don't need to be defined here, as they'll be included |
| 263 |
// automatically by Gutenberg. |
| 264 |
return array( |
| 265 |
'product' => array( |
| 266 |
'label' => __( 'Product', 'convertkit' ), |
| 267 |
'type' => 'select', |
| 268 |
'values' => $products, |
| 269 |
'data' => array( |
| 270 |
'products' => $convertkit_products->get(), |
| 271 |
'api_key' => $settings->get_api_key(), |
| 272 |
), |
| 273 |
), |
| 274 |
'text' => array( |
| 275 |
'label' => __( 'Button Text', 'convertkit' ), |
| 276 |
'type' => 'text', |
| 277 |
'description' => __( 'The text to display for the button.', 'convertkit' ), |
| 278 |
), |
| 279 |
|
| 280 |
// These fields will only display on the shortcode, and are deliberately not registered in get_attributes(), |
| 281 |
// because Gutenberg will register its own color pickers for link, background and text. |
| 282 |
'background_color' => array( |
| 283 |
'label' => __( 'Background color', 'convertkit' ), |
| 284 |
'type' => 'color', |
| 285 |
), |
| 286 |
'text_color' => array( |
| 287 |
'label' => __( 'Text color', 'convertkit' ), |
| 288 |
'type' => 'color', |
| 289 |
), |
| 290 |
); |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Returns this block's UI panels / sections. |
| 296 |
* |
| 297 |
* @since 1.9.8.5 |
| 298 |
* |
| 299 |
* @return bool|array |
| 300 |
*/ |
| 301 |
public function get_panels() { |
| 302 |
|
| 303 |
// Bail if the request is not for the WordPress Administration or frontend editor. |
| 304 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
// Gutenberg's built-in fields (such as styling, padding etc) don't need to be defined here, as they'll be included |
| 309 |
// automatically by Gutenberg. |
| 310 |
return array( |
| 311 |
'general' => array( |
| 312 |
'label' => __( 'General', 'convertkit' ), |
| 313 |
'fields' => array( |
| 314 |
'product', |
| 315 |
'text', |
| 316 |
'background_color', |
| 317 |
'text_color', |
| 318 |
), |
| 319 |
), |
| 320 |
); |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Returns this block's Default Values |
| 326 |
* |
| 327 |
* @since 1.9.8.5 |
| 328 |
* |
| 329 |
* @return array |
| 330 |
*/ |
| 331 |
public function get_default_values() { |
| 332 |
|
| 333 |
return array( |
| 334 |
'product' => '', |
| 335 |
'text' => __( 'Buy my product', 'convertkit' ), |
| 336 |
'background_color' => '', |
| 337 |
'text_color' => '', |
| 338 |
|
| 339 |
// Built-in Gutenberg block attributes. |
| 340 |
'backgroundColor' => '', |
| 341 |
'textColor' => '', |
| 342 |
'fontSize' => '', |
| 343 |
'style' => array( |
| 344 |
'visualizers' => array( |
| 345 |
'padding' => array( |
| 346 |
'top' => '', |
| 347 |
'bottom' => '', |
| 348 |
'left' => '', |
| 349 |
'right' => '', |
| 350 |
), |
| 351 |
), |
| 352 |
), |
| 353 |
); |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Returns the block's output, based on the supplied configuration attributes. |
| 359 |
* |
| 360 |
* @since 1.9.8.5 |
| 361 |
* |
| 362 |
* @param array $atts Block / Shortcode Attributes. |
| 363 |
* @return string Output |
| 364 |
*/ |
| 365 |
public function render( $atts ) { |
| 366 |
|
| 367 |
// Parse attributes, defining fallback defaults if required |
| 368 |
// and moving some attributes (such as Gutenberg's styles), if defined. |
| 369 |
$atts = $this->sanitize_and_declare_atts( $atts ); |
| 370 |
|
| 371 |
// Setup Settings class. |
| 372 |
$settings = new ConvertKit_Settings(); |
| 373 |
|
| 374 |
// Get Products Resource. |
| 375 |
$convertkit_products = new ConvertKit_Resource_Products(); |
| 376 |
|
| 377 |
// Build HTML. |
| 378 |
$html = $convertkit_products->get_html( $atts['product'], $atts['text'], $atts['_css_classes'], $atts['_css_styles'], $this->is_block_editor_request() ); |
| 379 |
|
| 380 |
// Bail if an error occured. |
| 381 |
if ( is_wp_error( $html ) ) { |
| 382 |
if ( $settings->debug_enabled() ) { |
| 383 |
return '<!-- ' . $html->get_error_message() . ' -->'; |
| 384 |
} |
| 385 |
|
| 386 |
return ''; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Filter the block's content immediately before it is output. |
| 391 |
* |
| 392 |
* @since 1.9.8.5 |
| 393 |
* |
| 394 |
* @param string $html ConvertKit Product button HTML. |
| 395 |
* @param array $atts Block Attributes. |
| 396 |
*/ |
| 397 |
$html = apply_filters( 'convertkit_block_product_render', $html, $atts ); |
| 398 |
|
| 399 |
return $html; |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
} |
| 404 |
|