/
glotpress-bridge.php
162 lines (137 loc) · 5.32 KB
/
glotpress-bridge.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/*
Plugin Name: GlotPress Bridge
Description: Syncs admin users between WordPress & GlotPress
Author: 9seeds
Version: 1.0
Author URI: http://9seeds.com
*/
class GlotPress_Bridge {
private $has_cli = NULL;
public function hook() {
//add_filter( 'all', array( $this, 'hook_debug' ) );
//add_action( 'all', array( $this, 'hook_debug' ) );
add_action( 'admin_init', array( $this, 'register_bridge_settings' ) );
add_action( 'admin_menu', array( $this, 'add_bridge_menu' ) );
add_action( 'admin_init', array( $this, 'maybe_make_admin' ) );
add_action( 'set_user_role', array( $this, 'maybe_add_admin' ), 10, 2 );
add_filter( 'user_row_actions', array( $this, 'add_admin_action' ), 10, 2 );
}
public function hook_debug( $name ) {
file_put_contents( '/tmp/hook.txt', "{$name}\n", FILE_APPEND );
}
public function add_bridge_menu() {
add_options_page( __( 'GlotPress Settings', 'gp-bridge' ),
__( 'GlotPress', 'gp-bridge' ),
'edit_users',
'gp-bridge-options',
array( $this, 'print_bridge_options' ) );
}
public function register_bridge_settings() {
register_setting('gp-bridge-settings-group','glotpress_dir', array( $this, 'sanitize_dir' ) );
add_settings_section( 'gp_dir', __( 'GlotPress Directory', 'gp-bridge' ), array( $this, 'print_dir_instructions' ), 'gp-bridge' ); //NULL / NULL no section label needed
add_settings_field( 'glotpress_dir', __( 'GlotPress Directory', 'gp-bridge' ), array( $this, 'print_dir_input' ), 'gp-bridge', 'gp_dir' );
}
public function print_bridge_options() {
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br/></div>
<h2><?php _e( 'GlotPress Settings', 'gp-bridge' ); ?></h2>
<form method="post" action="<?php echo admin_url( 'options.php' ); ?>">
<?php settings_fields( 'gp-bridge-settings-group' ); ?>
<?php do_settings_sections( 'gp-bridge' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function print_dir_instructions() {
?><p><?php _e( 'Please specify the directory where can be found so GlotPress functionality may be included.', 'gp-bridge' );?> </p><?php
}
public function print_dir_input() {
$dir = get_option( 'glotpress_dir' );
if ( ! $dir )
$dir = ABSPATH;
?><input type="text" id="glotpress_dir" name="glotpress_dir" value="<?php echo $dir; ?>" class="regular-text" /><?php
}
public function sanitize_dir( $dir ) {
if ( ! is_dir( $dir ) ) {
add_settings_error( 'glotpress_dir', 'glotpress_dir', 'Directory not found' );
return $dir;
}
if ( file_exists( trailingslashit( $dir ) . 'scripts/add-admin.php' ) ) {
$output = '';
if ( ! $this->gp_load_cli( $output ) )
add_settings_error( 'glotpress_dir', 'glotpress_dir', 'Error running PHP CLI (not installed or not in path) ' . join( "\n", $output ) );
return $dir;
}
add_settings_error( 'glotpress_dir', 'glotpress_dir', 'GlotPress not found in specified directory' );
return $dir;
}
/**
* This won't work - there are function name conflicts between GlotPress & WordPress
private function gp_load_native() {
$gp_load = trailingslashit( get_option( 'glotpress_dir' ) ) . 'gp-load.php';
if ( file_exists( $gp_load ) ) {
require_once $gp_load;
return true;
}
return false;
}
private function add_admin_native( $user_login ) {
$user_to_make_admin = GP::$user->by_login( $user_login );
if ( !$user_to_make_admin ) {
wp_die( "User '{$user_login}' doesn't exist in GlotPress." );
}
if ( !GP::$permission->create( array( 'user_id' => $user_to_make_admin->id, 'action' => 'admin' ) ) ) {
wp_die( "Error making '{$user_login}' an admin." );
}
}
*/
public function add_admin_action( $actions, $user_object ) {
if ( $this->gp_load_cli() && current_user_can( 'edit_users' ) && in_array( 'administrator', $user_object->roles ) ) {
$actions['gp-add-admin'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=gp-add-admin&user=$user_object->ID", 'glotpress' ) . "'>" . __( 'Make GP Admin' ) . "</a>";
}
return $actions;
}
public function maybe_make_admin() {
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'gp-add-admin' ) {
check_admin_referer( 'glotpress' );
if ( empty( $_REQUEST['user'] ) )
return;
$this->maybe_add_admin( $_REQUEST['user'], 'administrator' );
wp_redirect( 'users.php' );
}
}
public function maybe_add_admin( $id, $role ) {
//not sure if this works for super-admin as well
if ( $role == 'administrator' && $this->gp_load_cli() ) {
$user = get_user_by( 'id', $id );
$this->add_admin_cli( $user->user_login );
}
}
/**
* A hack, but should work in at least some instances
*/
private function gp_load_cli( &$output = NULL ) {
if ( $this->has_cli !== NULL )
return $this->has_cli;
exec( 'php -v', $output, $return );
//anything but zero (0) here is an error
if ( $return )
$this->has_cli = false;
else
$this->has_cli = true;
return $this->has_cli;
}
private function add_admin_cli( $user_login ) {
$add_admin = trailingslashit( get_option( 'glotpress_dir' ) ) . 'scripts/add-admin.php';
if ( file_exists( $add_admin ) ) {
exec( "php {$add_admin} {$user_login} 2>&1", $output, $return );
if ( $return )
wp_die( 'Error adding GlotPress admin via CLI: ' . join( "\n", $output ) );
}
}
}
$gp_bridge = new GlotPress_Bridge();
$gp_bridge->hook();