Advertisement
Guest User

functions.php

a guest
Jul 22nd, 2012
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 30.11 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Agility functions and definitions
  4.  *
  5.  * @package Agility
  6.  * @since Agility 1.0
  7.  */
  8.  
  9. /**
  10.  * Set the content width based on the theme's design and stylesheet.
  11.  *
  12.  * @since Agility 1.0
  13.  */
  14. include'modules/custom_post_types/post.php'
  15. if ( ! isset( $content_width ) )
  16.     $content_width = 640; /* pixels */
  17.  
  18.  
  19. if ( ! function_exists( 'agility_setup' ) ):
  20. /**
  21.  * Sets up theme defaults and registers support for various WordPress features.
  22.  *
  23.  * Note that this function is hooked into the after_setup_theme hook, which runs
  24.  * before the init hook. The init hook is too late for some features, such as indicating
  25.  * support post thumbnails.
  26.  *
  27.  * @since Agility 1.0
  28.  */
  29. function agility_setup() {
  30.  
  31.     /**
  32.      * Custom template tags for this theme.
  33.      */
  34.     require( get_template_directory() . '/inc/template-tags.php' );
  35.  
  36.     /**
  37.      * Custom widgets
  38.      */
  39.     require( get_template_directory() . '/inc/widgets.php' );
  40.  
  41.  
  42.     /**
  43.      * Make theme available for translation
  44.      * Translations can be filed in the /languages/ directory
  45.      * If you're building a theme based on Agility, use a find and replace
  46.      * to change 'agility' to the name of your theme in all the template files
  47.      */
  48.     load_theme_textdomain( 'agility', get_template_directory() . '/languages' );
  49.  
  50.     $locale = get_locale();
  51.     $locale_file = get_template_directory() . "/languages/$locale.php";
  52.     if ( is_readable( $locale_file ) )
  53.         require_once( $locale_file );
  54.  
  55.     /**
  56.      * Add default posts and comments RSS feed links to head
  57.      */
  58.     add_theme_support( 'automatic-feed-links' );
  59.  
  60.     /**
  61.      * This theme uses wp_nav_menu() in one location.
  62.      */
  63.     register_nav_menus( array(
  64.         'primary' => __( 'Primary Menu', 'agility' ),
  65.     ) );
  66.  
  67.     /**
  68.      * Add support for the Aside and Gallery Post Formats
  69.      */
  70.     add_theme_support( 'post-formats', array( 'image', 'video', 'gallery', 'status', 'quote'  ) );
  71.    
  72.     /**
  73.      * Add support for post thumbnails
  74.      */
  75.     add_theme_support( 'post-thumbnails' );
  76.  
  77.     /**
  78.      * Add Image sizes based on Control Panel Settings
  79.      */
  80.     global $agilitySettings;
  81.  
  82.     add_image_size( 'natural_940', 940 );
  83.     add_image_size( 'natural_640', 640 );
  84.  
  85.     $ratio_w = $agilitySettings->op( 'crop-ratio-w' );
  86.     $ratio_h = $agilitySettings->op( 'crop-ratio-h' );
  87.  
  88.     agility_register_crop_size( 940 , $ratio_w , $ratio_h );
  89.     agility_register_crop_size( 640 , $ratio_w , $ratio_h );
  90.     //agility_register_crop_size( 420 , $ratio_w , $ratio_h );
  91.  
  92.     //Initialize Post Types
  93.     $GLOBALS['cpt_portfolio'] = new PortfolioPostType();
  94.     $GLOBALS['cpt_slide'] = new SlidePostType();
  95.     $GLOBALS['cpt_query'] = new QueryPostType();
  96.  
  97.     //Initialize Meta Boxes
  98.     $GLOBALS['cpt_post_settings_metabox_post']  = new PostSettingsMetaBox( 'post_settings', __( 'Post Settings', 'agility' ), 'post', 'side', 'default' ); 
  99.     $GLOBALS['cpt_page_settings_metabox_post']  = new PageSettingsMetaBox( 'page_settings', __( 'Page Settings', 'agility') , 'page', 'side', 'default' );
  100.     $GLOBALS['cpt_bricklayer_metabox']          = new BrickLayerMetaBox( 'bricklayer_settings', 'BrickLayer', 'page', 'side', 'default' );
  101.     $GLOBALS['cpt_portfolio_metabox']           = new PortfolioMetaBox( 'portfolio_settings', __( 'Portfolio Settings', 'agility') , 'page', 'side', 'default' );
  102.  
  103.     if( $agilitySettings->op( 'self-hosted-video' ) ) {
  104.         $GLOBALS['cpt_shvideo_metabox_post']    = new SHVideoSettingsMetaBox( 'shvideo_settings', __( 'Self-Hosted Video', 'agility') , 'post', 'side', 'low' );   
  105.         $GLOBALS['cpt_shvideo_metabox_portfolio'] = new SHVideoSettingsMetaBox( 'shvideo_settings', __( 'Self-Hosted Video', 'agility') , 'portfolio-item', 'side', 'low' );
  106.     }  
  107.  
  108.     //Add support for styling the WYSIWIG editor
  109.     add_theme_support('editor_style');
  110.     add_editor_style( array(  'stylesheets/editor_styles.css' )  );
  111.  
  112.  
  113.     //UberMenu-specific
  114.     if( function_exists( 'uberMenu_easyIntegrate' ) ){
  115.         add_filter( 'wp_nav_menu_args' , 'agility_megaMenuFilter', 2100 );      //filters arguments passed to wp_nav_menu
  116.         //global $uberMenu; $uberMenu->ops['wpmega-menubar-full']['default'] = 'off';
  117.     }
  118.  
  119. }
  120. endif; // agility_setup
  121. add_action( 'after_setup_theme', 'agility_setup' );
  122.  
  123. /**
  124.  * Calculates the appropriate dimensions for an image size based on
  125.  * desired aspect ratio
  126.  */
  127. function agility_register_crop_size( $crop_w , $ratio_w , $ratio_h ){
  128.     $crop_h = (int) ( ( $ratio_h / $ratio_w ) * $crop_w );
  129.     add_image_size( 'crop_'.$crop_w, $crop_w, $crop_h, true );
  130. }
  131.  
  132. /**
  133.  * Register widgetized area and update sidebar with default widgets
  134.  *
  135.  * @since Agility 1.0
  136.  */
  137. function agility_widgets_init() {
  138.  
  139.     register_sidebar( array(
  140.         'name' => __( 'Sidebar', 'agility' ),
  141.         'id' => 'sidebar-1',
  142.         'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  143.         'after_widget' => "</aside>",
  144.         'before_title' => '<h1 class="widget-title">',
  145.         'after_title' => '</h1>',
  146.         'agility' => array(
  147.             'bricks'        =>  true,
  148.             'custom_side'   =>  true,
  149.             'wrap_cols'     =>  true,
  150.         )
  151.     ) );
  152.  
  153.     register_sidebar( array(
  154.         'name' => __( 'Drop Container', 'agility' ),
  155.         'id' => 'drop-container',
  156.         'before_widget' => '<div id="%1$s" class="widget %2$s cf nobottom">',
  157.         'after_widget' => "</div>",
  158.         'before_title' => '<h3 class="widget-title">',
  159.         'after_title' => '</h3>',
  160.         'agility' => array(
  161.             'bricks'        =>  false,
  162.             'custom_side'   =>  false,
  163.             'wrap_cols'     =>  false,
  164.         )
  165.     ) );
  166.  
  167.  
  168.     register_sidebar( array(
  169.         'name' => __( 'Home Template - Lower Area', 'agility' ),
  170.         'id' => 'home-template',
  171.         'before_widget' => '<aside id="%1$s" class="eight columns widget %2$s">',
  172.         'after_widget' => "</aside>",
  173.         'before_title' => '<h6 class="widget-title">',
  174.         'after_title' => '</h6>',
  175.         'agility' => array(
  176.             'bricks'        =>  false,
  177.             'custom_side'   =>  false,
  178.             'wrap_cols'     =>  false,
  179.         )
  180.     ) );
  181.  
  182.     register_sidebar( array(
  183.         'name' => __( 'After Post', 'agility' ),
  184.         'id' => 'after-post',
  185.         'before_widget' => '<aside id="%1$s" class="widget %2$s single-post-extra cf">',
  186.         'after_widget' => "</aside>",
  187.         'before_title' => '<h6 class="widget-title">',
  188.         'after_title' => '</h6>',
  189.         'agility' => array(
  190.             'bricks'        =>  true,
  191.             'custom_side'   =>  false,
  192.             'wrap_cols'     =>  true,
  193.         )
  194.     ) );
  195.  
  196.    
  197.  
  198.     register_sidebar( array(
  199.         'name' => __( 'Footer Left', 'agility' ),
  200.         'id' => 'footer-left',
  201.         'before_widget' => '<aside id="%1$s" class="widget %2$s cf">',
  202.         'after_widget' => "</aside>",
  203.         'before_title' => '<h3 class="widget-title">',
  204.         'after_title' => '</h3>',
  205.         'agility' => array(
  206.             'bricks'        =>  false,
  207.             'custom_side'   =>  false,
  208.             'wrap_cols'     =>  true,
  209.         )
  210.     ) );
  211.  
  212.     register_sidebar( array(
  213.         'name' => __( 'Footer Center', 'agility' ),
  214.         'id' => 'footer-center',
  215.         'before_widget' => '<aside id="%1$s" class="widget %2$s cf">',
  216.         'after_widget' => "</aside>",
  217.         'before_title' => '<h3 class="widget-title">',
  218.         'after_title' => '</h3>',
  219.         'agility' => array(
  220.             'bricks'        =>  false,
  221.             'custom_side'   =>  false,
  222.             'wrap_cols'     =>  true,
  223.         )
  224.     ) );
  225.  
  226.     register_sidebar( array(
  227.         'name' => __( 'Footer Right', 'agility' ),
  228.         'id' => 'footer-right',
  229.         'before_widget' => '<aside id="%1$s" class="widget %2$s cf">',
  230.         'after_widget' => "</aside>",
  231.         'before_title' => '<h3 class="widget-title">',
  232.         'after_title' => '</h3>',
  233.         'agility' => array(
  234.             'bricks'        =>  false,
  235.             'custom_side'   =>  false,
  236.             'wrap_cols'     =>  true,
  237.         )
  238.     ) );
  239.  
  240.  
  241.     /* Custom Sidebars */
  242.     global $agilitySettings;
  243.     $numSidebars = $agilitySettings->op( 'custom-sidebars' );
  244.  
  245.     if(!empty($numSidebars)){
  246.         if($numSidebars == 1){
  247.             register_sidebar(array(
  248.                 'name'          => __('Agility Custom Widget Area 1', 'agility'),
  249.                 'id'            => 'agility-custom-sidebar',
  250.                 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  251.                 'after_widget'  => "</aside>",
  252.                 'before_title'  => '<h1 class="widget-title">',
  253.                 'after_title'   => '</h1>',
  254.                 'description'   => 'This widget can replace the sidebar on a particular page, or be used in a BrickLayer layout',
  255.                 'agility' => array(
  256.                     'bricks'        =>  true,
  257.                     'custom_side'   =>  true,
  258.                     'wrap_cols'     =>  true,
  259.                 )
  260.             ));            
  261.         }
  262.         else{
  263.             register_sidebars( $numSidebars, array(
  264.                 'name'          => __('Agility Custom Widget Area %d', 'agility'),
  265.                 'id'            => 'agility-custom-sidebar',
  266.                 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  267.                 'after_widget'  => "</aside>",
  268.                 'before_title'  => '<h6 class="widget-title">',
  269.                 'after_title'   => '</h6>',
  270.                 'description'   => 'This widget can replace the sidebar on a particular page, or be used in a BrickLayer layout',
  271.                 'agility' => array(
  272.                     'bricks'        =>  true,
  273.                     'custom_side'   =>  true,
  274.                     'wrap_cols'     =>  true,
  275.                 )
  276.             ));
  277.         }
  278.     }
  279. }
  280. add_action( 'widgets_init', 'agility_widgets_init' );
  281.  
  282. /**
  283.  * Retrieve a list of registered sidebars for a specific group
  284.  */
  285. function agility_sidebar_ops( $for = 'custom_side' ){
  286.     global $wp_registered_sidebars;
  287.     $sidebars = array();
  288.  
  289.     foreach( $wp_registered_sidebars as $sidebar_id => $sidebar ){
  290.  
  291.         $name = $sidebar['name'];
  292.  
  293.         $valid = false;
  294.         if( !$for ) $valid = true;
  295.         else{
  296.             if( isset( $sidebar['agility'] ) ){
  297.                 $agops = $sidebar['agility'];
  298.  
  299.                 switch( $for ){
  300.  
  301.                     case 'custom_side':
  302.                         if( $agops['custom_side'] ) $valid = true;
  303.                         break;
  304.  
  305.                     case 'bricks':
  306.                         if( $agops['bricks'] ) $valid = true;
  307.                         break;
  308.  
  309.                 }
  310.             }
  311.         }
  312.  
  313.         if( $valid ) $sidebars[$sidebar_id] = $name;
  314.         if( $name == 'Sidebar' ) $sidebars[$sidebar_id] = $name . ' (Default)';
  315.     }
  316.     //ssd( $sidebars );
  317.     return $sidebars;
  318. }
  319.  
  320. /**
  321.  * Enqueue scripts and styles
  322.  */
  323. function agility_scripts_and_styles() {
  324.     global $post, $agilitySettings;
  325.  
  326.     $dir = get_template_directory_uri().'/';
  327.  
  328.     //Stylesheets
  329.     wp_enqueue_style( 'base',       $dir.'stylesheets/base.css' );
  330.     wp_enqueue_style( 'skeleton',   $dir.'stylesheets/skeleton.css' );
  331.     wp_enqueue_style( 'layout',     $dir.'stylesheets/layout.css' );
  332.     wp_enqueue_style( 'prettyPhoto',$dir.'js/prettyPhoto/css/prettyPhoto.css' );
  333.     wp_enqueue_style( 'style',      get_stylesheet_uri() ); //enqueue last so we can override
  334.    
  335.     //Fonts
  336.     wp_enqueue_style( 'font-open-sans', 'http://fonts.googleapis.com/css?family=Open+Sans:400italic,400,700' );
  337.     wp_enqueue_style( 'font-droid-serif', 'http://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic,700italic' );
  338.    
  339.  
  340.     //Javascript   
  341.     wp_enqueue_script( 'modernizr', $dir.'js/modernizr.js', array(), false, false );
  342.     wp_enqueue_script( 'jquery' );
  343.     //wp_enqueue_script( 'tabs' , $dir.'js/tabs.js' ); //Now incorporated into agility.js
  344.     wp_enqueue_script( 'prettyPhoto', $dir.'js/prettyPhoto/js/jquery.prettyPhoto.js', array( 'jquery' ), false, true );
  345.     wp_enqueue_script( 'isotope', $dir.'js/jquery.isotope.min.js', array( 'jquery' ), false, true );
  346.     wp_enqueue_script( 'agility-js', $dir.'js/agility.js', array( 'jquery' ), false, true );
  347.     wp_enqueue_script( 'google-maps', 'http://maps.googleapis.com/maps/api/js?sensor=false', array(), false, true );
  348.     wp_enqueue_script( 'twitter-js', 'http://twitterjs.googlecode.com/svn/trunk/src/twitter.min.js', array(), false, true );
  349.  
  350.     if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  351.         wp_enqueue_script( 'comment-reply' );
  352.     }
  353.  
  354.     if ( is_singular() && wp_attachment_is_image( $post->ID ) ) {
  355.         wp_enqueue_script( 'keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20120202' );
  356.     }
  357.  
  358.     //Load JavaScript Settings
  359.     wp_localize_script( 'agility-js', 'agilitySettings', array(
  360.         'prettyPhoto_default_width' =>  $agilitySettings->op( 'prettyPhoto_default_width' ),
  361.         'slideshowSpeed'            =>  $agilitySettings->op( 'slider-speed' ),
  362.         'animationSpeed'            =>  $agilitySettings->op( 'slider-animation-speed' ),
  363.         'animation'                 =>  $agilitySettings->op( 'slider-animation' ),
  364.         'autoplay'                  =>  $agilitySettings->op( 'slider-autoplay' ),
  365.     ));
  366.  
  367.  
  368. }
  369. add_action( 'wp_enqueue_scripts', 'agility_scripts_and_styles' );
  370.  
  371. function agility_insert_custom_css(){
  372.  
  373.     global $agilitySettings;
  374.     $custom = $agilitySettings->op( 'custom_css' );
  375.  
  376.     if( $custom ){
  377.         ?>
  378.  
  379. <!-- Agility Custom CSS Tweaks - Controlled through Agility Control Panel
  380. ================================================================ -->
  381. <style type="text/css" id="agility-custom-css">
  382. <?php echo $custom; ?>
  383.    
  384. </style>
  385. <!-- end Agility Custom CSS Tweaks -->
  386.        
  387.             <?php
  388.     }
  389.  
  390. }
  391. add_action( 'wp_head', 'agility_insert_custom_css' );
  392.  
  393.  
  394.  
  395.  
  396. if ( ! function_exists( 'ssd' ) ):
  397. /**
  398.  * Print function for testing
  399.  */
  400. function ssd($d){
  401.     echo '<pre style="clear:both">';
  402.     print_r($d);
  403.     echo '</pre>';
  404. }
  405. endif;
  406.  
  407.  
  408. add_filter('the_content', 'agility_remove_bad_br_tags');
  409. function agility_remove_bad_br_tags($content) {
  410.     $content = str_ireplace("<br />\n<input", "<input", $content);
  411.     $content = str_ireplace("<br />\n<label", "<label", $content);
  412.     return $content;
  413. }
  414.  
  415.  
  416. function agility_excerpt_more($more) {
  417.     return '...';
  418. }
  419. add_filter( 'excerpt_more', 'agility_excerpt_more' );
  420.  
  421.  
  422. /**
  423.  * Add Vimeo's https to OEmbed providers
  424.  */
  425. function agility_oembed_providers( $providers ){
  426.     /* Allow https:// to be used with Vimeo */
  427.     $providers ['#https://(www\.)?vimeo\.com/.*#i'] = array( 'http://vimeo.com/api/oembed.{format}', true  );
  428.  
  429.     return $providers;
  430. }
  431. add_filter( 'oembed_providers', 'agility_oembed_providers' );
  432.  
  433.  
  434.  
  435. function agility_image_send_to_editor_filter($html, $id, $caption, $title, $align, $url, $size, $alt){
  436.  
  437.     $html = get_image_tag($id, $alt, $title, $align.' scale-with-grid', $size);
  438.  
  439.     list( $img_src, $width, $height ) = image_downsize( $id, $size );
  440.  
  441.     $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
  442.  
  443.     if ( $url ){
  444.         if( $caption ){
  445.             $html = '<a class="img-prettyPhoto" data-rel="prettyPhoto" title="'.$title.'" href="' . esc_attr($url) . "\"$rel>$html</a>";
  446.         }
  447.         else{
  448.             $html = '<a style="width:'.$width.'px; max-height:'.$height.'px; max-width:100%;" class="img-prettyPhoto align'.$align.'" data-rel="prettyPhoto" title="'.$title.'" href="' . esc_attr($url) . "\"$rel>$html</a>";
  449.         }
  450.     }
  451.  
  452.     return $html;
  453.  
  454. }
  455. add_filter('image_send_to_editor', 'agility_image_send_to_editor_filter', 10, 8);
  456.  
  457.  
  458. /**
  459.  * Get the string version of 1/$n
  460.  */
  461. function agility_one_over( $n ){
  462.     $convert = array(
  463.  
  464.         '2'     =>  'half',
  465.         '3'     =>  'third',
  466.         '4'     =>  'fourth',
  467.         '5'     =>  'fifth',
  468.         '6'     =>  'sixth',
  469.         '7'     =>  'seventh',
  470.         '8'     =>  'eighth',
  471.         '9'     =>  'ninth',
  472.         '10'    =>  'tenth'
  473.     );
  474.  
  475.     return isset( $convert[$n] ) ? $convert[$n] : $n;
  476. }
  477.  
  478.  
  479. /**
  480.  * Get the maximum pixel width based on the number of grid columns
  481.  */
  482. function agility_max_pixel_width( $columns ){
  483.     $convert = array(
  484.  
  485.         '1'     => 420,
  486.         '2'     => 420,
  487.         '3'     => 420,
  488.         '4'     => 420,
  489.         '5'     => 420,
  490.         '6'     => 420,
  491.         '7'     => 420,
  492.         '8'     => 460,
  493.         '9'     => 520,
  494.         '10'    => 580,
  495.         '11'    => 640,
  496.         '12'    => 700,
  497.         '13'    => 760,
  498.         '14'    => 820,
  499.         '15'    => 880,
  500.         '16'    => 940,
  501.  
  502.         '1-4'   => 420,
  503.         '1-3'   => 420,
  504.         '1-2'   => 460,
  505.         '2-3'   => 620,
  506.         '3-4'   => 700,
  507.  
  508.         'f'     => 940,
  509.         'natural' => 940,
  510.     );
  511.  
  512.     if( !isset( $convert[$columns] ) ) return 940;
  513.  
  514.     return $convert[$columns];
  515. }
  516.  
  517. /**
  518.  * Convert a numeric number of columns to string class
  519.  */
  520. function agility_grid_columns_class( $num_cols , $include_columns = true, $offset = '' ){
  521.  
  522.     $convert = array(
  523.  
  524.         '1'     => 'one',
  525.         '2'     => 'two',
  526.         '3'     => 'three',
  527.         '4'     => 'four',
  528.         '5'     => 'five',
  529.         '6'     => 'six',
  530.         '7'     => 'seven',
  531.         '8'     => 'eight',
  532.         '9'     => 'nine',
  533.         '10'    => 'ten',
  534.         '11'    => 'eleven',
  535.         '12'    => 'twelve',
  536.         '13'    => 'thirteen',
  537.         '14'    => 'fourteen',
  538.         '15'    => 'fifteen',
  539.         '16'    => 'sixteen',
  540.  
  541.         '1-4'   => 'one-fourth',
  542.         '1-3'   => 'one-third',
  543.         '1-2'   => 'one-half',
  544.         '2-3'   => 'two-thirds',
  545.         '3-4'   => 'three-fourths',
  546.  
  547.         'f'     => 'full-width',
  548.         'natural' => '',
  549.     );
  550.  
  551.     $grid_cols = $convert[$num_cols];
  552.  
  553.     if( $offset && isset( $convert[$offset] ) ){
  554.         $grid_cols.= " offset-by-$offset";
  555.     }
  556.  
  557.     if( $include_columns ){
  558.         $grid_cols .= agility_grid_columns_plural( $num_cols );
  559.     }
  560.  
  561.     return $grid_cols;
  562.  
  563. }
  564.  
  565. /**
  566.  * Determine the appropriate class, 'columns' or 'column' for the Skeleton framework
  567.  */
  568. function agility_grid_columns_plural( $num_cols ){
  569.    
  570.     switch( $num_cols ){
  571.  
  572.        
  573.         case '2':
  574.         case '3':
  575.         case '4':
  576.         case '5':
  577.         case '6':
  578.         case '7':
  579.         case '8':
  580.         case '9':
  581.         case '10':
  582.         case '11':
  583.         case '12':
  584.         case '13':
  585.         case '14':
  586.         case '15':
  587.         case '16':
  588.         case 'f':
  589.        
  590.         case 'two':
  591.         case 'three':
  592.         case 'four':
  593.         case 'five':
  594.         case 'six':
  595.         case 'seven':
  596.         case 'eight':
  597.         case 'nine':
  598.         case 'ten':
  599.         case 'eleven':
  600.         case 'twelve':
  601.         case 'thirteen':
  602.         case 'fourteen':
  603.         case 'fifteen':
  604.         case 'sixteen':
  605.  
  606.             return ' columns';
  607.  
  608.         case '1':
  609.         case '1-4':
  610.         case '1-3':
  611.         case '1-2':
  612.         case '2-3':
  613.         case '3-4':
  614.         case 'f':
  615.  
  616.         case 'one':
  617.         case 'one-fourth':
  618.         case 'one-third':
  619.         case 'one-half':
  620.         case 'two-thirds':
  621.         case 'three-fourths':
  622.  
  623.             return ' column';
  624.  
  625.     }
  626. }
  627.  
  628. /**
  629.  * @param cols          integer, number of grid columns of an element
  630.  * @param container     integer, number of grid columns in the wrapper
  631.  *
  632.  */
  633. function agility_divide_columns( $cols , $container = 11, $default = 1 ){
  634.  
  635.     if( !is_numeric( $cols ) ) {
  636.  
  637.         if( $cols == 'f' ){
  638.             return 1;
  639.         }
  640.  
  641.         if( $fraction = agility_convert_fraction( $cols ) ){
  642.  
  643.             switch( $container ){
  644.  
  645.                 case 16:
  646.                 case 11:
  647.                     return substr( $cols , 2 );
  648.  
  649.             }
  650.             if( $container < 11 ){
  651.                 $cols_11 = substr( $cols , 2 );     //Number of items per row in 11
  652.                 $grid_cols = 11 / $cols_11;         //Number of grid columns per item
  653.                 return (int) ( $container / $grid_cols );
  654.             }
  655.             return $fraction * $container;
  656.         }
  657.         echo '<!-- ['.$cols.']not recognized.  $columns value is a string but not a recognized fraction -->';
  658.         return 1;
  659.     }
  660.     return (int) ( $container / $cols );
  661. }
  662.  
  663. /**
  664.  * Convert fraction class representation into a float that can be used for mathematical operations
  665.  */
  666. function agility_convert_fraction( $cols ){
  667.     switch( $cols ){
  668.         case '1-4':
  669.             return .25;
  670.         case '1-3':
  671.             return 1/3;
  672.         case '1-2':
  673.             return .5;
  674.         case '2-3':
  675.             return 2/3;
  676.         case '3-4':
  677.             return .75;
  678.         case 'f':
  679.             return 1;
  680.     }
  681.     return false;
  682. }
  683.  
  684. /**
  685.  * Convert string text names to numeric representation
  686.  */
  687. function agility_cols_string_to_num( $cols , $div = 1){
  688.     $convertInt = array(
  689.         'one'   =>  1,
  690.         'two'   =>  2,
  691.         'three' =>  3,
  692.         'four'  =>  4,
  693.         'five'  =>  5,
  694.         'six'   =>  6,
  695.         'seven' =>  7,
  696.         'eight' =>  8,
  697.         'nine'  =>  9,
  698.         'ten'   =>  10,
  699.         'eleven'=>  11,
  700.         'twelve'=>  12,
  701.         'thirteen'  => 13,
  702.         'fourteen'  => 14,
  703.         'fifteen'   => 15,
  704.         'sixteen'   => 16
  705.     );
  706.  
  707.     if( isset( $convertInt[$cols] ) ) return $convertInt[$cols];
  708.  
  709.     $convertFraction = array(
  710.  
  711.         'one-third' => ( 1/3 ),
  712.         'two-thirds'=> ( 2/3 ),
  713.         'one-fourth'=> .25,
  714.         'three-fourths'=> .75,
  715.         'one-half'  => .5,
  716.         'full-width'=> 1,
  717.     );
  718.  
  719.     if( isset( $convertFraction[$cols] ) ){
  720.         return $convertFraction[$cols] * $div;
  721.     }
  722.  
  723.     return 1;
  724. }
  725.  
  726. /**
  727.  * Determine, based on index and items per column, alpha and omega classes
  728.  */
  729. function agility_alphaomega( $index , $columns ){
  730.  
  731.     if( $columns == 0 ) return '';
  732.  
  733.     $ao = array();
  734.  
  735.     if( $index % $columns == 0 )        $ao[] = 'alpha';
  736.     if( ( $index + 1) % $columns == 0 ) $ao[] = 'omega';
  737.  
  738.     return implode( ' ' , $ao );
  739.  
  740. }
  741.  
  742. /**
  743.  * Get the terms for a specific post in a specific taxonomy
  744.  */
  745. function agility_get_post_terms( $post_id, $taxonomy ){
  746.     $terms = wp_get_object_terms( $post_id, $taxonomy );
  747.     if( is_wp_error( $terms )) return array();
  748.     return $terms;
  749. }
  750.  
  751.  
  752.  
  753. /**
  754.  * Determine the image size (from those that are registered) based on the width of the container and the
  755.  * cropping settings for that post.
  756.  *
  757.  * @param post_id - ID of the post.  Might be a post, portfolio, or slide
  758.  * @param width - the width, in pixels, that the image should fill
  759.  */
  760. function agility_image_size( $post_id , $width = 940 , $crop_feature = '' ){
  761.     global $agilitySettings;
  762.  
  763.     //If no override, use post setting
  764.     if( !$crop_feature ) $crop_feature = agility_meta_on( 'crop_feature' , 'off', $post_id );
  765.     //If we're overriding, translate to boolean
  766.     else{
  767.         $crop_feature = $crop_feature == 'on' ? true : false;
  768.     }
  769.  
  770.     $crop = $crop_feature && $agilitySettings->op( 'crop-ratio-w' );
  771.  
  772.     $size = 'full';
  773.  
  774.     if( $width <= 640 ){
  775.         if( $crop ) $size = 'crop_640';
  776.         else $size = 'natural_640';
  777.     }
  778.     else if( $width <= 940 ){
  779.         if( $crop ) $size = 'crop_940';
  780.         else $size = 'natural_940';
  781.     }
  782.  
  783.     //Allow users to define their own sizes and return them accordingly
  784.     $size = apply_filters( 'agility_image_size', $size, $post_id, $width , $crop );
  785.  
  786.     return $size;
  787.  
  788. }
  789.  
  790. /**
  791.  * Get the featured media type options available
  792.  */
  793. function agility_feature_types(){
  794.  
  795.     global $agilitySettings;
  796.  
  797.     $featureTypes = array(
  798.         'image'         =>  __( 'Featured Image', 'agility') ,
  799.         'video-embed'   =>  __( 'Featured Video - Embedded', 'agility') ,
  800.         'video-self'    =>  __( 'Featured Video - Self-Hosted', 'agility') ,
  801.         'slider'        =>  __( 'Image Slider (Attachments)', 'agility') ,
  802.         'none'          =>  __( 'None', 'agility')
  803.     );
  804.  
  805.     if( !$agilitySettings->op( 'self-hosted-video' ) ) {
  806.         unset( $featureTypes['video-self'] );
  807.     }
  808.  
  809.     return $featureTypes;
  810. }
  811.  
  812.  
  813. /**
  814.  * Allow users to be able to wrap content that shouldn't be auto-p'd and texturized with the [raw] code
  815.  */
  816. function agility_formatter($content) {
  817.        $new_content = '';
  818.        $pattern_full = '{(\[raw\].*?\[/raw\])}is';
  819.        $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
  820.        $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
  821.  
  822.        foreach ($pieces as $piece) {
  823.                if (preg_match($pattern_contents, $piece, $matches)) {
  824.                        $new_content .= $matches[1];
  825.                } else {
  826.                        $new_content .= wptexturize(wpautop($piece));
  827.                }
  828.        }
  829.  
  830.        return $new_content;
  831. }
  832.  
  833. remove_filter('the_content', 'wpautop');
  834. remove_filter('the_content', 'wptexturize');
  835.  
  836. add_filter('the_content', 'agility_formatter', 99);
  837.  
  838.  
  839. /**
  840.  * Setup the basic values for displaying posts in the blog loop
  841.  */
  842. function agility_post_loop_setup(){
  843.  
  844.     global $post, $post_index, $agilitySettings;
  845.  
  846.     $post_class     = 'cf';
  847.     $is_latest      = ( $post_index == 0 && get_query_var( 'paged' ) == 0 );
  848.     $feature_type   = get_post_meta( $post->ID , 'feature_type', true );
  849.     $show_feature   = has_post_thumbnail() && $feature_type != 'video-self';
  850.  
  851.     if( $show_feature && $is_latest ){
  852.         $post_class.= ' first';
  853.     }
  854.  
  855.     return compact( 'post_class' , 'is_latest' , 'feature_type' , 'show_feature' );
  856.  
  857. }
  858.  
  859. /**
  860.  * Setup values specific to Post type posts in the blog loop
  861.  */
  862. function agility_post_loop_setup_post( $show_feature , $is_latest ){
  863.     $left_class = '';
  864.     $right_class = '';
  865.     $right_header_class = '';
  866.     $meta_left_cols = 'four';
  867.     $meta_right_cols = 'seven';
  868.  
  869.     if( $show_feature ){
  870.         $left_class = "four columns alpha";
  871.         $right_class = $right_header_class = "seven columns omega";
  872.         $meta_left_cols = 'three';
  873.         $meta_right_cols = 'four';
  874.  
  875.         if( $is_latest ){
  876.             $left_class = "seven columns alpha";
  877.             $right_class = "four columns omega";
  878.             $right_header_class = '';
  879.             $meta_left_cols = 'four';
  880.             $meta_right_cols = 'seven';
  881.         }
  882.  
  883.     }
  884.     else{
  885.         $right_class = 'clearfix';
  886.     }
  887.  
  888.     return compact( 'left_class', 'right_class', 'right_header_class', 'meta_left_cols', 'meta_right_cols' );
  889. }
  890.  
  891.  
  892. /**
  893.  * Load Agility resources in the admin area
  894.  */
  895. function agility_admin_resources(){
  896.     $dir = get_template_directory_uri().'/';
  897.  
  898.     //Javascript   
  899.     wp_enqueue_script( 'agility-admin', $dir.'js/admin.js', array( 'jquery', 'quicktags' ), false, false );
  900.  
  901.     //Load styles for post edit
  902.     wp_enqueue_style( 'agility-edit', $dir.'stylesheets/agility_edit.css', array(), false, false );
  903.    
  904. }
  905. add_action( 'admin_print_styles-post.php', 'agility_admin_resources' );
  906. add_action( 'admin_print_styles-post-new.php', 'agility_admin_resources' );
  907.  
  908. function agility_nav_hint(){
  909.     ?>
  910.     <div class="menu-primary-container site-navigation main-navigation two-thirds column omega">
  911.         <div class="hint"><?php _e( 'To create a menu, visit Appearance > Menus in the WordPress admin and set a menu in the Primary Theme Location.', 'agility' ); ?></div>
  912.     </div>
  913.     <?php
  914. }
  915.  
  916. /**
  917.  * Adds classes to UberMenu to properly align menu
  918.  */
  919. function agility_megaMenuFilter( $args ){
  920.     $args['container_class'].= ' two-thirds column omega';
  921.     return $args;
  922.  
  923. }
  924.  
  925.  
  926. /**
  927.  * Loads an individual theme module
  928.  */
  929. function agility_load_module( $module , $rootFile='' ){
  930.     if( $rootFile == '' ) $rootFile = $module;
  931.     require_once( "modules/$module/$rootFile.php" );
  932. }
  933.  
  934. /**
  935.  * Loads all modules together.
  936.  */
  937. function agility_load_modules(){
  938.  
  939.     //Load Modules
  940.     agility_load_module( 'custom_post_types', 'CustomPostType.class' );
  941.     agility_load_module( 'custom_post_types', 'post-settings' );
  942.     agility_load_module( 'video' );
  943.     agility_load_module( 'slider' );
  944.     agility_load_module( 'portfolio' );
  945.     agility_load_module( 'author_profile' );
  946.  
  947.     agility_load_module( 'query_constructor', 'QueryConstructor.class');
  948.     agility_load_module( 'bricklayer', 'BrickLayer.class' );
  949. }
  950.  
  951. /**
  952.  * Custom Theme Options
  953.  *
  954.  * We have to load the SparkOptions module before the theme options, and before the shortcodes
  955.  * due to code dependencies
  956.  */
  957. agility_load_module( 'sparkoptions', 'SparkOptions.class');
  958. require( get_template_directory() . '/inc/theme-options.php' );
  959.  
  960. /**
  961.  * Custom shortcodes
  962.  */
  963. require( get_template_directory() . '/inc/shortcodes.php' );
  964.  
  965. //Load up the rest
  966. agility_load_modules();
  967.  
  968.  
  969. /*function agility_oembed_html(  $html, $url, $attr, $post_ID ){
  970.     $html = str_replace( 'feature=oembed', 'feature=oembed&#038;rel=0', $html );
  971.     return $html;
  972. }
  973. add_filter( 'embed_oembed_html', 'agility_oembed_html', 10, 4 );*/
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980. /**
  981.  * Provides a notification everytime the theme is updated
  982.  * Original code courtesy of João Araújo of Unisphere Design - http://themeforest.net/user/unisphere
  983.  */
  984.  
  985. function agility_update_notifier_menu() {  
  986.  
  987.     if( !function_exists( 'simplexml_load_string' ) ) return;
  988.     global $wp_admin_bar, $wpdb;
  989.  
  990.     $xml = agility_get_latest_theme_version(172800); // This tells the function to cache the remote call for 172800 seconds (48 hours)
  991.     $theme_data = get_theme_data(TEMPLATEPATH . '/style.css'); // Get theme data from style.css (current version is what we want)
  992.    
  993.     if(version_compare($theme_data['Version'], $xml->latest) == -1) {
  994.         $wp_admin_bar->add_menu( array( 'id' => 'agility_update_notifier', 'title' => '<span> Agility Theme <span id="ab-updates">New Updates</span></span>', 'href' => get_admin_url() . 'admin.php?page=agility-settings&updates=1' ) );
  995.     }
  996. }  
  997.  
  998. if( is_admin() ) add_action('admin_menu', 'agility_update_notifier_menu');
  999.  
  1000. function agility_update_notifier() {
  1001.     $xml = agility_get_latest_theme_version(172800); // This tells the function to cache the remote call for 172800 seconds (48 hours)
  1002.     $theme_data = get_theme_data(TEMPLATEPATH . '/style.css'); // Get theme data from style.css (current version is what we want) ?>
  1003.    
  1004.     <style>
  1005.         .update-nag {display: none;}
  1006.         #instructions {max-width: 800px;}
  1007.         h3.title {margin: 30px 0 0 0; padding: 30px 0 0 0; border-top: 1px solid #ddd;}
  1008.     </style>
  1009.  
  1010.     <div class="wrap">
  1011.    
  1012.         <div id="icon-tools" class="icon32"></div>
  1013.         <h2><?php echo $theme_data['Name']; ?> Theme Updates</h2>
  1014.         <div id="message" class="updated below-h2"><p><strong>There is a new version of the <?php echo $theme_data['Name']; ?> theme available.</strong> You have version <?php echo $theme_data['Version']; ?> installed. Update to version <?php echo $xml->latest; ?>.</p></div>
  1015.        
  1016.         <img style="float: left; margin: 0 20px 20px 0; border: 1px solid #ddd;" src="<?php echo get_bloginfo( 'template_url' ) . '/screenshot.png'; ?>" />
  1017.        
  1018.         <div id="instructions" style="max-width: 800px;">
  1019.             <h3>Update Download and Instructions</h3>
  1020.             <p><strong>Please note:</strong> make a <strong>backup</strong> of the Theme inside your WordPress installation folder <strong>/wp-content/themes/<?php echo strtolower($theme_data['Name']); ?>/</strong></p>
  1021.             <p>To update the Theme, login to <a href="http://www.themeforest.net/">ThemeForest</a>, head over to your <strong>downloads</strong> section and re-download the theme like you did when you bought it.</p>
  1022.             <p>Extract the zip's contents, look for the extracted theme folder, and after you have all the new files upload them using FTP to the <strong>/wp-content/themes/<?php echo strtolower($theme_data['Name']); ?>/</strong> folder overwriting the old ones (this is why it's important to backup any changes you've made to the theme files).</p>
  1023.         </div>
  1024.        
  1025.             <div class="clear"></div>
  1026.        
  1027.         <h3 class="title">Changelog</h3>
  1028.         <?php echo $xml->changelog; ?>
  1029.  
  1030.     </div>
  1031.    
  1032. <?php }
  1033.  
  1034. // This function retrieves a remote xml file on my server to see if there's a new update
  1035. // For performance reasons this function caches the xml content in the database for XX seconds ($interval variable)
  1036. function agility_get_latest_theme_version($interval) {
  1037.     // remote xml file location
  1038.     $notifier_file_url = 'http://sevensparklabs.com/updates/agility/notifier.xml';
  1039.    
  1040.     $db_cache_field = 'agility-notifier-cache';
  1041.     $db_cache_field_last_updated = 'agility-notifier-last-updated';
  1042.     $last = get_option( $db_cache_field_last_updated );
  1043.     $now = time();
  1044.     // check the cache
  1045.     if ( !$last || (( $now - $last ) > $interval) ) {
  1046.         // cache doesn't exist, or is old, so refresh it
  1047.         if( function_exists('curl_init') ) { // if cURL is available, use it...
  1048.             $ch = curl_init($notifier_file_url);
  1049.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1050.             curl_setopt($ch, CURLOPT_HEADER, 0);
  1051.             curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  1052.             $cache = curl_exec($ch);
  1053.             curl_close($ch);
  1054.         } else {
  1055.             $cache = file_get_contents($notifier_file_url); // ...if not, use the common file_get_contents()
  1056.         }
  1057.        
  1058.         if ($cache) {          
  1059.             // we got good results
  1060.             update_option( $db_cache_field, $cache );
  1061.             update_option( $db_cache_field_last_updated, time() );         
  1062.         }
  1063.         // read from the cache file
  1064.         $notifier_data = get_option( $db_cache_field );
  1065.     }
  1066.     else {
  1067.         // cache file is fresh enough, so read from it
  1068.         $notifier_data = get_option( $db_cache_field );
  1069.     }
  1070.    
  1071.     $xml = simplexml_load_string($notifier_data);
  1072.    
  1073.     return $xml;
  1074. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement