broker_post_system.php 3.1 KB
<?php



function wpdocs_updated_option( $option_name, $old_value, $value ) {

	if ( $option_name  === 'broker_list') {
        syncBrokerListToPosts();
	}

}
add_action( 'updated_option', 'wpdocs_updated_option', 10, 3 );

function syncBrokerListToPosts() {

    $brokerList = getBrokerageList();
    $broker_ids = [];
    foreach($brokerList as $broker) {
        if(!empty($broker['broker_id'])) {
            $broker_ids[] = $broker['broker_id'];
        }
    }

    global $wpdb;

    $broker_posts = $wpdb->get_results('select post_author,ID from wp_posts where post_author IN('.implode(',', $broker_ids).') and post_type = "broker"',OBJECT_K);
    
    foreach($brokerList as $bl) {

        if(!empty($bl['broker_id']) && empty($broker_posts[$bl['broker_id']])) {
            wp_insert_post(['post_type'=>'broker','post_author'=>$bl['broker_id'],'post_title'=>$bl['brokerage'],'post_status'   => 'publish']);
        } else {
            wp_update_post(['ID'=>$broker_posts[$bl['broker_id']]->ID,'post_author'=>$bl['broker_id'],'post_title'=>$bl['brokerage'],'post_status'   => 'publish']);
        }


    }



}

if( function_exists('acf_add_options_page') ) {

    //Create the custom post type for brokers.. make it hidden this is just for ACF purposes
    function broker_custom_post_type() {
        register_post_type('broker',
            array(
                'labels'      => array(
                    'name'          => __('Brokers', 'textdomain'),
                    'singular_name' => __('Broker', 'textdomain'),
                ),
                    'public'      => false,
                    'has_archive' => false,
            )
        );
    }
    add_action('init', 'broker_custom_post_type');

    //Create the options page for the broker assignments
    acf_add_options_page(array(
        'page_title'    => 'Broker Assignments',
        'menu_title'    => 'Broker Assignments',
        'menu_slug'     => 'broker-assignments',
        'redirect'      => false
    ));

    //Create the options page for the broker assignments
    acf_add_options_page(array(
        'page_title'    => 'Events',
        'menu_title'    => 'Events',
        'menu_slug'     => 'broker-events',
        'redirect'      => false
    ));

        //Create the options page for the broker assignments
        acf_add_options_page(array(
            'page_title'    => 'Team Assignments',
            'menu_title'    => 'Team Assignments',
            'menu_slug'     => 'team-assignments',
            'redirect'      => false
        ));

    //This dynamically loads
    // function be_acf_dynamic_icons( $field ) {

    //     if($field['name'] == 'brokerages') {

    //         $brokerList = getBrokerageList();
        
    //         $field['choices'] = [ 0 => 'all' ];
    //         foreach ($brokerList as $broker) {
    //             if(!empty($broker['broker_id'])) {
    //                 $field['choices'][ $broker['broker_id'] ] = $broker['broker_id'] . " - " . $broker['brokerage'];
    //             }
    //         }

    //     }
    
    //     return $field;


    // }
    // add_filter( 'acf/load_field', 'be_acf_dynamic_icons' );

}