Files
2meet-data-optimizer-latepo…/includes/class-tmdo-latepoint-bootstrap.php
T
wpdev 8393b213ae
Tests / PHP Lint (pull_request) Successful in 7s
chore(2meet-data-optimizer-latepoint-addon): add hpct_latepoint_idx install
- Create hpct_latepoint_idx via dbDelta keyed on a version option, so
  git-pull deploys also get the table (interceptor wrote to it blindly)
- Register the table with TMDO_Custom_Table_Registry; fix indexes key
- Bump version to 0.1.1

Verify: php -l includes/*.php; git status --short clean
PR: PR-001
2026-09-25 20:23:28 +08:00

128 lines
4.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Bootstrap for 2meet Data Optimizer LatePoint AddOn.
*
* @package TMDO_LATEPOINT
* @since 0.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class TMDO_Latepoint_Bootstrap {
const DB_VERSION_OPTION = 'tmdo_latepoint_db_version';
const DB_VERSION = '0.1.0';
public static function init(): void {
if ( ! class_exists( 'TMDO_API' ) && ! class_exists( 'WPDO_API' ) ) {
add_action( 'admin_notices', array( __CLASS__, 'missing_core_notice' ) );
return;
}
load_plugin_textdomain(
'tmdo-latepoint',
false,
dirname( plugin_basename( TMDO_LATEPOINT_FILE ) ) . '/languages'
);
self::maybe_install_table();
add_action( 'wpdo_register_custom_tables', array( __CLASS__, 'register_custom_tables' ) );
require_once TMDO_LATEPOINT_PATH . 'includes/class-tmdo-latepoint-interceptor.php';
if ( class_exists( 'TMDO_LatePoint_Interceptor' ) ) {
( new TMDO_LatePoint_Interceptor() )->register_hooks();
}
}
public static function missing_core_notice(): void {
echo '<div class="notice notice-error"><p>';
echo esc_html__( '2meet Data Optimizer LatePoint AddOn 需要 2meet-data-optimizer 核心外掛,請先安裝並啟用。', 'tmdo-latepoint' );
echo '</p></div>';
}
/**
* 建立 wp_hpct_latepoint_idx 表(若尚未存在或版本落後)。
*
* 這張表在整個 wp-data-optimizer / 2meet-data-optimizer 系譜中從未有過
* CREATE TABLE 定義——TMDO_LatePoint_Interceptor::sync_booking() 一直對著
* 一張可能不存在的表做 INSERT/UPDATE(外包在 try/catch 內靜默失敗)。
* 用 dbDelta version option(而非只掛 register_activation_hook)確保即使
* 外掛是透過 git pull 部署、未經停用/啟用循環也能補建表。
*/
public static function maybe_install_table(): void {
if ( get_option( self::DB_VERSION_OPTION ) === self::DB_VERSION ) {
return;
}
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$table = class_exists( 'TMDO_DB' ) ? TMDO_DB::table( 'hpct_latepoint_idx' ) : $wpdb->prefix . 'hpct_latepoint_idx';
$charset = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$table} (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
booking_id BIGINT NOT NULL,
agent_id BIGINT NOT NULL DEFAULT 0,
service_id BIGINT NOT NULL DEFAULT 0,
customer_id BIGINT NOT NULL DEFAULT 0,
status VARCHAR(50) NOT NULL DEFAULT '',
start_date VARCHAR(20) DEFAULT NULL,
end_date VARCHAR(20) DEFAULT NULL,
start_time INT NOT NULL DEFAULT 0,
end_time INT NOT NULL DEFAULT 0,
price VARCHAR(20) NOT NULL DEFAULT '0',
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY booking_id (booking_id),
KEY agent_id (agent_id),
KEY service_id (service_id),
KEY customer_id (customer_id)
) {$charset};";
dbDelta( $sql );
update_option( self::DB_VERSION_OPTION, self::DB_VERSION, false );
}
/**
* 註冊 wp_hpct_latepoint_idx 到 TMDO Custom Table Registry,
* 讓 `wp wpdo doctor`/`benchmark`/`cleanup` 能治理此表。
*
* @param mixed $registry TMDO_Custom_Table_Registry instance(loose typing 容錯舊版別名)。
*/
public static function register_custom_tables( $registry ): void {
if ( ! is_object( $registry ) || ! method_exists( $registry, 'register' ) ) {
return;
}
$registry->register(
'2meet-data-optimizer-latepoint-addon',
array(
'table_name' => 'hpct_latepoint_idx',
'primary_key' => 'id',
'post_type_link' => null,
'expected_columns' => array(
'id' => 'BIGINT',
'booking_id' => 'BIGINT',
'agent_id' => 'BIGINT',
'service_id' => 'BIGINT',
'customer_id' => 'BIGINT',
'status' => 'VARCHAR',
'start_date' => 'VARCHAR',
'end_date' => 'VARCHAR',
'start_time' => 'INT',
'end_time' => 'INT',
'price' => 'VARCHAR',
'created_at' => 'DATETIME',
),
'indexes' => array(
'booking_id' => array( 'booking_id' ),
'agent_id' => array( 'agent_id' ),
'service_id' => array( 'service_id' ),
'customer_id' => array( 'customer_id' ),
),
)
);
}
}