| 347 | /** |
| 348 | * Filters autoloaded options. |
| 349 | * This is particularly useful when option is set to be autoloaded in DB, |
| 350 | * but has a large size and causing performance impact. |
| 351 | * |
| 352 | * @param string $query Query being executed. |
| 353 | * |
| 354 | * @return string Filtered query to exclude certain options from autoloading. |
| 355 | */ |
| 356 | function wp_filter_autoloaded_options( $query ) { |
| 357 | global $wpdb; |
| 358 | |
| 359 | $alloptions_query = "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'"; |
| 360 | |
| 361 | // If this is not the alloptions query, return the original query. |
| 362 | if ( $query !== $alloptions_query ) { |
| 363 | return $query; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Filters the options which needs exclusion from autoloading. |
| 368 | * |
| 369 | * @since 6.1.2 |
| 370 | * |
| 371 | * @return array List of options to exlude from autoloading. |
| 372 | */ |
| 373 | $exclude_autoload_options = apply_filters( 'exlude_autoloaded_options', array( '_transient_dirsize_cache' ) ); |
| 374 | $not_in_query = implode( ', ', array_fill( 0, count( $exclude_autoload_options ), '%s' ) ); |
| 375 | $alloptions_query = $wpdb->prepare( $alloptions_query . " AND option_name NOT IN ( $not_in_query )", $exclude_autoload_options ); |
| 376 | |
| 377 | return $alloptions_query; |
| 378 | } |
| 379 | add_filter( 'query', 'wp_filter_autoloaded_options' ); |
| 380 | |