enabled()) { if (wp_next_scheduled('afop_stock_daily_check')) { wp_clear_scheduled_hook('afop_stock_daily_check'); } if (wp_next_scheduled('afop_stock_changed_check')) { wp_clear_scheduled_hook('afop_stock_changed_check'); } return; } if (!wp_next_scheduled('afop_stock_daily_check')) { wp_schedule_event(time() + MINUTE_IN_SECONDS, 'daily', 'afop_stock_daily_check'); } } /** Coalesce stock changes; never send email inside a checkout request. */ public function stock_changed() { $this->results = array(); if ($this->enabled() && !wp_next_scheduled('afop_stock_changed_check')) { wp_schedule_single_event(time() + 30, 'afop_stock_changed_check'); } } /** * WooCommerce's stock lookup avoids scanning the entire catalog in PHP. * The own-stock meta check excludes variations inheriting a parent's stock. * Draft/private/trashed products and children of unpublished parents are omitted. */ public function low_stock($page = 1, $limit = 20) { if (!$this->enabled()) { return array('products' => array(), 'total' => 0); } $page = max(1, (int) $page); $limit = max(1, min(100, (int) $limit)); $key = $this->threshold() . ':' . $page . ':' . $limit; if (isset($this->results[$key])) { return $this->results[$key]; } global $wpdb; $lookup = $wpdb->prefix . 'wc_product_meta_lookup'; $from = $wpdb->prepare( "FROM %i AS inventory INNER JOIN %i AS product ON product.ID = inventory.product_id WHERE product.post_type IN ('product', 'product_variation') AND product.post_status = 'publish' AND inventory.stock_quantity IS NOT NULL AND inventory.stock_quantity <= %d AND EXISTS (SELECT 1 FROM %i AS stock_setting WHERE stock_setting.post_id = product.ID AND stock_setting.meta_key = '_manage_stock' AND stock_setting.meta_value = 'yes') AND (product.post_type = 'product' OR EXISTS (SELECT 1 FROM %i AS parent WHERE parent.ID = product.post_parent AND parent.post_status = 'publish' AND parent.post_type = 'product'))", $lookup, $wpdb->posts, $this->threshold(), $wpdb->postmeta, $wpdb->posts ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared -- Fully prepared lookup query; inventory must reflect current quantities in this request. $total = (int) $wpdb->get_var('SELECT COUNT(*) ' . $from); $pagination = $wpdb->prepare(' ORDER BY inventory.stock_quantity ASC, product.ID ASC LIMIT %d OFFSET %d', $limit, ($page - 1) * $limit); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared -- Both SQL fragments are prepared above; bounded IDs hydrate through WooCommerce CRUD. $ids = $wpdb->get_col('SELECT product.ID ' . $from . $pagination); $products = array(); foreach ($ids as $id) { $product = wc_get_product($id); if ($product && $product->get_manage_stock('edit') === true && $product->get_stock_quantity('edit') !== null && $product->get_stock_quantity('edit') <= $this->threshold()) { $products[] = $product; } } $this->results[$key] = array('products' => $products, 'total' => $total); return $this->results[$key]; } public function register_menu() { add_submenu_page('afop-settings', __('Low Stock & Alerts', 'advance-fake-order-protector'), __('Stock Alerts', 'advance-fake-order-protector'), 'manage_woocommerce', 'afop-stock-alerts', array($this, 'render_page')); } private function page_url() { return admin_url('admin.php?page=afop-stock-alerts'); } private function edit_url($product) { $id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id(); return admin_url('post.php?post=' . absint($id) . '&action=edit'); } public function save_settings() { if (!current_user_can('manage_woocommerce')) { wp_die(esc_html__('You cannot manage stock alerts.', 'advance-fake-order-protector'), '', array('response' => 403)); } check_admin_referer('afop_save_stock_alerts'); $email = isset($_POST['afop_stock_alert_email']) ? sanitize_email(wp_unslash($_POST['afop_stock_alert_email'])) : ''; $raw_email = isset($_POST['afop_stock_alert_email']) ? trim(sanitize_text_field(wp_unslash($_POST['afop_stock_alert_email']))) : ''; if ($raw_email !== '' && (!is_email($email) || $email !== $raw_email)) { wp_safe_redirect(add_query_arg('afop_stock_error', 'email', $this->page_url())); exit; } $threshold = isset($_POST['afop_stock_alert_threshold']) ? max(0, min(1000000, (int) $_POST['afop_stock_alert_threshold'])) : 20; update_option('afop_stock_alert_threshold', $threshold, false); update_option('afop_stock_alert_email', $email, false); update_option('afop_stock_alerts_enabled', isset($_POST['afop_stock_alerts_enabled']) ? 'yes' : 'no', false); $this->results = array(); $this->sync_schedule(); $this->stock_changed(); wp_safe_redirect(add_query_arg('afop_stock_saved', '1', $this->page_url())); exit; } public function render_page() { if (!current_user_can('manage_woocommerce')) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only pagination; access is checked above and all settings writes require a nonce. $page = isset($_GET['paged']) ? max(1, absint($_GET['paged'])) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This flag only displays a fixed confirmation message; it changes no settings. $saved = isset($_GET['afop_stock_saved']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This flag only displays a fixed validation message; it changes no settings. $error = isset($_GET['afop_stock_error']); $data = $this->low_stock($page); ?>
threshold())); ?>
add_query_arg('paged', '%#%', $this->page_url()), 'format' => '', 'current' => $page, 'total' => max(1, (int) ceil($data['total'] / 20))))); ?>