first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,237 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'WPDesk_Flexible_Shipping_Admin_Notices' ) ) {
class WPDesk_Flexible_Shipping_Admin_Notices {
/**
*
*/
const SETTINGS_CHECKED_OPTION_NAME = 'flexible_shipping_smsc';
/**
*
*/
const SETTINGS_CHECKED_OPTION_NAME_DISMISS = 'flexible_shipping_smsc_dismiss';
/**
*
*/
const SETTINGS_CHECKED_OPTION_VALUE_SHOW_MESSAGE = '1';
/**
*
*/
const SETTINGS_CHECKED_OPTION_VALUE_DO_NOT_SHOW_MESSAGE = '2';
/**
*
*/
const BASED_ON_VALUE = 'value';
/**
* @var Flexible_Shipping_Plugin
*/
private $plugin;
/**
* WPDesk_Flexible_Shipping_Export constructor.
*
* @param Flexible_Shipping_Plugin $plugin
*/
public function __construct( Flexible_Shipping_Plugin $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
/**
*
*/
private function hooks() {
//add_action( 'admin_notices', array( $this, 'admin_notices_plugin_versions' ) );
add_action( 'admin_notices', array( $this, 'admin_notices_plugin_activepayments' ) );
add_action( 'admin_notices', array( $this, 'admin_notices_plugin_enadawca' ) );
add_action( 'admin_notices', array( $this, 'admin_notices_plugin_pwr' ) );
add_action( 'admin_notices', array( $this, 'admin_notices_plugin_woo_fs' ) );
add_action( 'admin_notices', array( $this, 'admin_notices_taxes' ) );
add_action( 'wp_ajax_flexible_shipping_taxes_notice', array( $this, 'wp_ajax_flexible_shipping_taxes_notice' ) );
}
/**
*
*/
public function wp_ajax_flexible_shipping_taxes_notice() {
update_option( self::SETTINGS_CHECKED_OPTION_NAME_DISMISS, 1 );
}
/**
* @param WC_Shipping_Method $shipping_method
*
* @return bool
*/
private function has_value_based_rule( $shipping_method ) {
$methods = get_option( 'flexible_shipping_methods_' . $shipping_method->instance_id, array() );
if ( is_array( $methods ) ) {
foreach ( $methods as $method_settings ) {
if ( isset( $method_settings['method_rules'] ) && is_array( $method_settings['method_rules'] ) ) {
foreach ( $method_settings['method_rules'] as $rule ) {
if ( isset( $rule['based_on'] ) && $rule['based_on'] == self::BASED_ON_VALUE ) {
return true;
}
}
}
}
}
return false;
}
/**
*
*/
private function update_show_admin_notice_taxes_option() {
$has_value_based_rule = false;
$shipping_zones = WC_Shipping_Zones::get_zones();
$shipping_zones[0] = WC_Shipping_Zones::get_zone_by( 'zone_id', 0 );
foreach ( $shipping_zones as $zone_id => $shipping_zone_array ) {
$shipping_zone = WC_Shipping_Zones::get_zone( $zone_id );
/** @var WC_Shipping_Zone $shipping_zone */
$shipping_methods = $shipping_zone->get_shipping_methods();
foreach ( $shipping_methods as $shipping_method ) {
/** @var WC_Shipping_Method $shipping_method */
if ( $shipping_method->id == 'flexible_shipping' ) {
$has_value_based_rule = $has_value_based_rule || $this->has_value_based_rule( $shipping_method );
}
}
}
if ( $has_value_based_rule ) {
$shipping_methods_settings_checked = self::SETTINGS_CHECKED_OPTION_VALUE_SHOW_MESSAGE;
}
else {
$shipping_methods_settings_checked = self::SETTINGS_CHECKED_OPTION_VALUE_DO_NOT_SHOW_MESSAGE;
}
update_option( self::SETTINGS_CHECKED_OPTION_NAME, $shipping_methods_settings_checked );
}
/**
* @return bool
*/
public function is_show_admin_notice_taxes() {
$shipping_methods_settings_checked = get_option( self::SETTINGS_CHECKED_OPTION_NAME, '0' );
if ( $shipping_methods_settings_checked == '0' ) {
$this->update_show_admin_notice_taxes_option();
$shipping_methods_settings_checked = get_option( self::SETTINGS_CHECKED_OPTION_NAME, '0' );
}
return $shipping_methods_settings_checked == self::SETTINGS_CHECKED_OPTION_VALUE_SHOW_MESSAGE;
}
/**
* @return bool
*/
public function is_in_zones() {
if ( isset( $_GET['page'] ) && $_GET['page'] == 'wc-settings'
&& isset( $_GET['tab'] ) && $_GET['tab'] == 'shipping'
&& ( !isset( $_GET['section'] ) || $_GET['section'] == '' )
) {
return true;
}
return false;
}
/**
* @return bool
*/
public function is_admin_notice_taxes_dismissed() {
if ( get_option( self::SETTINGS_CHECKED_OPTION_NAME_DISMISS, '0' ) == '1' ) {
return true;
}
return false;
}
/**
*
*/
public function admin_notices_taxes() {
if ( wc_tax_enabled() && !$this->is_admin_notice_taxes_dismissed() && $this->is_show_admin_notice_taxes() ) {
$class = 'notice notice-error is-dismissible flexible-shipping-taxes-notice';
$message = sprintf(
__( 'Flexible Shipping has changed the calculation method for shipping rules. Currently, the cart value for rules based on price is determined by WooCommerce tax option "Display prices during cart and checkout". You should check the %ssettings%s.', 'flexible-shipping' ),
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=tax' ) . '">',
'</a>'
);
$this->print_notice( $class, $message );
}
}
/*
*
*/
public function admin_notices_plugin_activepayments() {
if ( is_plugin_active( 'woocommerce-active-payments/activepayments.php' ) ) {
$plugin_activepayments = get_plugin_data( WP_PLUGIN_DIR . '/woocommerce-active-payments/activepayments.php' );
$version_compare = version_compare( $plugin_activepayments['Version'], '2.7' );
if ( $version_compare < 0 ) {
$class = 'notice notice-error';
$message = __( 'Flexible Shipping requires at least version 2.7 of Active Payments plugin.', 'flexible-shipping' );
$this->print_notice( $class, $message );
}
}
}
/**
*
*/
public function admin_notices_plugin_enadawca() {
if ( is_plugin_active( 'woocommerce-enadawca/woocommerce-enadawca.php' ) ) {
$plugin_enadawca = get_plugin_data( WP_PLUGIN_DIR . '/woocommerce-enadawca/woocommerce-enadawca.php' );
$version_compare = version_compare( $plugin_enadawca['Version'], '1.2' );
if ( $version_compare < 0 ) {
$class = 'notice notice-error';
$message = __( 'Flexible Shipping requires at least version 1.2 of eNadawca plugin.', 'flexible-shipping' );
$this->print_notice( $class, $message );
}
}
}
/**
*
*/
public function admin_notices_plugin_pwr() {
if ( is_plugin_active( 'woocommerce-paczka-w-ruchu/woocommerce-paczka-w-ruchu.php' ) ) {
$plugin_pwr = get_plugin_data( WP_PLUGIN_DIR . '/woocommerce-paczka-w-ruchu/woocommerce-paczka-w-ruchu.php' );
$version_compare = version_compare( $plugin_pwr['Version'], '1.1' );
if ( $version_compare < 0 ) {
$class = 'notice notice-error';
$message = __( 'Flexible Shipping requires at least version 1.1 of Paczka w Ruchu plugin.', 'flexible-shipping' );
$this->print_notice( $class, $message );
}
}
}
/*
*
*/
public function admin_notices_plugin_woo_fs() {
if ( is_plugin_active( 'woo-flexible-shipping/flexible-shipping.php' ) ) {
$class = 'notice notice-error';
$message = sprintf( __( 'You are using WooCommerce Flexible Shipping below 1.4. Please deactivate it on %splugins page%s. Read about big changes in Flexible Shipping on %sour blog →%s', 'flexible-shipping' ), '<a href="' . admin_url('plugins.php') . '">', '</a>', '<a href="https://www.wpdesk.pl/blog/nowy-flexible-shipping/">', '</a>' );
$this->print_notice( $class, $message );
}
}
/**
* @param string $class
* @param string $message
*/
private function print_notice( $class, $message ) {
printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
}
}
}

View File

@@ -0,0 +1,140 @@
<?php
/**
* Class WPDesk_Flexible_Shipping_Add_Shipping
*/
class WPDesk_Flexible_Shipping_Add_Shipping implements \FSVendor\WPDesk\PluginBuilder\Plugin\HookablePluginDependant {
use \FSVendor\WPDesk\PluginBuilder\Plugin\PluginAccess;
/**
* Shipping added?
*
* @var bool
*/
private $shipping_added = false;
/**
* Hooks.
*/
public function hooks() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ), 20, 2 );
add_action( 'admin_init', array( $this, 'handle_add_shipping' ) );
}
/**
* Add shipping.
*
* @param string $integration Integration.
*/
private function add_shipping( $integration ) {
$class_name = apply_filters( 'flexible_shipping_shipment_class', 'WPDesk_Flexible_Shipping_Shipment_' . $integration, $integration );
if ( class_exists( $class_name ) ) {
$order = wc_get_order( $_GET['post'] );
if ( $order ) {
$order_id = $order->get_id();
$integration = $_GET['fs_add_shipping'];
// Translators: order id and integration.
$post_title = sprintf( __( 'Shipment for order %1$s, %2$s', 'flexible-shipping' ), $order_id, $integration );
$shipment_post = array(
'post_title' => $post_title,
'post_type' => 'shipment',
'post_status' => 'fs-new',
'post_parent' => $order_id
);
$shipment_id = wp_insert_post( $shipment_post );
update_post_meta( $shipment_id, '_integration', $integration );
$shipment = fs_get_shipment( $shipment_id, $order );
$shipment->set_created_via_add_shipment();
if ( method_exists( $shipment, 'admin_add_shipment' ) ) {
$shipment->admin_add_shipment();
}
$shipment->save();
$order->add_order_note( sprintf( __( 'Added new shipment via metabox. Shipment ID: %s', 'flexible-shipping' ), $shipment->get_id() ) );
$this->shipping_added = true;
}
}
}
/**
* Handle add shipping.
*/
public function handle_add_shipping() {
if ( isset( $_GET['fs_add_shipping'] ) && isset( $_GET['post'] ) ) {
if ( isset( $_GET['_wpnonce'] ) ) {
if ( wp_verify_nonce( $_GET['_wpnonce'], 'fs_add_shipping' ) ) {
$integration = $_GET['fs_add_shipping'];
$this->add_shipping( $integration );
}
}
}
}
/**
* Add metabox.
*
* @param string $post_type Post type.
* @param WP_Post $post Post.
*/
public function add_meta_box( $post_type, $post ) {
if ( 'shop_order' === $post_type ) {
$add_metabox = false;
$order = wc_get_order( $post->ID );
$created_via = $order->get_created_via();
if ( 'checkout' !== $created_via ) {
$add_metabox = true;
}
if ( ! $add_metabox ) {
$order_shipping_methods = $order->get_shipping_methods();
$all_shipping_methods = flexible_shipping_get_all_shipping_methods();
$flexible_shipping = $all_shipping_methods['flexible_shipping'];
$flexible_shipping_rates = $flexible_shipping->get_all_rates();
foreach ( $order_shipping_methods as $order_shipping_method ) {
/** @var WC_Order_Item_Shipping $order_shipping_method */
$fs_method = $order_shipping_method->get_meta( '_fs_method' );
if ( ! empty( $fs_method ) && isset( $flexible_shipping_rates[ $fs_method['id_for_shipping'] ] ) ) {
$add_metabox = true;
}
}
}
$select_options = array();
$select_options = apply_filters( 'flexible_shipping_add_shipping_options', $select_options );
if ( $add_metabox && count( $select_options ) ) {
$select_options = array_merge(
array( '' => __( 'Select integration', 'flexible-shipping' ) ),
$select_options
);
$args = array(
'select_options' => $select_options,
'order_id' => $post->ID,
);
add_meta_box(
'add_shipping_meta_box',
__( 'Add shipping', 'flexible-shipping' ),
array( $this, 'display_order_metabox' ),
'shop_order',
'side',
'default',
$args
);
}
}
}
/**
* Display order metabox.
*
* @param WP_Post $post Post.
* @param array $args Args.
*/
public function display_order_metabox( $post, $args ) {
$select_options = $args['args']['select_options'];
$order_id = $args['args']['order_id'];
$add_shipping_url = admin_url( 'post.php?post=' . $order_id . '&action=edit' );
$add_shipping_url = wp_nonce_url( $add_shipping_url, 'fs_add_shipping' );
$add_shipping_url = str_replace( '&amp;', '&', $add_shipping_url );
include 'views/html-order-add_shipping-metabox.php';
}
}

