Group extends Base_Route implements Route

REST controller for the group route.

Tags
author

Barn2 Plugins support@barn2.com

license

GPL-3.0

copyright

Barn2 Media Ltd

Interfaces, Classes and Traits

Route

Table of Contents

$rest_base  : mixed
compare_options()  : mixed
create()  : WP_REST_Response|WP_Error
Create a group
delete()  : WP_REST_Response|WP_Error
Delete a group
duplicate()  : WP_REST_Response|WP_Error
Duplicate a group
export()  : WP_REST_Response|WP_Error
Export all or a selection of the groups.
get()  : WP_REST_Response|WP_Error
Retrieve a group by ID.
get_all()  : WP_REST_Response|WP_Error
Retrieve all groups.
get_categories()  : WP_REST_Response|WP_Error
Get a formatted list of product categories.
get_products()  : WP_REST_Response|WP_Error
Get a formatted list of products.
get_visibility()  : mixed
get_visibility_objects()  : mixed
import()  : WP_REST_Response
Process the import of groups from a JSON file.
import_groups()  : WP_REST_Response
Import groups into the database, based on the preflight data.
import_preflight()  : WP_REST_Response
Preflight import of groups.
permission_callback()  : bool
Permission callback to access the routes.
process_options_data()  : array<string|int, mixed>
Process the options data for import.
register_routes()  : mixed
Register the REST routes.
reorder()  : WP_REST_Response|WP_Error
Reorder the groups
toggle()  : WP_REST_Response
Set the group as enabled or disabled.
update()  : WP_REST_Response|WP_Error
Update a group
delete_missing_options()  : void
Determine which options were deleted by their lack of presence in the request.
find_duplicate_group()  : int|bool
Find a group with the same name and options.
get_group_schema()  : mixed
Retrieves the schema for the update and create endpoints.
import_source_image_data()  : mixed
Download an image from its original location and upload it to the media library.
process_category_array()  : array<string|int, mixed>
Process an array of category IDs to include additional information.
process_image_array()  : array<string|int, mixed>
Process an array of image IDs to include additional information.
process_product_array()  : array<string|int, mixed>
Process an array of product IDs to include additional information.
remap_imported_categories()  : array<string|int, mixed>
Remap the imported categories to the correct category IDs.
remap_imported_image()  : int|bool
Try to remap the imported image IDs to the correct image IDs.
remap_imported_products()  : array<string|int, mixed>
Remap the imported products to the correct product IDs.
run_group_addition()  : Group|false
Perform the addition of a group
update_formula_options()  : array<string|int, mixed>|null
Check if there are price formulas that need validity checks.
validate_formula_options()  : array<string|int, mixed>|null
Check if there are price formulas that need validity checks.

Properties

$rest_base

protected mixed $rest_base = 'groups'
Tags

Methods

compare_options()

public compare_options(mixed $option_1, mixed $option_2) : mixed
Parameters
$option_1 : mixed
$option_2 : mixed
Tags
Return values
mixed

create()

Create a group

public create(WP_REST_Request $request) : WP_REST_Response|WP_Error
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error

delete()

Delete a group

public delete(WP_REST_Request $request) : WP_REST_Response|WP_Error
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error

duplicate()

Duplicate a group

public duplicate(WP_REST_Request $request) : WP_REST_Response|WP_Error
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error

export()

Export all or a selection of the groups.

public export(WP_REST_Request $request) : WP_REST_Response|WP_Error

The export process expands product and category IDs to include additional information that can be used to remap products and categories during the import process.

Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error

get()

Retrieve a group by ID.

public get(WP_REST_Request $request) : WP_REST_Response|WP_Error
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error

get_all()

Retrieve all groups.

public get_all(WP_REST_Request $request) : WP_REST_Response|WP_Error
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error

get_categories()

Get a formatted list of product categories.

public get_categories(WP_REST_Request $request) : WP_REST_Response|WP_Error

The list also include every possible translation of the categories.

Parameters
$request : WP_REST_Request
Tags
todo

