setting. "form:cf7" toggles one form plugin. */ private static function switches() { return array( 'any_form', 'capture_registrations', 'bookings' ); } /** * Tile colours for integrations, so each keeps the same look. Initials stand in for logos. * * @param string $key Integration key. */ private static function tone( $key ) { $tones = array( 'cf7' => '#2E6BB5', 'wpforms' => '#E27730', 'gravity' => '#F15A2B', 'ninja' => '#EF4748', 'elementor' => '#92003B', 'fluent' => '#1A7EFB', 'formidable' => '#4199FD', 'motopress' => '#2B6CB0', 'latepoint' => '#1D7BFF', 'amelia' => '#1A84EE', 'ssa' => '#0C9A8C', 'woo' => '#7F54B3', 'wp' => '#3858E9', 'any' => '#636366', 'fluentcrm' => '#6366F1', 'groundhogg' => '#F58220', 'jetpackcrm' => '#069E08', 'webhook' => '#30B0C7', 'phone' => '#34C759', 'ai' => '#AF52DE', 'nodebook' => '#1C1C1E', 'bookings' => '#FF9500', ); return $tones[ $key ] ?? '#8E8E93'; } /** Two letters for a tile: "Contact Form 7" โ†’ "CF", "WooCommerce" โ†’ "Wo". */ private static function initials( $name ) { $words = preg_split( '/[\s\-]+/u', trim( (string) $name ), -1, PREG_SPLIT_NO_EMPTY ); if ( count( $words ) > 1 ) { return mb_strtoupper( mb_substr( $words[0], 0, 1 ) . mb_substr( $words[1], 0, 1 ) ); } return mb_substr( (string) $name, 0, 2 ); } /** * Submissions, bookings or orders that came in through a source in the last 30 days. * * @param string $source Source label stored on the activity (e.g. "Contact Form 7"). * @param array $types Activity types. */ private static function count( $source, array $types ) { global $wpdb; $since = gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ); $like = '%' . $wpdb->esc_like( '"source":' . wp_json_encode( $source ) ) . '%'; $rows = array(); foreach ( $types as $type ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table. $rows[] = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE type = %s AND created_at >= %s AND data LIKE %s', Nodebook_Activities::table(), $type, $since, $like ) ); } return array_sum( $rows ); } /** Orders on the timeline in the last 30 days. */ private static function orders() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE type = 'order' AND created_at >= %s", Nodebook_Activities::table(), gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ) ) ); } /** New contacts that came from WordPress sign-ups in the last 30 days. */ private static function signups() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE type = 'signup' AND created_at >= %s", Nodebook_Activities::table(), gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ) ) ); } /** * "12 submissions in the last 30 days" or "No submissions in the last 30 days". * * @param int $n Number. * @param string $kind submission, booking, order or signup. */ private static function came( $n, $kind ) { $n = (int) $n; switch ( $kind ) { case 'booking': /* translators: %s: number */ return $n ? sprintf( _n( '%s booking in the last 30 days', '%s bookings in the last 30 days', $n, 'nodebook' ), number_format_i18n( $n ) ) : __( 'No bookings in the last 30 days', 'nodebook' ); case 'order': /* translators: %s: number */ return $n ? sprintf( _n( '%s order in the last 30 days', '%s orders in the last 30 days', $n, 'nodebook' ), number_format_i18n( $n ) ) : __( 'No orders in the last 30 days', 'nodebook' ); case 'signup': /* translators: %s: number */ return $n ? sprintf( _n( '%s sign-up in the last 30 days', '%s sign-ups in the last 30 days', $n, 'nodebook' ), number_format_i18n( $n ) ) : __( 'No sign-ups in the last 30 days', 'nodebook' ); default: /* translators: %s: number */ return $n ? sprintf( _n( '%s submission in the last 30 days', '%s submissions in the last 30 days', $n, 'nodebook' ), number_format_i18n( $n ) ) : __( 'No submissions in the last 30 days', 'nodebook' ); } } public static function page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Only administrators can change integrations.', 'nodebook' ), 403 ); } $s = Nodebook_Settings::all(); $forms = array(); $later = array(); foreach ( Nodebook_Forms::integrations() as $key => $int ) { if ( call_user_func( $int[1] ) ) { $forms[ $key ] = $int[0]; } else { $later[] = $int[0]; } } $books = array(); $blate = array(); foreach ( Nodebook_Bookings::integrations() as $key => $int ) { if ( call_user_func( $int[1] ) ) { $books[ $key ] = $int[0]; } else { $blate[] = $int[0]; } } $crms = array(); foreach ( Nodebook_Import::available() as $key => $cls ) { $crms[ $key ] = call_user_func( array( $cls, 'label' ) ); } // Headline: how many are on, and what came in. $on = 0; $total = 0; foreach ( $forms as $key => $name ) { $on += in_array( $key, (array) $s['forms'], true ) ? 1 : 0; $total += self::count( $name, array( 'form' ) ); } foreach ( $books as $name ) { $on += ! empty( $s['bookings'] ) ? 1 : 0; $total += self::count( $name, array( 'booking', 'appointment' ) ); } $woo = Nodebook_Woocommerce::active(); if ( $woo ) { $on += 'off' !== Nodebook_Woocommerce::mode() ? 1 : 0; $total += self::orders(); } $on += ! empty( $s['any_form'] ) ? 1 : 0; $on += ! empty( $s['capture_registrations'] ) ? 1 : 0; $total += self::signups(); Nodebook_Admin::header( __( 'Integrations', 'nodebook' ), array(), array( 'sub' => __( 'What Nodebook is connected to. Switch things on or off here; the details live in Settings.', 'nodebook' ) ) ); echo '
'; echo '
' . wp_kses( Nodebook_Icons::svg( 'check', 13 ), Nodebook_Icons::kses() ) . '' . esc_html__( 'Switched on', 'nodebook' ) . '' . esc_html( number_format_i18n( $on ) ) . '' . esc_html__( 'Bringing people into Nodebook', 'nodebook' ) . '
'; echo '
' . wp_kses( Nodebook_Icons::svg( 'inbox', 13 ), Nodebook_Icons::kses() ) . '' . esc_html__( 'Came in', 'nodebook' ) . '' . esc_html( number_format_i18n( $total ) ) . '' . esc_html__( 'Forms, bookings, orders and sign-ups in 30 days', 'nodebook' ) . '
'; echo '
'; // Forms. self::group_start( 'forms', __( 'Forms', 'nodebook' ) ); foreach ( $forms as $key => $name ) { $is = in_array( $key, (array) $s['forms'], true ); self::row( $key, $name, $is ? self::came( self::count( $name, array( 'form' ) ), 'submission' ) : __( 'Off: submissions are not saved', 'nodebook' ), 'form:' . $key, $is ); } self::row( 'any', __( 'Any other form', 'nodebook' ), __( 'Theme, page-builder and hand-made HTML forms with an email or phone field', 'nodebook' ), 'any_form', ! empty( $s['any_form'] ) ); if ( $later ) { self::note( /* translators: %s: list of plugins */ sprintf( __( 'Also works with %s: captured as soon as one is installed.', 'nodebook' ), implode( ', ', $later ) ) ); } self::group_end( Nodebook_Admin::url( 'nodebook-settings' ) . '#nb-capture', __( 'Field matching and what to do with existing contacts', 'nodebook' ) ); // Bookings. self::group_start( 'bookings', __( 'Bookings and appointments', 'nodebook' ) ); if ( $books ) { self::row( 'bookings', __( 'Bookings on the timeline', 'nodebook' ), __( 'Bookings and appointments become contacts, with the property, service or room they booked.', 'nodebook' ), 'bookings', ! empty( $s['bookings'] ) ); foreach ( $books as $key => $name ) { self::row( $key, $name, ! empty( $s['bookings'] ) ? self::came( self::count( $name, array( 'booking', 'appointment' ) ), 'booking' ) : __( 'Off: bookings are not saved', 'nodebook' ), '', ! empty( $s['bookings'] ) ); } } else { self::note( __( 'No booking plugin found.', 'nodebook' ) ); } if ( $blate ) { self::note( /* translators: %s: list of plugins */ sprintf( __( 'Also works with %s.', 'nodebook' ), implode( ', ', $blate ) ) ); } self::group_end( '', '' ); // Shop and site. self::group_start( 'site', __( 'Shop and site', 'nodebook' ) ); if ( $woo ) { $modes = array( 'orders' => __( 'Every buyer becomes a contact at checkout', 'nodebook' ), 'paid' => __( 'Buyers become contacts once an order is paid', 'nodebook' ), 'existing' => __( 'Orders are added to people already in Nodebook', 'nodebook' ), 'off' => __( 'Off: nothing from WooCommerce', 'nodebook' ), ); $mode = Nodebook_Woocommerce::mode(); self::row( 'woo', 'WooCommerce', ( $modes[ $mode ] ?? '' ) . ( 'off' !== $mode ? ' ยท ' . self::came( self::orders(), 'order' ) : '' ), '', 'off' !== $mode, array( __( 'Change', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-settings' ) . '#nb-woo' ) ); } self::row( 'wp', __( 'WordPress sign-ups', 'nodebook' ), ! empty( $s['capture_registrations'] ) ? self::came( self::signups(), 'signup' ) : __( 'Off: new users are not added', 'nodebook' ), 'capture_registrations', ! empty( $s['capture_registrations'] ), array( __( 'Import existing users', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-tools' ) . '#nb-users' ) ); self::group_end( '', '' ); // Connect anything. self::group_start( 'connect', __( 'Connect anything', 'nodebook' ) ); echo '
  • ' . self::tile( 'webhook', '' ) . '
    ' . esc_html__( 'Inbound webhook', 'nodebook' ) . '' . esc_html__( 'Send leads and bookings from Zapier, Make, n8n, a channel manager or any other system.', 'nodebook' ) . '
    ' . esc_html__( 'POST JSON or form fields; email is required. Name, phone, company, type (form, booking, appointment), title, value, currency, check_in and check_out are recognised.', 'nodebook' ) . ' ' . esc_html__( 'Generate a new URL', 'nodebook' ) . '
  • '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- tile() escapes. self::row( 'phone', __( 'Phone app', 'nodebook' ), __( 'Today, follow-ups and contacts in your pocket, with one-tap call and WhatsApp.', 'nodebook' ), '', null, array( __( 'Connect a phone', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-settings' ) . '#nb-phone' ) ); $ai = class_exists( 'Nodebook_Modules' ) && Nodebook_Modules::is_enabled( 'abilities' ); self::row( 'ai', __( 'AI assistants', 'nodebook' ), $ai ? __( 'On: assistants connected to WordPress can look up and update contacts for signed-in users who may.', 'nodebook' ) : __( 'Off: let AI assistants that connect to WordPress look up contacts and add notes.', 'nodebook' ), '', $ai, array( $ai ? __( 'Manage', 'nodebook' ) : __( 'Switch on in Modules', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-modules' ) . '#module-abilities' ) ); self::group_end( '', '' ); if ( $crms ) { self::group_start( 'move', __( 'Move in from another CRM', 'nodebook' ) ); foreach ( $crms as $key => $name ) { /* translators: %s: CRM name */ self::row( $key, $name, sprintf( __( 'Found on this site. Bring over contacts, tags, lists and notes from %s in one go.', 'nodebook' ), $name ), '', null, array( __( 'Import', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-tools' ) . '#nb-move-' . $key ) ); } self::group_end( '', '' ); } do_action( 'nodebook_integrations_screen' ); Nodebook_Admin::footer(); } private static function group_start( $id, $title ) { echo '

    ' . esc_html( $title ) . '

    '; if ( $url ) { echo '

    ' . esc_html( $label ) . ' ' . wp_kses( Nodebook_Icons::svg( 'chevron-r', 14 ), Nodebook_Icons::kses() ) . '

    '; } echo '
    '; } private static function note( $text ) { echo '
  • ' . esc_html( $text ) . '
  • '; } private static function tile( $key, $name ) { $icons = array( 'any' => 'file-text', 'webhook' => 'external', 'phone' => 'phone', 'ai' => 'sparkles', 'bookings' => 'calendar', ); $inner = isset( $icons[ $key ] ) ? wp_kses( Nodebook_Icons::svg( $icons[ $key ], 18 ), Nodebook_Icons::kses() ) : esc_html( self::initials( $name ) ); if ( 'nodebook' === $key ) { $inner = 'N'; } return ''; } /** * One integration row: tile, name, what it does or brought in, and a switch or links. * * @param string $key Integration key (look of the tile). * @param string $name Name. * @param string $line Status line. * @param string $toggle Switch key ('' for none). * @param bool|null $on On or off; null when it has no state. * @param array $links label => URL. */ private static function row( $key, $name, $line, $toggle, $on, array $links = array() ) { echo '
  • ' . self::tile( $key, $name ) . '
    ' . esc_html( $name ) . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- tile() escapes. echo '' . ( true === $on ? '' : '' ) . esc_html( $line ) . '
    '; foreach ( $links as $label => $url ) { echo '' . esc_html( $label ) . ''; } if ( $toggle ) { $uid = 'nb-sw-' . sanitize_html_class( str_replace( ':', '-', $toggle ) . '-' . $key ); echo '
    '; wp_nonce_field( 'nodebook_integration' ); echo '
    '; } echo '
  • '; } /** Flip one switch and come back to its row. Only the setting named is changed. */ public static function save() { Nodebook_Admin::require_cap(); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Only administrators can change integrations.', 'nodebook' ), 403 ); } check_admin_referer( 'nodebook_integration' ); $key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; $on = ! empty( $_POST['on'] ); $back = isset( $_POST['back'] ) ? sanitize_key( wp_unslash( $_POST['back'] ) ) : ''; $all = Nodebook_Settings::all(); if ( 0 === strpos( $key, 'form:' ) ) { $form = sanitize_key( substr( $key, 5 ) ); if ( in_array( $form, Nodebook_Forms::keys(), true ) ) { $list = array_values( array_diff( (array) ( $all['forms'] ?? Nodebook_Forms::keys() ), array( $form ) ) ); $all['forms'] = $on ? array_merge( $list, array( $form ) ) : $list; update_option( Nodebook_Settings::OPTION, $all ); } } elseif ( 'outbox' === $key ) { Nodebook_Outbox::set( $on ); } elseif ( in_array( $key, self::switches(), true ) ) { $all[ $key ] = $on; update_option( Nodebook_Settings::OPTION, $all ); } Nodebook_Admin::redirect( Nodebook_Admin::url( self::PAGE ) . ( $back ? '#nb-int-' . $back : '' ), 'saved' ); } }