first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
/vendor/
.idea
build-coverage
swagger
composer.lock

View File

@@ -0,0 +1,15 @@
## [1.1.0] - 2022-08-30
### Removed
- strong typing for PSR\LoggerInterface
## [1.0.3] - 2021-06-30
### Removed
- libraries
## [1.0.2] - 2021-06-30
### Removed
- REST API classes
## [1.0.0] - 2021-06-30
### Added
- Init

View File

@@ -0,0 +1,27 @@
destination: docs
templateConfig: /app/theme-woocommerce/config.neon
extensions: [php]
source:
- classes
exclude:
- vendor
- tests
- languages
charset: [UTF-8]
main: Wordpress plugin
title: Plugin template more info
baseUrl: "/"
templateTheme: default
php: false
sourceCode: false
tree: true
deprecated: false
todo: false
download: false
accessLevels:
- public
- private
- protected

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,7 @@
<?php
/**
* .
*/
class WPDesk_Flexible_Shipping_Unable_To_Create_Tmp_File_Exception extends RuntimeException {
}

View File

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

View File

@@ -0,0 +1,61 @@
<?php
/**
* Labels builder.
*
* @package Flexible Shipping
*/
/**
* Abstract for labels builders.
*/
abstract class WPDesk_Flexible_Shipping_Integration_Label_Builder implements WPDesk_Flexible_Shipping_Labels_Builder {
/**
* Integration ID.
*
* @var string
*/
private $integration_id;
/**
* Shipments.
*
* @var WPDesk_Flexible_Shipping_Shipment_Interface[]
*/
protected $shipments = array();
/**
* .
*
* @param string $integration_id .
*/
public function __construct( $integration_id ) {
$this->integration_id = $integration_id;
}
/**
* Get integration id.
*
* @return string
*/
public function get_integration_id() {
return $this->integration_id;
}
/**
* .
*
* @param WPDesk_Flexible_Shipping_Shipment $shipment .
*/
public function add_shipment( WPDesk_Flexible_Shipping_Shipment $shipment ) {
$this->shipments[] = $shipment;
}
/**
* Get labels for shipments.
*
* @return array
*/
abstract public function get_labels_for_shipments();
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* Label builder.
*
* @package Flexible Shipping
*/
/**
* Interface for labels builder.
*/
interface WPDesk_Flexible_Shipping_Labels_Builder {
/**
* Get integration id.
*
* @return string
*/
public function get_integration_id();
/**
* Get labels for shipments.
*
* @return array
* Returns array of labels. Each label is array with fields:
* content: label file content (ie. pdf file content) as string
* file_name: label file name
*/
public function get_labels_for_shipments();
/**
* .
*
* @param WPDesk_Flexible_Shipping_Shipment $shipment .
*/
public function add_shipment( WPDesk_Flexible_Shipping_Shipment $shipment );
}

View File

@@ -0,0 +1,39 @@
<?php
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,210 @@
<?php
/**
* 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,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,31 @@
<?php
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,521 @@
<?php
/**
* Shipment.
*
* @package Flexible Shipping
*/
use Psr\Log\LoggerInterface;
/**
* Can handle shipment functionality.
*/
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';
const LABEL_ACTION_DOWNLOAD = 'download';
const LABEL_ACTION_OPEN = 'open';
/**
* Logger provided by Flexible Shipping plugin.
*
* @var LoggerInterface
*/
protected static $fs_logger;
/**
* @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;
}
/**
* Set logger. This logger is set by Flexible Shipping plugin.
*
* @param LoggerInterface $fs_logger .
*/
public static function set_fs_logger( $fs_logger ) {
static::$fs_logger = $fs_logger;
}
/**
* @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 .
*/
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->fs_log( 'debug', 'Shipment post saved', array( '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] );
$this->fs_log(
'debug',
'Shipment meta data saved',
array(
'key' => $key,
'value' => $this->meta_data[ $key ][0],
)
);
} else {
delete_post_meta( $this->id, $key );
$this->fs_log( 'debug', 'Shipment meta data deleted', array( 'key' => $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;
}
}
/**
* Writes log message to log provided by Flexible Shipping plugin.
*
* @param mixed $level .
* @param string $message .
* @param array $context .
*/
private function fs_log( $level, $message, array $context = array() ) {
if ( static::$fs_logger ) {
$context['order_id'] = $this->get_order()->get_id();
$context['shipment_id'] = $this->get_id();
$current_user = wp_get_current_user();
$context['user_id'] = $current_user->ID;
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$context['request_uri'] = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
}
static::$fs_logger->log( $level, $message, $context );
}
}
/**
* @return WC_Order
*/
public function get_order() {
if ( null === $this->order ) {
$this->order = wc_get_order( $this->post->post_parent );
}
return $this->order;
}
/**
* @return string|null
* Returns integration assigned to shipment
*/
public function get_integration() {
return $this->get_meta( '_integration' );
}
/**
* @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;
}
return null;
}
/**
* @return array
*/
public function get_meta_data() {
$this->load_meta_data();
return $this->meta_data;
}
/**
* 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
* 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 int
*/
public function get_order_id() {
return $this->post->post_parent;
}
/**
* @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 string
*/
public function get_status() {
return $this->post->post_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_args = array(
'action' => 'flexible_shipping_get_label',
'label_action' => $this->get_label_action(),
'shipment_id' => $this->get_id(),
);
$label_url = add_query_arg( $label_url_args, admin_url( 'admin-post.php' ) );
return wp_nonce_url( $label_url, 'flexible_shipping_get_label' );
}
/**
* Should open label?
* By default label should be downloaded. Integration can override this method when label should be opened in browser.
*
* @return bool
*/
protected function get_label_action() {
return self::LABEL_ACTION_DOWNLOAD;
}
/**
* @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 WPDesk_Flexible_Shipping_Manifest $manifest .
*/
public function add_to_manifest( WPDesk_Flexible_Shipping_Manifest $manifest ) {
$this->set_meta( '_manifest', $manifest->get_id() );
}
/**
* @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;
}
/**
* @return bool
*/
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 esc_html( __( '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 checkout.
*/
public function set_created_via_checkout() {
$this->set_created_via( 'checkout' );
}
/**
* 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 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 bulk.
*/
public function set_sent_via_bulk() {
$this->set_sent_via( self::SENT_VIA_BULK );
}
/**
* 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 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,163 @@
<?php
/**
* Shipments functions.
*
* @package Flexible Shipping
*/
/**
* @param string $integration .
*
* @return string
*/
function fs_shipment_integration_class( $integration ) {
return apply_filters( 'flexible_shipping_shipment_class', 'WPDesk_Flexible_Shipping_Shipment_' . $integration, $integration );
}
/**
* @param string $integration .
*
* @return bool
*/
function fs_shipment_integration_exists( $integration ) {
return class_exists( fs_shipment_integration_class( $integration ) );
}
/**
* @param string $integration .
* @param string $order_type .
*
* @return false
*/
function fs_integration_supports_order_type( $integration, $order_type ) {
$supports = true;
if ( 'shop_subscription' === $order_type ) {
$supports = false;
$class_name = fs_shipment_integration_class( $integration );
if ( class_exists( $class_name ) ) {
$supports = $class_name::is_subscriptions_supported();
}
}
return $supports;
}
/**
* @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'];
// Translators: order id and integration.
$post_title = sprintf( __( 'Shipment for order %s, %s', 'flexible-shipping' ), $order_id, $integration ); // phpcs:ignore.
$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 int $order_id .
* @param string|null $integration .
*
* @return WPDesk_Flexible_Shipping_Shipment[]
*/
function fs_get_order_shipments( $order_id, $integration = null ) {
$shipments_posts_query = array(
'nopaging' => true,
'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 = fs_shipment_integration_class( $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 ( count( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$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;
}
/**
* @param array $package .
*
* @return float|int
*/
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,101 @@
<?php
/**
* Interface WPDesk_Flexible_Shipping_Shipment_Interface
*
* @package Flexible Shipping
*/
/**
* Shipping.
*/
interface WPDesk_Flexible_Shipping_Shipment_Interface {
/**
* @param string $meta_key .
* @param null|string $default .
*
* @return array|string|null
*/
public function get_meta( $meta_key = '', $default = null );
/**
* @param string $meta_key .
* @param int|string|array|object|null $value .
*/
public function set_meta( $meta_key, $value );
/**
* @return array
*/
public function get_meta_data();
/**
* @param array $fs_method .
* @param array $package .
*
* @return void
* Executes on woocommerce checkout when order is created
*/
public function checkout( array $fs_method, $package );
/**
* Displays metabox in woocommerce order.
*
* @return void
*/
public function order_metabox();
/**
* Returns woocommerce metabox title.
*
* @return string
*/
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,34 @@
{
"name": "wpdesk/wp-wpdesk-fs-shipment-interfaces",
"authors": [
{
"name": "Krzysiek",
"email": "krzysiek@wpdesk.pl"
}
],
"config": {
"platform": {
"php": "5.6"
},
"sort-packages": true
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"10up/wp_mock": "^0.2.0",
"phpunit/phpunit": "<7",
"squizlabs/php_codesniffer": "^3.4.2",
"wp-coding-standards/wpcs": "^0.14.1"
},
"autoload": {
},
"autoload-dev": {
},
"scripts": {
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}

View File

@@ -0,0 +1,28 @@
<phpunit bootstrap="tests/integration/bootstrap.php"
backupGlobals="false"
>
<testsuites>
<testsuite>
<directory prefix="Test" suffix=".php">./tests/integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build-coverage/report.junit.xml"/>
<log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build-coverage/coverage.txt"/>
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
<php>
<env name="WP_DEVELOP_DIR" value="/tmp/wordpress-develop"/>
<env name="WC_DEVELOP_DIR" value="/tmp/woocommerce"/>
</php>
</phpunit>

View File

@@ -0,0 +1,21 @@
<phpunit bootstrap="tests/unit/bootstrap.php">
<testsuites>
<testsuite>
<directory prefix="Test" suffix=".php">./tests/unit/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build-coverage/report.junit.xml"/>
<log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build-coverage/coverage.txt"/>
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
</phpunit>