troubleshooting.php 34.8 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 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
<?php

use WPML\API\Sanitize;

require_once WPML_PLUGIN_PATH . '/inc/functions-troubleshooting.php';

global $wpdb;

/* DEBUG ACTION */
/**
 * @param \stdClass $term_object
 *
 * @return int
 */
function get_term_taxonomy_id_from_term_object( $term_object ) {
	return $term_object->term_taxonomy_id;
}

function get_ATE_account_data() {
	return get_option( WPML_TM_ATE_Authentication::AMS_DATA_KEY, [] );
}
$action = Sanitize::stringProp( 'debug_action', $_GET );
$nonce  = Sanitize::stringProp( 'nonce', $_GET );
if ( ! $action ) {
	$action = Sanitize::stringProp( 'debug_action', $_POST );
	$nonce  = Sanitize::stringProp( 'nonce', $_POST );
}

$otgs_twig_cache_disable_key = '_otgs_twig_cache_disabled';
if ( defined( 'WPML_Templates_Factory::OTGS_TWIG_CACHE_DISABLED_KEY' ) ) {
	$otgs_twig_cache_disable_key = WPML_Templates_Factory::OTGS_TWIG_CACHE_DISABLED_KEY;
}

if ( $nonce && $action && wp_verify_nonce( $nonce, $action ) ) {
	ob_end_clean();
	global $wpdb;
	switch ( $action ) {
		case 'otgs_twig_cache_enable':
			$new_value = false;

			if ( array_key_exists( 'new_value', $_POST ) ) {
				$new_value = (bool) $_POST['new_value'];
			}
			update_option( $otgs_twig_cache_disable_key, $new_value, 'no' );
			exit;
		case 'fix_languages':
			SitePress_Setup::fill_languages();
			SitePress_Setup::fill_languages_translations();
			SitePress_Setup::fill_flags();
			icl_cache_clear();
			exit;
		case 'icl_fix_collation':
			repair_el_type_collate();
			exit;

		case 'cache_clear':
			icl_cache_clear();
			$cache_directory = new WPML_Cache_Directory( new WPML_WP_API() );
			$cache_directory->remove();
			exit;

		case 'ghost_clean':
			// clean the icl_translations table
			$orphans = $wpdb->get_col(
				"
                SELECT t.translation_id, t.element_type
                FROM {$wpdb->prefix}icl_translations t
                LEFT JOIN {$wpdb->posts} p ON t.element_id = p.ID
                WHERE t.element_id IS NOT NULL AND t.element_type LIKE 'post\\_%' AND p.ID IS NULL
            "
			);
			if ( ! empty( $orphans ) ) {

				$upgrade_args_set = array();
				foreach ( $orphans as $orphan ) {
					$upgrade_args = array(
						'translation_id' => $orphan,
						'context'        => 'post',
					);

					$upgrade_args_set[] = $upgrade_args;

					do_action( 'wpml_translation_update', array_merge( $upgrade_args, array( 'type' => 'before_delete' ) ) );
				}

				$wpdb->query(
					"DELETE FROM {$wpdb->prefix}icl_translations
                               WHERE translation_id IN (" . wpml_prepare_in( $orphans, '%d' ) . ')'
				);

				foreach ( $upgrade_args_set as $upgrade_args ) {
					do_action( 'wpml_translation_update', array_merge( $upgrade_args, array( 'type' => 'after_delete' ) ) );
				}
			}

			$orphans = $wpdb->get_col(
				"
                SELECT t.translation_id
                FROM {$wpdb->prefix}icl_translations t
                LEFT JOIN {$wpdb->comments} c ON t.element_id = c.comment_ID
                WHERE t.element_type = 'comment' AND c.comment_ID IS NULL "
			);
			if ( false === $orphans ) {
				echo $wpdb->last_result;
			}
			if ( ! empty( $orphans ) ) {

				$upgrade_args_set = array();
				foreach ( $orphans as $orphan ) {
					$upgrade_args = array(
						'translation_id' => $orphan,
						'context'        => 'comment',
					);

					$upgrade_args_set[] = $upgrade_args;

					do_action( 'wpml_translation_update', array_merge( $upgrade_args, array( 'type' => 'before_delete' ) ) );
				}

				$wpdb->query(
					"DELETE FROM {$wpdb->prefix}icl_translations
                               WHERE translation_id IN (" . wpml_prepare_in( $orphans, '%d' ) . ')'
				);

				foreach ( $upgrade_args_set as $upgrade_args ) {
					do_action( 'wpml_translation_update', array_merge( $upgrade_args, array( 'type' => 'after_delete' ) ) );
				}
			}

			$orphans = $wpdb->get_col(
				"
                SELECT t.translation_id
                FROM {$wpdb->prefix}icl_translations t
                LEFT JOIN {$wpdb->term_taxonomy} p ON t.element_id = p.term_taxonomy_id
                WHERE t.element_id IS NOT NULL AND t.element_type LIKE 'tax\\_%' AND p.term_taxonomy_id IS NULL"
			);
			if ( ! empty( $orphans ) ) {

				$upgrade_args_set = array();
				foreach ( $orphans as $orphan ) {
					$upgrade_args = array(
						'translation_id' => $orphan,
						'context'        => 'tax',
					);

					$upgrade_args_set[] = $upgrade_args;
					do_action( 'wpml_translation_update', $upgrade_args );
				}

				$wpdb->query(
					"DELETE FROM {$wpdb->prefix}icl_translations
                               WHERE translation_id IN (" . wpml_prepare_in( $orphans, '%d' ) . ')'
				);

				foreach ( $upgrade_args_set as $upgrade_args ) {
					do_action( 'wpml_translation_update', array_merge( $upgrade_args, array( 'type' => 'after_delete' ) ) );
				}
			}

			global $wp_taxonomies;
			if ( is_array( $wp_taxonomies ) ) {
				foreach ( $wp_taxonomies as $t => $v ) {
					$orphans = $wpdb->get_col(
						"
                SELECT t.translation_id
                FROM {$wpdb->prefix}icl_translations t
                LEFT JOIN {$wpdb->term_taxonomy} p
                ON t.element_id = p.term_taxonomy_id
                WHERE t.element_type = 'tax_{$t}'
                AND p.taxonomy <> '{$t}'
                    "
					);
					if ( ! empty( $orphans ) ) {

						$upgrade_args_set = array();
						foreach ( $orphans as $orphan ) {

							$upgrade_args = array(
								'translation_id' => $orphan,
								'context'        => 'tax',
							);

							$upgrade_args_set[] = $upgrade_args;
							do_action( 'wpml_translation_update', $upgrade_args );
						}

						$wpdb->query(
							"DELETE FROM {$wpdb->prefix}icl_translations
                                       WHERE translation_id IN (" . wpml_prepare_in( $orphans, '%d' ) . ')'
						);

						foreach ( $upgrade_args_set as $upgrade_args ) {
							do_action( 'wpml_translation_update', array_merge( $upgrade_args, array( 'type' => 'after_delete' ) ) );
						}
					}
				}
			}

			// remove ghost translations
			// get unlinked rids
			$rids = $wpdb->get_col( "SELECT rid FROM {$wpdb->prefix}icl_translation_status WHERE translation_id NOT IN (SELECT translation_id FROM {$wpdb->prefix}icl_translations)" );
			if ( $rids ) {
				$jids = $wpdb->get_col( "SELECT job_id FROM {$wpdb->prefix}icl_translate_job WHERE rid IN (" . wpml_prepare_in( $rids, '%d' ) . ')' );
				if ( $jids ) {
					$wpdb->query( "DELETE FROM {$wpdb->prefix}icl_translate WHERE job_id IN (" . wpml_prepare_in( $jids, '%d' ) . ')' );
					$wpdb->query( "DELETE FROM {$wpdb->prefix}icl_translate_job WHERE job_id IN (" . wpml_prepare_in( $jids, '%d' ) . ')' );
					$wpdb->query( "DELETE FROM {$wpdb->prefix}icl_translation_status WHERE rid IN (" . wpml_prepare_in( $rids, '%d' ) . ')' );
				}
			}

			// remove any duplicates in icl_translations
			$trs = $wpdb->get_results(
				"SELECT element_id, GROUP_CONCAT(translation_id) AS tids FROM {$wpdb->prefix}icl_translations
                WHERE element_id > 0 AND element_type LIKE 'post\\_%' GROUP BY element_id"
			);
			foreach ( $trs as $r ) {
				$exp = explode( ',', $r->tids );
				if ( count( $exp ) > 1 ) {
					$maxtid = max( $exp );
					foreach ( $exp as $e ) {
						if ( $e != $maxtid ) {
							$upgrade_args = array(
								'translation_id' => $e,
								'context'        => 'post',
							);

							do_action( 'wpml_translation_update', array_merge( $upgrade_args, array( 'type' => 'before_delete' ) ) );

							$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $e ) );

							do_action( 'wpml_translation_update', array_merge( $upgrade_args, array( 'type' => 'after_delete' ) ) );
						}
					}
				}
			}


			exit;
			break;
		case 'assign_translation_status_to_duplicates':
			$updated_items = WPML\Troubleshooting\AssignTranslationStatusToDuplicates::run();
			echo json_encode( array( 'updated' => $updated_items ) );
			exit;
		case 'icl_ts_add_missing_language':
			global $iclTranslationManagement;
			$iclTranslationManagement->add_missing_language_information();
			exit;
		case 'link_post_type':
			$old_el_type = 'post_' . sanitize_key( filter_input( INPUT_GET, 'old_value' ) );
			$new_el_type = 'post_' . sanitize_key( filter_input( INPUT_GET, 'new_value' ) );

			$wpdb->update(
				$wpdb->prefix . 'icl_translations',
				array( 'element_type' => $new_el_type ),
				array( 'element_type' => $old_el_type )
			);

			do_action(
				'wpml_translation_update',
				array(
					'type'         => 'element_type_update',
					'element_type' => $new_el_type,
					'context'      => 'post',
				)
			);

			exit;
		case 'link_taxonomy':
			$new_el_type = 'tax_' . $_GET['new_value'];
			$old_el_type = 'tax_' . $_GET['old_value'];

			$wpdb->update(
				$wpdb->prefix . 'icl_translations',
				array( 'element_type' => $new_el_type ),
				array( 'element_type' => $old_el_type )
			);

			do_action(
				'wpml_translation_update',
				array(
					'type'         => 'element_type_update',
					'element_type' => $new_el_type,
					'content'      => 'tax',
				)
			);

			exit;
		case 'icl_fix_terms_count':
			global $sitepress;

			$has_get_terms_args_filter = remove_filter( 'get_terms_args', array( $sitepress, 'get_terms_args_filter' ) );
			$has_get_term_filter       = remove_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ), 1 );
			$has_terms_clauses_filter  = remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );

			foreach ( get_taxonomies( array(), 'names' ) as $taxonomy ) {

				$terms_objects = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => 0 ] );
				if ( is_array( $terms_objects ) ) {
					/** @phpstan-ignore-next-line For some reason 'get_term_taxonomy_id_from_term_object' is not recognised as function by PHPStan. */
					$term_taxonomy_ids = array_map( 'get_term_taxonomy_id_from_term_object', $terms_objects );
					wp_update_term_count( $term_taxonomy_ids, $taxonomy, true );
				}
			}

			if ( $has_terms_clauses_filter ) {
				add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10, 3 );
			}
			if ( $has_get_term_filter ) {
				add_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ), 1, 1 );
			}
			if ( $has_get_terms_args_filter ) {
				add_filter( 'get_terms_args', array( $sitepress, 'get_terms_args_filter' ), 10, 2 );
			}

			exit;
		case 'icl_remove_st_db_cache_logs':
			delete_option( 'wpml-st-persist-errors' );
			exit;
	}
}
/* DEBUG ACTION */
global $sitepress;

if ( wp_verify_nonce(
	(string) filter_input( INPUT_POST, 'icl_reset_allnonce' ),
	'icl_reset_all'
) ) {
	if ( $_POST['icl-reset-all'] == 'on' ) {
		icl_reset_wpml();
		echo '<script type="text/javascript">location.href=\'' . admin_url(
			'plugins.php?deactivate=true'
		) . '\'</script>';
		exit();
	}
}


?>
<div class="wrap">
<h2><?php echo __( 'Troubleshooting', 'sitepress' ); ?></h2>
<?php if ( isset( $_GET['message'] ) ) { ?>
	<div class="updated message fade"><p>
			<?php echo esc_html( $_GET['message'] ); ?>
		</p></div>
<?php } ?>
<?php

// phpcs:disable
echo \WPML\ICLToATEMigration\Loader::renderContainerIfNeeded();
// phpcs:enable

echo '<a href="#wpml-settings">' . __( 'WPML Settings', 'sitepress' ) . '</a>';
echo '<br /><hr /><h3 id="wpml-settings"> ' . __( 'WPML settings', 'sitepress' ) . '</h3>';
echo '<textarea style="font-size:10px;width:100%" wrap="off" rows="16" readonly="readonly">';
ob_start();
print_r( $sitepress->get_settings() );
$ob = ob_get_contents();
ob_end_clean();
echo esc_html( (string) $ob );
echo '</textarea>';

