| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Admin; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 6 |
|
| 7 |
// HPOS: This file does not access WooCommerce order data directly. |
| 8 |
// All meta access is for product or subscription data only, not WooCommerce order data. |
| 9 |
// If you add new order data access, use WooCommerce CRUD for HPOS compatibility. |
| 10 |
|
| 11 |
/** |
| 12 |
* Product class |
| 13 |
* |
| 14 |
* @package SpringDevs\Subscription\Admin |
| 15 |
*/ |
| 16 |
class Product { |
| 17 |
|
| 18 |
/** |
| 19 |
* Initialize the class |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 23 |
add_filter( 'woocommerce_product_data_tabs', array( $this, 'register_tab' ) ); |
| 24 |
add_action( 'woocommerce_product_data_panels', array( $this, 'subscription_forms' ) ); |
| 25 |
add_filter( 'product_type_options', array( $this, 'add_product_type_options' ) ); |
| 26 |
add_action( 'save_post_product', array( $this, 'save_subscrpt_data' ) ); |
| 27 |
add_filter( 'woocommerce_get_price_html', array( $this, 'change_price_html' ), 10, 2 ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add trial, signup fee etc. with product price. |
| 32 |
* |
| 33 |
* @param string $price Price. |
| 34 |
* @param \WC_Product $product Product. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function change_price_html( $price, $product ) { |
| 39 |
if ( $product->is_type( 'variable' ) || '' === $price || subscrpt_pro_activated() ) { |
| 40 |
return $price; |
| 41 |
} |
| 42 |
|
| 43 |
$enabled = $product->get_meta( '_subscrpt_enabled' ); |
| 44 |
if ( $enabled ) : |
| 45 |
$type = Helper::get_typos( 1, $product->get_meta( '_subscrpt_timing_option' ), true ); |
| 46 |
$meta_trial_time = $product->get_meta( '_subscrpt_trial_timing_per' ); |
| 47 |
$trial = null; |
| 48 |
if ( ! empty( $meta_trial_time ) && $meta_trial_time > 0 ) { |
| 49 |
$trial_type = Helper::get_typos( $meta_trial_time, $product->get_meta( '_subscrpt_trial_timing_option' ), true ); |
| 50 |
$trial = '<br/> + Get ' . $meta_trial_time . ' ' . ucfirst( $trial_type ) . ' free trial!'; |
| 51 |
} |
| 52 |
|
| 53 |
$price_html = $price . ' / ' . ucfirst( $type ) . $trial; |
| 54 |
return $price_html; |
| 55 |
else : |
| 56 |
return $price; |
| 57 |
endif; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Enqueue Assets. |
| 62 |
*/ |
| 63 |
public function enqueue_assets() { |
| 64 |
wp_enqueue_script( 'sdevs_subscription_admin' ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Register "Subscription" option tab. |
| 69 |
* |
| 70 |
* @param array $tabs Tabs. |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function register_tab( $tabs ) { |
| 75 |
$tabs['sdevs_subscription'] = array( |
| 76 |
'label' => __( 'Subscription', 'subscription' ), |
| 77 |
'class' => array( 'show_if_simple', 'show_if_subscription' ), |
| 78 |
'target' => 'sdevs_subscription_options', |
| 79 |
'priority' => 11, |
| 80 |
); |
| 81 |
return $tabs; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Display Enable Subscription Checkbox on product data tab. |
| 86 |
* |
| 87 |
* @param array $product_type_options Product Type options on Data tab. |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public function add_product_type_options( $product_type_options ) { |
| 92 |
$screen = get_current_screen(); |
| 93 |
$value = 'no'; |
| 94 |
if ( 'edit' === $screen->parent_base ) { |
| 95 |
$product = wc_get_product( get_the_ID() ); |
| 96 |
if ( $product ) { |
| 97 |
$value = $product->get_meta( '_subscrpt_enabled' ) ? 'yes' : 'no'; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
$wrapper_class = apply_filters( 'subscrpt_simple_enable_checkbox_classes', 'show_if_simple' ); |
| 102 |
$product_type_options['subscrpt_enable'] = array( |
| 103 |
'id' => 'subscrpt_enable', |
| 104 |
'wrapper_class' => $wrapper_class, |
| 105 |
'label' => __( 'Subscription', 'subscription' ), |
| 106 |
'description' => __( 'Enable Subscriptions', 'subscription' ), |
| 107 |
'default' => $value, |
| 108 |
); |
| 109 |
|
| 110 |
return $product_type_options; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Display forms on product create/edit. |
| 115 |
*/ |
| 116 |
public function subscription_forms() { |
| 117 |
if ( function_exists( 'subscrpt_pro_activated' ) ) { |
| 118 |
if ( subscrpt_pro_activated() ) { |
| 119 |
do_action( 'subscrpt_simple_pro_fields', get_the_ID() ); |
| 120 |
} else { |
| 121 |
$timing_types = array( |
| 122 |
'days' => __( 'Daily', 'subscription' ), |
| 123 |
'weeks' => __( 'Weekly', 'subscription' ), |
| 124 |
'months' => __( 'Monthly', 'subscription' ), |
| 125 |
'years' => __( 'Yearly', 'subscription' ), |
| 126 |
); |
| 127 |
$trial_timing_types = wps_subscription_get_timing_types(); |
| 128 |
$subscrpt_timing = null; |
| 129 |
$subscrpt_trial_time = null; |
| 130 |
$subscrpt_trial_timing = null; |
| 131 |
$subscrpt_cart_txt = 'subscribe'; |
| 132 |
$subscrpt_user_cancell = 'yes'; |
| 133 |
$subscrpt_limit = 'one'; |
| 134 |
|
| 135 |
$screen = get_current_screen(); |
| 136 |
if ( 'edit' === $screen->parent_base ) { |
| 137 |
$product = wc_get_product( get_the_ID() ); |
| 138 |
if ( $product ) { |
| 139 |
$subscrpt_timing = $product->get_meta( '_subscrpt_timing_option' ); |
| 140 |
$subscrpt_trial_time = $product->get_meta( '_subscrpt_trial_timing_per' ); |
| 141 |
$subscrpt_trial_timing = $product->get_meta( '_subscrpt_trial_timing_option' ); |
| 142 |
$subscrpt_cart_txt = $product->get_meta( '_subscrpt_cart_btn_label' ); |
| 143 |
$subscrpt_user_cancell = $product->get_meta( '_subscrpt_user_cancel' ); |
| 144 |
$subscrpt_limit = $product->get_meta( '_subscrpt_limit' ); |
| 145 |
} |
| 146 |
} |
| 147 |
include 'views/product-form.php'; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Save subscription settings. |
| 154 |
* |
| 155 |
* @param int $product_id Product Id. |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function save_subscrpt_data( $product_id ) { |
| 160 |
if ( function_exists( 'subscrpt_pro_activated' ) ) { |
| 161 |
if ( subscrpt_pro_activated() ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if ( ! isset( $_POST['_subscript_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_subscript_nonce'] ) ), '_subscript_edit_product_nonce' ) ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
remove_action( 'save_post_product', array( $this, 'save_subscrpt_data' ) ); |
| 171 |
|
| 172 |
$subscrpt_enable = isset( $_POST['subscrpt_enable'] ); |
| 173 |
$subscrpt_timing = isset( $_POST['subscrpt_timing'] ) ? sanitize_text_field( wp_unslash( $_POST['subscrpt_timing'] ) ) : ''; |
| 174 |
$subscrpt_trial_time = isset( $_POST['subscrpt_trial_time'] ) ? sanitize_text_field( wp_unslash( $_POST['subscrpt_trial_time'] ) ) : ''; |
| 175 |
$subscrpt_trial_timing = isset( $_POST['subscrpt_trial_timing'] ) ? sanitize_text_field( wp_unslash( $_POST['subscrpt_trial_timing'] ) ) : ''; |
| 176 |
$subscrpt_cart_txt = isset( $_POST['subscrpt_cart_txt'] ) ? sanitize_text_field( wp_unslash( $_POST['subscrpt_cart_txt'] ) ) : ''; |
| 177 |
$subscrpt_user_cancel = isset( $_POST['subscrpt_user_cancel'] ) ? sanitize_text_field( wp_unslash( $_POST['subscrpt_user_cancel'] ) ) : ''; |
| 178 |
$subscrpt_limit = isset( $_POST['subscrpt_limit'] ) ? sanitize_text_field( wp_unslash( $_POST['subscrpt_limit'] ) ) : null; |
| 179 |
|
| 180 |
$product = wc_get_product( $product_id ); |
| 181 |
$product->update_meta_data( '_subscrpt_enabled', $subscrpt_enable ); |
| 182 |
$product->update_meta_data( '_subscrpt_timing_option', $subscrpt_timing ); |
| 183 |
$product->update_meta_data( '_subscrpt_trial_timing_per', $subscrpt_trial_time ); |
| 184 |
$product->update_meta_data( '_subscrpt_trial_timing_option', $subscrpt_trial_timing ); |
| 185 |
$product->update_meta_data( '_subscrpt_cart_btn_label', $subscrpt_cart_txt ); |
| 186 |
$product->update_meta_data( '_subscrpt_user_cancel', $subscrpt_user_cancel ); |
| 187 |
$product->update_meta_data( '_subscrpt_limit', $subscrpt_limit ); |
| 188 |
$product->save(); |
| 189 |
|
| 190 |
add_action( 'save_post_product', array( $this, 'save_subscrpt_data' ) ); |
| 191 |
} |
| 192 |
} |
| 193 |
|