plug_shortcodes.php 7.9 KB

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