//. * Translations and originals that already have translations only show their language here * (they are managed in Softtrix Multilanguage > Pages & Posts). * * @package Soft_ML */ defined( 'ABSPATH' ) || exit; class Soft_ML_Post_Language { public static function init() { add_action( 'add_meta_boxes', array( __CLASS__, 'add_box' ) ); add_action( 'save_post', array( __CLASS__, 'save' ), 20, 2 ); } public static function add_box( $post_type ) { if ( in_array( $post_type, Soft_ML_Page_Creator::supported_types(), true ) ) { add_meta_box( 'softml_language', __( 'Language', 'softtrix-multilanguage' ), array( __CLASS__, 'render' ), $post_type, 'side', 'high' ); } } /** * Is the post linked on its own (no other language in its group)? * * @param int $post_id Post. * @return bool True also when the post is not linked at all. */ public static function is_standalone( $post_id ) { $row = Soft_ML_Translations::get_row( $post_id ); return ! $row || 1 === count( Soft_ML_Translations::get_group( $row['group_id'] ) ); } public static function render( $post ) { $default = Soft_ML_Languages::default_code(); $row = Soft_ML_Translations::get_row( $post->ID ); $lang = $row ? $row['language_code'] : $default; if ( ! self::is_standalone( $post->ID ) ) { $l = Soft_ML_Languages::get( $lang ); echo '

' . esc_html( $l ? $l['native_name'] : $lang ) . '

'; if ( $lang !== $default ) { $src = Soft_ML_Translations::get_translation_id( $post->ID, $default ); echo '

' . esc_html__( 'This is a translation.', 'softtrix-multilanguage' ); if ( $src ) { echo ' ' . esc_html__( 'Open the original', 'softtrix-multilanguage' ) . ''; } echo '

'; } else { echo '

' . esc_html__( 'Original with translations. Manage them under Softtrix Multilanguage > Pages & Posts.', 'softtrix-multilanguage' ) . '

'; } return; } wp_nonce_field( 'softml_post_language', 'softml_post_language_nonce' ); echo '

'; echo '

' . esc_html__( 'The language this is written in. Posts appear only on that language\'s version of the site (for example only under /fr/ for French).', 'softtrix-multilanguage' ) . '

'; } /** * @param int $post_id Post. * @param WP_Post $post Post. */ public static function save( $post_id, $post ) { if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || ! in_array( $post->post_type, Soft_ML_Page_Creator::supported_types(), true ) ) { return; } $row = Soft_ML_Translations::get_row( $post_id ); if ( isset( $_POST['softml_post_lang'], $_POST['softml_post_language_nonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['softml_post_language_nonce'] ) ), 'softml_post_language' ) && current_user_can( 'edit_post', $post_id ) && self::is_standalone( $post_id ) ) { $lang = sanitize_key( wp_unslash( $_POST['softml_post_lang'] ) ); $default = Soft_ML_Languages::default_code(); $current = $row ? $row['language_code'] : $default; if ( Soft_ML_Languages::is_enabled( $lang ) && $lang !== $current ) { if ( $row ) { Soft_ML_Translations::unlink( $post_id ); } if ( $lang !== $default ) { Soft_ML_Translations::link( $post_id, $lang, 0, 'post', 'publish' === $post->post_status ? 'published' : 'draft' ); } $row = Soft_ML_Translations::get_row( $post_id ); } } // A post written in another language follows its own WordPress status and slug. if ( $row && $row['language_code'] !== Soft_ML_Languages::default_code() && self::is_standalone( $post_id ) ) { $status = 'publish' === $post->post_status ? 'published' : 'draft'; if ( $row['status'] !== $status ) { Soft_ML_Translations::set_status( $post_id, $status ); } $slug = '' !== $post->post_name ? $post->post_name : sanitize_title( $post->post_title ); if ( '' !== $slug && get_post_meta( $post_id, '_softml_slug', true ) !== $slug ) { update_post_meta( $post_id, '_softml_slug', $slug ); Soft_ML_Links::bump(); } } } }