Advertisement
Guest User

Untitled

a guest
Sep 4th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. <?php
  2. if (!class_exists("Tag_Images")) {
  3. class Tag_Images
  4. {
  5. public function __construct()
  6. {
  7. //
  8. }
  9.  
  10. /**
  11. * Initialize the class and start calling our hooks and filters
  12. */
  13. public function init()
  14. {
  15. // Image actions
  16. add_action(
  17. "tag_add_form_fields",
  18. [$this, "add_tag_image"],
  19. 10,
  20. 2
  21. );
  22. add_action("created_tag", [$this, "save_tag_image"], 10, 2);
  23. add_action(
  24. "tag_edit_form_fields",
  25. [$this, "update_tag_image"],
  26. 10,
  27. 2
  28. );
  29. add_action(
  30. "edited_tag",
  31. [$this, "updated_tag_image"],
  32. 10,
  33. 2
  34. );
  35. add_action("admin_enqueue_scripts", [$this, "load_media"]);
  36. add_action("admin_footer", [$this, "add_script"]);
  37. }
  38.  
  39. public function load_media()
  40. {
  41. if (!isset($_GET["taxonomy"]) || $_GET["taxonomy"] != "post_tag") {
  42. return;
  43. }
  44. wp_enqueue_media();
  45. }
  46.  
  47. /**
  48. * Add a form field in the new tag page
  49. * @since 1.0.0
  50. */
  51.  
  52. public function add_tag_image($taxonomy)
  53. {
  54. ?>
  55. <div class="form-field term-group">
  56. <label for="image"><?php _e("Image", "showcase"); ?></label>
  57. <input type="hidden" id="image" name="image" class="custom_media_url" value="">
  58. <div id="tag-image-wrapper"></div>
  59. <p>
  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(
  61. "Add Image",
  62. "showcase"
  63. ); ?>" />
  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(
  65. "Remove Image",
  66. "showcase"
  67. ); ?>" />
  68. </p>
  69. </div>
  70. <?php
  71. }
  72.  
  73. /**
  74. * Save the form field
  75. * @since 1.0.0
  76. */
  77. public function save_tag_image($term_id, $tt_id)
  78. {
  79. if (isset($_POST["image"]) && "" !== $_POST["image"]) {
  80. add_term_meta($term_id, "image", absint($_POST["image"]), true);
  81. }
  82. }
  83.  
  84. /**
  85. * Edit the form field
  86. * @since 1.0.0
  87. */
  88. public function update_tag_image($term, $taxonomy)
  89. {
  90. ?>
  91. <tr class="form-field term-group-wrap">
  92. <th scope="row">
  93. <label for="image"><?php _e("Image", "showcase"); ?></label>
  94. </th>
  95. <td>
  96. <?php $image_id = get_term_meta($term->term_id, "image", true); ?>
  97. <input type="hidden" id="image" name="image" value="<?php echo esc_attr(
  98. $image_id
  99. ); ?>">
  100. <div id="tag-image-wrapper">
  101. <?php if ($image_id) { ?>
  102. <?php echo wp_get_attachment_image($image_id, "thumbnail"); ?>
  103. <?php } ?>
  104. </div>
  105. <p>
  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(
  107. "Add Image",
  108. "showcase"
  109. ); ?>" />
  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(
  111. "Remove Image",
  112. "showcase"
  113. ); ?>" />
  114. </p>
  115. </td>
  116. </tr>
  117. <?php
  118. }
  119.  
  120. /**
  121. * Update the form field value
  122. * @since 1.0.0
  123. */
  124. public function updated_tag_image($term_id, $tt_id)
  125. {
  126. if (isset($_POST["image"]) && "" !== $_POST["image"]) {
  127. update_term_meta($term_id, "image", absint($_POST["image"]));
  128. } else {
  129. update_term_meta($term_id, "image", "");
  130. }
  131. }
  132.  
  133. /**
  134. * Enqueue styles and scripts
  135. * @since 1.0.0
  136. */
  137. public function add_script()
  138. {
  139. if (!isset($_GET["taxonomy"]) || $_GET["taxonomy"] != "post_tag") {
  140. return;
  141. } ?>
  142. <script> jQuery(document).ready( function($) {
  143. _wpMediaViewsL10n.insertIntoPost = '<?php _e(
  144. "Insert",
  145. "showcase"
  146. ); ?>';
  147. function ct_media_upload(button_class) {
  148. var _custom_media = true, _orig_send_attachment = wp.media.editor.send.attachment;
  149. $('body').on('click', button_class, function(e) {
  150. var button_id = '#'+$(this).attr('id');
  151. var send_attachment_bkp = wp.media.editor.send.attachment;
  152. var button = $(button_id);
  153. _custom_media = true;
  154. wp.media.editor.send.attachment = function(props, attachment){
  155. if( _custom_media ) {
  156. $('#image').val(attachment.id);
  157. $('#tag-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
  158. $( '#tag-image-wrapper .custom_media_image' ).attr( 'src',attachment.url ).css( 'display','block' );
  159. } else {
  160. return _orig_send_attachment.apply( button_id, [props, attachment] );
  161. }
  162. }
  163. wp.media.editor.open(button); return false;
  164. });
  165. }
  166. ct_media_upload('.showcase_tax_media_button.button');
  167. $('body').on('click','.showcase_tax_media_remove',function(){
  168. $('#image').val('');
  169. $('#tag-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
  170. });
  171. // Thanks: http://stackoverflow.com/questions/15281995/wordpress-create-tag-ajax-response
  172. $(document).ajaxComplete(function(event, xhr, settings) {
  173. var queryStringArr = settings.data.split('&');
  174. if( $.inArray('action=add-tag', queryStringArr) !== -1 ){
  175. var xml = xhr.responseXML;
  176. $response = $(xml).find('term_id').text();
  177. if($response!=""){
  178. // Clear the thumb image
  179. $('#tag-image-wrapper').html('');
  180. }
  181. }
  182. });
  183. });
  184. </script>
  185. <?php
  186. }
  187. }
  188. $Tag_Images = new Tag_Images();
  189. $Tag_Images->init();
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement