Make WordPress Core

Ticket #22868: media-bug.php

File media-bug.php, 1.9 KB (added by geminorum, 12 years ago)

sample plugin

Line 
1<?php
2/*
3Plugin Name: 3.5 Media Bug
4Version: 0.1
5Plugin URI:
6Description:
7Author: geminorum
8*/
9
10function gtheme_images_attachment_fields_to_edit( $fields, $post ){
11        if ( ! isset( $_REQUEST['post_id'] ) )
12                return $fields;
13       
14        $sizes = array( 'dashboard', 'single' );
15        $images = get_post_meta( $_REQUEST['post_id'], '_gtheme_images', true );
16        if ( ! is_array( $images ) ) 
17                $images = array();
18
19        $html = '<div>';       
20        foreach( $sizes as $size ) {
21                if ( isset( $images[$size] ) && $images[$size] == $post->ID )
22                        $checked = ' checked="checked"';
23                else
24                        $checked = '';
25
26                $html .= '<label>'.$size.' <input type="checkbox" value="'.$size
27                        .'" id="gtheme-size-'.$post->ID.'-'.$size
28                        .'" name="gtheme-size-'.$post->ID.'-'.$size
29                        .'" '.$checked.' ></label>';
30        }
31        $html .= '</div>';
32       
33        return array_merge( $fields, array( 'all_tags' => array ( 
34                'label' => __( 'Extra Tags', 'gtheme' ),
35                'input' => 'html',
36                'html' => $html ) ) );
37} 
38
39function gtheme_images_attachment_fields_to_save( $post, $attachment ) {
40        if ( ! isset( $_REQUEST['post_id'] ) )
41                return $post;
42       
43        $sizes = array( 'dashboard', 'single' );       
44        $images = $striped = array();
45        $saved_images = get_post_meta( $_REQUEST['post_id'], '_gtheme_images', true );
46        if ( ! is_array( $saved_images ) )
47                $saved_images = array();
48       
49        foreach( $sizes as $size )
50                if ( isset( $_POST['gtheme-size-'.$post['ID'].'-'.$size] ) ) 
51                        $images[$size] = $post['ID'];
52       
53        foreach( $saved_images as $saved_size => $saved_id )
54                if ( $post['ID'] != $saved_id )
55                        $striped[$saved_size] = $saved_id;
56                       
57        $final = array_merge( $striped, $images );
58                       
59        if ( count( $final ) )
60                update_post_meta( $_REQUEST['post_id'], '_gtheme_images', $final );
61        else
62                delete_post_meta( $_REQUEST['post_id'], '_gtheme_images' );
63               
64    return $post;
65} 
66
67add_filter( 'attachment_fields_to_edit', 'gtheme_images_attachment_fields_to_edit', 10, 2 );
68add_filter( 'attachment_fields_to_save', 'gtheme_images_attachment_fields_to_save', 10 ,2 );