Skip to content
Snippets Groups Projects
Select Git revision
  • d47223472e97593cea577fca0e18ed95ef8bece7
  • master default
  • 0.1.5
  • 0.1.4
  • 0.1.3
  • 0.1.2
  • v0.1.1
7 results

cdn-rewrite.php

Blame
  • cdn-rewrite.php 5.56 KiB
    <?php
    /*
    Plugin Name: CDN Rewrite
    Plugin URI: http://voceconnect.com/
    Description: Rewrites asset URLs to CDN
    Version: 0.1
    Author: Chris Scott, Michael Pretty
    Author URI: http://voceconnect.com/
    */
    
    require_once('voce-settings.php');
    
    class CDN_Rewrite {
    
    	const OPTION_GENERAL = 'cdn_general';
    
    	private $submenu_general;
    	private $file_extensions;
    	private $blog_details;
    	private $cdn_root_url;
    
    	public function __construct() {
    		$this->file_extensions = $this->get_setting('file_extensions');
    		$this->cdn_root_url = untrailingslashit($this->get_setting('root_url'));
    	}
    
    	public function initialize() {
    		if (!class_exists('Voce_Settings')) {
    			return;
    		}
    
    		add_action('admin_menu', array($this, 'add_options_page'));
    		if ('' == $this->file_extensions || '' == $this->cdn_root_url) {
    			add_action('admin_notices', array($this, 'settings_warning'));
    			return;
    		}
    
    		if('/' != $this->cdn_root_url) {
    			add_action('template_redirect', array($this, 'start_buffer'), 1);
    		}
    	}
    
    	/**
    	 * get general setting
    	 *
    	 * @param string $setting setting name
    	 * @return mixed setting value or false if not set
    	 */
    	private 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'
    			);
    			update_option(self::OPTION_GENERAL, $settings);
    		}
    		return (isset($settings[$setting])) ? $settings[$setting] : false;
    	}
    
    	public function settings_warning() {
    		echo "<div class='update-nag'>The CDN Rewrite plugin is missing some required settings.</div>";
    	}
    
    	/**
    	 * adds the options page
    	 *
    	 * @return void
    	 */
    	public function add_options_page() {
    		$this->submenu_general = add_options_page('CDN Rewrite', 'CDN Rewrite', 'manage_options', self::OPTION_GENERAL, array($this, 'submenu_general'));