plugin = $plugin;
$this->layout_tools = $layout_tools;
$this->media_upload = self::media_supported() ? new Divi_Powers_Media_Upload() : null;
}
public function register() {
if ( ! self::media_supported() ) {
add_action( 'admin_notices', array( $this, 'media_upgrade_notice' ) );
}
if ( ! $this->plugin->is_power_enabled( 'builder_bridge' ) ) {
return;
}
add_filter( 'fived_mcp_tools', array( $this, 'register_tools' ) );
add_filter( 'fived_mcp_callback', array( $this, 'handle_tool_call' ), 20, 4 );
// Keep existing AI Engine grants usable during migration.
add_filter( 'mwai_mcp_tools', array( $this, 'filter_public_tool_catalog' ), PHP_INT_MAX );
add_filter( 'mwai_mcp_callback', array( $this, 'reject_private_tool_call' ), -9999, 4 );
add_filter( 'mwai_mcp_tools', array( $this, 'register_tools' ) );
add_filter( 'mwai_mcp_callback', array( $this, 'handle_customer_tool_call' ), 20, 4 );
}
public static function public_tool_names() {
$names = array(
'divi_powers_get_site_status',
'divi_powers_get_layout_contract',
'divi_powers_get_builder_context',
'divi_powers_get_page_layout',
'divi_powers_inspect_layout',
'divi_powers_validate_layout',
'divi_powers_export_layout_json',
'divi_powers_create_page_draft',
'divi_powers_save_page_layout',
'divi_powers_publish_working_copy',
'divi_powers_restore_page_revision',
);
return self::media_supported() ? array_merge( $names, Divi_Powers_Media_Upload::tool_names() ) : $names;
}
/** Existing clients may finish saved staged jobs, but new catalogs omit them. */
public static function legacy_tool_names() {
return array(
'divi_powers_stage_page_layout',
'divi_powers_get_job',
'divi_powers_apply_staged_layout',
'divi_powers_rollback_page_layout',
);
}
private static function media_supported() {
return defined( 'DIVI_POWERS_VERSION' ) && version_compare( DIVI_POWERS_VERSION, '0.23.9', '>=' )
&& defined( 'DIVI_POWERS_MEDIA_UPLOAD_API_VERSION' ) && DIVI_POWERS_MEDIA_UPLOAD_API_VERSION >= 1
&& class_exists( 'Divi_Powers_Media_Upload', false );
}
public function media_upgrade_notice() {
if ( current_user_can( 'update_plugins' ) ) {
echo '
' . esc_html__( 'Media uploads require an updated 5D Powers for Divi Free build with media upload API 1. Existing layout tools remain available.', '5d-powers-for-divi' ) . '
';
}
}
private function operation_definitions() {
return array_merge( $this->layout_tools->operation_definitions(), $this->media_upload ? $this->media_upload->operation_definitions() : array() );
}
public function register_tools( $tools ) {
$tools = is_array( $tools ) ? $tools : array();
foreach ( $this->operation_definitions() as $definition ) {
if ( isset( $definition['name'] ) && in_array( $definition['name'], self::public_tool_names(), true ) ) {
$tools[] = $definition;
}
}
return array_values( $tools );
}
public function filter_public_tool_catalog( $tools ) {
$tools = is_array( $tools ) ? $tools : array();
return array_values(
array_filter(
$tools,
function ( $tool ) {
$name = is_array( $tool ) && isset( $tool['name'] ) ? (string) $tool['name'] : '';
return '' === $this->access_error( $name );
}
)
);
}
public function reject_private_tool_call( $previous, $tool, $args, $id ) {
unset( $args );
$message = $this->access_error( $tool );
if ( '' === $message ) {
return $previous;
}
return array(
'jsonrpc' => '2.0',
'id' => $id,
'error' => array( 'code' => -32601, 'message' => $message ),
);
}
public function handle_customer_tool_call( $previous, $tool, $args, $id ) {
$guarded = $this->reject_private_tool_call( $previous, $tool, $args, $id );
if ( ! empty( $guarded ) ) {
return $guarded;
}
return $this->handle_tool_call( $previous, self::unqualified_tool_name( $tool ), $args, $id );
}
public function handle_tool_call( $previous, $tool, $args, $id ) {
if ( ! empty( $previous ) || 0 !== strpos( (string) $tool, 'divi_powers_' ) ) {
return $previous;
}
try {
$service = in_array( (string) $tool, array( 'divi_powers_get_media_upload_limits', 'divi_powers_upload_media' ), true ) ? $this->media_upload : $this->layout_tools;
$result = $service ? $service->dispatch_operation( (string) $tool, is_array( $args ) ? $args : array() ) : new WP_Error( 'divi_powers_media_requires_free_update', 'Update 5D Powers for Divi Free to a build with media upload API 1 before using media tools.' );
} catch ( Throwable $error ) {
$result = new WP_Error( 'divi_powers_ai_connector_exception', $error->getMessage() );
}
if ( is_wp_error( $result ) ) {
return array(
'jsonrpc' => '2.0',
'id' => $id,
'error' => array(
'code' => -32000,
'message' => $result->get_error_message(),
'data' => array( 'wordpress_code' => $result->get_error_code() ),
),
);
}
return array(
'jsonrpc' => '2.0',
'id' => $id,
'result' => array(
'content' => array(
array(
'type' => 'text',
'text' => wp_json_encode( $result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ),
),
),
),
);
}
private function access_error( $tool ) {
$name = self::unqualified_tool_name( $tool );
if ( 0 === strpos( $name, 'fived_' ) ) {
return 'Private site-management tools are unavailable through this connector.';
}
if ( 0 !== strpos( $name, 'divi_powers_' ) ) {
return '';
}
if ( ! in_array( $name, self::public_tool_names(), true ) && ! in_array( $name, self::legacy_tool_names(), true ) ) {
return 'This Divi operation is unavailable through this connector.';
}
$settings = get_option( FiveD_Site_Bridge_Settings::OPTION, array() );
$settings = is_array( $settings ) ? $settings : array();
if ( empty( $settings['customer_connection_enabled'] ) || ! $this->plugin->is_power_enabled( 'builder_bridge' ) ) {
return 'The Divi Powers AI connection is disabled on this site.';
}
$role = (string) ( $settings['customer_mcp_role'] ?? 'readonly' );
foreach ( $this->operation_definitions() as $definition ) {
if ( isset( $definition['name'] ) && $definition['name'] === $name ) {
return 'read' === ( $definition['accessLevel'] ?? '' ) || 'readwrite' === $role
? ''
: 'This connection permits inspection only.';
}
}
return 'This Divi operation is unavailable through this connector.';
}
private static function unqualified_tool_name( $tool ) {
$tool = trim( (string) $tool );
$dot = strrpos( $tool, '.' );
return false === $dot ? $tool : substr( $tool, $dot + 1 );
}
}