Skip to content
Merged
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
4 changes: 2 additions & 2 deletions ZeroBSCRM.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Jetpack CRM
* Plugin URI: https://jetpackcrm.com
* Description: Jetpack CRM is the simplest CRM for WordPress. Self host your own Customer Relationship Manager using WP.
* Version: 6.8.6
* Version: 6.8.7
* Author: Automattic - Jetpack CRM team
* Author URI: https://jetpackcrm.com
* Text Domain: zero-bs-crm
Expand All @@ -26,7 +26,7 @@
// Plugin version. Single source of truth, bumped by the release tooling
// (release.config.json "versionConstant"). ZeroBSCRM::VERSION reads from this,
// so this define must be set before the autoloader loads that class below.
define( 'JPCRM_VERSION', '6.8.6' );
define( 'JPCRM_VERSION', '6.8.7' );

// Enabling THIS will start LOGGING PERFORMANCE TESTS
// NOTE: This will do for EVERY page load, so just add temporarily else adds rolling DRAIN on sys
Expand Down
2 changes: 1 addition & 1 deletion includes/ZeroBSCRM.AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ function ZeroBSCRM_accept_quote() {
wp_send_json_error( array( 'access' => 1 ), 403, JSON_UNESCAPED_SLASHES );
}
// Resolve IDs safely
$customer_id = (int) zeroBS_getCustomerIDWithEmail( $uinfo->user_email );
$customer_id = jpcrm_portal_contact_id_for_user( get_current_user_id() );
$quote_contact_id = (int) $zbs->DAL->quotes->getQuoteContactID( $quoteID ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
// Both IDs must exist and match
if ( $customer_id <= 0 || $quote_contact_id <= 0 || $customer_id !== $quote_contact_id ) {
Expand Down
3 changes: 3 additions & 0 deletions includes/ZeroBSCRM.Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,9 @@ public function includes() {
// Generic CRM exceptions
require_once ZEROBSCRM_INCLUDE_PATH . 'class-crm-exception.php';

// Which contact a WordPress user is (Client Portal identity).
require_once ZEROBSCRM_INCLUDE_PATH . 'jpcrm-portal-identity.php';

// WordPress user integrations
require_once ZEROBSCRM_INCLUDE_PATH . 'class-wordpress-user-integration.php';

Expand Down
28 changes: 9 additions & 19 deletions includes/ZeroBSCRM.DAL3.Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,25 +522,11 @@ function zeroBSCRM_getClientPortalUserID( $cID = -1 ) {

global $zbs;

// first lets check if a user already exists with that email..
$email = $zbs->DAL->contacts->getContactEmail( $cID );
if ( ! empty( $email ) ) {
$userID = email_exists( $email );
if ( $userID != null ) {
// update_post_meta($cID, 'zbs_portal_wpid', $userID);
$zbs->DAL->contacts->addUpdateContactWPID(
array(
'id' => $cID,
'WPID' => $userID,
)
);
}
} else {
// no email in meta, but might be linked?
// $userID = get_post_meta($cID, 'zbs_portal_wpid', true);
$userID = $zbs->DAL->contacts->getContactWPID( $cID );
}
return $userID;
// The user this contact is linked to. A user who merely has the contact's email
// address is not linked by looking: see includes/jpcrm-portal-identity.php.
$linked_user_id = (int) $zbs->DAL->contacts->getContactWPID( $cID ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

return $linked_user_id > 0 ? $linked_user_id : false;
}
return false;
}
Expand Down Expand Up @@ -626,6 +612,10 @@ function zeroBSCRM_createClientPortalUser( $cID = -1, $email = '', $passwordLeng
)
);

// The account was made for this contact, with the contact's email address, and its
// password goes to that address: there is nothing further for the user to confirm.
update_user_meta( $user_id, JPCRM_PORTAL_CONFIRMED_EMAIL_META, jpcrm_portal_normalise_email( $email ) );

// any extra assigned role? (from settings)
$extraRole = zeroBSCRM_getSetting( 'portalusers_extrarole' );

Expand Down
20 changes: 19 additions & 1 deletion includes/ZeroBSCRM.DAL3.Obj.Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,7 @@ public function addUpdateContact( $args = array() ) {
*/
if ( is_array( $do_not_overwrite_populated ) && count( $do_not_overwrite_populated ) > 0 && ! empty( $id ) && $id > 0 ) {

$existing_contact = $this->getContact( $id, array( 'withCustomFields' => false ) );
$existing_contact = $this->getContact( $id, array( 'withCustomFields' => true ) );

if ( is_array( $existing_contact ) ) {

Expand Down Expand Up @@ -3093,6 +3093,24 @@ public function addUpdateContact( $args = array() ) {
)
);

// tags, outside the limitedFields check below, for the same reason.
//
// An update that leaves blanks alone is a limitedFields update, and the
// tags the caller sent with it used to be dropped. They are added to the
// contact's tags here, never swapped for them: this path exists so that
// what a contact already has is kept.
if ( $do_not_update_blanks && isset( $limitedFields ) && is_array( $limitedFields ) && isset( $data['tags'] ) && is_array( $data['tags'] ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

$this->addUpdateContactTags(
array(
'id' => $id,
'tag_input' => $data['tags'],
'mode' => 'append',
)
);

}

// if passing limitedFields instead of data, we ignore the following
// this doesn't work, because data is in args default as arr
// if (isset($data) && is_array($data)){
Expand Down
7 changes: 7 additions & 0 deletions includes/ZeroBSCRM.DAL3.php
Original file line number Diff line number Diff line change
Expand Up @@ -3788,6 +3788,13 @@ public function addUpdateTag( $args = array() ) {
if ( ! isset( $data['name'] ) || empty( $data['name'] ) ) {
return false;
}

// Tag names are plain text, whichever caller they arrive from.
$data['name'] = sanitize_text_field( $data['name'] );
if ( '' === $data['name'] ) {
return false;
}

if ( empty( $data['slug'] ) ) {

$potential_slug = sanitize_key( $data['name'] ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
Expand Down
8 changes: 6 additions & 2 deletions includes/ZeroBSCRM.ExternalSources.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ function jpcrm_get_external_source_info( $source_key ) {
// Returns a simplified 1 line explanation of an external source
function zeroBS_getExternalSourceTitle( $srcKey = '', $srcUID = '' ) {

// The ID is whatever the source gave us (an order number, an email address
// typed into a form), and is shown as text.
$srcUID = esc_html( (string) $srcUID ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

// some old hard typed:

switch ( $srcKey ) {
Expand Down Expand Up @@ -165,12 +169,12 @@ function zeroBS_getExternalSourceTitle( $srcKey = '', $srcUID = '' ) {
$name = $zbs->external_sources[ $srcKey ][0];
}

return '<i class="fa ' . $ico . '"></i> ' . $name . ':<br /><span>' . $srcUID . '</span>';
return '<i class="fa ' . esc_attr( $ico ) . '"></i> ' . esc_html( $name ) . ':<br /><span>' . $srcUID . '</span>'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

} else {

#} Generic for now
return '<i class="fa fa-users"></i> ' . ucwords( str_replace( '_', ' ', $srcKey ) ) . ':<br /><span>' . $srcUID . '</span>';
return '<i class="fa fa-users"></i> ' . esc_html( ucwords( str_replace( '_', ' ', $srcKey ) ) ) . ':<br /><span>' . $srcUID . '</span>'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

}

Expand Down
2 changes: 1 addition & 1 deletion includes/ZeroBSCRM.FormatHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ function zeroBSCRM_html_companyIntroSentence( $company ) {
$return = '';
if ( isset( $company['email'] ) && ! empty( $company['email'] ) ) {
$return .= __( 'Email:', 'zero-bs-crm' );
$return .= '<a href="mailto:' . $company['email'] . '" class="coemail"> ' . $company['email'] . '</a><br/>';
$return .= '<a href="' . esc_url( 'mailto:' . $company['email'] ) . '" class="coemail"> ' . esc_html( $company['email'] ) . '</a><br/>';
}

if ( is_array( $company ) && $company['created'] > 0 && isset( $company['created_date'] ) ) {
Expand Down
34 changes: 34 additions & 0 deletions includes/ZeroBSCRM.IntegrationFuncs.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ function zeroBS_integrations_getCustomer( $externalSource = '', $externalID = ''
return false;
}

/**
* The contact fields a submission from the public may fill in but not replace.
*
* A form or donation that anyone can submit names a contact by its email
* address, without showing the address is theirs. So when the contact already
* exists, what was sent fills in fields that are empty and leaves the rest as
* they are. Tags are added to, and the source is recorded, as before.
*
* @param array $contact_data The contact data built from the submission.
* @param string $source Where it came from, e.g. 'jetpack_form'.
* @return string[] Keys of `$contact_data` to pass as `do_not_overwrite_populated`.
*/
function jpcrm_public_submission_fields_to_protect( $contact_data, $source ) {

$fields = array_values(
array_diff(
array_keys( (array) $contact_data ),
array( 'email', 'externalSources', 'tags', 'tag_mode' )
)
);

/**
* Filters the contact fields a public submission may fill in but not replace.
*
* Returning an empty array restores the previous behaviour, where a submission
* replaced these fields on whichever contact held the email address.
*
* @param string[] $fields Field keys to protect.
* @param array $contact_data The contact data built from the submission.
* @param string $source Where it came from, e.g. 'jetpack_form'.
*/
return (array) apply_filters( 'jpcrm_public_submission_fields_to_protect', $fields, $contact_data, $source );
}

/*
|=======================================
| zeroBS_integrations_addOrUpdateCustomer
Expand Down
50 changes: 42 additions & 8 deletions includes/ZeroBSCRM.Jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@

add_action( 'grunion_after_feedback_post_inserted', 'zero_bs_crm_capture_jetpack_form', 10, 4 );

/**
* A value submitted through a Jetpack form, as the text to store on a contact.
*
* @param mixed $value The submitted value. Choice fields submit an array.
* @param bool $multi_line Whether to keep line breaks.
* @return string
*/
function jpcrm_jetpack_form_text_value( $value, $multi_line = false ) {

if ( is_array( $value ) ) {
$value = implode( ', ', array_filter( $value, 'is_scalar' ) );
}

if ( ! is_scalar( $value ) ) {
return '';
}

return $multi_line ? sanitize_textarea_field( (string) $value ) : sanitize_text_field( (string) $value );
}

/**
* Creates a ZBS contact from a Jetpack form submission.
*
Expand Down Expand Up @@ -69,6 +89,9 @@ function zero_bs_crm_capture_jetpack_form( $post_id, $all_field_data, $is_spam,
'lastcontacted',
'created',
'aliases',
'id',
'wpid',
'owner',
);

/*
Expand All @@ -92,10 +115,11 @@ function zero_bs_crm_capture_jetpack_form( $post_id, $all_field_data, $is_spam,
} else {
$contact_data[ $data_key ] = explode( ',', $field_data->value );
}
$contact_data[ $data_key ] = array_map( 'jpcrm_jetpack_form_text_value', $contact_data[ $data_key ] );
} elseif ( $field_data->get_attribute( 'type' ) === 'date' ) {
$contact_data[ $data_key ] = jpcrm_date_str_wp_format_to_uts( $field_data->value, true );
} else {
$contact_data[ $data_key ] = $field_data->value;
$contact_data[ $data_key ] = jpcrm_jetpack_form_text_value( $field_data->value, $field_data->get_attribute( 'type' ) === 'textarea' );
}
}
}
Expand All @@ -112,23 +136,23 @@ function zero_bs_crm_capture_jetpack_form( $post_id, $all_field_data, $is_spam,
&& ! empty( $field_data->value )
) {
// Use the first email field that's found.
$contact_data['email'] = $field_data->value;
$contact_data['email'] = jpcrm_jetpack_form_text_value( $field_data->value );
}

if ( 'telephone' === $field_data->get_attribute( 'type' )
&& ! isset( $contact_data['hometel'] )
&& ! empty( $field_data->value )
) {
// Use the first phone field that's found.
$contact_data['hometel'] = $field_data->value;
$contact_data['hometel'] = jpcrm_jetpack_form_text_value( $field_data->value );
}

if ( 'name' === $field_data->get_attribute( 'type' )
&& ! isset( $contact_data['fname'] )
&& ! empty( $field_data->value )
) {
// Use the first name field that's found.
$name = explode( ' ', $field_data->value, 2 );
$name = explode( ' ', jpcrm_jetpack_form_text_value( $field_data->value ), 2 );
$contact_data['fname'] = $name[0];
if ( ! empty( $name[1] ) ) {
$contact_data['lname'] = $name[1];
Expand All @@ -142,6 +166,12 @@ function zero_bs_crm_capture_jetpack_form( $post_id, $all_field_data, $is_spam,
return;
}

// The contact is matched on this, and it becomes the ID of its source, so it has
// to be an email address whichever kind of field it was typed into.
if ( ! zeroBSCRM_validateEmail( $contact_data['email'] ) ) {
return;
}

// Add the external source.
$contact_data['externalSources'] = array(
array(
Expand Down Expand Up @@ -190,10 +220,14 @@ function zero_bs_crm_capture_jetpack_form( $post_id, $all_field_data, $is_spam,
//phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$zbs->DAL->contacts->addUpdateContact(
array(
'data' => $contact_data,
'automatorPassthrough' => $success_log,
'fallBackLog' => $exists_log,
'extraMeta' => $extra_meta,
'data' => $contact_data,
'automatorPassthrough' => $success_log,
'fallBackLog' => $exists_log,
'extraMeta' => $extra_meta,
// Anyone can submit the form, against any email address: fill in what an
// existing contact is missing, and leave what it has.
'do_not_overwrite_populated' => jpcrm_public_submission_fields_to_protect( $contact_data, 'jetpack_form' ),
'do_not_update_blanks' => true,
)
);
}
30 changes: 29 additions & 1 deletion includes/ZeroBSCRM.MetaBoxes3.Companies.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ public function save_data( $company_id, $company ) {
}
}

// Only an email address is kept as the company's email. The posted value is still
// slashed here; it is unslashed below, before it is stored.
$posted_email = isset( $dataArr['email'] ) ? stripslashes( (string) $dataArr['email'] ) : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
if ( '' !== $posted_email && ! zeroBSCRM_validateEmail( $posted_email ) ) {

// Say so, and leave the email as it was.
$this->update_invalid_email( $posted_email );
$dataArr['email'] = $company_id > 0 ? (string) zeroBS_companyEmail( $company_id ) : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}

// now we check whether a user with this email already exists (separate to this company id), so we can warn them
// ... that it wont have changed the email
if ( isset( $dataArr['email'] ) && ! empty( $dataArr['email'] ) ) {
Expand Down Expand Up @@ -394,6 +404,24 @@ public function updateMessage( $created = false ) {
$zbs->pageMessages[] = $msg;
}

/**
* Queue a notice saying the email entered for the company was not saved.
*
* @param string $invalid_email The value that was entered.
*/
public function update_invalid_email( $invalid_email ) {
global $zbs;

$zbs->pageMessages[] = zeroBSCRM_UI2_messageHTML( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'info orange mini',
/* translators: %s: the email address that was entered. */
sprintf( __( 'The company email specified (%s) is not valid.', 'zero-bs-crm' ), esc_html( $invalid_email ) ),
'',
'address book outline',
'companyUpdated'
);
}

public function updateEmailDupeMessage( $otherCompanyID = -1 ) {

global $zbs;
Expand Down Expand Up @@ -508,7 +536,7 @@ public function html( $company, $metabox ) {
#} Description
$contactDesc = '<i class="calendar alternate outline icon"></i>' . __( 'Contact since', 'zero-bs-crm' ) . ' ' . zeroBSCRM_date_i18n( zeroBSCRM_getDateFormat(), $contact['createduts'], true, false );
if ( isset( $contact['email'] ) && ! empty( $contact['email'] ) ) {
$contactDesc .= '<br /><a href="' . $contactUrl . '" target="_blank">' . $contact['email'] . '</a>';
$contactDesc .= '<br /><a href="' . $contactUrl . '" target="_blank">' . esc_html( $contact['email'] ) . '</a>'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}

?>
Expand Down
2 changes: 1 addition & 1 deletion includes/ZeroBSCRM.Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ function jpcrm_can_wp_user_view_object( $wp_user, $obj_id, $obj_type_id ) {
}

// verify current user is assigned user
$contact_id = zeroBS_getCustomerIDWithEmail( $wp_user->user_email );
$contact_id = jpcrm_portal_contact_id_for_user( $wp_user->ID );
if ( $assigned_contact_id != $contact_id ) {
return false;
}
Expand Down
Loading
Loading