[ code => label ]. */ public static function catalog( $form_id = 0 ) { $inputs = array(); if ( $form_id ) { foreach ( Schema::fields( $form_id ) as $k => $f ) { $inputs[ '{inputs.' . $k . '}' ] = Schema::label( $f ); } } return apply_filters( 'fwren/smartcodes', array( __( 'Fields', 'fieldwren' ) => $inputs, __( 'Entry', 'fieldwren' ) => array( '{all_data}' => __( 'All submitted data (table)', 'fieldwren' ), '{all_data_text}' => __( 'All submitted data (plain text)', 'fieldwren' ), '{form_title}' => __( 'Form title', 'fieldwren' ), '{form_id}' => __( 'Form ID', 'fieldwren' ), '{entry.id}' => __( 'Entry ID', 'fieldwren' ), '{entry.serial}' => __( 'Entry number', 'fieldwren' ), '{entry.admin_url}' => __( 'Entry link (admin)', 'fieldwren' ), '{entry.view_url}' => __( 'Entry link (submitter)', 'fieldwren' ), '{entry.date}' => __( 'Submission date', 'fieldwren' ), '{entry.source_url}' => __( 'Page the form was submitted from', 'fieldwren' ), '{entry.ip}' => __( 'Submitter IP', 'fieldwren' ), '{payment.total}' => __( 'Payment total', 'fieldwren' ), '{payment.status}' => __( 'Payment status', 'fieldwren' ), '{quiz.score}' => __( 'Quiz score', 'fieldwren' ), '{quiz.grade}' => __( 'Quiz grade', 'fieldwren' ), ), __( 'User', 'fieldwren' ) => array( '{user.ID}' => 'ID', '{user.display_name}' => __( 'Display name', 'fieldwren' ), '{user.first_name}' => __( 'First name', 'fieldwren' ), '{user.last_name}' => __( 'Last name', 'fieldwren' ), '{user.user_email}' => __( 'Email', 'fieldwren' ), '{user.user_login}' => __( 'Username', 'fieldwren' ), '{user.meta.KEY}' => __( 'User meta', 'fieldwren' ), ), __( 'Page & request', 'fieldwren' ) => array( '{embed_post.ID}' => __( 'Page ID', 'fieldwren' ), '{embed_post.title}' => __( 'Page title', 'fieldwren' ), '{embed_post.permalink}' => __( 'Page URL', 'fieldwren' ), '{embed_post.meta.KEY}' => __( 'Page meta', 'fieldwren' ), '{get.PARAM}' => __( 'URL query parameter', 'fieldwren' ), '{cookie.NAME}' => __( 'Cookie', 'fieldwren' ), '{http_referer}' => __( 'Referrer', 'fieldwren' ), '{ip}' => __( 'IP address', 'fieldwren' ), '{browser.name}' => __( 'Browser', 'fieldwren' ), '{browser.platform}' => __( 'Platform', 'fieldwren' ), ), __( 'Site', 'fieldwren' ) => array( '{wp.site_title}' => __( 'Site title', 'fieldwren' ), '{wp.site_url}' => __( 'Site URL', 'fieldwren' ), '{wp.admin_email}' => __( 'Admin email', 'fieldwren' ), '{date.Y-m-d}' => __( 'Date (any PHP format after the dot)', 'fieldwren' ), '{random_string}' => __( 'Random string', 'fieldwren' ), ), ), $form_id ); } /** * Codes that need no entry. * * @param string $str Text. * @param int $form_id Form id. * @param array $extra Extra code => value pairs. * @return string */ public static function parse_default( $str, $form_id = 0, array $extra = array() ) { $str = (string) $str; if ( false === strpos( $str, '{' ) ) { return $str; } return preg_replace_callback( '/\{([a-z_]+)(?:\.([^{}\s]+))?\}/i', static function ( $m ) use ( $form_id, $extra ) { $full = $m[0]; if ( array_key_exists( $full, $extra ) ) { return (string) $extra[ $full ]; } $v = self::resolve_default( strtolower( $m[1] ), $m[2] ?? '', $form_id ); return null === $v ? $full : $v; }, $str ); } /** @return string|null Value of a request-level code, null when unknown. */ private static function resolve_default( $group, $key, $form_id ) { switch ( $group ) { case 'user': $u = wp_get_current_user(); if ( ! $u || ! $u->ID ) { return ''; } if ( 0 === strpos( $key, 'meta.' ) ) { return (string) get_user_meta( $u->ID, substr( $key, 5 ), true ); } if ( in_array( $key, array( 'first_name', 'last_name', 'description', 'nickname' ), true ) ) { return (string) get_user_meta( $u->ID, $key, true ); } return isset( $u->$key ) && is_scalar( $u->$key ) ? (string) $u->$key : ( 'email' === $key ? $u->user_email : '' ); case 'embed_post': $id = (int) get_queried_object_id(); if ( ! $id && isset( $_POST['_fwren_embed'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification $id = absint( $_POST['_fwren_embed'] ); // phpcs:ignore WordPress.Security.NonceVerification } if ( ! $id ) { return ''; } if ( 0 === strpos( $key, 'meta.' ) ) { return (string) get_post_meta( $id, substr( $key, 5 ), true ); } switch ( $key ) { case 'ID': case 'id': return (string) $id; case 'title': return get_the_title( $id ); case 'permalink': case 'url': return (string) get_permalink( $id ); } return ''; case 'get': return isset( $_GET[ $key ] ) ? sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) : ''; // phpcs:ignore case 'cookie': return isset( $_COOKIE[ $key ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $key ] ) ) : ''; case 'http_referer': return isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; case 'ip': return Submission::ip(); case 'browser': $b = self::browser(); return $b[ $key ] ?? ''; case 'wp': switch ( $key ) { case 'site_title': return get_bloginfo( 'name' ); case 'site_url': return home_url( '/' ); case 'admin_email': return (string) get_option( 'admin_email' ); } return ''; case 'date': return wp_date( $key ? $key : get_option( 'date_format' ) ); case 'random_string': return wp_generate_password( 10, false ); } return null; } /** * Codes with entry data. * * @param string $str Text. * @param array $ctx [ form_id, entry (row array), values, schema, extra ]. * @param string $mode html (escaped, tables) | text | raw (unescaped). * @return string */ public static function parse( $str, array $ctx, $mode = 'html' ) { $str = (string) $str; if ( false === strpos( $str, '{' ) ) { return $str; } $form_id = (int) ( $ctx['form_id'] ?? 0 ); $schema = $ctx['schema'] ?? Schema::get( $form_id ); $values = (array) ( $ctx['values'] ?? array() ); $entry = (array) ( $ctx['entry'] ?? array() ); $extra = (array) ( $ctx['extra'] ?? array() ); return preg_replace_callback( '/\{([a-z_]+)(?:\.([^{}\s]+))?\}/i', static function ( $m ) use ( $form_id, $schema, $values, $entry, $extra, $mode ) { $full = $m[0]; if ( array_key_exists( $full, $extra ) ) { return (string) $extra[ $full ]; } $group = strtolower( $m[1] ); $key = $m[2] ?? ''; switch ( $group ) { case 'inputs': case 'labels': $parts = explode( '.', $key, 2 ); $f = $schema['fields'][ $parts[0] ] ?? null; if ( ! $f ) { return ''; } if ( 'labels' === $group ) { return 'html' === $mode ? esc_html( Schema::label( $f ) ) : Schema::label( $f ); } $v = $values[ $parts[0] ] ?? ''; if ( isset( $parts[1] ) ) { $sub = is_array( $v ) ? ( $v[ $parts[1] ] ?? '' ) : ''; $sub = is_array( $sub ) ? implode( ', ', $sub ) : (string) $sub; return 'html' === $mode ? esc_html( $sub ) : $sub; } $type = Fields::get( $f['type'] ); return $type ? $type->format( $v, $f, 'html' === $mode ? 'html' : 'text' ) : ( is_array( $v ) ? implode( ', ', $v ) : (string) $v ); case 'all_data': return self::all_data( $schema, $values, 'html' === $mode ? 'html' : 'text' ); case 'all_data_text': return self::all_data( $schema, $values, 'text' ); case 'form_title': return get_the_title( $form_id ); case 'form_id': return (string) $form_id; case 'entry': switch ( $key ) { case 'id': return (string) ( $entry['id'] ?? '' ); case 'serial': return (string) ( $entry['serial'] ?? '' ); case 'admin_url': return isset( $entry['id'] ) ? admin_url( 'admin.php?page=fwren-entries&entry=' . (int) $entry['id'] ) : ''; case 'view_url': return isset( $entry['id'] ) ? Entries::public_url( (int) $entry['id'] ) : ''; case 'date': return isset( $entry['created_at'] ) ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $entry['created_at'] . ' UTC' ) ) : ''; case 'source_url': return (string) ( $entry['source_url'] ?? '' ); case 'ip': return (string) ( $entry['ip'] ?? '' ); case 'status': return (string) ( $entry['status'] ?? '' ); } return ''; case 'payment': if ( 'total' === $key ) { return isset( $entry['payment_total'] ) ? Entries::money( (int) $entry['payment_total'], (string) ( $entry['currency'] ?? '' ), $form_id ) : ''; } if ( 'status' === $key ) { return (string) ( $entry['payment_status'] ?? '' ); } break; // Other payment.* codes come from the payments module (fwren/smartcode). } $v = apply_filters( 'fwren/smartcode', null, $group, $key, $values, $entry, $form_id, $mode ); if ( null !== $v ) { return (string) $v; } return self::parse_default( $full, $form_id ); }, $str ); } /** * Every visible, non-empty value as a table (HTML) or "Label: value" lines. * * @param array $schema Schema. * @param array $values Values. * @param string $mode html | text. * @return string */ public static function all_data( array $schema, array $values, $mode = 'html' ) { $rows = array(); foreach ( $schema['order'] as $key ) { $f = $schema['fields'][ $key ] ?? null; if ( ! $f || ! array_key_exists( $key, $values ) || ! empty( $f['sensitive'] ) ) { continue; } $type = Fields::get( $f['type'] ); if ( ! $type || $type->is_empty( $values[ $key ] ) ) { continue; } $rows[] = array( Schema::label( $f ), $type->format( $values[ $key ], $f, $mode ) ); } if ( 'html' !== $mode ) { return implode( "\n", array_map( static function ( $r ) { return $r[0] . ': ' . $r[1]; }, $rows ) ); } $h = '
| ' . esc_html( $r[0] ) . ' | ' . $r[1] . ' |
|---|