prefix . 'wpwand_gen_jobs'; if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $jobs)) !== $jobs) { // phpcs:ignore WordPress.DB return; // Pre-1.1.0 install, or the table was never created. } // created_at is stored in site time, the same clock OPT_PERIOD is set from. $since = gmdate('Y-m-d H:i:s', $start); $rows = $wpdb->get_results( // phpcs:ignore WordPress.DB $wpdb->prepare( "SELECT settings FROM {$jobs} WHERE status = 'failed' AND created_at >= %s", // phpcs:ignore $since ), ARRAY_A ); $automation = 0; $bulk = 0; foreach ($rows ?: [] as $row) { $settings = json_decode((string) $row['settings'], true) ?: []; // A scheduled job carries the schedule it belongs to; a bulk job does not. if (($settings['schedule_id'] ?? '') !== '') { $automation++; } else { $bulk++; } } if ($automation > 0) { self::give_back(UsageLimits::OPT_FREE_AUTO_USED, $automation); } if ($bulk > 0) { self::give_back(UsageLimits::OPT_FREE_BULK_USED, $bulk); } } /** * Take $count off a counter without letting it go negative. */ private static function give_back(string $option, int $count): void { $used = (int) get_option($option, 0); if ($used <= 0) { return; } update_option($option, max(0, $used - $count), false); } }