|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Helper functions used for Object Cache Support Info. |
| 4 | + * |
| 5 | + * @package performance-lab |
| 6 | + * @since 2.1.0 |
| 7 | + */ |
| 8 | + |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { |
| 10 | + exit; // Exit if accessed directly. |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Callback for Object Cache Info fields. |
| 15 | + * |
| 16 | + * @return array Fields. |
| 17 | + * @since 3.3.0 |
| 18 | + */ |
| 19 | +function object_cache_supported_fields() { |
| 20 | + return array( |
| 21 | + 'extension' => array( |
| 22 | + 'label' => __( 'Extension', 'performance-lab' ), |
| 23 | + 'value' => wp_get_cache_type(), |
| 24 | + ), |
| 25 | + 'multiple_gets' => array( |
| 26 | + 'label' => __( 'Multiple gets', 'performance-lab' ), |
| 27 | + 'value' => wp_cache_supports( 'get_multiple' ) ? 'Enabled' : 'Disabled', |
| 28 | + ), |
| 29 | + 'multiple_sets' => array( |
| 30 | + 'label' => __( 'Multiple sets', 'performance-lab' ), |
| 31 | + 'value' => wp_cache_supports( 'set_multiple' ) ? 'Enabled' : 'Disabled', |
| 32 | + ), |
| 33 | + 'multiple_deletes' => array( |
| 34 | + 'label' => __( 'Multiple deletes', 'performance-lab' ), |
| 35 | + 'value' => wp_cache_supports( 'delete_multiple' ) ? 'Enabled' : 'Disabled', |
| 36 | + ), |
| 37 | + 'flush_group' => array( |
| 38 | + 'label' => __( 'Flush group', 'performance-lab' ), |
| 39 | + 'value' => wp_cache_supports( 'flush_group' ) ? 'Enabled' : 'Disabled', |
| 40 | + ), |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Attempts to determine which object cache is being used. |
| 46 | + * |
| 47 | + * Note that the guesses made by this function are based on the WP_Object_Cache classes |
| 48 | + * that define the 3rd party object cache extension. Changes to those classes could render |
| 49 | + * problems with this function's ability to determine which object cache is being used. |
| 50 | + * |
| 51 | + * @return string |
| 52 | + * @since 3.3.0 |
| 53 | + */ |
| 54 | +function wp_get_cache_type() { |
| 55 | + global $_wp_using_ext_object_cache, $wp_object_cache; |
| 56 | + |
| 57 | + if ( ! empty( $_wp_using_ext_object_cache ) ) { |
| 58 | + // Test for Memcached PECL extension memcached object cache (https://github.com/tollmanz/wordpress-memcached-backend) |
| 59 | + if ( isset( $wp_object_cache->m ) && $wp_object_cache->m instanceof \Memcached ) { |
| 60 | + $message = 'Memcached'; |
| 61 | + |
| 62 | + // Test for Memcache PECL extension memcached object cache (https://wordpress.org/extend/plugins/memcached/) |
| 63 | + } elseif ( isset( $wp_object_cache->mc ) ) { |
| 64 | + $is_memcache = true; |
| 65 | + foreach ( $wp_object_cache->mc as $bucket ) { |
| 66 | + if ( ! $bucket instanceof \Memcache && ! $bucket instanceof \Memcached ) { |
| 67 | + $is_memcache = false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if ( $is_memcache ) { |
| 72 | + $message = 'Memcache'; |
| 73 | + } |
| 74 | + |
| 75 | + // Test for Xcache object cache (https://plugins.svn.wordpress.org/xcache/trunk/object-cache.php) |
| 76 | + } elseif ( $wp_object_cache instanceof \XCache_Object_Cache ) { |
| 77 | + $message = 'Xcache'; |
| 78 | + |
| 79 | + // Test for WinCache object cache (https://wordpress.org/extend/plugins/wincache-object-cache-backend/) |
| 80 | + } elseif ( class_exists( 'WinCache_Object_Cache' ) ) { |
| 81 | + $message = 'WinCache'; |
| 82 | + |
| 83 | + // Test for APC object cache (https://wordpress.org/extend/plugins/apc/) |
| 84 | + } elseif ( class_exists( 'APC_Object_Cache' ) ) { |
| 85 | + $message = 'APC'; |
| 86 | + |
| 87 | + // Test for WP Redis (https://wordpress.org/plugins/wp-redis/) |
| 88 | + } elseif ( isset( $wp_object_cache->redis ) && $wp_object_cache->redis instanceof \Redis ) { |
| 89 | + $message = 'Redis'; |
| 90 | + |
| 91 | + // Test for Redis Object Cache (https://wordpress.org/plugins/redis-cache/) |
| 92 | + } elseif ( method_exists( $wp_object_cache, 'redis_instance' ) && method_exists( $wp_object_cache, |
| 93 | + 'redis_status' ) ) { |
| 94 | + $message = 'Redis'; |
| 95 | + |
| 96 | + // Test for Object Cache Pro (https://objectcache.pro/) |
| 97 | + } elseif ( method_exists( $wp_object_cache, 'config' ) && method_exists( $wp_object_cache, 'connection' ) ) { |
| 98 | + $message = 'Redis'; |
| 99 | + |
| 100 | + // Test for WP LCache Object cache (https://github.com/lcache/wp-lcache) |
| 101 | + } elseif ( isset( $wp_object_cache->lcache ) && $wp_object_cache->lcache instanceof \LCache\Integrated ) { |
| 102 | + $message = 'WP LCache'; |
| 103 | + |
| 104 | + } elseif ( function_exists( 'w3_instance' ) ) { |
| 105 | + $config = w3_instance( 'W3_Config' ); |
| 106 | + $message = 'Unknown'; |
| 107 | + |
| 108 | + if ( $config->get_boolean( 'objectcache.enabled' ) ) { |
| 109 | + $message = 'W3TC ' . $config->get_string( 'objectcache.engine' ); |
| 110 | + } |
| 111 | + } else { |
| 112 | + $message = 'Unknown'; |
| 113 | + } |
| 114 | + } else { |
| 115 | + $message = 'Disabled'; |
| 116 | + } |
| 117 | + |
| 118 | + return $message; |
| 119 | +} |
0 commit comments