static function plugins_for_current_user()
 {
     global $current_user;
     global $wpdb;
     //		$wpdb->set_blog_id(1,1);
     $plugins = $wpdb->get_results($wpdb->prepare("SELECT `ID`,`post_title`,`post_name`,`post_excerpt`,`post_content` FROM {$wpdb->posts}  WHERE `post_type`='product' ORDER BY post_title ASC"));
     // AND `user`=%s",$current_user->ID));
     $memberships = Memberships::get_memberships_for_user($current_user->ID);
     foreach ($plugins as $i => $plugin) {
         $plugin->file = get_post_meta($plugin->ID, 'filename', true);
         $plugin->version = get_post_meta($plugin->ID, 'version', true);
         $group_access = get_post_meta($plugin->ID, 'group_access', true);
         $has_access = false;
         if (is_array($group_access)) {
             foreach ($group_access as $group_id => $extra) {
                 if (array_key_exists($group_id, $memberships)) {
                     $has_access = true;
                 }
             }
         }
         $plugin_access = $current_user->plugin_access;
         if (isset($plugin_access[$plugin->post_name])) {
             $plugin->key = $plugin_access[$plugin->post_name]['key'];
             $plugin->sites = $plugin_access[$plugin->post_name]['sites'];
         } else {
             if (!$has_access) {
                 unset($plugins[$i]);
             }
         }
     }
     return $plugins;
 }
 function register()
 {
     $id = trim($_GET['id']);
     $regkey = trim($_GET['key']);
     $user_email = trim($_GET['email']);
     $email = urldecode($_REQUEST['email']);
     $user = get_user_by_email($email);
     if (!$user) {
         $user = get_user_by('login', $email);
     }
     $domain = Http::get_request_domain();
     $siteid = $this->get_siteid();
     if (!$siteid) {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'error', 'message' => 'You have to supply your email and registration key.')) . ')');
     }
     if (!$user) {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'error', 'message' => 'No user with this email ' . $user_email)) . ')');
     }
     global $wpdb;
     $product_ID = $wpdb->get_var($wpdb->prepare("SELECT `ID` FROM {$wpdb->posts}  WHERE `post_type`='product' AND `post_name`=%s", $id));
     if (!$product_ID) {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'error', 'message' => 'No plugin or theme with this Id:' . $id)) . ')');
     }
     $group_access = get_post_meta($product_ID, 'group_access', true);
     if (!class_exists('Groups')) {
         include plugin_dir_path(__FILE__) . '../MembersExtended/components/groups/models/groups.php';
     }
     if (!class_exists('Memberships')) {
         include plugin_dir_path(__FILE__) . '../MembersExtended/components/groups/models/memberships.php';
     }
     $memberships = Memberships::get_memberships_for_user($user->ID);
     $canHaveXNbrOfSites = false;
     foreach ($group_access as $group_id => $extra) {
         if (array_key_exists($group_id, $memberships)) {
             $canHaveXNbrOfSites = $extra['sites'];
         }
     }
     if (!$canHaveXNbrOfSites) {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'error', 'message' => 'You don\'t have the right to register this plugin with this site.')) . ')');
     }
     $plugin_access = get_usermeta($user->ID, 'plugin_access');
     /*			if(!array_key_exists($id,$plugin_access))
     				die($_GET['callback'].'('.json_encode(array('status'=>'error','message'=>'This user account don\'t have access.')).')');
     */
     if ($regkey != $plugin_access['key']) {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'error', 'type' => 0, 'message' => 'The supplied registration key does not match this user account.')) . ')');
     }
     $user_sites = $plugin_access[$id]['sites'];
     if (!is_array($user_sites) && empty($user_sites)) {
         $user_sites = array();
     }
     if (sizeof($user_sites) >= $canHaveXNbrOfSites && $canHaveXNbrOfSites !== '*') {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'error', 'message' => 'You cannot register more sites.', 'sites' => $canHaveXNbrOfSites, 'left' => $canHaveXNbrOfSites - sizeof($user_sites))) . ')');
     }
     if (array_key_exists($siteid, $user_sites)) {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'error', 'type' => 1, 'message' => 'This site is already registered. If your uninstallation has failed login to your account at artofwp.com and unregister this site.', 'sites' => $canHaveXNbrOfSites, 'left' => $canHaveXNbrOfSites - sizeof($user_sites))) . ')');
     }
     $user_sites[$siteid] = $domain;
     $plugin_access[$id]['sites'] = $user_sites;
     update_usermeta($user->ID, 'plugin_access', $plugin_access);
     if ($canHaveXNbrOfSites !== '*') {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'ok', 'sites' => $canHaveXNbrOfSites, 'left' => $canHaveXNbrOfSites - sizeof($user_sites))) . ')');
     } else {
         die($_GET['callback'] . '(' . json_encode(array('status' => 'ok', 'sites' => $canHaveXNbrOfSites, 'left' => $canHaveXNbrOfSites)) . ')');
     }
 }