View File

@@ -0,0 +1,7 @@
<?php
/**
* Class WPDesk_Flexible_Shipping_Send_Shipment_Exception
*/
class WPDesk_Flexible_Shipping_Cancel_Shipment_Exception extends RuntimeException {
}

View File

@@ -0,0 +1,7 @@
<?php
/**
* Class WPDesk_Flexible_Shipping_Send_Shipment_Exception
*/
class WPDesk_Flexible_Shipping_Get_Label_Exception extends RuntimeException {
}

View File

@@ -0,0 +1,7 @@
<?php
/**
* Class WPDesk_Flexible_Shipping_Send_Shipment_Exception
*/
class WPDesk_Flexible_Shipping_Label_Not_Available_Exception extends RuntimeException {
}

View File

@@ -0,0 +1,7 @@
<?php
/**
* Class WPDesk_Flexible_Shipping_Send_Shipment_Exception
*/
class WPDesk_Flexible_Shipping_Send_Shipment_Exception extends RuntimeException {
}

View File

@@ -0,0 +1,7 @@
<?php
/**
* Class WPDesk_Flexible_Shipping_Send_Shipment_Exception
*/
class WPDesk_Flexible_Shipping_Shipment_Plan_Exceeded_Exception extends RuntimeException {
}

View File

@@ -0,0 +1,42 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WPDesk_Flexible_Shipping_Manifest_FS extends WPDesk_Flexible_Shipping_Manifest implements WPDesk_Flexible_Shipping_Manifest_Interface {
/**
* @return array
* Returns manifest data in array
* file_name => file name for manifest
* content => pdf content
*/
public function get_manifest() {
return null;
}
/**
* @return string
* Returns manifest number
*/
public function get_number() {
return null;
}
/**
* @return null
* Generates manifest (ie. in API)
*/
public function generate() {
return null;
}
/**
* @return null
* Cancels manifest (ie. in API)
*/
public function cancel() {
return null;
}
}

View File

@@ -0,0 +1,214 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_Flexible_Shipping_Manifest' ) ) {
/**
* Class WPDesk_Flexible_Shipping_Shipment
*/
abstract class WPDesk_Flexible_Shipping_Manifest {
/**
* @var int
*/
private $id;
/**
* @var WP_Post
* Post assigned to manifest
*/
private $post;
/**
* @var bool
* True if assigned post ich changed. Used when saving post
*/
private $save_post = false;
/**
* @var null
* Holds old status when manifest status is changed
*/
private $old_status = null;
/**
* @var bool
* True when status changing
*/
private $status_changed = false;
/**
* @var array
* Manifest metadata (from postmeta table)
*/
private $meta_data = array();
/**
* @var bool
* True when manifest metadata loaded
*/
private $meta_data_loaded = false;
/**
* @var array
* Holds changed metadata keys. Used when saving manifest
*/
private $meta_data_save_keys = array();
/**
* WPDesk_Flexible_Shipping_Manifest constructor.
*
* @param int|WPDesk_Flexible_Shipping_Manifest|WP_Post $manifest Manifest.
* @param WC_Order|null $order Order.
*/
public function __construct( $manifest, $order = null ) {
if ( is_numeric( $manifest ) ) {
$this->id = absint( $manifest );
$this->post = get_post( $this->id );
} elseif ( $manifest instanceof WPDesk_Flexible_Shipping_Manifest ) {
$this->id = absint( $manifest->get_id() );
$this->post = $manifest->get_post();
} elseif ( isset( $manifest->ID ) ) {
$this->id = absint( $manifest->ID );
$this->post = $manifest;
}
$this->order = $order;
}
/**
* @return mixed
*/
public function get_id() {
return $this->id;
}
/**
* @return mixed
*/
public function get_post() {
return $this->post;
}
/**
* @param string $meta_key
* @param null|string $default
* @return array|string|null
*/
public function get_meta( $meta_key = '', $default = null ) {
$this->load_meta_data();
if ( $meta_key == '' ) {
return $this->meta_data;
}
if ( isset( $this->meta_data[$meta_key] ) ) {
return maybe_unserialize( $this->meta_data[$meta_key][0] );
}
else {
return $default;
}
}
/**
* @param string $meta_key
* @param int|string|array|object|null $value
*/
public function set_meta( $meta_key, $value ) {
$this->load_meta_data();
if ( !isset( $this->meta_data[$meta_key] ) ) {
$this->meta_data[$meta_key] = array();
}
$this->meta_data[$meta_key][0] = $value;
$this->meta_data_save_keys[$meta_key] = $meta_key;
}
/**
* @param string $meta_key
*/
public function delete_meta( $meta_key ) {
unset( $this->meta_data[$meta_key] );
$this->meta_data_save_keys[$meta_key] = $meta_key;
}
/**
* Saves manifest data to database.
*/
public function save() {
if ( $this->save_post ) {
wp_update_post($this->post);
$this->save_post = false;
}
foreach ( $this->meta_data_save_keys as $key ) {
if ( isset( $this->meta_data[$key] ) ) {
update_post_meta( $this->id, $key, $this->meta_data[$key][0] );
}
else {
delete_post_meta( $this->id, $key );
}
unset( $this->meta_data_save_keys[$key] );
}
if ( $this->status_changed ) {
do_action( 'flexible_shipping_manifest_status_updated', $this->old_status, $this->post->post_status, $this );
$this->status_changed = false;
$this->old_status = null;
}
}
/**
* Loads all meta data from postmeta
*/
public function load_meta_data() {
if ( !$this->meta_data_loaded ) {
$this->meta_data = get_post_meta( $this->id );
$this->meta_data_loaded = true;
}
}
/**
* @return array|null
* Returns integration assigned to manifest
*/
public function get_integration() {
return $this->get_meta( '_integration' );
}
/**
* @return string
*/
public function get_status() {
return $this->post->post_status;
}
public function get_date() {
return $this->post->post_date;
}
/**
* @param string $new_status
*/
public function update_status( $new_status ) {
$this->old_status = $this->post->post_status;
$this->post->post_status = $new_status;
$this->save_post = true;
$this->status_changed = true;
}
/**
* @param mixed $shipments
*/
public function add_shipments( $shipments ) {
if ( !is_array( $shipments ) ) {
$shipments = array( $shipments );
}
$shipments_ids = $this->get_meta( '_shipments', array() );
foreach ( $shipments as $shipment ) {
/* @var WPDesk_Flexible_Shipping_Shipment $shipment */
$shipment->add_to_manifest( $this );
$shipment->save();
$shipments_ids[] = $shipment->get_id();
}
$this->set_meta( '_shipments', $shipments_ids );
$this->save();
}
}
}

View File

