/**
  * Run the upgrade routine if necessary.
  */
 public static function upgrade()
 {
     $current_version = get_option('itelic_version', 0.1);
     if ($current_version != self::VERSION) {
         /**
          * Runs when the version upgrades.
          *
          * @param $current_version
          * @param $new_version
          */
         do_action('itelic_upgrade', self::VERSION, $current_version);
         update_option('itelic_version', self::VERSION);
     }
     foreach (get_tables() as $table) {
         if (!Manager::is_table_installed($table)) {
             add_action('all_admin_notices', function () {
                 echo '<div class="notice notice-error"><p>';
                 _e("We weren't able to install the tables necessary for Licensing to work. Please contact support.", Plugin::SLUG);
                 echo '</p></div>';
             });
             if (!function_exists('\\deactivate_plugins')) {
                 require_once ABSPATH . 'wp-admin/includes/plugin.php';
             }
             \deactivate_plugins(__FILE__);
             break;
         }
     }
 }
 /**
  * Send the notifications.
  *
  * @return Notification[]
  */
 public function get_notifications()
 {
     $reminders = Reminder\CPT::get_reminders();
     if (empty($reminders)) {
         return array();
     }
     $date_to_reminder = array();
     $table = Manager::get('itelic-keys');
     $tn = $table->get_table_name($GLOBALS['wpdb']);
     // retrieve key information and just the date value ( no time ) of when the key expires
     $sql = "SELECT *, Date(`expires`) AS EXP_DAY FROM {$tn} WHERE ";
     // START manual first record
     $sql .= $this->convert_interval_to_between($reminders[0]->get_interval());
     $date_to_reminder[$this->convert_interval_to_date($reminders[0]->get_interval())->format("Y-m-d")] = $reminders[0];
     unset($reminders[0]);
     // END manual first record
     foreach ($reminders as $reminder) {
         $interval = $reminder->get_interval();
         $date = $this->convert_interval_to_date($interval);
         // search for keys that expire any time during the day of the current reminder.
         $sql .= " OR " . $this->convert_datetime_to_between($date);
         // store a reference of that entire day to the reminder object for later use.
         $date_to_reminder[$date->format("Y-m-d")] = $reminder;
     }
     /*
      * SQL generated is similar to:
      * SELECT *, Date(`expires`) AS EXP_DAY FROM wp_itelic_keys WHERE
      * (`expires` BETWEEN '2015-03-08' AND '2015-03-08 23:59:59') OR
      * (`expires` BETWEEN '2017-03-01' AND '2017-03-01 23:59:59')
      */
     $result = $GLOBALS['wpdb']->get_results($sql);
     if (empty($result)) {
         return array();
     }
     $expire_to_key = array();
     foreach ($result as $record) {
         $expire_to_key[$record->EXP_DAY] = itelic_get_key_from_data($record);
     }
     $notifications = array();
     $manager = Factory::make('itelic-renewal-reminder');
     foreach ($expire_to_key as $expire => $key) {
         $notification = $this->make_notification($date_to_reminder[$expire], $key, $manager);
         if ($notification) {
             $notifications[] = $notification;
         }
     }
     return $notifications;
 }
 /**
  * Constructor.
  *
  * @param array $args
  */
 public function __construct(array $args = array())
 {
     parent::__construct(Manager::get('itelic-updates'), $args);
 }
 /**
  * Get the table object for this model.
  *
  * @since 1.0
  *
  * @returns Table
  */
 protected static function get_table()
 {
     return Manager::get('itelic-activations');
 }
 /**
  * Parse the product query.
  *
  * @since 1.0
  *
  * @return Join|null
  */
 protected function parse_join()
 {
     if (empty($this->args['product']) && empty($this->args['customer'])) {
         return null;
     }
     $on = new Where_Raw('k.lkey = q.lkey');
     if (!empty($this->args['product'])) {
         $on->qAnd(new Where('k.product', true, absint($this->args['product'])));
     }
     if (!empty($this->args['customer'])) {
         $on->qAnd(new Where('k.customer', true, absint($this->args['customer'])));
     }
     return new Join(new From(Manager::get('itelic-keys')->get_table_name($GLOBALS['wpdb']), 'k'), $on);
 }
 /**
  * Get the chart for this report.
  *
  * @since 1.0
  *
  * @param string $date_type
  * @param int    $product
  *
  * @return Base
  */
 public function get_chart($date_type = 'this_year', $product = 0)
 {
     $start = date('Y-m-d H:i:s', $this->convert_date($date_type));
     $end = date('Y-m-d H:i:s', $this->convert_date($date_type, true));
     $grouping = self::get_grouping_for_date_type($date_type);
     $sql = self::get_group_by($grouping, 'r.renewal_date');
     $group = $sql['group'];
     $per = $sql['per'];
     if ($per) {
         $per .= ' AS d, ';
     }
     if ($group) {
         $group = "GROUP BY {$group}";
     }
     /**
      * @var \wpdb $wpdb
      */
     global $wpdb;
     $rtn = Manager::get('itelic-renewals')->get_table_name($wpdb);
     $ktn = Manager::get('itelic-keys')->get_table_name($wpdb);
     if ($product) {
         $raw = "SELECT {$per}SUM(revenue) as c FROM {$rtn} r JOIN {$ktn} k ON(k.lkey = r.lkey AND k.product = %d) WHERE r.renewal_date BETWEEN %s AND %s";
         $raw .= $group;
         $revenue = $wpdb->get_results($wpdb->prepare($raw, $product, $start, $end));
     } else {
         $raw = "SELECT {$per}SUM(revenue) as c FROM {$rtn} r  WHERE r.renewal_date BETWEEN %s AND %s";
         $raw .= $group;
         $revenue = $wpdb->get_results($wpdb->prepare($raw, $start, $end));
     }
     $revenue = self::fill_gaps(self::translate_results($revenue), $start, $end, $grouping);
     $labels = self::get_labels($revenue, $date_type);
     $before = $after = '';
     $settings = it_exchange_get_option('settings_general');
     $currency = html_entity_decode(it_exchange_get_currency_symbol($settings['default-currency']));
     if ('after' === $settings['currency-symbol-position']) {
         $after = $currency;
     } else {
         $before = $currency;
     }
     $chart = new Line($labels, 600, 200, array('responsive' => true, 'tooltipTemplate' => "{$before}<%= value %>{$after}"));
     $chart->add_data_set(array_values($revenue), __("Revenue", Plugin::SLUG), array('fillColor' => "rgba(140,197,62,0.2)", 'strokeColor' => "rgba(140,197,62,1)", 'pointColor' => "rgba(140,197,62,1)", 'pointStrokeColor' => "#fff", 'pointHighlightFill' => "#fff", 'pointHighlightStroke' => "rgba(140,197,62,1)"));
     return $chart;
 }
 /**
  * Get the chart for this report.
  *
  * @since 1.0
  *
  * @param string $date_type
  * @param int    $product
  *
  * @return Base
  */
 public function get_chart($date_type = 'this_year', $product = 0)
 {
     $start = date('Y-m-d H:i:s', $this->convert_date($date_type));
     $end = date('Y-m-d H:i:s', $this->convert_date($date_type, true));
     $grouping = self::get_grouping_for_date_type($date_type);
     $sql = self::get_group_by($grouping, 'r.renewal_date');
     $group = $sql['group'];
     $per = $sql['per'];
     if ($per) {
         $per .= ' AS d, ';
     }
     if ($group) {
         $group = "GROUP BY {$group}";
     }
     /**
      * @var \wpdb $wpdb
      */
     global $wpdb;
     $rtn = Manager::get('itelic-renewals')->get_table_name($wpdb);
     $ktn = Manager::get('itelic-keys')->get_table_name($wpdb);
     if ($product) {
         $raw = "SELECT {$per}COUNT(1) as c FROM {$rtn} r JOIN {$ktn} k ON(k.lkey = r.lkey AND k.product = %d) WHERE r.renewal_date BETWEEN %s AND %s";
         $raw .= $group;
         $renewals = $wpdb->get_results($wpdb->prepare($raw, $product, $start, $end));
     } else {
         $raw = "SELECT {$per}COUNT(1) as c FROM {$rtn} r  WHERE r.renewal_date BETWEEN %s AND %s";
         $raw .= $group;
         $renewals = $wpdb->get_results($wpdb->prepare($raw, $start, $end));
     }
     $renewals = self::fill_gaps(self::translate_results($renewals), $start, $end, $grouping);
     $labels = self::get_labels($renewals, $date_type);
     $chart = new Line($labels, 600, 200, array('responsive' => true));
     $chart->add_data_set(array_values($renewals), __("Renewals", Plugin::SLUG), array('fillColor' => "rgba(151,187,205,0.2)", 'strokeColor' => "rgba(151,187,205,1)", 'pointColor' => "rgba(151,187,205,1)", 'pointStrokeColor' => "#fff", 'pointHighlightFill' => "#fff", 'pointHighlightStroke' => "rgba(151,187,205,1)"));
     return $chart;
 }
 /**
  * Get the chart for this report.
  *
  * @since 1.0
  *
  * @param string $date_type
  * @param int    $product
  *
  * @return Chart
  */
 public function get_chart($date_type = 'this_year', $product = 0)
 {
     if (!$product) {
         return null;
     }
     /**
      * @var \wpdb $wpdb
      */
     global $wpdb;
     $atn = Manager::get('itelic-activations')->get_table_name($wpdb);
     $ktn = Manager::get('itelic-keys')->get_table_name($wpdb);
     $raw = "SELECT COUNT(1) as c, `release_id` as d FROM {$atn} a JOIN {$ktn} k ON (k.lkey = a.lkey AND k.product = %d)\n\t\t\t\tWHERE a.status = %s GROUP BY `release_id` LIMIT 5";
     $results = $wpdb->get_results($wpdb->prepare($raw, $product, Activation::ACTIVE));
     $translated = self::translate_results($results);
     if (isset($translated[''])) {
         $unknown = $translated[''];
         $translated[__('Unknown', Plugin::SLUG)] = $unknown;
         unset($translated['']);
     }
     $colors = array(array('color' => '#E94F37', 'highlight' => '#FF6951'), array('color' => '#393E41', 'highlight' => '#53585B'), array('color' => '#3F88C5', 'highlight' => '#59A2DF'), array('color' => '#44BBA4', 'highlight' => '#5ED5BE'), array('color' => '#EDDDD4', 'highlight' => '#D4C4BB'));
     $chart = new Pie(600, 200, array('ibdShowLegend' => '#legend-' . $this->get_slug(), 'responsive' => true, 'tooltipTemplate' => '<%= value %> install<%if (value != 1){%>s<%}%>'));
     $i = 0;
     foreach ($translated as $label => $value) {
         if ($label != __('Unknown', Plugin::SLUG)) {
             $release = itelic_get_release($label);
             $label = $release->get_version();
             $label = "v{$label}";
         }
         $chart->add_data_set($value, $label, $colors[$i]);
         $i++;
     }
     return $chart;
 }
 /**
  * Get the table object for this model.
  *
  * @since 1.0
  *
  * @return Table
  */
 protected static function get_table()
 {
     return Manager::get('itelic-updates');
 }
 /**
  * Get the table object for this model.
  *
  * @since 1.0
  *
  * @returns Table
  */
 protected static function get_table()
 {
     return Manager::get('itelic-renewals');
 }
