Advertisement
Guenni007

buttons

Dec 8th, 2020 (edited)
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.96 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Button
  4.  * with custom-alt attribute and custom-title at the anchor itself - by Guenni007
  5.  * Displays a colored button that links to any url of your choice
  6.  */
  7. if ( ! defined( 'ABSPATH' ) ) {  exit;  }    // Exit if accessed directly
  8.  
  9.  
  10. if ( ! class_exists( 'avia_sc_button' ) )
  11. {
  12.     class avia_sc_button extends aviaShortcodeTemplate
  13.     {
  14.         /**
  15.          * Create the config array for the shortcode button
  16.          */
  17.         function shortcode_insert_button()
  18.         {
  19.             $this->config['version']        = '1.0';
  20.             $this->config['self_closing']   = 'yes';
  21.  
  22.             $this->config['name']       = __( 'Button', 'avia_framework' );
  23.             $this->config['tab']        = __( 'Content Elements', 'avia_framework' );
  24.             $this->config['icon']       = AviaBuilder::$path['imagesURL'] . 'sc-button.png';
  25.             $this->config['order']      = 85;
  26.             $this->config['target']     = 'avia-target-insert';
  27.             $this->config['shortcode']  = 'av_button';
  28.             $this->config['tooltip']    = __( 'Creates a colored button', 'avia_framework' );
  29.             $this->config['tinyMCE']    = array( 'tiny_always' => true );
  30.             $this->config['preview']    = true;
  31.             $this->config['id_name']    = 'id';
  32.             $this->config['id_show']    = 'yes';
  33.         }
  34.  
  35.  
  36.         function extra_assets()
  37.         {
  38.             //load css
  39.             wp_enqueue_style( 'avia-module-button', AviaBuilder::$path['pluginUrlRoot'] . 'avia-shortcodes/buttons/buttons.css', array( 'avia-layout' ), false );
  40.         }
  41.  
  42.         /**
  43.          * Popup Elements
  44.          *
  45.          * If this function is defined in a child class the element automatically gets an edit button, that, when pressed
  46.          * opens a modal window that allows to edit the element properties
  47.          *
  48.          * @return void
  49.          */
  50.         function popup_elements()
  51.         {
  52.             $this->elements = array(
  53.                
  54.                 array(
  55.                         'type'  => 'tab_container',
  56.                         'nodescription' => true
  57.                     ),
  58.                        
  59.                 array(
  60.                         'type'  => 'tab',
  61.                         'name'  => __( 'Content', 'avia_framework' ),
  62.                         'nodescription' => true
  63.                     ),
  64.                    
  65.                     array(
  66.                             'type'          => 'template',
  67.                             'template_id'   => 'toggle_container',
  68.                             'templates_include' => array(
  69.                                                     $this->popup_key( 'content_button' ),
  70.                                                     $this->popup_key( 'advanced_link' )
  71.                                                 ),
  72.                             'nodescription' => true
  73.                         ),
  74.                
  75.                 array(
  76.                         'type'  => 'tab_close',
  77.                         'nodescription' => true
  78.                     ),
  79.  
  80.                 array(
  81.                         'type'  => 'tab',
  82.                         'name'  => __( 'Styling', 'avia_framework' ),
  83.                         'nodescription' => true
  84.                     ),
  85.                
  86.                     array(
  87.                             'type'          => 'template',
  88.                             'template_id'   => 'toggle_container',
  89.                             'templates_include' => array(
  90.                                                     $this->popup_key( 'styling_appearance' ),
  91.                                                     $this->popup_key( 'styling_colors' )
  92.                                                 ),
  93.                             'nodescription' => true
  94.                         ),
  95.                
  96.                 array(
  97.                         'type'  => 'tab_close',
  98.                         'nodescription' => true
  99.                     ),
  100.                
  101.                 array(
  102.                         'type'  => 'tab',
  103.                         'name'  => __( 'Advanced', 'avia_framework' ),
  104.                         'nodescription' => true
  105.                     ),
  106.                
  107.                     array(
  108.                             'type'  => 'toggle_container',
  109.                             'nodescription' => true
  110.                         ),
  111.                
  112.                         array( 
  113.                                 'type'          => 'template',
  114.                                 'template_id'   => 'screen_options_toggle'
  115.                             ),
  116.                
  117.                         array( 
  118.                                 'type'          => 'template',
  119.                                 'template_id'   => 'developer_options_toggle',
  120.                                 'args'          => array( 'sc' => $this )
  121.                             ),
  122.                
  123.                     array(
  124.                             'type'  => 'toggle_container_close',
  125.                             'nodescription' => true
  126.                         ),
  127.                
  128.                 array(
  129.                         'type'  => 'tab_close',
  130.                         'nodescription' => true
  131.                     ),
  132.  
  133.                 array(
  134.                         'type'  => 'tab_container_close',
  135.                         'nodescription' => true
  136.                     )
  137.                    
  138.                        
  139.                 );
  140.  
  141.         }
  142.  
  143.         /**
  144.          * Create and register templates for easier maintainance
  145.          *
  146.          * @since 4.6.4
  147.          */
  148.         protected function register_dynamic_templates()
  149.         {
  150.            
  151.             /**
  152.              * Content Tab
  153.              * ===========
  154.              */
  155.            
  156.             $c = array(
  157.                         array(
  158.                             'name'  => __( 'Button Label', 'avia_framework' ),
  159.                             'desc'  => __( 'This is the text that appears on your button.', 'avia_framework' ),
  160.                             'id'    => 'label',
  161.                             'type'  => 'input',
  162.                             'std' => __( 'Click me', 'avia_framework' )
  163.                         ),
  164.                
  165.                         array(
  166.                             'name'  => __( 'Show Button Icon', 'avia_framework' ),
  167.                             'desc'  => __( 'Should an icon be displayed at the left or right side of the button', 'avia_framework' ),
  168.                             'id'    => 'icon_select',
  169.                             'type'  => 'select',
  170.                             'std'   => 'yes',
  171.                             'subtype'   => array(
  172.                                                 __( 'No Icon', 'avia_framework' )                   => 'no',
  173.                                                 __( 'Display icon to the left', 'avia_framework' )  => 'yes' , 
  174.                                                 __( 'Display icon to the right', 'avia_framework' ) => 'yes-right-icon',
  175.                                             )
  176.                         ),
  177.                
  178.                         array( 
  179.                             'name'  => __( 'Button Icon', 'avia_framework' ),
  180.                             'desc'  => __( 'Select an icon for your Button below', 'avia_framework' ),
  181.                             'id'    => 'icon',
  182.                             'type'  => 'iconfont',
  183.                             'std'   => '',
  184.                             'required'  => array( 'icon_select', 'not_empty_and', 'no' )
  185.                             ),
  186.                
  187.                         array( 
  188.                             'name'  => __( 'Icon Visibility', 'avia_framework' ),
  189.                             'desc'  => __( 'Check to only display icon on hover', 'avia_framework' ),
  190.                             'id'    => 'icon_hover',
  191.                             'type'  => 'checkbox',
  192.                             'std'   => '',
  193.                             'required'  => array( 'icon_select', 'not_empty_and', 'no' )
  194.                         )
  195.                
  196.                 );
  197.            
  198.             $template = array(
  199.                             array( 
  200.                                 'type'          => 'template',
  201.                                 'template_id'   => 'toggle',
  202.                                 'title'         => __( 'Button', 'avia_framework' ),
  203.                                 'content'       => $c
  204.                             ),
  205.                     );
  206.            
  207.            
  208.             AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'content_button' ), $template );
  209.            
  210.            
  211.             /**
  212.              * Styling Tab
  213.              * ===========
  214.              */
  215.            
  216.             $c = array(
  217.                         array( 
  218.                             'name'  => __( 'Button Size', 'avia_framework' ),
  219.                             'desc'  => __( 'Choose the size of your button here.', 'avia_framework' ),
  220.                             'id'    => 'size',
  221.                             'type'  => 'select',
  222.                             'std'   => 'small',
  223.                             'subtype'   => array(
  224.                                                 __( 'Small', 'avia_framework' )     => 'small',
  225.                                                 __( 'Medium', 'avia_framework' )    => 'medium',
  226.                                                 __( 'Large', 'avia_framework' )     => 'large',
  227.                                                 __( 'X Large', 'avia_framework' )   => 'x-large'
  228.                                             )
  229.                         ),
  230.                            
  231.                         array( 
  232.                             'name'  => __( 'Button Position', 'avia_framework' ),
  233.                             'desc'  => __( 'Choose the alignment of your button here', 'avia_framework' ),
  234.                             'id'    => 'position',
  235.                             'type'  => 'select',
  236.                             'std'   => 'center',
  237.                             'subtype'   => array(
  238.                                                 __( 'Align Left', 'avia_framework' )    => 'left',
  239.                                                 __( 'Align Center', 'avia_framework' )  => 'center',
  240.                                                 __( 'Align Right', 'avia_framework' )   => 'right',
  241.                                             ),
  242.                             'required'  => array( 'size', 'not', 'fullwidth' )
  243.                         ),
  244.                
  245.                         array( 
  246.                             'name'  => __( 'Button Label Display', 'avia_framework' ),
  247.                             'desc'  => __( 'Select how to display the label', 'avia_framework' ),
  248.                             'id'    => 'label_display',
  249.                             'type'  => 'select',
  250.                             'std'   => '',
  251.                             'subtype'   => array(
  252.                                                 __( 'Always display', 'avia_framework' )    => '', 
  253.                                                 __( 'Display on hover', 'avia_framework' )  => 'av-button-label-on-hover',
  254.                                             )
  255.                         ),
  256.                    
  257.                         array( 
  258.                             'name'      => __( 'Button Title Attribute', 'avia_framework' ),
  259.                             'desc'      => __( 'Add a title attribute for this button.', 'avia_framework' ),
  260.                             'id'        => 'title_attr',
  261.                             'type'      => 'input',
  262.                             'required'  => array( 'label_display', 'equals', '' ),
  263.                             'std'       => ''
  264.                         ),
  265.  
  266. // new array her to have alt-attribute
  267.                         array( 
  268.                             'name'      => __( 'Button Alt Attribute', 'avia_framework' ),
  269.                             'desc'      => __( 'Add a alt attribute for this button.', 'avia_framework' ),
  270.                             'id'        => 'alt_attr',
  271.                             'type'      => 'input',
  272.                         //  'required'  => array( 'label_display', 'equals', '' ),
  273.                             'std'       => ''
  274.                         ),
  275.                    
  276.                
  277.                 );
  278.            
  279.             $template = array(
  280.                             array( 
  281.                                 'type'          => 'template',
  282.                                 'template_id'   => 'toggle',
  283.                                 'title'         => __( 'Appearance', 'avia_framework' ),
  284.                                 'content'       => $c
  285.                             ),
  286.                     );
  287.            
  288.             AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'styling_appearance' ), $template );
  289.            
  290.             $c = array(
  291.                
  292.                         array( 
  293.                             'name'  => __( 'Button Colors Selection', 'avia_framework' ),
  294.                             'desc'  => __( 'Select the available options for button colors. Switching to advanced options for already existing buttons you need to set all options (color settings from basic options are ignored).', 'avia_framework' ),
  295.                             'id'    => 'color_options',
  296.                             'type'  => 'select',
  297.                             'std'   => '',
  298.                             'subtype'   => array(
  299.                                                 __( 'Basic options only', 'avia_framework' )    => '', 
  300.                                                 __( 'Advanced options', 'avia_framework' )      => 'color_options_advanced',
  301.                                             )
  302.                         ),
  303.                
  304.                         array( 
  305.                             'type'          => 'template',
  306.                             'template_id'   => 'named_colors',
  307.                             'custom'        => true,
  308.                             'required'      => array( 'color_options', 'equals', '' )
  309.                         ),
  310.                
  311.                         array( 
  312.                             'name'  => __( 'Custom Background Color', 'avia_framework' ),
  313.                             'desc'  => __( 'Select a custom background color for your button here', 'avia_framework' ),
  314.                             'id'    => 'custom_bg',
  315.                             'type'  => 'colorpicker',
  316.                             'std'   => '#444444',
  317.                             'required'  => array( 'color', 'equals', 'custom' )
  318.                         ), 
  319.                        
  320.                         array(
  321.                             'name'  => __( 'Custom Font Color', 'avia_framework' ),
  322.                             'desc'  => __( 'Select a custom font color for your button here', 'avia_framework' ),
  323.                             'id'    => 'custom_font',
  324.                             'type'  => 'colorpicker',
  325.                             'std'   => '#ffffff',
  326.                             'required'  => array( 'color', 'equals', 'custom')
  327.                         ),
  328.                
  329.                         array( 
  330.                             'type'          => 'template',
  331.                             'template_id'   => 'button_colors',
  332.                             'color_id'      => 'btn_color',
  333.                             'custom_id'     => 'btn_custom',
  334.                             'required'      => array( 'color_options', 'not', '' )
  335.                         )
  336.                
  337.                 );
  338.            
  339.             $template = array(
  340.                             array( 
  341.                                 'type'          => 'template',
  342.                                 'template_id'   => 'toggle',
  343.                                 'title'         => __( 'Colors', 'avia_framework' ),
  344.                                 'content'       => $c
  345.                             ),
  346.                     );
  347.            
  348.             AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'styling_colors' ), $template );
  349.            
  350.            
  351.            
  352.             /**
  353.              * Advanced Tab
  354.              * ===========
  355.              */
  356.            
  357.             $c = array(
  358.                         array( 
  359.                             'type'          => 'template',
  360.                             'template_id'   => 'linkpicker_toggle',
  361.                             'name'          => __( 'Button Link?', 'avia_framework' ),
  362.                             'desc'          => __( 'Where should your button link to?', 'avia_framework' ),
  363.                             'subtypes'      => array( 'manually', 'single', 'taxonomy' ),
  364.                             'target_id'     => 'link_target'
  365.                         ),
  366.    
  367.                 );
  368.            
  369.             AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'advanced_link' ), $c );
  370.                
  371.         }
  372.  
  373.  
  374.         /**
  375.          * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
  376.          * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
  377.          * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
  378.          *
  379.          *
  380.          * @param array $params this array holds the default values for $content and $args.
  381.          * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  382.          */
  383.         function editor_element( $params )
  384.         {
  385.             /**
  386.              * Fix a bug in 4.7 and 4.7.1 renaming option id (no longer backwards comp.) - can be removed in a future version again
  387.              */
  388.             if( isset( $params['args']['linktarget'] ) )
  389.             {
  390.                 $params['args']['link_target'] = $params['args']['linktarget'];
  391.             }
  392.            
  393.             extract( av_backend_icon( $params ) ); // creates $font and $display_char if the icon was passed as param 'icon' and the font as 'font'
  394.  
  395.             $inner  = "<div class='avia_button_box avia_hidden_bg_box avia_textblock avia_textblock_style'>";
  396.             $inner .=       '<div ' . $this->class_by_arguments( 'icon_select, color, size, position', $params['args'] ) . '>';
  397.             $inner .=           '<span ' . $this->class_by_arguments( 'font', $font ) . '>';
  398.             $inner .=               "<span data-update_with='icon_fakeArg' class='avia_button_icon avia_button_icon_left'>{$display_char}</span>";
  399.             $inner .=           '</span> ';
  400.             $inner .=           "<span data-update_with='label' class='avia_iconbox_title' >{$params['args']['label']}</span> ";
  401.             $inner .=           '<span ' . $this->class_by_arguments( 'font', $font ) . '>';
  402.             $inner .=               "<span data-update_with='icon_fakeArg' class='avia_button_icon avia_button_icon_right'>{$display_char}</span>";
  403.             $inner .=           '</span>';
  404.             $inner .=       '</div>';
  405.             $inner .= '</div>';
  406.  
  407.             $params['innerHtml'] = $inner;
  408.             $params['content'] = null;
  409.             $params['class'] = '';
  410.  
  411.             return $params;
  412.         }
  413.  
  414.  
  415.  
  416.         /**
  417.          * Frontend Shortcode Handler
  418.          *
  419.          * @param array $atts array of attributes
  420.          * @param string $content text within enclosing form of shortcode element
  421.          * @param string $shortcodename the shortcode found, when == callback name
  422.          * @return string $output returns the modified html string
  423.          */
  424.         function shortcode_handler( $atts, $content = '', $shortcodename = '', $meta = '' )
  425.         {
  426.             /**
  427.              * Fix a bug in 4.7 and 4.7.1 renaming option id (no longer backwards comp.) - can be removed in a future version again
  428.              */
  429.             if( isset( $atts['linktarget'] ) )
  430.             {
  431.                 $atts['link_target'] = $atts['linktarget'];
  432.             }
  433.            
  434.             extract( AviaHelper::av_mobile_sizes( $atts ) ); //return $av_font_classes, $av_title_font_classes and $av_display_classes
  435.  
  436.             $atts = shortcode_atts( array(
  437.                
  438.                             'label'         => 'Click me',
  439.                             'link'          => '',
  440.                             'link_target'   => '',
  441.                             'color'         => 'theme-color',
  442.                             'custom_bg'     => '#444444',
  443.                             'custom_font'   => '#ffffff',
  444.                             'size'          => 'small',
  445.                             'position'      => 'center',
  446.                             'icon_select'   => 'yes',
  447.                             'icon'          => '',
  448.                             'font'          => '',
  449.                             'icon_hover'    => '',
  450.                             'label_display' => '',
  451.                             'title_attr'    => '',
  452.                             'alt_attr'      => '',
  453.                
  454.                             'color_options'         => '',      //  added 4.7.5.1
  455.                             'btn_color_bg'          => 'theme-color',          
  456.                             'btn_color_bg_hover'    => 'theme-color',
  457.                             'btn_color_font'        => 'custom',
  458.                             'btn_custom_bg'         => '#444444',
  459.                             'btn_custom_bg_hover'   => '#444444',
  460.                             'btn_custom_font'       => '#ffffff',
  461. //                          'btn_color_font_hover'  => '#ffffff',
  462. //                          'btn_custom_font_hover' => '#ffffff'
  463.                            
  464.                         ), $atts, $this->config['shortcode'] );
  465.            
  466.            
  467.             if( $atts['icon_select'] == 'yes' )
  468.             {
  469.                 $atts['icon_select'] = 'yes-left-icon';
  470.             }
  471.  
  472.             $style = '';
  473.             $style_hover = '';
  474.             $data = '';
  475.             $background_hover = '';
  476.            
  477.             $display_char = av_icon( $atts['icon'], $atts['font'] );
  478.             $extraClass = $atts['icon_hover'] ? 'av-icon-on-hover' : '';
  479.            
  480.             if( '' == $atts['color_options'] )
  481.             {
  482.                 if( $atts['color'] == 'custom' )
  483.                 {
  484.                     $style .= AviaHelper::style_string( $atts, 'custom_bg', 'background-color' );
  485.                     $style .= AviaHelper::style_string( $atts, 'custom_bg', 'border-color' );
  486.                     $style .= AviaHelper::style_string( $atts, 'custom_font', 'color' );
  487.                 }
  488.                 else
  489.                 {
  490.                     $extraClass .= ' ' . $this->class_by_arguments( 'color', $atts, true );
  491.                 }
  492.             }
  493.             else        //  color_options_advanced - added 4.7.5.1
  494.             {
  495.                 if( 'custom' == $atts['btn_color_bg'] )
  496.                 {
  497.                     $style .= AviaHelper::style_string( $atts, 'btn_custom_bg', 'background-color' );
  498.                     $style .= AviaHelper::style_string( $atts, 'btn_custom_bg', 'border-color' );
  499.                 }
  500.                 else
  501.                 {
  502.                     $extraClass .= ' avia-color-' . $atts['btn_color_bg'] . ' ';
  503.                 }
  504.                
  505.                 if( 'custom' == $atts['btn_color_font'] )
  506.                 {
  507.                     $style .= AviaHelper::style_string( $atts, 'btn_custom_font', 'color' );
  508.                 }
  509.                 else
  510.                 {
  511.                     $extraClass .= ' avia-font-color-' . $atts['btn_color_font'];
  512.                 }
  513.                
  514.                 if( 'custom' == $atts['btn_color_bg_hover'] )
  515.                 {
  516.                     $style_hover = "style='background-color:{$atts['btn_custom_bg_hover']};'";
  517.                 }
  518.                
  519.                 $background_hover = "<span class='avia_button_background avia-button avia-color-" . $atts['btn_color_bg_hover'] . "' {$style_hover}></span>";
  520.             }
  521.            
  522.             $style = AviaHelper::style_string( $style );
  523.            
  524.             if( ! empty( $atts['label_display'] ) && $atts['label_display'] == 'av-button-label-on-hover' )
  525.             {
  526.                 $extraClass .= ' av-button-label-on-hover ';
  527.                 $data = 'data-avia-tooltip="' . htmlspecialchars( $atts['label'] ) . '"';
  528.                 $atts['label'] = '';
  529.             }
  530.  
  531.             if( empty( $atts['label'] ) )
  532.             {
  533.                 $extraClass .= ' av-button-notext ';
  534.             }
  535.  
  536.             $blank = AviaHelper::get_link_target( $atts['link_target'] );
  537.             $link = AviaHelper::get_url( $atts['link'] );
  538.             $link = ( ( $link == 'http://' ) || ( $link == 'manually' ) ) ? '' : $link;
  539.  
  540.             $title_attr = ! empty( $atts['title_attr'] ) && empty( $atts['label_display'] ) ? 'title="' . esc_attr( $atts['title_attr'] ) . '"' : '';
  541.            
  542. // similar ternär Operator for alt attribute          
  543.             $alt_attr = ! empty( $atts['alt_attr'] )  ? 'alt="' . esc_attr( $atts['alt_attr'] ) . '"' : '';
  544.  
  545.             $content_html = '';
  546.            
  547.             if( 'yes-left-icon' == $atts['icon_select'] )
  548.             {
  549.                 $content_html .= "<span class='avia_button_icon avia_button_icon_left ' {$display_char}></span>";
  550.             }
  551.            
  552.             $content_html .= "<span class='avia_iconbox_title' >{$atts['label']}</span>";
  553.            
  554.             if( 'yes-right-icon' == $atts['icon_select'] )
  555.             {
  556.                 $content_html .= "<span class='avia_button_icon avia_button_icon_right' {$display_char}></span>";
  557.             }
  558.  
  559.             $output  = '';
  560.             $output .=  "<a href='{$link}' {$data} {$title_attr} {$alt_attr} class='avia-button {$extraClass} {$av_display_classes} " . $this->class_by_arguments( 'icon_select, size, position', $atts, true ) . "' {$blank} {$style} >";
  561.             $output .=      $content_html;
  562.             $output .=      $background_hover;
  563.             $output .=  '</a>';
  564.  
  565.             $output =  "<div {$meta['custom_el_id']} class='avia-button-wrap avia-button-{$atts['position']} {$meta['el_class']}'>{$output}</div>";
  566.  
  567.             return $output;
  568.         }
  569.            
  570.     }
  571. }
  572.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement