isset( $link['title'] ) ? sanitize_text_field( $link['title'] ) : '',
'url' => isset( $link['url'] ) ? esc_url_raw( $link['url'] ) : '',
'size' => isset( $link['size'] ) ? sanitize_text_field( $link['size'] ) : '',
);
// If both title and URL are empty, consider it an invalid/empty row.
if ( empty( $sanitized_link['title'] ) && empty( $sanitized_link['url'] ) ) {
return null;
}
return $sanitized_link;
}
/**
* Saves the meta box data when the post is saved.
*
* @param int $post_id The ID of the post being saved.
* @param WP_Post $post The post object.
*/
public function save_meta_data( $post_id, $post ) {
// 1. Sanitize and verify the nonce for security.
if ( ! isset( $_POST['advanced_download_box_meta_box_nonce'] ) ) {
return;
}
$nonce = sanitize_text_field( wp_unslash( $_POST['advanced_download_box_meta_box_nonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'advanced_download_box_save_meta_box_data' ) ) {
return;
}
// 2. Prevent saving on autosave and check user permissions.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
return;
}
$post_types = array( 'post', 'page' );
if ( ! in_array( $post->post_type, $post_types, true ) ) {
return;
}
// 3. Sanitize and save the description field.
if ( isset( $_POST['advanced_download_box_description'] ) ) {
$description = wp_kses_post( wp_unslash( $_POST['advanced_download_box_description'] ) );
update_post_meta( $post_id, '_advanced_download_box_description', $description );
}
// 4. Sanitize and save the repeater links using the array_map pattern.
// This is the definitive fix for the 'InputNotSanitized' warning.
if ( isset( $_POST['advanced_download_box_links'] ) && is_array( $_POST['advanced_download_box_links'] ) ) {
$raw_links = wp_unslash( (array) $_POST['advanced_download_box_links'] );
$clean_links = array_map( array( $this, 'sanitize_link_entry' ), $raw_links );
// Remove any null (invalid or empty) entries from the array.
$clean_links = array_filter( $clean_links );
update_post_meta( $post_id, '_advanced_download_box_links', $clean_links );
} else {
// If no links are submitted, delete the meta key to clean up the database.
delete_post_meta( $post_id, '_advanced_download_box_links' );
}
}
}