/wp-content/plugins/quotes-collection/quotes-collection-shortcodes.php

https://github.com/Canuckaholic/Pop-Digital · PHP · 185 lines · 138 code · 44 blank · 3 comment · 50 complexity · 829e40fce79e8127e9c0c028baade9c2 MD5 · raw file

  1. <?php
  2. function quotescollection_shortcode_output_format($quotes)
  3. {
  4. $display = "";
  5. foreach($quotes as $quote_data) {
  6. $display .= "<blockquote class=\"quotescollection\" id=\"quote-".$quote_data['quote_id']."\">";
  7. $display .= quotescollection_output_format( $quote_data );
  8. $display .= "</blockquote>\n";
  9. }
  10. return apply_filters( 'quotescollection_shortcode_output_format', $display );
  11. }
  12. function quotescollection_shortcodes($atts = array())
  13. {
  14. extract( shortcode_atts( array(
  15. 'limit' => 0,
  16. 'id' => 0,
  17. 'author' => '',
  18. 'source' => '',
  19. 'tags' => '',
  20. 'orderby' => 'quote_id',
  21. 'order' => 'ASC',
  22. 'paging' => false,
  23. 'limit_per_page' => 10
  24. ), $atts ) );
  25. $condition = " WHERE public = 'yes'";
  26. if(isset($quote_id) && is_numeric($quote_id)) $id = $quote_id;
  27. if($id && is_numeric($id)) {
  28. $condition .= " AND quote_id = ".$id;
  29. if ($quote = quotescollection_get_quotes($condition))
  30. return quotescollection_shortcode_output_format($quote);
  31. else
  32. return "";
  33. }
  34. if($author)
  35. $condition .= " AND author = '".$author."'";
  36. if($source)
  37. $condition .= " AND source = '".$source."'";
  38. if ($tags) {
  39. $tags = html_entity_decode($tags);
  40. if(!$tags)
  41. break;
  42. $taglist = explode(',', $tags);
  43. $tags_condition = "";
  44. foreach($taglist as $tag) {
  45. $tag = trim($tag);
  46. if($tags_condition) $tags_condition .= " OR ";
  47. $tags_condition .= "tags = '{$tag}' OR tags LIKE '{$tag},%' OR tags LIKE '%,{$tag},%' OR tags LIKE '%,{$tag}'";
  48. }
  49. if($tags_condition) $condition .= " AND ".$tags_condition;
  50. }
  51. if($orderby == 'id' || !$orderby) $orderby = 'quote_id';
  52. else if ($orderby == 'date_added') $orderby = 'time_added';
  53. else if($orderby == 'random' || $orderby == 'rand') {
  54. $orderby = 'RAND(UNIX_TIMESTAMP(NOW()))';
  55. $order = '';
  56. $paging = false;
  57. };
  58. $order = strtoupper($order);
  59. if($order && $order != 'DESC')
  60. $order = 'ASC';
  61. $condition .= " ORDER BY {$orderby} {$order}";
  62. if($paging == true || $paging == 1) {
  63. $num_quotes = quotescollection_count($condition);
  64. $total_pages = ceil($num_quotes / $limit_per_page);
  65. if(!isset($_GET['quotes_page']) || !$_GET['quotes_page'] || !is_numeric($_GET['quotes_page']))
  66. $page = 1;
  67. else
  68. $page = $_GET['quotes_page'];
  69. if($page > $total_pages) $page = $total_pages;
  70. if($page_nav = quotescollection_pagenav($total_pages, $page, 0, 'quotes_page'))
  71. $page_nav = '<div class="quotescollection_pagenav">'.$page_nav.'</div>';
  72. $start = ($page - 1) * $limit_per_page;
  73. $condition .= " LIMIT {$start}, {$limit_per_page}";
  74. // return $condition;
  75. if($quotes = quotescollection_get_quotes($condition))
  76. return $page_nav.quotescollection_shortcode_output_format($quotes).$page_nav;
  77. else
  78. return "";
  79. }
  80. else if($limit && is_numeric($limit))
  81. $condition .= " LIMIT ".$limit;
  82. // return $condition;
  83. if($quotes = quotescollection_get_quotes($condition))
  84. return quotescollection_shortcode_output_format($quotes);
  85. else
  86. return "";
  87. }
  88. add_shortcode('quotescollection', 'quotescollection_shortcodes');
  89. add_shortcode('quotcoll', 'quotescollection_shortcodes');
  90. add_shortcode('quotecoll', 'quotescollection_shortcodes'); // just in case, somebody misspells the shortcode
  91. /* Backward compatibility for [quote] */
  92. function quotescollection_displayquote($matches)
  93. {
  94. if(!isset($matches[1]) || (isset($matches[1]) && !$matches[1]) || $matches[0] == "[quote|random]")
  95. $atts = array( 'orderby' => 'random', 'limit' => 1 );
  96. else
  97. $atts = array ( 'id' => $matches[1] );
  98. return quotescollection_shortcodes($atts);
  99. }
  100. function quotescollection_displayquotes_author($matches)
  101. {
  102. return quotescollection_shortcodes(array('author'=>$matches[1]));
  103. }
  104. function quotescollection_displayquotes_source($matches)
  105. {
  106. return quotescollection_shortcodes(array('source'=>$matches[1]));
  107. }
  108. function quotescollection_displayquotes_tags($matches)
  109. {
  110. return quotescollection_shortcodes(array('tags'=>$matches[1]));
  111. }
  112. function quotescollection_inpost( $text )
  113. {
  114. $start = strpos($text,"[quote|id=");
  115. if ($start !== FALSE) {
  116. $text = preg_replace_callback( "/\[quote\|id=(\d+)\]/i", "quotescollection_displayquote", $text );
  117. }
  118. $start = strpos($text,"[quote|random]");
  119. if ($start !== FALSE) {
  120. $text = preg_replace_callback( "/\[quote\|random\]/i", "quotescollection_displayquote", $text );
  121. }
  122. $start = strpos($text,"[quote|all]");
  123. if ($start !== FALSE) {
  124. $text = preg_replace_callback( "/\[quote\|all\]/i", "quotescollection_shortcodes", $text );
  125. }
  126. $start = strpos($text,"[quote|author=");
  127. if($start !== FALSE) {
  128. $text = preg_replace_callback("/\[quote\|author=(.{1,})?\]/i", "quotescollection_displayquotes_author", $text);
  129. }
  130. $start = strpos($text,"[quote|source=");
  131. if($start !== FALSE) {
  132. $text = preg_replace_callback("/\[quote\|source=(.{1,})?\]/i", "quotescollection_displayquotes_source", $text);
  133. }
  134. $start = strpos($text,"[quote|tags=");
  135. if($start !== FALSE) {
  136. $text = preg_replace_callback("/\[quote\|tags=(.{1,})?\]/i", "quotescollection_displayquotes_tags", $text);
  137. } return $text;
  138. }
  139. add_filter('the_content', 'quotescollection_inpost', 7);
  140. add_filter('the_excerpt', 'quotescollection_inpost', 7);
  141. ?>