Load next post by ID on a single page

It seems that I found a solution to this and it is pretty easy. So I’m gonna post all the code for future uses:

single.php

<?php get_header(); ?>
<?php
$post = get_post();
$post_cat = get_the_category($post);
$data = collection_data_json($post_cat[0]->term_id, $post->ID);
$data_details = collection_order_permalink($data);
$author_id = intval($post->post_author);
?>
<script type="text/javascript">
    var cat = "<?php print $post_cat_id ?>";
    var pid = "<?php print $post->ID ?>";
    var post_details = <?php print $data ?>;
    var post_order = <?php print $data_details ?>;

</script>

<script type="text/javascript" src="https://wordpress.stackexchange.com/questions/223844/<?php print get_template_directory_uri() ?>/lib/js/single-post.js"></script>
<div class="entries" id="entries">
    <article class="article" data-url="<?php echo get_the_permalink($post->ID); ?>" data-id="<?php print $post->ID ?>">
        <h1 class="start-indicator"><?php print $post->post_title ?></h1>
        <div class="meta">
            <span class="time"><?php print $post->post_date ?></span>
            <span class="category"><?php print $post_cat[0]->name ?></span>
        </div>
        <div class="category-mobile"><?php print $post_cat[0]->name ?></div>    
        <div class="meta meta-mobile">By <a href="<?php print get_author_posts_url($author_id) ?>"><?php print get_userdata($author_id)->display_name ?></a>, <?php print the_author_meta("job_position", $author_id); ?> at WePay | <?php print $post->post_date ?></div>
        <div class="speed-reading">
            <img src="<?php print get_template_directory_uri() ?>/images/speed-reading-black.png" alt="Estimated reading time">
            <span><?php print reading_time($post->post_content, intval(get_option("reading_time"))) ?></span>
        </div>
        <div class="description">
            <div class="post-by">
                <figure>
                    <img src="<?php print get_avatar_url($author_id); ?>" alt="<?php print get_userdata($author_id)->display_name ?>" title="<?php print get_userdata($author_id)->display_name ?>">
                    <figcaption>By <a href="<?php print get_author_posts_url($author_id) ?>" title="<?php print get_userdata($author_id)->display_name ?>"><?php print get_userdata($author_id)->display_name ?></a>, <?php print the_author_meta("job_position", $author_id); ?> at WePay</figcaption>
                </figure>
            </div>
            <?php print apply_filters("the_content", $post->post_content); ?>
        </div>
        <!-- End -->
        <div class="author">
            <a href="<?php print get_author_posts_url($author_id) ?>"> - <?php print get_userdata($author_id)->display_name ?></a>
        </div>
        <div class="about-author">
            <h3><span>About the author</span></h3>
            <div class="avatar">
                <img src="<?php print get_avatar_url($author_id); ?>" alt="<?php print get_userdata($author_id)->display_name ?>">
            </div>
            <div class="caption">
                <p class="position"><a href="<?php print get_author_posts_url($author_id) ?>" title="<?php print get_userdata($author_id)->display_name ?>"><?php print get_userdata($author_id)->display_name ?></a>, <?php print the_author_meta("job_position", $author_id); ?> at WePay</p>
                <p class="bio"><?php print the_author_meta("description", $author_id) ?></p>
                <p><a href="<?php print get_author_posts_url($author_id) ?>" class="more" title="More blog posts by <?php print get_userdata($author_id)->display_name ?>">More blog posts by <?php print get_userdata($author_id)->display_name ?></a></p>
            </div>
        </div>
        <div class="article-social">
            <h4>Follow us on</h4>
            <div class="social">
                <a href="http://twitter.com" class="t" title="Follow us on Twitter"></a>
                <a href="http://facebook.com" class="f" title="Follow us on Facebook"></a>
                <a href="http://linkedin.com" class="l" title="Find us on Linkedin"></a>
            </div>
        </div>
        <?php comments_template() ?>
    </article>
</div>

