| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: wpdatatables/edit-table |
| 4 |
* |
| 5 |
* Single entry point for editing an existing wpDataTable: dispatches to Simple-table |
| 6 |
* content/layout updates or standard table display/column settings based on table_type. |
| 7 |
* |
| 8 |
* @package wpDataTables_MCP_Server |
| 9 |
* @since 0.2.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) or die('Access denied.'); |
| 13 |
|
| 14 |
add_action( 'wp_abilities_api_init', 'wdtmcp_register_edit_table_ability' ); |
| 15 |
|
| 16 |
function wdtmcp_register_edit_table_ability() { |
| 17 |
wp_register_ability( |
| 18 |
'wpdatatables/edit-table', |
| 19 |
array( |
| 20 |
'label' => __( 'Edit Table', 'wpdatatables' ), |
| 21 |
'description' => __( 'Edits an existing wpDataTable. Automatically applies the correct update path: Simple tables (table_type=simple) — cell content, merges, template, custom_css (same as update-simple-table-styles); all other types (CSV, Excel, SQL, etc.) — title, pagination, sorting, filtering, column types/labels (same as update-table-settings). REQUIRED: table_id. Load table type with get-table-info first. Pass only fields relevant to that table kind. RETURNS: table_id, updated[] (and custom_css for Simple when set).', 'wpdatatables' ), |
| 22 |
'category' => 'wpdatatables-data', |
| 23 |
|
| 24 |
'input_schema' => array( |
| 25 |
'type' => 'object', |
| 26 |
'properties' => array( |
| 27 |
'table_id' => array( |
| 28 |
'type' => 'integer', |
| 29 |
'description' => 'The wpDataTable ID to edit.', |
| 30 |
), |
| 31 |
'title' => array( |
| 32 |
'type' => 'string', |
| 33 |
'description' => 'New table title (non-Simple tables only).', |
| 34 |
), |
| 35 |
'sorting' => array( |
| 36 |
'type' => 'boolean', |
| 37 |
'description' => 'Enable column sorting (non-Simple).', |
| 38 |
), |
| 39 |
'filtering' => array( |
| 40 |
'type' => 'boolean', |
| 41 |
'description' => 'Enable filtering (non-Simple).', |
| 42 |
), |
| 43 |
'pagination' => array( |
| 44 |
'type' => 'boolean', |
| 45 |
'description' => 'Enable pagination (non-Simple).', |
| 46 |
), |
| 47 |
'responsive' => array( |
| 48 |
'type' => 'boolean', |
| 49 |
'description' => 'Responsive mode (non-Simple).', |
| 50 |
), |
| 51 |
'display_length' => array( |
| 52 |
'type' => 'integer', |
| 53 |
'description' => 'Rows per page (non-Simple).', |
| 54 |
), |
| 55 |
'server_side' => array( |
| 56 |
'type' => 'boolean', |
| 57 |
'description' => 'Server-side processing (non-Simple).', |
| 58 |
), |
| 59 |
'global_search' => array( |
| 60 |
'type' => 'boolean', |
| 61 |
'description' => 'Global search box (non-Simple).', |
| 62 |
), |
| 63 |
'columns' => array( |
| 64 |
'type' => 'array', |
| 65 |
'description' => 'Column updates for non-Simple tables; each entry needs orig_header from get-table-info.', |
| 66 |
'items' => array( |
| 67 |
'type' => 'object', |
| 68 |
'properties' => array( |
| 69 |
'orig_header' => array( 'type' => 'string' ), |
| 70 |
'type' => array( 'type' => 'string' ), |
| 71 |
'filter_type' => array( 'type' => 'string' ), |
| 72 |
'display_header' => array( 'type' => 'string' ), |
| 73 |
'visible' => array( 'type' => 'boolean' ), |
| 74 |
'width' => array( 'type' => 'string' ), |
| 75 |
'sorting' => array( 'type' => 'boolean' ), |
| 76 |
'text_before' => array( 'type' => 'string' ), |
| 77 |
'text_after' => array( 'type' => 'string' ), |
| 78 |
'prefix' => array( 'type' => 'string' ), |
| 79 |
'suffix' => array( 'type' => 'string' ), |
| 80 |
), |
| 81 |
), |
| 82 |
), |
| 83 |
'table_settings' => array( |
| 84 |
'type' => 'object', |
| 85 |
'description' => 'Simple table layout flags (template, stripes, etc.).', |
| 86 |
'properties' => array( |
| 87 |
'template_id' => array( 'type' => 'integer' ), |
| 88 |
'simple_header' => array( 'type' => 'boolean' ), |
| 89 |
'stripe_table' => array( 'type' => 'boolean' ), |
| 90 |
'responsive' => array( 'type' => 'boolean' ), |
| 91 |
'remove_borders' => array( 'type' => 'boolean' ), |
| 92 |
'cell_padding' => array( 'type' => 'integer' ), |
| 93 |
'show_title' => array( 'type' => 'boolean' ), |
| 94 |
'show_table_description' => array( 'type' => 'boolean' ), |
| 95 |
'table_description' => array( 'type' => 'string' ), |
| 96 |
), |
| 97 |
), |
| 98 |
'cell_updates' => array( |
| 99 |
'type' => 'array', |
| 100 |
'description' => 'Simple table: {row_index, col_index, data?, attachment_id?, meta?}.', |
| 101 |
), |
| 102 |
'merged_cells' => array( |
| 103 |
'type' => 'array', |
| 104 |
'description' => 'Simple table: merged cells {row, col, rowspan, colspan}; empty array clears.', |
| 105 |
), |
| 106 |
'custom_css' => array( |
| 107 |
'type' => 'string', |
| 108 |
'description' => 'Simple table: CSS with #wpdtSimpleTable-{table_id}; empty string clears.', |
| 109 |
), |
| 110 |
'row_heights' => array( |
| 111 |
'type' => 'array', |
| 112 |
'description' => 'Simple table: row heights in px per row index.', |
| 113 |
'items' => array( 'type' => 'integer' ), |
| 114 |
), |
| 115 |
), |
| 116 |
'required' => array( 'table_id' ), |
| 117 |
), |
| 118 |
|
| 119 |
'output_schema' => array( |
| 120 |
'type' => 'object', |
| 121 |
'properties' => array( |
| 122 |
'table_id' => array( 'type' => 'integer', 'description' => 'Edited table ID.' ), |
| 123 |
'updated' => array( |
| 124 |
'type' => 'array', |
| 125 |
'items' => array( 'type' => 'string' ), |
| 126 |
'description' => 'List of fields or cells changed.', |
| 127 |
), |
| 128 |
'custom_css' => array( |
| 129 |
'type' => 'string', |
| 130 |
'description' => 'Substituted CSS (Simple tables only, when custom_css was sent).', |
| 131 |
), |
| 132 |
), |
| 133 |
), |
| 134 |
|
| 135 |
'execute_callback' => 'wdtmcp_execute_edit_table', |
| 136 |
|
| 137 |
'permission_callback' => function () { |
| 138 |
return current_user_can( 'manage_options' ); |
| 139 |
}, |
| 140 |
|
| 141 |
'meta' => array( |
| 142 |
'annotations' => array( |
| 143 |
'instructions' => __( 'Call get-table-info for table_type. Simple → use cell_updates, table_settings, merged_cells, custom_css. Other types → use title, columns, sorting, etc.', 'wpdatatables' ), |
| 144 |
'readonly' => false, |
| 145 |
'destructive' => false, |
| 146 |
'idempotent' => false, |
| 147 |
), |
| 148 |
), |
| 149 |
) |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Execute wpdatatables/edit-table: route by table_type. |
| 155 |
* |
| 156 |
* @param array $input Same shape as update-table-settings or update-simple-table-styles. |
| 157 |
* @return array|\WP_Error |
| 158 |
*/ |
| 159 |
function wdtmcp_execute_edit_table( $input ) { |
| 160 |
global $wpdb; |
| 161 |
|
| 162 |
$table_id = isset( $input['table_id'] ) ? (int) $input['table_id'] : 0; |
| 163 |
if ( $table_id <= 0 ) { |
| 164 |
return new \WP_Error( |
| 165 |
'wdtmcp_invalid_input', |
| 166 |
__( 'table_id is required and must be a positive integer.', 'wpdatatables' ) |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
$table_type = $wpdb->get_var( |
| 171 |
$wpdb->prepare( |
| 172 |
"SELECT table_type FROM {$wpdb->prefix}wpdatatables WHERE id = %d", |
| 173 |
$table_id |
| 174 |
) |
| 175 |
); |
| 176 |
|
| 177 |
if ( null === $table_type ) { |
| 178 |
return new \WP_Error( |
| 179 |
'wdtmcp_not_found', |
| 180 |
sprintf( |
| 181 |
/* translators: %d: table ID */ |
| 182 |
__( 'Table with ID %d was not found.', 'wpdatatables' ), |
| 183 |
$table_id |
| 184 |
) |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
if ( 'simple' === $table_type ) { |
| 189 |
if ( ! function_exists( 'wdtmcp_execute_update_simple_table_styles' ) ) { |
| 190 |
return new \WP_Error( |
| 191 |
'wdtmcp_missing_handler', |
| 192 |
__( 'Simple table update handler is not loaded.', 'wpdatatables' ) |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
return wdtmcp_execute_update_simple_table_styles( $input ); |
| 197 |
} |
| 198 |
|
| 199 |
if ( ! function_exists( 'wdtmcp_execute_update_table_settings' ) ) { |
| 200 |
return new \WP_Error( |
| 201 |
'wdtmcp_missing_handler', |
| 202 |
__( 'Table settings update handler is not loaded.', 'wpdatatables' ) |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
return wdtmcp_execute_update_table_settings( $input ); |
| 207 |
} |
| 208 |
|