Skip to content

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Conversation

TSMarkWalters
Copy link
Contributor

  • 各広告ポジションのコンテキスト要件がすべて満たされていない場合、送信時に通知を表示するようにしました。
  • エディタのバグを修正:複数のコンテキストを必要とするポジションで、コンテキストが表示されない問題を解決しました。
  • $_POST['tax_input']['ad-context'] に含まれる数値以外の値をフィルタリングする処理を追加しました。
taro-ad-fields-context-notice

この通知は add_action( 'save_post', function ( $post_id, $post ) { 内で set_transient() を使って表示され、tax_input の検証は taf_validate_tax_input() によって行われます。

/**
 * 各広告ポジションのコンテキスト要件がすべて満たされているかを検証します。
 *
 * 各ポジションに必要なコンテキスト(タームメタとして保存されている)について、
 * 送信された ad-content のコンテキストIDのうち少なくとも1つが
 * 親コンテキストとして一致しているかを確認します。
 *
 * @param array $tax_input タクソノミーの入力配列
 * @return bool すべての必須コンテキストに一致するものがあれば true を返します。
 */
function taf_validate_tax_input( $tax_input ) {
   ...
}

上記の画像のように、あるポジションに複数の "Available Contexts"(例: "Body Open")が設定されている場合にバグが発生することを確認しました。このバグは、JavaScript が各ポジションに対して1つのコンテキストのみを想定していたために起こり、複数のコンテキストがあると比較処理が壊れてしまっていました。

以前は、$_POST['tax_input']['ad-context'] の値が [3, 16, 'empty', 27, 4, 'empty'] のようになることがありました。これによって問題が発生したことは今のところありませんが、安全性を考慮してこれらの値をフィルタリングし、hidden input の値を "empty" ではなく空文字 "" に変更することにしました。

@TSMarkWalters TSMarkWalters requested a review from fumikito June 25, 2025 07:18
@fumikito fumikito requested a review from Copilot June 26, 2025 04:34
Copy link

@Copilot Copilot AI left a 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 before explode().
		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) &&
Copy link
Preview

Copilot AI Jun 26, 2025

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.

Suggested change
$(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 () {
Copy link
Preview

Copilot AI Jun 26, 2025

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.

Suggested change
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="" />';
Copy link
Preview

Copilot AI Jun 26, 2025

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(''));

Suggested change
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.

Copy link
Contributor

@fumikito fumikito left a 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' );
Copy link
Contributor

Choose a reason for hiding this comment

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

ここでグローバル変数に際割り当てしているのは、WordPressのデフォルトの保存機能(タームを保存する機能)を使っているからですか?
であれば、必要ないかと思いました。

Copy link
Contributor Author

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のみが含まれることになります。そういう意図でしたが、おそらく削除しても問題ありません。

@fumikito fumikito linked an issue Jun 26, 2025 that may be closed by this pull request
@TSMarkWalters TSMarkWalters requested a review from fumikito June 30, 2025 01:12
@TSMarkWalters
Copy link
Contributor Author

ad-position エディターに Contexts が表示されていないことに気づいたので、追加しました。これで、ユーザーはテーマ内でポジション用のアクションフックを呼び出すだけで、残りはUIから操作できるようになりました。

また、コンテキストを設定しないとこれらの広告は表示されないため、混乱を避けるために「Available Contexts」を「Required Contexts」に変更しました。

考えられるバグ
位置フックがテーマに追加され、すべて正常に動作しているにもかかわらず、「This position is not registered for themes」というメッセージが表示されます。

taro-ad-fields_required_contexts

@TSMarkWalters
Copy link
Contributor Author

新しい警告メッセージのスクリーンショット。

taro-ad-fields_warning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Contextsの使いやすさの向上
2 participants