title = __("Misc Shortcodes","dagsopt"); $this->pluginname = $ns->pluginname; $this->file = $ns->file; $this->ns = $ns; } function start() { add_filter('the_content', array(&$this,'CSV_Parse')); add_filter('the_content', array(&$this,'CSV_Parse2')); add_shortcode( 'acc-page', array(&$this,'getpage') ); } function getpage( $atts ) { extract( shortcode_atts( array( 'id' => 0 ), $atts ) ); $s = ""; if($id!==0){ $page = get_page( $id ); $s .= '
'; $s .= '
'.$page->post_title.'
'; $s .= '
'; $s .= apply_filters('the_content',$page->post_content); $s .= '
'; $s .= '
'; } return $s; } function CSV_Parse2($content) { $content = preg_replace_callback("/\(.*)\<\/a\>/i", array(&$this,"CSV_Render2"), $content); return $content; } function CSV_Render2($matches) { //0 full 1 filename 2 attributes 3 title $filen = explode("/",$matches[1]); $class = array_pop($filen); $filen[] = $class; $filen = implode("/",$filen).".csv"; $arguments = array(); $arguments['filename'] = $filen; $arguments['class'] = "tablesorter ".$class; $arguments['id'] = "".$class; $arguments['title'] = "".$matches[3]; return $this->CSV_Render_($arguments); } function CSV_Render($matches) { $matches[1] = str_replace(array('”','″'), '', $matches[1]); preg_match_all('/(\w*)=(.*?)" /i', $matches[1], $attributes); $arguments = array(); foreach ( (array) $attributes[1] as $key => $value ) { $arguments[$value] = str_replace('"', '', $attributes[2][$key]); } if(!isset($arguments['filename']) || !is_file(ABSPATH.$arguments['filename'])){ return "csv error: filename ".ABSPATH.$arguments['filename']." not readable"; } return $this->CSV_Render_($arguments); } function CSV_Parse($content) { $content = preg_replace_callback("/\[\[csv ([^]]*)\/\]\]/i", array(&$this,"CSV_Render"), $content); return $content; } function CSV_Render_($arguments) { if(!isset($arguments['filename']) || !is_file(ABSPATH.$arguments['filename'])){ return "csv error: filename ".ABSPATH.$arguments['filename']." not readable"; } $maxread = isset($arguments['read']) ? $arguments['read'] : (1024*1024*5); $delimiter = isset($arguments['delimiter']) ? $arguments['delimiter'] : ','; $quotechar = isset($arguments['quotechar']) ? $arguments['quotechar'] : '"'; $headers = isset($arguments['headers']) ? $arguments['headers'] : false; $fields = isset($arguments['fields']) ? explode(",",$arguments['fields']) : false; $tid = isset($arguments['id']) ? $arguments['id'] : 'mytable'; $class = isset($arguments['class']) ? $arguments['class'] : 'tablesorter'; $tables = array(); $rows = array(); $tmp = ""; $tmpheaders = array(); $handle = fopen(ABSPATH.$arguments['filename'], "r"); while (($data = fgetcsv($handle, $maxread, $delimiter,$quotechar)) !== FALSE) { if(substr($data[0],0,1) == "!"){ $tables[] = array($tmp,$tmpheaders,$rows); $rows = array(); $tmp = substr($data[0],1); if($fields){ $tmpheaders = array(); $num = count($fields); for ($c=0; $c < $num; $c++) { $tmpheaders[] = $data[$fields[$c]]; } }else{ array_shift($data); $tmpheaders = $data; //print_r($tmpheaders); //die(); } }else{ if($fields){ $num = count($fields); $obj = array(); for ($c=0; $c < $num; $c++) { $obj[] = $data[$fields[$c]]; } $rows[] = $obj; }else{ $num = count($data); $diff = count($tmpheaders) < $num ? $num - count($tmpheaders) : 0; $obj = array(); for ($c=0; $c < $num; $c++) { $obj[] = $data[$c+$diff]; } $rows[] = $obj; } } } if($headers){ $tmpheaders = explode(",",$headers); }else{ if(count($tmpheaders) == 0){ $tmpheaders = array_shift($rows); } } $tables[] = array($tmp,$tmpheaders,$rows); if(count($tables)>1){ array_shift($tables); } fclose($handle); $sa = ""; $index = 0; $menu = ""; foreach($tables as $table){ $title = $table[0]; $tt = explode("\n",$title); if(count($tt)>1){ $title = array_shift($tt); } $menu .= '
  • '.$title.'
  • '; } $sa .= ''; foreach($tables as $table){ $index ++; $title = $table[0]; $heads = $table[1]; $rows = $table[2]; $hhe = ""; $tt = explode("\n",$title); if(count($tt)>1){ $title = array_shift($tt); $hhe .= '

    '.$title.'

    '; $hhe .= '

    '.implode("
    ",$tt).'

    '; }else{ $hhe .= '

    '.$title.'

    '; } $sa .= ' '; $sa .= '
    '; $sa .= $hhe; $sa .= ''; $sa .= ''; $cols = count($heads); for($y=0;$y<$cols;$y++){ $sa .= ''; } $sa .= ''; for($i=0,$n=count($rows);$i<$n;$i++){ $row = $rows[$i]; if(strlen(join("",$row))==0){ }else{ $sa .= ''; for($y=0;$y<$cols;$y++){ $sa .= ''; } $sa .= ''; } } $sa .= '
    '; $sa .= $heads[$y]; $sa .= '
    '; $tt = explode("\n",$row[$y]); if(count($tt)>1){ $sa .= ''.array_shift($tt).'
    '; } $sa .= implode("
    ",$tt); $sa .= '
    '; } /*file_put_contents($cachefn,$sa);*/ return $sa; } function help(){ ?> [csv filename="" read="" delimiter="," quotechar='"' headers=false fields=false id="mytable" class="tablesorter"]
    < a href='test.csv' >Title < /a > dagsopt['plug_shortcodes'] = $plug_shortcodes; }