Skip to content

Commit

Permalink
Separate styles and scripts from media.php and give them their own fi…
Browse files Browse the repository at this point in the history
…les. This also introduces the new hybrid-core-css feature that allows themes to add support for specific stylesheets and allow the framework to load them.
  • Loading branch information
Justin Tadlock committed Sep 24, 2012
1 parent 52ab759 commit 9183530
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 90 deletions.
92 changes: 2 additions & 90 deletions functions/media.php
@@ -1,7 +1,7 @@
<?php
/**
* Functions file for loading scripts and stylesheets. This file also handles the output of attachment files
* by displaying appropriate HTML elements for the attachments.
* Functions for handling media (i.e., attachments) within themes. Most functions are for handling
* the display of appropriate HTML elements on attachment pages.
*
* @package HybridCore
* @subpackage Functions
Expand All @@ -11,97 +11,9 @@
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

/* Register Hybrid Core scripts. */
add_action( 'wp_enqueue_scripts', 'hybrid_register_scripts', 1 );

/* Load Hybrid Core scripts. */
add_action( 'wp_enqueue_scripts', 'hybrid_enqueue_scripts' );

/* Load the development stylsheet in script debug mode. */
add_filter( 'stylesheet_uri', 'hybrid_debug_stylesheet', 10, 2 );

/* Add all image sizes to the image editor to insert into post. */
add_filter( 'image_size_names_choose', 'hybrid_image_size_names_choose' );

/**
* Registers JavaScript files for the framework. This function merely registers scripts with WordPress using
* the wp_register_script() function. It does not load any script files on the site. If a theme wants to register
* its own custom scripts, it should do so on the 'wp_enqueue_scripts' hook.
*
* @since 1.2.0
* @access private
* @return void
*/
function hybrid_register_scripts() {

/* Supported JavaScript. */
$supports = get_theme_support( 'hybrid-core-javascript' );

/* Register the 'drop-downs' script if the current theme supports 'hybrid-core-drop-downs'. */
if ( current_theme_supports( 'hybrid-core-drop-downs' ) || ( isset( $supports[0] ) && in_array( 'drop-downs', $supports[0] ) ) )
wp_register_script( 'drop-downs', esc_url( apply_atomic( 'drop_downs_script', trailingslashit( HYBRID_JS ) . 'drop-downs.js' ) ), array( 'jquery' ), '20110920', true );

/* Register the 'nav-bar' script if the current theme supports 'hybrid-core-nav-bar'. */
if ( isset( $supports[0] ) && in_array( 'nav-bar', $supports[0] ) )
wp_register_script( 'nav-bar', esc_url( apply_atomic( 'nav_bar_script', trailingslashit( HYBRID_JS ) . 'nav-bar.js' ) ), array( 'jquery' ), '20111008', true );
}

/**
* Tells WordPress to load the scripts needed for the framework using the wp_enqueue_script() function.
*
* @since 1.2.0
* @access private
* @return void
*/
function hybrid_enqueue_scripts() {

/* Supported JavaScript. */
$supports = get_theme_support( 'hybrid-core-javascript' );

/* Load the comment reply script on singular posts with open comments if threaded comments are supported. */
if ( is_singular() && get_option( 'thread_comments' ) && comments_open() )
wp_enqueue_script( 'comment-reply' );

/* Load the 'drop-downs' script if the current theme supports 'hybrid-core-drop-downs'. */
if ( current_theme_supports( 'hybrid-core-drop-downs' ) || ( isset( $supports[0] ) && in_array( 'drop-downs', $supports[0] ) ) )
wp_enqueue_script( 'drop-downs' );

/* Load the 'nav-bar' script if the current theme supports 'hybrid-core-nav-bar'. */
if ( isset( $supports[0] ) && in_array( 'nav-bar', $supports[0] ) )
wp_enqueue_script( 'nav-bar' );
}

/**
* Function for using a debug stylesheet when developing. To develop with the debug stylesheet,
* SCRIPT_DEBUG must be set to 'true' in the 'wp-config.php' file. This will check if a 'style.dev.css'
* file is present within the theme folder and use it if it exists. Else, it defaults to 'style.css'.
*
* @since 0.9.0
* @access private
* @param string $stylesheet_uri The URI of the active theme's stylesheet.
* @param string $stylesheet_dir_uri The directory URI of the active theme's stylesheet.
* @return string $stylesheet_uri
*/
function hybrid_debug_stylesheet( $stylesheet_uri, $stylesheet_dir_uri ) {

/* If SCRIPT_DEBUG is set to true and the theme supports 'dev-stylesheet'. */
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && current_theme_supports( 'dev-stylesheet' ) ) {

/* Remove the stylesheet directory URI from the file name. */
$stylesheet = str_replace( trailingslashit( $stylesheet_dir_uri ), '', $stylesheet_uri );

/* Change the stylesheet name to 'style.dev.css'. */
$stylesheet = str_replace( '.css', '.dev.css', $stylesheet );

/* If the stylesheet exists in the stylesheet directory, set the stylesheet URI to the dev stylesheet. */
if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $stylesheet ) )
$stylesheet_uri = trailingslashit( $stylesheet_dir_uri ) . $stylesheet;
}

