Skip to content

Commit

Permalink
move git integration from admin to editor sidepanel
Browse files Browse the repository at this point in the history
  • Loading branch information
madhusudhand committed Jul 19, 2023
1 parent e229899 commit bd47175
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 229 deletions.
26 changes: 0 additions & 26 deletions admin/class-git-integration.php

This file was deleted.

191 changes: 0 additions & 191 deletions admin/git-integration/git-themes.php

This file was deleted.

36 changes: 36 additions & 0 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,42 @@ public function register_rest_routes() {
},
)
);

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

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

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

function rest_get_readme_data( $request ) {
Expand Down
2 changes: 0 additions & 2 deletions includes/class-create-block-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ 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 @@ -62,7 +61,6 @@ 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
88 changes: 85 additions & 3 deletions includes/class-git-themes-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,95 @@ class Git_Themes_API {

public function __construct() {
$this -> git = Git_Wrapper::get_git(CREATE_BLOCK_THEME_GIT_DIR);
WP_Filesystem();
}

public function get_git_config() {
$git_version = $this -> git -> get_version();
if (empty($git_version)) {
return array( 'version' => $git_version );
}

list($git_configured, $remote_url) = $this -> is_git_initialized();
return array(
'version' => $this -> git -> get_version(),
'git_configured' => false,
'remote_url' => ''
'version' => $git_version,
'git_configured' => $git_configured,
'remote_url' => $remote_url // TODO: mask access token
);
}

public function connect_git_repo($request) {
$repository = $request->get_params();

try {
wp_mkdir_p(CREATE_BLOCK_THEME_GIT_DIR);
$remote_url = $repository['remote_url'];
$author_name = $repository['author_name'];
$author_email = $repository['author_email'];

$this -> git -> init($author_name, $author_email);
$this -> git -> add_remote_url( $remote_url );
// TODO: it is taking long time sometimes resulting timeouts.
// May be make it a background process??
$this -> git -> fetch_ref();

return array("status" => "success");
} catch (\Throwable $th) {
return array("status" => "fail");
}
}

public function disconnect_git_repo($request) {

}

public function get_git_changes() {
// TODO: capture target path from user and save it to database.
$theme = wp_get_theme();
$target_path = $theme->get('TextDomain'); // using theme slug as target directory for testing
$source_path = get_template_directory(); // current theme directory

// copy theme into target path.
wp_mkdir_p(CREATE_BLOCK_THEME_GIT_DIR."/$target_path");
copy_dir($source_path, CREATE_BLOCK_THEME_GIT_DIR."/$target_path");

// add the changes to staging
$this -> git -> add(".");
list( , $changes ) = $this -> git -> status();

$result = array();

foreach ($changes as $file => $modifier) {
array_push($result, array(
"file" => $file,
"modifier" => $modifier,
));
}

return $result;
}

public function commit_changes($request) {
try {
$commit = $request->get_params();

$this->git->commit($commit['message']);
$local_branch = $this->git->get_local_branch();
$this->git->push($local_branch);

return array("status" => "success");
} catch (\Throwable $th) {
return array("status" => "fail");
}
}

private function is_git_initialized() {
global $wp_filesystem;
$git_initialized = $wp_filesystem -> exists(CREATE_BLOCK_THEME_GIT_DIR.'/.git');
$remote_url = $this -> git -> get_remote_url();
if (empty($remote_url)) {
$git_initialized = false;
}
return array($git_initialized, $remote_url);
}
}
8 changes: 8 additions & 0 deletions lib/git-constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

if ( !defined('CREATE_BLOCK_THEME_GIT_DIR') ) {
$plugin_dir = WP_PLUGIN_DIR.'/create-block-theme';
$theme_repos_dir = $plugin_dir.'/.git-repo';

define('CREATE_BLOCK_THEME_GIT_DIR', $theme_repos_dir);
}
2 changes: 2 additions & 0 deletions lib/git-wrapper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once __DIR__ . '/git-constants.php';

class Git_Wrapper {
private static $obj;
private $last_error = '';
Expand Down
Loading

0 comments on commit bd47175

Please sign in to comment.