From 5658305b1c06a1fca8a91e098ac72485cab76fe6 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov <sergeybiryukov.ru@gmail.com> Date: Thu, 7 Mar 2019 09:12:51 +0000 Subject: [PATCH] Date/Time: Reduce explicit local `current_time( 'timestamp' )` usage in favor of native PHP functions. Timestamps don't carry any timezone information, using the intended format directly simplifies the logic and makes the code less confusing. Props Rarst, jdgrimes. See #40657. Built from https://develop.svn.wordpress.org/trunk@44809 git-svn-id: http://core.svn.wordpress.org/trunk@44641 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 2 +- wp-admin/includes/dashboard.php | 7 +- wp-admin/includes/template.php | 23 +++-- wp-admin/includes/user.php | 2 +- wp-includes/blocks/calendar.php | 71 +++++++++++++++ wp-includes/blocks/rss.php | 137 +++++++++++++++++++++++++++++ wp-includes/blocks/search.php | 82 +++++++++++++++++ wp-includes/blocks/tag-cloud.php | 71 +++++++++++++++ wp-includes/general-template.php | 11 ++- wp-includes/link-template.php | 12 +-- wp-includes/ms-functions.php | 6 +- wp-includes/version.php | 2 +- 12 files changed, 393 insertions(+), 33 deletions(-) create mode 100644 wp-includes/blocks/calendar.php create mode 100644 wp-includes/blocks/rss.php create mode 100644 wp-includes/blocks/search.php create mode 100644 wp-includes/blocks/tag-cloud.php diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index c565817914..f273c4df6f 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -1464,7 +1464,7 @@ function wp_ajax_add_meta() { $post_data['post_ID'] = $pid; $post_data['post_type'] = $post->post_type; $post_data['post_status'] = 'draft'; - $now = current_time( 'timestamp', 1 ); + $now = time(); /* translators: 1: Post creation date, 2: Post creation time */ $post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( __( 'F j, Y' ), $now ), date( __( 'g:i a' ), $now ) ); diff --git a/wp-admin/includes/dashboard.php b/wp-admin/includes/dashboard.php index 7bebe3ff20..e3e8cca9a7 100644 --- a/wp-admin/includes/dashboard.php +++ b/wp-admin/includes/dashboard.php @@ -858,8 +858,9 @@ function wp_dashboard_recent_posts( $args ) { echo '<ul>'; - $today = date( 'Y-m-d', current_time( 'timestamp' ) ); - $tomorrow = date( 'Y-m-d', strtotime( '+1 day', current_time( 'timestamp' ) ) ); + $today = current_time( 'Y-m-d' ); + $tomorrow = gmdate( 'Y-m-d', strtotime( '+1 day', current_time( 'timestamp' ) ) ); + $year = current_time( 'Y' ); while ( $posts->have_posts() ) { $posts->the_post(); @@ -869,7 +870,7 @@ function wp_dashboard_recent_posts( $args ) { $relative = __( 'Today' ); } elseif ( date( 'Y-m-d', $time ) == $tomorrow ) { $relative = __( 'Tomorrow' ); - } elseif ( date( 'Y', $time ) !== date( 'Y', current_time( 'timestamp' ) ) ) { + } elseif ( date( 'Y', $time ) !== $year ) { /* translators: date and time format for recent posts on the dashboard, from a different calendar year, see https://secure.php.net/date */ $relative = date_i18n( __( 'M jS Y' ), $time ); } else { diff --git a/wp-admin/includes/template.php b/wp-admin/includes/template.php index 6336ee3d2d..a06f9228aa 100644 --- a/wp-admin/includes/template.php +++ b/wp-admin/includes/template.php @@ -774,20 +774,19 @@ function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) { // todo: Remove this? // echo '<label for="timestamp" style="display: block;"><input type="checkbox" class="checkbox" name="edit_date" value="1" id="timestamp"'.$tab_index_attribute.' /> '.__( 'Edit timestamp' ).'</label><br />'; - $time_adj = current_time( 'timestamp' ); $post_date = ( $for_post ) ? $post->post_date : get_comment()->comment_date; - $jj = ( $edit ) ? mysql2date( 'd', $post_date, false ) : gmdate( 'd', $time_adj ); - $mm = ( $edit ) ? mysql2date( 'm', $post_date, false ) : gmdate( 'm', $time_adj ); - $aa = ( $edit ) ? mysql2date( 'Y', $post_date, false ) : gmdate( 'Y', $time_adj ); - $hh = ( $edit ) ? mysql2date( 'H', $post_date, false ) : gmdate( 'H', $time_adj ); - $mn = ( $edit ) ? mysql2date( 'i', $post_date, false ) : gmdate( 'i', $time_adj ); - $ss = ( $edit ) ? mysql2date( 's', $post_date, false ) : gmdate( 's', $time_adj ); + $jj = ( $edit ) ? mysql2date( 'd', $post_date, false ) : current_time( 'd' ); + $mm = ( $edit ) ? mysql2date( 'm', $post_date, false ) : current_time( 'm' ); + $aa = ( $edit ) ? mysql2date( 'Y', $post_date, false ) : current_time( 'Y' ); + $hh = ( $edit ) ? mysql2date( 'H', $post_date, false ) : current_time( 'H' ); + $mn = ( $edit ) ? mysql2date( 'i', $post_date, false ) : current_time( 'i' ); + $ss = ( $edit ) ? mysql2date( 's', $post_date, false ) : current_time( 's' ); - $cur_jj = gmdate( 'd', $time_adj ); - $cur_mm = gmdate( 'm', $time_adj ); - $cur_aa = gmdate( 'Y', $time_adj ); - $cur_hh = gmdate( 'H', $time_adj ); - $cur_mn = gmdate( 'i', $time_adj ); + $cur_jj = current_time( 'd' ); + $cur_mm = current_time( 'm' ); + $cur_aa = current_time( 'Y' ); + $cur_hh = current_time( 'H' ); + $cur_mn = current_time( 'i' ); $month = '<label><span class="screen-reader-text">' . __( 'Month' ) . '</span><select ' . ( $multi ? '' : 'id="mm" ' ) . 'name="mm"' . $tab_index_attribute . ">\n"; for ( $i = 1; $i < 13; $i = $i + 1 ) { diff --git a/wp-admin/includes/user.php b/wp-admin/includes/user.php index 56224cfb65..356a7c68fc 100644 --- a/wp-admin/includes/user.php +++ b/wp-admin/includes/user.php @@ -1392,7 +1392,7 @@ abstract class WP_Privacy_Requests_Table extends WP_List_Table { return ''; } - $time_diff = current_time( 'timestamp', true ) - $timestamp; + $time_diff = time() - $timestamp; if ( $time_diff >= 0 && $time_diff < DAY_IN_SECONDS ) { /* translators: human readable timestamp */ diff --git a/wp-includes/blocks/calendar.php b/wp-includes/blocks/calendar.php new file mode 100644 index 0000000000..9177997692 --- /dev/null +++ b/wp-includes/blocks/calendar.php @@ -0,0 +1,71 @@ +<?php +/** + * Server-side rendering of the `core/calendar` block. + * + * @package WordPress + */ + +/** + * Renders the `core/calendar` block on server. + * + * @param array $attributes The block attributes. + * + * @return string Returns the block content. + */ +function render_block_core_calendar( $attributes ) { + global $monthnum, $year, $post; + $previous_monthnum = $monthnum; + $previous_year = $year; + + if ( isset( $attributes['month'] ) ) { + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited + $monthnum = $attributes['month']; + } + + if ( isset( $attributes['year'] ) ) { + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited + $year = $attributes['year']; + } + + $custom_class_name = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className']; + $align_class_name = empty( $attributes['align'] ) ? '' : ' ' . "align{$attributes['align']}"; + + return sprintf( + '<div class="%1$s">%2$s</div>', + esc_attr( 'wp-block-calendar' . $custom_class_name . $align_class_name ), + get_calendar( true, false ) + ); + + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited + $monthnum = $previous_monthnum; + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited + $year = $previous_year; +} + +/** + * Registers the `core/calendar` block on server. + */ +function register_block_core_calendar() { + register_block_type( + 'core/calendar', + array( + 'attributes' => array( + 'align' => array( + 'type' => 'string', + ), + 'className' => array( + 'type' => 'string', + ), + 'month' => array( + 'type' => 'integer', + ), + 'year' => array( + 'type' => 'integer', + ), + ), + 'render_callback' => 'render_block_core_calendar', + ) + ); +} + +add_action( 'init', 'register_block_core_calendar' ); diff --git a/wp-includes/blocks/rss.php b/wp-includes/blocks/rss.php new file mode 100644 index 0000000000..b5fe3abaf5 --- /dev/null +++ b/wp-includes/blocks/rss.php @@ -0,0 +1,137 @@ +<?php +/** + * Server-side rendering of the `core/rss` block. + * + * @package WordPress + */ + +/** + * Renders the `core/rss` block on server. + * + * @param array $attributes The block attributes. + * + * @return string Returns the block content with received rss items. + */ +function render_block_core_rss( $attributes ) { + $rss = fetch_feed( $attributes['feedURL'] ); + + if ( is_wp_error( $rss ) ) { + return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</div></div>'; + } + + if ( ! $rss->get_item_quantity() ) { + // PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks. + $rss->__destruct(); + unset( $rss ); + + return '<div class="components-placeholder"><div class="notice notice-error">' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</div></div>'; + } + + $rss_items = $rss->get_items( 0, $attributes['itemsToShow'] ); + $list_items = ''; + foreach ( $rss_items as $item ) { + $title = esc_html( trim( strip_tags( $item->get_title() ) ) ); + if ( empty( $title ) ) { + $title = __( '(Untitled)' ); + } + $link = $item->get_link(); + $link = esc_url( $link ); + if ( $link ) { + $title = "<a href='{$link}'>{$title}</a>"; + } + $title = "<div class='wp-block-rss__item-title'>{$title}</div>"; + + $date = ''; + if ( $attributes['displayDate'] ) { + $date = $item->get_date( 'U' ); + + if ( $date ) { + $date = sprintf( + '<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ', + date_i18n( get_option( 'c' ), $date ), + date_i18n( get_option( 'date_format' ), $date ) + ); + } + } + + $author = ''; + if ( $attributes['displayAuthor'] ) { + $author = $item->get_author(); + if ( is_object( $author ) ) { + $author = $author->get_name(); + $author = '<span class="wp-block-rss__item-author">' . __( 'by' ) . ' ' . esc_html( strip_tags( $author ) ) . '</span>'; + } + } + + $excerpt = ''; + if ( $attributes['displayExcerpt'] ) { + $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); + $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); + + // Change existing [...] to […]. + if ( '[...]' == substr( $excerpt, -5 ) ) { + $excerpt = substr( $excerpt, 0, -5 ) . '[…]'; + } + + $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>'; + } + + $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>"; + } + + $classes = 'grid' === $attributes['blockLayout'] ? ' is-grid columns-' . $attributes['columns'] : ''; + $list_items_markup = "<ul class='wp-block-rss{$classes}'>{$list_items}</ul>"; + + // PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks. + $rss->__destruct(); + unset( $rss ); + + return $list_items_markup; +} + +/** + * Registers the `core/rss` block on server. + */ +function register_block_core_rss() { + register_block_type( 'core/rss', + array( + 'attributes' => array( + 'columns' => array( + 'type' => 'number', + 'default' => 2, + ), + 'blockLayout' => array( + 'type' => 'string', + 'default' => 'list', + ), + 'feedURL' => array( + 'type' => 'string', + 'default' => '', + ), + 'itemsToShow' => array( + 'type' => 'number', + 'default' => 5, + ), + 'displayExcerpt' => array( + 'type' => 'boolean', + 'default' => false, + ), + 'displayAuthor' => array( + 'type' => 'boolean', + 'default' => false, + ), + 'displayDate' => array( + 'type' => 'boolean', + 'default' => false, + ), + 'excerptLength' => array( + 'type' => 'number', + 'default' => 55, + ), + ), + 'render_callback' => 'render_block_core_rss', + ) + ); +} + +add_action( 'init', 'register_block_core_rss' ); diff --git a/wp-includes/blocks/search.php b/wp-includes/blocks/search.php new file mode 100644 index 0000000000..ac7da463e9 --- /dev/null +++ b/wp-includes/blocks/search.php @@ -0,0 +1,82 @@ +<?php +/** + * Server-side rendering of the `core/search` block. + * + * @package WordPress + */ + +/** + * Dynamically renders the `core/search` block. + * + * @param array $attributes The block attributes. + * + * @return string The search block markup. + */ +function render_block_core_search( $attributes ) { + static $instance_id = 0; + + $input_id = 'wp-block-search__input-' . ++$instance_id; + + if ( ! empty( $attributes['label'] ) ) { + $label_markup = sprintf( + '<label for="%s" class="wp-block-search__label">%s</label>', + $input_id, + $attributes['label'] + ); + } + + $input_markup = sprintf( + '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" />', + $input_id, + esc_attr( get_search_query() ), + esc_attr( $attributes['placeholder'] ) + ); + + if ( ! empty( $attributes['buttonText'] ) ) { + $button_markup = sprintf( + '<button type="submit" class="wp-block-search__button">%s</button>', + $attributes['buttonText'] + ); + } + + $class = 'wp-block-search'; + if ( isset( $attributes['className'] ) ) { + $class .= ' ' . $attributes['className']; + } + + return sprintf( + '<form class="%s" role="search" method="get" action="%s">%s</form>', + $class, + esc_url( home_url( '/' ) ), + $label_markup . $input_markup . $button_markup + ); +} + +/** + * Registers the `core/search` block on the server. + */ +function register_block_core_search() { + register_block_type( + 'core/search', + array( + 'attributes' => array( + 'label' => array( + 'type' => 'string', + 'default' => __( 'Search' ), + ), + 'placeholder' => array( + 'type' => 'string', + 'default' => '', + ), + 'buttonText' => array( + 'type' => 'string', + 'default' => __( 'Search' ), + ), + ), + + 'render_callback' => 'render_block_core_search', + ) + ); +} + +add_action( 'init', 'register_block_core_search' ); diff --git a/wp-includes/blocks/tag-cloud.php b/wp-includes/blocks/tag-cloud.php new file mode 100644 index 0000000000..c60f8a1dd6 --- /dev/null +++ b/wp-includes/blocks/tag-cloud.php @@ -0,0 +1,71 @@ +<?php +/** + * Server-side rendering of the `core/tag-cloud` block. + * + * @package WordPress + */ + +/** + * Renders the `core/tag-cloud` block on server. + * + * @param array $attributes The block attributes. + * + * @return string Returns the tag cloud for selected taxonomy. + */ +function render_block_core_tag_cloud( $attributes ) { + $class = isset( $attributes['align'] ) ? + "wp-block-tag-cloud align{$attributes['align']}" : + 'wp-block-tag-cloud'; + + if ( isset( $attributes['className'] ) ) { + $class .= ' ' . $attributes['className']; + } + + $args = array( + 'echo' => false, + 'taxonomy' => $attributes['taxonomy'], + 'show_count' => $attributes['showTagCounts'], + ); + + $tag_cloud = wp_tag_cloud( $args ); + + if ( ! $tag_cloud ) { + $tag_cloud = esc_html( __( 'No terms to show.' ) ); + } + + return sprintf( + '<p class="%1$s">%2$s</p>', + esc_attr( $class ), + $tag_cloud + ); +} + +/** + * Registers the `core/tag-cloud` block on server. + */ +function register_block_core_tag_cloud() { + register_block_type( + 'core/tag-cloud', + array( + 'attributes' => array( + 'taxonomy' => array( + 'type' => 'string', + 'default' => 'post_tag', + ), + 'className' => array( + 'type' => 'string', + ), + 'showTagCounts' => array( + 'type' => 'boolean', + 'default' => false, + ), + 'align' => array( + 'type' => 'string', + ), + ), + 'render_callback' => 'render_block_core_tag_cloud', + ) + ); +} + +add_action( 'init', 'register_block_core_tag_cloud' ); diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 11d4b77ff4..f63bafb406 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -2005,7 +2005,6 @@ function get_calendar( $initial = true, $echo = true ) { } // week_begins = 0 stands for Sunday $week_begins = (int) get_option( 'start_of_week' ); - $ts = current_time( 'timestamp' ); // Let's figure out when we are if ( ! empty( $monthnum ) && ! empty( $year ) ) { @@ -2025,8 +2024,8 @@ function get_calendar( $initial = true, $echo = true ) { $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 ); } } else { - $thisyear = gmdate( 'Y', $ts ); - $thismonth = gmdate( 'm', $ts ); + $thisyear = current_time( 'Y' ); + $thismonth = current_time( 'm' ); } $unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear ); @@ -2136,9 +2135,9 @@ function get_calendar( $initial = true, $echo = true ) { } $newrow = false; - if ( $day == gmdate( 'j', $ts ) && - $thismonth == gmdate( 'm', $ts ) && - $thisyear == gmdate( 'Y', $ts ) ) { + if ( $day == current_time( 'j' ) && + $thismonth == current_time( 'm' ) && + $thisyear == current_time( 'Y' ) ) { $calendar_output .= '<td id="today">'; } else { $calendar_output .= '<td>'; diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index be39e36279..ef604dcc7d 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -470,7 +470,7 @@ function get_attachment_link( $post = null, $leavename = false ) { function get_year_link( $year ) { global $wp_rewrite; if ( ! $year ) { - $year = gmdate( 'Y', current_time( 'timestamp' ) ); + $year = current_time( 'Y' ); } $yearlink = $wp_rewrite->get_year_permastruct(); if ( ! empty( $yearlink ) ) { @@ -505,10 +505,10 @@ function get_year_link( $year ) { function get_month_link( $year, $month ) { global $wp_rewrite; if ( ! $year ) { - $year = gmdate( 'Y', current_time( 'timestamp' ) ); + $year = current_time( 'Y' ); } if ( ! $month ) { - $month = gmdate( 'm', current_time( 'timestamp' ) ); + $month = current_time( 'm' ); } $monthlink = $wp_rewrite->get_month_permastruct(); if ( ! empty( $monthlink ) ) { @@ -546,13 +546,13 @@ function get_month_link( $year, $month ) { function get_day_link( $year, $month, $day ) { global $wp_rewrite; if ( ! $year ) { - $year = gmdate( 'Y', current_time( 'timestamp' ) ); + $year = current_time( 'Y' ); } if ( ! $month ) { - $month = gmdate( 'm', current_time( 'timestamp' ) ); + $month = current_time( 'm' ); } if ( ! $day ) { - $day = gmdate( 'j', current_time( 'timestamp' ) ); + $day = current_time( 'j' ); } $daylink = $wp_rewrite->get_day_permastruct(); diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index 620e9c9194..75dab9e09c 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -518,7 +518,7 @@ function wpmu_validate_user_signup( $user_name, $user_email ) { $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name ) ); if ( $signup != null ) { $registered_at = mysql2date( 'U', $signup->registered ); - $now = current_time( 'timestamp', true ); + $now = time(); $diff = $now - $registered_at; // If registered more than two days ago, cancel registration and let this signup go through. if ( $diff > 2 * DAY_IN_SECONDS ) { @@ -530,7 +530,7 @@ function wpmu_validate_user_signup( $user_name, $user_email ) { $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email ) ); if ( $signup != null ) { - $diff = current_time( 'timestamp', true ) - mysql2date( 'U', $signup->registered ); + $diff = time() - mysql2date( 'U', $signup->registered ); // If registered more than two days ago, cancel registration and let this signup go through. if ( $diff > 2 * DAY_IN_SECONDS ) { $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) ); @@ -688,7 +688,7 @@ function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) { // Has someone already signed up for this domain? $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path ) ); // TODO: Check email too? if ( ! empty( $signup ) ) { - $diff = current_time( 'timestamp', true ) - mysql2date( 'U', $signup->registered ); + $diff = time() - mysql2date( 'U', $signup->registered ); // If registered more than two days ago, cancel registration and let this signup go through. if ( $diff > 2 * DAY_IN_SECONDS ) { $wpdb->delete( diff --git a/wp-includes/version.php b/wp-includes/version.php index 54e23561d4..c1043d473b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.2-alpha-44808'; +$wp_version = '5.2-alpha-44809'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.