class-wpml-upgrade-display-mode-for-posts.php
4.15 KB
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
<?php
class WPML_Upgrade_Display_Mode_For_Posts implements IWPML_Upgrade_Command {
const DISPLAY_MODE_SETTING = 'show_untranslated_blog_posts';
/** @var SitePress */
private $sitepress;
/** @var WPML_Settings_Helper */
private $settings;
/** @var WPML_Notices */
private $wpml_notices;
public function __construct( array $args ) {
$this->sitepress = $args[0];
$this->settings = $args[1];
$this->wpml_notices = $args[2];
}
/**
* @return bool
*/
public function run_admin() {
if ( $this->sitepress->get_setting( self::DISPLAY_MODE_SETTING ) ) {
add_action( 'init', [ $this, 'add_notice' ] );
return false;
} else {
return true;
}
}
public function add_notice() {
$notice = $this->wpml_notices->create_notice( __CLASS__, $this->get_notice_content() );
$notice->add_display_callback( array( 'WPML_Notice_Show_On_Dashboard_And_WPML_Pages', 'is_on_page' ) );
$notice->set_css_class_types( 'info' );
$this->wpml_notices->add_notice( $notice );
}
/**
* @return bool
*/
public function run_ajax() {
if ( isset( $_POST['mode'] ) ) {
if ( 'translate' === $_POST['mode'] ) {
$this->settings->set_post_type_translatable( 'post' );
$this->sitepress->set_setting( self::DISPLAY_MODE_SETTING, false, true );
$this->wpml_notices->remove_notice( 'default', __CLASS__ );
return true;
}
if ( 'display-as-translated' === $_POST['mode'] ) {
$this->settings->set_post_type_display_as_translated( 'post' );
$this->sitepress->set_setting( self::DISPLAY_MODE_SETTING, false, true );
$this->wpml_notices->remove_notice( 'default', __CLASS__ );
return true;
}
}
return false;
}
/**
* @return bool
*/
public function run_frontend() {
return false;
}
/**
* @return array
*/
public function get_results() {
return array();
}
private function get_notice_content() {
ob_start();
$action = str_replace( '_', '-', strtolower( __CLASS__ ) );
$setup_url = WPML_Admin_URL::multilingual_setup( 7 );
?>
<div class="js-main-content">
<h2><?php esc_html_e( 'Display mode for blog posts has changed', 'sitepress' ); ?></h2>
<p><?php esc_html_e( 'Until now, your site was set to display "all blog posts", even if they are not translated. That feature is now replaced with a better and more complete translation mode.', 'sitepress' ); ?></p>
<p><?php esc_html_e( "Which blog posts do you want to display on your site's translations?", 'sitepress' ); ?></p>
<p><label><input type="radio" name="mode"
value="display-as-translated"/> <?php esc_html_e( "Blog posts from the site's default language, or translations when they exist", 'sitepress' ); ?>
</label></p>
<p><label><input type="radio" name="mode"
value="translate"/> <?php esc_html_e( "Only translated blog posts (never display posts from the default language on translation languages)", 'sitepress' ); ?>
</label></p>
<input type="button" class="button-primary" name="save" value="<?php esc_attr_e( 'Save' ); ?>"
disabled="disabled"/>
<?php wp_nonce_field( $action . '-nonce', $action . '-nonce' ); ?>
</div>
<div class="js-thankyou-content" style="display: none">
<p><?php echo sprintf( esc_html__( 'Thank you for choosing. You can always change your selection in %sPost Types Translation setup%s.', 'sitepress' ), '<a href="' . $setup_url . '">', '</a>' ); ?></p>
</div>
<script>
jQuery( document ).ready( function ( $ ) {
$( '.js-main-content' ).find( 'input[name=mode]' ).on( 'change', function ( e ) {
$( '.js-main-content' ).find( 'input[name=save]' ).prop( 'disabled', false );
} );
$( '.js-main-content' ).find( 'input[name=save]' ).on( 'click', function ( e ) {
$( this ).prop( 'disabled', true );
$.ajax( {
url: ajaxurl,
type: "POST",
data: {
action: '<?php echo $action; ?>',
nonce: $( '#<?php echo $action; ?>-nonce' ).val(),
mode: $( 'input[name=mode]:checked', '.js-main-content' ).val()
},
success: function ( response ) {
$( '.js-main-content' ).hide();
$( '.js-thankyou-content' ).show();
}
} );
} );
} );
</script>
<?php
return ob_get_clean();
}
}