forked from Automattic/babble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation-group-tool.php
264 lines (233 loc) · 8.67 KB
/
translation-group-tool.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
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
<?php
/*
Plugin Name: Babble: Translation Group Tool
Plugin URI: http://simonwheatley.co.uk/wordpress/btgt
Description: This provides a page in Admin > Tools which allows you to see and edit Babble translation associations.
Version: 0.1
Author: Simon Wheatley
Author URI: http://simonwheatley.co.uk/wordpress/
*/
/* Copyright 2012 Simon Wheatley
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
require_once 'class-plugin.php';
/**
* Handles the display and functionality of the translation group tool.
*
* @package BabbleTranslationGroupTool
* @author Simon Wheatley
**/
class BabbleTranslationGroupTool extends Babble_Plugin {
/**
* Initiate!
*
* @return void
* @access public
**/
function __construct() {
$this->setup( 'babble-tgt', 'plugin' );
$this->add_action( 'admin_menu' );
$this->add_action( 'load-post-new.php', 'load_post' );
$this->add_action( 'load-post.php', 'load_post' );
$this->add_action( 'load-tools_page_btgt', 'load_tools_page' );
$this->add_action( 'save_post', null, null, 2 );
$this->add_filter( 'bbl_metaboxes_for_translators', 'metaboxes_for_translators' );
$this->add_filter( 'bbl_pre_sync_properties', 'pre_sync_properties', null, 2 );
}
// HOOKS AND ALL THAT
// ==================
/**
* Hooks the WP admin_menu action to add a menu to
* the Tools section.
*
* @return void
**/
public function admin_menu() {
add_management_page( __( 'Translation Groups', 'babble-tgt' ), __( 'Translation Groups', 'babble-tgt' ), 'manage_options', 'btgt', array( $this, 'tools_page' ) );
}
/**
* Hooks the dynamic load-* action called when
* this tools page loads.
*
* @return void
**/
public function load_tools_page() {
if ( ! $action = ( isset( $_GET[ 'btgt_action' ] ) ) ? $_GET[ 'btgt_action' ] : false )
return;
$obj_id = ( isset( $_GET[ 'obj_id' ] ) ) ? $_GET[ 'obj_id' ] : false;
$wp_nonce = ( isset( $_GET[ '_wpnonce' ] ) ) ? $_GET[ '_wpnonce' ] : false;
switch ( $action ) {
case 'delete_from_groups':
if ( ! wp_verify_nonce( $wp_nonce, "btgt_delete_from_groups_$obj_id" ) ) {
$this->set_admin_error( 'Sorry, went wrong. Please try again.' );
return;
}
wp_delete_object_term_relationships( $obj_id, 'post_translation' );
$this->set_admin_notice( "Deleted term relationships for $obj_id" );
break;
case 'delete_post':
wp_delete_object_term_relationships( $obj_id, 'post_translation' );
wp_delete_post( $obj_id, true );
break;
case 'trash_post':
wp_delete_object_term_relationships( $obj_id, 'post_translation' );
wp_trash_post( $obj_id );
break;
}
$args = array(
'page' => 'btgt',
'lang' => bbl_get_default_lang_code(),
);
$url = add_query_arg( $args, admin_url( 'tools.php' ) );
$url .= '#' . $_GET[ 'anchor' ];
wp_redirect( $url );
}
/**
* Hooks the various dynamic actions fired when the edit post and
* edit new post screens are loaded. Determines if the post to be
* edited has become disconnected from it's translation group,
* and shows the Reconnect metabox if it has.
*
* @return void
**/
public function load_post() {
$screen = get_current_screen();
if ( ! $post_id = isset( $_GET[ 'post' ] ) ? $_GET[ 'post' ] : false )
return;
$post = get_post( $post_id );
if ( ! in_array( $post->post_status, array( 'draft', 'pending', 'publish' ) ) )
return;
if ( bbl_get_post_lang_code( $post ) == bbl_get_default_lang_code() )
return;
if ( $default_lang_post = bbl_get_post_in_lang( $post, bbl_get_default_lang_code() ) )
return;
$this->add_meta_box( 'bbl_reconnect', 'Reconnect Translation', 'metabox_reconnect', $post->post_type, 'side' );
}
/**
* Hooks the WP save_post action
*
* @param int $post_id The ID of the post being saved
* @param object $post The WordPress post object being saved
* @return void
**/
function save_post( $post_id, $post ) {
if ( ! in_array( $post->post_status, array( 'draft', 'publish' ) ) )
return;
if ( ! isset( $_POST[ '_bbl_reconnect_nonce' ] ) )
return;
$posted_id = isset( $_POST[ 'post_ID' ] ) ? $_POST[ 'post_ID' ] : 0;
if ( $posted_id != $post_id )
return;
// While we're at it, let's check the nonce
check_admin_referer( "bbl_reconnect_translation_$post_id", '_bbl_reconnect_nonce' );
// Check the user has set a transid
if ( ! $transid = isset( $_POST[ 'bbl_transid' ] ) ? (int) $_POST[ 'bbl_transid' ] : false )
return;
// Check the transid the user has set actually exists
if ( ! term_exists( $transid, 'post_translation' ) ) {
$this->set_admin_error( __( 'The TransID you want to reconnect this content to does not exist. Please check the Translation Group information and try again.', 'babble' ) );
return;
}
global $bbl_post_public;
$bbl_post_public->set_transid( $post, $transid );
}
/**
* Hooks the Babble bbl_pre_sync_properties filter to
* log any changes to parent. We're not making changes
* to the data, just logging significant changes for
* debug purposes.
*
* @param array $postdata The data which will be applied to the post as part of the sync
* @param int $origin_id The ID of the post we are syncing from
* @return array The data which will be applied to the post as part of the sync
**/
public function pre_sync_properties( $postdata, $origin_id ) {
$current_post = get_post( $postdata[ 'ID' ] );
$origin_post = get_post( $origin_id );
if ( $current_post->post_parent != $postdata[ 'post_parent' ] ) {
$user = wp_get_current_user();
$remote_ip = $_SERVER[ 'REMOTE_ADDR' ];
$referer = $_SERVER[ 'HTTP_REFERER' ];
$lang = bbl_get_current_lang_code();
$origin_lang = bbl_get_post_lang_code( $origin_id );
error_log( "Babble: $user->user_login has changed {$postdata[ 'ID' ]} parent from $current_post->post_parent ($current_post->post_type) to {$postdata[ 'post_parent' ]}. \tOrigin: $origin_id. Origin lang: $origin_lang. IP $remote_ip. User lang: $lang. Referer $referer." );
}
return $postdata;
}
/**
* Hooks the Babble bbl_metaboxes_for_translators filter to
* add the bbl_reconnect metabox to the list of boxes allowed
* on translator screens.
*
* @param array $boxes The array of box names which are allowed
* @return array The array of box names which are allowed
**/
function metaboxes_for_translators( $boxes ) {
$boxes[] = 'bbl_reconnect';
return $boxes;
}
// CALLBACKS
// =========
/**
* The callback function which provides HTML for the Babble
* Translation Reconnection metabox, which allows an admin
* to reconnect a post to the equivalent post in the
* default language.
*
* @param object $post The WP Post object being edited
* @param array $metabox The args and params for this metabox
* @return void (echoes HTML)
**/
public function metabox_reconnect( $post, $metabox ) {
wp_nonce_field( "bbl_reconnect_translation_{$post->ID}", '_bbl_reconnect_nonce' );
?>
<p>
<label for="bbl_transid">TransID
<input type="text" name="bbl_transid" value="" id="bbl_transid" />
</label><br />
<span class="description">The option to specify a TransID will only show up if something terminal has happened to this translation. Do not change this if you do not know what you are doing.</span>
</p>
<?php
}
/**
* Callback function for the HTML for the tools page.
*
* @return void
**/
public function tools_page() {
require_once 'translation-group-tool-sorter.php';
$vars = array();
$this->render_admin( 'translation-groups.php', $vars );
}
// UTILITIES
// =========
/**
* Get a link to trash a particular post.
*
* @param int $post_id The ID of the post to trash
* @param string $action The action for this link
* @return string A Nonced action URL
**/
protected function get_action_link( $obj_id, $action, $anchor = null ) {
$args = array(
'btgt_action' => $action,
'obj_id' => $obj_id,
'lang' => bbl_get_default_lang_code(),
);
if ( ! is_null( $anchor ) )
$args[ 'anchor' ] = $anchor;
return wp_nonce_url( add_query_arg( $args ), "btgt_{$action}_$obj_id" );
}
} // END BabbleTranslationGroupTool class
$bbl_translation_group_tool = new BabbleTranslationGroupTool();
?>