plug_shortcodes.php 15 KB

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