
当我们需要构建一个预约交友平台时,选择合适的CMS系统至关重要。WordPress凭借其强大的生态和灵活性成为理想选择。本文将基于WordPress CMS,通过源码解析和API调用,实现用户预约交友功能的核心代码实现。
WordPress预约交友功能实现方案
用户预约功能的核心在于创建可预约的时间段管理、预约记录存储以及用户权限控制。以下是基于WordPress实现的完整解决方案:
首先,我们需要创建自定义预约表单,通过ACF插件扩展WordPress功能:
function create预约表单_post_type() {
$labels = array(
'name' => _x('预约记录', 'Post Type General Name', 'text_domain'),
'singular_name' => _x('预约记录', 'Post Type Singular Name', 'text_domain'),
'menu_name' => __('预约管理', 'text_domain'),
'name_admin_bar' => __('预约', 'text_domain'),
'archives' => __('预约记录', 'text_domain'),
'attributes' => __('预约属性', 'text_domain'),
'parent_item_colon' => __('父级预约:', 'text_domain'),
'all_items' => __('所有预约', 'text_domain'),
'add_new_item' => __('添加预约', 'text_domain'),
'add_new' => __('添加预约', 'text_domain'),
'new_item' => __('新建预约', 'text_domain'),
'edit_item' => __('编辑预约', 'text_domain'),
'update_item' => __('更新预约', 'text_domain'),
'view_item' => __('查看预约', 'text_domain'),
'view_items' => __('查看预约', 'text_domain'),
'search_items' => __('搜索预约', 'text_domain'),
'not_found' => __('未找到预约', 'text_domain'),
'not_found_in_trash' => __('回收站中没有预约', 'text_domain'),
'featured_image' => __('特色图', 'text_domain'),
'set_featured_image' => __('设置特色图', 'text_domain'),
'remove_featured_image' => __('移除特色图', 'text_domain'),
'use_featured_image' => __('使用特色图', 'text_domain'),
'insert_into_item' => __('插入到预约', 'text_domain'),
'uploaded_to_this_item' => __('上传到预约', 'text_domain'),
'items_list' => __('预约列表', 'text_domain'),
'items_list_navigation' => __('预约列表导航', 'text_domain'),
'filter_items_list' => __('筛选预约列表', 'text_domain'),
);
$args = array(
'label' => __('预约', 'text_domain'),
'description' => __('管理用户预约记录', 'text_domain'),
'labels' => $labels,
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
'taxonomies' => array('post_tag'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-calendar',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type('预约记录', $args);
}
add_action('init', 'create预约表单_post_type', 0);
通过这段代码,我们创建了名为”预约记录”的自定义帖子类型,用于存储用户的预约信息。该类型支持标题、编辑器、缩略图和自定义字段。
接下来,我们需要为预约表单添加自定义字段,使用ACF插件实现以下字段:
[
{
"key": "预约时间",
"label": "预约时间",
"name": "预约时间",
"type": "time_picker",
"instructions": "选择预约的具体时间",
"required": 1
},
{
"key": "预约时长",
"label": "预约时长",
"name": "预约时长",
"type": "number",
"default": 60,
"instructions": "设置预约时长(分钟)"
},
{
"key": "预约状态",
"label": "预约状态",
"name": "预约状态",
"type": "select",
"default": "待确认",
"options" : {
"待确认": "待确认",
"已确认": "已确认",
"已完成": "已完成",
"已取消": "已取消"
}
},
{
"key": "预约用户",
"label": "预约用户",
"name": "预约用户",
"type": "user",
"instructions": "选择预约的用户"
}
]
这些字段将用于收集用户预约的具体时间、时长、状态以及预约者信息。
预约冲突检测与时间管理
在处理预约请求时,必须确保同一时间段内不会有多个预约冲突。以下是在预约前进行冲突检测的函数实现:
function 检测预约冲突($预约时间, $预约时长, $预约用户ID) {
$冲突 = false;
$预约记录 = new WP_Query(array(
'post_type' => '预约记录',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '预约时间',
'value' => array($预约时间, $预约时间 + $预约时长),
'compare' => 'BETWEEN',
),
array(
'key' => '预约状态',
'value' => array('待确认', '已确认', '已完成'),
'compare' => 'IN'
)
)
));
if ($预约用户ID) {
$预约记录->meta_query[] = array(
'key' => '预约用户',
'value' => $预约用户ID,
'compare' => '='
);
}
if ($预约记录->have_posts()) {
$冲突 = true;
}
wp_reset_postdata();
return $冲突;
}
这个函数会检查指定时间范围内是否有任何状态为”待确认”、”已确认”或”已完成”的预约。如果存在,则返回true表示冲突;否则返回false。
同时,我们需要创建预约确认的钩子函数,确保预约成功后更新状态:
add_action('预约记录保存后', '确认预约状态', 10, 3);
function 确认预约状态($预约ID, $预约数据, $更新) {
if (isset($预约数据['预约状态']) && $预约数据['预约状态'] === '已确认') {
update_post_meta($预约ID, '预约状态', '已确认');
}
}
预约API接口开发
为了实现预约交友平台的前端交互,我们需要开发REST API接口供前端调用。以下是基于WordPress REST API的预约接口实现:
/
注册预约API路由
/
function 注册预约API() {
register_rest_route('预约/v1', '/预约', array(
'methods' => 'POST',
'callback' => '处理预约请求',
'permission_callback' => function() {
return is_user_logged_in();
}
));
register_rest_route('预约/v1', '/预约/获取可用时间', array(
'methods' => 'GET',
'callback' => '获取可用预约时间',
'permission_callback' => function() {
return is_user_logged_in();
}
));
}
add_action('rest_api_init', '注册预约API');
处理预约请求的回调函数:
function 处理预约请求($请求) {
$用户ID = get_current_user_id();
$预约时间 = $请求->get_param('预约时间');
$预约时长 = $请求->get_param('预约时长');
// 检测冲突
if (检测预约冲突($预约时间, $预约时长, $用户ID)) {
return new WP_Error('预约冲突', '该时间段已被预约', array('status' => 409));
}
// 创建预约记录
$预约数据 = array(
'post_title' => '用户预约 - ' . get_user_meta($用户ID, '用户名', true),
'post_type' => '预约记录',
'post_status' => '待确认',
'post_author' => $用户ID,
'meta_input' => array(
'预约时间' => $预约时间,
'预约时长' => $预约时长,
'预约用户' => $用户ID
)
);
$预约ID = wp_insert_post($预约数据);
if (is_wp_error($预约ID)) {
return new WP_Error('创建失败', '预约创建失败', array('status' => 500));
}
return array(
'预约ID' => $预约ID,
'预约时间' => $预约时间,
'预约时长' => $预约时长,
'预约状态' => '待确认'
);
}
获取可用预约时间的接口:
function 获取可用预约时间($请求) {
$当前日期 = $请求->get_param('日期');
$可用时间段 = array();
// 查询已预约的时间段
$已预约 = new WP_Query(array(
'post_type' => '预约记录',
'posts_per_page' => -1,
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => $当前日期 . ' 00:00:00',
'before' => $当前日期 . ' 23:59:59'
)
)
));
$已预约时间 = array();
if ($已预约->have_posts()) {
while ($已预约->have_posts()) {
$已预约->the_post();
$已预约时间[] = get_post_meta(get_the_ID(), '预约时间', true);
}
}
// 生成可用时间段
$开始时间 = strtotime($当前日期 . ' 09:00:00');
$结束时间 = strtotime($当前日期 . ' 18:00:00');
$时间段间隔 = 60; // 每个时间段60分钟
for ($i = $开始时间; $i < $结束时间; $i += $时间段间隔) {
$时间段开始 = date('H:i', $i);
$时间段结束 = date('H:i', $i + $
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。