Skip to content

Commit

Permalink
Merge pull request #1483 from sapayth/enhance/support_for_additional_…
Browse files Browse the repository at this point in the history
…cpts

enhance: support for additional CPT's
  • Loading branch information
sapayth authored Oct 22, 2024
2 parents f92a621 + dff2c51 commit 2decc63
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 15 deletions.
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'] ) );
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,
'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,7 +193,6 @@ public function is_submission_open( $form, $form_settings ) {
return [ $user_can_post, $info ];
}


if ( $this->is_charging_enabled() ) {
$pay_per_post = $this->is_enabled_pay_per_post();
// $pay_per_post_cost = (float) $this->get_pay_per_post_cost();
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 );
$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'] ) );
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 @@ -175,10 +175,13 @@ public function add_pack( $pack_id, $profile_id, $recurring, $status = null ) {
$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

0 comments on commit 2decc63

Please sign in to comment.