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,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>