Skip to content

Instantly share code, notes, and snippets.

@saifsultanc
Created November 25, 2025 19:17
Show Gist options
  • Select an option

  • Save saifsultanc/201faa28816d82c5e04eaf589b0caf4f to your computer and use it in GitHub Desktop.

Select an option

Save saifsultanc/201faa28816d82c5e04eaf589b0caf4f to your computer and use it in GitHub Desktop.
<?php
<?php
/**
* Gravity Perks // Populate Anything // Recteurs d'Académie JSON
*/
class GPPA_Object_Type_Recteurs extends GPPA_Object_Type {
public function __construct( $id ) {
parent::__construct( $id );
add_action( 'gppa_pre_object_type_query_' . $this->id, array( $this, 'add_filter_hooks' ) );
}
public function add_filter_hooks() {
add_filter( 'gppa_object_type_' . $this->id . '_filter', array( $this, 'process_filter_default' ), 10, 4 );
}
public function get_object_id( $object, $primary_property_value = null ) {
return rgar( $object, 'academie' );
}
public function get_label() {
return esc_html__( 'Recteurs d\'Académie', 'gp-populate-anything' );
}
public function get_groups() {
return array(
'result_properties' => array(
'label' => esc_html__( 'Propriétés', 'gp-populate-anything' ),
),
);
}
public function get_properties( $_ = null ) {
$properties = array();
// Only the three properties you need
$json_props = array(
'academie' => 'Académie',
'genre' => 'Genre',
'nom' => 'Nom',
);
foreach ( $json_props as $key => $label ) {
$properties[ $key ] = array(
'group' => 'result_properties',
'label' => $label,
'value' => $key,
'orderby' => true,
'callable' => '__return_empty_array',
'operators' => array(
'is',
'isnot',
'contains',
),
);
}
return $properties;
}
public function fetch() {
$cache_key = 'recteurs_academies_json';
if ( $data = get_transient( $cache_key ) ) {
return $data;
}
$url = 'https://raw.githubusercontent.com/guilamu/liste-des-Recteurs-et-retrices-des-acad-mies-francaises-toujours-jour/main/recteurs.json';
$response = wp_remote_get( $url, array(
'timeout' => 15,
) );
if ( is_wp_error( $response ) ) {
return array();
}
$body = wp_remote_retrieve_body( $response );
$api_response = json_decode( $body, true );
if ( ! is_array( $api_response ) ) {
return array();
}
set_transient( $cache_key, $api_response, DAY_IN_SECONDS );
return $api_response;
}
public function process_filter_default( $search, $args ) {
extract( $args );
$search[ $filter_group_index ][] = array(
'property' => $property_id,
'operator' => $filter['operator'],
'value' => $filter_value,
);
return $search;
}
public function query( $args ) {
$results = $this->fetch();
if ( empty( $results ) ) {
return array();
}
$search_params = $this->process_filter_groups( $args );
unset( $search_params['limit'] );
if ( ! empty( $search_params ) ) {
$results = array_filter( $results, function( $item ) use ( $search_params ) {
foreach ( $search_params as $group ) {
$group_match = true;
foreach ( $group as $filter ) {
$prop = $filter['property'];
$value = $filter['value'];
$operator = $filter['operator'];
if ( ! isset( $item[ $prop ] ) ) {
$group_match = false;
break;
}
$item_value = $item[ $prop ];
switch ( $operator ) {
case 'is':
if ( $item_value != $value ) {
$group_match = false;
}
break;
case 'isnot':
if ( $item_value == $value ) {
$group_match = false;
}
break;
case 'contains':
if ( stripos( $item_value, $value ) === false ) {
$group_match = false;
}
break;
}
if ( ! $group_match ) {
break;
}
}
if ( $group_match ) {
return true;
}
}
return false;
});
}
$query_limit = gp_populate_anything()->get_query_limit( $this, $args['field'] );
return array_slice( $results, 0, $query_limit );
}
public function get_object_prop_value( $object, $prop ) {
if ( ! isset( $object[ $prop ] ) ) {
return null;
}
return $object[ $prop ];
}
}
add_action( 'init', function() {
if ( function_exists( 'gp_populate_anything' ) ) {
gp_populate_anything()->register_object_type( 'recteurs', 'GPPA_Object_Type_Recteurs' );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment