arie_cristianD

frontend submit with wcs quota

Nov 9th, 2026
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. /**
  2.  * Simple Subscription article limits, keyed by product slug.
  3.  */
  4. function jnews_frontend_subscription_quota_payment( $subscription ) {
  5.     $limits = array(
  6.         '4-article-subs' => 4,
  7.         '8-article'      => 8,
  8.         '16-product'     => 16,
  9.     );
  10.     $limit  = 0;
  11.     foreach ( $subscription->get_items() as $item ) {
  12.         $product = $item->get_product();
  13.         if ( ! $product || ! $product->is_type( 'subscription' ) ) {
  14.             continue;
  15.         }
  16.         $key = $product->get_slug();
  17.         if ( ! isset( $limits[ $key ] ) ) {
  18.             $key = sanitize_title( $product->get_name() );
  19.         }
  20.         if ( isset( $limits[ $key ] ) ) {
  21.             $limit += $limits[ $key ];
  22.         }
  23.     }
  24.     $order = null;
  25.     if ( $limit ) {
  26.         foreach ( $subscription->get_related_orders( 'all', array( 'parent', 'renewal' ) ) as $related_order ) {
  27.             if ( $related_order->is_paid() && $related_order->get_date_paid()
  28.                 && ( ! $order || $related_order->get_id() > $order->get_id() ) ) {
  29.                 $order = $related_order;
  30.             }
  31.         }
  32.     }
  33.     return array(
  34.         'limit' => $limit,
  35.         'order' => $order,
  36.     );
  37. }
  38.  
  39. function jnews_frontend_subscription_is_valid( $subscription ) {
  40.     $end = $subscription->get_time( 'end' );
  41.     return $subscription->has_status( array( 'active', 'pending-cancel' ) ) && ( ! $end || $end > time() );
  42. }
  43.  
  44. /**
  45.  * Replace listing_left once per paid order. Status changes alone never refill it.
  46.  */
  47. function jnews_frontend_subscription_sync_quota( $subscription ) {
  48.     if ( ! get_theme_mod( 'jnews_frontend_submit_enable_woocommerce', false ) || ! jnews_frontend_subscription_is_valid( $subscription ) ) {
  49.         return;
  50.     }
  51.     $payment = jnews_frontend_subscription_quota_payment( $subscription );
  52.     $user_id = $subscription->get_user_id();
  53.     if ( ! $user_id || ! $payment['order'] ) {
  54.         return;
  55.     }
  56.     $order_id  = $payment['order']->get_id();
  57.     $processed = (int) $subscription->get_meta( '_jnews_quota_processed_order_id', true );
  58.     $latest    = (int) get_user_meta( $user_id, '_jnews_quota_latest_order_id', true );
  59.     if ( $processed >= $order_id || $latest >= $order_id ) {
  60.         return;
  61.     }
  62.     // Preserve articles charged by the previous implementation during migration.
  63.     $used      = array_filter( (array) $subscription->get_meta( '_jnews_article_usage_' . $order_id, true ) );
  64.     $remaining = max( 0, $payment['limit'] - count( $used ) );
  65.     update_user_meta( $user_id, 'listing_left', $remaining );
  66.     update_user_meta( $user_id, '_jnews_quota_latest_order_id', $order_id );
  67.     $subscription->update_meta_data( '_jnews_quota_processed_order_id', $order_id );
  68.     $subscription->save_meta_data();
  69. }
  70.  
  71. /**
  72.  * Block inactive subscribers; active subscribers use JNews's quota check.
  73.  */
  74. function jnews_frontend_submit_simple_subscription_access( $allowed ) {
  75.     if ( ! get_theme_mod( 'jnews_frontend_submit_enable_woocommerce', false ) || ! is_user_logged_in() || ! function_exists( 'wcs_get_users_subscriptions' ) ) {
  76.         return $allowed;
  77.     }
  78.     $managed = false;
  79.     foreach ( wcs_get_users_subscriptions( get_current_user_id() ) as $subscription ) {
  80.         $payment = jnews_frontend_subscription_quota_payment( $subscription );
  81.         if ( ! $payment['limit'] ) {
  82.             continue;
  83.         }
  84.         $managed = true;
  85.         if ( jnews_frontend_subscription_is_valid( $subscription ) ) {
  86.             return $allowed;
  87.         }
  88.     }
  89.     return $managed ? false : $allowed;
  90. }
  91.  
  92. /**
  93.  * Remove the article balance when a configured subscription is cancelled or expired.
  94.  */
  95. function jnews_frontend_subscription_clear_quota( $subscription ) {
  96.     $user_id = $subscription->get_user_id();
  97.     $payment = jnews_frontend_subscription_quota_payment( $subscription );
  98.     if ( $user_id && $payment['limit'] ) {
  99.         delete_user_meta( $user_id, 'listing_left' );
  100.     }
  101. }
  102.  
  103. add_filter( 'jnews_frontend_submit_user_subscription', 'jnews_frontend_submit_simple_subscription_access' );
  104. add_action( 'woocommerce_subscription_payment_complete', 'jnews_frontend_subscription_sync_quota' );
  105. add_action( 'woocommerce_subscription_status_updated', 'jnews_frontend_subscription_sync_quota' );
  106. add_action( 'woocommerce_subscription_status_cancelled', 'jnews_frontend_subscription_clear_quota' );
  107. add_action( 'woocommerce_subscription_status_expired', 'jnews_frontend_subscription_clear_quota' );
  108.  
Advertisement
Add Comment
Please, Sign In to add comment