Skip to content

Commit

Permalink
Fix _create_via warning
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Feb 23, 2024
1 parent 0ec7d08 commit 960dcab
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 20 deletions.
7 changes: 5 additions & 2 deletions includes/API.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* REST API Class.
* WooCommerce POS REST API Class, ie: /wcpos/v1/ endpoints.
*
* @author Paul Kilmurray <[email protected]>
*
* @see http://wcpos.com
* @package WCPOS\WooCommercePOS
*/

namespace WCPOS\WooCommercePOS;
Expand All @@ -17,6 +17,9 @@
use WP_REST_Response;
use WP_REST_Server;

/**
*
*/
class API {
/**
* WCPOS REST API namespaces and endpoints.
Expand Down
17 changes: 6 additions & 11 deletions includes/API/Product_Variations_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,12 @@ public function wcpos_variation_response( WP_REST_Response $response, WC_Data $v
$data['barcode'] = $this->wcpos_get_barcode( $variation );

// Check if the response has an image
if ( isset( $data['images'] ) && ! empty( $data['images'] ) ) {
foreach ( $data['images'] as $key => $image ) {
// Replace the full size 'src' with the URL of the medium size image.
$image_id = $image['id'];
$medium_image_data = image_downsize( $image_id, 'medium' );

if ( $medium_image_data && isset( $medium_image_data[0] ) ) {
$data['images'][ $key ]['src'] = $medium_image_data[0];
} else {
$data['images'][ $key ]['src'] = $image['src'];
}
if ( isset( $data['image'] ) && ! empty( $data['image'] ) && isset( $data['image']['id'] ) ) {
// Replace the full size 'src' with the URL of the medium size image.
$medium_image_data = image_downsize( $data['image']['id'], 'medium' );

if ( $medium_image_data && isset( $medium_image_data[0] ) ) {
$data['image']['src'] = $medium_image_data[0];
}
}

Expand Down
2 changes: 1 addition & 1 deletion includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function current_screen( $current_screen ): void {
*
*/
public function handle_wc_hpos_orders_screen() {
if ( 'edit' === $_GET['action'] ) {
if ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) {
new HPOS_Single_Order();
} else {
new HPOS_List_Orders();
Expand Down
2 changes: 1 addition & 1 deletion includes/Admin/Orders/HPOS_List_Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function orders_custom_column_content( string $column_name, $order ): voi
if ( $order instanceof WC_Abstract_Order ) {
// Use the getter methods for order meta data
$legacy = $order->get_meta( '_pos', true );
$created_via = $order->get_meta( '_created_via', true );
$created_via = $order->get_created_via();

// Check if the order was created via WooCommerce POS
if ( 'woocommerce-pos' === $created_via || '1' === $legacy ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/Admin/Orders/List_Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function pos_orders_list_column_content( string $column_name, int $post_i
if ( $order instanceof WC_Abstract_Order ) {
// Use the getter methods for order meta data
$legacy = $order->get_meta( '_pos', true );
$created_via = $order->get_meta( '_created_via', true );
$created_via = $order->get_created_via();

// Check if the order was created via WooCommerce POS
if ( 'woocommerce-pos' === $created_via || '1' === $legacy ) {
Expand Down
8 changes: 7 additions & 1 deletion includes/Gateways.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<?php

/**
* Loads the POS Payment Gateways.
*
* @author Paul Kilmurray <[email protected]>
*
* @see https://wcpos.com
* @package WCPOS\WooCommercePOS
*/

namespace WCPOS\WooCommercePOS;

/**
* Gateways class.
*/
class Gateways {
/**
*
*/
public function __construct() {
add_action( 'woocommerce_payment_gateways', array( $this, 'payment_gateways' ) );
add_filter( 'woocommerce_available_payment_gateways', array( $this, 'available_payment_gateways' ), 99 );
Expand Down
4 changes: 4 additions & 0 deletions includes/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function __construct() {
// Headers for API discoverability
add_filter( 'rest_pre_serve_request', array( $this, 'rest_pre_serve_request' ), 5, 4 );
add_action( 'send_headers', array( $this, 'send_headers' ), 99, 1 );

// add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
}

/**
Expand Down Expand Up @@ -110,6 +112,8 @@ private function init_integrations() {
public function init_rest_api(): void {
if ( woocommerce_pos_request() ) {
new API();
} else {
new WC_API();
}
}

Expand Down
91 changes: 91 additions & 0 deletions includes/WC_API.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* WooCommerce REST API Class, ie: /wc/v3/ endpoints.
*
* @author Paul Kilmurray <[email protected]>
*
* @see http://wcpos.com
* @package WCPOS\WooCommercePOS
*/

namespace WCPOS\WooCommercePOS;

/**
*
*/
class WC_API {

/**
*
*/
public function __construct() {
$pos_only_products = woocommerce_pos_get_settings( 'general', 'pos_only_products' );

if ( $pos_only_products ) {
add_action( 'woocommerce_product_query', array( $this, 'hide_pos_only_products' ) );
add_filter( 'woocommerce_variation_is_visible', array( $this, 'hide_pos_only_variations' ), 10, 4 );
}
}

/**
* Hide POS Only products from the shop and category pages.
*
* @TODO - this should be improved so that admin users can see the product, but get a message
*
* @param WP_Query $query Query instance.
*
* @return void
*/
public function hide_pos_only_products( $query ) {
$meta_query = $query->get( 'meta_query' );

// Define your default meta query.
$default_meta_query = array(
'relation' => 'OR',
array(
'key' => '_pos_visibility',
'value' => 'pos_only',
'compare' => '!=',
),
array(
'key' => '_pos_visibility',
'compare' => 'NOT EXISTS',
),
);

// Check if an existing meta query exists.
if ( is_array( $meta_query ) ) {
if ( ! isset( $meta_query ['relation'] ) ) {
$meta_query['relation'] = 'AND';
}
$meta_query[] = $default_meta_query;
} else {
$meta_query = $default_meta_query;
}

// Set the updated meta query back to the query.
$query->set( 'meta_query', $meta_query );
}

/**
* Remove POS Only variations from the storefront.
*
* @param bool $visible Whether the variation is visible.
* @param int $variation_id The variation ID.
* @param int $product_id The product ID.
* @param \WC_Product_Variation $variation The variation object.
*/
public function hide_pos_only_variations( $visible, $variation_id, $product_id, $variation ) {
if ( \is_shop() || \is_product_category() || \is_product() ) {
// Get the _pos_visibility meta value for the variation.
$pos_visibility = get_post_meta( $variation_id, '_pos_visibility', true );

// Check if _pos_visibility is 'pos_only' for this variation.
if ( $pos_visibility === 'pos_only' ) {
return false;
}
}

return $visible;
}
}
6 changes: 6 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co

== Changelog ==

= 1.4.11 - 2024/02/XX =
* Fix: regression in product variation images, use 'medium' sized product image instead of full size
* Fix: remove POS Only products from frontend WC REST API response
* Fix: generic get meta method should not be used for '_create_via'
* Fix: other minor PHP warnings

= 1.4.10 - 2024/01/23 =
* Fix: compatibility issue with WooCommerce < 6.7.0

Expand Down
40 changes: 38 additions & 2 deletions tests/includes/Test_Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace WCPOS\WooCommercePOS\Tests;

use Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper;
use WC_Unit_Test_Case;
use WC_REST_Unit_Test_Case;
use WP_Query;
use WC_Query;
use WCPOS\WooCommercePOS\Products;
Expand All @@ -14,7 +14,7 @@
*
* @coversNothing
*/
class Test_Products extends WC_Unit_Test_Case {
class Test_Products extends WC_REST_Unit_Test_Case {
public function setup(): void {
parent::setup();
}
Expand Down Expand Up @@ -118,4 +118,40 @@ function () {
// Assert that the variation without '_pos_visibility' set is in the query
$this->assertContains( $variation_ids[2], $queried_variation_ids );
}

/**
*
*/
public function test_pos_only_products_via_store_api() {
add_filter(
'woocommerce_pos_general_settings',
function () {
return array(
'pos_only_products' => true,
);
}
);
new Products(); // reinstantiate the class to apply the filter

// Create a visible product
$visible_product = ProductHelper::create_simple_product();

// Create a product with _pos_visibility set to 'pos_only'
$hidden_product = ProductHelper::create_simple_product();
update_post_meta( $hidden_product->get_id(), '_pos_visibility', 'pos_only' );

// Verify that the meta value is set correctly
$pos_visibility = get_post_meta( $hidden_product->get_id(), '_pos_visibility', true );
$this->assertEquals( 'pos_only', $pos_visibility, 'Meta value for _pos_visibility not set correctly' );

// Make WC REST request
add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
$request = new \WP_REST_Request( 'GET', '/wc/v3/products' );
$response = $this->server->dispatch( $request );

$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $visible_product->get_id(), $data[0]['id'] );
}
}
2 changes: 1 addition & 1 deletion woocommerce-pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @author Paul Kilmurray <[email protected]>
*
* @see http://wcpos.com
* @package WooCommercePOS
* @package WCPOS\WooCommercePOS
*/

namespace WCPOS\WooCommercePOS;
Expand Down

0 comments on commit 960dcab

Please sign in to comment.