Skip to content

Commit

Permalink
Add store help functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Nov 28, 2023
1 parent 3e5f313 commit 8df42ef
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 14 deletions.
65 changes: 54 additions & 11 deletions includes/API/Stores.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,17 @@ public function register_routes(): void {
*/
public function get_items( $request ) {
try {
$store = new Store();

// Check if store data is available
if ( ! $store ) {
return new \WP_Error(
'woocommerce_pos_store_not_found',
esc_html__( 'Store not found', 'woocommerce-pos' ),
array( 'status' => 404 )
);
$stores = wcpos_get_stores();

$response = array();
foreach ( $stores as $store ) {
$data = $this->prepare_item_for_response( $store, $request );
$response[] = $this->prepare_response_for_collection( $data );
}

$data = $store->get_data();
$response = rest_ensure_response( array( $data ) );
$response = rest_ensure_response( $response );
$response->header( 'X-WP-Total', count( $stores ) );
$response->header( 'X-WP-TotalPages', 1 );

return $response;

Expand All @@ -79,6 +77,31 @@ public function get_items( $request ) {
}
}

/**
* Prepare a single product output for response.
*
* @param Store $store Store object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $store, $request ) {
$data = $store->get_data();
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $store, $request ) );

/**
* Filter the data for a response.
*
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
* prepared for the response.
*
* @param WP_REST_Response $response The response object.
* @param Store $store Store object.
* @param WP_REST_Request $request Request object.
*/
return apply_filters( 'woocommerce_pos_rest_prepare_store', $response, $store, $request );
}

/**
* Check if the user is logged in.
*
Expand All @@ -95,4 +118,24 @@ public function check_permissions() {

return true;
}

/**
* Prepare links for the request.
*
* @param WC_Product $product Product object.
* @param WP_REST_Request $request Request object.
* @return array Links for the given product.
*/
protected function prepare_links( $store, $request ) {
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $store->get_id() ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
),
);

return $links;
}
}
1 change: 1 addition & 0 deletions includes/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Init {
public function __construct() {
// global helper functions
require_once PLUGIN_PATH . 'includes/wcpos-functions.php';
require_once PLUGIN_PATH . 'includes/wcpos-store-functions.php';

// Init hooks
add_action( 'init', array( $this, 'init' ) );
Expand Down
2 changes: 1 addition & 1 deletion includes/Services/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Store extends WC_Data {
*
* @var string
*/
protected $object_type = 'product';
protected $object_type = 'store';

/**
* Stores product data.
Expand Down
9 changes: 7 additions & 2 deletions includes/Templates/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ public function footer(): void {
$development = isset( $_ENV['DEVELOPMENT'] ) && $_ENV['DEVELOPMENT'];
$user = wp_get_current_user();
$github_url = 'https://wcpos.github.io/managed-expo/';
$store = new Store();
$auth_service = new Auth();
$stores = array_map(
function ( $store ) {
return $store->get_data();
},
wcpos_get_stores()
);

$site_uuid = get_option( 'woocommerce_pos_uuid' );
if ( ! $site_uuid ) {
Expand Down Expand Up @@ -110,7 +115,7 @@ public function footer(): void {
'use_jwt_as_param' => woocommerce_pos_get_settings( 'tools', 'use_jwt_as_param' ),
),
'wp_credentials' => $auth_service->get_user_data( $user ),
'stores' => array( $store->get_data() ),
'stores' => $stores,
);

/**
Expand Down
47 changes: 47 additions & 0 deletions includes/wcpos-store-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* WooCommerce POS Store Functions
*
* Functions for store specific things.
*/

defined( 'ABSPATH' ) || exit;

use WCPOS\WooCommercePOS\Services\Store;

/**
* Standard way of retrieving stores based on certain parameters.
*
* This function should be used for store retrieval so that we have a data agnostic
* way to get a list of stores.
*
* @since 1.4.0
*
* @param array $args Array of args.
* @return array|stdClass Number of pages and an array of product objects if
* paginate is true, or just an array of values.
*/
if ( ! \function_exists( 'wcpos_get_stores' ) ) {
function wcpos_get_stores( $args = array() ) {
$store = new Store();
return apply_filters( 'woocommerce_pos_get_stores', array( $store ), $args );
}
}

/**
* Main function for returning store.
*
* This function should only be called after 'init' action is finished, as there might be taxonomies that are getting
* registered during the init action.
*
* @since 1.4.0
*
* @param mixed $the_store Post object or post ID of the product.
* @return Store|null|false
*/
if ( ! \function_exists( 'wcpos_get_store' ) ) {
function wcpos_get_store( $the_store = false ) {
$store = new Store();
return apply_filters( 'woocommerce_pos_get_store', $store, $the_store );
}
}

0 comments on commit 8df42ef

Please sign in to comment.