Pilot-in / PiloPress

The most advanced WordPress Page Builder using Advanced Custom Fields & TailwindCSS
https://www.pilopress.com
63 stars 11 forks source link

Adding filters in `pip_button` shortcode to have more control on the html output #227

Closed barnemax-pilotin closed 1 year ago

barnemax-pilotin commented 2 years ago

Is your feature request related to a problem? Please describe. Sometimes FA icons are not enough and in some case the button has an element inside with .svg - with current shortcode it's not possible to make it happen

Describe the solution you'd like Adding some filters in the shortcode pip_button at the beginning and the end of the html output.

pip/shortcode/button/start_html_output pip/shortcode/button/end_html_output

How i would see things: https://github.com/Pilot-in/PiloPress/blob/master/includes/classes/admin/editor/class-shortcodes.php#L36

        public function pip_button( $attrs ) {

            // Parse attributes
            $attrs = shortcode_atts(
                array(
                    'text'          => false,
                    'type'          => false,
                    'alignment'     => false,
                    'target'        => false,
                    'download'      => false,
                    'download_name' => false,
                    'xclass'        => false,
                    'link'          => '',
                    'nodiv'         => false,
                    'icon'          => false,
                    'icon_position' => false,
                ),
                $attrs,
                'pip_button'
            );

            // Build class
            $btn_class  = '';
            $btn_class .= ( $attrs['type'] ) ? $attrs['type'] : '';
            $btn_class .= ( $attrs['xclass'] ) ? ' ' . $attrs['xclass'] : '';

            // Get args
            $btn_class     = esc_attr( trim( $btn_class ) );
            $alignment     = esc_attr( $attrs['alignment'] );
            $btn_link      = esc_url( $attrs['link'] );
            $target        = $attrs['target'] ? esc_attr( $attrs['target'] ) : '_blank';
            $download      = $attrs['download'];
            $download_name = $attrs['download_name'];
            $btn_text      = $attrs['text'];
            $no_div        = $attrs['nodiv'];
            $icon_class    = $attrs['icon'];
            $icon_pos      = $attrs['icon_position'];

            $html = '';

            if ( !$no_div ) {
                $html .= '<div class="' . $alignment . '">';
            }

            $download_option = '';
            if ( $download ) {
                $download_name   = $download_name ? $download_name : 'download';
                $download_option = 'download="' . $download_name . '"';
            }

            $html .= '<a href="' . $btn_link . '" class="' . $btn_class . '" target="' . $target . '" ' . $download_option . '>';
            $html  = apply_filters( 'pip/shortcode/button/start_html_output', $html, $attrs );

            if ( $icon_class ) {
                $is_rtl      = pip_is_rtl();
                $icon_margin = $icon_pos === 'left' ? ( $is_rtl ? 'ml-2' : 'mr-2' ) : ( $is_rtl ? 'mr-2' : 'ml-2' );
                $icon_margin = apply_filters( 'pip/shortcode/button/icon_margin', $icon_margin, $icon_pos, $icon_class, $is_rtl );
                $icon_html   = '<i class="' . $icon_class . ' ' . $icon_margin . '"></i>';

                switch ( $icon_pos ) {
                    case 'left':
                        $html .= $icon_html . $btn_text;
                        break;
                    case 'right':
                        $html .= $btn_text . $icon_html;
                        break;
                }

                $html = apply_filters( 'pip/shortcode/button/end_html_output', $html, $attrs );
                $html .= '</a>';
            } else {
                $html .= $btn_text;
                $html  = apply_filters( 'pip/shortcode/button/end_html_output', $html, $attrs );
                $html .= '</a>';
            }

            if ( !$no_div ) {
                $html .= '</div>';
            }

            // Render shortcode
            return do_shortcode( $html );
        }

Describe alternatives you've considered Otherwise i guess for my case the only other solution would be to override the shortcode in the project, but that's not a long term solution