diff --git a/CHANGELOG.md b/CHANGELOG.md index b32eb7a..bf00611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/README.md b/README.md index 03428a6..3135fb4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/classes/api_calls.php b/classes/api_calls.php index c70e995..3615237 100644 --- a/classes/api_calls.php +++ b/classes/api_calls.php @@ -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'] ); } /** @@ -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' ); @@ -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 ) ) { @@ -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; } } diff --git a/classes/api_data.php b/classes/api_data.php index bf3ac0e..4f537a6 100644 --- a/classes/api_data.php +++ b/classes/api_data.php @@ -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', @@ -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', @@ -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 ); } @@ -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; } @@ -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'], @@ -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 ); diff --git a/classes/clients.php b/classes/clients.php index 854a76c..c70895f 100644 --- a/classes/clients.php +++ b/classes/clients.php @@ -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', ); @@ -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 . "') "; diff --git a/classes/stats.php b/classes/stats.php index 30d01ea..dcf3e21 100644 --- a/classes/stats.php +++ b/classes/stats.php @@ -18,7 +18,7 @@ public function __construct() { global $wpdb; $this->tables = array( - 'transcactions' => $wpdb->prefix . 'codeable_transcactions', + 'transactions' => $wpdb->prefix . 'codeable_transactions', 'clients' => $wpdb->prefix . 'codeable_clients', 'amounts' => $wpdb->prefix . 'codeable_amounts', ); @@ -38,9 +38,9 @@ public function get_dates_totals( $from_day, $from_month, $from_year, $to_day, $ SUM(debit_user_amount) as total_cost, count(1) as tasks FROM - ' . $this->tables['transcactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' + ' . $this->tables['transactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' ON - ' . $this->tables['transcactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id + ' . $this->tables['transactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id WHERE `description` = 'task_completion' AND (dateadded BETWEEN '" . $first_date . "' AND '" . $last_date . "') @@ -69,9 +69,9 @@ public function get_days( $from_day, $from_month, $from_year, $to_day, $to_month debit_user_amount as total_cost, dateadded FROM - ' . $this->tables['transcactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' + ' . $this->tables['transactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' ON - ' . $this->tables['transcactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id + ' . $this->tables['transactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id WHERE `description` = 'task_completion' AND (dateadded BETWEEN '" . $first_date . "' AND '" . $last_date . "') @@ -118,9 +118,9 @@ public function get_dates_average( $from_day, $from_month, $from_year, $to_day, AVG(credit_revenue_amount) as revenue, AVG(debit_user_amount) as total_cost FROM - ' . $this->tables['transcactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' + ' . $this->tables['transactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' ON - ' . $this->tables['transcactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id + ' . $this->tables['transactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id WHERE `description` = 'task_completion' AND (dateadded BETWEEN '" . $first_date . "' AND '" . $last_date . "') @@ -216,12 +216,12 @@ public function get_first_task() { SELECT * FROM - ' . $this->tables['transcactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' + ' . $this->tables['transactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' ON - ' . $this->tables['transcactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id + ' . $this->tables['transactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id WHERE `description` = 'task_completion' - ORDER BY " . $this->tables['transcactions'] . '.id ASC + ORDER BY " . $this->tables['transactions'] . '.id ASC LIMIT 0,1 '; @@ -238,12 +238,12 @@ public function get_last_task() { SELECT * FROM - ' . $this->tables['transcactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' + ' . $this->tables['transactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' ON - ' . $this->tables['transcactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id + ' . $this->tables['transactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id WHERE `description` = 'task_completion' - ORDER BY " . $this->tables['transcactions'] . '.id DESC + ORDER BY " . $this->tables['transactions'] . '.id DESC LIMIT 0,1 '; @@ -270,9 +270,9 @@ public function get_amounts_range( $from_day, $from_month, $from_year, $to_day, SELECT credit_revenue_amount FROM - ' . $this->tables['transcactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' + ' . $this->tables['transactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' ON - ' . $this->tables['transcactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id + ' . $this->tables['transactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id WHERE `description` = 'task_completion' AND (dateadded BETWEEN '" . $first_date . "' AND '" . $last_date . "') @@ -318,7 +318,7 @@ public function get_tasks_per_month( $from_day, $from_month, $from_year, $to_day SELECT DATE_FORMAT(dateadded,'%Y-%m') as dateadded, count(1) as tasks_per_month FROM - " . $this->tables['transcactions'] . " + " . $this->tables['transactions'] . " WHERE `description` = 'task_completion' AND (dateadded BETWEEN '" . $first_date . "' AND '" . $last_date . "') @@ -349,9 +349,9 @@ public function get_tasks_type( $from_day, $from_month, $from_year, $to_day, $to SUM(credit_revenue_amount) as revenue, SUM(credit_fee_amount) as fee FROM - ' . $this->tables['transcactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' + ' . $this->tables['transactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' ON - ' . $this->tables['transcactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id + ' . $this->tables['transactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id WHERE `description` = 'task_completion' AND (dateadded BETWEEN '" . $first_date . "' AND '" . $last_date . "') @@ -391,9 +391,9 @@ public function get_preferred_count( $from_day, $from_month, $from_year, $to_day SUM(credit_revenue_amount) as revenue, SUM(credit_fee_amount) as fee FROM - ' . $this->tables['transcactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' + ' . $this->tables['transactions'] . ' LEFT JOIN ' . $this->tables['amounts'] . ' ON - ' . $this->tables['transcactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id + ' . $this->tables['transactions'] . '.task_id = ' . $this->tables['amounts'] . ".task_id WHERE `description` = 'task_completion' AND (preferred = 1 OR preferred = 0) diff --git a/functions/admin-page.php b/functions/admin-page.php index 588027e..a6a64ac 100644 --- a/functions/admin-page.php +++ b/functions/admin-page.php @@ -15,15 +15,15 @@ function wpcable_register_menu_page() { __( 'Codeable Stats', 'wpcable' ), __( 'Codeable Stats', 'wpcable' ), 'manage_options', - 'codeable_transcactions_stats', - 'codeable_transcactions_stats_cb', + 'codeable_transactions_stats', + 'codeable_transactions_stats_cb', 'data:image/svg+xml;base64,' . base64_encode( $icon_code ), 2 ); } add_action( 'admin_menu', 'wpcable_register_menu_page' ); -function codeable_transcactions_stats_cb() { +function codeable_transactions_stats_cb() { codeable_page_requires_login( __( 'Codeable Stats', 'wpcable' ) ); codeable_maybe_refresh_data(); @@ -141,7 +141,7 @@ function codeable_transcactions_stats_cb() {