Archive.php
3 weeks ago
Calendar.php
1 day ago
Core.php
2 months ago
Names.php
3 weeks ago
Posts.php
1 day ago
WPP_ParsiDate.php
1 day ago
Archive.php
226 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Archive class |
| 4 | * |
| 5 | * Print post, month, year archive links |
| 6 | */ |
| 7 | |
| 8 | namespace WPParsidate\Core; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | class Archive { |
| 13 | /** |
| 14 | * Create Persian Archives |
| 15 | * |
| 16 | * @param string|array $args |
| 17 | */ |
| 18 | public static function getPostTypeArchives( $args = '' ): void { |
| 19 | global $wpdb; |
| 20 | |
| 21 | $defaults = array( |
| 22 | 'type' => 'monthly', |
| 23 | 'limit' => '', |
| 24 | 'format' => 'html', |
| 25 | 'before' => '', |
| 26 | 'after' => '', |
| 27 | 'show_post_count' => false, |
| 28 | 'echo' => 1, |
| 29 | 'order' => 'DESC', |
| 30 | 'post_type' => 'post' |
| 31 | ); |
| 32 | |
| 33 | $r = wp_parse_args( $args, $defaults ); |
| 34 | $post_type_object = get_post_type_object( $r['post_type'] ); |
| 35 | |
| 36 | if ( is_null( $post_type_object ) || ! is_post_type_viewable( $post_type_object ) ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | $r['post_type'] = $post_type_object->name; |
| 41 | $results = $wpdb->get_results( |
| 42 | $wpdb->prepare( |
| 43 | " |
| 44 | SELECT date( post_date ) AS date, |
| 45 | COUNT( ID ) AS count |
| 46 | FROM $wpdb->posts |
| 47 | WHERE post_date < NOW() |
| 48 | AND post_type = %s |
| 49 | AND post_status = 'publish' |
| 50 | group by date |
| 51 | ORDER BY post_date DESC |
| 52 | ", |
| 53 | $r['post_type'] |
| 54 | ) |
| 55 | ); |
| 56 | |
| 57 | if ( ! empty( $results ) ) { |
| 58 | self::printArchive( $results, $r ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param string|array $args |
| 64 | */ |
| 65 | public static function getPostArchives( $args = '' ): void { |
| 66 | global $wpdb; |
| 67 | |
| 68 | $defaults = array( |
| 69 | 'type' => 'monthly', |
| 70 | 'limit' => '', |
| 71 | 'format' => 'html', |
| 72 | 'before' => '', |
| 73 | 'after' => '', |
| 74 | 'show_post_count' => false, |
| 75 | 'echo' => 1, |
| 76 | 'order' => 'DESC', |
| 77 | 'post_type' => 'post' |
| 78 | ); |
| 79 | |
| 80 | $r = wp_parse_args( $args, $defaults ); |
| 81 | |
| 82 | $results = $wpdb->get_results( |
| 83 | " |
| 84 | SELECT date ( post_date ) AS date, |
| 85 | COUNT( ID ) AS count |
| 86 | FROM $wpdb->posts |
| 87 | WHERE post_date < NOW() |
| 88 | AND post_type = 'post' |
| 89 | AND post_status = 'publish' |
| 90 | GROUP BY date |
| 91 | ORDER BY post_date DESC |
| 92 | " |
| 93 | ); |
| 94 | |
| 95 | if ( ! empty( $results ) ) { |
| 96 | self::printArchive( $results, $r ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param $year |
| 102 | * @param $format |
| 103 | * @param $before |
| 104 | * @param $count |
| 105 | * @param $show_post_count |
| 106 | * @param $r |
| 107 | */ |
| 108 | private static function printYearArchive( $year, $format, $before, $count, $show_post_count, $r ): void { |
| 109 | if ( $show_post_count ) { |
| 110 | $count = ' (' . fix_number( $count ) . ')'; |
| 111 | } else { |
| 112 | $count = ''; |
| 113 | } |
| 114 | |
| 115 | $url = get_year_link( $year ); |
| 116 | |
| 117 | if ( 'post' !== $r['post_type'] ) { |
| 118 | $url = add_query_arg( 'post_type', $r['post_type'], $url ); |
| 119 | } |
| 120 | |
| 121 | echo get_archives_link( $url, fix_number( $year ), $format, $before, $count ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param $old_date |
| 126 | * @param $format |
| 127 | * @param $before |
| 128 | * @param $count |
| 129 | * @param $show_post_count |
| 130 | * @param $r |
| 131 | */ |
| 132 | private static function printMonthArchive( $old_date, $format, $before, $count, $show_post_count, $r ): void { |
| 133 | $wpp_months_name = Names::getMonths(); |
| 134 | $year = substr( $old_date, 0, 4 ); |
| 135 | $month = substr( $old_date, 4, 2 ); |
| 136 | |
| 137 | if ( $show_post_count ) { |
| 138 | $count = ' (' . fix_number( $count ) . ')'; |
| 139 | } else { |
| 140 | $count = ''; |
| 141 | } |
| 142 | |
| 143 | $url = get_month_link( $year, $month ); |
| 144 | |
| 145 | if ( 'post' !== $r['post_type'] ) { |
| 146 | $url = add_query_arg( 'post_type', $r['post_type'], $url ); |
| 147 | } |
| 148 | |
| 149 | echo get_archives_link( $url, $wpp_months_name[ (int) $month ] . ' ' . fix_number( $year ), $format, $before, |
| 150 | $count ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @param $results |
| 155 | * @param $args |
| 156 | */ |
| 157 | private static function printArchive( $results, $args ): void { |
| 158 | global $wpp_months_name; |
| 159 | |
| 160 | if ( $args['type'] === 'yearly' ) { |
| 161 | $old_date = parsidate( 'Y', $results[0]->date, 'eng' ); |
| 162 | $count = $results[0]->count; |
| 163 | $c = count( $results ); |
| 164 | |
| 165 | for ( $i = 1; $i < $c; $i ++ ) { |
| 166 | $dt = $results[ $i ]; |
| 167 | $date = parsidate( 'Y', $dt->date, 'eng' ); |
| 168 | |
| 169 | if ( $date === $old_date ) { |
| 170 | $count += $dt->count; |
| 171 | } else { |
| 172 | self::printYearArchive( $old_date, $args['format'], $args['before'], $count, |
| 173 | $args['show_post_count'], |
| 174 | $args ); |
| 175 | |
| 176 | $old_date = $date; |
| 177 | $count = $dt->count; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | self::printYearArchive( $old_date, $args['format'], $args['before'], $count, $args['show_post_count'], |
| 182 | $args ); |
| 183 | |
| 184 | } elseif ( $args['type'] === 'monthly' ) { |
| 185 | $old_date = parsidate( 'Ym', $results[0]->date, 'eng' ); |
| 186 | $count = $results[0]->count; |
| 187 | $c = count( $results ); |
| 188 | |
| 189 | for ( $i = 1; $i < $c; $i ++ ) { |
| 190 | $dt = $results[ $i ]; |
| 191 | $date = parsidate( 'Ym', $dt->date, 'eng' ); |
| 192 | |
| 193 | if ( $date === $old_date ) { |
| 194 | $count += $dt->count; |
| 195 | } else { |
| 196 | self::printMonthArchive( $old_date, $args['format'], $args['before'], $count, |
| 197 | $args['show_post_count'], |
| 198 | $args ); |
| 199 | $old_date = $date; |
| 200 | $count = $dt->count; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | self::printMonthArchive( $old_date, $args['format'], $args['before'], $count, $args['show_post_count'], |
| 205 | $args ); |
| 206 | |
| 207 | } elseif ( $args['type'] === 'daily' ) { |
| 208 | foreach ( $results as $row ) { |
| 209 | $date = parsidate( 'Y,m,d', $row->date, 'eng' ); |
| 210 | $date = explode( ',', $date ); |
| 211 | |
| 212 | if ( $args['show_post_count'] ) { |
| 213 | $count = ' (' . fix_number( $row->count ) . ')'; |
| 214 | } else { |
| 215 | $count = ''; |
| 216 | } |
| 217 | |
| 218 | $text = fix_number( $date[2] ) . ' ' . $wpp_months_name[ (int) $date[1] ] . ' ' . fix_number( $date[0] ); |
| 219 | |
| 220 | echo get_archives_link( get_day_link( $date[0], $date[1], $date[2] ), $text, $args['format'], |
| 221 | $args['before'], $count ); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 |