Created
October 27, 2018 19:46
-
-
Save mikeschinkel/bba4833a61da1e23f87fe2f91a88f82e to your computer and use it in GitHub Desktop.
Fix GravityForms GFFormsModel::get_custom_field_names() for large numbers of posts and post meta by limiting to post_type='page'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Fix_Gravity_Forms | |
*/ | |
class Fix_Gravity_Forms { | |
const POST_TYPE = 'page'; | |
const SQL_MARKER = "WHERE meta_key NOT BETWEEN '_' AND '_z'"; | |
static function on_load() { | |
add_filter( | |
'gform_field_standard_settings', | |
[ __CLASS__, '_gform_field_standard_settings' ] | |
); | |
} | |
/** | |
* Wait to attach the query filter to just before we need it | |
* and remove just after we use it. | |
* | |
* @param int $number | |
*/ | |
static function _gform_field_standard_settings( $number ) { | |
switch ( $number ) { | |
case 650: | |
add_filter( 'query', [ __CLASS__, '_query' ] ); | |
break; | |
case 700: | |
remove_filter( 'query', [ __CLASS__, '_query' ] ); | |
break; | |
} | |
} | |
/** | |
* @param string $query | |
* @return string | |
*/ | |
static function _query( $query ) { | |
do { | |
if ( 'S' !== $query[ 0 ] ) { | |
break; | |
} | |
if ( false === strpos( $query, self::SQL_MARKER ) ) { | |
break; | |
} | |
global $wpdb; | |
$join_sql = " JOIN {$wpdb->posts} ON {$wpdb->posts}.ID={$wpdb->postmeta}.post_id" . | |
" AND {$wpdb->posts}.post_type='%s'"; | |
$query = str_replace( | |
self::SQL_MARKER, | |
$wpdb->prepare( $join_sql, self::POST_TYPE ) . self::SQL_MARKER, | |
$query | |
); | |
} while ( false ); | |
return $query; | |
} | |
} | |
Fix_Gravity_Forms::on_load(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment