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