<?php

add_action( 'init', 'wpfresh5_branding_init' );
function wpfresh5_branding_init() {
	if( is_admin() )
		return;
	$disable_fe = wpfresh5_get_settings( array( 'branding', 'adminbar', 'disable_fe' ) );
	if( $disable_fe )
		add_filter('show_admin_bar', '__return_false');
}

add_action( 'admin_head', 'wpfrsh5_print_admin_css', 11 );

function wpfrsh5_print_admin_css() {
	$items = wpfresh5_get_settings( array( 'branding' ) );
	$header_img = ( isset( $items['aheader']['header_img'] ) && !empty( $items['aheader']['header_img'] ) ) ? trim( $items['aheader']['header_img'] ) : '';
	$hide_screen = ( isset( $items['aheader']['hide_screen'] ) && !empty( $items['aheader']['hide_screen'] ) ) ? intval( $items['aheader']['hide_screen'] ) : 0;
	$hide_help = ( isset( $items['aheader']['hide_help'] ) && !empty( $items['aheader']['hide_help'] ) ) ? intval( $items['aheader']['hide_help'] ) : 0;
	$howdy_txt = ( isset( $items['aheader']['howdy_txt'] ) && !empty( $items['aheader']['howdy_txt'] ) ) ? trim( $items['aheader']['howdy_txt'] ) : '';

	$text1 = ( isset( $items['afooter']['text1'] ) && !empty( $items['afooter']['text1'] ) ) ? trim( $items['afooter']['text1'] ) : '';
	$text2 = ( isset( $items['afooter']['text2'] ) && !empty( $items['afooter']['text2'] ) ) ? trim( $items['afooter']['text2'] ) : '';


	$heading_txt = ( isset( $items['adashboard']['heading_txt'] ) && !empty( $items['adashboard']['heading_txt'] ) ) ? trim( $items['adashboard']['heading_txt'] ) : '';
	$custom_txt = ( isset( $items['adashboard']['custom_txt'] ) && !empty( $items['adashboard']['custom_txt'] ) ) ? trim( $items['adashboard']['custom_txt'] ) : '';

	$logo_url = ( isset( $items['adminbar']['logo_url'] ) && !empty( $items['adminbar']['logo_url'] ) ) ? trim( $items['adminbar']['logo_url'] ) : '';

?>
	<script>
   		jQuery(document).ready(function() {
			//jQuery("#footer-left").html('<?php echo addslashes(get_option('agca_footer_left')); ?>');
			// Admin Header
			<?php if( !empty( $header_img ) ) { ?>
				var img_url = '<?php echo addslashes($header_img); ?>';
				advanced_url = img_url;
				image = jQuery("<img style='max-width:98%;position:relative;'/>").attr("src",advanced_url);
				jQuery(image).load(function() {
					jQuery("#wpbody-content").prepend(image);
				});
			<?php } ?>
			<?php if( $hide_screen ) { ?>
				jQuery( '#screen-options-link-wrap').hide();
			<?php } ?>
			<?php if( $hide_help ) { ?>
				jQuery( '#contextual-help-link-wrap').hide();
			<?php } ?>

			<?php if( $howdy_txt ) { ?>
				var alltext="";
				alltext="";
				jQuery('li#wp-admin-bar-my-account').css('cursor','default');
				alltext = jQuery('li#wp-admin-bar-my-account').html();
				if(alltext!=null){
					var parts = alltext.split(',');
					alltext = "<?php echo $howdy_txt; ?>" + ", " + parts[1];
				}
				jQuery("li#wp-admin-bar-my-account").html("<a href=\"#\" class=\"ab-item\">"+alltext+"</a>");
			<?php } ?>

			// Admin Dashboard
			<?php if( !empty( $heading_txt ) ) { ?>
				jQuery("#dashboard-widgets-wrap").parent().find("h1").html("<?php echo addslashes( $heading_txt ); ?>");
			<?php } ?>

			<?php if( !empty( $custom_txt ) ) { ?>
				jQuery("#wpbody-content #dashboard-widgets-wrap").before('<div id="wpfresh5_custom_dashboard_content"></div>');
				jQuery("#wpfresh5_custom_dashboard_content").html('<br /><?php echo preg_replace('/(\r\n|\r|\n)/', '\n', addslashes(wpautop( $custom_txt ))); ?>');
			<?php } ?>

			// Admin Footer
			<?php if( !empty( $text1 ) ) { ?>
				jQuery("#footer-left").html( '<?php echo addslashes( $text1 ) ?>' );
			<?php } ?>
			<?php if( !empty( $text2 ) ) { ?>
				jQuery("#footer-upgrade").html( '<?php echo addslashes( $text2 ) ?>' );
			<?php } ?>

			// Admin Bar
			<?php if( !empty( $logo_url ) ) { ?>
				jQuery("li#wp-admin-bar-wp-logo > a.ab-item").attr('href',"<?php echo $logo_url; ?>")
			<?php } ?>
		});
	</script>
<?php
}

