Skip to content

Instantly share code, notes, and snippets.

View verygoodplugins's full-sized avatar

Very Good Plugins verygoodplugins

View GitHub Profile
@verygoodplugins
verygoodplugins / wpf-track-login-streak.php
Last active September 21, 2021 08:03
Updates a wpf_logins_streak field each time a user logs in on two consecutive days
<?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 August 28, 2021 10:47
Easy Digital Downloads grandfathered renewal discounts, with support for upgrades and reactivations
<?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 August 4, 2021 14:11
Uses a saved company_id to lookup the company for a HubSpot contact over the Companies API
<?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 March 17, 2021 08:16
Calculates a WooCommerce lifetime value for registered users when syncing metadata to the CRM
<?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 March 16, 2021 09:14
Show the number of users with a tag
<?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 March 11, 2021 11:06
Limit the number of tags loaded from the CRM to 1000
<?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 January 4, 2021 11:49
Denies access to any content that was published before the user's registration date
<?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 December 11, 2020 14:02
Creates a daily cron to run an Import Users operation via WP Fusion
<?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 November 19, 2020 11:13
Allows syncing the booking date from multiple FooEvents Bookings products to multiple fields in your CRM
<?php
function wpf_bookable_products_meta_fields( $fields ) {
$args = array(
'post_type' => 'product',
'nopaging' => true,
'meta_key' => 'WooCommerceEventsType',
'meta_value' => 'bookings',
);
@verygoodplugins
verygoodplugins / edd-calculate-lifetime-upgrade.php
Created November 3, 2020 10:01
Calculate the upgrade cost of an EDD license from annual to lifetime
<?php
// 3 is the upgrade ID of a 1 site lifetime license
// 4 is the upgrade ID of a 3 sites lifetime license
// 1 to 4 are the price IDs of annual licenses
// 5 is the price ID of a 1 site lifetime license
// 6 is the price ID of a 3 site lifetime license
function wpf_edd_sl_upgrade_cost( $cost, $license_id, $upgrade_id ) {