diff --git a/functions.php b/functions.php index 80b9f8d..fad58ff 100755 --- a/functions.php +++ b/functions.php @@ -421,10 +421,13 @@ function wp_autoupdates_themes_enabler() { } 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'] : ''; } + wp_redirect( self_admin_url( "themes.php?$action_type&$theme_status" ) ); exit; } @@ -436,10 +439,10 @@ function wp_autoupdates_themes_enabler() { */ function wp_autoupdates_enabler() { $pagenow = $GLOBALS['pagenow']; + if ( 'plugins.php' === $pagenow ) { wp_autoupdates_plugins_enabler(); - } - else if ( 'themes.php' === $pagenow ) { + } elseif ( 'themes.php' === $pagenow ) { wp_autoupdates_themes_enabler(); } } @@ -485,9 +488,7 @@ function wp_autoupdates_plugins_bulk_actions_handle( $redirect_to, $doaction, $i $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 ) { + } elseif ( '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' ) ); } @@ -555,6 +556,7 @@ function wp_autoupdates_plugins_notices() { _e( 'Selected plugins will be auto-updated.', 'wp-autoupdates' ); echo '

'; } + if ( isset( $_GET['disable-autoupdate'] ) ) { echo '

'; _e( 'Selected plugins will no longer be auto-updated.', 'wp-autoupdates' ); @@ -572,6 +574,7 @@ function wp_autoupdates_themes_notices() { _e( 'Selected themes will be auto-updated.', 'wp-autoupdates' ); echo '

'; } + if ( isset( $_GET['disable-autoupdate'] ) ) { echo '

'; _e( 'Selected themes will no longer be auto-updated.', 'wp-autoupdates' ); @@ -586,10 +589,10 @@ function wp_autoupdates_themes_notices() { function wp_autoupdates_notices() { // Plugins screen. $pagenow = $GLOBALS['pagenow']; + if ( 'plugins.php' === $pagenow ) { wp_autoupdates_plugins_notices(); - } - else if ( 'themes.php' === $pagenow ) { + } elseif ( 'themes.php' === $pagenow ) { wp_autoupdates_themes_notices(); } } @@ -635,6 +638,7 @@ function wp_autoupdates_plugins_status_links( $status_links ) { if ( 0 === $count ) { continue; } + switch( $type ) { case 'autoupdate_enabled': /* translators: %s: Number of plugins. */ @@ -703,7 +707,8 @@ function wp_autoupdates_plugins_filter_plugins_by_status( $plugins ) { } $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); - $_plugins = array(); + $_plugins = array(); + foreach ( $plugins as $plugin_file => $plugin_data ) { switch ( $_REQUEST['plugin_status'] ) { case 'autoupdate_enabled': @@ -807,6 +812,7 @@ function wp_autoupdates_debug_information( $info ) { /* translators: %s: Plugin author name. */ $plugin_version_string_debug = sprintf( __( 'author: %s, version: (undefined)', 'wp-autoupdates' ), $plugin_author ); } + if ( ! empty( $plugin_version ) ) { /* translators: %s: Plugin version number. */ $plugin_version_string = sprintf( __( 'Version %s', 'wp-autoupdates' ), $plugin_version ); @@ -844,11 +850,13 @@ function wp_autoupdates_debug_information( $info ) { $themes = wp_get_themes(); $active_theme = wp_get_theme(); + foreach ( $themes as $theme_path => $theme ) { $theme_version = sanitize_text_field( $theme['Version'] ); $theme_author = sanitize_text_field( $theme['Author'] ); $is_active_theme = $theme->name === $active_theme->name; + if ($is_active_theme) { $theme_part = 'wp-active-theme'; @@ -881,6 +889,7 @@ function wp_autoupdates_debug_information( $info ) { /* translators: %s: Theme author name. */ $theme_version_string_debug = sprintf( __( 'author: %s, version: (undefined)', 'wp-autoupdates' ), $theme_author ); } + if ( ! empty( $theme_version ) ) { /* translators: %s: Theme version number. */ $theme_version_string = sprintf( __( 'Version %s', 'wp-autoupdates' ), $theme_version ); @@ -899,6 +908,7 @@ function wp_autoupdates_debug_information( $info ) { $theme_name = sanitize_text_field( $theme['Name'] ); $label_name = sprintf( __( '%1$s (%2$s)', 'wp-autoupdates' ), $theme_name, $theme_path); + $info[ $theme_part ]['fields'][ $theme_name ] = array( 'label' => $label_name, 'value' => $theme_version_string, @@ -969,7 +979,8 @@ function wp_autoupdates_is_themes_auto_update_email_enabled() { */ function wp_autoupdates_automatic_updates_complete_notification( $results ) { $successful_updates = array(); - $failed_updates = array(); + $failed_updates = array(); + if ( isset( $results['plugin'] ) && wp_autoupdates_is_plugins_auto_update_email_enabled() ) { foreach ( $results['plugin'] as $update_result ) { if ( true === $update_result->result ) { @@ -979,6 +990,7 @@ function wp_autoupdates_automatic_updates_complete_notification( $results ) { } } } + if ( isset( $results['theme'] ) && wp_autoupdates_is_themes_auto_update_enabled() ) { foreach ( $results['theme'] as $update_result ) { if ( true === $update_result->result ) { @@ -988,9 +1000,11 @@ function wp_autoupdates_automatic_updates_complete_notification( $results ) { } } } + if ( empty( $successful_updates ) && empty( $failed_updates ) ) { return; } + if ( empty( $failed_updates ) ) { wp_autoupdates_send_email_notification( 'success', $successful_updates, $failed_updates ); } elseif ( empty( $successful_updates ) ) { @@ -1014,6 +1028,7 @@ function wp_autoupdates_send_email_notification( $type, $successful_updates, $fa if ( empty( $successful_updates ) && empty( $failed_updates ) ) { return; } + $body = array(); switch ( $type ) { @@ -1171,9 +1186,11 @@ function wp_autoupdates_add_themes_autoupdates_column( $columns ) { if ( ! current_user_can( 'update_themes' ) || ! wp_autoupdates_is_themes_auto_update_enabled() ) { return $columns; } + if ( ! isset( $_GET['theme_status'] ) || 'broken' !== $_GET['theme_status'] ) { $columns['autoupdates_column'] = __( 'Automatic updates', 'wp-autoupdates' ); } + return $columns; } add_filter( 'manage_themes-network_columns', 'wp_autoupdates_add_themes_autoupdates_column' ); @@ -1192,20 +1209,26 @@ function wp_autoupdates_add_themes_autoupdates_column_content( $column_name, $st if ( ! current_user_can( 'update_themes' ) || ! wp_autoupdates_is_themes_auto_update_enabled() ) { return; } + if ( 'autoupdates_column' !== $column_name ) { return; } + $themes = wp_get_themes(); $themes_updates = get_site_transient( 'update_themes' ); $page = isset( $_GET['paged'] ) && ! empty( $_GET['paged'] ) ? wp_unslash( esc_html( $_GET['paged'] ) ) : ''; $theme_status = isset( $_GET['theme_status'] ) && ! empty( $_GET['theme_status'] ) ? wp_unslash( esc_html( $_GET['theme_status'] ) ) : ''; + + // TODO: needs add_query_arg() $base_url = 'themes.php?action=autoupdate&theme=' . urlencode( $stylesheet ) . '&paged=' . $page . '&theme_status=' . $theme_status; if ( wp_autoupdates_is_themes_auto_update_enabled() ) { if ( ! isset( $themes[ $stylesheet ] ) ) { return; } + $wp_auto_update_themes = get_site_option( 'wp_auto_update_themes', array() ); + if ( in_array( $stylesheet, $wp_auto_update_themes, true ) ) { $aria_label = esc_attr( sprintf( @@ -1214,17 +1237,21 @@ function wp_autoupdates_add_themes_autoupdates_column_content( $column_name, $st esc_html( $themes[ $stylesheet ]->get( 'Name' ) ) ) ); + + // TODO: consider outputting HTML instead of echoing everything 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', @@ -1233,6 +1260,7 @@ function wp_autoupdates_add_themes_autoupdates_column_content( $column_name, $st __( 'Disable auto-updates', 'wp-autoupdates' ) ); } + echo '

'; } else { if ( current_user_can( 'update_themes', $stylesheet ) ) { @@ -1243,13 +1271,16 @@ function wp_autoupdates_add_themes_autoupdates_column_content( $column_name, $st esc_html( $themes[ $stylesheet ]->get( 'Name' ) ) ) ); + echo '

'; + echo sprintf( '%s', wp_nonce_url( $base_url, 'autoupdate-theme_' . $stylesheet ), $aria_label, __( 'Enable auto-updates', 'wp-autoupdates' ) ); + echo '

