plugins:timelyjscss:timelyjscss_code

NP_TimelyJSCSS.phpのソースコード

<?php
 
/**
 * NP_TimelyJSCSS v1.1.2
 * This plug-in adds additional information to the address of JS and CSS.
 * --------
 * Copyright (C) 2006 NKJG
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * --------
 * History :
 * ver.1.0 (20060318)
 * - 最初のリリース
 * ver.1.1 (20060322)
 * - 説明文の変更
 * - 出力バッファリングのコード変更
 * ver.1.1.1 (20060412)
 * - バグフィックス
 * ver.1.1.2 (20060427)
 * - バグフィックス
 */
 
class NP_TimelyJSCSS extends NucleusPlugin {
  var $started = false;
  var $addValue = 0;
  var $mode = 'not work';
  var $doJS = false;
  var $doCSS = false;
 
  function getName(){ return 'Timely JS and CSS'; }
  function getAuthor(){ return 'NKJG'; }
  function getURL(){ return 'http://niku.suku.name/'; }
  function getVersion(){ return '1.1.2'; }
  function getDescription(){
    return 'This plug-in adds additional information to the address of JS and CSS.';
  }
 
  function supportsFeature($what) { return (int)($what == 'SqlTablePrefix'); }
 
  function install() {
    $this->createOption('mode', 'Select working mode :', 'select', 'time', 'time|time|option|option|not work|not work');
    $this->createOption('doJS', 'Add additional information to the address of script :','yesno','yes');
    $this->createOption('doCSS', 'Add additional information to the address of CSS :','yesno','yes');
    $this->createOption('number', "Number added to address when operation mode is 'Option' :", 'text', '1', 'datatype=numerical');
  }
 
  function init() {
    $this->mode = $this->getOption('mode');
    $this->doJS = ($this->getOption('doJS') != 'no');
    $this->doCSS = ($this->getOption('doCSS') != 'no');
    $this->addValue = ($this->mode == 'option')
      ? $this->getOption('number')
        : time() ;
 
    $this->started = false;
  }
 
  function getEventList() {
    return array('PreSkinParse', 'PostSkinParse');
  }
 
  function event_PreSkinParse(&$data) {
    if($this->mode == 'not work' || !($this->doJS || $this->doCSS)) {
      return;
    }
    $this->started = ob_start(array(&$this,'doConvert'));
  }
 
  function event_PostSkinParse(&$data) {
    if($this->started) {
      ob_end_flush();
    }
  }
 
  function doConvert($contents) {
    if($this->doCSS) $contents = $this->CSSConvert($contents);
    if($this->doJS) $contents = $this->JSConvert($contents);
    return $contents;
  }
 
  function CSSConvert($contents) {
    $aContents = preg_split('!(</?head[^>]*>)!is', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
    $aContents[2] = preg_replace('!(\.css)!is', '$1?v=' . $this->addValue, $aContents[2]);
    $contents = implode('', $aContents);
    return $contents;
  }
 
  function JSConvert($contents) {
    $pattern = '!(<script[^>]*>)!is';
    $aContents = preg_split($pattern, $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
    for($i=0; $i<count($aContents); $i++) {
      if($i%2) {
        $aContents[$i] = preg_replace('!src="([^"]*\.js)"!is', 'src="$1?v=' . $this->addValue . '"', $aContents[$i]);
      }
    }
    $contents = implode('', $aContents);
    return $contents;
  }
}
 
?>
 
plugins/timelyjscss/timelyjscss_code.txt · 最終更新: 2011/03/23 21:28 (外部編集)