SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | - | if (!class_exists("Tag_Images")) { |
2 | + | /** |
3 | - | class Tag_Images |
3 | + | * Plugin class |
4 | - | { |
4 | + | **/ |
5 | - | public function __construct() |
5 | + | if ( ! class_exists( 'TAG_IMAGES' ) ) { |
6 | - | { |
6 | + | |
7 | - | // |
7 | + | class TAG_IMAGES{ |
8 | - | } |
8 | + | |
9 | public function __construct() { | |
10 | - | /** |
10 | + | // |
11 | - | * Initialize the class and start calling our hooks and filters |
11 | + | } |
12 | - | */ |
12 | + | |
13 | - | public function init() |
13 | + | /* |
14 | - | { |
14 | + | * Initialize the class and start calling our hooks and filters |
15 | - | // Image actions |
15 | + | * @since 1.0.0 |
16 | - | add_action( |
16 | + | */ |
17 | - | "tag_add_form_fields", |
17 | + | public function init() { |
18 | - | [$this, "add_tag_image"], |
18 | + | add_action( 'post_tag_add_form_fields', array ( $this, 'add_post_tag_image' ), 10, 2 ); |
19 | - | 10, |
19 | + | add_action( 'created_post_tag', array ( $this, 'save_post_tag_image' ), 10, 2 ); |
20 | - | 2 |
20 | + | add_action( 'post_tag_edit_form_fields', array ( $this, 'update_post_tag_image' ), 10, 2 ); |
21 | - | ); |
21 | + | add_action( 'edited_post_tag', array ( $this, 'updated_post_tag_image' ), 10, 2 ); |
22 | - | add_action("created_tag", [$this, "save_tag_image"], 10, 2); |
22 | + | add_action( 'admin_enqueue_scripts', array( $this, 'load_media' ) ); |
23 | - | add_action( |
23 | + | add_action( 'admin_footer', array ( $this, 'add_script' ) ); |
24 | - | "tag_edit_form_fields", |
24 | + | } |
25 | - | [$this, "update_tag_image"], |
25 | + | |
26 | - | 10, |
26 | + | public function load_media() { |
27 | - | 2 |
27 | + | wp_enqueue_media(); |
28 | - | ); |
28 | + | } |
29 | - | add_action( |
29 | + | |
30 | - | "edited_tag", |
30 | + | /* |
31 | - | [$this, "updated_tag_image"], |
31 | + | * Add a form field in the new post_tag page |
32 | - | 10, |
32 | + | * @since 1.0.0 |
33 | - | 2 |
33 | + | */ |
34 | - | ); |
34 | + | public function add_post_tag_image ( $taxonomy ) { ?> |
35 | - | add_action("admin_enqueue_scripts", [$this, "load_media"]); |
35 | + | <div class="form-field term-group"> |
36 | - | add_action("admin_footer", [$this, "add_script"]); |
36 | + | <label for="post_tag-image-id"><?php _e('Image', 'hero-theme'); ?></label> |
37 | - | } |
37 | + | <input type="hidden" id="post_tag-image-id" name="post_tag-image-id" class="custom_media_url" value=""> |
38 | <div id="post_tag-image-wrapper"></div> | |
39 | - | public function load_media() |
39 | + | <p> |
40 | - | { |
40 | + | <input type="button" class="button button-secondary ct_tax_media_button" id="ct_tax_media_button" name="ct_tax_media_button" value="<?php _e( 'Add Image', 'hero-theme' ); ?>" /> |
41 | - | if (!isset($_GET["taxonomy"]) || $_GET["taxonomy"] != "post_tag") { |
41 | + | <input type="button" class="button button-secondary ct_tax_media_remove" id="ct_tax_media_remove" name="ct_tax_media_remove" value="<?php _e( 'Remove Image', 'hero-theme' ); ?>" /> |
42 | - | return; |
42 | + | </p> |
43 | </div> | |
44 | - | wp_enqueue_media(); |
44 | + | <?php |
45 | - | } |
45 | + | } |
46 | ||
47 | - | /** |
47 | + | /* |
48 | - | * Add a form field in the new tag page |
48 | + | * Save the form field |
49 | - | * @since 1.0.0 |
49 | + | * @since 1.0.0 |
50 | - | */ |
50 | + | */ |
51 | public function save_post_tag_image ( $term_id, $tt_id ) { | |
52 | - | public function add_tag_image($taxonomy) |
52 | + | if( isset( $_POST['post_tag-image-id'] ) && '' !== $_POST['post_tag-image-id'] ){ |
53 | - | { |
53 | + | $image = $_POST['post_tag-image-id']; |
54 | - | ?> |
54 | + | add_term_meta( $term_id, 'post_tag-image-id', $image, true ); |
55 | - | <div class="form-field term-group"> |
55 | + | } |
56 | - | <label for="image"><?php _e("Image", "showcase"); ?></label> |
56 | + | } |
57 | - | <input type="hidden" id="image" name="image" class="custom_media_url" value=""> |
57 | + | |
58 | - | <div id="tag-image-wrapper"></div> |
58 | + | /* |
59 | - | <p> |
59 | + | * Edit the form field |
60 | - | <input type="button" class="button button-secondary showcase_tax_media_button" id="showcase_tax_media_button" name="showcase_tax_media_button" value="<?php _e( |
60 | + | * @since 1.0.0 |
61 | - | "Add Image", |
61 | + | */ |
62 | - | "showcase" |
62 | + | public function update_post_tag_image ( $term, $taxonomy ) { ?> |
63 | - | ); ?>" /> |
63 | + | <tr class="form-field term-group-wrap"> |
64 | - | <input type="button" class="button button-secondary showcase_tax_media_remove" id="showcase_tax_media_remove" name="showcase_tax_media_remove" value="<?php _e( |
64 | + | <th scope="row"> |
65 | - | "Remove Image", |
65 | + | <label for="post_tag-image-id"><?php _e( 'Image', 'hero-theme' ); ?></label> |
66 | - | "showcase" |
66 | + | </th> |
67 | - | ); ?>" /> |
67 | + | <td> |
68 | - | </p> |
68 | + | <?php $image_id = get_term_meta ( $term -> term_id, 'post_tag-image-id', true ); ?> |
69 | <input type="hidden" id="post_tag-image-id" name="post_tag-image-id" value="<?php echo $image_id; ?>"> | |
70 | - | <?php |
70 | + | <div id="post_tag-image-wrapper"> |
71 | - | } |
71 | + | <?php if ( $image_id ) { ?> |
72 | <?php echo wp_get_attachment_image ( $image_id, 'thumbnail' ); ?> | |
73 | - | /** |
73 | + | <?php } ?> |
74 | - | * Save the form field |
74 | + | |
75 | - | * @since 1.0.0 |
75 | + | <p> |
76 | - | */ |
76 | + | <input type="button" class="button button-secondary ct_tax_media_button" id="ct_tax_media_button" name="ct_tax_media_button" value="<?php _e( 'Add Image', 'hero-theme' ); ?>" /> |
77 | - | public function save_tag_image($term_id, $tt_id) |
77 | + | <input type="button" class="button button-secondary ct_tax_media_remove" id="ct_tax_media_remove" name="ct_tax_media_remove" value="<?php _e( 'Remove Image', 'hero-theme' ); ?>" /> |
78 | - | { |
78 | + | </p> |
79 | - | if (isset($_POST["image"]) && "" !== $_POST["image"]) { |
79 | + | </td> |
80 | - | add_term_meta($term_id, "image", absint($_POST["image"]), true); |
80 | + | </tr> |
81 | <?php | |
82 | - | } |
82 | + | } |
83 | ||
84 | - | /** |
84 | + | /* |
85 | - | * Edit the form field |
85 | + | * Update the form field value |
86 | - | * @since 1.0.0 |
86 | + | * @since 1.0.0 |
87 | - | */ |
87 | + | */ |
88 | - | public function update_tag_image($term, $taxonomy) |
88 | + | public function updated_post_tag_image ( $term_id, $tt_id ) { |
89 | - | { |
89 | + | if( isset( $_POST['post_tag-image-id'] ) && '' !== $_POST['post_tag-image-id'] ){ |
90 | - | ?> |
90 | + | $image = $_POST['post_tag-image-id']; |
91 | - | <tr class="form-field term-group-wrap"> |
91 | + | update_term_meta ( $term_id, 'post_tag-image-id', $image ); |
92 | - | <th scope="row"> |
92 | + | } else { |
93 | - | <label for="image"><?php _e("Image", "showcase"); ?></label> |
93 | + | update_term_meta ( $term_id, 'post_tag-image-id', '' ); |
94 | - | </th> |
94 | + | } |
95 | - | <td> |
95 | + | } |
96 | - | <?php $image_id = get_term_meta($term->term_id, "image", true); ?> |
96 | + | |
97 | - | <input type="hidden" id="image" name="image" value="<?php echo esc_attr( |
97 | + | /* |
98 | - | $image_id |
98 | + | * Add script |
99 | - | ); ?>"> |
99 | + | * @since 1.0.0 |
100 | - | <div id="tag-image-wrapper"> |
100 | + | */ |
101 | - | <?php if ($image_id) { ?> |
101 | + | public function add_script() { ?> |
102 | - | <?php echo wp_get_attachment_image($image_id, "thumbnail"); ?> |
102 | + | <script> |
103 | - | <?php } ?> |
103 | + | jQuery(document).ready( function($) { |
104 | - | </div> |
104 | + | function ct_media_upload(button_class) { |
105 | - | <p> |
105 | + | var _custom_media = true, |
106 | - | <input type="button" class="button button-secondary showcase_tax_media_button" id="showcase_tax_media_button" name="showcase_tax_media_button" value="<?php _e( |
106 | + | _orig_send_attachment = wp.media.editor.send.attachment; |
107 | - | "Add Image", |
107 | + | $('body').on('click', button_class, function(e) { |
108 | - | "showcase" |
108 | + | var button_id = '#'+$(this).attr('id'); |
109 | - | ); ?>" /> |
109 | + | var send_attachment_bkp = wp.media.editor.send.attachment; |
110 | - | <input type="button" class="button button-secondary showcase_tax_media_remove" id="showcase_tax_media_remove" name="showcase_tax_media_remove" value="<?php _e( |
110 | + | var button = $(button_id); |
111 | - | "Remove Image", |
111 | + | _custom_media = true; |
112 | - | "showcase" |
112 | + | wp.media.editor.send.attachment = function(props, attachment){ |
113 | - | ); ?>" /> |
113 | + | if ( _custom_media ) { |
114 | - | </p> |
114 | + | $('#post_tag-image-id').val(attachment.id); |
115 | - | </td> |
115 | + | $('#post_tag-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />'); |
116 | - | </tr> |
116 | + | $('#post_tag-image-wrapper .custom_media_image').attr('src',attachment.url).css('display','block'); |
117 | - | <?php |
117 | + | } else { |
118 | - | } |
118 | + | return _orig_send_attachment.apply( button_id, [props, attachment] ); |
119 | } | |
120 | - | /** |
120 | + | |
121 | - | * Update the form field value |
121 | + | wp.media.editor.open(button); |
122 | - | * @since 1.0.0 |
122 | + | return false; |
123 | - | */ |
123 | + | }); |
124 | - | public function updated_tag_image($term_id, $tt_id) |
124 | + | } |
125 | - | { |
125 | + | ct_media_upload('.ct_tax_media_button.button'); |
126 | - | if (isset($_POST["image"]) && "" !== $_POST["image"]) { |
126 | + | $('body').on('click','.ct_tax_media_remove',function(){ |
127 | - | update_term_meta($term_id, "image", absint($_POST["image"])); |
127 | + | $('#post_tag-image-id').val(''); |
128 | - | } else { |
128 | + | $('#post_tag-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />'); |
129 | - | update_term_meta($term_id, "image", ""); |
129 | + | }); |
130 | // Thanks: http://stackoverflow.com/questions/15281995/wordpress-create-post_tag-ajax-response | |
131 | - | } |
131 | + | $(document).ajaxComplete(function(event, xhr, settings) { |
132 | var queryStringArr = settings.data.split('&'); | |
133 | - | /** |
133 | + | if( $.inArray('action=add-tag', queryStringArr) !== -1 ){ |
134 | - | * Enqueue styles and scripts |
134 | + | var xml = xhr.responseXML; |
135 | - | * @since 1.0.0 |
135 | + | $response = $(xml).find('term_id').text(); |
136 | - | */ |
136 | + | if($response!=""){ |
137 | - | public function add_script() |
137 | + | // Clear the thumb image |
138 | - | { |
138 | + | $('#post_tag-image-wrapper').html(''); |
139 | - | if (!isset($_GET["taxonomy"]) || $_GET["taxonomy"] != "post_tag") { |
139 | + | |
140 | - | return; |
140 | + | } |
141 | - | } ?> |
141 | + | }); |
142 | - | <script> jQuery(document).ready( function($) { |
142 | + | }); |
143 | - | _wpMediaViewsL10n.insertIntoPost = '<?php _e( |
143 | + | </script> |
144 | - | "Insert", |
144 | + | <?php } |
145 | - | "showcase" |
145 | + | |
146 | - | ); ?>'; |
146 | + | } |
147 | - | function ct_media_upload(button_class) { |
147 | + | |
148 | - | var _custom_media = true, _orig_send_attachment = wp.media.editor.send.attachment; |
148 | + | $TAG_IMAGES = new TAG_IMAGES(); |
149 | - | $('body').on('click', button_class, function(e) { |
149 | + | $TAG_IMAGES -> init(); |
150 | - | var button_id = '#'+$(this).attr('id'); |
150 | + | |
151 | - | var send_attachment_bkp = wp.media.editor.send.attachment; |
151 | + |