plug_shortcodes.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. if (!class_exists("plug_shortcodes")) {
  3. class plug_shortcodes {
  4. function plug_shortcodes($ns) {
  5. $this->title = __("Misc Shortcodes","dagsopt");
  6. $this->pluginname = $ns->pluginname;
  7. $this->file = $ns->file;
  8. $this->ns = $ns;
  9. }
  10. function start() {
  11. add_filter('the_content', array(&$this,'CSV_Parse'));
  12. add_filter('the_content', array(&$this,'CSV_Parse2'));
  13. add_shortcode( 'acc-page', array(&$this,'getpage') );
  14. }
  15. function getpage( $atts ) {
  16. extract( shortcode_atts( array(
  17. 'id' => 0
  18. ), $atts ) );
  19. $s = "";
  20. if($id!==0){
  21. $page = get_page( $id );
  22. $s .= '<div class="hbox embed-page page-id-'.$id.'" >';
  23. $s .= '<div class="arrow">'.$page->post_title.'</div>';
  24. $s .= '<div class="box">';
  25. $s .= apply_filters('the_content',$page->post_content);
  26. $s .= '</div>';
  27. $s .= '</div>';
  28. }
  29. return $s;
  30. }
  31. function CSV_Parse2($content) {
  32. $content = preg_replace_callback("/\<a href=\'(.*).csv\'(.*)\>(.*)\<\/a\>/i", array(&$this,"CSV_Render2"), $content);
  33. return $content;
  34. }
  35. function CSV_Render2($matches) {
  36. //0 full 1 filename 2 attributes 3 title
  37. $filen = explode("/",$matches[1]);
  38. $class = array_pop($filen);
  39. $filen[] = $class;
  40. $filen = implode("/",$filen).".csv";
  41. $arguments = array();
  42. $arguments['filename'] = $filen;
  43. $arguments['class'] = "tablesorter ".$class;
  44. $arguments['id'] = "".$class;
  45. $arguments['title'] = "".$matches[3];
  46. return $this->CSV_Render_($arguments);
  47. }
  48. function CSV_Render($matches) {
  49. $matches[1] = str_replace(array('&#8221;','&#8243;'), '', $matches[1]);
  50. preg_match_all('/(\w*)=(.*?)" /i', $matches[1], $attributes);
  51. $arguments = array();
  52. foreach ( (array) $attributes[1] as $key => $value ) {
  53. $arguments[$value] = str_replace('"', '', $attributes[2][$key]);
  54. }
  55. if(!isset($arguments['filename']) || !is_file(ABSPATH.$arguments['filename'])){
  56. return "csv error: filename ".ABSPATH.$arguments['filename']." not readable";
  57. }
  58. return $this->CSV_Render_($arguments);
  59. }
  60. function CSV_Parse($content) {
  61. $content = preg_replace_callback("/\[\[csv ([^]]*)\/\]\]/i", array(&$this,"CSV_Render"), $content);
  62. return $content;
  63. }
  64. function CSV_Render_($arguments) {
  65. if(!isset($arguments['filename']) || !is_file(ABSPATH.$arguments['filename'])){
  66. return "csv error: filename ".ABSPATH.$arguments['filename']." not readable";
  67. }
  68. $maxread = isset($arguments['read']) ? $arguments['read'] : (1024*1024*5);
  69. $delimiter = isset($arguments['delimiter']) ? $arguments['delimiter'] : ',';
  70. $quotechar = isset($arguments['quotechar']) ? $arguments['quotechar'] : '"';
  71. $headers = isset($arguments['headers']) ? $arguments['headers'] : false;
  72. $fields = isset($arguments['fields']) ? explode(",",$arguments['fields']) : false;
  73. $tid = isset($arguments['id']) ? $arguments['id'] : 'mytable';
  74. $class = isset($arguments['class']) ? $arguments['class'] : 'tablesorter';
  75. $tables = array();
  76. $rows = array();
  77. $tmp = "";
  78. $tmpheaders = array();
  79. $handle = fopen(ABSPATH.$arguments['filename'], "r");
  80. while (($data = fgetcsv($handle, $maxread, $delimiter,$quotechar)) !== FALSE) {
  81. if(substr($data[0],0,1) == "!"){
  82. $tables[] = array($tmp,$tmpheaders,$rows);
  83. $rows = array();
  84. $tmp = substr($data[0],1);
  85. if($fields){
  86. $tmpheaders = array();
  87. $num = count($fields);
  88. for ($c=0; $c < $num; $c++) {
  89. $tmpheaders[] = $data[$fields[$c]];
  90. }
  91. }else{
  92. array_shift($data);
  93. $tmpheaders = $data;
  94. //print_r($tmpheaders);
  95. //die();
  96. }
  97. }else{
  98. if($fields){
  99. $num = count($fields);
  100. $obj = array();
  101. for ($c=0; $c < $num; $c++) {
  102. $obj[] = $data[$fields[$c]];
  103. }
  104. $rows[] = $obj;
  105. }else{
  106. $num = count($data);
  107. $diff = count($tmpheaders) < $num ? $num - count($tmpheaders) : 0;
  108. $obj = array();
  109. for ($c=0; $c < $num; $c++) {
  110. $obj[] = $data[$c+$diff];
  111. }
  112. $rows[] = $obj;
  113. }
  114. }
  115. }
  116. if($headers){
  117. $tmpheaders = explode(",",$headers);
  118. }else{
  119. if(count($tmpheaders) == 0){
  120. $tmpheaders = array_shift($rows);
  121. }
  122. }
  123. $tables[] = array($tmp,$tmpheaders,$rows);
  124. if(count($tables)>1){
  125. array_shift($tables);
  126. }
  127. fclose($handle);
  128. $sa = "";
  129. $index = 0;
  130. $menu = "";
  131. foreach($tables as $table){
  132. $title = $table[0];
  133. $tt = explode("\n",$title);
  134. if(count($tt)>1){
  135. $title = array_shift($tt);
  136. }
  137. $menu .= '<li><a href="#'.$title.'">'.$title.'</a></li>';
  138. }
  139. $sa .= '<ul id="'.$tid.'_menu" class="csvelement anchormenu">'.$menu.'</ul>';
  140. foreach($tables as $table){
  141. $index ++;
  142. $title = $table[0];
  143. $heads = $table[1];
  144. $rows = $table[2];
  145. $hhe = "";
  146. $tt = explode("\n",$title);
  147. if(count($tt)>1){
  148. $title = array_shift($tt);
  149. $hhe .= '<h3>'.$title.'</a></h3>';
  150. $hhe .= '<p>'.implode("<br>",$tt).'</p>';
  151. }else{
  152. $hhe .= '<h3>'.$title.'</h3>';
  153. }
  154. $sa .= '<a name="'.$title.'" class="anchor">&nbsp;</a>';
  155. $sa .= '<div id="'.$tid.'_'.$index.'" class="csvelement '.$tid.'">';
  156. $sa .= $hhe;
  157. $sa .= '<table class="'.$class.'">';
  158. $sa .= '<thead><tr>';
  159. $cols = count($heads);
  160. for($y=0;$y<$cols;$y++){
  161. $sa .= '<th class="col'.$y.'">';
  162. $sa .= $heads[$y];
  163. $sa .= '</th>';
  164. }
  165. $sa .= '</tr></thead><tbody>';
  166. for($i=0,$n=count($rows);$i<$n;$i++){
  167. $row = $rows[$i];
  168. if(strlen(join("",$row))==0){
  169. }else{
  170. $sa .= '<tr>';
  171. for($y=0;$y<$cols;$y++){
  172. $sa .= '<td class="col'.$y.'">';
  173. $tt = explode("\n",$row[$y]);
  174. if(count($tt)>1){
  175. $sa .= '<strong>'.array_shift($tt).'</strong><br>';
  176. }
  177. $sa .= implode("<br>",$tt);
  178. $sa .= '</td>';
  179. }
  180. $sa .= '</tr>';
  181. }
  182. }
  183. $sa .= '</tbody></table><div class="totoplink"><a href="#TOP">Top</a></div></div>';
  184. }
  185. /*file_put_contents($cachefn,$sa);*/
  186. return $sa;
  187. }
  188. function help(){
  189. ?>
  190. [csv filename="" read="" delimiter="," quotechar='"' headers=false fields=false id="mytable" class="tablesorter"] <br>
  191. < a href='test.csv' >Title < /a >
  192. <?php
  193. }
  194. function admin_line(){
  195. ?>
  196. <?php
  197. }
  198. }
  199. global $plug_shortcodes;
  200. $plug_shortcodes = new plug_shortcodes($this);
  201. $this->dagsopt['plug_shortcodes'] = $plug_shortcodes;
  202. }