Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 68 additions & 22 deletions inc/limits/class-post-type-limits.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,75 @@ public function register_emulated_post_types(): void {

$emulated_post_types = wu_get_setting('emulated_post_types', []);

if (is_array($emulated_post_types) && ! empty($emulated_post_types)) {
foreach ($emulated_post_types as $pt) {
$pt = (object) $pt;

$existing_pt = get_post_type_object($pt->post_type);

if ($existing_pt) {
continue;
}

register_post_type(
$pt->post_type,
[
'label' => $pt->label,
'exclude_from_search' => true,
'public' => true,
'show_in_menu' => false,
'has_archive' => false,
'can_export' => false,
'delete_with_user' => false,
]
);
if ( ! is_array($emulated_post_types) || empty($emulated_post_types)) {
return;
}

// Clean up corrupted data automatically
$cleaned_post_types = [];
$needs_update = false;

foreach ($emulated_post_types as $index => $pt) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove unused loop variable.

The $index variable is declared but never used in the loop body.

Apply this diff:

-		foreach ($emulated_post_types as $index => $pt) {
+		foreach ($emulated_post_types as $pt) {

Based on static analysis hints.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
foreach ($emulated_post_types as $index => $pt) {
foreach ($emulated_post_types as $pt) {
🧰 Tools
🪛 PHPMD (2.15.0)

89-89: Avoid unused local variables such as '$index'. (undefined)

(UnusedLocalVariable)

🤖 Prompt for AI Agents
In inc/limits/class-post-type-limits.php around line 89, the foreach declares an
unused $index variable; remove it so the loop reads foreach
($emulated_post_types as $pt) (or use a throwaway variable) to eliminate the
unused variable warning and keep the loop body unchanged.

// Verify that $pt is an array
if ( ! is_array($pt)) {
$needs_update = true;
continue;
}

// Verify that required keys exist (allow empty values for new entries)
if ( ! isset($pt['post_type']) || ! isset($pt['label'])) {
$needs_update = true;
continue;
}

// Add data (even if empty, to allow users to fill in new post types)
$cleaned_post_types[] = [
'post_type' => isset($pt['post_type']) ? sanitize_key($pt['post_type']) : '',
'label' => isset($pt['label']) ? sanitize_text_field($pt['label']) : '',
];
Comment on lines +103 to +106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Remove redundant isset checks.

Lines 104-105 check isset($pt['post_type']) and isset($pt['label']) again, but line 97 already verified these keys exist. The ternary operators are unnecessary here.

Apply this diff to simplify:

 			// Add data (even if empty, to allow users to fill in new post types)
 			$cleaned_post_types[] = [
-				'post_type' => isset($pt['post_type']) ? sanitize_key($pt['post_type']) : '',
-				'label'     => isset($pt['label']) ? sanitize_text_field($pt['label']) : '',
+				'post_type' => sanitize_key($pt['post_type']),
+				'label'     => sanitize_text_field($pt['label']),
 			];

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In inc/limits/class-post-type-limits.php around lines 103 to 106, the array
assignment redundantly checks isset($pt['post_type']) and isset($pt['label')
even though those keys were validated earlier (line ~97); remove the ternary
isset checks and directly use sanitize_key($pt['post_type']) and
sanitize_text_field($pt['label']) when building $cleaned_post_types so the code
is simpler and still safe given the prior validation.

}

// Save cleaned data if there were any changes
if ($needs_update) {
wu_save_setting('emulated_post_types', $cleaned_post_types);
$emulated_post_types = $cleaned_post_types;
}
Comment on lines +109 to +113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

Consider verifying save operation success.

The code continues with $cleaned_post_types regardless of whether wu_save_setting() succeeds. If the save fails silently, corrupted data will be cleaned in memory but persist in the database, requiring repeated cleaning on each request.

Consider adding error handling or logging:

if ($needs_update) {
    $saved = wu_save_setting('emulated_post_types', $cleaned_post_types);
    if ($saved) {
        $emulated_post_types = $cleaned_post_types;
    } else {
        // Log error or handle failure
    }
}

Check if wu_save_setting returns a boolean indicating success:


🏁 Script executed:

#!/bin/bash
# Description: Check the return value and behavior of wu_save_setting

# Find the implementation of wu_save_setting
ast-grep --pattern 'function wu_save_setting($$$) {
  $$$
}'

# Also check the settings class save_setting method
ast-grep --pattern 'function save_setting($$$) {
  $$$
}'

Length of output: 230



---

🏁 Script executed:

```shell
#!/bin/bash
# Description: Find the save_setting method implementation in the settings class

# Search for the save_setting method definition
ast-grep --pattern 'save_setting($$$) {
  $$$
}'

# Also search more broadly for this method
rg -n "save_setting" --type=php -B 2 -A 10 | head -100

Length of output: 9800



---

🏁 Script executed:

```shell
#!/bin/bash
# Description: Find the Settings class and save_setting method implementation

# Search for class Settings
rg -n "class Settings" --type=php -A 2

# Search for the save_setting function definition (not calls)
rg -n "function save_setting\(" --type=php -A 15 | head -150

Length of output: 2154



---

🏁 Script executed:

```shell
#!/bin/bash
# Description: Get the complete save_setting method with return statement

rg -n "public function save_setting" --type=php -A 25 inc/class-settings.php

Length of output: 1610



---

🏁 Script executed:

```shell
#!/bin/bash
# Description: Find wu_save_option implementation to confirm return value

rg -n "function wu_save_option" --type=php -A 15

Length of output: 852


Add return value handling for wu_save_setting() to prevent silent save failures.

The call to wu_save_setting() at line 112 returns a boolean indicating success, but this value is ignored. If the save fails, corrupted data will be cleaned in memory while stale data persists in the database, requiring repeated cleanup on each request.

Update lines 109-113 to check the return value:

if ($needs_update) {
    if (wu_save_setting('emulated_post_types', $cleaned_post_types)) {
        $emulated_post_types = $cleaned_post_types;
    } else {
        // Handle save failure
    }
}

Alternatively, log the failure or trigger an admin notice to alert users of potential data consistency issues.

🤖 Prompt for AI Agents
In inc/limits/class-post-type-limits.php around lines 109 to 113, the call to
wu_save_setting('emulated_post_types', $cleaned_post_types) currently ignores
its boolean return value which can hide save failures and leave the DB stale;
change the block to check the return: only set $emulated_post_types =
$cleaned_post_types when wu_save_setting returns true, and if it returns false
handle the failure (e.g., log an error with relevant context and/or trigger an
admin notice) so callers are informed and data consistency issues are surfaced.


// Register only valid post types (skip empty ones)
foreach ($emulated_post_types as $pt) {
// Skip if post_type or label is empty
if (empty($pt['post_type']) || empty($pt['label'])) {
continue;
}

$post_type = sanitize_key($pt['post_type']);
$label = sanitize_text_field($pt['label']);
Comment on lines +122 to +123
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Remove redundant sanitization.

These lines sanitize post_type and label again, but they were already sanitized in the cleaning loop (lines 104-105). When $needs_update is true, $emulated_post_types is replaced with $cleaned_post_types on line 112, which contains sanitized values. When $needs_update is false, the data passed validation but wasn't modified—yet sanitizing again is still defensive duplication.

Since the empty checks on lines 118-120 already guard against empty values, you can simplify lines 122-128 to:

 		// Register only valid post types (skip empty ones)
 		foreach ($emulated_post_types as $pt) {
 			// Skip if post_type or label is empty
 			if (empty($pt['post_type']) || empty($pt['label'])) {
 				continue;
 			}
 
-			$post_type = sanitize_key($pt['post_type']);
-			$label     = sanitize_text_field($pt['label']);
-
-			// Skip if sanitization resulted in empty values
-			if (empty($post_type) || empty($label)) {
-				continue;
-			}
-
 			// Check if post type is already registered
-			$existing_pt = get_post_type_object($post_type);
+			$existing_pt = get_post_type_object($pt['post_type']);
 
 			if ($existing_pt) {
 				continue;
 			}
 
 			// Register the post type
 			register_post_type(
-				$post_type,
+				$pt['post_type'],
 				[
-					'label'               => $label,
+					'label'               => $pt['label'],
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$post_type = sanitize_key($pt['post_type']);
$label = sanitize_text_field($pt['label']);
// Register only valid post types (skip empty ones)
foreach ($emulated_post_types as $pt) {
// Skip if post_type or label is empty
if (empty($pt['post_type']) || empty($pt['label'])) {
continue;
}
// Check if post type is already registered
$existing_pt = get_post_type_object($pt['post_type']);
if ($existing_pt) {
continue;
}
// Register the post type
register_post_type(
$pt['post_type'],
[
'label' => $pt['label'],
🤖 Prompt for AI Agents
In inc/limits/class-post-type-limits.php around lines 122-123, the code
redundantly re-sanitizes post_type and label even though $cleaned_post_types
already contains sanitized values; remove the sanitize_key/sanitize_text_field
calls and use the existing $pt['post_type'] and $pt['label'] directly (e.g.
assign $post_type = $pt['post_type']; $label = $pt['label'];), keeping the
existing empty checks on lines 118-120 and the subsequent logic unchanged so
behavior and validation remain identical.


// Skip if sanitization resulted in empty values
if (empty($post_type) || empty($label)) {
continue;
}

// Check if post type is already registered
$existing_pt = get_post_type_object($post_type);

if ($existing_pt) {
continue;
}

// Register the post type
register_post_type(
$post_type,
[
'label' => $label,
'exclude_from_search' => true,
'public' => true,
'show_in_menu' => false,
'has_archive' => false,
'can_export' => false,
'delete_with_user' => false,
]
);
}
}

Expand Down
Loading