Changeset 3511 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php
- Timestamp:
- 06/20/2016 05:34:18 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php
r3506 r3511 12 12 13 13 /** 14 * Local cache for translated content injected into meta 14 * Local cache for translated content injected into meta. 15 * 16 * @access private 17 * 18 * @var array 15 19 */ 16 20 private $i18n_meta = array(); … … 18 22 /** 19 23 * Fetch the instance of the Plugin_Directory class. 24 * 25 * @static 20 26 */ 21 27 public static function instance() { … … 25 31 } 26 32 33 /** 34 * Plugin_Directory constructor. 35 * 36 * @access private 37 */ 27 38 private function __construct() { 28 39 add_action( 'init', array( $this, 'init' ) ); … … 44 55 add_filter( 'oembed_providers', array( $this, 'oembed_whitelist' ) ); 45 56 46 // Shim in postmeta support for data which doesn't yet live in postmeta 57 // Shim in postmeta support for data which doesn't yet live in postmeta. 47 58 add_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ), 10, 3 ); 48 59 49 60 add_filter( 'map_meta_cap', array( __NAMESPACE__ . '\Capabilities', 'map_meta_cap' ), 10, 4 ); 50 61 51 // Load the API routes 62 // Load the API routes. 52 63 add_action( 'rest_api_init', array( __NAMESPACE__ . '\API\Base', 'load_routes' ) ); 53 64 54 // Load all Admin-specific items. 55 // Cannot be included on `admin_init` to allow access to menu hooks 65 /* 66 * Load all Admin-specific items. 67 * Cannot be included on `admin_init` to allow access to menu hooks. 68 */ 56 69 if ( defined( 'WP_ADMIN' ) && WP_ADMIN ) { 57 70 Customizations::instance(); 58 71 59 add_action( 'wp_insert_post_data', array( __NAMESPACE__ . '\Admin\Status_Transitions', 'can_change_post_status' ), 10, 2 );72 add_action( 'wp_insert_post_data', array( __NAMESPACE__ . '\Admin\Status_Transitions', 'can_change_post_status' ), 10, 2 ); 60 73 add_action( 'transition_post_status', array( __NAMESPACE__ . '\Admin\Status_Transitions', 'instance' ) ); 61 74 } … … 74 87 75 88 register_post_type( 'plugin', array( 76 'labels' 89 'labels' => array( 77 90 'name' => __( 'Plugins', 'wporg-plugins' ), 78 91 'singular_name' => __( 'Plugin', 'wporg-plugins' ), … … 105 118 'delete_posts' => 'do_not_allow', 106 119 'create_posts' => 'do_not_allow', 107 ) 120 ), 108 121 ) ); 109 122 … … 148 161 'capabilities' => array( 149 162 'assign_terms' => 'plugin_set_category', 150 ) 163 ), 151 164 ) ); 152 165 … … 156 169 'rewrite' => false, 157 170 'labels' => array( 158 'name' 171 'name' => __( 'Built For', 'wporg-plugins' ), 159 172 ), 160 173 'public' => true, … … 164 177 'capabilities' => array( 165 178 'assign_terms' => 'plugin_set_category', 166 ) 179 ), 167 180 ) ); 168 181 … … 172 185 'rewrite' => false, 173 186 'labels' => array( 174 'name' 187 'name' => __( 'Business Model', 'wporg-plugins' ), 175 188 ), 176 189 'public' => true, … … 180 193 'capabilities' => array( 181 194 'assign_terms' => 'plugin_set_category', 182 ) 195 ), 183 196 ) ); 184 197 … … 220 233 // /browse/ should be the popular archive view. 221 234 add_rewrite_rule( '^browse$', 'index.php?browse=popular', 'top' ); 235 222 236 // Create an archive for a users favorites too. 223 237 add_rewrite_rule( '^browse/favorites/([^/]+)$', 'index.php?browse=favorites&favorites_user=$matches[1]', 'top' ); 224 238 225 // Handle the old plugin tabs URLs 239 // Handle the old plugin tabs URLs. 226 240 add_rewrite_rule( '^([^/]+)/(installation|faq|screenshots|changelog|stats|developers|other_notes)$', 'index.php?redirect_plugin_tab=$matches[1]/#$matches[2]', 'top' ); 227 241 … … 229 243 //Capabilities::add_roles(); 230 244 231 // When this plugin is used in the context of a Rosetta site, handle it gracefully 245 // When this plugin is used in the context of a Rosetta site, handle it gracefully. 232 246 if ( 'wordpress.org' != $_SERVER['HTTP_HOST'] && defined( 'WPORG_PLUGIN_DIRECTORY_BLOGID' ) ) { 233 247 add_filter( 'option_home', array( $this, 'rosetta_network_localize_url' ) ); … … 236 250 237 251 if ( 'en_US' != get_locale() ) { 238 add_filter( 'get_term', array( __NAMESPACE__ . '\ i18n', 'translate_term' ) );252 add_filter( 'get_term', array( __NAMESPACE__ . '\I18n', 'translate_term' ) ); 239 253 add_filter( 'the_content', array( $this, 'translate_post_content' ), 1, 2 ); 240 254 add_filter( 'the_title', array( $this, 'translate_post_title' ), 1, 2 ); … … 250 264 // When Jetpack syncs, we want to add filters to inject additional metadata for Jetpack, so it syncs for ElasticSearch indexing. 251 265 add_action( 'shutdown', array( $this, 'append_meta_for_jetpack' ), 8 ); 252 253 266 } 254 267 … … 263 276 } 264 277 278 /** 279 * Register the Widgets used plugin detail pages. 280 */ 265 281 public function register_widgets() { 266 register_widget( __NAMESPACE__ . '\Widgets\Donate' );267 register_widget( __NAMESPACE__ . '\Widgets\Meta' );282 register_widget( __NAMESPACE__ . '\Widgets\Donate' ); 283 register_widget( __NAMESPACE__ . '\Widgets\Meta' ); 268 284 register_widget( __NAMESPACE__ . '\Widgets\Ratings' ); 269 285 register_widget( __NAMESPACE__ . '\Widgets\Support' ); … … 391 407 } 392 408 if ( 'plugin_built_for' == $term->taxonomy ) { 409 393 410 // Term slug = Post Slug = /%postname%/ 394 411 return trailingslashit( home_url( $term->slug ) ); … … 429 446 } 430 447 431 switch ( $wp_query-> query_vars['browse']) {448 switch ( $wp_query->get( 'browse' ) ) { 432 449 case 'favorites': 433 450 $favorites_user = wp_get_current_user(); 434 if ( ! empty( $wp_query->query_vars['favorites_user'] ) ) {451 if ( ! empty( $wp_query->query_vars['favorites_user'] ) ) { 435 452 $favorites_user = $wp_query->query_vars['favorites_user']; 436 } elseif ( ! empty( $_GET['favorites_user'] ) ) {453 } elseif ( ! empty( $_GET['favorites_user'] ) ) { 437 454 $favorites_user = $_GET['favorites_user']; 438 455 } … … 450 467 $wp_query->query_vars['p'] = -1; 451 468 } 452 453 469 break; 454 470 455 471 case 'new': 456 $wp_query->query_vars['orderby'] 472 $wp_query->query_vars['orderby'] = 'post_modified'; 457 473 break; 458 474 } 459 475 460 if ( isset( $wp_query->query['browse'] ) ) { 461 if ( 'beta' != $wp_query->query['browse'] && 'featured' != $wp_query->query['browse'] ) { 462 unset( $wp_query->query_vars['browse'] ); 463 464 add_filter( 'the_posts', function( $posts, $wp_query ) { 465 // Fix the queried object for the archive view 466 if ( ! $wp_query->queried_object && isset( $wp_query->query['browse'] ) ) { 467 $wp_query->query_vars['browse'] = $wp_query->query['browse']; 468 $wp_query->queried_object = get_term_by( 'slug', $wp_query->query['browse'], 'plugin_section' ); 469 } 470 return $posts; 471 }, 10, 2 ); 472 473 } 476 if ( isset( $wp_query->query['browse'] ) && 'beta' != $wp_query->query['browse'] && 'featured' != $wp_query->query['browse'] ) { 477 unset( $wp_query->query_vars['browse'] ); 478 479 add_filter( 'the_posts', function( $posts, $wp_query ) { 480 481 // Fix the queried object for the archive view. 482 if ( ! $wp_query->queried_object && isset( $wp_query->query['browse'] ) ) { 483 $wp_query->query_vars['browse'] = $wp_query->query['browse']; 484 $wp_query->queried_object = get_term_by( 'slug', $wp_query->query['browse'], 'plugin_section' ); 485 } 486 487 return $posts; 488 }, 10, 2 ); 474 489 } 475 490 … … 485 500 * @param string $content Post content. 486 501 * @param string $section Optional. Which readme section to translate. 502 * @param int $post_id Optional. Post ID. Default: 0. 487 503 * @return string 488 504 */ 489 public function translate_post_content( $content, $section = null, $post_id = null) {505 public function translate_post_content( $content, $section = null, $post_id = 0 ) { 490 506 if ( is_null( $section ) ) { 491 507 return $content; … … 514 530 * Returns the requested page's excerpt, translated. 515 531 * 516 * @param string $excerpt517 * @param \WP_Post $post532 * @param string $excerpt 533 * @param int|\WP_Post $post 518 534 * @return string 519 535 */ … … 523 539 524 540 /** 525 * Shutdown action that will add a filter to inject additional postmeta containing translated content if Jetpack is syncing.526 * 541 * Shutdown action that will add a filter to inject additional postmeta containing translated content if Jetpack 542 * is syncing. 527 543 */ 528 544 public function append_meta_for_jetpack() { 529 // Guess if a Jetpack sync is scheduled to run. It runs during shutdown at a lower priority than this action, so we can get in first. 530 // Fetching the extra meta to inject is expensive, so we only want to do this if a sync is likely. 531 if ( class_exists( 'Jetpack' ) && !empty(\Jetpack::$instance->sync->sync) ) { 545 546 /* 547 * Guess if a Jetpack sync is scheduled to run. It runs during shutdown at a lower priority than this action, 548 * so we can get in first.Fetching the extra meta to inject is expensive, so we only want to do this if a sync 549 * is likely. 550 */ 551 if ( class_exists( 'Jetpack' ) && ! empty( \Jetpack::$instance->sync->sync ) ) { 532 552 add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 ); 533 553 } 534 535 554 } 536 555 … … 538 557 * Filter for wporg_plugins_custom_meta_fields to inject translated content for ES. 539 558 * 559 * @global string $locale Current locale. 560 * 540 561 * @param array $meta 541 * @param int $post_id562 * @param int $post_id 542 563 * @return array 543 564 */ 544 565 public function filter_post_meta_i18n( $meta, $post_id ) { 545 // Prevent recursion and repeat runs 566 567 // Prevent recursion and repeat runs. 546 568 remove_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ) ); 547 569 548 570 if ( $post_id <= 200 ) { 549 $locales_to_sync = array( 'fr_FR', 'es_ES' ); // This should probably be a list of available translations for the plugin readme. 571 572 // This should probably be a list of available translations for the plugin readme. 573 $locales_to_sync = array( 574 'fr_FR', 575 'es_ES', 576 ); 550 577 551 578 global $locale; 552 579 $_locale = $locale; 580 553 581 foreach ( $locales_to_sync as $locale ) { 554 $this->i18n_meta[ $post_id]['title_'.$locale]= $this->translate_post_title( get_the_title( $post_id ), $post_id );555 $this->i18n_meta[ $post_id]['excerpt_'.$locale] = $this->translate_post_excerpt( get_the_excerpt( $post_id ), $post_id );556 557 // Split up the content to translate it in sections 558 $content = '';582 $this->i18n_meta[ $post_id ][ 'title_' . $locale ] = $this->translate_post_title( get_the_title( $post_id ), $post_id ); 583 $this->i18n_meta[ $post_id ][ 'excerpt_' . $locale ] = $this->translate_post_excerpt( get_the_excerpt( $post_id ), $post_id ); 584 585 // Split up the content to translate it in sections. 586 $content = ''; 559 587 $sections = $this->split_post_content_into_pages( get_the_content( $post_id ) ); 560 588 foreach ( $sections as $section => $section_content ) { 561 589 $content .= $this->translate_post_content( $section_content, $section, $post_id ); 562 590 } 563 $this->i18n_meta[$post_id]['content_'.$locale] = $content; 564 591 $this->i18n_meta[ $post_id ][ 'content_' . $locale ] = $content; 565 592 } 566 593 567 594 $locale = $_locale; 568 595 569 $meta = array_merge( $meta, array_keys( $this->i18n_meta[$post_id] ) ); 570 } 571 572 add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n'), 10, 2 ); 596 $meta = array_merge( $meta, array_keys( $this->i18n_meta[ $post_id ] ) ); 597 } 598 599 add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 ); 600 573 601 return $meta; 574 602 } 575 576 603 577 604 /** … … 583 610 public function filter_allowed_post_types( $allowed_post_types ) { 584 611 $allowed_post_types[] = 'plugin'; 612 585 613 return $allowed_post_types; 586 614 } … … 588 616 /** 589 617 * Filters the available public query vars to add our custom parameters. 618 * 619 * @param array $vars Public query vars. 620 * @return array 590 621 */ 591 622 public function filter_query_vars( $vars ) { 592 623 $vars[] = 'favorites_user'; 593 624 $vars[] = 'redirect_plugin_tab'; 625 594 626 return $vars; 595 627 } … … 602 634 */ 603 635 public function filter_jetpack_options( $new_value ) { 604 if ( is_array( $new_value ) && array_key_exists( 'public', $new_value ) ) 636 if ( is_array( $new_value ) && array_key_exists( 'public', $new_value ) ) { 605 637 $new_value['public'] = 1; 638 } 606 639 607 640 return $new_value; … … 621 654 */ 622 655 function redirect_old_plugin_urls() { 623 // Handle a redirect for /$plugin/$tab_name/ to /$plugin/#$tab_name 656 657 // Handle a redirect for /$plugin/$tab_name/ to /$plugin/#$tab_name. 624 658 if ( get_query_var( 'redirect_plugin_tab' ) ) { 625 659 wp_safe_redirect( site_url( get_query_var( 'redirect_plugin_tab' ) ) ); … … 630 664 if ( is_404() ) { 631 665 632 // [1] => plugins [2] => example-plugin-name [2..] => random() 666 // [1] => plugins [2] => example-plugin-name [2..] => random(). 633 667 $path = explode( '/', $_SERVER['REQUEST_URI'] ); 634 668 … … 646 680 } 647 681 648 // The about page is now over at /developers/ 682 // The about page is now over at /developers/. 649 683 if ( 'about' === $path[2] ) { 650 wp_safe_redirect( home_url( '/developers/' . ( ( isset( $path[3] ) && 'add' == $path[3] ) ? 'add/' : '' ) ) );684 wp_safe_redirect( home_url( '/developers/' . ( ( isset( $path[3] ) && 'add' == $path[3] ) ? 'add/' : '' ) ) ); 651 685 die(); 652 686 } 653 687 654 // Otherwise, handle a plugin redirect 688 // Otherwise, handle a plugin redirect. 655 689 if ( $plugin = self::get_plugin_post( $path[2] ) ) { 656 690 $is_disabled = in_array( $plugin->post_status, array( 'disabled', 'closed' ), true ); 657 691 658 692 if ( $is_disabled && current_user_can( 'edit_post', $plugin ) ) { 659 wp_safe_redirect( add_query_arg( array( 'post' => $plugin->ID, 'action' => 'edit' ), admin_url( 'post.php' ) ) ); 693 wp_safe_redirect( add_query_arg( array( 694 'post' => $plugin->ID, 695 'action' => 'edit', 696 ), admin_url( 'post.php' ) ) ); 660 697 die(); 661 698 } else if ( ! $is_disabled ) { … … 672 709 } 673 710 674 // new-style Search links 711 // new-style Search links. 675 712 if ( get_query_var( 's' ) && isset( $_GET['s'] ) ) { 676 713 wp_safe_redirect( site_url( '/search/' . urlencode( get_query_var( 's' ) ) . '/' ) ); … … 706 743 } 707 744 } 745 708 746 return false; 709 747 } ); … … 722 760 switch ( $meta_key ) { 723 761 case 'downloads': 724 $post = get_post( $object_id );762 $post = get_post( $object_id ); 725 763 $count = Template::get_downloads_count( $post ); 726 727 return array( $count ); 764 $value = array( $count ); 728 765 break; 766 729 767 case 'rating': 730 768 $post = get_post( $object_id ); … … 733 771 break; 734 772 } 773 735 774 $rating = wporg_get_rating_avg( 'plugin', $post->post_name ); 736 737 return array( $rating ); 775 $value = array( $rating ); 738 776 break; 777 739 778 case 'ratings': 740 779 $post = get_post( $object_id ); … … 742 781 break; 743 782 } 783 744 784 $ratings = wporg_get_rating_counts( 'plugin', $post->post_name ); 745 746 return array( $ratings ); 785 $value = array( $ratings ); 747 786 break; 787 748 788 case false: 789 749 790 // In the event $meta_key is false, the caller wants all meta fields, so we'll append our custom ones here too. 750 791 remove_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ) ); … … 760 801 761 802 foreach ( $custom_meta_fields as $key ) { 762 // When WordPress calls `get_post_meta( $post_id, false )` it expects an array of maybe_serialize()'d data 803 804 // When WordPress calls `get_post_meta( $post_id, false )` it expects an array of maybe_serialize()'d data. 763 805 $shimed_data = $this->filter_shim_postmeta( false, $object_id, $key ); 764 806 if ( $shimed_data ) { … … 766 808 } 767 809 } 768 769 810 break; 811 770 812 default: 771 813 if ( isset( $this->i18n_meta[ $object_id ][ $meta_key ] ) ) { … … 774 816 break; 775 817 } 818 776 819 return $value; 777 820 } … … 823 866 * Retrieve the WP_Post object representing a given plugin. 824 867 * 825 * @param $plugin_slug string|\WP_Post The slug of the plugin to retrieve. 868 * @static 869 * @global \WP_Post $post WordPress post object. 870 * 871 * @param string|\WP_Post $plugin_slug The slug of the plugin to retrieve. 826 872 * @return \WP_Post|bool 827 873 */ 828 static public function get_plugin_post( $plugin_slug ) {874 public static function get_plugin_post( $plugin_slug ) { 829 875 global $post; 876 830 877 if ( $plugin_slug instanceof \WP_Post ) { 831 878 return $plugin_slug; 832 879 } 880 833 881 // Use the global $post object when it matches to avoid hitting the database. 834 if ( ! empty( $post ) && 'plugin' == $post->post_type && $plugin_slug == $post->post_name ) {882 if ( ! empty( $post ) && 'plugin' == $post->post_type && $plugin_slug == $post->post_name ) { 835 883 return $post; 836 884 } … … 841 889 // We have a $post. 842 890 } else { 891 843 892 // get_post_by_slug(); 844 893 $posts = get_posts( array( … … 847 896 'post_status' => array( 'publish', 'pending', 'disabled', 'closed', 'draft', 'approved' ), 848 897 ) ); 898 849 899 if ( ! $posts ) { 850 900 $post = false; … … 861 911 /** 862 912 * Create a new post entry for a given plugin slug. 913 * 914 * @static 863 915 * 864 916 * @param array $args {
Note: See TracChangeset
for help on using the changeset viewer.