Skip Navigation

[Resolved] implement favorites system with toolset

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 61 replies, has 4 voices.

Last updated by simonM-5 3 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#1830625

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon,

Let me reply here about (2) and I'll be posting my reply about (1) on the other ticket.

The issue happened after you have changed the function get_duplicates to create both functions get_nanny_ad_duplicates and get_job_ad_duplicates. The function get_duplicates did not exist anymore and caused errors.

I took a new Duplicator copy and worked on it locally to fix that, so I introduced a check on the post type to use the relevant function, by updating these two functions in the "favourites" snippet:

function sync_favourite( $post_id ) {
	$post = get_post( $post_id );
	if ( $post->post_type == 'job-ad')
		$posts = get_job_ad_duplicates( $post_id );

	if ( $post->post_type == 'nanny-ad' )
		$posts = get_nanny_ad_duplicates( $post_id );
	
	foreach( $posts as $land => $post ) {
		if ( $post->element_id !== $post_id ) {
			remove_action('wpfp_after_add', 'sync_favourite');
			wpfp_add_favorite( $post->element_id );
		}
	}
}


function sync_unfavourite( $post_id ) {
	$post = get_post( $post_id );
	if ( $post->post_type == 'job-ad')
		$posts = get_job_ad_duplicates( $post_id );

	if ( $post->post_type == 'nanny-ad' )
		$posts = get_nanny_ad_duplicates( $post_id );
		
	foreach( $posts as $land => $post ) {
		if ( $post->element_id !== $post_id ) {
			remove_action('wpfp_after_remove', 'sync_unfavourite');
			wpfp_remove_favorite( $post->element_id );
		}
	}
}

Please replace only these two functions, and test again with new posts so we don't get in the previous error of the favoriting plugin (flag -2, -1, 0, 1, 2).
I run some tests locally and it worked. Syncing the favorite flag worked correctly.

#1831777

Hi Jamal

I replaced those snippets as requested and tested with fresh Ads. Unfortunately I'm still seeing error messages on favouriting and unfavouriting the Ads:

(1) Warning: A non-numeric value encountered in /.../wp-content/plugins/wp-favorite-posts/wp-favorite-posts.php on line 378

(2) Warning: Creating default object from empty value in /.../wp-content/plugins/wp-favorite-posts/wp-favorite-posts.php on line 152

Thanks and best regards
Simon

#1836271

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Jamal is on vacation. This is Minesh here.

I see the error is originated from the plugin "wp-favorite-posts" file. As Jamal shared the custom code I suggest you to wait for him and he will get in touch with you as soon as he back to work.

#1841269

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Simon

Just an update to say that Jamal will return tomorrow and be able to resume with this.

We have some unexpected absences and are currently very busy, and given the length of this thread haven't been able to take up the thread. Sorry to have kept you waiting.

#1841277

Hi Minesh, Nigel

Thanks for the updates. It's not a problem. We look forward to hearing from Jamal on his return. It's also easier than someone else taking up the ticket.

Hope you're all keeping safe!

Kind regards
Simon

#1842407

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon,

I was able to reproduce the issue when favoriting a new ad from a nanny by a family user. It seems that this is an edge case that the favoriting plugin does not handle. It happens for new posts because they do not have the plugin's custom field set. And the plugin does not handle this edge case (WPML duplication).
I was able to fix this issue by patching the plugin, changing the line 418 in wp-favorite-posts/wp-favorite-posts.php by changing the line from:

$val = get_post_meta($post_id, WPFP_META_KEY, true);

To

$val = (int) get_post_meta($post_id, WPFP_META_KEY, true);

The function wpfp_get_post_meta does not handle the case of an empty string in its first instruction.

function wpfp_get_post_meta($post_id) {
    $val = get_post_meta($post_id, WPFP_META_KEY, true);   // I patched this line.
    if ($val < 0) $val = 0;
    return $val;
}

Please patch the file from your end and test again with new posts.

Please note, that once you update the plugin, you will lose this patch. So, I suggest you reach to the plugin's authors on GitHub and report this use case(WPML duplication) and my suggested patch hidden link

#1844205

HI Jamal

Thanks for the update, I hope you had a nice break!

When you say "patch the file" on my end - what I did was just edit that one line in the plugin and upload it again via FTP (just checking - for me it was line 413 not 418 as you suggested, perhaps this was just an oversight, or your plugin maybe had a different version?). I hope this is what you meant by "patching".

I then deactivated the plugin and reactivated it, created a new Job Ad and a new Nanny Ad and tried to favourite them. It didn't work and now I am seeing the following error on line 152:

Warning: Creating default object from empty value in /.../wp-content/plugins/wp-favorite-posts/wp-favorite-posts.php on line 152

Please let me know if I have conducted the patch correctly or advise accordingly.

Kind regards
Simon

#1844407

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon,

Please allow me FTP access to check this further. The previous tickets were closed and credentials are automatically deleted.

Best regards,
Jamal

#1848391

Hi Jamal

I hope you are well.

Whilst you are investigating that error message, could you give please advise me of how I can filter on the favourited posts?

Obviously I need a view that filters only posts that have been favourited. I don't see any shortcode or field I can filter on. Might it be something that we need to create a shortcode for?

