WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/10/2017 08:03:01 PM (13 months ago)
Author:
azaozz
Message:

Dashboard: Update the existing WordPress News dashboard widget to also include upcoming meetup events and WordCamps near the current user’s location.

Props @afercia, @andreamiddleton, @azaozz, @camikaos, @coreymckrill, @chanthaboune, @courtneypk, @dd32, @iandunn, @iseulde, @mapk, @mayukojpn, @melchoyce, @nao, @obenland, @pento, @samuelsidler, @stephdau, @tellyworth.
See #40702.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/dashboard.php

    r40556 r40607  
    5353    }
    5454
    55     // WordPress News
    56     wp_add_dashboard_widget( 'dashboard_primary', __( 'WordPress News' ), 'wp_dashboard_primary' );
     55    // WordPress Events and News
     56    wp_add_dashboard_widget( 'dashboard_primary', __( 'WordPress Events and News' ), 'wp_dashboard_events_news' );
    5757
    5858    if ( is_network_admin() ) {
     
    128128    /** This action is documented in wp-admin/edit-form-advanced.php */
    129129    do_action( 'do_meta_boxes', $screen->id, 'side', '' );
     130}
     131
     132/**
     133 * Gets the community events data that needs to be passed to dashboard.js.
     134 *
     135 * @since 4.8.0
     136 *
     137 * @return array The script data.
     138 */
     139function wp_get_community_events_script_data() {
     140    require_once( ABSPATH . 'wp-admin/includes/class-wp-community-events.php' );
     141
     142    $user_id       = get_current_user_id();
     143    $user_location = get_user_option( 'community-events-location', $user_id );
     144    $events_client = new WP_Community_Events( $user_id, $user_location );
     145
     146    $script_data = array(
     147        'nonce' => wp_create_nonce( 'community_events' ),
     148        'cache' => $events_client->get_cached_events(),
     149
     150        'l10n' => array(
     151            'enter_closest_city' => __( 'Enter your closest city to find nearby events.' ),
     152            'error_occurred_please_try_again' => __( 'An error occured. Please try again.' ),
     153
     154            /*
     155             * These specific examples were chosen to highlight the fact that a
     156             * state is not needed, even for cities whose name is not unique.
     157             * It would be too cumbersome to include that in the instructions
     158             * to the user, so it's left as an implication.
     159             */
     160            /* translators: %s is the name of the city we couldn't locate. Replace the examples with cities in your locale, but test that they match the expected location before including them. Use endonyms (native locale names) whenever possible. */
     161            'could_not_locate_city' => __( "We couldn't locate %s. Please try another nearby city. For example: Kansas City; Springfield; Portland." ),
     162
     163            // This one is only used with wp.a11y.speak(), so it can/should be more brief.
     164            /* translators: %s is the name of a city. */
     165            'city_updated' => __( 'City updated. Listing events near %s.' ),
     166        )
     167    );
     168
     169    return $script_data;
    130170}
    131171
     
    10701110}
    10711111
     1112
     1113/**
     1114 * Renders the Events and News dashboard widget.
     1115 *
     1116 * @since 4.8.0
     1117 */
     1118function wp_dashboard_events_news() {
     1119    wp_print_community_events_markup();
     1120
     1121    ?>
     1122
     1123    <div class="wordpress-news hide-if-no-js">
     1124        <?php wp_dashboard_primary(); ?>
     1125    </div>
     1126
     1127    <p class="community-events-footer">
     1128        <a href="https://make.wordpress.org/community/meetups-landing-page" target="_blank">
     1129            <?php _e( 'Meetups' ); ?> <span class="dashicons dashicons-external"></span>
     1130        </a>
     1131
     1132        |
     1133
     1134        <a href="https://central.wordcamp.org/schedule/" target="_blank">
     1135            <?php _e( 'WordCamps' ); ?> <span class="dashicons dashicons-external"></span>
     1136        </a>
     1137
     1138        |
     1139
     1140        <?php // translators: If a Rosetta site exists (e.g. https://es.wordpress.org/news/), then use that. Otherwise, leave untranslated. ?>
     1141        <a href="<?php _e( 'https://wordpress.org/news/' ); ?>" target="_blank">
     1142            <?php _e( 'News' ); ?> <span class="dashicons dashicons-external"></span>
     1143        </a>
     1144    </p>
     1145
     1146    <?php
     1147}
     1148
     1149/**
     1150 * Prints the markup for the Community Events section of the Events and News Dashboard widget.
     1151 *
     1152 * @since 4.8.0
     1153 */
     1154function wp_print_community_events_markup() {
     1155    $script_data = wp_get_community_events_script_data();
     1156
     1157    ?>
     1158
     1159    <div class="community-events-errors notice notice-error inline hide-if-js">
     1160        <p class="hide-if-js">
     1161            <?php _e( 'This widget requires JavaScript.'); ?>
     1162        </p>
     1163
     1164        <p class="community-events-error-occurred" aria-hidden="true">
     1165            <?php echo $script_data['l10n']['error_occurred_please_try_again']; ?>
     1166        </p>
     1167
     1168        <p class="community-events-could-not-locate" aria-hidden="true"></p>
     1169    </div>
     1170
     1171    <div class="community-events-loading hide-if-no-js">
     1172        <?php _e( 'Loading&hellip;'); ?>
     1173    </div>
     1174
     1175    <?php
     1176    /*
     1177     * Hide the main element when the page first loads, because the content
     1178     * won't be ready until wp.communityEvents.renderEventsTemplate() has run.
     1179     */
     1180    ?>
     1181    <div id="community-events" class="community-events" aria-hidden="true">
     1182        <div class="activity-block">
     1183            <p>
     1184                <span id="community-events-location-message"></span>
     1185
     1186                <button class="button-link community-events-toggle-location" aria-label="<?php _e( 'Edit city'); ?>" aria-expanded="false">
     1187                    <span class="dashicons dashicons-edit"></span>
     1188                </button>
     1189            </p>
     1190
     1191            <form class="community-events-form" aria-hidden="true" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" method="post">
     1192                <label for="community-events-location">
     1193                    <?php _e( 'City:' ); ?>
     1194                </label>
     1195                <?php /* translators: Replace with the name of a city in your locale that shows events. Use only the city name itself, without any region or country. Use the endonym instead of the English name. */ ?>
     1196                <input id="community-events-location" class="regular-text" type="text" name="community-events-location" placeholder="<?php _e( 'Cincinnati' ); ?>" />
     1197
     1198                <?php submit_button( __( 'Submit' ), 'secondary', 'community-events-submit', false ); ?>
     1199
     1200                <button class="community-events-cancel button button-link" type="button" aria-expanded="false">
     1201                    <?php _e( 'Cancel' ); ?>
     1202                </button>
     1203
     1204                <span class="spinner"></span>
     1205            </form>
     1206        </div>
     1207
     1208        <ul class="community-events-results activity-block last"></ul>
     1209    </div>
     1210
     1211    <?php
     1212}
     1213
     1214/**
     1215 * Renders the events templates for the Event and News widget.
     1216 *
     1217 * @since 4.8.0
     1218 */
     1219function wp_print_community_events_templates() {
     1220    $script_data = wp_get_community_events_script_data();
     1221
     1222    ?>
     1223
     1224    <script id="tmpl-community-events-attend-event-near" type="text/template">
     1225        <?php printf(
     1226            /* translators: %s is a placeholder for the name of a city. */
     1227            __( 'Attend an upcoming event near %s.' ),
     1228            '<strong>{{ data.location }}</strong>'
     1229        ); ?>
     1230    </script>
     1231
     1232    <script id="tmpl-community-events-could-not-locate" type="text/template">
     1233        <?php printf(
     1234            $script_data['l10n']['could_not_locate_city'],
     1235            '<em>{{data.unknownCity}}</em>'
     1236        ); ?>
     1237    </script>
     1238
     1239    <script id="tmpl-community-events-event-list" type="text/template">
     1240        <# _.each( data.events, function( event ) { #>
     1241            <li class="event event-{{ event.type }} wp-clearfix">
     1242                <div class="event-info">
     1243                    <div class="dashicons event-icon" aria-hidden="true"></div>
     1244                    <div class="event-info-inner">
     1245                        <a class="event-title" href="{{ event.url }}">{{ event.title }}</a>
     1246                        <span class="event-city">{{ event.location.location }}</span>
     1247                    </div>
     1248                </div>
     1249
     1250                <div class="event-date-time">
     1251                    <span class="event-date">{{ event.formatted_date }}</span>
     1252                    <# if ( 'meetup' === event.type ) { #>
     1253                        <span class="event-time">{{ event.formatted_time }}</span>
     1254                    <# } #>
     1255                </div>
     1256            </li>
     1257        <# } ) #>
     1258    </script>
     1259
     1260    <script id="tmpl-community-events-no-upcoming-events" type="text/template">
     1261        <li class="event-none">
     1262            <?php printf(
     1263                /* translators: 1: the city the user searched for, 2: meetup organization documentation URL */
     1264                __( 'There aren&#8217;t any events scheduled near %1$s at the moment. Would you like to <a href="%2$s">organize one</a>?' ),
     1265                '{{data.location}}',
     1266                __( 'https://make.wordpress.org/community/handbook/meetup-organizer/welcome/' )
     1267            ); ?>
     1268        </li>
     1269    </script>
     1270
     1271    <?php
     1272}
     1273
    10721274/**
    10731275 * WordPress News dashboard widget.
    10741276 *
    10751277 * @since 2.7.0
     1278 * @since 4.8.0 Removed popular plugins feed.
    10761279 */
    10771280function wp_dashboard_primary() {
     
    11061309            'title'        => apply_filters( 'dashboard_primary_title', __( 'WordPress Blog' ) ),
    11071310            'items'        => 1,
    1108             'show_summary' => 1,
     1311            'show_summary' => 0,
    11091312            'show_author'  => 0,
    1110             'show_date'    => 1,
     1313            'show_date'    => 0,
    11111314        ),
    11121315        'planet' => array(
     
    11531356    );
    11541357
    1155     if ( ( ! wp_disallow_file_mods( 'dashboard_widget' ) ) && ( ! is_multisite() && is_blog_admin() && current_user_can( 'install_plugins' ) ) || ( is_network_admin() && current_user_can( 'manage_network_plugins' ) && current_user_can( 'install_plugins' ) ) ) {
    1156         $feeds['plugins'] = array(
    1157             'link'         => '',
    1158             'url'          => array(
    1159                 'popular' => 'http://wordpress.org/plugins/rss/browse/popular/',
    1160             ),
    1161             'title'        => '',
    1162             'items'        => 1,
    1163             'show_summary' => 0,
    1164             'show_author'  => 0,
    1165             'show_date'    => 0,
    1166         );
    1167     }
    1168 
    11691358    wp_dashboard_cached_rss_widget( 'dashboard_primary', 'wp_dashboard_primary_output', $feeds );
    11701359}
     
    11741363 *
    11751364 * @since 3.8.0
     1365 * @since 4.8.0 Removed popular plugins feed.
    11761366 *
    11771367 * @param string $widget_id Widget ID.
     
    11821372        $args['type'] = $type;
    11831373        echo '<div class="rss-widget">';
    1184         if ( $type === 'plugins' ) {
    1185             wp_dashboard_plugins_output( $args['url'], $args );
    1186         } else {
    11871374            wp_widget_rss_output( $args['url'], $args );
    1188         }
    11891375        echo "</div>";
    11901376    }
    1191 }
    1192 
    1193 /**
    1194  * Display plugins text for the WordPress news widget.
    1195  *
    1196  * @since 2.5.0
    1197  *
    1198  * @param string $rss  The RSS feed URL.
    1199  * @param array  $args Array of arguments for this RSS feed.
    1200  */
    1201 function wp_dashboard_plugins_output( $rss, $args = array() ) {
    1202     // Plugin feeds plus link to install them
    1203     $popular = fetch_feed( $args['url']['popular'] );
    1204 
    1205     if ( false === $plugin_slugs = get_transient( 'plugin_slugs' ) ) {
    1206         $plugin_slugs = array_keys( get_plugins() );
    1207         set_transient( 'plugin_slugs', $plugin_slugs, DAY_IN_SECONDS );
    1208     }
    1209 
    1210     echo '<ul>';
    1211 
    1212     foreach ( array( $popular ) as $feed ) {
    1213         if ( is_wp_error( $feed ) || ! $feed->get_item_quantity() )
    1214             continue;
    1215 
    1216         $items = $feed->get_items(0, 5);
    1217 
    1218         // Pick a random, non-installed plugin
    1219         while ( true ) {
    1220             // Abort this foreach loop iteration if there's no plugins left of this type
    1221             if ( 0 == count($items) )
    1222                 continue 2;
    1223 
    1224             $item_key = array_rand($items);
    1225             $item = $items[$item_key];
    1226 
    1227             list($link, $frag) = explode( '#', $item->get_link() );
    1228 
    1229             $link = esc_url($link);
    1230             if ( preg_match( '|/([^/]+?)/?$|', $link, $matches ) )
    1231                 $slug = $matches[1];
    1232             else {
    1233                 unset( $items[$item_key] );
    1234                 continue;
    1235             }
    1236 
    1237             // Is this random plugin's slug already installed? If so, try again.
    1238             reset( $plugin_slugs );
    1239             foreach ( $plugin_slugs as $plugin_slug ) {
    1240                 if ( $slug == substr( $plugin_slug, 0, strlen( $slug ) ) ) {
    1241                     unset( $items[$item_key] );
    1242                     continue 2;
    1243                 }
    1244             }
    1245 
    1246             // If we get to this point, then the random plugin isn't installed and we can stop the while().
    1247             break;
    1248         }
    1249 
    1250         // Eliminate some common badly formed plugin descriptions
    1251         while ( ( null !== $item_key = array_rand($items) ) && false !== strpos( $items[$item_key]->get_description(), 'Plugin Name:' ) )
    1252             unset($items[$item_key]);
    1253 
    1254         if ( !isset($items[$item_key]) )
    1255             continue;
    1256 
    1257         $raw_title = $item->get_title();
    1258 
    1259         $ilink = wp_nonce_url('plugin-install.php?tab=plugin-information&plugin=' . $slug, 'install-plugin_' . $slug) . '&amp;TB_iframe=true&amp;width=600&amp;height=800';
    1260         echo '<li class="dashboard-news-plugin"><span>' . __( 'Popular Plugin' ) . ':</span> ' . esc_html( $raw_title ) .
    1261             '&nbsp;<a href="' . $ilink . '" class="thickbox open-plugin-details-modal" aria-label="' .
    1262             /* translators: %s: plugin name */
    1263             esc_attr( sprintf( __( 'Install %s' ), $raw_title ) ) . '">(' . __( 'Install' ) . ')</a></li>';
    1264 
    1265         $feed->__destruct();
    1266         unset( $feed );
    1267     }
    1268 
    1269     echo '</ul>';
    12701377}
    12711378
Note: See TracChangeset for help on using the changeset viewer.