1356 | | if ( ! empty( $field['args']['label_for'] ) ) { |
1357 | | echo '<th scope="row"><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label></th>'; |
| 1443 | echo '<div class="settings-field-title">'; |
| 1444 | |
| 1445 | if ( ! empty( $field['title'] ) ) { |
| 1446 | if ( ! empty( $field['args']['fieldset'] ) ) { |
| 1447 | echo '<span aria-hidden="true">' . $field['title'] . '</span>'; |
| 1448 | } elseif ( ! empty( $field['args']['label_for'] ) ) { |
| 1449 | echo '<label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label>'; |
| 1450 | } else { |
| 1451 | echo $field['title']; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | echo '</div>'; |
| 1456 | |
| 1457 | echo '<div class="settings-field-control">'; |
| 1458 | |
| 1459 | // Duplicate arguments to not modify globals permanently. |
| 1460 | $field_args = $field['args']; |
| 1461 | |
| 1462 | if ( ! empty( $field_args['value_callback'] ) ) { |
| 1463 | $field_args['value'] = call_user_func( $field_args['value_callback'], $field_args ); |
| 1464 | } |
| 1465 | |
| 1466 | if ( ! empty( $field_args['fieldset'] ) ) { |
| 1467 | echo '<fieldset>'; |
| 1468 | if ( ! empty( $field['title'] ) ) { |
| 1469 | echo '<legend class="screen-reader-text">' . $field['title'] . '</legend>'; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | if ( $field_args['before'] ) { |
| 1474 | if ( is_string( $field_args['before'] ) ) { |
| 1475 | echo $field_args['before']; |
| 1476 | } elseif ( is_callable( $field_args['before'] ) ) { |
| 1477 | call_user_func( $field_args['before'], $field_args ); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | call_user_func( $field['callback'], $field_args ); |
| 1482 | |
| 1483 | if ( $field_args['after'] ) { |
| 1484 | if ( is_string( $field_args['after'] ) ) { |
| 1485 | echo $field_args['after']; |
| 1486 | } elseif ( is_callable( $field_args['after'] ) ) { |
| 1487 | call_user_func( $field_args['after'], $field_args ); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | if ( ! empty( $field_args['fieldset'] ) ) { |
| 1492 | echo '</fieldset>'; |
| 1493 | } |
| 1494 | |
| 1495 | echo '</div>'; |
| 1496 | echo '</div>'; |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | /** |
| 1501 | * Renders a text input for a settings field. |
| 1502 | * |
| 1503 | * This function is used as a default callback when specifying 'text', |
| 1504 | * 'email', 'url' or 'tel' for the $callback parameter in |
| 1505 | * `add_settings_field()`. |
| 1506 | * |
| 1507 | * @since 4.8.0 |
| 1508 | * |
| 1509 | * @param array $field_args Field arguments. See the documentation for the |
| 1510 | * $args parameter of `add_settings_field()` for a |
| 1511 | * list of default arguments. |
| 1512 | */ |
| 1513 | function render_settings_field_text( $field_args ) { |
| 1514 | $input_attrs = array( |
| 1515 | 'type' => ! empty( $field_args['type'] ) ? $field_args['type'] : 'text', |
| 1516 | 'id' => ! empty( $field_args['input_id'] ) ? $field_args['input_id'] : '', |
| 1517 | 'name' => ! empty( $field_args['input_name'] ) ? $field_args['input_name'] : '', |
| 1518 | 'class' => ! empty( $field_args['input_class'] ) ? $field_args['input_class'] : '', |
| 1519 | 'value' => ! empty( $field_args['value'] ) ? $field_args['value'] : '', |
| 1520 | ); |
| 1521 | |
| 1522 | $description_attrs = array(); |
| 1523 | |
| 1524 | if ( ! empty( $field_args['description'] ) ) { |
| 1525 | if ( ! empty( $field_args['description_id'] ) ) { |
| 1526 | $description_attrs['id'] = $field_args['description_id']; |
| 1527 | $input_attrs['aria-describedby'] = $field_args['description_id']; |
| 1528 | } |
| 1529 | $description_attrs['class'] = 'description'; |
| 1530 | } |
| 1531 | |
| 1532 | echo '<input' . attrs( $input_attrs, false ) . ' />'; |
| 1533 | |
| 1534 | if ( ! empty( $field_args['description'] ) ) { |
| 1535 | echo '<p' . attrs( $description_attrs, false ) . '>' . $field_args['description'] . '</p>'; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | /** |
| 1540 | * Renders a number input for a settings field. |
| 1541 | * |
| 1542 | * This function is used as a default callback when specifying 'number' |
| 1543 | * for the $callback parameter in `add_settings_field()`. |
| 1544 | * |
| 1545 | * @since 4.8.0 |
| 1546 | * |
| 1547 | * @param array $field_args Field arguments. See the documentation for the |
| 1548 | * $args parameter of `add_settings_field()` for a |
| 1549 | * list of default arguments. |
| 1550 | */ |
| 1551 | function render_settings_field_number( $field_args ) { |
| 1552 | $input_attrs = array( |
| 1553 | 'type' => 'number', |
| 1554 | 'id' => ! empty( $field_args['input_id'] ) ? $field_args['input_id'] : '', |
| 1555 | 'name' => ! empty( $field_args['input_name'] ) ? $field_args['input_name'] : '', |
| 1556 | 'class' => ! empty( $field_args['input_class'] ) ? $field_args['input_class'] : '', |
| 1557 | 'value' => ! empty( $field_args['value'] ) ? $field_args['value'] : '', |
| 1558 | ); |
| 1559 | |
| 1560 | foreach ( array( 'min', 'max', 'step' ) as $attr ) { |
| 1561 | if ( isset( $field_args[ $attr ] ) ) { |
| 1562 | $input_attrs[ $attr ] = $field_args[ $attr ]; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | $description_attrs = array(); |
| 1567 | |
| 1568 | if ( ! empty( $field_args['description'] ) ) { |
| 1569 | if ( ! empty( $field_args['description_id'] ) ) { |
| 1570 | $description_attrs['id'] = $field_args['description_id']; |
| 1571 | $input_attrs['aria-describedby'] = $field_args['description_id']; |
| 1572 | } |
| 1573 | $description_attrs['class'] = 'description'; |
| 1574 | } |
| 1575 | |
| 1576 | echo '<input' . attrs( $input_attrs, false ) . ' />'; |
| 1577 | |
| 1578 | if ( ! empty( $field_args['description'] ) ) { |
| 1579 | echo '<p' . attrs( $description_attrs, false ) . '>' . $field_args['description'] . '</p>'; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | /** |
| 1584 | * Renders a textarea input for a settings field. |
| 1585 | * |
| 1586 | * This function is used as a default callback when specifying 'textarea' |
| 1587 | * for the $callback parameter in `add_settings_field()`. |
| 1588 | * |
| 1589 | * @since 4.8.0 |
| 1590 | * |
| 1591 | * @param array $field_args Field arguments. See the documentation for the |
| 1592 | * $args parameter of `add_settings_field()` for a |
| 1593 | * list of default arguments. |
| 1594 | */ |
| 1595 | function render_settings_field_textarea( $field_args ) { |
| 1596 | $input_attrs = array( |
| 1597 | 'id' => ! empty( $field_args['input_id'] ) ? $field_args['input_id'] : '', |
| 1598 | 'name' => ! empty( $field_args['input_name'] ) ? $field_args['input_name'] : '', |
| 1599 | 'class' => ! empty( $field_args['input_class'] ) ? $field_args['input_class'] : '', |
| 1600 | ); |
| 1601 | |
| 1602 | if ( isset( $field_args['rows'] ) ) { |
| 1603 | $input_attrs['rows'] = $field_args['rows']; |
| 1604 | } |
| 1605 | |
| 1606 | if ( isset( $field_args['cols'] ) ) { |
| 1607 | $input_attrs['cols'] = $field_args['cols']; |
| 1608 | } |
| 1609 | |
| 1610 | $description_attrs = array(); |
| 1611 | |
| 1612 | if ( ! empty( $field_args['description'] ) ) { |
| 1613 | if ( ! empty( $field_args['description_id'] ) ) { |
| 1614 | $description_attrs['id'] = $field_args['description_id']; |
| 1615 | $input_attrs['aria-describedby'] = $field_args['description_id']; |
| 1616 | } |
| 1617 | $description_attrs['class'] = 'description'; |
| 1618 | } |
| 1619 | |
| 1620 | $value = ! empty( $field_args['value'] ) ? $field_args['value'] : ''; |
| 1621 | |
| 1622 | echo '<textarea' . attrs( $input_attrs, false ) . '>' . esc_textarea( $value ) . '</textarea>'; |
| 1623 | |
| 1624 | if ( ! empty( $field_args['description'] ) ) { |
| 1625 | echo '<p' . attrs( $description_attrs, false ) . '>' . $field_args['description'] . '</p>'; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * Renders a dropdown input for a settings field. |
| 1631 | * |
| 1632 | * This function is used as a default callback when specifying 'select' |
| 1633 | * for the $callback parameter in `add_settings_field()`. |
| 1634 | * |
| 1635 | * @since 4.8.0 |
| 1636 | * |
| 1637 | * @param array $field_args Field arguments. See the documentation for the |
| 1638 | * $args parameter of `add_settings_field()` for a |
| 1639 | * list of default arguments. |
| 1640 | */ |
| 1641 | function render_settings_field_select( $field_args ) { |
| 1642 | $input_attrs = array( |
| 1643 | 'id' => ! empty( $field_args['input_id'] ) ? $field_args['input_id'] : '', |
| 1644 | 'name' => ! empty( $field_args['input_name'] ) ? $field_args['input_name'] : '', |
| 1645 | 'class' => ! empty( $field_args['input_class'] ) ? $field_args['input_class'] : '', |
| 1646 | ); |
| 1647 | |
| 1648 | if ( ! empty( $field_args['multiple'] ) ) { |
| 1649 | if ( ! empty( $input_attrs['name'] ) ) { |
| 1650 | $input_attrs['name'] .= '[]'; |
| 1651 | } |
| 1652 | $input_attrs['multiple'] = 'multiple'; |
| 1653 | } |
| 1654 | |
| 1655 | $description_attrs = array(); |
| 1656 | |
| 1657 | if ( ! empty( $field_args['description'] ) ) { |
| 1658 | if ( ! empty( $field_args['description_id'] ) ) { |
| 1659 | $description_attrs['id'] = $field_args['description_id']; |
| 1660 | $input_attrs['aria-describedby'] = $field_args['description_id']; |
| 1661 | } |
| 1662 | $description_attrs['class'] = 'description'; |
| 1663 | } |
| 1664 | |
| 1665 | $choices = ! empty( $field_args['choices'] ) ? $field_args['choices'] : array(); |
| 1666 | $current = ! empty( $field_args['value'] ) ? $field_args['value'] : ''; |
| 1667 | if ( ! empty( $field_args['multiple'] ) ) { |
| 1668 | $current = ! empty( $current ) ? (array) $current : array(); |
| 1669 | } |
| 1670 | |
| 1671 | echo '<select' . attrs( $input_attrs, false ) . '>'; |
| 1672 | |
| 1673 | foreach ( $choices as $value => $label ) { |
| 1674 | $selected = ''; |
| 1675 | if ( ! empty( $field_args['multiple'] ) && in_array( $value, $current ) ) { |
| 1676 | $selected = ' selected="selected"'; |
| 1677 | } elseif ( empty( $field_args['multiple'] ) ) { |
| 1678 | $selected = selected( $current, $value, false ); |
| 1679 | } |
| 1680 | echo '<option value="' . esc_attr( $value ) . '"' . $selected . '>' . esc_html( $label ) . '</option>'; |
| 1681 | } |
| 1682 | |
| 1683 | echo '</select>'; |
| 1684 | |
| 1685 | if ( ! empty( $field_args['description'] ) ) { |
| 1686 | echo '<p' . attrs( $description_attrs, false ) . '>' . $field_args['description'] . '</p>'; |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | /** |
| 1691 | * Renders a checkbox input for a settings field. |
| 1692 | * |
| 1693 | * This function is used as a default callback when specifying 'checkbox' |
| 1694 | * for the $callback parameter in `add_settings_field()`. |
| 1695 | * |
| 1696 | * @since 4.8.0 |
| 1697 | * |
| 1698 | * @param array $field_args Field arguments. See the documentation for the |
| 1699 | * $args parameter of `add_settings_field()` for a |
| 1700 | * list of default arguments. |
| 1701 | */ |
| 1702 | function render_settings_field_checkbox( $field_args ) { |
| 1703 | $input_attrs = array( |
| 1704 | 'type' => 'checkbox', |
| 1705 | 'id' => ! empty( $field_args['input_id'] ) ? $field_args['input_id'] : '', |
| 1706 | 'name' => ! empty( $field_args['input_name'] ) ? $field_args['input_name'] : '', |
| 1707 | 'class' => ! empty( $field_args['input_class'] ) ? $field_args['input_class'] : '', |
| 1708 | 'value' => '1', |
| 1709 | ); |
| 1710 | |
| 1711 | $description_attrs = array(); |
| 1712 | |
| 1713 | if ( ! empty( $field_args['description'] ) ) { |
| 1714 | if ( ! empty( $field_args['description_id'] ) ) { |
| 1715 | $description_attrs['id'] = $field_args['description_id']; |
| 1716 | $input_attrs['aria-describedby'] = $field_args['description_id']; |
| 1717 | } |
| 1718 | $description_attrs['class'] = 'description'; |
| 1719 | } |
| 1720 | |
| 1721 | $current = ! empty( $field_args['value'] ) ? $field_args['value'] : ''; |
| 1722 | |
| 1723 | echo '<input' . attrs( $input_attrs, false ) . checked( $current, true, false ) . ' />'; |
| 1724 | |
| 1725 | if ( ! empty( $field_args['label'] ) ) { |
| 1726 | if ( ! empty( $field_args['label_for'] ) ) { |
| 1727 | echo ' <label for="' . esc_attr( $field_args['label_for'] ) . '">' . $field_args['label'] . '</label>'; |
| 1739 | * Renders a radio input for a settings field. |
| 1740 | * |
| 1741 | * This function is used as a default callback when specifying 'radio' |
| 1742 | * for the $callback parameter in `add_settings_field()`. |
| 1743 | * |
| 1744 | * @since 4.8.0 |
| 1745 | * |
| 1746 | * @param array $field_args Field arguments. See the documentation for the |
| 1747 | * $args parameter of `add_settings_field()` for a |
| 1748 | * list of default arguments. |
| 1749 | */ |
| 1750 | function render_settings_field_radio( $field_args ) { |
| 1751 | $input_attrs = array( |
| 1752 | 'type' => 'radio', |
| 1753 | 'id' => ! empty( $field_args['input_id'] ) ? $field_args['input_id'] : '', |
| 1754 | 'name' => ! empty( $field_args['input_name'] ) ? $field_args['input_name'] : '', |
| 1755 | 'class' => ! empty( $field_args['input_class'] ) ? $field_args['input_class'] : '', |
| 1756 | ); |
| 1757 | |
| 1758 | $choices = ! empty( $field_args['choices'] ) ? $field_args['choices'] : array(); |
| 1759 | $current = ! empty( $field_args['value'] ) ? $field_args['value'] : ''; |
| 1760 | |
| 1761 | $id_suffix = 0; |
| 1762 | foreach ( $choices as $value => $label ) { |
| 1763 | $id_suffix++; |
| 1764 | |
| 1765 | $radio_attrs = $input_attrs; |
| 1766 | $radio_attrs['id'] .= '-' . zeroise( $id_suffix, 2 ); |
| 1767 | $radio_attrs['value'] = $value; |
| 1768 | |
| 1769 | echo '<span class="radio-item">'; |
| 1770 | echo '<input' . attrs( $radio_attrs, false ) . checked( $current, $value, false ) . ' />'; |
| 1771 | echo ' <label for="' . $radio_attrs['id'] . '">' . $label . '</label>'; |
| 1772 | echo '</span><br />'; |
| 1773 | } |
| 1774 | |
| 1775 | $description_attrs = array(); |
| 1776 | |
| 1777 | if ( ! empty( $field_args['description'] ) ) { |
| 1778 | if ( ! empty( $field_args['description_id'] ) ) { |
| 1779 | $description_attrs['id'] = $field_args['description_id']; |
| 1780 | } |
| 1781 | |
| 1782 | $description_attrs['class'] = 'description'; |
| 1783 | |
| 1784 | echo '<p' . attrs( $description_attrs, false ) . '>' . $field_args['description'] . '</p>'; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | /** |
| 1789 | * Renders a multiple checkboxes input for a settings field. |
| 1790 | * |
| 1791 | * This function is used as a default callback when specifying 'multibox' |
| 1792 | * for the $callback parameter in `add_settings_field()`. |
| 1793 | * |
| 1794 | * @since 4.8.0 |
| 1795 | * |
| 1796 | * @param array $field_args Field arguments. See the documentation for the |
| 1797 | * $args parameter of `add_settings_field()` for a |
| 1798 | * list of default arguments. |
| 1799 | */ |
| 1800 | function render_settings_field_multibox( $field_args ) { |
| 1801 | $input_attrs = array( |
| 1802 | 'type' => 'checkbox', |
| 1803 | 'id' => ! empty( $field_args['input_id'] ) ? $field_args['input_id'] : '', |
| 1804 | 'name' => ! empty( $field_args['input_name'] ) ? $field_args['input_name'] . '[]' : '', |
| 1805 | 'class' => ! empty( $field_args['input_class'] ) ? $field_args['input_class'] : '', |
| 1806 | ); |
| 1807 | |
| 1808 | $choices = ! empty( $field_args['choices'] ) ? $field_args['choices'] : array(); |
| 1809 | $current = ! empty( $field_args['value'] ) ? (array) $field_args['value'] : array(); |
| 1810 | |
| 1811 | $id_suffix = 0; |
| 1812 | foreach ( $choices as $value => $label ) { |
| 1813 | $id_suffix++; |
| 1814 | |
| 1815 | $checkbox_attrs = $input_attrs; |
| 1816 | $checkbox_attrs['id'] .= '-' . zeroise( $id_suffix, 2 ); |
| 1817 | $checkbox_attrs['value'] = $value; |
| 1818 | |
| 1819 | if ( in_array( $value, $current ) ) { |
| 1820 | $checkbox_attrs['checked'] = 'checked'; |
| 1821 | } |
| 1822 | |
| 1823 | echo '<span class="multibox-item">'; |
| 1824 | echo '<input' . attrs( $checkbox_attrs, false ) . ' />'; |
| 1825 | echo ' <label for="' . $checkbox_attrs['id'] . '">' . $label . '</label>'; |
| 1826 | echo '</span><br />'; |
| 1827 | } |
| 1828 | |
| 1829 | $description_attrs = array(); |
| 1830 | |
| 1831 | if ( ! empty( $field_args['description'] ) ) { |
| 1832 | if ( ! empty( $field_args['description_id'] ) ) { |
| 1833 | $description_attrs['id'] = $field_args['description_id']; |
| 1834 | } |
| 1835 | |
| 1836 | $description_attrs['class'] = 'description'; |
| 1837 | |
| 1838 | echo '<p' . attrs( $description_attrs, false ) . '>' . $field_args['description'] . '</p>'; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | /** |
| 1843 | * Creates an attribute string from an array of attributes. |
| 1844 | * |
| 1845 | * @since 4.8.0 |
| 1846 | * |
| 1847 | * @param array $attrs Array of attributes as $key => $value pairs. |
| 1848 | * @param bool $echo Optional. Whether to echo the attribute string. |
| 1849 | * Default true. |
| 1850 | * @return string The attribute string. |
| 1851 | */ |
| 1852 | function attrs( $attrs, $echo = true ) { |
| 1853 | $attribute_string = ''; |
| 1854 | |
| 1855 | foreach ( $attrs as $key => $value ) { |
| 1856 | $attribute_string .= ' ' . $key . '="' . esc_attr( $value ) . '"'; |
| 1857 | } |
| 1858 | |
| 1859 | if ( $echo ) { |
| 1860 | echo $attribute_string; |
| 1861 | } |
| 1862 | |
| 1863 | return $attribute_string; |
| 1864 | } |
| 1865 | |
| 1866 | /** |
| 1867 | * Retrieves the value for a settings field, based on the field arguments. |
| 1868 | * |
| 1869 | * The function will call `get_option()` based on the 'input_name' argument |
| 1870 | * passed. It will automatically look for the correct key if an array option |
| 1871 | * is used for the field. |
| 1872 | * |
| 1873 | * This is the default callback function used when registering a field with |
| 1874 | * `add_settings_field()`. |
| 1875 | * |
| 1876 | * @since 4.8.0 |
| 1877 | * |
| 1878 | * @param array $field_args Field arguments. See the documentation for the |
| 1879 | * $args parameter of `add_settings_field()` for a |
| 1880 | * list of default arguments. |
| 1881 | * @return mixed The value for the settings field, or null if no value set. |
| 1882 | */ |
| 1883 | function get_settings_field_option( $field_args ) { |
| 1884 | if ( empty( $field_args['input_name'] ) ) { |
| 1885 | return null; |
| 1886 | } |
| 1887 | |
| 1888 | $keys = preg_split( '/\[/', str_replace( ']', '', $field_args['input_name'] ) ); |
| 1889 | $base = array_shift( $keys ); |
| 1890 | |
| 1891 | $value = get_option( $base, null ); |
| 1892 | while ( ! empty( $keys ) ) { |
| 1893 | $current_key = array_shift( $keys ); |
| 1894 | if ( ! isset( $value[ $current_key ] ) ) { |
| 1895 | return null; |
| 1896 | } |
| 1897 | |
| 1898 | $value = $value[ $current_key ]; |
| 1899 | } |
| 1900 | |
| 1901 | return $value; |
| 1902 | } |
| 1903 | |
| 1904 | /** |