<!-- Social Icons -->
<div class="share-post">
    <div class="share-on-twitter">
        <a href="https://www.facebook.com/sharer/sharer.php?u=<?php print get_the_permalink($post->ID); ?>" title="Share this post on Twitter">
            <span class="shares"></span>
            <img src="<?php print get_template_directory_uri() ?>/images/twitter.png" alt="Twitter">
            <img src="<?php print get_template_directory_uri() ?>/images/t.png" alt="Twitter" class="hover">
        </a>    
    </div>
    <div class="share-on-facebook">
        <a href="https://twitter.com/home?status=<?php echo get_the_permalink($post->ID); ?>" title="Share this post on Facebook">
            <span class="shares"></span>
            <img src="<?php print get_template_directory_uri() ?>/images/facebook.png" alt="Facebook">
            <img src="<?php print get_template_directory_uri() ?>/images/f.png" alt="Faceook" class="hover">
        </a>    
    </div>
    <div class="share-on-linkedin">
        <a href="https://www.linkedin.com/shareArticle?mini=true&url=<?php echo get_the_permalink($post->ID); ?>" title="Share this post on Linkedin">
            <span class="shares"></span>
            <img src="<?php print get_template_directory_uri() ?>/images/linkedin.png" alt="Linkedin">
            <img src="<?php print get_template_directory_uri() ?>/images/l.png" alt="Linkedin" class="hover">
        </a>    
    </div>
</div>
<?php get_footer(); ?>

single-post.js

var pc = 1;

$(document).ready(function () {
    post_order = formatCollection(post_order);
    post_details = formatCollection(post_details);

    function formatCollection(objCollection) {
        var items = {};
        var i = 0;
        for (var index in objCollection) {
            items[i] = objCollection[index];
            i++;
        }
        return items;
    }

    function set_first_url() {
        $("body").append($("<input>").attr("type", "hidden").attr("id", "url-identifier").attr("data-url", post_order[pc]));
    }
    set_first_url();

    function set_next_url(position) {
        if (pc >= Object.keys(post_order).length) {
            console.log("no posts left");
            remove_url_identifier();
            return false;
        }
        else {
            $("body").find("#url-identifier").detach();
            $("body").append($("<input>").attr("type", "hidden").attr("id", "url-identifier").attr("data-url", post_order[position]));
        }
    }

    function append_next_article(article) {
        $("div#entries").append(article);
    }

    function remove_url_identifier() {
        $("body").find("#url-identifier").detach();
    }

    function crawl_url(url) {
        $.ajax({
            type: "GET",
            url: url,
            success: function (data) {
                var article = $(data).find("div#entries article.article");
                append_next_article(article);
                pc++;
                set_next_url(pc);          
            },
            failure: function (errMsg) {
                alert("Oooooppps. Something happened. Don't tell my boss !");
                return false;
            }
        });
    }

    $(document).scroll(function (e) {
        if (processing)
            return false;
        if ($(window).scrollTop() >= ($(document).height() - $(window).height())) {
            processing = true;
            var next_url = $("body").find("#url-identifier").data("url");
            if (typeof next_url === "undefined")
                return false;
            crawl_url(next_url);

            unique = [];
            processing = false;
        }
    }); 
});

single-next.php

function collection_order_permalink($collection) {
    $collection = json_decode($collection);
    for ($i = 0; $i < count($collection); $i++) {
        $collection[$i] = get_permalink($collection[$i]->ID);
    }
    return json_encode($collection, JSON_PRETTY_PRINT);
}

function sort_collection_data($collection, $arrkey) {

    $result = [];
    $result[0] = $collection[$arrkey];
    for ($i = 0, $t = 1; $i < count($collection) - 1; $i++, $t++) {
        if ($i == $arrkey) {
            $t -= 1;
            continue;
        }
        $result[$t] = $collection[$i];
    }
    return $result;
}

function collection_data_json($cat, $pid = null) {
    $args = ['post_type' => 'post', 'posts_per_page' => 999999, 'cat' => $cat, 'orderby' => "date"];
    $allCategoryPosts = new WP_Query($args);
    $allCategoryPosts = $allCategoryPosts->posts; // Collection with the original post order
    $multikey = multidimensional_search($allCategoryPosts, $pid);
    $ordered = "";
    if ($multikey !== false) {
        $ordered = sort_collection_data($allCategoryPosts, $multikey);
        $ordered = filter_date($ordered);
        $ordered = filter_permalink($ordered);
        $ordered = filter_author($ordered);
    }
    return json_encode($ordered, JSON_PRETTY_PRINT);
}