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