PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/class/scripts.php

https://github.com/NuarHaruha/WPI-WordPress-Theme
PHP | 166 lines | 111 code | 46 blank | 9 comment | 11 complexity | b71bca5c6cbffca319d97110bd138777 MD5 | raw file
  1. <?php
  2. if ( !defined('KAIZEKU') ) { die( 42);}
  3. /**
  4. * Wpi scripts class
  5. *
  6. * $Id scripts.php, 0022 10/23/2008 12:24:28 PM ck $
  7. *
  8. * @links http://www.w3.org/TR/REC-html40/interact/scripts.html W3C/Scripts
  9. */
  10. class wpiScripts{
  11. const SEP = ',';
  12. public $wp_filter_id;
  13. public $tag = array();
  14. public $head = array();
  15. public $footer = array();
  16. public $path;
  17. public $js;
  18. public function __construct()
  19. {
  20. $this->path = WPI_JS_DIR;
  21. }
  22. public function setScripts()
  23. {
  24. $this->register('jquery','head');
  25. }
  26. public function register($tag = 'jquery', $type = 'head')
  27. {
  28. $prop = array();
  29. if (file_exists(WPI_JS_DIR.$tag.'.js')){
  30. switch ($type){
  31. case 'head':
  32. $this->head[] = $tag;
  33. break;
  34. case 'footer':
  35. $this->footer[] = $tag;
  36. break;
  37. default:
  38. $this->tag[] = $tag;
  39. break;
  40. }
  41. wp_register_script($tag.'-js', $this->theme_url.$tag.'.js');
  42. } else {
  43. return false;
  44. }
  45. }
  46. public function flushJs()
  47. {
  48. $this->js = null;
  49. }
  50. public function getScripts($type = 'tag')
  51. {
  52. $this->flushJs();
  53. if (has_count($this->$type)){
  54. $this->$type = array_unique($this->$type);
  55. $this->js = join(self::SEP,$this->$type);
  56. }
  57. }
  58. public function printHead()
  59. {
  60. $this->type = 'head';
  61. $this->flushJs();
  62. if (has_count($this->head)){
  63. $this->head = array_unique($this->head);
  64. $this->js = join(self::SEP,$this->head);
  65. }
  66. $this->printScripts();
  67. }
  68. public function printFooter()
  69. {
  70. $this->type = 'footer';
  71. $this->flushJs();
  72. if (has_count($this->footer)){
  73. $this->head = array_unique($this->footer);
  74. $this->js = join(self::SEP,$this->footer);
  75. }
  76. $this->printScripts();
  77. }
  78. public function printScripts()
  79. {
  80. echo PHP_EOL.PHP_T;
  81. t('script','',array(
  82. 'id'=>'wpi-js-'.$this->type,
  83. 'type'=>'text/javascript',
  84. 'src'=> wpi_get_scripts_url($this->js),
  85. 'charset'=>'utf-8') );
  86. }
  87. public function getHeaderScripts(){
  88. $this->getScripts('head');
  89. $this->printScripts();
  90. }
  91. public function embedScript()
  92. { global $wp_query;
  93. list($lang, $locale) = explode('-',get_bloginfo('language'));
  94. $pid = (isset($wp_query->post->ID)) ? $wp_query->post->ID : 0;
  95. $js = PHP_EOL.PHP_T;
  96. $js .= '/*<![CDATA[*/'.PHP_EOL.PHP_T.PHP_T;
  97. $js .= 'var wpi = {url:'.json_encode(WPI_URL_SLASHIT);
  98. $js .= ',id:'.json_encode(wpiTemplate::bodyID());
  99. $js .= ',blogname:'.json_encode(WPI_BLOG_NAME);
  100. $js .= ',theme_url:'.json_encode(WPI_THEME_URL);
  101. $js .= ',section:'.json_encode(is_at());
  102. $js .= ',permalink:'.json_encode(trailingslashit(self_uri()));
  103. $jspath = json_encode(rel(WPI_THEME_URL.'public/scripts/') );
  104. $jsurl = json_encode(wpi_get_scripts_url('%s'));
  105. $js .= ',script:{path:'.$jspath.',url:'.$jsurl.'}';
  106. if (wpi_option('client_time_styles')){
  107. $js .= ',pid:'.$pid.',cl_type:td};jQuery(document).ready(function(){if( $(\'#\'+wpi.id).hasClass(wpi.cl_type) == false){ $(\'#\'+wpi.id).addClass(wpi.cl_type);jQuery.cookie(\'wpi-cl\',wpi.cl_type,{duration: 1/24,path: "/"});};});'.PHP_EOL;
  108. } else {
  109. $js .= ',pid:'.$pid.'};'.PHP_EOL;
  110. }
  111. // check client cookie;
  112. if ($wp_query->is_search || $wp_query->is_404){
  113. // google webmaster 404 widget
  114. $js .= PHP_T.PHP_T.'var GOOG_FIXURL_LANG = \''.$lang.'\';var GOOG_FIXURL_SITE = wpi.url;'.PHP_EOL;
  115. }
  116. $js .= PHP_T.'/*]]>*/'.PHP_EOL.PHP_T;
  117. echo PHP_T;
  118. t('script',$js,array('id'=>'wp-js-head-embed','type'=>'text/javascript','defer'=>'defer','charset'=>'utf-8'));
  119. }
  120. }