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 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: 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') && key === 'additional_cpt_options' ) {
this.currentSubscription.meta_value[key] = {};
}

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
30 changes: 18 additions & 12 deletions includes/Admin/Admin_Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,26 @@ 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(
'Set the maximum number of %s users can create within their subscription period. Enter -1 for unlimited',
'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(
// translators: %s: post type label
__( 'Number of %s', 'wp-user-frontend' ),
esc_html( $post_type_object->label )
),
'tooltip' => sprintf(
// translators: %s: post type label
__(
'Set the maximum number of %s users can create within their subscription period. Enter -1 for unlimited',
'wp-user-frontend'
),
esc_html( $key )
),
'default' => '-1',
'default' => '-1',
];
}
}
Expand Down Expand Up @@ -809,9 +818,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
2 changes: 2 additions & 0 deletions includes/Ajax/Frontend_Form_Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Frontend_Form_Ajax {
private $post_expiration_message = 'wpuf-post_expiration_message';

/**
* An array of form fields retrieved from the form configuration.
*
* @var array
*/
private $form_fields;
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
5 changes: 4 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,13 @@
$result = '';
$subscription = wpuf()->subscription->get_subscription( $pack_id );

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

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