Styles). * * @package GripMCP */ declare( strict_types=1 ); namespace GripMCP\Tools\Posts; defined( 'ABSPATH' ) || exit; /** * WordPress keeps a site's saved styles as a theme.json document in a post * of type wp_global_styles, and reads that post only when it is a JSON * object with "isGlobalStylesUserThemeJSON" set and a "version" * (WP_Theme_JSON_Resolver::get_user_data(), WP_Theme_JSON_Schema::migrate()). * Anything else is dropped without a word, and the site loses every saved * style. The Site Editor never saves such a post: its REST route adds the * flag and the version itself. A write through these tools is checked the * same way. */ final class Global_Styles { public const TYPE = 'wp_global_styles'; /** * Why a document cannot be saved as a site's styles, or '' when it can. * * @param string $content The post_content that would be saved. * @param string $stored The post_content saved now, so CSS that is not * changed is not checked again. */ public static function content_error( string $content, string $stored = '' ): string { $latest = class_exists( 'WP_Theme_JSON' ) ? (int) \WP_Theme_JSON::LATEST_SCHEMA : 3; $object = json_decode( $content ); if ( ! $object instanceof \stdClass ) { $why = JSON_ERROR_NONE !== json_last_error() ? json_last_error_msg() : ''; return self::refusal( sprintf( /* translators: 1: theme.json version this WordPress reads, 2: why the text is not JSON, e.g. " (Syntax error)", or nothing. */ __( 'The site\'s saved styles are a theme.json document, a JSON object such as {"version": %1$d, "isGlobalStylesUserThemeJSON": true, "styles": {}, "settings": {}}, and this is not one%2$s. Saved, it would make WordPress drop every saved style on the site.', 'gripmcp' ), $latest, '' !== $why ? ' (' . $why . ')' : '' ) ); } $document = (array) json_decode( $content, true ); // WordPress reads the document only with the flag, and runs its own // filter for accounts without unfiltered_html only on flagged ones. if ( empty( $document['isGlobalStylesUserThemeJSON'] ) ) { return self::refusal( __( '"isGlobalStylesUserThemeJSON": true is missing from the site\'s saved styles. WordPress reads the document only with it, so saved without it, every saved style on the site would be dropped.', 'gripmcp' ) ); } // Without a version WordPress replaces the whole document with an // empty one; a newer one than it knows is read with the wrong rules. $version = $document['version'] ?? null; if ( ! is_int( $version ) || $version < 1 || $version > $latest ) { return self::refusal( sprintf( /* translators: %d: newest theme.json version this WordPress reads. */ __( '"version" in the site\'s saved styles must be a whole number from 1 to %d, the theme.json versions this WordPress reads. Without it, WordPress drops every saved style on the site.', 'gripmcp' ), $latest ) ); } // The Site Editor's REST route takes these two as objects. WordPress // saves an empty one as [] (PHP's empty array), which counts as one. foreach ( array( 'styles', 'settings' ) as $key ) { if ( isset( $object->{$key} ) && ! $object->{$key} instanceof \stdClass && array() !== $object->{$key} ) { return self::refusal( sprintf( /* translators: %s: "styles" or "settings". */ __( '"%s" in the site\'s saved styles must be a JSON object.', 'gripmcp' ), $key ) ); } } // The site's custom CSS (styles.css, and a block's own under // styles.blocks) is printed on every page. Unless Custom_Css::writable() // allows it, it is read but left as it is: any other change to the // document still saves. $before = json_decode( $stored, true ); $before = is_array( $before ) ? $before : array(); if ( ! Custom_Css::writable() && self::css_values( $document['styles'] ?? array() ) !== self::css_values( $before['styles'] ?? array() ) ) { return Custom_Css::read_only(); } // Only CSS this change writes is checked: CSS WordPress already saved // never blocks an edit elsewhere in the document. if ( isset( $document['styles']['css'] ) ) { if ( ! is_string( $document['styles']['css'] ) ) { return self::refusal( __( '"styles.css" in the site\'s saved styles must be text.', 'gripmcp' ) ); } $saved = isset( $before['styles']['css'] ) ? $before['styles']['css'] : null; if ( $saved !== $document['styles']['css'] ) { $markup = self::css_error( $document['styles']['css'] ); if ( null !== $markup ) { return $markup; } } } return ''; } /** * Whether a saved styles document holds custom CSS (styles.css, or a * block's own under styles.blocks) that WordPress prints. An empty or * null value prints nothing; any other value that is not text counts, so * a document is never taken for one without CSS by mistake. * * @param string $content The post_content of a wp_global_styles post. */ public static function has_css( string $content ): bool { $document = json_decode( $content, true ); if ( ! is_array( $document ) ) { return false; } foreach ( self::css_values( $document['styles'] ?? array() ) as $value ) { if ( null !== $value && ( ! is_string( $value ) || '' !== trim( $value ) ) ) { return true; } } return false; } /** * Why CSS cannot be saved in the site's styles, or null: the rule the * Site Editor's own route applies on this WordPress. Since 7.0 only a * closing ]|<(?:/(?:s(?:t(?:y(?:le?)?)?)?)?)?$#iD', $css ) ) { return null; } return __( 'The CSS closes its