Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Simple Subscription article limits, keyed by product slug.
- */
- function jnews_frontend_subscription_quota_payment( $subscription ) {
- $limits = array(
- '4-article-subs' => 4,
- '8-article' => 8,
- '16-product' => 16,
- );
- $limit = 0;
- foreach ( $subscription->get_items() as $item ) {
- $product = $item->get_product();
- if ( ! $product || ! $product->is_type( 'subscription' ) ) {
- continue;
- }
- $key = $product->get_slug();
- if ( ! isset( $limits[ $key ] ) ) {
- $key = sanitize_title( $product->get_name() );
- }
- if ( isset( $limits[ $key ] ) ) {
- $limit += $limits[ $key ];
- }
- }
- $order = null;
- if ( $limit ) {
- foreach ( $subscription->get_related_orders( 'all', array( 'parent', 'renewal' ) ) as $related_order ) {
- if ( $related_order->is_paid() && $related_order->get_date_paid()
- && ( ! $order || $related_order->get_id() > $order->get_id() ) ) {
- $order = $related_order;
- }
- }
- }
- return array(
- 'limit' => $limit,
- 'order' => $order,
- );
- }
- function jnews_frontend_subscription_is_valid( $subscription ) {
- $end = $subscription->get_time( 'end' );
- return $subscription->has_status( array( 'active', 'pending-cancel' ) ) && ( ! $end || $end > time() );
- }
- /**
- * Replace listing_left once per paid order. Status changes alone never refill it.
- */
- function jnews_frontend_subscription_sync_quota( $subscription ) {
- if ( ! get_theme_mod( 'jnews_frontend_submit_enable_woocommerce', false ) || ! jnews_frontend_subscription_is_valid( $subscription ) ) {
- return;
- }
- $payment = jnews_frontend_subscription_quota_payment( $subscription );
- $user_id = $subscription->get_user_id();
- if ( ! $user_id || ! $payment['order'] ) {
- return;
- }
- $order_id = $payment['order']->get_id();
- $processed = (int) $subscription->get_meta( '_jnews_quota_processed_order_id', true );
- $latest = (int) get_user_meta( $user_id, '_jnews_quota_latest_order_id', true );
- if ( $processed >= $order_id || $latest >= $order_id ) {
- return;
- }
- // Preserve articles charged by the previous implementation during migration.
- $used = array_filter( (array) $subscription->get_meta( '_jnews_article_usage_' . $order_id, true ) );
- $remaining = max( 0, $payment['limit'] - count( $used ) );
- update_user_meta( $user_id, 'listing_left', $remaining );
- update_user_meta( $user_id, '_jnews_quota_latest_order_id', $order_id );
- $subscription->update_meta_data( '_jnews_quota_processed_order_id', $order_id );
- $subscription->save_meta_data();
- }
- /**
- * Block inactive subscribers; active subscribers use JNews's quota check.
- */
- function jnews_frontend_submit_simple_subscription_access( $allowed ) {
- if ( ! get_theme_mod( 'jnews_frontend_submit_enable_woocommerce', false ) || ! is_user_logged_in() || ! function_exists( 'wcs_get_users_subscriptions' ) ) {
- return $allowed;
- }
- $managed = false;
- foreach ( wcs_get_users_subscriptions( get_current_user_id() ) as $subscription ) {
- $payment = jnews_frontend_subscription_quota_payment( $subscription );
- if ( ! $payment['limit'] ) {
- continue;
- }
- $managed = true;
- if ( jnews_frontend_subscription_is_valid( $subscription ) ) {
- return $allowed;
- }
- }
- return $managed ? false : $allowed;
- }
- /**
- * Remove the article balance when a configured subscription is cancelled or expired.
- */
- function jnews_frontend_subscription_clear_quota( $subscription ) {
- $user_id = $subscription->get_user_id();
- $payment = jnews_frontend_subscription_quota_payment( $subscription );
- if ( $user_id && $payment['limit'] ) {
- delete_user_meta( $user_id, 'listing_left' );
- }
- }
- add_filter( 'jnews_frontend_submit_user_subscription', 'jnews_frontend_submit_simple_subscription_access' );
- add_action( 'woocommerce_subscription_payment_complete', 'jnews_frontend_subscription_sync_quota' );
- add_action( 'woocommerce_subscription_status_updated', 'jnews_frontend_subscription_sync_quota' );
- add_action( 'woocommerce_subscription_status_cancelled', 'jnews_frontend_subscription_clear_quota' );
- add_action( 'woocommerce_subscription_status_expired', 'jnews_frontend_subscription_clear_quota' );
Advertisement
Add Comment
Please, Sign In to add comment