| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: wpdatatables/get-changelog |
| 4 |
* |
| 5 |
* Returns the wpDataTables plugin changelog for the installed version: |
| 6 |
* release date, new features, improvements, and bug fixes. |
| 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_get_changelog_ability' ); |
| 15 |
|
| 16 |
function wdtmcp_register_get_changelog_ability() { |
| 17 |
wp_register_ability( |
| 18 |
'wpdatatables/get-changelog', |
| 19 |
array( |
| 20 |
'label' => __( 'Get wpDataTables Changelog', 'wpdatatables' ), |
| 21 |
'description' => __( 'Returns the wpDataTables plugin changelog for the installed version: current version, release date, new features, improvements, and bug fixes. Also includes a link to the full public changelog. No parameters are required — pass an empty object {}. WHEN TO CALL: when the user asks what changed in the current version, what is new, or wants release notes. RETURNS: version, release_date, features[], improvements[], bugfixes[], full_changelog_url.', 'wpdatatables' ), |
| 22 |
'category' => 'wpdatatables-data', |
| 23 |
|
| 24 |
'input_schema' => array( |
| 25 |
'type' => 'object', |
| 26 |
'properties' => array(), |
| 27 |
'required' => array(), |
| 28 |
), |
| 29 |
|
| 30 |
'output_schema' => array( |
| 31 |
'type' => 'object', |
| 32 |
'properties' => array( |
| 33 |
'version' => array( 'type' => 'string', 'description' => 'Installed wpDataTables version.' ), |
| 34 |
'release_date' => array( 'type' => 'string', 'description' => 'Release date of the current version.' ), |
| 35 |
'features' => array( |
| 36 |
'type' => 'array', |
| 37 |
'description' => 'New features in this version.', |
| 38 |
'items' => array( |
| 39 |
'type' => 'object', |
| 40 |
'properties' => array( |
| 41 |
'text' => array( 'type' => 'string' ), |
| 42 |
'link' => array( 'type' => 'string' ), |
| 43 |
), |
| 44 |
), |
| 45 |
), |
| 46 |
'improvements' => array( |
| 47 |
'type' => 'array', |
| 48 |
'description' => 'Improvements in this version.', |
| 49 |
'items' => array( |
| 50 |
'type' => 'object', |
| 51 |
'properties' => array( |
| 52 |
'text' => array( 'type' => 'string' ), |
| 53 |
'link' => array( 'type' => 'string' ), |
| 54 |
), |
| 55 |
), |
| 56 |
), |
| 57 |
'bugfixes' => array( |
| 58 |
'type' => 'array', |
| 59 |
'description' => 'Bug fixes in this version.', |
| 60 |
'items' => array( |
| 61 |
'type' => 'object', |
| 62 |
'properties' => array( |
| 63 |
'text' => array( 'type' => 'string' ), |
| 64 |
'link' => array( 'type' => 'string' ), |
| 65 |
), |
| 66 |
), |
| 67 |
), |
| 68 |
'full_changelog_url' => array( 'type' => 'string', 'description' => 'URL to the full public changelog.' ), |
| 69 |
), |
| 70 |
), |
| 71 |
|
| 72 |
'execute_callback' => 'wdtmcp_execute_get_changelog', |
| 73 |
|
| 74 |
'permission_callback' => function () { |
| 75 |
return current_user_can( 'manage_options' ); |
| 76 |
}, |
| 77 |
|
| 78 |
'meta' => array( |
| 79 |
'annotations' => array( |
| 80 |
'instructions' => __( 'Call with an empty object {} to read release notes for the installed version.', 'wpdatatables' ), |
| 81 |
'readonly' => true, |
| 82 |
'destructive' => false, |
| 83 |
'idempotent' => true, |
| 84 |
), |
| 85 |
), |
| 86 |
) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Normalize changelog entries from WDTTools::getUpdateInfo() into a flat list. |
| 92 |
* |
| 93 |
* @param mixed $entries Raw features/improvements/bugfixes array. |
| 94 |
* @return array<int, array{text: string, link: string}> |
| 95 |
*/ |
| 96 |
function wdtmcp_normalize_changelog_entries( $entries ) { |
| 97 |
if ( ! is_array( $entries ) ) { |
| 98 |
return array(); |
| 99 |
} |
| 100 |
|
| 101 |
$normalized = array(); |
| 102 |
|
| 103 |
foreach ( $entries as $entry ) { |
| 104 |
if ( ! is_array( $entry ) || empty( $entry['text'] ) ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
$normalized[] = array( |
| 109 |
'text' => (string) $entry['text'], |
| 110 |
'link' => isset( $entry['link'] ) ? (string) $entry['link'] : '', |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
return $normalized; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Execute callback for wpdatatables/get-changelog. |
| 119 |
* |
| 120 |
* @return array|WP_Error |
| 121 |
*/ |
| 122 |
function wdtmcp_execute_get_changelog() { |
| 123 |
if ( ! class_exists( 'WDTTools' ) ) { |
| 124 |
return new \WP_Error( |
| 125 |
'wdtmcp_missing_class', |
| 126 |
__( 'wpDataTables core class WDTTools is not available.', 'wpdatatables' ) |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
$update_info = \WDTTools::getUpdateInfo(); |
| 131 |
|
| 132 |
if ( ! is_array( $update_info ) ) { |
| 133 |
return new \WP_Error( |
| 134 |
'wdtmcp_changelog_unavailable', |
| 135 |
__( 'Changelog data is not available.', 'wpdatatables' ) |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
$version = defined( 'WDT_CURRENT_VERSION' ) |
| 140 |
? (string) WDT_CURRENT_VERSION |
| 141 |
: (string) ( $update_info['version'] ?? 'unknown' ); |
| 142 |
|
| 143 |
return array( |
| 144 |
'version' => $version, |
| 145 |
'release_date' => isset( $update_info['release_date'] ) ? (string) $update_info['release_date'] : '', |
| 146 |
'features' => wdtmcp_normalize_changelog_entries( $update_info['features'] ?? array() ), |
| 147 |
'improvements' => wdtmcp_normalize_changelog_entries( $update_info['improvements'] ?? array() ), |
| 148 |
'bugfixes' => wdtmcp_normalize_changelog_entries( $update_info['bugfixes'] ?? array() ), |
| 149 |
'full_changelog_url' => 'https://wpdatatables.com/help/whats-new-changelog/', |
| 150 |
); |
| 151 |
} |
| 152 |
|