WP PLUGIN 

・選択カスタムフィールドをオートコンプリートに変更
This commit is contained in:
2021-06-26 16:23:35 +09:00
parent b69c5fbd88
commit 693752d71a
7 changed files with 1374 additions and 23 deletions
+2 -1
View File
@@ -5,7 +5,7 @@ Plugin URI: https://develop.n-k-y.net/wordpress/wp_plugin/apop/
Author: NBK45 Author: NBK45
Author URI: https://develop.n-k-y.net Author URI: https://develop.n-k-y.net
Description: 通常表示、検索表示、タクソノミー毎に投稿の表示順を設定するプラグイン Description: 通常表示、検索表示、タクソノミー毎に投稿の表示順を設定するプラグイン
Version: 1.1.2 Version: 1.1.3
License: GPLv2 License: GPLv2
*/ */
@@ -37,6 +37,7 @@ new APOP_POST;
//CSS, JSの読み込み //CSS, JSの読み込み
add_action( 'admin_enqueue_scripts', 'apop_register_my_styles' ); add_action( 'admin_enqueue_scripts', 'apop_register_my_styles' );
function apop_register_my_styles() { function apop_register_my_styles() {
wp_enqueue_style( 'jq_ui_css', APOP_PLUGIN_URL . 'css/jquery-ui.css' );
wp_enqueue_style( 'hrc_post_style', APOP_PLUGIN_URL . 'css/apop-style.css' ); wp_enqueue_style( 'hrc_post_style', APOP_PLUGIN_URL . 'css/apop-style.css' );
wp_enqueue_script( 'jquery-ui-sortable' ); wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'post-sort-cat-order_js', APOP_PLUGIN_URL . 'js/apop-style.js' ); wp_enqueue_script( 'post-sort-cat-order_js', APOP_PLUGIN_URL . 'js/apop-style.js' );
+6 -3
View File
@@ -346,8 +346,11 @@ value="' . esc_html( $sort_num ) . '">
$name_meta_key = '_' . $name_key . '[' . $target_key . '][field][meta_key]'; $name_meta_key = '_' . $name_key . '[' . $target_key . '][field][meta_key]';
$name_value_type = '_' . $name_key . '[' . $target_key . '][field][value_type]'; $name_value_type = '_' . $name_key . '[' . $target_key . '][field][value_type]';
$name_custom_field_type = '_' . $name_key . '[' . $target_key . '][field][custom_field_type]'; $name_custom_field_type = '_' . $name_key . '[' . $target_key . '][field][custom_field_type]';
$custom_field_val_1 = $custom_field_type == '1' ? $meta_key : '';
$custom_field_val_2 = $custom_field_type == '2' ? $meta_key : '';;
echo '<div class="sort-custom-field"> echo '<div class="sort-custom-field">
<div class="sort-custom-field-types"><div class="sort-custom-field-inner-label"> <div class="sort-custom-field-types">
<div class="sort-custom-field-inner-label">
<label> <label>
<input type="radio" <input type="radio"
class="custom-field-type" class="custom-field-type"
@@ -359,8 +362,8 @@ value="' . esc_html( $sort_num ) . '">
name="' . esc_attr( $name_custom_field_type ) . '" name="' . esc_attr( $name_custom_field_type ) . '"
value="2"' . esc_attr( self::set_search_normal_checked( $custom_field_type, '2' ) ) . '>追加</label> value="2"' . esc_attr( self::set_search_normal_checked( $custom_field_type, '2' ) ) . '>追加</label>
</div>'; </div>';
self::set_all_customfield_select( $name_meta_key, $meta_key ); echo '<input type="text" id="select-custom-field-input" class="custom_field_key_select" name="' . esc_attr( $name_meta_key ) . '" . value="' . esc_attr( $custom_field_val_1 ) . '" required>
echo '<input type="text" class="custom_field_key" name="' . esc_attr( $name_meta_key ) . '" value="' . esc_attr( $meta_key ) . '" required> <input type="text" class="custom_field_key" name="' . esc_attr( $name_meta_key ) . '" value="' . esc_attr( $custom_field_val_2 ) . '" required>
</div> </div>
<div class="sort-custom-field-inner"> <div class="sort-custom-field-inner">
<div class="sort-custom-field-inner-label">値タイプ:</div> <div class="sort-custom-field-inner-label">値タイプ:</div>
+4 -1
View File
@@ -6,15 +6,18 @@ if ( ! defined( 'ABSPATH' ) ) {
if ( ! class_exists( 'APOP' ) ) { if ( ! class_exists( 'APOP' ) ) {
require_once __DIR__ . '/../util/apop-order-setting.php'; require_once __DIR__ . '/../util/apop-order-setting.php';
require_once __DIR__ . '/../util/apop-customfield-select.php';
class APOP { class APOP {
use APOP_ORDER_SETTING; use APOP_ORDER_SETTING, CUSTOMFIELD_SELECT;
const TEMPLATE_DIR = __DIR__ . '/../template/'; const TEMPLATE_DIR = __DIR__ . '/../template/';
public function __construct() { public function __construct() {
add_action( 'admin_menu', array( $this, 'add_pages' ) ); add_action( 'admin_menu', array( $this, 'add_pages' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'set_custom_field_ajax' ) );
add_action( 'wp_ajax_set_custom_field', array( $this, 'set_custom_field' ) );
} }
public function add_pages() { public function add_pages() {
+1312
View File
File diff suppressed because it is too large Load Diff
+21
View File
@@ -0,0 +1,21 @@
jQuery(function ($) {
$('#select-custom-field-input').autocomplete({
source: function (req, resp) {
$.ajax({
type: 'GET',
url: localize.ajax_url,
dataType: 'json',
data: {
action: localize.action,
param: req.term
}
}).done(function (o) {
resp(o);
}).fail(function () {
resp(['']);
});
},
autoFocus: true,
minLength: 2
});
});
+10 -3
View File
@@ -4,7 +4,7 @@ Tags: 投稿,表示順,投稿表示順,カテゴリー,タグ,カスタム分類
Requires at least: 4.9 Requires at least: 4.9
Tested up to: 5.7.2 Tested up to: 5.7.2
Requires PHP: 7.0 Requires PHP: 7.0
Stable tag: 1.1.2 Stable tag: 1.1.3
License: GPLv2 or later License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html License URI: https://www.gnu.org/licenses/gpl-2.0.html
@@ -45,8 +45,12 @@ License URI: https://www.gnu.org/licenses/gpl-2.0.html
== Screenshots == == Screenshots ==
screenshot-1.png
screenshot-2.png
screenshot-3.png
screenshot-4.png
screenshot-5.png
screenshot-6.png
== Changelog == == Changelog ==
@@ -69,5 +73,8 @@ License URI: https://www.gnu.org/licenses/gpl-2.0.html
= 1.1.2 = = 1.1.2 =
選択カスタムフィールドを読み取り専用表示するよう修正 選択カスタムフィールドを読み取り専用表示するよう修正
= 1.1.3 =
選択カスタムフィールドをオートコンプリートに変更
== Upgrade Notice == == Upgrade Notice ==
No information No information
+19 -15
View File
@@ -7,25 +7,29 @@ if ( ! trait_exists( 'CUSTOMFIELD_SELECT' ) ) {
trait CUSTOMFIELD_SELECT { trait CUSTOMFIELD_SELECT {
public static function set_all_customfield_select( $name_meta_key, $meta_key ) { public function set_custom_field_ajax() {
$selects = self::get_all_custom_fields(); $handle = 'custom_field_ajax';
echo '<select class="custom_field_key_select" name="' . esc_attr( $name_meta_key ) . '" required>'; wp_register_script( $handle, APOP_PLUGIN_URL . 'js/custom_field.js', [ 'jquery' ], '', true );
foreach ( $selects as $select ) { $localize = [
if ( $select->meta_key == $meta_key ) { 'ajax_url' => admin_url( 'admin-ajax.php' ),
$selected = ' selected'; 'action' => 'set_custom_field',
} else { ];
$selected = ''; wp_localize_script( $handle, 'localize', $localize );
} wp_enqueue_script( $handle );
echo '<option value="' . esc_attr( $select->meta_key ) . '"' . esc_attr( $selected ) . '>' . esc_html( $select->meta_key ) . '</option>';
}
echo '</select>';
} }
private static function get_all_custom_fields() { public function set_custom_field() {
$param = filter_input( INPUT_GET, 'param', FILTER_SANITIZE_STRING );
echo json_encode( $this->get_custom_fields_by_param( $param ) );
die();
}
private function get_custom_fields_by_param( $param ) {
global $wpdb; global $wpdb;
$stmnt = "SELECT DISTINCT meta_key AS value, meta_key as label FROM $wpdb->postmeta WHERE meta_key LIKE %s AND meta_key NOT LIKE %s";
//アンダーバーで始まるシステム系のパラメータは除外する return $wpdb->get_results( $wpdb->prepare( $stmnt, $wpdb->esc_like( $param ) . '%', $wpdb->esc_like( '_%' ) ) );
return $wpdb->get_results( 'SELECT DISTINCT meta_key FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key NOT LIKE ' . "'\_%' ORDER BY meta_key ASC" );
} }
} }
} }