WordPress.org

Make WordPress Core

Ticket #29351: 29351-initial-idea.patch

File 29351-initial-idea.patch, 2.4 KB (added by hypertextranch, 5 years ago)

Initial idea patch

  • wp-includes/option.php

     
    703703}
    704704
    705705/**
     706* Get the value of a transient if it exists, if it has expired then refresh the
     707* value via the callback passed in and continue returning the stale value for
     708* subsquent calls for 1 minute.
     709*
     710* If no stale values exist in cache use the default value passed in as the
     711* initial stale value.
     712*
     713* @since 4.x
     714*
     715* @param string   $transient  Transient name. Expected to not be SQL-escaped.
     716*                             Must be 45 characters or fewer in length.
     717* @param callable $callback   PHP callback used to generate transient value.
     718*                             Must be serializable if non-scalar. Expected to
     719*                             not be SQL-escaped.
     720* @param int      $expiration Optional. Time until expiration in seconds.
     721*                             Default 0.
     722* @param mixed    $default    Optional. The initial stale cache value. Default
     723*                             false.
     724* @return mixed Value of transient.
     725*/
     726function get_set_soft_transient( $transient, $callback, $expiration = 0, $default = false ) {
     727        $soft_timeout = 60;
     728        $value = get_transient( $transient );
     729
     730        // Nothing in cache so construct default stale value
     731        if ( !isset( $value['_meta'] ) ) {
     732                $value = array(
     733                        '_meta' => array(
     734                                'refreshing' => false,
     735                                'expires' => 0,
     736                        ),
     737                        'payload' => $default,
     738                );
     739        }
     740
     741        // Cache refresh needed and no one else is currently refreshing it
     742        if ( !$value['_meta']['refreshing'] && -1 != $value['_meta']['expires'] && time() > $value['_meta']['expires'] ) {
     743                // Cache refreshing status so we can return stale values for 1 min
     744                $value['_meta']['refreshing'] = true;
     745                set_transient( $transient, $value, $soft_timeout );
     746
     747                // Run slow action and cache results
     748                $value['payload'] = call_user_func( $callback );
     749                $value['_meta'] = array(
     750                        'refreshing' => false,
     751                        'expires' => ( 0 === $expiration ? -1 : time() + $expiration ),
     752                );
     753                set_transient( $transient, $value, ( 0 === $expiration ? 0 : $soft_timeout + $expiration ) );
     754        }
     755
     756        return $value['payload'];
     757}
     758
     759/**
    706760 * Saves and restores user interface settings stored in a cookie.
    707761 *
    708762 * Checks if the current user-settings cookie is updated and stores it. When no