Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
3.4k views
in Q2A Core by
edited by

In my forum I allow anonymous users to answer questions and to comment.

However, this settings leads to having CKeditor + reCaptcha loaded for each visitor and causing a lot of bandwidth (some 750 MB extra per day).

And it slows down the site by ~ 450 ms:

slow ckeditor

My intention:

I would like to display the answer and comment button and reload the site on click with the URL parameter ?state=answer (as it is implemented already) or ?state=comment (not implemented yet) - loading the CKeditor with the reload.

For the answer we have the variable $formrequested in function qa_page_q_add_a_form(). For the comment not.

Any help greatly appreciated.

Kai

 

---

Scenario / Goal:

Anonymous visitor -> no answer/comment form -> visitor clicks on answer/comment button -> a/c form gets loaded (page reload)

Logged-in User -> always display answer/comment form

That should be the implementation.

 

--

Update July 2014: Check out the new plugin of sceditor!

Q2A version: 1.5.4
by
@Scott: You should definitely implement this as an option. The "hack" needs only 3 if-statements and saves Gigabytes over Gigabytes for my forums.
by
PS: I tried to do this with an advanced theme but it does not work too unset all the script lines that are not needed, we definitely need a core change here.

* Not working:
// QUESTION page
if($this->template=='question') {
    // only answer form if logged in user or if requested via ?state=answer
    if(!isset($userid) && is_null(qa_get_state())) {
        unset($this->content['a_form']);
        foreach($this->content['script_onloads'] as $scripts) {
            foreach($scripts as $scriptline) {
                // remove all sceditor scripts
                if(strpos($scriptline, 'sceditor') !== false) {
                    unset($scriptline);
                }
            }
        };
    }
    // only comment form if logged in user or if requested
    // unset $a_view['c_form']
   
    // only comment form if logged in user or if requested
    // unset $qa_content['q_view']['c_form']
}

------

Edit: I just realised the using the sceditor plugin, the js and css files of the editor get loaded. This needs to be unset too (!)

2 Answers

+1 vote
by
selected by
 
Best answer

One night and a good sleep later (where I also got dizzy thinking about the universe...) I got the following solution:

To only load CKEditor for anonymous users if they click on answer or comment button you need to edit qa-page-question.php:

find line (around 240):
    $qa_content['a_form']=qa_page_q_add_a_form($qa_content, 'anew', $usecaptcha, $questionid, @$anewin, @$anewerrors, $formtype=='a_add', $formrequested);

enclose it with this if statement:
    if(isset($userid) || $formrequested) {
        $qa_content['a_form']=qa_page_q_add_a_form($qa_content, 'anew', $usecaptcha, $questionid, @$anewin, @$anewerrors, $formtype=='a_add', $formrequested);
    }
 

find line (around 258):
    $qa_content['q_view']['c_form']=qa_page_q_add_c_form($qa_content, $questionid, $questionid, 'c'.$questionid,
        $usecaptcha, @$cnewin[$questionid], @$cnewerrors[$questionid], $formtype=='c_add');

enclose it with this if statement:
    if(isset($userid) || $formrequested) {
        $qa_content['q_view']['c_form']=qa_page_q_add_c_form($qa_content, $questionid, $questionid, 'c'.$questionid,
            $usecaptcha, @$cnewin[$questionid], @$cnewerrors[$questionid], $formtype=='c_add');
    }
 

find line (around 360):
    $a_view['c_form']=qa_page_q_add_c_form($qa_content, $questionid, $answerid, 'c'.$answerid,
        $usecaptcha, @$cnewin[$answerid], @$cnewerrors[$answerid], $formtype=='c_add');

enclose it with this if statement:
    if(isset($userid) || $formrequested) {
        $a_view['c_form']=qa_page_q_add_c_form($qa_content, $questionid, $answerid, 'c'.$answerid,
            $usecaptcha, @$cnewin[$answerid], @$cnewerrors[$answerid], $formtype=='c_add');
    }

 Done, seems to work.

 

by
This saves me for *each* visitor about 152 KB of page load and 15 requests less (ckeditor + recaptcha).

For my 5000 visitors this means: 5000 * 152 KB = 760000 kB = 742 MB less per day.

Furthermore: 5000 * 15 requests = 75.000 server requests less per day.

Awesomeness!
by
Works very well and it is a nice and clean solution.
On my new project I will adopt it once I have some traffic.
For the moment I will show the basic editor to anonymous users,
so they see something....

EDIT:

That should be a plugin for everybody..... or in core....
by
I am glad you like the solution! ;)

I wrote gidgreen he said he might set an option in the admin settings for v1.6.3.
by
After 2 weeks of using this approach, I have to tell it is quite satisfying! My page load has reduced by 2 seconds says google analytics.

There is only one slight "bug" appearing: When the anonymous asker clicks on comment and the page reloads (with the request to show the comment form), the "best answer" button is not showing up. Have not looked into it yet.
by
I do see the the yellow best answer star in the upper right corner if the answer was marked as best already. If I try to comment a best answer or a normal answer, in both cases I do see the star on the best answer...just for info... 2 seconds is really good...
by
thanks for letting me know! =)
0 votes
by

I had another solution, showing the standard editor to anonymous users. This is that what I will use.

Thatfore one has to change in

   qa-page-question-view.php

and for editing answers in

   qa-page-question-post.php

instead of $editorname=isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_as');

 

if (qa_is_logged_in()){

$editorname=isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_as');

}else{
 
$editorname="";
 
}

 

Same for comments.

 

However Ill try Your solution now a well.

by
Thanks monk333. I tried this as well but my sceditor plugin still loads all the cs and js files. So I guess I have to change the plugin itself...
...