'; } } @@ -1353,47 +1384,30 @@ function wp_autoupdates_themes_bulk_actions_handle( $redirect_to, $doaction, $it * Disable auto updates via Ajax. */ function wp_autoupdates_disable_auto_updates() { + if ( empty( $_POST['type'] ) || empty( $_POST['asset'] ) ) { + wp_send_json_error( array( 'error' => __( 'Invalid data. No selected item.' ) ) ); + } + $type = sanitize_text_field( $_POST['type'] ); $asset = sanitize_text_field( urldecode( $_POST['asset'] ) ); - check_ajax_referer( - sprintf( - 'autoupdate-%s_%s', - $type, - $asset - ) - ); - // Capability check. + check_ajax_referer( "autoupdate-{$type}_{$asset}" ); + if ( 'plugin' === $type ) { if ( ! current_user_can( 'update_plugins' ) ) { - wp_send_json_error( - array( - 'error' => __( 'You do not have permission to modify plugins.', 'wp-autoupdate' ), - ) - ); - } - } - if ( 'theme' === $type ) { - if ( ! current_user_can( 'update_themes' ) ) { - wp_send_json_error( - array( - 'error' => __( 'You do not have permission to modify themes.', 'wp-autoupdate' ), - ) - ); + wp_send_json_error( array( 'error' => __( 'You do not have permission to modify plugins.', 'wp-autoupdate' ) ) ); } - } - // Check Asset Type. - if ( 'plugin' === $type ) { + /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ + $all_plugins = apply_filters( 'all_plugins', get_plugins() ); + + // TODO: should probably check if the plugin ($asset) is in the all_plugins array + // and return error if not + $wp_autoupdate_plugins = (array) get_site_option( 'wp_auto_update_plugins', array() ); $wp_autoupdate_plugins = array_diff( $wp_autoupdate_plugins, array( $asset ) ); update_site_option( 'wp_auto_update_plugins', $wp_autoupdate_plugins ); - /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ - $all_plugins = apply_filters( 'all_plugins', get_plugins() ); - $wp_autoupdate_plugins = array_intersect( $wp_autoupdate_plugins, array_keys( $all_plugins ) ); - $enabled_count = count( $wp_autoupdate_plugins ); - $plugin_url = add_query_arg( array( 'action' => 'autoupdate', @@ -1412,6 +1426,7 @@ function wp_autoupdates_disable_auto_updates() { ); $return_html = sprintf( + // TODO: do not inject HTML from AJAX '

%s

', esc_url_raw( $plugin_url ), $aria_label, @@ -1420,22 +1435,24 @@ function wp_autoupdates_disable_auto_updates() { wp_send_json_success( array( - 'enabled_count' => '(' . $enabled_count . ')', - 'disabled_count' => '(' . absint( count( $all_plugins ) - $enabled_count ) . ')', - 'return_html' => wp_kses_post( $return_html ), - 'type' => 'plugin', + 'return_html' => wp_kses_post( $return_html ), + 'type' => 'plugin', ) ); } elseif ( 'theme' === $type ) { + if ( ! current_user_can( 'update_themes' ) ) { + wp_send_json_error( array( 'error' => __( 'You do not have permission to modify themes.', 'wp-autoupdate' ) ) ); + } + + $all_themes = wp_get_themes(); + + // TODO: should probably check if the theme ($asset) is in the all_themes array + // and return error if not + $wp_autoupdate_themes = (array) get_site_option( 'wp_auto_update_themes', array() ); $wp_autoupdate_themes = array_diff( $wp_autoupdate_themes, array( $asset ) ); update_site_option( 'wp_auto_update_themes', $wp_autoupdate_themes ); - /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ - $all_themes = wp_get_themes(); - $wp_autoupdate_themes = array_intersect( $wp_autoupdate_themes, array_keys( $all_themes ) ); - $enabled_count = count( $wp_autoupdate_themes ); - $theme_url = add_query_arg( array( 'action' => 'autoupdate', @@ -1454,6 +1471,7 @@ function wp_autoupdates_disable_auto_updates() { ); $return_html = sprintf( + // TODO: see above '

%s

