fieldRegistry = $fieldRegistry; $this->gate = $gate; $this->settings = $settings; } protected function getGate(): FeatureGate { return $this->gate; } /** * Register REST API routes. */ public function registerRoutes(): void { register_rest_route( self::NAMESPACE, '/fields', array( 'methods' => 'GET', 'callback' => array( $this, 'get_fields' ), 'permission_callback' => array( $this, 'check_read_permission' ), ) ); register_rest_route( self::NAMESPACE, '/templates', array( 'methods' => 'GET', 'callback' => array( $this, 'get_templates' ), 'permission_callback' => array( $this, 'check_read_permission' ), ) ); } // ──────────────────────────────────────────────────────────────────── // Handlers // ──────────────────────────────────────────────────────────────────── /** * Get all registered field definitions. */ public function get_fields( WP_REST_Request $request ): WP_REST_Response { $rateLimitResponse = $this->guardRateLimit( $request ); if ( $rateLimitResponse ) { return $rateLimitResponse; } $grouped = $request->get_param( 'grouped' ); Logger::debug( 'get_fields called with grouped=%s by user=%d', var_export( $grouped, true ), get_current_user_id() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions // Get CAPTCHA enabled states $captchaEnabled = $this->settings ? (bool) $this->settings->get( 'captcha_enabled', false ) : false; $recaptchaEnabled = $this->settings ? (bool) $this->settings->get( 'captcha_recaptcha_enabled', false ) : false; $hcaptchaEnabled = $this->settings ? (bool) $this->settings->get( 'captcha_hcaptcha_enabled', false ) : false; $turnstileEnabled = $this->settings ? (bool) $this->settings->get( 'captcha_turnstile_enabled', false ) : false; if ( $grouped === 'true' || $grouped === '1' ) { $fields = $this->fieldRegistry->byCategory(); $fields = array_map( function ( $categoryFields ) use ( $captchaEnabled, $recaptchaEnabled, $hcaptchaEnabled, $turnstileEnabled ) { return array_map( function ( $field ) use ( $captchaEnabled, $recaptchaEnabled, $hcaptchaEnabled, $turnstileEnabled ) { $fieldArray = $field->toArray(); if ( $field->type === 'recaptcha' ) { $fieldArray['enabled'] = $captchaEnabled && $recaptchaEnabled; } elseif ( $field->type === 'hcaptcha' ) { $fieldArray['enabled'] = $captchaEnabled && $hcaptchaEnabled; } elseif ( $field->type === 'turnstile' ) { $fieldArray['enabled'] = $captchaEnabled && $turnstileEnabled; } else { $fieldArray['enabled'] = true; } return $fieldArray; }, $categoryFields ); }, $fields ); } else { $fields = $this->fieldRegistry->toArray(); $fields = array_map( function ( $fieldArray ) use ( $captchaEnabled, $recaptchaEnabled, $hcaptchaEnabled, $turnstileEnabled ) { if ( isset( $fieldArray['type'] ) ) { if ( $fieldArray['type'] === 'recaptcha' ) { $fieldArray['enabled'] = $captchaEnabled && $recaptchaEnabled; } elseif ( $fieldArray['type'] === 'hcaptcha' ) { $fieldArray['enabled'] = $captchaEnabled && $hcaptchaEnabled; } elseif ( $fieldArray['type'] === 'turnstile' ) { $fieldArray['enabled'] = $captchaEnabled && $turnstileEnabled; } else { $fieldArray['enabled'] = true; } } return $fieldArray; }, $fields ); } Logger::debug( 'get_fields response: %s', print_r( $fields, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions return ApiResponse::success( $fields ); } /** * Get form templates. */ public function get_templates( WP_REST_Request $request ): WP_REST_Response { $rateLimitResponse = $this->guardRateLimit( $request ); if ( $rateLimitResponse ) { return $rateLimitResponse; } // Every template is usable. Add-on templates are listed because the // add-on that registered them is active. $templates = \SubtleForms\Templates\FormTemplates::get_all(); return ApiResponse::success( array( 'success' => true, 'templates' => $templates, ) ); } }