芝麻web文件管理V1.00
编辑当前文件:/var/www/cognitio.in/wp-content/plugins/stripe-payments/admin/includes/class-coupons.php
$coupon_code, 'valid' => true, ); //let's find coupon $coupon = get_posts( array( 'meta_key' => 'asp_coupon_code', 'meta_value' => $coupon_code, 'posts_per_page' => 1, 'offset' => 0, 'post_type' => self::$post_slug, ) ); wp_reset_postdata(); if ( empty( $coupon ) ) { //coupon not found $out['valid'] = false; $out['err_msg'] = __( 'Coupon not found.', 'stripe-payments' ); return $out; } $coupon = $coupon[0]; //check if coupon is active if ( ! get_post_meta( $coupon->ID, 'asp_coupon_active', true ) ) { $out['valid'] = false; $out['err_msg'] = __( 'Coupon is not active.', 'stripe-payments' ); return $out; } //check if coupon start date has come $start_date = get_post_meta( $coupon->ID, 'asp_coupon_start_date', true ); if ( empty( $start_date ) || strtotime( $start_date ) > time() ) { $out['valid'] = false; $out['err_msg'] = __( 'Coupon is not available yet.', 'stripe-payments' ); return $out; } //check if coupon has expired $exp_date = get_post_meta( $coupon->ID, 'asp_coupon_exp_date', true ); if ( ! empty( $exp_date ) && strtotime( $exp_date ) < time() ) { $out['valid'] = false; $out['err_msg'] = __( 'Coupon has expired.', 'stripe-payments' ); return $out; } //check if redemption limit is reached $red_limit = get_post_meta( $coupon->ID, 'asp_coupon_red_limit', true ); $red_count = get_post_meta( $coupon->ID, 'asp_coupon_red_count', true ); if ( ! empty( $red_limit ) && intval( $red_count ) >= intval( $red_limit ) ) { $out['valid'] = false; $out['err_msg'] = __( 'Coupon redemption limit is reached.', 'stripe-payments' ); return $out; } $out['id'] = $coupon->ID; $out['discount'] = get_post_meta( $coupon->ID, 'asp_coupon_discount', true ); $out['discountType'] = get_post_meta( $coupon->ID, 'asp_coupon_discount_type', true ); return $out; } public static function is_coupon_allowed_for_product( $coupon_id, $prod_id ) { //check if coupon is only available for specific products $only_for_allowed_products = get_post_meta( $coupon_id, 'asp_coupon_only_for_allowed_products', true ); if ( $only_for_allowed_products ) { $allowed_products = get_post_meta( $coupon_id, 'asp_coupon_allowed_products', true ); if ( is_array( $allowed_products ) && ! in_array( $prod_id, $allowed_products, true ) ) { return false; } } return true; } public function frontend_check_coupon() { $out = array(); $coupon_code = filter_input( INPUT_POST, 'coupon_code', FILTER_SANITIZE_STRING ); if ( empty( $coupon_code ) ) { $out['success'] = false; $out['msg'] = __( 'Empty coupon code', 'stripe-payments' ); wp_send_json( $out ); } $coupon_code = strtoupper( $coupon_code ); $tax = filter_input( INPUT_POST, 'tax', FILTER_SANITIZE_NUMBER_INT ); $tax = empty( $tax ) ? 0 : absint( $tax ); $shipping = filter_input( INPUT_POST, 'shipping', FILTER_SANITIZE_NUMBER_INT ); $shipping = empty( $shipping ) ? 0 : absint( $shipping ); $coupon = self::get_coupon( $coupon_code ); if ( ! $coupon['valid'] ) { $out['success'] = false; $out['msg'] = $coupon['err_msg']; wp_send_json( $out ); } $prod_id = filter_input( INPUT_POST, 'product_id', FILTER_SANITIZE_NUMBER_INT ); if ( empty( $prod_id ) ) { $out['success'] = false; $out['msg'] = __( 'No product ID specified.', 'stripe-payments' ); wp_send_json( $out ); } if ( ! self::is_coupon_allowed_for_product( $coupon['id'], $prod_id ) ) { $out['success'] = false; $out['msg'] = __( 'Coupon is not allowed for this product.', 'stripe-payments' ); wp_send_json( $out ); } $curr = filter_input( INPUT_POST, 'curr', FILTER_SANITIZE_STRING ); $curr = isset( $curr ) ? $curr : ''; $discount = $coupon['discount']; $discount_type = $coupon['discountType']; $amount = filter_input( INPUT_POST, 'amount', FILTER_SANITIZE_NUMBER_INT ); $perc = AcceptStripePayments::is_zero_cents( $curr ) ? 0 : 2; if ( $coupon['discountType'] === 'perc' ) { $discount_amount = round( $amount * ( $coupon['discount'] / 100 ), 0 ); } else { $discount_amount = $coupon['discount'] * ( $perc === 0 ? 1 : 100 ); } $out['discountAmount'] = $discount_amount; $amount = round( ( $amount - $discount_amount ) / ( $perc === 0 ? 1 : 100 ), $perc ); $amount = AcceptStripePayments::apply_tax( $amount, $tax, AcceptStripePayments::is_zero_cents( $curr ) ); $amount = round( $amount + $shipping / 100, 2 ); $out['tax'] = $tax; $out['shipping'] = $shipping; $out['amount'] = $amount; $out['success'] = true; $out['code'] = $coupon_code; $out['discount'] = $discount; $out['discountType'] = $discount_type; $out['discountStr'] = $coupon_code . ': - ' . ( $discount_type === 'perc' ? $discount . '%' : AcceptStripePayments::formatted_price( $discount, $curr ) ); $out['newAmountFmt'] = AcceptStripePayments::formatted_price( $amount, $curr ); wp_send_json( $out ); } public function init_handler() { $args = array( 'supports' => array( '' ), 'hierarchical' => false, 'public' => false, 'show_ui' => false, 'can_export' => false, 'has_archive' => false, 'exclude_from_search' => true, 'publicly_queryable' => false, 'capability_type' => 'post', ); register_post_type( self::$post_slug, $args ); if ( ! is_admin() ) { return; } $post_action = filter_input( INPUT_POST, 'asp_coupon_action', FILTER_SANITIZE_STRING ); $post_action = empty( $post_action ) ? '0' : $post_action; switch ( $post_action ) { case 'save_coupon': $this->save_coupon(); break; case 'save_settings': $this->save_settings(); break; default: break; } $action = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_STRING ); $action = empty( $action ) ? '' : $action; if ( $action === 'asp_delete_coupon' ) { $this->delete_coupon(); } } public function add_menu() { //Trigger filter hook to allow overriding of the add-ons menu capability. $asp_coupons_menu_capability = apply_filters( 'asp_coupons_menu_capability', ASP_MANAGEMENT_PERMISSION ); add_submenu_page( 'edit.php?post_type=' . ASPMain::$products_slug, __( 'Coupons', 'stripe-payments' ), __( 'Coupons', 'stripe-payments' ), $asp_coupons_menu_capability, 'stripe-payments-coupons', array( $this, 'display_coupons_menu_page' ) ); } public function save_settings() { check_admin_referer( 'asp-coupons-settings' ); $settings = get_option( 'AcceptStripePayments-settings' ); $opts = filter_input( INPUT_POST, 'asp_coupons_opts', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY ); $settings['coupons_enabled'] = isset( $opts['coupons_enabled'] ) ? 1 : 0; update_option( 'AcceptStripePayments-settings', $settings ); AcceptStripePayments_Admin::add_admin_notice( 'success', __( 'Settings updated.', 'stripe-payments' ), false ); } public function display_coupons_menu_page() { $action = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_STRING ); if ( ! empty( $action ) ) { if ( $action === 'asp_add_edit_coupon' ) { //coupon add or edit content $this->display_coupon_add_edit_page(); return; } } $asp_main = AcceptStripePayments::get_instance(); $coupons_enabled = $asp_main->get_setting( 'coupons_enabled' ); if ( ! class_exists( 'WP_List_Table' ) ) { require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; } require_once WP_ASP_PLUGIN_PATH . 'admin/includes/class-coupons-list-table.php'; $coupons_tbl = new ASP_Coupons_Table(); $coupons_tbl->prepare_items(); ?>
>
display(); ?>
$coupon_id, 'code' => get_post_meta( $coupon_id, 'asp_coupon_code', true ), 'active' => get_post_meta( $coupon_id, 'asp_coupon_active', true ), 'discount' => get_post_meta( $coupon_id, 'asp_coupon_discount', true ), 'discount_type' => get_post_meta( $coupon_id, 'asp_coupon_discount_type', true ), 'red_limit' => get_post_meta( $coupon_id, 'asp_coupon_red_limit', true ), 'red_count' => get_post_meta( $coupon_id, 'asp_coupon_red_count', true ), 'start_date' => get_post_meta( $coupon_id, 'asp_coupon_start_date', true ), 'exp_date' => get_post_meta( $coupon_id, 'asp_coupon_exp_date', true ), 'only_for_allowed_products' => get_post_meta( $coupon_id, 'asp_coupon_only_for_allowed_products', true ), 'allowed_products' => get_post_meta( $coupon_id, 'asp_coupon_allowed_products', true ), 'per_order' => get_post_meta( $coupon_id, 'asp_coupon_per_order', true ), ); } //generate array with all products $posts = get_posts( array( 'post_type' => untrailingslashit( ASPMain::$products_slug ), 'post_status' => 'publish', 'numberposts' => -1, ) ); $prod_inputs = ''; $input_tpl = '
%s
'; if ( $posts ) { foreach ( $posts as $the_post ) { $checked = ''; if ( ! empty( $coupon ) && is_array( $coupon['allowed_products'] ) ) { if ( in_array( strval( $the_post->ID ), $coupon['allowed_products'], true ) ) { $checked = ' checked'; } } $prod_inputs .= sprintf( $input_tpl, $the_post->ID, $checked, $the_post->post_title ); $prod_inputs .= '
'; } } else { $prod_inputs = __( 'No products created yet.', 'stripe-payments' ); } wp_reset_postdata(); ?>
>
value="">
>
>
>
>
>
>
>
post_type !== self::$post_slug ) { AcceptStripePayments_Admin::add_admin_notice( 'error', // translators: %d is coupon ID sprintf( __( 'Can\'t delete coupon: post #%d is not a coupon.', 'stripe-payments' ), $coupon_id ), false ); return false; } check_admin_referer( 'delete-coupon_' . $coupon_id ); wp_delete_post( $coupon_id, true ); AcceptStripePayments_Admin::add_admin_notice( 'success', // translators: %d is coupon ID sprintf( __( 'Coupon #%d has been deleted.', 'stripe-payments' ), $coupon_id ), false ); wp_safe_redirect( remove_query_arg( array( 'action', 'asp_coupon_id', '_wpnonce' ) ) ); exit(); } public function save_coupon() { check_admin_referer( 'asp-add-edit-coupon' ); $coupon = filter_input( INPUT_POST, 'asp_coupon', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY ); $coupon_id = isset( $_POST['asp_coupon_id'] ) ? absint( $_POST['asp_coupon_id'] ) : false; $is_edit = $coupon_id ? true : false; $err_msg = array(); $coupon['active'] = isset( $coupon['active'] ) ? 1 : 0; $coupon['per_order'] = isset( $coupon['per_order'] ) ? 1 : 0; $coupon['code'] = ! empty( $coupon['code'] ) ? sanitize_text_field( $coupon['code'] ) : ''; if ( empty( $coupon['code'] ) ) { $err_msg[] = __( 'Please enter coupon code.', 'stripe-payments' ); } if ( empty( $coupon['discount'] ) ) { $err_msg[] = __( 'Please enter discount.', 'stripe-payments' ); } if ( $coupon['discount_type'] === 'perc' && $coupon['discount'] > 100 ) { $err_msg[] = __( "Discount can't be more than 100%.", 'stripe-payments' ); } if ( ! empty( $err_msg ) ) { foreach ( $err_msg as $msg ) { AcceptStripePayments_Admin::add_admin_notice( 'error', $msg, false ); } return false; } if ( ! $is_edit ) { $post = array(); $post['post_title'] = ''; $post['post_status'] = 'publish'; $post['content'] = ''; $post['post_type'] = self::$post_slug; $coupon_id = wp_insert_post( $post ); } if ( empty( $coupon['allowed_products'] ) ) { $coupon['allowed_products'] = array(); } foreach ( $coupon as $key => $value ) { update_post_meta( $coupon_id, 'asp_coupon_' . $key, $value ); } do_action( 'asp_admin_save_coupon', $coupon_id, $coupon ); AcceptStripePayments_Admin::add_admin_notice( 'success', // translators: %s is coupon code sprintf( $is_edit ? __( 'Coupon "%s" has been updated.', 'stripe-payments' ) : __( 'Coupon "%s" has been created.', 'stripe-payments' ), $coupon['code'] ), false ); wp_safe_redirect( 'edit.php?post_type=' . ASPMain::$products_slug . '&page=stripe-payments-coupons' ); exit; } } new AcceptStripePayments_CouponsAdmin();