~ubuntu-branches/debian/jessie/wordpress/jessie

« back to all changes in this revision

Viewing changes to wp-admin/custom-header.php

  • Committer: Package Import Robot
  • Author(s): Craig Small
  • Date: 2014-04-17 20:56:19 UTC
  • mfrom: (1.2.35)
  • Revision ID: package-import@ubuntu.com-20140417205619-nurbet6eho4yvwfv
Tags: 3.9+dfsg-1
* New upstream release
* 3.9 seems to handle different locations for plugins so the
  plugin directory handling patches have been cut back.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
         * @since 3.0.0
41
41
         * @access private
42
42
         */
43
 
        var $default_headers = array();
 
43
        var $default_headers;
44
44
 
45
45
        /**
46
 
         * Holds custom headers uploaded by the user
 
46
         * Holds custom headers uploaded by the user.
47
47
         *
48
48
         * @var array
49
49
         * @since 3.2.0
73
73
                $this->admin_image_div_callback = $admin_image_div_callback;
74
74
 
75
75
                add_action( 'admin_menu', array( $this, 'init' ) );
 
76
 
 
77
                add_action( 'customize_save_after',         array( $this, 'customize_set_last_used' ) );
 
78
                add_action( 'wp_ajax_custom-header-crop',   array( $this, 'ajax_header_crop'        ) );
 
79
                add_action( 'wp_ajax_custom-header-add',    array( $this, 'ajax_header_add'         ) );
 
80
                add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove'      ) );
76
81
        }
77
82
 
78
83
        /**
93
98
                add_action("admin_head-$page", array($this, 'js'), 50);
94
99
                if ( $this->admin_header_callback )
95
100
                        add_action("admin_head-$page", $this->admin_header_callback, 51);
 
101
 
96
102
        }
97
103
 
98
104
        /**
131
137
                get_current_screen()->set_help_sidebar(
132
138
                        '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
133
139
                        '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Header_Screen" target="_blank">Documentation on Custom Header</a>' ) . '</p>' .
134
 
                        '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
 
140
                        '<p>' . __( '<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
135
141
                );
136
142
        }
137
143
 
177
183
        /**
178
184
         * Set up the enqueue for the CSS files
179
185
         *
180
 
         * @since 2.7
 
186
         * @since 2.7.0
181
187
         */
182
188
        function css_includes() {
183
189
                $step = $this->step();
248
254
                if ( !isset($_wp_default_headers) )
249
255
                        return;
250
256
 
 
257
                if ( is_array( $this->default_headers ) ) {
 
258
                        return;
 
259
                }
 
260
 
251
261
                $this->default_headers = $_wp_default_headers;
252
262
                $template_directory_uri = get_template_directory_uri();
253
263
                $stylesheet_directory_uri = get_stylesheet_directory_uri();
255
265
                        $this->default_headers[$header]['url'] =  sprintf( $this->default_headers[$header]['url'], $template_directory_uri, $stylesheet_directory_uri );
256
266
                        $this->default_headers[$header]['thumbnail_url'] =  sprintf( $this->default_headers[$header]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
257
267
                }
258
 
 
259
268
        }
260
269
 
