args = $args; $this->resource_content_callback = $resource_content_callback; $this->validate_arguments(); $this->validate_resource_content_callback(); $this->register_resource(); } /** * Register the resource. * * @return void */ private function register_resource(): void { WPMCP()->register_resource( $this->args ); WPMCP()->register_resource_callback( $this->args['uri'], $this->resource_content_callback ); } /** * Validate the arguments. * * @throws InvalidArgumentException When validation fails. * @return void */ private function validate_arguments(): void { if ( ! isset( $this->args['uri'] ) || empty( $this->args['uri'] ) ) { throw new InvalidArgumentException( 'Resource URI is required' ); } if ( ! isset( $this->args['name'] ) || empty( $this->args['name'] ) ) { throw new InvalidArgumentException( 'Resource name is required' ); } // Validate URI format. if ( ! filter_var( $this->args['uri'], FILTER_VALIDATE_URL ) && ! preg_match( '/^WordPress:\/\//', $this->args['uri'] ) ) { throw new InvalidArgumentException( 'Invalid resource URI format. Must follow WordPress://[host]/[path] format' ); } // Validate the MIME type if provided. if ( isset( $this->args['mimeType'] ) && ! empty( $this->args['mimeType'] ) ) { if ( ! in_array( $this->args['mimeType'], $this->accepted_mime_types, true ) ) { throw new InvalidArgumentException( 'Invalid MIME type format. Accepted mime types are: ' . esc_html( implode( ', ', $this->accepted_mime_types ) ) ); } } // Ensure no trailing whitespace in strings. foreach ( array( 'uri', 'name', 'description', 'mimeType' ) as $field ) { if ( isset( $this->args[ $field ] ) ) { $this->args[ $field ] = trim( $this->args[ $field ] ); } } } /** * Validate the resource content callback. * * @throws InvalidArgumentException When validation fails. * @return void */ private function validate_resource_content_callback(): void { if ( ! is_callable( $this->resource_content_callback ) ) { throw new InvalidArgumentException( 'Resource content callback must be a callable' ); } } }