-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub-to-wordpress-sync.php
More file actions
1356 lines (1125 loc) · 47.9 KB
/
github-to-wordpress-sync.php
File metadata and controls
1356 lines (1125 loc) · 47.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Github to WordPress Sync
* Plugin URI: https://github.com/sinanisler/github-to-wordpress-sync
* Description: GitHub to WordPress Sync: Streamline theme & plugin updates directly from GitHub. Easy, secure, and developer-friendly.
* Version: 0.10
* Author: sinanisler
* Author URI: https://github.com/sinanisler
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: snn
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('GTWS_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('GTWS_PLUGIN_URL', plugin_dir_url(__FILE__));
define('GTWS_PLUGIN_FILE', __FILE__);
/**
* GitHub API Integration Class
*/
class GTWS_Github_API {
/**
* Get repository owner and name from URL
*/
private function parse_github_url($url) {
// Remove .git extension if present
$url = str_replace('.git', '', $url);
$url = rtrim($url, '/');
// Extract owner and repo name
if (preg_match('/github\.com\/([^\/]+)\/([^\/]+)/', $url, $matches)) {
return array(
'owner' => $matches[1],
'repo' => $matches[2]
);
}
return false;
}
/**
* Get latest commit from a branch
*/
public function get_latest_commit($repo_url, $branch = 'main') {
$repo_info = $this->parse_github_url($repo_url);
if (!$repo_info) {
return false;
}
$api_url = sprintf(
'https://api.github.com/repos/%s/%s/commits/%s',
$repo_info['owner'],
$repo_info['repo'],
$branch
);
$response = $this->make_api_request($api_url);
if (!$response) {
return false;
}
return array(
'sha' => $response['sha'],
'message' => isset($response['commit']['message']) ? $response['commit']['message'] : '',
'date' => isset($response['commit']['committer']['date']) ? $response['commit']['committer']['date'] : '',
'author' => isset($response['commit']['author']['name']) ? $response['commit']['author']['name'] : '',
);
}
/**
* Get all branches for a repository
*/
public function get_branches($repo_url) {
$repo_info = $this->parse_github_url($repo_url);
if (!$repo_info) {
return false;
}
$api_url = sprintf(
'https://api.github.com/repos/%s/%s/branches',
$repo_info['owner'],
$repo_info['repo']
);
$response = $this->make_api_request($api_url);
if (!$response || !is_array($response)) {
return false;
}
$branches = array();
foreach ($response as $branch) {
if (isset($branch['name'])) {
$branches[] = $branch['name'];
}
}
return $branches;
}
/**
* Download repository as ZIP
*/
public function download_repository($repo_url, $branch = 'main') {
$repo_info = $this->parse_github_url($repo_url);
if (!$repo_info) {
return false;
}
// GitHub archive URL
$download_url = sprintf(
'https://github.com/%s/%s/archive/refs/heads/%s.zip',
$repo_info['owner'],
$repo_info['repo'],
$branch
);
// Create temporary file
$temp_file = wp_tempnam('gtws-repo-');
// Download the file
$response = wp_remote_get($download_url, array(
'timeout' => 300,
'stream' => true,
'filename' => $temp_file
));
if (is_wp_error($response)) {
@unlink($temp_file);
return false;
}
$response_code = wp_remote_retrieve_response_code($response);
if ($response_code !== 200) {
@unlink($temp_file);
return false;
}
return $temp_file;
}
/**
* Get repository info
*/
public function get_repository_info($repo_url) {
$repo_info = $this->parse_github_url($repo_url);
if (!$repo_info) {
return false;
}
$api_url = sprintf(
'https://api.github.com/repos/%s/%s',
$repo_info['owner'],
$repo_info['repo']
);
$response = $this->make_api_request($api_url);
if (!$response) {
return false;
}
return array(
'name' => isset($response['name']) ? $response['name'] : '',
'description' => isset($response['description']) ? $response['description'] : '',
'default_branch' => isset($response['default_branch']) ? $response['default_branch'] : 'main',
'updated_at' => isset($response['updated_at']) ? $response['updated_at'] : '',
'private' => isset($response['private']) ? $response['private'] : false,
);
}
/**
* Make API request to GitHub
*/
private function make_api_request($url) {
$args = array(
'timeout' => 15,
'headers' => array(
'Accept' => 'application/vnd.github.v3+json',
'User-Agent' => 'WordPress-Github-Sync-Plugin'
)
);
$response = wp_remote_get($url, $args);
if (is_wp_error($response)) {
return false;
}
$response_code = wp_remote_retrieve_response_code($response);
if ($response_code !== 200) {
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
return $data;
}
/**
* Get commits between two commits
*/
public function get_commits_between($repo_url, $branch, $since_commit = null) {
$repo_info = $this->parse_github_url($repo_url);
if (!$repo_info) {
return false;
}
$api_url = sprintf(
'https://api.github.com/repos/%s/%s/commits?sha=%s',
$repo_info['owner'],
$repo_info['repo'],
$branch
);
if ($since_commit) {
$api_url .= '&since=' . urlencode($since_commit);
}
$response = $this->make_api_request($api_url);
if (!$response || !is_array($response)) {
return false;
}
$commits = array();
foreach ($response as $commit) {
if (isset($commit['sha'])) {
$commits[] = array(
'sha' => $commit['sha'],
'message' => isset($commit['commit']['message']) ? $commit['commit']['message'] : '',
'date' => isset($commit['commit']['committer']['date']) ? $commit['commit']['committer']['date'] : '',
'author' => isset($commit['commit']['author']['name']) ? $commit['commit']['author']['name'] : '',
);
}
}
return $commits;
}
/**
* Get commit history for a branch (limited to recent commits)
*/
public function get_commit_history($repo_url, $branch = 'main', $per_page = 20) {
$repo_info = $this->parse_github_url($repo_url);
if (!$repo_info) {
return false;
}
$api_url = sprintf(
'https://api.github.com/repos/%s/%s/commits?sha=%s&per_page=%d',
$repo_info['owner'],
$repo_info['repo'],
$branch,
$per_page
);
$response = $this->make_api_request($api_url);
if (!$response || !is_array($response)) {
return false;
}
$commits = array();
foreach ($response as $commit) {
if (isset($commit['sha'])) {
$commits[] = array(
'sha' => $commit['sha'],
'message' => isset($commit['commit']['message']) ? $commit['commit']['message'] : '',
'date' => isset($commit['commit']['committer']['date']) ? $commit['commit']['committer']['date'] : '',
'author' => isset($commit['commit']['author']['name']) ? $commit['commit']['author']['name'] : '',
);
}
}
return $commits;
}
/**
* Download repository at specific commit
*/
public function download_repository_at_commit($repo_url, $commit_sha) {
$repo_info = $this->parse_github_url($repo_url);
if (!$repo_info) {
return false;
}
// GitHub archive URL for specific commit
$download_url = sprintf(
'https://github.com/%s/%s/archive/%s.zip',
$repo_info['owner'],
$repo_info['repo'],
$commit_sha
);
// Create temporary file
$temp_file = wp_tempnam('gtws-repo-');
// Download the file
$response = wp_remote_get($download_url, array(
'timeout' => 300,
'stream' => true,
'filename' => $temp_file
));
if (is_wp_error($response)) {
@unlink($temp_file);
return false;
}
$response_code = wp_remote_retrieve_response_code($response);
if ($response_code !== 200) {
@unlink($temp_file);
return false;
}
return $temp_file;
}
}
/**
* Sync Manager Class
* Handles downloading and syncing GitHub repositories to WordPress
*/
class GTWS_Sync_Manager {
private $github_api;
public function __construct() {
$this->github_api = new GTWS_Github_API();
}
/**
* Sync a project from GitHub (to latest commit or specific commit)
*/
public function sync_project($project, $commit_sha = null) {
try {
// Download repository
if ($commit_sha) {
$zip_file = $this->github_api->download_repository_at_commit(
$project['github_url'],
$commit_sha
);
} else {
$zip_file = $this->github_api->download_repository(
$project['github_url'],
$project['branch']
);
}
if (!$zip_file) {
return array(
'success' => false,
'message' => 'Failed to download repository from GitHub'
);
}
// Determine target directory
$target_dir = $this->get_target_directory($project['project_type'], $project['project_name']);
if (!$target_dir) {
@unlink($zip_file);
return array(
'success' => false,
'message' => 'Invalid project type'
);
}
// Extract and sync
$result = $this->extract_and_sync($zip_file, $target_dir, $project, $commit_sha);
// Clean up temp file
@unlink($zip_file);
return $result;
} catch (Exception $e) {
return array(
'success' => false,
'message' => 'Error: ' . $e->getMessage()
);
}
}
/**
* Get target directory based on project type
*/
private function get_target_directory($type, $name) {
switch ($type) {
case 'theme':
return WP_CONTENT_DIR . '/themes/' . $name;
case 'plugin':
return WP_CONTENT_DIR . '/plugins/' . $name;
default:
return false;
}
}
/**
* Extract ZIP and sync to target directory
*/
private function extract_and_sync($zip_file, $target_dir, $project, $commit_sha = null) {
// Load WordPress filesystem
WP_Filesystem();
global $wp_filesystem;
// Create temporary extraction directory
$temp_dir = wp_tempnam('gtws-extract-');
@unlink($temp_dir);
wp_mkdir_p($temp_dir);
// Extract ZIP file
$unzip_result = unzip_file($zip_file, $temp_dir);
if (is_wp_error($unzip_result)) {
$this->cleanup_directory($temp_dir);
return array(
'success' => false,
'message' => 'Failed to extract ZIP file: ' . $unzip_result->get_error_message()
);
}
// Find the extracted folder (GitHub creates a folder with repo-branch name or repo-commit)
$extracted_folders = glob($temp_dir . '/*', GLOB_ONLYDIR);
if (empty($extracted_folders)) {
$this->cleanup_directory($temp_dir);
return array(
'success' => false,
'message' => 'No folder found in extracted archive'
);
}
$source_dir = $extracted_folders[0];
// Remove existing directory completely (no backup - we have Git history!)
if (file_exists($target_dir)) {
$this->cleanup_directory($target_dir);
}
// Create target directory
wp_mkdir_p($target_dir);
// Sync files from source to target
$sync_result = $this->sync_directories($source_dir, $target_dir);
// Clean up temp directory
$this->cleanup_directory($temp_dir);
if ($sync_result) {
return array(
'success' => true,
'message' => 'Project synced successfully!',
'target_dir' => $target_dir,
'commit_sha' => $commit_sha
);
} else {
return array(
'success' => false,
'message' => 'Failed to sync files'
);
}
}
/**
* Sync files from source to target directory
*/
private function sync_directories($source, $target) {
if (!is_dir($source)) {
return false;
}
// Create target directory if it doesn't exist
if (!file_exists($target)) {
wp_mkdir_p($target);
}
// Get all files and directories
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($items as $item) {
$target_path = $target . DIRECTORY_SEPARATOR . $items->getSubPathName();
if ($item->isDir()) {
// Create directory
if (!file_exists($target_path)) {
wp_mkdir_p($target_path);
}
} else {
// Copy file
if (!copy($item, $target_path)) {
return false;
}
}
}
return true;
}
/**
* Get sync history for a project (stored in project data)
*/
public function get_sync_history($project_id) {
$projects = get_option('gtws_projects', array());
$project_index = array_search($project_id, array_column($projects, 'id'));
if ($project_index === false) {
return false;
}
$project = $projects[$project_index];
// Get sync history from project data
return isset($project['sync_history']) ? $project['sync_history'] : array();
}
/**
* Add sync record to project history
*/
public function add_sync_record($project_id, $commit_sha, $commit_message, $commit_date) {
$projects = get_option('gtws_projects', array());
$project_index = array_search($project_id, array_column($projects, 'id'));
if ($project_index === false) {
return false;
}
// Initialize sync history if not exists
if (!isset($projects[$project_index]['sync_history'])) {
$projects[$project_index]['sync_history'] = array();
}
// Add new sync record at the beginning (most recent first)
array_unshift($projects[$project_index]['sync_history'], array(
'commit_sha' => $commit_sha,
'commit_message' => $commit_message,
'commit_date' => $commit_date,
'synced_at' => current_time('mysql', true)
));
// Keep only last 20 sync records
$projects[$project_index]['sync_history'] = array_slice(
$projects[$project_index]['sync_history'],
0,
20
);
update_option('gtws_projects', $projects);
return true;
}
/**
* Clean up directory recursively
*/
private function cleanup_directory($dir) {
if (!file_exists($dir)) {
return;
}
if (is_dir($dir)) {
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
if ($item->isDir()) {
@rmdir($item->getRealPath());
} else {
@unlink($item->getRealPath());
}
}
@rmdir($dir);
}
}
/**
* Get list of files in directory
*/
public function get_directory_files($dir) {
if (!is_dir($dir)) {
return array();
}
$files = array();
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($items as $item) {
$files[] = $items->getSubPathName();
}
return $files;
}
/**
* Check if directory exists and is writable
*/
public function check_directory_permissions($type, $name) {
$dir = $this->get_target_directory($type, $name);
if (!$dir) {
return array(
'exists' => false,
'writable' => false,
'path' => ''
);
}
$parent_dir = dirname($dir);
return array(
'exists' => file_exists($dir),
'writable' => is_writable($parent_dir) && (!file_exists($dir) || is_writable($dir)),
'path' => $dir
);
}
}
/**
* Main Plugin Class
*/
class Github_To_WordPress_Sync {
private static $instance = null;
/**
* Get singleton instance
*/
public static function get_instance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->init();
}
/**
* Initialize plugin
*/
private function init() {
// Register activation/deactivation hooks
register_activation_hook(GTWS_PLUGIN_FILE, array($this, 'activate'));
register_deactivation_hook(GTWS_PLUGIN_FILE, array($this, 'deactivate'));
// Initialize admin
if (is_admin()) {
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets'));
}
// AJAX handlers
add_action('wp_ajax_gtws_add_project', array($this, 'ajax_add_project'));
add_action('wp_ajax_gtws_delete_project', array($this, 'ajax_delete_project'));
add_action('wp_ajax_gtws_check_updates', array($this, 'ajax_check_updates'));
add_action('wp_ajax_gtws_sync_project', array($this, 'ajax_sync_project'));
add_action('wp_ajax_gtws_get_branches', array($this, 'ajax_get_branches'));
add_action('wp_ajax_gtws_get_commit_history', array($this, 'ajax_get_commit_history'));
add_action('wp_ajax_gtws_restore_commit', array($this, 'ajax_restore_commit'));
}
/**
* Activate plugin
*/
public function activate() {
// Create options for storing projects
if (!get_option('gtws_projects')) {
add_option('gtws_projects', array());
}
}
/**
* Deactivate plugin
*/
public function deactivate() {
// Clean up if needed
}
/**
* Add admin menu
*/
public function add_admin_menu() {
add_options_page(
__('Github to WordPress Sync', 'github-to-wordpress-sync'),
__('Github Sync', 'github-to-wordpress-sync'),
'manage_options',
'github-to-wordpress-sync',
array($this, 'render_admin_page')
);
}
/**
* Enqueue admin assets
*/
public function enqueue_admin_assets($hook) {
// Only load on our settings page
if ($hook !== 'settings_page_github-to-wordpress-sync') {
return;
}
// Get version from plugin header
if (!function_exists('get_plugin_data')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data(GTWS_PLUGIN_FILE);
$version = $plugin_data['Version'];
wp_enqueue_style(
'gtws-admin-style',
GTWS_PLUGIN_URL . 'assets/css/admin.css',
array(),
$version
);
wp_enqueue_script(
'gtws-admin-script',
GTWS_PLUGIN_URL . 'assets/js/admin.js',
array('jquery'),
$version,
true
);
wp_localize_script('gtws-admin-script', 'gtws_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('gtws_nonce'),
));
}
/**
* Render admin page
*/
public function render_admin_page() {
$projects = get_option('gtws_projects', array());
?>
<div class="wrap gtws-admin-wrap">
<h1>
<span class="dashicons dashicons-update"></span>
<?php _e('Github to WordPress Sync', 'snn'); ?>
</h1>
<div class="gtws-container">
<!-- Add New Project Section -->
<div class="gtws-card gtws-collapsible-card">
<h2 class="gtws-card-header" data-toggle="collapse">
<span class="gtws-toggle-icon dashicons dashicons-arrow-right"></span>
<?php _e('Add New Project', 'snn'); ?>
</h2>
<div class="gtws-card-content" style="display: none;">
<form id="gtws-add-project-form" class="gtws-form">
<div class="gtws-form-group">
<label for="github_url">
<?php _e('GitHub Repository URL', 'snn'); ?>
<span class="required">*</span>
</label>
<input
type="text"
id="github_url"
name="github_url"
placeholder="https://github.com/username/repository-name"
required
>
<p class="description">
<?php _e('Enter the GitHub repository URL (without .git extension)', 'snn'); ?>
</p>
</div>
<div class="gtws-form-row">
<div class="gtws-form-group">
<label for="project_type">
<?php _e('Project Type', 'snn'); ?>
<span class="required">*</span>
</label>
<select id="project_type" name="project_type" required>
<option value=""><?php _e('Select Type', 'snn'); ?></option>
<option value="theme"><?php _e('Theme', 'snn'); ?></option>
<option value="plugin"><?php _e('Plugin', 'snn'); ?></option>
</select>
</div>
<div class="gtws-form-group">
<label for="project_name">
<?php _e('Project Name (Folder Name)', 'snn'); ?>
<span class="required">*</span>
</label>
<input
type="text"
id="project_name"
name="project_name"
placeholder="my-theme or my-plugin"
required
>
<p class="description">
<?php _e('The folder name in wp-content/themes or wp-content/plugins', 'snn'); ?>
</p>
</div>
</div>
<div class="gtws-form-group">
<label for="branch">
<?php _e('Branch', 'snn'); ?>
<span class="required">*</span>
</label>
<div class="gtws-branch-selector">
<input
type="text"
id="branch"
name="branch"
value="main"
required
>
<button type="button" id="fetch-branches" class="button">
<?php _e('Fetch Branches', 'snn'); ?>
</button>
</div>
<div id="branch-list" class="gtws-branch-list"></div>
</div>
<button type="submit" class="button button-primary button-large">
<span class="dashicons dashicons-plus-alt"></span>
<?php _e('Add Project', 'snn'); ?>
</button>
</form>
</div>
</div>
<!-- Projects List -->
<div class="gtws-card">
<h2><?php _e('Synced Projects', 'snn'); ?></h2>
<?php if (empty($projects)): ?>
<div class="gtws-empty-state">
<span class="dashicons dashicons-admin-plugins"></span>
<p><?php _e('No projects added yet. Add your first project above!', 'snn'); ?></p>
</div>
<?php else: ?>
<div class="gtws-projects-list">
<?php foreach ($projects as $project): ?>
<div class="gtws-project-item" data-project-id="<?php echo esc_attr($project['id']); ?>">
<div class="gtws-project-header">
<div class="gtws-project-info">
<h3>
<span class="gtws-project-type gtws-type-<?php echo esc_attr($project['project_type']); ?>">
<?php echo esc_html(ucfirst($project['project_type'])); ?>
</span>
<?php echo esc_html($project['project_name']); ?>
</h3>
<p class="gtws-project-url">
<span class="dashicons dashicons-admin-links"></span>
<a href="<?php echo esc_url($project['display_url']); ?>" target="_blank">
<?php echo esc_html($project['display_url']); ?>
</a>
</p>
</div>
<div class="gtws-project-actions">
<button
class="button gtws-view-history"
data-project-id="<?php echo esc_attr($project['id']); ?>"
>
<span class="dashicons dashicons-backup"></span>
<?php _e('History', 'snn'); ?>
</button>
<button
class="button gtws-check-update"
data-project-id="<?php echo esc_attr($project['id']); ?>"
>
<span class="dashicons dashicons-update"></span>
<?php _e('Check Update', 'snn'); ?>
</button>
<button
class="button button-primary gtws-sync-project"
data-project-id="<?php echo esc_attr($project['id']); ?>"
>
<span class="dashicons dashicons-download"></span>
<?php _e('Sync Now', 'snn'); ?>
</button>
<button
class="button button-link-delete gtws-delete-project"
data-project-id="<?php echo esc_attr($project['id']); ?>"
>
<span class="dashicons dashicons-trash"></span>
</button>
</div>
</div>
<div class="gtws-project-details">
<div class="gtws-detail-item">
<strong><?php _e('Branch:', 'snn'); ?></strong>
<span class="gtws-branch-badge"><?php echo esc_html($project['branch']); ?></span>
</div>
<?php if (!empty($project['last_commit'])): ?>
<div class="gtws-detail-item">
<strong><?php _e('Latest Commit:', 'snn'); ?></strong>
<code><?php echo esc_html(substr($project['last_commit'], 0, 7)); ?></code>
<?php if (!empty($project['commit_message'])): ?>
<span class="gtws-commit-message">
- <?php echo esc_html($project['commit_message']); ?>
</span>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if (!empty($project['commit_date'])): ?>
<div class="gtws-detail-item">
<strong><?php _e('Commit Date:', 'snn'); ?></strong>
<?php echo esc_html(wp_date('F j, Y g:i a', strtotime($project['commit_date']))); ?>
</div>
<?php endif; ?>
<?php if (!empty($project['last_sync'])): ?>
<div class="gtws-detail-item">
<strong><?php _e('Last Sync:', 'snn'); ?></strong>
<?php echo esc_html(wp_date('F j, Y g:i a', strtotime($project['last_sync']))); ?>
<?php if (!empty($project['last_sync_commit'])): ?>
<code><?php echo esc_html(substr($project['last_sync_commit'], 0, 7)); ?></code>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<div class="gtws-project-status"></div>
<!-- Commit History Section (Hidden by default) -->
<div class="gtws-commit-history" style="display: none;">
<h4><?php _e('Commit History', 'snn'); ?></h4>
<div class="gtws-history-timeline">
<!-- Timeline will be populated via JavaScript -->
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<!-- Info Section -->
<div class="gtws-card gtws-info-card gtws-collapsible-card">
<h3 class="gtws-card-header" data-toggle="collapse">
<span class="gtws-toggle-icon dashicons dashicons-arrow-right"></span>
<?php _e('How to Use', 'snn'); ?>
</h3>
<div class="gtws-card-content" style="display: none;">
<ol>
<li><?php _e('Add your GitHub repository URL (e.g., https://github.com/username/repo)', 'snn'); ?></li>
<li><?php _e('Select whether it\'s a theme or plugin', 'snn'); ?></li>
<li><?php _e('Enter the exact folder name as it should appear in wp-content/themes or wp-content/plugins', 'snn'); ?></li>
<li><?php _e('Choose the branch you want to sync (usually "main" or "master")', 'snn'); ?></li>
<li><?php _e('Click "Add Project" to save', 'snn'); ?></li>
<li><?php _e('Use "Check Update" to see if there are new commits', 'snn'); ?></li>
<li><?php _e('Click "Sync Now" to download and update your theme/plugin', 'snn'); ?></li>
</ol>
<div class="gtws-warning">
<span class="dashicons dashicons-warning"></span>
<strong><?php _e('Important:', 'snn'); ?></strong>
<?php _e('Syncing will overwrite local changes. Make sure to commit your work to GitHub first!', 'snn'); ?>
</div>
</div>
</div>
</div>
</div>
<?php