Skip to content

Instantly share code, notes, and snippets.

Avatar

Very Good Plugins verygoodplugins

View GitHub Profile
@verygoodplugins
verygoodplugins / wpf-multisite-meta-keys.php
Last active Nov 15, 2021
Prefix the WPF contact ID and tags meta keys with the current blog prefix
View wpf-multisite-meta-keys.php
<?php
/**
* When the user's contact ID or tags are saved, prefix the usermeta key with
* the blog prefix of the current blog.
*
* @param bool $check Whether or not to bypass the original database
* check.
* @param int $user_id The user ID.
* @param string $meta_key The meta key.
@verygoodplugins
verygoodplugins / wpf-track-login-streak.php
Last active Sep 21, 2021
Updates a wpf_logins_streak field each time a user logs in on two consecutive days
View wpf-track-login-streak.php
<?php
function wpf_track_login_streak( $meta_id, $user_id, $meta_key, $meta_value ) {
if ( 'wpf_last_login' === $meta_key ) { // this runs every time the wpf_last_login field is about to be updated.
$prev_value = get_user_meta( $user_id, 'wpf_last_login', true ); // get the previous last login timestamp.
$prev_value = floor( absint( $prev_value ) / DAY_IN_SECONDS ); // Convert to days since Jan 1st 1970.
@verygoodplugins
verygoodplugins / edd-improved-grandfathered-renewal-discount.php
Last active Aug 28, 2021
Easy Digital Downloads grandfathered renewal discounts, with support for upgrades and reactivations
View edd-improved-grandfathered-renewal-discount.php
<?php
/**
* Sets renewal discount to 30% for any customer that purchased before January
* 1, 2021.
*
* @param int $renewal_discount The renewal discount.
* @param int $license_id The license ID.
* @return int The renewal discount amount.
*/
function wpf_edd_grandfather_renewal_discount( $renewal_discount, $license_id ) {
@verygoodplugins
verygoodplugins / wpf-lookup-hubspot-company.php
Created Aug 4, 2021
Uses a saved company_id to lookup the company for a HubSpot contact over the Companies API
View wpf-lookup-hubspot-company.php
<?php
function example_hubspot_company_lookup( $user_id ) {
$company_id = get_user_meta( $user_id, 'company_id', true );
$request = 'https://api.hubapi.com/crm/v3/objects/companies/' . $company_id; // see https://developers.hubspot.com/docs/api/crm/companies
$response = wp_remote_get( $request, wp_fusion()->crm->get_params() );
if ( is_wp_error( $response ) ) {
wpf_log( 'error', 0, $response->get_error_message() );
@verygoodplugins
verygoodplugins / wpf-sync-lifetime-value.php
Created Mar 17, 2021
Calculates a WooCommerce lifetime value for registered users when syncing metadata to the CRM
View wpf-sync-lifetime-value.php
<?php
function example_sync_lifetime_value( $user_meta, $user_id ) {
$user_meta['lifetime_value'] = 0;
$customer_orders = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'shop_order',
'post_status' => wc_get_is_paid_statuses(),
@verygoodplugins
verygoodplugins / wpf-tag-count.php
Created Mar 16, 2021
Show the number of users with a tag
View wpf-tag-count.php
<?php
//
// Tag Count shortcode, use like [wpf_tag_count tag="Tag Name"]
//
function wpf_tag_count( $atts ) {
$tag = wpf_get_tag_id( $atts['tag'] );
$result = get_transient( 'wpf_tag_count_' . sanitize_key( $tag ) );
@verygoodplugins
verygoodplugins / wpf-limit-tags.php
Created Mar 11, 2021
Limit the number of tags loaded from the CRM to 1000
View wpf-limit-tags.php
<?php
// Limit number of tags loaded. More than 1000 tags can cause slowness in the interfaces
add_filter( 'wpf_set_setting_available_tags', function( $tags ) {
return array_slice( $tags, 0, 1000 );
} );
@verygoodplugins
verygoodplugins / wpf-restrict-past-content.php
Last active Jan 4, 2021
Denies access to any content that was published before the user's registration date
View wpf-restrict-past-content.php
<?php
//
// Denies access to any content that was published before the user's registration date
//
function disallow_before_date_published( $can_access, $user_id, $post_id ) {
$published = get_the_date( 'U', $post_id );
$userdata = get_userdata( $user_id );
@verygoodplugins
verygoodplugins / wpf-daily-import.php
Created Dec 11, 2020
Creates a daily cron to run an Import Users operation via WP Fusion
View wpf-daily-import.php
<?php
// This runs every day and imports any new users from the connected CRM who have the specified tag ID ("123" in this example)
// If contacts with that tag already have user accounts on the site they will be skipped and no data will be loaded
// i.e. this just imports *new* users
if ( ! wp_next_scheduled( 'wpf_daily_import' ) ) {
wp_schedule_event( time(), 'daily', 'wpf_daily_import' );
}
@verygoodplugins
verygoodplugins / wpf-fooevents-multiple-bookings.php
Created Nov 19, 2020
Allows syncing the booking date from multiple FooEvents Bookings products to multiple fields in your CRM
View wpf-fooevents-multiple-bookings.php
<?php
function wpf_bookable_products_meta_fields( $fields ) {
$args = array(
'post_type' => 'product',
'nopaging' => true,
'meta_key' => 'WooCommerceEventsType',
'meta_value' => 'bookings',
);