Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

CRM: Fix companies API endpoint param mapping #41508

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions projects/plugins/crm/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
// PhanUnextractableAnnotation : 2 occurrences
// PhanImpossibleTypeComparisonInLoop : 1 occurrence
// PhanNoopVariable : 1 occurrence
// PhanParamTooMany : 1 occurrence
// PhanPluginDuplicateArrayKey : 1 occurrence
// PhanPluginDuplicateCatchStatementBody : 1 occurrence
// PhanPluginUseReturnValueInternalKnown : 1 occurrence
Expand Down Expand Up @@ -162,7 +161,7 @@
'admin/system/partials/title.block.php' => ['PhanUndeclaredGlobalVariable'],
'admin/system/system-status.page.php' => ['PhanRedundantCondition', 'PhanTypePossiblyInvalidDimOffset'],
'admin/tags/tag-manager.page.php' => ['PhanTypeMismatchArgument'],
'api/companies.php' => ['PhanParamTooMany', 'PhanPluginSimplifyExpressionBool'],
'api/companies.php' => ['PhanPluginSimplifyExpressionBool'],
'api/create_company.php' => ['PhanImpossibleTypeComparisonInGlobalScope', 'PhanPossiblyUndeclaredGlobalVariable', 'PhanRedundantConditionInGlobalScope'],
'api/create_customer.php' => ['PhanImpossibleTypeComparisonInGlobalScope', 'PhanPossiblyUndeclaredGlobalVariable', 'PhanRedundantConditionInGlobalScope', 'PhanTypePossiblyInvalidDimOffset'],
'api/create_event.php' => ['PhanImpossibleTypeComparisonInGlobalScope'],
Expand Down
70 changes: 36 additions & 34 deletions projects/plugins/crm/api/companies.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<?php
/*
!
/**
* Jetpack CRM
* https://jetpackcrm.com
* V3.0
*
* Copyright 2020 Automattic
*
* Date: 04/06/2019
*
* @package automattic/jetpack-crm
*/

/*
======================================================
Breaking Checks ( stops direct access )
====================================================== */
if ( ! defined( 'ZEROBSCRM_PATH' ) ) {
exit( 0 );
}
/*
======================================================
/ Breaking Checks
====================================================== */

// Check the method
// Ultimately this should be switched to GET, but the docs have it as POST, so best to wait for a rewrite
Expand All @@ -29,44 +22,53 @@
$json_params = file_get_contents( 'php://input' );
$company_params = json_decode( $json_params, true );

$perPage = 10;
$items_per_page = 10;
if ( isset( $company_params['perpage'] ) ) {
$perPage = sanitize_text_field( $company_params['perpage'] );
$items_per_page = sanitize_text_field( $company_params['perpage'] );
}
$page = 0;
$page_num = 0;
if ( isset( $company_params['page'] ) ) {
$page = sanitize_text_field( $company_params['page'] );
$page_num = sanitize_text_field( $company_params['page'] );
}
$withInvoices = -1;
$with_invoices = -1;
if ( isset( $company_params['invoices'] ) ) {
$withInvoices = sanitize_text_field( $company_params['invoices'] );
$with_invoices = sanitize_text_field( $company_params['invoices'] );
}
$withQuotes = -1;
$with_quotes = -1;
if ( isset( $company_params['quotes'] ) ) {
$withQuotes = sanitize_text_field( $company_params['quotes'] );
}
$searchPhrase = '';
if ( isset( $company_params['search'] ) ) {
$searchPhrase = sanitize_text_field( $company_params['search'] );
$with_quotes = sanitize_text_field( $company_params['quotes'] );
}
$withTransactions = -1;
$with_transactions = -1;
if ( isset( $company_params['transactions'] ) ) {
$withTransactions = sanitize_text_field( $company_params['transactions'] );
$with_transactions = sanitize_text_field( $company_params['transactions'] );
}
$isOwned = -1;
$search_phrase = '';
if ( isset( $company_params['search'] ) ) {
$search_phrase = sanitize_text_field( $company_params['search'] );
}
$owned_by = -1;
if ( isset( $company_params['owned'] ) ) {
$isOwned = (int) $company_params['owned'];
$owned_by = (int) $company_params['owned'];
}

// #FORMIKENOTES -
// These should be Bools - see https://stackoverflow.com/questions/7336861/how-to-convert-string-to-boolean-php
// ... this forces them from string of "true" or "false" into a bool
$withInvoices = $withInvoices === 'true' ? true : false;
$withQuotes = $withQuotes === 'true' ? true : false;
$withTransactions = $withTransactions === 'true' ? true : false;
$isAssigned = false; // ??
$with_invoices = $with_invoices === 'true' ? true : false;
$with_quotes = $with_quotes === 'true' ? true : false;
$with_transactions = $with_transactions === 'true' ? true : false;

$args = array(
'perPage' => $items_per_page,
'page' => $page_num,
'searchPhrase' => $search_phrase,
'ownedBy' => $owned_by,
'withQuotes' => $with_quotes,
'withInvoices' => $with_invoices,
'withTransactions' => $with_transactions,
'sortByField' => 'ID',
'sortOrder' => 'DESC',
);

// needs moving to the $args version
$companies = zeroBS_getCompanies( true, $perPage, $page, $withInvoices, $withQuotes, $searchPhrase, $withTransactions, false, false, '', '', false, false, false, 'ID', 'DESC', false, $isAssigned );
global $zbs;
$companies = $zbs->DAL->companies->getCompanies( $args ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable,WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

wp_send_json( $companies );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

API: Fix `companies` endpoint param mapping.
1 change: 0 additions & 1 deletion tools/phpcs-excludelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"projects/plugins/crm/admin/user-profile/main.page.php",
"projects/plugins/crm/admin/user-profile/reminders.page.php",
"projects/plugins/crm/admin/user-profile/user-profile.page.php",
"projects/plugins/crm/api/companies.php",
"projects/plugins/crm/api/create_company.php",
"projects/plugins/crm/api/create_customer.php",
"projects/plugins/crm/api/create_event.php",
Expand Down
Loading