%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/businessmultisite/wp-content/themes/stripes_weekly/
Upload File :
Create Path :
Current File : /var/www/businessmultisite/wp-content/themes/stripes_weekly/functions.php

<?php
/**
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since   Timber 0.1
 */



/**
 * This ensures that Timber is loaded and available as a PHP class.
 * If not, it gives an error message to help direct developers on where to activate
 */
if ( ! class_exists( 'Timber' ) ) {

	add_action(
		'admin_notices',
		function() {
			echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url( admin_url( 'plugins.php#timber' ) ) . '">' . esc_url( admin_url( 'plugins.php' ) ) . '</a></p></div>';
		}
	);

	add_filter(
		'template_include',
		function( $template ) {
			return get_stylesheet_directory() . '/static/no-timber.html';
		}
	);
	return;
}

require_once('includes/dimox_breadcrumbs.php');
// require_once('includes/cf7_data_attributes.php');

/**
 * Sets the directories (inside your theme) to find .twig files
 */
Timber::$dirname = array( 'templates', 'views' );

/**
 * By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
 * No prob! Just set this value to true
 */
Timber::$autoescape = false;


/**
 * We're going to configure our theme inside of a subclass of Timber\Site
 * You can move this to its own file and include here via php's include("MySite.php")
 */
class StarterSite extends Timber\Site {
	/** Add timber support. */
	public function __construct() {
		add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
		add_filter( 'timber/context', array( $this, 'add_to_context' ) );
		add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
		add_action( 'init', array( $this, 'register_post_types' ) );
		add_action( 'init', array( $this, 'register_taxonomies' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'loadScripts' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_style') );
		add_filter( 'pre_get_posts', array( $this, 'custom_posttype_query') );

		add_action( 'wp_ajax_nopriv_load_more_posts', array($this, 'load_more_posts_callback') );
		add_action( 'wp_ajax_load_more_posts', array($this, 'load_more_posts_callback') );

		add_action('wp_ajax_saved_articles', array( $this, 'saved_articles_callback') );
		add_action('wp_ajax_nopriv_saved_articles', array( $this, 'saved_articles_callback') );

		add_action('wp_ajax_get_saved_size', array( $this, 'get_saved_size_callback') );
		add_action('wp_ajax_nopriv_get_saved_size', array( $this, 'get_saved_size_callback') );

		// add_action( 'pre_get_posts', array($this,'pre_get_posts_filter'), 1 );

		parent::__construct();
	}
	/** This is where you can register custom post types. */
	public function register_post_types() {

	}
	/** This is where you can register custom taxonomies. */
	public function register_taxonomies() {

	}

	public function loadScripts() {
	
		wp_enqueue_style('main-style', get_template_directory_uri().'/dist/app.css', array(), $this->get_modified_file('/dist/app.css'));

		wp_enqueue_script('jquery');
		
        wp_enqueue_script( 'main-app', get_template_directory_uri() . '/dist/app.js', array(), $this->get_modified_file('/dist/app.js'), true );
		

		wp_localize_script('main-app', 'theme', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
        ));
    }

	public function admin_style() {
		wp_enqueue_style('admin-styles', get_template_directory_uri().'/dist/admin.css', array(), $this->get_modified_file('/dist/admin.css'));

		wp_enqueue_script('admin-js', get_template_directory_uri() . '/dist/admin.js', array(), $this->get_modified_file('/dist/admin.js'), true);
	}

	
	
	public function get_modified_file($file){
		$dir = __DIR__;
		$filename = $dir.$file;
		if (file_exists($filename)) {
			return filemtime($filename);
		}
		
	}
	

	/** This is where you add some context
	 *
	 * @param string $context context['this'] Being the Twig's {{ this }}.
	 */
	public function add_to_context( $context ) {
		$context['options']   = get_fields('options');
		$context['menu']['primary']  = new \Timber\Menu('primary_navigation');
		$context['menu']['footer_1']  = new \Timber\Menu('footer_1');
		$context['menu']['footer_2']  = new \Timber\Menu('footer_2');
		$context['menu']['footer_3']  = new \Timber\Menu('footer_3');
		$context['page_id'] = get_queried_object_id();
		$context['site']  = $this;
		$context['breadcrumbs'] = dimox_breadcrumbs();
		$context['get_current_url'] = Timber\URLHelper::get_current_url();
		$context['placeholder']   = isset(get_field('placeholder', 'options')['ID']) ? new Timber\Image(get_field('placeholder', 'options')['ID']) : false;
		$context['posts_per_page'] = get_option('posts_per_page');

		if(!$context['placeholder']) {
			switch_to_blog( 1 );
			
			$context['placeholder']   = isset(get_field('placeholder', 'options')['ID']) ? new Timber\Image(get_field('placeholder', 'options')['ID']) : false;
				
			restore_current_blog();
		}

		$context['categories'] = Timber::get_terms('category', [
			'hide_empty' => true,
		]);

		foreach ($context['categories'] as $key => $term) {
	
			$term_articles = get_posts(array('post_type' => 'articles',
			'post_status' => 'publish',
			'posts_per_page' => -1,
			'post_parent' => 0,
			'cat' => $term->ID));
	
			if(empty($term_articles)) {
				unset($context['categories'][$key]);
			}
	
		}

		$context['categories_length'] = ceil(count($context['categories']) / 2);
		
		$context['tags'] = Timber::get_terms('post_tag', [
			'hide_empty' => true,
		]);

		foreach ($context['tags'] as $key => $term) {
	
			$term_articles = get_posts(array('post_type' => 'articles',
			'post_status' => 'publish',
			'posts_per_page' => -1,
			'post_parent' => 0,
			'tag_id' => $term->ID));
	
			if(empty($term_articles)) {
				unset($context['tags'][$key]);
			}
	
		}

		$context['widget_hot_categories'] = get_field('widget_hot_categories','options') ? Timber::get_terms('category', [
			'hide_empty' => true,
			'include' => get_field('widget_hot_categories','options')
		]) : false;

		if($context['widget_hot_categories']) {
			foreach ($context['widget_hot_categories'] as $key => $term) {
	
				$term_articles = get_posts(array('post_type' => 'articles',
				'post_status' => 'publish',
				'posts_per_page' => -1,
				'post_parent' => 0,
				'cat' => $term->ID));
		
				if(empty($term_articles)) {
					unset($context['widget_hot_categories'][$key]);
				}
		
			}
		}

		$context['widget_editorials'] =  get_field('widget_editorials', 'options') ? Timber::get_posts(get_field('widget_editorials', 'options')) : false;
		$context['widget_chronicles'] =  get_field('widget_chronicles', 'options') ? Timber::get_posts(get_field('widget_chronicles', 'options')) : false;
		$context['widget_hot_articles'] =  get_field('widget_hot_articles', 'options') ? Timber::get_posts(get_field('widget_hot_articles', 'options')) : false;

		
		$categories_template_pages = get_posts( array(
			'post_type' => 'page',
			'fields' => 'ids',
			'nopaging' => true,
			'meta_key' => '_wp_page_template',
			'meta_value' => 'page-categories.php'
		) );

		$context['categories_page_url'] = isset($categories_template_pages[0]) ? get_permalink($categories_template_pages[0]) : false;

		$saved_articles_page_url = get_posts( array(
			'post_type' => 'page',
			'fields' => 'ids',
			'nopaging' => true,
			'meta_key' => '_wp_page_template',
			'meta_value' => 'page-saved-articles.php'
		) );

		$context['saved_articles_page_url'] = isset($saved_articles_page_url[0]) ? get_permalink($saved_articles_page_url[0]) : false;

		
		$blogs = get_sites();
		// PC::debug($blogs, '$blogs');
		

		$current_blog = get_current_blog_id();
		// PC::debug($current_blog, '$current_blog');

		$get_main_site_id = get_main_site_id();
		// PC::debug($get_main_site_id, '$get_main_site_id');

		foreach( $blogs as $b ){
			// PC::debug(intval($b->blog_id), '$b->blog_id');
			if(intval($b->blog_id) == $current_blog) {
				$context['get_current_blog'] = $b;
			}
			
		} 

		

		$context['wp_is_mobile'] = wp_is_mobile() ? 'true' : 'false';

		$geo_menu = Timber::get_terms('geo', [
			'hide_empty' => true
		]);
		$context['geo_menu'] = array();
		sort_terms_hierarchically($geo_menu, $context['geo_menu']);

		$context['locations'] = (isset($_COOKIE['geo']) && $_COOKIE['geo'] != '') ? array_map('intval', explode(',', $_COOKIE['geo'] )) : false;
		

		
		

		
		
		return $context;
	}

	public function theme_supports() {

		load_theme_textdomain( 'stripes', get_template_directory() . '/languages' );


		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		/*
		 * Let WordPress manage the document title.
		 * By adding theme support, we declare that this theme does not use a
		 * hard-coded <title> tag in the document head, and expect WordPress to
		 * provide it for us.
		 */
		add_theme_support( 'title-tag' );

		/*
		 * Enable support for Post Thumbnails on posts and pages.
		 *
		 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
		 */
		add_theme_support( 'post-thumbnails' );

		/*
		 * Switch default core markup for search form, comment form, and comments
		 * to output valid HTML5.
		 */
		add_theme_support(
			'html5',
			array(
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
			)
		);

		/*
		 * Enable support for Post Formats.
		 *
		 * See: https://codex.wordpress.org/Post_Formats
		 */
		add_theme_support(
			'post-formats',
			array(
				'aside',
				'image',
				'video',
				'quote',
				'link',
				'gallery',
				'audio',
			)
		);

		register_nav_menus( [
			'primary_navigation' => 'Primary Navigation',
			'footer_1' => 'Footer 1',
			'footer_2' => 'Footer 2',
			'footer_3' => 'Footer 3',
		] );

		add_theme_support( 'menus' );

		if( function_exists('acf_add_options_page') ) {
            acf_add_options_page(array(
                'page_title' => 'Theme options',
                'icon_url' => 'dashicons-admin-settings'
            ));
        }


		if( function_exists('acf_add_options_sub_page') ) {
			if(get_current_blog_id() == get_main_site_id()) {
				acf_add_options_sub_page(array(
					'page_title' 	=> 'Management',
					'menu_title'	=> 'Management',
					'parent_slug'	=> 'edit.php?post_type=banner',
				));
			}
		}

		

	}

	/** This Would return 'foo bar!'.
	 *
	 * @param string $text being 'foo', then returned 'foo bar!'.
	 */
	public function myfoo( $text ) {
		$text .= ' bar!';
		return $text;
	}

	/** This is where you can add your own functions to twig.
	 *
	 * @param string $twig get extension.
	 */
	public function add_to_twig( $twig ) {
		$twig->addExtension( new Twig\Extension\StringLoaderExtension() );
		$twig->addFilter( new Twig\TwigFilter( 'myfoo', array( $this, 'myfoo' ) ) );
		$twig->addFilter( new Twig\TwigFilter( 'get_title', array( $this, 'twig_get_title' ) ) );
		$twig->addFilter( new Twig\TwigFilter( 'placeholder', array($this, 'get_placeholder') ) );
		$twig->addFilter( new Twig\TwigFilter( 'get_primary_category', array($this, 'get_primary_category') ) );
		$twig->addFilter( new Twig\TwigFilter( 'get_geo', array($this, 'get_geo') ) );
		$twig->addFilter( new Twig\TwigFilter( 'get_company', array($this, 'get_company') ) );
		$twig->addFilter( new Twig\TwigFilter( 'get_serie', array($this, 'get_serie') ) );

		$twig->addFunction( new Timber\Twig_Function( 'get_banner', array($this, 'get_banner') ) );
		$twig->addFunction( new Timber\Twig_Function( 'get_banner_by_post_id', array($this, 'get_banner_by_post_id') ) );
		
		
		
		// $twig->addFunction( new Timber\Twig_Function( 'cms_is_in_menu', array($this, 'cms_is_in_menu_callback') ) );

		return $twig;
	}


	public function twig_get_title($id) {
		$title = get_the_title($id);
		$custom_title = get_field('custom_title', $id);
		if( (isset($custom_title['strong']) && !empty($custom_title['strong'])) || (isset($custom_title['regular']) && !empty($custom_title['regular'])) ) {

			if(!empty($custom_title['strong'])) {
				$title = '<b>'.$custom_title['strong'].'</b>';
				$title .= ' '.$custom_title['regular'];
			} else {
				$title = $custom_title['regular'];
			}
			
		}
		
		return $title;
	}

	public function get_placeholder($image) {
		if(!$image || (is_object($image) && !$image->ID)) {
			$context = Timber::context();
			$image = $context['placeholder'];
		}
		
		return $image;
	}

	
	public function get_saved_size_callback() {
		$cnt = 0;
		
		if(array_key_exists('postsId', $_POST)) {
			$postsId = $_POST['postsId'];
	
			$args = array(
				'post_type' => array('articles','chronicle'),
				'posts_per_page' => -1,
			);
	
			$query = new WP_Query($args); 
			while ($query->have_posts()) {
				$query->the_post();
				if (in_array(get_the_ID(), $postsId)) {
					$cnt++;
				}
			}
		}
		
		echo $cnt;
	
	
		die(-1);
	}
	

	public function saved_articles_callback() {
		//PC::debug($_REQUEST, '$_REQ');
		
		$postsId = isset($_REQUEST['postsId']) ? explode(',', $_REQUEST['postsId']) : false;
		if($postsId) {

			$context = Timber::get_context();
			
			$context['posts'] = Timber::get_posts(array(
				'post_type' => 'articles',
				'posts_per_page' => -1,
				'post__in' => $postsId
			));

			$result['html'] = Timber::compile_string( '
			{% for post in posts %}
				<div class="col-xs-12 col-lg-6">
				{% include \'components/article-small.twig\' with { saved: true } %}
				</div>
			{% endfor %}
			', $context );

		}

		echo json_encode($result);
		exit();
	}

	public function get_banner($banner_id, $page, $wp_is_mobile, $domain, $slug = false) {

		// PC::debug($banner_id, 'banner_id');
		// PC::debug($page, 'page');
		// PC::debug($slug, 'slug');
		
		// PC::debug($wp_is_mobile, 'wp_is_mobile');
		// PC::debug($domain, 'domain');

		switch_to_blog( 1 );
			
		$sites = get_field('site', 'options');
			
		restore_current_blog();

		// PC::debug($sites, '$sites');

		foreach ($sites as $site) {
			if($site['domain'] == $domain) {
				$current_site = $site;
			}
		}


		if($page == 'header') {
			$current_page = $current_site['header'];
			$current_banner = isset($current_page['banner_'.$banner_id]) ? $current_page['banner_'.$banner_id] : false;
		}

		if($page == 'footer') {
			$current_page = $current_site['footer'];
			$current_banner = isset($current_page['banner_'.$banner_id]) ? $current_page['banner_'.$banner_id] : false;
		}


		if($page == 'index') {
			$current_page = $current_site['index'];
			$current_banner = isset($current_page['banner_'.$banner_id]) ? $current_page['banner_'.$banner_id] : false;

			// PC::debug($current_page, '$current_page');
			// PC::debug($current_banner, '$current_banner');
		}

		if($page == 'category') {

			$current_page = false;
			
			if($slug) {
				foreach ($current_site['categories'] as $category) {
					if($category['category'] == $slug) {
						$current_page = $category;
					}
				}
			}
			
			if($current_page && $current_page['banner_'.$banner_id]) {
				$current_banner = isset($current_page['banner_'.$banner_id]) ? $current_page['banner_'.$banner_id] : false;
			} else {
				$current_page = $current_site['category_default'];
				$current_banner = isset($current_page['banner_'.$banner_id]) ? $current_page['banner_'.$banner_id] : false;
			}
			
		}


		if($page == 'tag' || $page == 'author' || $page == 'company') {
			$current_page = $current_site['category_default'];
			$current_banner = isset($current_page['banner_'.$banner_id]) ? $current_page['banner_'.$banner_id] : false;
		}

		if($page == 'articles') {
			$current_page = $current_site['article'];
			$current_banner = isset($current_page['banner_'.$banner_id]) ? $current_page['banner_'.$banner_id] : false;
		}

		if($page == 'chronicle') {
			$current_page = $current_site['chronicle'];
			$current_banner = isset($current_page['banner_'.$banner_id]) ? $current_page['banner_'.$banner_id] : false;
		}


		if (isset($current_banner) && $current_banner) {
			switch_to_blog( 1 );
			if ($wp_is_mobile == 'false') {
				$image = get_field('image_desk', $current_banner);
			} else {
				$image = get_field('image_mob', $current_banner);
			}
			$link = get_field('link', $current_banner);
			restore_current_blog();
			return '<a href="'.$link.'" target="_blank">
				<img src="'.$image.'" alt="">
			</a>';
			
		} else {
			return false;
		}

		


		
		
		// return 'get_banner_return';
	}

	public function get_banner_by_post_id($wp_is_mobile, $banner_post = false) {

		$current_banner = $banner_post;

		if (isset($current_banner) && $current_banner) {
			switch_to_blog( 1 );
			if ($wp_is_mobile == 'false') {
				$image = get_field('image_desk', $current_banner);
			} else {
				$image = get_field('image_mob', $current_banner);
			}
			$link = get_field('link', $current_banner);
			restore_current_blog();
			return '<a href="'.$link.'" target="_blank">
				<img src="'.$image.'" alt="">
			</a>';
			
		} else {
			return false;
		}

	}

	

	public function custom_posttype_query( $query ) {
		if( (is_category() || is_tag()) && $query->is_archive() && empty( 
			$query->query_vars['suppress_filters'] ) && !is_admin()  && $query->is_main_query() ) {
			// $query->set( 'post_type', array('articles', 'chronicle'));
			$query->set( 'post_type', array('articles'));
			$query->set( 'post_parent', 0);
			if(isset($_COOKIE['geo']) && $_COOKIE['geo'] != '') {
				
				$tax_query = $query->get( 'tax_query' );
				if ( ! is_array( $tax_query ) ) {
					$tax_query = array();
				}
				$tax_query[] = array(
					'taxonomy' => 'geo',
					'field'    => 'id',
					'terms'    => explode(',',$_COOKIE['geo'])
				);
				$query->set( 'tax_query', $tax_query );
			}
			
		}

		if($query->is_archive() && $query->is_post_type_archive() && $query->is_main_query() && !is_admin() ) {
			if(isset($_COOKIE['geo']) && $_COOKIE['geo'] != '') {
				
				$tax_query = $query->get( 'tax_query' );
				if ( ! is_array( $tax_query ) ) {
					$tax_query = array();
				}
				$tax_query[] = array(
					'taxonomy' => 'geo',
					'field'    => 'id',
					'terms'    => explode(',',$_COOKIE['geo'])
				);

				$query->set( 'tax_query', $tax_query );
			}
		}

		if( is_author() && $query->is_archive() && empty( 
			$query->query_vars['suppress_filters'] ) && !is_admin()  && $query->is_main_query() ) {
			// $query->set( 'post_type', array('articles', 'chronicle'));
			$query->set( 'post_type', array('articles', 'chronicle'));
			$query->set( 'post_parent', 0);

			if(isset($_COOKIE['geo']) && $_COOKIE['geo'] != '') {
				
				$tax_query = $query->get( 'tax_query' );
				if ( ! is_array( $tax_query ) ) {
					$tax_query = array();
				}
				$tax_query[] = array(
					'taxonomy' => 'geo',
					'field'    => 'id',
					'terms'    => explode(',',$_COOKIE['geo'])
				);

				$query->set( 'tax_query', $tax_query );
			}
			
		}
	
		return $query;
	}


	public function get_primary_category($post_id) {
		if (function_exists('yoast_get_primary_term_id') && yoast_get_primary_term_id('category', $post_id)) {
			$category = yoast_get_primary_term_id('category', $post_id);
		} else {
			$category = get_the_category($post_id);
			$category = !empty($category) ? $category[0]->term_id : false;
		}

		
	
		return $category ? new Timber\Term($category) : false;
	}

	public function get_geo($post_id) {
		$geo = wp_get_post_terms( $post_id, 'geo', array('fields' => 'all') );
		$geo_names = [];
		if(!empty($geo)) {
			foreach ($geo as $location) {
				$geo_names[] = $location->name;
			}
		}
		
		return implode(' | ',$geo_names);
	}

	public function get_company($post_id) {

		$company = get_the_terms($post_id, 'company');
		if($company) {
			$company = $company[0];
			$company_link = get_term_link($company->term_id, 'company');
			$company = '<a href="'.$company_link.'">'.$company->name.'</a>';
		}
		
		return $company;
	}

	public function get_serie($post_id) {

		$post = get_post($post_id);

		if($post->post_parent == 0) {

			$children = Timber::get_posts(array(
				'post_type' => $post->post_type,
				'post_status' => 'publish',
				'posts_per_page' => -1,
				'post_parent' => $post->ID
			));	

		} else {
			$post_parent = Timber::get_post($post->post_parent);

			$children = Timber::get_posts(array(
				'post_type' => $post->post_type,
				'post_status' => 'publish',
				'posts_per_page' => -1,
				'post_parent' => $post->post_parent,
				'post__not_in' => array($post_id)
			));	

			array_unshift($children, $post_parent);
		}
		
		return empty($children) ? false : $children;

	}

	public function load_more_posts_callback() {
		// PC::debug($_REQUEST, '$_REQUEST');

		$result = [];

		$result['html'] = '';
		$result['hide_load_more'] = false;


		$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : false;
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : false;
		$offset = isset($_REQUEST['offset']) ? intval($_REQUEST['offset']) : false;
		$foundposts = isset($_REQUEST['foundposts']) ? intval($_REQUEST['foundposts']) : 0;
		$load_more_quantity = get_field('load_more_quantity', 'options') ? intval(get_field('load_more_quantity', 'options')) : 2;

		$args = [
			'post_status' => 'publish',
			'posts_per_page' => $load_more_quantity,
			'post_parent' => 0,
			'offset' => $offset
		];

		if($type == 'front-page') {
			$args['post_type'] = 'articles';
		}

		if($type == 'post_tag' && $id) {
			$args['post_type'] = 'articles';
			$args['tag_id'] = $id;
		}

		if($type == 'category' && $id) {
			$args['post_type'] = 'articles';
			$args['cat'] = $id;
		}

		if($type == 'chronicle') {
			$args['post_type'] = 'chronicle';
		}

		if($type == 'author') {
			$args['post_type'] = ['articles','chronicle'];
			$args['author'] = $id;

		}
		

		if($type == 'company' && $id) {
			$args['post_type'] = 'articles';
			$args['tax_query'] = array(
				array(
					'taxonomy' => 'company',
					'field'    => 'id',
					'terms'    => $id
				)
			);
		}

		if(isset($_COOKIE['geo']) && $_COOKIE['geo'] != '') {
				
			
			if ( !isset($args['tax_query']) )  {
				$args['tax_query'] = array();
			}
			$args['tax_query'][] = array(
				'taxonomy' => 'geo',
				'field'    => 'id',
				'terms'    => explode(',',$_COOKIE['geo'])
			);
		}


		$posts = Timber::get_posts($args);

		// PC::debug($posts, '$posts');

		$context = Timber::context();
		$context['location'] = 'main';

		if(!empty($posts)) {
			foreach($posts as $post) {
				$context['post'] = $post;
				if($type == 'author') {
					$result['html'] .= Timber::compile( 'components/article-small.twig', $context );
				} else {
					$result['html'] .= Timber::compile( 'components/article-big.twig', $context );
				}
			}
		}
	
		$result['offset'] = $offset + $load_more_quantity;

		if($result['offset'] >= $foundposts ) {
			$result['hide_load_more'] = true;
		}	

		echo json_encode($result);

		exit();
	}
	

}

new StarterSite();

function acf_readonly( $field ) {

    $field['readonly'] = true;
    return $field;
}


add_filter('acf/prepare_field/name=domain', 'acf_readonly');
add_filter('acf/prepare_field/name=category', 'acf_readonly');


add_filter('acf/fields/post_object/result', 'my_acf_fields_post_object_result', 10, 4);
function my_acf_fields_post_object_result( $text, $post, $field, $post_id ) {
	$image = get_field('image_desk', $post->ID);
    $text = '<div style="display: flex; align-items: center;"><img style="width: 30px; height: 30px; border: solid 1px #ccc; object-fit: cover; margin-right: 10px;" src="'.$image.'"><span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">'.$text.'</span></div>';
    return $text;
}

function acf_load_color_field_choices( $field ) {
    
    // reset choices
    $field['choices'] = array();
    
    $blogs = get_sites();
	// PC::debug($blogs, '$blogs');
	foreach( $blogs as $b ){
		// PC::debug($b, '$b');
		$field['choices'][ $b->domain ] = $b->domain;
		//Do stuff
	} 
    
    return $field;
    
}

add_filter('acf/load_field/name=domain', 'acf_load_color_field_choices');



function acf_load_sites_value( $value, $post_id, $field ) {

	$db_value = $value;
	// PC::debug($value, '$value');

	$value = [];
	$blogs = get_sites();
	
	foreach( $blogs as $key => $b ){

		// PC::debug($b->domain, 'domain');
		
		$site_fields = [];
		
		foreach ($db_value as $db_site) {
			
			if($db_site['field_6251f976dff92'] == $b->domain) {

				$categories_banners = [];

				switch_to_blog( $b->blog_id );
			
				$categories = get_terms( [
					'taxonomy' => 'category',
					'hide_empty' => false,
				] );

				// PC::debug($categories, '$categories');

				foreach ($categories as $category) {

					$is_category_saved = false;

					foreach ($db_site['field_6255d26cff586'] as $category_saved) {
						if($category_saved['field_6255d284ff587'] == $category->slug) {
							$categories_banners[] = $category_saved;
							$is_category_saved = true;
						}
					}

					if(!$is_category_saved) {
						$categories_banners[] = [
							'field_6255d284ff587' => $category->slug
						];
					}

				}
					
				restore_current_blog();

				$db_site['field_6255d26cff586'] = $categories_banners;

				// PC::debug($db_site, '$db_site');
				$site_fields = $db_site;
			}
		}

		if(empty($site_fields)) {

			$categories_banners = [];
			
			switch_to_blog( $b->blog_id );
			
			
			$categories = get_terms( [
				'taxonomy' => 'category',
				'hide_empty' => false,
			] );

			foreach ($categories as $category) {
				$categories_banners[] = [
					'field_6255d284ff587' => $category->slug
				];
			}
				
			restore_current_blog();

			$site_fields = [
				'field_6251f976dff92' => $b->domain,
				'field_6255d26cff586' => $categories_banners,
			];
		}

		$value[$key] = $site_fields;
		
	} 

	// PC::debug($value, '$value');

    
	


    return $value;
}

add_filter('acf/load_value/name=site', 'acf_load_sites_value', 10, 3);


function acf_load_multisite_banners( $field ) {
    
    // reset choices
    $field['choices'] = array();

	switch_to_blog( 1 );
			
			
	$banners = get_posts(array(
		'post_type' => 'banner',
		'post_status' => 'publish',
		'posts_per_page' => -1,
	));

	foreach ($banners as $banner) {
		// $field['choices'][ $banner->ID ] = $banner->post_title;
		$image = get_field('image_desk', $banner->ID);
		$text = $banner->post_title;
		$field['choices'][ $banner->ID ] = '<div style="display: flex; align-items: center;"><img style="width: 30px; height: 30px; border: solid 1px #ccc; object-fit: cover; margin-right: 10px;" src="'.$image.'"><span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">'.$text.'</span></div>';
	}

	// PC::debug($banners,'banners');
	
		
	restore_current_blog();
    
    // $blogs = get_sites();
	// // PC::debug($blogs, '$blogs');
	// foreach( $blogs as $b ){
	// 	// PC::debug($b, '$b');
	// 	$field['choices'][ $b->domain ] = $b->domain;
	// 	//Do stuff
	// } 
    
    return $field;
    
}

add_filter('acf/load_field/name=single_banner_1', 'acf_load_multisite_banners');
add_filter('acf/load_field/name=single_banner_2', 'acf_load_multisite_banners');


function sort_terms_hierarchically(Array &$cats, Array &$into, $parentId = 0)
{
    foreach ($cats as $i => $cat) {
        if ($cat->parent == $parentId) {
            $into[$cat->term_id] = $cat;
            unset($cats[$i]);
        }
    }

    foreach ($into as $topCat) {
        $topCat->children = array();
        sort_terms_hierarchically($cats, $topCat->children, $topCat->term_id);
    }
}

function add_locations_to_admin_title( $title, $id = null ) {
	$geo = wp_get_post_terms( $id, 'geo', array('fields' => 'all') );
	$geo_names = [];
	if(!empty($geo)) {
		foreach ($geo as $location) {
			$geo_names[] = $location->name;
		}

		if(isset($_GET['page']) && ($_GET['page'] == 'nestedpages-articles' || $_GET['page'] == 'nestedpages-chronicle')) {
			$title .= ' <span style="color: #f00;">('.implode(' | ',$geo_names).')</span>';
		}
	}
	

	
    return $title;
}
add_filter( 'the_title', 'add_locations_to_admin_title', 10, 2 );


function acf_fields_relationship_result_add_geo( $text, $post, $field, $post_id ) {
	$geo = wp_get_post_terms( $post->ID, 'geo', array('fields' => 'all') );
	$geo_names = [];
	if(!empty($geo)) {
		foreach ($geo as $location) {
			$geo_names[] = $location->name;
		}

		$text .= ' <span style="color: #f00;">('.implode(' | ',$geo_names).')</span>';
	}

    return $text;
}
add_filter('acf/fields/relationship/result', 'acf_fields_relationship_result_add_geo', 10, 4);
//

require_once('acf.php'); 

require_once('cptui.php'); 


	

Zerion Mini Shell 1.0