| 214 | | $this->process_default_headers(); |
| 215 | | $default = get_theme_support( 'custom-header', 'default-image' ); |
| 216 | | $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); |
| 217 | | foreach ( $this->default_headers as $header => $details ) { |
| 218 | | if ( $details['url'] == $default ) { |
| 219 | | $default_data = $details; |
| 220 | | break; |
| 221 | | } |
| 222 | | } |
| 223 | | set_theme_mod( 'header_image', $default ); |
| 224 | | if ( empty( $default_data['width'] ) ) |
| 225 | | $default_data['width'] = get_theme_support( 'custom-header', 'width' ); |
| 226 | | if ( empty( $default_data['height'] ) ) |
| 227 | | $default_data['height'] = get_theme_support( 'custom-header', 'height' ); |
| 228 | | set_theme_mod( 'header_image_data', (object) $default_data ); |
| | 214 | $this->reset_header_image(); |
| 259 | | if ( 'random-default-image' == $_POST['default-header'] ) { |
| 260 | | set_theme_mod( 'header_image', 'random-default-image' ); |
| 261 | | } elseif ( 'random-uploaded-image' == $_POST['default-header'] ) { |
| 262 | | set_theme_mod( 'header_image', 'random-uploaded-image' ); |
| 263 | | } else { |
| 264 | | $this->process_default_headers(); |
| 265 | | $uploaded = get_uploaded_header_images(); |
| 266 | | if ( isset( $uploaded[$_POST['default-header']] ) ) { |
| 267 | | set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) ); |
| 268 | | set_theme_mod( 'header_image_data', (object) $uploaded[$_POST['default-header']] ); |
| 269 | | } elseif ( isset( $this->default_headers[$_POST['default-header']] ) ) { |
| 270 | | set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) ); |
| 271 | | if ( empty( $this->default_headers[$_POST['default-header']]['width'] ) ) |
| 272 | | $this->default_headers[$_POST['default-header']]['width'] = get_theme_support( 'custom-header', 'width' ); |
| 273 | | if ( empty( $this->default_headers[$_POST['default-header']]['height'] ) ) |
| 274 | | $this->default_headers[$_POST['default-header']]['height'] = get_theme_support( 'custom-header', 'height' ); |
| 275 | | set_theme_mod( 'header_image_data', (object) $this->default_headers[$_POST['default-header']] ); |
| 276 | | } |
| 277 | | } |
| | 245 | $this->choose_existing_header_image( $_POST['default-header'] ); |
| | 246 | return; |
| | 961 | /** |
| | 962 | * Remove a header image. |
| | 963 | * |
| | 964 | * @since 3.4.0 |
| | 965 | */ |
| | 966 | final public function remove_header_image() { |
| | 967 | set_theme_mod( 'header_image', 'remove-header' ); |
| | 968 | remove_theme_mod( 'header_image_data' ); |
| | 969 | } |
| | 970 | |
| | 971 | /** |
| | 972 | * Reset a header image to the default image for the theme. |
| | 973 | * |
| | 974 | * This method does not do anything if the theme does not have a default header image. |
| | 975 | * |
| | 976 | * @since 3.4.0 |
| | 977 | */ |
| | 978 | final public function reset_header_image() { |
| | 979 | $this->process_default_headers(); |
| | 980 | $default = get_theme_support( 'custom-header', 'default-image' ); |
| | 981 | |
| | 982 | if ( ! $default ) |
| | 983 | return; |
| | 984 | |
| | 985 | $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); |
| | 986 | |
| | 987 | foreach ( $this->default_headers as $header => $details ) { |
| | 988 | if ( $details['url'] == $default ) { |
| | 989 | $default_data = $details; |
| | 990 | break; |
| | 991 | } |
| | 992 | } |
| | 993 | |
| | 994 | if ( empty( $default_data['width'] ) ) |
| | 995 | $default_data['width'] = get_theme_support( 'custom-header', 'width' ); |
| | 996 | if ( empty( $default_data['height'] ) ) |
| | 997 | $default_data['height'] = get_theme_support( 'custom-header', 'height' ); |
| | 998 | set_theme_mod( 'header_image', $default ); |
| | 999 | set_theme_mod( 'header_image_data', (object) $default_data ); |
| | 1000 | } |
| | 1001 | |
| | 1002 | /** |
| | 1003 | * Choose a header image, selected from existing uploaded and default headers. |
| | 1004 | * |
| | 1005 | * @param string $choice Which header image to select. Allows for values of 'random-default-image', |
| | 1006 | * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling |
| | 1007 | * among the uploaded images; the key of a default image registered for that theme; and |
| | 1008 | * the key of an image uploaded for that theme (the basename of the URL). |
| | 1009 | */ |
| | 1010 | final public function choose_existing_header_image( $choice ) { |
| | 1011 | if ( 'random-default-image' == $choice ) { |
| | 1012 | set_theme_mod( 'header_image', 'random-default-image' ); |
| | 1013 | remove_theme_mod( 'header_image_data' ); |
| | 1014 | return; |
| | 1015 | } |
| | 1016 | |
| | 1017 | if ( 'random-uploaded-image' == $choice ) { |
| | 1018 | set_theme_mod( 'header_image', 'random-uploaded-image' ); |
| | 1019 | remove_theme_mod( 'header_image_data' ); |
| | 1020 | return; |
| | 1021 | } |
| | 1022 | |
| | 1023 | $this->process_default_headers(); |
| | 1024 | if ( isset( $this->default_headers[ $choice ] ) ) { |
| | 1025 | if ( empty( $this->default_headers[ $choice ]['width'] ) ) |
| | 1026 | $this->default_headers[ $choice ]['width'] = get_theme_support( 'custom-header', 'width' ); |
| | 1027 | if ( empty( $this->default_headers[ $choice ]['height'] ) ) |
| | 1028 | $this->default_headers[ $choice ]['height'] = get_theme_support( 'custom-header', 'height' ); |
| | 1029 | set_theme_mod( 'header_image', esc_url_raw( $this->default_headers[ $choice ]['url'] ) ); |
| | 1030 | set_theme_mod( 'header_image_data', (object) $this->default_headers[ $choice ] ); |
| | 1031 | return; |
| | 1032 | } |
| | 1033 | |
| | 1034 | $uploaded = get_uploaded_header_images(); |
| | 1035 | if ( isset( $uploaded[ $choice ] ) ) { |
| | 1036 | set_theme_mod( 'header_image', esc_url_raw( $uploaded[ $choice ]['url'] ) ); |
| | 1037 | set_theme_mod( 'header_image_data', (object) $uploaded[ $choice ] ); |
| | 1038 | return; |
| | 1039 | } |
| | 1040 | } |
| | 1041 | |
| | 1042 | /** |
| | 1043 | * Select a header image, either newly uploaded or from the media library. |
| | 1044 | * |
| | 1045 | * @since 3.4.0 |
| | 1046 | * |
| | 1047 | * @param array $args An array of arguments: attachment_id, url, width, height. All are required. |
| | 1048 | */ |
| | 1049 | final public function select_new_header_image( $args ) { |
| | 1050 | extract( $args ); |
| | 1051 | |
| | 1052 | $url = esc_url_raw( $url ); |
| | 1053 | |
| | 1054 | $header_data = new stdClass(); |
| | 1055 | $header_data->attachment_id = $attachment_id; |
| | 1056 | $header_data->url = $url; |
| | 1057 | $header_data->thumbnail_url = $url; |
| | 1058 | $header_data->width = $width; |
| | 1059 | $header_data->height = $height; |
| | 1060 | |
| | 1061 | update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() ); |
| | 1062 | set_theme_mod( 'header_image', $url ); |
| | 1063 | set_theme_mod( 'header_image_data', $header_data ); |
| | 1064 | } |