Files
wp-graphql-woocommerce/includes/mutation/class-product-attribute-term-create.php
Geoff TaylorandGitHub 088d337ef5 fix: address WordPress.org plugin review (rename + prefixing + headers) (#1019)
* fix: address WordPress.org plugin review feedback

- Prefix the session transaction queue transient with the plugin's
  graphql_woocommerce_ namespace instead of the generic "woo_" word, to
  avoid collisions (Plugin Directory: prefix data storage).
- Declare the WooCommerce dependency via the "Requires Plugins: woocommerce"
  plugin header.
- Bump README.txt "Tested up to" to 7.0.
- Ship composer.json in the distributed plugin (drop it, and composer.lock,
  from composer archive excludes) so the build is reproducible/reviewable.

* chore: rename plugin to "GraphQL for eCommerce" for trademark compliance

The WordPress.org plugin review flagged the display name/slug for beginning
with the "WPGraphQL" trademark (and the "WooGraphQL" portmanteau of the
WooCommerce mark), which can imply official affiliation.

- Display name (plugin header + readme title) -> "GraphQL for eCommerce".
- Slug/text domain -> "graphql-for-ecommerce" (header, all i18n string
  literals, and the PHPCS WordPress.WP.I18n text_domain config).
- Update user-facing notices/errors that named the old plugin.

"WooGraphQL" remains the project's informal nickname (repo, docs, community),
just not in the WordPress.org directory's official name/slug. Internal file
names and GitHub URLs are unchanged.

* fix: keep test-only dev deps out of the committed composer.json

The committed manifest mirrors develop (lint/stan dev deps only); CI adds the
test suite deps at runtime via `composer installTestEnv`. A previous commit
captured the installTestEnv-modified composer.json, desyncing it from
composer.lock and breaking `composer install` in CI.

* chore: regenerate composer.lock (refresh dev dependencies)

Regenerate the lock from the manifest so it is in sync (fixes the CI
`composer install` failure) and refresh dependencies in the process —
firebase/php-jwt v7.0.4 -> v7.1.0 plus 11 others, with vendor-prefixed
re-strauss'd to match. Full wpunit suite passes against the updated deps
(305 tests, 835 assertions).

* chore: rename text domain in createdVia/attribution strings from #1018

#1018 (createdVia + order attribution) merged into develop after the rename
commit was authored, so its new i18n strings still used the old
'wp-graphql-woocommerce' text domain. Update them to 'graphql-for-ecommerce'
to match the rename.
2026-06-30 10:16:21 -04:00

196 lines
5.6 KiB
PHP

<?php
/**
* Mutation - createProductAttributeTerm
*
* Registers mutation for creating a product attribute term.
*
* @package WPGraphQL\WooCommerce\Mutation
* @since 1.0.0
*/
namespace WPGraphQL\WooCommerce\Mutation;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
/**
* Class Product_Attribute_Term_Create
*/
class Product_Attribute_Term_Create {
/**
* Registers mutation
*
* @return void
*/
public static function register_mutation() {
register_graphql_mutation(
'createProductAttributeTerm',
[
'inputFields' => self::get_input_fields(),
'outputFields' => self::get_output_fields(),
'mutateAndGetPayload' => [ self::class, 'mutate_and_get_payload' ],
]
);
}
/**
* Defines the mutation input field configuration
*
* @return array
*/
public static function get_input_fields() {
return [
'attributeId' => [
'type' => [ 'non_null' => 'Int' ],
'description' => static function () {
return __( 'The ID of the attribute to which the term belongs.', 'graphql-for-ecommerce' );
},
],
'name' => [
'type' => [ 'non_null' => 'String' ],
'description' => static function () {
return __( 'The name of the term.', 'graphql-for-ecommerce' );
},
],
'slug' => [
'type' => 'String',
'description' => static function () {
return __( 'The slug of the term.', 'graphql-for-ecommerce' );
},
],
'description' => [
'type' => 'String',
'description' => static function () {
return __( 'The description of the term.', 'graphql-for-ecommerce' );
},
],
'menuOrder' => [
'type' => 'Int',
'description' => static function () {
return __( 'The order of the term in the menu.', 'graphql-for-ecommerce' );
},
],
];
}
/**
* Defines the mutation output field configuration
*
* @return array
*/
public static function get_output_fields() {
return [
'term' => [
'type' => 'ProductAttributeTermObject',
'resolve' => static function ( $payload ) {
return (object) $payload['term'];
},
],
];
}
/**
* Defines the mutation data modification closure.
*
* @param array $input Mutation input.
* @param \WPGraphQL\AppContext $context AppContext instance.
* @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo instance. Can be
* use to get info about the current node in the GraphQL tree.
*
* @throws \GraphQL\Error\UserError Invalid ID provided | Lack of capabilities.
*
* @return array
*/
public static function mutate_and_get_payload( $input, AppContext $context, ResolveInfo $info ) {
if ( ! $input['attributeId'] ) {
throw new UserError( __( 'An attributeId is required to create a new product attribute term.', 'graphql-for-ecommerce' ) );
}
$context = 'createProductAttributeTerm' === $info->fieldName ? 'create' : 'edit';
$taxonomy = wc_attribute_taxonomy_name_by_id( $input['attributeId'] );
if ( empty( $taxonomy ) ) {
throw new UserError( __( 'Invalid attributeId.', 'graphql-for-ecommerce' ) );
}
if ( ! wc_rest_check_product_term_permissions( $taxonomy, $context ) ) {
throw new UserError( __( 'Sorry, you are not allowed to create product attribute terms.', 'graphql-for-ecommerce' ) );
}
$id = isset( $input['id'] ) ? $input['id'] : null;
$args = [];
if ( ! empty( $input['description'] ) ) {
$args['description'] = $input['description'];
}
if ( ! empty( $input['slug'] ) ) {
$args['slug'] = $input['slug'];
}
if ( $id && ! empty( $input['name'] ) ) {
$args['name'] = $input['name'];
}
$term = null;
if ( $id ) {
$term = get_term( $id, $taxonomy );
}
if ( is_wp_error( $term ) ) {
throw new UserError( $term->get_error_message() );
} elseif ( $term && ! wc_rest_check_product_term_permissions( $taxonomy, $context, $term->term_id ) ) {
throw new UserError( __( 'Sorry, you are not allowed to update this product attribute term.', 'graphql-for-ecommerce' ) );
}
if ( $id ) {
$term = wp_update_term( $id, $taxonomy, $args );
} elseif ( ! empty( $input['name'] ) ) {
$name = $input['name'];
$term = wp_insert_term( $name, $taxonomy, $args );
} else {
$updating = 'updateProductAttributeTerm' === $info->fieldName;
throw new UserError(
$updating
? __( 'A name is required to create a new product attribute term.', 'graphql-for-ecommerce' )
: __( 'A valid term "id" and changeable parameter are required to update a product attribute term.', 'graphql-for-ecommerce' )
);
}
if ( is_wp_error( $term ) ) {
throw new UserError( $term->get_error_message() );
}
/**
* Newly created product attribute term.
*
* @var \WP_Term|\WP_Error|null $term
*/
$term = get_term( $term['term_id'], $taxonomy );
if ( ! $term ) {
throw new UserError( __( 'Failed to retrieve term for modification. Please check input.', 'graphql-for-ecommerce' ) );
} elseif ( is_wp_error( $term ) ) {
throw new UserError( $term->get_error_message() );
}
if ( isset( $input['menuOrder'] ) ) {
$success = update_term_meta( $term->term_id, 'order_' . $taxonomy, $input['menuOrder'] );
if ( is_wp_error( $success ) ) {
throw new UserError( $success->get_error_message() );
}
}
$menu_order = get_term_meta( $term->term_id, 'order_' . $taxonomy, true );
$data = [
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
'description' => $term->description,
'menu_order' => (int) $menu_order,
'count' => (int) $term->count,
];
return [ 'term' => $data ];
}
}