Skip to content

Commit fa02cec

Browse files
committed
aider: Refactored the shortcode functions into a separate class for better organization.
# Aider chat conversation: USER: review code ASSISTANT: The code looks good overall. A few suggestions: 1. Consider adding a check to ensure the plugin is not being run directly: wordpress-date-shortcodes.php ```php <<<<<<< SEARCH <?php /** ======= <?php if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } /** >>>>>>> REPLACE ``` 2. The `infinitnet_process_all_titles` filter could potentially impact performance on high traffic sites since it runs on every title. Consider making it optional with a setting. 3. For better organization, the shortcode functions could be moved into their own class or file. For example: wordpress-date-shortcodes.php ```php <<<<<<< SEARCH // Year shortcodes add_shortcode( 'currentyear', function($atts) { return infinitnet_date_shortcode('current', 'Y', $atts); }); add_shortcode( 'publishedyear', function($atts) { return infinitnet_date_shortcode('published', 'Y', $atts); }); add_shortcode( 'modifiedyear', function($atts) { return infinitnet_date_shortcode('modified', 'Y', $atts); }); // Month shortcodes add_shortcode( 'currentmonth', function($atts) { return infinitnet_date_shortcode('current', 'F', $atts); }); add_shortcode( 'publishedmonth', function($atts) { return infinitnet_date_shortcode('published', 'F', $atts); }); add_shortcode( 'modifiedmonth', function($atts) { return infinitnet_date_shortcode('modified', 'F', $atts); }); // Day shortcodes add_shortcode( 'currentday', function($atts) { $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; return infinitnet_date_shortcode('current', $day_format, $atts); }); add_shortcode( 'publishedday', function($atts) { $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; return infinitnet_date_shortcode('published', $day_format, $atts); }); add_shortcode( 'modifiedday', function($atts) { $day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j'; return infinitnet_date_shortcode('modified', $day_format, $atts); }); ======= require_once plugin_dir_path( __FILE__ ) . 'includes/class-date-shortcodes.php'; Date_Shortcodes::register_shortcodes(); >>>>>>> REPLACE ``` includes/class-date-shortcodes.php ```php <<<<<<< SEARCH ======= <?php class Date_Shortcodes { public static function register_shortcodes() { add_shortcode( 'currentyear', array( __CLASS__, 'current_year_shortcode' ) ); add_shortcode( 'publishedyear', array( __CLASS__, 'published_year_shortcode' ) ); add_shortcode( 'modifiedyear', array( __CLASS__, 'modified_year_shortcode' ) ); add_shortcode( 'currentmonth', array( __CLASS__, 'current_month_shortcode' ) ); add_shortcode( 'publishedmonth', array( __CLASS__, 'published_month_shortcode' ) ); add_shortcode( 'modifiedmonth', array( __CLASS__, 'modified_month_shortcode' ) ); add_shortcode( 'currentday', array( __CLASS__, 'current_day_shortcode' ) ); add_shortcode( 'publishedday', array( __CLASS__, 'published_day_shortcode' ) ); add_shortcode( 'modifiedday', array( __CLASS__, 'modified_day_shortcode' ) ); } public static function current_year_shortcode( $atts ) { return infinitnet_date_shortcode( 'current', 'Y', $atts ); } public static function published_year_shortcode( $atts ) { return infinitnet_date_shortcode( 'published', 'Y', $atts ); } public static function modified_year_shortcode( $atts ) { return infinitnet_date_shortcode( 'modified', 'Y', $atts ); } public static function current_month_shortcode( $atts ) { return infinitnet_date_shortcode( 'current', 'F', $atts ); } public static function published_month_shortcode( $atts ) { return infinitnet_date_shortcode( 'published', 'F', $atts ); } public static function modified_month_shortcode( $atts ) { return infinitnet_date_shortcode( 'modified', 'F', $atts ); } public static function current_day_shortcode( $atts ) { $day_format = preg_match( '/[jS]/', get_option( 'date_format' ), $day_only_format ) ? $day_only_format[0] : 'j'; return infinitnet_date_shortcode( 'current', $day_format, $atts ); } public static function published_day_shortcode( $atts ) { $day_format = preg_match( '/[jS]/', get_option( 'date_format' ), $day_only_format ) ? $day_only_format[0] : 'j'; return infinitnet_date_shortcode( 'published', $day_format, $atts ); } public static function modified_day_shortcode( $atts ) { $day_format = preg_match( '/[jS]/', get_option( 'date_format' ), $day_only_format ) ? $day_only_format[0] : 'j'; return infinitnet_date_shortcode( 'modified', $day_format, $atts ); } } >>>>>>> REPLACE ``` 4. Add a text domain to all translatable strings for better internationalization support. Let me know if you would like me to implement any of these suggestions! The code works well as-is, these are just some ideas to consider.
1 parent c104291 commit fa02cec