?>

<script type="text/javascript">
	jQuery(document).ready(function () {
		jQuery('#icl_troubleshooting_more_options').submit(iclSaveForm);
	})
</script>
<br clear="all"/><br/>

<br/>
<script type="text/javascript">
	function wpml_ts_link_post_type(select, old_value) {
		if (!select.val()) return;
		select.attr('disabled', 'disabled');
		select.after(icl_ajxloaderimg);
		jQuery.post(WPML_core.sanitize(location.href) + '&debug_action=link_post_type&nonce=<?php echo wp_create_nonce( 'link_post_type' ); ?>&new_value=' + select.val() + '&old_value=' + old_value, function () {
			alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
			select.next().fadeOut();
			location.reload();
		});
	}

	function wpml_ts_link_taxonomy(select, old_value) {
		if (!select.val()) return;
		select.attr('disabled', 'disabled');
		select.after(icl_ajxloaderimg);
		jQuery.post(WPML_core.sanitize(location.href) + '&debug_action=link_taxonomy&nonce=<?php echo wp_create_nonce( 'link_taxonomy' ); ?>&new_value=' + select.val() + '&old_value=' + old_value, function () {
			alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
			select.next().fadeOut();
			location.reload();
		});
	}

	function parse_xhr_error(xhr, status, error) {
		return xhr.statusText || status || error;
	}

	jQuery(document).ready(function ($) {
		$('#otgs_twig_cache_enable').find('.button-secondary').on('click', function () {
			jQuery(this).attr('disabled', 'disabled');
			jQuery(this).after(icl_ajxloaderimg);

			var value = jQuery(this).parent().find('.otgs_twig_cache_new_value').val();
			var key = '<?php echo $otgs_twig_cache_disable_key; ?>';

			jQuery.ajax({
										type:    "POST",
										url:     WPML_core.sanitize( location.href ),
										data:    {
											'debug_action': 'otgs_twig_cache_enable',
											'nonce':        '<?php echo wp_create_nonce( 'otgs_twig_cache_enable' ); ?>',
											'new_value':    value
										},
										success: function () {
											alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
											location.reload();
										}
									});

		});

		jQuery('#icl_fix_languages').click(function () {
			jQuery(this).prop('disabled', true);
			jQuery(this).after(icl_ajxloaderimg);


			var icl_fix_languages = jQuery('#icl_fix_languages');

			jQuery.ajax({
				type: 'POST',
				contentType: "application/json; charset=utf-8",
				url: WPML_core.sanitize(location.href) + '&debug_action=fix_languages&nonce=<?php echo wp_create_nonce( 'fix_languages' ); ?>',
				timeout: 60000,
				success: function () {
					icl_fix_languages.prop('disabled', false);
					alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
					icl_fix_languages.next().fadeOut();
					location.reload();
				},
				error: function (jqXHR, status, error) {
					var parsed_response = parse_xhr_error(jqXHR, status, error);

					<?php
					$timeout_message  = 'The operation timed out, but languages may still get fixed in the background.\n';
					$timeout_message .= 'Please wait 5-10 minutes, then refresh or come back to this page.\n';
					$timeout_message .= 'If languages are still not fixed, please retry or contact the WPML support.'
					?>

					if(parsed_response=='timeout') {
						alert('<?php echo __( $timeout_message, 'sitepress' ); ?>');
					} else {
						alert(parsed_response);
					}
					icl_fix_languages.next().fadeOut();
				}
			});
		});

		jQuery('#icl_remove_ghost').click(function () {
			var self = jQuery(this);
			self.prop('disabled', true);
			self.after(icl_ajxloaderimg);
			jQuery.post(WPML_core.sanitize(location.href) + '&debug_action=ghost_clean&nonce=<?php echo wp_create_nonce( 'ghost_clean' ); ?>', function () {
				self.prop('disabled', false);
				alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
				self.next().fadeOut();

			});
		});
		// #assign_translation_status_to_duplicates_resp: BEGIN
		var assign_translation_status_to_duplicates_loader = jQuery(icl_ajxloaderimg);
		var assign_translation_status_to_duplicates_cycles = 0;
		var assign_translation_status_to_duplicates_updated = 0;
		var response_element = jQuery('#assign_translation_status_to_duplicates_resp');
		var assign_translation_status_to_duplicates_element = jQuery('#assign_translation_status_to_duplicates');
		assign_translation_status_to_duplicates_element.click(function () {
			assign_translation_status_to_duplicates();
		});

		function assign_translation_status_to_duplicates() {

			if (assign_translation_status_to_duplicates_cycles == 0) {
				assign_translation_status_to_duplicates_element.prop('disabled', true);
				response_element.text('');
				response_element.show();
				assign_translation_status_to_duplicates_element.after(assign_translation_status_to_duplicates_loader);

			}
			assign_translation_status_to_duplicates_cycles++;

			jQuery.ajax({
				type: 'POST',
				contentType: "application/json; charset=utf-8",
				url: WPML_core.sanitize(location.href) + '&debug_action=assign_translation_status_to_duplicates&nonce=<?php echo wp_create_nonce( 'assign_translation_status_to_duplicates' ); ?>',
				dataType: 'json',
				success: function (msg) {
					assign_translation_status_to_duplicates_updated += msg.updated;
					var response_message;
					if (msg.updated > 0) {
						response_message = assign_translation_status_to_duplicates_updated + ' <?php echo esc_js( _x( 'translation jobs updated', 'Sets the translation status to DUPLICATE in the icl_translation_status table, for posts that are marked as duplicated', 'sitepress' ) ); ?>';

						if (assign_translation_status_to_duplicates_cycles >= 50) {
							response_message += '. <?php echo esc_js( _x( 'Partially done.', 'Sets the translation status to DUPLICATE in the icl_translation_status table, for posts that are marked as duplicated', 'sitepress' ) ); ?>';
							response_message += '. <?php echo esc_js( _x( 'There might be more content to fix: please repeat the process.', 'Sets the translation status to DUPLICATE in the icl_translation_status table, for posts that are marked as duplicated', 'sitepress' ) ); ?>';
							response_element.text(response_message);
							alert('<?php echo esc_js( _x( 'Partially done', 'Sets the translation status to DUPLICATE in the icl_translation_status table, for posts that are marked as duplicated', 'sitepress' ) ); ?>');
							response_element.fadeOut();
							assign_translation_status_to_duplicates_loader.fadeOut(function() {
								assign_translation_status_to_duplicates_element.remove(assign_translation_status_to_duplicates_loader);
							});
							assign_translation_status_to_duplicates_element.prop('disabled', false);

							//Reset counters
							assign_translation_status_to_duplicates_cycles = 0;
							assign_translation_status_to_duplicates_updated = 0;
						} else {
							response_message += ' ...';
							response_element.text(response_message);
							assign_translation_status_to_duplicates();
						}
					} else {
						response_message = '';
						if (assign_translation_status_to_duplicates_updated != 0) {
							response_message += assign_translation_status_to_duplicates_updated + '.';
						}
						response_message += '<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>';
						response_element.text(response_message);

						alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');

						response_element.fadeOut();
						assign_translation_status_to_duplicates_loader.fadeOut(function() {
							assign_translation_status_to_duplicates_element.remove(assign_translation_status_to_duplicates_loader);
						});
						assign_translation_status_to_duplicates_element.prop('disabled', false);
					}
				},
				error: function (xhr, status, error) {
					var parsed_response = parse_xhr_error(xhr, status, error);
					response_element.text('');
					response_element.html(parsed_response);
					assign_translation_status_to_duplicates_loader.fadeOut(function() {
						assign_translation_status_to_duplicates_element.remove(assign_translation_status_to_duplicates_loader);
					});
					assign_translation_status_to_duplicates_element.attr('disabled', 'disabled');
				}
			});
		}

		jQuery('#icl_add_missing_lang').click(function () {
			var self = jQuery(this);
			self.prop('disabled', true);
			self.after(icl_ajxloaderimg);
			jQuery.post(WPML_core.sanitize(location.href) + '&debug_action=icl_ts_add_missing_language&nonce=<?php echo wp_create_nonce( 'icl_ts_add_missing_language' ); ?>', function () {
				self.prop('disabled', false);
				alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
				self.next().fadeOut();

			});
		});

		jQuery('#icl_fix_collation').click(function () {
			jQuery(this).prop('disabled', true);
			jQuery(this).after(icl_ajxloaderimg);
			jQuery.post(WPML_core.sanitize(location.href) + '&debug_action=icl_fix_collation&nonce=<?php echo wp_create_nonce( 'icl_fix_collation' ); ?>', function () {
				jQuery('#icl_fix_collation').prop('disabled', false);
				alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
				jQuery('#icl_fix_collation').next().fadeOut();

			});
		});

		jQuery('#icl_fix_terms_count').click(function () {
			var self = jQuery(this);
			self.prop('disabled', true);
			self.after(icl_ajxloaderimg);
			jQuery.post(WPML_core.sanitize(location.href) + '&debug_action=icl_fix_terms_count&nonce=<?php echo wp_create_nonce( 'icl_fix_terms_count' ); ?>', function () {
				self.prop('disabled', false);
				alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
				self.next().fadeOut();

			});
		});

		jQuery('#icl_remove_st_db_cache_logs').click(function () {
			var self = jQuery(this);
			self.prop('disabled', true);
			self.after(icl_ajxloaderimg);
			jQuery.post(WPML_core.sanitize(location.href) + '&debug_action=icl_remove_st_db_cache_logs&nonce=<?php echo wp_create_nonce( 'icl_remove_st_db_cache_logs' ); ?>', function () {
				self.prop('disabled', false);
				alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
				self.next().fadeOut();

			});
		});

		<?php
		$icl_ajax_url = wpml_get_admin_url(
			[
				'path'  => 'admin.php',
				'query' => [ 'page' => WPML_PLUGIN_FOLDER . '/menu/languages.php' ],
			]
		);
		?>

		jQuery('#icl_reset_languages').click(function () {
			var icl_ajx_url = '<?php echo esc_url( $icl_ajax_url ); ?>';
			var self = jQuery(this);
			self.prop('disabled', true);
			self.after(icl_ajxloaderimg);

			jQuery.post(icl_ajx_url + "&icl_ajx_action=reset_languages&_icl_nonce=" + jQuery('#_icl_nonce_rl').val(), function () {
				self.prop('disabled', false);
				alert('<?php echo esc_js( __( 'Done', 'sitepress' ) ); ?>');
				self.next().fadeOut();

			});
		});
	})
