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 wp-admin menu for git repository configuration #438

Closed
wants to merge 3 commits into from
Closed
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
152 changes: 152 additions & 0 deletions admin/class-git-integration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

require_once( __DIR__ . '/class-react-app.php' );
require_once( dirname( __DIR__ ) . '/includes/class-create-block-theme-settings.php' );

class Git_Integration_Admin {
public $plugin_settings;

public function __construct() {
$this->plugin_settings = new Create_Block_Theme_Settings();
add_action( 'admin_menu', array( $this, 'create_admin_menu' ) );
}

public function create_admin_menu() {
if ( ! wp_is_block_theme() ) {
return;
}
$menu_title = _x( 'Create Block Theme: Git Utilities', 'UI String', 'create-block-theme' );
$page_title = $menu_title;
add_menu_page( $page_title, $menu_title, 'edit_theme_options', 'themes-git-integration', array( $this, 'git_settings_page_react' ) );
}

public function git_settings_page_react() {
React_App::bootstrap();

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

public function git_settings_page() {
// Add your settings page content here
$git_url_format = 'https://github.com/username/repository';

if ( isset( $_POST['delete_git_config'] ) ) {
$this->delete_settings();
} elseif ( isset( $_POST['save_git_config'] ) ) {
$this->save_settings( $_POST );
}
$settings = $this->get_settings();
$repository_url = $settings['repository_url'];
$default_branch = $settings['default_branch'];
$access_token = $settings['access_token'];
$author_name = $settings['author_name'];
$author_email = $settings['author_email'];
?>

<div class="wrap">
<h2><?php echo __( 'Create Block Theme: Git Utilities', 'create-block-theme' ); ?></h2>
<p style="max-width: 400px;">
<?php echo __( 'Connect your WordPress site themes with a Git repository. You can pull and commit theme changes to the repository.', 'create-block-theme' ); ?>
</p>
<div class="theme-form">
<form action="" method="POST">
<input type="hidden" name="git_config_form" value="<?php echo wp_create_nonce( 'git_config_form' ); ?>" />

<?php
if ( ! empty( $repository_url ) ) {
echo '<p><h3>' . __( 'Your WordPress site is connected to the git repository.', 'create-block-theme' ) . ' ✅</h3></p>';
echo "<p style='display:grid;grid-template-columns:minmax(100px,120px) auto'>
<strong>" . __( 'Repository URL', 'create-block-theme' ) . ": </strong>
<span>$repository_url</span>
<strong>" . __( 'Default Branch', 'create-block-theme' ) . ": </strong>
<span>$default_branch</span>
</p>";

echo "<input type='submit' name='delete_git_config' class='button-secondary' value='" . __( 'Disconnect Repository', 'create-block-theme' ) . "'/><br/><br/>";

if ( ! empty( $access_token ) ) {
echo '<p><strong>' . __( 'Access token configured', 'create-block-theme' ) . ' ✅</strong> <br/>' . __( 'You can still update with a new access token.', 'create-block-theme' ) . '</p>';
}
} else {
?>
<p><?php echo __( 'Configure the options below to get started.', 'create-block-theme' ); ?></p>
<div>
<label for="repository_url"><?php echo __( 'Repository URL', 'create-block-theme' ); ?> (*): </label><br/>
<input type="text" class="regular-text" name="repository_url" id="repository_url" value="<?php echo sanitize_text_field( $_POST['repository_url'] ); ?>" placeholder="<?php echo $git_url_format; ?>">
</div>

<div>
<label for="default_branch"><?php echo __( 'Default branch', 'create-block-theme' ); ?>: </label><br/>
<input type="text" class="regular-text" name="default_branch" id="default_branch" placeholder="master / main / trunk">
</div>
<?php
}

if ( empty( $repository_url ) ) {
?>
<p style="max-width: 400px;">
<?php echo __( 'Following options are required if the repository is private or to commit the theme changes to git repository.', 'create-block-theme' ); ?>
<br/>
</p>
<?php } ?>

<div>
<label for="access_token"><?php echo __( 'Access token', 'create-block-theme' ); ?>: </label><br/>
<input type="text" class="regular-text" name="access_token" id="access_token" placeholder="<?php echo $access_token ? '********' : ''; ?>">
</div>

<div>
<label for="author_name"><?php echo __( 'Author name', 'create-block-theme' ); ?>: </label><br/>
<input type="text" class="regular-text" name="author_name" id="author_name" value="<?php echo $author_name; ?>">
</div>

<div>
<label for="author_email"><?php echo __( 'Author email', 'create-block-theme' ); ?>: </label><br/>
<input type="text" class="regular-text" name="author_email" id="author_email" value="<?php echo $author_email; ?>">
</div>
<br/>

<input type="submit" name="save_git_config" class="button-primary" value="<?php echo empty( $repository_url ) ? __( 'Connect Repository', 'create-block-theme' ) : __( 'Update Settings', 'create-block-theme' ); ?>" />
</form>
</div>
</div>
<?php
}

private function get_settings() {
return $this->plugin_settings->get_settings();
}

private function save_settings( $settings ) {
$repository_url = sanitize_text_field( $settings['repository_url'] );
$default_branch = sanitize_text_field( $settings['default_branch'] );
$access_token = sanitize_text_field( $settings['access_token'] );
$author_name = sanitize_text_field( $settings['author_name'] );
$author_email = sanitize_text_field( $settings['author_email'] );

// TODO: try cloning the git repository here.
// Show error if it fails
// save the options only if git clone is successful.

$this->plugin_settings->update_settings(
array(
'repository_url' => $repository_url,
'default_branch' => $default_branch,
'access_token' => $access_token,
'author_name' => $author_name,
'author_email' => $author_email,
)
);

echo '<div class="updated"><p>Settings saved!</p></div>';
}

public function delete_settings() {
$this->plugin_settings->delete_settings();
// TODO: delete the local clone of the repository
}
}
21 changes: 20 additions & 1 deletion create-block-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
/**
* The core plugin class.
*/
require plugin_dir_path( __FILE__ ) . 'includes/class-create-block-theme.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-create-block-theme.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-create-block-theme-settings.php';

/**
* Begins execution of the plugin.
Expand All @@ -44,3 +45,21 @@ function run_create_block_theme() {

}
run_create_block_theme();

function create_block_theme_activated() {
// none
}

function create_block_theme_deactivated() {
// none
}

function create_block_theme_uninstalled() {
$plugin_settings = new Create_Block_Theme_Settings();
$plugin_settings->delete_settings();
}

// may be some clean up required ???
register_activation_hook( __FILE__, 'create_block_theme_activated' );
register_deactivation_hook( __FILE__, 'create_block_theme_deactivated' );
register_uninstall_hook( __FILE__, 'create_block_theme_uninstalled' );
29 changes: 29 additions & 0 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once( __DIR__ . '/class-themes-git-api.php' );

/**
* The api functionality of the plugin leveraged by the site editor UI.
*
Expand All @@ -8,12 +10,14 @@
* @author WordPress.org
*/
class Create_Block_Theme_API {
private $git_api;

/**
* Initialize the class and set its properties.
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
$this->git_api = new Themes_Git_API();
}

/**
Expand Down Expand Up @@ -97,6 +101,31 @@ public function register_rest_routes() {
},
)
);

// GIT Integration routes
register_rest_route(
'create-block-theme/v1',
'/settings',
array(
'methods' => 'GET',
'callback' => array( $this->git_api, 'get_settings' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);

register_rest_route(
'create-block-theme/v1',
'/update-git-connection',
array(
'methods' => 'POST',
'callback' => array( $this->git_api, 'update_git_repo' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
}

function rest_get_readme_data( $request ) {
Expand Down
46 changes: 46 additions & 0 deletions includes/class-create-block-theme-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

class Create_Block_Theme_Settings {
function get_settings() {
// delete_option( 'connected_repos' );
return array(
'connected_repos' => get_option( 'connected_repos', array() ),
);
}

function add_git_connection( $connection ) {
$repos = get_option( 'connected_repos', array() );
array_push( $repos, $connection );
update_option( 'connected_repos', $repos );
}

function update_git_connection( $connection, $theme_slug ) {
// TODO: update only fields with values. keep the old values if value is not set
$repos = get_option( 'connected_repos', array() );
$repos = array_map(
function( $repo ) use ( $connection, $theme_slug ) {
if ( $repo['themeSlug'] === $theme_slug ) {
return array_merge( $repo, $connection );
}
return $repo;
},
$repos
);
update_option( 'connected_repos', $repos );
}

function delete_git_connection( $theme_slug ) {
$repos = get_option( 'connected_repos', array() );
$repos = array_filter(
$repos,
function( $repo ) use ( $theme_slug ) {
return $repo['themeSlug'] !== $theme_slug;
}
);
update_option( 'connected_repos', $repos );
}

function delete_settings() {
delete_option( 'connected_repos' );
}
}
2 changes: 2 additions & 0 deletions includes/class-create-block-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private function load_dependencies() {
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-create-theme.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-manage-fonts.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-git-integration.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/wp-org-theme-directory.php';

require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-create-block-theme-api.php';
Expand All @@ -61,6 +62,7 @@ private function define_admin_hooks() {

$plugin_admin = new Create_Block_Theme_Admin();
$manage_fonts_admin = new Manage_Fonts_Admin();
$git_admin = new Git_Integration_Admin();
$wp_theme_directory = new WP_Theme_Directory();
$plugin_api = new Create_Block_Theme_API();
}
Expand Down
69 changes: 69 additions & 0 deletions includes/class-themes-git-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

require_once( __DIR__ . '/class-create-block-theme-settings.php' );

class Themes_Git_API {
public $plugin_settings;

public function __construct() {
$this->plugin_settings = new Create_Block_Theme_Settings();
}

public function get_settings() {
try {
$settings = $this->plugin_settings->get_settings();
// TODO: mask accessToken

return array( 'settings' => $settings );
} catch ( \Throwable $th ) {
return array( 'status' => 'error' );
}
}

public function update_git_repo( $request ) {
$req_params = $request->get_params();
$action = $req_params['action'];
$repository = $req_params['repository'];
$theme_slug = $req_params['themeSlug'];
$theme_name = $req_params['themeName'];

// do not save any other params
$repository = array(
'repositoryUrl' => $repository['repositoryUrl'],
'defaultBranch' => $repository['defaultBranch'],
'accessToken' => $repository['accessToken'],
'authorName' => $repository['authorName'],
'authorEmail' => $repository['authorEmail'],
);

try {
if ( 'create' === $action ) {
$this->plugin_settings->add_git_connection(
array_merge(
$repository,
array(
'themeSlug' => $theme_slug,
'themeName' => $theme_name,
)
)
);
} elseif ( 'update' === $action ) {
$this->plugin_settings->update_git_connection( $repository, $theme_slug );
} elseif ( 'delete' === $action ) {
$this->plugin_settings->delete_git_connection( $theme_slug );
} else {
return array(
'status' => 'error',
'message' => 'Invalid action.',
);
}

return array( 'status' => 'ok' );
} catch ( \Throwable $th ) {
return array(
'status' => 'error',
'message' => $th . __toString(),
);
}
}
}
Loading
Loading