Skip to content

Commit

Permalink
adding roots-scripts.php, now using wp_enqueue_script
Browse files Browse the repository at this point in the history
  • Loading branch information
retlehs committed Jan 16, 2012
1 parent d6c0125 commit eca31bd
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 65 deletions.
13 changes: 6 additions & 7 deletions functions.php
Expand Up @@ -5,6 +5,7 @@
require_once locate_template('/inc/roots-activation.php'); // activation
require_once locate_template('/inc/roots-options.php'); // theme options
require_once locate_template('/inc/roots-cleanup.php'); // cleanup
require_once locate_template('/inc/roots-scripts.php'); // modified scripts output
require_once locate_template('/inc/roots-htaccess.php'); // rewrites for assets, h5bp htaccess
require_once locate_template('/inc/roots-hooks.php'); // hooks
require_once locate_template('/inc/roots-actions.php'); // actions
Expand Down Expand Up @@ -71,17 +72,15 @@ function roots_custom_image_header_admin() { ?>
add_action('after_setup_theme', 'roots_setup');

// http://codex.wordpress.org/Function_Reference/register_sidebar
// hook into 'widgets_init' function with a lower priority in your
// child theme to remove these sidebars
function roots_register_sidebars() {
$sidebars = array( 'Sidebar', 'Footer' );
$sidebars = array('Sidebar', 'Footer');

foreach( $sidebars as $sidebar ) {
foreach($sidebars as $sidebar) {
register_sidebar(
array(
'id'=> 'roots-' . strtolower( $sidebar ),
'name' => __( $sidebar, 'roots' ),
'description' => __( $sidebar, 'roots' ),
'id'=> 'roots-' . strtolower($sidebar),
'name' => __($sidebar, 'roots'),
'description' => __($sidebar, 'roots'),
'before_widget' => '<article id="%1$s" class="widget %2$s"><div class="container">',
'after_widget' => '</div></article>',
'before_title' => '<h3>',
Expand Down
2 changes: 0 additions & 2 deletions header.php
Expand Up @@ -21,8 +21,6 @@
<?php wp_head(); ?>
<?php roots_head(); ?>

<script defer src="<?php echo get_template_directory_uri(); ?>/js/plugins.js"></script>
<script defer src="<?php echo get_template_directory_uri(); ?>/js/script.js"></script>
</head>

<body <?php body_class(roots_body_class()); ?>>
Expand Down
57 changes: 1 addition & 56 deletions inc/roots-actions.php
Expand Up @@ -103,17 +103,11 @@ function roots_google_analytics() {

// 1140 specific
if (roots_current_framework() === '1140') {
add_action('roots_head', 'roots_1140_head');
add_action('roots_header_before', 'roots_1140_header_before');
add_action('roots_header_after', 'roots_1140_header_after');
add_action('roots_footer_before', 'roots_1140_footer_before');
add_action('roots_footer_after', 'roots_1140_footer_after');

function roots_1140_head() {
$template_uri = get_template_directory_uri();
echo "\t<script src=\"$template_uri/js/libs/css3-mediaqueries.js\"></script>";
}

function roots_1140_header_before() {
echo '<div class="container"><div class="row">', "\n";
}
Expand Down Expand Up @@ -153,65 +147,16 @@ function roots_adapt_head() {
echo "\t\t\t]\n";
echo "\t\t};\n";
echo "\t</script>\n";
echo "\t<script src=\"$template_uri/js/libs/adapt.min.js\"></script>";
}
}

// Foundation specific
if (roots_current_framework() === 'foundation') {
add_action('roots_head', 'roots_foundation_head');

function roots_foundation_head() {
$template_uri = get_template_directory_uri();
echo "\t<script src=\"$template_uri/js/foundation/jquery.reveal.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/foundation/jquery.orbit-1.3.0.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/foundation/forms.jquery.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/foundation/jquery.customforms.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/foundation/jquery.placeholder.min.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/foundation/app.js\"></script>\n";
}
}

// Bootstrap specific
if (roots_current_framework() === 'bootstrap' || roots_current_framework() === 'bootstrap_less') {
add_action('roots_head', 'roots_bootstrap_head');
add_action('roots_header_before', 'roots_bootstrap_header_before');
add_action('roots_header_after', 'roots_bootstrap_header_after');
add_action('roots_footer_before', 'roots_bootstrap_footer_before');
add_action('roots_footer_after', 'roots_bootstrap_footer_after');

function roots_bootstrap_head() {
global $roots_options;
$roots_bootstrap_js = $roots_options['bootstrap_javascript'];
$roots_bootstrap_less_js = $roots_options['bootstrap_less_javascript'];
$template_uri = get_template_directory_uri();
if (roots_current_framework() === 'bootstrap_less') {
echo "\t<script src=\"$template_uri/js/bootstrap/less-1.1.3.min.js\"></script>\n";
}
if ($roots_bootstrap_js === true) {
$roots_options['bootstrap_less_javascript'] = false;
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-modal.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-alerts.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-twipsy.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-popover.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-dropdown.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-scrollspy.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-tabs.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-buttons.js\"></script>\n";
}
if ($roots_bootstrap_less_js === true) {
$roots_options['bootstrap_javascript'] = false;
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-modal.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-alerts.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-twipsy.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-popover.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-dropdown.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-scrollspy.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-tabs.js\"></script>\n";
echo "\t<script src=\"$template_uri/js/bootstrap/bootstrap-buttons.js\"></script>\n";
}
}

function roots_bootstrap_header_before() {
echo '<div class="container">', "\n";
}
Expand All @@ -231,4 +176,4 @@ function roots_bootstrap_footer_after() {
}
}

?>
?>
101 changes: 101 additions & 0 deletions inc/roots-scripts.php
@@ -0,0 +1,101 @@
<?php

function roots_scripts() {
$template_uri = get_template_directory_uri();
wp_register_script('roots_plugins', ''.$template_uri.'/js/plugins.js', false, null, false);
wp_register_script('roots_script', ''.$template_uri.'/js/script.js', false, null, false);
wp_enqueue_script('roots_plugins');
wp_enqueue_script('roots_script');

if (roots_current_framework() === '1140') {
wp_register_script('css3-mediaqueries', ''.$template_uri.'/js/libs/css3-mediaqueries.js', false, null, false);
wp_enqueue_script('css3-mediaqueries');
}

if (roots_current_framework() === 'adapt') {
wp_register_script('adapt', ''.$template_uri.'/js/libs/adapt.min.js', false, null, false);
wp_enqueue_script('adapt');
}

if (roots_current_framework() === 'foundation') {
wp_register_script('foundation-jquery-reveal', ''.$template_uri.'/js/foundation/jquery.reveal.js', false, null, false);
wp_register_script('foundation-jquery-orbit', ''.$template_uri.'/js/foundation/jquery.orbit-1.3.0.js', false, null, false);
wp_register_script('foundation-forms-jquery', ''.$template_uri.'/js/foundation/forms.jquery.js', false, null, false);
wp_register_script('foundation-jquery-customforms', ''.$template_uri.'/js/foundation/jquery.customforms.js', false, null, false);
wp_register_script('foundation-jquery-placeholder', ''.$template_uri.'/js/foundation/jquery.placeholder.min.js', false, null, false);
wp_register_script('foundation-app', ''.$template_uri.'/js/foundation/app.js', false, null, false);
wp_enqueue_script('foundation-jquery-reveal');
wp_enqueue_script('foundation-jquery-orbit');
wp_enqueue_script('foundation-forms-jquery');
wp_enqueue_script('foundation-jquery-customforms');
wp_enqueue_script('foundation-jquery-placeholder');
wp_enqueue_script('foundation-app');
}

if (roots_current_framework() === 'bootstrap' || roots_current_framework() === 'bootstrap_less') {
global $roots_options;
$roots_bootstrap_js = $roots_options['bootstrap_javascript'];
$roots_bootstrap_less_js = $roots_options['bootstrap_less_javascript'];
$template_uri = get_template_directory_uri();
if (roots_current_framework() === 'bootstrap_less') {
wp_register_script('bootstrap-less', ''.$template_uri.'/js/bootstrap/less-1.1.3.min.js', false, null, false);
wp_enqueue_script('bootstrap-less');
}
if ($roots_bootstrap_js === true || $roots_bootstrap_less_js === true) {
$roots_options['bootstrap_less_javascript'] = false;
$roots_options['bootstrap_javascript'] = false;

wp_register_script('bootstrap-modal', ''.$template_uri.'/js/bootstrap/bootstrap-modal.js', false, null, false);
wp_register_script('bootstrap-alerts', ''.$template_uri.'/js/bootstrap/bootstrap-alerts.js', false, null, false);
wp_register_script('bootstrap-twipsy', ''.$template_uri.'/js/bootstrap/bootstrap-twipsy.js', false, null, false);
wp_register_script('bootstrap-popover', ''.$template_uri.'/js/bootstrap/bootstrap-popover.js', false, null, false);
wp_register_script('bootstrap-dropdown', ''.$template_uri.'/js/bootstrap/bootstrap-dropdown.js', false, null, false);
wp_register_script('bootstrap-scrollspy', ''.$template_uri.'/js/bootstrap/bootstrap-scrollspy.js', false, null, false);
wp_register_script('bootstrap-tabs', ''.$template_uri.'/js/bootstrap/bootstrap-tabs.js', false, null, false);
wp_register_script('bootstrap-buttons', ''.$template_uri.'/js/bootstrap/bootstrap-buttons.js', false, null, false);
wp_enqueue_script('bootstrap-modal');
wp_enqueue_script('bootstrap-alerts');
wp_enqueue_script('bootstrap-twipsy');
wp_enqueue_script('bootstrap-popover');
wp_enqueue_script('bootstrap-dropdown');
wp_enqueue_script('bootstrap-scrollspy');
wp_enqueue_script('bootstrap-tabs');
wp_enqueue_script('bootstrap-buttons');
}
}
}

add_action('wp_enqueue_scripts', 'roots_scripts');

if (!is_admin()) {
add_action('wp_print_scripts', 'roots_print_scripts');
}

function roots_print_scripts() {
global $wp_scripts;
$wp_scripts->all_deps($wp_scripts->queue);
$scripts = array();

foreach ($wp_scripts->queue as $key => $handle) {
$skip_scripts = array('jquery', 'roots_script', 'roots_plugins');

$src = $wp_scripts->registered[$handle]->src;
unset($wp_scripts->queue[$key]);
$wp_scripts->done[] = $handle;

if (!in_array($handle, $skip_scripts)) {
$scripts[] = '<script defer src="' . $src . '"></script>';
}
}

echo "\t" . implode("\n\t", $scripts) . "\n";

$template_uri = get_template_directory_uri();
echo "\t<script defer src=\"$template_uri/js/plugins.js\"></script>\n";
echo "\t<script defer src=\"$template_uri/js/script.js\"></script>\n";

$wp_scripts->reset();
return $wp_scripts->done;
}

?>

3 comments on commit eca31bd

@conatus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! Would it be better and a bit more DRY to do this series of wp_register_script and wp_enqeue_script as a loop? So we know the framework, and know all we are adding to it in every case is -something. This would look cleaner as well.

@swalkinshaw
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roots will be undergoing some big rewrites soon in terms of structure (not what it is/does). So you're correct but we might as well just wait until then to fix a lot of these issues.

@conatus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks very much.

Please sign in to comment.