Skip to content

Commit

Permalink
add Pro plugin updater
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Dec 5, 2023
1 parent a6e4e60 commit 7637c88
Show file tree
Hide file tree
Showing 7 changed files with 492 additions and 87 deletions.
111 changes: 61 additions & 50 deletions includes/API/Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace WCPOS\WooCommercePOS\API;

\defined('ABSPATH') || die;
\defined( 'ABSPATH' ) || die;

if ( ! class_exists('WC_REST_Orders_Controller') ) {
if ( ! class_exists( 'WC_REST_Orders_Controller' ) ) {
return;
}

Expand All @@ -18,6 +18,7 @@
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
use Automattic\WooCommerce\Utilities\OrderUtil;

/**
* Orders controller class.
Expand Down Expand Up @@ -58,24 +59,24 @@ public function __construct() {
*/
public function get_item_schema() {
$schema = parent::get_item_schema();

// Add barcode property to the schema
$schema['properties']['barcode'] = array(
'description' => __('Barcode', 'woocommerce-pos'),
'description' => __( 'Barcode', 'woocommerce-pos' ),
'type' => 'string',
'context' => array('view', 'edit'),
'context' => array( 'view', 'edit' ),
'readonly' => false,
);

// Check and remove email format validation from the billing property
if (isset($schema['properties']['billing']['properties']['email']['format'])) {
unset($schema['properties']['billing']['properties']['email']['format']);
if ( isset( $schema['properties']['billing']['properties']['email']['format'] ) ) {
unset( $schema['properties']['billing']['properties']['email']['format'] );
}

// Modify line_items->parent_name to accept 'string' or 'null'
if (isset($schema['properties']['line_items']) &&
\is_array($schema['properties']['line_items']['items']['properties'])) {
$schema['properties']['line_items']['items']['properties']['parent_name']['type'] = array('string', 'null');
if ( isset( $schema['properties']['line_items'] ) &&
\is_array( $schema['properties']['line_items']['items']['properties'] ) ) {
$schema['properties']['line_items']['items']['properties']['parent_name']['type'] = array( 'string', 'null' );
}

return $schema;
Expand Down Expand Up @@ -142,9 +143,12 @@ public function prepare_line_items( $posted, $action = 'create', $item = null )
$variation = wc_get_product( $item->get_variation_id() );
if ( $variation ) {
// Get the valid attribute keys and remove 'attribute_' prefix
$valid_keys = array_map(function($attribute_name) {
return str_replace( 'attribute_', '', $attribute_name );
}, array_keys( $variation->get_variation_attributes() ));
$valid_keys = array_map(
function ( $attribute_name ) {
return str_replace( 'attribute_', '', $attribute_name );
},
array_keys( $variation->get_variation_attributes() )
);

// Get existing meta data on the item
$meta_data = $item->get_meta_data();
Expand All @@ -158,11 +162,14 @@ public function prepare_line_items( $posted, $action = 'create', $item = null )

if ( \in_array( $key, $valid_keys, true ) ) {
// If the meta data doesn't have an ID, it's considered 'new' and can be replaced by one with an ID.
if ( ! isset($unique_keys[$key]) || (isset($meta_id) && ! isset($unique_keys[$key]['id'])) ) {
$unique_keys[$key] = array('index' => $index, 'id' => $meta_id);
if ( ! isset( $unique_keys[ $key ] ) || ( isset( $meta_id ) && ! isset( $unique_keys[ $key ]['id'] ) ) ) {
$unique_keys[ $key ] = array(
'index' => $index,
'id' => $meta_id,
);
} else {
// Remove the duplicate meta data.
if ($meta->id) {
if ( $meta->id ) {
$item->delete_meta_data_by_mid( $meta->id );
} else {
$meta->value = null;
Expand All @@ -187,14 +194,14 @@ public function prepare_line_items( $posted, $action = 'create', $item = null )
public function wcpos_validate_billing_email( WP_REST_Request $request ) {
// Your custom validation logic for the request data
$billing = $request['billing'] ?? null;
$email = \is_array($billing) ? ($billing['email'] ?? null) : null;
if ( ! \is_null($email) && '' !== $email && ! is_email( $email ) ) {
$email = \is_array( $billing ) ? ( $billing['email'] ?? null ) : null;

if ( ! \is_null( $email ) && '' !== $email && ! is_email( $email ) ) {
return new \WP_Error(
'rest_invalid_param',
// translators: Use default WordPress translation
__( 'Invalid email address.' ),
array('status' => 400)
array( 'status' => 400 )
);
}

Expand All @@ -208,15 +215,15 @@ public function wcpos_validate_billing_email( WP_REST_Request $request ) {
*/
public function get_collection_params() {
$params = parent::get_collection_params();
if (isset($params['per_page'])) {

if ( isset( $params['per_page'] ) ) {
$params['per_page']['minimum'] = -1;
}
if (isset($params['orderby']) && \is_array($params['orderby']['enum'])) {

if ( isset( $params['orderby'] ) && \is_array( $params['orderby']['enum'] ) ) {
$params['orderby']['enum'] = array_merge(
$params['orderby']['enum'],
array('status', 'customer_id', 'payment_method', 'total')
array( 'status', 'customer_id', 'payment_method', 'total' )
);
}

Expand All @@ -237,18 +244,21 @@ public function register_routes(): void {
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'wcpos_send_email' ),
'permission_callback' => array( $this, 'wcpos_send_email_permissions_check' ),
'args' => array_merge($this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
'email' => array(
'type' => 'string',
'description' => __( 'Email address', 'woocommerce-pos' ),
'required' => true,
),
'save_to' => array(
'type' => 'string',
'description' => __( 'Save email to order', 'woocommerce-pos' ),
'required' => false,
),
)),
'args' => array_merge(
$this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
array(
'email' => array(
'type' => 'string',
'description' => __( 'Email address', 'woocommerce-pos' ),
'required' => true,
),
'save_to' => array(
'type' => 'string',
'description' => __( 'Save email to order', 'woocommerce-pos' ),
'required' => false,
),
)
),
),
'schema' => array(),
)
Expand Down Expand Up @@ -294,7 +304,7 @@ public function wcpos_send_email( WP_REST_Request $request ) {

return rest_ensure_response( array( 'success' => true ) );

// $response->set_status( 201 );
// $response->set_status( 201 );
}

/**
Expand Down Expand Up @@ -365,10 +375,13 @@ public function wcpos_order_response( WP_REST_Response $response, WC_Order $orde
$this->maybe_add_post_uuid( $order );

// Add payment link to the order.
$pos_payment_url = add_query_arg(array(
'pay_for_order' => true,
'key' => $order->get_order_key(),
), get_home_url( null, '/wcpos-checkout/order-pay/' . $order->get_id() ));
$pos_payment_url = add_query_arg(
array(
'pay_for_order' => true,
'key' => $order->get_order_key(),
),
get_home_url( null, '/wcpos-checkout/order-pay/' . $order->get_id() )
);

$response->add_link( 'payment', $pos_payment_url, array( 'foo' => 'bar' ) );

Expand Down Expand Up @@ -435,7 +448,7 @@ public function wcpos_before_order_object_save( WC_Order $order ): void {
*
* @return array|WP_Error
*/
public function wcpos_get_all_posts(array $fields = array() ): array {
public function wcpos_get_all_posts( array $fields = array() ): array {
$args = array(
'limit' => -1,
'return' => 'ids',
Expand Down Expand Up @@ -463,8 +476,8 @@ public function wcpos_get_all_posts(array $fields = array() ): array {
* Filters all query clauses at once.
* Covers the fields (SELECT), JOIN, WHERE, GROUP BY, ORDER BY, and LIMIT clauses.
*
* @param string[] $clauses {
* Associative array of the clauses for the query.
* @param string[] $clauses {
* Associative array of the clauses for the query.
*
* @var string The SELECT clause of the query.
* @var string The JOIN clause of the query.
Expand All @@ -482,7 +495,7 @@ public function wcpos_get_all_posts(array $fields = array() ): array {
public function wcpos_hpos_orderby_status_query( array $clauses, $query, $args ) {
if ( isset( $clauses['orderby'] ) && '' === $clauses['orderby'] ) {
$order = $args['order'] ?? 'ASC';
$clauses['orderby'] = $query->get_table_name('orders') . '.status ' . $order;
$clauses['orderby'] = $query->get_table_name( 'orders' ) . '.status ' . $order;
}

return $clauses;
Expand Down Expand Up @@ -524,10 +537,8 @@ protected function prepare_objects_query( $request ) {
}

// If HPOS is enabled and $args['orderby'] = 'post_status', we need to add a custom query clause
if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) &&
method_exists( '\Automattic\WooCommerce\Utilities\OrderUtil', 'custom_orders_table_usage_is_enabled' ) &&
\Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
if ('status' === $request['orderby']) {
if ( class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
if ( 'status' === $request['orderby'] ) {
add_filter( 'woocommerce_orders_table_query_clauses', array( $this, 'wcpos_hpos_orderby_status_query' ), 10, 3 );
}
}
Expand Down
69 changes: 41 additions & 28 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Paul Kilmurray <[email protected]>
*
* @see http://www.wcpos.com
* @package WCPOS\Admin
*/

namespace WCPOS\WooCommercePOS;
Expand All @@ -23,23 +24,43 @@
use WCPOS\WooCommercePOS\Admin\Products\List_Products;
use WCPOS\WooCommercePOS\Admin\Products\Single_Product;
use WCPOS\WooCommercePOS\Admin\Settings;
use WCPOS\WooCommercePOS\Admin\Updaters\Pro_Plugin_Updater;

/**
* Admin class.
*/
class Admin {
/**
* @vars string Unique menu identifier
* POS Menu IDs.
*
* @var string[] Unique menu identifier.
*/
private $menu_ids = array();

/**
* Constructor.
*
* NOTE: WordPress fires the admin_menu hook before the admin_init.
* 1. admin_menu
* 2. admin_init
* 3. current_screen
*
* We need admin_menu at priority 5 so that we can hook the Analytics menu before WooCommerce.
*/
public function __construct() {
$this->init();
// NOTE: The admin_menu needs to hook the Analytics menu before WooCommerce calls the admin_menu hook.
add_action( 'admin_menu', array( $this, 'admin_menu' ), 5 );
add_action( 'admin_init', array( $this, 'init' ) );
add_action( 'current_screen', array( $this, 'current_screen' ) );
}

/**
* Load admin subclasses.
*/
public function init(): void {
new Notices();
new Pro_Plugin_Updater();
}

/**
* Fires before the administration menu loads in the admin.
*/
Expand All @@ -52,14 +73,14 @@ public function admin_menu(): void {
}

/**
* Conditionally load subclasses.
* Conditionally load subclasses based on admin screen.
*
* @param $current_screen
* @param \WP_Screen $current_screen Current screen object.
*/
public function current_screen( $current_screen ): void {
$action = $_GET['action'] ?? '';

// Main switch for screen IDs
// Main switch for screen IDs.
switch ( $current_screen->id ) {
case 'options-permalink':
new Permalink();
Expand All @@ -76,14 +97,14 @@ public function current_screen( $current_screen ): void {
return;
case 'shop_order':
new Single_Order();

return;
case 'edit-shop_order':
new List_Orders();

return;
case 'woocommerce_page_wc-orders':
if ('edit' === $action) {
if ( 'edit' === $action ) {
new HPOS_Single_Order();
} else {
new HPOS_List_Orders();
Expand All @@ -95,14 +116,14 @@ public function current_screen( $current_screen ): void {

return;
default:
// Check if the current screen matches a custom setting page ID
if ($this->is_woocommerce_pos_setting_page($current_screen)) {
// Check if the current screen matches a custom setting page ID.
if ( $this->is_woocommerce_pos_setting_page( $current_screen ) ) {
new Settings();

return;
}
// Check if the current screen is for WooCommerce Analytics
if ($this->is_woocommerce_analytics($current_screen)) {
// Check if the current screen is for WooCommerce Analytics.
if ( $this->is_woocommerce_analytics( $current_screen ) ) {
new Analytics();

return;
Expand All @@ -113,35 +134,27 @@ public function current_screen( $current_screen ): void {
/**
* Check if the current screen matches the POS setting page ID.
*
* @param mixed $current_screen
* @param \WP_Screen $current_screen Current screen object.
*/
private function is_woocommerce_pos_setting_page($current_screen) {
return \array_key_exists('settings', $this->menu_ids) && $this->menu_ids['settings'] === $current_screen->id;
private function is_woocommerce_pos_setting_page( $current_screen ) {
return \array_key_exists( 'settings', $this->menu_ids ) && $this->menu_ids['settings'] === $current_screen->id;
}

/**
* Check if the current screen is for WooCommerce Analytics.
*
* @param mixed $current_screen
* @param \WP_Screen $current_screen Current screen object.
*/
private function is_woocommerce_analytics($current_screen) {
if (class_exists('\Automattic\WooCommerce\Admin\PageController')) {
private function is_woocommerce_analytics( $current_screen ) {
if ( class_exists( '\Automattic\WooCommerce\Admin\PageController' ) ) {
$wc_admin_page_controller = PageController::get_instance();
$wc_admin_current_page = $wc_admin_page_controller->get_current_page();
$id = $wc_admin_current_page['id'] ?? null;
$parent = $wc_admin_current_page['parent'] ?? null;
$id = $wc_admin_current_page['id'] ?? null;
$parent = $wc_admin_current_page['parent'] ?? null;

return 'woocommerce-analytics' === $id || 'woocommerce-analytics' === $parent;
}

return false;
}


/**
* Load admin subclasses.
*/
private function init(): void {
new Notices();
}
}
Loading

0 comments on commit 7637c88

Please sign in to comment.