getInputs(); /* translators: %1$s reward name, %2$s redemption method, %3$s redeem type. */ $summary = sprintf( __( 'Create reward "%1$s" using %2$s / %3$s', 'aisentic' ), (string) ( $inputs['name'] ?? '(unnamed)' ), (string) ( $inputs['redeem_method'] ?? '(default method)' ), (string) ( $inputs['redeem_type'] ?? '(type not supplied)' ) ); if ( array_key_exists( 'cost_points', $inputs ) ) { /* translators: %s point cost. */ $summary .= sprintf( __( ', costing %s points', 'aisentic' ), (string) $inputs['cost_points'] ); } if ( array_key_exists( 'value', $inputs ) ) { /* translators: %s reward value. */ $summary .= sprintf( __( ', valued at %s', 'aisentic' ), (string) $inputs['value'] ); } return $summary; } public function get_approval_inputs(): array { $inputs = $this->getInputs(); foreach ( [ 'redeem_method', 'redeem_type' ] as $key ) { if ( array_key_exists( $key, $inputs ) ) { $inputs[ $key ] = (string) $inputs[ $key ] . __( ' (from live Pointics vocabulary)', 'aisentic' ); } } return $inputs; } public function __invoke( string $name, string $integration, string $redeem_type, ?string $redeem_method = null, ?int $cost_points = null, ?float $value = null, ?array $conditions = null, ?array $user_roles = null, ?array $redeem_params = null, ?string $currency = null, ?string $description = null, ?bool $is_active = null, ?int $sort_order = null, ?string $slug = null ): array { $guard = $this->guard( 'manage_pointics', 'manage_options' ); if ( null !== $guard ) { return $guard; } $ready = $this->pointics_tables_ready(); if ( null !== $ready ) { return $this->steer( $ready ); } try { $controller = pointics_container()->make( \Pointics\Api\Controllers\Rewards_Controller::class ); $methods = $this->taxonomy( $controller, 'list_methods', '/pointics/v1/rewards/methods', __( 'Failed to load live Pointics redemption methods.', 'aisentic' ) ); if ( $this->is_error( $methods ) ) { return $methods; } $types = $this->taxonomy( $controller, 'list_redeem_types', '/pointics/v1/rewards/redeem-types', __( 'Failed to load live Pointics redeem types.', 'aisentic' ) ); if ( $this->is_error( $types ) ) { return $types; } $method_ids = array_column( $methods['items'] ?? [], 'id' ); $type_ids = array_column( $types['items'] ?? [], 'id' ); if ( null !== $redeem_method && '' !== $redeem_method && ! in_array( $redeem_method, $method_ids, true ) ) { /* translators: %s redemption method ID. */ return $this->validation_error( sprintf( __( 'Unknown redeem method "%s". Use pointics-list-redemption-methods first.', 'aisentic' ), $redeem_method ) ); } if ( ! in_array( $redeem_type, $type_ids, true ) ) { /* translators: %s redeem type ID. */ return $this->validation_error( sprintf( __( 'Unknown redeem type "%s". Use pointics-list-redemption-methods first.', 'aisentic' ), $redeem_type ) ); } $duplicate = $this->find_duplicate_by_name( $controller, 'list_rewards', 'pointics-list-rewards', $name, 'reward', $slug ); if ( is_array( $duplicate ) ) { return $duplicate; } if ( null !== $duplicate ) { return $this->steer( [ /* translators: %1$s reward name, %2$d reward ID. */ 'error' => sprintf( __( 'A reward named "%1$s" already exists (ID: %2$d). Use pointics-update-reward instead of creating a duplicate.', 'aisentic' ), $name, $duplicate ), 'code' => 'logical_failure', 'status' => 409, ] ); } $body = [ 'name' => sanitize_text_field( $name ), 'integration' => sanitize_text_field( $integration ), 'redeem_type' => sanitize_text_field( $redeem_type ), ]; foreach ( [ 'redeem_method', 'currency', 'description' ] as $key ) { $value_for_key = 'redeem_method' === $key ? $redeem_method : ( 'currency' === $key ? $currency : $description ); if ( null !== $value_for_key && ! ( 'redeem_method' === $key && '' === $value_for_key ) ) { $body[ $key ] = sanitize_text_field( $value_for_key ); } } foreach ( [ 'cost_points', 'value', 'conditions', 'user_roles', 'redeem_params', 'is_active', 'sort_order', 'slug' ] as $key ) { $value_for_key = compact( 'cost_points', 'value', 'conditions', 'user_roles', 'redeem_params', 'is_active', 'sort_order', 'slug' )[ $key ]; if ( null !== $value_for_key ) { $body[ $key ] = $value_for_key; } } $request = $this->json_request( 'POST', '/pointics/v1/rewards', $body ); $data = $this->extract_response( $controller->create_reward( $request ), __( 'Failed to create the Pointics reward.', 'aisentic' ), 'pointics-create-reward' ); if ( $this->is_error( $data ) ) { return $data; } return [ 'success' => true, 'reward' => $this->shape_reward( $data ), 'message' => __( 'Pointics reward created.', 'aisentic' ), ]; } catch ( \Throwable $e ) { Log::error( 'Pointics_Create_Reward_Tool error', [ 'error' => $e->getMessage() ] ); return $this->steer( [ 'error' => __( 'Failed to create the Pointics reward.', 'aisentic' ), 'code' => 'internal', 'status' => null, ] ); } } private function taxonomy( object $controller, string $method, string $route, string $fallback ): array { $request = new \WP_REST_Request( 'GET', $route ); $data = $this->extract_response( $controller->$method( $request ), $fallback, 'pointics-list-redemption-methods' ); return $data; } private function validation_error( string $message ): array { return $this->steer( [ 'error' => $message, 'code' => 'validation', 'status' => 422, ] ); } }