<?php

/**
 * All the while label hooks related design handle from this file
 */

if (!defined('ABSPATH')) {
    exit;
}

// Remove WP logo if option is enabled
function sapmfly_custom_admin_bar_render()
{
    if (get_option('sapmfly_hide_wp_logo', 'no') === 'yes') {
        global $wp_admin_bar;
        $wp_admin_bar->remove_node('wp-logo');
    }
}

add_action('wp_before_admin_bar_render', 'sapmfly_custom_admin_bar_render');
// Hide help option from admin panel top right area
function sapmfly_hide_top_right_help_tab()
{
    $hide_help = get_option('sapmfly_hide_wp_help', 'no');
    if ($hide_help === 'yes') {
        echo '<style>
            #contextual-help-link-wrap {
                display: none !important;
            }
        </style>';
    }
}

add_action('admin_head', 'sapmfly_hide_top_right_help_tab');
//Hide WordPress version number related hooks
add_filter('update_footer', 'sapmfly_hide_wp_version_settings', 11);
function sapmfly_hide_wp_version_settings($version)
{
    $hide_version = get_option('sapmfly_hide_wp_version', 'no');
    // Return empty string if hiding is enabled
    if ($hide_version === 'yes') {
        return '';
    }
    return $version;
}

// Hide WordPress screen options related hook
function sapmfly_hide_screen_options_tab()
{
    $hide_screen = get_option('sapmfly_screen_options');
    if ($hide_screen === 'yes') {
        echo '<style>
            #screen-options-link-wrap {
                display: none !important;
            }
        </style>';
    }
}

add_action('admin_head', 'sapmfly_hide_screen_options_tab');

// Footer text credit related code
// This is for free version
add_filter('admin_footer_text', function ($text) {

    $hide = get_option('sapmfly_hide_footer_credit'); // yes/no
    $custom = get_option('sapmfly_custom_footer_credit'); // saved custom text

    // 1. If toggle is ON AND no custom text → hide
    if ($hide === 'yes' && empty($custom)) {
        return '';
    }

    // 2. If toggle is ON AND custom text exists → show custom text
    if ($hide === 'yes' && !empty($custom)) {
        return $custom;
    }

    // 3. Else default WP footer
    return $text;
});


// Footer text credit related code
// This is for pro version
add_action('wp_ajax_sapmfly_save_fc_customization', 'sapmfly_save_fc_customization');
function sapmfly_save_fc_customization()
{

    // Verify nonce
    if (
        ! isset($_POST['nonce']) ||
        ! wp_verify_nonce(
            sanitize_text_field(wp_unslash($_POST['nonce'])),
            'sapmfly_fc_nonce'
        )
    ) {
        wp_send_json_error(['message' => 'Security check failed!']);
    }

    // Get credit text
    $credit = isset($_POST['credit'])
        ? sanitize_textarea_field(wp_unslash($_POST['credit']))
        : '';

    update_option('sapmfly_custom_footer_credit', $credit);

    wp_send_json_success(['message' => 'Footer credit saved successfully.']);
}
