id ) { return; } $scripts = wp_scripts(); $data = $scripts->get_data( 'media-grid', 'data' ); if ( ! is_string( $data ) || '' === $data ) { return; } // `WP_Scripts::localize()` writes one `var = ;` // statement per call, newline-joined. Take the last one: that's // the value currently winning at runtime. if ( ! preg_match_all( '/^var _wpMediaGridSettings = (.+);$/m', $data, $matches ) ) { return; } // Decoded as objects, not associative arrays. An empty JSON object // round-tripped through an array comes back out as `[]`, and the // grid reads `queryVars` by key. Decoding to stdClass keeps every // nested object an object, however many core grows. // // `queryVars` is core's key, so the snake_case property sniff has // nothing to say about it that we can act on. // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase $settings = json_decode( end( $matches[1] ) ); if ( ! $settings instanceof stdClass || ! isset( $settings->queryVars ) || ! $settings->queryVars instanceof stdClass ) { return; } if ( ! isset( $settings->queryVars->openstation_chromeless ) ) { return; } unset( $settings->queryVars->openstation_chromeless ); // phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase wp_localize_script( 'media-grid', '_wpMediaGridSettings', (array) $settings ); } add_action( 'admin_enqueue_scripts', 'openstation_strip_chromeless_flag_from_media_grid', 999 );