| 1 |
<?php |
| 2 |
namespace Sellkit\Blocks\Render; |
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit\Blocks\Sellkit_Blocks; |
| 6 |
use Sellkit_Funnel; |
| 7 |
use Sellkit\Global_Checkout\Checkout as Global_Checkout; |
| 8 |
|
| 9 |
/** |
| 10 |
* Accept/Reject Button block. |
| 11 |
* |
| 12 |
* @since 2.3.0 |
| 13 |
*/ |
| 14 |
class Accept_Reject_Button { |
| 15 |
private $post_id; |
| 16 |
|
| 17 |
/** |
| 18 |
* Helper id. |
| 19 |
* |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
private $helper_id = 0; |
| 23 |
|
| 24 |
/** |
| 25 |
* Accept_Reject_Button constructor. |
| 26 |
* |
| 27 |
* @since 2.3.0 |
| 28 |
*/ |
| 29 |
public function __construct( $post_id ) { |
| 30 |
$this->post_id = $post_id; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Check block activation. |
| 35 |
* |
| 36 |
* @since 2.3.0 |
| 37 |
* @return boolean |
| 38 |
*/ |
| 39 |
public function is_active() { |
| 40 |
if ( empty( $this->post_id ) ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
$step_data = get_post_meta( $this->post_id, 'step_data', true ); |
| 45 |
|
| 46 |
if ( ! empty( $step_data['type']['key'] ) && in_array( $step_data['type']['key'], [ 'downsell', 'upsell' ], true ) ) { |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
if ( $this->is_active_steps() ) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
$global_checkout_id = get_option( Global_Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ); |
| 55 |
|
| 56 |
if ( |
| 57 |
0 !== $global_checkout_id || |
| 58 |
'publish' === get_post_status( (int) $global_checkout_id ) || |
| 59 |
( function_exists( 'is_checkout' ) && is_checkout() ) |
| 60 |
) { |
| 61 |
$steps = get_post_meta( $global_checkout_id, 'nodes', true ); |
| 62 |
|
| 63 |
if ( ! is_array( $steps ) ) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
foreach ( $steps as $step ) { |
| 68 |
$step['type'] = (array) $step['type']; |
| 69 |
|
| 70 |
if ( 'checkout' === $step['type']['key'] ) { |
| 71 |
$this->post_id = $step['page_id']; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ( $this->is_active_steps() ) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Check if block is available based on steps. |
| 85 |
* |
| 86 |
* @since 2.3.0 |
| 87 |
* @return boolean |
| 88 |
*/ |
| 89 |
public function is_active_steps() { |
| 90 |
$funnel = new Sellkit_Funnel( $this->post_id ); |
| 91 |
$next_step = $funnel->next_step_data; |
| 92 |
$popups = [ 'upsell', 'downsell' ]; |
| 93 |
|
| 94 |
if ( ! isset( $funnel->next_step_data['type'] ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$funnel->next_step_data['type'] = (array) $funnel->next_step_data['type']; |
| 99 |
|
| 100 |
if ( 'decision' === $funnel->next_step_data['type']['key'] ) { |
| 101 |
$page_id = isset( $funnel->next_step_data['page_id'] ) ? $funnel->next_step_data['page_id'] : 0; |
| 102 |
|
| 103 |
$this->helper_id = $this->post_id; |
| 104 |
|
| 105 |
if ( ! $page_id ) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
return $this->take_care_of_decision_step( $page_id, $funnel->funnel_id ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( in_array( $funnel->next_step_data['type']['key'], $popups, true ) ) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Gets step id before the decision step and return result. |
| 121 |
* |
| 122 |
* @param int $step_id id of the step before decision step. |
| 123 |
* @param int $funnel_id id of the funnel. |
| 124 |
* @since 2.3.0 |
| 125 |
*/ |
| 126 |
private function take_care_of_decision_step( $step_id, $funnel_id = null ) { |
| 127 |
$funnel = new Sellkit_Funnel( $step_id ); |
| 128 |
$conditions = ! empty( $funnel->current_step_data['data']['conditions'] ) ? $funnel->current_step_data['data']['conditions'] : []; |
| 129 |
$is_valid = sellkit_conditions_validation( $conditions ); |
| 130 |
$next_step = $funnel->next_no_step_data; |
| 131 |
$popups = [ 'upsell', 'downsell' ]; |
| 132 |
|
| 133 |
if ( $is_valid ) { |
| 134 |
$next_step = $funnel->next_step_data; |
| 135 |
} |
| 136 |
|
| 137 |
$next_step['type'] = (array) $next_step['type']; |
| 138 |
|
| 139 |
if ( 'decision' === $next_step['type']['key'] ) { |
| 140 |
$this->take_care_of_decision_step( $next_step['page_id'] ); |
| 141 |
} |
| 142 |
|
| 143 |
if ( in_array( $funnel->next_step_data['type']['key'], $popups, true ) ) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Register block from meta. |
| 152 |
* |
| 153 |
* @since 2.3.0 |
| 154 |
*/ |
| 155 |
public function register_block_meta() { |
| 156 |
if ( ! $this->is_active() ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
register_block_type_from_metadata( |
| 161 |
__DIR__, |
| 162 |
[ |
| 163 |
'render_callback' => [ $this, 'render' ], |
| 164 |
] |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Render block in front-end. |
| 170 |
* |
| 171 |
* @param array $attributes Block attributes. |
| 172 |
* @since 2.3.0 |
| 173 |
* @return string |
| 174 |
*/ |
| 175 |
public function render( $attributes, $content, $instance ) { |
| 176 |
if ( ! is_admin() ) { |
| 177 |
Sellkit_Blocks::load_scripts( 'accept-reject-button' ); |
| 178 |
} |
| 179 |
|
| 180 |
$order_key = sellkit_htmlspecialchars( INPUT_GET, 'order-key' ); |
| 181 |
$button_type_class = 'accept' === $attributes['offerType'] ? 'sellkit-upsell-accept-button' : 'sellkit-upsell-reject-button'; |
| 182 |
|
| 183 |
$button = sprintf( |
| 184 |
'<a class="%1$s %2$s" style="%4$s" data-order-key="%3$s">%5$s</a>', |
| 185 |
esc_attr( $attributes['dynamicClassNames'] ), |
| 186 |
esc_attr( $button_type_class ), |
| 187 |
esc_attr( $order_key ), |
| 188 |
esc_attr( $attributes['combinedStyle'] ), |
| 189 |
$attributes['title'] |
| 190 |
); |
| 191 |
|
| 192 |
ob_start(); |
| 193 |
?> |
| 194 |
<div class="sellkit-accept-reject-button-block-frontend"> |
| 195 |
<?php echo wp_kses_post( $button ); ?> |
| 196 |
<div class="sellkit-upsell-popup"> |
| 197 |
<div class="sellkit-upsell-popup-body"> |
| 198 |
<div class="sellkit-upsell-popup-header"> |
| 199 |
<img src="<?php echo esc_url( sellkit()->plugin_url() . 'assets/img/icons/close-cross.svg' ); ?>" > |
| 200 |
</div> |
| 201 |
<div class="sellkit-upsell-popup-content"> |
| 202 |
<div class="sellkit-upsell-popup-icon"> |
| 203 |
<img class="rotate sellkit-upsell-updating active" src="<?php echo esc_url( sellkit()->plugin_url() . 'assets/img/icons/sync-alt.svg' ); ?>" > |
| 204 |
<img class="sellkit-upsell-accepted" src="<?php echo esc_url( sellkit()->plugin_url() . 'assets/img/icons/check-circle.svg' ); ?>" > |
| 205 |
<img class="sellkit-upsell-rejected" src="<?php echo esc_url( sellkit()->plugin_url() . 'assets/img/icons/times-circle.svg' ); ?>" > |
| 206 |
</div> |
| 207 |
<div class="sellkit-upsell-popup-text"> |
| 208 |
<div class="sellkit-upsell-updating active"> |
| 209 |
<?php esc_html_e( 'Updating your order…', 'sellkit' ); ?> |
| 210 |
</div> |
| 211 |
<div class="sellkit-upsell-accepted"> |
| 212 |
<?php esc_html_e( 'Congratulations! Your item has been successfully added to the order.', 'sellkit' ); ?> |
| 213 |
</div> |
| 214 |
<div class="sellkit-upsell-rejected"> |
| 215 |
<?php esc_html_e( 'Sorry! We were unable to add this item to your order.', 'sellkit' ); ?> |
| 216 |
</div> |
| 217 |
</div> |
| 218 |
</div> |
| 219 |
</div> |
| 220 |
</div> |
| 221 |
</div> |
| 222 |
<?php |
| 223 |
return ob_get_clean(); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|