[ 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 '
' . 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 ) . '
' . 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' ) . '