| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Shortcodes; |
| 4 |
|
| 5 |
use YayMail\Utils\Helpers; |
| 6 |
use YayMail\Utils\SingletonTrait; |
| 7 |
use YayMail\Abstracts\BaseShortcode; |
| 8 |
use YayMail\Utils\TemplateHelpers; |
| 9 |
|
| 10 |
/** |
| 11 |
* @method: static PaymentsShortcodes get_instance() |
| 12 |
*/ |
| 13 |
class PaymentsShortcodes extends BaseShortcode { |
| 14 |
use SingletonTrait; |
| 15 |
|
| 16 |
public function get_shortcodes() { |
| 17 |
$shortcodes = []; |
| 18 |
$shortcodes[] = [ |
| 19 |
'name' => 'yaymail_order_payment_method', |
| 20 |
'description' => __( 'Payment method', 'yaymail' ), |
| 21 |
'group' => 'payments', |
| 22 |
'callback' => [ $this, 'yaymail_order_payment_method' ], |
| 23 |
]; |
| 24 |
$shortcodes[] = [ |
| 25 |
'name' => 'yaymail_order_payment_link', |
| 26 |
'description' => __( 'Payment Link', 'yaymail' ), |
| 27 |
'attributes' => [ |
| 28 |
'text_link' => __( 'Payment page', 'yaymail' ), |
| 29 |
], |
| 30 |
'group' => 'payments', |
| 31 |
'callback' => [ $this, 'yaymail_order_payment_link' ], |
| 32 |
]; |
| 33 |
$shortcodes[] = [ |
| 34 |
'name' => 'yaymail_order_payment_url', |
| 35 |
'description' => __( 'Payment URL (String)', 'yaymail' ), |
| 36 |
'group' => 'payments', |
| 37 |
'callback' => [ $this, 'yaymail_order_payment_url' ], |
| 38 |
]; |
| 39 |
$shortcodes[] = [ |
| 40 |
'name' => 'yaymail_payment_instructions', |
| 41 |
'description' => __( 'Payment Instructions', 'yaymail' ), |
| 42 |
'group' => 'payments', |
| 43 |
'callback' => [ $this, 'yaymail_payment_instructions' ], |
| 44 |
]; |
| 45 |
$shortcodes[] = [ |
| 46 |
'name' => 'yaymail_payment_transaction_id', |
| 47 |
'description' => __( 'Payment Transaction ID', 'yaymail' ), |
| 48 |
'group' => 'payments', |
| 49 |
'callback' => [ $this, 'yaymail_payment_transaction_id' ], |
| 50 |
]; |
| 51 |
return $shortcodes; |
| 52 |
} |
| 53 |
|
| 54 |
public function yaymail_order_payment_method( $data ) { |
| 55 |
|
| 56 |
$render_data = isset( $data['render_data'] ) ? $data['render_data'] : []; |
| 57 |
|
| 58 |
if ( ! empty( $render_data['is_sample'] ) ) { |
| 59 |
/** |
| 60 |
* Is sample order |
| 61 |
*/ |
| 62 |
return __( 'Direct bank transfer', 'yaymail' ); |
| 63 |
} |
| 64 |
|
| 65 |
$order = Helpers::get_order_from_shortcode_data( $render_data ); |
| 66 |
|
| 67 |
if ( empty( $order ) ) { |
| 68 |
/** |
| 69 |
* Not having order_id |
| 70 |
*/ |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
$order_item_totals = $order->get_order_item_totals(); |
| 75 |
|
| 76 |
return isset( $order_item_totals['payment_method']['value'] ) ? $order_item_totals['payment_method']['value'] : ''; |
| 77 |
} |
| 78 |
|
| 79 |
public function yaymail_order_payment_link( $data, $shortcode_atts = [] ) { |
| 80 |
|
| 81 |
$render_data = isset( $data['render_data'] ) ? $data['render_data'] : []; |
| 82 |
|
| 83 |
$is_placeholder = isset( $data['is_placeholder'] ) ? $data['is_placeholder'] : false; |
| 84 |
|
| 85 |
$text_link = isset( $shortcode_atts['text_link'] ) ? $shortcode_atts['text_link'] : TemplateHelpers::get_content_as_placeholder( 'text_link', __( 'Payment page', 'yaymail' ), $is_placeholder ); |
| 86 |
|
| 87 |
if ( ! empty( $render_data['is_sample'] ) ) { |
| 88 |
/** |
| 89 |
* Is sample order |
| 90 |
*/ |
| 91 |
return '<a href="#">' . $text_link . '</a>'; |
| 92 |
} |
| 93 |
|
| 94 |
$order = Helpers::get_order_from_shortcode_data( $render_data ); |
| 95 |
|
| 96 |
if ( empty( $order ) ) { |
| 97 |
/** |
| 98 |
* Not having order_id |
| 99 |
*/ |
| 100 |
return ''; |
| 101 |
} |
| 102 |
|
| 103 |
return '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . $text_link . '</a>'; |
| 104 |
} |
| 105 |
|
| 106 |
public function yaymail_order_payment_url( $data ) { |
| 107 |
|
| 108 |
$render_data = isset( $data['render_data'] ) ? $data['render_data'] : []; |
| 109 |
|
| 110 |
if ( ! empty( $render_data['is_sample'] ) ) { |
| 111 |
/** |
| 112 |
* Is sample order |
| 113 |
*/ |
| 114 |
return esc_url( wc_get_endpoint_url( 'order-pay', 0, wc_get_checkout_url() ) ); |
| 115 |
} |
| 116 |
|
| 117 |
$order = Helpers::get_order_from_shortcode_data( $render_data ); |
| 118 |
|
| 119 |
if ( empty( $order ) ) { |
| 120 |
/** |
| 121 |
* Not having order_id |
| 122 |
*/ |
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
return $order->get_checkout_payment_url(); |
| 127 |
} |
| 128 |
|
| 129 |
public function yaymail_payment_instructions( $data ) { |
| 130 |
|
| 131 |
$render_data = isset( $data['render_data'] ) ? $data['render_data'] : []; |
| 132 |
|
| 133 |
if ( ! empty( $render_data['is_sample'] ) ) { |
| 134 |
/** |
| 135 |
* Is sample order |
| 136 |
*/ |
| 137 |
return __( 'Payment Instructions', 'yaymail' ); |
| 138 |
} |
| 139 |
|
| 140 |
$order = Helpers::get_order_from_shortcode_data( $render_data ); |
| 141 |
|
| 142 |
if ( empty( $order ) ) { |
| 143 |
/** |
| 144 |
* Not having order_id |
| 145 |
*/ |
| 146 |
return ''; |
| 147 |
} |
| 148 |
$args = [ |
| 149 |
'order' => $order, |
| 150 |
]; |
| 151 |
|
| 152 |
$html = yaymail_get_content( 'templates/shortcodes/payment-instruction/main.php', $args ); |
| 153 |
return $html; |
| 154 |
} |
| 155 |
|
| 156 |
public function yaymail_payment_transaction_id( $data ) { |
| 157 |
|
| 158 |
$render_data = isset( $data['render_data'] ) ? $data['render_data'] : []; |
| 159 |
|
| 160 |
if ( ! empty( $render_data['is_sample'] ) ) { |
| 161 |
/** |
| 162 |
* Is sample order |
| 163 |
*/ |
| 164 |
return '1'; |
| 165 |
} |
| 166 |
|
| 167 |
$order = Helpers::get_order_from_shortcode_data( $render_data ); |
| 168 |
|
| 169 |
if ( empty( $order ) || empty( $order->get_transaction_id() ) ) { |
| 170 |
/** |
| 171 |
* Not having order_id |
| 172 |
*/ |
| 173 |
return ''; |
| 174 |
} |
| 175 |
|
| 176 |
return $order->get_transaction_id(); |
| 177 |
} |
| 178 |
} |
| 179 |
|