arraypress/cpt-inline-list-table

一个WordPress插件库,提供了一种简单的方式来增强管理界面,包括可排序、可复制的自定义帖子类型列表表,用于条件规则、通知等。

1.0.1 2024-03-30 14:43 UTC

This package is auto-updated.

Last update: 2024-10-01 00:08:48 UTC


README

通过利用自定义帖子类型内联列表表库,开发者可以轻松注册、显示和管理自定义帖子类型,同时提供增强功能,如自定义列、脚本和样式队列、管理菜单高亮显示等。该工具旨在与WordPress核心功能无缝集成,简化与自定义帖子类型相关的管理任务,为开发者提供定制和管理方面的友好方法。

主要功能

  • 自定义列管理:定义和管理您帖子类型的列表表的自定义列,提供定制的数据视图。
  • 脚本和样式队列:指定应加载自定义脚本和样式的管理屏幕,增强管理UI和UX。
  • 管理菜单高亮显示:在管理侧边栏中保持菜单和子菜单的高亮显示,改善导航和可用性。
  • 灵活的分页:控制列表表中每页显示的项目数量,支持高效的数据处理和展示。
  • 简化集成:利用钩子和WordPress核心功能实现平滑集成,遵循WordPress标准和最佳实践。

此库有助于创建自定义帖子类型的精细管理界面,确保在WordPress管理中提供一致和高效的管理体验。

安装

确保您的项目已安装该包。如果没有,您通常可以使用Composer来包含它

composer require arraypress/cpt-inline-list-table

示例用法

register_inline_table函数允许您轻松设置和配置自定义帖子类型的内联列表表。以下是使用方法:

// Example usage of register_inline_table_post_type to create a 'Conditional Fee' custom post type.
register_inline_post_type(
    'conditional_fee',                                  // The key for the custom post type.
    __( 'Conditional Fee', 'edd-conditional-fees' ),    // The singular name of the custom post type for labels.
    __( 'Conditional Fees', 'edd-conditional-fees' ),   // The plural name of the custom post type for labels.
    'conditional_fee',                                  // The slug for the custom post type.
    [ 'excerpt', 'custom-fields', 'editor' ],           // (Optional) Additional features the post type supports.
    false                                               // (Optional) Whether to expose this post type in the WordPress REST API. Enables use of the Gutenberg editor and REST API queries.
);

/**
 * Defines columns for the list table of a custom post type, showcasing conditional discounts.
 * This configuration automatically includes default columns such as title, date, and author.
 * Additional custom columns can specify callbacks for rendering their content or use formatters
 * for specific data presentation. If only a key and label are provided (without a callback),
 * the system will first look for a matching property in the post object, then check post meta.
 *
 * When defining columns, it's crucial to understand the built-in logic for data retrieval:
 *
 * 1. If a 'callback' is provided, it will be used to fetch and render the column's data.
 * 2. Without a 'callback', the system searches for a matching property within the post object.
 * 3. If not found in the post object, the system then searches the post meta.
 * 4. A 'formatter' function can be used to format the value obtained from the callback or automatic data retrieval.
 *
 * This approach provides flexibility in displaying both standard and custom data within your list table.
 */

$columns = [
	// Example of a custom column with a callback and formatter.
	'amount'          => [
		'label'     => __( 'Amount', 'edd-conditional-fees' ),
		'callback'  => function ( $post ) {
			return get_post_meta( $post->ID, 'amount', true );
		},
		'formatter' => function ( $value, $post ) {
			return edd_currency_filter( edd_format_amount( $value ) );
		},
	],
	// Example of a simple column that relies on automatic data sourcing.
	'expiration_date' => [
		'label' => __( 'Expiration Date', 'edd-conditional-fees' ),
		// No callback needed; the system will automatically search for 'expiration_date' in post object or meta.
	]
];

// Registers an inline list table for a specified custom post type, configuring it with
// custom columns, administrative URLs, and settings for menu highlighting.
register_inline_table(
	'conditional_fee', // The custom post type identifier.
	$columns, // Associative array of columns with render callbacks and formatters.
	'edd_conditional_fees_table', // Hook name to attach the list table initialization.
	10, // Priority for the hook to control when the list table is initialized.
	'edit.php?post_type=download&page=edd-settings&tab=extensions', // URL for admin redirects.
	[ 'download_page_edd-settings' ], // Admin screens where scripts/styles should be enqueued.
	'edit.php?post_type=download', // Parent file slug for menu highlighting.
	'edd-settings' // Submenu file slug for submenu highlighting.
);

// Registers a settings section for managing conditional fees within the extension settings.
function register_section( array $sections ): array {
	$sections['conditional_fees'] = __( 'Conditional Fees', 'edd-conditional-fees' );

	return $sections;
}

add_filter( 'edd_settings_sections_extensions', __NAMESPACE__ . '\\register_section' );

// Adds settings for the 'Conditional Fees' section within the extension settings, enabling the configuration of rules.
function register_settings( array $existing_settings ): array {
	return array_merge( $existing_settings, [
		'conditional_fees' => [
			[
				'id'   => 'conditional_fees_table',
				'name' => __( 'Conditional Fees', 'edd-conditional-fees' ),
				'type' => 'hook',
			],
		]
	] );
}

add_filter( 'edd_settings_extensions', __NAMESPACE__ . '\\register_settings' );

贡献

我们欢迎对库的功能和兼容性进行贡献。请随意在我们的GitHub仓库提交拉取请求或报告问题。

许可证

自定义帖子类型内联列表表库是开源软件,根据GPL-2.0-or-later许可证发布。它是免费的个人和商业用途软件,遵守GNU通用公共许可证的条款。