settings_api = new WpNinjaCoder_Settings_API;
add_action( 'admin_init', array($this, 'admin_init') );
add_action( 'admin_menu', array($this, 'admin_menu') );
}
public function admin_init() {
//set the settings
$this->settings_api->set_sections( $this->get_settings_sections() );
$this->settings_api->set_fields( $this->get_settings_fields() );
//initialize settings
$this->settings_api->admin_init();
}
public function admin_menu() {
add_options_page( 'Advanced Category & Taxonomy Image', 'Advanced Category & Taxonomy Image', 'manage_options', 'advanced-cat-tax-image', array($this, 'render_advanced_taxonomy_image_settings') );
}
public function get_settings_sections() {
$sections = array(
array(
'id' => 'ad_cat_tax_img_basic_settings',
'title' => __( 'General', 'cat-tax-image' )
),
array(
'id' => 'ad_cat_tax_img_advanced_settings',
'title' => __( 'Advanced ', 'cat-tax-image' )
)
);
return $sections;
}
/**
* Returns all the settings fields
*
* @return array settings fields
*/
public function get_settings_fields() {
$settings_fields = array(
'ad_cat_tax_img_basic_settings' => array(
array(
'name' => 'enabled_taxonomies',
'label' => __( 'Select Taxonomies', 'cat-tax-image' ),
'desc' => __( 'Please Select Taxonomies You Want To Include Custom Image', 'cat-tax-image' ),
'type' => 'multicheck',
'options' => $this->get_all_taxonomies()
)
),
'ad_cat_tax_img_advanced_settings' => array(
array(
'name' => 'enabled_devices',
'label' => __( 'Enable Device Filter', 'cat-tax-image' ),
'desc' => __( 'Please Select Device Type You Want To Use Enable For', 'cat-tax-image' ),
'type' => 'multicheck',
'default' => array('universal' => 'universal'),
'options' => array(
'android' => 'Android',
'ios' => 'iOS (Mac | iPhone | iPad | iPod)',
'windowsph' => 'Windows Phone',
'mobile' => 'Mobile (Any)',
'tablet' => 'Tablet',
'desktop' => 'Desktop'
)
)
)
);
return $settings_fields;
}
/**
* Render settings fields
*
*/
public function render_advanced_taxonomy_image_settings() {
echo '
';
$this->settings_api->show_navigation();
$this->settings_api->show_forms();
echo '
';
}
/**
* Returns all the taxonomies
*
* @return array taxonomies
*/
public function get_all_taxonomies(){
$args = array();
$output = 'objects'; // objects
$taxonomies = get_taxonomies( $args, $output );
$name_value_pair = array();
// exclude some wp & woocommerce private taxonomies
$disabled_taxonomies = array('nav_menu', 'link_category', 'post_format','product_visibility','product_shipping_class','action-group','product_type');
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
if ( in_array($taxonomy->name, $disabled_taxonomies )) continue;
$name_value_pair[$taxonomy->name] = ucwords( $taxonomy->label );
}
}
return $name_value_pair;
}
}
/**
* Returns option value
*
* @return string|array option value
*/
function ad_cat_tax_img_get_option( $option, $section, $default = '' ) {
$options = get_option( $section );
if ( isset( $options[$option] ) ) {
return $options[$option];
}
return $default;
}
$cat_tax_settings = new AD_CAT_TAX_IMG_SETTINGS();
// register custom image field for all enabled taxonomies
function ad_cat_tax_img_register_taxonomy_img_field(){
add_action( 'admin_enqueue_scripts', function(){
wp_enqueue_media(); // load WP Media Uploader Modal scripts
wp_enqueue_style( 'ad_cat_tax_img_custom_style', AD_CAT_TAX_IMG_ROOT_URL . 'assets/css/style.css', array(), false, 'all' );
wp_enqueue_script( 'ad_cat_tax_img_custom_script', AD_CAT_TAX_IMG_ROOT_URL . 'assets/js/script.js', array('jquery'), time(), true );
});
/**
* Add Go To Settings Page in Plugin List Table
*
*/
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), function( $links ) {
$plugin_actions = array();
$plugin_actions[] = sprintf( '%s', admin_url( 'options-general.php?page=advanced-cat-tax-image' ), __( 'Settings', 'cat-tax-image' ) );
$plugin_actions[] = sprintf( '%s', 'https://wordpress.org/plugins/wp-advanced-taxonomy-image/', __( 'Documentation', 'cat-tax-image' ) );
return array_merge( $links, $plugin_actions );
});
// get all image field enabled taxonomies
$enabled_taxonomies = ad_cat_tax_img_get_option( 'enabled_taxonomies', 'ad_cat_tax_img_basic_settings' );
//check if any taxonomy enabled
if (!empty( $enabled_taxonomies )) {
//iterate all enabled taxonomies
foreach ( $enabled_taxonomies as $enabled_taxonomy ) {
// Add shortcode column to taxonomy list
add_filter( "manage_edit-{$enabled_taxonomy}_columns" , function( $columns ) {
// add carousel shortcode column
$columns['taxonomy_image_template_tag'] = __('Taxonomy Image Template Tag');
return $columns;
});
// add shortcode column content
add_filter( "manage_{$enabled_taxonomy}_custom_column", function( $content, $column_name, $term_id ) {
// check if column is our custom column 'carousel_shortcode'
if ( 'taxonomy_image_template_tag' == $column_name ) {
$shortcode = 'get_taxonomy_image('.$term_id.')';
}
return $shortcode;
}, 10, 3 );
// register all enabled taxonomy to add taxonomy field
add_action( $enabled_taxonomy.'_add_form_fields', function(){
$label = 'Choose File';
// get all image field enabled devices
$enabled_devices = ad_cat_tax_img_get_option( 'enabled_devices', 'ad_cat_tax_img_advanced_settings' );
//check if any device enabled
if (!empty( $enabled_devices )) {
$html = '';
echo $html;
// registed custom image field for each enabled devices
foreach ( $enabled_devices as $enabled_device) {
$html = '';
echo $html;
}
}else{
$html = '';
echo $html;
}
});
add_action( $enabled_taxonomy.'_edit_form_fields', function( $taxonomy ){
$label = 'Choose File';
// get all image field enabled devices
$enabled_devices = ad_cat_tax_img_get_option( 'enabled_devices', 'ad_cat_tax_img_advanced_settings' );
//check if any device enabled
if (!empty( $enabled_devices )) {
$universal_image_url = esc_url( get_term_meta( $taxonomy->term_id, 'tax_image_url_universal', true ) );
$image = $universal_image_url != '' ? '
' : '';
$html = '';
$html .= ' | ';
$html .= $image;
$html .= ' ';
$html .= '';
$html .= '';
$html .= 'Choose Image To Show For All Devices |
';
echo $html;
// registed custom image field for each enabled devices
foreach ( $enabled_devices as $enabled_device) {
$device_image_url = esc_url( get_term_meta( $taxonomy->term_id, 'tax_image_url_'.$enabled_device, true ) );
$image = $device_image_url != '' ? '
' : '';
$html = '';
$html .= ' | ';
$html .= $image;
$html .= ' ';
$html .= '';
$html .= '';
$html .= 'Choose Image To Show For '.ucwords($enabled_device).' ';
echo $html;
}
}else{
$universal_image_url = esc_url( get_term_meta( $taxonomy->term_id, 'tax_image_url_universal', true ) );
$image = $universal_image_url != '' ? ' ' : '';
$html = ' |
';
$html .= ' | ';
$html .= $image;
$html .= ' ';
$html .= '';
$html .= '';
$html .= 'Choose Image To Show For All Devices |
';
}
});
}
}
}
ad_cat_tax_img_register_taxonomy_img_field();
//edit_$taxonomy
add_action('edit_term','ad_cat_tax_img_url_save');
add_action('create_term','ad_cat_tax_img_url_save');
// save taxonomy values
function ad_cat_tax_img_url_save($term_id){
if ( isset( $_POST['tax_image_url'] ) && !empty( $_POST['tax_image_url'] ) ){
if (is_array( $_POST['tax_image_url'] )) {
foreach ( $_POST['tax_image_url'] as $name => $value ) {
update_term_meta( $term_id, $name, esc_url( $value ) );
}
}
}
}
// register template tag function to show taxonomy image
function get_taxonomy_image( $term_id = '', $return_img_tag = false, $class = array() ){
require AD_CAT_TAX_IMG_ROOT_DIR. '/includes/Mobile_Detect.php';
$detect = new Mobile_Detect;
$term_id = $term_id == '' ? get_queried_object()->term_id : $term_id;
// get all image field enabled taxonomies
$enabled_taxonomies = ad_cat_tax_img_get_option( 'enabled_taxonomies', 'ad_cat_tax_img_basic_settings' );
// get all image field enabled devices
$enabled_devices = ad_cat_tax_img_get_option( 'enabled_devices', 'ad_cat_tax_img_advanced_settings' );
//check if any taxonomy enabled
if (!empty( $enabled_taxonomies )) {
$device_image_url = get_term_meta( $term_id, 'tax_image_url_universal', true );
//check if any device enabled
if (!empty( $enabled_devices )) {
// registed custom image field for each enabled devices
foreach ( $enabled_devices as $enabled_device ) {
// available devices
//'android' => 'Android',
//'ios' => 'iOS (Mac, iPhone, iPad, iPod)',
//'windowsph' => 'Windows Phone',
//'mobile' => 'Mobile',
//'tablet' => 'Tablet',
//'desktop' => 'Desktop'
if( $enabled_device == 'android' && $detect->isAndroidOS() ){
$device_image_url = get_term_meta( $term_id, 'tax_image_url_'.$enabled_device, true );
break; //android match found no need to check further
}
else if( $enabled_device == 'iphone' && $detect->isiOS() ){
$device_image_url = get_term_meta( $term_id, 'tax_image_url_'.$enabled_device, true );
break; //iOS match found no need to check further
}
else if( $enabled_device == 'windowsph' && $detect->version('Windows Phone') ){
$device_image_url = get_term_meta( $term_id, 'tax_image_url_'.$enabled_device, true );
break; //Windows Phone match found no need to check further
}
else if( $enabled_device == 'mobile' && $detect->isMobile() ){
$device_image_url = get_term_meta( $term_id, 'tax_image_url_'.$enabled_device, true );
break; //Any Mobile match found no need to check further
}
else if( $enabled_device == 'tablet' && $detect->isTablet() ){
$device_image_url = get_term_meta( $term_id, 'tax_image_url_'.$enabled_device, true );
break; //Any Mobile match found no need to check further
}
else if( $enabled_device == 'desktop' ){
$device_image_url = get_term_meta( $term_id, 'tax_image_url_'.$enabled_device, true );
break; //match found no need to check further
}
}
}
}else{
$device_image_url = 'Please Enable Taxonomies First!';
}
$classes = !empty($class) ? implode(' ', $class) : '';
$result = $return_img_tag ? "
" : esc_url( $device_image_url );
return !empty($device_image_url) ? $result : 'Please Upload Image First!';
}
?>