Skip to content

Commit

Permalink
Merge branch '2.x' into feat-rector
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed May 15, 2024
2 parents 50b35ef + e6cdf7e commit 23a8f7b
Show file tree
Hide file tree
Showing 23 changed files with 165 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/links-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
with:
ref: 2.x

- uses: lycheeverse/lychee-action@v1.9.3
- uses: lycheeverse/lychee-action@v1.10.0
id: lychee
with:
args: --verbose './docs/**/*.md' --max-concurrency 1 --max-retries 0 --accept 200,429 --cache
Expand Down
3 changes: 3 additions & 0 deletions docs/v2/guides/custom-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ For site options, it’s also possible to access it directly through its name:

Please be aware that using this might conflict with existing Timber methods on the `Timber\Site` object. That’s why the `option()` method is the preferred way to retrieve site options.


You cannot fetch ACF options with `site.option()`. You will need to add the fields to the context yourself. This process is described in the [ACF integration](https://timber.github.io/docs/v2/integrations/advanced-custom-fields/#options-page) documentation.

## Query by custom field value

This example that uses a [WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query) array shows the arguments to find all posts where a custom field called `color` has a value of `red`.
Expand Down
4 changes: 2 additions & 2 deletions docs/v2/guides/gutenberg.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Now that we have our block registered, we need to create a function to render ou
*/
function my_acf_block_render_callback($attributes, $content = '', $is_preview = false, $post_id = 0, $wp_block = null) {
// Create the slug of the block using the name property in the block.json.
$slug = str_replace( 'acf/', '', $block['name'] );
$slug = str_replace( 'acf/', '', $attributes['name'] );

$context = Timber::context();

Expand All @@ -148,7 +148,7 @@ function my_acf_block_render_callback($attributes, $content = '', $is_preview =
);
}
```
We call this function in the `renderCallback` object in the block.json file of our block. This function will work for all blocks as long as we follow the naming convention of `acf/your-block-name` for the name property in the block.json and name the template `your-block-name.twig` in the blocks folder inside the views directory.
We call this function in the `renderCallback` object in the block.json file of our block. This function will work for all blocks as long as we follow the naming convention of `acf/your-block-name` for the name property in the block.json and name the template `your-block-name.twig` in the `blocks/your-block-name` folder inside the root of your theme.

#### Create fields in ACF

Expand Down
5 changes: 0 additions & 5 deletions src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Timber;

use InvalidArgumentException;
use Timber\Factory\PostFactory;

/**
Expand Down Expand Up @@ -240,10 +239,6 @@ public function size(): ?int
return $this->size = (int) $size;
}

if (!ImageHelper::is_protocol_allowed($this->file_loc())) {
throw new InvalidArgumentException('The output file scheme is not supported.');
}

/**
* Filesize wasn't found in the metadata, so we'll try to get it from the file itself.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected function __construct()
* @internal
* @param WP_Comment $wp_comment a native WP_Comment instance
*/
public static function build(WP_Comment $wp_comment): self
public static function build(WP_Comment $wp_comment): static
{
$comment = new static();

Check failure on line 138 in src/Comment.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Unsafe usage of new static().
$comment->import($wp_comment);
Expand Down
14 changes: 7 additions & 7 deletions src/Image/Operation/Resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function get_target_sizes(WP_Image_Editor $image)
$w = \round($h * $src_ratio);
}

if (!$crop || $crop === 'default') {
if (!$crop) {
return [
'x' => 0,
'y' => 0,
Expand Down Expand Up @@ -204,12 +204,12 @@ public function run($load_filename, $save_filename)

$crop = $this->get_target_sizes($image);
$image->crop(
$crop['x'],
$crop['y'],
$crop['src_w'],
$crop['src_h'],
$crop['target_w'],
$crop['target_h']
(int) $crop['x'],
(int) $crop['y'],
(int) $crop['src_w'],
(int) $crop['src_h'],
(int) $crop['target_w'],
(int) $crop['target_h']
);
$quality = \apply_filters('wp_editor_set_quality', 82, 'image/jpeg');
$image->set_quality($quality);
Expand Down
5 changes: 0 additions & 5 deletions src/Image/Operation/ToJpg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Timber\Image\Operation;

use InvalidArgumentException;
use Timber\Image\Operation as ImageOperation;
use Timber\ImageHelper;

Expand Down Expand Up @@ -43,10 +42,6 @@ public function filename($src_filename, $src_extension = 'jpg')
*/
public function run($load_filename, $save_filename)
{
if (!ImageHelper::is_protocol_allowed($load_filename)) {
throw new InvalidArgumentException('The output file scheme is not supported.');
}

if (!\file_exists($load_filename)) {
return false;
}
Expand Down
5 changes: 0 additions & 5 deletions src/Image/Operation/ToWebp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Timber\Image\Operation;

use InvalidArgumentException;
use Timber\Helper;
use Timber\Image\Operation as ImageOperation;
use Timber\ImageHelper;
Expand Down Expand Up @@ -44,10 +43,6 @@ public function filename($src_filename, $src_extension = 'webp')
*/
public function run($load_filename, $save_filename)
{
if (!ImageHelper::is_protocol_allowed($load_filename)) {
throw new InvalidArgumentException('The output file scheme is not supported.');
}

if (!\is_file($load_filename)) {
return false;
}
Expand Down
6 changes: 0 additions & 6 deletions src/ImageDimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Timber;

use InvalidArgumentException;

/**
* Class FileSize
*
Expand Down Expand Up @@ -120,10 +118,6 @@ public function get_dimension($dimension): ?int
return $this->get_dimension_loaded($dimension);
}

if (!ImageHelper::is_protocol_allowed($this->file_loc)) {
throw new InvalidArgumentException('The output file scheme is not supported.');
}

// Load dimensions.
if (\file_exists($this->file_loc) && \filesize($this->file_loc)) {
if (ImageHelper::is_svg($this->file_loc)) {
Expand Down
61 changes: 1 addition & 60 deletions src/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Timber;

use InvalidArgumentException;
use Timber\Image\Operation;

/**
Expand Down Expand Up @@ -30,10 +29,6 @@ class ImageHelper

public static $home_url;

protected const ALLOWED_PROTOCOLS = ['file', 'http', 'https'];

protected const WINDOWS_LOCAL_FILENAME_REGEX = '/^[a-z]:(?:[\\\\\/]?(?:[\w\s!#()-]+|[\.]{1,2})+)*[\\\\\/]?/i';

/**
* Inits the object.
*/
Expand Down Expand Up @@ -145,11 +140,6 @@ public static function is_animated_gif($file)
//doesn't have .gif, bail
return false;
}

if (!ImageHelper::is_protocol_allowed($file)) {
throw new InvalidArgumentException('The output file scheme is not supported.');
}

// Its a gif so test
if (!($fh = @\fopen($file, 'rb'))) {
return false;
Expand Down Expand Up @@ -179,15 +169,7 @@ public static function is_animated_gif($file)
*/
public static function is_svg($file_path)
{
if ('' === $file_path) {
return false;
}

if (!ImageHelper::is_protocol_allowed($file_path)) {
throw new InvalidArgumentException('The output file scheme is not supported.');
}

if (!\file_exists($file_path)) {
if ('' === $file_path || !\file_exists($file_path)) {
return false;
}

Expand Down Expand Up @@ -449,10 +431,6 @@ public static function get_sideloaded_file_loc($file)
*/
public static function sideload_image($file)
{
if (!ImageHelper::is_protocol_allowed($file)) {
throw new InvalidArgumentException('The output file scheme is not supported.');
}

/**
* Adds a filter to change the upload folder temporarily.
*
Expand All @@ -466,7 +444,6 @@ public static function sideload_image($file)
\add_filter('upload_dir', [self::class, 'set_sideload_image_upload_dir']);

$loc = self::get_sideloaded_file_loc($file);

if (\file_exists($loc)) {
$url = URLHelper::file_system_to_url($loc);

Expand Down Expand Up @@ -829,10 +806,6 @@ private static function _operate($src, $op, $force = false)
return '';
}

if (!ImageHelper::is_protocol_allowed($src)) {
throw new InvalidArgumentException('The output file scheme is not supported.');
}

$allow_fs_write = \apply_filters('timber/allow_fs_write', true);

if ($allow_fs_write === false) {
Expand Down Expand Up @@ -976,36 +949,4 @@ public static function get_resize_file_path($url, $w, $h, $crop)
);
return $new_path;
}

/**
* Checks if the protocol of the given filename is allowed.
*
* This fixes a security issue with a PHAR deserialization vulnerability
* with file_exists() in PHP < 8.0.0.
*
* @param string $filepath File path.
* @return bool
*/
public static function is_protocol_allowed($filepath)
{
$parsed_url = \parse_url($filepath);

if (false === $parsed_url) {
throw new InvalidArgumentException('The filename is not valid.');
}

$protocol = isset($parsed_url['scheme'])
? \mb_strtolower($parsed_url['scheme'])
: 'file';

if (
\PHP_OS_FAMILY === 'Windows'
&& \strlen($protocol) === 1
&& \preg_match(self::WINDOWS_LOCAL_FILENAME_REGEX, $filepath)
) {
$protocol = 'file';
}

return \in_array($protocol, self::ALLOWED_PROTOCOLS, true);
}
}
6 changes: 6 additions & 0 deletions src/Integration/AcfIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ public static function transform_relationship($value, $id, $field)
*/
public static function transform_taxonomy($value, $id, $field)
{
if (empty($value)) {
return false;
}
if ($field['field_type'] === 'select' || $field['field_type'] === 'radio') {
return Timber::get_term((int) $value);
}
Expand All @@ -215,6 +218,9 @@ public static function transform_taxonomy($value, $id, $field)
*/
public static function transform_user($value, $id, $field)
{
if (empty($value)) {
return false;
}
if (!$field['multiple']) {
return Timber::get_user((int) $value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class MenuItem extends CoreEntity implements Stringable
* @param Menu $menu The `Menu` object the menu item is associated with.
* @return MenuItem a new MenuItem instance
*/
public static function build($data, ?Menu $menu = null): self
public static function build($data, ?Menu $menu = null): static

Check failure on line 116 in src/MenuItem.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

PHPDoc tag @return with type Timber\MenuItem is not subtype of native type static(Timber\MenuItem).
{
return new static($data, $menu);

Check failure on line 118 in src/MenuItem.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Unsafe usage of new static().
}
Expand Down
28 changes: 24 additions & 4 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class Post extends CoreEntity implements DatedInterface, Setupable, Stringable
* @internal
* @return Post
*/
public static function build(WP_Post $wp_post): self
public static function build(WP_Post $wp_post): static
{
$post = new static();

Expand Down Expand Up @@ -1213,10 +1213,11 @@ protected function get_revised_data_from_method($method, $args = false)
*
* @param int $page Optional. The page to show if the content of the post is split into multiple
* pages. Read more about this in the [Pagination Guide](https://timber.github.io/docs/v2/guides/pagination/#paged-content-within-a-post). Default `0`.
*
* @return string
* @param int $len Optional. The number of words to show. Default `-1` (show all).
* @param bool $remove_blocks Optional. Whether to remove blocks. Defaults to false. True when called from the $post->excerpt() method.
* @return string The content of the post.
*/
public function content($page = 0, $len = -1)
public function content($page = 0, $len = -1, $remove_blocks = false)
{
if ($rd = $this->get_revised_data_from_method('content', [$page, $len])) {
return $rd;
Expand Down Expand Up @@ -1261,6 +1262,25 @@ public function content($page = 0, $len = -1)
}
}

/**
* Filters whether the content produced by block editor blocks should be removed or not from the content.
*
* If truthy then block whose content does not belong in the excerpt, will be removed.
* This removal is done using WordPress Core `excerpt_remove_blocks` function.
*
* @since 2.1.1
*
* @param bool $remove_blocks Whether blocks whose content should not be part of the excerpt should be removed
* or not from the excerpt.
*
* @see excerpt_remove_blocks() The WordPress Core function that will handle the block removal from the excerpt.
*/
$remove_blocks = (bool) \apply_filters('timber/post/content/remove_blocks', $remove_blocks);

if ($remove_blocks) {
$content = \excerpt_remove_blocks($content);
}

$content = $this->content_handle_no_teaser_block($content);
$content = \apply_filters('the_content', ($content));

Expand Down
2 changes: 1 addition & 1 deletion src/PostExcerpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ protected function run()

// Build an excerpt text from the post’s content.
if (empty($text)) {
$text = $this->post->content();
$text = $this->post->content(0, -1, true);
$text = TextHelper::remove_tags($text, $this->destroy_tags);
$text_before_trim = \trim($text);
$text_before_char_trim = '';
Expand Down
2 changes: 1 addition & 1 deletion src/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function __construct()
* @param WP_Term $wp_term The vanilla WordPress term object to build from.
* @return Term
*/
public static function build(WP_Term $wp_term): self
public static function build(WP_Term $wp_term): static
{
$term = new static();
$term->init($wp_term);
Expand Down

0 comments on commit 23a8f7b

Please sign in to comment.