Skip to content

Commit

Permalink
Merge pull request #572 from alicolville/mixed-forms
Browse files Browse the repository at this point in the history
Mixed Weight Forms
  • Loading branch information
alicolville committed Nov 6, 2023
2 parents 57bf8e7 + cec36d1 commit e8d8285
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
9 changes: 7 additions & 2 deletions includes/converters.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,16 @@ function ws_ls_convert_kg_to_stone_pounds( $kg ) {
*
* @return array|null
*/
function ws_ls_weight_display( $kg, $user_id = NULL, $key = false, $force_admin = false, $comparison_value = false ) {
function ws_ls_weight_display( $kg, $user_id = NULL, $key = false, $force_admin = false, $comparison_value = false, $format = null ) {

$weight = [];
$weight[ 'user-id' ] = ( NULL === $user_id ) ? get_current_user_id() : $user_id;
$weight[ 'format' ] = ws_ls_setting( 'weight-unit', $weight[ 'user-id' ], $force_admin );

if ( true === empty( $format ) ) {
$format = ws_ls_setting( 'weight-unit', $weight[ 'user-id' ], $force_admin );
}

$weight[ 'format' ] = $format;
$weight[ 'kg' ] = $kg;

switch ( $weight[ 'format' ] ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ function ws_ls_user_preferences_get_formats( $db_fields ) {

$lookup = apply_filters( 'wlt-filter-user-settings-db-formats', $lookup );

foreach ( $db_fields as $key ) {
foreach ( $db_fields as $key => $value ) {
if( false === empty($lookup[$key])) {
$formats[] = $lookup[$key];
}
Expand Down
56 changes: 40 additions & 16 deletions includes/form-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ function ws_ls_form_post_handler(){
$result = false;

// Process posted form and save!
if ( 'target' === $submission_type ) {
$result = ws_ls_form_post_handler_target( $user_id );
} else { // weight / custom-fields
$result = ws_ls_form_post_handler_weight( $user_id, $submission_type );
if ( true === in_array( $submission_type, [ 'mixed', 'target' ] ) ) {

$prefix = ( 'mixed' === $submission_type ) ? 'ws-ls-target-' : 'ws-ls-weight-';

$result = ws_ls_form_post_handler_target( $user_id, $prefix );
}

if ( true === in_array( $submission_type, [ 'custom-fields', 'mixed', 'weight' ] ) ) {
$result = ws_ls_form_post_handler_weight( $user_id );
}

if ( true === empty( $result ) ) {
Expand All @@ -69,13 +74,16 @@ function ws_ls_form_post_handler(){

/**
* Update the user's target
*
* @param $user_id
* @param string $prefix
*
* @return bool
*/
function ws_ls_form_post_handler_target( $user_id ) {
function ws_ls_form_post_handler_target( $user_id, $prefix = 'ws-ls-weight-' ) {

$kg = ws_ls_form_post_handler_extract_weight();
// Start by searching for standard weight field names, if nothing (e.g. potentially in "mixed" mode) then look for target weight field names
$kg = ws_ls_form_post_handler_extract_weight( 'post', $prefix );

// If nothing specified, then delete existing target
if ( true === empty( $kg ) ) {
Expand All @@ -94,11 +102,10 @@ function ws_ls_form_post_handler_target( $user_id ) {
*
* @param $user_id
*
* @param string $type
*
* @return bool
*/
function ws_ls_form_post_handler_weight( $user_id, $type = 'weight' ) {
function ws_ls_form_post_handler_weight( $user_id ) {

if ( true === empty( $user_id ) ) {
return false;
Expand Down Expand Up @@ -207,9 +214,19 @@ function ws_ls_form_post_handler_weight( $user_id, $type = 'weight' ) {
* @return string|null
*/
function ws_ls_form_post_handler_determine_type() {

$type = ws_ls_post_value( 'type' );

return ( true === in_array( $type, [ 'custom-fields', 'target', 'weight' ] ) ) ?
/*
* "Mixed" isn't widely supported at the moment and was intended for forms that contain both latest weight and target.
*
* In "Mixed" mode, target weight fields are prefixed with "ws-ls-target-" instead of "ws-ls-weight-"
*
* Currently, "mixed" is only supported by custom plugins.
*
*/

return ( true === in_array( $type, [ 'custom-fields', 'mixed' , 'target', 'weight' ] ) ) ?
$type :
NULL;
}
Expand All @@ -218,27 +235,34 @@ function ws_ls_form_post_handler_determine_type() {
* Scan the form post for relevant weight fields and convert them into Kg
*
* @param string $get_or_post
* @param string $prefix "ws-ls-weight-" or "ws-ls-target-"
*
* @return float|null
*/
function ws_ls_form_post_handler_extract_weight( $get_or_post = 'post' ) {
function ws_ls_form_post_handler_extract_weight( $get_or_post = 'post', $prefix = 'ws-ls-weight-' ) {

$key = sprintf( '%s%s', $prefix, 'kg' );

// Are we lucky? Metric by default?
$kg = 'post' === $get_or_post ?
ws_ls_post_value( 'ws-ls-weight-kg', NULL, true ) :
ws_ls_querystring_value( 'ws-ls-weight-kg', false, NULL );
ws_ls_post_value( $key, NULL, true ) :
ws_ls_querystring_value( $key, false, NULL );

if ( NULL !== $kg ) {
return $kg;
}

$key = sprintf( '%s%s', $prefix, 'stones' );

$stones = 'post' === $get_or_post ?
ws_ls_post_value( 'ws-ls-weight-stones', NULL, true ) :
ws_ls_querystring_value( 'ws-ls-weight-stones', false, NULL );
ws_ls_post_value( $key, NULL, true ) :
ws_ls_querystring_value( $key, false, NULL );

$key = sprintf( '%s%s', $prefix, 'pounds' );

$pounds = 'post' === $get_or_post ?
ws_ls_post_value( 'ws-ls-weight-pounds', NULL, true ) :
ws_ls_querystring_value( 'ws-ls-weight-pounds', false, NULL );
ws_ls_post_value( $key, NULL, true ) :
ws_ls_querystring_value( $key, false, NULL );

// Stones and Pounds
if ( NULL !== $stones ) {
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: aliakro
Tags: weight,tracker,chart,bmi,bmr,macronutrient,measure,awards,custom fields,history,measurements,data
Requires at least: 5.7
Tested up to: 6.3
Stable tag: 10.7
Stable tag: 10.7.1
Requires PHP: 7.2
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -154,6 +154,10 @@ Measurements are created using Custom Fields. You can therefore specify the unit

= 10.7 =

* Improvement: Added support under the hood for mixed forms (e.g. that contain weights and targets) - note: this functionality is currently exposed to the end user.

= 10.7 =

* Maintenance: Removed support for the shortcode [wt-beta], you must use [wt] instead.
* Maintenance: Removed support for the shortcode [wlt], you must use [wt] instead.
* Maintenance: Removed support for the shortcode [wt-legacy], you must use [wt] instead.
Expand Down
2 changes: 1 addition & 1 deletion weight-loss-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Plugin Name: Weight Tracker
* Description: Allow your users to track their weight, body measurements, photos and other pieces of custom data. Display in charts, tables, shortcodes and widgets. Manage their data, issue awards, email notifications, etc! Provide advanced data on Body Mass Index (BMI), Basal Metabolic Rate (BMR), Calorie intake, Harris Benedict Formula, Macronutrients Calculator and more.
* Version: 10.7
* Version: 10.7.1
* Requires at least: 5.7
* Tested up to: 6.3
* Requires PHP: 7.2
Expand Down

0 comments on commit e8d8285

Please sign in to comment.