formsRepo = $formsRepo; $this->settings = $settings; $this->captchaManager = $captchaManager; } /** * Register [subtleforms] shortcode. */ public function register(): void { add_shortcode( 'subtleforms', array( $this, 'render' ) ); // Forms found before the page is drawn get their assets in the head // (2.6.0). Every other form still gets them when it renders. add_action( 'wp_enqueue_scripts', array( $this, 'enqueueDetected' ), 20 ); } /** * Enqueue the assets for the published forms this page is known to show * (FormDetector). Pages without a form enqueue nothing. */ public function enqueueDetected(): void { if ( is_admin() ) { return; } foreach ( FormDetector::formIdsOnPage() as $formId ) { $form = $this->formsRepo->find( $formId ); if ( is_array( $form ) && 'published' === ( $form['status'] ?? '' ) ) { $this->enqueueAssets( $formId ); $schema = \SubtleForms\Engine\FormRestrictions::activeSchema( $formId ); $schema = is_array( $schema ) ? $schema : null; // A closed or login-only form shows a message, not the form. if ( null === \SubtleForms\Engine\FormRestrictions::blockedReason( $formId, $schema ) ) { // A page that carries the form's data needs no hint to fetch it (2.6.1). if ( ! InlineData::enabled() ) { FrontendAssets::preloadSchema( $formId ); } FrontendAssets::preloadFieldCode( $schema ); } } } } /** * What a visitor sees for a form that is missing, a draft or in the trash. * One message for all three, so it does not say which forms exist. */ private static function unavailable(): string { return '
' . esc_html__( 'Form not available.', 'subtleforms' ) . '
'; } /** * Render form shortcode. */ public function render( $atts ): string { $atts = shortcode_atts( array( 'id' => 0, ), $atts ); $formId = intval( $atts['id'] ); if ( $formId <= 0 ) { return '' . esc_html__( 'Invalid form ID.', 'subtleforms' ) . '
'; } // Verify form exists $form = $this->formsRepo->find( $formId ); if ( ! $form ) { return self::unavailable(); } // Only render published forms on frontend if ( $form['status'] !== 'published' ) { return self::unavailable(); } // Login, schedule and entry-limit rules (2.0). $blocked = \SubtleForms\Engine\FormRestrictions::blockedReason( $formId, \SubtleForms\Engine\FormRestrictions::activeSchema( $formId ) ); if ( $blocked !== null ) { $this->enqueueAssets( $formId ); return \SubtleForms\Engine\FormRestrictions::render( $blocked ); } // Enqueue frontend assets $this->enqueueAssets( $formId ); // Render container, at the estimated height of the form so the page // does not jump when the form appears (UX2-27). $schema = \SubtleForms\Engine\FormRestrictions::activeSchema( $formId ); $schema = is_array( $schema ) ? $schema : null; $inline = self::inlineParts( $formId, $schema ); FrontendAssets::preloadFieldCode( $schema ); FrontendAssets::loadFieldTranslations( $schema ); return sprintf( '