Support

Account

Home Forums General Issues class for extends acf_location

Solving

class for extends acf_location

  • Good morning

    I am trying to get this code to work. He should normally add an option to the localization rules to the acf field. This code works when I only use functions but as soon as I transform it into a class I have critical errors. I don’t understand why, can you help me?

    P.S: I specify that I enter these codes on snippets.

    Thank you in advance

    
    // Définition d'une nouvelle classe de règle de localisation ACF
    class ACF_Location_Post_Category_Ancestor extends ACF_Location {
    	
    	// Initialisation de la classe avec un nom, un libellé et une catégorie de règle
    	public function initialize() {
    		$this->name = 'post_category_ancestor'; // Nom de la règle de localisation
    		$this->label = __("Une catégorie du produit", 'acf'); // Libellé affiché dans l'interface d'administration
    		$this->category = 'Publication'; // Catégorie de règle (utilisée pour l'organisation des règles dans l'interface d'administration)
    	}
    
    	// Fonction pour récupérer les choix disponibles pour la règle
    	public function get_values($rule) {
    		// Récupération de toutes les catégories de type 'product_cat'
    		$terms = get_terms(array(
    			'taxonomy' => 'product_cat',
    			'hide_empty' => false,
    		));
    		$choices = array();
    		if (!empty($terms)) {
    			// Ajout des choix en format 'value' => 'label'
    			foreach ($terms as $term) {
    				$choices[$term->term_id] = $term->name;
    			}
    		}
    		return $choices;
    	}
    			
    	// Fonction pour vérifier si la règle correspond à la situation actuelle
    	public function rule_match($rule, $screen, $field_group) {
    		$terms = array();
    		if (array_key_exists('product_cat', $rule['value'])) {
    			$terms = $rule['value']['product_cat'];
    		} else {
    			return false;
    		}
    		// Si aucun terme n'est sélectionné mais un ID de post est fourni, on récupère les termes associés au post
    		if (empty($terms) && isset($field_group['post_id'])) {
    			$post_id = $field_group['post_id'];
    			$terms = wp_get_post_terms($post_id, 'product_cat', array('fields' => 'ids'));
    		}
    		$ancestors = array();
    		// Pour chaque terme sélectionné, on récupère tous les ancêtres et on les ajoute à un tableau
    		foreach ($terms as $term_id) {
    			$ancestors = array_merge(get_ancestors($term_id, 'product_cat'), $ancestors);
    		}
    		$ancestors = array_unique($ancestors);
    		$match = false;
    		// On vérifie si les termes correspondants à la règle sont présents dans les ancêtres
    		foreach ($rule['value']['product_cat'] as $term_id) {
    			if (in_array($term_id, $ancestors)) {
    				$match = true;
    				break;
    			}
    		}
    		// Si l'opérateur est '!=' on inverse le résultat
    		if ($rule['operator'] == '!=') {
    			$match = !$match;
    		}
    		return $match;
    	}
    
    	/** Charge les choix possibles pour la règle
    	 *  @param array $field Les paramètres du champ
    	 *  @return array Les paramètres du champ mis à jour
    	 */
    // Fonction pour charger les choix possibles pour la règle
    public function load_field($field, $rule) {
        $field['choices'] = $this->get_values($rule);
        return $field;
    }
    
    }
    
    // Ajoute la règle de localisation dans les choix possibles 
    add_filter('acf/location/rule_types', function ($choices) { 
        if (!isset($choices['Publication']['post_category_ancestor'])) { 
            $rule = new ACF_Location_Post_Category_Ancestor(); 
            $choices['Publication'][$rule->name] = $rule; 
        } 
        return $choices;
    });
    
    // Ajout des opérateurs 
    add_filter('acf/location/rule_operators/post_category_ancestor', function ($choices) { 
        $choices['=='] = __("a pour ancêtre", 'acf'); 
        $choices['!='] = __("n\'a pas pour ancêtre", 'acf'); 
        return $choices; 
    });
    
    // Fin du code.
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.