Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Frontend Submit quotas per paid Simple Subscription period.
- * Keys are product slugs; unused articles do not carry into a new paid period.
- */
- function jnews_frontend_article_quota( $user_id ) {
- $result = array( 'managed' => false, 'remaining' => 0, 'claim' => null );
- if ( ! $user_id || ! function_exists( 'wcs_get_users_subscriptions' ) ) {
- return $result;
- }
- $limits = array( '4-article-subs' => 4, '8-article' => 8, '16-product' => 16 );
- foreach ( wcs_get_users_subscriptions( $user_id ) as $subscription ) {
- $limit = 0;
- foreach ( $subscription->get_items() as $item ) {
- $product = $item->get_product();
- if ( $product && $product->is_type( 'subscription' ) ) {
- $key = $product->get_slug();
- if ( ! isset( $limits[ $key ] ) ) {
- $key = sanitize_title( $product->get_name() );
- }
- if ( isset( $limits[ $key ] ) ) {
- $limit += $limits[ $key ];
- }
- }
- }
- if ( ! $limit ) {
- continue;
- }
- $result['managed'] = true;
- $end = $subscription->get_time( 'end' );
- if ( ! $subscription->has_status( array( 'active', 'pending-cancel' ) ) || ( $end && $end <= time() ) ) {
- continue;
- }
- // Only a paid parent/renewal order starts a quota period. An unpaid
- // renewal or reactivation of the same subscription cannot reset usage.
- $paid_order = null;
- $paid_time = 0;
- foreach ( $subscription->get_related_orders( 'all', array( 'parent', 'renewal' ) ) as $order ) {
- $date = $order->get_date_paid();
- if ( $order->is_paid() && $date && $date->getTimestamp() >= $paid_time ) {
- $paid_order = $order;
- $paid_time = $date->getTimestamp();
- }
- }
- if ( ! $paid_order ) {
- continue;
- }
- $key = '_jnews_article_usage_' . $paid_order->get_id();
- $used = (array) $subscription->get_meta( $key, true );
- $used = array_filter( $used );
- $remaining = max( 0, $limit - count( $used ) );
- $result['remaining'] += $remaining;
- if ( $remaining && ! $result['claim'] ) {
- $result['claim'] = array( 'subscription' => $subscription, 'key' => $key, 'used' => $used );
- }
- }
- return $result;
- }
- function jnews_frontend_submit_simple_subscription_access( $allowed ) {
- if ( ! get_theme_mod( 'jnews_frontend_submit_enable_woocommerce', false ) || ! is_user_logged_in() ) {
- return $allowed;
- }
- $quota = jnews_frontend_article_quota( get_current_user_id() );
- return $quota['managed'] ? $quota['remaining'] > 0 : $allowed;
- }
- add_filter( 'jnews_frontend_submit_user_subscription', 'jnews_frontend_submit_simple_subscription_access' );
- /**
- * Check before the plugin's wp_loaded handler without modifying that handler.
- * Serialize submissions by user so simultaneous requests cannot overspend.
- */
- function jnews_frontend_article_quota_before_submit() {
- if ( ! get_theme_mod( 'jnews_frontend_submit_enable_woocommerce', false ) || ! is_user_logged_in()
- || ! isset( $_REQUEST['jnews-action'], $_REQUEST['jnews-editor-nonce'] )
- || 'create-post' !== $_REQUEST['jnews-action']
- || ! is_string( $_REQUEST['jnews-editor-nonce'] )
- || ! wp_verify_nonce( $_REQUEST['jnews-editor-nonce'], 'jnews-editor' ) ) {
- return;
- }
- $user_id = get_current_user_id();
- $quota = jnews_frontend_article_quota( $user_id );
- if ( ! $quota['managed'] ) {
- return;
- }
- $lock = 'jnews_article_submit_lock_' . $user_id;
- $held = (int) get_option( $lock, 0 );
- if ( $held && $held < time() - 300 ) {
- delete_option( $lock );
- }
- if ( ! add_option( $lock, time(), '', false ) ) {
- wp_die( esc_html__( 'Another article is being submitted. Please try again shortly.', 'jnews' ), '', array( 'response' => 409 ) );
- }
- register_shutdown_function( function () use ( $lock ) { delete_option( $lock ); } );
- // Re-read after obtaining the lock in case another request just completed.
- $quota = jnews_frontend_article_quota( $user_id );
- if ( ! $quota['claim'] ) {
- wp_die( esc_html__( 'Your subscription has no article quota available. Please renew or activate your subscription.', 'jnews' ), '', array( 'response' => 403 ) );
- }
- $GLOBALS['jnews_frontend_article_claim'] = $quota['claim'];
- }
- add_action( 'wp_loaded', 'jnews_frontend_article_quota_before_submit', 19 );
- /**
- * Charge only a successfully inserted new article; editing/deletion never refunds it.
- */
- function jnews_frontend_article_quota_after_insert( $post_id, $post, $update ) {
- if ( $update || empty( $GLOBALS['jnews_frontend_article_claim'] ) || 'post' !== $post->post_type
- || (int) $post->post_author !== get_current_user_id() || in_array( $post->post_status, array( 'auto-draft', 'trash' ), true ) ) {
- return;
- }
- $claim = $GLOBALS['jnews_frontend_article_claim'];
- unset( $GLOBALS['jnews_frontend_article_claim'] );
- $claim['used'][] = $post_id;
- $claim['subscription']->update_meta_data( $claim['key'], array_unique( $claim['used'] ) );
- $claim['subscription']->save_meta_data();
- update_post_meta( $post_id, '_jnews_article_quota_subscription', $claim['subscription']->get_id() );
- // Subscription quota replaces the plugin's one-time Post Package deduction.
- update_post_meta( $post_id, 'finish_process', true );
- }
- add_action( 'wp_after_insert_post', 'jnews_frontend_article_quota_after_insert', 10, 3 );
- function jnews_frontend_article_quota_remaining_text( $text ) {
- $quota = jnews_frontend_article_quota( get_current_user_id() );
- return $quota['managed'] ? (string) $quota['remaining'] : $text;
- }
- add_filter( 'jnews_frontend_submit_remaining_quota_text', 'jnews_frontend_article_quota_remaining_text' );
Advertisement
Add Comment
Please, Sign In to add comment