diff --git a/trunk/chargify.php b/trunk/chargify.php index 349b45e..79c2d4f 100644 --- a/trunk/chargify.php +++ b/trunk/chargify.php @@ -1,11 +1,13 @@ ID; + if(empty($user->chargify_custid)) + return; + + $d = get_option('chargify'); + $opt = array("api_key" => $d["chargifyApiKey"],"test_api_key" => $d["chargifyTestApiKey"],"domain" => $d["chargifyDomain"],"test_domain" => $d["chargifyTestDomain"],"test_mode"=>($d["chargifyMode"] == 'test'? TRUE : FALSE)); + $connector = new ChargifyConnector($opt); + + $sub = $connector->getSubscriptionsByCustomerID($user->chargify_custid); + if(is_array($sub)) + { + //get user subscriptions + $usub = get_usermeta($user_id,'chargify_level',true); + foreach($sub as $s) + { + switch($s->getState()) + { + case 'canceled': + case 'expired': + case 'trial_ended': + if(isset($usub[$s->getProduct()->getHandle()])) + { + unset($usub[$s->getProduct()->getHandle()]); + update_usermeta($user_id,'chargify_level',$usub); + } + break; + + case 'paused': + case 'trialing': + case 'active': + if(!isset($usub[$s->getProduct()->getHandle()])) + { + $usub[$s->getProduct()->getHandle()] = time(); + update_usermeta($user_id,'chargify_level',$usub); + } + break; + } + } + } + } function adminScripts() { $pluginurl = WP_PLUGIN_URL . '/' . plugin_basename(dirname(__FILE__)); @@ -61,12 +105,12 @@ function manualTrim($text) function checkAccess($posts) { $chargify = get_option('chargify'); - + $u = wp_get_current_user(); if($u->roles[0] == 'administrator') { return $posts; - } + } if($u->ID && get_user_meta($u->ID,'chargify_access_check',true) < time()) { @@ -75,7 +119,7 @@ function checkAccess($posts) foreach($posts as $k => $post) { - $d = get_post_meta($post->ID, 'chargify_access', true); + $d = get_post_meta($post->ID, 'chargify_access', true); if(isset($d['levels']) && is_array($d["levels"]) && !empty($d['levels'])) { if(!$u->ID ||(is_array($d["levels"]) && !array_intersect_key($u->chargify_level,$d["levels"]))) @@ -86,7 +130,7 @@ function checkAccess($posts) $post->post_content = strlen(trim($post->post_excerpt)) ? $post->post_excerpt : self::manualTrim($post->post_content); break; default: - $post->post_content = self::partialcontent($post->post_content); + $post->post_content = self::partialcontent($post->post_content); } } else @@ -106,13 +150,13 @@ function checkAccess($posts) $post->post_content = strlen(trim($post->post_excerpt)) ? $post->post_excerpt : self::manualTrim($post->post_content); break; default: - $post->post_content = self::partialcontent($post->post_content); + $post->post_content = self::partialcontent($post->post_content); } } } } } - + $posts[$k] = $post; } return $posts; @@ -135,7 +179,7 @@ function products($fresh = false) $d = get_option('chargify'); $opt = array("api_key" => $d["chargifyApiKey"],"test_api_key" => $d["chargifyTestApiKey"],"domain" => $d["chargifyDomain"],"test_domain" => $d["chargifyTestDomain"],"test_mode"=>($d["chargifyMode"] == 'test'? TRUE : FALSE)); if($fresh) - { + { $connector = new ChargifyConnector($opt); $products = $connector->getAllProducts(); } @@ -169,7 +213,7 @@ function partialprotect($atts,$content = null) global $post; $chargify = get_option("chargify"); - $d = get_post_meta($post->ID, 'chargify_access', true); + $d = get_post_meta($post->ID, 'chargify_access', true); $u = wp_get_current_user(); if(current_user_can( 'manage_options' )) return $content; @@ -212,6 +256,7 @@ function subscriptionListShortCode($atts) } $d = get_option("chargify"); $return_url = $_GET['return_url']; + $plan = $_GET['plan']; if($_POST['chargifySignupFirst']) $first = $_POST['chargifySignupFirst']; @@ -235,7 +280,7 @@ function subscriptionListShortCode($atts) if($d["chargifySignupType"] == 'api') - { + { $monthDrop = ''; - + $products = self::products(); $form ='