WooCommerce Hides Info from Chrome Download Bar - woocommerce

When a user buys a downloadable product and page redirects to download page, then when users click on link of product to save it, chrome starts to download the link but file size is not known and so progress bar doesnt work.
It frustrates my client.
I've found function download_headers() in class-wc-download-handler.php and edited it to below code.
because php function filesize() doesn't work with non-absolute paths (not with urls, should be public_html/wordpress/.... .ex)
Also added Content-Range but it still doesn't work.
function download_headers( $file_path, $filename ) {
self::check_server_config();
self::clean_buffers();
nocache_headers();
header( "X-Robots-Tag: noindex, nofollow", true );
header( "Content-Type: " . self::get_download_content_type( $file_path ) );
header( "Content-Description: File Transfer" );
header( "Content-Disposition: attachment; filename=\"" . $filename . "\";" );
header( "Content-Transfer-Encoding: binary" );
$url = $file_path;
static $regex = '/^Content-Length: *+\K\d++$/im';
if ( ! $fp = #fopen( $url, 'rb' ) ) {
return false;
}
if ( isset( $http_response_header )
&& preg_match( $regex, implode( "\n", $http_response_header ), $matches ) ) {
return (int)$matches[0];
}
$size = strlen( stream_get_contents( $fp ) );
header( "Content-Range: 0-" . ($size-1) . "/" . $size );
header( "Content-Length: " . $size );
}

Related

Functions.php to set all external links with rel:="nofollow"

when i use the code:
add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');
function my_nofollow($content) {
return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}
function my_nofollow_callback($matches) {
$link = $matches[0];
$site_link = get_bloginfo('url');
if (strpos($link, 'rel') === false) {
$link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
$link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
}
return $link;
}
only in the_content and the_excerpt the links get a nofollow attribute. How i can edit the code, that the whole wordpress site use this functions (footer, sidebar...)
Thank you
a good script which allows to add nofollow automatically and to keep the other attributes
function nofollow(string $html, string $baseUrl = null) {
return preg_replace_callback(
'#<a([^>]*)>(.+)</a>#isU', function ($mach) use ($baseUrl) {
list ($a, $attr, $text) = $mach;
if (preg_match('#href=["\']([^"\']*)["\']#', $attr, $url)) {
$url = $url[1];
if (is_null($baseUrl) || !str_starts_with($url, $baseUrl)) {
if (preg_match('#rel=["\']([^"\']*)["\']#', $attr, $rel)) {
$relAttr = $rel[0];
$rel = $rel[1];
}
$rel = 'rel="' . ($rel ? (strpos($rel, 'nofollow') ? $rel : $rel . ' nofollow') : 'nofollow') . '"';
$attr = isset($relAttr) ? str_replace($relAttr, $rel, $attr) : $attr . ' ' . $rel;
$a = '<a ' . $attr . '>' . $text . '</a>';
}
}
return $a;
},
$html
);
}
The idea is to add your action in the full html buffer, once everything is loaded and filtered, not only in the 'content' area.
BE VERRY CAREFULL WITH THIS !!!! This action is verry RAW and low-level... You can really mess up everything with theses buffer actions. But it's also very nice to know ;-)
It works like this :
add_action( 'wp_loaded', 'buffer_start' ); function buffer_start() { ob_start( "textdomain_nofollow" ); }
add_action( 'shutdown', 'buffer_end' ); function buffer_end() { ob_end_flush(); }
Then do the replace action and setup the callback :
Juste imagine the $buffer variable as your full site in HTML, as it will load.
function textdomain_nofollow( $buffer ) {
return preg_replace_callback( '/<a[^>]+/', 'textdomain_nofollow_callback', $buffer );
}
Here is how I do the href checking (read my comments) :
function textdomain_nofollow_callback( $matches ) {
$link = $matches[0];
// if you need some specific external domains to exclude, just use :
//$exclude = '('. home_url() .'|(http|https)://([^.]+\.)?(domain-to-exclude.org|other-domain-to-exclude.com)|/)';
// By default, just exclude your own domain, and your relative urls that starts by a '/' (if you use relatives urls)
$exclude = '('. home_url() .'|/)';
if ( preg_match( '#href=\S('. $exclude .')#i', $link ) )
return $link;
if ( strpos( $link, 'rel=' ) === false ) {
$link = preg_replace( '/(?<=<a\s)/', 'rel="nofollow" ', $link );
} elseif ( preg_match( '#rel=\S(?!nofollow)#i', $link ) ) {
$link = preg_replace( '#(?<=rel=.)#', 'nofollow ', $link );
}
return $link;
}
It works well in my experience...
Q

WordPress: template_redirect hook sending wp_mail twice