</script>
<div class="icl_cyan_box">
	<h3><?php _e( 'Advanced settings', 'sitepress' ); ?></h3>

	<p id="otgs_twig_cache_enable">
	  <?php
		$twig_cache_disabled  = (bool) get_option( $otgs_twig_cache_disable_key, false );
		$twig_cache_new_value = ! $twig_cache_disabled;
		$twig_cache_label     = __( 'Disable cache for language switcher templates', 'sitepress' );
		if ( $twig_cache_disabled ) {
			$twig_cache_label = __( 'Enable cache for language switcher templates', 'sitepress' );
		}
		?>
		<input type="hidden" id="otgs_twig_cache_new_value" class="otgs_twig_cache_new_value" value="<?php echo $twig_cache_new_value; ?>"/>
		<input type="button" class="button-secondary" value="<?php echo $twig_cache_label; ?>"/><br/>
	</p>

</div><br clear="all"/>
<?php
	$registration_data = get_ATE_account_data();
	$shared            = array_key_exists( 'shared', $registration_data ) ? $registration_data['shared'] : null;
	$uuid              = get_option( WPML_Site_ID::SITE_ID_KEY . ':' . WPML_TM_ATE::SITE_ID_SCOPE, null );

if ( $shared ) {
	?>
		<div class="icl_cyan_box">
			<h3><?php _e( 'Your Automatic Translation account id is', 'sitepress' ); ?></h3>
			<p>
			<?php echo $uuid . '#' . $registration_data['shared']; ?>
			</p>
		</div><br clear="all"/>
	<?php
}
?>

