' . "\n";
}
return $tag;
}
public function custom_menu_icon_style() {
echo '';
}
public function add_admin_menu() {
add_menu_page(
'A11ysaurio PDF Checker',
'A11ysaurio',
'upload_files',
'a11ysaurio-pdf-checker',
array( $this, 'render_admin_page' ),
plugin_dir_url( __FILE__ ) . 'assets/images/logo_saurio_V2.png',
30
);
}
public function handle_delete_analysis() {
if ( ! isset( $_GET['attachment_id'] ) ) {
return;
}
$attachment_id = intval( $_GET['attachment_id'] );
check_admin_referer( 'a11ysaurio_delete_' . $attachment_id );
if ( ! current_user_can( 'upload_files' ) ) {
wp_die( 'No tienes permisos suficientes.' );
}
delete_post_meta( $attachment_id, '_a11ysaurio_analysis' );
wp_safe_redirect( admin_url( 'admin.php?page=a11ysaurio-pdf-checker' ) );
exit;
}
public function handle_bulk_delete_analysis() {
if ( ! current_user_can( 'upload_files' ) ) {
wp_die( 'Permisos insuficientes' );
}
check_admin_referer( 'a11ysaurio_bulk_delete_nonce' );
$action = isset( $_POST['action2'] ) ? $_POST['action2'] : '';
if ( $action === 'delete' && !empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) {
foreach ( $_POST['post_ids'] as $post_id ) {
$post_id = intval( $post_id );
if ( $post_id ) {
delete_post_meta( $post_id, '_a11ysaurio_analysis' );
}
}
}
wp_safe_redirect( admin_url( 'admin.php?page=a11ysaurio-pdf-checker' ) );
exit;
}
public function register_bulk_action( $bulk_actions ) {
$bulk_actions['a11ysaurio_analyze'] = __( 'Analizar con A11ysaurio', 'a11ysaurio-pdf-checker' );
return $bulk_actions;
}
public function handle_bulk_action( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== 'a11ysaurio_analyze' ) {
return $redirect_to;
}
$pdf_ids = array();
foreach ( $post_ids as $post_id ) {
if ( get_post_mime_type( $post_id ) === 'application/pdf' ) {
$pdf_ids[] = $post_id;
}
}
if ( empty( $pdf_ids ) ) {
return $redirect_to;
}
$redirect_to = admin_url( 'admin.php?page=a11ysaurio-pdf-checker&bulk_analyze=' . implode( ',', $pdf_ids ) );
return $redirect_to;
}
public function register_dashboard_widget() {
wp_add_dashboard_widget(
'a11ysaurio_dashboard_widget',
'Estado de Accesibilidad PDF (A11ysaurio)',
array( $this, 'render_dashboard_widget' )
);
}
public function render_dashboard_widget() {
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'application/pdf',
'post_status' => 'inherit',
'meta_key' => '_a11ysaurio_analysis',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
$total_pdfs = $query->found_posts;
$total_ok = 0;
$total_warn = 0;
$total_err = 0;
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$meta = get_post_meta( get_the_ID(), '_a11ysaurio_analysis', true );
$data = json_decode( $meta, true );
if ( isset( $data['validation_ok'] ) ) $total_ok += intval( $data['validation_ok'] );
if ( isset( $data['validation_warn'] ) ) $total_warn += intval( $data['validation_warn'] );
if ( isset( $data['validation_error'] ) ) $total_err += intval( $data['validation_error'] );
}
wp_reset_postdata();
}
echo '
';
echo '
';
echo '
PDFs Analizados
';
echo '' . esc_html( $total_pdfs ) . '';
echo '';
echo '
';
echo '
Errores Encontrados
';
echo '' . esc_html( $total_err ) . '';
echo '';
echo '
';
echo 'Aprobados totales: ' . esc_html( $total_ok ) . '
';
echo 'Avisos totales: ' . esc_html( $total_warn ) . '
';
echo '
';
echo 'Ver Historial Completo';
}
public function add_media_row_action( $actions, $post ) {
if ( 'application/pdf' === $post->post_mime_type ) {
$url = admin_url( 'admin.php?page=a11ysaurio-pdf-checker&attachment_id=' . $post->ID );
$has_analysis = get_post_meta( $post->ID, '_a11ysaurio_analysis', true );
$text = $has_analysis ? __( 'Ver Análisis (A11ysaurio)', 'a11ysaurio-pdf-checker' ) : __( 'Analizar PDF (A11ysaurio)', 'a11ysaurio-pdf-checker' );
$actions['a11ysaurio_analyze'] = sprintf(
'%s',
esc_url( $url ),
esc_attr( $text ),
esc_html( $text )
);
}
return $actions;
}
public function add_attachment_details_action( $form_fields, $post ) {
if ( 'application/pdf' === $post->post_mime_type ) {
$url = admin_url( 'admin.php?page=a11ysaurio-pdf-checker&attachment_id=' . $post->ID );
$has_analysis = get_post_meta( $post->ID, '_a11ysaurio_analysis', true );
$text = $has_analysis ? __( 'Ver Análisis', 'a11ysaurio-pdf-checker' ) : __( 'Analizar PDF con A11ysaurio', 'a11ysaurio-pdf-checker' );
$button_html = sprintf(
'%s',
esc_url( $url ),
esc_html( $text )
);
$form_fields['a11ysaurio_analyze'] = array(
'label' => 'A11ysaurio',
'input' => 'html',
'html' => $button_html,
'show_in_edit' => true,
'show_in_modal' => true,
);
}
return $form_fields;
}
public function add_submitbox_action() {
global $post;
if ( $post && 'application/pdf' === $post->post_mime_type ) {
$url = admin_url( 'admin.php?page=a11ysaurio-pdf-checker&attachment_id=' . $post->ID );
$has_analysis = get_post_meta( $post->ID, '_a11ysaurio_analysis', true );
$text = $has_analysis ? __( 'Ver Análisis', 'a11ysaurio-pdf-checker' ) : __( 'Analizar con A11ysaurio', 'a11ysaurio-pdf-checker' );
printf(
'',
esc_url( $url ),
esc_html( $text )
);
}
}
public function enqueue_admin_scripts( $hook_suffix ) {
$allowed_hooks = array('upload.php', 'media-new.php', 'post.php', 'post-new.php', 'toplevel_page_a11ysaurio-pdf-checker');
if ( !in_array($hook_suffix, $allowed_hooks) ) {
return;
}
$is_media_library = ( 'upload.php' === $hook_suffix );
$auto_analyze = get_option('a11ysaurio_auto_analyze') == 1;
$attachment_url = '';
$attachment_id = isset( $_GET['attachment_id'] ) ? intval( $_GET['attachment_id'] ) : 0;
$bulk_ids = array();
if ( isset( $_GET['bulk_analyze'] ) ) {
$bulk_ids = array_map( 'intval', explode( ',', $_GET['bulk_analyze'] ) );
}
// Enqueue JS on the history page so the bulk button works
// Removed early return
if ( $attachment_id ) {
$attachment_url = wp_get_attachment_url( $attachment_id );
}
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'a11ysaurio-admin-css', $plugin_url . 'assets/css/a11ysaurio-admin.css', array(), time() );
wp_enqueue_style( 'a11ysaurio-udl-css', $plugin_url . 'assets/css/udl_styles.css', array(), time() );
wp_enqueue_script( 'pdf-lib', $plugin_url . 'assets/vendor/pdf-lib.min.js', array(), '1.17.1', false );
$bulk_urls = array();
foreach ( $bulk_ids as $id ) {
$bulk_urls[$id] = wp_get_attachment_url( $id );
}
wp_enqueue_script( 'a11ysaurio-admin-js', $plugin_url . 'assets/js/a11ysaurio-admin.js', array(), time(), true );
wp_localize_script( 'a11ysaurio-admin-js', 'a11ysaurioData', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'a11ysaurio_nonce' ),
'attachmentId' => $attachment_id,
'attachmentUrl' => $attachment_url,
'bulkIds' => $bulk_ids,
'bulkUrls' => $bulk_urls,
'pluginUrl' => $plugin_url,
'siteLang' => get_locale(),
'isMediaLibrary'=> $is_media_library,
'autoAnalyzeEnabled' => $auto_analyze,
'i18n' => $this->get_i18n_strings()
) );
}
private function get_i18n_strings() {
$locales_file = plugin_dir_path( __FILE__ ) . '_locales/es/messages.json';
if ( file_exists( $locales_file ) ) {
$json = file_get_contents( $locales_file );
$data = json_decode( $json, true );
$strings = array();
if ( $data ) {
foreach ( $data as $key => $val ) {
$strings[$key] = $val['message'];
}
}
return $strings;
}
return array();
}
private function recursive_sanitize( $data ) {
if ( is_array( $data ) ) {
$sanitized = array();
foreach ( $data as $key => $value ) {
$sanitized_key = sanitize_key( $key );
$sanitized[ $sanitized_key ] = $this->recursive_sanitize( $value );
}
return $sanitized;
} elseif ( is_numeric( $data ) ) {
return $data; // Ints/floats are safe
} elseif ( is_bool( $data ) ) {
return $data; // Bools are safe
} else {
return sanitize_text_field( $data ); // Sanitize string
}
}
public function ajax_check_new_pdfs() {
if ( ! current_user_can( 'upload_files' ) ) {
wp_send_json_error( 'Permisos insuficientes.' );
}
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'application/pdf',
'post_status' => 'inherit',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => '_a11ysaurio_analysis',
'compare' => 'NOT EXISTS'
)
)
);
$query = new WP_Query( $args );
$results = array();
if ( $query->have_posts() ) {
foreach ( $query->posts as $post ) {
$results[] = array(
'id' => $post->ID,
'url' => wp_get_attachment_url( $post->ID )
);
}
}
wp_send_json_success( $results );
}
public function ajax_get_url() {
if ( ! current_user_can( 'upload_files' ) ) {
wp_send_json_error( 'Permisos insuficientes.' );
}
$attachment_id = isset( $_POST['attachment_id'] ) ? intval( $_POST['attachment_id'] ) : 0;
if ( $attachment_id ) {
$url = wp_get_attachment_url( $attachment_id );
if ( $url ) {
wp_send_json_success( array( 'url' => $url ) );
}
}
wp_send_json_error( 'No URL found' );
}
public function ajax_save_results() {
check_ajax_referer( 'a11ysaurio_nonce', 'nonce' );
if ( ! current_user_can( 'upload_files' ) ) {
wp_send_json_error( 'Permisos insuficientes' );
}
$attachment_id = isset( $_POST['attachment_id'] ) ? intval( $_POST['attachment_id'] ) : 0;
$results = isset( $_POST['results'] ) ? wp_unslash( $_POST['results'] ) : '';
if ( $attachment_id && $results ) {
$decoded = json_decode( $results, true );
if ( json_last_error() === JSON_ERROR_NONE ) {
$sanitized_decoded = $this->recursive_sanitize( $decoded );
update_post_meta( $attachment_id, '_a11ysaurio_analysis', wp_json_encode( $sanitized_decoded ) );
wp_send_json_success( 'Guardado correctamente' );
} else {
wp_send_json_error( 'JSON inválido' );
}
} else {
wp_send_json_error( 'Faltan datos' );
}
}
public function render_admin_page() {
$attachment_id = isset( $_GET['attachment_id'] ) ? intval( $_GET['attachment_id'] ) : 0;
$bulk_ids = isset( $_GET['bulk_analyze'] ) ? array_map( 'intval', explode( ',', $_GET['bulk_analyze'] ) ) : array();
?>
A11ysaurio PDF Checker
PDF Checker y Validador de Accesibilidad
Atención: Para analizar un documento nuevo, ve a la Biblioteca de Medios y haz clic en "Analizar PDF (A11ysaurio)" debajo de un archivo PDF.
Análisis por Lotes
Analiza automáticamente todos los PDFs de tu biblioteca que aún no tienen informe de accesibilidad.
Exportar Historial a CSV
Histórico de Análisis
'attachment',
'post_mime_type' => 'application/pdf',
'post_status' => 'inherit',
'meta_key' => '_a11ysaurio_analysis',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '
';
wp_reset_postdata();
} else {
echo '
No hay ningún PDF analizado en el historial.
';
}
?>
'attachment',
'post_mime_type' => 'application/pdf',
'post_status' => 'inherit',
'meta_key' => '_a11ysaurio_analysis',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$title = get_the_title();
$meta = get_post_meta( get_the_ID(), '_a11ysaurio_analysis', true );
$data = json_decode( $meta, true );
$date = isset( $data['timestamp'] ) ? wp_date( get_option('date_format'), intval($data['timestamp'] / 1000) ) : 'Desconocida';
$ok = isset($data['validation_ok']) ? intval($data['validation_ok']) : 0;
$warn = isset($data['validation_warn']) ? intval($data['validation_warn']) : 0;
$error = isset($data['validation_error']) ? intval($data['validation_error']) : 0;
fputcsv($output, array($title, $date, $ok, $warn, $error));
}
wp_reset_postdata();
}
fclose($output);
exit;
}
}
new A11ysaurio_PDF_Checker();