$row[0], 'released' => $row[1], 'active_until' => $row[2], 'security_until' => $row[3], 'latest' => $row[4], 'source' => 'bundled', ); } } return apply_filters( 'sunsetly_lifecycle_cycles', $cycles, $product ); } /** * Parse endoflife.date v1 (and legacy v0) payloads. * * @param mixed $data Decoded JSON. * @return array[] */ public static function parse_endoflife( $data ) { $out = array(); if ( ! is_array( $data ) ) { return $out; } // v1: { result: { releases: [ ... ] } }. if ( isset( $data['result']['releases'] ) && is_array( $data['result']['releases'] ) ) { foreach ( $data['result']['releases'] as $r ) { if ( empty( $r['name'] ) ) { continue; } $out[] = array( 'name' => (string) $r['name'], 'released' => isset( $r['releaseDate'] ) ? (string) $r['releaseDate'] : '', 'active_until' => self::date_or_empty( $r['eoasFrom'] ?? ( $r['isEoas'] ?? '' ) ), 'security_until' => self::date_or_empty( $r['eolFrom'] ?? ( $r['isEol'] ?? '' ) ), 'latest' => isset( $r['latest']['name'] ) ? (string) $r['latest']['name'] : '', 'source' => 'endoflife.date', ); } return $out; } // v0: [ { cycle, releaseDate, support, eol, latest } ]. if ( isset( $data[0]['cycle'] ) ) { foreach ( $data as $r ) { $out[] = array( 'name' => (string) $r['cycle'], 'released' => isset( $r['releaseDate'] ) ? (string) $r['releaseDate'] : '', 'active_until' => self::date_or_empty( $r['support'] ?? '' ), 'security_until' => self::date_or_empty( $r['eol'] ?? '' ), 'latest' => isset( $r['latest'] ) ? (string) $r['latest'] : '', 'source' => 'endoflife.date', ); } } return $out; } /** * Booleans in the feed mean "no date known"; keep only real dates. * * @param mixed $v Value. * @return string */ private static function date_or_empty( $v ) { return ( is_string( $v ) && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $v ) ) ? $v : ''; } /** * Find cycle for an installed version (longest matching prefix, e.g. "10.6" or "8"). * * @param array $cycles Cycles. * @param string $version Version. * @return array|null */ public static function match_cycle( $cycles, $version ) { $version = Sunsetly_Util::clean_version( $version ); $best = null; foreach ( $cycles as $c ) { $name = $c['name']; if ( $version === $name || 0 === strpos( $version, $name . '.' ) ) { if ( null === $best || strlen( $name ) > strlen( $best['name'] ) ) { $best = $c; } } } return $best; } /** * Build a status block for one product. * * @param string $label Display label. * @param string $product endoflife slug. * @param string $version Installed version. * @param int|null $now Timestamp (testing). * @param array $cycles Pre-fetched cycles (testing). * @return array */ public static function status( $label, $product, $version, $now = null, $cycles = null ) { $cycles = null === $cycles ? self::cycles( $product ) : $cycles; $version = Sunsetly_Util::clean_version( $version ); $cycle = self::match_cycle( $cycles, $version ); $status = array( 'label' => $label, 'product' => $product, 'installed' => $version, 'branch' => $cycle ? $cycle['name'] : Sunsetly_Util::branch( $version ), 'latest_patch' => $cycle ? $cycle['latest'] : '', 'patch_behind' => false, 'released' => $cycle ? $cycle['released'] : '', 'active_until' => $cycle ? $cycle['active_until'] : '', 'security_until' => $cycle ? $cycle['security_until'] : '', 'days_active' => null, 'days_security' => null, 'state' => 'unknown', 'recommended' => '', 'supported' => array(), 'source' => $cycle ? $cycle['source'] : '', ); if ( 'php' === $product && $cycle && '' === $status['latest_patch'] ) { $status['latest_patch'] = self::php_latest_patch( $cycle['name'] ); } if ( $status['latest_patch'] && version_compare( $version, $status['latest_patch'], '<' ) ) { $status['patch_behind'] = true; } $status['days_active'] = Sunsetly_Util::days_until( $status['active_until'], $now ); $status['days_security'] = Sunsetly_Util::days_until( $status['security_until'], $now ); if ( $cycle ) { if ( null !== $status['days_security'] && $status['days_security'] < 0 ) { $status['state'] = 'eol'; } elseif ( null !== $status['days_active'] && $status['days_active'] < 0 ) { $status['state'] = 'security'; } else { $status['state'] = 'active'; } } // Supported branches, newest first, and a conservative recommendation: // the newest branch that has had >= 12 months to mature and is still in active support. $now_ts = null === $now ? time() : $now; foreach ( $cycles as $c ) { $days = Sunsetly_Util::days_until( $c['security_until'], $now_ts ); if ( null === $days || $days >= 0 ) { $status['supported'][] = $c['name']; } } usort( $status['supported'], 'version_compare' ); $status['supported'] = array_reverse( $status['supported'] ); foreach ( $cycles as $c ) { $released = $c['released'] ? strtotime( $c['released'] ) : 0; $active = Sunsetly_Util::days_until( $c['active_until'], $now_ts ); if ( $released && ( $now_ts - $released ) >= YEAR_IN_SECONDS && ( null === $active || $active >= 0 ) ) { if ( '' === $status['recommended'] || version_compare( $c['name'], $status['recommended'], '>' ) ) { $status['recommended'] = $c['name']; } } } if ( '' === $status['recommended'] && $status['supported'] ) { $status['recommended'] = $status['supported'][0]; } $status['recommended'] = apply_filters( 'sunsetly_recommended_branch', $status['recommended'], $product, $cycles ); return $status; } /** * Latest patch for a PHP branch from php.net. * * @param string $branch Branch. * @return string */ public static function php_latest_patch( $branch ) { $data = Sunsetly_HTTP::get_json( sprintf( self::PHP_API, rawurlencode( $branch ) ), DAY_IN_SECONDS ); return ( is_array( $data ) && ! empty( $data['version'] ) ) ? (string) $data['version'] : ''; } /** * Detect database server. * * @return array{label:string,product:string,version:string} */ public static function detect_db() { global $wpdb; $info = method_exists( $wpdb, 'db_server_info' ) ? (string) $wpdb->db_server_info() : ''; $version = (string) $wpdb->db_version(); if ( false !== stripos( $info, 'mariadb' ) ) { // "5.5.5-10.6.12-MariaDB" style strings. if ( preg_match( '/(\d+\.\d+\.\d+)-MariaDB/i', $info, $m ) ) { $version = $m[1]; } return array( 'label' => 'MariaDB', 'product' => 'mariadb', 'version' => $version, ); } return array( 'label' => 'MySQL', 'product' => 'mysql', 'version' => $version, ); } }