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

V2 site overwrite magic __call method #2798

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -9,7 +9,7 @@

$site = new \Timber\Site();
$content_subdir = Timber\URLHelper::get_content_subdir();
$this->assertEquals($content_subdir . '/themes/timber-test-theme', $site->theme->path);

Check failure on line 12 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Access to an undefined property Timber\Theme::$path.

Check failure on line 12 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestTimberSite::assertEquals().

switch_theme('default');
}
Expand All @@ -19,17 +19,17 @@
restore_current_locale();
$site = new \Timber\Site();
$lang = $site->language_attributes();
$this->assertEquals('lang="en-US"', $lang);

Check failure on line 22 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestTimberSite::assertEquals().
}

public function testChildParentThemeLocation()
{
$content_subdir = Timber\URLHelper::get_content_subdir();
$this->assertFileExists(WP_CONTENT_DIR . '/themes/timber-test-theme-child/style.css');

Check failure on line 28 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestTimberSite::assertFileExists().
switch_theme('timber-test-theme-child');
$site = new Timber\Site();
$this->assertEquals($content_subdir . '/themes/timber-test-theme-child', $site->theme->path);

Check failure on line 31 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Access to an undefined property Timber\Theme::$path.

Check failure on line 31 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestTimberSite::assertEquals().
$this->assertEquals($content_subdir . '/themes/timber-test-theme', $site->theme->parent->path);

Check failure on line 32 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Access to an undefined property Timber\Theme::$path.

Check failure on line 32 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestTimberSite::assertEquals().

switch_theme('default');
}
Expand All @@ -38,8 +38,8 @@
{
switch_theme('timber-test-theme');

$context = Timber::context();

Check failure on line 41 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to static method context() on an unknown class Timber.
$this->assertEquals('timber-test-theme', $context['theme']->slug);

Check failure on line 42 in tests/test-timber-site.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestTimberSite::assertEquals().

switch_theme('default');
}
Expand Down Expand Up @@ -91,6 +91,18 @@
$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