<?php
if ( ! defined( 'ABSPATH' ) ) exit;

class ScholarSync_Admin {

    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $plugin_name    The ID of this plugin.
     */
    private $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $version    The current version of this plugin.
     */
    private $version;

//    public scholar_sync_author_post_type $scholar_sync_author_instance;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string    $plugin_name       The name of this plugin.
     * @param      string    $version    The version of this plugin.
     */
    public function __construct( $plugin_name, $version ) {

        $this->plugin_name = $plugin_name;
        $this->version = $version;

    }

    /**
     * Register the stylesheets for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_styles() {

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in ScholarSync_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The ScholarSync_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */
        wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/scholar-sync-admin.css', array(), $this->version, 'all' );
    }

    /**
     * Register the JavaScript for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts() {

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in ScholarSync_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The ScholarSync_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */

        $page = isset( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only, used for script enqueue routing
//        if (!empty($page)) {
            if (strpos($page, 'scholar-sync') !== false) {
                wp_enqueue_script('jquery');
                // Footer-only on preview: prevents print_head_scripts() from consuming the handle before preview.php calls wp_print_scripts() inside its standalone HTML.
                $in_footer = ( $page === 'scholar-sync-preview-slug' );
                wp_enqueue_script($this->plugin_name, plugin_dir_url( __FILE__ )  . 'js/scholar-sync-admin.js', array('jquery'), $this->version, $in_footer);
                if ( in_array( $page, array( 'scholar-sync-add-author-slug', 'scholar-sync-authors-slug', 'scholar-sync-co-authors-slug' ), true ) ) {
                    wp_enqueue_media();
                }
                if ( in_array( $page, array( 'scholar-sync-groups-slug' ), true ) ) {
                    wp_enqueue_script(
                        'scholar-sync-groups',
                        plugin_dir_url( __FILE__ ) . 'js/scholar-sync-groups.js',
                        array( 'jquery' ),
                        $this->version,
                        true
                    );
                }
                if ( $page === 'scholar-sync-co-authors-slug' ) {
                    wp_enqueue_script(
                        'scholar-sync-co-author-form',
                        plugin_dir_url( __FILE__ ) . 'js/scholar-sync-co-author-form.js',
                        array( $this->plugin_name ),
                        $this->version,
                        true
                    );
                }
                wp_localize_script( $this->plugin_name, 'scholar_sync_ajax_object',
                    array(
                        'dir'              => SCHOLAR_SYNC_PLUGIN_DIR,
                        'url'              => SCHOLAR_SYNC_SITE_URL,
                        'plugin_url'       => SCHOLAR_SYNC_PLUGIN_URL,
                        'rest'             => rest_url( 'sclr/v1/' ),
                        'rest_nonce'       => wp_create_nonce( 'wp_rest' ),
                        'user_id'          => get_current_user_id(),
                        'ajax_url'         => admin_url( 'admin-ajax.php' ),
                        'add_author_nonce' => wp_create_nonce( 'scholar_sync_save_author_profile' ),
                        'preview_nonce'    => wp_create_nonce( 'scholar_sync_preview' ),
                    )
                );
            }
//        }

    }

    /**
     * Guy 22-Oct-22
     * Create admin menu
     */

    public function scholar_sync_admin_menu() {


        add_menu_page(
            'Scholar Sync', //page title
            'Scholar Sync', //menu title
            'edit_others_posts', //capability
            'scholar-sync-parent-slug', //menu_slug
            array($this, 'scholar_sync_authors_callback'), //callback
//            'dashicons-welcome-learn-more', //icon
            'data:image/svg+xml;base64,' . base64_encode('<?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg    version="1.0"    width="237.000000pt"    height="237.000000pt"    viewBox="0 0 237.000000 237.000000"    preserveAspectRatio="xMidYMid meet"    sodipodi:docname="logo_only_rect.svg"    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"    xmlns="http://www.w3.org/2000/svg"    xmlns:svg="http://www.w3.org/2000/svg">   <path      id="rect14"      style="fill:#000000;stroke:#000000;stroke-width:0.75;fill-opacity:1"      d="M 70.375488 24 C 65.750488 23.875 61.55 24.499902 58.5 25.899902 C 50.8 29.399902 45.900488 38.300293 44.500488 51.300293 C 44.100488 55.000293 43.500098 58.000488 43.100098 58.000488 C 41.700098 58.000488 39 64.000098 39 67.100098 C 39 69.600098 39.900488 71.200586 43.000488 74.100586 C 45.200488 76.200586 46.999512 78.299805 46.999512 78.799805 C 46.999512 79.299805 44.200293 80.800488 40.800293 82.000488 C 30.400293 85.800488 17.3 92.899609 12 97.599609 C 6 103.09961 1.0004883 112.5 1.0004883 118.5 C 1.0004883 132.3 12.7 144.49951 34.5 153.49951 C 38.4 155.09951 41.599316 156.50059 41.799316 156.60059 C 41.800879 156.60137 41.803711 156.60228 41.805176 156.60352 C 41.984356 156.78764 41.388672 160.55273 40.400391 165 C 36.900391 182.3 40.000684 198.49951 48.200684 205.99951 C 59.700684 216.59951 83.699316 212.5998 109.29932 195.7998 C 114.79932 192.1998 117.6 190.9001 118.5 191.6001 C 139 206.2001 156.0001 212.80068 170.6001 211.70068 C 182.8001 210.80068 189.99961 205.4002 193.59961 194.2002 C 195.99961 187.1002 196.00068 171.1002 193.70068 162.7002 L 192.20068 156.89941 L 194.79932 155.79932 C 218.99932 145.69932 229.39961 137.1999 232.59961 124.8999 C 234.49961 117.5999 233.10039 111 227.90039 103.5 C 226.40039 101.3 224.90049 98.000293 224.50049 96.300293 C 223.80049 92.900293 219.10068 88.099414 216.20068 87.899414 C 215.30068 87.799414 212.69951 87.599512 210.49951 87.499512 C 207.59951 87.399512 188.60039 80.899609 187.40039 79.599609 C 187.30039 79.599609 187.89971 76.299512 188.69971 72.499512 C 189.69971 67.799512 189.9998 61.799512 189.7998 54.499512 C 189.3998 45.299512 188.9998 42.599512 186.7998 37.999512 C 181.9998 27.699512 176.19971 24 164.69971 24 C 152.79971 24 141.19932 28.599707 124.29932 40.199707 L 117.20068 45.099609 L 111.09961 40.700684 C 102.39961 34.300684 93.499805 29.500098 84.799805 26.600098 C 80.049805 25.000098 75.000488 24.125 70.375488 24 z M 165.3999 33 C 170.7999 33 171.50049 33.300195 175.00049 36.700195 C 180.70049 42.400195 182.50039 54.000488 179.90039 68.500488 C 178.40039 77.100488 178.30039 77.300488 172.40039 76.000488 C 169.70039 75.300488 162.3 74.100098 156 73.100098 L 144.49951 71.399414 L 134.69971 61.400391 L 124.99951 51.399902 L 129.20068 48.399902 C 137.50068 42.499902 146.29932 37.699414 152.79932 35.399414 C 156.89932 33.999414 161.7999 33 165.3999 33 z M 71.699707 33.300293 C 77.599707 33.600293 80.399902 34.400293 87.399902 37.800293 C 95.399902 41.600293 107.2999 48.999316 108.3999 50.799316 C 108.6999 51.299316 104.50068 56.199609 99.200684 61.599609 L 89.500488 71.600098 L 80.500488 72.799805 C 75.500488 73.499805 68.199707 74.800586 64.199707 75.600586 C 60.199707 76.500586 56.699414 77.000684 56.399414 76.700684 C 56.099414 76.400684 56.900098 74.700488 58.100098 73.000488 C 59.700098 71.000488 60.499512 68.499316 60.499512 65.799316 C 60.499512 62.499316 59.900391 61.3 56.900391 58.5 L 53.299805 55.199707 L 54.200684 49.5 C 55.300684 42.3 58.200293 36.5 61.800293 34.5 C 63.700293 33.4 66.499707 33.100293 71.699707 33.300293 z M 116.99268 58.000488 C 116.99536 58.000147 116.99766 58.000488 117 58.000488 C 117.3 58.000488 120.3998 60.799316 123.7998 64.299316 L 130.00049 70.5 L 117 70.599609 L 103.8999 70.599609 L 110.2002 64.299316 C 113.57363 60.82666 116.65176 58.043769 116.99268 58.000488 z M 136.29932 80.600098 C 139.59932 81.200098 140.7998 82.300391 146.2998 89.900391 C 156.5998 103.90039 163.30059 114.9001 162.60059 116.6001 C 161.00059 120.7001 149.00059 140.50068 143.10059 148.70068 L 136.5 157.99951 L 128.50049 158.2002 C 115.00049 158.6002 98.599707 157.8002 97.199707 156.7002 C 95.099707 155.0002 79.799609 131.50039 75.099609 122.90039 L 70.999512 115.40039 L 76.599609 106.40039 C 84.699609 93.400391 94.199512 81.199707 96.499512 80.699707 C 99.799512 79.999707 132.19932 80.000098 136.29932 80.600098 z M 80.450684 82.174805 C 80.925684 82.174805 81 82.350684 81 82.700684 C 81 83.000684 80.399609 84.099414 79.599609 84.899414 C 78.799609 85.799414 75.399609 90.499414 72.099609 95.399414 C 66.399609 103.79941 66.000586 104.10039 65.100586 101.90039 C 60.000586 89.000391 58.999805 86.599414 59.299805 86.399414 C 59.399805 86.299414 63.1 85.500586 67.5 84.600586 C 71.9 83.800586 76.400488 82.900684 77.500488 82.700684 C 79.100488 82.350684 79.975684 82.174805 80.450684 82.174805 z M 154.55127 82.281738 C 154.59433 82.27951 154.64443 82.285254 154.70068 82.299316 C 155.70068 82.599316 158.3 83.099414 160.5 83.399414 C 173.5 85.499414 175.0999 86.099316 174.3999 88.299316 C 172.9999 93.199316 168.59961 103.9999 168.09961 103.8999 C 167.79961 103.8999 164.19971 98.899316 160.19971 92.799316 C 155.52939 85.746191 153.6758 82.327044 154.55127 82.281738 z M 48.849609 89.355469 C 50.125562 89.335034 50.312109 90.337207 51.099609 92.699707 C 51.699609 94.799707 54.000195 100.6002 56.200195 105.7002 L 60.099609 114.7998 L 56.200195 122.69971 C 52.700195 129.69971 48.299414 139.40068 45.899414 145.70068 L 45.099609 148.00049 L 36.799805 144.49951 C 16.999805 136.09951 8.0001953 125.6002 11.200195 114.7002 C 13.600195 106.5002 26.400098 97.000098 43.100098 91.100098 C 46.206348 90.006348 47.857202 89.371362 48.849609 89.355469 z M 185.8374 89.562012 C 187.4749 89.524512 190.35049 90.650391 196.00049 92.900391 C 202.10049 95.400391 202.50059 95.700293 203.10059 99.300293 C 203.40059 101.50029 204.89941 104.2998 206.39941 105.7998 C 208.59941 108.0998 209.90029 108.49951 214.80029 108.49951 C 220.30029 108.49951 220.60068 108.60049 222.20068 112.00049 C 226.20068 119.90049 223.19971 128.40068 214.19971 135.20068 C 208.49971 139.60068 192.60039 147.29932 190.40039 146.79932 C 189.60039 146.59932 186.99961 141.99932 184.59961 136.29932 C 182.19961 130.69932 178.90068 123.60059 177.20068 120.60059 L 174.09961 115.19971 L 178.00049 106.19971 C 180.10049 101.29971 182.39961 95.400684 183.09961 93.200684 C 183.79961 90.800684 184.1999 89.599512 185.8374 89.562012 z M 117.30029 93 C 116.10029 93.1 92.499707 111.8 92.699707 112.5 C 92.799707 112.9 95.099902 114.89961 97.899902 117.09961 L 102.7998 121.00049 L 104.90039 118.1001 C 108.90039 112.6001 117.89941 110.30049 123.89941 113.50049 C 125.49941 114.30049 127.8999 116.3001 129.3999 118.1001 L 132 121.19971 L 136.99951 117 C 139.79951 114.7 142.00049 112.7 142.00049 112.5 C 142.00049 112.3 136.60049 107.79951 130.00049 102.49951 C 123.40049 97.299512 117.70029 93 117.30029 93 z M 117.36182 114.12451 C 115.89932 114.12451 114.45 114.55039 112.5 115.40039 C 98.9 121.40039 102.9002 141 117.7002 141 C 125.7002 141 131.90049 132.7 130.00049 124.5 C 129.00049 120.1 126.4998 117.20039 122.2998 115.40039 C 120.2998 114.55039 118.82432 114.12451 117.36182 114.12451 z M 65.39209 126 C 65.394592 125.9996 65.39707 126 65.399414 126 C 65.599414 126 67.999609 129.89971 70.599609 134.69971 C 73.299609 139.49971 77.400684 146.09941 79.700684 149.39941 C 83.400684 154.69941 83.700098 155.49932 82.100098 155.79932 C 80.100098 156.09932 56.199316 151.50059 55.299316 150.60059 C 54.599316 149.90059 56.999609 143.39971 61.599609 133.19971 C 63.484766 129.33018 65.07434 126.051 65.39209 126 z M 168.7998 126 C 169.2998 126 178.69941 146.09961 179.39941 148.59961 C 179.99941 150.59961 179.30029 150.90039 169.80029 152.90039 C 164.10029 154.10039 157.40029 155.3001 154.80029 155.6001 L 150 156.30029 L 156.30029 146.90039 C 159.70029 141.70039 163.89951 134.89932 165.49951 131.79932 C 167.19951 128.59932 168.5998 126 168.7998 126 z M 52.100098 160.00049 C 52.500098 160.00049 56.699707 160.90039 61.199707 161.90039 C 65.799707 163.00039 74.7 164.59951 81 165.49951 L 92.500488 167.10059 L 100.8999 175.70068 C 105.4999 180.30068 108.8999 184.50029 108.3999 184.80029 C 91.799902 196.90029 73.000586 204.00039 62.100586 202.40039 C 55.400586 201.40039 50.600488 196.40049 49.000488 188.50049 C 47.700488 182.20049 48.200586 172.19941 50.100586 164.39941 C 50.700586 161.99941 51.600098 160.00049 52.100098 160.00049 z M 180.78662 160.31396 C 183.10693 160.39893 183.82568 161.87451 184.70068 165.49951 C 186.20068 172.09951 186.2999 184.99961 184.8999 190.59961 C 180.9999 206.09961 162.20029 206.69961 136.80029 192.09961 C 132.80029 189.79961 128.50039 187.09961 127.40039 186.09961 L 125.2002 184.29932 L 133.40039 175.8999 L 141.49951 167.39941 L 155.50049 165.20068 C 163.20049 163.90068 171.79951 162.30049 174.49951 161.50049 C 177.42451 160.71299 179.39443 160.26299 180.78662 160.31396 z M 117.7002 168 L 127.40039 168 L 123 172.5 C 120.6 175 118.00029 177 117.30029 177 C 115.80029 177 108 170.19941 108 168.89941 C 108 168.39941 112.4002 168 117.7002 168 z " /> </svg>'),
            100 //position
        );

        $authors_hook = add_submenu_page(
            'scholar-sync-parent-slug', //parent_slug
            'All Scholars', //page_title
            'All Scholars' , //menu_title
            'edit_others_posts', //capability
            'scholar-sync-authors-slug', //menu_slug
            array($this, 'scholar_sync_authors_callback') //callback
        );
        add_action( 'load-' . $authors_hook, array( $this, 'handle_authors_bulk_action' ) );

        add_submenu_page(
            'scholar-sync-parent-slug',
            'Add New',
            'Add New' ,
            'edit_others_posts',
            'scholar-sync-add-author-slug',
            array($this, 'scholar_sync_add_author_callback')
        );

        add_submenu_page(
            'scholar-sync-parent-slug',
            __( 'Groups', 'scholar-sync' ),
            __( 'Groups', 'scholar-sync' ),
            'edit_others_posts',
            'scholar-sync-groups-slug',
            array( $this, 'scholar_sync_groups_callback' )
        );

        add_submenu_page(
            'scholar-sync-parent-slug',
            __( 'Co-Authors', 'scholar-sync' ),
            __( 'Co-Authors', 'scholar-sync' ),
            'edit_others_posts',
            'scholar-sync-co-authors-slug',
            array( $this, 'scholar_sync_co_authors_callback' )
        );

        add_submenu_page(
            'scholar-sync-parent-slug',
            'Scholar Sync Settings',
            'Settings',
            'manage_options',
            'scholar-sync-settings-slug',
            array($this, 'scholar_sync_display_settings_callback')
        );

        $hook = add_submenu_page(
            'hidden-menu',
            'hidden',
            'hidden',
            'edit_others_posts',
            'scholar-sync-preview-slug',
            function () {}
        );
        add_action('load-' . $hook, function() {
            ob_start();
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/preview.php';
            $content = ob_get_contents();
            ob_end_clean();
            echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted plugin-generated admin HTML
            exit;
        });

        $hook = add_submenu_page(
            'hidden-menu',
            'hidden',
            'hidden',
            'edit_others_posts',
            'scholar-sync-actions-slug',
            function () {}
        );
        add_action('load-' . $hook, function() {

            ob_start();
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/actions/main.php';
            $content = ob_get_contents();
            ob_end_clean();
            echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted plugin-generated admin HTML
            exit;
        });

        // Hidden group preview page.
        $hook = add_submenu_page(
            'hidden-menu',
            'hidden',
            'hidden',
            'edit_others_posts',
            'scholar-sync-preview-group-slug',
            function () {}
        );
        add_action( 'load-' . $hook, function () {
            ob_start();
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/group_preview.php';
            $content = ob_get_contents();
            ob_end_clean();
            echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted plugin-generated admin HTML
            exit;
        } );

        // Remove the automatically added "Scholars" submenu
        remove_submenu_page('scholar-sync-parent-slug', 'scholar-sync-parent-slug');

    }


    public function dummy_function() {
    }

    /**
     * Show a consent notice in the admin until the site owner agrees to
     * transmit data to scholar-sync.org (required for WordPress.org listing).
     */
    public function show_consent_notice() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        if ( get_option( 'scholar_sync_service_consent', false ) ) {
            return;
        }
        $screen = get_current_screen();
        if ( ! $screen || strpos( $screen->id, 'scholar-sync' ) === false ) {
            return;
        }
        $action_url = esc_url( admin_url( 'admin-post.php' ) );
        ?>
        <div class="notice notice-warning is-dismissible">
            <p>
                <strong><?php esc_html_e( 'Scholar Sync — External Service Notice', 'scholar-sync' ); ?></strong>
            </p>
            <p>
                <?php
                printf(
                    /* translators: 1: scholar-sync.org link, 2: Terms of Service link, 3: Privacy Policy link */
                    esc_html__( 'Scholar Sync connects to %1$s to sync and display publication data. Your site URL and a unique API key are sent to our service to authenticate your requests. Please review our %2$s and %3$s before connecting.', 'scholar-sync' ),
                    '<a href="https://scholar-sync.org" target="_blank" rel="noopener">scholar-sync.org</a>',
                    '<a href="https://scholar-sync.org/terms" target="_blank" rel="noopener">' . esc_html__( 'Terms of Service', 'scholar-sync' ) . '</a>',
                    '<a href="https://scholar-sync.org/privacy" target="_blank" rel="noopener">' . esc_html__( 'Privacy Policy', 'scholar-sync' ) . '</a>'
                );
                ?>
            </p>
            <form method="post" action="<?php echo esc_url( $action_url ); ?>">
                <?php wp_nonce_field( 'scholar_sync_consent', 'scholar_sync_consent_nonce' ); ?>
                <input type="hidden" name="action" value="scholar_sync_consent">
                <button type="submit" class="button button-primary">
                    <?php esc_html_e( 'I agree — Connect to scholar-sync.org', 'scholar-sync' ); ?>
                </button>
            </form>
        </div>
        <?php
    }

    /**
     * AJAX handler: create a draft WordPress page with a shortcode.
     */
    public function handle_create_page() {
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/create_preview_page.php';
    }

    /**
     * Option name that stores the draft page created for a single author.
     */
    public static function author_page_option_name( $author_id ) {
        return SCHOLAR_SYNC_OPTION_PREFIX . 'created-page-author-' . (int) $author_id;
    }

    /**
     * Option name that stores the draft page created for a group.
     */
    public static function group_page_option_name( $group_id ) {
        return SCHOLAR_SYNC_OPTION_PREFIX . 'created-page-group-' . (int) $group_id;
    }

    /**
     * Resolve the View Page / Edit in WP links for a tracked created page.
     *
     * Returns null when nothing is tracked or the page no longer exists / is
     * trashed; in the latter case the stale option is cleaned up.
     *
     * @param string $option_name Option storing the page id.
     * @return array|null { preview_url, edit_url } or null.
     */
    public static function get_tracked_page_links( $option_name ) {
        $page_id = (int) get_option( $option_name, 0 );
        if ( ! $page_id ) {
            return null;
        }
        $post = get_post( $page_id );
        if ( ! $post || 'trash' === $post->post_status ) {
            delete_option( $option_name );
            return null;
        }
        return array(
            'preview_url' => get_preview_post_link( $page_id ),
            'edit_url'    => get_edit_post_link( $page_id, 'raw' ),
        );
    }

    /**
     * Render the contents of the #wps-create-page-result box for an already
     * existing tracked page. Mirrors the markup produced client-side by
     * public/js/scholar-sync-create-page.js so reopening a preview shows the
     * same View Page / Edit in WP links.
     *
     * @param array|null $links Result of get_tracked_page_links().
     * @return string HTML, or empty string when there is nothing to show.
     */
    public static function render_tracked_page_links_html( $links ) {
        if ( empty( $links ) ) {
            return '';
        }
        $html  = esc_html__( 'Existing page:', 'scholar-sync' ) . ' ';
        $html .= '<a class="wps-create-page-link" href="' . esc_url( $links['preview_url'] ) . '" target="_blank" rel="noopener">' . esc_html__( 'View Page', 'scholar-sync' ) . '</a>';
        if ( ! empty( $links['edit_url'] ) ) {
            $html .= '<a class="wps-create-page-link" href="' . esc_url( $links['edit_url'] ) . '" target="_blank" rel="noopener">' . esc_html__( 'Edit in WP', 'scholar-sync' ) . '</a>';
        }
        return $html;
    }

    /**
     * Handle the admin-post.php consent action.
     */
    public function handle_consent_action() {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( esc_html__( 'Insufficient permissions.', 'scholar-sync' ) );
        }
        check_admin_referer( 'scholar_sync_consent', 'scholar_sync_consent_nonce' );
        update_option( 'scholar_sync_service_consent', true );
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/includes/class-scholar-sync-activator.php';
        ScholarSync_Activator::crete_apikey();
        wp_safe_redirect( admin_url( 'admin.php?page=scholar-sync-add-author-slug' ) );
        exit;
    }

    /**
     * menu callback functions
     */

    public function handle_authors_bulk_action() {
        if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
            return;
        }
        $action = ( isset( $_POST['action'] ) && sanitize_key( wp_unslash( $_POST['action'] ) ) !== '-1' ) ? sanitize_key( wp_unslash( $_POST['action'] ) )
                : ( ( isset( $_POST['action2'] ) && sanitize_key( wp_unslash( $_POST['action2'] ) ) !== '-1' ) ? sanitize_key( wp_unslash( $_POST['action2'] ) ) : '' );
        if ( $action === 'delete-author' && ! empty( $_POST['post'] ) ) {
            check_admin_referer( 'bulk-scholar-sync-authors' );
            require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/delete_author.php';
            scholar_sync_delete_authors( array_map( 'intval', (array) $_POST['post'] ) );
            wp_safe_redirect( add_query_arg( array( 'page' => 'scholar-sync-authors-slug' ), admin_url( 'admin.php' ) ) );
            exit;
        }
    }

    public function scholar_sync_authors_callback() {

        $current_screen = get_current_screen();

        if ($this->has_authors()) {
            ob_start();
            if (isset($_GET['author-id'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin page routing
                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/author_edit_view.php';
            } else {
                require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/authors_list_view.php';
            }
            $content = ob_get_contents();
            ob_end_clean();
            echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted plugin-generated admin HTML
        } else {
            $this->scholar_sync_add_author_callback();
        }
    }

    public function has_authors() {
        global $wpdb;
        $count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', SCHOLAR_SYNC_DB_AUTHORS ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- %i is the WordPress identifier placeholder (WP 6.2+)
        return $count > 0;
    }

    public function scholar_sync_dashboard_menu() {
        echo '<h3>Welcome to Scholar Sync dashboard</h3>';
    }

    public function handle_save_author_profile() {
        require_once SCHOLAR_SYNC_PLUGIN_DIR . '/admin/actions/save_author_profile.php';
    }

   public function scholar_sync_add_author_callback() {

        ob_start();
        if (isset($_GET['manual'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin page routing
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/author_add_manual.php';
        } else {
            require_once plugin_dir_path(dirname(__FILE__)) . 'admin/author_search_view.php';
        }
        $content = ob_get_contents();
        ob_end_clean();
        echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted plugin-generated admin HTML
   }
    public function scholar_sync_groups_callback() {
        ob_start();
        $action   = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin routing
        $group_id = isset( $_GET['group-id'] ) ? (int) $_GET['group-id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin routing

        if ( $action === 'new-group' || $group_id > 0 ) {
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/group_edit_view.php';
        } else {
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/groups_list_view.php';
        }
        $content = ob_get_contents();
        ob_end_clean();
        echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted plugin-generated admin HTML
    }

    public function scholar_sync_display_settings_callback() {

        ob_start();
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/settings_view.php';
        $content = ob_get_contents();
        ob_end_clean();
        echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted plugin-generated admin HTML
    }

    public function scholar_sync_co_authors_callback() {
        if ( ! current_user_can( 'edit_others_posts' ) ) {
            wp_die( esc_html__( 'You do not have permission to access this page.', 'scholar-sync' ) );
        }
        ob_start();
        $action       = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing between views; edit branch verifies a nonce below
        $co_author_id = isset( $_GET['co-author-id'] ) ? (int) $_GET['co-author-id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- edit branch verifies a nonce before use

        if ( $action === 'edit' && $co_author_id > 0 ) {
            check_admin_referer( 'scholar_sync_edit_co_author' );
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/co_author_edit_view.php';
        } elseif ( $action === 'create' ) {
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/co_author_edit_view.php';
        } else {
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/co_authors_list_view.php';
        }
        $content = ob_get_contents();
        ob_end_clean();
        echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted plugin-generated admin HTML
    }

}
