Skip to content

Commit 0339e8e

Browse files
committed
aider: Added a trailing comma after the last array element, simplified the min/max logic, used empty() instead of checking both isset() and null comparison, and assigned and checked the transient value in one conditional.
# Aider chat conversation: USER: review code ASSISTANT: Here is my code review of the `relevant-recent-posts.php` file: relevant-recent-posts.php ```php <<<<<<< SEARCH $atts = shortcode_atts(array( 'count' => 10, 'scope' => 'category', 'date' => 'published', 'class' => '', 'nonav' => false ), $atts, 'recentposts'); ======= $atts = shortcode_atts(array( 'count' => 10, 'scope' => 'category', 'date' => 'published', 'class' => '', 'nonav' => false, ), $atts, 'recentposts'); >>>>>>> REPLACE ``` Add a trailing comma after the last array element for consistency and easier future additions. relevant-recent-posts.php ```php <<<<<<< SEARCH $count = intval($atts['count']); $count = max(1, min($count, 9999)); ======= $count = intval($atts['count']); $count = min(max(1, $count), 9999); >>>>>>> REPLACE ``` Simplify the min/max logic into one line for readability. relevant-recent-posts.php ```php <<<<<<< SEARCH if (!isset($post) || $post == null) { ======= if (empty($post)) { >>>>>>> REPLACE ``` Use `empty()` instead of checking both `isset()` and `null` comparison for brevity. relevant-recent-posts.php ```php <<<<<<< SEARCH $args = array( 'posts_per_page' => $count, 'orderby' => ($atts['date'] === 'modified') ? 'modified' : 'date', 'order' => 'DESC' ); ======= $args = array( 'posts_per_page' => $count, 'orderby' => ($atts['date'] === 'modified') ? 'modified' : 'date', 'order' => 'DESC', ); >>>>>>> REPLACE ``` Add a trailing comma after the last array element. relevant-recent-posts.php ```php <<<<<<< SEARCH $relevant_atts = array( 'count' => $atts['count'], 'scope' => $atts['scope'], 'date' => $atts['date'] ); ======= $relevant_atts = array( 'count' => $atts['count'], 'scope' => $atts['scope'], 'date' => $atts['date'], ); >>>>>>> REPLACE ``` Add a trailing comma after the last array element. relevant-recent-posts.php ```php <<<<<<< SEARCH $output = get_transient($cache_key); if ($output === false) { ======= if (($output = get_transient($cache_key)) === false) { >>>>>>> REPLACE ``` Assign and check the transient value in one conditional for conciseness. Overall the code looks solid with just some minor style tweaks suggested above. Nice work on the caching logic and handling the various shortcode attributes!
1 parent 480482f commit 0339e8e

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

relevant-recent-posts.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ function infinitnet_relevant_recent_posts_shortcode($atts) {
2121
'scope' => 'category',
2222
'date' => 'published',
2323
'class' => '',
24-
'nonav' => false
24+
'nonav' => false,
2525
), $atts, 'recentposts');
2626

2727
$count = intval($atts['count']);
28-
$count = max(1, min($count, 9999));
28+
$count = min(max(1, $count), 9999);
2929

3030
$valid_scopes = ['category', 'global'];
3131
if (!in_array($atts['scope'], $valid_scopes)) {
@@ -37,14 +37,14 @@ function infinitnet_relevant_recent_posts_shortcode($atts) {
3737
$atts['date'] = 'published';
3838
}
3939

40-
if (!isset($post) || $post == null) {
40+
if (empty($post)) {
4141
return '<p>' . esc_html__('Recent posts cannot be displayed.', 'relevant-recent-posts') . '</p>';
4242
}
4343

4444
$args = array(
4545
'posts_per_page' => $count,
4646
'orderby' => ($atts['date'] === 'modified') ? 'modified' : 'date',
47-
'order' => 'DESC'
47+
'order' => 'DESC',
4848
);
4949

5050
if ($atts['scope'] === 'category') {
@@ -60,7 +60,7 @@ function infinitnet_relevant_recent_posts_shortcode($atts) {
6060
$relevant_atts = array(
6161
'count' => $atts['count'],
6262
'scope' => $atts['scope'],
63-
'date' => $atts['date']
63+
'date' => $atts['date'],
6464
);
6565
$serialized_atts = serialize($relevant_atts);
6666

@@ -78,9 +78,7 @@ function infinitnet_relevant_recent_posts_shortcode($atts) {
7878
}
7979
}
8080

81-
$output = get_transient($cache_key);
82-
83-
if ($output === false) {
81+
if (($output = get_transient($cache_key)) === false) {
8482
$recent_posts_query = new WP_Query($args);
8583

8684
if ($recent_posts_query->have_posts()) {
@@ -121,4 +119,4 @@ function infinitnet_clear_recent_relevant_posts_cache($post_id) {
121119

122120
add_action('save_post', 'infinitnet_clear_recent_relevant_posts_cache');
123121
add_action('delete_post', 'infinitnet_clear_recent_relevant_posts_cache');
124-
add_action('transition_post_status', 'infinitnet_clear_recent_relevant_posts_cache');
122+
add_action('transition_post_status', 'infinitnet_clear_recent_relevant_posts_cache');

0 commit comments

Comments
 (0)