|
6 | 6 | * Author URI: https://infinitnet.io/ |
7 | 7 | * Plugin URI: https://github.com/infinitnet/wordpress-date-shortcodes |
8 | 8 | * Update URI: https://github.com/infinitnet/wordpress-date-shortcodes |
9 | | - * Version: 1.1 |
| 9 | + * Version: 1.2 |
10 | 10 | * License: GPLv3 |
11 | 11 | * Text Domain: wordpress-date-shortcodes |
12 | 12 | */ |
13 | 13 |
|
14 | 14 | // Main function |
15 | 15 | if ( ! function_exists( 'infinitnet_date_shortcode' ) ) { |
16 | | - function infinitnet_date_shortcode( $type, $format, $adjustment = 0, $post_date = '' ) { |
| 16 | + function infinitnet_date_shortcode( $type, $format, $atts ) { |
| 17 | + $atts = shortcode_atts( array('adjust' => 0), $atts ); |
| 18 | + $adjustment = intval($atts['adjust']); |
| 19 | + |
17 | 20 | switch ( $type ) { |
18 | 21 | case 'current': |
19 | | - $date = date_i18n($format, strtotime("$adjustment days")); |
| 22 | + if ($format == 'Y') { |
| 23 | + $adjusted_date = strtotime("$adjustment year"); |
| 24 | + } elseif ($format == 'F') { |
| 25 | + $adjusted_date = strtotime("$adjustment month"); |
| 26 | + } else { |
| 27 | + $adjusted_date = strtotime("$adjustment day"); |
| 28 | + } |
| 29 | + $date = date_i18n($format, $adjusted_date); |
20 | 30 | break; |
21 | 31 | case 'published': |
22 | | - $date = get_the_date($format, get_the_ID()); |
23 | | - break; |
24 | 32 | case 'modified': |
25 | | - $date = get_the_modified_date($format, get_the_ID()); |
| 33 | + $post_date = ($type == 'published') ? get_the_date('Y-m-d', get_the_ID()) : get_the_modified_date('Y-m-d', get_the_ID()); |
| 34 | + if ($format == 'Y') { |
| 35 | + $date = date_i18n($format, strtotime("$post_date +$adjustment year")); |
| 36 | + } elseif ($format == 'F') { |
| 37 | + $date = date_i18n($format, strtotime("$post_date +$adjustment month")); |
| 38 | + } else { |
| 39 | + $date = date_i18n($format, strtotime("$post_date +$adjustment day")); |
| 40 | + } |
26 | 41 | break; |
27 | 42 | default: |
28 | 43 | $date = ''; |
29 | 44 | } |
30 | 45 |
|
31 | | - if ($type !== 'current' && $adjustment !== 0) { |
32 | | - $date = date_i18n($format, strtotime("$post_date $adjustment days")); |
33 | | - } |
34 | | - |
35 | 46 | return $date; |
36 | 47 | } |
37 | 48 | } |
38 | 49 |
|
39 | 50 | // Year shortcodes |
40 | | -add_shortcode( 'currentyear', function() { return infinitnet_date_shortcode('current', 'Y'); }); |
41 | | -add_shortcode( 'publishedyear', function() { return infinitnet_date_shortcode('published', 'Y'); }); |
42 | | -add_shortcode( 'modifiedyear', function() { return infinitnet_date_shortcode('modified', 'Y'); }); |
| 51 | +add_shortcode( 'currentyear', function($atts) { return infinitnet_date_shortcode('current', 'Y', $atts); }); |
| 52 | +add_shortcode( 'publishedyear', function($atts) { return infinitnet_date_shortcode('published', 'Y', $atts); }); |
| 53 | +add_shortcode( 'modifiedyear', function($atts) { return infinitnet_date_shortcode('modified', 'Y', $atts); }); |
43 | 54 |
|
44 | 55 | // Month shortcodes |
45 | | -add_shortcode( 'currentmonth', function() { return infinitnet_date_shortcode('current', 'F'); }); |
46 | | -add_shortcode( 'publishedmonth', function() { return infinitnet_date_shortcode('published', 'F'); }); |
47 | | -add_shortcode( 'modifiedmonth', function() { return infinitnet_date_shortcode('modified', 'F'); }); |
| 56 | +add_shortcode( 'currentmonth', function($atts) { return infinitnet_date_shortcode('current', 'F', $atts); }); |
| 57 | +add_shortcode( 'publishedmonth', function($atts) { return infinitnet_date_shortcode('published', 'F', $atts); }); |
| 58 | +add_shortcode( 'modifiedmonth', function($atts) { return infinitnet_date_shortcode('modified', 'F', $atts); }); |
48 | 59 |
|
49 | 60 | // Day shortcodes |
50 | 61 | add_shortcode( 'currentday', function($atts) { |
51 | | - $atts = shortcode_atts( array('adjust' => 0), $atts, 'currentday' ); |
52 | 62 | $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; |
53 | | - return infinitnet_date_shortcode('current', $day_format, intval($atts['adjust'])); |
| 63 | + return infinitnet_date_shortcode('current', $day_format, $atts); |
54 | 64 | }); |
55 | 65 | add_shortcode( 'publishedday', function($atts) { |
56 | | - $atts = shortcode_atts( array('adjust' => 0), $atts, 'publishedday' ); |
57 | 66 | $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; |
58 | | - return infinitnet_date_shortcode('published', $day_format, intval($atts['adjust']), get_the_date('Y-m-d')); |
| 67 | + return infinitnet_date_shortcode('published', $day_format, $atts); |
59 | 68 | }); |
60 | 69 | add_shortcode( 'modifiedday', function($atts) { |
61 | | - $atts = shortcode_atts( array('adjust' => 0), $atts, 'modifiedday' ); |
62 | 70 | $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; |
63 | | - return infinitnet_date_shortcode('modified', $day_format, intval($atts['adjust']), get_the_modified_date('Y-m-d')); |
| 71 | + return infinitnet_date_shortcode('modified', $day_format, $atts); |
64 | 72 | }); |
65 | 73 |
|
66 | 74 | // Unified function to process meta content with shortcodes |
|
0 commit comments