Skip to content

Commit

Permalink
Merge pull request #589 from alicolville/component-latest-previous
Browse files Browse the repository at this point in the history
10.8
  • Loading branch information
alicolville authored Feb 19, 2024
2 parents 77f23d4 + 3309fe9 commit 90a693f
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 17 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ GEM
zeitwerk (2.6.12)

PLATFORMS
arm64-darwin-23
universal-darwin-19
x86_64-darwin-23
x86_64-linux
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions docs/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ Below are the currently available summary boxes that you can use:

[![](/assets/images/component-calories-auto.png)](/assets/images/component-calories-auto.png)

#### custom-field-latest-[slug]

Display the latest value for the given custom field. Replace [slug] with the desired custom field's slug e.g.

custom-field-latest-leg

[![](/assets/images/component-custom-field-previous.png)](/assets/images/component-custom-field-previous.png)

#### custom-field-previous-[slug]

Display the previous value for the given custom field. Replace [slug] with the desired custom field's slug e.g.

custom-field-previous-leg

[![](/assets/images/component-custom-field-previous.png)](/assets/images/component-custom-field-previous.png)

#### custom-field-oldest-[slug]

Display the oldest value for the given custom field. Replace [slug] with the desired custom field's slug e.g.

custom-field-oldest-leg

[![](/assets/images/component-custom-field-oldest.png)](/assets/images/component-custom-field-oldest.png)


#### divider

