-
Notifications
You must be signed in to change notification settings - Fork 6
/
wp-deploy.php
139 lines (106 loc) · 3.86 KB
/
wp-deploy.php
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
<?php
/*
* Plugin Name: WP-Deploy
* Plugin URI: http://github.com/vmassuchetto/wp-deploy
* Description: Deploy websites via the admin bar using Git
* Version: 0.01
* Author: Leo Germani, Vinicius Massuchetto
* Author URI: http://github.com/vmassuchetto/wp-deploy
*/
class WP_Deploy {
var $custom_vars;
function WP_Deploy() {
if ( !is_super_admin() || !is_admin_bar_showing() )
return;
$this->custom_vars = array(
'wp_deploy_update_info',
'wp_deploy_reset_branch'
);
add_action( 'query_vars', array( $this, 'query_vars' ) );
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 999 );
add_action( 'wp', array( $this, 'wp' ) );
}
function wp() {
foreach ( $this->custom_vars as $var ) {
if ( $arg = get_query_var( $var ) ) {
$func = array( $this, preg_replace( '/^wp_deploy_/', '', $var ) );
if ( is_callable( $func ) )
call_user_func( $func , $arg );
}
}
}
function query_vars( $vars ) {
return $this->custom_vars + $vars;
}
function admin_bar_menu() {
global $wp, $wp_admin_bar;
$cleaned_url = $this->cleaned_url();
$wp_admin_bar->add_menu( array(
'id' => 'wp_deploy',
'title' => __( 'Deploy', 'wp-deploy' ),
'href' => false ) );
$wp_admin_bar->add_menu( array(
'id' => 'wp_deploy_update_info',
'parent' => 'wp_deploy',
'title' => __( 'Update branches', 'wp-deploy' ),
'href' => add_query_arg(
array( 'wp_deploy_update_info' => true ),
$cleaned_url ) ) );
foreach ( get_option( 'wp_deploy_branches' ) as $branch => $title ) {
$wp_admin_bar->add_menu( array(
'id' => 'wp_deploy_branch_' . $branch,
'parent' => 'wp_deploy',
'title' => $title,
'href' => add_query_arg(
array( 'wp_deploy_reset_branch' => $branch ),
$cleaned_url ) ) );
}
}
function cleaned_url() {
if ( $cleaned_url = wp_cache_get( 'wp_deploy_cleaned_url' ) )
return $cleaned_url;
global $wp;
$cleaned_url = home_url( $wp->request );
foreach ( $this->custom_vars as $var ) {
$cleaned_url = remove_query_arg( $var, $cleaned_url );
}
wp_cache_set( 'wp_deploy_cleaned_url', $cleaned_url );
return $cleaned_url;
}
function update_info() {
exec( 'cd ' . ABSPATH );
exec( 'git fetch -p' );
exec( 'git branch --all --list --no-color \
| grep -v "HEAD" \
| sed -e "s/^\* //g" \
| sed -e "s/^ //g" \
| sort -u ', $_branches );
$current_branch = exec( 'git branch \
| grep "^*" \
| sed -e "s/^\* //g"' );
$branches = array();
foreach ( $_branches as $branch ) {
$revision = exec( 'git show ' . $branch . ' \
| head -1 \
| sed -e "s/[^ ]* \(.\{7\}\).*/\1/g"' );
$branch = preg_replace( '#.*/(.*)#', '\1', $branch );
$prefix = $branch == $current_branch ? '* ' : '';
$branches[ $branch ] = $prefix . $branch
. ' <span>' . trim( $revision ) . '</span>';
}
ksort( $branches );
update_option( 'wp_deploy_branches', $branches );
}
function reset_branch( $branch ) {
$branch = preg_replace('/^\*/', '', $branch);
exec( 'git reset --hard HEAD' );
exec( 'git checkout ' . $branch );
exec( 'git pull' );
$this->update_info();
}
}
function wp_deploy_init() {
new WP_Deploy();
}
add_action( 'plugins_loaded', 'wp_deploy_init' );
?>