Changeset 13109 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-upload-handler.php
- Timestamp:
- 01/15/2024 05:23:52 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-upload-handler.php
r13021 r13109 2 2 namespace WordPressdotorg\Plugin_Directory\Shortcodes; 3 3 4 use WP_Error; 4 5 use WordPressdotorg\Plugin_Directory\CLI\Import; 5 6 use WordPressdotorg\Plugin_Directory\Readme\Parser; … … 58 59 * Runs various tests and creates plugin post. 59 60 * 61 * @param int $for_plugin Optional. The plugin being uploaded to. This is used when adding additional .zip files. 62 * 60 63 * @return string|WP_Error Confirmation message on success, WP_Error object on failure. 61 64 */ 62 public function process_upload( ) {65 public function process_upload( $for_plugin = 0 ) { 63 66 if ( UPLOAD_ERR_OK !== $_FILES['zip_file']['error'] ) { 64 return new \WP_Error( 'error_upload', __( 'Error in file upload.', 'wporg-plugins' ) );67 return new WP_Error( 'error_upload', __( 'Error in file upload.', 'wporg-plugins' ) ); 65 68 } 66 69 67 70 // Validate the maximum upload size. 68 71 if ( $_FILES['zip_file']['size'] > wp_max_upload_size() ) { 69 return new \WP_Error( 'error_upload', __( 'Error in file upload.', 'wporg-plugins' ) );72 return new WP_Error( 'error_upload', __( 'Error in file upload.', 'wporg-plugins' ) ); 70 73 } 71 74 … … 73 76 $has_upload_token = $this->has_valid_upload_token(); 74 77 $this->plugin_dir = Filesystem::unzip( $zip_file ); 78 79 $plugin_post = $for_plugin ? get_post( $for_plugin ) : false; 80 $updating_existing = (bool) $plugin_post; 81 $this->plugin_slug = $plugin_post->post_name ?? ''; 82 83 if ( $for_plugin && ! $updating_existing ) { 84 return new WP_Error( 'error_upload', __( 'Error in file upload.', 'wporg-plugins' ) ); 85 } 86 87 // Allow plugin reviewers to bypass some restrictions. 88 if ( $updating_existing && current_user_can( 'approve_plugins' ) && ! $has_upload_token ) { 89 $has_upload_token = true; 90 } 91 92 // If the plugin was uploaded using a token, we'll assume future uploads for the plugin should use one. 93 if ( $updating_existing && ! $has_upload_token && $plugin_post->{'_used_upload_token'} ) { 94 $has_upload_token = true; 95 } 75 96 76 97 $plugin_data = (array) Import::find_plugin_headers( $this->plugin_dir, 1 /* Max Depth to search */ ); … … 85 106 $error = __( 'Error: The plugin has no name.', 'wporg-plugins' ); 86 107 87 return new \WP_Error( 'no_name', $error . ' ' . sprintf(108 return new WP_Error( 'no_name', $error . ' ' . sprintf( 88 109 /* translators: 1: plugin header line, 2: Documentation URL */ 89 110 __( 'Add a %1$s line to your main plugin file and upload the plugin again. For more information, please review our documentation on <a href="%2$s">Plugin Headers</a>.', 'wporg-plugins' ), … … 94 115 95 116 // Determine the plugin slug based on the name of the plugin in the main plugin file. 96 $this->plugin_slug = remove_accents( $this->plugin['Name'] ); 97 $this->plugin_slug = preg_replace( '/[^a-z0-9 _.-]/i', '', $this->plugin_slug ); 98 $this->plugin_slug = str_replace( '_', '-', $this->plugin_slug ); 99 $this->plugin_slug = sanitize_title_with_dashes( $this->plugin_slug ); 117 if ( ! $this->plugin_slug ) { 118 $this->plugin_slug = $this->generate_plugin_slug( $this->plugin['Name'] ); 119 } 100 120 101 121 if ( ! $this->plugin_slug ) { 102 122 $error = __( 'Error: The plugin has an unsupported name.', 'wporg-plugins' ); 103 123 104 return new \WP_Error( 'unsupported_name', $error . ' ' . sprintf(124 return new WP_Error( 'unsupported_name', $error . ' ' . sprintf( 105 125 /* translators: %s: 'Plugin Name:' */ 106 126 __( 'Plugin names may only contain latin letters (A-z), numbers, spaces, and hyphens. Please change the %s line in your main plugin file and readme, then you may upload it again.', 'wporg-plugins' ), … … 114 134 $error = __( 'Error: The plugin has a reserved name.', 'wporg-plugins' ); 115 135 116 return new \WP_Error( 'reserved_name', $error . ' ' . sprintf(136 return new WP_Error( 'reserved_name', $error . ' ' . sprintf( 117 137 /* translators: 1: plugin slug, 2: 'Plugin Name:' */ 118 138 __( 'Your chosen plugin name - %1$s - has been reserved or otherwise restricted from use entirely. Please change the %2$s line in your main plugin file and readme, then you may upload it again.', 'wporg-plugins' ), … … 123 143 124 144 // Make sure it doesn't use a TRADEMARK protected slug. 125 if ( false !== $this->has_trademarked_slug() && ! $has_upload_token ) { 145 if ( ! $updating_existing ) { 146 $has_trademarked_slug = $this->has_trademarked_slug( $this->plugin_slug ); 147 } else { 148 // If we're updating an existing plugin, we need to check the new name, but the slug may be different. 149 $has_trademarked_slug = $this->has_trademarked_slug( 150 $this->generate_plugin_slug( $this->plugin['Name'] ) 151 ); 152 } 153 if ( false !== $has_trademarked_slug && ! $has_upload_token ) { 126 154 $error = __( 'Error: The plugin name includes a restricted term.', 'wporg-plugins' ); 127 155 128 if ( $ this->has_trademarked_slug() === trim( $this->has_trademarked_slug(), '-' ) ) {156 if ( $has_trademarked_slug === trim( $has_trademarked_slug, '-' ) ) { 129 157 // Trademarks that do NOT end in "-" indicate slug cannot contain term at all. 130 158 $message = sprintf( 131 159 /* translators: 1: plugin slug, 2: trademarked term, 3: 'Plugin Name:', 4: plugin email address */ 132 160 __( 'Your chosen plugin name - %1$s - contains the restricted term "%2$s" and cannot be used at all in your plugin permalink nor the display name. To proceed with this submission you must remove "%2$s" from the %3$s line in both your main plugin file and readme entirely. Once you\'ve finished, you may upload the plugin again. Do not attempt to work around this by removing letters (i.e. WordPess) or using numbers (4 instead of A). Those are seen as intentional actions to avoid our restrictions, and are not permitted. If you feel this is in error, such as you legally own the trademark for a term, please email us at %4$s and explain your situation.', 'wporg-plugins' ), 133 '<code>' . $this->plugin_slug. '</code>',134 trim( $ this->has_trademarked_slug(), '-' ),161 '<code>' . esc_html( $this->plugin['Name'] ) . '</code>', 162 trim( $has_trademarked_slug, '-' ), 135 163 '<code>Plugin Name:</code>', 136 164 '<code>plugins@wordpress.org</code>' … … 141 169 /* translators: 1: plugin slug, 2: trademarked term, 3: 'Plugin Name:', 4: plugin email address */ 142 170 __( 'Your chosen plugin name - %1$s - contains the restricted term "%2$s" and cannot be used to begin your permalink or display name. We disallow the use of certain terms in ways that are abused, or potentially infringe on and/or are misleading with regards to trademarks. In order to proceed with this submission, you must change the %3$s line in your main plugin file and readme to end with "-%2$s" instead. Once you\'ve finished, you may upload the plugin again. If you feel this is in error, such as you legally own the trademark for the term, please email us at %4$s and explain your situation.', 'wporg-plugins' ), 143 '<code>' . $this->plugin_slug. '</code>',144 trim( $ this->has_trademarked_slug(), '-' ),171 '<code>' . esc_html( $this->plugin['Name'] ) . '</code>', 172 trim( $has_trademarked_slug, '-' ), 145 173 '<code>Plugin Name:</code>', 146 174 '<code>plugins@wordpress.org</code>' … … 148 176 } 149 177 150 return new \WP_Error( 'trademarked_name', $error . ' ' . $message ); 151 } 152 153 $plugin_post = Plugin_Directory::get_plugin_post( $this->plugin_slug ); 178 return new WP_Error( 'trademarked_name', $error . ' ' . $message ); 179 } 180 181 if ( ! $plugin_post ) { 182 $plugin_post = Plugin_Directory::get_plugin_post( $this->plugin_slug ); 183 } 154 184 155 185 // If no matching plugin by that slug, check to see if a plugin exists with that Title in the database. … … 170 200 $error = __( 'Error: The plugin already exists.', 'wporg-plugins' ); 171 201 172 return new \WP_Error( 'already_exists', $error . ' ' . sprintf(202 return new WP_Error( 'already_exists', $error . ' ' . sprintf( 173 203 /* translators: 1: plugin slug, 2: 'Plugin Name:' */ 174 204 __( 'There is already a plugin with the name %1$s in the directory. You must rename your plugin by changing the %2$s line in your main plugin file and in your readme. Once you have done so, you may upload it again.', 'wporg-plugins' ), … … 179 209 180 210 // Is there already a plugin with the same slug by the same author? 181 if ( $plugin_post ) {211 if ( $plugin_post && ! $updating_existing ) { 182 212 $error = __( 'Error: The plugin has already been submitted.', 'wporg-plugins' ); 183 213 184 return new \WP_Error( 'already_submitted', $error . ' ' . sprintf(214 return new WP_Error( 'already_submitted', $error . ' ' . sprintf( 185 215 /* translators: 1: plugin slug, 2: Documentation URL, 3: plugins@wordpress.org */ 186 216 __( 'You have already submitted a plugin named %1$s. There is no need to resubmit existing plugins, even for new versions. Instead, please update your plugin within the directory via <a href="%2$s">SVN</a>. If you need assistance, email <a href="mailto:%3$s">%3$s</a> and let us know.', 'wporg-plugins' ), … … 195 225 $error = __( 'Error: The plugin slug is too short.', 'wporg-plugins' ); 196 226 197 return new \WP_Error( 'trademarked_name', $error . ' ' . sprintf(227 return new WP_Error( 'trademarked_name', $error . ' ' . sprintf( 198 228 /* translators: 1: plugin slug, 2: 'Plugin Name:' */ 199 229 __( 'Your chosen plugin name - %1$s - is not permitted because it is too short. Please change the %2$s line in your main plugin file and readme to a different name. When you have finished, you may upload your plugin again.', 'wporg-plugins' ), … … 207 237 $error = __( 'Error: The plugin has no description.', 'wporg-plugins' ); 208 238 209 return new \WP_Error( 'no_description', $error . ' ' . sprintf(239 return new WP_Error( 'no_description', $error . ' ' . sprintf( 210 240 /* translators: 1: plugin header line, 2: Documentation URL */ 211 241 __( 'We cannot find a description in your plugin headers. Please add a %1$s line to your main plugin file and upload the complete plugin again. If you need more information, please review our documentation on <a href="%2$s">Plugin Headers</a>.', 'wporg-plugins' ), … … 219 249 $error = __( 'Error: The plugin has no version.', 'wporg-plugins' ); 220 250 221 return new \WP_Error( 'no_version', $error . ' ' . sprintf(251 return new WP_Error( 'no_version', $error . ' ' . sprintf( 222 252 /* translators: 1: plugin header line, 2: Documentation URL */ 223 253 __( 'We cannot find a version listed in your plugin headers. Please add a %1$s line to your main plugin file and upload the complete plugin again. If you need more information, please review our documentation on <a href="%2$s">Plugin Headers</a>.', 'wporg-plugins' ), … … 231 261 $error = __( 'Error: Plugin versions are expected to be numbers.', 'wporg-plugins' ); 232 262 233 return new \WP_Error( 'invalid_version', $error . ' ' . sprintf(263 return new WP_Error( 'invalid_version', $error . ' ' . sprintf( 234 264 /* translators: %s: 'Version:' */ 235 265 __( 'Version strings may only contain numeric and period characters (i.e. 1.2). Please correct the %s line in your main plugin file and upload the plugin again.', 'wporg-plugins' ), … … 240 270 // Prevent duplicate URLs. 241 271 // This is part of how the API looks for updates, so having them different helps prevent conflicts. 242 if ( ! empty( $this->plugin['PluginURI'] ) && ! empty( $this->plugin['AuthorURI'] ) && $this->plugin['PluginURI'] == $this->plugin['AuthorURI'] ) { 272 if ( 273 ! empty( $this->plugin['PluginURI'] ) && 274 ! empty( $this->plugin['AuthorURI'] ) && 275 $this->plugin['PluginURI'] == $this->plugin['AuthorURI'] 276 ) { 243 277 $error = __( 'Error: Your plugin and author URIs are the same.', 'wporg-plugins' ); 244 278 245 return new \WP_Error(279 return new WP_Error( 246 280 'plugin_author_uri', $error . ' ' . 247 281 __( 'Your plugin headers in the main plugin file headers have the same value for both the plugin and author URI (Uniform Resource Identifier). A plugin URI is a webpage that provides details about this specific plugin. An author URI is a webpage that provides information about the author of the plugin. Those two must be different. You are not required to provide both, so pick the one that best applies to your situation.', 'wporg-plugins' ) … … 250 284 251 285 // Prevent uploads using popular Plugin names in the wild. 252 if ( function_exists( 'wporg_stats_get_plugin_name_install_count' ) && ! $has_upload_token ) {286 if ( function_exists( 'wporg_stats_get_plugin_name_install_count' ) && ! $has_upload_token && ! $updating_existing ) { 253 287 $installs = wporg_stats_get_plugin_name_install_count( $this->plugin['Name'] ); 254 288 … … 256 290 $error = __( 'Error: That plugin name is already in use.', 'wporg-plugins' ); 257 291 258 return new \WP_Error( 'already_exists_in_the_wild', $error . ' ' . sprintf(292 return new WP_Error( 'already_exists_in_the_wild', $error . ' ' . sprintf( 259 293 /* translators: 1: plugin slug, 2: 'Plugin Name:' */ 260 294 __( 'There is already a plugin with the name %1$s known to exist, though it is not hosted on WordPress.org. This means the permalink %2$s is already in use, and has a significant user base. Were we to accept it as-is, our system would overwrite those other installs and potentially damage any existing users. This is especially true since WordPress 5.5 and up will automatically update plugins and themes. You must rename your plugin by changing the %3$s line in your main plugin file and in your readme. Once you have done so, you may upload it again. If you feel this is an incorrect assessment of the situation, please email <a href="mailto:%4$s">%4$s</a> and explain why so that we may help you.', 'wporg-plugins' ), … … 272 306 $error = __( 'Error: The plugin has no readme.', 'wporg-plugins' ); 273 307 274 return new \WP_Error( 'no_readme', $error . ' ' . sprintf(308 return new WP_Error( 'no_readme', $error . ' ' . sprintf( 275 309 /* translators: 1: readme.txt, 2: readme.md */ 276 310 __( 'The zip file must include a file named %1$s or %2$s. We recommend using %1$s as it will allow you to fully utilize our directory.', 'wporg-plugins' ), … … 283 317 // Double check no existing plugins clash with the readme title. 284 318 $readme_plugin_post = get_posts( array( 285 'post_type' => 'plugin', 286 'title' => $readme->name, 287 'post_status' => array( 'publish', 'pending', 'disabled', 'closed', 'new', 'draft', 'approved' ), 319 'post_type' => 'plugin', 320 'title' => $readme->name, 321 'post_status' => array( 'publish', 'pending', 'disabled', 'closed', 'new', 'draft', 'approved' ), 322 'post__not_in' => $plugin_post ? array( $plugin_post->ID ) : [], 288 323 ) ); 289 324 if ( $readme_plugin_post && trim( $readme->name ) ) { … … 291 326 292 327 if ( $readme_plugin_post->post_author != get_current_user_id() ) { 293 return new \WP_Error( 'already_submitted', $error . ' ' . sprintf(328 return new WP_Error( 'already_submitted', $error . ' ' . sprintf( 294 329 /* translators: 1: plugin slug, 2: 'Plugin Name:' */ 295 330 __( 'There is already a plugin with the name %1$s in the directory. You must rename your plugin by changing the %2$s line in your main plugin file and in your readme. Once you have done so, you may upload it again.', 'wporg-plugins' ), … … 299 334 } 300 335 301 return new \WP_Error( 'already_submitted', $error . ' ' . sprintf(336 return new WP_Error( 'already_submitted', $error . ' ' . sprintf( 302 337 /* translators: 1: plugin slug, 2: Documentation URL, 3: plugins@wordpress.org */ 303 338 __( 'You have already submitted a plugin named %1$s. There is no need to resubmit existing plugins, even for new versions. Instead, please update your plugin within the directory via <a href="%2$s">SVN</a>. If you need assistance, email <a href="mailto:%3$s">%3$s</a> and let us know.', 'wporg-plugins' ), … … 308 343 } 309 344 310 if ( function_exists( 'wporg_stats_get_plugin_name_install_count' ) && ! $has_upload_token ) {345 if ( function_exists( 'wporg_stats_get_plugin_name_install_count' ) && ! $has_upload_token && ! $updating_existing ) { 311 346 $installs = wporg_stats_get_plugin_name_install_count( $readme->name ); 312 347 … … 314 349 $error = __( 'Error: That plugin name is already in use.', 'wporg-plugins' ); 315 350 316 return new \WP_Error( 'already_exists_in_the_wild', $error . ' ' . sprintf(351 return new WP_Error( 'already_exists_in_the_wild', $error . ' ' . sprintf( 317 352 /* translators: 1: plugin slug, 2: 'Plugin Name:' */ 318 353 __( 'There is already a plugin with the name %1$s known to exist, though it is not hosted on WordPress.org. This means the permalink %2$s is already in use, and has a significant user base. Were we to accept it as-is, our system would overwrite those other installs and potentially damage any existing users. This is especially true since WordPress 5.5 and up will automatically update plugins and themes. You must rename your plugin by changing the %3$s line in your main plugin file and in your readme. Once you have done so, you may upload it again. If you feel this is an incorrect assessment of the situation, please email <a href="mailto:%4$s">%4$s</a> and explain why so that we may help you.', 'wporg-plugins' ), … … 329 364 $error = __( 'Error: No license defined.', 'wporg-plugins' ); 330 365 331 return new \WP_Error( 'no_license', $error . ' ' . sprintf(366 return new WP_Error( 'no_license', $error . ' ' . sprintf( 332 367 /* translators: 1: readme.txt */ 333 368 __( 'Your plugin has no license declared. Please update your %1$s with a GPLv2 (or later) compatible license.', 'wporg-plugins' ), … … 338 373 // Pass it through Plugin Check and see how great this plugin really is. 339 374 // We're not actually using this right now. 340 $ result = $this->check_plugin();341 342 if ( ! $ result && ! $has_upload_token ) {375 $plugin_check_result = $this->check_plugin(); 376 377 if ( ! $plugin_check_result && ! $has_upload_token ) { 343 378 $error = __( 'Error: The plugin has failed the automated checks.', 'wporg-plugins' ); 344 379 345 return new \WP_Error( 'failed_checks', $error . ' ' . sprintf(380 return new WP_Error( 'failed_checks', $error . ' ' . sprintf( 346 381 /* translators: 1: Plugin Check Plugin URL, 2: https://make.wordpress.org/plugins */ 347 382 __( 'Please correct the listed problems with your plugin and upload it again. You can also use the <a href="%1$s">Plugin Check Plugin</a> to test your plugin before uploading. If you have any questions about this please post them to %2$s.', 'wporg-plugins' ), 348 ' //wordpress.org/plugins/plugin-check/',383 'https://wordpress.org/plugins/plugin-check/', 349 384 '<a href="https://make.wordpress.org/plugins">https://make.wordpress.org/plugins</a>' 350 385 ) ); … … 354 389 // Let's save everything and get things wrapped up. 355 390 // Create a new post on first-time submissions. 391 $content = ''; 392 foreach ( $readme->sections as $section => $section_content ) { 393 $content .= "\n\n<!--section={$section}-->\n{$section_content}"; 394 } 395 396 $post_args = array( 397 'ID' => $plugin_post->ID ?? 0, 398 'post_title' => $this->plugin['Name'], 399 'post_name' => $this->plugin_slug, 400 'post_status' => $plugin_post->post_status ?? 'new', 401 'post_content' => $content, 402 'post_excerpt' => $this->plugin['Description'], 403 // 'tax_input' => wp_unslash( $_POST['tax_input'] ), // for category selection 404 'meta_input' => array( 405 'tested' => $readme->tested, 406 'requires' => $readme->requires, 407 'requires_php' => $readme->requires_php, 408 'stable_tag' => $readme->stable_tag, 409 'upgrade_notice' => $readme->upgrade_notice, 410 'contributors' => $readme->contributors, 411 'screenshots' => $readme->screenshots, 412 'donate_link' => $readme->donate_link, 413 'license' => $readme->license, 414 'license_uri' => $readme->license_uri, 415 'sections' => array_keys( $readme->sections ), 416 'version' => $this->plugin['Version'], 417 'header_name' => $this->plugin['Name'], 418 'header_plugin_uri' => $this->plugin['PluginURI'], 419 'header_author' => $this->plugin['Author'], 420 'header_author_uri' => $this->plugin['AuthorURI'], 421 'header_textdomain' => $this->plugin['TextDomain'], 422 'header_description' => $this->plugin['Description'], 423 'requires_plugins' => array_filter( array_map( 'trim', explode( ',', $this->plugin['RequiresPlugins'] ) ) ), 424 'assets_screenshots' => array(), 425 'assets_icons' => array(), 426 'assets_banners' => array(), 427 'assets_banners_color' => false, 428 'support_threads' => 0, 429 'support_threads_resolved' => 0, 430 'downloads' => 0, 431 'last_updated' => gmdate( 'Y-m-d H:i:s' ), 432 'rating' => 0, 433 'ratings' => array(), 434 'active_installs' => 0, 435 '_active_installs' => 0, 436 'usage' => array(), 437 ), 438 ); 439 440 // First time submission, track some additional metadata. 356 441 if ( ! $plugin_post ) { 357 $content = ''; 358 foreach ( $readme->sections as $section => $section_content ) { 359 $content .= "\n\n<!--section={$section}-->\n{$section_content}"; 360 } 361 362 // Add a Plugin Directory entry for this plugin. 363 $plugin_post = Plugin_Directory::create_plugin_post( array( 364 'post_title' => $this->plugin['Name'], 365 'post_name' => $this->plugin_slug, 366 'post_status' => 'new', 367 'post_content' => $content, 368 'post_excerpt' => $this->plugin['Description'], 369 // 'tax_input' => wp_unslash( $_POST['tax_input'] ), // for category selection 370 'meta_input' => array( 371 'tested' => $readme->tested, 372 'requires' => $readme->requires, 373 'requires_php' => $readme->requires_php, 374 'stable_tag' => $readme->stable_tag, 375 'upgrade_notice' => $readme->upgrade_notice, 376 'contributors' => $readme->contributors, 377 'screenshots' => $readme->screenshots, 378 'donate_link' => $readme->donate_link, 379 'license' => $readme->license, 380 'license_uri' => $readme->license_uri, 381 'sections' => array_keys( $readme->sections ), 382 'version' => $this->plugin['Version'], 383 'header_name' => $this->plugin['Name'], 384 'header_plugin_uri' => $this->plugin['PluginURI'], 385 'header_author' => $this->plugin['Author'], 386 'header_author_uri' => $this->plugin['AuthorURI'], 387 'header_textdomain' => $this->plugin['TextDomain'], 388 'header_description' => $this->plugin['Description'], 389 'requires_plugins' => array_filter( array_map( 'trim', explode( ',', $this->plugin['RequiresPlugins'] ) ) ), 390 'assets_screenshots' => array(), 391 'assets_icons' => array(), 392 'assets_banners' => array(), 393 'assets_banners_color' => false, 394 'support_threads' => 0, 395 'support_threads_resolved' => 0, 396 'downloads' => 0, 397 'last_updated' => gmdate( 'Y-m-d H:i:s' ), 398 'rating' => 0, 399 'ratings' => array(), 400 'active_installs' => 0, 401 '_active_installs' => 0, 402 'usage' => array(), 403 '_author_ip' => preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ), 404 '_submitted_date' => time(), 405 ), 406 ) ); 407 if ( is_wp_error( $plugin_post ) ) { 408 return $plugin_post->get_error_message(); 409 } 442 $post_args['meta_input']['_author_ip'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ); 443 $post_args['meta_input']['_submitted_date'] = time(); 444 $post_args['meta_input']['_used_upload_token'] = $has_upload_token; 445 } 446 447 // Add/Update the Plugin Directory entry for this plugin. 448 $plugin_post = Plugin_Directory::create_plugin_post( $post_args ); 449 450 if ( is_wp_error( $plugin_post ) ) { 451 return $plugin_post; 410 452 } 411 453 412 454 $attachment = $this->save_zip_file( $plugin_post->ID ); 413 455 if ( is_wp_error( $attachment ) ) { 414 return $attachment ->get_error_message();456 return $attachment; 415 457 } 416 458 … … 422 464 update_post_meta( $plugin_post->ID, '_submitted_zip_loc', $lines_of_code ); 423 465 466 do_action( 'plugin_upload', $this->plugin, $plugin_post ); 467 468 if ( $updating_existing ) { 469 return sprintf( 470 __( 'New version of %s uploaded for review.', 'wporg-plugins' ), 471 esc_html( $this->plugin['Name'] ) 472 ); 473 } 474 424 475 // Send plugin author an email for peace of mind. 425 476 $this->send_email_notification(); 426 427 do_action( 'plugin_upload', $this->plugin, $plugin_post );428 477 429 478 $message = sprintf( … … 451 500 // Success! 452 501 return $message; 502 } 503 504 /** 505 * Generate a plugin slug from a Plugin name. 506 * 507 * @param string $plugin_name The plugin name. 508 * @return string The generated plugin slug. 509 */ 510 public function generate_plugin_slug( $plugin_name ) { 511 $plugin_slug = remove_accents( $plugin_name ); 512 $plugin_slug = preg_replace( '/[^a-z0-9 _.-]/i', '', $plugin_slug ); 513 $plugin_slug = str_replace( '_', '-', $plugin_slug ); 514 $plugin_slug = sanitize_title_with_dashes( $plugin_slug ); 515 516 return $plugin_slug; 453 517 } 454 518 … … 505 569 * @return string|false The trademarked slug if found, false otherwise. 506 570 */ 507 public function has_trademarked_slug() { 571 public function has_trademarked_slug( $plugin_slug = false ) { 572 $plugin_slug = $plugin_slug ?: $this->plugin_slug; 573 508 574 $trademarked_slugs = array( 509 575 'adobe-', … … 647 713 if ( '-' === $trademark[-1] ) { 648 714 // Trademarks ending in "-" indicate slug cannot begin with that term. 649 if ( 0 === strpos( $ this->plugin_slug, $trademark ) ) {715 if ( 0 === strpos( $plugin_slug, $trademark ) ) { 650 716 $has_trademarked_slug = $trademark; 651 717 break; 652 718 } 653 } elseif ( false !== strpos( $ this->plugin_slug, $trademark ) ) {719 } elseif ( false !== strpos( $plugin_slug, $trademark ) ) { 654 720 // Otherwise, the term cannot appear anywhere in slug. 655 721 $has_trademarked_slug = $trademark; … … 662 728 $for_trademark = '-for-' . $has_trademarked_slug; 663 729 // At this point we might be okay, but there's one more check. 664 if ( $for_trademark === substr( $ this->plugin_slug, -1 * strlen( $for_trademark ) ) ) {730 if ( $for_trademark === substr( $plugin_slug, -1 * strlen( $for_trademark ) ) ) { 665 731 // Yes the slug ENDS with 'for-TRADEMARK'. 666 732 $has_trademarked_slug = false; … … 670 736 // Check portmanteaus. 671 737 foreach ( $portmanteaus as $portmanteau ) { 672 if ( 0 === strpos( $ this->plugin_slug, $portmanteau ) ) {738 if ( 0 === strpos( $plugin_slug, $portmanteau ) ) { 673 739 $has_trademarked_slug = $portmanteau; 674 740 break; … … 720 786 * 721 787 * @param int $post_id Post ID. 722 * @return int| \WP_Error Attachment ID or upload error.788 * @return int|WP_Error Attachment ID or upload error. 723 789 */ 724 790 public function save_zip_file( $post_id ) { 791 $zip_hash = sha1_file( $_FILES['zip_file']['tmp_name'] ); 792 if ( in_array( $zip_hash, get_post_meta( $post_id, 'uploaded_zip_hash' ) ?: [], true ) ) { 793 return new WP_Error( 'already_uploaded', __( "You've already uploaded that ZIP file.", 'wporg-plugins' ) ); 794 } 725 795 726 796 // Upload folders are already year/month based. A second-based prefix should be specific enough. 797 $original_name = $_FILES['zip_file']['name']; 727 798 $_FILES['zip_file']['name'] = date( 'd_H-i-s' ) . '_' . $_FILES['zip_file']['name']; 728 799 … … 730 801 add_filter( 'default_site_option_upload_filetypes', array( $this, 'whitelist_zip_files' ) ); 731 802 732 $attachment_id = media_handle_upload( 'zip_file', $post_id ); 803 // Store the plugin details against the media as well. 804 $post_details = array( 805 'post_title' => sprintf( '%s Version %s', $this->plugin['Name'], $this->plugin['Version'] ), 806 'post_excerpt' => $this->plugin['Description'], 807 ); 808 $attachment_id = media_handle_upload( 'zip_file', $post_id, $post_details ); 733 809 734 810 remove_filter( 'site_option_upload_filetypes', array( $this, 'whitelist_zip_files' ) ); 735 811 remove_filter( 'default_site_option_upload_filetypes', array( $this, 'whitelist_zip_files' ) ); 812 813 if ( ! is_wp_error( $attachment_id ) ) { 814 // Save some basic details with the ZIP. 815 update_post_meta( $attachment_id, 'version', $this->plugin['Version'] ); 816 update_post_meta( $attachment_id, 'submitted_name', $original_name ); 817 818 // And record this ZIP as having been uploaded. 819 add_post_meta( $post_id, 'uploaded_zip_hash', $zip_hash ); 820 } 736 821 737 822 return $attachment_id;
Note: See TracChangeset
for help on using the changeset viewer.