| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Admin; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Illuminate\Action; |
| 6 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Subscriptions class |
| 10 |
* |
| 11 |
* @package SpringDevs\Subscription\Admin |
| 12 |
*/ |
| 13 |
class Subscriptions { |
| 14 |
|
| 15 |
/** |
| 16 |
* Initialize the class. |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 20 |
add_filter( 'post_row_actions', array( $this, 'post_row_actions' ) ); |
| 21 |
add_filter( 'manage_subscrpt_order_posts_columns', array( $this, 'add_custom_columns' ) ); |
| 22 |
add_action( 'manage_subscrpt_order_posts_custom_column', array( $this, 'add_custom_columns_data' ), 10, 2 ); |
| 23 |
add_action( 'add_meta_boxes', array( $this, 'create_meta_boxes' ) ); |
| 24 |
add_action( 'admin_head-post.php', array( $this, 'some_styles' ) ); |
| 25 |
add_action( 'admin_head-post-new.php', array( $this, 'some_styles' ) ); |
| 26 |
add_action( 'admin_footer-post.php', array( $this, 'some_scripts' ) ); |
| 27 |
add_action( 'admin_footer-post-new.php', array( $this, 'some_scripts' ) ); |
| 28 |
add_action( 'save_post', array( $this, 'save_subscrpt_order' ) ); |
| 29 |
add_filter( 'woocommerce_order_item_get_formatted_meta_data', array( $this, 'remove_order_meta' ), 10, 1 ); |
| 30 |
add_filter( 'bulk_actions-edit-subscrpt_order', array( $this, 'remove_bulk_actions' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Remove 'Edit` and 'Trash' from bulk actions. |
| 35 |
* |
| 36 |
* @param array $actions Action list. |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
public function remove_bulk_actions( $actions ) { |
| 41 |
unset( $actions['edit'] ); |
| 42 |
unset( $actions['trash'] ); |
| 43 |
return $actions; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Hide order meta key from custom fields. |
| 48 |
* |
| 49 |
* @param array $formatted_meta Data with key-value. |
| 50 |
* |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
public function remove_order_meta( $formatted_meta ): array { |
| 54 |
$temp_metas = array(); |
| 55 |
foreach ( $formatted_meta as $key => $meta ) { |
| 56 |
if ( isset( $meta->key ) && '_renew_subscrpt' !== $meta->key ) { |
| 57 |
$temp_metas[ $key ] = $meta; |
| 58 |
} |
| 59 |
} |
| 60 |
return $temp_metas; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Enqueue admin assets. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function enqueue_scripts() { |
| 69 |
wp_enqueue_style( 'subscrpt_admin_css' ); |
| 70 |
wp_enqueue_style( 'subscrpt_status_css' ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Remove default post actions. |
| 75 |
* |
| 76 |
* @param array $actions Actions. |
| 77 |
* |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
public function post_row_actions( $actions ) { |
| 81 |
global $current_screen; |
| 82 |
if ( 'subscrpt_order' !== $current_screen->post_type ) { |
| 83 |
return $actions; |
| 84 |
} |
| 85 |
unset( $actions['inline hide-if-no-js'] ); |
| 86 |
unset( $actions['view'] ); |
| 87 |
unset( $actions['trash'] ); |
| 88 |
unset( $actions['edit'] ); |
| 89 |
return $actions; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Register custom columns. |
| 94 |
* |
| 95 |
* @param array $columns Columns. |
| 96 |
* |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
public function add_custom_columns( $columns ) { |
| 100 |
$columns['subscrpt_start_date'] = __( 'Start Date', 'sdevs_subscrpt' ); |
| 101 |
$columns['subscrpt_customer'] = __( 'Customer', 'sdevs_subscrpt' ); |
| 102 |
$columns['subscrpt_next_date'] = __( 'Next Date', 'sdevs_subscrpt' ); |
| 103 |
$columns['subscrpt_status'] = __( 'Status', 'sdevs_subscrpt' ); |
| 104 |
unset( $columns['date'] ); |
| 105 |
unset( $columns['cb'] ); |
| 106 |
return $columns; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Display column data. |
| 111 |
* |
| 112 |
* @param string $column Column. |
| 113 |
* @param int $post_id Post Id. |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function add_custom_columns_data( $column, $post_id ) { |
| 118 |
$order_id = get_post_meta( $post_id, '_subscrpt_order_id', true ); |
| 119 |
$order = wc_get_order( $order_id ); |
| 120 |
if ( $order ) { |
| 121 |
if ( 'subscrpt_start_date' === $column ) { |
| 122 |
$start_date = get_post_meta( $post_id, '_subscrpt_start_date', true ); |
| 123 |
echo ! empty( $start_date ) ? esc_html( gmdate( 'F d, Y', $start_date ) ) : '-'; |
| 124 |
} elseif ( 'subscrpt_customer' === $column ) { |
| 125 |
?> |
| 126 |
<?php echo wp_kses_post( $order->get_formatted_billing_full_name() ); ?> |
| 127 |
<br /> |
| 128 |
<a href="mailto:<?php echo wp_kses_post( $order->get_billing_email() ); ?>"><?php echo wp_kses_post( $order->get_billing_email() ); ?></a> |
| 129 |
<br /> |
| 130 |
<?php if ( ! empty( $order->get_billing_phone() ) ) : ?> |
| 131 |
Phone : <a href="tel:<?php echo esc_js( $order->get_billing_phone() ); ?>"><?php echo esc_js( $order->get_billing_phone() ); ?></a> |
| 132 |
<?php endif; ?> |
| 133 |
<?php |
| 134 |
} elseif ( 'subscrpt_next_date' === $column ) { |
| 135 |
$next_date = get_post_meta( $post_id, '_subscrpt_next_date', true ); |
| 136 |
echo ! empty( $next_date ) ? esc_html( gmdate( 'F d, Y', $next_date ) ) : '-'; |
| 137 |
} elseif ( 'subscrpt_status' === $column ) { |
| 138 |
$status_obj = get_post_status_object( get_post_status( $post_id ) ); |
| 139 |
?> |
| 140 |
<span class="subscrpt-<?php echo esc_html( $status_obj->name ); ?>"><?php echo esc_html( $status_obj->label ); ?></span> |
| 141 |
<?php |
| 142 |
} |
| 143 |
} else { |
| 144 |
esc_html_e( 'Order not found !!', 'sdevs_subscrpt' ); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Create metaboxes for admin subscriptions. |
| 150 |
*/ |
| 151 |
public function create_meta_boxes() { |
| 152 |
remove_meta_box( 'submitdiv', 'subscrpt_order', 'side' ); |
| 153 |
add_meta_box( |
| 154 |
'subscrpt_order_save_post', |
| 155 |
__( 'Subscription Action', 'sdevs_subscrpt' ), |
| 156 |
array( $this, 'subscrpt_order_save_post' ), |
| 157 |
'subscrpt_order', |
| 158 |
'side', |
| 159 |
'default' |
| 160 |
); |
| 161 |
|
| 162 |
add_meta_box( |
| 163 |
'subscrpt_customer_info', |
| 164 |
__( 'Customer Info', 'sdevs_subscrpt' ), |
| 165 |
array( $this, 'customer_info' ), |
| 166 |
'subscrpt_order', |
| 167 |
'side', |
| 168 |
'default' |
| 169 |
); |
| 170 |
|
| 171 |
add_meta_box( |
| 172 |
'subscrpt_order_info', |
| 173 |
__( 'Subscription Info', 'sdevs_subscrpt' ), |
| 174 |
array( $this, 'subscrpt_order_info' ), |
| 175 |
'subscrpt_order', |
| 176 |
'normal', |
| 177 |
'default' |
| 178 |
); |
| 179 |
|
| 180 |
add_meta_box( |
| 181 |
'subscrpt_order_history', |
| 182 |
__( 'Subscription History', 'sdevs_subscrpt' ), |
| 183 |
array( $this, 'order_histories' ), |
| 184 |
'subscrpt_order', |
| 185 |
'normal', |
| 186 |
'default' |
| 187 |
); |
| 188 |
|
| 189 |
add_meta_box( |
| 190 |
'subscrpt_order_activities', |
| 191 |
__( 'Subscription Activities', 'sdevs_subscrpt' ), |
| 192 |
array( $this, 'order_activities' ), |
| 193 |
'subscrpt_order', |
| 194 |
'normal', |
| 195 |
'default' |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Display Order Histories. |
| 201 |
* |
| 202 |
* @param \WP_Post $post Post Object. |
| 203 |
* |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
public function order_histories( $post ) { |
| 207 |
$subscription_id = $post->ID; |
| 208 |
global $wpdb; |
| 209 |
$table_name = $wpdb->prefix . 'subscrpt_order_relation'; |
| 210 |
// @phpcs:ignore |
| 211 |
$order_histories = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE subscription_id=%d', array( $table_name, $subscription_id ) ) ); |
| 212 |
|
| 213 |
include 'views/order-history.php'; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Display Order Activities |
| 218 |
* |
| 219 |
* @param \WP_Post $post Post Object. |
| 220 |
* |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
public function order_activities( $post ) { |
| 224 |
if ( function_exists( 'subscrpt_pro_activated' ) ) : |
| 225 |
if ( subscrpt_pro_activated() ) : |
| 226 |
do_action( 'subscrpt_order_activities', $post->ID ); |
| 227 |
else : |
| 228 |
?> |
| 229 |
<a href="https://springdevs.com/subscription" target="_blank"> |
| 230 |
<img style="width: 100%;" src="<?php echo esc_html( SUBSCRPT_ASSETS . '/images/subscrpt-ads.png' ); ?>" /> |
| 231 |
</a> |
| 232 |
<?php |
| 233 |
endif; |
| 234 |
endif; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Save subscription HTML. |
| 239 |
*/ |
| 240 |
public function subscrpt_order_save_post() { |
| 241 |
$actions = array( |
| 242 |
array( |
| 243 |
'label' => __( 'Activate Subscription', 'sdevs_subscrpt' ), |
| 244 |
'value' => 'active', |
| 245 |
), |
| 246 |
array( |
| 247 |
'label' => __( 'Pending Subscription', 'sdevs_subscrpt' ), |
| 248 |
'value' => 'pending', |
| 249 |
), |
| 250 |
array( |
| 251 |
'label' => __( 'Expire Subscription', 'sdevs_subscrpt' ), |
| 252 |
'value' => 'expired', |
| 253 |
), |
| 254 |
array( |
| 255 |
'label' => __( 'Pending Cancel Subscription', 'sdevs_subscrpt' ), |
| 256 |
'value' => 'pe_cancelled', |
| 257 |
), |
| 258 |
array( |
| 259 |
'label' => __( 'Cancel Subscription', 'sdevs_subscrpt' ), |
| 260 |
'value' => 'cancelled', |
| 261 |
), |
| 262 |
); |
| 263 |
$status = get_post_status( get_the_ID() ); |
| 264 |
include 'views/subscription-save-meta.php'; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Display Customer Info |
| 269 |
* |
| 270 |
* @param \WP_Post $post Post Object. |
| 271 |
* |
| 272 |
* @return void |
| 273 |
*/ |
| 274 |
public function customer_info( $post ) { |
| 275 |
$order_id = get_post_meta( $post->ID, '_subscrpt_order_id', true ); |
| 276 |
$order = wc_get_order( $order_id ); |
| 277 |
if ( ! $order ) { |
| 278 |
return; |
| 279 |
} |
| 280 |
include 'views/subscription-customer.php'; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Display subscription info. |
| 285 |
* |
| 286 |
* @return void |
| 287 |
*/ |
| 288 |
public function subscrpt_order_info() { |
| 289 |
$order_id = get_post_meta( get_the_ID(), '_subscrpt_order_id', true ); |
| 290 |
$order_item_id = get_post_meta( get_the_ID(), '_subscrpt_order_item_id', true ); |
| 291 |
$trial = get_post_meta( get_the_ID(), '_subscrpt_trial', true ); |
| 292 |
$start_date = get_post_meta( get_the_ID(), '_subscrpt_start_date', true ); |
| 293 |
$next_date = get_post_meta( get_the_ID(), '_subscrpt_next_date', true ); |
| 294 |
$trial_start_date = get_post_meta( get_the_ID(), '_subscrpt_trial_started', true ); |
| 295 |
$trial_end_date = get_post_meta( get_the_ID(), '_subscrpt_trial_ended', true ); |
| 296 |
$trial_mode = get_post_meta( get_the_ID(), '_subscrpt_trial_mode', true ); |
| 297 |
$order = wc_get_order( $order_id ); |
| 298 |
if ( ! $order ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
$order_item = $order->get_item( $order_item_id ); |
| 302 |
|
| 303 |
$product_name = $order_item->get_name(); |
| 304 |
$product_link = get_the_permalink( $order_item->get_product_id() ); |
| 305 |
$rows = array( |
| 306 |
'product' => array( |
| 307 |
'label' => __( 'Product', 'sdevs_subscrpt' ), |
| 308 |
'value' => '<a href="' . esc_html( $product_link ) . '" target="_blank">' . esc_html( $product_name ) . '</a>', |
| 309 |
), |
| 310 |
'cost' => array( |
| 311 |
'label' => __( 'Cost', 'sdevs_subscrpt' ), |
| 312 |
'value' => Helper::format_price_with_order_item( get_post_meta( get_the_ID(), '_subscrpt_price', true ), $order_item->get_id() ), |
| 313 |
), |
| 314 |
'quantity' => array( |
| 315 |
'label' => __( 'Qty', 'sdevs_subscrpt' ), |
| 316 |
'value' => "x{$order_item->get_quantity()}", |
| 317 |
), |
| 318 |
'start_date' => array( |
| 319 |
'label' => __( 'Started date', 'sdevs_subscrpt' ), |
| 320 |
'value' => ! empty( $start_date ) ? gmdate( 'F d, Y', $trial && $trial_start_date ? $trial_start_date : $start_date ) : '-', |
| 321 |
), |
| 322 |
'next_date' => array( |
| 323 |
'label' => __( 'Payment due date', 'sdevs_subscrpt' ), |
| 324 |
'value' => ! empty( $next_date ) ? gmdate( 'F d, Y', $trial && $trial_end_date && 'on' === $trial_mode ? $trial_end_date : ( $next_date ?? '-' ) ) : '-', |
| 325 |
), |
| 326 |
'status' => array( |
| 327 |
'label' => __( 'Status', 'sdevs_subscrpt' ), |
| 328 |
'value' => '<span class="subscrpt-' . get_post_status() . '">' . get_post_status_object( get_post_status() )->label . '</span>', |
| 329 |
), |
| 330 |
'payment_method' => array( |
| 331 |
'label' => __( 'Payment Method', 'sdevs_subscrpt' ), |
| 332 |
'value' => empty( $order->get_payment_method_title() ) ? '-' : $order->get_payment_method_title(), |
| 333 |
), |
| 334 |
'billing_address' => array( |
| 335 |
'label' => __( 'Billing', 'sdevs_subscrpt' ), |
| 336 |
'value' => $order->get_formatted_billing_address() ? $order->get_formatted_billing_address() : __( 'No billing address set.', 'sdevs_subscrpt' ), |
| 337 |
), |
| 338 |
'shipping_address' => array( |
| 339 |
'label' => __( 'Shipping', 'sdevs_subscrpt' ), |
| 340 |
'value' => $order->get_formatted_shipping_address() ? $order->get_formatted_shipping_address() : __( 'No shipping address set.', 'sdevs_subscrpt' ), |
| 341 |
), |
| 342 |
); |
| 343 |
if ( $trial ) { |
| 344 |
$rows = array_slice( $rows, 0, 3, true ) + array( |
| 345 |
'trial' => array( |
| 346 |
'label' => __( 'Trial', 'sdevs_subscrpt' ), |
| 347 |
'value' => $trial, |
| 348 |
), |
| 349 |
'trial_period' => array( |
| 350 |
'label' => __( 'Trial Period', 'sdevs_subscrpt' ), |
| 351 |
'value' => ( $trial_start_date && $trial_end_date ? ' [ ' . gmdate( 'F d, Y', $trial_start_date ) . ' - ' . gmdate( 'F d, Y', $trial_end_date ) . ' ] ' : __( 'Trial isn\'t activated yet! ', 'sdevs_subscrpt' ) ), |
| 352 |
), |
| 353 |
) + array_slice( $rows, 3, count( $rows ) - 1, true ); |
| 354 |
} |
| 355 |
|
| 356 |
if ( class_exists( 'WC_Stripe' ) && 'stripe' === $order->get_payment_method() ) { |
| 357 |
$is_auto_renew = get_post_meta( get_the_ID(), '_subscrpt_auto_renew', true ); |
| 358 |
$new_rows = array(); |
| 359 |
foreach ( $rows as $key => $value ) { |
| 360 |
$new_rows[ $key ] = $value; |
| 361 |
if ( 'payment_method' === $key ) { |
| 362 |
$new_rows['stripe_auto_renewal'] = array( |
| 363 |
'label' => __( 'Stripe Auto Renewal', 'sdevs_subscrpt' ), |
| 364 |
'value' => '0' !== $is_auto_renew ? 'On' : 'Off', |
| 365 |
); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
$rows = $new_rows; |
| 370 |
} |
| 371 |
|
| 372 |
$rows = apply_filters( 'subscrpt_admin_info_rows', $rows, get_the_ID(), $order ); |
| 373 |
|
| 374 |
include 'views/subscription-info.php'; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Include some styles. |
| 379 |
* |
| 380 |
* @return void |
| 381 |
*/ |
| 382 |
public function some_styles() { |
| 383 |
global $post; |
| 384 |
if ( 'subscrpt_order' === $post->post_type ) : |
| 385 |
?> |
| 386 |
<style> |
| 387 |
.submitbox { |
| 388 |
display: flex; |
| 389 |
justify-content: space-around; |
| 390 |
} |
| 391 |
|
| 392 |
.subscrpt_sub_box { |
| 393 |
display: grid; |
| 394 |
line-height: 2; |
| 395 |
} |
| 396 |
</style> |
| 397 |
<?php |
| 398 |
endif; |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Disable changes popup. |
| 403 |
* |
| 404 |
* @return void |
| 405 |
*/ |
| 406 |
public function some_scripts() { |
| 407 |
global $post; |
| 408 |
if ( 'subscrpt_order' === $post->post_type ) : |
| 409 |
?> |
| 410 |
<script> |
| 411 |
jQuery(document).ready(function() { |
| 412 |
jQuery(window).off("beforeunload", null); |
| 413 |
}); |
| 414 |
</script> |
| 415 |
<?php |
| 416 |
endif; |
| 417 |
} |
| 418 |
|
| 419 |
public function save_subscrpt_order( $post_id ) { |
| 420 |
if ( wp_is_post_revision( $post_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! isset( $_POST['subscrpt_order_action'] ) ) { |
| 421 |
return; |
| 422 |
} |
| 423 |
remove_all_actions( 'save_post' ); |
| 424 |
|
| 425 |
$action = sanitize_text_field( $_POST['subscrpt_order_action'] ); |
| 426 |
wp_update_post( |
| 427 |
array( |
| 428 |
'ID' => $post_id, |
| 429 |
'post_status' => $action, |
| 430 |
) |
| 431 |
); |
| 432 |
|
| 433 |
$order_id = get_post_meta( $post_id, '_subscrpt_order_id', true ); |
| 434 |
if ( 'active' === $action ) { |
| 435 |
$order = wc_get_order( $order_id ); |
| 436 |
$order->update_status( 'completed' ); |
| 437 |
Action::status( $action, $post_id, false ); |
| 438 |
} else { |
| 439 |
Action::status( $action, $post_id ); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|