self::SLUG, 'step' => (int) $step ), admin_url( 'admin.php' ) ); } /** * @return array step => label */ private function steps() { return array( 1 => __( 'Languages', 'softtrix-multilanguage' ), 2 => __( 'Translation method', 'softtrix-multilanguage' ), 3 => __( 'Connect API', 'softtrix-multilanguage' ), 4 => __( 'Language switcher', 'softtrix-multilanguage' ), 5 => __( 'Ready', 'softtrix-multilanguage' ), ); } /* ------------------------------------------------------------ save */ public function handle() { if ( ! current_user_can( Soft_ML_Admin::CAP ) ) { wp_die( esc_html__( 'You do not have permission to do that.', 'softtrix-multilanguage' ), 403 ); } check_admin_referer( 'softml_wizard' ); $step = isset( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; $settings = Soft_ML_Settings::all(); $next = $step + 1; if ( 1 === $step ) { $site = isset( $_POST['site_lang'] ) ? sanitize_key( wp_unslash( $_POST['site_lang'] ) ) : ''; $codes = isset( $_POST['codes'] ) && is_array( $_POST['codes'] ) ? array_map( 'sanitize_key', wp_unslash( $_POST['codes'] ) ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $old = Soft_ML_Languages::default_code(); if ( $site && $site !== $old && 0 === self::linked_count() ) { if ( ! Soft_ML_Languages::exists( $site ) ) { $lang = Soft_ML_Catalog::get( $site ); if ( $lang ) { $all = Soft_ML_Languages::all(); $lang['order'] = -1; $all[ $site ] = $lang; Soft_ML_Languages::replace_all( $all ); } } if ( Soft_ML_Languages::exists( $site ) ) { Soft_ML_Languages::set_default( $site ); if ( ! in_array( $old, $codes, true ) ) { Soft_ML_Languages::delete( $old ); // The previous guess was wrong and nothing uses it yet. } $settings = Soft_ML_Settings::all(); $settings['seo']['x_default'] = $site; } } // Going back to this step: unticked languages are removed again, unless they already have translations. foreach ( array_keys( Soft_ML_Languages::all() ) as $code ) { if ( $code !== Soft_ML_Languages::default_code() && ! in_array( $code, $codes, true ) && 0 === Soft_ML_Translations::count_for_language( $code ) ) { Soft_ML_Languages::delete( $code ); } } $res = Soft_ML_Languages::add_from_catalog( array_diff( $codes, array( Soft_ML_Languages::default_code() ) ) ); if ( $res['errors'] ) { set_transient( 'softml_notice_' . get_current_user_id(), array( 'error', implode( ' ', $res['errors'] ) ), 60 ); } if ( count( Soft_ML_Languages::all() ) < 2 ) { set_transient( 'softml_notice_' . get_current_user_id(), array( 'error', __( 'Choose at least one language to translate your website into.', 'softtrix-multilanguage' ) ), 60 ); $next = 1; } } elseif ( 2 === $step ) { $settings['general']['method'] = isset( $_POST['method'] ) && 'api' === $_POST['method'] ? 'api' : 'manual'; $next = 'api' === $settings['general']['method'] ? 3 : 4; } elseif ( 4 === $step ) { $floating = isset( $_POST['floating'] ) ? sanitize_key( wp_unslash( $_POST['floating'] ) ) : 'none'; $menu = isset( $_POST['menu_location'] ) ? sanitize_key( wp_unslash( $_POST['menu_location'] ) ) : ''; $settings['switcher']['floating'] = in_array( $floating, array( 'none', 'bottom-right', 'bottom-left' ), true ) ? $floating : 'none'; $settings['switcher']['menu_location'] = array_key_exists( $menu, get_registered_nav_menus() ) ? $menu : ''; update_option( 'softml_setup_done', 1, false ); } Soft_ML_Settings::save( $settings ); wp_safe_redirect( self::url( $next ) ); exit; } /** * @return int Linked translations (the site language can only change before translating). */ private static function linked_count() { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', Soft_ML_Translations::table() ) ); // phpcs:ignore WordPress.DB } /* ------------------------------------------------------------ view */ public function render() { if ( ! current_user_can( Soft_ML_Admin::CAP ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'softtrix-multilanguage' ), 403 ); } $step = isset( $_GET['step'] ) ? max( 1, min( 5, absint( $_GET['step'] ) ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $steps = $this->steps(); $api = 'api' === Soft_ML_Settings::get( 'general', 'method' ); echo '
' . esc_html( $l ? $l['name'] : $default ) . ' ' . esc_html__( '(cannot be changed after translations exist)', 'softtrix-multilanguage' ) . '
'; } else { echo ''; } echo '' . esc_html__( 'Tick one or more. Each language gets its own address, for example example.com/fr/ for French. You can add more later.', 'softtrix-multilanguage' ) . '
'; echo '' . esc_html__( 'You can switch at any time, and use both: for example translate automatically, then correct a few texts by hand.', 'softtrix-multilanguage' ) . '
';
if ( ! $menus ) {
echo '
' . esc_html__( 'Your theme has no classic menu locations; use the floating button, the shortcode or the widget.', 'softtrix-multilanguage' ) . '';
}
echo '
' . esc_html__( 'Anywhere else: paste the shortcode [softml_switcher] into a page, widget or builder block, or add the "Language Switcher (Softtrix Multilanguage)" widget.', 'softtrix-multilanguage' ) . '
' . esc_html__( 'Language addresses like /fr/ need "pretty" permalinks. Choose any option except "Plain" under Settings > Permalinks.', 'softtrix-multilanguage' ) . ' ' . esc_html__( 'Open Permalinks', 'softtrix-multilanguage' ) . '