Skip to content

Commit a2d6de2

Browse files
committed
Merge pull request #35 from Upstatement/trace_scheduled_posts
Trace scheduled posts
2 parents cbcf316 + 2159c6a commit a2d6de2

File tree

4 files changed

+121
-40
lines changed

4 files changed

+121
-40
lines changed

includes/class-stream-manager-admin.php

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @package StreamManagerAdmin
1414
* @author Chris Voll + Upstatement
1515
*/
16+
1617
class StreamManagerAdmin {
1718

1819
/**
@@ -74,10 +75,6 @@ private function __construct() {
7475
// Saving Streams
7576
add_action( 'save_post', array( $this, 'save_stream' ) );
7677

77-
// Saving Posts (= updating streams)
78-
add_action( 'transition_post_status', array( $this, 'on_save_post' ), 10, 3 );
79-
80-
8178
// AJAX Helpers
8279
// ------------
8380

@@ -391,42 +388,6 @@ public static function parse_terms( $taxonomy, $terms, $return_objects = false )
391388
}
392389

393390

394-
/**
395-
* Update streams whenever any post status is changed
396-
*
397-
* @since 1.0.0
398-
*
399-
* @param string $new new post status
400-
* @param string $old old post status
401-
* @param object $post WordPress post object
402-
*/
403-
public function on_save_post( $new, $old, $post ) {
404-
if ( $post->post_type == 'sm_stream' ) return;
405-
406-
if ( $old == 'publish' && $new != 'publish' ) {
407-
// Remove from streams
408-
$streams = $this->plugin->get_streams();
409-
foreach ( $streams as $stream ) {
410-
$stream->remove_post( $post->ID );
411-
}
412-
}
413-
414-
if ( $old != 'publish' && $new == 'publish' ) {
415-
//seems weird, but it's necessary for ACF
416-
//and potentially other plugins
417-
//we can't be sure what actions have been added
418-
//so checking for infinite loop-type bugs isn't possible
419-
do_action('save_post', $post->ID, $post, true );
420-
remove_all_actions('save_post');
421-
// Add to streams
422-
$streams = $this->plugin->get_streams();
423-
foreach ( $streams as $stream ) {
424-
$stream->insert_post( $post->ID );
425-
}
426-
}
427-
}
428-
429-
430391
/**
431392
* Add help text to stream edit page
432393
*
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Stream Manager.
4+
*
5+
* @package StreamManagerManager
6+
* @author Upstatement
7+
* @license GPL-2.0+
8+
* @link http://upstatement.com
9+
* @since 1.1
10+
* @copyright 2015 Upstatement
11+
*/
12+
13+
class StreamManagerManager {
14+
15+
/**
16+
* Instance of this class.
17+
*
18+
* @since 1.0.0
19+
*
20+
* @var object
21+
*/
22+
protected static $instance = null;
23+
24+
/**
25+
* Instance of StreamManager.
26+
*
27+
* @since 1.0.0
28+
*
29+
* @var object
30+
*/
31+
public $plugin = null;
32+
33+
/**
34+
* Initialize the plugin
35+
*
36+
* @since 1.1.0
37+
*/
38+
private function __construct() {
39+
$this->plugin = StreamManager::get_instance();
40+
$this->plugin_slug = $this->plugin->get_plugin_slug();
41+
$this->post_type_slug = $this->plugin->get_post_type_slug();
42+
43+
// Saving Posts (= updating streams)
44+
add_action( 'transition_post_status', array( $this, 'on_save_post' ), 10, 3 );
45+
add_action( 'publish_future_post', function($post_id) {
46+
$post = get_post($post_id);
47+
$this->on_save_post('publish', 'future', $post);
48+
}, 10, 1);
49+
50+
}
51+
52+
/**
53+
* Return an instance of this class.
54+
*
55+
* @since 1.1.0
56+
*
57+
* @return object A single instance of this class.
58+
*/
59+
public static function get_instance() {
60+
61+
// If the single instance hasn't been set, set it now.
62+
if ( null == self::$instance ) {
63+
self::$instance = new self;
64+
}
65+
66+
return self::$instance;
67+
}
68+
69+
70+
/**
71+
* Update streams whenever any post status is changed
72+
*
73+
* @since 1.1.0
74+
*
75+
* @param string $new new post status
76+
* @param string $old old post status
77+
* @param object $post WordPress post object
78+
*/
79+
public function on_save_post( $new, $old, $post ) {
80+
if ( $post->post_type == 'sm_stream' ) return;
81+
82+
if ( $old == 'publish' && $new != 'publish' ) {
83+
// Remove from streams
84+
$streams = $this->plugin->get_streams();
85+
foreach ( $streams as $stream ) {
86+
$stream->remove_post( $post->ID );
87+
}
88+
}
89+
90+
if ( $old != 'publish' && $new == 'publish' ) {
91+
//seems weird, but it's necessary for ACF
92+
//and potentially other plugins
93+
//we can't be sure what actions have been added
94+
//so checking for infinite loop-type bugs isn't possible
95+
do_action('save_post', $post->ID, $post, true );
96+
remove_all_actions('save_post');
97+
// Add to streams
98+
$streams = $this->plugin->get_streams();
99+
foreach ( $streams as $stream ) {
100+
$stream->insert_post( $post->ID );
101+
}
102+
}
103+
}
104+
105+
106+
}

stream-manager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@
5555

5656
require_once( plugin_dir_path( __FILE__ ) . 'includes/class-stream-manager.php' );
5757
require_once( plugin_dir_path( __FILE__ ) . 'includes/timber-stream.php' );
58+
require_once( plugin_dir_path( __FILE__ ) . 'includes/class-stream-manager-manager.php');
59+
5860

5961
add_action( 'plugins_loaded', array( 'StreamManager', 'get_instance' ) );
62+
add_action( 'plugins_loaded', array( 'StreamManagerManager', 'get_instance' ) );
6063

6164

6265
////////////////////////////////////////////

tests/test-stream-manager-integration.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,15 @@ function testBasicStream() {
1717
$this->assertEquals($count, count($posts));
1818
}
1919

20+
function testPostPublish() {
21+
$stream = $this->buildStream();
22+
$this->buildPosts(4);
23+
$posts = $stream->get_posts();
24+
$this->assertEquals(4, count($posts));
25+
$post_id = $this->factory->post->create(array('post_status' => 'draft'));
26+
wp_publish_post($post_id);
27+
$posts = $stream->get_posts(array('post_type' => 'post'));
28+
$this->assertEquals(5, count($posts));
29+
}
30+
2031
}

0 commit comments

Comments
 (0)