<div class="icl_cyan_box">
	<h3><?php _e( 'Clean up', 'sitepress' ); ?></h3>

	<p class="icl_form_errors" style="padding:6px;"><?php _e( 'Please make backup of your database before using this.', 'sitepress' ); ?></p>

	<?php if ( ! SitePress_Setup::languages_complete() ) { ?>
		<p>
			<br />
			<label><input type="checkbox" onchange="if(jQuery(this).prop('checked')){jQuery('#icl_fix_languages').prop('disabled', false);}else{jQuery('#icl_fix_languages').prop('disabled', true);}">
				&nbsp;<?php _e( "This operation will reset WPML's language tables and reinstall it. Any custom languages that you added will be removed.", 'sitepress' ); ?></label><br /><br />
			<input disabled="disabled" id="icl_fix_languages" type="button" class="button-secondary" value="<?php _e( 'Clear language information and repopulate languages', 'sitepress' ); ?>"/><br/><br />
			<small style="margin-left:10px;"><?php _e( "This operation will remove WPML's language table and recreate it. You should use it if you just installed WPML and you're not seeing a complete list of available languages.", 'sitepress' ); ?></small>
			<br /><br />
		</p>
	<?php } ?>

	<?php if ( SitePress_Setup::setup_complete() ) { ?>
		<?php do_action( 'wpml_troubleshooting_after_setup_complete_cleanup_begin' ); ?>
		<?php do_action( 'before_setup_complete_troubleshooting_functions' ); ?>
	<p>
		<select name="wpml_post_type">
			<?php foreach ( $sitepress->get_translatable_documents() as $key => $translatable_document ) : ?>
			<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $translatable_document->label ); ?></option>
			<?php endforeach; ?>
		</select>
		<input id="wpml_sync_posts_taxonomies" type="button" class="button-secondary" value="<?php esc_html_e( 'Synchronize posts taxonomies', 'sitepress' ); ?>"/> <span class="wpml-notice"></span><br/>
		<small style="margin-left:10px;"><?php esc_html_e( 'This will assign the same taxonomy terms as the source for each post translation.', 'sitepress' ); ?></small>
	</p>
	<p>
		<input id="icl_cache_clear" type="button" class="button-secondary" value="<?php _e( 'Clear the cache in WPML', 'sitepress' ); ?>"/><br/>
		<small style="margin-left:10px;"><?php _e( 'This may solve issues like missing languages in the language switcher.', 'sitepress' ); ?></small>
	</p>
	<p>
		<input id="icl_remove_ghost" type="button" class="button-secondary" value="<?php _e( 'Remove ghost entries from the translation tables', 'sitepress' ); ?>"/><br/>
		<small style="margin-left:10px;"><?php _e( 'Removes entries from the WPML tables that are not linked properly. Cleans the table off entries left over upgrades, bug fixes or undetermined factors.', 'sitepress' ); ?></small>
	</p>
	<p>
		<input id="icl_fix_collation" type="button" class="button-secondary" value="<?php _e( 'Fix element_type collation', 'sitepress' ); ?>"/><br/>
		<small style="margin-left:10px;"><?php _e( 'Fixes the collation of the element_type column in icl_translations in case this setting changed for your posts.post_type column.', 'sitepress' ); ?></small>
	</p>

		<?php do_action( 'wpml_troubleshooting_after_fix_element_type_collation' ); ?>

		<?php do_action( 'wpml_tm_mcs_troubleshooting' ); ?>

		<?php if ( class_exists( 'TranslationManagement' ) ) { ?>
	<p>
		<input id="assign_translation_status_to_duplicates" type="button" class="button-secondary" value="<?php _e( 'Assign translation status to duplicated content', 'sitepress' ); ?>"/><span id="assign_translation_status_to_duplicates_resp"></span><br/>
		<small style="margin-left:10px;"><?php _e( 'Sets the translation status to DUPLICATE in the icl_translation_status table, for posts that are marked as duplicated.', 'sitepress' ); ?> </small>
	</p>
	<?php } ?>
	<p>
		<input id="icl_add_missing_lang" type="button" class="button-secondary" value="<?php _e( 'Set language information', 'sitepress' ); ?>"/><br/>
		<small style="margin-left:10px;"><?php _e( 'Adds language information to posts and taxonomies that are missing this information.', 'sitepress' ); ?></small>
	</p>

	<p>
		<?php wp_nonce_field( 'reset_languages_nonce', '_icl_nonce_rl' ); ?>
		<input class="button-secondary" type="button" id="icl_reset_languages" value="<?php esc_html_e( 'Reset languages', 'sitepress' ); ?>"/>
		<small style="margin-left:10px;"><?php esc_html_e( 'WPML will reset all language information to its default values. Any languages that you added or edited will be lost.', 'sitepress' ); ?></small>
	</p>
	<p>
		<input id="icl_fix_terms_count" type="button" class="button-secondary" value="<?php _e( 'Fix terms count', 'sitepress' ); ?>"/><br/>
		<small style="margin-left:10px;"><?php _e( 'Correct terms count in case something went wrong with translated contents.', 'sitepress' ); ?></small>
	</p>
	<p>
		<input id="icl_fix_post_types" type="button" class="button-secondary" value="<?php _e( 'Fix post type assignment for translations', 'sitepress' ); ?>"/><br/>
		<small style="margin-left:10px;"><?php _e( 'Correct post type assignment for translations of custom post types in case something went wrong.', 'sitepress' ); ?></small>
	</p>

		<?php if ( get_option( 'wpml-st-persist-errors' ) ) { ?>
	<p>
		<input id="icl_remove_st_db_cache_logs" type="button" class="button-secondary" value="<?php _e( 'Remove debug logs of String Translation cache', 'sitepress' ); ?>"/><br/>
		<small style="margin-left:10px;"><?php _e( 'Removes invalid rows stored in option and hide admin notice.', 'sitepress' ); ?></small>
	</p>
	<?php } ?>

	<p>
		<br/>
		<?php _e( 'Translatable custom posts linking', 'sitepress' ); ?><br/>
		<small style="margin-left:10px;"><?php _e( 'Allows linking existing translations after changing custom posts definition (name) ', 'sitepress' ); ?></small>

		<?php
		$translatable_posts = $sitepress->get_translatable_documents();
		$res                = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT DISTINCT element_type FROM {$wpdb->prefix}icl_translations WHERE element_type LIKE %s",
				array( wpml_like_escape( 'post_' ) . '%' )
			)
		);
		echo '<table class="widefat" style="width:300px;">';

		foreach ( $res as $row ) {

			$post_type = preg_replace( '#^post_#', '', $row );
			if ( $post_type == 'nav_menu_item' ) {
				continue;
			}

			echo '<tr>';
			echo '<td>' . $post_type . '</td>';

			if ( isset( $translatable_posts[ $post_type ] ) ) {

				echo '<td>' . __( 'linked to: ', 'sitepress' ) . $translatable_posts[ $post_type ]->labels->name . '</td>';

			} else {
				echo '<td>';
				echo '<select onchange="wpml_ts_link_post_type(jQuery(this), \'' . $post_type . '\')">';
				echo '<option value="">' . __( '--select--', 'sitepress' ) . '</option>';
				foreach ( $translatable_posts as $name => $type ) {
					echo '<option value="' . $name . '">' . $type->labels->name . '(' . $name . ')' . '</option>';
				}
				echo '</select>';
				echo '</td>';
			}
			echo '</tr>';
		}

		echo '</table>';
		echo '<br />';
		echo __( 'Note: if you edited the custom post declaration you may need to re-configure WPML to mark it as translatable.', 'sitepress' );

		?>
	</p>

	<p>
		<br/>
		<?php _e( 'Translatable taxonomies linking', 'sitepress' ); ?><br/>
		<small style="margin-left:10px;"><?php _e( 'Allows linking existing translations after changing custom taxonomies definition (name) ', 'sitepress' ); ?></small>

		<?php
		global $wp_post_types, $wp_taxonomies;
		$translatable_taxs = array();
		foreach ( $wp_post_types as $name => $post_type ) {
			$translatable_taxs = array_merge( $translatable_taxs, $sitepress->get_translatable_taxonomies( true, $name ) );
		}
		$translatable_taxs = array_unique( $translatable_taxs );

		$res = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT DISTINCT element_type FROM {$wpdb->prefix}icl_translations WHERE element_type LIKE %s",
				array( wpml_like_escape( 'tax_' ) . '%' )
			)
		);
		echo '<table class="widefat" style="width:300px;">';

		foreach ( $res as $row ) {

			$tax = preg_replace( '#^tax_#', '', $row );
			if ( $tax == 'link_category' || $tax == 'nav_menu' ) {
				continue;
			}

			echo '<tr>';

			echo '<td>' . $tax . '</td>';

			if ( in_array( $tax, $translatable_taxs ) ) {

				echo '<td>' . __( 'linked to: ', 'sitepress' ) . $wp_taxonomies[ $tax ]->labels->name . '</td>';

			} else {
				echo '<td>';
				echo '<select onchange="wpml_ts_link_taxonomy(jQuery(this), \'' . $tax . '\')">';
				echo '<option value="">' . __( '--select--', 'sitepress' ) . '</option>';
				foreach ( $translatable_taxs as $name ) {

					echo '<option value="' . $name . '">' . $wp_taxonomies[ $name ]->labels->name . '(' . $name . ')' . '</option>';
				}
				echo '</select>';
				echo '</td>';
			}
			echo '</tr>';
		}

		echo '</table>';
		echo '<br />';
		echo __( 'Note: if you edited the custom taxonomy declaration you may need to re-configure WPML to mark it as translatable.', 'sitepress' );

		?>
	</p>

		<?php do_action( 'wpml_troubleshooting_after_setup_complete_cleanup_end' ); ?>
		<?php do_action( 'after_setup_complete_troubleshooting_functions' ); ?>
		<?php ICL_AdminNotifier::troubleshooting(); ?>

	<?php } ?>

