[ label, type ]. */ const FIELDS = array( 'mapp_lo_title' => array( 'Title', 'text' ), 'mapp_lo_nmls' => array( 'NMLS #', 'text' ), 'mapp_lo_phone' => array( 'Phone', 'tel' ), 'mapp_lo_email' => array( 'Email (shown to borrowers, receives notifications)', 'email' ), 'mapp_lo_notify' => array( 'Also notify (comma-separated emails)', 'text' ), 'mapp_lo_ghl_email' => array( 'GHL user email (contact owner; defaults to the email above)', 'email' ), 'mapp_lo_booking_url' => array( 'Booking link', 'url' ), 'mapp_lo_photo' => array( 'Photo URL (or set a featured image)', 'url' ), ); /** Wire hooks. */ public static function register() { add_action( 'init', array( __CLASS__, 'register_post_type' ) ); add_action( 'load-post-new.php', array( __CLASS__, 'block_new_over_limit' ) ); add_filter( 'wp_insert_post_data', array( __CLASS__, 'hold_over_limit' ), 10, 2 ); add_action( 'admin_notices', array( __CLASS__, 'limit_notice' ) ); add_action( 'add_meta_boxes_' . self::POST_TYPE, array( __CLASS__, 'add_meta_box' ) ); add_action( 'save_post_' . self::POST_TYPE, array( __CLASS__, 'save_meta' ), 10, 2 ); add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', array( __CLASS__, 'columns' ) ); add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column', array( __CLASS__, 'column' ), 10, 2 ); } /** Register the post type under the MTG Application menu. */ public static function register_post_type() { register_post_type( self::POST_TYPE, array( 'labels' => array( 'name' => __( 'Loan Officers', '1003-mortgage-application' ), 'singular_name' => __( 'Loan Officer', '1003-mortgage-application' ), 'add_new_item' => __( 'Add loan officer', '1003-mortgage-application' ), 'edit_item' => __( 'Edit loan officer', '1003-mortgage-application' ), ), 'public' => false, 'show_ui' => true, 'show_in_menu' => 'edit.php?post_type=mortgage_application', 'menu_icon' => 'dashicons-businessperson', 'supports' => array( 'title', 'thumbnail' ), 'capability_type' => 'post', 'map_meta_cap' => true, ) ); } /** Meta box with the LO's details and shortcode. */ public static function add_meta_box() { add_meta_box( 'mapp_lo_details', __( 'Loan officer details', '1003-mortgage-application' ), array( __CLASS__, 'render_meta_box' ), self::POST_TYPE, 'normal', 'high' ); add_meta_box( 'mapp_lo_shortcodes', __( 'Shortcodes', '1003-mortgage-application' ), array( __CLASS__, 'render_shortcodes' ), self::POST_TYPE, 'side', 'high' ); } /** * @param \WP_Post $post Post. */ public static function render_meta_box( $post ) { wp_nonce_field( 'mapp_lo_save', 'mapp_lo_nonce' ); echo ''; $ghl = false !== has_action( 'mapp_milestone', array( 'Mapp\Integrations\Crm\Crm', 'on_milestone' ) ); foreach ( self::FIELDS as $key => $spec ) { if ( 'mapp_lo_ghl_email' === $key && ! $ghl ) { continue; // GHL is Premium. } $value = get_post_meta( $post->ID, $key, true ); printf( '', esc_attr( $key ), esc_html( $spec[0] ), esc_attr( 'tel' === $spec[1] ? 'tel' : ( 'url' === $spec[1] ? 'url' : ( 'email' === $spec[1] ? 'email' : 'text' ) ) ), esc_attr( $value ) ); } echo ''; } /** * Side box: this LO's application and upload-page shortcodes. * * @param \WP_Post $post Post. */ public static function render_shortcodes( $post ) { if ( 'publish' !== $post->post_status || ! $post->post_name ) { echo '

' . esc_html__( 'Publish this loan officer to get their shortcodes.', '1003-mortgage-application' ) . '

'; return; } $codes = array( __( 'Application form', '1003-mortgage-application' ) => '[mortgage_application_form lo="' . $post->post_name . '"]', ); // Premium: the upload page and GHL ownership. $uploads = post_type_exists( \Mapp\Files\Store::UPLOAD_POST_TYPE ); $ghl = false !== has_action( 'mapp_milestone', array( 'Mapp\Integrations\Crm\Crm', 'on_milestone' ) ); if ( $uploads ) { $codes[ __( 'Document upload page', '1003-mortgage-application' ) ] = '[mortgage_application_file_uploads lo="' . $post->post_name . '"]'; } foreach ( $codes as $label => $code ) { echo '

' . esc_html( $label ) . '' . esc_html( $code ) . '

'; } echo '

' . esc_html( $ghl ? __( 'Alerts for anything sent through these go to this loan officer, and in GHL they become the contact owner.', '1003-mortgage-application' ) : __( 'Alerts for applications sent through this go to this loan officer.', '1003-mortgage-application' ) ) . '

'; } /** * @param int $post_id Post ID. * @param \WP_Post $post Post. */ public static function save_meta( $post_id, $post ) { if ( ! isset( $_POST['mapp_lo_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['mapp_lo_nonce'] ) ), 'mapp_lo_save' ) ) { return; } if ( ! current_user_can( 'edit_post', $post_id ) || wp_is_post_revision( $post_id ) ) { return; } foreach ( self::FIELDS as $key => $spec ) { if ( 'mapp_lo_ghl_email' === $key && ! isset( $_POST[ $key ] ) ) { continue; // Hidden without GHL: keep what was saved. } $raw = isset( $_POST[ $key ] ) ? wp_unslash( $_POST[ $key ] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput switch ( $spec[1] ) { case 'email': $value = sanitize_email( $raw ); break; case 'url': $value = esc_url_raw( $raw ); break; default: $value = sanitize_text_field( $raw ); } if ( 'mapp_lo_notify' === $key ) { $value = implode( ', ', array_filter( array_map( 'sanitize_email', array_map( 'trim', explode( ',', $raw ) ) ) ) ); } update_post_meta( $post_id, $key, $value ); } } /** * @param array $columns Columns. * @return array */ public static function columns( $columns ) { $columns['mapp_lo_shortcode'] = __( 'Shortcode', '1003-mortgage-application' ); $columns['mapp_lo_email'] = __( 'Email', '1003-mortgage-application' ); return $columns; } /** * @param string $column Column. * @param int $post_id Post ID. */ public static function column( $column, $post_id ) { if ( 'mapp_lo_shortcode' === $column ) { printf( '[mortgage_application_form lo="%s"]', esc_html( get_post_field( 'post_name', $post_id ) ) ); } elseif ( 'mapp_lo_email' === $column ) { echo esc_html( get_post_meta( $post_id, 'mapp_lo_email', true ) ); } } /** * How many loan officers this site uses: one, or unlimited with the * Premium module (mapp_lo_limit). * * @return int */ public static function limit() { return max( 1, (int) apply_filters( 'mapp_lo_limit', 1 ) ); } /** * The loan officers in use: the oldest published ones, up to the limit. * Others keep their page but get no applications, alerts or card. * * @return int[] */ public static function in_use_ids() { $limit = self::limit(); return array_map( 'intval', get_posts( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => PHP_INT_MAX === $limit ? -1 : $limit, 'orderby' => 'ID', 'order' => 'ASC', 'fields' => 'ids', ) ) ); } /** * @param int $lo_id LO post ID. * @return bool Whether this LO is one of the ones in use. */ public static function in_use( $lo_id ) { return PHP_INT_MAX === self::limit() || in_array( (int) $lo_id, self::in_use_ids(), true ); } /** @return string Where to get more loan officers. */ private static function upgrade_message() { return __( 'This version includes one loan officer. Add as many as you need with 1003 Mortgage Application Premium.', '1003-mortgage-application' ); } /** "Add loan officer" at the limit: explain instead of opening an editor that cannot publish. */ public static function block_new_over_limit() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- screen check only. $type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : ''; if ( self::POST_TYPE !== $type || count( self::in_use_ids() ) < self::limit() ) { return; } wp_die( '

' . esc_html( self::upgrade_message() ) . '

' . esc_html__( 'See Premium', '1003-mortgage-application' ) . ' ยท ' . esc_html__( 'Back to loan officers', '1003-mortgage-application' ) . '

', esc_html__( 'Loan officer limit', '1003-mortgage-application' ), array( 'response' => 200 ) ); } /** * Publishing one more LO than the limit allows saves it as a draft. * * @param array $data Post data. * @param array $postarr Raw post array. * @return array */ public static function hold_over_limit( $data, $postarr ) { if ( self::POST_TYPE !== $data['post_type'] || 'publish' !== $data['post_status'] || PHP_INT_MAX === self::limit() ) { return $data; } $id = isset( $postarr['ID'] ) ? (int) $postarr['ID'] : 0; if ( ( $id && in_array( $id, self::in_use_ids(), true ) ) || count( self::in_use_ids() ) < self::limit() ) { return $data; } $data['post_status'] = 'draft'; set_transient( 'mapp_lo_limit_hit_' . get_current_user_id(), 1, MINUTE_IN_SECONDS ); return $data; } /** On the loan officer screens: the limit, when it applies. */ public static function limit_notice() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || self::POST_TYPE !== $screen->post_type || PHP_INT_MAX === self::limit() ) { return; } $hit = get_transient( 'mapp_lo_limit_hit_' . get_current_user_id() ); $total = (int) wp_count_posts( self::POST_TYPE )->publish; if ( ! $hit && $total <= self::limit() ) { return; } delete_transient( 'mapp_lo_limit_hit_' . get_current_user_id() ); $msg = $hit ? __( 'Saved as a draft: ', '1003-mortgage-application' ) . self::upgrade_message() : self::upgrade_message() . ' ' . __( 'Only the first loan officer gets applications; the others send theirs to the site\'s email recipients.', '1003-mortgage-application' ); echo '

' . esc_html( $msg ) . ' ' . esc_html__( 'See Premium', '1003-mortgage-application' ) . '

'; } /** * Find a published LO in use by slug or ID. * * @param string|int $ref Slug or ID. * @return int LO post ID, or 0. */ public static function find( $ref ) { $ref = trim( (string) $ref ); if ( '' === $ref ) { return 0; } if ( ctype_digit( $ref ) ) { $post = get_post( (int) $ref ); } else { $post = get_page_by_path( sanitize_title( $ref ), OBJECT, self::POST_TYPE ); } return ( $post && self::POST_TYPE === $post->post_type && 'publish' === $post->post_status && self::in_use( $post->ID ) ) ? (int) $post->ID : 0; } /** * Published loan officers, by name. * * @return array ID => name. */ public static function published() { $out = array(); foreach ( get_posts( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => 500, 'orderby' => 'title', 'order' => 'ASC' ) ) as $post ) { if ( ! self::in_use( $post->ID ) ) { continue; } $out[ (int) $post->ID ] = get_the_title( $post ); } return $out; } /** * Everything about an LO that the form, emails and CRM need. * * @param int $lo_id LO post ID. * @param bool $any Also an LO beyond the limit (to show who an older application was with). * @return array|null */ public static function profile( $lo_id, $any = false ) { $lo_id = (int) $lo_id; if ( ! $lo_id || self::POST_TYPE !== get_post_type( $lo_id ) || ( ! $any && ! self::in_use( $lo_id ) ) ) { return null; } $email = (string) get_post_meta( $lo_id, 'mapp_lo_email', true ); $photo = (string) get_post_meta( $lo_id, 'mapp_lo_photo', true ); if ( '' === $photo && has_post_thumbnail( $lo_id ) ) { $photo = (string) get_the_post_thumbnail_url( $lo_id, 'thumbnail' ); } $notify = array_filter( array_merge( array( $email ), array_map( 'trim', explode( ',', (string) get_post_meta( $lo_id, 'mapp_lo_notify', true ) ) ) ), 'is_email' ); $ghl = (string) get_post_meta( $lo_id, 'mapp_lo_ghl_email', true ); return array( 'id' => $lo_id, 'slug' => (string) get_post_field( 'post_name', $lo_id ), 'name' => get_the_title( $lo_id ), 'title' => (string) get_post_meta( $lo_id, 'mapp_lo_title', true ), 'nmls' => (string) get_post_meta( $lo_id, 'mapp_lo_nmls', true ), 'phone' => (string) get_post_meta( $lo_id, 'mapp_lo_phone', true ), 'email' => $email, 'notify' => array_values( array_unique( $notify ) ), 'ghl_email' => '' !== $ghl ? $ghl : $email, 'booking_url' => (string) get_post_meta( $lo_id, 'mapp_lo_booking_url', true ), 'photo' => $photo, ); } /** * The LO card the borrower sees (no internal fields). * * @param int $lo_id LO post ID. * @return array|null */ public static function public_card( $lo_id ) { $profile = self::profile( $lo_id ); if ( ! $profile ) { return null; } return array( 'name' => $profile['name'], 'title' => $profile['title'], 'nmls' => $profile['nmls'], 'phone' => $profile['phone'], 'email' => $profile['email'], 'booking_url' => $profile['booking_url'], 'photo' => $profile['photo'], ); } /** * The GHL user ID for an email in this location, cached for 6 hours. * Returns '' when no user has that email (never falls back to an admin). * * @param object $client GHL client (Premium). * @param string $email User email. * @return string */ public static function ghl_user_id( $client, $email ) { $email = strtolower( trim( (string) $email ) ); if ( ! is_email( $email ) ) { return ''; } $cache_key = 'mapp_ghl_users_' . md5( $client->location_id() ); $users = get_transient( $cache_key ); if ( ! is_array( $users ) ) { $users = array(); $response = $client->json( 'GET', '/users/', null, array( 'locationId' => $client->location_id() ) ); if ( '' === $response['error'] && isset( $response['body']['users'] ) && is_array( $response['body']['users'] ) ) { foreach ( $response['body']['users'] as $user ) { if ( ! empty( $user['email'] ) && ! empty( $user['id'] ) ) { $users[ strtolower( $user['email'] ) ] = (string) $user['id']; } } set_transient( $cache_key, $users, 6 * HOUR_IN_SECONDS ); } else { set_transient( $cache_key, array(), 10 * MINUTE_IN_SECONDS ); } } return isset( $users[ $email ] ) ? $users[ $email ] : ''; } }