categories-images.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. define('Z_IMAGE_PLACEHOLDER', get_random_bg_url());
  3. add_action('admin_init', 'z_init');
  4. function z_init() {
  5. $z_taxonomies = get_taxonomies();
  6. if (is_array($z_taxonomies)) {
  7. $zci_options = get_option('zci_options');
  8. $excluded_taxonomies = $zci_options['excluded_taxonomies'] ?? [];
  9. foreach ($z_taxonomies as $z_taxonomy) {
  10. if (in_array($z_taxonomy, $excluded_taxonomies))
  11. continue;
  12. add_action($z_taxonomy.'_add_form_fields', 'z_add_texonomy_field');
  13. add_action($z_taxonomy.'_edit_form_fields', 'z_edit_texonomy_field');
  14. add_filter( 'manage_edit-' . $z_taxonomy . '_columns', 'z_taxonomy_columns' );
  15. add_filter( 'manage_' . $z_taxonomy . '_custom_column', 'z_taxonomy_column', 10, 3 );
  16. }
  17. }
  18. }
  19. add_action( 'admin_head', 'z_add_style' );
  20. function z_add_style() {
  21. echo '<style media="screen">
  22. th.column-thumb {width:60px;}
  23. .form-field img.taxonomy-image {border:1px solid #eee;max-width:300px;max-height:300px;}
  24. .inline-edit-row fieldset .thumb label span.title {width:48px;height:48px;border:1px solid #eee;display:inline-block;}
  25. .column-thumb span {width:48px;height:48px;border:1px solid #eee;display:inline-block;}
  26. .inline-edit-row fieldset .thumb img,.column-thumb img {width:48px;height:48px;}
  27. </style>';
  28. }
  29. // 添加在添加窗体中的图像字段
  30. function z_add_texonomy_field() {
  31. if (get_bloginfo('version') >= 3.5) // worpdress 最低版本3.5
  32. wp_enqueue_media();
  33. else {
  34. wp_enqueue_style('thickbox');
  35. wp_enqueue_script('thickbox');
  36. }
  37. echo '<div class="form-field">
  38. <label for="taxonomy_image">' . __('category/tag image', 'sakurairo') /*分类/标签图像*/. '</label>
  39. <input type="text" name="taxonomy_image" id="taxonomy_image" value="" />
  40. <br/>
  41. <button class="z_upload_image_button button">' . __('add image', 'sakurairo') /*添加图像*/. '</button>
  42. </div>'.z_script();
  43. }
  44. // 在编辑窗体中添加图像字段
  45. function z_edit_texonomy_field($taxonomy) {
  46. if (get_bloginfo('version') >= 3.5)
  47. wp_enqueue_media();
  48. else {
  49. wp_enqueue_style('thickbox');
  50. wp_enqueue_script('thickbox');
  51. }
  52. if (z_taxonomy_image_url( $taxonomy->term_id, NULL, TRUE ) == Z_IMAGE_PLACEHOLDER)
  53. $image_url = "";
  54. else
  55. $image_url = z_taxonomy_image_url( $taxonomy->term_id, NULL, TRUE );
  56. echo '<tr class="form-field">
  57. <th scope="row" valign="top"><label for="taxonomy_image">' .__('category/tag image', 'sakurairo') /*分类/标签图像*/. '</label></th>
  58. <td><img class="taxonomy-image" src="' . z_taxonomy_image_url( $taxonomy->term_id, 'medium', TRUE ) . '"/><br/><input type="text" name="taxonomy_image" id="taxonomy_image" value="'.$image_url.'" /><br />
  59. <button class="z_upload_image_button button">' . __('add image', 'sakurairo') /*添加图像*/. '</button>
  60. <button class="z_remove_image_button button">' . __('remove image', 'sakurairo') /*删除图像*/. '</button>
  61. </td>
  62. </tr>'.z_script();
  63. }
  64. // 上传图像
  65. function z_script() {
  66. return '<script type="text/javascript">
  67. jQuery(document).ready(function($) {
  68. var wordpress_ver = "'.get_bloginfo("version").'", upload_button;
  69. $(".z_upload_image_button").click(function(event) {
  70. upload_button = $(this);
  71. var frame;
  72. if (wordpress_ver >= "3.5") {
  73. event.preventDefault();
  74. if (frame) {
  75. frame.open();
  76. return;
  77. }
  78. frame = wp.media();
  79. frame.on( "select", function() {
  80. // Grab the selected attachment.
  81. var attachment = frame.state().get("selection").first();
  82. frame.close();
  83. if (upload_button.parent().prev().children().hasClass("tax_list")) {
  84. upload_button.parent().prev().children().val(attachment.attributes.url);
  85. upload_button.parent().prev().prev().children().attr("src", attachment.attributes.url);
  86. }
  87. else
  88. $("#taxonomy_image").val(attachment.attributes.url);
  89. });
  90. frame.open();
  91. }
  92. else {
  93. tb_show("", "media-upload.php?type=image&amp;TB_iframe=true");
  94. return false;
  95. }
  96. });
  97. $(".z_remove_image_button").click(function() {
  98. $(".taxonomy-image").attr("src", "'.Z_IMAGE_PLACEHOLDER.'");
  99. $("#taxonomy_image").val("");
  100. $(this).parent().siblings(".title").children("img").attr("src","' . Z_IMAGE_PLACEHOLDER . '");
  101. $(".inline-edit-col :input[name=\'taxonomy_image\']").val("");
  102. return false;
  103. });
  104. if (wordpress_ver < "3.5") {
  105. window.send_to_editor = function(html) {
  106. imgurl = $("img",html).attr("src");
  107. if (upload_button.parent().prev().children().hasClass("tax_list")) {
  108. upload_button.parent().prev().children().val(imgurl);
  109. upload_button.parent().prev().prev().children().attr("src", imgurl);
  110. }
  111. else
  112. $("#taxonomy_image").val(imgurl);
  113. tb_remove();
  114. }
  115. }
  116. $(".editinline").click(function() {
  117. var tax_id = $(this).parents("tr").attr("id").substr(4);
  118. var thumb = $("#tag-"+tax_id+" .thumb img").attr("src");
  119. if (thumb != "' . Z_IMAGE_PLACEHOLDER . '") {
  120. $(".inline-edit-col :input[name=\'taxonomy_image\']").val(thumb);
  121. } else {
  122. $(".inline-edit-col :input[name=\'taxonomy_image\']").val("");
  123. }
  124. $(".inline-edit-col .title img").attr("src",thumb);
  125. });
  126. });
  127. </script>';
  128. }
  129. // 添加到钩子
  130. add_action('edit_term','z_save_taxonomy_image');
  131. add_action('create_term','z_save_taxonomy_image');
  132. function z_save_taxonomy_image($term_id) {
  133. if(isset($_POST['taxonomy_image']))
  134. update_option('z_taxonomy_image'.$term_id, $_POST['taxonomy_image'], NULL);
  135. }
  136. // 附件地址
  137. function z_get_attachment_id_by_url($image_src) {
  138. global $wpdb;
  139. $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid = %s", $image_src);
  140. $id = $wpdb->get_var($query);
  141. return (!empty($id)) ? $id : NULL;
  142. }
  143. // 我不知道
  144. function z_taxonomy_image_url($term_id = NULL, $size = 'full', $return_placeholder = FALSE) {
  145. if (!$term_id) {
  146. if (is_category())
  147. $term_id = get_query_var('cat');
  148. elseif (is_tag())
  149. $term_id = get_query_var('tag_id');
  150. elseif (is_tax()) {
  151. $current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
  152. $term_id = $current_term->term_id;
  153. }
  154. }
  155. $taxonomy_image_url = get_option('z_taxonomy_image'.$term_id);
  156. if(!empty($taxonomy_image_url)) {
  157. $attachment_id = z_get_attachment_id_by_url($taxonomy_image_url);
  158. if(!empty($attachment_id)) {
  159. $taxonomy_image_url = wp_get_attachment_image_src($attachment_id, $size);
  160. $taxonomy_image_url = $taxonomy_image_url[0];
  161. }
  162. }
  163. if ($return_placeholder)
  164. return ($taxonomy_image_url != '') ? $taxonomy_image_url : Z_IMAGE_PLACEHOLDER;
  165. else
  166. return $taxonomy_image_url;
  167. }
  168. function z_quick_edit_custom_box($column_name, $screen, $name) {
  169. if ($column_name == 'thumb')
  170. echo '<fieldset>
  171. <div class="thumb inline-edit-col">
  172. <label>
  173. <span class="title"><img src="" alt="暂无"/></span>
  174. <span class="input-text-wrap"><input type="text" name="taxonomy_image" value="" class="tax_list" /></span>
  175. <span class="input-text-wrap">
  176. <button class="z_upload_image_button button">' . __('add image', 'sakurairo') /*添加图像*/. '</button>
  177. <button class="z_remove_image_button button">' . __('remove image', 'sakurairo') /*删除图像*/. '</button>
  178. </span>
  179. </label>
  180. </div>
  181. </fieldset>';
  182. }
  183. /**
  184. * 添加类别管理
  185. */
  186. function z_taxonomy_columns( $columns ) {
  187. $new_columns = array();
  188. if(isset($columns)){
  189. $new_columns['cb'] = $columns['cb'];
  190. unset( $columns['cb'] );
  191. }
  192. $new_columns['thumb'] = __('image', 'sakurairo')/*图像*/;
  193. return array_merge( $new_columns, $columns );
  194. }
  195. /**
  196. * 图像列表
  197. */
  198. function z_taxonomy_column( $columns, $column, $id ) {
  199. if ( $column == 'thumb' )
  200. $columns = '<span><img src="' . z_taxonomy_image_url($id, 'thumbnail', TRUE) . '" alt="' . __('no image', 'sakurairo') /*暂无*/. '" class="wp-post-image" /></span>';
  201. return $columns;
  202. }
  203. // 将“插入帖子”更改为“使用此图片”
  204. function z_change_insert_button_text($safe_text, $text) {
  205. return str_replace("Insert into Post", "Use this image", $text);
  206. }
  207. // 列表图片样式
  208. if ( strpos( $_SERVER['SCRIPT_NAME'], 'edit-tags.php' ) > 0 ) {
  209. add_action('quick_edit_custom_box', 'z_quick_edit_custom_box', 10, 3);
  210. add_filter("attribute_escape", "z_change_insert_button_text", 10, 2);
  211. }