Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x cs imports #2700

Merged
merged 11 commits into from
Jul 25, 2023
4 changes: 4 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ f0216ce423bb133ce668774a5f75ca86455b7280
# Coding Style: some errors
3a19d4dabbe069fc0133b44f35f4c5e69026d744
40b44b28f6eae28a453a27a4358e5269097de598

# Coding Style: improve imports
e93cd98ba922529b8432b8ed217532f02cb31fda
9c65ea505db911b84d32a425f1f3c3b64c926c61
40 changes: 34 additions & 6 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
<?php

use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
use PhpCsFixer\Fixer\Import\FullyQualifiedStrictTypesFixer;
use PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer;
use PhpCsFixer\Fixer\Import\NoLeadingImportSlashFixer;
use PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer;
use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);
$ecsConfig->paths([__DIR__ . '/src', __DIR__ . '/tests', 'ecs.php']);

$ecsConfig->skip([
NotOperatorWithSuccessorSpaceFixer::class,
]);

$ecsConfig->sets([SetList::PSR_12, SetList::ARRAY, SetList::SPACES, SetList::NAMESPACES]);

$ecsConfig->skip([
NotOperatorWithSuccessorSpaceFixer::class,
]);
/**
* Import
* @see https://cs.symfony.com/doc/rules/index.html#import
*/
$ecsConfig->rule(FullyQualifiedStrictTypesFixer::class);
$ecsConfig->rule(NoLeadingImportSlashFixer::class);
$ecsConfig->rule(SingleImportPerStatementFixer::class);
$ecsConfig->ruleWithConfiguration(GlobalNamespaceImportFixer::class, [
'import_classes' => true,
]);

$ecsConfig->sets([SetList::PSR_12, SetList::ARRAY, SetList::SPACES, SetList::NAMESPACES]);
/**
* NativeFunctionInvocation
* @see https://cs.symfony.com/doc/rules/function_notation/native_function_invocation.html
*/
$ecsConfig->ruleWithConfiguration(NativeFunctionInvocationFixer::class, [
'include' => [
'@all',
],
'scope' => 'namespaced',
'strict' => true,
]);
};
9 changes: 5 additions & 4 deletions src/AccessesPostsLazily.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Timber;

use Timber\Factory\PostFactory;
use ReturnTypeWillChange;

use Timber\Factory\PostFactory;
use WP_Post;

/**
Expand Down Expand Up @@ -75,7 +76,7 @@ public function realize(): self
// Since arbitrary array index access may have happened previously,
// leverage that to ensure each Post is instantiated exactly once.
// We call parent::getArrayCopy() to avoid infinite mutual recursion.
foreach (array_keys(parent::getArrayCopy()) as $k) {
foreach (\array_keys(parent::getArrayCopy()) as $k) {
$this->offsetGet($k);
}
$this->realized = true;
Expand Down Expand Up @@ -110,7 +111,7 @@ public function to_array(): array
*/
public function get_posts(): array
{
Helper::deprecated(sprintf('%s::get_posts()', static::class), sprintf('%s::to_array()', static::class), '2.0.0');
Helper::deprecated(\sprintf('%s::get_posts()', static::class), \sprintf('%s::to_array()', static::class), '2.0.0');
return $this->getArrayCopy();
}

