| 1 |
<?php |
| 2 |
/** |
| 3 |
* MCP Tools: OTTO Persistence Settings |
| 4 |
* |
| 5 |
* Provides tools for reading and updating the 12 OTTO persistence flags |
| 6 |
* that control whether OTTO-generated SEO data is also written to native |
| 7 |
* WordPress fields (ensuring data survives plugin removal). |
| 8 |
* |
| 9 |
* @package MetaSync |
| 10 |
* @subpackage MCP_Server/Tools |
| 11 |
*/ |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Get OTTO Persistence Settings Tool |
| 19 |
*/ |
| 20 |
class MCP_Tool_Get_Otto_Persistence_Settings extends MCP_Tool_Base { |
| 21 |
|
| 22 |
public function get_name() { |
| 23 |
return 'wordpress_get_otto_persistence_settings'; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_description() { |
| 27 |
return 'Get OTTO persistence settings — which of the 12 OTTO data types are configured to save to native WordPress fields'; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_input_schema() { |
| 31 |
return [ |
| 32 |
'type' => 'object', |
| 33 |
'properties' => new stdClass(), |
| 34 |
'required' => [], |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
public function execute($params) { |
| 39 |
$this->validate_params($params); |
| 40 |
$this->require_capability('manage_options'); |
| 41 |
|
| 42 |
$persistence_settings_file = dirname(dirname(dirname(__FILE__))) . '/otto/class-metasync-otto-persistence-settings.php'; |
| 43 |
if (file_exists($persistence_settings_file)) { |
| 44 |
require_once $persistence_settings_file; |
| 45 |
} |
| 46 |
|
| 47 |
if (!class_exists('Metasync_Otto_Persistence_Settings')) { |
| 48 |
throw new Exception('Metasync_Otto_Persistence_Settings class is not available. Ensure the OTTO persistence module is loaded.'); |
| 49 |
} |
| 50 |
|
| 51 |
$settings = Metasync_Otto_Persistence_Settings::get_settings(); |
| 52 |
|
| 53 |
return $this->success([ |
| 54 |
'settings' => $settings, |
| 55 |
'enabled_count' => count(array_filter($settings)), |
| 56 |
'total_flags' => count($settings), |
| 57 |
], 'OTTO persistence settings retrieved successfully'); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Update OTTO Persistence Settings Tool |
| 63 |
*/ |
| 64 |
class MCP_Tool_Update_Otto_Persistence_Settings extends MCP_Tool_Base { |
| 65 |
|
| 66 |
public function get_name() { |
| 67 |
return 'wordpress_update_otto_persistence_settings'; |
| 68 |
} |
| 69 |
|
| 70 |
public function get_description() { |
| 71 |
return 'Enable or disable persistence for one or all OTTO data types. When enabled, OTTO writes data to native WordPress fields so it survives plugin removal.'; |
| 72 |
} |
| 73 |
|
| 74 |
public function get_input_schema() { |
| 75 |
return [ |
| 76 |
'type' => 'object', |
| 77 |
'properties' => [ |
| 78 |
'settings' => [ |
| 79 |
'type' => 'object', |
| 80 |
'description' => 'Key-value pairs of persistence flags to update. Keys: meta_title, meta_description, meta_keywords, og_title, og_description, twitter_title, twitter_description, canonical_url, image_alt_text, link_corrections, heading_changes, structured_data. Values: true or false.', |
| 81 |
], |
| 82 |
'setting_key' => [ |
| 83 |
'type' => 'string', |
| 84 |
'description' => 'Single persistence flag key to update (alternative to the settings object)', |
| 85 |
'enum' => [ |
| 86 |
'meta_title', |
| 87 |
'meta_description', |
| 88 |
'meta_keywords', |
| 89 |
'og_title', |
| 90 |
'og_description', |
| 91 |
'twitter_title', |
| 92 |
'twitter_description', |
| 93 |
'canonical_url', |
| 94 |
'image_alt_text', |
| 95 |
'link_corrections', |
| 96 |
'heading_changes', |
| 97 |
'structured_data', |
| 98 |
], |
| 99 |
], |
| 100 |
'setting_value' => [ |
| 101 |
'type' => 'boolean', |
| 102 |
'description' => 'Boolean value for the single setting_key update', |
| 103 |
], |
| 104 |
], |
| 105 |
'required' => [], |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
public function execute($params) { |
| 110 |
$this->validate_params($params); |
| 111 |
$this->require_capability('manage_options'); |
| 112 |
|
| 113 |
$persistence_settings_file = dirname(dirname(dirname(__FILE__))) . '/otto/class-metasync-otto-persistence-settings.php'; |
| 114 |
if (file_exists($persistence_settings_file)) { |
| 115 |
require_once $persistence_settings_file; |
| 116 |
} |
| 117 |
|
| 118 |
if (!class_exists('Metasync_Otto_Persistence_Settings')) { |
| 119 |
throw new Exception('Metasync_Otto_Persistence_Settings class is not available. Ensure the OTTO persistence module is loaded.'); |
| 120 |
} |
| 121 |
|
| 122 |
$valid_keys = [ |
| 123 |
'meta_title', 'meta_description', 'meta_keywords', |
| 124 |
'og_title', 'og_description', |
| 125 |
'twitter_title', 'twitter_description', |
| 126 |
'canonical_url', 'image_alt_text', |
| 127 |
'link_corrections', 'heading_changes', 'structured_data', |
| 128 |
]; |
| 129 |
|
| 130 |
$updated_keys = []; |
| 131 |
$current_settings = Metasync_Otto_Persistence_Settings::get_settings(); |
| 132 |
|
| 133 |
// Handle bulk settings object |
| 134 |
if (!empty($params['settings']) && is_array($params['settings'])) { |
| 135 |
foreach ($params['settings'] as $key => $value) { |
| 136 |
if (!in_array($key, $valid_keys, true)) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
if (!is_bool($value) && !is_string($value) && !is_numeric($value)) { |
| 140 |
throw new Exception( |
| 141 |
"Invalid value type for setting '{$key}': expected boolean, got " . gettype($value) |
| 142 |
); |
| 143 |
} |
| 144 |
$bool_value = $this->coerce_bool($value); |
| 145 |
Metasync_Otto_Persistence_Settings::set_setting($key, $bool_value); |
| 146 |
$updated_keys[] = $key; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// Handle single key update |
| 151 |
if (!empty($params['setting_key'])) { |
| 152 |
$key = sanitize_key($params['setting_key']); |
| 153 |
if (!in_array($key, $valid_keys, true)) { |
| 154 |
throw new Exception("Invalid setting_key '{$key}'. Must be one of: " . implode(', ', $valid_keys)); |
| 155 |
} |
| 156 |
if (!array_key_exists('setting_value', $params)) { |
| 157 |
throw new Exception("setting_value is required when setting_key is provided"); |
| 158 |
} |
| 159 |
$bool_value = $this->coerce_bool($params['setting_value']); |
| 160 |
Metasync_Otto_Persistence_Settings::set_setting($key, $bool_value); |
| 161 |
$updated_keys[] = $key; |
| 162 |
} |
| 163 |
|
| 164 |
if (empty($updated_keys)) { |
| 165 |
throw new Exception("No valid settings provided. Supply a 'settings' object or a 'setting_key'/'setting_value' pair."); |
| 166 |
} |
| 167 |
|
| 168 |
$updated_settings = Metasync_Otto_Persistence_Settings::get_settings(); |
| 169 |
|
| 170 |
return $this->success([ |
| 171 |
'settings' => $updated_settings, |
| 172 |
'updated_keys' => $updated_keys, |
| 173 |
'enabled_count' => count(array_filter($updated_settings)), |
| 174 |
'total_flags' => count($updated_settings), |
| 175 |
], sprintf('Updated %d persistence flag(s): %s', count($updated_keys), implode(', ', $updated_keys))); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Coerce a value to boolean with flexible input handling |
| 180 |
*/ |
| 181 |
private function coerce_bool($value) { |
| 182 |
if (is_bool($value)) { |
| 183 |
return $value; |
| 184 |
} |
| 185 |
if (is_string($value)) { |
| 186 |
return in_array(strtolower($value), ['true', '1', 'yes', 'on'], true); |
| 187 |
} |
| 188 |
if (is_numeric($value)) { |
| 189 |
return (bool) $value; |
| 190 |
} |
| 191 |
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false; |
| 192 |
} |
| 193 |
} |
| 194 |
|