|
public function maybe_generate_complete_family_eligible_emails_list ( $screen ) { |
|
if ( ! strpos($screen->id, "fontimator-config") == true || ! isset( $_GET['generate_complete_family_eligible_list'] ) ) { |
|
return; |
|
} |
|
global $mc4wp_aaa; |
|
|
|
// Settings for long requests |
|
ini_set('max_execution_time', 6000); // 6000 seconds = 100 minutes |
|
ob_end_flush(); |
|
ob_implicit_flush( true ); |
|
ob_end_flush(); |
|
|
|
// Use file |
|
$time = date('d-m-Y_His'); |
|
$emails_file = fopen("/var/www/output/family_eligible_emails_$time.txt", 'a') or die("Unable to open file!");;//opens file in append mode |
|
|
|
$acf = Fontimator::get_instance()->get_acf(); |
|
$limited_to_fonts = $acf->get_acf_field( 'complete_family_limit_fonts', 'options' ); |
|
$limit_days = $acf->get_acf_field( 'complete_family_limit_days_from_purchase', 'options' ); |
|
|
|
|
|
$all_users = get_users( array( |
|
'fields' => array( 'ID' ), |
|
// 'number' => 500, |
|
// 'offset' => 500 * ( $_GET['half_thousand'] ?: 0 ) |
|
) ); |
|
|
|
foreach ( $all_users as $user ) { |
|
$customer_id = $user->ID; |
|
|
|
// Skip if has subscription |
|
$subscription = wcs_user_has_subscription( $customer_id, '', 'active' ); |
|
if ( $subscription ) { |
|
continue; |
|
} |
|
|
|
// Skip if not subscribed |
|
$userdata = get_userdata($customer_id); |
|
$user_email = $userdata->user_email; |
|
if ( $mc4wp_aaa ) { |
|
if ( ! $mc4wp_aaa->is_user_subscribed( $user_email ) ) { |
|
continue; |
|
} |
|
} |
|
|
|
|
|
$downloads = wc_get_customer_available_downloads( $customer_id ); |
|
$fonts = []; |
|
foreach ( $downloads as $download ) { |
|
$variation = new Fontimator_Font_Variation( $download['product_id'] ); |
|
if ( ! $variation |
|
|| $variation->get_license_type() !== 'otf' |
|
|| strpos( $variation->get_family(), 'membership') !== false |
|
|| $variation->get_family() === 'membership-reseller' ) { |
|
continue; |
|
} |
|
$family_name = $variation->get_family(); |
|
|
|
// Check if font is in banner whitelist |
|
if ( $limited_to_fonts && ! in_array( $variation->get_parent_id(), $limited_to_fonts ) ) { |
|
continue; |
|
} |
|
|
|
if ( ! isset( $fonts[$family_name] ) ) { |
|
// Check if font was purchased long enough ago, if this is first weight of font |
|
if ( $limit_days ) { |
|
$first_order = wc_get_order( $download['order_id'] ); |
|
$date_created = $first_order->get_date_created(); |
|
$timestamp_created = $date_created->getTimestamp(); |
|
|
|
$datetime_now = new WC_DateTime(); // Get now datetime (from Woocommerce datetime object) |
|
$timestamp_now = $datetime_now->getTimestamp(); // Get now timestamp |
|
|
|
$time_delta = $timestamp_now - $timestamp_created; // Difference in seconds |
|
$days_in_seconds = $limit_days * 24 * 60 * 60; // x days in seconds |
|
|
|
if ( $time_delta < $days_in_seconds ) { |
|
continue; // Skip fonts purchased too recently. |
|
} |
|
} |
|
$fonts[$family_name] = []; |
|
} |
|
|
|
$fonts[$family_name][] = $variation->get_weight(); |
|
|
|
if ( $variation->get_weight() === '000-familybasic' ) { |
|
$font = new Fontimator_Font( $variation->get_parent_id() ); |
|
$fonts[$family_name] = array_unique( array_merge( $fonts[$family_name], $font->get_familybasic_weights( 'slug' ) ) ); |
|
} |
|
} // Downloads loop |
|
|
|
// Now, after we went over all the downloads, let's check every font for missing weights |
|
foreach ( $fonts as $font_name => $purchased_weights ) { |
|
if ( ! $font_id = get_page_by_path( $family_name, OBJECT, 'product' )->ID ) { |
|
continue; |
|
} |
|
$font = new Fontimator_Font( $font_id ); |
|
$visible_weights = $font->get_visible_weights( 'slug' ); |
|
if ( ! $visible_weights ) { |
|
continue; |
|
} |
|
$not_purchased_weights = array_diff( $visible_weights, $purchased_weights, [ '000-variable', '000-familybasic' ] ); |
|
if ( count( $purchased_weights ) // There are desktop weights, not just web/app |
|
&& count( $not_purchased_weights ) // There are weights not yet purchased |
|
&& ! in_array( '000-family', $purchased_weights ) // Person doesn't have both the entire family... |
|
&& ! in_array( '000-variable', $purchased_weights ) // and the variable font. |
|
) { |
|
fwrite($emails_file, $user_email . ",\n"); |
|
break; |
|
} |
|
} |
|
} // Customers loop |
|
|
|
fwrite($emails_file, "#### DONE ####\n"); |
|
fclose($emails_file); |
|
die(); |
|
} |