'GET', 'callback' => array($this, 'get_rules'), 'permission_callback' => array($this, 'get_permission') )); register_rest_route('awdp/v1', '/rules/(?P\d+)', array( 'methods' => 'GET', 'callback' => array($this, 'get_rules'), 'permission_callback' => array($this, 'get_permission'), 'args' => ['id'] )); register_rest_route('awdp/v1', '/rules/', array( 'methods' => 'POST', 'callback' => array($this, 'post_rule'), 'permission_callback' => array($this, 'get_permission') )); register_rest_route('awdp/v1', '/delete/', array( 'methods' => 'POST', 'callback' => array($this, 'action_delete'), 'permission_callback' => array($this, 'get_permission') )); register_rest_route('awdp/v1', '/userroles/', array( 'methods' => 'POST', 'callback' => array($this, 'user_roles'), 'permission_callback' => array($this, 'get_permission') )); register_rest_route('awdp/v1', '/productlist/', array( 'methods' => 'GET', 'callback' => array($this, 'product_list'), 'permission_callback' => array($this, 'get_permission') )); register_rest_route('awdp/v1', '/productlist/(?P\d+)', array( 'methods' => 'GET', 'callback' => array($this, 'product_list'), 'permission_callback' => array($this, 'get_permission'), 'args' => ['id'] )); register_rest_route('awdp/v1', '/product_rule/', array( 'methods' => 'POST', 'callback' => array($this, 'product_rule'), 'permission_callback' => array($this, 'get_permission') )); register_rest_route('awdp/v1', '/awdp_settings/', array( 'methods' => 'POST', 'callback' => array($this, 'awdp_settings'), 'permission_callback' => array($this, 'get_permission') )); register_rest_route('awdp/v1', '/awdp_settings/', array( 'methods' => 'GET', 'callback' => array($this, 'awdp_settings'), 'permission_callback' => array($this, 'get_permission') )); register_rest_route('awdp/v1', '/data/products', array( 'methods' => 'GET', 'callback' => array($this, 'get_products'), 'permission_callback' => array($this, 'get_permission') )); }); } /** * * Ensures only one instance of AWDP is loaded or can be loaded. * * @since 1.0.0 * @static * @see WordPress_Plugin_Template() * @return Main AWDP instance */ public static function instance($file = '', $version = '1.0.0') { if (is_null(self::$_instance)) { self::$_instance = new self($file, $version); } return self::$_instance; } function action_delete($data) { $data = $data->get_params(); if ($data['id']) { $pt = get_post_type($data['id']); if ($pt == AWDP_POST_TYPE && wp_delete_post($data['id'], true)) { return admin_url('admin.php?page=awdp_admin_ui'); } else if ($pt == AWDP_PRODUCT_LIST && wp_delete_post($data['id'], true)) { return admin_url('admin.php?page=awdp_admin_product_lists'); } } } function user_roles($data) { $result = array(); $aw_index = 0; global $wp_roles; if (!isset($wp_roles)) $wp_roles = new WP_Roles(); $result[$aw_index]['label'] = 'All Roles'; $result[$aw_index]['text'] = 0; $aw_index++; foreach ($wp_roles->get_names() as $key => $value) { $result[$aw_index]['label'] = $value; $result[$aw_index]['text'] = $key; $aw_index++; } return new WP_REST_Response($result, 200); } function awdp_settings($data) { $data = $data->get_params(); $result = array(); $timezone = $data['time_zone']; if (!get_option('awdp_timezone')) add_option('awdp_timezone', $timezone, '', 'yes'); else update_option('awdp_timezone', $timezone); $result['awdp_timezone'] = get_option('awdp_timezone'); return new WP_REST_Response($result, 200); } function post_rule($data) { $this->delete_transient(); $data = $data->get_params(); if ($data['id']) { $my_post = array( 'ID' => $data['id'], 'post_title' => wp_strip_all_tags($data['name']), 'post_content' => '', ); wp_update_post($my_post); $this->rule_update_meta($data, $data['id']); return $data['id']; } else { $my_post = array( 'post_type' => AWDP_POST_TYPE, 'post_title' => wp_strip_all_tags($data['name']), 'post_content' => '', 'post_status' => 'publish', ); $id = wp_insert_post($my_post); $this->rule_update_meta($data, $id); return $id; } } public function delete_transient() { delete_transient(AWDP_PRODUCTS_TRANSIENT_KEY); } function rule_update_meta($data, $id) { $wdp_start_date = isset($data['start_date']) ? $data['start_date'] : ''; $wdp_end_date = isset($data['end_date']) ? $data['end_date'] : ''; $wdp_discount_type = isset($data['discount_type']) ? $data['discount_type'] : ''; $wdp_discount = isset($data['discount']) ? $data['discount'] : ''; $wdp_status = isset($data['status']) ? $data['status'] : ''; $wdp_pricing_table = isset($data['pricing_table']) ? $data['pricing_table'] : ''; $wdp_priority = isset($data['priority']) ? $data['priority'] : ''; $wdp_product_list = isset($data['product_list']) ? $data['product_list'] : ''; $wdp_inc_tax = isset($data['inc_tax']) ? $data['inc_tax'] : ''; $wdp_label = isset($data['label']) ? $data['label'] : ''; $wdp_sequentially = isset($data['sequentially']) ? $data['sequentially'] : ''; $wdp_usage_limit = isset($data['usage_limit']) ? $data['usage_limit'] : ''; $wdp_disable_on_sale = isset($data['disable_on_sale']) ? $data['disable_on_sale'] : ''; $wdp_show_in_loop = isset($data['show_in_loop']) ? $data['show_in_loop'] : ''; $wdp_rules = isset($data['rules']) ? $data['rules'] : ''; $wdp_quantity_type = isset($data['quantity_type']) ? $data['quantity_type'] : ''; $wdp_weekday = isset($data['discount_schedule_weekday']) ? $data['discount_schedule_weekday'] : ''; $start_time = isset($data['startTime']) ? date('H:i', strtotime($data['startTime'] )) : ''; $end_time = isset($data['endTime']) ? date('H:i', strtotime($data['endTime'] )) : ''; $table_layout = isset($data['table_layout']) ? $data['table_layout'] : ''; $discount_schedule_days = isset($data['discount_schedule_days']) ? serialize($data['discount_schedule_days']) : ''; $schedules = isset($data['schedules']) ? $data['schedules'] : ''; $schedule_array = []; $key = 0; foreach($schedules as $schedule){ // Start Date if($schedule['start_date']){ $start_date = $schedule['start_date']; $start_date = date("Y-m-d H:i:s", strtotime($start_date)); if( ( strtotime(get_post_meta($id, 'discount_start_date', true)) > strtotime($start_date) ) || $key == 0 ) { update_post_meta($id, 'discount_start_date', $start_date); } } else { $start_date = ''; } // End Date if($schedule['end_date']){ $end_date = $schedule['end_date']; $end_date = date("Y-m-d H:i:s", strtotime($end_date)); if( ( strtotime(get_post_meta($id, 'discount_end_date', true)) < strtotime($end_date) ) || $key == 0 ) { update_post_meta($id, 'discount_end_date', $end_date); } } else { update_post_meta($id, 'discount_end_date', ''); $end_date = ''; } $schedule_array[$key][start_date] = $start_date; $schedule_array[$key][end_date] = $end_date; $key++; } $serialize_data = array_values($schedule_array); $schedule_serialize = serialize($serialize_data); $quantityranges = isset($data['quantityranges']) ? serialize($data['quantityranges']) : ''; $cartamount = isset($data['cartamount']) ? serialize($data['cartamount']) : ''; update_post_meta($id, 'discount_schedules', $schedule_serialize); update_post_meta($id, 'discount_quantityranges', $quantityranges); update_post_meta($id, 'discount_cartamount', $cartamount); update_post_meta($id, 'discount_start_time', $start_time); update_post_meta($id, 'discount_end_time', $end_time); update_post_meta($id, 'discount_schedule_days', $discount_schedule_days); update_post_meta($id, 'discount_quantity_type', $wdp_quantity_type); update_post_meta($id, 'discount_schedule_weekday', $wdp_weekday); update_post_meta($id, 'discount_table_layout', $table_layout); update_post_meta($id, 'discount_type', $wdp_discount_type); update_post_meta($id, 'discount_value', $wdp_discount); update_post_meta($id, 'discount_status', $wdp_status); update_post_meta($id, 'discount_pricing_table', $wdp_pricing_table); update_post_meta($id, 'discount_priority', $wdp_priority); update_post_meta($id, 'discount_product_list', $wdp_product_list); $other_config = array( 'inc_tax' => $wdp_inc_tax, 'label' => $wdp_label, 'sequentially' => $wdp_sequentially, 'usage_limit' => $wdp_usage_limit, 'disable_on_sale' => $wdp_disable_on_sale, 'show_in_loop' => $wdp_show_in_loop, 'rules' => base64_encode(serialize($wdp_rules)), ); update_post_meta($id, 'discount_config', $other_config); } function get_rules($data) { if (isset($data['id'])) { $result = array(); $discount_rule = get_post($data['id']); $discount_config = get_post_meta($discount_rule->ID, 'discount_config', true); // Scheduling dates if(get_post_meta($discount_rule->ID, 'discount_schedules', true)){ $schedules = unserialize(get_post_meta($discount_rule->ID, 'discount_schedules', true)); } else if(get_post_meta($discount_rule->ID, 'discount_start_date', true) && get_post_meta($discount_rule->ID, 'discount_end_date', true)){ // data before scheduling $schedules[0]['start_date'] = get_post_meta($discount_rule->ID, 'discount_start_date', true); $schedules[0]['end_date'] = get_post_meta($discount_rule->ID, 'discount_end_date', true); } $result = Array( 'name' => $discount_rule->post_title, 'id' => $discount_rule->ID, 'status' => get_post_meta($discount_rule->ID, 'discount_status', true), 'pricing_table' => get_post_meta($discount_rule->ID, 'discount_pricing_table', true), 'discount_type' => get_post_meta($discount_rule->ID, 'discount_type', true), 'start_date' => get_post_meta($discount_rule->ID, 'discount_start_date', true), 'end_date' => get_post_meta($discount_rule->ID, 'discount_end_date', true), 'start_time' => date('Y-m-d H:i:s', strtotime(get_post_meta($discount_rule->ID, 'discount_start_time', true))), 'end_time' => date('Y-m-d H:i:s', strtotime(get_post_meta($discount_rule->ID, 'discount_end_time', true))), 'discount_schedule_days' => unserialize(get_post_meta($discount_rule->ID, 'discount_schedule_days', true)), 'product_list' => (int)get_post_meta($discount_rule->ID, 'discount_product_list', true), 'priority' => get_post_meta($discount_rule->ID, 'discount_priority', true), 'discount' => get_post_meta($discount_rule->ID, 'discount_value', true), 'table_layout' => get_post_meta($discount_rule->ID, 'discount_table_layout', true), 'quantityranges' => unserialize(get_post_meta($discount_rule->ID, 'discount_quantityranges', true)), 'cartamount' => unserialize(get_post_meta($discount_rule->ID, 'discount_cartamount', true)), 'quantity_type' => get_post_meta($discount_rule->ID, 'discount_quantity_type', true), 'schedule_weekday' => get_post_meta($discount_rule->ID, 'discount_schedule_weekday', true), 'schedules' => $schedules, 'disable_on_sale' => $discount_config['disable_on_sale'], 'inc_tax' => $discount_config['inc_tax'], 'label' => $discount_config['label'], 'usage_limit' => $discount_config['usage_limit'], 'sequentially' => $discount_config['sequentially'], 'show_in_loop' => $discount_config['show_in_loop'], 'rules' => unserialize(base64_decode($discount_config['rules'])) ); return new WP_REST_Response($result, 200); } $all_listings = get_posts(array('posts_per_page' => -1, 'post_type' => AWDP_POST_TYPE)); $result = array(); $discount_type_name = Array( 'percent_total_amount' => 'Percentage of cart total amount', 'percent_product_price' => 'Percentage of product price', 'fixed_product_price' => 'Fixed price of product price', 'fixed_cart_amount' => 'Fixed price of cart total amount', 'cart_quantity' => 'Quantity based discount' ); foreach ($all_listings as $value) { $result[] = Array( 'discount_id' => $value->ID, 'discount_title' => $value->post_title, 'discount_status' => get_post_meta($value->ID, 'discount_status', true), 'discount_start_date' => get_post_meta($value->ID, 'discount_start_date', true) ? date_format(date_create(get_post_meta($value->ID, 'discount_start_date', true)), 'd M Y') : '', 'discount_end_date' => get_post_meta($value->ID, 'discount_end_date', true) ? date_format(date_create(get_post_meta($value->ID, 'discount_end_date', true)), 'd M Y') : '', 'discount_type' => get_post_meta($value->ID, 'discount_type', true), 'discount_value' => get_post_meta($value->ID, 'discount_type', true) != 'cart_quantity' ? get_post_meta($value->ID, 'discount_value', true) : '', 'discount_date' => date_format(date_create($value->post_date), 'd M Y'), 'discount_priority' => get_post_meta($value->ID, 'discount_priority', true), 'discount_type_name' => $discount_type_name[get_post_meta($value->ID, 'discount_type', true)] ); } return new WP_REST_Response($result, 200); } function product_list($data) { if (isset($data['id'])) { $result = array(); $list_item = get_post($data['id']); $result['list_name'] = $list_item->post_title; $result['list_id'] = $list_item->ID; $result['list_type'] = get_post_meta($list_item->ID, 'list_type', true); $other_config = get_post_meta($list_item->ID, 'product_list_config', true); $result['selectedProducts'] = ($other_config['selectedProducts']); $result['productAuthor'] = ($other_config['productAuthor']); $result['excludedProducts'] = ($other_config['excludedProducts']); $result['taxRelation'] = ($other_config['taxRelation']); $result['rules'] = ($other_config['rules']); $defaultProducts = array_merge(is_array($result['excludedProducts']) ? $result['excludedProducts'] : [], is_array($result['selectedProducts']) ? $result['selectedProducts'] : []); $result['defaultProducts'] = empty($defaultProducts) ? [] : $this->get_products($defaultProducts); // it will be using for product list suggestion dropdown return new WP_REST_Response($result, 200); } $all_listings = get_posts(array('numberposts' => -1, 'post_type' => AWDP_PRODUCT_LIST)); $result = array(); foreach ($all_listings as $value) { $result[] = array( 'list_id' => $value->ID, 'list_name' => $value->post_title, 'list_date' => date_format(date_create($value->post_date), 'd M Y') ); } return new WP_REST_Response($result, 200); } /** * */ public function get_products($arg) { if (is_a($arg, 'WP_REST_Request')) { $productslist = get_posts(array('numberposts' => -1, 'post_type' => 'product')); $products = Array(); foreach ($productslist as $product) { $products[] = [ 'value' => $product->ID, 'label' => $product->post_title, 'post_thumbnail' => get_the_post_thumbnail_url($product->ID) ]; } return new WP_REST_Response($products, 200); } else { $productslist = get_posts(array('numberposts' => -1, 'post_type' => 'product', 'post__in' => $arg)); $products = Array(); foreach ($productslist as $product) { $products[] = [ 'value' => $product->ID, 'label' => $product->post_title, 'post_thumbnail' => get_the_post_thumbnail_url($product->ID) ]; } return $products; } } function product_rule($data) { $data = $data->get_params(); $this->delete_transient(); if ($data['id']) { $my_post = array( 'ID' => $data['id'], 'post_title' => wp_strip_all_tags($data['name']), 'post_content' => '', ); wp_update_post($my_post); $this->update_product_rule_meta($data['id'], $data); return $data['id']; } else { $my_post = array( 'post_type' => AWDP_PRODUCT_LIST, 'post_title' => wp_strip_all_tags($data['name']), 'post_content' => '', 'post_status' => 'publish', ); $id = wp_insert_post($my_post); $this->update_product_rule_meta($id, $data); return $id; } } function update_product_rule_meta($id, $data) { update_post_meta($id, 'list_type', $data['list_type']); $other_config = array( 'selectedProducts' => ($data['selectedProducts']), 'productAuthor' => ($data['productAuthor']), 'excludedProducts' => ($data['excludedProducts']), 'taxRelation' => ($data['taxRelation']), 'rules' => ($data['rules']), ); update_post_meta($id, 'product_list_config', $other_config); } /** * Permission Callback **/ public function get_permission() { return true; if (!(current_user_can('administrator') || current_user_can('editor'))) { return false; } else { return true; } } /** * Cloning is forbidden. * * @since 1.0.0 */ public function __clone() { _doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?'), $this->_version); } /** * Unserializing instances of this class is forbidden. * * @since 1.0.0 */ public function __wakeup() { _doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?'), $this->_version); } }