Making WordPress.org


Ignore:
Timestamp:
03/19/2015 07:14:42 AM (10 years ago)
Author:
dd32
Message:

Theme Directory: Move some functions from the themes functions.php to the plugin instead.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/theme-directory.php

    r1403 r1416  
    3232 */
    3333function wporg_themes_activate() {
     34    wporg_themes_init();
    3435    flush_rewrite_rules();
    3536
     
    99100            ),
    100101            'supports'            => array( 'title', 'editor', 'author', 'custom-fields' ),
    101             'public'              => true,
     102            'public'              => false,
    102103            'show_in_nav_menus'   => false,
    103104            'exclude_from_search' => true,
     105            'rewrite'             => false,
    104106            'menu_icon'           => 'dashicons-businessman',
    105107        ) );
     
    600602    );
    601603}
     604
     605
     606/**
     607 * Bootstraps found themes for the frontend JS handler.
     608 *
     609 * @return array
     610 */
     611function wporg_themes_prepare_themes_for_js() {
     612    global $wp_query;
     613
     614    include_once API_WPORGPATH . 'themes/info/1.0/class-themes-api.php';
     615    $api = new Themes_API( 'get_result' );
     616    $api->fields = array_merge( $api->fields, array(
     617        'description'  => true,
     618        'sections'     => false,
     619        'tested'       => true,
     620        'requires'     => true,
     621        'rating'       => true,
     622        'ratings'      => true,
     623        'downloaded'   => true,
     624        'downloadlink' => true,
     625        'last_updated' => true,
     626        'homepage'     => true,
     627        'tags'         => true,
     628        'num_ratings'  => true,
     629        'parent'       => true,
     630        'theme_url'    => true,
     631    ) );
     632
     633    $themes = array_map( array( $api, 'fill_theme' ), $wp_query->posts );
     634    $themes = array_map( 'wporg_themes_ajax_prepare_theme', $themes );
     635
     636    $request = array();
     637    if ( get_query_var( 'browse' ) ) {
     638        $request['browse'] = get_query_var( 'browse' );
     639    } else if ( $wp_query->is_tag() ) {
     640        $request['tag'] = (array) explode( '+', get_query_var( 'tag' ) );
     641    }
     642    else if ( $wp_query->is_search() ) {
     643        $request['search'] = get_query_var( 's' );
     644    }
     645    else if ( $wp_query->is_author() ) {
     646        $request['author'] = get_user_by( 'id', get_query_var( 'author' ) )->user_nicename;
     647    }
     648    else if ( $wp_query->is_singular( 'repopackage' ) ) {
     649        $request['theme'] = get_query_var( 'name' );
     650    }
     651
     652    return array(
     653        'themes'  => $themes,
     654        'request' => $request,
     655        'total'   => $wp_query->found_posts,
     656    );
     657 }
     658
     659
     660/**
     661 * Removes Core's built-in query-themes handler, so we can safely add ours later on.
     662 */
     663function wporg_themes_remove_ajax_action() {
     664    remove_action( 'wp_ajax_query-themes', 'wp_ajax_query_themes', 1 );
     665}
     666add_action( 'wp_ajax_query-themes', 'wporg_themes_remove_ajax_action', -1 );
     667
     668/**
     669 * A recreation of Core's implementation without capability check, since there is nothing to install.
     670 */
     671function wporg_themes_query_themes() {
     672    $request = wp_unslash( $_REQUEST['request'] );
     673    $request['fields'] = wp_parse_args( $request['fields'], array(
     674        'description'  => true,
     675        'sections'     => false,
     676        'tested'       => true,
     677        'requires'     => true,
     678        'rating'       => true,
     679        'ratings'      => true,
     680        'downloaded'   => true,
     681        'downloadlink' => true,
     682        'last_updated' => true,
     683        'homepage'     => true,
     684        'tags'         => true,
     685        'num_ratings'  => true,
     686        'parent'       => true,
     687        'theme_url'    => true,
     688    ) );
     689    $args = wp_parse_args( $request, array(
     690        'per_page' => 20,
     691    ) );
     692
     693    include_once API_WPORGPATH . 'themes/info/1.0/class-themes-api.php';
     694    $api = new Themes_API( 'query_themes', $args );
     695    $api = $api->response;
     696
     697    if ( is_wp_error( $api ) ) {
     698        wp_send_json_error();
     699    }
     700
     701    foreach ( $api->themes as $key => $theme ) {
     702        $api->themes[ $key ] = wporg_themes_ajax_prepare_theme( $theme );
     703    }
     704
     705    wp_send_json_success( $api );
     706}
     707add_action( 'wp_ajax_query-themes',        'wporg_themes_query_themes' );
     708add_action( 'wp_ajax_nopriv_query-themes', 'wporg_themes_query_themes' );
     709
     710function wporg_themes_theme_info() {
     711    $request = wp_unslash( $_REQUEST['request'] );
     712    $request['fields'] = wp_parse_args( $request['fields'], array(
     713        'description'  => true,
     714        'sections'     => false,
     715        'tested'       => true,
     716        'requires'     => true,
     717        'rating'       => true,
     718        'ratings'      => true,
     719        'downloaded'   => true,
     720        'downloadlink' => true,
     721        'last_updated' => true,
     722        'homepage'     => true,
     723        'tags'         => true,
     724        'num_ratings'  => true,
     725        'parent'       => true,
     726        'theme_url'    => true,
     727    ) );
     728
     729    include_once API_WPORGPATH . 'themes/info/1.0/class-themes-api.php';
     730    $api = new Themes_API( 'theme_information', $request );
     731    $api = $api->response;
     732
     733    if ( empty( $api ) ) {
     734        wp_send_json_error();
     735    }
     736
     737    $api = wporg_themes_ajax_prepare_theme( $api );
     738
     739    wp_send_json_success( $api );
     740}
     741add_action( 'wp_ajax_theme-info',        'wporg_themes_theme_info' );
     742add_action( 'wp_ajax_nopriv_theme-info', 'wporg_themes_theme_info' );
     743
     744function wporg_themes_ajax_prepare_theme( $theme ) {
     745    global $themes_allowedtags;
     746    if ( empty( $themes_allowedtags ) ) {
     747        $themes_allowedtags = array(
     748            'a'       => array( 'href' => array(), 'title' => array(), 'target' => array() ),
     749            'abbr'    => array( 'title' => array() ),
     750            'acronym' => array( 'title' => array() ),
     751            'code'    => array(),
     752            'pre'     => array(),
     753            'em'      => array(),
     754            'strong'  => array(),
     755            'div'     => array(),
     756            'p'       => array(),
     757            'ul'      => array(),
     758            'ol'      => array(),
     759            'li'      => array(),
     760            'h1'      => array(),
     761            'h2'      => array(),
     762            'h3'      => array(),
     763            'h4'      => array(),
     764            'h5'      => array(),
     765            'h6'      => array(),
     766            'img'     => array( 'src' => array(), 'class' => array(), 'alt' => array() ),
     767        );
     768    }
     769
     770    $author        = get_user_by( 'slug', $theme->author );
     771    $theme->author = new StdClass;
     772    foreach ( array( 'user_nicename', 'display_name' ) as $property ) {
     773        $theme->author->$property = get_the_author_meta( $property, $author->ID );
     774    }
     775
     776    $theme->name        = wp_kses( $theme->name, $themes_allowedtags );
     777    $theme->version     = wp_kses( $theme->version, $themes_allowedtags );
     778    $theme->description = wp_kses( $theme->description, $themes_allowedtags );
     779    $theme->downloaded  = number_format_i18n( $theme->downloaded );
     780    $theme->rating_text = sprintf( _n( '(based on %s rating)', '(based on %s ratings)', $theme->num_ratings ), number_format_i18n( $theme->num_ratings ) );
     781    $theme->num_ratings = number_format_i18n( $theme->num_ratings );
     782    $theme->preview_url = set_url_scheme( $theme->preview_url );
     783
     784    if ( preg_match( '/screenshot.(jpg|jpeg|png|gif)/', $theme->screenshot_url, $match ) ) {
     785        $theme->screenshot_url = sprintf( 'https://i0.wp.com/themes.svn.wordpress.org/%1$s/%2$s/%3$s',
     786            $theme->slug,
     787            $theme->version,
     788            $match[0]
     789        );
     790    }
     791
     792    return $theme;
     793}
Note: See TracChangeset for help on using the changeset viewer.