| 1 |
<?php |
| 2 |
/** |
| 3 |
* Publish Post |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WordPress\Triggers\Publish_Post |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_WordPress_Publish_Post extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize the trigger |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
public function __construct( $integration ) { |
| 20 |
|
| 21 |
$this->integration = $integration; |
| 22 |
$this->trigger = $integration . '_publish_post'; |
| 23 |
|
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the trigger |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public function register() { |
| 34 |
|
| 35 |
automatorwp_register_trigger( $this->trigger, array( |
| 36 |
'integration' => $this->integration, |
| 37 |
'label' => __( 'User publishes a post', 'automatorwp' ), |
| 38 |
'select_option' => __( 'User publishes <strong>a post</strong>', 'automatorwp' ), |
| 39 |
/* translators: %1$s: Number of times. */ |
| 40 |
'edit_label' => sprintf( __( 'User publishes a post %1$s time(s)', 'automatorwp' ), '{times}' ), |
| 41 |
'log_label' => __( 'User publishes a post', 'automatorwp' ), |
| 42 |
'action' => 'transition_post_status', |
| 43 |
'function' => array( $this, 'listener' ), |
| 44 |
'priority' => 10, |
| 45 |
'accepted_args' => 3, |
| 46 |
'options' => array( |
| 47 |
'times' => automatorwp_utilities_times_option(), |
| 48 |
), |
| 49 |
'tags' => array_merge( |
| 50 |
automatorwp_utilities_post_tags(), |
| 51 |
automatorwp_utilities_times_tag() |
| 52 |
) |
| 53 |
) ); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Trigger listener |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* |
| 62 |
* @param string $new_status The new post status |
| 63 |
* @param string $old_status The old post status |
| 64 |
* @param WP_Post $post The post |
| 65 |
*/ |
| 66 |
public function listener( $new_status, $old_status, $post ) { |
| 67 |
|
| 68 |
// Bail if not is a post |
| 69 |
if( $post->post_type !== 'post' ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
// Bail if post has been already published |
| 74 |
if( $old_status === 'publish' ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
// Bail if post is not published |
| 79 |
if( $new_status !== 'publish' ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
automatorwp_trigger_event( array( |
| 84 |
'trigger' => $this->trigger, |
| 85 |
'user_id' => $post->post_author, |
| 86 |
'post_id' => $post->ID, |
| 87 |
) ); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
new AutomatorWP_WordPress_Publish_Post( 'wordpress' ); |
| 94 |
new AutomatorWP_WordPress_Publish_Post( 'posts' ); |