WordPress 3.5 Changed User.php Which Breaks Plugin, Can this function be the issue? Can it be fixed? - plugins

A plugin provides login for a WordPress multi-site network. This plugin now fails under WP 3.5 because the user.php file was changed. The old lines in /wp-includes/user.php can be added back in and the plugin would work. Obviously this is not a long term solution.
This is the old code from user.php
if ( empty( $user ) )
$user = wp_get_current_user();
else
$user = new WP_User( $user );
if ( ! isset( $user->ID ) )
return false;
This is the new code from 3.5
if ( empty( $user ) )
$user = get_current_user_id();
if ( ! $user = get_userdata( $user ) )
return false;
I'm a beginner trying to learn php and so I'm not sure on all of the meanings. However, I think this is the code in the plugin causing issue:
function get_userdata( $user_id ) {
global $wpdb;
if ( ! is_numeric( $user_id ) )
return false;
$user_id = absint( $user_id );
if ( ! $user_id )
return false;
$user = wp_cache_get( $user_id, 'users' ); //check to see if the cache object already has the user
if ( $user )
{
return $user; //it was in the cache
}
$user = new StdClass ();
global $XF;
XF_User_Data::fillUserData($XF->visitor, $user, $user_id);
update_user_caches($user);
return $user;
}
The new StdClass can be commented out and the error is removed but then no one can login.
How could I re-write this function to not cause the error?
*Fatal error: Call to undefined method stdClass::has_prop() /wp-includes/user.php*
The developer of the plugin is 'on vacation' and hasn't updated. He's waiting for some other changes, however, this needs to be fixed.
Any suggestions on fixing the code? Am I looking in the wrong place? Are other details needed before someone can help?

Even if you are a skilled PHP programmer, modifying WP core or any plugin script is really a bad idea. In my opinion, you should downgrade to previous version and wait for the plugin's update. Check this link to do it easily. In fact, many plugins had problems with version 3.5, including the popular CKEditor, but they are being updated and in this case, waiting seems to be the best option. Make sure all plugins are compatible with 3.5 before trying another update.

If you post which plugin you're using and a link to download it I'll take a look at it and see what the issue is.

Related

WooCommerce pdf attachment to custom email is not working

I am using WooCommerce order status manager plugin for custom order statuses and emails notifications. I have created custom status for paid orders via card pay - card-on-hold.
I have also created custom email which will be triggered when statuses will change from receivet to card-on-hold.
my code is:
add_filter( 'woocommerce_email_attachments', 'attach_manual_pdf_to_email', 10, 3);
function attach_manual_pdf_to_email ( $attachments, $status , $order ) {
$allowed_statuses = array( 'customer_processing_order', 'customer_on_hold_order', 'customer_order_status_email' );
if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
$dokument = get_template_directory() . '/woocommerce/emails/attach.pdf';
$attachments = $dokument;
}
return $attachments;
}
Email Ids customer_processing_order and customer_on_hold_order works like a charm.
But customer_order_status_email which is php template for order status manager is not working. It is located in betheme/woocommerce/emails/customer-order-status-email.php and I did also try to move it to same locations where other templates are but that did not help.
Any idea how to make this working please?
okay i did find a solution:
i did use echo '<pre>'; print_r($email->id); echo '</pre>' in template of an email which printed me an email ID. i used this id in my allowed statuses and it is working now.

LARAVEL FACEBOOKPROVIDER

I'm having trouble with the recent facebook update with their API.
I was having an error that said
Laravel FacebookProvider Error. Undefined variable: access_token.
I found another question where they said to do this :
In vendor/laravel/socialite/src/Two/FacebookProvider.php
Replace
parse_str($body, $data);
return $access_token;
With
parse_str($body, $data);
$json = json_decode(key($data));
return $json->access_token;
After I made those changes, now i'm having this error
FatalErrorException in FacebookProvider.php line 67:
Cannot access empty property
Here's the link : https://concoura.com
Never, ever modify vendor files. Instead, update your package through composer. This was fixed awhile ago as the FacebookProvider has changed greatly and utilizes a completely different method to retrieve and provide the access code.
/**
* {#inheritdoc}
*/
public function getAccessTokenResponse($code)
{
$postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
$postKey => $this->getTokenFields($code),
]);
$data = [];
$data = json_decode($response->getBody(), true);
return Arr::add($data, 'expires_in', Arr::pull($data, 'expires'));
}
I had the same problem and I solved with
composer require laravel/socialite:^2.0
Tested with Laravel 5.2 and 5.3

