This script was working perfectly for costum twitter URL shortener but for some reason it just started to put broken links.
And now all links are broken even those from before which were good.
It is showing on twitter domain.com/p(somenumber) and it is opening in browser that URL instead to redirecting to post. I guess the problem is in this Perl code which is autoposting wordpress posts on twitter.
#!/usr/bin/perl -s
use strict;
use Data::Dumper;
use XML::Simple;
use Net::Twitter;
use Scalar::Util 'blessed';
use WWW::Facebook::API;
use JSON::Any;
use lib '/var/www/perl/.';
use MyLib::DB;
use MyLib::Settings;
my $settings = new Settings();
my $config = $settings->getConfig();
my $db = new DB(
dsn =>$config->{sql_connection}->{dsn},
database =>$config->{sql_connection}->{database},
server =>$config->{sql_connection}->{server},
login =>$config->{sql_connection}->{login},
password =>$config->{sql_connection}->{password}
);
my %posts = $db->getList('select * from add_ready_post where wp_id > ( select max(wp_id) from add_ready_post where is_sent=1 ) and post_title <> "" limit 1;');
my $nt = Net::Twitter->new(legacy => 0);
my $nt = Net::Twitter->new(
traits => [qw/OAuth API::REST/],
consumer_key => '',
consumer_secret => '',
access_token => '',
access_token_secret => '',
);
map {
my $message = $posts{$_}[4];
my $id = $posts{$_}[0];
my $url = ' domain.com/p' . $posts{$_}[1];
if ( $message ne '' )
{
# print 'Original: ' . $message . "\n";
if ( length( $message ) < 110 )
{
$message = $message . ".";
}
if ( length( $message ) > 110 )
{
$message = substr( $message, 0, 110 );
$message =~ /.*(\s\w+)$/i;
my $newMessage = substr( $message, 0, index( $message, $1 ) );
$message = $newMessage . '...';
}
# print 'Result : ' . $message . $url . "\n";
my $result = $nt->update( $message . $url );
# print Dumper( $result );
# print "\n";
my $sql = "UPDATE add_ready_post SET is_sent=1 WHERE id=" . int( $id ) . ";";
$db->execute( $sql );
}
} keys %posts;
You will probably get closed with "not a real question" - but anyway - few comments:
Haven't idea what "doing" your MyLib::DB;, so can't analyze the code without knowing what contains the %posts hash.
Use use warnings; - for example you will get $nt redefined message...
my $nt = Net::Twitter->new(legacy => 0);
my $nt = Net::Twitter->new(
Using map { in this way is not a best practice. It will be much readable using
foreach my $post (keys %posts) {
}
What you'll get when the message will not match here? And what if the index returns -1?
$message =~ /.*(\s\w+)$/i;
my $newMessage = substr( $message, 0, index( $message, $1 ) );
And when the message will contains only spaces? Think about:
if ( $message ne '' )
And here is much more... I'm understand than you learning perl but try decompose the problem to manageable parts and ask an real question.
Related
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.
I am using WordPress native function wp_remote_get and wp_remote_retrieve_response_code like below:
$wpfs_response = wp_remote_get ( get_site_url() );
$wpfs_response_code = wp_remote_retrieve_response_code ( $wpfs_response );
Now, using the above code how can I verify:
SSL found,
SSL found but not a valid certificate
any idea or solution?
The answer is no, you can't.
To get the cert data and know if there's a valid cert, requires a different approach. This is the function I use, inspired by the one found in Really Simple SSL plugin on Wordpress.org.
function get_ssl_data( $domain ) {
$url = 'https://' . str_replace( array( 'https://', 'http://' ), '', $domain );
$original_parse = parse_url( $url, PHP_URL_HOST );
if ( $original_parse ) {
$get = stream_context_create( array( "ssl" => array( "capture_peer_cert" => true ) ) );
if ( $get ) {
set_error_handler( '__return_true' );
$read = stream_socket_client( "ssl://" . $original_parse . ":443", $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $get );
restore_error_handler();
if ( ! $read ) {
$certinfo = 'no-response';
}
if ( $errno == 0 && $read ) {
$cert = stream_context_get_params( $read );
if ( isset( $cert['options']['ssl']['peer_certificate'] ) ) {
$certinfo = openssl_x509_parse( $cert['options']['ssl']['peer_certificate'] );
} else {
$certinfo = 'no-response';
}
}
}
}
return $certinfo;
}
Please note that:
this is using the __return_true WordPress' function to provide a set_error_handler callback for errors;
in order to check if there's a valid cert, use is_array( get_ssl_data( $domain ) )
some time ago when the customers made a new order, in the mail header the "reply-to" was not shown in the mail that came to me (as an admin), but now if, and I need to avoid showing it, I have tested with
add_filter( 'woocommerce_email_headers', 'add_reply_to_wc_admin_new_order', 10, 3 );
function add_reply_to_wc_admin_new_order( $headers = '', $id = '', $order ) {
if ( $id == 'new_order' ) {
$reply_to_email = $order->billing_email;
$headers .= "Reply-to: <custom#custom.com>\r\n";
}
return $headers;
}
but the client's email keeps appearing in the reply-to, any idea? How to solve this?
here is your solution to keep only the header type without Reply to:
add_filter('woocommerce_email_headers', 'add_reply_to_wc_admin_new_order', 10, 3);
function add_reply_to_wc_admin_new_order($header = '', $id = '', $order)
{
$wc_email = new WC_Email(); //instantiate wc meail
if ($id == 'new_order') {
$reply_to_email = $order->billing_email;
$header = 'Content-Type: ' . $wc_email->get_content_type() . "\r\n";
}
return $header;
}
When i load the page it displays the following warning:
Warning: Cannot assign an empty string to a string offset in
C:xampp\htdocs\wordpress\smart-m\wp-includes\class.wp-scripts.php on
line 447
I think its not logical problem. Can be something due to wordpress ?
That's the function in wp-scripts.php file on line 447
public function localize( $handle, $object_name, $l10n ) {
if ( $handle === 'jquery' )
$handle = 'jquery-core';
if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
$after = $l10n['l10n_print_after'];
unset($l10n['l10n_print_after']);
}
foreach ( (array) $l10n as $key => $value ) {
if ( !is_scalar($value) )
continue;
$l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
}
$script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
if ( !empty($after) )
$script .= "\n$after;";
$data = $this->get_data( $handle, 'data' );
if ( !empty( $data ) )
$script = "$data\n$script";
return $this->add_data( $handle, 'data', $script );
}
You can delete this code:
$l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
Before you do please make a copy of you original file
That's a problem occuring on WP4.6.* and PHP7.1.
Please provide your PHP and WP version. But if you are running WP4.6 and PHP7.1 please update to WP4.7 which solves this warning.
I used check_password_reset_key() function to implement function forget password on wordpress, but when I use check_password_reset_key() to check ($key, $user) this function always return "expire key".
// Generate something random for a password reset key.
$key = wp_generate_password( 20, false );
// Now insert the key, hashed, into the DB.
if ( empty( $wp_hasher ) ) {
require_once ABSPATH . WPINC . '/class-phpass.php';
$wp_hasher = new PasswordHash( 8, true );
}
$hashed = $wp_hasher->HashPassword( $key );
$wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user_login ) );
Check :
$user = check_password_reset_key( $rp_key, $rp_login );
In my case, I resolved the issue by using WordPress's own function to create the password reset key: get_password_reset_key.
It is quite similar to your way of doing it but it also adds the current time to the key. So, instead of
$hashed = $wp_hasher->HashPassword( $key );
it does:
$hashed = time() . ':' . $wp_hasher->HashPassword( $key );