*/ public static function snapshot() { global $wpdb; $plugin_active = self::plugin_is_active(); $wp_version = function_exists( 'get_bloginfo' ) ? (string) get_bloginfo( 'version' ) : ''; $php_version = PHP_VERSION; $db = self::database_info(); $schema = self::schema(); $workflows = array( 'active' => 0, 'disabled' => 0, 'trash' => 0, ); $workflows = Pawa_Integrations::counts(); $logs = array( 'level' => Pawa_Logger::level(), 'count' => 0, 'errors' => 0, ); $counts = Pawa_Logger::counts(); $logs['count'] = isset( $counts['all'] ) ? (int) $counts['all'] : 0; $logs['errors'] = isset( $counts['error'] ) ? (int) $counts['error'] : 0; $integrations = self::third_party(); $cron = self::cron(); $rest = self::rest(); $fatal = ! $db['connected'] || ! $schema['tables']['pawa_runs'] || ! $schema['tables']['pawa_waits']; $degraded = false; if ( ! $cron['tick'] || ! $cron['maintenance'] ) { $degraded = true; } if ( ! $schema['current'] ) { $degraded = true; } $status = 'ok'; if ( $fatal ) { $status = 'error'; } elseif ( $degraded ) { $status = 'degraded'; } return Pawa_Cli_IO::redact( array( 'status' => $status, 'plugin' => array( 'name' => 'Workflow Automation', 'active' => $plugin_active, 'version' => defined( 'PAWA_VERSION' ) ? PAWA_VERSION : '', 'loaded' => class_exists( 'Pawa_Engine' ), ), 'wordpress' => array( 'version' => $wp_version, ), 'php' => array( 'version' => $php_version, ), 'database' => $db, 'schema' => array( 'stored' => $schema['stored'], 'expected' => $schema['expected'], 'current' => $schema['current'], 'tables' => $schema['tables'], ), 'cron' => $cron, 'rest' => $rest, 'workflows' => $workflows, 'logs' => $logs, 'integrations' => $integrations, // No `credential` in this key: the redactor above blanks such a key. 'platforms' => self::credentials_summary(), ) ); } /** * Doctor checks. Each row: id, ok, severity (error|warning|info), message. * * @return array{status:string,checks:array,snapshot:array} */ public static function doctor() { $snap = self::snapshot(); $checks = array(); $checks[] = self::check( 'wordpress', defined( 'ABSPATH' ) && function_exists( 'get_bloginfo' ), 'error', 'WordPress ' . ( isset( $snap['wordpress']['version'] ) ? $snap['wordpress']['version'] : '' ) ); $checks[] = self::check( 'php', version_compare( PHP_VERSION, '7.4', '>=' ), 'error', 'PHP ' . PHP_VERSION . ' (requires 7.4+)' ); $checks[] = self::check( 'plugin_loaded', ! empty( $snap['plugin']['loaded'] ), 'error', 'Workflow Automation ' . ( isset( $snap['plugin']['version'] ) ? $snap['plugin']['version'] : '' ) . ' loaded' ); $checks[] = self::check( 'plugin_active', ! empty( $snap['plugin']['active'] ), 'warning', ! empty( $snap['plugin']['active'] ) ? 'Plugin is active' : 'Plugin is loaded but not marked active' ); $checks[] = self::check( 'database', ! empty( $snap['database']['connected'] ), 'error', isset( $snap['database']['label'] ) ? $snap['database']['label'] : 'Database' ); $checks[] = self::check( 'table_pawa_runs', ! empty( $snap['schema']['tables']['pawa_runs'] ), 'error', 'Table wp_pawa_runs' ); $checks[] = self::check( 'table_pawa_waits', ! empty( $snap['schema']['tables']['pawa_waits'] ), 'error', 'Table wp_pawa_waits' ); $checks[] = self::check( 'table_pawa_deliveries', ! empty( $snap['schema']['tables']['pawa_deliveries'] ), 'error', 'Table wp_pawa_deliveries' ); $checks[] = self::check( 'table_pawa_steps', ! empty( $snap['schema']['tables']['pawa_steps'] ), 'error', 'Table wp_pawa_steps' ); $checks[] = self::check( 'table_pawa_revisions', ! empty( $snap['schema']['tables']['pawa_revisions'] ), 'error', 'Table wp_pawa_revisions' ); $checks[] = self::check( 'table_pawa_logs', ! empty( $snap['schema']['tables']['pawa_logs'] ), 'warning', 'Table wp_pawa_logs' ); $checks[] = self::check( 'schema_version', ! empty( $snap['schema']['current'] ), 'warning', 'Schema ' . ( isset( $snap['schema']['stored'] ) ? $snap['schema']['stored'] : '?' ) . ' (expected ' . ( isset( $snap['schema']['expected'] ) ? $snap['schema']['expected'] : '?' ) . ')' ); $block_problems = Pawa_Block_Loader::problems(); $checks[] = self::check( 'blocks_load', empty( $block_problems['broken'] ), 'error', empty( $block_problems['broken'] ) ? 'All blocks loaded' : 'Blocks failed to load: ' . implode( ', ', $block_problems['broken'] ) ); $checks[] = self::check( 'manifests_valid', empty( $block_problems['manifests'] ), 'error', empty( $block_problems['manifests'] ) ? 'All block and credential manifests are valid' : 'Invalid manifests: ' . implode( '; ', $block_problems['manifests'] ) ); $checks[] = self::check( 'block_credentials', empty( $block_problems['credentials'] ), 'warning', empty( $block_problems['credentials'] ) ? 'Every block credential is installed' : 'Missing credentials: ' . implode( '; ', $block_problems['credentials'] ) ); // Published workflows and waiting runs a For Each item variable stops. $loops = method_exists( 'Pawa_Integrations', 'item_variable_problems' ) ? Pawa_Integrations::item_variable_problems( true ) : array( 'workflows' => array(), 'runs' => array(), ); $loop_message = 'No published workflow or waiting run has a For Each with a reserved item variable'; if ( $loops['workflows'] || $loops['runs'] ) { $parts = array(); foreach ( $loops['workflows'] as $workflow ) { $parts[] = 'workflow #' . (int) $workflow['id'] . ' will not run: ' . (string) $workflow['reason']; } if ( $loops['runs'] ) { $run_ids = array(); foreach ( $loops['runs'] as $run ) { $run_ids[] = '#' . (int) $run['id'] . ' (workflow #' . (int) $run['integration_id'] . ')'; } $parts[] = 'waiting runs that will end with an error when they resume, without running another block: ' . implode( ', ', $run_ids ); } $loop_message = implode( '; ', $parts ) . '. Fix: open the workflow, rename the For Each item variable and the {{name.field}} tokens inside the loop, save, and start those runs again.'; } $checks[] = self::check( 'for_each_item_variables', empty( $loops['workflows'] ) && empty( $loops['runs'] ), 'error', $loop_message ); $schema = self::schema(); foreach ( $schema['columns'] as $table => $info ) { $missing = isset( $info['missing'] ) ? $info['missing'] : array(); $checked = ! empty( $info['checked'] ); $ok = ! $checked || empty( $missing ); $msg = ! $checked ? 'Columns on ' . $table . ' not inspected (DESCRIBE unavailable)' : ( empty( $missing ) ? 'Columns on ' . $table . ' match' : 'Missing columns on ' . $table . ': ' . implode( ', ', $missing ) ); $checks[] = self::check( 'columns_' . $table, $ok, $checked ? 'error' : 'warning', $msg ); } foreach ( $schema['indexes'] as $table => $info ) { $missing = isset( $info['missing'] ) ? $info['missing'] : array(); $ok = empty( $missing ) || empty( $info['checked'] ); $checks[] = self::check( 'indexes_' . $table, $ok, 'warning', empty( $info['checked'] ) ? 'Indexes on ' . $table . ' not inspected (DESCRIBE/SHOW INDEX unavailable)' : ( empty( $missing ) ? 'Indexes on ' . $table . ' match' : 'Missing indexes on ' . $table . ': ' . implode( ', ', $missing ) ) ); } $checks[] = self::check( 'option_log_level', in_array( Pawa_Logger::level(), Pawa_Logger::gates(), true ), 'warning', 'Log level: ' . ( Pawa_Logger::level()) ); $checks[] = self::check( 'cron_tick', ! empty( $snap['cron']['tick'] ), 'warning', ! empty( $snap['cron']['tick'] ) ? 'Engine tick is scheduled' : 'Engine tick (pawa_engine_tick) is not scheduled' ); $checks[] = self::check( 'cron_maintenance', ! empty( $snap['cron']['maintenance'] ), 'warning', ! empty( $snap['cron']['maintenance'] ) ? 'Maintenance cron is scheduled' : 'Maintenance cron (pawa_maintenance) is not scheduled' ); $deferred = 'inline' !== (string) get_option( 'pawa_execution_mode', 'deferred' ); $wp_cron = ! ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ); $checks[] = self::check( 'worker', ! $deferred || $wp_cron, 'warning', ! $deferred ? 'Flows run inline (pawa_execution_mode = inline)' : ( $wp_cron ? 'Deferred runs execute from WP-Cron' : 'DISABLE_WP_CRON is set: deferred runs need a system cron calling wp-cron.php or `wp pawa work`' ) ); $checks[] = self::check( 'rest', ! empty( $snap['rest']['available'] ), 'warning', 'REST namespace pawa/v1' ); $checks[] = self::check( 'crypto', Pawa_Credentials::can_encrypt(), 'warning', Pawa_Credentials::can_encrypt() ? 'Credential encryption is available' : 'Credential encryption is unavailable (secrets cannot be stored)' ); foreach ( $snap['integrations'] as $row ) { $id = isset( $row['id'] ) ? (string) $row['id'] : ''; $active = ! empty( $row['active'] ); $title = isset( $row['title'] ) ? (string) $row['title'] : $id; $message = $title; if ( $active ) { $message .= ' active'; } elseif ( ! empty( $row['installed'] ) ) { $message .= ' installed but inactive'; } else { $message .= ' not installed'; } $checks[] = self::check( 'integration_' . $id, true, 'info', $message ); } $status = 'ok'; foreach ( $checks as $row ) { if ( empty( $row['ok'] ) && 'error' === $row['severity'] ) { $status = 'error'; break; } if ( empty( $row['ok'] ) && 'warning' === $row['severity'] && 'error' !== $status ) { $status = 'degraded'; } } return array( 'status' => $status, 'checks' => $checks, 'snapshot' => $snap, ); } /** * Schema inspection for required Workflow Automation tables. * * @return array */ public static function schema() { $stored = (string) get_option( Pawa_Upgrades::OPTION, '' ); $expected = Pawa_Upgrades::DB_VERSION; $current = ( '' !== $stored && '' !== $expected && version_compare( $stored, $expected, '>=' ) ); $tables = array( 'pawa_runs' => self::table_exists( 'pawa_runs' ), 'pawa_waits' => self::table_exists( 'pawa_waits' ), 'pawa_deliveries' => self::table_exists( 'pawa_deliveries' ), 'pawa_steps' => self::table_exists( 'pawa_steps' ), 'pawa_revisions' => self::table_exists( 'pawa_revisions' ), 'pawa_logs' => self::table_exists( 'pawa_logs' ), ); $columns = array( 'pawa_runs' => self::column_check( 'pawa_runs', self::RUNS_COLUMNS ), 'pawa_waits' => self::column_check( 'pawa_waits', self::WAITS_COLUMNS ), 'pawa_deliveries' => self::column_check( 'pawa_deliveries', self::DELIVERIES_COLUMNS ), 'pawa_steps' => self::column_check( 'pawa_steps', self::STEPS_COLUMNS ), 'pawa_revisions' => self::column_check( 'pawa_revisions', self::REVISIONS_COLUMNS ), 'pawa_logs' => self::column_check( 'pawa_logs', self::LOGS_COLUMNS ), ); $indexes = array( 'pawa_runs' => self::index_check( 'pawa_runs', array( 'PRIMARY', 'run_key', 'integration_status', 'started_at' ) ), 'pawa_waits' => self::index_check( 'pawa_waits', array( 'PRIMARY', 'run_step', 'due', 'token_hash' ) ), 'pawa_deliveries' => self::index_check( 'pawa_deliveries', array( 'PRIMARY', 'integration_delivery', 'expires_at' ) ), 'pawa_steps' => self::index_check( 'pawa_steps', array( 'PRIMARY', 'run_step', 'run_seq', 'run_effect' ) ), 'pawa_revisions' => self::index_check( 'pawa_revisions', array( 'PRIMARY', 'integration_revision', 'run_id' ) ), 'pawa_logs' => self::index_check( 'pawa_logs', array_merge( array( 'PRIMARY' ), Pawa_Logger::INDEXES ) ), ); return array( 'stored' => $stored, 'expected' => $expected, 'current' => $current, 'tables' => $tables, 'columns' => $columns, 'indexes' => $indexes, ); } /** * @param string $suffix Table suffix. * @return bool */ public static function table_exists( $suffix ) { global $wpdb; if ( ! isset( $wpdb ) || ! is_object( $wpdb ) ) { return false; } $table = $wpdb->prefix . ltrim( (string) $suffix, '_' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- schema or server probe; not cacheable data. $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); return is_string( $found ) && $found === $table; } /** * Third-party (and core WordPress) CLI adapters with availability. * * Secrets are never included. * * @return array> */ public static function third_party() { if ( ! class_exists( 'Pawa_Cli_Registry' ) ) { return array(); } $out = array(); foreach ( Pawa_Cli_Registry::instance()->all() as $item ) { $avail = $item->availability(); $out[] = array( 'id' => $item->id(), 'title' => $item->title(), 'kind' => $item->kind(), 'installed' => ! empty( $avail['installed'] ), 'active' => ! empty( $avail['active'] ), 'message' => isset( $avail['message'] ) ? (string) $avail['message'] : '', 'scenarios' => array_keys( $item->scenarios() ), 'aliases' => $item->aliases(), ); } return $out; } /** * Summarize a stored integration for list output. * * @param array $item Pawa_Integrations::to_rest() row. * @return array */ public static function workflow_summary( $item ) { $item = is_array( $item ) ? $item : array(); $status = isset( $item['status'] ) ? (string) $item['status'] : ''; $event = ''; if ( isset( $item['trigger']['data']['eventName'] ) ) { $event = (string) $item['trigger']['data']['eventName']; } $cli_status = $status; if ( 'publish' === $status ) { $cli_status = 'active'; } elseif ( 'draft' === $status ) { $cli_status = 'disabled'; } return array( 'id' => isset( $item['id'] ) ? (int) $item['id'] : 0, 'name' => isset( $item['title'] ) ? (string) $item['title'] : '', 'title' => isset( $item['title'] ) ? (string) $item['title'] : '', 'status' => $cli_status, 'post_status' => $status, 'event' => $event, 'eventTitle' => isset( $item['eventTitle'] ) ? (string) $item['eventTitle'] : '', 'modified' => isset( $item['modified'] ) ? (string) $item['modified'] : '', 'blocks' => isset( $item['blocks'] ) && is_array( $item['blocks'] ) ? count( $item['blocks'] ) : 0, ); } /** * Map CLI status tokens onto post_status. * * @param string $status active|disabled|publish|draft|trash. * @return string */ public static function post_status( $status ) { $status = (string) $status; if ( in_array( $status, array( 'active', 'enabled', 'publish' ), true ) ) { return 'publish'; } if ( in_array( $status, array( 'disabled', 'draft' ), true ) ) { return 'draft'; } return $status; } /** * @return bool */ public static function plugin_is_active() { if ( ! function_exists( 'is_plugin_active' ) ) { $plugin_inc = defined( 'ABSPATH' ) ? ABSPATH . 'wp-admin/includes/plugin.php' : ''; if ( $plugin_inc && is_file( $plugin_inc ) ) { require_once $plugin_inc; } } $basename = defined( 'PAWA_FILE' ) ? plugin_basename( PAWA_FILE ) : 'pluginsandapps-workflow-automation/pluginsandapps-workflow-automation.php'; return function_exists( 'is_plugin_active' ) && is_plugin_active( $basename ); } /** * Human status report lines. * * @param array $snap Snapshot. * @return string[] */ public static function status_lines( $snap ) { $snap = is_array( $snap ) ? $snap : array(); $ok = function ( $cond ) { return $cond ? 'OK' : 'ERROR'; }; $mark = function ( $cond ) { return $cond ? '✓' : '✗'; }; $plugin = isset( $snap['plugin'] ) ? $snap['plugin'] : array(); $db = isset( $snap['database'] ) ? $snap['database'] : array(); $schema = isset( $snap['schema'] ) ? $snap['schema'] : array(); $wf = isset( $snap['workflows'] ) ? $snap['workflows'] : array(); $lines = array( 'Workflow Automation Status', '', 'Plugin: ' . ( ! empty( $plugin['active'] ) ? 'Active' : ( ! empty( $plugin['loaded'] ) ? 'Loaded' : 'Missing' ) ), 'Version: ' . ( isset( $plugin['version'] ) ? $plugin['version'] : '' ), 'WordPress: ' . ( isset( $snap['wordpress']['version'] ) ? $snap['wordpress']['version'] : '' ), 'PHP: ' . ( isset( $snap['php']['version'] ) ? $snap['php']['version'] : '' ), 'Database: ' . $ok( ! empty( $db['connected'] ) ), 'Schema: ' . ( ! empty( $schema['current'] ) ? 'Current' : ( isset( $schema['stored'] ) ? $schema['stored'] : 'Unknown' ) ), '', 'Integrations:', ); foreach ( isset( $snap['integrations'] ) ? $snap['integrations'] : array() as $row ) { if ( isset( $row['kind'] ) && 'core' === $row['kind'] ) { continue; } $lines[] = $mark( ! empty( $row['active'] ) ) . ' ' . ( isset( $row['title'] ) ? $row['title'] : $row['id'] ); } $lines[] = ''; $lines[] = 'Workflows:'; $lines[] = 'Active: ' . ( isset( $wf['active'] ) ? (int) $wf['active'] : 0 ); $lines[] = 'Disabled: ' . ( isset( $wf['disabled'] ) ? (int) $wf['disabled'] : 0 ); $lines[] = ''; $lines[] = 'Status: ' . strtoupper( isset( $snap['status'] ) ? (string) $snap['status'] : 'unknown' ); return $lines; } /** * @return array{connected:bool,name:string,label:string} */ private static function database_info() { global $wpdb; $connected = false; $name = ''; $label = 'Database unavailable'; if ( isset( $wpdb ) && is_object( $wpdb ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- schema or server probe; not cacheable data. $one = $wpdb->get_var( 'SELECT 1' ); if ( '1' === (string) $one ) { $connected = true; $name = isset( $wpdb->dbname ) ? (string) $wpdb->dbname : ''; $label = 'Database connected' . ( $name ? ' (' . $name . ')' : '' ); } elseif ( ! empty( $wpdb->last_error ) ) { $label = 'Database error: ' . $wpdb->last_error; } } return array( 'connected' => $connected, 'name' => $name, 'label' => $label, ); } /** * @return array{tick:bool,resume:mixed,maintenance:bool} */ private static function cron() { $tick = false; $maint = false; $resume = false; if ( function_exists( 'wp_next_scheduled' ) ) { $tick = (bool) wp_next_scheduled( Pawa_Engine::TICK_HOOK ); $resume = wp_next_scheduled( Pawa_Engine::RESUME_HOOK ); $maint = (bool) wp_next_scheduled( Pawa_Logger::CRON_HOOK ); } return array( 'tick' => $tick, 'resume' => (bool) $resume, 'maintenance' => $maint, ); } /** * @return array{available:bool,namespace:string,routes:string[]} */ private static function rest() { $namespace = 'pawa/v1'; $routes = array( '/pawa/v1/status', '/pawa/v1/events-catalog', '/pawa/v1/run', '/pawa/v1/credentials', '/pawa/v1/integrations', ); $available = class_exists( 'Pawa_Rest' ); return array( 'available' => $available, 'namespace' => $namespace, 'routes' => $routes, ); } /** * Platform keys and whether any account exists. Never secrets. * * @return array */ private static function credentials_summary() { $out = array(); foreach ( Pawa_Credentials::platforms() as $platform => $schema ) { $accounts = Pawa_Credentials::accounts( $platform ); $out[] = array( 'platform' => (string) $platform, 'title' => isset( $schema['title'] ) ? (string) $schema['title'] : (string) $platform, 'accounts' => is_array( $accounts ) ? count( $accounts ) : 0, ); } return $out; } /** * @param string $id Check id. * @param bool $ok Pass? * @param string $severity error|warning|info. * @param string $message Message. * @return array{id:string,ok:bool,severity:string,message:string} */ private static function check( $id, $ok, $severity, $message ) { return array( 'id' => (string) $id, 'ok' => (bool) $ok, 'severity' => (string) $severity, 'message' => (string) $message, ); } /** * @param string $suffix Table suffix. * @param string[] $expected Expected columns. * @return array{present:string[],missing:string[],checked:bool} */ private static function column_check( $suffix, $expected ) { global $wpdb; $present = array(); $checked = false; if ( self::table_exists( $suffix ) && isset( $wpdb ) && is_object( $wpdb ) && method_exists( $wpdb, 'get_col' ) ) { $table = $wpdb->prefix . ltrim( $suffix, '_' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- DESCRIBE of this plugin's own table: its name is $wpdb->prefix plus a fixed suffix, and DESCRIBE takes no placeholders; schema or server probe; not cacheable data. $cols = $wpdb->get_col( 'DESCRIBE ' . $table, 0 ); if ( is_array( $cols ) && $cols ) { $present = array_map( 'strval', $cols ); $checked = true; } } $missing = array(); if ( $checked ) { foreach ( $expected as $col ) { if ( ! in_array( $col, $present, true ) ) { $missing[] = $col; } } } return array( 'present' => $present, 'missing' => $missing, 'checked' => $checked, ); } /** * @param string $suffix Table suffix. * @param string[] $expected Expected index names. * @return array{present:string[],missing:string[],checked:bool} */ private static function index_check( $suffix, $expected ) { global $wpdb; $present = array(); $checked = false; if ( self::table_exists( $suffix ) && isset( $wpdb ) && is_object( $wpdb ) && method_exists( $wpdb, 'get_results' ) ) { $table = $wpdb->prefix . ltrim( $suffix, '_' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- SHOW INDEX on this plugin's own table: its name is $wpdb->prefix plus a fixed suffix, and SHOW takes no placeholders; schema or server probe; not cacheable data. $rows = $wpdb->get_results( 'SHOW INDEX FROM ' . $table, ARRAY_A ); if ( is_array( $rows ) && $rows ) { $checked = true; foreach ( $rows as $row ) { if ( isset( $row['Key_name'] ) ) { $present[] = (string) $row['Key_name']; } } $present = array_values( array_unique( $present ) ); } } $missing = array(); if ( $checked ) { foreach ( $expected as $name ) { if ( ! in_array( $name, $present, true ) ) { $missing[] = $name; } } } return array( 'present' => $present, 'missing' => $missing, 'checked' => $checked, ); } }