*/ class Advanced_Usps_Shipping_Method_Admin { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Advanced_Usps_Shipping_Method_Loader as all of the hooks are defined * in that particular class. * * The Advanced_Usps_Shipping_Method_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/advanced-usps-shipping-method-admin.css', array(), $this->version, 'all' ); wp_enqueue_style( $this->plugin_name . '-ausm-media', plugin_dir_url( __FILE__ ) . 'css/ausm-media.css', array(), $this->version, 'all' ); wp_enqueue_style( $this->plugin_name . '-font-awesome', plugin_dir_url( __FILE__ ) . 'css/font-awesome.min.css', array(), $this->version, 'all' ); wp_enqueue_style( $this->plugin_name . 'plugin-new-style', plugin_dir_url( __FILE__ ) . 'css/plugin-new-style.css', array(), 'all' ); wp_enqueue_style( $this->plugin_name . '-header', plugin_dir_url( __FILE__ ) . 'css/ausm-header.css', array(), $this->version, 'all' ); wp_enqueue_style( $this->plugin_name . '-footer', plugin_dir_url( __FILE__ ) . 'css/ausm-footer.css', array(), $this->version, 'all' ); wp_enqueue_style( $this->plugin_name . '-promotional-bar', plugin_dir_url( __FILE__ ) . 'css/advanced-usps-shipping-method-promotional-bar.css', array(), 'all' ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Advanced_Usps_Shipping_Method_Loader as all of the hooks are defined * in that particular class. * * The Advanced_Usps_Shipping_Method_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/advanced-usps-shipping-method-admin.js', array('jquery'), $this->version, false ); wp_localize_script( $this->plugin_name, 'ausm_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'ausm_nonce' => wp_create_nonce( 'ausm-ajax-nonce' ), 'process_text' => esc_html__( 'Please wait...', 'advanced-usps-shipping-method' ), 'import_error_file' => esc_html__( 'Please choose JSON file for import.', 'advanced-usps-shipping-method' ), ) ); wp_enqueue_script( 'jquery-ui-sortable' ); wp_enqueue_script( $this->plugin_name . '-tiptip', plugin_dir_url( __FILE__ ) . 'js/jquery.tipTip.min.js', array('jquery'), $this->version, false ); wp_enqueue_script( $this->plugin_name . '-blockui', plugin_dir_url( __FILE__ ) . 'js/jquery.blockUI.min.js', array('jquery'), $this->version, false ); wp_enqueue_script( $this->plugin_name . '-freemius_pro', 'https://checkout.freemius.com/js/v1/', array('jquery'), $this->version, true ); } public function ausm_menu_for_setting() { global $GLOBALS; if ( empty( $GLOBALS['admin_page_hooks']['dots_store'] ) ) { add_menu_page( 'Dotstore Plugins', esc_html__( 'Dotstore Plugins', 'advanced-usps-shipping-method' ), 'null', 'dots_store', array(), 'dashicons-marker', 25 ); } //Configuration page add_submenu_page( 'dots_store', 'USPS Shipping', 'USPS Shipping', 'manage_options', 'advanced-usps-shipping-method', array($this, 'ausm_configuration_page') ); add_submenu_page( 'dots_store', 'Import/Export', 'Import/Export', 'manage_options', 'ausm-import-export', array($this, 'ausm_import_export_page') ); add_submenu_page( 'dots_store', 'Getting Started', 'Getting Started', 'manage_options', 'ausm-get-started', array($this, 'ausm_get_started_page') ); //Remove footer WP version $get_page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $page = ( !empty( $get_page ) ? sanitize_text_field( $get_page ) : '' ); if ( !empty( $page ) && (false !== strpos( $page, 'ausm' ) || false !== strpos( $page, 'advanced-usps-shipping-method' )) ) { remove_filter( 'update_footer', 'core_update_footer' ); } } /** * Add custom css for dotstore icon in admin area * */ public function ausm_admin_menu_icon_style() { echo ''; } /** * Create a menu for plugin. * * @param string $current current page. * * @since 3.6.1 */ public function ausm_pro_menus( $current = 'advanced-usps-shipping-method' ) { $wpfp_menus = array( 'main_menu' => array( 'pro_menu' => array( 'advanced-usps-shipping-method' => array( 'menu_title' => __( 'Configure USPS', 'advanced-usps-shipping-method' ), 'menu_slug' => 'advanced-usps-shipping-method', 'menu_url' => $this->ausm_pro_plugins_url( '', 'advanced-usps-shipping-method', '', '', '' ), ), 'ausm-import-export' => array( 'menu_title' => __( 'Settings', 'advanced-usps-shipping-method' ), 'menu_slug' => 'ausm-import-export', 'menu_url' => $this->ausm_pro_plugins_url( '', 'ausm-import-export', '', '', '' ), ), 'advanced-usps-shipping-method-account' => array( 'menu_title' => __( 'License', 'advanced-usps-shipping-method' ), 'menu_slug' => 'advanced-usps-shipping-method-account', 'menu_url' => esc_url( ausm_fs()->get_account_url() ), ), ), 'free_menu' => array( 'advanced-usps-shipping-method' => array( 'menu_title' => __( 'Configure USPS', 'advanced-usps-shipping-method' ), 'menu_slug' => 'advanced-usps-shipping-method', 'menu_url' => $this->ausm_pro_plugins_url( '', 'advanced-usps-shipping-method', '', '', '' ), ), ), ), ); $ausm_get_started = ( isset( $current ) && 'ausm-get-started' === $current ? 'active' : '' ); ?>
'advanced-usps-shipping-method', 'error' => true, ); } else { //Sanitizing requests $get_ausm_status = filter_input( INPUT_POST, 'ausm_status', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_debug_mode = filter_input( INPUT_POST, 'ausm_debug_mode', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_request_mode = filter_input( INPUT_POST, 'ausm_request_mode', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_api_mode = filter_input( INPUT_POST, 'ausm_api_mode', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_client_id = filter_input( INPUT_POST, 'ausm_client_id', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_client_secret = filter_input( INPUT_POST, 'ausm_client_secret', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_user_id = filter_input( INPUT_POST, 'ausm_user_id', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_origin = filter_input( INPUT_POST, 'ausm_origin', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_service_type = filter_input( INPUT_POST, 'ausm_service_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_rates_preference = filter_input( INPUT_POST, 'ausm_rates_preference', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $services_filter = array( 'ausm_services' => array( 'filter' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, 'flags' => FILTER_REQUIRE_ARRAY, ), ); $api_services_filter = array( 'ausm_api_services' => array( 'filter' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, 'flags' => FILTER_REQUIRE_ARRAY, ), ); $get_ausm_services = filter_input_array( INPUT_POST, $services_filter ); $get_ausm_api_services = filter_input_array( INPUT_POST, $api_services_filter ); $get_ausm_default_title = filter_input( INPUT_POST, 'ausm_default_title', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $get_ausm_default_rate = filter_input( INPUT_POST, 'ausm_default_rate', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); $get_ausm_cheapest_rate = filter_input( INPUT_POST, 'ausm_cheapest_rate', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); //Validating requests $ausm_status = ( isset( $get_ausm_status ) ? sanitize_text_field( $get_ausm_status ) : '' ); $ausm_debug_mode = ( isset( $get_ausm_debug_mode ) ? sanitize_text_field( $get_ausm_debug_mode ) : '' ); $ausm_request_mode = ( isset( $get_ausm_request_mode ) ? sanitize_text_field( $get_ausm_request_mode ) : 'legacy' ); $ausm_api_mode = ( isset( $get_ausm_api_mode ) ? sanitize_text_field( $get_ausm_api_mode ) : 'sandbox' ); $ausm_client_id = ( isset( $get_ausm_client_id ) ? sanitize_text_field( $get_ausm_client_id ) : '' ); $ausm_client_secret = ( isset( $get_ausm_client_secret ) ? sanitize_text_field( $get_ausm_client_secret ) : '' ); $ausm_user_id = ( isset( $get_ausm_user_id ) ? sanitize_text_field( $get_ausm_user_id ) : '' ); $ausm_origin = ( isset( $get_ausm_origin ) ? sanitize_text_field( $get_ausm_origin ) : '' ); $ausm_service_type = ( isset( $get_ausm_service_type ) ? sanitize_text_field( $get_ausm_service_type ) : '' ); $ausm_services = ( isset( $get_ausm_services ) ? $get_ausm_services['ausm_services'] : array() ); $ausm_api_services = ( isset( $get_ausm_api_services ) ? $get_ausm_api_services['ausm_api_services'] : array() ); $ausm_default_title = ( isset( $get_ausm_default_title ) ? sanitize_text_field( $get_ausm_default_title ) : '' ); $ausm_default_rate = ( isset( $get_ausm_default_rate ) ? sanitize_text_field( $get_ausm_default_rate ) : '' ); $ausm_cheapest_rate = ( isset( $get_ausm_cheapest_rate ) ? sanitize_text_field( $get_ausm_cheapest_rate ) : '' ); $ausm_rates_preference = ( isset( $get_ausm_rates_preference ) ? sanitize_text_field( $get_ausm_rates_preference ) : 'commercial' ); //Storing requests $ausm_data = array(); $ausm_data['ausm_status'] = $ausm_status; $ausm_data['ausm_debug_mode'] = $ausm_debug_mode; $ausm_data['ausm_request_mode'] = $ausm_request_mode; $ausm_data['ausm_api_mode'] = $ausm_api_mode; $ausm_data['ausm_client_id'] = $ausm_client_id; $ausm_data['ausm_client_secret'] = $ausm_client_secret; $ausm_data['ausm_user_id'] = $ausm_user_id; $ausm_data['ausm_origin'] = $ausm_origin; $ausm_data['ausm_service_type'] = $ausm_service_type; $ausm_data['ausm_services'] = $ausm_services; $ausm_data['ausm_api_services'] = $ausm_api_services; $ausm_data['ausm_default_title'] = $ausm_default_title; $ausm_data['ausm_default_rate'] = $ausm_default_rate; $ausm_data['ausm_cheapest_rate'] = $ausm_cheapest_rate; $ausm_data['ausm_rates_preference'] = $ausm_rates_preference; update_option( 'ausm_config', $ausm_data ); //Return message $return_arr = array( 'page' => 'advanced-usps-shipping-method', 'success' => true, ); } wp_safe_redirect( add_query_arg( $return_arr, admin_url( 'admin.php' ) ) ); exit; } /** * Remove section from shipping settings because we have added new menu in admin section * * @param array $sections * * @return array $sections * * @since 1.0.0 */ public function ausm_remove_section( $sections ) { unset($sections['dots_ausm']); return $sections; } /** * Remove submenu from admin screeen * * @since 1.0.0 */ public function ausm_remove_admin_submenus() { remove_submenu_page( 'dots_store', 'ausm-import-export' ); remove_submenu_page( 'dots_store', 'ausm-get-started' ); } /** * Fetch USPS API Access Token with client_id/secret. * * @param string $client_id * @param string $client_secret * @param string $api_mode ('production' or 'sandbox') * @param bool $return_ajax_response If true returns wp_send_json_*, otherwise returns array/object or WP_Error. * @return mixed */ function ausm_fetch_usps_access_token( $client_id, $client_secret, $api_mode = 'sandbox', $return_ajax_response = false ) { $request_data = array( 'client_id' => $client_id, 'client_secret' => $client_secret, 'response_type' => 'code', 'scope' => 'prices labels tracking payments international-prices international-labels carrier-pickup standards service-standards', 'grant_type' => 'client_credentials', ); $request_body = wp_json_encode( $request_data ); $endpoint = ( 'production' === $api_mode ? 'https://apis.usps.com/oauth2/v3/token' : 'https://apis-tem.usps.com/oauth2/v3/token' ); $response = wp_remote_post( $endpoint, array( 'method' => 'POST', 'timeout' => 30, 'blocking' => true, 'body' => $request_body, 'headers' => array( 'Content-Type' => 'application/json', ), ) ); if ( is_wp_error( $response ) ) { if ( $return_ajax_response ) { return wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } return $response; } $response_body = json_decode( $response['body'] ); if ( isset( $response_body->error ) ) { if ( $return_ajax_response ) { return wp_send_json_error( array( 'message' => $response_body->error, ) ); } return new WP_Error('usps_access_token_error', $response_body->error); } if ( isset( $response_body->status ) && 'approved' === $response_body->status && isset( $response_body->access_token ) ) { return $response_body->access_token; } else { if ( $return_ajax_response ) { return wp_send_json_error( array( 'message' => __( 'Access token not found', 'advanced-usps-shipping-method' ), ) ); } return new WP_Error('usps_access_token_error', __( 'Access token not found', 'advanced-usps-shipping-method' )); } // Default failure. if ( $return_ajax_response ) { return wp_send_json_error( array( 'message' => __( 'Failed to connect to USPS API', 'advanced-usps-shipping-method' ), ) ); } return new WP_Error('usps_access_token_error', __( 'Failed to connect to USPS API', 'advanced-usps-shipping-method' )); } /** * Admin footer review * * @since 1.0.0 */ public function ausm_pro_admin_footer_review() { $html = ''; $url = ''; $url = esc_url( 'https://wordpress.org/plugins/advanced-usps-shipping-method/#reviews' ); /* translators: %1$s is the URL to the review page */ $html = sprintf( wp_kses( __( 'We need your support to keep updating and improving the plugin. Please help us by leaving a good review :) Thanks!', 'advanced-usps-shipping-method' ), array( 'strong' => array(), 'a' => array( 'href' => array(), 'target' => 'blank', ), ) ), esc_url( $url ) ); echo wp_kses_post( $html ); } /** * Add review stars in plugin row meta * * @since 1.0.0 */ public function ausm_plugin_row_meta_action_links( $plugin_meta, $plugin_file, $plugin_data, $status ) { if ( isset( $plugin_data['TextDomain'] ) && $plugin_data['TextDomain'] !== 'advanced-usps-shipping-method' ) { return $plugin_meta; } $url = ''; $url = esc_url( 'https://wordpress.org/plugins/advanced-usps-shipping-method/#reviews' ); $plugin_meta[] = sprintf( '%s', $url, esc_html( '★★★★★' ) ); return $plugin_meta; } /** * Get dynamic promotional bar of plugin * * @param String $plugin_slug slug of the plugin added in the site option * @since 1.0.5 * * @return null */ public function ausm_get_promotional_bar( $plugin_slug = '' ) { $promotional_bar_upi_url = AUSM_STORE_URL . 'wp-json/dpb-promotional-banner/v2/dpb-promotional-banner?' . wp_rand(); $promotional_banner_request = wp_remote_get( $promotional_bar_upi_url ); //phpcs:ignore if ( empty( $promotional_banner_request->errors ) ) { $promotional_banner_request_body = $promotional_banner_request['body']; $promotional_banner_request_body = json_decode( $promotional_banner_request_body, true ); echo '