I have a few restricted pages. If a user tries to access those, I warn them by email using wp_mail and alert admin with some data such as username, URL, time, etc. For that, I am using the template_redirect hook.
All is working fine but wp_mail sending the email twice. I am not sure what is wrong in my code and why it is sending twice.
Callback Function and Hook
cp_write_log() is a custom log function that write a log to debug.log. Entries also adding twice to the debug.log file.
function module_permission_check() {
if ( is_page() ) {
$template = get_page_template_slug();
$post_type = get_cpt_for_template( $template );
if ( ! is_current_user_granted_for_module( $post_type ) ) {
$user = wp_get_current_user();
//$headers = [ 'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo( 'admin_email' ) . '>' ];
// user message
$message = str_replace(
[ '%%name%%', '%%sitename%%' ],
[
$user->data->user_login,
get_bloginfo( 'name' ),
],
cp_get_setting( 'warning_unauthorized_email_message' )
);
if ( wp_mail( $user->data->user_email, 'Warning: Unauthorized access', $message ) ) {
cp_write_log( 'Email sent to ' . $user->data->user_email );
}
global $wp;
$current_url = home_url( add_query_arg( [], $wp->request ) );
$admin_message = "Hello,\n\nA member has tried to access unauthorized content.\n\nUsername: {$user->data->user_login}\nURL: {$current_url}\nLocal Time: " . current_time( 'l, j F Y \a\t H:i ' ) . "\nUTC Time: " . current_time( 'l, j F Y \a\t H:i ', TRUE ) . "\n\nRegards,\n" . get_bloginfo( 'name' );
if ( wp_mail( get_bloginfo( 'admin_email' ), 'Alert: Unauthorized access', $admin_message ) ) {
cp_write_log( 'Email sent to admin' );
}
wp_redirect(home_url());
exit();
}
}
}
add_action('template_redirect', 'module_permission_check');
Update
I found it is only happening on Safari browser. Very strange.

WordPress wp_email() is not accepting from value properly

I am sending an email with wp_mail() function with the following header details but it's not sending email!
// get header details
$sn_header_name = !empty ( get_option( 'sn_header_name' ) ) ? get_option( 'sn_header_name' ) : get_bloginfo( 'name' );
$sn_header_email = !empty ( get_option( 'sn_header_email' ) ) ? get_option( 'sn_header_email' ) : get_bloginfo( 'admin_email' );
$headers = array(
"Content-Type: text/html; charset=UTF-8",
"From: {$sn_header_name} <{$sn_header_email}>"
);
Using the above header details wp_mail() is not working but if I remove this line:
"From: {$sn_header_name} <{$sn_header_email}>"
It's sending email properly.
can you tell me why?
There maybe some special character or html tag in header name which stopping to send mail. Can you please try this
// get header details
$sn_header_name = !empty ( get_option( 'sn_header_name' ) ) ? get_option( 'sn_header_name' ) : get_bloginfo( 'name' );
$sn_header_email = !empty ( get_option( 'sn_header_email' ) ) ? get_option( 'sn_header_email' ) : get_bloginfo( 'admin_email' );
$sn_header_email= esc_attr($sn_header_email);
$sn_header_name = esc_attr($sn_header_name);
$headers = array(
"Content-Type: text/html; charset=UTF-8",
"From: {$sn_header_name} <{$sn_header_email}>"
);
If you're trying to set it once and forget it, you can use these 2 hooks in your theme's functions.php :
wp_mail_from :
add_filter('wp_mail_from','custom_wp_mail_from');
function custom_wp_mail_from($original_email) {
return 'mail#example.com';
}
wp_mail_from_name :
add_filter('wp_mail_from_name','custom_wp_mail_from_name');
function custom_wp_mail_from_name($original_name) {
return 'John Doe';
}

wp_mail doesn't send the attachment in WordPress

I have this code and it should work, the email is sent but without the attachment.
$attachments = THEME_DIR . '/resources/img/emails/cropped.png';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: Me Myself <me#example.net>';
wp_mail( 'test#example.org', 'subject', 'message', $headers, $attachments );
Okay, turns out that you just need to use ABSPATH.
$attachments = [
ABSPATH . 'wp-content/themes/themename/resources/img/emails/cropped.png',
ABSPATH . 'wp-content/themes/themename/resources/img/emails/facebook.png'
];
// Send the email and the invoice as an attachment.
wp_mail( 'test#email.com', 'New Invoice', 'Message body sent with attachment.', $headers, $attachments );
You can set up also these or just include them in $headers:
add_filter( 'wp_mail_content_type', function ( $content_type ) {
return 'text/html';
} );
add_filter( 'wp_mail_from', 'yoursite_wp_mail_from' );
function yoursite_wp_mail_from( $content_type ) {
return 'contact#yoursite.ca';
}
add_filter( 'wp_mail_from_name', 'yoursite_wp_mail_from_name' );
function yoursite_wp_mail_from_name( $name ) {
return 'Your Site Inc.';
}

Why does my WordPress email go to spam folder?

I am sending all my WordPress emails using Mailgun SMTP.
Other emails from WordPress arrive to the inbox fine, but I have just created a new function to automatically generate an email when a post is published and it always gets caught by the spam filter.
Can you see why?
Maybe something to do with my headers config?
I have tried sending with and without the attachment, it makes no difference.
add_action( 'publish_post', 'notify_on_publish' );
function notify_on_publish( $post_id ) {
global $post;
$from_name = get_bloginfo( 'name' );
$from_email = get_bloginfo( 'admin_email' );
$to_email = get_post_meta( $post_id, 'contact_email', true );
$attachment = get_post_meta( $post_id, 'post_image', true );
$headers = "From: '$from_name' <$from_email> \r\n Content-type: text/html; charset=" . get_option('blog_charset') . "\r\n";
$subject = "This is a test subject";
$message = "<p>This is some test body text</p>";
$attachment_url = $attachment['guid'];
wp_mail( $to_email, $subject, $message, $headers, $attachment_url );
}

Resources