<?php

namespace ProudNerds\Plugins;

/**
 * Class: ACF
 *
 * Add changes to the functionality of Advanced Custom Fields.
 */
class ACF {
    /**
     * ACF constructor.
     */
    public function __construct() {
        if (get_template_directory() !== get_stylesheet_directory() && class_exists('acf')) {
            $this->addFilters();
            $this->addActions();
        }
    }

    /**
     * Add actions.
     */
    private function addActions() {
        add_action('init', array(__CLASS__, 'removeTextEditor'), 100, 0);
    }

    /**
     * Add filters.
     */
    private function addFilters() {
        add_filter('acf/settings/save_json', array(__CLASS__, 'ACFJSONSavePoint'), 10, 0);
        add_filter('acf/settings/load_json', array(__CLASS__, 'ACFJSONLoadPoint'), 10, 1);
    }

    /**
     * Set the folder where ACF groups are saved as JSON.
     *
     * @return string The directory where ACF groups are saved as JSON files.
     */
    static function ACFJSONSavePoint(): string {
        return get_stylesheet_directory() . '/assets/general-assets/acf-fields';
    }

    /**
     * Set the folder where ACF groups are loaded as JSON.
     *
     * @param array $paths The array with paths where ACF groups are loaded from.
     * @return array The array with paths where ACF groups are loaded from.
     */
    static function ACFJSONLoadPoint(array $paths): array {
        unset($paths[0]);
        $paths[0] = get_stylesheet_directory() . '/assets/general-assets/acf-fields';
        return $paths;
    }

    /**
     * Remove default text editor.
     */
    static function removeTextEditor() {
        if(!USE_GUTENBERG){
           remove_post_type_support( 'page', 'editor');
        }

    }
}