change logo and colors
This commit is contained in:
@@ -14,6 +14,9 @@ defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/** Idioma actual: 'es' o 'eu'. El panel de administración siempre en castellano. */
|
||||
function ga_lang() {
|
||||
if ( isset( $GLOBALS['ga_forced_lang'] ) ) {
|
||||
return 'eu' === $GLOBALS['ga_forced_lang'] ? 'eu' : 'es';
|
||||
}
|
||||
if ( is_admin() && ! wp_doing_ajax() ) {
|
||||
return 'es';
|
||||
}
|
||||
@@ -108,6 +111,11 @@ add_action( 'init', function () {
|
||||
'menu_position' => 5,
|
||||
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
|
||||
'show_in_rest' => true,
|
||||
'rest_base' => 'animales',
|
||||
// WPGraphQL
|
||||
'show_in_graphql' => true,
|
||||
'graphql_single_name' => 'animal',
|
||||
'graphql_plural_name' => 'animales',
|
||||
) );
|
||||
|
||||
register_taxonomy( 'especie', 'animal', array(
|
||||
@@ -119,6 +127,10 @@ add_action( 'init', function () {
|
||||
'hierarchical' => true,
|
||||
'show_admin_column' => true,
|
||||
'show_in_rest' => true,
|
||||
'rest_base' => 'especies',
|
||||
'show_in_graphql' => true,
|
||||
'graphql_single_name' => 'especie',
|
||||
'graphql_plural_name' => 'especies',
|
||||
'rewrite' => array( 'slug' => 'especie', 'with_front' => false ),
|
||||
) );
|
||||
|
||||
@@ -632,3 +644,121 @@ function ga_handle_contact() {
|
||||
|
||||
$go( 'ok' );
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* APIs: REST (/wp-json/wp/v2/animales) y GraphQL (/graphql)
|
||||
* ---------------------------------------------------------------------- */
|
||||
|
||||
/** Ficha de un animal lista para las APIs: valores internos + textos legibles. */
|
||||
function ga_animal_api_data( $post_id ) {
|
||||
$lang = function_exists( 'pll_get_post_language' ) ? ( pll_get_post_language( $post_id ) ?: 'es' ) : 'es';
|
||||
$list = fn( $key ) => array_values( array_filter( explode( ',', ga_get( $post_id, $key ) ) ) );
|
||||
$data = array(
|
||||
'estado' => ga_get( $post_id, 'estado' ) ?: 'disponible',
|
||||
'sexo' => ga_get( $post_id, 'sexo' ),
|
||||
'edad' => '' === ga_get( $post_id, 'edad' ) ? null : (float) ga_get( $post_id, 'edad' ),
|
||||
'edad_unidad' => ga_get( $post_id, 'edad_unidad' ),
|
||||
'tamano' => ga_get( $post_id, 'tamano' ),
|
||||
'raza' => ga_get( $post_id, 'raza' ),
|
||||
'llegada' => ga_get( $post_id, 'llegada' ),
|
||||
'salud' => $list( 'salud' ),
|
||||
'compatible' => $list( 'compatible' ),
|
||||
'idioma' => $lang,
|
||||
);
|
||||
// Textos legibles en el idioma del animal.
|
||||
$labels = function () use ( $post_id ) {
|
||||
return array(
|
||||
'estado' => ga_label( $post_id, 'estado' ) ?: ga__( 'En adopción', 'Adopzioan' ),
|
||||
'sexo' => ga_label( $post_id, 'sexo' ),
|
||||
'edad' => ga_age( $post_id ),
|
||||
'tamano' => ga_label( $post_id, 'tamano' ),
|
||||
'salud' => ga_label( $post_id, 'salud' ) ?: array(),
|
||||
'compatible' => ga_label( $post_id, 'compatible' ) ?: array(),
|
||||
);
|
||||
};
|
||||
$data['textos'] = ga_with_lang( $lang, $labels );
|
||||
return $data;
|
||||
}
|
||||
|
||||
/** Ejecuta $callback como si el idioma actual fuera $lang (para las etiquetas). */
|
||||
function ga_with_lang( $lang, callable $callback ) {
|
||||
$GLOBALS['ga_forced_lang'] = $lang;
|
||||
try {
|
||||
return $callback();
|
||||
} finally {
|
||||
unset( $GLOBALS['ga_forced_lang'] );
|
||||
}
|
||||
}
|
||||
|
||||
/* REST: campo «ficha» de solo lectura en /wp/v2/animales */
|
||||
add_action( 'rest_api_init', function () {
|
||||
register_rest_field( 'animal', 'ficha', array(
|
||||
'get_callback' => fn( $post ) => ga_animal_api_data( $post['id'] ),
|
||||
'schema' => array(
|
||||
'description' => 'Ficha del animal (valores internos y textos legibles en su idioma).',
|
||||
'type' => 'object',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
'properties' => array(
|
||||
'estado' => array( 'type' => 'string', 'enum' => array( 'disponible', 'urgente', 'acogida', 'reservado', 'adoptado' ) ),
|
||||
'sexo' => array( 'type' => 'string' ),
|
||||
'edad' => array( 'type' => array( 'number', 'null' ) ),
|
||||
'edad_unidad' => array( 'type' => 'string', 'enum' => array( '', 'anos', 'meses' ) ),
|
||||
'tamano' => array( 'type' => 'string' ),
|
||||
'raza' => array( 'type' => 'string' ),
|
||||
'llegada' => array( 'type' => 'string', 'format' => 'date' ),
|
||||
'salud' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
|
||||
'compatible' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
|
||||
'idioma' => array( 'type' => 'string' ),
|
||||
'textos' => array( 'type' => 'object' ),
|
||||
),
|
||||
),
|
||||
) );
|
||||
} );
|
||||
|
||||
/* GraphQL: tipo AnimalFicha y campo «ficha» en Animal */
|
||||
add_action( 'graphql_register_types', function () {
|
||||
register_graphql_object_type( 'AnimalFichaTextos', array(
|
||||
'description' => 'Textos legibles de la ficha, en el idioma del animal.',
|
||||
'fields' => array(
|
||||
'estado' => array( 'type' => 'String' ),
|
||||
'sexo' => array( 'type' => 'String' ),
|
||||
'edad' => array( 'type' => 'String' ),
|
||||
'tamano' => array( 'type' => 'String' ),
|
||||
'salud' => array( 'type' => array( 'list_of' => 'String' ) ),
|
||||
'compatible' => array( 'type' => array( 'list_of' => 'String' ) ),
|
||||
),
|
||||
) );
|
||||
register_graphql_object_type( 'AnimalFicha', array(
|
||||
'description' => 'Ficha del animal en adopción.',
|
||||
'fields' => array(
|
||||
'estado' => array( 'type' => 'String', 'description' => 'disponible, urgente, acogida, reservado o adoptado' ),
|
||||
'sexo' => array( 'type' => 'String' ),
|
||||
'edad' => array( 'type' => 'Float' ),
|
||||
'edadUnidad' => array( 'type' => 'String', 'description' => 'anos o meses' ),
|
||||
'tamano' => array( 'type' => 'String' ),
|
||||
'raza' => array( 'type' => 'String' ),
|
||||
'llegada' => array( 'type' => 'String' ),
|
||||
'salud' => array( 'type' => array( 'list_of' => 'String' ) ),
|
||||
'compatible' => array( 'type' => array( 'list_of' => 'String' ) ),
|
||||
'idioma' => array( 'type' => 'String' ),
|
||||
'textos' => array( 'type' => 'AnimalFichaTextos' ),
|
||||
),
|
||||
) );
|
||||
register_graphql_field( 'Animal', 'ficha', array(
|
||||
'type' => 'AnimalFicha',
|
||||
'resolve' => function ( $post ) {
|
||||
$data = ga_animal_api_data( $post->databaseId );
|
||||
$data['edadUnidad'] = $data['edad_unidad'];
|
||||
return $data;
|
||||
},
|
||||
) );
|
||||
} );
|
||||
|
||||
/* Documentación Swagger (/rest-api/docs/): solo para usuarios conectados, salvo API_DOCS_PUBLIC=true */
|
||||
add_action( 'template_redirect', function () {
|
||||
if ( 'docs' === get_query_var( 'swagger_api' ) && 'true' !== getenv( 'API_DOCS_PUBLIC' ) && ! is_user_logged_in() ) {
|
||||
auth_redirect();
|
||||
}
|
||||
}, 1 );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user