File: /home/zeusxp5/chuair.org/wp-content/plugins/wd-framework/woo-taxonomy-field.php
<?php
if(!class_exists('woo_details_Taxonomy')) {
class woo_details_Taxonomy {
public function __construct () {
//
}
public function load_media () {
if(!isset($_GET['taxonomy']) || $_GET['taxonomy'] != 'product_creatore_tax') {
return;
}
wp_enqueue_media();
}
/**
* Add a form field in the new category page
* @since 1.0.0
*/
public function add_category_image ($taxonomy) { ?>
<div class="form-field term-group">
<label for="thumbnail_id"><?php _e('Image', 'bidpro'); ?></label>
<input type="hidden" id="thumbnail_id" name="thumbnail_id" class="custom_media_url"
value="">
<div id="category-image-wrapper"></div>
<p>
<input type="button" class="button button-secondary showcase_tax_media_button" id="showcase_tax_media_button"
name="showcase_tax_media_button" value="<?php _e('Add Image', 'bidpro'); ?>"/>
<input type="button" class="button button-secondary showcase_tax_media_remove" id="showcase_tax_media_remove"
name="showcase_tax_media_remove" value="<?php _e('Remove Image', 'bidpro'); ?>"/>
</p>
</div>
<?php }
/**
* Save the form field
* @since 1.0.0
*/
public function save_category_image ($term_id, $tt_id) {
if(isset($_POST['thumbnail_id']) && '' !== $_POST['thumbnail_id']) {
add_term_meta($term_id, 'thumbnail_id', absint($_POST['thumbnail_id']), true);
}
}
/**
* Edit the form field
* @since 1.0.0
*/
public function update_category_image ($term, $taxonomy) { ?>
<tr class="form-field term-group-wrap">
<th scope="row">
<label for="thumbnail_id"><?php _e('Image', 'bidpro'); ?></label>
</th>
<td>
<?php
$image_id = get_term_meta($term->term_id, 'thumbnail_id', true);
?>
<input type="hidden" id="thumbnail_id" name="thumbnail_id"
value="<?php echo esc_attr($image_id); ?>">
<div id="category-image-wrapper">
<?php if($image_id) { ?>
<?php echo wp_get_attachment_image($image_id, 'thumbnail'); ?>
<?php } ?>
</div>
<p>
<input type="button" class="button button-secondary showcase_tax_media_button"
id="showcase_tax_media_button" name="showcase_tax_media_button"
value="<?php _e('Add Image', 'bidpro'); ?>"/>
<input type="button" class="button button-secondary showcase_tax_media_remove"
id="showcase_tax_media_remove" name="showcase_tax_media_remove"
value="<?php _e('Remove Image', 'bidpro'); ?>"/>
</p>
</td>
</tr>
<?php }
/**
* Update the form field value
* @since 1.0.0
*/
public function updated_category_image ($term_id, $tt_id) {
if(isset($_POST['thumbnail_id']) && '' !== $_POST['thumbnail_id']) {
update_term_meta($term_id, 'thumbnail_id', absint($_POST['thumbnail_id']));
}
else {
update_term_meta($term_id, 'thumbnail_id', '');
}
}
/**
* Enqueue styles and scripts
* @since 1.0.0
*/
public function add_script () {
if(!isset($_GET['taxonomy']) || $_GET['taxonomy'] != 'product_creatore_tax') {
return;
} ?>
<script> jQuery(document).ready(function ($) {
_wpMediaViewsL10n.insertIntoPost = '<?php _e("Insert", "showcase"); ?>';
function ct_media_upload(button_class) {
var _custom_media = true, _orig_send_attachment = wp.media.editor.send.attachment;
$('body').on('click', button_class, function (e) {
var button_id = '#' + $(this).attr('id');
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(button_id);
_custom_media = true;
wp.media.editor.send.attachment = function (props, attachment) {
if (_custom_media) {
$('#thumbnail_id').val(attachment.id);
$('#category-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
$('#category-image-wrapper .custom_media_image').attr('src', attachment.url).css('display', 'block');
} else {
return _orig_send_attachment.apply(button_id, [props, attachment]);
}
}
wp.media.editor.open(button);
return false;
});
}
ct_media_upload('.showcase_tax_media_button.button');
$('body').on('click', '.showcase_tax_media_remove', function () {
$('#thumbnail_id').val('');
$('#category-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
});
// Thanks: http://stackoverflow.com/questions/15281995/wordpress-create-category-ajax-response
$(document).ajaxComplete(function (event, xhr, settings) {
var queryStringArr = settings.data.split('&');
if ($.inArray('action=add-tag', queryStringArr) !== -1) {
var xml = xhr.responseXML;
$response = $(xml).find('term_id').text();
if ($response != "") {
// Clear the thumb image
$('#category-image-wrapper').html('');
}
}
});
});
</script>
<?php }
/**
* Initialize the class and start calling our hooks and filters
*/
public function init () {
// Image actions
add_action('product_creatore_tax_add_form_fields', array($this, 'add_category_image'), 10, 2);
add_action('created_product_creatore_tax', array($this, 'save_category_image'), 10, 2);
add_action('product_creatore_tax_edit_form_fields', array($this, 'update_category_image'), 10, 2);
add_action('edited_product_creatore_tax', array($this, 'updated_category_image'), 10, 2);
add_action('admin_enqueue_scripts', array($this, 'load_media'));
add_action('admin_footer', array($this, 'add_script'));
add_action('product_details_tax_add_form_fields', array($this, 'add_details_taxonomy'), 10, 2);
add_action('product_details_tax_edit_form_fields', array($this, 'update_details_taxonomy'), 10, 2);
add_action('created_product_details_tax', array($this, 'save_details_taxonomy'), 10, 2);
add_action('edited_product_details_tax', array($this, 'updated_details_taxonomy'), 10, 2);
}
/**
* Add a form field in the new category page
* @since 1.0.0
*/
public function add_details_taxonomy ($taxonomy) {
?>
<div class="form-field">
<label for="show_in_list"><?php _e('Show in List', 'wh'); ?></label>
<?php
woocommerce_form_field(
'show_in_list', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __(''),
));
?>
</div>
<div class="form-field">
<label for="show_on_details_page"><?php _e('Show on details page', 'wh'); ?></label>
<?php
woocommerce_form_field(
'show_on_details_page', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
));
?>
</div>
<div class="form-field">
<label for="numeric_value"><?php _e('Numeric value', 'wh'); ?></label>
<?php
woocommerce_form_field(
'numeric_value', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
));
?>
</div>
<div class="form-field">
<label for="unit_of_measurement"><?php _e('Unit of measurement', 'wh'); ?></label>
<input type="text" name="unit_of_measurement" id="unit_of_measurement" value="">
</div>
<?php }
/**
* Save the form field
* @since 1.0.0
*/
public function save_details_taxonomy ($term_id, $tt_id) {
$show_in_list = filter_input(INPUT_POST, 'show_in_list');
$show_on_details_page = filter_input(INPUT_POST, 'show_on_details_page');
$numeric_value = filter_input(INPUT_POST, 'numeric_value');
update_term_meta($term_id, 'show_in_list', $show_in_list);
update_term_meta($term_id, 'show_on_details_page', $show_on_details_page);
update_term_meta($term_id, 'numeric_value', $numeric_value);
}
/**
* Edit the form field
* @since 1.0.0
*/
public function update_details_taxonomy ($term, $taxonomy) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field.
$show_in_list = get_term_meta($term_id, 'show_in_list', true);
$show_on_details_page = get_term_meta($term_id, 'show_on_details_page', true);
$numeric_value = get_term_meta($term_id, 'numeric_value', true);
$unit_of_measurement = get_term_meta($term_id, 'unit_of_measurement', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="show_in_list"><?php _e('Show in List', 'wh'); ?></label></th>
<td>
<?php
//$checked = $show_in_list ? $show_in_list : 1;
woocommerce_form_field(
'show_in_list', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
),$show_in_list);
?>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="show_on_details_page"><?php _e('Show on details page', 'wh'); ?></label></th>
<td>
<?php
woocommerce_form_field(
'show_on_details_page', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
),$show_on_details_page);
?>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="numeric_value"><?php _e('Numeric value', 'wh'); ?></label></th>
<td>
<?php
woocommerce_form_field(
'numeric_value', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
),$numeric_value);
?>
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="unit_of_measurement"><?php _e('Unit of measurement', 'wh'); ?></label></th>
<td>
<input type="text" name="unit_of_measurement" id="unit_of_measurement" value="<?php echo esc_attr($unit_of_measurement) ? esc_attr($unit_of_measurement) : ''; ?>">
</td>
</tr>
<?php }
/**
* Update the form field value
* @since 1.0.0
*/
public function updated_details_taxonomy ($term_id, $tt_id) {
$show_in_list = filter_input(INPUT_POST, 'show_in_list');
$show_on_details_page = filter_input(INPUT_POST, 'show_on_details_page');
$numeric_value = filter_input(INPUT_POST, 'numeric_value');
$unit_of_measurement = filter_input(INPUT_POST, 'unit_of_measurement');
update_term_meta($term_id, 'show_in_list', $show_in_list);
update_term_meta($term_id, 'show_on_details_page', $show_on_details_page);
update_term_meta($term_id, 'numeric_value', $numeric_value);
update_term_meta($term_id, 'unit_of_measurement', $unit_of_measurement);
}
}
$woo_details_Taxonomy = new woo_details_Taxonomy();
$woo_details_Taxonomy->init();
}