$def ) { if ( in_array( $key, $delete, true ) ) { Nodebook_Props::delete_def( $object, $key ); continue; } if ( isset( $custom[ $key ] ) ) { Nodebook_Props::save_def( $object, (array) $custom[ $key ], $key ); } } self::back( $object, 'saved' ); } public static function add() { self::guard( 'nodebook_field_add' ); $object = self::object_from_post(); $key = Nodebook_Props::save_def( $object, array( 'label' => isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '', 'type' => isset( $_POST['type'] ) ? sanitize_key( wp_unslash( $_POST['type'] ) ) : 'text', 'options' => isset( $_POST['options'] ) ? sanitize_textarea_field( wp_unslash( $_POST['options'] ) ) : '', ) ); if ( $key && 'contact' === $object ) { // Data already captured under this name moves into the new field. Nodebook_Props::queue( 'contact', $key, array( Nodebook_Props::get( 'contact', $key )['label'] ) ); } self::back( $object, $key ? 'fieldadded' : 'fieldfail' ); } public static function presets() { self::guard( 'nodebook_field_presets' ); $keys = Nodebook_Props::add_presets(); self::back( 'contact', $keys ? 'fieldadded' : 'fieldfail' ); } /** Turn one or more captured custom-field names into a field (new or existing). */ public static function promote() { self::guard( 'nodebook_field_promote' ); $groups = isset( $_POST['found'] ) ? array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['found'] ) ) : array(); $target = isset( $_POST['target'] ) ? sanitize_key( wp_unslash( $_POST['target'] ) ) : ''; $names = array(); foreach ( Nodebook_Props::discovered() as $g ) { if ( in_array( $g['key'], $groups, true ) ) { $names = array_merge( $names, $g['names'] ); } } if ( ! $names ) { Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-fields' ) . '#nb-found', 'fieldfail' ); } if ( '__new' === $target || '' === $target ) { $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : ''; $key = Nodebook_Props::save_def( 'contact', array( 'label' => '' !== trim( $label ) ? $label : $names[0], 'type' => isset( $_POST['type'] ) ? sanitize_key( wp_unslash( $_POST['type'] ) ) : 'text', 'options' => isset( $_POST['options'] ) ? sanitize_textarea_field( wp_unslash( $_POST['options'] ) ) : '', 'aliases' => $names, ) ); } else { $def = Nodebook_Props::get( 'contact', $target ); $key = $def ? Nodebook_Props::save_def( 'contact', array_merge( $def, array( 'aliases' => $names ) ), $target ) : ''; } if ( ! $key ) { Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-fields' ) . '#nb-found', 'fieldfail' ); } Nodebook_Props::queue( 'contact', $key, $names ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-fields' ) . '#nb-found', Nodebook_Props::pending() ? 'fieldmoving' : 'fieldmoved' ); } /* ------------------------------------------------------------------ * * Screen * ------------------------------------------------------------------ */ /** Built-in fields (renamed ones carry their original name in default_label). */ private static function defaults( $object ) { if ( 'deal' === $object ) { return array(); // Deals have no built-in fields to rename here. } return 'company' === $object ? Nodebook_Fields::company() : Nodebook_Fields::contact(); } public static function page() { if ( ! current_user_can( 'manage_options' ) ) { return; } Nodebook_Admin::header( __( 'Fields', 'nodebook' ) ); echo '

' . esc_html__( 'Add the fields your business needs, like a budget, travel dates or the number of bedrooms. They can be filled in from your forms and imports, shown as columns and used in segments. Built-in fields can be renamed or hidden.', 'nodebook' ) . '

'; if ( Nodebook_Props::pending() ) { echo '

' . esc_html__( 'Moving data from your forms into fields in the background. This page shows the result once it is done.', 'nodebook' ) . '

'; } $found = count( self::found() ); $objects = array( 'contact' => __( 'Contacts', 'nodebook' ), 'company' => __( 'Companies', 'nodebook' ), 'deal' => __( 'Deals', 'nodebook' ), ); echo ''; echo '
'; self::panel( 'contact', __( 'Contact fields', 'nodebook' ), __( 'Shown on every contact, filled in from forms and imports.', 'nodebook' ) ); self::panel( 'company', __( 'Company fields', 'nodebook' ), __( 'Shown on every company.', 'nodebook' ) ); self::panel( 'deal', __( 'Deal fields', 'nodebook' ), __( 'What you need to know about each deal, like an event date, the number of guests or the venue. Tick “On cards” to see a field on the deals board.', 'nodebook' ) ); echo '
'; Nodebook_Admin::footer(); } /** One object: add bar, your fields, built-in fields; contacts get the form fields and suggestions beside it. */ private static function panel( $object, $title, $lead ) { echo '
'; echo '
'; self::object_card( $object, $title, $lead ); echo '
'; echo '
'; } private static function side_note( $object ) { echo '

' . esc_html__( 'Good to know', 'nodebook' ) . '

'; } private static function presets_card() { $missing = Nodebook_Props::missing_presets(); if ( ! $missing || count( Nodebook_Props::defs( 'contact' ) ) >= 3 ) { return; } $niche = Nodebook_Niche::all()[ Nodebook_Niche::current() ]['label'] ?? ''; echo '

' . esc_html( $niche /* translators: %s: business type, e.g. "Vacation rentals" */ ? sprintf( __( 'Suggested for %s', 'nodebook' ), $niche ) : __( 'Suggested fields', 'nodebook' ) ) . '

'; foreach ( $missing as $label => $p ) { echo '' . esc_html( $label ) . ' ' . esc_html( Nodebook_Props::types()[ $p[0] ] ?? $p[0] ) . ''; } echo '

' . esc_html__( 'Form fields with these names (in several languages) fill them in automatically, including data you already have.', 'nodebook' ) . '

'; echo '
'; wp_nonce_field( 'nodebook_field_presets' ); echo '
'; } /** The add bar: name, type, choices. A form of its own, above the list. */ private static function add_bar( $object, $full ) { $types = Nodebook_Props::types(); echo '
'; wp_nonce_field( 'nodebook_field_add' ); echo ''; echo ''; echo ''; echo ''; if ( $full ) { echo '

' . esc_html( sprintf( /* translators: %d: maximum */ __( 'You have %d fields, the most Nodebook keeps per record. Delete one to add another.', 'nodebook' ), Nodebook_Props::MAX ) ) . '

'; } echo '
'; } private static function object_card( $object, $title, $lead ) { $builtin = self::defaults( $object ); $custom = Nodebook_Props::defs( $object ); $deal = 'deal' === $object; $types = Nodebook_Props::types(); $counts = self::counts( $object ); echo '

' . esc_html( $title ) . '

' . esc_html( $lead ) . '

'; self::add_bar( $object, count( $custom ) >= Nodebook_Props::MAX ); echo '
'; wp_nonce_field( 'nodebook_fields_save' ); if ( ! $custom ) { echo '

' . esc_html__( 'No fields of your own yet.', 'nodebook' ) . ' ' . esc_html__( 'Type a name above and pick a type: text, number, money, date, yes/no or a list of choices.', 'nodebook' ) . '

'; } else { echo '
    '; foreach ( $custom as $key => $def ) { $n = (int) ( $counts[ $key ] ?? 0 ); echo '
  1. '; echo ''; echo '
    '; echo '
    '; echo '
    ' . esc_html( $types[ $def['type'] ] ?? $def['type'] ) . ''; echo '' . esc_html( $n /* translators: %s: number of records */ ? sprintf( _n( '%s filled in', '%s filled in', $n, 'nodebook' ), number_format_i18n( $n ) ) : __( 'Not filled in yet', 'nodebook' ) ) . ''; if ( $deal ) { echo ''; } echo '
    '; if ( 'select' === $def['type'] ) { echo ''; } echo '
    '; echo ''; echo ''; echo ''; echo '
  2. '; } echo '
'; } $changed = count( Nodebook_Props::builtin( $object ) ); if ( $builtin ) { echo '
' . esc_html__( 'Built-in fields', 'nodebook' ) . ' ' . esc_html__( 'rename or hide', 'nodebook' ) . ( $changed ? ' · ' . esc_html( sprintf( /* translators: %d: number of changed fields */ _n( '%d changed', '%d changed', $changed, 'nodebook' ), $changed ) ) : '' ) . ''; echo '
'; foreach ( $builtin as $key => $def ) { if ( ! empty( $def['readonly'] ) || ( isset( $def['group'] ) && 'journey' === $def['group'] ) ) { continue; } $locked = in_array( $key, Nodebook_Props::LOCKED[ $object ], true ); $orig = $def['default_label'] ?? $def['label']; echo '
'; if ( $locked ) { echo '' . esc_html__( 'Always shown', 'nodebook' ) . ''; } else { echo ''; } echo '
'; } echo '

' . esc_html__( 'Hidden fields keep their data: they are only left out of forms and profiles.', 'nodebook' ) . '

'; } if ( $custom || $builtin ) { echo '
'; submit_button( __( 'Save fields', 'nodebook' ), 'primary', 'submit', false, array( 'data-nb-confirm-delete' => __( 'Delete the ticked fields and all of their values? This cannot be undone.', 'nodebook' ) ) ); if ( $custom ) { echo '' . esc_html__( 'Deleting one of your own fields removes its values from every record.', 'nodebook' ) . ''; } echo '
'; } echo '
'; } /** A small mark per field type. */ private static function glyph( $type ) { $map = array( 'text' => 'Aa', 'textarea' => '¶', 'number' => '#', 'money' => '¤', 'date' => '◷', 'select' => '≡', 'checkbox' => '✓', 'url' => '↗', ); return $map[ $type ] ?? 'Aa'; } /** Captured custom-field names that are not a field yet. */ private static function found() { return array_values( array_filter( Nodebook_Props::discovered(), static function ( $g ) { return ! Nodebook_Props::match( 'contact', $g['names'][0] ?? '' ); } ) ); } /** Captured custom-field names that are not a field yet. */ private static function found_card() { $found = self::found(); echo '

' . esc_html__( 'Found in your forms', 'nodebook' ) . ( $found ? ' ' . esc_html( number_format_i18n( count( $found ) ) ) . '' : '' ) . '

'; if ( ! $found ) { echo '

' . esc_html__( 'Nothing waiting. Form fields that Nodebook does not recognise are listed here, so you can turn them into fields.', 'nodebook' ) . '

'; return; } echo '

' . esc_html__( 'Your forms sent these extra fields. Tick the ones that mean the same thing (e.g. “Budget” and “Presupuesto”) and turn them into one field: the data you already have moves over, and future submissions fill it in.', 'nodebook' ) . '

'; echo '
'; wp_nonce_field( 'nodebook_field_promote' ); echo ''; echo '
'; echo '
'; echo '

' . esc_html__( 'Values that do not fit the type (e.g. “next summer” for a date) stay where they are.', 'nodebook' ) . '

'; } /** How many records have each field filled in. */ private static function counts( $object ) { global $wpdb; $out = array(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table, admin screen. foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT field_key, COUNT(*) AS n FROM %i WHERE object_type = %s GROUP BY field_key', Nodebook_Props::table(), $object ), ARRAY_A ) as $r ) { $out[ $r['field_key'] ] = (int) $r['n']; } return $out; } }