| | 31 | |
| | 32 | <details> |
| | 33 | <summary>Or upload a plugin ZIP file.</summary> |
| | 34 | <form id="upload_form" class="plugin-upload-form" enctype="multipart/form-data" method="POST" action=""> |
| | 35 | <?php wp_nonce_field( 'wporg-block-upload', 'block-upload-nonce' ); ?> |
| | 36 | <input type="hidden" name="action" value="upload"/> |
| | 37 | |
| | 38 | <input type="file" id="zip_file" class="plugin-file" name="zip_file" size="25" accept=".zip"/> |
| | 39 | <label class="button button-secondary" for="zip_file"><?php _e( 'Select File', 'wporg-plugins' ); ?></label> |
| | 40 | |
| | 41 | <input id="upload_button" name="block-directory-upload" class="button button-primary" type="submit" value="<?php esc_attr_e( 'Upload', 'wporg-plugins' ); ?>"/> |
| | 42 | |
| | 43 | <p> |
| | 44 | <small> |
| | 45 | <?php |
| | 46 | printf( |
| | 47 | /* translators: Maximum allowed file size. */ |
| | 48 | esc_html__( 'Maximum allowed file size: %s', 'wporg-plugins' ), |
| | 49 | esc_html( size_format( wp_max_upload_size() ) ) |
| | 50 | ); |
| | 51 | ?> |
| | 52 | </small> |
| | 53 | </p> |
| | 54 | </form> |
| | 55 | |
| | 56 | <?php |
| | 57 | $upload_script = ' |
| | 58 | ( function ( $ ) { |
| | 59 | var $label = $( "label.button" ), |
| | 60 | labelText = $label.text(); |
| | 61 | $( "#zip_file" ) |
| | 62 | .on( "change", function( event ) { |
| | 63 | var fileName = event.target.value.split( "\\\\" ).pop(); |
| | 64 | fileName ? $label.text( fileName ) : $label.text( labelText ); |
| | 65 | } ) |
| | 66 | .on( "focus", function() { $label.addClass( "focus" ); } ) |
| | 67 | .on( "blur", function() { $label.removeClass( "focus" ); } ); |
| | 68 | } ( window.jQuery ) );'; |
| | 69 | |
| | 70 | if ( ! wp_script_is( 'jquery', 'done' ) ) { |
| | 71 | wp_enqueue_script( 'jquery' ); |
| | 72 | wp_add_inline_script( 'jquery-migrate', $upload_script ); |
| | 73 | } else { |
| | 74 | printf( '<script>%s</script>', $upload_script ); |
| | 75 | } |
| | 76 | ?> |
| | 77 | </details> |
| 35 | | $post = get_post( intval( $_POST['plugin-id'] ) ); |
| 36 | | if ( $post && wp_verify_nonce( $_POST['block-directory-nonce'], 'block-directory-edit-' . $post->ID ) ) { |
| 37 | | if ( current_user_can( 'edit_post', $post->ID ) || current_user_can( 'plugin_admin_edit', $post->ID ) ) { |
| 38 | | $terms = wp_list_pluck( get_the_terms( $post->ID, 'plugin_section' ), 'slug' ); |
| 39 | | if ( 'add' === $_POST['block-directory-edit'] ) { |
| 40 | | $terms[] = 'block'; |
| 41 | | } elseif ( 'remove' === $_POST['block-directory-edit'] ) { |
| 42 | | $terms = array_diff( $terms, array( 'block' ) ); |
| 43 | | } |
| 44 | | $result = wp_set_object_terms( $post->ID, $terms, 'plugin_section' ); |
| 45 | | if ( !is_wp_error( $result ) && !empty( $result ) ) { |
| 46 | | if ( 'add' === $_POST['block-directory-edit'] ) { |
| 47 | | Tools::audit_log( 'Plugin added to block directory.', $post->ID ); |
| 48 | | self::maybe_send_email_plugin_added( $post ); |
| 49 | | Plugin_Import::queue( $post->post_name, array( 'tags_touched' => array( $post->stable_tag ) ) ); |
| 50 | | echo '<div class="notice notice-success notice-alt"><p>' . __( 'Plugin added to the block directory.', 'wporg-plugins' ) . '</p></div>'; |
| 51 | | } elseif ( 'remove' === $_POST['block-directory-edit'] ) { |
| 52 | | Tools::audit_log( 'Plugin removed from block directory.', $post->ID ); |
| 53 | | echo '<div class="notice notice-info notice-alt"><p>' . __( 'Plugin removed from the block directory.', 'wporg-plugins' ) . '</p></div>'; |
| 54 | | } |
| 55 | | } |
| 56 | | } |
| 57 | | |
| 58 | | self::validate_block( $post->post_name ); |
| 59 | | } |
| | 85 | self::handle_edit_form(); |
| | 97 | protected static function handle_file_upload() { |
| | 98 | if ( |
| | 99 | ! empty( $_POST['block-upload-nonce'] ) |
| | 100 | && wp_verify_nonce( $_POST['block-upload-nonce'], 'wporg-block-upload' ) |
| | 101 | && 'upload' === $_POST['action'] |
| | 102 | ) { |
| | 103 | if ( UPLOAD_ERR_OK === $_FILES['zip_file']['error'] ) { |
| | 104 | self::validate_block_from_zip( $_FILES['zip_file']['tmp_name'] ); |
| | 105 | } else { |
| | 106 | $message = __( 'Error in file upload.', 'wporg-plugins' ); |
| | 107 | } |
| | 108 | |
| | 109 | if ( ! empty( $message ) ) { |
| | 110 | echo "<div class='notice notice-warning notice-alt'><p>{$message}</p></div>\n"; |
| | 111 | } |
| | 112 | } |
| | 113 | |
| | 114 | } |
| | 115 | |
| | 116 | protected static function handle_edit_form() { |
| | 117 | $post = get_post( intval( $_POST['plugin-id'] ) ); |
| | 118 | if ( $post && wp_verify_nonce( $_POST['block-directory-nonce'], 'block-directory-edit-' . $post->ID ) ) { |
| | 119 | if ( current_user_can( 'edit_post', $post->ID ) || current_user_can( 'plugin_admin_edit', $post->ID ) ) { |
| | 120 | $terms = wp_list_pluck( get_the_terms( $post->ID, 'plugin_section' ), 'slug' ); |
| | 121 | if ( 'add' === $_POST['block-directory-edit'] ) { |
| | 122 | $terms[] = 'block'; |
| | 123 | } elseif ( 'remove' === $_POST['block-directory-edit'] ) { |
| | 124 | $terms = array_diff( $terms, array( 'block' ) ); |
| | 125 | } |
| | 126 | $result = wp_set_object_terms( $post->ID, $terms, 'plugin_section' ); |
| | 127 | if ( !is_wp_error( $result ) && !empty( $result ) ) { |
| | 128 | if ( 'add' === $_POST['block-directory-edit'] ) { |
| | 129 | Tools::audit_log( 'Plugin added to block directory.', $post->ID ); |
| | 130 | self::maybe_send_email_plugin_added( $post ); |
| | 131 | Plugin_Import::queue( $post->post_name, array( 'tags_touched' => array( $post->stable_tag ) ) ); |
| | 132 | echo '<div class="notice notice-success notice-alt"><p>' . __( 'Plugin added to the block directory.', 'wporg-plugins' ) . '</p></div>'; |
| | 133 | } elseif ( 'remove' === $_POST['block-directory-edit'] ) { |
| | 134 | Tools::audit_log( 'Plugin removed from block directory.', $post->ID ); |
| | 135 | echo '<div class="notice notice-info notice-alt"><p>' . __( 'Plugin removed from the block directory.', 'wporg-plugins' ) . '</p></div>'; |
| | 136 | } |
| | 137 | } |
| | 138 | } |
| | 139 | |
| | 140 | return self::validate_block( $post->post_name ); |
| | 141 | } |
| | 142 | } |
| | 143 | |
| | 188 | /** |
| | 189 | * Validates a block plugin to check that blocks are correctly registered and detectable. |
| | 190 | * |
| | 191 | * @param string $plugin_url The URL of a Subversion or GitHub repository. |
| | 192 | */ |
| | 193 | protected static function validate_block_from_zip( $zip_file ) { |
| | 194 | $path = Tools\Filesystem::unzip( $zip_file ); |
| | 195 | $checker = new Block_Plugin_Checker(); |
| | 196 | $results = $checker->run_check_plugin_files( $path ); |
| | 197 | self::display_results( $checker ); |
| | 198 | } |
| | 199 | |
| | 200 | /** |
| | 201 | * Display the results of a Block_Plugin_Checker run. |
| | 202 | * |
| | 203 | * @param array $results The Block_Plugin_Checker output. |
| | 204 | */ |
| | 205 | protected static function display_results( $checker ) { |
| | 206 | |