</div>

<br clear="all"/>
<?php
// TODO: [WPML 3.3] we should use the new hooks to add elements to the troubleshooting page
echo WPML_Troubleshooting_Terms_Menu::display_terms_with_suffix();
?>

<br clear="all"/>

	<div class="icl_cyan_box">

		<?php
		echo '<h3 id="wpml-settings"> ' . __( 'Reset', 'sitepress' ) . '</h3>';
		?>

		<?php if ( function_exists( 'is_multisite' ) && is_multisite() ) { ?>

			<p><?php _e( 'This function is available through the Network Admin section.', 'sitepress' ); ?></p>
			<?php if ( current_user_can( 'manage_sites' ) ) { ?>
				<a href="
				<?php
				echo esc_url(
					network_admin_url( 'admin.php?page=' . WPML_PLUGIN_FOLDER . '/menu/network.php' )
				)
				?>
				"><?php _e( 'Go to WPML Network settings.', 'sitepress' ); ?></a>
				<p><small><?php _e( 'Note: WPML must be activated on the primary site or must be network activated.', 'sitepress' ); ?></small></p>
			<?php } else { ?>
				<i><?php _e( 'You are not allowed to manage the WPML Network settings.', 'sitepress' ); ?></i>
			<?php } ?>

		<?php } else { ?>


			<?php
			echo '<form method="post" onsubmit="return confirm(\'' . __(
				'Are you sure you want to reset all translation and language data? This operation cannot be reversed!',
				'sitepress'
			) . '\')">';
			wp_nonce_field( 'icl_reset_all', 'icl_reset_allnonce' );
			echo '<p class="error" style="padding:6px;">';
			_e(
				"The 'Reset' action will deactivate the WPML plugin after it deletes the WPML tables (tables with the 'icl_' prefix) from the database. The action will NOT delete any content (posts, taxonomy terms etc.). It only affects translation and language information that WPML associates with each content type.",
				'sitepress'
			);
			echo '</p>';
			echo '<p class="error" style="padding:6px;">';
			_e( "Please note that all translations you have sent to remote translation services will be lost if you reset WPML's data. They cannot be recovered later.", 'sitepress' );
			echo '</p>';
			echo '<p class="error" style="padding:6px;">';
			_e( 'If you are using the Advanced Translation Editor, you will lose the translations that are in progress, as well as the existing translation memory and glossary. You will also lose access to purchases, invoices, and history related to your work with the Advanced Translation Editor.', 'sitepress' );
			echo '</p>';
			echo '<label><input type="checkbox" name="icl-reset-all" ';
			if ( ! function_exists( 'is_super_admin' ) || is_super_admin() ) {
				echo 'onchange="if(this.checked) jQuery(\'#reset-all-but\').prop(\'disabled\', false); else  jQuery(\'#reset-all-but\').prop(\'disabled\', true);"';
			}
			echo ' /> ' . __( 'I am about to reset all translation and language data.', 'sitepress' ) . '</label><br /><br />';

			echo '<input id="reset-all-but" type="submit" disabled="disabled" class="button-primary" value="' . __(
				'Reset and deactivate WPML',
				'sitepress'
			) . '" />';
			echo '</form>';
			?>

		<?php } ?>

	</div>

<?php do_action( 'icl_menu_footer' ); ?>
</div>