@@ -0,0 +1,328 @@
<?php
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WPDesk_Flexible_Shipping_Shipping_Manifest_CPT {
private $plugin = null;
public function __construct( Flexible_Shipping_Plugin $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
public function hooks() {
add_action( 'init', array( $this, 'register_post_types' ), 20 );
add_action( 'admin_init', array( $this, 'cancel_manifest' ), 20 );
add_action( 'admin_init', array( $this, 'download_manifest' ), 20 );
add_action( 'admin_menu', array( $this, 'admin_menu' ), 199 );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20, 2 );
add_action( 'trash_shipping_manifest', array( $this, 'trash_shipping_manifest' ) );
add_filter( 'manage_edit-shipping_manifest_columns', array( $this, 'manage_edit_shipping_manifest_columns' ), 11 );
add_action( 'manage_shipping_manifest_posts_custom_column', array(
$this,
'manage_shipping_manifest_posts_custom_column'
), 11 );
add_filter( 'post_row_actions', array( $this, 'shipping_manifest_row_actions' ), 10, 2 ) ;
add_action( 'do_meta_boxes', array( $this, 'hide_publish_metabox' ) );
add_filter( 'woocommerce_screen_ids', array( $this, 'woocommerce_screen_ids' ) );
add_filter( 'bulk_actions-edit-shipping_manifest', array( $this, 'bulk_actions_edit_shipping_manifest' ) );
add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ), 9999 );
add_filter( 'parse_query', array( $this, 'parse_query' ), 999 );
}
/**
* Register post types.
*/
public function register_post_types() {
if ( post_type_exists( 'shipping_manifest' ) ) {
return;
}
register_post_type( 'shipping_manifest',
array(
'labels' => array(
'name' => __( 'Shipping Manifests', 'flexible-shipping' ),
'singular_name' => __( 'Shipping Manifest', 'flexible-shipping' ),
'menu_name' => __( 'Shipping Manifests', 'flexible-shipping' ),
'parent_item_colon' => '',
'all_items' => __( 'Shipping Manifests', 'flexible-shipping' ),
'view_item' => __( 'View Shipping Manifests', 'flexible-shipping' ),
'add_new_item' => __( 'Add new Shipping Manifest', 'flexible-shipping' ),
'add_new' => __( 'Add new Shipping Manifests', 'flexible-shipping' ),
'edit_item' => __( 'Edit Shipping Manifest', 'flexible-shipping' ),
'update_item' => __( 'Save Shipping Manifest', 'flexible-shipping' ),
'search_items' => __( 'Search Shipping Manifests', 'flexible-shipping' ),
'not_found' => __( 'Shipping Manifests not found', 'flexible-shipping' ),
'not_found_in_trash' => __( 'Shipping Manifests not found in trash', 'flexible-shipping' )
),
'description' => __( 'Shipping Manifests.', 'flexible-shipping' ),
'public' => false,
'show_ui' => true,
'capability_type' => 'post',
'capabilities' => array( 'create_posts' => false ),
'map_meta_cap' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'query_var' => true,
'supports' => array( 'title' ),
'has_archive' => false,
'show_in_nav_menus' => false,
'show_in_menu' => 'edit.php?post_type=shop_order',
'menu_icon' => 'dashicons-upload',
)
);
}
public function admin_menu() {
$show_in_menu = current_user_can( 'manage_woocommerce' ) ? 'woocommerce' : false;
if ( apply_filters( 'flexible_shipping_has_manifests', false ) ) {
$slug = add_submenu_page( $show_in_menu, __( 'Shipping Manifests', 'flexible-shipping' ), __( 'Shipping Manifests', 'flexible-shipping' ), 'manage_woocommerce', 'edit.php?post_type=shipping_manifest' );
}
}
public function add_meta_boxes( $post_type, $post ) {
if ( $post_type == 'shipping_manifest' ) {
/*
add_meta_box(
'shipping_manifest_meta_box',
__('Shipping manifest data', 'flexible-shipping'),
array( $this, 'metabox' ),
'shipping_manifest',
'normal',
'high'
);
*/
add_meta_box(
'shipping_manifest_shipments',
__('Shipments', 'flexible-shipping'),
array( $this, 'shipments_metabox' ),
'shipping_manifest',
'normal',
'high'
);
}
}
public function metabox() {
global $post;
echo '<pre>';
print_r( $post );
echo '</pre>';
$meta_data = get_post_meta( $post->ID );
foreach ( $meta_data as $key => $val ) {
echo '<pre>';
echo $key;
echo ' = ';
print_r( maybe_unserialize( $val[0] ) );
echo '</pre>';
}
}
public function shipments_metabox() {
global $post;
$manifest = fs_get_manifest( $post->ID );
$shipments_array = $manifest->get_meta( '_shipments', array() );
$shipments = array();
foreach ( $shipments_array as $shipment_id ) {
$shipments[] = fs_get_shipment( $shipment_id );
}
include( 'views/manifest-metabox.php' );
/*
echo "<pre>";
print_r($shipments);
echo "</pre>";
*/
}
public function manage_edit_shipping_manifest_columns( $columns ) {
unset( $columns['title'] );
unset( $columns['date'] );
unset( $columns['cb'] );
$columns['manifest_date'] = __( 'Date', 'flexible-shipping' );
$columns['integration'] = __( 'Integration', 'flexible-shipping' );
$columns['external_number'] = __( 'Number', 'flexible-shipping' );
$columns['shipment_count'] = __( 'Shipments count', 'flexible-shipping' );
$columns['actions'] = __( 'Actions', 'flexible-shipping' );
return $columns;
}
public function shipping_manifest_row_actions( $actions, $post ) {
if ( $post->post_type == 'shipping_manifest' ) {
$actions = array();
}
return $actions;
}
public function manage_shipping_manifest_posts_custom_column( $column ) {
global $post;
global $manifest;
$integrations = apply_filters( 'flexible_shipping_integration_options', array() );
if ( empty( $manifest ) || $manifest->get_id() != $post->ID ) {
$manifest = fs_get_manifest( $post->ID );
}
if ( $column == 'manifest_date' ) {
echo $manifest->get_date();
}
if ( $column == 'integration' ) {
echo $integrations[$manifest->get_integration()];
}
if ( $column == 'external_number' ) {
$download_manifest_url = admin_url('edit.php?post_type=shipping_manifest&flexible_shipping_download_manifest=' . $manifest->get_id() . '&nonce=' . wp_create_nonce('flexible_shipping_download_manifest'));
include( 'views/column-number.php' );
}
if ( $column == 'shipment_count' ) {
echo count( $manifest->get_meta( '_shipments', array() ) );
}
if ( $column == 'actions' ) {
if ( $manifest->get_status() != 'trash' ) {
$download_manifest_url = admin_url('edit.php?post_type=shipping_manifest&flexible_shipping_download_manifest=' . $manifest->get_id() . '&nonce=' . wp_create_nonce('flexible_shipping_download_manifest'));
$cancel_url = admin_url('edit.php?post_type=shipping_manifest&flexible_shipping_cancel_manifest=' . $manifest->get_id() . '&nonce=' . wp_create_nonce('flexible_shipping_cancel_manifest'));
include( 'views/column-actions.php' );
}
}
}
public function woocommerce_screen_ids( $screen_ids ) {
$screen_ids[] = 'edit-shipping_manifest';
$screen_ids[] = 'shipping_manifest';
return $screen_ids;
}
public function bulk_actions_edit_shipping_manifest( $bulk_actions ) {
$bulk_actions = array();
return $bulk_actions;
}
public function cancel_manifest() {
if ( !empty( $_GET['flexible_shipping_cancel_manifest'] ) && !empty( $_GET['nonce'] ) ) {
$nonce = $_GET['nonce'];
if ( !wp_verify_nonce( $nonce, 'flexible_shipping_cancel_manifest' ) ) {
echo __( 'Invalid nonce!', 'flexible-shipping' );
exit;
}
$sendback = admin_url( 'edit.php?post_type=shipping_manifest' );
try {
$shipping_manifest_id = $_GET['flexible_shipping_cancel_manifest'];
$shipping_manifest = fs_get_manifest( $shipping_manifest_id );
$shipping_manifest->cancel();
fs_delete_manifest( $shipping_manifest );
wp_redirect( $sendback );
exit();
}
catch ( Exception $e ) {
wp_redirect( $sendback );
exit();
}
}
}
public function download_manifest() {
if ( !empty( $_GET['flexible_shipping_download_manifest'] ) && !empty( $_GET['nonce'] ) ) {
$nonce = $_GET['nonce'];
if ( !wp_verify_nonce( $nonce, 'flexible_shipping_download_manifest' ) ) {
echo __( 'Invalid nonce!', 'flexible-shipping' );
}
try {
$shipping_manifest_id = $_GET['flexible_shipping_download_manifest'];
$shipping_manifest = fs_get_manifest( $shipping_manifest_id );
$manifest = $shipping_manifest->get_manifest();
header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=" . $manifest['file_name'] );
echo $manifest['content'];
}
catch ( Exception $e ) {
echo $e->getMessage();
}
exit();
}
}
public function hide_publish_metabox() {
remove_meta_box( 'submitdiv', 'shipping_manifest', 'side' );
}
public function trash_shipping_manifest( $post_id ) {
$manifest = fs_get_manifest( $post_id );
$shipments_posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'shipment',
'post_status' => 'any',
'meta_key' => '_manifest',
'meta_value' => $post_id
));
foreach ( $shipments_posts as $shipment_post ) {
$shipment = fs_get_shipment( $shipment_post->ID );
$shipment->delete_meta( '_manifest' );
$shipment->update_status('fs-confirmed' );
$shipment->save();
}
$manifest->delete_meta( '_shipments' );
$manifest->save();
}
public function restrict_manage_posts() {
global $typenow;
if ( 'shipping_manifest' == $typenow ){
$integrations = apply_filters( 'flexible_shipping_integration_options', array() );
foreach ( $integrations as $key => $integration ) {
if ( !class_exists( 'WPDesk_Flexible_Shipping_Manifest_' . $key ) ) {
unset( $integrations[$key] );
}
}
$integration = '';
if ( isset( $_GET['flexible_shipping_integration_filter'] ) ) {
$integration = $_GET['flexible_shipping_integration_filter'];
}
include( 'views/filter-form.php' );
}
}
public function parse_query( $query ) {
global $pagenow;
$type = 'shipping_manifest';
if ( isset( $_GET['post_type'] ) ) {
$type = $_GET['post_type'];
}
if ( isset( $query->query_vars['post_type'] ) && $query->query_vars['post_type'] == 'shipping_manifest' ) {
if ( 'shipping_manifest' == $type && is_admin() && 'edit.php' == $pagenow ) {
$integration = '';
if ( isset( $_GET['flexible_shipping_integration_filter'] ) ) {
$integration = $_GET['flexible_shipping_integration_filter'];
}
if ( $integration != '' ) {
if ($integration != '') {
if (!isset($query->query_vars['meta_query'])) {
$query->query_vars['meta_query'] = array();
}
$meta_query = array();
$meta_query['key'] = '_integration';
$meta_query['value'] = $integration;
$query->query_vars['meta_query'][] = $meta_query;
}
}
}
}
}
}

View File