// 3. Admin Menus

function wpfresh5_default_menus( $sub = false, $menu  ) {
	// var_dump($sub);
	// return ;
	$arr_menus = array(
		'index.php' => __( 'Dashboard' ),
		'edit.php' => __( 'Posts' ),
		'upload.php' => __( 'Media' ),
		'edit.php?post_type=page' => __( 'Pages' ),
		'edit-comments.php' => __( 'Comments' ),
		'themes.php' => __( 'Appearance' ),
		'plugins.php' => __( 'Plugins' ),
		'users.php' => __( 'Users' ),
		'tools.php' => __( 'Tools' ),
		'settings.php' => __( 'Settings' ),
	);

	if( $sub ) {
		if( isset ( $arr_menus[ $sub ] ) )
			return $arr_menus[ $sub ];
		return $menu[0];
	}
	// die();
	return $arr_menus;
}


function wpfresh5_change_post_menu_label() {
    global $menu;
	$items = wpfresh5_get_settings( array( 'branding', 'amenu', 'menu_els' ) );
	if( !$items )
		return;
	// var_dump($menu);
	// var_dump($items);
	foreach( $menu as $key => $menu_item ) {
		$slug = $menu_item[2];
		if( isset( $items[$slug]['name'] ) && !empty( $items[$slug]['name'] ) )
			$menu[$key][0] = $items[$slug]['name'];
		if( isset( $items[$slug]['disabled'] ) && ( $items[$slug]['disabled'] ) && (!current_user_can('manage_options') ) )
			unset( $menu[$key] );

	}
    // global $submenu;
    // $menu[5][0] = 'News';
    // $submenu['edit.php'][5][0] = 'News Items';
    // $submenu['edit.php'][10][0] = 'Add News Item';
}
add_action( 'admin_menu', 'wpfresh5_change_post_menu_label', 100 );

// 4. Admin Dashboard 
function wpfresh5_get_admin_widgets() {
	return array(
		'welcome' => array(
		  'name' => 'Welcome Panel',
		  'key' => 'dashboard_welcome_panel',
		  'area' => 'side'
		  ),
	'activity' => array(
	  'name' => 'Activity',
	  'key' => 'dashboard_activity',
	  'area' => 'side'
	  ),
	  'quick_press' => array(
	    'name' => 'Quick Draft',
	    'key' => 'dashboard_quick_press',
	    'area' => 'side'
	    ),
	  // 'incoming_links' => array(
	  //   'name' => 'Incoming Links',
	  //   'key' => 'dashboard_incoming_links',
	  //   'area' => 'normal'
	  //   ),
	  // 'other_wordpress_news' => array(
	  //   'name' => 'Other WordPress News',
	  //   'key' => 'dashboard_secondary',
	  //   'area' => 'side'
	  //   ),
	  // 'plugins' => array(
	  //   'name' => 'Plugins',
	  //   'key' => 'dashboard_plugins',
	  //   'area' => 'normal'
	  //   ),
	  'recent_comments' => array(
	    'name' => 'Recent Comments',
	    'key' => 'dashboard_recent_comments',
	    'area' => 'normal'
	    ),
	  'wordpress_blog' => array(
	    'name' => 'WordPress Events and News',
	    'key' => 'dashboard_primary',
	    'area' => 'side'
	    ),
	  'recent_drafts' => array(
	    'name' => 'Recent Drafts',
	    'key' => 'dashboard_recent_drafts',
	    'area' => 'normal'
	    ),
	  'right_now' => array(
	    'name' => 'At a Glance',
	    'key' => 'dashboard_right_now',
	    'area' => 'normal'
	    )
	);
}
function wpfresh5_print_admin_widgets( $sel_widgets ) {
	// Widget Details
	$widgets = wpfresh5_get_admin_widgets();
// var_dump($widgets);
	$str = '<table class="widefat" style="width:auto">'.
	'<tr><th></th><th>Disable Widget</th></tr>';
	foreach( $widgets as $key => $widget ) {
	    // var_dump($item);
	    $slug = $widget['key'];
        $is_checked = isset( $sel_widgets[$slug]['disabled'] ) && $sel_widgets[$slug]['disabled'];
        $str .= '<tr><td><b>'.$widget['name'].'</b></td><td style="text-align:center"><input type="checkbox" '.checked( $is_checked, 1, false ).' name="wpfresh5_dw_el['.$slug.'][disabled]" /></td></tr>';
	    }
	$str .= '</table><br/><br/>';
	echo $str;
}

function wpfresh5_remove_dashboard_meta() {
	$disabled_widgets = wpfresh5_get_settings( array( 'branding', 'adashboard', 'widgets' ) );
	if( empty( $disabled_widgets ) )
		return;
	$disabled_widgets = array_keys( $disabled_widgets );
	$all_widgets = wpfresh5_get_admin_widgets();
	foreach( $all_widgets as $widget) {
		if( in_array( $widget['key'], $disabled_widgets ) )
			if ( 'dashboard_welcome_panel' === $widget['key'] )
				remove_action( 'welcome_panel', 'wp_welcome_panel' );
			else
        		remove_meta_box( $widget['key'], 'dashboard', $widget['area'] );

	}
}
add_action( 'admin_init', 'wpfresh5_remove_dashboard_meta' );

// 5. Admin Bar

function wpfreshstart_custom_logo() {
	$logo = wpfresh5_get_settings( array( 'branding', 'adminbar', 'logo' ) );
	$hide_subitems = wpfresh5_get_settings( array( 'branding', 'adminbar', 'hide_subitems' ) );
	if( $logo ) {
		echo '
		<style type="text/css">
			#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
			background-image: url(' . $logo .') !important;
			background-position: 0 0;
			color:rgba(0, 0, 0, 0);
			width: 32px;
			height: 32px;
			background-repeat: no-repeat;
			background-size: cover;
		}
		#wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
		   background-position: 0 0;
		}
		</style>
		';
	}

	if( $hide_subitems ) {
		echo '
		<style type="text/css">
		#wpadminbar #wp-admin-bar-wp-logo > .ab-sub-wrapper {
			display: none!important;
		}
		</style>
		';
	}
}
//hook into the administrative header output
add_action('wp_before_admin_bar_render', 'wpfreshstart_custom_logo', 50 );

// 6. Login Page

function wpfresh5_login_logo_url() {
    // $wpfresh5_options = get_option('wpfresh5_options');
	$wpfresh5_options = wpfresh5_get_settings( array( 'branding', 'login' ) );

    if($wpfresh5_options && $wpfresh5_options['custom_enabled']){
        $wpfresh5_login_logo_url = get_bloginfo('url');
        if( isset( $wpfresh5_options['logo_url'] ) && $wpfresh5_options['logo_url'] ){
            $wpfresh5_login_logo_url = $wpfresh5_options['logo_url'];
        }
        return $wpfresh5_login_logo_url;
    }
}
add_filter('login_headerurl', 'wpfresh5_login_logo_url');

/* CUSTOMIZE LOGO TITLE */
function wpfresh5_login_logo_title() {
	$wpfresh5_options = wpfresh5_get_settings( array( 'branding', 'login' ) );

    if($wpfresh5_options && $wpfresh5_options['custom_enabled']){
        $wpfresh5_login_logo_title = get_bloginfo('description');
        if( isset( $wpfresh5_options['logo_title'] ) && $wpfresh5_options['logo_title'] ){
            $wpfresh5_login_logo_title = $wpfresh5_options['logo_title'];
        }
        return $wpfresh5_login_logo_title;
    }
}
add_filter('login_headertitle', 'wpfresh5_login_logo_title');

