Skip to content

Sort of works again :-) #62

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@
*0.0.20 (2019-07-07)*
* Update UI layout to match current WordPress standards
* Apply WordPress coding standards to code

*0.3.2 (2025-05-15)*
* Fixes assignment of auth_token from API login
* Renames typo `transcactions` to `transactions`
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Codeable Expert Stats Plugin Readme
*Contributors:* Spyros Vlachopoulos, Panagiotis Synetos, John Leskas, Justin Frydman, Jonathan Bossenger, Rob Scott, Philipp Stracker, Milan Dinić

*Tested up to:* WordPress 5.2.2
*Tested up to:* WordPress 6.8.1

Codeable Expert Stats plugin makes it easy for you to track and monitor your success as an expert on Codeable via an easy-to-understand dashboard for your personal/agency WordPress site.

Expand Down
24 changes: 14 additions & 10 deletions classes/api_calls.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public function login( $email, $password ) {
}

$this->set_auth_token( $login_call['auth_token'] );

$account_details = $login_call;
unset( $account_details['auth_token']);
update_option( 'wpcable_account_details', $account_details );

update_option( 'wpcable_average', $account_details['stats']['avg_task_size'] );
update_option( 'wpcable_balance', $account_details['balance'] );
}

/**
Expand Down Expand Up @@ -155,7 +162,7 @@ public function self() {
* @return array
*/
public function transactions_page( $page = 1 ) {
$url = 'users/me/transactions';
$url = 'experts/transactions/';
$args = [ 'page' => $page ];

$transactions = $this->request( $url, $args, 'get' );
Expand Down Expand Up @@ -241,15 +248,6 @@ private function request( $url, $args = [], $method = 'POST', $headers = [] ) {
}

$response_body = json_decode( $response['body'], true );

if( isset( $response['headers'] ) ) {

$full_header = $response['headers']->getAll();
if ( isset( $full_header['auth-token'] ) && !empty( $full_header['auth-token'] ) ) {

$response_body['auth_token'] = $full_header['auth-token'];
}
}

if ( is_array( $response_body ) && ! empty( $response_body['errors'] ) ) {
if ( false !== array_search( 'Invalid login credentials', $response_body['errors'], true ) ) {
Expand All @@ -260,6 +258,12 @@ private function request( $url, $args = [], $method = 'POST', $headers = [] ) {
}
}

$data = $response['headers']->getAll();

if ( isset( $data['auth-token'] ) ) {
$response_body['auth_token'] = $data['auth-token'];
}

return $response_body;
}
}
123 changes: 93 additions & 30 deletions classes/api_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct() {
global $wpdb;

$this->tables = [
'transcactions' => $wpdb->prefix . 'codeable_transcactions',
'transactions' => $wpdb->prefix . 'codeable_transactions',
'clients' => $wpdb->prefix . 'codeable_clients',
'amounts' => $wpdb->prefix . 'codeable_amounts',
'tasks' => $wpdb->prefix . 'codeable_tasks',
Expand All @@ -57,12 +57,18 @@ public function __construct() {
public function prepare_queue() {
$queue = [];

/*

Profile comes from login API call, no need to do it again.

$queue[] = [
'task' => 'profile',
'label' => 'User profile',
'page' => 0,
'paged' => false,
];

*/
$queue[] = [
'task' => 'transactions',
'label' => 'Transactions',
Expand Down Expand Up @@ -226,54 +232,99 @@ private function store_transactions( $page ) {
$single_page = $this->api_calls->transactions_page( $page );

if ( 2 === $page ) {
update_option( 'wpcable_average', $single_page['average_task_size'] );
update_option( 'wpcable_balance', $single_page['balance'] );
update_option( 'wpcable_revenue', $single_page['revenue'] );
//wc_get_logger()->debug( print_r( $single_page, 1 ), ['source' => 'Transactions page 2'] );
// TODO: find out revenue
//update_option( 'wpcable_revenue', $single_page['revenue'] );
}

if ( empty( $single_page['transactions'] ) ) {
if ( empty( $single_page ) ) {

return false;

} else {


// Get all data to the DB.
foreach ( $single_page['transactions'] as $tr ) {
foreach ( $single_page as $tr ) {

if ( ! in_array( $tr['description'], array( 'ad_hoc_expert_credit', 'partial_refund', 'task_completion' ) ) ) {

/*
ad_hoc_expert_credit
ad_hoc_expert_debit
ad_hoc_team_work
contractor_withdrawal
expert_withdrawal_request
partial_refund
task_completion

*/

continue;
}


// Check if transactions already exists.
$check = $wpdb->get_results(
"SELECT COUNT(1) AS totalrows
FROM `{$this->tables['transcactions']}`
FROM `{$this->tables['transactions']}`
WHERE id = '{$tr['id']}';
"
);

$exists = $check[0]->totalrows > 0;

$new_tr = [
'id' => $tr['id'],
// Is it an additional task?
if ( isset( $tr['resources']['sub_task'] ) ) {

$task_id = $tr['resources']['sub_task']['id'];

$new_tr = [
'id' => $tr['id'],
'description' => $tr['description'],
'dateadded' => $tr['created_at'],
'fee_percentage' => $tr['fee_percentage'],
'fee_amount' => $tr['fee_amount'],
'task_type' => $tr['resources']['sub_task']['kind'],
'task_id' => $tr['resources']['sub_task']['id'],
'task_title' => $tr['resources']['sub_task']['title'],
'parent_task_id' => $tr['resources']['sub_task']['parent_task_id'],
'preferred' => $tr['resources']['sub_task']['current_user_is_preferred_contractor'],
'client_id' => $tr['resources']['client']['id'],
'last_sync' => time(),
];

} else {

$task_id = $tr['resources']['project']['id'];

$new_tr = [
'id' => $tr['id'],
'description' => $tr['description'],
'dateadded' => date( 'Y-m-d H:i:s', $tr['timestamp'] ),
'fee_percentage' => $tr['fee_percentage'],
'fee_amount' => $tr['fee_amount'],
'task_type' => $tr['task']['kind'],
'task_id' => $tr['task']['id'],
'task_title' => $tr['task']['title'],
'parent_task_id' => ( $tr['task']['parent_task_id'] > 0 ? $tr['task']['parent_task_id'] : 0 ),
'preferred' => $tr['task']['current_user_is_preferred_contractor'],
'client_id' => $tr['task_client']['id'],
'last_sync' => time(),
];
'dateadded' => $tr['created_at'],
'fee_percentage' => $tr['fee_percentage'],
'fee_amount' => $tr['fee_amount'],
'task_type' => $tr['resources']['project']['kind'],
'task_id' => $tr['resources']['project']['id'],
'task_title' => $tr['resources']['project']['title'],
'parent_task_id' => $tr['resources']['project']['parent_task_id'],
'preferred' => $tr['resources']['project']['current_user_is_preferred_contractor'],
'client_id' => $tr['resources']['client']['id'],
'last_sync' => time(),
];
}

// the API is returning some blank rows, ensure we have a valid client_id.
if ( $new_tr['id'] && is_int( $new_tr['id'] ) ) {
if ( $exists ) {
$db_res = $wpdb->update(
$this->tables['transcactions'],
$this->tables['transactions'],
$new_tr,
[ 'id' => $tr['id'] ]
);
} else {
$db_res = $wpdb->insert(
$this->tables['transcactions'],
$this->tables['transactions'],
$new_tr
);
}
Expand All @@ -289,17 +340,14 @@ private function store_transactions( $page ) {

$this->store_client( $tr['task_client'] );
$this->store_amount(
$tr['task']['id'],
$tr['task_client']['id'],
$tr['credit_amounts'],
$tr['debit_amounts']
$task_id,
$tr['resources']['client']['id'],
$tr['amount'],
$tr['fee_amount']
);

// If we find a transaction that already exists, bail out and don't continue updating all the transactions
if ( $exists ) {
update_option( 'wpcable_average', $single_page['average_task_size'] );
update_option( 'wpcable_balance', $single_page['balance'] );
update_option( 'wpcable_revenue', $single_page['revenue'] );
return false;
}

Expand Down Expand Up @@ -563,7 +611,7 @@ private function store_amount( $task_id, $client_id, $credit, $debit ) {
return;
}

$new_amount = [
/*$new_amount = [
'task_id' => $task_id,
'client_id' => $client_id,
'credit_revenue_id' => $credit[0]['id'],
Expand All @@ -576,6 +624,21 @@ private function store_amount( $task_id, $client_id, $credit, $debit ) {
'debit_cost_amount' => $debit[0]['amount'],
'debit_user_id' => $debit[1]['id'],
'debit_user_amount' => $debit[1]['amount'],
];*/

$new_amount = [
'task_id' => $task_id,
'client_id' => $client_id,
'credit_revenue_id' => $task_id,
'credit_revenue_amount' => $credit,
'credit_fee_id' => $task_id,
'credit_fee_amount' => $credit,
'credit_user_id' => $client_id,
'credit_user_amount' => $credit,
'debit_cost_id' => $task_id,
'debit_cost_amount' => $debit,
'debit_user_id' => $client_id,
'debit_user_amount' => $debit,
];

$wpdb->replace( $this->tables['amounts'], $new_amount );
Expand Down
4 changes: 2 additions & 2 deletions classes/clients.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct() {
global $wpdb;

$this->tables = array(
'transactions' => $wpdb->prefix . 'codeable_transcactions',
'transactions' => $wpdb->prefix . 'codeable_transactions',
'clients' => $wpdb->prefix . 'codeable_clients',
'amounts' => $wpdb->prefix . 'codeable_amounts',
);
Expand Down Expand Up @@ -59,7 +59,7 @@ public function get_clients( $from_month = '', $from_year = '', $to_month = '',
ON
' . $this->tables['transactions'] . '.task_ID = ' . $this->tables['amounts'] . ".task_ID
WHERE
`description` = 'task_completion' OR `description` = 'partial_refund'
`description` = 'task_completion' or `description` = 'ad_hoc_expert_credit' OR `description` = 'partial_refund'
AND (dateadded BETWEEN '" . $firstdate . "' AND '" . $lastdate . "')
";

Expand Down
Loading