HybridGallery.php 3.87 KB
<?php

namespace Tz\WordPress\Tools\HybridGallery;

use Tz\WordPress\Tools;
use Tz\WordPress\Tools\ShortCodes as SC;

//error_reporting(E_ALL);

/*
Plugin Name:    Hybrid Gallery
Version:        0.0.1
Description:    Mix of photos and videos into one Media Gallery Plugin
Author:         Kevin Burton
Author URI:     http://www.gotenzing.com/
*/





    
const SETTINGS_NS   = 'hybridgallery';
const ADMIN_PAGE    = 'hybridgallery-settings';
const CAPABILITY    = 'hybridgallery_admin';
    

    $role = get_role('administrator');
    $role->add_cap(CAPABILITY);
    
    Vars::$widget_dir = Tools\url('./', __FILE__);
    Vars::$upload_dir = __DIR__.'/uploads/';
    
    require('inc/config.php');
    Vars::$defaults = $config;
    
    unset($config);
    
    $mArr = array();
    foreach(Vars::$defaults as $option => $items) {
        $mArr[$option] = $items[0];
    }
    
    Vars::$WP_Settings = new Tools\WP_Option(SETTINGS_NS, $mArr);
                    
    SC\registerClass(__NAMESPACE__ . '\ShortCodes');
    Tools\add_actions(__NAMESPACE__ . '\Actions');
    
function adminDisplay() {
    $defaults = Vars::$defaults;
    require(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'settings.php');
}


class Vars {
    public static $defaults;
    public static $WP_Settings;
    public static $widget_dir;
    public static $upload_dir;
}


class Actions {

    public static function init() {
    
        global $wpdb;
        $wpdb->show_errors();
        
        register_post_type( 'gallery', array(
              'label'           => __('Gallery Entries')
            , 'public'          => true
            , 'show_ui'         => false
            , 'hierarchical'    => false
            , 'supports'        => array('category')
            ) );
        register_taxonomy( 'hbGalleries', 'gallery', array( 'hierarchical' => true, 'label' => __('Galleries') ) );
        
        
        // create a default gallery:
        
        $tablename = $wpdb->prefix . 'terms';
        $query = $wpdb->get_row("SELECT * FROM `$tablename` WHERE `slug`='hg-gallery-general' LIMIT 1",'ARRAY_A');
        
        
        
        if ($wpdb->num_rows == 0) {
            // doesn't exist, need to create default
            $wpdb->query("INSERT INTO $tablename (name,slug) VALUES ('HG General Gallery','hg-gallery-general')");
            $term_id = $wpdb->insert_id;
            $tablename = $wpdb->prefix . 'term_taxonomy';
            $wpdb->query("INSERT INTO $tablename (term_id,taxonomy,parent,count) VALUES ($term_id,'hbGalleries',0,0)");
        }
        
    }
    
    public static function admin_menu() {
        add_options_page('HybridGallery Settings', 'HybridGallery Settings', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\adminDisplay');
    }
    
    public static function admin_init() {
        register_setting(SETTINGS_NS, SETTINGS_NS);
    }
    
    public static function wp_print_scripts() {
        wp_enqueue_script('jquery');
        wp_enqueue_script('jquery-form');
        _enqueue_script('swfobject', Tools\url('assets/uploadify/swfobject.js', __FILE__), array('jquery'));
        _enqueue_script('jquery-uploadify', Tools\url('assets/uploadify/jquery.uploadify.v2.1.0.js', __FILE__), array('jquery','swfobject'));
    }

}

class ShortCodes {
    
    public static $defaults;
    
    public static function HybridGallery($args = array(), $content, $tag) {
    
        $WP_Settings = new Tools\WP_Option(SETTINGS_NS);
        
        foreach (Vars::$defaults as $key => $val) {
            if (isset($WP_Settings[$key])) {
                self::$defaults[$key] = $WP_Settings[$key];
            }
        }

        self::$defaults = shortcode_atts(self::$defaults, $args);
                
        ob_start();
        include('views/gallery.php');
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    
    }
    
}


?>