plug_shortcodes.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. if (!class_exists("plug_shortcodes")) {
  3. class plug_shortcodes {
  4. function __construct($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. add_shortcode('posts', array(&$this, 'r_get_posts'));
  15. }
  16. function r_get_posts($atts, $content = null) {
  17. extract(shortcode_atts(array(
  18. "num" => '5',
  19. "cat" => '',
  20. "tpl" => '1big',
  21. "wrap" => 'box',
  22. ), $atts));
  23. global $post, $r_get_posts_excluded, $r_get_posts_cat_collect;
  24. $oldpost = $post;
  25. $caten = get_category_by_slug($cat);
  26. $ret = '';
  27. if ($caten) {
  28. $catid = $caten->cat_ID;
  29. } else {
  30. $catid = 3;
  31. }
  32. $r_get_posts_cat_collect[] = $catid;
  33. $myposts = get_posts('numberposts=' . $num . '&order=DESC&orderby=post_date&category=' . $catid . "&exclude=" . implode(",", $r_get_posts_excluded));
  34. ob_start();
  35. ob_clean();
  36. foreach ($myposts as $post):
  37. $r_get_posts_excluded[] = $post->ID;
  38. setup_postdata($post);
  39. if (file_exists(TEMPLATEPATH . '/tpl_' . $tpl . '.php')) {
  40. include TEMPLATEPATH . '/tpl_' . $tpl . '.php';
  41. }
  42. endforeach;
  43. if (isset($_REQUEST['debug'])) {
  44. echo ("atts:" . print_r($atts, true) . "\n");
  45. echo ("CAT:" . $cat . "\n");
  46. echo ("caten:" . print_r($caten, true) . "\n");
  47. echo ("posts:" . print_r($myposts, true) . "\n");
  48. echo ("r_get_posts_cat_collect:" . print_r($r_get_posts_cat_collect, true) . "\n");
  49. echo ("r_get_posts_excluded:" . print_r($r_get_posts_excluded, true) . "\n");
  50. }
  51. $ret = ob_get_contents();
  52. ob_clean();
  53. $ret .= '';
  54. $post = $oldpost;
  55. return $ret;
  56. }
  57. function getpage($atts) {
  58. extract(shortcode_atts(array(
  59. 'id' => 0,
  60. ), $atts));
  61. $s = "";
  62. if ($id !== 0) {
  63. $page = get_page($id);
  64. $s .= '<div class="hbox embed-page page-id-' . $id . '" >';
  65. $s .= '<div class="arrow">' . $page->post_title . '</div>';
  66. $s .= '<div class="box">';
  67. $s .= apply_filters('the_content', $page->post_content);
  68. $s .= '</div>';
  69. $s .= '</div>';
  70. }
  71. return $s;
  72. }
  73. function CSV_Parse2($content) {
  74. $content = preg_replace_callback("/\<a href=\'(.*).csv\'(.*)\>(.*)\<\/a\>/i", array(&$this, "CSV_Render2"), $content);
  75. return $content;
  76. }
  77. function CSV_Render2($matches) {
  78. //0 full 1 filename 2 attributes 3 title
  79. $filen = explode("/", $matches[1]);
  80. $class = array_pop($filen);
  81. $filen[] = $class;
  82. $filen = implode("/", $filen) . ".csv";
  83. $arguments = array();
  84. $arguments['filename'] = $filen;
  85. $arguments['class'] = "tablesorter " . $class;
  86. $arguments['id'] = "" . $class;
  87. $arguments['title'] = "" . $matches[3];
  88. return $this->CSV_Render_($arguments);
  89. }
  90. function CSV_Render($matches) {
  91. $matches[1] = str_replace(array('&#8221;', '&#8243;'), '', $matches[1]);
  92. preg_match_all('/(\w*)=(.*?)" /i', $matches[1], $attributes);
  93. $arguments = array();
  94. foreach ((array) $attributes[1] as $key => $value) {
  95. $arguments[$value] = str_replace('"', '', $attributes[2][$key]);
  96. }
  97. if (!isset($arguments['filename']) || !is_file(ABSPATH . $arguments['filename'])) {
  98. return "csv error: filename " . ABSPATH . $arguments['filename'] . " not readable";
  99. }
  100. return $this->CSV_Render_($arguments);
  101. }
  102. function CSV_Parse($content) {
  103. $content = preg_replace_callback("/\[\[csv ([^]]*)\/\]\]/i", array(&$this, "CSV_Render"), $content);
  104. return $content;
  105. }
  106. function CSV_Render_($arguments) {
  107. if (!isset($arguments['filename']) || !is_file(ABSPATH . $arguments['filename'])) {
  108. return "csv error: filename " . ABSPATH . $arguments['filename'] . " not readable";
  109. }
  110. $maxread = isset($arguments['read']) ? $arguments['read'] : (1024 * 1024 * 5);
  111. $delimiter = isset($arguments['delimiter']) ? $arguments['delimiter'] : ',';
  112. $quotechar = isset($arguments['quotechar']) ? $arguments['quotechar'] : '"';
  113. $headers = isset($arguments['headers']) ? $arguments['headers'] : false;
  114. $fields = isset($arguments['fields']) ? explode(",", $arguments['fields']) : false;
  115. $tid = isset($arguments['id']) ? $arguments['id'] : 'mytable';
  116. $class = isset($arguments['class']) ? $arguments['class'] : 'tablesorter';
  117. $tables = array();
  118. $rows = array();
  119. $tmp = "";
  120. $tmpheaders = array();
  121. $handle = fopen(ABSPATH . $arguments['filename'], "r");
  122. while (($data = fgetcsv($handle, $maxread, $delimiter, $quotechar)) !== FALSE) {
  123. if (substr($data[0], 0, 1) == "!") {
  124. $tables[] = array($tmp, $tmpheaders, $rows);
  125. $rows = array();
  126. $tmp = substr($data[0], 1);
  127. if ($fields) {
  128. $tmpheaders = array();
  129. $num = count($fields);
  130. for ($c = 0; $c < $num; $c++) {
  131. $tmpheaders[] = $data[$fields[$c]];
  132. }
  133. } else {
  134. array_shift($data);
  135. $tmpheaders = $data;
  136. //print_r($tmpheaders);
  137. //die();
  138. }
  139. } else {
  140. if ($fields) {
  141. $num = count($fields);
  142. $obj = array();
  143. for ($c = 0; $c < $num; $c++) {
  144. $obj[] = $data[$fields[$c]];
  145. }
  146. $rows[] = $obj;
  147. } else {
  148. $num = count($data);
  149. $diff = count($tmpheaders) < $num ? $num - count($tmpheaders) : 0;
  150. $obj = array();
  151. for ($c = 0; $c < $num; $c++) {
  152. $obj[] = $data[$c + $diff];
  153. }
  154. $rows[] = $obj;
  155. }
  156. }
  157. }
  158. if ($headers) {
  159. $tmpheaders = explode(",", $headers);
  160. } else {
  161. if (count($tmpheaders) == 0) {
  162. $tmpheaders = array_shift($rows);
  163. }
  164. }
  165. $tables[] = array($tmp, $tmpheaders, $rows);
  166. if (count($tables) > 1) {
  167. array_shift($tables);
  168. }
  169. fclose($handle);
  170. $sa = "";
  171. $index = 0;
  172. $menu = "";
  173. foreach ($tables as $table) {
  174. $title = $table[0];
  175. $tt = explode("\n", $title);
  176. if (count($tt) > 1) {
  177. $title = array_shift($tt);
  178. }
  179. $menu .= '<li><a href="#' . $title . '">' . $title . '</a></li>';
  180. }
  181. $sa .= '<ul id="' . $tid . '_menu" class="csvelement anchormenu">' . $menu . '</ul>';
  182. foreach ($tables as $table) {
  183. $index++;
  184. $title = $table[0];
  185. $heads = $table[1];
  186. $rows = $table[2];
  187. $hhe = "";
  188. $tt = explode("\n", $title);
  189. if (count($tt) > 1) {
  190. $title = array_shift($tt);
  191. $hhe .= '<h3>' . $title . '</a></h3>';
  192. $hhe .= '<p>' . implode("<br>", $tt) . '</p>';
  193. } else {
  194. $hhe .= '<h3>' . $title . '</h3>';
  195. }
  196. $sa .= '<a name="' . $title . '" class="anchor">&nbsp;</a>';
  197. $sa .= '<div id="' . $tid . '_' . $index . '" class="csvelement ' . $tid . '">';
  198. $sa .= $hhe;
  199. $sa .= '<table class="' . $class . '">';
  200. $sa .= '<thead><tr>';
  201. $cols = count($heads);
  202. for ($y = 0; $y < $cols; $y++) {
  203. $sa .= '<th class="col' . $y . '">';
  204. $sa .= $heads[$y];
  205. $sa .= '</th>';
  206. }
  207. $sa .= '</tr></thead><tbody>';
  208. for ($i = 0, $n = count($rows); $i < $n; $i++) {
  209. $row = $rows[$i];
  210. if (strlen(join("", $row)) == 0) {
  211. } else {
  212. $sa .= '<tr>';
  213. for ($y = 0; $y < $cols; $y++) {
  214. $sa .= '<td class="col' . $y . '">';
  215. $tt = explode("\n", $row[$y]);
  216. if (count($tt) > 1) {
  217. $sa .= '<strong>' . array_shift($tt) . '</strong><br>';
  218. }
  219. $sa .= implode("<br>", $tt);
  220. $sa .= '</td>';
  221. }
  222. $sa .= '</tr>';
  223. }
  224. }
  225. $sa .= '</tbody></table><div class="totoplink"><a href="#TOP">Top</a></div></div>';
  226. }
  227. /*file_put_contents($cachefn,$sa);*/
  228. return $sa;
  229. }
  230. function help() {
  231. ?>
  232. [csv filename="" read="" delimiter="," quotechar='"' headers=false fields=false id="mytable" class="tablesorter"] <br>
  233. < a href='test.csv' >Title < /a >
  234. <?php
  235. }
  236. function admin_line() {
  237. ?>
  238. <?php
  239. }
  240. }
  241. global $plug_shortcodes, $r_get_posts_excluded, $r_get_posts_cat_collect;
  242. $r_get_posts_excluded = array();
  243. $r_get_posts_cat_collect = array();
  244. $plug_shortcodes = new plug_shortcodes($this);
  245. $this->dagsopt['plug_shortcodes'] = $plug_shortcodes;
  246. }