Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 95 |
|
0.00% |
0 / 4 |
CRAP | n/a |
0 / 0 |
|
| wpcom_page_quick_actions | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
90 | |||
| wpcom_validate_quick_action | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
240 | |||
| wpcom_set_homepage | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| wpcom_set_posts_page | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Quick actions for the Pages list page. |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Adds quick actions to change the homepage and post page. |
| 10 | * |
| 11 | * @param string[] $actions An array of action links to be displayed. |
| 12 | * @param WP_Post $page Page object. |
| 13 | * |
| 14 | * @return string[] Filtered actions. |
| 15 | */ |
| 16 | function wpcom_page_quick_actions( $actions, $page ) { |
| 17 | if ( ! current_user_can( 'manage_options' ) ) { |
| 18 | return $actions; |
| 19 | } |
| 20 | |
| 21 | if ( $page->post_status !== 'publish' ) { |
| 22 | return $actions; |
| 23 | } |
| 24 | |
| 25 | $homepage_id = (int) get_option( 'page_on_front' ); |
| 26 | $posts_page_id = (int) get_option( 'page_for_posts' ); |
| 27 | $has_static_homepage = 'page' === get_option( 'show_on_front' ) && (bool) $homepage_id; |
| 28 | $is_homepage = $page->ID === $homepage_id; |
| 29 | $is_posts_page = $page->ID === $posts_page_id; |
| 30 | |
| 31 | if ( ! $has_static_homepage || $is_homepage ) { |
| 32 | return $actions; |
| 33 | } |
| 34 | |
| 35 | $set_homepage_action = 'set-homepage'; |
| 36 | $set_posts_page_action = 'set-posts-page'; |
| 37 | |
| 38 | $cleaned_current_url = remove_query_arg( array( $set_homepage_action, $set_posts_page_action, '_wpnonce' ) ); |
| 39 | |
| 40 | $set_homepage_link = add_query_arg( $set_homepage_action, $page->ID, $cleaned_current_url ); |
| 41 | $set_homepage_link = wp_nonce_url( $set_homepage_link, $set_homepage_action . '_' . $page->ID ); |
| 42 | |
| 43 | $actions[ $set_homepage_action ] = sprintf( |
| 44 | '<a href="%1$s" aria-label="%2$s">%3$s</a>', |
| 45 | esc_url( $set_homepage_link ), |
| 46 | /* translators: page title */ |
| 47 | esc_attr( sprintf( __( 'Set “%s” as your site\'s homepage', 'jetpack-mu-wpcom' ), $page->post_title ) ), |
| 48 | esc_html( __( 'Set as homepage', 'jetpack-mu-wpcom' ) ) |
| 49 | ); |
| 50 | |
| 51 | $new_posts_page = $is_posts_page ? 0 : $page->ID; |
| 52 | |
| 53 | $set_posts_page_link = add_query_arg( $set_posts_page_action, $new_posts_page, $cleaned_current_url ); |
| 54 | $set_posts_page_link = wp_nonce_url( $set_posts_page_link, $set_posts_page_action . '_' . $new_posts_page ); |
| 55 | /* translators: page title */ |
| 56 | $set_posts_page_label = $is_posts_page ? sprintf( __( 'Unset “%s” as the page that displays your latest posts', 'jetpack-mu-wpcom' ), $page->post_title ) : sprintf( __( 'Set “%s” as the page that displays your latest posts', 'jetpack-mu-wpcom' ), $page->post_title ); |
| 57 | $set_posts_page_text = $is_posts_page ? __( 'Unset as posts page', 'jetpack-mu-wpcom' ) : __( 'Set as posts page', 'jetpack-mu-wpcom' ); |
| 58 | |
| 59 | $actions[ $set_posts_page_action ] = sprintf( |
| 60 | '<a href="%1$s" aria-label="%2$s">%3$s</a>', |
| 61 | esc_url( $set_posts_page_link ), |
| 62 | esc_attr( $set_posts_page_label ), |
| 63 | esc_html( $set_posts_page_text ) |
| 64 | ); |
| 65 | |
| 66 | return $actions; |
| 67 | } |
| 68 | add_filter( 'page_row_actions', 'wpcom_page_quick_actions', 10, 2 ); |
| 69 | |
| 70 | /** |
| 71 | * Checks if the current request can perform a quick action valid for a given page. |
| 72 | * |
| 73 | * @param string $action Action name ('set-homepage', 'set-posts-page'). |
| 74 | * |
| 75 | * @return false|int The page ID is the request is valid, false otherwise. |
| 76 | */ |
| 77 | function wpcom_validate_quick_action( $action ) { |
| 78 | global $pagenow; |
| 79 | |
| 80 | if ( 'edit.php' !== $pagenow ) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if ( ! isset( $_GET['post_type'] ) || 'page' !== sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) ) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if ( ! current_user_can( 'manage_options' ) ) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if ( ! isset( $_GET[ $action ] ) ) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | $page_id = sanitize_text_field( wp_unslash( $_GET[ $action ] ) ); |
| 97 | if ( ! is_numeric( $page_id ) ) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | check_admin_referer( $action . '_' . $page_id ); |
| 102 | |
| 103 | $homepage_id = (int) get_option( 'page_on_front' ); |
| 104 | $has_static_homepage = 'page' === get_option( 'show_on_front' ) && (bool) $homepage_id; |
| 105 | |
| 106 | if ( ! $has_static_homepage ) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | $page_id = (int) $page_id; |
| 111 | |
| 112 | if ( $action === 'set-posts-page' && $page_id === 0 ) { |
| 113 | return $page_id; |
| 114 | } |
| 115 | |
| 116 | if ( $page_id === $homepage_id ) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | $page = get_post( $page_id ); |
| 121 | if ( ! ( $page instanceof WP_Post ) || $page->post_type !== 'page' || $page->post_status !== 'publish' ) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | return $page_id; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Changes the homepage. |
| 130 | */ |
| 131 | function wpcom_set_homepage() { |
| 132 | $new_homepage_id = wpcom_validate_quick_action( 'set-homepage' ); |
| 133 | if ( ! is_int( $new_homepage_id ) ) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | update_option( 'page_on_front', $new_homepage_id ); |
| 138 | |
| 139 | add_action( |
| 140 | 'admin_notices', |
| 141 | function () { |
| 142 | wp_admin_notice( |
| 143 | __( 'Homepage changed successfully.', 'jetpack-mu-wpcom' ), |
| 144 | array( |
| 145 | 'type' => 'success', |
| 146 | 'dismissible' => true, |
| 147 | ) |
| 148 | ); |
| 149 | } |
| 150 | ); |
| 151 | } |
| 152 | add_action( 'init', 'wpcom_set_homepage', 0 ); // Before masterbar_init_wp_posts_list |
| 153 | |
| 154 | /** |
| 155 | * Changes the posts_page. |
| 156 | */ |
| 157 | function wpcom_set_posts_page() { |
| 158 | $new_posts_page_id = wpcom_validate_quick_action( 'set-posts-page' ); |
| 159 | if ( ! is_int( $new_posts_page_id ) ) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | update_option( 'page_for_posts', $new_posts_page_id ); |
| 164 | |
| 165 | add_action( |
| 166 | 'admin_notices', |
| 167 | function () { |
| 168 | wp_admin_notice( |
| 169 | __( 'Posts page changed successfully.', 'jetpack-mu-wpcom' ), |
| 170 | array( |
| 171 | 'type' => 'success', |
| 172 | 'dismissible' => true, |
| 173 | ) |
| 174 | ); |
| 175 | } |
| 176 | ); |
| 177 | } |
| 178 | add_action( 'init', 'wpcom_set_posts_page', 0 ); // Before masterbar_init_wp_posts_list |