| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: wpdatatables/get-table-info |
| 4 |
* |
| 5 |
* Returns the full configuration of a single wpDataTable, |
| 6 |
* including column definitions, data-source details, and behavioural flags. |
| 7 |
* |
| 8 |
* Uses WDTConfigController::loadTableFromDB() to stay in sync with |
| 9 |
* wpDataTables' own config layer. |
| 10 |
* |
| 11 |
* @package wpDataTables_MCP_Server |
| 12 |
*/ |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) or die('Access denied.'); |
| 15 |
|
| 16 |
add_action( 'wp_abilities_api_init', 'wdtmcp_register_get_table_info_ability' ); |
| 17 |
|
| 18 |
function wdtmcp_register_get_table_info_ability() { |
| 19 |
wp_register_ability( |
| 20 |
'wpdatatables/get-table-info', |
| 21 |
array( |
| 22 |
'label' => __( 'Get wpDataTable Info', 'wpdatatables' ), |
| 23 |
'description' => __( 'Returns the full configuration of a single wpDataTable by ID, including title, source type, SQL query or file source, database connection, column definitions (name, type, label, visibility, position, filter type, sorting, text_before/text_after for prefix/suffix), and behavioural flags. Use this after list-tables to understand a table\'s structure before querying its data. WHEN TO CALL: before reading rows, changing settings, or creating a chart from a table — whenever you need column names (orig_header), types, filters, or whether server_side is enabled. REQUIRED INPUT: table_id (integer) from list-tables. RETURNS: full table config including columns[] with orig_header, display_header, type, filter_type, visible, server_side, pagination, sorting, filtering, and more. TYPICAL WORKFLOW: list-tables → get-table-info → get-table-data (use orig_header values for order_by and column_search). For Simple tables (table_type=simple), use create-simple-table / update-simple-table-styles instead of update-table-settings.', 'wpdatatables' ), |
| 24 |
'category' => 'wpdatatables-data', |
| 25 |
|
| 26 |
'input_schema' => array( |
| 27 |
'type' => 'object', |
| 28 |
'properties' => array( |
| 29 |
'table_id' => array( |
| 30 |
'type' => 'integer', |
| 31 |
'description' => 'The unique wpDataTable ID (as returned by list-tables).', |
| 32 |
), |
| 33 |
), |
| 34 |
'required' => array( 'table_id' ), |
| 35 |
), |
| 36 |
|
| 37 |
'output_schema' => array( |
| 38 |
'type' => 'object', |
| 39 |
'properties' => array( |
| 40 |
'id' => array( 'type' => 'integer', 'description' => 'Table ID.' ), |
| 41 |
'title' => array( 'type' => 'string', 'description' => 'Table title.' ), |
| 42 |
'description' => array( 'type' => 'string', 'description' => 'Optional table description.' ), |
| 43 |
'table_type' => array( 'type' => 'string', 'description' => 'Source type (mysql, csv, xls, google_spreadsheet, json, xml, serialized, simple, manual).' ), |
| 44 |
'connection' => array( 'type' => 'string', 'description' => 'Database connection identifier (empty for default WP connection).' ), |
| 45 |
'mysql_table_name' => array( 'type' => 'string', 'description' => 'Underlying MySQL table name (for SQL-based tables).' ), |
| 46 |
'content' => array( 'type' => 'string', 'description' => 'SQL query or file path / URL that provides the table data.' ), |
| 47 |
'server_side' => array( 'type' => 'boolean', 'description' => 'Whether server-side processing is enabled.' ), |
| 48 |
'editable' => array( 'type' => 'boolean', 'description' => 'Whether front-end editing is enabled.' ), |
| 49 |
'inline_editing' => array( 'type' => 'boolean', 'description' => 'Whether inline (cell) editing is enabled.' ), |
| 50 |
'filtering' => array( 'type' => 'boolean', 'description' => 'Whether column filtering is enabled.' ), |
| 51 |
'global_search' => array( 'type' => 'boolean', 'description' => 'Whether the global search box is shown.' ), |
| 52 |
'sorting' => array( 'type' => 'boolean', 'description' => 'Whether column sorting is enabled.' ), |
| 53 |
'responsive' => array( 'type' => 'boolean', 'description' => 'Whether responsive mode is enabled.' ), |
| 54 |
'pagination' => array( 'type' => 'boolean', 'description' => 'Whether pagination is enabled.' ), |
| 55 |
'display_length' => array( 'type' => 'integer', 'description' => 'Default number of rows per page.' ), |
| 56 |
'cache_source_data' => array( 'type' => 'boolean', 'description' => 'Whether source data caching is enabled.' ), |
| 57 |
'columns' => array( |
| 58 |
'type' => 'array', |
| 59 |
'description' => 'Column definitions ordered by position.', |
| 60 |
'items' => array( |
| 61 |
'type' => 'object', |
| 62 |
'properties' => array( |
| 63 |
'id' => array( 'type' => 'integer', 'description' => 'Column ID.' ), |
| 64 |
'orig_header' => array( 'type' => 'string', 'description' => 'Original (internal) column header / key.' ), |
| 65 |
'display_header' => array( 'type' => 'string', 'description' => 'Display label shown to users.' ), |
| 66 |
'type' => array( 'type' => 'string', 'description' => 'Column data type (string, int, float, date, datetime, time, link, email, image, formula, select).' ), |
| 67 |
'filter_type' => array( 'type' => 'string', 'description' => 'Filter widget type (none, text, number, number-range, date-range, select, checkbox, multiselect).' ), |
| 68 |
'visible' => array( 'type' => 'boolean', 'description' => 'Whether the column is visible by default.' ), |
| 69 |
'position' => array( 'type' => 'integer', 'description' => 'Column display position (0-based).' ), |
| 70 |
'sorting' => array( 'type' => 'boolean', 'description' => 'Whether sorting is enabled for this column.' ), |
| 71 |
'formula' => array( 'type' => 'string', 'description' => 'Calculation formula (for formula columns, empty otherwise).' ), |
| 72 |
'text_before' => array( 'type' => 'string', 'description' => 'Cell content prefix (e.g. $ for currency), shown before value.' ), |
| 73 |
'text_after' => array( 'type' => 'string', 'description' => 'Cell content suffix (e.g. % for percentages), shown after value.' ), |
| 74 |
'prefix' => array( 'type' => 'string', 'description' => 'Alias for text_before (cell content prefix).' ), |
| 75 |
'suffix' => array( 'type' => 'string', 'description' => 'Alias for text_after (cell content suffix).' ), |
| 76 |
), |
| 77 |
), |
| 78 |
), |
| 79 |
'column_count' => array( 'type' => 'integer', 'description' => 'Total number of columns.' ), |
| 80 |
), |
| 81 |
), |
| 82 |
|
| 83 |
'execute_callback' => 'wdtmcp_execute_get_table_info', |
| 84 |
|
| 85 |
'permission_callback' => function () { |
| 86 |
return current_user_can( 'manage_options' ); |
| 87 |
}, |
| 88 |
|
| 89 |
'meta' => array( |
| 90 |
'annotations' => array( |
| 91 |
'instructions' => __( 'Requires table_id from list-tables. Inspect columns.orig_header, table_type, and server_side before calling get-table-data, update-table-settings, or create-chart.', 'wpdatatables' ), |
| 92 |
'readonly' => true, |
| 93 |
'destructive' => false, |
| 94 |
'idempotent' => true, |
| 95 |
), |
| 96 |
), |
| 97 |
) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Execute callback for wpdatatables/get-table-info. |
| 103 |
* |
| 104 |
* @param array $input { table_id: int } |
| 105 |
* @return array|WP_Error |
| 106 |
*/ |
| 107 |
function wdtmcp_execute_get_table_info( $input ) { |
| 108 |
$table_id = isset( $input['table_id'] ) ? (int) $input['table_id'] : 0; |
| 109 |
|
| 110 |
if ( $table_id <= 0 ) { |
| 111 |
return new \WP_Error( |
| 112 |
'wdtmcp_invalid_input', |
| 113 |
__( 'A valid table_id (positive integer) is required.', 'wpdatatables' ) |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
if ( ! class_exists( 'WDTConfigController' ) ) { |
| 118 |
return new \WP_Error( |
| 119 |
'wdtmcp_missing_class', |
| 120 |
__( 'wpDataTables core class WDTConfigController is not available.', 'wpdatatables' ) |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
try { |
| 125 |
$table_data = \WDTConfigController::loadTableFromDB( $table_id ); |
| 126 |
} catch ( \Exception $e ) { |
| 127 |
return new \WP_Error( |
| 128 |
'wdtmcp_load_error', |
| 129 |
sprintf( |
| 130 |
__( 'Error loading table %d: %s', 'wpdatatables' ), |
| 131 |
$table_id, |
| 132 |
$e->getMessage() |
| 133 |
) |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
if ( empty( $table_data ) ) { |
| 138 |
return new \WP_Error( |
| 139 |
'wdtmcp_not_found', |
| 140 |
sprintf( __( 'Table with ID %d was not found.', 'wpdatatables' ), $table_id ) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
$columns = array(); |
| 145 |
if ( ! empty( $table_data->columns ) && is_array( $table_data->columns ) ) { |
| 146 |
foreach ( $table_data->columns as $col ) { |
| 147 |
$columns[] = array( |
| 148 |
'id' => isset( $col->id ) ? (int) $col->id : 0, |
| 149 |
'orig_header' => isset( $col->orig_header ) ? (string) $col->orig_header : '', |
| 150 |
'display_header' => isset( $col->display_header ) ? (string) $col->display_header : '', |
| 151 |
'type' => isset( $col->type ) ? (string) $col->type : '', |
| 152 |
'filter_type' => isset( $col->filter_type ) ? (string) $col->filter_type : 'none', |
| 153 |
'visible' => ! empty( $col->visible ), |
| 154 |
'position' => isset( $col->pos ) ? (int) $col->pos : 0, |
| 155 |
'sorting' => isset( $col->sorting ) ? ! empty( $col->sorting ) : true, |
| 156 |
'formula' => isset( $col->formula ) ? (string) $col->formula : '', |
| 157 |
'text_before' => isset( $col->text_before ) ? (string) $col->text_before : '', |
| 158 |
'text_after' => isset( $col->text_after ) ? (string) $col->text_after : '', |
| 159 |
'prefix' => isset( $col->text_before ) ? (string) $col->text_before : '', |
| 160 |
'suffix' => isset( $col->text_after ) ? (string) $col->text_after : '', |
| 161 |
); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
$table_type_raw = isset( $table_data->table_type ) ? $table_data->table_type : ''; |
| 166 |
$table_type = ( $table_type_raw === 'mysql' ) ? 'SQL' : $table_type_raw; |
| 167 |
|
| 168 |
$out = array( |
| 169 |
'id' => (int) $table_data->id, |
| 170 |
'title' => (string) $table_data->title, |
| 171 |
'description' => isset( $table_data->table_description ) ? (string) $table_data->table_description : '', |
| 172 |
'table_type' => $table_type, |
| 173 |
'connection' => isset( $table_data->connection ) ? (string) $table_data->connection : '', |
| 174 |
'mysql_table_name' => isset( $table_data->mysql_table_name ) ? (string) $table_data->mysql_table_name : '', |
| 175 |
'content' => isset( $table_data->content ) ? (string) $table_data->content : '', |
| 176 |
'server_side' => ! empty( $table_data->server_side ), |
| 177 |
'editable' => ! empty( $table_data->editable ), |
| 178 |
'inline_editing' => ! empty( $table_data->inline_editing ), |
| 179 |
'filtering' => ! empty( $table_data->filtering ), |
| 180 |
'global_search' => ! empty( $table_data->global_search ), |
| 181 |
'sorting' => ! empty( $table_data->sorting ), |
| 182 |
'responsive' => ! empty( $table_data->responsive ), |
| 183 |
'pagination' => ! empty( $table_data->pagination ), |
| 184 |
'display_length' => isset( $table_data->display_length ) ? (int) $table_data->display_length : 10, |
| 185 |
'cache_source_data' => ! empty( $table_data->cache_source_data ), |
| 186 |
'columns' => $columns, |
| 187 |
'column_count' => count( $columns ), |
| 188 |
); |
| 189 |
$stored_css = function_exists( 'wdtmcp_get_table_custom_css' ) ? wdtmcp_get_table_custom_css( $table_id ) : ''; |
| 190 |
if ( '' !== $stored_css ) { |
| 191 |
$out['custom_css'] = $stored_css; |
| 192 |
} |
| 193 |
return $out; |
| 194 |
} |
| 195 |
|