/** * @param foolic_licensekey $key * * @return array */ function foolic_license_listing_get_sorted_domains($key) { $domain_array = $key->get_domains(); $domain_objects = array(); if ($domain_array !== false) { foreach ($domain_array as $domain) { $domain_object = foolic_domain::get($domain); $domain_object->attached = $key->is_domain_attached($domain_object->url); if ($domain_object->attached) { $domain_object->sort_order = 0; } else { if ($domain_object->localhost) { $domain_object->sort_order = 2; } else { if ($domain_object->blacklisted) { $domain_object->sort_order = 3; } else { $domain_object->sort_order = 1; } } } $domain_objects[] = $domain_object; } usort($domain_objects, 'foolic_license_listing_sort_compare'); } return $domain_objects; }
/** * Attach a domain to a license key * * @param int $licensekey_id The ID of the license key * @param int $domain_id The ID of the domain * * @return bool If the attachment was successful */ function foolic_attach_domain_to_licensekey($licensekey_id, $domain_id) { $licensekey = foolic_licensekey::get_by_id($licensekey_id); if ($licensekey->ID > 0) { return $licensekey->attach_domain($domain_id); } return false; }
/** * @param foolic_licensekey $license_key * * @returns int */ function foolic_renewal_percentage($license_key) { $discount = 0; //we only want to give a discount if the license expires if ($license_key->does_expire()) { if (!$license_key->has_expired()) { //the license key has not expired yet - good boy! $discount = foolic_renewal_discount_early(); } else { $expiration_date = strtotime($license_key->expires); $grace_period_expiry_date = strtotime('+' . foolic_renewal_grace_period() . ' days', $expiration_date); //days ahead of expiry if ($grace_period_expiry_date >= time()) { //within grace period - lucky boy! $discount = foolic_renewal_discount_grace(); } else { //late - boo! $discount = foolic_renewal_discount_late(); } } } return $discount; }
/** * @param int $licensekey_id * @param int $upgrade_license_id */ function foolic_perform_upgrade($licensekey_id, $upgrade_license_id) { $license_key = foolic_licensekey::get_by_id($licensekey_id); if ($license_key->ID == 0) { return false; } if (!$license_key->is_deactivated()) { $date_format = get_option('date_format'); $expiry_date = $license_key->expires; $domain_limit = $license_key->domain_limit; //get the expiry date $expiry = strtotime($license_key->expires); //if already expired, then use today's date if ($expiry < time()) { $expiry = time(); } //ensure we have our connections registered foolic_post_relationships::register_connections(); //load new license $license = foolic_license::get_by_id($upgrade_license_id); if ($license->ID > 0) { $domain_limit = $license->domain_limit; if ($license->expires_in_days > 0) { $expiry_date = date($date_format, strtotime('+' . $license->expires_in_days . ' days', $expiry)); } else { $expiry_date = 'never'; } } //save new expiry date and domain limit $license_key->domain_limit = $domain_limit; $license_key->expires = $expiry_date; $license_key->process_domains(); //process domains immediately and update //delete connection $license_key->disconnect_from_existing_license(); //connect to new license $license->link_to_licensekey($license_key->ID); //save upgrade post meta $existing_upgrades = foolic_get_upgrades($license_key->ID); $existing_upgrades[] = array('upgrade_date' => date($date_format), 'upgrade_details' => __('Upgrade to', 'foolicensing') . ' ' . $license->name); foolic_update_upgrades($license_key->ID, $existing_upgrades); return true; } return false; }
function custom_column_content($column) { global $post; switch ($column) { case FOOLIC_CPT_LICENSE . '_version': $license = new foolic_license(); $license->load($post); echo $license->update_version; break; case FOOLIC_CPT_LICENSE . '_domains': $license = new foolic_license(); $license->load($post); $limit = $license->domain_limit; if ($limit == '' || $limit == '0') { echo 'Unlimited'; } else { echo $limit; } break; case FOOLIC_CPT_LICENSE . '_expires': $license = new foolic_license(); $license->load($post); $days = $license->expires_in_days; if ($days == '' || $days == '0') { echo 'Never'; } else { echo sprintf(__('%s days', 'foolic'), $days); } break; case FOOLIC_CPT_LICENSE_KEY . '_issued': echo date('d M Y', strtotime($post->post_date)); break; case FOOLIC_CPT_LICENSE_KEY . '_status': $licensekey = new foolic_licensekey(); $licensekey->load($post); $valid = foolic_licensekey_checker::validate_license_key($licensekey); echo '<span title="' . $valid['message'] . '" style="color:' . $valid['color'] . '">' . $valid['status'] . '</span>'; if ($licensekey->has_exceeded_domain_limit()) { echo ' ' . __('Usage:', 'foolic') . ' (' . $licensekey->usage_html() . ')'; } if ($licensekey->has_expired()) { echo ' ' . __('Expires:', 'foolic') . ' ' . $licensekey->expires; } break; } }
function render_licensekey_details_metabox($post) { $license_key = foolic_licensekey::get($post); ?> <table class="form-table"> <tbody> <tr> <td style="width:150px" class="first-column" valign="top"><?php _e('Deactivated', 'foolic'); ?> </td> <td> <?php echo $license_key->deactivated ? "<strong style='color:#f00'>YES</strong>" : "no"; ?> <input name="<?php echo FOOLIC_CPT_LICENSE_KEY; ?> [deactivated]" type="checkbox" value="on" <?php echo $license_key->deactivated ? 'checked="checked"' : ''; ?> /> </td> <td style="width:150px" class="first-column" valign="top"><?php _e('Expires', 'foolic'); ?> </td> <td><input name="<?php echo FOOLIC_CPT_LICENSE_KEY; ?> [expires]" size="10" maxlength="20" type="text" value="<?php echo $license_key->expires; ?> " /></td> </tr> <tr> <td class="first-column" valign="top"><?php _e('Domain Limit', 'foolic'); ?> </td> <td><input name="<?php echo FOOLIC_CPT_LICENSE_KEY; ?> [domain_limit]" size="10" maxlength="4" type="text" value="<?php echo $license_key->domain_limit; ?> " /></td> <td class="first-column" valign="top"><?php _e('Domain Limit Exceeded', 'foolic'); ?> </td> <td> <?php echo $license_key->exceeded ? "<strong style='color:#f00'>YES</strong>" : "no"; ?> </td> </tr> <?php do_action('foolic_render_licensekey_details_metabox', $post); ?> </tbody> </table><?php }
<br /> </div> <h2><?php _e('License Key Overages', 'foolic'); ?> </h2> <?php if (!get_option('foolic_licensekey_setpostmeta')) { $args = array('post_type' => FOOLIC_CPT_LICENSE_KEY, 'nopaging' => true, 'fields' => 'ids'); $query = new WP_Query(); $keys = $query->query($args); if ($keys) { foreach ($keys as $licensekey_id) { $key = foolic_licensekey::get_by_id($licensekey_id); $key->store_meta_data(); } } _e('Post meta updated!', 'foolic'); add_option('foolic_licensekey_setpostmeta', true); } else { echo '<h3>' . __('Summary') . '</h3>'; echo 'Expiring license keys: ' . count(foolic_get_expiring_licensekeys()) . '<br />'; echo 'Expired license keys: ' . count(foolic_get_expired_licensekeys()) . '<br />'; echo 'Exceeded license keys: ' . count(foolic_get_exceeded_licensekeys()) . '<br />'; return; $filter = isset($_GET['licensekey_filter_status']) ? $_GET['licensekey_filter_status'] : ''; echo __('Overage Type', 'foolic') . '<select name="licensekey_filter_status"><option>' . __('--select--', 'foolic') . '</option>'; echo '<option value="expiring"' . ($filter == 'expiring' ? ' selected="selected"' : '') . '>' . __('Expiring', 'foolic') . '</option>'; echo '<option value="expired"' . ($filter == 'expired' ? ' selected="selected"' : '') . '>' . __('Expired', 'foolic') . '</option>';
public static function get_by_id($post_id) { $license_key = new foolic_licensekey(); $license_key->load_by_id($post_id); return $license_key; }
function create_license_key($user_id = 0, $user_identifier = '', $meta = array(), $meta_display = array()) { //set expiry dates if ($this->expires_in_days > 0) { //we need to set a future expiry date $date_format = get_option('date_format'); $current_date = date($date_format); $expiry_date = date($date_format, strtotime($current_date . ' + ' . $this->expires_in_days . ' days')); } else { $expiry_date = 'never'; } $licensekey_meta = array('expires' => $expiry_date, 'domain_limit' => $this->domain_limit); $user_id = absint($user_id); $licensekey_meta_extra = array('user' => $user_identifier, 'user_id' => $user_id, 'license_id' => $this->ID, 'license' => $this->slug, 'meta' => $meta); $author = apply_filters('foolic_licensekey_author_override', $user_id > 0 ? $user_id : 1); $vendor = $this->author; $licence_key_string = $this->generate_license_key(true); $licensekey_override_args = array('timestamp' => time(), 'proposed_licensekey' => $licence_key_string, 'expires' => $expiry_date, 'domain_limit' => $this->domain_limit, 'vendor_id' => $vendor, 'user' => $user_identifier, 'user_id' => $user_id, 'license' => $this->ID, 'meta' => $meta, 'meta_display' => $meta_display); $licence_key_response = apply_filters('foolic_licensekey_response_override', $licence_key_string, $licensekey_override_args); //first check if the response is a stdClass and cast to array if so if ($licence_key_response instanceof stdClass) { $licence_key_response = (array) $licence_key_response; } if (is_array($licence_key_response)) { //handle an array response //try get license key if (array_key_exists('licensekey', $licence_key_response)) { $licence_key_string = $licence_key_response['licensekey']; unset($licence_key_response['licensekey']); } //try get license key again using slightly different key if (array_key_exists('license_key', $licence_key_response)) { $licence_key_string = $licence_key_response['license_key']; unset($licence_key_response['license_key']); } $meta_display = array_merge($meta_display, $licence_key_response); } else { //we are getting back a string $licence_key_string = $licence_key_response; } $licence_key = new foolic_licensekey(); if ($licence_key->create($licence_key_string, $vendor, $licensekey_meta, $licensekey_meta_extra, $meta_display)) { //link the licensekey to the license $this->link_to_licensekey($licence_key->ID); //link the licensekey to the user $licence_key->link_to_user($user_id); return $licence_key; } //there was an error! return false; }