@@ -0,0 +1,62 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
function fs_manifest_integration_exists( $integration ) {
$class_name = 'WPDesk_Flexible_Shipping_Manifest' . '_' . $integration ;
if ( class_exists( $class_name ) ) {
return true;
}
return false;
}
function fs_create_manifest( $integration ) {
$post_title = sprintf( __( 'Shipping manifest %s, %s', 'flexible-shipping' ), $integration, date_i18n( get_option( 'date_format' ) ) );
$post_title = apply_filters( 'flexible_shipping_manifest_post_title_'. $integration, $post_title );
$manifest_post = array(
'post_title' => $post_title,
'post_type' => 'shipping_manifest',
'post_status' => 'publish',
);
$manifest_id = wp_insert_post( $manifest_post );
update_post_meta( $manifest_id, '_integration', $integration );
return fs_get_manifest( $manifest_id );
}
/**
* @param $manifest_id
* @return WPDesk_Flexible_Shipping_Manifest
*/
function fs_get_manifest( $manifest_id ) {
$integration = get_post_meta( $manifest_id, '_integration', true );
$class_name = 'WPDesk_Flexible_Shipping_Manifest';
if ( class_exists( $class_name . '_' . $integration ) ) {
$class_name = $class_name . '_' . $integration;
}
else {
$class_name = 'WPDesk_Flexible_Shipping_Manifest_FS';
}
return new $class_name( $manifest_id );
}
function fs_delete_manifest( $manifest ) {
$shipments_posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'shipment',
'post_status' => 'any',
'meta_key' => '_manifest',
'meta_value' => $manifest->get_id()
) );
foreach ( $shipments_posts as $shipment_post ) {
$shipment = fs_get_shipment( $shipment_post->ID );
$shipment->delete_meta( '_manifest' );
$shipment->update_status('fs-confirmed' );
$shipment->save();
}
$manifest->set_meta( '_shipments', array() );
$manifest->update_status( 'trash' );
$manifest->save();
}

View File

@@ -0,0 +1,37 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! interface_exists( 'WPDesk_Flexible_Shipping_Manifest_Interface' ) ) {
interface WPDesk_Flexible_Shipping_Manifest_Interface {
/**
* @return array
* Returns manifest data in array
* file_name => file name for manifest
* content => pdf content
*/
public function get_manifest();
/**
* @return string
* Returns manifest number
*/
public function get_number();
/**
* @return null
* Generates manifest (ie. in API)
*/
public function generate();
/**
* @return null
* Cancels manifest (ie. in API)
*/
public function cancel();
}
}

View File

@@ -0,0 +1,2 @@
<a class="button button-primary" target="_blank" href="<?php echo $download_manifest_url; ?>"><?php _e( 'Download', 'flexible-shipping' ); ?></a>
<a class="button" href="<?php echo $cancel_url; ?>"><?php _e( 'Cancel', 'flexible-shipping' ); ?></a>

View File

@@ -0,0 +1 @@
<a target="_blank" href="<?php echo $download_manifest_url; ?>"><?php echo $manifest->get_number(); ?></a>

View File

@@ -0,0 +1,11 @@
<?php ?>
<div class="alignleft actions">
<select name="flexible_shipping_integration_filter">
<option value=""><?php _e( 'All manifests', 'flexible-shipping' ); ?></option>
<optgroup label="<?php _e( 'Integration', 'flexible-shipping' ); ?>">
<?php foreach ( $integrations as $key => $val ) : ?>
<option value="<?php echo $key; ?>" <?php echo ($key == $integration ? 'selected' : '' ); ?>><?php echo $val; ?></option>
<?php endforeach; ?>
</optgroup>
</select>
</div>

View File

@@ -0,0 +1,55 @@
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>
</th>
<th>
<?php _e( 'Shipment', 'flexible-shipping' ); ?>
</th>
<th>
<?php _e( 'Order', 'flexible-shipping' ); ?>
</th>
</tr>
</thead>
<tbody id="the-list">
<?php $count = 0; ?>
<?php foreach ( $shipments as $shipment ) : ?>
<?php
$count++;
$order = $shipment->get_order();
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$order_id = $order->id;
}
else {
$order_id = $order->get_id();
}
?>
<tr>
<td>
<?php echo $count; ?>
</td>
<td>
<a href="<?php echo $shipment->get_order_metabox_url(); ?>"><?php echo $shipment->get_tracking_number(); ?></a>
</td>
<td>
<a href="<?php echo admin_url( 'post.php?action=edit&post=' . $order_id ); ?>"><?php echo $order->get_order_number(); ?></a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<th>
</th>
<th>
<?php _e( 'Shipment', 'flexible-shipping' ); ?>
</th>
<th>
<?php _e( 'Order', 'flexible-shipping' ); ?>
</th>
</tr>
</tfoot>
</table>

View File

