Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
wpcom_add_set_default_category_quick_action
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
wpcom_set_default_category
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Quick actions for the categories list page.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8/**
9 * Adds a quick action to change the default post category.
10 *
11 * @param string[] $actions An array of action links to be displayed.
12 * @param WP_Term  $category Category object.
13 *
14 * @return string[] Filtered actions.
15 */
16function wpcom_add_set_default_category_quick_action( $actions, $category ) {
17    if ( ! current_user_can( 'manage_options' ) ) {
18        return $actions;
19    }
20
21    $default_category = (int) get_option( 'default_category' );
22    if ( $category->term_id === $default_category ) {
23        return $actions;
24    }
25
26    $action = 'set-default';
27
28    $base_url = admin_url( 'edit-tags.php?taxonomy=category' );
29    $link     = add_query_arg( array( $action => $category->term_id ), $base_url );
30    $link     = wp_nonce_url( $link, $action . '_' . $category->term_id );
31
32    $actions[ $action ] = sprintf(
33        '<a href="%1$s" aria-label="%2$s">%3$s</a>',
34        esc_url( $link ),
35        /* translators: category name */
36        esc_attr( sprintf( __( 'Set &#8220;%s&#8221; as the default category', 'jetpack-mu-wpcom' ), $category->name ) ),
37        esc_html( __( 'Set as default', 'jetpack-mu-wpcom' ) )
38    );
39    return $actions;
40}
41add_filter( 'category_row_actions', 'wpcom_add_set_default_category_quick_action', 10, 2 );
42
43/**
44 * Changes the default post category.
45 */
46function wpcom_set_default_category() {
47    if ( ! isset( $_GET['taxonomy'] ) || 'category' !== sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) ) {
48        return;
49    }
50
51    if ( ! current_user_can( 'manage_options' ) ) {
52        return;
53    }
54
55    $action = 'set-default';
56
57    if ( ! isset( $_GET[ $action ] ) ) {
58        return;
59    }
60
61    $category_id = sanitize_text_field( wp_unslash( $_GET[ $action ] ) );
62    if ( ! is_numeric( $category_id ) ) {
63        return;
64    }
65
66    check_admin_referer( $action . '_' . $category_id );
67
68    $category = get_category( (int) $category_id );
69    if ( is_wp_error( $category ) || ! $category ) {
70        return;
71    }
72
73    update_option( 'default_category', $category->term_id );
74
75    add_action(
76        'admin_notices',
77        function () {
78            wp_admin_notice(
79                __( 'Default category changed successfully.', 'jetpack-mu-wpcom' ),
80                array(
81                    'type'        => 'success',
82                    'dismissible' => true,
83                )
84            );
85        }
86    );
87}
88add_action( 'load-edit-tags.php', 'wpcom_set_default_category' );