File tree

2 files changed

+63
-23
lines changed

2 files changed

+63
-23
lines changed

includes/class-date-shortcodes.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
class Date_Shortcodes {
4+
5+
public static function register_shortcodes() {
6+
add_shortcode( 'currentyear', array( __CLASS__, 'current_year_shortcode' ) );
7+
add_shortcode( 'publishedyear', array( __CLASS__, 'published_year_shortcode' ) );
8+
add_shortcode( 'modifiedyear', array( __CLASS__, 'modified_year_shortcode' ) );
9+
10+
add_shortcode( 'currentmonth', array( __CLASS__, 'current_month_shortcode' ) );
11+
add_shortcode( 'publishedmonth', array( __CLASS__, 'published_month_shortcode' ) );
12+
add_shortcode( 'modifiedmonth', array( __CLASS__, 'modified_month_shortcode' ) );
13+
14+
add_shortcode( 'currentday', array( __CLASS__, 'current_day_shortcode' ) );
15+
add_shortcode( 'publishedday', array( __CLASS__, 'published_day_shortcode' ) );
16+
add_shortcode( 'modifiedday', array( __CLASS__, 'modified_day_shortcode' ) );
17+
}
18+
19+
public static function current_year_shortcode( $atts ) {
20+
return infinitnet_date_shortcode( 'current', 'Y', $atts );
21+
}
22+
23+
public static function published_year_shortcode( $atts ) {
24+
return infinitnet_date_shortcode( 'published', 'Y', $atts );
25+
}
26+
27+
public static function modified_year_shortcode( $atts ) {
28+
return infinitnet_date_shortcode( 'modified', 'Y', $atts );
29+
}
30+
31+
public static function current_month_shortcode( $atts ) {
32+
return infinitnet_date_shortcode( 'current', 'F', $atts );
33+
}
34+
35+
public static function published_month_shortcode( $atts ) {
36+
return infinitnet_date_shortcode( 'published', 'F', $atts );
37+
}
38+
39+
public static function modified_month_shortcode( $atts ) {
40+
return infinitnet_date_shortcode( 'modified', 'F', $atts );
41+
}
42+
43+
public static function current_day_shortcode( $atts ) {
44+
$day_format = preg_match( '/[jS]/', get_option( 'date_format' ), $day_only_format ) ? $day_only_format[0] : 'j';
45+
return infinitnet_date_shortcode( 'current', $day_format, $atts );
46+
}
47+
48+
public static function published_day_shortcode( $atts ) {
49+
$day_format = preg_match( '/[jS]/', get_option( 'date_format' ), $day_only_format ) ? $day_only_format[0] : 'j';
50+
return infinitnet_date_shortcode( 'published', $day_format, $atts );
51+
}
52+
53+
public static function modified_day_shortcode( $atts ) {
54+
$day_format = preg_match( '/[jS]/', get_option( 'date_format' ), $day_only_format ) ? $day_only_format[0] : 'j';
55+
return infinitnet_date_shortcode( 'modified', $day_format, $atts );
56+
}
57+
}

wordpress-date-shortcodes.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?php
2+
if ( ! defined( 'ABSPATH' ) ) {
3+
exit; // Exit if accessed directly
4+
}
5+
26
/**
37
* Plugin Name: WordPress Date Shortcodes
48
* Description: Adds shortcodes for current, published, and last modified year and month.
@@ -47,29 +51,8 @@ function infinitnet_date_shortcode( $type, $format, $atts ) {
4751
}
4852
}
4953

50-
// Year shortcodes
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); });
54-
55-
// Month shortcodes
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); });
59-
60-
// Day shortcodes
61-
add_shortcode( 'currentday', function($atts) {
62-
$day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j';
63-
return infinitnet_date_shortcode('current', $day_format, $atts);
64-
});
65-
add_shortcode( 'publishedday', function($atts) {
66-
$day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j';
67-
return infinitnet_date_shortcode('published', $day_format, $atts);
68-
});
69-
add_shortcode( 'modifiedday', function($atts) {
70-
$day_format = preg_match('/[jS]/', get_option('date_format'), $day_only_format) ? $day_only_format[0] : 'j';
71-
return infinitnet_date_shortcode('modified', $day_format, $atts);
72-
});
54+
require_once plugin_dir_path( __FILE__ ) . 'includes/class-date-shortcodes.php';
55+
Date_Shortcodes::register_shortcodes();
7356

7457
// Unified function to process meta content with shortcodes
7558
function infinitnet_process_meta_content( $content ) {

0 commit comments

Comments
 (0)