| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Product Link Block Formatter class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit Product Link Block Formatter class. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Block_Formatter_Product_Link extends ConvertKit_Block_Formatter { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the ConvertKit Products resource class. |
| 19 |
* |
| 20 |
* @since 2.2.0 |
| 21 |
* |
| 22 |
* @var bool|ConvertKit_Resource_Products |
| 23 |
*/ |
| 24 |
public $products = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* Holds the Post's content. |
| 28 |
* |
| 29 |
* @since 2.2.0 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $content = ''; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor |
| 37 |
* |
| 38 |
* @since 2.2.0 |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
// Register this as a Gutenberg block formatter in the ConvertKit Plugin, |
| 43 |
// if forms exist on ConvertKit. |
| 44 |
$this->products = new ConvertKit_Resource_Products( 'block_formatter_register' ); |
| 45 |
if ( $this->products->exist() ) { |
| 46 |
add_filter( 'convertkit_get_block_formatters', array( $this, 'register' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns this formatter's programmatic name, excluding the convertkit- prefix. |
| 53 |
* |
| 54 |
* @since 2.2.0 |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function get_name() { |
| 59 |
|
| 60 |
return 'product-link'; |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns this formatters's Title, Description and Icon |
| 66 |
* |
| 67 |
* @since 2.2.0 |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function get_overview() { |
| 72 |
|
| 73 |
return array( |
| 74 |
'title' => __( 'Kit Product Trigger', 'convertkit' ), |
| 75 |
'description' => __( 'Displays the Product modal when the link is pressed.', 'convertkit' ), |
| 76 |
'icon' => 'resources/backend/images/block-icon-product.svg', |
| 77 |
|
| 78 |
// Gutenberg: Block Icon in Editor. |
| 79 |
'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-product.svg' ), |
| 80 |
); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns this formatter's attributes, which are applied |
| 86 |
* to the tag. |
| 87 |
* |
| 88 |
* @since 2.2.0 |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public function get_attributes() { |
| 93 |
|
| 94 |
return array( |
| 95 |
'data-id' => '', |
| 96 |
'data-commerce' => '', |
| 97 |
'href' => '', |
| 98 |
); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns this formatter's fields to display when the formatter |
| 104 |
* button is clicked in the toolbar. |
| 105 |
* |
| 106 |
* @since 2.2.0 |
| 107 |
* |
| 108 |
* @return bool|array |
| 109 |
*/ |
| 110 |
public function get_fields() { |
| 111 |
|
| 112 |
// Bail if the request is not for the WordPress Administration or frontend editor. |
| 113 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
// Get ConvertKit Products. |
| 118 |
$products = array(); |
| 119 |
$products_data = array(); |
| 120 |
if ( $this->products->exist() ) { |
| 121 |
foreach ( $this->products->get() as $product ) { |
| 122 |
$products[ absint( $product['id'] ) ] = sanitize_text_field( $product['name'] ); |
| 123 |
$products_data[ absint( $product['id'] ) ] = array( |
| 124 |
'data-id' => sanitize_text_field( $product['id'] ), |
| 125 |
'data-commerce' => '1', |
| 126 |
'href' => $product['url'], |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// Return field. |
| 132 |
return array( |
| 133 |
'data-id' => array( |
| 134 |
'label' => __( 'Product', 'convertkit' ), |
| 135 |
'type' => 'select', |
| 136 |
'description' => __( 'The product modal to display when the text is clicked.', 'convertkit' ), |
| 137 |
|
| 138 |
// Key/value pairs for the <select> dropdown. |
| 139 |
'values' => $products, |
| 140 |
|
| 141 |
// Contains all additional data required to build the link. |
| 142 |
'data' => $products_data, |
| 143 |
), |
| 144 |
); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|