array( __CLASS__, 'render_block' ) ) ); wp_set_script_translations( 'nonprofit-forms-form-editor-script', 'nonprofit-forms' ); } /** * Block render callback. * * @param array $attributes Block attributes. * @return string */ public static function render_block( $attributes ) { $id = isset( $attributes['formId'] ) ? (int) $attributes['formId'] : 0; $form = self::form( $id ); if ( '' === $form ) { return ''; } list( $classes, $style ) = self::block_style( $attributes ); $wrapper = get_block_wrapper_attributes( array( 'class' => 'npfrm-block ' . $classes, 'style' => $style, ) ); return '
' . $form . '
'; } /** * Accept only plain colour values (hex, rgb, hsl) from block attributes. * * @param mixed $value Colour. * @return string Empty when not valid. */ public static function clean_color( $value ) { $value = is_string( $value ) ? trim( $value ) : ''; return preg_match( '/^(#[0-9a-f]{3,8}|(rgb|hsl)a?\([0-9\s.,%\/deg]+\))$/i', $value ) ? $value : ''; } /** * Turn block attributes into CSS custom properties and state classes. * * @param array $a Attributes. * @return array{0:string,1:string} Classes and inline style. */ public static function block_style( array $a ) { $map = array( 'labelColor' => '--npfrm-label', 'fieldBackground' => '--npfrm-field-bg', 'fieldText' => '--npfrm-field-text', 'fieldBorder' => '--npfrm-field-border', 'buttonBackground' => '--npfrm-btn-bg', 'buttonText' => '--npfrm-btn-text', ); $style = ''; $classes = array(); foreach ( $map as $attr => $var ) { $color = isset( $a[ $attr ] ) ? self::clean_color( $a[ $attr ] ) : ''; if ( '' !== $color ) { $style .= $var . ':' . $color . ';'; } } if ( isset( $a['fieldRadius'] ) ) { $style .= '--npfrm-radius:' . min( 32, max( 0, (int) $a['fieldRadius'] ) ) . 'px;'; } if ( ! empty( $a['buttonBackground'] ) || ! empty( $a['buttonText'] ) || ( isset( $a['buttonStyle'] ) && 'outline' === $a['buttonStyle'] ) ) { $classes[] = 'has-npfrm-button-colors'; } if ( isset( $a['buttonStyle'] ) && 'outline' === $a['buttonStyle'] ) { $classes[] = 'is-npfrm-outline'; } return array( implode( ' ', $classes ), $style ); } /** * Shortcode callback. * * @param array $atts Attributes. * @return string */ public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => 0 ), $atts, 'nonprofit_form' ); return self::form( (int) $atts['id'] ); } /** * Build a form's HTML. * * @param int $id Form ID. * @return string */ public static function form( $id ) { $form = NPFRM_Form::get( $id ); if ( ! $form ) { return current_user_can( 'manage_options' ) ? '

' . esc_html__( 'Nonprofit Forms: this form was not found or is not published.', 'nonprofit-forms' ) . '

' : ''; } wp_enqueue_style( 'nonprofit-forms' ); wp_enqueue_script( 'nonprofit-forms' ); if ( ! NPFRM_Form::can_access( $form ) ) { return '

' . esc_html( $form['settings']['access_msg'] ) . '

'; } $closed = NPFRM_Form::closed_reason( $form ); // Read-only display state from the redirect after a submission. // phpcs:disable WordPress.Security.NonceVerification.Recommended $status = ''; if ( isset( $_GET['npfrm_form'] ) && (int) $_GET['npfrm_form'] === $form['id'] && isset( $_GET['npfrm_s'] ) ) { $status = sanitize_key( wp_unslash( $_GET['npfrm_s'] ) ); } $state = array( 'values' => array(), 'errors' => array(), ); if ( 'error' === $status && isset( $_GET['npfrm_k'] ) ) { $saved = get_transient( 'npfrm_state_' . sanitize_key( wp_unslash( $_GET['npfrm_k'] ) ) ); if ( is_array( $saved ) ) { $state = wp_parse_args( $saved, $state ); } } // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( '' !== $closed && 'success' !== $status ) { return '

' . esc_html( $closed ) . '

'; } if ( 'success' !== $status && ! is_user_logged_in() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) && ! is_admin() ) { NPFRM_Stats::bump( $form['id'], 'views' ); } $anchor = 'npfrm-form-' . $form['id']; $html = '
'; if ( 'success' === $status ) { return $html . '
' . esc_html( $form['settings']['success'] ) . '
'; } if ( 'error' === $status ) { $msg = ! empty( $state['general'] ) ? $state['general'] : __( 'Please fix the highlighted fields and try again.', 'nonprofit-forms' ); $html .= ''; } $return = get_permalink(); if ( ! $return ) { $return = home_url( '/' ); } $html .= '
'; $html .= ''; $html .= ''; $html .= ''; $html .= NPFRM_Spam::render_inputs( $form['id'] ); // Show the unconditional consent checkbox last, just above the privacy note and Send. $ordered = array(); $consent = array(); foreach ( $form['fields'] as $field ) { if ( 'consent' === $field['type'] && empty( $field['show_if']['field'] ) ) { $consent[] = $field; } else { $ordered[] = $field; } } $ordered = array_merge( $ordered, $consent ); foreach ( $ordered as $field ) { if ( 'consent' === $field['type'] ) { $field['privacy_url'] = NPFRM_Form::privacy_url( $form ); } $name = NPFRM_Fields::name( $field ); $value = isset( $state['values'][ $name ] ) ? $state['values'][ $name ] : ''; $error = isset( $state['errors'][ $name ] ) ? $state['errors'][ $name ] : ''; $html .= NPFRM_Fields::render( $field, $value, $form['id'], $error ); } $notice = NPFRM_Form::notice_text( $form ); if ( '' !== $notice ) { $html .= '

' . esc_html( $notice ); $purl = NPFRM_Form::privacy_url( $form ); if ( '' !== $purl ) { $html .= ' ' . esc_html__( 'Privacy Policy', 'nonprofit-forms' ) . ''; } $html .= '

'; } $html .= '
'; $html .= '
'; /** * Filter the finished form HTML. * * @param string $html Markup. * @param array $form Form. */ return apply_filters( 'npfrm_form_html', $html, $form ); } }