261
270
        /**
450
459
?>
451
460
 
452
461
<div class="wrap">
453
 
<h2><?php _e('Custom Header'); ?></h2>
 
462
<h2><?php _e( 'Custom Header' ); ?></h2>
454
463
 
455
464
<?php if ( ! empty( $this->updated ) ) { ?>
456
465
<div id="message" class="updated">
464
473
<tbody>
465
474
 
466
475
<?php if ( get_custom_header() || display_header_text() ) : ?>
467
 
<tr valign="top">
 
476
<tr>
468
477
<th scope="row"><?php _e( 'Preview' ); ?></th>
469
478
<td>
470
 
        <?php if ( $this->admin_image_div_callback ) {
471
 
          call_user_func( $this->admin_image_div_callback );
 
479
        <?php
 
480
        if ( $this->admin_image_div_callback ) {
 
481
                call_user_func( $this->admin_image_div_callback );
472
482
        } else {
473
483
                $custom_header = get_custom_header();
474
484
                $header_image_style = 'background-image:url(' . esc_url( get_header_image() ) . ');';
493
503
<?php endif; ?>
494
504
 
495
505
<?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
496
 
<tr valign="top">
 
506
<tr>
497
507
<th scope="row"><?php _e( 'Select Image' ); ?></th>
498
508
<td>
499
509
        <p><?php _e( 'You can select an image to be shown at the top of your site by uploading from your computer or choosing from your media library. After selecting an image you will be able to crop it.' ); ?><br />
547
557
<table class="form-table">
548
558
<tbody>
549
559
        <?php if ( get_uploaded_header_images() ) : ?>
550
 
<tr valign="top">
 
560
<tr>
551
561
<th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
552
562
<td>
553
563
        <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
558
568
</tr>
559
569
        <?php endif;
560
570
        if ( ! empty( $this->default_headers ) ) : ?>
561
 
<tr valign="top">
 
571
<tr>
562
572
<th scope="row"><?php _e( 'Default Images' ); ?></th>
563
573
<td>
564
574
<?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
573
583
</tr>
574
584
        <?php endif;
575
585
        if ( get_header_image() ) : ?>
576
 
<tr valign="top">
 
586
<tr>
577
587
<th scope="row"><?php _e( 'Remove Image' ); ?></th>
578
588
<td>
579
589
        <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
584
594
 
585
595
        $default_image = get_theme_support( 'custom-header', 'default-image' );
586
596
        if ( $default_image && get_header_image() != $default_image ) : ?>
587
 
<tr valign="top">
 
597
<tr>
588
598
<th scope="row"><?php _e( 'Reset Image' ); ?></th>
589
599
<td>
590
600
        <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
601
611
 
602
612
<table class="form-table">
603
613
<tbody>
604
 
<tr valign="top">
 
614
<tr>
605
615
<th scope="row"><?php _e( 'Header Text' ); ?></th>
606
616
<td>
607
617
        <p>
610
620
</td>
611
621
</tr>
612
622
 
613
 
<tr valign="top" class="displaying-header-text">
 
623
<tr class="displaying-header-text">
614
624
<th scope="row"><?php _e( 'Text Color' ); ?></th>
615
625
<td>
616
626
        <p>
818
828
                $attachment_id = absint( $_POST['attachment_id'] );
819
829
                $original = get_attached_file($attachment_id);
820
830
 
821
 
 
822
 
                $max_width = 0;
823
 
                // For flex, limit size of image displayed to 1500px unless theme says otherwise
824
 
                if ( current_theme_supports( 'custom-header', 'flex-width' ) )
825
 
                        $max_width = 1500;
826
 
 
827
 
                if ( current_theme_supports( 'custom-header', 'max-width' ) )
828
 
                        $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
829
 
                $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
830
 
 
831
 
                if ( ( current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) || $_POST['width'] > $max_width )
832
 
                        $dst_height = absint( $_POST['height'] * ( $max_width / $_POST['width'] ) );
833
 
                elseif ( current_theme_supports( 'custom-header', 'flex-height' ) && current_theme_supports( 'custom-header', 'flex-width' ) )
834
 
                        $dst_height = absint( $_POST['height'] );
835
 
                else
836
 
                        $dst_height = get_theme_support( 'custom-header', 'height' );
837
 
 
838
 
                if ( ( current_theme_supports( 'custom-header', 'flex-width' ) && ! current_theme_supports( 'custom-header', 'flex-height' ) ) || $_POST['width'] > $max_width )
839
 
                        $dst_width = absint( $_POST['width'] * ( $max_width / $_POST['width'] ) );
840
 
                elseif ( current_theme_supports( 'custom-header', 'flex-width' ) && current_theme_supports( 'custom-header', 'flex-height' ) )
841
 
                        $dst_width = absint( $_POST['width'] );
842
 
                else
843
 
                        $dst_width = get_theme_support( 'custom-header', 'width' );
 
831
                $dimensions = $this->get_header_dimensions( array(
 
832
                        'height' => $_POST['height'],
 
833
                        'width'  => $_POST['width'],
 
834
                ) );
 
835
                $height = $dimensions['dst_height'];
 
836
                $width = $dimensions['dst_width'];
844
837
 
845
838
                if ( empty( $_POST['skip-cropping'] ) )
846
 
                        $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height );
 
839
                        $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height );
847
840
                elseif ( ! empty( $_POST['create-new-attachment'] ) )
848
841
                        $cropped = _copy_image_file( $attachment_id );
849
842
                else
855
848
                /** This filter is documented in wp-admin/custom-header.php */
