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

enhance: support for additional CPT's #1483

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 0 deletions assets/js/stores/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export const useSubscriptionStore = defineStore( 'subscription', {
return;
}

if ( typeof this.currentSubscription.meta_value[key] === 'string' ) {
this.currentSubscription.meta_value[key] = {};
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

⚠️ Potential issue

Conversion of meta_value[key] from String to Object Affects Multiple Components

The verification indicates that meta_value is utilized in various parts of the codebase. Changing meta_value[key] from a string to an object in the modifyCurrentSubscription method may lead to inconsistencies and unexpected behaviors in other components that expect it to be a string.

Potential Issues:

  1. Data Inconsistency: Other parts of the application may assume meta_value[key] is a string, leading to type errors or unexpected behavior.
  2. Data Loss: Converting a string to an empty object discards the original value, which could result in loss of important data.
  3. Lack of Error Handling: Without logging or error reporting, debugging issues related to this change may become challenging.

Recommendations:

  1. Preserve Original Data:
    if (typeof this.currentSubscription.meta_value[key] === 'string') {
      const originalValue = this.currentSubscription.meta_value[key];
      console.warn(`Converting meta_value[${key}] from string to object. Original value: ${originalValue}`);
      this.currentSubscription.meta_value[key] = { originalValue };
    }
  2. Audit Dependent Code: Review all instances where meta_value is used to ensure they handle the new object structure appropriately.
  3. Implement Comprehensive Logging: Add logging mechanisms to track when and why conversions occur, aiding in future debugging efforts.
  4. Investigate Root Cause: Determine why meta_value[key] is a string in the first place and address any underlying data handling inconsistencies.
🔗 Analysis chain

Ensure data integrity when converting meta_value[key] to an object

The added type check and conversion for meta_value[key] prevents errors when assigning properties to a string. However, there are some potential issues to consider:

  1. This change might alter existing data structures, affecting parts of the application that expect meta_value[key] to be a string.
  2. Converting to an empty object discards any existing string value, which could lead to data loss.

Consider the following improvements:

  1. Add error logging when this conversion occurs to aid in debugging:
if (typeof this.currentSubscription.meta_value[key] === 'string') {
  console.warn(`Converting meta_value[${key}] from string to object. Original value: ${this.currentSubscription.meta_value[key]}`);
  this.currentSubscription.meta_value[key] = {};
}
  1. Instead of converting to an empty object, consider preserving the original string value:
if (typeof this.currentSubscription.meta_value[key] === 'string') {
  const originalValue = this.currentSubscription.meta_value[key];
  this.currentSubscription.meta_value[key] = { originalValue };
}
  1. Investigate why meta_value[key] might be a string and address the root cause if possible.

To ensure this change doesn't introduce regressions, please run the following script to check for other occurrences of meta_value usage:

This will help identify other parts of the code that might be affected by this change.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other usages of meta_value in the codebase
rg --type js "meta_value" -C 3

Length of output: 11996

this.currentSubscription.meta_value[key][serializeKey] = value;
},
getMetaValue( key ) {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/subscriptions.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions class/subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ public static function get_subscription_meta( $subscription_id, $pack_post = nul
$meta['trial_duration'] = get_post_meta( $subscription_id, '_trial_duration', true );
$meta['trial_duration_type'] = get_post_meta( $subscription_id, '_trial_duration_type', true );
$meta['post_type_name'] = get_post_meta( $subscription_id, '_post_type_name', true );
$meta['additional_cpt_options'] = get_post_meta( $subscription_id, 'additional_cpt_options', true );
$meta['_enable_post_expiration'] = get_post_meta( $subscription_id, '_enable_post_expiration', true );
$meta['_post_expiration_time'] = get_post_meta( $subscription_id, '_post_expiration_time', true );
$meta['_expired_post_status'] = get_post_meta( $subscription_id, '_expired_post_status', true );
Expand Down Expand Up @@ -418,6 +419,7 @@ public function save_form_meta( $subscription_id, $post ) {
update_post_meta( $subscription_id, '_trial_duration', $trial_duration );
update_post_meta( $subscription_id, '_trial_duration_type', $trial_duration_type );
update_post_meta( $subscription_id, '_post_type_name', array_map( 'sanitize_text_field', $post_data['post_type_name'] ) );
update_post_meta( $subscription_id, 'additional_cpt_options', array_map( 'sanitize_text_field', $post_data['additional_cpt_options'] ) );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure 'additional_cpt_options' exists in $post_data before accessing it

To prevent potential undefined index notices, please check if 'additional_cpt_options' exists in $post_data before accessing it.

Apply this diff to add the necessary check:

+if ( isset( $post_data['additional_cpt_options'] ) ) {
    update_post_meta( $subscription_id, 'additional_cpt_options', array_map( 'sanitize_text_field', $post_data['additional_cpt_options'] ) );
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
update_post_meta( $subscription_id, 'additional_cpt_options', array_map( 'sanitize_text_field', $post_data['additional_cpt_options'] ) );
if ( isset( $post_data['additional_cpt_options'] ) ) {
update_post_meta( $subscription_id, 'additional_cpt_options', array_map( 'sanitize_text_field', $post_data['additional_cpt_options'] ) );
}

update_post_meta( $subscription_id, '_enable_post_expiration', $enable_post_expir );
update_post_meta( $subscription_id, '_post_expiration_time', $expiration_time );
update_post_meta( $subscription_id, '_expired_post_status', $expire_post_status );
Expand Down
20 changes: 9 additions & 11 deletions includes/Admin/Admin_Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ public function third_party_cpt_options( $additional_options ) {

if ( $post_type_object ) {
$additional_options['additional'][ $key ] = [
'id' => $key,
'name' => $key,
'db_key' => $key,
'db_type' => 'meta',
'type' => 'input-number',
'label' => sprintf( 'Number of %s', esc_html( $post_type_object->label ) ),
'tooltip' => sprintf(
'id' => $key,
'name' => $key,
'db_key' => 'additional_cpt_options',
'db_type' => 'meta_serialized',
'serialize_key' => $key,
Comment on lines +69 to +73
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improper Use of Meta Keys and Serialization

The array keys 'db_key' and 'serialize_key' in lines 71 and 73 might cause confusion. You're setting 'db_key' to 'additional_cpt_options' and 'serialize_key' to $key, which could lead to inconsistent data storage and retrieval, especially when handling serialized meta values.

Consider refactoring to ensure that each custom post type option has a unique and coherent meta key. For example:

-    'db_key'        => 'additional_cpt_options',
-    'db_type'       => 'meta_serialized',
-    'serialize_key' => $key,
+    'db_key'        => 'additional_cpt_options_' . $key,
+    'db_type'       => 'meta',
+    'serialize_key' => null,

This change assigns a unique meta key for each post type option and avoids complex serialization.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'id' => $key,
'name' => $key,
'db_key' => 'additional_cpt_options',
'db_type' => 'meta_serialized',
'serialize_key' => $key,
'id' => $key,
'name' => $key,
'db_key' => 'additional_cpt_options_' . $key,
'db_type' => 'meta',
'serialize_key' => null,

'type' => 'input-number',
'label' => sprintf( 'Number of %s', esc_html( $post_type_object->label ) ),
'tooltip' => sprintf(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Internationalization (i18n) Issue in String Formatting

The usage of sprintf with plain strings on lines 75 and 76 isn't prepared for translation. Strings should be wrapped with __() or _e() before being passed to sprintf for proper localization.

Update the code to wrap the strings with translation functions:

-    'label'         => sprintf( 'Number of %s', esc_html( $post_type_object->label ) ),
-    'tooltip'       => sprintf(
-        'Set the maximum number of %s users can create within their subscription period. Enter -1 for unlimited',
+    'label'         => sprintf( __( 'Number of %s', 'wp-user-frontend' ), esc_html( $post_type_object->label ) ),
+    'tooltip'       => sprintf(
+        __( 'Set the maximum number of %s users can create within their subscription period. Enter -1 for unlimited', 'wp-user-frontend' ),

Committable suggestion was skipped due to low confidence.

'Set the maximum number of %s users can create within their subscription period. Enter -1 for unlimited',
esc_html( $key )
),
'default' => '-1',
'default' => '-1',
];
}
}
Expand Down Expand Up @@ -809,9 +810,6 @@ public function profile_subscription_details( $profileuser ) {
$_post_expiration_time = explode( ' ', isset( $user_sub['_post_expiration_time'] ) ? $user_sub['_post_expiration_time'] : '' );
$time_value = isset( $_post_expiration_time[0] ) && ! empty( $_post_expiration_time[0] ) ? $_post_expiration_time[0] : '1';
$time_type = isset( $_post_expiration_time[1] ) && ! empty( $_post_expiration_time[1] ) ? $_post_expiration_time[1] : 'day';

error_log( print_r( $_post_expiration_time, true ) );
error_log( print_r( $time_type, true ) );
?>
<tr>
<th><label><?php esc_html_e( 'Post Expiration Enabled', 'wp-user-frontend' ); ?></label></th>
Expand Down
1 change: 0 additions & 1 deletion includes/Admin/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,38 +193,37 @@
return [ $user_can_post, $info ];
}


if ( $this->is_charging_enabled() ) {
$pay_per_post = $this->is_enabled_pay_per_post();

Check warning on line 197 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Equals sign not aligned correctly; expected 1 space but found 6 spaces
// $pay_per_post_cost = (float) $this->get_pay_per_post_cost();

Check warning on line 198 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

This comment is 67% valid code; is this commented out code?
$force_pack = $this->is_enabled_force_pack();
$fallback_enabled = $this->is_enabled_fallback_cost();
// $fallback_cost = $this->get_subs_fallback_cost();

Check warning on line 201 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

This comment is 70% valid code; is this commented out code?

// guest post payment checking
if ( ! is_user_logged_in() && isset( $form_settings['guest_post'] ) && $form_settings['guest_post'] === 'true' ) {

//if ( $form->is_charging_enabled() ) {

Check warning on line 206 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

This comment is 62% valid code; is this commented out code?

if ( $force_pack ) {
$user_can_post = 'no';
$pack_page = get_permalink( wpuf_get_option( 'subscription_page',

Check failure on line 210 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Opening parenthesis of a multi-line function call must be the last content on the line

Check failure on line 210 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Opening parenthesis of a multi-line function call must be the last content on the line
'wpuf_payment' ) );

Check warning on line 211 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found precision alignment of 1 spaces.

Check failure on line 211 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Multi-line function call not indented correctly; expected 20 spaces but found 69

Check failure on line 211 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Multi-line function call not indented correctly; expected 20 spaces but found 69

Check failure on line 211 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Closing parenthesis of a multi-line function call must be on a line by itself

Check failure on line 211 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Closing parenthesis of a multi-line function call must be on a line by itself
/* translators: %s: Pack page link */
$info = sprintf( __( 'You need to <a href="%s">purchase a subscription package</a> to post in this form', 'wp-user-frontend' ), $pack_page );

Check warning on line 213 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Equals sign not aligned correctly; expected 1 space but found 10 spaces
} elseif ( $pay_per_post && ! $force_pack ) {
$user_can_post = 'yes';
// $info = sprintf( __( 'There is a <strong>%s</strong> charge to add a new post.', 'wpuf' ), wpuf_format_price( $pay_per_post_cost ));

Check warning on line 216 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

This comment is 50% valid code; is this commented out code?
// echo '<div class="wpuf-info">' . apply_filters( 'wpuf_ppp_notice', $info, $id, $form_settings ) . '</div>';
} else {
$user_can_post = 'no';
$info = sprintf( __( 'Payment type not selected for this form. Please contact admin.', 'wp-user-frontend' ) );
}

// } else {

Check warning on line 223 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

This comment is 50% valid code; is this commented out code?
// $user_can_post = 'yes';
// }
} else {

Check failure on line 226 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

If control structure block found as the only statement within an "else" block. Use elseif instead.
// regular payment checking
if ( $force_pack && is_user_logged_in() ) {
$current_pack = $current_user->subscription()->current_pack();
Expand All @@ -233,7 +232,7 @@
// user has valid post count
if ( $has_post_count ) {
$user_can_post = 'yes';
} else {

Check failure on line 235 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

If control structure block found as the only statement within an "else" block. Use elseif instead.
if ( $fallback_enabled && ! $has_post_count ) {
$user_can_post = 'yes';
} else {
Expand All @@ -247,7 +246,7 @@
}
} elseif ( $pay_per_post && is_user_logged_in() && ! $current_user->subscription()->has_post_count( $form_settings['post_type'] ) ) {
$user_can_post = 'yes';
// $info = sprintf( __( 'There is a <strong>%s</strong> charge to add a new post.', 'wpuf' ), wpuf_format_price( $pay_per_post_cost ));

Check warning on line 249 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

This comment is 50% valid code; is this commented out code?
// echo '<div class="wpuf-info">' . apply_filters( 'wpuf_ppp_notice', $info, $id, $form_settings ) . '</div>';
} elseif ( ! $pay_per_post && ! $current_user->subscription()->has_post_count( $form_settings['post_type'] ) ) {
$user_can_post = 'no';
Expand All @@ -262,7 +261,7 @@
}
}
}
} else {

Check failure on line 264 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

If control structure block found as the only statement within an "else" block. Use elseif instead.
if ( isset( $form_settings['guest_post'] ) && $form_settings['guest_post'] === 'true' && !
is_user_logged_in() ) {
$user_can_post = 'yes';
Expand Down Expand Up @@ -368,7 +367,7 @@
$field['recaptcha_theme'] = isset( $field['recaptcha_theme'] ) ? $field['recaptcha_theme'] : 'light';
}

// $form_fields[] = apply_filters( 'wpuf-get-form-field', $field );

Check warning on line 370 in includes/Admin/Forms/Form.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

This comment is 63% valid code; is this commented out code?

$form_fields[] = apply_filters( 'wpuf-get-form-fields', $field );
}
Expand Down
2 changes: 2 additions & 0 deletions includes/Admin/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ public static function get_subscription_meta( $subscription_id, $pack_post = nul
$meta['_trial_duration_type'] = $meta['trial_duration_type'];
$meta['post_type_name'] = get_post_meta( $subscription_id, '_post_type_name', true );
$meta['_post_type_name'] = $meta['post_type_name'];
$meta['additional_cpt_options'] = get_post_meta( $subscription_id, 'additional_cpt_options', true );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use consistent meta key naming with underscore prefix

In the existing code, meta keys are stored with an underscore prefix (e.g., _billing_amount, _expiration_number). For consistency and to prevent unintended exposure via the WordPress API, consider prefixing 'additional_cpt_options' with an underscore when retrieving the meta value.

Apply this diff to adjust the meta key:

-$meta['additional_cpt_options']     = get_post_meta( $subscription_id, 'additional_cpt_options', true );
+$meta['_additional_cpt_options']    = get_post_meta( $subscription_id, '_additional_cpt_options', true );
+$meta['additional_cpt_options']     = $meta['_additional_cpt_options'];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$meta['additional_cpt_options'] = get_post_meta( $subscription_id, 'additional_cpt_options', true );
$meta['_additional_cpt_options'] = get_post_meta( $subscription_id, '_additional_cpt_options', true );
$meta['additional_cpt_options'] = $meta['_additional_cpt_options'];

$meta['_enable_post_expiration'] = get_post_meta( $subscription_id, '_enable_post_expiration', true );
$meta['_post_expiration_time'] = get_post_meta( $subscription_id, '_post_expiration_time', true );
$meta['_post_expiration_number'] = get_post_meta( $subscription_id, '_post_expiration_number', true );
Expand Down Expand Up @@ -431,6 +432,7 @@ public function save_form_meta( $subscription_id, $post ) {
update_post_meta( $subscription_id, '_trial_duration', $trial_duration );
update_post_meta( $subscription_id, '_trial_duration_type', $trial_duration_type );
update_post_meta( $subscription_id, '_post_type_name', array_map( 'sanitize_text_field', $post_data['post_type_name'] ) );
update_post_meta( $subscription_id, 'additional_cpt_options', array_map( 'sanitize_text_field', $post_data['additional_cpt_options'] ) );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add check for 'additional_cpt_options' existence before updating meta

To prevent undefined index notices, ensure that 'additional_cpt_options' exists in $post_data before using it. This check prevents potential PHP warnings if the key is not set. Additionally, consider prefixing the meta key with an underscore for consistency.

Apply this diff to add the necessary check and adjust the meta key:

+if ( ! empty( $post_data['additional_cpt_options'] ) && is_array( $post_data['additional_cpt_options'] ) ) {
+    $sanitized_options = array_map( 'sanitize_text_field', $post_data['additional_cpt_options'] );
+    update_post_meta( $subscription_id, '_additional_cpt_options', $sanitized_options );
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
update_post_meta( $subscription_id, 'additional_cpt_options', array_map( 'sanitize_text_field', $post_data['additional_cpt_options'] ) );
if ( ! empty( $post_data['additional_cpt_options'] ) && is_array( $post_data['additional_cpt_options'] ) ) {
$sanitized_options = array_map( 'sanitize_text_field', $post_data['additional_cpt_options'] );
update_post_meta( $subscription_id, '_additional_cpt_options', $sanitized_options );
}

update_post_meta( $subscription_id, '_enable_post_expiration', $enable_post_expir );
update_post_meta( $subscription_id, '_post_expiration_time', $expiration_time );
update_post_meta( $subscription_id, '_expired_post_status', $expire_post_status );
Expand Down
4 changes: 4 additions & 0 deletions includes/Ajax/Frontend_Form_Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Frontend_Form_Ajax {
private $expired_post_status = 'wpuf-expired_post_status';

private $post_expiration_message = 'wpuf-post_expiration_message';
/**
* @var array
*/
private $form_fields;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Proper Documentation for New Property

The new private property $form_fields lacks a descriptive docblock explaining its purpose. Consider adding a meaningful description to enhance code readability and maintainability.

Apply this diff to add documentation:

 /**
- * @var array
+ * An array of form fields retrieved from the form configuration.
+ *
+ * @var array
  */
 private $form_fields;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* @var array
*/
private $form_fields;
/**
* An array of form fields retrieved from the form configuration.
*
* @var array
*/
private $form_fields;


/**
* New/Edit post submit handler
Expand Down
4 changes: 4 additions & 0 deletions includes/Api/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ public function create_or_update_item( $request ) {
$post_type_name = ! empty( $subscription['meta_value']['_post_type_name'] ) ? array_map(
'sanitize_text_field', $subscription['meta_value']['_post_type_name']
) : '';
$additional_cpt_options = ! empty( $subscription['meta_value']['additional_cpt_options'] ) ? array_map(
'sanitize_text_field', $subscription['meta_value']['additional_cpt_options']
) : '';
$enable_post_expir = ! empty( $subscription['meta_value']['_enable_post_expiration'] ) ? sanitize_text_field(
$subscription['meta_value']['_enable_post_expiration']
) : 'no';
Expand Down Expand Up @@ -493,6 +496,7 @@ public function create_or_update_item( $request ) {
update_post_meta( $id, '_trial_duration', $trial_duration );
update_post_meta( $id, '_trial_duration_type', $trial_duration_type );
update_post_meta( $id, '_post_type_name', $post_type_name );
update_post_meta( $id, 'additional_cpt_options', $additional_cpt_options );
update_post_meta( $id, '_enable_post_expiration', $enable_post_expir );
update_post_meta( $id, '_post_expiration_number', $post_expiration_number );
update_post_meta( $id, '_post_expiration_period', $post_expiration_period );
Expand Down
2 changes: 1 addition & 1 deletion includes/Traits/TaxableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ function wpuf_tax_rate_country_state( $country, $state ) {
continue;
}
if ( $rate['state'] === $state && $rate['country'] === $country ) {
$tax_amount = $rate['rate'];
$tax_amount = ! empty( $rate['rate'] ) ? $rate['rate'] : 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct the check for 'rate' to accurately handle zero values

Using ! empty( $rate['rate'] ) may inadvertently treat a valid zero tax rate (0 or '0') as empty, causing $tax_amount to default to 0 even when a specific zero rate is intended. To ensure that zero rates are correctly assigned, consider using isset( $rate['rate'] ) instead.

Apply this diff to fix the condition:

-                    $tax_amount = ! empty( $rate['rate'] ) ? $rate['rate'] : 0;
+                    $tax_amount = isset( $rate['rate'] ) ? $rate['rate'] : 0;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$tax_amount = ! empty( $rate['rate'] ) ? $rate['rate'] : 0;
$tax_amount = isset( $rate['rate'] ) ? $rate['rate'] : 0;

}

if ( intval( $tax_amount ) === 0 && $rate['country'] === $country && 'country_wide' === $rate['state'] ) {
Expand Down
4 changes: 3 additions & 1 deletion includes/User_Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
if ( ! isset( $this->pack['pack_id'] ) ) {
$pack_page = get_permalink( wpuf_get_option( 'subscription_page', 'wpuf_payment' ) );

return new WP_Error( 'no-pack', sprintf( __( 'You must <a href="%s">purchase a subscription package</a> before posting', 'wp-user-frontend' ), $pack_page ) );

Check failure on line 59 in includes/User_Subscription.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

A function call to __() with texts containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.
}

// seems like the user has a pack, now check expiration
Expand Down Expand Up @@ -175,10 +175,12 @@
$result = '';
$subscription = wpuf()->subscription->get_subscription( $pack_id );

$additional_cpt_options = ! empty( $subscription->meta_value['additional_cpt_options'] ) ? $subscription->meta_value['additional_cpt_options'] : [];

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use Null Coalescing Operator for Default Value Assignment

For improved readability and cleaner code, consider using the null coalescing operator ?? instead of the ! empty() check with a ternary operator.

Apply this diff:

-$additional_cpt_options = ! empty( $subscription->meta_value['additional_cpt_options'] ) ? $subscription->meta_value['additional_cpt_options'] : [];
+$additional_cpt_options = $subscription->meta_value['additional_cpt_options'] ?? [];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$additional_cpt_options = ! empty( $subscription->meta_value['additional_cpt_options'] ) ? $subscription->meta_value['additional_cpt_options'] : [];
$additional_cpt_options = $subscription->meta_value['additional_cpt_options'] ?? [];

if ( $this->user->id && $subscription ) {
$user_meta = [
'pack_id' => $pack_id,
'posts' => $subscription->meta_value['post_type_name'],
'posts' => array_merge( $subscription->meta_value['post_type_name'], $additional_cpt_options ),
sapayth marked this conversation as resolved.
Show resolved Hide resolved
'total_feature_item' => $subscription->meta_value['_total_feature_item'],
'remove_feature_item' => $subscription->meta_value['_remove_feature_item'],
'status' => $status,
Expand Down
Loading