| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register Fields for Category. |
| 4 |
* |
| 5 |
* @link https://codesupply.co |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Sight |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Register Fields |
| 13 |
*/ |
| 14 |
class Sight_Category_Fields { |
| 15 |
|
| 16 |
/** |
| 17 |
* Initialize |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_action( 'sight-categories_add_form_fields', array( $this, 'category_add_form_fields' ), 10 ); |
| 21 |
add_action( 'sight-categories_edit_form_fields', array( $this, 'category_edit_form_fields' ), 10, 2 ); |
| 22 |
add_action( 'created_sight-categories', array( $this, 'save_category' ), 10, 2 ); |
| 23 |
add_action( 'edited_sight-categories', array( $this, 'save_category' ), 10, 2 ); |
| 24 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 10 ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Add fields to Category |
| 29 |
* |
| 30 |
* @param string $taxonomy The taxonomy slug. |
| 31 |
*/ |
| 32 |
public function category_add_form_fields( $taxonomy ) { |
| 33 |
wp_nonce_field( 'category_options', 'sight_category' ); |
| 34 |
?> |
| 35 |
<div class="form-field"> |
| 36 |
<label><?php esc_html_e( 'Featured Image', 'sight' ); ?></label> |
| 37 |
|
| 38 |
<div class="sight-featured-image" data-frame-title="<?php esc_html_e( 'Select or upload image', 'sight' ); ?>" data-frame-btn-text="<?php esc_html_e( 'Set image', 'sight' ); ?>"> |
| 39 |
<p class="uploaded-img-box"> |
| 40 |
<span class="sight-uploaded-image"></span> |
| 41 |
<input id="sight_featured_image" class="sight-uploaded-img-id" name="sight_featured_image" type="hidden"/> |
| 42 |
</p> |
| 43 |
<p class="hide-if-no-js"> |
| 44 |
<a class="sight-upload-img-link button button-primary" href="#"><?php esc_html_e( 'Upload image', 'sight' ); ?></a> |
| 45 |
<a class="sight-delete-img-link button button-secondary hidden" href="#"><?php esc_html_e( 'Remove image', 'sight' ); ?></a> |
| 46 |
</p> |
| 47 |
</div> |
| 48 |
</div> |
| 49 |
<br> |
| 50 |
<?php |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Edit fields from Category |
| 55 |
* |
| 56 |
* @param object $tag Current taxonomy term object. |
| 57 |
* @param string $taxonomy Current taxonomy slug. |
| 58 |
*/ |
| 59 |
public function category_edit_form_fields( $tag, $taxonomy ) { |
| 60 |
wp_nonce_field( 'category_options', 'sight_category' ); |
| 61 |
|
| 62 |
$sight_featured_image = get_term_meta( $tag->term_id, 'sight_featured_image', true ); |
| 63 |
?> |
| 64 |
<tr class="form-field"> |
| 65 |
<th scope="row" valign="top"> |
| 66 |
<label for="sight_featured_image"><?php esc_html_e( 'Featured Image', 'sight' ); ?></label> |
| 67 |
</th> |
| 68 |
<td> |
| 69 |
<div class="sight-featured-image" data-frame-title="<?php esc_html_e( 'Select or upload image', 'sight' ); ?>" data-frame-btn-text="<?php esc_html_e( 'Set image', 'sight' ); ?>"> |
| 70 |
<p class="uploaded-img-box"> |
| 71 |
<span class="sight-uploaded-image"> |
| 72 |
<?php if ( $sight_featured_image ) : ?> |
| 73 |
<?php |
| 74 |
echo wp_get_attachment_image( $sight_featured_image, 'large', false, array( |
| 75 |
'style' => 'max-width:100%; height: auto;', |
| 76 |
) ); |
| 77 |
?> |
| 78 |
<?php endif; ?> |
| 79 |
</span> |
| 80 |
|
| 81 |
<input id="sight_featured_image" class="sight-uploaded-img-id" name="sight_featured_image" type="hidden" value="<?php echo esc_attr( $sight_featured_image ); ?>" /> |
| 82 |
</p> |
| 83 |
<p class="hide-if-no-js"> |
| 84 |
<a class="sight-upload-img-link button button-primary <?php echo esc_attr( $sight_featured_image ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Upload image', 'sight' ); ?></a> |
| 85 |
<a class="sight-delete-img-link button button-secondary <?php echo esc_attr( ! $sight_featured_image ? 'hidden' : '' ); ?>" href="#"><?php esc_html_e( 'Remove image', 'sight' ); ?></a> |
| 86 |
</p> |
| 87 |
</div> |
| 88 |
|
| 89 |
<p class="description"><?php esc_html_e( 'This image is used in the category blocks.', 'sight' ); ?></p> |
| 90 |
</td> |
| 91 |
</tr> |
| 92 |
<?php |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Save category fields |
| 97 |
* |
| 98 |
* @param int $term_id ID of the term about to be edited. |
| 99 |
* @param string $taxonomy Taxonomy slug of the related term. |
| 100 |
*/ |
| 101 |
public function save_category( $term_id, $taxonomy ) { |
| 102 |
|
| 103 |
// Bail if we're doing an auto save. |
| 104 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// if our nonce isn't there, or we can't verify it, bail. |
| 109 |
if ( ! isset( $_POST['sight_category'] ) || ! wp_verify_nonce( $_POST['sight_category'], 'category_options' ) ) { // Input var ok; sanitization ok. |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if ( isset( $_POST['sight_featured_image'] ) ) { // Input var ok; sanitization ok. |
| 114 |
$sight_featured_image = sanitize_text_field( $_POST['sight_featured_image'] ); // Input var ok; sanitization ok. |
| 115 |
|
| 116 |
update_term_meta( $term_id, 'sight_featured_image', $sight_featured_image ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Register the stylesheets and JavaScript for the admin area. |
| 122 |
* |
| 123 |
* @param string $page Current page. |
| 124 |
*/ |
| 125 |
public function admin_enqueue_scripts( $page ) { |
| 126 |
if ( 'edit-tags.php' === $page || 'term.php' === $page ) { |
| 127 |
wp_enqueue_script( 'jquery-core' ); |
| 128 |
|
| 129 |
wp_enqueue_media(); |
| 130 |
|
| 131 |
ob_start(); |
| 132 |
?> |
| 133 |
<script> |
| 134 |
( function() { |
| 135 |
|
| 136 |
var portfolioFeaturedContainer = '.sight-featured-image'; |
| 137 |
|
| 138 |
var portfolioFeaturedFrame; |
| 139 |
|
| 140 |
|
| 141 |
jQuery( document ).ready( function( $ ) { |
| 142 |
|
| 143 |
/* Add Image Link */ |
| 144 |
jQuery( portfolioFeaturedContainer ).find( '.sight-upload-img-link' ).on( 'click', function( event ){ |
| 145 |
event.preventDefault(); |
| 146 |
|
| 147 |
var parentContainer = $( this ).parents( portfolioFeaturedContainer ); |
| 148 |
|
| 149 |
// Options. |
| 150 |
var options = { |
| 151 |
title: parentContainer.data( 'frame-title' ) ? parentContainer.data( 'frame-title' ) : 'Select or Upload Media', |
| 152 |
button: { |
| 153 |
text: parentContainer.data( 'frame-btn-text' ) ? parentContainer.data( 'frame-btn-text' ) : 'Use this media', |
| 154 |
}, |
| 155 |
library : { type : 'image' }, |
| 156 |
multiple: false // Set to true to allow multiple files to be selected. |
| 157 |
}; |
| 158 |
|
| 159 |
// Create a new media frame |
| 160 |
portfolioFeaturedFrame = wp.media( options ); |
| 161 |
|
| 162 |
// When an image is selected in the media frame... |
| 163 |
portfolioFeaturedFrame.on( 'select', function() { |
| 164 |
|
| 165 |
// Get media attachment details from the frame state. |
| 166 |
var attachment = portfolioFeaturedFrame.state().get('selection').first().toJSON(); |
| 167 |
|
| 168 |
// Send the attachment URL to our custom image input field. |
| 169 |
parentContainer.find( '.sight-uploaded-image' ).html( '<img src="' + attachment.url + '" style="max-width:100%;"/>' ); |
| 170 |
parentContainer.find( '.sight-uploaded-img-id' ).val( attachment.id ).change(); |
| 171 |
parentContainer.find( '.sight-upload-img-link' ).addClass( 'hidden' ); |
| 172 |
parentContainer.find( '.sight-delete-img-link' ).removeClass( 'hidden' ); |
| 173 |
|
| 174 |
portfolioFeaturedFrame.close(); |
| 175 |
}); |
| 176 |
|
| 177 |
// Finally, open the modal on click. |
| 178 |
portfolioFeaturedFrame.open(); |
| 179 |
}); |
| 180 |
|
| 181 |
|
| 182 |
/* Delete Image Link */ |
| 183 |
$( portfolioFeaturedContainer ).find( '.sight-delete-img-link' ).on( 'click', function( event ){ |
| 184 |
event.preventDefault(); |
| 185 |
|
| 186 |
$( this ).parents( portfolioFeaturedContainer ).find( '.sight-uploaded-image' ).html( '' ); |
| 187 |
$( this ).parents( portfolioFeaturedContainer ).find( '.sight-upload-img-link' ).removeClass( 'hidden' ); |
| 188 |
$( this ).parents( portfolioFeaturedContainer ).find( '.sight-delete-img-link' ).addClass( 'hidden' ); |
| 189 |
$( this ).parents( portfolioFeaturedContainer ).find( '.sight-uploaded-img-id' ).val( '' ).change(); |
| 190 |
}); |
| 191 |
}); |
| 192 |
|
| 193 |
jQuery( document ).ajaxSuccess(function(e, request, settings){ |
| 194 |
let action = settings.data.indexOf( 'action=add-tag' ); |
| 195 |
let screen = settings.data.indexOf( 'screen=edit-category' ); |
| 196 |
let taxonomy = settings.data.indexOf( 'taxonomy=category' ); |
| 197 |
|
| 198 |
if( action > -1 && screen > -1 && taxonomy > -1 ){ |
| 199 |
$( portfolioFeaturedContainer ).find( '.sight-delete-img-link' ).click(); |
| 200 |
} |
| 201 |
}); |
| 202 |
|
| 203 |
} )(); |
| 204 |
</script> |
| 205 |
<?php |
| 206 |
wp_add_inline_script( 'jquery-core', str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
new Sight_Category_Fields(); |
| 212 |
|