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