From a415c29efee45520ae252d2aa28f1083a521cd7b Mon Sep 17 00:00:00 2001
From: Daniel Baumann
Date: Wed, 17 Apr 2024 09:56:49 +0200
Subject: Adding upstream version 6.4.3+dfsg1.
Signed-off-by: Daniel Baumann
---
wp-admin/includes/admin-filters.php | 174 +
wp-admin/includes/admin.php | 101 +
wp-admin/includes/ajax-actions.php | 5612 +++++++++++++++++++
wp-admin/includes/bookmark.php | 379 ++
.../includes/class-automatic-upgrader-skin.php | 135 +
.../includes/class-bulk-plugin-upgrader-skin.php | 87 +
.../includes/class-bulk-theme-upgrader-skin.php | 88 +
wp-admin/includes/class-bulk-upgrader-skin.php | 187 +
wp-admin/includes/class-core-upgrader.php | 420 ++
wp-admin/includes/class-custom-background.php | 666 +++
wp-admin/includes/class-custom-image-header.php | 1617 ++++++
wp-admin/includes/class-file-upload-upgrader.php | 158 +
wp-admin/includes/class-ftp-pure.php | 186 +
wp-admin/includes/class-ftp-sockets.php | 246 +
wp-admin/includes/class-ftp.php | 913 ++++
.../includes/class-language-pack-upgrader-skin.php | 97 +
wp-admin/includes/class-language-pack-upgrader.php | 474 ++
wp-admin/includes/class-pclzip.php | 5732 ++++++++++++++++++++
wp-admin/includes/class-plugin-installer-skin.php | 349 ++
wp-admin/includes/class-plugin-upgrader-skin.php | 123 +
wp-admin/includes/class-plugin-upgrader.php | 712 +++
wp-admin/includes/class-theme-installer-skin.php | 384 ++
wp-admin/includes/class-theme-upgrader-skin.php | 144 +
wp-admin/includes/class-theme-upgrader.php | 771 +++
.../includes/class-walker-category-checklist.php | 138 +
.../includes/class-walker-nav-menu-checklist.php | 126 +
wp-admin/includes/class-walker-nav-menu-edit.php | 323 ++
wp-admin/includes/class-wp-ajax-upgrader-skin.php | 159 +
.../class-wp-application-passwords-list-table.php | 267 +
wp-admin/includes/class-wp-automatic-updater.php | 1554 ++++++
wp-admin/includes/class-wp-comments-list-table.php | 1120 ++++
wp-admin/includes/class-wp-community-events.php | 530 ++
wp-admin/includes/class-wp-debug-data.php | 1716 ++++++
wp-admin/includes/class-wp-filesystem-base.php | 864 +++
wp-admin/includes/class-wp-filesystem-direct.php | 688 +++
wp-admin/includes/class-wp-filesystem-ftpext.php | 830 +++
.../includes/class-wp-filesystem-ftpsockets.php | 718 +++
wp-admin/includes/class-wp-filesystem-ssh2.php | 834 +++
wp-admin/includes/class-wp-importer.php | 327 ++
wp-admin/includes/class-wp-internal-pointers.php | 175 +
wp-admin/includes/class-wp-links-list-table.php | 354 ++
wp-admin/includes/class-wp-list-table-compat.php | 67 +
wp-admin/includes/class-wp-list-table.php | 1874 +++++++
wp-admin/includes/class-wp-media-list-table.php | 891 +++
wp-admin/includes/class-wp-ms-sites-list-table.php | 861 +++
.../includes/class-wp-ms-themes-list-table.php | 1038 ++++
wp-admin/includes/class-wp-ms-users-list-table.php | 546 ++
.../class-wp-plugin-install-list-table.php | 831 +++
wp-admin/includes/class-wp-plugins-list-table.php | 1394 +++++
.../includes/class-wp-post-comments-list-table.php | 77 +
wp-admin/includes/class-wp-posts-list-table.php | 2119 ++++++++
...-wp-privacy-data-export-requests-list-table.php | 160 +
...wp-privacy-data-removal-requests-list-table.php | 167 +
.../includes/class-wp-privacy-policy-content.php | 706 +++
.../includes/class-wp-privacy-requests-table.php | 565 ++
wp-admin/includes/class-wp-screen.php | 1359 +++++
.../includes/class-wp-site-health-auto-updates.php | 458 ++
wp-admin/includes/class-wp-site-health.php | 3635 +++++++++++++
wp-admin/includes/class-wp-site-icon.php | 234 +
wp-admin/includes/class-wp-terms-list-table.php | 755 +++
.../includes/class-wp-theme-install-list-table.php | 570 ++
wp-admin/includes/class-wp-themes-list-table.php | 360 ++
wp-admin/includes/class-wp-upgrader-skin.php | 278 +
wp-admin/includes/class-wp-upgrader-skins.php | 44 +
wp-admin/includes/class-wp-upgrader.php | 1236 +++++
wp-admin/includes/class-wp-users-list-table.php | 683 +++
wp-admin/includes/comment.php | 219 +
wp-admin/includes/continents-cities.php | 549 ++
wp-admin/includes/credits.php | 166 +
wp-admin/includes/dashboard.php | 2137 ++++++++
wp-admin/includes/deprecated.php | 1591 ++++++
wp-admin/includes/edit-tag-messages.php | 59 +
wp-admin/includes/export.php | 686 +++
wp-admin/includes/file.php | 2804 ++++++++++
wp-admin/includes/image-edit.php | 1132 ++++
wp-admin/includes/image.php | 1166 ++++
wp-admin/includes/import.php | 232 +
wp-admin/includes/list-table.php | 108 +
wp-admin/includes/media.php | 3866 +++++++++++++
wp-admin/includes/menu.php | 383 ++
wp-admin/includes/meta-boxes.php | 1751 ++++++
wp-admin/includes/misc.php | 1644 ++++++
wp-admin/includes/ms-admin-filters.php | 41 +
wp-admin/includes/ms-deprecated.php | 140 +
wp-admin/includes/ms.php | 1172 ++++
wp-admin/includes/nav-menu.php | 1552 ++++++
wp-admin/includes/network.php | 760 +++
wp-admin/includes/noop.php | 113 +
wp-admin/includes/options.php | 136 +
wp-admin/includes/plugin-install.php | 926 ++++
wp-admin/includes/plugin.php | 2597 +++++++++
wp-admin/includes/post.php | 2637 +++++++++
wp-admin/includes/privacy-tools.php | 968 ++++
wp-admin/includes/revision.php | 473 ++
wp-admin/includes/schema.php | 1382 +++++
wp-admin/includes/screen.php | 244 +
wp-admin/includes/taxonomy.php | 316 ++
wp-admin/includes/template.php | 2821 ++++++++++
wp-admin/includes/theme-install.php | 271 +
wp-admin/includes/theme.php | 1250 +++++
wp-admin/includes/translation-install.php | 277 +
wp-admin/includes/update-core.php | 1870 +++++++
wp-admin/includes/update.php | 1165 ++++
wp-admin/includes/upgrade.php | 3671 +++++++++++++
wp-admin/includes/user.php | 756 +++
wp-admin/includes/widgets.php | 329 ++
106 files changed, 95746 insertions(+)
create mode 100644 wp-admin/includes/admin-filters.php
create mode 100644 wp-admin/includes/admin.php
create mode 100644 wp-admin/includes/ajax-actions.php
create mode 100644 wp-admin/includes/bookmark.php
create mode 100644 wp-admin/includes/class-automatic-upgrader-skin.php
create mode 100644 wp-admin/includes/class-bulk-plugin-upgrader-skin.php
create mode 100644 wp-admin/includes/class-bulk-theme-upgrader-skin.php
create mode 100644 wp-admin/includes/class-bulk-upgrader-skin.php
create mode 100644 wp-admin/includes/class-core-upgrader.php
create mode 100644 wp-admin/includes/class-custom-background.php
create mode 100644 wp-admin/includes/class-custom-image-header.php
create mode 100644 wp-admin/includes/class-file-upload-upgrader.php
create mode 100644 wp-admin/includes/class-ftp-pure.php
create mode 100644 wp-admin/includes/class-ftp-sockets.php
create mode 100644 wp-admin/includes/class-ftp.php
create mode 100644 wp-admin/includes/class-language-pack-upgrader-skin.php
create mode 100644 wp-admin/includes/class-language-pack-upgrader.php
create mode 100644 wp-admin/includes/class-pclzip.php
create mode 100644 wp-admin/includes/class-plugin-installer-skin.php
create mode 100644 wp-admin/includes/class-plugin-upgrader-skin.php
create mode 100644 wp-admin/includes/class-plugin-upgrader.php
create mode 100644 wp-admin/includes/class-theme-installer-skin.php
create mode 100644 wp-admin/includes/class-theme-upgrader-skin.php
create mode 100644 wp-admin/includes/class-theme-upgrader.php
create mode 100644 wp-admin/includes/class-walker-category-checklist.php
create mode 100644 wp-admin/includes/class-walker-nav-menu-checklist.php
create mode 100644 wp-admin/includes/class-walker-nav-menu-edit.php
create mode 100644 wp-admin/includes/class-wp-ajax-upgrader-skin.php
create mode 100644 wp-admin/includes/class-wp-application-passwords-list-table.php
create mode 100644 wp-admin/includes/class-wp-automatic-updater.php
create mode 100644 wp-admin/includes/class-wp-comments-list-table.php
create mode 100644 wp-admin/includes/class-wp-community-events.php
create mode 100644 wp-admin/includes/class-wp-debug-data.php
create mode 100644 wp-admin/includes/class-wp-filesystem-base.php
create mode 100644 wp-admin/includes/class-wp-filesystem-direct.php
create mode 100644 wp-admin/includes/class-wp-filesystem-ftpext.php
create mode 100644 wp-admin/includes/class-wp-filesystem-ftpsockets.php
create mode 100644 wp-admin/includes/class-wp-filesystem-ssh2.php
create mode 100644 wp-admin/includes/class-wp-importer.php
create mode 100644 wp-admin/includes/class-wp-internal-pointers.php
create mode 100644 wp-admin/includes/class-wp-links-list-table.php
create mode 100644 wp-admin/includes/class-wp-list-table-compat.php
create mode 100644 wp-admin/includes/class-wp-list-table.php
create mode 100644 wp-admin/includes/class-wp-media-list-table.php
create mode 100644 wp-admin/includes/class-wp-ms-sites-list-table.php
create mode 100644 wp-admin/includes/class-wp-ms-themes-list-table.php
create mode 100644 wp-admin/includes/class-wp-ms-users-list-table.php
create mode 100644 wp-admin/includes/class-wp-plugin-install-list-table.php
create mode 100644 wp-admin/includes/class-wp-plugins-list-table.php
create mode 100644 wp-admin/includes/class-wp-post-comments-list-table.php
create mode 100644 wp-admin/includes/class-wp-posts-list-table.php
create mode 100644 wp-admin/includes/class-wp-privacy-data-export-requests-list-table.php
create mode 100644 wp-admin/includes/class-wp-privacy-data-removal-requests-list-table.php
create mode 100644 wp-admin/includes/class-wp-privacy-policy-content.php
create mode 100644 wp-admin/includes/class-wp-privacy-requests-table.php
create mode 100644 wp-admin/includes/class-wp-screen.php
create mode 100644 wp-admin/includes/class-wp-site-health-auto-updates.php
create mode 100644 wp-admin/includes/class-wp-site-health.php
create mode 100644 wp-admin/includes/class-wp-site-icon.php
create mode 100644 wp-admin/includes/class-wp-terms-list-table.php
create mode 100644 wp-admin/includes/class-wp-theme-install-list-table.php
create mode 100644 wp-admin/includes/class-wp-themes-list-table.php
create mode 100644 wp-admin/includes/class-wp-upgrader-skin.php
create mode 100644 wp-admin/includes/class-wp-upgrader-skins.php
create mode 100644 wp-admin/includes/class-wp-upgrader.php
create mode 100644 wp-admin/includes/class-wp-users-list-table.php
create mode 100644 wp-admin/includes/comment.php
create mode 100644 wp-admin/includes/continents-cities.php
create mode 100644 wp-admin/includes/credits.php
create mode 100644 wp-admin/includes/dashboard.php
create mode 100644 wp-admin/includes/deprecated.php
create mode 100644 wp-admin/includes/edit-tag-messages.php
create mode 100644 wp-admin/includes/export.php
create mode 100644 wp-admin/includes/file.php
create mode 100644 wp-admin/includes/image-edit.php
create mode 100644 wp-admin/includes/image.php
create mode 100644 wp-admin/includes/import.php
create mode 100644 wp-admin/includes/list-table.php
create mode 100644 wp-admin/includes/media.php
create mode 100644 wp-admin/includes/menu.php
create mode 100644 wp-admin/includes/meta-boxes.php
create mode 100644 wp-admin/includes/misc.php
create mode 100644 wp-admin/includes/ms-admin-filters.php
create mode 100644 wp-admin/includes/ms-deprecated.php
create mode 100644 wp-admin/includes/ms.php
create mode 100644 wp-admin/includes/nav-menu.php
create mode 100644 wp-admin/includes/network.php
create mode 100644 wp-admin/includes/noop.php
create mode 100644 wp-admin/includes/options.php
create mode 100644 wp-admin/includes/plugin-install.php
create mode 100644 wp-admin/includes/plugin.php
create mode 100644 wp-admin/includes/post.php
create mode 100644 wp-admin/includes/privacy-tools.php
create mode 100644 wp-admin/includes/revision.php
create mode 100644 wp-admin/includes/schema.php
create mode 100644 wp-admin/includes/screen.php
create mode 100644 wp-admin/includes/taxonomy.php
create mode 100644 wp-admin/includes/template.php
create mode 100644 wp-admin/includes/theme-install.php
create mode 100644 wp-admin/includes/theme.php
create mode 100644 wp-admin/includes/translation-install.php
create mode 100644 wp-admin/includes/update-core.php
create mode 100644 wp-admin/includes/update.php
create mode 100644 wp-admin/includes/upgrade.php
create mode 100644 wp-admin/includes/user.php
create mode 100644 wp-admin/includes/widgets.php
(limited to 'wp-admin/includes')
diff --git a/wp-admin/includes/admin-filters.php b/wp-admin/includes/admin-filters.php
new file mode 100644
index 0000000..b5adb94
--- /dev/null
+++ b/wp-admin/includes/admin-filters.php
@@ -0,0 +1,174 @@
+id and the JS global 'pagenow'.
+ if ( ! empty( $_POST['screen_id'] ) ) {
+ $screen_id = sanitize_key( $_POST['screen_id'] );
+ } else {
+ $screen_id = 'front';
+ }
+
+ if ( ! empty( $_POST['data'] ) ) {
+ $data = wp_unslash( (array) $_POST['data'] );
+
+ /**
+ * Filters Heartbeat Ajax response in no-privilege environments.
+ *
+ * @since 3.6.0
+ *
+ * @param array $response The no-priv Heartbeat response.
+ * @param array $data The $_POST data sent.
+ * @param string $screen_id The screen ID.
+ */
+ $response = apply_filters( 'heartbeat_nopriv_received', $response, $data, $screen_id );
+ }
+
+ /**
+ * Filters Heartbeat Ajax response in no-privilege environments when no data is passed.
+ *
+ * @since 3.6.0
+ *
+ * @param array $response The no-priv Heartbeat response.
+ * @param string $screen_id The screen ID.
+ */
+ $response = apply_filters( 'heartbeat_nopriv_send', $response, $screen_id );
+
+ /**
+ * Fires when Heartbeat ticks in no-privilege environments.
+ *
+ * Allows the transport to be easily replaced with long-polling.
+ *
+ * @since 3.6.0
+ *
+ * @param array $response The no-priv Heartbeat response.
+ * @param string $screen_id The screen ID.
+ */
+ do_action( 'heartbeat_nopriv_tick', $response, $screen_id );
+
+ // Send the current time according to the server.
+ $response['server_time'] = time();
+
+ wp_send_json( $response );
+}
+
+//
+// GET-based Ajax handlers.
+//
+
+/**
+ * Handles fetching a list table via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_fetch_list() {
+ $list_class = $_GET['list_args']['class'];
+ check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' );
+
+ $wp_list_table = _get_list_table( $list_class, array( 'screen' => $_GET['list_args']['screen']['id'] ) );
+ if ( ! $wp_list_table ) {
+ wp_die( 0 );
+ }
+
+ if ( ! $wp_list_table->ajax_user_can() ) {
+ wp_die( -1 );
+ }
+
+ $wp_list_table->ajax_response();
+
+ wp_die( 0 );
+}
+
+/**
+ * Handles tag search via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_ajax_tag_search() {
+ if ( ! isset( $_GET['tax'] ) ) {
+ wp_die( 0 );
+ }
+
+ $taxonomy = sanitize_key( $_GET['tax'] );
+ $taxonomy_object = get_taxonomy( $taxonomy );
+
+ if ( ! $taxonomy_object ) {
+ wp_die( 0 );
+ }
+
+ if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) {
+ wp_die( -1 );
+ }
+
+ $search = wp_unslash( $_GET['q'] );
+
+ $comma = _x( ',', 'tag delimiter' );
+ if ( ',' !== $comma ) {
+ $search = str_replace( $comma, ',', $search );
+ }
+
+ if ( str_contains( $search, ',' ) ) {
+ $search = explode( ',', $search );
+ $search = $search[ count( $search ) - 1 ];
+ }
+
+ $search = trim( $search );
+
+ /**
+ * Filters the minimum number of characters required to fire a tag search via Ajax.
+ *
+ * @since 4.0.0
+ *
+ * @param int $characters The minimum number of characters required. Default 2.
+ * @param WP_Taxonomy $taxonomy_object The taxonomy object.
+ * @param string $search The search term.
+ */
+ $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $taxonomy_object, $search );
+
+ /*
+ * Require $term_search_min_chars chars for matching (default: 2)
+ * ensure it's a non-negative, non-zero integer.
+ */
+ if ( ( 0 == $term_search_min_chars ) || ( strlen( $search ) < $term_search_min_chars ) ) {
+ wp_die();
+ }
+
+ $results = get_terms(
+ array(
+ 'taxonomy' => $taxonomy,
+ 'name__like' => $search,
+ 'fields' => 'names',
+ 'hide_empty' => false,
+ 'number' => isset( $_GET['number'] ) ? (int) $_GET['number'] : 0,
+ )
+ );
+
+ /**
+ * Filters the Ajax term search results.
+ *
+ * @since 6.1.0
+ *
+ * @param string[] $results Array of term names.
+ * @param WP_Taxonomy $taxonomy_object The taxonomy object.
+ * @param string $search The search term.
+ */
+ $results = apply_filters( 'ajax_term_search_results', $results, $taxonomy_object, $search );
+
+ echo implode( "\n", $results );
+ wp_die();
+}
+
+/**
+ * Handles compression testing via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_wp_compression_test() {
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_die( -1 );
+ }
+
+ if ( ini_get( 'zlib.output_compression' ) || 'ob_gzhandler' === ini_get( 'output_handler' ) ) {
+ // Use `update_option()` on single site to mark the option for autoloading.
+ if ( is_multisite() ) {
+ update_site_option( 'can_compress_scripts', 0 );
+ } else {
+ update_option( 'can_compress_scripts', 0, 'yes' );
+ }
+ wp_die( 0 );
+ }
+
+ if ( isset( $_GET['test'] ) ) {
+ header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
+ header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
+ header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
+ header( 'Content-Type: application/javascript; charset=UTF-8' );
+ $force_gzip = ( defined( 'ENFORCE_GZIP' ) && ENFORCE_GZIP );
+ $test_str = '"wpCompressionTest Lorem ipsum dolor sit amet consectetuer mollis sapien urna ut a. Eu nonummy condimentum fringilla tempor pretium platea vel nibh netus Maecenas. Hac molestie amet justo quis pellentesque est ultrices interdum nibh Morbi. Cras mattis pretium Phasellus ante ipsum ipsum ut sociis Suspendisse Lorem. Ante et non molestie. Porta urna Vestibulum egestas id congue nibh eu risus gravida sit. Ac augue auctor Ut et non a elit massa id sodales. Elit eu Nulla at nibh adipiscing mattis lacus mauris at tempus. Netus nibh quis suscipit nec feugiat eget sed lorem et urna. Pellentesque lacus at ut massa consectetuer ligula ut auctor semper Pellentesque. Ut metus massa nibh quam Curabitur molestie nec mauris congue. Volutpat molestie elit justo facilisis neque ac risus Ut nascetur tristique. Vitae sit lorem tellus et quis Phasellus lacus tincidunt nunc Fusce. Pharetra wisi Suspendisse mus sagittis libero lacinia Integer consequat ac Phasellus. Et urna ac cursus tortor aliquam Aliquam amet tellus volutpat Vestibulum. Justo interdum condimentum In augue congue tellus sollicitudin Quisque quis nibh."';
+
+ if ( 1 == $_GET['test'] ) {
+ echo $test_str;
+ wp_die();
+ } elseif ( 2 == $_GET['test'] ) {
+ if ( ! isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
+ wp_die( -1 );
+ }
+
+ if ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate' ) && function_exists( 'gzdeflate' ) && ! $force_gzip ) {
+ header( 'Content-Encoding: deflate' );
+ $out = gzdeflate( $test_str, 1 );
+ } elseif ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) && function_exists( 'gzencode' ) ) {
+ header( 'Content-Encoding: gzip' );
+ $out = gzencode( $test_str, 1 );
+ } else {
+ wp_die( -1 );
+ }
+
+ echo $out;
+ wp_die();
+ } elseif ( 'no' === $_GET['test'] ) {
+ check_ajax_referer( 'update_can_compress_scripts' );
+ // Use `update_option()` on single site to mark the option for autoloading.
+ if ( is_multisite() ) {
+ update_site_option( 'can_compress_scripts', 0 );
+ } else {
+ update_option( 'can_compress_scripts', 0, 'yes' );
+ }
+ } elseif ( 'yes' === $_GET['test'] ) {
+ check_ajax_referer( 'update_can_compress_scripts' );
+ // Use `update_option()` on single site to mark the option for autoloading.
+ if ( is_multisite() ) {
+ update_site_option( 'can_compress_scripts', 1 );
+ } else {
+ update_option( 'can_compress_scripts', 1, 'yes' );
+ }
+ }
+ }
+
+ wp_die( 0 );
+}
+
+/**
+ * Handles image editor previews via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_imgedit_preview() {
+ $post_id = (int) $_GET['postid'];
+ if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
+ wp_die( -1 );
+ }
+
+ check_ajax_referer( "image_editor-$post_id" );
+
+ require_once ABSPATH . 'wp-admin/includes/image-edit.php';
+
+ if ( ! stream_preview_image( $post_id ) ) {
+ wp_die( -1 );
+ }
+
+ wp_die();
+}
+
+/**
+ * Handles oEmbed caching via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @global WP_Embed $wp_embed
+ */
+function wp_ajax_oembed_cache() {
+ $GLOBALS['wp_embed']->cache_oembed( $_GET['post'] );
+ wp_die( 0 );
+}
+
+/**
+ * Handles user autocomplete via AJAX.
+ *
+ * @since 3.4.0
+ */
+function wp_ajax_autocomplete_user() {
+ if ( ! is_multisite() || ! current_user_can( 'promote_users' ) || wp_is_large_network( 'users' ) ) {
+ wp_die( -1 );
+ }
+
+ /** This filter is documented in wp-admin/user-new.php */
+ if ( ! current_user_can( 'manage_network_users' ) && ! apply_filters( 'autocomplete_users_for_site_admins', false ) ) {
+ wp_die( -1 );
+ }
+
+ $return = array();
+
+ /*
+ * Check the type of request.
+ * Current allowed values are `add` and `search`.
+ */
+ if ( isset( $_REQUEST['autocomplete_type'] ) && 'search' === $_REQUEST['autocomplete_type'] ) {
+ $type = $_REQUEST['autocomplete_type'];
+ } else {
+ $type = 'add';
+ }
+
+ /*
+ * Check the desired field for value.
+ * Current allowed values are `user_email` and `user_login`.
+ */
+ if ( isset( $_REQUEST['autocomplete_field'] ) && 'user_email' === $_REQUEST['autocomplete_field'] ) {
+ $field = $_REQUEST['autocomplete_field'];
+ } else {
+ $field = 'user_login';
+ }
+
+ // Exclude current users of this blog.
+ if ( isset( $_REQUEST['site_id'] ) ) {
+ $id = absint( $_REQUEST['site_id'] );
+ } else {
+ $id = get_current_blog_id();
+ }
+
+ $include_blog_users = ( 'search' === $type ? get_users(
+ array(
+ 'blog_id' => $id,
+ 'fields' => 'ID',
+ )
+ ) : array() );
+
+ $exclude_blog_users = ( 'add' === $type ? get_users(
+ array(
+ 'blog_id' => $id,
+ 'fields' => 'ID',
+ )
+ ) : array() );
+
+ $users = get_users(
+ array(
+ 'blog_id' => false,
+ 'search' => '*' . $_REQUEST['term'] . '*',
+ 'include' => $include_blog_users,
+ 'exclude' => $exclude_blog_users,
+ 'search_columns' => array( 'user_login', 'user_nicename', 'user_email' ),
+ )
+ );
+
+ foreach ( $users as $user ) {
+ $return[] = array(
+ /* translators: 1: User login, 2: User email address. */
+ 'label' => sprintf( _x( '%1$s (%2$s)', 'user autocomplete result' ), $user->user_login, $user->user_email ),
+ 'value' => $user->$field,
+ );
+ }
+
+ wp_die( wp_json_encode( $return ) );
+}
+
+/**
+ * Handles Ajax requests for community events
+ *
+ * @since 4.8.0
+ */
+function wp_ajax_get_community_events() {
+ require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php';
+
+ check_ajax_referer( 'community_events' );
+
+ $search = isset( $_POST['location'] ) ? wp_unslash( $_POST['location'] ) : '';
+ $timezone = isset( $_POST['timezone'] ) ? wp_unslash( $_POST['timezone'] ) : '';
+ $user_id = get_current_user_id();
+ $saved_location = get_user_option( 'community-events-location', $user_id );
+ $events_client = new WP_Community_Events( $user_id, $saved_location );
+ $events = $events_client->get_events( $search, $timezone );
+ $ip_changed = false;
+
+ if ( is_wp_error( $events ) ) {
+ wp_send_json_error(
+ array(
+ 'error' => $events->get_error_message(),
+ )
+ );
+ } else {
+ if ( empty( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) ) {
+ $ip_changed = true;
+ } elseif ( isset( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) && $saved_location['ip'] !== $events['location']['ip'] ) {
+ $ip_changed = true;
+ }
+
+ /*
+ * The location should only be updated when it changes. The API doesn't always return
+ * a full location; sometimes it's missing the description or country. The location
+ * that was saved during the initial request is known to be good and complete, though.
+ * It should be left intact until the user explicitly changes it (either by manually
+ * searching for a new location, or by changing their IP address).
+ *
+ * If the location was updated with an incomplete response from the API, then it could
+ * break assumptions that the UI makes (e.g., that there will always be a description
+ * that corresponds to a latitude/longitude location).
+ *
+ * The location is stored network-wide, so that the user doesn't have to set it on each site.
+ */
+ if ( $ip_changed || $search ) {
+ update_user_meta( $user_id, 'community-events-location', $events['location'] );
+ }
+
+ wp_send_json_success( $events );
+ }
+}
+
+/**
+ * Handles dashboard widgets via AJAX.
+ *
+ * @since 3.4.0
+ */
+function wp_ajax_dashboard_widgets() {
+ require_once ABSPATH . 'wp-admin/includes/dashboard.php';
+
+ $pagenow = $_GET['pagenow'];
+ if ( 'dashboard-user' === $pagenow || 'dashboard-network' === $pagenow || 'dashboard' === $pagenow ) {
+ set_current_screen( $pagenow );
+ }
+
+ switch ( $_GET['widget'] ) {
+ case 'dashboard_primary':
+ wp_dashboard_primary();
+ break;
+ }
+ wp_die();
+}
+
+/**
+ * Handles Customizer preview logged-in status via AJAX.
+ *
+ * @since 3.4.0
+ */
+function wp_ajax_logged_in() {
+ wp_die( 1 );
+}
+
+//
+// Ajax helpers.
+//
+
+/**
+ * Sends back current comment total and new page links if they need to be updated.
+ *
+ * Contrary to normal success Ajax response ("1"), die with time() on success.
+ *
+ * @since 2.7.0
+ * @access private
+ *
+ * @param int $comment_id
+ * @param int $delta
+ */
+function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) {
+ $total = isset( $_POST['_total'] ) ? (int) $_POST['_total'] : 0;
+ $per_page = isset( $_POST['_per_page'] ) ? (int) $_POST['_per_page'] : 0;
+ $page = isset( $_POST['_page'] ) ? (int) $_POST['_page'] : 0;
+ $url = isset( $_POST['_url'] ) ? sanitize_url( $_POST['_url'] ) : '';
+
+ // JS didn't send us everything we need to know. Just die with success message.
+ if ( ! $total || ! $per_page || ! $page || ! $url ) {
+ $time = time();
+ $comment = get_comment( $comment_id );
+ $comment_status = '';
+ $comment_link = '';
+
+ if ( $comment ) {
+ $comment_status = $comment->comment_approved;
+ }
+
+ if ( 1 === (int) $comment_status ) {
+ $comment_link = get_comment_link( $comment );
+ }
+
+ $counts = wp_count_comments();
+
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'comment',
+ // Here for completeness - not used.
+ 'id' => $comment_id,
+ 'supplemental' => array(
+ 'status' => $comment_status,
+ 'postId' => $comment ? $comment->comment_post_ID : '',
+ 'time' => $time,
+ 'in_moderation' => $counts->moderated,
+ 'i18n_comments_text' => sprintf(
+ /* translators: %s: Number of comments. */
+ _n( '%s Comment', '%s Comments', $counts->approved ),
+ number_format_i18n( $counts->approved )
+ ),
+ 'i18n_moderation_text' => sprintf(
+ /* translators: %s: Number of comments. */
+ _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ),
+ number_format_i18n( $counts->moderated )
+ ),
+ 'comment_link' => $comment_link,
+ ),
+ )
+ );
+ $x->send();
+ }
+
+ $total += $delta;
+ if ( $total < 0 ) {
+ $total = 0;
+ }
+
+ // Only do the expensive stuff on a page-break, and about 1 other time per page.
+ if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) {
+ $post_id = 0;
+ // What type of comment count are we looking for?
+ $status = 'all';
+ $parsed = parse_url( $url );
+
+ if ( isset( $parsed['query'] ) ) {
+ parse_str( $parsed['query'], $query_vars );
+
+ if ( ! empty( $query_vars['comment_status'] ) ) {
+ $status = $query_vars['comment_status'];
+ }
+
+ if ( ! empty( $query_vars['p'] ) ) {
+ $post_id = (int) $query_vars['p'];
+ }
+
+ if ( ! empty( $query_vars['comment_type'] ) ) {
+ $type = $query_vars['comment_type'];
+ }
+ }
+
+ if ( empty( $type ) ) {
+ // Only use the comment count if not filtering by a comment_type.
+ $comment_count = wp_count_comments( $post_id );
+
+ // We're looking for a known type of comment count.
+ if ( isset( $comment_count->$status ) ) {
+ $total = $comment_count->$status;
+ }
+ }
+ // Else use the decremented value from above.
+ }
+
+ // The time since the last comment count.
+ $time = time();
+ $comment = get_comment( $comment_id );
+ $counts = wp_count_comments();
+
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'comment',
+ 'id' => $comment_id,
+ 'supplemental' => array(
+ 'status' => $comment ? $comment->comment_approved : '',
+ 'postId' => $comment ? $comment->comment_post_ID : '',
+ /* translators: %s: Number of comments. */
+ 'total_items_i18n' => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ),
+ 'total_pages' => ceil( $total / $per_page ),
+ 'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),
+ 'total' => $total,
+ 'time' => $time,
+ 'in_moderation' => $counts->moderated,
+ 'i18n_moderation_text' => sprintf(
+ /* translators: %s: Number of comments. */
+ _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ),
+ number_format_i18n( $counts->moderated )
+ ),
+ ),
+ )
+ );
+ $x->send();
+}
+
+//
+// POST-based Ajax handlers.
+//
+
+/**
+ * Handles adding a hierarchical term via AJAX.
+ *
+ * @since 3.1.0
+ * @access private
+ */
+function _wp_ajax_add_hierarchical_term() {
+ $action = $_POST['action'];
+ $taxonomy = get_taxonomy( substr( $action, 4 ) );
+ check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name );
+
+ if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) {
+ wp_die( -1 );
+ }
+
+ $names = explode( ',', $_POST[ 'new' . $taxonomy->name ] );
+ $parent = isset( $_POST[ 'new' . $taxonomy->name . '_parent' ] ) ? (int) $_POST[ 'new' . $taxonomy->name . '_parent' ] : 0;
+
+ if ( 0 > $parent ) {
+ $parent = 0;
+ }
+
+ if ( 'category' === $taxonomy->name ) {
+ $post_category = isset( $_POST['post_category'] ) ? (array) $_POST['post_category'] : array();
+ } else {
+ $post_category = ( isset( $_POST['tax_input'] ) && isset( $_POST['tax_input'][ $taxonomy->name ] ) ) ? (array) $_POST['tax_input'][ $taxonomy->name ] : array();
+ }
+
+ $checked_categories = array_map( 'absint', (array) $post_category );
+ $popular_ids = wp_popular_terms_checklist( $taxonomy->name, 0, 10, false );
+
+ foreach ( $names as $cat_name ) {
+ $cat_name = trim( $cat_name );
+ $category_nicename = sanitize_title( $cat_name );
+
+ if ( '' === $category_nicename ) {
+ continue;
+ }
+
+ $cat_id = wp_insert_term( $cat_name, $taxonomy->name, array( 'parent' => $parent ) );
+
+ if ( ! $cat_id || is_wp_error( $cat_id ) ) {
+ continue;
+ } else {
+ $cat_id = $cat_id['term_id'];
+ }
+
+ $checked_categories[] = $cat_id;
+
+ if ( $parent ) { // Do these all at once in a second.
+ continue;
+ }
+
+ ob_start();
+
+ wp_terms_checklist(
+ 0,
+ array(
+ 'taxonomy' => $taxonomy->name,
+ 'descendants_and_self' => $cat_id,
+ 'selected_cats' => $checked_categories,
+ 'popular_cats' => $popular_ids,
+ )
+ );
+
+ $data = ob_get_clean();
+
+ $add = array(
+ 'what' => $taxonomy->name,
+ 'id' => $cat_id,
+ 'data' => str_replace( array( "\n", "\t" ), '', $data ),
+ 'position' => -1,
+ );
+ }
+
+ if ( $parent ) { // Foncy - replace the parent and all its children.
+ $parent = get_term( $parent, $taxonomy->name );
+ $term_id = $parent->term_id;
+
+ while ( $parent->parent ) { // Get the top parent.
+ $parent = get_term( $parent->parent, $taxonomy->name );
+ if ( is_wp_error( $parent ) ) {
+ break;
+ }
+ $term_id = $parent->term_id;
+ }
+
+ ob_start();
+
+ wp_terms_checklist(
+ 0,
+ array(
+ 'taxonomy' => $taxonomy->name,
+ 'descendants_and_self' => $term_id,
+ 'selected_cats' => $checked_categories,
+ 'popular_cats' => $popular_ids,
+ )
+ );
+
+ $data = ob_get_clean();
+
+ $add = array(
+ 'what' => $taxonomy->name,
+ 'id' => $term_id,
+ 'data' => str_replace( array( "\n", "\t" ), '', $data ),
+ 'position' => -1,
+ );
+ }
+
+ ob_start();
+
+ wp_dropdown_categories(
+ array(
+ 'taxonomy' => $taxonomy->name,
+ 'hide_empty' => 0,
+ 'name' => 'new' . $taxonomy->name . '_parent',
+ 'orderby' => 'name',
+ 'hierarchical' => 1,
+ 'show_option_none' => '— ' . $taxonomy->labels->parent_item . ' —',
+ )
+ );
+
+ $sup = ob_get_clean();
+
+ $add['supplemental'] = array( 'newcat_parent' => $sup );
+
+ $x = new WP_Ajax_Response( $add );
+ $x->send();
+}
+
+/**
+ * Handles deleting a comment via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_delete_comment() {
+ $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
+
+ $comment = get_comment( $id );
+
+ if ( ! $comment ) {
+ wp_die( time() );
+ }
+
+ if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
+ wp_die( -1 );
+ }
+
+ check_ajax_referer( "delete-comment_$id" );
+ $status = wp_get_comment_status( $comment );
+ $delta = -1;
+
+ if ( isset( $_POST['trash'] ) && 1 == $_POST['trash'] ) {
+ if ( 'trash' === $status ) {
+ wp_die( time() );
+ }
+
+ $r = wp_trash_comment( $comment );
+ } elseif ( isset( $_POST['untrash'] ) && 1 == $_POST['untrash'] ) {
+ if ( 'trash' !== $status ) {
+ wp_die( time() );
+ }
+
+ $r = wp_untrash_comment( $comment );
+
+ // Undo trash, not in Trash.
+ if ( ! isset( $_POST['comment_status'] ) || 'trash' !== $_POST['comment_status'] ) {
+ $delta = 1;
+ }
+ } elseif ( isset( $_POST['spam'] ) && 1 == $_POST['spam'] ) {
+ if ( 'spam' === $status ) {
+ wp_die( time() );
+ }
+
+ $r = wp_spam_comment( $comment );
+ } elseif ( isset( $_POST['unspam'] ) && 1 == $_POST['unspam'] ) {
+ if ( 'spam' !== $status ) {
+ wp_die( time() );
+ }
+
+ $r = wp_unspam_comment( $comment );
+
+ // Undo spam, not in spam.
+ if ( ! isset( $_POST['comment_status'] ) || 'spam' !== $_POST['comment_status'] ) {
+ $delta = 1;
+ }
+ } elseif ( isset( $_POST['delete'] ) && 1 == $_POST['delete'] ) {
+ $r = wp_delete_comment( $comment );
+ } else {
+ wp_die( -1 );
+ }
+
+ if ( $r ) {
+ // Decide if we need to send back '1' or a more complicated response including page links and comment counts.
+ _wp_ajax_delete_comment_response( $comment->comment_ID, $delta );
+ }
+
+ wp_die( 0 );
+}
+
+/**
+ * Handles deleting a tag via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_delete_tag() {
+ $tag_id = (int) $_POST['tag_ID'];
+ check_ajax_referer( "delete-tag_$tag_id" );
+
+ if ( ! current_user_can( 'delete_term', $tag_id ) ) {
+ wp_die( -1 );
+ }
+
+ $taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag';
+ $tag = get_term( $tag_id, $taxonomy );
+
+ if ( ! $tag || is_wp_error( $tag ) ) {
+ wp_die( 1 );
+ }
+
+ if ( wp_delete_term( $tag_id, $taxonomy ) ) {
+ wp_die( 1 );
+ } else {
+ wp_die( 0 );
+ }
+}
+
+/**
+ * Handles deleting a link via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_delete_link() {
+ $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
+
+ check_ajax_referer( "delete-bookmark_$id" );
+
+ if ( ! current_user_can( 'manage_links' ) ) {
+ wp_die( -1 );
+ }
+
+ $link = get_bookmark( $id );
+ if ( ! $link || is_wp_error( $link ) ) {
+ wp_die( 1 );
+ }
+
+ if ( wp_delete_link( $id ) ) {
+ wp_die( 1 );
+ } else {
+ wp_die( 0 );
+ }
+}
+
+/**
+ * Handles deleting meta via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_delete_meta() {
+ $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
+
+ check_ajax_referer( "delete-meta_$id" );
+ $meta = get_metadata_by_mid( 'post', $id );
+
+ if ( ! $meta ) {
+ wp_die( 1 );
+ }
+
+ if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $meta->post_id, $meta->meta_key ) ) {
+ wp_die( -1 );
+ }
+
+ if ( delete_meta( $meta->meta_id ) ) {
+ wp_die( 1 );
+ }
+
+ wp_die( 0 );
+}
+
+/**
+ * Handles deleting a post via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @param string $action Action to perform.
+ */
+function wp_ajax_delete_post( $action ) {
+ if ( empty( $action ) ) {
+ $action = 'delete-post';
+ }
+
+ $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
+ check_ajax_referer( "{$action}_$id" );
+
+ if ( ! current_user_can( 'delete_post', $id ) ) {
+ wp_die( -1 );
+ }
+
+ if ( ! get_post( $id ) ) {
+ wp_die( 1 );
+ }
+
+ if ( wp_delete_post( $id ) ) {
+ wp_die( 1 );
+ } else {
+ wp_die( 0 );
+ }
+}
+
+/**
+ * Handles sending a post to the Trash via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @param string $action Action to perform.
+ */
+function wp_ajax_trash_post( $action ) {
+ if ( empty( $action ) ) {
+ $action = 'trash-post';
+ }
+
+ $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
+ check_ajax_referer( "{$action}_$id" );
+
+ if ( ! current_user_can( 'delete_post', $id ) ) {
+ wp_die( -1 );
+ }
+
+ if ( ! get_post( $id ) ) {
+ wp_die( 1 );
+ }
+
+ if ( 'trash-post' === $action ) {
+ $done = wp_trash_post( $id );
+ } else {
+ $done = wp_untrash_post( $id );
+ }
+
+ if ( $done ) {
+ wp_die( 1 );
+ }
+
+ wp_die( 0 );
+}
+
+/**
+ * Handles restoring a post from the Trash via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @param string $action Action to perform.
+ */
+function wp_ajax_untrash_post( $action ) {
+ if ( empty( $action ) ) {
+ $action = 'untrash-post';
+ }
+
+ wp_ajax_trash_post( $action );
+}
+
+/**
+ * Handles deleting a page via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @param string $action Action to perform.
+ */
+function wp_ajax_delete_page( $action ) {
+ if ( empty( $action ) ) {
+ $action = 'delete-page';
+ }
+
+ $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
+ check_ajax_referer( "{$action}_$id" );
+
+ if ( ! current_user_can( 'delete_page', $id ) ) {
+ wp_die( -1 );
+ }
+
+ if ( ! get_post( $id ) ) {
+ wp_die( 1 );
+ }
+
+ if ( wp_delete_post( $id ) ) {
+ wp_die( 1 );
+ } else {
+ wp_die( 0 );
+ }
+}
+
+/**
+ * Handles dimming a comment via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_dim_comment() {
+ $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
+ $comment = get_comment( $id );
+
+ if ( ! $comment ) {
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'comment',
+ 'id' => new WP_Error(
+ 'invalid_comment',
+ /* translators: %d: Comment ID. */
+ sprintf( __( 'Comment %d does not exist' ), $id )
+ ),
+ )
+ );
+ $x->send();
+ }
+
+ if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && ! current_user_can( 'moderate_comments' ) ) {
+ wp_die( -1 );
+ }
+
+ $current = wp_get_comment_status( $comment );
+
+ if ( isset( $_POST['new'] ) && $_POST['new'] == $current ) {
+ wp_die( time() );
+ }
+
+ check_ajax_referer( "approve-comment_$id" );
+
+ if ( in_array( $current, array( 'unapproved', 'spam' ), true ) ) {
+ $result = wp_set_comment_status( $comment, 'approve', true );
+ } else {
+ $result = wp_set_comment_status( $comment, 'hold', true );
+ }
+
+ if ( is_wp_error( $result ) ) {
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'comment',
+ 'id' => $result,
+ )
+ );
+ $x->send();
+ }
+
+ // Decide if we need to send back '1' or a more complicated response including page links and comment counts.
+ _wp_ajax_delete_comment_response( $comment->comment_ID );
+ wp_die( 0 );
+}
+
+/**
+ * Handles adding a link category via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @param string $action Action to perform.
+ */
+function wp_ajax_add_link_category( $action ) {
+ if ( empty( $action ) ) {
+ $action = 'add-link-category';
+ }
+
+ check_ajax_referer( $action );
+
+ $taxonomy_object = get_taxonomy( 'link_category' );
+
+ if ( ! current_user_can( $taxonomy_object->cap->manage_terms ) ) {
+ wp_die( -1 );
+ }
+
+ $names = explode( ',', wp_unslash( $_POST['newcat'] ) );
+ $x = new WP_Ajax_Response();
+
+ foreach ( $names as $cat_name ) {
+ $cat_name = trim( $cat_name );
+ $slug = sanitize_title( $cat_name );
+
+ if ( '' === $slug ) {
+ continue;
+ }
+
+ $cat_id = wp_insert_term( $cat_name, 'link_category' );
+
+ if ( ! $cat_id || is_wp_error( $cat_id ) ) {
+ continue;
+ } else {
+ $cat_id = $cat_id['term_id'];
+ }
+
+ $cat_name = esc_html( $cat_name );
+
+ $x->add(
+ array(
+ 'what' => 'link-category',
+ 'id' => $cat_id,
+ 'data' => " $cat_name ",
+ 'position' => -1,
+ )
+ );
+ }
+ $x->send();
+}
+
+/**
+ * Handles adding a tag via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_add_tag() {
+ check_ajax_referer( 'add-tag', '_wpnonce_add-tag' );
+
+ $taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag';
+ $taxonomy_object = get_taxonomy( $taxonomy );
+
+ if ( ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
+ wp_die( -1 );
+ }
+
+ $x = new WP_Ajax_Response();
+
+ $tag = wp_insert_term( $_POST['tag-name'], $taxonomy, $_POST );
+
+ if ( $tag && ! is_wp_error( $tag ) ) {
+ $tag = get_term( $tag['term_id'], $taxonomy );
+ }
+
+ if ( ! $tag || is_wp_error( $tag ) ) {
+ $message = __( 'An error has occurred. Please reload the page and try again.' );
+ $error_code = 'error';
+
+ if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
+ $message = $tag->get_error_message();
+ }
+
+ if ( is_wp_error( $tag ) && $tag->get_error_code() ) {
+ $error_code = $tag->get_error_code();
+ }
+
+ $x->add(
+ array(
+ 'what' => 'taxonomy',
+ 'data' => new WP_Error( $error_code, $message ),
+ )
+ );
+ $x->send();
+ }
+
+ $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => $_POST['screen'] ) );
+
+ $level = 0;
+ $noparents = '';
+
+ if ( is_taxonomy_hierarchical( $taxonomy ) ) {
+ $level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) );
+ ob_start();
+ $wp_list_table->single_row( $tag, $level );
+ $noparents = ob_get_clean();
+ }
+
+ ob_start();
+ $wp_list_table->single_row( $tag );
+ $parents = ob_get_clean();
+
+ require ABSPATH . 'wp-admin/includes/edit-tag-messages.php';
+
+ $message = '';
+ if ( isset( $messages[ $taxonomy_object->name ][1] ) ) {
+ $message = $messages[ $taxonomy_object->name ][1];
+ } elseif ( isset( $messages['_item'][1] ) ) {
+ $message = $messages['_item'][1];
+ }
+
+ $x->add(
+ array(
+ 'what' => 'taxonomy',
+ 'data' => $message,
+ 'supplemental' => array(
+ 'parents' => $parents,
+ 'noparents' => $noparents,
+ 'notice' => $message,
+ ),
+ )
+ );
+
+ $x->add(
+ array(
+ 'what' => 'term',
+ 'position' => $level,
+ 'supplemental' => (array) $tag,
+ )
+ );
+
+ $x->send();
+}
+
+/**
+ * Handles getting a tagcloud via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_get_tagcloud() {
+ if ( ! isset( $_POST['tax'] ) ) {
+ wp_die( 0 );
+ }
+
+ $taxonomy = sanitize_key( $_POST['tax'] );
+ $taxonomy_object = get_taxonomy( $taxonomy );
+
+ if ( ! $taxonomy_object ) {
+ wp_die( 0 );
+ }
+
+ if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) {
+ wp_die( -1 );
+ }
+
+ $tags = get_terms(
+ array(
+ 'taxonomy' => $taxonomy,
+ 'number' => 45,
+ 'orderby' => 'count',
+ 'order' => 'DESC',
+ )
+ );
+
+ if ( empty( $tags ) ) {
+ wp_die( $taxonomy_object->labels->not_found );
+ }
+
+ if ( is_wp_error( $tags ) ) {
+ wp_die( $tags->get_error_message() );
+ }
+
+ foreach ( $tags as $key => $tag ) {
+ $tags[ $key ]->link = '#';
+ $tags[ $key ]->id = $tag->term_id;
+ }
+
+ // We need raw tag names here, so don't filter the output.
+ $return = wp_generate_tag_cloud(
+ $tags,
+ array(
+ 'filter' => 0,
+ 'format' => 'list',
+ )
+ );
+
+ if ( empty( $return ) ) {
+ wp_die( 0 );
+ }
+
+ echo $return;
+ wp_die();
+}
+
+/**
+ * Handles getting comments via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @global int $post_id
+ *
+ * @param string $action Action to perform.
+ */
+function wp_ajax_get_comments( $action ) {
+ global $post_id;
+
+ if ( empty( $action ) ) {
+ $action = 'get-comments';
+ }
+
+ check_ajax_referer( $action );
+
+ if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) {
+ $id = absint( $_REQUEST['p'] );
+ if ( ! empty( $id ) ) {
+ $post_id = $id;
+ }
+ }
+
+ if ( empty( $post_id ) ) {
+ wp_die( -1 );
+ }
+
+ $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
+
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
+ wp_die( -1 );
+ }
+
+ $wp_list_table->prepare_items();
+
+ if ( ! $wp_list_table->has_items() ) {
+ wp_die( 1 );
+ }
+
+ $x = new WP_Ajax_Response();
+
+ ob_start();
+ foreach ( $wp_list_table->items as $comment ) {
+ if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && 0 === $comment->comment_approved ) {
+ continue;
+ }
+ get_comment( $comment );
+ $wp_list_table->single_row( $comment );
+ }
+ $comment_list_item = ob_get_clean();
+
+ $x->add(
+ array(
+ 'what' => 'comments',
+ 'data' => $comment_list_item,
+ )
+ );
+
+ $x->send();
+}
+
+/**
+ * Handles replying to a comment via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @param string $action Action to perform.
+ */
+function wp_ajax_replyto_comment( $action ) {
+ if ( empty( $action ) ) {
+ $action = 'replyto-comment';
+ }
+
+ check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
+
+ $comment_post_id = (int) $_POST['comment_post_ID'];
+ $post = get_post( $comment_post_id );
+
+ if ( ! $post ) {
+ wp_die( -1 );
+ }
+
+ if ( ! current_user_can( 'edit_post', $comment_post_id ) ) {
+ wp_die( -1 );
+ }
+
+ if ( empty( $post->post_status ) ) {
+ wp_die( 1 );
+ } elseif ( in_array( $post->post_status, array( 'draft', 'pending', 'trash' ), true ) ) {
+ wp_die( __( 'You cannot reply to a comment on a draft post.' ) );
+ }
+
+ $user = wp_get_current_user();
+
+ if ( $user->exists() ) {
+ $comment_author = wp_slash( $user->display_name );
+ $comment_author_email = wp_slash( $user->user_email );
+ $comment_author_url = wp_slash( $user->user_url );
+ $user_id = $user->ID;
+
+ if ( current_user_can( 'unfiltered_html' ) ) {
+ if ( ! isset( $_POST['_wp_unfiltered_html_comment'] ) ) {
+ $_POST['_wp_unfiltered_html_comment'] = '';
+ }
+
+ if ( wp_create_nonce( 'unfiltered-html-comment' ) != $_POST['_wp_unfiltered_html_comment'] ) {
+ kses_remove_filters(); // Start with a clean slate.
+ kses_init_filters(); // Set up the filters.
+ remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
+ add_filter( 'pre_comment_content', 'wp_filter_kses' );
+ }
+ }
+ } else {
+ wp_die( __( 'Sorry, you must be logged in to reply to a comment.' ) );
+ }
+
+ $comment_content = trim( $_POST['content'] );
+
+ if ( '' === $comment_content ) {
+ wp_die( __( 'Please type your comment text.' ) );
+ }
+
+ $comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : 'comment';
+
+ $comment_parent = 0;
+
+ if ( isset( $_POST['comment_ID'] ) ) {
+ $comment_parent = absint( $_POST['comment_ID'] );
+ }
+
+ $comment_auto_approved = false;
+
+ $commentdata = array(
+ 'comment_post_ID' => $comment_post_id,
+ );
+
+ $commentdata += compact(
+ 'comment_author',
+ 'comment_author_email',
+ 'comment_author_url',
+ 'comment_content',
+ 'comment_type',
+ 'comment_parent',
+ 'user_id'
+ );
+
+ // Automatically approve parent comment.
+ if ( ! empty( $_POST['approve_parent'] ) ) {
+ $parent = get_comment( $comment_parent );
+
+ if ( $parent && '0' === $parent->comment_approved && $parent->comment_post_ID == $comment_post_id ) {
+ if ( ! current_user_can( 'edit_comment', $parent->comment_ID ) ) {
+ wp_die( -1 );
+ }
+
+ if ( wp_set_comment_status( $parent, 'approve' ) ) {
+ $comment_auto_approved = true;
+ }
+ }
+ }
+
+ $comment_id = wp_new_comment( $commentdata );
+
+ if ( is_wp_error( $comment_id ) ) {
+ wp_die( $comment_id->get_error_message() );
+ }
+
+ $comment = get_comment( $comment_id );
+
+ if ( ! $comment ) {
+ wp_die( 1 );
+ }
+
+ $position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1';
+
+ ob_start();
+ if ( isset( $_REQUEST['mode'] ) && 'dashboard' === $_REQUEST['mode'] ) {
+ require_once ABSPATH . 'wp-admin/includes/dashboard.php';
+ _wp_dashboard_recent_comments_row( $comment );
+ } else {
+ if ( isset( $_REQUEST['mode'] ) && 'single' === $_REQUEST['mode'] ) {
+ $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
+ } else {
+ $wp_list_table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
+ }
+ $wp_list_table->single_row( $comment );
+ }
+ $comment_list_item = ob_get_clean();
+
+ $response = array(
+ 'what' => 'comment',
+ 'id' => $comment->comment_ID,
+ 'data' => $comment_list_item,
+ 'position' => $position,
+ );
+
+ $counts = wp_count_comments();
+ $response['supplemental'] = array(
+ 'in_moderation' => $counts->moderated,
+ 'i18n_comments_text' => sprintf(
+ /* translators: %s: Number of comments. */
+ _n( '%s Comment', '%s Comments', $counts->approved ),
+ number_format_i18n( $counts->approved )
+ ),
+ 'i18n_moderation_text' => sprintf(
+ /* translators: %s: Number of comments. */
+ _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ),
+ number_format_i18n( $counts->moderated )
+ ),
+ );
+
+ if ( $comment_auto_approved ) {
+ $response['supplemental']['parent_approved'] = $parent->comment_ID;
+ $response['supplemental']['parent_post_id'] = $parent->comment_post_ID;
+ }
+
+ $x = new WP_Ajax_Response();
+ $x->add( $response );
+ $x->send();
+}
+
+/**
+ * Handles editing a comment via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_edit_comment() {
+ check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
+
+ $comment_id = (int) $_POST['comment_ID'];
+
+ if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
+ wp_die( -1 );
+ }
+
+ if ( '' === $_POST['content'] ) {
+ wp_die( __( 'Please type your comment text.' ) );
+ }
+
+ if ( isset( $_POST['status'] ) ) {
+ $_POST['comment_status'] = $_POST['status'];
+ }
+
+ $updated = edit_comment();
+ if ( is_wp_error( $updated ) ) {
+ wp_die( $updated->get_error_message() );
+ }
+
+ $position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1';
+ $checkbox = ( isset( $_POST['checkbox'] ) && true == $_POST['checkbox'] ) ? 1 : 0;
+ $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
+
+ $comment = get_comment( $comment_id );
+
+ if ( empty( $comment->comment_ID ) ) {
+ wp_die( -1 );
+ }
+
+ ob_start();
+ $wp_list_table->single_row( $comment );
+ $comment_list_item = ob_get_clean();
+
+ $x = new WP_Ajax_Response();
+
+ $x->add(
+ array(
+ 'what' => 'edit_comment',
+ 'id' => $comment->comment_ID,
+ 'data' => $comment_list_item,
+ 'position' => $position,
+ )
+ );
+
+ $x->send();
+}
+
+/**
+ * Handles adding a menu item via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_add_menu_item() {
+ check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_die( -1 );
+ }
+
+ require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
+
+ /*
+ * For performance reasons, we omit some object properties from the checklist.
+ * The following is a hacky way to restore them when adding non-custom items.
+ */
+ $menu_items_data = array();
+
+ foreach ( (array) $_POST['menu-item'] as $menu_item_data ) {
+ if (
+ ! empty( $menu_item_data['menu-item-type'] ) &&
+ 'custom' !== $menu_item_data['menu-item-type'] &&
+ ! empty( $menu_item_data['menu-item-object-id'] )
+ ) {
+ switch ( $menu_item_data['menu-item-type'] ) {
+ case 'post_type':
+ $_object = get_post( $menu_item_data['menu-item-object-id'] );
+ break;
+
+ case 'post_type_archive':
+ $_object = get_post_type_object( $menu_item_data['menu-item-object'] );
+ break;
+
+ case 'taxonomy':
+ $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] );
+ break;
+ }
+
+ $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) );
+ $_menu_item = reset( $_menu_items );
+
+ // Restore the missing menu item properties.
+ $menu_item_data['menu-item-description'] = $_menu_item->description;
+ }
+
+ $menu_items_data[] = $menu_item_data;
+ }
+
+ $item_ids = wp_save_nav_menu_items( 0, $menu_items_data );
+ if ( is_wp_error( $item_ids ) ) {
+ wp_die( 0 );
+ }
+
+ $menu_items = array();
+
+ foreach ( (array) $item_ids as $menu_item_id ) {
+ $menu_obj = get_post( $menu_item_id );
+
+ if ( ! empty( $menu_obj->ID ) ) {
+ $menu_obj = wp_setup_nav_menu_item( $menu_obj );
+ $menu_obj->title = empty( $menu_obj->title ) ? __( 'Menu Item' ) : $menu_obj->title;
+ $menu_obj->label = $menu_obj->title; // Don't show "(pending)" in ajax-added items.
+ $menu_items[] = $menu_obj;
+ }
+ }
+
+ /** This filter is documented in wp-admin/includes/nav-menu.php */
+ $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu'] );
+
+ if ( ! class_exists( $walker_class_name ) ) {
+ wp_die( 0 );
+ }
+
+ if ( ! empty( $menu_items ) ) {
+ $args = array(
+ 'after' => '',
+ 'before' => '',
+ 'link_after' => '',
+ 'link_before' => '',
+ 'walker' => new $walker_class_name(),
+ );
+
+ echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
+ }
+
+ wp_die();
+}
+
+/**
+ * Handles adding meta via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_add_meta() {
+ check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' );
+ $c = 0;
+ $pid = (int) $_POST['post_id'];
+ $post = get_post( $pid );
+
+ if ( isset( $_POST['metakeyselect'] ) || isset( $_POST['metakeyinput'] ) ) {
+ if ( ! current_user_can( 'edit_post', $pid ) ) {
+ wp_die( -1 );
+ }
+
+ if ( isset( $_POST['metakeyselect'] ) && '#NONE#' === $_POST['metakeyselect'] && empty( $_POST['metakeyinput'] ) ) {
+ wp_die( 1 );
+ }
+
+ // If the post is an autodraft, save the post as a draft and then attempt to save the meta.
+ if ( 'auto-draft' === $post->post_status ) {
+ $post_data = array();
+ $post_data['action'] = 'draft'; // Warning fix.
+ $post_data['post_ID'] = $pid;
+ $post_data['post_type'] = $post->post_type;
+ $post_data['post_status'] = 'draft';
+ $now = time();
+ /* translators: 1: Post creation date, 2: Post creation time. */
+ $post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), gmdate( __( 'F j, Y' ), $now ), gmdate( __( 'g:i a' ), $now ) );
+
+ $pid = edit_post( $post_data );
+
+ if ( $pid ) {
+ if ( is_wp_error( $pid ) ) {
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'meta',
+ 'data' => $pid,
+ )
+ );
+ $x->send();
+ }
+
+ $mid = add_meta( $pid );
+ if ( ! $mid ) {
+ wp_die( __( 'Please provide a custom field value.' ) );
+ }
+ } else {
+ wp_die( 0 );
+ }
+ } else {
+ $mid = add_meta( $pid );
+ if ( ! $mid ) {
+ wp_die( __( 'Please provide a custom field value.' ) );
+ }
+ }
+
+ $meta = get_metadata_by_mid( 'post', $mid );
+ $pid = (int) $meta->post_id;
+ $meta = get_object_vars( $meta );
+
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'meta',
+ 'id' => $mid,
+ 'data' => _list_meta_row( $meta, $c ),
+ 'position' => 1,
+ 'supplemental' => array( 'postid' => $pid ),
+ )
+ );
+ } else { // Update?
+ $mid = (int) key( $_POST['meta'] );
+ $key = wp_unslash( $_POST['meta'][ $mid ]['key'] );
+ $value = wp_unslash( $_POST['meta'][ $mid ]['value'] );
+
+ if ( '' === trim( $key ) ) {
+ wp_die( __( 'Please provide a custom field name.' ) );
+ }
+
+ $meta = get_metadata_by_mid( 'post', $mid );
+
+ if ( ! $meta ) {
+ wp_die( 0 ); // If meta doesn't exist.
+ }
+
+ if (
+ is_protected_meta( $meta->meta_key, 'post' ) || is_protected_meta( $key, 'post' ) ||
+ ! current_user_can( 'edit_post_meta', $meta->post_id, $meta->meta_key ) ||
+ ! current_user_can( 'edit_post_meta', $meta->post_id, $key )
+ ) {
+ wp_die( -1 );
+ }
+
+ if ( $meta->meta_value != $value || $meta->meta_key != $key ) {
+ $u = update_metadata_by_mid( 'post', $mid, $value, $key );
+ if ( ! $u ) {
+ wp_die( 0 ); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems).
+ }
+ }
+
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'meta',
+ 'id' => $mid,
+ 'old_id' => $mid,
+ 'data' => _list_meta_row(
+ array(
+ 'meta_key' => $key,
+ 'meta_value' => $value,
+ 'meta_id' => $mid,
+ ),
+ $c
+ ),
+ 'position' => 0,
+ 'supplemental' => array( 'postid' => $meta->post_id ),
+ )
+ );
+ }
+ $x->send();
+}
+
+/**
+ * Handles adding a user via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @param string $action Action to perform.
+ */
+function wp_ajax_add_user( $action ) {
+ if ( empty( $action ) ) {
+ $action = 'add-user';
+ }
+
+ check_ajax_referer( $action );
+
+ if ( ! current_user_can( 'create_users' ) ) {
+ wp_die( -1 );
+ }
+
+ $user_id = edit_user();
+
+ if ( ! $user_id ) {
+ wp_die( 0 );
+ } elseif ( is_wp_error( $user_id ) ) {
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'user',
+ 'id' => $user_id,
+ )
+ );
+ $x->send();
+ }
+
+ $user_object = get_userdata( $user_id );
+ $wp_list_table = _get_list_table( 'WP_Users_List_Table' );
+
+ $role = current( $user_object->roles );
+
+ $x = new WP_Ajax_Response(
+ array(
+ 'what' => 'user',
+ 'id' => $user_id,
+ 'data' => $wp_list_table->single_row( $user_object, '', $role ),
+ 'supplemental' => array(
+ 'show-link' => sprintf(
+ /* translators: %s: The new user. */
+ __( 'User %s added' ),
+ '' . $user_object->user_login . ' '
+ ),
+ 'role' => $role,
+ ),
+ )
+ );
+ $x->send();
+}
+
+/**
+ * Handles closed post boxes via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_closed_postboxes() {
+ check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
+ $closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed'] ) : array();
+ $closed = array_filter( $closed );
+
+ $hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array();
+ $hidden = array_filter( $hidden );
+
+ $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
+
+ if ( sanitize_key( $page ) != $page ) {
+ wp_die( 0 );
+ }
+
+ $user = wp_get_current_user();
+ if ( ! $user ) {
+ wp_die( -1 );
+ }
+
+ if ( is_array( $closed ) ) {
+ update_user_meta( $user->ID, "closedpostboxes_$page", $closed );
+ }
+
+ if ( is_array( $hidden ) ) {
+ // Postboxes that are always shown.
+ $hidden = array_diff( $hidden, array( 'submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu' ) );
+ update_user_meta( $user->ID, "metaboxhidden_$page", $hidden );
+ }
+
+ wp_die( 1 );
+}
+
+/**
+ * Handles hidden columns via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_hidden_columns() {
+ check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );
+ $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
+
+ if ( sanitize_key( $page ) != $page ) {
+ wp_die( 0 );
+ }
+
+ $user = wp_get_current_user();
+ if ( ! $user ) {
+ wp_die( -1 );
+ }
+
+ $hidden = ! empty( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array();
+ update_user_meta( $user->ID, "manage{$page}columnshidden", $hidden );
+
+ wp_die( 1 );
+}
+
+/**
+ * Handles updating whether to display the welcome panel via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_update_welcome_panel() {
+ check_ajax_referer( 'welcome-panel-nonce', 'welcomepanelnonce' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_die( -1 );
+ }
+
+ update_user_meta( get_current_user_id(), 'show_welcome_panel', empty( $_POST['visible'] ) ? 0 : 1 );
+
+ wp_die( 1 );
+}
+
+/**
+ * Handles for retrieving menu meta boxes via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_menu_get_metabox() {
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_die( -1 );
+ }
+
+ require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
+
+ if ( isset( $_POST['item-type'] ) && 'post_type' === $_POST['item-type'] ) {
+ $type = 'posttype';
+ $callback = 'wp_nav_menu_item_post_type_meta_box';
+ $items = (array) get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
+ } elseif ( isset( $_POST['item-type'] ) && 'taxonomy' === $_POST['item-type'] ) {
+ $type = 'taxonomy';
+ $callback = 'wp_nav_menu_item_taxonomy_meta_box';
+ $items = (array) get_taxonomies( array( 'show_ui' => true ), 'object' );
+ }
+
+ if ( ! empty( $_POST['item-object'] ) && isset( $items[ $_POST['item-object'] ] ) ) {
+ $menus_meta_box_object = $items[ $_POST['item-object'] ];
+
+ /** This filter is documented in wp-admin/includes/nav-menu.php */
+ $item = apply_filters( 'nav_menu_meta_box_object', $menus_meta_box_object );
+
+ $box_args = array(
+ 'id' => 'add-' . $item->name,
+ 'title' => $item->labels->name,
+ 'callback' => $callback,
+ 'args' => $item,
+ );
+
+ ob_start();
+ $callback( null, $box_args );
+
+ $markup = ob_get_clean();
+
+ echo wp_json_encode(
+ array(
+ 'replace-id' => $type . '-' . $item->name,
+ 'markup' => $markup,
+ )
+ );
+ }
+
+ wp_die();
+}
+
+/**
+ * Handles internal linking via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_wp_link_ajax() {
+ check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' );
+
+ $args = array();
+
+ if ( isset( $_POST['search'] ) ) {
+ $args['s'] = wp_unslash( $_POST['search'] );
+ }
+
+ if ( isset( $_POST['term'] ) ) {
+ $args['s'] = wp_unslash( $_POST['term'] );
+ }
+
+ $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
+
+ if ( ! class_exists( '_WP_Editors', false ) ) {
+ require ABSPATH . WPINC . '/class-wp-editor.php';
+ }
+
+ $results = _WP_Editors::wp_link_query( $args );
+
+ if ( ! isset( $results ) ) {
+ wp_die( 0 );
+ }
+
+ echo wp_json_encode( $results );
+ echo "\n";
+
+ wp_die();
+}
+
+/**
+ * Handles saving menu locations via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_menu_locations_save() {
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_die( -1 );
+ }
+
+ check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
+
+ if ( ! isset( $_POST['menu-locations'] ) ) {
+ wp_die( 0 );
+ }
+
+ set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) );
+ wp_die( 1 );
+}
+
+/**
+ * Handles saving the meta box order via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_meta_box_order() {
+ check_ajax_referer( 'meta-box-order' );
+ $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false;
+ $page_columns = isset( $_POST['page_columns'] ) ? $_POST['page_columns'] : 'auto';
+
+ if ( 'auto' !== $page_columns ) {
+ $page_columns = (int) $page_columns;
+ }
+
+ $page = isset( $_POST['page'] ) ? $_POST['page'] : '';
+
+ if ( sanitize_key( $page ) != $page ) {
+ wp_die( 0 );
+ }
+
+ $user = wp_get_current_user();
+ if ( ! $user ) {
+ wp_die( -1 );
+ }
+
+ if ( $order ) {
+ update_user_meta( $user->ID, "meta-box-order_$page", $order );
+ }
+
+ if ( $page_columns ) {
+ update_user_meta( $user->ID, "screen_layout_$page", $page_columns );
+ }
+
+ wp_send_json_success();
+}
+
+/**
+ * Handles menu quick searching via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_menu_quick_search() {
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_die( -1 );
+ }
+
+ require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
+
+ _wp_ajax_menu_quick_search( $_POST );
+
+ wp_die();
+}
+
+/**
+ * Handles retrieving a permalink via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_get_permalink() {
+ check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
+ $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
+ wp_die( get_preview_post_link( $post_id ) );
+}
+
+/**
+ * Handles retrieving a sample permalink via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_sample_permalink() {
+ check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
+ $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
+ $title = isset( $_POST['new_title'] ) ? $_POST['new_title'] : '';
+ $slug = isset( $_POST['new_slug'] ) ? $_POST['new_slug'] : null;
+ wp_die( get_sample_permalink_html( $post_id, $title, $slug ) );
+}
+
+/**
+ * Handles Quick Edit saving a post from a list table via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @global string $mode List table view mode.
+ */
+function wp_ajax_inline_save() {
+ global $mode;
+
+ check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
+
+ if ( ! isset( $_POST['post_ID'] ) || ! (int) $_POST['post_ID'] ) {
+ wp_die();
+ }
+
+ $post_id = (int) $_POST['post_ID'];
+
+ if ( 'page' === $_POST['post_type'] ) {
+ if ( ! current_user_can( 'edit_page', $post_id ) ) {
+ wp_die( __( 'Sorry, you are not allowed to edit this page.' ) );
+ }
+ } else {
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
+ wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
+ }
+ }
+
+ $last = wp_check_post_lock( $post_id );
+ if ( $last ) {
+ $last_user = get_userdata( $last );
+ $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
+
+ /* translators: %s: User's display name. */
+ $msg_template = __( 'Saving is disabled: %s is currently editing this post.' );
+
+ if ( 'page' === $_POST['post_type'] ) {
+ /* translators: %s: User's display name. */
+ $msg_template = __( 'Saving is disabled: %s is currently editing this page.' );
+ }
+
+ printf( $msg_template, esc_html( $last_user_name ) );
+ wp_die();
+ }
+
+ $data = &$_POST;
+
+ $post = get_post( $post_id, ARRAY_A );
+
+ // Since it's coming from the database.
+ $post = wp_slash( $post );
+
+ $data['content'] = $post['post_content'];
+ $data['excerpt'] = $post['post_excerpt'];
+
+ // Rename.
+ $data['user_ID'] = get_current_user_id();
+
+ if ( isset( $data['post_parent'] ) ) {
+ $data['parent_id'] = $data['post_parent'];
+ }
+
+ // Status.
+ if ( isset( $data['keep_private'] ) && 'private' === $data['keep_private'] ) {
+ $data['visibility'] = 'private';
+ $data['post_status'] = 'private';
+ } else {
+ $data['post_status'] = $data['_status'];
+ }
+
+ if ( empty( $data['comment_status'] ) ) {
+ $data['comment_status'] = 'closed';
+ }
+
+ if ( empty( $data['ping_status'] ) ) {
+ $data['ping_status'] = 'closed';
+ }
+
+ // Exclude terms from taxonomies that are not supposed to appear in Quick Edit.
+ if ( ! empty( $data['tax_input'] ) ) {
+ foreach ( $data['tax_input'] as $taxonomy => $terms ) {
+ $tax_object = get_taxonomy( $taxonomy );
+ /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */
+ if ( ! apply_filters( 'quick_edit_show_taxonomy', $tax_object->show_in_quick_edit, $taxonomy, $post['post_type'] ) ) {
+ unset( $data['tax_input'][ $taxonomy ] );
+ }
+ }
+ }
+
+ // Hack: wp_unique_post_slug() doesn't work for drafts, so we will fake that our post is published.
+ if ( ! empty( $data['post_name'] ) && in_array( $post['post_status'], array( 'draft', 'pending' ), true ) ) {
+ $post['post_status'] = 'publish';
+ $data['post_name'] = wp_unique_post_slug( $data['post_name'], $post['ID'], $post['post_status'], $post['post_type'], $post['post_parent'] );
+ }
+
+ // Update the post.
+ edit_post();
+
+ $wp_list_table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => $_POST['screen'] ) );
+
+ $mode = 'excerpt' === $_POST['post_view'] ? 'excerpt' : 'list';
+
+ $level = 0;
+ if ( is_post_type_hierarchical( $wp_list_table->screen->post_type ) ) {
+ $request_post = array( get_post( $_POST['post_ID'] ) );
+ $parent = $request_post[0]->post_parent;
+
+ while ( $parent > 0 ) {
+ $parent_post = get_post( $parent );
+ $parent = $parent_post->post_parent;
+ ++$level;
+ }
+ }
+
+ $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ), $level );
+
+ wp_die();
+}
+
+/**
+ * Handles Quick Edit saving for a term via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_inline_save_tax() {
+ check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
+
+ $taxonomy = sanitize_key( $_POST['taxonomy'] );
+ $taxonomy_object = get_taxonomy( $taxonomy );
+
+ if ( ! $taxonomy_object ) {
+ wp_die( 0 );
+ }
+
+ if ( ! isset( $_POST['tax_ID'] ) || ! (int) $_POST['tax_ID'] ) {
+ wp_die( -1 );
+ }
+
+ $id = (int) $_POST['tax_ID'];
+
+ if ( ! current_user_can( 'edit_term', $id ) ) {
+ wp_die( -1 );
+ }
+
+ $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => 'edit-' . $taxonomy ) );
+
+ $tag = get_term( $id, $taxonomy );
+ $_POST['description'] = $tag->description;
+
+ $updated = wp_update_term( $id, $taxonomy, $_POST );
+
+ if ( $updated && ! is_wp_error( $updated ) ) {
+ $tag = get_term( $updated['term_id'], $taxonomy );
+ if ( ! $tag || is_wp_error( $tag ) ) {
+ if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
+ wp_die( $tag->get_error_message() );
+ }
+ wp_die( __( 'Item not updated.' ) );
+ }
+ } else {
+ if ( is_wp_error( $updated ) && $updated->get_error_message() ) {
+ wp_die( $updated->get_error_message() );
+ }
+ wp_die( __( 'Item not updated.' ) );
+ }
+
+ $level = 0;
+ $parent = $tag->parent;
+
+ while ( $parent > 0 ) {
+ $parent_tag = get_term( $parent, $taxonomy );
+ $parent = $parent_tag->parent;
+ ++$level;
+ }
+
+ $wp_list_table->single_row( $tag, $level );
+ wp_die();
+}
+
+/**
+ * Handles querying posts for the Find Posts modal via AJAX.
+ *
+ * @see window.findPosts
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_find_posts() {
+ check_ajax_referer( 'find-posts' );
+
+ $post_types = get_post_types( array( 'public' => true ), 'objects' );
+ unset( $post_types['attachment'] );
+
+ $args = array(
+ 'post_type' => array_keys( $post_types ),
+ 'post_status' => 'any',
+ 'posts_per_page' => 50,
+ );
+
+ $search = wp_unslash( $_POST['ps'] );
+
+ if ( '' !== $search ) {
+ $args['s'] = $search;
+ }
+
+ $posts = get_posts( $args );
+
+ if ( ! $posts ) {
+ wp_send_json_error( __( 'No items found.' ) );
+ }
+
+ $html = '';
+
+ wp_send_json_success( $html );
+}
+
+/**
+ * Handles saving the widgets order via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_widgets_order() {
+ check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_die( -1 );
+ }
+
+ unset( $_POST['savewidgets'], $_POST['action'] );
+
+ // Save widgets order for all sidebars.
+ if ( is_array( $_POST['sidebars'] ) ) {
+ $sidebars = array();
+
+ foreach ( wp_unslash( $_POST['sidebars'] ) as $key => $val ) {
+ $sb = array();
+
+ if ( ! empty( $val ) ) {
+ $val = explode( ',', $val );
+
+ foreach ( $val as $k => $v ) {
+ if ( ! str_contains( $v, 'widget-' ) ) {
+ continue;
+ }
+
+ $sb[ $k ] = substr( $v, strpos( $v, '_' ) + 1 );
+ }
+ }
+ $sidebars[ $key ] = $sb;
+ }
+
+ wp_set_sidebars_widgets( $sidebars );
+ wp_die( 1 );
+ }
+
+ wp_die( -1 );
+}
+
+/**
+ * Handles saving a widget via AJAX.
+ *
+ * @since 3.1.0
+ *
+ * @global array $wp_registered_widgets
+ * @global array $wp_registered_widget_controls
+ * @global array $wp_registered_widget_updates
+ */
+function wp_ajax_save_widget() {
+ global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;
+
+ check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['id_base'] ) ) {
+ wp_die( -1 );
+ }
+
+ unset( $_POST['savewidgets'], $_POST['action'] );
+
+ /**
+ * Fires early when editing the widgets displayed in sidebars.
+ *
+ * @since 2.8.0
+ */
+ do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
+
+ /**
+ * Fires early when editing the widgets displayed in sidebars.
+ *
+ * @since 2.8.0
+ */
+ do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
+
+ /** This action is documented in wp-admin/widgets.php */
+ do_action( 'sidebar_admin_setup' );
+
+ $id_base = wp_unslash( $_POST['id_base'] );
+ $widget_id = wp_unslash( $_POST['widget-id'] );
+ $sidebar_id = $_POST['sidebar'];
+ $multi_number = ! empty( $_POST['multi_number'] ) ? (int) $_POST['multi_number'] : 0;
+ $settings = isset( $_POST[ 'widget-' . $id_base ] ) && is_array( $_POST[ 'widget-' . $id_base ] ) ? $_POST[ 'widget-' . $id_base ] : false;
+ $error = '' . __( 'An error has occurred. Please reload the page and try again.' ) . '
';
+
+ $sidebars = wp_get_sidebars_widgets();
+ $sidebar = isset( $sidebars[ $sidebar_id ] ) ? $sidebars[ $sidebar_id ] : array();
+
+ // Delete.
+ if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) {
+
+ if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
+ wp_die( $error );
+ }
+
+ $sidebar = array_diff( $sidebar, array( $widget_id ) );
+ $_POST = array(
+ 'sidebar' => $sidebar_id,
+ 'widget-' . $id_base => array(),
+ 'the-widget-id' => $widget_id,
+ 'delete_widget' => '1',
+ );
+
+ /** This action is documented in wp-admin/widgets.php */
+ do_action( 'delete_widget', $widget_id, $sidebar_id, $id_base );
+
+ } elseif ( $settings && preg_match( '/__i__|%i%/', key( $settings ) ) ) {
+ if ( ! $multi_number ) {
+ wp_die( $error );
+ }
+
+ $_POST[ 'widget-' . $id_base ] = array( $multi_number => reset( $settings ) );
+ $widget_id = $id_base . '-' . $multi_number;
+ $sidebar[] = $widget_id;
+ }
+ $_POST['widget-id'] = $sidebar;
+
+ foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
+
+ if ( $name == $id_base ) {
+ if ( ! is_callable( $control['callback'] ) ) {
+ continue;
+ }
+
+ ob_start();
+ call_user_func_array( $control['callback'], $control['params'] );
+ ob_end_clean();
+ break;
+ }
+ }
+
+ if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) {
+ $sidebars[ $sidebar_id ] = $sidebar;
+ wp_set_sidebars_widgets( $sidebars );
+ echo "deleted:$widget_id";
+ wp_die();
+ }
+
+ if ( ! empty( $_POST['add_new'] ) ) {
+ wp_die();
+ }
+
+ $form = $wp_registered_widget_controls[ $widget_id ];
+ if ( $form ) {
+ call_user_func_array( $form['callback'], $form['params'] );
+ }
+
+ wp_die();
+}
+
+/**
+ * Handles updating a widget via AJAX.
+ *
+ * @since 3.9.0
+ *
+ * @global WP_Customize_Manager $wp_customize
+ */
+function wp_ajax_update_widget() {
+ global $wp_customize;
+ $wp_customize->widgets->wp_ajax_update_widget();
+}
+
+/**
+ * Handles removing inactive widgets via AJAX.
+ *
+ * @since 4.4.0
+ */
+function wp_ajax_delete_inactive_widgets() {
+ check_ajax_referer( 'remove-inactive-widgets', 'removeinactivewidgets' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_die( -1 );
+ }
+
+ unset( $_POST['removeinactivewidgets'], $_POST['action'] );
+ /** This action is documented in wp-admin/includes/ajax-actions.php */
+ do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
+ /** This action is documented in wp-admin/includes/ajax-actions.php */
+ do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
+ /** This action is documented in wp-admin/widgets.php */
+ do_action( 'sidebar_admin_setup' );
+
+ $sidebars_widgets = wp_get_sidebars_widgets();
+
+ foreach ( $sidebars_widgets['wp_inactive_widgets'] as $key => $widget_id ) {
+ $pieces = explode( '-', $widget_id );
+ $multi_number = array_pop( $pieces );
+ $id_base = implode( '-', $pieces );
+ $widget = get_option( 'widget_' . $id_base );
+ unset( $widget[ $multi_number ] );
+ update_option( 'widget_' . $id_base, $widget );
+ unset( $sidebars_widgets['wp_inactive_widgets'][ $key ] );
+ }
+
+ wp_set_sidebars_widgets( $sidebars_widgets );
+
+ wp_die();
+}
+
+/**
+ * Handles creating missing image sub-sizes for just uploaded images via AJAX.
+ *
+ * @since 5.3.0
+ */
+function wp_ajax_media_create_image_subsizes() {
+ check_ajax_referer( 'media-form' );
+
+ if ( ! current_user_can( 'upload_files' ) ) {
+ wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to upload files.' ) ) );
+ }
+
+ if ( empty( $_POST['attachment_id'] ) ) {
+ wp_send_json_error( array( 'message' => __( 'Upload failed. Please reload and try again.' ) ) );
+ }
+
+ $attachment_id = (int) $_POST['attachment_id'];
+
+ if ( ! empty( $_POST['_wp_upload_failed_cleanup'] ) ) {
+ // Upload failed. Cleanup.
+ if ( wp_attachment_is_image( $attachment_id ) && current_user_can( 'delete_post', $attachment_id ) ) {
+ $attachment = get_post( $attachment_id );
+
+ // Created at most 10 min ago.
+ if ( $attachment && ( time() - strtotime( $attachment->post_date_gmt ) < 600 ) ) {
+ wp_delete_attachment( $attachment_id, true );
+ wp_send_json_success();
+ }
+ }
+ }
+
+ /*
+ * Set a custom header with the attachment_id.
+ * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
+ */
+ if ( ! headers_sent() ) {
+ header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
+ }
+
+ /*
+ * This can still be pretty slow and cause timeout or out of memory errors.
+ * The js that handles the response would need to also handle HTTP 500 errors.
+ */
+ wp_update_image_subsizes( $attachment_id );
+
+ if ( ! empty( $_POST['_legacy_support'] ) ) {
+ // The old (inline) uploader. Only needs the attachment_id.
+ $response = array( 'id' => $attachment_id );
+ } else {
+ // Media modal and Media Library grid view.
+ $response = wp_prepare_attachment_for_js( $attachment_id );
+
+ if ( ! $response ) {
+ wp_send_json_error( array( 'message' => __( 'Upload failed.' ) ) );
+ }
+ }
+
+ // At this point the image has been uploaded successfully.
+ wp_send_json_success( $response );
+}
+
+/**
+ * Handles uploading attachments via AJAX.
+ *
+ * @since 3.3.0
+ */
+function wp_ajax_upload_attachment() {
+ check_ajax_referer( 'media-form' );
+ /*
+ * This function does not use wp_send_json_success() / wp_send_json_error()
+ * as the html4 Plupload handler requires a text/html Content-Type for older IE.
+ * See https://core.trac.wordpress.org/ticket/31037
+ */
+
+ if ( ! current_user_can( 'upload_files' ) ) {
+ echo wp_json_encode(
+ array(
+ 'success' => false,
+ 'data' => array(
+ 'message' => __( 'Sorry, you are not allowed to upload files.' ),
+ 'filename' => esc_html( $_FILES['async-upload']['name'] ),
+ ),
+ )
+ );
+
+ wp_die();
+ }
+
+ if ( isset( $_REQUEST['post_id'] ) ) {
+ $post_id = $_REQUEST['post_id'];
+
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
+ echo wp_json_encode(
+ array(
+ 'success' => false,
+ 'data' => array(
+ 'message' => __( 'Sorry, you are not allowed to attach files to this post.' ),
+ 'filename' => esc_html( $_FILES['async-upload']['name'] ),
+ ),
+ )
+ );
+
+ wp_die();
+ }
+ } else {
+ $post_id = null;
+ }
+
+ $post_data = ! empty( $_REQUEST['post_data'] ) ? _wp_get_allowed_postdata( _wp_translate_postdata( false, (array) $_REQUEST['post_data'] ) ) : array();
+
+ if ( is_wp_error( $post_data ) ) {
+ wp_die( $post_data->get_error_message() );
+ }
+
+ // If the context is custom header or background, make sure the uploaded file is an image.
+ if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ), true ) ) {
+ $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'] );
+
+ if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
+ echo wp_json_encode(
+ array(
+ 'success' => false,
+ 'data' => array(
+ 'message' => __( 'The uploaded file is not a valid image. Please try again.' ),
+ 'filename' => esc_html( $_FILES['async-upload']['name'] ),
+ ),
+ )
+ );
+
+ wp_die();
+ }
+ }
+
+ $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data );
+
+ if ( is_wp_error( $attachment_id ) ) {
+ echo wp_json_encode(
+ array(
+ 'success' => false,
+ 'data' => array(
+ 'message' => $attachment_id->get_error_message(),
+ 'filename' => esc_html( $_FILES['async-upload']['name'] ),
+ ),
+ )
+ );
+
+ wp_die();
+ }
+
+ if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) {
+ if ( 'custom-background' === $post_data['context'] ) {
+ update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', $post_data['theme'] );
+ }
+
+ if ( 'custom-header' === $post_data['context'] ) {
+ update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', $post_data['theme'] );
+ }
+ }
+
+ $attachment = wp_prepare_attachment_for_js( $attachment_id );
+ if ( ! $attachment ) {
+ wp_die();
+ }
+
+ echo wp_json_encode(
+ array(
+ 'success' => true,
+ 'data' => $attachment,
+ )
+ );
+
+ wp_die();
+}
+
+/**
+ * Handles image editing via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_image_editor() {
+ $attachment_id = (int) $_POST['postid'];
+
+ if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) {
+ wp_die( -1 );
+ }
+
+ check_ajax_referer( "image_editor-$attachment_id" );
+ require_once ABSPATH . 'wp-admin/includes/image-edit.php';
+
+ $msg = false;
+
+ switch ( $_POST['do'] ) {
+ case 'save':
+ $msg = wp_save_image( $attachment_id );
+ if ( ! empty( $msg->error ) ) {
+ wp_send_json_error( $msg );
+ }
+
+ wp_send_json_success( $msg );
+ break;
+ case 'scale':
+ $msg = wp_save_image( $attachment_id );
+ break;
+ case 'restore':
+ $msg = wp_restore_image( $attachment_id );
+ break;
+ }
+
+ ob_start();
+ wp_image_editor( $attachment_id, $msg );
+ $html = ob_get_clean();
+
+ if ( ! empty( $msg->error ) ) {
+ wp_send_json_error(
+ array(
+ 'message' => $msg,
+ 'html' => $html,
+ )
+ );
+ }
+
+ wp_send_json_success(
+ array(
+ 'message' => $msg,
+ 'html' => $html,
+ )
+ );
+}
+
+/**
+ * Handles setting the featured image via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_set_post_thumbnail() {
+ $json = ! empty( $_REQUEST['json'] ); // New-style request.
+
+ $post_id = (int) $_POST['post_id'];
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
+ wp_die( -1 );
+ }
+
+ $thumbnail_id = (int) $_POST['thumbnail_id'];
+
+ if ( $json ) {
+ check_ajax_referer( "update-post_$post_id" );
+ } else {
+ check_ajax_referer( "set_post_thumbnail-$post_id" );
+ }
+
+ if ( '-1' == $thumbnail_id ) {
+ if ( delete_post_thumbnail( $post_id ) ) {
+ $return = _wp_post_thumbnail_html( null, $post_id );
+ $json ? wp_send_json_success( $return ) : wp_die( $return );
+ } else {
+ wp_die( 0 );
+ }
+ }
+
+ if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) {
+ $return = _wp_post_thumbnail_html( $thumbnail_id, $post_id );
+ $json ? wp_send_json_success( $return ) : wp_die( $return );
+ }
+
+ wp_die( 0 );
+}
+
+/**
+ * Handles retrieving HTML for the featured image via AJAX.
+ *
+ * @since 4.6.0
+ */
+function wp_ajax_get_post_thumbnail_html() {
+ $post_id = (int) $_POST['post_id'];
+
+ check_ajax_referer( "update-post_$post_id" );
+
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
+ wp_die( -1 );
+ }
+
+ $thumbnail_id = (int) $_POST['thumbnail_id'];
+
+ // For backward compatibility, -1 refers to no featured image.
+ if ( -1 === $thumbnail_id ) {
+ $thumbnail_id = null;
+ }
+
+ $return = _wp_post_thumbnail_html( $thumbnail_id, $post_id );
+ wp_send_json_success( $return );
+}
+
+/**
+ * Handles setting the featured image for an attachment via AJAX.
+ *
+ * @since 4.0.0
+ *
+ * @see set_post_thumbnail()
+ */
+function wp_ajax_set_attachment_thumbnail() {
+ if ( empty( $_POST['urls'] ) || ! is_array( $_POST['urls'] ) ) {
+ wp_send_json_error();
+ }
+
+ $thumbnail_id = (int) $_POST['thumbnail_id'];
+ if ( empty( $thumbnail_id ) ) {
+ wp_send_json_error();
+ }
+
+ if ( false === check_ajax_referer( 'set-attachment-thumbnail', '_ajax_nonce', false ) ) {
+ wp_send_json_error();
+ }
+
+ $post_ids = array();
+ // For each URL, try to find its corresponding post ID.
+ foreach ( $_POST['urls'] as $url ) {
+ $post_id = attachment_url_to_postid( $url );
+ if ( ! empty( $post_id ) ) {
+ $post_ids[] = $post_id;
+ }
+ }
+
+ if ( empty( $post_ids ) ) {
+ wp_send_json_error();
+ }
+
+ $success = 0;
+ // For each found attachment, set its thumbnail.
+ foreach ( $post_ids as $post_id ) {
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
+ continue;
+ }
+
+ if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) {
+ ++$success;
+ }
+ }
+
+ if ( 0 === $success ) {
+ wp_send_json_error();
+ } else {
+ wp_send_json_success();
+ }
+
+ wp_send_json_error();
+}
+
+/**
+ * Handles formatting a date via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_date_format() {
+ wp_die( date_i18n( sanitize_option( 'date_format', wp_unslash( $_POST['date'] ) ) ) );
+}
+
+/**
+ * Handles formatting a time via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_time_format() {
+ wp_die( date_i18n( sanitize_option( 'time_format', wp_unslash( $_POST['date'] ) ) ) );
+}
+
+/**
+ * Handles saving posts from the fullscreen editor via AJAX.
+ *
+ * @since 3.1.0
+ * @deprecated 4.3.0
+ */
+function wp_ajax_wp_fullscreen_save_post() {
+ $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0;
+
+ $post = null;
+
+ if ( $post_id ) {
+ $post = get_post( $post_id );
+ }
+
+ check_ajax_referer( 'update-post_' . $post_id, '_wpnonce' );
+
+ $post_id = edit_post();
+
+ if ( is_wp_error( $post_id ) ) {
+ wp_send_json_error();
+ }
+
+ if ( $post ) {
+ $last_date = mysql2date( __( 'F j, Y' ), $post->post_modified );
+ $last_time = mysql2date( __( 'g:i a' ), $post->post_modified );
+ } else {
+ $last_date = date_i18n( __( 'F j, Y' ) );
+ $last_time = date_i18n( __( 'g:i a' ) );
+ }
+
+ $last_id = get_post_meta( $post_id, '_edit_last', true );
+ if ( $last_id ) {
+ $last_user = get_userdata( $last_id );
+ /* translators: 1: User's display name, 2: Date of last edit, 3: Time of last edit. */
+ $last_edited = sprintf( __( 'Last edited by %1$s on %2$s at %3$s' ), esc_html( $last_user->display_name ), $last_date, $last_time );
+ } else {
+ /* translators: 1: Date of last edit, 2: Time of last edit. */
+ $last_edited = sprintf( __( 'Last edited on %1$s at %2$s' ), $last_date, $last_time );
+ }
+
+ wp_send_json_success( array( 'last_edited' => $last_edited ) );
+}
+
+/**
+ * Handles removing a post lock via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_wp_remove_post_lock() {
+ if ( empty( $_POST['post_ID'] ) || empty( $_POST['active_post_lock'] ) ) {
+ wp_die( 0 );
+ }
+
+ $post_id = (int) $_POST['post_ID'];
+ $post = get_post( $post_id );
+
+ if ( ! $post ) {
+ wp_die( 0 );
+ }
+
+ check_ajax_referer( 'update-post_' . $post_id );
+
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
+ wp_die( -1 );
+ }
+
+ $active_lock = array_map( 'absint', explode( ':', $_POST['active_post_lock'] ) );
+
+ if ( get_current_user_id() != $active_lock[1] ) {
+ wp_die( 0 );
+ }
+
+ /**
+ * Filters the post lock window duration.
+ *
+ * @since 3.3.0
+ *
+ * @param int $interval The interval in seconds the post lock duration
+ * should last, plus 5 seconds. Default 150.
+ */
+ $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', 150 ) + 5 ) . ':' . $active_lock[1];
+ update_post_meta( $post_id, '_edit_lock', $new_lock, implode( ':', $active_lock ) );
+ wp_die( 1 );
+}
+
+/**
+ * Handles dismissing a WordPress pointer via AJAX.
+ *
+ * @since 3.1.0
+ */
+function wp_ajax_dismiss_wp_pointer() {
+ $pointer = $_POST['pointer'];
+
+ if ( sanitize_key( $pointer ) != $pointer ) {
+ wp_die( 0 );
+ }
+
+ // check_ajax_referer( 'dismiss-pointer_' . $pointer );
+
+ $dismissed = array_filter( explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ) );
+
+ if ( in_array( $pointer, $dismissed, true ) ) {
+ wp_die( 0 );
+ }
+
+ $dismissed[] = $pointer;
+ $dismissed = implode( ',', $dismissed );
+
+ update_user_meta( get_current_user_id(), 'dismissed_wp_pointers', $dismissed );
+ wp_die( 1 );
+}
+
+/**
+ * Handles getting an attachment via AJAX.
+ *
+ * @since 3.5.0
+ */
+function wp_ajax_get_attachment() {
+ if ( ! isset( $_REQUEST['id'] ) ) {
+ wp_send_json_error();
+ }
+
+ $id = absint( $_REQUEST['id'] );
+ if ( ! $id ) {
+ wp_send_json_error();
+ }
+
+ $post = get_post( $id );
+ if ( ! $post ) {
+ wp_send_json_error();
+ }
+
+ if ( 'attachment' !== $post->post_type ) {
+ wp_send_json_error();
+ }
+
+ if ( ! current_user_can( 'upload_files' ) ) {
+ wp_send_json_error();
+ }
+
+ $attachment = wp_prepare_attachment_for_js( $id );
+ if ( ! $attachment ) {
+ wp_send_json_error();
+ }
+
+ wp_send_json_success( $attachment );
+}
+
+/**
+ * Handles querying attachments via AJAX.
+ *
+ * @since 3.5.0
+ */
+function wp_ajax_query_attachments() {
+ if ( ! current_user_can( 'upload_files' ) ) {
+ wp_send_json_error();
+ }
+
+ $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
+ $keys = array(
+ 's',
+ 'order',
+ 'orderby',
+ 'posts_per_page',
+ 'paged',
+ 'post_mime_type',
+ 'post_parent',
+ 'author',
+ 'post__in',
+ 'post__not_in',
+ 'year',
+ 'monthnum',
+ );
+
+ foreach ( get_taxonomies_for_attachments( 'objects' ) as $t ) {
+ if ( $t->query_var && isset( $query[ $t->query_var ] ) ) {
+ $keys[] = $t->query_var;
+ }
+ }
+
+ $query = array_intersect_key( $query, array_flip( $keys ) );
+ $query['post_type'] = 'attachment';
+
+ if (
+ MEDIA_TRASH &&
+ ! empty( $_REQUEST['query']['post_status'] ) &&
+ 'trash' === $_REQUEST['query']['post_status']
+ ) {
+ $query['post_status'] = 'trash';
+ } else {
+ $query['post_status'] = 'inherit';
+ }
+
+ if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) {
+ $query['post_status'] .= ',private';
+ }
+
+ // Filter query clauses to include filenames.
+ if ( isset( $query['s'] ) ) {
+ add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
+ }
+
+ /**
+ * Filters the arguments passed to WP_Query during an Ajax
+ * call for querying attachments.
+ *
+ * @since 3.7.0
+ *
+ * @see WP_Query::parse_query()
+ *
+ * @param array $query An array of query variables.
+ */
+ $query = apply_filters( 'ajax_query_attachments_args', $query );
+ $attachments_query = new WP_Query( $query );
+ update_post_parent_caches( $attachments_query->posts );
+
+ $posts = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts );
+ $posts = array_filter( $posts );
+ $total_posts = $attachments_query->found_posts;
+
+ if ( $total_posts < 1 ) {
+ // Out-of-bounds, run the query again without LIMIT for total count.
+ unset( $query['paged'] );
+
+ $count_query = new WP_Query();
+ $count_query->query( $query );
+ $total_posts = $count_query->found_posts;
+ }
+
+ $posts_per_page = (int) $attachments_query->get( 'posts_per_page' );
+
+ $max_pages = $posts_per_page ? ceil( $total_posts / $posts_per_page ) : 0;
+
+ header( 'X-WP-Total: ' . (int) $total_posts );
+ header( 'X-WP-TotalPages: ' . (int) $max_pages );
+
+ wp_send_json_success( $posts );
+}
+
+/**
+ * Handles updating attachment attributes via AJAX.
+ *
+ * @since 3.5.0
+ */
+function wp_ajax_save_attachment() {
+ if ( ! isset( $_REQUEST['id'] ) || ! isset( $_REQUEST['changes'] ) ) {
+ wp_send_json_error();
+ }
+
+ $id = absint( $_REQUEST['id'] );
+ if ( ! $id ) {
+ wp_send_json_error();
+ }
+
+ check_ajax_referer( 'update-post_' . $id, 'nonce' );
+
+ if ( ! current_user_can( 'edit_post', $id ) ) {
+ wp_send_json_error();
+ }
+
+ $changes = $_REQUEST['changes'];
+ $post = get_post( $id, ARRAY_A );
+
+ if ( 'attachment' !== $post['post_type'] ) {
+ wp_send_json_error();
+ }
+
+ if ( isset( $changes['parent'] ) ) {
+ $post['post_parent'] = $changes['parent'];
+ }
+
+ if ( isset( $changes['title'] ) ) {
+ $post['post_title'] = $changes['title'];
+ }
+
+ if ( isset( $changes['caption'] ) ) {
+ $post['post_excerpt'] = $changes['caption'];
+ }
+
+ if ( isset( $changes['description'] ) ) {
+ $post['post_content'] = $changes['description'];
+ }
+
+ if ( MEDIA_TRASH && isset( $changes['status'] ) ) {
+ $post['post_status'] = $changes['status'];
+ }
+
+ if ( isset( $changes['alt'] ) ) {
+ $alt = wp_unslash( $changes['alt'] );
+ if ( get_post_meta( $id, '_wp_attachment_image_alt', true ) !== $alt ) {
+ $alt = wp_strip_all_tags( $alt, true );
+ update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) );
+ }
+ }
+
+ if ( wp_attachment_is( 'audio', $post['ID'] ) ) {
+ $changed = false;
+ $id3data = wp_get_attachment_metadata( $post['ID'] );
+
+ if ( ! is_array( $id3data ) ) {
+ $changed = true;
+ $id3data = array();
+ }
+
+ foreach ( wp_get_attachment_id3_keys( (object) $post, 'edit' ) as $key => $label ) {
+ if ( isset( $changes[ $key ] ) ) {
+ $changed = true;
+ $id3data[ $key ] = sanitize_text_field( wp_unslash( $changes[ $key ] ) );
+ }
+ }
+
+ if ( $changed ) {
+ wp_update_attachment_metadata( $id, $id3data );
+ }
+ }
+
+ if ( MEDIA_TRASH && isset( $changes['status'] ) && 'trash' === $changes['status'] ) {
+ wp_delete_post( $id );
+ } else {
+ wp_update_post( $post );
+ }
+
+ wp_send_json_success();
+}
+
+/**
+ * Handles saving backward compatible attachment attributes via AJAX.
+ *
+ * @since 3.5.0
+ */
+function wp_ajax_save_attachment_compat() {
+ if ( ! isset( $_REQUEST['id'] ) ) {
+ wp_send_json_error();
+ }
+
+ $id = absint( $_REQUEST['id'] );
+ if ( ! $id ) {
+ wp_send_json_error();
+ }
+
+ if ( empty( $_REQUEST['attachments'] ) || empty( $_REQUEST['attachments'][ $id ] ) ) {
+ wp_send_json_error();
+ }
+
+ $attachment_data = $_REQUEST['attachments'][ $id ];
+
+ check_ajax_referer( 'update-post_' . $id, 'nonce' );
+
+ if ( ! current_user_can( 'edit_post', $id ) ) {
+ wp_send_json_error();
+ }
+
+ $post = get_post( $id, ARRAY_A );
+
+ if ( 'attachment' !== $post['post_type'] ) {
+ wp_send_json_error();
+ }
+
+ /** This filter is documented in wp-admin/includes/media.php */
+ $post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data );
+
+ if ( isset( $post['errors'] ) ) {
+ $errors = $post['errors']; // @todo return me and display me!
+ unset( $post['errors'] );
+ }
+
+ wp_update_post( $post );
+
+ foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
+ if ( isset( $attachment_data[ $taxonomy ] ) ) {
+ wp_set_object_terms( $id, array_map( 'trim', preg_split( '/,+/', $attachment_data[ $taxonomy ] ) ), $taxonomy, false );
+ }
+ }
+
+ $attachment = wp_prepare_attachment_for_js( $id );
+
+ if ( ! $attachment ) {
+ wp_send_json_error();
+ }
+
+ wp_send_json_success( $attachment );
+}
+
+/**
+ * Handles saving the attachment order via AJAX.
+ *
+ * @since 3.5.0
+ */
+function wp_ajax_save_attachment_order() {
+ if ( ! isset( $_REQUEST['post_id'] ) ) {
+ wp_send_json_error();
+ }
+
+ $post_id = absint( $_REQUEST['post_id'] );
+ if ( ! $post_id ) {
+ wp_send_json_error();
+ }
+
+ if ( empty( $_REQUEST['attachments'] ) ) {
+ wp_send_json_error();
+ }
+
+ check_ajax_referer( 'update-post_' . $post_id, 'nonce' );
+
+ $attachments = $_REQUEST['attachments'];
+
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
+ wp_send_json_error();
+ }
+
+ foreach ( $attachments as $attachment_id => $menu_order ) {
+ if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
+ continue;
+ }
+
+ $attachment = get_post( $attachment_id );
+
+ if ( ! $attachment ) {
+ continue;
+ }
+
+ if ( 'attachment' !== $attachment->post_type ) {
+ continue;
+ }
+
+ wp_update_post(
+ array(
+ 'ID' => $attachment_id,
+ 'menu_order' => $menu_order,
+ )
+ );
+ }
+
+ wp_send_json_success();
+}
+
+/**
+ * Handles sending an attachment to the editor via AJAX.
+ *
+ * Generates the HTML to send an attachment to the editor.
+ * Backward compatible with the {@see 'media_send_to_editor'} filter
+ * and the chain of filters that follow.
+ *
+ * @since 3.5.0
+ */
+function wp_ajax_send_attachment_to_editor() {
+ check_ajax_referer( 'media-send-to-editor', 'nonce' );
+
+ $attachment = wp_unslash( $_POST['attachment'] );
+
+ $id = (int) $attachment['id'];
+
+ $post = get_post( $id );
+ if ( ! $post ) {
+ wp_send_json_error();
+ }
+
+ if ( 'attachment' !== $post->post_type ) {
+ wp_send_json_error();
+ }
+
+ if ( current_user_can( 'edit_post', $id ) ) {
+ // If this attachment is unattached, attach it. Primarily a back compat thing.
+ $insert_into_post_id = (int) $_POST['post_id'];
+
+ if ( 0 == $post->post_parent && $insert_into_post_id ) {
+ wp_update_post(
+ array(
+ 'ID' => $id,
+ 'post_parent' => $insert_into_post_id,
+ )
+ );
+ }
+ }
+
+ $url = empty( $attachment['url'] ) ? '' : $attachment['url'];
+ $rel = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $id ) === $url );
+
+ remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );
+
+ if ( str_starts_with( $post->post_mime_type, 'image' ) ) {
+ $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none';
+ $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
+ $alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
+
+ // No whitespace-only captions.
+ $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
+ if ( '' === trim( $caption ) ) {
+ $caption = '';
+ }
+
+ $title = ''; // We no longer insert title tags into tags, as they are redundant.
+ $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt );
+ } elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post ) ) {
+ $html = stripslashes_deep( $_POST['html'] );
+ } else {
+ $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
+ $rel = $rel ? ' rel="attachment wp-att-' . $id . '"' : ''; // Hard-coded string, $id is already sanitized.
+
+ if ( ! empty( $url ) ) {
+ $html = '' . $html . ' ';
+ }
+ }
+
+ /** This filter is documented in wp-admin/includes/media.php */
+ $html = apply_filters( 'media_send_to_editor', $html, $id, $attachment );
+
+ wp_send_json_success( $html );
+}
+
+/**
+ * Handles sending a link to the editor via AJAX.
+ *
+ * Generates the HTML to send a non-image embed link to the editor.
+ *
+ * Backward compatible with the following filters:
+ * - file_send_to_editor_url
+ * - audio_send_to_editor_url
+ * - video_send_to_editor_url
+ *
+ * @since 3.5.0
+ *
+ * @global WP_Post $post Global post object.
+ * @global WP_Embed $wp_embed
+ */
+function wp_ajax_send_link_to_editor() {
+ global $post, $wp_embed;
+
+ check_ajax_referer( 'media-send-to-editor', 'nonce' );
+
+ $src = wp_unslash( $_POST['src'] );
+ if ( ! $src ) {
+ wp_send_json_error();
+ }
+
+ if ( ! strpos( $src, '://' ) ) {
+ $src = 'http://' . $src;
+ }
+
+ $src = sanitize_url( $src );
+ if ( ! $src ) {
+ wp_send_json_error();
+ }
+
+ $link_text = trim( wp_unslash( $_POST['link_text'] ) );
+ if ( ! $link_text ) {
+ $link_text = wp_basename( $src );
+ }
+
+ $post = get_post( isset( $_POST['post_id'] ) ? $_POST['post_id'] : 0 );
+
+ // Ping WordPress for an embed.
+ $check_embed = $wp_embed->run_shortcode( '[embed]' . $src . '[/embed]' );
+
+ // Fallback that WordPress creates when no oEmbed was found.
+ $fallback = $wp_embed->maybe_make_link( $src );
+
+ if ( $check_embed !== $fallback ) {
+ // TinyMCE view for [embed] will parse this.
+ $html = '[embed]' . $src . '[/embed]';
+ } elseif ( $link_text ) {
+ $html = '' . $link_text . ' ';
+ } else {
+ $html = '';
+ }
+
+ // Figure out what filter to run:
+ $type = 'file';
+ $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src );
+ if ( $ext ) {
+ $ext_type = wp_ext2type( $ext );
+ if ( 'audio' === $ext_type || 'video' === $ext_type ) {
+ $type = $ext_type;
+ }
+ }
+
+ /** This filter is documented in wp-admin/includes/media.php */
+ $html = apply_filters( "{$type}_send_to_editor_url", $html, $src, $link_text );
+
+ wp_send_json_success( $html );
+}
+
+/**
+ * Handles the Heartbeat API via AJAX.
+ *
+ * Runs when the user is logged in.
+ *
+ * @since 3.6.0
+ */
+function wp_ajax_heartbeat() {
+ if ( empty( $_POST['_nonce'] ) ) {
+ wp_send_json_error();
+ }
+
+ $response = array();
+ $data = array();
+ $nonce_state = wp_verify_nonce( $_POST['_nonce'], 'heartbeat-nonce' );
+
+ // 'screen_id' is the same as $current_screen->id and the JS global 'pagenow'.
+ if ( ! empty( $_POST['screen_id'] ) ) {
+ $screen_id = sanitize_key( $_POST['screen_id'] );
+ } else {
+ $screen_id = 'front';
+ }
+
+ if ( ! empty( $_POST['data'] ) ) {
+ $data = wp_unslash( (array) $_POST['data'] );
+ }
+
+ if ( 1 !== $nonce_state ) {
+ /**
+ * Filters the nonces to send to the New/Edit Post screen.
+ *
+ * @since 4.3.0
+ *
+ * @param array $response The Heartbeat response.
+ * @param array $data The $_POST data sent.
+ * @param string $screen_id The screen ID.
+ */
+ $response = apply_filters( 'wp_refresh_nonces', $response, $data, $screen_id );
+
+ if ( false === $nonce_state ) {
+ // User is logged in but nonces have expired.
+ $response['nonces_expired'] = true;
+ wp_send_json( $response );
+ }
+ }
+
+ if ( ! empty( $data ) ) {
+ /**
+ * Filters the Heartbeat response received.
+ *
+ * @since 3.6.0
+ *
+ * @param array $response The Heartbeat response.
+ * @param array $data The $_POST data sent.
+ * @param string $screen_id The screen ID.
+ */
+ $response = apply_filters( 'heartbeat_received', $response, $data, $screen_id );
+ }
+
+ /**
+ * Filters the Heartbeat response sent.
+ *
+ * @since 3.6.0
+ *
+ * @param array $response The Heartbeat response.
+ * @param string $screen_id The screen ID.
+ */
+ $response = apply_filters( 'heartbeat_send', $response, $screen_id );
+
+ /**
+ * Fires when Heartbeat ticks in logged-in environments.
+ *
+ * Allows the transport to be easily replaced with long-polling.
+ *
+ * @since 3.6.0
+ *
+ * @param array $response The Heartbeat response.
+ * @param string $screen_id The screen ID.
+ */
+ do_action( 'heartbeat_tick', $response, $screen_id );
+
+ // Send the current time according to the server.
+ $response['server_time'] = time();
+
+ wp_send_json( $response );
+}
+
+/**
+ * Handles getting revision diffs via AJAX.
+ *
+ * @since 3.6.0
+ */
+function wp_ajax_get_revision_diffs() {
+ require ABSPATH . 'wp-admin/includes/revision.php';
+
+ $post = get_post( (int) $_REQUEST['post_id'] );
+ if ( ! $post ) {
+ wp_send_json_error();
+ }
+
+ if ( ! current_user_can( 'edit_post', $post->ID ) ) {
+ wp_send_json_error();
+ }
+
+ // Really just pre-loading the cache here.
+ $revisions = wp_get_post_revisions( $post->ID, array( 'check_enabled' => false ) );
+ if ( ! $revisions ) {
+ wp_send_json_error();
+ }
+
+ $return = array();
+
+ if ( function_exists( 'set_time_limit' ) ) {
+ set_time_limit( 0 );
+ }
+
+ foreach ( $_REQUEST['compare'] as $compare_key ) {
+ list( $compare_from, $compare_to ) = explode( ':', $compare_key ); // from:to
+
+ $return[] = array(
+ 'id' => $compare_key,
+ 'fields' => wp_get_revision_ui_diff( $post, $compare_from, $compare_to ),
+ );
+ }
+ wp_send_json_success( $return );
+}
+
+/**
+ * Handles auto-saving the selected color scheme for
+ * a user's own profile via AJAX.
+ *
+ * @since 3.8.0
+ *
+ * @global array $_wp_admin_css_colors
+ */
+function wp_ajax_save_user_color_scheme() {
+ global $_wp_admin_css_colors;
+
+ check_ajax_referer( 'save-color-scheme', 'nonce' );
+
+ $color_scheme = sanitize_key( $_POST['color_scheme'] );
+
+ if ( ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) {
+ wp_send_json_error();
+ }
+
+ $previous_color_scheme = get_user_meta( get_current_user_id(), 'admin_color', true );
+ update_user_meta( get_current_user_id(), 'admin_color', $color_scheme );
+
+ wp_send_json_success(
+ array(
+ 'previousScheme' => 'admin-color-' . $previous_color_scheme,
+ 'currentScheme' => 'admin-color-' . $color_scheme,
+ )
+ );
+}
+
+/**
+ * Handles getting themes from themes_api() via AJAX.
+ *
+ * @since 3.9.0
+ *
+ * @global array $themes_allowedtags
+ * @global array $theme_field_defaults
+ */
+function wp_ajax_query_themes() {
+ global $themes_allowedtags, $theme_field_defaults;
+
+ if ( ! current_user_can( 'install_themes' ) ) {
+ wp_send_json_error();
+ }
+
+ $args = wp_parse_args(
+ wp_unslash( $_REQUEST['request'] ),
+ array(
+ 'per_page' => 20,
+ 'fields' => array_merge(
+ (array) $theme_field_defaults,
+ array(
+ 'reviews_url' => true, // Explicitly request the reviews URL to be linked from the Add Themes screen.
+ )
+ ),
+ )
+ );
+
+ if ( isset( $args['browse'] ) && 'favorites' === $args['browse'] && ! isset( $args['user'] ) ) {
+ $user = get_user_option( 'wporg_favorites' );
+ if ( $user ) {
+ $args['user'] = $user;
+ }
+ }
+
+ $old_filter = isset( $args['browse'] ) ? $args['browse'] : 'search';
+
+ /** This filter is documented in wp-admin/includes/class-wp-theme-install-list-table.php */
+ $args = apply_filters( 'install_themes_table_api_args_' . $old_filter, $args );
+
+ $api = themes_api( 'query_themes', $args );
+
+ if ( is_wp_error( $api ) ) {
+ wp_send_json_error();
+ }
+
+ $update_php = network_admin_url( 'update.php?action=install-theme' );
+
+ $installed_themes = search_theme_directories();
+
+ if ( false === $installed_themes ) {
+ $installed_themes = array();
+ }
+
+ foreach ( $installed_themes as $theme_slug => $theme_data ) {
+ // Ignore child themes.
+ if ( str_contains( $theme_slug, '/' ) ) {
+ unset( $installed_themes[ $theme_slug ] );
+ }
+ }
+
+ foreach ( $api->themes as &$theme ) {
+ $theme->install_url = add_query_arg(
+ array(
+ 'theme' => $theme->slug,
+ '_wpnonce' => wp_create_nonce( 'install-theme_' . $theme->slug ),
+ ),
+ $update_php
+ );
+
+ if ( current_user_can( 'switch_themes' ) ) {
+ if ( is_multisite() ) {
+ $theme->activate_url = add_query_arg(
+ array(
+ 'action' => 'enable',
+ '_wpnonce' => wp_create_nonce( 'enable-theme_' . $theme->slug ),
+ 'theme' => $theme->slug,
+ ),
+ network_admin_url( 'themes.php' )
+ );
+ } else {
+ $theme->activate_url = add_query_arg(
+ array(
+ 'action' => 'activate',
+ '_wpnonce' => wp_create_nonce( 'switch-theme_' . $theme->slug ),
+ 'stylesheet' => $theme->slug,
+ ),
+ admin_url( 'themes.php' )
+ );
+ }
+ }
+
+ $is_theme_installed = array_key_exists( $theme->slug, $installed_themes );
+
+ // We only care about installed themes.
+ $theme->block_theme = $is_theme_installed && wp_get_theme( $theme->slug )->is_block_theme();
+
+ if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
+ $customize_url = $theme->block_theme ? admin_url( 'site-editor.php' ) : wp_customize_url( $theme->slug );
+
+ $theme->customize_url = add_query_arg(
+ array(
+ 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ),
+ ),
+ $customize_url
+ );
+ }
+
+ $theme->name = wp_kses( $theme->name, $themes_allowedtags );
+ $theme->author = wp_kses( $theme->author['display_name'], $themes_allowedtags );
+ $theme->version = wp_kses( $theme->version, $themes_allowedtags );
+ $theme->description = wp_kses( $theme->description, $themes_allowedtags );
+
+ $theme->stars = wp_star_rating(
+ array(
+ 'rating' => $theme->rating,
+ 'type' => 'percent',
+ 'number' => $theme->num_ratings,
+ 'echo' => false,
+ )
+ );
+
+ $theme->num_ratings = number_format_i18n( $theme->num_ratings );
+ $theme->preview_url = set_url_scheme( $theme->preview_url );
+ $theme->compatible_wp = is_wp_version_compatible( $theme->requires );
+ $theme->compatible_php = is_php_version_compatible( $theme->requires_php );
+ }
+
+ wp_send_json_success( $api );
+}
+
+/**
+ * Applies [embed] Ajax handlers to a string.
+ *
+ * @since 4.0.0
+ *
+ * @global WP_Post $post Global post object.
+ * @global WP_Embed $wp_embed Embed API instance.
+ * @global WP_Scripts $wp_scripts
+ * @global int $content_width
+ */
+function wp_ajax_parse_embed() {
+ global $post, $wp_embed, $content_width;
+
+ if ( empty( $_POST['shortcode'] ) ) {
+ wp_send_json_error();
+ }
+
+ $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0;
+
+ if ( $post_id > 0 ) {
+ $post = get_post( $post_id );
+
+ if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
+ wp_send_json_error();
+ }
+ setup_postdata( $post );
+ } elseif ( ! current_user_can( 'edit_posts' ) ) { // See WP_oEmbed_Controller::get_proxy_item_permissions_check().
+ wp_send_json_error();
+ }
+
+ $shortcode = wp_unslash( $_POST['shortcode'] );
+
+ preg_match( '/' . get_shortcode_regex() . '/s', $shortcode, $matches );
+ $atts = shortcode_parse_atts( $matches[3] );
+
+ if ( ! empty( $matches[5] ) ) {
+ $url = $matches[5];
+ } elseif ( ! empty( $atts['src'] ) ) {
+ $url = $atts['src'];
+ } else {
+ $url = '';
+ }
+
+ $parsed = false;
+ $wp_embed->return_false_on_fail = true;
+
+ if ( 0 === $post_id ) {
+ /*
+ * Refresh oEmbeds cached outside of posts that are past their TTL.
+ * Posts are excluded because they have separate logic for refreshing
+ * their post meta caches. See WP_Embed::cache_oembed().
+ */
+ $wp_embed->usecache = false;
+ }
+
+ if ( is_ssl() && str_starts_with( $url, 'http://' ) ) {
+ /*
+ * Admin is ssl and the user pasted non-ssl URL.
+ * Check if the provider supports ssl embeds and use that for the preview.
+ */
+ $ssl_shortcode = preg_replace( '%^(\\[embed[^\\]]*\\])http://%i', '$1https://', $shortcode );
+ $parsed = $wp_embed->run_shortcode( $ssl_shortcode );
+
+ if ( ! $parsed ) {
+ $no_ssl_support = true;
+ }
+ }
+
+ // Set $content_width so any embeds fit in the destination iframe.
+ if ( isset( $_POST['maxwidth'] ) && is_numeric( $_POST['maxwidth'] ) && $_POST['maxwidth'] > 0 ) {
+ if ( ! isset( $content_width ) ) {
+ $content_width = (int) $_POST['maxwidth'];
+ } else {
+ $content_width = min( $content_width, (int) $_POST['maxwidth'] );
+ }
+ }
+
+ if ( $url && ! $parsed ) {
+ $parsed = $wp_embed->run_shortcode( $shortcode );
+ }
+
+ if ( ! $parsed ) {
+ wp_send_json_error(
+ array(
+ 'type' => 'not-embeddable',
+ /* translators: %s: URL that could not be embedded. */
+ 'message' => sprintf( __( '%s failed to embed.' ), '' . esc_html( $url ) . '
' ),
+ )
+ );
+ }
+
+ if ( has_shortcode( $parsed, 'audio' ) || has_shortcode( $parsed, 'video' ) ) {
+ $styles = '';
+ $mce_styles = wpview_media_sandbox_styles();
+
+ foreach ( $mce_styles as $style ) {
+ $styles .= sprintf( ' ', $style );
+ }
+
+ $html = do_shortcode( $parsed );
+
+ global $wp_scripts;
+
+ if ( ! empty( $wp_scripts ) ) {
+ $wp_scripts->done = array();
+ }
+
+ ob_start();
+ wp_print_scripts( array( 'mediaelement-vimeo', 'wp-mediaelement' ) );
+ $scripts = ob_get_clean();
+
+ $parsed = $styles . $html . $scripts;
+ }
+
+ if ( ! empty( $no_ssl_support ) || ( is_ssl() && ( preg_match( '%<(iframe|script|embed) [^>]*src="http://%', $parsed ) ||
+ preg_match( '% ]*href="http://%', $parsed ) ) ) ) {
+ // Admin is ssl and the embed is not. Iframes, scripts, and other "active content" will be blocked.
+ wp_send_json_error(
+ array(
+ 'type' => 'not-ssl',
+ 'message' => __( 'This preview is unavailable in the editor.' ),
+ )
+ );
+ }
+
+ $return = array(
+ 'body' => $parsed,
+ 'attr' => $wp_embed->last_attr,
+ );
+
+ if ( str_contains( $parsed, 'class="wp-embedded-content' ) ) {
+ if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
+ $script_src = includes_url( 'js/wp-embed.js' );
+ } else {
+ $script_src = includes_url( 'js/wp-embed.min.js' );
+ }
+
+ $return['head'] = '';
+ $return['sandbox'] = true;
+ }
+
+ wp_send_json_success( $return );
+}
+
+/**
+ * @since 4.0.0
+ *
+ * @global WP_Post $post Global post object.
+ * @global WP_Scripts $wp_scripts
+ */
+function wp_ajax_parse_media_shortcode() {
+ global $post, $wp_scripts;
+
+ if ( empty( $_POST['shortcode'] ) ) {
+ wp_send_json_error();
+ }
+
+ $shortcode = wp_unslash( $_POST['shortcode'] );
+
+ // Only process previews for media related shortcodes:
+ $found_shortcodes = get_shortcode_tags_in_content( $shortcode );
+ $media_shortcodes = array(
+ 'audio',
+ 'embed',
+ 'playlist',
+ 'video',
+ 'gallery',
+ );
+
+ $other_shortcodes = array_diff( $found_shortcodes, $media_shortcodes );
+
+ if ( ! empty( $other_shortcodes ) ) {
+ wp_send_json_error();
+ }
+
+ if ( ! empty( $_POST['post_ID'] ) ) {
+ $post = get_post( (int) $_POST['post_ID'] );
+ }
+
+ // The embed shortcode requires a post.
+ if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
+ if ( in_array( 'embed', $found_shortcodes, true ) ) {
+ wp_send_json_error();
+ }
+ } else {
+ setup_postdata( $post );
+ }
+
+ $parsed = do_shortcode( $shortcode );
+
+ if ( empty( $parsed ) ) {
+ wp_send_json_error(
+ array(
+ 'type' => 'no-items',
+ 'message' => __( 'No items found.' ),
+ )
+ );
+ }
+
+ $head = '';
+ $styles = wpview_media_sandbox_styles();
+
+ foreach ( $styles as $style ) {
+ $head .= ' ';
+ }
+
+ if ( ! empty( $wp_scripts ) ) {
+ $wp_scripts->done = array();
+ }
+
+ ob_start();
+
+ echo $parsed;
+
+ if ( 'playlist' === $_REQUEST['type'] ) {
+ wp_underscore_playlist_templates();
+
+ wp_print_scripts( 'wp-playlist' );
+ } else {
+ wp_print_scripts( array( 'mediaelement-vimeo', 'wp-mediaelement' ) );
+ }
+
+ wp_send_json_success(
+ array(
+ 'head' => $head,
+ 'body' => ob_get_clean(),
+ )
+ );
+}
+
+/**
+ * Handles destroying multiple open sessions for a user via AJAX.
+ *
+ * @since 4.1.0
+ */
+function wp_ajax_destroy_sessions() {
+ $user = get_userdata( (int) $_POST['user_id'] );
+
+ if ( $user ) {
+ if ( ! current_user_can( 'edit_user', $user->ID ) ) {
+ $user = false;
+ } elseif ( ! wp_verify_nonce( $_POST['nonce'], 'update-user_' . $user->ID ) ) {
+ $user = false;
+ }
+ }
+
+ if ( ! $user ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'Could not log out user sessions. Please try again.' ),
+ )
+ );
+ }
+
+ $sessions = WP_Session_Tokens::get_instance( $user->ID );
+
+ if ( get_current_user_id() === $user->ID ) {
+ $sessions->destroy_others( wp_get_session_token() );
+ $message = __( 'You are now logged out everywhere else.' );
+ } else {
+ $sessions->destroy_all();
+ /* translators: %s: User's display name. */
+ $message = sprintf( __( '%s has been logged out.' ), $user->display_name );
+ }
+
+ wp_send_json_success( array( 'message' => $message ) );
+}
+
+/**
+ * Handles cropping an image via AJAX.
+ *
+ * @since 4.3.0
+ */
+function wp_ajax_crop_image() {
+ $attachment_id = absint( $_POST['id'] );
+
+ check_ajax_referer( 'image_editor-' . $attachment_id, 'nonce' );
+
+ if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) {
+ wp_send_json_error();
+ }
+
+ $context = str_replace( '_', '-', $_POST['context'] );
+ $data = array_map( 'absint', $_POST['cropDetails'] );
+ $cropped = wp_crop_image( $attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height'] );
+
+ if ( ! $cropped || is_wp_error( $cropped ) ) {
+ wp_send_json_error( array( 'message' => __( 'Image could not be processed.' ) ) );
+ }
+
+ switch ( $context ) {
+ case 'site-icon':
+ require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php';
+ $wp_site_icon = new WP_Site_Icon();
+
+ // Skip creating a new attachment if the attachment is a Site Icon.
+ if ( get_post_meta( $attachment_id, '_wp_attachment_context', true ) == $context ) {
+
+ // Delete the temporary cropped file, we don't need it.
+ wp_delete_file( $cropped );
+
+ // Additional sizes in wp_prepare_attachment_for_js().
+ add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );
+ break;
+ }
+
+ /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
+ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
+ $attachment = $wp_site_icon->create_attachment_object( $cropped, $attachment_id );
+ unset( $attachment['ID'] );
+
+ // Update the attachment.
+ add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );
+ $attachment_id = $wp_site_icon->insert_attachment( $attachment, $cropped );
+ remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );
+
+ // Additional sizes in wp_prepare_attachment_for_js().
+ add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );
+ break;
+
+ default:
+ /**
+ * Fires before a cropped image is saved.
+ *
+ * Allows to add filters to modify the way a cropped image is saved.
+ *
+ * @since 4.3.0
+ *
+ * @param string $context The Customizer control requesting the cropped image.
+ * @param int $attachment_id The attachment ID of the original image.
+ * @param string $cropped Path to the cropped image file.
+ */
+ do_action( 'wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped );
+
+ /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
+ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
+
+ $parent_url = wp_get_attachment_url( $attachment_id );
+ $parent_basename = wp_basename( $parent_url );
+ $url = str_replace( $parent_basename, wp_basename( $cropped ), $parent_url );
+
+ $size = wp_getimagesize( $cropped );
+ $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
+
+ // Get the original image's post to pre-populate the cropped image.
+ $original_attachment = get_post( $attachment_id );
+ $sanitized_post_title = sanitize_file_name( $original_attachment->post_title );
+ $use_original_title = (
+ ( '' !== trim( $original_attachment->post_title ) ) &&
+ /*
+ * Check if the original image has a title other than the "filename" default,
+ * meaning the image had a title when originally uploaded or its title was edited.
+ */
+ ( $parent_basename !== $sanitized_post_title ) &&
+ ( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
+ );
+ $use_original_description = ( '' !== trim( $original_attachment->post_content ) );
+
+ $attachment = array(
+ 'post_title' => $use_original_title ? $original_attachment->post_title : wp_basename( $cropped ),
+ 'post_content' => $use_original_description ? $original_attachment->post_content : $url,
+ 'post_mime_type' => $image_type,
+ 'guid' => $url,
+ 'context' => $context,
+ );
+
+ // Copy the image caption attribute (post_excerpt field) from the original image.
+ if ( '' !== trim( $original_attachment->post_excerpt ) ) {
+ $attachment['post_excerpt'] = $original_attachment->post_excerpt;
+ }
+
+ // Copy the image alt text attribute from the original image.
+ if ( '' !== trim( $original_attachment->_wp_attachment_image_alt ) ) {
+ $attachment['meta_input'] = array(
+ '_wp_attachment_image_alt' => wp_slash( $original_attachment->_wp_attachment_image_alt ),
+ );
+ }
+
+ $attachment_id = wp_insert_attachment( $attachment, $cropped );
+ $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
+
+ /**
+ * Filters the cropped image attachment metadata.
+ *
+ * @since 4.3.0
+ *
+ * @see wp_generate_attachment_metadata()
+ *
+ * @param array $metadata Attachment metadata.
+ */
+ $metadata = apply_filters( 'wp_ajax_cropped_attachment_metadata', $metadata );
+ wp_update_attachment_metadata( $attachment_id, $metadata );
+
+ /**
+ * Filters the attachment ID for a cropped image.
+ *
+ * @since 4.3.0
+ *
+ * @param int $attachment_id The attachment ID of the cropped image.
+ * @param string $context The Customizer control requesting the cropped image.
+ */
+ $attachment_id = apply_filters( 'wp_ajax_cropped_attachment_id', $attachment_id, $context );
+ }
+
+ wp_send_json_success( wp_prepare_attachment_for_js( $attachment_id ) );
+}
+
+/**
+ * Handles generating a password via AJAX.
+ *
+ * @since 4.4.0
+ */
+function wp_ajax_generate_password() {
+ wp_send_json_success( wp_generate_password( 24 ) );
+}
+
+/**
+ * Handles generating a password in the no-privilege context via AJAX.
+ *
+ * @since 5.7.0
+ */
+function wp_ajax_nopriv_generate_password() {
+ wp_send_json_success( wp_generate_password( 24 ) );
+}
+
+/**
+ * Handles saving the user's WordPress.org username via AJAX.
+ *
+ * @since 4.4.0
+ */
+function wp_ajax_save_wporg_username() {
+ if ( ! current_user_can( 'install_themes' ) && ! current_user_can( 'install_plugins' ) ) {
+ wp_send_json_error();
+ }
+
+ check_ajax_referer( 'save_wporg_username_' . get_current_user_id() );
+
+ $username = isset( $_REQUEST['username'] ) ? wp_unslash( $_REQUEST['username'] ) : false;
+
+ if ( ! $username ) {
+ wp_send_json_error();
+ }
+
+ wp_send_json_success( update_user_meta( get_current_user_id(), 'wporg_favorites', $username ) );
+}
+
+/**
+ * Handles installing a theme via AJAX.
+ *
+ * @since 4.6.0
+ *
+ * @see Theme_Upgrader
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ */
+function wp_ajax_install_theme() {
+ check_ajax_referer( 'updates' );
+
+ if ( empty( $_POST['slug'] ) ) {
+ wp_send_json_error(
+ array(
+ 'slug' => '',
+ 'errorCode' => 'no_theme_specified',
+ 'errorMessage' => __( 'No theme specified.' ),
+ )
+ );
+ }
+
+ $slug = sanitize_key( wp_unslash( $_POST['slug'] ) );
+
+ $status = array(
+ 'install' => 'theme',
+ 'slug' => $slug,
+ );
+
+ if ( ! current_user_can( 'install_themes' ) ) {
+ $status['errorMessage'] = __( 'Sorry, you are not allowed to install themes on this site.' );
+ wp_send_json_error( $status );
+ }
+
+ require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
+ require_once ABSPATH . 'wp-admin/includes/theme.php';
+
+ $api = themes_api(
+ 'theme_information',
+ array(
+ 'slug' => $slug,
+ 'fields' => array( 'sections' => false ),
+ )
+ );
+
+ if ( is_wp_error( $api ) ) {
+ $status['errorMessage'] = $api->get_error_message();
+ wp_send_json_error( $status );
+ }
+
+ $skin = new WP_Ajax_Upgrader_Skin();
+ $upgrader = new Theme_Upgrader( $skin );
+ $result = $upgrader->install( $api->download_link );
+
+ if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
+ $status['debug'] = $skin->get_upgrade_messages();
+ }
+
+ if ( is_wp_error( $result ) ) {
+ $status['errorCode'] = $result->get_error_code();
+ $status['errorMessage'] = $result->get_error_message();
+ wp_send_json_error( $status );
+ } elseif ( is_wp_error( $skin->result ) ) {
+ $status['errorCode'] = $skin->result->get_error_code();
+ $status['errorMessage'] = $skin->result->get_error_message();
+ wp_send_json_error( $status );
+ } elseif ( $skin->get_errors()->has_errors() ) {
+ $status['errorMessage'] = $skin->get_error_messages();
+ wp_send_json_error( $status );
+ } elseif ( is_null( $result ) ) {
+ global $wp_filesystem;
+
+ $status['errorCode'] = 'unable_to_connect_to_filesystem';
+ $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
+
+ // Pass through the error from WP_Filesystem if one was raised.
+ if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
+ $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
+ }
+
+ wp_send_json_error( $status );
+ }
+
+ $status['themeName'] = wp_get_theme( $slug )->get( 'Name' );
+
+ if ( current_user_can( 'switch_themes' ) ) {
+ if ( is_multisite() ) {
+ $status['activateUrl'] = add_query_arg(
+ array(
+ 'action' => 'enable',
+ '_wpnonce' => wp_create_nonce( 'enable-theme_' . $slug ),
+ 'theme' => $slug,
+ ),
+ network_admin_url( 'themes.php' )
+ );
+ } else {
+ $status['activateUrl'] = add_query_arg(
+ array(
+ 'action' => 'activate',
+ '_wpnonce' => wp_create_nonce( 'switch-theme_' . $slug ),
+ 'stylesheet' => $slug,
+ ),
+ admin_url( 'themes.php' )
+ );
+ }
+ }
+
+ $theme = wp_get_theme( $slug );
+ $status['blockTheme'] = $theme->is_block_theme();
+
+ if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
+ $status['customizeUrl'] = add_query_arg(
+ array(
+ 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ),
+ ),
+ wp_customize_url( $slug )
+ );
+ }
+
+ /*
+ * See WP_Theme_Install_List_Table::_get_theme_status() if we wanted to check
+ * on post-installation status.
+ */
+ wp_send_json_success( $status );
+}
+
+/**
+ * Handles updating a theme via AJAX.
+ *
+ * @since 4.6.0
+ *
+ * @see Theme_Upgrader
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ */
+function wp_ajax_update_theme() {
+ check_ajax_referer( 'updates' );
+
+ if ( empty( $_POST['slug'] ) ) {
+ wp_send_json_error(
+ array(
+ 'slug' => '',
+ 'errorCode' => 'no_theme_specified',
+ 'errorMessage' => __( 'No theme specified.' ),
+ )
+ );
+ }
+
+ $stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) );
+ $status = array(
+ 'update' => 'theme',
+ 'slug' => $stylesheet,
+ 'oldVersion' => '',
+ 'newVersion' => '',
+ );
+
+ if ( ! current_user_can( 'update_themes' ) ) {
+ $status['errorMessage'] = __( 'Sorry, you are not allowed to update themes for this site.' );
+ wp_send_json_error( $status );
+ }
+
+ $theme = wp_get_theme( $stylesheet );
+ if ( $theme->exists() ) {
+ $status['oldVersion'] = $theme->get( 'Version' );
+ }
+
+ require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
+
+ $current = get_site_transient( 'update_themes' );
+ if ( empty( $current ) ) {
+ wp_update_themes();
+ }
+
+ $skin = new WP_Ajax_Upgrader_Skin();
+ $upgrader = new Theme_Upgrader( $skin );
+ $result = $upgrader->bulk_upgrade( array( $stylesheet ) );
+
+ if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
+ $status['debug'] = $skin->get_upgrade_messages();
+ }
+
+ if ( is_wp_error( $skin->result ) ) {
+ $status['errorCode'] = $skin->result->get_error_code();
+ $status['errorMessage'] = $skin->result->get_error_message();
+ wp_send_json_error( $status );
+ } elseif ( $skin->get_errors()->has_errors() ) {
+ $status['errorMessage'] = $skin->get_error_messages();
+ wp_send_json_error( $status );
+ } elseif ( is_array( $result ) && ! empty( $result[ $stylesheet ] ) ) {
+
+ // Theme is already at the latest version.
+ if ( true === $result[ $stylesheet ] ) {
+ $status['errorMessage'] = $upgrader->strings['up_to_date'];
+ wp_send_json_error( $status );
+ }
+
+ $theme = wp_get_theme( $stylesheet );
+ if ( $theme->exists() ) {
+ $status['newVersion'] = $theme->get( 'Version' );
+ }
+
+ wp_send_json_success( $status );
+ } elseif ( false === $result ) {
+ global $wp_filesystem;
+
+ $status['errorCode'] = 'unable_to_connect_to_filesystem';
+ $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
+
+ // Pass through the error from WP_Filesystem if one was raised.
+ if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
+ $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
+ }
+
+ wp_send_json_error( $status );
+ }
+
+ // An unhandled error occurred.
+ $status['errorMessage'] = __( 'Theme update failed.' );
+ wp_send_json_error( $status );
+}
+
+/**
+ * Handles deleting a theme via AJAX.
+ *
+ * @since 4.6.0
+ *
+ * @see delete_theme()
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ */
+function wp_ajax_delete_theme() {
+ check_ajax_referer( 'updates' );
+
+ if ( empty( $_POST['slug'] ) ) {
+ wp_send_json_error(
+ array(
+ 'slug' => '',
+ 'errorCode' => 'no_theme_specified',
+ 'errorMessage' => __( 'No theme specified.' ),
+ )
+ );
+ }
+
+ $stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) );
+ $status = array(
+ 'delete' => 'theme',
+ 'slug' => $stylesheet,
+ );
+
+ if ( ! current_user_can( 'delete_themes' ) ) {
+ $status['errorMessage'] = __( 'Sorry, you are not allowed to delete themes on this site.' );
+ wp_send_json_error( $status );
+ }
+
+ if ( ! wp_get_theme( $stylesheet )->exists() ) {
+ $status['errorMessage'] = __( 'The requested theme does not exist.' );
+ wp_send_json_error( $status );
+ }
+
+ // Check filesystem credentials. `delete_theme()` will bail otherwise.
+ $url = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet );
+
+ ob_start();
+ $credentials = request_filesystem_credentials( $url );
+ ob_end_clean();
+
+ if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
+ global $wp_filesystem;
+
+ $status['errorCode'] = 'unable_to_connect_to_filesystem';
+ $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
+
+ // Pass through the error from WP_Filesystem if one was raised.
+ if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
+ $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
+ }
+
+ wp_send_json_error( $status );
+ }
+
+ require_once ABSPATH . 'wp-admin/includes/theme.php';
+
+ $result = delete_theme( $stylesheet );
+
+ if ( is_wp_error( $result ) ) {
+ $status['errorMessage'] = $result->get_error_message();
+ wp_send_json_error( $status );
+ } elseif ( false === $result ) {
+ $status['errorMessage'] = __( 'Theme could not be deleted.' );
+ wp_send_json_error( $status );
+ }
+
+ wp_send_json_success( $status );
+}
+
+/**
+ * Handles installing a plugin via AJAX.
+ *
+ * @since 4.6.0
+ *
+ * @see Plugin_Upgrader
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ */
+function wp_ajax_install_plugin() {
+ check_ajax_referer( 'updates' );
+
+ if ( empty( $_POST['slug'] ) ) {
+ wp_send_json_error(
+ array(
+ 'slug' => '',
+ 'errorCode' => 'no_plugin_specified',
+ 'errorMessage' => __( 'No plugin specified.' ),
+ )
+ );
+ }
+
+ $status = array(
+ 'install' => 'plugin',
+ 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),
+ );
+
+ if ( ! current_user_can( 'install_plugins' ) ) {
+ $status['errorMessage'] = __( 'Sorry, you are not allowed to install plugins on this site.' );
+ wp_send_json_error( $status );
+ }
+
+ require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
+ require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
+
+ $api = plugins_api(
+ 'plugin_information',
+ array(
+ 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),
+ 'fields' => array(
+ 'sections' => false,
+ ),
+ )
+ );
+
+ if ( is_wp_error( $api ) ) {
+ $status['errorMessage'] = $api->get_error_message();
+ wp_send_json_error( $status );
+ }
+
+ $status['pluginName'] = $api->name;
+
+ $skin = new WP_Ajax_Upgrader_Skin();
+ $upgrader = new Plugin_Upgrader( $skin );
+ $result = $upgrader->install( $api->download_link );
+
+ if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
+ $status['debug'] = $skin->get_upgrade_messages();
+ }
+
+ if ( is_wp_error( $result ) ) {
+ $status['errorCode'] = $result->get_error_code();
+ $status['errorMessage'] = $result->get_error_message();
+ wp_send_json_error( $status );
+ } elseif ( is_wp_error( $skin->result ) ) {
+ $status['errorCode'] = $skin->result->get_error_code();
+ $status['errorMessage'] = $skin->result->get_error_message();
+ wp_send_json_error( $status );
+ } elseif ( $skin->get_errors()->has_errors() ) {
+ $status['errorMessage'] = $skin->get_error_messages();
+ wp_send_json_error( $status );
+ } elseif ( is_null( $result ) ) {
+ global $wp_filesystem;
+
+ $status['errorCode'] = 'unable_to_connect_to_filesystem';
+ $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
+
+ // Pass through the error from WP_Filesystem if one was raised.
+ if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
+ $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
+ }
+
+ wp_send_json_error( $status );
+ }
+
+ $install_status = install_plugin_install_status( $api );
+ $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : '';
+
+ // If installation request is coming from import page, do not return network activation link.
+ $plugins_url = ( 'import' === $pagenow ) ? admin_url( 'plugins.php' ) : network_admin_url( 'plugins.php' );
+
+ if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) {
+ $status['activateUrl'] = add_query_arg(
+ array(
+ '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ),
+ 'action' => 'activate',
+ 'plugin' => $install_status['file'],
+ ),
+ $plugins_url
+ );
+ }
+
+ if ( is_multisite() && current_user_can( 'manage_network_plugins' ) && 'import' !== $pagenow ) {
+ $status['activateUrl'] = add_query_arg( array( 'networkwide' => 1 ), $status['activateUrl'] );
+ }
+
+ wp_send_json_success( $status );
+}
+
+/**
+ * Handles updating a plugin via AJAX.
+ *
+ * @since 4.2.0
+ *
+ * @see Plugin_Upgrader
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ */
+function wp_ajax_update_plugin() {
+ check_ajax_referer( 'updates' );
+
+ if ( empty( $_POST['plugin'] ) || empty( $_POST['slug'] ) ) {
+ wp_send_json_error(
+ array(
+ 'slug' => '',
+ 'errorCode' => 'no_plugin_specified',
+ 'errorMessage' => __( 'No plugin specified.' ),
+ )
+ );
+ }
+
+ $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) );
+
+ $status = array(
+ 'update' => 'plugin',
+ 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),
+ 'oldVersion' => '',
+ 'newVersion' => '',
+ );
+
+ if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) {
+ $status['errorMessage'] = __( 'Sorry, you are not allowed to update plugins for this site.' );
+ wp_send_json_error( $status );
+ }
+
+ $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
+ $status['plugin'] = $plugin;
+ $status['pluginName'] = $plugin_data['Name'];
+
+ if ( $plugin_data['Version'] ) {
+ /* translators: %s: Plugin version. */
+ $status['oldVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
+ }
+
+ require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
+
+ wp_update_plugins();
+
+ $skin = new WP_Ajax_Upgrader_Skin();
+ $upgrader = new Plugin_Upgrader( $skin );
+ $result = $upgrader->bulk_upgrade( array( $plugin ) );
+
+ if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
+ $status['debug'] = $skin->get_upgrade_messages();
+ }
+
+ if ( is_wp_error( $skin->result ) ) {
+ $status['errorCode'] = $skin->result->get_error_code();
+ $status['errorMessage'] = $skin->result->get_error_message();
+ wp_send_json_error( $status );
+ } elseif ( $skin->get_errors()->has_errors() ) {
+ $status['errorMessage'] = $skin->get_error_messages();
+ wp_send_json_error( $status );
+ } elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) {
+
+ /*
+ * Plugin is already at the latest version.
+ *
+ * This may also be the return value if the `update_plugins` site transient is empty,
+ * e.g. when you update two plugins in quick succession before the transient repopulates.
+ *
+ * Preferably something can be done to ensure `update_plugins` isn't empty.
+ * For now, surface some sort of error here.
+ */
+ if ( true === $result[ $plugin ] ) {
+ $status['errorMessage'] = $upgrader->strings['up_to_date'];
+ wp_send_json_error( $status );
+ }
+
+ $plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] );
+ $plugin_data = reset( $plugin_data );
+
+ if ( $plugin_data['Version'] ) {
+ /* translators: %s: Plugin version. */
+ $status['newVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
+ }
+
+ wp_send_json_success( $status );
+ } elseif ( false === $result ) {
+ global $wp_filesystem;
+
+ $status['errorCode'] = 'unable_to_connect_to_filesystem';
+ $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
+
+ // Pass through the error from WP_Filesystem if one was raised.
+ if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
+ $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
+ }
+
+ wp_send_json_error( $status );
+ }
+
+ // An unhandled error occurred.
+ $status['errorMessage'] = __( 'Plugin update failed.' );
+ wp_send_json_error( $status );
+}
+
+/**
+ * Handles deleting a plugin via AJAX.
+ *
+ * @since 4.6.0
+ *
+ * @see delete_plugins()
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ */
+function wp_ajax_delete_plugin() {
+ check_ajax_referer( 'updates' );
+
+ if ( empty( $_POST['slug'] ) || empty( $_POST['plugin'] ) ) {
+ wp_send_json_error(
+ array(
+ 'slug' => '',
+ 'errorCode' => 'no_plugin_specified',
+ 'errorMessage' => __( 'No plugin specified.' ),
+ )
+ );
+ }
+
+ $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) );
+
+ $status = array(
+ 'delete' => 'plugin',
+ 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),
+ );
+
+ if ( ! current_user_can( 'delete_plugins' ) || 0 !== validate_file( $plugin ) ) {
+ $status['errorMessage'] = __( 'Sorry, you are not allowed to delete plugins for this site.' );
+ wp_send_json_error( $status );
+ }
+
+ $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
+ $status['plugin'] = $plugin;
+ $status['pluginName'] = $plugin_data['Name'];
+
+ if ( is_plugin_active( $plugin ) ) {
+ $status['errorMessage'] = __( 'You cannot delete a plugin while it is active on the main site.' );
+ wp_send_json_error( $status );
+ }
+
+ // Check filesystem credentials. `delete_plugins()` will bail otherwise.
+ $url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&checked[]=' . $plugin, 'bulk-plugins' );
+
+ ob_start();
+ $credentials = request_filesystem_credentials( $url );
+ ob_end_clean();
+
+ if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
+ global $wp_filesystem;
+
+ $status['errorCode'] = 'unable_to_connect_to_filesystem';
+ $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
+
+ // Pass through the error from WP_Filesystem if one was raised.
+ if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
+ $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
+ }
+
+ wp_send_json_error( $status );
+ }
+
+ $result = delete_plugins( array( $plugin ) );
+
+ if ( is_wp_error( $result ) ) {
+ $status['errorMessage'] = $result->get_error_message();
+ wp_send_json_error( $status );
+ } elseif ( false === $result ) {
+ $status['errorMessage'] = __( 'Plugin could not be deleted.' );
+ wp_send_json_error( $status );
+ }
+
+ wp_send_json_success( $status );
+}
+
+/**
+ * Handles searching plugins via AJAX.
+ *
+ * @since 4.6.0
+ *
+ * @global string $s Search term.
+ */
+function wp_ajax_search_plugins() {
+ check_ajax_referer( 'updates' );
+
+ // Ensure after_plugin_row_{$plugin_file} gets hooked.
+ wp_plugin_update_rows();
+
+ $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : '';
+ if ( 'plugins-network' === $pagenow || 'plugins' === $pagenow ) {
+ set_current_screen( $pagenow );
+ }
+
+ /** @var WP_Plugins_List_Table $wp_list_table */
+ $wp_list_table = _get_list_table(
+ 'WP_Plugins_List_Table',
+ array(
+ 'screen' => get_current_screen(),
+ )
+ );
+
+ $status = array();
+
+ if ( ! $wp_list_table->ajax_user_can() ) {
+ $status['errorMessage'] = __( 'Sorry, you are not allowed to manage plugins for this site.' );
+ wp_send_json_error( $status );
+ }
+
+ // Set the correct requester, so pagination works.
+ $_SERVER['REQUEST_URI'] = add_query_arg(
+ array_diff_key(
+ $_POST,
+ array(
+ '_ajax_nonce' => null,
+ 'action' => null,
+ )
+ ),
+ network_admin_url( 'plugins.php', 'relative' )
+ );
+
+ $GLOBALS['s'] = wp_unslash( $_POST['s'] );
+
+ $wp_list_table->prepare_items();
+
+ ob_start();
+ $wp_list_table->display();
+ $status['count'] = count( $wp_list_table->items );
+ $status['items'] = ob_get_clean();
+
+ wp_send_json_success( $status );
+}
+
+/**
+ * Handles searching plugins to install via AJAX.
+ *
+ * @since 4.6.0
+ */
+function wp_ajax_search_install_plugins() {
+ check_ajax_referer( 'updates' );
+
+ $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : '';
+ if ( 'plugin-install-network' === $pagenow || 'plugin-install' === $pagenow ) {
+ set_current_screen( $pagenow );
+ }
+
+ /** @var WP_Plugin_Install_List_Table $wp_list_table */
+ $wp_list_table = _get_list_table(
+ 'WP_Plugin_Install_List_Table',
+ array(
+ 'screen' => get_current_screen(),
+ )
+ );
+
+ $status = array();
+
+ if ( ! $wp_list_table->ajax_user_can() ) {
+ $status['errorMessage'] = __( 'Sorry, you are not allowed to manage plugins for this site.' );
+ wp_send_json_error( $status );
+ }
+
+ // Set the correct requester, so pagination works.
+ $_SERVER['REQUEST_URI'] = add_query_arg(
+ array_diff_key(
+ $_POST,
+ array(
+ '_ajax_nonce' => null,
+ 'action' => null,
+ )
+ ),
+ network_admin_url( 'plugin-install.php', 'relative' )
+ );
+
+ $wp_list_table->prepare_items();
+
+ ob_start();
+ $wp_list_table->display();
+ $status['count'] = (int) $wp_list_table->get_pagination_arg( 'total_items' );
+ $status['items'] = ob_get_clean();
+
+ wp_send_json_success( $status );
+}
+
+/**
+ * Handles editing a theme or plugin file via AJAX.
+ *
+ * @since 4.9.0
+ *
+ * @see wp_edit_theme_plugin_file()
+ */
+function wp_ajax_edit_theme_plugin_file() {
+ $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) ); // Validation of args is done in wp_edit_theme_plugin_file().
+
+ if ( is_wp_error( $r ) ) {
+ wp_send_json_error(
+ array_merge(
+ array(
+ 'code' => $r->get_error_code(),
+ 'message' => $r->get_error_message(),
+ ),
+ (array) $r->get_error_data()
+ )
+ );
+ } else {
+ wp_send_json_success(
+ array(
+ 'message' => __( 'File edited successfully.' ),
+ )
+ );
+ }
+}
+
+/**
+ * Handles exporting a user's personal data via AJAX.
+ *
+ * @since 4.9.6
+ */
+function wp_ajax_wp_privacy_export_personal_data() {
+
+ if ( empty( $_POST['id'] ) ) {
+ wp_send_json_error( __( 'Missing request ID.' ) );
+ }
+
+ $request_id = (int) $_POST['id'];
+
+ if ( $request_id < 1 ) {
+ wp_send_json_error( __( 'Invalid request ID.' ) );
+ }
+
+ if ( ! current_user_can( 'export_others_personal_data' ) ) {
+ wp_send_json_error( __( 'Sorry, you are not allowed to perform this action.' ) );
+ }
+
+ check_ajax_referer( 'wp-privacy-export-personal-data-' . $request_id, 'security' );
+
+ // Get the request.
+ $request = wp_get_user_request( $request_id );
+
+ if ( ! $request || 'export_personal_data' !== $request->action_name ) {
+ wp_send_json_error( __( 'Invalid request type.' ) );
+ }
+
+ $email_address = $request->email;
+ if ( ! is_email( $email_address ) ) {
+ wp_send_json_error( __( 'A valid email address must be given.' ) );
+ }
+
+ if ( ! isset( $_POST['exporter'] ) ) {
+ wp_send_json_error( __( 'Missing exporter index.' ) );
+ }
+
+ $exporter_index = (int) $_POST['exporter'];
+
+ if ( ! isset( $_POST['page'] ) ) {
+ wp_send_json_error( __( 'Missing page index.' ) );
+ }
+
+ $page = (int) $_POST['page'];
+
+ $send_as_email = isset( $_POST['sendAsEmail'] ) ? ( 'true' === $_POST['sendAsEmail'] ) : false;
+
+ /**
+ * Filters the array of exporter callbacks.
+ *
+ * @since 4.9.6
+ *
+ * @param array $args {
+ * An array of callable exporters of personal data. Default empty array.
+ *
+ * @type array ...$0 {
+ * Array of personal data exporters.
+ *
+ * @type callable $callback Callable exporter function that accepts an
+ * email address and a page number and returns an
+ * array of name => value pairs of personal data.
+ * @type string $exporter_friendly_name Translated user facing friendly name for the
+ * exporter.
+ * }
+ * }
+ */
+ $exporters = apply_filters( 'wp_privacy_personal_data_exporters', array() );
+
+ if ( ! is_array( $exporters ) ) {
+ wp_send_json_error( __( 'An exporter has improperly used the registration filter.' ) );
+ }
+
+ // Do we have any registered exporters?
+ if ( 0 < count( $exporters ) ) {
+ if ( $exporter_index < 1 ) {
+ wp_send_json_error( __( 'Exporter index cannot be negative.' ) );
+ }
+
+ if ( $exporter_index > count( $exporters ) ) {
+ wp_send_json_error( __( 'Exporter index is out of range.' ) );
+ }
+
+ if ( $page < 1 ) {
+ wp_send_json_error( __( 'Page index cannot be less than one.' ) );
+ }
+
+ $exporter_keys = array_keys( $exporters );
+ $exporter_key = $exporter_keys[ $exporter_index - 1 ];
+ $exporter = $exporters[ $exporter_key ];
+
+ if ( ! is_array( $exporter ) ) {
+ wp_send_json_error(
+ /* translators: %s: Exporter array index. */
+ sprintf( __( 'Expected an array describing the exporter at index %s.' ), $exporter_key )
+ );
+ }
+
+ if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) {
+ wp_send_json_error(
+ /* translators: %s: Exporter array index. */
+ sprintf( __( 'Exporter array at index %s does not include a friendly name.' ), $exporter_key )
+ );
+ }
+
+ $exporter_friendly_name = $exporter['exporter_friendly_name'];
+
+ if ( ! array_key_exists( 'callback', $exporter ) ) {
+ wp_send_json_error(
+ /* translators: %s: Exporter friendly name. */
+ sprintf( __( 'Exporter does not include a callback: %s.' ), esc_html( $exporter_friendly_name ) )
+ );
+ }
+
+ if ( ! is_callable( $exporter['callback'] ) ) {
+ wp_send_json_error(
+ /* translators: %s: Exporter friendly name. */
+ sprintf( __( 'Exporter callback is not a valid callback: %s.' ), esc_html( $exporter_friendly_name ) )
+ );
+ }
+
+ $callback = $exporter['callback'];
+ $response = call_user_func( $callback, $email_address, $page );
+
+ if ( is_wp_error( $response ) ) {
+ wp_send_json_error( $response );
+ }
+
+ if ( ! is_array( $response ) ) {
+ wp_send_json_error(
+ /* translators: %s: Exporter friendly name. */
+ sprintf( __( 'Expected response as an array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
+ );
+ }
+
+ if ( ! array_key_exists( 'data', $response ) ) {
+ wp_send_json_error(
+ /* translators: %s: Exporter friendly name. */
+ sprintf( __( 'Expected data in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
+ );
+ }
+
+ if ( ! is_array( $response['data'] ) ) {
+ wp_send_json_error(
+ /* translators: %s: Exporter friendly name. */
+ sprintf( __( 'Expected data array in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
+ );
+ }
+
+ if ( ! array_key_exists( 'done', $response ) ) {
+ wp_send_json_error(
+ /* translators: %s: Exporter friendly name. */
+ sprintf( __( 'Expected done (boolean) in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
+ );
+ }
+ } else {
+ // No exporters, so we're done.
+ $exporter_key = '';
+
+ $response = array(
+ 'data' => array(),
+ 'done' => true,
+ );
+ }
+
+ /**
+ * Filters a page of personal data exporter data. Used to build the export report.
+ *
+ * Allows the export response to be consumed by destinations in addition to Ajax.
+ *
+ * @since 4.9.6
+ *
+ * @param array $response The personal data for the given exporter and page number.
+ * @param int $exporter_index The index of the exporter that provided this data.
+ * @param string $email_address The email address associated with this personal data.
+ * @param int $page The page number for this response.
+ * @param int $request_id The privacy request post ID associated with this request.
+ * @param bool $send_as_email Whether the final results of the export should be emailed to the user.
+ * @param string $exporter_key The key (slug) of the exporter that provided this data.
+ */
+ $response = apply_filters( 'wp_privacy_personal_data_export_page', $response, $exporter_index, $email_address, $page, $request_id, $send_as_email, $exporter_key );
+
+ if ( is_wp_error( $response ) ) {
+ wp_send_json_error( $response );
+ }
+
+ wp_send_json_success( $response );
+}
+
+/**
+ * Handles erasing personal data via AJAX.
+ *
+ * @since 4.9.6
+ */
+function wp_ajax_wp_privacy_erase_personal_data() {
+
+ if ( empty( $_POST['id'] ) ) {
+ wp_send_json_error( __( 'Missing request ID.' ) );
+ }
+
+ $request_id = (int) $_POST['id'];
+
+ if ( $request_id < 1 ) {
+ wp_send_json_error( __( 'Invalid request ID.' ) );
+ }
+
+ // Both capabilities are required to avoid confusion, see `_wp_personal_data_removal_page()`.
+ if ( ! current_user_can( 'erase_others_personal_data' ) || ! current_user_can( 'delete_users' ) ) {
+ wp_send_json_error( __( 'Sorry, you are not allowed to perform this action.' ) );
+ }
+
+ check_ajax_referer( 'wp-privacy-erase-personal-data-' . $request_id, 'security' );
+
+ // Get the request.
+ $request = wp_get_user_request( $request_id );
+
+ if ( ! $request || 'remove_personal_data' !== $request->action_name ) {
+ wp_send_json_error( __( 'Invalid request type.' ) );
+ }
+
+ $email_address = $request->email;
+
+ if ( ! is_email( $email_address ) ) {
+ wp_send_json_error( __( 'Invalid email address in request.' ) );
+ }
+
+ if ( ! isset( $_POST['eraser'] ) ) {
+ wp_send_json_error( __( 'Missing eraser index.' ) );
+ }
+
+ $eraser_index = (int) $_POST['eraser'];
+
+ if ( ! isset( $_POST['page'] ) ) {
+ wp_send_json_error( __( 'Missing page index.' ) );
+ }
+
+ $page = (int) $_POST['page'];
+
+ /**
+ * Filters the array of personal data eraser callbacks.
+ *
+ * @since 4.9.6
+ *
+ * @param array $args {
+ * An array of callable erasers of personal data. Default empty array.
+ *
+ * @type array ...$0 {
+ * Array of personal data exporters.
+ *
+ * @type callable $callback Callable eraser that accepts an email address and a page
+ * number, and returns an array with boolean values for
+ * whether items were removed or retained and any messages
+ * from the eraser, as well as if additional pages are
+ * available.
+ * @type string $exporter_friendly_name Translated user facing friendly name for the eraser.
+ * }
+ * }
+ */
+ $erasers = apply_filters( 'wp_privacy_personal_data_erasers', array() );
+
+ // Do we have any registered erasers?
+ if ( 0 < count( $erasers ) ) {
+
+ if ( $eraser_index < 1 ) {
+ wp_send_json_error( __( 'Eraser index cannot be less than one.' ) );
+ }
+
+ if ( $eraser_index > count( $erasers ) ) {
+ wp_send_json_error( __( 'Eraser index is out of range.' ) );
+ }
+
+ if ( $page < 1 ) {
+ wp_send_json_error( __( 'Page index cannot be less than one.' ) );
+ }
+
+ $eraser_keys = array_keys( $erasers );
+ $eraser_key = $eraser_keys[ $eraser_index - 1 ];
+ $eraser = $erasers[ $eraser_key ];
+
+ if ( ! is_array( $eraser ) ) {
+ /* translators: %d: Eraser array index. */
+ wp_send_json_error( sprintf( __( 'Expected an array describing the eraser at index %d.' ), $eraser_index ) );
+ }
+
+ if ( ! array_key_exists( 'eraser_friendly_name', $eraser ) ) {
+ /* translators: %d: Eraser array index. */
+ wp_send_json_error( sprintf( __( 'Eraser array at index %d does not include a friendly name.' ), $eraser_index ) );
+ }
+
+ $eraser_friendly_name = $eraser['eraser_friendly_name'];
+
+ if ( ! array_key_exists( 'callback', $eraser ) ) {
+ wp_send_json_error(
+ sprintf(
+ /* translators: %s: Eraser friendly name. */
+ __( 'Eraser does not include a callback: %s.' ),
+ esc_html( $eraser_friendly_name )
+ )
+ );
+ }
+
+ if ( ! is_callable( $eraser['callback'] ) ) {
+ wp_send_json_error(
+ sprintf(
+ /* translators: %s: Eraser friendly name. */
+ __( 'Eraser callback is not valid: %s.' ),
+ esc_html( $eraser_friendly_name )
+ )
+ );
+ }
+
+ $callback = $eraser['callback'];
+ $response = call_user_func( $callback, $email_address, $page );
+
+ if ( is_wp_error( $response ) ) {
+ wp_send_json_error( $response );
+ }
+
+ if ( ! is_array( $response ) ) {
+ wp_send_json_error(
+ sprintf(
+ /* translators: 1: Eraser friendly name, 2: Eraser array index. */
+ __( 'Did not receive array from %1$s eraser (index %2$d).' ),
+ esc_html( $eraser_friendly_name ),
+ $eraser_index
+ )
+ );
+ }
+
+ if ( ! array_key_exists( 'items_removed', $response ) ) {
+ wp_send_json_error(
+ sprintf(
+ /* translators: 1: Eraser friendly name, 2: Eraser array index. */
+ __( 'Expected items_removed key in response array from %1$s eraser (index %2$d).' ),
+ esc_html( $eraser_friendly_name ),
+ $eraser_index
+ )
+ );
+ }
+
+ if ( ! array_key_exists( 'items_retained', $response ) ) {
+ wp_send_json_error(
+ sprintf(
+ /* translators: 1: Eraser friendly name, 2: Eraser array index. */
+ __( 'Expected items_retained key in response array from %1$s eraser (index %2$d).' ),
+ esc_html( $eraser_friendly_name ),
+ $eraser_index
+ )
+ );
+ }
+
+ if ( ! array_key_exists( 'messages', $response ) ) {
+ wp_send_json_error(
+ sprintf(
+ /* translators: 1: Eraser friendly name, 2: Eraser array index. */
+ __( 'Expected messages key in response array from %1$s eraser (index %2$d).' ),
+ esc_html( $eraser_friendly_name ),
+ $eraser_index
+ )
+ );
+ }
+
+ if ( ! is_array( $response['messages'] ) ) {
+ wp_send_json_error(
+ sprintf(
+ /* translators: 1: Eraser friendly name, 2: Eraser array index. */
+ __( 'Expected messages key to reference an array in response array from %1$s eraser (index %2$d).' ),
+ esc_html( $eraser_friendly_name ),
+ $eraser_index
+ )
+ );
+ }
+
+ if ( ! array_key_exists( 'done', $response ) ) {
+ wp_send_json_error(
+ sprintf(
+ /* translators: 1: Eraser friendly name, 2: Eraser array index. */
+ __( 'Expected done flag in response array from %1$s eraser (index %2$d).' ),
+ esc_html( $eraser_friendly_name ),
+ $eraser_index
+ )
+ );
+ }
+ } else {
+ // No erasers, so we're done.
+ $eraser_key = '';
+
+ $response = array(
+ 'items_removed' => false,
+ 'items_retained' => false,
+ 'messages' => array(),
+ 'done' => true,
+ );
+ }
+
+ /**
+ * Filters a page of personal data eraser data.
+ *
+ * Allows the erasure response to be consumed by destinations in addition to Ajax.
+ *
+ * @since 4.9.6
+ *
+ * @param array $response {
+ * The personal data for the given exporter and page number.
+ *
+ * @type bool $items_removed Whether items were actually removed or not.
+ * @type bool $items_retained Whether items were retained or not.
+ * @type string[] $messages An array of messages to add to the personal data export file.
+ * @type bool $done Whether the eraser is finished or not.
+ * }
+ * @param int $eraser_index The index of the eraser that provided this data.
+ * @param string $email_address The email address associated with this personal data.
+ * @param int $page The page number for this response.
+ * @param int $request_id The privacy request post ID associated with this request.
+ * @param string $eraser_key The key (slug) of the eraser that provided this data.
+ */
+ $response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id, $eraser_key );
+
+ if ( is_wp_error( $response ) ) {
+ wp_send_json_error( $response );
+ }
+
+ wp_send_json_success( $response );
+}
+
+/**
+ * Handles site health checks on server communication via AJAX.
+ *
+ * @since 5.2.0
+ * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_dotorg_communication()
+ * @see WP_REST_Site_Health_Controller::test_dotorg_communication()
+ */
+function wp_ajax_health_check_dotorg_communication() {
+ _doing_it_wrong(
+ 'wp_ajax_health_check_dotorg_communication',
+ sprintf(
+ // translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
+ __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
+ 'wp_ajax_health_check_dotorg_communication',
+ 'WP_REST_Site_Health_Controller::test_dotorg_communication'
+ ),
+ '5.6.0'
+ );
+
+ check_ajax_referer( 'health-check-site-status' );
+
+ if ( ! current_user_can( 'view_site_health_checks' ) ) {
+ wp_send_json_error();
+ }
+
+ if ( ! class_exists( 'WP_Site_Health' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
+ }
+
+ $site_health = WP_Site_Health::get_instance();
+ wp_send_json_success( $site_health->get_test_dotorg_communication() );
+}
+
+/**
+ * Handles site health checks on background updates via AJAX.
+ *
+ * @since 5.2.0
+ * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_background_updates()
+ * @see WP_REST_Site_Health_Controller::test_background_updates()
+ */
+function wp_ajax_health_check_background_updates() {
+ _doing_it_wrong(
+ 'wp_ajax_health_check_background_updates',
+ sprintf(
+ // translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
+ __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
+ 'wp_ajax_health_check_background_updates',
+ 'WP_REST_Site_Health_Controller::test_background_updates'
+ ),
+ '5.6.0'
+ );
+
+ check_ajax_referer( 'health-check-site-status' );
+
+ if ( ! current_user_can( 'view_site_health_checks' ) ) {
+ wp_send_json_error();
+ }
+
+ if ( ! class_exists( 'WP_Site_Health' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
+ }
+
+ $site_health = WP_Site_Health::get_instance();
+ wp_send_json_success( $site_health->get_test_background_updates() );
+}
+
+/**
+ * Handles site health checks on loopback requests via AJAX.
+ *
+ * @since 5.2.0
+ * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_loopback_requests()
+ * @see WP_REST_Site_Health_Controller::test_loopback_requests()
+ */
+function wp_ajax_health_check_loopback_requests() {
+ _doing_it_wrong(
+ 'wp_ajax_health_check_loopback_requests',
+ sprintf(
+ // translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
+ __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
+ 'wp_ajax_health_check_loopback_requests',
+ 'WP_REST_Site_Health_Controller::test_loopback_requests'
+ ),
+ '5.6.0'
+ );
+
+ check_ajax_referer( 'health-check-site-status' );
+
+ if ( ! current_user_can( 'view_site_health_checks' ) ) {
+ wp_send_json_error();
+ }
+
+ if ( ! class_exists( 'WP_Site_Health' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
+ }
+
+ $site_health = WP_Site_Health::get_instance();
+ wp_send_json_success( $site_health->get_test_loopback_requests() );
+}
+
+/**
+ * Handles site health check to update the result status via AJAX.
+ *
+ * @since 5.2.0
+ */
+function wp_ajax_health_check_site_status_result() {
+ check_ajax_referer( 'health-check-site-status-result' );
+
+ if ( ! current_user_can( 'view_site_health_checks' ) ) {
+ wp_send_json_error();
+ }
+
+ set_transient( 'health-check-site-status-result', wp_json_encode( $_POST['counts'] ) );
+
+ wp_send_json_success();
+}
+
+/**
+ * Handles site health check to get directories and database sizes via AJAX.
+ *
+ * @since 5.2.0
+ * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::get_directory_sizes()
+ * @see WP_REST_Site_Health_Controller::get_directory_sizes()
+ */
+function wp_ajax_health_check_get_sizes() {
+ _doing_it_wrong(
+ 'wp_ajax_health_check_get_sizes',
+ sprintf(
+ // translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
+ __( 'The Site Health check for %1$s has been replaced with %2$s.' ),
+ 'wp_ajax_health_check_get_sizes',
+ 'WP_REST_Site_Health_Controller::get_directory_sizes'
+ ),
+ '5.6.0'
+ );
+
+ check_ajax_referer( 'health-check-site-status-result' );
+
+ if ( ! current_user_can( 'view_site_health_checks' ) || is_multisite() ) {
+ wp_send_json_error();
+ }
+
+ if ( ! class_exists( 'WP_Debug_Data' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
+ }
+
+ $sizes_data = WP_Debug_Data::get_sizes();
+ $all_sizes = array( 'raw' => 0 );
+
+ foreach ( $sizes_data as $name => $value ) {
+ $name = sanitize_text_field( $name );
+ $data = array();
+
+ if ( isset( $value['size'] ) ) {
+ if ( is_string( $value['size'] ) ) {
+ $data['size'] = sanitize_text_field( $value['size'] );
+ } else {
+ $data['size'] = (int) $value['size'];
+ }
+ }
+
+ if ( isset( $value['debug'] ) ) {
+ if ( is_string( $value['debug'] ) ) {
+ $data['debug'] = sanitize_text_field( $value['debug'] );
+ } else {
+ $data['debug'] = (int) $value['debug'];
+ }
+ }
+
+ if ( ! empty( $value['raw'] ) ) {
+ $data['raw'] = (int) $value['raw'];
+ }
+
+ $all_sizes[ $name ] = $data;
+ }
+
+ if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
+ wp_send_json_error( $all_sizes );
+ }
+
+ wp_send_json_success( $all_sizes );
+}
+
+/**
+ * Handles renewing the REST API nonce via AJAX.
+ *
+ * @since 5.3.0
+ */
+function wp_ajax_rest_nonce() {
+ exit( wp_create_nonce( 'wp_rest' ) );
+}
+
+/**
+ * Handles enabling or disable plugin and theme auto-updates via AJAX.
+ *
+ * @since 5.5.0
+ */
+function wp_ajax_toggle_auto_updates() {
+ check_ajax_referer( 'updates' );
+
+ if ( empty( $_POST['type'] ) || empty( $_POST['asset'] ) || empty( $_POST['state'] ) ) {
+ wp_send_json_error( array( 'error' => __( 'Invalid data. No selected item.' ) ) );
+ }
+
+ $asset = sanitize_text_field( urldecode( $_POST['asset'] ) );
+
+ if ( 'enable' !== $_POST['state'] && 'disable' !== $_POST['state'] ) {
+ wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown state.' ) ) );
+ }
+ $state = $_POST['state'];
+
+ if ( 'plugin' !== $_POST['type'] && 'theme' !== $_POST['type'] ) {
+ wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown type.' ) ) );
+ }
+ $type = $_POST['type'];
+
+ switch ( $type ) {
+ case 'plugin':
+ if ( ! current_user_can( 'update_plugins' ) ) {
+ $error_message = __( 'Sorry, you are not allowed to modify plugins.' );
+ wp_send_json_error( array( 'error' => $error_message ) );
+ }
+
+ $option = 'auto_update_plugins';
+ /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
+ $all_items = apply_filters( 'all_plugins', get_plugins() );
+ break;
+ case 'theme':
+ if ( ! current_user_can( 'update_themes' ) ) {
+ $error_message = __( 'Sorry, you are not allowed to modify themes.' );
+ wp_send_json_error( array( 'error' => $error_message ) );
+ }
+
+ $option = 'auto_update_themes';
+ $all_items = wp_get_themes();
+ break;
+ default:
+ wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown type.' ) ) );
+ }
+
+ if ( ! array_key_exists( $asset, $all_items ) ) {
+ $error_message = __( 'Invalid data. The item does not exist.' );
+ wp_send_json_error( array( 'error' => $error_message ) );
+ }
+
+ $auto_updates = (array) get_site_option( $option, array() );
+
+ if ( 'disable' === $state ) {
+ $auto_updates = array_diff( $auto_updates, array( $asset ) );
+ } else {
+ $auto_updates[] = $asset;
+ $auto_updates = array_unique( $auto_updates );
+ }
+
+ // Remove items that have been deleted since the site option was last updated.
+ $auto_updates = array_intersect( $auto_updates, array_keys( $all_items ) );
+
+ update_site_option( $option, $auto_updates );
+
+ wp_send_json_success();
+}
+
+/**
+ * Handles sending a password reset link via AJAX.
+ *
+ * @since 5.7.0
+ */
+function wp_ajax_send_password_reset() {
+
+ // Validate the nonce for this action.
+ $user_id = isset( $_POST['user_id'] ) ? (int) $_POST['user_id'] : 0;
+ check_ajax_referer( 'reset-password-for-' . $user_id, 'nonce' );
+
+ // Verify user capabilities.
+ if ( ! current_user_can( 'edit_user', $user_id ) ) {
+ wp_send_json_error( __( 'Cannot send password reset, permission denied.' ) );
+ }
+
+ // Send the password reset link.
+ $user = get_userdata( $user_id );
+ $results = retrieve_password( $user->user_login );
+
+ if ( true === $results ) {
+ wp_send_json_success(
+ /* translators: %s: User's display name. */
+ sprintf( __( 'A password reset link was emailed to %s.' ), $user->display_name )
+ );
+ } else {
+ wp_send_json_error( $results->get_error_message() );
+ }
+}
diff --git a/wp-admin/includes/bookmark.php b/wp-admin/includes/bookmark.php
new file mode 100644
index 0000000..c5600bf
--- /dev/null
+++ b/wp-admin/includes/bookmark.php
@@ -0,0 +1,379 @@
+' . __( 'You need a higher level of permission.' ) . '' .
+ '' . __( 'Sorry, you are not allowed to edit the links for this site.' ) . '
',
+ 403
+ );
+ }
+
+ $_POST['link_url'] = esc_url( $_POST['link_url'] );
+ $_POST['link_name'] = esc_html( $_POST['link_name'] );
+ $_POST['link_image'] = esc_html( $_POST['link_image'] );
+ $_POST['link_rss'] = esc_url( $_POST['link_rss'] );
+ if ( ! isset( $_POST['link_visible'] ) || 'N' !== $_POST['link_visible'] ) {
+ $_POST['link_visible'] = 'Y';
+ }
+
+ if ( ! empty( $link_id ) ) {
+ $_POST['link_id'] = $link_id;
+ return wp_update_link( $_POST );
+ } else {
+ return wp_insert_link( $_POST );
+ }
+}
+
+/**
+ * Retrieves the default link for editing.
+ *
+ * @since 2.0.0
+ *
+ * @return stdClass Default link object.
+ */
+function get_default_link_to_edit() {
+ $link = new stdClass();
+ if ( isset( $_GET['linkurl'] ) ) {
+ $link->link_url = esc_url( wp_unslash( $_GET['linkurl'] ) );
+ } else {
+ $link->link_url = '';
+ }
+
+ if ( isset( $_GET['name'] ) ) {
+ $link->link_name = esc_attr( wp_unslash( $_GET['name'] ) );
+ } else {
+ $link->link_name = '';
+ }
+
+ $link->link_visible = 'Y';
+
+ return $link;
+}
+
+/**
+ * Deletes a specified link from the database.
+ *
+ * @since 2.0.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int $link_id ID of the link to delete.
+ * @return true Always true.
+ */
+function wp_delete_link( $link_id ) {
+ global $wpdb;
+ /**
+ * Fires before a link is deleted.
+ *
+ * @since 2.0.0
+ *
+ * @param int $link_id ID of the link to delete.
+ */
+ do_action( 'delete_link', $link_id );
+
+ wp_delete_object_term_relationships( $link_id, 'link_category' );
+
+ $wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );
+
+ /**
+ * Fires after a link has been deleted.
+ *
+ * @since 2.2.0
+ *
+ * @param int $link_id ID of the deleted link.
+ */
+ do_action( 'deleted_link', $link_id );
+
+ clean_bookmark_cache( $link_id );
+
+ return true;
+}
+
+/**
+ * Retrieves the link category IDs associated with the link specified.
+ *
+ * @since 2.1.0
+ *
+ * @param int $link_id Link ID to look up.
+ * @return int[] The IDs of the requested link's categories.
+ */
+function wp_get_link_cats( $link_id = 0 ) {
+ $cats = wp_get_object_terms( $link_id, 'link_category', array( 'fields' => 'ids' ) );
+ return array_unique( $cats );
+}
+
+/**
+ * Retrieves link data based on its ID.
+ *
+ * @since 2.0.0
+ *
+ * @param int|stdClass $link Link ID or object to retrieve.
+ * @return object Link object for editing.
+ */
+function get_link_to_edit( $link ) {
+ return get_bookmark( $link, OBJECT, 'edit' );
+}
+
+/**
+ * Inserts a link into the database, or updates an existing link.
+ *
+ * Runs all the necessary sanitizing, provides default values if arguments are missing,
+ * and finally saves the link.
+ *
+ * @since 2.0.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param array $linkdata {
+ * Elements that make up the link to insert.
+ *
+ * @type int $link_id Optional. The ID of the existing link if updating.
+ * @type string $link_url The URL the link points to.
+ * @type string $link_name The title of the link.
+ * @type string $link_image Optional. A URL of an image.
+ * @type string $link_target Optional. The target element for the anchor tag.
+ * @type string $link_description Optional. A short description of the link.
+ * @type string $link_visible Optional. 'Y' means visible, anything else means not.
+ * @type int $link_owner Optional. A user ID.
+ * @type int $link_rating Optional. A rating for the link.
+ * @type string $link_rel Optional. A relationship of the link to you.
+ * @type string $link_notes Optional. An extended description of or notes on the link.
+ * @type string $link_rss Optional. A URL of an associated RSS feed.
+ * @type int $link_category Optional. The term ID of the link category.
+ * If empty, uses default link category.
+ * }
+ * @param bool $wp_error Optional. Whether to return a WP_Error object on failure. Default false.
+ * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
+ */
+function wp_insert_link( $linkdata, $wp_error = false ) {
+ global $wpdb;
+
+ $defaults = array(
+ 'link_id' => 0,
+ 'link_name' => '',
+ 'link_url' => '',
+ 'link_rating' => 0,
+ );
+
+ $parsed_args = wp_parse_args( $linkdata, $defaults );
+ $parsed_args = wp_unslash( sanitize_bookmark( $parsed_args, 'db' ) );
+
+ $link_id = $parsed_args['link_id'];
+ $link_name = $parsed_args['link_name'];
+ $link_url = $parsed_args['link_url'];
+
+ $update = false;
+ if ( ! empty( $link_id ) ) {
+ $update = true;
+ }
+
+ if ( '' === trim( $link_name ) ) {
+ if ( '' !== trim( $link_url ) ) {
+ $link_name = $link_url;
+ } else {
+ return 0;
+ }
+ }
+
+ if ( '' === trim( $link_url ) ) {
+ return 0;
+ }
+
+ $link_rating = ( ! empty( $parsed_args['link_rating'] ) ) ? $parsed_args['link_rating'] : 0;
+ $link_image = ( ! empty( $parsed_args['link_image'] ) ) ? $parsed_args['link_image'] : '';
+ $link_target = ( ! empty( $parsed_args['link_target'] ) ) ? $parsed_args['link_target'] : '';
+ $link_visible = ( ! empty( $parsed_args['link_visible'] ) ) ? $parsed_args['link_visible'] : 'Y';
+ $link_owner = ( ! empty( $parsed_args['link_owner'] ) ) ? $parsed_args['link_owner'] : get_current_user_id();
+ $link_notes = ( ! empty( $parsed_args['link_notes'] ) ) ? $parsed_args['link_notes'] : '';
+ $link_description = ( ! empty( $parsed_args['link_description'] ) ) ? $parsed_args['link_description'] : '';
+ $link_rss = ( ! empty( $parsed_args['link_rss'] ) ) ? $parsed_args['link_rss'] : '';
+ $link_rel = ( ! empty( $parsed_args['link_rel'] ) ) ? $parsed_args['link_rel'] : '';
+ $link_category = ( ! empty( $parsed_args['link_category'] ) ) ? $parsed_args['link_category'] : array();
+
+ // Make sure we set a valid category.
+ if ( ! is_array( $link_category ) || 0 === count( $link_category ) ) {
+ $link_category = array( get_option( 'default_link_category' ) );
+ }
+
+ if ( $update ) {
+ if ( false === $wpdb->update( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ), compact( 'link_id' ) ) ) {
+ if ( $wp_error ) {
+ return new WP_Error( 'db_update_error', __( 'Could not update link in the database.' ), $wpdb->last_error );
+ } else {
+ return 0;
+ }
+ }
+ } else {
+ if ( false === $wpdb->insert( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ) ) ) {
+ if ( $wp_error ) {
+ return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database.' ), $wpdb->last_error );
+ } else {
+ return 0;
+ }
+ }
+ $link_id = (int) $wpdb->insert_id;
+ }
+
+ wp_set_link_cats( $link_id, $link_category );
+
+ if ( $update ) {
+ /**
+ * Fires after a link was updated in the database.
+ *
+ * @since 2.0.0
+ *
+ * @param int $link_id ID of the link that was updated.
+ */
+ do_action( 'edit_link', $link_id );
+ } else {
+ /**
+ * Fires after a link was added to the database.
+ *
+ * @since 2.0.0
+ *
+ * @param int $link_id ID of the link that was added.
+ */
+ do_action( 'add_link', $link_id );
+ }
+ clean_bookmark_cache( $link_id );
+
+ return $link_id;
+}
+
+/**
+ * Updates link with the specified link categories.
+ *
+ * @since 2.1.0
+ *
+ * @param int $link_id ID of the link to update.
+ * @param int[] $link_categories Array of link category IDs to add the link to.
+ */
+function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
+ // If $link_categories isn't already an array, make it one:
+ if ( ! is_array( $link_categories ) || 0 === count( $link_categories ) ) {
+ $link_categories = array( get_option( 'default_link_category' ) );
+ }
+
+ $link_categories = array_map( 'intval', $link_categories );
+ $link_categories = array_unique( $link_categories );
+
+ wp_set_object_terms( $link_id, $link_categories, 'link_category' );
+
+ clean_bookmark_cache( $link_id );
+}
+
+/**
+ * Updates a link in the database.
+ *
+ * @since 2.0.0
+ *
+ * @param array $linkdata Link data to update. See wp_insert_link() for accepted arguments.
+ * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
+ */
+function wp_update_link( $linkdata ) {
+ $link_id = (int) $linkdata['link_id'];
+
+ $link = get_bookmark( $link_id, ARRAY_A );
+
+ // Escape data pulled from DB.
+ $link = wp_slash( $link );
+
+ // Passed link category list overwrites existing category list if not empty.
+ if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )
+ && count( $linkdata['link_category'] ) > 0
+ ) {
+ $link_cats = $linkdata['link_category'];
+ } else {
+ $link_cats = $link['link_category'];
+ }
+
+ // Merge old and new fields with new fields overwriting old ones.
+ $linkdata = array_merge( $link, $linkdata );
+ $linkdata['link_category'] = $link_cats;
+
+ return wp_insert_link( $linkdata );
+}
+
+/**
+ * Outputs the 'disabled' message for the WordPress Link Manager.
+ *
+ * @since 3.5.0
+ * @access private
+ *
+ * @global string $pagenow The filename of the current screen.
+ */
+function wp_link_manager_disabled_message() {
+ global $pagenow;
+
+ if ( ! in_array( $pagenow, array( 'link-manager.php', 'link-add.php', 'link.php' ), true ) ) {
+ return;
+ }
+
+ add_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
+ $really_can_manage_links = current_user_can( 'manage_links' );
+ remove_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
+
+ if ( $really_can_manage_links ) {
+ $plugins = get_plugins();
+
+ if ( empty( $plugins['link-manager/link-manager.php'] ) ) {
+ if ( current_user_can( 'install_plugins' ) ) {
+ $install_url = wp_nonce_url(
+ self_admin_url( 'update.php?action=install-plugin&plugin=link-manager' ),
+ 'install-plugin_link-manager'
+ );
+
+ wp_die(
+ sprintf(
+ /* translators: %s: A link to install the Link Manager plugin. */
+ __( 'If you are looking to use the link manager, please install the Link Manager plugin .' ),
+ esc_url( $install_url )
+ )
+ );
+ }
+ } elseif ( is_plugin_inactive( 'link-manager/link-manager.php' ) ) {
+ if ( current_user_can( 'activate_plugins' ) ) {
+ $activate_url = wp_nonce_url(
+ self_admin_url( 'plugins.php?action=activate&plugin=link-manager/link-manager.php' ),
+ 'activate-plugin_link-manager/link-manager.php'
+ );
+
+ wp_die(
+ sprintf(
+ /* translators: %s: A link to activate the Link Manager plugin. */
+ __( 'Please activate the Link Manager plugin to use the link manager.' ),
+ esc_url( $activate_url )
+ )
+ );
+ }
+ }
+ }
+
+ wp_die( __( 'Sorry, you are not allowed to edit the links for this site.' ) );
+}
diff --git a/wp-admin/includes/class-automatic-upgrader-skin.php b/wp-admin/includes/class-automatic-upgrader-skin.php
new file mode 100644
index 0000000..4fee620
--- /dev/null
+++ b/wp-admin/includes/class-automatic-upgrader-skin.php
@@ -0,0 +1,135 @@
+options['context'] = $context;
+ }
+ /*
+ * TODO: Fix up request_filesystem_credentials(), or split it, to allow us to request a no-output version.
+ * This will output a credentials form in event of failure. We don't want that, so just hide with a buffer.
+ */
+ ob_start();
+ $result = parent::request_filesystem_credentials( $error, $context, $allow_relaxed_file_ownership );
+ ob_end_clean();
+ return $result;
+ }
+
+ /**
+ * Retrieves the upgrade messages.
+ *
+ * @since 3.7.0
+ *
+ * @return string[] Messages during an upgrade.
+ */
+ public function get_upgrade_messages() {
+ return $this->messages;
+ }
+
+ /**
+ * Stores a message about the upgrade.
+ *
+ * @since 3.7.0
+ * @since 5.9.0 Renamed `$data` to `$feedback` for PHP 8 named parameter support.
+ *
+ * @param string|array|WP_Error $feedback Message data.
+ * @param mixed ...$args Optional text replacements.
+ */
+ public function feedback( $feedback, ...$args ) {
+ if ( is_wp_error( $feedback ) ) {
+ $string = $feedback->get_error_message();
+ } elseif ( is_array( $feedback ) ) {
+ return;
+ } else {
+ $string = $feedback;
+ }
+
+ if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
+ $string = $this->upgrader->strings[ $string ];
+ }
+
+ if ( str_contains( $string, '%' ) ) {
+ if ( ! empty( $args ) ) {
+ $string = vsprintf( $string, $args );
+ }
+ }
+
+ $string = trim( $string );
+
+ // Only allow basic HTML in the messages, as it'll be used in emails/logs rather than direct browser output.
+ $string = wp_kses(
+ $string,
+ array(
+ 'a' => array(
+ 'href' => true,
+ ),
+ 'br' => true,
+ 'em' => true,
+ 'strong' => true,
+ )
+ );
+
+ if ( empty( $string ) ) {
+ return;
+ }
+
+ $this->messages[] = $string;
+ }
+
+ /**
+ * Creates a new output buffer.
+ *
+ * @since 3.7.0
+ */
+ public function header() {
+ ob_start();
+ }
+
+ /**
+ * Retrieves the buffered content, deletes the buffer, and processes the output.
+ *
+ * @since 3.7.0
+ */
+ public function footer() {
+ $output = ob_get_clean();
+ if ( ! empty( $output ) ) {
+ $this->feedback( $output );
+ }
+ }
+}
diff --git a/wp-admin/includes/class-bulk-plugin-upgrader-skin.php b/wp-admin/includes/class-bulk-plugin-upgrader-skin.php
new file mode 100644
index 0000000..7cbf334
--- /dev/null
+++ b/wp-admin/includes/class-bulk-plugin-upgrader-skin.php
@@ -0,0 +1,87 @@
+upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)' );
+ }
+
+ /**
+ * @param string $title
+ */
+ public function before( $title = '' ) {
+ parent::before( $this->plugin_info['Title'] );
+ }
+
+ /**
+ * @param string $title
+ */
+ public function after( $title = '' ) {
+ parent::after( $this->plugin_info['Title'] );
+ $this->decrement_update_count( 'plugin' );
+ }
+
+ /**
+ */
+ public function bulk_footer() {
+ parent::bulk_footer();
+
+ $update_actions = array(
+ 'plugins_page' => sprintf(
+ '%s ',
+ self_admin_url( 'plugins.php' ),
+ __( 'Go to Plugins page' )
+ ),
+ 'updates_page' => sprintf(
+ '%s ',
+ self_admin_url( 'update-core.php' ),
+ __( 'Go to WordPress Updates page' )
+ ),
+ );
+
+ if ( ! current_user_can( 'activate_plugins' ) ) {
+ unset( $update_actions['plugins_page'] );
+ }
+
+ /**
+ * Filters the list of action links available following bulk plugin updates.
+ *
+ * @since 3.0.0
+ *
+ * @param string[] $update_actions Array of plugin action links.
+ * @param array $plugin_info Array of information for the last-updated plugin.
+ */
+ $update_actions = apply_filters( 'update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
+
+ if ( ! empty( $update_actions ) ) {
+ $this->feedback( implode( ' | ', (array) $update_actions ) );
+ }
+ }
+}
diff --git a/wp-admin/includes/class-bulk-theme-upgrader-skin.php b/wp-admin/includes/class-bulk-theme-upgrader-skin.php
new file mode 100644
index 0000000..8ec3bbf
--- /dev/null
+++ b/wp-admin/includes/class-bulk-theme-upgrader-skin.php
@@ -0,0 +1,88 @@
+upgrader->strings['skin_before_update_header'] = __( 'Updating Theme %1$s (%2$d/%3$d)' );
+ }
+
+ /**
+ * @param string $title
+ */
+ public function before( $title = '' ) {
+ parent::before( $this->theme_info->display( 'Name' ) );
+ }
+
+ /**
+ * @param string $title
+ */
+ public function after( $title = '' ) {
+ parent::after( $this->theme_info->display( 'Name' ) );
+ $this->decrement_update_count( 'theme' );
+ }
+
+ /**
+ */
+ public function bulk_footer() {
+ parent::bulk_footer();
+
+ $update_actions = array(
+ 'themes_page' => sprintf(
+ '%s ',
+ self_admin_url( 'themes.php' ),
+ __( 'Go to Themes page' )
+ ),
+ 'updates_page' => sprintf(
+ '%s ',
+ self_admin_url( 'update-core.php' ),
+ __( 'Go to WordPress Updates page' )
+ ),
+ );
+
+ if ( ! current_user_can( 'switch_themes' ) && ! current_user_can( 'edit_theme_options' ) ) {
+ unset( $update_actions['themes_page'] );
+ }
+
+ /**
+ * Filters the list of action links available following bulk theme updates.
+ *
+ * @since 3.0.0
+ *
+ * @param string[] $update_actions Array of theme action links.
+ * @param WP_Theme $theme_info Theme object for the last-updated theme.
+ */
+ $update_actions = apply_filters( 'update_bulk_theme_complete_actions', $update_actions, $this->theme_info );
+
+ if ( ! empty( $update_actions ) ) {
+ $this->feedback( implode( ' | ', (array) $update_actions ) );
+ }
+ }
+}
diff --git a/wp-admin/includes/class-bulk-upgrader-skin.php b/wp-admin/includes/class-bulk-upgrader-skin.php
new file mode 100644
index 0000000..4613119
--- /dev/null
+++ b/wp-admin/includes/class-bulk-upgrader-skin.php
@@ -0,0 +1,187 @@
+ '',
+ 'nonce' => '',
+ );
+ $args = wp_parse_args( $args, $defaults );
+
+ parent::__construct( $args );
+ }
+
+ /**
+ */
+ public function add_strings() {
+ $this->upgrader->strings['skin_upgrade_start'] = __( 'The update process is starting. This process may take a while on some hosts, so please be patient.' );
+ /* translators: 1: Title of an update, 2: Error message. */
+ $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while updating %1$s: %2$s' );
+ /* translators: %s: Title of an update. */
+ $this->upgrader->strings['skin_update_failed'] = __( 'The update of %s failed.' );
+ /* translators: %s: Title of an update. */
+ $this->upgrader->strings['skin_update_successful'] = __( '%s updated successfully.' );
+ $this->upgrader->strings['skin_upgrade_end'] = __( 'All updates have been completed.' );
+ }
+
+ /**
+ * @since 5.9.0 Renamed `$string` (a PHP reserved keyword) to `$feedback` for PHP 8 named parameter support.
+ *
+ * @param string $feedback Message data.
+ * @param mixed ...$args Optional text replacements.
+ */
+ public function feedback( $feedback, ...$args ) {
+ if ( isset( $this->upgrader->strings[ $feedback ] ) ) {
+ $feedback = $this->upgrader->strings[ $feedback ];
+ }
+
+ if ( str_contains( $feedback, '%' ) ) {
+ if ( $args ) {
+ $args = array_map( 'strip_tags', $args );
+ $args = array_map( 'esc_html', $args );
+ $feedback = vsprintf( $feedback, $args );
+ }
+ }
+ if ( empty( $feedback ) ) {
+ return;
+ }
+ if ( $this->in_loop ) {
+ echo "$feedback \n";
+ } else {
+ echo "$feedback
\n";
+ }
+ }
+
+ /**
+ */
+ public function header() {
+ // Nothing. This will be displayed within an iframe.
+ }
+
+ /**
+ */
+ public function footer() {
+ // Nothing. This will be displayed within an iframe.
+ }
+
+ /**
+ * @since 5.9.0 Renamed `$error` to `$errors` for PHP 8 named parameter support.
+ *
+ * @param string|WP_Error $errors Errors.
+ */
+ public function error( $errors ) {
+ if ( is_string( $errors ) && isset( $this->upgrader->strings[ $errors ] ) ) {
+ $this->error = $this->upgrader->strings[ $errors ];
+ }
+
+ if ( is_wp_error( $errors ) ) {
+ $messages = array();
+ foreach ( $errors->get_error_messages() as $emessage ) {
+ if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
+ $messages[] = $emessage . ' ' . esc_html( strip_tags( $errors->get_error_data() ) );
+ } else {
+ $messages[] = $emessage;
+ }
+ }
+ $this->error = implode( ', ', $messages );
+ }
+ echo '';
+ }
+
+ /**
+ */
+ public function bulk_header() {
+ $this->feedback( 'skin_upgrade_start' );
+ }
+
+ /**
+ */
+ public function bulk_footer() {
+ $this->feedback( 'skin_upgrade_end' );
+ }
+
+ /**
+ * @param string $title
+ */
+ public function before( $title = '' ) {
+ $this->in_loop = true;
+ printf( '' . $this->upgrader->strings['skin_before_update_header'] . ' ', $title, $this->upgrader->update_current, $this->upgrader->update_count );
+ echo '';
+ // This progress messages div gets moved via JavaScript when clicking on "More details.".
+ echo '';
+ $this->flush_output();
+ }
+
+ /**
+ * @param string $title
+ */
+ public function after( $title = '' ) {
+ echo '
';
+ if ( $this->error || ! $this->result ) {
+ if ( $this->error ) {
+ $after_error_message = sprintf( $this->upgrader->strings['skin_update_failed_error'], $title, '' . $this->error . ' ' );
+ } else {
+ $after_error_message = sprintf( $this->upgrader->strings['skin_update_failed'], $title );
+ }
+ wp_admin_notice(
+ $after_error_message,
+ array(
+ 'additional_classes' => array( 'error' ),
+ )
+ );
+
+ echo '';
+ }
+ if ( $this->result && ! is_wp_error( $this->result ) ) {
+ if ( ! $this->error ) {
+ echo '' .
+ '
' . sprintf( $this->upgrader->strings['skin_update_successful'], $title ) .
+ ' ' . __( 'More details.' ) . ' ' .
+ '
';
+ }
+
+ echo '';
+ }
+
+ $this->reset();
+ $this->flush_output();
+ }
+
+ /**
+ */
+ public function reset() {
+ $this->in_loop = false;
+ $this->error = false;
+ }
+
+ /**
+ */
+ public function flush_output() {
+ wp_ob_end_flush_all();
+ flush();
+ }
+}
diff --git a/wp-admin/includes/class-core-upgrader.php b/wp-admin/includes/class-core-upgrader.php
new file mode 100644
index 0000000..165e1f7
--- /dev/null
+++ b/wp-admin/includes/class-core-upgrader.php
@@ -0,0 +1,420 @@
+strings['up_to_date'] = __( 'WordPress is at the latest version.' );
+ $this->strings['locked'] = __( 'Another update is currently in progress.' );
+ $this->strings['no_package'] = __( 'Update package not available.' );
+ /* translators: %s: Package URL. */
+ $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s…' ), '%s ' );
+ $this->strings['unpack_package'] = __( 'Unpacking the update…' );
+ $this->strings['copy_failed'] = __( 'Could not copy files.' );
+ $this->strings['copy_failed_space'] = __( 'Could not copy files. You may have run out of disk space.' );
+ $this->strings['start_rollback'] = __( 'Attempting to restore the previous version.' );
+ $this->strings['rollback_was_required'] = __( 'Due to an error during updating, WordPress has been restored to your previous version.' );
+ }
+
+ /**
+ * Upgrades WordPress core.
+ *
+ * @since 2.8.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ * @global callable $_wp_filesystem_direct_method
+ *
+ * @param object $current Response object for whether WordPress is current.
+ * @param array $args {
+ * Optional. Arguments for upgrading WordPress core. Default empty array.
+ *
+ * @type bool $pre_check_md5 Whether to check the file checksums before
+ * attempting the upgrade. Default true.
+ * @type bool $attempt_rollback Whether to attempt to rollback the chances if
+ * there is a problem. Default false.
+ * @type bool $do_rollback Whether to perform this "upgrade" as a rollback.
+ * Default false.
+ * }
+ * @return string|false|WP_Error New WordPress version on success, false or WP_Error on failure.
+ */
+ public function upgrade( $current, $args = array() ) {
+ global $wp_filesystem;
+
+ require ABSPATH . WPINC . '/version.php'; // $wp_version;
+
+ $start_time = time();
+
+ $defaults = array(
+ 'pre_check_md5' => true,
+ 'attempt_rollback' => false,
+ 'do_rollback' => false,
+ 'allow_relaxed_file_ownership' => false,
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->upgrade_strings();
+
+ // Is an update available?
+ if ( ! isset( $current->response ) || 'latest' === $current->response ) {
+ return new WP_Error( 'up_to_date', $this->strings['up_to_date'] );
+ }
+
+ $res = $this->fs_connect( array( ABSPATH, WP_CONTENT_DIR ), $parsed_args['allow_relaxed_file_ownership'] );
+ if ( ! $res || is_wp_error( $res ) ) {
+ return $res;
+ }
+
+ $wp_dir = trailingslashit( $wp_filesystem->abspath() );
+
+ $partial = true;
+ if ( $parsed_args['do_rollback'] ) {
+ $partial = false;
+ } elseif ( $parsed_args['pre_check_md5'] && ! $this->check_files() ) {
+ $partial = false;
+ }
+
+ /*
+ * If partial update is returned from the API, use that, unless we're doing
+ * a reinstallation. If we cross the new_bundled version number, then use
+ * the new_bundled zip. Don't though if the constant is set to skip bundled items.
+ * If the API returns a no_content zip, go with it. Finally, default to the full zip.
+ */
+ if ( $parsed_args['do_rollback'] && $current->packages->rollback ) {
+ $to_download = 'rollback';
+ } elseif ( $current->packages->partial && 'reinstall' !== $current->response && $wp_version === $current->partial_version && $partial ) {
+ $to_download = 'partial';
+ } elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' )
+ && ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) ) {
+ $to_download = 'new_bundled';
+ } elseif ( $current->packages->no_content ) {
+ $to_download = 'no_content';
+ } else {
+ $to_download = 'full';
+ }
+
+ // Lock to prevent multiple Core Updates occurring.
+ $lock = WP_Upgrader::create_lock( 'core_updater', 15 * MINUTE_IN_SECONDS );
+ if ( ! $lock ) {
+ return new WP_Error( 'locked', $this->strings['locked'] );
+ }
+
+ $download = $this->download_package( $current->packages->$to_download, true );
+
+ /*
+ * Allow for signature soft-fail.
+ * WARNING: This may be removed in the future.
+ */
+ if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) {
+ // Output the failure error as a normal feedback, and not as an error:
+ /** This filter is documented in wp-admin/includes/update-core.php */
+ apply_filters( 'update_feedback', $download->get_error_message() );
+
+ // Report this failure back to WordPress.org for debugging purposes.
+ wp_version_check(
+ array(
+ 'signature_failure_code' => $download->get_error_code(),
+ 'signature_failure_data' => $download->get_error_data(),
+ )
+ );
+
+ // Pretend this error didn't happen.
+ $download = $download->get_error_data( 'softfail-filename' );
+ }
+
+ if ( is_wp_error( $download ) ) {
+ WP_Upgrader::release_lock( 'core_updater' );
+ return $download;
+ }
+
+ $working_dir = $this->unpack_package( $download );
+ if ( is_wp_error( $working_dir ) ) {
+ WP_Upgrader::release_lock( 'core_updater' );
+ return $working_dir;
+ }
+
+ // Copy update-core.php from the new version into place.
+ if ( ! $wp_filesystem->copy( $working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true ) ) {
+ $wp_filesystem->delete( $working_dir, true );
+ WP_Upgrader::release_lock( 'core_updater' );
+ return new WP_Error( 'copy_failed_for_update_core_file', __( 'The update cannot be installed because some files could not be copied. This is usually due to inconsistent file permissions.' ), 'wp-admin/includes/update-core.php' );
+ }
+ $wp_filesystem->chmod( $wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE );
+
+ wp_opcache_invalidate( ABSPATH . 'wp-admin/includes/update-core.php' );
+ require_once ABSPATH . 'wp-admin/includes/update-core.php';
+
+ if ( ! function_exists( 'update_core' ) ) {
+ WP_Upgrader::release_lock( 'core_updater' );
+ return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] );
+ }
+
+ $result = update_core( $working_dir, $wp_dir );
+
+ // In the event of an issue, we may be able to roll back.
+ if ( $parsed_args['attempt_rollback'] && $current->packages->rollback && ! $parsed_args['do_rollback'] ) {
+ $try_rollback = false;
+ if ( is_wp_error( $result ) ) {
+ $error_code = $result->get_error_code();
+ /*
+ * Not all errors are equal. These codes are critical: copy_failed__copy_dir,
+ * mkdir_failed__copy_dir, copy_failed__copy_dir_retry, and disk_full.
+ * do_rollback allows for update_core() to trigger a rollback if needed.
+ */
+ if ( str_contains( $error_code, 'do_rollback' ) ) {
+ $try_rollback = true;
+ } elseif ( str_contains( $error_code, '__copy_dir' ) ) {
+ $try_rollback = true;
+ } elseif ( 'disk_full' === $error_code ) {
+ $try_rollback = true;
+ }
+ }
+
+ if ( $try_rollback ) {
+ /** This filter is documented in wp-admin/includes/update-core.php */
+ apply_filters( 'update_feedback', $result );
+
+ /** This filter is documented in wp-admin/includes/update-core.php */
+ apply_filters( 'update_feedback', $this->strings['start_rollback'] );
+
+ $rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) );
+
+ $original_result = $result;
+ $result = new WP_Error(
+ 'rollback_was_required',
+ $this->strings['rollback_was_required'],
+ (object) array(
+ 'update' => $original_result,
+ 'rollback' => $rollback_result,
+ )
+ );
+ }
+ }
+
+ /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
+ do_action(
+ 'upgrader_process_complete',
+ $this,
+ array(
+ 'action' => 'update',
+ 'type' => 'core',
+ )
+ );
+
+ // Clear the current updates.
+ delete_site_transient( 'update_core' );
+
+ if ( ! $parsed_args['do_rollback'] ) {
+ $stats = array(
+ 'update_type' => $current->response,
+ 'success' => true,
+ 'fs_method' => $wp_filesystem->method,
+ 'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ),
+ 'fs_method_direct' => ! empty( $GLOBALS['_wp_filesystem_direct_method'] ) ? $GLOBALS['_wp_filesystem_direct_method'] : '',
+ 'time_taken' => time() - $start_time,
+ 'reported' => $wp_version,
+ 'attempted' => $current->version,
+ );
+
+ if ( is_wp_error( $result ) ) {
+ $stats['success'] = false;
+ // Did a rollback occur?
+ if ( ! empty( $try_rollback ) ) {
+ $stats['error_code'] = $original_result->get_error_code();
+ $stats['error_data'] = $original_result->get_error_data();
+ // Was the rollback successful? If not, collect its error too.
+ $stats['rollback'] = ! is_wp_error( $rollback_result );
+ if ( is_wp_error( $rollback_result ) ) {
+ $stats['rollback_code'] = $rollback_result->get_error_code();
+ $stats['rollback_data'] = $rollback_result->get_error_data();
+ }
+ } else {
+ $stats['error_code'] = $result->get_error_code();
+ $stats['error_data'] = $result->get_error_data();
+ }
+ }
+
+ wp_version_check( $stats );
+ }
+
+ WP_Upgrader::release_lock( 'core_updater' );
+
+ return $result;
+ }
+
+ /**
+ * Determines if this WordPress Core version should update to an offered version or not.
+ *
+ * @since 3.7.0
+ *
+ * @param string $offered_ver The offered version, of the format x.y.z.
+ * @return bool True if we should update to the offered version, otherwise false.
+ */
+ public static function should_update_to_version( $offered_ver ) {
+ require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
+
+ $current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); // x.y
+ $new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y
+
+ $current_is_development_version = (bool) strpos( $wp_version, '-' );
+
+ // Defaults:
+ $upgrade_dev = get_site_option( 'auto_update_core_dev', 'enabled' ) === 'enabled';
+ $upgrade_minor = get_site_option( 'auto_update_core_minor', 'enabled' ) === 'enabled';
+ $upgrade_major = get_site_option( 'auto_update_core_major', 'unset' ) === 'enabled';
+
+ // WP_AUTO_UPDATE_CORE = true (all), 'beta', 'rc', 'development', 'branch-development', 'minor', false.
+ if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
+ if ( false === WP_AUTO_UPDATE_CORE ) {
+ // Defaults to turned off, unless a filter allows it.
+ $upgrade_dev = false;
+ $upgrade_minor = false;
+ $upgrade_major = false;
+ } elseif ( true === WP_AUTO_UPDATE_CORE
+ || in_array( WP_AUTO_UPDATE_CORE, array( 'beta', 'rc', 'development', 'branch-development' ), true )
+ ) {
+ // ALL updates for core.
+ $upgrade_dev = true;
+ $upgrade_minor = true;
+ $upgrade_major = true;
+ } elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) {
+ // Only minor updates for core.
+ $upgrade_dev = false;
+ $upgrade_minor = true;
+ $upgrade_major = false;
+ }
+ }
+
+ // 1: If we're already on that version, not much point in updating?
+ if ( $offered_ver === $wp_version ) {
+ return false;
+ }
+
+ // 2: If we're running a newer version, that's a nope.
+ if ( version_compare( $wp_version, $offered_ver, '>' ) ) {
+ return false;
+ }
+
+ $failure_data = get_site_option( 'auto_core_update_failed' );
+ if ( $failure_data ) {
+ // If this was a critical update failure, cannot update.
+ if ( ! empty( $failure_data['critical'] ) ) {
+ return false;
+ }
+
+ // Don't claim we can update on update-core.php if we have a non-critical failure logged.
+ if ( $wp_version === $failure_data['current'] && str_contains( $offered_ver, '.1.next.minor' ) ) {
+ return false;
+ }
+
+ /*
+ * Cannot update if we're retrying the same A to B update that caused a non-critical failure.
+ * Some non-critical failures do allow retries, like download_failed.
+ * 3.7.1 => 3.7.2 resulted in files_not_writable, if we are still on 3.7.1 and still trying to update to 3.7.2.
+ */
+ if ( empty( $failure_data['retry'] ) && $wp_version === $failure_data['current'] && $offered_ver === $failure_data['attempted'] ) {
+ return false;
+ }
+ }
+
+ // 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2.
+ if ( $current_is_development_version ) {
+
+ /**
+ * Filters whether to enable automatic core updates for development versions.
+ *
+ * @since 3.7.0
+ *
+ * @param bool $upgrade_dev Whether to enable automatic updates for
+ * development versions.
+ */
+ if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) ) {
+ return false;
+ }
+ // Else fall through to minor + major branches below.
+ }
+
+ // 4: Minor in-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4).
+ if ( $current_branch === $new_branch ) {
+
+ /**
+ * Filters whether to enable minor automatic core updates.
+ *
+ * @since 3.7.0
+ *
+ * @param bool $upgrade_minor Whether to enable minor automatic core updates.
+ */
+ return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor );
+ }
+
+ // 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1).
+ if ( version_compare( $new_branch, $current_branch, '>' ) ) {
+
+ /**
+ * Filters whether to enable major automatic core updates.
+ *
+ * @since 3.7.0
+ *
+ * @param bool $upgrade_major Whether to enable major automatic core updates.
+ */
+ return apply_filters( 'allow_major_auto_core_updates', $upgrade_major );
+ }
+
+ // If we're not sure, we don't want it.
+ return false;
+ }
+
+ /**
+ * Compares the disk file checksums against the expected checksums.
+ *
+ * @since 3.7.0
+ *
+ * @global string $wp_version The WordPress version string.
+ * @global string $wp_local_package Locale code of the package.
+ *
+ * @return bool True if the checksums match, otherwise false.
+ */
+ public function check_files() {
+ global $wp_version, $wp_local_package;
+
+ $checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );
+
+ if ( ! is_array( $checksums ) ) {
+ return false;
+ }
+
+ foreach ( $checksums as $file => $checksum ) {
+ // Skip files which get updated.
+ if ( str_starts_with( $file, 'wp-content' ) ) {
+ continue;
+ }
+ if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum ) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
diff --git a/wp-admin/includes/class-custom-background.php b/wp-admin/includes/class-custom-background.php
new file mode 100644
index 0000000..2eb3ccf
--- /dev/null
+++ b/wp-admin/includes/class-custom-background.php
@@ -0,0 +1,666 @@
+admin_header_callback = $admin_header_callback;
+ $this->admin_image_div_callback = $admin_image_div_callback;
+
+ add_action( 'admin_menu', array( $this, 'init' ) );
+
+ add_action( 'wp_ajax_custom-background-add', array( $this, 'ajax_background_add' ) );
+
+ // Unused since 3.5.0.
+ add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
+ }
+
+ /**
+ * Sets up the hooks for the Custom Background admin page.
+ *
+ * @since 3.0.0
+ */
+ public function init() {
+ $page = add_theme_page(
+ _x( 'Background', 'custom background' ),
+ _x( 'Background', 'custom background' ),
+ 'edit_theme_options',
+ 'custom-background',
+ array( $this, 'admin_page' )
+ );
+
+ if ( ! $page ) {
+ return;
+ }
+
+ add_action( "load-{$page}", array( $this, 'admin_load' ) );
+ add_action( "load-{$page}", array( $this, 'take_action' ), 49 );
+ add_action( "load-{$page}", array( $this, 'handle_upload' ), 49 );
+
+ if ( $this->admin_header_callback ) {
+ add_action( "admin_head-{$page}", $this->admin_header_callback, 51 );
+ }
+ }
+
+ /**
+ * Sets up the enqueue for the CSS & JavaScript files.
+ *
+ * @since 3.0.0
+ */
+ public function admin_load() {
+ get_current_screen()->add_help_tab(
+ array(
+ 'id' => 'overview',
+ 'title' => __( 'Overview' ),
+ 'content' =>
+ '' . __( 'You can customize the look of your site without touching any of your theme’s code by using a custom background. Your background can be an image or a color.' ) . '
' .
+ '' . __( 'To use a background image, simply upload it or choose an image that has already been uploaded to your Media Library by clicking the “Choose Image” button. You can display a single instance of your image, or tile it to fill the screen. You can have your background fixed in place, so your site content moves on top of it, or you can have it scroll with your site.' ) . '
' .
+ '' . __( 'You can also choose a background color by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. “#ff0000” for red, or by choosing a color using the color picker.' ) . '
' .
+ '' . __( 'Do not forget to click on the Save Changes button when you are finished.' ) . '
',
+ )
+ );
+
+ get_current_screen()->set_help_sidebar(
+ '' . __( 'For more information:' ) . '
' .
+ '' . __( 'Documentation on Custom Background ' ) . '
' .
+ '' . __( 'Support forums ' ) . '
'
+ );
+
+ wp_enqueue_media();
+ wp_enqueue_script( 'custom-background' );
+ wp_enqueue_style( 'wp-color-picker' );
+ }
+
+ /**
+ * Executes custom background modification.
+ *
+ * @since 3.0.0
+ */
+ public function take_action() {
+ if ( empty( $_POST ) ) {
+ return;
+ }
+
+ if ( isset( $_POST['reset-background'] ) ) {
+ check_admin_referer( 'custom-background-reset', '_wpnonce-custom-background-reset' );
+
+ remove_theme_mod( 'background_image' );
+ remove_theme_mod( 'background_image_thumb' );
+
+ $this->updated = true;
+ return;
+ }
+
+ if ( isset( $_POST['remove-background'] ) ) {
+ // @todo Uploaded files are not removed here.
+ check_admin_referer( 'custom-background-remove', '_wpnonce-custom-background-remove' );
+
+ set_theme_mod( 'background_image', '' );
+ set_theme_mod( 'background_image_thumb', '' );
+
+ $this->updated = true;
+ wp_safe_redirect( $_POST['_wp_http_referer'] );
+ return;
+ }
+
+ if ( isset( $_POST['background-preset'] ) ) {
+ check_admin_referer( 'custom-background' );
+
+ if ( in_array( $_POST['background-preset'], array( 'default', 'fill', 'fit', 'repeat', 'custom' ), true ) ) {
+ $preset = $_POST['background-preset'];
+ } else {
+ $preset = 'default';
+ }
+
+ set_theme_mod( 'background_preset', $preset );
+ }
+
+ if ( isset( $_POST['background-position'] ) ) {
+ check_admin_referer( 'custom-background' );
+
+ $position = explode( ' ', $_POST['background-position'] );
+
+ if ( in_array( $position[0], array( 'left', 'center', 'right' ), true ) ) {
+ $position_x = $position[0];
+ } else {
+ $position_x = 'left';
+ }
+
+ if ( in_array( $position[1], array( 'top', 'center', 'bottom' ), true ) ) {
+ $position_y = $position[1];
+ } else {
+ $position_y = 'top';
+ }
+
+ set_theme_mod( 'background_position_x', $position_x );
+ set_theme_mod( 'background_position_y', $position_y );
+ }
+
+ if ( isset( $_POST['background-size'] ) ) {
+ check_admin_referer( 'custom-background' );
+
+ if ( in_array( $_POST['background-size'], array( 'auto', 'contain', 'cover' ), true ) ) {
+ $size = $_POST['background-size'];
+ } else {
+ $size = 'auto';
+ }
+
+ set_theme_mod( 'background_size', $size );
+ }
+
+ if ( isset( $_POST['background-repeat'] ) ) {
+ check_admin_referer( 'custom-background' );
+
+ $repeat = $_POST['background-repeat'];
+
+ if ( 'no-repeat' !== $repeat ) {
+ $repeat = 'repeat';
+ }
+
+ set_theme_mod( 'background_repeat', $repeat );
+ }
+
+ if ( isset( $_POST['background-attachment'] ) ) {
+ check_admin_referer( 'custom-background' );
+
+ $attachment = $_POST['background-attachment'];
+
+ if ( 'fixed' !== $attachment ) {
+ $attachment = 'scroll';
+ }
+
+ set_theme_mod( 'background_attachment', $attachment );
+ }
+
+ if ( isset( $_POST['background-color'] ) ) {
+ check_admin_referer( 'custom-background' );
+
+ $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['background-color'] );
+
+ if ( strlen( $color ) === 6 || strlen( $color ) === 3 ) {
+ set_theme_mod( 'background_color', $color );
+ } else {
+ set_theme_mod( 'background_color', '' );
+ }
+ }
+
+ $this->updated = true;
+ }
+
+ /**
+ * Displays the custom background page.
+ *
+ * @since 3.0.0
+ */
+ public function admin_page() {
+ ?>
+
+
+
+ Customizer.' ),
+ admin_url( 'customize.php?autofocus[control]=background_image' )
+ );
+ wp_admin_notice(
+ $message,
+ array(
+ 'type' => 'info',
+ 'additional_classes' => array( 'hide-if-no-customize' ),
+ )
+ );
+ }
+
+ if ( ! empty( $this->updated ) ) {
+ $updated_message = sprintf(
+ /* translators: %s: Home URL. */
+ __( 'Background updated.
Visit your site to see how it looks.' ),
+ esc_url( home_url( '/' ) )
+ );
+ wp_admin_notice(
+ $updated_message,
+ array(
+ 'id' => 'message',
+ 'additional_classes' => array( 'updated' ),
+ )
+ );
+ }
+ ?>
+
+
+
+
+
+
+
+
+
+ false );
+
+ $uploaded_file = $_FILES['import'];
+ $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
+ if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
+ wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
+ }
+
+ $file = wp_handle_upload( $uploaded_file, $overrides );
+
+ if ( isset( $file['error'] ) ) {
+ wp_die( $file['error'] );
+ }
+
+ $url = $file['url'];
+ $type = $file['type'];
+ $file = $file['file'];
+ $filename = wp_basename( $file );
+
+ // Construct the attachment array.
+ $attachment = array(
+ 'post_title' => $filename,
+ 'post_content' => $url,
+ 'post_mime_type' => $type,
+ 'guid' => $url,
+ 'context' => 'custom-background',
+ );
+
+ // Save the data.
+ $id = wp_insert_attachment( $attachment, $file );
+
+ // Add the metadata.
+ wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
+ update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
+
+ set_theme_mod( 'background_image', sanitize_url( $url ) );
+
+ $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
+ set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) );
+
+ /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
+ $file = apply_filters( 'wp_create_file_in_uploads', $file, $id ); // For replication.
+
+ $this->updated = true;
+ }
+
+ /**
+ * Handles Ajax request for adding custom background context to an attachment.
+ *
+ * Triggers when the user adds a new background image from the
+ * Media Manager.
+ *
+ * @since 4.1.0
+ */
+ public function ajax_background_add() {
+ check_ajax_referer( 'background-add', 'nonce' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_send_json_error();
+ }
+
+ $attachment_id = absint( $_POST['attachment_id'] );
+ if ( $attachment_id < 1 ) {
+ wp_send_json_error();
+ }
+
+ update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );
+
+ wp_send_json_success();
+ }
+
+ /**
+ * @since 3.4.0
+ * @deprecated 3.5.0
+ *
+ * @param array $form_fields
+ * @return array $form_fields
+ */
+ public function attachment_fields_to_edit( $form_fields ) {
+ return $form_fields;
+ }
+
+ /**
+ * @since 3.4.0
+ * @deprecated 3.5.0
+ *
+ * @param array $tabs
+ * @return array $tabs
+ */
+ public function filter_upload_tabs( $tabs ) {
+ return $tabs;
+ }
+
+ /**
+ * @since 3.4.0
+ * @deprecated 3.5.0
+ */
+ public function wp_set_background_image() {
+ check_ajax_referer( 'custom-background' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['attachment_id'] ) ) {
+ exit;
+ }
+
+ $attachment_id = absint( $_POST['attachment_id'] );
+
+ $sizes = array_keys(
+ /** This filter is documented in wp-admin/includes/media.php */
+ apply_filters(
+ 'image_size_names_choose',
+ array(
+ 'thumbnail' => __( 'Thumbnail' ),
+ 'medium' => __( 'Medium' ),
+ 'large' => __( 'Large' ),
+ 'full' => __( 'Full Size' ),
+ )
+ )
+ );
+
+ $size = 'thumbnail';
+ if ( in_array( $_POST['size'], $sizes, true ) ) {
+ $size = esc_attr( $_POST['size'] );
+ }
+
+ update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );
+
+ $url = wp_get_attachment_image_src( $attachment_id, $size );
+ $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
+ set_theme_mod( 'background_image', sanitize_url( $url[0] ) );
+ set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) );
+ exit;
+ }
+}
diff --git a/wp-admin/includes/class-custom-image-header.php b/wp-admin/includes/class-custom-image-header.php
new file mode 100644
index 0000000..ee3bcb1
--- /dev/null
+++ b/wp-admin/includes/class-custom-image-header.php
@@ -0,0 +1,1617 @@
+admin_header_callback = $admin_header_callback;
+ $this->admin_image_div_callback = $admin_image_div_callback;
+
+ add_action( 'admin_menu', array( $this, 'init' ) );
+
+ add_action( 'customize_save_after', array( $this, 'customize_set_last_used' ) );
+ add_action( 'wp_ajax_custom-header-crop', array( $this, 'ajax_header_crop' ) );
+ add_action( 'wp_ajax_custom-header-add', array( $this, 'ajax_header_add' ) );
+ add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove' ) );
+ }
+
+ /**
+ * Sets up the hooks for the Custom Header admin page.
+ *
+ * @since 2.1.0
+ */
+ public function init() {
+ $page = add_theme_page(
+ _x( 'Header', 'custom image header' ),
+ _x( 'Header', 'custom image header' ),
+ 'edit_theme_options',
+ 'custom-header',
+ array( $this, 'admin_page' )
+ );
+
+ if ( ! $page ) {
+ return;
+ }
+
+ add_action( "admin_print_scripts-{$page}", array( $this, 'js_includes' ) );
+ add_action( "admin_print_styles-{$page}", array( $this, 'css_includes' ) );
+ add_action( "admin_head-{$page}", array( $this, 'help' ) );
+ add_action( "admin_head-{$page}", array( $this, 'take_action' ), 50 );
+ add_action( "admin_head-{$page}", array( $this, 'js' ), 50 );
+
+ if ( $this->admin_header_callback ) {
+ add_action( "admin_head-{$page}", $this->admin_header_callback, 51 );
+ }
+ }
+
+ /**
+ * Adds contextual help.
+ *
+ * @since 3.0.0
+ */
+ public function help() {
+ get_current_screen()->add_help_tab(
+ array(
+ 'id' => 'overview',
+ 'title' => __( 'Overview' ),
+ 'content' =>
+ '' . __( 'This screen is used to customize the header section of your theme.' ) . '
' .
+ '' . __( 'You can choose from the theme’s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.' ) . '
',
+ )
+ );
+
+ get_current_screen()->add_help_tab(
+ array(
+ 'id' => 'set-header-image',
+ 'title' => __( 'Header Image' ),
+ 'content' =>
+ '
' . __( 'You can set a custom image header for your site. Simply upload the image and crop it, and the new header will go live immediately. Alternatively, you can use an image that has already been uploaded to your Media Library by clicking the “Choose Image” button.' ) . '
' .
+ '' . __( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you would like and click the “Save Changes” button.' ) . '
' .
+ '' . __( 'If your theme has more than one default header image, or you have uploaded more than one custom header image, you have the option of having WordPress display a randomly different image on each page of your site. Click the “Random” radio button next to the Uploaded Images or Default Images section to enable this feature.' ) . '
' .
+ '' . __( 'If you do not want a header image to be displayed on your site at all, click the “Remove Header Image” button at the bottom of the Header Image section of this page. If you want to re-enable the header image later, you just have to select one of the other image options and click “Save Changes”.' ) . '
',
+ )
+ );
+
+ get_current_screen()->add_help_tab(
+ array(
+ 'id' => 'set-header-text',
+ 'title' => __( 'Header Text' ),
+ 'content' =>
+ '' . sprintf(
+ /* translators: %s: URL to General Settings screen. */
+ __( 'For most themes, the header text is your Site Title and Tagline, as defined in the General Settings section.' ),
+ admin_url( 'options-general.php' )
+ ) .
+ '
' .
+ '' . __( 'In the Header Text section of this page, you can choose whether to display this text or hide it. You can also choose a color for the text by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. “#ff0000” for red, or by choosing a color using the color picker.' ) . '
' .
+ '' . __( 'Do not forget to click “Save Changes” when you are done!' ) . '
',
+ )
+ );
+
+ get_current_screen()->set_help_sidebar(
+ '' . __( 'For more information:' ) . '
' .
+ '' . __( 'Documentation on Custom Header ' ) . '
' .
+ '' . __( 'Support forums ' ) . '
'
+ );
+ }
+
+ /**
+ * Gets the current step.
+ *
+ * @since 2.6.0
+ *
+ * @return int Current step.
+ */
+ public function step() {
+ if ( ! isset( $_GET['step'] ) ) {
+ return 1;
+ }
+
+ $step = (int) $_GET['step'];
+ if ( $step < 1 || 3 < $step ||
+ ( 2 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
+ ( 3 === $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
+ ) {
+ return 1;
+ }
+
+ return $step;
+ }
+
+ /**
+ * Sets up the enqueue for the JavaScript files.
+ *
+ * @since 2.1.0
+ */
+ public function js_includes() {
+ $step = $this->step();
+
+ if ( ( 1 === $step || 3 === $step ) ) {
+ wp_enqueue_media();
+ wp_enqueue_script( 'custom-header' );
+ if ( current_theme_supports( 'custom-header', 'header-text' ) ) {
+ wp_enqueue_script( 'wp-color-picker' );
+ }
+ } elseif ( 2 === $step ) {
+ wp_enqueue_script( 'imgareaselect' );
+ }
+ }
+
+ /**
+ * Sets up the enqueue for the CSS files.
+ *
+ * @since 2.7.0
+ */
+ public function css_includes() {
+ $step = $this->step();
+
+ if ( ( 1 === $step || 3 === $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
+ wp_enqueue_style( 'wp-color-picker' );
+ } elseif ( 2 === $step ) {
+ wp_enqueue_style( 'imgareaselect' );
+ }
+ }
+
+ /**
+ * Executes custom header modification.
+ *
+ * @since 2.6.0
+ */
+ public function take_action() {
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ return;
+ }
+
+ if ( empty( $_POST ) ) {
+ return;
+ }
+
+ $this->updated = true;
+
+ if ( isset( $_POST['resetheader'] ) ) {
+ check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
+
+ $this->reset_header_image();
+
+ return;
+ }
+
+ if ( isset( $_POST['removeheader'] ) ) {
+ check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
+
+ $this->remove_header_image();
+
+ return;
+ }
+
+ if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) {
+ check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
+
+ set_theme_mod( 'header_textcolor', 'blank' );
+ } elseif ( isset( $_POST['text-color'] ) ) {
+ check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
+
+ $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
+
+ $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] );
+
+ if ( strlen( $color ) === 6 || strlen( $color ) === 3 ) {
+ set_theme_mod( 'header_textcolor', $color );
+ } elseif ( ! $color ) {
+ set_theme_mod( 'header_textcolor', 'blank' );
+ }
+ }
+
+ if ( isset( $_POST['default-header'] ) ) {
+ check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
+
+ $this->set_header_image( $_POST['default-header'] );
+
+ return;
+ }
+ }
+
+ /**
+ * Processes the default headers.
+ *
+ * @since 3.0.0
+ *
+ * @global array $_wp_default_headers
+ */
+ public function process_default_headers() {
+ global $_wp_default_headers;
+
+ if ( ! isset( $_wp_default_headers ) ) {
+ return;
+ }
+
+ if ( ! empty( $this->default_headers ) ) {
+ return;
+ }
+
+ $this->default_headers = $_wp_default_headers;
+ $template_directory_uri = get_template_directory_uri();
+ $stylesheet_directory_uri = get_stylesheet_directory_uri();
+
+ foreach ( array_keys( $this->default_headers ) as $header ) {
+ $this->default_headers[ $header ]['url'] = sprintf(
+ $this->default_headers[ $header ]['url'],
+ $template_directory_uri,
+ $stylesheet_directory_uri
+ );
+
+ $this->default_headers[ $header ]['thumbnail_url'] = sprintf(
+ $this->default_headers[ $header ]['thumbnail_url'],
+ $template_directory_uri,
+ $stylesheet_directory_uri
+ );
+ }
+ }
+
+ /**
+ * Displays UI for selecting one of several default headers.
+ *
+ * Shows the random image option if this theme has multiple header images.
+ * Random image option is on by default if no header has been set.
+ *
+ * @since 3.0.0
+ *
+ * @param string $type The header type. One of 'default' (for the Uploaded Images control)
+ * or 'uploaded' (for the Uploaded Images control).
+ */
+ public function show_header_selector( $type = 'default' ) {
+ if ( 'default' === $type ) {
+ $headers = $this->default_headers;
+ } else {
+ $headers = get_uploaded_header_images();
+ $type = 'uploaded';
+ }
+
+ if ( 1 < count( $headers ) ) {
+ echo '';
+ echo ' ';
+ _e( 'Random: Show a different image on each page.' );
+ echo ' ';
+ echo '
';
+ }
+
+ echo '';
+ }
+
+ /**
+ * Executes JavaScript depending on step.
+ *
+ * @since 2.1.0
+ */
+ public function js() {
+ $step = $this->step();
+
+ if ( ( 1 === $step || 3 === $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) {
+ $this->js_1();
+ } elseif ( 2 === $step ) {
+ $this->js_2();
+ }
+ }
+
+ /**
+ * Displays JavaScript based on Step 1 and 3.
+ *
+ * @since 2.6.0
+ */
+ public function js_1() {
+ $default_color = '';
+ if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) {
+ $default_color = get_theme_support( 'custom-header', 'default-text-color' );
+ if ( $default_color && ! str_contains( $default_color, '#' ) ) {
+ $default_color = '#' . $default_color;
+ }
+ }
+ ?>
+
+
+
+ process_default_headers();
+ ?>
+
+
+
+
+ Customizer.' ),
+ admin_url( 'customize.php?autofocus[control]=header_image' )
+ );
+ wp_admin_notice(
+ $message,
+ array(
+ 'type' => 'info',
+ 'additional_classes' => array( 'hide-if-no-customize' ),
+ )
+ );
+ }
+
+ if ( ! empty( $this->updated ) ) {
+ $updated_message = sprintf(
+ /* translators: %s: Home URL. */
+ __( 'Header updated.
Visit your site to see how it looks.' ),
+ esc_url( home_url( '/' ) )
+ );
+ wp_admin_notice(
+ $updated_message,
+ array(
+ 'id' => 'message',
+ 'additional_classes' => array( 'updated' ),
+ )
+ );
+ }
+ ?>
+
+
+
+
+
+
+
+
+ ' . __( 'Something went wrong.' ) . '' .
+ '' . __( 'The active theme does not support uploading a custom header image.' ) . '
',
+ 403
+ );
+ }
+
+ if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
+ $attachment_id = absint( $_GET['file'] );
+ $file = get_attached_file( $attachment_id, true );
+ $url = wp_get_attachment_image_src( $attachment_id, 'full' );
+ $url = $url[0];
+ } elseif ( isset( $_POST ) ) {
+ $data = $this->step_2_manage_upload();
+ $attachment_id = $data['attachment_id'];
+ $file = $data['file'];
+ $url = $data['url'];
+ }
+
+ if ( file_exists( $file ) ) {
+ list( $width, $height, $type, $attr ) = wp_getimagesize( $file );
+ } else {
+ $data = wp_get_attachment_metadata( $attachment_id );
+ $height = isset( $data['height'] ) ? (int) $data['height'] : 0;
+ $width = isset( $data['width'] ) ? (int) $data['width'] : 0;
+ unset( $data );
+ }
+
+ $max_width = 0;
+
+ // For flex, limit size of image displayed to 1500px unless theme says otherwise.
+ if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
+ $max_width = 1500;
+ }
+
+ if ( current_theme_supports( 'custom-header', 'max-width' ) ) {
+ $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
+ }
+
+ $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
+
+ // If flexible height isn't supported and the image is the exact right size.
+ if ( ! current_theme_supports( 'custom-header', 'flex-height' )
+ && ! current_theme_supports( 'custom-header', 'flex-width' )
+ && (int) get_theme_support( 'custom-header', 'width' ) === $width
+ && (int) get_theme_support( 'custom-header', 'height' ) === $height
+ ) {
+ // Add the metadata.
+ if ( file_exists( $file ) ) {
+ wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
+ }
+
+ $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
+
+ /**
+ * Filters the attachment file path after the custom header or background image is set.
+ *
+ * Used for file replication.
+ *
+ * @since 2.1.0
+ *
+ * @param string $file Path to the file.
+ * @param int $attachment_id Attachment ID.
+ */
+ $file = apply_filters( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication.
+
+ return $this->finished();
+ } elseif ( $width > $max_width ) {
+ $oitar = $width / $max_width;
+
+ $image = wp_crop_image(
+ $attachment_id,
+ 0,
+ 0,
+ $width,
+ $height,
+ $max_width,
+ $height / $oitar,
+ false,
+ str_replace( wp_basename( $file ), 'midsize-' . wp_basename( $file ), $file )
+ );
+
+ if ( ! $image || is_wp_error( $image ) ) {
+ wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
+ }
+
+ /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
+ $image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication.
+
+ $url = str_replace( wp_basename( $url ), wp_basename( $image ), $url );
+ $width = $width / $oitar;
+ $height = $height / $oitar;
+ } else {
+ $oitar = 1;
+ }
+ ?>
+
+
+ false );
+
+ $uploaded_file = $_FILES['import'];
+ $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
+
+ if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
+ wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
+ }
+
+ $file = wp_handle_upload( $uploaded_file, $overrides );
+
+ if ( isset( $file['error'] ) ) {
+ wp_die( $file['error'], __( 'Image Upload Error' ) );
+ }
+
+ $url = $file['url'];
+ $type = $file['type'];
+ $file = $file['file'];
+ $filename = wp_basename( $file );
+
+ // Construct the attachment array.
+ $attachment = array(
+ 'post_title' => $filename,
+ 'post_content' => $url,
+ 'post_mime_type' => $type,
+ 'guid' => $url,
+ 'context' => 'custom-header',
+ );
+
+ // Save the data.
+ $attachment_id = wp_insert_attachment( $attachment, $file );
+
+ return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
+ }
+
+ /**
+ * Displays third step of custom header image page.
+ *
+ * @since 2.1.0
+ * @since 4.4.0 Switched to using wp_get_attachment_url() instead of the guid
+ * for retrieving the header image URL.
+ */
+ public function step_3() {
+ check_admin_referer( 'custom-header-crop-image' );
+
+ if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
+ wp_die(
+ '' . __( 'Something went wrong.' ) . ' ' .
+ '' . __( 'The active theme does not support uploading a custom header image.' ) . '
',
+ 403
+ );
+ }
+
+ if ( ! empty( $_POST['skip-cropping'] )
+ && ! current_theme_supports( 'custom-header', 'flex-height' )
+ && ! current_theme_supports( 'custom-header', 'flex-width' )
+ ) {
+ wp_die(
+ '' . __( 'Something went wrong.' ) . ' ' .
+ '' . __( 'The active theme does not support a flexible sized header image.' ) . '
',
+ 403
+ );
+ }
+
+ if ( $_POST['oitar'] > 1 ) {
+ $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
+ $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
+ $_POST['width'] = $_POST['width'] * $_POST['oitar'];
+ $_POST['height'] = $_POST['height'] * $_POST['oitar'];
+ }
+
+ $attachment_id = absint( $_POST['attachment_id'] );
+ $original = get_attached_file( $attachment_id );
+
+ $dimensions = $this->get_header_dimensions(
+ array(
+ 'height' => $_POST['height'],
+ 'width' => $_POST['width'],
+ )
+ );
+ $height = $dimensions['dst_height'];
+ $width = $dimensions['dst_width'];
+
+ if ( empty( $_POST['skip-cropping'] ) ) {
+ $cropped = wp_crop_image(
+ $attachment_id,
+ (int) $_POST['x1'],
+ (int) $_POST['y1'],
+ (int) $_POST['width'],
+ (int) $_POST['height'],
+ $width,
+ $height
+ );
+ } elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
+ $cropped = _copy_image_file( $attachment_id );
+ } else {
+ $cropped = get_attached_file( $attachment_id );
+ }
+
+ if ( ! $cropped || is_wp_error( $cropped ) ) {
+ wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
+ }
+
+ /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
+ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
+
+ $attachment = $this->create_attachment_object( $cropped, $attachment_id );
+
+ if ( ! empty( $_POST['create-new-attachment'] ) ) {
+ unset( $attachment['ID'] );
+ }
+
+ // Update the attachment.
+ $attachment_id = $this->insert_attachment( $attachment, $cropped );
+
+ $url = wp_get_attachment_url( $attachment_id );
+ $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
+
+ // Cleanup.
+ $medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original ), $original );
+ if ( file_exists( $medium ) ) {
+ wp_delete_file( $medium );
+ }
+
+ if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
+ wp_delete_file( $original );
+ }
+
+ return $this->finished();
+ }
+
+ /**
+ * Displays last step of custom header image page.
+ *
+ * @since 2.1.0
+ */
+ public function finished() {
+ $this->updated = true;
+ $this->step_1();
+ }
+
+ /**
+ * Displays the page based on the current step.
+ *
+ * @since 2.1.0
+ */
+ public function admin_page() {
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_die( __( 'Sorry, you are not allowed to customize headers.' ) );
+ }
+
+ $step = $this->step();
+
+ if ( 2 === $step ) {
+ $this->step_2();
+ } elseif ( 3 === $step ) {
+ $this->step_3();
+ } else {
+ $this->step_1();
+ }
+ }
+
+ /**
+ * Unused since 3.5.0.
+ *
+ * @since 3.4.0
+ *
+ * @param array $form_fields
+ * @return array $form_fields
+ */
+ public function attachment_fields_to_edit( $form_fields ) {
+ return $form_fields;
+ }
+
+ /**
+ * Unused since 3.5.0.
+ *
+ * @since 3.4.0
+ *
+ * @param array $tabs
+ * @return array $tabs
+ */
+ public function filter_upload_tabs( $tabs ) {
+ return $tabs;
+ }
+
+ /**
+ * Chooses a header image, selected from existing uploaded and default headers,
+ * or provides an array of uploaded header data (either new, or from media library).
+ *
+ * @since 3.4.0
+ *
+ * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
+ * for randomly cycling among the default images; 'random-uploaded-image',
+ * for randomly cycling among the uploaded images; the key of a default image
+ * registered for that theme; and the key of an image uploaded for that theme
+ * (the attachment ID of the image). Or an array of arguments: attachment_id,
+ * url, width, height. All are required.
+ */
+ final public function set_header_image( $choice ) {
+ if ( is_array( $choice ) || is_object( $choice ) ) {
+ $choice = (array) $choice;
+
+ if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) {
+ return;
+ }
+
+ $choice['url'] = sanitize_url( $choice['url'] );
+
+ $header_image_data = (object) array(
+ 'attachment_id' => $choice['attachment_id'],
+ 'url' => $choice['url'],
+ 'thumbnail_url' => $choice['url'],
+ 'height' => $choice['height'],
+ 'width' => $choice['width'],
+ );
+
+ update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
+
+ set_theme_mod( 'header_image', $choice['url'] );
+ set_theme_mod( 'header_image_data', $header_image_data );
+
+ return;
+ }
+
+ if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ), true ) ) {
+ set_theme_mod( 'header_image', $choice );
+ remove_theme_mod( 'header_image_data' );
+
+ return;
+ }
+
+ $uploaded = get_uploaded_header_images();
+
+ if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
+ $header_image_data = $uploaded[ $choice ];
+ } else {
+ $this->process_default_headers();
+ if ( isset( $this->default_headers[ $choice ] ) ) {
+ $header_image_data = $this->default_headers[ $choice ];
+ } else {
+ return;
+ }
+ }
+
+ set_theme_mod( 'header_image', sanitize_url( $header_image_data['url'] ) );
+ set_theme_mod( 'header_image_data', $header_image_data );
+ }
+
+ /**
+ * Removes a header image.
+ *
+ * @since 3.4.0
+ */
+ final public function remove_header_image() {
+ $this->set_header_image( 'remove-header' );
+ }
+
+ /**
+ * Resets a header image to the default image for the theme.
+ *
+ * This method does not do anything if the theme does not have a default header image.
+ *
+ * @since 3.4.0
+ */
+ final public function reset_header_image() {
+ $this->process_default_headers();
+ $default = get_theme_support( 'custom-header', 'default-image' );
+
+ if ( ! $default ) {
+ $this->remove_header_image();
+ return;
+ }
+
+ $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
+
+ $default_data = array();
+ foreach ( $this->default_headers as $header => $details ) {
+ if ( $details['url'] === $default ) {
+ $default_data = $details;
+ break;
+ }
+ }
+
+ set_theme_mod( 'header_image', $default );
+ set_theme_mod( 'header_image_data', (object) $default_data );
+ }
+
+ /**
+ * Calculates width and height based on what the currently selected theme supports.
+ *
+ * @since 3.9.0
+ *
+ * @param array $dimensions
+ * @return array dst_height and dst_width of header image.
+ */
+ final public function get_header_dimensions( $dimensions ) {
+ $max_width = 0;
+ $width = absint( $dimensions['width'] );
+ $height = absint( $dimensions['height'] );
+ $theme_height = get_theme_support( 'custom-header', 'height' );
+ $theme_width = get_theme_support( 'custom-header', 'width' );
+ $has_flex_width = current_theme_supports( 'custom-header', 'flex-width' );
+ $has_flex_height = current_theme_supports( 'custom-header', 'flex-height' );
+ $has_max_width = current_theme_supports( 'custom-header', 'max-width' );
+ $dst = array(
+ 'dst_height' => null,
+ 'dst_width' => null,
+ );
+
+ // For flex, limit size of image displayed to 1500px unless theme says otherwise.
+ if ( $has_flex_width ) {
+ $max_width = 1500;
+ }
+
+ if ( $has_max_width ) {
+ $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
+ }
+ $max_width = max( $max_width, $theme_width );
+
+ if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
+ $dst['dst_height'] = absint( $height * ( $max_width / $width ) );
+ } elseif ( $has_flex_height && $has_flex_width ) {
+ $dst['dst_height'] = $height;
+ } else {
+ $dst['dst_height'] = $theme_height;
+ }
+
+ if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
+ $dst['dst_width'] = absint( $width * ( $max_width / $width ) );
+ } elseif ( $has_flex_width && $has_flex_height ) {
+ $dst['dst_width'] = $width;
+ } else {
+ $dst['dst_width'] = $theme_width;
+ }
+
+ return $dst;
+ }
+
+ /**
+ * Creates an attachment 'object'.
+ *
+ * @since 3.9.0
+ *
+ * @param string $cropped Cropped image URL.
+ * @param int $parent_attachment_id Attachment ID of parent image.
+ * @return array An array with attachment object data.
+ */
+ final public function create_attachment_object( $cropped, $parent_attachment_id ) {
+ $parent = get_post( $parent_attachment_id );
+ $parent_url = wp_get_attachment_url( $parent->ID );
+ $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
+
+ $size = wp_getimagesize( $cropped );
+ $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
+
+ $attachment = array(
+ 'ID' => $parent_attachment_id,
+ 'post_title' => wp_basename( $cropped ),
+ 'post_mime_type' => $image_type,
+ 'guid' => $url,
+ 'context' => 'custom-header',
+ 'post_parent' => $parent_attachment_id,
+ );
+
+ return $attachment;
+ }
+
+ /**
+ * Inserts an attachment and its metadata.
+ *
+ * @since 3.9.0
+ *
+ * @param array $attachment An array with attachment object data.
+ * @param string $cropped File path to cropped image.
+ * @return int Attachment ID.
+ */
+ final public function insert_attachment( $attachment, $cropped ) {
+ $parent_id = isset( $attachment['post_parent'] ) ? $attachment['post_parent'] : null;
+ unset( $attachment['post_parent'] );
+
+ $attachment_id = wp_insert_attachment( $attachment, $cropped );
+ $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
+
+ // If this is a crop, save the original attachment ID as metadata.
+ if ( $parent_id ) {
+ $metadata['attachment_parent'] = $parent_id;
+ }
+
+ /**
+ * Filters the header image attachment metadata.
+ *
+ * @since 3.9.0
+ *
+ * @see wp_generate_attachment_metadata()
+ *
+ * @param array $metadata Attachment metadata.
+ */
+ $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata );
+
+ wp_update_attachment_metadata( $attachment_id, $metadata );
+
+ return $attachment_id;
+ }
+
+ /**
+ * Gets attachment uploaded by Media Manager, crops it, then saves it as a
+ * new object. Returns JSON-encoded object details.
+ *
+ * @since 3.9.0
+ */
+ public function ajax_header_crop() {
+ check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_send_json_error();
+ }
+
+ if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
+ wp_send_json_error();
+ }
+
+ $crop_details = $_POST['cropDetails'];
+
+ $dimensions = $this->get_header_dimensions(
+ array(
+ 'height' => $crop_details['height'],
+ 'width' => $crop_details['width'],
+ )
+ );
+
+ $attachment_id = absint( $_POST['id'] );
+
+ $cropped = wp_crop_image(
+ $attachment_id,
+ (int) $crop_details['x1'],
+ (int) $crop_details['y1'],
+ (int) $crop_details['width'],
+ (int) $crop_details['height'],
+ (int) $dimensions['dst_width'],
+ (int) $dimensions['dst_height']
+ );
+
+ if ( ! $cropped || is_wp_error( $cropped ) ) {
+ wp_send_json_error( array( 'message' => __( 'Image could not be processed. Please go back and try again.' ) ) );
+ }
+
+ /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
+ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
+
+ $attachment = $this->create_attachment_object( $cropped, $attachment_id );
+
+ $previous = $this->get_previous_crop( $attachment );
+
+ if ( $previous ) {
+ $attachment['ID'] = $previous;
+ } else {
+ unset( $attachment['ID'] );
+ }
+
+ $new_attachment_id = $this->insert_attachment( $attachment, $cropped );
+
+ $attachment['attachment_id'] = $new_attachment_id;
+ $attachment['url'] = wp_get_attachment_url( $new_attachment_id );
+
+ $attachment['width'] = $dimensions['dst_width'];
+ $attachment['height'] = $dimensions['dst_height'];
+
+ wp_send_json_success( $attachment );
+ }
+
+ /**
+ * Given an attachment ID for a header image, updates its "last used"
+ * timestamp to now.
+ *
+ * Triggered when the user tries adds a new header image from the
+ * Media Manager, even if s/he doesn't save that change.
+ *
+ * @since 3.9.0
+ */
+ public function ajax_header_add() {
+ check_ajax_referer( 'header-add', 'nonce' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_send_json_error();
+ }
+
+ $attachment_id = absint( $_POST['attachment_id'] );
+ if ( $attachment_id < 1 ) {
+ wp_send_json_error();
+ }
+
+ $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
+ update_post_meta( $attachment_id, $key, time() );
+ update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
+
+ wp_send_json_success();
+ }
+
+ /**
+ * Given an attachment ID for a header image, unsets it as a user-uploaded
+ * header image for the active theme.
+ *
+ * Triggered when the user clicks the overlay "X" button next to each image
+ * choice in the Customizer's Header tool.
+ *
+ * @since 3.9.0
+ */
+ public function ajax_header_remove() {
+ check_ajax_referer( 'header-remove', 'nonce' );
+
+ if ( ! current_user_can( 'edit_theme_options' ) ) {
+ wp_send_json_error();
+ }
+
+ $attachment_id = absint( $_POST['attachment_id'] );
+ if ( $attachment_id < 1 ) {
+ wp_send_json_error();
+ }
+
+ $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
+ delete_post_meta( $attachment_id, $key );
+ delete_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
+
+ wp_send_json_success();
+ }
+
+ /**
+ * Updates the last-used postmeta on a header image attachment after saving a new header image via the Customizer.
+ *
+ * @since 3.9.0
+ *
+ * @param WP_Customize_Manager $wp_customize Customize manager.
+ */
+ public function customize_set_last_used( $wp_customize ) {
+
+ $header_image_data_setting = $wp_customize->get_setting( 'header_image_data' );
+
+ if ( ! $header_image_data_setting ) {
+ return;
+ }
+
+ $data = $header_image_data_setting->post_value();
+
+ if ( ! isset( $data['attachment_id'] ) ) {
+ return;
+ }
+
+ $attachment_id = $data['attachment_id'];
+ $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
+ update_post_meta( $attachment_id, $key, time() );
+ }
+
+ /**
+ * Gets the details of default header images if defined.
+ *
+ * @since 3.9.0
+ *
+ * @return array Default header images.
+ */
+ public function get_default_header_images() {
+ $this->process_default_headers();
+
+ // Get the default image if there is one.
+ $default = get_theme_support( 'custom-header', 'default-image' );
+
+ if ( ! $default ) { // If not, easy peasy.
+ return $this->default_headers;
+ }
+
+ $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
+
+ $already_has_default = false;
+
+ foreach ( $this->default_headers as $k => $h ) {
+ if ( $h['url'] === $default ) {
+ $already_has_default = true;
+ break;
+ }
+ }
+
+ if ( $already_has_default ) {
+ return $this->default_headers;
+ }
+
+ // If the one true image isn't included in the default set, prepend it.
+ $header_images = array();
+ $header_images['default'] = array(
+ 'url' => $default,
+ 'thumbnail_url' => $default,
+ 'description' => 'Default',
+ );
+
+ // The rest of the set comes after.
+ return array_merge( $header_images, $this->default_headers );
+ }
+
+ /**
+ * Gets the previously uploaded header images.
+ *
+ * @since 3.9.0
+ *
+ * @return array Uploaded header images.
+ */
+ public function get_uploaded_header_images() {
+ $header_images = get_uploaded_header_images();
+ $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
+ $alt_text_key = '_wp_attachment_image_alt';
+
+ foreach ( $header_images as &$header_image ) {
+ $header_meta = get_post_meta( $header_image['attachment_id'] );
+ $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : '';
+ $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';
+ }
+
+ return $header_images;
+ }
+
+ /**
+ * Gets the ID of a previous crop from the same base image.
+ *
+ * @since 4.9.0
+ *
+ * @param array $attachment An array with a cropped attachment object data.
+ * @return int|false An attachment ID if one exists. False if none.
+ */
+ public function get_previous_crop( $attachment ) {
+ $header_images = $this->get_uploaded_header_images();
+
+ // Bail early if there are no header images.
+ if ( empty( $header_images ) ) {
+ return false;
+ }
+
+ $previous = false;
+
+ foreach ( $header_images as $image ) {
+ if ( $image['attachment_parent'] === $attachment['post_parent'] ) {
+ $previous = $image['attachment_id'];
+ break;
+ }
+ }
+
+ return $previous;
+ }
+}
diff --git a/wp-admin/includes/class-file-upload-upgrader.php b/wp-admin/includes/class-file-upload-upgrader.php
new file mode 100644
index 0000000..e625615
--- /dev/null
+++ b/wp-admin/includes/class-file-upload-upgrader.php
@@ -0,0 +1,158 @@
+ false,
+ 'test_type' => false,
+ );
+ $file = wp_handle_upload( $_FILES[ $form ], $overrides );
+
+ if ( isset( $file['error'] ) ) {
+ wp_die( $file['error'] );
+ }
+
+ if ( 'pluginzip' === $form || 'themezip' === $form ) {
+ $archive_is_valid = false;
+
+ /** This filter is documented in wp-admin/includes/file.php */
+ if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
+ $archive = new ZipArchive();
+ $archive_is_valid = $archive->open( $file['file'], ZIPARCHIVE::CHECKCONS );
+
+ if ( true === $archive_is_valid ) {
+ $archive->close();
+ }
+ } else {
+ require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
+
+ $archive = new PclZip( $file['file'] );
+ $archive_is_valid = is_array( $archive->properties() );
+ }
+
+ if ( true !== $archive_is_valid ) {
+ wp_delete_file( $file['file'] );
+ wp_die( __( 'Incompatible Archive.' ) );
+ }
+ }
+
+ $this->filename = $_FILES[ $form ]['name'];
+ $this->package = $file['file'];
+
+ // Construct the attachment array.
+ $attachment = array(
+ 'post_title' => $this->filename,
+ 'post_content' => $file['url'],
+ 'post_mime_type' => $file['type'],
+ 'guid' => $file['url'],
+ 'context' => 'upgrader',
+ 'post_status' => 'private',
+ );
+
+ // Save the data.
+ $this->id = wp_insert_attachment( $attachment, $file['file'] );
+
+ // Schedule a cleanup for 2 hours from now in case of failed installation.
+ wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) );
+
+ } elseif ( is_numeric( $_GET[ $urlholder ] ) ) {
+ // Numeric Package = previously uploaded file, see above.
+ $this->id = (int) $_GET[ $urlholder ];
+ $attachment = get_post( $this->id );
+ if ( empty( $attachment ) ) {
+ wp_die( __( 'Please select a file' ) );
+ }
+
+ $this->filename = $attachment->post_title;
+ $this->package = get_attached_file( $attachment->ID );
+ } else {
+ // Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.
+ $uploads = wp_upload_dir();
+ if ( ! ( $uploads && false === $uploads['error'] ) ) {
+ wp_die( $uploads['error'] );
+ }
+
+ $this->filename = sanitize_file_name( $_GET[ $urlholder ] );
+ $this->package = $uploads['basedir'] . '/' . $this->filename;
+
+ if ( ! str_starts_with( realpath( $this->package ), realpath( $uploads['basedir'] ) ) ) {
+ wp_die( __( 'Please select a file' ) );
+ }
+ }
+ }
+
+ /**
+ * Deletes the attachment/uploaded file.
+ *
+ * @since 3.2.2
+ *
+ * @return bool Whether the cleanup was successful.
+ */
+ public function cleanup() {
+ if ( $this->id ) {
+ wp_delete_attachment( $this->id );
+
+ } elseif ( file_exists( $this->package ) ) {
+ return @unlink( $this->package );
+ }
+
+ return true;
+ }
+}
diff --git a/wp-admin/includes/class-ftp-pure.php b/wp-admin/includes/class-ftp-pure.php
new file mode 100644
index 0000000..4c51876
--- /dev/null
+++ b/wp-admin/includes/class-ftp-pure.php
@@ -0,0 +1,186 @@
+
+//
+//
+
+ function _settimeout($sock) {
+ if(!@stream_set_timeout($sock, $this->_timeout)) {
+ $this->PushError('_settimeout','socket set send timeout');
+ $this->_quit();
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+ function _connect($host, $port) {
+ $this->SendMSG("Creating socket");
+ $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
+ if (!$sock) {
+ $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
+ return FALSE;
+ }
+ $this->_connected=true;
+ return $sock;
+ }
+
+ function _readmsg($fnction="_readmsg"){
+ if(!$this->_connected) {
+ $this->PushError($fnction, 'Connect first');
+ return FALSE;
+ }
+ $result=true;
+ $this->_message="";
+ $this->_code=0;
+ $go=true;
+ do {
+ $tmp=@fgets($this->_ftp_control_sock, 512);
+ if($tmp===false) {
+ $go=$result=false;
+ $this->PushError($fnction,'Read failed');
+ } else {
+ $this->_message.=$tmp;
+ if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
+ }
+ } while($go);
+ if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
+ $this->_code=(int)$regs[1];
+ return $result;
+ }
+
+ function _exec($cmd, $fnction="_exec") {
+ if(!$this->_ready) {
+ $this->PushError($fnction,'Connect first');
+ return FALSE;
+ }
+ if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
+ $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
+ if($status===false) {
+ $this->PushError($fnction,'socket write failed');
+ return FALSE;
+ }
+ $this->_lastaction=time();
+ if(!$this->_readmsg($fnction)) return FALSE;
+ return TRUE;
+ }
+
+ function _data_prepare($mode=FTP_ASCII) {
+ if(!$this->_settype($mode)) return FALSE;
+ if($this->_passive) {
+ if(!$this->_exec("PASV", "pasv")) {
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!$this->_checkCode()) {
+ $this->_data_close();
+ return FALSE;
+ }
+ $ip_port = explode(",", preg_replace("/^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*$/s", "\\1", $this->_message));
+ $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
+ $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
+ $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
+ $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout);
+ if(!$this->_ftp_data_sock) {
+ $this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")");
+ $this->_data_close();
+ return FALSE;
+ }
+ else $this->_ftp_data_sock;
+ } else {
+ $this->SendMSG("Only passive connections available!");
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+ function _data_read($mode=FTP_ASCII, $fp=NULL) {
+ if(is_resource($fp)) $out=0;
+ else $out="";
+ if(!$this->_passive) {
+ $this->SendMSG("Only passive connections available!");
+ return FALSE;
+ }
+ while (!feof($this->_ftp_data_sock)) {
+ $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size);
+ if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
+ if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
+ else $out.=$block;
+ }
+ return $out;
+ }
+
+ function _data_write($mode=FTP_ASCII, $fp=NULL) {
+ if(is_resource($fp)) $out=0;
+ else $out="";
+ if(!$this->_passive) {
+ $this->SendMSG("Only passive connections available!");
+ return FALSE;
+ }
+ if(is_resource($fp)) {
+ while(!feof($fp)) {
+ $block=fread($fp, $this->_ftp_buff_size);
+ if(!$this->_data_write_block($mode, $block)) return false;
+ }
+ } elseif(!$this->_data_write_block($mode, $fp)) return false;
+ return TRUE;
+ }
+
+ function _data_write_block($mode, $block) {
+ if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
+ do {
+ if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
+ $this->PushError("_data_write","Can't write to socket");
+ return FALSE;
+ }
+ $block=substr($block, $t);
+ } while(!empty($block));
+ return true;
+ }
+
+ function _data_close() {
+ @fclose($this->_ftp_data_sock);
+ $this->SendMSG("Disconnected data from remote host");
+ return TRUE;
+ }
+
+ function _quit($force=FALSE) {
+ if($this->_connected or $force) {
+ @fclose($this->_ftp_control_sock);
+ $this->_connected=false;
+ $this->SendMSG("Socket closed");
+ }
+ }
+}
+
+?>
diff --git a/wp-admin/includes/class-ftp-sockets.php b/wp-admin/includes/class-ftp-sockets.php
new file mode 100644
index 0000000..575b393
--- /dev/null
+++ b/wp-admin/includes/class-ftp-sockets.php
@@ -0,0 +1,246 @@
+
+//
+//
+
+ function _settimeout($sock) {
+ if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
+ $this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock)));
+ @socket_close($sock);
+ return FALSE;
+ }
+ if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
+ $this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock)));
+ @socket_close($sock);
+ return FALSE;
+ }
+ return true;
+ }
+
+ function _connect($host, $port) {
+ $this->SendMSG("Creating socket");
+ if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
+ $this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock)));
+ return FALSE;
+ }
+ if(!$this->_settimeout($sock)) return FALSE;
+ $this->SendMSG("Connecting to \"".$host.":".$port."\"");
+ if (!($res = @socket_connect($sock, $host, $port))) {
+ $this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock)));
+ @socket_close($sock);
+ return FALSE;
+ }
+ $this->_connected=true;
+ return $sock;
+ }
+
+ function _readmsg($fnction="_readmsg"){
+ if(!$this->_connected) {
+ $this->PushError($fnction,'Connect first');
+ return FALSE;
+ }
+ $result=true;
+ $this->_message="";
+ $this->_code=0;
+ $go=true;
+ do {
+ $tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ);
+ if($tmp===false) {
+ $go=$result=false;
+ $this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock)));
+ } else {
+ $this->_message.=$tmp;
+ $go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs);
+ }
+ } while($go);
+ if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
+ $this->_code=(int)$regs[1];
+ return $result;
+ }
+
+ function _exec($cmd, $fnction="_exec") {
+ if(!$this->_ready) {
+ $this->PushError($fnction,'Connect first');
+ return FALSE;
+ }
+ if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
+ $status=@socket_write($this->_ftp_control_sock, $cmd.CRLF);
+ if($status===false) {
+ $this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream)));
+ return FALSE;
+ }
+ $this->_lastaction=time();
+ if(!$this->_readmsg($fnction)) return FALSE;
+ return TRUE;
+ }
+
+ function _data_prepare($mode=FTP_ASCII) {
+ if(!$this->_settype($mode)) return FALSE;
+ $this->SendMSG("Creating data socket");
+ $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+ if ($this->_ftp_data_sock < 0) {
+ $this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock)));
+ return FALSE;
+ }
+ if(!$this->_settimeout($this->_ftp_data_sock)) {
+ $this->_data_close();
+ return FALSE;
+ }
+ if($this->_passive) {
+ if(!$this->_exec("PASV", "pasv")) {
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!$this->_checkCode()) {
+ $this->_data_close();
+ return FALSE;
+ }
+ $ip_port = explode(",", preg_replace("/^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*$/s", "\\1", $this->_message));
+ $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
+ $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
+ $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
+ if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
+ $this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock)));
+ $this->_data_close();
+ return FALSE;
+ }
+ else $this->_ftp_temp_sock=$this->_ftp_data_sock;
+ } else {
+ if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) {
+ $this->PushError("_data_prepare","cannot get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock)));
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!@socket_bind($this->_ftp_data_sock,$addr)){
+ $this->PushError("_data_prepare","cannot bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!@socket_listen($this->_ftp_data_sock)) {
+ $this->PushError("_data_prepare","cannot listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
+ $this->PushError("_data_prepare","cannot get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock)));
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) {
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!$this->_checkCode()) {
+ $this->_data_close();
+ return FALSE;
+ }
+ }
+ return TRUE;
+ }
+
+ function _data_read($mode=FTP_ASCII, $fp=NULL) {
+ $NewLine=$this->_eol_code[$this->OS_local];
+ if(is_resource($fp)) $out=0;
+ else $out="";
+ if(!$this->_passive) {
+ $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
+ $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
+ if($this->_ftp_temp_sock===FALSE) {
+ $this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
+ $this->_data_close();
+ return FALSE;
+ }
+ }
+
+ while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) {
+ if($block==="") break;
+ if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
+ if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
+ else $out.=$block;
+ }
+ return $out;
+ }
+
+ function _data_write($mode=FTP_ASCII, $fp=NULL) {
+ $NewLine=$this->_eol_code[$this->OS_local];
+ if(is_resource($fp)) $out=0;
+ else $out="";
+ if(!$this->_passive) {
+ $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
+ $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
+ if($this->_ftp_temp_sock===FALSE) {
+ $this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
+ $this->_data_close();
+ return false;
+ }
+ }
+ if(is_resource($fp)) {
+ while(!feof($fp)) {
+ $block=fread($fp, $this->_ftp_buff_size);
+ if(!$this->_data_write_block($mode, $block)) return false;
+ }
+ } elseif(!$this->_data_write_block($mode, $fp)) return false;
+ return true;
+ }
+
+ function _data_write_block($mode, $block) {
+ if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
+ do {
+ if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) {
+ $this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
+ $this->_data_close();
+ return FALSE;
+ }
+ $block=substr($block, $t);
+ } while(!empty($block));
+ return true;
+ }
+
+ function _data_close() {
+ @socket_close($this->_ftp_temp_sock);
+ @socket_close($this->_ftp_data_sock);
+ $this->SendMSG("Disconnected data from remote host");
+ return TRUE;
+ }
+
+ function _quit() {
+ if($this->_connected) {
+ @socket_close($this->_ftp_control_sock);
+ $this->_connected=false;
+ $this->SendMSG("Socket closed");
+ }
+ }
+}
+?>
diff --git a/wp-admin/includes/class-ftp.php b/wp-admin/includes/class-ftp.php
new file mode 100644
index 0000000..7658a0b
--- /dev/null
+++ b/wp-admin/includes/class-ftp.php
@@ -0,0 +1,913 @@
+LocalEcho=$le;
+ $this->Verbose=$verb;
+ $this->_lastaction=NULL;
+ $this->_error_array=array();
+ $this->_eol_code=array(FTP_OS_Unix=>"\n", FTP_OS_Mac=>"\r", FTP_OS_Windows=>"\r\n");
+ $this->AuthorizedTransferMode=array(FTP_AUTOASCII, FTP_ASCII, FTP_BINARY);
+ $this->OS_FullName=array(FTP_OS_Unix => 'UNIX', FTP_OS_Windows => 'WINDOWS', FTP_OS_Mac => 'MACOS');
+ $this->AutoAsciiExt=array("ASP","BAT","C","CPP","CSS","CSV","JS","H","HTM","HTML","SHTML","INI","LOG","PHP3","PHTML","PL","PERL","SH","SQL","TXT");
+ $this->_port_available=($port_mode==TRUE);
+ $this->SendMSG("Staring FTP client class".($this->_port_available?"":" without PORT mode support"));
+ $this->_connected=FALSE;
+ $this->_ready=FALSE;
+ $this->_can_restore=FALSE;
+ $this->_code=0;
+ $this->_message="";
+ $this->_ftp_buff_size=4096;
+ $this->_curtype=NULL;
+ $this->SetUmask(0022);
+ $this->SetType(FTP_AUTOASCII);
+ $this->SetTimeout(30);
+ $this->Passive(!$this->_port_available);
+ $this->_login="anonymous";
+ $this->_password="anon@ftp.com";
+ $this->_features=array();
+ $this->OS_local=FTP_OS_Unix;
+ $this->OS_remote=FTP_OS_Unix;
+ $this->features=array();
+ if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $this->OS_local=FTP_OS_Windows;
+ elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'MAC') $this->OS_local=FTP_OS_Mac;
+ }
+
+ function ftp_base($port_mode=FALSE) {
+ $this->__construct($port_mode);
+ }
+
+//
+//
+//
+
+ function parselisting($line) {
+ $is_windows = ($this->OS_remote == FTP_OS_Windows);
+ if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|) +(.+)/",$line,$lucifer)) {
+ $b = array();
+ if ($lucifer[3]<70) { $lucifer[3]+=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
+ $b['isdir'] = ($lucifer[7]=="");
+ if ( $b['isdir'] )
+ $b['type'] = 'd';
+ else
+ $b['type'] = 'f';
+ $b['size'] = $lucifer[7];
+ $b['month'] = $lucifer[1];
+ $b['day'] = $lucifer[2];
+ $b['year'] = $lucifer[3];
+ $b['hour'] = $lucifer[4];
+ $b['minute'] = $lucifer[5];
+ $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
+ $b['am/pm'] = $lucifer[6];
+ $b['name'] = $lucifer[8];
+ } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
+ //echo $line."\n";
+ $lcount=count($lucifer);
+ if ($lcount<8) return '';
+ $b = array();
+ $b['isdir'] = $lucifer[0][0] === "d";
+ $b['islink'] = $lucifer[0][0] === "l";
+ if ( $b['isdir'] )
+ $b['type'] = 'd';
+ elseif ( $b['islink'] )
+ $b['type'] = 'l';
+ else
+ $b['type'] = 'f';
+ $b['perms'] = $lucifer[0];
+ $b['number'] = $lucifer[1];
+ $b['owner'] = $lucifer[2];
+ $b['group'] = $lucifer[3];
+ $b['size'] = $lucifer[4];
+ if ($lcount==8) {
+ sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
+ sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
+ $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
+ $b['name'] = $lucifer[7];
+ } else {
+ $b['month'] = $lucifer[5];
+ $b['day'] = $lucifer[6];
+ if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
+ $b['year'] = gmdate("Y");
+ $b['hour'] = $l2[1];
+ $b['minute'] = $l2[2];
+ } else {
+ $b['year'] = $lucifer[7];
+ $b['hour'] = 0;
+ $b['minute'] = 0;
+ }
+ $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
+ $b['name'] = $lucifer[8];
+ }
+ }
+
+ return $b;
+ }
+
+ function SendMSG($message = "", $crlf=true) {
+ if ($this->Verbose) {
+ echo $message.($crlf?CRLF:"");
+ flush();
+ }
+ return TRUE;
+ }
+
+ function SetType($mode=FTP_AUTOASCII) {
+ if(!in_array($mode, $this->AuthorizedTransferMode)) {
+ $this->SendMSG("Wrong type");
+ return FALSE;
+ }
+ $this->_type=$mode;
+ $this->SendMSG("Transfer type: ".($this->_type==FTP_BINARY?"binary":($this->_type==FTP_ASCII?"ASCII":"auto ASCII") ) );
+ return TRUE;
+ }
+
+ function _settype($mode=FTP_ASCII) {
+ if($this->_ready) {
+ if($mode==FTP_BINARY) {
+ if($this->_curtype!=FTP_BINARY) {
+ if(!$this->_exec("TYPE I", "SetType")) return FALSE;
+ $this->_curtype=FTP_BINARY;
+ }
+ } elseif($this->_curtype!=FTP_ASCII) {
+ if(!$this->_exec("TYPE A", "SetType")) return FALSE;
+ $this->_curtype=FTP_ASCII;
+ }
+ } else return FALSE;
+ return TRUE;
+ }
+
+ function Passive($pasv=NULL) {
+ if(is_null($pasv)) $this->_passive=!$this->_passive;
+ else $this->_passive=$pasv;
+ if(!$this->_port_available and !$this->_passive) {
+ $this->SendMSG("Only passive connections available!");
+ $this->_passive=TRUE;
+ return FALSE;
+ }
+ $this->SendMSG("Passive mode ".($this->_passive?"on":"off"));
+ return TRUE;
+ }
+
+ function SetServer($host, $port=21, $reconnect=true) {
+ if(!is_long($port)) {
+ $this->verbose=true;
+ $this->SendMSG("Incorrect port syntax");
+ return FALSE;
+ } else {
+ $ip=@gethostbyname($host);
+ $dns=@gethostbyaddr($host);
+ if(!$ip) $ip=$host;
+ if(!$dns) $dns=$host;
+ // Validate the IPAddress PHP4 returns -1 for invalid, PHP5 false
+ // -1 === "255.255.255.255" which is the broadcast address which is also going to be invalid
+ $ipaslong = ip2long($ip);
+ if ( ($ipaslong == false) || ($ipaslong === -1) ) {
+ $this->SendMSG("Wrong host name/address \"".$host."\"");
+ return FALSE;
+ }
+ $this->_host=$ip;
+ $this->_fullhost=$dns;
+ $this->_port=$port;
+ $this->_dataport=$port-1;
+ }
+ $this->SendMSG("Host \"".$this->_fullhost."(".$this->_host."):".$this->_port."\"");
+ if($reconnect){
+ if($this->_connected) {
+ $this->SendMSG("Reconnecting");
+ if(!$this->quit(FTP_FORCE)) return FALSE;
+ if(!$this->connect()) return FALSE;
+ }
+ }
+ return TRUE;
+ }
+
+ function SetUmask($umask=0022) {
+ $this->_umask=$umask;
+ umask($this->_umask);
+ $this->SendMSG("UMASK 0".decoct($this->_umask));
+ return TRUE;
+ }
+
+ function SetTimeout($timeout=30) {
+ $this->_timeout=$timeout;
+ $this->SendMSG("Timeout ".$this->_timeout);
+ if($this->_connected)
+ if(!$this->_settimeout($this->_ftp_control_sock)) return FALSE;
+ return TRUE;
+ }
+
+ function connect($server=NULL) {
+ if(!empty($server)) {
+ if(!$this->SetServer($server)) return false;
+ }
+ if($this->_ready) return true;
+ $this->SendMsg('Local OS : '.$this->OS_FullName[$this->OS_local]);
+ if(!($this->_ftp_control_sock = $this->_connect($this->_host, $this->_port))) {
+ $this->SendMSG("Error : Cannot connect to remote host \"".$this->_fullhost." :".$this->_port."\"");
+ return FALSE;
+ }
+ $this->SendMSG("Connected to remote host \"".$this->_fullhost.":".$this->_port."\". Waiting for greeting.");
+ do {
+ if(!$this->_readmsg()) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ $this->_lastaction=time();
+ } while($this->_code<200);
+ $this->_ready=true;
+ $syst=$this->systype();
+ if(!$syst) $this->SendMSG("Cannot detect remote OS");
+ else {
+ if(preg_match("/win|dos|novell/i", $syst[0])) $this->OS_remote=FTP_OS_Windows;
+ elseif(preg_match("/os/i", $syst[0])) $this->OS_remote=FTP_OS_Mac;
+ elseif(preg_match("/(li|u)nix/i", $syst[0])) $this->OS_remote=FTP_OS_Unix;
+ else $this->OS_remote=FTP_OS_Mac;
+ $this->SendMSG("Remote OS: ".$this->OS_FullName[$this->OS_remote]);
+ }
+ if(!$this->features()) $this->SendMSG("Cannot get features list. All supported - disabled");
+ else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
+ return TRUE;
+ }
+
+ function quit($force=false) {
+ if($this->_ready) {
+ if(!$this->_exec("QUIT") and !$force) return FALSE;
+ if(!$this->_checkCode() and !$force) return FALSE;
+ $this->_ready=false;
+ $this->SendMSG("Session finished");
+ }
+ $this->_quit();
+ return TRUE;
+ }
+
+ function login($user=NULL, $pass=NULL) {
+ if(!is_null($user)) $this->_login=$user;
+ else $this->_login="anonymous";
+ if(!is_null($pass)) $this->_password=$pass;
+ else $this->_password="anon@anon.com";
+ if(!$this->_exec("USER ".$this->_login, "login")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ if($this->_code!=230) {
+ if(!$this->_exec((($this->_code==331)?"PASS ":"ACCT ").$this->_password, "login")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ }
+ $this->SendMSG("Authentication succeeded");
+ if(empty($this->_features)) {
+ if(!$this->features()) $this->SendMSG("Cannot get features list. All supported - disabled");
+ else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
+ }
+ return TRUE;
+ }
+
+ function pwd() {
+ if(!$this->_exec("PWD", "pwd")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return preg_replace("/^[0-9]{3} \"(.+)\".*$/s", "\\1", $this->_message);
+ }
+
+ function cdup() {
+ if(!$this->_exec("CDUP", "cdup")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return true;
+ }
+
+ function chdir($pathname) {
+ if(!$this->_exec("CWD ".$pathname, "chdir")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return TRUE;
+ }
+
+ function rmdir($pathname) {
+ if(!$this->_exec("RMD ".$pathname, "rmdir")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return TRUE;
+ }
+
+ function mkdir($pathname) {
+ if(!$this->_exec("MKD ".$pathname, "mkdir")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return TRUE;
+ }
+
+ function rename($from, $to) {
+ if(!$this->_exec("RNFR ".$from, "rename")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ if($this->_code==350) {
+ if(!$this->_exec("RNTO ".$to, "rename")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ } else return FALSE;
+ return TRUE;
+ }
+
+ function filesize($pathname) {
+ if(!isset($this->_features["SIZE"])) {
+ $this->PushError("filesize", "not supported by server");
+ return FALSE;
+ }
+ if(!$this->_exec("SIZE ".$pathname, "filesize")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return preg_replace("/^[0-9]{3} ([0-9]+).*$/s", "\\1", $this->_message);
+ }
+
+ function abort() {
+ if(!$this->_exec("ABOR", "abort")) return FALSE;
+ if(!$this->_checkCode()) {
+ if($this->_code!=426) return FALSE;
+ if(!$this->_readmsg("abort")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ }
+ return true;
+ }
+
+ function mdtm($pathname) {
+ if(!isset($this->_features["MDTM"])) {
+ $this->PushError("mdtm", "not supported by server");
+ return FALSE;
+ }
+ if(!$this->_exec("MDTM ".$pathname, "mdtm")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ $mdtm = preg_replace("/^[0-9]{3} ([0-9]+).*$/s", "\\1", $this->_message);
+ $date = sscanf($mdtm, "%4d%2d%2d%2d%2d%2d");
+ $timestamp = mktime($date[3], $date[4], $date[5], $date[1], $date[2], $date[0]);
+ return $timestamp;
+ }
+
+ function systype() {
+ if(!$this->_exec("SYST", "systype")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ $DATA = explode(" ", $this->_message);
+ return array($DATA[1], $DATA[3]);
+ }
+
+ function delete($pathname) {
+ if(!$this->_exec("DELE ".$pathname, "delete")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return TRUE;
+ }
+
+ function site($command, $fnction="site") {
+ if(!$this->_exec("SITE ".$command, $fnction)) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return TRUE;
+ }
+
+ function chmod($pathname, $mode) {
+ if(!$this->site( sprintf('CHMOD %o %s', $mode, $pathname), "chmod")) return FALSE;
+ return TRUE;
+ }
+
+ function restore($from) {
+ if(!isset($this->_features["REST"])) {
+ $this->PushError("restore", "not supported by server");
+ return FALSE;
+ }
+ if($this->_curtype!=FTP_BINARY) {
+ $this->PushError("restore", "cannot restore in ASCII mode");
+ return FALSE;
+ }
+ if(!$this->_exec("REST ".$from, "resore")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return TRUE;
+ }
+
+ function features() {
+ if(!$this->_exec("FEAT", "features")) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ $f=preg_split("/[".CRLF."]+/", preg_replace("/[0-9]{3}[ -].*[".CRLF."]+/", "", $this->_message), -1, PREG_SPLIT_NO_EMPTY);
+ $this->_features=array();
+ foreach($f as $k=>$v) {
+ $v=explode(" ", trim($v));
+ $this->_features[array_shift($v)]=$v;
+ }
+ return true;
+ }
+
+ function rawlist($pathname="", $arg="") {
+ return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "LIST", "rawlist");
+ }
+
+ function nlist($pathname="", $arg="") {
+ return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "NLST", "nlist");
+ }
+
+ function is_exists($pathname) {
+ return $this->file_exists($pathname);
+ }
+
+ function file_exists($pathname) {
+ $exists=true;
+ if(!$this->_exec("RNFR ".$pathname, "rename")) $exists=FALSE;
+ else {
+ if(!$this->_checkCode()) $exists=FALSE;
+ $this->abort();
+ }
+ if($exists) $this->SendMSG("Remote file ".$pathname." exists");
+ else $this->SendMSG("Remote file ".$pathname." does not exist");
+ return $exists;
+ }
+
+ function fget($fp, $remotefile, $rest=0) {
+ if($this->_can_restore and $rest!=0) fseek($fp, $rest);
+ $pi=pathinfo($remotefile);
+ if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
+ else $mode=FTP_BINARY;
+ if(!$this->_data_prepare($mode)) {
+ return FALSE;
+ }
+ if($this->_can_restore and $rest!=0) $this->restore($rest);
+ if(!$this->_exec("RETR ".$remotefile, "get")) {
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!$this->_checkCode()) {
+ $this->_data_close();
+ return FALSE;
+ }
+ $out=$this->_data_read($mode, $fp);
+ $this->_data_close();
+ if(!$this->_readmsg()) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return $out;
+ }
+
+ function get($remotefile, $localfile=NULL, $rest=0) {
+ if(is_null($localfile)) $localfile=$remotefile;
+ if (@file_exists($localfile)) $this->SendMSG("Warning : local file will be overwritten");
+ $fp = @fopen($localfile, "w");
+ if (!$fp) {
+ $this->PushError("get","cannot open local file", "Cannot create \"".$localfile."\"");
+ return FALSE;
+ }
+ if($this->_can_restore and $rest!=0) fseek($fp, $rest);
+ $pi=pathinfo($remotefile);
+ if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
+ else $mode=FTP_BINARY;
+ if(!$this->_data_prepare($mode)) {
+ fclose($fp);
+ return FALSE;
+ }
+ if($this->_can_restore and $rest!=0) $this->restore($rest);
+ if(!$this->_exec("RETR ".$remotefile, "get")) {
+ $this->_data_close();
+ fclose($fp);
+ return FALSE;
+ }
+ if(!$this->_checkCode()) {
+ $this->_data_close();
+ fclose($fp);
+ return FALSE;
+ }
+ $out=$this->_data_read($mode, $fp);
+ fclose($fp);
+ $this->_data_close();
+ if(!$this->_readmsg()) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return $out;
+ }
+
+ function fput($remotefile, $fp, $rest=0) {
+ if($this->_can_restore and $rest!=0) fseek($fp, $rest);
+ $pi=pathinfo($remotefile);
+ if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
+ else $mode=FTP_BINARY;
+ if(!$this->_data_prepare($mode)) {
+ return FALSE;
+ }
+ if($this->_can_restore and $rest!=0) $this->restore($rest);
+ if(!$this->_exec("STOR ".$remotefile, "put")) {
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!$this->_checkCode()) {
+ $this->_data_close();
+ return FALSE;
+ }
+ $ret=$this->_data_write($mode, $fp);
+ $this->_data_close();
+ if(!$this->_readmsg()) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return $ret;
+ }
+
+ function put($localfile, $remotefile=NULL, $rest=0) {
+ if(is_null($remotefile)) $remotefile=$localfile;
+ if (!file_exists($localfile)) {
+ $this->PushError("put","cannot open local file", "No such file or directory \"".$localfile."\"");
+ return FALSE;
+ }
+ $fp = @fopen($localfile, "r");
+
+ if (!$fp) {
+ $this->PushError("put","cannot open local file", "Cannot read file \"".$localfile."\"");
+ return FALSE;
+ }
+ if($this->_can_restore and $rest!=0) fseek($fp, $rest);
+ $pi=pathinfo($localfile);
+ if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
+ else $mode=FTP_BINARY;
+ if(!$this->_data_prepare($mode)) {
+ fclose($fp);
+ return FALSE;
+ }
+ if($this->_can_restore and $rest!=0) $this->restore($rest);
+ if(!$this->_exec("STOR ".$remotefile, "put")) {
+ $this->_data_close();
+ fclose($fp);
+ return FALSE;
+ }
+ if(!$this->_checkCode()) {
+ $this->_data_close();
+ fclose($fp);
+ return FALSE;
+ }
+ $ret=$this->_data_write($mode, $fp);
+ fclose($fp);
+ $this->_data_close();
+ if(!$this->_readmsg()) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ return $ret;
+ }
+
+ function mput($local=".", $remote=NULL, $continious=false) {
+ $local=realpath($local);
+ if(!@file_exists($local)) {
+ $this->PushError("mput","cannot open local folder", "Cannot stat folder \"".$local."\"");
+ return FALSE;
+ }
+ if(!is_dir($local)) return $this->put($local, $remote);
+ if(empty($remote)) $remote=".";
+ elseif(!$this->file_exists($remote) and !$this->mkdir($remote)) return FALSE;
+ if($handle = opendir($local)) {
+ $list=array();
+ while (false !== ($file = readdir($handle))) {
+ if ($file != "." && $file != "..") $list[]=$file;
+ }
+ closedir($handle);
+ } else {
+ $this->PushError("mput","cannot open local folder", "Cannot read folder \"".$local."\"");
+ return FALSE;
+ }
+ if(empty($list)) return TRUE;
+ $ret=true;
+ foreach($list as $el) {
+ if(is_dir($local."/".$el)) $t=$this->mput($local."/".$el, $remote."/".$el);
+ else $t=$this->put($local."/".$el, $remote."/".$el);
+ if(!$t) {
+ $ret=FALSE;
+ if(!$continious) break;
+ }
+ }
+ return $ret;
+
+ }
+
+ function mget($remote, $local=".", $continious=false) {
+ $list=$this->rawlist($remote, "-lA");
+ if($list===false) {
+ $this->PushError("mget","cannot read remote folder list", "Cannot read remote folder \"".$remote."\" contents");
+ return FALSE;
+ }
+ if(empty($list)) return true;
+ if(!@file_exists($local)) {
+ if(!@mkdir($local)) {
+ $this->PushError("mget","cannot create local folder", "Cannot create folder \"".$local."\"");
+ return FALSE;
+ }
+ }
+ foreach($list as $k=>$v) {
+ $list[$k]=$this->parselisting($v);
+ if( ! $list[$k] or $list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
+ }
+ $ret=true;
+ foreach($list as $el) {
+ if($el["type"]=="d") {
+ if(!$this->mget($remote."/".$el["name"], $local."/".$el["name"], $continious)) {
+ $this->PushError("mget", "cannot copy folder", "Cannot copy remote folder \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
+ $ret=false;
+ if(!$continious) break;
+ }
+ } else {
+ if(!$this->get($remote."/".$el["name"], $local."/".$el["name"])) {
+ $this->PushError("mget", "cannot copy file", "Cannot copy remote file \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
+ $ret=false;
+ if(!$continious) break;
+ }
+ }
+ @chmod($local."/".$el["name"], $el["perms"]);
+ $t=strtotime($el["date"]);
+ if($t!==-1 and $t!==false) @touch($local."/".$el["name"], $t);
+ }
+ return $ret;
+ }
+
+ function mdel($remote, $continious=false) {
+ $list=$this->rawlist($remote, "-la");
+ if($list===false) {
+ $this->PushError("mdel","cannot read remote folder list", "Cannot read remote folder \"".$remote."\" contents");
+ return false;
+ }
+
+ foreach($list as $k=>$v) {
+ $list[$k]=$this->parselisting($v);
+ if( ! $list[$k] or $list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
+ }
+ $ret=true;
+
+ foreach($list as $el) {
+ if ( empty($el) )
+ continue;
+
+ if($el["type"]=="d") {
+ if(!$this->mdel($remote."/".$el["name"], $continious)) {
+ $ret=false;
+ if(!$continious) break;
+ }
+ } else {
+ if (!$this->delete($remote."/".$el["name"])) {
+ $this->PushError("mdel", "cannot delete file", "Cannot delete remote file \"".$remote."/".$el["name"]."\"");
+ $ret=false;
+ if(!$continious) break;
+ }
+ }
+ }
+
+ if(!$this->rmdir($remote)) {
+ $this->PushError("mdel", "cannot delete folder", "Cannot delete remote folder \"".$remote."/".$el["name"]."\"");
+ $ret=false;
+ }
+ return $ret;
+ }
+
+ function mmkdir($dir, $mode = 0777) {
+ if(empty($dir)) return FALSE;
+ if($this->is_exists($dir) or $dir == "/" ) return TRUE;
+ if(!$this->mmkdir(dirname($dir), $mode)) return false;
+ $r=$this->mkdir($dir, $mode);
+ $this->chmod($dir,$mode);
+ return $r;
+ }
+
+ function glob($pattern, $handle=NULL) {
+ $path=$output=null;
+ if(PHP_OS=='WIN32') $slash='\\';
+ else $slash='/';
+ $lastpos=strrpos($pattern,$slash);
+ if(!($lastpos===false)) {
+ $path=substr($pattern,0,-$lastpos-1);
+ $pattern=substr($pattern,$lastpos);
+ } else $path=getcwd();
+ if(is_array($handle) and !empty($handle)) {
+ foreach($handle as $dir) {
+ if($this->glob_pattern_match($pattern,$dir))
+ $output[]=$dir;
+ }
+ } else {
+ $handle=@opendir($path);
+ if($handle===false) return false;
+ while($dir=readdir($handle)) {
+ if($this->glob_pattern_match($pattern,$dir))
+ $output[]=$dir;
+ }
+ closedir($handle);
+ }
+ if(is_array($output)) return $output;
+ return false;
+ }
+
+ function glob_pattern_match($pattern,$subject) {
+ $out=null;
+ $chunks=explode(';',$pattern);
+ foreach($chunks as $pattern) {
+ $escape=array('$','^','.','{','}','(',')','[',']','|');
+ while(str_contains($pattern,'**'))
+ $pattern=str_replace('**','*',$pattern);
+ foreach($escape as $probe)
+ $pattern=str_replace($probe,"\\$probe",$pattern);
+ $pattern=str_replace('?*','*',
+ str_replace('*?','*',
+ str_replace('*',".*",
+ str_replace('?','.{1,1}',$pattern))));
+ $out[]=$pattern;
+ }
+ if(count($out)==1) return($this->glob_regexp("^$out[0]$",$subject));
+ else {
+ foreach($out as $tester)
+ // TODO: This should probably be glob_regexp(), but needs tests.
+ if($this->my_regexp("^$tester$",$subject)) return true;
+ }
+ return false;
+ }
+
+ function glob_regexp($pattern,$subject) {
+ $sensitive=(PHP_OS!='WIN32');
+ return ($sensitive?
+ preg_match( '/' . preg_quote( $pattern, '/' ) . '/', $subject ) :
+ preg_match( '/' . preg_quote( $pattern, '/' ) . '/i', $subject )
+ );
+ }
+
+ function dirlist($remote) {
+ $list=$this->rawlist($remote, "-la");
+ if($list===false) {
+ $this->PushError("dirlist","cannot read remote folder list", "Cannot read remote folder \"".$remote."\" contents");
+ return false;
+ }
+
+ $dirlist = array();
+ foreach($list as $k=>$v) {
+ $entry=$this->parselisting($v);
+ if ( empty($entry) )
+ continue;
+
+ if($entry["name"]=="." or $entry["name"]=="..")
+ continue;
+
+ $dirlist[$entry['name']] = $entry;
+ }
+
+ return $dirlist;
+ }
+//
+//
+//
+ function _checkCode() {
+ return ($this->_code<400 and $this->_code>0);
+ }
+
+ function _list($arg="", $cmd="LIST", $fnction="_list") {
+ if(!$this->_data_prepare()) return false;
+ if(!$this->_exec($cmd.$arg, $fnction)) {
+ $this->_data_close();
+ return FALSE;
+ }
+ if(!$this->_checkCode()) {
+ $this->_data_close();
+ return FALSE;
+ }
+ $out="";
+ if($this->_code<200) {
+ $out=$this->_data_read();
+ $this->_data_close();
+ if(!$this->_readmsg()) return FALSE;
+ if(!$this->_checkCode()) return FALSE;
+ if($out === FALSE ) return FALSE;
+ $out=preg_split("/[".CRLF."]+/", $out, -1, PREG_SPLIT_NO_EMPTY);
+// $this->SendMSG(implode($this->_eol_code[$this->OS_local], $out));
+ }
+ return $out;
+ }
+
+//
+//
+//
+// Gnre une erreur pour traitement externe la classe
+ function PushError($fctname,$msg,$desc=false){
+ $error=array();
+ $error['time']=time();
+ $error['fctname']=$fctname;
+ $error['msg']=$msg;
+ $error['desc']=$desc;
+ if($desc) $tmp=' ('.$desc.')'; else $tmp='';
+ $this->SendMSG($fctname.': '.$msg.$tmp);
+ return(array_push($this->_error_array,$error));
+ }
+
+// Rcupre une erreur externe
+ function PopError(){
+ if(count($this->_error_array)) return(array_pop($this->_error_array));
+ else return(false);
+ }
+}
+
+$mod_sockets = extension_loaded( 'sockets' );
+if ( ! $mod_sockets && function_exists( 'dl' ) && is_callable( 'dl' ) ) {
+ $prefix = ( PHP_SHLIB_SUFFIX == 'dll' ) ? 'php_' : '';
+ @dl( $prefix . 'sockets.' . PHP_SHLIB_SUFFIX ); // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.dlDeprecated
+ $mod_sockets = extension_loaded( 'sockets' );
+}
+
+require_once __DIR__ . "/class-ftp-" . ( $mod_sockets ? "sockets" : "pure" ) . ".php";
+
+if ( $mod_sockets ) {
+ class ftp extends ftp_sockets {}
+} else {
+ class ftp extends ftp_pure {}
+}
diff --git a/wp-admin/includes/class-language-pack-upgrader-skin.php b/wp-admin/includes/class-language-pack-upgrader-skin.php
new file mode 100644
index 0000000..57b0a1c
--- /dev/null
+++ b/wp-admin/includes/class-language-pack-upgrader-skin.php
@@ -0,0 +1,97 @@
+ '',
+ 'nonce' => '',
+ 'title' => __( 'Update Translations' ),
+ 'skip_header_footer' => false,
+ );
+ $args = wp_parse_args( $args, $defaults );
+ if ( $args['skip_header_footer'] ) {
+ $this->done_header = true;
+ $this->done_footer = true;
+ $this->display_footer_actions = false;
+ }
+ parent::__construct( $args );
+ }
+
+ /**
+ */
+ public function before() {
+ $name = $this->upgrader->get_name_for_update( $this->language_update );
+
+ echo '';
+
+ /* translators: 1: Project name (plugin, theme, or WordPress), 2: Language. */
+ printf( '
' . __( 'Updating translations for %1$s (%2$s)…' ) . ' ', $name, $this->language_update->language );
+ }
+
+ /**
+ * @since 5.9.0 Renamed `$error` to `$errors` for PHP 8 named parameter support.
+ *
+ * @param string|WP_Error $errors Errors.
+ */
+ public function error( $errors ) {
+ echo '
';
+ parent::error( $errors );
+ echo '
';
+ }
+
+ /**
+ */
+ public function after() {
+ echo '
';
+ }
+
+ /**
+ */
+ public function bulk_footer() {
+ $this->decrement_update_count( 'translation' );
+
+ $update_actions = array(
+ 'updates_page' => sprintf(
+ '%s ',
+ self_admin_url( 'update-core.php' ),
+ __( 'Go to WordPress Updates page' )
+ ),
+ );
+
+ /**
+ * Filters the list of action links available following a translations update.
+ *
+ * @since 3.7.0
+ *
+ * @param string[] $update_actions Array of translations update links.
+ */
+ $update_actions = apply_filters( 'update_translations_complete_actions', $update_actions );
+
+ if ( $update_actions && $this->display_footer_actions ) {
+ $this->feedback( implode( ' | ', $update_actions ) );
+ }
+ }
+}
diff --git a/wp-admin/includes/class-language-pack-upgrader.php b/wp-admin/includes/class-language-pack-upgrader.php
new file mode 100644
index 0000000..3c3d42a
--- /dev/null
+++ b/wp-admin/includes/class-language-pack-upgrader.php
@@ -0,0 +1,474 @@
+is_vcs_checkout( WP_CONTENT_DIR ) ) {
+ return;
+ }
+
+ foreach ( $language_updates as $key => $language_update ) {
+ $update = ! empty( $language_update->autoupdate );
+
+ /**
+ * Filters whether to asynchronously update translation for core, a plugin, or a theme.
+ *
+ * @since 4.0.0
+ *
+ * @param bool $update Whether to update.
+ * @param object $language_update The update offer.
+ */
+ $update = apply_filters( 'async_update_translation', $update, $language_update );
+
+ if ( ! $update ) {
+ unset( $language_updates[ $key ] );
+ }
+ }
+
+ if ( empty( $language_updates ) ) {
+ return;
+ }
+
+ // Re-use the automatic upgrader skin if the parent upgrader is using it.
+ if ( $upgrader && $upgrader->skin instanceof Automatic_Upgrader_Skin ) {
+ $skin = $upgrader->skin;
+ } else {
+ $skin = new Language_Pack_Upgrader_Skin(
+ array(
+ 'skip_header_footer' => true,
+ )
+ );
+ }
+
+ $lp_upgrader = new Language_Pack_Upgrader( $skin );
+ $lp_upgrader->bulk_upgrade( $language_updates );
+ }
+
+ /**
+ * Initializes the upgrade strings.
+ *
+ * @since 3.7.0
+ */
+ public function upgrade_strings() {
+ $this->strings['starting_upgrade'] = __( 'Some of your translations need updating. Sit tight for a few more seconds while they are updated as well.' );
+ $this->strings['up_to_date'] = __( 'Your translations are all up to date.' );
+ $this->strings['no_package'] = __( 'Update package not available.' );
+ /* translators: %s: Package URL. */
+ $this->strings['downloading_package'] = sprintf( __( 'Downloading translation from %s…' ), '%s ' );
+ $this->strings['unpack_package'] = __( 'Unpacking the update…' );
+ $this->strings['process_failed'] = __( 'Translation update failed.' );
+ $this->strings['process_success'] = __( 'Translation updated successfully.' );
+ $this->strings['remove_old'] = __( 'Removing the old version of the translation…' );
+ $this->strings['remove_old_failed'] = __( 'Could not remove the old translation.' );
+ }
+
+ /**
+ * Upgrades a language pack.
+ *
+ * @since 3.7.0
+ *
+ * @param string|false $update Optional. Whether an update offer is available. Default false.
+ * @param array $args Optional. Other optional arguments, see
+ * Language_Pack_Upgrader::bulk_upgrade(). Default empty array.
+ * @return array|bool|WP_Error The result of the upgrade, or a WP_Error object instead.
+ */
+ public function upgrade( $update = false, $args = array() ) {
+ if ( $update ) {
+ $update = array( $update );
+ }
+
+ $results = $this->bulk_upgrade( $update, $args );
+
+ if ( ! is_array( $results ) ) {
+ return $results;
+ }
+
+ return $results[0];
+ }
+
+ /**
+ * Upgrades several language packs at once.
+ *
+ * @since 3.7.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param object[] $language_updates Optional. Array of language packs to update. See {@see wp_get_translation_updates()}.
+ * Default empty array.
+ * @param array $args {
+ * Other arguments for upgrading multiple language packs. Default empty array.
+ *
+ * @type bool $clear_update_cache Whether to clear the update cache when done.
+ * Default true.
+ * }
+ * @return array|bool|WP_Error Will return an array of results, or true if there are no updates,
+ * false or WP_Error for initial errors.
+ */
+ public function bulk_upgrade( $language_updates = array(), $args = array() ) {
+ global $wp_filesystem;
+
+ $defaults = array(
+ 'clear_update_cache' => true,
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->upgrade_strings();
+
+ if ( ! $language_updates ) {
+ $language_updates = wp_get_translation_updates();
+ }
+
+ if ( empty( $language_updates ) ) {
+ $this->skin->header();
+ $this->skin->set_result( true );
+ $this->skin->feedback( 'up_to_date' );
+ $this->skin->bulk_footer();
+ $this->skin->footer();
+ return true;
+ }
+
+ if ( 'upgrader_process_complete' === current_filter() ) {
+ $this->skin->feedback( 'starting_upgrade' );
+ }
+
+ // Remove any existing upgrade filters from the plugin/theme upgraders #WP29425 & #WP29230.
+ remove_all_filters( 'upgrader_pre_install' );
+ remove_all_filters( 'upgrader_clear_destination' );
+ remove_all_filters( 'upgrader_post_install' );
+ remove_all_filters( 'upgrader_source_selection' );
+
+ add_filter( 'upgrader_source_selection', array( $this, 'check_package' ), 10, 2 );
+
+ $this->skin->header();
+
+ // Connect to the filesystem first.
+ $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
+ if ( ! $res ) {
+ $this->skin->footer();
+ return false;
+ }
+
+ $results = array();
+
+ $this->update_count = count( $language_updates );
+ $this->update_current = 0;
+
+ /*
+ * The filesystem's mkdir() is not recursive. Make sure WP_LANG_DIR exists,
+ * as we then may need to create a /plugins or /themes directory inside of it.
+ */
+ $remote_destination = $wp_filesystem->find_folder( WP_LANG_DIR );
+ if ( ! $wp_filesystem->exists( $remote_destination ) ) {
+ if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
+ return new WP_Error( 'mkdir_failed_lang_dir', $this->strings['mkdir_failed'], $remote_destination );
+ }
+ }
+
+ $language_updates_results = array();
+
+ foreach ( $language_updates as $language_update ) {
+
+ $this->skin->language_update = $language_update;
+
+ $destination = WP_LANG_DIR;
+ if ( 'plugin' === $language_update->type ) {
+ $destination .= '/plugins';
+ } elseif ( 'theme' === $language_update->type ) {
+ $destination .= '/themes';
+ }
+
+ ++$this->update_current;
+
+ $options = array(
+ 'package' => $language_update->package,
+ 'destination' => $destination,
+ 'clear_destination' => true,
+ 'abort_if_destination_exists' => false, // We expect the destination to exist.
+ 'clear_working' => true,
+ 'is_multi' => true,
+ 'hook_extra' => array(
+ 'language_update_type' => $language_update->type,
+ 'language_update' => $language_update,
+ ),
+ );
+
+ $result = $this->run( $options );
+
+ $results[] = $this->result;
+
+ // Prevent credentials auth screen from displaying multiple times.
+ if ( false === $result ) {
+ break;
+ }
+
+ $language_updates_results[] = array(
+ 'language' => $language_update->language,
+ 'type' => $language_update->type,
+ 'slug' => isset( $language_update->slug ) ? $language_update->slug : 'default',
+ 'version' => $language_update->version,
+ );
+ }
+
+ // Remove upgrade hooks which are not required for translation updates.
+ remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
+ remove_action( 'upgrader_process_complete', 'wp_version_check' );
+ remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
+ remove_action( 'upgrader_process_complete', 'wp_update_themes' );
+
+ /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
+ do_action(
+ 'upgrader_process_complete',
+ $this,
+ array(
+ 'action' => 'update',
+ 'type' => 'translation',
+ 'bulk' => true,
+ 'translations' => $language_updates_results,
+ )
+ );
+
+ // Re-add upgrade hooks.
+ add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
+ add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
+ add_action( 'upgrader_process_complete', 'wp_update_plugins', 10, 0 );
+ add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 );
+
+ $this->skin->bulk_footer();
+
+ $this->skin->footer();
+
+ // Clean up our hooks, in case something else does an upgrade on this connection.
+ remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
+
+ if ( $parsed_args['clear_update_cache'] ) {
+ wp_clean_update_cache();
+ }
+
+ return $results;
+ }
+
+ /**
+ * Checks that the package source contains .mo and .po files.
+ *
+ * Hooked to the {@see 'upgrader_source_selection'} filter by
+ * Language_Pack_Upgrader::bulk_upgrade().
+ *
+ * @since 3.7.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param string|WP_Error $source The path to the downloaded package source.
+ * @param string $remote_source Remote file source location.
+ * @return string|WP_Error The source as passed, or a WP_Error object on failure.
+ */
+ public function check_package( $source, $remote_source ) {
+ global $wp_filesystem;
+
+ if ( is_wp_error( $source ) ) {
+ return $source;
+ }
+
+ // Check that the folder contains a valid language.
+ $files = $wp_filesystem->dirlist( $remote_source );
+
+ // Check to see if a .po and .mo exist in the folder.
+ $po = false;
+ $mo = false;
+ foreach ( (array) $files as $file => $filedata ) {
+ if ( str_ends_with( $file, '.po' ) ) {
+ $po = true;
+ } elseif ( str_ends_with( $file, '.mo' ) ) {
+ $mo = true;
+ }
+ }
+
+ if ( ! $mo || ! $po ) {
+ return new WP_Error(
+ 'incompatible_archive_pomo',
+ $this->strings['incompatible_archive'],
+ sprintf(
+ /* translators: 1: .po, 2: .mo */
+ __( 'The language pack is missing either the %1$s or %2$s files.' ),
+ '.po
',
+ '.mo
'
+ )
+ );
+ }
+
+ return $source;
+ }
+
+ /**
+ * Gets the name of an item being updated.
+ *
+ * @since 3.7.0
+ *
+ * @param object $update The data for an update.
+ * @return string The name of the item being updated.
+ */
+ public function get_name_for_update( $update ) {
+ switch ( $update->type ) {
+ case 'core':
+ return 'WordPress'; // Not translated.
+
+ case 'theme':
+ $theme = wp_get_theme( $update->slug );
+ if ( $theme->exists() ) {
+ return $theme->Get( 'Name' );
+ }
+ break;
+ case 'plugin':
+ $plugin_data = get_plugins( '/' . $update->slug );
+ $plugin_data = reset( $plugin_data );
+ if ( $plugin_data ) {
+ return $plugin_data['Name'];
+ }
+ break;
+ }
+ return '';
+ }
+
+ /**
+ * Clears existing translations where this item is going to be installed into.
+ *
+ * @since 5.1.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param string $remote_destination The location on the remote filesystem to be cleared.
+ * @return bool|WP_Error True upon success, WP_Error on failure.
+ */
+ public function clear_destination( $remote_destination ) {
+ global $wp_filesystem;
+
+ $language_update = $this->skin->language_update;
+ $language_directory = WP_LANG_DIR . '/'; // Local path for use with glob().
+
+ if ( 'core' === $language_update->type ) {
+ $files = array(
+ $remote_destination . $language_update->language . '.po',
+ $remote_destination . $language_update->language . '.mo',
+ $remote_destination . 'admin-' . $language_update->language . '.po',
+ $remote_destination . 'admin-' . $language_update->language . '.mo',
+ $remote_destination . 'admin-network-' . $language_update->language . '.po',
+ $remote_destination . 'admin-network-' . $language_update->language . '.mo',
+ $remote_destination . 'continents-cities-' . $language_update->language . '.po',
+ $remote_destination . 'continents-cities-' . $language_update->language . '.mo',
+ );
+
+ $json_translation_files = glob( $language_directory . $language_update->language . '-*.json' );
+ if ( $json_translation_files ) {
+ foreach ( $json_translation_files as $json_translation_file ) {
+ $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file );
+ }
+ }
+ } else {
+ $files = array(
+ $remote_destination . $language_update->slug . '-' . $language_update->language . '.po',
+ $remote_destination . $language_update->slug . '-' . $language_update->language . '.mo',
+ );
+
+ $language_directory = $language_directory . $language_update->type . 's/';
+ $json_translation_files = glob( $language_directory . $language_update->slug . '-' . $language_update->language . '-*.json' );
+ if ( $json_translation_files ) {
+ foreach ( $json_translation_files as $json_translation_file ) {
+ $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file );
+ }
+ }
+ }
+
+ $files = array_filter( $files, array( $wp_filesystem, 'exists' ) );
+
+ // No files to delete.
+ if ( ! $files ) {
+ return true;
+ }
+
+ // Check all files are writable before attempting to clear the destination.
+ $unwritable_files = array();
+
+ // Check writability.
+ foreach ( $files as $file ) {
+ if ( ! $wp_filesystem->is_writable( $file ) ) {
+ // Attempt to alter permissions to allow writes and try again.
+ $wp_filesystem->chmod( $file, FS_CHMOD_FILE );
+ if ( ! $wp_filesystem->is_writable( $file ) ) {
+ $unwritable_files[] = $file;
+ }
+ }
+ }
+
+ if ( ! empty( $unwritable_files ) ) {
+ return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
+ }
+
+ foreach ( $files as $file ) {
+ if ( ! $wp_filesystem->delete( $file ) ) {
+ return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
+ }
+ }
+
+ return true;
+ }
+}
diff --git a/wp-admin/includes/class-pclzip.php b/wp-admin/includes/class-pclzip.php
new file mode 100644
index 0000000..3fdade5
--- /dev/null
+++ b/wp-admin/includes/class-pclzip.php
@@ -0,0 +1,5732 @@
+zipname = $p_zipname;
+ $this->zip_fd = 0;
+ $this->magic_quotes_status = -1;
+
+ // ----- Return
+ return;
+ }
+
+ public function PclZip($p_zipname) {
+ self::__construct($p_zipname);
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function :
+ // create($p_filelist, $p_add_dir="", $p_remove_dir="")
+ // create($p_filelist, $p_option, $p_option_value, ...)
+ // Description :
+ // This method supports two different synopsis. The first one is historical.
+ // This method creates a Zip Archive. The Zip file is created in the
+ // filesystem. The files and directories indicated in $p_filelist
+ // are added in the archive. See the parameters description for the
+ // supported format of $p_filelist.
+ // When a directory is in the list, the directory and its content is added
+ // in the archive.
+ // In this synopsis, the function takes an optional variable list of
+ // options. See below the supported options.
+ // Parameters :
+ // $p_filelist : An array containing file or directory names, or
+ // a string containing one filename or one directory name, or
+ // a string containing a list of filenames and/or directory
+ // names separated by spaces.
+ // $p_add_dir : A path to add before the real path of the archived file,
+ // in order to have it memorized in the archive.
+ // $p_remove_dir : A path to remove from the real path of the file to archive,
+ // in order to have a shorter path memorized in the archive.
+ // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
+ // is removed first, before $p_add_dir is added.
+ // Options :
+ // PCLZIP_OPT_ADD_PATH :
+ // PCLZIP_OPT_REMOVE_PATH :
+ // PCLZIP_OPT_REMOVE_ALL_PATH :
+ // PCLZIP_OPT_COMMENT :
+ // PCLZIP_CB_PRE_ADD :
+ // PCLZIP_CB_POST_ADD :
+ // Return Values :
+ // 0 on failure,
+ // The list of the added files, with a status of the add action.
+ // (see PclZip::listContent() for list entry format)
+ // --------------------------------------------------------------------------------
+ function create($p_filelist)
+ {
+ $v_result=1;
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Set default values
+ $v_options = array();
+ $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
+
+ // ----- Look for variable options arguments
+ $v_size = func_num_args();
+
+ // ----- Look for arguments
+ if ($v_size > 1) {
+ // ----- Get the arguments
+ $v_arg_list = func_get_args();
+
+ // ----- Remove from the options list the first argument
+ array_shift($v_arg_list);
+ $v_size--;
+
+ // ----- Look for first arg
+ if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
+
+ // ----- Parse the options
+ $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
+ array (PCLZIP_OPT_REMOVE_PATH => 'optional',
+ PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
+ PCLZIP_OPT_ADD_PATH => 'optional',
+ PCLZIP_CB_PRE_ADD => 'optional',
+ PCLZIP_CB_POST_ADD => 'optional',
+ PCLZIP_OPT_NO_COMPRESSION => 'optional',
+ PCLZIP_OPT_COMMENT => 'optional',
+ PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
+ PCLZIP_OPT_TEMP_FILE_ON => 'optional',
+ PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
+ //, PCLZIP_OPT_CRYPT => 'optional'
+ ));
+ if ($v_result != 1) {
+ return 0;
+ }
+ }
+
+ // ----- Look for 2 args
+ // Here we need to support the first historic synopsis of the
+ // method.
+ else {
+
+ // ----- Get the first argument
+ $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
+
+ // ----- Look for the optional second argument
+ if ($v_size == 2) {
+ $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
+ }
+ else if ($v_size > 2) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
+ "Invalid number / type of arguments");
+ return 0;
+ }
+ }
+ }
+
+ // ----- Look for default option values
+ $this->privOptionDefaultThreshold($v_options);
+
+ // ----- Init
+ $v_string_list = array();
+ $v_att_list = array();
+ $v_filedescr_list = array();
+ $p_result_list = array();
+
+ // ----- Look if the $p_filelist is really an array
+ if (is_array($p_filelist)) {
+
+ // ----- Look if the first element is also an array
+ // This will mean that this is a file description entry
+ if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
+ $v_att_list = $p_filelist;
+ }
+
+ // ----- The list is a list of string names
+ else {
+ $v_string_list = $p_filelist;
+ }
+ }
+
+ // ----- Look if the $p_filelist is a string
+ else if (is_string($p_filelist)) {
+ // ----- Create a list from the string
+ $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
+ }
+
+ // ----- Invalid variable type for $p_filelist
+ else {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
+ return 0;
+ }
+
+ // ----- Reformat the string list
+ if (sizeof($v_string_list) != 0) {
+ foreach ($v_string_list as $v_string) {
+ if ($v_string != '') {
+ $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
+ }
+ else {
+ }
+ }
+ }
+
+ // ----- For each file in the list check the attributes
+ $v_supported_attributes
+ = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
+ ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
+ ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
+ ,PCLZIP_ATT_FILE_MTIME => 'optional'
+ ,PCLZIP_ATT_FILE_CONTENT => 'optional'
+ ,PCLZIP_ATT_FILE_COMMENT => 'optional'
+ );
+ foreach ($v_att_list as $v_entry) {
+ $v_result = $this->privFileDescrParseAtt($v_entry,
+ $v_filedescr_list[],
+ $v_options,
+ $v_supported_attributes);
+ if ($v_result != 1) {
+ return 0;
+ }
+ }
+
+ // ----- Expand the filelist (expand directories)
+ $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
+ if ($v_result != 1) {
+ return 0;
+ }
+
+ // ----- Call the create fct
+ $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
+ if ($v_result != 1) {
+ return 0;
+ }
+
+ // ----- Return
+ return $p_result_list;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function :
+ // add($p_filelist, $p_add_dir="", $p_remove_dir="")
+ // add($p_filelist, $p_option, $p_option_value, ...)
+ // Description :
+ // This method supports two synopsis. The first one is historical.
+ // This methods add the list of files in an existing archive.
+ // If a file with the same name already exists, it is added at the end of the
+ // archive, the first one is still present.
+ // If the archive does not exist, it is created.
+ // Parameters :
+ // $p_filelist : An array containing file or directory names, or
+ // a string containing one filename or one directory name, or
+ // a string containing a list of filenames and/or directory
+ // names separated by spaces.
+ // $p_add_dir : A path to add before the real path of the archived file,
+ // in order to have it memorized in the archive.
+ // $p_remove_dir : A path to remove from the real path of the file to archive,
+ // in order to have a shorter path memorized in the archive.
+ // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
+ // is removed first, before $p_add_dir is added.
+ // Options :
+ // PCLZIP_OPT_ADD_PATH :
+ // PCLZIP_OPT_REMOVE_PATH :
+ // PCLZIP_OPT_REMOVE_ALL_PATH :
+ // PCLZIP_OPT_COMMENT :
+ // PCLZIP_OPT_ADD_COMMENT :
+ // PCLZIP_OPT_PREPEND_COMMENT :
+ // PCLZIP_CB_PRE_ADD :
+ // PCLZIP_CB_POST_ADD :
+ // Return Values :
+ // 0 on failure,
+ // The list of the added files, with a status of the add action.
+ // (see PclZip::listContent() for list entry format)
+ // --------------------------------------------------------------------------------
+ function add($p_filelist)
+ {
+ $v_result=1;
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Set default values
+ $v_options = array();
+ $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
+
+ // ----- Look for variable options arguments
+ $v_size = func_num_args();
+
+ // ----- Look for arguments
+ if ($v_size > 1) {
+ // ----- Get the arguments
+ $v_arg_list = func_get_args();
+
+ // ----- Remove form the options list the first argument
+ array_shift($v_arg_list);
+ $v_size--;
+
+ // ----- Look for first arg
+ if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
+
+ // ----- Parse the options
+ $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
+ array (PCLZIP_OPT_REMOVE_PATH => 'optional',
+ PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
+ PCLZIP_OPT_ADD_PATH => 'optional',
+ PCLZIP_CB_PRE_ADD => 'optional',
+ PCLZIP_CB_POST_ADD => 'optional',
+ PCLZIP_OPT_NO_COMPRESSION => 'optional',
+ PCLZIP_OPT_COMMENT => 'optional',
+ PCLZIP_OPT_ADD_COMMENT => 'optional',
+ PCLZIP_OPT_PREPEND_COMMENT => 'optional',
+ PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
+ PCLZIP_OPT_TEMP_FILE_ON => 'optional',
+ PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
+ //, PCLZIP_OPT_CRYPT => 'optional'
+ ));
+ if ($v_result != 1) {
+ return 0;
+ }
+ }
+
+ // ----- Look for 2 args
+ // Here we need to support the first historic synopsis of the
+ // method.
+ else {
+
+ // ----- Get the first argument
+ $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
+
+ // ----- Look for the optional second argument
+ if ($v_size == 2) {
+ $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
+ }
+ else if ($v_size > 2) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
+
+ // ----- Return
+ return 0;
+ }
+ }
+ }
+
+ // ----- Look for default option values
+ $this->privOptionDefaultThreshold($v_options);
+
+ // ----- Init
+ $v_string_list = array();
+ $v_att_list = array();
+ $v_filedescr_list = array();
+ $p_result_list = array();
+
+ // ----- Look if the $p_filelist is really an array
+ if (is_array($p_filelist)) {
+
+ // ----- Look if the first element is also an array
+ // This will mean that this is a file description entry
+ if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
+ $v_att_list = $p_filelist;
+ }
+
+ // ----- The list is a list of string names
+ else {
+ $v_string_list = $p_filelist;
+ }
+ }
+
+ // ----- Look if the $p_filelist is a string
+ else if (is_string($p_filelist)) {
+ // ----- Create a list from the string
+ $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
+ }
+
+ // ----- Invalid variable type for $p_filelist
+ else {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
+ return 0;
+ }
+
+ // ----- Reformat the string list
+ if (sizeof($v_string_list) != 0) {
+ foreach ($v_string_list as $v_string) {
+ $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
+ }
+ }
+
+ // ----- For each file in the list check the attributes
+ $v_supported_attributes
+ = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
+ ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
+ ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
+ ,PCLZIP_ATT_FILE_MTIME => 'optional'
+ ,PCLZIP_ATT_FILE_CONTENT => 'optional'
+ ,PCLZIP_ATT_FILE_COMMENT => 'optional'
+ );
+ foreach ($v_att_list as $v_entry) {
+ $v_result = $this->privFileDescrParseAtt($v_entry,
+ $v_filedescr_list[],
+ $v_options,
+ $v_supported_attributes);
+ if ($v_result != 1) {
+ return 0;
+ }
+ }
+
+ // ----- Expand the filelist (expand directories)
+ $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
+ if ($v_result != 1) {
+ return 0;
+ }
+
+ // ----- Call the create fct
+ $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
+ if ($v_result != 1) {
+ return 0;
+ }
+
+ // ----- Return
+ return $p_result_list;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : listContent()
+ // Description :
+ // This public method, gives the list of the files and directories, with their
+ // properties.
+ // The properties of each entries in the list are (used also in other functions) :
+ // filename : Name of the file. For a create or add action it is the filename
+ // given by the user. For an extract function it is the filename
+ // of the extracted file.
+ // stored_filename : Name of the file / directory stored in the archive.
+ // size : Size of the stored file.
+ // compressed_size : Size of the file's data compressed in the archive
+ // (without the headers overhead)
+ // mtime : Last known modification date of the file (UNIX timestamp)
+ // comment : Comment associated with the file
+ // folder : true | false
+ // index : index of the file in the archive
+ // status : status of the action (depending of the action) :
+ // Values are :
+ // ok : OK !
+ // filtered : the file / dir is not extracted (filtered by user)
+ // already_a_directory : the file can not be extracted because a
+ // directory with the same name already exists
+ // write_protected : the file can not be extracted because a file
+ // with the same name already exists and is
+ // write protected
+ // newer_exist : the file was not extracted because a newer file exists
+ // path_creation_fail : the file is not extracted because the folder
+ // does not exist and can not be created
+ // write_error : the file was not extracted because there was an
+ // error while writing the file
+ // read_error : the file was not extracted because there was an error
+ // while reading the file
+ // invalid_header : the file was not extracted because of an archive
+ // format error (bad file header)
+ // Note that each time a method can continue operating when there
+ // is an action error on a file, the error is only logged in the file status.
+ // Return Values :
+ // 0 on an unrecoverable failure,
+ // The list of the files in the archive.
+ // --------------------------------------------------------------------------------
+ function listContent()
+ {
+ $v_result=1;
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Check archive
+ if (!$this->privCheckFormat()) {
+ return(0);
+ }
+
+ // ----- Call the extracting fct
+ $p_list = array();
+ if (($v_result = $this->privList($p_list)) != 1)
+ {
+ unset($p_list);
+ return(0);
+ }
+
+ // ----- Return
+ return $p_list;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function :
+ // extract($p_path="./", $p_remove_path="")
+ // extract([$p_option, $p_option_value, ...])
+ // Description :
+ // This method supports two synopsis. The first one is historical.
+ // This method extract all the files / directories from the archive to the
+ // folder indicated in $p_path.
+ // If you want to ignore the 'root' part of path of the memorized files
+ // you can indicate this in the optional $p_remove_path parameter.
+ // By default, if a newer file with the same name already exists, the
+ // file is not extracted.
+ //
+ // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH options
+ // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
+ // at the end of the path value of PCLZIP_OPT_PATH.
+ // Parameters :
+ // $p_path : Path where the files and directories are to be extracted
+ // $p_remove_path : First part ('root' part) of the memorized path
+ // (if any similar) to remove while extracting.
+ // Options :
+ // PCLZIP_OPT_PATH :
+ // PCLZIP_OPT_ADD_PATH :
+ // PCLZIP_OPT_REMOVE_PATH :
+ // PCLZIP_OPT_REMOVE_ALL_PATH :
+ // PCLZIP_CB_PRE_EXTRACT :
+ // PCLZIP_CB_POST_EXTRACT :
+ // Return Values :
+ // 0 or a negative value on failure,
+ // The list of the extracted files, with a status of the action.
+ // (see PclZip::listContent() for list entry format)
+ // --------------------------------------------------------------------------------
+ function extract()
+ {
+ $v_result=1;
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Check archive
+ if (!$this->privCheckFormat()) {
+ return(0);
+ }
+
+ // ----- Set default values
+ $v_options = array();
+// $v_path = "./";
+ $v_path = '';
+ $v_remove_path = "";
+ $v_remove_all_path = false;
+
+ // ----- Look for variable options arguments
+ $v_size = func_num_args();
+
+ // ----- Default values for option
+ $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
+
+ // ----- Look for arguments
+ if ($v_size > 0) {
+ // ----- Get the arguments
+ $v_arg_list = func_get_args();
+
+ // ----- Look for first arg
+ if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
+
+ // ----- Parse the options
+ $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
+ array (PCLZIP_OPT_PATH => 'optional',
+ PCLZIP_OPT_REMOVE_PATH => 'optional',
+ PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
+ PCLZIP_OPT_ADD_PATH => 'optional',
+ PCLZIP_CB_PRE_EXTRACT => 'optional',
+ PCLZIP_CB_POST_EXTRACT => 'optional',
+ PCLZIP_OPT_SET_CHMOD => 'optional',
+ PCLZIP_OPT_BY_NAME => 'optional',
+ PCLZIP_OPT_BY_EREG => 'optional',
+ PCLZIP_OPT_BY_PREG => 'optional',
+ PCLZIP_OPT_BY_INDEX => 'optional',
+ PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
+ PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
+ PCLZIP_OPT_REPLACE_NEWER => 'optional'
+ ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
+ ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
+ PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
+ PCLZIP_OPT_TEMP_FILE_ON => 'optional',
+ PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
+ ));
+ if ($v_result != 1) {
+ return 0;
+ }
+
+ // ----- Set the arguments
+ if (isset($v_options[PCLZIP_OPT_PATH])) {
+ $v_path = $v_options[PCLZIP_OPT_PATH];
+ }
+ if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
+ $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
+ }
+ if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
+ $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
+ }
+ if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
+ // ----- Check for '/' in last path char
+ if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
+ $v_path .= '/';
+ }
+ $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
+ }
+ }
+
+ // ----- Look for 2 args
+ // Here we need to support the first historic synopsis of the
+ // method.
+ else {
+
+ // ----- Get the first argument
+ $v_path = $v_arg_list[0];
+
+ // ----- Look for the optional second argument
+ if ($v_size == 2) {
+ $v_remove_path = $v_arg_list[1];
+ }
+ else if ($v_size > 2) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
+
+ // ----- Return
+ return 0;
+ }
+ }
+ }
+
+ // ----- Look for default option values
+ $this->privOptionDefaultThreshold($v_options);
+
+ // ----- Trace
+
+ // ----- Call the extracting fct
+ $p_list = array();
+ $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
+ $v_remove_all_path, $v_options);
+ if ($v_result < 1) {
+ unset($p_list);
+ return(0);
+ }
+
+ // ----- Return
+ return $p_list;
+ }
+ // --------------------------------------------------------------------------------
+
+
+ // --------------------------------------------------------------------------------
+ // Function :
+ // extractByIndex($p_index, $p_path="./", $p_remove_path="")
+ // extractByIndex($p_index, [$p_option, $p_option_value, ...])
+ // Description :
+ // This method supports two synopsis. The first one is historical.
+ // This method is doing a partial extract of the archive.
+ // The extracted files or folders are identified by their index in the
+ // archive (from 0 to n).
+ // Note that if the index identify a folder, only the folder entry is
+ // extracted, not all the files included in the archive.
+ // Parameters :
+ // $p_index : A single index (integer) or a string of indexes of files to
+ // extract. The form of the string is "0,4-6,8-12" with only numbers
+ // and '-' for range or ',' to separate ranges. No spaces or ';'
+ // are allowed.
+ // $p_path : Path where the files and directories are to be extracted
+ // $p_remove_path : First part ('root' part) of the memorized path
+ // (if any similar) to remove while extracting.
+ // Options :
+ // PCLZIP_OPT_PATH :
+ // PCLZIP_OPT_ADD_PATH :
+ // PCLZIP_OPT_REMOVE_PATH :
+ // PCLZIP_OPT_REMOVE_ALL_PATH :
+ // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
+ // not as files.
+ // The resulting content is in a new field 'content' in the file
+ // structure.
+ // This option must be used alone (any other options are ignored).
+ // PCLZIP_CB_PRE_EXTRACT :
+ // PCLZIP_CB_POST_EXTRACT :
+ // Return Values :
+ // 0 on failure,
+ // The list of the extracted files, with a status of the action.
+ // (see PclZip::listContent() for list entry format)
+ // --------------------------------------------------------------------------------
+ //function extractByIndex($p_index, options...)
+ function extractByIndex($p_index)
+ {
+ $v_result=1;
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Check archive
+ if (!$this->privCheckFormat()) {
+ return(0);
+ }
+
+ // ----- Set default values
+ $v_options = array();
+// $v_path = "./";
+ $v_path = '';
+ $v_remove_path = "";
+ $v_remove_all_path = false;
+
+ // ----- Look for variable options arguments
+ $v_size = func_num_args();
+
+ // ----- Default values for option
+ $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
+
+ // ----- Look for arguments
+ if ($v_size > 1) {
+ // ----- Get the arguments
+ $v_arg_list = func_get_args();
+
+ // ----- Remove form the options list the first argument
+ array_shift($v_arg_list);
+ $v_size--;
+
+ // ----- Look for first arg
+ if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
+
+ // ----- Parse the options
+ $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
+ array (PCLZIP_OPT_PATH => 'optional',
+ PCLZIP_OPT_REMOVE_PATH => 'optional',
+ PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
+ PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
+ PCLZIP_OPT_ADD_PATH => 'optional',
+ PCLZIP_CB_PRE_EXTRACT => 'optional',
+ PCLZIP_CB_POST_EXTRACT => 'optional',
+ PCLZIP_OPT_SET_CHMOD => 'optional',
+ PCLZIP_OPT_REPLACE_NEWER => 'optional'
+ ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
+ ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
+ PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
+ PCLZIP_OPT_TEMP_FILE_ON => 'optional',
+ PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
+ ));
+ if ($v_result != 1) {
+ return 0;
+ }
+
+ // ----- Set the arguments
+ if (isset($v_options[PCLZIP_OPT_PATH])) {
+ $v_path = $v_options[PCLZIP_OPT_PATH];
+ }
+ if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
+ $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
+ }
+ if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
+ $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
+ }
+ if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
+ // ----- Check for '/' in last path char
+ if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
+ $v_path .= '/';
+ }
+ $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
+ }
+ if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
+ $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
+ }
+ else {
+ }
+ }
+
+ // ----- Look for 2 args
+ // Here we need to support the first historic synopsis of the
+ // method.
+ else {
+
+ // ----- Get the first argument
+ $v_path = $v_arg_list[0];
+
+ // ----- Look for the optional second argument
+ if ($v_size == 2) {
+ $v_remove_path = $v_arg_list[1];
+ }
+ else if ($v_size > 2) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
+
+ // ----- Return
+ return 0;
+ }
+ }
+ }
+
+ // ----- Trace
+
+ // ----- Trick
+ // Here I want to reuse extractByRule(), so I need to parse the $p_index
+ // with privParseOptions()
+ $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
+ $v_options_trick = array();
+ $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
+ array (PCLZIP_OPT_BY_INDEX => 'optional' ));
+ if ($v_result != 1) {
+ return 0;
+ }
+ $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
+
+ // ----- Look for default option values
+ $this->privOptionDefaultThreshold($v_options);
+
+ // ----- Call the extracting fct
+ if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
+ return(0);
+ }
+
+ // ----- Return
+ return $p_list;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function :
+ // delete([$p_option, $p_option_value, ...])
+ // Description :
+ // This method removes files from the archive.
+ // If no parameters are given, then all the archive is emptied.
+ // Parameters :
+ // None or optional arguments.
+ // Options :
+ // PCLZIP_OPT_BY_INDEX :
+ // PCLZIP_OPT_BY_NAME :
+ // PCLZIP_OPT_BY_EREG :
+ // PCLZIP_OPT_BY_PREG :
+ // Return Values :
+ // 0 on failure,
+ // The list of the files which are still present in the archive.
+ // (see PclZip::listContent() for list entry format)
+ // --------------------------------------------------------------------------------
+ function delete()
+ {
+ $v_result=1;
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Check archive
+ if (!$this->privCheckFormat()) {
+ return(0);
+ }
+
+ // ----- Set default values
+ $v_options = array();
+
+ // ----- Look for variable options arguments
+ $v_size = func_num_args();
+
+ // ----- Look for arguments
+ if ($v_size > 0) {
+ // ----- Get the arguments
+ $v_arg_list = func_get_args();
+
+ // ----- Parse the options
+ $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
+ array (PCLZIP_OPT_BY_NAME => 'optional',
+ PCLZIP_OPT_BY_EREG => 'optional',
+ PCLZIP_OPT_BY_PREG => 'optional',
+ PCLZIP_OPT_BY_INDEX => 'optional' ));
+ if ($v_result != 1) {
+ return 0;
+ }
+ }
+
+ // ----- Magic quotes trick
+ $this->privDisableMagicQuotes();
+
+ // ----- Call the delete fct
+ $v_list = array();
+ if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
+ $this->privSwapBackMagicQuotes();
+ unset($v_list);
+ return(0);
+ }
+
+ // ----- Magic quotes trick
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_list;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : deleteByIndex()
+ // Description :
+ // ***** Deprecated *****
+ // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be preferred.
+ // --------------------------------------------------------------------------------
+ function deleteByIndex($p_index)
+ {
+
+ $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
+
+ // ----- Return
+ return $p_list;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : properties()
+ // Description :
+ // This method gives the properties of the archive.
+ // The properties are :
+ // nb : Number of files in the archive
+ // comment : Comment associated with the archive file
+ // status : not_exist, ok
+ // Parameters :
+ // None
+ // Return Values :
+ // 0 on failure,
+ // An array with the archive properties.
+ // --------------------------------------------------------------------------------
+ function properties()
+ {
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Magic quotes trick
+ $this->privDisableMagicQuotes();
+
+ // ----- Check archive
+ if (!$this->privCheckFormat()) {
+ $this->privSwapBackMagicQuotes();
+ return(0);
+ }
+
+ // ----- Default properties
+ $v_prop = array();
+ $v_prop['comment'] = '';
+ $v_prop['nb'] = 0;
+ $v_prop['status'] = 'not_exist';
+
+ // ----- Look if file exists
+ if (@is_file($this->zipname))
+ {
+ // ----- Open the zip file
+ if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
+ {
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
+
+ // ----- Return
+ return 0;
+ }
+
+ // ----- Read the central directory information
+ $v_central_dir = array();
+ if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
+ {
+ $this->privSwapBackMagicQuotes();
+ return 0;
+ }
+
+ // ----- Close the zip file
+ $this->privCloseFd();
+
+ // ----- Set the user attributes
+ $v_prop['comment'] = $v_central_dir['comment'];
+ $v_prop['nb'] = $v_central_dir['entries'];
+ $v_prop['status'] = 'ok';
+ }
+
+ // ----- Magic quotes trick
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_prop;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : duplicate()
+ // Description :
+ // This method creates an archive by copying the content of an other one. If
+ // the archive already exist, it is replaced by the new one without any warning.
+ // Parameters :
+ // $p_archive : The filename of a valid archive, or
+ // a valid PclZip object.
+ // Return Values :
+ // 1 on success.
+ // 0 or a negative value on error (error code).
+ // --------------------------------------------------------------------------------
+ function duplicate($p_archive)
+ {
+ $v_result = 1;
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Look if the $p_archive is an instantiated PclZip object
+ if ($p_archive instanceof pclzip)
+ {
+
+ // ----- Duplicate the archive
+ $v_result = $this->privDuplicate($p_archive->zipname);
+ }
+
+ // ----- Look if the $p_archive is a string (so a filename)
+ else if (is_string($p_archive))
+ {
+
+ // ----- Check that $p_archive is a valid zip file
+ // TBC : Should also check the archive format
+ if (!is_file($p_archive)) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
+ $v_result = PCLZIP_ERR_MISSING_FILE;
+ }
+ else {
+ // ----- Duplicate the archive
+ $v_result = $this->privDuplicate($p_archive);
+ }
+ }
+
+ // ----- Invalid variable
+ else
+ {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
+ $v_result = PCLZIP_ERR_INVALID_PARAMETER;
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : merge()
+ // Description :
+ // This method merge the $p_archive_to_add archive at the end of the current
+ // one ($this).
+ // If the archive ($this) does not exist, the merge becomes a duplicate.
+ // If the $p_archive_to_add archive does not exist, the merge is a success.
+ // Parameters :
+ // $p_archive_to_add : It can be directly the filename of a valid zip archive,
+ // or a PclZip object archive.
+ // Return Values :
+ // 1 on success,
+ // 0 or negative values on error (see below).
+ // --------------------------------------------------------------------------------
+ function merge($p_archive_to_add)
+ {
+ $v_result = 1;
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Check archive
+ if (!$this->privCheckFormat()) {
+ return(0);
+ }
+
+ // ----- Look if the $p_archive_to_add is an instantiated PclZip object
+ if ($p_archive_to_add instanceof pclzip)
+ {
+
+ // ----- Merge the archive
+ $v_result = $this->privMerge($p_archive_to_add);
+ }
+
+ // ----- Look if the $p_archive_to_add is a string (so a filename)
+ else if (is_string($p_archive_to_add))
+ {
+
+ // ----- Create a temporary archive
+ $v_object_archive = new PclZip($p_archive_to_add);
+
+ // ----- Merge the archive
+ $v_result = $this->privMerge($v_object_archive);
+ }
+
+ // ----- Invalid variable
+ else
+ {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
+ $v_result = PCLZIP_ERR_INVALID_PARAMETER;
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+
+
+ // --------------------------------------------------------------------------------
+ // Function : errorCode()
+ // Description :
+ // Parameters :
+ // --------------------------------------------------------------------------------
+ function errorCode()
+ {
+ if (PCLZIP_ERROR_EXTERNAL == 1) {
+ return(PclErrorCode());
+ }
+ else {
+ return($this->error_code);
+ }
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : errorName()
+ // Description :
+ // Parameters :
+ // --------------------------------------------------------------------------------
+ function errorName($p_with_code=false)
+ {
+ $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
+ PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
+ PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
+ PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
+ PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
+ PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
+ PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
+ PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
+ PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
+ PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
+ PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
+ PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
+ PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
+ PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
+ PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
+ PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
+ PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
+ PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
+ PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
+ ,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
+ ,PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
+ );
+
+ if (isset($v_name[$this->error_code])) {
+ $v_value = $v_name[$this->error_code];
+ }
+ else {
+ $v_value = 'NoName';
+ }
+
+ if ($p_with_code) {
+ return($v_value.' ('.$this->error_code.')');
+ }
+ else {
+ return($v_value);
+ }
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : errorInfo()
+ // Description :
+ // Parameters :
+ // --------------------------------------------------------------------------------
+ function errorInfo($p_full=false)
+ {
+ if (PCLZIP_ERROR_EXTERNAL == 1) {
+ return(PclErrorString());
+ }
+ else {
+ if ($p_full) {
+ return($this->errorName(true)." : ".$this->error_string);
+ }
+ else {
+ return($this->error_string." [code ".$this->error_code."]");
+ }
+ }
+ }
+ // --------------------------------------------------------------------------------
+
+
+// --------------------------------------------------------------------------------
+// ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
+// ***** *****
+// ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
+// --------------------------------------------------------------------------------
+
+
+
+ // --------------------------------------------------------------------------------
+ // Function : privCheckFormat()
+ // Description :
+ // This method check that the archive exists and is a valid zip archive.
+ // Several level of check exists. (futur)
+ // Parameters :
+ // $p_level : Level of check. Default 0.
+ // 0 : Check the first bytes (magic codes) (default value))
+ // 1 : 0 + Check the central directory (futur)
+ // 2 : 1 + Check each file header (futur)
+ // Return Values :
+ // true on success,
+ // false on error, the error code is set.
+ // --------------------------------------------------------------------------------
+ function privCheckFormat($p_level=0)
+ {
+ $v_result = true;
+
+ // ----- Reset the file system cache
+ clearstatcache();
+
+ // ----- Reset the error handler
+ $this->privErrorReset();
+
+ // ----- Look if the file exits
+ if (!is_file($this->zipname)) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
+ return(false);
+ }
+
+ // ----- Check that the file is readable
+ if (!is_readable($this->zipname)) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
+ return(false);
+ }
+
+ // ----- Check the magic code
+ // TBC
+
+ // ----- Check the central header
+ // TBC
+
+ // ----- Check each file header
+ // TBC
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privParseOptions()
+ // Description :
+ // This internal methods reads the variable list of arguments ($p_options_list,
+ // $p_size) and generate an array with the options and values ($v_result_list).
+ // $v_requested_options contains the options that can be present and those that
+ // must be present.
+ // $v_requested_options is an array, with the option value as key, and 'optional',
+ // or 'mandatory' as value.
+ // Parameters :
+ // See above.
+ // Return Values :
+ // 1 on success.
+ // 0 on failure.
+ // --------------------------------------------------------------------------------
+ function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
+ {
+ $v_result=1;
+
+ // ----- Read the options
+ $i=0;
+ while ($i<$p_size) {
+
+ // ----- Check if the option is supported
+ if (!isset($v_requested_options[$p_options_list[$i]])) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Look for next option
+ switch ($p_options_list[$i]) {
+ // ----- Look for options that request a path value
+ case PCLZIP_OPT_PATH :
+ case PCLZIP_OPT_REMOVE_PATH :
+ case PCLZIP_OPT_ADD_PATH :
+ // ----- Check the number of parameters
+ if (($i+1) >= $p_size) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value
+ $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
+ $i++;
+ break;
+
+ case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
+ // ----- Check the number of parameters
+ if (($i+1) >= $p_size) {
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+ return PclZip::errorCode();
+ }
+
+ // ----- Check for incompatible options
+ if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
+ return PclZip::errorCode();
+ }
+
+ // ----- Check the value
+ $v_value = $p_options_list[$i+1];
+ if ((!is_integer($v_value)) || ($v_value<0)) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value (and convert it in bytes)
+ $v_result_list[$p_options_list[$i]] = $v_value*1048576;
+ $i++;
+ break;
+
+ case PCLZIP_OPT_TEMP_FILE_ON :
+ // ----- Check for incompatible options
+ if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
+ return PclZip::errorCode();
+ }
+
+ $v_result_list[$p_options_list[$i]] = true;
+ break;
+
+ case PCLZIP_OPT_TEMP_FILE_OFF :
+ // ----- Check for incompatible options
+ if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
+ return PclZip::errorCode();
+ }
+ // ----- Check for incompatible options
+ if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
+ return PclZip::errorCode();
+ }
+
+ $v_result_list[$p_options_list[$i]] = true;
+ break;
+
+ case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
+ // ----- Check the number of parameters
+ if (($i+1) >= $p_size) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value
+ if ( is_string($p_options_list[$i+1])
+ && ($p_options_list[$i+1] != '')) {
+ $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
+ $i++;
+ }
+ else {
+ }
+ break;
+
+ // ----- Look for options that request an array of string for value
+ case PCLZIP_OPT_BY_NAME :
+ // ----- Check the number of parameters
+ if (($i+1) >= $p_size) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value
+ if (is_string($p_options_list[$i+1])) {
+ $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
+ }
+ else if (is_array($p_options_list[$i+1])) {
+ $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
+ }
+ else {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+ $i++;
+ break;
+
+ // ----- Look for options that request an EREG or PREG expression
+ case PCLZIP_OPT_BY_EREG :
+ // ereg() is deprecated starting with PHP 5.3. Move PCLZIP_OPT_BY_EREG
+ // to PCLZIP_OPT_BY_PREG
+ $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
+ case PCLZIP_OPT_BY_PREG :
+ //case PCLZIP_OPT_CRYPT :
+ // ----- Check the number of parameters
+ if (($i+1) >= $p_size) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value
+ if (is_string($p_options_list[$i+1])) {
+ $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
+ }
+ else {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+ $i++;
+ break;
+
+ // ----- Look for options that takes a string
+ case PCLZIP_OPT_COMMENT :
+ case PCLZIP_OPT_ADD_COMMENT :
+ case PCLZIP_OPT_PREPEND_COMMENT :
+ // ----- Check the number of parameters
+ if (($i+1) >= $p_size) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
+ "Missing parameter value for option '"
+ .PclZipUtilOptionText($p_options_list[$i])
+ ."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value
+ if (is_string($p_options_list[$i+1])) {
+ $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
+ }
+ else {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
+ "Wrong parameter value for option '"
+ .PclZipUtilOptionText($p_options_list[$i])
+ ."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+ $i++;
+ break;
+
+ // ----- Look for options that request an array of index
+ case PCLZIP_OPT_BY_INDEX :
+ // ----- Check the number of parameters
+ if (($i+1) >= $p_size) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value
+ $v_work_list = array();
+ if (is_string($p_options_list[$i+1])) {
+
+ // ----- Remove spaces
+ $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
+
+ // ----- Parse items
+ $v_work_list = explode(",", $p_options_list[$i+1]);
+ }
+ else if (is_integer($p_options_list[$i+1])) {
+ $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
+ }
+ else if (is_array($p_options_list[$i+1])) {
+ $v_work_list = $p_options_list[$i+1];
+ }
+ else {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Reduce the index list
+ // each index item in the list must be a couple with a start and
+ // an end value : [0,3], [5-5], [8-10], ...
+ // ----- Check the format of each item
+ $v_sort_flag=false;
+ $v_sort_value=0;
+ for ($j=0; $j= $p_size) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value
+ $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
+ $i++;
+ break;
+
+ // ----- Look for options that request a call-back
+ case PCLZIP_CB_PRE_EXTRACT :
+ case PCLZIP_CB_POST_EXTRACT :
+ case PCLZIP_CB_PRE_ADD :
+ case PCLZIP_CB_POST_ADD :
+ /* for futur use
+ case PCLZIP_CB_PRE_DELETE :
+ case PCLZIP_CB_POST_DELETE :
+ case PCLZIP_CB_PRE_LIST :
+ case PCLZIP_CB_POST_LIST :
+ */
+ // ----- Check the number of parameters
+ if (($i+1) >= $p_size) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Get the value
+ $v_function_name = $p_options_list[$i+1];
+
+ // ----- Check that the value is a valid existing function
+ if (!function_exists($v_function_name)) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Set the attribute
+ $v_result_list[$p_options_list[$i]] = $v_function_name;
+ $i++;
+ break;
+
+ default :
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
+ "Unknown parameter '"
+ .$p_options_list[$i]."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Next options
+ $i++;
+ }
+
+ // ----- Look for mandatory options
+ if ($v_requested_options !== false) {
+ for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
+ // ----- Look for mandatory option
+ if ($v_requested_options[$key] == 'mandatory') {
+ // ----- Look if present
+ if (!isset($v_result_list[$key])) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+ }
+ }
+ }
+
+ // ----- Look for default values
+ if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
+
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privOptionDefaultThreshold()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privOptionDefaultThreshold(&$p_options)
+ {
+ $v_result=1;
+
+ if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
+ || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
+ return $v_result;
+ }
+
+ // ----- Get 'memory_limit' configuration value
+ $v_memory_limit = ini_get('memory_limit');
+ $v_memory_limit = trim($v_memory_limit);
+ $v_memory_limit_int = (int) $v_memory_limit;
+ $last = strtolower(substr($v_memory_limit, -1));
+
+ if($last == 'g')
+ //$v_memory_limit_int = $v_memory_limit_int*1024*1024*1024;
+ $v_memory_limit_int = $v_memory_limit_int*1073741824;
+ if($last == 'm')
+ //$v_memory_limit_int = $v_memory_limit_int*1024*1024;
+ $v_memory_limit_int = $v_memory_limit_int*1048576;
+ if($last == 'k')
+ $v_memory_limit_int = $v_memory_limit_int*1024;
+
+ $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit_int*PCLZIP_TEMPORARY_FILE_RATIO);
+
+
+ // ----- Sanity check : No threshold if value lower than 1M
+ if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
+ unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privFileDescrParseAtt()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // 1 on success.
+ // 0 on failure.
+ // --------------------------------------------------------------------------------
+ function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
+ {
+ $v_result=1;
+
+ // ----- For each file in the list check the attributes
+ foreach ($p_file_list as $v_key => $v_value) {
+
+ // ----- Check if the option is supported
+ if (!isset($v_requested_options[$v_key])) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Look for attribute
+ switch ($v_key) {
+ case PCLZIP_ATT_FILE_NAME :
+ if (!is_string($v_value)) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
+ return PclZip::errorCode();
+ }
+
+ $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
+
+ if ($p_filedescr['filename'] == '') {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
+ return PclZip::errorCode();
+ }
+
+ break;
+
+ case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
+ if (!is_string($v_value)) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
+ return PclZip::errorCode();
+ }
+
+ $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
+
+ if ($p_filedescr['new_short_name'] == '') {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
+ return PclZip::errorCode();
+ }
+ break;
+
+ case PCLZIP_ATT_FILE_NEW_FULL_NAME :
+ if (!is_string($v_value)) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
+ return PclZip::errorCode();
+ }
+
+ $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
+
+ if ($p_filedescr['new_full_name'] == '') {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
+ return PclZip::errorCode();
+ }
+ break;
+
+ // ----- Look for options that takes a string
+ case PCLZIP_ATT_FILE_COMMENT :
+ if (!is_string($v_value)) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
+ return PclZip::errorCode();
+ }
+
+ $p_filedescr['comment'] = $v_value;
+ break;
+
+ case PCLZIP_ATT_FILE_MTIME :
+ if (!is_integer($v_value)) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
+ return PclZip::errorCode();
+ }
+
+ $p_filedescr['mtime'] = $v_value;
+ break;
+
+ case PCLZIP_ATT_FILE_CONTENT :
+ $p_filedescr['content'] = $v_value;
+ break;
+
+ default :
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
+ "Unknown parameter '".$v_key."'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Look for mandatory options
+ if ($v_requested_options !== false) {
+ for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
+ // ----- Look for mandatory option
+ if ($v_requested_options[$key] == 'mandatory') {
+ // ----- Look if present
+ if (!isset($p_file_list[$key])) {
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
+ return PclZip::errorCode();
+ }
+ }
+ }
+ }
+
+ // end foreach
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privFileDescrExpand()
+ // Description :
+ // This method look for each item of the list to see if its a file, a folder
+ // or a string to be added as file. For any other type of files (link, other)
+ // just ignore the item.
+ // Then prepare the information that will be stored for that file.
+ // When its a folder, expand the folder with all the files that are in that
+ // folder (recursively).
+ // Parameters :
+ // Return Values :
+ // 1 on success.
+ // 0 on failure.
+ // --------------------------------------------------------------------------------
+ function privFileDescrExpand(&$p_filedescr_list, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Create a result list
+ $v_result_list = array();
+
+ // ----- Look each entry
+ for ($i=0; $iprivCalculateStoredFilename($v_descr, $p_options);
+
+ // ----- Add the descriptor in result list
+ $v_result_list[sizeof($v_result_list)] = $v_descr;
+
+ // ----- Look for folder
+ if ($v_descr['type'] == 'folder') {
+ // ----- List of items in folder
+ $v_dirlist_descr = array();
+ $v_dirlist_nb = 0;
+ if ($v_folder_handler = @opendir($v_descr['filename'])) {
+ while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
+
+ // ----- Skip '.' and '..'
+ if (($v_item_handler == '.') || ($v_item_handler == '..')) {
+ continue;
+ }
+
+ // ----- Compose the full filename
+ $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
+
+ // ----- Look for different stored filename
+ // Because the name of the folder was changed, the name of the
+ // files/sub-folders also change
+ if (($v_descr['stored_filename'] != $v_descr['filename'])
+ && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
+ if ($v_descr['stored_filename'] != '') {
+ $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
+ }
+ else {
+ $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
+ }
+ }
+
+ $v_dirlist_nb++;
+ }
+
+ @closedir($v_folder_handler);
+ }
+ else {
+ // TBC : unable to open folder in read mode
+ }
+
+ // ----- Expand each element of the list
+ if ($v_dirlist_nb != 0) {
+ // ----- Expand
+ if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
+ return $v_result;
+ }
+
+ // ----- Concat the resulting list
+ $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
+ }
+ else {
+ }
+
+ // ----- Free local array
+ unset($v_dirlist_descr);
+ }
+ }
+
+ // ----- Get the result list
+ $p_filedescr_list = $v_result_list;
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privCreate()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
+ {
+ $v_result=1;
+ $v_list_detail = array();
+
+ // ----- Magic quotes trick
+ $this->privDisableMagicQuotes();
+
+ // ----- Open the file in write mode
+ if (($v_result = $this->privOpenFd('wb')) != 1)
+ {
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Add the list of files
+ $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
+
+ // ----- Close
+ $this->privCloseFd();
+
+ // ----- Magic quotes trick
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privAdd()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
+ {
+ $v_result=1;
+ $v_list_detail = array();
+
+ // ----- Look if the archive exists or is empty
+ if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
+ {
+
+ // ----- Do a create
+ $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
+
+ // ----- Return
+ return $v_result;
+ }
+ // ----- Magic quotes trick
+ $this->privDisableMagicQuotes();
+
+ // ----- Open the zip file
+ if (($v_result=$this->privOpenFd('rb')) != 1)
+ {
+ // ----- Magic quotes trick
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Read the central directory information
+ $v_central_dir = array();
+ if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
+ {
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+ return $v_result;
+ }
+
+ // ----- Go to beginning of File
+ @rewind($this->zip_fd);
+
+ // ----- Creates a temporary file
+ $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
+
+ // ----- Open the temporary file in write mode
+ if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
+ {
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Copy the files from the archive to the temporary file
+ // TBC : Here I should better append the file and go back to erase the central dir
+ $v_size = $v_central_dir['offset'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = fread($this->zip_fd, $v_read_size);
+ @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Swap the file descriptor
+ // Here is a trick : I swap the temporary fd with the zip fd, in order to use
+ // the following methods on the temporary fil and not the real archive
+ $v_swap = $this->zip_fd;
+ $this->zip_fd = $v_zip_temp_fd;
+ $v_zip_temp_fd = $v_swap;
+
+ // ----- Add the files
+ $v_header_list = array();
+ if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
+ {
+ fclose($v_zip_temp_fd);
+ $this->privCloseFd();
+ @unlink($v_zip_temp_name);
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Store the offset of the central dir
+ $v_offset = @ftell($this->zip_fd);
+
+ // ----- Copy the block of file headers from the old archive
+ $v_size = $v_central_dir['size'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
+ @fwrite($this->zip_fd, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Create the Central Dir files header
+ for ($i=0, $v_count=0; $iprivWriteCentralFileHeader($v_header_list[$i])) != 1) {
+ fclose($v_zip_temp_fd);
+ $this->privCloseFd();
+ @unlink($v_zip_temp_name);
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_result;
+ }
+ $v_count++;
+ }
+
+ // ----- Transform the header to a 'usable' info
+ $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
+ }
+
+ // ----- Zip file comment
+ $v_comment = $v_central_dir['comment'];
+ if (isset($p_options[PCLZIP_OPT_COMMENT])) {
+ $v_comment = $p_options[PCLZIP_OPT_COMMENT];
+ }
+ if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
+ $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
+ }
+ if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
+ $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
+ }
+
+ // ----- Calculate the size of the central header
+ $v_size = @ftell($this->zip_fd)-$v_offset;
+
+ // ----- Create the central dir footer
+ if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
+ {
+ // ----- Reset the file list
+ unset($v_header_list);
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Swap back the file descriptor
+ $v_swap = $this->zip_fd;
+ $this->zip_fd = $v_zip_temp_fd;
+ $v_zip_temp_fd = $v_swap;
+
+ // ----- Close
+ $this->privCloseFd();
+
+ // ----- Close the temporary file
+ @fclose($v_zip_temp_fd);
+
+ // ----- Magic quotes trick
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Delete the zip file
+ // TBC : I should test the result ...
+ @unlink($this->zipname);
+
+ // ----- Rename the temporary file
+ // TBC : I should test the result ...
+ //@rename($v_zip_temp_name, $this->zipname);
+ PclZipUtilRename($v_zip_temp_name, $this->zipname);
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privOpenFd()
+ // Description :
+ // Parameters :
+ // --------------------------------------------------------------------------------
+ function privOpenFd($p_mode)
+ {
+ $v_result=1;
+
+ // ----- Look if already open
+ if ($this->zip_fd != 0)
+ {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Open the zip file
+ if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
+ {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privCloseFd()
+ // Description :
+ // Parameters :
+ // --------------------------------------------------------------------------------
+ function privCloseFd()
+ {
+ $v_result=1;
+
+ if ($this->zip_fd != 0)
+ @fclose($this->zip_fd);
+ $this->zip_fd = 0;
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privAddList()
+ // Description :
+ // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
+ // different from the real path of the file. This is useful if you want to have PclTar
+ // running in any directory, and memorize relative path from an other directory.
+ // Parameters :
+ // $p_list : An array containing the file or directory names to add in the tar
+ // $p_result_list : list of added files with their properties (specially the status field)
+ // $p_add_dir : Path to add in the filename path archived
+ // $p_remove_dir : Path to remove in the filename path archived
+ // Return Values :
+ // --------------------------------------------------------------------------------
+// function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
+ function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Add the files
+ $v_header_list = array();
+ if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
+ {
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Store the offset of the central dir
+ $v_offset = @ftell($this->zip_fd);
+
+ // ----- Create the Central Dir files header
+ for ($i=0,$v_count=0; $iprivWriteCentralFileHeader($v_header_list[$i])) != 1) {
+ // ----- Return
+ return $v_result;
+ }
+ $v_count++;
+ }
+
+ // ----- Transform the header to a 'usable' info
+ $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
+ }
+
+ // ----- Zip file comment
+ $v_comment = '';
+ if (isset($p_options[PCLZIP_OPT_COMMENT])) {
+ $v_comment = $p_options[PCLZIP_OPT_COMMENT];
+ }
+
+ // ----- Calculate the size of the central header
+ $v_size = @ftell($this->zip_fd)-$v_offset;
+
+ // ----- Create the central dir footer
+ if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
+ {
+ // ----- Reset the file list
+ unset($v_header_list);
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privAddFileList()
+ // Description :
+ // Parameters :
+ // $p_filedescr_list : An array containing the file description
+ // or directory names to add in the zip
+ // $p_result_list : list of added files with their properties (specially the status field)
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
+ {
+ $v_result=1;
+ $v_header = array();
+
+ // ----- Recuperate the current number of elt in list
+ $v_nb = sizeof($p_result_list);
+
+ // ----- Loop on the files
+ for ($j=0; ($jprivAddFile($p_filedescr_list[$j], $v_header,
+ $p_options);
+ if ($v_result != 1) {
+ return $v_result;
+ }
+
+ // ----- Store the file infos
+ $p_result_list[$v_nb++] = $v_header;
+ }
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privAddFile()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privAddFile($p_filedescr, &$p_header, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Working variable
+ $p_filename = $p_filedescr['filename'];
+
+ // TBC : Already done in the fileAtt check ... ?
+ if ($p_filename == "") {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Look for a stored different filename
+ /* TBC : Removed
+ if (isset($p_filedescr['stored_filename'])) {
+ $v_stored_filename = $p_filedescr['stored_filename'];
+ }
+ else {
+ $v_stored_filename = $p_filedescr['stored_filename'];
+ }
+ */
+
+ // ----- Set the file properties
+ clearstatcache();
+ $p_header['version'] = 20;
+ $p_header['version_extracted'] = 10;
+ $p_header['flag'] = 0;
+ $p_header['compression'] = 0;
+ $p_header['crc'] = 0;
+ $p_header['compressed_size'] = 0;
+ $p_header['filename_len'] = strlen($p_filename);
+ $p_header['extra_len'] = 0;
+ $p_header['disk'] = 0;
+ $p_header['internal'] = 0;
+ $p_header['offset'] = 0;
+ $p_header['filename'] = $p_filename;
+// TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
+ $p_header['stored_filename'] = $p_filedescr['stored_filename'];
+ $p_header['extra'] = '';
+ $p_header['status'] = 'ok';
+ $p_header['index'] = -1;
+
+ // ----- Look for regular file
+ if ($p_filedescr['type']=='file') {
+ $p_header['external'] = 0x00000000;
+ $p_header['size'] = filesize($p_filename);
+ }
+
+ // ----- Look for regular folder
+ else if ($p_filedescr['type']=='folder') {
+ $p_header['external'] = 0x00000010;
+ $p_header['mtime'] = filemtime($p_filename);
+ $p_header['size'] = filesize($p_filename);
+ }
+
+ // ----- Look for virtual file
+ else if ($p_filedescr['type'] == 'virtual_file') {
+ $p_header['external'] = 0x00000000;
+ $p_header['size'] = strlen($p_filedescr['content']);
+ }
+
+
+ // ----- Look for filetime
+ if (isset($p_filedescr['mtime'])) {
+ $p_header['mtime'] = $p_filedescr['mtime'];
+ }
+ else if ($p_filedescr['type'] == 'virtual_file') {
+ $p_header['mtime'] = time();
+ }
+ else {
+ $p_header['mtime'] = filemtime($p_filename);
+ }
+
+ // ------ Look for file comment
+ if (isset($p_filedescr['comment'])) {
+ $p_header['comment_len'] = strlen($p_filedescr['comment']);
+ $p_header['comment'] = $p_filedescr['comment'];
+ }
+ else {
+ $p_header['comment_len'] = 0;
+ $p_header['comment'] = '';
+ }
+
+ // ----- Look for pre-add callback
+ if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
+
+ // ----- Generate a local information
+ $v_local_header = array();
+ $this->privConvertHeader2FileInfo($p_header, $v_local_header);
+
+ // ----- Call the callback
+ // Here I do not use call_user_func() because I need to send a reference to the
+ // header.
+ $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
+ if ($v_result == 0) {
+ // ----- Change the file status
+ $p_header['status'] = "skipped";
+ $v_result = 1;
+ }
+
+ // ----- Update the information
+ // Only some fields can be modified
+ if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
+ $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
+ }
+ }
+
+ // ----- Look for empty stored filename
+ if ($p_header['stored_filename'] == "") {
+ $p_header['status'] = "filtered";
+ }
+
+ // ----- Check the path length
+ if (strlen($p_header['stored_filename']) > 0xFF) {
+ $p_header['status'] = 'filename_too_long';
+ }
+
+ // ----- Look if no error, or file not skipped
+ if ($p_header['status'] == 'ok') {
+
+ // ----- Look for a file
+ if ($p_filedescr['type'] == 'file') {
+ // ----- Look for using temporary file to zip
+ if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
+ && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
+ || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
+ && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])) ) ) {
+ $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
+ if ($v_result < PCLZIP_ERR_NO_ERROR) {
+ return $v_result;
+ }
+ }
+
+ // ----- Use "in memory" zip algo
+ else {
+
+ // ----- Open the source file
+ if (($v_file = @fopen($p_filename, "rb")) == 0) {
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the file content
+ if ($p_header['size'] > 0) {
+ $v_content = @fread($v_file, $p_header['size']);
+ }
+ else {
+ $v_content = '';
+ }
+
+ // ----- Close the file
+ @fclose($v_file);
+
+ // ----- Calculate the CRC
+ $p_header['crc'] = @crc32($v_content);
+
+ // ----- Look for no compression
+ if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
+ // ----- Set header parameters
+ $p_header['compressed_size'] = $p_header['size'];
+ $p_header['compression'] = 0;
+ }
+
+ // ----- Look for normal compression
+ else {
+ // ----- Compress the content
+ $v_content = @gzdeflate($v_content);
+
+ // ----- Set header parameters
+ $p_header['compressed_size'] = strlen($v_content);
+ $p_header['compression'] = 8;
+ }
+
+ // ----- Call the header generation
+ if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
+ @fclose($v_file);
+ return $v_result;
+ }
+
+ // ----- Write the compressed (or not) content
+ @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
+
+ }
+
+ }
+
+ // ----- Look for a virtual file (a file from string)
+ else if ($p_filedescr['type'] == 'virtual_file') {
+
+ $v_content = $p_filedescr['content'];
+
+ // ----- Calculate the CRC
+ $p_header['crc'] = @crc32($v_content);
+
+ // ----- Look for no compression
+ if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
+ // ----- Set header parameters
+ $p_header['compressed_size'] = $p_header['size'];
+ $p_header['compression'] = 0;
+ }
+
+ // ----- Look for normal compression
+ else {
+ // ----- Compress the content
+ $v_content = @gzdeflate($v_content);
+
+ // ----- Set header parameters
+ $p_header['compressed_size'] = strlen($v_content);
+ $p_header['compression'] = 8;
+ }
+
+ // ----- Call the header generation
+ if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
+ @fclose($v_file);
+ return $v_result;
+ }
+
+ // ----- Write the compressed (or not) content
+ @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
+ }
+
+ // ----- Look for a directory
+ else if ($p_filedescr['type'] == 'folder') {
+ // ----- Look for directory last '/'
+ if (@substr($p_header['stored_filename'], -1) != '/') {
+ $p_header['stored_filename'] .= '/';
+ }
+
+ // ----- Set the file properties
+ $p_header['size'] = 0;
+ //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
+ $p_header['external'] = 0x00000010; // Value for a folder : to be checked
+
+ // ----- Call the header generation
+ if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
+ {
+ return $v_result;
+ }
+ }
+ }
+
+ // ----- Look for post-add callback
+ if (isset($p_options[PCLZIP_CB_POST_ADD])) {
+
+ // ----- Generate a local information
+ $v_local_header = array();
+ $this->privConvertHeader2FileInfo($p_header, $v_local_header);
+
+ // ----- Call the callback
+ // Here I do not use call_user_func() because I need to send a reference to the
+ // header.
+ $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
+ if ($v_result == 0) {
+ // ----- Ignored
+ $v_result = 1;
+ }
+
+ // ----- Update the information
+ // Nothing can be modified
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privAddFileUsingTempFile()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
+ {
+ $v_result=PCLZIP_ERR_NO_ERROR;
+
+ // ----- Working variable
+ $p_filename = $p_filedescr['filename'];
+
+
+ // ----- Open the source file
+ if (($v_file = @fopen($p_filename, "rb")) == 0) {
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
+ return PclZip::errorCode();
+ }
+
+ // ----- Creates a compressed temporary file
+ $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
+ if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
+ fclose($v_file);
+ PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
+ $v_size = filesize($p_filename);
+ while ($v_size != 0) {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($v_file, $v_read_size);
+ //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
+ @gzputs($v_file_compressed, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Close the file
+ @fclose($v_file);
+ @gzclose($v_file_compressed);
+
+ // ----- Check the minimum file size
+ if (filesize($v_gzip_temp_name) < 18) {
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
+ return PclZip::errorCode();
+ }
+
+ // ----- Extract the compressed attributes
+ if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the gzip file header
+ $v_binary_data = @fread($v_file_compressed, 10);
+ $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
+
+ // ----- Check some parameters
+ $v_data_header['os'] = bin2hex($v_data_header['os']);
+
+ // ----- Read the gzip file footer
+ @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
+ $v_binary_data = @fread($v_file_compressed, 8);
+ $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
+
+ // ----- Set the attributes
+ $p_header['compression'] = ord($v_data_header['cm']);
+ //$p_header['mtime'] = $v_data_header['mtime'];
+ $p_header['crc'] = $v_data_footer['crc'];
+ $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
+
+ // ----- Close the file
+ @fclose($v_file_compressed);
+
+ // ----- Call the header generation
+ if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
+ return $v_result;
+ }
+
+ // ----- Add the compressed data
+ if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0)
+ {
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
+ fseek($v_file_compressed, 10);
+ $v_size = $p_header['compressed_size'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($v_file_compressed, $v_read_size);
+ //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
+ @fwrite($this->zip_fd, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Close the file
+ @fclose($v_file_compressed);
+
+ // ----- Unlink the temporary file
+ @unlink($v_gzip_temp_name);
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privCalculateStoredFilename()
+ // Description :
+ // Based on file descriptor properties and global options, this method
+ // calculate the filename that will be stored in the archive.
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privCalculateStoredFilename(&$p_filedescr, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Working variables
+ $p_filename = $p_filedescr['filename'];
+ if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
+ $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
+ }
+ else {
+ $p_add_dir = '';
+ }
+ if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
+ $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
+ }
+ else {
+ $p_remove_dir = '';
+ }
+ if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
+ $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
+ }
+ else {
+ $p_remove_all_dir = 0;
+ }
+
+
+ // ----- Look for full name change
+ if (isset($p_filedescr['new_full_name'])) {
+ // ----- Remove drive letter if any
+ $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
+ }
+
+ // ----- Look for path and/or short name change
+ else {
+
+ // ----- Look for short name change
+ // Its when we change just the filename but not the path
+ if (isset($p_filedescr['new_short_name'])) {
+ $v_path_info = pathinfo($p_filename);
+ $v_dir = '';
+ if ($v_path_info['dirname'] != '') {
+ $v_dir = $v_path_info['dirname'].'/';
+ }
+ $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
+ }
+ else {
+ // ----- Calculate the stored filename
+ $v_stored_filename = $p_filename;
+ }
+
+ // ----- Look for all path to remove
+ if ($p_remove_all_dir) {
+ $v_stored_filename = basename($p_filename);
+ }
+ // ----- Look for partial path remove
+ else if ($p_remove_dir != "") {
+ if (substr($p_remove_dir, -1) != '/')
+ $p_remove_dir .= "/";
+
+ if ( (substr($p_filename, 0, 2) == "./")
+ || (substr($p_remove_dir, 0, 2) == "./")) {
+
+ if ( (substr($p_filename, 0, 2) == "./")
+ && (substr($p_remove_dir, 0, 2) != "./")) {
+ $p_remove_dir = "./".$p_remove_dir;
+ }
+ if ( (substr($p_filename, 0, 2) != "./")
+ && (substr($p_remove_dir, 0, 2) == "./")) {
+ $p_remove_dir = substr($p_remove_dir, 2);
+ }
+ }
+
+ $v_compare = PclZipUtilPathInclusion($p_remove_dir,
+ $v_stored_filename);
+ if ($v_compare > 0) {
+ if ($v_compare == 2) {
+ $v_stored_filename = "";
+ }
+ else {
+ $v_stored_filename = substr($v_stored_filename,
+ strlen($p_remove_dir));
+ }
+ }
+ }
+
+ // ----- Remove drive letter if any
+ $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
+
+ // ----- Look for path to add
+ if ($p_add_dir != "") {
+ if (substr($p_add_dir, -1) == "/")
+ $v_stored_filename = $p_add_dir.$v_stored_filename;
+ else
+ $v_stored_filename = $p_add_dir."/".$v_stored_filename;
+ }
+ }
+
+ // ----- Filename (reduce the path of stored name)
+ $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
+ $p_filedescr['stored_filename'] = $v_stored_filename;
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privWriteFileHeader()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privWriteFileHeader(&$p_header)
+ {
+ $v_result=1;
+
+ // ----- Store the offset position of the file
+ $p_header['offset'] = ftell($this->zip_fd);
+
+ // ----- Transform UNIX mtime to DOS format mdate/mtime
+ $v_date = getdate($p_header['mtime']);
+ $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
+ $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
+
+ // ----- Packed data
+ $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
+ $p_header['version_extracted'], $p_header['flag'],
+ $p_header['compression'], $v_mtime, $v_mdate,
+ $p_header['crc'], $p_header['compressed_size'],
+ $p_header['size'],
+ strlen($p_header['stored_filename']),
+ $p_header['extra_len']);
+
+ // ----- Write the first 148 bytes of the header in the archive
+ fputs($this->zip_fd, $v_binary_data, 30);
+
+ // ----- Write the variable fields
+ if (strlen($p_header['stored_filename']) != 0)
+ {
+ fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
+ }
+ if ($p_header['extra_len'] != 0)
+ {
+ fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privWriteCentralFileHeader()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privWriteCentralFileHeader(&$p_header)
+ {
+ $v_result=1;
+
+ // TBC
+ //for(reset($p_header); $key = key($p_header); next($p_header)) {
+ //}
+
+ // ----- Transform UNIX mtime to DOS format mdate/mtime
+ $v_date = getdate($p_header['mtime']);
+ $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
+ $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
+
+
+ // ----- Packed data
+ $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
+ $p_header['version'], $p_header['version_extracted'],
+ $p_header['flag'], $p_header['compression'],
+ $v_mtime, $v_mdate, $p_header['crc'],
+ $p_header['compressed_size'], $p_header['size'],
+ strlen($p_header['stored_filename']),
+ $p_header['extra_len'], $p_header['comment_len'],
+ $p_header['disk'], $p_header['internal'],
+ $p_header['external'], $p_header['offset']);
+
+ // ----- Write the 42 bytes of the header in the zip file
+ fputs($this->zip_fd, $v_binary_data, 46);
+
+ // ----- Write the variable fields
+ if (strlen($p_header['stored_filename']) != 0)
+ {
+ fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
+ }
+ if ($p_header['extra_len'] != 0)
+ {
+ fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
+ }
+ if ($p_header['comment_len'] != 0)
+ {
+ fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privWriteCentralHeader()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
+ {
+ $v_result=1;
+
+ // ----- Packed data
+ $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
+ $p_nb_entries, $p_size,
+ $p_offset, strlen($p_comment));
+
+ // ----- Write the 22 bytes of the header in the zip file
+ fputs($this->zip_fd, $v_binary_data, 22);
+
+ // ----- Write the variable fields
+ if (strlen($p_comment) != 0)
+ {
+ fputs($this->zip_fd, $p_comment, strlen($p_comment));
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privList()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privList(&$p_list)
+ {
+ $v_result=1;
+
+ // ----- Magic quotes trick
+ $this->privDisableMagicQuotes();
+
+ // ----- Open the zip file
+ if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
+ {
+ // ----- Magic quotes trick
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the central directory information
+ $v_central_dir = array();
+ if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
+ {
+ $this->privSwapBackMagicQuotes();
+ return $v_result;
+ }
+
+ // ----- Go to beginning of Central Dir
+ @rewind($this->zip_fd);
+ if (@fseek($this->zip_fd, $v_central_dir['offset']))
+ {
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read each entry
+ for ($i=0; $i<$v_central_dir['entries']; $i++)
+ {
+ // ----- Read the file header
+ if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
+ {
+ $this->privSwapBackMagicQuotes();
+ return $v_result;
+ }
+ $v_header['index'] = $i;
+
+ // ----- Get the only interesting attributes
+ $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
+ unset($v_header);
+ }
+
+ // ----- Close the zip file
+ $this->privCloseFd();
+
+ // ----- Magic quotes trick
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privConvertHeader2FileInfo()
+ // Description :
+ // This function takes the file information from the central directory
+ // entries and extract the interesting parameters that will be given back.
+ // The resulting file infos are set in the array $p_info
+ // $p_info['filename'] : Filename with full path. Given by user (add),
+ // extracted in the filesystem (extract).
+ // $p_info['stored_filename'] : Stored filename in the archive.
+ // $p_info['size'] = Size of the file.
+ // $p_info['compressed_size'] = Compressed size of the file.
+ // $p_info['mtime'] = Last modification date of the file.
+ // $p_info['comment'] = Comment associated with the file.
+ // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
+ // $p_info['status'] = status of the action on the file.
+ // $p_info['crc'] = CRC of the file content.
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privConvertHeader2FileInfo($p_header, &$p_info)
+ {
+ $v_result=1;
+
+ // ----- Get the interesting attributes
+ $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
+ $p_info['filename'] = $v_temp_path;
+ $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
+ $p_info['stored_filename'] = $v_temp_path;
+ $p_info['size'] = $p_header['size'];
+ $p_info['compressed_size'] = $p_header['compressed_size'];
+ $p_info['mtime'] = $p_header['mtime'];
+ $p_info['comment'] = $p_header['comment'];
+ $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
+ $p_info['index'] = $p_header['index'];
+ $p_info['status'] = $p_header['status'];
+ $p_info['crc'] = $p_header['crc'];
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privExtractByRule()
+ // Description :
+ // Extract a file or directory depending of rules (by index, by name, ...)
+ // Parameters :
+ // $p_file_list : An array where will be placed the properties of each
+ // extracted file
+ // $p_path : Path to add while writing the extracted files
+ // $p_remove_path : Path to remove (from the file memorized path) while writing the
+ // extracted files. If the path does not match the file path,
+ // the file is extracted with its memorized path.
+ // $p_remove_path does not apply to 'list' mode.
+ // $p_path and $p_remove_path are commulative.
+ // Return Values :
+ // 1 on success,0 or less on error (see error code list)
+ // --------------------------------------------------------------------------------
+ function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Magic quotes trick
+ $this->privDisableMagicQuotes();
+
+ // ----- Check the path
+ if ( ($p_path == "")
+ || ( (substr($p_path, 0, 1) != "/")
+ && (substr($p_path, 0, 3) != "../")
+ && (substr($p_path,1,2)!=":/")))
+ $p_path = "./".$p_path;
+
+ // ----- Reduce the path last (and duplicated) '/'
+ if (($p_path != "./") && ($p_path != "/"))
+ {
+ // ----- Look for the path end '/'
+ while (substr($p_path, -1) == "/")
+ {
+ $p_path = substr($p_path, 0, strlen($p_path)-1);
+ }
+ }
+
+ // ----- Look for path to remove format (should end by /)
+ if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
+ {
+ $p_remove_path .= '/';
+ }
+ $p_remove_path_size = strlen($p_remove_path);
+
+ // ----- Open the zip file
+ if (($v_result = $this->privOpenFd('rb')) != 1)
+ {
+ $this->privSwapBackMagicQuotes();
+ return $v_result;
+ }
+
+ // ----- Read the central directory information
+ $v_central_dir = array();
+ if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
+ {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+
+ return $v_result;
+ }
+
+ // ----- Start at beginning of Central Dir
+ $v_pos_entry = $v_central_dir['offset'];
+
+ // ----- Read each entry
+ $j_start = 0;
+ for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
+ {
+
+ // ----- Read next Central dir entry
+ @rewind($this->zip_fd);
+ if (@fseek($this->zip_fd, $v_pos_entry))
+ {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the file header
+ $v_header = array();
+ if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
+ {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+
+ return $v_result;
+ }
+
+ // ----- Store the index
+ $v_header['index'] = $i;
+
+ // ----- Store the file position
+ $v_pos_entry = ftell($this->zip_fd);
+
+ // ----- Look for the specific extract rules
+ $v_extract = false;
+
+ // ----- Look for extract by name rule
+ if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
+ && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
+
+ // ----- Look if the filename is in the list
+ for ($j=0; ($j strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
+ && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
+ $v_extract = true;
+ }
+ }
+ // ----- Look for a filename
+ elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
+ $v_extract = true;
+ }
+ }
+ }
+
+ // ----- Look for extract by ereg rule
+ // ereg() is deprecated with PHP 5.3
+ /*
+ else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
+ && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
+
+ if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
+ $v_extract = true;
+ }
+ }
+ */
+
+ // ----- Look for extract by preg rule
+ else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
+ && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
+
+ if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
+ $v_extract = true;
+ }
+ }
+
+ // ----- Look for extract by index rule
+ else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
+ && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
+
+ // ----- Look if the index is in the list
+ for ($j=$j_start; ($j=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
+ $v_extract = true;
+ }
+ if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
+ $j_start = $j+1;
+ }
+
+ if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
+ break;
+ }
+ }
+ }
+
+ // ----- Look for no rule, which means extract all the archive
+ else {
+ $v_extract = true;
+ }
+
+ // ----- Check compression method
+ if ( ($v_extract)
+ && ( ($v_header['compression'] != 8)
+ && ($v_header['compression'] != 0))) {
+ $v_header['status'] = 'unsupported_compression';
+
+ // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
+ if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
+ && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
+
+ $this->privSwapBackMagicQuotes();
+
+ PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
+ "Filename '".$v_header['stored_filename']."' is "
+ ."compressed by an unsupported compression "
+ ."method (".$v_header['compression'].") ");
+
+ return PclZip::errorCode();
+ }
+ }
+
+ // ----- Check encrypted files
+ if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
+ $v_header['status'] = 'unsupported_encryption';
+
+ // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
+ if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
+ && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
+
+ $this->privSwapBackMagicQuotes();
+
+ PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
+ "Unsupported encryption for "
+ ." filename '".$v_header['stored_filename']
+ ."'");
+
+ return PclZip::errorCode();
+ }
+ }
+
+ // ----- Look for real extraction
+ if (($v_extract) && ($v_header['status'] != 'ok')) {
+ $v_result = $this->privConvertHeader2FileInfo($v_header,
+ $p_file_list[$v_nb_extracted++]);
+ if ($v_result != 1) {
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+ return $v_result;
+ }
+
+ $v_extract = false;
+ }
+
+ // ----- Look for real extraction
+ if ($v_extract)
+ {
+
+ // ----- Go to the file position
+ @rewind($this->zip_fd);
+ if (@fseek($this->zip_fd, $v_header['offset']))
+ {
+ // ----- Close the zip file
+ $this->privCloseFd();
+
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Look for extraction as string
+ if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
+
+ $v_string = '';
+
+ // ----- Extracting the file
+ $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
+ if ($v_result1 < 1) {
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+ return $v_result1;
+ }
+
+ // ----- Get the only interesting attributes
+ if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
+ {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+
+ return $v_result;
+ }
+
+ // ----- Set the file content
+ $p_file_list[$v_nb_extracted]['content'] = $v_string;
+
+ // ----- Next extracted file
+ $v_nb_extracted++;
+
+ // ----- Look for user callback abort
+ if ($v_result1 == 2) {
+ break;
+ }
+ }
+ // ----- Look for extraction in standard output
+ elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
+ && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
+ // ----- Extracting the file in standard output
+ $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
+ if ($v_result1 < 1) {
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+ return $v_result1;
+ }
+
+ // ----- Get the only interesting attributes
+ if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+ return $v_result;
+ }
+
+ // ----- Look for user callback abort
+ if ($v_result1 == 2) {
+ break;
+ }
+ }
+ // ----- Look for normal extraction
+ else {
+ // ----- Extracting the file
+ $v_result1 = $this->privExtractFile($v_header,
+ $p_path, $p_remove_path,
+ $p_remove_all_path,
+ $p_options);
+ if ($v_result1 < 1) {
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+ return $v_result1;
+ }
+
+ // ----- Get the only interesting attributes
+ if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
+ {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+
+ return $v_result;
+ }
+
+ // ----- Look for user callback abort
+ if ($v_result1 == 2) {
+ break;
+ }
+ }
+ }
+ }
+
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $this->privSwapBackMagicQuotes();
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privExtractFile()
+ // Description :
+ // Parameters :
+ // Return Values :
+ //
+ // 1 : ... ?
+ // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
+ // --------------------------------------------------------------------------------
+ function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Read the file header
+ if (($v_result = $this->privReadFileHeader($v_header)) != 1)
+ {
+ // ----- Return
+ return $v_result;
+ }
+
+
+ // ----- Check that the file header is coherent with $p_entry info
+ if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
+ // TBC
+ }
+
+ // ----- Look for all path to remove
+ if ($p_remove_all_path == true) {
+ // ----- Look for folder entry that not need to be extracted
+ if (($p_entry['external']&0x00000010)==0x00000010) {
+
+ $p_entry['status'] = "filtered";
+
+ return $v_result;
+ }
+
+ // ----- Get the basename of the path
+ $p_entry['filename'] = basename($p_entry['filename']);
+ }
+
+ // ----- Look for path to remove
+ else if ($p_remove_path != "")
+ {
+ if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
+ {
+
+ // ----- Change the file status
+ $p_entry['status'] = "filtered";
+
+ // ----- Return
+ return $v_result;
+ }
+
+ $p_remove_path_size = strlen($p_remove_path);
+ if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
+ {
+
+ // ----- Remove the path
+ $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
+
+ }
+ }
+
+ // ----- Add the path
+ if ($p_path != '') {
+ $p_entry['filename'] = $p_path."/".$p_entry['filename'];
+ }
+
+ // ----- Check a base_dir_restriction
+ if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
+ $v_inclusion
+ = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
+ $p_entry['filename']);
+ if ($v_inclusion == 0) {
+
+ PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
+ "Filename '".$p_entry['filename']."' is "
+ ."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
+
+ return PclZip::errorCode();
+ }
+ }
+
+ // ----- Look for pre-extract callback
+ if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
+
+ // ----- Generate a local information
+ $v_local_header = array();
+ $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
+
+ // ----- Call the callback
+ // Here I do not use call_user_func() because I need to send a reference to the
+ // header.
+ $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
+ if ($v_result == 0) {
+ // ----- Change the file status
+ $p_entry['status'] = "skipped";
+ $v_result = 1;
+ }
+
+ // ----- Look for abort result
+ if ($v_result == 2) {
+ // ----- This status is internal and will be changed in 'skipped'
+ $p_entry['status'] = "aborted";
+ $v_result = PCLZIP_ERR_USER_ABORTED;
+ }
+
+ // ----- Update the information
+ // Only some fields can be modified
+ $p_entry['filename'] = $v_local_header['filename'];
+ }
+
+
+ // ----- Look if extraction should be done
+ if ($p_entry['status'] == 'ok') {
+
+ // ----- Look for specific actions while the file exist
+ if (file_exists($p_entry['filename']))
+ {
+
+ // ----- Look if file is a directory
+ if (is_dir($p_entry['filename']))
+ {
+
+ // ----- Change the file status
+ $p_entry['status'] = "already_a_directory";
+
+ // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
+ // For historical reason first PclZip implementation does not stop
+ // when this kind of error occurs.
+ if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
+ && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
+
+ PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
+ "Filename '".$p_entry['filename']."' is "
+ ."already used by an existing directory");
+
+ return PclZip::errorCode();
+ }
+ }
+ // ----- Look if file is write protected
+ else if (!is_writeable($p_entry['filename']))
+ {
+
+ // ----- Change the file status
+ $p_entry['status'] = "write_protected";
+
+ // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
+ // For historical reason first PclZip implementation does not stop
+ // when this kind of error occurs.
+ if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
+ && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
+
+ PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
+ "Filename '".$p_entry['filename']."' exists "
+ ."and is write protected");
+
+ return PclZip::errorCode();
+ }
+ }
+
+ // ----- Look if the extracted file is older
+ else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
+ {
+ // ----- Change the file status
+ if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
+ && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
+ }
+ else {
+ $p_entry['status'] = "newer_exist";
+
+ // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
+ // For historical reason first PclZip implementation does not stop
+ // when this kind of error occurs.
+ if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
+ && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
+
+ PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
+ "Newer version of '".$p_entry['filename']."' exists "
+ ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
+
+ return PclZip::errorCode();
+ }
+ }
+ }
+ else {
+ }
+ }
+
+ // ----- Check the directory availability and create it if necessary
+ else {
+ if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
+ $v_dir_to_check = $p_entry['filename'];
+ else if (!strstr($p_entry['filename'], "/"))
+ $v_dir_to_check = "";
+ else
+ $v_dir_to_check = dirname($p_entry['filename']);
+
+ if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
+
+ // ----- Change the file status
+ $p_entry['status'] = "path_creation_fail";
+
+ // ----- Return
+ //return $v_result;
+ $v_result = 1;
+ }
+ }
+ }
+
+ // ----- Look if extraction should be done
+ if ($p_entry['status'] == 'ok') {
+
+ // ----- Do the extraction (if not a folder)
+ if (!(($p_entry['external']&0x00000010)==0x00000010))
+ {
+ // ----- Look for not compressed file
+ if ($p_entry['compression'] == 0) {
+
+ // ----- Opening destination file
+ if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
+ {
+
+ // ----- Change the file status
+ $p_entry['status'] = "write_error";
+
+ // ----- Return
+ return $v_result;
+ }
+
+
+ // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
+ $v_size = $p_entry['compressed_size'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($this->zip_fd, $v_read_size);
+ /* Try to speed up the code
+ $v_binary_data = pack('a'.$v_read_size, $v_buffer);
+ @fwrite($v_dest_file, $v_binary_data, $v_read_size);
+ */
+ @fwrite($v_dest_file, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Closing the destination file
+ fclose($v_dest_file);
+
+ // ----- Change the file mtime
+ touch($p_entry['filename'], $p_entry['mtime']);
+
+
+ }
+ else {
+ // ----- TBC
+ // Need to be finished
+ if (($p_entry['flag'] & 1) == 1) {
+ PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
+ return PclZip::errorCode();
+ }
+
+
+ // ----- Look for using temporary file to unzip
+ if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
+ && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
+ || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
+ && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])) ) ) {
+ $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
+ if ($v_result < PCLZIP_ERR_NO_ERROR) {
+ return $v_result;
+ }
+ }
+
+ // ----- Look for extract in memory
+ else {
+
+
+ // ----- Read the compressed file in a buffer (one shot)
+ if ($p_entry['compressed_size'] > 0) {
+ $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
+ }
+ else {
+ $v_buffer = '';
+ }
+
+ // ----- Decompress the file
+ $v_file_content = @gzinflate($v_buffer);
+ unset($v_buffer);
+ if ($v_file_content === FALSE) {
+
+ // ----- Change the file status
+ // TBC
+ $p_entry['status'] = "error";
+
+ return $v_result;
+ }
+
+ // ----- Opening destination file
+ if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
+
+ // ----- Change the file status
+ $p_entry['status'] = "write_error";
+
+ return $v_result;
+ }
+
+ // ----- Write the uncompressed data
+ @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
+ unset($v_file_content);
+
+ // ----- Closing the destination file
+ @fclose($v_dest_file);
+
+ }
+
+ // ----- Change the file mtime
+ @touch($p_entry['filename'], $p_entry['mtime']);
+ }
+
+ // ----- Look for chmod option
+ if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
+
+ // ----- Change the mode of the file
+ @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
+ }
+
+ }
+ }
+
+ // ----- Change abort status
+ if ($p_entry['status'] == "aborted") {
+ $p_entry['status'] = "skipped";
+ }
+
+ // ----- Look for post-extract callback
+ elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
+
+ // ----- Generate a local information
+ $v_local_header = array();
+ $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
+
+ // ----- Call the callback
+ // Here I do not use call_user_func() because I need to send a reference to the
+ // header.
+ $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
+
+ // ----- Look for abort result
+ if ($v_result == 2) {
+ $v_result = PCLZIP_ERR_USER_ABORTED;
+ }
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privExtractFileUsingTempFile()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privExtractFileUsingTempFile(&$p_entry, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Creates a temporary file
+ $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
+ if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
+ fclose($v_file);
+ PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
+ return PclZip::errorCode();
+ }
+
+
+ // ----- Write gz file format header
+ $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
+ @fwrite($v_dest_file, $v_binary_data, 10);
+
+ // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
+ $v_size = $p_entry['compressed_size'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($this->zip_fd, $v_read_size);
+ //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
+ @fwrite($v_dest_file, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Write gz file format footer
+ $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
+ @fwrite($v_dest_file, $v_binary_data, 8);
+
+ // ----- Close the temporary file
+ @fclose($v_dest_file);
+
+ // ----- Opening destination file
+ if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
+ $p_entry['status'] = "write_error";
+ return $v_result;
+ }
+
+ // ----- Open the temporary gz file
+ if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
+ @fclose($v_dest_file);
+ $p_entry['status'] = "read_error";
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
+ return PclZip::errorCode();
+ }
+
+
+ // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
+ $v_size = $p_entry['size'];
+ while ($v_size != 0) {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @gzread($v_src_file, $v_read_size);
+ //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
+ @fwrite($v_dest_file, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+ @fclose($v_dest_file);
+ @gzclose($v_src_file);
+
+ // ----- Delete the temporary file
+ @unlink($v_gzip_temp_name);
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privExtractFileInOutput()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privExtractFileInOutput(&$p_entry, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Read the file header
+ if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
+ return $v_result;
+ }
+
+
+ // ----- Check that the file header is coherent with $p_entry info
+ if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
+ // TBC
+ }
+
+ // ----- Look for pre-extract callback
+ if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
+
+ // ----- Generate a local information
+ $v_local_header = array();
+ $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
+
+ // ----- Call the callback
+ // Here I do not use call_user_func() because I need to send a reference to the
+ // header.
+// eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
+ $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
+ if ($v_result == 0) {
+ // ----- Change the file status
+ $p_entry['status'] = "skipped";
+ $v_result = 1;
+ }
+
+ // ----- Look for abort result
+ if ($v_result == 2) {
+ // ----- This status is internal and will be changed in 'skipped'
+ $p_entry['status'] = "aborted";
+ $v_result = PCLZIP_ERR_USER_ABORTED;
+ }
+
+ // ----- Update the information
+ // Only some fields can be modified
+ $p_entry['filename'] = $v_local_header['filename'];
+ }
+
+ // ----- Trace
+
+ // ----- Look if extraction should be done
+ if ($p_entry['status'] == 'ok') {
+
+ // ----- Do the extraction (if not a folder)
+ if (!(($p_entry['external']&0x00000010)==0x00000010)) {
+ // ----- Look for not compressed file
+ if ($p_entry['compressed_size'] == $p_entry['size']) {
+
+ // ----- Read the file in a buffer (one shot)
+ if ($p_entry['compressed_size'] > 0) {
+ $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
+ }
+ else {
+ $v_buffer = '';
+ }
+
+ // ----- Send the file to the output
+ echo $v_buffer;
+ unset($v_buffer);
+ }
+ else {
+
+ // ----- Read the compressed file in a buffer (one shot)
+ if ($p_entry['compressed_size'] > 0) {
+ $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
+ }
+ else {
+ $v_buffer = '';
+ }
+
+ // ----- Decompress the file
+ $v_file_content = gzinflate($v_buffer);
+ unset($v_buffer);
+
+ // ----- Send the file to the output
+ echo $v_file_content;
+ unset($v_file_content);
+ }
+ }
+ }
+
+ // ----- Change abort status
+ if ($p_entry['status'] == "aborted") {
+ $p_entry['status'] = "skipped";
+ }
+
+ // ----- Look for post-extract callback
+ elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
+
+ // ----- Generate a local information
+ $v_local_header = array();
+ $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
+
+ // ----- Call the callback
+ // Here I do not use call_user_func() because I need to send a reference to the
+ // header.
+ $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
+
+ // ----- Look for abort result
+ if ($v_result == 2) {
+ $v_result = PCLZIP_ERR_USER_ABORTED;
+ }
+ }
+
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privExtractFileAsString()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
+ {
+ $v_result=1;
+
+ // ----- Read the file header
+ $v_header = array();
+ if (($v_result = $this->privReadFileHeader($v_header)) != 1)
+ {
+ // ----- Return
+ return $v_result;
+ }
+
+
+ // ----- Check that the file header is coherent with $p_entry info
+ if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
+ // TBC
+ }
+
+ // ----- Look for pre-extract callback
+ if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
+
+ // ----- Generate a local information
+ $v_local_header = array();
+ $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
+
+ // ----- Call the callback
+ // Here I do not use call_user_func() because I need to send a reference to the
+ // header.
+ $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
+ if ($v_result == 0) {
+ // ----- Change the file status
+ $p_entry['status'] = "skipped";
+ $v_result = 1;
+ }
+
+ // ----- Look for abort result
+ if ($v_result == 2) {
+ // ----- This status is internal and will be changed in 'skipped'
+ $p_entry['status'] = "aborted";
+ $v_result = PCLZIP_ERR_USER_ABORTED;
+ }
+
+ // ----- Update the information
+ // Only some fields can be modified
+ $p_entry['filename'] = $v_local_header['filename'];
+ }
+
+
+ // ----- Look if extraction should be done
+ if ($p_entry['status'] == 'ok') {
+
+ // ----- Do the extraction (if not a folder)
+ if (!(($p_entry['external']&0x00000010)==0x00000010)) {
+ // ----- Look for not compressed file
+ // if ($p_entry['compressed_size'] == $p_entry['size'])
+ if ($p_entry['compression'] == 0) {
+
+ // ----- Reading the file
+ if ($p_entry['compressed_size'] > 0) {
+ $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
+ }
+ else {
+ $p_string = '';
+ }
+ }
+ else {
+
+ // ----- Reading the file
+ if ($p_entry['compressed_size'] > 0) {
+ $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
+ }
+ else {
+ $v_data = '';
+ }
+
+ // ----- Decompress the file
+ if (($p_string = @gzinflate($v_data)) === FALSE) {
+ // TBC
+ }
+ }
+
+ // ----- Trace
+ }
+ else {
+ // TBC : error : can not extract a folder in a string
+ }
+
+ }
+
+ // ----- Change abort status
+ if ($p_entry['status'] == "aborted") {
+ $p_entry['status'] = "skipped";
+ }
+
+ // ----- Look for post-extract callback
+ elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
+
+ // ----- Generate a local information
+ $v_local_header = array();
+ $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
+
+ // ----- Swap the content to header
+ $v_local_header['content'] = $p_string;
+ $p_string = '';
+
+ // ----- Call the callback
+ // Here I do not use call_user_func() because I need to send a reference to the
+ // header.
+ $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
+
+ // ----- Swap back the content to header
+ $p_string = $v_local_header['content'];
+ unset($v_local_header['content']);
+
+ // ----- Look for abort result
+ if ($v_result == 2) {
+ $v_result = PCLZIP_ERR_USER_ABORTED;
+ }
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privReadFileHeader()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privReadFileHeader(&$p_header)
+ {
+ $v_result=1;
+
+ // ----- Read the 4 bytes signature
+ $v_binary_data = @fread($this->zip_fd, 4);
+ $v_data = unpack('Vid', $v_binary_data);
+
+ // ----- Check signature
+ if ($v_data['id'] != 0x04034b50)
+ {
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the first 42 bytes of the header
+ $v_binary_data = fread($this->zip_fd, 26);
+
+ // ----- Look for invalid block size
+ if (strlen($v_binary_data) != 26)
+ {
+ $p_header['filename'] = "";
+ $p_header['status'] = "invalid_header";
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Extract the values
+ $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
+
+ // ----- Get filename
+ $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
+
+ // ----- Get extra_fields
+ if ($v_data['extra_len'] != 0) {
+ $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
+ }
+ else {
+ $p_header['extra'] = '';
+ }
+
+ // ----- Extract properties
+ $p_header['version_extracted'] = $v_data['version'];
+ $p_header['compression'] = $v_data['compression'];
+ $p_header['size'] = $v_data['size'];
+ $p_header['compressed_size'] = $v_data['compressed_size'];
+ $p_header['crc'] = $v_data['crc'];
+ $p_header['flag'] = $v_data['flag'];
+ $p_header['filename_len'] = $v_data['filename_len'];
+
+ // ----- Recuperate date in UNIX format
+ $p_header['mdate'] = $v_data['mdate'];
+ $p_header['mtime'] = $v_data['mtime'];
+ if ($p_header['mdate'] && $p_header['mtime'])
+ {
+ // ----- Extract time
+ $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
+ $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
+ $v_seconde = ($p_header['mtime'] & 0x001F)*2;
+
+ // ----- Extract date
+ $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
+ $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
+ $v_day = $p_header['mdate'] & 0x001F;
+
+ // ----- Get UNIX date format
+ $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
+
+ }
+ else
+ {
+ $p_header['mtime'] = time();
+ }
+
+ // TBC
+ //for(reset($v_data); $key = key($v_data); next($v_data)) {
+ //}
+
+ // ----- Set the stored filename
+ $p_header['stored_filename'] = $p_header['filename'];
+
+ // ----- Set the status field
+ $p_header['status'] = "ok";
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privReadCentralFileHeader()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privReadCentralFileHeader(&$p_header)
+ {
+ $v_result=1;
+
+ // ----- Read the 4 bytes signature
+ $v_binary_data = @fread($this->zip_fd, 4);
+ $v_data = unpack('Vid', $v_binary_data);
+
+ // ----- Check signature
+ if ($v_data['id'] != 0x02014b50)
+ {
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the first 42 bytes of the header
+ $v_binary_data = fread($this->zip_fd, 42);
+
+ // ----- Look for invalid block size
+ if (strlen($v_binary_data) != 42)
+ {
+ $p_header['filename'] = "";
+ $p_header['status'] = "invalid_header";
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Extract the values
+ $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
+
+ // ----- Get filename
+ if ($p_header['filename_len'] != 0)
+ $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
+ else
+ $p_header['filename'] = '';
+
+ // ----- Get extra
+ if ($p_header['extra_len'] != 0)
+ $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
+ else
+ $p_header['extra'] = '';
+
+ // ----- Get comment
+ if ($p_header['comment_len'] != 0)
+ $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
+ else
+ $p_header['comment'] = '';
+
+ // ----- Extract properties
+
+ // ----- Recuperate date in UNIX format
+ //if ($p_header['mdate'] && $p_header['mtime'])
+ // TBC : bug : this was ignoring time with 0/0/0
+ if (1)
+ {
+ // ----- Extract time
+ $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
+ $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
+ $v_seconde = ($p_header['mtime'] & 0x001F)*2;
+
+ // ----- Extract date
+ $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
+ $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
+ $v_day = $p_header['mdate'] & 0x001F;
+
+ // ----- Get UNIX date format
+ $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
+
+ }
+ else
+ {
+ $p_header['mtime'] = time();
+ }
+
+ // ----- Set the stored filename
+ $p_header['stored_filename'] = $p_header['filename'];
+
+ // ----- Set default status to ok
+ $p_header['status'] = 'ok';
+
+ // ----- Look if it is a directory
+ if (substr($p_header['filename'], -1) == '/') {
+ //$p_header['external'] = 0x41FF0010;
+ $p_header['external'] = 0x00000010;
+ }
+
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privCheckFileHeaders()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // 1 on success,
+ // 0 on error;
+ // --------------------------------------------------------------------------------
+ function privCheckFileHeaders(&$p_local_header, &$p_central_header)
+ {
+ $v_result=1;
+
+ // ----- Check the static values
+ // TBC
+ if ($p_local_header['filename'] != $p_central_header['filename']) {
+ }
+ if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
+ }
+ if ($p_local_header['flag'] != $p_central_header['flag']) {
+ }
+ if ($p_local_header['compression'] != $p_central_header['compression']) {
+ }
+ if ($p_local_header['mtime'] != $p_central_header['mtime']) {
+ }
+ if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
+ }
+
+ // ----- Look for flag bit 3
+ if (($p_local_header['flag'] & 8) == 8) {
+ $p_local_header['size'] = $p_central_header['size'];
+ $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
+ $p_local_header['crc'] = $p_central_header['crc'];
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privReadEndCentralDir()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privReadEndCentralDir(&$p_central_dir)
+ {
+ $v_result=1;
+
+ // ----- Go to the end of the zip file
+ $v_size = filesize($this->zipname);
+ @fseek($this->zip_fd, $v_size);
+ if (@ftell($this->zip_fd) != $v_size)
+ {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- First try : look if this is an archive with no commentaries (most of the time)
+ // in this case the end of central dir is at 22 bytes of the file end
+ $v_found = 0;
+ if ($v_size > 26) {
+ @fseek($this->zip_fd, $v_size-22);
+ if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
+ {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read for bytes
+ $v_binary_data = @fread($this->zip_fd, 4);
+ $v_data = @unpack('Vid', $v_binary_data);
+
+ // ----- Check signature
+ if ($v_data['id'] == 0x06054b50) {
+ $v_found = 1;
+ }
+
+ $v_pos = ftell($this->zip_fd);
+ }
+
+ // ----- Go back to the maximum possible size of the Central Dir End Record
+ if (!$v_found) {
+ $v_maximum_size = 65557; // 0xFFFF + 22;
+ if ($v_maximum_size > $v_size)
+ $v_maximum_size = $v_size;
+ @fseek($this->zip_fd, $v_size-$v_maximum_size);
+ if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
+ {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read byte per byte in order to find the signature
+ $v_pos = ftell($this->zip_fd);
+ $v_bytes = 0x00000000;
+ while ($v_pos < $v_size)
+ {
+ // ----- Read a byte
+ $v_byte = @fread($this->zip_fd, 1);
+
+ // ----- Add the byte
+ //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
+ // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
+ // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
+ $v_bytes = ( ($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
+
+ // ----- Compare the bytes
+ if ($v_bytes == 0x504b0506)
+ {
+ $v_pos++;
+ break;
+ }
+
+ $v_pos++;
+ }
+
+ // ----- Look if not found end of central dir
+ if ($v_pos == $v_size)
+ {
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+ }
+
+ // ----- Read the first 18 bytes of the header
+ $v_binary_data = fread($this->zip_fd, 18);
+
+ // ----- Look for invalid block size
+ if (strlen($v_binary_data) != 18)
+ {
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Extract the values
+ $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
+
+ // ----- Check the global size
+ if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
+
+ // ----- Removed in release 2.2 see readme file
+ // The check of the file size is a little too strict.
+ // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
+ // While decrypted, zip has training 0 bytes
+ if (0) {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
+ 'The central dir is not at the end of the archive.'
+ .' Some trailing bytes exists after the archive.');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+ }
+
+ // ----- Get comment
+ if ($v_data['comment_size'] != 0) {
+ $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
+ }
+ else
+ $p_central_dir['comment'] = '';
+
+ $p_central_dir['entries'] = $v_data['entries'];
+ $p_central_dir['disk_entries'] = $v_data['disk_entries'];
+ $p_central_dir['offset'] = $v_data['offset'];
+ $p_central_dir['size'] = $v_data['size'];
+ $p_central_dir['disk'] = $v_data['disk'];
+ $p_central_dir['disk_start'] = $v_data['disk_start'];
+
+ // TBC
+ //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
+ //}
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privDeleteByRule()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privDeleteByRule(&$p_result_list, &$p_options)
+ {
+ $v_result=1;
+ $v_list_detail = array();
+
+ // ----- Open the zip file
+ if (($v_result=$this->privOpenFd('rb')) != 1)
+ {
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Read the central directory information
+ $v_central_dir = array();
+ if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
+ {
+ $this->privCloseFd();
+ return $v_result;
+ }
+
+ // ----- Go to beginning of File
+ @rewind($this->zip_fd);
+
+ // ----- Scan all the files
+ // ----- Start at beginning of Central Dir
+ $v_pos_entry = $v_central_dir['offset'];
+ @rewind($this->zip_fd);
+ if (@fseek($this->zip_fd, $v_pos_entry))
+ {
+ // ----- Close the zip file
+ $this->privCloseFd();
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read each entry
+ $v_header_list = array();
+ $j_start = 0;
+ for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
+ {
+
+ // ----- Read the file header
+ $v_header_list[$v_nb_extracted] = array();
+ if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
+ {
+ // ----- Close the zip file
+ $this->privCloseFd();
+
+ return $v_result;
+ }
+
+
+ // ----- Store the index
+ $v_header_list[$v_nb_extracted]['index'] = $i;
+
+ // ----- Look for the specific extract rules
+ $v_found = false;
+
+ // ----- Look for extract by name rule
+ if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
+ && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
+
+ // ----- Look if the filename is in the list
+ for ($j=0; ($j strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
+ && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
+ $v_found = true;
+ }
+ elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
+ && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
+ $v_found = true;
+ }
+ }
+ // ----- Look for a filename
+ elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
+ $v_found = true;
+ }
+ }
+ }
+
+ // ----- Look for extract by ereg rule
+ // ereg() is deprecated with PHP 5.3
+ /*
+ else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
+ && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
+
+ if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
+ $v_found = true;
+ }
+ }
+ */
+
+ // ----- Look for extract by preg rule
+ else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
+ && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
+
+ if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
+ $v_found = true;
+ }
+ }
+
+ // ----- Look for extract by index rule
+ else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
+ && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
+
+ // ----- Look if the index is in the list
+ for ($j=$j_start; ($j=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
+ $v_found = true;
+ }
+ if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
+ $j_start = $j+1;
+ }
+
+ if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
+ break;
+ }
+ }
+ }
+ else {
+ $v_found = true;
+ }
+
+ // ----- Look for deletion
+ if ($v_found)
+ {
+ unset($v_header_list[$v_nb_extracted]);
+ }
+ else
+ {
+ $v_nb_extracted++;
+ }
+ }
+
+ // ----- Look if something need to be deleted
+ if ($v_nb_extracted > 0) {
+
+ // ----- Creates a temporary file
+ $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
+
+ // ----- Creates a temporary zip archive
+ $v_temp_zip = new PclZip($v_zip_temp_name);
+
+ // ----- Open the temporary zip file in write mode
+ if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
+ $this->privCloseFd();
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Look which file need to be kept
+ for ($i=0; $izip_fd);
+ if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $v_temp_zip->privCloseFd();
+ @unlink($v_zip_temp_name);
+
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Read the file header
+ $v_local_header = array();
+ if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $v_temp_zip->privCloseFd();
+ @unlink($v_zip_temp_name);
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Check that local file header is same as central file header
+ if ($this->privCheckFileHeaders($v_local_header,
+ $v_header_list[$i]) != 1) {
+ // TBC
+ }
+ unset($v_local_header);
+
+ // ----- Write the file header
+ if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $v_temp_zip->privCloseFd();
+ @unlink($v_zip_temp_name);
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Read/write the data block
+ if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
+ // ----- Close the zip file
+ $this->privCloseFd();
+ $v_temp_zip->privCloseFd();
+ @unlink($v_zip_temp_name);
+
+ // ----- Return
+ return $v_result;
+ }
+ }
+
+ // ----- Store the offset of the central dir
+ $v_offset = @ftell($v_temp_zip->zip_fd);
+
+ // ----- Re-Create the Central Dir files header
+ for ($i=0; $iprivWriteCentralFileHeader($v_header_list[$i])) != 1) {
+ $v_temp_zip->privCloseFd();
+ $this->privCloseFd();
+ @unlink($v_zip_temp_name);
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Transform the header to a 'usable' info
+ $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
+ }
+
+
+ // ----- Zip file comment
+ $v_comment = '';
+ if (isset($p_options[PCLZIP_OPT_COMMENT])) {
+ $v_comment = $p_options[PCLZIP_OPT_COMMENT];
+ }
+
+ // ----- Calculate the size of the central header
+ $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
+
+ // ----- Create the central dir footer
+ if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
+ // ----- Reset the file list
+ unset($v_header_list);
+ $v_temp_zip->privCloseFd();
+ $this->privCloseFd();
+ @unlink($v_zip_temp_name);
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Close
+ $v_temp_zip->privCloseFd();
+ $this->privCloseFd();
+
+ // ----- Delete the zip file
+ // TBC : I should test the result ...
+ @unlink($this->zipname);
+
+ // ----- Rename the temporary file
+ // TBC : I should test the result ...
+ //@rename($v_zip_temp_name, $this->zipname);
+ PclZipUtilRename($v_zip_temp_name, $this->zipname);
+
+ // ----- Destroy the temporary archive
+ unset($v_temp_zip);
+ }
+
+ // ----- Remove every files : reset the file
+ else if ($v_central_dir['entries'] != 0) {
+ $this->privCloseFd();
+
+ if (($v_result = $this->privOpenFd('wb')) != 1) {
+ return $v_result;
+ }
+
+ if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
+ return $v_result;
+ }
+
+ $this->privCloseFd();
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privDirCheck()
+ // Description :
+ // Check if a directory exists, if not it creates it and all the parents directory
+ // which may be useful.
+ // Parameters :
+ // $p_dir : Directory path to check.
+ // Return Values :
+ // 1 : OK
+ // -1 : Unable to create directory
+ // --------------------------------------------------------------------------------
+ function privDirCheck($p_dir, $p_is_dir=false)
+ {
+ $v_result = 1;
+
+
+ // ----- Remove the final '/'
+ if (($p_is_dir) && (substr($p_dir, -1)=='/'))
+ {
+ $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
+ }
+
+ // ----- Check the directory availability
+ if ((is_dir($p_dir)) || ($p_dir == ""))
+ {
+ return 1;
+ }
+
+ // ----- Extract parent directory
+ $p_parent_dir = dirname($p_dir);
+
+ // ----- Just a check
+ if ($p_parent_dir != $p_dir)
+ {
+ // ----- Look for parent directory
+ if ($p_parent_dir != "")
+ {
+ if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
+ {
+ return $v_result;
+ }
+ }
+ }
+
+ // ----- Create the directory
+ if (!@mkdir($p_dir, 0777))
+ {
+ // ----- Error log
+ PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privMerge()
+ // Description :
+ // If $p_archive_to_add does not exist, the function exit with a success result.
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privMerge(&$p_archive_to_add)
+ {
+ $v_result=1;
+
+ // ----- Look if the archive_to_add exists
+ if (!is_file($p_archive_to_add->zipname))
+ {
+
+ // ----- Nothing to merge, so merge is a success
+ $v_result = 1;
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Look if the archive exists
+ if (!is_file($this->zipname))
+ {
+
+ // ----- Do a duplicate
+ $v_result = $this->privDuplicate($p_archive_to_add->zipname);
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Open the zip file
+ if (($v_result=$this->privOpenFd('rb')) != 1)
+ {
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Read the central directory information
+ $v_central_dir = array();
+ if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
+ {
+ $this->privCloseFd();
+ return $v_result;
+ }
+
+ // ----- Go to beginning of File
+ @rewind($this->zip_fd);
+
+ // ----- Open the archive_to_add file
+ if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
+ {
+ $this->privCloseFd();
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Read the central directory information
+ $v_central_dir_to_add = array();
+ if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
+ {
+ $this->privCloseFd();
+ $p_archive_to_add->privCloseFd();
+
+ return $v_result;
+ }
+
+ // ----- Go to beginning of File
+ @rewind($p_archive_to_add->zip_fd);
+
+ // ----- Creates a temporary file
+ $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
+
+ // ----- Open the temporary file in write mode
+ if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
+ {
+ $this->privCloseFd();
+ $p_archive_to_add->privCloseFd();
+
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Copy the files from the archive to the temporary file
+ // TBC : Here I should better append the file and go back to erase the central dir
+ $v_size = $v_central_dir['offset'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = fread($this->zip_fd, $v_read_size);
+ @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Copy the files from the archive_to_add into the temporary file
+ $v_size = $v_central_dir_to_add['offset'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
+ @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Store the offset of the central dir
+ $v_offset = @ftell($v_zip_temp_fd);
+
+ // ----- Copy the block of file headers from the old archive
+ $v_size = $v_central_dir['size'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($this->zip_fd, $v_read_size);
+ @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Copy the block of file headers from the archive_to_add
+ $v_size = $v_central_dir_to_add['size'];
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
+ @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Merge the file comments
+ $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
+
+ // ----- Calculate the size of the (new) central header
+ $v_size = @ftell($v_zip_temp_fd)-$v_offset;
+
+ // ----- Swap the file descriptor
+ // Here is a trick : I swap the temporary fd with the zip fd, in order to use
+ // the following methods on the temporary fil and not the real archive fd
+ $v_swap = $this->zip_fd;
+ $this->zip_fd = $v_zip_temp_fd;
+ $v_zip_temp_fd = $v_swap;
+
+ // ----- Create the central dir footer
+ if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
+ {
+ $this->privCloseFd();
+ $p_archive_to_add->privCloseFd();
+ @fclose($v_zip_temp_fd);
+ $this->zip_fd = null;
+
+ // ----- Reset the file list
+ unset($v_header_list);
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Swap back the file descriptor
+ $v_swap = $this->zip_fd;
+ $this->zip_fd = $v_zip_temp_fd;
+ $v_zip_temp_fd = $v_swap;
+
+ // ----- Close
+ $this->privCloseFd();
+ $p_archive_to_add->privCloseFd();
+
+ // ----- Close the temporary file
+ @fclose($v_zip_temp_fd);
+
+ // ----- Delete the zip file
+ // TBC : I should test the result ...
+ @unlink($this->zipname);
+
+ // ----- Rename the temporary file
+ // TBC : I should test the result ...
+ //@rename($v_zip_temp_name, $this->zipname);
+ PclZipUtilRename($v_zip_temp_name, $this->zipname);
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privDuplicate()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privDuplicate($p_archive_filename)
+ {
+ $v_result=1;
+
+ // ----- Look if the $p_archive_filename exists
+ if (!is_file($p_archive_filename))
+ {
+
+ // ----- Nothing to duplicate, so duplicate is a success.
+ $v_result = 1;
+
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Open the zip file
+ if (($v_result=$this->privOpenFd('wb')) != 1)
+ {
+ // ----- Return
+ return $v_result;
+ }
+
+ // ----- Open the temporary file in write mode
+ if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
+ {
+ $this->privCloseFd();
+
+ PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
+
+ // ----- Return
+ return PclZip::errorCode();
+ }
+
+ // ----- Copy the files from the archive to the temporary file
+ // TBC : Here I should better append the file and go back to erase the central dir
+ $v_size = filesize($p_archive_filename);
+ while ($v_size != 0)
+ {
+ $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = fread($v_zip_temp_fd, $v_read_size);
+ @fwrite($this->zip_fd, $v_buffer, $v_read_size);
+ $v_size -= $v_read_size;
+ }
+
+ // ----- Close
+ $this->privCloseFd();
+
+ // ----- Close the temporary file
+ @fclose($v_zip_temp_fd);
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privErrorLog()
+ // Description :
+ // Parameters :
+ // --------------------------------------------------------------------------------
+ function privErrorLog($p_error_code=0, $p_error_string='')
+ {
+ if (PCLZIP_ERROR_EXTERNAL == 1) {
+ PclError($p_error_code, $p_error_string);
+ }
+ else {
+ $this->error_code = $p_error_code;
+ $this->error_string = $p_error_string;
+ }
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privErrorReset()
+ // Description :
+ // Parameters :
+ // --------------------------------------------------------------------------------
+ function privErrorReset()
+ {
+ if (PCLZIP_ERROR_EXTERNAL == 1) {
+ PclErrorReset();
+ }
+ else {
+ $this->error_code = 0;
+ $this->error_string = '';
+ }
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privDisableMagicQuotes()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privDisableMagicQuotes()
+ {
+ $v_result=1;
+
+ // EDIT for WordPress 5.3.0
+ // magic_quote functions are deprecated in PHP 7.4, now assuming it's always off.
+ /*
+
+ // ----- Look if function exists
+ if ( (!function_exists("get_magic_quotes_runtime"))
+ || (!function_exists("set_magic_quotes_runtime"))) {
+ return $v_result;
+ }
+
+ // ----- Look if already done
+ if ($this->magic_quotes_status != -1) {
+ return $v_result;
+ }
+
+ // ----- Get and memorize the magic_quote value
+ $this->magic_quotes_status = @get_magic_quotes_runtime();
+
+ // ----- Disable magic_quotes
+ if ($this->magic_quotes_status == 1) {
+ @set_magic_quotes_runtime(0);
+ }
+ */
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : privSwapBackMagicQuotes()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function privSwapBackMagicQuotes()
+ {
+ $v_result=1;
+
+ // EDIT for WordPress 5.3.0
+ // magic_quote functions are deprecated in PHP 7.4, now assuming it's always off.
+ /*
+
+ // ----- Look if function exists
+ if ( (!function_exists("get_magic_quotes_runtime"))
+ || (!function_exists("set_magic_quotes_runtime"))) {
+ return $v_result;
+ }
+
+ // ----- Look if something to do
+ if ($this->magic_quotes_status != -1) {
+ return $v_result;
+ }
+
+ // ----- Swap back magic_quotes
+ if ($this->magic_quotes_status == 1) {
+ @set_magic_quotes_runtime($this->magic_quotes_status);
+ }
+
+ */
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ }
+ // End of class
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : PclZipUtilPathReduction()
+ // Description :
+ // Parameters :
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function PclZipUtilPathReduction($p_dir)
+ {
+ $v_result = "";
+
+ // ----- Look for not empty path
+ if ($p_dir != "") {
+ // ----- Explode path by directory names
+ $v_list = explode("/", $p_dir);
+
+ // ----- Study directories from last to first
+ $v_skip = 0;
+ for ($i=sizeof($v_list)-1; $i>=0; $i--) {
+ // ----- Look for current path
+ if ($v_list[$i] == ".") {
+ // ----- Ignore this directory
+ // Should be the first $i=0, but no check is done
+ }
+ else if ($v_list[$i] == "..") {
+ $v_skip++;
+ }
+ else if ($v_list[$i] == "") {
+ // ----- First '/' i.e. root slash
+ if ($i == 0) {
+ $v_result = "/".$v_result;
+ if ($v_skip > 0) {
+ // ----- It is an invalid path, so the path is not modified
+ // TBC
+ $v_result = $p_dir;
+ $v_skip = 0;
+ }
+ }
+ // ----- Last '/' i.e. indicates a directory
+ else if ($i == (sizeof($v_list)-1)) {
+ $v_result = $v_list[$i];
+ }
+ // ----- Double '/' inside the path
+ else {
+ // ----- Ignore only the double '//' in path,
+ // but not the first and last '/'
+ }
+ }
+ else {
+ // ----- Look for item to skip
+ if ($v_skip > 0) {
+ $v_skip--;
+ }
+ else {
+ $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
+ }
+ }
+ }
+
+ // ----- Look for skip
+ if ($v_skip > 0) {
+ while ($v_skip > 0) {
+ $v_result = '../'.$v_result;
+ $v_skip--;
+ }
+ }
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : PclZipUtilPathInclusion()
+ // Description :
+ // This function indicates if the path $p_path is under the $p_dir tree. Or,
+ // said in an other way, if the file or sub-dir $p_path is inside the dir
+ // $p_dir.
+ // The function indicates also if the path is exactly the same as the dir.
+ // This function supports path with duplicated '/' like '//', but does not
+ // support '.' or '..' statements.
+ // Parameters :
+ // Return Values :
+ // 0 if $p_path is not inside directory $p_dir
+ // 1 if $p_path is inside directory $p_dir
+ // 2 if $p_path is exactly the same as $p_dir
+ // --------------------------------------------------------------------------------
+ function PclZipUtilPathInclusion($p_dir, $p_path)
+ {
+ $v_result = 1;
+
+ // ----- Look for path beginning by ./
+ if ( ($p_dir == '.')
+ || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
+ $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
+ }
+ if ( ($p_path == '.')
+ || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
+ $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
+ }
+
+ // ----- Explode dir and path by directory separator
+ $v_list_dir = explode("/", $p_dir);
+ $v_list_dir_size = sizeof($v_list_dir);
+ $v_list_path = explode("/", $p_path);
+ $v_list_path_size = sizeof($v_list_path);
+
+ // ----- Study directories paths
+ $i = 0;
+ $j = 0;
+ while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
+
+ // ----- Look for empty dir (path reduction)
+ if ($v_list_dir[$i] == '') {
+ $i++;
+ continue;
+ }
+ if ($v_list_path[$j] == '') {
+ $j++;
+ continue;
+ }
+
+ // ----- Compare the items
+ if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
+ $v_result = 0;
+ }
+
+ // ----- Next items
+ $i++;
+ $j++;
+ }
+
+ // ----- Look if everything seems to be the same
+ if ($v_result) {
+ // ----- Skip all the empty items
+ while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
+ while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
+
+ if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
+ // ----- There are exactly the same
+ $v_result = 2;
+ }
+ else if ($i < $v_list_dir_size) {
+ // ----- The path is shorter than the dir
+ $v_result = 0;
+ }
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : PclZipUtilCopyBlock()
+ // Description :
+ // Parameters :
+ // $p_mode : read/write compression mode
+ // 0 : src & dest normal
+ // 1 : src gzip, dest normal
+ // 2 : src normal, dest gzip
+ // 3 : src & dest gzip
+ // Return Values :
+ // --------------------------------------------------------------------------------
+ function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
+ {
+ $v_result = 1;
+
+ if ($p_mode==0)
+ {
+ while ($p_size != 0)
+ {
+ $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($p_src, $v_read_size);
+ @fwrite($p_dest, $v_buffer, $v_read_size);
+ $p_size -= $v_read_size;
+ }
+ }
+ else if ($p_mode==1)
+ {
+ while ($p_size != 0)
+ {
+ $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @gzread($p_src, $v_read_size);
+ @fwrite($p_dest, $v_buffer, $v_read_size);
+ $p_size -= $v_read_size;
+ }
+ }
+ else if ($p_mode==2)
+ {
+ while ($p_size != 0)
+ {
+ $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @fread($p_src, $v_read_size);
+ @gzwrite($p_dest, $v_buffer, $v_read_size);
+ $p_size -= $v_read_size;
+ }
+ }
+ else if ($p_mode==3)
+ {
+ while ($p_size != 0)
+ {
+ $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
+ $v_buffer = @gzread($p_src, $v_read_size);
+ @gzwrite($p_dest, $v_buffer, $v_read_size);
+ $p_size -= $v_read_size;
+ }
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : PclZipUtilRename()
+ // Description :
+ // This function tries to do a simple rename() function. If it fails, it
+ // tries to copy the $p_src file in a new $p_dest file and then unlink the
+ // first one.
+ // Parameters :
+ // $p_src : Old filename
+ // $p_dest : New filename
+ // Return Values :
+ // 1 on success, 0 on failure.
+ // --------------------------------------------------------------------------------
+ function PclZipUtilRename($p_src, $p_dest)
+ {
+ $v_result = 1;
+
+ // ----- Try to rename the files
+ if (!@rename($p_src, $p_dest)) {
+
+ // ----- Try to copy & unlink the src
+ if (!@copy($p_src, $p_dest)) {
+ $v_result = 0;
+ }
+ else if (!@unlink($p_src)) {
+ $v_result = 0;
+ }
+ }
+
+ // ----- Return
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : PclZipUtilOptionText()
+ // Description :
+ // Translate option value in text. Mainly for debug purpose.
+ // Parameters :
+ // $p_option : the option value.
+ // Return Values :
+ // The option text value.
+ // --------------------------------------------------------------------------------
+ function PclZipUtilOptionText($p_option)
+ {
+
+ $v_list = get_defined_constants();
+ for (reset($v_list); $v_key = key($v_list); next($v_list)) {
+ $v_prefix = substr($v_key, 0, 10);
+ if (( ($v_prefix == 'PCLZIP_OPT')
+ || ($v_prefix == 'PCLZIP_CB_')
+ || ($v_prefix == 'PCLZIP_ATT'))
+ && ($v_list[$v_key] == $p_option)) {
+ return $v_key;
+ }
+ }
+
+ $v_result = 'Unknown';
+
+ return $v_result;
+ }
+ // --------------------------------------------------------------------------------
+
+ // --------------------------------------------------------------------------------
+ // Function : PclZipUtilTranslateWinPath()
+ // Description :
+ // Translate windows path by replacing '\' by '/' and optionally removing
+ // drive letter.
+ // Parameters :
+ // $p_path : path to translate.
+ // $p_remove_disk_letter : true | false
+ // Return Values :
+ // The path translated.
+ // --------------------------------------------------------------------------------
+ function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
+ {
+ if (stristr(php_uname(), 'windows')) {
+ // ----- Look for potential disk letter
+ if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
+ $p_path = substr($p_path, $v_position+1);
+ }
+ // ----- Change potential windows directory separator
+ if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
+ $p_path = strtr($p_path, '\\', '/');
+ }
+ }
+ return $p_path;
+ }
+ // --------------------------------------------------------------------------------
+
+
+?>
diff --git a/wp-admin/includes/class-plugin-installer-skin.php b/wp-admin/includes/class-plugin-installer-skin.php
new file mode 100644
index 0000000..ed165ed
--- /dev/null
+++ b/wp-admin/includes/class-plugin-installer-skin.php
@@ -0,0 +1,349 @@
+ 'web',
+ 'url' => '',
+ 'plugin' => '',
+ 'nonce' => '',
+ 'title' => '',
+ 'overwrite' => '',
+ );
+ $args = wp_parse_args( $args, $defaults );
+
+ $this->type = $args['type'];
+ $this->url = $args['url'];
+ $this->api = isset( $args['api'] ) ? $args['api'] : array();
+ $this->overwrite = $args['overwrite'];
+
+ parent::__construct( $args );
+ }
+
+ /**
+ * Performs an action before installing a plugin.
+ *
+ * @since 2.8.0
+ */
+ public function before() {
+ if ( ! empty( $this->api ) ) {
+ $this->upgrader->strings['process_success'] = sprintf(
+ $this->upgrader->strings['process_success_specific'],
+ $this->api->name,
+ $this->api->version
+ );
+ }
+ }
+
+ /**
+ * Hides the `process_failed` error when updating a plugin by uploading a zip file.
+ *
+ * @since 5.5.0
+ *
+ * @param WP_Error $wp_error WP_Error object.
+ * @return bool True if the error should be hidden, false otherwise.
+ */
+ public function hide_process_failed( $wp_error ) {
+ if (
+ 'upload' === $this->type &&
+ '' === $this->overwrite &&
+ $wp_error->get_error_code() === 'folder_exists'
+ ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Performs an action following a plugin install.
+ *
+ * @since 2.8.0
+ */
+ public function after() {
+ // Check if the plugin can be overwritten and output the HTML.
+ if ( $this->do_overwrite() ) {
+ return;
+ }
+
+ $plugin_file = $this->upgrader->plugin_info();
+
+ $install_actions = array();
+
+ $from = isset( $_GET['from'] ) ? wp_unslash( $_GET['from'] ) : 'plugins';
+
+ if ( 'import' === $from ) {
+ $install_actions['activate_plugin'] = sprintf(
+ '%s ',
+ wp_nonce_url( 'plugins.php?action=activate&from=import&plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
+ __( 'Activate Plugin & Run Importer' )
+ );
+ } elseif ( 'press-this' === $from ) {
+ $install_actions['activate_plugin'] = sprintf(
+ '%s ',
+ wp_nonce_url( 'plugins.php?action=activate&from=press-this&plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
+ __( 'Activate Plugin & Go to Press This' )
+ );
+ } else {
+ $install_actions['activate_plugin'] = sprintf(
+ '%s ',
+ wp_nonce_url( 'plugins.php?action=activate&plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
+ __( 'Activate Plugin' )
+ );
+ }
+
+ if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
+ $install_actions['network_activate'] = sprintf(
+ '%s ',
+ wp_nonce_url( 'plugins.php?action=activate&networkwide=1&plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
+ __( 'Network Activate' )
+ );
+ unset( $install_actions['activate_plugin'] );
+ }
+
+ if ( 'import' === $from ) {
+ $install_actions['importers_page'] = sprintf(
+ '%s ',
+ admin_url( 'import.php' ),
+ __( 'Go to Importers' )
+ );
+ } elseif ( 'web' === $this->type ) {
+ $install_actions['plugins_page'] = sprintf(
+ '%s ',
+ self_admin_url( 'plugin-install.php' ),
+ __( 'Go to Plugin Installer' )
+ );
+ } elseif ( 'upload' === $this->type && 'plugins' === $from ) {
+ $install_actions['plugins_page'] = sprintf(
+ '%s ',
+ self_admin_url( 'plugin-install.php' ),
+ __( 'Go to Plugin Installer' )
+ );
+ } else {
+ $install_actions['plugins_page'] = sprintf(
+ '%s ',
+ self_admin_url( 'plugins.php' ),
+ __( 'Go to Plugins page' )
+ );
+ }
+
+ if ( ! $this->result || is_wp_error( $this->result ) ) {
+ unset( $install_actions['activate_plugin'], $install_actions['network_activate'] );
+ } elseif ( ! current_user_can( 'activate_plugin', $plugin_file ) || is_plugin_active( $plugin_file ) ) {
+ unset( $install_actions['activate_plugin'] );
+ }
+
+ /**
+ * Filters the list of action links available following a single plugin installation.
+ *
+ * @since 2.7.0
+ *
+ * @param string[] $install_actions Array of plugin action links.
+ * @param object $api Object containing WordPress.org API plugin data. Empty
+ * for non-API installs, such as when a plugin is installed
+ * via upload.
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ */
+ $install_actions = apply_filters( 'install_plugin_complete_actions', $install_actions, $this->api, $plugin_file );
+
+ if ( ! empty( $install_actions ) ) {
+ $this->feedback( implode( ' ', (array) $install_actions ) );
+ }
+ }
+
+ /**
+ * Checks if the plugin can be overwritten and outputs the HTML for overwriting a plugin on upload.
+ *
+ * @since 5.5.0
+ *
+ * @return bool Whether the plugin can be overwritten and HTML was outputted.
+ */
+ private function do_overwrite() {
+ if ( 'upload' !== $this->type || ! is_wp_error( $this->result ) || 'folder_exists' !== $this->result->get_error_code() ) {
+ return false;
+ }
+
+ $folder = $this->result->get_error_data( 'folder_exists' );
+ $folder = ltrim( substr( $folder, strlen( WP_PLUGIN_DIR ) ), '/' );
+
+ $current_plugin_data = false;
+ $all_plugins = get_plugins();
+
+ foreach ( $all_plugins as $plugin => $plugin_data ) {
+ if ( strrpos( $plugin, $folder ) !== 0 ) {
+ continue;
+ }
+
+ $current_plugin_data = $plugin_data;
+ }
+
+ $new_plugin_data = $this->upgrader->new_plugin_data;
+
+ if ( ! $current_plugin_data || ! $new_plugin_data ) {
+ return false;
+ }
+
+ echo '' . esc_html__( 'This plugin is already installed.' ) . ' ';
+
+ $this->is_downgrading = version_compare( $current_plugin_data['Version'], $new_plugin_data['Version'], '>' );
+
+ $rows = array(
+ 'Name' => __( 'Plugin name' ),
+ 'Version' => __( 'Version' ),
+ 'Author' => __( 'Author' ),
+ 'RequiresWP' => __( 'Required WordPress version' ),
+ 'RequiresPHP' => __( 'Required PHP version' ),
+ );
+
+ $table = '';
+ $table .= '' . esc_html_x( 'Current', 'plugin' ) . ' ';
+ $table .= '' . esc_html_x( 'Uploaded', 'plugin' ) . ' ';
+
+ $is_same_plugin = true; // Let's consider only these rows.
+
+ foreach ( $rows as $field => $label ) {
+ $old_value = ! empty( $current_plugin_data[ $field ] ) ? (string) $current_plugin_data[ $field ] : '-';
+ $new_value = ! empty( $new_plugin_data[ $field ] ) ? (string) $new_plugin_data[ $field ] : '-';
+
+ $is_same_plugin = $is_same_plugin && ( $old_value === $new_value );
+
+ $diff_field = ( 'Version' !== $field && $new_value !== $old_value );
+ $diff_version = ( 'Version' === $field && $this->is_downgrading );
+
+ $table .= '' . $label . ' ' . wp_strip_all_tags( $old_value ) . ' ';
+ $table .= ( $diff_field || $diff_version ) ? '' : ' ';
+ $table .= wp_strip_all_tags( $new_value ) . ' ';
+ }
+
+ $table .= '
';
+
+ /**
+ * Filters the compare table output for overwriting a plugin package on upload.
+ *
+ * @since 5.5.0
+ *
+ * @param string $table The output table with Name, Version, Author, RequiresWP, and RequiresPHP info.
+ * @param array $current_plugin_data Array with current plugin data.
+ * @param array $new_plugin_data Array with uploaded plugin data.
+ */
+ echo apply_filters( 'install_plugin_overwrite_comparison', $table, $current_plugin_data, $new_plugin_data );
+
+ $install_actions = array();
+ $can_update = true;
+
+ $blocked_message = '' . esc_html__( 'The plugin cannot be updated due to the following:' ) . '
';
+ $blocked_message .= '';
+
+ $requires_php = isset( $new_plugin_data['RequiresPHP'] ) ? $new_plugin_data['RequiresPHP'] : null;
+ $requires_wp = isset( $new_plugin_data['RequiresWP'] ) ? $new_plugin_data['RequiresWP'] : null;
+
+ if ( ! is_php_version_compatible( $requires_php ) ) {
+ $error = sprintf(
+ /* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */
+ __( 'The PHP version on your server is %1$s, however the uploaded plugin requires %2$s.' ),
+ PHP_VERSION,
+ $requires_php
+ );
+
+ $blocked_message .= '' . esc_html( $error ) . ' ';
+ $can_update = false;
+ }
+
+ if ( ! is_wp_version_compatible( $requires_wp ) ) {
+ $error = sprintf(
+ /* translators: 1: Current WordPress version, 2: Version required by the uploaded plugin. */
+ __( 'Your WordPress version is %1$s, however the uploaded plugin requires %2$s.' ),
+ get_bloginfo( 'version' ),
+ $requires_wp
+ );
+
+ $blocked_message .= '' . esc_html( $error ) . ' ';
+ $can_update = false;
+ }
+
+ $blocked_message .= ' ';
+
+ if ( $can_update ) {
+ if ( $this->is_downgrading ) {
+ $warning = sprintf(
+ /* translators: %s: Documentation URL. */
+ __( 'You are uploading an older version of a current plugin. You can continue to install the older version, but be sure to back up your database and files first.' ),
+ __( 'https://wordpress.org/documentation/article/wordpress-backups/' )
+ );
+ } else {
+ $warning = sprintf(
+ /* translators: %s: Documentation URL. */
+ __( 'You are updating a plugin. Be sure to back up your database and files first.' ),
+ __( 'https://wordpress.org/documentation/article/wordpress-backups/' )
+ );
+ }
+
+ echo '' . $warning . '
';
+
+ $overwrite = $this->is_downgrading ? 'downgrade-plugin' : 'update-plugin';
+
+ $install_actions['overwrite_plugin'] = sprintf(
+ '%s ',
+ wp_nonce_url( add_query_arg( 'overwrite', $overwrite, $this->url ), 'plugin-upload' ),
+ _x( 'Replace current with uploaded', 'plugin' )
+ );
+ } else {
+ echo $blocked_message;
+ }
+
+ $cancel_url = add_query_arg( 'action', 'upload-plugin-cancel-overwrite', $this->url );
+
+ $install_actions['plugins_page'] = sprintf(
+ '%s ',
+ wp_nonce_url( $cancel_url, 'plugin-upload-cancel-overwrite' ),
+ __( 'Cancel and go back' )
+ );
+
+ /**
+ * Filters the list of action links available following a single plugin installation failure
+ * when overwriting is allowed.
+ *
+ * @since 5.5.0
+ *
+ * @param string[] $install_actions Array of plugin action links.
+ * @param object $api Object containing WordPress.org API plugin data.
+ * @param array $new_plugin_data Array with uploaded plugin data.
+ */
+ $install_actions = apply_filters( 'install_plugin_overwrite_actions', $install_actions, $this->api, $new_plugin_data );
+
+ if ( ! empty( $install_actions ) ) {
+ printf(
+ '%s
',
+ __( 'The uploaded file has expired. Please go back and upload it again.' )
+ );
+ echo '' . implode( ' ', (array) $install_actions ) . '
';
+ }
+
+ return true;
+ }
+}
diff --git a/wp-admin/includes/class-plugin-upgrader-skin.php b/wp-admin/includes/class-plugin-upgrader-skin.php
new file mode 100644
index 0000000..0e3e778
--- /dev/null
+++ b/wp-admin/includes/class-plugin-upgrader-skin.php
@@ -0,0 +1,123 @@
+ '',
+ 'plugin' => '',
+ 'nonce' => '',
+ 'title' => __( 'Update Plugin' ),
+ );
+ $args = wp_parse_args( $args, $defaults );
+
+ $this->plugin = $args['plugin'];
+
+ $this->plugin_active = is_plugin_active( $this->plugin );
+ $this->plugin_network_active = is_plugin_active_for_network( $this->plugin );
+
+ parent::__construct( $args );
+ }
+
+ /**
+ * Performs an action following a single plugin update.
+ *
+ * @since 2.8.0
+ */
+ public function after() {
+ $this->plugin = $this->upgrader->plugin_info();
+ if ( ! empty( $this->plugin ) && ! is_wp_error( $this->result ) && $this->plugin_active ) {
+ // Currently used only when JS is off for a single plugin update?
+ printf(
+ '',
+ esc_attr__( 'Update progress' ),
+ wp_nonce_url( 'update.php?action=activate-plugin&networkwide=' . $this->plugin_network_active . '&plugin=' . urlencode( $this->plugin ), 'activate-plugin_' . $this->plugin )
+ );
+ }
+
+ $this->decrement_update_count( 'plugin' );
+
+ $update_actions = array(
+ 'activate_plugin' => sprintf(
+ '%s ',
+ wp_nonce_url( 'plugins.php?action=activate&plugin=' . urlencode( $this->plugin ), 'activate-plugin_' . $this->plugin ),
+ __( 'Activate Plugin' )
+ ),
+ 'plugins_page' => sprintf(
+ '%s ',
+ self_admin_url( 'plugins.php' ),
+ __( 'Go to Plugins page' )
+ ),
+ );
+
+ if ( $this->plugin_active || ! $this->result || is_wp_error( $this->result ) || ! current_user_can( 'activate_plugin', $this->plugin ) ) {
+ unset( $update_actions['activate_plugin'] );
+ }
+
+ /**
+ * Filters the list of action links available following a single plugin update.
+ *
+ * @since 2.7.0
+ *
+ * @param string[] $update_actions Array of plugin action links.
+ * @param string $plugin Path to the plugin file relative to the plugins directory.
+ */
+ $update_actions = apply_filters( 'update_plugin_complete_actions', $update_actions, $this->plugin );
+
+ if ( ! empty( $update_actions ) ) {
+ $this->feedback( implode( ' | ', (array) $update_actions ) );
+ }
+ }
+}
diff --git a/wp-admin/includes/class-plugin-upgrader.php b/wp-admin/includes/class-plugin-upgrader.php
new file mode 100644
index 0000000..02743f6
--- /dev/null
+++ b/wp-admin/includes/class-plugin-upgrader.php
@@ -0,0 +1,712 @@
+strings['up_to_date'] = __( 'The plugin is at the latest version.' );
+ $this->strings['no_package'] = __( 'Update package not available.' );
+ /* translators: %s: Package URL. */
+ $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s…' ), '%s ' );
+ $this->strings['unpack_package'] = __( 'Unpacking the update…' );
+ $this->strings['remove_old'] = __( 'Removing the old version of the plugin…' );
+ $this->strings['remove_old_failed'] = __( 'Could not remove the old plugin.' );
+ $this->strings['process_failed'] = __( 'Plugin update failed.' );
+ $this->strings['process_success'] = __( 'Plugin updated successfully.' );
+ $this->strings['process_bulk_success'] = __( 'Plugins updated successfully.' );
+ }
+
+ /**
+ * Initializes the installation strings.
+ *
+ * @since 2.8.0
+ */
+ public function install_strings() {
+ $this->strings['no_package'] = __( 'Installation package not available.' );
+ /* translators: %s: Package URL. */
+ $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s…' ), '%s ' );
+ $this->strings['unpack_package'] = __( 'Unpacking the package…' );
+ $this->strings['installing_package'] = __( 'Installing the plugin…' );
+ $this->strings['remove_old'] = __( 'Removing the current plugin…' );
+ $this->strings['remove_old_failed'] = __( 'Could not remove the current plugin.' );
+ $this->strings['no_files'] = __( 'The plugin contains no files.' );
+ $this->strings['process_failed'] = __( 'Plugin installation failed.' );
+ $this->strings['process_success'] = __( 'Plugin installed successfully.' );
+ /* translators: 1: Plugin name, 2: Plugin version. */
+ $this->strings['process_success_specific'] = __( 'Successfully installed the plugin %1$s %2$s .' );
+
+ if ( ! empty( $this->skin->overwrite ) ) {
+ if ( 'update-plugin' === $this->skin->overwrite ) {
+ $this->strings['installing_package'] = __( 'Updating the plugin…' );
+ $this->strings['process_failed'] = __( 'Plugin update failed.' );
+ $this->strings['process_success'] = __( 'Plugin updated successfully.' );
+ }
+
+ if ( 'downgrade-plugin' === $this->skin->overwrite ) {
+ $this->strings['installing_package'] = __( 'Downgrading the plugin…' );
+ $this->strings['process_failed'] = __( 'Plugin downgrade failed.' );
+ $this->strings['process_success'] = __( 'Plugin downgraded successfully.' );
+ }
+ }
+ }
+
+ /**
+ * Install a plugin package.
+ *
+ * @since 2.8.0
+ * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
+ *
+ * @param string $package The full local path or URI of the package.
+ * @param array $args {
+ * Optional. Other arguments for installing a plugin package. Default empty array.
+ *
+ * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
+ * Default true.
+ * }
+ * @return bool|WP_Error True if the installation was successful, false or a WP_Error otherwise.
+ */
+ public function install( $package, $args = array() ) {
+ $defaults = array(
+ 'clear_update_cache' => true,
+ 'overwrite_package' => false, // Do not overwrite files.
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->install_strings();
+
+ add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
+
+ if ( $parsed_args['clear_update_cache'] ) {
+ // Clear cache so wp_update_plugins() knows about the new plugin.
+ add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
+ }
+
+ $this->run(
+ array(
+ 'package' => $package,
+ 'destination' => WP_PLUGIN_DIR,
+ 'clear_destination' => $parsed_args['overwrite_package'],
+ 'clear_working' => true,
+ 'hook_extra' => array(
+ 'type' => 'plugin',
+ 'action' => 'install',
+ ),
+ )
+ );
+
+ remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
+ remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
+
+ if ( ! $this->result || is_wp_error( $this->result ) ) {
+ return $this->result;
+ }
+
+ // Force refresh of plugin update information.
+ wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
+
+ if ( $parsed_args['overwrite_package'] ) {
+ /**
+ * Fires when the upgrader has successfully overwritten a currently installed
+ * plugin or theme with an uploaded zip package.
+ *
+ * @since 5.5.0
+ *
+ * @param string $package The package file.
+ * @param array $data The new plugin or theme data.
+ * @param string $package_type The package type ('plugin' or 'theme').
+ */
+ do_action( 'upgrader_overwrote_package', $package, $this->new_plugin_data, 'plugin' );
+ }
+
+ return true;
+ }
+
+ /**
+ * Upgrades a plugin.
+ *
+ * @since 2.8.0
+ * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
+ *
+ * @param string $plugin Path to the plugin file relative to the plugins directory.
+ * @param array $args {
+ * Optional. Other arguments for upgrading a plugin package. Default empty array.
+ *
+ * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
+ * Default true.
+ * }
+ * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
+ */
+ public function upgrade( $plugin, $args = array() ) {
+ $defaults = array(
+ 'clear_update_cache' => true,
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->upgrade_strings();
+
+ $current = get_site_transient( 'update_plugins' );
+ if ( ! isset( $current->response[ $plugin ] ) ) {
+ $this->skin->before();
+ $this->skin->set_result( false );
+ $this->skin->error( 'up_to_date' );
+ $this->skin->after();
+ return false;
+ }
+
+ // Get the URL to the zip file.
+ $r = $current->response[ $plugin ];
+
+ add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
+ add_filter( 'upgrader_pre_install', array( $this, 'active_before' ), 10, 2 );
+ add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
+ add_filter( 'upgrader_post_install', array( $this, 'active_after' ), 10, 2 );
+ /*
+ * There's a Trac ticket to move up the directory for zips which are made a bit differently, useful for non-.org plugins.
+ * 'source_selection' => array( $this, 'source_selection' ),
+ */
+ if ( $parsed_args['clear_update_cache'] ) {
+ // Clear cache so wp_update_plugins() knows about the new plugin.
+ add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
+ }
+
+ $this->run(
+ array(
+ 'package' => $r->package,
+ 'destination' => WP_PLUGIN_DIR,
+ 'clear_destination' => true,
+ 'clear_working' => true,
+ 'hook_extra' => array(
+ 'plugin' => $plugin,
+ 'type' => 'plugin',
+ 'action' => 'update',
+ 'temp_backup' => array(
+ 'slug' => dirname( $plugin ),
+ 'src' => WP_PLUGIN_DIR,
+ 'dir' => 'plugins',
+ ),
+ ),
+ )
+ );
+
+ // Cleanup our hooks, in case something else does an upgrade on this connection.
+ remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
+ remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
+ remove_filter( 'upgrader_pre_install', array( $this, 'active_before' ) );
+ remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
+ remove_filter( 'upgrader_post_install', array( $this, 'active_after' ) );
+
+ if ( ! $this->result || is_wp_error( $this->result ) ) {
+ return $this->result;
+ }
+
+ // Force refresh of plugin update information.
+ wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
+
+ /*
+ * Ensure any future auto-update failures trigger a failure email by removing
+ * the last failure notification from the list when plugins update successfully.
+ */
+ $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
+
+ if ( isset( $past_failure_emails[ $plugin ] ) ) {
+ unset( $past_failure_emails[ $plugin ] );
+ update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
+ }
+
+ return true;
+ }
+
+ /**
+ * Upgrades several plugins at once.
+ *
+ * @since 2.8.0
+ * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
+ *
+ * @global string $wp_version The WordPress version string.
+ *
+ * @param string[] $plugins Array of paths to plugin files relative to the plugins directory.
+ * @param array $args {
+ * Optional. Other arguments for upgrading several plugins at once.
+ *
+ * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful. Default true.
+ * }
+ * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
+ */
+ public function bulk_upgrade( $plugins, $args = array() ) {
+ global $wp_version;
+
+ $defaults = array(
+ 'clear_update_cache' => true,
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->bulk = true;
+ $this->upgrade_strings();
+
+ $current = get_site_transient( 'update_plugins' );
+
+ add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
+
+ $this->skin->header();
+
+ // Connect to the filesystem first.
+ $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
+ if ( ! $res ) {
+ $this->skin->footer();
+ return false;
+ }
+
+ $this->skin->bulk_header();
+
+ /*
+ * Only start maintenance mode if:
+ * - running Multisite and there are one or more plugins specified, OR
+ * - a plugin with an update available is currently active.
+ * @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
+ */
+ $maintenance = ( is_multisite() && ! empty( $plugins ) );
+ foreach ( $plugins as $plugin ) {
+ $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin ] ) );
+ }
+ if ( $maintenance ) {
+ $this->maintenance_mode( true );
+ }
+
+ $results = array();
+
+ $this->update_count = count( $plugins );
+ $this->update_current = 0;
+ foreach ( $plugins as $plugin ) {
+ ++$this->update_current;
+ $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true );
+
+ if ( ! isset( $current->response[ $plugin ] ) ) {
+ $this->skin->set_result( 'up_to_date' );
+ $this->skin->before();
+ $this->skin->feedback( 'up_to_date' );
+ $this->skin->after();
+ $results[ $plugin ] = true;
+ continue;
+ }
+
+ // Get the URL to the zip file.
+ $r = $current->response[ $plugin ];
+
+ $this->skin->plugin_active = is_plugin_active( $plugin );
+
+ if ( isset( $r->requires ) && ! is_wp_version_compatible( $r->requires ) ) {
+ $result = new WP_Error(
+ 'incompatible_wp_required_version',
+ sprintf(
+ /* translators: 1: Current WordPress version, 2: WordPress version required by the new plugin version. */
+ __( 'Your WordPress version is %1$s, however the new plugin version requires %2$s.' ),
+ $wp_version,
+ $r->requires
+ )
+ );
+
+ $this->skin->before( $result );
+ $this->skin->error( $result );
+ $this->skin->after();
+ } elseif ( isset( $r->requires_php ) && ! is_php_version_compatible( $r->requires_php ) ) {
+ $result = new WP_Error(
+ 'incompatible_php_required_version',
+ sprintf(
+ /* translators: 1: Current PHP version, 2: PHP version required by the new plugin version. */
+ __( 'The PHP version on your server is %1$s, however the new plugin version requires %2$s.' ),
+ PHP_VERSION,
+ $r->requires_php
+ )
+ );
+
+ $this->skin->before( $result );
+ $this->skin->error( $result );
+ $this->skin->after();
+ } else {
+ add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
+ $result = $this->run(
+ array(
+ 'package' => $r->package,
+ 'destination' => WP_PLUGIN_DIR,
+ 'clear_destination' => true,
+ 'clear_working' => true,
+ 'is_multi' => true,
+ 'hook_extra' => array(
+ 'plugin' => $plugin,
+ 'temp_backup' => array(
+ 'slug' => dirname( $plugin ),
+ 'src' => WP_PLUGIN_DIR,
+ 'dir' => 'plugins',
+ ),
+ ),
+ )
+ );
+ remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
+ }
+
+ $results[ $plugin ] = $result;
+
+ // Prevent credentials auth screen from displaying multiple times.
+ if ( false === $result ) {
+ break;
+ }
+ } // End foreach $plugins.
+
+ $this->maintenance_mode( false );
+
+ // Force refresh of plugin update information.
+ wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
+
+ /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
+ do_action(
+ 'upgrader_process_complete',
+ $this,
+ array(
+ 'action' => 'update',
+ 'type' => 'plugin',
+ 'bulk' => true,
+ 'plugins' => $plugins,
+ )
+ );
+
+ $this->skin->bulk_footer();
+
+ $this->skin->footer();
+
+ // Cleanup our hooks, in case something else does an upgrade on this connection.
+ remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
+
+ /*
+ * Ensure any future auto-update failures trigger a failure email by removing
+ * the last failure notification from the list when plugins update successfully.
+ */
+ $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
+
+ foreach ( $results as $plugin => $result ) {
+ // Maintain last failure notification when plugins failed to update manually.
+ if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $plugin ] ) ) {
+ continue;
+ }
+
+ unset( $past_failure_emails[ $plugin ] );
+ }
+
+ update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
+
+ return $results;
+ }
+
+ /**
+ * Checks that the source package contains a valid plugin.
+ *
+ * Hooked to the {@see 'upgrader_source_selection'} filter by Plugin_Upgrader::install().
+ *
+ * @since 3.3.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ * @global string $wp_version The WordPress version string.
+ *
+ * @param string $source The path to the downloaded package source.
+ * @return string|WP_Error The source as passed, or a WP_Error object on failure.
+ */
+ public function check_package( $source ) {
+ global $wp_filesystem, $wp_version;
+
+ $this->new_plugin_data = array();
+
+ if ( is_wp_error( $source ) ) {
+ return $source;
+ }
+
+ $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
+ if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
+ return $source;
+ }
+
+ // Check that the folder contains at least 1 valid plugin.
+ $files = glob( $working_directory . '*.php' );
+ if ( $files ) {
+ foreach ( $files as $file ) {
+ $info = get_plugin_data( $file, false, false );
+ if ( ! empty( $info['Name'] ) ) {
+ $this->new_plugin_data = $info;
+ break;
+ }
+ }
+ }
+
+ if ( empty( $this->new_plugin_data ) ) {
+ return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
+ }
+
+ $requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
+ $requires_wp = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
+
+ if ( ! is_php_version_compatible( $requires_php ) ) {
+ $error = sprintf(
+ /* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */
+ __( 'The PHP version on your server is %1$s, however the uploaded plugin requires %2$s.' ),
+ PHP_VERSION,
+ $requires_php
+ );
+
+ return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
+ }
+
+ if ( ! is_wp_version_compatible( $requires_wp ) ) {
+ $error = sprintf(
+ /* translators: 1: Current WordPress version, 2: Version required by the uploaded plugin. */
+ __( 'Your WordPress version is %1$s, however the uploaded plugin requires %2$s.' ),
+ $wp_version,
+ $requires_wp
+ );
+
+ return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
+ }
+
+ return $source;
+ }
+
+ /**
+ * Retrieves the path to the file that contains the plugin info.
+ *
+ * This isn't used internally in the class, but is called by the skins.
+ *
+ * @since 2.8.0
+ *
+ * @return string|false The full path to the main plugin file, or false.
+ */
+ public function plugin_info() {
+ if ( ! is_array( $this->result ) ) {
+ return false;
+ }
+ if ( empty( $this->result['destination_name'] ) ) {
+ return false;
+ }
+
+ // Ensure to pass with leading slash.
+ $plugin = get_plugins( '/' . $this->result['destination_name'] );
+ if ( empty( $plugin ) ) {
+ return false;
+ }
+
+ // Assume the requested plugin is the first in the list.
+ $pluginfiles = array_keys( $plugin );
+
+ return $this->result['destination_name'] . '/' . $pluginfiles[0];
+ }
+
+ /**
+ * Deactivates a plugin before it is upgraded.
+ *
+ * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
+ *
+ * @since 2.8.0
+ * @since 4.1.0 Added a return value.
+ *
+ * @param bool|WP_Error $response The installation response before the installation has started.
+ * @param array $plugin Plugin package arguments.
+ * @return bool|WP_Error The original `$response` parameter or WP_Error.
+ */
+ public function deactivate_plugin_before_upgrade( $response, $plugin ) {
+
+ if ( is_wp_error( $response ) ) { // Bypass.
+ return $response;
+ }
+
+ // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it.
+ if ( wp_doing_cron() ) {
+ return $response;
+ }
+
+ $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
+ if ( empty( $plugin ) ) {
+ return new WP_Error( 'bad_request', $this->strings['bad_request'] );
+ }
+
+ if ( is_plugin_active( $plugin ) ) {
+ // Deactivate the plugin silently, Prevent deactivation hooks from running.
+ deactivate_plugins( $plugin, true );
+ }
+
+ return $response;
+ }
+
+ /**
+ * Turns on maintenance mode before attempting to background update an active plugin.
+ *
+ * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
+ *
+ * @since 5.4.0
+ *
+ * @param bool|WP_Error $response The installation response before the installation has started.
+ * @param array $plugin Plugin package arguments.
+ * @return bool|WP_Error The original `$response` parameter or WP_Error.
+ */
+ public function active_before( $response, $plugin ) {
+ if ( is_wp_error( $response ) ) {
+ return $response;
+ }
+
+ // Only enable maintenance mode when in cron (background update).
+ if ( ! wp_doing_cron() ) {
+ return $response;
+ }
+
+ $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
+
+ // Only run if plugin is active.
+ if ( ! is_plugin_active( $plugin ) ) {
+ return $response;
+ }
+
+ // Change to maintenance mode. Bulk edit handles this separately.
+ if ( ! $this->bulk ) {
+ $this->maintenance_mode( true );
+ }
+
+ return $response;
+ }
+
+ /**
+ * Turns off maintenance mode after upgrading an active plugin.
+ *
+ * Hooked to the {@see 'upgrader_post_install'} filter by Plugin_Upgrader::upgrade().
+ *
+ * @since 5.4.0
+ *
+ * @param bool|WP_Error $response The installation response after the installation has finished.
+ * @param array $plugin Plugin package arguments.
+ * @return bool|WP_Error The original `$response` parameter or WP_Error.
+ */
+ public function active_after( $response, $plugin ) {
+ if ( is_wp_error( $response ) ) {
+ return $response;
+ }
+
+ // Only disable maintenance mode when in cron (background update).
+ if ( ! wp_doing_cron() ) {
+ return $response;
+ }
+
+ $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
+
+ // Only run if plugin is active.
+ if ( ! is_plugin_active( $plugin ) ) {
+ return $response;
+ }
+
+ // Time to remove maintenance mode. Bulk edit handles this separately.
+ if ( ! $this->bulk ) {
+ $this->maintenance_mode( false );
+ }
+
+ return $response;
+ }
+
+ /**
+ * Deletes the old plugin during an upgrade.
+ *
+ * Hooked to the {@see 'upgrader_clear_destination'} filter by
+ * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
+ *
+ * @since 2.8.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param bool|WP_Error $removed Whether the destination was cleared.
+ * True on success, WP_Error on failure.
+ * @param string $local_destination The local package destination.
+ * @param string $remote_destination The remote package destination.
+ * @param array $plugin Extra arguments passed to hooked filters.
+ * @return bool|WP_Error
+ */
+ public function delete_old_plugin( $removed, $local_destination, $remote_destination, $plugin ) {
+ global $wp_filesystem;
+
+ if ( is_wp_error( $removed ) ) {
+ return $removed; // Pass errors through.
+ }
+
+ $plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
+ if ( empty( $plugin ) ) {
+ return new WP_Error( 'bad_request', $this->strings['bad_request'] );
+ }
+
+ $plugins_dir = $wp_filesystem->wp_plugins_dir();
+ $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
+
+ if ( ! $wp_filesystem->exists( $this_plugin_dir ) ) { // If it's already vanished.
+ return $removed;
+ }
+
+ /*
+ * If plugin is in its own directory, recursively delete the directory.
+ * Base check on if plugin includes directory separator AND that it's not the root plugin folder.
+ */
+ if ( strpos( $plugin, '/' ) && $this_plugin_dir !== $plugins_dir ) {
+ $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
+ } else {
+ $deleted = $wp_filesystem->delete( $plugins_dir . $plugin );
+ }
+
+ if ( ! $deleted ) {
+ return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
+ }
+
+ return true;
+ }
+}
diff --git a/wp-admin/includes/class-theme-installer-skin.php b/wp-admin/includes/class-theme-installer-skin.php
new file mode 100644
index 0000000..99fe322
--- /dev/null
+++ b/wp-admin/includes/class-theme-installer-skin.php
@@ -0,0 +1,384 @@
+ 'web',
+ 'url' => '',
+ 'theme' => '',
+ 'nonce' => '',
+ 'title' => '',
+ 'overwrite' => '',
+ );
+ $args = wp_parse_args( $args, $defaults );
+
+ $this->type = $args['type'];
+ $this->url = $args['url'];
+ $this->api = isset( $args['api'] ) ? $args['api'] : array();
+ $this->overwrite = $args['overwrite'];
+
+ parent::__construct( $args );
+ }
+
+ /**
+ * Performs an action before installing a theme.
+ *
+ * @since 2.8.0
+ */
+ public function before() {
+ if ( ! empty( $this->api ) ) {
+ $this->upgrader->strings['process_success'] = sprintf(
+ $this->upgrader->strings['process_success_specific'],
+ $this->api->name,
+ $this->api->version
+ );
+ }
+ }
+
+ /**
+ * Hides the `process_failed` error when updating a theme by uploading a zip file.
+ *
+ * @since 5.5.0
+ *
+ * @param WP_Error $wp_error WP_Error object.
+ * @return bool True if the error should be hidden, false otherwise.
+ */
+ public function hide_process_failed( $wp_error ) {
+ if (
+ 'upload' === $this->type &&
+ '' === $this->overwrite &&
+ $wp_error->get_error_code() === 'folder_exists'
+ ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Performs an action following a single theme install.
+ *
+ * @since 2.8.0
+ */
+ public function after() {
+ if ( $this->do_overwrite() ) {
+ return;
+ }
+
+ if ( empty( $this->upgrader->result['destination_name'] ) ) {
+ return;
+ }
+
+ $theme_info = $this->upgrader->theme_info();
+ if ( empty( $theme_info ) ) {
+ return;
+ }
+
+ $name = $theme_info->display( 'Name' );
+ $stylesheet = $this->upgrader->result['destination_name'];
+ $template = $theme_info->get_template();
+
+ $activate_link = add_query_arg(
+ array(
+ 'action' => 'activate',
+ 'template' => urlencode( $template ),
+ 'stylesheet' => urlencode( $stylesheet ),
+ ),
+ admin_url( 'themes.php' )
+ );
+ $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
+
+ $install_actions = array();
+
+ if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) && ! $theme_info->is_block_theme() ) {
+ $customize_url = add_query_arg(
+ array(
+ 'theme' => urlencode( $stylesheet ),
+ 'return' => urlencode( admin_url( 'web' === $this->type ? 'theme-install.php' : 'themes.php' ) ),
+ ),
+ admin_url( 'customize.php' )
+ );
+
+ $install_actions['preview'] = sprintf(
+ '' .
+ '%s %s ',
+ esc_url( $customize_url ),
+ __( 'Live Preview' ),
+ /* translators: Hidden accessibility text. %s: Theme name. */
+ sprintf( __( 'Live Preview “%s”' ), $name )
+ );
+ }
+
+ $install_actions['activate'] = sprintf(
+ '' .
+ '%s %s ',
+ esc_url( $activate_link ),
+ __( 'Activate' ),
+ /* translators: Hidden accessibility text. %s: Theme name. */
+ sprintf( _x( 'Activate “%s”', 'theme' ), $name )
+ );
+
+ if ( is_network_admin() && current_user_can( 'manage_network_themes' ) ) {
+ $install_actions['network_enable'] = sprintf(
+ '%s ',
+ esc_url( wp_nonce_url( 'themes.php?action=enable&theme=' . urlencode( $stylesheet ), 'enable-theme_' . $stylesheet ) ),
+ __( 'Network Enable' )
+ );
+ }
+
+ if ( 'web' === $this->type ) {
+ $install_actions['themes_page'] = sprintf(
+ '%s ',
+ self_admin_url( 'theme-install.php' ),
+ __( 'Go to Theme Installer' )
+ );
+ } elseif ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) ) {
+ $install_actions['themes_page'] = sprintf(
+ '%s ',
+ self_admin_url( 'themes.php' ),
+ __( 'Go to Themes page' )
+ );
+ }
+
+ if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() || ! current_user_can( 'switch_themes' ) ) {
+ unset( $install_actions['activate'], $install_actions['preview'] );
+ } elseif ( get_option( 'template' ) === $stylesheet ) {
+ unset( $install_actions['activate'] );
+ }
+
+ /**
+ * Filters the list of action links available following a single theme installation.
+ *
+ * @since 2.8.0
+ *
+ * @param string[] $install_actions Array of theme action links.
+ * @param object $api Object containing WordPress.org API theme data.
+ * @param string $stylesheet Theme directory name.
+ * @param WP_Theme $theme_info Theme object.
+ */
+ $install_actions = apply_filters( 'install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info );
+ if ( ! empty( $install_actions ) ) {
+ $this->feedback( implode( ' | ', (array) $install_actions ) );
+ }
+ }
+
+ /**
+ * Checks if the theme can be overwritten and outputs the HTML for overwriting a theme on upload.
+ *
+ * @since 5.5.0
+ *
+ * @return bool Whether the theme can be overwritten and HTML was outputted.
+ */
+ private function do_overwrite() {
+ if ( 'upload' !== $this->type || ! is_wp_error( $this->result ) || 'folder_exists' !== $this->result->get_error_code() ) {
+ return false;
+ }
+
+ $folder = $this->result->get_error_data( 'folder_exists' );
+ $folder = rtrim( $folder, '/' );
+
+ $current_theme_data = false;
+ $all_themes = wp_get_themes( array( 'errors' => null ) );
+
+ foreach ( $all_themes as $theme ) {
+ $stylesheet_dir = wp_normalize_path( $theme->get_stylesheet_directory() );
+
+ if ( rtrim( $stylesheet_dir, '/' ) !== $folder ) {
+ continue;
+ }
+
+ $current_theme_data = $theme;
+ }
+
+ $new_theme_data = $this->upgrader->new_theme_data;
+
+ if ( ! $current_theme_data || ! $new_theme_data ) {
+ return false;
+ }
+
+ echo '' . esc_html__( 'This theme is already installed.' ) . ' ';
+
+ // Check errors for active theme.
+ if ( is_wp_error( $current_theme_data->errors() ) ) {
+ $this->feedback( 'current_theme_has_errors', $current_theme_data->errors()->get_error_message() );
+ }
+
+ $this->is_downgrading = version_compare( $current_theme_data['Version'], $new_theme_data['Version'], '>' );
+
+ $is_invalid_parent = false;
+ if ( ! empty( $new_theme_data['Template'] ) ) {
+ $is_invalid_parent = ! in_array( $new_theme_data['Template'], array_keys( $all_themes ), true );
+ }
+
+ $rows = array(
+ 'Name' => __( 'Theme name' ),
+ 'Version' => __( 'Version' ),
+ 'Author' => __( 'Author' ),
+ 'RequiresWP' => __( 'Required WordPress version' ),
+ 'RequiresPHP' => __( 'Required PHP version' ),
+ 'Template' => __( 'Parent theme' ),
+ );
+
+ $table = '';
+ $table .= '' . esc_html_x( 'Active', 'theme' ) . ' ' . esc_html_x( 'Uploaded', 'theme' ) . ' ';
+
+ $is_same_theme = true; // Let's consider only these rows.
+
+ foreach ( $rows as $field => $label ) {
+ $old_value = $current_theme_data->display( $field, false );
+ $old_value = $old_value ? (string) $old_value : '-';
+
+ $new_value = ! empty( $new_theme_data[ $field ] ) ? (string) $new_theme_data[ $field ] : '-';
+
+ if ( $old_value === $new_value && '-' === $new_value && 'Template' === $field ) {
+ continue;
+ }
+
+ $is_same_theme = $is_same_theme && ( $old_value === $new_value );
+
+ $diff_field = ( 'Version' !== $field && $new_value !== $old_value );
+ $diff_version = ( 'Version' === $field && $this->is_downgrading );
+ $invalid_parent = false;
+
+ if ( 'Template' === $field && $is_invalid_parent ) {
+ $invalid_parent = true;
+ $new_value .= ' ' . __( '(not found)' );
+ }
+
+ $table .= '' . $label . ' ' . wp_strip_all_tags( $old_value ) . ' ';
+ $table .= ( $diff_field || $diff_version || $invalid_parent ) ? '' : ' ';
+ $table .= wp_strip_all_tags( $new_value ) . ' ';
+ }
+
+ $table .= '
';
+
+ /**
+ * Filters the compare table output for overwriting a theme package on upload.
+ *
+ * @since 5.5.0
+ *
+ * @param string $table The output table with Name, Version, Author, RequiresWP, and RequiresPHP info.
+ * @param WP_Theme $current_theme_data Active theme data.
+ * @param array $new_theme_data Array with uploaded theme data.
+ */
+ echo apply_filters( 'install_theme_overwrite_comparison', $table, $current_theme_data, $new_theme_data );
+
+ $install_actions = array();
+ $can_update = true;
+
+ $blocked_message = '' . esc_html__( 'The theme cannot be updated due to the following:' ) . '
';
+ $blocked_message .= '';
+
+ $requires_php = isset( $new_theme_data['RequiresPHP'] ) ? $new_theme_data['RequiresPHP'] : null;
+ $requires_wp = isset( $new_theme_data['RequiresWP'] ) ? $new_theme_data['RequiresWP'] : null;
+
+ if ( ! is_php_version_compatible( $requires_php ) ) {
+ $error = sprintf(
+ /* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
+ __( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
+ PHP_VERSION,
+ $requires_php
+ );
+
+ $blocked_message .= '' . esc_html( $error ) . ' ';
+ $can_update = false;
+ }
+
+ if ( ! is_wp_version_compatible( $requires_wp ) ) {
+ $error = sprintf(
+ /* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
+ __( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
+ get_bloginfo( 'version' ),
+ $requires_wp
+ );
+
+ $blocked_message .= '' . esc_html( $error ) . ' ';
+ $can_update = false;
+ }
+
+ $blocked_message .= ' ';
+
+ if ( $can_update ) {
+ if ( $this->is_downgrading ) {
+ $warning = sprintf(
+ /* translators: %s: Documentation URL. */
+ __( 'You are uploading an older version of the active theme. You can continue to install the older version, but be sure to back up your database and files first.' ),
+ __( 'https://wordpress.org/documentation/article/wordpress-backups/' )
+ );
+ } else {
+ $warning = sprintf(
+ /* translators: %s: Documentation URL. */
+ __( 'You are updating a theme. Be sure to back up your database and files first.' ),
+ __( 'https://wordpress.org/documentation/article/wordpress-backups/' )
+ );
+ }
+
+ echo '' . $warning . '
';
+
+ $overwrite = $this->is_downgrading ? 'downgrade-theme' : 'update-theme';
+
+ $install_actions['overwrite_theme'] = sprintf(
+ '%s ',
+ wp_nonce_url( add_query_arg( 'overwrite', $overwrite, $this->url ), 'theme-upload' ),
+ _x( 'Replace active with uploaded', 'theme' )
+ );
+ } else {
+ echo $blocked_message;
+ }
+
+ $cancel_url = add_query_arg( 'action', 'upload-theme-cancel-overwrite', $this->url );
+
+ $install_actions['themes_page'] = sprintf(
+ '%s ',
+ wp_nonce_url( $cancel_url, 'theme-upload-cancel-overwrite' ),
+ __( 'Cancel and go back' )
+ );
+
+ /**
+ * Filters the list of action links available following a single theme installation failure
+ * when overwriting is allowed.
+ *
+ * @since 5.5.0
+ *
+ * @param string[] $install_actions Array of theme action links.
+ * @param object $api Object containing WordPress.org API theme data.
+ * @param array $new_theme_data Array with uploaded theme data.
+ */
+ $install_actions = apply_filters( 'install_theme_overwrite_actions', $install_actions, $this->api, $new_theme_data );
+
+ if ( ! empty( $install_actions ) ) {
+ printf(
+ '%s
',
+ __( 'The uploaded file has expired. Please go back and upload it again.' )
+ );
+ echo '' . implode( ' ', (array) $install_actions ) . '
';
+ }
+
+ return true;
+ }
+}
diff --git a/wp-admin/includes/class-theme-upgrader-skin.php b/wp-admin/includes/class-theme-upgrader-skin.php
new file mode 100644
index 0000000..97d76a8
--- /dev/null
+++ b/wp-admin/includes/class-theme-upgrader-skin.php
@@ -0,0 +1,144 @@
+ '',
+ 'theme' => '',
+ 'nonce' => '',
+ 'title' => __( 'Update Theme' ),
+ );
+ $args = wp_parse_args( $args, $defaults );
+
+ $this->theme = $args['theme'];
+
+ parent::__construct( $args );
+ }
+
+ /**
+ * Performs an action following a single theme update.
+ *
+ * @since 2.8.0
+ */
+ public function after() {
+ $this->decrement_update_count( 'theme' );
+
+ $update_actions = array();
+ $theme_info = $this->upgrader->theme_info();
+ if ( $theme_info ) {
+ $name = $theme_info->display( 'Name' );
+ $stylesheet = $this->upgrader->result['destination_name'];
+ $template = $theme_info->get_template();
+
+ $activate_link = add_query_arg(
+ array(
+ 'action' => 'activate',
+ 'template' => urlencode( $template ),
+ 'stylesheet' => urlencode( $stylesheet ),
+ ),
+ admin_url( 'themes.php' )
+ );
+ $activate_link = wp_nonce_url( $activate_link, 'switch-theme_' . $stylesheet );
+
+ $customize_url = add_query_arg(
+ array(
+ 'theme' => urlencode( $stylesheet ),
+ 'return' => urlencode( admin_url( 'themes.php' ) ),
+ ),
+ admin_url( 'customize.php' )
+ );
+
+ if ( get_stylesheet() === $stylesheet ) {
+ if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
+ $update_actions['preview'] = sprintf(
+ '' .
+ '%s %s ',
+ esc_url( $customize_url ),
+ __( 'Customize' ),
+ /* translators: Hidden accessibility text. %s: Theme name. */
+ sprintf( __( 'Customize “%s”' ), $name )
+ );
+ }
+ } elseif ( current_user_can( 'switch_themes' ) ) {
+ if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
+ $update_actions['preview'] = sprintf(
+ '' .
+ '%s %s ',
+ esc_url( $customize_url ),
+ __( 'Live Preview' ),
+ /* translators: Hidden accessibility text. %s: Theme name. */
+ sprintf( __( 'Live Preview “%s”' ), $name )
+ );
+ }
+
+ $update_actions['activate'] = sprintf(
+ '' .
+ '%s %s ',
+ esc_url( $activate_link ),
+ __( 'Activate' ),
+ /* translators: Hidden accessibility text. %s: Theme name. */
+ sprintf( _x( 'Activate “%s”', 'theme' ), $name )
+ );
+ }
+
+ if ( ! $this->result || is_wp_error( $this->result ) || is_network_admin() ) {
+ unset( $update_actions['preview'], $update_actions['activate'] );
+ }
+ }
+
+ $update_actions['themes_page'] = sprintf(
+ '%s ',
+ self_admin_url( 'themes.php' ),
+ __( 'Go to Themes page' )
+ );
+
+ /**
+ * Filters the list of action links available following a single theme update.
+ *
+ * @since 2.8.0
+ *
+ * @param string[] $update_actions Array of theme action links.
+ * @param string $theme Theme directory name.
+ */
+ $update_actions = apply_filters( 'update_theme_complete_actions', $update_actions, $this->theme );
+
+ if ( ! empty( $update_actions ) ) {
+ $this->feedback( implode( ' | ', (array) $update_actions ) );
+ }
+ }
+}
diff --git a/wp-admin/includes/class-theme-upgrader.php b/wp-admin/includes/class-theme-upgrader.php
new file mode 100644
index 0000000..12bd477
--- /dev/null
+++ b/wp-admin/includes/class-theme-upgrader.php
@@ -0,0 +1,771 @@
+strings['up_to_date'] = __( 'The theme is at the latest version.' );
+ $this->strings['no_package'] = __( 'Update package not available.' );
+ /* translators: %s: Package URL. */
+ $this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s…' ), '%s ' );
+ $this->strings['unpack_package'] = __( 'Unpacking the update…' );
+ $this->strings['remove_old'] = __( 'Removing the old version of the theme…' );
+ $this->strings['remove_old_failed'] = __( 'Could not remove the old theme.' );
+ $this->strings['process_failed'] = __( 'Theme update failed.' );
+ $this->strings['process_success'] = __( 'Theme updated successfully.' );
+ }
+
+ /**
+ * Initializes the installation strings.
+ *
+ * @since 2.8.0
+ */
+ public function install_strings() {
+ $this->strings['no_package'] = __( 'Installation package not available.' );
+ /* translators: %s: Package URL. */
+ $this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s…' ), '%s ' );
+ $this->strings['unpack_package'] = __( 'Unpacking the package…' );
+ $this->strings['installing_package'] = __( 'Installing the theme…' );
+ $this->strings['remove_old'] = __( 'Removing the old version of the theme…' );
+ $this->strings['remove_old_failed'] = __( 'Could not remove the old theme.' );
+ $this->strings['no_files'] = __( 'The theme contains no files.' );
+ $this->strings['process_failed'] = __( 'Theme installation failed.' );
+ $this->strings['process_success'] = __( 'Theme installed successfully.' );
+ /* translators: 1: Theme name, 2: Theme version. */
+ $this->strings['process_success_specific'] = __( 'Successfully installed the theme %1$s %2$s .' );
+ $this->strings['parent_theme_search'] = __( 'This theme requires a parent theme. Checking if it is installed…' );
+ /* translators: 1: Theme name, 2: Theme version. */
+ $this->strings['parent_theme_prepare_install'] = __( 'Preparing to install %1$s %2$s …' );
+ /* translators: 1: Theme name, 2: Theme version. */
+ $this->strings['parent_theme_currently_installed'] = __( 'The parent theme, %1$s %2$s , is currently installed.' );
+ /* translators: 1: Theme name, 2: Theme version. */
+ $this->strings['parent_theme_install_success'] = __( 'Successfully installed the parent theme, %1$s %2$s .' );
+ /* translators: %s: Theme name. */
+ $this->strings['parent_theme_not_found'] = sprintf( __( 'The parent theme could not be found. You will need to install the parent theme, %s, before you can use this child theme.' ), '%s ' );
+ /* translators: %s: Theme error. */
+ $this->strings['current_theme_has_errors'] = __( 'The active theme has the following error: "%s".' );
+
+ if ( ! empty( $this->skin->overwrite ) ) {
+ if ( 'update-theme' === $this->skin->overwrite ) {
+ $this->strings['installing_package'] = __( 'Updating the theme…' );
+ $this->strings['process_failed'] = __( 'Theme update failed.' );
+ $this->strings['process_success'] = __( 'Theme updated successfully.' );
+ }
+
+ if ( 'downgrade-theme' === $this->skin->overwrite ) {
+ $this->strings['installing_package'] = __( 'Downgrading the theme…' );
+ $this->strings['process_failed'] = __( 'Theme downgrade failed.' );
+ $this->strings['process_success'] = __( 'Theme downgraded successfully.' );
+ }
+ }
+ }
+
+ /**
+ * Checks if a child theme is being installed and its parent also needs to be installed.
+ *
+ * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::install().
+ *
+ * @since 3.4.0
+ *
+ * @param bool $install_result
+ * @param array $hook_extra
+ * @param array $child_result
+ * @return bool
+ */
+ public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
+ // Check to see if we need to install a parent theme.
+ $theme_info = $this->theme_info();
+
+ if ( ! $theme_info->parent() ) {
+ return $install_result;
+ }
+
+ $this->skin->feedback( 'parent_theme_search' );
+
+ if ( ! $theme_info->parent()->errors() ) {
+ $this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display( 'Name' ), $theme_info->parent()->display( 'Version' ) );
+ // We already have the theme, fall through.
+ return $install_result;
+ }
+
+ // We don't have the parent theme, let's install it.
+ $api = themes_api(
+ 'theme_information',
+ array(
+ 'slug' => $theme_info->get( 'Template' ),
+ 'fields' => array(
+ 'sections' => false,
+ 'tags' => false,
+ ),
+ )
+ ); // Save on a bit of bandwidth.
+
+ if ( ! $api || is_wp_error( $api ) ) {
+ $this->skin->feedback( 'parent_theme_not_found', $theme_info->get( 'Template' ) );
+ // Don't show activate or preview actions after installation.
+ add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
+ return $install_result;
+ }
+
+ // Backup required data we're going to override:
+ $child_api = $this->skin->api;
+ $child_success_message = $this->strings['process_success'];
+
+ // Override them.
+ $this->skin->api = $api;
+
+ $this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];
+
+ $this->skin->feedback( 'parent_theme_prepare_install', $api->name, $api->version );
+
+ add_filter( 'install_theme_complete_actions', '__return_false', 999 ); // Don't show any actions after installing the theme.
+
+ // Install the parent theme.
+ $parent_result = $this->run(
+ array(
+ 'package' => $api->download_link,
+ 'destination' => get_theme_root(),
+ 'clear_destination' => false, // Do not overwrite files.
+ 'clear_working' => true,
+ )
+ );
+
+ if ( is_wp_error( $parent_result ) ) {
+ add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
+ }
+
+ // Start cleaning up after the parent's installation.
+ remove_filter( 'install_theme_complete_actions', '__return_false', 999 );
+
+ // Reset child's result and data.
+ $this->result = $child_result;
+ $this->skin->api = $child_api;
+ $this->strings['process_success'] = $child_success_message;
+
+ return $install_result;
+ }
+
+ /**
+ * Don't display the activate and preview actions to the user.
+ *
+ * Hooked to the {@see 'install_theme_complete_actions'} filter by
+ * Theme_Upgrader::check_parent_theme_filter() when installing
+ * a child theme and installing the parent theme fails.
+ *
+ * @since 3.4.0
+ *
+ * @param array $actions Preview actions.
+ * @return array
+ */
+ public function hide_activate_preview_actions( $actions ) {
+ unset( $actions['activate'], $actions['preview'] );
+ return $actions;
+ }
+
+ /**
+ * Install a theme package.
+ *
+ * @since 2.8.0
+ * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
+ *
+ * @param string $package The full local path or URI of the package.
+ * @param array $args {
+ * Optional. Other arguments for installing a theme package. Default empty array.
+ *
+ * @type bool $clear_update_cache Whether to clear the updates cache if successful.
+ * Default true.
+ * }
+ *
+ * @return bool|WP_Error True if the installation was successful, false or a WP_Error object otherwise.
+ */
+ public function install( $package, $args = array() ) {
+ $defaults = array(
+ 'clear_update_cache' => true,
+ 'overwrite_package' => false, // Do not overwrite files.
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->install_strings();
+
+ add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
+ add_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ), 10, 3 );
+
+ if ( $parsed_args['clear_update_cache'] ) {
+ // Clear cache so wp_update_themes() knows about the new theme.
+ add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
+ }
+
+ $this->run(
+ array(
+ 'package' => $package,
+ 'destination' => get_theme_root(),
+ 'clear_destination' => $parsed_args['overwrite_package'],
+ 'clear_working' => true,
+ 'hook_extra' => array(
+ 'type' => 'theme',
+ 'action' => 'install',
+ ),
+ )
+ );
+
+ remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
+ remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
+ remove_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ) );
+
+ if ( ! $this->result || is_wp_error( $this->result ) ) {
+ return $this->result;
+ }
+
+ // Refresh the Theme Update information.
+ wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
+
+ if ( $parsed_args['overwrite_package'] ) {
+ /** This action is documented in wp-admin/includes/class-plugin-upgrader.php */
+ do_action( 'upgrader_overwrote_package', $package, $this->new_theme_data, 'theme' );
+ }
+
+ return true;
+ }
+
+ /**
+ * Upgrades a theme.
+ *
+ * @since 2.8.0
+ * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
+ *
+ * @param string $theme The theme slug.
+ * @param array $args {
+ * Optional. Other arguments for upgrading a theme. Default empty array.
+ *
+ * @type bool $clear_update_cache Whether to clear the update cache if successful.
+ * Default true.
+ * }
+ * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
+ */
+ public function upgrade( $theme, $args = array() ) {
+ $defaults = array(
+ 'clear_update_cache' => true,
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->upgrade_strings();
+
+ // Is an update available?
+ $current = get_site_transient( 'update_themes' );
+ if ( ! isset( $current->response[ $theme ] ) ) {
+ $this->skin->before();
+ $this->skin->set_result( false );
+ $this->skin->error( 'up_to_date' );
+ $this->skin->after();
+ return false;
+ }
+
+ $r = $current->response[ $theme ];
+
+ add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
+ add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
+ add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
+ if ( $parsed_args['clear_update_cache'] ) {
+ // Clear cache so wp_update_themes() knows about the new theme.
+ add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
+ }
+
+ $this->run(
+ array(
+ 'package' => $r['package'],
+ 'destination' => get_theme_root( $theme ),
+ 'clear_destination' => true,
+ 'clear_working' => true,
+ 'hook_extra' => array(
+ 'theme' => $theme,
+ 'type' => 'theme',
+ 'action' => 'update',
+ 'temp_backup' => array(
+ 'slug' => $theme,
+ 'src' => get_theme_root( $theme ),
+ 'dir' => 'themes',
+ ),
+ ),
+ )
+ );
+
+ remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
+ remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
+ remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
+ remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
+
+ if ( ! $this->result || is_wp_error( $this->result ) ) {
+ return $this->result;
+ }
+
+ wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
+
+ /*
+ * Ensure any future auto-update failures trigger a failure email by removing
+ * the last failure notification from the list when themes update successfully.
+ */
+ $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
+
+ if ( isset( $past_failure_emails[ $theme ] ) ) {
+ unset( $past_failure_emails[ $theme ] );
+ update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
+ }
+
+ return true;
+ }
+
+ /**
+ * Upgrades several themes at once.
+ *
+ * @since 3.0.0
+ * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
+ *
+ * @param string[] $themes Array of the theme slugs.
+ * @param array $args {
+ * Optional. Other arguments for upgrading several themes at once. Default empty array.
+ *
+ * @type bool $clear_update_cache Whether to clear the update cache if successful.
+ * Default true.
+ * }
+ * @return array[]|false An array of results, or false if unable to connect to the filesystem.
+ */
+ public function bulk_upgrade( $themes, $args = array() ) {
+ $defaults = array(
+ 'clear_update_cache' => true,
+ );
+ $parsed_args = wp_parse_args( $args, $defaults );
+
+ $this->init();
+ $this->bulk = true;
+ $this->upgrade_strings();
+
+ $current = get_site_transient( 'update_themes' );
+
+ add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
+ add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
+ add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
+
+ $this->skin->header();
+
+ // Connect to the filesystem first.
+ $res = $this->fs_connect( array( WP_CONTENT_DIR ) );
+ if ( ! $res ) {
+ $this->skin->footer();
+ return false;
+ }
+
+ $this->skin->bulk_header();
+
+ /*
+ * Only start maintenance mode if:
+ * - running Multisite and there are one or more themes specified, OR
+ * - a theme with an update available is currently in use.
+ * @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
+ */
+ $maintenance = ( is_multisite() && ! empty( $themes ) );
+ foreach ( $themes as $theme ) {
+ $maintenance = $maintenance || get_stylesheet() === $theme || get_template() === $theme;
+ }
+ if ( $maintenance ) {
+ $this->maintenance_mode( true );
+ }
+
+ $results = array();
+
+ $this->update_count = count( $themes );
+ $this->update_current = 0;
+ foreach ( $themes as $theme ) {
+ ++$this->update_current;
+
+ $this->skin->theme_info = $this->theme_info( $theme );
+
+ if ( ! isset( $current->response[ $theme ] ) ) {
+ $this->skin->set_result( true );
+ $this->skin->before();
+ $this->skin->feedback( 'up_to_date' );
+ $this->skin->after();
+ $results[ $theme ] = true;
+ continue;
+ }
+
+ // Get the URL to the zip file.
+ $r = $current->response[ $theme ];
+
+ $result = $this->run(
+ array(
+ 'package' => $r['package'],
+ 'destination' => get_theme_root( $theme ),
+ 'clear_destination' => true,
+ 'clear_working' => true,
+ 'is_multi' => true,
+ 'hook_extra' => array(
+ 'theme' => $theme,
+ 'temp_backup' => array(
+ 'slug' => $theme,
+ 'src' => get_theme_root( $theme ),
+ 'dir' => 'themes',
+ ),
+ ),
+ )
+ );
+
+ $results[ $theme ] = $result;
+
+ // Prevent credentials auth screen from displaying multiple times.
+ if ( false === $result ) {
+ break;
+ }
+ } // End foreach $themes.
+
+ $this->maintenance_mode( false );
+
+ // Refresh the Theme Update information.
+ wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
+
+ /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
+ do_action(
+ 'upgrader_process_complete',
+ $this,
+ array(
+ 'action' => 'update',
+ 'type' => 'theme',
+ 'bulk' => true,
+ 'themes' => $themes,
+ )
+ );
+
+ $this->skin->bulk_footer();
+
+ $this->skin->footer();
+
+ // Cleanup our hooks, in case something else does an upgrade on this connection.
+ remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
+ remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
+ remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
+
+ /*
+ * Ensure any future auto-update failures trigger a failure email by removing
+ * the last failure notification from the list when themes update successfully.
+ */
+ $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
+
+ foreach ( $results as $theme => $result ) {
+ // Maintain last failure notification when themes failed to update manually.
+ if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $theme ] ) ) {
+ continue;
+ }
+
+ unset( $past_failure_emails[ $theme ] );
+ }
+
+ update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
+
+ return $results;
+ }
+
+ /**
+ * Checks that the package source contains a valid theme.
+ *
+ * Hooked to the {@see 'upgrader_source_selection'} filter by Theme_Upgrader::install().
+ *
+ * @since 3.3.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ * @global string $wp_version The WordPress version string.
+ *
+ * @param string $source The path to the downloaded package source.
+ * @return string|WP_Error The source as passed, or a WP_Error object on failure.
+ */
+ public function check_package( $source ) {
+ global $wp_filesystem, $wp_version;
+
+ $this->new_theme_data = array();
+
+ if ( is_wp_error( $source ) ) {
+ return $source;
+ }
+
+ // Check that the folder contains a valid theme.
+ $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
+ if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
+ return $source;
+ }
+
+ // A proper archive should have a style.css file in the single subdirectory.
+ if ( ! file_exists( $working_directory . 'style.css' ) ) {
+ return new WP_Error(
+ 'incompatible_archive_theme_no_style',
+ $this->strings['incompatible_archive'],
+ sprintf(
+ /* translators: %s: style.css */
+ __( 'The theme is missing the %s stylesheet.' ),
+ 'style.css
'
+ )
+ );
+ }
+
+ // All these headers are needed on Theme_Installer_Skin::do_overwrite().
+ $info = get_file_data(
+ $working_directory . 'style.css',
+ array(
+ 'Name' => 'Theme Name',
+ 'Version' => 'Version',
+ 'Author' => 'Author',
+ 'Template' => 'Template',
+ 'RequiresWP' => 'Requires at least',
+ 'RequiresPHP' => 'Requires PHP',
+ )
+ );
+
+ if ( empty( $info['Name'] ) ) {
+ return new WP_Error(
+ 'incompatible_archive_theme_no_name',
+ $this->strings['incompatible_archive'],
+ sprintf(
+ /* translators: %s: style.css */
+ __( 'The %s stylesheet does not contain a valid theme header.' ),
+ 'style.css
'
+ )
+ );
+ }
+
+ /*
+ * Parent themes must contain an index file:
+ * - classic themes require /index.php
+ * - block themes require /templates/index.html or block-templates/index.html (deprecated 5.9.0).
+ */
+ if (
+ empty( $info['Template'] ) &&
+ ! file_exists( $working_directory . 'index.php' ) &&
+ ! file_exists( $working_directory . 'templates/index.html' ) &&
+ ! file_exists( $working_directory . 'block-templates/index.html' )
+ ) {
+ return new WP_Error(
+ 'incompatible_archive_theme_no_index',
+ $this->strings['incompatible_archive'],
+ sprintf(
+ /* translators: 1: templates/index.html, 2: index.php, 3: Documentation URL, 4: Template, 5: style.css */
+ __( 'Template is missing. Standalone themes need to have a %1$s or %2$s template file. Child themes need to have a %4$s header in the %5$s stylesheet.' ),
+ 'templates/index.html
',
+ 'index.php
',
+ __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
+ 'Template
',
+ 'style.css
'
+ )
+ );
+ }
+
+ $requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
+ $requires_wp = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
+
+ if ( ! is_php_version_compatible( $requires_php ) ) {
+ $error = sprintf(
+ /* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
+ __( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
+ PHP_VERSION,
+ $requires_php
+ );
+
+ return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
+ }
+ if ( ! is_wp_version_compatible( $requires_wp ) ) {
+ $error = sprintf(
+ /* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
+ __( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
+ $wp_version,
+ $requires_wp
+ );
+
+ return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
+ }
+
+ $this->new_theme_data = $info;
+
+ return $source;
+ }
+
+ /**
+ * Turns on maintenance mode before attempting to upgrade the active theme.
+ *
+ * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
+ * Theme_Upgrader::bulk_upgrade().
+ *
+ * @since 2.8.0
+ *
+ * @param bool|WP_Error $response The installation response before the installation has started.
+ * @param array $theme Theme arguments.
+ * @return bool|WP_Error The original `$response` parameter or WP_Error.
+ */
+ public function current_before( $response, $theme ) {
+ if ( is_wp_error( $response ) ) {
+ return $response;
+ }
+
+ $theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
+
+ // Only run if active theme.
+ if ( get_stylesheet() !== $theme ) {
+ return $response;
+ }
+
+ // Change to maintenance mode. Bulk edit handles this separately.
+ if ( ! $this->bulk ) {
+ $this->maintenance_mode( true );
+ }
+
+ return $response;
+ }
+
+ /**
+ * Turns off maintenance mode after upgrading the active theme.
+ *
+ * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
+ * and Theme_Upgrader::bulk_upgrade().
+ *
+ * @since 2.8.0
+ *
+ * @param bool|WP_Error $response The installation response after the installation has finished.
+ * @param array $theme Theme arguments.
+ * @return bool|WP_Error The original `$response` parameter or WP_Error.
+ */
+ public function current_after( $response, $theme ) {
+ if ( is_wp_error( $response ) ) {
+ return $response;
+ }
+
+ $theme = isset( $theme['theme'] ) ? $theme['theme'] : '';
+
+ // Only run if active theme.
+ if ( get_stylesheet() !== $theme ) {
+ return $response;
+ }
+
+ // Ensure stylesheet name hasn't changed after the upgrade:
+ if ( get_stylesheet() === $theme && $theme !== $this->result['destination_name'] ) {
+ wp_clean_themes_cache();
+ $stylesheet = $this->result['destination_name'];
+ switch_theme( $stylesheet );
+ }
+
+ // Time to remove maintenance mode. Bulk edit handles this separately.
+ if ( ! $this->bulk ) {
+ $this->maintenance_mode( false );
+ }
+ return $response;
+ }
+
+ /**
+ * Deletes the old theme during an upgrade.
+ *
+ * Hooked to the {@see 'upgrader_clear_destination'} filter by Theme_Upgrader::upgrade()
+ * and Theme_Upgrader::bulk_upgrade().
+ *
+ * @since 2.8.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem Subclass
+ *
+ * @param bool $removed
+ * @param string $local_destination
+ * @param string $remote_destination
+ * @param array $theme
+ * @return bool
+ */
+ public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
+ global $wp_filesystem;
+
+ if ( is_wp_error( $removed ) ) {
+ return $removed; // Pass errors through.
+ }
+
+ if ( ! isset( $theme['theme'] ) ) {
+ return $removed;
+ }
+
+ $theme = $theme['theme'];
+ $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
+ if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
+ if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) ) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Gets the WP_Theme object for a theme.
+ *
+ * @since 2.8.0
+ * @since 3.0.0 The `$theme` argument was added.
+ *
+ * @param string $theme The directory name of the theme. This is optional, and if not supplied,
+ * the directory name from the last result will be used.
+ * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
+ * and the last result isn't set.
+ */
+ public function theme_info( $theme = null ) {
+ if ( empty( $theme ) ) {
+ if ( ! empty( $this->result['destination_name'] ) ) {
+ $theme = $this->result['destination_name'];
+ } else {
+ return false;
+ }
+ }
+
+ $theme = wp_get_theme( $theme );
+ $theme->cache_delete();
+
+ return $theme;
+ }
+}
diff --git a/wp-admin/includes/class-walker-category-checklist.php b/wp-admin/includes/class-walker-category-checklist.php
new file mode 100644
index 0000000..1deb3f9
--- /dev/null
+++ b/wp-admin/includes/class-walker-category-checklist.php
@@ -0,0 +1,138 @@
+ 'parent',
+ 'id' => 'term_id',
+ ); // TODO: Decouple this.
+
+ /**
+ * Starts the list before the elements are added.
+ *
+ * @see Walker:start_lvl()
+ *
+ * @since 2.5.1
+ *
+ * @param string $output Used to append additional content (passed by reference).
+ * @param int $depth Depth of category. Used for tab indentation.
+ * @param array $args An array of arguments. See {@see wp_terms_checklist()}.
+ */
+ public function start_lvl( &$output, $depth = 0, $args = array() ) {
+ $indent = str_repeat( "\t", $depth );
+ $output .= "$indent\n";
+ }
+
+ /**
+ * Ends the list of after the elements are added.
+ *
+ * @see Walker::end_lvl()
+ *
+ * @since 2.5.1
+ *
+ * @param string $output Used to append additional content (passed by reference).
+ * @param int $depth Depth of category. Used for tab indentation.
+ * @param array $args An array of arguments. See {@see wp_terms_checklist()}.
+ */
+ public function end_lvl( &$output, $depth = 0, $args = array() ) {
+ $indent = str_repeat( "\t", $depth );
+ $output .= "$indent \n";
+ }
+
+ /**
+ * Start the element output.
+ *
+ * @see Walker::start_el()
+ *
+ * @since 2.5.1
+ * @since 5.9.0 Renamed `$category` to `$data_object` and `$id` to `$current_object_id`
+ * to match parent class for PHP 8 named parameter support.
+ *
+ * @param string $output Used to append additional content (passed by reference).
+ * @param WP_Term $data_object The current term object.
+ * @param int $depth Depth of the term in reference to parents. Default 0.
+ * @param array $args An array of arguments. See {@see wp_terms_checklist()}.
+ * @param int $current_object_id Optional. ID of the current term. Default 0.
+ */
+ public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $category = $data_object;
+
+ if ( empty( $args['taxonomy'] ) ) {
+ $taxonomy = 'category';
+ } else {
+ $taxonomy = $args['taxonomy'];
+ }
+
+ if ( 'category' === $taxonomy ) {
+ $name = 'post_category';
+ } else {
+ $name = 'tax_input[' . $taxonomy . ']';
+ }
+
+ $args['popular_cats'] = ! empty( $args['popular_cats'] ) ? array_map( 'intval', $args['popular_cats'] ) : array();
+
+ $class = in_array( $category->term_id, $args['popular_cats'], true ) ? ' class="popular-category"' : '';
+
+ $args['selected_cats'] = ! empty( $args['selected_cats'] ) ? array_map( 'intval', $args['selected_cats'] ) : array();
+
+ if ( ! empty( $args['list_only'] ) ) {
+ $aria_checked = 'false';
+ $inner_class = 'category';
+
+ if ( in_array( $category->term_id, $args['selected_cats'], true ) ) {
+ $inner_class .= ' selected';
+ $aria_checked = 'true';
+ }
+
+ $output .= "\n" . '' .
+ '' .
+ /** This filter is documented in wp-includes/category-template.php */
+ esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . '
';
+ } else {
+ $is_selected = in_array( $category->term_id, $args['selected_cats'], true );
+ $is_disabled = ! empty( $args['disabled'] );
+
+ $output .= "\n " .
+ ' ' .
+ /** This filter is documented in wp-includes/category-template.php */
+ esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . ' ';
+ }
+ }
+
+ /**
+ * Ends the element output, if needed.
+ *
+ * @see Walker::end_el()
+ *
+ * @since 2.5.1
+ * @since 5.9.0 Renamed `$category` to `$data_object` to match parent class for PHP 8 named parameter support.
+ *
+ * @param string $output Used to append additional content (passed by reference).
+ * @param WP_Term $data_object The current term object.
+ * @param int $depth Depth of the term in reference to parents. Default 0.
+ * @param array $args An array of arguments. See {@see wp_terms_checklist()}.
+ */
+ public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
+ $output .= " \n";
+ }
+}
diff --git a/wp-admin/includes/class-walker-nav-menu-checklist.php b/wp-admin/includes/class-walker-nav-menu-checklist.php
new file mode 100644
index 0000000..6fc5c41
--- /dev/null
+++ b/wp-admin/includes/class-walker-nav-menu-checklist.php
@@ -0,0 +1,126 @@
+db_fields = $fields;
+ }
+ }
+
+ /**
+ * Starts the list before the elements are added.
+ *
+ * @see Walker_Nav_Menu::start_lvl()
+ *
+ * @since 3.0.0
+ *
+ * @param string $output Used to append additional content (passed by reference).
+ * @param int $depth Depth of page. Used for padding.
+ * @param stdClass $args Not used.
+ */
+ public function start_lvl( &$output, $depth = 0, $args = null ) {
+ $indent = str_repeat( "\t", $depth );
+ $output .= "\n$indent\n";
+ }
+
+ /**
+ * Ends the list of after the elements are added.
+ *
+ * @see Walker_Nav_Menu::end_lvl()
+ *
+ * @since 3.0.0
+ *
+ * @param string $output Used to append additional content (passed by reference).
+ * @param int $depth Depth of page. Used for padding.
+ * @param stdClass $args Not used.
+ */
+ public function end_lvl( &$output, $depth = 0, $args = null ) {
+ $indent = str_repeat( "\t", $depth );
+ $output .= "\n$indent ";
+ }
+
+ /**
+ * Start the element output.
+ *
+ * @see Walker_Nav_Menu::start_el()
+ *
+ * @since 3.0.0
+ * @since 5.9.0 Renamed `$item` to `$data_object` and `$id` to `$current_object_id`
+ * to match parent class for PHP 8 named parameter support.
+ *
+ * @global int $_nav_menu_placeholder
+ * @global int|string $nav_menu_selected_id
+ *
+ * @param string $output Used to append additional content (passed by reference).
+ * @param WP_Post $data_object Menu item data object.
+ * @param int $depth Depth of menu item. Used for padding.
+ * @param stdClass $args Not used.
+ * @param int $current_object_id Optional. ID of the current menu item. Default 0.
+ */
+ public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
+ global $_nav_menu_placeholder, $nav_menu_selected_id;
+
+ // Restores the more descriptive, specific name for use within this method.
+ $menu_item = $data_object;
+
+ $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? (int) $_nav_menu_placeholder - 1 : -1;
+ $possible_object_id = isset( $menu_item->post_type ) && 'nav_menu_item' === $menu_item->post_type ? $menu_item->object_id : $_nav_menu_placeholder;
+ $possible_db_id = ( ! empty( $menu_item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $menu_item->ID : 0;
+
+ $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
+
+ $output .= $indent . '';
+ $output .= '';
+
+ // Menu item hidden fields.
+ $output .= '';
+ $output .= '';
+ $output .= '';
+ $output .= '';
+ $output .= '';
+ $output .= '';
+ $output .= '';
+ $output .= '';
+ $output .= '';
+ $output .= '';
+ }
+}
diff --git a/wp-admin/includes/class-walker-nav-menu-edit.php b/wp-admin/includes/class-walker-nav-menu-edit.php
new file mode 100644
index 0000000..7cc7052
--- /dev/null
+++ b/wp-admin/includes/class-walker-nav-menu-edit.php
@@ -0,0 +1,323 @@
+ $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
+
+ ob_start();
+ $item_id = esc_attr( $menu_item->ID );
+ $removed_args = array(
+ 'action',
+ 'customlink-tab',
+ 'edit-menu-item',
+ 'menu-item',
+ 'page-tab',
+ '_wpnonce',
+ );
+
+ $original_title = false;
+
+ if ( 'taxonomy' === $menu_item->type ) {
+ $original_object = get_term( (int) $menu_item->object_id, $menu_item->object );
+ if ( $original_object && ! is_wp_error( $original_object ) ) {
+ $original_title = $original_object->name;
+ }
+ } elseif ( 'post_type' === $menu_item->type ) {
+ $original_object = get_post( $menu_item->object_id );
+ if ( $original_object ) {
+ $original_title = get_the_title( $original_object->ID );
+ }
+ } elseif ( 'post_type_archive' === $menu_item->type ) {
+ $original_object = get_post_type_object( $menu_item->object );
+ if ( $original_object ) {
+ $original_title = $original_object->labels->archives;
+ }
+ }
+
+ $classes = array(
+ 'menu-item menu-item-depth-' . $depth,
+ 'menu-item-' . esc_attr( $menu_item->object ),
+ 'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id === $_GET['edit-menu-item'] ) ? 'active' : 'inactive' ),
+ );
+
+ $title = $menu_item->title;
+
+ if ( ! empty( $menu_item->_invalid ) ) {
+ $classes[] = 'menu-item-invalid';
+ /* translators: %s: Title of an invalid menu item. */
+ $title = sprintf( __( '%s (Invalid)' ), $menu_item->title );
+ } elseif ( isset( $menu_item->post_status ) && 'draft' === $menu_item->post_status ) {
+ $classes[] = 'pending';
+ /* translators: %s: Title of a menu item in draft status. */
+ $title = sprintf( __( '%s (Pending)' ), $menu_item->title );
+ }
+
+ $title = ( ! isset( $menu_item->label ) || '' === $menu_item->label ) ? $title : $menu_item->label;
+
+ $submenu_text = '';
+ if ( 0 === $depth ) {
+ $submenu_text = 'style="display: none;"';
+ }
+
+ ?>
+ \n";
+ echo '';
+ }
+
+ /**
+ * Retrieves the list of bulk actions available for this table.
+ *
+ * The format is an associative array where each element represents either a top level option value and label, or
+ * an array representing an optgroup and its options.
+ *
+ * For a standard option, the array element key is the field value and the array element value is the field label.
+ *
+ * For an optgroup, the array element key is the label and the array element value is an associative array of
+ * options as above.
+ *
+ * Example:
+ *
+ * [
+ * 'edit' => 'Edit',
+ * 'delete' => 'Delete',
+ * 'Change State' => [
+ * 'feature' => 'Featured',
+ * 'sale' => 'On Sale',
+ * ]
+ * ]
+ *
+ * @since 3.1.0
+ * @since 5.6.0 A bulk action can now contain an array of options in order to create an optgroup.
+ *
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ return array();
+ }
+
+ /**
+ * Displays the bulk actions dropdown.
+ *
+ * @since 3.1.0
+ *
+ * @param string $which The location of the bulk actions: 'top' or 'bottom'.
+ * This is designated as optional for backward compatibility.
+ */
+ protected function bulk_actions( $which = '' ) {
+ if ( is_null( $this->_actions ) ) {
+ $this->_actions = $this->get_bulk_actions();
+
+ /**
+ * Filters the items in the bulk actions menu of the list table.
+ *
+ * The dynamic portion of the hook name, `$this->screen->id`, refers
+ * to the ID of the current screen.
+ *
+ * @since 3.1.0
+ * @since 5.6.0 A bulk action can now contain an array of options in order to create an optgroup.
+ *
+ * @param array $actions An array of the available bulk actions.
+ */
+ $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
+
+ $two = '';
+ } else {
+ $two = '2';
+ }
+
+ if ( empty( $this->_actions ) ) {
+ return;
+ }
+
+ echo '' .
+ /* translators: Hidden accessibility text. */
+ __( 'Select bulk action' ) .
+ ' ';
+ echo '\n";
+ echo '' . __( 'Bulk actions' ) . " \n";
+
+ foreach ( $this->_actions as $key => $value ) {
+ if ( is_array( $value ) ) {
+ echo "\t" . '' . "\n";
+
+ foreach ( $value as $name => $title ) {
+ $class = ( 'edit' === $name ) ? ' class="hide-if-no-js"' : '';
+
+ echo "\t\t" . '' . $title . " \n";
+ }
+ echo "\t" . " \n";
+ } else {
+ $class = ( 'edit' === $key ) ? ' class="hide-if-no-js"' : '';
+
+ echo "\t" . '' . $value . " \n";
+ }
+ }
+
+ echo " \n";
+
+ submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
+ echo "\n";
+ }
+
+ /**
+ * Gets the current action selected from the bulk actions dropdown.
+ *
+ * @since 3.1.0
+ *
+ * @return string|false The action name. False if no action was selected.
+ */
+ public function current_action() {
+ if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) ) {
+ return false;
+ }
+
+ if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] ) {
+ return $_REQUEST['action'];
+ }
+
+ return false;
+ }
+
+ /**
+ * Generates the required HTML for a list of row action links.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $actions An array of action links.
+ * @param bool $always_visible Whether the actions should be always visible.
+ * @return string The HTML for the row actions.
+ */
+ protected function row_actions( $actions, $always_visible = false ) {
+ $action_count = count( $actions );
+
+ if ( ! $action_count ) {
+ return '';
+ }
+
+ $mode = get_user_setting( 'posts_list_mode', 'list' );
+
+ if ( 'excerpt' === $mode ) {
+ $always_visible = true;
+ }
+
+ $output = '';
+
+ $i = 0;
+
+ foreach ( $actions as $action => $link ) {
+ ++$i;
+
+ $separator = ( $i < $action_count ) ? ' | ' : '';
+
+ $output .= "{$link}{$separator} ";
+ }
+
+ $output .= '
';
+
+ $output .= '' .
+ /* translators: Hidden accessibility text. */
+ __( 'Show more details' ) .
+ ' ';
+
+ return $output;
+ }
+
+ /**
+ * Displays a dropdown for filtering items in the list table by month.
+ *
+ * @since 3.1.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @global WP_Locale $wp_locale WordPress date and time locale object.
+ *
+ * @param string $post_type The post type.
+ */
+ protected function months_dropdown( $post_type ) {
+ global $wpdb, $wp_locale;
+
+ /**
+ * Filters whether to remove the 'Months' drop-down from the post list table.
+ *
+ * @since 4.2.0
+ *
+ * @param bool $disable Whether to disable the drop-down. Default false.
+ * @param string $post_type The post type.
+ */
+ if ( apply_filters( 'disable_months_dropdown', false, $post_type ) ) {
+ return;
+ }
+
+ /**
+ * Filters whether to short-circuit performing the months dropdown query.
+ *
+ * @since 5.7.0
+ *
+ * @param object[]|false $months 'Months' drop-down results. Default false.
+ * @param string $post_type The post type.
+ */
+ $months = apply_filters( 'pre_months_dropdown_query', false, $post_type );
+
+ if ( ! is_array( $months ) ) {
+ $extra_checks = "AND post_status != 'auto-draft'";
+ if ( ! isset( $_GET['post_status'] ) || 'trash' !== $_GET['post_status'] ) {
+ $extra_checks .= " AND post_status != 'trash'";
+ } elseif ( isset( $_GET['post_status'] ) ) {
+ $extra_checks = $wpdb->prepare( ' AND post_status = %s', $_GET['post_status'] );
+ }
+
+ $months = $wpdb->get_results(
+ $wpdb->prepare(
+ "SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
+ FROM $wpdb->posts
+ WHERE post_type = %s
+ $extra_checks
+ ORDER BY post_date DESC",
+ $post_type
+ )
+ );
+ }
+
+ /**
+ * Filters the 'Months' drop-down results.
+ *
+ * @since 3.7.0
+ *
+ * @param object[] $months Array of the months drop-down query results.
+ * @param string $post_type The post type.
+ */
+ $months = apply_filters( 'months_dropdown_results', $months, $post_type );
+
+ $month_count = count( $months );
+
+ if ( ! $month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) {
+ return;
+ }
+
+ $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
+ ?>
+ labels->filter_by_date; ?>
+
+ value="0">
+ year ) {
+ continue;
+ }
+
+ $month = zeroise( $arc_row->month, 2 );
+ $year = $arc_row->year;
+
+ printf(
+ "%s \n",
+ selected( $m, $year . $month, false ),
+ esc_attr( $arc_row->year . $month ),
+ /* translators: 1: Month name, 2: 4-digit year. */
+ sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
+ );
+ }
+ ?>
+
+
+
+
+ modes as $mode => $title ) {
+ $classes = array( 'view-' . $mode );
+ $aria_current = '';
+
+ if ( $current_mode === $mode ) {
+ $classes[] = 'current';
+ $aria_current = ' aria-current="page"';
+ }
+
+ printf(
+ "
" .
+ "%s " .
+ " \n",
+ esc_url( remove_query_arg( 'attachment-filter', add_query_arg( 'mode', $mode ) ) ),
+ implode( ' ', $classes ),
+ $title
+ );
+ }
+ ?>
+
+ post_password ) &&
+ current_user_can( 'read_post', $post_id )
+ )
+ ) {
+ // The user has access to the post and thus can see comments
+ } else {
+ return false;
+ }
+
+ if ( ! $approved_comments && ! $pending_comments ) {
+ // No comments at all.
+ printf(
+ '— ' .
+ '%s ',
+ __( 'No comments' )
+ );
+ } elseif ( $approved_comments && 'trash' === get_post_status( $post_id ) ) {
+ // Don't link the comment bubble for a trashed post.
+ printf(
+ '' .
+ '' .
+ '%s ' .
+ ' ',
+ $approved_comments_number,
+ $pending_comments ? $approved_phrase : $approved_only_phrase
+ );
+ } elseif ( $approved_comments ) {
+ // Link the comment bubble to approved comments.
+ printf(
+ '' .
+ '' .
+ '%s ' .
+ ' ',
+ esc_url(
+ add_query_arg(
+ array(
+ 'p' => $post_id,
+ 'comment_status' => 'approved',
+ ),
+ admin_url( 'edit-comments.php' )
+ )
+ ),
+ $approved_comments_number,
+ $pending_comments ? $approved_phrase : $approved_only_phrase
+ );
+ } else {
+ // Don't link the comment bubble when there are no approved comments.
+ printf(
+ '',
+ $approved_comments_number,
+ $pending_comments ?
+ /* translators: Hidden accessibility text. */
+ __( 'No approved comments' ) :
+ /* translators: Hidden accessibility text. */
+ __( 'No comments' )
+ );
+ }
+
+ if ( $pending_comments ) {
+ printf(
+ '' .
+ '' .
+ '%s ' .
+ ' ',
+ esc_url(
+ add_query_arg(
+ array(
+ 'p' => $post_id,
+ 'comment_status' => 'moderated',
+ ),
+ admin_url( 'edit-comments.php' )
+ )
+ ),
+ $pending_comments_number,
+ $pending_phrase
+ );
+ } else {
+ printf(
+ '' .
+ '' .
+ '%s ' .
+ ' ',
+ $pending_comments_number,
+ $approved_comments ?
+ /* translators: Hidden accessibility text. */
+ __( 'No pending comments' ) :
+ /* translators: Hidden accessibility text. */
+ __( 'No comments' )
+ );
+ }
+ }
+
+ /**
+ * Gets the current page number.
+ *
+ * @since 3.1.0
+ *
+ * @return int
+ */
+ public function get_pagenum() {
+ $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
+
+ if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) {
+ $pagenum = $this->_pagination_args['total_pages'];
+ }
+
+ return max( 1, $pagenum );
+ }
+
+ /**
+ * Gets the number of items to display on a single page.
+ *
+ * @since 3.1.0
+ *
+ * @param string $option User option name.
+ * @param int $default_value Optional. The number of items to display. Default 20.
+ * @return int
+ */
+ protected function get_items_per_page( $option, $default_value = 20 ) {
+ $per_page = (int) get_user_option( $option );
+ if ( empty( $per_page ) || $per_page < 1 ) {
+ $per_page = $default_value;
+ }
+
+ /**
+ * Filters the number of items to be displayed on each page of the list table.
+ *
+ * The dynamic hook name, `$option`, refers to the `per_page` option depending
+ * on the type of list table in use. Possible filter names include:
+ *
+ * - `edit_comments_per_page`
+ * - `sites_network_per_page`
+ * - `site_themes_network_per_page`
+ * - `themes_network_per_page'`
+ * - `users_network_per_page`
+ * - `edit_post_per_page`
+ * - `edit_page_per_page'`
+ * - `edit_{$post_type}_per_page`
+ * - `edit_post_tag_per_page`
+ * - `edit_category_per_page`
+ * - `edit_{$taxonomy}_per_page`
+ * - `site_users_network_per_page`
+ * - `users_per_page`
+ *
+ * @since 2.9.0
+ *
+ * @param int $per_page Number of items to be displayed. Default 20.
+ */
+ return (int) apply_filters( "{$option}", $per_page );
+ }
+
+ /**
+ * Displays the pagination.
+ *
+ * @since 3.1.0
+ *
+ * @param string $which
+ */
+ protected function pagination( $which ) {
+ if ( empty( $this->_pagination_args ) ) {
+ return;
+ }
+
+ $total_items = $this->_pagination_args['total_items'];
+ $total_pages = $this->_pagination_args['total_pages'];
+ $infinite_scroll = false;
+ if ( isset( $this->_pagination_args['infinite_scroll'] ) ) {
+ $infinite_scroll = $this->_pagination_args['infinite_scroll'];
+ }
+
+ if ( 'top' === $which && $total_pages > 1 ) {
+ $this->screen->render_screen_reader_content( 'heading_pagination' );
+ }
+
+ $output = '' . sprintf(
+ /* translators: %s: Number of items. */
+ _n( '%s item', '%s items', $total_items ),
+ number_format_i18n( $total_items )
+ ) . ' ';
+
+ $current = $this->get_pagenum();
+ $removable_query_args = wp_removable_query_args();
+
+ $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
+
+ $current_url = remove_query_arg( $removable_query_args, $current_url );
+
+ $page_links = array();
+
+ $total_pages_before = '';
+ $total_pages_after = ' ';
+
+ $disable_first = false;
+ $disable_last = false;
+ $disable_prev = false;
+ $disable_next = false;
+
+ if ( 1 == $current ) {
+ $disable_first = true;
+ $disable_prev = true;
+ }
+ if ( $total_pages == $current ) {
+ $disable_last = true;
+ $disable_next = true;
+ }
+
+ if ( $disable_first ) {
+ $page_links[] = '« ';
+ } else {
+ $page_links[] = sprintf(
+ "" .
+ "%s " .
+ "%s " .
+ ' ',
+ esc_url( remove_query_arg( 'paged', $current_url ) ),
+ /* translators: Hidden accessibility text. */
+ __( 'First page' ),
+ '«'
+ );
+ }
+
+ if ( $disable_prev ) {
+ $page_links[] = '‹ ';
+ } else {
+ $page_links[] = sprintf(
+ "" .
+ "%s " .
+ "%s " .
+ ' ',
+ esc_url( add_query_arg( 'paged', max( 1, $current - 1 ), $current_url ) ),
+ /* translators: Hidden accessibility text. */
+ __( 'Previous page' ),
+ '‹'
+ );
+ }
+
+ if ( 'bottom' === $which ) {
+ $html_current_page = $current;
+ $total_pages_before = sprintf(
+ '%s ' .
+ '' .
+ '',
+ /* translators: Hidden accessibility text. */
+ __( 'Current Page' )
+ );
+ } else {
+ $html_current_page = sprintf(
+ '%s ' .
+ " " .
+ "",
+ /* translators: Hidden accessibility text. */
+ __( 'Current Page' ),
+ $current,
+ strlen( $total_pages )
+ );
+ }
+
+ $html_total_pages = sprintf( "%s ", number_format_i18n( $total_pages ) );
+
+ $page_links[] = $total_pages_before . sprintf(
+ /* translators: 1: Current page, 2: Total pages. */
+ _x( '%1$s of %2$s', 'paging' ),
+ $html_current_page,
+ $html_total_pages
+ ) . $total_pages_after;
+
+ if ( $disable_next ) {
+ $page_links[] = '› ';
+ } else {
+ $page_links[] = sprintf(
+ "" .
+ "%s " .
+ "%s " .
+ ' ',
+ esc_url( add_query_arg( 'paged', min( $total_pages, $current + 1 ), $current_url ) ),
+ /* translators: Hidden accessibility text. */
+ __( 'Next page' ),
+ '›'
+ );
+ }
+
+ if ( $disable_last ) {
+ $page_links[] = '» ';
+ } else {
+ $page_links[] = sprintf(
+ "" .
+ "%s " .
+ "%s " .
+ ' ',
+ esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
+ /* translators: Hidden accessibility text. */
+ __( 'Last page' ),
+ '»'
+ );
+ }
+
+ $pagination_links_class = 'pagination-links';
+ if ( ! empty( $infinite_scroll ) ) {
+ $pagination_links_class .= ' hide-if-js';
+ }
+ $output .= "\n';
+
+ if ( $total_pages ) {
+ $page_class = $total_pages < 2 ? ' one-page' : '';
+ } else {
+ $page_class = ' no-pages';
+ }
+ $this->_pagination = "$output
";
+
+ echo $this->_pagination;
+ }
+
+ /**
+ * Gets a list of columns.
+ *
+ * The format is:
+ * - `'internal-name' => 'Title'`
+ *
+ * @since 3.1.0
+ * @abstract
+ *
+ * @return array
+ */
+ public function get_columns() {
+ die( 'function WP_List_Table::get_columns() must be overridden in a subclass.' );
+ }
+
+ /**
+ * Gets a list of sortable columns.
+ *
+ * The format is:
+ * - `'internal-name' => 'orderby'`
+ * - `'internal-name' => array( 'orderby', bool, 'abbr', 'orderby-text', 'initially-sorted-column-order' )` -
+ * - `'internal-name' => array( 'orderby', 'asc' )` - The second element sets the initial sorting order.
+ * - `'internal-name' => array( 'orderby', true )` - The second element makes the initial order descending.
+ *
+ * In the second format, passing true as second parameter will make the initial
+ * sorting order be descending. Following parameters add a short column name to
+ * be used as 'abbr' attribute, a translatable string for the current sorting,
+ * and the initial order for the initial sorted column, 'asc' or 'desc' (default: false).
+ *
+ * @since 3.1.0
+ * @since 6.3.0 Added 'abbr', 'orderby-text' and 'initially-sorted-column-order'.
+ *
+ * @return array
+ */
+ protected function get_sortable_columns() {
+ return array();
+ }
+
+ /**
+ * Gets the name of the default primary column.
+ *
+ * @since 4.3.0
+ *
+ * @return string Name of the default primary column, in this case, an empty string.
+ */
+ protected function get_default_primary_column_name() {
+ $columns = $this->get_columns();
+ $column = '';
+
+ if ( empty( $columns ) ) {
+ return $column;
+ }
+
+ /*
+ * We need a primary defined so responsive views show something,
+ * so let's fall back to the first non-checkbox column.
+ */
+ foreach ( $columns as $col => $column_name ) {
+ if ( 'cb' === $col ) {
+ continue;
+ }
+
+ $column = $col;
+ break;
+ }
+
+ return $column;
+ }
+
+ /**
+ * Gets the name of the primary column.
+ *
+ * Public wrapper for WP_List_Table::get_default_primary_column_name().
+ *
+ * @since 4.4.0
+ *
+ * @return string Name of the default primary column.
+ */
+ public function get_primary_column() {
+ return $this->get_primary_column_name();
+ }
+
+ /**
+ * Gets the name of the primary column.
+ *
+ * @since 4.3.0
+ *
+ * @return string The name of the primary column.
+ */
+ protected function get_primary_column_name() {
+ $columns = get_column_headers( $this->screen );
+ $default = $this->get_default_primary_column_name();
+
+ /*
+ * If the primary column doesn't exist,
+ * fall back to the first non-checkbox column.
+ */
+ if ( ! isset( $columns[ $default ] ) ) {
+ $default = self::get_default_primary_column_name();
+ }
+
+ /**
+ * Filters the name of the primary column for the current list table.
+ *
+ * @since 4.3.0
+ *
+ * @param string $default Column name default for the specific list table, e.g. 'name'.
+ * @param string $context Screen ID for specific list table, e.g. 'plugins'.
+ */
+ $column = apply_filters( 'list_table_primary_column', $default, $this->screen->id );
+
+ if ( empty( $column ) || ! isset( $columns[ $column ] ) ) {
+ $column = $default;
+ }
+
+ return $column;
+ }
+
+ /**
+ * Gets a list of all, hidden, and sortable columns, with filter applied.
+ *
+ * @since 3.1.0
+ *
+ * @return array
+ */
+ protected function get_column_info() {
+ // $_column_headers is already set / cached.
+ if (
+ isset( $this->_column_headers ) &&
+ is_array( $this->_column_headers )
+ ) {
+ /*
+ * Backward compatibility for `$_column_headers` format prior to WordPress 4.3.
+ *
+ * In WordPress 4.3 the primary column name was added as a fourth item in the
+ * column headers property. This ensures the primary column name is included
+ * in plugins setting the property directly in the three item format.
+ */
+ if ( 4 === count( $this->_column_headers ) ) {
+ return $this->_column_headers;
+ }
+
+ $column_headers = array( array(), array(), array(), $this->get_primary_column_name() );
+ foreach ( $this->_column_headers as $key => $value ) {
+ $column_headers[ $key ] = $value;
+ }
+
+ $this->_column_headers = $column_headers;
+
+ return $this->_column_headers;
+ }
+
+ $columns = get_column_headers( $this->screen );
+ $hidden = get_hidden_columns( $this->screen );
+
+ $sortable_columns = $this->get_sortable_columns();
+ /**
+ * Filters the list table sortable columns for a specific screen.
+ *
+ * The dynamic portion of the hook name, `$this->screen->id`, refers
+ * to the ID of the current screen.
+ *
+ * @since 3.1.0
+ *
+ * @param array $sortable_columns An array of sortable columns.
+ */
+ $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns );
+
+ $sortable = array();
+ foreach ( $_sortable as $id => $data ) {
+ if ( empty( $data ) ) {
+ continue;
+ }
+
+ $data = (array) $data;
+ // Descending initial sorting.
+ if ( ! isset( $data[1] ) ) {
+ $data[1] = false;
+ }
+ // Current sorting translatable string.
+ if ( ! isset( $data[2] ) ) {
+ $data[2] = '';
+ }
+ // Initial view sorted column and asc/desc order, default: false.
+ if ( ! isset( $data[3] ) ) {
+ $data[3] = false;
+ }
+ // Initial order for the initial sorted column, default: false.
+ if ( ! isset( $data[4] ) ) {
+ $data[4] = false;
+ }
+
+ $sortable[ $id ] = $data;
+ }
+
+ $primary = $this->get_primary_column_name();
+ $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
+
+ return $this->_column_headers;
+ }
+
+ /**
+ * Returns the number of visible columns.
+ *
+ * @since 3.1.0
+ *
+ * @return int
+ */
+ public function get_column_count() {
+ list ( $columns, $hidden ) = $this->get_column_info();
+ $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) );
+ return count( $columns ) - count( $hidden );
+ }
+
+ /**
+ * Prints column headers, accounting for hidden and sortable columns.
+ *
+ * @since 3.1.0
+ *
+ * @param bool $with_id Whether to set the ID attribute or not
+ */
+ public function print_column_headers( $with_id = true ) {
+ list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
+
+ $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
+ $current_url = remove_query_arg( 'paged', $current_url );
+
+ // When users click on a column header to sort by other columns.
+ if ( isset( $_GET['orderby'] ) ) {
+ $current_orderby = $_GET['orderby'];
+ // In the initial view there's no orderby parameter.
+ } else {
+ $current_orderby = '';
+ }
+
+ // Not in the initial view and descending order.
+ if ( isset( $_GET['order'] ) && 'desc' === $_GET['order'] ) {
+ $current_order = 'desc';
+ } else {
+ // The initial view is not always 'asc', we'll take care of this below.
+ $current_order = 'asc';
+ }
+
+ if ( ! empty( $columns['cb'] ) ) {
+ static $cb_counter = 1;
+ $columns['cb'] = '
+ ' .
+ '' .
+ /* translators: Hidden accessibility text. */
+ __( 'Select All' ) .
+ ' ' .
+ ' ';
+ ++$cb_counter;
+ }
+
+ foreach ( $columns as $column_key => $column_display_name ) {
+ $class = array( 'manage-column', "column-$column_key" );
+ $aria_sort_attr = '';
+ $abbr_attr = '';
+ $order_text = '';
+
+ if ( in_array( $column_key, $hidden, true ) ) {
+ $class[] = 'hidden';
+ }
+
+ if ( 'cb' === $column_key ) {
+ $class[] = 'check-column';
+ } elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ), true ) ) {
+ $class[] = 'num';
+ }
+
+ if ( $column_key === $primary ) {
+ $class[] = 'column-primary';
+ }
+
+ if ( isset( $sortable[ $column_key ] ) ) {
+ $orderby = isset( $sortable[ $column_key ][0] ) ? $sortable[ $column_key ][0] : '';
+ $desc_first = isset( $sortable[ $column_key ][1] ) ? $sortable[ $column_key ][1] : false;
+ $abbr = isset( $sortable[ $column_key ][2] ) ? $sortable[ $column_key ][2] : '';
+ $orderby_text = isset( $sortable[ $column_key ][3] ) ? $sortable[ $column_key ][3] : '';
+ $initial_order = isset( $sortable[ $column_key ][4] ) ? $sortable[ $column_key ][4] : '';
+
+ /*
+ * We're in the initial view and there's no $_GET['orderby'] then check if the
+ * initial sorting information is set in the sortable columns and use that.
+ */
+ if ( '' === $current_orderby && $initial_order ) {
+ // Use the initially sorted column $orderby as current orderby.
+ $current_orderby = $orderby;
+ // Use the initially sorted column asc/desc order as initial order.
+ $current_order = $initial_order;
+ }
+
+ /*
+ * True in the initial view when an initial orderby is set via get_sortable_columns()
+ * and true in the sorted views when the actual $_GET['orderby'] is equal to $orderby.
+ */
+ if ( $current_orderby === $orderby ) {
+ // The sorted column. The `aria-sort` attribute must be set only on the sorted column.
+ if ( 'asc' === $current_order ) {
+ $order = 'desc';
+ $aria_sort_attr = ' aria-sort="ascending"';
+ } else {
+ $order = 'asc';
+ $aria_sort_attr = ' aria-sort="descending"';
+ }
+
+ $class[] = 'sorted';
+ $class[] = $current_order;
+ } else {
+ // The other sortable columns.
+ $order = strtolower( $desc_first );
+
+ if ( ! in_array( $order, array( 'desc', 'asc' ), true ) ) {
+ $order = $desc_first ? 'desc' : 'asc';
+ }
+
+ $class[] = 'sortable';
+ $class[] = 'desc' === $order ? 'asc' : 'desc';
+
+ /* translators: Hidden accessibility text. */
+ $asc_text = __( 'Sort ascending.' );
+ /* translators: Hidden accessibility text. */
+ $desc_text = __( 'Sort descending.' );
+ $order_text = 'asc' === $order ? $asc_text : $desc_text;
+ }
+
+ if ( '' !== $order_text ) {
+ $order_text = ' ' . $order_text . ' ';
+ }
+
+ // Print an 'abbr' attribute if a value is provided via get_sortable_columns().
+ $abbr_attr = $abbr ? ' abbr="' . esc_attr( $abbr ) . '"' : '';
+
+ $column_display_name = sprintf(
+ '' .
+ '%2$s ' .
+ '' .
+ ' ' .
+ ' ' .
+ ' ' .
+ '%3$s' .
+ ' ',
+ esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ),
+ $column_display_name,
+ $order_text
+ );
+ }
+
+ $tag = ( 'cb' === $column_key ) ? 'td' : 'th';
+ $scope = ( 'th' === $tag ) ? 'scope="col"' : '';
+ $id = $with_id ? "id='$column_key'" : '';
+
+ if ( ! empty( $class ) ) {
+ $class = "class='" . implode( ' ', $class ) . "'";
+ }
+
+ echo "<$tag $scope $id $class $aria_sort_attr $abbr_attr>$column_display_name$tag>";
+ }
+ }
+
+ /**
+ * Print a table description with information about current sorting and order.
+ *
+ * For the table initial view, information about initial orderby and order
+ * should be provided via get_sortable_columns().
+ *
+ * @since 6.3.0
+ * @access public
+ */
+ public function print_table_description() {
+ list( $columns, $hidden, $sortable ) = $this->get_column_info();
+
+ if ( empty( $sortable ) ) {
+ return;
+ }
+
+ // When users click on a column header to sort by other columns.
+ if ( isset( $_GET['orderby'] ) ) {
+ $current_orderby = $_GET['orderby'];
+ // In the initial view there's no orderby parameter.
+ } else {
+ $current_orderby = '';
+ }
+
+ // Not in the initial view and descending order.
+ if ( isset( $_GET['order'] ) && 'desc' === $_GET['order'] ) {
+ $current_order = 'desc';
+ } else {
+ // The initial view is not always 'asc', we'll take care of this below.
+ $current_order = 'asc';
+ }
+
+ foreach ( array_keys( $columns ) as $column_key ) {
+
+ if ( isset( $sortable[ $column_key ] ) ) {
+ $orderby = isset( $sortable[ $column_key ][0] ) ? $sortable[ $column_key ][0] : '';
+ $desc_first = isset( $sortable[ $column_key ][1] ) ? $sortable[ $column_key ][1] : false;
+ $abbr = isset( $sortable[ $column_key ][2] ) ? $sortable[ $column_key ][2] : '';
+ $orderby_text = isset( $sortable[ $column_key ][3] ) ? $sortable[ $column_key ][3] : '';
+ $initial_order = isset( $sortable[ $column_key ][4] ) ? $sortable[ $column_key ][4] : '';
+
+ if ( ! is_string( $orderby_text ) || '' === $orderby_text ) {
+ return;
+ }
+ /*
+ * We're in the initial view and there's no $_GET['orderby'] then check if the
+ * initial sorting information is set in the sortable columns and use that.
+ */
+ if ( '' === $current_orderby && $initial_order ) {
+ // Use the initially sorted column $orderby as current orderby.
+ $current_orderby = $orderby;
+ // Use the initially sorted column asc/desc order as initial order.
+ $current_order = $initial_order;
+ }
+
+ /*
+ * True in the initial view when an initial orderby is set via get_sortable_columns()
+ * and true in the sorted views when the actual $_GET['orderby'] is equal to $orderby.
+ */
+ if ( $current_orderby === $orderby ) {
+ /* translators: Hidden accessibility text. */
+ $asc_text = __( 'Ascending.' );
+ /* translators: Hidden accessibility text. */
+ $desc_text = __( 'Descending.' );
+ $order_text = 'asc' === $current_order ? $asc_text : $desc_text;
+ echo '' . $orderby_text . ' ' . $order_text . ' ';
+
+ return;
+ }
+ }
+ }
+ }
+
+ /**
+ * Displays the table.
+ *
+ * @since 3.1.0
+ */
+ public function display() {
+ $singular = $this->_args['singular'];
+
+ $this->display_tablenav( 'top' );
+
+ $this->screen->render_screen_reader_content( 'heading_list' );
+ ?>
+
+ print_table_description(); ?>
+
+
+ print_column_headers(); ?>
+
+
+
+
+ >
+ display_rows_or_placeholder(); ?>
+
+
+
+
+ print_column_headers( false ); ?>
+
+
+
+
+ display_tablenav( 'bottom' );
+ }
+
+ /**
+ * Gets a list of CSS classes for the WP_List_Table table tag.
+ *
+ * @since 3.1.0
+ *
+ * @return string[] Array of CSS classes for the table tag.
+ */
+ protected function get_table_classes() {
+ $mode = get_user_setting( 'posts_list_mode', 'list' );
+
+ $mode_class = esc_attr( 'table-view-' . $mode );
+
+ return array( 'widefat', 'fixed', 'striped', $mode_class, $this->_args['plural'] );
+ }
+
+ /**
+ * Generates the table navigation above or below the table
+ *
+ * @since 3.1.0
+ * @param string $which
+ */
+ protected function display_tablenav( $which ) {
+ if ( 'top' === $which ) {
+ wp_nonce_field( 'bulk-' . $this->_args['plural'] );
+ }
+ ?>
+
+
+ has_items() ) : ?>
+
+ bulk_actions( $which ); ?>
+
+ extra_tablenav( $which );
+ $this->pagination( $which );
+ ?>
+
+
+
+ has_items() ) {
+ $this->display_rows();
+ } else {
+ echo '';
+ $this->no_items();
+ echo ' ';
+ }
+ }
+
+ /**
+ * Generates the table rows.
+ *
+ * @since 3.1.0
+ */
+ public function display_rows() {
+ foreach ( $this->items as $item ) {
+ $this->single_row( $item );
+ }
+ }
+
+ /**
+ * Generates content for a single row of the table.
+ *
+ * @since 3.1.0
+ *
+ * @param object|array $item The current item
+ */
+ public function single_row( $item ) {
+ echo '';
+ $this->single_row_columns( $item );
+ echo ' ';
+ }
+
+ /**
+ * @param object|array $item
+ * @param string $column_name
+ */
+ protected function column_default( $item, $column_name ) {}
+
+ /**
+ * @param object|array $item
+ */
+ protected function column_cb( $item ) {}
+
+ /**
+ * Generates the columns for a single row of the table.
+ *
+ * @since 3.1.0
+ *
+ * @param object|array $item The current item.
+ */
+ protected function single_row_columns( $item ) {
+ list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
+
+ foreach ( $columns as $column_name => $column_display_name ) {
+ $classes = "$column_name column-$column_name";
+ if ( $primary === $column_name ) {
+ $classes .= ' has-row-actions column-primary';
+ }
+
+ if ( in_array( $column_name, $hidden, true ) ) {
+ $classes .= ' hidden';
+ }
+
+ /*
+ * Comments column uses HTML in the display name with screen reader text.
+ * Strip tags to get closer to a user-friendly string.
+ */
+ $data = 'data-colname="' . esc_attr( wp_strip_all_tags( $column_display_name ) ) . '"';
+
+ $attributes = "class='$classes' $data";
+
+ if ( 'cb' === $column_name ) {
+ echo ' ';
+ echo $this->column_cb( $item );
+ echo ' ';
+ } elseif ( method_exists( $this, '_column_' . $column_name ) ) {
+ echo call_user_func(
+ array( $this, '_column_' . $column_name ),
+ $item,
+ $classes,
+ $data,
+ $primary
+ );
+ } elseif ( method_exists( $this, 'column_' . $column_name ) ) {
+ echo "";
+ echo call_user_func( array( $this, 'column_' . $column_name ), $item );
+ echo $this->handle_row_actions( $item, $column_name, $primary );
+ echo ' ';
+ } else {
+ echo "";
+ echo $this->column_default( $item, $column_name );
+ echo $this->handle_row_actions( $item, $column_name, $primary );
+ echo ' ';
+ }
+ }
+ }
+
+ /**
+ * Generates and display row actions links for the list table.
+ *
+ * @since 4.3.0
+ *
+ * @param object|array $item The item being acted upon.
+ * @param string $column_name Current column name.
+ * @param string $primary Primary column name.
+ * @return string The row actions HTML, or an empty string
+ * if the current column is not the primary column.
+ */
+ protected function handle_row_actions( $item, $column_name, $primary ) {
+ return $column_name === $primary ? '' .
+ /* translators: Hidden accessibility text. */
+ __( 'Show more details' ) .
+ ' ' : '';
+ }
+
+ /**
+ * Handles an incoming ajax request (called from admin-ajax.php)
+ *
+ * @since 3.1.0
+ */
+ public function ajax_response() {
+ $this->prepare_items();
+
+ ob_start();
+ if ( ! empty( $_REQUEST['no_placeholder'] ) ) {
+ $this->display_rows();
+ } else {
+ $this->display_rows_or_placeholder();
+ }
+
+ $rows = ob_get_clean();
+
+ $response = array( 'rows' => $rows );
+
+ if ( isset( $this->_pagination_args['total_items'] ) ) {
+ $response['total_items_i18n'] = sprintf(
+ /* translators: Number of items. */
+ _n( '%s item', '%s items', $this->_pagination_args['total_items'] ),
+ number_format_i18n( $this->_pagination_args['total_items'] )
+ );
+ }
+ if ( isset( $this->_pagination_args['total_pages'] ) ) {
+ $response['total_pages'] = $this->_pagination_args['total_pages'];
+ $response['total_pages_i18n'] = number_format_i18n( $this->_pagination_args['total_pages'] );
+ }
+
+ die( wp_json_encode( $response ) );
+ }
+
+ /**
+ * Sends required variables to JavaScript land.
+ *
+ * @since 3.1.0
+ */
+ public function _js_vars() {
+ $args = array(
+ 'class' => get_class( $this ),
+ 'screen' => array(
+ 'id' => $this->screen->id,
+ 'base' => $this->screen->base,
+ ),
+ );
+
+ printf( "\n", wp_json_encode( $args ) );
+ }
+}
diff --git a/wp-admin/includes/class-wp-media-list-table.php b/wp-admin/includes/class-wp-media-list-table.php
new file mode 100644
index 0000000..02362e3
--- /dev/null
+++ b/wp-admin/includes/class-wp-media-list-table.php
@@ -0,0 +1,891 @@
+detached = ( isset( $_REQUEST['attachment-filter'] ) && 'detached' === $_REQUEST['attachment-filter'] );
+
+ $this->modes = array(
+ 'list' => __( 'List view' ),
+ 'grid' => __( 'Grid view' ),
+ );
+
+ parent::__construct(
+ array(
+ 'plural' => 'media',
+ 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
+ )
+ );
+ }
+
+ /**
+ * @return bool
+ */
+ public function ajax_user_can() {
+ return current_user_can( 'upload_files' );
+ }
+
+ /**
+ * @global string $mode List table view mode.
+ * @global WP_Query $wp_query WordPress Query object.
+ * @global array $post_mime_types
+ * @global array $avail_post_mime_types
+ */
+ public function prepare_items() {
+ global $mode, $wp_query, $post_mime_types, $avail_post_mime_types;
+
+ $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
+
+ /*
+ * Exclude attachments scheduled for deletion in the next two hours
+ * if they are for zip packages for interrupted or failed updates.
+ * See File_Upload_Upgrader class.
+ */
+ $not_in = array();
+
+ $crons = _get_cron_array();
+
+ if ( is_array( $crons ) ) {
+ foreach ( $crons as $cron ) {
+ if ( isset( $cron['upgrader_scheduled_cleanup'] ) ) {
+ $details = reset( $cron['upgrader_scheduled_cleanup'] );
+
+ if ( ! empty( $details['args'][0] ) ) {
+ $not_in[] = (int) $details['args'][0];
+ }
+ }
+ }
+ }
+
+ if ( ! empty( $_REQUEST['post__not_in'] ) && is_array( $_REQUEST['post__not_in'] ) ) {
+ $not_in = array_merge( array_values( $_REQUEST['post__not_in'] ), $not_in );
+ }
+
+ if ( ! empty( $not_in ) ) {
+ $_REQUEST['post__not_in'] = $not_in;
+ }
+
+ list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
+
+ $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' === $_REQUEST['attachment-filter'];
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $wp_query->found_posts,
+ 'total_pages' => $wp_query->max_num_pages,
+ 'per_page' => $wp_query->query_vars['posts_per_page'],
+ )
+ );
+ if ( $wp_query->posts ) {
+ update_post_thumbnail_cache( $wp_query );
+ update_post_parent_caches( $wp_query->posts );
+ }
+ }
+
+ /**
+ * @global array $post_mime_types
+ * @global array $avail_post_mime_types
+ * @return array
+ */
+ protected function get_views() {
+ global $post_mime_types, $avail_post_mime_types;
+
+ $type_links = array();
+
+ $filter = empty( $_GET['attachment-filter'] ) ? '' : $_GET['attachment-filter'];
+
+ $type_links['all'] = sprintf(
+ '%s ',
+ selected( $filter, true, false ),
+ __( 'All media items' )
+ );
+
+ foreach ( $post_mime_types as $mime_type => $label ) {
+ if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) {
+ continue;
+ }
+
+ $selected = selected(
+ $filter && str_starts_with( $filter, 'post_mime_type:' ) &&
+ wp_match_mime_types( $mime_type, str_replace( 'post_mime_type:', '', $filter ) ),
+ true,
+ false
+ );
+
+ $type_links[ $mime_type ] = sprintf(
+ '%s ',
+ esc_attr( $mime_type ),
+ $selected,
+ $label[0]
+ );
+ }
+
+ $type_links['detached'] = 'detached ? ' selected="selected"' : '' ) . '>' . _x( 'Unattached', 'media items' ) . ' ';
+
+ $type_links['mine'] = sprintf(
+ '%s ',
+ selected( 'mine' === $filter, true, false ),
+ _x( 'Mine', 'media items' )
+ );
+
+ if ( $this->is_trash || ( defined( 'MEDIA_TRASH' ) && MEDIA_TRASH ) ) {
+ $type_links['trash'] = sprintf(
+ '%s ',
+ selected( 'trash' === $filter, true, false ),
+ _x( 'Trash', 'attachment filter' )
+ );
+ }
+
+ return $type_links;
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ $actions = array();
+
+ if ( MEDIA_TRASH ) {
+ if ( $this->is_trash ) {
+ $actions['untrash'] = __( 'Restore' );
+ $actions['delete'] = __( 'Delete permanently' );
+ } else {
+ $actions['trash'] = __( 'Move to Trash' );
+ }
+ } else {
+ $actions['delete'] = __( 'Delete permanently' );
+ }
+
+ if ( $this->detached ) {
+ $actions['attach'] = __( 'Attach' );
+ }
+
+ return $actions;
+ }
+
+ /**
+ * @param string $which
+ */
+ protected function extra_tablenav( $which ) {
+ if ( 'bar' !== $which ) {
+ return;
+ }
+ ?>
+
+ is_trash ) {
+ $this->months_dropdown( 'attachment' );
+ }
+
+ /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
+ do_action( 'restrict_manage_posts', $this->screen->post_type, $which );
+
+ submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
+
+ if ( $this->is_trash && $this->has_items()
+ && current_user_can( 'edit_others_posts' )
+ ) {
+ submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
+ }
+ ?>
+
+ is_trash ) {
+ _e( 'No media files found in Trash.' );
+ } else {
+ _e( 'No media files found.' );
+ }
+ }
+
+ /**
+ * Overrides parent views to use the filter bar display.
+ *
+ * @global string $mode List table view mode.
+ */
+ public function views() {
+ global $mode;
+
+ $views = $this->get_views();
+
+ $this->screen->render_screen_reader_content( 'heading_views' );
+ ?>
+
+
+ view_switcher( $mode ); ?>
+
+
+
+
+
+ $view ) {
+ echo "\t$view\n";
+ }
+ }
+ ?>
+
+
+ extra_tablenav( 'bar' );
+
+ /** This filter is documented in wp-admin/includes/class-wp-list-table.php */
+ $views = apply_filters( "views_{$this->screen->id}", array() );
+
+ // Back compat for pre-4.0 view links.
+ if ( ! empty( $views ) ) {
+ echo '
';
+ foreach ( $views as $class => $view ) {
+ echo "$view ";
+ }
+ echo ' ';
+ }
+ ?>
+
+
+
+
+ ';
+ /* translators: Column name. */
+ $posts_columns['title'] = _x( 'File', 'column name' );
+ $posts_columns['author'] = __( 'Author' );
+
+ $taxonomies = get_taxonomies_for_attachments( 'objects' );
+ $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
+
+ /**
+ * Filters the taxonomy columns for attachments in the Media list table.
+ *
+ * @since 3.5.0
+ *
+ * @param string[] $taxonomies An array of registered taxonomy names to show for attachments.
+ * @param string $post_type The post type. Default 'attachment'.
+ */
+ $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
+ $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
+
+ foreach ( $taxonomies as $taxonomy ) {
+ if ( 'category' === $taxonomy ) {
+ $column_key = 'categories';
+ } elseif ( 'post_tag' === $taxonomy ) {
+ $column_key = 'tags';
+ } else {
+ $column_key = 'taxonomy-' . $taxonomy;
+ }
+
+ $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
+ }
+
+ /* translators: Column name. */
+ if ( ! $this->detached ) {
+ $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
+
+ if ( post_type_supports( 'attachment', 'comments' ) ) {
+ $posts_columns['comments'] = sprintf(
+ '%2$s ',
+ esc_attr__( 'Comments' ),
+ /* translators: Hidden accessibility text. */
+ __( 'Comments' )
+ );
+ }
+ }
+
+ /* translators: Column name. */
+ $posts_columns['date'] = _x( 'Date', 'column name' );
+
+ /**
+ * Filters the Media list table columns.
+ *
+ * @since 2.5.0
+ *
+ * @param string[] $posts_columns An array of columns displayed in the Media list table.
+ * @param bool $detached Whether the list table contains media not attached
+ * to any posts. Default true.
+ */
+ return apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_sortable_columns() {
+ return array(
+ 'title' => array( 'title', false, _x( 'File', 'column name' ), __( 'Table ordered by File Name.' ) ),
+ 'author' => array( 'author', false, __( 'Author' ), __( 'Table ordered by Author.' ) ),
+ 'parent' => array( 'parent', false, _x( 'Uploaded to', 'column name' ), __( 'Table ordered by Uploaded To.' ) ),
+ 'comments' => array( 'comment_count', __( 'Comments' ), false, __( 'Table ordered by Comments.' ) ),
+ 'date' => array( 'date', true, __( 'Date' ), __( 'Table ordered by Date.' ), 'desc' ),
+ );
+ }
+
+ /**
+ * Handles the checkbox column output.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Post $item The current WP_Post object.
+ */
+ public function column_cb( $item ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $post = $item;
+
+ if ( current_user_can( 'edit_post', $post->ID ) ) {
+ ?>
+
+
+
+
+
+
+ post_mime_type );
+
+ $attachment_id = $post->ID;
+
+ if ( has_post_thumbnail( $post ) ) {
+ $thumbnail_id = get_post_thumbnail_id( $post );
+
+ if ( ! empty( $thumbnail_id ) ) {
+ $attachment_id = $thumbnail_id;
+ }
+ }
+
+ $title = _draft_or_post_title();
+ $thumb = wp_get_attachment_image( $attachment_id, array( 60, 60 ), true, array( 'alt' => '' ) );
+ $link_start = '';
+ $link_end = '';
+
+ if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
+ $link_start = sprintf(
+ '',
+ get_edit_post_link( $post->ID ),
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( '“%s” (Edit)' ), $title ) )
+ );
+ $link_end = ' ';
+ }
+
+ $class = $thumb ? ' class="has-media-icon"' : '';
+ ?>
+ >
+
+
+
+
+
+
+
+
+ ID );
+ echo esc_html( wp_basename( $file ) );
+ ?>
+
+ %s',
+ esc_url( add_query_arg( array( 'author' => get_the_author_meta( 'ID' ) ), 'upload.php' ) ),
+ get_the_author()
+ );
+ }
+
+ /**
+ * Handles the description column output.
+ *
+ * @since 4.3.0
+ * @deprecated 6.2.0
+ *
+ * @param WP_Post $post The current WP_Post object.
+ */
+ public function column_desc( $post ) {
+ _deprecated_function( __METHOD__, '6.2.0' );
+
+ echo has_excerpt() ? $post->post_excerpt : '';
+ }
+
+ /**
+ * Handles the date column output.
+ *
+ * @since 4.3.0
+ *
+ * @param WP_Post $post The current WP_Post object.
+ */
+ public function column_date( $post ) {
+ if ( '0000-00-00 00:00:00' === $post->post_date ) {
+ $h_time = __( 'Unpublished' );
+ } else {
+ $time = get_post_timestamp( $post );
+ $time_diff = time() - $time;
+
+ if ( $time && $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
+ /* translators: %s: Human-readable time difference. */
+ $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
+ } else {
+ $h_time = get_the_time( __( 'Y/m/d' ), $post );
+ }
+ }
+
+ /**
+ * Filters the published time of an attachment displayed in the Media list table.
+ *
+ * @since 6.0.0
+ *
+ * @param string $h_time The published time.
+ * @param WP_Post $post Attachment object.
+ * @param string $column_name The column name.
+ */
+ echo apply_filters( 'media_date_column_time', $h_time, $post, 'date' );
+ }
+
+ /**
+ * Handles the parent column output.
+ *
+ * @since 4.3.0
+ *
+ * @param WP_Post $post The current WP_Post object.
+ */
+ public function column_parent( $post ) {
+ $user_can_edit = current_user_can( 'edit_post', $post->ID );
+
+ if ( $post->post_parent > 0 ) {
+ $parent = get_post( $post->post_parent );
+ } else {
+ $parent = false;
+ }
+
+ if ( $parent ) {
+ $title = _draft_or_post_title( $post->post_parent );
+ $parent_type = get_post_type_object( $parent->post_type );
+
+ if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) {
+ printf( '%s ', get_edit_post_link( $post->post_parent ), $title );
+ } elseif ( $parent_type && current_user_can( 'read_post', $post->post_parent ) ) {
+ printf( '%s ', $title );
+ } else {
+ _e( '(Private post)' );
+ }
+
+ if ( $user_can_edit ) :
+ $detach_url = add_query_arg(
+ array(
+ 'parent_post_id' => $post->post_parent,
+ 'media[]' => $post->ID,
+ '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] ),
+ ),
+ 'upload.php'
+ );
+ printf(
+ '%s ',
+ $detach_url,
+ /* translators: %s: Title of the post the attachment is attached to. */
+ esc_attr( sprintf( __( 'Detach from “%s”' ), $title ) ),
+ __( 'Detach' )
+ );
+ endif;
+ } else {
+ _e( '(Unattached)' );
+ ?>
+ post_parent );
+ printf(
+ '%s ',
+ $post->ID,
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'Attach “%s” to existing content' ), $title ) ),
+ __( 'Attach' )
+ );
+ }
+ }
+ }
+
+ /**
+ * Handles the comments column output.
+ *
+ * @since 4.3.0
+ *
+ * @param WP_Post $post The current WP_Post object.
+ */
+ public function column_comments( $post ) {
+ echo '';
+
+ if ( isset( $this->comment_pending_count[ $post->ID ] ) ) {
+ $pending_comments = $this->comment_pending_count[ $post->ID ];
+ } else {
+ $pending_comments = get_pending_comments_num( $post->ID );
+ }
+
+ $this->comments_bubble( $post->ID, $pending_comments );
+
+ echo '
';
+ }
+
+ /**
+ * Handles output for the default column.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Post $item The current WP_Post object.
+ * @param string $column_name Current column name.
+ */
+ public function column_default( $item, $column_name ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $post = $item;
+
+ if ( 'categories' === $column_name ) {
+ $taxonomy = 'category';
+ } elseif ( 'tags' === $column_name ) {
+ $taxonomy = 'post_tag';
+ } elseif ( str_starts_with( $column_name, 'taxonomy-' ) ) {
+ $taxonomy = substr( $column_name, 9 );
+ } else {
+ $taxonomy = false;
+ }
+
+ if ( $taxonomy ) {
+ $terms = get_the_terms( $post->ID, $taxonomy );
+
+ if ( is_array( $terms ) ) {
+ $output = array();
+
+ foreach ( $terms as $t ) {
+ $posts_in_term_qv = array();
+ $posts_in_term_qv['taxonomy'] = $taxonomy;
+ $posts_in_term_qv['term'] = $t->slug;
+
+ $output[] = sprintf(
+ '%s ',
+ esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
+ esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
+ );
+ }
+
+ echo implode( wp_get_list_item_separator(), $output );
+ } else {
+ echo '— ' . get_taxonomy( $taxonomy )->labels->no_terms . ' ';
+ }
+
+ return;
+ }
+
+ /**
+ * Fires for each custom column in the Media list table.
+ *
+ * Custom columns are registered using the {@see 'manage_media_columns'} filter.
+ *
+ * @since 2.5.0
+ *
+ * @param string $column_name Name of the custom column.
+ * @param int $post_id Attachment ID.
+ */
+ do_action( 'manage_media_custom_column', $column_name, $post->ID );
+ }
+
+ /**
+ * @global WP_Post $post Global post object.
+ * @global WP_Query $wp_query WordPress Query object.
+ */
+ public function display_rows() {
+ global $post, $wp_query;
+
+ $post_ids = wp_list_pluck( $wp_query->posts, 'ID' );
+ reset( $wp_query->posts );
+
+ $this->comment_pending_count = get_pending_comments_num( $post_ids );
+
+ add_filter( 'the_title', 'esc_html' );
+
+ while ( have_posts() ) :
+ the_post();
+
+ if ( $this->is_trash && 'trash' !== $post->post_status
+ || ! $this->is_trash && 'trash' === $post->post_status
+ ) {
+ continue;
+ }
+
+ $post_owner = ( get_current_user_id() === (int) $post->post_author ) ? 'self' : 'other';
+ ?>
+
+ single_row_columns( $post ); ?>
+
+ is_trash && current_user_can( 'edit_post', $post->ID ) ) {
+ $actions['edit'] = sprintf(
+ '%s ',
+ esc_url( get_edit_post_link( $post->ID ) ),
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ),
+ __( 'Edit' )
+ );
+ }
+
+ if ( current_user_can( 'delete_post', $post->ID ) ) {
+ if ( $this->is_trash ) {
+ $actions['untrash'] = sprintf(
+ '%s ',
+ esc_url( wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-post_' . $post->ID ) ),
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'Restore “%s” from the Trash' ), $att_title ) ),
+ __( 'Restore' )
+ );
+ } elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
+ $actions['trash'] = sprintf(
+ '%s ',
+ esc_url( wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) ),
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'Move “%s” to the Trash' ), $att_title ) ),
+ _x( 'Trash', 'verb' )
+ );
+ }
+
+ if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) {
+ $show_confirmation = ( ! $this->is_trash && ! MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
+
+ $actions['delete'] = sprintf(
+ '%s ',
+ esc_url( wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID ) ),
+ $show_confirmation,
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'Delete “%s” permanently' ), $att_title ) ),
+ __( 'Delete Permanently' )
+ );
+ }
+ }
+
+ $attachment_url = wp_get_attachment_url( $post->ID );
+
+ if ( ! $this->is_trash ) {
+ $permalink = get_permalink( $post->ID );
+
+ if ( $permalink ) {
+ $actions['view'] = sprintf(
+ '%s ',
+ esc_url( $permalink ),
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ),
+ __( 'View' )
+ );
+ }
+
+ if ( $attachment_url ) {
+ $actions['copy'] = sprintf(
+ '%s %s ',
+ esc_url( $attachment_url ),
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'Copy “%s” URL to clipboard' ), $att_title ) ),
+ __( 'Copy URL' ),
+ __( 'Copied!' )
+ );
+ }
+ }
+
+ if ( $attachment_url ) {
+ $actions['download'] = sprintf(
+ '%s ',
+ esc_url( $attachment_url ),
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'Download “%s”' ), $att_title ) ),
+ __( 'Download file' )
+ );
+ }
+
+ if ( $this->detached && current_user_can( 'edit_post', $post->ID ) ) {
+ $actions['attach'] = sprintf(
+ '%s ',
+ $post->ID,
+ /* translators: %s: Attachment title. */
+ esc_attr( sprintf( __( 'Attach “%s” to existing content' ), $att_title ) ),
+ __( 'Attach' )
+ );
+ }
+
+ /**
+ * Filters the action links for each attachment in the Media list table.
+ *
+ * @since 2.8.0
+ *
+ * @param string[] $actions An array of action links for each attachment.
+ * Includes 'Edit', 'Delete Permanently', 'View',
+ * 'Copy URL' and 'Download file'.
+ * @param WP_Post $post WP_Post object for the current attachment.
+ * @param bool $detached Whether the list table contains media not attached
+ * to any posts. Default true.
+ */
+ return apply_filters( 'media_row_actions', $actions, $post, $this->detached );
+ }
+
+ /**
+ * Generates and displays row action links.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Post $item Attachment being acted upon.
+ * @param string $column_name Current column name.
+ * @param string $primary Primary column name.
+ * @return string Row actions output for media attachments, or an empty string
+ * if the current column is not the primary column.
+ */
+ protected function handle_row_actions( $item, $column_name, $primary ) {
+ if ( $primary !== $column_name ) {
+ return '';
+ }
+
+ // Restores the more descriptive, specific name for use within this method.
+ $post = $item;
+
+ $att_title = _draft_or_post_title();
+ $actions = $this->_get_row_actions( $post, $att_title );
+
+ return $this->row_actions( $actions );
+ }
+}
diff --git a/wp-admin/includes/class-wp-ms-sites-list-table.php b/wp-admin/includes/class-wp-ms-sites-list-table.php
new file mode 100644
index 0000000..ffa2231
--- /dev/null
+++ b/wp-admin/includes/class-wp-ms-sites-list-table.php
@@ -0,0 +1,861 @@
+status_list = array(
+ 'archived' => array( 'site-archived', __( 'Archived' ) ),
+ 'spam' => array( 'site-spammed', _x( 'Spam', 'site' ) ),
+ 'deleted' => array( 'site-deleted', __( 'Deleted' ) ),
+ 'mature' => array( 'site-mature', __( 'Mature' ) ),
+ );
+
+ parent::__construct(
+ array(
+ 'plural' => 'sites',
+ 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
+ )
+ );
+ }
+
+ /**
+ * @return bool
+ */
+ public function ajax_user_can() {
+ return current_user_can( 'manage_sites' );
+ }
+
+ /**
+ * Prepares the list of sites for display.
+ *
+ * @since 3.1.0
+ *
+ * @global string $mode List table view mode.
+ * @global string $s
+ * @global wpdb $wpdb WordPress database abstraction object.
+ */
+ public function prepare_items() {
+ global $mode, $s, $wpdb;
+
+ if ( ! empty( $_REQUEST['mode'] ) ) {
+ $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
+ set_user_setting( 'sites_list_mode', $mode );
+ } else {
+ $mode = get_user_setting( 'sites_list_mode', 'list' );
+ }
+
+ $per_page = $this->get_items_per_page( 'sites_network_per_page' );
+
+ $pagenum = $this->get_pagenum();
+
+ $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
+ $wild = '';
+ if ( str_contains( $s, '*' ) ) {
+ $wild = '*';
+ $s = trim( $s, '*' );
+ }
+
+ /*
+ * If the network is large and a search is not being performed, show only
+ * the latest sites with no paging in order to avoid expensive count queries.
+ */
+ if ( ! $s && wp_is_large_network() ) {
+ if ( ! isset( $_REQUEST['orderby'] ) ) {
+ $_GET['orderby'] = '';
+ $_REQUEST['orderby'] = '';
+ }
+ if ( ! isset( $_REQUEST['order'] ) ) {
+ $_GET['order'] = 'DESC';
+ $_REQUEST['order'] = 'DESC';
+ }
+ }
+
+ $args = array(
+ 'number' => (int) $per_page,
+ 'offset' => (int) ( ( $pagenum - 1 ) * $per_page ),
+ 'network_id' => get_current_network_id(),
+ );
+
+ if ( empty( $s ) ) {
+ // Nothing to do.
+ } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s )
+ || preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s )
+ || preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s )
+ || preg_match( '/^[0-9]{1,3}\.$/', $s )
+ ) {
+ // IPv4 address.
+ $sql = $wpdb->prepare(
+ "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s",
+ $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' )
+ );
+
+ $reg_blog_ids = $wpdb->get_col( $sql );
+
+ if ( $reg_blog_ids ) {
+ $args['site__in'] = $reg_blog_ids;
+ }
+ } elseif ( is_numeric( $s ) && empty( $wild ) ) {
+ $args['ID'] = $s;
+ } else {
+ $args['search'] = $s;
+
+ if ( ! is_subdomain_install() ) {
+ $args['search_columns'] = array( 'path' );
+ }
+ }
+
+ $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
+ if ( 'registered' === $order_by ) {
+ // 'registered' is a valid field name.
+ } elseif ( 'lastupdated' === $order_by ) {
+ $order_by = 'last_updated';
+ } elseif ( 'blogname' === $order_by ) {
+ if ( is_subdomain_install() ) {
+ $order_by = 'domain';
+ } else {
+ $order_by = 'path';
+ }
+ } elseif ( 'blog_id' === $order_by ) {
+ $order_by = 'id';
+ } elseif ( ! $order_by ) {
+ $order_by = false;
+ }
+
+ $args['orderby'] = $order_by;
+
+ if ( $order_by ) {
+ $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? 'DESC' : 'ASC';
+ }
+
+ if ( wp_is_large_network() ) {
+ $args['no_found_rows'] = true;
+ } else {
+ $args['no_found_rows'] = false;
+ }
+
+ // Take into account the role the user has selected.
+ $status = isset( $_REQUEST['status'] ) ? wp_unslash( trim( $_REQUEST['status'] ) ) : '';
+ if ( in_array( $status, array( 'public', 'archived', 'mature', 'spam', 'deleted' ), true ) ) {
+ $args[ $status ] = 1;
+ }
+
+ /**
+ * Filters the arguments for the site query in the sites list table.
+ *
+ * @since 4.6.0
+ *
+ * @param array $args An array of get_sites() arguments.
+ */
+ $args = apply_filters( 'ms_sites_list_table_query_args', $args );
+
+ $_sites = get_sites( $args );
+ if ( is_array( $_sites ) ) {
+ update_site_cache( $_sites );
+
+ $this->items = array_slice( $_sites, 0, $per_page );
+ }
+
+ $total_sites = get_sites(
+ array_merge(
+ $args,
+ array(
+ 'count' => true,
+ 'offset' => 0,
+ 'number' => 0,
+ )
+ )
+ );
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $total_sites,
+ 'per_page' => $per_page,
+ )
+ );
+ }
+
+ /**
+ */
+ public function no_items() {
+ _e( 'No sites found.' );
+ }
+
+ /**
+ * Gets links to filter sites by status.
+ *
+ * @since 5.3.0
+ *
+ * @return array
+ */
+ protected function get_views() {
+ $counts = wp_count_sites();
+
+ $statuses = array(
+ /* translators: %s: Number of sites. */
+ 'all' => _nx_noop(
+ 'All (%s) ',
+ 'All (%s) ',
+ 'sites'
+ ),
+
+ /* translators: %s: Number of sites. */
+ 'public' => _n_noop(
+ 'Public (%s) ',
+ 'Public (%s) '
+ ),
+
+ /* translators: %s: Number of sites. */
+ 'archived' => _n_noop(
+ 'Archived (%s) ',
+ 'Archived (%s) '
+ ),
+
+ /* translators: %s: Number of sites. */
+ 'mature' => _n_noop(
+ 'Mature (%s) ',
+ 'Mature (%s) '
+ ),
+
+ /* translators: %s: Number of sites. */
+ 'spam' => _nx_noop(
+ 'Spam (%s) ',
+ 'Spam (%s) ',
+ 'sites'
+ ),
+
+ /* translators: %s: Number of sites. */
+ 'deleted' => _n_noop(
+ 'Deleted (%s) ',
+ 'Deleted (%s) '
+ ),
+ );
+
+ $view_links = array();
+ $requested_status = isset( $_REQUEST['status'] ) ? wp_unslash( trim( $_REQUEST['status'] ) ) : '';
+ $url = 'sites.php';
+
+ foreach ( $statuses as $status => $label_count ) {
+ if ( (int) $counts[ $status ] > 0 ) {
+ $label = sprintf(
+ translate_nooped_plural( $label_count, $counts[ $status ] ),
+ number_format_i18n( $counts[ $status ] )
+ );
+
+ $full_url = 'all' === $status ? $url : add_query_arg( 'status', $status, $url );
+
+ $view_links[ $status ] = array(
+ 'url' => esc_url( $full_url ),
+ 'label' => $label,
+ 'current' => $requested_status === $status || ( '' === $requested_status && 'all' === $status ),
+ );
+ }
+ }
+
+ return $this->get_views_links( $view_links );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ $actions = array();
+ if ( current_user_can( 'delete_sites' ) ) {
+ $actions['delete'] = __( 'Delete' );
+ }
+ $actions['spam'] = _x( 'Mark as spam', 'site' );
+ $actions['notspam'] = _x( 'Not spam', 'site' );
+
+ return $actions;
+ }
+
+ /**
+ * @global string $mode List table view mode.
+ *
+ * @param string $which The location of the pagination nav markup: 'top' or 'bottom'.
+ */
+ protected function pagination( $which ) {
+ global $mode;
+
+ parent::pagination( $which );
+
+ if ( 'top' === $which ) {
+ $this->view_switcher( $mode );
+ }
+ }
+
+ /**
+ * Displays extra controls between bulk actions and pagination.
+ *
+ * @since 5.3.0
+ *
+ * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
+ */
+ protected function extra_tablenav( $which ) {
+ ?>
+
+ 'site-query-submit' ) );
+ }
+ }
+ ?>
+
+ ' ',
+ 'blogname' => __( 'URL' ),
+ 'lastupdated' => __( 'Last Updated' ),
+ 'registered' => _x( 'Registered', 'site' ),
+ 'users' => __( 'Users' ),
+ );
+
+ if ( has_filter( 'wpmublogsaction' ) ) {
+ $sites_columns['plugins'] = __( 'Actions' );
+ }
+
+ /**
+ * Filters the displayed site columns in Sites list table.
+ *
+ * @since MU (3.0.0)
+ *
+ * @param string[] $sites_columns An array of displayed site columns. Default 'cb',
+ * 'blogname', 'lastupdated', 'registered', 'users'.
+ */
+ return apply_filters( 'wpmu_blogs_columns', $sites_columns );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_sortable_columns() {
+
+ if ( is_subdomain_install() ) {
+ $blogname_abbr = __( 'Domain' );
+ $blogname_orderby_text = __( 'Table ordered by Site Domain Name.' );
+ } else {
+ $blogname_abbr = __( 'Path' );
+ $blogname_orderby_text = __( 'Table ordered by Site Path.' );
+ }
+
+ return array(
+ 'blogname' => array( 'blogname', false, $blogname_abbr, $blogname_orderby_text ),
+ 'lastupdated' => array( 'lastupdated', true, __( 'Last Updated' ), __( 'Table ordered by Last Updated.' ) ),
+ 'registered' => array( 'blog_id', true, _x( 'Registered', 'site' ), __( 'Table ordered by Site Registered Date.' ), 'desc' ),
+ );
+ }
+
+ /**
+ * Handles the checkbox column output.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$blog` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param array $item Current site.
+ */
+ public function column_cb( $item ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $blog = $item;
+
+ if ( ! is_main_site( $blog['blog_id'] ) ) :
+ $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
+ ?>
+
+
+
+
+
+
+
+
+ %2$s',
+ esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ),
+ $blogname
+ );
+
+ $this->site_states( $blog );
+ ?>
+
+ ';
+ printf(
+ /* translators: 1: Site title, 2: Site tagline. */
+ __( '%1$s – %2$s' ),
+ get_option( 'blogname' ),
+ '' . get_option( 'blogdescription' ) . ' '
+ );
+ echo '
';
+ restore_current_blog();
+ }
+ }
+
+ /**
+ * Handles the lastupdated column output.
+ *
+ * @since 4.3.0
+ *
+ * @global string $mode List table view mode.
+ *
+ * @param array $blog Current site.
+ */
+ public function column_lastupdated( $blog ) {
+ global $mode;
+
+ if ( 'list' === $mode ) {
+ $date = __( 'Y/m/d' );
+ } else {
+ $date = __( 'Y/m/d g:i:s a' );
+ }
+
+ if ( '0000-00-00 00:00:00' === $blog['last_updated'] ) {
+ _e( 'Never' );
+ } else {
+ echo mysql2date( $date, $blog['last_updated'] );
+ }
+ }
+
+ /**
+ * Handles the registered column output.
+ *
+ * @since 4.3.0
+ *
+ * @global string $mode List table view mode.
+ *
+ * @param array $blog Current site.
+ */
+ public function column_registered( $blog ) {
+ global $mode;
+
+ if ( 'list' === $mode ) {
+ $date = __( 'Y/m/d' );
+ } else {
+ $date = __( 'Y/m/d g:i:s a' );
+ }
+
+ if ( '0000-00-00 00:00:00' === $blog['registered'] ) {
+ echo '—';
+ } else {
+ echo mysql2date( $date, $blog['registered'] );
+ }
+ }
+
+ /**
+ * Handles the users column output.
+ *
+ * @since 4.3.0
+ *
+ * @param array $blog Current site.
+ */
+ public function column_users( $blog ) {
+ $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
+ if ( ! $user_count ) {
+ $blog_users = new WP_User_Query(
+ array(
+ 'blog_id' => $blog['blog_id'],
+ 'fields' => 'ID',
+ 'number' => 1,
+ 'count_total' => true,
+ )
+ );
+ $user_count = $blog_users->get_total();
+ wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
+ }
+
+ printf(
+ '%2$s ',
+ esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
+ number_format_i18n( $user_count )
+ );
+ }
+
+ /**
+ * Handles the plugins column output.
+ *
+ * @since 4.3.0
+ *
+ * @param array $blog Current site.
+ */
+ public function column_plugins( $blog ) {
+ if ( has_filter( 'wpmublogsaction' ) ) {
+ /**
+ * Fires inside the auxiliary 'Actions' column of the Sites list table.
+ *
+ * By default this column is hidden unless something is hooked to the action.
+ *
+ * @since MU (3.0.0)
+ *
+ * @param int $blog_id The site ID.
+ */
+ do_action( 'wpmublogsaction', $blog['blog_id'] );
+ }
+ }
+
+ /**
+ * Handles output for the default column.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$blog` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param array $item Current site.
+ * @param string $column_name Current column name.
+ */
+ public function column_default( $item, $column_name ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $blog = $item;
+
+ /**
+ * Fires for each registered custom column in the Sites list table.
+ *
+ * @since 3.1.0
+ *
+ * @param string $column_name The name of the column to display.
+ * @param int $blog_id The site ID.
+ */
+ do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
+ }
+
+ /**
+ * @global string $mode List table view mode.
+ */
+ public function display_rows() {
+ foreach ( $this->items as $blog ) {
+ $blog = $blog->to_array();
+ $class = '';
+ reset( $this->status_list );
+
+ foreach ( $this->status_list as $status => $col ) {
+ if ( '1' === $blog[ $status ] ) {
+ $class = " class='{$col[0]}'";
+ }
+ }
+
+ echo "";
+
+ $this->single_row_columns( $blog );
+
+ echo ' ';
+ }
+ }
+
+ /**
+ * Determines whether to output comma-separated site states.
+ *
+ * @since 5.3.0
+ *
+ * @param array $site
+ */
+ protected function site_states( $site ) {
+ $site_states = array();
+
+ // $site is still an array, so get the object.
+ $_site = WP_Site::get_instance( $site['blog_id'] );
+
+ if ( is_main_site( $_site->id ) ) {
+ $site_states['main'] = __( 'Main' );
+ }
+
+ reset( $this->status_list );
+
+ $site_status = isset( $_REQUEST['status'] ) ? wp_unslash( trim( $_REQUEST['status'] ) ) : '';
+ foreach ( $this->status_list as $status => $col ) {
+ if ( '1' === $_site->{$status} && $site_status !== $status ) {
+ $site_states[ $col[0] ] = $col[1];
+ }
+ }
+
+ /**
+ * Filters the default site display states for items in the Sites list table.
+ *
+ * @since 5.3.0
+ *
+ * @param string[] $site_states An array of site states. Default 'Main',
+ * 'Archived', 'Mature', 'Spam', 'Deleted'.
+ * @param WP_Site $site The current site object.
+ */
+ $site_states = apply_filters( 'display_site_states', $site_states, $_site );
+
+ if ( ! empty( $site_states ) ) {
+ $state_count = count( $site_states );
+
+ $i = 0;
+
+ echo ' — ';
+
+ foreach ( $site_states as $state ) {
+ ++$i;
+
+ $separator = ( $i < $state_count ) ? ', ' : '';
+
+ echo "{$state}{$separator} ";
+ }
+ }
+ }
+
+ /**
+ * Gets the name of the default primary column.
+ *
+ * @since 4.3.0
+ *
+ * @return string Name of the default primary column, in this case, 'blogname'.
+ */
+ protected function get_default_primary_column_name() {
+ return 'blogname';
+ }
+
+ /**
+ * Generates and displays row action links.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$blog` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param array $item Site being acted upon.
+ * @param string $column_name Current column name.
+ * @param string $primary Primary column name.
+ * @return string Row actions output for sites in Multisite, or an empty string
+ * if the current column is not the primary column.
+ */
+ protected function handle_row_actions( $item, $column_name, $primary ) {
+ if ( $primary !== $column_name ) {
+ return '';
+ }
+
+ // Restores the more descriptive, specific name for use within this method.
+ $blog = $item;
+
+ $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
+
+ // Preordered.
+ $actions = array(
+ 'edit' => '',
+ 'backend' => '',
+ 'activate' => '',
+ 'deactivate' => '',
+ 'archive' => '',
+ 'unarchive' => '',
+ 'spam' => '',
+ 'unspam' => '',
+ 'delete' => '',
+ 'visit' => '',
+ );
+
+ $actions['edit'] = sprintf(
+ '%2$s ',
+ esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ),
+ __( 'Edit' )
+ );
+
+ $actions['backend'] = sprintf(
+ '%2$s ',
+ esc_url( get_admin_url( $blog['blog_id'] ) ),
+ __( 'Dashboard' )
+ );
+
+ if ( ! is_main_site( $blog['blog_id'] ) ) {
+ if ( '1' === $blog['deleted'] ) {
+ $actions['activate'] = sprintf(
+ '%2$s ',
+ esc_url(
+ wp_nonce_url(
+ network_admin_url( 'sites.php?action=confirm&action2=activateblog&id=' . $blog['blog_id'] ),
+ 'activateblog_' . $blog['blog_id']
+ )
+ ),
+ __( 'Activate' )
+ );
+ } else {
+ $actions['deactivate'] = sprintf(
+ '%2$s ',
+ esc_url(
+ wp_nonce_url(
+ network_admin_url( 'sites.php?action=confirm&action2=deactivateblog&id=' . $blog['blog_id'] ),
+ 'deactivateblog_' . $blog['blog_id']
+ )
+ ),
+ __( 'Deactivate' )
+ );
+ }
+
+ if ( '1' === $blog['archived'] ) {
+ $actions['unarchive'] = sprintf(
+ '%2$s ',
+ esc_url(
+ wp_nonce_url(
+ network_admin_url( 'sites.php?action=confirm&action2=unarchiveblog&id=' . $blog['blog_id'] ),
+ 'unarchiveblog_' . $blog['blog_id']
+ )
+ ),
+ __( 'Unarchive' )
+ );
+ } else {
+ $actions['archive'] = sprintf(
+ '%2$s ',
+ esc_url(
+ wp_nonce_url(
+ network_admin_url( 'sites.php?action=confirm&action2=archiveblog&id=' . $blog['blog_id'] ),
+ 'archiveblog_' . $blog['blog_id']
+ )
+ ),
+ _x( 'Archive', 'verb; site' )
+ );
+ }
+
+ if ( '1' === $blog['spam'] ) {
+ $actions['unspam'] = sprintf(
+ '%2$s ',
+ esc_url(
+ wp_nonce_url(
+ network_admin_url( 'sites.php?action=confirm&action2=unspamblog&id=' . $blog['blog_id'] ),
+ 'unspamblog_' . $blog['blog_id']
+ )
+ ),
+ _x( 'Not Spam', 'site' )
+ );
+ } else {
+ $actions['spam'] = sprintf(
+ '%2$s ',
+ esc_url(
+ wp_nonce_url(
+ network_admin_url( 'sites.php?action=confirm&action2=spamblog&id=' . $blog['blog_id'] ),
+ 'spamblog_' . $blog['blog_id']
+ )
+ ),
+ _x( 'Spam', 'site' )
+ );
+ }
+
+ if ( current_user_can( 'delete_site', $blog['blog_id'] ) ) {
+ $actions['delete'] = sprintf(
+ '%2$s ',
+ esc_url(
+ wp_nonce_url(
+ network_admin_url( 'sites.php?action=confirm&action2=deleteblog&id=' . $blog['blog_id'] ),
+ 'deleteblog_' . $blog['blog_id']
+ )
+ ),
+ __( 'Delete' )
+ );
+ }
+ }
+
+ $actions['visit'] = sprintf(
+ '%2$s ',
+ esc_url( get_home_url( $blog['blog_id'], '/' ) ),
+ __( 'Visit' )
+ );
+
+ /**
+ * Filters the action links displayed for each site in the Sites list table.
+ *
+ * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
+ * default for each site. The site's status determines whether to show the
+ * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
+ * 'Not Spam' or 'Spam' link for each site.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $actions An array of action links to be displayed.
+ * @param int $blog_id The site ID.
+ * @param string $blogname Site path, formatted depending on whether it is a sub-domain
+ * or subdirectory multisite installation.
+ */
+ $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
+
+ return $this->row_actions( $actions );
+ }
+}
diff --git a/wp-admin/includes/class-wp-ms-themes-list-table.php b/wp-admin/includes/class-wp-ms-themes-list-table.php
new file mode 100644
index 0000000..cc0206e
--- /dev/null
+++ b/wp-admin/includes/class-wp-ms-themes-list-table.php
@@ -0,0 +1,1038 @@
+ 'themes',
+ 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
+ )
+ );
+
+ $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all';
+ if ( ! in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken', 'auto-update-enabled', 'auto-update-disabled' ), true ) ) {
+ $status = 'all';
+ }
+
+ $page = $this->get_pagenum();
+
+ $this->is_site_themes = ( 'site-themes-network' === $this->screen->id ) ? true : false;
+
+ if ( $this->is_site_themes ) {
+ $this->site_id = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0;
+ }
+
+ $this->show_autoupdates = wp_is_auto_update_enabled_for_type( 'theme' ) &&
+ ! $this->is_site_themes && current_user_can( 'update_themes' );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_table_classes() {
+ // @todo Remove and add CSS for .themes.
+ return array( 'widefat', 'plugins' );
+ }
+
+ /**
+ * @return bool
+ */
+ public function ajax_user_can() {
+ if ( $this->is_site_themes ) {
+ return current_user_can( 'manage_sites' );
+ } else {
+ return current_user_can( 'manage_network_themes' );
+ }
+ }
+
+ /**
+ * @global string $status
+ * @global array $totals
+ * @global int $page
+ * @global string $orderby
+ * @global string $order
+ * @global string $s
+ */
+ public function prepare_items() {
+ global $status, $totals, $page, $orderby, $order, $s;
+
+ wp_reset_vars( array( 'orderby', 'order', 's' ) );
+
+ $themes = array(
+ /**
+ * Filters the full array of WP_Theme objects to list in the Multisite
+ * themes list table.
+ *
+ * @since 3.1.0
+ *
+ * @param WP_Theme[] $all Array of WP_Theme objects to display in the list table.
+ */
+ 'all' => apply_filters( 'all_themes', wp_get_themes() ),
+ 'search' => array(),
+ 'enabled' => array(),
+ 'disabled' => array(),
+ 'upgrade' => array(),
+ 'broken' => $this->is_site_themes ? array() : wp_get_themes( array( 'errors' => true ) ),
+ );
+
+ if ( $this->show_autoupdates ) {
+ $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
+
+ $themes['auto-update-enabled'] = array();
+ $themes['auto-update-disabled'] = array();
+ }
+
+ if ( $this->is_site_themes ) {
+ $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
+ $allowed_where = 'site';
+ } else {
+ $themes_per_page = $this->get_items_per_page( 'themes_network_per_page' );
+ $allowed_where = 'network';
+ }
+
+ $current = get_site_transient( 'update_themes' );
+ $maybe_update = current_user_can( 'update_themes' ) && ! $this->is_site_themes && $current;
+
+ foreach ( (array) $themes['all'] as $key => $theme ) {
+ if ( $this->is_site_themes && $theme->is_allowed( 'network' ) ) {
+ unset( $themes['all'][ $key ] );
+ continue;
+ }
+
+ if ( $maybe_update && isset( $current->response[ $key ] ) ) {
+ $themes['all'][ $key ]->update = true;
+ $themes['upgrade'][ $key ] = $themes['all'][ $key ];
+ }
+
+ $filter = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
+ $themes[ $filter ][ $key ] = $themes['all'][ $key ];
+
+ $theme_data = array(
+ 'update_supported' => isset( $theme->update_supported ) ? $theme->update_supported : true,
+ );
+
+ // Extra info if known. array_merge() ensures $theme_data has precedence if keys collide.
+ if ( isset( $current->response[ $key ] ) ) {
+ $theme_data = array_merge( (array) $current->response[ $key ], $theme_data );
+ } elseif ( isset( $current->no_update[ $key ] ) ) {
+ $theme_data = array_merge( (array) $current->no_update[ $key ], $theme_data );
+ } else {
+ $theme_data['update_supported'] = false;
+ }
+
+ $theme->update_supported = $theme_data['update_supported'];
+
+ /*
+ * Create the expected payload for the auto_update_theme filter, this is the same data
+ * as contained within $updates or $no_updates but used when the Theme is not known.
+ */
+ $filter_payload = array(
+ 'theme' => $key,
+ 'new_version' => '',
+ 'url' => '',
+ 'package' => '',
+ 'requires' => '',
+ 'requires_php' => '',
+ );
+
+ $filter_payload = (object) array_merge( $filter_payload, array_intersect_key( $theme_data, $filter_payload ) );
+
+ $auto_update_forced = wp_is_auto_update_forced_for_item( 'theme', null, $filter_payload );
+
+ if ( ! is_null( $auto_update_forced ) ) {
+ $theme->auto_update_forced = $auto_update_forced;
+ }
+
+ if ( $this->show_autoupdates ) {
+ $enabled = in_array( $key, $auto_updates, true ) && $theme->update_supported;
+ if ( isset( $theme->auto_update_forced ) ) {
+ $enabled = (bool) $theme->auto_update_forced;
+ }
+
+ if ( $enabled ) {
+ $themes['auto-update-enabled'][ $key ] = $theme;
+ } else {
+ $themes['auto-update-disabled'][ $key ] = $theme;
+ }
+ }
+ }
+
+ if ( $s ) {
+ $status = 'search';
+ $themes['search'] = array_filter( array_merge( $themes['all'], $themes['broken'] ), array( $this, '_search_callback' ) );
+ }
+
+ $totals = array();
+ $js_themes = array();
+ foreach ( $themes as $type => $list ) {
+ $totals[ $type ] = count( $list );
+ $js_themes[ $type ] = array_keys( $list );
+ }
+
+ if ( empty( $themes[ $status ] ) && ! in_array( $status, array( 'all', 'search' ), true ) ) {
+ $status = 'all';
+ }
+
+ $this->items = $themes[ $status ];
+ WP_Theme::sort_by_name( $this->items );
+
+ $this->has_items = ! empty( $themes['all'] );
+ $total_this_page = $totals[ $status ];
+
+ wp_localize_script(
+ 'updates',
+ '_wpUpdatesItemCounts',
+ array(
+ 'themes' => $js_themes,
+ 'totals' => wp_get_update_data(),
+ )
+ );
+
+ if ( $orderby ) {
+ $orderby = ucfirst( $orderby );
+ $order = strtoupper( $order );
+
+ if ( 'Name' === $orderby ) {
+ if ( 'ASC' === $order ) {
+ $this->items = array_reverse( $this->items );
+ }
+ } else {
+ uasort( $this->items, array( $this, '_order_callback' ) );
+ }
+ }
+
+ $start = ( $page - 1 ) * $themes_per_page;
+
+ if ( $total_this_page > $themes_per_page ) {
+ $this->items = array_slice( $this->items, $start, $themes_per_page, true );
+ }
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $total_this_page,
+ 'per_page' => $themes_per_page,
+ )
+ );
+ }
+
+ /**
+ * @param WP_Theme $theme
+ * @return bool
+ */
+ public function _search_callback( $theme ) {
+ static $term = null;
+ if ( is_null( $term ) ) {
+ $term = wp_unslash( $_REQUEST['s'] );
+ }
+
+ foreach ( array( 'Name', 'Description', 'Author', 'Author', 'AuthorURI' ) as $field ) {
+ // Don't mark up; Do translate.
+ if ( false !== stripos( $theme->display( $field, false, true ), $term ) ) {
+ return true;
+ }
+ }
+
+ if ( false !== stripos( $theme->get_stylesheet(), $term ) ) {
+ return true;
+ }
+
+ if ( false !== stripos( $theme->get_template(), $term ) ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ // Not used by any core columns.
+ /**
+ * @global string $orderby
+ * @global string $order
+ * @param array $theme_a
+ * @param array $theme_b
+ * @return int
+ */
+ public function _order_callback( $theme_a, $theme_b ) {
+ global $orderby, $order;
+
+ $a = $theme_a[ $orderby ];
+ $b = $theme_b[ $orderby ];
+
+ if ( $a === $b ) {
+ return 0;
+ }
+
+ if ( 'DESC' === $order ) {
+ return ( $a < $b ) ? 1 : -1;
+ } else {
+ return ( $a < $b ) ? -1 : 1;
+ }
+ }
+
+ /**
+ */
+ public function no_items() {
+ if ( $this->has_items ) {
+ _e( 'No themes found.' );
+ } else {
+ _e( 'No themes are currently available.' );
+ }
+ }
+
+ /**
+ * @return string[] Array of column titles keyed by their column name.
+ */
+ public function get_columns() {
+ $columns = array(
+ 'cb' => ' ',
+ 'name' => __( 'Theme' ),
+ 'description' => __( 'Description' ),
+ );
+
+ if ( $this->show_autoupdates ) {
+ $columns['auto-updates'] = __( 'Automatic Updates' );
+ }
+
+ return $columns;
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_sortable_columns() {
+ return array(
+ 'name' => array( 'name', false, __( 'Theme' ), __( 'Table ordered by Theme Name.' ), 'asc' ),
+ );
+ }
+
+ /**
+ * Gets the name of the primary column.
+ *
+ * @since 4.3.0
+ *
+ * @return string Unalterable name of the primary column name, in this case, 'name'.
+ */
+ protected function get_primary_column_name() {
+ return 'name';
+ }
+
+ /**
+ * @global array $totals
+ * @global string $status
+ * @return array
+ */
+ protected function get_views() {
+ global $totals, $status;
+
+ $status_links = array();
+ foreach ( $totals as $type => $count ) {
+ if ( ! $count ) {
+ continue;
+ }
+
+ switch ( $type ) {
+ case 'all':
+ /* translators: %s: Number of themes. */
+ $text = _nx(
+ 'All (%s) ',
+ 'All (%s) ',
+ $count,
+ 'themes'
+ );
+ break;
+ case 'enabled':
+ /* translators: %s: Number of themes. */
+ $text = _nx(
+ 'Enabled (%s) ',
+ 'Enabled (%s) ',
+ $count,
+ 'themes'
+ );
+ break;
+ case 'disabled':
+ /* translators: %s: Number of themes. */
+ $text = _nx(
+ 'Disabled (%s) ',
+ 'Disabled (%s) ',
+ $count,
+ 'themes'
+ );
+ break;
+ case 'upgrade':
+ /* translators: %s: Number of themes. */
+ $text = _nx(
+ 'Update Available (%s) ',
+ 'Update Available (%s) ',
+ $count,
+ 'themes'
+ );
+ break;
+ case 'broken':
+ /* translators: %s: Number of themes. */
+ $text = _nx(
+ 'Broken (%s) ',
+ 'Broken (%s) ',
+ $count,
+ 'themes'
+ );
+ break;
+ case 'auto-update-enabled':
+ /* translators: %s: Number of themes. */
+ $text = _n(
+ 'Auto-updates Enabled (%s) ',
+ 'Auto-updates Enabled (%s) ',
+ $count
+ );
+ break;
+ case 'auto-update-disabled':
+ /* translators: %s: Number of themes. */
+ $text = _n(
+ 'Auto-updates Disabled (%s) ',
+ 'Auto-updates Disabled (%s) ',
+ $count
+ );
+ break;
+ }
+
+ if ( $this->is_site_themes ) {
+ $url = 'site-themes.php?id=' . $this->site_id;
+ } else {
+ $url = 'themes.php';
+ }
+
+ if ( 'search' !== $type ) {
+ $status_links[ $type ] = array(
+ 'url' => esc_url( add_query_arg( 'theme_status', $type, $url ) ),
+ 'label' => sprintf( $text, number_format_i18n( $count ) ),
+ 'current' => $type === $status,
+ );
+ }
+ }
+
+ return $this->get_views_links( $status_links );
+ }
+
+ /**
+ * @global string $status
+ *
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ global $status;
+
+ $actions = array();
+ if ( 'enabled' !== $status ) {
+ $actions['enable-selected'] = $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' );
+ }
+ if ( 'disabled' !== $status ) {
+ $actions['disable-selected'] = $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' );
+ }
+ if ( ! $this->is_site_themes ) {
+ if ( current_user_can( 'update_themes' ) ) {
+ $actions['update-selected'] = __( 'Update' );
+ }
+ if ( current_user_can( 'delete_themes' ) ) {
+ $actions['delete-selected'] = __( 'Delete' );
+ }
+ }
+
+ if ( $this->show_autoupdates ) {
+ if ( 'auto-update-enabled' !== $status ) {
+ $actions['enable-auto-update-selected'] = __( 'Enable Auto-updates' );
+ }
+
+ if ( 'auto-update-disabled' !== $status ) {
+ $actions['disable-auto-update-selected'] = __( 'Disable Auto-updates' );
+ }
+ }
+
+ return $actions;
+ }
+
+ /**
+ */
+ public function display_rows() {
+ foreach ( $this->items as $theme ) {
+ $this->single_row( $theme );
+ }
+ }
+
+ /**
+ * Handles the checkbox column output.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Theme $item The current WP_Theme object.
+ */
+ public function column_cb( $item ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $theme = $item;
+
+ $checkbox_id = 'checkbox_' . md5( $theme->get( 'Name' ) );
+ ?>
+
+
+
+ display( 'Name' )
+ );
+ ?>
+
+
+ is_site_themes ) {
+ $url = "site-themes.php?id={$this->site_id}&";
+ $allowed = $theme->is_allowed( 'site', $this->site_id );
+ } else {
+ $url = 'themes.php?';
+ $allowed = $theme->is_allowed( 'network' );
+ }
+
+ // Pre-order.
+ $actions = array(
+ 'enable' => '',
+ 'disable' => '',
+ 'delete' => '',
+ );
+
+ $stylesheet = $theme->get_stylesheet();
+ $theme_key = urlencode( $stylesheet );
+
+ if ( ! $allowed ) {
+ if ( ! $theme->errors() ) {
+ $url = add_query_arg(
+ array(
+ 'action' => 'enable',
+ 'theme' => $theme_key,
+ 'paged' => $page,
+ 's' => $s,
+ ),
+ $url
+ );
+
+ if ( $this->is_site_themes ) {
+ /* translators: %s: Theme name. */
+ $aria_label = sprintf( __( 'Enable %s' ), $theme->display( 'Name' ) );
+ } else {
+ /* translators: %s: Theme name. */
+ $aria_label = sprintf( __( 'Network Enable %s' ), $theme->display( 'Name' ) );
+ }
+
+ $actions['enable'] = sprintf(
+ '%s ',
+ esc_url( wp_nonce_url( $url, 'enable-theme_' . $stylesheet ) ),
+ esc_attr( $aria_label ),
+ ( $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' ) )
+ );
+ }
+ } else {
+ $url = add_query_arg(
+ array(
+ 'action' => 'disable',
+ 'theme' => $theme_key,
+ 'paged' => $page,
+ 's' => $s,
+ ),
+ $url
+ );
+
+ if ( $this->is_site_themes ) {
+ /* translators: %s: Theme name. */
+ $aria_label = sprintf( __( 'Disable %s' ), $theme->display( 'Name' ) );
+ } else {
+ /* translators: %s: Theme name. */
+ $aria_label = sprintf( __( 'Network Disable %s' ), $theme->display( 'Name' ) );
+ }
+
+ $actions['disable'] = sprintf(
+ '%s ',
+ esc_url( wp_nonce_url( $url, 'disable-theme_' . $stylesheet ) ),
+ esc_attr( $aria_label ),
+ ( $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' ) )
+ );
+ }
+
+ if ( ! $allowed && ! $this->is_site_themes
+ && current_user_can( 'delete_themes' )
+ && get_option( 'stylesheet' ) !== $stylesheet
+ && get_option( 'template' ) !== $stylesheet
+ ) {
+ $url = add_query_arg(
+ array(
+ 'action' => 'delete-selected',
+ 'checked[]' => $theme_key,
+ 'theme_status' => $context,
+ 'paged' => $page,
+ 's' => $s,
+ ),
+ 'themes.php'
+ );
+
+ /* translators: %s: Theme name. */
+ $aria_label = sprintf( _x( 'Delete %s', 'theme' ), $theme->display( 'Name' ) );
+
+ $actions['delete'] = sprintf(
+ '%s ',
+ esc_url( wp_nonce_url( $url, 'bulk-themes' ) ),
+ esc_attr( $aria_label ),
+ __( 'Delete' )
+ );
+ }
+ /**
+ * Filters the action links displayed for each theme in the Multisite
+ * themes list table.
+ *
+ * The action links displayed are determined by the theme's status, and
+ * which Multisite themes list table is being displayed - the Network
+ * themes list table (themes.php), which displays all installed themes,
+ * or the Site themes list table (site-themes.php), which displays the
+ * non-network enabled themes when editing a site in the Network admin.
+ *
+ * The default action links for the Network themes list table include
+ * 'Network Enable', 'Network Disable', and 'Delete'.
+ *
+ * The default action links for the Site themes list table include
+ * 'Enable', and 'Disable'.
+ *
+ * @since 2.8.0
+ *
+ * @param string[] $actions An array of action links.
+ * @param WP_Theme $theme The current WP_Theme object.
+ * @param string $context Status of the theme, one of 'all', 'enabled', or 'disabled'.
+ */
+ $actions = apply_filters( 'theme_action_links', array_filter( $actions ), $theme, $context );
+
+ /**
+ * Filters the action links of a specific theme in the Multisite themes
+ * list table.
+ *
+ * The dynamic portion of the hook name, `$stylesheet`, refers to the
+ * directory name of the theme, which in most cases is synonymous
+ * with the template name.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $actions An array of action links.
+ * @param WP_Theme $theme The current WP_Theme object.
+ * @param string $context Status of the theme, one of 'all', 'enabled', or 'disabled'.
+ */
+ $actions = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, $context );
+
+ echo $this->row_actions( $actions, true );
+ }
+
+ /**
+ * Handles the description column output.
+ *
+ * @since 4.3.0
+ *
+ * @global string $status
+ * @global array $totals
+ *
+ * @param WP_Theme $theme The current WP_Theme object.
+ */
+ public function column_description( $theme ) {
+ global $status, $totals;
+
+ if ( $theme->errors() ) {
+ $pre = 'broken' === $status ? __( 'Broken Theme:' ) . ' ' : '';
+ echo '' . $pre . $theme->errors()->get_error_message() . '
';
+ }
+
+ if ( $this->is_site_themes ) {
+ $allowed = $theme->is_allowed( 'site', $this->site_id );
+ } else {
+ $allowed = $theme->is_allowed( 'network' );
+ }
+
+ $class = ! $allowed ? 'inactive' : 'active';
+ if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) ) {
+ $class .= ' update';
+ }
+
+ echo "" . $theme->display( 'Description' ) . "
+ ";
+
+ $stylesheet = $theme->get_stylesheet();
+ $theme_meta = array();
+
+ if ( $theme->get( 'Version' ) ) {
+ /* translators: %s: Theme version. */
+ $theme_meta[] = sprintf( __( 'Version %s' ), $theme->display( 'Version' ) );
+ }
+
+ /* translators: %s: Theme author. */
+ $theme_meta[] = sprintf( __( 'By %s' ), $theme->display( 'Author' ) );
+
+ if ( $theme->get( 'ThemeURI' ) ) {
+ /* translators: %s: Theme name. */
+ $aria_label = sprintf( __( 'Visit theme site for %s' ), $theme->display( 'Name' ) );
+
+ $theme_meta[] = sprintf(
+ '
%s ',
+ $theme->display( 'ThemeURI' ),
+ esc_attr( $aria_label ),
+ __( 'Visit Theme Site' )
+ );
+ }
+
+ if ( $theme->parent() ) {
+ $theme_meta[] = sprintf(
+ /* translators: %s: Theme name. */
+ __( 'Child theme of %s' ),
+ '
' . $theme->parent()->display( 'Name' ) . ' '
+ );
+ }
+
+ /**
+ * Filters the array of row meta for each theme in the Multisite themes
+ * list table.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $theme_meta An array of the theme's metadata, including
+ * the version, author, and theme URI.
+ * @param string $stylesheet Directory name of the theme.
+ * @param WP_Theme $theme WP_Theme object.
+ * @param string $status Status of the theme.
+ */
+ $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $stylesheet, $theme, $status );
+
+ echo implode( ' | ', $theme_meta );
+
+ echo '
';
+ }
+
+ /**
+ * Handles the auto-updates column output.
+ *
+ * @since 5.5.0
+ *
+ * @global string $status
+ * @global int $page
+ *
+ * @param WP_Theme $theme The current WP_Theme object.
+ */
+ public function column_autoupdates( $theme ) {
+ global $status, $page;
+
+ static $auto_updates, $available_updates;
+
+ if ( ! $auto_updates ) {
+ $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
+ }
+ if ( ! $available_updates ) {
+ $available_updates = get_site_transient( 'update_themes' );
+ }
+
+ $stylesheet = $theme->get_stylesheet();
+
+ if ( isset( $theme->auto_update_forced ) ) {
+ if ( $theme->auto_update_forced ) {
+ // Forced on.
+ $text = __( 'Auto-updates enabled' );
+ } else {
+ $text = __( 'Auto-updates disabled' );
+ }
+ $action = 'unavailable';
+ $time_class = ' hidden';
+ } elseif ( empty( $theme->update_supported ) ) {
+ $text = '';
+ $action = 'unavailable';
+ $time_class = ' hidden';
+ } elseif ( in_array( $stylesheet, $auto_updates, true ) ) {
+ $text = __( 'Disable auto-updates' );
+ $action = 'disable';
+ $time_class = '';
+ } else {
+ $text = __( 'Enable auto-updates' );
+ $action = 'enable';
+ $time_class = ' hidden';
+ }
+
+ $query_args = array(
+ 'action' => "{$action}-auto-update",
+ 'theme' => $stylesheet,
+ 'paged' => $page,
+ 'theme_status' => $status,
+ );
+
+ $url = add_query_arg( $query_args, 'themes.php' );
+
+ if ( 'unavailable' === $action ) {
+ $html[] = '' . $text . ' ';
+ } else {
+ $html[] = sprintf(
+ '',
+ wp_nonce_url( $url, 'updates' ),
+ $action
+ );
+
+ $html[] = ' ';
+ $html[] = '' . $text . ' ';
+ $html[] = ' ';
+
+ }
+
+ if ( isset( $available_updates->response[ $stylesheet ] ) ) {
+ $html[] = sprintf(
+ '%s
',
+ $time_class,
+ wp_get_auto_update_message()
+ );
+ }
+
+ $html = implode( '', $html );
+
+ /**
+ * Filters the HTML of the auto-updates setting for each theme in the Themes list table.
+ *
+ * @since 5.5.0
+ *
+ * @param string $html The HTML for theme's auto-update setting, including
+ * toggle auto-update action link and time to next update.
+ * @param string $stylesheet Directory name of the theme.
+ * @param WP_Theme $theme WP_Theme object.
+ */
+ echo apply_filters( 'theme_auto_update_setting_html', $html, $stylesheet, $theme );
+
+ wp_admin_notice(
+ '',
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'inline', 'hidden' ),
+ )
+ );
+ }
+
+ /**
+ * Handles default column output.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Theme $item The current WP_Theme object.
+ * @param string $column_name The current column name.
+ */
+ public function column_default( $item, $column_name ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $theme = $item;
+
+ $stylesheet = $theme->get_stylesheet();
+
+ /**
+ * Fires inside each custom column of the Multisite themes list table.
+ *
+ * @since 3.1.0
+ *
+ * @param string $column_name Name of the column.
+ * @param string $stylesheet Directory name of the theme.
+ * @param WP_Theme $theme Current WP_Theme object.
+ */
+ do_action( 'manage_themes_custom_column', $column_name, $stylesheet, $theme );
+ }
+
+ /**
+ * Handles the output for a single table row.
+ *
+ * @since 4.3.0
+ *
+ * @param WP_Theme $item The current WP_Theme object.
+ */
+ public function single_row_columns( $item ) {
+ list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
+
+ foreach ( $columns as $column_name => $column_display_name ) {
+ $extra_classes = '';
+ if ( in_array( $column_name, $hidden, true ) ) {
+ $extra_classes .= ' hidden';
+ }
+
+ switch ( $column_name ) {
+ case 'cb':
+ echo '';
+
+ $this->column_cb( $item );
+
+ echo ' ';
+ break;
+
+ case 'name':
+ $active_theme_label = '';
+
+ /* The presence of the site_id property means that this is a subsite view and a label for the active theme needs to be added */
+ if ( ! empty( $this->site_id ) ) {
+ $stylesheet = get_blog_option( $this->site_id, 'stylesheet' );
+ $template = get_blog_option( $this->site_id, 'template' );
+
+ /* Add a label for the active template */
+ if ( $item->get_template() === $template ) {
+ $active_theme_label = ' — ' . __( 'Active Theme' );
+ }
+
+ /* In case this is a child theme, label it properly */
+ if ( $stylesheet !== $template && $item->get_stylesheet() === $stylesheet ) {
+ $active_theme_label = ' — ' . __( 'Active Child Theme' );
+ }
+ }
+
+ echo "" . $item->display( 'Name' ) . $active_theme_label . ' ';
+
+ $this->column_name( $item );
+
+ echo ' ';
+ break;
+
+ case 'description':
+ echo "";
+
+ $this->column_description( $item );
+
+ echo ' ';
+ break;
+
+ case 'auto-updates':
+ echo "";
+
+ $this->column_autoupdates( $item );
+
+ echo ' ';
+ break;
+ default:
+ echo "";
+
+ $this->column_default( $item, $column_name );
+
+ echo ' ';
+ break;
+ }
+ }
+ }
+
+ /**
+ * @global string $status
+ * @global array $totals
+ *
+ * @param WP_Theme $theme
+ */
+ public function single_row( $theme ) {
+ global $status, $totals;
+
+ if ( $this->is_site_themes ) {
+ $allowed = $theme->is_allowed( 'site', $this->site_id );
+ } else {
+ $allowed = $theme->is_allowed( 'network' );
+ }
+
+ $stylesheet = $theme->get_stylesheet();
+
+ $class = ! $allowed ? 'inactive' : 'active';
+ if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) ) {
+ $class .= ' update';
+ }
+
+ printf(
+ '',
+ esc_attr( $class ),
+ esc_attr( $stylesheet )
+ );
+
+ $this->single_row_columns( $theme );
+
+ echo ' ';
+
+ if ( $this->is_site_themes ) {
+ remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row' );
+ }
+
+ /**
+ * Fires after each row in the Multisite themes list table.
+ *
+ * @since 3.1.0
+ *
+ * @param string $stylesheet Directory name of the theme.
+ * @param WP_Theme $theme Current WP_Theme object.
+ * @param string $status Status of the theme.
+ */
+ do_action( 'after_theme_row', $stylesheet, $theme, $status );
+
+ /**
+ * Fires after each specific row in the Multisite themes list table.
+ *
+ * The dynamic portion of the hook name, `$stylesheet`, refers to the
+ * directory name of the theme, most often synonymous with the template
+ * name of the theme.
+ *
+ * @since 3.5.0
+ *
+ * @param string $stylesheet Directory name of the theme.
+ * @param WP_Theme $theme Current WP_Theme object.
+ * @param string $status Status of the theme.
+ */
+ do_action( "after_theme_row_{$stylesheet}", $stylesheet, $theme, $status );
+ }
+}
diff --git a/wp-admin/includes/class-wp-ms-users-list-table.php b/wp-admin/includes/class-wp-ms-users-list-table.php
new file mode 100644
index 0000000..ec12321
--- /dev/null
+++ b/wp-admin/includes/class-wp-ms-users-list-table.php
@@ -0,0 +1,546 @@
+get_items_per_page( 'users_network_per_page' );
+
+ $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
+
+ $paged = $this->get_pagenum();
+
+ $args = array(
+ 'number' => $users_per_page,
+ 'offset' => ( $paged - 1 ) * $users_per_page,
+ 'search' => $usersearch,
+ 'blog_id' => 0,
+ 'fields' => 'all_with_meta',
+ );
+
+ if ( wp_is_large_network( 'users' ) ) {
+ $args['search'] = ltrim( $args['search'], '*' );
+ } elseif ( '' !== $args['search'] ) {
+ $args['search'] = trim( $args['search'], '*' );
+ $args['search'] = '*' . $args['search'] . '*';
+ }
+
+ if ( 'super' === $role ) {
+ $args['login__in'] = get_super_admins();
+ }
+
+ /*
+ * If the network is large and a search is not being performed,
+ * show only the latest users with no paging in order to avoid
+ * expensive count queries.
+ */
+ if ( ! $usersearch && wp_is_large_network( 'users' ) ) {
+ if ( ! isset( $_REQUEST['orderby'] ) ) {
+ $_GET['orderby'] = 'id';
+ $_REQUEST['orderby'] = 'id';
+ }
+ if ( ! isset( $_REQUEST['order'] ) ) {
+ $_GET['order'] = 'DESC';
+ $_REQUEST['order'] = 'DESC';
+ }
+ $args['count_total'] = false;
+ }
+
+ if ( isset( $_REQUEST['orderby'] ) ) {
+ $args['orderby'] = $_REQUEST['orderby'];
+ }
+
+ if ( isset( $_REQUEST['order'] ) ) {
+ $args['order'] = $_REQUEST['order'];
+ }
+
+ /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
+ $args = apply_filters( 'users_list_table_query_args', $args );
+
+ // Query the user IDs for this page.
+ $wp_user_search = new WP_User_Query( $args );
+
+ $this->items = $wp_user_search->get_results();
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $wp_user_search->get_total(),
+ 'per_page' => $users_per_page,
+ )
+ );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ $actions = array();
+ if ( current_user_can( 'delete_users' ) ) {
+ $actions['delete'] = __( 'Delete' );
+ }
+ $actions['spam'] = _x( 'Mark as spam', 'user' );
+ $actions['notspam'] = _x( 'Not spam', 'user' );
+
+ return $actions;
+ }
+
+ /**
+ */
+ public function no_items() {
+ _e( 'No users found.' );
+ }
+
+ /**
+ * @global string $role
+ * @return array
+ */
+ protected function get_views() {
+ global $role;
+
+ $total_users = get_user_count();
+ $super_admins = get_super_admins();
+ $total_admins = count( $super_admins );
+
+ $role_links = array();
+ $role_links['all'] = array(
+ 'url' => network_admin_url( 'users.php' ),
+ 'label' => sprintf(
+ /* translators: Number of users. */
+ _nx(
+ 'All (%s) ',
+ 'All (%s) ',
+ $total_users,
+ 'users'
+ ),
+ number_format_i18n( $total_users )
+ ),
+ 'current' => 'super' !== $role,
+ );
+
+ $role_links['super'] = array(
+ 'url' => network_admin_url( 'users.php?role=super' ),
+ 'label' => sprintf(
+ /* translators: Number of users. */
+ _n(
+ 'Super Admin (%s) ',
+ 'Super Admins (%s) ',
+ $total_admins
+ ),
+ number_format_i18n( $total_admins )
+ ),
+ 'current' => 'super' === $role,
+ );
+
+ return $this->get_views_links( $role_links );
+ }
+
+ /**
+ * @global string $mode List table view mode.
+ *
+ * @param string $which
+ */
+ protected function pagination( $which ) {
+ global $mode;
+
+ parent::pagination( $which );
+
+ if ( 'top' === $which ) {
+ $this->view_switcher( $mode );
+ }
+ }
+
+ /**
+ * @return string[] Array of column titles keyed by their column name.
+ */
+ public function get_columns() {
+ $users_columns = array(
+ 'cb' => ' ',
+ 'username' => __( 'Username' ),
+ 'name' => __( 'Name' ),
+ 'email' => __( 'Email' ),
+ 'registered' => _x( 'Registered', 'user' ),
+ 'blogs' => __( 'Sites' ),
+ );
+ /**
+ * Filters the columns displayed in the Network Admin Users list table.
+ *
+ * @since MU (3.0.0)
+ *
+ * @param string[] $users_columns An array of user columns. Default 'cb', 'username',
+ * 'name', 'email', 'registered', 'blogs'.
+ */
+ return apply_filters( 'wpmu_users_columns', $users_columns );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_sortable_columns() {
+ return array(
+ 'username' => array( 'login', false, __( 'Username' ), __( 'Table ordered by Username.' ), 'asc' ),
+ 'name' => array( 'name', false, __( 'Name' ), __( 'Table ordered by Name.' ) ),
+ 'email' => array( 'email', false, __( 'E-mail' ), __( 'Table ordered by E-mail.' ) ),
+ 'registered' => array( 'id', false, _x( 'Registered', 'user' ), __( 'Table ordered by User Registered Date.' ) ),
+ );
+ }
+
+ /**
+ * Handles the checkbox column output.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_User $item The current WP_User object.
+ */
+ public function column_cb( $item ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $user = $item;
+
+ if ( is_super_admin( $user->ID ) ) {
+ return;
+ }
+ ?>
+
+
+
+ user_login );
+ ?>
+
+
+ ID;
+ }
+
+ /**
+ * Handles the username column output.
+ *
+ * @since 4.3.0
+ *
+ * @param WP_User $user The current WP_User object.
+ */
+ public function column_username( $user ) {
+ $super_admins = get_super_admins();
+ $avatar = get_avatar( $user->user_email, 32 );
+
+ echo $avatar;
+
+ if ( current_user_can( 'edit_user', $user->ID ) ) {
+ $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
+ $edit = "{$user->user_login} ";
+ } else {
+ $edit = $user->user_login;
+ }
+
+ ?>
+
+ user_login, $super_admins, true ) ) {
+ echo ' — ' . __( 'Super Admin' );
+ }
+ ?>
+
+ first_name && $user->last_name ) {
+ printf(
+ /* translators: 1: User's first name, 2: Last name. */
+ _x( '%1$s %2$s', 'Display name based on first name and last name' ),
+ $user->first_name,
+ $user->last_name
+ );
+ } elseif ( $user->first_name ) {
+ echo $user->first_name;
+ } elseif ( $user->last_name ) {
+ echo $user->last_name;
+ } else {
+ echo '— ' .
+ /* translators: Hidden accessibility text. */
+ _x( 'Unknown', 'name' ) .
+ ' ';
+ }
+ }
+
+ /**
+ * Handles the email column output.
+ *
+ * @since 4.3.0
+ *
+ * @param WP_User $user The current WP_User object.
+ */
+ public function column_email( $user ) {
+ echo "$user->user_email ";
+ }
+
+ /**
+ * Handles the registered date column output.
+ *
+ * @since 4.3.0
+ *
+ * @global string $mode List table view mode.
+ *
+ * @param WP_User $user The current WP_User object.
+ */
+ public function column_registered( $user ) {
+ global $mode;
+ if ( 'list' === $mode ) {
+ $date = __( 'Y/m/d' );
+ } else {
+ $date = __( 'Y/m/d g:i:s a' );
+ }
+ echo mysql2date( $date, $user->user_registered );
+ }
+
+ /**
+ * @since 4.3.0
+ *
+ * @param WP_User $user
+ * @param string $classes
+ * @param string $data
+ * @param string $primary
+ */
+ protected function _column_blogs( $user, $classes, $data, $primary ) {
+ echo '';
+ echo $this->column_blogs( $user );
+ echo $this->handle_row_actions( $user, 'blogs', $primary );
+ echo ' ';
+ }
+
+ /**
+ * Handles the sites column output.
+ *
+ * @since 4.3.0
+ *
+ * @param WP_User $user The current WP_User object.
+ */
+ public function column_blogs( $user ) {
+ $blogs = get_blogs_of_user( $user->ID, true );
+ if ( ! is_array( $blogs ) ) {
+ return;
+ }
+
+ foreach ( $blogs as $site ) {
+ if ( ! can_edit_network( $site->site_id ) ) {
+ continue;
+ }
+
+ $path = ( '/' === $site->path ) ? '' : $site->path;
+ $site_classes = array( 'site-' . $site->site_id );
+ /**
+ * Filters the span class for a site listing on the mulisite user list table.
+ *
+ * @since 5.2.0
+ *
+ * @param string[] $site_classes Array of class names used within the span tag. Default "site-#" with the site's network ID.
+ * @param int $site_id Site ID.
+ * @param int $network_id Network ID.
+ * @param WP_User $user WP_User object.
+ */
+ $site_classes = apply_filters( 'ms_user_list_site_class', $site_classes, $site->userblog_id, $site->site_id, $user );
+ if ( is_array( $site_classes ) && ! empty( $site_classes ) ) {
+ $site_classes = array_map( 'sanitize_html_class', array_unique( $site_classes ) );
+ echo '';
+ } else {
+ echo '';
+ }
+ echo '' . str_replace( '.' . get_network()->domain, '', $site->domain . $path ) . ' ';
+ echo ' ';
+ $actions = array();
+ $actions['edit'] = '' . __( 'Edit' ) . ' ';
+
+ $class = '';
+ if ( 1 === (int) $site->spam ) {
+ $class .= 'site-spammed ';
+ }
+ if ( 1 === (int) $site->mature ) {
+ $class .= 'site-mature ';
+ }
+ if ( 1 === (int) $site->deleted ) {
+ $class .= 'site-deleted ';
+ }
+ if ( 1 === (int) $site->archived ) {
+ $class .= 'site-archived ';
+ }
+
+ $actions['view'] = '' . __( 'View' ) . ' ';
+
+ /**
+ * Filters the action links displayed next the sites a user belongs to
+ * in the Network Admin Users list table.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $actions An array of action links to be displayed. Default 'Edit', 'View'.
+ * @param int $userblog_id The site ID.
+ */
+ $actions = apply_filters( 'ms_user_list_site_actions', $actions, $site->userblog_id );
+
+ $action_count = count( $actions );
+
+ $i = 0;
+
+ foreach ( $actions as $action => $link ) {
+ ++$i;
+
+ $separator = ( $i < $action_count ) ? ' | ' : '';
+
+ echo "{$link}{$separator} ";
+ }
+
+ echo ' ';
+ }
+ }
+
+ /**
+ * Handles the default column output.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_User $item The current WP_User object.
+ * @param string $column_name The current column name.
+ */
+ public function column_default( $item, $column_name ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $user = $item;
+
+ /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
+ echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
+ }
+
+ public function display_rows() {
+ foreach ( $this->items as $user ) {
+ $class = '';
+
+ $status_list = array(
+ 'spam' => 'site-spammed',
+ 'deleted' => 'site-deleted',
+ );
+
+ foreach ( $status_list as $status => $col ) {
+ if ( $user->$status ) {
+ $class .= " $col";
+ }
+ }
+
+ ?>
+
+ single_row_columns( $user ); ?>
+
+ ID ) ) {
+ $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
+ $actions['edit'] = '' . __( 'Edit' ) . ' ';
+ }
+
+ if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins, true ) ) {
+ $actions['delete'] = '' . __( 'Delete' ) . ' ';
+ }
+
+ /**
+ * Filters the action links displayed under each user in the Network Admin Users list table.
+ *
+ * @since 3.2.0
+ *
+ * @param string[] $actions An array of action links to be displayed. Default 'Edit', 'Delete'.
+ * @param WP_User $user WP_User object.
+ */
+ $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
+
+ return $this->row_actions( $actions );
+ }
+}
diff --git a/wp-admin/includes/class-wp-plugin-install-list-table.php b/wp-admin/includes/class-wp-plugin-install-list-table.php
new file mode 100644
index 0000000..7823f00
--- /dev/null
+++ b/wp-admin/includes/class-wp-plugin-install-list-table.php
@@ -0,0 +1,831 @@
+no_update ) ) {
+ foreach ( $plugin_info->no_update as $plugin ) {
+ if ( isset( $plugin->slug ) ) {
+ $plugin->upgrade = false;
+ $plugins[ $plugin->slug ] = $plugin;
+ }
+ }
+ }
+
+ if ( isset( $plugin_info->response ) ) {
+ foreach ( $plugin_info->response as $plugin ) {
+ if ( isset( $plugin->slug ) ) {
+ $plugin->upgrade = true;
+ $plugins[ $plugin->slug ] = $plugin;
+ }
+ }
+ }
+
+ return $plugins;
+ }
+
+ /**
+ * Returns a list of slugs of installed plugins, if known.
+ *
+ * Uses the transient data from the updates API to determine the slugs of
+ * known installed plugins. This might be better elsewhere, perhaps even
+ * within get_plugins().
+ *
+ * @since 4.0.0
+ *
+ * @return array
+ */
+ protected function get_installed_plugin_slugs() {
+ return array_keys( $this->get_installed_plugins() );
+ }
+
+ /**
+ * @global array $tabs
+ * @global string $tab
+ * @global int $paged
+ * @global string $type
+ * @global string $term
+ */
+ public function prepare_items() {
+ require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
+
+ global $tabs, $tab, $paged, $type, $term;
+
+ wp_reset_vars( array( 'tab' ) );
+
+ $paged = $this->get_pagenum();
+
+ $per_page = 36;
+
+ // These are the tabs which are shown on the page.
+ $tabs = array();
+
+ if ( 'search' === $tab ) {
+ $tabs['search'] = __( 'Search Results' );
+ }
+
+ if ( 'beta' === $tab || str_contains( get_bloginfo( 'version' ), '-' ) ) {
+ $tabs['beta'] = _x( 'Beta Testing', 'Plugin Installer' );
+ }
+
+ $tabs['featured'] = _x( 'Featured', 'Plugin Installer' );
+ $tabs['popular'] = _x( 'Popular', 'Plugin Installer' );
+ $tabs['recommended'] = _x( 'Recommended', 'Plugin Installer' );
+ $tabs['favorites'] = _x( 'Favorites', 'Plugin Installer' );
+
+ if ( current_user_can( 'upload_plugins' ) ) {
+ /*
+ * No longer a real tab. Here for filter compatibility.
+ * Gets skipped in get_views().
+ */
+ $tabs['upload'] = __( 'Upload Plugin' );
+ }
+
+ $nonmenu_tabs = array( 'plugin-information' ); // Valid actions to perform which do not have a Menu item.
+
+ /**
+ * Filters the tabs shown on the Add Plugins screen.
+ *
+ * @since 2.7.0
+ *
+ * @param string[] $tabs The tabs shown on the Add Plugins screen. Defaults include
+ * 'featured', 'popular', 'recommended', 'favorites', and 'upload'.
+ */
+ $tabs = apply_filters( 'install_plugins_tabs', $tabs );
+
+ /**
+ * Filters tabs not associated with a menu item on the Add Plugins screen.
+ *
+ * @since 2.7.0
+ *
+ * @param string[] $nonmenu_tabs The tabs that don't have a menu item on the Add Plugins screen.
+ */
+ $nonmenu_tabs = apply_filters( 'install_plugins_nonmenu_tabs', $nonmenu_tabs );
+
+ // If a non-valid menu tab has been selected, And it's not a non-menu action.
+ if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs, true ) ) ) {
+ $tab = key( $tabs );
+ }
+
+ $installed_plugins = $this->get_installed_plugins();
+
+ $args = array(
+ 'page' => $paged,
+ 'per_page' => $per_page,
+ // Send the locale to the API so it can provide context-sensitive results.
+ 'locale' => get_user_locale(),
+ );
+
+ switch ( $tab ) {
+ case 'search':
+ $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
+ $term = isset( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : '';
+
+ switch ( $type ) {
+ case 'tag':
+ $args['tag'] = sanitize_title_with_dashes( $term );
+ break;
+ case 'term':
+ $args['search'] = $term;
+ break;
+ case 'author':
+ $args['author'] = $term;
+ break;
+ }
+
+ break;
+
+ case 'featured':
+ case 'popular':
+ case 'new':
+ case 'beta':
+ $args['browse'] = $tab;
+ break;
+ case 'recommended':
+ $args['browse'] = $tab;
+ // Include the list of installed plugins so we can get relevant results.
+ $args['installed_plugins'] = array_keys( $installed_plugins );
+ break;
+
+ case 'favorites':
+ $action = 'save_wporg_username_' . get_current_user_id();
+ if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), $action ) ) {
+ $user = isset( $_GET['user'] ) ? wp_unslash( $_GET['user'] ) : get_user_option( 'wporg_favorites' );
+
+ // If the save url parameter is passed with a falsey value, don't save the favorite user.
+ if ( ! isset( $_GET['save'] ) || $_GET['save'] ) {
+ update_user_meta( get_current_user_id(), 'wporg_favorites', $user );
+ }
+ } else {
+ $user = get_user_option( 'wporg_favorites' );
+ }
+ if ( $user ) {
+ $args['user'] = $user;
+ } else {
+ $args = false;
+ }
+
+ add_action( 'install_plugins_favorites', 'install_plugins_favorites_form', 9, 0 );
+ break;
+
+ default:
+ $args = false;
+ break;
+ }
+
+ /**
+ * Filters API request arguments for each Add Plugins screen tab.
+ *
+ * The dynamic portion of the hook name, `$tab`, refers to the plugin install tabs.
+ *
+ * Possible hook names include:
+ *
+ * - `install_plugins_table_api_args_favorites`
+ * - `install_plugins_table_api_args_featured`
+ * - `install_plugins_table_api_args_popular`
+ * - `install_plugins_table_api_args_recommended`
+ * - `install_plugins_table_api_args_upload`
+ * - `install_plugins_table_api_args_search`
+ * - `install_plugins_table_api_args_beta`
+ *
+ * @since 3.7.0
+ *
+ * @param array|false $args Plugin install API arguments.
+ */
+ $args = apply_filters( "install_plugins_table_api_args_{$tab}", $args );
+
+ if ( ! $args ) {
+ return;
+ }
+
+ $api = plugins_api( 'query_plugins', $args );
+
+ if ( is_wp_error( $api ) ) {
+ $this->error = $api;
+ return;
+ }
+
+ $this->items = $api->plugins;
+
+ if ( $this->orderby ) {
+ uasort( $this->items, array( $this, 'order_callback' ) );
+ }
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $api->info['results'],
+ 'per_page' => $args['per_page'],
+ )
+ );
+
+ if ( isset( $api->info['groups'] ) ) {
+ $this->groups = $api->info['groups'];
+ }
+
+ if ( $installed_plugins ) {
+ $js_plugins = array_fill_keys(
+ array( 'all', 'search', 'active', 'inactive', 'recently_activated', 'mustuse', 'dropins' ),
+ array()
+ );
+
+ $js_plugins['all'] = array_values( wp_list_pluck( $installed_plugins, 'plugin' ) );
+ $upgrade_plugins = wp_filter_object_list( $installed_plugins, array( 'upgrade' => true ), 'and', 'plugin' );
+
+ if ( $upgrade_plugins ) {
+ $js_plugins['upgrade'] = array_values( $upgrade_plugins );
+ }
+
+ wp_localize_script(
+ 'updates',
+ '_wpUpdatesItemCounts',
+ array(
+ 'plugins' => $js_plugins,
+ 'totals' => wp_get_update_data(),
+ )
+ );
+ }
+ }
+
+ /**
+ */
+ public function no_items() {
+ if ( isset( $this->error ) ) {
+ $error_message = '' . $this->error->get_error_message() . '
';
+ $error_message .= '' . __( 'Try Again' ) . '
';
+ wp_admin_notice(
+ $error_message,
+ array(
+ 'additional_classes' => array( 'inline', 'error' ),
+ 'paragraph_wrap' => false,
+ )
+ );
+ ?>
+
+
+ $text ) {
+ $display_tabs[ 'plugin-install-' . $action ] = array(
+ 'url' => self_admin_url( 'plugin-install.php?tab=' . $action ),
+ 'label' => $text,
+ 'current' => $action === $tab,
+ );
+ }
+ // No longer a real tab.
+ unset( $display_tabs['plugin-install-upload'] );
+
+ return $this->get_views_links( $display_tabs );
+ }
+
+ /**
+ * Overrides parent views so we can use the filter bar display.
+ */
+ public function views() {
+ $views = $this->get_views();
+
+ /** This filter is documented in wp-admin/includes/class-wp-list-table.php */
+ $views = apply_filters( "views_{$this->screen->id}", $views );
+
+ $this->screen->render_screen_reader_content( 'heading_views' );
+ ?>
+
+
+ $view ) {
+ $views[ $class ] = "\t$view";
+ }
+ echo implode( " \n", $views ) . "\n";
+ }
+ ?>
+
+
+
+
+ _args['singular'];
+
+ $data_attr = '';
+
+ if ( $singular ) {
+ $data_attr = " data-wp-lists='list:$singular'";
+ }
+
+ $this->display_tablenav( 'top' );
+
+ ?>
+
+ screen->render_screen_reader_content( 'heading_list' );
+ ?>
+
>
+ display_rows_or_placeholder(); ?>
+
+
+ display_tablenav( 'bottom' );
+ }
+
+ /**
+ * @global string $tab
+ *
+ * @param string $which
+ */
+ protected function display_tablenav( $which ) {
+ if ( 'featured' === $GLOBALS['tab'] ) {
+ return;
+ }
+
+ if ( 'top' === $which ) {
+ wp_referer_field();
+ ?>
+
+
+
+
+ pagination( $which ); ?>
+
+
+
+
+ pagination( $which ); ?>
+
+
+ _args['plural'] );
+ }
+
+ /**
+ * @return string[] Array of column titles keyed by their column name.
+ */
+ public function get_columns() {
+ return array();
+ }
+
+ /**
+ * @param object $plugin_a
+ * @param object $plugin_b
+ * @return int
+ */
+ private function order_callback( $plugin_a, $plugin_b ) {
+ $orderby = $this->orderby;
+ if ( ! isset( $plugin_a->$orderby, $plugin_b->$orderby ) ) {
+ return 0;
+ }
+
+ $a = $plugin_a->$orderby;
+ $b = $plugin_b->$orderby;
+
+ if ( $a === $b ) {
+ return 0;
+ }
+
+ if ( 'DESC' === $this->order ) {
+ return ( $a < $b ) ? 1 : -1;
+ } else {
+ return ( $a < $b ) ? -1 : 1;
+ }
+ }
+
+ public function display_rows() {
+ $plugins_allowedtags = array(
+ 'a' => array(
+ 'href' => array(),
+ 'title' => array(),
+ 'target' => array(),
+ ),
+ 'abbr' => array( 'title' => array() ),
+ 'acronym' => array( 'title' => array() ),
+ 'code' => array(),
+ 'pre' => array(),
+ 'em' => array(),
+ 'strong' => array(),
+ 'ul' => array(),
+ 'ol' => array(),
+ 'li' => array(),
+ 'p' => array(),
+ 'br' => array(),
+ );
+
+ $plugins_group_titles = array(
+ 'Performance' => _x( 'Performance', 'Plugin installer group title' ),
+ 'Social' => _x( 'Social', 'Plugin installer group title' ),
+ 'Tools' => _x( 'Tools', 'Plugin installer group title' ),
+ );
+
+ $group = null;
+
+ foreach ( (array) $this->items as $plugin ) {
+ if ( is_object( $plugin ) ) {
+ $plugin = (array) $plugin;
+ }
+
+ // Display the group heading if there is one.
+ if ( isset( $plugin['group'] ) && $plugin['group'] !== $group ) {
+ if ( isset( $this->groups[ $plugin['group'] ] ) ) {
+ $group_name = $this->groups[ $plugin['group'] ];
+ if ( isset( $plugins_group_titles[ $group_name ] ) ) {
+ $group_name = $plugins_group_titles[ $group_name ];
+ }
+ } else {
+ $group_name = $plugin['group'];
+ }
+
+ // Starting a new group, close off the divs of the last one.
+ if ( ! empty( $group ) ) {
+ echo '';
+ }
+
+ echo '' . esc_html( $group_name ) . ' ';
+ // Needs an extra wrapping div for nth-child selectors to work.
+ echo '
';
+
+ $group = $plugin['group'];
+ }
+
+ $title = wp_kses( $plugin['name'], $plugins_allowedtags );
+
+ // Remove any HTML from the description.
+ $description = strip_tags( $plugin['short_description'] );
+
+ /**
+ * Filters the plugin card description on the Add Plugins screen.
+ *
+ * @since 6.0.0
+ *
+ * @param string $description Plugin card description.
+ * @param array $plugin An array of plugin data. See {@see plugins_api()}
+ * for the list of possible values.
+ */
+ $description = apply_filters( 'plugin_install_description', $description, $plugin );
+
+ $version = wp_kses( $plugin['version'], $plugins_allowedtags );
+
+ $name = strip_tags( $title . ' ' . $version );
+
+ $author = wp_kses( $plugin['author'], $plugins_allowedtags );
+ if ( ! empty( $author ) ) {
+ /* translators: %s: Plugin author. */
+ $author = '
' . sprintf( __( 'By %s' ), $author ) . ' ';
+ }
+
+ $requires_php = isset( $plugin['requires_php'] ) ? $plugin['requires_php'] : null;
+ $requires_wp = isset( $plugin['requires'] ) ? $plugin['requires'] : null;
+
+ $compatible_php = is_php_version_compatible( $requires_php );
+ $compatible_wp = is_wp_version_compatible( $requires_wp );
+ $tested_wp = ( empty( $plugin['tested'] ) || version_compare( get_bloginfo( 'version' ), $plugin['tested'], '<=' ) );
+
+ $action_links = array();
+
+ if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
+ $status = install_plugin_install_status( $plugin );
+
+ switch ( $status['status'] ) {
+ case 'install':
+ if ( $status['url'] ) {
+ if ( $compatible_php && $compatible_wp ) {
+ $action_links[] = sprintf(
+ '
%s ',
+ esc_attr( $plugin['slug'] ),
+ esc_url( $status['url'] ),
+ /* translators: %s: Plugin name and version. */
+ esc_attr( sprintf( _x( 'Install %s now', 'plugin' ), $name ) ),
+ esc_attr( $name ),
+ __( 'Install Now' )
+ );
+ } else {
+ $action_links[] = sprintf(
+ '
%s ',
+ _x( 'Cannot Install', 'plugin' )
+ );
+ }
+ }
+ break;
+
+ case 'update_available':
+ if ( $status['url'] ) {
+ if ( $compatible_php && $compatible_wp ) {
+ $action_links[] = sprintf(
+ '
%s ',
+ esc_attr( $status['file'] ),
+ esc_attr( $plugin['slug'] ),
+ esc_url( $status['url'] ),
+ /* translators: %s: Plugin name and version. */
+ esc_attr( sprintf( _x( 'Update %s now', 'plugin' ), $name ) ),
+ esc_attr( $name ),
+ __( 'Update Now' )
+ );
+ } else {
+ $action_links[] = sprintf(
+ '
%s ',
+ _x( 'Cannot Update', 'plugin' )
+ );
+ }
+ }
+ break;
+
+ case 'latest_installed':
+ case 'newer_installed':
+ if ( is_plugin_active( $status['file'] ) ) {
+ $action_links[] = sprintf(
+ '
%s ',
+ _x( 'Active', 'plugin' )
+ );
+ } elseif ( current_user_can( 'activate_plugin', $status['file'] ) ) {
+ if ( $compatible_php && $compatible_wp ) {
+ $button_text = __( 'Activate' );
+ /* translators: %s: Plugin name. */
+ $button_label = _x( 'Activate %s', 'plugin' );
+ $activate_url = add_query_arg(
+ array(
+ '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ),
+ 'action' => 'activate',
+ 'plugin' => $status['file'],
+ ),
+ network_admin_url( 'plugins.php' )
+ );
+
+ if ( is_network_admin() ) {
+ $button_text = __( 'Network Activate' );
+ /* translators: %s: Plugin name. */
+ $button_label = _x( 'Network Activate %s', 'plugin' );
+ $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url );
+ }
+
+ $action_links[] = sprintf(
+ '
%3$s ',
+ esc_url( $activate_url ),
+ esc_attr( sprintf( $button_label, $plugin['name'] ) ),
+ $button_text
+ );
+ } else {
+ $action_links[] = sprintf(
+ '
%s ',
+ _x( 'Cannot Activate', 'plugin' )
+ );
+ }
+ } else {
+ $action_links[] = sprintf(
+ '
%s ',
+ _x( 'Installed', 'plugin' )
+ );
+ }
+ break;
+ }
+ }
+
+ $details_link = self_admin_url(
+ 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] .
+ '&TB_iframe=true&width=600&height=550'
+ );
+
+ $action_links[] = sprintf(
+ '
%s ',
+ esc_url( $details_link ),
+ /* translators: %s: Plugin name and version. */
+ esc_attr( sprintf( __( 'More information about %s' ), $name ) ),
+ esc_attr( $name ),
+ __( 'More Details' )
+ );
+
+ if ( ! empty( $plugin['icons']['svg'] ) ) {
+ $plugin_icon_url = $plugin['icons']['svg'];
+ } elseif ( ! empty( $plugin['icons']['2x'] ) ) {
+ $plugin_icon_url = $plugin['icons']['2x'];
+ } elseif ( ! empty( $plugin['icons']['1x'] ) ) {
+ $plugin_icon_url = $plugin['icons']['1x'];
+ } else {
+ $plugin_icon_url = $plugin['icons']['default'];
+ }
+
+ /**
+ * Filters the install action links for a plugin.
+ *
+ * @since 2.7.0
+ *
+ * @param string[] $action_links An array of plugin action links.
+ * Defaults are links to Details and Install Now.
+ * @param array $plugin An array of plugin data. See {@see plugins_api()}
+ * for the list of possible values.
+ */
+ $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );
+
+ $last_updated_timestamp = strtotime( $plugin['last_updated'] );
+ ?>
+
+ Please update WordPress, and then
learn more about updating PHP .' ),
+ self_admin_url( 'update-core.php' ),
+ esc_url( wp_get_update_php_url() )
+ );
+ $incompatible_notice_message .= wp_update_php_annotation( '
', ' ', false );
+ } elseif ( current_user_can( 'update_core' ) ) {
+ $incompatible_notice_message .= sprintf(
+ /* translators: %s: URL to WordPress Updates screen. */
+ ' ' . __( 'Please update WordPress .' ),
+ self_admin_url( 'update-core.php' )
+ );
+ } elseif ( current_user_can( 'update_php' ) ) {
+ $incompatible_notice_message .= sprintf(
+ /* translators: %s: URL to Update PHP page. */
+ ' ' . __( 'Learn more about updating PHP .' ),
+ esc_url( wp_get_update_php_url() )
+ );
+ $incompatible_notice_message .= wp_update_php_annotation( '
', ' ', false );
+ }
+ } elseif ( ! $compatible_wp ) {
+ $incompatible_notice_message .= __( 'This plugin does not work with your version of WordPress.' );
+ if ( current_user_can( 'update_core' ) ) {
+ $incompatible_notice_message .= printf(
+ /* translators: %s: URL to WordPress Updates screen. */
+ ' ' . __( 'Please update WordPress .' ),
+ self_admin_url( 'update-core.php' )
+ );
+ }
+ } elseif ( ! $compatible_php ) {
+ $incompatible_notice_message .= __( 'This plugin does not work with your version of PHP.' );
+ if ( current_user_can( 'update_php' ) ) {
+ $incompatible_notice_message .= sprintf(
+ /* translators: %s: URL to Update PHP page. */
+ ' ' . __( 'Learn more about updating PHP .' ),
+ esc_url( wp_get_update_php_url() )
+ );
+ $incompatible_notice_message .= wp_update_php_annotation( '
', ' ', false );
+ }
+ }
+
+ wp_admin_notice(
+ $incompatible_notice_message,
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'inline' ),
+ )
+ );
+ }
+ ?>
+
+
+
+
' . implode( ' ', $action_links ) . ' ';
+ }
+ ?>
+
+
+
+
+
+ $plugin['rating'],
+ 'type' => 'percent',
+ 'number' => $plugin['num_ratings'],
+ )
+ );
+ ?>
+ ()
+
+
+
+
+
+
+ = 1000000 ) {
+ $active_installs_millions = floor( $plugin['active_installs'] / 1000000 );
+ $active_installs_text = sprintf(
+ /* translators: %s: Number of millions. */
+ _nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ),
+ number_format_i18n( $active_installs_millions )
+ );
+ } elseif ( 0 === $plugin['active_installs'] ) {
+ $active_installs_text = _x( 'Less Than 10', 'Active plugin installations' );
+ } else {
+ $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+';
+ }
+ /* translators: %s: Number of installations. */
+ printf( __( '%s Active Installations' ), $active_installs_text );
+ ?>
+
+
+ ' . __( 'Untested with your version of WordPress' ) . '';
+ } elseif ( ! $compatible_wp ) {
+ echo '' . __( 'Incompatible with your version of WordPress' ) . ' ';
+ } else {
+ echo '' . __( 'Compatible with your version of WordPress' ) . ' ';
+ }
+ ?>
+
+
+
+
';
+ }
+ }
+}
diff --git a/wp-admin/includes/class-wp-plugins-list-table.php b/wp-admin/includes/class-wp-plugins-list-table.php
new file mode 100644
index 0000000..5c92fba
--- /dev/null
+++ b/wp-admin/includes/class-wp-plugins-list-table.php
@@ -0,0 +1,1394 @@
+ 'plugins',
+ 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
+ )
+ );
+
+ $allowed_statuses = array( 'active', 'inactive', 'recently_activated', 'upgrade', 'mustuse', 'dropins', 'search', 'paused', 'auto-update-enabled', 'auto-update-disabled' );
+
+ $status = 'all';
+ if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], $allowed_statuses, true ) ) {
+ $status = $_REQUEST['plugin_status'];
+ }
+
+ if ( isset( $_REQUEST['s'] ) ) {
+ $_SERVER['REQUEST_URI'] = add_query_arg( 's', wp_unslash( $_REQUEST['s'] ) );
+ }
+
+ $page = $this->get_pagenum();
+
+ $this->show_autoupdates = wp_is_auto_update_enabled_for_type( 'plugin' )
+ && current_user_can( 'update_plugins' )
+ && ( ! is_multisite() || $this->screen->in_admin( 'network' ) );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_table_classes() {
+ return array( 'widefat', $this->_args['plural'] );
+ }
+
+ /**
+ * @return bool
+ */
+ public function ajax_user_can() {
+ return current_user_can( 'activate_plugins' );
+ }
+
+ /**
+ * @global string $status
+ * @global array $plugins
+ * @global array $totals
+ * @global int $page
+ * @global string $orderby
+ * @global string $order
+ * @global string $s
+ */
+ public function prepare_items() {
+ global $status, $plugins, $totals, $page, $orderby, $order, $s;
+
+ wp_reset_vars( array( 'orderby', 'order' ) );
+
+ /**
+ * Filters the full array of plugins to list in the Plugins list table.
+ *
+ * @since 3.0.0
+ *
+ * @see get_plugins()
+ *
+ * @param array $all_plugins An array of plugins to display in the list table.
+ */
+ $all_plugins = apply_filters( 'all_plugins', get_plugins() );
+
+ $plugins = array(
+ 'all' => $all_plugins,
+ 'search' => array(),
+ 'active' => array(),
+ 'inactive' => array(),
+ 'recently_activated' => array(),
+ 'upgrade' => array(),
+ 'mustuse' => array(),
+ 'dropins' => array(),
+ 'paused' => array(),
+ );
+ if ( $this->show_autoupdates ) {
+ $auto_updates = (array) get_site_option( 'auto_update_plugins', array() );
+
+ $plugins['auto-update-enabled'] = array();
+ $plugins['auto-update-disabled'] = array();
+ }
+
+ $screen = $this->screen;
+
+ if ( ! is_multisite() || ( $screen->in_admin( 'network' ) && current_user_can( 'manage_network_plugins' ) ) ) {
+
+ /**
+ * Filters whether to display the advanced plugins list table.
+ *
+ * There are two types of advanced plugins - must-use and drop-ins -
+ * which can be used in a single site or Multisite network.
+ *
+ * The $type parameter allows you to differentiate between the type of advanced
+ * plugins to filter the display of. Contexts include 'mustuse' and 'dropins'.
+ *
+ * @since 3.0.0
+ *
+ * @param bool $show Whether to show the advanced plugins for the specified
+ * plugin type. Default true.
+ * @param string $type The plugin type. Accepts 'mustuse', 'dropins'.
+ */
+ if ( apply_filters( 'show_advanced_plugins', true, 'mustuse' ) ) {
+ $plugins['mustuse'] = get_mu_plugins();
+ }
+
+ /** This action is documented in wp-admin/includes/class-wp-plugins-list-table.php */
+ if ( apply_filters( 'show_advanced_plugins', true, 'dropins' ) ) {
+ $plugins['dropins'] = get_dropins();
+ }
+
+ if ( current_user_can( 'update_plugins' ) ) {
+ $current = get_site_transient( 'update_plugins' );
+ foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
+ if ( isset( $current->response[ $plugin_file ] ) ) {
+ $plugins['all'][ $plugin_file ]['update'] = true;
+ $plugins['upgrade'][ $plugin_file ] = $plugins['all'][ $plugin_file ];
+ }
+ }
+ }
+ }
+
+ if ( ! $screen->in_admin( 'network' ) ) {
+ $show = current_user_can( 'manage_network_plugins' );
+ /**
+ * Filters whether to display network-active plugins alongside plugins active for the current site.
+ *
+ * This also controls the display of inactive network-only plugins (plugins with
+ * "Network: true" in the plugin header).
+ *
+ * Plugins cannot be network-activated or network-deactivated from this screen.
+ *
+ * @since 4.4.0
+ *
+ * @param bool $show Whether to show network-active plugins. Default is whether the current
+ * user can manage network plugins (ie. a Super Admin).
+ */
+ $show_network_active = apply_filters( 'show_network_active_plugins', $show );
+ }
+
+ if ( $screen->in_admin( 'network' ) ) {
+ $recently_activated = get_site_option( 'recently_activated', array() );
+ } else {
+ $recently_activated = get_option( 'recently_activated', array() );
+ }
+
+ foreach ( $recently_activated as $key => $time ) {
+ if ( $time + WEEK_IN_SECONDS < time() ) {
+ unset( $recently_activated[ $key ] );
+ }
+ }
+
+ if ( $screen->in_admin( 'network' ) ) {
+ update_site_option( 'recently_activated', $recently_activated );
+ } else {
+ update_option( 'recently_activated', $recently_activated );
+ }
+
+ $plugin_info = get_site_transient( 'update_plugins' );
+
+ foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) {
+ // Extra info if known. array_merge() ensures $plugin_data has precedence if keys collide.
+ if ( isset( $plugin_info->response[ $plugin_file ] ) ) {
+ $plugin_data = array_merge( (array) $plugin_info->response[ $plugin_file ], array( 'update-supported' => true ), $plugin_data );
+ } elseif ( isset( $plugin_info->no_update[ $plugin_file ] ) ) {
+ $plugin_data = array_merge( (array) $plugin_info->no_update[ $plugin_file ], array( 'update-supported' => true ), $plugin_data );
+ } elseif ( empty( $plugin_data['update-supported'] ) ) {
+ $plugin_data['update-supported'] = false;
+ }
+
+ /*
+ * Create the payload that's used for the auto_update_plugin filter.
+ * This is the same data contained within $plugin_info->(response|no_update) however
+ * not all plugins will be contained in those keys, this avoids unexpected warnings.
+ */
+ $filter_payload = array(
+ 'id' => $plugin_file,
+ 'slug' => '',
+ 'plugin' => $plugin_file,
+ 'new_version' => '',
+ 'url' => '',
+ 'package' => '',
+ 'icons' => array(),
+ 'banners' => array(),
+ 'banners_rtl' => array(),
+ 'tested' => '',
+ 'requires_php' => '',
+ 'compatibility' => new stdClass(),
+ );
+
+ $filter_payload = (object) wp_parse_args( $plugin_data, $filter_payload );
+
+ $auto_update_forced = wp_is_auto_update_forced_for_item( 'plugin', null, $filter_payload );
+
+ if ( ! is_null( $auto_update_forced ) ) {
+ $plugin_data['auto-update-forced'] = $auto_update_forced;
+ }
+
+ $plugins['all'][ $plugin_file ] = $plugin_data;
+ // Make sure that $plugins['upgrade'] also receives the extra info since it is used on ?plugin_status=upgrade.
+ if ( isset( $plugins['upgrade'][ $plugin_file ] ) ) {
+ $plugins['upgrade'][ $plugin_file ] = $plugin_data;
+ }
+
+ // Filter into individual sections.
+ if ( is_multisite() && ! $screen->in_admin( 'network' ) && is_network_only_plugin( $plugin_file ) && ! is_plugin_active( $plugin_file ) ) {
+ if ( $show_network_active ) {
+ // On the non-network screen, show inactive network-only plugins if allowed.
+ $plugins['inactive'][ $plugin_file ] = $plugin_data;
+ } else {
+ // On the non-network screen, filter out network-only plugins as long as they're not individually active.
+ unset( $plugins['all'][ $plugin_file ] );
+ }
+ } elseif ( ! $screen->in_admin( 'network' ) && is_plugin_active_for_network( $plugin_file ) ) {
+ if ( $show_network_active ) {
+ // On the non-network screen, show network-active plugins if allowed.
+ $plugins['active'][ $plugin_file ] = $plugin_data;
+ } else {
+ // On the non-network screen, filter out network-active plugins.
+ unset( $plugins['all'][ $plugin_file ] );
+ }
+ } elseif ( ( ! $screen->in_admin( 'network' ) && is_plugin_active( $plugin_file ) )
+ || ( $screen->in_admin( 'network' ) && is_plugin_active_for_network( $plugin_file ) ) ) {
+ /*
+ * On the non-network screen, populate the active list with plugins that are individually activated.
+ * On the network admin screen, populate the active list with plugins that are network-activated.
+ */
+ $plugins['active'][ $plugin_file ] = $plugin_data;
+
+ if ( ! $screen->in_admin( 'network' ) && is_plugin_paused( $plugin_file ) ) {
+ $plugins['paused'][ $plugin_file ] = $plugin_data;
+ }
+ } else {
+ if ( isset( $recently_activated[ $plugin_file ] ) ) {
+ // Populate the recently activated list with plugins that have been recently activated.
+ $plugins['recently_activated'][ $plugin_file ] = $plugin_data;
+ }
+ // Populate the inactive list with plugins that aren't activated.
+ $plugins['inactive'][ $plugin_file ] = $plugin_data;
+ }
+
+ if ( $this->show_autoupdates ) {
+ $enabled = in_array( $plugin_file, $auto_updates, true ) && $plugin_data['update-supported'];
+ if ( isset( $plugin_data['auto-update-forced'] ) ) {
+ $enabled = (bool) $plugin_data['auto-update-forced'];
+ }
+
+ if ( $enabled ) {
+ $plugins['auto-update-enabled'][ $plugin_file ] = $plugin_data;
+ } else {
+ $plugins['auto-update-disabled'][ $plugin_file ] = $plugin_data;
+ }
+ }
+ }
+
+ if ( strlen( $s ) ) {
+ $status = 'search';
+ $plugins['search'] = array_filter( $plugins['all'], array( $this, '_search_callback' ) );
+ }
+
+ /**
+ * Filters the array of plugins for the list table.
+ *
+ * @since 6.3.0
+ *
+ * @param array[] $plugins An array of arrays of plugin data, keyed by context.
+ */
+ $plugins = apply_filters( 'plugins_list', $plugins );
+
+ $totals = array();
+ foreach ( $plugins as $type => $list ) {
+ $totals[ $type ] = count( $list );
+ }
+
+ if ( empty( $plugins[ $status ] ) && ! in_array( $status, array( 'all', 'search' ), true ) ) {
+ $status = 'all';
+ }
+
+ $this->items = array();
+ foreach ( $plugins[ $status ] as $plugin_file => $plugin_data ) {
+ // Translate, don't apply markup, sanitize HTML.
+ $this->items[ $plugin_file ] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true );
+ }
+
+ $total_this_page = $totals[ $status ];
+
+ $js_plugins = array();
+ foreach ( $plugins as $key => $list ) {
+ $js_plugins[ $key ] = array_keys( $list );
+ }
+
+ wp_localize_script(
+ 'updates',
+ '_wpUpdatesItemCounts',
+ array(
+ 'plugins' => $js_plugins,
+ 'totals' => wp_get_update_data(),
+ )
+ );
+
+ if ( ! $orderby ) {
+ $orderby = 'Name';
+ } else {
+ $orderby = ucfirst( $orderby );
+ }
+
+ $order = strtoupper( $order );
+
+ uasort( $this->items, array( $this, '_order_callback' ) );
+
+ $plugins_per_page = $this->get_items_per_page( str_replace( '-', '_', $screen->id . '_per_page' ), 999 );
+
+ $start = ( $page - 1 ) * $plugins_per_page;
+
+ if ( $total_this_page > $plugins_per_page ) {
+ $this->items = array_slice( $this->items, $start, $plugins_per_page );
+ }
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $total_this_page,
+ 'per_page' => $plugins_per_page,
+ )
+ );
+ }
+
+ /**
+ * @global string $s URL encoded search term.
+ *
+ * @param array $plugin
+ * @return bool
+ */
+ public function _search_callback( $plugin ) {
+ global $s;
+
+ foreach ( $plugin as $value ) {
+ if ( is_string( $value ) && false !== stripos( strip_tags( $value ), urldecode( $s ) ) ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * @global string $orderby
+ * @global string $order
+ * @param array $plugin_a
+ * @param array $plugin_b
+ * @return int
+ */
+ public function _order_callback( $plugin_a, $plugin_b ) {
+ global $orderby, $order;
+
+ $a = $plugin_a[ $orderby ];
+ $b = $plugin_b[ $orderby ];
+
+ if ( $a === $b ) {
+ return 0;
+ }
+
+ if ( 'DESC' === $order ) {
+ return strcasecmp( $b, $a );
+ } else {
+ return strcasecmp( $a, $b );
+ }
+ }
+
+ /**
+ * @global array $plugins
+ */
+ public function no_items() {
+ global $plugins;
+
+ if ( ! empty( $_REQUEST['s'] ) ) {
+ $s = esc_html( urldecode( wp_unslash( $_REQUEST['s'] ) ) );
+
+ /* translators: %s: Plugin search term. */
+ printf( __( 'No plugins found for: %s.' ), '
' . $s . ' ' );
+
+ // We assume that somebody who can install plugins in multisite is experienced enough to not need this helper link.
+ if ( ! is_multisite() && current_user_can( 'install_plugins' ) ) {
+ echo '
' . __( 'Search for plugins in the WordPress Plugin Directory.' ) . ' ';
+ }
+ } elseif ( ! empty( $plugins['all'] ) ) {
+ _e( 'No plugins found.' );
+ } else {
+ _e( 'No plugins are currently available.' );
+ }
+ }
+
+ /**
+ * Displays the search box.
+ *
+ * @since 4.6.0
+ *
+ * @param string $text The 'submit' button label.
+ * @param string $input_id ID attribute value for the search input field.
+ */
+ public function search_box( $text, $input_id ) {
+ if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
+ return;
+ }
+
+ $input_id = $input_id . '-search-input';
+
+ if ( ! empty( $_REQUEST['orderby'] ) ) {
+ echo '
';
+ }
+ if ( ! empty( $_REQUEST['order'] ) ) {
+ echo '
';
+ }
+ ?>
+
+ :
+
+ 'search-submit' ) ); ?>
+
+ ! in_array( $status, array( 'mustuse', 'dropins' ), true ) ? '
' : '',
+ 'name' => __( 'Plugin' ),
+ 'description' => __( 'Description' ),
+ );
+
+ if ( $this->show_autoupdates && ! in_array( $status, array( 'mustuse', 'dropins' ), true ) ) {
+ $columns['auto-updates'] = __( 'Automatic Updates' );
+ }
+
+ return $columns;
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_sortable_columns() {
+ return array();
+ }
+
+ /**
+ * @global array $totals
+ * @global string $status
+ * @return array
+ */
+ protected function get_views() {
+ global $totals, $status;
+
+ $status_links = array();
+ foreach ( $totals as $type => $count ) {
+ if ( ! $count ) {
+ continue;
+ }
+
+ switch ( $type ) {
+ case 'all':
+ /* translators: %s: Number of plugins. */
+ $text = _nx(
+ 'All
(%s) ',
+ 'All
(%s) ',
+ $count,
+ 'plugins'
+ );
+ break;
+ case 'active':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Active
(%s) ',
+ 'Active
(%s) ',
+ $count
+ );
+ break;
+ case 'recently_activated':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Recently Active
(%s) ',
+ 'Recently Active
(%s) ',
+ $count
+ );
+ break;
+ case 'inactive':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Inactive
(%s) ',
+ 'Inactive
(%s) ',
+ $count
+ );
+ break;
+ case 'mustuse':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Must-Use
(%s) ',
+ 'Must-Use
(%s) ',
+ $count
+ );
+ break;
+ case 'dropins':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Drop-in
(%s) ',
+ 'Drop-ins
(%s) ',
+ $count
+ );
+ break;
+ case 'paused':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Paused
(%s) ',
+ 'Paused
(%s) ',
+ $count
+ );
+ break;
+ case 'upgrade':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Update Available
(%s) ',
+ 'Update Available
(%s) ',
+ $count
+ );
+ break;
+ case 'auto-update-enabled':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Auto-updates Enabled
(%s) ',
+ 'Auto-updates Enabled
(%s) ',
+ $count
+ );
+ break;
+ case 'auto-update-disabled':
+ /* translators: %s: Number of plugins. */
+ $text = _n(
+ 'Auto-updates Disabled
(%s) ',
+ 'Auto-updates Disabled
(%s) ',
+ $count
+ );
+ break;
+ }
+
+ if ( 'search' !== $type ) {
+ $status_links[ $type ] = array(
+ 'url' => add_query_arg( 'plugin_status', $type, 'plugins.php' ),
+ 'label' => sprintf( $text, number_format_i18n( $count ) ),
+ 'current' => $type === $status,
+ );
+ }
+ }
+
+ return $this->get_views_links( $status_links );
+ }
+
+ /**
+ * @global string $status
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ global $status;
+
+ $actions = array();
+
+ if ( 'active' !== $status ) {
+ $actions['activate-selected'] = $this->screen->in_admin( 'network' ) ? __( 'Network Activate' ) : __( 'Activate' );
+ }
+
+ if ( 'inactive' !== $status && 'recent' !== $status ) {
+ $actions['deactivate-selected'] = $this->screen->in_admin( 'network' ) ? __( 'Network Deactivate' ) : __( 'Deactivate' );
+ }
+
+ if ( ! is_multisite() || $this->screen->in_admin( 'network' ) ) {
+ if ( current_user_can( 'update_plugins' ) ) {
+ $actions['update-selected'] = __( 'Update' );
+ }
+
+ if ( current_user_can( 'delete_plugins' ) && ( 'active' !== $status ) ) {
+ $actions['delete-selected'] = __( 'Delete' );
+ }
+
+ if ( $this->show_autoupdates ) {
+ if ( 'auto-update-enabled' !== $status ) {
+ $actions['enable-auto-update-selected'] = __( 'Enable Auto-updates' );
+ }
+ if ( 'auto-update-disabled' !== $status ) {
+ $actions['disable-auto-update-selected'] = __( 'Disable Auto-updates' );
+ }
+ }
+ }
+
+ return $actions;
+ }
+
+ /**
+ * @global string $status
+ * @param string $which
+ */
+ public function bulk_actions( $which = '' ) {
+ global $status;
+
+ if ( in_array( $status, array( 'mustuse', 'dropins' ), true ) ) {
+ return;
+ }
+
+ parent::bulk_actions( $which );
+ }
+
+ /**
+ * @global string $status
+ * @param string $which
+ */
+ protected function extra_tablenav( $which ) {
+ global $status;
+
+ if ( ! in_array( $status, array( 'recently_activated', 'mustuse', 'dropins' ), true ) ) {
+ return;
+ }
+
+ echo '
';
+
+ if ( 'recently_activated' === $status ) {
+ submit_button( __( 'Clear List' ), '', 'clear-recent-list', false );
+ } elseif ( 'top' === $which && 'mustuse' === $status ) {
+ echo '
' . sprintf(
+ /* translators: %s: mu-plugins directory name. */
+ __( 'Files in the %s directory are executed automatically.' ),
+ '' . str_replace( ABSPATH, '/', WPMU_PLUGIN_DIR ) . '
'
+ ) . '
';
+ } elseif ( 'top' === $which && 'dropins' === $status ) {
+ echo '
' . sprintf(
+ /* translators: %s: wp-content directory name. */
+ __( 'Drop-ins are single files, found in the %s directory, that replace or enhance WordPress features in ways that are not possible for traditional plugins.' ),
+ '' . str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '
'
+ ) . '
';
+ }
+ echo '
';
+ }
+
+ /**
+ * @return string
+ */
+ public function current_action() {
+ if ( isset( $_POST['clear-recent-list'] ) ) {
+ return 'clear-recent-list';
+ }
+
+ return parent::current_action();
+ }
+
+ /**
+ * @global string $status
+ */
+ public function display_rows() {
+ global $status;
+
+ if ( is_multisite() && ! $this->screen->in_admin( 'network' ) && in_array( $status, array( 'mustuse', 'dropins' ), true ) ) {
+ return;
+ }
+
+ foreach ( $this->items as $plugin_file => $plugin_data ) {
+ $this->single_row( array( $plugin_file, $plugin_data ) );
+ }
+ }
+
+ /**
+ * @global string $status
+ * @global int $page
+ * @global string $s
+ * @global array $totals
+ *
+ * @param array $item
+ */
+ public function single_row( $item ) {
+ global $status, $page, $s, $totals;
+ static $plugin_id_attrs = array();
+
+ list( $plugin_file, $plugin_data ) = $item;
+
+ $plugin_slug = isset( $plugin_data['slug'] ) ? $plugin_data['slug'] : sanitize_title( $plugin_data['Name'] );
+ $plugin_id_attr = $plugin_slug;
+
+ // Ensure the ID attribute is unique.
+ $suffix = 2;
+ while ( in_array( $plugin_id_attr, $plugin_id_attrs, true ) ) {
+ $plugin_id_attr = "$plugin_slug-$suffix";
+ ++$suffix;
+ }
+
+ $plugin_id_attrs[] = $plugin_id_attr;
+
+ $context = $status;
+ $screen = $this->screen;
+
+ // Pre-order.
+ $actions = array(
+ 'deactivate' => '',
+ 'activate' => '',
+ 'details' => '',
+ 'delete' => '',
+ );
+
+ // Do not restrict by default.
+ $restrict_network_active = false;
+ $restrict_network_only = false;
+
+ $requires_php = isset( $plugin_data['RequiresPHP'] ) ? $plugin_data['RequiresPHP'] : null;
+ $requires_wp = isset( $plugin_data['RequiresWP'] ) ? $plugin_data['RequiresWP'] : null;
+
+ $compatible_php = is_php_version_compatible( $requires_php );
+ $compatible_wp = is_wp_version_compatible( $requires_wp );
+
+ if ( 'mustuse' === $context ) {
+ $is_active = true;
+ } elseif ( 'dropins' === $context ) {
+ $dropins = _get_dropins();
+ $plugin_name = $plugin_file;
+
+ if ( $plugin_file !== $plugin_data['Name'] ) {
+ $plugin_name .= '
' . $plugin_data['Name'];
+ }
+
+ if ( true === ( $dropins[ $plugin_file ][1] ) ) { // Doesn't require a constant.
+ $is_active = true;
+ $description = '
' . $dropins[ $plugin_file ][0] . '
';
+ } elseif ( defined( $dropins[ $plugin_file ][1] ) && constant( $dropins[ $plugin_file ][1] ) ) { // Constant is true.
+ $is_active = true;
+ $description = '
' . $dropins[ $plugin_file ][0] . '
';
+ } else {
+ $is_active = false;
+ $description = '
' . $dropins[ $plugin_file ][0] . ' ' . __( 'Inactive:' ) . ' ' .
+ sprintf(
+ /* translators: 1: Drop-in constant name, 2: wp-config.php */
+ __( 'Requires %1$s in %2$s file.' ),
+ "define('" . $dropins[ $plugin_file ][1] . "', true);
",
+ 'wp-config.php
'
+ ) . '
';
+ }
+
+ if ( $plugin_data['Description'] ) {
+ $description .= '
' . $plugin_data['Description'] . '
';
+ }
+ } else {
+ if ( $screen->in_admin( 'network' ) ) {
+ $is_active = is_plugin_active_for_network( $plugin_file );
+ } else {
+ $is_active = is_plugin_active( $plugin_file );
+ $restrict_network_active = ( is_multisite() && is_plugin_active_for_network( $plugin_file ) );
+ $restrict_network_only = ( is_multisite() && is_network_only_plugin( $plugin_file ) && ! $is_active );
+ }
+
+ if ( $screen->in_admin( 'network' ) ) {
+ if ( $is_active ) {
+ if ( current_user_can( 'manage_network_plugins' ) ) {
+ $actions['deactivate'] = sprintf(
+ '
%s ',
+ wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . urlencode( $plugin_file ) . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'deactivate-plugin_' . $plugin_file ),
+ esc_attr( $plugin_id_attr ),
+ /* translators: %s: Plugin name. */
+ esc_attr( sprintf( _x( 'Network Deactivate %s', 'plugin' ), $plugin_data['Name'] ) ),
+ __( 'Network Deactivate' )
+ );
+ }
+ } else {
+ if ( current_user_can( 'manage_network_plugins' ) ) {
+ if ( $compatible_php && $compatible_wp ) {
+ $actions['activate'] = sprintf(
+ '
%s ',
+ wp_nonce_url( 'plugins.php?action=activate&plugin=' . urlencode( $plugin_file ) . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'activate-plugin_' . $plugin_file ),
+ esc_attr( $plugin_id_attr ),
+ /* translators: %s: Plugin name. */
+ esc_attr( sprintf( _x( 'Network Activate %s', 'plugin' ), $plugin_data['Name'] ) ),
+ __( 'Network Activate' )
+ );
+ } else {
+ $actions['activate'] = sprintf(
+ '
%s ',
+ _x( 'Cannot Activate', 'plugin' )
+ );
+ }
+ }
+
+ if ( current_user_can( 'delete_plugins' ) && ! is_plugin_active( $plugin_file ) ) {
+ $actions['delete'] = sprintf(
+ '
%s ',
+ wp_nonce_url( 'plugins.php?action=delete-selected&checked[]=' . urlencode( $plugin_file ) . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'bulk-plugins' ),
+ esc_attr( $plugin_id_attr ),
+ /* translators: %s: Plugin name. */
+ esc_attr( sprintf( _x( 'Delete %s', 'plugin' ), $plugin_data['Name'] ) ),
+ __( 'Delete' )
+ );
+ }
+ }
+ } else {
+ if ( $restrict_network_active ) {
+ $actions = array(
+ 'network_active' => __( 'Network Active' ),
+ );
+ } elseif ( $restrict_network_only ) {
+ $actions = array(
+ 'network_only' => __( 'Network Only' ),
+ );
+ } elseif ( $is_active ) {
+ if ( current_user_can( 'deactivate_plugin', $plugin_file ) ) {
+ $actions['deactivate'] = sprintf(
+ '
%s ',
+ wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . urlencode( $plugin_file ) . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'deactivate-plugin_' . $plugin_file ),
+ esc_attr( $plugin_id_attr ),
+ /* translators: %s: Plugin name. */
+ esc_attr( sprintf( _x( 'Deactivate %s', 'plugin' ), $plugin_data['Name'] ) ),
+ __( 'Deactivate' )
+ );
+ }
+
+ if ( current_user_can( 'resume_plugin', $plugin_file ) && is_plugin_paused( $plugin_file ) ) {
+ $actions['resume'] = sprintf(
+ '
%s ',
+ wp_nonce_url( 'plugins.php?action=resume&plugin=' . urlencode( $plugin_file ) . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'resume-plugin_' . $plugin_file ),
+ esc_attr( $plugin_id_attr ),
+ /* translators: %s: Plugin name. */
+ esc_attr( sprintf( _x( 'Resume %s', 'plugin' ), $plugin_data['Name'] ) ),
+ __( 'Resume' )
+ );
+ }
+ } else {
+ if ( current_user_can( 'activate_plugin', $plugin_file ) ) {
+ if ( $compatible_php && $compatible_wp ) {
+ $actions['activate'] = sprintf(
+ '
%s ',
+ wp_nonce_url( 'plugins.php?action=activate&plugin=' . urlencode( $plugin_file ) . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'activate-plugin_' . $plugin_file ),
+ esc_attr( $plugin_id_attr ),
+ /* translators: %s: Plugin name. */
+ esc_attr( sprintf( _x( 'Activate %s', 'plugin' ), $plugin_data['Name'] ) ),
+ __( 'Activate' )
+ );
+ } else {
+ $actions['activate'] = sprintf(
+ '
%s ',
+ _x( 'Cannot Activate', 'plugin' )
+ );
+ }
+ }
+
+ if ( ! is_multisite() && current_user_can( 'delete_plugins' ) ) {
+ $actions['delete'] = sprintf(
+ '
%s ',
+ wp_nonce_url( 'plugins.php?action=delete-selected&checked[]=' . urlencode( $plugin_file ) . '&plugin_status=' . $context . '&paged=' . $page . '&s=' . $s, 'bulk-plugins' ),
+ esc_attr( $plugin_id_attr ),
+ /* translators: %s: Plugin name. */
+ esc_attr( sprintf( _x( 'Delete %s', 'plugin' ), $plugin_data['Name'] ) ),
+ __( 'Delete' )
+ );
+ }
+ } // End if $is_active.
+ } // End if $screen->in_admin( 'network' ).
+ } // End if $context.
+
+ $actions = array_filter( $actions );
+
+ if ( $screen->in_admin( 'network' ) ) {
+
+ /**
+ * Filters the action links displayed for each plugin in the Network Admin Plugins list table.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $actions An array of plugin action links. By default this can include
+ * 'activate', 'deactivate', and 'delete'.
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ * @param array $plugin_data An array of plugin data. See get_plugin_data()
+ * and the {@see 'plugin_row_meta'} filter for the list
+ * of possible values.
+ * @param string $context The plugin context. By default this can include 'all',
+ * 'active', 'inactive', 'recently_activated', 'upgrade',
+ * 'mustuse', 'dropins', and 'search'.
+ */
+ $actions = apply_filters( 'network_admin_plugin_action_links', $actions, $plugin_file, $plugin_data, $context );
+
+ /**
+ * Filters the list of action links displayed for a specific plugin in the Network Admin Plugins list table.
+ *
+ * The dynamic portion of the hook name, `$plugin_file`, refers to the path
+ * to the plugin file, relative to the plugins directory.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $actions An array of plugin action links. By default this can include
+ * 'activate', 'deactivate', and 'delete'.
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ * @param array $plugin_data An array of plugin data. See get_plugin_data()
+ * and the {@see 'plugin_row_meta'} filter for the list
+ * of possible values.
+ * @param string $context The plugin context. By default this can include 'all',
+ * 'active', 'inactive', 'recently_activated', 'upgrade',
+ * 'mustuse', 'dropins', and 'search'.
+ */
+ $actions = apply_filters( "network_admin_plugin_action_links_{$plugin_file}", $actions, $plugin_file, $plugin_data, $context );
+
+ } else {
+
+ /**
+ * Filters the action links displayed for each plugin in the Plugins list table.
+ *
+ * @since 2.5.0
+ * @since 2.6.0 The `$context` parameter was added.
+ * @since 4.9.0 The 'Edit' link was removed from the list of action links.
+ *
+ * @param string[] $actions An array of plugin action links. By default this can include
+ * 'activate', 'deactivate', and 'delete'. With Multisite active
+ * this can also include 'network_active' and 'network_only' items.
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ * @param array $plugin_data An array of plugin data. See get_plugin_data()
+ * and the {@see 'plugin_row_meta'} filter for the list
+ * of possible values.
+ * @param string $context The plugin context. By default this can include 'all',
+ * 'active', 'inactive', 'recently_activated', 'upgrade',
+ * 'mustuse', 'dropins', and 'search'.
+ */
+ $actions = apply_filters( 'plugin_action_links', $actions, $plugin_file, $plugin_data, $context );
+
+ /**
+ * Filters the list of action links displayed for a specific plugin in the Plugins list table.
+ *
+ * The dynamic portion of the hook name, `$plugin_file`, refers to the path
+ * to the plugin file, relative to the plugins directory.
+ *
+ * @since 2.7.0
+ * @since 4.9.0 The 'Edit' link was removed from the list of action links.
+ *
+ * @param string[] $actions An array of plugin action links. By default this can include
+ * 'activate', 'deactivate', and 'delete'. With Multisite active
+ * this can also include 'network_active' and 'network_only' items.
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ * @param array $plugin_data An array of plugin data. See get_plugin_data()
+ * and the {@see 'plugin_row_meta'} filter for the list
+ * of possible values.
+ * @param string $context The plugin context. By default this can include 'all',
+ * 'active', 'inactive', 'recently_activated', 'upgrade',
+ * 'mustuse', 'dropins', and 'search'.
+ */
+ $actions = apply_filters( "plugin_action_links_{$plugin_file}", $actions, $plugin_file, $plugin_data, $context );
+
+ }
+
+ $class = $is_active ? 'active' : 'inactive';
+ $checkbox_id = 'checkbox_' . md5( $plugin_file );
+
+ if ( $restrict_network_active || $restrict_network_only || in_array( $status, array( 'mustuse', 'dropins' ), true ) || ! $compatible_php ) {
+ $checkbox = '';
+ } else {
+ $checkbox = sprintf(
+ '
' .
+ '
%3$s ',
+ esc_attr( $plugin_file ),
+ $checkbox_id,
+ /* translators: Hidden accessibility text. %s: Plugin name. */
+ sprintf( __( 'Select %s' ), $plugin_data['Name'] )
+ );
+ }
+
+ if ( 'dropins' !== $context ) {
+ $description = '
' . ( $plugin_data['Description'] ? $plugin_data['Description'] : ' ' ) . '
';
+ $plugin_name = $plugin_data['Name'];
+ }
+
+ if ( ! empty( $totals['upgrade'] ) && ! empty( $plugin_data['update'] )
+ || ! $compatible_php || ! $compatible_wp
+ ) {
+ $class .= ' update';
+ }
+
+ $paused = ! $screen->in_admin( 'network' ) && is_plugin_paused( $plugin_file );
+
+ if ( $paused ) {
+ $class .= ' paused';
+ }
+
+ if ( is_uninstallable_plugin( $plugin_file ) ) {
+ $class .= ' is-uninstallable';
+ }
+
+ printf(
+ '
',
+ esc_attr( $class ),
+ esc_attr( $plugin_slug ),
+ esc_attr( $plugin_file )
+ );
+
+ list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
+
+ $auto_updates = (array) get_site_option( 'auto_update_plugins', array() );
+
+ foreach ( $columns as $column_name => $column_display_name ) {
+ $extra_classes = '';
+ if ( in_array( $column_name, $hidden, true ) ) {
+ $extra_classes = ' hidden';
+ }
+
+ switch ( $column_name ) {
+ case 'cb':
+ echo "$checkbox ";
+ break;
+ case 'name':
+ echo "$plugin_name ";
+ echo $this->row_actions( $actions, true );
+ echo ' ';
+ break;
+ case 'description':
+ $classes = 'column-description desc';
+
+ echo "';
+ break;
+ case 'auto-updates':
+ if ( ! $this->show_autoupdates || in_array( $status, array( 'mustuse', 'dropins' ), true ) ) {
+ break;
+ }
+
+ echo "";
+
+ $html = array();
+
+ if ( isset( $plugin_data['auto-update-forced'] ) ) {
+ if ( $plugin_data['auto-update-forced'] ) {
+ // Forced on.
+ $text = __( 'Auto-updates enabled' );
+ } else {
+ $text = __( 'Auto-updates disabled' );
+ }
+ $action = 'unavailable';
+ $time_class = ' hidden';
+ } elseif ( empty( $plugin_data['update-supported'] ) ) {
+ $text = '';
+ $action = 'unavailable';
+ $time_class = ' hidden';
+ } elseif ( in_array( $plugin_file, $auto_updates, true ) ) {
+ $text = __( 'Disable auto-updates' );
+ $action = 'disable';
+ $time_class = '';
+ } else {
+ $text = __( 'Enable auto-updates' );
+ $action = 'enable';
+ $time_class = ' hidden';
+ }
+
+ $query_args = array(
+ 'action' => "{$action}-auto-update",
+ 'plugin' => $plugin_file,
+ 'paged' => $page,
+ 'plugin_status' => $status,
+ );
+
+ $url = add_query_arg( $query_args, 'plugins.php' );
+
+ if ( 'unavailable' === $action ) {
+ $html[] = '' . $text . ' ';
+ } else {
+ $html[] = sprintf(
+ '',
+ wp_nonce_url( $url, 'updates' ),
+ $action
+ );
+
+ $html[] = ' ';
+ $html[] = '' . $text . ' ';
+ $html[] = ' ';
+ }
+
+ if ( ! empty( $plugin_data['update'] ) ) {
+ $html[] = sprintf(
+ '%s
',
+ $time_class,
+ wp_get_auto_update_message()
+ );
+ }
+
+ $html = implode( '', $html );
+
+ /**
+ * Filters the HTML of the auto-updates setting for each plugin in the Plugins list table.
+ *
+ * @since 5.5.0
+ *
+ * @param string $html The HTML of the plugin's auto-update column content,
+ * including toggle auto-update action links and
+ * time to next update.
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ * @param array $plugin_data An array of plugin data. See get_plugin_data()
+ * and the {@see 'plugin_row_meta'} filter for the list
+ * of possible values.
+ */
+ echo apply_filters( 'plugin_auto_update_setting_html', $html, $plugin_file, $plugin_data );
+
+ wp_admin_notice(
+ '',
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'inline', 'hidden' ),
+ )
+ );
+
+ echo ' ';
+
+ break;
+ default:
+ $classes = "$column_name column-$column_name $class";
+
+ echo "';
+ }
+ }
+
+ echo ' ';
+
+ if ( ! $compatible_php || ! $compatible_wp ) {
+ printf(
+ '
',
+ esc_attr( $this->get_column_count() )
+ );
+
+ $incompatible_message = '';
+ if ( ! $compatible_php && ! $compatible_wp ) {
+ $incompatible_message .= __( 'This plugin does not work with your versions of WordPress and PHP.' );
+ if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
+ $incompatible_message .= sprintf(
+ /* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
+ ' ' . __( 'Please update WordPress , and then learn more about updating PHP .' ),
+ self_admin_url( 'update-core.php' ),
+ esc_url( wp_get_update_php_url() )
+ );
+ $incompatible_message .= wp_update_php_annotation( '', ' ', false );
+ } elseif ( current_user_can( 'update_core' ) ) {
+ $incompatible_message .= sprintf(
+ /* translators: %s: URL to WordPress Updates screen. */
+ ' ' . __( 'Please update WordPress .' ),
+ self_admin_url( 'update-core.php' )
+ );
+ } elseif ( current_user_can( 'update_php' ) ) {
+ $incompatible_message .= sprintf(
+ /* translators: %s: URL to Update PHP page. */
+ ' ' . __( 'Learn more about updating PHP .' ),
+ esc_url( wp_get_update_php_url() )
+ );
+ $incompatible_message .= wp_update_php_annotation( '
', ' ', false );
+ }
+ } elseif ( ! $compatible_wp ) {
+ $incompatible_message .= __( 'This plugin does not work with your version of WordPress.' );
+ if ( current_user_can( 'update_core' ) ) {
+ $incompatible_message .= sprintf(
+ /* translators: %s: URL to WordPress Updates screen. */
+ ' ' . __( 'Please update WordPress .' ),
+ self_admin_url( 'update-core.php' )
+ );
+ }
+ } elseif ( ! $compatible_php ) {
+ $incompatible_message .= __( 'This plugin does not work with your version of PHP.' );
+ if ( current_user_can( 'update_php' ) ) {
+ $incompatible_message .= sprintf(
+ /* translators: %s: URL to Update PHP page. */
+ ' ' . __( 'Learn more about updating PHP .' ),
+ esc_url( wp_get_update_php_url() )
+ );
+ $incompatible_message .= wp_update_php_annotation( '
', ' ', false );
+ }
+ }
+
+ wp_admin_notice(
+ $incompatible_message,
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'inline', 'update-message' ),
+ )
+ );
+
+ echo '
';
+ }
+
+ /**
+ * Fires after each row in the Plugins list table.
+ *
+ * @since 2.3.0
+ * @since 5.5.0 Added 'auto-update-enabled' and 'auto-update-disabled'
+ * to possible values for `$status`.
+ *
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ * @param array $plugin_data An array of plugin data. See get_plugin_data()
+ * and the {@see 'plugin_row_meta'} filter for the list
+ * of possible values.
+ * @param string $status Status filter currently applied to the plugin list.
+ * Possible values are: 'all', 'active', 'inactive',
+ * 'recently_activated', 'upgrade', 'mustuse', 'dropins',
+ * 'search', 'paused', 'auto-update-enabled', 'auto-update-disabled'.
+ */
+ do_action( 'after_plugin_row', $plugin_file, $plugin_data, $status );
+
+ /**
+ * Fires after each specific row in the Plugins list table.
+ *
+ * The dynamic portion of the hook name, `$plugin_file`, refers to the path
+ * to the plugin file, relative to the plugins directory.
+ *
+ * @since 2.7.0
+ * @since 5.5.0 Added 'auto-update-enabled' and 'auto-update-disabled'
+ * to possible values for `$status`.
+ *
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ * @param array $plugin_data An array of plugin data. See get_plugin_data()
+ * and the {@see 'plugin_row_meta'} filter for the list
+ * of possible values.
+ * @param string $status Status filter currently applied to the plugin list.
+ * Possible values are: 'all', 'active', 'inactive',
+ * 'recently_activated', 'upgrade', 'mustuse', 'dropins',
+ * 'search', 'paused', 'auto-update-enabled', 'auto-update-disabled'.
+ */
+ do_action( "after_plugin_row_{$plugin_file}", $plugin_file, $plugin_data, $status );
+ }
+
+ /**
+ * Gets the name of the primary column for this specific list table.
+ *
+ * @since 4.3.0
+ *
+ * @return string Unalterable name for the primary column, in this case, 'name'.
+ */
+ protected function get_primary_column_name() {
+ return 'name';
+ }
+}
diff --git a/wp-admin/includes/class-wp-post-comments-list-table.php b/wp-admin/includes/class-wp-post-comments-list-table.php
new file mode 100644
index 0000000..4454a77
--- /dev/null
+++ b/wp-admin/includes/class-wp-post-comments-list-table.php
@@ -0,0 +1,77 @@
+ __( 'Author' ),
+ 'comment' => _x( 'Comment', 'column name' ),
+ ),
+ array(),
+ array(),
+ 'comment',
+ );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_table_classes() {
+ $classes = parent::get_table_classes();
+ $classes[] = 'wp-list-table';
+ $classes[] = 'comments-box';
+ return $classes;
+ }
+
+ /**
+ * @param bool $output_empty
+ */
+ public function display( $output_empty = false ) {
+ $singular = $this->_args['singular'];
+
+ wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
+ ?>
+
+ 'posts',
+ 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
+ )
+ );
+
+ $post_type = $this->screen->post_type;
+ $post_type_object = get_post_type_object( $post_type );
+
+ $exclude_states = get_post_stati(
+ array(
+ 'show_in_admin_all_list' => false,
+ )
+ );
+
+ $this->user_posts_count = (int) $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT COUNT( 1 )
+ FROM $wpdb->posts
+ WHERE post_type = %s
+ AND post_status NOT IN ( '" . implode( "','", $exclude_states ) . "' )
+ AND post_author = %d",
+ $post_type,
+ get_current_user_id()
+ )
+ );
+
+ if ( $this->user_posts_count
+ && ! current_user_can( $post_type_object->cap->edit_others_posts )
+ && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['all_posts'] )
+ && empty( $_REQUEST['author'] ) && empty( $_REQUEST['show_sticky'] )
+ ) {
+ $_GET['author'] = get_current_user_id();
+ }
+
+ $sticky_posts = get_option( 'sticky_posts' );
+
+ if ( 'post' === $post_type && $sticky_posts ) {
+ $sticky_posts = implode( ', ', array_map( 'absint', (array) $sticky_posts ) );
+
+ $this->sticky_posts_count = (int) $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT COUNT( 1 )
+ FROM $wpdb->posts
+ WHERE post_type = %s
+ AND post_status NOT IN ('trash', 'auto-draft')
+ AND ID IN ($sticky_posts)",
+ $post_type
+ )
+ );
+ }
+ }
+
+ /**
+ * Sets whether the table layout should be hierarchical or not.
+ *
+ * @since 4.2.0
+ *
+ * @param bool $display Whether the table layout should be hierarchical.
+ */
+ public function set_hierarchical_display( $display ) {
+ $this->hierarchical_display = $display;
+ }
+
+ /**
+ * @return bool
+ */
+ public function ajax_user_can() {
+ return current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_posts );
+ }
+
+ /**
+ * @global string $mode List table view mode.
+ * @global array $avail_post_stati
+ * @global WP_Query $wp_query WordPress Query object.
+ * @global int $per_page
+ */
+ public function prepare_items() {
+ global $mode, $avail_post_stati, $wp_query, $per_page;
+
+ if ( ! empty( $_REQUEST['mode'] ) ) {
+ $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
+ set_user_setting( 'posts_list_mode', $mode );
+ } else {
+ $mode = get_user_setting( 'posts_list_mode', 'list' );
+ }
+
+ // Is going to call wp().
+ $avail_post_stati = wp_edit_posts_query();
+
+ $this->set_hierarchical_display(
+ is_post_type_hierarchical( $this->screen->post_type )
+ && 'menu_order title' === $wp_query->query['orderby']
+ );
+
+ $post_type = $this->screen->post_type;
+ $per_page = $this->get_items_per_page( 'edit_' . $post_type . '_per_page' );
+
+ /** This filter is documented in wp-admin/includes/post.php */
+ $per_page = apply_filters( 'edit_posts_per_page', $per_page, $post_type );
+
+ if ( $this->hierarchical_display ) {
+ $total_items = $wp_query->post_count;
+ } elseif ( $wp_query->found_posts || $this->get_pagenum() === 1 ) {
+ $total_items = $wp_query->found_posts;
+ } else {
+ $post_counts = (array) wp_count_posts( $post_type, 'readable' );
+
+ if ( isset( $_REQUEST['post_status'] ) && in_array( $_REQUEST['post_status'], $avail_post_stati, true ) ) {
+ $total_items = $post_counts[ $_REQUEST['post_status'] ];
+ } elseif ( isset( $_REQUEST['show_sticky'] ) && $_REQUEST['show_sticky'] ) {
+ $total_items = $this->sticky_posts_count;
+ } elseif ( isset( $_GET['author'] ) && get_current_user_id() === (int) $_GET['author'] ) {
+ $total_items = $this->user_posts_count;
+ } else {
+ $total_items = array_sum( $post_counts );
+
+ // Subtract post types that are not included in the admin all list.
+ foreach ( get_post_stati( array( 'show_in_admin_all_list' => false ) ) as $state ) {
+ $total_items -= $post_counts[ $state ];
+ }
+ }
+ }
+
+ $this->is_trash = isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'];
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $total_items,
+ 'per_page' => $per_page,
+ )
+ );
+ }
+
+ /**
+ * @return bool
+ */
+ public function has_items() {
+ return have_posts();
+ }
+
+ /**
+ */
+ public function no_items() {
+ if ( isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
+ echo get_post_type_object( $this->screen->post_type )->labels->not_found_in_trash;
+ } else {
+ echo get_post_type_object( $this->screen->post_type )->labels->not_found;
+ }
+ }
+
+ /**
+ * Determines if the current view is the "All" view.
+ *
+ * @since 4.2.0
+ *
+ * @return bool Whether the current view is the "All" view.
+ */
+ protected function is_base_request() {
+ $vars = $_GET;
+ unset( $vars['paged'] );
+
+ if ( empty( $vars ) ) {
+ return true;
+ } elseif ( 1 === count( $vars ) && ! empty( $vars['post_type'] ) ) {
+ return $this->screen->post_type === $vars['post_type'];
+ }
+
+ return 1 === count( $vars ) && ! empty( $vars['mode'] );
+ }
+
+ /**
+ * Creates a link to edit.php with params.
+ *
+ * @since 4.4.0
+ *
+ * @param string[] $args Associative array of URL parameters for the link.
+ * @param string $link_text Link text.
+ * @param string $css_class Optional. Class attribute. Default empty string.
+ * @return string The formatted link string.
+ */
+ protected function get_edit_link( $args, $link_text, $css_class = '' ) {
+ $url = add_query_arg( $args, 'edit.php' );
+
+ $class_html = '';
+ $aria_current = '';
+
+ if ( ! empty( $css_class ) ) {
+ $class_html = sprintf(
+ ' class="%s"',
+ esc_attr( $css_class )
+ );
+
+ if ( 'current' === $css_class ) {
+ $aria_current = ' aria-current="page"';
+ }
+ }
+
+ return sprintf(
+ '
%s ',
+ esc_url( $url ),
+ $class_html,
+ $aria_current,
+ $link_text
+ );
+ }
+
+ /**
+ * @global array $locked_post_status This seems to be deprecated.
+ * @global array $avail_post_stati
+ * @return array
+ */
+ protected function get_views() {
+ global $locked_post_status, $avail_post_stati;
+
+ $post_type = $this->screen->post_type;
+
+ if ( ! empty( $locked_post_status ) ) {
+ return array();
+ }
+
+ $status_links = array();
+ $num_posts = wp_count_posts( $post_type, 'readable' );
+ $total_posts = array_sum( (array) $num_posts );
+ $class = '';
+
+ $current_user_id = get_current_user_id();
+ $all_args = array( 'post_type' => $post_type );
+ $mine = '';
+
+ // Subtract post types that are not included in the admin all list.
+ foreach ( get_post_stati( array( 'show_in_admin_all_list' => false ) ) as $state ) {
+ $total_posts -= $num_posts->$state;
+ }
+
+ if ( $this->user_posts_count && $this->user_posts_count !== $total_posts ) {
+ if ( isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ) ) {
+ $class = 'current';
+ }
+
+ $mine_args = array(
+ 'post_type' => $post_type,
+ 'author' => $current_user_id,
+ );
+
+ $mine_inner_html = sprintf(
+ /* translators: %s: Number of posts. */
+ _nx(
+ 'Mine
(%s) ',
+ 'Mine
(%s) ',
+ $this->user_posts_count,
+ 'posts'
+ ),
+ number_format_i18n( $this->user_posts_count )
+ );
+
+ $mine = array(
+ 'url' => esc_url( add_query_arg( $mine_args, 'edit.php' ) ),
+ 'label' => $mine_inner_html,
+ 'current' => isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ),
+ );
+
+ $all_args['all_posts'] = 1;
+ $class = '';
+ }
+
+ $all_inner_html = sprintf(
+ /* translators: %s: Number of posts. */
+ _nx(
+ 'All
(%s) ',
+ 'All
(%s) ',
+ $total_posts,
+ 'posts'
+ ),
+ number_format_i18n( $total_posts )
+ );
+
+ $status_links['all'] = array(
+ 'url' => esc_url( add_query_arg( $all_args, 'edit.php' ) ),
+ 'label' => $all_inner_html,
+ 'current' => empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ),
+ );
+
+ if ( $mine ) {
+ $status_links['mine'] = $mine;
+ }
+
+ foreach ( get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) {
+ $class = '';
+
+ $status_name = $status->name;
+
+ if ( ! in_array( $status_name, $avail_post_stati, true ) || empty( $num_posts->$status_name ) ) {
+ continue;
+ }
+
+ if ( isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'] ) {
+ $class = 'current';
+ }
+
+ $status_args = array(
+ 'post_status' => $status_name,
+ 'post_type' => $post_type,
+ );
+
+ $status_label = sprintf(
+ translate_nooped_plural( $status->label_count, $num_posts->$status_name ),
+ number_format_i18n( $num_posts->$status_name )
+ );
+
+ $status_links[ $status_name ] = array(
+ 'url' => esc_url( add_query_arg( $status_args, 'edit.php' ) ),
+ 'label' => $status_label,
+ 'current' => isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'],
+ );
+ }
+
+ if ( ! empty( $this->sticky_posts_count ) ) {
+ $class = ! empty( $_REQUEST['show_sticky'] ) ? 'current' : '';
+
+ $sticky_args = array(
+ 'post_type' => $post_type,
+ 'show_sticky' => 1,
+ );
+
+ $sticky_inner_html = sprintf(
+ /* translators: %s: Number of posts. */
+ _nx(
+ 'Sticky
(%s) ',
+ 'Sticky
(%s) ',
+ $this->sticky_posts_count,
+ 'posts'
+ ),
+ number_format_i18n( $this->sticky_posts_count )
+ );
+
+ $sticky_link = array(
+ 'sticky' => array(
+ 'url' => esc_url( add_query_arg( $sticky_args, 'edit.php' ) ),
+ 'label' => $sticky_inner_html,
+ 'current' => ! empty( $_REQUEST['show_sticky'] ),
+ ),
+ );
+
+ // Sticky comes after Publish, or if not listed, after All.
+ $split = 1 + array_search( ( isset( $status_links['publish'] ) ? 'publish' : 'all' ), array_keys( $status_links ), true );
+ $status_links = array_merge( array_slice( $status_links, 0, $split ), $sticky_link, array_slice( $status_links, $split ) );
+ }
+
+ return $this->get_views_links( $status_links );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ $actions = array();
+ $post_type_obj = get_post_type_object( $this->screen->post_type );
+
+ if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
+ if ( $this->is_trash ) {
+ $actions['untrash'] = __( 'Restore' );
+ } else {
+ $actions['edit'] = __( 'Edit' );
+ }
+ }
+
+ if ( current_user_can( $post_type_obj->cap->delete_posts ) ) {
+ if ( $this->is_trash || ! EMPTY_TRASH_DAYS ) {
+ $actions['delete'] = __( 'Delete permanently' );
+ } else {
+ $actions['trash'] = __( 'Move to Trash' );
+ }
+ }
+
+ return $actions;
+ }
+
+ /**
+ * Displays a categories drop-down for filtering on the Posts list table.
+ *
+ * @since 4.6.0
+ *
+ * @global int $cat Currently selected category.
+ *
+ * @param string $post_type Post type slug.
+ */
+ protected function categories_dropdown( $post_type ) {
+ global $cat;
+
+ /**
+ * Filters whether to remove the 'Categories' drop-down from the post list table.
+ *
+ * @since 4.6.0
+ *
+ * @param bool $disable Whether to disable the categories drop-down. Default false.
+ * @param string $post_type Post type slug.
+ */
+ if ( false !== apply_filters( 'disable_categories_dropdown', false, $post_type ) ) {
+ return;
+ }
+
+ if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
+ $dropdown_options = array(
+ 'show_option_all' => get_taxonomy( 'category' )->labels->all_items,
+ 'hide_empty' => 0,
+ 'hierarchical' => 1,
+ 'show_count' => 0,
+ 'orderby' => 'name',
+ 'selected' => $cat,
+ );
+
+ echo '
' . get_taxonomy( 'category' )->labels->filter_by_item . ' ';
+
+ wp_dropdown_categories( $dropdown_options );
+ }
+ }
+
+ /**
+ * Displays a formats drop-down for filtering items.
+ *
+ * @since 5.2.0
+ * @access protected
+ *
+ * @param string $post_type Post type slug.
+ */
+ protected function formats_dropdown( $post_type ) {
+ /**
+ * Filters whether to remove the 'Formats' drop-down from the post list table.
+ *
+ * @since 5.2.0
+ * @since 5.5.0 The `$post_type` parameter was added.
+ *
+ * @param bool $disable Whether to disable the drop-down. Default false.
+ * @param string $post_type Post type slug.
+ */
+ if ( apply_filters( 'disable_formats_dropdown', false, $post_type ) ) {
+ return;
+ }
+
+ // Return if the post type doesn't have post formats or if we're in the Trash.
+ if ( ! is_object_in_taxonomy( $post_type, 'post_format' ) || $this->is_trash ) {
+ return;
+ }
+
+ // Make sure the dropdown shows only formats with a post count greater than 0.
+ $used_post_formats = get_terms(
+ array(
+ 'taxonomy' => 'post_format',
+ 'hide_empty' => true,
+ )
+ );
+
+ // Return if there are no posts using formats.
+ if ( ! $used_post_formats ) {
+ return;
+ }
+
+ $displayed_post_format = isset( $_GET['post_format'] ) ? $_GET['post_format'] : '';
+ ?>
+
+
+
+
+ value="">
+ slug );
+ // Pretty, translated version of the post format slug.
+ $pretty_name = get_post_format_string( $slug );
+
+ // Skip the standard post format.
+ if ( 'standard' === $slug ) {
+ continue;
+ }
+ ?>
+ value="">
+
+
+
+
+ months_dropdown( $this->screen->post_type );
+ $this->categories_dropdown( $this->screen->post_type );
+ $this->formats_dropdown( $this->screen->post_type );
+
+ /**
+ * Fires before the Filter button on the Posts and Pages list tables.
+ *
+ * The Filter button allows sorting by date and/or category on the
+ * Posts list table, and sorting by date on the Pages list table.
+ *
+ * @since 2.1.0
+ * @since 4.4.0 The `$post_type` parameter was added.
+ * @since 4.6.0 The `$which` parameter was added.
+ *
+ * @param string $post_type The post type slug.
+ * @param string $which The location of the extra table nav markup:
+ * 'top' or 'bottom' for WP_Posts_List_Table,
+ * 'bar' for WP_Media_List_Table.
+ */
+ do_action( 'restrict_manage_posts', $this->screen->post_type, $which );
+
+ $output = ob_get_clean();
+
+ if ( ! empty( $output ) ) {
+ echo $output;
+ submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
+ }
+ }
+
+ if ( $this->is_trash && $this->has_items()
+ && current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_others_posts )
+ ) {
+ submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
+ }
+ ?>
+
+ screen->post_type ) ? 'pages' : 'posts',
+ );
+ }
+
+ /**
+ * @return string[] Array of column titles keyed by their column name.
+ */
+ public function get_columns() {
+ $post_type = $this->screen->post_type;
+
+ $posts_columns = array();
+
+ $posts_columns['cb'] = '
';
+
+ /* translators: Posts screen column name. */
+ $posts_columns['title'] = _x( 'Title', 'column name' );
+
+ if ( post_type_supports( $post_type, 'author' ) ) {
+ $posts_columns['author'] = __( 'Author' );
+ }
+
+ $taxonomies = get_object_taxonomies( $post_type, 'objects' );
+ $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
+
+ /**
+ * Filters the taxonomy columns in the Posts list table.
+ *
+ * The dynamic portion of the hook name, `$post_type`, refers to the post
+ * type slug.
+ *
+ * Possible hook names include:
+ *
+ * - `manage_taxonomies_for_post_columns`
+ * - `manage_taxonomies_for_page_columns`
+ *
+ * @since 3.5.0
+ *
+ * @param string[] $taxonomies Array of taxonomy names to show columns for.
+ * @param string $post_type The post type.
+ */
+ $taxonomies = apply_filters( "manage_taxonomies_for_{$post_type}_columns", $taxonomies, $post_type );
+ $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
+
+ foreach ( $taxonomies as $taxonomy ) {
+ if ( 'category' === $taxonomy ) {
+ $column_key = 'categories';
+ } elseif ( 'post_tag' === $taxonomy ) {
+ $column_key = 'tags';
+ } else {
+ $column_key = 'taxonomy-' . $taxonomy;
+ }
+
+ $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
+ }
+
+ $post_status = ! empty( $_REQUEST['post_status'] ) ? $_REQUEST['post_status'] : 'all';
+
+ if ( post_type_supports( $post_type, 'comments' )
+ && ! in_array( $post_status, array( 'pending', 'draft', 'future' ), true )
+ ) {
+ $posts_columns['comments'] = sprintf(
+ '
%2$s ',
+ esc_attr__( 'Comments' ),
+ /* translators: Hidden accessibility text. */
+ __( 'Comments' )
+ );
+ }
+
+ $posts_columns['date'] = __( 'Date' );
+
+ if ( 'page' === $post_type ) {
+
+ /**
+ * Filters the columns displayed in the Pages list table.
+ *
+ * @since 2.5.0
+ *
+ * @param string[] $post_columns An associative array of column headings.
+ */
+ $posts_columns = apply_filters( 'manage_pages_columns', $posts_columns );
+ } else {
+
+ /**
+ * Filters the columns displayed in the Posts list table.
+ *
+ * @since 1.5.0
+ *
+ * @param string[] $post_columns An associative array of column headings.
+ * @param string $post_type The post type slug.
+ */
+ $posts_columns = apply_filters( 'manage_posts_columns', $posts_columns, $post_type );
+ }
+
+ /**
+ * Filters the columns displayed in the Posts list table for a specific post type.
+ *
+ * The dynamic portion of the hook name, `$post_type`, refers to the post type slug.
+ *
+ * Possible hook names include:
+ *
+ * - `manage_post_posts_columns`
+ * - `manage_page_posts_columns`
+ *
+ * @since 3.0.0
+ *
+ * @param string[] $post_columns An associative array of column headings.
+ */
+ return apply_filters( "manage_{$post_type}_posts_columns", $posts_columns );
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_sortable_columns() {
+
+ $post_type = $this->screen->post_type;
+
+ if ( 'page' === $post_type ) {
+ if ( isset( $_GET['orderby'] ) ) {
+ $title_orderby_text = __( 'Table ordered by Title.' );
+ } else {
+ $title_orderby_text = __( 'Table ordered by Hierarchical Menu Order and Title.' );
+ }
+
+ $sortables = array(
+ 'title' => array( 'title', false, __( 'Title' ), $title_orderby_text, 'asc' ),
+ 'parent' => array( 'parent', false ),
+ 'comments' => array( 'comment_count', false, __( 'Comments' ), __( 'Table ordered by Comments.' ) ),
+ 'date' => array( 'date', true, __( 'Date' ), __( 'Table ordered by Date.' ) ),
+ );
+ } else {
+ $sortables = array(
+ 'title' => array( 'title', false, __( 'Title' ), __( 'Table ordered by Title.' ) ),
+ 'parent' => array( 'parent', false ),
+ 'comments' => array( 'comment_count', false, __( 'Comments' ), __( 'Table ordered by Comments.' ) ),
+ 'date' => array( 'date', true, __( 'Date' ), __( 'Table ordered by Date.' ), 'desc' ),
+ );
+ }
+ // Custom Post Types: there's a filter for that, see get_column_info().
+
+ return $sortables;
+ }
+
+ /**
+ * @global WP_Query $wp_query WordPress Query object.
+ * @global int $per_page
+ * @param array $posts
+ * @param int $level
+ */
+ public function display_rows( $posts = array(), $level = 0 ) {
+ global $wp_query, $per_page;
+
+ if ( empty( $posts ) ) {
+ $posts = $wp_query->posts;
+ }
+
+ add_filter( 'the_title', 'esc_html' );
+
+ if ( $this->hierarchical_display ) {
+ $this->_display_rows_hierarchical( $posts, $this->get_pagenum(), $per_page );
+ } else {
+ $this->_display_rows( $posts, $level );
+ }
+ }
+
+ /**
+ * @param array $posts
+ * @param int $level
+ */
+ private function _display_rows( $posts, $level = 0 ) {
+ $post_type = $this->screen->post_type;
+
+ // Create array of post IDs.
+ $post_ids = array();
+
+ foreach ( $posts as $a_post ) {
+ $post_ids[] = $a_post->ID;
+ }
+
+ if ( post_type_supports( $post_type, 'comments' ) ) {
+ $this->comment_pending_count = get_pending_comments_num( $post_ids );
+ }
+ update_post_author_caches( $posts );
+
+ foreach ( $posts as $post ) {
+ $this->single_row( $post, $level );
+ }
+ }
+
+ /**
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @global WP_Post $post Global post object.
+ * @param array $pages
+ * @param int $pagenum
+ * @param int $per_page
+ */
+ private function _display_rows_hierarchical( $pages, $pagenum = 1, $per_page = 20 ) {
+ global $wpdb;
+
+ $level = 0;
+
+ if ( ! $pages ) {
+ $pages = get_pages( array( 'sort_column' => 'menu_order' ) );
+
+ if ( ! $pages ) {
+ return;
+ }
+ }
+
+ /*
+ * Arrange pages into two parts: top level pages and children_pages.
+ * children_pages is two dimensional array. Example:
+ * children_pages[10][] contains all sub-pages whose parent is 10.
+ * It only takes O( N ) to arrange this and it takes O( 1 ) for subsequent lookup operations
+ * If searching, ignore hierarchy and treat everything as top level
+ */
+ if ( empty( $_REQUEST['s'] ) ) {
+ $top_level_pages = array();
+ $children_pages = array();
+
+ foreach ( $pages as $page ) {
+ // Catch and repair bad pages.
+ if ( $page->post_parent === $page->ID ) {
+ $page->post_parent = 0;
+ $wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'ID' => $page->ID ) );
+ clean_post_cache( $page );
+ }
+
+ if ( $page->post_parent > 0 ) {
+ $children_pages[ $page->post_parent ][] = $page;
+ } else {
+ $top_level_pages[] = $page;
+ }
+ }
+
+ $pages = &$top_level_pages;
+ }
+
+ $count = 0;
+ $start = ( $pagenum - 1 ) * $per_page;
+ $end = $start + $per_page;
+ $to_display = array();
+
+ foreach ( $pages as $page ) {
+ if ( $count >= $end ) {
+ break;
+ }
+
+ if ( $count >= $start ) {
+ $to_display[ $page->ID ] = $level;
+ }
+
+ ++$count;
+
+ if ( isset( $children_pages ) ) {
+ $this->_page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page, $to_display );
+ }
+ }
+
+ // If it is the last pagenum and there are orphaned pages, display them with paging as well.
+ if ( isset( $children_pages ) && $count < $end ) {
+ foreach ( $children_pages as $orphans ) {
+ foreach ( $orphans as $op ) {
+ if ( $count >= $end ) {
+ break;
+ }
+
+ if ( $count >= $start ) {
+ $to_display[ $op->ID ] = 0;
+ }
+
+ ++$count;
+ }
+ }
+ }
+
+ $ids = array_keys( $to_display );
+ _prime_post_caches( $ids );
+ $_posts = array_map( 'get_post', $ids );
+ update_post_author_caches( $_posts );
+
+ if ( ! isset( $GLOBALS['post'] ) ) {
+ $GLOBALS['post'] = reset( $ids );
+ }
+
+ foreach ( $to_display as $page_id => $level ) {
+ echo "\t";
+ $this->single_row( $page_id, $level );
+ }
+ }
+
+ /**
+ * Displays the nested hierarchy of sub-pages together with paging
+ * support, based on a top level page ID.
+ *
+ * @since 3.1.0 (Standalone function exists since 2.6.0)
+ * @since 4.2.0 Added the `$to_display` parameter.
+ *
+ * @param array $children_pages
+ * @param int $count
+ * @param int $parent_page
+ * @param int $level
+ * @param int $pagenum
+ * @param int $per_page
+ * @param array $to_display List of pages to be displayed. Passed by reference.
+ */
+ private function _page_rows( &$children_pages, &$count, $parent_page, $level, $pagenum, $per_page, &$to_display ) {
+ if ( ! isset( $children_pages[ $parent_page ] ) ) {
+ return;
+ }
+
+ $start = ( $pagenum - 1 ) * $per_page;
+ $end = $start + $per_page;
+
+ foreach ( $children_pages[ $parent_page ] as $page ) {
+ if ( $count >= $end ) {
+ break;
+ }
+
+ // If the page starts in a subtree, print the parents.
+ if ( $count === $start && $page->post_parent > 0 ) {
+ $my_parents = array();
+ $my_parent = $page->post_parent;
+
+ while ( $my_parent ) {
+ // Get the ID from the list or the attribute if my_parent is an object.
+ $parent_id = $my_parent;
+
+ if ( is_object( $my_parent ) ) {
+ $parent_id = $my_parent->ID;
+ }
+
+ $my_parent = get_post( $parent_id );
+ $my_parents[] = $my_parent;
+
+ if ( ! $my_parent->post_parent ) {
+ break;
+ }
+
+ $my_parent = $my_parent->post_parent;
+ }
+
+ $num_parents = count( $my_parents );
+
+ while ( $my_parent = array_pop( $my_parents ) ) {
+ $to_display[ $my_parent->ID ] = $level - $num_parents;
+ --$num_parents;
+ }
+ }
+
+ if ( $count >= $start ) {
+ $to_display[ $page->ID ] = $level;
+ }
+
+ ++$count;
+
+ $this->_page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page, $to_display );
+ }
+
+ unset( $children_pages[ $parent_page ] ); // Required in order to keep track of orphans.
+ }
+
+ /**
+ * Handles the checkbox column output.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Post $item The current WP_Post object.
+ */
+ public function column_cb( $item ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $post = $item;
+
+ $show = current_user_can( 'edit_post', $post->ID );
+
+ /**
+ * Filters whether to show the bulk edit checkbox for a post in its list table.
+ *
+ * By default the checkbox is only shown if the current user can edit the post.
+ *
+ * @since 5.7.0
+ *
+ * @param bool $show Whether to show the checkbox.
+ * @param WP_Post $post The current WP_Post object.
+ */
+ if ( apply_filters( 'wp_list_table_show_post_checkbox', $show, $post ) ) :
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+ ';
+ echo $this->column_title( $post );
+ echo $this->handle_row_actions( $post, 'title', $primary );
+ echo '';
+ }
+
+ /**
+ * Handles the title column output.
+ *
+ * @since 4.3.0
+ *
+ * @global string $mode List table view mode.
+ *
+ * @param WP_Post $post The current WP_Post object.
+ */
+ public function column_title( $post ) {
+ global $mode;
+
+ if ( $this->hierarchical_display ) {
+ if ( 0 === $this->current_level && (int) $post->post_parent > 0 ) {
+ // Sent level 0 by accident, by default, or because we don't know the actual level.
+ $find_main_page = (int) $post->post_parent;
+
+ while ( $find_main_page > 0 ) {
+ $parent = get_post( $find_main_page );
+
+ if ( is_null( $parent ) ) {
+ break;
+ }
+
+ ++$this->current_level;
+ $find_main_page = (int) $parent->post_parent;
+
+ if ( ! isset( $parent_name ) ) {
+ /** This filter is documented in wp-includes/post-template.php */
+ $parent_name = apply_filters( 'the_title', $parent->post_title, $parent->ID );
+ }
+ }
+ }
+ }
+
+ $can_edit_post = current_user_can( 'edit_post', $post->ID );
+
+ if ( $can_edit_post && 'trash' !== $post->post_status ) {
+ $lock_holder = wp_check_post_lock( $post->ID );
+
+ if ( $lock_holder ) {
+ $lock_holder = get_userdata( $lock_holder );
+ $locked_avatar = get_avatar( $lock_holder->ID, 18 );
+ /* translators: %s: User's display name. */
+ $locked_text = esc_html( sprintf( __( '%s is currently editing' ), $lock_holder->display_name ) );
+ } else {
+ $locked_avatar = '';
+ $locked_text = '';
+ }
+
+ echo '
' . $locked_avatar . ' ' . $locked_text . "
\n";
+ }
+
+ $pad = str_repeat( '— ', $this->current_level );
+ echo '
';
+
+ $title = _draft_or_post_title();
+
+ if ( $can_edit_post && 'trash' !== $post->post_status ) {
+ printf(
+ '%s%s ',
+ get_edit_post_link( $post->ID ),
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( '“%s” (Edit)' ), $title ) ),
+ $pad,
+ $title
+ );
+ } else {
+ printf(
+ '%s%s ',
+ $pad,
+ $title
+ );
+ }
+ _post_states( $post );
+
+ if ( isset( $parent_name ) ) {
+ $post_type_object = get_post_type_object( $post->post_type );
+ echo ' | ' . $post_type_object->labels->parent_item_colon . ' ' . esc_html( $parent_name );
+ }
+
+ echo " \n";
+
+ if ( 'excerpt' === $mode
+ && ! is_post_type_hierarchical( $this->screen->post_type )
+ && current_user_can( 'read_post', $post->ID )
+ ) {
+ if ( post_password_required( $post ) ) {
+ echo '
' . esc_html( get_the_excerpt() ) . ' ';
+ } else {
+ echo esc_html( get_the_excerpt() );
+ }
+ }
+
+ /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */
+ $quick_edit_enabled = apply_filters( 'quick_edit_enabled_for_post_type', true, $post->post_type );
+
+ if ( $quick_edit_enabled ) {
+ get_inline_data( $post );
+ }
+ }
+
+ /**
+ * Handles the post date column output.
+ *
+ * @since 4.3.0
+ *
+ * @global string $mode List table view mode.
+ *
+ * @param WP_Post $post The current WP_Post object.
+ */
+ public function column_date( $post ) {
+ global $mode;
+
+ if ( '0000-00-00 00:00:00' === $post->post_date ) {
+ $t_time = __( 'Unpublished' );
+ $time_diff = 0;
+ } else {
+ $t_time = sprintf(
+ /* translators: 1: Post date, 2: Post time. */
+ __( '%1$s at %2$s' ),
+ /* translators: Post date format. See https://www.php.net/manual/datetime.format.php */
+ get_the_time( __( 'Y/m/d' ), $post ),
+ /* translators: Post time format. See https://www.php.net/manual/datetime.format.php */
+ get_the_time( __( 'g:i a' ), $post )
+ );
+
+ $time = get_post_timestamp( $post );
+ $time_diff = time() - $time;
+ }
+
+ if ( 'publish' === $post->post_status ) {
+ $status = __( 'Published' );
+ } elseif ( 'future' === $post->post_status ) {
+ if ( $time_diff > 0 ) {
+ $status = '
' . __( 'Missed schedule' ) . ' ';
+ } else {
+ $status = __( 'Scheduled' );
+ }
+ } else {
+ $status = __( 'Last Modified' );
+ }
+
+ /**
+ * Filters the status text of the post.
+ *
+ * @since 4.8.0
+ *
+ * @param string $status The status text.
+ * @param WP_Post $post Post object.
+ * @param string $column_name The column name.
+ * @param string $mode The list display mode ('excerpt' or 'list').
+ */
+ $status = apply_filters( 'post_date_column_status', $status, $post, 'date', $mode );
+
+ if ( $status ) {
+ echo $status . '
';
+ }
+
+ /**
+ * Filters the published, scheduled, or unpublished time of the post.
+ *
+ * @since 2.5.1
+ * @since 5.5.0 Removed the difference between 'excerpt' and 'list' modes.
+ * The published time and date are both displayed now,
+ * which is equivalent to the previous 'excerpt' mode.
+ *
+ * @param string $t_time The published time.
+ * @param WP_Post $post Post object.
+ * @param string $column_name The column name.
+ * @param string $mode The list display mode ('excerpt' or 'list').
+ */
+ echo apply_filters( 'post_date_column_time', $t_time, $post, 'date', $mode );
+ }
+
+ /**
+ * Handles the comments column output.
+ *
+ * @since 4.3.0
+ *
+ * @param WP_Post $post The current WP_Post object.
+ */
+ public function column_comments( $post ) {
+ ?>
+
+ comment_pending_count[ $post->ID ] ) ? $this->comment_pending_count[ $post->ID ] : 0;
+
+ $this->comments_bubble( $post->ID, $pending_comments );
+ ?>
+
+ $post->post_type,
+ 'author' => get_the_author_meta( 'ID' ),
+ );
+ echo $this->get_edit_link( $args, get_the_author() );
+ }
+
+ /**
+ * Handles the default column output.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Post $item The current WP_Post object.
+ * @param string $column_name The current column name.
+ */
+ public function column_default( $item, $column_name ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $post = $item;
+
+ if ( 'categories' === $column_name ) {
+ $taxonomy = 'category';
+ } elseif ( 'tags' === $column_name ) {
+ $taxonomy = 'post_tag';
+ } elseif ( str_starts_with( $column_name, 'taxonomy-' ) ) {
+ $taxonomy = substr( $column_name, 9 );
+ } else {
+ $taxonomy = false;
+ }
+
+ if ( $taxonomy ) {
+ $taxonomy_object = get_taxonomy( $taxonomy );
+ $terms = get_the_terms( $post->ID, $taxonomy );
+
+ if ( is_array( $terms ) ) {
+ $term_links = array();
+
+ foreach ( $terms as $t ) {
+ $posts_in_term_qv = array();
+
+ if ( 'post' !== $post->post_type ) {
+ $posts_in_term_qv['post_type'] = $post->post_type;
+ }
+
+ if ( $taxonomy_object->query_var ) {
+ $posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug;
+ } else {
+ $posts_in_term_qv['taxonomy'] = $taxonomy;
+ $posts_in_term_qv['term'] = $t->slug;
+ }
+
+ $label = esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) );
+
+ $term_links[] = $this->get_edit_link( $posts_in_term_qv, $label );
+ }
+
+ /**
+ * Filters the links in `$taxonomy` column of edit.php.
+ *
+ * @since 5.2.0
+ *
+ * @param string[] $term_links Array of term editing links.
+ * @param string $taxonomy Taxonomy name.
+ * @param WP_Term[] $terms Array of term objects appearing in the post row.
+ */
+ $term_links = apply_filters( 'post_column_taxonomy_links', $term_links, $taxonomy, $terms );
+
+ echo implode( wp_get_list_item_separator(), $term_links );
+ } else {
+ echo '
— ' . $taxonomy_object->labels->no_terms . ' ';
+ }
+ return;
+ }
+
+ if ( is_post_type_hierarchical( $post->post_type ) ) {
+
+ /**
+ * Fires in each custom column on the Posts list table.
+ *
+ * This hook only fires if the current post type is hierarchical,
+ * such as pages.
+ *
+ * @since 2.5.0
+ *
+ * @param string $column_name The name of the column to display.
+ * @param int $post_id The current post ID.
+ */
+ do_action( 'manage_pages_custom_column', $column_name, $post->ID );
+ } else {
+
+ /**
+ * Fires in each custom column in the Posts list table.
+ *
+ * This hook only fires if the current post type is non-hierarchical,
+ * such as posts.
+ *
+ * @since 1.5.0
+ *
+ * @param string $column_name The name of the column to display.
+ * @param int $post_id The current post ID.
+ */
+ do_action( 'manage_posts_custom_column', $column_name, $post->ID );
+ }
+
+ /**
+ * Fires for each custom column of a specific post type in the Posts list table.
+ *
+ * The dynamic portion of the hook name, `$post->post_type`, refers to the post type.
+ *
+ * Possible hook names include:
+ *
+ * - `manage_post_posts_custom_column`
+ * - `manage_page_posts_custom_column`
+ *
+ * @since 3.1.0
+ *
+ * @param string $column_name The name of the column to display.
+ * @param int $post_id The current post ID.
+ */
+ do_action( "manage_{$post->post_type}_posts_custom_column", $column_name, $post->ID );
+ }
+
+ /**
+ * @global WP_Post $post Global post object.
+ *
+ * @param int|WP_Post $post
+ * @param int $level
+ */
+ public function single_row( $post, $level = 0 ) {
+ $global_post = get_post();
+
+ $post = get_post( $post );
+ $this->current_level = $level;
+
+ $GLOBALS['post'] = $post;
+ setup_postdata( $post );
+
+ $classes = 'iedit author-' . ( get_current_user_id() === (int) $post->post_author ? 'self' : 'other' );
+
+ $lock_holder = wp_check_post_lock( $post->ID );
+
+ if ( $lock_holder ) {
+ $classes .= ' wp-locked';
+ }
+
+ if ( $post->post_parent ) {
+ $count = count( get_post_ancestors( $post->ID ) );
+ $classes .= ' level-' . $count;
+ } else {
+ $classes .= ' level-0';
+ }
+ ?>
+
+ single_row_columns( $post ); ?>
+
+ post_type );
+ $can_edit_post = current_user_can( 'edit_post', $post->ID );
+ $actions = array();
+ $title = _draft_or_post_title();
+
+ if ( $can_edit_post && 'trash' !== $post->post_status ) {
+ $actions['edit'] = sprintf(
+ '
%s ',
+ get_edit_post_link( $post->ID ),
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'Edit “%s”' ), $title ) ),
+ __( 'Edit' )
+ );
+
+ /**
+ * Filters whether Quick Edit should be enabled for the given post type.
+ *
+ * @since 6.4.0
+ *
+ * @param bool $enable Whether to enable the Quick Edit functionality. Default true.
+ * @param string $post_type Post type name.
+ */
+ $quick_edit_enabled = apply_filters( 'quick_edit_enabled_for_post_type', true, $post->post_type );
+
+ if ( $quick_edit_enabled && 'wp_block' !== $post->post_type ) {
+ $actions['inline hide-if-no-js'] = sprintf(
+ '
%s ',
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'Quick edit “%s” inline' ), $title ) ),
+ __( 'Quick Edit' )
+ );
+ }
+ }
+
+ if ( current_user_can( 'delete_post', $post->ID ) ) {
+ if ( 'trash' === $post->post_status ) {
+ $actions['untrash'] = sprintf(
+ '
%s ',
+ wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ),
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'Restore “%s” from the Trash' ), $title ) ),
+ __( 'Restore' )
+ );
+ } elseif ( EMPTY_TRASH_DAYS ) {
+ $actions['trash'] = sprintf(
+ '
%s ',
+ get_delete_post_link( $post->ID ),
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'Move “%s” to the Trash' ), $title ) ),
+ _x( 'Trash', 'verb' )
+ );
+ }
+
+ if ( 'trash' === $post->post_status || ! EMPTY_TRASH_DAYS ) {
+ $actions['delete'] = sprintf(
+ '
%s ',
+ get_delete_post_link( $post->ID, '', true ),
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'Delete “%s” permanently' ), $title ) ),
+ __( 'Delete Permanently' )
+ );
+ }
+ }
+
+ if ( is_post_type_viewable( $post_type_object ) ) {
+ if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ), true ) ) {
+ if ( $can_edit_post ) {
+ $preview_link = get_preview_post_link( $post );
+ $actions['view'] = sprintf(
+ '
%s ',
+ esc_url( $preview_link ),
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'Preview “%s”' ), $title ) ),
+ __( 'Preview' )
+ );
+ }
+ } elseif ( 'trash' !== $post->post_status ) {
+ $actions['view'] = sprintf(
+ '
%s ',
+ get_permalink( $post->ID ),
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'View “%s”' ), $title ) ),
+ __( 'View' )
+ );
+ }
+ }
+
+ if ( 'wp_block' === $post->post_type ) {
+ $actions['export'] = sprintf(
+ '
%s ',
+ $post->ID,
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'Export “%s” as JSON' ), $title ) ),
+ __( 'Export as JSON' )
+ );
+ }
+
+ if ( is_post_type_hierarchical( $post->post_type ) ) {
+
+ /**
+ * Filters the array of row action links on the Pages list table.
+ *
+ * The filter is evaluated only for hierarchical post types.
+ *
+ * @since 2.8.0
+ *
+ * @param string[] $actions An array of row action links. Defaults are
+ * 'Edit', 'Quick Edit', 'Restore', 'Trash',
+ * 'Delete Permanently', 'Preview', and 'View'.
+ * @param WP_Post $post The post object.
+ */
+ $actions = apply_filters( 'page_row_actions', $actions, $post );
+ } else {
+
+ /**
+ * Filters the array of row action links on the Posts list table.
+ *
+ * The filter is evaluated only for non-hierarchical post types.
+ *
+ * @since 2.8.0
+ *
+ * @param string[] $actions An array of row action links. Defaults are
+ * 'Edit', 'Quick Edit', 'Restore', 'Trash',
+ * 'Delete Permanently', 'Preview', and 'View'.
+ * @param WP_Post $post The post object.
+ */
+ $actions = apply_filters( 'post_row_actions', $actions, $post );
+ }
+
+ return $this->row_actions( $actions );
+ }
+
+ /**
+ * Outputs the hidden row displayed when inline editing
+ *
+ * @since 3.1.0
+ *
+ * @global string $mode List table view mode.
+ */
+ public function inline_edit() {
+ global $mode;
+
+ $screen = $this->screen;
+
+ $post = get_default_post_to_edit( $screen->post_type );
+ $post_type_object = get_post_type_object( $screen->post_type );
+
+ $taxonomy_names = get_object_taxonomies( $screen->post_type );
+ $hierarchical_taxonomies = array();
+ $flat_taxonomies = array();
+
+ foreach ( $taxonomy_names as $taxonomy_name ) {
+ $taxonomy = get_taxonomy( $taxonomy_name );
+
+ $show_in_quick_edit = $taxonomy->show_in_quick_edit;
+
+ /**
+ * Filters whether the current taxonomy should be shown in the Quick Edit panel.
+ *
+ * @since 4.2.0
+ *
+ * @param bool $show_in_quick_edit Whether to show the current taxonomy in Quick Edit.
+ * @param string $taxonomy_name Taxonomy name.
+ * @param string $post_type Post type of current Quick Edit post.
+ */
+ if ( ! apply_filters( 'quick_edit_show_taxonomy', $show_in_quick_edit, $taxonomy_name, $screen->post_type ) ) {
+ continue;
+ }
+
+ if ( $taxonomy->hierarchical ) {
+ $hierarchical_taxonomies[] = $taxonomy;
+ } else {
+ $flat_taxonomies[] = $taxonomy;
+ }
+ }
+
+ $m = ( isset( $mode ) && 'excerpt' === $mode ) ? 'excerpt' : 'list';
+ $can_publish = current_user_can( $post_type_object->cap->publish_posts );
+ $core_columns = array(
+ 'cb' => true,
+ 'date' => true,
+ 'title' => true,
+ 'categories' => true,
+ 'tags' => true,
+ 'comments' => true,
+ 'author' => true,
+ );
+ ?>
+
+
+ status;
+ $request_id = $item->ID;
+ $nonce = wp_create_nonce( 'wp-privacy-export-personal-data-' . $request_id );
+
+ $download_data_markup = '
';
+
+ $download_data_markup .= '' . __( 'Download personal data' ) . ' ' .
+ '' . __( 'Downloading data...' ) . ' ' .
+ '' . __( 'Download personal data again' ) . ' ' .
+ '' . __( 'Download failed.' ) . ' ' . __( 'Retry' ) . ' ';
+
+ $download_data_markup .= ' ';
+
+ $row_actions['download-data'] = $download_data_markup;
+
+ if ( 'request-completed' !== $status ) {
+ $complete_request_markup = '
';
+ $complete_request_markup .= sprintf(
+ '%s ',
+ esc_url(
+ wp_nonce_url(
+ add_query_arg(
+ array(
+ 'action' => 'complete',
+ 'request_id' => array( $request_id ),
+ ),
+ admin_url( 'export-personal-data.php' )
+ ),
+ 'bulk-privacy_requests'
+ )
+ ),
+ esc_attr(
+ sprintf(
+ /* translators: %s: Request email. */
+ __( 'Mark export request for “%s” as completed.' ),
+ $item->email
+ )
+ ),
+ __( 'Complete request' )
+ );
+ $complete_request_markup .= ' ';
+ }
+
+ if ( ! empty( $complete_request_markup ) ) {
+ $row_actions['complete-request'] = $complete_request_markup;
+ }
+
+ return sprintf( '
%2$s %3$s', esc_url( 'mailto:' . $item->email ), $item->email, $this->row_actions( $row_actions ) );
+ }
+
+ /**
+ * Displays the next steps column.
+ *
+ * @since 4.9.6
+ *
+ * @param WP_User_Request $item Item being shown.
+ */
+ public function column_next_steps( $item ) {
+ $status = $item->status;
+
+ switch ( $status ) {
+ case 'request-pending':
+ esc_html_e( 'Waiting for confirmation' );
+ break;
+ case 'request-confirmed':
+ /** This filter is documented in wp-admin/includes/ajax-actions.php */
+ $exporters = apply_filters( 'wp_privacy_personal_data_exporters', array() );
+ $exporters_count = count( $exporters );
+ $request_id = $item->ID;
+ $nonce = wp_create_nonce( 'wp-privacy-export-personal-data-' . $request_id );
+
+ echo '
';
+
+ ?>
+
+
+
+
+ ';
+ break;
+ case 'request-failed':
+ echo '
' . __( 'Retry' ) . ' ';
+ break;
+ case 'request-completed':
+ echo '
' . esc_html__( 'Remove request' ) . ' ';
+ break;
+ }
+ }
+}
diff --git a/wp-admin/includes/class-wp-privacy-data-removal-requests-list-table.php b/wp-admin/includes/class-wp-privacy-data-removal-requests-list-table.php
new file mode 100644
index 0000000..7165351
--- /dev/null
+++ b/wp-admin/includes/class-wp-privacy-data-removal-requests-list-table.php
@@ -0,0 +1,167 @@
+status;
+ $request_id = $item->ID;
+ $row_actions = array();
+ if ( 'request-confirmed' !== $status ) {
+ /** This filter is documented in wp-admin/includes/ajax-actions.php */
+ $erasers = apply_filters( 'wp_privacy_personal_data_erasers', array() );
+ $erasers_count = count( $erasers );
+ $nonce = wp_create_nonce( 'wp-privacy-erase-personal-data-' . $request_id );
+
+ $remove_data_markup = '
';
+
+ $remove_data_markup .= '' . __( 'Force erase personal data' ) . ' ' .
+ '' . __( 'Erasing data...' ) . ' ' .
+ '' . __( 'Erasure completed.' ) . ' ' .
+ '' . __( 'Force erasure has failed.' ) . ' ' . __( 'Retry' ) . ' ';
+
+ $remove_data_markup .= ' ';
+
+ $row_actions['remove-data'] = $remove_data_markup;
+ }
+
+ if ( 'request-completed' !== $status ) {
+ $complete_request_markup = '
';
+ $complete_request_markup .= sprintf(
+ '%s ',
+ esc_url(
+ wp_nonce_url(
+ add_query_arg(
+ array(
+ 'action' => 'complete',
+ 'request_id' => array( $request_id ),
+ ),
+ admin_url( 'erase-personal-data.php' )
+ ),
+ 'bulk-privacy_requests'
+ )
+ ),
+ esc_attr(
+ sprintf(
+ /* translators: %s: Request email. */
+ __( 'Mark export request for “%s” as completed.' ),
+ $item->email
+ )
+ ),
+ __( 'Complete request' )
+ );
+ $complete_request_markup .= ' ';
+ }
+
+ if ( ! empty( $complete_request_markup ) ) {
+ $row_actions['complete-request'] = $complete_request_markup;
+ }
+
+ return sprintf( '
%2$s %3$s', esc_url( 'mailto:' . $item->email ), $item->email, $this->row_actions( $row_actions ) );
+ }
+
+ /**
+ * Outputs the Next steps column.
+ *
+ * @since 4.9.6
+ *
+ * @param WP_User_Request $item Item being shown.
+ */
+ public function column_next_steps( $item ) {
+ $status = $item->status;
+
+ switch ( $status ) {
+ case 'request-pending':
+ esc_html_e( 'Waiting for confirmation' );
+ break;
+ case 'request-confirmed':
+ /** This filter is documented in wp-admin/includes/ajax-actions.php */
+ $erasers = apply_filters( 'wp_privacy_personal_data_erasers', array() );
+ $erasers_count = count( $erasers );
+ $request_id = $item->ID;
+ $nonce = wp_create_nonce( 'wp-privacy-erase-personal-data-' . $request_id );
+
+ echo '
';
+
+ ?>
+
+
+
+
+ ';
+
+ break;
+ case 'request-failed':
+ echo '
' . __( 'Retry' ) . ' ';
+ break;
+ case 'request-completed':
+ echo '
' . esc_html__( 'Remove request' ) . ' ';
+ break;
+ }
+ }
+}
diff --git a/wp-admin/includes/class-wp-privacy-policy-content.php b/wp-admin/includes/class-wp-privacy-policy-content.php
new file mode 100644
index 0000000..a64d043
--- /dev/null
+++ b/wp-admin/includes/class-wp-privacy-policy-content.php
@@ -0,0 +1,706 @@
+ $plugin_name,
+ 'policy_text' => $policy_text,
+ );
+
+ if ( ! in_array( $data, self::$policy_content, true ) ) {
+ self::$policy_content[] = $data;
+ }
+ }
+
+ /**
+ * Performs a quick check to determine whether any privacy info has changed.
+ *
+ * @since 4.9.6
+ */
+ public static function text_change_check() {
+
+ $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
+
+ // The site doesn't have a privacy policy.
+ if ( empty( $policy_page_id ) ) {
+ return false;
+ }
+
+ if ( ! current_user_can( 'edit_post', $policy_page_id ) ) {
+ return false;
+ }
+
+ $old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
+
+ // Updates are not relevant if the user has not reviewed any suggestions yet.
+ if ( empty( $old ) ) {
+ return false;
+ }
+
+ $cached = get_option( '_wp_suggested_policy_text_has_changed' );
+
+ /*
+ * When this function is called before `admin_init`, `self::$policy_content`
+ * has not been populated yet, so use the cached result from the last
+ * execution instead.
+ */
+ if ( ! did_action( 'admin_init' ) ) {
+ return 'changed' === $cached;
+ }
+
+ $new = self::$policy_content;
+
+ // Remove the extra values added to the meta.
+ foreach ( $old as $key => $data ) {
+ if ( ! is_array( $data ) || ! empty( $data['removed'] ) ) {
+ unset( $old[ $key ] );
+ continue;
+ }
+
+ $old[ $key ] = array(
+ 'plugin_name' => $data['plugin_name'],
+ 'policy_text' => $data['policy_text'],
+ );
+ }
+
+ // Normalize the order of texts, to facilitate comparison.
+ sort( $old );
+ sort( $new );
+
+ /*
+ * The == operator (equal, not identical) was used intentionally.
+ * See https://www.php.net/manual/en/language.operators.array.php
+ */
+ if ( $new != $old ) {
+ /*
+ * A plugin was activated or deactivated, or some policy text has changed.
+ * Show a notice on the relevant screens to inform the admin.
+ */
+ add_action( 'admin_notices', array( 'WP_Privacy_Policy_Content', 'policy_text_changed_notice' ) );
+ $state = 'changed';
+ } else {
+ $state = 'not-changed';
+ }
+
+ // Cache the result for use before `admin_init` (see above).
+ if ( $cached !== $state ) {
+ update_option( '_wp_suggested_policy_text_has_changed', $state );
+ }
+
+ return 'changed' === $state;
+ }
+
+ /**
+ * Outputs a warning when some privacy info has changed.
+ *
+ * @since 4.9.6
+ */
+ public static function policy_text_changed_notice() {
+ $screen = get_current_screen()->id;
+
+ if ( 'privacy' !== $screen ) {
+ return;
+ }
+
+ $privacy_message = sprintf(
+ /* translators: %s: Privacy Policy Guide URL. */
+ __( 'The suggested privacy policy text has changed. Please
review the guide and update your privacy policy.' ),
+ esc_url( admin_url( 'privacy-policy-guide.php?tab=policyguide' ) )
+ );
+
+ wp_admin_notice(
+ $privacy_message,
+ array(
+ 'type' => 'warning',
+ 'additional_classes' => array( 'policy-text-updated' ),
+ 'dismissible' => true,
+ )
+ );
+ }
+
+ /**
+ * Updates the cached policy info when the policy page is updated.
+ *
+ * @since 4.9.6
+ * @access private
+ *
+ * @param int $post_id The ID of the updated post.
+ */
+ public static function _policy_page_updated( $post_id ) {
+ $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
+
+ if ( ! $policy_page_id || $policy_page_id !== (int) $post_id ) {
+ return;
+ }
+
+ // Remove updated|removed status.
+ $old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
+ $done = array();
+ $update_cache = false;
+
+ foreach ( $old as $old_key => $old_data ) {
+ if ( ! empty( $old_data['removed'] ) ) {
+ // Remove the old policy text.
+ $update_cache = true;
+ continue;
+ }
+
+ if ( ! empty( $old_data['updated'] ) ) {
+ // 'updated' is now 'added'.
+ $done[] = array(
+ 'plugin_name' => $old_data['plugin_name'],
+ 'policy_text' => $old_data['policy_text'],
+ 'added' => $old_data['updated'],
+ );
+ $update_cache = true;
+ } else {
+ $done[] = $old_data;
+ }
+ }
+
+ if ( $update_cache ) {
+ delete_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
+ // Update the cache.
+ foreach ( $done as $data ) {
+ add_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content', $data );
+ }
+ }
+ }
+
+ /**
+ * Checks for updated, added or removed privacy policy information from plugins.
+ *
+ * Caches the current info in post_meta of the policy page.
+ *
+ * @since 4.9.6
+ *
+ * @return array The privacy policy text/information added by core and plugins.
+ */
+ public static function get_suggested_policy_text() {
+ $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
+ $checked = array();
+ $time = time();
+ $update_cache = false;
+ $new = self::$policy_content;
+ $old = array();
+
+ if ( $policy_page_id ) {
+ $old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
+ }
+
+ // Check for no-changes and updates.
+ foreach ( $new as $new_key => $new_data ) {
+ foreach ( $old as $old_key => $old_data ) {
+ $found = false;
+
+ if ( $new_data['policy_text'] === $old_data['policy_text'] ) {
+ // Use the new plugin name in case it was changed, translated, etc.
+ if ( $old_data['plugin_name'] !== $new_data['plugin_name'] ) {
+ $old_data['plugin_name'] = $new_data['plugin_name'];
+ $update_cache = true;
+ }
+
+ // A plugin was re-activated.
+ if ( ! empty( $old_data['removed'] ) ) {
+ unset( $old_data['removed'] );
+ $old_data['added'] = $time;
+ $update_cache = true;
+ }
+
+ $checked[] = $old_data;
+ $found = true;
+ } elseif ( $new_data['plugin_name'] === $old_data['plugin_name'] ) {
+ // The info for the policy was updated.
+ $checked[] = array(
+ 'plugin_name' => $new_data['plugin_name'],
+ 'policy_text' => $new_data['policy_text'],
+ 'updated' => $time,
+ );
+ $found = true;
+ $update_cache = true;
+ }
+
+ if ( $found ) {
+ unset( $new[ $new_key ], $old[ $old_key ] );
+ continue 2;
+ }
+ }
+ }
+
+ if ( ! empty( $new ) ) {
+ // A plugin was activated.
+ foreach ( $new as $new_data ) {
+ if ( ! empty( $new_data['plugin_name'] ) && ! empty( $new_data['policy_text'] ) ) {
+ $new_data['added'] = $time;
+ $checked[] = $new_data;
+ }
+ }
+ $update_cache = true;
+ }
+
+ if ( ! empty( $old ) ) {
+ // A plugin was deactivated.
+ foreach ( $old as $old_data ) {
+ if ( ! empty( $old_data['plugin_name'] ) && ! empty( $old_data['policy_text'] ) ) {
+ $data = array(
+ 'plugin_name' => $old_data['plugin_name'],
+ 'policy_text' => $old_data['policy_text'],
+ 'removed' => $time,
+ );
+
+ $checked[] = $data;
+ }
+ }
+ $update_cache = true;
+ }
+
+ if ( $update_cache && $policy_page_id ) {
+ delete_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
+ // Update the cache.
+ foreach ( $checked as $data ) {
+ add_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content', $data );
+ }
+ }
+
+ return $checked;
+ }
+
+ /**
+ * Adds a notice with a link to the guide when editing the privacy policy page.
+ *
+ * @since 4.9.6
+ * @since 5.0.0 The `$post` parameter was made optional.
+ *
+ * @global WP_Post $post Global post object.
+ *
+ * @param WP_Post|null $post The currently edited post. Default null.
+ */
+ public static function notice( $post = null ) {
+ if ( is_null( $post ) ) {
+ global $post;
+ } else {
+ $post = get_post( $post );
+ }
+
+ if ( ! ( $post instanceof WP_Post ) ) {
+ return;
+ }
+
+ if ( ! current_user_can( 'manage_privacy_options' ) ) {
+ return;
+ }
+
+ $current_screen = get_current_screen();
+ $policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
+
+ if ( 'post' !== $current_screen->base || $policy_page_id !== $post->ID ) {
+ return;
+ }
+
+ $message = __( 'Need help putting together your new Privacy Policy page? Check out our guide for recommendations on what content to include, along with policies suggested by your plugins and theme.' );
+ $url = esc_url( admin_url( 'options-privacy.php?tab=policyguide' ) );
+ $label = __( 'View Privacy Policy Guide.' );
+
+ if ( get_current_screen()->is_block_editor() ) {
+ wp_enqueue_script( 'wp-notices' );
+ $action = array(
+ 'url' => $url,
+ 'label' => $label,
+ );
+ wp_add_inline_script(
+ 'wp-notices',
+ sprintf(
+ 'wp.data.dispatch( "core/notices" ).createWarningNotice( "%s", { actions: [ %s ], isDismissible: false } )',
+ $message,
+ wp_json_encode( $action )
+ ),
+ 'after'
+ );
+ } else {
+ $message .= sprintf(
+ '
%s %s ',
+ $url,
+ $label,
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ );
+ wp_admin_notice(
+ $message,
+ array(
+ 'type' => 'warning',
+ 'additional_classes' => array( 'inline', 'wp-pp-notice' ),
+ )
+ );
+ }
+ }
+
+ /**
+ * Outputs the privacy policy guide together with content from the theme and plugins.
+ *
+ * @since 4.9.6
+ */
+ public static function privacy_policy_guide() {
+
+ $content_array = self::get_suggested_policy_text();
+ $content = '';
+ $date_format = __( 'F j, Y' );
+
+ foreach ( $content_array as $section ) {
+ $class = '';
+ $meta = '';
+ $removed = '';
+
+ if ( ! empty( $section['removed'] ) ) {
+ $badge_class = ' red';
+ $date = date_i18n( $date_format, $section['removed'] );
+ /* translators: %s: Date of plugin deactivation. */
+ $badge_title = sprintf( __( 'Removed %s.' ), $date );
+
+ /* translators: %s: Date of plugin deactivation. */
+ $removed = sprintf( __( 'You deactivated this plugin on %s and may no longer need this policy.' ), $date );
+ $removed = wp_get_admin_notice(
+ $removed,
+ array(
+ 'type' => 'info',
+ 'additional_classes' => array( 'inline' ),
+ )
+ );
+ } elseif ( ! empty( $section['updated'] ) ) {
+ $badge_class = ' blue';
+ $date = date_i18n( $date_format, $section['updated'] );
+ /* translators: %s: Date of privacy policy text update. */
+ $badge_title = sprintf( __( 'Updated %s.' ), $date );
+ }
+
+ $plugin_name = esc_html( $section['plugin_name'] );
+
+ $sanitized_policy_name = sanitize_title_with_dashes( $plugin_name );
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ' . __( 'Suggested text:' ) . ' ';
+ $content = '';
+ $strings = array();
+
+ // Start of the suggested privacy policy text.
+ if ( $description ) {
+ $strings[] = '
';
+ }
+
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Who we are' ) . ' ';
+
+ if ( $description ) {
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should note your site URL, as well as the name of the company, organization, or individual behind it, and some accurate contact information.' ) . '
';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'The amount of information you may be required to show will vary depending on your local or national business regulations. You may, for example, be required to display a physical address, a registered address, or your company registration number.' ) . '
';
+ } else {
+ /* translators: Default privacy policy text. %s: Site URL. */
+ $strings[] = '
' . $suggested_text . sprintf( __( 'Our website address is: %s.' ), get_bloginfo( 'url', 'display' ) ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'What personal data we collect and why we collect it' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should note what personal data you collect from users and site visitors. This may include personal data, such as name, email address, personal account preferences; transactional data, such as purchase information; and technical data, such as information about cookies.' ) . '
';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'You should also note any collection and retention of sensitive personal data, such as data concerning health.' ) . '
';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In addition to listing what personal data you collect, you need to note why you collect it. These explanations must note either the legal basis for your data collection and retention or the active consent the user has given.' ) . '
';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'Personal data is not just created by a user’s interactions with your site. Personal data is also generated from technical processes such as contact forms, comments, cookies, analytics, and third party embeds.' ) . '
';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'By default WordPress does not collect any personal data about visitors, and only collects the data shown on the User Profile screen from registered users. However some of your plugins may collect personal data. You should add the relevant information below.' ) . '
';
+ }
+
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Comments' ) . ' ';
+
+ if ( $description ) {
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this subsection you should note what information is captured through comments. We have noted the data which WordPress collects by default.' ) . '
';
+ } else {
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . $suggested_text . __( 'When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection.' ) . '
';
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . __( 'An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.' ) . '
';
+ }
+
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Media' ) . ' ';
+
+ if ( $description ) {
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this subsection you should note what information may be disclosed by users who can upload media files. All uploaded files are usually publicly accessible.' ) . '
';
+ } else {
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . $suggested_text . __( 'If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Contact forms' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'By default, WordPress does not include a contact form. If you use a contact form plugin, use this subsection to note what personal data is captured when someone submits a contact form, and how long you keep it. For example, you may note that you keep contact form submissions for a certain period for customer service purposes, but you do not use the information submitted through them for marketing purposes.' ) . '
';
+ }
+
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Cookies' ) . ' ';
+
+ if ( $description ) {
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this subsection you should list the cookies your web site uses, including those set by your plugins, social media, and analytics. We have provided the cookies which WordPress installs by default.' ) . '
';
+ } else {
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . $suggested_text . __( 'If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year.' ) . '
';
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . __( 'If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser.' ) . '
';
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . __( 'When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed.' ) . '
';
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . __( 'If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.' ) . '
';
+ }
+
+ if ( ! $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Embedded content from other websites' ) . ' ';
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . $suggested_text . __( 'Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website.' ) . '
';
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . __( 'These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Analytics' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this subsection you should note what analytics package you use, how users can opt out of analytics tracking, and a link to your analytics provider’s privacy policy, if any.' ) . '
';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'By default WordPress does not collect any analytics data. However, many web hosting accounts collect some anonymous analytics data. You may also have installed a WordPress plugin that provides analytics services. In that case, add information from that plugin here.' ) . '
';
+ }
+
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Who we share your data with' ) . ' ';
+
+ if ( $description ) {
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should name and list all third party providers with whom you share site data, including partners, cloud-based services, payment processors, and third party service providers, and note what data you share with them and why. Link to their own privacy policies if possible.' ) . '
';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'By default WordPress does not share any personal data with anyone.' ) . '
';
+ } else {
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . $suggested_text . __( 'If you request a password reset, your IP address will be included in the reset email.' ) . '
';
+ }
+
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'How long we retain your data' ) . ' ';
+
+ if ( $description ) {
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should explain how long you retain personal data collected or processed by the web site. While it is your responsibility to come up with the schedule of how long you keep each dataset for and why you keep it, that information does need to be listed here. For example, you may want to say that you keep contact form entries for six months, analytics records for a year, and customer purchase records for ten years.' ) . '
';
+ } else {
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . $suggested_text . __( 'If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue.' ) . '
';
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . __( 'For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.' ) . '
';
+ }
+
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'What rights you have over your data' ) . ' ';
+
+ if ( $description ) {
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should explain what rights your users have over their data and how they can invoke those rights.' ) . '
';
+ } else {
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . $suggested_text . __( 'If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.' ) . '
';
+ }
+
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Where your data is sent' ) . ' ';
+
+ if ( $description ) {
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should list all transfers of your site data outside the European Union and describe the means by which that data is safeguarded to European data protection standards. This could include your web hosting, cloud storage, or other third party services.' ) . '
';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'European data protection law requires data about European residents which is transferred outside the European Union to be safeguarded to the same standards as if the data was in Europe. So in addition to listing where data goes, you should describe how you ensure that these standards are met either by yourself or by your third party providers, whether that is through an agreement such as Privacy Shield, model clauses in your contracts, or binding corporate rules.' ) . '
';
+ } else {
+ /* translators: Default privacy policy text. */
+ $strings[] = '
' . $suggested_text . __( 'Visitor comments may be checked through an automated spam detection service.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Contact information' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should provide a contact method for privacy-specific concerns. If you are required to have a Data Protection Officer, list their name and full contact details here as well.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Additional information' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'If you use your site for commercial purposes and you engage in more complex collection or processing of personal data, you should note the following information in your privacy policy in addition to the information we have already discussed.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'How we protect your data' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should explain what measures you have taken to protect your users’ data. This could include technical measures such as encryption; security measures such as two factor authentication; and measures such as staff training in data protection. If you have carried out a Privacy Impact Assessment, you can mention it here too.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'What data breach procedures we have in place' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'In this section you should explain what procedures you have in place to deal with data breaches, either potential or real, such as internal reporting systems, contact mechanisms, or bug bounties.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'What third parties we receive data from' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'If your web site receives data about users from third parties, including advertisers, this information must be included within the section of your privacy policy dealing with third party data.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'What automated decision making and/or profiling we do with user data' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'If your web site provides a service which includes automated decision making - for example, allowing customers to apply for credit, or aggregating their data into an advertising profile - you must note that this is taking place, and include information about how that information is used, what decisions are made with that aggregated data, and what rights users have over decisions made without human intervention.' ) . '
';
+ }
+
+ if ( $description ) {
+ /* translators: Default privacy policy heading. */
+ $strings[] = '
' . __( 'Industry regulatory disclosure requirements' ) . ' ';
+ /* translators: Privacy policy tutorial. */
+ $strings[] = '
' . __( 'If you are a member of a regulated industry, or if you are subject to additional privacy laws, you may be required to disclose that information here.' ) . '
';
+ $strings[] = '
';
+ }
+
+ if ( $blocks ) {
+ foreach ( $strings as $key => $string ) {
+ if ( str_starts_with( $string, '
' ) ) {
+ $strings[ $key ] = '' . $string . '';
+ }
+
+ if ( str_starts_with( $string, '
' ) ) {
+ $strings[ $key ] = '' . $string . '';
+ }
+ }
+ }
+
+ $content = implode( '', $strings );
+ // End of the suggested privacy policy text.
+
+ /**
+ * Filters the default content suggested for inclusion in a privacy policy.
+ *
+ * @since 4.9.6
+ * @since 5.0.0 Added the `$strings`, `$description`, and `$blocks` parameters.
+ * @deprecated 5.7.0 Use wp_add_privacy_policy_content() instead.
+ *
+ * @param string $content The default policy content.
+ * @param string[] $strings An array of privacy policy content strings.
+ * @param bool $description Whether policy descriptions should be included.
+ * @param bool $blocks Whether the content should be formatted for the block editor.
+ */
+ return apply_filters_deprecated(
+ 'wp_get_default_privacy_policy_content',
+ array( $content, $strings, $description, $blocks ),
+ '5.7.0',
+ 'wp_add_privacy_policy_content()'
+ );
+ }
+
+ /**
+ * Adds the suggested privacy policy text to the policy postbox.
+ *
+ * @since 4.9.6
+ */
+ public static function add_suggested_content() {
+ $content = self::get_default_content( false, false );
+ wp_add_privacy_policy_content( __( 'WordPress' ), $content );
+ }
+}
diff --git a/wp-admin/includes/class-wp-privacy-requests-table.php b/wp-admin/includes/class-wp-privacy-requests-table.php
new file mode 100644
index 0000000..61a917c
--- /dev/null
+++ b/wp-admin/includes/class-wp-privacy-requests-table.php
@@ -0,0 +1,565 @@
+ ' ',
+ 'email' => __( 'Requester' ),
+ 'status' => __( 'Status' ),
+ 'created_timestamp' => __( 'Requested' ),
+ 'next_steps' => __( 'Next steps' ),
+ );
+ return $columns;
+ }
+
+ /**
+ * Normalizes the admin URL to the current page (by request_type).
+ *
+ * @since 5.3.0
+ *
+ * @return string URL to the current admin page.
+ */
+ protected function get_admin_url() {
+ $pagenow = str_replace( '_', '-', $this->request_type );
+
+ if ( 'remove-personal-data' === $pagenow ) {
+ $pagenow = 'erase-personal-data';
+ }
+
+ return admin_url( $pagenow . '.php' );
+ }
+
+ /**
+ * Gets a list of sortable columns.
+ *
+ * @since 4.9.6
+ *
+ * @return array Default sortable columns.
+ */
+ protected function get_sortable_columns() {
+ /*
+ * The initial sorting is by 'Requested' (post_date) and descending.
+ * With initial sorting, the first click on 'Requested' should be ascending.
+ * With 'Requester' sorting active, the next click on 'Requested' should be descending.
+ */
+ $desc_first = isset( $_GET['orderby'] );
+
+ return array(
+ 'email' => 'requester',
+ 'created_timestamp' => array( 'requested', $desc_first ),
+ );
+ }
+
+ /**
+ * Returns the default primary column.
+ *
+ * @since 4.9.6
+ *
+ * @return string Default primary column name.
+ */
+ protected function get_default_primary_column_name() {
+ return 'email';
+ }
+
+ /**
+ * Counts the number of requests for each status.
+ *
+ * @since 4.9.6
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @return object Number of posts for each status.
+ */
+ protected function get_request_counts() {
+ global $wpdb;
+
+ $cache_key = $this->post_type . '-' . $this->request_type;
+ $counts = wp_cache_get( $cache_key, 'counts' );
+
+ if ( false !== $counts ) {
+ return $counts;
+ }
+
+ $query = "
+ SELECT post_status, COUNT( * ) AS num_posts
+ FROM {$wpdb->posts}
+ WHERE post_type = %s
+ AND post_name = %s
+ GROUP BY post_status";
+
+ $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $this->post_type, $this->request_type ), ARRAY_A );
+ $counts = array_fill_keys( get_post_stati(), 0 );
+
+ foreach ( $results as $row ) {
+ $counts[ $row['post_status'] ] = $row['num_posts'];
+ }
+
+ $counts = (object) $counts;
+ wp_cache_set( $cache_key, $counts, 'counts' );
+
+ return $counts;
+ }
+
+ /**
+ * Gets an associative array ( id => link ) with the list of views available on this table.
+ *
+ * @since 4.9.6
+ *
+ * @return string[] An array of HTML links keyed by their view.
+ */
+ protected function get_views() {
+ $current_status = isset( $_REQUEST['filter-status'] ) ? sanitize_text_field( $_REQUEST['filter-status'] ) : '';
+ $statuses = _wp_privacy_statuses();
+ $views = array();
+ $counts = $this->get_request_counts();
+ $total_requests = absint( array_sum( (array) $counts ) );
+
+ // Normalized admin URL.
+ $admin_url = $this->get_admin_url();
+
+ $status_label = sprintf(
+ /* translators: %s: Number of requests. */
+ _nx(
+ 'All (%s) ',
+ 'All (%s) ',
+ $total_requests,
+ 'requests'
+ ),
+ number_format_i18n( $total_requests )
+ );
+
+ $views['all'] = array(
+ 'url' => esc_url( $admin_url ),
+ 'label' => $status_label,
+ 'current' => empty( $current_status ),
+ );
+
+ foreach ( $statuses as $status => $label ) {
+ $post_status = get_post_status_object( $status );
+ if ( ! $post_status ) {
+ continue;
+ }
+
+ $total_status_requests = absint( $counts->{$status} );
+
+ if ( ! $total_status_requests ) {
+ continue;
+ }
+
+ $status_label = sprintf(
+ translate_nooped_plural( $post_status->label_count, $total_status_requests ),
+ number_format_i18n( $total_status_requests )
+ );
+
+ $status_link = add_query_arg( 'filter-status', $status, $admin_url );
+
+ $views[ $status ] = array(
+ 'url' => esc_url( $status_link ),
+ 'label' => $status_label,
+ 'current' => $status === $current_status,
+ );
+ }
+
+ return $this->get_views_links( $views );
+ }
+
+ /**
+ * Gets bulk actions.
+ *
+ * @since 4.9.6
+ *
+ * @return array Array of bulk action labels keyed by their action.
+ */
+ protected function get_bulk_actions() {
+ return array(
+ 'resend' => __( 'Resend confirmation requests' ),
+ 'complete' => __( 'Mark requests as completed' ),
+ 'delete' => __( 'Delete requests' ),
+ );
+ }
+
+ /**
+ * Process bulk actions.
+ *
+ * @since 4.9.6
+ * @since 5.6.0 Added support for the `complete` action.
+ */
+ public function process_bulk_action() {
+ $action = $this->current_action();
+ $request_ids = isset( $_REQUEST['request_id'] ) ? wp_parse_id_list( wp_unslash( $_REQUEST['request_id'] ) ) : array();
+
+ if ( empty( $request_ids ) ) {
+ return;
+ }
+
+ $count = 0;
+ $failures = 0;
+
+ check_admin_referer( 'bulk-privacy_requests' );
+
+ switch ( $action ) {
+ case 'resend':
+ foreach ( $request_ids as $request_id ) {
+ $resend = _wp_privacy_resend_request( $request_id );
+
+ if ( $resend && ! is_wp_error( $resend ) ) {
+ ++$count;
+ } else {
+ ++$failures;
+ }
+ }
+
+ if ( $failures ) {
+ add_settings_error(
+ 'bulk_action',
+ 'bulk_action',
+ sprintf(
+ /* translators: %d: Number of requests. */
+ _n(
+ '%d confirmation request failed to resend.',
+ '%d confirmation requests failed to resend.',
+ $failures
+ ),
+ $failures
+ ),
+ 'error'
+ );
+ }
+
+ if ( $count ) {
+ add_settings_error(
+ 'bulk_action',
+ 'bulk_action',
+ sprintf(
+ /* translators: %d: Number of requests. */
+ _n(
+ '%d confirmation request re-sent successfully.',
+ '%d confirmation requests re-sent successfully.',
+ $count
+ ),
+ $count
+ ),
+ 'success'
+ );
+ }
+
+ break;
+
+ case 'complete':
+ foreach ( $request_ids as $request_id ) {
+ $result = _wp_privacy_completed_request( $request_id );
+
+ if ( $result && ! is_wp_error( $result ) ) {
+ ++$count;
+ }
+ }
+
+ add_settings_error(
+ 'bulk_action',
+ 'bulk_action',
+ sprintf(
+ /* translators: %d: Number of requests. */
+ _n(
+ '%d request marked as complete.',
+ '%d requests marked as complete.',
+ $count
+ ),
+ $count
+ ),
+ 'success'
+ );
+ break;
+
+ case 'delete':
+ foreach ( $request_ids as $request_id ) {
+ if ( wp_delete_post( $request_id, true ) ) {
+ ++$count;
+ } else {
+ ++$failures;
+ }
+ }
+
+ if ( $failures ) {
+ add_settings_error(
+ 'bulk_action',
+ 'bulk_action',
+ sprintf(
+ /* translators: %d: Number of requests. */
+ _n(
+ '%d request failed to delete.',
+ '%d requests failed to delete.',
+ $failures
+ ),
+ $failures
+ ),
+ 'error'
+ );
+ }
+
+ if ( $count ) {
+ add_settings_error(
+ 'bulk_action',
+ 'bulk_action',
+ sprintf(
+ /* translators: %d: Number of requests. */
+ _n(
+ '%d request deleted successfully.',
+ '%d requests deleted successfully.',
+ $count
+ ),
+ $count
+ ),
+ 'success'
+ );
+ }
+
+ break;
+ }
+ }
+
+ /**
+ * Prepares items to output.
+ *
+ * @since 4.9.6
+ * @since 5.1.0 Added support for column sorting.
+ */
+ public function prepare_items() {
+ $this->items = array();
+ $posts_per_page = $this->get_items_per_page( $this->request_type . '_requests_per_page' );
+ $args = array(
+ 'post_type' => $this->post_type,
+ 'post_name__in' => array( $this->request_type ),
+ 'posts_per_page' => $posts_per_page,
+ 'offset' => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page : 0,
+ 'post_status' => 'any',
+ 's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '',
+ );
+
+ $orderby_mapping = array(
+ 'requester' => 'post_title',
+ 'requested' => 'post_date',
+ );
+
+ if ( isset( $_REQUEST['orderby'] ) && isset( $orderby_mapping[ $_REQUEST['orderby'] ] ) ) {
+ $args['orderby'] = $orderby_mapping[ $_REQUEST['orderby'] ];
+ }
+
+ if ( isset( $_REQUEST['order'] ) && in_array( strtoupper( $_REQUEST['order'] ), array( 'ASC', 'DESC' ), true ) ) {
+ $args['order'] = strtoupper( $_REQUEST['order'] );
+ }
+
+ if ( ! empty( $_REQUEST['filter-status'] ) ) {
+ $filter_status = isset( $_REQUEST['filter-status'] ) ? sanitize_text_field( $_REQUEST['filter-status'] ) : '';
+ $args['post_status'] = $filter_status;
+ }
+
+ $requests_query = new WP_Query( $args );
+ $requests = $requests_query->posts;
+
+ foreach ( $requests as $request ) {
+ $this->items[] = wp_get_user_request( $request->ID );
+ }
+
+ $this->items = array_filter( $this->items );
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $requests_query->found_posts,
+ 'per_page' => $posts_per_page,
+ )
+ );
+ }
+
+ /**
+ * Returns the markup for the Checkbox column.
+ *
+ * @since 4.9.6
+ *
+ * @param WP_User_Request $item Item being shown.
+ * @return string Checkbox column markup.
+ */
+ public function column_cb( $item ) {
+ return sprintf(
+ ' ' .
+ '%2$s ',
+ esc_attr( $item->ID ),
+ /* translators: Hidden accessibility text. %s: Email address. */
+ sprintf( __( 'Select %s' ), $item->email )
+ );
+ }
+
+ /**
+ * Status column.
+ *
+ * @since 4.9.6
+ *
+ * @param WP_User_Request $item Item being shown.
+ * @return string Status column markup.
+ */
+ public function column_status( $item ) {
+ $status = get_post_status( $item->ID );
+ $status_object = get_post_status_object( $status );
+
+ if ( ! $status_object || empty( $status_object->label ) ) {
+ return '-';
+ }
+
+ $timestamp = false;
+
+ switch ( $status ) {
+ case 'request-confirmed':
+ $timestamp = $item->confirmed_timestamp;
+ break;
+ case 'request-completed':
+ $timestamp = $item->completed_timestamp;
+ break;
+ }
+
+ echo '';
+ echo esc_html( $status_object->label );
+
+ if ( $timestamp ) {
+ echo ' (' . $this->get_timestamp_as_date( $timestamp ) . ')';
+ }
+
+ echo ' ';
+ }
+
+ /**
+ * Converts a timestamp for display.
+ *
+ * @since 4.9.6
+ *
+ * @param int $timestamp Event timestamp.
+ * @return string Human readable date.
+ */
+ protected function get_timestamp_as_date( $timestamp ) {
+ if ( empty( $timestamp ) ) {
+ return '';
+ }
+
+ $time_diff = time() - $timestamp;
+
+ if ( $time_diff >= 0 && $time_diff < DAY_IN_SECONDS ) {
+ /* translators: %s: Human-readable time difference. */
+ return sprintf( __( '%s ago' ), human_time_diff( $timestamp ) );
+ }
+
+ return date_i18n( get_option( 'date_format' ), $timestamp );
+ }
+
+ /**
+ * Handles the default column.
+ *
+ * @since 4.9.6
+ * @since 5.7.0 Added `manage_{$this->screen->id}_custom_column` action.
+ *
+ * @param WP_User_Request $item Item being shown.
+ * @param string $column_name Name of column being shown.
+ */
+ public function column_default( $item, $column_name ) {
+ /**
+ * Fires for each custom column of a specific request type in the Requests list table.
+ *
+ * Custom columns are registered using the {@see 'manage_export-personal-data_columns'}
+ * and the {@see 'manage_erase-personal-data_columns'} filters.
+ *
+ * @since 5.7.0
+ *
+ * @param string $column_name The name of the column to display.
+ * @param WP_User_Request $item The item being shown.
+ */
+ do_action( "manage_{$this->screen->id}_custom_column", $column_name, $item );
+ }
+
+ /**
+ * Returns the markup for the Created timestamp column. Overridden by children.
+ *
+ * @since 5.7.0
+ *
+ * @param WP_User_Request $item Item being shown.
+ * @return string Human readable date.
+ */
+ public function column_created_timestamp( $item ) {
+ return $this->get_timestamp_as_date( $item->created_timestamp );
+ }
+
+ /**
+ * Actions column. Overridden by children.
+ *
+ * @since 4.9.6
+ *
+ * @param WP_User_Request $item Item being shown.
+ * @return string Email column markup.
+ */
+ public function column_email( $item ) {
+ return sprintf( '%2$s %3$s', esc_url( 'mailto:' . $item->email ), $item->email, $this->row_actions( array() ) );
+ }
+
+ /**
+ * Returns the markup for the next steps column. Overridden by children.
+ *
+ * @since 4.9.6
+ *
+ * @param WP_User_Request $item Item being shown.
+ */
+ public function column_next_steps( $item ) {}
+
+ /**
+ * Generates content for a single row of the table,
+ *
+ * @since 4.9.6
+ *
+ * @param WP_User_Request $item The current item.
+ */
+ public function single_row( $item ) {
+ $status = $item->status;
+
+ echo '';
+ $this->single_row_columns( $item );
+ echo ' ';
+ }
+
+ /**
+ * Embeds scripts used to perform actions. Overridden by children.
+ *
+ * @since 4.9.6
+ */
+ public function embed_scripts() {}
+}
diff --git a/wp-admin/includes/class-wp-screen.php b/wp-admin/includes/class-wp-screen.php
new file mode 100644
index 0000000..739a182
--- /dev/null
+++ b/wp-admin/includes/class-wp-screen.php
@@ -0,0 +1,1359 @@
+post_type;
+
+ /** This filter is documented in wp-admin/post.php */
+ $replace_editor = apply_filters( 'replace_editor', false, $post );
+
+ if ( ! $replace_editor ) {
+ $is_block_editor = use_block_editor_for_post( $post );
+ }
+ }
+ }
+ break;
+ case 'edit-tags':
+ case 'term':
+ if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) ) {
+ $post_type = 'post';
+ }
+ break;
+ case 'upload':
+ $post_type = 'attachment';
+ break;
+ }
+ }
+
+ switch ( $base ) {
+ case 'post':
+ if ( null === $post_type ) {
+ $post_type = 'post';
+ }
+
+ // When creating a new post, use the default block editor support value for the post type.
+ if ( empty( $post_id ) ) {
+ $is_block_editor = use_block_editor_for_post_type( $post_type );
+ }
+
+ $id = $post_type;
+ break;
+ case 'edit':
+ if ( null === $post_type ) {
+ $post_type = 'post';
+ }
+ $id .= '-' . $post_type;
+ break;
+ case 'edit-tags':
+ case 'term':
+ if ( null === $taxonomy ) {
+ $taxonomy = 'post_tag';
+ }
+ // The edit-tags ID does not contain the post type. Look for it in the request.
+ if ( null === $post_type ) {
+ $post_type = 'post';
+ if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) {
+ $post_type = $_REQUEST['post_type'];
+ }
+ }
+
+ $id = 'edit-' . $taxonomy;
+ break;
+ }
+
+ if ( 'network' === $in_admin ) {
+ $id .= '-network';
+ $base .= '-network';
+ } elseif ( 'user' === $in_admin ) {
+ $id .= '-user';
+ $base .= '-user';
+ }
+
+ if ( isset( self::$_registry[ $id ] ) ) {
+ $screen = self::$_registry[ $id ];
+ if ( get_current_screen() === $screen ) {
+ return $screen;
+ }
+ } else {
+ $screen = new self();
+ $screen->id = $id;
+ }
+
+ $screen->base = $base;
+ $screen->action = $action;
+ $screen->post_type = (string) $post_type;
+ $screen->taxonomy = (string) $taxonomy;
+ $screen->is_user = ( 'user' === $in_admin );
+ $screen->is_network = ( 'network' === $in_admin );
+ $screen->in_admin = $in_admin;
+ $screen->is_block_editor = $is_block_editor;
+
+ self::$_registry[ $id ] = $screen;
+
+ return $screen;
+ }
+
+ /**
+ * Makes the screen object the current screen.
+ *
+ * @see set_current_screen()
+ * @since 3.3.0
+ *
+ * @global WP_Screen $current_screen WordPress current screen object.
+ * @global string $typenow The post type of the current screen.
+ * @global string $taxnow The taxonomy of the current screen.
+ */
+ public function set_current_screen() {
+ global $current_screen, $taxnow, $typenow;
+
+ $current_screen = $this;
+ $typenow = $this->post_type;
+ $taxnow = $this->taxonomy;
+
+ /**
+ * Fires after the current screen has been set.
+ *
+ * @since 3.0.0
+ *
+ * @param WP_Screen $current_screen Current WP_Screen object.
+ */
+ do_action( 'current_screen', $current_screen );
+ }
+
+ /**
+ * Constructor
+ *
+ * @since 3.3.0
+ */
+ private function __construct() {}
+
+ /**
+ * Indicates whether the screen is in a particular admin.
+ *
+ * @since 3.5.0
+ *
+ * @param string $admin The admin to check against (network | user | site).
+ * If empty any of the three admins will result in true.
+ * @return bool True if the screen is in the indicated admin, false otherwise.
+ */
+ public function in_admin( $admin = null ) {
+ if ( empty( $admin ) ) {
+ return (bool) $this->in_admin;
+ }
+
+ return ( $admin === $this->in_admin );
+ }
+
+ /**
+ * Sets or returns whether the block editor is loading on the current screen.
+ *
+ * @since 5.0.0
+ *
+ * @param bool $set Optional. Sets whether the block editor is loading on the current screen or not.
+ * @return bool True if the block editor is being loaded, false otherwise.
+ */
+ public function is_block_editor( $set = null ) {
+ if ( null !== $set ) {
+ $this->is_block_editor = (bool) $set;
+ }
+
+ return $this->is_block_editor;
+ }
+
+ /**
+ * Sets the old string-based contextual help for the screen for backward compatibility.
+ *
+ * @since 3.3.0
+ *
+ * @param WP_Screen $screen A screen object.
+ * @param string $help Help text.
+ */
+ public static function add_old_compat_help( $screen, $help ) {
+ self::$_old_compat_help[ $screen->id ] = $help;
+ }
+
+ /**
+ * Sets the parent information for the screen.
+ *
+ * This is called in admin-header.php after the menu parent for the screen has been determined.
+ *
+ * @since 3.3.0
+ *
+ * @param string $parent_file The parent file of the screen. Typically the $parent_file global.
+ */
+ public function set_parentage( $parent_file ) {
+ $this->parent_file = $parent_file;
+ list( $this->parent_base ) = explode( '?', $parent_file );
+ $this->parent_base = str_replace( '.php', '', $this->parent_base );
+ }
+
+ /**
+ * Adds an option for the screen.
+ *
+ * Call this in template files after admin.php is loaded and before admin-header.php is loaded
+ * to add screen options.
+ *
+ * @since 3.3.0
+ *
+ * @param string $option Option ID.
+ * @param mixed $args Option-dependent arguments.
+ */
+ public function add_option( $option, $args = array() ) {
+ $this->_options[ $option ] = $args;
+ }
+
+ /**
+ * Removes an option from the screen.
+ *
+ * @since 3.8.0
+ *
+ * @param string $option Option ID.
+ */
+ public function remove_option( $option ) {
+ unset( $this->_options[ $option ] );
+ }
+
+ /**
+ * Removes all options from the screen.
+ *
+ * @since 3.8.0
+ */
+ public function remove_options() {
+ $this->_options = array();
+ }
+
+ /**
+ * Gets the options registered for the screen.
+ *
+ * @since 3.8.0
+ *
+ * @return array Options with arguments.
+ */
+ public function get_options() {
+ return $this->_options;
+ }
+
+ /**
+ * Gets the arguments for an option for the screen.
+ *
+ * @since 3.3.0
+ *
+ * @param string $option Option name.
+ * @param string|false $key Optional. Specific array key for when the option is an array.
+ * Default false.
+ * @return string The option value if set, null otherwise.
+ */
+ public function get_option( $option, $key = false ) {
+ if ( ! isset( $this->_options[ $option ] ) ) {
+ return null;
+ }
+ if ( $key ) {
+ if ( isset( $this->_options[ $option ][ $key ] ) ) {
+ return $this->_options[ $option ][ $key ];
+ }
+ return null;
+ }
+ return $this->_options[ $option ];
+ }
+
+ /**
+ * Gets the help tabs registered for the screen.
+ *
+ * @since 3.4.0
+ * @since 4.4.0 Help tabs are ordered by their priority.
+ *
+ * @return array Help tabs with arguments.
+ */
+ public function get_help_tabs() {
+ $help_tabs = $this->_help_tabs;
+
+ $priorities = array();
+ foreach ( $help_tabs as $help_tab ) {
+ if ( isset( $priorities[ $help_tab['priority'] ] ) ) {
+ $priorities[ $help_tab['priority'] ][] = $help_tab;
+ } else {
+ $priorities[ $help_tab['priority'] ] = array( $help_tab );
+ }
+ }
+
+ ksort( $priorities );
+
+ $sorted = array();
+ foreach ( $priorities as $list ) {
+ foreach ( $list as $tab ) {
+ $sorted[ $tab['id'] ] = $tab;
+ }
+ }
+
+ return $sorted;
+ }
+
+ /**
+ * Gets the arguments for a help tab.
+ *
+ * @since 3.4.0
+ *
+ * @param string $id Help Tab ID.
+ * @return array Help tab arguments.
+ */
+ public function get_help_tab( $id ) {
+ if ( ! isset( $this->_help_tabs[ $id ] ) ) {
+ return null;
+ }
+ return $this->_help_tabs[ $id ];
+ }
+
+ /**
+ * Adds a help tab to the contextual help for the screen.
+ *
+ * Call this on the `load-$pagenow` hook for the relevant screen,
+ * or fetch the `$current_screen` object, or use get_current_screen()
+ * and then call the method from the object.
+ *
+ * You may need to filter `$current_screen` using an if or switch statement
+ * to prevent new help tabs from being added to ALL admin screens.
+ *
+ * @since 3.3.0
+ * @since 4.4.0 The `$priority` argument was added.
+ *
+ * @param array $args {
+ * Array of arguments used to display the help tab.
+ *
+ * @type string $title Title for the tab. Default false.
+ * @type string $id Tab ID. Must be HTML-safe and should be unique for this menu.
+ * It is NOT allowed to contain any empty spaces. Default false.
+ * @type string $content Optional. Help tab content in plain text or HTML. Default empty string.
+ * @type callable $callback Optional. A callback to generate the tab content. Default false.
+ * @type int $priority Optional. The priority of the tab, used for ordering. Default 10.
+ * }
+ */
+ public function add_help_tab( $args ) {
+ $defaults = array(
+ 'title' => false,
+ 'id' => false,
+ 'content' => '',
+ 'callback' => false,
+ 'priority' => 10,
+ );
+ $args = wp_parse_args( $args, $defaults );
+
+ $args['id'] = sanitize_html_class( $args['id'] );
+
+ // Ensure we have an ID and title.
+ if ( ! $args['id'] || ! $args['title'] ) {
+ return;
+ }
+
+ // Allows for overriding an existing tab with that ID.
+ $this->_help_tabs[ $args['id'] ] = $args;
+ }
+
+ /**
+ * Removes a help tab from the contextual help for the screen.
+ *
+ * @since 3.3.0
+ *
+ * @param string $id The help tab ID.
+ */
+ public function remove_help_tab( $id ) {
+ unset( $this->_help_tabs[ $id ] );
+ }
+
+ /**
+ * Removes all help tabs from the contextual help for the screen.
+ *
+ * @since 3.3.0
+ */
+ public function remove_help_tabs() {
+ $this->_help_tabs = array();
+ }
+
+ /**
+ * Gets the content from a contextual help sidebar.
+ *
+ * @since 3.4.0
+ *
+ * @return string Contents of the help sidebar.
+ */
+ public function get_help_sidebar() {
+ return $this->_help_sidebar;
+ }
+
+ /**
+ * Adds a sidebar to the contextual help for the screen.
+ *
+ * Call this in template files after admin.php is loaded and before admin-header.php is loaded
+ * to add a sidebar to the contextual help.
+ *
+ * @since 3.3.0
+ *
+ * @param string $content Sidebar content in plain text or HTML.
+ */
+ public function set_help_sidebar( $content ) {
+ $this->_help_sidebar = $content;
+ }
+
+ /**
+ * Gets the number of layout columns the user has selected.
+ *
+ * The layout_columns option controls the max number and default number of
+ * columns. This method returns the number of columns within that range selected
+ * by the user via Screen Options. If no selection has been made, the default
+ * provisioned in layout_columns is returned. If the screen does not support
+ * selecting the number of layout columns, 0 is returned.
+ *
+ * @since 3.4.0
+ *
+ * @return int Number of columns to display.
+ */
+ public function get_columns() {
+ return $this->columns;
+ }
+
+ /**
+ * Gets the accessible hidden headings and text used in the screen.
+ *
+ * @since 4.4.0
+ *
+ * @see set_screen_reader_content() For more information on the array format.
+ *
+ * @return string[] An associative array of screen reader text strings.
+ */
+ public function get_screen_reader_content() {
+ return $this->_screen_reader_content;
+ }
+
+ /**
+ * Gets a screen reader text string.
+ *
+ * @since 4.4.0
+ *
+ * @param string $key Screen reader text array named key.
+ * @return string Screen reader text string.
+ */
+ public function get_screen_reader_text( $key ) {
+ if ( ! isset( $this->_screen_reader_content[ $key ] ) ) {
+ return null;
+ }
+ return $this->_screen_reader_content[ $key ];
+ }
+
+ /**
+ * Adds accessible hidden headings and text for the screen.
+ *
+ * @since 4.4.0
+ *
+ * @param array $content {
+ * An associative array of screen reader text strings.
+ *
+ * @type string $heading_views Screen reader text for the filter links heading.
+ * Default 'Filter items list'.
+ * @type string $heading_pagination Screen reader text for the pagination heading.
+ * Default 'Items list navigation'.
+ * @type string $heading_list Screen reader text for the items list heading.
+ * Default 'Items list'.
+ * }
+ */
+ public function set_screen_reader_content( $content = array() ) {
+ $defaults = array(
+ 'heading_views' => __( 'Filter items list' ),
+ 'heading_pagination' => __( 'Items list navigation' ),
+ 'heading_list' => __( 'Items list' ),
+ );
+ $content = wp_parse_args( $content, $defaults );
+
+ $this->_screen_reader_content = $content;
+ }
+
+ /**
+ * Removes all the accessible hidden headings and text for the screen.
+ *
+ * @since 4.4.0
+ */
+ public function remove_screen_reader_content() {
+ $this->_screen_reader_content = array();
+ }
+
+ /**
+ * Renders the screen's help section.
+ *
+ * This will trigger the deprecated filters for backward compatibility.
+ *
+ * @since 3.3.0
+ *
+ * @global string $screen_layout_columns
+ */
+ public function render_screen_meta() {
+
+ /**
+ * Filters the legacy contextual help list.
+ *
+ * @since 2.7.0
+ * @deprecated 3.3.0 Use {@see get_current_screen()->add_help_tab()} or
+ * {@see get_current_screen()->remove_help_tab()} instead.
+ *
+ * @param array $old_compat_help Old contextual help.
+ * @param WP_Screen $screen Current WP_Screen instance.
+ */
+ self::$_old_compat_help = apply_filters_deprecated(
+ 'contextual_help_list',
+ array( self::$_old_compat_help, $this ),
+ '3.3.0',
+ 'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()'
+ );
+
+ $old_help = isset( self::$_old_compat_help[ $this->id ] ) ? self::$_old_compat_help[ $this->id ] : '';
+
+ /**
+ * Filters the legacy contextual help text.
+ *
+ * @since 2.7.0
+ * @deprecated 3.3.0 Use {@see get_current_screen()->add_help_tab()} or
+ * {@see get_current_screen()->remove_help_tab()} instead.
+ *
+ * @param string $old_help Help text that appears on the screen.
+ * @param string $screen_id Screen ID.
+ * @param WP_Screen $screen Current WP_Screen instance.
+ */
+ $old_help = apply_filters_deprecated(
+ 'contextual_help',
+ array( $old_help, $this->id, $this ),
+ '3.3.0',
+ 'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()'
+ );
+
+ // Default help only if there is no old-style block of text and no new-style help tabs.
+ if ( empty( $old_help ) && ! $this->get_help_tabs() ) {
+
+ /**
+ * Filters the default legacy contextual help text.
+ *
+ * @since 2.8.0
+ * @deprecated 3.3.0 Use {@see get_current_screen()->add_help_tab()} or
+ * {@see get_current_screen()->remove_help_tab()} instead.
+ *
+ * @param string $old_help_default Default contextual help text.
+ */
+ $default_help = apply_filters_deprecated(
+ 'default_contextual_help',
+ array( '' ),
+ '3.3.0',
+ 'get_current_screen()->add_help_tab(), get_current_screen()->remove_help_tab()'
+ );
+ if ( $default_help ) {
+ $old_help = ' ' . $default_help . '
';
+ }
+ }
+
+ if ( $old_help ) {
+ $this->add_help_tab(
+ array(
+ 'id' => 'old-contextual-help',
+ 'title' => __( 'Overview' ),
+ 'content' => $old_help,
+ )
+ );
+ }
+
+ $help_sidebar = $this->get_help_sidebar();
+
+ $help_class = 'hidden';
+ if ( ! $help_sidebar ) {
+ $help_class .= ' no-sidebar';
+ }
+
+ // Time to render!
+ ?>
+
+ get_help_tabs() && ! $this->show_screen_options() ) {
+ return;
+ }
+ ?>
+
+ _show_screen_options ) ) {
+ return $this->_show_screen_options;
+ }
+
+ $columns = get_column_headers( $this );
+
+ $show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' );
+
+ $this->_screen_settings = '';
+
+ if ( 'post' === $this->base ) {
+ $expand = '
' . __( 'Additional settings' ) . ' ';
+ $expand .= ' ';
+ $expand .= __( 'Enable full-height editor and distraction-free functionality.' ) . ' ';
+ $this->_screen_settings = $expand;
+ }
+
+ /**
+ * Filters the screen settings text displayed in the Screen Options tab.
+ *
+ * @since 3.0.0
+ *
+ * @param string $screen_settings Screen settings.
+ * @param WP_Screen $screen WP_Screen object.
+ */
+ $this->_screen_settings = apply_filters( 'screen_settings', $this->_screen_settings, $this );
+
+ if ( $this->_screen_settings || $this->_options ) {
+ $show_screen = true;
+ }
+
+ /**
+ * Filters whether to show the Screen Options tab.
+ *
+ * @since 3.2.0
+ *
+ * @param bool $show_screen Whether to show Screen Options tab.
+ * Default true.
+ * @param WP_Screen $screen Current WP_Screen instance.
+ */
+ $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this );
+ return $this->_show_screen_options;
+ }
+
+ /**
+ * Renders the screen options tab.
+ *
+ * @since 3.3.0
+ *
+ * @param array $options {
+ * Options for the tab.
+ *
+ * @type bool $wrap Whether the screen-options-wrap div will be included. Defaults to true.
+ * }
+ */
+ public function render_screen_options( $options = array() ) {
+ $options = wp_parse_args(
+ $options,
+ array(
+ 'wrap' => true,
+ )
+ );
+
+ $wrapper_start = '';
+ $wrapper_end = '';
+ $form_start = '';
+ $form_end = '';
+
+ // Output optional wrapper.
+ if ( $options['wrap'] ) {
+ $wrapper_start = '
';
+ $wrapper_end = '
';
+ }
+
+ // Don't output the form and nonce for the widgets accessibility mode links.
+ if ( 'widgets' !== $this->base ) {
+ $form_start = "\n
\n";
+ $form_end = "\n" . wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false, false ) . "\n \n";
+ }
+
+ echo $wrapper_start . $form_start;
+
+ $this->render_meta_boxes_preferences();
+ $this->render_list_table_columns_preferences();
+ $this->render_screen_layout();
+ $this->render_per_page_options();
+ $this->render_view_mode();
+ echo $this->_screen_settings;
+
+ /**
+ * Filters whether to show the Screen Options submit button.
+ *
+ * @since 4.4.0
+ *
+ * @param bool $show_button Whether to show Screen Options submit button.
+ * Default false.
+ * @param WP_Screen $screen Current WP_Screen instance.
+ */
+ $show_button = apply_filters( 'screen_options_show_submit', false, $this );
+
+ if ( $show_button ) {
+ submit_button( __( 'Apply' ), 'primary', 'screen-options-apply', true );
+ }
+
+ echo $form_end . $wrapper_end;
+ }
+
+ /**
+ * Renders the meta boxes preferences.
+ *
+ * @since 4.4.0
+ *
+ * @global array $wp_meta_boxes
+ */
+ public function render_meta_boxes_preferences() {
+ global $wp_meta_boxes;
+
+ if ( ! isset( $wp_meta_boxes[ $this->id ] ) ) {
+ return;
+ }
+ ?>
+
+
+
+
+
+
+
+ id && has_action( 'welcome_panel' ) && current_user_can( 'edit_theme_options' ) ) {
+ if ( isset( $_GET['welcome'] ) ) {
+ $welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1;
+ update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked );
+ } else {
+ $welcome_checked = (int) get_user_meta( get_current_user_id(), 'show_welcome_panel', true );
+ if ( 2 === $welcome_checked && wp_get_current_user()->user_email !== get_option( 'admin_email' ) ) {
+ $welcome_checked = false;
+ }
+ }
+ echo '';
+ echo ' ';
+ echo _x( 'Welcome', 'Welcome panel' ) . " \n";
+ }
+ ?>
+
+
+
+
+
+ $title ) {
+ // Can't hide these for they are special.
+ if ( in_array( $column, $special, true ) ) {
+ continue;
+ }
+
+ if ( empty( $title ) ) {
+ continue;
+ }
+
+ /*
+ * The Comments column uses HTML in the display name with some screen
+ * reader text. Make sure to strip tags from the Comments column
+ * title and any other custom column title plugins might add.
+ */
+ $title = wp_strip_all_tags( $title );
+
+ $id = "$column-hide";
+ echo '';
+ echo ' ';
+ echo "$title \n";
+ }
+ ?>
+
+ get_option( 'layout_columns' ) ) {
+ return;
+ }
+
+ $screen_layout_columns = $this->get_columns();
+ $num = $this->get_option( 'layout_columns', 'max' );
+
+ ?>
+
+
+
+
+ />
+
+
+
+
+ get_option( 'per_page' ) ) {
+ return;
+ }
+
+ $per_page_label = $this->get_option( 'per_page', 'label' );
+ if ( null === $per_page_label ) {
+ $per_page_label = __( 'Number of items per page:' );
+ }
+
+ $option = $this->get_option( 'per_page', 'option' );
+ if ( ! $option ) {
+ $option = str_replace( '-', '_', "{$this->id}_per_page" );
+ }
+
+ $per_page = (int) get_user_option( $option );
+ if ( empty( $per_page ) || $per_page < 1 ) {
+ $per_page = $this->get_option( 'per_page', 'default' );
+ if ( ! $per_page ) {
+ $per_page = 20;
+ }
+ }
+
+ if ( 'edit_comments_per_page' === $option ) {
+ $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
+
+ /** This filter is documented in wp-admin/includes/class-wp-comments-list-table.php */
+ $per_page = apply_filters( 'comments_per_page', $per_page, $comment_status );
+ } elseif ( 'categories_per_page' === $option ) {
+ /** This filter is documented in wp-admin/includes/class-wp-terms-list-table.php */
+ $per_page = apply_filters( 'edit_categories_per_page', $per_page );
+ } else {
+ /** This filter is documented in wp-admin/includes/class-wp-list-table.php */
+ $per_page = apply_filters( "{$option}", $per_page );
+ }
+
+ // Back compat.
+ if ( isset( $this->post_type ) ) {
+ /** This filter is documented in wp-admin/includes/post.php */
+ $per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type );
+ }
+
+ // This needs a submit button.
+ add_filter( 'screen_options_show_submit', '__return_true' );
+
+ ?>
+
+
+
+
+
+
+
+
+ base && 'edit-comments' !== $screen->base ) {
+ return;
+ }
+
+ $view_mode_post_types = get_post_types( array( 'show_ui' => true ) );
+
+ /**
+ * Filters the post types that have different view mode options.
+ *
+ * @since 4.4.0
+ *
+ * @param string[] $view_mode_post_types Array of post types that can change view modes.
+ * Default post types with show_ui on.
+ */
+ $view_mode_post_types = apply_filters( 'view_mode_post_types', $view_mode_post_types );
+
+ if ( 'edit' === $screen->base && ! in_array( $this->post_type, $view_mode_post_types, true ) ) {
+ return;
+ }
+
+ if ( ! isset( $mode ) ) {
+ $mode = get_user_setting( 'posts_list_mode', 'list' );
+ }
+
+ // This needs a submit button.
+ add_filter( 'screen_options_show_submit', '__return_true' );
+ ?>
+
+
+
+ />
+
+
+
+ />
+
+
+
+ _screen_reader_content[ $key ] ) ) {
+ return;
+ }
+ echo "<$tag class='screen-reader-text'>" . $this->_screen_reader_content[ $key ] . "$tag>";
+ }
+}
diff --git a/wp-admin/includes/class-wp-site-health-auto-updates.php b/wp-admin/includes/class-wp-site-health-auto-updates.php
new file mode 100644
index 0000000..85decaa
--- /dev/null
+++ b/wp-admin/includes/class-wp-site-health-auto-updates.php
@@ -0,0 +1,458 @@
+test_constants( 'WP_AUTO_UPDATE_CORE', array( true, 'beta', 'rc', 'development', 'branch-development', 'minor' ) ),
+ $this->test_wp_version_check_attached(),
+ $this->test_filters_automatic_updater_disabled(),
+ $this->test_wp_automatic_updates_disabled(),
+ $this->test_if_failed_update(),
+ $this->test_vcs_abspath(),
+ $this->test_check_wp_filesystem_method(),
+ $this->test_all_files_writable(),
+ $this->test_accepts_dev_updates(),
+ $this->test_accepts_minor_updates(),
+ );
+
+ $tests = array_filter( $tests );
+ $tests = array_map(
+ static function ( $test ) {
+ $test = (object) $test;
+
+ if ( empty( $test->severity ) ) {
+ $test->severity = 'warning';
+ }
+
+ return $test;
+ },
+ $tests
+ );
+
+ return $tests;
+ }
+
+ /**
+ * Tests if auto-updates related constants are set correctly.
+ *
+ * @since 5.2.0
+ * @since 5.5.1 The `$value` parameter can accept an array.
+ *
+ * @param string $constant The name of the constant to check.
+ * @param bool|string|array $value The value that the constant should be, if set,
+ * or an array of acceptable values.
+ * @return array The test results.
+ */
+ public function test_constants( $constant, $value ) {
+ $acceptable_values = (array) $value;
+
+ if ( defined( $constant ) && ! in_array( constant( $constant ), $acceptable_values, true ) ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: 1: Name of the constant used. 2: Value of the constant used. */
+ __( 'The %1$s constant is defined as %2$s' ),
+ "
$constant
",
+ '
' . esc_html( var_export( constant( $constant ), true ) ) . '
'
+ ),
+ 'severity' => 'fail',
+ );
+ }
+ }
+
+ /**
+ * Checks if updates are intercepted by a filter.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function test_wp_version_check_attached() {
+ if ( ( ! is_multisite() || is_main_site() && is_network_admin() )
+ && ! has_filter( 'wp_version_check', 'wp_version_check' )
+ ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: %s: Name of the filter used. */
+ __( 'A plugin has prevented updates by disabling %s.' ),
+ '
wp_version_check()
'
+ ),
+ 'severity' => 'fail',
+ );
+ }
+ }
+
+ /**
+ * Checks if automatic updates are disabled by a filter.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function test_filters_automatic_updater_disabled() {
+ /** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
+ if ( apply_filters( 'automatic_updater_disabled', false ) ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: %s: Name of the filter used. */
+ __( 'The %s filter is enabled.' ),
+ '
automatic_updater_disabled
'
+ ),
+ 'severity' => 'fail',
+ );
+ }
+ }
+
+ /**
+ * Checks if automatic updates are disabled.
+ *
+ * @since 5.3.0
+ *
+ * @return array|false The test results. False if auto-updates are enabled.
+ */
+ public function test_wp_automatic_updates_disabled() {
+ if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
+ }
+
+ $auto_updates = new WP_Automatic_Updater();
+
+ if ( ! $auto_updates->is_disabled() ) {
+ return false;
+ }
+
+ return array(
+ 'description' => __( 'All automatic updates are disabled.' ),
+ 'severity' => 'fail',
+ );
+ }
+
+ /**
+ * Checks if automatic updates have tried to run, but failed, previously.
+ *
+ * @since 5.2.0
+ *
+ * @return array|false The test results. False if the auto-updates failed.
+ */
+ public function test_if_failed_update() {
+ $failed = get_site_option( 'auto_core_update_failed' );
+
+ if ( ! $failed ) {
+ return false;
+ }
+
+ if ( ! empty( $failed['critical'] ) ) {
+ $description = __( 'A previous automatic background update ended with a critical failure, so updates are now disabled.' );
+ $description .= ' ' . __( 'You would have received an email because of this.' );
+ $description .= ' ' . __( "When you've been able to update using the \"Update now\" button on Dashboard > Updates, this error will be cleared for future update attempts." );
+ $description .= ' ' . sprintf(
+ /* translators: %s: Code of error shown. */
+ __( 'The error code was %s.' ),
+ '
' . $failed['error_code'] . '
'
+ );
+ return array(
+ 'description' => $description,
+ 'severity' => 'warning',
+ );
+ }
+
+ $description = __( 'A previous automatic background update could not occur.' );
+ if ( empty( $failed['retry'] ) ) {
+ $description .= ' ' . __( 'You would have received an email because of this.' );
+ }
+
+ $description .= ' ' . __( 'Another attempt will be made with the next release.' );
+ $description .= ' ' . sprintf(
+ /* translators: %s: Code of error shown. */
+ __( 'The error code was %s.' ),
+ '
' . $failed['error_code'] . '
'
+ );
+ return array(
+ 'description' => $description,
+ 'severity' => 'warning',
+ );
+ }
+
+ /**
+ * Checks if WordPress is controlled by a VCS (Git, Subversion etc).
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function test_vcs_abspath() {
+ $context_dirs = array( ABSPATH );
+ $vcs_dirs = array( '.svn', '.git', '.hg', '.bzr' );
+ $check_dirs = array();
+
+ foreach ( $context_dirs as $context_dir ) {
+ // Walk up from $context_dir to the root.
+ do {
+ $check_dirs[] = $context_dir;
+
+ // Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
+ if ( dirname( $context_dir ) === $context_dir ) {
+ break;
+ }
+
+ // Continue one level at a time.
+ } while ( $context_dir = dirname( $context_dir ) );
+ }
+
+ $check_dirs = array_unique( $check_dirs );
+
+ // Search all directories we've found for evidence of version control.
+ foreach ( $vcs_dirs as $vcs_dir ) {
+ foreach ( $check_dirs as $check_dir ) {
+ // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition,Squiz.PHP.DisallowMultipleAssignments
+ if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) ) {
+ break 2;
+ }
+ }
+ }
+
+ /** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
+ if ( $checkout && ! apply_filters( 'automatic_updates_is_vcs_checkout', true, ABSPATH ) ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: 1: Folder name. 2: Version control directory. 3: Filter name. */
+ __( 'The folder %1$s was detected as being under version control (%2$s), but the %3$s filter is allowing updates.' ),
+ '
' . $check_dir . '
',
+ "
$vcs_dir
",
+ '
automatic_updates_is_vcs_checkout
'
+ ),
+ 'severity' => 'info',
+ );
+ }
+
+ if ( $checkout ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: 1: Folder name. 2: Version control directory. */
+ __( 'The folder %1$s was detected as being under version control (%2$s).' ),
+ '
' . $check_dir . '
',
+ "
$vcs_dir
"
+ ),
+ 'severity' => 'warning',
+ );
+ }
+
+ return array(
+ 'description' => __( 'No version control systems were detected.' ),
+ 'severity' => 'pass',
+ );
+ }
+
+ /**
+ * Checks if we can access files without providing credentials.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function test_check_wp_filesystem_method() {
+ // Make sure the `request_filesystem_credentials()` function is available during our REST API call.
+ if ( ! function_exists( 'request_filesystem_credentials' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/file.php';
+ }
+
+ $skin = new Automatic_Upgrader_Skin();
+ $success = $skin->request_filesystem_credentials( false, ABSPATH );
+
+ if ( ! $success ) {
+ $description = __( 'Your installation of WordPress prompts for FTP credentials to perform updates.' );
+ $description .= ' ' . __( '(Your site is performing updates over FTP due to file ownership. Talk to your hosting company.)' );
+
+ return array(
+ 'description' => $description,
+ 'severity' => 'fail',
+ );
+ }
+
+ return array(
+ 'description' => __( 'Your installation of WordPress does not require FTP credentials to perform updates.' ),
+ 'severity' => 'pass',
+ );
+ }
+
+ /**
+ * Checks if core files are writable by the web user/group.
+ *
+ * @since 5.2.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @return array|false The test results. False if they're not writeable.
+ */
+ public function test_all_files_writable() {
+ global $wp_filesystem;
+
+ require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
+
+ $skin = new Automatic_Upgrader_Skin();
+ $success = $skin->request_filesystem_credentials( false, ABSPATH );
+
+ if ( ! $success ) {
+ return false;
+ }
+
+ WP_Filesystem();
+
+ if ( 'direct' !== $wp_filesystem->method ) {
+ return false;
+ }
+
+ // Make sure the `get_core_checksums()` function is available during our REST API call.
+ if ( ! function_exists( 'get_core_checksums' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/update.php';
+ }
+
+ $checksums = get_core_checksums( $wp_version, 'en_US' );
+ $dev = ( str_contains( $wp_version, '-' ) );
+ // Get the last stable version's files and test against that.
+ if ( ! $checksums && $dev ) {
+ $checksums = get_core_checksums( (float) $wp_version - 0.1, 'en_US' );
+ }
+
+ // There aren't always checksums for development releases, so just skip the test if we still can't find any.
+ if ( ! $checksums && $dev ) {
+ return false;
+ }
+
+ if ( ! $checksums ) {
+ $description = sprintf(
+ /* translators: %s: WordPress version. */
+ __( "Couldn't retrieve a list of the checksums for WordPress %s." ),
+ $wp_version
+ );
+ $description .= ' ' . __( 'This could mean that connections are failing to WordPress.org.' );
+ return array(
+ 'description' => $description,
+ 'severity' => 'warning',
+ );
+ }
+
+ $unwritable_files = array();
+ foreach ( array_keys( $checksums ) as $file ) {
+ if ( str_starts_with( $file, 'wp-content' ) ) {
+ continue;
+ }
+ if ( ! file_exists( ABSPATH . $file ) ) {
+ continue;
+ }
+ if ( ! is_writable( ABSPATH . $file ) ) {
+ $unwritable_files[] = $file;
+ }
+ }
+
+ if ( $unwritable_files ) {
+ if ( count( $unwritable_files ) > 20 ) {
+ $unwritable_files = array_slice( $unwritable_files, 0, 20 );
+ $unwritable_files[] = '...';
+ }
+ return array(
+ 'description' => __( 'Some files are not writable by WordPress:' ) . '
' . implode( ' ', $unwritable_files ) . ' ',
+ 'severity' => 'fail',
+ );
+ } else {
+ return array(
+ 'description' => __( 'All of your WordPress files are writable.' ),
+ 'severity' => 'pass',
+ );
+ }
+ }
+
+ /**
+ * Checks if the install is using a development branch and can use nightly packages.
+ *
+ * @since 5.2.0
+ *
+ * @return array|false The test results. False if it isn't a development version.
+ */
+ public function test_accepts_dev_updates() {
+ require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
+ // Only for dev versions.
+ if ( ! str_contains( $wp_version, '-' ) ) {
+ return false;
+ }
+
+ if ( defined( 'WP_AUTO_UPDATE_CORE' ) && ( 'minor' === WP_AUTO_UPDATE_CORE || false === WP_AUTO_UPDATE_CORE ) ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: %s: Name of the constant used. */
+ __( 'WordPress development updates are blocked by the %s constant.' ),
+ '
WP_AUTO_UPDATE_CORE
'
+ ),
+ 'severity' => 'fail',
+ );
+ }
+
+ /** This filter is documented in wp-admin/includes/class-core-upgrader.php */
+ if ( ! apply_filters( 'allow_dev_auto_core_updates', $wp_version ) ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: %s: Name of the filter used. */
+ __( 'WordPress development updates are blocked by the %s filter.' ),
+ '
allow_dev_auto_core_updates
'
+ ),
+ 'severity' => 'fail',
+ );
+ }
+ }
+
+ /**
+ * Checks if the site supports automatic minor updates.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function test_accepts_minor_updates() {
+ if ( defined( 'WP_AUTO_UPDATE_CORE' ) && false === WP_AUTO_UPDATE_CORE ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: %s: Name of the constant used. */
+ __( 'WordPress security and maintenance releases are blocked by %s.' ),
+ "
define( 'WP_AUTO_UPDATE_CORE', false );
"
+ ),
+ 'severity' => 'fail',
+ );
+ }
+
+ /** This filter is documented in wp-admin/includes/class-core-upgrader.php */
+ if ( ! apply_filters( 'allow_minor_auto_core_updates', true ) ) {
+ return array(
+ 'description' => sprintf(
+ /* translators: %s: Name of the filter used. */
+ __( 'WordPress security and maintenance releases are blocked by the %s filter.' ),
+ '
allow_minor_auto_core_updates
'
+ ),
+ 'severity' => 'fail',
+ );
+ }
+ }
+}
diff --git a/wp-admin/includes/class-wp-site-health.php b/wp-admin/includes/class-wp-site-health.php
new file mode 100644
index 0000000..b73e1e7
--- /dev/null
+++ b/wp-admin/includes/class-wp-site-health.php
@@ -0,0 +1,3635 @@
+maybe_create_scheduled_event();
+
+ // Save memory limit before it's affected by wp_raise_memory_limit( 'admin' ).
+ $this->php_memory_limit = ini_get( 'memory_limit' );
+
+ $this->timeout_late_cron = 0;
+ $this->timeout_missed_cron = - 5 * MINUTE_IN_SECONDS;
+
+ if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
+ $this->timeout_late_cron = - 15 * MINUTE_IN_SECONDS;
+ $this->timeout_missed_cron = - 1 * HOUR_IN_SECONDS;
+ }
+
+ add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
+
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
+ add_action( 'wp_site_health_scheduled_check', array( $this, 'wp_cron_scheduled_check' ) );
+
+ add_action( 'site_health_tab_content', array( $this, 'show_site_health_tab' ) );
+ }
+
+ /**
+ * Outputs the content of a tab in the Site Health screen.
+ *
+ * @since 5.8.0
+ *
+ * @param string $tab Slug of the current tab being displayed.
+ */
+ public function show_site_health_tab( $tab ) {
+ if ( 'debug' === $tab ) {
+ require_once ABSPATH . 'wp-admin/site-health-info.php';
+ }
+ }
+
+ /**
+ * Returns an instance of the WP_Site_Health class, or create one if none exist yet.
+ *
+ * @since 5.4.0
+ *
+ * @return WP_Site_Health|null
+ */
+ public static function get_instance() {
+ if ( null === self::$instance ) {
+ self::$instance = new WP_Site_Health();
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Enqueues the site health scripts.
+ *
+ * @since 5.2.0
+ */
+ public function enqueue_scripts() {
+ $screen = get_current_screen();
+ if ( 'site-health' !== $screen->id && 'dashboard' !== $screen->id ) {
+ return;
+ }
+
+ $health_check_js_variables = array(
+ 'screen' => $screen->id,
+ 'nonce' => array(
+ 'site_status' => wp_create_nonce( 'health-check-site-status' ),
+ 'site_status_result' => wp_create_nonce( 'health-check-site-status-result' ),
+ ),
+ 'site_status' => array(
+ 'direct' => array(),
+ 'async' => array(),
+ 'issues' => array(
+ 'good' => 0,
+ 'recommended' => 0,
+ 'critical' => 0,
+ ),
+ ),
+ );
+
+ $issue_counts = get_transient( 'health-check-site-status-result' );
+
+ if ( false !== $issue_counts ) {
+ $issue_counts = json_decode( $issue_counts );
+
+ $health_check_js_variables['site_status']['issues'] = $issue_counts;
+ }
+
+ if ( 'site-health' === $screen->id && ( ! isset( $_GET['tab'] ) || empty( $_GET['tab'] ) ) ) {
+ $tests = WP_Site_Health::get_tests();
+
+ // Don't run https test on development environments.
+ if ( $this->is_development_environment() ) {
+ unset( $tests['async']['https_status'] );
+ }
+
+ foreach ( $tests['direct'] as $test ) {
+ if ( is_string( $test['test'] ) ) {
+ $test_function = sprintf(
+ 'get_test_%s',
+ $test['test']
+ );
+
+ if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
+ $health_check_js_variables['site_status']['direct'][] = $this->perform_test( array( $this, $test_function ) );
+ continue;
+ }
+ }
+
+ if ( is_callable( $test['test'] ) ) {
+ $health_check_js_variables['site_status']['direct'][] = $this->perform_test( $test['test'] );
+ }
+ }
+
+ foreach ( $tests['async'] as $test ) {
+ if ( is_string( $test['test'] ) ) {
+ $health_check_js_variables['site_status']['async'][] = array(
+ 'test' => $test['test'],
+ 'has_rest' => ( isset( $test['has_rest'] ) ? $test['has_rest'] : false ),
+ 'completed' => false,
+ 'headers' => isset( $test['headers'] ) ? $test['headers'] : array(),
+ );
+ }
+ }
+ }
+
+ wp_localize_script( 'site-health', 'SiteHealth', $health_check_js_variables );
+ }
+
+ /**
+ * Runs a Site Health test directly.
+ *
+ * @since 5.4.0
+ *
+ * @param callable $callback
+ * @return mixed|void
+ */
+ private function perform_test( $callback ) {
+ /**
+ * Filters the output of a finished Site Health test.
+ *
+ * @since 5.3.0
+ *
+ * @param array $test_result {
+ * An associative array of test result data.
+ *
+ * @type string $label A label describing the test, and is used as a header in the output.
+ * @type string $status The status of the test, which can be a value of `good`, `recommended` or `critical`.
+ * @type array $badge {
+ * Tests are put into categories which have an associated badge shown, these can be modified and assigned here.
+ *
+ * @type string $label The test label, for example `Performance`.
+ * @type string $color Default `blue`. A string representing a color to use for the label.
+ * }
+ * @type string $description A more descriptive explanation of what the test looks for, and why it is important for the end user.
+ * @type string $actions An action to direct the user to where they can resolve the issue, if one exists.
+ * @type string $test The name of the test being ran, used as a reference point.
+ * }
+ */
+ return apply_filters( 'site_status_test_result', call_user_func( $callback ) );
+ }
+
+ /**
+ * Runs the SQL version checks.
+ *
+ * These values are used in later tests, but the part of preparing them is more easily managed
+ * early in the class for ease of access and discovery.
+ *
+ * @since 5.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ */
+ private function prepare_sql_data() {
+ global $wpdb;
+
+ $mysql_server_type = $wpdb->db_server_info();
+
+ $this->mysql_server_version = $wpdb->get_var( 'SELECT VERSION()' );
+
+ if ( stristr( $mysql_server_type, 'mariadb' ) ) {
+ $this->is_mariadb = true;
+ $this->mysql_recommended_version = $this->mariadb_recommended_version;
+ }
+
+ $this->is_acceptable_mysql_version = version_compare( $this->mysql_required_version, $this->mysql_server_version, '<=' );
+ $this->is_recommended_mysql_version = version_compare( $this->mysql_recommended_version, $this->mysql_server_version, '<=' );
+ }
+
+ /**
+ * Tests whether `wp_version_check` is blocked.
+ *
+ * It's possible to block updates with the `wp_version_check` filter, but this can't be checked
+ * during an Ajax call, as the filter is never introduced then.
+ *
+ * This filter overrides a standard page request if it's made by an admin through the Ajax call
+ * with the right query argument to check for this.
+ *
+ * @since 5.2.0
+ */
+ public function check_wp_version_check_exists() {
+ if ( ! is_admin() || ! is_user_logged_in() || ! current_user_can( 'update_core' ) || ! isset( $_GET['health-check-test-wp_version_check'] ) ) {
+ return;
+ }
+
+ echo ( has_filter( 'wp_version_check', 'wp_version_check' ) ? 'yes' : 'no' );
+
+ die();
+ }
+
+ /**
+ * Tests for WordPress version and outputs it.
+ *
+ * Gives various results depending on what kind of updates are available, if any, to encourage
+ * the user to install security updates as a priority.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test result.
+ */
+ public function get_test_wordpress_version() {
+ $result = array(
+ 'label' => '',
+ 'status' => '',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => '',
+ 'actions' => '',
+ 'test' => 'wordpress_version',
+ );
+
+ $core_current_version = get_bloginfo( 'version' );
+ $core_updates = get_core_updates();
+
+ if ( ! is_array( $core_updates ) ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = sprintf(
+ /* translators: %s: Your current version of WordPress. */
+ __( 'WordPress version %s' ),
+ $core_current_version
+ );
+
+ $result['description'] = sprintf(
+ '
%s
',
+ __( 'Unable to check if any new versions of WordPress are available.' )
+ );
+
+ $result['actions'] = sprintf(
+ '
%s ',
+ esc_url( admin_url( 'update-core.php?force-check=1' ) ),
+ __( 'Check for updates manually' )
+ );
+ } else {
+ foreach ( $core_updates as $core => $update ) {
+ if ( 'upgrade' === $update->response ) {
+ $current_version = explode( '.', $core_current_version );
+ $new_version = explode( '.', $update->version );
+
+ $current_major = $current_version[0] . '.' . $current_version[1];
+ $new_major = $new_version[0] . '.' . $new_version[1];
+
+ $result['label'] = sprintf(
+ /* translators: %s: The latest version of WordPress available. */
+ __( 'WordPress update available (%s)' ),
+ $update->version
+ );
+
+ $result['actions'] = sprintf(
+ '
%s ',
+ esc_url( admin_url( 'update-core.php' ) ),
+ __( 'Install the latest version of WordPress' )
+ );
+
+ if ( $current_major !== $new_major ) {
+ // This is a major version mismatch.
+ $result['status'] = 'recommended';
+ $result['description'] = sprintf(
+ '
%s
',
+ __( 'A new version of WordPress is available.' )
+ );
+ } else {
+ // This is a minor version, sometimes considered more critical.
+ $result['status'] = 'critical';
+ $result['badge']['label'] = __( 'Security' );
+ $result['description'] = sprintf(
+ '
%s
',
+ __( 'A new minor update is available for your site. Because minor updates often address security, it’s important to install them.' )
+ );
+ }
+ } else {
+ $result['status'] = 'good';
+ $result['label'] = sprintf(
+ /* translators: %s: The current version of WordPress installed on this site. */
+ __( 'Your version of WordPress (%s) is up to date' ),
+ $core_current_version
+ );
+
+ $result['description'] = sprintf(
+ '
%s
',
+ __( 'You are currently running the latest version of WordPress available, keep it up!' )
+ );
+ }
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if plugins are outdated, or unnecessary.
+ *
+ * The test checks if your plugins are up to date, and encourages you to remove any
+ * that are not in use.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test result.
+ */
+ public function get_test_plugin_version() {
+ $result = array(
+ 'label' => __( 'Your plugins are all up to date' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Plugins extend your site’s functionality with things like contact forms, ecommerce and much more. That means they have deep access to your site, so it’s vital to keep them up to date.' )
+ ),
+ 'actions' => sprintf(
+ '
%s
',
+ esc_url( admin_url( 'plugins.php' ) ),
+ __( 'Manage your plugins' )
+ ),
+ 'test' => 'plugin_version',
+ );
+
+ $plugins = get_plugins();
+ $plugin_updates = get_plugin_updates();
+
+ $plugins_active = 0;
+ $plugins_total = 0;
+ $plugins_need_update = 0;
+
+ // Loop over the available plugins and check their versions and active state.
+ foreach ( $plugins as $plugin_path => $plugin ) {
+ ++$plugins_total;
+
+ if ( is_plugin_active( $plugin_path ) ) {
+ ++$plugins_active;
+ }
+
+ if ( array_key_exists( $plugin_path, $plugin_updates ) ) {
+ ++$plugins_need_update;
+ }
+ }
+
+ // Add a notice if there are outdated plugins.
+ if ( $plugins_need_update > 0 ) {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'You have plugins waiting to be updated' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %d: The number of outdated plugins. */
+ _n(
+ 'Your site has %d plugin waiting to be updated.',
+ 'Your site has %d plugins waiting to be updated.',
+ $plugins_need_update
+ ),
+ $plugins_need_update
+ )
+ );
+
+ $result['actions'] .= sprintf(
+ '
%s
',
+ esc_url( network_admin_url( 'plugins.php?plugin_status=upgrade' ) ),
+ __( 'Update your plugins' )
+ );
+ } else {
+ if ( 1 === $plugins_active ) {
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Your site has 1 active plugin, and it is up to date.' )
+ );
+ } elseif ( $plugins_active > 0 ) {
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %d: The number of active plugins. */
+ _n(
+ 'Your site has %d active plugin, and it is up to date.',
+ 'Your site has %d active plugins, and they are all up to date.',
+ $plugins_active
+ ),
+ $plugins_active
+ )
+ );
+ } else {
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Your site does not have any active plugins.' )
+ );
+ }
+ }
+
+ // Check if there are inactive plugins.
+ if ( $plugins_total > $plugins_active && ! is_multisite() ) {
+ $unused_plugins = $plugins_total - $plugins_active;
+
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'You should remove inactive plugins' );
+
+ $result['description'] .= sprintf(
+ '
%s %s
',
+ sprintf(
+ /* translators: %d: The number of inactive plugins. */
+ _n(
+ 'Your site has %d inactive plugin.',
+ 'Your site has %d inactive plugins.',
+ $unused_plugins
+ ),
+ $unused_plugins
+ ),
+ __( 'Inactive plugins are tempting targets for attackers. If you are not going to use a plugin, you should consider removing it.' )
+ );
+
+ $result['actions'] .= sprintf(
+ '
%s
',
+ esc_url( admin_url( 'plugins.php?plugin_status=inactive' ) ),
+ __( 'Manage inactive plugins' )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if themes are outdated, or unnecessary.
+ *
+ * Checks if your site has a default theme (to fall back on if there is a need),
+ * if your themes are up to date and, finally, encourages you to remove any themes
+ * that are not needed.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_theme_version() {
+ $result = array(
+ 'label' => __( 'Your themes are all up to date' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Themes add your site’s look and feel. It’s important to keep them up to date, to stay consistent with your brand and keep your site secure.' )
+ ),
+ 'actions' => sprintf(
+ '
%s
',
+ esc_url( admin_url( 'themes.php' ) ),
+ __( 'Manage your themes' )
+ ),
+ 'test' => 'theme_version',
+ );
+
+ $theme_updates = get_theme_updates();
+
+ $themes_total = 0;
+ $themes_need_updates = 0;
+ $themes_inactive = 0;
+
+ // This value is changed during processing to determine how many themes are considered a reasonable amount.
+ $allowed_theme_count = 1;
+
+ $has_default_theme = false;
+ $has_unused_themes = false;
+ $show_unused_themes = true;
+ $using_default_theme = false;
+
+ // Populate a list of all themes available in the install.
+ $all_themes = wp_get_themes();
+ $active_theme = wp_get_theme();
+
+ // If WP_DEFAULT_THEME doesn't exist, fall back to the latest core default theme.
+ $default_theme = wp_get_theme( WP_DEFAULT_THEME );
+ if ( ! $default_theme->exists() ) {
+ $default_theme = WP_Theme::get_core_default_theme();
+ }
+
+ if ( $default_theme ) {
+ $has_default_theme = true;
+
+ if (
+ $active_theme->get_stylesheet() === $default_theme->get_stylesheet()
+ ||
+ is_child_theme() && $active_theme->get_template() === $default_theme->get_template()
+ ) {
+ $using_default_theme = true;
+ }
+ }
+
+ foreach ( $all_themes as $theme_slug => $theme ) {
+ ++$themes_total;
+
+ if ( array_key_exists( $theme_slug, $theme_updates ) ) {
+ ++$themes_need_updates;
+ }
+ }
+
+ // If this is a child theme, increase the allowed theme count by one, to account for the parent.
+ if ( is_child_theme() ) {
+ ++$allowed_theme_count;
+ }
+
+ // If there's a default theme installed and not in use, we count that as allowed as well.
+ if ( $has_default_theme && ! $using_default_theme ) {
+ ++$allowed_theme_count;
+ }
+
+ if ( $themes_total > $allowed_theme_count ) {
+ $has_unused_themes = true;
+ $themes_inactive = ( $themes_total - $allowed_theme_count );
+ }
+
+ // Check if any themes need to be updated.
+ if ( $themes_need_updates > 0 ) {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'You have themes waiting to be updated' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %d: The number of outdated themes. */
+ _n(
+ 'Your site has %d theme waiting to be updated.',
+ 'Your site has %d themes waiting to be updated.',
+ $themes_need_updates
+ ),
+ $themes_need_updates
+ )
+ );
+ } else {
+ // Give positive feedback about the site being good about keeping things up to date.
+ if ( 1 === $themes_total ) {
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Your site has 1 installed theme, and it is up to date.' )
+ );
+ } elseif ( $themes_total > 0 ) {
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %d: The number of themes. */
+ _n(
+ 'Your site has %d installed theme, and it is up to date.',
+ 'Your site has %d installed themes, and they are all up to date.',
+ $themes_total
+ ),
+ $themes_total
+ )
+ );
+ } else {
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Your site does not have any installed themes.' )
+ );
+ }
+ }
+
+ if ( $has_unused_themes && $show_unused_themes && ! is_multisite() ) {
+
+ // This is a child theme, so we want to be a bit more explicit in our messages.
+ if ( $active_theme->parent() ) {
+ // Recommend removing inactive themes, except a default theme, your current one, and the parent theme.
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'You should remove inactive themes' );
+
+ if ( $using_default_theme ) {
+ $result['description'] .= sprintf(
+ '
%s %s
',
+ sprintf(
+ /* translators: %d: The number of inactive themes. */
+ _n(
+ 'Your site has %d inactive theme.',
+ 'Your site has %d inactive themes.',
+ $themes_inactive
+ ),
+ $themes_inactive
+ ),
+ sprintf(
+ /* translators: 1: The currently active theme. 2: The active theme's parent theme. */
+ __( 'To enhance your site’s security, you should consider removing any themes you are not using. You should keep your active theme, %1$s, and %2$s, its parent theme.' ),
+ $active_theme->name,
+ $active_theme->parent()->name
+ )
+ );
+ } else {
+ $result['description'] .= sprintf(
+ '
%s %s
',
+ sprintf(
+ /* translators: %d: The number of inactive themes. */
+ _n(
+ 'Your site has %d inactive theme.',
+ 'Your site has %d inactive themes.',
+ $themes_inactive
+ ),
+ $themes_inactive
+ ),
+ sprintf(
+ /* translators: 1: The default theme for WordPress. 2: The currently active theme. 3: The active theme's parent theme. */
+ __( 'To enhance your site’s security, you should consider removing any themes you are not using. You should keep %1$s, the default WordPress theme, %2$s, your active theme, and %3$s, its parent theme.' ),
+ $default_theme ? $default_theme->name : WP_DEFAULT_THEME,
+ $active_theme->name,
+ $active_theme->parent()->name
+ )
+ );
+ }
+ } else {
+ // Recommend removing all inactive themes.
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'You should remove inactive themes' );
+
+ if ( $using_default_theme ) {
+ $result['description'] .= sprintf(
+ '
%s %s
',
+ sprintf(
+ /* translators: 1: The amount of inactive themes. 2: The currently active theme. */
+ _n(
+ 'Your site has %1$d inactive theme, other than %2$s, your active theme.',
+ 'Your site has %1$d inactive themes, other than %2$s, your active theme.',
+ $themes_inactive
+ ),
+ $themes_inactive,
+ $active_theme->name
+ ),
+ __( 'You should consider removing any unused themes to enhance your site’s security.' )
+ );
+ } else {
+ $result['description'] .= sprintf(
+ '
%s %s
',
+ sprintf(
+ /* translators: 1: The amount of inactive themes. 2: The default theme for WordPress. 3: The currently active theme. */
+ _n(
+ 'Your site has %1$d inactive theme, other than %2$s, the default WordPress theme, and %3$s, your active theme.',
+ 'Your site has %1$d inactive themes, other than %2$s, the default WordPress theme, and %3$s, your active theme.',
+ $themes_inactive
+ ),
+ $themes_inactive,
+ $default_theme ? $default_theme->name : WP_DEFAULT_THEME,
+ $active_theme->name
+ ),
+ __( 'You should consider removing any unused themes to enhance your site’s security.' )
+ );
+ }
+ }
+ }
+
+ // If no default Twenty* theme exists.
+ if ( ! $has_default_theme ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'Have a default theme available' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Your site does not have any default theme. Default themes are used by WordPress automatically if anything is wrong with your chosen theme.' )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if the supplied PHP version is supported.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_php_version() {
+ $response = wp_check_php_version();
+
+ $result = array(
+ 'label' => sprintf(
+ /* translators: %s: The current PHP version. */
+ __( 'Your site is running the current version of PHP (%s)' ),
+ PHP_VERSION
+ ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: The minimum recommended PHP version. */
+ __( 'PHP is one of the programming languages used to build WordPress. Newer versions of PHP receive regular security updates and may increase your site’s performance. The minimum recommended version of PHP is %s.' ),
+ $response ? $response['recommended_version'] : ''
+ )
+ ),
+ 'actions' => sprintf(
+ '
%s %s
',
+ esc_url( wp_get_update_php_url() ),
+ __( 'Learn more about updating PHP' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ ),
+ 'test' => 'php_version',
+ );
+
+ // PHP is up to date.
+ if ( ! $response || version_compare( PHP_VERSION, $response['recommended_version'], '>=' ) ) {
+ return $result;
+ }
+
+ // The PHP version is older than the recommended version, but still receiving active support.
+ if ( $response['is_supported'] ) {
+ $result['label'] = sprintf(
+ /* translators: %s: The server PHP version. */
+ __( 'Your site is running on an older version of PHP (%s)' ),
+ PHP_VERSION
+ );
+ $result['status'] = 'recommended';
+
+ return $result;
+ }
+
+ /*
+ * The PHP version is still receiving security fixes, but is lower than
+ * the expected minimum version that will be required by WordPress in the near future.
+ */
+ if ( $response['is_secure'] && $response['is_lower_than_future_minimum'] ) {
+ // The `is_secure` array key name doesn't actually imply this is a secure version of PHP. It only means it receives security updates.
+
+ $result['label'] = sprintf(
+ /* translators: %s: The server PHP version. */
+ __( 'Your site is running on an outdated version of PHP (%s), which soon will not be supported by WordPress.' ),
+ PHP_VERSION
+ );
+
+ $result['status'] = 'critical';
+ $result['badge']['label'] = __( 'Requirements' );
+
+ return $result;
+ }
+
+ // The PHP version is only receiving security fixes.
+ if ( $response['is_secure'] ) {
+ $result['label'] = sprintf(
+ /* translators: %s: The server PHP version. */
+ __( 'Your site is running on an older version of PHP (%s), which should be updated' ),
+ PHP_VERSION
+ );
+ $result['status'] = 'recommended';
+
+ return $result;
+ }
+
+ // No more security updates for the PHP version, and lower than the expected minimum version required by WordPress.
+ if ( $response['is_lower_than_future_minimum'] ) {
+ $message = sprintf(
+ /* translators: %s: The server PHP version. */
+ __( 'Your site is running on an outdated version of PHP (%s), which does not receive security updates and soon will not be supported by WordPress.' ),
+ PHP_VERSION
+ );
+ } else {
+ // No more security updates for the PHP version, must be updated.
+ $message = sprintf(
+ /* translators: %s: The server PHP version. */
+ __( 'Your site is running on an outdated version of PHP (%s), which does not receive security updates. It should be updated.' ),
+ PHP_VERSION
+ );
+ }
+
+ $result['label'] = $message;
+ $result['status'] = 'critical';
+
+ $result['badge']['label'] = __( 'Security' );
+
+ return $result;
+ }
+
+ /**
+ * Checks if the passed extension or function are available.
+ *
+ * Make the check for available PHP modules into a simple boolean operator for a cleaner test runner.
+ *
+ * @since 5.2.0
+ * @since 5.3.0 The `$constant_name` and `$class_name` parameters were added.
+ *
+ * @param string $extension_name Optional. The extension name to test. Default null.
+ * @param string $function_name Optional. The function name to test. Default null.
+ * @param string $constant_name Optional. The constant name to test for. Default null.
+ * @param string $class_name Optional. The class name to test for. Default null.
+ * @return bool Whether or not the extension and function are available.
+ */
+ private function test_php_extension_availability( $extension_name = null, $function_name = null, $constant_name = null, $class_name = null ) {
+ // If no extension or function is passed, claim to fail testing, as we have nothing to test against.
+ if ( ! $extension_name && ! $function_name && ! $constant_name && ! $class_name ) {
+ return false;
+ }
+
+ if ( $extension_name && ! extension_loaded( $extension_name ) ) {
+ return false;
+ }
+
+ if ( $function_name && ! function_exists( $function_name ) ) {
+ return false;
+ }
+
+ if ( $constant_name && ! defined( $constant_name ) ) {
+ return false;
+ }
+
+ if ( $class_name && ! class_exists( $class_name ) ) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Tests if required PHP modules are installed on the host.
+ *
+ * This test builds on the recommendations made by the WordPress Hosting Team
+ * as seen at https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions
+ *
+ * @since 5.2.0
+ *
+ * @return array
+ */
+ public function get_test_php_extensions() {
+ $result = array(
+ 'label' => __( 'Required and recommended modules are installed' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
%s
',
+ __( 'PHP modules perform most of the tasks on the server that make your site run. Any changes to these must be made by your server administrator.' ),
+ sprintf(
+ /* translators: 1: Link to the hosting group page about recommended PHP modules. 2: Additional link attributes. 3: Accessibility text. */
+ __( 'The WordPress Hosting Team maintains a list of those modules, both recommended and required, in
the team handbook%3$s .' ),
+ /* translators: Localized team handbook, if one exists. */
+ esc_url( __( 'https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions' ) ),
+ 'target="_blank" rel="noopener"',
+ sprintf(
+ '
%s ',
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ )
+ )
+ ),
+ 'actions' => '',
+ 'test' => 'php_extensions',
+ );
+
+ $modules = array(
+ 'curl' => array(
+ 'function' => 'curl_version',
+ 'required' => false,
+ ),
+ 'dom' => array(
+ 'class' => 'DOMNode',
+ 'required' => false,
+ ),
+ 'exif' => array(
+ 'function' => 'exif_read_data',
+ 'required' => false,
+ ),
+ 'fileinfo' => array(
+ 'function' => 'finfo_file',
+ 'required' => false,
+ ),
+ 'hash' => array(
+ 'function' => 'hash',
+ 'required' => false,
+ ),
+ 'imagick' => array(
+ 'extension' => 'imagick',
+ 'required' => false,
+ ),
+ 'json' => array(
+ 'function' => 'json_last_error',
+ 'required' => true,
+ ),
+ 'mbstring' => array(
+ 'function' => 'mb_check_encoding',
+ 'required' => false,
+ ),
+ 'mysqli' => array(
+ 'function' => 'mysqli_connect',
+ 'required' => false,
+ ),
+ 'libsodium' => array(
+ 'constant' => 'SODIUM_LIBRARY_VERSION',
+ 'required' => false,
+ 'php_bundled_version' => '7.2.0',
+ ),
+ 'openssl' => array(
+ 'function' => 'openssl_encrypt',
+ 'required' => false,
+ ),
+ 'pcre' => array(
+ 'function' => 'preg_match',
+ 'required' => false,
+ ),
+ 'mod_xml' => array(
+ 'extension' => 'libxml',
+ 'required' => false,
+ ),
+ 'zip' => array(
+ 'class' => 'ZipArchive',
+ 'required' => false,
+ ),
+ 'filter' => array(
+ 'function' => 'filter_list',
+ 'required' => false,
+ ),
+ 'gd' => array(
+ 'extension' => 'gd',
+ 'required' => false,
+ 'fallback_for' => 'imagick',
+ ),
+ 'iconv' => array(
+ 'function' => 'iconv',
+ 'required' => false,
+ ),
+ 'intl' => array(
+ 'extension' => 'intl',
+ 'required' => false,
+ ),
+ 'mcrypt' => array(
+ 'extension' => 'mcrypt',
+ 'required' => false,
+ 'fallback_for' => 'libsodium',
+ ),
+ 'simplexml' => array(
+ 'extension' => 'simplexml',
+ 'required' => false,
+ 'fallback_for' => 'mod_xml',
+ ),
+ 'xmlreader' => array(
+ 'extension' => 'xmlreader',
+ 'required' => false,
+ 'fallback_for' => 'mod_xml',
+ ),
+ 'zlib' => array(
+ 'extension' => 'zlib',
+ 'required' => false,
+ 'fallback_for' => 'zip',
+ ),
+ );
+
+ /**
+ * Filters the array representing all the modules we wish to test for.
+ *
+ * @since 5.2.0
+ * @since 5.3.0 The `$constant` and `$class` parameters were added.
+ *
+ * @param array $modules {
+ * An associative array of modules to test for.
+ *
+ * @type array ...$0 {
+ * An associative array of module properties used during testing.
+ * One of either `$function` or `$extension` must be provided, or they will fail by default.
+ *
+ * @type string $function Optional. A function name to test for the existence of.
+ * @type string $extension Optional. An extension to check if is loaded in PHP.
+ * @type string $constant Optional. A constant name to check for to verify an extension exists.
+ * @type string $class Optional. A class name to check for to verify an extension exists.
+ * @type bool $required Is this a required feature or not.
+ * @type string $fallback_for Optional. The module this module replaces as a fallback.
+ * }
+ * }
+ */
+ $modules = apply_filters( 'site_status_test_php_modules', $modules );
+
+ $failures = array();
+
+ foreach ( $modules as $library => $module ) {
+ $extension_name = ( isset( $module['extension'] ) ? $module['extension'] : null );
+ $function_name = ( isset( $module['function'] ) ? $module['function'] : null );
+ $constant_name = ( isset( $module['constant'] ) ? $module['constant'] : null );
+ $class_name = ( isset( $module['class'] ) ? $module['class'] : null );
+
+ // If this module is a fallback for another function, check if that other function passed.
+ if ( isset( $module['fallback_for'] ) ) {
+ /*
+ * If that other function has a failure, mark this module as required for usual operations.
+ * If that other function hasn't failed, skip this test as it's only a fallback.
+ */
+ if ( isset( $failures[ $module['fallback_for'] ] ) ) {
+ $module['required'] = true;
+ } else {
+ continue;
+ }
+ }
+
+ if ( ! $this->test_php_extension_availability( $extension_name, $function_name, $constant_name, $class_name )
+ && ( ! isset( $module['php_bundled_version'] )
+ || version_compare( PHP_VERSION, $module['php_bundled_version'], '<' ) )
+ ) {
+ if ( $module['required'] ) {
+ $result['status'] = 'critical';
+
+ $class = 'error';
+ /* translators: Hidden accessibility text. */
+ $screen_reader = __( 'Error' );
+ $message = sprintf(
+ /* translators: %s: The module name. */
+ __( 'The required module, %s, is not installed, or has been disabled.' ),
+ $library
+ );
+ } else {
+ $class = 'warning';
+ /* translators: Hidden accessibility text. */
+ $screen_reader = __( 'Warning' );
+ $message = sprintf(
+ /* translators: %s: The module name. */
+ __( 'The optional module, %s, is not installed, or has been disabled.' ),
+ $library
+ );
+ }
+
+ if ( ! $module['required'] && 'good' === $result['status'] ) {
+ $result['status'] = 'recommended';
+ }
+
+ $failures[ $library ] = "
$screen_reader $message";
+ }
+ }
+
+ if ( ! empty( $failures ) ) {
+ $output = '
';
+
+ foreach ( $failures as $failure ) {
+ $output .= sprintf(
+ '%s ',
+ $failure
+ );
+ }
+
+ $output .= ' ';
+ }
+
+ if ( 'good' !== $result['status'] ) {
+ if ( 'recommended' === $result['status'] ) {
+ $result['label'] = __( 'One or more recommended modules are missing' );
+ }
+ if ( 'critical' === $result['status'] ) {
+ $result['label'] = __( 'One or more required modules are missing' );
+ }
+
+ $result['description'] .= $output;
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if the PHP default timezone is set to UTC.
+ *
+ * @since 5.3.1
+ *
+ * @return array The test results.
+ */
+ public function get_test_php_default_timezone() {
+ $result = array(
+ 'label' => __( 'PHP default timezone is valid' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'PHP default timezone was configured by WordPress on loading. This is necessary for correct calculations of dates and times.' )
+ ),
+ 'actions' => '',
+ 'test' => 'php_default_timezone',
+ );
+
+ if ( 'UTC' !== date_default_timezone_get() ) {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'PHP default timezone is invalid' );
+
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: date_default_timezone_set() */
+ __( 'PHP default timezone was changed after WordPress loading by a %s function call. This interferes with correct calculations of dates and times.' ),
+ '
date_default_timezone_set()
'
+ )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if there's an active PHP session that can affect loopback requests.
+ *
+ * @since 5.5.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_php_sessions() {
+ $result = array(
+ 'label' => __( 'No PHP sessions detected' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: session_start(), 2: session_write_close() */
+ __( 'PHP sessions created by a %1$s function call may interfere with REST API and loopback requests. An active session should be closed by %2$s before making any HTTP requests.' ),
+ '
session_start()
',
+ '
session_write_close()
'
+ )
+ ),
+ 'test' => 'php_sessions',
+ );
+
+ if ( function_exists( 'session_status' ) && PHP_SESSION_ACTIVE === session_status() ) {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'An active PHP session was detected' );
+
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: session_start(), 2: session_write_close() */
+ __( 'A PHP session was created by a %1$s function call. This interferes with REST API and loopback requests. The session should be closed by %2$s before making any HTTP requests.' ),
+ '
session_start()
',
+ '
session_write_close()
'
+ )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if the SQL server is up to date.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_sql_server() {
+ if ( ! $this->mysql_server_version ) {
+ $this->prepare_sql_data();
+ }
+
+ $result = array(
+ 'label' => __( 'SQL server is up to date' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'The SQL server is a required piece of software for the database WordPress uses to store all your site’s content and settings.' )
+ ),
+ 'actions' => sprintf(
+ '
%s %s
',
+ /* translators: Localized version of WordPress requirements if one exists. */
+ esc_url( __( 'https://wordpress.org/about/requirements/' ) ),
+ __( 'Learn more about what WordPress requires to run.' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ ),
+ 'test' => 'sql_server',
+ );
+
+ $db_dropin = file_exists( WP_CONTENT_DIR . '/db.php' );
+
+ if ( ! $this->is_recommended_mysql_version ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'Outdated SQL server' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: The database engine in use (MySQL or MariaDB). 2: Database server recommended version number. */
+ __( 'For optimal performance and security reasons, you should consider running %1$s version %2$s or higher. Contact your web hosting company to correct this.' ),
+ ( $this->is_mariadb ? 'MariaDB' : 'MySQL' ),
+ $this->mysql_recommended_version
+ )
+ );
+ }
+
+ if ( ! $this->is_acceptable_mysql_version ) {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'Severely outdated SQL server' );
+ $result['badge']['label'] = __( 'Security' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: The database engine in use (MySQL or MariaDB). 2: Database server minimum version number. */
+ __( 'WordPress requires %1$s version %2$s or higher. Contact your web hosting company to correct this.' ),
+ ( $this->is_mariadb ? 'MariaDB' : 'MySQL' ),
+ $this->mysql_required_version
+ )
+ );
+ }
+
+ if ( $db_dropin ) {
+ $result['description'] .= sprintf(
+ '
%s
',
+ wp_kses(
+ sprintf(
+ /* translators: 1: The name of the drop-in. 2: The name of the database engine. */
+ __( 'You are using a %1$s drop-in which might mean that a %2$s database is not being used.' ),
+ '
wp-content/db.php
',
+ ( $this->is_mariadb ? 'MariaDB' : 'MySQL' )
+ ),
+ array(
+ 'code' => true,
+ )
+ )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if the database server is capable of using utf8mb4.
+ *
+ * @since 5.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @return array The test results.
+ */
+ public function get_test_utf8mb4_support() {
+ global $wpdb;
+
+ if ( ! $this->mysql_server_version ) {
+ $this->prepare_sql_data();
+ }
+
+ $result = array(
+ 'label' => __( 'UTF8MB4 is supported' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'UTF8MB4 is the character set WordPress prefers for database storage because it safely supports the widest set of characters and encodings, including Emoji, enabling better support for non-English languages.' )
+ ),
+ 'actions' => '',
+ 'test' => 'utf8mb4_support',
+ );
+
+ if ( ! $this->is_mariadb ) {
+ if ( version_compare( $this->mysql_server_version, '5.5.3', '<' ) ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'utf8mb4 requires a MySQL update' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: Version number. */
+ __( 'WordPress’ utf8mb4 support requires MySQL version %s or greater. Please contact your server administrator.' ),
+ '5.5.3'
+ )
+ );
+ } else {
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Your MySQL version supports utf8mb4.' )
+ );
+ }
+ } else { // MariaDB introduced utf8mb4 support in 5.5.0.
+ if ( version_compare( $this->mysql_server_version, '5.5.0', '<' ) ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'utf8mb4 requires a MariaDB update' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: Version number. */
+ __( 'WordPress’ utf8mb4 support requires MariaDB version %s or greater. Please contact your server administrator.' ),
+ '5.5.0'
+ )
+ );
+ } else {
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Your MariaDB version supports utf8mb4.' )
+ );
+ }
+ }
+
+ // phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysqli_get_client_info
+ $mysql_client_version = mysqli_get_client_info();
+
+ /*
+ * libmysql has supported utf8mb4 since 5.5.3, same as the MySQL server.
+ * mysqlnd has supported utf8mb4 since 5.0.9.
+ */
+ if ( str_contains( $mysql_client_version, 'mysqlnd' ) ) {
+ $mysql_client_version = preg_replace( '/^\D+([\d.]+).*/', '$1', $mysql_client_version );
+ if ( version_compare( $mysql_client_version, '5.0.9', '<' ) ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'utf8mb4 requires a newer client library' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: Name of the library, 2: Number of version. */
+ __( 'WordPress’ utf8mb4 support requires MySQL client library (%1$s) version %2$s or newer. Please contact your server administrator.' ),
+ 'mysqlnd',
+ '5.0.9'
+ )
+ );
+ }
+ } else {
+ if ( version_compare( $mysql_client_version, '5.5.3', '<' ) ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'utf8mb4 requires a newer client library' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: Name of the library, 2: Number of version. */
+ __( 'WordPress’ utf8mb4 support requires MySQL client library (%1$s) version %2$s or newer. Please contact your server administrator.' ),
+ 'libmysql',
+ '5.5.3'
+ )
+ );
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if the site can communicate with WordPress.org.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_dotorg_communication() {
+ $result = array(
+ 'label' => __( 'Can communicate with WordPress.org' ),
+ 'status' => '',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Communicating with the WordPress servers is used to check for new versions, and to both install and update WordPress core, themes or plugins.' )
+ ),
+ 'actions' => '',
+ 'test' => 'dotorg_communication',
+ );
+
+ $wp_dotorg = wp_remote_get(
+ 'https://api.wordpress.org',
+ array(
+ 'timeout' => 10,
+ )
+ );
+ if ( ! is_wp_error( $wp_dotorg ) ) {
+ $result['status'] = 'good';
+ } else {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'Could not reach WordPress.org' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ '
%s %s',
+ /* translators: Hidden accessibility text. */
+ __( 'Error' ),
+ sprintf(
+ /* translators: 1: The IP address WordPress.org resolves to. 2: The error returned by the lookup. */
+ __( 'Your site is unable to reach WordPress.org at %1$s, and returned the error: %2$s' ),
+ gethostbyname( 'api.wordpress.org' ),
+ $wp_dotorg->get_error_message()
+ )
+ )
+ );
+
+ $result['actions'] = sprintf(
+ '
%s %s
',
+ /* translators: Localized Support reference. */
+ esc_url( __( 'https://wordpress.org/support/forums/' ) ),
+ __( 'Get help resolving this issue.' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if debug information is enabled.
+ *
+ * When WP_DEBUG is enabled, errors and information may be disclosed to site visitors,
+ * or logged to a publicly accessible file.
+ *
+ * Debugging is also frequently left enabled after looking for errors on a site,
+ * as site owners do not understand the implications of this.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_is_in_debug_mode() {
+ $result = array(
+ 'label' => __( 'Your site is not set to output debug information' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Debug mode is often enabled to gather more details about an error or site failure, but may contain sensitive information which should not be available on a publicly available website.' )
+ ),
+ 'actions' => sprintf(
+ '
%s %s
',
+ /* translators: Documentation explaining debugging in WordPress. */
+ esc_url( __( 'https://wordpress.org/documentation/article/debugging-in-wordpress/' ) ),
+ __( 'Learn more about debugging in WordPress.' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ ),
+ 'test' => 'is_in_debug_mode',
+ );
+
+ if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
+ if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
+ $result['label'] = __( 'Your site is set to log errors to a potentially public file' );
+
+ $result['status'] = str_starts_with( ini_get( 'error_log' ), ABSPATH ) ? 'critical' : 'recommended';
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: WP_DEBUG_LOG */
+ __( 'The value, %s, has been added to this website’s configuration file. This means any errors on the site will be written to a file which is potentially available to all users.' ),
+ '
WP_DEBUG_LOG
'
+ )
+ );
+ }
+
+ if ( defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY ) {
+ $result['label'] = __( 'Your site is set to display errors to site visitors' );
+
+ $result['status'] = 'critical';
+
+ // On development environments, set the status to recommended.
+ if ( $this->is_development_environment() ) {
+ $result['status'] = 'recommended';
+ }
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: WP_DEBUG_DISPLAY, 2: WP_DEBUG */
+ __( 'The value, %1$s, has either been enabled by %2$s or added to your configuration file. This will make errors display on the front end of your site.' ),
+ '
WP_DEBUG_DISPLAY
',
+ '
WP_DEBUG
'
+ )
+ );
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if the site is serving content over HTTPS.
+ *
+ * Many sites have varying degrees of HTTPS support, the most common of which is sites that have it
+ * enabled, but only if you visit the right site address.
+ *
+ * @since 5.2.0
+ * @since 5.7.0 Updated to rely on {@see wp_is_using_https()} and {@see wp_is_https_supported()}.
+ *
+ * @return array The test results.
+ */
+ public function get_test_https_status() {
+ /*
+ * Check HTTPS detection results.
+ */
+ $errors = wp_get_https_detection_errors();
+
+ $default_update_url = wp_get_default_update_https_url();
+
+ $result = array(
+ 'label' => __( 'Your website is using an active HTTPS connection' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'An HTTPS connection is a more secure way of browsing the web. Many services now have HTTPS as a requirement. HTTPS allows you to take advantage of new features that can increase site speed, improve search rankings, and gain the trust of your visitors by helping to protect their online privacy.' )
+ ),
+ 'actions' => sprintf(
+ '
%s %s
',
+ esc_url( $default_update_url ),
+ __( 'Learn more about why you should use HTTPS' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ ),
+ 'test' => 'https_status',
+ );
+
+ if ( ! wp_is_using_https() ) {
+ /*
+ * If the website is not using HTTPS, provide more information
+ * about whether it is supported and how it can be enabled.
+ */
+ $result['status'] = 'recommended';
+ $result['label'] = __( 'Your website does not use HTTPS' );
+
+ if ( wp_is_site_url_using_https() ) {
+ if ( is_ssl() ) {
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: URL to Settings > General > Site Address. */
+ __( 'You are accessing this website using HTTPS, but your
Site Address is not set up to use HTTPS by default.' ),
+ esc_url( admin_url( 'options-general.php' ) . '#home' )
+ )
+ );
+ } else {
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: URL to Settings > General > Site Address. */
+ __( 'Your
Site Address is not set up to use HTTPS.' ),
+ esc_url( admin_url( 'options-general.php' ) . '#home' )
+ )
+ );
+ }
+ } else {
+ if ( is_ssl() ) {
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: URL to Settings > General > WordPress Address, 2: URL to Settings > General > Site Address. */
+ __( 'You are accessing this website using HTTPS, but your
WordPress Address and
Site Address are not set up to use HTTPS by default.' ),
+ esc_url( admin_url( 'options-general.php' ) . '#siteurl' ),
+ esc_url( admin_url( 'options-general.php' ) . '#home' )
+ )
+ );
+ } else {
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: URL to Settings > General > WordPress Address, 2: URL to Settings > General > Site Address. */
+ __( 'Your
WordPress Address and
Site Address are not set up to use HTTPS.' ),
+ esc_url( admin_url( 'options-general.php' ) . '#siteurl' ),
+ esc_url( admin_url( 'options-general.php' ) . '#home' )
+ )
+ );
+ }
+ }
+
+ if ( wp_is_https_supported() ) {
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'HTTPS is already supported for your website.' )
+ );
+
+ if ( defined( 'WP_HOME' ) || defined( 'WP_SITEURL' ) ) {
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: wp-config.php, 2: WP_HOME, 3: WP_SITEURL */
+ __( 'However, your WordPress Address is currently controlled by a PHP constant and therefore cannot be updated. You need to edit your %1$s and remove or update the definitions of %2$s and %3$s.' ),
+ '
wp-config.php
',
+ '
WP_HOME
',
+ '
WP_SITEURL
'
+ )
+ );
+ } elseif ( current_user_can( 'update_https' ) ) {
+ $default_direct_update_url = add_query_arg( 'action', 'update_https', wp_nonce_url( admin_url( 'site-health.php' ), 'wp_update_https' ) );
+ $direct_update_url = wp_get_direct_update_https_url();
+
+ if ( ! empty( $direct_update_url ) ) {
+ $result['actions'] = sprintf(
+ '
%2$s %3$s
',
+ esc_url( $direct_update_url ),
+ __( 'Update your site to use HTTPS' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ );
+ } else {
+ $result['actions'] = sprintf(
+ '
%2$s
',
+ esc_url( $default_direct_update_url ),
+ __( 'Update your site to use HTTPS' )
+ );
+ }
+ }
+ } else {
+ // If host-specific "Update HTTPS" URL is provided, include a link.
+ $update_url = wp_get_update_https_url();
+ if ( $update_url !== $default_update_url ) {
+ $result['description'] .= sprintf(
+ '
%s %s
',
+ esc_url( $update_url ),
+ __( 'Talk to your web host about supporting HTTPS for your website.' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ );
+ } else {
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Talk to your web host about supporting HTTPS for your website.' )
+ );
+ }
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Checks if the HTTP API can handle SSL/TLS requests.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test result.
+ */
+ public function get_test_ssl_support() {
+ $result = array(
+ 'label' => '',
+ 'status' => '',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Securely communicating between servers are needed for transactions such as fetching files, conducting sales on store sites, and much more.' )
+ ),
+ 'actions' => '',
+ 'test' => 'ssl_support',
+ );
+
+ $supports_https = wp_http_supports( array( 'ssl' ) );
+
+ if ( $supports_https ) {
+ $result['status'] = 'good';
+
+ $result['label'] = __( 'Your site can communicate securely with other services' );
+ } else {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'Your site is unable to communicate securely with other services' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'Talk to your web host about OpenSSL support for PHP.' )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if scheduled events run as intended.
+ *
+ * If scheduled events are not running, this may indicate something with WP_Cron is not working
+ * as intended, or that there are orphaned events hanging around from older code.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_scheduled_events() {
+ $result = array(
+ 'label' => __( 'Scheduled events are running' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Scheduled events are what periodically looks for updates to plugins, themes and WordPress itself. It is also what makes sure scheduled posts are published on time. It may also be used by various plugins to make sure that planned actions are executed.' )
+ ),
+ 'actions' => '',
+ 'test' => 'scheduled_events',
+ );
+
+ $this->wp_schedule_test_init();
+
+ if ( is_wp_error( $this->has_missed_cron() ) ) {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'It was not possible to check your scheduled events' );
+
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: The error message returned while from the cron scheduler. */
+ __( 'While trying to test your site’s scheduled events, the following error was returned: %s' ),
+ $this->has_missed_cron()->get_error_message()
+ )
+ );
+ } elseif ( $this->has_missed_cron() ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'A scheduled event has failed' );
+
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: The name of the failed cron event. */
+ __( 'The scheduled event, %s, failed to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
+ $this->last_missed_cron
+ )
+ );
+ } elseif ( $this->has_late_cron() ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'A scheduled event is late' );
+
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: The name of the late cron event. */
+ __( 'The scheduled event, %s, is late to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
+ $this->last_late_cron
+ )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if WordPress can run automated background updates.
+ *
+ * Background updates in WordPress are primarily used for minor releases and security updates.
+ * It's important to either have these working, or be aware that they are intentionally disabled
+ * for whatever reason.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_background_updates() {
+ $result = array(
+ 'label' => __( 'Background updates are working' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Background updates ensure that WordPress can auto-update if a security update is released for the version you are currently using.' )
+ ),
+ 'actions' => '',
+ 'test' => 'background_updates',
+ );
+
+ if ( ! class_exists( 'WP_Site_Health_Auto_Updates' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/class-wp-site-health-auto-updates.php';
+ }
+
+ /*
+ * Run the auto-update tests in a separate class,
+ * as there are many considerations to be made.
+ */
+ $automatic_updates = new WP_Site_Health_Auto_Updates();
+ $tests = $automatic_updates->run_tests();
+
+ $output = '
';
+
+ foreach ( $tests as $test ) {
+ /* translators: Hidden accessibility text. */
+ $severity_string = __( 'Passed' );
+
+ if ( 'fail' === $test->severity ) {
+ $result['label'] = __( 'Background updates are not working as expected' );
+
+ $result['status'] = 'critical';
+
+ /* translators: Hidden accessibility text. */
+ $severity_string = __( 'Error' );
+ }
+
+ if ( 'warning' === $test->severity && 'good' === $result['status'] ) {
+ $result['label'] = __( 'Background updates may not be working properly' );
+
+ $result['status'] = 'recommended';
+
+ /* translators: Hidden accessibility text. */
+ $severity_string = __( 'Warning' );
+ }
+
+ $output .= sprintf(
+ '%s %s ',
+ esc_attr( $test->severity ),
+ $severity_string,
+ $test->description
+ );
+ }
+
+ $output .= ' ';
+
+ if ( 'good' !== $result['status'] ) {
+ $result['description'] .= $output;
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if plugin and theme auto-updates appear to be configured correctly.
+ *
+ * @since 5.5.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_plugin_theme_auto_updates() {
+ $result = array(
+ 'label' => __( 'Plugin and theme auto-updates appear to be configured correctly' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Plugin and theme auto-updates ensure that the latest versions are always installed.' )
+ ),
+ 'actions' => '',
+ 'test' => 'plugin_theme_auto_updates',
+ );
+
+ $check_plugin_theme_updates = $this->detect_plugin_theme_auto_update_issues();
+
+ $result['status'] = $check_plugin_theme_updates->status;
+
+ if ( 'good' !== $result['status'] ) {
+ $result['label'] = __( 'Your site may have problems auto-updating plugins and themes' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ $check_plugin_theme_updates->message
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests available disk space for updates.
+ *
+ * @since 6.3.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_available_updates_disk_space() {
+ $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR . '/upgrade/' ) : false;
+
+ $result = array(
+ 'label' => __( 'Disk space available to safely perform updates' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ /* translators: %s: Available disk space in MB or GB. */
+ '
' . __( '%s available disk space was detected, update routines can be performed safely.' ) . '
',
+ size_format( $available_space )
+ ),
+ 'actions' => '',
+ 'test' => 'available_updates_disk_space',
+ );
+
+ if ( false === $available_space ) {
+ $result['description'] = __( 'Could not determine available disk space for updates.' );
+ $result['status'] = 'recommended';
+ } elseif ( $available_space < 20 * MB_IN_BYTES ) {
+ $result['description'] = __( 'Available disk space is critically low, less than 20 MB available. Proceed with caution, updates may fail.' );
+ $result['status'] = 'critical';
+ } elseif ( $available_space < 100 * MB_IN_BYTES ) {
+ $result['description'] = __( 'Available disk space is low, less than 100 MB available.' );
+ $result['status'] = 'recommended';
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if plugin and theme temporary backup directories are writable or can be created.
+ *
+ * @since 6.3.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @return array The test results.
+ */
+ public function get_test_update_temp_backup_writable() {
+ global $wp_filesystem;
+
+ $result = array(
+ 'label' => __( 'Plugin and theme temporary backup directory is writable' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ /* translators: %s: wp-content/upgrade-temp-backup */
+ '
' . __( 'The %s directory used to improve the stability of plugin and theme updates is writable.' ) . '
',
+ '
wp-content/upgrade-temp-backup
'
+ ),
+ 'actions' => '',
+ 'test' => 'update_temp_backup_writable',
+ );
+
+ if ( ! function_exists( 'WP_Filesystem' ) ) {
+ require_once ABSPATH . '/wp-admin/includes/file.php';
+ }
+
+ ob_start();
+ $credentials = request_filesystem_credentials( '' );
+ ob_end_clean();
+
+ if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
+ $result['status'] = 'recommended';
+ $result['label'] = __( 'Could not access filesystem' );
+ $result['description'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
+ return $result;
+ }
+
+ $wp_content = $wp_filesystem->wp_content_dir();
+
+ if ( ! $wp_content ) {
+ $result['status'] = 'critical';
+ $result['label'] = __( 'Unable to locate WordPress content directory' );
+ $result['description'] = sprintf(
+ /* translators: %s: wp-content */
+ '
' . __( 'The %s directory cannot be located.' ) . '
',
+ '
wp-content
'
+ );
+ return $result;
+ }
+
+ $upgrade_dir_exists = $wp_filesystem->is_dir( "$wp_content/upgrade" );
+ $upgrade_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade" );
+ $backup_dir_exists = $wp_filesystem->is_dir( "$wp_content/upgrade-temp-backup" );
+ $backup_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade-temp-backup" );
+
+ $plugins_dir_exists = $wp_filesystem->is_dir( "$wp_content/upgrade-temp-backup/plugins" );
+ $plugins_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade-temp-backup/plugins" );
+ $themes_dir_exists = $wp_filesystem->is_dir( "$wp_content/upgrade-temp-backup/themes" );
+ $themes_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade-temp-backup/themes" );
+
+ if ( $plugins_dir_exists && ! $plugins_dir_is_writable && $themes_dir_exists && ! $themes_dir_is_writable ) {
+ $result['status'] = 'critical';
+ $result['label'] = __( 'Plugin and theme temporary backup directories exist but are not writable' );
+ $result['description'] = sprintf(
+ /* translators: 1: wp-content/upgrade-temp-backup/plugins, 2: wp-content/upgrade-temp-backup/themes. */
+ '
' . __( 'The %1$s and %2$s directories exist but are not writable. These directories are used to improve the stability of plugin updates. Please make sure the server has write permissions to these directories.' ) . '
',
+ '
wp-content/upgrade-temp-backup/plugins
',
+ '
wp-content/upgrade-temp-backup/themes
'
+ );
+ return $result;
+ }
+
+ if ( $plugins_dir_exists && ! $plugins_dir_is_writable ) {
+ $result['status'] = 'critical';
+ $result['label'] = __( 'Plugin temporary backup directory exists but is not writable' );
+ $result['description'] = sprintf(
+ /* translators: %s: wp-content/upgrade-temp-backup/plugins */
+ '
' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of plugin updates. Please make sure the server has write permissions to this directory.' ) . '
',
+ '
wp-content/upgrade-temp-backup/plugins
'
+ );
+ return $result;
+ }
+
+ if ( $themes_dir_exists && ! $themes_dir_is_writable ) {
+ $result['status'] = 'critical';
+ $result['label'] = __( 'Theme temporary backup directory exists but is not writable' );
+ $result['description'] = sprintf(
+ /* translators: %s: wp-content/upgrade-temp-backup/themes */
+ '
' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of theme updates. Please make sure the server has write permissions to this directory.' ) . '
',
+ '
wp-content/upgrade-temp-backup/themes
'
+ );
+ return $result;
+ }
+
+ if ( ( ! $plugins_dir_exists || ! $themes_dir_exists ) && $backup_dir_exists && ! $backup_dir_is_writable ) {
+ $result['status'] = 'critical';
+ $result['label'] = __( 'The temporary backup directory exists but is not writable' );
+ $result['description'] = sprintf(
+ /* translators: %s: wp-content/upgrade-temp-backup */
+ '
' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of plugin and theme updates. Please make sure the server has write permissions to this directory.' ) . '
',
+ '
wp-content/upgrade-temp-backup
'
+ );
+ return $result;
+ }
+
+ if ( ! $backup_dir_exists && $upgrade_dir_exists && ! $upgrade_dir_is_writable ) {
+ $result['status'] = 'critical';
+ $result['label'] = __( 'The upgrade directory exists but is not writable' );
+ $result['description'] = sprintf(
+ /* translators: %s: wp-content/upgrade */
+ '
' . __( 'The %s directory exists but is not writable. This directory is used for plugin and theme updates. Please make sure the server has write permissions to this directory.' ) . '
',
+ '
wp-content/upgrade
'
+ );
+ return $result;
+ }
+
+ if ( ! $upgrade_dir_exists && ! $wp_filesystem->is_writable( $wp_content ) ) {
+ $result['status'] = 'critical';
+ $result['label'] = __( 'The upgrade directory cannot be created' );
+ $result['description'] = sprintf(
+ /* translators: 1: wp-content/upgrade, 2: wp-content. */
+ '
' . __( 'The %1$s directory does not exist, and the server does not have write permissions in %2$s to create it. This directory is used for plugin and theme updates. Please make sure the server has write permissions in %2$s.' ) . '
',
+ '
wp-content/upgrade
',
+ '
wp-content
'
+ );
+ return $result;
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if loopbacks work as expected.
+ *
+ * A loopback is when WordPress queries itself, for example to start a new WP_Cron instance,
+ * or when editing a plugin or theme. This has shown itself to be a recurring issue,
+ * as code can very easily break this interaction.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_loopback_requests() {
+ $result = array(
+ 'label' => __( 'Your site can perform loopback requests' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'Loopback requests are used to run scheduled events, and are also used by the built-in editors for themes and plugins to verify code stability.' )
+ ),
+ 'actions' => '',
+ 'test' => 'loopback_requests',
+ );
+
+ $check_loopback = $this->can_perform_loopback();
+
+ $result['status'] = $check_loopback->status;
+
+ if ( 'good' !== $result['status'] ) {
+ $result['label'] = __( 'Your site could not complete a loopback request' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ $check_loopback->message
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if HTTP requests are blocked.
+ *
+ * It's possible to block all outgoing communication (with the possibility of allowing certain
+ * hosts) via the HTTP API. This may create problems for users as many features are running as
+ * services these days.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_http_requests() {
+ $result = array(
+ 'label' => __( 'HTTP requests seem to be working as expected' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'It is possible for site maintainers to block all, or some, communication to other sites and services. If set up incorrectly, this may prevent plugins and themes from working as intended.' )
+ ),
+ 'actions' => '',
+ 'test' => 'http_requests',
+ );
+
+ $blocked = false;
+ $hosts = array();
+
+ if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL ) {
+ $blocked = true;
+ }
+
+ if ( defined( 'WP_ACCESSIBLE_HOSTS' ) ) {
+ $hosts = explode( ',', WP_ACCESSIBLE_HOSTS );
+ }
+
+ if ( $blocked && 0 === count( $hosts ) ) {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'HTTP requests are blocked' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: Name of the constant used. */
+ __( 'HTTP requests have been blocked by the %s constant, with no allowed hosts.' ),
+ '
WP_HTTP_BLOCK_EXTERNAL
'
+ )
+ );
+ }
+
+ if ( $blocked && 0 < count( $hosts ) ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'HTTP requests are partially blocked' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: Name of the constant used. 2: List of allowed hostnames. */
+ __( 'HTTP requests have been blocked by the %1$s constant, with some allowed hosts: %2$s.' ),
+ '
WP_HTTP_BLOCK_EXTERNAL
',
+ implode( ',', $hosts )
+ )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if the REST API is accessible.
+ *
+ * Various security measures may block the REST API from working, or it may have been disabled in general.
+ * This is required for the new block editor to work, so we explicitly test for this.
+ *
+ * @since 5.2.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_rest_availability() {
+ $result = array(
+ 'label' => __( 'The REST API is available' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'The REST API is one way that WordPress and other applications communicate with the server. For example, the block editor screen relies on the REST API to display and save your posts and pages.' )
+ ),
+ 'actions' => '',
+ 'test' => 'rest_availability',
+ );
+
+ $cookies = wp_unslash( $_COOKIE );
+ $timeout = 10; // 10 seconds.
+ $headers = array(
+ 'Cache-Control' => 'no-cache',
+ 'X-WP-Nonce' => wp_create_nonce( 'wp_rest' ),
+ );
+ /** This filter is documented in wp-includes/class-wp-http-streams.php */
+ $sslverify = apply_filters( 'https_local_ssl_verify', false );
+
+ // Include Basic auth in loopback requests.
+ if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
+ $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
+ }
+
+ $url = rest_url( 'wp/v2/types/post' );
+
+ // The context for this is editing with the new block editor.
+ $url = add_query_arg(
+ array(
+ 'context' => 'edit',
+ ),
+ $url
+ );
+
+ $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
+
+ if ( is_wp_error( $r ) ) {
+ $result['status'] = 'critical';
+
+ $result['label'] = __( 'The REST API encountered an error' );
+
+ $result['description'] .= sprintf(
+ '
%s
%s %s
',
+ __( 'When testing the REST API, an error was encountered:' ),
+ sprintf(
+ // translators: %s: The REST API URL.
+ __( 'REST API Endpoint: %s' ),
+ $url
+ ),
+ sprintf(
+ // translators: 1: The WordPress error code. 2: The WordPress error message.
+ __( 'REST API Response: (%1$s) %2$s' ),
+ $r->get_error_code(),
+ $r->get_error_message()
+ )
+ );
+ } elseif ( 200 !== wp_remote_retrieve_response_code( $r ) ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'The REST API encountered an unexpected result' );
+
+ $result['description'] .= sprintf(
+ '
%s
%s %s
',
+ __( 'When testing the REST API, an unexpected result was returned:' ),
+ sprintf(
+ // translators: %s: The REST API URL.
+ __( 'REST API Endpoint: %s' ),
+ $url
+ ),
+ sprintf(
+ // translators: 1: The WordPress error code. 2: The HTTP status code error message.
+ __( 'REST API Response: (%1$s) %2$s' ),
+ wp_remote_retrieve_response_code( $r ),
+ wp_remote_retrieve_response_message( $r )
+ )
+ );
+ } else {
+ $json = json_decode( wp_remote_retrieve_body( $r ), true );
+
+ if ( false !== $json && ! isset( $json['capabilities'] ) ) {
+ $result['status'] = 'recommended';
+
+ $result['label'] = __( 'The REST API did not behave correctly' );
+
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: %s: The name of the query parameter being tested. */
+ __( 'The REST API did not process the %s query parameter correctly.' ),
+ '
context
'
+ )
+ );
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if 'file_uploads' directive in PHP.ini is turned off.
+ *
+ * @since 5.5.0
+ *
+ * @return array The test results.
+ */
+ public function get_test_file_uploads() {
+ $result = array(
+ 'label' => __( 'Files can be uploaded' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: file_uploads, 2: php.ini */
+ __( 'The %1$s directive in %2$s determines if uploading files is allowed on your site.' ),
+ '
file_uploads
',
+ '
php.ini
'
+ )
+ ),
+ 'actions' => '',
+ 'test' => 'file_uploads',
+ );
+
+ if ( ! function_exists( 'ini_get' ) ) {
+ $result['status'] = 'critical';
+ $result['description'] .= sprintf(
+ /* translators: %s: ini_get() */
+ __( 'The %s function has been disabled, some media settings are unavailable because of this.' ),
+ '
ini_get()
'
+ );
+ return $result;
+ }
+
+ if ( empty( ini_get( 'file_uploads' ) ) ) {
+ $result['status'] = 'critical';
+ $result['description'] .= sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: file_uploads, 2: 0 */
+ __( '%1$s is set to %2$s. You won\'t be able to upload files on your site.' ),
+ '
file_uploads
',
+ '
0
'
+ )
+ );
+ return $result;
+ }
+
+ $post_max_size = ini_get( 'post_max_size' );
+ $upload_max_filesize = ini_get( 'upload_max_filesize' );
+
+ if ( wp_convert_hr_to_bytes( $post_max_size ) < wp_convert_hr_to_bytes( $upload_max_filesize ) ) {
+ $result['label'] = sprintf(
+ /* translators: 1: post_max_size, 2: upload_max_filesize */
+ __( 'The "%1$s" value is smaller than "%2$s"' ),
+ 'post_max_size',
+ 'upload_max_filesize'
+ );
+ $result['status'] = 'recommended';
+
+ if ( 0 === wp_convert_hr_to_bytes( $post_max_size ) ) {
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: post_max_size, 2: upload_max_filesize */
+ __( 'The setting for %1$s is currently configured as 0, this could cause some problems when trying to upload files through plugin or theme features that rely on various upload methods. It is recommended to configure this setting to a fixed value, ideally matching the value of %2$s, as some upload methods read the value 0 as either unlimited, or disabled.' ),
+ '
post_max_size
',
+ '
upload_max_filesize
'
+ )
+ );
+ } else {
+ $result['description'] = sprintf(
+ '
%s
',
+ sprintf(
+ /* translators: 1: post_max_size, 2: upload_max_filesize */
+ __( 'The setting for %1$s is smaller than %2$s, this could cause some problems when trying to upload files.' ),
+ '
post_max_size
',
+ '
upload_max_filesize
'
+ )
+ );
+ }
+
+ return $result;
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if the Authorization header has the expected values.
+ *
+ * @since 5.6.0
+ *
+ * @return array
+ */
+ public function get_test_authorization_header() {
+ $result = array(
+ 'label' => __( 'The Authorization header is working as expected' ),
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Security' ),
+ 'color' => 'blue',
+ ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'The Authorization header is used by third-party applications you have approved for this site. Without this header, those apps cannot connect to your site.' )
+ ),
+ 'actions' => '',
+ 'test' => 'authorization_header',
+ );
+
+ if ( ! isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) {
+ $result['label'] = __( 'The authorization header is missing' );
+ } elseif ( 'user' !== $_SERVER['PHP_AUTH_USER'] || 'pwd' !== $_SERVER['PHP_AUTH_PW'] ) {
+ $result['label'] = __( 'The authorization header is invalid' );
+ } else {
+ return $result;
+ }
+
+ $result['status'] = 'recommended';
+ $result['description'] .= sprintf(
+ '
%s
',
+ __( 'If you are still seeing this warning after having tried the actions below, you may need to contact your hosting provider for further assistance.' )
+ );
+
+ if ( ! function_exists( 'got_mod_rewrite' ) ) {
+ require_once ABSPATH . 'wp-admin/includes/misc.php';
+ }
+
+ if ( got_mod_rewrite() ) {
+ $result['actions'] .= sprintf(
+ '
%s
',
+ esc_url( admin_url( 'options-permalink.php' ) ),
+ __( 'Flush permalinks' )
+ );
+ } else {
+ $result['actions'] .= sprintf(
+ '
%s %s
',
+ __( 'https://developer.wordpress.org/rest-api/frequently-asked-questions/#why-is-authentication-not-working' ),
+ __( 'Learn how to configure the Authorization header.' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ );
+ }
+
+ return $result;
+ }
+
+ /**
+ * Tests if a full page cache is available.
+ *
+ * @since 6.1.0
+ *
+ * @return array The test result.
+ */
+ public function get_test_page_cache() {
+ $description = '
' . __( 'Page cache enhances the speed and performance of your site by saving and serving static pages instead of calling for a page every time a user visits.' ) . '
';
+ $description .= '
' . __( 'Page cache is detected by looking for an active page cache plugin as well as making three requests to the homepage and looking for one or more of the following HTTP client caching response headers:' ) . '
';
+ $description .= '
' . implode( '
,
', array_keys( $this->get_page_cache_headers() ) ) . '.
';
+
+ $result = array(
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'description' => wp_kses_post( $description ),
+ 'test' => 'page_cache',
+ 'status' => 'good',
+ 'label' => '',
+ 'actions' => sprintf(
+ '
%2$s %3$s
',
+ __( 'https://wordpress.org/documentation/article/optimization/#Caching' ),
+ __( 'Learn more about page cache' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ ),
+ );
+
+ $page_cache_detail = $this->get_page_cache_detail();
+
+ if ( is_wp_error( $page_cache_detail ) ) {
+ $result['label'] = __( 'Unable to detect the presence of page cache' );
+ $result['status'] = 'recommended';
+ $error_info = sprintf(
+ /* translators: 1: Error message, 2: Error code. */
+ __( 'Unable to detect page cache due to possible loopback request problem. Please verify that the loopback request test is passing. Error: %1$s (Code: %2$s)' ),
+ $page_cache_detail->get_error_message(),
+ $page_cache_detail->get_error_code()
+ );
+ $result['description'] = wp_kses_post( "
$error_info
" ) . $result['description'];
+ return $result;
+ }
+
+ $result['status'] = $page_cache_detail['status'];
+
+ switch ( $page_cache_detail['status'] ) {
+ case 'recommended':
+ $result['label'] = __( 'Page cache is not detected but the server response time is OK' );
+ break;
+ case 'good':
+ $result['label'] = __( 'Page cache is detected and the server response time is good' );
+ break;
+ default:
+ if ( empty( $page_cache_detail['headers'] ) && ! $page_cache_detail['advanced_cache_present'] ) {
+ $result['label'] = __( 'Page cache is not detected and the server response time is slow' );
+ } else {
+ $result['label'] = __( 'Page cache is detected but the server response time is still slow' );
+ }
+ }
+
+ $page_cache_test_summary = array();
+
+ if ( empty( $page_cache_detail['response_time'] ) ) {
+ $page_cache_test_summary[] = '
' . __( 'Server response time could not be determined. Verify that loopback requests are working.' );
+ } else {
+
+ $threshold = $this->get_good_response_time_threshold();
+ if ( $page_cache_detail['response_time'] < $threshold ) {
+ $page_cache_test_summary[] = '
' . sprintf(
+ /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
+ __( 'Median server response time was %1$s milliseconds. This is less than the recommended %2$s milliseconds threshold.' ),
+ number_format_i18n( $page_cache_detail['response_time'] ),
+ number_format_i18n( $threshold )
+ );
+ } else {
+ $page_cache_test_summary[] = '
' . sprintf(
+ /* translators: 1: The response time in milliseconds, 2: The recommended threshold in milliseconds. */
+ __( 'Median server response time was %1$s milliseconds. It should be less than the recommended %2$s milliseconds threshold.' ),
+ number_format_i18n( $page_cache_detail['response_time'] ),
+ number_format_i18n( $threshold )
+ );
+ }
+
+ if ( empty( $page_cache_detail['headers'] ) ) {
+ $page_cache_test_summary[] = '
' . __( 'No client caching response headers were detected.' );
+ } else {
+ $headers_summary = '
';
+ $headers_summary .= ' ' . sprintf(
+ /* translators: %d: Number of caching headers. */
+ _n(
+ 'There was %d client caching response header detected:',
+ 'There were %d client caching response headers detected:',
+ count( $page_cache_detail['headers'] )
+ ),
+ count( $page_cache_detail['headers'] )
+ );
+ $headers_summary .= '
' . implode( '
,
', $page_cache_detail['headers'] ) . '
.';
+ $page_cache_test_summary[] = $headers_summary;
+ }
+ }
+
+ if ( $page_cache_detail['advanced_cache_present'] ) {
+ $page_cache_test_summary[] = '
' . __( 'A page cache plugin was detected.' );
+ } elseif ( ! ( is_array( $page_cache_detail ) && ! empty( $page_cache_detail['headers'] ) ) ) {
+ // Note: This message is not shown if client caching response headers were present since an external caching layer may be employed.
+ $page_cache_test_summary[] = '
' . __( 'A page cache plugin was not detected.' );
+ }
+
+ $result['description'] .= '
' . implode( ' ', $page_cache_test_summary ) . ' ';
+ return $result;
+ }
+
+ /**
+ * Tests if the site uses persistent object cache and recommends to use it if not.
+ *
+ * @since 6.1.0
+ *
+ * @return array The test result.
+ */
+ public function get_test_persistent_object_cache() {
+ /**
+ * Filters the action URL for the persistent object cache health check.
+ *
+ * @since 6.1.0
+ *
+ * @param string $action_url Learn more link for persistent object cache health check.
+ */
+ $action_url = apply_filters(
+ 'site_status_persistent_object_cache_url',
+ /* translators: Localized Support reference. */
+ __( 'https://wordpress.org/documentation/article/optimization/#persistent-object-cache' )
+ );
+
+ $result = array(
+ 'test' => 'persistent_object_cache',
+ 'status' => 'good',
+ 'badge' => array(
+ 'label' => __( 'Performance' ),
+ 'color' => 'blue',
+ ),
+ 'label' => __( 'A persistent object cache is being used' ),
+ 'description' => sprintf(
+ '
%s
',
+ __( 'A persistent object cache makes your site’s database more efficient, resulting in faster load times because WordPress can retrieve your site’s content and settings much more quickly.' )
+ ),
+ 'actions' => sprintf(
+ '
%s %s
',
+ esc_url( $action_url ),
+ __( 'Learn more about persistent object caching.' ),
+ /* translators: Hidden accessibility text. */
+ __( '(opens in a new tab)' )
+ ),
+ );
+
+ if ( wp_using_ext_object_cache() ) {
+ return $result;
+ }
+
+ if ( ! $this->should_suggest_persistent_object_cache() ) {
+ $result['label'] = __( 'A persistent object cache is not required' );
+
+ return $result;
+ }
+
+ $available_services = $this->available_object_cache_services();
+
+ $notes = __( 'Your hosting provider can tell you if a persistent object cache can be enabled on your site.' );
+
+ if ( ! empty( $available_services ) ) {
+ $notes .= ' ' . sprintf(
+ /* translators: Available object caching services. */
+ __( 'Your host appears to support the following object caching services: %s.' ),
+ implode( ', ', $available_services )
+ );
+ }
+
+ /**
+ * Filters the second paragraph of the health check's description
+ * when suggesting the use of a persistent object cache.
+ *
+ * Hosts may want to replace the notes to recommend their preferred object caching solution.
+ *
+ * Plugin authors may want to append notes (not replace) on why object caching is recommended for their plugin.
+ *
+ * @since 6.1.0
+ *
+ * @param string $notes The notes appended to the health check description.
+ * @param string[] $available_services The list of available persistent object cache services.
+ */
+ $notes = apply_filters( 'site_status_persistent_object_cache_notes', $notes, $available_services );
+
+ $result['status'] = 'recommended';
+ $result['label'] = __( 'You should use a persistent object cache' );
+ $result['description'] .= sprintf(
+ '
%s
',
+ wp_kses(
+ $notes,
+ array(
+ 'a' => array( 'href' => true ),
+ 'code' => true,
+ 'em' => true,
+ 'strong' => true,
+ )
+ )
+ );
+
+ return $result;
+ }
+
+ /**
+ * Returns a set of tests that belong to the site status page.
+ *
+ * Each site status test is defined here, they may be `direct` tests, that run on page load, or `async` tests
+ * which will run later down the line via JavaScript calls to improve page performance and hopefully also user
+ * experiences.
+ *
+ * @since 5.2.0
+ * @since 5.6.0 Added support for `has_rest` and `permissions`.
+ *
+ * @return array The list of tests to run.
+ */
+ public static function get_tests() {
+ $tests = array(
+ 'direct' => array(
+ 'wordpress_version' => array(
+ 'label' => __( 'WordPress Version' ),
+ 'test' => 'wordpress_version',
+ ),
+ 'plugin_version' => array(
+ 'label' => __( 'Plugin Versions' ),
+ 'test' => 'plugin_version',
+ ),
+ 'theme_version' => array(
+ 'label' => __( 'Theme Versions' ),
+ 'test' => 'theme_version',
+ ),
+ 'php_version' => array(
+ 'label' => __( 'PHP Version' ),
+ 'test' => 'php_version',
+ ),
+ 'php_extensions' => array(
+ 'label' => __( 'PHP Extensions' ),
+ 'test' => 'php_extensions',
+ ),
+ 'php_default_timezone' => array(
+ 'label' => __( 'PHP Default Timezone' ),
+ 'test' => 'php_default_timezone',
+ ),
+ 'php_sessions' => array(
+ 'label' => __( 'PHP Sessions' ),
+ 'test' => 'php_sessions',
+ ),
+ 'sql_server' => array(
+ 'label' => __( 'Database Server version' ),
+ 'test' => 'sql_server',
+ ),
+ 'utf8mb4_support' => array(
+ 'label' => __( 'MySQL utf8mb4 support' ),
+ 'test' => 'utf8mb4_support',
+ ),
+ 'ssl_support' => array(
+ 'label' => __( 'Secure communication' ),
+ 'test' => 'ssl_support',
+ ),
+ 'scheduled_events' => array(
+ 'label' => __( 'Scheduled events' ),
+ 'test' => 'scheduled_events',
+ ),
+ 'http_requests' => array(
+ 'label' => __( 'HTTP Requests' ),
+ 'test' => 'http_requests',
+ ),
+ 'rest_availability' => array(
+ 'label' => __( 'REST API availability' ),
+ 'test' => 'rest_availability',
+ 'skip_cron' => true,
+ ),
+ 'debug_enabled' => array(
+ 'label' => __( 'Debugging enabled' ),
+ 'test' => 'is_in_debug_mode',
+ ),
+ 'file_uploads' => array(
+ 'label' => __( 'File uploads' ),
+ 'test' => 'file_uploads',
+ ),
+ 'plugin_theme_auto_updates' => array(
+ 'label' => __( 'Plugin and theme auto-updates' ),
+ 'test' => 'plugin_theme_auto_updates',
+ ),
+ 'update_temp_backup_writable' => array(
+ 'label' => __( 'Plugin and theme temporary backup directory access' ),
+ 'test' => 'update_temp_backup_writable',
+ ),
+ 'available_updates_disk_space' => array(
+ 'label' => __( 'Available disk space' ),
+ 'test' => 'available_updates_disk_space',
+ ),
+ ),
+ 'async' => array(
+ 'dotorg_communication' => array(
+ 'label' => __( 'Communication with WordPress.org' ),
+ 'test' => rest_url( 'wp-site-health/v1/tests/dotorg-communication' ),
+ 'has_rest' => true,
+ 'async_direct_test' => array( WP_Site_Health::get_instance(), 'get_test_dotorg_communication' ),
+ ),
+ 'background_updates' => array(
+ 'label' => __( 'Background updates' ),
+ 'test' => rest_url( 'wp-site-health/v1/tests/background-updates' ),
+ 'has_rest' => true,
+ 'async_direct_test' => array( WP_Site_Health::get_instance(), 'get_test_background_updates' ),
+ ),
+ 'loopback_requests' => array(
+ 'label' => __( 'Loopback request' ),
+ 'test' => rest_url( 'wp-site-health/v1/tests/loopback-requests' ),
+ 'has_rest' => true,
+ 'async_direct_test' => array( WP_Site_Health::get_instance(), 'get_test_loopback_requests' ),
+ ),
+ 'https_status' => array(
+ 'label' => __( 'HTTPS status' ),
+ 'test' => rest_url( 'wp-site-health/v1/tests/https-status' ),
+ 'has_rest' => true,
+ 'async_direct_test' => array( WP_Site_Health::get_instance(), 'get_test_https_status' ),
+ ),
+ ),
+ );
+
+ // Conditionally include Authorization header test if the site isn't protected by Basic Auth.
+ if ( ! wp_is_site_protected_by_basic_auth() ) {
+ $tests['async']['authorization_header'] = array(
+ 'label' => __( 'Authorization header' ),
+ 'test' => rest_url( 'wp-site-health/v1/tests/authorization-header' ),
+ 'has_rest' => true,
+ 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'user:pwd' ) ),
+ 'skip_cron' => true,
+ );
+ }
+
+ // Only check for caches in production environments.
+ if ( 'production' === wp_get_environment_type() ) {
+ $tests['async']['page_cache'] = array(
+ 'label' => __( 'Page cache' ),
+ 'test' => rest_url( 'wp-site-health/v1/tests/page-cache' ),
+ 'has_rest' => true,
+ 'async_direct_test' => array( WP_Site_Health::get_instance(), 'get_test_page_cache' ),
+ );
+
+ $tests['direct']['persistent_object_cache'] = array(
+ 'label' => __( 'Persistent object cache' ),
+ 'test' => 'persistent_object_cache',
+ );
+ }
+
+ /**
+ * Filters which site status tests are run on a site.
+ *
+ * The site health is determined by a set of tests based on best practices from
+ * both the WordPress Hosting Team and web standards in general.
+ *
+ * Some sites may not have the same requirements, for example the automatic update
+ * checks may be handled by a host, and are therefore disabled in core.
+ * Or maybe you want to introduce a new test, is caching enabled/disabled/stale for example.
+ *
+ * Tests may be added either as direct, or asynchronous ones. Any test that may require some time
+ * to complete should run asynchronously, to avoid extended loading periods within wp-admin.
+ *
+ * @since 5.2.0
+ * @since 5.6.0 Added the `async_direct_test` array key for asynchronous tests.
+ * Added the `skip_cron` array key for all tests.
+ *
+ * @param array[] $tests {
+ * An associative array of direct and asynchronous tests.
+ *
+ * @type array[] $direct {
+ * An array of direct tests.
+ *
+ * @type array ...$identifier {
+ * `$identifier` should be a unique identifier for the test. Plugins and themes are encouraged to
+ * prefix test identifiers with their slug to avoid collisions between tests.
+ *
+ * @type string $label The friendly label to identify the test.
+ * @type callable $test The callback function that runs the test and returns its result.
+ * @type bool $skip_cron Whether to skip this test when running as cron.
+ * }
+ * }
+ * @type array[] $async {
+ * An array of asynchronous tests.
+ *
+ * @type array ...$identifier {
+ * `$identifier` should be a unique identifier for the test. Plugins and themes are encouraged to
+ * prefix test identifiers with their slug to avoid collisions between tests.
+ *
+ * @type string $label The friendly label to identify the test.
+ * @type string $test An admin-ajax.php action to be called to perform the test, or
+ * if `$has_rest` is true, a URL to a REST API endpoint to perform
+ * the test.
+ * @type bool $has_rest Whether the `$test` property points to a REST API endpoint.
+ * @type bool $skip_cron Whether to skip this test when running as cron.
+ * @type callable $async_direct_test A manner of directly calling the test marked as asynchronous,
+ * as the scheduled event can not authenticate, and endpoints
+ * may require authentication.
+ * }
+ * }
+ * }
+ */
+ $tests = apply_filters( 'site_status_tests', $tests );
+
+ // Ensure that the filtered tests contain the required array keys.
+ $tests = array_merge(
+ array(
+ 'direct' => array(),
+ 'async' => array(),
+ ),
+ $tests
+ );
+
+ return $tests;
+ }
+
+ /**
+ * Adds a class to the body HTML tag.
+ *
+ * Filters the body class string for admin pages and adds our own class for easier styling.
+ *
+ * @since 5.2.0
+ *
+ * @param string $body_class The body class string.
+ * @return string The modified body class string.
+ */
+ public function admin_body_class( $body_class ) {
+ $screen = get_current_screen();
+ if ( 'site-health' !== $screen->id ) {
+ return $body_class;
+ }
+
+ $body_class .= ' site-health';
+
+ return $body_class;
+ }
+
+ /**
+ * Initiates the WP_Cron schedule test cases.
+ *
+ * @since 5.2.0
+ */
+ private function wp_schedule_test_init() {
+ $this->schedules = wp_get_schedules();
+ $this->get_cron_tasks();
+ }
+
+ /**
+ * Populates the list of cron events and store them to a class-wide variable.
+ *
+ * @since 5.2.0
+ */
+ private function get_cron_tasks() {
+ $cron_tasks = _get_cron_array();
+
+ if ( empty( $cron_tasks ) ) {
+ $this->crons = new WP_Error( 'no_tasks', __( 'No scheduled events exist on this site.' ) );
+ return;
+ }
+
+ $this->crons = array();
+
+ foreach ( $cron_tasks as $time => $cron ) {
+ foreach ( $cron as $hook => $dings ) {
+ foreach ( $dings as $sig => $data ) {
+
+ $this->crons[ "$hook-$sig-$time" ] = (object) array(
+ 'hook' => $hook,
+ 'time' => $time,
+ 'sig' => $sig,
+ 'args' => $data['args'],
+ 'schedule' => $data['schedule'],
+ 'interval' => isset( $data['interval'] ) ? $data['interval'] : null,
+ );
+
+ }
+ }
+ }
+ }
+
+ /**
+ * Checks if any scheduled tasks have been missed.
+ *
+ * Returns a boolean value of `true` if a scheduled task has been missed and ends processing.
+ *
+ * If the list of crons is an instance of WP_Error, returns the instance instead of a boolean value.
+ *
+ * @since 5.2.0
+ *
+ * @return bool|WP_Error True if a cron was missed, false if not. WP_Error if the cron is set to that.
+ */
+ public function has_missed_cron() {
+ if ( is_wp_error( $this->crons ) ) {
+ return $this->crons;
+ }
+
+ foreach ( $this->crons as $id => $cron ) {
+ if ( ( $cron->time - time() ) < $this->timeout_missed_cron ) {
+ $this->last_missed_cron = $cron->hook;
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Checks if any scheduled tasks are late.
+ *
+ * Returns a boolean value of `true` if a scheduled task is late and ends processing.
+ *
+ * If the list of crons is an instance of WP_Error, returns the instance instead of a boolean value.
+ *
+ * @since 5.3.0
+ *
+ * @return bool|WP_Error True if a cron is late, false if not. WP_Error if the cron is set to that.
+ */
+ public function has_late_cron() {
+ if ( is_wp_error( $this->crons ) ) {
+ return $this->crons;
+ }
+
+ foreach ( $this->crons as $id => $cron ) {
+ $cron_offset = $cron->time - time();
+ if (
+ $cron_offset >= $this->timeout_missed_cron &&
+ $cron_offset < $this->timeout_late_cron
+ ) {
+ $this->last_late_cron = $cron->hook;
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Checks for potential issues with plugin and theme auto-updates.
+ *
+ * Though there is no way to 100% determine if plugin and theme auto-updates are configured
+ * correctly, a few educated guesses could be made to flag any conditions that would
+ * potentially cause unexpected behaviors.
+ *
+ * @since 5.5.0
+ *
+ * @return object The test results.
+ */
+ public function detect_plugin_theme_auto_update_issues() {
+ $mock_plugin = (object) array(
+ 'id' => 'w.org/plugins/a-fake-plugin',
+ 'slug' => 'a-fake-plugin',
+ 'plugin' => 'a-fake-plugin/a-fake-plugin.php',
+ 'new_version' => '9.9',
+ 'url' => 'https://wordpress.org/plugins/a-fake-plugin/',
+ 'package' => 'https://downloads.wordpress.org/plugin/a-fake-plugin.9.9.zip',
+ 'icons' => array(
+ '2x' => 'https://ps.w.org/a-fake-plugin/assets/icon-256x256.png',
+ '1x' => 'https://ps.w.org/a-fake-plugin/assets/icon-128x128.png',
+ ),
+ 'banners' => array(
+ '2x' => 'https://ps.w.org/a-fake-plugin/assets/banner-1544x500.png',
+ '1x' => 'https://ps.w.org/a-fake-plugin/assets/banner-772x250.png',
+ ),
+ 'banners_rtl' => array(),
+ 'tested' => '5.5.0',
+ 'requires_php' => '5.6.20',
+ 'compatibility' => new stdClass(),
+ );
+
+ $mock_theme = (object) array(
+ 'theme' => 'a-fake-theme',
+ 'new_version' => '9.9',
+ 'url' => 'https://wordpress.org/themes/a-fake-theme/',
+ 'package' => 'https://downloads.wordpress.org/theme/a-fake-theme.9.9.zip',
+ 'requires' => '5.0.0',
+ 'requires_php' => '5.6.20',
+ );
+
+ $test_plugins_enabled = wp_is_auto_update_forced_for_item( 'plugin', true, $mock_plugin );
+ $test_themes_enabled = wp_is_auto_update_forced_for_item( 'theme', true, $mock_theme );
+
+ $ui_enabled_for_plugins = wp_is_auto_update_enabled_for_type( 'plugin' );
+ $ui_enabled_for_themes = wp_is_auto_update_enabled_for_type( 'theme' );
+ $plugin_filter_present = has_filter( 'auto_update_plugin' );
+ $theme_filter_present = has_filter( 'auto_update_theme' );
+
+ if ( ( ! $test_plugins_enabled && $ui_enabled_for_plugins )
+ || ( ! $test_themes_enabled && $ui_enabled_for_themes )
+ ) {
+ return (object) array(
+ 'status' => 'critical',
+ 'message' => __( 'Auto-updates for plugins and/or themes appear to be disabled, but settings are still set to be displayed. This could cause auto-updates to not work as expected.' ),
+ );
+ }
+
+ if ( ( ! $test_plugins_enabled && $plugin_filter_present )
+ && ( ! $test_themes_enabled && $theme_filter_present )
+ ) {
+ return (object) array(
+ 'status' => 'recommended',
+ 'message' => __( 'Auto-updates for plugins and themes appear to be disabled. This will prevent your site from receiving new versions automatically when available.' ),
+ );
+ } elseif ( ! $test_plugins_enabled && $plugin_filter_present ) {
+ return (object) array(
+ 'status' => 'recommended',
+ 'message' => __( 'Auto-updates for plugins appear to be disabled. This will prevent your site from receiving new versions automatically when available.' ),
+ );
+ } elseif ( ! $test_themes_enabled && $theme_filter_present ) {
+ return (object) array(
+ 'status' => 'recommended',
+ 'message' => __( 'Auto-updates for themes appear to be disabled. This will prevent your site from receiving new versions automatically when available.' ),
+ );
+ }
+
+ return (object) array(
+ 'status' => 'good',
+ 'message' => __( 'There appear to be no issues with plugin and theme auto-updates.' ),
+ );
+ }
+
+ /**
+ * Runs a loopback test on the site.
+ *
+ * Loopbacks are what WordPress uses to communicate with itself to start up WP_Cron, scheduled posts,
+ * make sure plugin or theme edits don't cause site failures and similar.
+ *
+ * @since 5.2.0
+ *
+ * @return object The test results.
+ */
+ public function can_perform_loopback() {
+ $body = array( 'site-health' => 'loopback-test' );
+ $cookies = wp_unslash( $_COOKIE );
+ $timeout = 10; // 10 seconds.
+ $headers = array(
+ 'Cache-Control' => 'no-cache',
+ );
+ /** This filter is documented in wp-includes/class-wp-http-streams.php */
+ $sslverify = apply_filters( 'https_local_ssl_verify', false );
+
+ // Include Basic auth in loopback requests.
+ if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
+ $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
+ }
+
+ $url = site_url( 'wp-cron.php' );
+
+ /*
+ * A post request is used for the wp-cron.php loopback test to cause the file
+ * to finish early without triggering cron jobs. This has two benefits:
+ * - cron jobs are not triggered a second time on the site health page,
+ * - the loopback request finishes sooner providing a quicker result.
+ *
+ * Using a POST request causes the loopback to differ slightly to the standard
+ * GET request WordPress uses for wp-cron.php loopback requests but is close
+ * enough. See https://core.trac.wordpress.org/ticket/52547
+ */
+ $r = wp_remote_post( $url, compact( 'body', 'cookies', 'headers', 'timeout', 'sslverify' ) );
+
+ if ( is_wp_error( $r ) ) {
+ return (object) array(
+ 'status' => 'critical',
+ 'message' => sprintf(
+ '%s
%s',
+ __( 'The loopback request to your site failed, this means features relying on them are not currently working as expected.' ),
+ sprintf(
+ /* translators: 1: The WordPress error message. 2: The WordPress error code. */
+ __( 'Error: %1$s (%2$s)' ),
+ $r->get_error_message(),
+ $r->get_error_code()
+ )
+ ),
+ );
+ }
+
+ if ( 200 !== wp_remote_retrieve_response_code( $r ) ) {
+ return (object) array(
+ 'status' => 'recommended',
+ 'message' => sprintf(
+ /* translators: %d: The HTTP response code returned. */
+ __( 'The loopback request returned an unexpected http status code, %d, it was not possible to determine if this will prevent features from working as expected.' ),
+ wp_remote_retrieve_response_code( $r )
+ ),
+ );
+ }
+
+ return (object) array(
+ 'status' => 'good',
+ 'message' => __( 'The loopback request to your site completed successfully.' ),
+ );
+ }
+
+ /**
+ * Creates a weekly cron event, if one does not already exist.
+ *
+ * @since 5.4.0
+ */
+ public function maybe_create_scheduled_event() {
+ if ( ! wp_next_scheduled( 'wp_site_health_scheduled_check' ) && ! wp_installing() ) {
+ wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', 'wp_site_health_scheduled_check' );
+ }
+ }
+
+ /**
+ * Runs the scheduled event to check and update the latest site health status for the website.
+ *
+ * @since 5.4.0
+ */
+ public function wp_cron_scheduled_check() {
+ // Bootstrap wp-admin, as WP_Cron doesn't do this for us.
+ require_once trailingslashit( ABSPATH ) . 'wp-admin/includes/admin.php';
+
+ $tests = WP_Site_Health::get_tests();
+
+ $results = array();
+
+ $site_status = array(
+ 'good' => 0,
+ 'recommended' => 0,
+ 'critical' => 0,
+ );
+
+ // Don't run https test on development environments.
+ if ( $this->is_development_environment() ) {
+ unset( $tests['async']['https_status'] );
+ }
+
+ foreach ( $tests['direct'] as $test ) {
+ if ( ! empty( $test['skip_cron'] ) ) {
+ continue;
+ }
+
+ if ( is_string( $test['test'] ) ) {
+ $test_function = sprintf(
+ 'get_test_%s',
+ $test['test']
+ );
+
+ if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
+ $results[] = $this->perform_test( array( $this, $test_function ) );
+ continue;
+ }
+ }
+
+ if ( is_callable( $test['test'] ) ) {
+ $results[] = $this->perform_test( $test['test'] );
+ }
+ }
+
+ foreach ( $tests['async'] as $test ) {
+ if ( ! empty( $test['skip_cron'] ) ) {
+ continue;
+ }
+
+ // Local endpoints may require authentication, so asynchronous tests can pass a direct test runner as well.
+ if ( ! empty( $test['async_direct_test'] ) && is_callable( $test['async_direct_test'] ) ) {
+ // This test is callable, do so and continue to the next asynchronous check.
+ $results[] = $this->perform_test( $test['async_direct_test'] );
+ continue;
+ }
+
+ if ( is_string( $test['test'] ) ) {
+ // Check if this test has a REST API endpoint.
+ if ( isset( $test['has_rest'] ) && $test['has_rest'] ) {
+ $result_fetch = wp_remote_get(
+ $test['test'],
+ array(
+ 'body' => array(
+ '_wpnonce' => wp_create_nonce( 'wp_rest' ),
+ ),
+ )
+ );
+ } else {
+ $result_fetch = wp_remote_post(
+ admin_url( 'admin-ajax.php' ),
+ array(
+ 'body' => array(
+ 'action' => $test['test'],
+ '_wpnonce' => wp_create_nonce( 'health-check-site-status' ),
+ ),
+ )
+ );
+ }
+
+ if ( ! is_wp_error( $result_fetch ) && 200 === wp_remote_retrieve_response_code( $result_fetch ) ) {
+ $result = json_decode( wp_remote_retrieve_body( $result_fetch ), true );
+ } else {
+ $result = false;
+ }
+
+ if ( is_array( $result ) ) {
+ $results[] = $result;
+ } else {
+ $results[] = array(
+ 'status' => 'recommended',
+ 'label' => __( 'A test is unavailable' ),
+ );
+ }
+ }
+ }
+
+ foreach ( $results as $result ) {
+ if ( 'critical' === $result['status'] ) {
+ ++$site_status['critical'];
+ } elseif ( 'recommended' === $result['status'] ) {
+ ++$site_status['recommended'];
+ } else {
+ ++$site_status['good'];
+ }
+ }
+
+ set_transient( 'health-check-site-status-result', wp_json_encode( $site_status ) );
+ }
+
+ /**
+ * Checks if the current environment type is set to 'development' or 'local'.
+ *
+ * @since 5.6.0
+ *
+ * @return bool True if it is a development environment, false if not.
+ */
+ public function is_development_environment() {
+ return in_array( wp_get_environment_type(), array( 'development', 'local' ), true );
+ }
+
+ /**
+ * Returns a list of headers and its verification callback to verify if page cache is enabled or not.
+ *
+ * Note: key is header name and value could be callable function to verify header value.
+ * Empty value mean existence of header detect page cache is enabled.
+ *
+ * @since 6.1.0
+ *
+ * @return array List of client caching headers and their (optional) verification callbacks.
+ */
+ public function get_page_cache_headers() {
+
+ $cache_hit_callback = static function ( $header_value ) {
+ return str_contains( strtolower( $header_value ), 'hit' );
+ };
+
+ $cache_headers = array(
+ 'cache-control' => static function ( $header_value ) {
+ return (bool) preg_match( '/max-age=[1-9]/', $header_value );
+ },
+ 'expires' => static function ( $header_value ) {
+ return strtotime( $header_value ) > time();
+ },
+ 'age' => static function ( $header_value ) {
+ return is_numeric( $header_value ) && $header_value > 0;
+ },
+ 'last-modified' => '',
+ 'etag' => '',
+ 'x-cache-enabled' => static function ( $header_value ) {
+ return 'true' === strtolower( $header_value );
+ },
+ 'x-cache-disabled' => static function ( $header_value ) {
+ return ( 'on' !== strtolower( $header_value ) );
+ },
+ 'x-srcache-store-status' => $cache_hit_callback,
+ 'x-srcache-fetch-status' => $cache_hit_callback,
+ );
+
+ /**
+ * Filters the list of cache headers supported by core.
+ *
+ * @since 6.1.0
+ *
+ * @param array $cache_headers Array of supported cache headers.
+ */
+ return apply_filters( 'site_status_page_cache_supported_cache_headers', $cache_headers );
+ }
+
+ /**
+ * Checks if site has page cache enabled or not.
+ *
+ * @since 6.1.0
+ *
+ * @return WP_Error|array {
+ * Page cache detection details or else error information.
+ *
+ * @type bool $advanced_cache_present Whether a page cache plugin is present.
+ * @type array[] $page_caching_response_headers Sets of client caching headers for the responses.
+ * @type float[] $response_timing Response timings.
+ * }
+ */
+ private function check_for_page_caching() {
+
+ /** This filter is documented in wp-includes/class-wp-http-streams.php */
+ $sslverify = apply_filters( 'https_local_ssl_verify', false );
+
+ $headers = array();
+
+ /*
+ * Include basic auth in loopback requests. Note that this will only pass along basic auth when user is
+ * initiating the test. If a site requires basic auth, the test will fail when it runs in WP Cron as part of
+ * wp_site_health_scheduled_check. This logic is copied from WP_Site_Health::can_perform_loopback().
+ */
+ if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
+ $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
+ }
+
+ $caching_headers = $this->get_page_cache_headers();
+ $page_caching_response_headers = array();
+ $response_timing = array();
+
+ for ( $i = 1; $i <= 3; $i++ ) {
+ $start_time = microtime( true );
+ $http_response = wp_remote_get( home_url( '/' ), compact( 'sslverify', 'headers' ) );
+ $end_time = microtime( true );
+
+ if ( is_wp_error( $http_response ) ) {
+ return $http_response;
+ }
+ if ( wp_remote_retrieve_response_code( $http_response ) !== 200 ) {
+ return new WP_Error(
+ 'http_' . wp_remote_retrieve_response_code( $http_response ),
+ wp_remote_retrieve_response_message( $http_response )
+ );
+ }
+
+ $response_headers = array();
+
+ foreach ( $caching_headers as $header => $callback ) {
+ $header_values = wp_remote_retrieve_header( $http_response, $header );
+ if ( empty( $header_values ) ) {
+ continue;
+ }
+ $header_values = (array) $header_values;
+ if ( empty( $callback ) || ( is_callable( $callback ) && count( array_filter( $header_values, $callback ) ) > 0 ) ) {
+ $response_headers[ $header ] = $header_values;
+ }
+ }
+
+ $page_caching_response_headers[] = $response_headers;
+ $response_timing[] = ( $end_time - $start_time ) * 1000;
+ }
+
+ return array(
+ 'advanced_cache_present' => (
+ file_exists( WP_CONTENT_DIR . '/advanced-cache.php' )
+ &&
+ ( defined( 'WP_CACHE' ) && WP_CACHE )
+ &&
+ /** This filter is documented in wp-settings.php */
+ apply_filters( 'enable_loading_advanced_cache_dropin', true )
+ ),
+ 'page_caching_response_headers' => $page_caching_response_headers,
+ 'response_timing' => $response_timing,
+ );
+ }
+
+ /**
+ * Gets page cache details.
+ *
+ * @since 6.1.0
+ *
+ * @return WP_Error|array {
+ * Page cache detail or else a WP_Error if unable to determine.
+ *
+ * @type string $status Page cache status. Good, Recommended or Critical.
+ * @type bool $advanced_cache_present Whether page cache plugin is available or not.
+ * @type string[] $headers Client caching response headers detected.
+ * @type float $response_time Response time of site.
+ * }
+ */
+ private function get_page_cache_detail() {
+ $page_cache_detail = $this->check_for_page_caching();
+ if ( is_wp_error( $page_cache_detail ) ) {
+ return $page_cache_detail;
+ }
+
+ // Use the median server response time.
+ $response_timings = $page_cache_detail['response_timing'];
+ rsort( $response_timings );
+ $page_speed = $response_timings[ floor( count( $response_timings ) / 2 ) ];
+
+ // Obtain unique set of all client caching response headers.
+ $headers = array();
+ foreach ( $page_cache_detail['page_caching_response_headers'] as $page_caching_response_headers ) {
+ $headers = array_merge( $headers, array_keys( $page_caching_response_headers ) );
+ }
+ $headers = array_unique( $headers );
+
+ // Page cache is detected if there are response headers or a page cache plugin is present.
+ $has_page_caching = ( count( $headers ) > 0 || $page_cache_detail['advanced_cache_present'] );
+
+ if ( $page_speed && $page_speed < $this->get_good_response_time_threshold() ) {
+ $result = $has_page_caching ? 'good' : 'recommended';
+ } else {
+ $result = 'critical';
+ }
+
+ return array(
+ 'status' => $result,
+ 'advanced_cache_present' => $page_cache_detail['advanced_cache_present'],
+ 'headers' => $headers,
+ 'response_time' => $page_speed,
+ );
+ }
+
+ /**
+ * Gets the threshold below which a response time is considered good.
+ *
+ * @since 6.1.0
+ *
+ * @return int Threshold in milliseconds.
+ */
+ private function get_good_response_time_threshold() {
+ /**
+ * Filters the threshold below which a response time is considered good.
+ *
+ * The default is based on https://web.dev/time-to-first-byte/.
+ *
+ * @param int $threshold Threshold in milliseconds. Default 600.
+ *
+ * @since 6.1.0
+ */
+ return (int) apply_filters( 'site_status_good_response_time_threshold', 600 );
+ }
+
+ /**
+ * Determines whether to suggest using a persistent object cache.
+ *
+ * @since 6.1.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @return bool Whether to suggest using a persistent object cache.
+ */
+ public function should_suggest_persistent_object_cache() {
+ global $wpdb;
+
+ /**
+ * Filters whether to suggest use of a persistent object cache and bypass default threshold checks.
+ *
+ * Using this filter allows to override the default logic, effectively short-circuiting the method.
+ *
+ * @since 6.1.0
+ *
+ * @param bool|null $suggest Boolean to short-circuit, for whether to suggest using a persistent object cache.
+ * Default null.
+ */
+ $short_circuit = apply_filters( 'site_status_should_suggest_persistent_object_cache', null );
+ if ( is_bool( $short_circuit ) ) {
+ return $short_circuit;
+ }
+
+ if ( is_multisite() ) {
+ return true;
+ }
+
+ /**
+ * Filters the thresholds used to determine whether to suggest the use of a persistent object cache.
+ *
+ * @since 6.1.0
+ *
+ * @param int[] $thresholds The list of threshold numbers keyed by threshold name.
+ */
+ $thresholds = apply_filters(
+ 'site_status_persistent_object_cache_thresholds',
+ array(
+ 'alloptions_count' => 500,
+ 'alloptions_bytes' => 100000,
+ 'comments_count' => 1000,
+ 'options_count' => 1000,
+ 'posts_count' => 1000,
+ 'terms_count' => 1000,
+ 'users_count' => 1000,
+ )
+ );
+
+ $alloptions = wp_load_alloptions();
+
+ if ( $thresholds['alloptions_count'] < count( $alloptions ) ) {
+ return true;
+ }
+
+ if ( $thresholds['alloptions_bytes'] < strlen( serialize( $alloptions ) ) ) {
+ return true;
+ }
+
+ $table_names = implode( "','", array( $wpdb->comments, $wpdb->options, $wpdb->posts, $wpdb->terms, $wpdb->users ) );
+
+ // With InnoDB the `TABLE_ROWS` are estimates, which are accurate enough and faster to retrieve than individual `COUNT()` queries.
+ $results = $wpdb->get_results(
+ $wpdb->prepare(
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- This query cannot use interpolation.
+ "SELECT TABLE_NAME AS 'table', TABLE_ROWS AS 'rows', SUM(data_length + index_length) as 'bytes' FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME IN ('$table_names') GROUP BY TABLE_NAME;",
+ DB_NAME
+ ),
+ OBJECT_K
+ );
+
+ $threshold_map = array(
+ 'comments_count' => $wpdb->comments,
+ 'options_count' => $wpdb->options,
+ 'posts_count' => $wpdb->posts,
+ 'terms_count' => $wpdb->terms,
+ 'users_count' => $wpdb->users,
+ );
+
+ foreach ( $threshold_map as $threshold => $table ) {
+ if ( $thresholds[ $threshold ] <= $results[ $table ]->rows ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns a list of available persistent object cache services.
+ *
+ * @since 6.1.0
+ *
+ * @return string[] The list of available persistent object cache services.
+ */
+ private function available_object_cache_services() {
+ $extensions = array_map(
+ 'extension_loaded',
+ array(
+ 'APCu' => 'apcu',
+ 'Redis' => 'redis',
+ 'Relay' => 'relay',
+ 'Memcache' => 'memcache',
+ 'Memcached' => 'memcached',
+ )
+ );
+
+ $services = array_keys( array_filter( $extensions ) );
+
+ /**
+ * Filters the persistent object cache services available to the user.
+ *
+ * This can be useful to hide or add services not included in the defaults.
+ *
+ * @since 6.1.0
+ *
+ * @param string[] $services The list of available persistent object cache services.
+ */
+ return apply_filters( 'site_status_available_object_cache_services', $services );
+ }
+}
diff --git a/wp-admin/includes/class-wp-site-icon.php b/wp-admin/includes/class-wp-site-icon.php
new file mode 100644
index 0000000..ff41771
--- /dev/null
+++ b/wp-admin/includes/class-wp-site-icon.php
@@ -0,0 +1,234 @@
+ID );
+ $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
+
+ $size = wp_getimagesize( $cropped );
+ $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
+
+ $attachment = array(
+ 'ID' => $parent_attachment_id,
+ 'post_title' => wp_basename( $cropped ),
+ 'post_content' => $url,
+ 'post_mime_type' => $image_type,
+ 'guid' => $url,
+ 'context' => 'site-icon',
+ );
+
+ return $attachment;
+ }
+
+ /**
+ * Inserts an attachment.
+ *
+ * @since 4.3.0
+ *
+ * @param array $attachment An array with attachment object data.
+ * @param string $file File path of the attached image.
+ * @return int Attachment ID.
+ */
+ public function insert_attachment( $attachment, $file ) {
+ $attachment_id = wp_insert_attachment( $attachment, $file );
+ $metadata = wp_generate_attachment_metadata( $attachment_id, $file );
+
+ /**
+ * Filters the site icon attachment metadata.
+ *
+ * @since 4.3.0
+ *
+ * @see wp_generate_attachment_metadata()
+ *
+ * @param array $metadata Attachment metadata.
+ */
+ $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
+ wp_update_attachment_metadata( $attachment_id, $metadata );
+
+ return $attachment_id;
+ }
+
+ /**
+ * Adds additional sizes to be made when creating the site icon images.
+ *
+ * @since 4.3.0
+ *
+ * @param array[] $sizes Array of arrays containing information for additional sizes.
+ * @return array[] Array of arrays containing additional image sizes.
+ */
+ public function additional_sizes( $sizes = array() ) {
+ $only_crop_sizes = array();
+
+ /**
+ * Filters the different dimensions that a site icon is saved in.
+ *
+ * @since 4.3.0
+ *
+ * @param int[] $site_icon_sizes Array of sizes available for the Site Icon.
+ */
+ $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
+
+ // Use a natural sort of numbers.
+ natsort( $this->site_icon_sizes );
+ $this->site_icon_sizes = array_reverse( $this->site_icon_sizes );
+
+ // Ensure that we only resize the image into sizes that allow cropping.
+ foreach ( $sizes as $name => $size_array ) {
+ if ( isset( $size_array['crop'] ) ) {
+ $only_crop_sizes[ $name ] = $size_array;
+ }
+ }
+
+ foreach ( $this->site_icon_sizes as $size ) {
+ if ( $size < $this->min_size ) {
+ $only_crop_sizes[ 'site_icon-' . $size ] = array(
+ 'width ' => $size,
+ 'height' => $size,
+ 'crop' => true,
+ );
+ }
+ }
+
+ return $only_crop_sizes;
+ }
+
+ /**
+ * Adds Site Icon sizes to the array of image sizes on demand.
+ *
+ * @since 4.3.0
+ *
+ * @param string[] $sizes Array of image size names.
+ * @return string[] Array of image size names.
+ */
+ public function intermediate_image_sizes( $sizes = array() ) {
+ /** This filter is documented in wp-admin/includes/class-wp-site-icon.php */
+ $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
+ foreach ( $this->site_icon_sizes as $size ) {
+ $sizes[] = 'site_icon-' . $size;
+ }
+
+ return $sizes;
+ }
+
+ /**
+ * Deletes the Site Icon when the image file is deleted.
+ *
+ * @since 4.3.0
+ *
+ * @param int $post_id Attachment ID.
+ */
+ public function delete_attachment_data( $post_id ) {
+ $site_icon_id = (int) get_option( 'site_icon' );
+
+ if ( $site_icon_id && $post_id === $site_icon_id ) {
+ delete_option( 'site_icon' );
+ }
+ }
+
+ /**
+ * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
+ *
+ * @since 4.3.0
+ *
+ * @param null|array|string $value The value get_metadata() should return a single metadata value, or an
+ * array of values.
+ * @param int $post_id Post ID.
+ * @param string $meta_key Meta key.
+ * @param bool $single Whether to return only the first value of the specified `$meta_key`.
+ * @return array|null|string The attachment metadata value, array of values, or null.
+ */
+ public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
+ if ( $single && '_wp_attachment_backup_sizes' === $meta_key ) {
+ $site_icon_id = (int) get_option( 'site_icon' );
+
+ if ( $post_id === $site_icon_id ) {
+ add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
+ }
+ }
+
+ return $value;
+ }
+}
diff --git a/wp-admin/includes/class-wp-terms-list-table.php b/wp-admin/includes/class-wp-terms-list-table.php
new file mode 100644
index 0000000..b3d9ec5
--- /dev/null
+++ b/wp-admin/includes/class-wp-terms-list-table.php
@@ -0,0 +1,755 @@
+ 'tags',
+ 'singular' => 'tag',
+ 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
+ )
+ );
+
+ $action = $this->screen->action;
+ $post_type = $this->screen->post_type;
+ $taxonomy = $this->screen->taxonomy;
+
+ if ( empty( $taxonomy ) ) {
+ $taxonomy = 'post_tag';
+ }
+
+ if ( ! taxonomy_exists( $taxonomy ) ) {
+ wp_die( __( 'Invalid taxonomy.' ) );
+ }
+
+ $tax = get_taxonomy( $taxonomy );
+
+ // @todo Still needed? Maybe just the show_ui part.
+ if ( empty( $post_type ) || ! in_array( $post_type, get_post_types( array( 'show_ui' => true ) ), true ) ) {
+ $post_type = 'post';
+ }
+ }
+
+ /**
+ * @return bool
+ */
+ public function ajax_user_can() {
+ return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms );
+ }
+
+ /**
+ */
+ public function prepare_items() {
+ $taxonomy = $this->screen->taxonomy;
+
+ $tags_per_page = $this->get_items_per_page( "edit_{$taxonomy}_per_page" );
+
+ if ( 'post_tag' === $taxonomy ) {
+ /**
+ * Filters the number of terms displayed per page for the Tags list table.
+ *
+ * @since 2.8.0
+ *
+ * @param int $tags_per_page Number of tags to be displayed. Default 20.
+ */
+ $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
+
+ /**
+ * Filters the number of terms displayed per page for the Tags list table.
+ *
+ * @since 2.7.0
+ * @deprecated 2.8.0 Use {@see 'edit_tags_per_page'} instead.
+ *
+ * @param int $tags_per_page Number of tags to be displayed. Default 20.
+ */
+ $tags_per_page = apply_filters_deprecated( 'tagsperpage', array( $tags_per_page ), '2.8.0', 'edit_tags_per_page' );
+ } elseif ( 'category' === $taxonomy ) {
+ /**
+ * Filters the number of terms displayed per page for the Categories list table.
+ *
+ * @since 2.8.0
+ *
+ * @param int $tags_per_page Number of categories to be displayed. Default 20.
+ */
+ $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page );
+ }
+
+ $search = ! empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
+
+ $args = array(
+ 'taxonomy' => $taxonomy,
+ 'search' => $search,
+ 'page' => $this->get_pagenum(),
+ 'number' => $tags_per_page,
+ 'hide_empty' => 0,
+ );
+
+ if ( ! empty( $_REQUEST['orderby'] ) ) {
+ $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );
+ }
+
+ if ( ! empty( $_REQUEST['order'] ) ) {
+ $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) );
+ }
+
+ $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
+
+ // Save the values because 'number' and 'offset' can be subsequently overridden.
+ $this->callback_args = $args;
+
+ if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
+ // We'll need the full set of terms then.
+ $args['number'] = 0;
+ $args['offset'] = $args['number'];
+ }
+
+ $this->items = get_terms( $args );
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => wp_count_terms(
+ array(
+ 'taxonomy' => $taxonomy,
+ 'search' => $search,
+ )
+ ),
+ 'per_page' => $tags_per_page,
+ )
+ );
+ }
+
+ /**
+ */
+ public function no_items() {
+ echo get_taxonomy( $this->screen->taxonomy )->labels->not_found;
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_bulk_actions() {
+ $actions = array();
+
+ if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) ) {
+ $actions['delete'] = __( 'Delete' );
+ }
+
+ return $actions;
+ }
+
+ /**
+ * @return string
+ */
+ public function current_action() {
+ if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && 'delete' === $_REQUEST['action'] ) {
+ return 'bulk-delete';
+ }
+
+ return parent::current_action();
+ }
+
+ /**
+ * @return string[] Array of column titles keyed by their column name.
+ */
+ public function get_columns() {
+ $columns = array(
+ 'cb' => '
',
+ 'name' => _x( 'Name', 'term name' ),
+ 'description' => __( 'Description' ),
+ 'slug' => __( 'Slug' ),
+ );
+
+ if ( 'link_category' === $this->screen->taxonomy ) {
+ $columns['links'] = __( 'Links' );
+ } else {
+ $columns['posts'] = _x( 'Count', 'Number/count of items' );
+ }
+
+ return $columns;
+ }
+
+ /**
+ * @return array
+ */
+ protected function get_sortable_columns() {
+ $taxonomy = $this->screen->taxonomy;
+
+ if ( ! isset( $_GET['orderby'] ) && is_taxonomy_hierarchical( $taxonomy ) ) {
+ $name_orderby_text = __( 'Table ordered hierarchically.' );
+ } else {
+ $name_orderby_text = __( 'Table ordered by Name.' );
+ }
+
+ return array(
+ 'name' => array( 'name', false, _x( 'Name', 'term name' ), $name_orderby_text, 'asc' ),
+ 'description' => array( 'description', false, __( 'Description' ), __( 'Table ordered by Description.' ) ),
+ 'slug' => array( 'slug', false, __( 'Slug' ), __( 'Table ordered by Slug.' ) ),
+ 'posts' => array( 'count', false, _x( 'Count', 'Number/count of items' ), __( 'Table ordered by Posts Count.' ) ),
+ 'links' => array( 'count', false, __( 'Links' ), __( 'Table ordered by Links.' ) ),
+ );
+ }
+
+ /**
+ */
+ public function display_rows_or_placeholder() {
+ $taxonomy = $this->screen->taxonomy;
+
+ $number = $this->callback_args['number'];
+ $offset = $this->callback_args['offset'];
+
+ // Convert it to table rows.
+ $count = 0;
+
+ if ( empty( $this->items ) || ! is_array( $this->items ) ) {
+ echo '
';
+ $this->no_items();
+ echo ' ';
+ return;
+ }
+
+ if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $this->callback_args['orderby'] ) ) {
+ if ( ! empty( $this->callback_args['search'] ) ) {// Ignore children on searches.
+ $children = array();
+ } else {
+ $children = _get_term_hierarchy( $taxonomy );
+ }
+
+ /*
+ * Some funky recursion to get the job done (paging & parents mainly) is contained within.
+ * Skip it for non-hierarchical taxonomies for performance sake.
+ */
+ $this->_rows( $taxonomy, $this->items, $children, $offset, $number, $count );
+ } else {
+ foreach ( $this->items as $term ) {
+ $this->single_row( $term );
+ }
+ }
+ }
+
+ /**
+ * @param string $taxonomy
+ * @param array $terms
+ * @param array $children
+ * @param int $start
+ * @param int $per_page
+ * @param int $count
+ * @param int $parent_term
+ * @param int $level
+ */
+ private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent_term = 0, $level = 0 ) {
+
+ $end = $start + $per_page;
+
+ foreach ( $terms as $key => $term ) {
+
+ if ( $count >= $end ) {
+ break;
+ }
+
+ if ( $term->parent !== $parent_term && empty( $_REQUEST['s'] ) ) {
+ continue;
+ }
+
+ // If the page starts in a subtree, print the parents.
+ if ( $count === $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
+ $my_parents = array();
+ $parent_ids = array();
+ $p = $term->parent;
+
+ while ( $p ) {
+ $my_parent = get_term( $p, $taxonomy );
+ $my_parents[] = $my_parent;
+ $p = $my_parent->parent;
+
+ if ( in_array( $p, $parent_ids, true ) ) { // Prevent parent loops.
+ break;
+ }
+
+ $parent_ids[] = $p;
+ }
+
+ unset( $parent_ids );
+
+ $num_parents = count( $my_parents );
+
+ while ( $my_parent = array_pop( $my_parents ) ) {
+ echo "\t";
+ $this->single_row( $my_parent, $level - $num_parents );
+ --$num_parents;
+ }
+ }
+
+ if ( $count >= $start ) {
+ echo "\t";
+ $this->single_row( $term, $level );
+ }
+
+ ++$count;
+
+ unset( $terms[ $key ] );
+
+ if ( isset( $children[ $term->term_id ] ) && empty( $_REQUEST['s'] ) ) {
+ $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
+ }
+ }
+ }
+
+ /**
+ * @global string $taxonomy
+ * @param WP_Term $tag Term object.
+ * @param int $level
+ */
+ public function single_row( $tag, $level = 0 ) {
+ global $taxonomy;
+ $tag = sanitize_term( $tag, $taxonomy );
+
+ $this->level = $level;
+
+ if ( $tag->parent ) {
+ $count = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) );
+ $level = 'level-' . $count;
+ } else {
+ $level = 'level-0';
+ }
+
+ echo '
';
+ $this->single_row_columns( $tag );
+ echo ' ';
+ }
+
+ /**
+ * @since 5.9.0 Renamed `$tag` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Term $item Term object.
+ * @return string
+ */
+ public function column_cb( $item ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $tag = $item;
+
+ if ( current_user_can( 'delete_term', $tag->term_id ) ) {
+ return sprintf(
+ '
' .
+ '
%2$s ',
+ $tag->term_id,
+ /* translators: Hidden accessibility text. %s: Taxonomy term name. */
+ sprintf( __( 'Select %s' ), $tag->name )
+ );
+ }
+
+ return ' ';
+ }
+
+ /**
+ * @param WP_Term $tag Term object.
+ * @return string
+ */
+ public function column_name( $tag ) {
+ $taxonomy = $this->screen->taxonomy;
+
+ $pad = str_repeat( '— ', max( 0, $this->level ) );
+
+ /**
+ * Filters display of the term name in the terms list table.
+ *
+ * The default output may include padding due to the term's
+ * current level in the term hierarchy.
+ *
+ * @since 2.5.0
+ *
+ * @see WP_Terms_List_Table::column_name()
+ *
+ * @param string $pad_tag_name The term name, padded if not top-level.
+ * @param WP_Term $tag Term object.
+ */
+ $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
+
+ $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
+
+ $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
+
+ $edit_link = get_edit_term_link( $tag, $taxonomy, $this->screen->post_type );
+
+ if ( $edit_link ) {
+ $edit_link = add_query_arg(
+ 'wp_http_referer',
+ urlencode( wp_unslash( $uri ) ),
+ $edit_link
+ );
+ $name = sprintf(
+ '
%s ',
+ esc_url( $edit_link ),
+ /* translators: %s: Taxonomy term name. */
+ esc_attr( sprintf( __( '“%s” (Edit)' ), $tag->name ) ),
+ $name
+ );
+ }
+
+ $output = sprintf(
+ '
%s ',
+ $name
+ );
+
+ /** This filter is documented in wp-admin/includes/class-wp-terms-list-table.php */
+ $quick_edit_enabled = apply_filters( 'quick_edit_enabled_for_taxonomy', true, $taxonomy );
+
+ if ( $quick_edit_enabled ) {
+ $output .= '
';
+ $output .= '
' . $qe_data->name . '
';
+
+ /** This filter is documented in wp-admin/edit-tag-form.php */
+ $output .= '
' . apply_filters( 'editable_slug', $qe_data->slug, $qe_data ) . '
';
+ $output .= '
' . $qe_data->parent . '
';
+ }
+
+ return $output;
+ }
+
+ /**
+ * Gets the name of the default primary column.
+ *
+ * @since 4.3.0
+ *
+ * @return string Name of the default primary column, in this case, 'name'.
+ */
+ protected function get_default_primary_column_name() {
+ return 'name';
+ }
+
+ /**
+ * Generates and displays row action links.
+ *
+ * @since 4.3.0
+ * @since 5.9.0 Renamed `$tag` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Term $item Tag being acted upon.
+ * @param string $column_name Current column name.
+ * @param string $primary Primary column name.
+ * @return string Row actions output for terms, or an empty string
+ * if the current column is not the primary column.
+ */
+ protected function handle_row_actions( $item, $column_name, $primary ) {
+ if ( $primary !== $column_name ) {
+ return '';
+ }
+
+ // Restores the more descriptive, specific name for use within this method.
+ $tag = $item;
+
+ $taxonomy = $this->screen->taxonomy;
+ $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
+
+ $actions = array();
+
+ if ( current_user_can( 'edit_term', $tag->term_id ) ) {
+ $actions['edit'] = sprintf(
+ '
%s ',
+ esc_url(
+ add_query_arg(
+ 'wp_http_referer',
+ urlencode( wp_unslash( $uri ) ),
+ get_edit_term_link( $tag, $taxonomy, $this->screen->post_type )
+ )
+ ),
+ /* translators: %s: Taxonomy term name. */
+ esc_attr( sprintf( __( 'Edit “%s”' ), $tag->name ) ),
+ __( 'Edit' )
+ );
+
+ /**
+ * Filters whether Quick Edit should be enabled for the given taxonomy.
+ *
+ * @since 6.4.0
+ *
+ * @param bool $enable Whether to enable the Quick Edit functionality. Default true.
+ * @param string $taxonomy Taxonomy name.
+ */
+ $quick_edit_enabled = apply_filters( 'quick_edit_enabled_for_taxonomy', true, $taxonomy );
+
+ if ( $quick_edit_enabled ) {
+ $actions['inline hide-if-no-js'] = sprintf(
+ '
%s ',
+ /* translators: %s: Taxonomy term name. */
+ esc_attr( sprintf( __( 'Quick edit “%s” inline' ), $tag->name ) ),
+ __( 'Quick Edit' )
+ );
+ }
+ }
+
+ if ( current_user_can( 'delete_term', $tag->term_id ) ) {
+ $actions['delete'] = sprintf(
+ '
%s ',
+ wp_nonce_url( "edit-tags.php?action=delete&taxonomy=$taxonomy&tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ),
+ /* translators: %s: Taxonomy term name. */
+ esc_attr( sprintf( __( 'Delete “%s”' ), $tag->name ) ),
+ __( 'Delete' )
+ );
+ }
+
+ if ( is_term_publicly_viewable( $tag ) ) {
+ $actions['view'] = sprintf(
+ '
%s ',
+ get_term_link( $tag ),
+ /* translators: %s: Taxonomy term name. */
+ esc_attr( sprintf( __( 'View “%s” archive' ), $tag->name ) ),
+ __( 'View' )
+ );
+ }
+
+ /**
+ * Filters the action links displayed for each term in the Tags list table.
+ *
+ * @since 2.8.0
+ * @since 3.0.0 Deprecated in favor of {@see '{$taxonomy}_row_actions'} filter.
+ * @since 5.4.2 Restored (un-deprecated).
+ *
+ * @param string[] $actions An array of action links to be displayed. Default
+ * 'Edit', 'Quick Edit', 'Delete', and 'View'.
+ * @param WP_Term $tag Term object.
+ */
+ $actions = apply_filters( 'tag_row_actions', $actions, $tag );
+
+ /**
+ * Filters the action links displayed for each term in the terms list table.
+ *
+ * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
+ *
+ * Possible hook names include:
+ *
+ * - `category_row_actions`
+ * - `post_tag_row_actions`
+ *
+ * @since 3.0.0
+ *
+ * @param string[] $actions An array of action links to be displayed. Default
+ * 'Edit', 'Quick Edit', 'Delete', and 'View'.
+ * @param WP_Term $tag Term object.
+ */
+ $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
+
+ return $this->row_actions( $actions );
+ }
+
+ /**
+ * @param WP_Term $tag Term object.
+ * @return string
+ */
+ public function column_description( $tag ) {
+ if ( $tag->description ) {
+ return $tag->description;
+ } else {
+ return '
— ' .
+ /* translators: Hidden accessibility text. */
+ __( 'No description' ) .
+ ' ';
+ }
+ }
+
+ /**
+ * @param WP_Term $tag Term object.
+ * @return string
+ */
+ public function column_slug( $tag ) {
+ /** This filter is documented in wp-admin/edit-tag-form.php */
+ return apply_filters( 'editable_slug', $tag->slug, $tag );
+ }
+
+ /**
+ * @param WP_Term $tag Term object.
+ * @return string
+ */
+ public function column_posts( $tag ) {
+ $count = number_format_i18n( $tag->count );
+
+ $tax = get_taxonomy( $this->screen->taxonomy );
+
+ $ptype_object = get_post_type_object( $this->screen->post_type );
+ if ( ! $ptype_object->show_ui ) {
+ return $count;
+ }
+
+ if ( $tax->query_var ) {
+ $args = array( $tax->query_var => $tag->slug );
+ } else {
+ $args = array(
+ 'taxonomy' => $tax->name,
+ 'term' => $tag->slug,
+ );
+ }
+
+ if ( 'post' !== $this->screen->post_type ) {
+ $args['post_type'] = $this->screen->post_type;
+ }
+
+ if ( 'attachment' === $this->screen->post_type ) {
+ return "
$count ";
+ }
+
+ return "
$count ";
+ }
+
+ /**
+ * @param WP_Term $tag Term object.
+ * @return string
+ */
+ public function column_links( $tag ) {
+ $count = number_format_i18n( $tag->count );
+
+ if ( $count ) {
+ $count = "
$count ";
+ }
+
+ return $count;
+ }
+
+ /**
+ * @since 5.9.0 Renamed `$tag` to `$item` to match parent class for PHP 8 named parameter support.
+ *
+ * @param WP_Term $item Term object.
+ * @param string $column_name Name of the column.
+ * @return string
+ */
+ public function column_default( $item, $column_name ) {
+ // Restores the more descriptive, specific name for use within this method.
+ $tag = $item;
+
+ /**
+ * Filters the displayed columns in the terms list table.
+ *
+ * The dynamic portion of the hook name, `$this->screen->taxonomy`,
+ * refers to the slug of the current taxonomy.
+ *
+ * Possible hook names include:
+ *
+ * - `manage_category_custom_column`
+ * - `manage_post_tag_custom_column`
+ *
+ * @since 2.8.0
+ *
+ * @param string $string Custom column output. Default empty.
+ * @param string $column_name Name of the column.
+ * @param int $term_id Term ID.
+ */
+ return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
+ }
+
+ /**
+ * Outputs the hidden row displayed when inline editing
+ *
+ * @since 3.1.0
+ */
+ public function inline_edit() {
+ $tax = get_taxonomy( $this->screen->taxonomy );
+
+ if ( ! current_user_can( $tax->cap->edit_terms ) ) {
+ return;
+ }
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true,
+ 'description' => true,
+ 'name' => true,
+ 'slug' => true,
+ 'posts' => true,
+ );
+
+ list( $columns ) = $this->get_column_info();
+
+ foreach ( $columns as $column_name => $column_display_name ) {
+ if ( isset( $core_columns[ $column_name ] ) ) {
+ continue;
+ }
+
+ /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
+ do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
+ }
+ ?>
+
+
+ labels->update_item; ?>
+
+
+
+
+
+
+
+ ',
+ array(
+ 'type' => 'error',
+ 'additional_classes' => array( 'notice-alt', 'inline', 'hidden' ),
+ 'paragraph_wrap' => false,
+ )
+ );
+ ?>
+
+
+
+
+
+
+
+ features = $_REQUEST['features'];
+ }
+
+ $paged = $this->get_pagenum();
+
+ $per_page = 36;
+
+ // These are the tabs which are shown on the page,
+ $tabs = array();
+ $tabs['dashboard'] = __( 'Search' );
+ if ( 'search' === $tab ) {
+ $tabs['search'] = __( 'Search Results' );
+ }
+ $tabs['upload'] = __( 'Upload' );
+ $tabs['featured'] = _x( 'Featured', 'themes' );
+ //$tabs['popular'] = _x( 'Popular', 'themes' );
+ $tabs['new'] = _x( 'Latest', 'themes' );
+ $tabs['updated'] = _x( 'Recently Updated', 'themes' );
+
+ $nonmenu_tabs = array( 'theme-information' ); // Valid actions to perform which do not have a Menu item.
+
+ /** This filter is documented in wp-admin/theme-install.php */
+ $tabs = apply_filters( 'install_themes_tabs', $tabs );
+
+ /**
+ * Filters tabs not associated with a menu item on the Install Themes screen.
+ *
+ * @since 2.8.0
+ *
+ * @param string[] $nonmenu_tabs The tabs that don't have a menu item on
+ * the Install Themes screen.
+ */
+ $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
+
+ // If a non-valid menu tab has been selected, And it's not a non-menu action.
+ if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs, true ) ) ) {
+ $tab = key( $tabs );
+ }
+
+ $args = array(
+ 'page' => $paged,
+ 'per_page' => $per_page,
+ 'fields' => $theme_field_defaults,
+ );
+
+ switch ( $tab ) {
+ case 'search':
+ $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
+ switch ( $type ) {
+ case 'tag':
+ $args['tag'] = array_map( 'sanitize_key', $search_terms );
+ break;
+ case 'term':
+ $args['search'] = $search_string;
+ break;
+ case 'author':
+ $args['author'] = $search_string;
+ break;
+ }
+
+ if ( ! empty( $this->features ) ) {
+ $args['tag'] = $this->features;
+ $_REQUEST['s'] = implode( ',', $this->features );
+ $_REQUEST['type'] = 'tag';
+ }
+
+ add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
+ break;
+
+ case 'featured':
+ // case 'popular':
+ case 'new':
+ case 'updated':
+ $args['browse'] = $tab;
+ break;
+
+ default:
+ $args = false;
+ break;
+ }
+
+ /**
+ * Filters API request arguments for each Install Themes screen tab.
+ *
+ * The dynamic portion of the hook name, `$tab`, refers to the theme install
+ * tab.
+ *
+ * Possible hook names include:
+ *
+ * - `install_themes_table_api_args_dashboard`
+ * - `install_themes_table_api_args_featured`
+ * - `install_themes_table_api_args_new`
+ * - `install_themes_table_api_args_search`
+ * - `install_themes_table_api_args_updated`
+ * - `install_themes_table_api_args_upload`
+ *
+ * @since 3.7.0
+ *
+ * @param array|false $args Theme install API arguments.
+ */
+ $args = apply_filters( "install_themes_table_api_args_{$tab}", $args );
+
+ if ( ! $args ) {
+ return;
+ }
+
+ $api = themes_api( 'query_themes', $args );
+
+ if ( is_wp_error( $api ) ) {
+ wp_die( '
' . $api->get_error_message() . '
' . __( 'Try Again' ) . '
' );
+ }
+
+ $this->items = $api->themes;
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $api->info['results'],
+ 'per_page' => $args['per_page'],
+ 'infinite_scroll' => true,
+ )
+ );
+ }
+
+ /**
+ */
+ public function no_items() {
+ _e( 'No themes match your request.' );
+ }
+
+ /**
+ * @global array $tabs
+ * @global string $tab
+ * @return array
+ */
+ protected function get_views() {
+ global $tabs, $tab;
+
+ $display_tabs = array();
+ foreach ( (array) $tabs as $action => $text ) {
+ $display_tabs[ 'theme-install-' . $action ] = array(
+ 'url' => self_admin_url( 'theme-install.php?tab=' . $action ),
+ 'label' => $text,
+ 'current' => $action === $tab,
+ );
+ }
+
+ return $this->get_views_links( $display_tabs );
+ }
+
+ /**
+ * Displays the theme install table.
+ *
+ * Overrides the parent display() method to provide a different container.
+ *
+ * @since 3.1.0
+ */
+ public function display() {
+ wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
+ ?>
+
+
+
+
+ pagination( 'top' ); ?>
+
+
+
+
+ display_rows_or_placeholder(); ?>
+
+
+ tablenav( 'bottom' );
+ }
+
+ /**
+ */
+ public function display_rows() {
+ $themes = $this->items;
+ foreach ( $themes as $theme ) {
+ ?>
+
+ single_row( $theme );
+ ?>
+
+ theme_installer();
+ }
+
+ /**
+ * Prints a theme from the WordPress.org API.
+ *
+ * @since 3.1.0
+ *
+ * @global array $themes_allowedtags
+ *
+ * @param stdClass $theme {
+ * An object that contains theme data returned by the WordPress.org API.
+ *
+ * @type string $name Theme name, e.g. 'Twenty Twenty-One'.
+ * @type string $slug Theme slug, e.g. 'twentytwentyone'.
+ * @type string $version Theme version, e.g. '1.1'.
+ * @type string $author Theme author username, e.g. 'melchoyce'.
+ * @type string $preview_url Preview URL, e.g. 'https://2021.wordpress.net/'.
+ * @type string $screenshot_url Screenshot URL, e.g. 'https://wordpress.org/themes/twentytwentyone/'.
+ * @type float $rating Rating score.
+ * @type int $num_ratings The number of ratings.
+ * @type string $homepage Theme homepage, e.g. 'https://wordpress.org/themes/twentytwentyone/'.
+ * @type string $description Theme description.
+ * @type string $download_link Theme ZIP download URL.
+ * }
+ */
+ public function single_row( $theme ) {
+ global $themes_allowedtags;
+
+ if ( empty( $theme ) ) {
+ return;
+ }
+
+ $name = wp_kses( $theme->name, $themes_allowedtags );
+ $author = wp_kses( $theme->author, $themes_allowedtags );
+
+ /* translators: %s: Theme name. */
+ $preview_title = sprintf( __( 'Preview “%s”' ), $name );
+ $preview_url = add_query_arg(
+ array(
+ 'tab' => 'theme-information',
+ 'theme' => $theme->slug,
+ ),
+ self_admin_url( 'theme-install.php' )
+ );
+
+ $actions = array();
+
+ $install_url = add_query_arg(
+ array(
+ 'action' => 'install-theme',
+ 'theme' => $theme->slug,
+ ),
+ self_admin_url( 'update.php' )
+ );
+
+ $update_url = add_query_arg(
+ array(
+ 'action' => 'upgrade-theme',
+ 'theme' => $theme->slug,
+ ),
+ self_admin_url( 'update.php' )
+ );
+
+ $status = $this->_get_theme_status( $theme );
+
+ switch ( $status ) {
+ case 'update_available':
+ $actions[] = sprintf(
+ '
%s ',
+ esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
+ /* translators: %s: Theme version. */
+ esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
+ __( 'Update' )
+ );
+ break;
+ case 'newer_installed':
+ case 'latest_installed':
+ $actions[] = sprintf(
+ '
%s ',
+ esc_attr__( 'This theme is already installed and is up to date' ),
+ _x( 'Installed', 'theme' )
+ );
+ break;
+ case 'install':
+ default:
+ $actions[] = sprintf(
+ '
%s ',
+ esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
+ /* translators: %s: Theme name. */
+ esc_attr( sprintf( _x( 'Install %s', 'theme' ), $name ) ),
+ __( 'Install Now' )
+ );
+ break;
+ }
+
+ $actions[] = sprintf(
+ '
%s ',
+ esc_url( $preview_url ),
+ /* translators: %s: Theme name. */
+ esc_attr( sprintf( __( 'Preview %s' ), $name ) ),
+ __( 'Preview' )
+ );
+
+ /**
+ * Filters the install action links for a theme in the Install Themes list table.
+ *
+ * @since 3.4.0
+ *
+ * @param string[] $actions An array of theme action links. Defaults are
+ * links to Install Now, Preview, and Details.
+ * @param stdClass $theme An object that contains theme data returned by the
+ * WordPress.org API.
+ */
+ $actions = apply_filters( 'theme_install_actions', $actions, $theme );
+
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+ install_theme_info( $theme );
+ }
+
+ /**
+ * Prints the wrapper for the theme installer.
+ */
+ public function theme_installer() {
+ ?>
+
+
+
+ name, $themes_allowedtags );
+ $author = wp_kses( $theme->author, $themes_allowedtags );
+
+ $install_url = add_query_arg(
+ array(
+ 'action' => 'install-theme',
+ 'theme' => $theme->slug,
+ ),
+ self_admin_url( 'update.php' )
+ );
+
+ $update_url = add_query_arg(
+ array(
+ 'action' => 'upgrade-theme',
+ 'theme' => $theme->slug,
+ ),
+ self_admin_url( 'update.php' )
+ );
+
+ $status = $this->_get_theme_status( $theme );
+
+ ?>
+
+ %s',
+ esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
+ /* translators: %s: Theme version. */
+ esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
+ __( 'Update' )
+ );
+ break;
+ case 'newer_installed':
+ case 'latest_installed':
+ printf(
+ '
%s ',
+ esc_attr__( 'This theme is already installed and is up to date' ),
+ _x( 'Installed', 'theme' )
+ );
+ break;
+ case 'install':
+ default:
+ printf(
+ '
%s ',
+ esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
+ __( 'Install' )
+ );
+ break;
+ }
+ ?>
+
+
+
+
+ screenshot_url ) ) : ?>
+
+
+
+ $theme->rating,
+ 'type' => 'percent',
+ 'number' => $theme->num_ratings,
+ )
+ );
+ ?>
+
+
+ version, $themes_allowedtags ); ?>
+
+
+ description, $themes_allowedtags ); ?>
+
+
+
+
+ Install screen
+ * @global string $type Type of search.
+ *
+ * @param array $extra_args Unused.
+ */
+ public function _js_vars( $extra_args = array() ) {
+ global $tab, $type;
+ parent::_js_vars( compact( 'tab', 'type' ) );
+ }
+
+ /**
+ * Checks to see if the theme is already installed.
+ *
+ * @since 3.4.0
+ *
+ * @param stdClass $theme A WordPress.org Theme API object.
+ * @return string Theme status.
+ */
+ private function _get_theme_status( $theme ) {
+ $status = 'install';
+
+ $installed_theme = wp_get_theme( $theme->slug );
+ if ( $installed_theme->exists() ) {
+ if ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '=' ) ) {
+ $status = 'latest_installed';
+ } elseif ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '>' ) ) {
+ $status = 'newer_installed';
+ } else {
+ $status = 'update_available';
+ }
+ }
+
+ return $status;
+ }
+}
diff --git a/wp-admin/includes/class-wp-themes-list-table.php b/wp-admin/includes/class-wp-themes-list-table.php
new file mode 100644
index 0000000..8d385f9
--- /dev/null
+++ b/wp-admin/includes/class-wp-themes-list-table.php
@@ -0,0 +1,360 @@
+ true,
+ 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
+ )
+ );
+ }
+
+ /**
+ * @return bool
+ */
+ public function ajax_user_can() {
+ // Do not check edit_theme_options here. Ajax calls for available themes require switch_themes.
+ return current_user_can( 'switch_themes' );
+ }
+
+ /**
+ */
+ public function prepare_items() {
+ $themes = wp_get_themes( array( 'allowed' => true ) );
+
+ if ( ! empty( $_REQUEST['s'] ) ) {
+ $this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
+ }
+
+ if ( ! empty( $_REQUEST['features'] ) ) {
+ $this->features = $_REQUEST['features'];
+ }
+
+ if ( $this->search_terms || $this->features ) {
+ foreach ( $themes as $key => $theme ) {
+ if ( ! $this->search_theme( $theme ) ) {
+ unset( $themes[ $key ] );
+ }
+ }
+ }
+
+ unset( $themes[ get_option( 'stylesheet' ) ] );
+ WP_Theme::sort_by_name( $themes );
+
+ $per_page = 36;
+ $page = $this->get_pagenum();
+
+ $start = ( $page - 1 ) * $per_page;
+
+ $this->items = array_slice( $themes, $start, $per_page, true );
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => count( $themes ),
+ 'per_page' => $per_page,
+ 'infinite_scroll' => true,
+ )
+ );
+ }
+
+ /**
+ */
+ public function no_items() {
+ if ( $this->search_terms || $this->features ) {
+ _e( 'No items found.' );
+ return;
+ }
+
+ $blog_id = get_current_blog_id();
+ if ( is_multisite() ) {
+ if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
+ printf(
+ /* translators: 1: URL to Themes tab on Edit Site screen, 2: URL to Add Themes screen. */
+ __( 'You only have one theme enabled for this site right now. Visit the Network Admin to
enable or
install more themes.' ),
+ network_admin_url( 'site-themes.php?id=' . $blog_id ),
+ network_admin_url( 'theme-install.php' )
+ );
+
+ return;
+ } elseif ( current_user_can( 'manage_network_themes' ) ) {
+ printf(
+ /* translators: %s: URL to Themes tab on Edit Site screen. */
+ __( 'You only have one theme enabled for this site right now. Visit the Network Admin to
enable more themes.' ),
+ network_admin_url( 'site-themes.php?id=' . $blog_id )
+ );
+
+ return;
+ }
+ // Else, fallthrough. install_themes doesn't help if you can't enable it.
+ } else {
+ if ( current_user_can( 'install_themes' ) ) {
+ printf(
+ /* translators: %s: URL to Add Themes screen. */
+ __( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress Theme Directory at any time: just click on the
Install Themes tab above.' ),
+ admin_url( 'theme-install.php' )
+ );
+
+ return;
+ }
+ }
+ // Fallthrough.
+ printf(
+ /* translators: %s: Network title. */
+ __( 'Only the active theme is available to you. Contact the %s administrator for information about accessing additional themes.' ),
+ get_site_option( 'site_name' )
+ );
+ }
+
+ /**
+ * @param string $which
+ */
+ public function tablenav( $which = 'top' ) {
+ if ( $this->get_pagination_arg( 'total_pages' ) <= 1 ) {
+ return;
+ }
+ ?>
+
+ pagination( $which ); ?>
+
+
+
+
+ tablenav( 'top' ); ?>
+
+
+ display_rows_or_placeholder(); ?>
+
+
+ tablenav( 'bottom' ); ?>
+ has_items() ) {
+ $this->display_rows();
+ } else {
+ echo '
';
+ $this->no_items();
+ echo '
';
+ }
+ }
+
+ /**
+ */
+ public function display_rows() {
+ $themes = $this->items;
+
+ foreach ( $themes as $theme ) :
+ ?>
+
+ get_template();
+ $stylesheet = $theme->get_stylesheet();
+ $title = $theme->display( 'Name' );
+ $version = $theme->display( 'Version' );
+ $author = $theme->display( 'Author' );
+
+ $activate_link = wp_nonce_url( 'themes.php?action=activate&template=' . urlencode( $template ) . '&stylesheet=' . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
+
+ $actions = array();
+ $actions['activate'] = sprintf(
+ '
%s ',
+ $activate_link,
+ /* translators: %s: Theme name. */
+ esc_attr( sprintf( _x( 'Activate “%s”', 'theme' ), $title ) ),
+ __( 'Activate' )
+ );
+
+ if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
+ $actions['preview'] .= sprintf(
+ '
%s ',
+ wp_customize_url( $stylesheet ),
+ __( 'Live Preview' )
+ );
+ }
+
+ if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) {
+ $actions['delete'] = sprintf(
+ '
%s ',
+ wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ),
+ /* translators: %s: Theme name. */
+ esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) ),
+ __( 'Delete' )
+ );
+ }
+
+ /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
+ $actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' );
+
+ /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
+ $actions = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, 'all' );
+ $delete_action = isset( $actions['delete'] ) ? '
' . $actions['delete'] . '
' : '';
+ unset( $actions['delete'] );
+
+ $screenshot = $theme->get_screenshot();
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
display( 'Description' ); ?>
+ parent() ) {
+ printf(
+ /* translators: 1: Link to documentation on child themes, 2: Name of parent theme. */
+ '
' . __( 'This child theme requires its parent theme, %2$s.' ) . '
',
+ __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
+ $theme->parent()->display( 'Name' )
+ );
+ }
+ ?>
+
+
+
+ features as $word ) {
+ if ( ! in_array( $word, $theme->get( 'Tags' ), true ) ) {
+ return false;
+ }
+ }
+
+ // Match all phrases.
+ foreach ( $this->search_terms as $word ) {
+ if ( in_array( $word, $theme->get( 'Tags' ), true ) ) {
+ continue;
+ }
+
+ foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
+ // Don't mark up; Do translate.
+ if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
+ continue 2;
+ }
+ }
+
+ if ( false !== stripos( $theme->get_stylesheet(), $word ) ) {
+ continue;
+ }
+
+ if ( false !== stripos( $theme->get_template(), $word ) ) {
+ continue;
+ }
+
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Send required variables to JavaScript land
+ *
+ * @since 3.4.0
+ *
+ * @param array $extra_args
+ */
+ public function _js_vars( $extra_args = array() ) {
+ $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
+
+ $args = array(
+ 'search' => $search_string,
+ 'features' => $this->features,
+ 'paged' => $this->get_pagenum(),
+ 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
+ );
+
+ if ( is_array( $extra_args ) ) {
+ $args = array_merge( $args, $extra_args );
+ }
+
+ printf( "\n", wp_json_encode( $args ) );
+ parent::_js_vars();
+ }
+}
diff --git a/wp-admin/includes/class-wp-upgrader-skin.php b/wp-admin/includes/class-wp-upgrader-skin.php
new file mode 100644
index 0000000..598724f
--- /dev/null
+++ b/wp-admin/includes/class-wp-upgrader-skin.php
@@ -0,0 +1,278 @@
+ '',
+ 'nonce' => '',
+ 'title' => '',
+ 'context' => false,
+ );
+ $this->options = wp_parse_args( $args, $defaults );
+ }
+
+ /**
+ * @since 2.8.0
+ *
+ * @param WP_Upgrader $upgrader
+ */
+ public function set_upgrader( &$upgrader ) {
+ if ( is_object( $upgrader ) ) {
+ $this->upgrader =& $upgrader;
+ }
+ $this->add_strings();
+ }
+
+ /**
+ * @since 3.0.0
+ */
+ public function add_strings() {
+ }
+
+ /**
+ * Sets the result of an upgrade.
+ *
+ * @since 2.8.0
+ *
+ * @param string|bool|WP_Error $result The result of an upgrade.
+ */
+ public function set_result( $result ) {
+ $this->result = $result;
+ }
+
+ /**
+ * Displays a form to the user to request for their FTP/SSH details in order
+ * to connect to the filesystem.
+ *
+ * @since 2.8.0
+ * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
+ *
+ * @see request_filesystem_credentials()
+ *
+ * @param bool|WP_Error $error Optional. Whether the current request has failed to connect,
+ * or an error object. Default false.
+ * @param string $context Optional. Full path to the directory that is tested
+ * for being writable. Default empty.
+ * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
+ * @return bool True on success, false on failure.
+ */
+ public function request_filesystem_credentials( $error = false, $context = '', $allow_relaxed_file_ownership = false ) {
+ $url = $this->options['url'];
+ if ( ! $context ) {
+ $context = $this->options['context'];
+ }
+ if ( ! empty( $this->options['nonce'] ) ) {
+ $url = wp_nonce_url( $url, $this->options['nonce'] );
+ }
+
+ $extra_fields = array();
+
+ return request_filesystem_credentials( $url, '', $error, $context, $extra_fields, $allow_relaxed_file_ownership );
+ }
+
+ /**
+ * @since 2.8.0
+ */
+ public function header() {
+ if ( $this->done_header ) {
+ return;
+ }
+ $this->done_header = true;
+ echo '
';
+ echo '
' . $this->options['title'] . ' ';
+ }
+
+ /**
+ * @since 2.8.0
+ */
+ public function footer() {
+ if ( $this->done_footer ) {
+ return;
+ }
+ $this->done_footer = true;
+ echo '';
+ }
+
+ /**
+ * @since 2.8.0
+ *
+ * @param string|WP_Error $errors Errors.
+ */
+ public function error( $errors ) {
+ if ( ! $this->done_header ) {
+ $this->header();
+ }
+ if ( is_string( $errors ) ) {
+ $this->feedback( $errors );
+ } elseif ( is_wp_error( $errors ) && $errors->has_errors() ) {
+ foreach ( $errors->get_error_messages() as $message ) {
+ if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
+ $this->feedback( $message . ' ' . esc_html( strip_tags( $errors->get_error_data() ) ) );
+ } else {
+ $this->feedback( $message );
+ }
+ }
+ }
+ }
+
+ /**
+ * @since 2.8.0
+ * @since 5.9.0 Renamed `$string` (a PHP reserved keyword) to `$feedback` for PHP 8 named parameter support.
+ *
+ * @param string $feedback Message data.
+ * @param mixed ...$args Optional text replacements.
+ */
+ public function feedback( $feedback, ...$args ) {
+ if ( isset( $this->upgrader->strings[ $feedback ] ) ) {
+ $feedback = $this->upgrader->strings[ $feedback ];
+ }
+
+ if ( str_contains( $feedback, '%' ) ) {
+ if ( $args ) {
+ $args = array_map( 'strip_tags', $args );
+ $args = array_map( 'esc_html', $args );
+ $feedback = vsprintf( $feedback, $args );
+ }
+ }
+ if ( empty( $feedback ) ) {
+ return;
+ }
+ show_message( $feedback );
+ }
+
+ /**
+ * Performs an action before an update.
+ *
+ * @since 2.8.0
+ */
+ public function before() {}
+
+ /**
+ * Performs and action following an update.
+ *
+ * @since 2.8.0
+ */
+ public function after() {}
+
+ /**
+ * Outputs JavaScript that calls function to decrement the update counts.
+ *
+ * @since 3.9.0
+ *
+ * @param string $type Type of update count to decrement. Likely values include 'plugin',
+ * 'theme', 'translation', etc.
+ */
+ protected function decrement_update_count( $type ) {
+ if ( ! $this->result || is_wp_error( $this->result ) || 'up_to_date' === $this->result ) {
+ return;
+ }
+
+ if ( defined( 'IFRAME_REQUEST' ) ) {
+ echo '';
+ } else {
+ echo '';
+ }
+ }
+
+ /**
+ * @since 3.0.0
+ */
+ public function bulk_header() {}
+
+ /**
+ * @since 3.0.0
+ */
+ public function bulk_footer() {}
+
+ /**
+ * Hides the `process_failed` error message when updating by uploading a zip file.
+ *
+ * @since 5.5.0
+ *
+ * @param WP_Error $wp_error WP_Error object.
+ * @return bool True if the error should be hidden, false otherwise.
+ */
+ public function hide_process_failed( $wp_error ) {
+ return false;
+ }
+}
diff --git a/wp-admin/includes/class-wp-upgrader-skins.php b/wp-admin/includes/class-wp-upgrader-skins.php
new file mode 100644
index 0000000..636ce18
--- /dev/null
+++ b/wp-admin/includes/class-wp-upgrader-skins.php
@@ -0,0 +1,44 @@
+skin = new WP_Upgrader_Skin();
+ } else {
+ $this->skin = $skin;
+ }
+ }
+
+ /**
+ * Initializes the upgrader.
+ *
+ * This will set the relationship between the skin being used and this upgrader,
+ * and also add the generic strings to `WP_Upgrader::$strings`.
+ *
+ * Additionally, it will schedule a weekly task to clean up the temporary backup directory.
+ *
+ * @since 2.8.0
+ * @since 6.3.0 Added the `schedule_temp_backup_cleanup()` task.
+ */
+ public function init() {
+ $this->skin->set_upgrader( $this );
+ $this->generic_strings();
+
+ if ( ! wp_installing() ) {
+ $this->schedule_temp_backup_cleanup();
+ }
+ }
+
+ /**
+ * Schedules the cleanup of the temporary backup directory.
+ *
+ * @since 6.3.0
+ */
+ protected function schedule_temp_backup_cleanup() {
+ if ( false === wp_next_scheduled( 'wp_delete_temp_updater_backups' ) ) {
+ wp_schedule_event( time(), 'weekly', 'wp_delete_temp_updater_backups' );
+ }
+ }
+
+ /**
+ * Adds the generic strings to WP_Upgrader::$strings.
+ *
+ * @since 2.8.0
+ */
+ public function generic_strings() {
+ $this->strings['bad_request'] = __( 'Invalid data provided.' );
+ $this->strings['fs_unavailable'] = __( 'Could not access filesystem.' );
+ $this->strings['fs_error'] = __( 'Filesystem error.' );
+ $this->strings['fs_no_root_dir'] = __( 'Unable to locate WordPress root directory.' );
+ /* translators: %s: Directory name. */
+ $this->strings['fs_no_content_dir'] = sprintf( __( 'Unable to locate WordPress content directory (%s).' ), 'wp-content' );
+ $this->strings['fs_no_plugins_dir'] = __( 'Unable to locate WordPress plugin directory.' );
+ $this->strings['fs_no_themes_dir'] = __( 'Unable to locate WordPress theme directory.' );
+ /* translators: %s: Directory name. */
+ $this->strings['fs_no_folder'] = __( 'Unable to locate needed folder (%s).' );
+
+ $this->strings['download_failed'] = __( 'Download failed.' );
+ $this->strings['installing_package'] = __( 'Installing the latest version…' );
+ $this->strings['no_files'] = __( 'The package contains no files.' );
+ $this->strings['folder_exists'] = __( 'Destination folder already exists.' );
+ $this->strings['mkdir_failed'] = __( 'Could not create directory.' );
+ $this->strings['incompatible_archive'] = __( 'The package could not be installed.' );
+ $this->strings['files_not_writable'] = __( 'The update cannot be installed because some files could not be copied. This is usually due to inconsistent file permissions.' );
+
+ $this->strings['maintenance_start'] = __( 'Enabling Maintenance mode…' );
+ $this->strings['maintenance_end'] = __( 'Disabling Maintenance mode…' );
+
+ /* translators: %s: upgrade-temp-backup */
+ $this->strings['temp_backup_mkdir_failed'] = sprintf( __( 'Could not create the %s directory.' ), 'upgrade-temp-backup' );
+ /* translators: %s: upgrade-temp-backup */
+ $this->strings['temp_backup_move_failed'] = sprintf( __( 'Could not move the old version to the %s directory.' ), 'upgrade-temp-backup' );
+ /* translators: %s: The plugin or theme slug. */
+ $this->strings['temp_backup_restore_failed'] = __( 'Could not restore the original version of %s.' );
+ /* translators: %s: The plugin or theme slug. */
+ $this->strings['temp_backup_delete_failed'] = __( 'Could not delete the temporary backup directory for %s.' );
+ }
+
+ /**
+ * Connects to the filesystem.
+ *
+ * @since 2.8.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param string[] $directories Optional. Array of directories. If any of these do
+ * not exist, a WP_Error object will be returned.
+ * Default empty array.
+ * @param bool $allow_relaxed_file_ownership Whether to allow relaxed file ownership.
+ * Default false.
+ * @return bool|WP_Error True if able to connect, false or a WP_Error otherwise.
+ */
+ public function fs_connect( $directories = array(), $allow_relaxed_file_ownership = false ) {
+ global $wp_filesystem;
+
+ $credentials = $this->skin->request_filesystem_credentials( false, $directories[0], $allow_relaxed_file_ownership );
+ if ( false === $credentials ) {
+ return false;
+ }
+
+ if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
+ $error = true;
+ if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->has_errors() ) {
+ $error = $wp_filesystem->errors;
+ }
+ // Failed to connect. Error and request again.
+ $this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
+ return false;
+ }
+
+ if ( ! is_object( $wp_filesystem ) ) {
+ return new WP_Error( 'fs_unavailable', $this->strings['fs_unavailable'] );
+ }
+
+ if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
+ return new WP_Error( 'fs_error', $this->strings['fs_error'], $wp_filesystem->errors );
+ }
+
+ foreach ( (array) $directories as $dir ) {
+ switch ( $dir ) {
+ case ABSPATH:
+ if ( ! $wp_filesystem->abspath() ) {
+ return new WP_Error( 'fs_no_root_dir', $this->strings['fs_no_root_dir'] );
+ }
+ break;
+ case WP_CONTENT_DIR:
+ if ( ! $wp_filesystem->wp_content_dir() ) {
+ return new WP_Error( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
+ }
+ break;
+ case WP_PLUGIN_DIR:
+ if ( ! $wp_filesystem->wp_plugins_dir() ) {
+ return new WP_Error( 'fs_no_plugins_dir', $this->strings['fs_no_plugins_dir'] );
+ }
+ break;
+ case get_theme_root():
+ if ( ! $wp_filesystem->wp_themes_dir() ) {
+ return new WP_Error( 'fs_no_themes_dir', $this->strings['fs_no_themes_dir'] );
+ }
+ break;
+ default:
+ if ( ! $wp_filesystem->find_folder( $dir ) ) {
+ return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
+ }
+ break;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Downloads a package.
+ *
+ * @since 2.8.0
+ * @since 5.2.0 Added the `$check_signatures` parameter.
+ * @since 5.5.0 Added the `$hook_extra` parameter.
+ *
+ * @param string $package The URI of the package. If this is the full path to an
+ * existing local file, it will be returned untouched.
+ * @param bool $check_signatures Whether to validate file signatures. Default false.
+ * @param array $hook_extra Extra arguments to pass to the filter hooks. Default empty array.
+ * @return string|WP_Error The full path to the downloaded package file, or a WP_Error object.
+ */
+ public function download_package( $package, $check_signatures = false, $hook_extra = array() ) {
+ /**
+ * Filters whether to return the package.
+ *
+ * @since 3.7.0
+ * @since 5.5.0 Added the `$hook_extra` parameter.
+ *
+ * @param bool $reply Whether to bail without returning the package.
+ * Default false.
+ * @param string $package The package file name.
+ * @param WP_Upgrader $upgrader The WP_Upgrader instance.
+ * @param array $hook_extra Extra arguments passed to hooked filters.
+ */
+ $reply = apply_filters( 'upgrader_pre_download', false, $package, $this, $hook_extra );
+ if ( false !== $reply ) {
+ return $reply;
+ }
+
+ if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) { // Local file or remote?
+ return $package; // Must be a local file.
+ }
+
+ if ( empty( $package ) ) {
+ return new WP_Error( 'no_package', $this->strings['no_package'] );
+ }
+
+ $this->skin->feedback( 'downloading_package', $package );
+
+ $download_file = download_url( $package, 300, $check_signatures );
+
+ if ( is_wp_error( $download_file ) && ! $download_file->get_error_data( 'softfail-filename' ) ) {
+ return new WP_Error( 'download_failed', $this->strings['download_failed'], $download_file->get_error_message() );
+ }
+
+ return $download_file;
+ }
+
+ /**
+ * Unpacks a compressed package file.
+ *
+ * @since 2.8.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param string $package Full path to the package file.
+ * @param bool $delete_package Optional. Whether to delete the package file after attempting
+ * to unpack it. Default true.
+ * @return string|WP_Error The path to the unpacked contents, or a WP_Error on failure.
+ */
+ public function unpack_package( $package, $delete_package = true ) {
+ global $wp_filesystem;
+
+ $this->skin->feedback( 'unpack_package' );
+
+ if ( ! $wp_filesystem->wp_content_dir() ) {
+ return new WP_Error( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
+ }
+
+ $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
+
+ // Clean up contents of upgrade directory beforehand.
+ $upgrade_files = $wp_filesystem->dirlist( $upgrade_folder );
+ if ( ! empty( $upgrade_files ) ) {
+ foreach ( $upgrade_files as $file ) {
+ $wp_filesystem->delete( $upgrade_folder . $file['name'], true );
+ }
+ }
+
+ // We need a working directory - strip off any .tmp or .zip suffixes.
+ $working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
+
+ // Clean up working directory.
+ if ( $wp_filesystem->is_dir( $working_dir ) ) {
+ $wp_filesystem->delete( $working_dir, true );
+ }
+
+ // Unzip package to working directory.
+ $result = unzip_file( $package, $working_dir );
+
+ // Once extracted, delete the package if required.
+ if ( $delete_package ) {
+ unlink( $package );
+ }
+
+ if ( is_wp_error( $result ) ) {
+ $wp_filesystem->delete( $working_dir, true );
+ if ( 'incompatible_archive' === $result->get_error_code() ) {
+ return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
+ }
+ return $result;
+ }
+
+ return $working_dir;
+ }
+
+ /**
+ * Flattens the results of WP_Filesystem_Base::dirlist() for iterating over.
+ *
+ * @since 4.9.0
+ * @access protected
+ *
+ * @param array $nested_files Array of files as returned by WP_Filesystem_Base::dirlist().
+ * @param string $path Relative path to prepend to child nodes. Optional.
+ * @return array A flattened array of the $nested_files specified.
+ */
+ protected function flatten_dirlist( $nested_files, $path = '' ) {
+ $files = array();
+
+ foreach ( $nested_files as $name => $details ) {
+ $files[ $path . $name ] = $details;
+
+ // Append children recursively.
+ if ( ! empty( $details['files'] ) ) {
+ $children = $this->flatten_dirlist( $details['files'], $path . $name . '/' );
+
+ // Merge keeping possible numeric keys, which array_merge() will reindex from 0..n.
+ $files = $files + $children;
+ }
+ }
+
+ return $files;
+ }
+
+ /**
+ * Clears the directory where this item is going to be installed into.
+ *
+ * @since 4.3.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param string $remote_destination The location on the remote filesystem to be cleared.
+ * @return true|WP_Error True upon success, WP_Error on failure.
+ */
+ public function clear_destination( $remote_destination ) {
+ global $wp_filesystem;
+
+ $files = $wp_filesystem->dirlist( $remote_destination, true, true );
+
+ // False indicates that the $remote_destination doesn't exist.
+ if ( false === $files ) {
+ return true;
+ }
+
+ // Flatten the file list to iterate over.
+ $files = $this->flatten_dirlist( $files );
+
+ // Check all files are writable before attempting to clear the destination.
+ $unwritable_files = array();
+
+ // Check writability.
+ foreach ( $files as $filename => $file_details ) {
+ if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
+ // Attempt to alter permissions to allow writes and try again.
+ $wp_filesystem->chmod( $remote_destination . $filename, ( 'd' === $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
+ if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
+ $unwritable_files[] = $filename;
+ }
+ }
+ }
+
+ if ( ! empty( $unwritable_files ) ) {
+ return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
+ }
+
+ if ( ! $wp_filesystem->delete( $remote_destination, true ) ) {
+ return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
+ }
+
+ return true;
+ }
+
+ /**
+ * Install a package.
+ *
+ * Copies the contents of a package from a source directory, and installs them in
+ * a destination directory. Optionally removes the source. It can also optionally
+ * clear out the destination folder if it already exists.
+ *
+ * @since 2.8.0
+ * @since 6.2.0 Use move_dir() instead of copy_dir() when possible.
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ * @global array $wp_theme_directories
+ *
+ * @param array|string $args {
+ * Optional. Array or string of arguments for installing a package. Default empty array.
+ *
+ * @type string $source Required path to the package source. Default empty.
+ * @type string $destination Required path to a folder to install the package in.
+ * Default empty.
+ * @type bool $clear_destination Whether to delete any files already in the destination
+ * folder. Default false.
+ * @type bool $clear_working Whether to delete the files from the working directory
+ * after copying them to the destination. Default false.
+ * @type bool $abort_if_destination_exists Whether to abort the installation if
+ * the destination folder already exists. Default true.
+ * @type array $hook_extra Extra arguments to pass to the filter hooks called by
+ * WP_Upgrader::install_package(). Default empty array.
+ * }
+ *
+ * @return array|WP_Error The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure.
+ */
+ public function install_package( $args = array() ) {
+ global $wp_filesystem, $wp_theme_directories;
+
+ $defaults = array(
+ 'source' => '', // Please always pass this.
+ 'destination' => '', // ...and this.
+ 'clear_destination' => false,
+ 'clear_working' => false,
+ 'abort_if_destination_exists' => true,
+ 'hook_extra' => array(),
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+
+ // These were previously extract()'d.
+ $source = $args['source'];
+ $destination = $args['destination'];
+ $clear_destination = $args['clear_destination'];
+
+ if ( function_exists( 'set_time_limit' ) ) {
+ set_time_limit( 300 );
+ }
+
+ if ( empty( $source ) || empty( $destination ) ) {
+ return new WP_Error( 'bad_request', $this->strings['bad_request'] );
+ }
+ $this->skin->feedback( 'installing_package' );
+
+ /**
+ * Filters the installation response before the installation has started.
+ *
+ * Returning a value that could be evaluated as a `WP_Error` will effectively
+ * short-circuit the installation, returning that value instead.
+ *
+ * @since 2.8.0
+ *
+ * @param bool|WP_Error $response Installation response.
+ * @param array $hook_extra Extra arguments passed to hooked filters.
+ */
+ $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
+
+ if ( is_wp_error( $res ) ) {
+ return $res;
+ }
+
+ // Retain the original source and destinations.
+ $remote_source = $args['source'];
+ $local_destination = $destination;
+
+ $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
+ $remote_destination = $wp_filesystem->find_folder( $local_destination );
+
+ // Locate which directory to copy to the new folder. This is based on the actual folder holding the files.
+ if ( 1 === count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) {
+ // Only one folder? Then we want its contents.
+ $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
+ } elseif ( 0 === count( $source_files ) ) {
+ // There are no files?
+ return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] );
+ } else {
+ /*
+ * It's only a single file, the upgrader will use the folder name of this file as the destination folder.
+ * Folder name is based on zip filename.
+ */
+ $source = trailingslashit( $args['source'] );
+ }
+
+ /**
+ * Filters the source file location for the upgrade package.
+ *
+ * @since 2.8.0
+ * @since 4.4.0 The $hook_extra parameter became available.
+ *
+ * @param string $source File source location.
+ * @param string $remote_source Remote file source location.
+ * @param WP_Upgrader $upgrader WP_Upgrader instance.
+ * @param array $hook_extra Extra arguments passed to hooked filters.
+ */
+ $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );
+
+ if ( is_wp_error( $source ) ) {
+ return $source;
+ }
+
+ if ( ! empty( $args['hook_extra']['temp_backup'] ) ) {
+ $temp_backup = $this->move_to_temp_backup_dir( $args['hook_extra']['temp_backup'] );
+
+ if ( is_wp_error( $temp_backup ) ) {
+ return $temp_backup;
+ }
+
+ $this->temp_backups[] = $args['hook_extra']['temp_backup'];
+ }
+
+ // Has the source location changed? If so, we need a new source_files list.
+ if ( $source !== $remote_source ) {
+ $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
+ }
+
+ /*
+ * Protection against deleting files in any important base directories.
+ * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
+ * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
+ * to copy the directory into the directory, whilst they pass the source
+ * as the actual files to copy.
+ */
+ $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
+
+ if ( is_array( $wp_theme_directories ) ) {
+ $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
+ }
+
+ if ( in_array( $destination, $protected_directories, true ) ) {
+ $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
+ $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
+ }
+
+ if ( $clear_destination ) {
+ // We're going to clear the destination if there's something there.
+ $this->skin->feedback( 'remove_old' );
+
+ $removed = $this->clear_destination( $remote_destination );
+
+ /**
+ * Filters whether the upgrader cleared the destination.
+ *
+ * @since 2.8.0
+ *
+ * @param true|WP_Error $removed Whether the destination was cleared.
+ * True upon success, WP_Error on failure.
+ * @param string $local_destination The local package destination.
+ * @param string $remote_destination The remote package destination.
+ * @param array $hook_extra Extra arguments passed to hooked filters.
+ */
+ $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
+
+ if ( is_wp_error( $removed ) ) {
+ return $removed;
+ }
+ } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists( $remote_destination ) ) {
+ /*
+ * If we're not clearing the destination folder and something exists there already, bail.
+ * But first check to see if there are actually any files in the folder.
+ */
+ $_files = $wp_filesystem->dirlist( $remote_destination );
+ if ( ! empty( $_files ) ) {
+ $wp_filesystem->delete( $remote_source, true ); // Clear out the source files.
+ return new WP_Error( 'folder_exists', $this->strings['folder_exists'], $remote_destination );
+ }
+ }
+
+ /*
+ * If 'clear_working' is false, the source should not be removed, so use copy_dir() instead.
+ *
+ * Partial updates, like language packs, may want to retain the destination.
+ * If the destination exists or has contents, this may be a partial update,
+ * and the destination should not be removed, so use copy_dir() instead.
+ */
+ if ( $args['clear_working']
+ && (
+ // Destination does not exist or has no contents.
+ ! $wp_filesystem->exists( $remote_destination )
+ || empty( $wp_filesystem->dirlist( $remote_destination ) )
+ )
+ ) {
+ $result = move_dir( $source, $remote_destination, true );
+ } else {
+ // Create destination if needed.
+ if ( ! $wp_filesystem->exists( $remote_destination ) ) {
+ if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
+ return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
+ }
+ }
+ $result = copy_dir( $source, $remote_destination );
+ }
+
+ // Clear the working directory?
+ if ( $args['clear_working'] ) {
+ $wp_filesystem->delete( $remote_source, true );
+ }
+
+ if ( is_wp_error( $result ) ) {
+ return $result;
+ }
+
+ $destination_name = basename( str_replace( $local_destination, '', $destination ) );
+ if ( '.' === $destination_name ) {
+ $destination_name = '';
+ }
+
+ $this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
+
+ /**
+ * Filters the installation response after the installation has finished.
+ *
+ * @since 2.8.0
+ *
+ * @param bool $response Installation response.
+ * @param array $hook_extra Extra arguments passed to hooked filters.
+ * @param array $result Installation result data.
+ */
+ $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
+
+ if ( is_wp_error( $res ) ) {
+ $this->result = $res;
+ return $res;
+ }
+
+ // Bombard the calling function will all the info which we've just used.
+ return $this->result;
+ }
+
+ /**
+ * Runs an upgrade/installation.
+ *
+ * Attempts to download the package (if it is not a local file), unpack it, and
+ * install it in the destination folder.
+ *
+ * @since 2.8.0
+ *
+ * @param array $options {
+ * Array or string of arguments for upgrading/installing a package.
+ *
+ * @type string $package The full path or URI of the package to install.
+ * Default empty.
+ * @type string $destination The full path to the destination folder.
+ * Default empty.
+ * @type bool $clear_destination Whether to delete any files already in the
+ * destination folder. Default false.
+ * @type bool $clear_working Whether to delete the files from the working
+ * directory after copying them to the destination.
+ * Default true.
+ * @type bool $abort_if_destination_exists Whether to abort the installation if the destination
+ * folder already exists. When true, `$clear_destination`
+ * should be false. Default true.
+ * @type bool $is_multi Whether this run is one of multiple upgrade/installation
+ * actions being performed in bulk. When true, the skin
+ * WP_Upgrader::header() and WP_Upgrader::footer()
+ * aren't called. Default false.
+ * @type array $hook_extra Extra arguments to pass to the filter hooks called by
+ * WP_Upgrader::run().
+ * }
+ * @return array|false|WP_Error The result from self::install_package() on success, otherwise a WP_Error,
+ * or false if unable to connect to the filesystem.
+ */
+ public function run( $options ) {
+
+ $defaults = array(
+ 'package' => '', // Please always pass this.
+ 'destination' => '', // ...and this.
+ 'clear_destination' => false,
+ 'clear_working' => true,
+ 'abort_if_destination_exists' => true, // Abort if the destination directory exists. Pass clear_destination as false please.
+ 'is_multi' => false,
+ 'hook_extra' => array(), // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
+ );
+
+ $options = wp_parse_args( $options, $defaults );
+
+ /**
+ * Filters the package options before running an update.
+ *
+ * See also {@see 'upgrader_process_complete'}.
+ *
+ * @since 4.3.0
+ *
+ * @param array $options {
+ * Options used by the upgrader.
+ *
+ * @type string $package Package for update.
+ * @type string $destination Update location.
+ * @type bool $clear_destination Clear the destination resource.
+ * @type bool $clear_working Clear the working resource.
+ * @type bool $abort_if_destination_exists Abort if the Destination directory exists.
+ * @type bool $is_multi Whether the upgrader is running multiple times.
+ * @type array $hook_extra {
+ * Extra hook arguments.
+ *
+ * @type string $action Type of action. Default 'update'.
+ * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'.
+ * @type bool $bulk Whether the update process is a bulk update. Default true.
+ * @type string $plugin Path to the plugin file relative to the plugins directory.
+ * @type string $theme The stylesheet or template name of the theme.
+ * @type string $language_update_type The language pack update type. Accepts 'plugin', 'theme',
+ * or 'core'.
+ * @type object $language_update The language pack update offer.
+ * }
+ * }
+ */
+ $options = apply_filters( 'upgrader_package_options', $options );
+
+ if ( ! $options['is_multi'] ) { // Call $this->header separately if running multiple times.
+ $this->skin->header();
+ }
+
+ // Connect to the filesystem first.
+ $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
+ // Mainly for non-connected filesystem.
+ if ( ! $res ) {
+ if ( ! $options['is_multi'] ) {
+ $this->skin->footer();
+ }
+ return false;
+ }
+
+ $this->skin->before();
+
+ if ( is_wp_error( $res ) ) {
+ $this->skin->error( $res );
+ $this->skin->after();
+ if ( ! $options['is_multi'] ) {
+ $this->skin->footer();
+ }
+ return $res;
+ }
+
+ /*
+ * Download the package. Note: If the package is the full path
+ * to an existing local file, it will be returned untouched.
+ */
+ $download = $this->download_package( $options['package'], true, $options['hook_extra'] );
+
+ /*
+ * Allow for signature soft-fail.
+ * WARNING: This may be removed in the future.
+ */
+ if ( is_wp_error( $download ) && $download->get_error_data( 'softfail-filename' ) ) {
+
+ // Don't output the 'no signature could be found' failure message for now.
+ if ( 'signature_verification_no_signature' !== $download->get_error_code() || WP_DEBUG ) {
+ // Output the failure error as a normal feedback, and not as an error.
+ $this->skin->feedback( $download->get_error_message() );
+
+ // Report this failure back to WordPress.org for debugging purposes.
+ wp_version_check(
+ array(
+ 'signature_failure_code' => $download->get_error_code(),
+ 'signature_failure_data' => $download->get_error_data(),
+ )
+ );
+ }
+
+ // Pretend this error didn't happen.
+ $download = $download->get_error_data( 'softfail-filename' );
+ }
+
+ if ( is_wp_error( $download ) ) {
+ $this->skin->error( $download );
+ $this->skin->after();
+ if ( ! $options['is_multi'] ) {
+ $this->skin->footer();
+ }
+ return $download;
+ }
+
+ $delete_package = ( $download !== $options['package'] ); // Do not delete a "local" file.
+
+ // Unzips the file into a temporary directory.
+ $working_dir = $this->unpack_package( $download, $delete_package );
+ if ( is_wp_error( $working_dir ) ) {
+ $this->skin->error( $working_dir );
+ $this->skin->after();
+ if ( ! $options['is_multi'] ) {
+ $this->skin->footer();
+ }
+ return $working_dir;
+ }
+
+ // With the given options, this installs it to the destination directory.
+ $result = $this->install_package(
+ array(
+ 'source' => $working_dir,
+ 'destination' => $options['destination'],
+ 'clear_destination' => $options['clear_destination'],
+ 'abort_if_destination_exists' => $options['abort_if_destination_exists'],
+ 'clear_working' => $options['clear_working'],
+ 'hook_extra' => $options['hook_extra'],
+ )
+ );
+
+ /**
+ * Filters the result of WP_Upgrader::install_package().
+ *
+ * @since 5.7.0
+ *
+ * @param array|WP_Error $result Result from WP_Upgrader::install_package().
+ * @param array $hook_extra Extra arguments passed to hooked filters.
+ */
+ $result = apply_filters( 'upgrader_install_package_result', $result, $options['hook_extra'] );
+
+ $this->skin->set_result( $result );
+
+ if ( is_wp_error( $result ) ) {
+ if ( ! empty( $options['hook_extra']['temp_backup'] ) ) {
+ $this->temp_restores[] = $options['hook_extra']['temp_backup'];
+
+ /*
+ * Restore the backup on shutdown.
+ * Actions running on `shutdown` are immune to PHP timeouts,
+ * so in case the failure was due to a PHP timeout,
+ * it will still be able to properly restore the previous version.
+ */
+ add_action( 'shutdown', array( $this, 'restore_temp_backup' ) );
+ }
+ $this->skin->error( $result );
+
+ if ( ! method_exists( $this->skin, 'hide_process_failed' ) || ! $this->skin->hide_process_failed( $result ) ) {
+ $this->skin->feedback( 'process_failed' );
+ }
+ } else {
+ // Installation succeeded.
+ $this->skin->feedback( 'process_success' );
+ }
+
+ $this->skin->after();
+
+ // Clean up the backup kept in the temporary backup directory.
+ if ( ! empty( $options['hook_extra']['temp_backup'] ) ) {
+ // Delete the backup on `shutdown` to avoid a PHP timeout.
+ add_action( 'shutdown', array( $this, 'delete_temp_backup' ), 100, 0 );
+ }
+
+ if ( ! $options['is_multi'] ) {
+
+ /**
+ * Fires when the upgrader process is complete.
+ *
+ * See also {@see 'upgrader_package_options'}.
+ *
+ * @since 3.6.0
+ * @since 3.7.0 Added to WP_Upgrader::run().
+ * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
+ *
+ * @param WP_Upgrader $upgrader WP_Upgrader instance. In other contexts this might be a
+ * Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
+ * @param array $hook_extra {
+ * Array of bulk item update data.
+ *
+ * @type string $action Type of action. Default 'update'.
+ * @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
+ * @type bool $bulk Whether the update process is a bulk update. Default true.
+ * @type array $plugins Array of the basename paths of the plugins' main files.
+ * @type array $themes The theme slugs.
+ * @type array $translations {
+ * Array of translations update data.
+ *
+ * @type string $language The locale the translation is for.
+ * @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'.
+ * @type string $slug Text domain the translation is for. The slug of a theme/plugin or
+ * 'default' for core translations.
+ * @type string $version The version of a theme, plugin, or core.
+ * }
+ * }
+ */
+ do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
+
+ $this->skin->footer();
+ }
+
+ return $result;
+ }
+
+ /**
+ * Toggles maintenance mode for the site.
+ *
+ * Creates/deletes the maintenance file to enable/disable maintenance mode.
+ *
+ * @since 2.8.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param bool $enable True to enable maintenance mode, false to disable.
+ */
+ public function maintenance_mode( $enable = false ) {
+ global $wp_filesystem;
+ $file = $wp_filesystem->abspath() . '.maintenance';
+ if ( $enable ) {
+ $this->skin->feedback( 'maintenance_start' );
+ // Create maintenance file to signal that we are upgrading.
+ $maintenance_string = '';
+ $wp_filesystem->delete( $file );
+ $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE );
+ } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
+ $this->skin->feedback( 'maintenance_end' );
+ $wp_filesystem->delete( $file );
+ }
+ }
+
+ /**
+ * Creates a lock using WordPress options.
+ *
+ * @since 4.5.0
+ *
+ * @global wpdb $wpdb The WordPress database abstraction object.
+ *
+ * @param string $lock_name The name of this unique lock.
+ * @param int $release_timeout Optional. The duration in seconds to respect an existing lock.
+ * Default: 1 hour.
+ * @return bool False if a lock couldn't be created or if the lock is still valid. True otherwise.
+ */
+ public static function create_lock( $lock_name, $release_timeout = null ) {
+ global $wpdb;
+ if ( ! $release_timeout ) {
+ $release_timeout = HOUR_IN_SECONDS;
+ }
+ $lock_option = $lock_name . '.lock';
+
+ // Try to lock.
+ $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_option, time() ) );
+
+ if ( ! $lock_result ) {
+ $lock_result = get_option( $lock_option );
+
+ // If a lock couldn't be created, and there isn't a lock, bail.
+ if ( ! $lock_result ) {
+ return false;
+ }
+
+ // Check to see if the lock is still valid. If it is, bail.
+ if ( $lock_result > ( time() - $release_timeout ) ) {
+ return false;
+ }
+
+ // There must exist an expired lock, clear it and re-gain it.
+ WP_Upgrader::release_lock( $lock_name );
+
+ return WP_Upgrader::create_lock( $lock_name, $release_timeout );
+ }
+
+ // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
+ update_option( $lock_option, time() );
+
+ return true;
+ }
+
+ /**
+ * Releases an upgrader lock.
+ *
+ * @since 4.5.0
+ *
+ * @see WP_Upgrader::create_lock()
+ *
+ * @param string $lock_name The name of this unique lock.
+ * @return bool True if the lock was successfully released. False on failure.
+ */
+ public static function release_lock( $lock_name ) {
+ return delete_option( $lock_name . '.lock' );
+ }
+
+ /**
+ * Moves the plugin or theme being updated into a temporary backup directory.
+ *
+ * @since 6.3.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param string[] $args {
+ * Array of data for the temporary backup.
+ *
+ * @type string $slug Plugin or theme slug.
+ * @type string $src Path to the root directory for plugins or themes.
+ * @type string $dir Destination subdirectory name. Accepts 'plugins' or 'themes'.
+ * }
+ *
+ * @return bool|WP_Error True on success, false on early exit, otherwise WP_Error.
+ */
+ public function move_to_temp_backup_dir( $args ) {
+ global $wp_filesystem;
+
+ if ( empty( $args['slug'] ) || empty( $args['src'] ) || empty( $args['dir'] ) ) {
+ return false;
+ }
+
+ /*
+ * Skip any plugin that has "." as its slug.
+ * A slug of "." will result in a `$src` value ending in a period.
+ *
+ * On Windows, this will cause the 'plugins' folder to be moved,
+ * and will cause a failure when attempting to call `mkdir()`.
+ */
+ if ( '.' === $args['slug'] ) {
+ return false;
+ }
+
+ if ( ! $wp_filesystem->wp_content_dir() ) {
+ return new WP_Error( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
+ }
+
+ $dest_dir = $wp_filesystem->wp_content_dir() . 'upgrade-temp-backup/';
+ $sub_dir = $dest_dir . $args['dir'] . '/';
+
+ // Create the temporary backup directory if it does not exist.
+ if ( ! $wp_filesystem->is_dir( $sub_dir ) ) {
+ if ( ! $wp_filesystem->is_dir( $dest_dir ) ) {
+ $wp_filesystem->mkdir( $dest_dir, FS_CHMOD_DIR );
+ }
+
+ if ( ! $wp_filesystem->mkdir( $sub_dir, FS_CHMOD_DIR ) ) {
+ // Could not create the backup directory.
+ return new WP_Error( 'fs_temp_backup_mkdir', $this->strings['temp_backup_mkdir_failed'] );
+ }
+ }
+
+ $src_dir = $wp_filesystem->find_folder( $args['src'] );
+ $src = trailingslashit( $src_dir ) . $args['slug'];
+ $dest = $dest_dir . trailingslashit( $args['dir'] ) . $args['slug'];
+
+ // Delete the temporary backup directory if it already exists.
+ if ( $wp_filesystem->is_dir( $dest ) ) {
+ $wp_filesystem->delete( $dest, true );
+ }
+
+ // Move to the temporary backup directory.
+ $result = move_dir( $src, $dest, true );
+ if ( is_wp_error( $result ) ) {
+ return new WP_Error( 'fs_temp_backup_move', $this->strings['temp_backup_move_failed'] );
+ }
+
+ return true;
+ }
+
+ /**
+ * Restores the plugin or theme from temporary backup.
+ *
+ * @since 6.3.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @return bool|WP_Error True on success, false on early exit, otherwise WP_Error.
+ */
+ public function restore_temp_backup() {
+ global $wp_filesystem;
+
+ $errors = new WP_Error();
+
+ foreach ( $this->temp_restores as $args ) {
+ if ( empty( $args['slug'] ) || empty( $args['src'] ) || empty( $args['dir'] ) ) {
+ return false;
+ }
+
+ if ( ! $wp_filesystem->wp_content_dir() ) {
+ $errors->add( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
+ return $errors;
+ }
+
+ $src = $wp_filesystem->wp_content_dir() . 'upgrade-temp-backup/' . $args['dir'] . '/' . $args['slug'];
+ $dest_dir = $wp_filesystem->find_folder( $args['src'] );
+ $dest = trailingslashit( $dest_dir ) . $args['slug'];
+
+ if ( $wp_filesystem->is_dir( $src ) ) {
+ // Cleanup.
+ if ( $wp_filesystem->is_dir( $dest ) && ! $wp_filesystem->delete( $dest, true ) ) {
+ $errors->add(
+ 'fs_temp_backup_delete',
+ sprintf( $this->strings['temp_backup_restore_failed'], $args['slug'] )
+ );
+ continue;
+ }
+
+ // Move it.
+ $result = move_dir( $src, $dest, true );
+ if ( is_wp_error( $result ) ) {
+ $errors->add(
+ 'fs_temp_backup_delete',
+ sprintf( $this->strings['temp_backup_restore_failed'], $args['slug'] )
+ );
+ continue;
+ }
+ }
+ }
+
+ return $errors->has_errors() ? $errors : true;
+ }
+
+ /**
+ * Deletes a temporary backup.
+ *
+ * @since 6.3.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @return bool|WP_Error True on success, false on early exit, otherwise WP_Error.
+ */
+ public function delete_temp_backup() {
+ global $wp_filesystem;
+
+ $errors = new WP_Error();
+
+ foreach ( $this->temp_backups as $args ) {
+ if ( empty( $args['slug'] ) || empty( $args['dir'] ) ) {
+ return false;
+ }
+
+ if ( ! $wp_filesystem->wp_content_dir() ) {
+ $errors->add( 'fs_no_content_dir', $this->strings['fs_no_content_dir'] );
+ return $errors;
+ }
+
+ $temp_backup_dir = $wp_filesystem->wp_content_dir() . "upgrade-temp-backup/{$args['dir']}/{$args['slug']}";
+
+ if ( ! $wp_filesystem->delete( $temp_backup_dir, true ) ) {
+ $errors->add(
+ 'temp_backup_delete_failed',
+ sprintf( $this->strings['temp_backup_delete_failed'], $args['slug'] )
+ );
+ continue;
+ }
+ }
+
+ return $errors->has_errors() ? $errors : true;
+ }
+}
+
+/** Plugin_Upgrader class */
+require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
+
+/** Theme_Upgrader class */
+require_once ABSPATH . 'wp-admin/includes/class-theme-upgrader.php';
+
+/** Language_Pack_Upgrader class */
+require_once ABSPATH . 'wp-admin/includes/class-language-pack-upgrader.php';
+
+/** Core_Upgrader class */
+require_once ABSPATH . 'wp-admin/includes/class-core-upgrader.php';
+
+/** File_Upload_Upgrader class */
+require_once ABSPATH . 'wp-admin/includes/class-file-upload-upgrader.php';
+
+/** WP_Automatic_Updater class */
+require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
diff --git a/wp-admin/includes/class-wp-users-list-table.php b/wp-admin/includes/class-wp-users-list-table.php
new file mode 100644
index 0000000..ecb8eb4
--- /dev/null
+++ b/wp-admin/includes/class-wp-users-list-table.php
@@ -0,0 +1,683 @@
+ 'user',
+ 'plural' => 'users',
+ 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
+ )
+ );
+
+ $this->is_site_users = 'site-users-network' === $this->screen->id;
+
+ if ( $this->is_site_users ) {
+ $this->site_id = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0;
+ }
+ }
+
+ /**
+ * Checks the current user's permissions.
+ *
+ * @since 3.1.0
+ *
+ * @return bool
+ */
+ public function ajax_user_can() {
+ if ( $this->is_site_users ) {
+ return current_user_can( 'manage_sites' );
+ } else {
+ return current_user_can( 'list_users' );
+ }
+ }
+
+ /**
+ * Prepares the users list for display.
+ *
+ * @since 3.1.0
+ *
+ * @global string $role
+ * @global string $usersearch
+ */
+ public function prepare_items() {
+ global $role, $usersearch;
+
+ $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
+
+ $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
+
+ $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
+ $users_per_page = $this->get_items_per_page( $per_page );
+
+ $paged = $this->get_pagenum();
+
+ if ( 'none' === $role ) {
+ $args = array(
+ 'number' => $users_per_page,
+ 'offset' => ( $paged - 1 ) * $users_per_page,
+ 'include' => wp_get_users_with_no_role( $this->site_id ),
+ 'search' => $usersearch,
+ 'fields' => 'all_with_meta',
+ );
+ } else {
+ $args = array(
+ 'number' => $users_per_page,
+ 'offset' => ( $paged - 1 ) * $users_per_page,
+ 'role' => $role,
+ 'search' => $usersearch,
+ 'fields' => 'all_with_meta',
+ );
+ }
+
+ if ( '' !== $args['search'] ) {
+ $args['search'] = '*' . $args['search'] . '*';
+ }
+
+ if ( $this->is_site_users ) {
+ $args['blog_id'] = $this->site_id;
+ }
+
+ if ( isset( $_REQUEST['orderby'] ) ) {
+ $args['orderby'] = $_REQUEST['orderby'];
+ }
+
+ if ( isset( $_REQUEST['order'] ) ) {
+ $args['order'] = $_REQUEST['order'];
+ }
+
+ /**
+ * Filters the query arguments used to retrieve users for the current users list table.
+ *
+ * @since 4.4.0
+ *
+ * @param array $args Arguments passed to WP_User_Query to retrieve items for the current
+ * users list table.
+ */
+ $args = apply_filters( 'users_list_table_query_args', $args );
+
+ // Query the user IDs for this page.
+ $wp_user_search = new WP_User_Query( $args );
+
+ $this->items = $wp_user_search->get_results();
+
+ $this->set_pagination_args(
+ array(
+ 'total_items' => $wp_user_search->get_total(),
+ 'per_page' => $users_per_page,
+ )
+ );
+ }
+
+ /**
+ * Outputs 'no users' message.
+ *
+ * @since 3.1.0
+ */
+ public function no_items() {
+ _e( 'No users found.' );
+ }
+
+ /**
+ * Returns an associative array listing all the views that can be used
+ * with this table.
+ *
+ * Provides a list of roles and user count for that role for easy
+ * Filtersing of the user table.
+ *
+ * @since 3.1.0
+ *
+ * @global string $role
+ *
+ * @return string[] An array of HTML links keyed by their view.
+ */
+ protected function get_views() {
+ global $role;
+
+ $wp_roles = wp_roles();
+
+ $count_users = ! wp_is_large_user_count();
+
+ if ( $this->is_site_users ) {
+ $url = 'site-users.php?id=' . $this->site_id;
+ } else {
+ $url = 'users.php';
+ }
+
+ $role_links = array();
+ $avail_roles = array();
+ $all_text = __( 'All' );
+
+ if ( $count_users ) {
+ if ( $this->is_site_users ) {
+ switch_to_blog( $this->site_id );
+ $users_of_blog = count_users( 'time', $this->site_id );
+ restore_current_blog();
+ } else {
+ $users_of_blog = count_users();
+ }
+
+ $total_users = $users_of_blog['total_users'];
+ $avail_roles =& $users_of_blog['avail_roles'];
+ unset( $users_of_blog );
+
+ $all_text = sprintf(
+ /* translators: %s: Number of users. */
+ _nx(
+ 'All
(%s) ',
+ 'All
(%s) ',
+ $total_users,
+ 'users'
+ ),
+ number_format_i18n( $total_users )
+ );
+ }
+
+ $role_links['all'] = array(
+ 'url' => $url,
+ 'label' => $all_text,
+ 'current' => empty( $role ),
+ );
+
+ foreach ( $wp_roles->get_names() as $this_role => $name ) {
+ if ( $count_users && ! isset( $avail_roles[ $this_role ] ) ) {
+ continue;
+ }
+
+ $name = translate_user_role( $name );
+ if ( $count_users ) {
+ $name = sprintf(
+ /* translators: 1: User role name, 2: Number of users. */
+ __( '%1$s
(%2$s) ' ),
+ $name,
+ number_format_i18n( $avail_roles[ $this_role ] )
+ );
+ }
+
+ $role_links[ $this_role ] = array(
+ 'url' => esc_url( add_query_arg( 'role', $this_role, $url ) ),
+ 'label' => $name,
+ 'current' => $this_role === $role,
+ );
+ }
+
+ if ( ! empty( $avail_roles['none'] ) ) {
+
+ $name = __( 'No role' );
+ $name = sprintf(
+ /* translators: 1: User role name, 2: Number of users. */
+ __( '%1$s
(%2$s) ' ),
+ $name,
+ number_format_i18n( $avail_roles['none'] )
+ );
+
+ $role_links['none'] = array(
+ 'url' => esc_url( add_query_arg( 'role', 'none', $url ) ),
+ 'label' => $name,
+ 'current' => 'none' === $role,
+ );
+ }
+
+ return $this->get_views_links( $role_links );
+ }
+
+ /**
+ * Retrieves an associative array of bulk actions available on this table.
+ *
+ * @since 3.1.0
+ *
+ * @return array Array of bulk action labels keyed by their action.
+ */
+ protected function get_bulk_actions() {
+ $actions = array();
+
+ if ( is_multisite() ) {
+ if ( current_user_can( 'remove_users' ) ) {
+ $actions['remove'] = __( 'Remove' );
+ }
+ } else {
+ if ( current_user_can( 'delete_users' ) ) {
+ $actions['delete'] = __( 'Delete' );
+ }
+ }
+
+ // Add a password reset link to the bulk actions dropdown.
+ if ( current_user_can( 'edit_users' ) ) {
+ $actions['resetpassword'] = __( 'Send password reset' );
+ }
+
+ return $actions;
+ }
+
+ /**
+ * Outputs the controls to allow user roles to be changed in bulk.
+ *
+ * @since 3.1.0
+ *
+ * @param string $which Whether this is being invoked above ("top")
+ * or below the table ("bottom").
+ */
+ protected function extra_tablenav( $which ) {
+ $id = 'bottom' === $which ? 'new_role2' : 'new_role';
+ $button_id = 'bottom' === $which ? 'changeit2' : 'changeit';
+ ?>
+
+ has_items() ) : ?>
+
+
+
+
+
+
+
+
+
+
+ '
',
+ 'username' => __( 'Username' ),
+ 'name' => __( 'Name' ),
+ 'email' => __( 'Email' ),
+ 'role' => __( 'Role' ),
+ 'posts' => _x( 'Posts', 'post type general name' ),
+ );
+
+ if ( $this->is_site_users ) {
+ unset( $columns['posts'] );
+ }
+
+ return $columns;
+ }
+
+ /**
+ * Gets a list of sortable columns for the list table.
+ *
+ * @since 3.1.0
+ *
+ * @return array Array of sortable columns.
+ */
+ protected function get_sortable_columns() {
+ $columns = array(
+ 'username' => array( 'login', false, __( 'Username' ), __( 'Table ordered by Username.' ), 'asc' ),
+ 'email' => array( 'email', false, __( 'E-mail' ), __( 'Table ordered by E-mail.' ) ),
+ );
+
+ return $columns;
+ }
+
+ /**
+ * Generates the list table rows.
+ *
+ * @since 3.1.0
+ */
+ public function display_rows() {
+ // Query the post counts for this page.
+ if ( ! $this->is_site_users ) {
+ $post_counts = count_many_users_posts( array_keys( $this->items ) );
+ }
+
+ foreach ( $this->items as $userid => $user_object ) {
+ echo "\n\t" . $this->single_row( $user_object, '', '', isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
+ }
+ }
+
+ /**
+ * Generates HTML for a single row on the users.php admin panel.
+ *
+ * @since 3.1.0
+ * @since 4.2.0 The `$style` parameter was deprecated.
+ * @since 4.4.0 The `$role` parameter was deprecated.
+ *
+ * @param WP_User $user_object The current user object.
+ * @param string $style Deprecated. Not used.
+ * @param string $role Deprecated. Not used.
+ * @param int $numposts Optional. Post count to display for this user. Defaults
+ * to zero, as in, a new user has made zero posts.
+ * @return string Output for a single row.
+ */
+ public function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
+ if ( ! ( $user_object instanceof WP_User ) ) {
+ $user_object = get_userdata( (int) $user_object );
+ }
+ $user_object->filter = 'display';
+ $email = $user_object->user_email;
+
+ if ( $this->is_site_users ) {
+ $url = "site-users.php?id={$this->site_id}&";
+ } else {
+ $url = 'users.php?';
+ }
+
+ $user_roles = $this->get_role_list( $user_object );
+
+ // Set up the hover actions for this user.
+ $actions = array();
+ $checkbox = '';
+ $super_admin = '';
+
+ if ( is_multisite() && current_user_can( 'manage_network_users' ) ) {
+ if ( in_array( $user_object->user_login, get_super_admins(), true ) ) {
+ $super_admin = ' — ' . __( 'Super Admin' );
+ }
+ }
+
+ // Check if the user for this row is editable.
+ if ( current_user_can( 'list_users' ) ) {
+ // Set up the user editing link.
+ $edit_link = esc_url(
+ add_query_arg(
+ 'wp_http_referer',
+ urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
+ get_edit_user_link( $user_object->ID )
+ )
+ );
+
+ if ( current_user_can( 'edit_user', $user_object->ID ) ) {
+ $edit = "
{$user_object->user_login} {$super_admin}";
+ $actions['edit'] = '
' . __( 'Edit' ) . ' ';
+ } else {
+ $edit = "
{$user_object->user_login}{$super_admin} ";
+ }
+
+ if ( ! is_multisite()
+ && get_current_user_id() !== $user_object->ID
+ && current_user_can( 'delete_user', $user_object->ID )
+ ) {
+ $actions['delete'] = "
" . __( 'Delete' ) . ' ';
+ }
+
+ if ( is_multisite()
+ && current_user_can( 'remove_user', $user_object->ID )
+ ) {
+ $actions['remove'] = "
" . __( 'Remove' ) . ' ';
+ }
+
+ // Add a link to the user's author archive, if not empty.
+ $author_posts_url = get_author_posts_url( $user_object->ID );
+ if ( $author_posts_url ) {
+ $actions['view'] = sprintf(
+ '
%s ',
+ esc_url( $author_posts_url ),
+ /* translators: %s: Author's display name. */
+ esc_attr( sprintf( __( 'View posts by %s' ), $user_object->display_name ) ),
+ __( 'View' )
+ );
+ }
+
+ // Add a link to send the user a reset password link by email.
+ if ( get_current_user_id() !== $user_object->ID
+ && current_user_can( 'edit_user', $user_object->ID )
+ && true === wp_is_password_reset_allowed_for_user( $user_object )
+ ) {
+ $actions['resetpassword'] = "
" . __( 'Send password reset' ) . ' ';
+ }
+
+ /**
+ * Filters the action links displayed under each user in the Users list table.
+ *
+ * @since 2.8.0
+ *
+ * @param string[] $actions An array of action links to be displayed.
+ * Default 'Edit', 'Delete' for single site, and
+ * 'Edit', 'Remove' for Multisite.
+ * @param WP_User $user_object WP_User object for the currently listed user.
+ */
+ $actions = apply_filters( 'user_row_actions', $actions, $user_object );
+
+ // Role classes.
+ $role_classes = esc_attr( implode( ' ', array_keys( $user_roles ) ) );
+
+ // Set up the checkbox (because the user is editable, otherwise it's empty).
+ $checkbox = sprintf(
+ '
' .
+ '
%3$s ',
+ $user_object->ID,
+ $role_classes,
+ /* translators: Hidden accessibility text. %s: User login. */
+ sprintf( __( 'Select %s' ), $user_object->user_login )
+ );
+
+ } else {
+ $edit = "
{$user_object->user_login}{$super_admin} ";
+ }
+
+ $avatar = get_avatar( $user_object->ID, 32 );
+
+ // Comma-separated list of user roles.
+ $roles_list = implode( ', ', $user_roles );
+
+ $row = "
";
+
+ list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
+
+ foreach ( $columns as $column_name => $column_display_name ) {
+ $classes = "$column_name column-$column_name";
+ if ( $primary === $column_name ) {
+ $classes .= ' has-row-actions column-primary';
+ }
+ if ( 'posts' === $column_name ) {
+ $classes .= ' num'; // Special case for that column.
+ }
+
+ if ( in_array( $column_name, $hidden, true ) ) {
+ $classes .= ' hidden';
+ }
+
+ $data = 'data-colname="' . esc_attr( wp_strip_all_tags( $column_display_name ) ) . '"';
+
+ $attributes = "class='$classes' $data";
+
+ if ( 'cb' === $column_name ) {
+ $row .= "$checkbox ";
+ } else {
+ $row .= "";
+ switch ( $column_name ) {
+ case 'username':
+ $row .= "$avatar $edit";
+ break;
+ case 'name':
+ if ( $user_object->first_name && $user_object->last_name ) {
+ $row .= sprintf(
+ /* translators: 1: User's first name, 2: Last name. */
+ _x( '%1$s %2$s', 'Display name based on first name and last name' ),
+ $user_object->first_name,
+ $user_object->last_name
+ );
+ } elseif ( $user_object->first_name ) {
+ $row .= $user_object->first_name;
+ } elseif ( $user_object->last_name ) {
+ $row .= $user_object->last_name;
+ } else {
+ $row .= sprintf(
+ '— %s ',
+ /* translators: Hidden accessibility text. */
+ _x( 'Unknown', 'name' )
+ );
+ }
+ break;
+ case 'email':
+ $row .= "$email ";
+ break;
+ case 'role':
+ $row .= esc_html( $roles_list );
+ break;
+ case 'posts':
+ if ( $numposts > 0 ) {
+ $row .= sprintf(
+ '%s %s ',
+ "edit.php?author={$user_object->ID}",
+ $numposts,
+ sprintf(
+ /* translators: Hidden accessibility text. %s: Number of posts. */
+ _n( '%s post by this author', '%s posts by this author', $numposts ),
+ number_format_i18n( $numposts )
+ )
+ );
+ } else {
+ $row .= 0;
+ }
+ break;
+ default:
+ /**
+ * Filters the display output of custom columns in the Users list table.
+ *
+ * @since 2.8.0
+ *
+ * @param string $output Custom column output. Default empty.
+ * @param string $column_name Column name.
+ * @param int $user_id ID of the currently-listed user.
+ */
+ $row .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
+ }
+
+ if ( $primary === $column_name ) {
+ $row .= $this->row_actions( $actions );
+ }
+ $row .= ' ';
+ }
+ }
+ $row .= ' ';
+
+ return $row;
+ }
+
+ /**
+ * Gets the name of the default primary column.
+ *
+ * @since 4.3.0
+ *
+ * @return string Name of the default primary column, in this case, 'username'.
+ */
+ protected function get_default_primary_column_name() {
+ return 'username';
+ }
+
+ /**
+ * Returns an array of translated user role names for a given user object.
+ *
+ * @since 4.4.0
+ *
+ * @param WP_User $user_object The WP_User object.
+ * @return string[] An array of user role names keyed by role.
+ */
+ protected function get_role_list( $user_object ) {
+ $wp_roles = wp_roles();
+
+ $role_list = array();
+
+ foreach ( $user_object->roles as $role ) {
+ if ( isset( $wp_roles->role_names[ $role ] ) ) {
+ $role_list[ $role ] = translate_user_role( $wp_roles->role_names[ $role ] );
+ }
+ }
+
+ if ( empty( $role_list ) ) {
+ $role_list['none'] = _x( 'None', 'no user roles' );
+ }
+
+ /**
+ * Filters the returned array of translated role names for a user.
+ *
+ * @since 4.4.0
+ *
+ * @param string[] $role_list An array of translated user role names keyed by role.
+ * @param WP_User $user_object A WP_User object.
+ */
+ return apply_filters( 'get_role_list', $role_list, $user_object );
+ }
+}
diff --git a/wp-admin/includes/comment.php b/wp-admin/includes/comment.php
new file mode 100644
index 0000000..ffec90c
--- /dev/null
+++ b/wp-admin/includes/comment.php
@@ -0,0 +1,219 @@
+get_var(
+ $wpdb->prepare(
+ "SELECT comment_post_ID FROM $wpdb->comments
+ WHERE comment_author = %s AND $date_field = %s",
+ stripslashes( $comment_author ),
+ stripslashes( $comment_date )
+ )
+ );
+}
+
+/**
+ * Updates a comment with values provided in $_POST.
+ *
+ * @since 2.0.0
+ * @since 5.5.0 A return value was added.
+ *
+ * @return int|WP_Error The value 1 if the comment was updated, 0 if not updated.
+ * A WP_Error object on failure.
+ */
+function edit_comment() {
+ if ( ! current_user_can( 'edit_comment', (int) $_POST['comment_ID'] ) ) {
+ wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) );
+ }
+
+ if ( isset( $_POST['newcomment_author'] ) ) {
+ $_POST['comment_author'] = $_POST['newcomment_author'];
+ }
+ if ( isset( $_POST['newcomment_author_email'] ) ) {
+ $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
+ }
+ if ( isset( $_POST['newcomment_author_url'] ) ) {
+ $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
+ }
+ if ( isset( $_POST['comment_status'] ) ) {
+ $_POST['comment_approved'] = $_POST['comment_status'];
+ }
+ if ( isset( $_POST['content'] ) ) {
+ $_POST['comment_content'] = $_POST['content'];
+ }
+ if ( isset( $_POST['comment_ID'] ) ) {
+ $_POST['comment_ID'] = (int) $_POST['comment_ID'];
+ }
+
+ foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
+ if ( ! empty( $_POST[ 'hidden_' . $timeunit ] ) && $_POST[ 'hidden_' . $timeunit ] !== $_POST[ $timeunit ] ) {
+ $_POST['edit_date'] = '1';
+ break;
+ }
+ }
+
+ if ( ! empty( $_POST['edit_date'] ) ) {
+ $aa = $_POST['aa'];
+ $mm = $_POST['mm'];
+ $jj = $_POST['jj'];
+ $hh = $_POST['hh'];
+ $mn = $_POST['mn'];
+ $ss = $_POST['ss'];
+ $jj = ( $jj > 31 ) ? 31 : $jj;
+ $hh = ( $hh > 23 ) ? $hh - 24 : $hh;
+ $mn = ( $mn > 59 ) ? $mn - 60 : $mn;
+ $ss = ( $ss > 59 ) ? $ss - 60 : $ss;
+
+ $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
+ }
+
+ return wp_update_comment( $_POST, true );
+}
+
+/**
+ * Returns a WP_Comment object based on comment ID.
+ *
+ * @since 2.0.0
+ *
+ * @param int $id ID of comment to retrieve.
+ * @return WP_Comment|false Comment if found. False on failure.
+ */
+function get_comment_to_edit( $id ) {
+ $comment = get_comment( $id );
+ if ( ! $comment ) {
+ return false;
+ }
+
+ $comment->comment_ID = (int) $comment->comment_ID;
+ $comment->comment_post_ID = (int) $comment->comment_post_ID;
+
+ $comment->comment_content = format_to_edit( $comment->comment_content );
+ /**
+ * Filters the comment content before editing.
+ *
+ * @since 2.0.0
+ *
+ * @param string $comment_content Comment content.
+ */
+ $comment->comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
+
+ $comment->comment_author = format_to_edit( $comment->comment_author );
+ $comment->comment_author_email = format_to_edit( $comment->comment_author_email );
+ $comment->comment_author_url = format_to_edit( $comment->comment_author_url );
+ $comment->comment_author_url = esc_url( $comment->comment_author_url );
+
+ return $comment;
+}
+
+/**
+ * Gets the number of pending comments on a post or posts.
+ *
+ * @since 2.3.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int|int[] $post_id Either a single Post ID or an array of Post IDs
+ * @return int|int[] Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs
+ */
+function get_pending_comments_num( $post_id ) {
+ global $wpdb;
+
+ $single = false;
+ if ( ! is_array( $post_id ) ) {
+ $post_id_array = (array) $post_id;
+ $single = true;
+ } else {
+ $post_id_array = $post_id;
+ }
+ $post_id_array = array_map( 'intval', $post_id_array );
+ $post_id_in = "'" . implode( "', '", $post_id_array ) . "'";
+
+ $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_A );
+
+ if ( $single ) {
+ if ( empty( $pending ) ) {
+ return 0;
+ } else {
+ return absint( $pending[0]['num_comments'] );
+ }
+ }
+
+ $pending_keyed = array();
+
+ // Default to zero pending for all posts in request.
+ foreach ( $post_id_array as $id ) {
+ $pending_keyed[ $id ] = 0;
+ }
+
+ if ( ! empty( $pending ) ) {
+ foreach ( $pending as $pend ) {
+ $pending_keyed[ $pend['comment_post_ID'] ] = absint( $pend['num_comments'] );
+ }
+ }
+
+ return $pending_keyed;
+}
+
+/**
+ * Adds avatars to relevant places in admin.
+ *
+ * @since 2.5.0
+ *
+ * @param string $name User name.
+ * @return string Avatar with the user name.
+ */
+function floated_admin_avatar( $name ) {
+ $avatar = get_avatar( get_comment(), 32, 'mystery' );
+ return "$avatar $name";
+}
+
+/**
+ * Enqueues comment shortcuts jQuery script.
+ *
+ * @since 2.7.0
+ */
+function enqueue_comment_hotkeys_js() {
+ if ( 'true' === get_user_option( 'comment_shortcuts' ) ) {
+ wp_enqueue_script( 'jquery-table-hotkeys' );
+ }
+}
+
+/**
+ * Displays error message at bottom of comments.
+ *
+ * @param string $msg Error Message. Assumed to contain HTML and be sanitized.
+ */
+function comment_footer_die( $msg ) {
+ echo "
";
+ require_once ABSPATH . 'wp-admin/admin-footer.php';
+ die;
+}
diff --git a/wp-admin/includes/continents-cities.php b/wp-admin/includes/continents-cities.php
new file mode 100644
index 0000000..cb8e033
--- /dev/null
+++ b/wp-admin/includes/continents-cities.php
@@ -0,0 +1,549 @@
+ 'WordPress/' . $version . '; ' . home_url( '/' ) );
+
+ if ( wp_http_supports( array( 'ssl' ) ) ) {
+ $url = set_url_scheme( $url, 'https' );
+ }
+
+ $response = wp_remote_get( $url, $options );
+
+ if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ return false;
+ }
+
+ $results = json_decode( wp_remote_retrieve_body( $response ), true );
+
+ if ( ! is_array( $results ) ) {
+ return false;
+ }
+
+ set_site_transient( 'wordpress_credits_' . $locale, $results, DAY_IN_SECONDS );
+ }
+
+ return $results;
+}
+
+/**
+ * Retrieves the link to a contributor's WordPress.org profile page.
+ *
+ * @access private
+ * @since 3.2.0
+ *
+ * @param string $display_name The contributor's display name (passed by reference).
+ * @param string $username The contributor's username.
+ * @param string $profiles URL to the contributor's WordPress.org profile page.
+ */
+function _wp_credits_add_profile_link( &$display_name, $username, $profiles ) {
+ $display_name = '
' . esc_html( $display_name ) . ' ';
+}
+
+/**
+ * Retrieves the link to an external library used in WordPress.
+ *
+ * @access private
+ * @since 3.2.0
+ *
+ * @param string $data External library data (passed by reference).
+ */
+function _wp_credits_build_object_link( &$data ) {
+ $data = '
' . esc_html( $data[0] ) . ' ';
+}
+
+/**
+ * Displays the title for a given group of contributors.
+ *
+ * @since 5.3.0
+ *
+ * @param array $group_data The current contributor group.
+ */
+function wp_credits_section_title( $group_data = array() ) {
+ if ( ! count( $group_data ) ) {
+ return;
+ }
+
+ if ( $group_data['name'] ) {
+ if ( 'Translators' === $group_data['name'] ) {
+ // Considered a special slug in the API response. (Also, will never be returned for en_US.)
+ $title = _x( 'Translators', 'Translate this to be the equivalent of English Translators in your language for the credits page Translators section' );
+ } elseif ( isset( $group_data['placeholders'] ) ) {
+ // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
+ $title = vsprintf( translate( $group_data['name'] ), $group_data['placeholders'] );
+ } else {
+ // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
+ $title = translate( $group_data['name'] );
+ }
+
+ echo '
' . esc_html( $title ) . " \n";
+ }
+}
+
+/**
+ * Displays a list of contributors for a given group.
+ *
+ * @since 5.3.0
+ *
+ * @param array $credits The credits groups returned from the API.
+ * @param string $slug The current group to display.
+ */
+function wp_credits_section_list( $credits = array(), $slug = '' ) {
+ $group_data = isset( $credits['groups'][ $slug ] ) ? $credits['groups'][ $slug ] : array();
+ $credits_data = $credits['data'];
+ if ( ! count( $group_data ) ) {
+ return;
+ }
+
+ if ( ! empty( $group_data['shuffle'] ) ) {
+ shuffle( $group_data['data'] ); // We were going to sort by ability to pronounce "hierarchical," but that wouldn't be fair to Matt.
+ }
+
+ switch ( $group_data['type'] ) {
+ case 'list':
+ array_walk( $group_data['data'], '_wp_credits_add_profile_link', $credits_data['profiles'] );
+ echo '
' . wp_sprintf( '%l.', $group_data['data'] ) . "
\n\n";
+ break;
+ case 'libraries':
+ array_walk( $group_data['data'], '_wp_credits_build_object_link' );
+ echo '
' . wp_sprintf( '%l.', $group_data['data'] ) . "
\n\n";
+ break;
+ default:
+ $compact = 'compact' === $group_data['type'];
+ $classes = 'wp-people-group ' . ( $compact ? 'compact' : '' );
+ echo '
\n";
+ break;
+ }
+}
diff --git a/wp-admin/includes/dashboard.php b/wp-admin/includes/dashboard.php
new file mode 100644
index 0000000..5b50423
--- /dev/null
+++ b/wp-admin/includes/dashboard.php
@@ -0,0 +1,2137 @@
+cap->create_posts ) ) {
+ $quick_draft_title = sprintf( '
%1$s %2$s ', __( 'Quick Draft' ), __( 'Your Recent Drafts' ) );
+ wp_add_dashboard_widget( 'dashboard_quick_press', $quick_draft_title, 'wp_dashboard_quick_press' );
+ }
+
+ // WordPress Events and News.
+ wp_add_dashboard_widget( 'dashboard_primary', __( 'WordPress Events and News' ), 'wp_dashboard_events_news' );
+
+ if ( is_network_admin() ) {
+
+ /**
+ * Fires after core widgets for the Network Admin dashboard have been registered.
+ *
+ * @since 3.1.0
+ */
+ do_action( 'wp_network_dashboard_setup' );
+
+ /**
+ * Filters the list of widgets to load for the Network Admin dashboard.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $dashboard_widgets An array of dashboard widget IDs.
+ */
+ $dashboard_widgets = apply_filters( 'wp_network_dashboard_widgets', array() );
+ } elseif ( is_user_admin() ) {
+
+ /**
+ * Fires after core widgets for the User Admin dashboard have been registered.
+ *
+ * @since 3.1.0
+ */
+ do_action( 'wp_user_dashboard_setup' );
+
+ /**
+ * Filters the list of widgets to load for the User Admin dashboard.
+ *
+ * @since 3.1.0
+ *
+ * @param string[] $dashboard_widgets An array of dashboard widget IDs.
+ */
+ $dashboard_widgets = apply_filters( 'wp_user_dashboard_widgets', array() );
+ } else {
+
+ /**
+ * Fires after core widgets for the admin dashboard have been registered.
+ *
+ * @since 2.5.0
+ */
+ do_action( 'wp_dashboard_setup' );
+
+ /**
+ * Filters the list of widgets to load for the admin dashboard.
+ *
+ * @since 2.5.0
+ *
+ * @param string[] $dashboard_widgets An array of dashboard widget IDs.
+ */
+ $dashboard_widgets = apply_filters( 'wp_dashboard_widgets', array() );
+ }
+
+ foreach ( $dashboard_widgets as $widget_id ) {
+ $name = empty( $wp_registered_widgets[ $widget_id ]['all_link'] ) ? $wp_registered_widgets[ $widget_id ]['name'] : $wp_registered_widgets[ $widget_id ]['name'] . "
" . __( 'View all' ) . ' ';
+ wp_add_dashboard_widget( $widget_id, $name, $wp_registered_widgets[ $widget_id ]['callback'], $wp_registered_widget_controls[ $widget_id ]['callback'] );
+ }
+
+ if ( 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['widget_id'] ) ) {
+ check_admin_referer( 'edit-dashboard-widget_' . $_POST['widget_id'], 'dashboard-widget-nonce' );
+ ob_start(); // Hack - but the same hack wp-admin/widgets.php uses.
+ wp_dashboard_trigger_widget_control( $_POST['widget_id'] );
+ ob_end_clean();
+ wp_redirect( remove_query_arg( 'edit' ) );
+ exit;
+ }
+
+ /** This action is documented in wp-admin/includes/meta-boxes.php */
+ do_action( 'do_meta_boxes', $screen->id, 'normal', '' );
+
+ /** This action is documented in wp-admin/includes/meta-boxes.php */
+ do_action( 'do_meta_boxes', $screen->id, 'side', '' );
+}
+
+/**
+ * Adds a new dashboard widget.
+ *
+ * @since 2.7.0
+ * @since 5.6.0 The `$context` and `$priority` parameters were added.
+ *
+ * @global callable[] $wp_dashboard_control_callbacks
+ *
+ * @param string $widget_id Widget ID (used in the 'id' attribute for the widget).
+ * @param string $widget_name Title of the widget.
+ * @param callable $callback Function that fills the widget with the desired content.
+ * The function should echo its output.
+ * @param callable $control_callback Optional. Function that outputs controls for the widget. Default null.
+ * @param array $callback_args Optional. Data that should be set as the $args property of the widget array
+ * (which is the second parameter passed to your callback). Default null.
+ * @param string $context Optional. The context within the screen where the box should display.
+ * Accepts 'normal', 'side', 'column3', or 'column4'. Default 'normal'.
+ * @param string $priority Optional. The priority within the context where the box should show.
+ * Accepts 'high', 'core', 'default', or 'low'. Default 'core'.
+ */
+function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null, $callback_args = null, $context = 'normal', $priority = 'core' ) {
+ global $wp_dashboard_control_callbacks;
+
+ $screen = get_current_screen();
+
+ $private_callback_args = array( '__widget_basename' => $widget_name );
+
+ if ( is_null( $callback_args ) ) {
+ $callback_args = $private_callback_args;
+ } elseif ( is_array( $callback_args ) ) {
+ $callback_args = array_merge( $callback_args, $private_callback_args );
+ }
+
+ if ( $control_callback && is_callable( $control_callback ) && current_user_can( 'edit_dashboard' ) ) {
+ $wp_dashboard_control_callbacks[ $widget_id ] = $control_callback;
+
+ if ( isset( $_GET['edit'] ) && $widget_id === $_GET['edit'] ) {
+ list($url) = explode( '#', add_query_arg( 'edit', false ), 2 );
+ $widget_name .= '
' . __( 'Cancel' ) . ' ';
+ $callback = '_wp_dashboard_control_callback';
+ } else {
+ list($url) = explode( '#', add_query_arg( 'edit', $widget_id ), 2 );
+ $widget_name .= '
' . __( 'Configure' ) . ' ';
+ }
+ }
+
+ $side_widgets = array( 'dashboard_quick_press', 'dashboard_primary' );
+
+ if ( in_array( $widget_id, $side_widgets, true ) ) {
+ $context = 'side';
+ }
+
+ $high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' );
+
+ if ( in_array( $widget_id, $high_priority_widgets, true ) ) {
+ $priority = 'high';
+ }
+
+ if ( empty( $context ) ) {
+ $context = 'normal';
+ }
+
+ if ( empty( $priority ) ) {
+ $priority = 'core';
+ }
+
+ add_meta_box( $widget_id, $widget_name, $callback, $screen, $context, $priority, $callback_args );
+}
+
+/**
+ * Outputs controls for the current dashboard widget.
+ *
+ * @access private
+ * @since 2.7.0
+ *
+ * @param mixed $dashboard
+ * @param array $meta_box
+ */
+function _wp_dashboard_control_callback( $dashboard, $meta_box ) {
+ echo '
';
+ wp_dashboard_trigger_widget_control( $meta_box['id'] );
+ wp_nonce_field( 'edit-dashboard-widget_' . $meta_box['id'], 'dashboard-widget-nonce' );
+ echo ' ';
+ submit_button( __( 'Save Changes' ) );
+ echo ' ';
+}
+
+/**
+ * Displays the dashboard.
+ *
+ * @since 2.5.0
+ */
+function wp_dashboard() {
+ $screen = get_current_screen();
+ $columns = absint( $screen->get_columns() );
+ $columns_css = '';
+
+ if ( $columns ) {
+ $columns_css = " columns-$columns";
+ }
+ ?>
+
+
+
+
+
+ publish ) {
+ if ( 'post' === $post_type ) {
+ /* translators: %s: Number of posts. */
+ $text = _n( '%s Post', '%s Posts', $num_posts->publish );
+ } else {
+ /* translators: %s: Number of pages. */
+ $text = _n( '%s Page', '%s Pages', $num_posts->publish );
+ }
+
+ $text = sprintf( $text, number_format_i18n( $num_posts->publish ) );
+ $post_type_object = get_post_type_object( $post_type );
+
+ if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) {
+ printf( '%2$s ', $post_type, $text );
+ } else {
+ printf( '%2$s ', $post_type, $text );
+ }
+ }
+ }
+
+ // Comments.
+ $num_comm = wp_count_comments();
+
+ if ( $num_comm && ( $num_comm->approved || $num_comm->moderated ) ) {
+ /* translators: %s: Number of comments. */
+ $text = sprintf( _n( '%s Comment', '%s Comments', $num_comm->approved ), number_format_i18n( $num_comm->approved ) );
+ ?>
+
+ moderated );
+ /* translators: %s: Number of comments. */
+ $text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $num_comm->moderated ), $moderated_comments_count_i18n );
+ ?>
+
+ ' . implode( "\n", $elements ) . " \n";
+ }
+
+ ?>
+
+
$content ";
+ }
+ ?>
+
+
+
+
+
+ ' . __( 'Create a New Site' ) . '';
+ }
+ if ( current_user_can( 'create_users' ) ) {
+ $actions['create-user'] = '
' . __( 'Create a New User' ) . ' ';
+ }
+
+ $c_users = get_user_count();
+ $c_blogs = get_blog_count();
+
+ /* translators: %s: Number of users on the network. */
+ $user_text = sprintf( _n( '%s user', '%s users', $c_users ), number_format_i18n( $c_users ) );
+ /* translators: %s: Number of sites on the network. */
+ $blog_text = sprintf( _n( '%s site', '%s sites', $c_blogs ), number_format_i18n( $c_blogs ) );
+
+ /* translators: 1: Text indicating the number of sites on the network, 2: Text indicating the number of users on the network. */
+ $sentence = sprintf( __( 'You have %1$s and %2$s.' ), $blog_text, $user_text );
+
+ if ( $actions ) {
+ echo '
';
+ foreach ( $actions as $class => $action ) {
+ $actions[ $class ] = "\t$action";
+ }
+ echo implode( " | \n", $actions ) . "\n";
+ echo ' ';
+ }
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 'submit_users' ) ); ?>
+
+
+
+
+
+
+
+
+
+ 'submit_sites' ) ); ?>
+
+
+ post_status ) { // auto-draft doesn't exist anymore.
+ $post = get_default_post_to_edit( 'post', true );
+ update_user_option( get_current_user_id(), 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID.
+ } else {
+ $post->post_title = ''; // Remove the auto draft title.
+ }
+ } else {
+ $post = get_default_post_to_edit( 'post', true );
+ $user_id = get_current_user_id();
+
+ // Don't create an option if this is a super admin who does not belong to this site.
+ if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ), true ) ) {
+ update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID.
+ }
+ }
+
+ $post_ID = (int) $post->ID;
+ ?>
+
+
+
+ array( 'error' ),
+ )
+ );
+ }
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 'save-post' ) ); ?>
+
+
+
+
+ 'post',
+ 'post_status' => 'draft',
+ 'author' => get_current_user_id(),
+ 'posts_per_page' => 4,
+ 'orderby' => 'modified',
+ 'order' => 'DESC',
+ );
+
+ /**
+ * Filters the post query arguments for the 'Recent Drafts' dashboard widget.
+ *
+ * @since 4.4.0
+ *
+ * @param array $query_args The query arguments for the 'Recent Drafts' dashboard widget.
+ */
+ $query_args = apply_filters( 'dashboard_recent_drafts_query_args', $query_args );
+
+ $drafts = get_posts( $query_args );
+ if ( ! $drafts ) {
+ return;
+ }
+ }
+
+ echo '
';
+
+ if ( count( $drafts ) > 3 ) {
+ printf(
+ '
%s
' . "\n",
+ esc_url( admin_url( 'edit.php?post_status=draft' ) ),
+ __( 'View all drafts' )
+ );
+ }
+
+ echo '
' . __( 'Your Recent Drafts' ) . " \n";
+ echo '
';
+
+ /* translators: Maximum number of words used in a preview of a draft on the dashboard. */
+ $draft_length = (int) _x( '10', 'draft_length' );
+
+ $drafts = array_slice( $drafts, 0, 3 );
+ foreach ( $drafts as $draft ) {
+ $url = get_edit_post_link( $draft->ID );
+ $title = _draft_or_post_title( $draft->ID );
+
+ echo "\n";
+ printf(
+ '',
+ esc_url( $url ),
+ /* translators: %s: Post title. */
+ esc_attr( sprintf( __( 'Edit “%s”' ), $title ) ),
+ esc_html( $title ),
+ get_the_time( 'c', $draft ),
+ get_the_time( __( 'F j, Y' ), $draft )
+ );
+
+ $the_content = wp_trim_words( $draft->post_content, $draft_length );
+
+ if ( $the_content ) {
+ echo '' . $the_content . '
';
+ }
+ echo " \n";
+ }
+
+ echo " \n";
+ echo '
';
+}
+
+/**
+ * Outputs a row for the Recent Comments widget.
+ *
+ * @access private
+ * @since 2.7.0
+ *
+ * @global WP_Comment $comment Global comment object.
+ *
+ * @param WP_Comment $comment The current comment.
+ * @param bool $show_date Optional. Whether to display the date.
+ */
+function _wp_dashboard_recent_comments_row( &$comment, $show_date = true ) {
+ $GLOBALS['comment'] = clone $comment;
+
+ if ( $comment->comment_post_ID > 0 ) {
+ $comment_post_title = _draft_or_post_title( $comment->comment_post_ID );
+ $comment_post_url = get_the_permalink( $comment->comment_post_ID );
+ $comment_post_link = '
' . $comment_post_title . ' ';
+ } else {
+ $comment_post_link = '';
+ }
+
+ $actions_string = '';
+ if ( current_user_can( 'edit_comment', $comment->comment_ID ) ) {
+ // Pre-order it: Approve | Reply | Edit | Spam | Trash.
+ $actions = array(
+ 'approve' => '',
+ 'unapprove' => '',
+ 'reply' => '',
+ 'edit' => '',
+ 'spam' => '',
+ 'trash' => '',
+ 'delete' => '',
+ 'view' => '',
+ );
+
+ $del_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "delete-comment_$comment->comment_ID" ) );
+ $approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "approve-comment_$comment->comment_ID" ) );
+
+ $approve_url = esc_url( "comment.php?action=approvecomment&p=$comment->comment_post_ID&c=$comment->comment_ID&$approve_nonce" );
+ $unapprove_url = esc_url( "comment.php?action=unapprovecomment&p=$comment->comment_post_ID&c=$comment->comment_ID&$approve_nonce" );
+ $spam_url = esc_url( "comment.php?action=spamcomment&p=$comment->comment_post_ID&c=$comment->comment_ID&$del_nonce" );
+ $trash_url = esc_url( "comment.php?action=trashcomment&p=$comment->comment_post_ID&c=$comment->comment_ID&$del_nonce" );
+ $delete_url = esc_url( "comment.php?action=deletecomment&p=$comment->comment_post_ID&c=$comment->comment_ID&$del_nonce" );
+
+ $actions['approve'] = sprintf(
+ '
%s ',
+ $approve_url,
+ "dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved",
+ esc_attr__( 'Approve this comment' ),
+ __( 'Approve' )
+ );
+
+ $actions['unapprove'] = sprintf(
+ '
%s ',
+ $unapprove_url,
+ "dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=unapproved",
+ esc_attr__( 'Unapprove this comment' ),
+ __( 'Unapprove' )
+ );
+
+ $actions['edit'] = sprintf(
+ '
%s ',
+ "comment.php?action=editcomment&c={$comment->comment_ID}",
+ esc_attr__( 'Edit this comment' ),
+ __( 'Edit' )
+ );
+
+ $actions['reply'] = sprintf(
+ '
%s ',
+ $comment->comment_ID,
+ $comment->comment_post_ID,
+ esc_attr__( 'Reply to this comment' ),
+ __( 'Reply' )
+ );
+
+ $actions['spam'] = sprintf(
+ '
%s ',
+ $spam_url,
+ "delete:the-comment-list:comment-{$comment->comment_ID}::spam=1",
+ esc_attr__( 'Mark this comment as spam' ),
+ /* translators: "Mark as spam" link. */
+ _x( 'Spam', 'verb' )
+ );
+
+ if ( ! EMPTY_TRASH_DAYS ) {
+ $actions['delete'] = sprintf(
+ '
%s ',
+ $delete_url,
+ "delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
+ esc_attr__( 'Delete this comment permanently' ),
+ __( 'Delete Permanently' )
+ );
+ } else {
+ $actions['trash'] = sprintf(
+ '
%s ',
+ $trash_url,
+ "delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
+ esc_attr__( 'Move this comment to the Trash' ),
+ _x( 'Trash', 'verb' )
+ );
+ }
+
+ $actions['view'] = sprintf(
+ '',
+ esc_url( get_comment_link( $comment ) ),
+ esc_attr__( 'View this comment' ),
+ __( 'View' )
+ );
+
+ /**
+ * Filters the action links displayed for each comment in the 'Recent Comments'
+ * dashboard widget.
+ *
+ * @since 2.6.0
+ *
+ * @param string[] $actions An array of comment actions. Default actions include:
+ * 'Approve', 'Unapprove', 'Edit', 'Reply', 'Spam',
+ * 'Delete', and 'Trash'.
+ * @param WP_Comment $comment The comment object.
+ */
+ $actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );
+
+ $i = 0;
+
+ foreach ( $actions as $action => $link ) {
+ ++$i;
+
+ if ( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i )
+ || 1 === $i
+ ) {
+ $separator = '';
+ } else {
+ $separator = ' | ';
+ }
+
+ // Reply and quickedit need a hide-if-no-js span.
+ if ( 'reply' === $action || 'quickedit' === $action ) {
+ $action .= ' hide-if-no-js';
+ }
+
+ if ( 'view' === $action && '1' !== $comment->comment_approved ) {
+ $action .= ' hidden';
+ }
+
+ $actions_string .= "
{$separator}{$link} ";
+ }
+ }
+ ?>
+
+