', esc_url_raw( $theme_url ), $aria_label, @@ -1462,66 +1480,47 @@ function wp_autoupdates_disable_auto_updates() { wp_send_json_success( array( - 'enabled_count' => '(' . $enabled_count . ')', - 'disabled_count' => '(' . absint( count( $all_themes ) - $enabled_count ) . ')', - 'return_html' => wp_kses_post( $return_html ), - 'type' => 'theme', + 'return_html' => wp_kses_post( $return_html ), + 'type' => 'theme', ) ); } - wp_send_json_error( - array( - 'error' => __( 'Could not disable auto-updates for the selected item.', 'wp-autoupdates' ), - ) - ); + // TODO: this is unreachable? + wp_send_json_error( array( 'error' => __( 'Could not disable auto-updates for the selected item.' ) ) ); } add_action( 'wp_ajax_disable_auto_updates', 'wp_autoupdates_disable_auto_updates' ); + +// TODO: Merge with the above function. Code is nearly identical, lots of repetition... /** * Enable auto updates via Ajax. */ function wp_autoupdates_enable_auto_updates() { + if ( empty( $_POST['type'] ) || empty( $_POST['asset'] ) ) { + wp_send_json_error( array( 'error' => __( 'Invalid data. No selected item.' ) ) ); + } + $type = sanitize_text_field( $_POST['type'] ); $asset = sanitize_text_field( urldecode( $_POST['asset'] ) ); - check_ajax_referer( - sprintf( - 'autoupdate-%s_%s', - $type, - $asset - ) - ); - // Capability check. + check_ajax_referer( "autoupdate-{$type}_{$asset}" ); + + // Check Asset Type. if ( 'plugin' === $type ) { if ( ! current_user_can( 'update_plugins' ) ) { - wp_send_json_error( - array( - 'error' => __( 'You do not have permission to modify plugins.', 'wp-autoupdate' ), - ) - ); - } - } - if ( 'theme' === $type ) { - if ( ! current_user_can( 'update_themes' ) ) { - wp_send_json_error( - array( - 'error' => __( 'You do not have permission to modify themes.', 'wp-autoupdate' ), - ) - ); + wp_send_json_error( array( 'error' => __( 'You do not have permission to modify plugins.', 'wp-autoupdate' ) ) ); } - } - // Check Asset Type. - if ( 'plugin' === $type ) { + /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ + $all_plugins = apply_filters( 'all_plugins', get_plugins() ); + + //TODO: Check if $asset is in the all_plugins array? + $wp_autoupdate_plugins = (array) get_site_option( 'wp_auto_update_plugins', array() ); $wp_autoupdate_plugins[] = $asset; - array_unique( $wp_autoupdate_plugins ); - update_site_option( 'wp_auto_update_plugins', $wp_autoupdate_plugins ); + $wp_autoupdated_plugins = array_unique( $wp_autoupdate_plugins ); - /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ - $all_plugins = apply_filters( 'all_plugins', get_plugins() ); - $wp_autoupdate_plugins = array_intersect( $wp_autoupdate_plugins, array_keys( $all_plugins ) ); - $enabled_count = count( $wp_autoupdate_plugins ); + update_site_option( 'wp_auto_update_plugins', $wp_autoupdate_plugins ); $plugin_url = add_query_arg( array( @@ -1535,12 +1534,13 @@ function wp_autoupdates_enable_auto_updates() { $aria_label = esc_attr( sprintf( /* translators: Plugin name. */ - _x( 'Disable automatic updates for %s', 'plugin', 'wp-autoupdates' ), + _x( 'Disable automatic updates for %s', 'plugin name', 'wp-autoupdates' ), esc_html( $all_plugins[ $asset ]['Name'] ) ) ); $return_html = sprintf( + // TODO: Do not inject HTML from AJAX '

%s
%s

