| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit WooCommerce Product Form class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Outputs ConvertKit Forms on WooCommerce Products. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_WooCommerce_Product_Form { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor. |
| 19 |
* |
| 20 |
* @since 1.9.6 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
add_action( 'convertkit_output_output_form', array( $this, 'output_form' ) ); |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Output the ConvertKit Form after the Product's Summary, as `the_content` isn't a reliable |
| 30 |
* hook to use for output when viewing a WooCommerce Product. |
| 31 |
* |
| 32 |
* @since 1.9.6 |
| 33 |
*/ |
| 34 |
public function output_form() { |
| 35 |
|
| 36 |
// Bail if WooCommerce isn't active. |
| 37 |
if ( ! $this->is_active() ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// Bail if not a singular Product. |
| 42 |
if ( ! is_singular( 'product' ) ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
// Remove the_content filter, as this isn't always reliable. |
| 47 |
remove_filter( 'the_content', array( WP_ConvertKit()->get_class( 'output' ), 'append_form_to_content' ) ); |
| 48 |
|
| 49 |
// Output the Form after the Product's Summary. |
| 50 |
add_action( 'woocommerce_after_single_product_summary', array( $this, 'append_form_to_product_summary' ) ); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Append the ConvertKit Form to the Product's Summary. |
| 56 |
* |
| 57 |
* @since 1.9.6 |
| 58 |
*/ |
| 59 |
public function append_form_to_product_summary() { |
| 60 |
|
| 61 |
// Output is already escaped in append_form_to_content(). |
| 62 |
echo WP_ConvertKit()->get_class( 'output' )->append_form_to_content( '' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Determines if the WooCommerce Plugin is active. |
| 68 |
* |
| 69 |
* @since 1.9.6 |
| 70 |
* |
| 71 |
* @return bool Plugin Active. |
| 72 |
*/ |
| 73 |
public function is_active() { |
| 74 |
|
| 75 |
return ( defined( 'WC_PLUGIN_FILE' ) ? true : false ); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
// Bootstrap. |
| 82 |
add_action( |
| 83 |
'convertkit_initialize_global', |
| 84 |
function () { |
| 85 |
|
| 86 |
new ConvertKit_WooCommerce_Product_Form(); |
| 87 |
|
| 88 |
} |
| 89 |
); |
| 90 |
|