From e00d71b781b8bd49bede2fff090cf612b328db5e Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 5 Apr 2022 16:23:13 -0300 Subject: [PATCH] Remove WP < 4.5.0 compatibility code This commit removes the method Posts::getTerms() as it existed to add a compatibility layer for sites running WP < 4.5.0 and we don't support this version anymore. The signature of the WP get_terms() function was changed in version 4.5.0 and that is why this compatibility layer was needed. The integration test class for this method was also removed. The only place where this method was used, AutomatedLatestContent::getTerms(), now call WPFunctions->getTerms() directly. A very basic integration test was added to cover the happy path of AutomatedLatestContent::getTerms(). [MAILPOET-4225] --- .../API/JSON/v1/AutomatedLatestContent.php | 4 +- mailpoet/lib/WP/Posts.php | 12 ---- .../JSON/v1/AutomatedLatestContentTest.php | 24 ++++++-- mailpoet/tests/unit/WP/PostsTest.php | 60 ------------------- 4 files changed, 22 insertions(+), 78 deletions(-) delete mode 100644 mailpoet/tests/unit/WP/PostsTest.php diff --git a/mailpoet/lib/API/JSON/v1/AutomatedLatestContent.php b/mailpoet/lib/API/JSON/v1/AutomatedLatestContent.php index dc48d2c237..13956bb0c8 100644 --- a/mailpoet/lib/API/JSON/v1/AutomatedLatestContent.php +++ b/mailpoet/lib/API/JSON/v1/AutomatedLatestContent.php @@ -59,8 +59,8 @@ class AutomatedLatestContent extends APIEndpoint { 'order' => 'ASC', ]; - $args = $this->wp->applyFilters('mailpoet_search_terms_args', $args); - $terms = WPPosts::getTerms($args); + $args = (array)$this->wp->applyFilters('mailpoet_search_terms_args', $args); + $terms = WPFunctions::get()->getTerms($args); return $this->successResponse(array_values($terms)); } diff --git a/mailpoet/lib/WP/Posts.php b/mailpoet/lib/WP/Posts.php index 1d89a93771..5a809de4c7 100644 --- a/mailpoet/lib/WP/Posts.php +++ b/mailpoet/lib/WP/Posts.php @@ -5,18 +5,6 @@ namespace MailPoet\WP; use MailPoet\WP\Functions as WPFunctions; class Posts { - public static function getTerms($args) { - // Since WordPress 4.5.0 signature of get_terms changed to require - // one argument array, where taxonomy is key of that array - if (version_compare(WPFunctions::get()->getBloginfo('version'), '4.5.0', '>=')) { - return WPFunctions::get()->getTerms($args); - } else { - $taxonomy = $args['taxonomy']; - unset($args['taxonomy']); - return WPFunctions::get()->getTerms($taxonomy, $args); - } - } - public static function getTypes($args = [], $output = 'names', $operator = 'and') { $defaults = [ 'exclude_from_search' => false, diff --git a/mailpoet/tests/integration/API/JSON/v1/AutomatedLatestContentTest.php b/mailpoet/tests/integration/API/JSON/v1/AutomatedLatestContentTest.php index b7323ed62e..450b3fa864 100644 --- a/mailpoet/tests/integration/API/JSON/v1/AutomatedLatestContentTest.php +++ b/mailpoet/tests/integration/API/JSON/v1/AutomatedLatestContentTest.php @@ -2,12 +2,21 @@ namespace MailPoet\Test\API\JSON\v1; +use MailPoet\API\JSON\SuccessResponse; use MailPoet\API\JSON\v1\AutomatedLatestContent; class AutomatedLatestContentTest extends \MailPoetTest { + /** @var AutomatedLatestContent */ + private $endpoint; + + public function _before() { + parent::_before(); + + $this->endpoint = $this->diContainer->get(AutomatedLatestContent::class); + } + public function testItGetsPostTypes() { - $endpoint = $this->diContainer->get(AutomatedLatestContent::class); - $response = $endpoint->getPostTypes(); + $response = $this->endpoint->getPostTypes(); expect($response->data)->notEmpty(); foreach ($response->data as $postType) { expect($postType)->count(2); @@ -17,8 +26,7 @@ class AutomatedLatestContentTest extends \MailPoetTest { } public function testItDoesNotGetPostTypesExludedFromSearch() { - $endpoint = $this->diContainer->get(AutomatedLatestContent::class); - $response = $endpoint ->getPostTypes(); + $response = $this->endpoint ->getPostTypes(); // WP's default post type 'revision' is excluded from search // https://codex.wordpress.org/Post_Types $revisionPostType = get_post_type_object('revision'); @@ -26,4 +34,12 @@ class AutomatedLatestContentTest extends \MailPoetTest { expect($revisionPostType->exclude_from_search)->true(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps expect(isset($response->data['revision']))->false(); } + + public function testItGetTerms() { + $response = $this->endpoint->getTerms(); + + $this->assertInstanceOf(SuccessResponse::class, $response); + $this->assertCount(1, $response->data); + $this->assertSame('Uncategorized', $response->data['0']->name); + } } diff --git a/mailpoet/tests/unit/WP/PostsTest.php b/mailpoet/tests/unit/WP/PostsTest.php deleted file mode 100644 index c69e1eca91..0000000000 --- a/mailpoet/tests/unit/WP/PostsTest.php +++ /dev/null @@ -1,60 +0,0 @@ - 'post_tags', - 'hide_empty' => true, - ]; - - WPFunctions::set(Stub::make(new WPFunctions, [ - 'getBloginfo' => function($key) { - return '4.6.0'; - }, - 'getTerms' => function($key) { - return [ - 'call check' => 'get_terms called', - 'arguments' => func_get_args(), - ]; - }, - ])); - - $result = Posts::getTerms($args); - expect($result['call check'])->equals('get_terms called'); - expect($result['arguments'][0])->equals($args); - } - - public function testGetTermsPassesTaxonomyAsFirstArgumentInOldVersions() { - $args = [ - 'taxonomy' => 'post_tags', - 'hide_empty' => true, - ]; - - WPFunctions::set(Stub::make(new WPFunctions, [ - 'getBloginfo' => function($key) { - return '4.4.0'; - }, - 'getTerms' => function($key) { - return [ - 'call check' => 'get_terms called', - 'arguments' => func_get_args(), - ]; - }, - ])); - - $result = Posts::getTerms($args); - expect($result['call check'])->equals('get_terms called'); - expect($result['arguments'][0])->equals($args['taxonomy']); - expect($result['arguments'][1])->equals(array_diff_key($args, ['taxonomy' => ''])); - } - - public function _after() { - WPFunctions::set(new WPFunctions); - } -}