| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package CategoriesImages |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Plugin Name: Categories Images |
| 8 |
* Plugin URI: https://zahlan.net/blog/categories-images/ |
| 9 |
* Description: Categories Images Plugin allow you to add an image to category or any custom term. |
| 10 |
* Author: Muhammad El Zahlan |
| 11 |
* Version: 3.3.0 |
| 12 |
* Author URI: https://zahlan.net/ |
| 13 |
* Domain Path: /languages |
| 14 |
* Text Domain: categories-images |
| 15 |
* License: GPLv2 or later |
| 16 |
*/ |
| 17 |
|
| 18 |
if (!defined('ABSPATH')) |
| 19 |
die; |
| 20 |
|
| 21 |
if (!defined('Z_PLUGIN_URL')) |
| 22 |
define('Z_PLUGIN_URL', untrailingslashit(plugins_url('', __FILE__))); |
| 23 |
|
| 24 |
if (!defined('ZCI_VERSION')) |
| 25 |
define('ZCI_VERSION', '3.3.0'); |
| 26 |
|
| 27 |
class ZCategoriesImages |
| 28 |
{ |
| 29 |
public $plugin_name; |
| 30 |
private $zci_placeholder; |
| 31 |
private static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Singleton instance |
| 35 |
*/ |
| 36 |
public static function get_instance() { |
| 37 |
if ( null == self::$instance ) { |
| 38 |
self::$instance = new self; |
| 39 |
} |
| 40 |
return self::$instance; |
| 41 |
} |
| 42 |
|
| 43 |
private function __construct() { |
| 44 |
$this->plugin_name = plugin_basename(__FILE__); |
| 45 |
|
| 46 |
// The placeholder image url |
| 47 |
$this->zci_placeholder = plugins_url('/assets/images/placeholder.png', __FILE__); |
| 48 |
add_action('init', [$this, 'zInit']); |
| 49 |
add_action('admin_init', [$this, 'zAdminInit']); |
| 50 |
|
| 51 |
// save our taxonomy image while edit or create term |
| 52 |
add_action('edit_term', [$this, 'zSaveTaxonomyImage']); |
| 53 |
add_action('create_term', [$this, 'zSaveTaxonomyImage']); |
| 54 |
|
| 55 |
// Plugin menu in admin panel |
| 56 |
add_action('admin_menu', [$this, 'zSettingsMenu']); |
| 57 |
|
| 58 |
// Settings page link in plugins list |
| 59 |
add_filter("plugin_action_links_{$this->plugin_name}", [$this, 'zSettingsLink']); |
| 60 |
|
| 61 |
// Elementor Dynamic Tag Registration |
| 62 |
add_action('elementor/dynamic_tags/register', [$this, 'zRegisterElementorTags']); |
| 63 |
|
| 64 |
// Register REST API Field |
| 65 |
add_action('rest_api_init', [$this, 'zInitRestApi']); |
| 66 |
|
| 67 |
// Enqueue frontend styles |
| 68 |
add_action('wp_enqueue_scripts', [$this, 'zPublicEnqueue']); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Abstraction for retrieving term metadata |
| 73 |
*/ |
| 74 |
public function zci_get_term_meta( $term_id, $key ) { |
| 75 |
if ( function_exists( 'get_term_meta' ) ) { |
| 76 |
return get_term_meta( $term_id, $key, true ); |
| 77 |
} else { |
| 78 |
return get_option( $key . $term_id ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Abstraction for updating term metadata |
| 84 |
*/ |
| 85 |
public function zci_update_term_meta( $term_id, $key, $value ) { |
| 86 |
if ( function_exists( 'update_term_meta' ) ) { |
| 87 |
return update_term_meta( $term_id, $key, $value ); |
| 88 |
} else { |
| 89 |
return update_option( $key . $term_id, $value ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Abstraction for deleting term metadata |
| 95 |
*/ |
| 96 |
public function zci_delete_term_meta( $term_id, $key ) { |
| 97 |
if ( function_exists( 'delete_term_meta' ) ) { |
| 98 |
return delete_term_meta( $term_id, $key ); |
| 99 |
} else { |
| 100 |
return delete_option( $key . $term_id ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
function zAdminInit() { |
| 105 |
// Migration Routine: Move data from options to term_meta if supported and not yet done |
| 106 |
if ( function_exists( 'get_term_meta' ) && ! get_option( 'zci_migrated_to_termmeta' ) ) { |
| 107 |
$taxonomies = get_taxonomies(); |
| 108 |
foreach ( $taxonomies as $taxonomy ) { |
| 109 |
$terms = get_terms( $taxonomy, ['hide_empty' => false] ); |
| 110 |
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { |
| 111 |
foreach ( $terms as $term ) { |
| 112 |
// Check for old option data |
| 113 |
$app_option_url = get_option( 'z_taxonomy_image' . $term->term_id ); |
| 114 |
$app_option_id = get_option( 'z_taxonomy_image_id' . $term->term_id ); |
| 115 |
|
| 116 |
if ( $app_option_url !== false ) { |
| 117 |
update_term_meta( $term->term_id, 'z_taxonomy_image', $app_option_url ); |
| 118 |
delete_option( 'z_taxonomy_image' . $term->term_id ); |
| 119 |
} |
| 120 |
if ( $app_option_id !== false ) { |
| 121 |
update_term_meta( $term->term_id, 'z_taxonomy_image_id', $app_option_id ); |
| 122 |
delete_option( 'z_taxonomy_image_id' . $term->term_id ); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
update_option( 'zci_migrated_to_termmeta', 1 ); |
| 128 |
} |
| 129 |
|
| 130 |
$z_taxonomies = get_taxonomies(); |
| 131 |
if (is_array($z_taxonomies)) { |
| 132 |
$zci_options = get_option('zci_options'); |
| 133 |
|
| 134 |
if (!is_array($zci_options)) |
| 135 |
$zci_options = []; |
| 136 |
|
| 137 |
if (empty($zci_options['excluded_taxonomies'])) |
| 138 |
$zci_options['excluded_taxonomies'] = []; |
| 139 |
|
| 140 |
foreach ($z_taxonomies as $z_taxonomy) { |
| 141 |
if (in_array($z_taxonomy, $zci_options['excluded_taxonomies'])) |
| 142 |
continue; |
| 143 |
add_action($z_taxonomy.'_add_form_fields', [$this, 'zAddTexonomyField']); |
| 144 |
add_action($z_taxonomy.'_edit_form_fields', [$this, 'zEditTexonomyField']); |
| 145 |
add_filter('manage_edit-'.$z_taxonomy.'_columns', [$this, 'zTaxonomyColumns']); |
| 146 |
add_filter('manage_'.$z_taxonomy.'_custom_column', [$this, 'zTaxonomyColumn'], 10, 3 ); |
| 147 |
|
| 148 |
// If tax is deleted |
| 149 |
// Note: term_meta deletes automatically when term is deleted in WP 4.4+, but we need to handle fallback. |
| 150 |
add_action("delete_{$z_taxonomy}", [$this, 'zDeleteTaxonomyData']); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Register styles and scripts |
| 155 |
if ( strpos( $_SERVER['SCRIPT_NAME'], 'edit-tags.php' ) > 0 || strpos( $_SERVER['SCRIPT_NAME'], 'term.php' ) > 0 ) { |
| 156 |
add_action('admin_enqueue_scripts', [$this, 'zAdminEnqueue']); |
| 157 |
add_action('quick_edit_custom_box', [$this, 'zQuickEditCustomBox'], 10, 3); |
| 158 |
} |
| 159 |
|
| 160 |
// Register settings |
| 161 |
register_setting('zci_options', 'zci_options'); |
| 162 |
add_settings_section('zci_settings', __('Categories Images settings', 'categories-images'), [$this, 'zSectionText'], 'zci-options'); |
| 163 |
add_settings_field('z_excluded_taxonomies', __('Excluded Taxonomies', 'categories-images'), [$this, 'zExcludedTaxonomies'], 'zci-options', 'zci_settings'); |
| 164 |
} |
| 165 |
|
| 166 |
function zInit() { |
| 167 |
// Register Shortcodes |
| 168 |
add_shortcode('z_taxonomy_image', [$this, 'z_taxonomy_image_shortcode']); |
| 169 |
add_shortcode('z_taxonomy_list', [$this, 'z_taxonomy_list_shortcode']); |
| 170 |
} |
| 171 |
|
| 172 |
function zDeleteTaxonomyData($tt_id) { |
| 173 |
// delete_term_meta handles itself, but for backward compatibility with options: |
| 174 |
if ( ! function_exists( 'delete_term_meta' ) ) { |
| 175 |
delete_option('z_taxonomy_image'.$tt_id); |
| 176 |
delete_option('z_taxonomy_image_id'.$tt_id); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
function zAdminEnqueue() { |
| 181 |
wp_enqueue_style('categories-images-admin-styles', plugins_url('/assets/css/zci-admin.css', __FILE__), [], ZCI_VERSION); |
| 182 |
wp_enqueue_script('categories-images-scripts', plugins_url('/assets/js/zci-scripts.js', __FILE__), [], ZCI_VERSION); |
| 183 |
|
| 184 |
$zci_js_config = [ |
| 185 |
'wordpress_ver' => get_bloginfo("version"), |
| 186 |
'placeholder' => $this->zci_placeholder |
| 187 |
]; |
| 188 |
wp_localize_script('categories-images-scripts', 'zci_config', $zci_js_config); |
| 189 |
} |
| 190 |
|
| 191 |
function zPublicEnqueue() { |
| 192 |
wp_enqueue_style('categories-images-styles', plugins_url('/assets/css/zci-styles.css', __FILE__), [], ZCI_VERSION); |
| 193 |
} |
| 194 |
|
| 195 |
// add image field in add form |
| 196 |
function zAddTexonomyField() { |
| 197 |
wp_enqueue_media(); |
| 198 |
|
| 199 |
$nonce = wp_create_nonce( 'zci_save_image' ); |
| 200 |
|
| 201 |
echo '<div class="form-field"> |
| 202 |
<input type="hidden" name="zci_nonce" value="' . $nonce . '" /> |
| 203 |
<input type="hidden" name="zci_taxonomy_image_id" id="zci_taxonomy_image_id" value="" /> |
| 204 |
<label for="zci_taxonomy_image">' . __('Image', 'categories-images') . '</label> |
| 205 |
<input type="text" name="zci_taxonomy_image" id="zci_taxonomy_image" value="" /> |
| 206 |
<br/> |
| 207 |
<button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button> |
| 208 |
</div>'; |
| 209 |
} |
| 210 |
|
| 211 |
// add image field in edit form |
| 212 |
function zEditTexonomyField($taxonomy) { |
| 213 |
wp_enqueue_media(); |
| 214 |
|
| 215 |
if ($this->zTaxonomyImageUrl( $taxonomy->term_id, NULL, TRUE ) == $this->zci_placeholder) { |
| 216 |
$image_url = ""; |
| 217 |
$image_id = ""; |
| 218 |
} else { |
| 219 |
$image_url = $this->zTaxonomyImageUrl( $taxonomy->term_id, NULL, TRUE ); |
| 220 |
$image_id = $this->zTaxonomyImageID( $taxonomy->term_id ); |
| 221 |
} |
| 222 |
|
| 223 |
$nonce = wp_create_nonce( 'zci_save_image' ); |
| 224 |
|
| 225 |
echo '<tr class="form-field"> |
| 226 |
<th scope="row" valign="top"><label for="zci_taxonomy_image">' . __('Image', 'categories-images') . '</label></th> |
| 227 |
<td> |
| 228 |
<input type="hidden" name="zci_nonce" value="' . $nonce . '" /> |
| 229 |
<input type="hidden" name="zci_taxonomy_image_id" id="zci_taxonomy_image_id" value="'.esc_attr($image_id).'" /><img class="zci-taxonomy-image" src="' . esc_url( $this->zTaxonomyImageUrl( $taxonomy->term_id, 'medium', TRUE ) ) . '"/><br/><input type="text" name="zci_taxonomy_image" id="zci_taxonomy_image" value="'.esc_url($image_url).'" /><br /> |
| 230 |
<button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button> |
| 231 |
<button class="z_remove_image_button button">' . __('Remove image', 'categories-images') . '</button> |
| 232 |
</td> |
| 233 |
</tr>'; |
| 234 |
} |
| 235 |
|
| 236 |
function zTaxonomyColumns( $columns ) { |
| 237 |
$new_columns = []; |
| 238 |
|
| 239 |
// Keep checkbox column if it exists |
| 240 |
if ( isset( $columns['cb'] ) ) { |
| 241 |
$new_columns['cb'] = $columns['cb']; |
| 242 |
unset( $columns['cb'] ); |
| 243 |
} |
| 244 |
|
| 245 |
// Add image column |
| 246 |
$new_columns['thumb'] = __( 'Image', 'categories-images' ); |
| 247 |
|
| 248 |
// Merge back the rest of the columns |
| 249 |
return array_merge( $new_columns, $columns ); |
| 250 |
} |
| 251 |
|
| 252 |
function zTaxonomyColumn( $columns, $column, $id ) { |
| 253 |
if ( $column == 'thumb' ) |
| 254 |
$columns = '<span><img src="' . $this->zTaxonomyImageUrl($id, 'thumbnail', TRUE) . '" alt="' . __('Thumbnail', 'categories-images') . '" class="wp-post-image" /></span>'; |
| 255 |
|
| 256 |
return $columns; |
| 257 |
} |
| 258 |
|
| 259 |
function zQuickEditCustomBox($column_name, $screen, $name) { |
| 260 |
if ($column_name == 'thumb') { |
| 261 |
$nonce = wp_create_nonce( 'zci_save_image' ); |
| 262 |
echo '<fieldset> |
| 263 |
<div class="thumb inline-edit-col"> |
| 264 |
<label> |
| 265 |
<span class="title"><img src="" alt="Thumbnail"/></span> |
| 266 |
<span class="input-text-wrap"><input type="text" name="zci_taxonomy_image" value="" class="tax_list" /></span> |
| 267 |
<span class="input-text-wrap"> |
| 268 |
<input type="hidden" name="zci_nonce" value="' . $nonce . '" /> |
| 269 |
<button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button> |
| 270 |
<button class="z_remove_image_button button">' . __('Remove image', 'categories-images') . '</button> |
| 271 |
</span> |
| 272 |
<span class="input-text-wrap"> |
| 273 |
<input type="hidden" name="zci_taxonomy_image_id" value="" /> |
| 274 |
</span> |
| 275 |
</label> |
| 276 |
</div> |
| 277 |
</fieldset>'; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
function zSaveTaxonomyImage($term_id) { |
| 282 |
// Security check |
| 283 |
if ( ! isset( $_POST['zci_nonce'] ) || ! wp_verify_nonce( $_POST['zci_nonce'], 'zci_save_image' ) ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
if(isset($_POST['zci_taxonomy_image'])) { |
| 288 |
$this->zci_update_term_meta($term_id, 'z_taxonomy_image', esc_url_raw($_POST['zci_taxonomy_image'])); |
| 289 |
} |
| 290 |
if(isset($_POST['zci_taxonomy_image_id'])) { |
| 291 |
$this->zci_update_term_meta($term_id, 'z_taxonomy_image_id', absint($_POST['zci_taxonomy_image_id'])); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
// get attachment ID by image url |
| 296 |
function zGetAttachmentIdByUrl($image_src) { |
| 297 |
global $wpdb; |
| 298 |
$query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid = %s", $image_src); |
| 299 |
$id = $wpdb->get_var($query); |
| 300 |
return (!empty($id)) ? $id : NULL; |
| 301 |
} |
| 302 |
|
| 303 |
// get attachment ID by term id |
| 304 |
function zTaxonomyImageID($term_id = NULL) { |
| 305 |
if (!$term_id) { |
| 306 |
if (is_category()) |
| 307 |
$term_id = get_query_var('cat'); |
| 308 |
elseif (is_tag()) |
| 309 |
$term_id = get_query_var('tag_id'); |
| 310 |
elseif (is_tax()) { |
| 311 |
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); |
| 312 |
$term_id = $current_term->term_id; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
return $this->zci_get_term_meta($term_id, 'z_taxonomy_image_id'); |
| 317 |
} |
| 318 |
|
| 319 |
// get taxonomy image url for the given term_id (Place holder image by default) |
| 320 |
function zTaxonomyImageUrl($term_id = NULL, $size = 'full', $return_placeholder = FALSE) { |
| 321 |
if (!$term_id) { |
| 322 |
if (is_category()) |
| 323 |
$term_id = get_query_var('cat'); |
| 324 |
elseif (is_tag()) |
| 325 |
$term_id = get_query_var('tag_id'); |
| 326 |
elseif (is_tax()) { |
| 327 |
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); |
| 328 |
$term_id = $current_term->term_id; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
$taxonomy_image_url = $this->zci_get_term_meta($term_id, 'z_taxonomy_image'); |
| 333 |
|
| 334 |
if(!empty($taxonomy_image_url)) { |
| 335 |
$attachment_id = $this->zGetAttachmentIdByUrl($taxonomy_image_url); |
| 336 |
if(empty($attachment_id)) { |
| 337 |
$attachment_id = $this->zTaxonomyImageID($term_id); |
| 338 |
} |
| 339 |
if(!empty($attachment_id)) { |
| 340 |
$taxonomy_image_url = wp_get_attachment_image_src($attachment_id, $size); |
| 341 |
$taxonomy_image_url = $taxonomy_image_url[0]; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if ($return_placeholder) |
| 346 |
return ($taxonomy_image_url != '') ? $taxonomy_image_url : $this->zci_placeholder; |
| 347 |
else |
| 348 |
return $taxonomy_image_url; |
| 349 |
} |
| 350 |
|
| 351 |
// display taxonomy image for the given term_id |
| 352 |
function zTaxonomyImage($term_id = NULL, $size = 'full', $attr = NULL, $echo = TRUE) { |
| 353 |
if (!$term_id) { |
| 354 |
if (is_category()) |
| 355 |
$term_id = get_query_var('cat'); |
| 356 |
elseif (is_tag()) |
| 357 |
$term_id = get_query_var('tag_id'); |
| 358 |
elseif (is_tax()) { |
| 359 |
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); |
| 360 |
$term_id = $current_term->term_id; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
$taxonomy_image_url = $this->zci_get_term_meta($term_id, 'z_taxonomy_image'); |
| 365 |
|
| 366 |
if(!empty($taxonomy_image_url)) { |
| 367 |
$attachment_id = $this->zGetAttachmentIdByUrl($taxonomy_image_url); |
| 368 |
if(empty($attachment_id)) { |
| 369 |
$attachment_id = $this->zTaxonomyImageID($term_id); |
| 370 |
} |
| 371 |
if(!empty($attachment_id)) |
| 372 |
$taxonomy_image = wp_get_attachment_image($attachment_id, $size, FALSE, $attr); |
| 373 |
else { |
| 374 |
$image_attr = ''; |
| 375 |
if(is_array($attr)) { |
| 376 |
if(!empty($attr['class'])) |
| 377 |
$image_attr .= ' class="'.$attr['class'].'" '; |
| 378 |
if(!empty($attr['alt'])) |
| 379 |
$image_attr .= ' alt="'.$attr['alt'].'" '; |
| 380 |
if(!empty($attr['width'])) |
| 381 |
$image_attr .= ' width="'.$attr['width'].'" '; |
| 382 |
if(!empty($attr['height'])) |
| 383 |
$image_attr .= ' height="'.$attr['height'].'" '; |
| 384 |
if(!empty($attr['title'])) |
| 385 |
$image_attr .= ' title="'.$attr['title'].'" '; |
| 386 |
} |
| 387 |
$taxonomy_image = '<img src="'.$taxonomy_image_url.'" '.$image_attr.'/>'; |
| 388 |
} |
| 389 |
} |
| 390 |
else{ |
| 391 |
$taxonomy_image = ''; |
| 392 |
} |
| 393 |
|
| 394 |
if ($echo) |
| 395 |
echo $taxonomy_image; |
| 396 |
else |
| 397 |
return $taxonomy_image; |
| 398 |
} |
| 399 |
|
| 400 |
function zSettingsMenu() { |
| 401 |
add_options_page(__('Categories Images settings', 'categories-images'), __('Categories Images', 'categories-images'), 'manage_options', 'zci_settings', [$this, 'zSettingsPage']); |
| 402 |
} |
| 403 |
|
| 404 |
// Plugin option page |
| 405 |
function zSettingsPage() { |
| 406 |
if (!current_user_can('manage_options')) |
| 407 |
wp_die(__( 'You do not have sufficient permissions to access this page.', 'categories-images')); |
| 408 |
|
| 409 |
// Enqueue admin styles for settings page if not already there |
| 410 |
wp_enqueue_style('categories-images-admin-styles', plugins_url('/assets/css/zci-admin.css', __FILE__)); |
| 411 |
|
| 412 |
require_once plugin_dir_path(__FILE__).'templates/admin.php'; |
| 413 |
} |
| 414 |
|
| 415 |
function zSettingsLink($links) { |
| 416 |
$settings_link = '<a href="options-general.php?page=zci_settings">Settings</a>'; |
| 417 |
array_push($links, $settings_link); |
| 418 |
return $links; |
| 419 |
} |
| 420 |
|
| 421 |
// Settings section description |
| 422 |
function zSectionText() { |
| 423 |
echo '<p>'.__('Please select the taxonomies you want to exclude it from Categories Images plugin', 'categories-images').'</p>'; |
| 424 |
} |
| 425 |
|
| 426 |
// Excluded taxonomies checkboxs |
| 427 |
function zExcludedTaxonomies() { |
| 428 |
$options = get_option('zci_options'); |
| 429 |
$disabled_taxonomies = ['nav_menu', 'link_category', 'post_format']; |
| 430 |
foreach (get_taxonomies() as $tax) : if (in_array($tax, $disabled_taxonomies)) continue; ?> |
| 431 |
<input type="checkbox" name="zci_options[excluded_taxonomies][<?php echo $tax ?>]" value="<?php echo $tax ?>" <?php checked(isset($options['excluded_taxonomies'][$tax])); ?> /> <?php echo $tax ;?><br /> |
| 432 |
<?php endforeach; |
| 433 |
} |
| 434 |
|
| 435 |
function activate() { |
| 436 |
// Things will happen if the plugin activated. |
| 437 |
flush_rewrite_rules(); |
| 438 |
} |
| 439 |
|
| 440 |
function deactivate() { |
| 441 |
// Things will happen if the plugin deactivated. |
| 442 |
flush_rewrite_rules(); |
| 443 |
} |
| 444 |
|
| 445 |
function zRegisterElementorTags( $dynamic_tags ) { |
| 446 |
// Include the tag class file |
| 447 |
if ( ! class_exists( 'ZCI_Elementor_Taxonomy_Image_Tag' ) ) { |
| 448 |
require_once plugin_dir_path( __FILE__ ) . 'includes/elementor-integration.php'; |
| 449 |
} |
| 450 |
|
| 451 |
// Register the tag |
| 452 |
$dynamic_tags->register( new ZCI_Elementor_Taxonomy_Image_Tag() ); |
| 453 |
} |
| 454 |
|
| 455 |
// Register field 'z_taxonomy_image_url' to the WP REST API |
| 456 |
function zInitRestApi() { |
| 457 |
$taxonomies = get_taxonomies(); |
| 458 |
$zci_options = get_option('zci_options'); |
| 459 |
if (empty($zci_options['excluded_taxonomies'])) |
| 460 |
$zci_options['excluded_taxonomies'] = []; |
| 461 |
|
| 462 |
foreach ($taxonomies as $taxonomy) { |
| 463 |
if (in_array($taxonomy, $zci_options['excluded_taxonomies'])) |
| 464 |
continue; |
| 465 |
|
| 466 |
register_rest_field($taxonomy, 'z_taxonomy_image_url', [ |
| 467 |
'get_callback' => [$this, 'zGetTermImage'], |
| 468 |
'update_callback' => null, |
| 469 |
'schema' => null, |
| 470 |
]); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
function zGetTermImage($object, $field_name, $request) { |
| 475 |
$term_id = $object['id']; |
| 476 |
$image_url = $this->zTaxonomyImageUrl($term_id, 'full', true); |
| 477 |
|
| 478 |
return $image_url; |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Shortcode [z_taxonomy_image] |
| 483 |
*/ |
| 484 |
function z_taxonomy_image_shortcode($atts) { |
| 485 |
$raw_atts = $atts; |
| 486 |
$atts = shortcode_atts([ |
| 487 |
'term_id' => '', |
| 488 |
'taxonomy' => 'category', |
| 489 |
'size' => 'full', |
| 490 |
'class' => '', |
| 491 |
'default' => null, |
| 492 |
'link' => 'no', // yes, no, custom_url |
| 493 |
'format' => 'img' // img, url |
| 494 |
], $atts); |
| 495 |
|
| 496 |
// Handle positional 'default' attribute |
| 497 |
if (is_array($raw_atts) && in_array('default', $raw_atts, true)) { |
| 498 |
$atts['default'] = ""; |
| 499 |
} |
| 500 |
|
| 501 |
$term_id = $atts['term_id']; |
| 502 |
|
| 503 |
// If no term_id, try to detect |
| 504 |
if (empty($term_id)) { |
| 505 |
$term_id = get_queried_object_id(); |
| 506 |
} |
| 507 |
|
| 508 |
// Output logic |
| 509 |
if ($atts['format'] === 'url') { |
| 510 |
$url = $this->zTaxonomyImageUrl($term_id, $atts['size'], true); |
| 511 |
if ($url == $this->zci_placeholder) { |
| 512 |
if ($atts['default'] === "") { |
| 513 |
return $this->zci_placeholder; |
| 514 |
} elseif (!empty($atts['default'])) { |
| 515 |
return $atts['default']; |
| 516 |
} elseif (is_null($atts['default'])) { |
| 517 |
return ''; // No placeholder by default |
| 518 |
} |
| 519 |
} |
| 520 |
return $url; |
| 521 |
} |
| 522 |
|
| 523 |
$image = $this->zTaxonomyImage($term_id, $atts['size'], ['class' => $atts['class']], false); |
| 524 |
|
| 525 |
// Handle empty/placeholder |
| 526 |
if (empty($image) || strpos($image, 'placeholder.png') !== false) { |
| 527 |
if ($atts['default'] === "") { |
| 528 |
$image = '<img src="' . esc_url($this->zci_placeholder) . '" class="' . esc_attr($atts['class']) . '" />'; |
| 529 |
} elseif (!empty($atts['default'])) { |
| 530 |
$image = '<img src="' . esc_url($atts['default']) . '" class="' . esc_attr($atts['class']) . '" />'; |
| 531 |
} else { |
| 532 |
$image = ''; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
// Link wrapping |
| 537 |
if ($atts['link'] === 'yes') { |
| 538 |
$term_link = get_term_link((int)$term_id, $atts['taxonomy']); |
| 539 |
if (!is_wp_error($term_link)) { |
| 540 |
$image = '<a href="' . esc_url($term_link) . '">' . $image . '</a>'; |
| 541 |
} |
| 542 |
} elseif ($atts['link'] !== 'no') { |
| 543 |
// custom URL |
| 544 |
$image = '<a href="' . esc_url($atts['link']) . '">' . $image . '</a>'; |
| 545 |
} |
| 546 |
|
| 547 |
return $image; |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Shortcode [z_taxonomy_list] |
| 552 |
*/ |
| 553 |
function z_taxonomy_list_shortcode($atts) { |
| 554 |
$atts = shortcode_atts([ |
| 555 |
'taxonomy' => 'category', |
| 556 |
'post_id' => '', |
| 557 |
'include' => '', |
| 558 |
'exclude' => '', |
| 559 |
'parent' => '', |
| 560 |
'orderby' => 'name', |
| 561 |
'order' => 'ASC', |
| 562 |
'hide_empty' => 'yes', |
| 563 |
'size' => 'full', |
| 564 |
'style' => 'list', // list, grid, inline |
| 565 |
'columns' => '3', |
| 566 |
'show_name' => 'no', |
| 567 |
'show_count' => 'no', |
| 568 |
'format' => 'img' |
| 569 |
], $atts); |
| 570 |
|
| 571 |
// Get Terms |
| 572 |
$args = [ |
| 573 |
'taxonomy' => $atts['taxonomy'], |
| 574 |
'orderby' => $atts['orderby'], |
| 575 |
'order' => $atts['order'], |
| 576 |
'hide_empty' => ($atts['hide_empty'] === 'yes'), |
| 577 |
]; |
| 578 |
|
| 579 |
if (!empty($atts['include'])) { |
| 580 |
$args['include'] = explode(',', $atts['include']); |
| 581 |
} |
| 582 |
if (!empty($atts['exclude'])) { |
| 583 |
$args['exclude'] = explode(',', $atts['exclude']); |
| 584 |
} |
| 585 |
if ($atts['parent'] !== '') { |
| 586 |
$args['parent'] = (int)$atts['parent']; |
| 587 |
} |
| 588 |
|
| 589 |
$terms = []; |
| 590 |
if (!empty($atts['post_id'])) { |
| 591 |
$pid = ($atts['post_id'] === 'current') ? get_the_ID() : (int)$atts['post_id']; |
| 592 |
$terms = get_the_terms($pid, $atts['taxonomy']); |
| 593 |
} else { |
| 594 |
$terms = get_terms($args); |
| 595 |
} |
| 596 |
|
| 597 |
if (empty($terms) || is_wp_error($terms)) { |
| 598 |
return ''; |
| 599 |
} |
| 600 |
|
| 601 |
if ($atts['format'] === 'array') { |
| 602 |
return '<pre>' . print_r($terms, true) . '</pre>'; |
| 603 |
} |
| 604 |
|
| 605 |
// CSS Classes |
| 606 |
$classes = 'zci-taxonomy-list zci-' . $atts['style']; |
| 607 |
|
| 608 |
// Grid Styles |
| 609 |
$style_attr = ''; |
| 610 |
if ($atts['style'] === 'grid') { |
| 611 |
$style_attr = 'style="--zci-columns: ' . intval($atts['columns']) . ';"'; |
| 612 |
} |
| 613 |
|
| 614 |
$output = '<ul class="' . esc_attr($classes) . '" ' . $style_attr . '>'; |
| 615 |
|
| 616 |
foreach ($terms as $term) { |
| 617 |
$image = $this->zTaxonomyImage($term->term_id, $atts['size'], ['class' => 'zci-img'], false); |
| 618 |
|
| 619 |
// Skip if no image and no name to show |
| 620 |
if (empty($image) && $atts['show_name'] !== 'yes') { |
| 621 |
continue; |
| 622 |
} |
| 623 |
|
| 624 |
$link = get_term_link($term); |
| 625 |
|
| 626 |
$output .= '<li class="zci-item">'; |
| 627 |
$output .= '<a href="' . esc_url($link) . '" class="zci-link">'; |
| 628 |
$output .= '<span class="zci-image">' . $image . '</span>'; |
| 629 |
|
| 630 |
if ($atts['show_name'] === 'yes') { |
| 631 |
$output .= '<span class="zci-term-name">' . esc_html($term->name); |
| 632 |
if ($atts['show_count'] === 'yes') { |
| 633 |
$output .= ' <span class="zci-term-count">(' . $term->count . ')</span>'; |
| 634 |
} |
| 635 |
$output .= '</span>'; |
| 636 |
} |
| 637 |
$output .= '</a>'; |
| 638 |
$output .= '</li>'; |
| 639 |
} |
| 640 |
|
| 641 |
$output .= '</ul>'; |
| 642 |
|
| 643 |
return $output; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
if (class_exists('ZCategoriesImages')) { |
| 648 |
// Instantiate the class via Singleton |
| 649 |
$z_categories_images = ZCategoriesImages::get_instance(); |
| 650 |
|
| 651 |
// After activating the plugin |
| 652 |
register_activation_hook(__FILE__, [$z_categories_images, 'activate']); |
| 653 |
|
| 654 |
// After deactivating the plugin |
| 655 |
register_deactivation_hook(__FILE__, [$z_categories_images, 'deactivate']); |
| 656 |
|
| 657 |
function z_taxonomy_image_url($term_id = NULL, $size = 'full', $return_placeholder = FALSE) { |
| 658 |
$zci = ZCategoriesImages::get_instance(); |
| 659 |
return $zci->zTaxonomyImageUrl($term_id, $size, $return_placeholder); |
| 660 |
} |
| 661 |
|
| 662 |
function z_taxonomy_image_id($term_id = NULL) { |
| 663 |
$zci = ZCategoriesImages::get_instance(); |
| 664 |
return $zci->zTaxonomyImageID($term_id); |
| 665 |
} |
| 666 |
|
| 667 |
function z_taxonomy_image($term_id = NULL, $size = 'full', $attr = NULL, $echo = TRUE) { |
| 668 |
$zci = ZCategoriesImages::get_instance(); |
| 669 |
return $zci->zTaxonomyImage($term_id, $size, $attr, $echo); |
| 670 |
} |
| 671 |
} |