public function get_Relationships_by_Tag($tag, $_method, $_route, $_path, $_headers)
 {
     if (empty($tag) || !is_string($tag)) {
         return new WP_Error('json_options_invalid_tag', __("Invalid options tag"), ['status' => 404]);
     }
     $site = $_headers['REMOTE'];
     $tags = explode(',', $tag);
     if ($tags) {
         $tags_query['relation'] = 'OR';
         foreach ($tags as $tag) {
             $tags_query[] = array('key' => 'related_tags', 'value' => $tag, 'compare' => 'LIKE');
         }
     }
     $args = array('post_type' => 'relationship', 'meta_query' => array('relation' => 'AND', array('key' => 'site_address', 'value' => $site), $tags_query));
     $relationships = new WP_Query($args);
     if ($relationships->have_posts()) {
         // Get the Target Categories
         $targets = [];
         while ($relationships->have_posts()) {
             $relationships->the_post();
             $targets[] = get_field('target_category');
         }
         $targets = dedupe($targets);
         $related_categories = implode(', ', $targets);
         // Find Matching Relationships
         $args = array('post_type' => 'relationship', 'tax_query' => array(array('taxonomy' => 'related_categories', 'terms' => $related_categories)));
         $relationships = new WP_Query($args);
         // Build Relationships JSON Reponse
         include 'related-json-response.php';
     }
     wp_reset_query();
     return $sites;
 }
<?php

