$args Display overrides.
* @return string
*/
public function render( int|string $identifier, array $args = array() ): string {
$form = $this->forms->find( $identifier );
if ( ! $form || ( 'published' !== $form['status'] && ! current_user_can( 'manage_options' ) ) ) {
return current_user_can( 'manage_options' ) ? '
' . esc_html__( 'فرم پیدا نشد یا هنوز منتشر نشده است.', 'mrn-forms' ) . '
' : '';
}
wp_enqueue_style( 'mrnf-frontend', MRNF_URL . 'assets/css/frontend.css', array(), MRNF_VERSION );
wp_enqueue_script( 'mrnf-frontend', MRNF_URL . 'assets/js/frontend.js', array(), MRNF_VERSION, true );
wp_localize_script(
'mrnf-frontend',
'mrnfFrontend',
array(
'restUrl' => esc_url_raw( rest_url( 'mrn-form/v1/forms/' ) ),
'processing' => __( 'در حال ارسال…', 'mrn-forms' ),
'network' => __( 'ارتباط با سرور برقرار نشد. دوباره تلاش کنید.', 'mrn-forms' ),
'close' => __( 'بستن', 'mrn-forms' ),
/* translators: 1: field label, 2: related field label. */
'requiredWithout' => __( 'حداقل یکی از «%1$s» یا «%2$s» باید تکمیل شود.', 'mrn-forms' ),
)
);
$settings = wp_parse_args( $args, $form['settings'] );
$style = sprintf(
'--mrnf-primary:%1$s;--mrnf-accent:%2$s;--mrnf-bg:%3$s;--mrnf-text:%4$s;--mrnf-gap:%5$dpx;--mrnf-radius:%6$dpx',
esc_attr( $settings['primaryColor'] ),
esc_attr( $settings['accentColor'] ),
esc_attr( $settings['backgroundColor'] ),
esc_attr( $settings['textColor'] ),
absint( $settings['layoutGap'] ),
absint( $settings['borderRadius'] )
);
$classes = array( 'mrnf-form', 'mrnf-labels--' . sanitize_html_class( $settings['labelPosition'] ) );
if ( ! empty( $settings['buttonFullWidth'] ) ) {
$classes[] = 'mrnf-form--wide-button';
}
if ( ! empty( $settings['customClass'] ) ) {
$classes[] = sanitize_html_class( $settings['customClass'] );
}
$direction = 'auto' === $settings['direction'] ? ( is_rtl() ? 'rtl' : 'ltr' ) : $settings['direction'];
ob_start();
?>
$field Field.
* @return string
*/
private function field( array $field ): string {
$type = $field['type'];
$key = $field['key'];
$id = 'mrnf-' . $field['id'];
$required = ! empty( $field['required'] );
$condition = wp_json_encode( $field['condition'], JSON_UNESCAPED_UNICODE );
$validation = wp_json_encode( $field['validation'], JSON_UNESCAPED_UNICODE );
$required_without = sanitize_key( $field['validation']['requiredWithout'] ?? '' );
$classes = 'mrnf-field mrnf-field--' . sanitize_html_class( $type );
if ( 'heading' === $type ) {
return '' . esc_html( $field['label'] ) . '
' . ( $field['content'] ? '
' . esc_html( $field['content'] ) . '
' : '' ) . '
';
}
if ( 'html' === $type ) {
return '' . wp_kses_post( $field['content'] ) . '
';
}
ob_start();
?>
>
control( $field, $id ); ?>
$field Field.
* @param string $id HTML ID.
* @return void
*/
private function control( array $field, string $id ): void {
$type = $field['type'];
$key = $field['key'];
$name = 'mrnf[' . $key . ']';
$required = ! empty( $field['required'] );
$attrs = $required ? ' required aria-required="true"' : '';
$attrs .= $field['placeholder'] ? ' placeholder="' . esc_attr( $field['placeholder'] ) . '"' : '';
$rules = (array) $field['validation'];
if ( ! empty( $rules['minLength'] ) ) {
$attrs .= ' minlength="' . absint( $rules['minLength'] ) . '"';
}
if ( ! empty( $rules['maxLength'] ) ) {
$attrs .= ' maxlength="' . absint( $rules['maxLength'] ) . '"';
}
if ( ! empty( $rules['pattern'] ) ) {
$attrs .= ' pattern="' . esc_attr( $rules['pattern'] ) . '"';
}
if ( 'textarea' === $type ) {
echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attributes are built from escaped values.
return;
}
if ( 'select' === $type ) {
echo '';
return;
}
if ( in_array( $type, array( 'radio', 'checkbox' ), true ) ) {
echo '';
foreach ( $field['choices'] as $index => $choice ) {
$choice_id = $id . '-' . $index;
$inputname = 'checkbox' === $type ? $name . '[]' : $name;
echo '';
}
echo '
';
return;
}
if ( 'consent' === $type ) {
echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
if ( 'file' === $type ) {
$accept = implode( ',', array_map( static fn( $ext ): string => '.' . sanitize_key( $ext ), explode( ',', $rules['extensions'] ?? '' ) ) );
echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
if ( 'hidden' === $type ) {
echo '';
return;
}
$html_type = in_array( $type, array( 'text', 'email', 'tel', 'number', 'date' ), true ) ? $type : 'text';
if ( 'number' === $type ) {
$attrs .= '' !== (string) ( $rules['min'] ?? '' ) ? ' min="' . esc_attr( $rules['min'] ) . '"' : '';
$attrs .= '' !== (string) ( $rules['max'] ?? '' ) ? ' max="' . esc_attr( $rules['max'] ) . '"' : '';
}
echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}