Is it possible to hide form from non-registered users?

I need to hide form with some shortcode [contact-form-7 id="3080"] from non-registered users in WordPress.
So i've tried to use inserted tags like this '[client][contact-form-7 id="3080"][/client]' and it doesn't work.
with this php code
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'access', 'access_check_shortcode' );
This one isn't interesting, cause i need to show it inside the template
<?php
if ( is_user_logged_in() )
echo do_shortcode( '[contact-form-7 id="1234" title="Contact form 1"]' );
?>
Are you willing/able to install third party plugins? If so, you might want to check out either or both of these:
Hide This (https://wordpress.org/plugins/hide-this/)
Eyes Only (https://bs.wordpress.org/plugins/eyes-only-user-access-shortcode/)
Both of these work by enabling shortcode that can be wrapped around specific content. I believe both have options to how only to logged-in users.

Making Register Plus Redux work with Nextend Google Connect

I'm trying to make two popular WordPress plug-ins work well together. Hopefully this question isn't too specific to my setup -- I think enough people use these plug-ins to make it a common issue.
I'm using Register Plus Redex (RPR) to require user registration to be accepted (by admin) before a user can log-in. Alone, this works fine.
I'm also using Nextend Google Connect (NGC) to allow users to log-in with Google. Those also need to be approved before they can log-in.
When NGC creates a new user in the database, it correctly has the "not activated" flag set. However, the user is still logged in. This allows them to see some blog pages that are protected by "Members Only" (another plug-in). I could maybe update Members Only or other areas to avoid this, but I would rather these users see the same behavior a normal user would see, one that just logs in with user/password, not Google. They get a nice "Your account has not been activated yet" message.
RPR has this code to authenticate, I think I need to use it from NGC some way:
public /*.object.*/ function rpr_authenticate( /*.object.*/ $user, /*.string.*/ $username, /*.string.*/ $password) {
if ( !empty($user) && !is_wp_error( $user ) ) {
if ( NULL !== get_role( 'rpr_unverified' ) && in_array( 'rpr_unverified', $user->roles ) ) {
return null;
}
}
return $user;
}
I think this is the section of NGC code I need to modify:
$secure_cookie = is_ssl();
$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie
$auth_secure_cookie = $secure_cookie;
wp_set_auth_cookie($ID, true, $secure_cookie);
$user_info = get_userdata($ID);
do_action('wp_login', $user_info->user_login, $user_info);
do_action('nextend_google_user_logged_in', $ID, $u, $oauth2);
update_user_meta($ID, 'google_profile_picture', 'https://profiles.google.com/s2/photos/profile/' . $u['id']);
The NGC code uses what I think is a "hacked" method of log-in. It doesn't use any of the methods I have seen recommended online, like the new wp_signon or older wp_login functions.
Is what I'm trying to do a major project? If so, is there another combination of plug-ins (or a single one) that will handle the following:
Require users to be logged in to see any pages (what Members Only does)
Require admin to moderate/approve new users (what RPR does)
Support log-in via Facebook, Twitter, and Google (what the Nextend Connect plug-ins do)
Update:
I changed the NGC code to this, and now it doesn't log the user in, but it just leaves them on the log-in page with no error message. I'm not sure how I can add an error message to the default log-in page, everything I find online is related to custom log-in pages.
if ($ID) { // Login
$user_info = get_userdata($ID);
if ( !empty($user_info) && !is_wp_error( $user_info ) ) {
if ( NULL !== get_role( 'rpr_unverified' ) && in_array( 'rpr_unverified', $user_info->roles ) ) {
// TODO - How to add error message to log-in page?
return;
}
}
$secure_cookie = is_ssl();
$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie
$auth_secure_cookie = $secure_cookie;
wp_set_auth_cookie($ID, true, $secure_cookie);
//
do_action('wp_login', $user_info->user_login, $user_info);
do_action('nextend_google_user_logged_in', $ID, $u, $oauth2);
update_user_meta($ID, 'google_profile_picture', 'https://profiles.google.com/s2/photos/profile/' . $u['id']);
}
I'm sure there is a better way to do this, one that will not be undone anytime I update the plug-in, but for now this works for me.
I updated nextend-google-connect.php, part of the Nextend Google Connect plug-in, and changed the Login code (starting around line 230 depending on your version) to this:
if ($ID) { // Login
$user_info = get_userdata($ID);
if ( !empty($user_info) && !is_wp_error( $user_info ) ) {
if ( NULL !== get_role( 'rpr_unverified' ) && in_array( 'rpr_unverified', $user_info->roles ) ) {
wp_redirect('wp-login.php?checkemail=registered');
exit;
}
}
$secure_cookie = is_ssl();
$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie
$auth_secure_cookie = $secure_cookie;
wp_set_auth_cookie($ID, true, $secure_cookie);
do_action('wp_login', $user_info->user_login, $user_info);
do_action('nextend_google_user_logged_in', $ID, $u, $oauth2);
update_user_meta($ID, 'google_profile_picture', 'https://profiles.google.com/s2/photos/profile/' . $u['id']);
}
By redirecting to that special URL, the Redux plug-in already has code to display a nice message to the user letting them know the admin needs to verify the account.

wp_is_post_autosave is not working

I want to auto publish all articles of my WP blog on Facebook without using any plugin.
I wrote some working code to do that and it's OK... but I also need to invoke this code only when I publish a new article (not for revisions or autosave).
That's the part of my function.php file that you need to see:
add_action( 'save_post', 'koolmind_facebook_post_article',3 );
function koolmind_facebook_post_article( $post_id ) {
/* configuration of facebook params */
....
....
/* end config */
if ( !wp_is_post_revision( $post_id ) && !wp_is_post_autosave( $post_id ) ) {
/* retrieve some data to publish */
/* invoke my code to publish on facebook */
}
}
My code is invoked as soon as I click on "add new article", and an empty draft is sent to my Facebook page.
In add, as soon as I insert a single char on my article body, autosave is triggered and a new post (almost empty) is sent again to facebook.
I just want to block this automatic publishing and send my data to facebook only when I press the PUBLISH button.
Is that possible?
UPDATE
Finally I've found the problem. There was an error inside my fb code.
Problem now is avoiding multiple pubblication when updating my post.
Here's the code now:
add_action('pending_to_publish', 'koolmind_facebook_post_article');
add_action('draft_to_publish', 'koolmind_facebook_post_article');
add_action('new_to_publish', 'koolmind_facebook_post_article');
function koolmind_facebook_post_article( $post_id ) {
require_once 'facebook/facebook.php';
/* some code here */
//verify post is not a revision
if ( !wp_is_post_revision( $post_id ) ) {
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$post_excerpt = get_the_excerpt();
$post_image = 'http://.../default.jpg'; //default image
if( $thumb_id = get_post_thumbnail_id( $post_id ) ){
$image_attributes = wp_get_attachment_image_src( $thumb_id );
$post_image = $image_attributes[0];
}
/* some code here */
}
}
Let me explain the issue:
If I use these 3 hooks I have no problem, but the code is executed before my featured image is stored into the database, so $post_image is always equals to the default image.
If I use publish_post hook instead, featured image is set properly (maybe because this hook is called after all data have been saved), but I cannot avoid data sending to Facebook if I update my post (wp_is_post_revision seems not to be fired).
Hope you have a good idea... now the code is almost OK! :)
The 'save_post' hook 'Runs after the data is saved to the database'. This means you can do this validation:
//WP hook
//the last parameter 2 means you pass 2 variables to the callback:
//the ID and the post WP object
add_action( 'save_post', 'koolmind_facebook_post_article',3,2 );
//Callback
function koolmind_facebook_post_article( $post_id, $post ) {
// Validation:
//If current WP user has no permissions to edit posts: exit function
if( !current_user_can('edit_post', $post_id) ) return;
//If is doing auto-save: exit function
if( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE ) return;
//If is doing auto-save via AJAX: exit function
if( defined( 'DOING_AJAX' ) && DOING_AJAX ) return;
//If is a revision or an autosave version or anything else: exit function
if( $post->post_status != 'publish') return;
/* configuration of facebook params */
/* invoke my code to publish on facebook */
}
It worked for me.
Try using:
add_action('publish_post', 'koolmind_facebook_post_article');