<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- included by main.php after nonce verification; local variables only
// phpcs:disable WordPress.Security.NonceVerification -- nonce verified in main.php before this file is included
// phpcs:disable WordPress.Security.ValidatedSanitizedInput -- inputs sanitized with absint

    global $wpdb;
    $table_name = SCHOLAR_SYNC_DB_AUTHORS;

    $values_array = array(
        'directory_link' => esc_url_raw( scholar_sync_get_param('directory_link') ),
    );
    $result = $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Update bypasses cache safely
        $table_name,
        $values_array,
        array( 'author_id' => absint( scholar_sync_get_param('author-id') ) )
    );

    // clean the url
    unset($_GET['directory_link']);
    $_GET['msg'] = 'update-directory-link';
    $_GET['action'] = '';

    $url = add_query_arg(
        array(
            'page'      => 'scholar-sync-authors-slug',
            'author-id' => absint( scholar_sync_get_param( 'author-id' ) ),
            'msg'       => sanitize_key( isset( $_GET['msg'] ) ? wp_unslash( $_GET['msg'] ) : '' ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
            'action'    => '',
            '_wpnonce'  => wp_create_nonce( 'scholar_sync_notice' ),
        ),
        admin_url( 'admin.php' )
    );

    wp_safe_redirect($url);
    exit();
