| 1361 | * Sanitize filenames to latin |
| 1362 | * |
| 1363 | * Plugin sanitizes cyrillic (Ukrainian, Russian, Belorussian), German, French, Polish, Spanish, Hungarian, Czech, Greek, Swedish and other filenames to latin characters. |
| 1364 | * |
| 1365 | * @since 4.5.0 |
| 1366 | * |
| 1367 | * @param string $filename The filename to be sanitized |
| 1368 | * @return string The sanitized filename |
| 1369 | */ |
| 1370 | function sanitize_filename_to_latin( $filename ) { |
| 1371 | $chars_table = array( |
| 1372 | |
| 1373 | // Cyrillic alphabet |
| 1374 | '/А/' => 'a', |
| 1375 | '/Б/' => 'b', |
| 1376 | '/В/' => 'v', |
| 1377 | '/Г/' => 'g', |
| 1378 | '/Д/' => 'd', |
| 1379 | '/а/' => 'a', |
| 1380 | '/б/' => 'b', |
| 1381 | '/в/' => 'v', |
| 1382 | '/г/' => 'g', |
| 1383 | '/д/' => 'd', |
| 1384 | |
| 1385 | '/Е/' => 'e', |
| 1386 | '/Ж/' => 'zh', |
| 1387 | '/З/' => 'z', |
| 1388 | '/И/' => 'i', |
| 1389 | '/Й/' => 'j', |
| 1390 | '/е/' => 'e', |
| 1391 | '/ж/' => 'zh', |
| 1392 | '/з/' => 'z', |
| 1393 | '/и/' => 'i', |
| 1394 | '/й/' => 'j', |
| 1395 | |
| 1396 | '/К/' => 'k', |
| 1397 | '/Л/' => 'l', |
| 1398 | '/М/' => 'm', |
| 1399 | '/Н/' => 'n', |
| 1400 | '/О/' => 'o', |
| 1401 | '/к/' => 'k', |
| 1402 | '/л/' => 'l', |
| 1403 | '/м/' => 'm', |
| 1404 | '/н/' => 'n', |
| 1405 | '/о/' => 'o', |
| 1406 | |
| 1407 | '/П/' => 'p', |
| 1408 | '/Р/' => 'r', |
| 1409 | '/С/' => 's', |
| 1410 | '/Т/' => 't', |
| 1411 | '/У/' => 'u', |
| 1412 | '/п/' => 'p', |
| 1413 | '/р/' => 'r', |
| 1414 | '/с/' => 's', |
| 1415 | '/т/' => 't', |
| 1416 | '/у/' => 'u', |
| 1417 | |
| 1418 | '/Ф/' => 'f', |
| 1419 | '/Х/' => 'h', |
| 1420 | '/Ц/' => 'c', |
| 1421 | '/Ч/' => 'ch', |
| 1422 | '/Ш/' => 'sh', |
| 1423 | '/ф/' => 'f', |
| 1424 | '/х/' => 'h', |
| 1425 | '/ц/' => 'c', |
| 1426 | '/ч/' => 'ch', |
| 1427 | '/ш/' => 'sh', |
| 1428 | |
| 1429 | '/Щ/' => 'shch', |
| 1430 | '/Ь/' => '', |
| 1431 | '/Ю/' => 'ju', |
| 1432 | '/Я/' => 'ja', |
| 1433 | '/щ/' => 'shch', |
| 1434 | '/ь/' => '', |
| 1435 | '/ю/' => 'ju', |
| 1436 | '/я/' => 'ja', |
| 1437 | |
| 1438 | // Ukrainian |
| 1439 | '/Ґ/' => 'g', |
| 1440 | '/Є/' => 'ye', |
| 1441 | '/І/' => 'i', |
| 1442 | '/Ї/' => 'yi', |
| 1443 | '/ґ/' => 'g', |
| 1444 | '/є/' => 'ye', |
| 1445 | '/і/' => 'i', |
| 1446 | '/ї/' => 'yi', |
| 1447 | |
| 1448 | // Russian |
| 1449 | '/Ё/' => 'yo', |
| 1450 | '/Ы/' => 'y', |
| 1451 | '/Ъ/' => '', |
| 1452 | '/Э/' => 'e', |
| 1453 | '/ё/' => 'yo', |
| 1454 | '/ы/' => 'y', |
| 1455 | '/ъ/' => '', |
| 1456 | '/э/' => 'e', |
| 1457 | |
| 1458 | // Belorussian |
| 1459 | '/Ў/' => 'u', |
| 1460 | '/ў/' => 'u', |
| 1461 | |
| 1462 | // German |
| 1463 | '/Ä/' => 'ae', |
| 1464 | '/Ö/' => 'oe', |
| 1465 | '/Ü/' => 'ue', |
| 1466 | '/ß/' => 'ss', |
| 1467 | '/ä/' => 'ae', |
| 1468 | '/ö/' => 'oe', |
| 1469 | '/ü/' => 'ue', |
| 1470 | |
| 1471 | // Polish |
| 1472 | '/Ą/' => 'a', |
| 1473 | '/Ć/' => 'c', |
| 1474 | '/Ę/' => 'e', |
| 1475 | '/Ł/' => 'l', |
| 1476 | '/Ń/' => 'n', |
| 1477 | '/ą/' => 'a', |
| 1478 | '/ć/' => 'c', |
| 1479 | '/ę/' => 'e', |
| 1480 | '/ł/' => 'l', |
| 1481 | '/ń/' => 'n', |
| 1482 | '/Ó/' => 'o', |
| 1483 | '/Ś/' => 's', |
| 1484 | '/Ź/' => 'z', |
| 1485 | '/Ż/' => 'z', |
| 1486 | '/ó/' => 'o', |
| 1487 | '/ś/' => 's', |
| 1488 | '/ź/' => 'z', |
| 1489 | '/ż/' => 'z', |
| 1490 | |
| 1491 | // Hungarian |
| 1492 | '/Ő/' => 'o', |
| 1493 | '/Ű/' => 'u', |
| 1494 | '/ő/' => 'o', |
| 1495 | '/ű/' => 'u', |
| 1496 | |
| 1497 | // Czech |
| 1498 | '/Ě/' => 'e', |
| 1499 | '/Š/' => 's', |
| 1500 | '/Č/' => 'c', |
| 1501 | '/Ř/' => 'r', |
| 1502 | '/Ž/' => 'z', |
| 1503 | '/ě/' => 'e', |
| 1504 | '/š/' => 's', |
| 1505 | '/č/' => 'c', |
| 1506 | '/ř/' => 'r', |
| 1507 | '/ž/' => 'z', |
| 1508 | |
| 1509 | '/Ý/' => 'y', |
| 1510 | '/Á/' => 'a', |
| 1511 | '/É/' => 'e', |
| 1512 | '/Ď/' => 'd', |
| 1513 | '/Ť/' => 't', |
| 1514 | '/ý/' => 'y', |
| 1515 | '/á/' => 'a', |
| 1516 | '/é/' => 'e', |
| 1517 | '/ď/' => 'd', |
| 1518 | '/ť/' => 't', |
| 1519 | |
| 1520 | '/Ň/' => 'n', |
| 1521 | '/Ú/' => 'u', |
| 1522 | '/Ů/' => 'u', |
| 1523 | '/ň/' => 'n', |
| 1524 | '/ú/' => 'u', |
| 1525 | '/ů/' => 'u', |
| 1526 | |
| 1527 | // Greek alphabet & modern polytonic characters |
| 1528 | '/Α/' => 'a', |
| 1529 | '/Β/' => 'v', |
| 1530 | '/Γ/' => 'g', |
| 1531 | '/Δ/' => 'd', |
| 1532 | '/Ε/' => 'e', |
| 1533 | '/α/' => 'a', |
| 1534 | '/β/' => 'v', |
| 1535 | '/γ/' => 'g', |
| 1536 | '/δ/' => 'd', |
| 1537 | '/ε/' => 'e', |
| 1538 | |
| 1539 | '/Ζ/' => 'z', |
| 1540 | '/Η/' => 'i', |
| 1541 | '/Θ/' => 'th', |
| 1542 | '/Ι/' => 'i', |
| 1543 | '/Κ/' => 'k', |
| 1544 | '/ζ/' => 'z', |
| 1545 | '/η/' => 'i', |
| 1546 | '/θ/' => 'th', |
| 1547 | '/ι/' => 'i', |
| 1548 | '/κ/' => 'k', |
| 1549 | |
| 1550 | '/Λ/' => 'l', |
| 1551 | '/Μ/' => 'm', |
| 1552 | '/Ν/' => 'n', |
| 1553 | '/Ξ/' => 'x', |
| 1554 | '/Ο/' => 'o', |
| 1555 | '/λ/' => 'l', |
| 1556 | '/μ/' => 'm', |
| 1557 | '/ν/' => 'n', |
| 1558 | '/ξ/' => 'x', |
| 1559 | '/ο/' => 'o', |
| 1560 | |
| 1561 | '/Π/' => 'p', |
| 1562 | '/Ρ/' => 'r', |
| 1563 | '/Σ/' => 's', |
| 1564 | '/Τ/' => 't', |
| 1565 | '/Υ/' => 'y', |
| 1566 | '/π/' => 'p', |
| 1567 | '/ρ/' => 'r', |
| 1568 | '/σ/' => 's', |
| 1569 | '/τ/' => 't', |
| 1570 | '/υ/' => 'y', |
| 1571 | |
| 1572 | '/Φ/' => 'f', |
| 1573 | '/Χ/' => 'ch', |
| 1574 | '/Ψ/' => 'ps', |
| 1575 | '/Ω/' => 'o', |
| 1576 | '/Ά/' => 'a', |
| 1577 | '/φ/' => 'f', |
| 1578 | '/χ/' => 'ch', |
| 1579 | '/ψ/' => 'ps', |
| 1580 | '/ω/' => 'o', |
| 1581 | '/ά/' => 'a', |
| 1582 | |
| 1583 | '/Έ/' => 'e', |
| 1584 | '/Ή/' => 'i', |
| 1585 | '/Ί/' => 'i', |
| 1586 | '/Ό/' => 'o', |
| 1587 | '/Ύ/' => 'y', |
| 1588 | '/έ/' => 'e', |
| 1589 | '/ή/' => 'i', |
| 1590 | '/ί/' => 'i', |
| 1591 | '/ό/' => 'o', |
| 1592 | '/ύ/' => 'y', |
| 1593 | |
| 1594 | '/Ώ/' => 'o', |
| 1595 | '/Ϊ/' => 'i', |
| 1596 | '/Ϋ/' => 'y', |
| 1597 | '/ώ/' => 'o', |
| 1598 | '/ς/' => 's', |
| 1599 | '/ΐ/' => 'i', |
| 1600 | '/ϊ/' => 'i', |
| 1601 | '/ϋ/' => 'y', |
| 1602 | '/ΰ/' => 'y', |
| 1603 | |
| 1604 | // Extra all (http://www.atm.ox.ac.uk/user/iwi/charmap.html) |
| 1605 | '/À/' => 'a', |
| 1606 | '/Á/' => 'a', |
| 1607 | '/Â/' => 'a', |
| 1608 | '/Ã/' => 'a', |
| 1609 | '/Å/' => 'a', |
| 1610 | '/à/' => 'a', |
| 1611 | '/á/' => 'a', |
| 1612 | '/â/' => 'a', |
| 1613 | '/ã/' => 'a', |
| 1614 | '/å/' => 'a', |
| 1615 | |
| 1616 | '/Æ/' => 'ae', |
| 1617 | '/Ç/' => 'c', |
| 1618 | '/È/' => 'e', |
| 1619 | '/É/' => 'e', |
| 1620 | '/Ê/' => 'e', |
| 1621 | '/æ/' => 'ae', |
| 1622 | '/ç/' => 'c', |
| 1623 | '/è/' => 'e', |
| 1624 | '/é/' => 'e', |
| 1625 | '/ê/' => 'e', |
| 1626 | |
| 1627 | '/Ë/' => 'e', |
| 1628 | '/Ì/' => 'i', |
| 1629 | '/Í/' => 'i', |
| 1630 | '/Î/' => 'i', |
| 1631 | '/Ï/' => 'i', |
| 1632 | '/ë/' => 'e', |
| 1633 | '/ì/' => 'i', |
| 1634 | '/í/' => 'i', |
| 1635 | '/î/' => 'i', |
| 1636 | '/ï/' => 'i', |
| 1637 | |
| 1638 | '/Ð/' => 'd', |
| 1639 | '/Ñ/' => 'n', |
| 1640 | '/Ò/' => 'o', |
| 1641 | '/Ô/' => 'o', |
| 1642 | '/Õ/' => 'o', |
| 1643 | '/ð/' => 'd', |
| 1644 | '/ñ/' => 'n', |
| 1645 | '/ò/' => 'o', |
| 1646 | '/ô/' => 'o', |
| 1647 | '/õ/' => 'o', |
| 1648 | |
| 1649 | '/×/' => 'x', |
| 1650 | '/Ø/' => 'o', |
| 1651 | '/Ù/' => 'u', |
| 1652 | '/Ú/' => 'u', |
| 1653 | '/Û/' => 'u', |
| 1654 | '/×/' => 'x', |
| 1655 | '/ø/' => 'o', |
| 1656 | '/ù/' => 'u', |
| 1657 | '/ú/' => 'u', |
| 1658 | '/û/' => 'u', |
| 1659 | |
| 1660 | '/Þ/' => 'p', |
| 1661 | '/Ÿ/' => 'y', |
| 1662 | '/þ/' => 'p', |
| 1663 | '/ÿ/' => 'y', |
| 1664 | |
| 1665 | // Other |
| 1666 | '/№/' => '', |
| 1667 | '/“/' => '', |
| 1668 | '/”/' => '', |
| 1669 | '/«/' => '', |
| 1670 | '/»/' => '', |
| 1671 | '/„/' => '', |
| 1672 | '/@/' => '', |
| 1673 | '/%/' => '', |
| 1674 | '/‘/' => '', |
| 1675 | '/’/' => '', |
| 1676 | '/`/' => '', |
| 1677 | '/´/' => '', |
| 1678 | '/º/' => 'o', |
| 1679 | '/ª/' => 'a', |
| 1680 | |
| 1681 | ); |
| 1682 | |
| 1683 | // rewrite some chars for some languages |
| 1684 | $locale = get_locale(); |
| 1685 | switch ( $locale ) { |
| 1686 | case 'uk_UA': // Ukrainian |
| 1687 | case 'uk_ua': |
| 1688 | case 'uk': |
| 1689 | $chars_table_ext = array( |
| 1690 | '/Г/' => 'h', |
| 1691 | '/г/' => 'h', |
| 1692 | '/И/' => 'y', |
| 1693 | '/и/' => 'y' |
| 1694 | ); |
| 1695 | $chars_table = array_merge( $chars_table, $chars_table_ext ); |
| 1696 | break; |
| 1697 | case 'sv_SE': // Swedish |
| 1698 | case 'sv_se': |
| 1699 | $chars_table_ext = array( |
| 1700 | '/Å/' => 'a', |
| 1701 | '/å/' => 'a', |
| 1702 | '/Ä/' => 'a', |
| 1703 | '/ä/' => 'a', |
| 1704 | '/Ö/' => 'o', |
| 1705 | '/ö/' => 'o' |
| 1706 | ); |
| 1707 | $chars_table = array_merge( $chars_table, $chars_table_ext ); |
| 1708 | break; |
| 1709 | case 'bg_BG': // Bulgarian |
| 1710 | case 'bg_bg': |
| 1711 | $chars_table_ext = array( |
| 1712 | '/Щ/' => 'sht', |
| 1713 | '/щ/' => 'sht', |
| 1714 | '/Ъ/' => 'a', |
| 1715 | '/ъ/' => 'a' |
| 1716 | ); |
| 1717 | $chars_table = array_merge( $chars_table, $chars_table_ext ); |
| 1718 | break; |
| 1719 | } |
| 1720 | |
| 1721 | $friendly_filename = preg_replace( array_keys( $chars_table ), array_values( $chars_table ), |
| 1722 | $filename ); // replace original chars in filename with friendly chars |
| 1723 | |
| 1724 | return strtolower( $friendly_filename ); |
| 1725 | } |
| 1726 | |
| 1727 | /** |