Skip to content

Commit

Permalink
Resolve minor papercuts to enable using the plugin with git
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Dec 30, 2024
1 parent c07add9 commit decd81e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
"step": "activatePlugin",
"pluginPath": "z-data-liberation-static-files-editor/plugin.php"
},
{
"step": "activatePlugin",
"pluginPath": "gutenberg/gutenberg.php"
},
{
"step": "runPHP",
"code": "<?php require_once '/wordpress/wp-load.php'; wp_insert_post(array('post_title' => 'My Notes', 'post_status' => 'publish', 'post_type' => 'page'));"
Expand Down
16 changes: 14 additions & 2 deletions packages/playground/data-liberation-static-files-editor/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,27 @@ class WP_Static_Files_Editor_Plugin {
static private function get_fs() {
if(!self::$fs) {
$dot_git_path = WP_CONTENT_DIR . '/.static-pages.git';
if(!is_dir($dot_git_path)) {
mkdir($dot_git_path, 0777, true);
}
$local_fs = new WP_Local_Filesystem($dot_git_path);
$repo = new WP_Git_Repository($local_fs);
$repo->add_remote('origin', GIT_REPO_URL);
$repo->set_ref_head('HEAD', 'refs/heads/' . GIT_BRANCH);
$repo->set_config_value('user.name', GIT_USER_NAME);
$repo->set_config_value('user.email', GIT_USER_EMAIL);

$client = new WP_Git_Client($repo);
if(false === $client->force_pull()) {
_doing_it_wrong(__METHOD__, 'Failed to pull from remote repository', '1.0.0');
}

self::$fs = new WP_Git_Filesystem(
$repo,
[
'root' => GIT_DIRECTORY_ROOT,
'auto_push' => true,
'client' => $client,
]
);
}
Expand All @@ -68,6 +78,7 @@ static public function initialize() {
register_activation_hook( __FILE__, array(self::class, 'import_static_pages') );

add_action('init', function() {
self::get_fs();
self::register_post_type();
});

Expand All @@ -86,7 +97,7 @@ static public function initialize() {
$post_id = wp_insert_post(array(
'post_title' => 'My first note',
'post_type' => WP_LOCAL_FILE_POST_TYPE,
'post_status' => 'draft'
'post_status' => 'publish'
));
} else {
$post_id = $posts[0]->ID;
Expand Down Expand Up @@ -440,7 +451,8 @@ static public function import_static_pages() {
$importer = WP_Stream_Importer::create(
function () {
return new WP_Filesystem_Entity_Reader(
new WP_Local_Filesystem(WP_STATIC_CONTENT_DIR),
self::get_fs(),
// new WP_Local_Filesystem(WP_STATIC_CONTENT_DIR)
array(
'post_type' => WP_LOCAL_FILE_POST_TYPE,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ bun --inspect ../cli/src/cli.ts \
--mount=../data-liberation-static-files-editor:/wordpress/wp-content/plugins/z-data-liberation-static-files-editor \
--mount=../data-liberation-markdown:/wordpress/wp-content/plugins/z-data-liberation-markdown \
--mount=../data-liberation:/wordpress/wp-content/plugins/data-liberation \
--mount=../../../../gutenberg:/wordpress/wp-content/plugins/gutenberg \
--mount=./my-notes/workdir:/wordpress/wp-content/uploads/static-pages \
--blueprint=./blueprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ public function list_objects($ref_hash) {
return WP_Git_Pack_Processor::decode($pack_data);
}

public function force_pull($branch_name, $path = '/') {
public function force_pull($branch_name=null, $path = '/') {
if(!$branch_name) {
$branch_name = $this->index->get_ref_head('HEAD', ['resolve_ref' => false]);
$branch_name = $this->localize_ref_name($branch_name);
}
$path = '/' . ltrim($path, '/');
$remote_refs = $this->fetchRefs('refs/heads/' . $branch_name);
$remote_head = $remote_refs['refs/heads/' . $branch_name];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,7 @@ public function get_ref_head($ref='HEAD', $options=[]) {
}
$contents = trim($this->fs->read_file($path));
if($options['resolve_ref'] ?? true) {
if(strpos($contents, 'ref: ') === 0) {
$branch = trim(substr($contents, 5));
return $this->get_ref_head($branch, $options);
}
return $this->get_ref_head($contents, $options);
}
return $contents;
}
Expand Down

0 comments on commit decd81e

Please sign in to comment.