| 851 | /** |
| 852 | * Builds and validates a year string based on the comparison operator. |
| 853 | * |
| 854 | * @since 5.0.0 |
| 855 | * |
| 856 | * @param string $compare The compare operator to use |
| 857 | * @param string|array $value The value |
| 858 | * @return string|false|int The value to be used in SQL or false on error. |
| 859 | */ |
| 860 | public function build_year( $compare, $column, $value ) { |
| 861 | if ( ! isset( $value ) ) { |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | switch ( $compare ) { |
| 866 | case 'IN': |
| 867 | case 'NOT IN': |
| 868 | $value = (array) $value; |
| 869 | |
| 870 | // Remove non-numeric values. |
| 871 | $value = array_filter( $value, 'is_numeric' ); |
| 872 | |
| 873 | if ( empty( $value ) ) { |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | return $column . $compare . 'DATE(' . implode( ',', array_map( 'intval', $value ) ) . '-01-01)'; |
| 878 | |
| 879 | case 'BETWEEN': |
| 880 | case 'NOT BETWEEN': |
| 881 | if ( ! is_array( $value ) || 2 != count( $value ) ) { |
| 882 | $value = array( $value, $value ); |
| 883 | } else { |
| 884 | $value = array_values( $value ); |
| 885 | } |
| 886 | |
| 887 | // If either value is non-numeric, bail. |
| 888 | foreach ( $value as $v ) { |
| 889 | if ( ! is_numeric( $v ) ) { |
| 890 | return false; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | $value = array_map( 'intval', $value ); |
| 895 | |
| 896 | return $column . ' >= DATE("' . $value[0] . '-01-01") AND ' . $column . ' < DATE("' . $value[1] . '-01-01")'; |
| 897 | |
| 898 | default: |
| 899 | if ( ! is_numeric( $value ) ) { |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | return (int) $value; |
| 904 | } |
| 905 | } |
| 906 | |