Advertisement
Guest User

collapsing archive

a guest
Dec 4th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.32 KB | None | 0 0
  1. <?php
  2. /*
  3. Collapsing Archives version: 2.0.2
  4.  
  5. Copyright 2007-2010 Robert Felty
  6.  
  7. This file is part of Collapsing Archives
  8.  
  9.   Collapsing Archives is free software; you can redistribute it and/or modify
  10.   it under the terms of the GNU General Public License as published by
  11.   the Free Software Foundation; either version 2 of the License, or
  12.   (at your option) any later version.
  13.  
  14.   Collapsing Archives is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.   GNU General Public License for more details.
  18.  
  19.   You should have received a copy of the GNU General Public License
  20.   along with Collapsing Archives; if not, write to the Free Software
  21.   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  22.  
  23. */
  24. ?>
  25. <?php
  26. global $collapsArchItems;
  27. $collapsArchItems = array();
  28. function list_archives($options) {
  29.   global $wpdb, $month, $collapsArchItems;
  30.   extract($options);
  31.  
  32.   include('symbols.php');
  33.     $inExclusionsCat = array();
  34.     if ( !empty($inExcludeCat) && !empty($inExcludeCats) ) {
  35.         $exterms = preg_split('/[,]+/',$inExcludeCats);
  36.     if ($inExcludeCat=='include') {
  37.       $in='IN';
  38.     } else {
  39.       $in='NOT IN';
  40.     }
  41.         if ( count($exterms) ) {
  42.             foreach ( $exterms as $exterm ) {
  43.                 if (empty($inExclusionsCat))
  44.                     $inExclusionsCat = "'" . sanitize_title($exterm) . "'";
  45.                 else
  46.                     $inExclusionsCat .= ", '" . sanitize_title($exterm) . "' ";
  47.             }
  48.         }
  49.     }
  50.     if ( empty($inExclusionsCat) ) {
  51.         $inExcludeCatQuery = "";
  52.   } else {
  53.     $inExcludeCatQuery ="AND $wpdb->terms.slug $in ($inExclusionsCat)AND taxonomy = 'category'";
  54.   }
  55.     $inExclusionsYear = array();
  56.     if ( !empty($inExcludeYear) && !empty($inExcludeYears) ) {
  57.         $exterms = preg_split('/[,]+/',$inExcludeYears);
  58.     if ($inExcludeYear=='include') {
  59.       $in='IN';
  60.     } else {
  61.       $in='NOT IN';
  62.     }
  63.         if ( count($exterms) ) {
  64.             foreach ( $exterms as $exterm ) {
  65.                 if (empty($inExclusionsYear))
  66.                     $inExclusionsYear = "'" .$exterm . "'";
  67.                 else
  68.                     $inExclusionsYear .= ", '" . $exterm . "' ";
  69.             }
  70.         }
  71.     }
  72.     if ( empty($inExclusionsYear) ) {
  73.         $inExcludeYearQuery = "";
  74.   } else {
  75.     $inExcludeYearQuery ="AND YEAR($wpdb->posts.post_date) $in ($inExclusionsYear)";
  76.   }
  77.  
  78.   if (is_array($post_type)) {
  79.     $postTypeQuery="AND $wpdb->posts.post_type IN (";
  80.     foreach ($post_type as $type) {
  81.       $postTypeQuery .= "'$type',";
  82.     }
  83.     $postTypeQuery .= ')';
  84.   } else if ($post_type=='all') {
  85.     $postTypeQuery="AND $wpdb->posts.post_type NOT IN ('page', 'revision', 'nav_menu_item', 'attachment')";
  86.   } else {
  87.     $postTypeQuery="AND $wpdb->posts.post_type='$post_type'";
  88.   }
  89.     if ($defaultExpand!='') {
  90.         $autoExpand = preg_split('/,\s*/',$defaultExpand);
  91.   } else {
  92.       $autoExpand = array();
  93.   }
  94.  
  95.  
  96.   $postquery= "SELECT $wpdb->terms.slug, $wpdb->posts.ID,
  97.    $wpdb->posts.post_name, $wpdb->posts.post_title, $wpdb->posts.post_author,
  98.    $wpdb->posts.post_date, YEAR($wpdb->posts.post_date) AS 'year',
  99.    MONTH($wpdb->posts.post_date) AS 'month' ,
  100.    $wpdb->posts.post_type
  101.    FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID =
  102.    $wpdb->term_relationships.object_id
  103.         LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id =
  104.                                                                             $wpdb->term_relationships.term_taxonomy_id
  105.         LEFT JOIN $wpdb->terms ON $wpdb->terms.term_id =
  106.                                   $wpdb->term_taxonomy.term_id
  107.  WHERE post_status='publish' $postTypeQuery $inExcludeYearQuery $inExcludeCatQuery
  108.  GROUP BY $wpdb->posts.ID
  109.  ORDER BY $wpdb->posts.post_date $sort";
  110.  
  111.   $allPosts=$wpdb->get_results($postquery);
  112.  
  113.   if ($debug==1) {
  114.     echo  "<pre style='display:none' >";
  115.     printf ("MySQL server version: %s\n", mysql_get_server_info());
  116.     echo  "\ncollapsArch options:\n";
  117.     print_r($options);
  118.     echo  "POST QUERY:\n $postquery\n";
  119.     echo  "\nPOST QUERY RESULTS\n";
  120.     print_r($allPosts);
  121.     echo  "</pre>";
  122.   }
  123.  
  124.   if (!$allPosts)
  125.     return;
  126.  
  127.   $currentYear = -1;
  128.   $currentMonth = -1;
  129.   $lastMonth=-1;
  130.   $lastYear=-1;
  131.   for ($i=0; $i<count($allPosts); $i++) {
  132.     if ($allPosts[$i]->year != $lastyear) {
  133.       $lastYear=$allPosts[$i]->year;
  134.     }
  135.     if ($allPosts[$i]->month != $lastMonth) {
  136.       $lastMonth=$allPosts[$i]->month;
  137.     }
  138.     $yearCounts{"$lastYear"}++;
  139.     $monthCounts{"$lastYear$lastMonth"}++;
  140.   }
  141.  
  142.   $newYear = false;
  143.   $newMonth = false;
  144.   $closePreviousYear = false;
  145.   $monthCount=0;
  146.   $i=0;
  147.   foreach( $allPosts as $archPost ) {
  148.     $expanded=false;
  149.     $monthStyle = "style='display:none'";
  150.     $postStyle = "style='display:none'";
  151.  
  152.     if ($currentYear != $archPost->year ) {
  153.       $lastYear=$currentYear;
  154.       $lastMonth=$currentMonth;
  155.       $currentYear = $archPost->year;
  156.       $theID = "collapsArch-$currentYear:$number";
  157.       /* this should fix the "sparse year" problem
  158.        * Thanks to Aishda
  159.        */
  160.       $currentMonth = 0;
  161.       $newYear = true;
  162.       if ($showYearCount) {
  163.         $yearCount = ' <span class="yearCount">(' .
  164.             $yearCounts{"$currentYear"} . ")</span>\n";
  165.       }
  166.       else {
  167.         $yearCount = '';
  168.       }
  169.       $ding = $expandSym;
  170.       $yearRel = 'expand';
  171.       $monthRel = 'expand';
  172.       $yearTitle= __('click to expand', 'collapsArch');
  173.       $monthTitle= __('click to expand', 'collapsArch');
  174.       /* rel = expand means that it will be hidden, and clicking on the
  175.        * triangle will expand it. rel = collapse means that is will be shown, and
  176.        * clicking on the triangle will collapse it
  177.        */
  178.       if (( $expandCurrentYear AND $archPost->year == date('Y'))
  179.           OR ($useCookies AND $_COOKIE[$theID]==1)) {
  180.         $ding = $collapseSym;
  181.         $yearRel = 'collapse';
  182.         $yearTitle= __('click to collapse', 'collapsArch');
  183.         $monthStyle = '';
  184.       }
  185.      
  186.       if($i>=1 && $allPosts[$i-1]->year != $archPost->year ) {
  187.         if ($currentMonth==0) {
  188.           $lastID = "collapsArch-$lastYear-$lastMonth:$number";
  189.         } else {
  190.           $lastID = "collapsArch-$currentYear-$currentMonth:$number";
  191.         }
  192.         if( $expandYears ) {
  193.           if( $expandMonths ) {
  194.             $expandingCurrentMonth = $expandCurrentMonth && $currentYear == date('Y')
  195.             && $lastMonth== date('n');
  196.             $cookieSet = $useCookies AND isset($_COOKIE[$lastID])
  197.                 AND $_COOKIE[$lastID]==1;
  198.             if ($expanded)
  199.               $archives .= "\n        </ul>\n";
  200.             $archives .= "        </div>\n      </li> <!-- close expanded month --> \n";
  201.           } else {
  202.             $archives .= "      </li> <!-- close month --> \n";
  203.           }
  204.           $archives .= "  </ul>\n     </div>\n  </li> <!-- end year -->\n";
  205.         } else {
  206.           if( $expandMonths ) {
  207.             $archives .= "   </ul>\n     </div>\n  </li> <!-- end year -->\n";
  208.           } else {
  209.             $archives .= "  </li> <!-- end year -->\n";
  210.           }
  211.         }
  212.       }
  213.       $home = get_settings('home');
  214.       if( $expandYears  || $expandMonths) {
  215.         $archives .= "  <li class='collapsing archives $yearRel'><span title='$yearTitle' " .
  216.             "class='collapsing archives $yearRel'>" .
  217.             "<span class='sym'>$ding</span>";
  218.       } else {
  219.         $archives .= "  <li class='collapsing archives item'>\n";
  220.       }
  221.       if (in_array($post_type, array('all', 'post'))) {
  222.         $yearLink = "<a href='".get_year_link($archPost->year). "'>$currentYear $yearCount</a>\n";
  223.       } else {
  224.         $yearLink = "<a href='". add_query_arg('post_type', $post_type, get_year_link($archPost->year)). "'>$currentYear $yearCount</a>\n";
  225.       }
  226.       if ($linkToArch) {
  227.         $archives .=  "</span>";
  228.         $archives .=  $yearLink;
  229.       } else {
  230.         $archives .=  $yearLink;
  231.         $archives .= "</span>";
  232.       }
  233.     if( $expandYears || $expandMonths ) {
  234.       $archives .= "    <div $monthStyle id='$theID'>\n";
  235.       //if ($expandCurrentYear AND $currentYear==date('Y'))
  236.         $archives .= "    <ul>\n";
  237.     }
  238.     $newYear = false;
  239.   }
  240.  
  241.   if ($currentMonth != $archPost->month) {
  242.     $i++;
  243.     if ($currentMonth==0) {
  244.       $lastID = "collapsArch-$lastYear-$lastMonth:$number";
  245.     } else {
  246.       $lastID = "collapsArch-$currentYear-$currentMonth:$number";
  247.     }
  248.     $lastMonth = $currentMonth;
  249.     $currentMonth = $archPost->month;
  250.     $newMonth = true;
  251.     if ($expandYears)
  252.       $theID = "collapsArch-$currentYear-$currentMonth:$number";
  253.     if ($i>1) {
  254.       if ($monthText!='')
  255.         $monthText = "<ul>$monthText</ul>";
  256.       $collapsArchItems[$lastID] = "$monthText";
  257.       $monthText='';
  258.     }
  259.     if ($expandCurrentMonth AND $currentYear == date('Y')
  260.         AND $lastMonth== date('n')
  261.         OR ($useCookies AND isset($_COOKIE[$lastID]) AND $_COOKIE[$lastID]==1))
  262.       $expanded=true;
  263.     else
  264.       $expanded=false;
  265.     if($newYear == false) { #close off last month
  266.      $newYear=true;
  267.     } else {
  268.       if ($expandYears) {
  269.         if ($expandMonths) {
  270.           if ($expanded)
  271.             $archives .= "\n        </ul>\n";
  272.           $archives .= "       </div>\n      </li> <!-- close expanded month --> \n";
  273.         } else {
  274.           $archives .= "      </li> <!-- close month --> \n";
  275.         }
  276.       }
  277.     }
  278.  
  279.     if ($showMonthCount) {
  280.       $monthCount = ' <span class="monthCount">(' .
  281.           $monthCounts{"$currentYear$currentMonth"} . ")</span>\n";
  282.     } else {
  283.       $monthCount = '';
  284.     }
  285.     if( $expandYears ) {
  286.       $text = sprintf('%s', $month[zeroise($currentMonth,2)]);
  287.  
  288.       $text = wptexturize($text);
  289.       $title_text = wp_specialchars($text,1);
  290.  
  291.       if (in_array($post_type, array('all', 'post'))) {
  292.         $monthLink = get_month_link( $currentYear, $currentMonth );
  293.       } else {
  294.         $monthLink = add_query_arg('post_type', $post_type,get_month_link( $currentYear, $currentMonth ));
  295.       }
  296.       if ($expandMonths ) {
  297.         $link = 'javascript:;';
  298.         $monthCollapse = 'collapsing archives';
  299.         if(( $expandCurrentMonth && $currentYear == date('Y')
  300.             AND $currentMonth == date('n'))
  301.             OR ($useCookies AND $_COOKIE[$theID]==1)) {
  302.           $monthRel = 'collapse';
  303.           $monthTitle= __('click to collapse', 'collapsArch');
  304.           $postStyle = '';
  305.           $ding = $collapseSym;
  306.         } else {
  307.           $monthRel = 'expand';
  308.           $monthTitle= __('click to expand', 'collapsArch');
  309.           $ding = $expandSym;
  310.         }
  311.         $the_span = "<span title='$monthTitle' " .
  312.             "class='$monthCollapse $monthRel'>" ;
  313.         $the_ding="<span class='sym'>$ding</span>";
  314.         if ($linkToArch) {
  315.           $the_link= "$the_span$the_ding</span>";
  316.           $the_link .="<a href='$monthLink' title='$title_text'>";
  317.           $the_link .="$text $monthCount</a>\n";
  318.         } else {
  319.           $the_link ="$the_span$the_ding<a href='$monthLink'>$text $monthCount</a>";
  320.           $the_link.="</span>";
  321.         }
  322.       } else {
  323.         $monthRel = '';
  324.         $monthTitle = '';
  325.         $monthCollapse = 'collapsing archives';
  326.         $the_link ="<a href='$monthLink' title='$title_text'>";
  327.         $the_link .="$text $monthCount</a>\n";
  328.       }
  329.  
  330.       $archives .= "      <li class='collapsing archives $monthRel'>".$the_link;
  331.  
  332.     }
  333.     if ($expandYears && $expandMonths ) {
  334.       $archives .= "        <div $postStyle " .
  335.           "id='$theID'>\n";
  336.       if (($expandCurrentMonth AND $currentYear == date('Y')
  337.           AND $currentMonth== date('n'))
  338.           OR ($useCookies AND isset($_COOKIE[$theID]) AND $_COOKIE[$theID]==1))
  339.         $archives .= "        <ul>\n";
  340.       if ($expanded)
  341.       $text = '';
  342.     }
  343.   } else {
  344.     if( $expandYears && $expandMonths ) {
  345.       $text = '';
  346.     }
  347.   }
  348.   $text = '';
  349.   if( $showPostNumber ) {
  350.     $text .= '#'.$archPost->ID;
  351.   }
  352.  
  353.     if ($showPostTitle  && $expandMonths) {
  354.  
  355.       //$title_text = htmlspecialchars(strip_tags(__($archPost->post_title)), ENT_QUOTES);
  356.       $title_text = apply_filters('post_title', $archPost->post_title);
  357.       if(strlen(trim($title_text))==0) {
  358.         $title_text = $noTitle;
  359.       }
  360.       $tmp_text = '';
  361.       if ($postTitleLength>0 && strlen($title_text)>$postTitleLength ) {
  362.         $tmp_text = substr($title_text, 0, $postTitleLength );
  363.         $tmp_text .= ' &hellip;';
  364.       }
  365.  
  366.       $text .= ( $tmp_text == '') ? $title_text : $tmp_text;
  367.       if ($showPostDate ) {
  368.         $theDate = mysql2date($postDateFormat, $archPost->post_date );
  369.         if ($postDateAppend=='after') {
  370.           $text .= ( $text == '' ? $theDate : " $theDate" );
  371.         } else {
  372.           $text = ( $text == '' ? $theDate : "$theDate " ) . $text;
  373.         }
  374.       }
  375.  
  376.       if ($showCommentCount ) {
  377.         $commcount = ' ('.get_comments_number($archPost->ID).')';
  378.       }
  379.  
  380.       $link = get_permalink($archPost);
  381.       $monthText .= "          <li class='collapsing archives item'><a href='$link' " .
  382.           "title='$title_text'>$text</a>$commcount</li>\n";
  383.       if (($expandCurrentMonth  AND $expandYears
  384.             AND $currentYear == date('Y') AND $currentMonth == date('n'))
  385.             OR ($useCookies AND $_COOKIE[$theID]==1)) {
  386.         $archives .= "$monthText";
  387.         $monthText='';
  388.       } elseif (($expandCurrentYear  && $expandMonths
  389.            && $currentYear == date('Y'))
  390.            OR ($useCookies AND $_COOKIE[$theID]==1)) {
  391.       }
  392.     }
  393.   }
  394.   if ($expandMonths) {
  395.     if ((!$currentYear == date('Y') && !$currentMonth == date('n'))
  396.         OR ($useCookies AND !$_COOKIE[$theID]==1)) {
  397.       $archives .= '';
  398.     }
  399.     $archives .= "        </div>
  400.    </li> <!-- close month -->\n";
  401.     $archives .= "  </ul></div>";
  402.     if ($monthText!='')
  403.       $monthText = "<ul>$monthText</ul>";
  404.     if ($expandYears) {
  405.       $collapsArchItems[$theID] = $monthText;
  406.     } else {
  407.       $thisID = "collapsArch-$currentYear-$currentMonth:$number";
  408.       $collapsArchItems[$thisID] .= $monthText;
  409.     }
  410.     $monthText='';
  411.   }
  412.   if (!$expandYears && $expandMonths) {
  413.   }
  414.  
  415.   if ($expandYears && !$expandMonths) {
  416.     $archives .= "  </li> <!-- close month --></div><!-- close year -->\n";
  417.     $collapsArchItems[$theID] = $theID;
  418.   }
  419.   if ($expandYears) {
  420.     $archives .= "</li> <!-- end of collapsing archives -->";
  421.   }
  422.   return($archives);
  423. }
  424. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement