1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
<?php
class DFW_Plans_Admin_Settings {
public function __construct() {
add_action( 'admin_menu', array($this, 'dfw_add_plans_menu_item' ), 10 );
add_action( 'admin_enqueue_scripts', array( &$this, 'dfw_add_plans_scripts' ) );
}
public function dfw_add_plans_scripts(){
wp_enqueue_style( 'dfw-deposits-plans-admin', DFW_PLUGIN_URL . 'assets/css/payment-plans.min.css' );
wp_register_script( 'dfw-deposits-plans-script', DFW_PLUGIN_URL . 'assets/js/payment-plans.js');
}
public function dfw_add_plans_menu_item() {
$page = add_submenu_page( 'edit.php?post_type=product', __( 'Payment Plans', 'deposits-for-woocommerce' ), __( 'Payment Plans', 'deposits-for-woocommerce' ), 'manage_woocommerce', 'dfw-payment-plans', array( &$this, 'add_payment_plans' ) );
}
public function add_payment_plans() {
global $wpdb;
wp_enqueue_script( 'wc-deposits-plans-script', DFW_PLUGIN_URL . 'assets/js/payment-plans.js');
$wpdb->dfw_deposits_payment_plans = $wpdb->prefix . 'dfw_deposits_payment_plans';
$wpdb->dfw_deposits_payment_plans_schedule = $wpdb->prefix . 'dfw_deposits_payment_plans_schedule';
if( !empty ( $_POST ) ) {
$add_plan = $this->dfw_save_plan();
if ( is_wp_error( $add_plan ) ) {
echo '<div class="error"><p>' . $add_plan->get_error_message() . '</p></div>';
} elseif ( $add_plan ) {
echo '<div class="updated success"><p>' . __( 'Plan saved successfully', 'deposits-for-woocommerce' ) . '</p></div>';
}
}
if ( ! empty( $_GET['delete_plan'] ) ) {
$delete_id = absint( $_GET['delete_plan'] );
$wpdb->delete( $wpdb->dfw_deposits_payment_plans, array( 'ID' => $delete_id ) );
$wpdb->delete( $wpdb->dfw_deposits_payment_plans_schedule, array( 'plan_id' => $delete_id ) );
echo '<div class="updated success"><p>' . __( 'Plan deleted successfully', 'deposits-for-woocommerce' ) . '</p></div>';
}
$plan_name = '';
$plan_desc = '';
$schedule = array( 'plan_amount' => 0 );
if ( ! empty( $_REQUEST['plan_id'] ) ) {
$is_editing = absint( $_REQUEST['plan_id'] );
$plan = DFW_Manage_Plans::get_plan( $is_editing );
$plan_name = $plan->get_plan_name();
$plan_desc = $plan->get_plan_description();
$schedule = $plan->get_plan_schedule();
include( 'views/edit-payment-plans-html.php' );
} else {
$is_editing = false;
include( 'views/payment-plans-admin-html.php' );
}
}
public function dfw_save_plan(){
global $wpdb;
if( isset( $_POST['plan_name'] ) ) {
if ( ! empty( $_REQUEST['plan_id'] ) ) {
$is_editing = absint( $_REQUEST['plan_id'] );
} else {
$is_editing = false;
}
$plan_id = $is_editing;
$plan_name = empty( $_POST['plan_name'] )? __( 'Payment Plan', 'deposits-for-woocommerce' ): sanitize_text_field( $_POST['plan_name'] );
$plan_desc = empty( $_POST['plan_desc'] )? '': wp_kses_post( $_POST['plan_desc'] );
$plan_amounts = array_map( 'absint', $_POST['plan_amounts'] );
$interval_amounts = array_map( 'absint', $_POST['interval_amounts'] );
$interval_time = array_map( 'sanitize_text_field', $_POST['interval_time'] );
$plan_due = array_map( 'sanitize_text_field', $_POST['due'] );
$schedule = array();
$wpdb->dfw_deposits_payment_plans = $wpdb->prefix . 'dfw_deposits_payment_plans';
$wpdb->dfw_deposits_payment_plans_schedule = $wpdb->prefix . 'dfw_deposits_payment_plans_schedule';
if ( $is_editing ){
$wpdb->update(
$wpdb->dfw_deposits_payment_plans,
array(
'name' => $plan_name,
'description' => $plan_desc
),
array(
'ID' => $is_editing
)
);
} else {
$wpdb->insert(
$wpdb->dfw_deposits_payment_plans,
array(
'name' => $plan_name,
'description' => $plan_desc
)
);
$plan_id = $wpdb->insert_id;
}
if ( ! $plan_id ) {
return new WP_Error( 'error', __( 'Unable to save payment plan', 'deposits-for-woocommerce' ) );
}
foreach ( $plan_amounts as $index => $plan_amount ) {
$schedule[] = array (
'schedule_index' => $index,
'plan_id' => $plan_id,
'plan_amount' => $plan_amount,
'plan_due' => $plan_due[$index],
'plan_interval_amount' => $interval_amounts[$index],
'plan_interval_time' => $interval_time[$index]
);
}
$query = "SELECT schedule_index, plan_id, plan_amount, plan_due, plan_interval_amount, plan_interval_time FROM {$wpdb->dfw_deposits_payment_plans_schedule} WHERE plan_id =" . absint( $plan_id ) ."" ;
$existing = $wpdb->get_results( $query );
if( $existing !== $schedule ) {
$wpdb->delete( $wpdb->dfw_deposits_payment_plans_schedule, array( 'plan_id' => $plan_id ) );
foreach ( $schedule as $payment_row ) {
$wpdb->insert(
$wpdb->dfw_deposits_payment_plans_schedule, $payment_row
);
}
}
return true;
}
return false;
}
public function display_plans() {
if( ! class_exists( 'DFW_Payment_Plans_List' ) ) {
require_once( 'views/dfw-payment-plans-list.php' );
}
$list_table = new DFW_Payment_Plans_List();
$list_table->prepare_items();
$list_table->display();
}
}
$dfw_plans_admin_settings = new DFW_Plans_Admin_Settings();