Hello,
We are running quite a few Gravity Forms forms and form submissions have slowed down significantly over time.
I wrote a timer utility to check and see which part of the process was taking so much extra time and was a bit surprised to see that the actual entry creation was taking 26+ seconds per form submission.
After a bit of digging, I narrowed the culprit down ( gravity forms 1.7.6 ) to the forms_model.php file, get_default_post_title function.
Currently it looks like the following:
private static function get_default_post_title(){
global $wpdb;
$title = "Untitled";
$count = 1;
$titles = $wpdb->get_col("SELECT post_title FROM $wpdb->posts WHERE post_title like '%Untitled%'");
$titles = array_values($titles);
while(in_array($title, $titles)){
$title = "Untitled_$count";
$count++;
}
return $title;
}
With 25000 records that match, a procedural language approach ( like C, C++, php, etc. ) will take a good long while to run through the loop and find an untaken value.
It would be much better to use a set language approach ( sql ) which might look like the following:
- Grab the ID ( or title ) of the most recently added post
- Add one to the ID ( or parse the title, grab the integer, and increment it )
- Set the title to Untitled_N where N is the result of the integer found + 1
- If needed, use a small loop to check for existence of a post having that title, and increment the integer until an unused title is found
- return the resulting title
Doing things this way would save a ton of processing power and time, and the fix would be rather simple.
Obviously, it would be even better to use a custom post type for these things as that would avoid cluttering the post list with tons of drafts, allow full control over title generation ( e.g. with your own custom post type you could be certain the next unused id was available for use as a title ), etc. But that would also take a lot more work, especially to maintain backward compatibility.
Anywise, if you could take a look at that function and revise it so that it doesn't bog larger systems down, it would be much appreciated.