$selection Server id => block keys. * @return array */ public static function build( array $selection ): array { $servers = array(); foreach ( $selection as $server_id => $blocks ) { $row = ServerQuery::instance()->get_item( (int) $server_id ); // A server deleted between rendering the form and submitting it is // skipped rather than fatal — the operator gets the rest of their // export, which is more use than an error page. if ( ! $row instanceof ServerRow ) { continue; } $servers[] = ServerExporter::export( $row, is_array( $blocks ) ? $blocks : ExportConstants::BLOCKS ); } return array( 'format' => ExportConstants::FORMAT, 'version' => ExportConstants::VERSION, 'generated_at' => gmdate( 'c' ), 'source' => array( 'site_url' => home_url(), 'plugin_version' => defined( 'ACROSSAI_MCP_MANAGER_VERSION' ) ? ACROSSAI_MCP_MANAGER_VERSION : '', ), 'servers' => $servers, ); } /** * Every server on the site, with every block. * * What the automatic pre-import snapshot takes: a snapshot narrowed to the * blocks being imported would not be a restore point, because the blocks * it left out are exactly the ones nothing else would put back. * * @since 0.4.9 * @return array */ public static function build_everything(): array { $selection = array(); foreach ( (array) ServerQuery::instance()->query( array( 'number' => 0 ) ) as $row ) { if ( $row instanceof ServerRow ) { $selection[ (int) $row->id ] = ExportConstants::BLOCKS; } } return self::build( $selection ); } /** * The filename a download is offered under. * * Host plus date, so a folder of exports from several sites stays legible. * * @since 0.4.9 * @return string */ public static function filename(): string { $host = (string) wp_parse_url( home_url(), PHP_URL_HOST ); $host = '' !== $host ? sanitize_file_name( $host ) : 'site'; return sprintf( 'acrossai-mcp-%s-%s.json', $host, gmdate( 'Y-m-d' ) ); } /** * The payload as the JSON that actually ships. * * Pretty-printed and with slashes unescaped: an export is read, diffed and * kept in version control by people, and `https:\/\/example.com` helps * nobody. Not a security control — `wp_json_encode` still escapes what * matters for a JSON document. * * @since 0.4.9 * @param array $payload Built payload. * @return string Empty string when encoding fails. */ public static function to_json( array $payload ): string { $json = wp_json_encode( $payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); return is_string( $json ) ? $json : ''; } }