Skip to content

Instantly share code, notes, and snippets.

@Matthieu63
Created October 6, 2017 19:54
Show Gist options
  • Save Matthieu63/d21245092f38f3f3d066b957dd608290 to your computer and use it in GitHub Desktop.
Save Matthieu63/d21245092f38f3f3d066b957dd608290 to your computer and use it in GitHub Desktop.
<?php
class Wpadverts_Ci_Admin {
/**
* Admin Constructor
*
* Creates object which manages WP Adverts Category Icons integration options.
*
* @since 1.0
* @return Wpadverts_Ci_Admin
*/
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
/**
* Admin Init
*
* Initiates wp-admin actions and filters
*
* @since 1.0
* @return void
*/
public function init() {
add_action( 'edited_advert_category', array( &$this, 'save_advert_category_image' ), 10, 2 );
add_action( 'create_advert_category', array( &$this, 'save_advert_category_image' ), 10, 2 );
add_action( 'advert_category_edit_form_fields', array( &$this, 'advert_category_edit_meta_field' ), 10, 2 );
if( defined( "ADVERTS_PATH" ) ) {
include_once ADVERTS_PATH . 'includes/class-updates-manager.php';
$manager = new Adverts_Updates_Manager(
"wpadverts-category-icons/wpadverts-category-icons.php",
"wpadverts-category-icons",
"1.0.1"
);
$manager->connect();
}
}
// Add Upload fields to "Add New Taxonomy" form
function add_wpadverts_category_small_icon_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="wpadverts_category_small_icon"><?php _e( 'Small Icon:', 'wpadverts-ci' ); ?></label>
<input type="text" name="wpadverts_category_small_icon[image_small]" id="wpadverts_category_small_icon[image_small]" class="small-image" value="">
<input class="upload_image_button button" name="_add_wpadverts_category_small_icon" id="_add_wpadverts_category_small_icon" type="button" value="<?php _e("Select / Upload Image", "wpadverts-ci") ?>" />
<script>
jQuery(document).ready(function() {
jQuery('#_add_wpadverts_category_small_icon').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('.small-image').val(attachment.url);
}
wp.media.editor.open(this);
return false;
});
});
</script>
</div>
<div class="form-field">
<label for="wpadverts_category_big_icon"><?php _e( 'Big Icon:', 'wpadverts-ci' ); ?></label>
<input type="text" name="wpadverts_category_big_icon[image_big]" id="wpadverts_category_big_icon[image_big]" class="big-image" value="">
<input class="upload_image_button button" name="_add_wpadverts_category_big_icon" id="_add_wpadverts_category_big_icon" type="button" value="<?php _e("Select / Upload Image", "wpadverts-ci") ?>" />
<script>
jQuery(document).ready(function() {
jQuery('#_add_wpadverts_category_big_icon').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('.big-image').val(attachment.url);
}
wp.media.editor.open(this);
return false;
});
});
</script>
</div>
<?php
}
// Add Upload fields to "Edit Taxonomy" form
function advert_category_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "advert_category_$t_id" );
?>
wp_enqueue_media();
<tr class="form-field">
<th scope="row" valign="top"><label for="_wpadverts_category_small_icon"><?php _e( 'Small Icon', 'wpadverts-ci' ); ?></label></th>
<td>
<style>
input[type=text].small-image {
width: 55%;
}
div.img-wrap-small {
background: #aaa no-repeat left;
background-size:contain;
max-width: 16px;
max-height: 16px;
width: 100%;
height: 100%;
overflow:hidden;
}
div.img-wrap img {
max-width: 16px;
}
div.img-wrap-small.active {
background: transparent;
border: 1px solid #aaa;
}
</style>
<?php
$smallimage = esc_attr( $term_meta['image_small'] ) ? esc_attr( $term_meta['image_small'] ) : '';
?>
<input type="text" name="wpadverts_category_small_icon[image_small]" id="wpadverts_category_small_icon[image_small]" class="small-image" value="<?php echo $smallimage; ?>">
<input class="upload_image_button button" name="_wpadverts_category_small_icon" id="_wpadverts_category_small_icon" type="button" value="Select/Upload Image" />
<input class="upload_image_button button" name="_wpadverts_category_small_icon_remove" id="_wpadverts_category_small_icon_remove" type="button" value="<?php _e('Remove Image', 'wpadverts-ci'); ?>" />
<p class="description"><?php _e('This icon will replace "Category Icon" field selection. Size: 16px x 16px.', 'wpadverts-ci'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"></th>
<td style="height: 16px;">
<div class="img-wrap-small <?php if(strlen($smallimage) > 0 ): ?> active <?php endif; ?>">
<img src="<?php echo $smallimage; ?>" id="small-img">
</div>
<script>
jQuery(document).ready(function() {
jQuery('#_wpadverts_category_small_icon').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('#small-img').attr("src",attachment.url);
jQuery('.small-image').val(attachment.url);
jQuery('.img-wrap-small').addClass("active");
}
wp.media.editor.open(this);
return false;
});
jQuery('#_wpadverts_category_small_icon_remove').click(function() {
jQuery('#small-img').attr("src", "");
jQuery('.small-image').val("");
jQuery('.img-wrap-small').removeClass("active");
});
});
</script>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="_wpadverts_category_big_icon"><?php _e( 'Big Icon', 'wpadverts-ci' ); ?></label></th>
<td>
<?php
$bigimage = esc_attr( $term_meta['image_big'] ) ? esc_attr( $term_meta['image_big'] ) : '';
?>
<input type="text" name="wpadverts_category_big_icon[image_big]" id="wpadverts_category_big_icon[image_big]" class="big-image" value="<?php echo $bigimage; ?>">
<input class="upload_image_button button" name="_wpadverts_category_big_icon" id="_wpadverts_category_big_icon" type="button" value="<?php _e('Select/Upload Image', 'wpadverts-ci'); ?>" />
<input class="upload_image_button button" name="_wpadverts_category_big_icon_remove" id="_wpadverts_category_big_icon_remove" type="button" value="<?php _e('Remove Image', 'wpadverts-ci'); ?>" />
<p class="description"><?php _e('This icon will replace "Category Icon" field selection. Size: 64px x 64px.', 'wpadverts-ci'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"></th>
<td style="height: 64px;">
<style>
input[type=text].big-image {
width: 55%;
}
div.img-wrap-big {
background: #aaa no-repeat left;
background-size:contain;
max-width: 64px;
max-height: 64px;
width: 100%;
height: 100%;
overflow:hidden;
}
div.img-wrap img {
max-width: 64px;
}
div.img-wrap-big.active {
background: transparent;
border: 1px solid #aaa;
}
</style>
<div class="img-wrap-big <?php if(strlen($bigimage) > 0 ): ?> active <?php endif; ?>">
<img src="<?php echo $bigimage; ?>" id="big-img">
</div>
<script>
jQuery(document).ready(function() {
jQuery('#_wpadverts_category_big_icon').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('#big-img').attr("src",attachment.url);
jQuery('.big-image').val(attachment.url);
jQuery('.img-wrap-big').addClass("active");
}
wp.media.editor.open(this);
return false;
});
jQuery('#_wpadverts_category_big_icon_remove').click(function() {
jQuery('#big-img').attr("src", "");
jQuery('.big-image').val("");
jQuery('.img-wrap-big').removeClass("active");
});
});
</script>
</td>
</tr>
<?php
}
// Save Taxonomy Image fields callback function.
function save_advert_category_image( $term_id ) {
if ( isset( $_POST['wpadverts_category_small_icon'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "advert_category_$t_id" );
$cat_keys = array_keys( $_POST['wpadverts_category_small_icon'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['wpadverts_category_small_icon'][$key] ) ) {
$term_meta[$key] = $_POST['wpadverts_category_small_icon'][$key];
}
}
// Save the option array.
update_option( "advert_category_$t_id", $term_meta );
}
if ( isset( $_POST['wpadverts_category_big_icon'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "advert_category_$t_id" );
$cat_keys = array_keys( $_POST['wpadverts_category_big_icon'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['wpadverts_category_big_icon'][$key] ) ) {
$term_meta[$key] = $_POST['wpadverts_category_big_icon'][$key];
}
}
// Save the option array.
update_option( "advert_category_$t_id", $term_meta );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment