• Resolved codejp3

    (@codejp3)


    @robin-w – This is a follow-up to the tinymce quote issue mentioned in the BSP 5.2.6 update video.

    I’ve narrowed it down and figured out the cause. This topic is for figuring out the best solution.

    It actually DOES affect the current 5.2.5 BSP release, and the current 6.1.1 WP release.

    PROBLEM

    The problem is that the bsp_quote.js file is written specifically for tinymce. It works great if you have either:

    2.) Visual Editor Only
    3.) Show Both Visual and Text Editors

    within “Topic/Reply Form” tab #9.

    But for option:

    1.) Text Editor Only

    Tinymce is not enqueued or used period. It enqueues only the default bbPress editor.js and WP editors.css files:

    /wp-content/plugins/bbpress/templates/default/js/editor.js
    /wp-includes/css/editor.css

    POSSIBLE SOLUTION

    Trying to keep it simple, I think a viable solution is to either:

    1.) Re-write the bsp_quote.js file so that it is pure jQuery and works regardless of which editor is enqueued (editor.js or tinymce.js)

    or

    2.) Keep bsp_quote.js as-is and write a new js file specifically for Text Editor Only, and then either enqueue bsp_quote.js for Visual/Both setting, and bsp_default_quote.js for Text Editor Only setting.

    Either solution could pull from this old/abandoned plugin for generic jQuery code:

    https://www.ads-software.com/plugins/bbpress-direct-quotes/

    Either solution would also have to handle the bsp_quotes.css styling. Currently, they’re only included within mce_css filter, and would need to be enqueued separately with any pure jQuery solution.

    Not ready to act on this yet. Just wanted a little discussion going to get your input, and perhaps include a fix in a near-future BSP release.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter codejp3

    (@codejp3)

    I went ahead and took care of this. Wasn’t as simple as I was expecting, but nothing too serious.

    Here’s the exact changes made:

    1.) /js/bsp_quote.js
    Now checks for which editor is currently visible. This handles all cases, even if both editors are active.

    jQuery(document).ready(function($) {
    	 
            $('.bsp-quote-link').click(function() {
    
                    var id = $(this).attr("href").substr(1);
    
                    var data = {
                            'action' : 'get_status_by_ajax',
                            'id' : id,
                            'security': bsp_ajax_object.quote
                    }
                    $.post(bsp_ajax_object.ajax_url, data, function(response) {
    
                            // if tinymce editor currently visible
                            if( $('.mce-tinymce.mce-container.mce-panel').is(':visible') ) {
                                    tinymce.get("bbp_reply_content").execCommand("mceInsertContent", false, response); 
                            }
    
                            // if default text editor currently visible
                            if( $('textarea#bbp_reply_content').is(':visible') ) {
                                    quote = response;
                                    replyTextfield = $("#bbp_reply_content");
                                    text = replyTextfield.val();
                                    if(jQuery.trim(text) != ''){
                                            text += "\n\n";
                                    }
                                    text += quote;
                                    replyTextfield.val(text);
                            }
    
                            // scroll to new_post form
                            location.hash = "#new-post" ;
    
                    });	        
            });  
     });

    2.) /includes/generate_css.php
    Now enqueues the bspstyle-quotes CSS file both ways – directly in tinymce (if active) and as a separate stylesheet (if text editor active). Also now includes the versioning timestamps for that file in case it gets enqueued as a separate stylesheet:

    //if quotes active	
    if ( !empty( $bsp_style_settings_quote['quote_activate'] ) ) {
     
            // determine which editor is currently being used
            // no settings specified, using default text editor
            if ( empty( $bsp_style_settings_form['Show_editorsactivate'] ) ) {
                    $text_editor = true;
                    $tinymce_editor = false;
            // settings specified, set flags based on settings value
            } else {
                    switch ( $bsp_style_settings_form['Show_editorsactivate'] ) {
                            case 0 :
                                    $text_editor = true;
                                    $tinymce_editor = false;
                                    break;
                            case 1 :
                                    $text_editor = false;
                                    $tinymce_editor = true;
                                    break;
                            case 2 :
                                    $text_editor = true;
                                    $tinymce_editor = true;
                                    break;
                    }
            }       
        
    	/* add the style sheet */
            $quotes_css = bsp_add_custom_editor_style();
            // only if quotes css file exists
            if ( $quotes_css ) {
                
                    // if tinymce being used
                    if ( $tinymce_editor )
                            add_filter( 'mce_css', 'bsp_add_custom_editor_style' );
    
                    // if default text editor being used
                    if ( $text_editor )
                            add_action( 'wp_enqueue_scripts', 'bsp_enqueue_quote_style' );
                            
            }
            
    	//and enqueue the js
    	add_action( 'wp_enqueue_scripts', 'bsp_enqueue_quote' );
    }
    
    
    //this function creates the style sheet, generated when the quotes tab is accessed.
    function generate_quote_style_css() {
    	require_once( ABSPATH . 'wp-admin/includes/file.php' );
    	global $bsp_css_location ;
    	ob_start(); // Capture all output (output buffering)
    	require ( BSP_PLUGIN_DIR . '/css/styles-quote.php' );
    	$css = ob_get_clean(); // Get generated CSS (output buffering)
            $css_filename = 'bspstyle-quotes'.( is_multisite() ? '-'.get_current_blog_id() : '' ).'.css';
            update_option( 'bsp_style_quote_generation', time(), true );
    	if ( !empty($bsp_css_location['activate css location'] ) && !empty( $bsp_css_location['location'] ) ) {
    		$location = $bsp_css_location['location'] ;
                    // if it starts with '/' -  remove
                    if ( 0 === strpos( $location, '/' ) ) {
                    $location = substr( $location, 1, strlen( $location ) ) ;
                    }
    		// if it doesn't end with a '/' add one
    		if (substr( $location, strlen( $location )-1, strlen( $location ) ) !== '/' ) {
    			$location = $location.'/' ;
    		}
    		$path = get_home_path();
    		$path = $path.'/'.$location ;
    		file_put_contents( $path.$css_filename, $css, LOCK_EX ); // Save it
    	}
    	else 
    	file_put_contents( BSP_PLUGIN_DIR . '/css/'.$css_filename, $css, LOCK_EX ); // Save it
    }
    
    function bsp_add_custom_editor_style() {
            $css_filename = 'bspstyle-quotes'.( is_multisite() ? '-'.get_current_blog_id() : '' ).'.css';
            if ( !empty( $bsp_css_location['activate css location'] ) && !empty( $bsp_css_location['location'] ) ) {
    		$location = $bsp_css_location['location'];
                    // if it starts with '/' -  remove
    		if (0 === strpos( $location, '/' ) ) {
    			$location = substr( $location, 1, strlen( $location ) ) ;
    		}
    		// if it doesn't end with a '/' add one
    		if ( substr( $location, strlen( $location )-1, strlen( $location ) ) !== '/' ) {
    			$location = $location.'/' ;
    		}
                    // return custom URL location
                    if ( file_exists( ABSPATH.'/'.$location.$css_filename ) ) {
                            $location = home_url().'/'.$location ;
                            return $location.$css_filename;
                    }
    
    	} else {
                    // return default URL location
                    if ( file_exists( plugin_dir_path( dirname( __FILE__ ) ).'css/'.$css_filename ) ) {
                            return plugins_url( 'css/'.$css_filename, dirname( __FILE__ ) );
                    }
            }
            // return false, no file found
            return false;
    }
    
    function bsp_enqueue_quote_style() {
            wp_register_style( 'bsp_quotes', bsp_add_custom_editor_style(), array(), get_option( 'bsp_style_quote_generation', time() ) );	
            wp_enqueue_style( 'bsp_quotes' );
    }
    
    function bsp_enqueue_quote() {    
            wp_enqueue_script( 'bsp_quote', plugins_url('js/bsp_quote.js',dirname( __FILE__ )), array( 'jquery' ), get_option( 'bsp_version' ) );
            wp_localize_script( 'bsp_quote', 'bsp_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ),'quote' => wp_create_nonce('get_id_content') ) );
    }
    

    The end result is that quotes are now working regardless of which editor you have active, and if you have BOTH editors active, the quotes functionality will happen based on which editor tab is CURRENTLY ACTIVE in the reply form.

    I’m including this in the 5.2.6 release. Uploading to GitHub now. Re-download with these new changes to test it out for yourself:
    https://github.com/codejp3/bbp-style-pack/archive/refs/heads/proposed-v5.2.6.zip

    Thread Starter codejp3

    (@codejp3)

    Changed the code slightly from what’s above. Instead of setting flags in the switch/case and then enqueueing based on those flags later, just combined them so there’s no flags….it just enqueues directly in the switch/case based on the active editor(s).

    Functionality is identical. Just saved about 15 lines of unnecessary code by combining.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Quotes not working with “Text Editor Only”’ is closed to new replies.