| 1 |
<?php |
| 2 |
/** |
| 3 |
* Order details shortcode class |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Shortcodes; |
| 8 |
|
| 9 |
use WPFunnels\Wpfnl_functions; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class WC_Shortcode_Optin |
| 13 |
* |
| 14 |
* @package WPFunnels\Shortcodes |
| 15 |
*/ |
| 16 |
class Wpfnl_Shortcode_Order_details { |
| 17 |
|
| 18 |
/** |
| 19 |
* Attributes |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $attributes = array(); |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Wpfnl_Shortcode_Order_details constructor. |
| 28 |
* |
| 29 |
* @param array $attributes |
| 30 |
*/ |
| 31 |
public function __construct( $attributes = array() ) { |
| 32 |
$this->attributes = $this->parse_attributes( $attributes ); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Get shortcode attributes. |
| 38 |
* |
| 39 |
* @since 3.2.0 |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public function get_attributes() { |
| 43 |
return $this->attributes; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Parse attributes |
| 49 |
* |
| 50 |
* @param $attributes |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
protected function parse_attributes( $attributes ) { |
| 55 |
$attributes = shortcode_atts( |
| 56 |
array( |
| 57 |
'class' => '', |
| 58 |
'child_order_class' => '', |
| 59 |
'step_id' => '', |
| 60 |
), |
| 61 |
$attributes |
| 62 |
); |
| 63 |
return $attributes; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* Get wrapper classes |
| 69 |
* |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
protected function get_wrapper_classes() { |
| 73 |
$classes = array( 'wpfnl', 'wpfnl-order-details-wrapper' ); |
| 74 |
return $classes; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Content of optin form |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public function get_content() { |
| 84 |
$output = ''; |
| 85 |
$show_dummy_order_details = false; |
| 86 |
|
| 87 |
// allow to show the order details markup on preview |
| 88 |
if ( is_admin() ) { |
| 89 |
$show_dummy_order_details = apply_filters( 'wpfunnels/show_dummy_order_details', true ); |
| 90 |
} |
| 91 |
|
| 92 |
if(Wpfnl_functions::is_elementor_active()) { |
| 93 |
$elementor_preview_active = \Elementor\Plugin::$instance->preview->is_preview_mode(); |
| 94 |
if( $elementor_preview_active ) { |
| 95 |
$show_dummy_order_details = apply_filters( 'wpfunnels/show_dummy_order_details', false ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$is_funnel_step = $this->attributes['step_id'] ? Wpfnl_functions::maybe_funnel_step( $this->attributes['step_id'] ) : false; |
| 100 |
|
| 101 |
if( Wpfnl_functions::is_wc_active() && Wpfnl_functions::check_if_this_is_step_type('thankyou') || $show_dummy_order_details || $is_funnel_step ) { |
| 102 |
add_filter( 'woocommerce_order_item_permalink', '__return_false' ); |
| 103 |
|
| 104 |
$order = false; |
| 105 |
|
| 106 |
$id_param = 'wpfnl-order'; |
| 107 |
$key_param = 'wpfnl-key'; |
| 108 |
$child_orders = null; |
| 109 |
if ( !isset($_GET[$id_param]) && $show_dummy_order_details ) { |
| 110 |
$args = array( |
| 111 |
'limit' => 1, |
| 112 |
'order' => 'DESC', |
| 113 |
'post_type' => 'shop_order', |
| 114 |
'status' => array( 'completed', 'processing' ), |
| 115 |
); |
| 116 |
|
| 117 |
$latest_order = wc_get_orders( $args ); |
| 118 |
|
| 119 |
$order_id = ( ! empty( $latest_order ) ) ? current( $latest_order )->get_id() : 0; |
| 120 |
if ( $order_id > 0 ) { |
| 121 |
/** |
| 122 |
* Fires when the page is the "Thank You" page. |
| 123 |
* |
| 124 |
* This hook allows you to perform actions specific to the "Thank You" page, |
| 125 |
* typically triggered if there is at least one order created |
| 126 |
* |
| 127 |
* @param int $order_id The ID of the completed order. |
| 128 |
* @since 2.0.0 |
| 129 |
*/ |
| 130 |
do_action('wpfunnels/page_is_thankyou',$order_id); |
| 131 |
$order = wc_get_order( $order_id ); |
| 132 |
if ( ! $order ) { |
| 133 |
$order = false; |
| 134 |
} |
| 135 |
} else { |
| 136 |
return '<p class="woocommerce-notice">' . __( 'No completed orders found in your shop for demo.', 'wpfnl' ) . '</p>'; |
| 137 |
} |
| 138 |
} |
| 139 |
else { |
| 140 |
|
| 141 |
if (!isset($_GET[$id_param])) { //phpcs:ignore |
| 142 |
return '<p class="woocommerce-notice">' . __( 'Order not found. You cannot access this page directly.', 'wpfnl' ) . '</p>'; |
| 143 |
} |
| 144 |
|
| 145 |
$order_id = empty($_GET[$id_param]) ? 0 : intval($_GET[$id_param]); |
| 146 |
$order_key = empty($_GET[$key_param]) ? '' : wc_clean(wp_unslash($_GET[$key_param])); |
| 147 |
|
| 148 |
if ( $order_id > 0 ) { |
| 149 |
/** |
| 150 |
* Fires when the page is the "Thank You" page. |
| 151 |
* |
| 152 |
* This hook allows you to perform actions specific to the "Thank You" page, |
| 153 |
* typically triggered if there is at least one order created |
| 154 |
* |
| 155 |
* @param int $order_id The ID of the completed order. |
| 156 |
* @since 2.0.0 |
| 157 |
*/ |
| 158 |
do_action('wpfunnels/page_is_thankyou',$order_id); |
| 159 |
$order = wc_get_order( $order_id ); |
| 160 |
|
| 161 |
if (!Wpfnl_functions::is_valid_order_owner( $order )){ |
| 162 |
return '<p class="woocommerce-notice">' . __( 'You are not authorized to view the order details.', 'wpfnl' ) . '</p>'; |
| 163 |
} |
| 164 |
|
| 165 |
$child_orders = get_post_meta($order_id, '_wpfnl_offer_child_orders', true); |
| 166 |
if ( ! $order || $order->get_order_key() !== $order_key ) { |
| 167 |
$order = false; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if( is_a( $order, 'WC_Order' ) ) { |
| 173 |
$class = isset($this->attributes['class']) ? esc_html($this->attributes['class']) : ''; |
| 174 |
$child_order_class = isset($this->attributes['child_order_class']) ? esc_html($this->attributes['child_order_class']) : ''; |
| 175 |
// Empty awaiting payment session. |
| 176 |
unset( WC()->session->order_awaiting_payment ); |
| 177 |
ob_start(); |
| 178 |
echo "<div class='woocommerce'>"; |
| 179 |
echo "<div class='wpfnl-thankyou-wrapper ".$class."' id='wpfnl-thankyou-wrapper'>"; |
| 180 |
wc_get_template( 'checkout/thankyou.php', |
| 181 |
array( |
| 182 |
'order' => $order |
| 183 |
) |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
if ( $child_orders ) { |
| 189 |
echo "<div class='wpfnl-child-order-wrapper ".$child_order_class."' id='wpfnl-child-order-wrapper'>"; |
| 190 |
foreach ( $child_orders as $order_id => $value) { |
| 191 |
wc_get_template( 'checkout/thankyou.php', |
| 192 |
array( |
| 193 |
'order' => $order |
| 194 |
) |
| 195 |
); |
| 196 |
} |
| 197 |
echo '</div>'; |
| 198 |
} |
| 199 |
echo '</div>'; |
| 200 |
echo '</div>'; |
| 201 |
$output = ob_get_clean(); |
| 202 |
} |
| 203 |
|
| 204 |
return $output; |
| 205 |
} |
| 206 |
} |
| 207 |
|