Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Feature/implement irving is main query #272

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
20 changes: 20 additions & 0 deletions inc/components/components/site-theme-provider/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "irving/site-theme-provider",
"_alias": "irving/fragment",
"description": "Provide context from the Site Theme.",
"config": {
"default": {
"default": null,
"description": "The default argument for get_site_theme()."
},
"selector": {
"default": "",
"description": "The selector argument for get_site_theme().",
"type": "string"
},
"value": {}
},
"provides_context": {
"irving/site-theme": "value"
}
}
25 changes: 25 additions & 0 deletions inc/components/components/site-theme-provider/component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Provider for get_site_theme().
*
* Provide context from the Site Theme.
*
* @package WP_Irving
*/

namespace WP_Irving\Components;

use WP_Irving\Templates;

/**
* Register the component and callback.
*/
register_component_from_config(
__DIR__ . '/component',
[
'config_callback' => function ( array $config ): array {
$config['value'] = Templates\get_site_theme( $config['selector'] ?? '', $config['default'] ?? null );
return $config;
},
]
);
12 changes: 12 additions & 0 deletions inc/components/components/template-part/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "irving/template-part",
"_alias": "irving/fragment",
"description": "Load a template part.",
"config": {
"slug": {
"default": "",
"description": "The slug used to locate the template part.",
"type": "string"
}
}
}
28 changes: 28 additions & 0 deletions inc/components/components/template-part/component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Template part loader.
*
* Load a template part.
*
* @package WP_Irving
*/

namespace WP_Irving\Components;

use WP_Irving\Templates;

/**
* Register the component and callback.
*/
register_component_from_config(
__DIR__ . '/component',
[
'children_callback' => function( array $children, array $config ): array {
return Templates\hydrate_template_parts(
[
'name' => 'template-parts/' . ( $config['slug'] ?? '' ),
]
);
},
]
);
11 changes: 11 additions & 0 deletions inc/endpoints/class-components-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
class Components_Endpoint extends Endpoint {

/**
* Flag for determining when the main WP_Query in the Component Endpoint is
* being created. Irving equivelent to is_main_query().
*
* @var boolean
*/
public static $is_main_irving_query = false;

/**
* Path being queried.
*
Expand Down Expand Up @@ -141,7 +149,9 @@ function( $key ) {
ARRAY_FILTER_USE_KEY
);

self::$is_main_irving_query = true;
$this->query = $this->build_query();
self::$is_main_irving_query = false;

// Force trailing slashes on paths.
$this->force_trailing_slashes();
Expand Down Expand Up @@ -349,6 +359,7 @@ public function build_query() {
// This ensures we always parse the query so is_home and other flags
// get properly set before we handle template.
if ( empty( $query ) ) {
$wp_query->query( $query );
Copy link
Contributor Author

@jameswburke jameswburke Oct 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joemcgill, I think we should consider adding this call to query().

If $query is empty in the constructor, then query() never runs, which means get_posts() doesn't run, which means the pre_get_posts hook never runs.

Which means, pre_get_posts never runs on the index/home/front-page query, because $query is an empty string.

Looking for a gut-check from you if this makes sense, or there's something I'm not seeing here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense, though I'm curious what happens on a traditional home page load. I assume we are needing to work around this because normally on a home page load the query args would not be empty.

If we add this call to query() here we should be able to remove the parse_query() call below, since WP_Query::query() calls WP_Query::get_posts(), which calls WP_Query::parse_query() under the hood (see code).

$wp_query->parse_query();
}

Expand Down
20 changes: 20 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Irving functionality.
*
* @package WP_Irving
*/

namespace WP_Irving;

use WP_Irving\REST_API;

/**
* Irving equivelent to is_main_query(). True for the Components Endpoint
* query.
*
* @return boolean
*/
function is_main_irving_query() {
return REST_API\Components_Endpoint::$is_main_irving_query;
}
128 changes: 128 additions & 0 deletions tests/components/test-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ class Test_Components extends WP_UnitTestCase {
*/
public static $term_id;

/**
* Example theme.
*
* @var array
*/
public $site_theme = [
'colors' => [
'black' => '#000000',
'white' => '#FFFFFF',
],
'brand' => [
'primary' => [
'light' => '#FFFFFF',
'main' => '#EEEEEE',
'dark' => '#DDDDDD',
],
],
'nested' => [
'first' => 'colors.white',
'second' => [
'value' => 'colors.white',
],
'third' => [
'value' => 'nested.second.value',
],
],
];

/**
* Helper to get the author ID.
*
Expand Down Expand Up @@ -208,6 +236,22 @@ public function setUp() {

// Disable date organized uploads.
add_filter( 'upload_dir', 'WP_Irving\Test_Helpers::upload_dir_no_subdir' );

// Setup the site theme for testing.
add_filter(
'wp_irving_setup_site_theme',
function( $theme ) {
return $this->site_theme;
}
);

// Hook up template part path filters.
add_filter(
'wp_irving_template_part_path',
function () {
return dirname( __FILE__ ) . '/test-components';
}
);
}

/**
Expand Down Expand Up @@ -971,6 +1015,59 @@ public function test_component_post_title() {
$this->assertComponentEquals( $expected, $component );
}

/**
* Test irving/site-theme-provider component.
*
* @group core-components
*/
public function test_component_provide_site_theme() {
$expected = $this->get_expected_component(
'irving/site-theme-provider',
[
'_alias' => 'irving/fragment',
'config' => [
'selector' => 'nested.first',
'value' => '#FFFFFF',
],
]
);

$component = new Component(
'irving/site-theme-provider',
[
'config' => [
'selector' => 'nested.first',
],
]
);

$this->assertComponentEquals( $expected, $component );

$expected_using_default = $this->get_expected_component(
'irving/site-theme-provider',
[
'_alias' => 'irving/fragment',
'config' => [
'default' => '#000000',
'selector' => 'does.not.exist',
'value' => '#000000',
],
]
);

$component_using_default = new Component(
'irving/site-theme-provider',
[
'config' => [
'default' => '#000000',
'selector' => 'does.not.exist',
],
]
);

$this->assertComponentEquals( $expected_using_default, $component_using_default );
}

/**
* Test irving/query-pagination component.
*
Expand Down Expand Up @@ -1386,6 +1483,37 @@ public function test_component_term_name() {
$this->assertComponentEquals( $expected, $component );
}

/**
* Test irving/template-part component.
*
* @group core-components
*/
public function test_component_template_part() {
$expected = $this->get_expected_component(
'irving/template-part',
[
'_alias' => 'irving/fragment',
'config' => [
'slug' => 'basic',
],
'children' => [
$this->get_expected_component( 'test/basic' ),
],
]
);

$component = new Component(
'irving/template-part',
[
'config' => [
'slug' => 'basic',
],
]
);

$this->assertComponentEquals( $expected, $component );
}

/**
* Test irving/term-link component.
*
Expand Down
1 change: 1 addition & 0 deletions wp-irving.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
require_once WP_IRVING_PATH . '/inc/class-cache.php';
require_once WP_IRVING_PATH . '/inc/class-previews.php';
require_once WP_IRVING_PATH . '/inc/customizer.php';
require_once WP_IRVING_PATH . '/inc/namespace.php';
require_once WP_IRVING_PATH . '/inc/redirects.php';
require_once WP_IRVING_PATH . '/inc/rewrites.php';

Expand Down