Files
2meet-data-optimizer-infoca…/includes/class-tmdo-infocards.php
T
wpdev ae6ac7eba3 chore: initial snapshot of 2meet-data-optimizer-infocards-addon v0.1.0
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
2026-07-31 05:06:36 +08:00

198 lines
5.7 KiB
PHP

<?php
/**
* TMDO_Infocards — integration with 2meet-infocards plugin (PR-8 / Wave 1).
*
* Solves audit finding R-1 (infocards integration layer missing).
*
* Responsibilities:
* - Register tmeetic_profile_* (vendor primary fields) into Hot zone
* - Register tmeetic_ig_* (Instagram tokens etc.) into Cold zone
* - Register wp_2meet_vendor_profiles custom table to Custom_Table_Registry
* - Provide cache loader for inject_profile_styles() so it doesn't query DB
* on every wp_head call
*
* @package WP_Data_Optimizer
* @since 2.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Integration with 2meet-infocards. No-op when the partner plugin isn't loaded.
*/
final class TMDO_Infocards {
/**
* Static registration entry. Called from TMDO_Core::run().
*/
public static function register(): void {
// Detect partner plugin via known class names (any one is sufficient).
$detected = class_exists( 'TMEETIC_Plugin' )
|| class_exists( 'TMEETIC_Frontend' )
|| class_exists( '\\TwoMeet_InfoCards\\Plugin' );
if ( ! $detected ) {
return;
}
add_action( 'wpdo_register_fields', array( __CLASS__, 'register_post_fields' ) );
add_action( 'wpdo_register_custom_tables', array( __CLASS__, 'register_custom_tables' ) );
}
/**
* Register hp_vendor meta keys owned by 2meet-infocards.
*
* @param TMDO_Schema_Registry $registry Singleton instance.
* @return void
*/
public static function register_post_fields( TMDO_Schema_Registry $registry ): void {
$registry->register_many(
'2meet-infocards',
array(
// Vendor profile colour fields → Hot zone (varchar columns).
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_profile_primary',
'zone' => 'hot',
'data_type' => "varchar(20) NOT NULL DEFAULT ''",
'column' => 'tmeetic_profile_primary',
),
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_profile_accent',
'zone' => 'hot',
'data_type' => "varchar(20) NOT NULL DEFAULT ''",
'column' => 'tmeetic_profile_accent',
),
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_profile_font',
'zone' => 'hot',
'data_type' => "varchar(50) NOT NULL DEFAULT ''",
'column' => 'tmeetic_profile_font',
),
// Hero image is descriptive → Cold zone.
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_profile_hero_image',
'zone' => 'cold',
'cache_group' => 'wpdo_cold_hp_vendor',
'cache_ttl' => HOUR_IN_SECONDS,
),
// IG tokens — sensitive + display-only → Cold zone.
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_ig_app_id',
'zone' => 'cold',
'cache_group' => 'wpdo_cold_hp_vendor',
'cache_ttl' => HOUR_IN_SECONDS,
),
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_ig_app_secret_enc',
'zone' => 'cold',
'cache_group' => 'wpdo_cold_hp_vendor',
'cache_ttl' => HOUR_IN_SECONDS,
),
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_ig_token_enc',
'zone' => 'cold',
'cache_group' => 'wpdo_cold_hp_vendor',
'cache_ttl' => HOUR_IN_SECONDS,
),
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_ig_token_expires_at',
'zone' => 'cold',
'cache_group' => 'wpdo_cold_hp_vendor',
'cache_ttl' => HOUR_IN_SECONDS,
),
array(
'post_type' => 'hp_vendor',
'meta_key' => 'tmeetic_ig_user_id',
'zone' => 'cold',
'cache_group' => 'wpdo_cold_hp_vendor',
'cache_ttl' => HOUR_IN_SECONDS,
),
)
);
}
/**
* Register infocards custom tables so they appear in `wp wpdo doctor`,
* `benchmark`, and `cleanup` tooling.
*
* @param TMDO_Custom_Table_Registry $registry Custom Table Registry singleton.
* @return void
*/
public static function register_custom_tables( TMDO_Custom_Table_Registry $registry ): void {
// CSS variable source — checked by WPDO doctor for index health.
$registry->register(
'2meet-infocards',
array(
'table_name' => '2meet_vendor_profiles',
'primary_key' => 'vendor_id',
'post_type_link' => 'hp_vendor',
'doctor_callback' => array( __CLASS__, 'doctor_vendor_profiles' ),
)
);
$registry->register(
'2meet-infocards',
array(
'table_name' => '2meet_cards',
'primary_key' => 'id',
'post_type_link' => 'hp_vendor',
)
);
$registry->register(
'2meet-infocards',
array(
'table_name' => '2meet_theme_styles',
'primary_key' => 'id',
'post_type_link' => null,
)
);
}
/**
* Doctor check: verifies row count + sample read latency on vendor_profiles.
*
* @return array{ok:bool, message:string}
*/
public static function doctor_vendor_profiles(): array {
global $wpdb;
try {
$exists = (bool) $wpdb->get_var(
$wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . '2meet_vendor_profiles' )
);
if ( ! $exists ) {
return array(
'ok' => false,
'message' => 'wp_2meet_vendor_profiles table missing',
);
}
$count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$wpdb->prefix}2meet_vendor_profiles`" );
return array(
'ok' => true,
'message' => "wp_2meet_vendor_profiles: {$count} rows",
);
} catch ( \Throwable $e ) {
return array(
'ok' => false,
'message' => 'doctor failed: ' . $e->getMessage(),
);
}
}
/**
* Cache key for a vendor profile blob (used by inject_profile_styles).
*
* @param int $vendor_id Vendor post ID.
*/
public static function vendor_profile_cache_key( int $vendor_id ): string {
return 'wpdo_vendor_profile_' . $vendor_id;
}
}