HybridGallery.php 4.25 KB
<?php

use Tz\WordPress\Tools\ShortCodes;
use Tz\WordPress\Tools;

//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/
*/



class HybridGallery {

    public static $defaults;
    public static $WP_Settings;
    public static $widget_dir;
    public static $upload_dir;
    
    
    const SETTINGS_NS   = 'hybridgallery';
    const ADMIN_PAGE    = 'hybridgallery-settings';
    const CAPABILITY    = 'hybridgallery_admin';
    
    public static function cInit() {
        $role = get_role('administrator');
        $role->add_cap(self::CAPABILITY);
        
        self::$widget_dir = Tools\url('./', __FILE__);
        self::$upload_dir = __DIR__.'/uploads/';
        
        require('inc/config.php');
        self::$defaults = $config;
        
        
        $mArr = array();
        foreach(self::$defaults as $option => $items) {
            $mArr[$option] = $items[0];
        }
        
        self::$WP_Settings = new \WP_Option(self::SETTINGS_NS, $mArr);
                        
        ShortCodes\registerClass('HybridGallery_ShortCodes');
        Tools\add_actions('HybridGallery_Actions');
    }
    
    public static function adminDisplay() {
        $defaults = self::$defaults;
        require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'settings.php');
    }

}

class HybridGallery_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', HybridGallery::CAPABILITY, HybridGallery::ADMIN_PAGE, Array('HybridGallery', 'adminDisplay'));
    }
    
    public static function admin_init() {
        register_setting(HybridGallery::SETTINGS_NS, HybridGallery::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 HybridGallery_ShortCodes {
    
    public static $defaults;
    
    public static function HybridGallery($args = array(), $content, $tag) {
    
        $WP_Settings = new \WP_Option(HybridGallery::SETTINGS_NS);
        
        foreach (HybridGallery::$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;
    
    }
    
}

HybridGallery::cInit();

?>