Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add theme json inspector #520

Merged
merged 10 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions admin/resolver_additions.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
$data['$schema'] = $schema;
$theme_json = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
return preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json );

}

public static function get_theme_file_contents() {
Expand Down Expand Up @@ -128,7 +127,6 @@ public static function clean_cached_data() {
// Does this clear the Gutenberg equivalent?
static::$theme_json_file_cache = array();
}

}
}

Expand Down
31 changes: 31 additions & 0 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,37 @@ public function register_rest_routes() {
},
)
);
register_rest_route(
'create-block-theme/v1',
'/get-theme-data',
array(
'methods' => 'GET',
'callback' => array( $this, 'rest_get_theme_data' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
),
);
}

function rest_get_theme_data( $request ) {
try {
$theme_data = MY_Theme_JSON_Resolver::get_theme_file_contents();
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Theme data retrieved.', 'create-block-theme' ),
'data' => $theme_data,
),
);
} catch ( Exception $error ) {
return new WP_REST_Response(
array(
'status' => 'FAILURE',
'message' => $error->getMessage(),
)
);
}
}

function rest_get_readme_data( $request ) {
Expand Down
209 changes: 209 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
},
"main": "build/index.js",
"dependencies": {
"@codemirror/lang-json": "^6.0.1",
"@uiw/react-codemirror": "^4.21.24",
"@wordpress/icons": "^9.24.0",
"lib-font": "^2.4.0"
},
Expand Down
38 changes: 38 additions & 0 deletions src/editor-sidebar/json-editor-modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useState, useEffect } from '@wordpress/element';
import { Modal } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import CodeMirror from '@uiw/react-codemirror';
import { json } from '@codemirror/lang-json';
import { fetchThemeJson } from '../resolvers';

const ThemeJsonEditorModal = ( { onRequestClose } ) => {
const [ themeData, setThemeData ] = useState( '' );
const themeName = useSelect( ( select ) =>
select( 'core' ).getCurrentTheme()
)?.name?.raw;
const fetchThemeData = async () => {
setThemeData( await fetchThemeJson() );
};
const handleSave = () => {};

useEffect( () => {
fetchThemeData();
} );

return (
<Modal
isFullScreen
title={ `theme.json for ${ themeName }` }
onRequestClose={ onRequestClose }
>
<CodeMirror
extensions={ [ json() ] }
value={ themeData }
onChange={ handleSave }
vcanales marked this conversation as resolved.
Show resolved Hide resolved
readOnly={ true }
/>
</Modal>
);
};

export default ThemeJsonEditorModal;
Loading
Loading