| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/*---------------------------------------------------------*/ |
| 5 |
/* Cusom Taxonomies */ |
| 6 |
/*---------------------------------------------------------*/ |
| 7 |
|
| 8 |
function mbt_taxonomies_init() { |
| 9 |
add_action('init', 'mbt_register_taxonomies'); |
| 10 |
add_filter('parent_file', 'mbt_override_taxonomy_parent_files'); |
| 11 |
add_action('admin_init', 'mbt_taxonomy_images_init'); |
| 12 |
add_action('admin_init', 'mbt_author_priorities_init'); |
| 13 |
add_action('admin_enqueue_scripts', 'mbt_enqueue_taxonomy_js'); |
| 14 |
} |
| 15 |
add_action('mbt_init', 'mbt_taxonomies_init'); |
| 16 |
|
| 17 |
function mbt_enqueue_taxonomy_js() { |
| 18 |
if(!mbt_is_mbt_admin_page()) { return; } |
| 19 |
|
| 20 |
wp_enqueue_script("mbt-taxonomies", plugins_url('js/taxonomies.js', dirname(__FILE__)), array('jquery'), MBT_VERSION); |
| 21 |
} |
| 22 |
|
| 23 |
function mbt_register_taxonomies() |
| 24 |
{ |
| 25 |
register_taxonomy('mbt_author', 'mbt_book', array( |
| 26 |
'hierarchical' => true, |
| 27 |
'labels' => array( |
| 28 |
'name' => __('Authors', 'mybooktable'), |
| 29 |
'singular_name' => __('Author', 'mybooktable'), |
| 30 |
'all_items' => __('All Authors', 'mybooktable'), |
| 31 |
'add_new' => __('Add New', 'mybooktable'), |
| 32 |
'add_new_item' => __('Add New Author', 'mybooktable'), |
| 33 |
'new_item_name' => __('New Author', 'mybooktable'), |
| 34 |
'edit_item' => __('Edit Author', 'mybooktable'), |
| 35 |
'view_item' => __('View Authors', 'mybooktable'), |
| 36 |
'update_item' => __('Update Author', 'mybooktable'), |
| 37 |
'search_items' => __('Search Authors', 'mybooktable'), |
| 38 |
'parent_item' => __('Parent Author', 'mybooktable'), |
| 39 |
'parent_item_colon' => __('Parent Authors:', 'mybooktable'), |
| 40 |
), |
| 41 |
'show_ui' => true, |
| 42 |
'show_admin_column' => true, |
| 43 |
'show_in_rest' => true, |
| 44 |
'rewrite' => array('slug' => apply_filters('mbt_author_rewrite_name', _x('authors', 'URL slug', 'mybooktable'))) |
| 45 |
)); |
| 46 |
|
| 47 |
register_taxonomy('mbt_genre', 'mbt_book', array( |
| 48 |
'hierarchical' => true, |
| 49 |
'labels' => array( |
| 50 |
'name' => __('Genres', 'mybooktable'), |
| 51 |
'singular_name' => __('Genre', 'mybooktable'), |
| 52 |
'all_items' => __('All Genres', 'mybooktable'), |
| 53 |
'add_new' => __('Add New', 'mybooktable'), |
| 54 |
'add_new_item' => __('Add New Genre', 'mybooktable'), |
| 55 |
'new_item_name' => __('New Genre', 'mybooktable'), |
| 56 |
'edit_item' => __('Edit Genre', 'mybooktable'), |
| 57 |
'view_item' => __('View Genres', 'mybooktable'), |
| 58 |
'update_item' => __('Update Genre', 'mybooktable'), |
| 59 |
'search_items' => __('Search Genres', 'mybooktable'), |
| 60 |
'parent_item' => __('Parent Genre', 'mybooktable'), |
| 61 |
'parent_item_colon' => __('Parent Genres:', 'mybooktable'), |
| 62 |
), |
| 63 |
'show_ui' => true, |
| 64 |
'show_admin_column' => true, |
| 65 |
'show_in_rest' => true, |
| 66 |
'rewrite' => array('slug' => apply_filters('mbt_genre_rewrite_name', _x('genre', 'URL slug', 'mybooktable'))) |
| 67 |
)); |
| 68 |
|
| 69 |
register_taxonomy('mbt_series', 'mbt_book', array( |
| 70 |
'hierarchical' => true, |
| 71 |
'labels' => array( |
| 72 |
'name' => __('Series', 'mybooktable'), |
| 73 |
'singular_name' => __('Series', 'mybooktable'), |
| 74 |
'all_items' => __('All Series', 'mybooktable'), |
| 75 |
'add_new' => __('Add New', 'mybooktable'), |
| 76 |
'add_new_item' => __('Add New Series', 'mybooktable'), |
| 77 |
'new_item_name' => __('New Series', 'mybooktable'), |
| 78 |
'edit_item' => __('Edit Series', 'mybooktable'), |
| 79 |
'view_item' => __('View Series', 'mybooktable'), |
| 80 |
'update_item' => __('Update Series', 'mybooktable'), |
| 81 |
'search_items' => __('Search Series', 'mybooktable'), |
| 82 |
'parent_item' => __('Parent Series', 'mybooktable'), |
| 83 |
'parent_item_colon' => __('Parent Series:', 'mybooktable'), |
| 84 |
), |
| 85 |
'show_ui' => true, |
| 86 |
'show_admin_column' => true, |
| 87 |
'show_in_rest' => true, |
| 88 |
'rewrite' => array('slug' => apply_filters('mbt_series_rewrite_name', _x('series', 'URL slug', 'mybooktable'))) |
| 89 |
)); |
| 90 |
|
| 91 |
register_taxonomy('mbt_tag', 'mbt_book', array( |
| 92 |
'hierarchical' => true, |
| 93 |
'labels' => array( |
| 94 |
'name' => __('Tags', 'mybooktable'), |
| 95 |
'singular_name' => __('Tag', 'mybooktable'), |
| 96 |
'all_items' => __('All Tags', 'mybooktable'), |
| 97 |
'add_new' => __('Add New', 'mybooktable'), |
| 98 |
'add_new_item' => __('Add New Tag', 'mybooktable'), |
| 99 |
'new_item_name' => __('New Tag', 'mybooktable'), |
| 100 |
'edit_item' => __('Edit Tag', 'mybooktable'), |
| 101 |
'view_item' => __('View Tags', 'mybooktable'), |
| 102 |
'update_item' => __('Update Tag', 'mybooktable'), |
| 103 |
'search_items' => __('Search Tags', 'mybooktable'), |
| 104 |
'parent_item' => __('Parent Tag', 'mybooktable'), |
| 105 |
'parent_item_colon' => __('Parent Tags:', 'mybooktable'), |
| 106 |
), |
| 107 |
'show_ui' => true, |
| 108 |
'show_admin_column' => true, |
| 109 |
'show_in_rest' => true, |
| 110 |
'rewrite' => array('slug' => apply_filters('mbt_tag_rewrite_name', mbt_get_product_slug()._x('tag', 'URL slug', 'mybooktable'))) |
| 111 |
)); |
| 112 |
|
| 113 |
if(!(bool)term_exists('recommended', 'mbt_tag')) { wp_insert_term('Recommended Books', 'mbt_tag', array('slug' => 'recommended')); } |
| 114 |
} |
| 115 |
|
| 116 |
function mbt_override_taxonomy_parent_files() { |
| 117 |
global $pagenow, $parent_file, $submenu_file; |
| 118 |
$taxonomy = filter_input(INPUT_GET,'taxonomy'); |
| 119 |
if(($pagenow == "edit-tags.php" or $pagenow == "term.php") and ($taxonomy == "mbt_series" or $taxonomy == "mbt_genre" or $taxonomy == "mbt_author" or $taxonomy == "mbt_tag")) { |
| 120 |
$parent_file = "mbt_dashboard"; |
| 121 |
} |
| 122 |
|
| 123 |
return $parent_file; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
/*---------------------------------------------------------*/ |
| 129 |
/* Custom Images for Taxonomies */ |
| 130 |
/*---------------------------------------------------------*/ |
| 131 |
|
| 132 |
function mbt_taxonomy_images_init() { |
| 133 |
add_filter('mbt_author_edit_form_fields', 'mbt_add_taxonomy_image_edit_form'); |
| 134 |
add_filter('mbt_author_add_form_fields', 'mbt_add_taxonomy_image_add_form'); |
| 135 |
add_action('edited_mbt_author', 'mbt_save_taxonomy_image_edit_form'); |
| 136 |
add_action('created_mbt_author', 'mbt_save_taxonomy_image_add_form'); |
| 137 |
|
| 138 |
add_filter('mbt_genre_edit_form_fields', 'mbt_add_taxonomy_image_edit_form'); |
| 139 |
add_filter('mbt_genre_add_form_fields', 'mbt_add_taxonomy_image_add_form'); |
| 140 |
add_action('edited_mbt_genre', 'mbt_save_taxonomy_image_edit_form'); |
| 141 |
add_action('created_mbt_genre', 'mbt_save_taxonomy_image_add_form'); |
| 142 |
|
| 143 |
add_filter('mbt_series_edit_form_fields', 'mbt_add_taxonomy_image_edit_form'); |
| 144 |
add_filter('mbt_series_add_form_fields', 'mbt_add_taxonomy_image_add_form'); |
| 145 |
add_action('edited_mbt_series', 'mbt_save_taxonomy_image_edit_form'); |
| 146 |
add_action('created_mbt_series', 'mbt_save_taxonomy_image_add_form'); |
| 147 |
|
| 148 |
add_filter('mbt_tag_edit_form_fields', 'mbt_add_taxonomy_image_edit_form'); |
| 149 |
add_filter('mbt_tag_add_form_fields', 'mbt_add_taxonomy_image_add_form'); |
| 150 |
add_action('edited_mbt_tag', 'mbt_save_taxonomy_image_edit_form'); |
| 151 |
add_action('created_mbt_tag', 'mbt_save_taxonomy_image_add_form'); |
| 152 |
} |
| 153 |
|
| 154 |
function mbt_add_taxonomy_image_edit_form() { |
| 155 |
$taxonomy = filter_input(INPUT_GET,'taxonomy'); |
| 156 |
$tag_ID = filter_input(INPUT_GET,'tag_ID'); |
| 157 |
$mbt_nonce = wp_nonce_field('mbt-tax-img-edit-nonce','data-tax-img-edit-nonce'); |
| 158 |
?> |
| 159 |
<tr class="form-field"> |
| 160 |
<th><label for="mbt_tax_image_url"><?php esc_attr_e('Image', 'mybooktable') ?></label></th> |
| 161 |
<td> |
| 162 |
<?php echo $mbt_nonce; |
| 163 |
$mbt_tax_image = mbt_get_taxonomy_image($taxonomy, $tag_ID); |
| 164 |
?> |
| 165 |
<input type="text" id="mbt_tax_image_url" name="mbt_tax_image_url" value="<?php echo(wp_kses_post($mbt_tax_image));?>" /> |
| 166 |
<input id="mbt_upload_tax_image_button" class="button mbt_upload_button" data-upload-target="mbt_tax_image_url" data-upload-title="<?php esc_attr_e('Taxonomy Image', 'mybooktable'); ?>" type="button" value="<?php esc_attr_e('Upload', 'mybooktable'); ?>" /> |
| 167 |
</td> |
| 168 |
</tr> |
| 169 |
<?php |
| 170 |
} |
| 171 |
|
| 172 |
function mbt_add_taxonomy_image_add_form() { |
| 173 |
$mbt_nonce = wp_nonce_field('mbt-tax-img-add-nonce','data-tax-img-add-nonce'); |
| 174 |
?> |
| 175 |
<div class="form-field"> |
| 176 |
<label for="mbt_tax_image_url"><?php esc_attr_e('Image', 'mybooktable') ?></label> |
| 177 |
<?php echo $mbt_nonce; ?> |
| 178 |
<input type="text" id="mbt_tax_image_url" name="mbt_tax_image_url" value="" /> |
| 179 |
<input id="mbt_upload_tax_image_button" class="button mbt_upload_button" data-upload-target="mbt_tax_image_url" data-upload-title="<?php esc_attr_e('Taxonomy Image', 'mybooktable'); ?>" type="button" value="<?php esc_attr_e('Upload', 'mybooktable'); ?>" /> |
| 180 |
</div> |
| 181 |
<?php |
| 182 |
} |
| 183 |
|
| 184 |
function mbt_save_taxonomy_image_edit_form() { |
| 185 |
if( isset($_REQUEST['data-tax-img-edit-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-tax-img-edit-nonce']),'mbt-tax-img-edit-nonce') ) { |
| 186 |
if(isset($_REQUEST['taxonomy']) and isset($_REQUEST['tag_ID']) and isset($_REQUEST['mbt_tax_image_url'])) { |
| 187 |
mbt_save_taxonomy_image(sanitize_text_field(wp_unslash($_REQUEST['taxonomy'])), sanitize_text_field(wp_unslash($_REQUEST['tag_ID'])), sanitize_text_field(wp_unslash($_REQUEST['mbt_tax_image_url']))); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
function mbt_save_taxonomy_image_add_form($term_id) { |
| 193 |
if( isset($_REQUEST['data-tax-img-add-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-tax-img-add-nonce']),'mbt-tax-img-add-nonce') ) { |
| 194 |
if(isset($_REQUEST['taxonomy']) and isset($_REQUEST['mbt_tax_image_url'])) { |
| 195 |
mbt_save_taxonomy_image(sanitize_text_field(wp_unslash($_REQUEST['taxonomy'])), $term_id, sanitize_text_field(wp_unslash($_REQUEST['mbt_tax_image_url']))); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
/*---------------------------------------------------------*/ |
| 203 |
/* Author Priority Sorting */ |
| 204 |
/*---------------------------------------------------------*/ |
| 205 |
|
| 206 |
function mbt_author_priorities_init() { |
| 207 |
add_filter('mbt_author_edit_form_fields', 'mbt_add_author_priority_edit_form'); |
| 208 |
add_filter('mbt_author_add_form_fields', 'mbt_add_author_priority_add_form'); |
| 209 |
add_action('edited_mbt_author', 'mbt_save_author_priority_edit_form'); |
| 210 |
add_action('created_mbt_author', 'mbt_save_author_priority_add_form'); |
| 211 |
} |
| 212 |
|
| 213 |
function mbt_add_author_priority_edit_form() { |
| 214 |
$mbt_nonce = wp_nonce_field('mbt-authpri-ed-nonce','data-authpri-ed-nonce'); |
| 215 |
$tag_ID = filter_input(INPUT_GET,'tag_ID'); |
| 216 |
?> |
| 217 |
<tr class="form-field"> |
| 218 |
<th><label for="mbt_author_priority"><?php esc_attr_e('Priority', 'mybooktable') ?></label></th> |
| 219 |
<td> |
| 220 |
<?php echo $mbt_nonce; ?> |
| 221 |
<input type="text" id="mbt_author_priority" name="mbt_author_priority" value="<?php echo(esc_attr(mbt_get_author_priority(intval($tag_ID)))); ?>" /> |
| 222 |
<div id="mbt_author_priority_slider"></div> |
| 223 |
<div id="mbt_author_priority_display"></div> |
| 224 |
<div style="clear:both"></div> |
| 225 |
<p class="description">Authors with higher priority will be shown first in the list of authors when a book has multiple authors.</p> |
| 226 |
</td> |
| 227 |
</tr> |
| 228 |
<?php |
| 229 |
} |
| 230 |
|
| 231 |
function mbt_add_author_priority_add_form() { |
| 232 |
$mbt_nonce = wp_nonce_field('mbt-authpri-add-nonce','data-authpri-add-nonce'); |
| 233 |
?> |
| 234 |
<div class="form-field"> |
| 235 |
<label for="mbt_author_priority"><?php esc_attr_e('Priority', 'mybooktable') ?></label> |
| 236 |
<?php echo $mbt_nonce; ?> |
| 237 |
<input type="text" id="mbt_author_priority" name="mbt_author_priority" value="50" /> |
| 238 |
<div id="mbt_author_priority_slider"></div> |
| 239 |
<div id="mbt_author_priority_display"></div> |
| 240 |
<div style="clear:both"></div> |
| 241 |
<p class="description">Authors with higher priority will be shown first in the list of authors when a book has multiple authors.</p> |
| 242 |
</div> |
| 243 |
<?php |
| 244 |
} |
| 245 |
|
| 246 |
function mbt_save_author_priority_edit_form() { |
| 247 |
if(isset($_REQUEST['data-authpri-ed-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-authpri-ed-nonce']),'mbt-authpri-ed-nonce') ) { |
| 248 |
if(isset($_REQUEST['taxonomy']) and isset($_REQUEST['tag_ID']) and isset($_REQUEST['mbt_author_priority'])) { |
| 249 |
mbt_save_author_priority(sanitize_text_field(wp_unslash($_REQUEST['tag_ID'])), sanitize_text_field(wp_unslash($_REQUEST['mbt_author_priority']))); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
function mbt_save_author_priority_add_form($term_id) { |
| 255 |
if(isset($_REQUEST['data-authpri-add-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-authpri-add-nonce']),'mbt-authpri-ed-nonce') ) { |
| 256 |
if(isset($_REQUEST['taxonomy']) and isset($_REQUEST['mbt_author_priority'])) { |
| 257 |
mbt_save_author_priority($term_id, sanitize_text_field(wp_unslash($_REQUEST['mbt_author_priority']))); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|