| 1004 | | global $_wp_additional_image_sizes; |
| 1005 | | |
| 1006 | | ?><script type="text/javascript"> |
| 1007 | | jQuery( function($) { |
| 1008 | | var $element = $('#select-featured-image'), |
| 1009 | | $thumbnailId = $element.find('input[name="thumbnail_id"]'), |
| 1010 | | title = '<?php _e( "Choose a Featured Image" ); ?>', |
| 1011 | | update = '<?php _e( "Update Featured Image" ); ?>', |
| 1012 | | Attachment = wp.media.model.Attachment, |
| 1013 | | frame, setFeaturedImage; |
| 1014 | | |
| 1015 | | setFeaturedImage = function( thumbnailId ) { |
| 1016 | | var selection; |
| 1017 | | |
| 1018 | | $element.find('img').remove(); |
| 1019 | | $element.toggleClass( 'has-featured-image', -1 != thumbnailId ); |
| 1020 | | $thumbnailId.val( thumbnailId ); |
| 1021 | | |
| 1022 | | if ( frame ) { |
| 1023 | | selection = frame.state('library').get('selection'); |
| 1024 | | |
| 1025 | | if ( -1 === thumbnailId ) |
| 1026 | | selection.clear(); |
| 1027 | | else |
| 1028 | | selection.add( Attachment.get( thumbnailId ) ); |
| 1029 | | } |
| 1030 | | }; |
| 1031 | | |
| 1032 | | $element.on( 'click', '.choose, img', function( event ) { |
| 1033 | | var options, thumbnailId, attachment; |
| 1034 | | |
| 1035 | | event.preventDefault(); |
| 1036 | | |
| 1037 | | if ( frame ) { |
| 1038 | | frame.open(); |
| 1039 | | return; |
| 1040 | | } |
| 1041 | | |
| 1042 | | options = { |
| 1043 | | title: title, |
| 1044 | | library: { |
| 1045 | | type: 'image' |
| 1046 | | } |
| 1047 | | }; |
| 1048 | | |
| 1049 | | thumbnailId = $thumbnailId.val(); |
| 1050 | | if ( '' !== thumbnailId && -1 !== thumbnailId ) { |
| 1051 | | attachment = Attachment.get( thumbnailId ); |
| 1052 | | attachment.fetch(); |
| 1053 | | options.selection = [ attachment ]; |
| 1054 | | } |
| 1055 | | |
| 1056 | | frame = wp.media( options ); |
| 1057 | | |
| 1058 | | frame.state('library').set( 'filterable', 'uploaded' ); |
| 1059 | | |
| 1060 | | frame.toolbar.on( 'activate:select', function() { |
| 1061 | | frame.toolbar.view().set({ |
| 1062 | | select: { |
| 1063 | | style: 'primary', |
| 1064 | | text: update, |
| 1065 | | |
| 1066 | | click: function() { |
| 1067 | | var selection = frame.state().get('selection'), |
| 1068 | | model = selection.first(), |
| 1069 | | sizes = model.get('sizes'), |
| 1070 | | size; |
| 1071 | | |
| 1072 | | setFeaturedImage( model.id ); |
| 1073 | | |
| 1074 | | // @todo: might need a size hierarchy equivalent. |
| 1075 | | if ( sizes ) |
| 1076 | | size = sizes['post-thumbnail'] || sizes.medium; |
| 1077 | | |
| 1078 | | // @todo: Need a better way of accessing full size |
| 1079 | | // data besides just calling toJSON(). |
| 1080 | | size = size || model.toJSON(); |
| 1081 | | |
| 1082 | | frame.close(); |
| 1083 | | |
| 1084 | | $( '<img />', { |
| 1085 | | src: size.url, |
| 1086 | | width: size.width |
| 1087 | | }).prependTo( $element ); |
| 1088 | | } |
| 1089 | | } |
| 1090 | | }); |
| 1091 | | }); |
| 1092 | | |
| 1093 | | frame.toolbar.mode('select'); |
| 1094 | | }); |
| 1095 | | |
| 1096 | | $element.on( 'click', '.remove', function( event ) { |
| 1097 | | event.preventDefault(); |
| 1098 | | setFeaturedImage( -1 ); |
| 1099 | | }); |
| 1100 | | }); |
| 1101 | | </script> |
| 1102 | | |
| 1103 | | <?php |
| 1104 | | $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true ); |
| 1105 | | $thumbnail_size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : 'medium'; |
| 1106 | | $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $thumbnail_size ); |
| 1107 | | |
| 1108 | | $classes = empty( $thumbnail_id ) ? '' : 'has-featured-image'; |
| 1109 | | |
| 1110 | | ?><div id="select-featured-image" |
| 1111 | | class="<?php echo esc_attr( $classes ); ?>" |
| 1112 | | data-post-id="<?php echo esc_attr( $post->ID ); ?>"> |
| 1113 | | <?php echo $thumbnail_html; ?> |
| 1114 | | <input type="hidden" name="thumbnail_id" value="<?php echo esc_attr( $thumbnail_id ); ?>" /> |
| 1115 | | <a href="#" class="choose button-secondary"><?php _e( 'Choose a Featured Image' ); ?></a> |
| 1116 | | <a href="#" class="remove"><?php _e( 'Remove Featured Image' ); ?></a> |
| 1117 | | </div> |
| 1118 | | <?php |
| | 1004 | $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true ); |
| | 1005 | echo _wp_post_thumbnail_html( $thumbnail_id, $post->ID ); |