Have a bug with register
-
Not work /// Have a bug.. My issue with the Bulk Media Register plugin is that when I choose to register certain media files, after it says “completed,” it logs the following errors:
[18-Jul-2024 19:51:04 UTC] PHP Warning: foreach() argument must be of type array|object, false given in X:\xampp\htdocs\wp-content\plugins\bulk-media-register\lib\class-bulkmediaregister.php on line 707
[18-Jul-2024 19:51:04 UTC] PHP Warning: Trying to access array offset on false in X:\xampp\htdocs\wp-content\plugins\bulk-media-register\lib\class-bulkmediaregister.php on line 712
[18-Jul-2024 19:51:04 UTC] PHP Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in X:\xampp\htdocs\wp-content\plugins\bulk-media-register\lib\class-bulkmediaregister.php:712
Stack trace:
#0 X:\xampp\htdocs\wp-includes\class-wp-hook.php(324): BulkMediaRegister->mail_register_message(1)
#1 X:\xampp\htdocs\wp-includes\class-wp-hook.php(348): WP_Hook->apply_filters('', Array)
#2 X:\xampp\htdocs\wp-includes\plugin.php(517): WP_Hook->do_action(Array)
#3 X:\xampp\htdocs\wp-content\plugins\bulk-media-register\lib\class-bulkmediaregister.php(954): do_action('bmr_mail_regist...', 1)
#4 X:\xampp\htdocs\wp-includes\class-wp-hook.php(324): BulkMediaRegister->bulkmediaregister_message_callback('')
#5 X:\xampp\htdocs\wp-includes\class-wp-hook.php(348): WP_Hook->apply_filters('', Array)
#6 X:\xampp\htdocs\wp-includes\plugin.php(517): WP_Hook->do_action(Array)
#7 X:\xampp\htdocs\wp-admin\admin-ajax.php(192): do_action('wp_ajax_bulkmed...')
#8 {main}
thrown in X:\xampp\htdocs\wp-content\plugins\bulk-media-register\lib\class-bulkmediaregister.php on line 712Temporary solution
The error is related to the
get_user_option()
function returningfalse
instead of the expected array. This can happen if the options you are trying to retrieve do not exist for this user. We need to add a check for the existence and correctness of the retrieved data before using them inforeach
andcount
.We will make changes to your
mail_register_message
function to check for the presence of options before using them:public function mail_register_message( $uid ) {
$bulkmediaregister_settings = get_user_option( 'bulkmediaregister', $uid );
if ( $bulkmediaregister_settings && $bulkmediaregister_settings['mail_send'] ) {
if ( function_exists( 'wp_date' ) ) {
$now_date_time = wp_date( 'Y-m-d H:i:s' );
} else {
$now_date_time = date_i18n( 'Y-m-d H:i:s' );
}
$messages = get_user_option( 'bulkmediaregister_messages', $uid );
$csvs = get_user_option( 'bulkmediaregister_csvs', $uid );
if (is_array($csvs)) {
$max_col_count = 0;
foreach ( $csvs as $value ) {
if ( is_array($value) && $max_col_count < count( $value ) ) {
$max_col_count = count( $value );
}
}
if (isset($csvs[0]) && is_array($csvs[0])) {
$start_comma = count( $csvs[0] );
$plus_comma = $max_col_count - count( $csvs[0] );
for ( $i = $start_comma; $i < $start_comma + $plus_comma; $i++ ) {
$csvs[0][ $i ] = __( 'Images' ) . strval( $i - $start_comma + 1 );
}
$csv_filename = sanitize_file_name( 'registered' . $now_date_time . '.csv' );
$csv_file = wp_normalize_path( $this->plugin_tmp_dir . '/' ) . $csv_filename;
$filter = null;
if ( get_locale() == 'ja' ) {
$filter = 'php://filter/write=' . urlencode( 'convert.iconv.utf-8/cp932//TRANSLIT' ) . '/resource=';
}
$file = new SplFileObject( $filter . $csv_file, 'a' );
foreach ( $csvs as $line ) {
if (is_array($line)) {
$file->fputcsv( $line );
}
}
$file = null;
if ( $messages ) {
/* translators: Date and Time */
$message_head = sprintf( __( 'Bulk Media Register : %s', 'bulk-media-register' ), $now_date_time ) . "\r\n\r\n";
/* translators: count of media */
$message_head .= sprintf( __( '%1$d media were registered. Attached the registration data as a CSV file[%2$s].', 'bulk-media-register' ), count( $messages ), $csv_filename ) . "\r\n\r\n";
$to = get_userdata( $uid )->user_email;
/* translators: blogname for subject */
$subject = sprintf( __( '[%s] Media Register', 'bulk-media-register' ), get_option( 'blogname' ) );
wp_mail( $to, $subject, $message_head . implode( $messages ), null, $csv_file );
delete_user_option( $uid, 'bulkmediaregister_messages' );
delete_user_option( $uid, 'bulkmediaregister_csvs' );
wp_delete_file( $csv_file );
}
}
}
}
}
- The topic ‘Have a bug with register’ is closed to new replies.