plug_shortcodes.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. if (!defined('DOM_SLUG')) {
  3. define('DOM_SLUG', str_replace(".", "-", $_SERVER['HTTP_HOST']));
  4. }
  5. if (!class_exists("plug_shortcodes")) {
  6. class plug_shortcodes {
  7. function __construct($ns) {
  8. $this->title = __("Misc Shortcodes", "dagsopt");
  9. $this->pluginname = $ns->pluginname;
  10. $this->file = $ns->file;
  11. $this->ns = $ns;
  12. }
  13. function start() {
  14. add_filter('the_content', array(&$this, 'CSV_Parse'));
  15. add_filter('the_content', array(&$this, 'CSV_Parse2'));
  16. add_shortcode('acc-page', array(&$this, 'getpage'));
  17. add_shortcode('posts', array(&$this, 'r_get_posts'));
  18. add_shortcode('domposts', array(&$this, 'r_get_dom_posts'));
  19. }
  20. function r_get_dom_posts($atts, $content = null) {
  21. extract(shortcode_atts(array(
  22. "num" => '5',
  23. "cat" => '',
  24. "tpl" => 'default',
  25. "wrap" => 'box',
  26. ), $atts));
  27. global $post, $r_get_posts_excluded, $r_get_posts_cat_collect;
  28. $oldpost = $post;
  29. if (defined("DOM_SLUG")) {
  30. $caten = get_category_by_slug(DOM_SLUG);
  31. } else {
  32. $caten = get_category_by_slug($cat);
  33. }
  34. $ret = '';
  35. if ($caten) {
  36. $catid = $caten->cat_ID;
  37. } else {
  38. $catid = 3;
  39. }
  40. $r_get_posts_cat_collect[] = $catid;
  41. $myposts = get_posts('numberposts=' . $num . '&order=DESC&orderby=post_date&category=' . $catid . "&exclude=" . implode(",", $r_get_posts_excluded));
  42. ob_start();
  43. ob_clean();
  44. foreach ($myposts as $post):
  45. $r_get_posts_excluded[] = $post->ID;
  46. setup_postdata($post);
  47. if (file_exists(TEMPLATEPATH . '/tpl_' . $tpl . '.php')) {
  48. include TEMPLATEPATH . '/tpl_' . $tpl . '.php';
  49. } else {
  50. include TEMPLATEPATH . '/tpl_default.php';
  51. }
  52. endforeach;
  53. if (isset($_REQUEST['debug'])) {
  54. echo ("atts:" . print_r($atts, true) . "\n");
  55. echo ("CAT:" . $cat . "\n");
  56. echo ("caten:" . print_r($caten, true) . "\n");
  57. echo ("posts:" . print_r($myposts, true) . "\n");
  58. echo ("r_get_posts_cat_collect:" . print_r($r_get_posts_cat_collect, true) . "\n");
  59. echo ("r_get_posts_excluded:" . print_r($r_get_posts_excluded, true) . "\n");
  60. }
  61. $ret = ob_get_contents();
  62. ob_clean();
  63. $ret .= '';
  64. $post = $oldpost;
  65. return $ret;
  66. }
  67. function r_get_posts($atts, $content = null) {
  68. extract(shortcode_atts(array(
  69. "num" => '5',
  70. "cat" => '',
  71. "tpl" => '1big',
  72. "wrap" => 'box',
  73. ), $atts));
  74. global $post, $r_get_posts_excluded, $r_get_posts_cat_collect;
  75. $oldpost = $post;
  76. $caten = get_category_by_slug($cat);
  77. $ret = '';
  78. if ($caten) {
  79. $catid = $caten->cat_ID;
  80. } else {
  81. $catid = 3;
  82. }
  83. $r_get_posts_cat_collect[] = $catid;
  84. $myposts = get_posts('numberposts=' . $num . '&order=DESC&orderby=post_date&category=' . $catid . "&exclude=" . implode(",", $r_get_posts_excluded));
  85. ob_start();
  86. ob_clean();
  87. foreach ($myposts as $post):
  88. $r_get_posts_excluded[] = $post->ID;
  89. setup_postdata($post);
  90. if (file_exists(TEMPLATEPATH . '/tpl_' . $tpl . '.php')) {
  91. include TEMPLATEPATH . '/tpl_' . $tpl . '.php';
  92. } else {
  93. include TEMPLATEPATH . '/tpl_default.php';
  94. }
  95. endforeach;
  96. if (isset($_REQUEST['debug'])) {
  97. echo ("atts:" . print_r($atts, true) . "\n");
  98. echo ("CAT:" . $cat . "\n");
  99. echo ("caten:" . print_r($caten, true) . "\n");
  100. echo ("posts:" . print_r($myposts, true) . "\n");
  101. echo ("r_get_posts_cat_collect:" . print_r($r_get_posts_cat_collect, true) . "\n");
  102. echo ("r_get_posts_excluded:" . print_r($r_get_posts_excluded, true) . "\n");
  103. }
  104. $ret = ob_get_contents();
  105. ob_clean();
  106. $ret .= '';
  107. $post = $oldpost;
  108. return $ret;
  109. }
  110. function getpage($atts) {
  111. extract(shortcode_atts(array(
  112. 'id' => 0,
  113. ), $atts));
  114. $s = "";
  115. if ($id !== 0) {
  116. $page = get_page($id);
  117. $s .= '<div class="hbox embed-page page-id-' . $id . '" >';
  118. $s .= '<div class="arrow">' . $page->post_title . '</div>';
  119. $s .= '<div class="box">';
  120. $s .= apply_filters('the_content', $page->post_content);
  121. $s .= '</div>';
  122. $s .= '</div>';
  123. }
  124. return $s;
  125. }
  126. function CSV_Parse2($content) {
  127. $content = preg_replace_callback("/\<a href=\'(.*).csv\'(.*)\>(.*)\<\/a\>/i", array(&$this, "CSV_Render2"), $content);
  128. return $content;
  129. }
  130. function CSV_Render2($matches) {
  131. //0 full 1 filename 2 attributes 3 title
  132. $filen = explode("/", $matches[1]);
  133. $class = array_pop($filen);
  134. $filen[] = $class;
  135. $filen = implode("/", $filen) . ".csv";
  136. $arguments = array();
  137. $arguments['filename'] = $filen;
  138. $arguments['class'] = "tablesorter " . $class;
  139. $arguments['id'] = "" . $class;
  140. $arguments['title'] = "" . $matches[3];
  141. return $this->CSV_Render_($arguments);
  142. }
  143. function CSV_Render($matches) {
  144. $matches[1] = str_replace(array('&#8221;', '&#8243;'), '', $matches[1]);
  145. preg_match_all('/(\w*)=(.*?)" /i', $matches[1], $attributes);
  146. $arguments = array();
  147. foreach ((array) $attributes[1] as $key => $value) {
  148. $arguments[$value] = str_replace('"', '', $attributes[2][$key]);
  149. }
  150. if (!isset($arguments['filename']) || !is_file(ABSPATH . $arguments['filename'])) {
  151. return "csv error: filename " . ABSPATH . $arguments['filename'] . " not readable";
  152. }
  153. return $this->CSV_Render_($arguments);
  154. }
  155. function CSV_Parse($content) {
  156. $content = preg_replace_callback("/\[\[csv ([^]]*)\/\]\]/i", array(&$this, "CSV_Render"), $content);
  157. return $content;
  158. }
  159. function CSV_Render_($arguments) {
  160. if (!isset($arguments['filename']) || !is_file(ABSPATH . $arguments['filename'])) {
  161. return "csv error: filename " . ABSPATH . $arguments['filename'] . " not readable";
  162. }
  163. $maxread = isset($arguments['read']) ? $arguments['read'] : (1024 * 1024 * 5);
  164. $delimiter = isset($arguments['delimiter']) ? $arguments['delimiter'] : ',';
  165. $quotechar = isset($arguments['quotechar']) ? $arguments['quotechar'] : '"';
  166. $headers = isset($arguments['headers']) ? $arguments['headers'] : false;
  167. $fields = isset($arguments['fields']) ? explode(",", $arguments['fields']) : false;
  168. $tid = isset($arguments['id']) ? $arguments['id'] : 'mytable';
  169. $class = isset($arguments['class']) ? $arguments['class'] : 'tablesorter';
  170. $tables = array();
  171. $rows = array();
  172. $tmp = "";
  173. $tmpheaders = array();
  174. $handle = fopen(ABSPATH . $arguments['filename'], "r");
  175. while (($data = fgetcsv($handle, $maxread, $delimiter, $quotechar)) !== FALSE) {
  176. if (substr($data[0], 0, 1) == "!") {
  177. $tables[] = array($tmp, $tmpheaders, $rows);
  178. $rows = array();
  179. $tmp = substr($data[0], 1);
  180. if ($fields) {
  181. $tmpheaders = array();
  182. $num = count($fields);
  183. for ($c = 0; $c < $num; $c++) {
  184. $tmpheaders[] = $data[$fields[$c]];
  185. }
  186. } else {
  187. array_shift($data);
  188. $tmpheaders = $data;
  189. //print_r($tmpheaders);
  190. //die();
  191. }
  192. } else {
  193. if ($fields) {
  194. $num = count($fields);
  195. $obj = array();
  196. for ($c = 0; $c < $num; $c++) {
  197. $obj[] = $data[$fields[$c]];
  198. }
  199. $rows[] = $obj;
  200. } else {
  201. $num = count($data);
  202. $diff = count($tmpheaders) < $num ? $num - count($tmpheaders) : 0;
  203. $obj = array();
  204. for ($c = 0; $c < $num; $c++) {
  205. $obj[] = $data[$c + $diff];
  206. }
  207. $rows[] = $obj;
  208. }
  209. }
  210. }
  211. if ($headers) {
  212. $tmpheaders = explode(",", $headers);
  213. } else {
  214. if (count($tmpheaders) == 0) {
  215. $tmpheaders = array_shift($rows);
  216. }
  217. }
  218. $tables[] = array($tmp, $tmpheaders, $rows);
  219. if (count($tables) > 1) {
  220. array_shift($tables);
  221. }
  222. fclose($handle);
  223. $sa = "";
  224. $index = 0;
  225. $menu = "";
  226. foreach ($tables as $table) {
  227. $title = $table[0];
  228. $tt = explode("\n", $title);
  229. if (count($tt) > 1) {
  230. $title = array_shift($tt);
  231. }
  232. $menu .= '<li><a href="#' . $title . '">' . $title . '</a></li>';
  233. }
  234. $sa .= '<ul id="' . $tid . '_menu" class="csvelement anchormenu">' . $menu . '</ul>';
  235. foreach ($tables as $table) {
  236. $index++;
  237. $title = $table[0];
  238. $heads = $table[1];
  239. $rows = $table[2];
  240. $hhe = "";
  241. $tt = explode("\n", $title);
  242. if (count($tt) > 1) {
  243. $title = array_shift($tt);
  244. $hhe .= '<h3>' . $title . '</a></h3>';
  245. $hhe .= '<p>' . implode("<br>", $tt) . '</p>';
  246. } else {
  247. $hhe .= '<h3>' . $title . '</h3>';
  248. }
  249. $sa .= '<a name="' . $title . '" class="anchor">&nbsp;</a>';
  250. $sa .= '<div id="' . $tid . '_' . $index . '" class="csvelement ' . $tid . '">';
  251. $sa .= $hhe;
  252. $sa .= '<table class="' . $class . '">';
  253. $sa .= '<thead><tr>';
  254. $cols = count($heads);
  255. for ($y = 0; $y < $cols; $y++) {
  256. $sa .= '<th class="col' . $y . '">';
  257. $sa .= $heads[$y];
  258. $sa .= '</th>';
  259. }
  260. $sa .= '</tr></thead><tbody>';
  261. for ($i = 0, $n = count($rows); $i < $n; $i++) {
  262. $row = $rows[$i];
  263. if (strlen(join("", $row)) == 0) {
  264. } else {
  265. $sa .= '<tr>';
  266. for ($y = 0; $y < $cols; $y++) {
  267. $sa .= '<td class="col' . $y . '">';
  268. $tt = explode("\n", $row[$y]);
  269. if (count($tt) > 1) {
  270. $sa .= '<strong>' . array_shift($tt) . '</strong><br>';
  271. }
  272. $sa .= implode("<br>", $tt);
  273. $sa .= '</td>';
  274. }
  275. $sa .= '</tr>';
  276. }
  277. }
  278. $sa .= '</tbody></table><div class="totoplink"><a href="#TOP">Top</a></div></div>';
  279. }
  280. /*file_put_contents($cachefn,$sa);*/
  281. return $sa;
  282. }
  283. function help() {
  284. ?>
  285. [csv filename="" read="" delimiter="," quotechar='"' headers=false fields=false id="mytable" class="tablesorter"] <br>
  286. < a href='test.csv' >Title < /a >
  287. <?php
  288. }
  289. function admin_line() {
  290. ?>
  291. <?php
  292. }
  293. }
  294. global $plug_shortcodes, $r_get_posts_excluded, $r_get_posts_cat_collect;
  295. $r_get_posts_excluded = array();
  296. $r_get_posts_cat_collect = array();
  297. $plug_shortcodes = new plug_shortcodes($this);
  298. $this->dagsopt['plug_shortcodes'] = $plug_shortcodes;
  299. }
  300. if (!function_exists("post_image_")) {
  301. function post_image_($postid, $format = "thumbnail") {
  302. global $wpdb;
  303. $image = array();
  304. if (isset($postid)) {
  305. $post_image_attachments = @$wpdb->get_results("SELECT ID, post_title, post_excerpt, post_content FROM {$wpdb->posts} WHERE post_parent = '{$postid}' AND post_type = 'attachment' AND post_mime_type LIKE '%image%' ORDER BY ID ASC LIMIT 1");
  306. foreach ($post_image_attachments as $attachment) {
  307. $url = get_attachment_link($attachment->ID);
  308. $id = $attachment->ID;
  309. $img_title = apply_filters('the_title', $attachment->post_title);
  310. $srcc = wp_get_attachment_image_src($attachment->ID, $format);
  311. if (defined("WP_SITEURL_SEC")) {
  312. $srcc[0] = str_replace(WP_SITEURL_SEC, WP_SITEURL_CDN, $srcc[0]);
  313. }
  314. $image = array('title' => $img_title, 'id' => $id, 'url' => $url, 'src' => $srcc, 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content);
  315. }
  316. }
  317. return $image;
  318. }
  319. }
  320. if (!function_exists("post_badge")) {
  321. function post_badge() {
  322. echo '<div class="badge_buy"></div>';
  323. return false;
  324. if (is_restricted()) {
  325. echo '<div class="badge_buy"><a href="/abonnement/">Køb abonnement</a></div>';
  326. } else {
  327. if (in_category('492') && !(in_category('9') || in_category(125))) {
  328. echo '<div class="badge_member"><a href="/wp-admin" style="float: left; margin-left: -100px; background-color: #777777;">Perspektiv</a></div>';
  329. } else {
  330. if (in_category('9') || in_category(125)) {
  331. if (in_category('492')) {
  332. echo '<div class="badge_member"><a href="/wp-admin" style="float: left; margin-left: -100px; background-color: #777777;">Perspektiv</a> <a href="/wp-admin">Kun for abonnenter</a></div>';
  333. } else {
  334. echo '<div class="badge_member"><a href="/wp-admin">Kun for abonnenter</a></div>';
  335. }
  336. }
  337. }
  338. }
  339. }
  340. }