Skip to content

Commit

Permalink
Updated to latest Fontimator 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kinging123 committed Mar 26, 2020
1 parent 1aba142 commit 9ee3804
Show file tree
Hide file tree
Showing 33 changed files with 1,934 additions and 520 deletions.
125 changes: 125 additions & 0 deletions admin/class-fontimator-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,129 @@ public function can_edit_orders() {
return true;
}

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();
}

public function add_delete_user_link($user_object) {
if ( ! is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) ) {
echo "<a class='button button-link-delete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" .
sprintf( __( 'Delete &#8220;%s&#8221; permanently' ), $user_object->display_name ) . '</a>';
}
}

}
2 changes: 1 addition & 1 deletion fontimator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Start at version 1.0.0 and use SemVer - https://semver.org
* Rename this for your plugin and update it as you release new versions.
*/
define( 'FONTIMATOR_VERSION', '2.2.0' );
define( 'FONTIMATOR_VERSION', '2.3.0' );

/**
* The code that runs during plugin activation.
Expand Down
157 changes: 157 additions & 0 deletions includes/acf-config/fontimator-complete-family.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

acf_add_local_field_group(array(
'key' => 'fontimator-complete-family',
'title' => 'Complete Family Discounts',
'fields' => array(
array(
'key' => 'complete_family_enabled',
'label' => 'Enable Complete Family Banner',
'name' => 'complete_family_enabled',
'type' => 'true_false',
'instructions' => 'Show the complete family banner in the downloads page of the my account section, where the user purchased only some of the font\'s weights.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => 'Enabled',
'ui_off_text' => 'Disabled',
),
array(
'key' => 'complete_family_discount_percent',
'label' => 'Discount for missing weights',
'name' => 'complete_family_discount_percent',
'type' => 'number',
'instructions' => 'The percentage to be taken off the missing weights',
'required' => 1,
'conditional_logic' => array(
array(
array(
'field' => 'complete_family_enabled',
'operator' => '==',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 60,
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => '',
),
array(
'key' => 'complete_family_limit_fonts',
'label' => 'Limit to these fonts:',
'name' => 'complete_family_limit_fonts',
'type' => 'post_object',
'instructions' => 'Only show on the selected fonts. If empty, show on all of them.',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'complete_family_enabled',
'operator' => '==',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array(
0 => 'product',
),
'taxonomy' => '',
'allow_null' => 1,
'multiple' => 1,
'return_format' => 'id',
'ui' => 1,
),
array(
'key' => 'complete_family_limit_days_from_purchase',
'label' => 'Limit to fonts purchased before:',
'name' => 'complete_family_limit_days_from_purchase',
'type' => 'number',
'instructions' => 'If the font was bought more recently than the amount of days specified here, don\'t show the banner to prevent frauds.',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'complete_family_enabled',
'operator' => '==',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 30,
'placeholder' => '',
'prepend' => '',
'append' => 'Days ago',
'min' => 0,
'max' => '',
'step' => 1,
),
array(
'key' => 'generate_eligible_emails_list',
'label' => 'Get all users eligible for the discount',
'name' => 'generate_eligible_emails_list',
'type' => 'message',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'complete_family_enabled',
'operator' => '==',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '<a class="button-secondary" href="admin.php?page=fontimator-config&generate_complete_family_eligible_list=1">Generate a list of all eligible users</a><br /><small>The list will be saved in <code>/var/www/output/</code></small>',
'new_lines' => '',
'esc_html' => 0,
),
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'fontimator-config',
),
),
),
'menu_order' => 10,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
2 changes: 1 addition & 1 deletion includes/acf-config/fontimator-free-downloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'label' => 'Downloads',
'name' => 'ftm_free_downloads',
'type' => 'repeater',
'instructions' => '',
'instructions' => '<span dir="ltr">Example: [fontimator-free-download download="font_heshbon"]</span>',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

acf_add_local_field_group(
array(
'key' => 'fontimator-wsms-gifts',
'title' => 'WSMS MailChimp Font Gifts Settings',
'key' => 'fontimator-mc-gifts',
'title' => 'MailChimp Font Gifts Settings',
'fields' => array(
array(
'key' => 'field_5bb4b99c46a40',
Expand Down
6 changes: 4 additions & 2 deletions includes/class-fontimator-acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public function __construct() {
'fontimator-font-options',
'fontimator-font-price-formulas',
'fontimator-free-downloads',
'fontimator-complete-family',
);

if ( class_exists( 'WSMS' ) ) {
$this->field_groups[] = 'fontimator-wsms-gifts';
global $mc4wp_aaa;
if ( $mc4wp_aaa ) {
$this->field_groups[] = 'fontimator-mc-gifts';
}
}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-fontimator-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function activate() {
private static function install_free_downloads_db() {
global $wpdb;

$table_name = $wpdb->prefix . Fontimator_Free_Download::$db_table_name;
$table_name = $wpdb->prefix . 'ftm_free_downloads';

$charset_collate = $wpdb->get_charset_collate();

Expand Down
14 changes: 11 additions & 3 deletions includes/class-fontimator-font-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@ public function setup_download_link() {
$weight_clean = Zipomator::get_clean_weight( $this->weight );
$license_clean = Zipomator::get_clean_license( $this->license );

$downloads = $this->get_downloads();
if ( count( $downloads ) && reset( $downloads ) ) {
$old_download = reset( $downloads );
$download_id = $old_download->get_id();
} else {
$download_id = wp_generate_uuid4();
}

$zip_file = new WC_Product_Download();
$zip_file->set_id( wp_generate_uuid4() );
$zip_file->set_id( $download_id );
$zip_file->set_name( Zipomator::single_name( $this->family, $weight_clean, $license_clean, $this->get_version() ) . '.zip' );

$download_url = Zipomator::get_bundle_url( $this->family, $weight_clean, $this->license );
Expand Down Expand Up @@ -137,7 +145,7 @@ public function get_version() {
}

public function get_license_type() {
$license_parts = explode( '-', $this->license );
$license_parts = explode( '-', $this->license, 2 );
return $license_parts[0];
}

Expand All @@ -156,7 +164,7 @@ public function get_name( $context = 'view' ) {
}

if ( $this->license ) {
switch ( explode( '-', $this->license )[0] ) {
switch ( explode( '-', $this->license, 2 )[0] ) {
case 'web':
$license_name = __( 'web', 'fontimator' );
break;
Expand Down
3 changes: 3 additions & 0 deletions includes/class-fontimator-font.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public function get_archived_weights() {

public function get_visible_weights( $return_format = 'id' ) {
$weights = $this->get_attributes()[ 'pa_' . FTM_WEIGHT_ATTRIBUTE ];
if ( ! $weights ) {
return null;
}
if ( 'id' === $return_format ) {
$all_weights = $weights['options'];
$family_weights = array(
Expand Down
Loading

0 comments on commit 9ee3804

Please sign in to comment.