$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' ) . '
'; 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 '' . esc_html( $lead ) . '
' . esc_html__( 'Nothing waiting. Form fields that Nodebook does not recognise are listed here, so you can turn them into fields.', 'nodebook' ) . '
' . 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 ''; } /** 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; } }