>} */ public static function compare( $expected ) { $expected = is_array( $expected ) ? $expected : array(); $checks = array(); foreach ( $expected as $root => $spec ) { $root = (string) $root; if ( 'workflow' === $root || 'integration' === $root ) { self::check_workflow( $spec, $checks, $root ); } elseif ( 'workflows' === $root && is_array( $spec ) ) { foreach ( $spec as $i => $row ) { self::check_workflow( $row, $checks, 'workflows.' . $i ); } } elseif ( 'option' === $root && is_array( $spec ) ) { foreach ( $spec as $key => $value ) { $actual = get_option( (string) $key, null ); self::push( $checks, 'option.' . $key, $value, $actual ); } } elseif ( 'table' === $root && is_array( $spec ) ) { self::check_tables( $spec, $checks ); } elseif ( 'run' === $root ) { self::check_run( $spec, $checks, 'run' ); } elseif ( 'post' === $root && is_array( $spec ) ) { self::check_post( $spec, $checks, 'post' ); } elseif ( 'postmeta' === $root && is_array( $spec ) ) { self::check_postmeta( $spec, $checks ); } elseif ( 'usermeta' === $root && is_array( $spec ) ) { self::check_usermeta( $spec, $checks ); } else { self::push( $checks, $root, $spec, null, false, 'unknown expected root `' . $root . '`' ); } } $matched = true; foreach ( $checks as $row ) { if ( empty( $row['matched'] ) ) { $matched = false; } } return array( 'matched' => $matched, 'checks' => $checks, ); } /** * @param mixed $spec Spec. * @param array $checks Checks (by ref). * @param string $prefix Path prefix. */ private static function check_workflow( $spec, &$checks, $prefix ) { if ( ! is_array( $spec ) ) { self::push( $checks, $prefix, $spec, null, false, 'workflow spec must be an object' ); return; } $id = isset( $spec['id'] ) ? (int) $spec['id'] : 0; if ( $id < 1 ) { self::push( $checks, $prefix . '.id', $spec['id'] ?? null, null, false, 'workflow id is required' ); return; } $item = Pawa_Integrations::get( $id ); if ( ! is_array( $item ) ) { self::push( $checks, $prefix, array( 'id' => $id ), null, false, 'workflow ' . $id . ' was not found' ); return; } $summary = Pawa_Cli_Inspect::workflow_summary( $item ); foreach ( $spec as $field => $expected ) { $field = (string) $field; if ( 'id' === $field ) { self::push( $checks, $prefix . '.id', (int) $expected, (int) $summary['id'] ); continue; } if ( 'status' === $field ) { $want = Pawa_Cli_Inspect::post_status( (string) $expected ); if ( in_array( (string) $expected, array( 'active', 'disabled', 'enabled' ), true ) ) { self::push( $checks, $prefix . '.status', (string) $expected, $summary['status'] ); } else { self::push( $checks, $prefix . '.status', $want, $summary['post_status'] ); } continue; } if ( 'title' === $field || 'name' === $field ) { self::push( $checks, $prefix . '.' . $field, (string) $expected, $summary['title'] ); continue; } if ( 'event' === $field || 'eventName' === $field ) { self::push( $checks, $prefix . '.event', (string) $expected, $summary['event'] ); continue; } if ( 'post_status' === $field ) { self::push( $checks, $prefix . '.post_status', (string) $expected, $summary['post_status'] ); continue; } self::push( $checks, $prefix . '.' . $field, $expected, isset( $item[ $field ] ) ? $item[ $field ] : null ); } } /** * @param array $spec Spec. * @param array $checks Checks. */ private static function check_tables( $spec, &$checks ) { foreach ( $spec as $suffix => $row ) { $suffix = (string) $suffix; $path = 'table.' . $suffix; $exists = Pawa_Cli_Inspect::table_exists( $suffix ); if ( ! is_array( $row ) ) { self::push( $checks, $path, $row, $exists ); continue; } if ( array_key_exists( 'exists', $row ) ) { self::push( $checks, $path . '.exists', (bool) $row['exists'], $exists ); } if ( array_key_exists( 'count', $row ) ) { self::push( $checks, $path . '.count', (int) $row['count'], self::table_count( $suffix ) ); } } } /** * @param mixed $spec Spec. * @param array $checks Checks. * @param string $prefix Path. */ private static function check_run( $spec, &$checks, $prefix ) { if ( ! is_array( $spec ) ) { self::push( $checks, $prefix, $spec, null, false, 'run spec must be an object' ); return; } $args = array( 'limit' => 20 ); if ( ! empty( $spec['integration_id'] ) ) { $args['integration_id'] = (int) $spec['integration_id']; } if ( ! empty( $spec['status'] ) ) { $args['status'] = (string) $spec['status']; } $rows = Pawa_Runs::recent( $args ); $hit = null; foreach ( (array) $rows as $row ) { if ( ! empty( $spec['event_name'] ) && (string) $row['event_name'] !== (string) $spec['event_name'] ) { continue; } $hit = $row; break; } if ( ! is_array( $hit ) ) { self::push( $checks, $prefix, $spec, null, false, 'no matching run row' ); return; } foreach ( $spec as $field => $expected ) { $field = (string) $field; $actual = isset( $hit[ $field ] ) ? $hit[ $field ] : null; if ( in_array( $field, array( 'id', 'integration_id' ), true ) ) { self::push( $checks, $prefix . '.' . $field, (int) $expected, (int) $actual ); } else { self::push( $checks, $prefix . '.' . $field, $expected, $actual ); } } if ( ! empty( $spec['integration_id'] ) && class_exists( 'Pawa_Cli_Harness' ) ) { // Optional database confirmation when a live harness is not required: global $wpdb; if ( isset( $wpdb ) && Pawa_Cli_Inspect::table_exists( 'pawa_runs' ) ) { $table = esc_sql( $wpdb->prefix . 'pawa_runs' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin-owned table holding live state; a cache would serve stale rows. $db = $wpdb->get_row( $wpdb->prepare( 'SELECT status, event_name FROM %i WHERE integration_id = %d ORDER BY id DESC LIMIT 1', $table, (int) $spec['integration_id'] ), ARRAY_A ); if ( is_array( $db ) && isset( $spec['status'] ) ) { self::push( $checks, $prefix . '.status_db', (string) $spec['status'], (string) $db['status'] ); } } } } /** * @param array $spec Spec. * @param array $checks Checks. * @param string $prefix Path. */ private static function check_post( $spec, &$checks, $prefix ) { $id = isset( $spec['id'] ) ? (int) $spec['id'] : 0; if ( $id < 1 ) { self::push( $checks, $prefix . '.id', $spec['id'] ?? null, null, false, 'post id is required' ); return; } $post = get_post( $id ); if ( ! $post ) { self::push( $checks, $prefix, array( 'id' => $id ), null, false, 'post ' . $id . ' was not found' ); return; } foreach ( $spec as $field => $expected ) { $field = (string) $field; if ( 'id' === $field || 'ID' === $field ) { self::push( $checks, $prefix . '.id', (int) $expected, (int) $post->ID ); continue; } $actual = isset( $post->{$field} ) ? $post->{$field} : null; self::push( $checks, $prefix . '.' . $field, $expected, $actual ); } } /** * @param array $spec Spec: { "123": { "key": "value" } } or { "id": 123, "key": "k", "value": "v" }. * @param array $checks Checks. */ private static function check_postmeta( $spec, &$checks ) { if ( isset( $spec['id'], $spec['key'] ) ) { $id = (int) $spec['id']; $key = (string) $spec['key']; $app = get_post_meta( $id, $key, true ); $want = array_key_exists( 'value', $spec ) ? $spec['value'] : null; self::push( $checks, 'postmeta.' . $id . '.' . $key, $want, $app ); return; } foreach ( $spec as $id => $fields ) { if ( ! is_array( $fields ) ) { continue; } foreach ( $fields as $key => $expected ) { $app = get_post_meta( (int) $id, (string) $key, true ); self::push( $checks, 'postmeta.' . $id . '.' . $key, $expected, $app ); } } } /** * @param array $spec Spec. * @param array $checks Checks. */ private static function check_usermeta( $spec, &$checks ) { if ( isset( $spec['id'], $spec['key'] ) ) { $id = (int) $spec['id']; $key = (string) $spec['key']; $app = get_user_meta( $id, $key, true ); $want = array_key_exists( 'value', $spec ) ? $spec['value'] : null; self::push( $checks, 'usermeta.' . $id . '.' . $key, $want, $app ); return; } foreach ( $spec as $id => $fields ) { if ( ! is_array( $fields ) ) { continue; } foreach ( $fields as $key => $expected ) { $app = get_user_meta( (int) $id, (string) $key, true ); self::push( $checks, 'usermeta.' . $id . '.' . $key, $expected, $app ); } } } /** * @param string $suffix Table suffix. * @return int */ private static function table_count( $suffix ) { global $wpdb; if ( ! Pawa_Cli_Inspect::table_exists( $suffix ) ) { return 0; } $table = $wpdb->prefix . ltrim( $suffix, '_' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- COUNT on this plugin's own table: its name is $wpdb->prefix plus a fixed suffix; plugin-owned table holding live state; a cache would serve stale rows. return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $table ); } /** * @param array $checks Checks. * @param string $field Dotted path. * @param mixed $expected Expected. * @param mixed $actual Actual. * @param bool $matched Override. * @param string $note Optional note used as mismatch reason. */ private static function push( &$checks, $field, $expected, $actual, $matched = null, $note = '' ) { if ( null === $matched ) { $matched = self::same( $expected, $actual ); } $row = array( 'field' => (string) $field, 'expected' => $expected, 'actual' => $actual, 'matched' => (bool) $matched, ); if ( '' !== $note && ! $matched ) { $row['message'] = $note; } $checks[] = $row; } /** * Loose compare: numeric strings vs ints, bool vs 0/1. * * @param mixed $expected Expected. * @param mixed $actual Actual. * @return bool */ private static function same( $expected, $actual ) { if ( $expected === $actual ) { return true; } if ( is_bool( $expected ) ) { return $expected === (bool) $actual; } if ( is_int( $expected ) || is_float( $expected ) ) { return (string) $expected === (string) $actual; } if ( is_string( $expected ) && ( is_int( $actual ) || is_float( $actual ) || is_string( $actual ) ) ) { return (string) $expected === (string) $actual; } return false; } }