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() {
foreach(WPPosts::getTypes() as $post_type) {
add_filter(
'publish_' . $post_type,
'\MailPoet\Newsletter\Scheduler\Scheduler::schedulePostNotification',
10, 1
);
}
add_action(
'transition_post_status',
'\MailPoet\Newsletter\Scheduler\Scheduler::transitionHook',
10, 3
);
}
}

View File

@@ -12,6 +12,7 @@ use MailPoet\Models\ScheduledTask;
use MailPoet\Models\SendingQueue;
use MailPoet\Tasks\Sending as SendingTask;
use MailPoet\WP\Functions as WPFunctions;
use MailPoet\WP\Posts;
class Scheduler {
const SECONDS_IN_HOUR = 3600;
@@ -24,6 +25,22 @@ class Scheduler {
const INTERVAL_MONTHLY = 'monthly';
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) {
Logger::getLogger('post-notifications')->addInfo(
'schedule post notification hook',

View File

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

View File

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