Making WordPress.org

Changeset 1416


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

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

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
2 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}
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/functions.php

    r1409 r1416  
    129129
    130130/**
    131  * Bootstraps found themes for the frontend JS handler.
    132  *
    133  * @return array
    134  */
    135 function wporg_themes_prepare_themes_for_js() {
    136         global $wp_query;
    137 
    138         include_once API_WPORGPATH . 'themes/info/1.0/class-themes-api.php';
    139         $api = new Themes_API( 'get_result' );
    140         $api->fields = array_merge( $api->fields, array(
    141                 'description'  => true,
    142                 'sections'     => false,
    143                 'tested'       => true,
    144                 'requires'     => true,
    145                 'rating'       => true,
    146                 'ratings'      => true,
    147                 'downloaded'   => true,
    148                 'downloadlink' => true,
    149                 'last_updated' => true,
    150                 'homepage'     => true,
    151                 'tags'         => true,
    152                 'num_ratings'  => true,
    153                 'parent'       => true,
    154                 'theme_url'    => true,
    155         ) );
    156 
    157         $themes = array_map( array( $api, 'fill_theme' ), $wp_query->posts );
    158         $themes = array_map( 'wporg_themes_ajax_prepare_theme', $themes );
    159 
    160         $request = array();
    161         if ( get_query_var( 'browse' ) ) {
    162                 $request['browse'] = get_query_var( 'browse' );
    163         } else if ( $wp_query->is_tag() ) {
    164                 $request['tag'] = (array) explode( '+', get_query_var( 'tag' ) );
    165         }
    166         else if ( $wp_query->is_search() ) {
    167                 $request['search'] = get_query_var( 's' );
    168         }
    169         else if ( $wp_query->is_author() ) {
    170                 $request['author'] = get_user_by( 'id', get_query_var( 'author' ) )->user_nicename;
    171         }
    172         else if ( $wp_query->is_singular( 'repopackage' ) ) {
    173                 $request['theme'] = get_query_var( 'name' );
    174         }
    175 
    176         return array(
    177                 'themes'  => $themes,
    178                 'request' => $request,
    179                 'total'   => $wp_query->found_posts,
    180         );
    181  }
    182 
    183 /**
    184  * @param  object $args
    185  * @param  string $action
    186  *
    187  * @return array
    188  */
    189 function wporg_themes_api_args( $args, $action ) {
    190         if ( in_array( $action, array( 'query_themes', 'theme_information' ) ) ) {
    191                 $args->per_page = 30;
    192                 $args->fields['parent']    = true;
    193                 $args->fields['ratings']   = true;
    194                 $args->fields['tags']      = true;
    195                 $args->fields['theme_url'] = true;
    196         }
    197 
    198         return $args;
    199 }
    200 add_filter( 'themes_api_args', 'wporg_themes_api_args', 10, 2 );
    201 
    202 /**
    203  * Removes Core's built-in query-themes handler, so we can safely add ours later on.
    204  */
    205 function wporg_themes_remove_ajax_action() {
    206         remove_action( 'wp_ajax_query-themes', 'wp_ajax_query_themes', 1 );
    207 }
    208 add_action( 'wp_ajax_query-themes', 'wporg_themes_remove_ajax_action', -1 );
    209 
    210 /**
    211  * A recreation of Core's implementation without capability check, since there is nothing to install.
    212  */
    213 function wporg_themes_query_themes() {
    214         $request = wp_unslash( $_REQUEST['request'] );
    215         $request['fields'] = wp_parse_args( $request['fields'], array(
    216                 'description'  => true,
    217                 'sections'     => false,
    218                 'tested'       => true,
    219                 'requires'     => true,
    220                 'rating'       => true,
    221                 'ratings'      => true,
    222                 'downloaded'   => true,
    223                 'downloadlink' => true,
    224                 'last_updated' => true,
    225                 'homepage'     => true,
    226                 'tags'         => true,
    227                 'num_ratings'  => true,
    228                 'parent'       => true,
    229                 'theme_url'    => true,
    230         ) );
    231         $args = wp_parse_args( $request, array(
    232                 'per_page' => 20,
    233         ) );
    234 
    235         include_once API_WPORGPATH . 'themes/info/1.0/class-themes-api.php';
    236         $api = new Themes_API( 'query_themes', $args );
    237         $api = $api->response;
    238 
    239         if ( is_wp_error( $api ) ) {
    240                 wp_send_json_error();
    241         }
    242 
    243         foreach ( $api->themes as $key => $theme ) {
    244                 $api->themes[ $key ] = wporg_themes_ajax_prepare_theme( $theme );
    245         }
    246 
    247         wp_send_json_success( $api );
    248 }
    249 add_action( 'wp_ajax_query-themes',        'wporg_themes_query_themes' );
    250 add_action( 'wp_ajax_nopriv_query-themes', 'wporg_themes_query_themes' );
    251 
    252 function wporg_themes_theme_info() {
    253         $request = wp_unslash( $_REQUEST['request'] );
    254         $request['fields'] = wp_parse_args( $request['fields'], array(
    255                 'description'  => true,
    256                 'sections'     => false,
    257                 'tested'       => true,
    258                 'requires'     => true,
    259                 'rating'       => true,
    260                 'ratings'      => true,
    261                 'downloaded'   => true,
    262                 'downloadlink' => true,
    263                 'last_updated' => true,
    264                 'homepage'     => true,
    265                 'tags'         => true,
    266                 'num_ratings'  => true,
    267                 'parent'       => true,
    268                 'theme_url'    => true,
    269         ) );
    270 
    271         include_once API_WPORGPATH . 'themes/info/1.0/class-themes-api.php';
    272         $api = new Themes_API( 'theme_information', $request );
    273         $api = $api->response;
    274 
    275         if ( empty( $api ) ) {
    276                 wp_send_json_error();
    277         }
    278 
    279         $api = wporg_themes_ajax_prepare_theme( $api );
    280 
    281         wp_send_json_success( $api );
    282 }
    283 add_action( 'wp_ajax_theme-info',        'wporg_themes_theme_info' );
    284 add_action( 'wp_ajax_nopriv_theme-info', 'wporg_themes_theme_info' );
    285 
    286 function wporg_themes_ajax_prepare_theme( $theme ) {
    287         global $themes_allowedtags;
    288         if ( empty( $themes_allowedtags ) ) {
    289                 $themes_allowedtags = array(
    290                         'a'       => array( 'href' => array(), 'title' => array(), 'target' => array() ),
    291                         'abbr'    => array( 'title' => array() ),
    292                         'acronym' => array( 'title' => array() ),
    293                         'code'    => array(),
    294                         'pre'     => array(),
    295                         'em'      => array(),
    296                         'strong'  => array(),
    297                         'div'     => array(),
    298                         'p'       => array(),
    299                         'ul'      => array(),
    300                         'ol'      => array(),
    301                         'li'      => array(),
    302                         'h1'      => array(),
    303                         'h2'      => array(),
    304                         'h3'      => array(),
    305                         'h4'      => array(),
    306                         'h5'      => array(),
    307                         'h6'      => array(),
    308                         'img'     => array( 'src' => array(), 'class' => array(), 'alt' => array() ),
    309                 );
    310         }
    311 
    312         $author        = get_user_by( 'slug', $theme->author );
    313         $theme->author = new StdClass;
    314         foreach ( array( 'user_nicename', 'display_name' ) as $property ) {
    315                 $theme->author->$property = get_the_author_meta( $property, $author->ID );
    316         }
    317 
    318         $theme->name        = wp_kses( $theme->name, $themes_allowedtags );
    319         $theme->version     = wp_kses( $theme->version, $themes_allowedtags );
    320         $theme->description = wp_kses( $theme->description, $themes_allowedtags );
    321         $theme->downloaded  = number_format_i18n( $theme->downloaded );
    322         $theme->rating_text = sprintf( _n( '(based on %s rating)', '(based on %s ratings)', $theme->num_ratings ), number_format_i18n( $theme->num_ratings ) );
    323         $theme->num_ratings = number_format_i18n( $theme->num_ratings );
    324         $theme->preview_url = set_url_scheme( $theme->preview_url );
    325 
    326         if ( preg_match( '/screenshot.(jpg|jpeg|png|gif)/', $theme->screenshot_url, $match ) ) {
    327                 $theme->screenshot_url = sprintf( 'https://i0.wp.com/themes.svn.wordpress.org/%1$s/%2$s/%3$s',
    328                         $theme->slug,
    329                         $theme->version,
    330                         $match[0]
    331                 );
    332         }
    333 
    334         return $theme;
    335 }
    336 
    337 /**
    338131 * Include view templates in the footer.
    339132 */
Note: See TracChangeset for help on using the changeset viewer.