-
Notifications
You must be signed in to change notification settings - Fork 1
Usability improvements #46
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
base: master
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR enhances ad posting by enforcing context requirements, fixing a multi-context display bug, and sanitizing submitted context values.
- Adds a transient-based admin notice when required contexts are missing on post save
- Fixes JavaScript to correctly detect multiple context buttons per position
- Filters non-numeric values from
$_POST['tax_input']['ad-context']
before validation
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
includes/post_type.php | Updated JS selector logic to handle multiple .button elements |
includes/meta-box.php | Added pre-save filter for ad-context and admin notice logic |
includes/functions.php | Introduced taf_validate_tax_input() to verify context rules |
includes/context.php | Changed hidden input for ad-context from "empty" to "" |
Comments suppressed due to low confidence (1)
includes/functions.php:326
- The code assumes
ad-position
is a comma-separated string, but in this form it’s submitted as an array. This may emit warnings or skip positions. Consider normalizing both string and array inputs, e.g. by checking if it's already an array beforeexplode()
.
array_map( 'trim', explode( ',', $tax_input['ad-position'] ?? '' ) ),
@@ -78,7 +78,10 @@ function refresh_context() { | |||
|
|||
// check if Position exists that requires Context and is checked. | |||
$('.adPosition__item').each(function () { | |||
if ($(this).find('.button').text().trim() === $parent && $(this).find('.adPosition__check').is(':checked')) { | |||
if ( | |||
$(this).find('.button').toArray().some(el => $(el).text().trim() === $parent) && |
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.
[nitpick] The use of .toArray().some()
works but is less idiomatic. You could simplify this with jQuery’s .filter()
and .length
, e.g.: $(this).find('.button').filter((_,el) => $(el).text().trim() === $parent).length > 0
for better readability and slight performance gain.
$(this).find('.button').toArray().some(el => $(el).text().trim() === $parent) && | |
$(this).find('.button').filter((_, el) => $(el).text().trim() === $parent).length > 0 && |
Copilot uses AI. Check for mistakes.
@@ -47,6 +47,13 @@ | |||
<?php | |||
}, 11 ); | |||
|
|||
// Filter ad-context on submit | |||
add_action('save_post', function () { |
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.
[nitpick] This global save_post
filter will run on every post type. To avoid unintended side effects, consider checking get_post_type()
inside the callback or scoping the hook to your custom post type only.
add_action('save_post', function () { | |
add_action('save_post', function ( $post_id ) { | |
// Check if the post type is 'ad-content'. | |
if ( 'ad-content' !== get_post_type( $post_id ) ) { | |
return; | |
} |
Copilot uses AI. Check for mistakes.
@@ -75,7 +75,7 @@ function taf_context_meta_box_callback( $post ) { | |||
printf( '<p class="description">%s</p>', nl2br( esc_html( $parent->description ) ) ); | |||
} | |||
// Allow selecting no context by adding a hidden input field. | |||
echo '<input type="hidden" name="tax_input[ad-context][]" value="empty" />'; | |||
echo '<input type="hidden" name="tax_input[ad-context][]" value="" />'; |
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.
[nitpick] Directly echoing HTML attributes can risk malformed output. Consider using printf()
with esc_attr()
to ensure correct escaping, e.g.: printf('<input type="hidden" name="tax_input[ad-context][]" value="%s" />', esc_attr(''));
echo '<input type="hidden" name="tax_input[ad-context][]" value="" />'; | |
printf( '<input type="hidden" name="tax_input[ad-context][]" value="%s" />', esc_attr( '' ) ); |
Copilot uses AI. Check for mistakes.
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.
いくつか変更要望があります。
バリデーションはtax_inputに対して行うのではなく、$postに対して行うのが良いと思います。
// Filter ad-context on submit | ||
add_action('save_post', function () { | ||
if ( isset( $_POST['tax_input']['ad-context'] ) && is_array( $_POST['tax_input']['ad-context'] ) ) { | ||
$_POST['tax_input']['ad-context'] = array_filter( $_POST['tax_input']['ad-context'], 'is_numeric' ); |
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.
ここでグローバル変数に際割り当てしているのは、WordPressのデフォルトの保存機能(タームを保存する機能)を使っているからですか?
であれば、必要ないかと思いました。
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.
@fumikito この関数は、他の save_post
アクションの前に、優先度9で実行されます。これは、非表示の入力フィールドから送信された空の文字列をフィルタリングするためです。おそらく不要かもしれませんが、WordPressが ad-context
内で予期しない値をどう処理するか確認できなかったため、安全のために追加しました。こうしておけば、優先度10で実行される save_post
では、ad-context
には有効なIDのみが含まれることになります。そういう意図でしたが、おそらく削除しても問題ありません。
$_POST['tax_input']['ad-context']
に含まれる数値以外の値をフィルタリングする処理を追加しました。この通知は
add_action( 'save_post', function ( $post_id, $post ) {
内でset_transient()
を使って表示され、tax_input
の検証はtaf_validate_tax_input()
によって行われます。上記の画像のように、あるポジションに複数の "Available Contexts"(例: "Body Open")が設定されている場合にバグが発生することを確認しました。このバグは、JavaScript が各ポジションに対して1つのコンテキストのみを想定していたために起こり、複数のコンテキストがあると比較処理が壊れてしまっていました。
以前は、
$_POST['tax_input']['ad-context']
の値が[3, 16, 'empty', 27, 4, 'empty']
のようになることがありました。これによって問題が発生したことは今のところありませんが、安全性を考慮してこれらの値をフィルタリングし、hidden input の値を "empty" ではなく空文字 "" に変更することにしました。