$minimum, 'message' => $message, 'checked_at' => time(), ), false ); } /** * Forget the stored answer. Called on disconnect: what Send4 asked for is * derived from the connection, and a site with none shows no notice. * * Dismissals stay: each one names the minimum it was for. * * @return void */ public static function forget() { delete_option( self::OPTION ); } /** * Ask Send4 again if the stored answer is more than a day old. * * Run from the daily sync event. Failure leaves the stored answer as it * was: an outage is not news about the minimum. * * @return void */ public static function refresh_if_stale() { if ( ! Send4_Settings::has_credentials() || ! Send4_Settings::base_url_is_allowed() ) { return; } $stored = self::stored(); if ( null !== $stored && ( time() - $stored['checked_at'] ) < self::MAX_AGE ) { return; } $result = ( new Send4_Api_Client() )->handshake(); if ( is_array( $result ) ) { self::remember( $result ); } } /** * The stored answer, or null when Send4 has not been asked since the site * connected. `minimum_version` is '' when Send4 asked for none. * * @return array{minimum_version:string,message:string,checked_at:int}|null */ public static function stored() { $stored = get_option( self::OPTION, null ); if ( ! is_array( $stored ) ) { return null; } return array( 'minimum_version' => isset( $stored['minimum_version'] ) ? (string) $stored['minimum_version'] : '', 'message' => isset( $stored['message'] ) ? (string) $stored['message'] : '', 'checked_at' => isset( $stored['checked_at'] ) ? (int) $stored['checked_at'] : 0, ); } /** * The minimum this version falls short of, or null when it does not. * * @return string|null */ public static function unmet() { $stored = self::stored(); if ( null === $stored || '' === $stored['minimum_version'] || ! version_compare( SEND4_VERSION, $stored['minimum_version'], '<' ) ) { return null; } return $stored['minimum_version']; } /** * Whether the current user should see the notice on this screen. * * Never on a site with no connection: an update would change nothing for * it, and the daily refresh that could clear the notice no longer runs. * * @return bool */ public static function should_show() { $minimum = self::unmet(); if ( ! Send4_Settings::has_credentials() || null === $minimum || ! current_user_can( 'update_plugins' ) ) { return false; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( null === $screen || ! self::is_notice_screen( $screen ) ) { return false; } $dismissed = (string) get_user_meta( get_current_user_id(), self::DISMISSED_META, true ); return '' === $dismissed || version_compare( $dismissed, $minimum, '<' ); } /** * The Plugins screens, and our own. * * @param WP_Screen $screen Current screen. * @return bool */ private static function is_notice_screen( $screen ) { if ( in_array( $screen->id, self::SCREENS, true ) ) { return true; } // Matched on the end of the id: WordPress builds a submenu screen's id // from the parent's translated menu title, so only the slug is stable. $slugs = array( Send4_Admin::PAGE_SLUG, Send4_Admin::FORMS_SLUG, Send4_Admin::CONNECTION_SLUG ); foreach ( $slugs as $slug ) { $suffix = '_page_' . $slug; if ( substr( (string) $screen->id, -strlen( $suffix ) ) === $suffix ) { return true; } } return false; } /** * Print the notice, when it is due. * * @return void */ public static function render() { if ( ! self::should_show() ) { return; } $stored = self::stored(); $minimum = self::unmet(); if ( null === $stored || null === $minimum ) { return; } $message = '' !== $stored['message'] ? $stored['message'] : sprintf( /* translators: %s: the minimum plugin version Send4 asks for, e.g. 0.3.0. */ __( 'Send4 needs version %s or newer of this plugin. Update it from Dashboard → Updates. Your forms keep sending in the meantime.', 'send4' ), $minimum ); $updates_url = is_multisite() ? network_admin_url( 'update-core.php' ) : admin_url( 'update-core.php' ); $dismiss_url = wp_nonce_url( add_query_arg( 'action', self::DISMISS_ACTION, admin_url( 'admin-post.php' ) ), self::DISMISS_ACTION ); echo '
' . esc_html__( 'Send4', 'send4' ) . ': ' . esc_html( $message ) . '
' . esc_html__( 'Go to Updates', 'send4' ) . ' ' . esc_html__( 'Dismiss', 'send4' ) . '