856
849
                $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
857
850
 
858
 
                $parent = get_post($attachment_id);
859
 
                $parent_url = $parent->guid;
860
 
                $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
861
 
 
862
 
                $size = @getimagesize( $cropped );
863
 
                $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
864
 
 
865
 
                // Construct the object array
866
 
                $object = array(
867
 
                        'ID' => $attachment_id,
868
 
                        'post_title' => basename($cropped),
869
 
                        'post_content' => $url,
870
 
                        'post_mime_type' => $image_type,
871
 
                        'guid' => $url,
872
 
                        'context' => 'custom-header'
873
 
                );
 
851
                $object = $this->create_attachment_object( $cropped, $attachment_id );
 
852
 
874
853
                if ( ! empty( $_POST['create-new-attachment'] ) )
875
854
                        unset( $object['ID'] );
876
855
 
877
856
                // Update the attachment
878
 
                $attachment_id = wp_insert_attachment( $object, $cropped );
879
 
                wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
 
857
                $attachment_id = $this->insert_attachment( $object, $cropped );
880
858
 
881
 
                $width = $dst_width;
882
 
                $height = $dst_height;
 
859
                $url = $object['guid'];
883
860
                $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
884
861
 
885
862
                // cleanup
1040
1017
                set_theme_mod( 'header_image', $default );
1041
1018
                set_theme_mod( 'header_image_data', (object) $default_data );
1042
1019
        }
 
1020
 
 
1021
        /**
 
1022
         * Calculate width and height based on what the currently selected theme supports.
 
1023
         *
 
1024
         * @return array dst_height and dst_width of header image.
 
1025
         */
 
1026
        final public function get_header_dimensions( $dimensions ) {
 
1027
                $max_width = 0;
 
1028
                $width = absint( $dimensions['width'] );
 
1029
                $height = absint( $dimensions['height'] );
 
1030
                $theme_height = get_theme_support( 'custom-header', 'height' );
 
1031
                $theme_width = get_theme_support( 'custom-header', 'width' );
 
1032
                $has_flex_width = current_theme_supports( 'custom-header', 'flex-width' );
 
1033
                $has_flex_height = current_theme_supports( 'custom-header', 'flex-height' );
 
1034
                $has_max_width = current_theme_supports( 'custom-header', 'max-width' ) ;
 
1035
                $dst = array( 'dst_height' => null, 'dst_height' => null );
 
1036
 
 
1037
                // For flex, limit size of image displayed to 1500px unless theme says otherwise
 
1038
                if ( $has_flex_width ) {
 
1039
                        $max_width = 1500;
 
1040
                }
 
1041
 
 
1042
                if ( $has_max_width ) {
 
1043
                        $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
 
1044
                }
 
1045
                $max_width = max( $max_width, $theme_width );
 
1046
 
 
1047
                if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
 
1048
                        $dst['dst_height'] = absint( $height * ( $max_width / $width ) );
 
1049
                }
 
1050
                elseif ( $has_flex_height && $has_flex_width ) {
 
1051
                        $dst['dst_height'] = $height;
 
1052
                }
 
1053
                else {
 
1054
                        $dst['dst_height'] = $theme_height;
 
1055
                }
 
1056
 
 
1057
                if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
 
1058
                        $dst['dst_width'] = absint( $width * ( $max_width / $width ) );
 
1059
                }
 
1060
                elseif ( $has_flex_width && $has_flex_height ) {
 
1061
                        $dst['dst_width'] = $width;
 
1062
                }
 
1063
                else {
 
1064
                        $dst['dst_width'] = $theme_width;
 
1065
                }
 
1066
 
 
1067
                return $dst;
 
1068
        }
 
1069
 
 
1070
        /**
 
1071
         * Create an attachment 'object'.
 
1072
         *
 
1073
         * @param string $cropped Cropped image URL.
 
1074
         * @param int $parent_attachment_id Attachment ID of parent image.
 
1075
         *
 
1076
         * @return array Attachment object.
 
1077
         */
 
1078
        final public function create_attachment_object( $cropped, $parent_attachment_id ) {
 
1079
                $parent = get_post( $parent_attachment_id );
 
1080
                $parent_url = $parent->guid;
 
1081
                $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
 
1082
 
 
1083
                $size = @getimagesize( $cropped );
 
1084
                $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
 
1085
 
 
1086
                $object = array(
 
1087
                        'ID' => $parent_attachment_id,
 
1088
                        'post_title' => basename($cropped),
 
1089
                        'post_content' => $url,
 
1090
                        'post_mime_type' => $image_type,
 
1091
                        'guid' => $url,
 
1092
                        'context' => 'custom-header'
 
1093
                );
 
1094
 
 
1095
                return $object;
 
1096
        }
 
1097
 
 
1098
        /**
 
1099
         * Insert an attachment & its metadata.
 
1100
         *
 
1101
         * @param array $object Attachment object.
 
1102
         * @param string $cropped Cropped image URL.
 
1103
         *
 
1104
         * @return int Attachment ID.
 
1105
         */
 
1106
        final public function insert_attachment( $object, $cropped ) {
 
1107
                $attachment_id = wp_insert_attachment( $object, $cropped );
 
1108
                $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
 
1109
                /**
 
1110
                 * Allows us to insert custom meta data for an attachment.
 
1111
                 *
 
1112
                 */
 
1113
                $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata );
 
1114
                wp_update_attachment_metadata( $attachment_id, $metadata );
 
1115
                return $attachment_id;
 
1116
        }
 
1117
 
 
1118
        /**
 
1119
         * Gets attachment uploaded by Media Manager, crops it, then saves it as a
 
1120
         * new object. Returns JSON-encoded object details.
 
1121
         */
 
1122
        function ajax_header_crop() {
 
1123
                check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' );
 
1124
 
 
1125
                if ( ! current_user_can( 'edit_theme_options' ) ) {
 
1126
                        wp_send_json_error();
 
1127
                }
 
1128
 
 
1129
                if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
 
1130
                        wp_send_json_error();
 
1131
                }
 
1132
 
 
1133
                $crop_details = $_POST['cropDetails'];
 
1134
 
 
1135
                $dimensions = $this->get_header_dimensions( array(
 
1136
                        'height' => $crop_details['height'],
 
1137
                        'width'  => $crop_details['width'],
 
1138
                ) );
 
1139
 
 
1140
                $attachment_id = absint( $_POST['id'] );
 
1141
 
 
1142
                $cropped = wp_crop_image(
 
1143
                        $attachment_id,
 
1144
                        (int) $crop_details['x1'],
 
1145
                        (int) $crop_details['y1'],
 
1146
                        (int) $crop_details['width'],
 
1147
                        (int) $crop_details['height'],
 
1148
                        (int) $dimensions['dst_width'],
 
1149
                        (int) $dimensions['dst_height']
 
1150
                );
 
1151
 
 
1152
                if ( ! $cropped || is_wp_error( $cropped ) ) {
 
1153
                        wp_send_json_error( array( 'message' => __( 'Image could not be processed. Please go back and try again.' ) ) );
 
1154
                }
 
1155
 
 
1156
                $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication
 
1157
 
 
1158
                $object = $this->create_attachment_object( $cropped, $attachment_id );
 
1159
 
 
1160
                unset( $object['ID'] );
 
1161
 
 
1162
                $new_attachment_id = $this->insert_attachment( $object, $cropped );
 
1163
 
 
1164
                $object['attachment_id'] = $new_attachment_id;
 
1165
                $object['width']         = $dimensions['dst_width'];
 
1166
                $object['height']        = $dimensions['dst_height'];
 
1167
 
 
1168
                wp_send_json_success( $object );
 
1169
        }
 
1170
 
 
1171
        /**
 
1172
         * Given an attachment ID for a header image, updates its "last used"
 
1173
         * timestamp to now.
 
1174
         *
 
1175
         * Triggered when the user tries adds a new header image from the
 
1176
         * Media Manager, even if s/he doesn't save that change.
 
1177
         */
 
