<?php
/**
 * This file handles the admin area and functions.
 *
 * @author LeapIn Corp.
 * @since 1.0.0
 */

/************* ADMIN NOTICE *****************/
function leapin_get_admin_notices(){
    return [
        'install-bundle' => [
            'level' => 'warning',
            'disp-in-pro' => false,
            'message' => sprintf(__('Please install plugin <a href=%s target="_blank">Pjax Blog Bundle</a> to speed up your site and unlock custom options:)', 'pjax-blog'), esc_url('https://leap-in.com/plugin-pjax-blog-bundle/'), esc_url('https://leap-in.com/documents/'))
        ],
        'learn-more' => [
            'level' => 'info',
            'disp-in-pro' => true,
            'message' => sprintf(__('Learn Pjax Blog More. <a href=%s target="_blank">Website</a> | <a href=%s target="_blank">Documents</a>.', 'pjax-blog'), esc_url('https://leap-in.com/'), esc_url('https://leap-in.com/documents/'))
        ],
        'upgrade' => [
            'level' => 'info',
            'disp-in-pro' => false,
            'message' => sprintf(__('Upgrade to <a class="button button-primary" href=%s target="_blank">Pjax Blog Pro</a> to unlock <a href=%s target="_blank">More Features</a>!', 'pjax-blog'), esc_url('https://leap-in.com/'), esc_url('https://demo.leap-in.com/pjax-blog-1/category/features/'))
        ]
    ];
}

function leapin_add_document_url_to_admin_panel()
{
    global $pagenow;


    $messages = leapin_get_admin_notices();
    // Only show this message on the admin dashboard and if asked for
    if ('index.php' === $pagenow):
        foreach ($messages as $id => $message){
            leapin_the_dismissable_message($id, $message);
        }
    endif;
}
add_action('admin_notices', 'leapin_add_document_url_to_admin_panel');


/**
 * Register dismissal of admin notices.
 *
 * Acts on the dismiss link in the admin nag messages.
 * If clicked, the admin notice disappears and will no longer be visible to this user until next login.
 *
 * @since 1.6.10
 */
function leapin_dismiss_notice()
{
    if (isset($_GET['leapin-dismiss']) && check_admin_referer('leapin-dismiss-' . get_current_user_id())) {
        update_user_meta(get_current_user_id(), 'leapin_dismissed_notice-' . esc_attr($_GET['leapin-dismiss']), 1);
    }
}

add_action('admin_head', 'leapin_dismiss_notice');
/**
 * Delete dismissable nag option when user login.
 *
 * This ensures that the user(s) is/are again reminded via nag of required
 * and/or recommended plugins if they re-activate the theme.
 *
 * @since 1.6.10
 */
function leapin_reset_dismiss_notice()
{
    $messages = leapin_get_admin_notices();
    foreach ($messages as $id => $val){
        delete_metadata('user', null, 'leapin_dismissed_notice-' . $id, null, true);
    }
}
add_action('wp_login', 'leapin_reset_dismiss_notice');

function leapin_the_dismissable_message($id, $message)
{
    $dismiss_endpoint = esc_url(wp_nonce_url(add_query_arg('leapin-dismiss', $id), 'leapin-dismiss-' . get_current_user_id()));
    $ms = $message['message'];
    $level = $message['level'];
    $disp_in_pro = $message['disp-in-pro'];
    if(!($disp_in_pro === false && leapin_is_pro())):
        if(!get_user_meta(get_current_user_id(), 'leapin_dismissed_notice-' . $id, 1)):
    ?>
    <div class="leapin notice notice-<?php echo $level; ?> is-dismissible" data-dismiss-url="<?php echo $dismiss_endpoint; ?>">
        <p><?php echo $ms; ?></p>
    </div>
    <?php endif;
    endif;
}

/**
 * Helper function for outputting an Alpha Color Picker field.
 *
 * @param  string $class The class attribute value.
 * @param  string $name The name attribute value.
 * @param  string $value The initial color value.
 * @param  string $palette The palette of colors to include. Supports 'true' for
 *                                default palette, 'false' for no palette, or a | separated
 *                                list of colors.
 * @param  string $default The default color value.
 * @param  string $show_opacity Whether to show the opacity number on the slider. Supports
 *                                'true' or 'false'.
 * @param  string $id The id attribute value.
 *
 */
function leapin_admin_output_alpha_color_picker_field($class, $name, $value, $palette = 'true', $default = '#222', $show_opacity = 'true', $id = '')
{

    printf(
        '<input id="%s" type="text" class="%s" name="%s" value="%s" data-palette="%s" data-default-color="%s" data-show-opacity="%s" />',
        esc_attr($id),
        esc_attr($class),
        esc_attr($name),
        esc_attr($value),
        esc_attr($palette),
        esc_attr($default),
        esc_attr($show_opacity)
    );
}

/**
 * @todo implement in latter version
 */
//function leapin_get_tax_color($tax_id_or_slug)
//{
//    global $leapin_vars;
//    $taxonomies = $leapin_vars['taxonomies'];
//    if (is_numeric($tax_id_or_slug)) {
//        $t_id = $tax_id_or_slug;
//
//    } else {
//        if (array_key_exists($tax_id_or_slug, $taxonomies)) {
//            $t_id = $taxonomies[$tax_id_or_slug]->ID;
//        }
//    }
//
//    // Get the custom fields based on the $presenter term ID
//    $custom_fields = get_option("taxonomy_term_$t_id");
//
//    // Return the value for the "presenter_id" custom field
//    $tax_color = $custom_fields['tax_color'];
//
//    return $tax_color ? $tax_color : false;
//}

//function leapin_all_tax_color()
//{
//    global $leapin_vars;
//    $taxonomies = $leapin_vars['taxonomies'];
//
//    $tax_terms = [];
//    foreach ($taxonomies as $tax => $tax_value) {
//        $terms = get_terms(['taxonomy' => $tax, 'hide_empty' => false,]);
//        foreach ($terms as $term) {
//            $t_id = $term->term_id;
//            // Get the custom fields based on the $presenter term ID
//            $custom_fields = get_option("taxonomy_term_$t_id");
//
//            // Return the value for the "presenter_id" custom field
//            $tax_color = $custom_fields['tax_color'];
//
//            $tax_color = $tax_color ? $tax_color : false;
//            if ($tax_color) {
//                $tax_terms[$term->term_id] = $tax_color;
//            }
//        }
//    }
//    return $tax_terms;
//}

?>
