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

Integrations API #2405

Merged
merged 30 commits into from
Sep 28, 2021
Merged

Integrations API #2405

merged 30 commits into from
Sep 28, 2021

Conversation

acobster
Copy link
Collaborator

@acobster acobster commented Jan 13, 2021

Ticket: #2402

Issue

New Integrations API!

Solution

  • For consistency with Factory and Image, renamed the namespace to Timber\Integration
  • Implemented the API per proposal What is the official way of initializing Timber? #2404
  • Preferring Gravatars in CoAuthors Plus meant you had to set CoAuthorsPlus::$prefer_gravatar = true which is awkward and non-idiomatic. Updated this to use a plain old filter instead.

Left to do

  • Document how to build a custom integration
  • Fix an issue with image URLs in WPML integration
  • Fix potential issues in test-timber-meta-deprecated.php
  • Not sure what Timber\Integrations\Command is for - I haven't dug into that code yet. But it's not one of the ones that was previously initialized in Integrations::__construct()...

Impact

Cleaner internal code, easier external integrations.

Usage Changes

This wasn't an external API until now, so usage changes will be covered by the introduction of docs for this new API.

Considerations

Please look carefully at the WPML integration and how it's tested. I'm less confident in that than I am in the others, because WPML has a bigger scope.

Testing

Reworked tests to use the new API. Tests were mostly unaffected.

Coby Tamayo added 5 commits January 13, 2021 11:33
This switches from using the public, static CoAuthorsPlusIntegration::$prefer_gravatar
variable to using a basic custom filter for this part of the API. This is more
consistent with WP World in general.
@acobster acobster self-assigned this Jan 13, 2021
@acobster acobster marked this pull request as draft January 13, 2021 22:03
@acobster
Copy link
Collaborator Author

@gchtr the image resize test that's failing now (vendor/bin/phpunit --group integrations) is confusing to me. See my most recent commit. It seems to have to do with the "sideloaded" images that I believe you were working on recently - can you take a look?

@gchtr
Copy link
Member

gchtr commented Jan 14, 2021

@gchtr the image resize test that's failing now (vendor/bin/phpunit --group integrations) is confusing to me. See my most recent commit. It seems to have to do with the "sideloaded" images that I believe you were working on recently - can you take a look?

That was a missing ICL_LANGUAGE_CODE constant for the WPML test. I hope that works now.

I moved all WPML-related tests to a new tests file that matches other integration tests. But I guess some things could still be moved around. We could probably use a setUp() function for the ICL_LANGUAGE_CODE constant, because that is a constant that is always set in a WPML environment.

@acobster
Copy link
Collaborator Author

We could probably use a setUp() function for the ICL_LANGUAGE_CODE constant, because that is a constant that is always set in a WPML environment.

Couple quick thoughts -

  1. bootstrap.php is probably the better place for constants
  2. If the constant is always set in a WPML environment, then the defined check will never fail, and we don't have a way to test the other code path (i.e. when ICL_LANGUAGE_CODE is not defined) anyway. Should we just remove that check?

@gchtr
Copy link
Member

gchtr commented Jan 15, 2021

@acobster Yeah, probably bootstrap.php is a better place. Since we don’t actually install WPML when testing, setting that constant should work fine and then we wouldn’t need the defined() check.

* define ICL_LANGUAGE_CODE in test bootstrap
* inline WPML directory setup (only used once)
@acobster acobster marked this pull request as ready for review January 15, 2021 18:55
@acobster
Copy link
Collaborator Author

@gchtr thanks for taking a look and consolidating those tests! Still not sure why the meta tests are failing. Also, please take a look at the new comments in test-timber-user.php - the tests pass now but if I add those same filters in a setUp method they fail, which indicates some implicit dependency to me. Haven't figure it out yet. I'd prefer to do it in setUp() because A.) it's cleaner, and B.) it uses the default Timber\User class across the board. Currently it's a CoAuthorsPlusUser instance, since the integration is global.

@gchtr
Copy link
Member

gchtr commented Jan 17, 2021

🤔 It works for me if I use setUp(), including parent::setUp():

function setUp() {
    parent::setUp();

    $this->add_filter_temporarily('timber/user/classmap', function() {
        return User::class;
    });
}

@acobster
Copy link
Collaborator Author

Oh, duh! I was forgetting to call parent 😛

@gchtr
Copy link
Member

gchtr commented Jan 18, 2021

🙈😅 Sometimes it’s that, sometimes it’s the order in which tests are called, sometimes it’s something else. No worries!

@jarednova
Copy link
Member

Oh, duh! I was forgetting to call parent 😛

