*/ if ( ! defined( 'ABSPATH' ) ) { exit; } if ( class_exists( 'Alopeyk_WooCommerce_Shipping_Admin' ) ) { return; } /** * @since 1.0.0 */ class Alopeyk_WooCommerce_Shipping_Admin { private $plugin_name; private $version; /** * @since 1.0.0 * @param string $plugin_name * @param string $version */ public function __construct( $plugin_name = null, $version = null ) { $this->plugin_name = $plugin_name; $this->version = $version; $this->set_helpers(); } /** * @since 1.0.0 */ public function set_helpers() { $this->helpers = new Alopeyk_WooCommerce_Shipping_Common(); } /** * @since 1.0.0 * @return boolean */ public function is_order_edit() { $screen = get_current_screen(); return $screen->id == 'shop_order'; } /** * @since 1.0.0 */ public function enqueue_styles() { $screen = get_current_screen(); if ( in_array( $screen->id, array( 'edit-shop_order', 'edit-alopeyk_order', 'alopeyk_order', 'shop_order' ) ) || strpos( $screen->id, '_page_alopeyk-credit' ) ) { wp_deregister_style( 'jquery-ui-style' ); wp_enqueue_style( 'wp-jquery-ui-dialog' ); } wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/alopeyk-woocommerce-shipping-admin' . ( WP_DEBUG ? '.min' : '' ) . '.css', array(), $this->version, 'all' ); if ( $this->is_order_edit() ) { wp_enqueue_style( $this->plugin_name . '__front', plugins_url( 'public/css/alopeyk-woocommerce-shipping-public' . ( WP_DEBUG ? '.min' : '' ) . '.css', dirname( __FILE__ ) ), array(), $this->version, 'all' ); } } /** * @since 1.0.0 */ public function enqueue_scripts() { wp_enqueue_media(); wp_enqueue_script( 'jquery-ui-dialog' ); wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/alopeyk-woocommerce-shipping-admin' . ( WP_DEBUG ? '.min' : '' ) . '.js', array( 'jquery' ), $this->version, false ); if ( $this->is_order_edit() ) { wp_enqueue_script( $this->plugin_name . '__front', plugins_url( 'public/js/alopeyk-woocommerce-shipping-public' . ( WP_DEBUG ? '.min' : '' ) . '.js', dirname( __FILE__ ) ), array(), $this->version, 'all' ); } } /** * @since 1.0.0 * @return boolean */ public function get_wc_settings_url() { return version_compare( WC()->version, '2.1', '>=' ) ? 'wc-settings' : 'woocommerce_settings'; } /** * @since 1.0.0 * @return boolean */ public function supports_plugin_meta() { global $wp_version; return version_compare( $wp_version, '2.8alpha', '>' ); } /** * @since 1.0.0 * @param array $links * @param string $file * @return array */ public function meta_links( $links, $file ) { if ( $file == PLUGIN_BASENAME ) { $plugin_links = array( '' . __( 'Support', 'alopeyk-woocommerce-shipping' ) . '' ); $links = array_merge( $links, $plugin_links ); } return $links; } /** * @since 1.0.0 * @param array $links * @return array */ public function action_links( $links ) { $plugin_links[] = '' . __( 'Settings', 'alopeyk-woocommerce-shipping' ) . ''; if ( ! $this->supports_plugin_meta() ) { $meta_links = array( '' . __( 'Support', 'alopeyk-woocommerce-shipping' ) . '' ); $plugin_links = array_merge( $plugin_links, $meta_links ); } return array_merge( $plugin_links, $links ); } /** * @since 1.0.0 * @return string */ public function get_settings_url() { return admin_url( 'admin.php?page=' . $this->get_wc_settings_url() . '&tab=shipping§ion=' . METHOD_ID ); } /** * @since 1.0.0 * @param array $fields * @return array */ public function add_address_fields( $fields ) { global $post; $extra_fields = array( 'address_latitude' => array( 'label' => null, 'show' => false, 'type' => 'hidden', 'wrapper_class' => 'awcshm-hidden', ), 'address_longitude' => array( 'label' => null, 'show' => false, 'type' => 'hidden', 'wrapper_class' => 'awcshm-hidden', ), 'address_location_city' => array( 'label' => null, 'show' => false, 'type' => 'hidden', 'wrapper_class' => 'awcshm-hidden', ), 'address_location' => array( 'label' => null, 'show' => false, 'type' => 'hidden', 'wrapper_class' => 'form-field-wide', 'custom_attributes' => array( 'data-autocomplete-placeholder' => __( 'Please enter your address ...', 'alopeyk-woocommerce-shipping' ) ) ), 'address_unit' => array( 'label' => __( 'Unit', 'alopeyk-woocommerce-shipping' ), 'show' => false, ), 'address_number' => array( 'label' => __( 'Plaque', 'alopeyk-woocommerce-shipping' ), 'show' => false, ), ); return array_merge( $fields, $extra_fields ); } /** * @since 1.0.0 */ public function add_address_description_field() { if ( $this->helpers->is_enabled() ) { global $post; echo '

' . '' . '' . __( 'This will be shown on courier device if order is being sent via Alopeyk shipping method and usually consists of order value, address details or any other sort of data needed for courier to know.', 'alopeyk-woocommerce-shipping' ) . '' . '' . '

'; } } /** * @since 1.0.0 * @param integer $post_id */ public function save_address_description_field( $post_id ) { $shipping_address_description_field = '_shipping_address_description'; if ( get_post_type( $post_id ) == 'shop_order' && isset( $_POST[$shipping_address_description_field] ) ) { update_post_meta( $post_id, $shipping_address_description_field, $_POST[$shipping_address_description_field] ); } } /** * @since 1.0.0 */ public function check_address_fields() { // Check new added address fields if needed while order is being saved in admin panel } /** * @since 1.0.0 */ public function handle_meta_boxes() { global $post; if ( $post && $common = $this->helpers ) { if ( $common->is_enabled() ) { $screen = get_current_screen(); $screen = $screen->id; if ( $post->post_type == $common::$order_post_type_name ) { $post_title = $post->post_title; remove_meta_box( 'submitdiv', $screen, 'side' ); $order_data = get_post_meta( $post->ID, '_awcshm_order_data', true ); if ( $order_data ) { add_meta_box( METHOD_ID . '-order-courier-actions', __( 'Courier Information', 'alopeyk-woocommerce-shipping' ), function () use ( $order_data ) { $courier_info = isset( $order_data->courier_info ) && $order_data->courier_info ? $order_data->courier_info : null; if ( $courier_info ) { $content = get_local_template_part( 'alopeyk-woocommerce-shipping-admin-meta-courier-info', array ( 'order_data' => $order_data ) ); } else { $content = ''; } echo $content; }, $screen, 'side', 'default' ); add_meta_box( METHOD_ID . '-order-info-actions', __( 'Order Information', 'alopeyk-woocommerce-shipping' ), function () use ( $order_data ) { echo get_local_template_part( 'alopeyk-woocommerce-shipping-admin-meta-order-info', array ( 'order_data' => $order_data ) ); }, $screen, 'side', 'default' ); add_meta_box( METHOD_ID . '-order-shipping-actions', __( 'Shipping Details', 'alopeyk-woocommerce-shipping' ), function () use ( $order_data ) { echo get_local_template_part( 'alopeyk-woocommerce-shipping-admin-meta-shipping-info', array ( 'order_data' => $order_data ) ); }, $screen, 'normal', 'high' ); } else { add_action( 'admin_notices', function () { echo '

' . __( 'Order details not found.', 'alopeyk-woocommerce-shipping' ) . '

'; }); } } else if ( $post->post_type == 'shop_order' ) { $last_status = $common->get_order_history( $post->ID, array( 'posts_per_page' => 1 ) ); add_meta_box( METHOD_ID . '-wcorder-actions', __( 'Ship via Alopeyk', 'alopeyk-woocommerce-shipping' ), function () use ( $last_status ) { echo get_local_template_part( 'alopeyk-woocommerce-shipping-admin-meta-wcorder', array ( 'last_status' => $last_status ? $last_status[0] : null ) ); }, $screen, 'side', 'default' ); $args = array(); $orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : null; $order = isset( $_GET['order'] ) ? $_GET['order'] : null; if ( $order ) { $args['order'] = $order; } if ( $orderby ) { if ( in_array( $orderby, array( 'date', 'title' ) ) ) { $args['orderby'] = $orderby; } else if ( $orderby == 'customer' ) { $args['orderby'] = 'meta_value_num'; $args['meta_key'] = '_awcshm_user_id'; } else if ( $orderby == 'wc_order' ) { $args['orderby'] = 'meta_value_num'; $args['meta_key'] = '_awcshm_wc_order_id'; } else { $args['orderby'] = 'meta_value_num'; $args['meta_key'] = '_awcshm_' . $orderby; } } $last_status = $common->get_order_history( $post->ID, $args ); add_meta_box( METHOD_ID . '-wcorder-history', __( 'Alopeyk Orders History', 'alopeyk-woocommerce-shipping' ), function () use ( $last_status ) { echo get_local_template_part( 'alopeyk-woocommerce-shipping-admin-order-history', array ( 'history' => $last_status ) ); }, $screen, 'normal', 'high' ); } } } } /** * @since 1.0.0 */ public function admin_menu_items() { add_menu_page( __( 'Alopeyk', 'alopeyk-woocommerce-shipping' ), __( 'Alopeyk', 'alopeyk-woocommerce-shipping' ), 'manage_options', 'alopeyk', null, plugins_url( 'admin/img/icon.svg', dirname( __FILE__ ) ), '55.7' ); add_submenu_page( 'alopeyk', __( 'Credit', 'alopeyk-woocommerce-shipping' ), __( 'Credit', 'alopeyk-woocommerce-shipping' ), 'manage_options', 'alopeyk-credit', function () { if ( $user_data = $this->helpers->get_user_data() ) { echo get_local_template_part( 'alopeyk-woocommerce-shipping-admin-credit-page', array( 'user_data' => $user_data, 'user_credit' => $this->helpers->normalize_price( $this->helpers->get_user_data( 'credit' ) * 10 ) ) ); } else { echo '

' . sprintf( __( 'User data not found. You have to enter a valid API key in settings page in order to access this page.', 'alopeyk-woocommerce-shipping' ), $this->get_settings_url() ) . '

'; } }); add_submenu_page( 'alopeyk', __( 'Settings', 'alopeyk-woocommerce-shipping' ), __( 'Settings', 'alopeyk-woocommerce-shipping' ), 'manage_options', 'alopeyk-shipping-settings', function () { wp_redirect( $this->get_settings_url(), 301 ); exit; }); add_submenu_page( 'alopeyk', __( 'Support', 'alopeyk-woocommerce-shipping' ), __( 'Support', 'alopeyk-woocommerce-shipping' ), 'manage_options', 'alopeyk-support', function () { echo get_local_template_part( 'alopeyk-woocommerce-shipping-admin-support-page', array( 'log_url' => $this->helpers->get_log_url(), 'chat_url' => $this->helpers->get_chat_url(), 'dev_email' => $this->helpers->get_config( 'devEmail' ), 'support_tel' => $this->helpers->get_support_tel(), )); }); } /** * @since 1.0.0 * @param string $content * @return string */ public function remove_footer_content( $content ) { $screen = get_current_screen(); return strpos( $screen->id, '_page_alopeyk-support' ) ? '' : $content; } /** * @since 1.0.0 */ public function remove_admin_notices() { $screen = get_current_screen(); if ( strpos( $screen->id, '_page_alopeyk-support' ) ) { remove_all_actions( 'admin_notices' ); } } /** * @since 1.0.0 * @param array $columns * @return array */ public function add_columns( $columns = array() ) { unset( $columns[ 'date' ] ); unset( $columns['title'] ); $columns['post_status'] = __( '', 'alopeyk-woocommerce-shipping' ); $columns['order_title'] = __( 'Order', 'alopeyk-woocommerce-shipping' ); $columns['order_type'] = __( 'Type', 'alopeyk-woocommerce-shipping' ); $columns['wc_order'] = __( 'Shop Order(s)', 'alopeyk-woocommerce-shipping' ); $columns['customer'] = __( 'Customer(s)', 'alopeyk-woocommerce-shipping' ); $columns['order_price'] = __( 'Cost', 'alopeyk-woocommerce-shipping' ); $columns['order_date'] = __( 'Date', 'alopeyk-woocommerce-shipping' ); $columns['order_actions'] = __( 'Actions', 'alopeyk-woocommerce-shipping' ); return $columns; } /** * @since 1.0.0 * @param array $columns * @return array */ public function add_sortable_columns( $columns = array() ) { $columns['post_status'] = array( 'order_status', false ); $columns['order_title'] = array( 'title', false ); $columns['order_type'] = array( 'order_type', false ); $columns['customer'] = array( 'customer', false ); $columns['wc_order'] = array( 'wc_order', false ); $columns['order_price'] = array( 'order_price', false ); $columns['order_date'] = array( 'date', false ); return $columns; } /** * @since 1.0.0 * @param string $column * @param integer $post_id */ public function set_custom_column_content( $column, $post_id ) { $post = get_post( $post_id ); $post_type = $post->post_type; $post_status = $post->post_status; $post_status_label = $this->helpers->get_order_status_label( $post_status ); $alopeyk_order_id = get_post_meta( $post_id, '_awcshm_order_id' ); switch ( $column ) { case 'post_status' : echo '' . $post_status_label . ''; break; case 'order_title' : echo '#' . $post->post_title . '' . __( 'ID', 'alopeyk-woocommerce-shipping' ) . ': ' . ( $alopeyk_order_id ? $alopeyk_order_id[0] : '—' ) . ''; break; case 'order_type' : $type = get_post_meta( $post_id, '_awcshm_order_type' ); $type_label = $type ? ( $type[0] == 'motorbike' ? __( 'Motorbike', 'alopeyk-woocommerce-shipping' ) : ( $type[0] == 'cargo' ? __( 'Cargo', 'alopeyk-woocommerce-shipping' ) : $type[0] ) ) : null; echo $type_label ? '' . $type_label . '' : '—'; break; case 'customer' : $user_ids = get_post_meta( $post_id, '_awcshm_user_id' ); if ( $user_ids && count( $user_ids ) ) { $user_output = array(); foreach ( $user_ids as $user_id ) { if ( $user_id ) { $user_data = get_userdata( $user_id ); if ( $user_data ) { $user_output[] = '' . $user_data->first_name . ' ' . $user_data->last_name . ''; } } } echo implode( __( ',', 'alopeyk-woocommerce-shipping' ) . ' ', array_unique( $user_output ) ); } else { echo '—'; } break; case 'wc_order' : $order_ids = get_post_meta( $post_id, '_awcshm_wc_order_id' ); if ( $order_ids && count( $order_ids ) ) { $order_output = array(); foreach ( $order_ids as $order_id ) { $order_output[] = '#' . $order_id . ''; } echo implode( __( ',', 'alopeyk-woocommerce-shipping' ) . ' ', $order_output ); } else { echo '—'; } break; case 'order_price' : $price = get_post_meta( $post_id, '_awcshm_order_price', true ); echo $price ? wc_price( $this->helpers->normalize_price( $price ) ) : '—'; break; case 'order_date' : $timezone = get_option( 'timezone_string' ); if ( $timezone && ! empty( $timezone ) ) { date_default_timezone_set( $timezone ); } $datetime = new DateTime( get_post_time( 'Y-m-d H:i:s', false, $post_id ) ); $datetime->setTimezone( new DateTimeZone( 'Asia/Tehran' ) ); $post_date = strtotime( $datetime->format( 'Y-m-d H:i:s' ) ); echo date_i18n( 'j F Y', $post_date ) . '
' . date_i18n( 'g:i A', $post_date ); break; case 'order_actions' : $order_data = get_post_meta( $post_id, '_awcshm_order_data', true ); echo '' . __( 'View', 'alopeyk-woocommerce-shipping' ) . ''; if ( $this->helpers->can_be_tracked( $order_data ) ) { echo '' . __( 'Track', 'alopeyk-woocommerce-shipping' ) . ''; } if ( $this->helpers->can_be_invoiced( $order_data ) ) { echo '' . __( 'Invoice', 'alopeyk-woocommerce-shipping' ) . ''; } $cancel = $this->helpers->can_be_canceled( $order_data ); if ( $cancel['enabled'] ) { echo '' . __( 'Cancel', 'alopeyk-woocommerce-shipping' ) . ''; } if ( $this->helpers->can_be_rated( $order_data ) ) { echo '' . __( 'Rate', 'alopeyk-woocommerce-shipping' ) . ''; } $order_ids = get_post_meta( $post_id, '_awcshm_wc_order_id' ); if ( $this->helpers->can_be_repeated( $order_data ) && $order_ids && count( $order_ids ) ) { echo '' . __( 'Ship Again', 'alopeyk-woocommerce-shipping' ) . ''; } break; } } /** * @since 1.0.0 * @param string $current_post_type */ public function add_orders_filter( $current_post_type ) { $common = $this->helpers; $post_type = $common::$order_post_type_name; if ( $post_type == $current_post_type ) { $types = array( 'cargo' => __( 'Cargo', 'alopeyk-woocommerce-shipping' ), 'motorbike' => __( 'Motorbike', 'alopeyk-woocommerce-shipping' ) ); ?>