Files
gureametsabilagun/wp-content/mu-plugins/gure-ametsa/questionnaire.php
Josep Piguillem d7a17a2aca Cuestionario de adopción online y hueco para el aviso de nuevos animales
- [ga_cuestionario]: preguntas editables en ES/EU (Personalizar), borrador guardado en el
  navegador, envío a Mensajes y por correo. Páginas «Cuestionario de adopción /
  Adopzio-galdetegia» fuera del menú y con noindex; admite ?adoptar=ID.
- Pie: formulario de suscripción de MailPoet cuando se configure su shortcode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 10:32:29 +02:00

125 lines
6.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Cuestionario de adopción online [ga_cuestionario]. Las preguntas se editan en
* Personalizar → Adopción y colabora → Cuestionario. La página no se indexa.
*/
defined( 'ABSPATH' ) || exit;
function ga_questionnaire_questions() {
return array_values( array_filter( array_map( 'trim', explode( "\n", (string) ga_setting( 'cuestionario', 'questions', '' ) ) ) ) );
}
add_shortcode( 'ga_cuestionario', function () {
$status = sanitize_key( $_GET['cuestionario'] ?? '' );
$animal_id = absint( $_GET['adoptar'] ?? 0 );
$animal = $animal_id && 'animal' === get_post_type( $animal_id ) ? get_the_title( $animal_id ) : '';
$questions = ga_questionnaire_questions();
$privacy = ga_page_url( 'politica-de-privacidad' );
ob_start();
if ( 'ok' === $status ) {
echo '<div class="ga-notice ga-notice--ok" role="status" data-ga-clear-draft>' . esc_html( ga_setting( 'cuestionario', 'confirm', '' ) ) . '</div>';
return ob_get_clean();
}
if ( 'error' === $status ) {
echo '<div class="ga-notice ga-notice--error" role="alert">' . esc_html( ga__( 'Falta algún dato obligatorio. Revisa el formulario: lo que habías escrito se ha guardado.', 'Derrigorrezko daturen bat falta da. Berrikusi formularioa: idatzitakoa gorde da.' ) ) . '</div>';
}
?>
<form class="ga-form ga-questionnaire" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" data-ga-draft="ga-cuestionario-<?php echo esc_attr( ga_lang() ); ?>">
<input type="hidden" name="action" value="ga_cuestionario">
<input type="hidden" name="idioma" value="<?php echo esc_attr( ga_lang() ); ?>">
<input type="hidden" name="redirect" value="<?php echo esc_url( get_permalink() ); ?>">
<?php wp_nonce_field( 'ga_cuestionario', 'ga_nonce' ); ?>
<div class="ga-hp" aria-hidden="true"><label>No rellenar <input type="text" name="web" tabindex="-1" autocomplete="off"></label></div>
<fieldset>
<legend><?php ga_e( 'Tus datos', 'Zure datuak' ); ?></legend>
<div class="ga-form__row">
<label><?php ga_e( 'Nombre y apellidos', 'Izen-abizenak' ); ?> *<input type="text" name="nombre" required autocomplete="name"></label>
<label><?php ga_e( 'Correo electrónico', 'Posta elektronikoa' ); ?> *<input type="email" name="email" required autocomplete="email"></label>
</div>
<div class="ga-form__row">
<label><?php ga_e( 'Teléfono', 'Telefonoa' ); ?> *<input type="tel" name="telefono" required autocomplete="tel"></label>
<label><?php ga_e( 'Localidad', 'Herria' ); ?><input type="text" name="localidad" autocomplete="address-level2"></label>
</div>
<label><?php ga_e( 'Animal por el que te interesas', 'Interesatzen zaizun animalia' ); ?><input type="text" name="animal" value="<?php echo esc_attr( $animal ); ?>"></label>
</fieldset>
<fieldset>
<legend><?php ga_e( 'Sobre ti y tu hogar', 'Zuri eta zure etxeari buruz' ); ?></legend>
<?php foreach ( $questions as $i => $question ) : ?>
<label><?php echo esc_html( $question ); ?> *
<textarea name="respuestas[<?php echo (int) $i; ?>]" rows="3" required></textarea>
</label>
<?php endforeach; ?>
</fieldset>
<label class="ga-form__check">
<input type="checkbox" name="privacidad" value="1" required>
<span><?php printf( wp_kses( ga__( 'He leído y acepto la <a href="%s" target="_blank">política de privacidad</a>. *', '<a href="%s" target="_blank">Pribatutasun-politika</a> irakurri eta onartzen dut. *' ), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ), esc_url( $privacy ) ); ?></span>
</label>
<p class="ga-form__hint"><?php ga_e( 'Lo que escribes se guarda en este navegador hasta que lo envíes.', 'Idazten duzuna nabigatzaile honetan gordetzen da bidali arte.' ); ?></p>
<button type="submit" class="ga-btn"><?php ga_e( 'Enviar cuestionario', 'Galdetegia bidali' ); ?></button>
</form>
<?php
return ob_get_clean();
} );
add_action( 'admin_post_nopriv_ga_cuestionario', 'ga_handle_questionnaire' );
add_action( 'admin_post_ga_cuestionario', 'ga_handle_questionnaire' );
function ga_handle_questionnaire() {
$redirect = wp_validate_redirect( esc_url_raw( wp_unslash( $_POST['redirect'] ?? '' ) ), home_url( '/' ) );
$go = function ( $status ) use ( $redirect ) {
wp_safe_redirect( add_query_arg( 'cuestionario', $status, $redirect ) );
exit;
};
if ( ! isset( $_POST['ga_nonce'] ) || ! wp_verify_nonce( $_POST['ga_nonce'], 'ga_cuestionario' ) ) {
$go( 'error' );
}
if ( ! empty( $_POST['web'] ) ) {
$go( 'ok' );
}
$lang = 'eu' === ( $_POST['idioma'] ?? '' ) ? 'eu' : 'es';
$questions = ga_with_lang( $lang, 'ga_questionnaire_questions' );
$answers = array_map( fn( $a ) => sanitize_textarea_field( wp_unslash( $a ) ), (array) ( $_POST['respuestas'] ?? array() ) );
$data = array(
'nombre' => sanitize_text_field( wp_unslash( $_POST['nombre'] ?? '' ) ),
'email' => sanitize_email( wp_unslash( $_POST['email'] ?? '' ) ),
'telefono' => sanitize_text_field( wp_unslash( $_POST['telefono'] ?? '' ) ),
'localidad' => sanitize_text_field( wp_unslash( $_POST['localidad'] ?? '' ) ),
'animal' => sanitize_text_field( wp_unslash( $_POST['animal'] ?? '' ) ),
);
if ( ! $data['nombre'] || ! is_email( $data['email'] ) || ! $data['telefono'] || empty( $_POST['privacidad'] ) ) {
$go( 'error' );
}
foreach ( array_keys( $questions ) as $i ) {
if ( '' === trim( $answers[ $i ] ?? '' ) ) {
$go( 'error' );
}
}
$subject = sprintf( '[Web] Cuestionario de adopción – %s%s', $data['nombre'], $data['animal'] ? ' (' . $data['animal'] . ')' : '' );
$body = sprintf(
"Nombre: %s\nEmail: %s\nTeléfono: %s\nLocalidad: %s\nAnimal: %s\nIdioma: %s\n",
$data['nombre'], $data['email'], $data['telefono'], $data['localidad'] ?: '-', $data['animal'] ?: '-', 'eu' === $lang ? 'Euskera' : 'Castellano'
);
foreach ( $questions as $i => $question ) {
$body .= "\n" . $question . "\n" . ( $answers[ $i ] ?? '' ) . "\n";
}
wp_insert_post( array( 'post_type' => 'ga_mensaje', 'post_status' => 'private', 'post_title' => $subject, 'post_content' => $body ) );
wp_mail( ga_contact_email(), $subject, $body, array( 'Reply-To: ' . $data['nombre'] . ' <' . $data['email'] . '>' ) );
$go( 'ok' );
}
/* La página del cuestionario no aparece en buscadores (el enlace lo envía el equipo). */
add_filter( 'wp_robots', function ( $robots ) {
if ( is_page( array( 'cuestionario-adopcion', 'adopzio-galdetegia' ) ) ) {
$robots['noindex'] = true;
$robots['nofollow'] = true;
}
return $robots;
} );