register_category( ... ) ); add_action( 'wp_abilities_api_init', $this->register_ability( ... ) ); } /** * Registers the ability category. * * @return void */ public function register_category(): void { wp_register_ability_category( self::CATEGORY, array( 'label' => __( 'Add to Cart Text', 'add-to-cart-text' ), 'description' => __( 'Abilities of the Add to Cart Text plugin for WooCommerce button texts.', 'add-to-cart-text' ), ) ); } /** * Registers the ability. * * @return void */ public function register_ability(): void { $contexts = array_map( static fn ( Context $context ): string => $context->value, Context::cases() ); $sources = array_map( static fn ( Text_Source $source ): string => $source->value, Text_Source::cases() ); wp_register_ability( self::NAME, array( 'label' => __( 'Get button text for a product', 'add-to-cart-text' ), 'description' => __( 'Returns the effective "Add to cart" button text of a WooCommerce product for the product page or the shop, and where it comes from (product, category, product type, out of stock text or WooCommerce default).', 'add-to-cart-text' ), 'category' => self::CATEGORY, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'product_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => __( 'ID of the product or variation.', 'add-to-cart-text' ), ), 'context' => array( 'type' => 'string', 'enum' => $contexts, 'default' => Context::Single->value, 'description' => __( '"single" for the product page, "archive" for the shop, archives and blocks.', 'add-to-cart-text' ), ), ), 'required' => array( 'product_id' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'product_id' => array( 'type' => 'integer' ), 'context' => array( 'type' => 'string', 'enum' => $contexts, ), 'text' => array( 'type' => 'string', 'description' => __( 'Effective button text as shown to customers.', 'add-to-cart-text' ), ), 'source' => array( 'type' => 'string', 'enum' => $sources, ), 'source_id' => array( 'type' => array( 'integer', 'null' ), 'description' => __( 'Product ID or category term ID the text comes from.', 'add-to-cart-text' ), ), 'enabled' => array( 'type' => 'boolean' ), ), 'required' => array( 'product_id', 'context', 'text', 'source', 'source_id', 'enabled' ), ), 'execute_callback' => $this->execute( ... ), 'permission_callback' => $this->can_execute( ... ), 'meta' => array( 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ), 'show_in_rest' => true, ), ) ); } /** * Permission check: shop managers and product editors. * * @param mixed $input Ability input (unused). * @return bool */ public function can_execute( mixed $input = null ): bool { unset( $input ); // phpcs:ignore WordPress.WP.Capabilities.Unknown -- Registered by WooCommerce. return current_user_can( 'edit_products' ) || current_user_can( 'manage_woocommerce' ); } /** * Executes the ability. * * @param mixed $input Validated input. * @return array|\WP_Error */ public function execute( mixed $input = null ): array|\WP_Error { $input = is_array( $input ) ? $input : array(); $product_id = absint( $input['product_id'] ?? 0 ); $context = Context::tryFrom( (string) ( $input['context'] ?? '' ) ) ?? Context::Single; $product = $product_id > 0 ? wc_get_product( $product_id ) : null; if ( ! $product instanceof \WC_Product ) { return new \WP_Error( 'adtct_product_not_found', __( 'Product not found.', 'add-to-cart-text' ), array( 'status' => 404 ) ); } // Per-product check: roles with edit_products alone must not read other people's drafts or private products. $owner_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(); if ( ! current_user_can( 'edit_post', $owner_id ) ) { return new \WP_Error( 'adtct_forbidden', __( 'You are not allowed to view this product.', 'add-to-cart-text' ), array( 'status' => 403 ) ); } $resolution = $this->resolver->resolve( $product, $context ); $text = Context::Single === $context ? $product->single_add_to_cart_text() : $product->add_to_cart_text(); return array( 'product_id' => $product_id, 'context' => $context->value, 'text' => trim( wp_strip_all_tags( (string) $text ) ), 'source' => $resolution->source->value, 'source_id' => $resolution->source_id, 'enabled' => $this->settings->is_enabled(), ); } }