__( '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::desc( $desc ); echo ''; } /** Textarea row. */ public static function row_textarea( $name, $value, $label, $desc = '', $rows = 4 ) { if ( self::admin( 'textarea', array( $name, $value, $label, $desc, $rows ) ) ) { return; } $id = self::dom_id( $name ); echo ''; self::desc( $desc ); echo ''; } /** Select row. */ public static function row_select( $name, $value, $label, array $options, $desc = '' ) { if ( self::admin( 'select', array( $name, $value, $label, $options, $desc ) ) ) { return; } $id = self::dom_id( $name ); echo '' . self::select( $name, $value, $options, array( 'id' => $id ) ); // phpcs:ignore self::desc( $desc ); echo ''; } /** Toggle row. */ public static function row_toggle( $name, $checked, $label, $desc = '' ) { if ( self::admin( 'toggle', array( $name, $checked, $label, $desc ) ) ) { return; } echo '' . esc_html( $label ) . '' . self::checkbox( $name, $checked ) . ''; // phpcs:ignore self::desc( $desc ); echo ''; } /** Field picker row. */ public static function row_field( $name, $form_id, $value, $label, $filter = null, $desc = '' ) { if ( self::admin( 'field_select', array( $name, $form_id, $value, $label, $filter, $desc ) ) ) { return; } self::row_select( $name, $value, $label, array( '' => __( '— None —', 'fieldwren' ) ) + Schema::field_choices( $form_id, $filter ), $desc ); } /** Conditions row. */ public static function row_conditions( $name, $form_id, $value, $label ) { if ( self::admin( 'conditions', array( $name, $form_id, $value, $label ) ) ) { return; } $v = is_array( $value ) ? $value : array(); $rules = array_values( (array) ( $v['rules'] ?? array() ) ); $rules = array_merge( $rules, array( array(), array() ) ); $ops = \FWREN\Conditions::operators(); echo '' . esc_html( $label ) . ''; 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 ''; } /** Smartcodes picker button. */ public static function smartcodes_button( $target_id, $form_id ) { self::admin( 'smartcodes_button', array( $target_id, $form_id ) ); } /** Normalize a posted condition set to { enabled, match, rules }. */ public static function clean_conditions( $c ) { $c = is_array( $c ) ? $c : array(); $rules = array(); foreach ( (array) ( $c['rules'] ?? array() ) as $r ) { if ( is_array( $r ) && ! empty( $r['field'] ) ) { $rules[] = array( 'field' => sanitize_text_field( (string) $r['field'] ), 'operator' => sanitize_text_field( (string) ( $r['operator'] ?? '=' ) ), 'value' => sanitize_text_field( (string) ( $r['value'] ?? '' ) ), ); } } return array( 'enabled' => self::on( $c['enabled'] ?? '' ) && $rules, 'match' => 'any' === ( $c['match'] ?? 'all' ) ? 'any' : 'all', 'rules' => $rules, ); } /* ------------------------------------------------------------------ * * Markup * ------------------------------------------------------------------ */ /** @return string '; foreach ( $options as $k => $l ) { $h .= ''; } return $h . ''; } /** @return string Checkbox with a hidden 0. */ public static function checkbox( $name, $checked, $label = '' ) { $h = ''; } /** Description paragraph. */ public static function desc( $desc ) { if ( '' !== (string) $desc ) { echo '

' . wp_kses_post( $desc ) . '

'; } } /** @return string DOM id from an input name. */ public static function dom_id( $name ) { return 'fwren-' . trim( preg_replace( '/[^a-z0-9]+/i', '-', $name ), '-' ); } /** Section heading row inside a form table. */ public static function heading( $title, $desc = '' ) { echo '

' . esc_html( $title ) . '

'; self::desc( $desc ); echo ''; } /** Minimal standalone page (opt-in confirmed, approval done, entry view). */ public static function page( $title, $html, $status = 200 ) { status_header( $status ); nocache_headers(); $fwren_page_title = $title; $fwren_page_html = $html; include FWREN_PATH . 'modules/templates/message.php'; exit; } /** @return array Allowed post statuses of feeds. */ public static function roles( $exclude_admin = true ) { $out = array(); foreach ( wp_roles()->roles as $k => $r ) { if ( $exclude_admin && ( ! empty( $r['capabilities']['manage_options'] ) || ! empty( $r['capabilities']['promote_users'] ) ) ) { continue; } $out[ $k ] = translate_user_role( $r['name'] ); } return $out; } }