Skip to content
Snippets Groups Projects
Select Git revision
1 result Searching

cloudfiles-cdn.php

Blame
  • cloudfiles-cdn.php 14.68 KiB
    <?php
    /*
    Plugin Name: Cloudfiles CDN
    Plugin URI: http://voceconnect.com/
    Description: Adds/Deletes uploaded images on CDN and rewrites asset URLs to CDN
    Version: 0.1
    Author: Chris Scott, Michael Pretty
    Author URI: http://voceconnect.com/
    */
    
    require_once('voce-settings.php');
    
    class CloudfilesCdn {
    
    	var $submenu_general;
    
    	//private static $option_group = 'cloudfiles_cdn';
    	const OPTION_GENERAL = 'cloudfiles_cdn_general';
    
    	/**
    	 * get general setting
    	 *
    	 * @param string $setting setting name
    	 * @return mixed setting value or false if not set
    	 */
    	public static function get_setting($setting) {
    		$settings = get_option(self::OPTION_GENERAL);
    		if(!$settings || !is_array($settings)) {
    			$settings = array(
    				'file_extensions' => 'bmp|bz2|css|gif|ico|gz|jpg|jpeg|js|mp3|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|zip'
    			);
    		}
    		return (isset($settings[$setting])) ? $settings[$setting] : false;
    	}
    
    	public function __construct() {}
    
    	public function initialize() {
    		// relies on Voce_Settings
    		if (!class_exists('Voce_Settings')) {
    			return;
    		}
    
    		add_action('admin_menu', array($this, 'add_options_page'));
    
    		if (!self::get_setting('username') || !self::get_setting('api_key') || !self::get_setting('container') || !self::get_setting('root_url') || !self::get_setting('file_extensions')) {
    			add_action('admin_notices', array($this, 'settings_warning'));
    			return;
    		}
    
    		add_filter('wp_handle_upload', array($this, 'catch_wp_handle_upload'));
    		add_filter('wp_delete_file', array($this, 'catch_wp_delete_file'));
    		add_filter('wp_generate_attachment_metadata', array($this, 'catch_wp_generate_attachment_metadata'));
    		add_filter('bp_core_avatar_cropstore', array($this, 'catch_bp_core_avatar_cropstore'));
    		add_action('bp_core_avatar_save', array($this, 'catch_bp_core_avatar_save'), 10, 2);
    
    	}
    
    	public function settings_warning() {
    		echo "<div class='update-nag'>The Cloudfiles CDN plugin is missing some required settings.</div>";
    	}
    
    	/**
    	 * adds the options page
    	 *
    	 * @return void
    	 */
    	public function add_options_page() {
    		$this->submenu_general = add_options_page('Cloudfiles CDN', 'Cloudfiles CDN', 'manage_options', self::OPTION_GENERAL, array($this, 'submenu_general'));
    		$settings = new Voce_Settings(self::OPTION_GENERAL, self::OPTION_GENERAL);