1178
        function ajax_header_add() {
 
1179
                check_ajax_referer( 'header-add', 'nonce' );
 
1180
 
 
1181
                if ( ! current_user_can( 'edit_theme_options' ) ) {
 
1182
                        wp_send_json_error();
 
1183
                }
 
1184
 
 
1185
                $attachment_id = absint( $_POST['attachment_id'] );
 
1186
                if ( $attachment_id < 1 ) {
 
1187
                        wp_send_json_error();
 
1188
                }
 
1189
 
 
1190
                $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
 
1191
                update_post_meta( $attachment_id, $key, time() );
 
1192
                update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
 
1193
 
 
1194
                wp_send_json_success();
 
1195
        }
 
1196
 
 
1197
        /**
 
1198
         * Given an attachment ID for a header image, unsets it as a user-uploaded
 
1199
         * header image for the current theme.
 
1200
         *
 
1201
         * Triggered when the user clicks the overlay "X" button next to each image
 
1202
         * choice in the Customizer's Header tool.
 
1203
         */
 
1204
        function ajax_header_remove() {
 
1205
                check_ajax_referer( 'header-remove', 'nonce' );
 
1206
 
 
1207
                if ( ! current_user_can( 'edit_theme_options' ) ) {
 
1208
                        wp_send_json_error();
 
1209
                }
 
1210
 
 
1211
                $attachment_id = absint( $_POST['attachment_id'] );
 
1212
                if ( $attachment_id < 1 ) {
 
1213
                        wp_send_json_error();
 
1214
                }
 
1215
 
 
1216
                $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
 
1217
                delete_post_meta( $attachment_id, $key );
 
1218
                delete_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
 
1219
 
 
1220
                wp_send_json_success();
 
1221
        }
 
1222
 
 
1223
        function customize_set_last_used( $wp_customize ) {
 
1224
                $data = $wp_customize->get_setting( 'header_image_data' )->post_value();
 
1225
 
 
1226
                if ( ! isset( $data['attachment_id'] ) ) {
 
1227
                        return;
 
1228
                }
 
1229
 
 
1230
                $attachment_id = $data['attachment_id'];
 
1231
                $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
 
1232
                update_post_meta( $attachment_id, $key, time() );
 
1233
        }
 
1234
 
 
1235
        public function get_default_header_images() {
 
1236
                $this->process_default_headers();
 
1237
 
 
1238
                // Get the default image if there is one.
 
1239
                $default = get_theme_support( 'custom-header', 'default-image' );
 
1240
 
 
1241
                if ( ! $default ) { // If not,
 
1242
                        return $this->default_headers; // easy peasy.
 
1243
                }
 
1244
 
 
1245
                $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
 
1246
                $already_has_default = false;
 
1247
 
 
1248
                foreach ( $this->default_headers as $k => $h ) {
 
1249
                        if ( $h['url'] === $default ) {
 
1250
                                $already_has_default = true;
 
1251
                                break;
 
1252
                        }
 
1253
                }
 
1254
 
 
1255
                if ( $already_has_default ) {
 
1256
                        return $this->default_headers;
 
1257
                }
 
1258
 
 
1259
                // If the one true image isn't included in the default set, prepend it.
 
1260
                $header_images = array();
 
1261
                $header_images['default'] = array(
 
1262
                        'url'           => $default,
 
1263
                        'thumbnail_url' => $default,
 
1264
                        'description'   => 'Default'
 
1265
                );
 
1266
 
 
1267
                // The rest of the set comes after.
 
1268
                $header_images = array_merge( $header_images, $this->default_headers );
 
1269
                return $header_images;
 
1270
        }
 
1271
 
 
1272
        public function get_uploaded_header_images() {
 
1273
                $header_images = get_uploaded_header_images();
 
1274
                $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
 
1275
                $alt_text_key = '_wp_attachment_image_alt';
 
1276
 
 
1277
                foreach ( $header_images as &$header_image ) {
 
1278
                        $header_meta = get_post_meta( $header_image['attachment_id'] );
 
1279
                        $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
 
1280
                        $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
 
1281
                }
 
1282
 
 
1283
                return $header_images;
 
1284
        }
1043
1285
}