This method is necessary to handle a bug in WPML (see: https://wpml.org/forums/topic/woocommerce-rest-api-include-parameter-used-with-langall-returns-incorrect-results) Once the bug is fixed, we can remove this method altogether and restore the script invoking this REST API endpoint to use the /wc/v3/products/categories one instead.

Return values
WP_REST_Response|WP_Error

get_products()

Get a formatted list of products.

public get_products(WP_REST_Request $request) : WP_REST_Response|WP_Error

The list also include every possible translation of the products.

Parameters
$request : WP_REST_Request
Tags
todo

This method is necessary to handle a bug in WPML (see: https://wpml.org/forums/topic/woocommerce-rest-api-include-parameter-used-with-langall-returns-incorrect-results) Once the bug is fixed, we can remove this method altogether and restore the script invoking this REST API endpoint to use the /wc/v3/products one instead.

Return values
WP_REST_Response|WP_Error

get_visibility()

public get_visibility(mixed $request) : mixed
Parameters
$request : mixed
Tags
Return values
mixed

get_visibility_objects()

public get_visibility_objects(mixed $group) : mixed
Parameters
$group : mixed
Tags
Return values
mixed

import()

Process the import of groups from a JSON file.

public import(WP_REST_Request $request) : WP_REST_Response

The JSON file is read on the client side and passed as part of the request body.

The process runs in two steps:

  1. If preflight is set to true, the method reads the groups from the body, checks for any conflicts with existing groups and sets flags and actions of each group being imported before returning them to the client.
  2. If preflight is not set, the actual import step reads the preflight data as edited by the user on the client side and imports the groups that are marked for import.
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response

import_groups()

Import groups into the database, based on the preflight data.

public import_groups(array<string|int, mixed> $data) : WP_REST_Response
Parameters
$data : array<string|int, mixed>

The data coming from the import preflight.

Tags
Return values
WP_REST_Response

import_preflight()

Preflight import of groups.

public import_preflight(array<string|int, mixed> $data) : WP_REST_Response

This method performs a preliminary processing of the groups being imported providing the user with information about potential conflicts and changes.

Parameters
$data : array<string|int, mixed>

The data read from an exported JSON file.

Tags
Return values
WP_REST_Response

permission_callback()

Permission callback to access the routes.

public permission_callback() : bool
Tags
Return values
bool

process_options_data()

Process the options data for import.

public process_options_data(array<string|int, mixed> $options, array<string|int, mixed> $maps) : array<string|int, mixed>
Parameters
$options : array<string|int, mixed>

The array of options to process.

$maps : array<string|int, mixed>

The maps of categories, products and images being remapped.

Tags
Return values
array<string|int, mixed>

register_routes()

Register the REST routes.

public register_routes() : mixed
Tags
Return values
mixed

reorder()

Reorder the groups

public reorder(WP_REST_Request $request) : WP_REST_Response|WP_Error
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error

toggle()

Set the group as enabled or disabled.

public toggle(WP_REST_Request $request) : WP_REST_Response
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response

update()

Update a group

public update(WP_REST_Request $request) : WP_REST_Response|WP_Error
Parameters
$request : WP_REST_Request
Tags
Return values
WP_REST_Response|WP_Error

delete_missing_options()

Determine which options were deleted by their lack of presence in the request.

private delete_missing_options(int $group_id, array<string|int, mixed> $options) : void
Parameters
$group_id : int
$options : array<string|int, mixed>
Tags
Return values
void

find_duplicate_group()

Find a group with the same name and options.

private find_duplicate_group(array<string|int, mixed> $group) : int|bool
Parameters
$group : array<string|int, mixed>
Tags
Return values
int|bool

The ID of the group if found, false otherwise.

get_group_schema()

Retrieves the schema for the update and create endpoints.

private get_group_schema() : mixed
Tags
@return

[]

Return values
mixed

import_source_image_data()

Download an image from its original location and upload it to the media library.

private import_source_image_data(string $media) : mixed
Parameters
$media : string

The array with the media information.

Tags
Return values
mixed

process_category_array()

Process an array of category IDs to include additional information.

private process_category_array(array<string|int, mixed> $category_ids[, string|bool $column = false ]) : array<string|int, mixed>
Parameters
$category_ids : array<string|int, mixed>
$column : string|bool = false

The column where category IDs are stored or false to return the full category array.

Tags
Return values
array<string|int, mixed>

process_image_array()

Process an array of image IDs to include additional information.

private process_image_array(array<string|int, mixed> $image_ids) : array<string|int, mixed>
Parameters
$image_ids : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>

process_product_array()

Process an array of product IDs to include additional information.

private process_product_array(array<string|int, mixed> $product_ids[, string|bool $column = false ]) : array<string|int, mixed>
Parameters
$product_ids : array<string|int, mixed>
$column : string|bool = false

The column where product IDs are stored or false to return the full product array.

Tags
Return values
array<string|int, mixed>

remap_imported_categories()

Remap the imported categories to the correct category IDs.

private remap_imported_categories(mixed $category_ids[, mixed $category_map = [] ]) : array<string|int, mixed>
Parameters
$category_ids : mixed
$category_map : mixed = []
Tags
Return values
array<string|int, mixed>

remap_imported_image()

Try to remap the imported image IDs to the correct image IDs.

private remap_imported_image(mixed $media) : int|bool

The remapping is based on the image URL. If the database contains an image attachment with the same basename, we check if file size, dimensions and mime type all match. If they do, we assume that the image is the same and remap the ID. If they don't, we try to download the image from the URL and re-upload it to the media library. If the image can't be downloaded or uploaded, we skip the image and return false so that the error can be reported to the user.

Parameters
$media : mixed
Tags
Return values
int|bool

The remapped image ID if successful, false otherwise.

remap_imported_products()

Remap the imported products to the correct product IDs.

private remap_imported_products(array<string|int, mixed> $product_ids[, array<string|int, mixed> $product_map = [] ]) : array<string|int, mixed>
Parameters
$product_ids : array<string|int, mixed>

An array of products with product_id, sku, name and type.

$product_map : array<string|int, mixed> = []

An array of product info to remap to.

Tags
Return values
array<string|int, mixed>

run_group_addition()

Perform the addition of a group

private run_group_addition(array<string|int, mixed> $data) : Group|false
Parameters
$data : array<string|int, mixed>

The data of the group to be added.

Tags
Return values
Group|false

update_formula_options()

Check if there are price formulas that need validity checks.

private update_formula_options(Group $group) : array<string|int, mixed>|null
Parameters
$group : Group

The group to update the formula options for.

Tags
Return values
array<string|int, mixed>|null

Array of warning messages or null if no warnings.

validate_formula_options()

Check if there are price formulas that need validity checks.

private validate_formula_options(Group $group) : array<string|int, mixed>|null
Parameters
$group : Group
Tags
Return values
array<string|int, mixed>|null

Array of warning messages or null if no warnings.

Search results