diff --git a/functions.php b/functions.php new file mode 100755 index 0000000..8614da9 --- /dev/null +++ b/functions.php @@ -0,0 +1,1246 @@ + '; + $autoupdate_text .= $update_message; + $autoupdate_text .= ' '; + $script .= 'jQuery(".check-column input[value=\'' . $theme . '\']").closest("tr").find(".plugin-title > p").append(\'' . $autoupdate_text . '\');'; + } + } + + if ( wp_autoupdates_is_plugins_auto_update_enabled() ) { + $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); + + $update_message = wp_autoupdates_get_update_message(); + foreach ( $wp_auto_update_plugins as $plugin ) { + $autoupdate_text = ' '; + $autoupdate_text .= $update_message; + $autoupdate_text .= ' '; + $script .= 'jQuery(".check-column input[value=\'' . $plugin . '\']").closest("tr").find(".plugin-title > p").append(\'' . $autoupdate_text . '\');'; + } + } + $script .= '});'; + wp_add_inline_script( 'jquery', $script ); + } + + // When manually updating a plugin the 'time until next update' text needs to be hidden. + // Doesn't need to be done on the update-core.php page since that page refreshes after an update. + if ( 'plugins.php' === $hook ) { + $script = 'jQuery( document ).ready(function() { + jQuery( ".update-link" ).click( function() { + var plugin = jQuery( this ).closest("tr").data("plugin"); + var plugin_row = jQuery( "tr.update[data-plugin=\'" + plugin + "\']" ); + var plugin_auto_update_time_text = plugin_row.find("span.plugin-autoupdate-time"); + plugin_auto_update_time_text.remove(); + }); + });'; + wp_add_inline_script( 'jquery', $script ); + } + + if ( 'themes.php' === $hook ) { + if ( wp_autoupdates_is_themes_auto_update_enabled() ) { + $script = 'jQuery( document ).ready( function() {'; + + /* translators: %s: Theme name. */ + $aria_label_enable = sprintf( _x( 'Enable automatic update for %s', 'theme' ), '{{ data.name }}' ); + $aria_label_disable = sprintf( _x( 'Disable automatic update for %s', 'theme' ), '{{ data.name }}' ); + + // Put the enable/disable link below the author and before the update box. + $autoupdate_text = '
<# if ( data.autoupdate ) { #>'; + $autoupdate_text .= ''; + $autoupdate_text .= ' ' . __( 'Disable automatic updates' ) . ''; + $autoupdate_text .= ''; + $autoupdate_text .= '<# } else { #>'; + $autoupdate_text .= ''; + $autoupdate_text .= ' ' . __( 'Enable automatic updates' ) . ''; + $autoupdate_text .= ''; + $autoupdate_text .= '<# } #>
'; + + $script .= ' const theme_template_single = jQuery( "#tmpl-theme-single" ); + + // Pull template into new html element, manipulate, then put back. + // Props https://stackoverflow.com/a/42248980. + function insert_into_template(positioning_text, added_text, insert_before) { + var template_text = theme_template_single.text(); + var position = template_text.search(positioning_text); + if ( -1 !== position ) { + if ( true !== insert_before ) { + position += positioning_text.length; + } + + const new_template_text = template_text.substr(0, position) + added_text + template_text.substr(position); + theme_template_single.text( new_template_text ); + } + } + + const position_beginning_of_update_box = "<# if \\\\( data.hasUpdate \\\\) { #>"; + insert_into_template(position_beginning_of_update_box, "' . str_replace('"', '\"', $autoupdate_text) . '", true); + '; + + // Put the time until next update within the data.hasUpdate block. + $update_message = wp_autoupdates_get_update_message(); + $autoupdate_time_text = '<# if ( data.autoupdate ) { #>'; + $autoupdate_time_text .= '' . $update_message . '
'; + $autoupdate_time_text .= '<# } #>'; + + $script .= ' + const position_data_update = "{{{ data.update }}}"; + insert_into_template(position_data_update, "' . str_replace('"', '\"', $autoupdate_time_text) . '", false); + '; + + $script .= '});'; + wp_add_inline_script( 'jquery', $script ); + } + } +} +add_action( 'admin_enqueue_scripts', 'wp_autoupdates_enqueues' ); + + +/** + * Filter the themes prepared for JavaScript, for themes.php. + */ +function wp_autoupdates_prepare_themes_for_js( $prepared_themes ) { + $wp_auto_update_themes = get_option( 'wp_auto_update_themes', array() ); + foreach( $prepared_themes as $theme ) { + // Set extra data for use in the template. + $slug = $theme['id']; + $encoded_slug = urlencode( $slug ); + + $theme['autoupdate'] = in_array( $slug, $wp_auto_update_themes, true ); + $theme['actions']['autoupdate'] = current_user_can( 'update_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=autoupdate&theme=' . $encoded_slug ), 'autoupdate-theme_' . $slug ) : null; + + $prepared_themes[ $slug ] = $theme; + } + + return $prepared_themes; +} +add_action( 'wp_prepare_themes_for_js', 'wp_autoupdates_prepare_themes_for_js' ); + + +/** + * Checks whether plugins manual auto-update is enabled. + */ +function wp_autoupdates_is_plugins_auto_update_enabled() { + $enabled = ! defined( 'WP_DISABLE_PLUGINS_AUTO_UPDATE' ) || ! WP_DISABLE_PLUGINS_AUTO_UPDATE; + + /** + * Filters whether plugins manual auto-update is enabled. + * + * @param bool $enabled True if plugins auto-update is enabled, false otherwise. + */ + return apply_filters( 'wp_plugins_auto_update_enabled', $enabled ); +} + + +/** + * Checks whether themes manual auto-update is enabled. + */ +function wp_autoupdates_is_themes_auto_update_enabled() { + $enabled = ! defined( 'WP_DISABLE_THEMES_AUTO_UPDATE' ) || ! WP_DISABLE_THEMES_AUTO_UPDATE; + + /** + * Filters whether themes manual auto-update is enabled. + * + * @param bool $enabled True if themes auto-update is enabled, false otherwise. + */ + return apply_filters( 'wp_themes_auto_update_enabled', $enabled ); +} + + +/** + * Autoupdate selected plugins. + */ +function wp_autoupdates_selected_plugins( $update, $item ) { + $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); + if ( in_array( $item->plugin, $wp_auto_update_plugins, true ) && wp_autoupdates_is_plugins_auto_update_enabled() ) { + return true; + } else { + return $update; + } +} +add_filter( 'auto_update_plugin', 'wp_autoupdates_selected_plugins', 10, 2 ); + + +/** + * Autoupdate selected themes. + */ +function wp_autoupdates_selected_themes( $update, $item ) { + $wp_auto_update_themes = get_site_option( 'wp_auto_update_themes', array() ); + if ( in_array( $item->theme, $wp_auto_update_themes, true ) && wp_autoupdates_is_themes_auto_update_enabled() ) { + return true; + } else { + return $update; + } +} +add_filter( 'auto_update_theme', 'wp_autoupdates_selected_themes', 10, 2 ); + + +/** + * Add autoupdate column to plugins screen. + */ +function wp_autoupdates_add_plugins_autoupdates_column( $columns ) { + if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { + return $columns; + } + if ( ! isset( $_GET['plugin_status'] ) || ( 'mustuse' !== $_GET['plugin_status'] && 'dropins' !== $_GET['plugin_status'] ) ) { + $columns['autoupdates_column'] = __( 'Automatic updates', 'wp-autoupdates' ); + } + return $columns; +} +add_filter( is_multisite() ? 'manage_plugins-network_columns' : 'manage_plugins_columns', 'wp_autoupdates_add_plugins_autoupdates_column' ); + +/** + * Render autoupdate column’s content. + */ +function wp_autoupdates_add_plugins_autoupdates_column_content( $column_name, $plugin_file, $plugin_data ) { + if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { + return; + } + if ( 'autoupdates_column' !== $column_name ) { + return; + } + $plugins = get_plugins(); + $plugins_updates = get_site_transient( 'update_plugins' ); + $page = isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; + $plugin_status = isset( $_GET['plugin_status'] ) && ! empty( $_GET['plugin_status'] ) ? wp_unslash( esc_html( $_GET['plugin_status'] ) ) : ''; + if ( wp_autoupdates_is_plugins_auto_update_enabled() ) { + if ( ! isset( $plugins[ $plugin_file ] ) ) { + return; + } + $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); + if ( in_array( $plugin_file, $wp_auto_update_plugins, true ) ) { + $aria_label = esc_attr( + sprintf( + /* translators: Plugin name. */ + _x( 'Disable automatic updates for %s', 'plugin', 'wp-autoupdates' ), + esc_html( $plugins[ $plugin_file ]['Name'] ) + ) + ); + echo '';
+ echo '' . __( 'Auto-updates enabled', 'wp-autoupdates' ) . '';
+ echo '
';
+
+ $update_message = wp_autoupdates_get_update_message();
+ if ( isset( $plugins_updates->response[$plugin_file] ) ) {
+ echo '';
+ echo $update_message;
+ echo '
';
+ echo '';
+ }
+ if ( current_user_can( 'update_plugins', $plugin_file ) ) {
+ echo sprintf(
+ '%s',
+ wp_nonce_url( 'plugins.php?action=autoupdate&plugin=' . urlencode( $plugin_file ) . '&paged=' . $page . '&plugin_status=' . $plugin_status, 'autoupdate-plugin_' . $plugin_file ),
+ $aria_label,
+ __( 'Disable', 'wp-autoupdates' )
+ );
+ }
+ echo '
'; + echo sprintf( + ' %s', + wp_nonce_url( 'plugins.php?action=autoupdate&plugin=' . urlencode( $plugin_file ) . '&paged=' . $page . '&plugin_status=' . $plugin_status, 'autoupdate-plugin_' . $plugin_file ), + $aria_label, + __( 'Enable', 'wp-autoupdates' ) + ); + echo '
'; + } + } + } +} +add_action( 'manage_plugins_custom_column' , 'wp_autoupdates_add_plugins_autoupdates_column_content', 10, 3 ); + + +/** + * Add plugins autoupdates bulk actions + */ +function wp_autoupdates_plugins_bulk_actions( $actions ) { + $actions['enable-autoupdate-selected'] = __( 'Enable auto-updates', 'wp-autoupdates' ); + $actions['disable-autoupdate-selected'] = __( 'Disable auto-updates', 'wp-autoupdates' ); + return $actions; +} +add_action( 'bulk_actions-plugins', 'wp_autoupdates_plugins_bulk_actions' ); +add_action( 'bulk_actions-plugins-network', 'wp_autoupdates_plugins_bulk_actions' ); + + +/** + * Handles auto-updates enabling for plugins + */ +function wp_autoupdates_plugins_enabler() { + $action = isset( $_GET['action'] ) && ! empty( esc_html( $_GET['action'] ) ) ? wp_unslash( esc_html( $_GET['action'] ) ) : ''; + if ( 'autoupdate' === $action ) { + if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { + wp_die( __( 'Sorry, you are not allowed to enable plugins automatic updates.', 'wp-autoupdates' ) ); + } + + if ( is_multisite() && ! is_network_admin() ) { + wp_die( __( 'Please connect to your network admin to manage plugins automatic updates.', 'wp-autoupdates' ) ); + } + + $plugin = ! empty( esc_html( $_GET['plugin'] ) ) ? wp_unslash( esc_html( $_GET['plugin'] ) ) : ''; + $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; + $status = isset( $_GET['plugin_status'] ) && ! empty( esc_html( $_GET['plugin_status'] ) ) ? wp_unslash( esc_html( $_GET['plugin_status'] ) ) : ''; + $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; + + if ( empty( $plugin ) ) { + wp_redirect( self_admin_url( "plugins.php?plugin_status=$status&paged=$page&s=$s" ) ); + exit; + } + + check_admin_referer( 'autoupdate-plugin_' . $plugin ); + $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); + + if ( in_array( $plugin, $wp_auto_update_plugins, true ) ) { + $wp_auto_update_plugins = array_diff( $wp_auto_update_plugins, array( $plugin ) ); + $action_type = 'disable-autoupdate=true'; + } else { + array_push( $wp_auto_update_plugins, $plugin ); + $action_type = 'enable-autoupdate=true'; + } + update_site_option( 'wp_auto_update_plugins', $wp_auto_update_plugins ); + wp_redirect( self_admin_url( "plugins.php?$action_type&plugin_status=$status&paged=$page&s=$s" ) ); + exit; + } +} + + +/** + * Handles auto-updates enabling for themes + */ +function wp_autoupdates_themes_enabler() { + $pagenow = $GLOBALS['pagenow']; + + $action = isset( $_GET['action'] ) && ! empty( esc_html( $_GET['action'] ) ) ? wp_unslash( esc_html( $_GET['action'] ) ) : ''; + if ( 'autoupdate' === $action ) { + if ( ! current_user_can( 'update_themes' ) || ! wp_autoupdates_is_themes_auto_update_enabled() ) { + wp_die( __( 'Sorry, you are not allowed to enable themes automatic updates.', 'wp-autoupdates' ) ); + } + + if ( is_multisite() && ! is_network_admin() ) { + wp_die( __( 'Please connect to your network admin to manage themes automatic updates.', 'wp-autoupdates' ) ); + } + + $theme = ! empty( esc_html( $_GET['theme'] ) ) ? wp_unslash( esc_html( $_GET['theme'] ) ) : ''; + if ( empty( $theme ) ) { + if ( 'themes.php' === $pagenow ) { + wp_redirect( self_admin_url( 'themes.php' ) ); + } + else { + $id = ! empty( esc_html( $_GET['id'] ) ) ? wp_unslash( esc_html( $_GET['id'] ) ) : ''; + wp_redirect( self_admin_url( "site-themes.php?id=$id" ) ); + } + exit; + } + + check_admin_referer( 'autoupdate-theme_' . $theme ); + $wp_auto_update_themes = get_site_option( 'wp_auto_update_themes', array() ); + + if ( in_array( $theme, $wp_auto_update_themes, true ) ) { + $wp_auto_update_themes = array_diff( $wp_auto_update_themes, array( $theme ) ); + $action_type = 'disable-autoupdate=true'; + } else { + array_push( $wp_auto_update_themes, $theme ); + $action_type = 'enable-autoupdate=true'; + } + + update_site_option( 'wp_auto_update_themes', $wp_auto_update_themes ); + $theme_status = ''; + if ( is_multisite() && is_network_admin() ) { + $theme_status = ! empty( $_GET['theme_status'] ) ? "theme_status=" . $_GET['theme_status'] : ''; + } + if ( 'themes.php' === $pagenow ) { + wp_redirect( self_admin_url( "themes.php?$action_type&$theme_status" ) ); + } + else { + $id = ! empty( esc_html( $_GET['id'] ) ) ? wp_unslash( esc_html( $_GET['id'] ) ) : ''; + wp_redirect( self_admin_url( "site-themes.php?id=$id&$action_type&$theme_status" ) ); + } + exit; + } +} + + +/** + * Handle autoupdates enabling + */ +function wp_autoupdates_enabler() { + $pagenow = $GLOBALS['pagenow']; + if ( 'plugins.php' === $pagenow ) { + wp_autoupdates_plugins_enabler(); + } + else if ( 'themes.php' === $pagenow || 'site-themes.php' === $pagenow ) { + wp_autoupdates_themes_enabler(); + } +} +add_action( 'admin_init', 'wp_autoupdates_enabler' ); + + +/** + * Handle plugins autoupdates bulk actions + */ +function wp_autoupdates_plugins_bulk_actions_handle( $redirect_to, $doaction, $items ) { + if ( 'enable-autoupdate-selected' === $doaction ) { + if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { + wp_die( __( 'Sorry, you are not allowed to enable plugins automatic updates.', 'wp-autoupdates' ) ); + } + + if ( is_multisite() && ! is_network_admin() ) { + wp_die( __( 'Please connect to your network admin to manage plugins automatic updates.', 'wp-autoupdates' ) ); + } + + check_admin_referer( 'bulk-plugins' ); + + $plugins = ! empty( $items ) ? (array) wp_unslash( $items ) : array(); + $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; + $status = isset( $_GET['plugin_status'] ) && ! empty( esc_html( $_GET['plugin_status'] ) ) ? wp_unslash( esc_html( $_GET['plugin_status'] ) ) : ''; + $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; + + if ( empty( $plugins ) ) { + $redirect_to = self_admin_url( "plugins.php?plugin_status=$status&paged=$page&s=$s" ); + return $redirect_to; + } + + $previous_autoupdated_plugins = get_site_option( 'wp_auto_update_plugins', array() ); + + $new_autoupdated_plugins = array_merge( $previous_autoupdated_plugins, $plugins ); + $new_autoupdated_plugins = array_unique( $new_autoupdated_plugins ); + + update_site_option( 'wp_auto_update_plugins', $new_autoupdated_plugins ); + + $redirect_to = self_admin_url( "plugins.php?enable-autoupdate=true&plugin_status=$status&paged=$page&s=$s" ); + return $redirect_to; + } + + if ( 'disable-autoupdate-selected' === $doaction ) { + if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { + wp_die( __( 'Sorry, you are not allowed to enable plugins automatic updates.', 'wp-autoupdates' ) ); + } + + if ( is_multisite() && ! is_network_admin() ) { + wp_die( __( 'Please connect to your network admin to manage plugins automatic updates.', 'wp-autoupdates' ) ); + } + + check_admin_referer( 'bulk-plugins' ); + + $plugins = ! empty( $items ) ? (array) wp_unslash( $items ) : array(); + $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; + $status = isset( $_GET['plugin_status'] ) && ! empty( esc_html( $_GET['plugin_status'] ) ) ? wp_unslash( esc_html( $_GET['plugin_status'] ) ) : ''; + $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; + + if ( empty( $plugins ) ) { + $redirect_to = self_admin_url( "plugins.php?plugin_status=$status&paged=$page&s=$s" ); + return $redirect_to; + } + + $previous_autoupdated_plugins = get_site_option( 'wp_auto_update_plugins', array() ); + + $new_autoupdated_plugins = array_diff( $previous_autoupdated_plugins, $plugins ); + $new_autoupdated_plugins = array_unique( $new_autoupdated_plugins ); + + update_site_option( 'wp_auto_update_plugins', $new_autoupdated_plugins ); + + $redirect_to = self_admin_url( "plugins.php?disable-autoupdate=true&plugin_status=$status&paged=$page&s=$s" ); + return $redirect_to; + } + +} +add_action( 'handle_bulk_actions-plugins', 'wp_autoupdates_plugins_bulk_actions_handle', 10, 3 ); +add_action( 'handle_bulk_actions-plugins-network', 'wp_autoupdates_plugins_bulk_actions_handle', 10, 3 ); + + +/** + * Handle cleanup when plugin deleted + */ +function wp_autoupdates_plugin_deleted( $plugin_file, $deleted ) { + // Do nothing if the plugin wasn't deleted + if ( ! $deleted ) { + return; + } + + // Remove settings + $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); + if ( in_array( $plugin_file, $wp_auto_update_plugins, true ) ) { + $wp_auto_update_plugins = array_diff( $wp_auto_update_plugins, array( $plugin_file ) ); + update_site_option( 'wp_auto_update_plugins', $wp_auto_update_plugins ); + } +} +add_action( 'deleted_plugin', 'wp_autoupdates_plugin_deleted', 10, 2 ); + + +/** + * Auto-update notices for plugins + */ +function wp_autoupdates_plugins_notices() { + if ( isset( $_GET['enable-autoupdate'] ) ) { + echo ''; + _e( 'The selected plugins will now update automatically.', 'wp-autoupdates' ); + echo '
'; + _e( 'The selected plugins won’t automatically update anymore.', 'wp-autoupdates' ); + echo '
'; + _e( 'The selected themes will now update automatically.', 'wp-autoupdates' ); + echo '
'; + _e( 'The selected themes won’t automatically update anymore.', 'wp-autoupdates' ); + echo '
';
+ echo '' . __( 'Auto-updates enabled', 'wp-autoupdates' ) . '';
+ echo '
';
+
+ $update_message = wp_autoupdates_get_update_message();
+ if ( isset( $themes_updates->response[ $stylesheet ] ) ) {
+ echo '';
+ echo $update_message;
+ echo '
';
+ echo '';
+ }
+ if ( current_user_can( 'update_themes', $stylesheet ) ) {
+ echo sprintf(
+ '%s',
+ wp_nonce_url( $base_url, 'autoupdate-theme_' . $stylesheet ),
+ $aria_label,
+ __( 'Disable', 'wp-autoupdates' )
+ );
+ }
+ echo '
'; + echo sprintf( + ' %s', + wp_nonce_url( $base_url, 'autoupdate-theme_' . $stylesheet ), + $aria_label, + __( 'Enable', 'wp-autoupdates' ) + ); + echo '
'; + } + } + } +} +add_action( 'manage_themes_custom_column' , 'wp_autoupdates_add_themes_autoupdates_column_content', 10, 3 ); + + +/** + * Add themes autoupdates bulk actions + */ +function wp_autoupdates_themes_bulk_actions( $actions ) { + $actions['enable-autoupdate-selected'] = __( 'Enable auto-updates', 'wp-autoupdates' ); + $actions['disable-autoupdate-selected'] = __( 'Disable auto-updates', 'wp-autoupdates' ); + return $actions; +} +add_action( 'bulk_actions-themes-network', 'wp_autoupdates_themes_bulk_actions' ); +add_action( 'bulk_actions-site-themes-network', 'wp_autoupdates_themes_bulk_actions' ); + + +/** + * Handle themes autoupdates bulk actions + */ +function wp_autoupdates_themes_bulk_actions_handle( $redirect_to, $doaction, $items ) { + $pagenow = $GLOBALS['pagenow']; + + if ( 'enable-autoupdate-selected' === $doaction ) { + if ( ! current_user_can( 'update_themes' ) || ! wp_autoupdates_is_themes_auto_update_enabled() ) { + wp_die( __( 'Sorry, you are not allowed to enable themes automatic updates.', 'wp-autoupdates' ) ); + } + + if ( is_multisite() && ! is_network_admin() ) { + wp_die( __( 'Please connect to your network admin to manage themes automatic updates.', 'wp-autoupdates' ) ); + } + + check_admin_referer( 'bulk-themes' ); + + $themes = ! empty( $items ) ? (array) wp_unslash( $items ) : array(); + $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; + $status = isset( $_GET['theme_status'] ) && ! empty( esc_html( $_GET['theme_status'] ) ) ? wp_unslash( esc_html( $_GET['theme_status'] ) ) : ''; + $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; + $id = isset( $_GET['id'] ) && ! empty( esc_html( $_GET['id'] ) ) ? wp_unslash( esc_html( $_GET['id'] ) ) : ''; + + if ( empty( $themes ) ) { + if ( 'themes.php' === $pagenow ) { + $redirect_to = self_admin_url( "themes.php?theme_status=$status&paged=$page&s=$s" ); + } + else { + $redirect_to = self_admin_url( "site-themes.php?id=$id&theme_status=$status&paged=$page&s=$s" ); + } + return $redirect_to; + } + + $previous_autoupdated_themes = get_site_option( 'wp_auto_update_themes', array() ); + + $new_autoupdated_themes = array_merge( $previous_autoupdated_themes, $themes ); + $new_autoupdated_themes = array_unique( $new_autoupdated_themes ); + + update_site_option( 'wp_auto_update_themes', $new_autoupdated_themes ); + + if ( 'themes.php' === $pagenow ) { + $redirect_to = self_admin_url( "themes.php?enable-autoupdate=true&theme_status=$status&paged=$page&s=$s" ); + } + else { + $redirect_to = self_admin_url( "site-themes.php?id=$id&enable-autoupdate=true&theme_status=$status&paged=$page&s=$s" ); + } + return $redirect_to; + } + + if ( 'disable-autoupdate-selected' === $doaction ) { + if ( ! current_user_can( 'update_themes' ) || ! wp_autoupdates_is_themes_auto_update_enabled() ) { + wp_die( __( 'Sorry, you are not allowed to enable themes automatic updates.', 'wp-autoupdates' ) ); + } + + if ( is_multisite() && ! is_network_admin() ) { + wp_die( __( 'Please connect to your network admin to manage themes automatic updates.', 'wp-autoupdates' ) ); + } + + check_admin_referer( 'bulk-themes' ); + + $themes = ! empty( $items ) ? (array) wp_unslash( $items ) : array(); + $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; + $status = isset( $_GET['theme_status'] ) && ! empty( esc_html( $_GET['theme_status'] ) ) ? wp_unslash( esc_html( $_GET['theme_status'] ) ) : ''; + $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; + $id = isset( $_GET['id'] ) && ! empty( esc_html( $_GET['id'] ) ) ? wp_unslash( esc_html( $_GET['id'] ) ) : ''; + + if ( empty( $themes ) ) { + if ( 'themes.php' === $pagenow ) { + $redirect_to = self_admin_url( "themes.php?theme_status=$status&paged=$page&s=$s" ); + } + else { + $redirect_to = self_admin_url( "site-themes.php?id=$id&theme_status=$status&paged=$page&s=$s" ); + } + return $redirect_to; + } + + $previous_autoupdated_themes = get_site_option( 'wp_auto_update_themes', array() ); + + $new_autoupdated_themes = array_diff( $previous_autoupdated_themes, $themes ); + $new_autoupdated_themes = array_unique( $new_autoupdated_themes ); + + update_site_option( 'wp_auto_update_themes', $new_autoupdated_themes ); + + if ( 'themes.php' === $pagenow ) { + $redirect_to = self_admin_url( "themes.php?disable-autoupdate=true&theme_status=$status&paged=$page&s=$s" ); + } + else { + $redirect_to = self_admin_url( "site-themes.php?id=$id&disable-autoupdate=true&theme_status=$status&paged=$page&s=$s" ); + } + return $redirect_to; + } +} +add_action( 'handle_network_bulk_actions-themes-network', 'wp_autoupdates_themes_bulk_actions_handle', 10, 3 ); +add_action( 'handle_network_bulk_actions-site-themes-network', 'wp_autoupdates_themes_bulk_actions_handle', 10, 3 ); diff --git a/wp-autoupdates.php b/wp-autoupdates.php index 11ef4a0..4e41e64 100755 --- a/wp-autoupdates.php +++ b/wp-autoupdates.php @@ -1,19 +1,21 @@ '; - $autoupdate_text .= $update_message; - $autoupdate_text .= ' '; - $script .= 'jQuery(".check-column input[value=\'' . $theme . '\']").closest("tr").find(".plugin-title > p").append(\'' . $autoupdate_text . '\');'; - } - } - - if ( wp_autoupdates_is_plugins_auto_update_enabled() ) { - $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); - - $update_message = wp_autoupdates_get_update_message(); - foreach ( $wp_auto_update_plugins as $plugin ) { - $autoupdate_text = ' '; - $autoupdate_text .= $update_message; - $autoupdate_text .= ' '; - $script .= 'jQuery(".check-column input[value=\'' . $plugin . '\']").closest("tr").find(".plugin-title > p").append(\'' . $autoupdate_text . '\');'; - } - } - $script .= '});'; - wp_add_inline_script( 'jquery', $script ); - } - - // When manually updating a plugin the 'time until next update' text needs to be hidden. - // Doesn't need to be done on the update-core.php page since that page refreshes after an update. - if ( 'plugins.php' === $hook ) { - $script = 'jQuery( document ).ready(function() { - jQuery( ".update-link" ).click( function() { - var plugin = jQuery( this ).closest("tr").data("plugin"); - var plugin_row = jQuery( "tr.update[data-plugin=\'" + plugin + "\']" ); - var plugin_auto_update_time_text = plugin_row.find("span.plugin-autoupdate-time"); - plugin_auto_update_time_text.remove(); - }); - });'; - wp_add_inline_script( 'jquery', $script ); - } - - if ( 'themes.php' === $hook ) { - if ( wp_autoupdates_is_themes_auto_update_enabled() ) { - $script = 'jQuery( document ).ready( function() {'; - - /* translators: %s: Theme name. */ - $aria_label_enable = sprintf( _x( 'Enable automatic update for %s', 'theme' ), '{{ data.name }}' ); - $aria_label_disable = sprintf( _x( 'Disable automatic update for %s', 'theme' ), '{{ data.name }}' ); - - // Put the enable/disable link below the author and before the update box. - $autoupdate_text = '';
- $autoupdate_text .= '<# if ( data.autoupdate ) { #>';
- $autoupdate_text .= '';
- $autoupdate_text .= ' ' . __( 'Disable automatic updates' ) . '';
- $autoupdate_text .= '';
- $autoupdate_text .= '<# } else { #>';
- $autoupdate_text .= '';
- $autoupdate_text .= ' ' . __( 'Enable automatic updates' ) . '';
- $autoupdate_text .= '';
- $autoupdate_text .= '<# } #>';
-
- $update_message = wp_autoupdates_get_update_message();
- $autoupdate_text .= '<# if ( data.hasUpdate && data.autoupdate ) { #>';
- $autoupdate_text .= '
' . $update_message . '';
- $autoupdate_text .= '<# } #>';
- $autoupdate_text .= '
\'; - insert_into_template(position_beginning_of_update_box, "' . str_replace('"', '\"', $autoupdate_text) . '", true); - '; - - $script .= '});'; - wp_add_inline_script( 'jquery', $script ); - } - } -} -add_action( 'admin_enqueue_scripts', 'wp_autoupdates_enqueues' ); - - -/** - * Filter the themes prepared for JavaScript, for themes.php. - */ -function wp_autoupdates_prepare_themes_for_js( $prepared_themes ) { - $wp_auto_update_themes = get_option( 'wp_auto_update_themes', array() ); - foreach( $prepared_themes as $theme ) { - // Set extra data for use in the template. - $slug = $theme['id']; - $encoded_slug = urlencode( $slug ); - - $theme['autoupdate'] = in_array( $slug, $wp_auto_update_themes, true ); - $theme['actions']['autoupdate'] = current_user_can( 'update_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=autoupdate&theme=' . $encoded_slug ), 'autoupdate-theme_' . $slug ) : null; - - $prepared_themes[ $slug ] = $theme; - } - - return $prepared_themes; -} -add_action( 'wp_prepare_themes_for_js', 'wp_autoupdates_prepare_themes_for_js' ); - - -/** - * Checks whether plugins manual auto-update is enabled. - */ -function wp_autoupdates_is_plugins_auto_update_enabled() { - $enabled = ! defined( 'WP_DISABLE_PLUGINS_AUTO_UPDATE' ) || ! WP_DISABLE_PLUGINS_AUTO_UPDATE; - - /** - * Filters whether plugins manual auto-update is enabled. - * - * @param bool $enabled True if plugins auto-update is enabled, false otherwise. - */ - return apply_filters( 'wp_plugins_auto_update_enabled', $enabled ); -} - - -/** - * Checks whether themes manual auto-update is enabled. - */ -function wp_autoupdates_is_themes_auto_update_enabled() { - $enabled = ! defined( 'WP_DISABLE_THEMES_AUTO_UPDATE' ) || ! WP_DISABLE_THEMES_AUTO_UPDATE; - - /** - * Filters whether themes manual auto-update is enabled. - * - * @param bool $enabled True if themes auto-update is enabled, false otherwise. - */ - return apply_filters( 'wp_themes_auto_update_enabled', $enabled ); -} - - -/** - * Autoupdate selected plugins. - */ -function wp_autoupdates_selected_plugins( $update, $item ) { - $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); - if ( in_array( $item->plugin, $wp_auto_update_plugins, true ) && wp_autoupdates_is_plugins_auto_update_enabled() ) { - return true; - } else { - return $update; - } -} -add_filter( 'auto_update_plugin', 'wp_autoupdates_selected_plugins', 10, 2 ); - - -/** - * Autoupdate selected themes. - */ -function wp_autoupdates_selected_themes( $update, $item ) { - $wp_auto_update_themes = get_site_option( 'wp_auto_update_themes', array() ); - if ( in_array( $item->theme, $wp_auto_update_themes, true ) && wp_autoupdates_is_themes_auto_update_enabled() ) { - return true; - } else { - return $update; - } -} -add_filter( 'auto_update_theme', 'wp_autoupdates_selected_themes', 10, 2 ); - - -/** - * Add autoupdate column to plugins screen. - */ -function wp_autoupdates_add_plugins_autoupdates_column( $columns ) { - if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { - return $columns; - } - if ( ! isset( $_GET['plugin_status'] ) || ( 'mustuse' !== $_GET['plugin_status'] && 'dropins' !== $_GET['plugin_status'] ) ) { - $columns['autoupdates_column'] = __( 'Automatic updates', 'wp-autoupdates' ); - } - return $columns; -} -add_filter( is_multisite() ? 'manage_plugins-network_columns' : 'manage_plugins_columns', 'wp_autoupdates_add_plugins_autoupdates_column' ); - -/** - * Render autoupdate column’s content. - */ -function wp_autoupdates_add_plugins_autoupdates_column_content( $column_name, $plugin_file, $plugin_data ) { - if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { - return; - } - if ( 'autoupdates_column' !== $column_name ) { - return; - } - $plugins = get_plugins(); - $plugins_updates = get_site_transient( 'update_plugins' ); - $page = isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; - $plugin_status = isset( $_GET['plugin_status'] ) && ! empty( $_GET['plugin_status'] ) ? wp_unslash( esc_html( $_GET['plugin_status'] ) ) : ''; - if ( wp_autoupdates_is_plugins_auto_update_enabled() ) { - if ( ! isset( $plugins[ $plugin_file ] ) ) { - return; - } - $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); - if ( in_array( $plugin_file, $wp_auto_update_plugins, true ) ) { - $aria_label = esc_attr( - sprintf( - /* translators: Plugin name. */ - _x( 'Disable automatic updates for %s', 'plugin', 'wp-autoupdates' ), - esc_html( $plugins[ $plugin_file ]['Name'] ) - ) - ); - echo '
';
- echo '' . __( 'Auto-updates enabled', 'wp-autoupdates' ) . '';
- echo '
';
-
- $update_message = wp_autoupdates_get_update_message();
- if ( isset( $plugins_updates->response[$plugin_file] ) ) {
- echo '';
- echo $update_message;
- echo '
';
- echo '';
- }
- if ( current_user_can( 'update_plugins', $plugin_file ) ) {
- echo sprintf(
- '%s',
- wp_nonce_url( 'plugins.php?action=autoupdate&plugin=' . urlencode( $plugin_file ) . '&paged=' . $page . '&plugin_status=' . $plugin_status, 'autoupdate-plugin_' . $plugin_file ),
- $aria_label,
- __( 'Disable', 'wp-autoupdates' )
- );
- }
- echo '
'; - echo sprintf( - ' %s', - wp_nonce_url( 'plugins.php?action=autoupdate&plugin=' . urlencode( $plugin_file ) . '&paged=' . $page . '&plugin_status=' . $plugin_status, 'autoupdate-plugin_' . $plugin_file ), - $aria_label, - __( 'Enable', 'wp-autoupdates' ) - ); - echo '
'; - } - } - } -} -add_action( 'manage_plugins_custom_column' , 'wp_autoupdates_add_plugins_autoupdates_column_content', 10, 3 ); - - -/** - * Add plugins autoupdates bulk actions - */ -function wp_autoupdates_plugins_bulk_actions( $actions ) { - $actions['enable-autoupdate-selected'] = __( 'Enable auto-updates', 'wp-autoupdates' ); - $actions['disable-autoupdate-selected'] = __( 'Disable auto-updates', 'wp-autoupdates' ); - return $actions; -} -add_action( 'bulk_actions-plugins', 'wp_autoupdates_plugins_bulk_actions' ); -add_action( 'bulk_actions-plugins-network', 'wp_autoupdates_plugins_bulk_actions' ); - - -/** - * Handles auto-updates enabling for plugins - */ -function wp_autoupdates_plugins_enabler() { - $action = isset( $_GET['action'] ) && ! empty( esc_html( $_GET['action'] ) ) ? wp_unslash( esc_html( $_GET['action'] ) ) : ''; - if ( 'autoupdate' === $action ) { - if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { - wp_die( __( 'Sorry, you are not allowed to enable plugins automatic updates.', 'wp-autoupdates' ) ); - } - - if ( is_multisite() && ! is_network_admin() ) { - wp_die( __( 'Please connect to your network admin to manage plugins automatic updates.', 'wp-autoupdates' ) ); - } - - $plugin = ! empty( esc_html( $_GET['plugin'] ) ) ? wp_unslash( esc_html( $_GET['plugin'] ) ) : ''; - $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; - $status = isset( $_GET['plugin_status'] ) && ! empty( esc_html( $_GET['plugin_status'] ) ) ? wp_unslash( esc_html( $_GET['plugin_status'] ) ) : ''; - $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; - - if ( empty( $plugin ) ) { - wp_redirect( self_admin_url( "plugins.php?plugin_status=$status&paged=$page&s=$s" ) ); - exit; - } - - check_admin_referer( 'autoupdate-plugin_' . $plugin ); - $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); - - if ( in_array( $plugin, $wp_auto_update_plugins, true ) ) { - $wp_auto_update_plugins = array_diff( $wp_auto_update_plugins, array( $plugin ) ); - $action_type = 'disable-autoupdate=true'; - } else { - array_push( $wp_auto_update_plugins, $plugin ); - $action_type = 'enable-autoupdate=true'; - } - update_site_option( 'wp_auto_update_plugins', $wp_auto_update_plugins ); - wp_redirect( self_admin_url( "plugins.php?$action_type&plugin_status=$status&paged=$page&s=$s" ) ); - exit; - } -} - - -/** - * Handles auto-updates enabling for themes - */ -function wp_autoupdates_themes_enabler() { - $pagenow = $GLOBALS['pagenow']; - - $action = isset( $_GET['action'] ) && ! empty( esc_html( $_GET['action'] ) ) ? wp_unslash( esc_html( $_GET['action'] ) ) : ''; - if ( 'autoupdate' === $action ) { - if ( ! current_user_can( 'update_themes' ) || ! wp_autoupdates_is_themes_auto_update_enabled() ) { - wp_die( __( 'Sorry, you are not allowed to enable themes automatic updates.', 'wp-autoupdates' ) ); - } - - if ( is_multisite() && ! is_network_admin() ) { - wp_die( __( 'Please connect to your network admin to manage themes automatic updates.', 'wp-autoupdates' ) ); - } - - $theme = ! empty( esc_html( $_GET['theme'] ) ) ? wp_unslash( esc_html( $_GET['theme'] ) ) : ''; - if ( empty( $theme ) ) { - if ( 'themes.php' === $pagenow ) { - wp_redirect( self_admin_url( 'themes.php' ) ); - } - else { - $id = ! empty( esc_html( $_GET['id'] ) ) ? wp_unslash( esc_html( $_GET['id'] ) ) : ''; - wp_redirect( self_admin_url( "site-themes.php?id=$id" ) ); - } - exit; - } - - check_admin_referer( 'autoupdate-theme_' . $theme ); - $wp_auto_update_themes = get_site_option( 'wp_auto_update_themes', array() ); - - if ( in_array( $theme, $wp_auto_update_themes, true ) ) { - $wp_auto_update_themes = array_diff( $wp_auto_update_themes, array( $theme ) ); - $action_type = 'disable-autoupdate=true'; - } else { - array_push( $wp_auto_update_themes, $theme ); - $action_type = 'enable-autoupdate=true'; - } - - update_site_option( 'wp_auto_update_themes', $wp_auto_update_themes ); - $theme_status = ''; - if ( is_multisite() && is_network_admin() ) { - $theme_status = ! empty( $_GET['theme_status'] ) ? "theme_status=" . $_GET['theme_status'] : ''; - } - if ( 'themes.php' === $pagenow ) { - wp_redirect( self_admin_url( "themes.php?$action_type&$theme_status" ) ); - } - else { - $id = ! empty( esc_html( $_GET['id'] ) ) ? wp_unslash( esc_html( $_GET['id'] ) ) : ''; - wp_redirect( self_admin_url( "site-themes.php?id=$id&$action_type&$theme_status" ) ); - } - exit; - } -} - - -/** - * Handle autoupdates enabling - */ -function wp_autoupdates_enabler() { - $pagenow = $GLOBALS['pagenow']; - if ( 'plugins.php' === $pagenow ) { - wp_autoupdates_plugins_enabler(); - } - else if ( 'themes.php' === $pagenow || 'site-themes.php' === $pagenow ) { - wp_autoupdates_themes_enabler(); - } -} -add_action( 'admin_init', 'wp_autoupdates_enabler' ); - - -/** - * Handle plugins autoupdates bulk actions - */ -function wp_autoupdates_plugins_bulk_actions_handle( $redirect_to, $doaction, $items ) { - if ( 'enable-autoupdate-selected' === $doaction ) { - if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { - wp_die( __( 'Sorry, you are not allowed to enable plugins automatic updates.', 'wp-autoupdates' ) ); - } - - if ( is_multisite() && ! is_network_admin() ) { - wp_die( __( 'Please connect to your network admin to manage plugins automatic updates.', 'wp-autoupdates' ) ); - } - - check_admin_referer( 'bulk-plugins' ); - - $plugins = ! empty( $items ) ? (array) wp_unslash( $items ) : array(); - $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; - $status = isset( $_GET['plugin_status'] ) && ! empty( esc_html( $_GET['plugin_status'] ) ) ? wp_unslash( esc_html( $_GET['plugin_status'] ) ) : ''; - $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; - - if ( empty( $plugins ) ) { - $redirect_to = self_admin_url( "plugins.php?plugin_status=$status&paged=$page&s=$s" ); - return $redirect_to; - } - - $previous_autoupdated_plugins = get_site_option( 'wp_auto_update_plugins', array() ); - - $new_autoupdated_plugins = array_merge( $previous_autoupdated_plugins, $plugins ); - $new_autoupdated_plugins = array_unique( $new_autoupdated_plugins ); - - update_site_option( 'wp_auto_update_plugins', $new_autoupdated_plugins ); - - $redirect_to = self_admin_url( "plugins.php?enable-autoupdate=true&plugin_status=$status&paged=$page&s=$s" ); - return $redirect_to; - } - - if ( 'disable-autoupdate-selected' === $doaction ) { - if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { - wp_die( __( 'Sorry, you are not allowed to enable plugins automatic updates.', 'wp-autoupdates' ) ); - } - - if ( is_multisite() && ! is_network_admin() ) { - wp_die( __( 'Please connect to your network admin to manage plugins automatic updates.', 'wp-autoupdates' ) ); - } - - check_admin_referer( 'bulk-plugins' ); - - $plugins = ! empty( $items ) ? (array) wp_unslash( $items ) : array(); - $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; - $status = isset( $_GET['plugin_status'] ) && ! empty( esc_html( $_GET['plugin_status'] ) ) ? wp_unslash( esc_html( $_GET['plugin_status'] ) ) : ''; - $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; - - if ( empty( $plugins ) ) { - $redirect_to = self_admin_url( "plugins.php?plugin_status=$status&paged=$page&s=$s" ); - return $redirect_to; - } - - $previous_autoupdated_plugins = get_site_option( 'wp_auto_update_plugins', array() ); - - $new_autoupdated_plugins = array_diff( $previous_autoupdated_plugins, $plugins ); - $new_autoupdated_plugins = array_unique( $new_autoupdated_plugins ); - - update_site_option( 'wp_auto_update_plugins', $new_autoupdated_plugins ); - - $redirect_to = self_admin_url( "plugins.php?disable-autoupdate=true&plugin_status=$status&paged=$page&s=$s" ); - return $redirect_to; - } - -} -add_action( 'handle_bulk_actions-plugins', 'wp_autoupdates_plugins_bulk_actions_handle', 10, 3 ); -add_action( 'handle_bulk_actions-plugins-network', 'wp_autoupdates_plugins_bulk_actions_handle', 10, 3 ); - - -/** - * Handle cleanup when plugin deleted - */ -function wp_autoupdates_plugin_deleted( $plugin_file, $deleted ) { - // Do nothing if the plugin wasn't deleted - if ( ! $deleted ) { - return; - } - - // Remove settings - $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); - if ( in_array( $plugin_file, $wp_auto_update_plugins, true ) ) { - $wp_auto_update_plugins = array_diff( $wp_auto_update_plugins, array( $plugin_file ) ); - update_site_option( 'wp_auto_update_plugins', $wp_auto_update_plugins ); - } -} -add_action( 'deleted_plugin', 'wp_autoupdates_plugin_deleted', 10, 2 ); - - -/** - * Auto-update notices for plugins - */ -function wp_autoupdates_plugins_notices() { - if ( isset( $_GET['enable-autoupdate'] ) ) { - echo ''; - _e( 'The selected plugins will now update automatically.', 'wp-autoupdates' ); - echo '
'; - _e( 'The selected plugins won’t automatically update anymore.', 'wp-autoupdates' ); - echo '
'; - _e( 'The selected themes will now update automatically.', 'wp-autoupdates' ); - echo '
'; - _e( 'The selected themes won’t automatically update anymore.', 'wp-autoupdates' ); - echo '
';
- echo '' . __( 'Auto-updates enabled', 'wp-autoupdates' ) . '';
- echo '
';
-
- $update_message = wp_autoupdates_get_update_message();
- if ( isset( $themes_updates->response[ $stylesheet ] ) ) {
- echo '';
- echo $update_message;
- echo '
';
- echo '';
- }
- if ( current_user_can( 'update_themes', $stylesheet ) ) {
- echo sprintf(
- '%s',
- wp_nonce_url( $base_url, 'autoupdate-theme_' . $stylesheet ),
- $aria_label,
- __( 'Disable', 'wp-autoupdates' )
- );
- }
- echo '
'; - echo sprintf( - ' %s', - wp_nonce_url( $base_url, 'autoupdate-theme_' . $stylesheet ), - $aria_label, - __( 'Enable', 'wp-autoupdates' ) - ); - echo '
'; - } - } - } -} -add_action( 'manage_themes_custom_column' , 'wp_autoupdates_add_themes_autoupdates_column_content', 10, 3 ); - - -/** - * Add themes autoupdates bulk actions - */ -function wp_autoupdates_themes_bulk_actions( $actions ) { - $actions['enable-autoupdate-selected'] = __( 'Enable auto-updates', 'wp-autoupdates' ); - $actions['disable-autoupdate-selected'] = __( 'Disable auto-updates', 'wp-autoupdates' ); - return $actions; -} -add_action( 'bulk_actions-themes-network', 'wp_autoupdates_themes_bulk_actions' ); -add_action( 'bulk_actions-site-themes-network', 'wp_autoupdates_themes_bulk_actions' ); - - -/** - * Handle themes autoupdates bulk actions - */ -function wp_autoupdates_themes_bulk_actions_handle( $redirect_to, $doaction, $items ) { - $pagenow = $GLOBALS['pagenow']; - - if ( 'enable-autoupdate-selected' === $doaction ) { - if ( ! current_user_can( 'update_themes' ) || ! wp_autoupdates_is_themes_auto_update_enabled() ) { - wp_die( __( 'Sorry, you are not allowed to enable themes automatic updates.', 'wp-autoupdates' ) ); - } - - if ( is_multisite() && ! is_network_admin() ) { - wp_die( __( 'Please connect to your network admin to manage themes automatic updates.', 'wp-autoupdates' ) ); - } - - check_admin_referer( 'bulk-themes' ); - - $themes = ! empty( $items ) ? (array) wp_unslash( $items ) : array(); - $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; - $status = isset( $_GET['theme_status'] ) && ! empty( esc_html( $_GET['theme_status'] ) ) ? wp_unslash( esc_html( $_GET['theme_status'] ) ) : ''; - $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; - $id = isset( $_GET['id'] ) && ! empty( esc_html( $_GET['id'] ) ) ? wp_unslash( esc_html( $_GET['id'] ) ) : ''; - - if ( empty( $themes ) ) { - if ( 'themes.php' === $pagenow ) { - $redirect_to = self_admin_url( "themes.php?theme_status=$status&paged=$page&s=$s" ); - } - else { - $redirect_to = self_admin_url( "site-themes.php?id=$id&theme_status=$status&paged=$page&s=$s" ); - } - return $redirect_to; - } - - $previous_autoupdated_themes = get_site_option( 'wp_auto_update_themes', array() ); - - $new_autoupdated_themes = array_merge( $previous_autoupdated_themes, $themes ); - $new_autoupdated_themes = array_unique( $new_autoupdated_themes ); - - update_site_option( 'wp_auto_update_themes', $new_autoupdated_themes ); - - if ( 'themes.php' === $pagenow ) { - $redirect_to = self_admin_url( "themes.php?enable-autoupdate=true&theme_status=$status&paged=$page&s=$s" ); - } - else { - $redirect_to = self_admin_url( "site-themes.php?id=$id&enable-autoupdate=true&theme_status=$status&paged=$page&s=$s" ); - } - return $redirect_to; - } - - if ( 'disable-autoupdate-selected' === $doaction ) { - if ( ! current_user_can( 'update_themes' ) || ! wp_autoupdates_is_themes_auto_update_enabled() ) { - wp_die( __( 'Sorry, you are not allowed to enable themes automatic updates.', 'wp-autoupdates' ) ); - } - - if ( is_multisite() && ! is_network_admin() ) { - wp_die( __( 'Please connect to your network admin to manage themes automatic updates.', 'wp-autoupdates' ) ); - } - - check_admin_referer( 'bulk-themes' ); - - $themes = ! empty( $items ) ? (array) wp_unslash( $items ) : array(); - $page = isset( $_GET['paged'] ) && ! empty( esc_html( $_GET['paged'] ) ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; - $status = isset( $_GET['theme_status'] ) && ! empty( esc_html( $_GET['theme_status'] ) ) ? wp_unslash( esc_html( $_GET['theme_status'] ) ) : ''; - $s = isset( $_GET['s'] ) && ! empty( esc_html( $_GET['s'] ) ) ? wp_unslash( esc_html( $_GET['s'] ) ) : ''; - $id = isset( $_GET['id'] ) && ! empty( esc_html( $_GET['id'] ) ) ? wp_unslash( esc_html( $_GET['id'] ) ) : ''; - - if ( empty( $themes ) ) { - if ( 'themes.php' === $pagenow ) { - $redirect_to = self_admin_url( "themes.php?theme_status=$status&paged=$page&s=$s" ); - } - else { - $redirect_to = self_admin_url( "site-themes.php?id=$id&theme_status=$status&paged=$page&s=$s" ); - } - return $redirect_to; - } - - $previous_autoupdated_themes = get_site_option( 'wp_auto_update_themes', array() ); - - $new_autoupdated_themes = array_diff( $previous_autoupdated_themes, $themes ); - $new_autoupdated_themes = array_unique( $new_autoupdated_themes ); - - update_site_option( 'wp_auto_update_themes', $new_autoupdated_themes ); - - if ( 'themes.php' === $pagenow ) { - $redirect_to = self_admin_url( "themes.php?disable-autoupdate=true&theme_status=$status&paged=$page&s=$s" ); - } - else { - $redirect_to = self_admin_url( "site-themes.php?id=$id&disable-autoupdate=true&theme_status=$status&paged=$page&s=$s" ); - } - return $redirect_to; - } +if ( ! function_exists( 'wp_is_plugins_auto_update_enabled' ) ) { + include_once plugin_dir_path( __FILE__ ) . 'functions.php'; } -add_action( 'handle_network_bulk_actions-themes-network', 'wp_autoupdates_themes_bulk_actions_handle', 10, 3 ); -add_action( 'handle_network_bulk_actions-site-themes-network', 'wp_autoupdates_themes_bulk_actions_handle', 10, 3 );