@@ -0,0 +1,74 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_Flexible_Shipping_Shipment_Ajax' ) ) {
class WPDesk_Flexible_Shipping_Shipment_Ajax {
private $plugin = null;
public function __construct( Flexible_Shipping_Plugin $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
public function hooks() {
add_action( 'wp_ajax_flexible_shipping', array( $this, 'wp_ajax_flexible_shipping' ) );
}
public function wp_ajax_flexible_shipping() {
$json = array('status' => 'fail');
$json['message'] = __( 'Unknown error!', 'flexible-shipping' );
if ( empty( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'flexible_shipping_shipment_nonce' ) ) {
$json['status'] = 'fail';
$json['message'] = __( 'Nonce verification error! Invalid request.', 'flexible-shipping' );
}
else if ( empty( $_REQUEST['shipment_id'] ) ) {
$json['status'] = 'fail';
$json['message'] = __( 'No shipment id!', 'flexible-shipping' );
}
else if ( empty( $_REQUEST['data'] ) || !is_array( $_REQUEST['data'] ) ) {
$json['status'] = 'fail';
$json['message'] = __( 'No data!', 'flexible-shipping' );
}
else {
$shipment = fs_get_shipment( intval( $_REQUEST['shipment_id'] ) );
$action = $_REQUEST['fs_action'];
$data = $_REQUEST['data'];
try {
$ajax_request = $shipment->ajax_request( $action, $data );
if ( is_array( $ajax_request ) ) {
$json['content'] = $ajax_request['content'];
$json['message'] = '';
if ( isset( $ajax_request['message'] ) ) {
$json['message'] = $ajax_request['message'];
}
}
else {
$json['content'] = $ajax_request;
$json['message'] = '';
if ( $action == 'save' ) {
$json['message'] = __( 'Saved', 'flexible-shipping' );
}
if ( $action == 'send' ) {
$json['message'] = __( 'Created', 'flexible-shipping' );
}
}
$json['status'] = 'success';
if ( ! empty( $ajax_request['status'] ) ) {
$json['status'] = $ajax_request['status'];
}
}
catch ( Exception $e ) {
$json['status'] = 'fail';
$json['message'] = $e->getMessage();
}
}
echo json_encode($json);
die;
}
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace WPDesk\FS\Rate;
/**
* Display rate notice.
*/
class WPDesk_Flexible_Shipping_Rate_Notice implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable {
const FIRST_NOTICE_MIN_ORDERS = 100;
const CLOSE_TEMPORARY_NOTICE_NUMBER = 'close-temporary-notice-number';
const CLOSE_TEMPORARY_NOTICE_DATE = 'close-temporary-notice-date';
const CLOSE_ALREADY_DID = 'already-did';
const SETTINGS_OPTION_DISMISSED_COUNT = 'flexible_shipping_rate_dismissed_count';
const SETTINGS_RATE_NOTICE_VARIANT_ID = 'flexible_shipping_rate_notice_variant_id';
const SETTINGS_OPTION_RATE_NOTICE_DATE_DISMISS = 'flexible_shipping_rate_notice_date_dismiss';
const SETTINGS_OPTION_RATE_NOTICE_NUMBER_DISMISS = 'flexible_shipping_rate_notice_number_dismiss';
/**
* Hooks.
*/
public function hooks() {
add_action( 'admin_notices', array( $this, 'add_admin_notice_action' ) );
add_action( 'wpdesk_notice_dismissed_notice', array( $this, 'reset_rate_variant_action' ), 10, 2 );
add_action( 'wp_ajax_flexible_shipping_rate_notice', array( $this, 'wp_ajax_flexible_shipping_rate_notice' ) );
add_action( 'wp_ajax_flexible_shipping_close_rate_notice', array( $this, 'wp_ajax_flexible_shipping_close_rate_notice' ) );
}
/**
* Reset rate variant
*
* @param string $notice_name Notice name.
* @param string $source Sorcue.
*/
public function reset_rate_variant_action( $notice_name, $source ) {
$variant_id = get_option( self::SETTINGS_RATE_NOTICE_VARIANT_ID );
if ( 'flexible_shipping_rate_plugin' !== $notice_name ) {
return false;
}
$dismissed_count = (int) get_option( self::SETTINGS_OPTION_DISMISSED_COUNT, 0 );
if ( ( empty( $source ) || self::CLOSE_TEMPORARY_NOTICE_DATE === $source ) && ( $variant_id === FirstRateNotice::SETTINGS_VARIANT_ID || $variant_id === SecondRateNotice::SETTINGS_VARIANT_ID ) ) {
update_option( self::SETTINGS_OPTION_RATE_NOTICE_DATE_DISMISS, date( "Y-m-d H:i:s", strtotime( 'NOW + 2 weeks' ) ) );
delete_option( \FSVendor\WPDesk\Notice\PermanentDismissibleNotice::OPTION_NAME_PREFIX . $notice_name );
update_option( self::SETTINGS_OPTION_DISMISSED_COUNT, 1 );
} elseif ( ( empty( $source ) || self::CLOSE_TEMPORARY_NOTICE_NUMBER === $source ) && $variant_id === ThirdRateNotice::SETTINGS_VARIANT_ID ) {
update_option( Flexible_Shipping_Order_Counter::FS_ORDER_COUNTER, 0 );
delete_option( \FSVendor\WPDesk\Notice\PermanentDismissibleNotice::OPTION_NAME_PREFIX . $notice_name );
update_option( self::SETTINGS_OPTION_DISMISSED_COUNT, 1 );
} elseif ( self::CLOSE_ALREADY_DID === $source ) {
update_option( \FSVendor\WPDesk\Notice\PermanentDismissibleNotice::OPTION_NAME_PREFIX . $notice_name, 1 );
}
if ( $dismissed_count > 0 ) {
update_option( \FSVendor\WPDesk\Notice\PermanentDismissibleNotice::OPTION_NAME_PREFIX . $notice_name, 1 );
}
}
/**
* Should display notice.
*
* @return bool
*/
private function should_display_notice() {
$current_screen = get_current_screen();
$display_on_screens = [ 'shop_order', 'edit-shop_order', 'woocommerce_page_wc-settings' ];
if ( ! empty( $current_screen ) && in_array( $current_screen->id, $display_on_screens, true ) ) {
return true;
}
return false;
}
/**
* Generate rate notice variant ID
*
* @return string
*/
private function generate_rate_notice_variant_id()
{
$variant = get_option(self::SETTINGS_RATE_NOTICE_VARIANT_ID, '0');
if ( $variant === '0' ) {
$variant = 'notice_' . mt_rand(1, 3);
add_option( self::SETTINGS_RATE_NOTICE_VARIANT_ID, $variant );
$this->set_notice_defaults( $variant );
}
return $variant;
}
/**
* Set defaults for notice
*
* @param string $variant Variant ID.
*/
private function set_notice_defaults( $variant ) {
if( 'notice_3' !== $variant ) {
add_option( self::SETTINGS_OPTION_RATE_NOTICE_DATE_DISMISS, date( "Y-m-d H:i:s", strtotime('NOW + 2 weeks') ) );
} else {
add_option( Flexible_Shipping_Order_Counter::FS_ORDER_COUNTER, 0 );
}
}
/**
* Add admin notice.
*/
public function add_admin_notice_action()
{
$variant = $this->generate_rate_notice_variant_id();
if ( $this->should_display_notice() ) {
$creator = new \WPDesk\FS\Rate\RateNoticeCreator();
$instance = $creator->create( $variant );
if( $instance->should_show_message() ) {
$instance->show_message();
}
}
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace WPDesk\FS\Rate;
abstract class RateNotice implements RateNoticeInterface {
const NOTICE_NAME = 'flexible_shipping_rate_plugin';
const CLOSE_TEMPORARY_NOTICE = 'close-temporary-notice-date';
/**
* Get message
*
* @return mixed
*/
abstract protected function get_message();
/**
* Action links
*
* @return array
*/
protected function action_links() {
$actions[] = sprintf(
__( '%1$sOk, you deserved it%2$s', 'flexible-shipping' ),
'<a target="_blank" href="' . esc_url( 'https://wpde.sk/fs-rate' ) . '">',
'</a>'
);
$actions[] = sprintf(
__( '%1$sNope, maybe later%2$s', 'flexible-shipping' ),
'<a data-type="date" class="fs-close-temporary-notice notice-dismiss-link" data-source="' . self::CLOSE_TEMPORARY_NOTICE . '" href="#">',
'</a>'
);
$actions[] = sprintf(
__( '%1$sI already did%2$s', 'flexible-shipping' ),
'<a class="close-rate-notice notice-dismiss-link" data-source="already-did" href="#">',
'</a>'
);
return $actions;
}
/**
* Should show message
*
* @return bool
*/
public function should_show_message() {
return true;
}
/**
* Show admin notice
*
* @return string|void
*/
public function show_message() {
new \FSVendor\WPDesk\Notice\PermanentDismissibleNotice(
$this->get_message(),
self::NOTICE_NAME,
\FSVendor\WPDesk\Notice\Notice::NOTICE_TYPE_INFO,
10,
array(
'class' => self::NOTICE_NAME,
'id' => self::NOTICE_NAME,
)
);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace WPDesk\FS\Rate;
class FirstRateNotice extends RateNotice
{
const SETTINGS_VARIANT_ID = 'notice_1';
/**
* Action links
*
* @return array
*/
protected function action_links() {
$actions[] = sprintf(
__( '%1$sOk, you deserved it%2$s', 'flexible-shipping' ),
'<a target="_blank" href="' . esc_url( 'https://wpde.sk/fs-rate-1' ) . '">',
'</a>'
);
$actions[] = sprintf(
__( '%1$sNope, maybe later%2$s', 'flexible-shipping' ),
'<a data-type="date" class="fs-close-temporary-notice notice-dismiss-link" data-source="' . self::CLOSE_TEMPORARY_NOTICE . '" href="#">',
'</a>'
);
$actions[] = sprintf(
__( '%1$sI already did%2$s', 'flexible-shipping' ),
'<a class="close-rate-notice notice-dismiss-link" data-source="already-did" href="#">',
'</a>'
);
return $actions;
}
/**
* Should show message
*
* @return bool
*/
public function should_show_message() {
$notice_date_dissmis = get_option( WPDesk_Flexible_Shipping_Rate_Notice::SETTINGS_OPTION_RATE_NOTICE_DATE_DISMISS, date( "Y-m-d H:i:s", strtotime( 'NOW + 2 weeks' ) ) );
$notice_date = strtotime( $notice_date_dissmis );
$current_date = strtotime( 'NOW' );
$difference = $current_date - $notice_date;
$days = (int) floor( $difference / ( 60 * 60 * 24 ) );
if ( $days > 0 ) {
return true;
}
return false;
}
/**
* Get rate message
*
* @return string
*/
protected function get_message() {
$message = __( 'Awesome, you\'ve been using Flexible Shipping for more than 2 weeks. May I ask you to give it a 5-star rating on WordPress?', 'flexible-shipping' );
$message .= '<br/>';
$message .= implode( ' | ', $this->action_links() );
return $message;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace WPDesk\FS\Rate;
interface RateNoticeInterface {
/**
* Show message
*
* @return string
*/
public function show_message();
/**
* Should show message
*
* @return bool
*/
public function should_show_message();
}

View File

@@ -0,0 +1,53 @@
<?php
namespace WPDesk\FS\Rate;
/**
* Counts orders with FS shipping method.
*/
class Flexible_Shipping_Order_Counter implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable {
const ORDER_STATUS_COMPLETED = 'completed';
const FS_ORDER_COUNTER = 'flexible_shipping_rate_notice_counter';
const FS_METHOD = 'flexible_shipping';
/**
* Hooks.
*/
public function hooks() {
add_action( 'woocommerce_order_status_changed', array( $this, 'count_order_for_fs_methods' ), 10, 4 );
}
/**
* Count order.
*
* @param WC_Order $order .
*/
private function count_order( $order ) {
update_option( self::FS_ORDER_COUNTER, intval( get_option( self::FS_ORDER_COUNTER, '0' ) ) + 1 );
$order->update_meta_data( self::FS_ORDER_COUNTER, 1 );
$order->save();
}
/**
* Count orders for FS methods.
*
* @param int $order_id Order ID.
* @param string $status_from Status from.
* @param string $status_to Status to.
* @param WC_Order $order Order.
*/
public function count_order_for_fs_methods( $order_id, $status_from, $status_to, $order ) {
if ( self::ORDER_STATUS_COMPLETED === $status_to ) {
$shipping_methods = $order->get_shipping_methods();
foreach ( $shipping_methods as $shipping_method ) {
if ( self::FS_METHOD === $shipping_method->get_method_id() ) {
if ( '' === $order->get_meta( self::FS_ORDER_COUNTER ) ) {
$this->count_order( $order );
}
}
}
}
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace WPDesk\FS\Rate;
class RateNoticeCreator {
const SETTINGS_RATE_NOTICE_VARIANT_ID = 'flexible_shipping_rate_notice_variant_id';
/**
* Create rate variant
*
* @param string $notice_id Variant ID.
*
* @return RateNoticeInterface;
*/
public function create( $variant ) {
switch( $variant ) {
case FirstRateNotice::SETTINGS_VARIANT_ID: return new FirstRateNotice(); break;
case SecondRateNotice::SETTINGS_VARIANT_ID: return new SecondRateNotice(); break;
case ThirdRateNotice::SETTINGS_VARIANT_ID: return new ThirdRateNotice(); break;
default: return new FirstRateNotice();
}
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace WPDesk\FS\Rate;
class SecondRateNotice extends RateNotice
{
const SETTINGS_VARIANT_ID = 'notice_2';
/**
* Action links
*
* @return array
*/
protected function action_links() {
$actions[] = sprintf(
__( '%1$sOk, you deserved it%2$s', 'flexible-shipping' ),
'<a target="_blank" href="' . esc_url( 'https://wpde.sk/fs-rate-2' ) . '">',
'</a>'
);
$actions[] = sprintf(
__( '%1$sNope, maybe later%2$s', 'flexible-shipping' ),
'<a data-type="date" class="fs-close-temporary-notice notice-dismiss-link" data-source="' . self::CLOSE_TEMPORARY_NOTICE . '" href="#">',
'</a>'
);
$actions[] = sprintf(
__( '%1$sI already did%2$s', 'flexible-shipping' ),
'<a class="close-rate-notice notice-dismiss-link" data-source="already-did" href="#">',
'</a>'
);
return $actions;
}
/**
* Should show message
*
* @return bool
*/
public function should_show_message() {
$notice_date_dissmis = get_option( WPDesk_Flexible_Shipping_Rate_Notice::SETTINGS_OPTION_RATE_NOTICE_DATE_DISMISS, date( "Y-m-d H:i:s", strtotime( 'NOW + 2 weeks' ) ) );
$notice_date = strtotime( $notice_date_dissmis );
$current_date = strtotime( 'NOW' );
$difference = $current_date - $notice_date;
$days = (int) floor( $difference / ( 60 * 60 * 24 ) );
if ( $days > 0 ) {
return true;
}
return false;
}
/**
* Get rate message
*
* @return string
*/
protected function get_message() {
$message = __( 'Awesome, you\'ve been using Flexible Shipping for more than 2 weeks. Could you please do me a BIG favor and give it a 5-star rating on WordPress? ~ Peter', 'flexible-shipping' );
$message .= '<br/>';
$message .= implode( ' | ', $this->action_links() );
return $message;
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace WPDesk\FS\Rate;
class ThirdRateNotice extends RateNotice
{
const CLOSE_TEMPORARY_NOTICE = 'close-temporary-notice-number';
const SETTINGS_VARIANT_ID = 'notice_3';
/**
* Action links
*
* @return array
*/
protected function action_links() {
$actions[] = sprintf(
__( '%1$sOk, you deserved it%2$s', 'flexible-shipping' ),
'<a target="_blank" href="' . esc_url( 'https://wpde.sk/fs-rate-3' ) . '">',
'</a>'
);
$actions[] = sprintf(
__( '%1$sNope, maybe later%2$s', 'flexible-shipping' ),
'<a data-type="number" class="fs close-temporary-notice notice-dismiss-link" data-source="' . self::CLOSE_TEMPORARY_NOTICE . '" href="#">',
'</a>'
);
$actions[] = sprintf(
__( '%1$sI already did%2$s', 'flexible-shipping' ),
'<a class="close-rate-notice notice-dismiss-link" data-source="already-did" href="#">',
'</a>'
);
return $actions;
}
/**
* Should show message
*
* @return bool
*/
public function should_show_message() {
$total_orders = intval( get_option( Flexible_Shipping_Order_Counter::FS_ORDER_COUNTER, '0' ) );
if ( $total_orders >= 100 ) {
return true;
}
return false;
}
/**
* Get rate message
*
* @return string
*/
protected function get_message() {
$message = __( 'Awesome, you just crossed the 100 orders on Flexible Shipping method. Could you please do me a BIG favor and give it a 5-star rating on WordPress? ~ Peter', 'flexible-shipping' );
$message .= '<br/>';
$message .= implode( ' | ', $this->action_links() );
return $message;
}
}

View File

@@ -0,0 +1,72 @@
<?php
/**
* Default data provider. Can get data from shipment.
*/
class WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider_Default implements WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider {
const COMMON_KEYS_TO_REMOVE = array(
'_fs_method',
'_shipping_method',
'_package',
'_packages',
);
/**
* Keys to remove.
*
* @var array
*/
protected $keys_to_remove = array();
/**
* Get data from shipment.
*
* @param WPDesk_Flexible_Shipping_Shipment $shipment .
*
* @return array
*/
public function get_data_from_shipment( WPDesk_Flexible_Shipping_Shipment $shipment ) {
return $this->remove_internal_data_from_shipment_data( get_post_meta( $shipment->get_id() ) );
}
/**
* Filter data.
*
* @param array $data .
*
* @return array
*/
protected function remove_internal_data_from_shipment_data( array $data ) {
$keys_to_remove = array_merge( self::COMMON_KEYS_TO_REMOVE, $this->keys_to_remove );
foreach ( $keys_to_remove as $key ) {
if ( isset( $data[ $key ] ) ) {
unset( $data[ $key ] );
}
}
return $this->format_data( $data );
}
/**
* Format data.
*
* @param array $data .
*
* @return array
*/
private function format_data( array $data ) {
$formatted_data = array();
foreach ( $data as $key => $value ) {
if ( is_array( $value ) && isset( $value[0] ) ) {
$formatted_data[ $key ] = $value[0];
} else {
$formatted_data[ $key ] = $value;
}
}
return $formatted_data;
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* Data providers.
* Collects data providers and can return provider per integration or default provider.
*/
class WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Collection {
/**
* Providers.
*
* @var WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider[]
*/
private $providers = array();
/**
* Add provider.
*
* @param string $integration .
* @param WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider $provider .
*/
public function set_provider( $integration, WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider $provider ) {
$this->providers[ $integration ] = $provider;
}
/**
* Get provider for integration.
*
* @param string $integration .
*
* @return WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider
*/
public function get_provider_for_integration( $integration ) {
if ( isset( $this->providers[ $integration ] ) ) {
return $this->providers[ $integration ];
}
return new WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider_Default();
}
}

View File

@@ -0,0 +1,27 @@
<?php
/**
* Data providers factory.
*/
class WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Factory {
/**
* Providers.
*
* @var WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Collection
*/
private static $data_providers = null;
/**
* Get data providers.
*
* @return WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Collection
*/
public static function get_providers() {
if ( empty( self::$data_providers ) ) {
self::$data_providers = new WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Collection();
}
return self::$data_providers;
}
}

View File

@@ -0,0 +1,69 @@
<?php
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Can append shipments data to WooCommerce REST API Order response.
*/
class WPDesk_Flexible_Shipping_Rest_Api_Order_Response_Data_Appender implements Hookable {
const REST_API_DATA_KEY = 'fs_shipping_lines';
/**
* Hooks.
*/
public function hooks() {
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( $this, 'maybe_append_shipment_to_order_data' ), 10, 3 );
}
/**
* Appends shipment data if exists to order in REST API response.
*
* @param WP_REST_Response $response .
* @param WC_Order $order .
* @param WP_REST_Request $request .
*
* @return WP_REST_Response
*/
public function maybe_append_shipment_to_order_data( $response, $order, $request ) {
$shipments = fs_get_order_shipments( $order->get_id() );
if ( ! empty( $shipments ) ) {
return $this->append_shipment_to_order_data( $response, $order, $request, $shipments );
}
return $response;
}
/**
* Appends shipment data to order in REST API response.
*
* @param WP_REST_Response $response .
* @param WC_Order $order .
* @param WP_REST_Request $request .
* @param WPDesk_Flexible_Shipping_Shipment[] $shipments .
*
* @return WP_REST_Response
*/
private function append_shipment_to_order_data( $response, $order, $request, $shipments ) {
$response_data = $response->get_data();
if ( empty( $response_data[ self::REST_API_DATA_KEY ] ) ) {
$response_data[ self::REST_API_DATA_KEY ] = array();
}
$providers = WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Factory::get_providers();
foreach ( $shipments as $shipment ) {
$integration = $shipment->get_integration();
$data_provider = $providers->get_provider_for_integration( $integration );
$response_data[ self::REST_API_DATA_KEY ][] = $data_provider->get_data_from_shipment( $shipment );
}
$response->set_data( $response_data );
return $response;
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Defines interface that REST API Order Data Provider should implement.
*/
interface WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider {
/**
* Get data from shipment.
*
* @param WPDesk_Flexible_Shipping_Shipment $shipment .
*
* @return array
*/
public function get_data_from_shipment( WPDesk_Flexible_Shipping_Shipment $shipment );
}

View File

@@ -0,0 +1,435 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_Flexible_Shipping_Shipment' ) ) {
/**
* Class WPDesk_Flexible_Shipping_Shipment
*/
abstract class WPDesk_Flexible_Shipping_Shipment {
const STATUS_FS_NEW = 'fs-new';
const STATUS_FS_CREATED = 'fs-created';
const STATUS_FS_CONFIRMED = 'fs-confirmed';
const STATUS_FS_FAILED = 'fs-failed';
const STATUS_FS_MANIFEST = 'fs-manifest';
const CREATED_VIA = 'created_via';
const SENT_VIA_METABOX = 'metabox';
const SENT_VIA_BULK = 'bulk';
const SENT_VIA_AUTO = 'auto';
const SENT_VIA = '_sent_via';
const NOT_SET = 'not_set';
/**
* @var int
*/
private $id;
/**
* @var WP_Post
* Post assigned to shipment
*/
private $post;
/**
* @var null|WC_Order
* WC_Order assigned to shipment
*/
private $order = null;
/**
* @var bool
* True if assigned post ich changed. Used when saving post
*/
private $save_post = false;
/**
* @var null
* Holds old status when shipment status is changed
*/
private $old_status = null;
/**
* @var bool
* True when status changing
*/
private $status_changed = false;
/**
* @var array
* Shipment metadata (from postmeta table)
*/
private $meta_data = array();
/**
* @var bool
* True when shipment metadata loaded
*/
private $meta_data_loaded = false;
/**
* @var array
* Holds changed metadata keys. Used when saving shipment
*/
private $meta_data_save_keys = array();
/**
* @var string
* Context for order metabox
*/
private $order_metabox_context = 'side';
/**
* WPDesk_Flexible_Shipping_Shipment constructor.
*
* @param int|WP_Post|WPDesk_Flexible_Shipping_Shipment $shipment Shipment or shipment ID.
* @param WC_Order|null $order Order.
*/
public function __construct( $shipment, $order = null ) {
if ( is_numeric( $shipment ) ) {
$this->id = absint( $shipment );
$this->post = get_post( $this->id );
} elseif ( $shipment instanceof WPDesk_Flexible_Shipping_Shipment ) {
$this->id = absint( $shipment->get_id() );
$this->post = $shipment->get_post();
} elseif ( isset( $shipment->ID ) ) {
$this->id = absint( $shipment->ID );
$this->post = $shipment;
}
$this->order = $order;
}
/**
* @return mixed
*/
public function get_id() {
return $this->id;
}
/**
* @return mixed
*/
public function get_post() {
return $this->post;
}
/**
* @return WC_Order
*/
public function get_order() {
if ( $this->order == null ) {
$this->order = wc_get_order( $this->post->post_parent );
}
return $this->order;
}
/**
* @return int
*/
public function get_order_id() {
return $this->post->post_parent;
}
/**
* @return string
*/
public function get_order_metabox_context() {
return $this->order_metabox_context;
}
/**
* @param string $order_metabox_context
*/
public function set_order_metabox_context( $order_metabox_context ) {
$this->order_metabox_context = $order_metabox_context;
}
/**
* @param string $meta_key
* @param null|sting $default
* @return array|string|null
*/
public function get_meta( $meta_key = '', $default = null ) {
$this->load_meta_data();
if ( $meta_key == '' ) {
return $this->meta_data;
}
if ( isset( $this->meta_data[$meta_key] ) ) {
return maybe_unserialize( $this->meta_data[$meta_key][0] );
}
else {
return $default;
}
return null;
}
/**
* @param string $meta_key
* @param int|string|array|object|null $value
*/
public function set_meta( $meta_key, $value ) {
$this->load_meta_data();
if ( !isset( $this->meta_data[$meta_key] ) ) {
$this->meta_data[$meta_key] = array();
}
$this->meta_data[$meta_key][0] = $value;
$this->meta_data_save_keys[$meta_key] = $meta_key;
}
/**
* @param string $meta_key
*/
public function delete_meta( $meta_key ) {
unset( $this->meta_data[$meta_key] );
$this->meta_data_save_keys[$meta_key] = $meta_key;
}
/**
* Saves shipment data to database.
*/
public function save() {
if ( $this->save_post ) {
wp_update_post($this->post);
$this->save_post = false;
}
foreach ( $this->meta_data_save_keys as $key ) {
if ( isset( $this->meta_data[$key] ) ) {
update_post_meta( $this->id, $key, $this->meta_data[$key][0] );
}
else {
delete_post_meta( $this->id, $key );
}
unset( $this->meta_data_save_keys[$key] );
}
if ( $this->status_changed ) {
do_action( 'flexible_shipping_shipment_status_updated', $this->old_status, $this->post->post_status, $this );
$this->status_changed = false;
$this->old_status = null;
}
}
/**
* Loads all meta data from postmeta
*/
public function load_meta_data() {
if ( !$this->meta_data_loaded ) {
$this->meta_data = get_post_meta( $this->id );
$this->meta_data_loaded = true;
}
}
/**
* @return string|null
* Returns integration assigned to shipment
*/
public function get_integration() {
return $this->get_meta( '_integration' );
}
/**
* @return string
* Returns URL for admin metabox for this shipment
*/
public function get_order_metabox_url() {
return admin_url( 'post.php?post=' . $this->get_order_id() . '&action=edit#shipment_meta_box_' . $this->get_id() );
}
/**
* @return string
*/
public function get_status() {
return $this->post->post_status;
}
/**
* @return string
*/
public function get_status_for_shipping_column() {
$statuses = array(
self::STATUS_FS_NEW => 'new',
self::STATUS_FS_CREATED => 'created',
self::STATUS_FS_CONFIRMED => 'confirmed',
self::STATUS_FS_FAILED => 'error',
self::STATUS_FS_MANIFEST => 'manifest',
);
return $statuses[$this->get_status()];
}
/**
* @return null|string
* Returns URL for label
*/
public function get_label_url() {
if ( in_array( $this->get_status(), array( self::STATUS_FS_NEW, self::STATUS_FS_CREATED, self::STATUS_FS_FAILED ) ) ) {
return null;
}
$label_url = '?flexible_shipping_get_label=' . $this->get_id() . '&nonce=' . wp_create_nonce( 'flexible_shipping_get_label' ) ;
return site_url( $label_url );
}
/**
* @param string $new_status
*/
public function update_status( $new_status ) {
$this->old_status = $this->post->post_status;
$this->post->post_status = $new_status;
$this->save_post = true;
$this->status_changed = true;
}
public function add_to_manifest( WPDesk_Flexible_Shipping_Manifest $manifest ) {
$this->set_meta( '_manifest', $manifest->get_id() );
}
public function label_avaliable() {
if ( in_array( $this->get_status(), array( self::STATUS_FS_CONFIRMED, self::STATUS_FS_MANIFEST ) ) ) {
return true;
}
return false;
}
/**
* Displays shipping column in orders list view.
* Must be overwritten!
*/
public function shipping_column() {
echo _( 'Please override shipping_column method!', 'flexible-shipping' );
echo '<pre>';
print_r( $this->post );
echo '</pre>';
echo '<pre>';
print_r( $this->meta_data );
echo '</pre>';
}
/**
* Is status fs-new?
*
* @return bool
*/
public function is_status_fs_new() {
return self::STATUS_FS_NEW === $this->get_status();
}
/**
* Is status fs-created?
*
* @return bool
*/
public function is_status_fs_created() {
return self::STATUS_FS_CREATED === $this->get_status();
}
/**
* Is status fs-confirmed?
*
* @return bool
*/
public function is_status_fs_confirmed() {
return self::STATUS_FS_CONFIRMED === $this->get_status();
}
/**
* Is status fs-failed?
*
* @return bool
*/
public function is_status_fs_failed() {
return self::STATUS_FS_FAILED === $this->get_status();
}
/**
* Is status fs-manifest?
*
* @return bool
*/
public function is_status_fs_manifest() {
return self::STATUS_FS_MANIFEST === $this->get_status();
}
/**
* Set created via.
*
* @param string $created_via Created via.
*/
public function set_created_via( $created_via ) {
$this->set_meta( self::CREATED_VIA, $created_via );
}
/**
* Set created via checkout.
*/
public function set_created_via_checkout() {
$this->set_created_via( 'checkout' );
}
/**
* Set created via add shipment.
*/
public function set_created_via_add_shipment() {
$this->set_created_via( 'add_shipment' );
}
/**
* Get created via.
*
* @return string
*/
public function get_created_via() {
return $this->get_meta( self::CREATED_VIA, self::NOT_SET );
}
/**
* Set sent via.
*
* @param string $sent_via
*/
public function set_sent_via( $sent_via ) {
$this->set_meta( self::SENT_VIA, $sent_via );
}
/**
* Set sent via bulk.
*/
public function set_sent_via_bulk() {
$this->set_sent_via( self::SENT_VIA_BULK );
}
/**
* Set sent via auto.
*/
public function set_sent_via_auto() {
$this->set_sent_via( self::SENT_VIA_AUTO );
}
/**
* Set sent via metabox.
*/
public function set_sent_via_metabox() {
$this->set_sent_via( self::SENT_VIA_METABOX );
}
/**
* Get sent via.
*
* @return string
*/
public function get_sent_via() {
return $this->get_meta( self::SENT_VIA, self::NOT_SET );
}
/**
* Get meta shipping method.
*
* @return WC_Order_Item_Shipping
*/
protected function get_meta_shipping_method() {
return $this->get_meta( '_shipping_method' );
}
}
}

View File

@@ -0,0 +1,344 @@
<?php
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WPDesk_Flexible_Shipping_Shipment_CPT {
const POST_TYPE_SHIPMENT = 'shipment';
private $plugin = null;
/**
* Is order processed on checkout?
*
* @var bool
*/
private $is_order_processed_on_checkout = false;
public function __construct( Flexible_Shipping_Plugin $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
public function hooks() {
add_action( 'init', array( $this, 'register_post_types' ), 20 );
add_action( 'init', array( $this, 'flexible_shipping_get_label' ), 9999999 );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20, 2 );
$last_priority = PHP_INT_MAX;
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'create_shipping_for_order' ), $last_priority );
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'woocommerce_order_details_after_order_table' ) );
add_action( 'woocommerce_email_after_order_table', array( $this, 'woocommerce_email_after_order_table' ), 10, 2 );
}
/**
* Register post types.
*/
public function register_post_types() {
if ( post_type_exists( self::POST_TYPE_SHIPMENT ) ) {
return;
}
register_post_type( self::POST_TYPE_SHIPMENT,
array(
'labels' => array(
'name' => __( 'Shipments', 'flexible-shipping' ),
'singular_name' => __( 'Shipment', 'flexible-shipping' ),
'menu_name' => __( 'Shipments', 'flexible-shipping' ),
'parent_item_colon' => '',
'all_items' => __( 'Shipments', 'flexible-shipping' ),
'view_item' => __( 'View Shipments', 'flexible-shipping' ),
'add_new_item' => __( 'Add new Shipment', 'flexible-shipping' ),
'add_new' => __( 'Add new Shipment', 'flexible-shipping' ),
'edit_item' => __( 'Edit Shipment', 'flexible-shipping' ),
'update_item' => __( 'Save Shipment', 'flexible-shipping' ),
'search_items' => __( 'Search Shipments', 'flexible-shipping' ),
'not_found' => __( 'Shipment not found', 'flexible-shipping' ),
'not_found_in_trash' => __( 'Shipment not found in trash', 'flexible-shipping' )
),
'description' => __( 'Shipments.', 'flexible-shipping' ),
'public' => false,
'show_ui' => false,
'capability_type' => 'post',
'capabilities' => array(),
'map_meta_cap' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'query_var' => true,
'supports' => array( 'title' ),
'has_archive' => false,
'show_in_nav_menus' => true,
'menu_icon' => 'dashicons-upload',
)
);
$shipment_statuses = apply_filters( 'flexible_shipping_register_shipment_statuses',
array(
'fs-new' => array(
'label' => _x( 'New', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'New <span class="count">(%s)</span>', 'New <span class="count">(%s)</span>', 'flexible-shipping' ),
),
'fs-created' => array(
'label' => _x( 'Created', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Created <span class="count">(%s)</span>', 'Created <span class="count">(%s)</span>', 'flexible-shipping' ),
),
'fs-confirmed' => array(
'label' => _x( 'Confirmed', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Confirmed <span class="count">(%s)</span>', 'Confirmed <span class="count">(%s)</span>', 'flexible-shipping' ),
),
'fs-manifest' => array(
'label' => _x( 'Manifest created', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Confirmed <span class="count">(%s)</span>', 'Confirmed <span class="count">(%s)</span>', 'flexible-shipping' ),
),
'fs-failed' => array(
'label' => _x( 'Failed', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Failed <span class="count">(%s)</span>', 'Failed <span class="count">(%s)</span>', 'flexible-shipping' ),
),
)
);
foreach ( $shipment_statuses as $shipment_status => $values ) {
register_post_status( $shipment_status, $values );
}
}
public function add_meta_boxes( $post_type, $post ) {
if ( $post_type == self::POST_TYPE_SHIPMENT ) {
add_meta_box(
'shipment_meta_box',
__('Shipment data', 'flexible-shipping'),
array( $this, 'metabox' ),
'shipment',
'normal',
'high'
);
}
if ( $post_type == 'shop_order' ) {
$shipments = fs_get_order_shipments( $post->ID );
foreach ( $shipments as $shipment ) {
$args = array( 'shipment' => $shipment );
add_meta_box(
'shipment_meta_box_' . $shipment->get_id(),
$shipment->get_order_metabox_title(),
array( $this, 'order_metabox' ),
'shop_order',
$shipment->get_order_metabox_context(),
'default',
$args
);
}
}
}
public function order_metabox( $post, $args ) {
/** @var WPDesk_Flexible_Shipping_Shipment $shipment */
$shipment = $args['args']['shipment'];
$shipment_id = $shipment->get_id();
$message = $shipment->get_error_message();
$message_heading = $shipment->get_order_metabox_title();
$message_css_style = '';
include( 'views/order-metabox.php' );
}
public function metabox() {
global $post;
echo '<pre>';
print_r( $post );
echo '</pre>';
$meta_data = get_post_meta( $post->ID );
foreach ( $meta_data as $key => $val ) {
echo '<pre>';
echo $key;
echo ' = ';
print_r( maybe_unserialize( $val[0] ) );
echo '</pre>';
}
}
public function flexible_shipping_get_label() {
if ( !empty( $_GET['flexible_shipping_get_label'] ) && !empty( $_GET['nonce'] ) ) {
$nonce = $_GET['nonce'];
if ( !wp_verify_nonce( $nonce, 'flexible_shipping_get_label' ) ) {
echo __( 'Invalid nonce!', 'flexible-shipping' );
exit;
}
try {
$shipment_id = $_GET['flexible_shipping_get_label'];
$shipment = fs_get_shipment( $shipment_id );
$label_data = $shipment->get_label();
header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=" . $label_data['file_name'] );
echo $label_data['content'];
}
catch ( Exception $e ) {
echo $e->getMessage();
}
exit();
}
}
/**
* Get Flexible Shipping method from order shipping method meta data.
*
* @param WC_Order_Item_Shipping $shipping_method
*
* @return array
*/
private function get_fs_method_from_order_shipping_method( $shipping_method ) {
$fs_method = array();
if ( version_compare( WC_VERSION, '2.6', '<' ) ) {
if ( isset( $shipping_method['item_meta'] )
&& isset( $shipping_method['item_meta']['method_id'] )
&& isset( $shipping_method['item_meta']['method_id'][0] )
) {
$all_shipping_methods = flexible_shipping_get_all_shipping_methods();
$flexible_shipping = $all_shipping_methods['flexible_shipping'];
$flexible_shipping_rates = $flexible_shipping->get_all_rates();
$fs_method = $flexible_shipping_rates[ $shipping_method['item_meta']['method_id'][0] ];
}
}
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
if ( isset( $shipping_method['item_meta'] )
&& isset( $shipping_method['item_meta']['_fs_method'] )
&& isset( $shipping_method['item_meta']['_fs_method'][0] )
) {
$fs_method = unserialize( $shipping_method['item_meta']['_fs_method'][0] );
}
} else {
if ( isset( $shipping_method['item_meta'] )
&& isset( $shipping_method['item_meta']['_fs_method'] )
) {
$fs_method = $shipping_method['item_meta']['_fs_method'];
}
}
return $fs_method;
}
/**
* Create shipment for order and shipping method.
*
* @param WC_Order $order Order.
* @param array $fs_method Flexible Shipping shipping method.
* @param string $shipping_id Shipping Id.
* @param WC_Order_Item_Shipping $shipping_method Shipping method.
* @param array $packages Packages.
* @param int $package_id Package Id.
*
* @return WPDesk_Flexible_Shipping_Shipment
*/
private function create_shipment_for_order_and_fs_shipping_method(
WC_Order $order,
array $fs_method,
$shipping_id,
WC_Order_Item_Shipping $shipping_method,
array $packages,
$package_id
) {
$shipment = fs_create_shipment( $order, $fs_method );
$shipment->set_meta( '_fs_method', $fs_method );
$shipment->set_meta( '_shipping_id', $shipping_id );
$shipment->set_meta( '_shipping_method', $shipping_method );
$shipment->set_meta( '_package', $packages[ $package_id ] );
$shipment->set_meta( '_packages', $packages );
$shipment->set_created_via_checkout();
$shipment->checkout( $fs_method, $packages[ $package_id ] );
$shipment->save();
return $shipment;
}
/**
* Create shipping for order.
*
* @param $order_id
*/
public function create_shipping_for_order( $order_id ) {
$order = wc_get_order( $order_id );
if ( $order && ! $this->is_order_processed_on_checkout ) {
$mutex = \FSVendor\WPDesk\Mutex\WordpressPostMutex::fromOrder( $order );
$mutex->acquireLock();
$shipments = fs_get_order_shipments( $order_id );
if ( 0 === count( $shipments ) ) {
$this->is_order_processed_on_checkout = true;
$order_shipping_methods = $order->get_shipping_methods();
$packages = WC()->shipping->get_packages();
$package_id = - 1;
global $fs_package_id;
foreach ( $order_shipping_methods as $shipping_id => $shipping_method ) {
$package_id ++;
$fs_package_id = $package_id;
$fs_method = $this->get_fs_method_from_order_shipping_method( $shipping_method );
if ( ! empty( $fs_method['method_integration'] ) ) {
if ( fs_shipment_integration_exists( $fs_method['method_integration'] ) ) {
$shipment = $this->create_shipment_for_order_and_fs_shipping_method(
$order, $fs_method, $shipping_id, $shipping_method, $packages, $package_id
);
}
}
}
}
$mutex->releaseLock();
}
}
/**
* Hook woocommerce_order_details_after_order_table.
*
* @param WC_Abstract_Order $order Order.
*/
public function woocommerce_order_details_after_order_table( $order ) {
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$order_id = $order->id;
}
else {
$order_id = $order->get_id();
}
$shipments = fs_get_order_shipments( $order_id );
foreach ( $shipments as $shipment ) {
echo $shipment->get_after_order_table();
}
}
public function woocommerce_email_after_order_table( $order, $sent_to_admin ) {
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$order_id = $order->id;
}
else {
$order_id = $order->get_id();
}
$shipments = fs_get_order_shipments( $order_id );
foreach ( $shipments as $shipment ) {
echo $shipment->get_email_after_order_table();
}
}
}

View File

@@ -0,0 +1,126 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
function fs_shipment_integration_exists( $integration ) {
$class_name = apply_filters( 'flexible_shipping_shipment_class', 'WPDesk_Flexible_Shipping_Shipment' . '_' . $integration, $integration );
if ( class_exists( $class_name ) ) {
return true;
}
return false;
}
/**
* @param WC_Abstract_Order $order Order.
* @param array $fs_method Flexible shipping method.
*
* @return WPDesk_Flexible_Shipping_Shipment
*/
function fs_create_shipment( $order, $fs_method ) {
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$order_id = $order->id;
}
else {
$order_id = $order->get_id();
}
$integration = $fs_method['method_integration'];
$post_title = sprintf( __( 'Shipment for order %s, %s', 'flexible-shipping' ), $order_id, $integration );
$post_title = apply_filters( 'flexible_shipping_shipment_post_title_'. $integration, $post_title, $fs_method );
$shipment_post = array(
'post_title' => $post_title,
'post_type' => 'shipment',
'post_status' => 'fs-new',
'post_parent' => $order_id
);
$shipment_id = wp_insert_post( $shipment_post );
update_post_meta( $shipment_id, '_integration', $integration );
return fs_get_shipment( $shipment_id, $order );
}
/**
* Returns shipments for order.
* Shipments are ordered from oldest to newest.
*
* @param $order_id
* @param string|null $integration
* @return WPDesk_Flexible_Shipping_Shipment_Interface[]
*/
function fs_get_order_shipments( $order_id, $integration = null ) {
$shipments_posts_query = array(
'posts_per_page' => -1,
'post_parent' => $order_id,
'post_type' => 'shipment',
'post_status' => 'any',
'orderby' => 'ID',
'order' => 'ASC',
);
if ( !empty( $integration ) ) {
$shipments_posts_query['meta_key'] = '_integration';
$shipments_posts_query['meta_value'] = $integration;
}
$shipments_posts = get_posts( $shipments_posts_query );
$shipments = array();
if ( count( $shipments_posts ) ) {
$order = wc_get_order($order_id);
foreach ($shipments_posts as $shipment_post) {
$integration = get_post_meta($shipment_post->ID, '_integration', true);
if ( fs_shipment_integration_exists( $integration ) ) {
$shipments[] = fs_get_shipment($shipment_post->ID, $order);
}
}
}
return $shipments;
}
/**
* Get shipment.
*
* @param int $shipment_id Shipment id.
* @param WC_Order|null $order Order.
* @return WPDesk_Flexible_Shipping_Shipment
*/
function fs_get_shipment( $shipment_id, $order = null ) {
$integration = get_post_meta( $shipment_id, '_integration', true );
$class_name = 'WPDesk_Flexible_Shipping_Shipment';
$integration_class_name = 'WPDesk_Flexible_Shipping_Shipment' . '_' . $integration;
$integration_class_name = apply_filters( 'flexible_shipping_shipment_class', $integration_class_name, $integration );
if ( class_exists( $integration_class_name ) ) {
$class_name = $integration_class_name;
}
return new $class_name( $shipment_id, $order );
}
/**
* @param WC_Abstract_Order $order Order.
*
* @return float
*/
function fs_calculate_order_weight( $order ) {
$weight = 0;
if ( sizeof( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$product = $order->get_product_from_item( $item );
}
else {
$product = $item->get_product();
}
$product_weight = $product->get_weight();
if ( ! $product->is_virtual() && is_numeric( $product_weight ) ) {
$weight += floatval( $product->get_weight() ) * floatval( $item['qty'] );
}
}
}
}
return $weight;
}
function fs_calculate_package_weight( $package ) {
$weight = 0;
foreach( $package['contents'] as $item ) {
$weight += floatval( $item['data']->get_weight() ) * floatval( $item['quantity'] );
}
return $weight;
}

View File

@@ -0,0 +1,76 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! interface_exists( 'WPDesk_Flexible_Shipping_Shipment_Interface' ) ) {
interface WPDesk_Flexible_Shipping_Shipment_Interface {
/**
* @param array $fs_method
* @param array $package
* @return void
* Executes on woocommerce checkout when order is created
*/
public function checkout( array $fs_method, $package );
/**
* @return void
* Displays metabox in woocommerce order
*/
public function order_metabox();
/**
* @return string
* Returns woocommerce metabox title
*/
public function get_order_metabox_title();
/**
* @param string $action
* @param array $data
* @return void
* Executes on ajax request. $data contains all woocommerce order metabox fields values from metabox generated in order_metabox() method.
*/
public function ajax_request( $action, $data );
/**
* @return string
* Returns error message
*/
public function get_error_message();
/**
* @return string
* Returns tracking number for shipment
*/
public function get_tracking_number();
/**
* @return string
* Returns tracking URL for shipping
*/
public function get_tracking_url();
/**
* @return array
* Return label data foe shipping in array:
* 'label_format' => 'pdf'
* 'content' => pdf content,
* 'file_name' => file name for label
*/
public function get_label();
/**
* @return mixed
*/
public function get_after_order_table();
/**
* @return mixed
*/
public function get_email_after_order_table();
}
}

View File

@@ -0,0 +1,9 @@
<div class="flexible_shipping_shipment" id="flexible_shipping_shipment_<?php echo $shipment->get_id(); ?>" data-id="<?php echo $shipment_id; ?>">
<?php wp_nonce_field( 'flexible_shipping_shipment_nonce', 'flexible_shipping_shipment_nonce_' . $shipment_id, false ); ?>
<div class="flexible_shipping_shipment_content">
<?php $shipment->order_metabox(); ?>
</div>
<div class="flexible_shipping_shipment_message flexible_shipping_shipment_message_error" style="<?php echo $message_css_style; ?>">
<?php echo $message; ?>
</div>
</div>

View File

@@ -0,0 +1,40 @@
<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php
$params = array(
'type' => 'select',
'options' => $select_options,
'class' => array('first','paczkomaty'),
//'label' => __( 'Integration ', 'flexible-shipping' ),
'id' => 'fs_add_shipping',
);
woocommerce_form_field( 'fs_add_shipping', $params );
?>
<button id="fs_add_shipping_button" class="button button-primary" href="" disabled="disabled"><?php _e( 'Add', 'flexible-shipping' ); ?></button>
<script type="text/javascript">
jQuery('#fs_add_shipping').change(function(){
if ( jQuery(this).val() != '' ) {
jQuery('#fs_add_shipping_button').attr( 'disabled', false );
}
else {
jQuery('#fs_add_shipping_button').attr( 'disabled', true );
}
})
jQuery('#fs_add_shipping_button').click(function(e){
e.preventDefault();
window.location.href = '<?php echo $add_shipping_url; ?>' + '&fs_add_shipping=' + jQuery('#fs_add_shipping').val();
});
if ( typeof window.history.pushState == 'function' ) {
var url = document.location.href;
var url2 = document.location.href;
url = fs_removeParam('_wpnonce', url);
url = fs_removeParam('fs_add_shipping', url);
url = fs_trimChar(url,'?');
if ( url != url2 ) {
window.history.pushState({}, "", url);
}
}
</script>