buttons.php (15514B)
1 <?php 2 3 4 function materialis_header_buttons_defaults() { 5 return materialis_mod_default( 'header_content_buttons' ); 6 } 7 8 function materialis_front_page_header_buttons_options( $section, $prefix, $priority ) { 9 materialis_add_kirki_field( array( 10 'type' => 'checkbox', 11 'settings' => 'header_content_show_buttons', 12 'label' => esc_html__( 'Show buttons', 'materialis' ), 13 'section' => $section, 14 'default' => true, 15 'priority' => $priority, 16 ) ); 17 18 materialis_add_kirki_field( array( 19 'type' => 'sidebar-button-group', 20 'settings' => 'header_content_buttons_group', 21 'label' => esc_html__( 'Options', 'materialis' ), 22 'section' => $section, 23 'priority' => $priority, 24 'choices' => apply_filters( 'materialis_header_buttons_group', array( 25 'header_content_buttons', 26 ) ), 27 'active_callback' => array( 28 array( 29 'setting' => 'header_content_show_buttons', 30 'operator' => '==', 31 'value' => true, 32 ), 33 ), 34 'in_row_with' => array( 'header_content_show_buttons' ), 35 ) ); 36 37 $companion = apply_filters( 'materialis_is_companion_installed', false ); 38 materialis_add_kirki_field( 39 array( 40 'type' => 'repeater', 41 'settings' => 'header_content_buttons', 42 'label' => esc_html__( 'Buttons', 'materialis' ), 43 'section' => $section, 44 'priority' => $priority, 45 'default' => materialis_header_buttons_defaults(), 46 'choices' => array( 47 'limit' => apply_filters( 'header_content_buttons_limit', 2 ), 48 'beforeValueSet' => $companion ? '' : 'materialis_header_content_buttons_before_set', 49 'button_defaults' => materialis_header_buttons_defaults(), 50 ), 51 'row_label' => array( 52 'type' => 'text', 53 'value' => esc_html__( 'Button', 'materialis' ), 54 ), 55 'fields' => apply_filters( 'materialis_navigation_custom_area_buttons_fields', array( 56 'label' => array( 57 'type' => $companion ? 'hidden' : 'text', 58 'label' => esc_attr__( 'Label', 'materialis' ), 59 'default' => __( 'Action Button', 'materialis' ), 60 ), 61 'url' => array( 62 'type' => $companion ? 'hidden' : 'text', 63 'label' => esc_attr__( 'Link', 'materialis' ), 64 'default' => '#', 65 ), 66 'target' => array( 67 'type' => 'hidden', 68 'label' => esc_attr__( 'Target', 'materialis' ), 69 'default' => '_self', 70 ), 71 'class' => array( 72 'type' => 'hidden', 73 'label' => esc_attr__( 'Class', 'materialis' ), 74 'default' => '', 75 ), 76 ) ), 77 'active_callback' => apply_filters( 'materialis_header_normal_buttons_active', array() ), 78 ) 79 ); 80 81 82 materialis_add_kirki_field( array( 83 'type' => 'ope-info-pro', 84 'label' => esc_html__( 'More colors and typography options available in PRO. @BTN@', 'materialis' ), 85 'section' => $section, 86 'priority' => $priority, 87 'settings' => 'header_content_typography_pro_info', 88 'default' => true, 89 'transport' => 'postMessage', 90 ) ); 91 } 92 93 function maetrialis_header_buttons_defaults_loggedout() { 94 95 $latest_posts = wp_get_recent_posts( array( 'numberposts' => 2, 'post_status' => 'publish' ) ); 96 $result = array(); 97 $classes = array( 98 'button btn-default big color1 mdc-elevation--z3', 99 'button btn-default big white outline mdc-elevation--z3', 100 ); 101 102 foreach ( $latest_posts as $id => $post ) { 103 $result[] = array( 104 'label' => get_the_title( $post['ID'] ), 105 'url' => get_post_permalink( $post['ID'] ), 106 'target' => '_self', 107 'class' => $classes[ $id ], 108 ); 109 } 110 111 return $result; 112 } 113 114 function materialis_print_header_content_main_hook() { 115 116 $content = ""; 117 $enabled = materialis_get_theme_mod( "header_content_show_buttons", true ); 118 119 if ( $enabled ) { 120 ob_start(); 121 122 $default = array(); 123 $shadow_class = ''; 124 125 126 if ( materialis_can_show_demo_content() ) { 127 $default = materialis_header_buttons_defaults(); 128 } 129 130 materialis_print_buttons_list( "header_content_buttons", $default ); 131 132 $content = ob_get_clean(); 133 $content = apply_filters( 'materialis_header_buttons_content', $content, $enabled ); 134 135 $background_enabled = materialis_get_theme_mod( 'header_content_buttons_background_enabled', false ); 136 $shadow_value = materialis_get_theme_mod( 'header_content_buttons_background_shadow', 0 ); 137 $shadow_class = ''; 138 139 if ( $background_enabled && $shadow_value ) { 140 $shadow_class .= 'mdc-elevation--z' . $shadow_value; 141 } 142 143 $content = '<div data-dynamic-mod-container class="header-buttons-wrapper ' . $shadow_class . '"><div class="remove-gutter">' . $content . '</div></div>'; 144 } 145 146 147 echo $content; 148 149 } 150 151 add_action( "materialis_print_header_content", 'materialis_print_header_content_main_hook', 1 ); 152 153 154 /* 155 template functions 156 */ 157 158 159 function materialis_buttons_list_item_mods_attr( $index, $setting ) { 160 $item_mods = materialis_buttons_list_item_mods( $index, $setting ); 161 $result = "data-theme='" . esc_attr( $item_mods['mod'] ) . "'"; 162 163 foreach ( $item_mods['atts'] as $key => $value ) { 164 $result .= " data-theme-{$key}='" . esc_attr( $value ) . "'"; 165 } 166 167 $result .= " data-dynamic-mod='true'"; 168 169 return $result; 170 } 171 172 function materialis_print_buttons_list( $setting, $default = array() ) { 173 $buttons = materialis_get_theme_mod( $setting, $default ); 174 175 if ( ! materialis_can_show_demo_content() ) { 176 $buttons_mod_content = get_theme_mod( $setting, null ); 177 if ( ! is_array( $buttons_mod_content ) ) { 178 179 if ( empty( $buttons_mod_content ) ) { 180 if ( ! materialis_is_modified() && is_front_page() ) { 181 $buttons = maetrialis_header_buttons_defaults_loggedout(); 182 } 183 } 184 } 185 } 186 187 $default_cnt = materialis_count_default_buttons( $buttons ); 188 189 190 foreach ( $buttons as $index => $button ) { 191 192 if ( $default_cnt == 2 ) { 193 $button = apply_filters( 'materialis_print_buttons_list_button', $button, $setting, $index, 0 ); 194 } elseif ( ! isset( $button['class'] ) || ! trim( $button['class'] ) ) { 195 if ( $default_cnt == 0 ) { 196 $button = apply_filters( 'materialis_print_buttons_list_button', $button, $setting, $index, 1 ); 197 } 198 if ( $default_cnt == 1 ) { 199 $button = apply_filters( 'materialis_print_buttons_list_button', $button, $setting, $index, 2 ); 200 } 201 $default_cnt ++; 202 } 203 204 $title = $button['label']; 205 $url = $button['url']; 206 $target = $button['target']; 207 $class = $button['class']; 208 209 if ( empty( $title ) ) { 210 $title = __( 'Action button', 'materialis' ); 211 } 212 213 $extraAtts = apply_filters( 'materialis_button_extra_atts', array(), $button ); 214 $extraAttsString = ""; 215 216 foreach ( $extraAtts as $key => $value ) { 217 $extraAttsString .= " {$key}='" . esc_attr( $value ) . "'"; 218 } 219 220 221 $title = html_entity_decode( $title ); 222 223 if ( is_customize_preview() ) { 224 $mod_attr = materialis_buttons_list_item_mods_attr( $index, $setting ); 225 $btn_string = '<a class="%4$s" target="%3$s" href="%1$s" ' . $mod_attr . ' ' . $extraAttsString . '>%2$s</a>'; 226 printf( $btn_string, esc_url( $url ), materialis_wp_kses_post( $title ), esc_attr( $target ), 227 esc_attr( $class ) ); 228 } else { 229 printf( '<a class="%4$s" target="%3$s" href="%1$s" ' . $extraAttsString . '>%2$s</a>', esc_url( $url ), 230 materialis_wp_kses_post( $title ), esc_attr( $target ), esc_attr( $class ) ); 231 } 232 } 233 } 234 235 function materialis_count_default_buttons( $buttons ) { 236 $defaults_cnt = 0; 237 foreach ( $buttons as $button ) { 238 if ( strpos( $button['class'], 'btn-default' ) !== false ) { 239 $defaults_cnt ++; 240 } 241 } 242 243 return $defaults_cnt; 244 } 245 246 function materialis_header_content_buttons_buttons_list_filter( $button, $setting, $index, $add_default_index ) { 247 248 if ( $setting === "header_content_buttons" ) { 249 250 $hasClass = ( isset( $button['class'] ) && trim( $button['class'] ) ); 251 252 if ( $add_default_index == 0 ) { 253 $button['class'] = $hasClass ? $button['class'] : 'button big'; 254 } else { 255 $buttonDefaults = materialis_header_buttons_defaults(); 256 257 if ( $add_default_index == 1 ) { 258 $button['class'] = $hasClass ? $button['class'] : $buttonDefaults[0]['class']; 259 } 260 if ( $add_default_index == 2 ) { 261 $button['class'] = $hasClass ? $button['class'] : $buttonDefaults[1]['class']; 262 } 263 } 264 265 } 266 267 return $button; 268 } 269 270 add_filter( 'materialis_print_buttons_list_button', 'materialis_header_content_buttons_buttons_list_filter', 10, 4 ); 271 function materialis_buttons_list_item_mods( $index, $setting ) { 272 $result = array( 273 "type" => 'data-theme', 274 "mod" => "{$setting}|$index|label", 275 "atts" => array( 276 "href" => "{$setting}|{$index}|url", 277 "target" => "{$setting}|{$index}|target", 278 "class" => "{$setting}|{$index}|class", 279 ), 280 ); 281 282 $result = apply_filters( 'materialis_buttons_list_item_mods', $result, $setting, $index ); 283 284 return $result; 285 } 286 287 add_filter( 'materialis_header_buttons_group', 'materialis_header_buttons_background_controls_group' ); 288 289 290 function materialis_header_buttons_background_controls_group( $controls ) { 291 292 $controls[] = 'header_content_buttons_background_options_separator'; 293 $controls[] = 'header_content_buttons_background_enabled'; 294 $controls[] = 'header_content_buttons_background_color'; 295 $controls[] = 'header_content_buttons_background_spacing'; 296 $controls[] = 'header_content_buttons_background_border_radius'; 297 $controls[] = 'header_content_buttons_background_border_color'; 298 $controls[] = 'header_content_buttons_background_border_thickness'; 299 $controls[] = 'header_content_buttons_background_shadow'; 300 301 return $controls; 302 } 303 304 add_filter( 'materialis_front_page_header_buttons_options_before', 'materialis_header_buttons_background_controls', 10, 305 3 ); 306 307 function materialis_header_buttons_background_controls( $section, $prefix, $priority ) { 308 $companion = apply_filters( 'materialis_is_companion_installed', false ); 309 310 $buttons_background_options_control = array( 311 'type' => 'sectionseparator', 312 'label' => esc_html__( 'Background Options', 'materialis' ), 313 'section' => $section, 314 'settings' => "header_content_buttons_background_options_separator", 315 'priority' => $priority, 316 317 ); 318 319 if ( ! $companion ) { 320 $buttons_background_options_control['partial_refresh'] = array( 321 'header_buttons' => array( 322 'selector' => ".header-buttons-wrapper .remove-gutter", 323 'render_callback' => function () { 324 return get_theme_mod( 'header_content_buttons_background_options_separator' ); 325 }, 326 ), 327 ); 328 } 329 330 materialis_add_kirki_field( $buttons_background_options_control ); 331 332 materialis_add_kirki_field( array( 333 'type' => 'checkbox', 334 'label' => esc_html__( 'Enable Background', 'materialis' ), 335 'section' => $section, 336 'settings' => 'header_content_buttons_background_enabled', 337 'priority' => $priority, 338 'default' => materialis_mod_default( "header_element_background_enabled" ), 339 ) ); 340 341 materialis_add_kirki_field( array( 342 'type' => 'color', 343 'label' => esc_html__( 'Background Color', 'materialis' ), 344 'section' => $section, 345 'settings' => 'header_content_buttons_background_color', 346 'default' => materialis_mod_default( "header_element_background_color" ), 347 'transport' => 'postMessage', 348 'priority' => $priority, 349 'choices' => array( 350 'alpha' => true, 351 ), 352 'active_callback' => array( 353 array( 354 'setting' => 'header_content_buttons_background_enabled', 355 'operator' => '==', 356 'value' => true, 357 ), 358 ), 359 "output" => array( 360 array( 361 'element' => '.header-buttons-wrapper', 362 'property' => 'background', 363 ), 364 ), 365 'js_vars' => array( 366 array( 367 'element' => ".header-buttons-wrapper", 368 'function' => 'css', 369 'property' => 'background', 370 ), 371 ), 372 ) ); 373 374 materialis_add_kirki_field( array( 375 'type' => 'spacing', 376 'settings' => 'header_content_buttons_background_spacing', 377 'label' => esc_html__( 'Background Spacing', 'materialis' ), 378 'section' => $section, 379 'priority' => $priority, 380 'default' => materialis_mod_default( "header_element_background_spacing" ), 381 'transport' => 'postMessage', 382 'active_callback' => array( 383 array( 384 'setting' => 'header_content_buttons_background_enabled', 385 'operator' => '==', 386 'value' => true, 387 ), 388 ), 389 'output' => array( 390 array( 391 'element' => '.header-buttons-wrapper', 392 'property' => 'padding', 393 ), 394 ), 395 'js_vars' => array( 396 array( 397 'element' => ".header-buttons-wrapper", 398 'function' => 'style', 399 'property' => 'padding', 400 ), 401 ), 402 ) ); 403 404 materialis_add_kirki_field( array( 405 'type' => 'dimension', 406 'settings' => 'header_content_buttons_background_border_radius', 407 'label' => esc_html__( 'Border Radius', 'materialis' ), 408 'section' => $section, 409 'priority' => $priority, 410 'default' => materialis_mod_default( "header_element_background_radius", 0 ), 411 'transport' => 'postMessage', 412 'active_callback' => array( 413 array( 414 'setting' => 'header_content_buttons_background_enabled', 415 'operator' => '==', 416 'value' => true, 417 ), 418 ), 419 'output' => array( 420 array( 421 'element' => '.header-buttons-wrapper', 422 'property' => 'border-radius', 423 ), 424 ), 425 'js_vars' => array( 426 array( 427 'element' => ".header-buttons-wrapper", 428 'function' => 'style', 429 'property' => 'border-radius', 430 ), 431 ), 432 ) ); 433 434 materialis_add_kirki_field( array( 435 'type' => 'color', 436 'settings' => 'header_content_buttons_background_border_color', 437 'label' => esc_html__( 'Border Color', 'materialis' ), 438 'section' => $section, 439 'default' => materialis_mod_default( 'header_element_background_border_color' ), 440 'transport' => 'postMessage', 441 'priority' => $priority, 442 'choices' => array( 443 'alpha' => true, 444 ), 445 'active_callback' => array( 446 array( 447 'setting' => 'header_content_buttons_background_enabled', 448 'operator' => '==', 449 'value' => true, 450 ), 451 ), 452 'output' => array( 453 array( 454 'element' => '.header-buttons-wrapper', 455 'property' => 'border-color', 456 ), 457 ), 458 'js_vars' => array( 459 array( 460 'element' => '.header-buttons-wrapper', 461 'function' => 'css', 462 'property' => 'border-color', 463 ), 464 ), 465 ) ); 466 467 materialis_add_kirki_field( array( 468 'type' => 'spacing', 469 'settings' => 'header_content_buttons_background_border_thickness', 470 'label' => esc_html__( 'Background Border Thickness', 'materialis' ), 471 'section' => $section, 472 'priority' => $priority, 473 'default' => materialis_mod_default( 'header_element_background_border_thickness' ), 474 'transport' => 'postMessage', 475 'active_callback' => array( 476 array( 477 'setting' => 'header_content_buttons_background_enabled', 478 'operator' => '==', 479 'value' => true, 480 ), 481 ), 482 ) ); 483 484 materialis_add_kirki_field( array( 485 'type' => 'slider', 486 'settings' => 'header_content_buttons_background_shadow', 487 'label' => esc_html__( 'Shadow Elevation', 'materialis' ), 488 'section' => $section, 489 'priority' => $priority, 490 'default' => materialis_mod_default( "header_element_background_shadow" ), 491 'choices' => array( 492 'min' => '0', 493 'max' => '12', 494 'step' => '1', 495 ), 496 'transport' => 'postMessage', 497 'active_callback' => array( 498 array( 499 'setting' => 'header_content_buttons_background_enabled', 500 'operator' => '==', 501 'value' => true, 502 ), 503 ), 504 ) ); 505 }