/*
Theme Name: Axton Theme
Theme URI: https://axtonconsulting.com
Template: Divi
Author: Ahmed Ramy
Author URI: https://ahmedramy.us.kg/
Description: Smart. Flexible. Beautiful. Divi is the most powerful theme in our collection.
Version: 5.14.0.1791187714
Updated: 2026-10-05 08:08:34

*/

/**
 * Axton Related Content
 * Shortcode: [axton_related_items]
 */
function axton_related_items_shortcode() {

    $section = get_field( 'related_content_section' );

    if ( empty( $section ) || ! is_array( $section ) ) {
        return '';
    }

    $mode = $section['selection_mode'] ?? 'manual';
    $items = array();

    /*
     * --------------------------------------------------
     * MANUAL MODE
     * --------------------------------------------------
     */
    if ( 'manual' === $mode ) {

        $items = $section['related_items'] ?? array();

    /*
     * --------------------------------------------------
     * AUTOMATIC MODE
     * --------------------------------------------------
     */
    } elseif ( 'automatic' === $mode ) {

        $post_type = $section['source_post_type'] ?? '';

        $number = isset( $section['number_of_items'] )
            ? absint( $section['number_of_items'] )
            : 5;

        if ( $number < 1 ) {
            $number = 5;
        }

        if ( $number > 20 ) {
            $number = 20;
        }

        /*
         * Make sure the selected post type really exists.
         */
        if ( $post_type && post_type_exists( $post_type ) ) {

            $query = new WP_Query(
                array(
                    'post_type'           => $post_type,
                    'post_status'         => 'publish',
                    'posts_per_page'      => $number,
                    'orderby'             => 'date',
                    'order'               => 'DESC',
                    'ignore_sticky_posts' => true,

                    /*
                     * Don't show the current post
                     * if querying the same CPT.
                     */
                    'post__not_in'         => array( get_the_ID() ),
                )
            );

            $items = $query->posts;
        }
    }

    if ( empty( $items ) ) {
        return '';
    }

    /*
     * --------------------------------------------------
     * OUTPUT
     * --------------------------------------------------
     */
    ob_start();
    ?>

    <div class="axton-related-items">

        <?php foreach ( $items as $index => $item ) :

            $post_id = is_object( $item )
                ? $item->ID
                : absint( $item );

            if ( ! $post_id ) {
                continue;
            }

            $number = str_pad(
                (string) ( $index + 1 ),
                2,
                '0',
                STR_PAD_LEFT
            );
            ?>

            <a
                class="axton-related-item"
                href="<?php echo esc_url( get_permalink( $post_id ) ); ?>"
            >
                <span class="axton-related-number">
                    <?php echo esc_html( $number ); ?>
                </span>

                <span class="axton-related-title">
                    <?php echo esc_html( get_the_title( $post_id ) ); ?>
                </span>

                <span class="axton-related-arrow" aria-hidden="true">
                    ↗
                </span>
            </a>

        <?php endforeach; ?>

    </div>

    <?php

    wp_reset_postdata();

    return ob_get_clean();
}

add_shortcode(
    'axton_related_items',
    'axton_related_items_shortcode'
);