How to verify SSL using WordPress wp_remote_get() function? - wordpress

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 ) )

Related

How to rewrite URL of custom post type without post type in URL

I am working on a tour site. Where they have a custom post type built with a custom plugin.
That custom post type has base slug like "/destination/".$post->post_name.
They want to me to remove that base slug so that it could be only $post->post_name
I have tried a code from internet listed below.
It works for single level for that destination.
But when I have a parent destination like New York in United State America.
So it does not work.
Here is an example:
function update_destination_page_link_filter($post_link, $post, $leavename = null, $sample = null ) {
if ( $post->post_type === 'destination' ) {
$post_link = str_replace('/destination/', '/', $post_link);
if($post->post_parent !== 0){
$parent_slug = get_parent_link($post->post_parent, '');
$post_link = '/'.$parent_slug.$post->post_name;
}
$post_link = update_url_base( $post, $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'update_destination_page_link_filter', 1, 3 );
function allow_destination_direct_by_name( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
$query->set( 'post_type', array( 'post', 'destination', 'page' ) );
}
add_action( 'pre_get_posts', 'allow_destination_direct_by_name', 1);
Single Level
http://siteurl/united-state-america works well
http://siteurl/united-state-america/new-york not working. It should open the new-york location page but it is showing 404
It may also be more specification in location
Like http://siteurl/united-state-america/new-york/brooklyn
The following code may help you, in this regards.
add_filter( 'post_type_link', 'my_post_type_link', 10, 3);
function my_post_type_link($permalink, $post, $leavename) {
if ($post->post_type == <your-post-type>)
{
$p_name=$post->post_name;
$parent_slug = get_parent_link($post->post_parent, '');
if (isset($parent_slug) && !empty($parent_slug))
{
$permalink = home_url( "" . $parent_slug . "/" . $p_name . "/");
}
}
return $permalink;
}
add_filter( 'rewrite_rules_array', 'my_rewrite_rules_array');
function my_rewrite_rules_array($rules) {
$rules = array('([^/]*)/([^/]*)/?$' => 'index.php?post_type=<your-post-type>&name=$matches[2]&meta=$matches[1]') + $rules;
return $rules;
}

WordPress Custom MD5 slug page not found

My "werknemers" post type need an MD5 generated slug to make them unique. In order to do that, I have added the following code:
function isValidMd5($md5 =''){
return preg_match('/^[a-f0-9]{32}$/', $md5);
}
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if(isValidMd5($slug)) { } else {
if ( 'werknemers' == $post_type ) {
$slug = md5( time() );
}
}
return $slug;
}
add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug', 10, 4 );
Works perfectly however, the posts are now not accessible, giving a "Page not found" error. Changing the permalinks doesn't help and neither did resetting ".htaccess". I assume I need something specific to be placed in ".htaccess", but I don't know what. Any ideas?
Because I removed the post type slug, the post couldn't be found.
It actually had nothing to do with the MD5 generator.
To fix it, I had to apply the following code (in case anyone else has this problem)
function na_remove_slug( $post_link, $post, $leavename ) {
if ( 'werknemers' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
function na_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'werknemers', 'page' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );

Cannot assign an empty string to a string offset Warning in Wordpress

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.

Custom post type url rewriting

I am rewriting the url of CPT 'entertainment' to /entertainment/%year%/%monthnum%/%post_id%/ It is re-writing but gives 404 not found error. How can I solve this?
Here is my code:
add_action( 'init', 'my_rewrite');
function my_rewrite() {
global $wp_rewrite;
$entertainment_structure = '/entertainment/%year%/%monthnum%/%post_id%/';
$wp_rewrite->add_rewrite_tag("%entertainment%", '([^/]+)', "entertainment=");
$wp_rewrite->add_permastruct('entertainment', $entertainment_structure, false);
flush_rewrite_rules();
}
// Add filter to plugin init function
add_filter('post_type_link', 'entertainment_permalink', 10, 3);
// Adapted from get_permalink function in wp-includes/link-template.php
function entertainment_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
$rewritecode = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
$leavename? '' : '%post_id%',
'%post_id%',
'%category%',
'%author%',
$leavename? '' : '%post_id%',
);
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
$unixtime = strtotime($post->post_date);
$category = '';
if ( strpos($permalink, '%category%') !== false ) {
$cats = get_the_category($post->ID);
if ( $cats ) {
usort($cats, '_usort_terms_by_ID'); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent )
$category = get_category_parents($parent, false, '/', true) . $category;
}
// show default category in permalinks, without
// having to assign it explicitly
if ( empty($category) ) {
$default_category = get_category( get_option( 'default_category' ) );
$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
}
}
$author = '';
if ( strpos($permalink, '%author%') !== false ) {
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
}
$date = explode(" ",date('Y m d H i s', $unixtime));
$rewritereplace =
array(
$date[0],
$date[1],
$date[2],
$date[3],
$date[4],
$date[5],
$post->ID,
$post->ID,
$category,
$author,
$post->ID,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
} else { // if they're not using the fancy permalink option
}
return $permalink;
}
If I replace $entertainment_structure = '/entertainment/%year%/%monthnum%/%post_id%/'; with $entertainment_structure = '/entertainment/%year%/%monthnum%/%entertainment%/'; it works fine
You aren't supposed to hook init, but either register_activation_hook() it, if its a plugin or use after_switch_theme if its a theme (its important to either deactivate/reactivate the plugin or switch themes in order for your rewrites to apply). <= further read here
You might want to have a read of the rewrite API

Wordpress redirection on login

We have a bilingual Wordpress theme. The English version is at mydomain.com and the Czech version is in a virtual directory at mydomain.com/cs/
When a user logs in at mydomain.com/login they are redirected to mydomain.com/dashboard as specified in the code below (that is the page name). I need users logging in using mydomain.com/cs/login to be logged in to mydomain.com/cs/dashboard
The code in use for that function is here:
<?php wp_login_form( apply_filters( 'atcf_shortcode_profile_login_args', array(
'redirect' => isset ( $edd_options[ 'profile_page' ] ) ? get_permalink( $edd_options[ 'profile_page' ] ) : home_url()
) ) ); ?>
home_url is what returns the full URL of the site. Any ideas how I can achieve a redirection by escaping the current directory (/login)?
You can check the $_SERVER["REQUEST_URI"] value to see if it starts with /cs/ and then dynamically update the profile page value accordingly.
// The current URI (does not incude host/domain)
$uri = $_SERVER["REQUEST_URI"];
// The home URL
$redirect = home_url();
// $edd_options[ 'profile_page' ] must return the page ID for get_permalink to work
if ( isset( $edd_options[ 'profile_page' ] ) ) {
// The profile URL.
$profile_page_id = $edd_options[ 'profile_page' ];
$redirect = get_permalink( $profile_page_id );
// Check if the URI starts with /cs/
if ( strpos( $uri, '/cs/' ) == 0 ){
// Explode into an array
$url_array = explode( '/', $profile_page );
// Insert /cs/ into array
$url_array = array_slice($url_array, 0, 3, true) +
array("x"=>"cs") +
array_slice($url_array, 3, count($url_array)-3, true);
// Implode back to a string
$redirect = implode( '/', $url_array );
}
}
// Set up params to send to login form
$args = apply_filters( 'atcf_shortcode_profile_login_args', array( 'redirect' => $redirect ) );
wp_login_form( $args );

Resources