Thanks and regards
Simon

#1849605

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon,

I was able to reproduce the same warning but I couldn't find the cause behind it, Maybe the plugin's authors can help here.
This warning is not affecting the favoriting/unfavoriting process at all. In fact, I turned debugging to hide the warning and I checked favoriting/unfavoriting for family users and the syncing is done without issues. Check this screencast hidden link

Regarding your last question, we can create a custom shortcode to pass the favorite posts to the view, or we can hook into the view query filter and pass the favorites to the view's query similarly to my reply here https://toolset.com/forums/topic/implement-favorites-system-with-toolset/page/3/#post-1817475

add_filter( 'wpv_filter_query', 'prefix_get_favorite_posts', 30, 3 );
  
function prefix_get_favorite_posts( $query_args, $view_settings, $view_id ) {
    if ( ($view_id == 69)  and function_exists( 'wpfp_get_user_meta' ) ){
        $query_args['post__in'] = wpfp_get_user_meta();
    }
     
    return $query_args;
}
#1851645

HI Jamal

Hmm, 🤔

(1) I can confirm that it does seem to be working, with the debug messages off. However, I like to leave the debug display option on in DEV to track possible errors.

(2) Have you any tips as to what we ask the developer to do, or should I just report the error message to them as it stands?

(3) I need some help understanding what you wrote in the last update. I tried logging in to the "Favourites" test site we worked on together to see how it was done there, but I get the error "Error establishing a database connection". Is that site dead now? I cannot remember if we already had a "My Favourite Ads" page there or not.

"Regarding your last question, we can create a custom shortcode to pass the favorite posts to the view, or we can hook into the view query filter and pass the favorites to the view's query similarly to my reply here https://toolset.com/forums/topic/implement-favorites-system-with-toolset/page/3/#post-1817475"

Can we do a shortcode instead? Or is it advantageous or preferable to "hook into the view query filter and pass the favorites to the view's query"? My problem is I don't know how to do that! 😅😅

I see "Custom Field - wpfp-favorites" in the list of filter options, but don't know what I would set it to in order to filter the favourites correctly. Or is the method you're talking about "passed" to the view in a different way?

Can you please advise?

Thanks again for your dedicated support. It is so appreciated!

Best regards
Simon

#1853427

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon,

(1) I understand your point of view(1) Indeed debugging should be active for dev environments.

(2) I suggest that you explain to the plugin's authors the whole situation, WPML duplication with custom code, and all the custom code that we have added. Explain also the patch that I suggested in my reply https://toolset.com/forums/topic/implement-favorites-system-with-toolset/page/4/#post-1842407

Explain also the following patch that I have implemented on your site to resolve this issue. It consists of changing line 152 from:

$post_id = &$post->ID;

To:

$post_id = isset ($post->ID) ? $post->ID : 0;

Tell them about this ticket, they will be able to view it, except for links and images.

(3) I do not think that my updates are behind these errors(database connection). Have you reverted to a backup? Because I am not encountering these errors at all.

Can we do a shortcode instead? Or is it advantageous or preferable to "hook into the view query filter and pass the favorites to the view's query"? My problem is I don't know how to do that! 😅😅
I think that hooks are more efficient. But we can still use a shortcode. I would prefer to answer this on a separate ticket 😉

I see "Custom Field - wpfp-favorites" in the list of filter options, but don't know what I would set it to in order to filter the favourites correctly. Or is the method you're talking about "passed" to the view in a different way?
I do not think that we can use this custom field inside Toolset views. The plugin stores data in this custom field in a way that will need custom code to integrate it with Toolset. I would suggest ignoring it while creating Toolset Views/Content template/archive template.

Best regards,
Jamal

#1853443

Hi Jamal

You're a genius and a gentleman 🙂

(1) and (2) Thanks for solving that other error message and enabling the display_errors again on our site. I will indeed take this issue up with the plugin developer.

(3) Regarding this, please open a separate ticket for this. I think it is clear what we need - just a way to be able to display the favourited Job Ads or Nanny Ads in a View, synced across WPML languages. If you need any logins or anything, please send me a private reply in the new tickets and I'll provide you what you need.

I think you misunderstood the first sentence of my point (3). My apologies for not expressing myself clearly. I meant here the Favourites test site we created together (hidden link)

Kind regards
Simon

#1853859

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon,

Sorry for misunderstanding point (3). Indeed the site was crashing because of a database error. I recovered it, you can log in to check.

For some reason, I am not able to make it work with a shortcode. Even if the shortcode does return a list of IDs to pass to the view, it crashes when used inside the view's shortcode. Check this screenshot hidden link
I guess, there is no need to open a separate ticket then.

The custom code for the query filter hook and the shortcode, are both in the Toolset->Settings->Custom Code section. hidden link

Check the query filter solution on the following page and view:
- Page: hidden link
- View: hidden link

Check the query filter attempts on the following page and view:
- hidden link
- hidden link

Let me know if you have any questions.

#1853951

Hi Jamal

You legend! 😀🏆

It looks like we have it working now, but we would like a couple of days to test all our use case scenarios before we close the ticket.

Kind regards
Simon

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.