/**
 * Count the number of releases belonging to each status.
 *
 * @internal
 *
 * @since 1.0
 *
 * @param string $status
 *
 * @return array
 */
function count_releases($status = '')
{
    $counts = wp_cache_get('itelic-release-counts');
    if (!is_array($counts)) {
        /** @var $wpdb \wpdb */
        global $wpdb;
        $tn = Manager::get('itelic-releases')->get_table_name($wpdb);
        $raw = $wpdb->get_results("SELECT COUNT(1) AS C, r.status AS S FROM {$tn} r GROUP BY r.status");
        $counts = array();
        foreach ($raw as $result) {
            $counts[$result->S] = $result->C;
        }
        $counts = wp_parse_args($counts, array(Release::STATUS_ACTIVE => 0, Release::STATUS_ARCHIVED => 0, Release::STATUS_DRAFT => 0, Release::STATUS_PAUSED => 0));
        wp_cache_set('itelic-release-counts', $counts);
    }
    if ($status) {
        return $counts[$status];
    }
    return $counts;
}
/**
 * Install custom DB tables.
 *
 * @since 1.0
 */
function install_tables()
{
    foreach (\ITELIC\get_tables() as $table) {
        Manager::maybe_install_table($table);
    }
}
 /**
  * Get the chart for this report.
  *
  * @since 1.0
  *
  * @param string $date_type
  * @param int    $product
  *
  * @return Chart
  */
 public function get_chart($date_type = 'this_year', $product = 0)
 {
     $start = date('Y-m-d H:i:s', $this->convert_date($date_type));
     $end = date('Y-m-d H:i:s', $this->convert_date($date_type, true));
     $grouping = self::get_grouping_for_date_type($date_type);
     $sql = self::get_group_by($grouping, 'p.post_date');
     $group = $sql['group'];
     $per = $sql['per'];
     if ($per) {
         $per .= ' AS d, ';
     }
     if ($group) {
         $group = "GROUP BY {$group}";
     }
     /**
      * @var \wpdb $wpdb
      */
     global $wpdb;
     $ktn = Manager::get('itelic-keys')->get_table_name($wpdb);
     $ptn = $wpdb->posts;
     $raw = "SELECT {$per}COUNT(1) as c FROM {$ktn} k JOIN {$ptn} p ON (k.transaction_id = p.ID and p.post_date BETWEEN %s and %s) WHERE k.status = %s ";
     if ($product) {
         $product = absint($product);
         $raw .= "AND product = '{$product}' ";
     }
     $raw .= $group;
     $active = $wpdb->get_results($wpdb->prepare($raw, $start, $end, Key::ACTIVE));
     $expired = $wpdb->get_results($wpdb->prepare($raw, $start, $end, Key::EXPIRED));
     $disabled = $wpdb->get_results($wpdb->prepare($raw, $start, $end, Key::DISABLED));
     $active = self::fill_gaps(self::translate_results($active), $start, $end, $grouping);
     $expired = self::fill_gaps(self::translate_results($expired), $start, $end, $grouping);
     $disabled = self::fill_gaps(self::translate_results($disabled), $start, $end, $grouping);
     $labels = self::get_labels($active, $date_type);
     $chart = new Line($labels, 600, 200, array('ibdShowLegend' => '#legend-' . $this->get_slug(), 'responsive' => true));
     $statuses = Key::get_statuses();
     $chart->add_data_set(array_values($active), $statuses[Key::ACTIVE], array('fillColor' => "rgba(140,197,62,0.2)", 'strokeColor' => "rgba(140,197,62,1)", 'pointColor' => "rgba(140,197,62,1)", 'pointStrokeColor' => "#fff", 'pointHighlightFill' => "#fff", 'pointHighlightStroke' => "rgba(140,197,62,1)"));
     $chart->add_data_set(array_values($expired), $statuses[Key::EXPIRED], array('fillColor' => "rgba(255,186,0,0.2)", 'strokeColor' => "rgba(255,186,0,1)", 'pointColor' => "rgba(255,186,0,1)", 'pointStrokeColor' => "#fff", 'pointHighlightFill' => "#fff", 'pointHighlightStroke' => "rgba(255,186,0,1)"));
     $chart->add_data_set(array_values($disabled), $statuses[Key::DISABLED], array('fillColor' => "rgba(221,61,54,0.2)", 'strokeColor' => "rgba(221,61,54,1)", 'pointColor' => "rgba(221,61,54,1)", 'pointStrokeColor' => "#fff", 'pointHighlightFill' => "#fff", 'pointHighlightStroke' => "rgba(221,61,54,1)"));
     return $chart;
 }
 /**
  * Get the chart for this report.
  *
  * @since 1.0
  *
  * @param string $date_type
  * @param int    $product
  *
  * @return Base
  */
 public function get_chart($date_type = 'this_year', $product = 0)
 {
     if ($date_type != 'all_time') {
         $start = date('Y-m-d H:i:s', $this->convert_date($date_type));
         $end = date('Y-m-d H:i:s', $this->convert_date($date_type, true));
     }
     /**
      * @var \wpdb $wpdb
      */
     global $wpdb;
     $rtn = Manager::get('itelic-renewals')->get_table_name($wpdb);
     $ktn = Manager::get('itelic-keys')->get_table_name($wpdb);
     if ($product) {
         $product = absint($product);
         $product = " AND k.product = {$product}";
     } else {
         $product = '';
     }
     $expired_status = Key::EXPIRED;
     $all_where = " WHERE k.status = '{$expired_status}'";
     if ($date_type != 'all_time') {
         $renew_where = " WHERE r.renewal_date BETWEEN '{$start}' AND '{$end}'";
         $all_where .= " AND k.expires BETWEEN '{$start}' AND '{$end}'";
     } else {
         $renew_where = '';
     }
     if ($product) {
         $all_where .= $product;
     }
     $raw_renewed = "SELECT COUNT(1) as c FROM {$rtn} r JOIN {$ktn} k ON (k.lkey = r.lkey{$product}){$renew_where}";
     $renewed_results = $wpdb->get_results($raw_renewed);
     $renewed = (int) $renewed_results[0]->c;
     $raw_expired = "SELECT COUNT(1) as c FROM {$ktn} k{$all_where}";
     $expired_results = $wpdb->get_results($raw_expired);
     $expired = (int) $expired_results[0]->c;
     if ($expired == 0) {
         $renewed = 100;
         $expired = 0;
     } else {
         $renewed = number_format($renewed / ($expired + $renewed) * 100, 0);
         $expired = 100 - $renewed;
     }
     $colors = array(array('color' => '#E94F37', 'highlight' => '#FF6951'), array('color' => '#393E41', 'highlight' => '#53585B'), array('color' => '#3F88C5', 'highlight' => '#59A2DF'), array('color' => '#44BBA4', 'highlight' => '#5ED5BE'), array('color' => '#EDDDD4', 'highlight' => '#D4C4BB'));
     $chart = new Pie(600, 200, array('ibdShowLegend' => '#legend-' . $this->get_slug(), 'responsive' => true, 'tooltipTemplate' => '<%= value %>%'));
     $chart->add_data_set($renewed, __("Renewed", Plugin::SLUG), $colors[2]);
     if ($expired > 0) {
         $chart->add_data_set($expired, __("Expired", Plugin::SLUG), $colors[0]);
     }
     return $chart;
 }
 /**
  * Get notifications.
  *
  * @since 1.0
  *
  * @param Release $release
  * @param string  $message
  * @param string  $subject
  *
  * @return Notification[]
  */
 public function get_notifications(Release $release, $message, $subject)
 {
     /** @var $wpdb \wpdb */
     global $wpdb;
     $atn = Manager::get('itelic-activations')->get_table_name($wpdb);
     $ktn = Manager::get('itelic-keys')->get_table_name($wpdb);
     $rtn = Manager::get('itelic-releases')->get_table_name($wpdb);
     $results = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT k.customer FROM {$atn} a JOIN {$ktn} k ON ( a.lkey = k.lkey AND k.`product` = %d )\n\t\t\t WHERE a.status = %s AND a.release_id IN (\n\t\t\t SELECT r.ID FROM {$rtn} r WHERE r.product = %d AND r.`start_date` < %s )", $release->get_product()->ID, Activation::ACTIVE, $release->get_product()->ID, $release->get_start_date()->format('Y-m-d H:i:s')));
     if (empty($results)) {
         return array();
     }
     $notifications = array();
     foreach ($results as $result) {
         $to = get_user_by('id', $result->customer);
         if (!$to instanceof \WP_User) {
             continue;
         }
         $notification = new Notification($to, Factory::make('itelic-outdated-customers'), $message, $subject);
         $notification->add_data_source($release);
         $notifications[] = $notification;
     }
     return $notifications;
 }