Skip to content

Commit

Permalink
Merge pull request #2798 from timber/v2-site-overwrite-call-method
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Oct 26, 2023
2 parents 5149aa7 + c33ec4d commit dc49063
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __isset($field)
}

/**
* Magic method dispatcher for meta fields, for convience in Twig views.
* Magic method dispatcher for meta fields, for convenience in Twig views.
*
* Called when explicitly invoking non-existent methods on a Core object. This method is not
* meant to be called directly.
Expand Down
29 changes: 29 additions & 0 deletions src/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ public function __construct($site_name_or_id = null)
}
}

/**
* Magic method dispatcher for site option fields, for convenience in Twig views.
*
* Called when explicitly invoking non-existent methods on the Site object. This method is not
* meant to be called directly.
*
* @example
* The following example will dynamically dispatch the magic __call() method with an argument
* of "users_can_register" #}
*
* ```twig
* {% if site.users_can_register %}
* {# Show a notification and link to the register form #}
* {% endif %}
* @link https://secure.php.net/manual/en/language.oop5.overloading.php#object.call
* @link https://github.com/twigphp/Twig/issues/2
* @api
*
* @param string $option The name of the method being called.
* @param array $arguments Enumerated array containing the parameters passed to the function.
* Not used.
*
* @return mixed The value of the option field named `$field` if truthy, `false` otherwise.
*/
public function __call($option, $arguments)
{
return $this->option($option);
}

/**
* Gets the underlying WordPress Core object.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/test-timber-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public function testSiteGet()
$this->assertEquals('bar', $site->foo);
}

public function testSiteCall()
{
update_option('foo', 'barr');
$site = new Timber\Site();

$twig_string = '{{site.foo}}';
$result = Timber\Timber::compile_string($twig_string, [
'site' => $site,
]);
$this->assertEquals('barr', $result);
}

/**
* @expectedDeprecated {{ site.meta() }}
*/
Expand Down

0 comments on commit dc49063

Please sign in to comment.