+
+/**
+ * Autosave with heartbeat
+ *
+ * @since 3.9.0
+ *
+ * @param array $response The Heartbeat response.
+ * @param array $data The $_POST data sent.
+ * @return array The Heartbeat response.
+ */
+function heartbeat_autosave( $response, $data ) {
+ if ( ! empty( $data['wp_autosave'] ) ) {
+ $saved = wp_autosave( $data['wp_autosave'] );
+
+ if ( is_wp_error( $saved ) ) {
+ $response['wp_autosave'] = array( 'success' => false, 'message' => $saved->get_error_message() );
+ } elseif ( empty( $saved ) ) {
+ $response['wp_autosave'] = array( 'success' => false, 'message' => __( 'Error while saving.' ) );
+ } else {
+ /* translators: draft saved date format, see https://secure.php.net/date */
+ $draft_saved_date_format = __( 'g:i:s a' );
+ /* translators: %s: date and time */
+ $response['wp_autosave'] = array( 'success' => true, 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ) );
+ }
+ }
+
+ return $response;
+}
+
+/**
+ * Remove single-use URL parameters and create canonical link based on new URL.
+ *
+ * Remove specific query string parameters from a URL, create the canonical link,
+ * put it in the admin header, and change the current URL to match.
+ *
+ * @since 4.2.0
+ */
+function wp_admin_canonical_url() {
+ $removable_query_args = wp_removable_query_args();
+
+ if ( empty( $removable_query_args ) ) {
+ return;
+ }
+
+ // Ensure we're using an absolute URL.
+ $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
+ $filtered_url = remove_query_arg( $removable_query_args, $current_url );
+ ?>
+ <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
+ <script>
+ if ( window.history.replaceState ) {
+ window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
+ }
+ </script>
+<?php
+}
+
+/**
+ * Outputs JS that reloads the page if the user navigated to it with the Back or Forward button.
+ *
+ * Used on the Edit Post and Add New Post screens. Needed to ensure the page is not loaded from browser cache,
+ * so the post title and editor content are the last saved versions. Ideally this script should run first in the head.
+ *
+ * @since 4.6.0
+ */
+function wp_page_reload_on_back_button_js() {
+ ?>
+ <script>
+ if ( typeof performance !== 'undefined' && performance.navigation && performance.navigation.type === 2 ) {
+ document.location.reload( true );
+ }
+ </script>
+ <?php
+}