array(
'name' => 'Finales felices',
'singular_name' => 'Final feliz',
'add_new_item' => 'Añadir final feliz',
'edit_item' => 'Revisar final feliz',
'all_items' => 'Finales felices',
'not_found' => 'Aún no hay historias',
'featured_image' => 'Foto',
),
'public' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=animal',
'supports' => array( 'title', 'editor', 'thumbnail' ),
'map_meta_cap' => true,
) );
} );
/* Contador de historias pendientes en el menú */
add_action( 'admin_menu', function () {
global $submenu;
$pending = (int) wp_count_posts( 'ga_historia' )->pending;
foreach ( $submenu['edit.php?post_type=animal'] ?? array() as $i => $item ) {
if ( 'edit.php?post_type=ga_historia' === $item[2] && $pending ) {
$submenu['edit.php?post_type=animal'][ $i ][0] .= ' ' . $pending . '';
}
}
}, 99 );
/** Historias publicadas en el idioma actual (o en cualquiera si no hay). */
function ga_query_stories( $number = 6 ) {
return new WP_Query( array(
'post_type' => 'ga_historia',
'post_status' => 'publish',
'posts_per_page' => $number,
'no_found_rows' => true,
) );
}
/* -------------------------------------------------------------------------
* [ga_final_feliz] formulario para que las familias envíen su historia
* ---------------------------------------------------------------------- */
add_shortcode( 'ga_final_feliz', function () {
$status = sanitize_key( $_GET['historia'] ?? '' );
$privacy = ga_page_url( 'politica-de-privacidad' );
ob_start();
if ( 'ok' === $status ) {
echo '
' . esc_html( ga__( '¡Gracias por compartirlo! Lo revisaremos antes de publicarlo.', 'Eskerrik asko partekatzeagatik! Argitaratu aurretik berrikusiko dugu.' ) ) . '
';
return ob_get_clean();
}
if ( 'error' === $status ) {
echo '' . esc_html( ga__( 'No hemos podido enviarlo. Revisa los campos y que la foto sea JPG, PNG o WebP de menos de 5 MB.', 'Ezin izan dugu bidali. Egiaztatu eremuak eta argazkia JPG, PNG edo WebP dela eta 5 MB baino gutxiago dituela.' ) ) . '
';
}
?>
= 3 ) {
$go( 'error' );
}
set_transient( $key, $count + 1, HOUR_IN_SECONDS );
$animal = sanitize_text_field( wp_unslash( $_POST['animal'] ?? '' ) );
$familia = sanitize_text_field( wp_unslash( $_POST['familia'] ?? '' ) );
$email = sanitize_email( wp_unslash( $_POST['email'] ?? '' ) );
$texto = mb_substr( sanitize_textarea_field( wp_unslash( $_POST['texto'] ?? '' ) ), 0, 800 );
if ( ! $animal || ! $familia || ! is_email( $email ) || ! $texto || empty( $_POST['publicar'] ) || empty( $_POST['privacidad'] ) ) {
$go( 'error' );
}
$story_id = wp_insert_post( array(
'post_type' => 'ga_historia',
'post_status' => 'pending',
'post_title' => $animal,
'post_content' => $texto,
) );
if ( ! $story_id || is_wp_error( $story_id ) ) {
$go( 'error' );
}
update_post_meta( $story_id, '_ga_familia', $familia );
update_post_meta( $story_id, '_ga_email', $email );
update_post_meta( $story_id, '_ga_idioma', 'eu' === ( $_POST['idioma'] ?? '' ) ? 'eu' : 'es' );
// Foto: solo imágenes reales de hasta 5 MB.
if ( ! empty( $_FILES['foto']['tmp_name'] ) && UPLOAD_ERR_OK === $_FILES['foto']['error'] ) {
$file = $_FILES['foto'];
$check = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], array( 'jpg|jpeg' => 'image/jpeg', 'png' => 'image/png', 'webp' => 'image/webp' ) );
if ( $file['size'] > 5 * MB_IN_BYTES || ! $check['type'] || ! @getimagesize( $file['tmp_name'] ) ) {
wp_delete_post( $story_id, true );
$go( 'error' );
}
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$attachment = media_handle_upload( 'foto', $story_id, array(), array( 'test_form' => false, 'mimes' => array( 'jpg|jpeg' => 'image/jpeg', 'png' => 'image/png', 'webp' => 'image/webp' ) ) );
if ( ! is_wp_error( $attachment ) ) {
set_post_thumbnail( $story_id, $attachment );
}
}
wp_mail(
ga_contact_email(),
'[Web] Nuevo final feliz por revisar: ' . $animal,
sprintf( "%s ha enviado la historia de %s.\n\nRevísala y publícala aquí: %s", $familia, $animal, admin_url( 'post.php?action=edit&post=' . $story_id ) )
);
$go( 'ok' );
}
/* En el panel: quién la envió (sin publicar su correo) */
add_action( 'add_meta_boxes_ga_historia', function () {
add_meta_box( 'ga_historia_info', 'Enviada por', function ( $post ) {
printf(
'%s
%s
Idioma: %s
Para publicarla pulsa «Publicar». El nombre de la familia se muestra; el correo no.
',
esc_html( get_post_meta( $post->ID, '_ga_familia', true ) ?: '—' ),
esc_html( get_post_meta( $post->ID, '_ga_email', true ) ?: '' ),
esc_html( get_post_meta( $post->ID, '_ga_idioma', true ) ?: 'es' )
);
}, 'ga_historia', 'side' );
} );