PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / 4.0.0
پارسی دیت – Parsi Date v4.0.0
6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0.1 2.3.0.2 2.3.1 2.3.2 2.3.3 2.3.4 3.0.1 3.0.2 3.0.3 4.0.0 4.0.1 4.0.2 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5
wp-parsidate / includes / fixes-permalinks.php
wp-parsidate / includes Last commit date
admin 4 years ago plugins 4 years ago widget 4 years ago fixes-archive.php 4 years ago fixes-archives.php 4 years ago fixes-calendar.php 4 years ago fixes-dates.php 4 years ago fixes-misc.php 4 years ago fixes-permalinks.php 4 years ago general.php 4 years ago install.php 4 years ago parsidate.php 4 years ago settings.php 4 years ago
fixes-permalinks.php
390 lines
1 <?php
2
3 defined( 'ABSPATH' ) or exit( 'No direct script access allowed' );
4
5 /**
6 * Fixes and converts permalinks date to Jalali
7 *
8 * @author Mobin Ghasempoor
9 * @package WP-Parsidate
10 * @subpackage Fixes/Permalinks
11 */
12 global $wpp_settings;
13
14 if ( wpp_is_active( 'conv_permalinks' ) ) {
15 add_filter( 'posts_where', 'wpp_posts_where', 10, 2 );
16 add_action( 'pre_get_posts', 'wpp_pre_get_posts' );
17 add_filter( 'post_link', 'wpp_permalink', 10, 3 );
18 }
19
20 /**
21 * Converts post date pointer to Jalali pointer
22 *
23 * @param string $where
24 * @param string $wp_query
25 *
26 * @return string
27 */
28 function wpp_posts_where( $where, $wp_query = '' ) {
29 global $wpdb;
30
31 if ( empty( $wp_query ) ) {
32 global $wp_query;
33 }
34
35 if ( ! $wp_query->is_main_query() || empty( $wp_query->query_vars ) ) {
36 return $where;
37 }
38
39 $pd = bn_parsidate::getInstance();
40
41 $m = ( isset( $wp_query->query_vars['m'] ) ) ? $wp_query->query_vars['m'] : '';
42 $hour = ( isset( $wp_query->query_vars['hour'] ) ) ? $wp_query->query_vars['hour'] : '';
43 $minute = ( isset( $wp_query->query_vars['minute'] ) ) ? $wp_query->query_vars['minute'] : '';
44 $second = ( isset( $wp_query->query_vars['second'] ) ) ? $wp_query->query_vars['second'] : '';
45 $year = ( isset( $wp_query->query_vars['year'] ) ) ? $wp_query->query_vars['year'] : '';
46 $month = ( isset( $wp_query->query_vars['monthnum'] ) ) ? $wp_query->query_vars['monthnum'] : '';
47 $day = ( isset( $wp_query->query_vars['day'] ) ) ? $wp_query->query_vars['day'] : '';
48
49 if ( ! empty( $m ) ) {
50 $len = strlen( $m );
51 $year = substr( $m, 0, 4 );
52
53 if ( $len > 5 ) {
54 $month = substr( $m, 4, 2 );
55 }
56
57 if ( $len > 7 ) {
58 $day = substr( $m, 6, 2 );
59 }
60
61 if ( $len > 9 ) {
62 $hour = substr( $m, 8, 2 );
63 }
64 if ( $len > 11 ) {
65 $minute = substr( $m, 10, 2 );
66 }
67
68 if ( $len > 13 ) {
69 $second = substr( $m, 12, 2 );
70 }
71 }
72
73 if ( empty( $year ) || $year > 1700 ) {
74 return $where;
75 }
76
77 $stamon = 1;
78 $staday = 1;
79 $stahou = '00';
80 $stamin = '00';
81 $stasec = '00';
82 $endmon = 1;
83 $endday = 1;
84 $endhou = '00';
85 $endmin = '00';
86 $endsec = '00';
87 $stayear = $year;
88 $endyear = $year + 1;
89
90 if ( $month != '' ) {
91 $stamon = $month;
92 $endmon = ( $month == 12 ? 1 : $month + 1 );
93 $endyear = ( $endmon == 1 ? $stayear + 1 : $stayear );
94 }
95
96 if ( $day != '' ) {
97 $staday = $day;
98 $endday = ( $day == $pd->j_days_in_month[ (int) $month - 1 ] ? 1 : $day + 1 );
99 $endmon = ( $endday == 1 ? $stamon + 1 : $stamon );
100 }
101
102 if ( $hour != '' ) {
103 $stahou = $hour;
104 $endhou = ( $hour == 24 ? '00' : $hour + 1 );
105 $endday = ( $endhou == '00' ? $staday + 1 : $staday );
106 }
107
108 if ( $minute != '' ) {
109 $stamin = $minute;
110 $endmin = ( $minute == 59 ? '00' : $minute + 1 );
111 $endhou = ( $endmin == '00' ? $stahou + 1 : $stahou );
112 }
113
114 if ( $second != '' ) {
115 $stasec = $second;
116 $endsec = ( $second == 59 ? '00' : $second + 1 );
117 $endmin = ( $endsec == '00' ? $stamin + 1 : $stamin );
118 }
119
120 $stadate = "$stayear-$stamon-$staday";
121 $enddate = "$endyear-$endmon-$endday";
122 $stadate = gregdate( 'Y-m-d', $stadate );
123 $enddate = gregdate( 'Y-m-d', $enddate );
124 $stadate .= " $stahou:$stamin:$stasec";
125 $enddate .= " $endhou:$endmin:$endsec";
126
127 $patterns = array(
128 '/YEAR\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
129 '/DAYOFMONTH\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
130 '/MONTH\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
131 '/HOUR\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
132 '/MINUTE\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
133 '/SECOND\((.*?)post_date\s*\)\s*=\s*[0-9\']*/'
134 );
135
136 foreach ( $patterns as $pattern ) {
137 $where = preg_replace( $pattern, '1 = 1', $where );
138 }
139
140 $prefixp = "{$wpdb->posts}.";
141 $prefixp = ( strpos( $where, $prefixp ) == false ) ? '' : $prefixp;
142 $where .= " AND {$prefixp}post_date >= '$stadate' AND {$prefixp}post_date < '$enddate' ";
143
144 return $where;
145 }
146
147 /**
148 * Converts post dates to Georgian dates for preventing errors
149 *
150 * @param WP_Query $query
151 *
152 * @return WP_Query
153 */
154 function wpp_pre_get_posts( $query ) {
155 global $wpdb;
156
157 $permalink = $query->query;
158 $year = '';
159 $monthnum = '';
160 $day = '';//start
161
162 if ( isset( $permalink['year'] ) ) {
163 $year = $permalink['year'];
164 }
165
166 if ( isset( $permalink['monthnum'] ) ) {
167 $monthnum = $permalink['monthnum'];
168 }
169
170 if ( isset( $permalink['day'] ) ) {
171 $day = $permalink['day'];
172 }
173
174 if ( $year > 1700 ) {
175 return $query;
176 }
177
178 $out = false;
179 $pd = bn_parsidate::getInstance();
180
181 if ( isset( $permalink['name'] ) ) {
182 $var = $wpdb->get_var( "SELECT post_date FROM {$wpdb->prefix}posts WHERE post_name='{$permalink['name']}' AND post_type!='attachment' ORDER BY id" );
183 $per = parsidate( 'Y-m-d', $var, 'eng' );
184 $per = explode( '-', $per );
185 $out = true;
186
187 if ( ! empty( $year ) && $year != $per[0] ) {
188 $out = false;
189 }
190
191 if ( $out && ! empty( $monthnum ) && $monthnum != $per[1] ) {
192 $out = false;
193 }
194
195 if ( $out && ! empty( $day ) && $day != $per[2] ) {
196 $out = false;
197 }
198 } elseif ( isset( $permalink['post_id'] ) ) {
199 $out = true;
200 $var = $wpdb->get_var( "SELECT post_date FROM {$wpdb->prefix}posts WHERE ID={$permalink['post_id']}" );
201 } elseif ( ! empty( $year ) && ! empty( $monthnum ) && ! empty( $day ) ) {
202 $out = true;
203 $var = gregdate( 'Y-m-d', "$year-$monthnum-$day" );
204 } elseif ( ! empty( $year ) && ! empty( $monthnum ) ) {
205 $stadate = $pd->persian_to_gregorian( $year, $monthnum, 1 );
206 $enddate = $pd->persian_to_gregorian( $year, $monthnum, $pd->j_days_in_month[ ( $monthnum - 1 ) ] );
207 $date_query = array(
208 array(
209 'after' => array(
210 'year' => $stadate[0],
211 'month' => $stadate[1],
212 'day' => $stadate[2] - 1,
213 ),
214 'before' => array(
215 'year' => $enddate[0],
216 'month' => $enddate[1],
217 'day' => $enddate[2] + 1,
218 ),
219 'inclusive' => true,
220 ),
221 );
222
223 $query->set( 'date_query', $date_query );
224
225 $out = false;
226 } elseif ( ! empty( $year ) ) {
227 $stadate = $pd->persian_to_gregorian( $year, 1, 1 );
228 $enddate = $pd->persian_to_gregorian( ( $year + 1 ), 1, 1 );
229 $date_query = array(
230 array(
231 'after' => array(
232 'year' => $stadate[0],
233 'month' => $stadate[1],
234 'day' => $stadate[2] - 1,
235 ),
236 'before' => array(
237 'year' => $enddate[0],
238 'month' => $enddate[1],
239 'day' => $enddate[2],
240 ),
241 'inclusive' => true,
242 ),
243 );
244
245 $query->set( 'date_query', $date_query );
246
247 $out = false;
248 }
249
250 if ( $out ) {
251 preg_match_all( '!\d+!', $var, $matches );
252
253 $var = $matches[0];
254
255 $query->set( 'year', $var[0] );
256 $query->set( 'monthnum', $var[1] );
257 $query->set( 'day', $var[2] );
258
259 $query->is_404 = false;
260 $query->query_vars['error'] = '';
261 }
262
263 return $query;
264 }
265
266 /**
267 * Convert permalink structure to Jalali format
268 *
269 * @param mixed $perma
270 * @param WP_Post $post
271 * @param bool $leavename
272 *
273 * @return string New permalink
274 */
275 function wpp_permalink( $perma, $post, $leavename = false ) {
276 if ( empty( $post->ID ) ) {
277 return false;
278 }
279
280 if ( $post->post_type == 'page' || $post->post_status == 'static' ) {
281 return get_page_link( $post->ID );
282 } elseif ( $post->post_type == 'attachment' ) {
283 return get_attachment_link( $post->ID );
284 } elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ) ) ) {
285 return get_post_permalink( $post->ID );
286 }
287
288 $permalink = get_option( 'permalink_structure' );
289
290 preg_match_all( '%\%([^\%]*)\%%', $permalink, $rewriteCode );
291
292 $rewriteCode = $rewriteCode[0];
293
294 if ( '' != $permalink && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
295 if ( $leavename ) {
296 $rewriteCode = array_diff( $rewriteCode, array( '%postname%', '%pagename%' ) );
297 }
298
299 $date = explode( ' ', parsidate( 'Y m d H i s', $post->post_date, 'eng' ) );
300 $out = array();
301
302 foreach ( $rewriteCode as $rewrite ) {
303 switch ( $rewrite ) {
304 case '%year%':
305 $out[] = $date[0];
306 break;
307 case '%monthnum%':
308 $out[] = $date[1];
309 break;
310 case '%day%':
311 $out[] = $date[2];
312 break;
313 case '%hour%':
314 $out[] = $date[3];
315 break;
316 case '%minute%':
317 $out[] = $date[4];
318 break;
319 case '%second%':
320 $out[] = $date[5];
321 break;
322 case '%post_id%':
323 $out[] = $post->ID;
324 break;
325 case '%postname%':
326 $out[] = $post->post_name;
327 break;
328 case '%category%':
329 $category = '';
330 /**
331 * This code from wp-includes/link-template.php:171
332 * */
333 $cats = get_the_category( $post->ID );
334 if ( $cats ) {
335 $cats = wp_list_sort(
336 $cats,
337 array(
338 'term_id' => 'ASC',
339 )
340 );
341
342 /**
343 * Filters the category that gets used in the %category% permalink token.
344 *
345 * @param WP_Term $cat The category to use in the permalink.
346 * @param array $cats Array of all categories (WP_Term objects) associated with the post.
347 * @param WP_Post $post The post in question.
348 *
349 * @since 3.5.0
350 *
351 */
352 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
353 $category_object = get_term( $category_object, 'category' );
354 $category = $category_object->slug;
355
356 if ( $category_object->parent ) {
357 $category = get_category_parents( $category_object->parent, false, '/', true ) . $category;
358 }
359 }
360
361 // Show default category in permalinks,
362 // without having to assign it explicitly.
363 if ( empty( $category ) ) {
364 $default_category = get_term( get_option( 'default_category' ), 'category' );
365
366 if ( $default_category && ! is_wp_error( $default_category ) ) {
367 $category = $default_category->slug;
368 }
369 }
370
371 $out[] = $category;
372 break;
373 case '%author%':
374 $authordata = get_userdata( $post->post_author );
375 $out[] = $authordata->user_nicename;
376 break;
377 default:
378 unset( $rewriteCode[ array_search( $rewrite, $rewriteCode ) ] );
379 break;
380 }
381 }
382
383 $permalink = home_url( str_replace( $rewriteCode, $out, $permalink ) );
384
385 return user_trailingslashit( $permalink, 'single' );
386 }
387
388 return home_url( "?p=$post->ID" );
389 }
390