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

Refactor react app code for general purpose #320

Merged
merged 4 commits into from
Apr 14, 2023
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
File renamed without changes.
32 changes: 32 additions & 0 deletions admin/class-react-app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

class React_App {
public static function bootstrap() {
// Load the required WordPress packages.
// Automatically load imported dependencies and assets version.
$asset_file = include plugin_dir_path( __DIR__ ) . 'build/index.asset.php';

// Enqueue CSS dependencies of the scripts included in the build.
foreach ( $asset_file['dependencies'] as $style ) {
wp_enqueue_style( $style );
}

// Enqueue CSS of the app
wp_enqueue_style( 'create-block-theme-app', plugins_url( 'build/index.css', __DIR__ ), array(), $asset_file['version'] );

// Load our app.js.
array_push( $asset_file['dependencies'], 'wp-i18n' );
wp_enqueue_script( 'create-block-theme-app', plugins_url( 'build/index.js', __DIR__ ), $asset_file['dependencies'], $asset_file['version'] );

// Set google fonts json file url.
wp_localize_script(
'create-block-theme-app',
'createBlockTheme',
array(
'googleFontsDataUrl' => plugins_url( 'assets/google-fonts/fallback-fonts-list.json', __DIR__ ),
'adminUrl' => admin_url(),
'themeUrl' => get_stylesheet_directory_uri(),
)
);
}
}
35 changes: 4 additions & 31 deletions admin/manage-fonts/fonts-page.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

require_once( dirname( __DIR__ ) . '/class-react-app.php' );

class Fonts_Page {
public static function manage_fonts_admin_page() {
self::load_fonts_react_app();
React_App::bootstrap();

$theme_name = wp_get_theme()->get( 'Name' );

Expand All @@ -25,37 +27,8 @@ public static function manage_fonts_admin_page() {

?>
<p name="theme-fonts-json" id="theme-fonts-json" class="hidden"><?php echo $fonts_json_string; ?></p>
<div id="fonts-app"></div>
<div id="create-block-theme-app"></div>
<input type="hidden" name="nonce" id="nonce" value="<?php echo wp_create_nonce( 'create_block_theme' ); ?>" />
<?php
}

public static function load_fonts_react_app() {
// Load the required WordPress packages.
// Automatically load imported dependencies and assets version.
$asset_file = include plugin_dir_path( dirname( __DIR__ ) ) . 'build/index.asset.php';

// Enqueue CSS dependencies of the scripts included in the build.
foreach ( $asset_file['dependencies'] as $style ) {
wp_enqueue_style( $style );
}

// Enqueue CSS of the app
wp_enqueue_style( 'fonts-app', plugins_url( 'build/index.css', dirname( __DIR__ ) ), array(), $asset_file['version'] );

// Load our app.js.
array_push( $asset_file['dependencies'], 'wp-i18n' );
wp_enqueue_script( 'create-block-theme-app', plugins_url( 'build/index.js', dirname( __DIR__ ) ), $asset_file['dependencies'], $asset_file['version'] );

// Set google fonts json file url.
wp_localize_script(
'create-block-theme-app',
'createBlockTheme',
array(
'googleFontsDataUrl' => plugins_url( 'assets/google-fonts/fallback-fonts-list.json', dirname( __DIR__ ) ),
'adminUrl' => admin_url(),
'themeUrl' => get_stylesheet_directory_uri(),
)
);
}
}
6 changes: 3 additions & 3 deletions admin/manage-fonts/google-fonts-page.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

require_once( __DIR__ . '/fonts-page.php' );
require_once( dirname( __DIR__ ) . '/class-react-app.php' );

class Google_Fonts {
public static function google_fonts_admin_page() {
Fonts_Page::load_fonts_react_app();
React_App::bootstrap();
?>
<input id="nonce" type="hidden" value="<?php echo wp_create_nonce( 'create_block_theme' ); ?>" />
<div id="fonts-app"></div>
<div id="create-block-theme-app"></div>

<?php
}
Expand Down
6 changes: 3 additions & 3 deletions admin/manage-fonts/local-fonts-page.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

require_once( __DIR__ . '/fonts-page.php' );
require_once( dirname( __DIR__ ) . '/class-react-app.php' );

class Local_Fonts {
public static function local_fonts_admin_page() {
// JS dependencies needed to read the file data from .woff and .woff2 files. (no needed for .ttf files)
wp_enqueue_script( 'inflate', plugin_dir_url( dirname( __FILE__ ) ) . 'js/lib/inflate.js', array(), '', false );
wp_enqueue_script( 'unbrotli', plugin_dir_url( dirname( __FILE__ ) ) . 'js/lib/unbrotli.js', array(), '', false );

Fonts_Page::load_fonts_react_app();
React_App::bootstrap();

?>
<input id="nonce" type="hidden" value="<?php echo wp_create_nonce( 'create_block_theme' ); ?>" />
<div id="fonts-app"></div>
<div id="create-block-theme-app"></div>

<?php
}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-create-block-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private function load_dependencies() {
/**
* The class responsible for defining all actions that occur in the admin area.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-create-block-theme-admin.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-create-theme.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-manage-fonts.php';

$this->loader = new Create_Block_Theme_Loader();
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function App() {
window.addEventListener(
'load',
function () {
render( <App />, document.querySelector( '#fonts-app' ) );
render( <App />, document.querySelector( '#create-block-theme-app' ) );
},
false
);