コード例 #1
0
<?php

if (!isset($shub_account_id) || !isset($shub_message_id)) {
    exit;
}
if ($shub_account_id && $shub_message_id) {
    $envato = new shub_envato_account($shub_account_id);
    if ($shub_account_id && $envato->get('shub_account_id') == $shub_account_id) {
        $envato_message = new shub_message($envato, false, $shub_message_id);
        $envato_message->output_message_page('popup');
    }
}
if ($shub_account_id && !(int) $shub_message_id) {
    $envato = new shub_envato_account($shub_account_id);
    if ($shub_account_id && $envato->get('shub_account_id') == $shub_account_id) {
        /* @var $groups shub_item[] */
        $groups = $envato->get('groups');
        //print_r($groups);
        ?>
	    <form action="" method="post" enctype="multipart/form-data">
		    <input type="hidden" name="_process" value="send_envato_message">
			<?php 
        wp_nonce_field('send-envato' . (int) $envato->get('shub_account_id'));
        ?>
		    <?php 
        $fieldset_data = array('heading' => array('type' => 'h3', 'title' => 'Compose message'), 'class' => 'tableclass tableclass_form tableclass_full', 'elements' => array('item' => array('title' => __('envato Group', 'support_hub'), 'fields' => array()), 'message' => array('title' => __('message', 'support_hub'), 'field' => array('type' => 'textarea', 'name' => 'message', 'id' => 'envato_compose_message', 'value' => '')), 'type' => array('title' => __('Type', 'support_hub'), 'fields' => array('<input type="radio" name="post_type" id="post_type_wall" value="wall" checked> ', '<label for="post_type_wall">', __('Wall Post', 'support_hub'), '</label>', '<input type="radio" name="post_type" id="post_type_link" value="link"> ', '<label for="post_type_link">', __('Link Post', 'support_hub'), '</label>', '<input type="radio" name="post_type" id="post_type_picture" value="picture"> ', '<label for="post_type_picture">', __('Picture Post', 'support_hub'), '</label>')), 'link' => array('title' => __('Link', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link', 'id' => 'message_link_url', 'value' => ''), '<div id="envato_link_loading_message"></div>', '<span class="envato-type-link envato-type-option"></span>')), 'link_picture' => array('title' => __('Link Picture', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link_picture', 'value' => ''), 'Full URL (eg: http://) to the picture to use for this link preview', '<span class="envato-type-link envato-type-option"></span>')), 'link_name' => array('title' => __('Link Title', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link_name', 'value' => ''), 'Title to use instead of the automatically generated one from the Link page', '<span class="envato-type-link envato-type-option"></span>')), 'link_caption' => array('title' => __('Link Caption', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link_caption', 'value' => ''), 'Caption to use instead of the automatically generated one from the Link page', '<span class="envato-type-link envato-type-option"></span>')), 'link_description' => array('title' => __('Link Description', 'support_hub'), 'fields' => array(array('type' => 'text', 'name' => 'link_description', 'value' => ''), 'Description to use instead of the automatically generated one from the Link page', '<span class="envato-type-link envato-type-option"></span>')), 'picture' => array('title' => __('Picture', 'support_hub'), 'fields' => array('<input type="file" name="picture" value="">', '<span class="envato-type-picture envato-type-option"></span>')), 'schedule' => array('title' => __('Schedule', 'support_hub'), 'fields' => array(array('type' => 'date', 'name' => 'schedule_date', 'value' => ''), array('type' => 'time', 'name' => 'schedule_time', 'value' => ''), ' ', sprintf(__('Currently: %s', 'support_hub'), date('c')), ' (Leave blank to send now, or pick a date in the future.)')), 'debug' => array('title' => __('Debug', 'support_hub'), 'field' => array('type' => 'check', 'name' => 'debug', 'value' => '1', 'checked' => false, 'help' => 'Show debug output while posting the message'))));
        foreach ($groups as $item_id => $group) {
            $fieldset_data['elements']['item']['fields'][] = '<div id="envato_compose_group_select">' . '<input type="checkbox" name="compose_group_id[' . $item_id . ']" value="1" checked> ' . '<img src="//graph.envato.com/' . $item_id . '/picture"> ' . htmlspecialchars($group->get('item_name')) . '</div>';
        }
        echo shub_module_form::generate_fieldset($fieldset_data);
        ?>
