*/ private static $synced_in_request = array(); /** * Mark a post as synced for this HTTP request. * * @param int $post_id */ private function mark_synced_in_request($post_id) { self::$synced_in_request[intval($post_id)] = true; } /** * Whether this post was already synced earlier in the same request. * * @param int $post_id * @return bool */ private function was_synced_in_request($post_id) { return !empty(self::$synced_in_request[intval($post_id)]); } /** * Queue Products Portal sync on shutdown so Update/Publish can finish * without blocking on (or fatalling from) remote API work. * * @param int $post_id */ private function queue_product_sync($post_id) { $post_id = intval($post_id); if ($post_id <= 0 || $this->was_synced_in_request($post_id)) { return; } $this->mark_synced_in_request($post_id); add_action('shutdown', function () use ($post_id) { if (!class_exists('Qixit_Product')) { return; } try { Qixit_Product::get_instance()->sync_product_to_api($post_id); } catch (\Throwable $e) { if (function_exists('qixit_debug_log')) { qixit_debug_log('Deferred product sync failed post=' . $post_id . ' err=' . $e->getMessage()); } if (function_exists('set_transient')) { set_transient( 'qixit_last_sync_error_' . get_current_user_id(), $e->getMessage(), MINUTE_IN_SECONDS ); } } }, 20); } /** * Open comments without re-entering save_post handlers (nested wp_update_post). * * @param int $post_id */ private function open_comments_without_reentry($post_id) { $post_id = intval($post_id); if ($post_id <= 0) { return; } if (get_post_field('comment_status', $post_id) === 'open') { return; } if (!empty($GLOBALS['qixit_skip_save_hooks'])) { return; } $GLOBALS['qixit_skip_save_hooks'] = true; wp_update_post(array( 'ID' => $post_id, 'comment_status' => 'open', )); $GLOBALS['qixit_skip_save_hooks'] = false; } /** * Re-sync an existing premium / paid-comments product to Products Portal. * Keeps accessHours, delivery URL, price, and forwarder settings current. * * @param int $post_id * @return bool True when a sync was queued/attempted */ private function resync_existing_product($post_id) { $post_id = intval($post_id); if ($post_id <= 0 || $this->was_synced_in_request($post_id)) { return false; } if (!empty($GLOBALS['qixit_skip_save_hooks'])) { return false; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return false; } if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) { return false; } $product_manager = Qixit_Product::get_instance(); $product = $product_manager->get_product_by_post_id($post_id); if (!$product) { return false; } // String enums — never intval() these (intval('REG') === intval('PREMIUM_...') === 0). $is_premium = !empty($product->qixit_post_type) && $product->qixit_post_type !== QIXIT_REG; $has_paid_comments = !empty($product->enable_paid_comments); if (!$is_premium && !$has_paid_comments) { return false; } // Refresh local permalink before sync so delivery/pitch URLs stay accurate. $product_manager->update_local_product( intval($product->product_id), array('premium_post_url' => get_permalink($post_id)) ); $this->queue_product_sync($post_id); return true; } /** * After any save of a published post, re-sync existing Qixit products. * Metabox save already syncs; this covers edits that never posted metabox fields. * * @param int $post_id * @param WP_Post $post * @param bool $update */ public function resync_existing_product_on_save($post_id, $post = null, $update = true) { if (!empty($GLOBALS['qixit_skip_save_hooks'])) { return; } if (!$post instanceof WP_Post) { $post = get_post($post_id); } if (!$post || $post->post_status !== 'publish') { return; } $this->resync_existing_product($post_id); } /** * When a post transitions to publish, push the purchase product immediately. * * @param string $new_status * @param string $old_status * @param WP_Post $post */ public function resync_on_status_change($new_status, $old_status, $post) { if ($new_status !== 'publish' || $old_status === 'publish') { return; } if (!$post instanceof WP_Post) { return; } $this->resync_existing_product(intval($post->ID)); } /** * Add meta boxes to post editor */ public function add_meta_boxes() { if (function_exists('qixit_debug_log')) { qixit_debug_log('Qixit_Metabox: add_meta_boxes() called'); } $post_types = apply_filters('qixit_metabox_post_types', array('post', 'page')); if (function_exists('qixit_debug_log')) { qixit_debug_log('Qixit_Metabox: Registering for post types: ' . implode(', ', $post_types)); } foreach ($post_types as $post_type) { // Register metabox in sidebar with high priority for both editors add_meta_box( 'qixit-settings', __('2WayMicropay Settings', '2waymicropay'), array($this, 'render_meta_box'), $post_type, 'side', // Always sidebar 'high', // High priority (appears at top) null // No callback args ); if (function_exists('qixit_debug_log')) { qixit_debug_log('Qixit_Metabox: Added metabox for post type: ' . $post_type); } } // For Block Editor (Gutenberg) - ensure metabox appears in sidebar // by adding to the document settings panel location add_action('enqueue_block_editor_assets', array($this, 'enqueue_block_editor_sidebar')); } /** * Enqueue styles for consistent metabox appearance in Gutenberg * Note: In Block Editor, PHP metaboxes appear below content. * We add styling to make the Qixit metabox more prominent. */ public function enqueue_block_editor_sidebar() { // Add styles to make the metabox stand out when it appears below content wp_add_inline_style('wp-edit-post', " /* Qixit Metabox styling in Block Editor */ #qixit-settings { border: 2px solid #52ac5b !important; border-radius: 8px !important; background: linear-gradient(135deg, #f1f9f1 0%, #fff 100%) !important; } #qixit-settings .postbox-header { background: linear-gradient(135deg, #52ac5b 0%, #306817 100%) !important; border-radius: 6px 6px 0 0 !important; } #qixit-settings .postbox-header h2 { color: #fff !important; } #qixit-settings .postbox-header .handle-actions button { color: #fff !important; } /* Add notice at top of Block Editor for Qixit settings location */ .edit-post-layout__metaboxes:has(#qixit-settings)::before { content: '↓ Qixit Micropay Settings are below'; display: block; background: #52ac5b; color: white; padding: 8px 16px; text-align: center; font-weight: 600; margin-bottom: 10px; border-radius: 4px; } "); } /** * Render the meta box content * * @param WP_Post $post Current post object */ public function render_meta_box($post) { // Add nonce for security wp_nonce_field('qixit_metabox', 'qixit_metabox_nonce'); // Get existing product data $product_manager = Qixit_Product::get_instance(); $product = $product_manager->get_product_by_post_id($post->ID); // Set defaults $post_type = $product ? $product->qixit_post_type : QIXIT_REG; $cost = $product ? $product->premium_post_cost : QIXIT_DEFAULT_COST; $default_author_split = intval(get_option('qixit_author_default_split_percent', QIXIT_DEFAULT_PERCENT_TO_AUTHOR)); $percent_to_author = $product ? $product->percent_to_author : $default_author_split; $can_manage_author_share = current_user_can('manage_options') || current_user_can('edit_others_posts'); $use_readmore = $product ? (isset($product->use_readmore) ? $product->use_readmore : 0) : 0; $comments_cost = $product ? (isset($product->premium_comments_cost) ? $product->premium_comments_cost : 0) : 0; $allow_unpaid_comments = get_post_meta($post->ID, '_qixit_allow_unpaid_comments', true) === '1'; $paid_comments_opt_out = get_post_meta($post->ID, '_qixit_paid_comments_opt_out', true) === '1'; $enable_paid_comments = $product ? (isset($product->enable_paid_comments) ? $product->enable_paid_comments : 0) : 0; $global_paid_comments_enabled = class_exists('Qixit_Comments') ? Qixit_Comments::is_global_paid_comments_enabled() : boolval(get_option('qixit_enable_paid_comments_global', false)); $global_default_comment_price = class_exists('Qixit_Comments') ? Qixit_Comments::get_global_paid_comments_default_price() : 0.05; // access_hours: -1 = forever (pay once), -2 = pay per view, 0 = session only, N = hours $access_hours = $product ? (isset($product->access_hours) ? intval($product->access_hours) : -1) : -1; // Commercial category - defaults to settings value $default_commercial_category = get_option('qixit_default_commercial_category', ''); $commercial_category = $product ? (isset($product->commercial_category) ? $product->commercial_category : $default_commercial_category) : $default_commercial_category; // Default new author/contributor posts to paid comments at $0.20. // This applies only when no local product exists yet. $post_author_user = get_userdata(intval($post->post_author)); $is_author_content = $post_author_user && array_intersect(array('author', 'contributor'), (array) $post_author_user->roles); if (!$product && !$paid_comments_opt_out && !$allow_unpaid_comments && $global_paid_comments_enabled) { $enable_paid_comments = 1; if (floatval($comments_cost) <= 0) { $comments_cost = $global_default_comment_price; } } if (!$product && $is_author_content) { $enable_paid_comments = 1; if (floatval($comments_cost) <= 0) { $comments_cost = 0.20; } } // Fetch commercial categories from the API (uses cached values) $admin = Qixit_Admin::get_instance(); $commercial_categories = $admin->get_commercial_categories(); // Remove the empty "Select" option for metabox since we're just picking a category unset($commercial_categories['']); // Connection status is site-level (publisher credentials / Site API Key), // not per-author. Paid contributors should use the publisher's config. $auth = Qixit_Auth::get_instance(); $is_authenticated = $auth->is_authenticated(); $site_api_key = trim((string) get_option('qixit_site_api_key', '')); if ($site_api_key === '') { $site_api_key = trim((string) get_option('qixit_developer_api_key', '')); } $is_connected = ($site_api_key !== '') || $is_authenticated; ?>
open_comments_without_reentry($post_id); } else { delete_post_meta($post_id, '_qixit_allow_unpaid_comments'); } $global_paid_comments_enabled = class_exists('Qixit_Comments') ? Qixit_Comments::is_global_paid_comments_enabled() : boolval(get_option('qixit_enable_paid_comments_global', false)); if ($global_paid_comments_enabled) { if ($enable_paid_comments || $allow_unpaid_comments) { delete_post_meta($post_id, '_qixit_paid_comments_opt_out'); } else { update_post_meta($post_id, '_qixit_paid_comments_opt_out', '1'); } } else { delete_post_meta($post_id, '_qixit_paid_comments_opt_out'); } // Get access hours $access_hours = isset($_POST['qixit_access_hours']) ? intval(wp_unslash($_POST['qixit_access_hours'])) : -1; // Get commercial category (default from settings) $default_commercial_category = get_option('qixit_default_commercial_category', 'CONTENT'); $commercial_category = isset($_POST['qixit_commercial_category']) ? sanitize_text_field(wp_unslash($_POST['qixit_commercial_category'])) : $default_commercial_category; // Get product manager $product_manager = Qixit_Product::get_instance(); $existing_product = $product_manager->get_product_by_post_id($post_id); $post_author_user = get_userdata(intval(get_post_field('post_author', $post_id))); $is_author_content = $post_author_user && array_intersect(array('author', 'contributor'), (array) $post_author_user->roles); if (!$existing_product && $is_author_content && !isset($_POST['qixit_enable_paid_comments'])) { $enable_paid_comments = 1; } if ($enable_paid_comments && (!$existing_product || floatval($comments_cost) <= 0)) { $default_comment_price = class_exists('Qixit_Comments') ? Qixit_Comments::get_global_paid_comments_default_price() : 0.05; if ($is_author_content) { $default_comment_price = max(0.20, $default_comment_price); } $comments_cost = max($default_comment_price, floatval($comments_cost)); } $default_author_split = intval(get_option('qixit_author_default_split_percent', QIXIT_DEFAULT_PERCENT_TO_AUTHOR)); $can_manage_author_share = current_user_can('manage_options') || current_user_can('edit_others_posts'); if ($can_manage_author_share) { $author_percent = isset($_POST['qixit_author_percent']) ? intval(wp_unslash($_POST['qixit_author_percent'])) : $default_author_split; } else { // Authors/contributors cannot change this field; preserve existing value. $author_percent = $existing_product ? intval($existing_product->percent_to_author) : $default_author_split; } // If changing to regular (free) and NO paid comments, just update type and return if ($post_type === QIXIT_REG && !$enable_paid_comments) { if ($existing_product) { // Update to regular type (essentially deactivating premium) $product_manager->update_product_type($post_id, QIXIT_REG); } return; } // For free posts with paid comments OR premium posts, we need a product record // Prepare product data $product_data = array( 'post_id' => $post_id, 'qixit_post_type' => $post_type, 'premium_post_cost' => $cost, 'percent_to_author' => $author_percent, 'enable_paid_comments' => $enable_paid_comments, 'premium_comments_cost' => $enable_paid_comments ? $comments_cost : 0, 'use_readmore' => $use_readmore, 'premium_post_url' => get_permalink($post_id), 'access_hours' => $access_hours, 'commercial_category' => $commercial_category ); // Create or update product if ($existing_product) { $product_manager->update_local_product($existing_product->product_id, $product_data); } else { $product_manager->create_product_for_post($post_id, $product_data); } // Defer API sync to shutdown so Update/Publish never fatals on remote API work. // Local product row is already saved above; AJAX "Save & Sync" still syncs immediately. $this->queue_product_sync($post_id); } /** * Register bulk actions */ public function register_bulk_actions($bulk_actions) { // Premium options with flexible access duration $bulk_actions['qixit_premium'] = __('2WayMicropay: Set Premium (Forever Access)', '2waymicropay'); $bulk_actions['qixit_premium_payperplay'] = __('2WayMicropay: Set Premium (Pay Per View)', '2waymicropay'); $bulk_actions['qixit_premium_readmore'] = __('2WayMicropay: Set Premium + Read More Paywall', '2waymicropay'); $bulk_actions['qixit_premium_limited'] = __('2WayMicropay: Set Premium (Limited Hours)', '2waymicropay'); $bulk_actions['qixit_premium_session'] = __('2WayMicropay: Set Premium (Session Only)', '2waymicropay'); // Category change action $bulk_actions['qixit_change_category'] = __('2WayMicropay: Change Commercial Code', '2waymicropay'); // Paid comments actions $bulk_actions['qixit_enable_paid_comments'] = __('2WayMicropay: Enable Paid Comments', '2waymicropay'); $bulk_actions['qixit_disable_paid_comments'] = __('2WayMicropay: Disable Paid Comments', '2waymicropay'); // Other actions $bulk_actions['qixit_set_free'] = __('2WayMicropay: Set as Free', '2waymicropay'); $bulk_actions['qixit_sync_all'] = __('2WayMicropay: Sync All to API', '2waymicropay'); return $bulk_actions; } /** * Handle bulk actions */ public function handle_bulk_actions($redirect_to, $doaction, $post_ids) { $premium_actions = array( 'qixit_premium', 'qixit_premium_payperplay', 'qixit_premium_readmore', 'qixit_premium_limited', 'qixit_premium_session', // Legacy actions for backward compatibility 'qixit_payonce', 'qixit_payonce_readmore', 'qixit_payperwork', 'qixit_payperwork_readmore' ); $all_actions = array_merge($premium_actions, array( 'qixit_set_free', 'qixit_sync_all', 'qixit_change_category', 'qixit_enable_paid_comments', 'qixit_disable_paid_comments' )); if (!in_array($doaction, $all_actions)) { return $redirect_to; } // Verify bulk action nonce before processing request-driven values. if ( !isset($_REQUEST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'bulk-posts') ) { return $redirect_to; } $product_manager = Qixit_Product::get_instance(); $processed = 0; $synced = 0; // Get custom price from request (set by JavaScript) $custom_price = isset($_REQUEST['qixit_bulk_price']) ? floatval(wp_unslash($_REQUEST['qixit_bulk_price'])) : 0; // Get access hours from request (set by JavaScript) $access_hours = isset($_REQUEST['qixit_bulk_access_hours']) ? intval(wp_unslash($_REQUEST['qixit_bulk_access_hours'])) : 24; // Get commercial category from request (set by JavaScript) $bulk_category = isset($_REQUEST['qixit_bulk_category']) ? sanitize_text_field(wp_unslash($_REQUEST['qixit_bulk_category'])) : ''; // Validate price for premium actions if (in_array($doaction, $premium_actions)) { if ($custom_price <= 0) { // Redirect back with error - no price specified $redirect_to = add_query_arg(array( 'qixit_bulk_error' => 'no_price' ), $redirect_to); return $redirect_to; } } foreach ($post_ids as $post_id) { switch ($doaction) { // New actions with access_hours case 'qixit_premium': $this->set_post_premium($post_id, $custom_price, false, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, -1); $processed++; break; case 'qixit_premium_payperplay': $this->set_post_premium($post_id, $custom_price, false, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, -2); $processed++; break; case 'qixit_premium_readmore': $this->set_post_premium($post_id, $custom_price, true, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, -1); $processed++; break; case 'qixit_premium_limited': $this->set_post_premium($post_id, $custom_price, false, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, $access_hours); $processed++; break; case 'qixit_premium_session': $this->set_post_premium($post_id, $custom_price, false, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, 0); $processed++; break; // Legacy actions (backward compatibility) case 'qixit_payonce': $this->set_post_premium($post_id, $custom_price, false, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, -1); $processed++; break; case 'qixit_payonce_readmore': $this->set_post_premium($post_id, $custom_price, true, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, -1); $processed++; break; case 'qixit_payperwork': $this->set_post_premium($post_id, $custom_price, false, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, 24); $processed++; break; case 'qixit_payperwork_readmore': $this->set_post_premium($post_id, $custom_price, true, QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME, 24); $processed++; break; case 'qixit_set_free': $this->set_post_free($post_id); $processed++; break; case 'qixit_sync_all': $product = $product_manager->get_product_by_post_id($post_id); if ($product && $product->qixit_post_type !== QIXIT_REG) { $result = $product_manager->sync_product_to_api($post_id); if ($result['success']) { $synced++; } } $processed++; break; case 'qixit_change_category': if (!empty($bulk_category)) { $product = $product_manager->get_product_by_post_id($post_id); if ($product) { // Update local product with new category $product_manager->update_local_product($product->product_id, array( 'commercial_category' => $bulk_category )); // Also sync to API if the product has a GUID if (!empty($product->post_qixit_PID_guid)) { $result = $product_manager->sync_product_to_api($post_id); if ($result['success']) { $synced++; } } $processed++; } } break; case 'qixit_enable_paid_comments': // Get comment price from request or use default $comment_price = isset($_REQUEST['qixit_bulk_comment_price']) ? floatval(wp_unslash($_REQUEST['qixit_bulk_comment_price'])) : 0.05; if ($comment_price < 0.01) $comment_price = 0.05; // Enable paid comments (also auto-enables WordPress comments and syncs to API) Qixit_Comments::enable_paid_comments_for_post($post_id, $comment_price); $processed++; $synced++; break; case 'qixit_disable_paid_comments': Qixit_Comments::disable_paid_comments_for_post($post_id); $processed++; break; } } $redirect_to = add_query_arg(array( 'qixit_bulk_action' => $doaction, 'qixit_processed' => $processed, 'qixit_synced' => $synced, 'qixit_price' => $custom_price, 'qixit_access_hours' => $access_hours ), $redirect_to); return $redirect_to; } /** * Set a post as premium * * @param int $post_id Post ID * @param float $cost Price for the content * @param bool $use_readmore Whether to use Read More tag as paywall * @param string $product_type Product type (legacy, kept for compatibility) * @param int $access_hours Access duration: -1=forever, 0=session, N=hours * @param bool $sync_to_api Whether to sync changes to the API (default true) */ private function set_post_premium($post_id, $cost, $use_readmore = false, $product_type = null, $access_hours = -1, $sync_to_api = true) { if ($product_type === null) { $product_type = QIXIT_PREMIUM_PAY_ONCE_VIEW_ANYTIME; } $product_manager = Qixit_Product::get_instance(); $existing = $product_manager->get_product_by_post_id($post_id); $data = array( 'post_id' => $post_id, 'qixit_post_type' => $product_type, 'premium_post_cost' => $cost, 'percent_to_author' => floatval(QIXIT_DEFAULT_PERCENT_TO_AUTHOR), 'premium_post_url' => get_permalink($post_id), 'use_readmore' => $use_readmore ? 1 : 0, 'access_hours' => $access_hours ); if ($existing) { $product_manager->update_local_product($existing->product_id, $data); } else { $product_manager->create_product_for_post($post_id, $data); } // Auto-sync to API if enabled if ($sync_to_api) { $product_manager->sync_product_to_api($post_id); $this->mark_synced_in_request($post_id); } } /** * Set a post as free (clear premium settings) * * @param int $post_id Post ID */ private function set_post_free($post_id) { $product_manager = Qixit_Product::get_instance(); $existing = $product_manager->get_product_by_post_id($post_id); if ($existing) { // Clear all premium settings $data = array( 'qixit_post_type' => QIXIT_REG, 'premium_post_cost' => 0, 'use_readmore' => 0, 'access_hours' => -1 // Reset to default ); $product_manager->update_local_product($existing->product_id, $data); // Note: We keep the API product if it exists, in case it's reactivated later // The product on the API side will show as available but the WordPress side // won't enforce the paywall since qixit_post_type = QIXIT_REG } } /** * Bulk action admin notices */ public function bulk_action_notices() { // Check for stale GUID purge notices (set by sync process when API product not found) global $post; if ($post && is_object($post)) { $post_id = $post->ID; // Check for main product stale GUID if (get_transient('qixit_stale_guid_purged_' . $post_id)) { delete_transient('qixit_stale_guid_purged_' . $post_id); echo ''; echo '2WayMicropay: '; echo esc_html__('The product ID for this post was not found on the API (may have been deleted). A new product has been created automatically.', '2waymicropay'); echo '
'; echo '2WayMicropay: '; echo esc_html__('The comments product ID for this post was not found on the API (may have been deleted). A new comments product has been created automatically.', '2waymicropay'); echo '
' . esc_html($error_message) . '
' . esc_html($message) . '