/* Return the theme stylesheet. */
return $stylesheet_uri;
}

/**
* Adds theme/plugin custom images sizes added with add_image_size() to the image uploader/editor. This
* allows users to insert these images within their post content editor.
Expand Down
69 changes: 69 additions & 0 deletions functions/scripts.php
@@ -0,0 +1,69 @@
<?php
/**
* Functions for handling JavaScript in the framework. Themes can add support for the
* 'hybrid-core-javascript' feature to allow the framework to handle loading the stylesheets into
* the theme header or footer at an appropriate time.
*
* @package HybridCore
* @subpackage Functions
* @author Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2008 - 2012, Justin Tadlock
* @link http://themehybrid.com/hybrid-core
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

/* Register Hybrid Core scripts. */
add_action( 'wp_enqueue_scripts', 'hybrid_register_scripts', 1 );

/* Load Hybrid Core scripts. */
add_action( 'wp_enqueue_scripts', 'hybrid_enqueue_scripts' );

/**
* Registers JavaScript files for the framework. This function merely registers scripts with WordPress using
* the wp_register_script() function. It does not load any script files on the site. If a theme wants to register
* its own custom scripts, it should do so on the 'wp_enqueue_scripts' hook.
*
* @since 1.2.0
* @access private
* @return void
*/
function hybrid_register_scripts() {

/* Supported JavaScript. */
$supports = get_theme_support( 'hybrid-core-javascript' );

/* Register the 'drop-downs' script if the current theme supports 'hybrid-core-drop-downs'. */
if ( current_theme_supports( 'hybrid-core-drop-downs' ) || ( isset( $supports[0] ) && in_array( 'drop-downs', $supports[0] ) ) )
wp_register_script( 'drop-downs', esc_url( apply_atomic( 'drop_downs_script', trailingslashit( HYBRID_JS ) . 'drop-downs.js' ) ), array( 'jquery' ), '20110920', true );

/* Register the 'nav-bar' script if the current theme supports 'hybrid-core-nav-bar'. */
if ( isset( $supports[0] ) && in_array( 'nav-bar', $supports[0] ) )
wp_register_script( 'nav-bar', esc_url( apply_atomic( 'nav_bar_script', trailingslashit( HYBRID_JS ) . 'nav-bar.js' ) ), array( 'jquery' ), '20111008', true );
}

/**
* Tells WordPress to load the scripts needed for the framework using the wp_enqueue_script() function.
*
* @since 1.2.0
* @access private
* @return void
*/
function hybrid_enqueue_scripts() {

/* Supported JavaScript. */
$supports = get_theme_support( 'hybrid-core-javascript' );

/* Load the comment reply script on singular posts with open comments if threaded comments are supported. */
if ( is_singular() && get_option( 'thread_comments' ) && comments_open() )
wp_enqueue_script( 'comment-reply' );

/* Load the 'drop-downs' script if the current theme supports 'hybrid-core-drop-downs'. */
if ( isset( $supports[0] ) && in_array( 'drop-downs', $supports[0] ) )
wp_enqueue_script( 'drop-downs' );

/* Load the 'nav-bar' script if the current theme supports 'hybrid-core-nav-bar'. */
if ( isset( $supports[0] ) && in_array( 'nav-bar', $supports[0] ) )
wp_enqueue_script( 'nav-bar' );
}

?>
153 changes: 153 additions & 0 deletions functions/styles.php
@@ -0,0 +1,153 @@
<?php
/**
* Functions for handling stylesheets in the framework. Themes can add support for the
* 'hybrid-core-css' feature to allow the framework to handle loading the stylesheets into the
* theme header at an appropriate point.
*
* @package HybridCore
* @subpackage Functions
* @author Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2008 - 2012, Justin Tadlock
* @link http://themehybrid.com/hybrid-core
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

/* Register Hybrid Core styles. */
add_action( 'wp_enqueue_scripts', 'hybrid_register_styles', 1 );

/* Load Hybrid Core styles. */
add_action( 'wp_enqueue_scripts', 'hybrid_enqueue_styles', 5 );

/* Load the development stylsheet in script debug mode. */
add_filter( 'stylesheet_uri', 'hybrid_debug_stylesheet', 10, 2 );

