name; // 1) Excluir explícitamente tipos no deseados if ( in_array( $post_type_name, $blacklist_post_types, true ) ) { continue; } // 2) Excluir tipos internos que comienzan por 'wp_' if ( 0 === strpos( $post_type_name, 'wp_' ) ) { continue; } // 3) Requerir que tenga UI en admin o sea público $has_ui = isset( $post_type_obj->show_ui ) ? (bool) $post_type_obj->show_ui : false; $is_public = isset( $post_type_obj->public ) ? (bool) $post_type_obj->public : false; if ( ! $has_ui && ! $is_public ) { continue; } $filtered_post_types[ $post_type_name ] = $post_type_obj; } // Ordenar por etiqueta humana (singular_name o name) uasort( $filtered_post_types, function( $a, $b ) { $label_a = ! empty( $a->labels->singular_name ) ? $a->labels->singular_name : $a->name; $label_b = ! empty( $b->labels->singular_name ) ? $b->labels->singular_name : $b->name; return strcasecmp( $label_a, $label_b ); } ); // Lista de nombres permitidos para validación al guardar $allowed_post_type_names = array_keys( $filtered_post_types ); /** * 2) Guardado de ajustes (validar contra lista permitida) * * IMPORTANTE: Verificar nonce y permisos ANTES de procesar inputs. * Se usan filtros de PHP que realizan una sanitización inicial, y luego funciones WP específicas. */ // Detectar submit usando filtro seguro (sanitizamos la detección) $submit_detected = filter_input( INPUT_POST, 'aptas_save_settings', FILTER_SANITIZE_SPECIAL_CHARS ); if ( null !== $submit_detected ) { // Verificar nonce — si falla, WP detendrá la ejecución mostrando mensaje de error. check_admin_referer( $admin_nonce_action, $admin_nonce_name ); // Comprobar permisos por seguridad adicional if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'all-post-type-ajax-search' ) ); } // --- Post types enviados (array) --- // Intentamos leer el array usando filter_input con sanitización inicial $raw_post_types = filter_input( INPUT_POST, 'aptas_post_types', FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY ); if ( ! is_array( $raw_post_types ) ) { // Fallback: intentar leer como string simple (compatibilidad con clientes que no envían arrays correctamente) $single_raw = filter_input( INPUT_POST, 'aptas_post_types', FILTER_SANITIZE_SPECIAL_CHARS ); if ( null !== $single_raw && '' !== $single_raw ) { $raw_post_types = array( $single_raw ); } else { $raw_post_types = array(); } } $posted_post_types = array(); if ( ! empty( $raw_post_types ) ) { foreach ( $raw_post_types as $pt_value ) { // sanitize_key es apropiado para slugs/post_type names (minúsculas, a-z0-9_-) $candidate = sanitize_key( (string) $pt_value ); // Aceptar solo si está en la lista permitida if ( in_array( $candidate, $allowed_post_type_names, true ) ) { $posted_post_types[] = $candidate; } } // Eliminar duplicados y reindexar $posted_post_types = array_values( array_unique( $posted_post_types ) ); } // Guardar post types (si admin desmarca todo, guardamos array vacío) update_option( $option_post_types_key, $posted_post_types ); // --- Imagen por defecto --- // Leer y sanear la URL con FILTER_SANITIZE_URL para sanitización temprana $raw_default_image = filter_input( INPUT_POST, 'aptas_default_image', FILTER_SANITIZE_URL ); $raw_default_image = ( null === $raw_default_image ) ? '' : trim( (string) $raw_default_image ); if ( empty( $raw_default_image ) ) { // Si el admin dejó vacío, guardamos el fallback local update_option( $option_default_image_key, $default_image_fallback ); } else { // Sanitizar la URL de manera segura antes de guardarla $sanitized_image_url = esc_url_raw( $raw_default_image ); if ( empty( $sanitized_image_url ) ) { // si la URL no es válida, fallback update_option( $option_default_image_key, $default_image_fallback ); } else { update_option( $option_default_image_key, $sanitized_image_url ); } } // Mensaje seguro escapado echo '
' . esc_html__( 'Settings saved.', 'all-post-type-ajax-search' ) . '