-
-
Notifications
You must be signed in to change notification settings - Fork 72
Improve post type registration logic #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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) { | ||||||||||||||||||||||||||||||||||||||||||||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Remove redundant isset checks. Lines 104-105 check 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']),
];
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainConsider verifying save operation success. The code continues with 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 🏁 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 Length of output: 9800 Length of output: 2154 Length of output: 1610 Length of output: 852 Add return value handling for The call to 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 |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Remove redundant sanitization. These lines sanitize 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // 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, | ||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused loop variable.
The
$indexvariable is declared but never used in the loop body.Apply this diff:
Based on static analysis hints.
📝 Committable suggestion
🧰 Tools
🪛 PHPMD (2.15.0)
89-89: Avoid unused local variables such as '$index'. (undefined)
(UnusedLocalVariable)
🤖 Prompt for AI Agents