| 1 |
<?php |
| 2 |
|
| 3 |
/* Posts List Table */ |
| 4 |
|
| 5 |
add_filter( 'manage_pages_columns', 'bogo_pages_columns', 10, 1 ); |
| 6 |
|
| 7 |
function bogo_pages_columns( $posts_columns ) { |
| 8 |
return bogo_posts_columns( $posts_columns, 'page' ); |
| 9 |
} |
| 10 |
|
| 11 |
|
| 12 |
add_filter( 'manage_posts_columns', 'bogo_posts_columns', 10, 2 ); |
| 13 |
|
| 14 |
function bogo_posts_columns( $posts_columns, $post_type ) { |
| 15 |
if ( ! bogo_is_localizable_post_type( $post_type ) ) { |
| 16 |
return $posts_columns; |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! isset( $posts_columns['locale'] ) ) { |
| 20 |
$posts_columns = array_merge( |
| 21 |
array_slice( $posts_columns, 0, 3 ), |
| 22 |
array( 'locale' => __( 'Locale', 'bogo' ) ), |
| 23 |
array_slice( $posts_columns, 3 ) |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
return $posts_columns; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
add_action( 'manage_pages_custom_column', |
| 32 |
'bogo_manage_posts_custom_column', 10, 2 |
| 33 |
); |
| 34 |
|
| 35 |
add_action( 'manage_posts_custom_column', |
| 36 |
'bogo_manage_posts_custom_column', 10, 2 |
| 37 |
); |
| 38 |
|
| 39 |
function bogo_manage_posts_custom_column( $column_name, $post_id ) { |
| 40 |
if ( 'locale' !== $column_name ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$post_type = get_post_type( $post_id ); |
| 45 |
|
| 46 |
if ( ! bogo_is_localizable_post_type( $post_type ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$locale = get_post_meta( $post_id, '_locale', true ); |
| 51 |
|
| 52 |
if ( empty( $locale ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$language = bogo_get_language( $locale ); |
| 57 |
|
| 58 |
if ( empty( $language ) ) { |
| 59 |
$language = $locale; |
| 60 |
} |
| 61 |
|
| 62 |
echo sprintf( '<a href="%1$s">%2$s</a>', |
| 63 |
esc_url( |
| 64 |
add_query_arg( array( |
| 65 |
'post_type' => $post_type, |
| 66 |
'lang' => $locale, |
| 67 |
), 'edit.php' ) |
| 68 |
), |
| 69 |
esc_html( $language ) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
add_action( 'restrict_manage_posts', 'bogo_restrict_manage_posts', 10, 2 ); |
| 75 |
|
| 76 |
function bogo_restrict_manage_posts( $post_type, $which ) { |
| 77 |
if ( ! bogo_is_localizable_post_type( $post_type ) ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$available_languages = bogo_available_languages(); |
| 82 |
$current_locale = $_GET['lang'] ?? ''; |
| 83 |
|
| 84 |
$options = array( |
| 85 |
sprintf( |
| 86 |
'<option %1$s>%2$s</option>', |
| 87 |
bogo_format_atts( array( |
| 88 |
'value' => '', |
| 89 |
'selected' => '' === $current_locale, |
| 90 |
) ), |
| 91 |
__( 'Show all locales', 'bogo' ) |
| 92 |
), |
| 93 |
); |
| 94 |
|
| 95 |
foreach ( $available_languages as $locale => $lang ) { |
| 96 |
$options[] = sprintf( |
| 97 |
'<option %1$s>%2$s</option>', |
| 98 |
bogo_format_atts( array( |
| 99 |
'value' => $locale, |
| 100 |
'selected' => $locale === $current_locale, |
| 101 |
) ), |
| 102 |
$lang |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
$dropdown = sprintf( |
| 107 |
'<select name="lang">%s</select>', |
| 108 |
implode( $options ) |
| 109 |
); |
| 110 |
|
| 111 |
echo wp_kses( $dropdown, 'bogo_form_inside' ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
add_filter( 'post_row_actions', 'bogo_post_row_actions', 10, 2 ); |
| 116 |
add_filter( 'page_row_actions', 'bogo_post_row_actions', 10, 2 ); |
| 117 |
|
| 118 |
function bogo_post_row_actions( $actions, $post ) { |
| 119 |
if ( |
| 120 |
! bogo_is_localizable_post_type( $post->post_type ) or |
| 121 |
'trash' === $post->post_status |
| 122 |
) { |
| 123 |
return $actions; |
| 124 |
} |
| 125 |
|
| 126 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 127 |
|
| 128 |
if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) { |
| 129 |
return $actions; |
| 130 |
} |
| 131 |
|
| 132 |
$user_locale = bogo_get_user_locale(); |
| 133 |
$post_locale = bogo_get_post_locale( $post->ID ); |
| 134 |
|
| 135 |
if ( $user_locale === $post_locale ) { |
| 136 |
return $actions; |
| 137 |
} |
| 138 |
|
| 139 |
if ( $translation = bogo_get_post_translation( $post, $user_locale ) ) { |
| 140 |
if ( empty( $translation->ID ) or $translation->ID === $post->ID ) { |
| 141 |
return $actions; |
| 142 |
} |
| 143 |
|
| 144 |
/* translators: %s: Language name */ |
| 145 |
$text = __( 'Edit %s translation', 'bogo' ); |
| 146 |
|
| 147 |
$edit_link = get_edit_post_link( $translation->ID ); |
| 148 |
} else { |
| 149 |
/* translators: %s: Language name */ |
| 150 |
$text = __( 'Translate into %s', 'bogo' ); |
| 151 |
|
| 152 |
$edit_link = admin_url( 'post-new.php?post_type=' . $post->post_type |
| 153 |
. '&action=bogo-add-translation' |
| 154 |
. '&locale=' . $user_locale |
| 155 |
. '&original_post=' . $post->ID |
| 156 |
); |
| 157 |
|
| 158 |
$edit_link = wp_nonce_url( $edit_link, 'bogo-add-translation' ); |
| 159 |
} |
| 160 |
|
| 161 |
$language = bogo_get_language( $user_locale ); |
| 162 |
|
| 163 |
if ( empty( $language ) ) { |
| 164 |
$language = $user_locale; |
| 165 |
} |
| 166 |
|
| 167 |
$actions['translate'] = sprintf( |
| 168 |
'<a href="%1$s">%2$s</a>', |
| 169 |
$edit_link, |
| 170 |
esc_html( sprintf( $text, $language ) ) |
| 171 |
); |
| 172 |
|
| 173 |
return $actions; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
add_action( 'admin_init', 'bogo_add_translation', 10, 0 ); |
| 178 |
|
| 179 |
function bogo_add_translation() { |
| 180 |
if ( |
| 181 |
empty( $_REQUEST['action'] ) or |
| 182 |
'bogo-add-translation' !== $_REQUEST['action'] |
| 183 |
) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
check_admin_referer( 'bogo-add-translation' ); |
| 188 |
|
| 189 |
$locale = trim( $_REQUEST['locale'] ?? '' ); |
| 190 |
$original_post = absint( $_REQUEST['original_post'] ?? 0 ); |
| 191 |
|
| 192 |
if ( ! bogo_is_available_locale( $locale ) ) { |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
if ( ! $original_post or ! $original_post = get_post( $original_post ) ) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
$post_type_object = get_post_type_object( $original_post->post_type ); |
| 201 |
|
| 202 |
if ( |
| 203 |
$post_type_object and |
| 204 |
current_user_can( $post_type_object->cap->edit_posts ) |
| 205 |
) { |
| 206 |
$new_post_id = bogo_duplicate_post( $original_post, $locale ); |
| 207 |
|
| 208 |
if ( $new_post_id ) { |
| 209 |
$redirect_to = get_edit_post_link( $new_post_id, 'raw' ); |
| 210 |
wp_safe_redirect( $redirect_to ); |
| 211 |
exit(); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
/* Single Post */ |
| 218 |
|
| 219 |
add_action( 'add_meta_boxes', 'bogo_add_l10n_meta_boxes', 10, 2 ); |
| 220 |
|
| 221 |
function bogo_add_l10n_meta_boxes( $post_type, $post ) { |
| 222 |
if ( ! bogo_is_localizable_post_type( $post_type ) ) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
if ( in_array( $post_type, array( 'comment', 'link' ) ) ) { |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
add_meta_box( 'bogol10ndiv', __( 'Language', 'bogo' ), |
| 231 |
'bogo_l10n_meta_box', null, 'side', 'high', |
| 232 |
array( |
| 233 |
'__back_compat_meta_box' => true, |
| 234 |
) |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
function bogo_l10n_meta_box( $post ) { |
| 240 |
$initial = ( 'auto-draft' === $post->post_status ); |
| 241 |
|
| 242 |
if ( $initial ) { |
| 243 |
$post_locale = bogo_get_user_locale(); |
| 244 |
} else { |
| 245 |
$post_locale = bogo_get_post_locale( $post->ID ); |
| 246 |
} |
| 247 |
|
| 248 |
$translations = bogo_get_post_translations( $post->ID ); |
| 249 |
|
| 250 |
$available_languages = bogo_available_languages( array( |
| 251 |
'current_user_can_access' => true, |
| 252 |
) ); |
| 253 |
|
| 254 |
$post_language = $available_languages[$post_locale] ?? $post_locale; |
| 255 |
|
| 256 |
unset( $available_languages[$post_locale] ); |
| 257 |
|
| 258 |
?> |
| 259 |
|
| 260 |
<div class="descriptions"> |
| 261 |
<p><?php |
| 262 |
|
| 263 |
echo wp_kses_data( sprintf( |
| 264 |
'<strong>%1$s</strong> %2$s', |
| 265 |
__( 'Language:', 'bogo' ), |
| 266 |
$post_language |
| 267 |
) ); |
| 268 |
|
| 269 |
?></p> |
| 270 |
</div> |
| 271 |
|
| 272 |
<div class="descriptions"> |
| 273 |
<p> |
| 274 |
<strong><?php echo wp_kses_data( __( 'Translations:', 'bogo' ) ); ?></strong> |
| 275 |
</p> |
| 276 |
|
| 277 |
<ul id="bogo-translations"> |
| 278 |
<?php |
| 279 |
|
| 280 |
foreach ( $translations as $locale => $translation ) { |
| 281 |
$li_content = get_the_title( $translation->ID ); |
| 282 |
|
| 283 |
if ( $edit_link = get_edit_post_link( $translation->ID ) ) { |
| 284 |
$li_content = sprintf( |
| 285 |
'<a %1$s>%2$s<span class="screen-reader-text">%3$s</span></a>', |
| 286 |
bogo_format_atts( array( |
| 287 |
'href' => esc_url( $edit_link ), |
| 288 |
'target' => '_blank', |
| 289 |
) ), |
| 290 |
$li_content, |
| 291 |
/* translators: accessibility text */ |
| 292 |
esc_html( __( '(opens in a new window)', 'bogo' ) ) |
| 293 |
); |
| 294 |
} |
| 295 |
|
| 296 |
$li_content .= sprintf( |
| 297 |
' [%s]', |
| 298 |
$available_languages[$locale] ?? $locale |
| 299 |
); |
| 300 |
|
| 301 |
echo wp_kses_post( sprintf( '<li>%s</li>', $li_content ) ); |
| 302 |
} |
| 303 |
|
| 304 |
?> |
| 305 |
</ul> |
| 306 |
</div> |
| 307 |
|
| 308 |
<?php |
| 309 |
|
| 310 |
$available_languages = array_diff_key( $available_languages, $translations ); |
| 311 |
|
| 312 |
if ( ! $initial and $available_languages ) { |
| 313 |
|
| 314 |
?> |
| 315 |
|
| 316 |
<div class="descriptions" id="bogo-add-translation-actions"> |
| 317 |
<p> |
| 318 |
<strong><?php echo wp_kses_data( __( 'Add Translation:', 'bogo' ) ); ?></strong> |
| 319 |
</p> |
| 320 |
|
| 321 |
<?php |
| 322 |
|
| 323 |
foreach ( $available_languages as $locale => $lang ) { |
| 324 |
echo wp_kses_post( sprintf( |
| 325 |
'<p><button %1$s>%2$s</button> <span class="spinner"></span></p>', |
| 326 |
bogo_format_atts( array( |
| 327 |
'type' => 'button', |
| 328 |
'class' => 'button', |
| 329 |
'data-locale' => $locale, |
| 330 |
) ), |
| 331 |
sprintf( |
| 332 |
/* translators: %s: Language name. */ |
| 333 |
__( 'Add %s translation', 'bogo' ), |
| 334 |
$lang |
| 335 |
) |
| 336 |
) ); |
| 337 |
} |
| 338 |
|
| 339 |
?> |
| 340 |
<div class="clear"></div> |
| 341 |
</div> |
| 342 |
<?php |
| 343 |
|
| 344 |
} |
| 345 |
} |
| 346 |
|