/**
* Registers stylesheets for the framework. This function merely registers styles with WordPress using
* the wp_register_style() function. It does not load any stylesheets on the site. If a theme wants to
* register its own custom styles, it should do so on the 'wp_enqueue_scripts' hook.
*
* @since 1.5.0
* @access private
* @return void
*/
function hybrid_register_styles() {

/* Get framework styles. */
$styles = hybrid_get_styles();

/* Loop through each style and register it. */
foreach ( $styles as $style => $args ) {

$defaults = array(
'handle' => $style,
'src' => trailingslashit( HYBRID_CSS ) . "{$style}.css",
'deps' => null,
'version' => false,
'media' => 'all'
);

$args = wp_parse_args( $args, $defaults );

wp_register_style(
sanitize_key( $args['handle'] ),
esc_url( $args['src'] ),
is_array( $args['deps'] ) ? $args['deps'] : null,
preg_replace( '/[^a-z0-9_\-.]/', '', strtolower( $args['version'] ) ),
esc_attr( $args['media'] )
);
}
}

/**
* Tells WordPress to load the styles needed for the framework using the wp_enqueue_style() function.
*
* @since 1.5.0
* @access private
* @return void
*/
function hybrid_enqueue_styles() {

/* Get the theme-supported stylesheets. */
$supports = get_theme_support( 'hybrid-core-css' );

/* If the theme doesn't add support for any styles, return. */
if ( !is_array( $supports[0] ) )
return;

/* Get framework styles. */
$styles = hybrid_get_styles();

/* Loop through each of the core framework styles and enqueue them if supported. */
foreach ( $styles as $style => $args ) {

if ( in_array( $style, $supports[0] ) )
wp_enqueue_style( $style );
}
}

/**
* Returns an array of the core framework's available styles for use in themes.
*
* @since 1.5.0
* @access private
* @return array $styles All the available framework styles.
*/
function hybrid_get_styles() {

$styles = array(
'18px' => array( 'version' => '20110523' ),
'20px' => array( 'version' => '20110523' ),
'21px' => array( 'version' => '20110523' ),
'22px' => array( 'version' => '20110523' ),
'24px' => array( 'version' => '20110523' ),
'25px' => array( 'version' => '20110523' ),
'drop-downs' => array( 'version' => '20110919' ),
'nav-bar' => array( 'version' => '20110519' ),
'gallery' => array( 'version' => '20120222' ),
);

/* If a child theme is active, add the parent theme's style. */
if ( is_child_theme() ) {
$parent = wp_get_theme( get_template(), get_theme_root( get_template_directory() ) );
$styles['parent'] = array( 'src' => trailingslashit( THEME_URI ) . 'style.css', 'version' => $parent->get( 'Version' ) );
}

/* Add the active theme style. */
$styles['style'] = array( 'src' => get_stylesheet_uri(), 'version' => wp_get_theme()->get( 'Version' ) );

/* Return the array of styles. */
return $styles;
}

/**
* Function for using a debug stylesheet when developing. To develop with the debug stylesheet,
* SCRIPT_DEBUG must be set to 'true' in the 'wp-config.php' file. This will check if a 'style.dev.css'
* file is present within the theme folder and use it if it exists. Else, it defaults to 'style.css'.
*
* @since 0.9.0
* @access private
* @param string $stylesheet_uri The URI of the active theme's stylesheet.
* @param string $stylesheet_dir_uri The directory URI of the active theme's stylesheet.
* @return string $stylesheet_uri
*/
function hybrid_debug_stylesheet( $stylesheet_uri, $stylesheet_dir_uri ) {

/* If SCRIPT_DEBUG is set to true and the theme supports 'dev-stylesheet'. */
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && current_theme_supports( 'dev-stylesheet' ) ) {

/* Remove the stylesheet directory URI from the file name. */
$stylesheet = str_replace( trailingslashit( $stylesheet_dir_uri ), '', $stylesheet_uri );

/* Change the stylesheet name to 'style.dev.css'. */
$stylesheet = str_replace( '.css', '.dev.css', $stylesheet );

/* If the stylesheet exists in the stylesheet directory, set the stylesheet URI to the dev stylesheet. */
if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $stylesheet ) )
$stylesheet_uri = trailingslashit( $stylesheet_dir_uri ) . $stylesheet;
}

/* Return the theme stylesheet. */
return $stylesheet_uri;
}

?>
6 changes: 6 additions & 0 deletions hybrid.php
Expand Up @@ -269,6 +269,12 @@ function functions() {
/* Load the template hierarchy if supported. */
require_if_theme_supports( 'hybrid-core-template-hierarchy', trailingslashit( HYBRID_FUNCTIONS ) . 'template-hierarchy.php' );

/* Load the styles if supported. */
require_if_theme_supports( 'hybrid-core-css', trailingslashit( HYBRID_FUNCTIONS ) . 'styles.php' );

/* Load the scripts if supported. */
require_if_theme_supports( 'hybrid-core-javascript', trailingslashit( HYBRID_FUNCTIONS ) . 'scripts.php' );

/* Load the deprecated functions if supported. */
require_if_theme_supports( 'hybrid-core-deprecated', trailingslashit( HYBRID_FUNCTIONS ) . 'deprecated.php' );
}
Expand Down

0 comments on commit 9183530

Please sign in to comment.