// Called by search to determine which sites to search...
define('WP_USE_THEMES', false);
require_once '../../../../wp-load.php';
// Find Related Sites
$args = array('post_type' => 'relationship');
$relationships = new WP_Query($args);
if ($relationships->have_posts()) {
    while ($relationships->have_posts()) {
        $relationships->the_post();
        $related_sites[] = get_field('site_address');
    }
    $primary_site = get_site_url() . '/';
    $related_sites[] = $primary_site;
    $related_sites = dedupe($related_sites);
    header('Content-Type: application/json');
    echo json_encode($related_sites);
}
    // Check Combined Recent Posts for Duplicates, Sort by Date, and Trim to Limit
    if (!empty($combined_recent_posts)) {
        $combined_recent_posts = dedupe($combined_recent_posts);
        usort($combined_recent_posts, create_function('$a,$b', 'return strcmp($b->date, $a->date);'));
        $combined_recent_posts = array_slice($combined_recent_posts, 0, $recent);
    }
    // Check Combined Random Posts for Duplicates, Sort by Random, and Trim to Limit
    if (!empty($combined_random_posts)) {
        $combined_random_posts = dedupe($combined_random_posts);
        shuffle($combined_random_posts);
        $combined_random_posts = array_slice($combined_random_posts, 0, $random);
    }
    // Merge Both to Get Total Posts
    $cached_posts = array_merge($combined_recent_posts, $combined_random_posts);
    // Finally Clean All the Posts
    $cached_posts = dedupe($cached_posts);
    usort($cached_posts, create_function('$a,$b', 'return strcmp($b->date, $a->date);'));
    // Set the transient
    set_transient($post_transient, $cached_posts, 21600);
}
// The Loop
if (!empty($cached_posts)) {
    echo '<div id="related-content" data-related="' . json_encode($relationships) . '" class="' . $widget . '" style="max-width: 100%; width: 100%; left: 0; margin: 0 auto;">';
    echo '<ul class="custom-slider" id="slider-' . $widget . '">';
    $hue = 0;
    foreach ($cached_posts as $post) {
        include $dir . 'related-loop.php';
    }
    echo '</ul>';
    echo '</div>';
}
function getPostCat($data, $transient)
{
    // Build category list as array
    $feed_url = str_replace('\\', '', $_GET['feedURL']);
    $feed_url = str_replace('"', '', $feed_url);
    $json_posts = json_decode(file_get_contents($feed_url), true);
    foreach ($json_posts as $json_post) {
        $categories = $json_post['category'];
        $categories = explode(', ', $categories);
        foreach ($categories as $category) {
            if (!empty($category)) {
                $combined_cats[] = $category;
            }
        }
        $combined_cats = dedupe($combined_cats);
    }
    // Format the site name
    $site_name = str_replace('\\', '', $_GET['siteName']);
    $site_name = str_replace('"', '', $site_name);
    // Format the site address
    $site_address = str_replace('\\', '', $_GET['siteAddress']);
    $site_address = str_replace('"', '', $site_address);
    // Create array of posts for each category
    $sorted_json_posts = array();
    $all_posts = array();
    $combined_cat_posts = array();
    foreach ($combined_cats as $combined_cat) {
        // Reformat the JSON Post
        foreach ($json_posts as $json_post) {
            $post_date = explode('T', $json_post['Post date']);
            $post_date[1] = explode('-', $post_date[1]);
            $post_date[1] = $post_date[1][0];
            $json_post['date'] = implode($post_date, 'T');
            unset($json_post['Post date']);
            $json_post['link'] = $json_post['path'];
            unset($json_post['path']);
            $json_post['excerpt'] = $json_post['summary'];
            unset($json_post['summary']);
            $json_post['type'] = 'JSON';
            $json_post['site'] = $site_address;
            $json_post['sitename'] = $site_name;
            $categories = $json_post['category'];
            if (strpos($categories, $combined_cat) !== false) {
                $sorted_json_posts[$combined_cat][] = $json_post;
            }
        }
        // Set the Transient
        $category_posts = $sorted_json_posts[$combined_cat];
        if (!empty($category_posts)) {
            // Format the transient name using category
            $combined_cat = htmlspecialchars($combined_cat);
            $combined_cat = str_replace(' &amp;', '', $combined_cat);
            $combined_cat = str_replace('amp;', '', $combined_cat);
            $combined_cat = str_replace(' ', '_', $combined_cat);
            $cat_transient = 'json_posts_' . $combined_cat;
            set_transient($cat_transient, json_encode($category_posts), 21600);
            // Add current category posts to combined
            if ($cat_transient == $transient) {
                $combined_cat_posts = array_merge($category_posts, $combined_cat_posts);
            }
            // Add current category posts to $all_posts
            $all_posts = array_merge($category_posts, $all_posts);
        }
    }
    // Handle combined post results
    if (!empty($combined_cat_posts) && isset($_GET['searchCats'])) {
        return $combined_cat_posts;
    }
    // Handle all_posts scenario
    if (!empty($all_posts) || $_GET['searchCats'] == '') {
        set_transient('json_posts_all_posts', json_encode($all_posts), 21600);
        if (!isset($_GET['searchCats'])) {
            return $all_posts;
        }
    }
}
function getRelationships($data, $transient)
{
    // Translate Data Variables
    $searchCats = $data['searchCats'];
    $searchCats = str_replace('\\', '', $searchCats);
    $searchCats = json_decode($searchCats);
    // Find Matching Relationships
    $args = array('post_type' => 'relationship', 'tax_query' => array(array('taxonomy' => 'related_categories', 'terms' => $searchCats)));
    $relationships = new WP_Query($args);
    include 'related-json-response.php';
    // Query Each Site for Posts Matching Relationships
    $posts_per_site = 100;
    $combined_remote_posts = [];
    if ($searchCats == ["all_posts"]) {
        // Get the List of Sites
        wp_reset_query();
        $relationships = new WP_Query(array('post_type' => 'relationship'));
        $relationships = $relationships->posts;
        foreach ($relationships as $relationship) {
            $sites[] = get_field('site_address', $relationship->ID);
        }
        $sites = dedupe($sites);
        $sites[] = get_site_url() . '/';
        // Get All Posts and Combine
        $combined_posts = [];
        foreach ($sites as $site) {
            // Resolve the Site Name
            if (!property_exists($relationship, 'sitename')) {
                if ($site == get_site_url() . '/') {
                    $sitename = get_bloginfo('name');
                } elseif ($site == 'https://www.makingconnectionsnews.org/') {
                    $sitename = 'Making Connections';
                } elseif ($site == 'https://www.wmmt.org/') {
                    $sitename = 'WMMT FM';
                } elseif ($site == 'https://ami.appalshop.org/') {
                    $sitename = 'AMI';
                } elseif ($site == 'https://www.appalshoparchive.org/') {
                    $sitename = 'Appalshop Archive';
                } else {
                    $sitename = '';
                }
            } else {
                $sitename = $relationship->sitename;
            }
            $url_base = $site . 'wp-json/posts';
            $args = array('filter[posts_per_page]' => $posts_per_site);
            $url = add_query_arg($args, $url_base);
            $response = wp_remote_get($url, array('timeout' => 10));
            if (is_array($response)) {
                $posts = json_decode($response['body']);
                foreach ($posts as $post) {
                    $post->site = $site;
                    $post->sitename = $sitename;
                }
                $combined_posts = array_merge((array) $combined_posts, (array) $posts);
            }
        }
        $combined_posts = dedupe($combined_posts);
        usort($combined_posts, create_function('$a,$b', 'return strcmp($b->date, $a->date);'));
        // Get Roadside Posts
        //$roadside_posts = json_decode(file_get_contents(get_stylesheet_directory() . '/includes/example.json'), true);
        $roadside_posts = json_decode(file_get_contents('https://roadside.org/blog-export'), true);
        foreach ($roadside_posts as $roadside_post) {
            $post_date = explode('T', $roadside_post['Post date']);
            $post_date[1] = explode('-', $post_date[1]);
            $post_date[1] = $post_date[1][0];
            $roadside_post['date'] = implode($post_date, 'T');
            unset($roadside_post['Post date']);
            $roadside_post['link'] = $roadside_post['path'];
            unset($roadside_post['path']);
            $roadside_post['excerpt'] = $roadside_post['summary'];
            unset($roadside_post['summary']);
            $roadside_post['site'] = 'https://www.roadside.org/';
            $roadside_post['sitename'] = 'Roadside Theater';
            $rs_posts[] = json_decode(json_encode($roadside_post), FALSE);
        }
        $combined_posts = array_merge((array) $combined_posts, (array) $rs_posts);
        usort($combined_posts, create_function('$a,$b', 'return strcmp($b->date, $a->date);'));
        echo json_encode($combined_posts);
        // Set the Transient
        if (!empty($combined_posts)) {
            set_transient($transient, json_encode($combined_posts), 21600);
        }
    } else {
        // Loop through Sites to Get Posts for Matching Categories and Tags
        foreach ($sites as $relationship) {
            if (property_exists($relationship, 'related_categories')) {
                $matching_categories = $relationship->related_categories;
                $categories = implode(",", (array) $matching_categories);
            } else {
                $categories = [];
            }
            if (property_exists($relationship, 'related_tags')) {
                $matching_tags = $relationship->related_tags;
                $tags = implode(",", (array) $matching_tags);
            } else {
                $tags = [];
            }
            // Get Site Information
            if (property_exists($relationship, 'address')) {
                $site = $relationship->address;
            } else {
                return false;
            }
            $url_base = $site . 'wp-json/posts';
            // Resolve the Site Name
            if (!property_exists($relationship, 'sitename')) {
                if ($site == get_site_url() . '/') {
                    $sitename = get_bloginfo('name');
                } elseif ($site == 'https://www.makingconnectionsnews.org/') {
                    $sitename = 'Making Connections';
                } elseif ($site == 'https://www.wmmt.org/') {
                    $sitename = 'WMMT FM';
                } elseif ($site == 'https://ami.appalshop.org/') {
                    $sitename = 'AMI';
                } elseif ($site == 'https://www.appalshoparchive.org/') {
                    $sitename = 'Appalshop Archive';
                } else {
                    $sitename = '';
                }
            } else {
                $sitename = $relationship->sitename;
            }
            // Remote Category Query
            if ($categories) {
                $args = array('filter[category_name]' => $categories, 'filter[posts_per_page]' => $posts_per_site);
                $url = add_query_arg($args, $url_base);
                $response = wp_remote_get($url, array('timeout' => 10));
                if (is_wp_error($response)) {
                    $error_string = $response->get_error_message();
                    echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
                    $remote_cat_posts = array();
                } else {
                    $remote_cat_posts = json_decode($response['body']);
                }
            } else {
                $remote_cat_posts = array();
            }
            // Remote Tag Query
            if ($tags) {
                $args = array('filter[tag]' => $tags, 'filter[posts_per_page]' => $posts_per_site);
                $url = add_query_arg($args, $url_base);
                $response = wp_remote_get($url, array('timeout' => 10));
                if (is_wp_error($response)) {
                    $error_string = $response->get_error_message();
                    echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
                    $remote_tag_posts = array();
                } else {
                    $remote_tag_posts = json_decode($response['body']);
                }
            } else {
                $remote_tag_posts = array();
            }
            // Combine remote Posts
            $remote_posts = array_merge((array) $remote_cat_posts, (array) $remote_tag_posts);
            $remote_posts = dedupe($remote_posts);
            if (is_array($remote_posts)) {
                foreach ($remote_posts as $post) {
                    $post->site = $site;
                    $post->sitename = $sitename;
                }
            }
            $combined_remote_posts = array_merge((array) $combined_remote_posts, (array) $remote_posts);
        }
        // Get the local posts too!
        foreach ($searchCats as $related_category) {
            $term = get_term($related_category, 'related_categories');
            $category_slugs[] = $term->slug;
        }
        $category_slugs = implode(',', $category_slugs);
        $url_base = get_site_url() . '/wp-json/posts';
        $args = array('filter[posts_per_page]' => $posts_per_site, 'filter[related_categories]' => $category_slugs);
        $url = add_query_arg($args, $url_base);
        $response = wp_remote_get($url);
        if (is_array($response)) {
            $local_posts = json_decode($response['body']);
            foreach ($local_posts as $post) {
                $post->site = get_site_url() . '/';
                $post->sitename = get_bloginfo('name');
            }
        }
        $combined_posts = array_merge((array) $combined_remote_posts, (array) $local_posts);
        usort($combined_posts, create_function('$a,$b', 'return strcmp($b->date, $a->date);'));
        echo json_encode($combined_posts);
        // Set the Transient
        if (!empty($combined_posts)) {
            set_transient($transient, json_encode($combined_posts), 21600);
        }
    }
}
$primary_relationships->address = $primary_site;
if (!empty($categories)) {
    $primary_relationships->related_categories[] = $categories;
}
if (!empty($tags)) {
    $primary_relationships->related_tags[] = $tags;
}
// Combine Relationships and Check for Duplicates / Empty Arrays
if (empty($cat_relationships)) {
    $cat_relationships = [];
}
if (empty($tag_relationships)) {
    $tag_relationships = [];
}
$relationships = array_merge($cat_relationships, $tag_relationships);
$relationships = dedupe($relationships);
$relationships['Primary Relationships'] = $primary_relationships;
// Set the Number of Posts
$recent = get_field('how_many_recent', 'options');
$random = get_field('how_many_random', 'options');
$total_posts = $recent + $random;
if (!empty($relationships)) {
    $is_widget = false;
    $related_transient = array();
    if (!empty($categories)) {
        $related_transient[] = $categories;
    }
    if (!empty($tags)) {
        $related_transient[] = $tags;
    }
    if (empty($related_transient)) {
                $site->json_categories = get_field('json_categories');
            }
            if (get_field('related_categories')) {
                $related_categories = get_field('related_categories');
            } else {
                $related_categories = [];
            }
            if (get_field('related_tags')) {
                $related_tags = get_field('related_tags');
            } else {
                $related_tags = [];
            }
        }
        if (!empty($related_categories)) {
            foreach ($related_categories as $related_category) {
                $site->related_categories[] = $related_category;
            }
        }
        if (!empty($related_tags)) {
            foreach ($related_tags as $related_tag) {
                $site->related_tags[] = $related_tag;
            }
        }
    }
    if (!empty($related_categories)) {
        $site->related_categories = dedupe($site->related_categories);
    }
    if (!empty($related_tags)) {
        $site->related_tags = dedupe($site->related_tags);
    }
}