Last active
January 18, 2020 03:34
-
-
Save wpmudev-sls/33c4a4366595bc30a67497c4c4508a0e to your computer and use it in GitHub Desktop.
Improve SQL query performance on meta keywords and deactive custom field metabox on product post type
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 | |
/** | |
* Plugin Name: [Smartcrawl Pro] Improve SQL query performance | |
* Description: Improve SQL query performance on meta keywords and deactive custom field metabox on product post type | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_general_improve_performance_on_admin_screen_func', 100 ); | |
function wpmudev_general_improve_performance_on_admin_screen_func() { | |
if( ! is_admin() ) return; | |
// deactive custom fields on product post type | |
if( class_exists('WooCommerce') ){ | |
add_action( 'init', 'wpmudev_remove_custom_fields_metabox_on_product_type', 999 ); | |
function wpmudev_remove_custom_fields_metabox_on_product_type(){ | |
remove_post_type_support( 'product', 'custom-fields' ); | |
} | |
} | |
// optimize SQL query analyzing meta key words | |
if ( defined( 'SMARTCRAWL_VERSION' ) && SMARTCRAWL_VERSION && class_exists('Smartcrawl_Settings') && user_can_see_seo_metabox() ) { | |
add_filter( 'get_meta_sql', 'wpmudev_smc_custom_meta_query_sql', 10, 4 ); | |
function wpmudev_smc_custom_meta_query_sql( $sql, $queries, $type, $primary_table ){ | |
if( 'post' === $type && ! empty( $queries ) && 2 < count( $queries[0] ) && strpos( $sql['where'], '_wds_focus-keywords') ){ | |
foreach( $queries[0] as $meta_query ){ | |
if( isset( $meta_query['key'] ) && '_wds_focus-keywords' !== $meta_query['key'] ){ | |
return $sql; | |
} | |
} | |
global $wpdb; | |
// custom meta_query sql | |
$meta_table = $wpdb->postmeta; | |
$sql['join'] = " INNER JOIN {$meta_table} AS mt ON ( {$primary_table}.ID = mt.post_id )"; | |
$sql_where = array(); | |
foreach( $queries[0] as $id => $meta_query ){ | |
if( ! empty( $meta_query['value'] ) ){ | |
$meta_value = trim( $meta_query['value'] ); | |
$meta_value = '%' . $wpdb->esc_like( $meta_query['value'] ) . '%'; | |
$where = $wpdb->prepare( '%s', $meta_value ); | |
$sql_where[] = "mt.meta_value LIKE {$where}"; | |
} | |
} | |
$sql['where'] = ' AND( ( '. $wpdb->prepare( "mt.meta_key = %s", '_wds_focus-keywords' ) .' AND ('; | |
$sql['where'] .= join( ' AND ', $sql_where ); | |
$sql['where'] .= ') ) )'; | |
} | |
return $sql; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment