__( 'Copied!', 'fieldwren' ), 'choose' => __( 'Choose image', 'fieldwren' ), ) ); if ( function_exists( 'wp_enqueue_media' ) ) { wp_enqueue_media(); } } /* ------------------------------------------------------------------ * * Tokens * ------------------------------------------------------------------ */ /** @return string 32 hex characters, unguessable. */ public static function token() { try { return bin2hex( random_bytes( 16 ) ); } catch ( \Exception $e ) { return md5( wp_generate_password( 40, true, true ) . microtime() ); } } /** @return string HMAC signature of a string. */ public static function sign( $data ) { return substr( hash_hmac( 'sha256', 'fwren|' . $data, wp_salt( 'auth' ) ), 0, 32 ); } /** @return bool Whether a signature matches. */ public static function verify( $data, $sig ) { return is_string( $sig ) && '' !== $sig && hash_equals( self::sign( $data ), $sig ); } /** @return bool 32-char hex token. */ public static function is_token( $t ) { return is_string( $t ) && (bool) preg_match( '/^[a-f0-9]{32}$/', $t ); } /* ------------------------------------------------------------------ * * Settings * ------------------------------------------------------------------ */ /** @return array Raw saved settings of a form (no defaults merged). */ public static function raw_settings( $form_id ) { $raw = get_post_meta( $form_id, '_fwren_settings', true ); $saved = is_string( $raw ) && '' !== $raw ? json_decode( $raw, true ) : ( is_array( $raw ) ? $raw : array() ); return is_array( $saved ) ? $saved : array(); } /** Replace one top-level settings key of a form, keeping everything else as saved. */ public static function save_setting( $form_id, $key, $value ) { $raw = self::raw_settings( $form_id ); $raw[ $key ] = $value; Form::save_settings( $form_id, $raw ); } /** @return array The `fwren[...]` subtree of a posted settings form, unslashed. */ public static function posted( array $post, $key ) { $fwren = isset( $post['fwren'] ) && is_array( $post['fwren'] ) ? wp_unslash( $post['fwren'] ) : array(); return isset( $fwren[ $key ] ) && is_array( $fwren[ $key ] ) ? $fwren[ $key ] : array(); } /** @return bool Truthy setting ('1', 1, true, 'yes'). */ public static function on( $v ) { return ! empty( $v ) && 'no' !== $v && '0' !== $v; } /** @return string Plain text, clean. */ public static function text( $v ) { return sanitize_text_field( is_scalar( $v ) ? (string) $v : '' ); } /** @return string Comparable option value (entities decoded, trimmed). */ public static function norm( $v ) { return trim( html_entity_decode( is_scalar( $v ) ? (string) $v : '', ENT_QUOTES, 'UTF-8' ) ); } /** @return string HTML allowed in posts. */ public static function html( $v ) { return wp_kses_post( is_scalar( $v ) ? (string) $v : '' ); } /* ------------------------------------------------------------------ * * Fields * ------------------------------------------------------------------ */ /** @return string Key of the form's first email field ('' when none). */ public static function first_email_field( $form_id ) { foreach ( Schema::fields( $form_id ) as $k => $f ) { if ( 'email' === $f['type'] ) { return $k; } } return ''; } /** @return string Email value of an entry for a field (falls back to the first email field). */ public static function email_of( $form_id, array $values, $field = '' ) { $keys = array_filter( array( $field, self::first_email_field( $form_id ) ) ); foreach ( $keys as $k ) { $v = isset( $values[ $k ] ) && is_scalar( $values[ $k ] ) ? sanitize_email( (string) $values[ $k ] ) : ''; if ( $v && is_email( $v ) ) { return $v; } } return ''; } /** @return callable Schema filter for a set of field types. */ public static function types( array $types ) { return static function ( $f ) use ( $types ) { return in_array( $f['type'], $types, true ); }; } /* ------------------------------------------------------------------ * * Admin rows (call \FWREN\Admin helpers; fallback markup when missing) * ------------------------------------------------------------------ */ /** Call an Admin helper, or the local fallback. */ private static function admin( $method, array $args ) { if ( class_exists( '\FWREN\Admin' ) && method_exists( '\FWREN\Admin', $method ) ) { call_user_func_array( array( '\FWREN\Admin', $method ), $args ); return true; } return false; } /** Text row. */ public static function row_text( $name, $value, $label, $desc = '', $type = 'text' ) { if ( self::admin( 'text', array( $name, $value, $label, $desc, $type ) ) ) { return; } $id = self::dom_id( $name ); echo '
' . self::checkbox( $name . '[enabled]', ! empty( $v['enabled'] ), __( 'Only when', 'fieldwren' ) ) . ' '; // phpcs:ignore echo self::select( $name . '[match]', $v['match'] ?? 'all', array( 'all' => __( 'all rules match', 'fieldwren' ), 'any' => __( 'any rule matches', 'fieldwren' ) ) ) . '
'; // phpcs:ignore foreach ( $rules as $i => $r ) { echo '' . self::select( $name . '[rules][' . $i . '][field]', $r['field'] ?? '', array( '' => '—' ) + Schema::field_choices( $form_id ) ); // phpcs:ignore echo ' ' . self::select( $name . '[rules][' . $i . '][operator]', $r['operator'] ?? '=', $ops ); // phpcs:ignore echo '
'; } echo '