Skip to content

Commit

Permalink
Merge pull request #595 from alicolville/show-weight-loss-too
Browse files Browse the repository at this point in the history
10.8.1
  • Loading branch information
alicolville committed Apr 17, 2024
2 parents 8a10f37 + 17fb194 commit bf08ffc
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 104 deletions.
8 changes: 8 additions & 0 deletions docs/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ Display the oldest value for the given custom field. Replace [slug] with the des

[![](/assets/images/component-latest-weight.png)](/assets/images/component-latest-weight.png)

#### latest-weight-difference-as-percentage

Same box as "latest-weight" but forces the difference to be displayed as a percentage.

#### latest-weight-difference-as-weight

Same box as "latest-weight" but forces the difference to be displayed in weight.

#### latest-versus-target

[![](/assets/images/component-latest-versus-target.png)](/assets/images/component-latest-versus-target.png)
Expand Down
4 changes: 2 additions & 2 deletions docs/shortcodes-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Display the difference between the current user's most recent weight entry and t
[wt-difference-from-target invert="true" display="percentage"]

Also supports the following arguments:
* "display" - Specifies whether to display a "weight" (default) or "percentage"
* "display" - Specifies whether to display a "weight" (default), "percentage" or "both".
* "invert" to invert negative numbers into positive and vice versa e.g.
* "include-percentage-sign" - if displaying a percentage, specifies whether or not to include the percentage sign. Accepts "true" (default) or "false".

Expand All @@ -63,7 +63,7 @@ Display the difference between the current user's most recent weight entry and t
[wt-difference-between-latest-previous invert="true" display="percentage”]
Also supports the following arguments:
* "display" - Specifies whether to display a "weight" (default) or "percentage"
* "display" - Specifies whether to display a "weight" (default), "percentage" or "both".
* "invert" to invert negative numbers into positive and vice versa e.g.
* "include-percentage-sign" - if displaying a percentage, specifies whether or not to include the percentage sign. Accepts "true" (default) or "false".

Expand Down
16 changes: 10 additions & 6 deletions includes/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ function ws_ls_uikit_summary_boxes( $arguments, $boxes = [] ) {
$html .= ws_ls_component_number_of_days_tracking( [ 'user-id' => $arguments[ 'user-id' ] ] );
break;
case 'latest-weight':
case 'latest-weight-difference-as-percentage':
$html .= ws_ls_component_latest_weight( [ 'user-id' => $arguments[ 'user-id' ] ] );
break;
case 'latest-weight-difference-as-weight':
$html .= ws_ls_component_latest_weight( [ 'user-id' => $arguments[ 'user-id' ], 'difference-display' => 'weight' ] );
break;
case 'latest-award':
$html .= ws_ls_component_latest_award( [ 'user-id' => $arguments[ 'user-id' ] ] );
break;
Expand Down Expand Up @@ -226,7 +230,7 @@ function ws_ls_component_custom_field_render( $args) {
*/
function ws_ls_component_latest_weight( $args = [] ) {

$args = wp_parse_args( $args, [ 'user-id' => get_current_user_id() ] );
$args = wp_parse_args( $args, [ 'difference-display' => 'percentage', 'user-id' => get_current_user_id() ] );
$latest_entry = ws_ls_entry_get_latest( $args );

$text_date = '';
Expand All @@ -240,9 +244,9 @@ function ws_ls_component_latest_weight( $args = [] ) {
<a href="%s">%s</a>
</span>', ws_ls_wt_link_edit_entry( $latest_entry[ 'id' ] ), $latest_entry[ 'display-date' ] );

$difference = ws_ls_shortcode_difference_in_weight_previous_latest( [ 'display' => 'percentage',
'include-percentage-sign' => false,
'invert' => true,
$difference = ws_ls_shortcode_difference_in_weight_previous_latest( [ 'display' => $args[ 'difference-display' ],
'include-percentage-sign' => true,
'invert' => ( 'percentage' === $args[ 'difference-display' ] ),
'user-id' => $args[ 'user-id']
] );

Expand All @@ -257,7 +261,7 @@ function ws_ls_component_latest_weight( $args = [] ) {
$class = 'ykuk-label-warning';
}

$text_data .= sprintf( ' <span class="ykuk-label %s" ykuk-tooltip="%s">%s%%</span>',
$text_data .= sprintf( ' <span class="ykuk-label %s ykuk-width-1-1" ykuk-tooltip="%s">%s</span>',
$class,
__( 'The difference between your latest weight and previous.', WE_LS_SLUG ),
$difference
Expand Down Expand Up @@ -290,7 +294,7 @@ function ws_ls_component_weight_difference_since_previous( $args = [] ) {

$args = wp_parse_args( $args, [ 'user-id' => get_current_user_id() ] );

$text_data = ws_ls_shortcode_difference_in_weight_previous_latest( [ 'display' => 'weight',
$text_data = ws_ls_shortcode_difference_in_weight_previous_latest( [ 'display' => 'percentage',
'include-percentage-sign' => false,
'invert' => false,
'user-id' => $args[ 'user-id'],
Expand Down
200 changes: 108 additions & 92 deletions includes/shortcode-various.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,84 +79,6 @@ function ws_ls_shortcode_recent_weight( $user_id = NULL ) {
add_shortcode( 'wlt-weight-most-recent', 'ws_ls_shortcode_recent_weight' );
add_shortcode( 'wt-latest-weight', 'ws_ls_shortcode_recent_weight' );

/**
* Shortcide [wt-difference-between-latest-previous] display difference between previous
*
* @param array $user_defined_arguments
*
* @return string|null
*/
function ws_ls_shortcode_difference_in_weight_previous_latest( $user_defined_arguments = [] ){

$arguments = shortcode_atts( [ 'user-id' => get_current_user_id(),
'invert' => false,
'display' => 'weight', // weight or percentage
'include-percentage-sign' => true,
'kiosk-mode' => false
]
, $user_defined_arguments );

$cache_key = 'shortcode-diff-prev-latest-' . md5( json_encode( $arguments ) );

if ( $cache = ws_ls_cache_user_get( $arguments[ 'user-id' ], $cache_key ) ) {
return $cache;
}

$latest_entry = ws_ls_entry_get_latest( $arguments );

if ( true === empty( $latest_entry[ 'kg' ] ) ) {
return '';
}

$previous_entry = ws_ls_entry_get_previous( $arguments );

if( true === empty( $previous_entry ) ) {
return '';
}

if ( $previous_entry[ 'id' ] === $latest_entry[ 'id' ] ) {
return '';
}

if ( 'percentage' == $arguments[ 'display' ] ) {

$output = ws_ls_calculate_percentage_difference( $previous_entry[ 'kg' ], $latest_entry[ 'kg' ] );

if ( true === empty( $output[ 'percentage' ] ) ) {
return '';
}

$output = ( true === $output[ 'increase' ] ) ? $output[ 'percentage' ] : -$output[ 'percentage' ];

$output = ws_ls_round_number( $output, 1 );

if ( true === $arguments[ 'include-percentage-sign' ] ) {
$output .= '%';
}

} else {

$difference = $latest_entry[ 'kg' ] - $previous_entry[ 'kg' ];
$difference = ( false === ws_ls_to_bool( $arguments[ 'invert' ] ) ) ? $difference : -$difference ;
$sign = ( $difference > 0 ) ? '+' : '';
$weight_to_display = ws_ls_weight_display( $difference, $arguments[ 'user-id' ], false, false, true );
$output = sprintf ('%s%s', $sign, $weight_to_display[ 'display' ] );

if ( true === $arguments[ 'kiosk-mode' ] ) {

// Note, this currently only supports lose weight!
$label = ( $difference <= 0 ) ? 'ykuk-label ykuk-label-success' : 'ykuk-label ykuk-label-warning';

$output = sprintf( '<span class="%s">%s</span>', $label, $output );
}
}

ws_ls_cache_user_set( $arguments[ 'user-id' ], $cache_key, $output );

return $output;
}
add_shortcode( 'wt-difference-between-latest-previous', 'ws_ls_shortcode_difference_in_weight_previous_latest' );

/**
* Display shortcode for difference since start
* @param null $user_id
Expand Down Expand Up @@ -228,36 +150,130 @@ function ws_ls_shortcode_difference_in_weight_target( $user_defined_arguments =
return '';
}

if ( 'percentage' == $arguments[ 'display' ] ) {
$html = '';

if ( true === in_array( $arguments[ 'display' ], [ 'both', 'percentage' ] ) ) {

$output = ws_ls_calculate_percentage_difference($latest_entry['kg'], $target_weight);

$output = ws_ls_calculate_percentage_difference( $latest_entry[ 'kg' ], $target_weight );
if (true === empty($output['percentage'])) {
return '';
}

if ( true === empty( $output[ 'percentage' ] ) ) {
return '';
}
$difference = (true === $output['increase']) ? $output['percentage'] : -$output['percentage'];

$output = ( true === $output[ 'increase' ] ) ? $output[ 'percentage' ] : -$output[ 'percentage' ];
$html .= ws_ls_round_number( $difference, 1 );

$output = ws_ls_round_number( $output, 1 );
if (true === $arguments['include-percentage-sign']) {
$html .= '%';
}
}

if ( true === $arguments[ 'include-percentage-sign' ] ) {
$output .= '%';
}
if ( 'both' == $arguments[ 'display' ] ) {
$html .= ' / ';
}

if ( true === in_array( $arguments[ 'display' ], [ 'both', 'weight' ] ) ) {

} else {
$difference = $latest_entry[ 'kg' ] - $target_weight;

$difference = ( false === ws_ls_to_bool( $arguments[ 'invert' ] ) ) ? $difference : -$difference ;

$sign = ( $difference > 0 ) ? '+' : '';

$difference = ws_ls_weight_display( $difference, $arguments[ 'user-id' ], false, false, true );
$output = sprintf ('%s%s', $sign, $difference[ 'display' ] );
$html .= sprintf ('%s%s', $sign, $difference[ 'display' ] );
}

ws_ls_cache_user_set( $arguments[ 'user-id' ], 'shortcode-target', $output );
ws_ls_cache_user_set( $arguments[ 'user-id' ], 'shortcode-target', $html );

return $output;
return $html;
}
add_shortcode( 'wlt-weight-diff-from-target', 'ws_ls_shortcode_difference_in_weight_target' );
add_shortcode( 'wt-difference-from-target', 'ws_ls_shortcode_difference_in_weight_target' );

/**
* Shortcide [wt-difference-between-latest-previous] display difference between previous
*
* @param array $user_defined_arguments
*
* @return string|null
*/
function ws_ls_shortcode_difference_in_weight_previous_latest( $user_defined_arguments = [] ){

$arguments = shortcode_atts( [ 'user-id' => get_current_user_id(),
'invert' => false,
'display' => 'weight', // both, weight or percentage
'include-percentage-sign' => true,
'kiosk-mode' => false
]
, $user_defined_arguments );

$cache_key = 'shortcode-diff-prev-latest-' . md5( json_encode( $arguments ) );

if ( $cache = ws_ls_cache_user_get( $arguments[ 'user-id' ], $cache_key ) ) {
return $cache;
}

$latest_entry = ws_ls_entry_get_latest( $arguments );

if ( true === empty( $latest_entry[ 'kg' ] ) ) {
return '';
}

$previous_entry = ws_ls_entry_get_previous( $arguments );

if( true === empty( $previous_entry ) ) {
return '';
}

if ( $previous_entry[ 'id' ] === $latest_entry[ 'id' ] ) {
return '';
}

$html = '';

if ( true === in_array( $arguments[ 'display' ], [ 'both', 'percentage' ] ) ) {

$output = ws_ls_calculate_percentage_difference( $previous_entry[ 'kg' ], $latest_entry[ 'kg' ] );

if ( true === empty( $output[ 'percentage' ] ) ) {
return '';
}

$difference = ( true === $output[ 'increase' ] ) ? $output[ 'percentage' ] : -$output[ 'percentage' ];

$html .= ws_ls_round_number( $difference, 1 );

if ( true === $arguments[ 'include-percentage-sign' ] ) {
$html .= '%';
}

}

if ( 'both' == $arguments[ 'display' ] ) {
$html .= ' / ';
}

if ( true === in_array( $arguments[ 'display' ], [ 'both', 'weight' ] ) ) {

$difference = $latest_entry[ 'kg' ] - $previous_entry[ 'kg' ];
$difference = ( false === ws_ls_to_bool( $arguments[ 'invert' ] ) ) ? $difference : -$difference ;
$sign = ( $difference > 0 ) ? '+' : '';
$weight_to_display = ws_ls_weight_display( $difference, $arguments[ 'user-id' ], false, false, true );
$html .= sprintf ('%s%s', $sign, $weight_to_display[ 'display' ] );

if ( true === $arguments[ 'kiosk-mode' ] ) {

// Note, this currently only supports lose weight!
$label = ( $difference <= 0 ) ? 'ykuk-label ykuk-label-success' : 'ykuk-label ykuk-label-warning';

$html .= sprintf( '<span class="%s">%s</span>', $label, $html );
}
}

ws_ls_cache_user_set( $arguments[ 'user-id' ], $cache_key, $html );

return $html;
}
add_shortcode( 'wt-difference-between-latest-previous', 'ws_ls_shortcode_difference_in_weight_previous_latest' );
7 changes: 5 additions & 2 deletions 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: 6.0
Tested up to: 6.4.3
Stable tag: 10.8
Stable tag: 10.8.1
Requires PHP: 7.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -152,8 +152,11 @@ Measurements are created using Custom Fields. You can therefore specify the unit

== Changelog ==

= 10.*.* =
= 10.8.1 =

* New feature: Added the option "both" to the "display" argument of [wt-difference-from-target]. This option will display both weight and percentage.
* New feature: Added the option "both" to the "display" argument of [wt-difference-between-latest-previous]. This option will display both weight and percentage.
* New feature: Added the slightly new components "latest-weight-difference-as-percentage" and "latest-weight-difference-as-weight" to change how the difference figure of "latest-weight" is rendered". Read more: https://docs.yeken.uk/components.html
* Maintenance: Updated Chart.js library to 4.4.1.
* Maintenance: Removed deprecated "ltrim()" warning when no redirect-url specified on [wt] and [wt-form]

Expand Down
4 changes: 2 additions & 2 deletions 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.8
* Version: 10.8.1
* Requires at least: 6.0
* Tested up to: 6.4.3
* Requires PHP: 7.4
Expand All @@ -18,7 +18,7 @@
*/

define( 'WS_LS_ABSPATH', plugin_dir_path( __FILE__ ) );
define( 'WE_LS_CURRENT_VERSION', '10.8' );
define( 'WE_LS_CURRENT_VERSION', '10.8.1' );
define( 'WE_LS_TITLE', 'Weight Tracker' );
define( 'WE_LS_SLUG', 'weight-loss-tracker' );
define( 'WE_LS_LICENSE_TYPES_URL', 'https://docs.yeken.uk/features.html' );
Expand Down

0 comments on commit bf08ffc

Please sign in to comment.