コード例 #2
0
            }
            ?>
				</p>


			</form>
		</div>
	<?php 
        }
    }
} else {
    // show account overview:
    $myListTable = new SupportHub_Account_Data_List_Table();
    $accounts = $shub_envato->get_accounts();
    foreach ($accounts as $account_id => $account) {
        $a = new shub_envato_account($account['shub_account_id']);
        $accounts[$account_id]['edit_link'] = $a->link_edit();
        $accounts[$account_id]['title'] = $a->get('account_name');
        $accounts[$account_id]['last_checked'] = $a->get('last_checked') ? shub_print_date($a->get('last_checked')) : 'N/A';
    }
    $myListTable->set_data($accounts);
    $myListTable->prepare_items();
    ?>
	<div class="wrap">
		<h2>
			<?php 
    _e('Envato Accounts', 'support_hub');
    ?>
			<a href="?page=<?php 
    echo esc_attr($_GET['page']);
    ?>
コード例 #3
0
 public function pull_purchase_code($api, $purchase_code, $api_raw_data = array(), $existing_shub_user_id = false)
 {
     $purchase_code = strtolower(preg_replace('#([a-z0-9]{8})-?([a-z0-9]{4})-?([a-z0-9]{4})-?([a-z0-9]{4})-?([a-z0-9]{12})#', '$1-$2-$3-$4-$5', $purchase_code));
     // todo: add documentation that it needs to be named "Purchase Code"
     // todo: add a default extra value called Purchase Code.
     if (strlen($purchase_code) != 36) {
         return false;
     }
     // check existing purchase code and return that if it's cached
     $existing_purchase = shub_get_single('shub_envato_purchase', 'purchase_code', $purchase_code);
     if ($existing_purchase && $existing_purchase['shub_envato_purchase_id']) {
         // if there is a matching support id, grab that cached support data, add shub_user_id do it and return it
         // this matches our return data below.
         // if we don't find this then we continue below and call the API again to get fresh data.
         $existing_support = shub_get_single('shub_envato_support', array('shub_envato_purchase_id'), array($existing_purchase['shub_envato_purchase_id']));
         if ($existing_support && $existing_support['shub_user_id'] && $existing_support['support_data']) {
             $return = @json_decode($existing_support['support_data'], true);
             if ($return) {
                 $return['shub_user_id'] = $existing_support['shub_user_id'];
                 return $return;
             }
         }
     }
     $accounts = array();
     if (!$api) {
         // loop through accounts and try an API call on each one.
         $accounts = $this->get_accounts();
     }
     do {
         if ($accounts) {
             // grab an API from this account.
             $account = array_shift($accounts);
             $shub_envato_account = new shub_envato_account($account['shub_account_id']);
             // found the account, pull in the API and build the url
             $api = $shub_envato_account->get_api();
         }
         if (!$api) {
             break;
         }
         //            $result = $api->api('v1/market/private/user/verify-purchase:' . $purchase_code . '.json');
         $result = $api->api('v2/market/author/sale?code=' . $purchase_code);
         /*
                     {
                       "amount": "12.60",
                       "sold_at": "2015-08-18T21:30:02+10:00",
                       "item": {
                         "id": 1299019,
                         "name": "WooCommerce Australia Post Shipping Calculator",
                         "description": ...
                         "site": "codecanyon.net",
                         "classification": "wordpress/ecommerce/woocommerce/shipping",
                         "classification_url": "http://codecanyon.net/category/wordpress/ecommerce/woocommerce/shipping",
                         "price_cents": 1800,
                         "number_of_sales": 1054,
                         "author_username": "******",
                         "author_url": "http://codecanyon.net/user/dtbaker",
                         "author_image": "https://0.s3.envato.com/files/111547951/dtbaker-php-scripts-wordpress-themes-and-plugins.png",
                         "url": "http://codecanyon.net/item/woocommerce-australia-post-shipping-calculator/1299019",
                         "thumbnail_url": "https://0.s3.envato.com/files/15047665/thumb.png",
                         "summary": "High Resolution: No, Compatible With: WooCommerce 2.3.x, Software Version: WordPress 4.2, WordPress 4.1, WordPress 4.0, WordPress 3.9, WordPress 3.8, WordPress 3.7",
                         "rating": {
                           "rating": 3.82,
                           "count": 49
                         },
                         "updated_at": "2015-08-13T12:34:59+10:00",
                         "published_at": "2012-01-15T17:59:18+11:00",
                         "trending": false,
                         "previews": {
                           "landscape_preview": {
                             "landscape_url": "https://image-cc.s3.envato.com/files/15047663/preview.jpg"
                           }
                         }
                       },
                       "license": "Regular License",
                       "code": "c77ff344-9be4-4ba1-9e50-ce92e37c33e0",
                       "support_amount": ""
                     }
                     }*/
         if ($result && !empty($result['item']) && !empty($result['item']['id'])) {
             // valid purchase code.
             // what is the username attached to this purchase result?
             $envato_username = $result['buyer'];
             // find this user in our system.
             $shub_user = new SupportHubUser_Envato();
             if ($existing_shub_user_id) {
                 $shub_user->load($existing_shub_user_id);
             } else {
                 $shub_user->load_by_meta('envato_username', strtolower($envato_username));
             }
             if (!$shub_user->get('shub_user_id')) {
                 // no users exists in our system with this username.
                 // add a new one.
                 $shub_user->create_new();
             }
             if (!$shub_user->get('user_username')) {
                 $shub_user->update('user_username', $envato_username);
             }
             $shub_user->add_unique_meta('envato_username', strtolower($envato_username));
             // find out which product this purchase code is relating to
             $existing_products = SupportHub::getInstance()->get_products();
             // check if this item exists already
             $exists = false;
             foreach ($existing_products as $existing_product) {
                 if (isset($existing_product['product_data']['envato_item_id']) && $existing_product['product_data']['envato_item_id'] == $result['item']['id']) {
                     $exists = $existing_product['shub_product_id'];
                 }
             }
             $newproduct = new SupportHubProduct();
             if (!$exists) {
                 $newproduct->create_new();
             } else {
                 $newproduct->load($exists);
             }
             if (!$newproduct->get('product_name')) {
                 $newproduct->update('product_name', $result['item']['name']);
             }
             $existing_product_data = $newproduct->get('product_data');
             if (!is_array($existing_product_data)) {
                 $existing_product_data = array();
             }
             if (empty($existing_product_data['envato_item_id'])) {
                 $existing_product_data['envato_item_id'] = $result['item']['id'];
             }
             if (empty($existing_product_data['envato_item_data'])) {
                 // get these item details from api
                 $existing_product_data['envato_item_data'] = $result['item'];
                 if (empty($existing_product_data['image'])) {
                     $existing_product_data['image'] = $result['item']['thumbnail_url'];
                 }
                 if (empty($existing_product_data['url'])) {
                     $existing_product_data['url'] = $result['item']['url'];
                 }
             }
             $newproduct->update('product_data', $existing_product_data);
             if ($newproduct->get('shub_product_id')) {
             }
             $shub_envato_purchase_id = false;
             // store this in our purchase code database so we can access it easier later on
             $existing_purchase = shub_get_single('shub_envato_purchase', 'purchase_code', $purchase_code);
             if (!$existing_purchase) {
                 // see if we can find an existing purchase by this user at the same time, without a purchase code.
                 // (because results from the purchase api do not contian purchase codes)
                 $possible_purchases = shub_get_multiple('shub_envato_purchase', array('shub_user_id' => $shub_user->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'purchase_time' => strtotime($result['sold_at'])));
                 foreach ($possible_purchases as $possible_purchase) {
                     if (empty($possible_purchases['purchase_code'])) {
                         // this purchase came from the other api and doesn't have a purchase code.
                         // add it in!
                         $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', $possible_purchase['shub_envato_purchase_id'], 'shub_envato_purchase', array('purchase_code' => $purchase_code, 'api_time' => time()));
                         if (empty($possible_purchases['purchase_data'])) {
                             $raw_purchase_data = array_merge(array('author/sale' => $result), is_array($api_raw_data) ? $api_raw_data : array());
                             $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', $possible_purchase['shub_envato_purchase_id'], 'shub_envato_purchase', array('purchase_data' => json_encode($raw_purchase_data)));
                         }
                     }
                 }
             } else {
                 // we do have an existing purchase.
                 $shub_envato_purchase_id = $existing_purchase['shub_envato_purchase_id'];
                 //                    if (empty($existing_purchase['shub_user_id'])) {
                 shub_update_insert('shub_envato_purchase_id', $shub_envato_purchase_id, 'shub_envato_purchase', array('shub_user_id' => $shub_user->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'purchase_time' => strtotime($result['sold_at'])));
                 //                    }
             }
             if (!$shub_envato_purchase_id) {
                 // add new one
                 $raw_purchase_data = array_merge(array('author/sale' => $result), is_array($api_raw_data) ? $api_raw_data : array());
                 $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', false, 'shub_envato_purchase', array('shub_user_id' => $shub_user->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'envato_user_id' => 0, 'api_time' => time(), 'api_type' => 'author/sale', 'purchase_time' => strtotime($result['sold_at']), 'purchase_code' => $purchase_code, 'purchase_data' => json_encode($raw_purchase_data)));
             }
             if ($shub_envato_purchase_id) {
                 // support expiry time is 6 months from the purchase date, or as specified by the api result.
                 if (strtotime($result['sold_at']) < strtotime("2015-09-01")) {
                     $support_expiry_time = strtotime("+6 months", strtotime("2015-09-01"));
                 } else {
                     $support_expiry_time = strtotime("+6 months", strtotime($result['sold_at']));
                 }
                 if (!empty($result['supported_until'])) {
                     $support_expiry_time = strtotime($result['supported_until']);
                 }
                 $existing_support = shub_get_single('shub_envato_support', array('shub_envato_purchase_id'), array($shub_envato_purchase_id));
                 if ($existing_support && empty($existing_support['shub_user_id'])) {
                     shub_update_insert('shub_envato_support_id', $existing_support['shub_envato_support_id'], 'shub_envato_support', array('shub_user_id' => $shub_user->get('shub_user_id')));
                 }
                 if ($existing_support && $existing_support['shub_envato_support_id'] && $existing_support['start_time'] == strtotime($result['sold_at'])) {
                     // check the existing support expiry matches the one we have in the database.
                     if ($existing_support['end_time'] < $support_expiry_time) {
                         // we have a support extension!
                         $shub_envato_support_id = shub_update_insert('shub_envato_support_id', $existing_support['shub_envato_support_id'], 'shub_envato_support', array('end_time' => $support_expiry_time, 'api_type' => 'author/sale', 'support_data' => json_encode($result)));
                     }
                 } else {
                     // we are adding a new support entry
                     $shub_envato_support_id = shub_update_insert('shub_envato_support_id', false, 'shub_envato_support', array('shub_user_id' => $shub_user->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'shub_envato_purchase_id' => $shub_envato_purchase_id, 'api_type' => 'author/sale', 'start_time' => strtotime($result['sold_at']), 'end_time' => $support_expiry_time, 'support_data' => json_encode($result)));
                 }
             }
             $result['shub_user_id'] = $shub_user->get('shub_user_id');
             return $result;
         }
     } while (count($accounts));
     // if we get here it means the API request failed or it's an invalid purchase code.
     // log it in our database so we can at least re-check it at a later point in time or show it as failed in the UI somehow.
     $existing_purchase = shub_get_single('shub_envato_purchase', 'purchase_code', $purchase_code);
     if (!$existing_purchase) {
         $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', false, 'shub_envato_purchase', array('shub_user_id' => $existing_shub_user_id, 'shub_product_id' => 0, 'envato_user_id' => 0, 'api_type' => 'fail', 'purchase_time' => 0, 'purchase_code' => $purchase_code, 'purchase_data' => ''));
     }
     return false;
 }
コード例 #4
0
 public function update_purchase_history()
 {
     $tokens = shub_get_multiple('shub_envato_oauth', array('shub_user_id' => $this->shub_user_id));
     // find the latest token for this user, per account.
     $account_tokens = array();
     // if any of them have expired, refresh the token from the api
     foreach ($tokens as $token) {
         if (!$token['shub_account_id']) {
             continue;
         }
         if (!isset($account_tokens[$token['shub_account_id']]) || $token['expire_time'] > $account_tokens[$token['shub_account_id']]['expire_time']) {
             $account_tokens[$token['shub_account_id']] = $token;
         }
     }
     foreach ($account_tokens as $account_token) {
         $shub_envato_account = new shub_envato_account($account_token['shub_account_id']);
         // found the account, pull in the API and build the url
         $api = $shub_envato_account->get_api();
         $api->set_manual_token($account_token);
         if ($account_token['expire_time'] <= time()) {
             // renew this token!
             $new_access_token = $api->refresh_token();
             if ($new_access_token) {
                 shub_update_insert('shub_envato_oauth_id', $account_token['shub_envato_oauth_id'], 'shub_envato_oauth', array('access_token' => $new_access_token, 'expire_time' => time() + 3600));
             } else {
                 echo 'Token refresh failed';
                 return false;
             }
         }
         $api_result = $api->api('v1/market/private/user/username.json', array(), false);
         $api_result_email = $api->api('v1/market/private/user/email.json', array(), false);
         if ($api_result && !empty($api_result['username'])) {
             $this->add_unique_meta('envato_username', $api_result['username']);
         }
         if ($api_result_email && !empty($api_result_email['email'])) {
             $email = trim(strtolower($api_result_email['email']));
             // todo: not sure if best to update users eamail , if they change email accounts and stuff
             $this->update('user_email', $email);
         }
         $api_result_purchase_history = $api->api('v2/market/buyer/purchases', array(), false);
         // store this purchase history in our db for later use.
         if ($api_result_purchase_history && !empty($api_result_purchase_history['buyer']['id']) && !empty($api_result_purchase_history['buyer']['username']) && $api_result_purchase_history['buyer']['username'] == $api_result['username']) {
             // we have the buyer ID! yay! this is better than a username.
             $this->add_unique_meta('envato_user_id', $api_result_purchase_history['buyer']['id']);
             if (!empty($api_result_purchase_history['purchases']) && is_array($api_result_purchase_history['purchases'])) {
                 foreach ($api_result_purchase_history['purchases'] as $purchase) {
                     if (!empty($purchase['item']['id'])) {
                         // todo: beg envato to add the purchase code to this output so we can link it together correctly.
                         // find out which shub product this is for
                         // if we cannot find one then we create one. this helps when new items are made.
                         $existing_products = SupportHub::getInstance()->get_products();
                         // check if this item exists already
                         $exists = false;
                         foreach ($existing_products as $existing_product) {
                             if (isset($existing_product['product_data']['envato_item_id']) && $existing_product['product_data']['envato_item_id'] == $purchase['item']['id']) {
                                 $exists = $existing_product['shub_product_id'];
                             }
                         }
                         $newproduct = new SupportHubProduct();
                         if (!$exists) {
                             $newproduct->create_new();
                         } else {
                             $newproduct->load($exists);
                         }
                         if (!$newproduct->get('product_name')) {
                             $newproduct->update('product_name', $purchase['item']['name']);
                         }
                         $existing_product_data = $newproduct->get('product_data');
                         if (!is_array($existing_product_data)) {
                             $existing_product_data = array();
                         }
                         if (empty($existing_product_data['envato_item_id'])) {
                             $existing_product_data['envato_item_id'] = $purchase['item']['id'];
                         }
                         if (empty($existing_product_data['envato_item_data'])) {
                             $existing_product_data['envato_item_data'] = $purchase['item'];
                         }
                         if (empty($existing_product_data['image'])) {
                             $existing_product_data['image'] = $purchase['item']['thumbnail_url'];
                         }
                         if (empty($existing_product_data['url'])) {
                             $existing_product_data['url'] = $purchase['item']['url'];
                         }
                         $newproduct->update('product_data', $existing_product_data);
                         if ($newproduct->get('shub_product_id')) {
                             // product has been added
                             // time to add it to the purchase db
                             // check if this already exists in the db
                             $existing_purchase = shub_get_single('shub_envato_purchase', array('purchase_code'), array($purchase['code']));
                             if (!$existing_purchase) {
                                 $shub_envato_purchase_id = shub_update_insert('shub_envato_purchase_id', false, 'shub_envato_purchase', array('shub_user_id' => $this->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'purchase_time' => strtotime($purchase['sold_at']), 'envato_user_id' => $api_result_purchase_history['buyer']['id'], 'purchase_code' => $purchase['code'], 'api_type' => 'buyer/purchases', 'purchase_data' => json_encode($purchase)));
                             } else {
                                 if (!$existing_purchase['shub_user_id']) {
                                     shub_update_insert('shub_envato_purchase_id', $existing_purchase['shub_envato_purchase_id'], 'shub_envato_purchase', array('shub_user_id' => $this->get('shub_user_id')));
                                 }
                                 $shub_envato_purchase_id = $existing_purchase['shub_envato_purchase_id'];
                             }
                             if ($shub_envato_purchase_id) {
                                 // we have a purchase in the db
                                 // add or update the support expiry based on this purchase history.
                                 // work out when this purchase support expires
                                 // this is the expiry date returned in the api or just 6 months from the original purchase date.
                                 $support_expiry_time = strtotime("+6 months", strtotime($purchase['sold_at']));
                                 // todo - check for this expiry time in the new api results.
                                 $existing_support = shub_get_single('shub_envato_support', array('shub_envato_purchase_id'), array($shub_envato_purchase_id));
                                 if ($existing_support && empty($existing_support['shub_user_id'])) {
                                     shub_update_insert('shub_envato_support_id', $existing_support['shub_envato_support_id'], 'shub_envato_support', array('shub_user_id' => $this->get('shub_user_id')));
                                 }
                                 if ($existing_support && $existing_support['shub_envato_support_id'] && $existing_support['start_time'] == strtotime($purchase['sold_at'])) {
                                     // check the existing support expiry matches the one we have in the database.
                                     if ($existing_support['end_time'] < $support_expiry_time) {
                                         // we have a support extension!
                                         $shub_envato_support_id = shub_update_insert('shub_envato_support_id', $existing_support['shub_envato_support_id'], 'shub_envato_support', array('end_time' => $support_expiry_time, 'api_type' => 'buyer/purchases', 'support_data' => json_encode($purchase)));
                                     }
                                 } else {
                                     // we are adding a new support entry
                                     $shub_envato_support_id = shub_update_insert('shub_envato_support_id', false, 'shub_envato_support', array('shub_user_id' => $this->get('shub_user_id'), 'shub_product_id' => $newproduct->get('shub_product_id'), 'shub_envato_purchase_id' => $shub_envato_purchase_id, 'start_time' => strtotime($purchase['sold_at']), 'end_time' => $support_expiry_time, 'api_type' => 'buyer/purchases', 'support_data' => json_encode($purchase)));
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return true;
 }