Skip to content
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

[ カスタム投稿タイプ設定 ] Menu Icon設定を追加 #1086

Merged
merged 7 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
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
79 changes: 79 additions & 0 deletions inc/post-type-manager/package/class.post-type-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,66 @@ public static function add_meta_box_action() {
echo '<input class="form-control" type="text" id="veu_menu_position" name="veu_menu_position" value="' . esc_attr( $post->veu_menu_position ) . '" size="30">';

echo '<hr>';

/*******************************************
* Menu Icon
*/
echo '<h4>' . esc_html__( 'Menu Icon(Optional)', 'vk-all-in-one-expansion-unit' ) . '</h4>';
echo '<p>' . esc_html__( 'Select an icon from the images below, or enter a custom Dashicon class.' ) . '</p>';

echo '<div style="margin-bottom: 1rem;">';
$icons = [
'dashicons-admin-post',
'dashicons-admin-site',
'dashicons-admin-users',
'dashicons-admin-media',
'dashicons-admin-comments',
'dashicons-admin-appearance',
'dashicons-welcome-write-blog',
'dashicons-dashboard',
'dashicons-admin-plugins',
'dashicons-admin-settings',
'dashicons-admin-network',
'dashicons-admin-home',
'dashicons-admin-generic',
'dashicons-admin-collapse',
];

foreach ($icons as $icon) {
echo '<button type="button" class="button" style="margin-right: 10px; margin-bottom: 10px; width: 40px; height: 40px; padding: 5px;" onclick="updateIconSelection(\'' . esc_attr($icon) . '\');">';
echo '<span class="dashicons ' . esc_attr($icon) . '" style="font-size: 20px; vertical-align: sub;"></span>';
echo '</button>';
}

echo '<div>';
echo '<input type="text" id="veu_menu_icon" name="veu_menu_icon" value="' . esc_attr($post->veu_menu_icon) . '" style="margin-right: 10px;" size="30">';
echo '<a href="https://developer.wordpress.org/resource/dashicons/" class="button" target="_blank">' . esc_html__('Dashicons Library', 'vk-all-in-one-expansion-unit') . '</a>';
echo '</div>';

echo '</div>';

echo '<hr>';

// JavaScript to update icon selection and validate input
echo '<script>
function updateIconSelection(icon) {
document.getElementById("veu_menu_icon").value = icon;
}

document.addEventListener("DOMContentLoaded", function () {
var inputField = document.getElementById("veu_menu_icon");

// `change` イベントを使用して、フォーカスが外れたときにチェックする
inputField.addEventListener("change", function() {
// SVGデータURI、\'none\'、または\'dashicons-\'で始まる値を許可
if (!this.value.startsWith("dashicons-") && !this.value.startsWith("data:image/svg+xml;base64,") && this.value !== \'none\') {
alert("' . __( 'Please enter a valid input. You can enter a Dashicon class, a base64-encoded SVG, or \'none\' to leave it blank for CSS customization.', 'vk-all-in-one-expansion-unit' ) . '");
this.value = ""; // 不正な入力をクリア
}
});
});
</script>';


/*******************************************
* Export to Rest api
Expand Down Expand Up @@ -270,6 +330,7 @@ public static function save_cf_value( $post_id ) {
'veu_post_type_id',
'veu_post_type_items',
'veu_menu_position',
'veu_menu_icon',
'veu_post_type_export_to_api',
'veu_post_type_rewrite',
'veu_taxonomy',
Expand Down Expand Up @@ -311,8 +372,21 @@ public static function add_post_type() {
);
$custom_post_types = get_posts( $args );
if ( $custom_post_types ) {

foreach ( $custom_post_types as $key => $post ) {

/*******************************************
* メニューアイコンを設定するためのコード
*/
$menu_icon = get_post_meta($post->ID, 'veu_menu_icon', true);
if (empty($menu_icon)) {
$menu_icon = 'dashicons-admin-post';
} elseif ($menu_icon === 'none') {
$menu_icon = ''; // CSSでスタイリング可能に
} elseif (!strpos($menu_icon, 'dashicons-') === 0 && !strpos($menu_icon, 'data:image/svg+xml;base64,') === 0) {
$menu_icon = 'dashicons-admin-post';
}

/*******************************************
* 投稿タイプ追加.
*/
Expand All @@ -329,6 +403,10 @@ public static function add_post_type() {
foreach ( $post_type_items as $key => $value ) {
$supports[] = $key;
}

// 投稿タイプのアイコンを取得
$menu_icon = get_post_meta($post->ID, 'veu_menu_icon', true);
$menu_icon = !empty($menu_icon) ? $menu_icon : 'dashicons-admin-post';

// カスタム投稿タイプのスラッグ.
$post_type_id = mb_strimwidth( mb_convert_kana( mb_strtolower( esc_html( get_post_meta( $post->ID, 'veu_post_type_id', true ) ) ), 'a' ), 0, 20, '', 'UTF-8' );
Expand Down Expand Up @@ -362,6 +440,7 @@ public static function add_post_type() {
'public' => true,
'has_archive' => true,
'menu_position' => $menu_position,
'menu_icon' => $menu_icon,
'supports' => $supports,
'rewrite' => $rewrite,
);
Expand Down
2 changes: 2 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ e.g.

== Changelog ==

[ Add setting ][ Post Type Manager ] Add a menu icon setting.

= 9.97.2 =
[ Bug fix ] In WordPress 6.5, a link to the plugin page has been added to the admin bar on the front end by default. Consequently, ExUnit has been modified to no longer add this link on the front end.

Expand Down
Loading