diff --git a/projects/packages/jetpack-mu-wpcom/changelog/add-set-default-category-quick-action b/projects/packages/jetpack-mu-wpcom/changelog/add-set-default-category-quick-action new file mode 100644 index 0000000000000..562cea91dca9a --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/add-set-default-category-quick-action @@ -0,0 +1,4 @@ +Significance: patch +Type: added + +Post categories: Add quick action to change default category diff --git a/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php b/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php index 26cf4192d604a..89597cf7ce895 100644 --- a/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php +++ b/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php @@ -107,6 +107,7 @@ public static function load_features() { require_once __DIR__ . '/features/import-customizations/import-customizations.php'; require_once __DIR__ . '/features/marketplace-products-updater/class-marketplace-products-updater.php'; require_once __DIR__ . '/features/media/heif-support.php'; + require_once __DIR__ . '/features/post-categories/quick-actions.php'; require_once __DIR__ . '/features/site-editor-dashboard-link/site-editor-dashboard-link.php'; require_once __DIR__ . '/features/wpcom-admin-dashboard/wpcom-admin-dashboard.php'; require_once __DIR__ . '/features/wpcom-block-editor/class-jetpack-wpcom-block-editor.php'; diff --git a/projects/packages/jetpack-mu-wpcom/src/features/post-categories/quick-actions.php b/projects/packages/jetpack-mu-wpcom/src/features/post-categories/quick-actions.php new file mode 100644 index 0000000000000..3259f07466dca --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/src/features/post-categories/quick-actions.php @@ -0,0 +1,87 @@ +term_id === $default_category ) { + return $actions; + } + + $action = 'set-default'; + + $link = add_query_arg( array( $action => $category->term_id ) ); + $link = wp_nonce_url( $link, $action . '_' . $category->term_id ); + + $actions[ $action ] = sprintf( + '%3$s', + esc_url( $link ), + /* translators: category name */ + esc_attr( sprintf( __( 'Set “%s” as the default category', 'jetpack-mu-wpcom' ), $category->name ) ), + esc_html( __( 'Set as default', 'jetpack-mu-wpcom' ) ) + ); + return $actions; +} +add_filter( 'category_row_actions', 'wpcom_add_set_default_category_quick_action', 10, 2 ); + +/** + * Changes the default post category. + */ +function wpcom_set_default_category() { + if ( ! isset( $_GET['taxonomy'] ) || 'category' !== sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) ) { + return; + } + + if ( ! current_user_can( 'manage_options' ) ) { + return; + } + + $action = 'set-default'; + + if ( ! isset( $_GET[ $action ] ) ) { + return; + } + + $category_id = sanitize_text_field( wp_unslash( $_GET[ $action ] ) ); + if ( ! is_numeric( $category_id ) ) { + return; + } + + check_admin_referer( $action . '_' . $category_id ); + + $category = get_category( (int) $category_id ); + if ( is_wp_error( $category ) || ! $category ) { + return; + } + + update_option( 'default_category', $category->term_id ); + + add_action( + 'admin_notices', + function () { + wp_admin_notice( + __( 'Default category changed successfully.', 'jetpack-mu-wpcom' ), + array( + 'type' => 'success', + 'dismissible' => true, + ) + ); + } + ); +} +add_action( 'load-edit-tags.php', 'wpcom_set_default_category' );