WP_REST_Server::EDITABLE, 'callback' => array($this, 'set_settings'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/getuserroles', array( 'methods' => 'POST', 'callback'=> array($this, 'getuserroles'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() )); register_rest_route('grwoo/v1', '/getproductcategories', array( 'methods' => 'POST', 'callback'=> array($this, 'getproductcategories'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() )); register_rest_route('grwoo/v1', '/getorderdetails', array( 'methods' => 'POST', 'callback'=> array($this, 'getorderdetails'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() )); register_rest_route('grwoo/v1', '/getcustomerorders', array( 'methods' => 'POST', 'callback'=> array($this, 'getcustomerorders'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() )); register_rest_route('grwoo/v1', '/getversion', array( 'methods' => 'POST', 'callback'=> array($this, 'getversion'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() )); register_rest_route('grwoo/v1', '/getPage', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array($this, 'get_page'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/addPage', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array($this, 'add_page'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/editPage', array( array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array($this, 'edit_page'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/deletePage', array( array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array($this, 'delete_page'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/verifyUser', array( array( 'methods' => 'POST', 'callback' => array($this, 'verify_user'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/verifyReviewEnabled', array( array( 'methods' => 'POST', 'callback' => array($this, 'verify_review_enabled'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/verifyCouponCode', array( array( 'methods' => 'POST', 'callback' => array($this, 'verify_coupon_code'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/updateCouponCode', array( array( 'methods' => 'POST', 'callback' => array($this, 'update_coupon_code'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/deleteCouponCode', array( array( 'methods' => 'POST', 'callback' => array($this, 'delete_coupon_code'), 'permission_callback' => array($this, 'check_api_permission'), 'args' => array() ) )); register_rest_route('grwoo/v1', '/resetInstallation', array( array( 'methods' => 'POST', 'callback' => array($this, 'reset_installation'), 'permission_callback' => array($this, 'check_api_permission_lite'), 'args' => array() ) )); } public function getversion($request) { try { $version = ''; if (class_exists('GR_Connect')) { $version = GR_Connect::$_plugin_version; } $data = array('error' => 0, 'plugin_version' => $version); } catch (Exception $e) { $data['error'] = 1; $data['msg'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function check_api_permission($request) { if (strpos($request->get_header('user_agent'), 'Appsmav') === false) { return false; } else { $payload = get_option('grconnect_payload', 0); $post_payload = sanitize_text_field($_POST['payload']); if (empty($_POST['payload']) || $payload != $post_payload) { return false; } } return true; } public function check_api_permission_lite($request) { if (strpos($request->get_header('user_agent'), 'Appsmav') === false) { return false; } return true; } public function getproductcategories() { $data = array(); try { $cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => false, ); $categories = get_terms( 'product_cat', $cat_args ); $data = array( 'error' => 0, 'product_categories' => !empty($categories) ? $categories : array() ); } catch(Exception $e) { $data['error'] = 1; $data['msg'] = "Something went wrong"; } $data['plugin_version'] = GR_Connect::$_plugin_version; return new WP_REST_Response($data, 200); } public function getorderdetails() { $data = array(); try { $order_id = sanitize_text_field($_POST['order_id']); $order = new WC_Order($order_id); if (empty($order)) { throw new Exception("Order not found"); } $data = array( 'error' => 0, 'order' => $order->get_data() ); } catch(Exception $e) { $data['error'] = 1; $data['msg'] = $e->getMessage(); } $data['plugin_version'] = GR_Connect::$_plugin_version; return new WP_REST_Response($data, 200); } public function getcustomerorders() { $data = array(); try { $email = sanitize_text_field($_POST['email']); $user = get_user_by( 'email', $email); // Get logged in user's order list $customer_orders = get_posts( array( 'meta_key' => '_customer_user', 'meta_value' => $user->id, 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_order_statuses() ), 'numberposts' => -1 )); $args = array( 'limit' => 1000, 'customer' => $user->id ); $customer_orders = wc_get_orders( $args ); foreach ( $customer_orders as $order ) { $orders[] = array( 'id_order' => $order->get_id(), 'amount' => $order->get_total(), 'discount' => $order->get_total_discount(), 'order_number' => $order->get_id(), 'first_name' => $order->get_billing_first_name(), 'last_name' => $order->get_billing_last_name(), 'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), 'currency' => $order->get_currency(), 'coupon' => $order->get_used_coupons(), 'status' => $order->get_status() ); } $data = array( 'error' => 0, 'order' => $orders ); } catch(Exception $e) { $data['error'] = 1; $data['msg'] = $e->getMessage(); } $data['plugin_version'] = GR_Connect::$_plugin_version; return new WP_REST_Response($data, 200); } public function getuserroles($request) { try { global $wp_roles; $user_roles = $wp_roles->get_names(); $data = array( 'error' => 0, 'user_roles' => !empty($user_roles) ? $user_roles : array() ); } catch(Exception $e) { $data['error'] = 1; $data['msg'] = "Something went wrong"; } return new WP_REST_Response($data, 200); } public function set_settings($request) { $data = array('error' => 0); $data['review_enabled'] = (get_option('woocommerce_enable_reviews', 0) === 'yes') ? 'yes' : 'no'; try { if(empty($_POST['data'])) throw new Exception('No config to set'); if(empty($_POST['data']) || !is_array($_POST['data'])) throw new Exception('Invalid config to set'); $config = $_POST['data']; $app_config = gr_get_app_config(); if(!empty($app_config) && is_array($app_config)) $config = array_merge($app_config, $config); $config['date_updated'] = time(); if(gr_set_app_config($config) == FALSE) throw new Exception(__('Config file is not created')); //$data['config'] = $config; $data['msg'] = __('Settings updated successfully'); } catch(Exception $e) { $data['error'] = 1; $data['msg'] = $e->getMessage(); } $data['plugin_version'] = GR_Connect::$_plugin_version; return new WP_REST_Response($data, 200); } public function get_page($request) { $data = array('error' => 0); try { if (empty($_POST['id'])) { throw new Exception('Invalid Page'); } $id_post = sanitize_text_field($_POST['id']); if (!get_post_status($id_post)) { throw new Exception('Invalid Page'); } $page = get_post($id_post); if(is_wp_error($page)) { throw new Exception('cannot_update_page'. $page->get_error_message()); } $data['error'] = 0; $data['id'] = $page->ID; $data['url'] = get_permalink($id); $data['is_embed_landing_url'] = get_post_meta(get_the_ID(), 'is_embed_landing_url'); $data['msg'] = 'Success'; } catch(Exception $e) { $data['error'] = 1; $data['error_message'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function add_page($request) { $data = array('error' => 0); try { if (empty($_POST['title'])) { throw new Exception('Invalid Title'); } if (empty($_POST['content'])) { throw new Exception('Invalid Content'); } $new_page = array( 'post_title' => sanitize_text_field($_POST['title']), 'post_content' => sanitize_text_field($_POST['content']), 'post_status' => 'publish', 'post_type' => 'page', 'meta_input' => array( 'is_embed_landing_url' => 1 ) ); $id = wp_insert_post( $new_page, $wp_error = false ); if(is_wp_error($id)) { throw new Exception('cannot_create_page'. $id->get_error_message()); } $data['error'] = 0; $data['id'] = $id; $data['url'] = get_permalink($id); $data['msg'] = 'Success'; } catch(Exception $e) { $data['error'] = 1; $data['error_message'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function edit_page($request) { $data = array('error' => 0); try { if (isset($_POST['title']) && empty($_POST['title']) && !isset($_POST['publish'])) { throw new Exception('Invalid Title'); } if (empty($_POST['id'])) { throw new Exception('Invalid Page'); } $params['ID'] = sanitize_text_field($_POST['id']); if (!get_post_status($params['ID'])) { throw new Exception('Invalid Page'); } if (isset($_POST['publish'])) { $publish_status = sanitize_text_field($_POST['publish']); $params['post_status'] = ($publish_status == 1) ? 'publish' : 'draft'; update_post_meta($params['ID'], 'is_embed_landing_url', $publish_status); } else { $params['post_title'] = sanitize_text_field($_POST['title']); } $id = wp_update_post( $params, $wp_error = true ); if(is_wp_error($id)) throw new Exception('cannot_update_page'. $id->get_error_message()); $page_info = get_post($id); $data['error'] = 0; $data['id'] = $page_info->ID; $data['title'] = $page_info->post_title; $data['url'] = get_permalink($page_info->ID); $data['msg'] = 'Success'; } catch(Exception $e) { $data['error'] = 1; $data['error_message'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function delete_page($request) { $data = array('error' => 0); try { if (empty($_POST['id'])) { throw new Exception('Invalid Page'); } $id_page = sanitize_text_field($_POST['id']); if (!get_post_status($id_page)) { throw new Exception('Invalid Page'); } if(!wp_delete_post($id_page, true)) { throw new Exception('cannot_delete_page'); } $data['error'] = 0; $data['msg'] = 'Success'; } catch(Exception $e) { $data['error'] = 1; $data['error_message'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function verify_user($request) { $data['error'] = 1; $data['msg'] = 'No User Exist'; try { if (empty($_POST['verify_user'])) { throw new Exception('Invalid Email'); } if (class_exists('GR_Connect')) { $data['plugin_version'] = GR_Connect::$_plugin_version; } $email = sanitize_email( $_POST['verify_user'] ); $user = get_user_by('email', $email); if (!empty($user)) { $data['error'] = 0; $data['msg'] = 'User Exist'; $data['name'] = $user->first_name . ' ' . $user->last_name; $data['id'] = $user->ID; } } catch(Exception $e) { $data['error'] = 1; $data['error_message'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function verify_review_enabled($request) { try { $data['error'] = 0; $data['msg'] = get_option('woocommerce_enable_reviews', 'no'); if (class_exists('GR_Connect')) { $data['plugin_version'] = GR_Connect::$_plugin_version; } } catch(Exception $e) { $data['error'] = 1; $data['msg'] = 'Invalid'; } return new WP_REST_Response($data, 200); } public function verify_coupon_code($request) { try { $data['error'] = 0; if (empty($_POST['coupon_code'])) { throw new Exception('Coupon code cannot be empty. Please check'); } $coupon_code = sanitize_text_field($_POST['coupon_code']); $coupon = new WC_Coupon($coupon_code); if (!empty($coupon->id)) throw new Exception('Yes'); else throw new Exception('No'); } catch(Exception $e) { $data['error'] = 1; $data['msg'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function update_coupon_code($request) { try { $data['error'] = 0; if (empty($_POST['old_coupon_code'])) { throw new Exception('Coupon code cannot be empty. Please check'); } if (empty($_POST['new_coupon_code'])) { throw new Exception('Coupon code cannot be empty. Please enter a unique coupon code.'); } $old_coupon_code = sanitize_text_field($_POST['old_coupon_code']); $new_coupon_code = sanitize_text_field($_POST['new_coupon_code']); $coupon = new WC_Coupon($new_coupon_code); if (!empty($coupon->id)) throw new Exception('Coupon code already exists. Please check and enter a new unique coupon code'); $coupon = new WC_Coupon($old_coupon_code); if (empty($coupon->id)) throw new Exception('Coupon code not found. Please check and try again'); // Update coupon details starts $my_post = array( 'ID' => $coupon->id, 'post_title' => $new_coupon_code ); $post_id = wp_update_post( $my_post ); if ( is_wp_error( $post_id ) ) { throw new Exception( $post_id->get_error_message()); } $data['msg'] = 'Successfully updated'; } catch(Exception $e) { $data['error'] = 1; $data['msg'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function delete_coupon_code($request) { try { $data['error'] = 0; if (empty($_POST['coupon_code'])) { throw new Exception('Invalid coupon code'); } $coupon_code = sanitize_text_field($_POST['coupon_code']); $coupon = new WC_Coupon($coupon_code); if (!empty($coupon->id)) { $post_id = wp_delete_post($coupon->id, TRUE); if ( is_wp_error( $post_id ) ) { throw new Exception( $post_id->get_error_message()); } $data['msg'] = 'Successfully Deleted'; } else { $data['msg'] = 'Coupon code not found.'; } } catch(Exception $e) { $data['error'] = 1; $data['msg'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } public function reset_installation($request) { try { $data['error'] = 0; // Reset flags to show login screen update_option('grconnect_register', 3); $data['msg'] = 'yes'; } catch(Exception $e) { $data['error'] = 1; $data['msg'] = $e->getMessage(); } return new WP_REST_Response($data, 200); } }