Skip to content

Commit

Permalink
CRM: Add the order capability to the API events endpoint. (#38838)
Browse files Browse the repository at this point in the history
* Add to the API event the pagination order support.

* changelog

* Update API pagination processing with new function

* Refactor jpcrm_api_process_pagination for cleaner syntax

* Add sort order for customer search

* Make ASC the default sort order

* Add sort order for quotes

* Add order for invoices

* Add order for transactions

* Update code to use DAL call

* Update code to use DAL call

* Remove unneeded ignoreowner argument

* Make DESC the default sort order (better this way)

---------

Co-authored-by: gogdzl <diego.rodrigues@automattic.com>
  • Loading branch information
cleacos and gogdzl authored Jan 10, 2025
1 parent a996e15 commit 6296b27
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 32 deletions.
8 changes: 4 additions & 4 deletions projects/plugins/crm/api/customer_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
jpcrm_api_check_http_method( array( 'GET' ) );

// Process the pagination parameters from the query
list( $page, $per_page ) = jpcrm_api_process_pagination();

$pagination = jpcrm_api_process_pagination();
$search_phrase = jpcrm_api_process_search();
$replace_hyphens_in_response = jpcrm_api_process_replace_hyphens_in_json_keys();

$args = array(
'searchPhrase' => $search_phrase,
'perPage' => $per_page,
'page' => $page,
'page' => $pagination['page'],
'perPage' => $pagination['per_page'],
'sortOrder' => $pagination['order'],
);

global $zbs;
Expand Down
7 changes: 4 additions & 3 deletions projects/plugins/crm/api/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
jpcrm_api_check_http_method( array( 'GET' ) );

// Process the pagination parameters from the query
list( $page, $per_page ) = jpcrm_api_process_pagination();
$pagination = jpcrm_api_process_pagination();

/**
* Allow events to be filtered by owner. Docs are ambiguous about
Expand All @@ -44,8 +44,9 @@

$args = array(
'withAssigned' => true,
'page' => $page,
'perPage' => $per_page,
'page' => $pagination['page'],
'perPage' => $pagination['per_page'],
'sortOrder' => $pagination['order'],
'ownedBy' => $owner,
'ignoreowner' => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_TASK ),
);
Expand Down
14 changes: 10 additions & 4 deletions projects/plugins/crm/api/invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
jpcrm_api_check_http_method( array( 'GET' ) );

// Process the pagination parameters from the query
list( $page, $per_page ) = jpcrm_api_process_pagination();
$pagination = jpcrm_api_process_pagination();
$args = array(
'withAssigned' => false,
'page' => $pagination['page'],
'perPage' => $pagination['per_page'],
'sortOrder' => $pagination['order'],
);

global $zbs;
$invoices = $zbs->DAL->invoices->getInvoices( $args ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

// needs moving to the $args version
// v3.0 needs these objects refined, including textify for html
$invoices = zeroBS_getInvoices( true, $per_page, $page );

wp_send_json( $invoices );
8 changes: 4 additions & 4 deletions projects/plugins/crm/api/quotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
jpcrm_api_check_http_method( array( 'GET' ) );

// Process the pagination parameters from the query
list( $page, $per_page ) = jpcrm_api_process_pagination();
$pagination = jpcrm_api_process_pagination();

global $zbs;

Expand All @@ -39,9 +39,9 @@
// 'withAssigned' => $withCustomerDeets,
'suppressContent' => true, // NO HTML!
'sortByField' => 'ID',
'sortOrder' => 'DESC',
'page' => $page,
'perPage' => $per_page,
'page' => $pagination['page'],
'perPage' => $pagination['per_page'],
'sortOrder' => $pagination['order'],
'ignoreowner' => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_QUOTE ),
);

Expand Down
14 changes: 10 additions & 4 deletions projects/plugins/crm/api/transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
jpcrm_api_check_http_method( array( 'GET' ) );

// Process the pagination parameters from the query
list( $page, $per_page ) = jpcrm_api_process_pagination();
$pagination = jpcrm_api_process_pagination();
$args = array(
'withAssigned' => false,
'withTags' => false,
'page' => $pagination['page'],
'perPage' => $pagination['per_page'],
'sortOrder' => $pagination['order'],
);

// needs moving to the $args version
// v3.0 needs these objects refined, including textify for html
$transactions = zeroBS_getTransactions( true, $per_page, $page );
global $zbs;
$transactions = $zbs->DAL->transactions->getTransactions( $args ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

wp_send_json( $transactions );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

API-events: Add the pagination result sort/order capability to the endpoint.
23 changes: 10 additions & 13 deletions projects/plugins/crm/includes/ZeroBSCRM.API.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,17 @@ function zeroBS_api_rewrite_endpoint() {
* Process the query and get page and items per page
*/
function jpcrm_api_process_pagination() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$page = isset( $_GET['page'] ) ? max( (int) $_GET['page'], 1 ) : 1;
$per_page = isset( $_GET['perpage'] ) ? max( (int) $_GET['perpage'], 1 ) : 10;
$order = strtoupper( $_GET['order'] ?? '' ) === 'ASC' ? 'ASC' : 'DESC'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
// phpcs:enable WordPress.Security.NonceVerification.Recommended

if ( isset( $_GET['page'] ) && (int) $_GET['page'] >= 0 ) {
$page = (int) $_GET['page'];
} else {
$page = 0;
}

if ( isset( $_GET['perpage'] ) && (int) $_GET['perpage'] >= 0 ) {
$per_page = (int) $_GET['perpage'];
} else {
$per_page = 10;
}

return array( $page, $per_page );
return array(
'page' => $page,
'per_page' => $per_page,
'order' => $order,
);
}

/**
Expand Down

0 comments on commit 6296b27

Please sign in to comment.