Make WordPress Core


Ignore:
Timestamp:
09/01/2015 01:49:00 PM (10 years ago)
Author:
wonderboymusic
Message:

Move widget classes to their own files in wp-includes/widgets:

  • default-widgets.php now requires all of the individual classes
  • Move the functions scattered about this file to widget-functions.php, which loads before default-widgets.php, which only conditionally loads anyway in wp_maybe_load_widgets(), which is hooked on plugins_loaded

See #33413, #23012.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widget-functions.php

    r33757 r33843  
    10961096    return $sidebars_widgets;
    10971097}
     1098
     1099/**
     1100 * Display the RSS entries in a list.
     1101 *
     1102 * @since 2.5.0
     1103 *
     1104 * @param string|array|object $rss RSS url.
     1105 * @param array $args Widget arguments.
     1106 */
     1107function wp_widget_rss_output( $rss, $args = array() ) {
     1108    if ( is_string( $rss ) ) {
     1109        $rss = fetch_feed($rss);
     1110    } elseif ( is_array($rss) && isset($rss['url']) ) {
     1111        $args = $rss;
     1112        $rss = fetch_feed($rss['url']);
     1113    } elseif ( !is_object($rss) ) {
     1114        return;
     1115    }
     1116
     1117    if ( is_wp_error($rss) ) {
     1118        if ( is_admin() || current_user_can('manage_options') )
     1119            echo '<p>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>';
     1120        return;
     1121    }
     1122
     1123    $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0, 'items' => 0 );
     1124    $args = wp_parse_args( $args, $default_args );
     1125
     1126    $items = (int) $args['items'];
     1127    if ( $items < 1 || 20 < $items )
     1128        $items = 10;
     1129    $show_summary  = (int) $args['show_summary'];
     1130    $show_author   = (int) $args['show_author'];
     1131    $show_date     = (int) $args['show_date'];
     1132
     1133    if ( !$rss->get_item_quantity() ) {
     1134        echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
     1135        $rss->__destruct();
     1136        unset($rss);
     1137        return;
     1138    }
     1139
     1140    echo '<ul>';
     1141    foreach ( $rss->get_items( 0, $items ) as $item ) {
     1142        $link = $item->get_link();
     1143        while ( stristr( $link, 'http' ) != $link ) {
     1144            $link = substr( $link, 1 );
     1145        }
     1146        $link = esc_url( strip_tags( $link ) );
     1147
     1148        $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
     1149        if ( empty( $title ) ) {
     1150            $title = __( 'Untitled' );
     1151        }
     1152
     1153        $desc = @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
     1154        $desc = esc_attr( wp_trim_words( $desc, 55, ' [&hellip;]' ) );
     1155
     1156        $summary = '';
     1157        if ( $show_summary ) {
     1158            $summary = $desc;
     1159
     1160            // Change existing [...] to [&hellip;].
     1161            if ( '[...]' == substr( $summary, -5 ) ) {
     1162                $summary = substr( $summary, 0, -5 ) . '[&hellip;]';
     1163            }
     1164
     1165            $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
     1166        }
     1167
     1168        $date = '';
     1169        if ( $show_date ) {
     1170            $date = $item->get_date( 'U' );
     1171
     1172            if ( $date ) {
     1173                $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
     1174            }
     1175        }
     1176
     1177        $author = '';
     1178        if ( $show_author ) {
     1179            $author = $item->get_author();
     1180            if ( is_object($author) ) {
     1181                $author = $author->get_name();
     1182                $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
     1183            }
     1184        }
     1185
     1186        if ( $link == '' ) {
     1187            echo "<li>$title{$date}{$summary}{$author}</li>";
     1188        } elseif ( $show_summary ) {
     1189            echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>";
     1190        } else {
     1191            echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>";
     1192        }
     1193    }
     1194    echo '</ul>';
     1195    $rss->__destruct();
     1196    unset($rss);
     1197}
     1198
     1199/**
     1200 * Display RSS widget options form.
     1201 *
     1202 * The options for what fields are displayed for the RSS form are all booleans
     1203 * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
     1204 * 'show_date'.
     1205 *
     1206 * @since 2.5.0
     1207 *
     1208 * @param array|string $args Values for input fields.
     1209 * @param array $inputs Override default display options.
     1210 */
     1211function wp_widget_rss_form( $args, $inputs = null ) {
     1212    $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
     1213    $inputs = wp_parse_args( $inputs, $default_inputs );
     1214
     1215    $args['title'] = isset( $args['title'] ) ? $args['title'] : '';
     1216    $args['url'] = isset( $args['url'] ) ? $args['url'] : '';
     1217    $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;
     1218
     1219    if ( $args['items'] < 1 || 20 < $args['items'] ) {
     1220        $args['items'] = 10;
     1221    }
     1222
     1223    $args['show_summary']   = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
     1224    $args['show_author']    = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
     1225    $args['show_date']      = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];
     1226
     1227    if ( ! empty( $args['error'] ) ) {
     1228        echo '<p class="widget-error"><strong>' . sprintf( __( 'RSS Error: %s' ), $args['error'] ) . '</strong></p>';
     1229    }
     1230
     1231    $esc_number = esc_attr( $args['number'] );
     1232    if ( $inputs['url'] ) :
     1233?>
     1234    <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label>
     1235    <input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p>
     1236<?php endif; if ( $inputs['title'] ) : ?>
     1237    <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label>
     1238    <input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p>
     1239<?php endif; if ( $inputs['items'] ) : ?>
     1240    <p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label>
     1241    <select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]">
     1242    <?php
     1243    for ( $i = 1; $i <= 20; ++$i ) {
     1244        echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>";
     1245    }
     1246    ?>
     1247    </select></p>
     1248<?php endif; if ( $inputs['show_summary'] ) : ?>
     1249    <p><input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> />
     1250    <label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label></p>
     1251<?php endif; if ( $inputs['show_author'] ) : ?>
     1252    <p><input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> />
     1253    <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label></p>
     1254<?php endif; if ( $inputs['show_date'] ) : ?>
     1255    <p><input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/>
     1256    <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label></p>
     1257<?php
     1258    endif;
     1259    foreach ( array_keys($default_inputs) as $input ) :
     1260        if ( 'hidden' === $inputs[$input] ) :
     1261            $id = str_replace( '_', '-', $input );
     1262?>
     1263    <input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" />
     1264<?php
     1265        endif;
     1266    endforeach;
     1267}
     1268
     1269/**
     1270 * Process RSS feed widget data and optionally retrieve feed items.
     1271 *
     1272 * The feed widget can not have more than 20 items or it will reset back to the
     1273 * default, which is 10.
     1274 *
     1275 * The resulting array has the feed title, feed url, feed link (from channel),
     1276 * feed items, error (if any), and whether to show summary, author, and date.
     1277 * All respectively in the order of the array elements.
     1278 *
     1279 * @since 2.5.0
     1280 *
     1281 * @param array $widget_rss RSS widget feed data. Expects unescaped data.
     1282 * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
     1283 * @return array
     1284 */
     1285function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
     1286    $items = (int) $widget_rss['items'];
     1287    if ( $items < 1 || 20 < $items )
     1288        $items = 10;
     1289    $url           = esc_url_raw( strip_tags( $widget_rss['url'] ) );
     1290    $title         = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
     1291    $show_summary  = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
     1292    $show_author   = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] :0;
     1293    $show_date     = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
     1294
     1295    if ( $check_feed ) {
     1296        $rss = fetch_feed($url);
     1297        $error = false;
     1298        $link = '';
     1299        if ( is_wp_error($rss) ) {
     1300            $error = $rss->get_error_message();
     1301        } else {
     1302            $link = esc_url(strip_tags($rss->get_permalink()));
     1303            while ( stristr($link, 'http') != $link )
     1304                $link = substr($link, 1);
     1305
     1306            $rss->__destruct();
     1307            unset($rss);
     1308        }
     1309    }
     1310
     1311    return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
     1312}
     1313
     1314/**
     1315 * Register all of the default WordPress widgets on startup.
     1316 *
     1317 * Calls 'widgets_init' action after all of the WordPress widgets have been
     1318 * registered.
     1319 *
     1320 * @since 2.2.0
     1321 */
     1322function wp_widgets_init() {
     1323    if ( !is_blog_installed() )
     1324        return;
     1325
     1326    register_widget('WP_Widget_Pages');
     1327
     1328    register_widget('WP_Widget_Calendar');
     1329
     1330    register_widget('WP_Widget_Archives');
     1331
     1332    if ( get_option( 'link_manager_enabled' ) )
     1333        register_widget('WP_Widget_Links');
     1334
     1335    register_widget('WP_Widget_Meta');
     1336
     1337    register_widget('WP_Widget_Search');
     1338
     1339    register_widget('WP_Widget_Text');
     1340
     1341    register_widget('WP_Widget_Categories');
     1342
     1343    register_widget('WP_Widget_Recent_Posts');
     1344
     1345    register_widget('WP_Widget_Recent_Comments');
     1346
     1347    register_widget('WP_Widget_RSS');
     1348
     1349    register_widget('WP_Widget_Tag_Cloud');
     1350
     1351    register_widget('WP_Nav_Menu_Widget');
     1352
     1353    /**
     1354     * Fires after all default WordPress widgets have been registered.
     1355     *
     1356     * @since 2.2.0
     1357     */
     1358    do_action( 'widgets_init' );
     1359}
Note: See TracChangeset for help on using the changeset viewer.