I just banged my head against a wall for 30 minutes on #2413 on this. @acobster is this something you see as a required 2.0 element? or something that belongs in the 2.1 camp?

@acobster acobster added the 2.0 label Jan 20, 2021
@acobster
Copy link
Collaborator Author

I think 2.0 because we will need to refactor some of the ACF/meta stuff on top of this, and that stuff has at least one breaking change.

@acobster
Copy link
Collaborator Author

I will get around to fixing those User tests soon and then I think this will be solid enough to merge in. The meta tests that are failing will probably need to be revisited anyway as we change the API/integration with ACF etc.

Copy link
Member

@gchtr gchtr left a comment

Choose a reason for hiding this comment

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

@gchtr work came knocking for me too, sorry for falling off the face of the Earth!

Same here :)

I quickly checked and found two things that look like stray debugging code (in separate comments).

And what’s the status of the following "Left to do" list?

Left to do

* Document how to build a custom integration

* Fix an image with image URLs in WPML integration

* Fix potential issues in `test-timber-meta-deprecated.php`

* Not sure what `Timber\Integrations\Command` is for - I haven't dug into that code yet. But it's not one of the ones that was previously initialized in `Integrations::__construct()`...

I think an Integration guide would be great. I’ll add it to my list of things to improve, but can’t guarantee when I can work on it 😅.

lib/ImageHelper.php Outdated Show resolved Hide resolved
lib/Timber.php Outdated Show resolved Hide resolved
@acobster
Copy link
Collaborator Author

@gchtr thanks for looking! Those have been cleaned up.

  • Document how to build a custom integration

Guide added!

  • Fix an image with image URLs in WPML integration

I think this had to do with that leftover debug statement and is now fixed.

  • Fix potential issues in test-timber-meta-deprecated.php

Still running into issues where the tests expecting specific meta values are seeing null instead. Repro: vendor/bin/phpunit tests/test-timber-meta-deprecated.php

  • Not sure what Timber\Integrations\Command is for - I haven't dug into that code yet. But it's not one of the ones that was previously initialized in Integrations::__construct()...

I looked and that's just the WP-CLI integration. It's not really an "integration" in the same sense (WP-CLI is not a plugin), although we could treat it as such:

class WpCliIntegration implements IntegrationInterface {
  public function should_init() {
    return defined( 'WP_CLI' ) && WP_CLI;
  }

  public function init() {
    WP_CLI::add_command( 'timber', TimberCommand::class );
  }
}

I feel like it's fine to consider this out of scope here though. Command isn't used anywhere currently.

@gchtr
Copy link
Member

gchtr commented Jul 12, 2021

  • Fix potential issues in test-timber-meta-deprecated.php

Still running into issues where the tests expecting specific meta values are seeing null instead. Repro: vendor/bin/phpunit tests/test-timber-meta-deprecated.php

I found the issue and fixed it in 5560141 (#2405). Classic "hook setup in a class" bug. If you want to make filters removable, you can’t use $this when adding the filter, because the filter will be added on the instance. If you want to remove the filter, you’ll have to get the exact instance.

That’s why the filters of the ACF integration were probably set up with static classes in the first place. I reverted it to what it was before, because that seemed to be the easiest change to fix this.

Another way to solve this would be to save the result of the initialized integrations in the Timber\Timber class and add a get_integrations() method that can be used to get the very instance we would have to use to remove the filters. But this is probably not needed for a couple of deprecated tests that will be removed with the next major version.

  • Document how to build a custom integration

Guide added!

Cool! I looked over it and saw that you added it in the /docs/v1 folder. I moved it to the v2 folder :).

And I added a note in the Upgrade Guide.

# Conflicts:
#	lib/Integration/AcfIntegration.php
#	tests/test-timber-integration-acf.php
@jarednova
Copy link
Member

Hmmmm, it seems there's a failing test we'll need to resolve regarding how post meta data is stored/set ...

1) TestTimberMeta::testNonNullReturnInPreMetaFilterDisablesDatabaseFetch
Failed asserting that true matches expected false.

Error: /home/runner/work/timber/timber/tests/test-timber-meta.php:182

https://github.com/timber/timber/blob/feature/integrations-api/tests/test-timber-meta.php#L182

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.2%) to 89.963% when pulling 413bb1b on feature/integrations-api into c864b53 on 2.x.

@gchtr
Copy link
Member

gchtr commented Sep 28, 2021

@jarednova I fixed the failing test in 17f1761 (#2405). Apart from the decreased coverage, I think this one is good to merge.

@jarednova jarednova merged commit 1714b01 into 2.x Sep 28, 2021
@jarednova jarednova deleted the feature/integrations-api branch September 28, 2021 19:53
@gchtr gchtr mentioned this pull request Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants