Best functions.php organization (functionalities division)

This commit is contained in:
Fabiano Alencar 2018-02-22 15:04:01 -03:00
parent a556a4288e
commit 1774a31c4f
4 changed files with 116 additions and 54 deletions

View File

@ -1,56 +1,5 @@
<?php <?php
require get_template_directory() . '/functions/setup.php';
if(!function_exists('tainacan_setup')) { require get_template_directory() . '/functions/enqueues.php';
require get_template_directory() . '/functions/pagination.php';
/**
* Execulta após o tema ser inicializado.
* Isso é usado para a configuração básica do tema, registro dos recursos do tema e init hooks.
* Observe que esta função está conectada ao gancho after_setup_theme, que é executado antes do gancho de init.
*/
function tainacan_setup() {
/**
* Não exibe o menu do administrador na pagina do site. Mesmo quando estiver logado!
**/
show_admin_bar( false );
/**
* Desabilita o FTP na instalação de Plugins
*/
define('FS_METHOD', 'direct');
}
}
add_action( 'after_setup_theme', 'tainacan_setup' );
/**
* Inclui os scripts javascript necessários ao front do thema
*/
function tainacanEnqueueScripts(){
//Adicionado o jquery ao footer das páginas.
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', includes_url( '/js/jquery/jquery.js' ), false, NULL, true );
wp_enqueue_script( 'jquery' );
//Dependencia do bootstrap 4
wp_enqueue_script('popper', get_template_directory_uri() . '/assets/vendor/bootstrap/js/popper.min.js', '', '', true);
//JS Bootstrap 4
wp_enqueue_script('bootstrap', get_template_directory_uri() . '/assets/vendor/bootstrap/js/bootstrap.min.js', array('jquery'), '4.0.0', true);
wp_enqueue_script( 'charts', 'https://www.gstatic.com/charts/loader.js');
//Javascript for Tainacan
wp_enqueue_script('jsTainacan', get_template_directory_uri() . '/assets/js/js.js', '', '1.0', true);
wp_enqueue_script('collectionGraph', get_template_directory_uri() . '/assets/js/charts.js', array('charts'), '1.0');
}
add_action('wp_enqueue_scripts', 'tainacanEnqueueScripts');
/**
* Inclui os styles necessários ao front do thema
*/
function tainacanEnqueueStyles(){
//Style Bootstrap 4
wp_enqueue_style('bootstrap', get_template_directory_uri() . '/assets/vendor/bootstrap/css/bootstrap.min.css');
//Style Tainacan
wp_enqueue_style('style', get_stylesheet_uri(), array('bootstrap'));
}
add_action('wp_enqueue_scripts', 'tainacanEnqueueStyles');

View File

@ -0,0 +1,55 @@
<?php
/**
* Enqueues Theme
*/
if ( ! function_exists('tainacan_Enqueues') ) {
/**
* Inclui os scripts javascript e os styles necessários ao front-end do thema
*/
function tainacan_Enqueues(){
/**
* Adicionando o jquery ao footer das páginas.
*/
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', includes_url( '/js/jquery/jquery.js' ), false, NULL, true );
wp_enqueue_script( 'jquery' );
/**
* Bootstrap 4
*/
//Style
wp_register_style('bootstrap4CSS', get_template_directory_uri() . '/assets/vendor/bootstrap/css/bootstrap.min.css', '', '4.0.0', '');
wp_enqueue_style('bootstrap4CSS');
//Javascript
wp_register_script('bootstrap4JS', get_template_directory_uri() . '/assets/vendor/bootstrap/js/bootstrap.min.js', array('jquery'), '4.0.0', true);
wp_enqueue_script('bootstrap4JS');
//Popper
wp_register_script('popper_bootstrap4', get_template_directory_uri() . '/assets/vendor/bootstrap/js/popper.min.js', '', '', true);
wp_enqueue_script('popper_bootstrap4');
/**
* Google Charts
*/
wp_register_script( 'googleCharts', 'https://www.gstatic.com/charts/loader.js');
wp_enqueue_script('googleCharts');
/**
* Tainacan Theme
*/
wp_register_style('tainacanStyle', get_stylesheet_uri(), array('bootstrap4CSS'));
wp_enqueue_style('tainacanStyle');
wp_register_script('tainacanJS', get_template_directory_uri() . '/assets/js/js.js', '', '1.0', true);
wp_enqueue_script('tainacanJS');
wp_register_script('collectionGraph_googleCharts', get_template_directory_uri() . '/assets/js/charts.js', array('googleCharts'), '1.0');
wp_enqueue_script('collectionGraph_googleCharts');
/**
* Comments
*/
if (is_singular() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
}
}
add_action('wp_enqueue_scripts', 'tainacan_Enqueues');

View File

@ -0,0 +1,32 @@
<?php
/**!
* Bootstrap 4 pagination
*/
if ( ! function_exists( 'pagination_bst4' ) ) {
function pagination_bst4() {
global $wp_query;
$big = 999999999; // This needs to be an unlikely integer
// For more options and info view the docs for paginate_links()
// http://codex.wordpress.org/Function_Reference/paginate_links
$paginate_links = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'mid_size' => 5,
'prev_next' => True,
'prev_text' => __('< Newer', 'tainacan'),
'next_text' => __('Older >', 'tainacan'),
'type' => 'list'
) );
$paginate_links = str_replace( "<ul class='page-numbers'>", "<ul class='pagination'>", $paginate_links );
$paginate_links = str_replace( "<li>", "<li class='page-item'>", $paginate_links );
$paginate_links = str_replace( "<li class='page-item'><span aria-current='page' class='page-numbers current'>", "<li class='page-item active'><a class='page-link' href='#'>", $paginate_links );
$paginate_links = str_replace( "<a", "<a class='page-link' ", $paginate_links );
$paginate_links = str_replace( "</span>", "</a>", $paginate_links );
$paginate_links = preg_replace( "/\s*page-numbers/", "", $paginate_links );
// Display the pagination if more than one page is found
if ( $paginate_links ) {
echo $paginate_links;
}
}
}

26
src/functions/setup.php Normal file
View File

@ -0,0 +1,26 @@
<?php
/**
* Setup Theme
*/
if(!function_exists('tainacan_setup')) {
/**
* Execulta após o tema ser inicializado.
* Isso é usado para a configuração básica do tema, registro dos recursos do tema e init hooks.
* Observe que esta função está conectada ao gancho after_setup_theme, que é executado antes do gancho de init.
*/
function tainacan_setup() {
/**
* Não exibe o menu do administrador na pagina do site. Mesmo quando estiver logado!
**/
show_admin_bar( false );
/**
* Desabilita o FTP na instalação de Plugins
*/
define('FS_METHOD', 'direct');
}
}
add_action( 'after_setup_theme', 'tainacan_setup' );