[ icon, pitch, features: [ deals, gatherings, growth ] ] */ public static function all() { $p = array( 'general' => array( 'sparkles', __( 'Leads from your forms, follow-ups and deals.', 'nodebook' ), true, false ), 'shop' => array( 'shopping-bag', __( 'Customers, orders and who comes back. No sales pipeline.', 'nodebook' ), false, false ), 'rentals' => array( 'key', __( 'Guests, the properties and dates they check, and their bookings.', 'nodebook' ), false, false ), 'realestate' => array( 'home', __( 'Buyers and renters, the listings they view, viewings and deals.', 'nodebook' ), true, false ), 'services' => array( 'heart', __( 'Clients, the treatments they read about, and appointments.', 'nodebook' ), false, false ), 'b2b' => array( 'building', __( 'Companies, quote requests and a sales pipeline.', 'nodebook' ), true, false ), 'retreats' => array( 'sun', __( 'Retreats, workshops and trips: a roster, deposits and balances.', 'nodebook' ), true, true ), ); $out = array(); foreach ( Nodebook_Niche::all() as $key => $n ) { $d = $p[ $key ] ?? array( 'sparkles', '', true, false ); $out[ $key ] = array( 'label' => $n['label'], 'icon' => $d[0], 'pitch' => $d[1], 'features' => array( 'deals' => $d[2], 'gatherings' => $d[3], 'growth' => true, ), ); } /** * Presets for each kind of business: label, icon, pitch, features (deals, gatherings, growth, and any * module ID an add-on wants switched on). * * @param array $presets niche => preset. */ return (array) apply_filters( 'nodebook_presets', $out ); } public static function done() { return (bool) get_option( self::DONE ); } /** Why we guess this kind of business, for the setup screen. */ public static function reason( $niche ) { if ( 'shop' === $niche && class_exists( 'WooCommerce' ) ) { return __( 'WooCommerce is installed', 'nodebook' ); } if ( 'rentals' === $niche ) { return __( 'a hotel booking plugin is installed', 'nodebook' ); } if ( 'services' === $niche ) { return __( 'an appointment plugin is installed', 'nodebook' ); } if ( 'realestate' === $niche ) { return __( 'your site has listings', 'nodebook' ); } return ''; } /** * What the setup shows as switched on: the site's current state once it was set up, the preset otherwise. * * @return array deals, gatherings, growth, abilities */ public static function current_features() { $preset = self::all()[ Nodebook_Niche::current() ]['features'] ?? array(); if ( ! self::done() ) { return $preset + array( 'abilities' => false ); } return array( 'deals' => Nodebook_Deals::on(), 'gatherings' => Nodebook_Modules::is_enabled( 'gatherings' ), 'growth' => (bool) Nodebook_Settings::get( 'node_growth' ), 'abilities' => Nodebook_Modules::is_enabled( 'abilities' ), ); } /** * Apply the answers of the setup. * * @param array $in niche, team_size, woo_capture, features (deals, gatherings, growth, abilities: bool), * fields (bool: add the suggested fields), personality, morning (bool). * @return array What changed, for the summary: list of short lines. */ public static function apply( array $in ) { $niches = Nodebook_Niche::all(); $s = Nodebook_Settings::all(); $done = array(); if ( isset( $in['niche'] ) && isset( $niches[ $in['niche'] ] ) ) { $s['niche'] = $in['niche']; } if ( isset( $in['team_size'] ) && in_array( $in['team_size'], array( 'solo', 'team' ), true ) ) { $s['team_size'] = $in['team_size']; } if ( isset( $in['woo_capture'] ) && in_array( $in['woo_capture'], array( 'orders', 'paid', 'off' ), true ) ) { $s['woo_capture'] = $in['woo_capture']; } $f = isset( $in['features'] ) && is_array( $in['features'] ) ? $in['features'] : array(); if ( array_key_exists( 'deals', $f ) ) { $s['deals'] = (bool) $f['deals']; } if ( array_key_exists( 'growth', $f ) ) { $s['node_growth'] = (bool) $f['growth']; } if ( isset( $in['personality'] ) && in_array( $in['personality'], array( 'playful', 'calm', 'off' ), true ) ) { $s['node_personality'] = $in['personality']; } if ( array_key_exists( 'morning', $in ) ) { $s['node_email'] = (bool) $in['morning'] && 'off' !== ( $s['node_personality'] ?? '' ); if ( $s['node_email'] && empty( $s['node_owner'] ) ) { $s['node_owner'] = get_current_user_id(); } } update_option( Nodebook_Settings::OPTION, Nodebook_Settings::sanitize( $s ) ); // Modules: switched on when asked, switched off (data kept) when not. foreach ( array( 'gatherings', 'abilities' ) as $id ) { if ( ! array_key_exists( $id, $f ) || ! Nodebook_Modules::get( $id ) ) { continue; } if ( $f[ $id ] && ! Nodebook_Modules::is_enabled( $id ) && '' === Nodebook_Modules::problem( $id ) ) { Nodebook_Modules::enable( $id ); } elseif ( ! $f[ $id ] && Nodebook_Modules::is_enabled( $id ) ) { Nodebook_Modules::disable( $id ); } } if ( ! empty( $in['fields'] ) ) { $added = Nodebook_Props::add_presets(); if ( $added ) { /* translators: %d: number of fields */ $done[] = sprintf( _n( '%d field added', '%d fields added', count( $added ), 'nodebook' ), count( $added ) ); } } update_option( self::DONE, time(), false ); Nodebook_Node::flush(); /** * The setup was applied. * * @param array $in The answers. */ do_action( 'nodebook_preset_applied', $in ); return $done; } /* ------------------------------------------------------------------ * * First visit after activation * ------------------------------------------------------------------ */ /** Called on activation: the next admin page opens the setup, once, on a fresh site. */ public static function on_activate() { if ( ! self::done() ) { set_transient( 'nodebook_welcome', 1, 5 * MINUTE_IN_SECONDS ); } } public static function maybe_redirect() { if ( ! get_transient( 'nodebook_welcome' ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- redirect decision only. if ( wp_doing_ajax() || wp_doing_cron() || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { return; } delete_transient( 'nodebook_welcome' ); if ( self::done() ) { return; } wp_safe_redirect( Nodebook_Admin::url( 'nodebook-welcome' ) ); exit; } }