Skip to content

Instantly share code, notes, and snippets.

@bhwebworks
Last active October 22, 2019 12:05
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bhwebworks/11384243 to your computer and use it in GitHub Desktop.
Prevent unwanted results from appearing in (future) Stream records
<?php
// Prevent unwanted results from appearing in (future) Stream records - v 1.x
add_filter( 'wp_stream_record_array', 'bhww_core_wp_stream_filter_record_array', 10, 1 );
function bhww_core_wp_stream_filter_record_array( $recordarr ) {
// BackupBuddy (iThemes) entries
$context = 'settings';
$option = 'ithemes-updater-cache';
if ( isset( $recordarr['contexts'][ $context ] ) && $option == $recordarr['meta']['option'] ) {
$recordarr = array();
}
// WP Help entries
$context = 'wp-help';
$singular_name = 'help document';
if ( isset( $recordarr['contexts'][ $context ] ) && $singular_name == $recordarr['meta']['singular_name'] ) {
$recordarr = array();
}
return $recordarr;
}
<?php
// Prevent unwanted results from appearing in (future) Stream records - v 2.x
add_filter( 'wp_stream_record_array', 'bhww_core_wp_stream_filter_record_array', 10, 1 );
function bhww_core_wp_stream_filter_record_array( $recordarr ) {
if ( ! isset( $recordarr[0] ) )
return array();
// BackupBuddy (iThemes) entries
if ( 'settings' === $recordarr[0]['connector'] && isset( $recordarr[0]['stream_meta']['option'] ) && 'ithemes-updater-cache' === $recordarr[0]['stream_meta']['option'] )
return array();
// WP Help entries
if ( 'wp-help' === $recordarr[0]['context'] && isset( $recordarr[0]['stream_meta']['singular_name'] ) && 'help document' == $recordarr[0]['stream_meta']['singular_name'] )
return array();
// All other entries
return $recordarr;
}
<?php
// Prevent unwanted results from appearing in (future) Stream records - v 3.x
add_filter( 'wp_stream_record_array', 'bhww_core_wp_stream_filter_record_array', 10, 1 );
function bhww_core_wp_stream_filter_record_array( $recordarr ) {
if ( ! isset( $recordarr ) )
return array();
// BackupBuddy (iThemes) entries
if ( 'settings' === $recordarr['connector'] && isset( $recordarr['meta']['option'] ) && 'ithemes-updater-cache' === $recordarr['meta']['option'] )
return array();
if ( 'settings' === $recordarr['connector'] && isset( $recordarr['meta']['option'] ) && 'pb_backupbuddy_notifications' === $recordarr['meta']['option'] )
return array();
// Jetpack Protect entries
if ( 'jetpack' === $recordarr['connector'] && isset( $recordarr['meta']['option'] ) && 'jetpack_protect_blocked_attempts' === $recordarr['meta']['option'] )
return array();
// WP Help entries
if ( 'wp-help' === $recordarr['context'] && isset( $recordarr['meta']['singular_name'] ) && 'help document' == $recordarr['meta']['singular_name'] )
return array();
// All other entries
return $recordarr;
}
@bhwebworks
Copy link
Author

Added updated filters for Stream v2.x and 3.x.

@lukecarbis
Copy link

💥 This is a great example of how to filter records in Stream. Thanks @bhwebworks.

@bhwebworks
Copy link
Author

The v3 exclusion filters now include BackupBuddy, Jetpack Protect (added 8/30/2015), and WP Help entries.

@bhwebworks
Copy link
Author

Updated v3 filters for Stream 3.0.2.

@greggh
Copy link

greggh commented Oct 18, 2019

Any chance you're still watching this? I am wondering how to remove two plugins from writing anything to the stream log: Imagify and WP Rocket. Both are flooding the log.

http://i.imgur.com/cGYzLO1.png
http://i.imgur.com/vrqDf0k.png

Both are Settings. I just don't know how to convert what I see there into the right options for filtering like you do.

Any help will be really appreciated!

@bhwebworks
Copy link
Author

bhwebworks commented Oct 18, 2019

@greggh,

Do you have access to your database? That code basically reflects entries in the stream and stream_meta database tables.

'settings' === $recordarr['connector']

would be a more generic entry from the stream table, and, for example,

isset( $recordarr['meta']['option'] ) && 'jetpack_protect_blocked_attempts' === $recordarr['meta']['option']

would be a Jetpack related entry in the stream_meta table.

This didn't occur to me four years ago when I was working on this, but you could use file_put_contents in that filter callback function to write the Stream arrays into a log file so you can see better how to write your exclusion code. Something like this (untested) would work, I believe:

file_put_contents( dirname(__FILE__) . '/array-log.txt', print_r( $recordarr, true ), FILE_APPEND );

@bhwebworks
Copy link
Author

Actually I just tested that file_put_contents approach and it works great.

@greggh
Copy link

greggh commented Oct 18, 2019

Thank you! Using that info I solved it. Just had to change the match to a bit more fuzzy. The WP Rocket and Imagify ones append those keys to the end of their 'option', so they are all unique. But they all start with the same code:

// imagify
if ( 'settings' === $data_array['connector'] && isset( $data_array['meta']['option'] ) && strpos($data_array['meta']['option'], 'imagify_optimize_media_batch') !== false )
	return array();

// WP Rocket
if ( 'settings' === $data_array['connector'] && isset( $data_array['meta']['option'] ) && strpos($data_array['meta']['option'], 'rocket_preload_batch') !== false )
	return array();

@bhwebworks
Copy link
Author

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment