@@ -93,7 +93,7 @@ function cache_add_instead_of_set() {
9393$ b = function () {
9494 global $ wpdb ;
9595
96- if ( ! ( $ listofthings = wp_cache_get ( $ foo ) ) ) {
96+ if ( ! ( $ listofthings = \Wp_Cache_Get ( $ foo ) ) ) {
9797 $ listofthings = $ wpdb ->get_col ( 'SELECT something FROM somewhere WHERE someotherthing = 1 ' ); // Warning.
9898 wp_cache_set ( 'foo ' , $ listofthings );
9999 }
@@ -379,3 +379,99 @@ function methodNamesSameAsCacheFunctions() {
379379
380380 return $ listofthings ;
381381}
382+
383+ /*
384+ * Safeguard correct handling of namespaced function calls. The sniff deliberately does not distinguish between calls to
385+ * WP global cache functions and calls to namespaced functions that mirror the name of the WP global cache functions as
386+ * those are likely custom cache functions. This is consistent with the behavior for method calls (see the
387+ * methodNamesSameAsCacheFunctions() test above).
388+ */
389+ function callToFullyQualifiedCacheGetSet () {
390+ global $ wpdb ;
391+
392+ if ( ! ( $ listofthings = \wp_cache_get ( $ foo ) ) ) {
393+ $ listofthings = $ wpdb ->get_col ( 'SELECT something FROM somewhere WHERE someotherthing = 1 ' ); // Warning direct DB call.
394+ \wp_cache_set ( 'foo ' , $ listofthings );
395+ }
396+
397+ return $ listofthings ;
398+ }
399+
400+ function callToQualifiedNamespacedCacheGetSet () {
401+ global $ wpdb ;
402+
403+ if ( ! ( $ listofthings = MyNamespace \wp_cache_get ( $ foo ) ) ) {
404+ $ listofthings = $ wpdb ->get_col ( 'SELECT something FROM somewhere WHERE someotherthing = 1 ' ); // Warning direct DB call.
405+ MyNamespace \wp_cache_add ( 'foo ' , $ listofthings );
406+ }
407+
408+ return $ listofthings ;
409+ }
410+
411+ function callToFullyQualifiedNamespacedCacheGetSet () {
412+ global $ wpdb ;
413+
414+ if ( ! ( $ listofthings = \MyNamespace \wp_cache_get ( $ foo ) ) ) {
415+ $ listofthings = $ wpdb ->get_col ( 'SELECT something FROM somewhere WHERE someotherthing = 1 ' ); // Warning direct DB call.
416+ \MyNamespace \wp_cache_set ( 'foo ' , $ listofthings );
417+ }
418+
419+ return $ listofthings ;
420+ }
421+
422+ function callToRelativeNamespacedCacheGetSet () {
423+ global $ wpdb ;
424+
425+ if ( ! ( $ listofthings = namespace \wp_cache_get ( $ foo ) ) ) {
426+ $ listofthings = $ wpdb ->get_col ( 'SELECT something FROM somewhere WHERE someotherthing = 1 ' ); // Warning direct DB call.
427+ namespace \wp_cache_add ( 'foo ' , $ listofthings );
428+ }
429+
430+ return $ listofthings ;
431+ }
432+
433+ function callToMultiLevelNamespaceRelativeCacheGetSet () {
434+ global $ wpdb ;
435+
436+ if ( ! ( $ listofthings = namespace \Sub \wp_cache_get ( $ foo ) ) ) {
437+ $ listofthings = $ wpdb ->get_col ( 'SELECT something FROM somewhere WHERE someotherthing = 1 ' ); // Warning direct DB call.
438+ namespace \Sub \wp_cache_set ( 'foo ' , $ listofthings );
439+ }
440+
441+ return $ listofthings ;
442+ }
443+
444+ function callToFullyQualifiedCacheDelete () {
445+ global $ wpdb ;
446+
447+ $ wpdb ->query ( 'SELECT X FROM Y ' ); // Warning direct DB call.
448+ \wp_cache_delete ( 'key ' , 'group ' );
449+ }
450+
451+ function callToQualifiedNamespacedCacheDelete () {
452+ global $ wpdb ;
453+
454+ $ wpdb ->query ( 'SELECT X FROM Y ' ); // Warning direct DB call.
455+ MyNamespace \clean_attachment_cache ( 1 );
456+ }
457+
458+ function callToFullyQualifiedNamespacedCacheDelete () {
459+ global $ wpdb ;
460+
461+ $ wpdb ->query ( 'SELECT X FROM Y ' ); // Warning direct DB call.
462+ \MyNamespace \CLEAN_POST_CACHE ( 1 );
463+ }
464+
465+ function callToRelativeNamespacedCacheDelete () {
466+ global $ wpdb ;
467+
468+ $ wpdb ->query ( 'SELECT X FROM Y ' ); // Warning direct DB call.
469+ namespace \clean_user_cache ( 1 );
470+ }
471+
472+ function callToMultiLevelNamespaceRelativeCacheDelete () {
473+ global $ wpdb ;
474+
475+ $ wpdb ->query ( 'SELECT X FROM Y ' ); // Warning direct DB call.
476+ namespace \Sub \clean_blog_cache ( 1 );
477+ }
0 commit comments