', esc_html__( 'Auto-updates enabled', 'wp-autoupdates' ), esc_url_raw( $plugin_url ), @@ -1550,22 +1550,24 @@ function wp_autoupdates_enable_auto_updates() { wp_send_json_success( array( - 'enabled_count' => '(' . absint( $enabled_count ) . ')', - 'disabled_count' => '(' . absint( count( $all_plugins ) - $enabled_count ) . ')', - 'return_html' => wp_kses_post( $return_html ), - 'type' => 'plugin', + 'return_html' => wp_kses_post( $return_html ), + 'type' => 'plugin', ) ); } elseif ( 'theme' === $type ) { + if ( ! current_user_can( 'update_themes' ) ) { + wp_send_json_error( array( 'error' => __( 'You do not have permission to modify themes.', 'wp-autoupdate' ) ) ); + } + + $all_themes = wp_get_themes(); + + // TODO: see above + $wp_autoupdate_themes = (array) get_site_option( 'wp_auto_update_themes', array() ); $wp_autoupdate_themes[] = $asset; - array_unique( $wp_autoupdate_themes ); + $wp_autoupdate_themes = array_unique( $wp_autoupdate_themes ); update_site_option( 'wp_auto_update_themes', $wp_autoupdate_themes ); - $all_themes = wp_get_themes(); - $wp_autoupdate_themes = array_intersect( $wp_autoupdate_themes, array_keys( $all_themes ) ); - $enabled_count = count( $wp_autoupdate_themes ); - $theme_url = add_query_arg( array( 'action' => 'autoupdate', @@ -1584,6 +1586,7 @@ function wp_autoupdates_enable_auto_updates() { ); $return_html = sprintf( + // TODO: see above '

%s
%s

', esc_html__( 'Auto-updates enabled', 'wp-autoupdates' ), esc_url_raw( $theme_url ), @@ -1593,18 +1596,13 @@ function wp_autoupdates_enable_auto_updates() { wp_send_json_success( array( - 'enabled_count' => '(' . absint( $enabled_count ) . ')', - 'disabled_count' => '(' . absint( count( $all_themes ) - $enabled_count ) . ')', - 'return_html' => wp_kses_post( $return_html ), - 'type' => 'theme', + 'return_html' => wp_kses_post( $return_html ), + 'type' => 'theme', ) ); } - wp_send_json_error( - array( - 'error' => __( 'Could not enable auto-updates for the selected item.', 'wp-autoupdates' ), - ) - ); + // TODO: this is unreachable? + wp_send_json_error( array( 'error' => __( 'Could not enable auto-updates for the selected item.' ) ) ); } add_action( 'wp_ajax_enable_auto_updates', 'wp_autoupdates_enable_auto_updates' ); diff --git a/js/wp-autoupdates.js b/js/wp-autoupdates.js index 3607767..4203e58 100644 --- a/js/wp-autoupdates.js +++ b/js/wp-autoupdates.js @@ -1,181 +1,238 @@ -jQuery(function ($) { +jQuery( function( $ ) { + // TODO: needs fixing? function add_error_notice( html, error ) { html += '

' + error + '

