scheduler = $scheduler; } /** * Collect health. * * @return HealthReport */ public function collect() { $queue = $this->scheduler ? $this->scheduler->inspect_health( Constants::SCHEDULER_GROUP ) : array( 'pending' => 0, 'running' => 0, 'wp_cron_delayed' => false, ); global $wpdb; $pending_orders = 0; $failed_orders = 0; $table = $wpdb->prefix . 'marginbeacon_order_risk'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $pending_orders = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table} WHERE current_token = 1 AND presentation_state = 'pending'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $failed_orders = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table} WHERE current_token = 1 AND presentation_state = 'failed'" ); } $tier = ( new EntitlementService() )->resolve()->tier(); $installed = (string) get_option( Constants::OPTION_SCHEMA_VERSION, '' ); $verified = (string) get_option( Constants::OPTION_SCHEMA_VERIFIED, '' ); $schema_ok = version_compare( $installed, AKSMB_SCHEMA_VERSION, '>=' ) && hash_equals( (string) AKSMB_SCHEMA_VERSION, $verified ); $status = 'ok'; if ( ! $schema_ok ) { $status = 'degraded'; } if ( ! empty( $queue['wp_cron_delayed'] ) ) { $status = 'degraded'; } if ( $failed_orders > 0 || $pending_orders > 50 ) { $status = 'degraded'; } if ( ! empty( $queue['failed'] ) && (int) $queue['failed'] > 0 ) { $status = 'degraded'; } $report = new HealthReport( array( 'status' => $status, 'plugin_version' => AKSMB_VERSION, 'schema_version' => get_option( Constants::OPTION_SCHEMA_VERSION, '' ), 'schema_healthy' => $schema_ok, 'formula_version' => AKSMB_FORMULA_VERSION, 'php_version' => PHP_VERSION, 'wp_version' => get_bloginfo( 'version' ), 'wc_version' => defined( 'WC_VERSION' ) ? WC_VERSION : null, 'queue' => $queue, 'orders_pending' => $pending_orders, 'orders_failed' => $failed_orders, 'activation_gmt' => get_option( Constants::OPTION_ACTIVATION_GMT, '' ), 'tier' => $tier, 'generated_at_gmt' => gmdate( 'c' ), ) ); update_option( Constants::OPTION_LAST_HEALTH, $report->to_array(), false ); return $report; } }