Use a different hook for post notifications

[MAILPOET-1638]
This commit is contained in:
Pavel Dohnal
2018-11-27 16:01:09 +01:00
parent 0e4de4197d
commit 5db3536ded
4 changed files with 25 additions and 12 deletions

View File

@@ -167,12 +167,10 @@ class Hooks {
} }
function setupPostNotifications() { function setupPostNotifications() {
foreach(WPPosts::getTypes() as $post_type) { add_action(
add_filter( 'transition_post_status',
'publish_' . $post_type, '\MailPoet\Newsletter\Scheduler\Scheduler::transitionHook',
'\MailPoet\Newsletter\Scheduler\Scheduler::schedulePostNotification', 10, 3
10, 1 );
);
}
} }
} }

View File

@@ -12,6 +12,7 @@ use MailPoet\Models\ScheduledTask;
use MailPoet\Models\SendingQueue; use MailPoet\Models\SendingQueue;
use MailPoet\Tasks\Sending as SendingTask; use MailPoet\Tasks\Sending as SendingTask;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
use MailPoet\WP\Posts;
class Scheduler { class Scheduler {
const SECONDS_IN_HOUR = 3600; const SECONDS_IN_HOUR = 3600;
@@ -24,6 +25,22 @@ class Scheduler {
const INTERVAL_MONTHLY = 'monthly'; const INTERVAL_MONTHLY = 'monthly';
const INTERVAL_NTHWEEKDAY = 'nthWeekDay'; const INTERVAL_NTHWEEKDAY = 'nthWeekDay';
static function transitionHook($new_status, $old_status, $post) {
Logger::getLogger('post-notifications')->addInfo(
'transition post notification hook initiated',
[
'post_id' => $post->ID,
'new_status' => $new_status,
'old_status' => $old_status,
]
);
$types = Posts::getTypes();
if(($new_status !== 'publish') || !isset($types[$post->post_type])) {
return;
}
self::schedulePostNotification($post->ID);
}
static function schedulePostNotification($post_id) { static function schedulePostNotification($post_id) {
Logger::getLogger('post-notifications')->addInfo( Logger::getLogger('post-notifications')->addInfo(
'schedule post notification hook', 'schedule post notification hook',

View File

@@ -8,8 +8,6 @@ class HooksTest extends \MailPoetTest {
function testItHooksSchedulerToMultiplePostTypes() { function testItHooksSchedulerToMultiplePostTypes() {
$hooks = new Hooks(); $hooks = new Hooks();
$hooks->setupPostNotifications(); $hooks->setupPostNotifications();
foreach(WPPosts::getTypes() as $post_type) { expect(has_filter('transition_post_status', '\MailPoet\Newsletter\Scheduler\Scheduler::transitionHook'))->notEmpty();
expect(has_filter('publish_' . $post_type, '\MailPoet\Newsletter\Scheduler\Scheduler::schedulePostNotification'))->notEmpty();
}
} }
} }

View File

@@ -815,7 +815,7 @@ class SchedulerTest extends \MailPoetTest {
foreach(WPPosts::getTypes() as $post_type) { foreach(WPPosts::getTypes() as $post_type) {
remove_filter( remove_filter(
'publish_' . $post_type, 'publish_' . $post_type,
'\MailPoet\Newsletter\Scheduler\Scheduler::schedulePostNotification', '\MailPoet\Newsletter\Scheduler\Scheduler::transitionHook',
10, 1 10, 1
); );
} }