| 1 |
<?php |
| 2 |
namespace UiCoreElements\Utils; |
| 3 |
|
| 4 |
use Elementor\Controls_Manager; |
| 5 |
use Elementor\Group_Control_Typography; |
| 6 |
use UiCoreElements\Helper; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly |
| 10 |
} |
| 11 |
|
| 12 |
trait Meta_Trait { |
| 13 |
|
| 14 |
function get_meta_content_controls($product_metas = false) |
| 15 |
{ |
| 16 |
|
| 17 |
// Meta options |
| 18 |
$options = [ |
| 19 |
'none' => __( 'None', 'uicore-elements' ), |
| 20 |
]; |
| 21 |
$generic_posts = [ |
| 22 |
'author' => __( 'Author', 'uicore-elements' ), |
| 23 |
'date' => __( 'Posted Date', 'uicore-elements' ), |
| 24 |
'updated date' => __( 'Updated Date', 'uicore-elements' ), |
| 25 |
'comment' => __( 'Comments Count', 'uicore-elements' ), |
| 26 |
'reading time' => __( 'Reading Time', 'uicore-elements' ), |
| 27 |
'category' => __( 'Category', 'uicore-elements' ), |
| 28 |
'tag' => __( 'Tag', 'uicore-elements' ), |
| 29 |
]; |
| 30 |
$products = [ |
| 31 |
'product price' => __( 'Product Price', 'uicore-elements' ), |
| 32 |
'product rating' => __( 'Product Rating', 'uicore-elements' ), |
| 33 |
'product stock' => __( 'Product Stock', 'uicore-elements' ), |
| 34 |
'product category' => __( 'Product Category', 'uicore-elements' ), |
| 35 |
'product tag' => __( 'Product Tag', 'uicore-elements' ), |
| 36 |
'product attribute' => __( 'Product Attribute', 'uicore-elements' ), |
| 37 |
'product sale' => __( 'Product Sale', 'uicore-elements' ), |
| 38 |
]; |
| 39 |
$custom = [ |
| 40 |
'custom meta' => __( 'Custom Meta', 'uicore-elements' ), |
| 41 |
'custom taxonomy' => __( 'Custom Taxonomy', 'uicore-elements' ), |
| 42 |
'custom html' => __( 'Custom HTML', 'uicore-elements' ), |
| 43 |
]; |
| 44 |
$portfolio = [ |
| 45 |
'portfolio category' => __( 'Portfolio Category', 'uicore-elements' ), |
| 46 |
]; |
| 47 |
|
| 48 |
$options = $product_metas ? |
| 49 |
array_merge($options, $products, $custom) : // specific to product widgets |
| 50 |
array_merge($options, $generic_posts, $products, $portfolio, $custom); // generic posts widgets |
| 51 |
|
| 52 |
|
| 53 |
$repeater = new \Elementor\Repeater(); |
| 54 |
$repeater->add_control('type',[ |
| 55 |
'label' => __( 'Meta', 'uicore-elements' ), |
| 56 |
'type' => Controls_Manager::SELECT, |
| 57 |
'default' => '', |
| 58 |
'options' => $options |
| 59 |
] |
| 60 |
); |
| 61 |
$repeater->add_control('type_custom',[ |
| 62 |
'label' => __( 'Custom Field Name', 'uicore-elements' ), |
| 63 |
'type' => Controls_Manager::TEXT, |
| 64 |
'condition' => [ |
| 65 |
'type' => ['custom meta','custom taxonomy'] |
| 66 |
] |
| 67 |
] |
| 68 |
); |
| 69 |
$repeater->add_control('html_custom',[ |
| 70 |
'label' => __( 'Custom HTML', 'uicore-elements' ), |
| 71 |
'type' => Controls_Manager::TEXTAREA, |
| 72 |
'condition' => [ |
| 73 |
'type' => ['custom html'] |
| 74 |
] |
| 75 |
] |
| 76 |
); |
| 77 |
$repeater->add_control('date_format',[ |
| 78 |
'label' => esc_html__( 'Date Format', 'uicore-elements' ), |
| 79 |
'type' => Controls_Manager::SELECT, |
| 80 |
'default' => 'default', |
| 81 |
'options' => [ |
| 82 |
'default' => esc_html__( 'Default', 'uicore-elements' ), |
| 83 |
'F j, Y' => gmdate( 'F j, Y' ), |
| 84 |
'Y-m-d' => gmdate( 'Y-m-d' ), |
| 85 |
'm/d/Y' => gmdate( 'm/d/Y' ), |
| 86 |
'd/m/Y' => gmdate( 'd/m/Y' ), |
| 87 |
'custom' => esc_html__( 'Custom', 'uicore-elements' ), |
| 88 |
], |
| 89 |
'condition' => [ |
| 90 |
'type' => ['date','updated date'] |
| 91 |
] |
| 92 |
] |
| 93 |
); |
| 94 |
$repeater->add_control('custom_format',[ |
| 95 |
'label' => esc_html__( 'Custom Format', 'uicore-elements' ), |
| 96 |
'default' => get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), |
| 97 |
'description' => sprintf( '<a href="https://wordpress.org/documentation/article/customize-date-and-time-format/" target="_blank">%s</a>', esc_html__( 'Documentation on date and time formatting', 'uicore-elements' ) ), |
| 98 |
'condition' => [ |
| 99 |
'date_format' => 'custom', |
| 100 |
'type' => ['date','updated date'] |
| 101 |
], |
| 102 |
] |
| 103 |
); |
| 104 |
$repeater->add_control('before',[ |
| 105 |
'label' => __( 'Text Before', 'uicore-elements' ), |
| 106 |
'type' => Controls_Manager::TEXT, |
| 107 |
'condition' => [ |
| 108 |
'type!' => 'none', |
| 109 |
] |
| 110 |
] |
| 111 |
); |
| 112 |
$repeater->add_control('after',[ |
| 113 |
'label' => __( 'Text After', 'uicore-elements' ), |
| 114 |
'type' => Controls_Manager::TEXT, |
| 115 |
'condition' => [ |
| 116 |
'type!' => 'none', |
| 117 |
] |
| 118 |
] |
| 119 |
); |
| 120 |
$repeater->add_control('autor_display',[ |
| 121 |
'label' => __( 'Display Type', 'uicore-elements' ), |
| 122 |
'type' => Controls_Manager::SELECT, |
| 123 |
'default' => 'name', |
| 124 |
'options' => [ |
| 125 |
'name' => __( 'Name', 'uicore-elements' ), |
| 126 |
'full' => __( 'Avatar & Name', 'uicore-elements' ), |
| 127 |
'avatar' => __( 'Avatar', 'uicore-elements' ), |
| 128 |
], |
| 129 |
'condition' => [ |
| 130 |
'type' => 'author', |
| 131 |
], |
| 132 |
] |
| 133 |
); |
| 134 |
$repeater->add_control('icon',[ |
| 135 |
'label' => __( 'Icon', 'uicore-elements' ), |
| 136 |
'type' => Controls_Manager::ICONS, |
| 137 |
'condition' => [ |
| 138 |
'autor_display' => 'name', |
| 139 |
'type!' => 'none', |
| 140 |
], |
| 141 |
] |
| 142 |
); |
| 143 |
return $repeater->get_controls(); |
| 144 |
} |
| 145 |
|
| 146 |
function get_meta_style_controls($position = 'tb-meta') |
| 147 |
{ |
| 148 |
$this->add_group_control( |
| 149 |
Group_Control_Typography::get_type(), |
| 150 |
[ |
| 151 |
'name' => $position.'_meta_typography', |
| 152 |
'selector' => '{{WRAPPER}} .ui-e-'.$position, |
| 153 |
'separator' => 'before', |
| 154 |
] |
| 155 |
); |
| 156 |
$this->add_control( |
| 157 |
$position.'_meta_color', |
| 158 |
[ |
| 159 |
'label' => __( 'Text Color', 'uicore-elements' ), |
| 160 |
'type' => Controls_Manager::COLOR, |
| 161 |
'selectors' => [ |
| 162 |
'{{WRAPPER}} .ui-e-'.$position => 'color: {{VALUE}}', |
| 163 |
'{{WRAPPER}} .ui-e-'.$position.' svg' => 'fill: {{VALUE}}', |
| 164 |
], |
| 165 |
] |
| 166 |
); |
| 167 |
$this->add_control( |
| 168 |
$position.'_link_color', |
| 169 |
[ |
| 170 |
'label' => __( 'Link Color', 'uicore-elements' ), |
| 171 |
'type' => Controls_Manager::COLOR, |
| 172 |
'selectors' => [ |
| 173 |
'{{WRAPPER}} .ui-e-'.$position.' .ui-e-meta-item a' => 'color: {{VALUE}}', |
| 174 |
'{{WRAPPER}} .ui-e-'.$position.' .ui-e-meta-item svg' => 'fill: {{VALUE}}', |
| 175 |
], |
| 176 |
] |
| 177 |
); |
| 178 |
$this->add_control( |
| 179 |
$position.'_linkh_color', |
| 180 |
[ |
| 181 |
'label' => __( 'Link Hover Color', 'uicore-elements' ), |
| 182 |
'type' => Controls_Manager::COLOR, |
| 183 |
'selectors' => [ |
| 184 |
'{{WRAPPER}} .ui-e-'.$position.' .ui-e-meta-item a:hover' => 'color: {{VALUE}}', |
| 185 |
], |
| 186 |
] |
| 187 |
); |
| 188 |
$this->add_control( |
| 189 |
$position.'_meta_background', |
| 190 |
[ |
| 191 |
'label' => __( 'Background Color', 'uicore-elements' ), |
| 192 |
'type' => Controls_Manager::COLOR, |
| 193 |
'selectors' => [ |
| 194 |
'{{WRAPPER}} .ui-e-'.$position.' .ui-e-meta-item' => 'background-color: {{VALUE}}', |
| 195 |
|
| 196 |
], |
| 197 |
] |
| 198 |
); |
| 199 |
$this->add_control( |
| 200 |
$position.'_meta_radius', |
| 201 |
[ |
| 202 |
'label' => esc_html__('Border Radius', 'uicore-elements'), |
| 203 |
'type' => Controls_Manager::DIMENSIONS, |
| 204 |
'selectors' => [ |
| 205 |
'{{WRAPPER}} .ui-e-'.$position.' .ui-e-meta-item' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' |
| 206 |
], |
| 207 |
] |
| 208 |
); |
| 209 |
$this->add_group_control( |
| 210 |
\Elementor\Group_Control_Box_Shadow::get_type(), |
| 211 |
[ |
| 212 |
'name' => $position.'_meta_shadow', |
| 213 |
'label' => esc_html__( 'Box Shadow', 'uicore-elements' ), |
| 214 |
'selector' => '{{WRAPPER}} .ui-e-'.$position.' .ui-e-meta-item', |
| 215 |
] |
| 216 |
); |
| 217 |
$this->add_responsive_control( |
| 218 |
$position.'_meta_padding', |
| 219 |
[ |
| 220 |
'label' => esc_html__('Padding', 'uicore-elements'), |
| 221 |
'type' => Controls_Manager::DIMENSIONS, |
| 222 |
'size_units' => [ 'px', 'em', '%' ], |
| 223 |
'selectors' => [ |
| 224 |
'{{WRAPPER}} .ui-e-'.$position.' .ui-e-meta-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' |
| 225 |
] |
| 226 |
] |
| 227 |
); |
| 228 |
if($position === 'top'){ |
| 229 |
$this->add_responsive_control( |
| 230 |
$position.'_meta_margin', |
| 231 |
[ |
| 232 |
'label' => esc_html__('Margin', 'uicore-elements'), |
| 233 |
'type' => Controls_Manager::DIMENSIONS, |
| 234 |
'size_units' => [ 'px', 'em', '%' ], |
| 235 |
'selectors' => [ |
| 236 |
'{{WRAPPER}} .ui-e-'.$position => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' |
| 237 |
] |
| 238 |
] |
| 239 |
); |
| 240 |
}else{ |
| 241 |
$this->add_control($position.'_meta_margin',[ |
| 242 |
'label' => __( 'Meta Top Space', 'uicore-elements' ), |
| 243 |
'type' => Controls_Manager::SLIDER, |
| 244 |
'size_units' => [ 'em' ], |
| 245 |
'separator' => 'after', |
| 246 |
'range' => [ |
| 247 |
'px' => [ |
| 248 |
'min' => 0, |
| 249 |
'max' => 3, |
| 250 |
'step' => 0.1, |
| 251 |
], |
| 252 |
], |
| 253 |
'default' => [ |
| 254 |
'unit' => 'em', |
| 255 |
'size' => 1.2, |
| 256 |
], |
| 257 |
'selectors' => [ |
| 258 |
'{{WRAPPER}} .ui-e-'.$position => 'margin-top: {{SIZE}}em;', |
| 259 |
], |
| 260 |
] |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
$this->add_responsive_control($position.'_meta_gap',[ |
| 265 |
'label' => __( 'Items Gap', 'uicore-elements' ), |
| 266 |
'type' => Controls_Manager::SLIDER, |
| 267 |
'size_units' => [ 'px' ], |
| 268 |
'range' => [ |
| 269 |
'px' => [ |
| 270 |
'min' => 0, |
| 271 |
'max' => 30, |
| 272 |
], |
| 273 |
], |
| 274 |
'default' => [ |
| 275 |
'unit' => 'px', |
| 276 |
'size' => 8, |
| 277 |
], |
| 278 |
'selectors' => [ |
| 279 |
'{{WRAPPER}} .ui-e-'.$position.' ' => 'gap: {{SIZE}}px;', |
| 280 |
], |
| 281 |
] |
| 282 |
); |
| 283 |
$this->add_control($position.'_meta_separator',[ |
| 284 |
'label' => __( 'Separator', 'uicore-elements' ), |
| 285 |
'type' => Controls_Manager::TEXT, |
| 286 |
] |
| 287 |
); |
| 288 |
if($position === 'top'){ |
| 289 |
$this->add_control($position.'_meta_placement',[ |
| 290 |
'label' => __( 'Items placement', 'uicore-elements' ), |
| 291 |
'type' => Controls_Manager::SELECT, |
| 292 |
'default' => '', |
| 293 |
'options' => [ |
| 294 |
'start left' => __( 'Top Left', 'uicore-elements' ), |
| 295 |
'start right' => __( 'Top Right', 'uicore-elements' ), |
| 296 |
'end left' => __( 'Bottom Left', 'uicore-elements' ), |
| 297 |
'end right' => __( 'Bottom Right', 'uicore-elements' ), |
| 298 |
], |
| 299 |
'selectors' => [ |
| 300 |
'{{WRAPPER}} .ui-e-post-top-meta' => 'place-content: {{VALUE}};', |
| 301 |
], |
| 302 |
] |
| 303 |
); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
function get_meta_the_author($mode) |
| 308 |
{ |
| 309 |
global $post; |
| 310 |
$author_id = $post->post_author; |
| 311 |
|
| 312 |
// name, full, avatar |
| 313 |
if($mode === 'avatar'){ |
| 314 |
$display = '<img class="ui-e-meta-avatar" src="' . esc_url( get_avatar_url($author_id, array('size' => 100)) ) . '" />'; |
| 315 |
}elseif($mode === 'full'){ |
| 316 |
$display = '<img class="ui-e-meta-avatar" src="' . esc_url( get_avatar_url($author_id, array('size' => 100)) ) . '" /> '.get_the_author_meta('display_name', $author_id); |
| 317 |
}else{ |
| 318 |
$display = esc_html(\get_the_author_meta('display_name', $author_id)); |
| 319 |
} |
| 320 |
$link = sprintf( |
| 321 |
/* translators: 1: Author archive url, 2: Author meta title, 3: Author display name */ |
| 322 |
'<a href="%1$s" title="%2$s" rel="author">%3$s</a>', |
| 323 |
esc_url( get_author_posts_url( $author_id) ), |
| 324 |
/* translators: %s: Author's display name. */ |
| 325 |
esc_attr( sprintf( __( 'View %s’s posts', 'uicore-elements' ), esc_html($display) ) ), |
| 326 |
$display |
| 327 |
); |
| 328 |
return $link; |
| 329 |
} |
| 330 |
|
| 331 |
function get_woo_meta($type) |
| 332 |
{ |
| 333 |
global $product; |
| 334 |
|
| 335 |
if (empty($product)) { |
| 336 |
return 'No product found.'; |
| 337 |
} |
| 338 |
|
| 339 |
switch ($type) { |
| 340 |
case 'product price': |
| 341 |
return $product->get_price_html(); |
| 342 |
case 'product rating': |
| 343 |
return wc_get_rating_html( $product->get_average_rating() ); |
| 344 |
case 'product category': |
| 345 |
return get_the_term_list($product->get_id(), 'product_cat', '', ', ', ''); |
| 346 |
case 'product tag': |
| 347 |
return get_the_term_list($product->get_id(), 'product_tag', '', ', ', ''); |
| 348 |
case 'product stock' : |
| 349 |
return ( $product->managing_stock() && $product->is_type('simple') && $product->get_stock_quantity() !== null ) ? $product->get_stock_quantity() : ''; |
| 350 |
case 'product attribute': |
| 351 |
return wc_display_product_attributes($product); |
| 352 |
case 'product sale' : |
| 353 |
return $product->is_on_sale() ? esc_html__( 'Sale!', 'uicore-elements' ) : ''; |
| 354 |
default: |
| 355 |
return 'Invalid meta type.'; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
function display_meta($meta) |
| 360 |
{ |
| 361 |
|
| 362 |
if( $meta['type'] === 'none' ) |
| 363 |
return; |
| 364 |
|
| 365 |
$content = ''; |
| 366 |
$wrapper = '<div class="ui-e-meta-item">'; |
| 367 |
$type = $meta['type']; |
| 368 |
$prefix = $meta['before'] ? '<span>'.esc_html($meta['before']).'</span>' : ''; |
| 369 |
$suffix = $meta['after'] ? '<span class="ui-e-meta-after">'.esc_html($meta['after']).'</span>' : ''; |
| 370 |
|
| 371 |
ob_start(); |
| 372 |
\Elementor\Icons_Manager::render_icon( $meta['icon'], [ 'aria-hidden' => 'true', 'class'=> 'ui-e-meta-icon' ], 'span' ); |
| 373 |
$icon = ob_get_clean(); |
| 374 |
|
| 375 |
// Build content |
| 376 |
switch ($type) { |
| 377 |
case 'author': |
| 378 |
$content .= $this->get_meta_the_author($meta['autor_display']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 379 |
break; |
| 380 |
case 'date': |
| 381 |
$content .= Helper::format_date( get_the_date('U'), $meta['date_format'], $meta['custom_format']); // 'U' param returns the date in timestamp, necessary for format_date() |
| 382 |
break; |
| 383 |
case 'updated date': |
| 384 |
$content .= Helper::format_date( get_the_modified_date('U'), $meta['date_format'], $meta['custom_format']); // 'U' param returns the date in timestamp, necessary for format_date() |
| 385 |
break; |
| 386 |
case 'category': |
| 387 |
$content .= Helper::get_taxonomy('category'); |
| 388 |
break; |
| 389 |
case 'tag': |
| 390 |
$content .= Helper::get_taxonomy('post_tag'); |
| 391 |
break; |
| 392 |
case 'portfolio category': |
| 393 |
$content .= Helper::get_taxonomy('portfolio_category'); |
| 394 |
break; |
| 395 |
case 'comment': |
| 396 |
$content .= esc_html( get_comments_number() ); |
| 397 |
break; |
| 398 |
case 'custom meta': |
| 399 |
$content .= Helper::get_custom_meta($meta['type_custom']); |
| 400 |
break; |
| 401 |
case 'custom taxonomy': |
| 402 |
$content .= Helper::get_taxonomy($meta['type_custom']); |
| 403 |
break; |
| 404 |
case 'custom html': |
| 405 |
$content .= wp_kses_post($meta['html_custom']); |
| 406 |
break; |
| 407 |
case 'reading time': |
| 408 |
$content .= esc_html( Helper::get_reading_time() ); |
| 409 |
break; |
| 410 |
default: |
| 411 |
if (strpos($type, 'product') === 0) { |
| 412 |
if ($this->get_woo_meta($type) === false) return; // Abort if there's no data for this product meta |
| 413 |
$content .= wp_kses_post($this->get_woo_meta($type)); |
| 414 |
} else { |
| 415 |
echo \esc_html($type); |
| 416 |
} |
| 417 |
break; |
| 418 |
} |
| 419 |
|
| 420 |
// Only output if there's content |
| 421 |
if (!empty($content)) { |
| 422 |
echo $wrapper . Helper::esc_svg($icon) . $prefix . $content . $suffix . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 423 |
} |
| 424 |
} |
| 425 |
} |