/* CUSTOMIZE LOGIN FORM */
function wpfresh5_custom_login() {
	$wpfresh5_options = wpfresh5_get_settings( array( 'branding', 'login' ) );

    if( $wpfresh5_options && $wpfresh5_options['custom_enabled'] ){
        ?>
        <style type="text/css">
            /* bg body */
            <?php
                $wpfresh5_attachment_url = false;
                $field_name = 'bg_img';
                /*if( is_array($wpfresh5_options) && $wpfresh5_options[$field_name] != "" ):
                    $wpfresh5_attachment_id = $wpfresh5_options[$field_name];
                    $attachmentArr = wp_get_attachment_image_src($wpfresh5_attachment_id, 'full');
                    $wpfresh5_attachment_url = $attachmentArr[0];
                endif;*/
            ?>
            html,body.login{
                background-color:<?php echo $wpfresh5_options['body_color']; ?>;
                <?php if( isset( $wpfresh5_options['body_img']) && !empty( $wpfresh5_options['body_img'] ) ) { ?>
                background-image:url('<?php echo $wpfresh5_options['body_img'] ?>');
                background-position: center center !important;
                background-size: cover !important;
                <?php } ?>
            }

            /* logo */
            h1 a {
                <?php if( isset( $wpfresh5_options['logo_img']) && !empty( $wpfresh5_options['logo_img'] ) ) : ?>
                background-image:url('<?php echo $wpfresh5_options['logo_img']; ?>') !important;
                background-position: center center !important;
                background-size: contain !important;
                <?php endif; ?>
                height: 85px !importnat;
                width: 280px !important;
                margin: auto !important;
                padding: 0 !important;
            }

            /* form */
            .login form{
                <?php if( isset( $wpfresh5_options['form_img']) && !empty( $wpfresh5_options['form_img'] ) ) : ?>
                background-image:url('<?php echo $wpfresh5_options['form_img']; ?>');
                background-position: center center !important;
                background-size: cover !important;
                <?php endif; ?>
                <?php if( isset( $wpfresh5_options['form_color']) && !empty( $wpfresh5_options['form_color'] ) ) { ?>
                background-color: <?php echo $wpfresh5_options['form_color']; ?>;
                <?php } ?>
                /*box-shadow: 0 1px 4px -1px <?php echo $wpfresh5_options['form_shadow_color']; ?>;
                -moz-box-shadow: 0 1px 4px -1px <?php echo $wpfresh5_options['form_shadow_color']; ?>;
                -webkit-box-shadow: 0 1px 4px -1px <?php echo $wpfresh5_options['form_shadow_color']; ?>;*/
            }
            <?php if( isset( $wpfresh5_options['form_text_color']) && !empty( $wpfresh5_options['form_text_color'] ) ) { ?>
            .login label{
                color:<?php echo $wpfresh5_options['form_text_color']; ?>;
            }
            <?php } ?>
            /*.login form .input, .login form input[type=checkbox], .login input[type=text]{
                background-color: <?php echo $wpfresh5_options['form_input_color']; ?>;
            }*/
            .login .button-primary{
                border-color: <?php echo $wpfresh5_options['button_color']; ?>;
                background-color: <?php echo $wpfresh5_options['button_color']; ?>;
                color: <?php echo $wpfresh5_options['button_text_color']; ?>;
                text-shadow: none;
                /*-webkit-box-shadow: 0 1px 0 <?php echo $wpfresh5_options['form_btn_s_color']; ?>;
                box-shadow: 0 1px 0 <?php echo $wpfresh5_options['form_btn_s_color']; ?>;
                -webkit-box-shadow: 0 1px 0 <?php echo $wpfresh5_options['form_btn_s_color']; ?>;
                box-shadow: 0 1px 0 <?php echo $wpfresh5_options['form_btn_s_color']; ?>;*/

            }
            /*.login .button-primary:hover{
                border-color: <?php echo $wpfresh5_options['form_btn_border_h_color']; ?>;
                background-color: <?php echo $wpfresh5_options['form_btn_bg_h_color']; ?>;
                color: <?php echo $wpfresh5_options['form_btn_h_color']; ?>;
                -webkit-box-shadow: 0 1px 0 <?php echo $wpfresh5_options['form_btn_s_h_color']; ?>;
                -moz-box-shadow: 0 1px 0 <?php echo $wpfresh5_options['form_btn_s_h_color']; ?>;
                box-shadow: 0 1px 0 <?php echo $wpfresh5_options['form_btn_s_h_color']; ?>;
                text-shadow: 0 -1px 1px <?php echo $wpfresh5_options['form_btn_t_s_h_color']; ?>,1px 0 1px <?php echo $wpfresh5_options['form_btn_t_s_h_color']; ?>,0 1px 1px <?php echo $wpfresh5_options['form_btn_t_s_h_color']; ?>,-1px 0 1px <?php echo $wpfresh5_options['form_btn_t_s_h_color']; ?>;
            }*/
            .login #backtoblog a, .login #nav a, .login h1 a {
                color: <?php echo $wpfresh5_options['links_color']; ?>;
            }
            .login #backtoblog a:hover, .login #nav a:hover, .login h1 a:hover {
                color: <?php echo $wpfresh5_options['links_hover_color']; ?>;
            }
			/*.login .message {
			    border-left-color: <?php echo $wpfresh5_options['links_color']; ?>;
			}
			.login #login_error{
			    border-left-color: <?php echo $wpfresh5_options['links_hover_color']; ?>;
			}*/
        </style>
        <?php
    }
}

add_action( 'login_head', 'wpfresh5_custom_login');
?>