functions.php 9.66 KB
<?php

function accesibility_theme_support()
{
    add_theme_support('title-tag');
    add_theme_support('custom-logo');
    add_theme_support('post-thumbnails');
}

add_action('after_setup_theme', 'accesibility_theme_support');

function accesibility_menus()
{
    $locations = array(
        'main' => 'Menú Principal',
    );

    register_nav_menus($locations);
}

add_action('init', 'accesibility_menus');

function accesibility_register_styles()
{
    $version = wp_get_theme()->get('Version');
    wp_enqueue_style('accesibility-boostrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css', array(), '4.0.0', 'all');
    wp_enqueue_style('accesibility-sidebar-style', 'https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.css', array('accesibility-boostrap'), '4.0.0', 'all');
    wp_enqueue_style('accesibility-style', get_template_directory_uri() . '/assets/css/style.css', array('accesibility-boostrap', 'accesibility-sidebar-style'), $version, 'all');
}

add_action('wp_enqueue_scripts', 'accesibility_register_styles');

function accesibility_register_scripts()
{
    $version = wp_get_theme()->get('Version');
    wp_enqueue_script('accesibility-jquery', 'https://code.jquery.com/jquery-3.2.1.slim.min.js', array(), '3.2.1', true);
    wp_enqueue_script('accesibility-popper', 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js', array(), '1.12.9', true);
    wp_enqueue_script('accesibility-boostrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js', array('accesibility-jquery'), '4.0.0', true);
    wp_enqueue_script('accesibility-sidebar-js', 'https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.concat.min.js', array('accesibility-jquery'), '3.1.5', true);
    wp_enqueue_script('accesibility-bundle', get_template_directory_uri() . '/assets/js/bundle.js', array('accesibility-jquery', 'accesibility-boostrap-js','accesibility-sidebar-js'), $version, true);
}

add_action('wp_enqueue_scripts', 'accesibility_register_scripts');

/**
 * Register Custom Navigation Walker
 */
function register_navwalker()
{
    require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
    require_once get_template_directory() . '/class-wp-bootstrap-navwalker-sidebar.php';
}
add_action('after_setup_theme', 'register_navwalker');


function bootstrap_pagination(\WP_Query $wp_query = null, $echo = true, $params = [])
{
    if (null === $wp_query) {
        global $wp_query;
    }

    $add_args = [];

    //add query (GET) parameters to generated page URLs
    /*if (isset($_GET[ 'sort' ])) {
        $add_args[ 'sort' ] = (string)$_GET[ 'sort' ];
    }*/

    $pages = paginate_links(
        array_merge([
            'base'         => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
            'format'       => '?paged=%#%',
            'current'      => max(1, get_query_var('paged')),
            'total'        => $wp_query->max_num_pages,
            'type'         => 'array',
            'show_all'     => false,
            'end_size'     => 3,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => __('« Anterior'),
            'next_text'    => __('Siguiente »'),
            'add_args'     => $add_args,
            'add_fragment' => ''
        ], $params)
    );

    if (is_array($pages)) {
        $pagination = '<nav class="nav-pagination" aria-label="Paginación de las Noticias"><ul class="pagination justify-content-center">';

        foreach ($pages as $page) {
            $pagination .= '<li class="page-item' . (strpos($page, 'current') !== false ? ' active' : '') . '" ' . (strpos($page, 'current') !== false ? ' aria-current="page"' : '') . '> ' . str_replace('page-numbers', 'page-link', $page) . '</li>';
        }

        $pagination .= '</ul></nav>';

        if ($echo) {
            echo $pagination;
        } else {
            return $pagination;
        }
    }

    return null;
}

function add_query_vars_filter($vars)
{
    $vars[] = "s";

    return $vars;
}

add_filter('query_vars', 'add_query_vars_filter');


/**
 * Retrieve category parents.
 *
 * @param int $id Category ID.
 * @param array $visited Optional. Already linked to categories to prevent duplicates.
 * @return string|WP_Error A list of category parents on success, WP_Error on failure.
 */
function custom_get_category_parents($id, $visited = array())
{
    $chain = '';
    $parent = get_term($id, 'category');
  
    if (is_wp_error($parent)) {
        return $parent;
    }
  
    $name = $parent->name;
  
    if ($parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) {
        $visited[] = $parent->parent;
        $chain .= custom_get_category_parents($parent->parent, $visited);
    }
  
    $chain .= '<li class="breadcrumb-item"><a href="' . esc_url(get_category_link($parent->term_id)) . '">' . $name. '</a>' . '</li>';
  
    return $chain;
}


function bootstrap_breadcrumb()
{
    global $post;
  
    $html = '<div aria-label="Breadcrumb"><ol class="breadcrumb pl-0 pt-0">';
  
    if ((is_front_page()) || (is_home())) {
        $html .= '<li class="breadcrumb-item active">Portada</li>';
    } else {
        $html .= '<li class="breadcrumb-item"><a href="'.esc_url(home_url('/')).'">Portada</a></li>';
    
        if (is_attachment()) {
            $parent = get_post($post->post_parent);
            $categories = get_the_category($parent->ID);
      
            if ($categories[0]) {
                $html .= custom_get_category_parents($categories[0]);
            }
      
            $html .= '<li class="breadcrumb-item"><a href="' . esc_url(get_permalink($parent)) . '">' . $parent->post_title . '</a></li>';
            $html .= '<li class="breadcrumb-item active">' . get_the_title() . '</li>';
        } elseif (is_category()) {
            $category = get_category(get_query_var('cat'));
      
            if ($category->parent != 0) {
                $html .= custom_get_category_parents($category->parent);
            }
      
            $html .= '<li class="breadcrumb-item active">' . single_cat_title('', false) . '</li>';
        } elseif (is_page() && !is_front_page()) {
            $parent_id = $post->post_parent;
            $parent_pages = array();
      
            while ($parent_id) {
                $page = get_page($parent_id);
                $parent_pages[] = $page;
                $parent_id = $page->post_parent;
            }
      
            $parent_pages = array_reverse($parent_pages);
      
            if (!empty($parent_pages)) {
                foreach ($parent_pages as $parent) {
                    $html .= '<li class="breadcrumb-item"><a href="' . esc_url(get_permalink($parent->ID)) . '">' . get_the_title($parent->ID) . '</a></li>';
                }
            }
      
            $html .= '<li class="breadcrumb-item active">' . get_the_title() . '</li>';
        } elseif (is_singular('post')) {
            $categories = get_the_category();
      
            if ($categories[0]) {
                $html .= custom_get_category_parents($categories[0]);
            }
      
            $html .= '<li class="breadcrumb-item active">' . get_the_title() . '</li>';
        } elseif (is_tag()) {
            $html .= '<li class="breadcrumb-item active">' . single_tag_title('', false) . '</li>';
        } elseif (is_day()) {
            $html .= '<li class="breadcrumb-item"><a href="' . esc_url(get_year_link(get_the_time('Y'))) . '">' . get_the_time('Y') . '</a></li>';
            $html .= '<li class="breadcrumb-item"><a href="' . esc_url(get_month_link(get_the_time('Y'), get_the_time('m'))) . '">' . get_the_time('m') . '</a></li>';
            $html .= '<li class="breadcrumb-item active">' . get_the_time('d') . '</li>';
        } elseif (is_month()) {
            $html .= '<li class="breadcrumb-item"><a href="' . esc_url(get_year_link(get_the_time('Y'))) . '">' . get_the_time('Y') . '</a></li>';
            $html .= '<li class="breadcrumb-item active">' . get_the_time('F') . '</li>';
        } elseif (is_year()) {
            $html .= '<li class="breadcrumb-item active">' . get_the_time('Y') . '</li>';
        } elseif (is_author()) {
            $html .= '<li class="breadcrumb-item active">' . get_the_author() . '</li>';
        } elseif (is_search()) {
        } elseif (is_404()) {
        }
    }
  
    $html .= '</ol></div>';
  
    echo $html;
}


function accesibility_sidebar()
{
    register_sidebar(array(
        'name' => 'Barra Lateral Derecha',
        'id' => 'accesibility-sidebar',
        'class' => 'external-link',
        'description' => 'Sidebar',
        'before_widget' => '<div class="external-link">',
        'after_widget' => '</div>'
    ));
}

add_action('widgets_init', 'accesibility_sidebar');

function accesibility_footer()
{
    register_sidebar(array(
        'name' => 'Pie de Página Izquierda',
        'id' => 'accesibility-footer-1',
        'class' => 'external-link',
        'description' => 'Footer',
        'before_widget' => '<div class="external-link">',
        'after_widget' => '</div>'
    ));
    register_sidebar(array(
        'name' => 'Pie de Página Centro',
        'id' => 'accesibility-footer-2',
        'class' => 'external-link',
        'description' => 'Footer',
        'before_widget' => '<div class="external-link">',
        'after_widget' => '</div>'
    ));
    register_sidebar(array(
        'name' => 'Pie de Página Derecha',
        'id' => 'accesibility-footer-3',
        'class' => 'external-link',
        'description' => 'Footer',
        'before_widget' => '<div class="external-link">',
        'after_widget' => '</div>'
    ));
}

add_action('widgets_init', 'accesibility_footer');