-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-advanced-taxonomy-image.php
598 lines (460 loc) · 21 KB
/
wp-advanced-taxonomy-image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
/**
* Plugin Name: Advanced Category and Custom Taxonomy Image
* Plugin URI: https://wordpress.org/plugins/advanced-category-and-custom-taxonomy-image/
* Description: Advanced Category and Taxonomy Image Plugin allow you to add image to your category / tag / custom taxonomy for different platforms (Mobile/ Desktop/ Tablet/ Mac/ Universal etc).
* Version: 1.0.9
* Author: Sajjad Hossain Sagor
* Author URI: https://sajjadhsagor.com/
* Text Domain: advanced-category-and-custom-taxonomy-image
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
define( 'AD_CAT_TAX_IMG_ROOT_DIR', dirname( __FILE__ ) ); // Plugin root dir
define( 'AD_CAT_TAX_IMG_ROOT_URL', plugin_dir_url( __FILE__ ) ); // Plugin root url
define( 'AD_CAT_TAX_IMG_VERSION', '1.0.9' ); // Plugin current version
// add settings api wrapper
require AD_CAT_TAX_IMG_ROOT_DIR . '/includes/class.settings-api.php';
/**
* Category & Taxonomy Settings Class
*
* @author Sajjad Hossain Sagor
*/
class AD_CAT_TAX_IMG_SETTINGS
{
private $settings_api;
function __construct()
{
$this->settings_api = new WpNinjaCoder_Settings_API;
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
}
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-and-custom-taxonomy-image' ), __( 'Advanced Category & Taxonomy Image', 'advanced-category-and-custom-taxonomy-image' ), 'manage_options', 'advanced-cat-tax-image', array( $this, 'render_advanced_taxonomy_image_settings' ) );
}
public function load_plugin_textdomain()
{
load_plugin_textdomain( 'advanced-category-and-custom-taxonomy-image', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
public function get_settings_sections()
{
$sections = array(
array(
'id' => 'ad_cat_tax_img_basic_settings',
'title' => __( 'General', 'advanced-category-and-custom-taxonomy-image' )
),
array(
'id' => 'ad_cat_tax_img_advanced_settings',
'title' => __( 'Advanced ', 'advanced-category-and-custom-taxonomy-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', 'advanced-category-and-custom-taxonomy-image' ),
'desc' => __( 'Please Select Taxonomies You Want To Include Custom Image', 'advanced-category-and-custom-taxonomy-image' ),
'type' => 'multicheck',
'options' => $this->get_all_taxonomies()
)
),
'ad_cat_tax_img_advanced_settings' => array(
array(
'name' => 'enabled_devices',
'label' => __( 'Enable Device Filter', 'advanced-category-and-custom-taxonomy-image' ),
'desc' => __( 'Please Select Device Type You Want To Use Enable For', 'advanced-category-and-custom-taxonomy-image' ),
'type' => 'multicheck',
'options' => array(
'any' => __( 'Any', 'advanced-category-and-custom-taxonomy-image' ),
'android' => __( 'Android', 'advanced-category-and-custom-taxonomy-image' ),
'ios' => __( 'iOS (Mac | iPhone | iPad | iPod)', 'advanced-category-and-custom-taxonomy-image' ),
'windowsph' => __( 'Windows Phone', 'advanced-category-and-custom-taxonomy-image' ),
'mobile' => __( 'Mobile (Any)', 'advanced-category-and-custom-taxonomy-image' ),
'tablet' => __( 'Tablet', 'advanced-category-and-custom-taxonomy-image' ),
'desktop' => __( 'Desktop', 'advanced-category-and-custom-taxonomy-image' ),
),
)
)
);
return $settings_fields;
}
/**
* Render settings fields
*
*/
public function render_advanced_taxonomy_image_settings()
{
echo '<div class="wrap">';
$this->settings_api->show_navigation();
$this->settings_api->show_forms();
echo '</div>';
}
/**
* 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',
'wp_theme',
'wp_template_part_area',
);
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(), AD_CAT_TAX_IMG_VERSION, 'all' );
wp_enqueue_script( 'ad_cat_tax_img_custom_script', AD_CAT_TAX_IMG_ROOT_URL . 'assets/js/script.js', array( 'jquery' ), AD_CAT_TAX_IMG_VERSION, 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( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=advanced-cat-tax-image' ), __( 'Settings', 'advanced-category-and-custom-taxonomy-image' ) );
$plugin_actions[] = sprintf( '<a href="%s">%s</a>', 'https://wordpress.org/plugins/advanced-category-and-custom-taxonomy-image/#description-header', __( 'Documentation', 'advanced-category-and-custom-taxonomy-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', 'advanced-category-and-custom-taxonomy-image' );
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 )
{
return ad_tax_image_available( $term_id ) ? '<code>echo get_taxonomy_image( ' . $term_id . ', true, "your-custom-class-list-seperated-by-space" );<br><br>echo do_shortcode( \'[ad_tax_image term_id="' . $term_id . '" return_img_tag="true" class="your-custom-class-list-seperated-by-space"]\' );</code>' : '';
}
return '';
}, 10, 3 );
// register all enabled taxonomy to add taxonomy field
add_action( $enabled_taxonomy . '_add_form_fields', function()
{
$label = __( 'Choose File', 'advanced-category-and-custom-taxonomy-image' );
// get all image field enabled devices
$enabled_devices = ad_cat_tax_img_get_option( 'enabled_devices', 'advanced-category-and-custom-taxonomy-image' );
//check if any device enabled
if ( ! empty( $enabled_devices ) )
{
$html = '<div class="form-field"> <label for="tax_image_url_universal">' . __( 'Taxonomy Image For All Devices', 'advanced-category-and-custom-taxonomy-image' ) . '</label>';
$html .= '<input type="text" class="tax_image_upload wpsa-url" id="tax_image_url_universal" name="tax_image_url[tax_image_url_universal]" value=""/>';
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
$html .= '<p class="description">' . __( 'Choose Image To Show For All Devices', 'advanced-category-and-custom-taxonomy-image' ) . '</p>';
$html .= '</div>';
echo $html;
// registed custom image field for each enabled devices
foreach ( $enabled_devices as $enabled_device )
{
$html = '<div class="form-field"> <label for="tax_image_url_' . $enabled_device . '">' . __( 'Taxonomy Image For ', 'advanced-category-and-custom-taxonomy-image' ) . ucwords( $enabled_device ) . '</label>';
$html .= '<input type="text" class="tax_image_upload wpsa-url" id="tax_image_url_' . $enabled_device . '" name="tax_image_url[tax_image_url_' . $enabled_device . ']" value=""/>';
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
$html .= '<p class="description">' . __( 'Choose Image To Show For ', 'advanced-category-and-custom-taxonomy-image' ) . ucwords( $enabled_device ) . '</p>';
$html .= '</div>';
echo $html;
}
}
else
{
$html = '<div class="form-field"><label for="tax_image_url_universal">' . __( 'Taxonomy Image For All Devices', 'advanced-category-and-custom-taxonomy-image' ) . '</label>';
$html .= '<input type="text" class="tax_image_upload wpsa-url" id="tax_image_url_universal" name="tax_image_url[tax_image_url_universal]" value=""/>';
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
$html .= '<p class="description">' . __( 'Choose Image To Show For All Devices', 'advanced-category-and-custom-taxonomy-image' ) . '</p>';
$html .= '</div>';
echo $html;
}
} );
add_action( $enabled_taxonomy . '_edit_form_fields', function( $taxonomy )
{
$label = __( 'Choose File', 'advanced-category-and-custom-taxonomy-image' );
// 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 != '' ? '<img src="' . $universal_image_url . '" width="100" height="100"/>' : '';
$html = '<tr class="form-field">';
$html .= '<th scope="row" valign="top"><label for="tax_image_url_universal">' . __( 'Taxonomy Image For All Devices', 'advanced-category-and-custom-taxonomy-image' ) . '</label></th><td>';
$html .= $image;
$html .= '<br />';
$html .= '<input type="text" class="tax_image_upload wpsa-url" id="tax_image_url_universal" name="tax_image_url[tax_image_url_universal]" value="' . $universal_image_url . '"/>';
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
$html .= '<p class="description">' . __( 'Choose Image To Show For All Devices', 'advanced-category-and-custom-taxonomy-image' ) . '</p></td></tr>';
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 != '' ? '<img src="' . $device_image_url . '" width="100" height="100"/>' : '';
$html = '<tr class="form-field">';
$html .= '<th scope="row" valign="top"><label for="tax_image_url_' . $enabled_device . '">' . __( 'Taxonomy Image For ', 'advanced-category-and-custom-taxonomy-image' ) . ucwords( $enabled_device ) . '</label></th><td>';
$html .= $image;
$html .= '<br />';
$html .= '<input type="text" class="tax_image_upload wpsa-url" id="tax_image_url_' . $enabled_device . '" name="tax_image_url[tax_image_url_' . $enabled_device . ']" value="' . $device_image_url . '"/>';
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
$html .= '<p class="description">' . __( 'Choose Image To Show For ', 'advanced-category-and-custom-taxonomy-image' ) . ucwords( $enabled_device ) . '</p>';
echo $html;
}
}
else
{
$universal_image_url = esc_url( get_term_meta( $taxonomy->term_id, 'tax_image_url_universal', true ) );
$image = $universal_image_url != '' ? '<img src="' . $universal_image_url . '" width="100" height="100"/>' : '';
$html = '<tr class="form-field">';
$html .= '<th scope="row" valign="top"><label for="tax_image_url_universal">' . __( 'Taxonomy Image For All Devices', 'advanced-category-and-custom-taxonomy-image' ) . '</label></th><td>';
$html .= $image;
$html .= '<br />';
$html .= '<input type="text" class="tax_image_upload wpsa-url" id="tax_image_url_universal" name="tax_image_url[tax_image_url_universal]" value="' . $universal_image_url . '"/>';
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
$html .= '<p class="description">' . __( 'Choose Image To Show For All Devices', 'advanced-category-and-custom-taxonomy-image' ) . '</p></td></tr>';
echo $html;
}
} );
}
}
}
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_once 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 == 'all' )
{
$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 == '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!', 'advanced-category-and-custom-taxonomy-image' );
}
$classes = ! empty( $class ) ? implode( ' ', $class ) : '';
$result = $return_img_tag ? "<img src='" . esc_url( $device_image_url ) . "' class='" . $classes . "'>" : esc_url( $device_image_url );
return ! empty( $device_image_url ) ? $result : __( 'Please Upload Image First!', 'advanced-category-and-custom-taxonomy-image' );
}
// checks if taxonomy image is available for any device
function ad_tax_image_available( $term_id = '' )
{
if ( empty( $term_id ) && ! $term_id ) return false;
// 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 == 'all' )
{
$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 == 'android' )
{
$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' )
{
$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' )
{
$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' )
{
$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' )
{
$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
{
return false;
}
return ! empty( $device_image_url ) ? true : false;
}
add_shortcode( 'ad_tax_image', function( $atts )
{
$atts = shortcode_atts( array(
'term_id' => '',
'class' => '',
'return_img_tag' => false,
), $atts );
return get_taxonomy_image( $atts['term_id'], filter_var( $atts['return_img_tag'], FILTER_VALIDATE_BOOLEAN ), explode( ' ', $atts['class'] ) );
} );