| 32 | } elseif ( $_POST && !empty( $_POST['block-directory-edit'] ) ) { |
| 33 | $post = get_post( intval( $_POST['plugin-id'] ) ); |
| 34 | if ( $post && wp_verify_nonce( $_POST['block-directory-nonce'], 'block-directory-edit-' . $post->ID ) ) { |
| 35 | if ( current_user_can( 'edit_post', $post->ID ) ) { |
| 36 | $terms = wp_list_pluck( get_the_terms( $post->ID, 'plugin_section' ), 'slug' ); |
| 37 | if ( 'add' === $_POST['block-directory-edit'] ) { |
| 38 | $terms[] = 'block'; |
| 39 | } elseif ( 'remove' === $_POST['block-directory-edit'] ) { |
| 40 | $terms = array_diff( $terms, array( 'block' ) ); |
| 41 | } |
| 42 | wp_set_object_terms( $post->ID, $terms, 'plugin_section' ); |
| 43 | } |
| 44 | |
| 45 | self::validate_block( $post->post_name ); |
| 46 | } |
| 58 | protected static function get_plugin_by_slug( $slug ) { |
| 59 | if ( !trim( $slug ) ) |
| 60 | return false; |
| 61 | |
| 62 | $args = array( |
| 63 | 'post_type' => 'plugin', |
| 64 | 'post_status' => 'publish', |
| 65 | 'posts_per_page' => 1, |
| 66 | 'name' => $slug, |
| 67 | ); |
| 68 | $query = new \WP_Query( $args ); |
| 69 | |
| 70 | if ( 1 === count( $query->posts ) ) |
| 71 | return $query->posts[0]; |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | protected static function plugin_is_in_block_directory( $slug ) { |
| 77 | $plugin = self::get_plugin_by_slug( $slug ); |
| 78 | |
| 79 | return ( |
| 80 | $plugin && |
| 81 | $plugin->post_name === $slug && |
| 82 | has_term( 'block', 'plugin_section', $plugin ) |
| 83 | ); |
| 84 | } |
| 85 | |
| 108 | if ( $checker->slug ) { |
| 109 | $plugin = self::get_plugin_by_slug( $checker->slug ); |
| 110 | if ( current_user_can( 'edit_post', $plugin->ID ) ) { |
| 111 | echo '<form method="post" action="">'; |
| 112 | echo '<fieldset>'; |
| 113 | echo '<legend>' . __( 'Plugin Review Tools', 'wporg-plugins' ) . '</legend>'; |
| 114 | echo wp_nonce_field( 'block-directory-edit-' . $plugin->ID, 'block-directory-nonce' ); |
| 115 | echo '<input type="hidden" name="plugin-id" value="' . esc_attr( $plugin->ID ) . '" />'; |
| 116 | if ( self::plugin_is_in_block_directory( $checker->slug ) ) { |
| 117 | echo '<button type="submit" name="block-directory-edit" value="remove">' . __( 'Remove from Block Directory', 'wporg-plugins' ) . '</button>'; |
| 118 | } else { |
| 119 | echo '<button type="submit" name="block-directory-edit" value="add">' . __( 'Add to Block Directory', 'wporg-plugins' ) . '</button>'; |
| 120 | } |
| 121 | |
| 122 | echo '<ul><li><a href="' . get_edit_post_link( $plugin->ID ) . '">' . __( 'Edit plugin', 'wporg-plugins' ) . '</a></li>'; |
| 123 | echo '<li><a href="' . esc_url( 'https://plugins.trac.wordpress.org/browser/' . $checker->slug . '/trunk' ) .'">' . __( 'Trac browser', 'wporg-plugins' ) . '</a></li></ul>'; |
| 124 | echo '</fieldset>'; |
| 125 | } |
| 126 | } |
| 127 | |