| 1 |
<?php |
| 2 |
/** |
| 3 |
* This class is responsible for rendering the testimonial section on frontend checkout pages. |
| 4 |
* |
| 5 |
* @package WPFunnels |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Classes\TestimonialActions; |
| 8 |
|
| 9 |
use WPFunnels\Wpfnl_functions; |
| 10 |
|
| 11 |
class Wpfnl_Testimonial_Action { |
| 12 |
|
| 13 |
/** |
| 14 |
* Testimonial config for the current checkout step. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $config = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* Current checkout step ID. |
| 22 |
* |
| 23 |
* @var int |
| 24 |
*/ |
| 25 |
protected $checkout_id = 0; |
| 26 |
|
| 27 |
public function __construct() { |
| 28 |
add_action( 'wpfunnels/before_checkout_form', array( $this, 'load_actions' ), 20, 1 ); |
| 29 |
add_action( 'wpfunnels/elementor_render_testimonial', array( $this, 'load_elementor_actions' ), 9999, 1 ); |
| 30 |
add_action( 'woocommerce_checkout_update_order_review', array( $this, 'load_ajax_actions' ), 5 ); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Load action hooks for testimonial render (all builder paths). |
| 36 |
* |
| 37 |
* @param int $checkout_id |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function load_actions( $checkout_id ) { |
| 42 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 43 |
require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 44 |
} |
| 45 |
|
| 46 |
if ( is_plugin_active( 'elementor/elementor.php' ) ) { |
| 47 |
if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$this->checkout_id = (int) $checkout_id; |
| 53 |
$this->config = $this->get_config( $this->checkout_id ); |
| 54 |
|
| 55 |
$this->trigger_testimonial_actions(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Load action hooks during the update_order_review AJAX request. |
| 61 |
* |
| 62 |
* `wpfunnels/before_checkout_form` only fires while the full checkout form is |
| 63 |
* rendered, so on an AJAX refresh nothing would be listening on |
| 64 |
* `wpfunnels/after_order_total`. The refreshed `.wpfnl-after-order-total` |
| 65 |
* fragment would then come back holding only the coupon field and replace the |
| 66 |
* rendered one, making the testimonial vanish after the first refresh. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function load_ajax_actions() { |
| 71 |
$checkout_id = Wpfnl_functions::get_checkout_id_from_post( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 72 |
|
| 73 |
if ( ! $checkout_id ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$this->checkout_id = (int) $checkout_id; |
| 78 |
$this->config = $this->get_config( $this->checkout_id ); |
| 79 |
|
| 80 |
$this->trigger_testimonial_actions(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* Load action hooks for Elementor editor/preview. |
| 86 |
* |
| 87 |
* @param int $checkout_id |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function load_elementor_actions( $checkout_id ) { |
| 92 |
$this->checkout_id = (int) $checkout_id; |
| 93 |
$this->config = $this->get_config( $this->checkout_id ); |
| 94 |
$this->trigger_testimonial_actions(); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Get and validate testimonial config from post meta. |
| 100 |
* |
| 101 |
* @param int $checkout_id |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
private function get_config( $checkout_id ) { |
| 106 |
$raw = get_post_meta( $checkout_id, 'wpf_checkout_testimonial', true ); |
| 107 |
|
| 108 |
if ( ! is_array( $raw ) ) { |
| 109 |
$raw = array(); |
| 110 |
} |
| 111 |
|
| 112 |
$defaults = array( |
| 113 |
'enabled' => false, |
| 114 |
'layout' => 'layout-1', |
| 115 |
'position' => 'after_bump', |
| 116 |
'testimonial' => array( |
| 117 |
'text' => '', |
| 118 |
'author' => '', |
| 119 |
'rating' => 5, |
| 120 |
), |
| 121 |
'guarantee' => array( |
| 122 |
'headline' => '', |
| 123 |
'text' => '', |
| 124 |
'days' => 30, |
| 125 |
'image' => array( 'id' => 0, 'url' => '' ), |
| 126 |
), |
| 127 |
'benefits' => array( |
| 128 |
'title' => 'Here\'s what you get', |
| 129 |
'items' => array(), |
| 130 |
), |
| 131 |
); |
| 132 |
|
| 133 |
return wp_parse_args( $raw, $defaults ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Register WooCommerce/WPFunnels action hooks based on the configured position. |
| 139 |
* |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
private function trigger_testimonial_actions() { |
| 143 |
if ( empty( $this->config['enabled'] ) ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
$position = isset( $this->config['position'] ) ? $this->config['position'] : 'after_bump'; |
| 148 |
|
| 149 |
if ( 'after_bump' === $position ) { |
| 150 |
add_action( 'wpfunnels/after_order_total', array( $this, 'render' ), 20 ); |
| 151 |
} elseif ( 'before_bump' === $position ) { |
| 152 |
add_action( 'woocommerce_review_order_before_cart_contents', array( $this, 'render' ), 20 ); |
| 153 |
} elseif ( 'above_form' === $position ) { |
| 154 |
add_action( 'woocommerce_before_checkout_form', array( $this, 'render' ), 20 ); |
| 155 |
} elseif ( 'below_form' === $position ) { |
| 156 |
add_action( 'woocommerce_review_order_after_payment', array( $this, 'render' ), 20 ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* Render the testimonial section HTML. |
| 163 |
* |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
public function render() { |
| 167 |
if ( empty( $this->config['enabled'] ) ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
$layout = sanitize_key( isset( $this->config['layout'] ) ? $this->config['layout'] : 'layout-1' ); |
| 172 |
$template = WPFNL_DIR . 'public/modules/checkout/testimonial/' . $layout . '.php'; |
| 173 |
|
| 174 |
if ( file_exists( $template ) ) { |
| 175 |
load_template( $template, false, $this->config ); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|