Expand All @@ -119,7 +120,7 @@ public function get_posts(): array
*
* @internal
*/
#[\ReturnTypeWillChange]
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
$post = parent::offsetGet($offset);
Expand Down
6 changes: 3 additions & 3 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class Admin
public static function init()
{
global $wp_version;
if (version_compare('5.3.0', $wp_version) === 1) {
if (\version_compare('5.3.0', $wp_version) === 1) {
// user is running something older that WordPress 5.3 show them an error
$upgrade_url = admin_url('update-core.php');
$upgrade_url = \admin_url('update-core.php');
self::show_notice("<a href='https://github.com/timber/timber'>Timber 2.0</a> requires <strong>WordPress 5.3</strong> or greater, but you are running <strong>WordPress $wp_version</strong>. Please <a href='$upgrade_url'>upgrade WordPress</a> in order to use Timber 2.0.");
}
return true;
Expand All @@ -28,7 +28,7 @@ public static function init()
*/
protected static function show_notice($text, $class = 'error')
{
add_action('admin_notices', function () use ($text, $class) {
\add_action('admin_notices', function () use ($text, $class) {
echo '<div class="' . $class . '"><p>' . $text . '</p></div>';
}, 1);
}
Expand Down
88 changes: 44 additions & 44 deletions src/Archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public function init($args = null, $base = '')
protected function get_archives_link($url, $text, $post_count = 0)
{
$ret = [];
$ret['text'] = $ret['title'] = $ret['name'] = wptexturize($text);
$ret['url'] = $ret['link'] = esc_url(URLHelper::prepend_to_url($url, $this->base));
$ret['text'] = $ret['title'] = $ret['name'] = \wptexturize($text);
$ret['url'] = $ret['link'] = \esc_url(URLHelper::prepend_to_url($url, $this->base));
if ($post_count) {
$ret['post_count'] = (int) $post_count;
}
Expand All @@ -134,16 +134,16 @@ protected function get_items_yearly($args, $last_changed, $join, $where, $order,
global $wpdb;
$output = [];
$query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM {$wpdb->posts} $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
$key = md5($query);
$key = \md5($query);
$key = "wp_get_archives:$key:$last_changed";
if (!$results = wp_cache_get($key, 'posts')) {
if (!$results = \wp_cache_get($key, 'posts')) {
$results = $wpdb->get_results($query);
wp_cache_set($key, $results, 'posts');
\wp_cache_set($key, $results, 'posts');
}
if ($results) {
foreach ((array) $results as $result) {
$url = get_year_link($result->year);
$text = sprintf('%d', $result->year);
$url = \get_year_link($result->year);
$text = \sprintf('%d', $result->year);
$output[] = $this->get_archives_link($url, $text, $result->posts);
}
}
Expand All @@ -168,24 +168,24 @@ protected function get_items_monthly($args, $last_changed, $join, $where, $order
$defaults = [
'show_year' => false,
];
$r = wp_parse_args($args, $defaults);
$r = \wp_parse_args($args, $defaults);

$show_year = $r['show_year'];
//will need to specify which year we're looking for
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts "
. "FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) "
. "ORDER BY post_date $order $limit";
$key = md5($query);
$key = \md5($query);
$key = "wp_get_archives:$key:$last_changed";
if (!$results = wp_cache_get($key, 'posts')) {
if (!$results = \wp_cache_get($key, 'posts')) {
$results = $wpdb->get_results($query);
wp_cache_set($key, $results, 'posts');
\wp_cache_set($key, $results, 'posts');
}
if ($results) {
foreach ((array) $results as $result) {
$url = get_month_link($result->year, $result->month);
$url = \get_month_link($result->year, $result->month);
if ($show_year && !$nested) {
$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year);
$text = \sprintf(\__('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year);
} else {
$text = $wp_locale->get_month($result->month);
}
Expand All @@ -202,7 +202,7 @@ protected function get_items_monthly($args, $last_changed, $join, $where, $order
$out2[] = [
'name' => $year,
'children' => $months,
'post_count' => array_sum(array_column($months, 'post_count')),
'post_count' => \array_sum(\array_column($months, 'post_count')),
];
}
return $out2;
Expand Down Expand Up @@ -248,7 +248,7 @@ public function items($args = null)
$args = $this->args;
}

$args = wp_parse_args($args, $defaults);
$args = \wp_parse_args($args, $defaults);
$post_type = $args['post_type'];
$order = $args['order'];
$nested = $args['nested'];
Expand All @@ -262,10 +262,10 @@ public function items($args = null)
}

if (!empty($args['limit'])) {
$limit = absint($args['limit']);
$limit = \absint($args['limit']);
$limit = ' LIMIT ' . $limit;
}
$order = strtoupper($order);
$order = \strtoupper($order);
if ($order !== 'ASC') {
$order = 'DESC';
}
Expand All @@ -284,28 +284,28 @@ public function items($args = null)
$archive_week_end_date_format = 'Y/m/d';

if (!$archive_date_format_over_ride) {
$archive_day_date_format = get_option('date_format');
$archive_week_start_date_format = get_option('date_format');
$archive_week_end_date_format = get_option('date_format');
$archive_day_date_format = \get_option('date_format');
$archive_week_start_date_format = \get_option('date_format');
$archive_week_end_date_format = \get_option('date_format');
}

$where = $wpdb->prepare('WHERE post_type = "%s" AND post_status = "publish"', $post_type);

/**
* @link https://developer.wordpress.org/reference/hooks/getarchives_where/
*/
$where = apply_filters('getarchives_where', $where, $args);
$where = \apply_filters('getarchives_where', $where, $args);

/**
* @link https://developer.wordpress.org/reference/hooks/getarchives_join/
*/
$join = apply_filters('getarchives_join', '', $args);
$join = \apply_filters('getarchives_join', '', $args);

$output = [];
$last_changed = wp_cache_get('last_changed', 'posts');
$last_changed = \wp_cache_get('last_changed', 'posts');
if (!$last_changed) {
$last_changed = microtime();
wp_cache_set('last_changed', $last_changed, 'posts');
$last_changed = \microtime();
\wp_cache_set('last_changed', $last_changed, 'posts');
}
if ('monthly' == $type) {
$output = $this->get_items_monthly($args, $last_changed, $join, $where, $order, $limit, $nested);
Expand All @@ -315,42 +315,42 @@ public function items($args = null)
$output = $this->get_items_monthly($args, $last_changed, $join, $where, $order, $limit);
} elseif ('daily' == $type) {
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit";
$key = md5($query);
$key = \md5($query);
$key = "wp_get_archives:$key:$last_changed";
if (!$results = wp_cache_get($key, 'posts')) {
if (!$results = \wp_cache_get($key, 'posts')) {
$results = $wpdb->get_results($query);
$cache = [];
$cache[$key] = $results;
wp_cache_set($key, $results, 'posts');
\wp_cache_set($key, $results, 'posts');
}
if ($results) {
foreach ((array) $results as $result) {
$url = get_day_link($result->year, $result->month, $result->dayofmonth);
$date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth);
$text = mysql2date($archive_day_date_format, $date);
$url = \get_day_link($result->year, $result->month, $result->dayofmonth);
$date = \sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth);
$text = \mysql2date($archive_day_date_format, $date);
$output[] = $this->get_archives_link($url, $text, $result->posts);
}
}
} elseif ('weekly' == $type) {
$week = _wp_mysql_week('`post_date`');
$week = \_wp_mysql_week('`post_date`');
$query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, "
. "count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
$key = md5($query);
$key = \md5($query);
$key = "wp_get_archives:$key:$last_changed";
if (!$results = wp_cache_get($key, 'posts')) {
if (!$results = \wp_cache_get($key, 'posts')) {
$results = $wpdb->get_results($query);
wp_cache_set($key, $results, 'posts');
\wp_cache_set($key, $results, 'posts');
}
$arc_w_last = '';
if ($results) {
foreach ((array) $results as $result) {
if ($result->week != $arc_w_last) {
$arc_year = $result->yr;
$arc_w_last = $result->week;
$arc_week = get_weekstartend($result->yyyymmdd, get_option('start_of_week'));
$arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
$arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
$url = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;', '=', $result->week);
$arc_week = \get_weekstartend($result->yyyymmdd, \get_option('start_of_week'));
$arc_week_start = \date_i18n($archive_week_start_date_format, $arc_week['start']);
$arc_week_end = \date_i18n($archive_week_end_date_format, $arc_week['end']);
$url = \sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', \home_url(), '', '?', '=', $arc_year, '&amp;', '=', $result->week);
$text = $arc_week_start . $archive_week_separator . $arc_week_end;
$output[] = $this->get_archives_link($url, $text, $result->posts);
}
Expand All @@ -359,20 +359,20 @@ public function items($args = null)
} elseif ('postbypost' == $type || 'alpha' == $type) {
$orderby = 'alpha' == $type ? 'post_title ASC ' : 'post_date DESC ';
$query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
$key = md5($query);
$key = \md5($query);
$key = "wp_get_archives:$key:$last_changed";
if (!$results = wp_cache_get($key, 'posts')) {
if (!$results = \wp_cache_get($key, 'posts')) {
$results = $wpdb->get_results($query);
wp_cache_set($key, $results, 'posts');
\wp_cache_set($key, $results, 'posts');
}
if ($results) {
foreach ((array) $results as $result) {
if ($result->post_date != '0000-00-00 00:00:00') {
$url = get_permalink($result);
$url = \get_permalink($result);
$text = $result->ID;
if ($result->post_title) {
/** This filter is documented in wp-includes/post-template.php */
$text = strip_tags(apply_filters('the_title', $result->post_title, $result->ID));
$text = \strip_tags(\apply_filters('the_title', $result->post_title, $result->ID));
}
$output[] = $this->get_archives_link($url, $text);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public function __toString()
protected function get_info(array $data): array
{
$post_data = parent::get_info($data);
$image_info = wp_get_attachment_metadata($this->wp_object->ID) ?: [];
$image_info = \wp_get_attachment_metadata($this->wp_object->ID) ?: [];
$meta_values = $this->raw_meta();

$data = array_merge($post_data, $image_info, $meta_values);
$data = \array_merge($post_data, $image_info, $meta_values);

$basedir = wp_get_upload_dir()['basedir'];
$basedir = \wp_get_upload_dir()['basedir'];

if (isset($data['file'])) {
$data['file_loc'] = $basedir . DIRECTORY_SEPARATOR . $data['file'];
Expand Down Expand Up @@ -145,7 +145,7 @@ public function link()
return $this->abs_url;
}

return get_permalink($this->ID);
return \get_permalink($this->ID);
}

/**
Expand Down Expand Up @@ -187,7 +187,7 @@ public function src()
return URLHelper::maybe_secure_url($this->abs_url) ?: null;
}

return wp_get_attachment_url($this->ID) ?: null;
return \wp_get_attachment_url($this->ID) ?: null;
}

/**
Expand Down Expand Up @@ -219,7 +219,7 @@ public function caption(): string
* @param string $caption Caption for the given attachment.
* @param int $post_id Attachment ID.
*/
return apply_filters('wp_get_attachment_caption', $this->post_excerpt, $this->ID);
return \apply_filters('wp_get_attachment_caption', $this->post_excerpt, $this->ID);
}

/**
Expand Down Expand Up @@ -297,10 +297,10 @@ public function size_raw()
public function extension(): ?string
{
if (!$this->file_extension) {
$file_info = wp_check_filetype($this->file);
$file_info = \wp_check_filetype($this->file);

if (!empty($file_info['ext'])) {
$this->file_extension = strtoupper($file_info['ext']);
$this->file_extension = \strtoupper($file_info['ext']);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Cleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function delete_transients()

// Delete transients from multisite, if configured as such

if (is_multisite() && is_main_network()) {
if (\is_multisite() && \is_main_network()) {
$records .= self::delete_transients_multisite();
}
return $records;
Expand Down