'; return html; } + + function updateCounts( number ) { + var $enabled = $( '.autoupdate_enabled span' ); + var $disabled = $( '.autoupdate_disabled span' ); + var enabledNumber = parseInt( $enabled.text().replace( /[^\d]+/g, '' ) ) || 0; + var disabledNumber = parseInt( $disabled.text().replace( /[^\d]+/g, '' ) ) || 0; + + if ( number === 1 ) { + ++enabledNumber; + --disabledNumber; + } else if ( number === -1 ) { + --enabledNumber; + ++disabledNumber; + } else { + return; + } + + if ( enabledNumber < 0 ) { + enabledNumber = 0; + } + + if ( disabledNumber < 0 ) { + disabledNumber = 0; + } + + $enabled.text( '(' + enabledNumber + ')' ); + $disabled.text( '(' + disabledNumber + ')' ); + } + // Disable auto-updates for a plugin. - $('.autoupdates_column').on('click', 'a.plugin-autoupdate-disable', function (e) { - e.preventDefault(); + $( '.autoupdates_column' ).on( 'click', 'a.plugin-autoupdate-disable', function ( event ) { var $anchor = $( this ); - $anchor.blur(); - var href = wpAjax.unserialize($anchor.attr( 'href' ) ); + // TODO: Drop use of unserialize, perhaps switch to data-* attr + var href = wpAjax.unserialize( $anchor.attr( 'href' ) ); var $parent = $anchor.parents( '.autoupdates_column' ); + var html; + var data; + + event.preventDefault(); + $anchor.blur(); //? + // Clear errors $parent.find( '.notice' ).remove(); - var html = $parent.html(); + html = $parent.html(); // Show loading status. + // TODO: replace text not HTML to keep the focus $anchor.html( ' ' + wp_autoupdates.disabling ); - $.post( - ajaxurl, - { - action: 'disable_auto_updates', - _ajax_nonce: href._wpnonce, - type: 'plugin', - asset: href.plugin - }, - function (response) { + data = { + action: 'disable_auto_updates', + _ajax_nonce: href._wpnonce, + type: 'plugin', + asset: href.plugin, + }; + + $.post( ajaxurl, data ).done( function( response ) { + var errorHTML; - } - ) - .done(function (response) { if ( response.success ) { - $( '.autoupdate_enabled span' ).html( response.data.enabled_count ); - $( '.autoupdate_disabled span' ).html( response.data.disabled_count ); + updateCounts( -1 ); + + // TODO: avoid injecting external HTML $parent.html( response.data.return_html ); - $parent.find('.plugin-autoupdate-enable').focus(); + + // TODO: fix changing the focus. Better to update the elements text and keep the focus where it is. + $parent.find( '.plugin-autoupdate-enable' ).focus(); wp.a11y.speak( wp_autoupdates.auto_disabled, 'polite' ); } else { - var errorHTML = add_error_notice( html, response.data.error ); + // TODO: avoid injecting external HTML + errorHTML = add_error_notice( html, response.data.error ); wp.a11y.speak( response.data.error, 'polite' ); + + // TODO: avoid injecting external HTML $parent.html( errorHTML ); } - }) - .fail(function (response) { + }).fail( function( response ) { var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error ); wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' ); + + // TODO: avoid injecting external HTML. Fix loosing focus. $parent.html( errorHTML ); - }) - .always(function (response) { }); }); + // Enable auto-updates for a plugin. - $('.autoupdates_column').on('click', 'a.plugin-autoupdate-enable', function (e) { - e.preventDefault(); + // TODO: merge with the above function. No need to be separate. + $( '.autoupdates_column' ).on( 'click', 'a.plugin-autoupdate-enable', function( event ) { var $anchor = $( this ); - $anchor.blur(); var href = wpAjax.unserialize( $anchor.attr( 'href' ) ); var $parent = $anchor.parents( '.autoupdates_column' ); + var html; + var data; + + event.preventDefault(); + // Clear errors $parent.find( '.notice' ).remove(); - var html = $parent.html(); + html = $parent.html(); + + $anchor.blur(); //? // Show loading status. $anchor.addClass( 'spin' ).find( '.plugin-autoupdate-label' ).html( ' ' + wp_autoupdates.enabling ); - $.post( - ajaxurl, - { - action: 'enable_auto_updates', - _ajax_nonce: href._wpnonce, - type: 'plugin', - asset: href.plugin - }, - function (response) { + data = { + action: 'enable_auto_updates', + _ajax_nonce: href._wpnonce, + type: 'plugin', + asset: href.plugin + }; + + $.post( ajaxurl, data ).done( function( response ) { + var errorHTML; - } - ) - .done(function (response) { if ( response.success ) { - $( '.autoupdate_enabled span' ).html( response.data.enabled_count ); - $( '.autoupdate_disabled span' ).html( response.data.disabled_count ); + updateCounts( 1 ); + + // TODO: avoid injecting external HTML $parent.html( response.data.return_html ); - $parent.find('.plugin-autoupdate-disable').focus(); + + // TODO: fix changing the focus + $parent.find( '.plugin-autoupdate-disable' ).focus(); wp.a11y.speak( wp_autoupdates.auto_enabled, 'polite' ); } else { - var errorHTML = add_error_notice( html, response.data.error ); + // TODO: avoid injecting external HTML + errorHTML = add_error_notice( html, response.data.error ); wp.a11y.speak( response.data.error, 'polite' ); + + // TODO: avoid injecting external HTML $parent.html( errorHTML ); } - }) - .fail(function (response) { + }).fail( function( response ) { + // TODO: avoid injecting external HTML var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error ); wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' ); + + // TODO: avoid injecting external HTML $parent.html( errorHTML ); - }) - .always(function (response) { }); }); + // Disable auto-updates for a theme. - $('.autoupdates_column').on('click', 'a.theme-autoupdate-disable', function (e) { - e.preventDefault(); + $( '.autoupdates_column' ).on( 'click', 'a.theme-autoupdate-disable', function( event ) { var $anchor = $( this ); - $anchor.blur(); var href = wpAjax.unserialize($anchor.attr( 'href' ) ); var $parent = $anchor.parents( '.autoupdates_column' ); + var html; + var data; + + event.preventDefault(); + $anchor.blur(); + // Clear errors $parent.find( '.notice' ).remove(); - var html = $parent.html(); + html = $parent.html(); // Show loading status. $anchor.html( ' ' + wp_autoupdates.disabling ); - $.post( - ajaxurl, - { - action: 'disable_auto_updates', - _ajax_nonce: href._wpnonce, - type: 'theme', - asset: href.theme - }, - function (response) { + data = { + action: 'disable_auto_updates', + _ajax_nonce: href._wpnonce, + type: 'theme', + asset: href.theme + }; + + $.post( ajaxurl, data ).done( function( response ) { + var errorHTML; - } - ) - .done(function (response) { if ( response.success ) { - $( '.autoupdate_enabled span' ).html( response.data.enabled_count ); - $( '.autoupdate_disabled span' ).html( response.data.disabled_count ); + updateCounts( -1 ); + + // TODO: avoid injecting external HTML $parent.html( response.data.return_html ); $parent.find('.theme-autoupdate-enable').focus(); wp.a11y.speak( wp_autoupdates.auto_disabled, 'polite' ); } else { - var errorHTML = add_error_notice( html, response.data.error ); + errorHTML = add_error_notice( html, response.data.error ); wp.a11y.speak( response.data.error, 'polite' ); + + // TODO: avoid injecting external HTML $parent.html( errorHTML ); } - }) - .fail(function (response) { + }).fail( function( response ) { var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error ); wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' ); + + // TODO: avoid injecting external HTML $parent.html( errorHTML ); - }) - .always(function (response) { }); }); + // Enable auto-updates for a theme. - $('.autoupdates_column').on('click', 'a.theme-autoupdate-enable', function (e) { - e.preventDefault(); + // TODO: merge with the above function/avoid repeating the same + $( '.autoupdates_column' ).on( 'click', 'a.theme-autoupdate-enable', function( event ) { var $anchor = $( this ); - $anchor.blur(); var href = wpAjax.unserialize( $anchor.attr( 'href' ) ); var $parent = $anchor.parents( '.autoupdates_column' ); + var html; + var data; + + event.preventDefault(); + $anchor.blur(); // TODO: better management of focus + // Clear errors $parent.find( '.notice' ).remove(); - var html = $parent.html(); + html = $parent.html(); // Show loading status. + // TODO: fix $anchor.addClass( 'spin' ).find( '.theme-autoupdate-label' ).html( ' ' + wp_autoupdates.enabling ); - $.post( - ajaxurl, - { - action: 'enable_auto_updates', - _ajax_nonce: href._wpnonce, - type: 'theme', - asset: href.theme - }, - function (response) { + data = { + action: 'enable_auto_updates', + _ajax_nonce: href._wpnonce, + type: 'theme', + asset: href.theme, + }; - } - ) - .done(function (response) { + $.post( ajaxurl, data ).done( function( response ) { if ( response.success ) { - $( '.autoupdate_enabled span' ).html( response.data.enabled_count ); - $( '.autoupdate_disabled span' ).html( response.data.disabled_count ); + updateCounts( 1 ); + + // TODO: avoid injecting external HTML $parent.html( response.data.return_html ); + + $parent.find('.theme-autoupdate-disable').focus(); wp.a11y.speak( wp_autoupdates.auto_enabled, 'polite' ); } else { @@ -183,15 +240,17 @@ jQuery(function ($) { wp.a11y.speak( response.data.error, 'polite' ); $parent.html( errorHTML ); } - }) - .fail(function (response) { + }).fail( function() { var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error ); wp.a11y.speak( wp_autoupdates.auto_update_error, 'polite' ); + + // TODO: avoid injecting external HTML $parent.html( errorHTML ); - }) - .always(function (response) { }); }); + + + // TODO: Needs refactoring. Code is nearly identical. Merge with the above. // Disable auto-updates for a theme. $('.theme-overlay').on('click', 'a.theme-autoupdate-disable', function (e) { e.preventDefault(); @@ -237,6 +296,8 @@ jQuery(function ($) { .always(function (response) { }); }); + + // TODO: Needs refactoring. Code is nearly identical. Merge with the above. // Enable auto-updates for a theme. $('.theme-overlay').on('click', 'a.theme-autoupdate-enable', function (e) { e.preventDefault(); @@ -273,7 +334,7 @@ jQuery(function ($) { wp.a11y.speak( response.data.error, 'polite' ); $parent.html( errorHTML ); } - + }) .fail(function (response) { var errorHTML = add_error_notice( html, wp_autoupdates.auto_update_error ); @@ -283,4 +344,4 @@ jQuery(function ($) { .always(function (response) { }); }); -}); \ No newline at end of file +});