| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; // Exit if accessed directly. |
| 5 |
} |
| 6 |
|
| 7 |
if (!class_exists('Ultimate_Post_Kit_Category_Image')) { |
| 8 |
class Ultimate_Post_Kit_Category_Image { |
| 9 |
public function __construct() { |
| 10 |
add_action('category_add_form_fields', [$this, 'add_new_category_image'], 10, 2); |
| 11 |
add_action('created_category', [$this, 'save_category_image'], 10, 2); |
| 12 |
add_action('category_edit_form_fields', [$this, 'edit_category_image'], 10, 2); |
| 13 |
add_action('edited_category', [$this, 'updated_category_image'], 10, 2); |
| 14 |
add_action('admin_enqueue_scripts', [$this, 'load_media_files']); |
| 15 |
add_action('admin_footer', [$this, 'load_category_media_scripts']); |
| 16 |
} |
| 17 |
|
| 18 |
public function add_new_category_image() { ?> |
| 19 |
<div class="form-field term-group-wrap"> |
| 20 |
<label for="upk-category-image-id"><?php esc_html_e('Image', 'ultimate-post-kit'); ?></label> |
| 21 |
<input type="hidden" id="upk-category-image-id" name="upk-category-image-id" class="upk-hidden-media-url" value=""> |
| 22 |
<div id="upk-category-image-wrapper"> |
| 23 |
</div> |
| 24 |
<p> |
| 25 |
<input type="button" class="button button-secondary upk-category-image-add" id="upk-category-image-add" name="upk-category-image-add" value="<?php esc_html_e('Add Image', 'ultimate-post-kit'); ?>" /> |
| 26 |
<input type="button" class="button button-secondary upk-category-image-remove" id="upk-category-image-remove" name="upk-category-image-remove" value="<?php esc_html_e('Remove Image', 'ultimate-post-kit'); ?>" /> |
| 27 |
</p> |
| 28 |
</div> |
| 29 |
<?php |
| 30 |
} |
| 31 |
public function save_category_image($term_id) { |
| 32 |
if (!current_user_can('manage_categories')) { |
| 33 |
return; |
| 34 |
} |
| 35 |
if (!isset($_POST['_wpnonce_add-tag']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce_add-tag'])), 'add-tag')) { |
| 36 |
return; |
| 37 |
} |
| 38 |
if (isset($_POST['upk-category-image-id']) && '' !== $_POST['upk-category-image-id']) { |
| 39 |
$image = sanitize_key(wp_unslash($_POST['upk-category-image-id'])); |
| 40 |
add_term_meta($term_id, 'upk-category-image-id', $image, true); |
| 41 |
} |
| 42 |
} |
| 43 |
public function edit_category_image($term) { ?> |
| 44 |
<tr class="form-field term-group-wrap"> |
| 45 |
<th scope="row"> |
| 46 |
<label for="upk-category-image-id"><?php esc_html_e('Image', 'ultimate-post-kit'); ?></label> |
| 47 |
</th> |
| 48 |
<td> |
| 49 |
<?php $image_id = get_term_meta($term->term_id, 'upk-category-image-id', true); ?> |
| 50 |
<input type="hidden" id="upk-category-image-id" name="upk-category-image-id" value="<?php echo esc_attr($image_id); ?>"> |
| 51 |
<div id="upk-category-image-wrapper"> |
| 52 |
<?php if ($image_id) { ?> |
| 53 |
<?php echo wp_get_attachment_image($image_id, 'thumbnail'); ?> |
| 54 |
<?php } ?> |
| 55 |
</div> |
| 56 |
<p> |
| 57 |
<input type="button" class="button button-secondary upk-category-image-add" id="upk-category-image-add" name="upk-category-image-add" value="<?php esc_html_e('Add Image', 'ultimate-post-kit'); ?>" /> |
| 58 |
<input type="button" class="button button-secondary upk-category-image-remove" id="upk-category-image-remove" name="upk-category-image-remove" value="<?php esc_html_e('Remove Image', 'ultimate-post-kit'); ?>" /> |
| 59 |
</p> |
| 60 |
</td> |
| 61 |
</tr> |
| 62 |
<?php |
| 63 |
} |
| 64 |
|
| 65 |
public function updated_category_image($term_id) { |
| 66 |
if (!current_user_can('manage_categories')) { |
| 67 |
return; |
| 68 |
} |
| 69 |
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'update-tag_' . $term_id)) { |
| 70 |
return; |
| 71 |
} |
| 72 |
if (isset($_POST['upk-category-image-id']) && '' !== $_POST['upk-category-image-id']) { |
| 73 |
$image = sanitize_key(wp_unslash($_POST['upk-category-image-id'])); |
| 74 |
update_term_meta($term_id, 'upk-category-image-id', $image); |
| 75 |
} else { |
| 76 |
update_term_meta($term_id, 'upk-category-image-id', ''); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public function load_media_files() { |
| 81 |
wp_enqueue_media(); |
| 82 |
} |
| 83 |
|
| 84 |
public function load_category_media_scripts() { ?> |
| 85 |
<script> |
| 86 |
(function($) { |
| 87 |
$(document).ready(function() { |
| 88 |
$(".upk-category-image-add.button").on("click", function() { |
| 89 |
let UPK = wp.media({ |
| 90 |
multiple: false |
| 91 |
}); |
| 92 |
UPK.on('select', function() { |
| 93 |
let attachment = UPK.state().get('selection').first().toJSON(); |
| 94 |
$("#upk-category-image-id").val(attachment.id); |
| 95 |
$("#upk-category-image-url").val(attachment.url); |
| 96 |
$("#upk-category-image-wrapper").html(`<img width="150" height="150" src='${attachment.url}' />`); |
| 97 |
}); |
| 98 |
UPK.open(); |
| 99 |
return false; |
| 100 |
}); |
| 101 |
$('.upk-category-image-remove.button').on("click", function() { |
| 102 |
$('#upk-category-image-id').val(''); |
| 103 |
$('#upk-category-image-wrapper').html('<img width=150; height=150; class="upk-media-hidden-image" src="" style="margin:0; max-height:100px; padding:0;float:none;" />'); |
| 104 |
}) |
| 105 |
}); |
| 106 |
})(jQuery); |
| 107 |
</script> |
| 108 |
<?php } |
| 109 |
} |
| 110 |
|
| 111 |
new Ultimate_Post_Kit_Category_Image(); |
| 112 |
} |
| 113 |
|