| 1 |
<?php |
| 2 |
/** |
| 3 |
* Reports a pending WordPress core update to the shell as the `coreUpdate` |
| 4 |
* config value. The shell resolves the release art and renders the |
| 5 |
* notification client-side (see `src/update-notice.ts`). |
| 6 |
* |
| 7 |
* @package OpenStation |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Whether `$available` is a new major (X.Y branch) relative to `$installed` |
| 14 |
* — `6.9.2 -> 7.0` is major, `7.0 -> 7.0.2` is not. |
| 15 |
* |
| 16 |
* @param string $installed Installed version. |
| 17 |
* @param string $available Available version. |
| 18 |
* @return bool |
| 19 |
*/ |
| 20 |
function openstation_is_major_update( $installed, $available ) { |
| 21 |
$branch = static function ( $v ) { |
| 22 |
$p = explode( '.', (string) $v ); |
| 23 |
return ( isset( $p[0] ) ? $p[0] : '0' ) . '.' . ( isset( $p[1] ) ? $p[1] : '0' ); |
| 24 |
}; |
| 25 |
return version_compare( $branch( $available ), $branch( $installed ), '>' ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* The X.Y branch for a version (`7.0.2` -> `7.0`). |
| 30 |
* |
| 31 |
* @param string $version Version string. |
| 32 |
* @return string |
| 33 |
*/ |
| 34 |
function openstation_release_branch( $version ) { |
| 35 |
$p = explode( '.', (string) $version ); |
| 36 |
return ( isset( $p[0] ) ? $p[0] : '0' ) . '.' . ( isset( $p[1] ) ? $p[1] : '0' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* The pending core update as a shell descriptor, or null when none is |
| 41 |
* pending / the user can't `update_core`. |
| 42 |
* |
| 43 |
* `version` is the branch when crossing into a new major (the codename is |
| 44 |
* added client-side), else the exact version; `available` is the exact |
| 45 |
* version (the dismissal key); `crossing` flags a new major. |
| 46 |
* |
| 47 |
* @return array{version:string,available:string,branch:string,url:string,crossing:bool}|null |
| 48 |
*/ |
| 49 |
function openstation_get_core_update() { |
| 50 |
if ( ! current_user_can( 'update_core' ) ) { |
| 51 |
return null; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! function_exists( 'get_preferred_from_update_core' ) ) { |
| 55 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 56 |
} |
| 57 |
if ( ! function_exists( 'get_preferred_from_update_core' ) ) { |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
$cur = get_preferred_from_update_core(); |
| 62 |
if ( ! isset( $cur->response ) || 'upgrade' !== $cur->response ) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$available = isset( $cur->current ) ? (string) $cur->current : ''; |
| 67 |
if ( '' === $available ) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Whether to show the desktop core-update notification. Return false |
| 73 |
* to hide it. |
| 74 |
* |
| 75 |
* @param bool $show Default true. |
| 76 |
*/ |
| 77 |
if ( ! apply_filters( 'openstation_show_core_update_notice', true ) ) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
$branch = openstation_release_branch( $available ); |
| 82 |
$crossing = openstation_is_major_update( get_bloginfo( 'version' ), $available ); |
| 83 |
|
| 84 |
return array( |
| 85 |
'version' => $crossing ? $branch : $available, |
| 86 |
'available' => $available, |
| 87 |
'branch' => $branch, |
| 88 |
'url' => self_admin_url( 'update-core.php' ), |
| 89 |
'crossing' => $crossing, |
| 90 |
); |
| 91 |
} |
| 92 |
|