(this creates a new section and adds a divider)
Expand Down
75 changes: 67 additions & 8 deletions includes/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@
*/
function ws_ls_uikit_summary_boxes( $arguments, $boxes = [] ) {

// TODO: refactor this. Do we really need this check?
$allowed_boxes = [ 'number-of-entries', 'number-of-weight-entries', 'latest-weight', 'start-weight', 'number-of-days-tracking',
'target-weight', 'previous-weight', 'latest-versus-target', 'bmi', 'bmr', 'latest-award', 'number-of-awards',
'name-and-email', 'start-bmr', 'start-bmi', 'age-dob', 'activity-level', 'height', 'aim', 'gender', 'group',
'latest-versus-start', 'divider', 'weight-difference-since-previous', 'calories-maintain', 'calories-lose', 'calories-gain', 'calories-auto', 'user-id' ];

// Default box selection
if ( true === empty( $boxes ) ) {
$boxes = [ 'number-of-entries', 'number-of-days-tracking', 'latest-weight', 'start-weight' ];
}

$boxes = array_intersect( $boxes, $allowed_boxes );

if ( true === empty( $boxes ) ) {
return '<!-- No valid summary boxes -->';
}
Expand All @@ -40,6 +32,14 @@ function ws_ls_uikit_summary_boxes( $arguments, $boxes = [] ) {

foreach ( $boxes as $box ) {

$custom_field = ws_ls_component_is_custom_field( $box );

if ( false !== $custom_field ) {

$html .= ws_ls_component_custom_field_render( [ 'custom-field' => $custom_field, 'user-id' => $arguments[ 'user-id' ] ] );
continue;
}

switch ( $box ) {
case 'weight-difference-since-previous':
$html .= ws_ls_component_weight_difference_since_previous( [ 'user-id' => $arguments[ 'user-id' ] ] );
Expand Down Expand Up @@ -159,6 +159,65 @@ function ws_ls_uikit_summary_boxes( $arguments, $boxes = [] ) {
return $html;
}

/**
* Is the component specified a custom field?
* @param $box
* @return array|false
*/
function ws_ls_component_is_custom_field( $box ) {

if ( false === strpos( $box, 'custom-field-' ) ) {
return false;
}

$custom_field = [ 'mode' => 'latest',
'slug' => str_replace( [ 'custom-field-latest-', 'custom-field-previous-', 'custom-field-oldest-' ], [ '', '', '' ], $box )
];

if ( null === ws_ls_meta_fields_slug_to_id( $custom_field[ 'slug' ] ) ) {
return false;
}

if ( false !== strpos( $box, 'custom-field-oldest-' ) ) {
$custom_field[ 'mode' ] = 'oldest';
} else if ( false !== strpos( $box, 'custom-field-previous' ) ) {
$custom_field[ 'mode' ] = 'previous';
}

return $custom_field;
}

/**
* Render a custom field component
* @param $args
* @return string
*/
function ws_ls_component_custom_field_render( $args) {

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

$args = wp_parse_args( $args, [ 'custom-field' => $args, 'user-id' => get_current_user_id() ] );
$custom_field = ws_ls_meta_fields_shortcode_value_latest( [ 'slug' => $args[ 'custom-field' ]['slug'], 'user-id' => $args[ 'user-id' ], 'which' => $args[ 'custom-field' ]['mode'], 'return-as-array' => true ] );

$title = sprintf( '%s %s', ucwords( $args[ 'custom-field' ]['mode'] ), ws_ls_meta_fields_get_column( $custom_field[ 'id' ], 'field_name' ) );
$value = sprintf( '%s%s', $custom_field[ 'display' ], ws_ls_meta_fields_get_column( $custom_field[ 'id' ], 'suffix' ) );

return sprintf( '<div>
<div class="ykuk-card ykuk-card-small ykuk-card-body ykuk-box-shadow-small">
<span class="ykuk-info-box-header">%1$s</span><br />
<span class="ykuk-text-bold">
%2$s
</span>
</div>
</div>',
$title,
$value
);

}

/**
* Component to display the user's latest weight
* @param array $args
Expand Down
15 changes: 10 additions & 5 deletions pro-features/plus/meta-fields/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,31 @@ function ws_ls_meta_fields_shortcode_value_latest( $user_defined_arguments ) {
return ws_ls_display_pro_upgrade_notice_for_shortcode();
}

$arguments = shortcode_atts( [ 'slug' => '', 'user-id' => get_current_user_id(), 'which' => 'latest' ] , $user_defined_arguments );
$arguments = shortcode_atts( [ 'slug' => '', 'user-id' => get_current_user_id(), 'which' => 'latest', 'return-as-array' => false ] , $user_defined_arguments );

if ( true === empty( $arguments[ 'slug' ] ) ) {
return __( 'You must specify a slug.', WE_LS_SLUG );
}

$meta_field_id = ws_ls_meta_fields_slug_to_id( $arguments[ 'slug' ] );
$meta_field = [ 'id' => ws_ls_meta_fields_slug_to_id( $arguments[ 'slug' ] ), 'slug' => $arguments[ 'slug' ] ];

if ( true === empty( $meta_field_id ) ) {
$meta_field[ 'id' ] = ws_ls_meta_fields_slug_to_id( $arguments[ 'slug' ] );

if ( true === empty( $meta_field[ 'id' ] ) ) {
return __( 'The slug you specified does not exist.', WE_LS_SLUG );
}

$arguments[ 'key' ] = $meta_field_id;
$arguments[ 'key' ] = $meta_field[ 'id' ];
$value = ws_meta_fields_value_get( $arguments );

if ( false === empty( $value[ 'error' ] ) ) {
return $value[ 'error' ];
}

return ws_ls_fields_display_field_value( $value[ 'value' ], $arguments[ 'key' ] );
$meta_field[ 'value' ] = $value;
$meta_field[ 'display' ] = ws_ls_fields_display_field_value( $value[ 'value' ], $arguments[ 'key' ] );

return ( false === $arguments[ 'return-as-array' ] ) ? $meta_field[ 'display' ] : $meta_field;
}
add_shortcode( 'wt-custom-fields-latest', 'ws_ls_meta_fields_shortcode_value_latest' );

Expand Down
8 changes: 6 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.7.4
Stable tag: 10.8
Requires PHP: 7.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -148,10 +148,14 @@ Measurements are created using Custom Fields. You can therefore specify the unit

== Upgrade Notice ==

10.4: New award for user's meeting their target
10.8: New components for displaying custom field data on [wt] shortcode.

== Changelog ==

= 10.8 =

* New feature: Added new components to display custom fields (latest, previous and oldest) within summary boxes on [wt] shortcode. Read more: https://docs.yeken.uk/components.html

= 10.7.3 =

* Improvement: When [wt-table] is within edit mode, clicking an edit link will jump to the correct place om the page. Read more: https://github.com/alicolville/Weight-Tracker/issues/569
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.7.4
* Version: 10.8
* 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.7.4' );
define( 'WE_LS_CURRENT_VERSION', '10.8' );
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 90a693f

Please sign in to comment.