helsingborg-stad / wpservice
Simplifies WordPress integration by providing a centralized WpService that exposes global WordPress functions in a streamlined manner. Simplify your development workflow and enhance WordPress integration with ease.
Installs: 7 142
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
Requires (Dev)
- helsingborg-stad/phpcs: ^0.3.5
- johnpbloch/wordpress: ^6.6
- php-stubs/generator: *
- php-stubs/wordpress-stubs: ^6.6
- phpunit/phpunit: ^9.6
- dev-main
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.52.0
- 1.51.0
- 1.50.0
- 1.49.0
- 1.48.0
- 1.47.0
- 1.46.0
- 1.45.0
- 1.44.4
- 1.44.2
- 1.44.1
- 1.43.0
- 1.42.0
- 1.41.0
- 1.40.6
- 1.40.5
- 1.40.2
- 1.39.0
- 1.38.0
- 1.37.1
- 1.37.0
- 1.36.0
- 1.35.0
- 1.34.1
- 1.33.2
- 1.33.0
- 1.32.2
- 1.32.1
- 1.32.0
- 1.31.2
- 1.31.0
- 1.30.0
- 1.29.0
- 1.28.0
- 1.27.0
- 1.26.0
- 1.25.2
- 1.25.1
- 1.25.0
- 1.24.0
- 1.23.0
- 1.22.2
- 1.22.1
- 1.22.0
- 1.21.0
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.1
- 1.16.0
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.3
- 1.11.2
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- dev-fix/improve-filesystem-error-message
- dev-feat/attachments-and-files
- dev-feat/getTaxonomy
- dev-feat/updatePost
- dev-feat/getPostTypeObject
- dev-feat/getObjectTaxonomies
- dev-feat/more-functions
- dev-fix/renaming-wp
- dev-wp_remote_retrieve_body
- dev-wp_remote_get
- dev-update_option
- dev-get_post_types
This package is auto-updated.
Last update: 2024-11-22 10:51:36 UTC
README
WpService
Simplifies WordPress integration by providing a centralized WpService that exposes global WordPress functions in a streamlined manner. Simplify your development workflow and enhance WordPress integration with ease.
About WpService
Enable the use of global WordPress functions in plugins and themes where applying Interface Segregation. Different flavors of the WordPress Service can be utilized by applying available decorators.
Getting Started
Installation
- Install the package via composer:
composer require helsingborg-stad/wpservice
- Use the WpService in your plugin or theme:
use WpService\Implementations\NativeWpService; $wpService = new NativeWpService(); $wpService->getTheId();
Decorators
Text Domain Decorator
Applies default text domain to translation functions.
use WpService\Implementations\WpServiceWithTextDomain; use WpService\Implementations\NativeWpService; $service = new NativeWpService(); $service = new WpServiceWithTextDomain($service, 'my-text-domain'); $service->__('Hello World'); // Will automatically use 'my-text-domain' as the text domain.
Using WpService in tests
To make it easier when testing code that depends on the WpService or parts of it, a FakeWpService implementation is available. This implementation is useful when you want to test your code without having to rely on the WordPress functions.
Example
Consider that you have the following class that utilizes a part of the WpService:
use WpService\Contracts\GetTheId; class MyService { public function __construct(private GetTheId $wpService) { } public function getCurrentPostId(): int { return $this->wpService->getTheId(); } }
You can then use FakeWpService in your tests to fake the results of the WpService as well as veifyin the calls made to the WpService functions:
use WpService\Implementations\FakeWpService; use PHPUnit\Framework\TestCase; class MyServiceTest extends TestCase { public function testGetCurrentPostId() { // Given $fakeWpService = new FakeWpService(['getTheId' => 123]); $myService = new MyService($fakeWpService); // When $postId = $myService->getCurrentPostId(); // Then $this->assertEquals([123], $wpService->methodCalls['isSingle'][0]); $this->assertEquals(123, $postId); } }
Passing return values to the FakeWpService
The FakeWpService constructor accepts an array of key-value pairs where the key is the name of the method and the value is the return value of the method.
# Using a generic return value for all calls to the method. $fakeWpService = new FakeWpService(['getTheTitle' => 'Test Title']); $fakeWpService->getTheTitle(); // Returns 'Test Title' $fakeWpService->getTheTitle(321); // Returns 'Test Title' $fakeWpService->getTheTitle(123); // Returns 'Test Title' # Using a callback to determine the return value based on the arguments passed to the method. $return = fn($postId) => $postId === 123 ? 'Test Title' : ''; $fakeWpService = new FakeWpService(['getTheId' => $return]); $fakeWpService->getTheTitle(); // Returns '' $fakeWpService->getTheTitle(321); // Returns '' $fakeWpService->getTheTitle(123); // Returns 'Test Title'
Built With
- PHP
Tests
Run tests
Run composer test
in the terminal.
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License.