<?php
/**
 * Custom functions for gralbcn theme
 *
 * @package gralbcn
 */

/**
 * Fix the ridiculous hard coded extra 10px width on captions.
 *
 * @param string $attrs does a thing.
 */
function fix_extra_caption_padding( $attrs ) {
	if ( ! empty( $attrs['width'] ) ) {
		$attrs['width'] -= 10;
	}
	return $attrs;
}
add_filter( 'shortcode_atts_caption', 'fix_extra_caption_padding' );

/**
 * Fix the ridiculous hard coded width on figure tag.
 */
add_filter( 'img_caption_shortcode_width', '__return_zero' );


/**
 * Theme support elements.
 */
function gralbcn_theme_setup() {
	/**
	Add logo to customiser with ability to choose whether name and description shown.
	 */
	$defaults = array(
		'height'      => 100,
		'width'       => 400,
		'flex-height' => true,
		'flex-width'  => true,
		'header-text' => array( 'site-title', 'site-description' ),
	);
	add_theme_support( 'custom-logo', $defaults );
	/**
	Newer way of adding meta title.
	 */
	add_theme_support( 'title-tag' );
	/**
	Support Featured Images.
	 */
	add_theme_support( 'post-thumbnails' );
	/**
	 * Theme translation setup.
	 */
	load_theme_textdomain( 'gralbcn', get_template_directory() . '/languages' );
	/**
	 * Add links to RSS feeds.
	 */
	add_theme_support( 'automatic-feed-links' );
}
add_action( 'after_setup_theme', 'gralbcn_theme_setup' );



/**
 * Add customiser live update for blog name and description, just shows nothing without this.
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function gralbcn_wp_customize_register( $wp_customize ) {
	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial(
			'blogname',
			array(
				'selector'        => '.site-title a',
				'render_callback' => 'gralbcn_wp_customize_partial_blogname',
			)
		);
		$wp_customize->selective_refresh->add_partial(
			'blogdescription',
			array(
				'selector'        => '.site-description',
				'render_callback' => 'gralbcn_wp_customize_partial_blogdescription',
			)
		);
	}
}
add_action( 'customize_register', 'gralbcn_wp_customize_register' );

/**
 * Render the site title for the selective refresh partial.
 *
 * @return void
 */
function gralbcn_wp_customize_partial_blogname() {
	bloginfo( 'name' );
}

/**
 * Render the site tagline for the selective refresh partial.
 *
 * @return void
 */
function gralbcn_wp_customize_partial_blogdescription() {
	bloginfo( 'description' );
}


function mytheme_customize_register( $wp_customize ) {

  // Sección Social
  $wp_customize->add_section('social_links_section', array(
    'title' => __('Social', 'textdomain'),
    'priority' => 30,
  ));

  // Facebook
  $wp_customize->add_setting('social_facebook', array(
    'default' => '',
    'sanitize_callback' => 'esc_url_raw',
  ));

  $wp_customize->add_control('social_facebook', array(
    'label' => __('Facebook URL', 'textdomain'),
    'section' => 'social_links_section',
    'type' => 'url',
  ));

  // X (Twitter)
  $wp_customize->add_setting('social_x', array(
    'default' => '',
    'sanitize_callback' => 'esc_url_raw',
  ));

  $wp_customize->add_control('social_x', array(
    'label' => __('X (Twitter) URL', 'textdomain'),
    'section' => 'social_links_section',
    'type' => 'url',
  ));

  // Email
  $wp_customize->add_setting('social_email', array(
    'default' => '',
    'sanitize_callback' => 'sanitize_email',
  ));

  $wp_customize->add_control('social_email', array(
    'label' => __('Email', 'textdomain'),
    'section' => 'social_links_section',
    'type' => 'email',
  ));

  // ResearchGate
  $wp_customize->add_setting('social_researchgate', array(
    'default' => '',
    'sanitize_callback' => 'esc_url_raw',
  ));

  $wp_customize->add_control('social_researchgate', array(
    'label' => __('ResearchGate URL', 'textdomain'),
    'section' => 'social_links_section',
    'type' => 'url',
  ));
}

add_action('customize_register', 'mytheme_customize_register');


/**
 * Customiser theme colours.
 * https://stackoverflow.com/questions/36786520/how-to-add-color-option-in-wordpress-customize.
 *
 * @param WP_Customize_Manager $wp_customize Object that holds the customizer data.
 */
function gralbcn_customize_register( $wp_customize ) {
	/**
	 * All our sections, settings, and controls will be added here.
	 */
	$wp_customize->add_setting(
		'body_background',
		array(
			'default'           => '#ffffff',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_hex_color', // validates 3 or 6 digit HTML hex color code.
		)
	);
	$wp_customize->add_setting(
		'body_textcolour',
		array(
			'default'           => '#000000',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_hex_color', // validates 3 or 6 digit HTML hex color code.
		)
	);
	$wp_customize->add_setting(
		'heading_textcolour',
		array(
			'default'           => '#000000',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_hex_color', // validates 3 or 6 digit HTML hex color code.
		)
	);
	$wp_customize->add_setting(
		'link_colour',
		array(
			'default'           => '#0b36a5',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_hex_color', // validates 3 or 6 digit HTML hex color code.
		)
	);
	$wp_customize->add_setting(
		'link_hover_colour',
		array(
			'default'           => '#0b36a5',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_hex_color', // validates 3 or 6 digit HTML hex color code.
		)
	);
	$wp_customize->add_setting(
		'accent_colour',
		array(
			'default'           => '#eaeaea',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_hex_color', // validates 3 or 6 digit HTML hex color code.
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'body_background',
			array(
				'label'   => __( 'Background', 'gralbcn' ),
				'section' => 'colors',
			)
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'body_textcolour',
			array(
				'label'   => __( 'Text Colour', 'gralbcn' ),
				'section' => 'colors',
			)
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'heading_textcolour',
			array(
				'label'   => __( 'Heading Colour', 'gralbcn' ),
				'section' => 'colors',
			)
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'link_colour',
			array(
				'label'   => __( 'Link Colour', 'gralbcn' ),
				'section' => 'colors',
			)
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'link_hover_colour',
			array(
				'label'   => __( 'Link Hover Colour', 'gralbcn' ),
				'section' => 'colors',
			)
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'accent_colour',
			array(
				'label'   => __( 'Accent Colour', 'gralbcn' ),
				'section' => 'colors',
			)
		)
	);
}
add_action( 'customize_register', 'gralbcn_customize_register' );

/**
 * Add customizer styles to header of front end.
 */
function gralbcn_customise_css() {
	?>
	<!-- User editable styles from customiser -->
	<style type="text/css">
		body { background-color: <?php echo esc_html( get_theme_mod( 'body_background', '#fcfcfc' ) ); ?>;	}
		body, #headerLogo a { color: <?php echo esc_html( get_theme_mod( 'body_textcolour', '#042825' ) ); ?>; }
		h1,h2,h3,h4,h5,h6 { color: <?php echo esc_html( get_theme_mod( 'heading_textcolour', '#042825' ) ); ?>; }
		a, a:visited { color: <?php echo esc_html( get_theme_mod( 'link_colour', '#00804D' ) ); ?>; } 
		#headerMenuMobile button span, #headerMenuMobile button:before,	#headerMenuMobile button:after { background-color: <?php echo esc_html( get_theme_mod( 'link_colour', '#00804D' ) ); ?>; } 
		button, .btn, .btn:visited, input[type="button"], input[type="submit"] { background-color: <?php echo esc_html( get_theme_mod( 'link_colour', '#00804D' ) ); ?>; }
		a:hover { color: <?php echo esc_html( get_theme_mod( 'link_hover_colour', '#00D581' ) ); ?>; }
		.button:hover, .btn:hover, input[type="submit"]:hover, input[type="reset"]:hover, input[type="button"]:hover { background-color: <?php echo esc_html( get_theme_mod( 'link_hover_colour', '#00D581' ) ); ?>; }
		#footer { background-color: <?php echo esc_html( get_theme_mod( 'body_textcolour', '#042825' ) ); ?>; }
		blockquote { border-left-color: <?php echo esc_html( get_theme_mod( 'body_textcolour', '#042825' ) ); ?>; } 
		pre, code, blockquote, fieldset, .gridRow .column, .gridRow .columns { background: <?php echo esc_html( get_theme_mod( 'accent_colour', '#DFE5E2' ) ); ?>; }
	</style>
	<?php
}
add_action( 'wp_head', 'gralbcn_customise_css' );


/**
 * Remove unwanted CSS files from plugins etc - find the handle in the plugin file and add here.
 */
function gralbcn_remove_unwanted_css() {
	wp_dequeue_style( 'unwanted-css-file' );
}
add_action( 'wp_enqueue_scripts', 'gralbcn_remove_unwanted_css', 100 );


/**
 * Add theme's CSS file.
 */
function gralbcn_css() {
	wp_enqueue_style( 'gralbcn-style', get_stylesheet_uri(), array(), '1.0' );
}
add_action( 'wp_enqueue_scripts', 'gralbcn_css', 200 );


/**
Custom read more function.
 */
function custom_read_more() {
	return '...';
}

/**
 * Custom excerpt - use echo excerpt(25); to output in theme.
 *
 * @param integer $limit Length of excerpt passed from get_the template tag.
 */
function excerpt( $limit ) {
	return wp_trim_words( get_the_excerpt(), $limit, custom_read_more() );
}

/**
 * Remove link from author name in comments section.
 */
function gralbcn_remove_author_url() {
	return '';
}
add_filter( 'get_comment_author_url', 'gralbcn_remove_author_url', 10, 3 );


/**
 * Admin login page CSS
 */
function gralbcn_login_stylesheet() {
	wp_enqueue_style( 'custom-login', get_template_directory_uri() . '/style-login.css', 1.0, true );
}
add_action( 'login_enqueue_scripts', 'gralbcn_login_stylesheet' );


/**
 * Custom admin CSS to remove plugin ads etc.
 */

 
function gralbcn_custom_admin_css() {
  $css_path = get_template_directory() . '/style-admin.css';
  $css_url = get_template_directory_uri() . '/style-admin.css';
  $version = file_exists($css_path) ? filemtime($css_path) : time();

  wp_enqueue_style( 'custom_admin_css', $css_url, array(), $version, false );
}
add_action( 'admin_enqueue_scripts', 'gralbcn_custom_admin_css' );



/**
 * Add button class to blog pagination links.
 */
function gralbcn_posts_link_attributes() {
	return 'class="btn"';
}
add_filter( 'next_posts_link_attributes', 'gralbcn_posts_link_attributes' );
add_filter( 'previous_posts_link_attributes', 'gralbcn_posts_link_attributes' );



/**
 * Remove emoji junk.
 */
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );



/**
 * Remove jQuery migrate.
 *
 * @param array $scripts List of scripts loaded by WP.
 */
function gralbcn_remove_jquery_migrate( &$scripts ) {
	if ( ! is_admin() ) {
		$scripts->remove( 'jquery' );
		$scripts->add( 'jquery', false, array( 'jquery-core' ), '1.10.2' );
	}
}
add_filter( 'wp_default_scripts', 'gralbcn_remove_jquery_migrate' );



/**
 * Move Yoast to bottom.
 */
function gralbcn_yoasttobottom() {
	return 'low';
}
add_filter( 'wpseo_metabox_prio', 'gralbcn_yoasttobottom' );


/**
 * Register Main Menu.
 */
function gralbcn_register_my_menu() {
	register_nav_menu( 'main-menu', __( 'Main Menu', 'gralbcn' ) );
	register_nav_menu( 'footer-menu', __( 'Footer Menu', 'gralbcn' ) );
}
add_action( 'init', 'gralbcn_register_my_menu' );



/**
 * Register sidebars.
 */
function gralbcn_sidebar_widgets_init() {
	register_sidebar(
		array(
			'name'          => __( 'Blog Sidebar', 'gralbcn' ),
			'id'            => 'blog-sidebar',
			'before_widget' => '<li id="%1$s" class="widget %2$s">',
			'after_widget'  => '</li>',
			'before_title'  => '<h3>',
			'after_title'   => '</h3>',
		),
	);
	register_sidebar(
		array(
			'name'          => __( 'Page Sidebar', 'gralbcn' ),
			'id'            => 'page-sidebar',
			'before_widget' => '<li id="%1$s" class="widget %2$s">',
			'after_widget'  => '</li>',
			'before_title'  => '<h3>',
			'after_title'   => '</h3>',
		)
	);
}
add_action( 'widgets_init', 'gralbcn_sidebar_widgets_init' );



/**
 * Load Scripts Properly.
 */
function gralbcn_scripts_method() {
	// Add a script.
	// wp_register_script('gralbcn-script', get_template_directory_uri() . '/js/jquery.gralbcn.min.js', array('jquery'));
	// wp_enqueue_script('gralbcn-script', true);
}
add_action( 'wp_enqueue_scripts', 'gralbcn_scripts_method' );


/**
 * Callback function to insert styleselect into the buttons array.
 *
 * @param array $buttons List of buttons loaded by WP editor.
 */
function gralbcn_mce_buttons_2( $buttons ) {
	array_unshift( $buttons, 'styleselect' );
	return $buttons;
}
add_filter( 'mce_buttons_2', 'gralbcn_mce_buttons_2' );

/**
 * Set up additional styles for editor button
 *
 * @param array $settings List of settings loaded by WP editor.
 */
function gralbcn_mce_before_init( $settings ) {
	$style_formats                           = array(
		array(
			'title'    => __( 'Positive', 'gralbcn' ),
			'selector' => 'p',
			'classes'  => 'positive',
		),
		array(
			'title'    => __( 'Error', 'gralbcn' ),
			'selector' => 'p',
			'classes'  => 'negative',
		),
		array(
			'title'    => __( 'Warning', 'gralbcn' ),
			'selector' => 'p',
			'classes'  => 'warning',
		),
		array(
			'title'    => __( 'Smaller text', 'gralbcn' ),
			'selector' => 'p',
			'classes'  => 'has-small-font-size',
		),
		array(
			'title'    => __( 'Larger text', 'gralbcn' ),
			'selector' => 'p',
			'classes'  => 'has-large-font-size',
		),
		array(
			'title'    => __( 'Button', 'gralbcn' ),
			'selector' => 'a',
			'classes'  => 'btn',
		),
	);
	$settings['style_formats']               = wp_json_encode( $style_formats );
	$settings['theme_advanced_blockformats'] = 'p,h3,h4';
	$settings['theme_advanced_disable']      = 'forecolor';

	return $settings;
}
add_filter( 'tiny_mce_before_init', 'gralbcn_mce_before_init' );

/**
 * Add editor fonts/styles.
 */



 /*
function gralbcn_add_editor_styles() {
	add_editor_style( 'style-editor.css' );
}
add_action( 'init', 'gralbcn_add_editor_styles' );
*/




function rename_uncategorized_category() {
  $category = get_category_by_slug('uncategorized');
  if ($category) {
    wp_update_term($category->term_id, 'category', array(
      'name' => 'Presentations',
      'slug' => 'presentations',
    ));
  }
}
add_action('init', 'rename_uncategorized_category');


function gralbcn_custom_admin_css_by_user() {
  if ( ! is_user_logged_in() ) return;

  $current_user = wp_get_current_user();
  ?>
  <style>
    #dashboard-widgets-wrap {
      display: none !important;
    }
    <?php if ( $current_user->user_login === 'info_s6s8yknd' ) : ?>
    #dashboard-widgets-wrap {
      display: block !important;
    }
    <?php endif; ?>
  </style>
  <?php
}
add_action( 'admin_head', 'gralbcn_custom_admin_css_by_user' );




function gralbcn_customize_page_titles( $title ) {
  $site_name = get_bloginfo('name');
  $site_desc = get_bloginfo('description');

  if ( is_front_page() || is_home() ) {
    // Home: "site-title site-description"
    $title['title'] = $site_name . ' ' . $site_desc;
    unset($title['tagline']); // elimina tagline si existe
    unset($title['site']);    // elimina site-title extra
  } else {
    // Resto de páginas: "site-title site-description - nombre de página"
    if ( isset($title['title']) ) {
      $page_title = $title['title'];
    } else {
      $page_title = '';
    }

    $title['title'] = $site_name . ' ' . $site_desc . ( $page_title ? ' - ' . $page_title : '' );

    unset($title['tagline']); // elimina tagline si existe
    unset($title['site']);    // elimina site-title extra
  }

  return $title;
}
add_filter( 'document_title_parts', 'gralbcn_customize_page_titles' );