Making WordPress.org


Ignore:
Timestamp:
11/21/2014 10:04:40 AM (9 years ago)
Author:
obenland
Message:

WP.org Themes: Bring theme installer goodies to the front-end.

This brings most of the Backbone and Themes API-based functionality
from the admin's theme install screen to the front-end.

There are of course still loads of things to do, but it works
decently enough to share it with the world and give an idea of where
this is headed.

See https://make.wordpress.org/meta/2014/11/20/theme-repository-theme/
See #745.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-themes/functions.php

    r887 r1001  
    11<?php
     2/**
     3 * WP.org Themes' functions and definitions.
     4 *
     5 * @package wporg-themes
     6 */
     7
     8/**
     9 * Sets up theme defaults and registers support for various WordPress features.
     10 *
     11 * Note that this function is hooked into the after_setup_theme hook, which
     12 * runs before the init hook. The init hook is too late for some features, such
     13 * as indicating support for post thumbnails.
     14 */
     15
    216function wporg_themes_setup() {
    317//  load_theme_textdomain( 'wporg-themes', get_template_directory() . '/languages' );
     
    1529add_action( 'after_setup_theme', 'wporg_themes_setup' );
    1630
    17 function wporg_themes_style() {
    18     //<link rel="stylesheet" href="http://localhost/repotest/wp-admin/css/themes.css" />
     31/**
     32 * Enqueue scripts and styles.
     33 */
     34function wporg_themes_scripts() {
     35
     36    wp_enqueue_style( 'global-style', '//s.w.org/style/wp4.css', array(), '14' );
     37    wp_enqueue_style( 'themes-style', self_admin_url( 'css/themes.css' ) );
    1938    wp_enqueue_style( 'wporg-themes-style', get_stylesheet_uri() );
     39
     40    wp_enqueue_script( 'theme', self_admin_url( 'js/theme.js' ), array( 'wp-backbone' ), false, 1 );
     41    wp_enqueue_script( 'wporg-theme', get_template_directory_uri() . '/js/theme.js', array( 'theme' ), false, 1 );
     42
     43    wp_localize_script( 'theme', '_wpThemeSettings', array(
     44        'themes'   => false,
     45        'settings' => array(
     46            'isInstall'  => true,
     47            'canInstall' => false,
     48            'installURI' => null,
     49            'adminUrl'   => '',
     50        ),
     51        'l10n' => array(
     52            'addNew'            => __( 'Add New Theme' ),
     53            'search'            => __( 'Search Themes' ),
     54            'searchPlaceholder' => __( 'Search themes...' ), // placeholder (no ellipsis)
     55            'upload'            => __( 'Upload Theme' ),
     56            'back'              => __( 'Back' ),
     57            'error'             => __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' )
     58        ),
     59        'installedThemes' => array(),
     60    ) );
    2061}
    21 add_action( 'wp_enqueue_scripts', 'wporg_themes_style' );
     62add_action( 'wp_enqueue_scripts', 'wporg_themes_scripts' );
    2263
    23 // force the post type to the repopackages
    24 // TODO smarter
    25 function wporg_themes_pregetposts( &$q ) {
    26     $q->set('post_type', 'repopackage');
     64/**
     65 * Removes Core's built-in query-themes handler, so we can safely add ours later on.
     66 */
     67function wporg_themes_remove_ajax_action() {
     68    remove_action( 'wp_ajax_query-themes', 'wp_ajax_query_themes', 1 );
    2769}
    28 add_action( 'pre_get_posts', 'wporg_themes_pregetposts' );
     70add_action( 'wp_ajax_query-themes', 'wporg_themes_remove_ajax_action', -1 );
     71
     72/**
     73 * A recreation of Core's implementation without capability check, since there is nothing to install.
     74 */
     75function wporg_themes_query_themes() {
     76    global $themes_allowedtags, $theme_field_defaults;
     77
     78    $args = wp_parse_args( wp_unslash( $_REQUEST['request'] ), array(
     79        'per_page' => 20,
     80        'fields'   => $theme_field_defaults,
     81    ) );
     82
     83    $old_filter = isset( $args['browse'] ) ? $args['browse'] : 'search';
     84
     85    /** This filter is documented in wp-admin/includes/class-wp-theme-install-list-table.php */
     86    $args = apply_filters( 'install_themes_table_api_args_' . $old_filter, $args );
     87
     88    $api = themes_api( 'query_themes', $args );
     89
     90    if ( is_wp_error( $api ) ) {
     91        wp_send_json_error();
     92    }
     93
     94    foreach ( $api->themes as &$theme ) {
     95        $theme->name        = wp_kses( $theme->name,        $themes_allowedtags );
     96        $theme->author      = wp_kses( $theme->author,      $themes_allowedtags );
     97        $theme->version     = wp_kses( $theme->version,     $themes_allowedtags );
     98        $theme->description = wp_kses( $theme->description, $themes_allowedtags );
     99        $theme->num_ratings = sprintf( _n( '(based on %s rating)', '(based on %s ratings)', $theme->num_ratings ), number_format_i18n( $theme->num_ratings ) );
     100        $theme->preview_url = set_url_scheme( $theme->preview_url );
     101    }
     102
     103    wp_send_json_success( $api );
     104}
     105add_action( 'wp_ajax_query-themes',        'wporg_themes_query_themes' );
     106add_action( 'wp_ajax_nopriv_query-themes', 'wporg_themes_query_themes' );
     107
     108/**
     109 * Include view templates in the footer.
     110 */
     111function wporg_themes_view_templates() {
     112    get_template_part( 'view-templates/theme' );
     113    get_template_part( 'view-templates/theme-preview' );
     114    get_template_part( 'view-templates/theme-single' );
     115}
     116add_action( 'wp_footer', 'wporg_themes_view_templates' );
Note: See TracChangeset for help on using the changeset viewer.