<?php
/* =========================================================
   SACHI HOSPITAL - DYNAMIC DOCTORS SHORTCODE
   ACF + CPT UI
   CPT SLUG: doctors
   SHORTCODE: [sachi_doctors]

   Doctor Name:
   Uses WordPress Post Title

   ACF Fields:
   - doctor_image
   - specialization
   - doctor_description
   ========================================================= */


/* =========================================================
   DOCTORS SHORTCODE
   ========================================================= */

function sachi_doctors_shortcode() {

    /*
     * ============================================
     * DOCTOR CPT
     * ============================================
     */
    $doctor_post_type = 'doctors';


    /*
     * ============================================
     * CHECK CPT EXISTS
     * ============================================
     */

    if ( ! post_type_exists( $doctor_post_type ) ) {

        return '
        <div class="sachi-doctors-error">
            <strong>Doctor CPT not found.</strong><br>
            Please check that the Custom Post Type slug is:
            <strong>doctors</strong>
        </div>';
    }


    /*
     * ============================================
     * QUERY PUBLISHED DOCTORS
     * ============================================
     */

    $doctors = new WP_Query(
        array(
            'post_type'           => 'doctors',
            'post_status'         => 'publish',
            'posts_per_page'      => -1,
            'orderby'             => 'date',
            'order'               => 'ASC',
            'ignore_sticky_posts' => true,
            'no_found_rows'       => true,
        )
    );


    /*
     * ============================================
     * START OUTPUT BUFFER
     * ============================================
     */

    ob_start();


    /*
     * ============================================
     * NO DOCTORS FOUND
     * ============================================
     */

    if ( ! $doctors->have_posts() ) {

        echo '<div class="sachi-doctors-empty">';

            echo '<div class="sachi-doctors-empty-icon">';
                echo '👨‍⚕️';
            echo '</div>';

            echo '<h3>No Doctors Found</h3>';

            echo '<p>';
                echo 'Please make sure at least one doctor is published under ';
                echo '<strong>Doctors</strong>';
                echo ' in WordPress.';
            echo '</p>';

        echo '</div>';

        wp_reset_postdata();

        return ob_get_clean();
    }


    /*
     * ============================================
     * DOCTORS SECTION
     * ============================================
     */

    echo '<div class="sachi-doctors-section">';


    /*
     * ============================================
     * DOCTORS GRID
     * ============================================
     */

    echo '<div class="sachi-doctors-grid">';


    /*
     * ============================================
     * LOOP THROUGH DOCTORS
     * ============================================
     */

    while ( $doctors->have_posts() ) {

        $doctors->the_post();


        /*
         * ========================================
         * CURRENT DOCTOR POST ID
         * ========================================
         */

        $doctor_id = get_the_ID();


        /*
         * ========================================
         * DOCTOR NAME
         *
         * IMPORTANT:
         * Doctor name comes from WordPress Title.
         * Example:
         *
         * Dr M R Srinivasa Yogan
         * ========================================
         */

        $doctor_name = get_the_title( $doctor_id );


        /*
         * ========================================
         * ACF FIELDS
         * ========================================
         */

        $doctor_image = get_field(
            'doctor_image',
            $doctor_id
        );

        $specialization = get_field(
            'specialization',
            $doctor_id
        );

        $description = get_field(
            'doctor_description',
            $doctor_id
        );


        /*
         * ========================================
         * IMAGE URL
         * ========================================
         */

        $image_url = '';


        /*
         * ACF IMAGE RETURN FORMAT:
         * ARRAY
         * ========================================
         */

        if ( is_array( $doctor_image ) ) {

            /*
             * Image URL
             */
            if ( ! empty( $doctor_image['url'] ) ) {

                $image_url = $doctor_image['url'];

            /*
             * Image ID
             */
            } elseif ( ! empty( $doctor_image['ID'] ) ) {

                $image_url = wp_get_attachment_image_url(
                    $doctor_image['ID'],
                    'large'
                );
            }
        }


        /*
         * ACF IMAGE RETURN FORMAT:
         * IMAGE ID
         * ========================================
         */

        elseif ( is_numeric( $doctor_image ) ) {

            $image_url = wp_get_attachment_image_url(
                $doctor_image,
                'large'
            );
        }


        /*
         * ACF IMAGE RETURN FORMAT:
         * IMAGE URL
         * ========================================
         */

        elseif ( is_string( $doctor_image ) ) {

            $image_url = trim( $doctor_image );
        }


        /*
         * ========================================
         * DOCTOR CARD
         * ========================================
         */

        echo '<article class="sachi-doctor-card">';


            /*
             * ====================================
             * DOCTOR IMAGE
             * ====================================
             */

            echo '<div class="sachi-doctor-image">';


                if ( ! empty( $image_url ) ) {

                    echo '<img
                        src="' . esc_url( $image_url ) . '"
                        alt="' . esc_attr( $doctor_name ) . '"
                        loading="lazy"
                    >';

                } else {

                    echo '<div class="sachi-doctor-placeholder">';
                        echo '👨‍⚕️';
                    echo '</div>';
                }


            echo '</div>';


            /*
             * ====================================
             * DOCTOR CONTENT
             * ====================================
             */

            echo '<div class="sachi-doctor-content">';


                /*
                 * DOCTOR NAME
                 */

                if ( ! empty( $doctor_name ) ) {

                    echo '<h3 class="sachi-doctor-name">';
                        echo esc_html( $doctor_name );
                    echo '</h3>';
                }


                /*
                 * =================================
                 * SPECIALIZATION
                 * =================================
                 */

                if ( ! empty( $specialization ) ) {

                    echo '<div class="sachi-doctor-specialization">';
                        echo esc_html( $specialization );
                    echo '</div>';
                }


                /*
                 * =================================
                 * DESCRIPTION
                 * =================================
                 */

                if ( ! empty( $description ) ) {

                    echo '<p class="sachi-doctor-description">';
                        echo esc_html( $description );
                    echo '</p>';
                }


            echo '</div>';


        echo '</article>';
    }


    /*
     * ============================================
     * CLOSE GRID
     * ============================================
     */

    echo '</div>';


    /*
     * ============================================
     * CLOSE SECTION
     * ============================================
     */

    echo '</div>';


    /*
     * ============================================
     * RESET WORDPRESS POST DATA
     * ============================================
     */

    wp_reset_postdata();


    /*
     * ============================================
     * RETURN OUTPUT
     * ============================================
     */

    return ob_get_clean();
}


/* =========================================================
   REGISTER SHORTCODE
   ========================================================= */

add_shortcode(
    'sachi_doctors',
    'sachi_doctors_shortcode'
);