Skip to content

Instantly share code, notes, and snippets.

@jtsternberg
Forked from gregrickaby/grd_functions.php
Last active April 10, 2018 02:44
Show Gist options
  • Save jtsternberg/6918144 to your computer and use it in GitHub Desktop.
Save jtsternberg/6918144 to your computer and use it in GitHub Desktop.
<?php
/**
* A class filled with functions that will never go away upon theme deactivation
*
* @package WordPress
* @subpackage GRD
* @version 0.1.0
*/
class GRD_Functions {
/**
* A single instance of this class.
* @var object
*/
public static $instance = null;
/**
* Creates or returns an instance of this class.
* @since 0.1.0
* @return GRD_Functions A single instance of this class.
*/
public static function engage() {
if ( self::$instance === null )
self::$instance = new self();
return self::$instance;
}
/**
* Build our class and run those methods
* @since 0.1.0
*/
public function __construct() {
add_action( 'after_setup_theme', array( $this, 'constants' ), 5 ); // early, but not too early
add_action( 'init', array( $this, 'post_types' ) );
add_action( 'init', array( $this, 'taxonomies' ) );
add_action( 'wp_head', array( $this, 'dns_prefetch' ), 1 );
add_action( 'wp_head', array( $this, 'header_scripts' ) );
add_action( 'wp_head', array( $this, 'enqueue_scripts' ) );
add_action( 'wp_footer', array( $this, 'footer_scripts' ), 999 );
}
/**
* Defines the constant paths for use within the theme.
* @since 0.1.0
*/
public function constants() {
/* Sets the path to the child theme directory. */
$this->define_const( 'GRD_DIR', get_stylesheet_directory_uri() );
$GRD_DIR = trailingslashit( GRD_DIR );
/* Sets the path to the css directory. */
$this->define_const( 'GRD_CSS', $GRD_DIR . 'css' );
/* Sets the path to the images directory. */
$this->define_const( 'GRD_IMG', $GRD_DIR . 'images' );
/* Sets the path to the included files directory. */
$this->define_const( 'GRD_INC', $GRD_DIR . 'inc' );
/* Sets the path to the javascript directory. */
$this->define_const( 'GRD_JS', $GRD_DIR . 'js' );
/* Sets the path to the languages directory. */
$this->define_const( 'GRD_LANG', $GRD_DIR . 'languages' );
/* Sets the path to the sass directory. */
$this->define_const( 'GRD_SASS', $GRD_DIR . 'sass' );
/* Sets the path to CDN URL. */
$this->define_const( 'GRD_CDN', 'http://cdn.gregrickaby.com/' );
$GRD_CDN = trailingslashit( GRD_CDN );
/* Sets the path to CDN styles. */
$this->define_const( 'GRD_CDN_CSS', $GRD_CDN . 'css' );
/* Sets the path to CDN images. */
$this->define_const( 'GRD_CDN_IMG', $GRD_CDN . 'images' );
/* Sets the path to CDN scripts. */
$this->define_const( 'GRD_CDN_JS', $GRD_CDN . 'js' );
}
/**
* Define a constant if it hasn't been already (this allows them to be overridden)
* @since 0.1.0
* @param string $constant Name of constant to define
* @param string $value Value of defined constant
*/
public function define_const( $constant, $value ) {
// (can be overridden via wp-config, etc)
if ( ! defined( $constant ) )
define( $constant, $value );
}
/**
* Enqueue scripts
* @since 0.1.0
*/
public function enqueue_scripts() {
wp_enqueue_script( 'grd-progress', trailingslashit( GRD_CDN_JS ) . 'nprogress.js', array( 'jquery' ), '1.0.0', false );
}
/**
* Setup custom post types
* @since 0.1.0
*/
public function post_types() {
$this->post_type( array( 'Code', 'Code' ), array( 'menu_position' => '1' ) );
$this->post_type( 'Gif', array( 'menu_position' => '2' ) );
$this->post_type( 'Photo', array( 'menu_position' => '3' ) );
$this->post_type( 'Recipe', array( 'menu_position' => '4' ) );
}
/**
* Register custom post type
* @since 0.1.0
* @param string|array $type post_type singular label or array with names,slugs,keys
* @param array $args arguments
*/
public function post_type( $type, $args = array() ) {
// Generate our name variables $type, $types, $key, $slug
extract( $types = $this->generate_names( $type ) );
// Setup our labels
$labels = array(
'name' => $type,
'singular_name' => $type,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' .$type,
'edit_item' => 'Edit '.$type,
'new_item' => 'New '.$type,
'view_item' => 'View '.$type,
'search_items' => 'Search '.$types,
'not_found' => 'No '.$types.' found',
'not_found_in_trash' => 'No '.$types.' found in Trash',
'parent_item_colon' => '',
'menu_name' => $types
);
$rewrite = array(
'slug' => $slug,
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = wp_parse_args( $args, array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => '5',
'has_archive' => true,
'exclude_from_search' => false,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'genesis-cpt-archives-settings' ),
'taxonomies' => array()
));
// Finally, register all of this with WordPress
register_post_type( $key, $args );
}
/**
* Setup taxonomies
* @since 0.1.0
*/
public function taxonomies() {
$this->taxonomy( array( 'Tag', 'Code Tags', 'code', 'code-tags' ), array( 'code' ) );
$this->taxonomy( array( 'Tag', 'Gif Tags', 'gifs', 'gif-tags' ), array( 'gifs' ) );
$this->taxonomy( array( 'Tag', 'Photo Tags', 'photos', 'photos-tags' ), array( 'photos' ) );
$this->taxonomy( array( 'Tag', 'Recipe Tags', 'recipes', 'recipes-tags' ), array( 'recipes' ) );
}
/**
* Register custom Taxonomy
* @since 0.1.0
* @param string|array $type Taxonomy singular label or array with names,slugs,keys
* @param array $args Taxonomy arguments
*/
public function taxonomy( $type, $post_type_keys ) {
// Generate our name variables $type, $types, $key, $slug
extract( $types = $this->generate_names( $type ) );
$labels = array(
'name' => $types,
'singular_name' => $type,
'search_items' => 'Search '.$types,
'popular_items' => 'Common '.$types,
'all_items' => 'All '.$types,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit '.$type,
'update_item' => 'Update '.$type,
'add_new_item' => 'Add New '.$type,
'new_item_name' => 'New '. $type .' Name',
'separate_items_with_commas' => 'Separate '. $types. ' with commas',
'add_or_remove_items' => 'Add or remove '.$types,
'choose_from_most_used' => 'Choose from the most used '.$types
);
$rewrite = array(
'slug' => $slug,
'with_front' => true,
'hierarchical' => true,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
'rewrite' => $rewrite
);
// Finally, register all of this with WordPress
register_taxonomy( $key, $post_type_keys, $args );
}
/**
* Get post_type and taxonomy labels
* @since 0.1.0
* @param string|array $type post_type singular label or array with names,slugs,keys
* @return array All of our necessary labels generated
*/
public function generate_names( $type ) {
$types_array = array();
// If $type is an array, we'll look in that array for our different names
if ( is_array( $type ) ) {
$types_array['type'] = $type[0];
$types_array['types'] = isset( $type[1] ) ? $type[1] : $type[0] . 's';
$types_array['key'] = isset( $type[2] ) ? $type[2] : strtolower( str_ireplace( ' ', '_', $type[1] ) );
$types_array['slug'] = isset( $type[3] ) ? $type[3] : str_ireplace( '_', '-', $types_array['key'] );
}
// Otherwise, we'll auto-create our names from the name provided
else {
$types_array['type'] = $type;
$types_array['types'] = $type . 's';
$types_array['key'] = strtolower( str_ireplace( ' ', '_', $types_array['types'] ) );
$types_array['slug'] = str_ireplace( '_', '-', $types_array['key'] );
}
return $types_array;
}
/**
* DNS Prefetch
* @since 0.1.0
*/
public function dns_prefetch() { ?>
<link rel="dns-prefetch" href="//cdn.gregrickaby.com">
<link rel="dns-prefetch" href="//api.google.com">
<link rel="dns-prefetch" href="//fonts.googleapis.com">
<link rel="dns-prefetch" href="//themes.googleusercontent.com">
<link rel="dns-prefetch" href="//s0.wp.com">
<link rel="dns-prefetch" href="//stats.wordpress.com">
<link rel="dns-prefetch" href="//jetpack.wordpress.com">
<?php }
/**
* Header Scripts (Google Analytics)
* @since 0.1.0
*/
public function header_scripts() { ?>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-XX']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?php }
/**
* Footer Scripts
* @since 0.1.0
*/
public function footer_scripts() { ?>
<script>
if ( typeof NProgress !== 'undefined' ) {
NProgress.start();
NProgress.set(0.4);
//Increment
var interval = setInterval(function() { NProgress.inc(); }, 1000);
jQuery(document).ready(function(){
NProgress.done();
clearInterval(interval);
});
}
</script>
<?php }
} // class GRD_Functions
// Instantiate the class
GRD_Functions::engage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment