← All changes
|
integrations/automatorwp/triggers/post-created.php
+125
-0
5.7.6
→
6.0.4
View file →
| @@ -1,0 +1,125 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Post Created | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\AutomatorWP\Triggers\Post_Created | |
| 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_AutomatorWP_Post_Created 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 . '_post_created'; | |
| 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' => __( 'Post gets created through an automation', 'automatorwp' ), | |
| 38 | + 'select_option' => __( 'Post gets created through <strong>an automation</strong>', 'automatorwp' ), | |
| 39 | + /* translators: %1$s: Automation title. %2$s: Number of times. */ | |
| 40 | + 'edit_label' => sprintf( __( 'Post gets created through %1$s %2$s time(s)', 'automatorwp' ), '{automation}', '{times}' ), | |
| 41 | + /* translators: %1$s: Automation title. */ | |
| 42 | + 'log_label' => sprintf( __( 'Post gets created through %1$s', 'automatorwp' ), '{automation}' ), | |
| 43 | + 'action' => 'automatorwp_wordpress_create_post_executed', | |
| 44 | + 'function' => array( $this, 'listener' ), | |
| 45 | + 'priority' => 10, | |
| 46 | + 'accepted_args' => 5, | |
| 47 | + 'options' => array( | |
| 48 | + 'automation' => automatorwp_utilities_automation_option(), | |
| 49 | + 'times' => automatorwp_utilities_times_option(), | |
| 50 | + ), | |
| 51 | + 'tags' => array_merge( | |
| 52 | + automatorwp_utilities_post_tags(), | |
| 53 | + automatorwp_utilities_times_tag() | |
| 54 | + ) | |
| 55 | + ) ); | |
| 56 | + | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Trigger listener | |
| 61 | + * | |
| 62 | + * @since 1.0.0 | |
| 63 | + * | |
| 64 | + * @param int $post_id The post ID | |
| 65 | + * @param stdClass $action The action object | |
| 66 | + * @param int $user_id The user ID (user who triggered the automation) | |
| 67 | + * @param array $action_options The action's stored options (with tags already passed, included on meta keys and values) | |
| 68 | + * @param stdClass $automation The action's automation object | |
| 69 | + */ | |
| 70 | + public function listener( $post_id, $action, $user_id, $action_options, $automation ) { | |
| 71 | + | |
| 72 | + $post = get_post( $post_id ); | |
| 73 | + | |
| 74 | + automatorwp_trigger_event( array( | |
| 75 | + 'trigger' => $this->trigger, | |
| 76 | + 'post_id' => $post_id, | |
| 77 | + 'user_id' => $post->post_author, | |
| 78 | + 'automation_id' => $automation->id, | |
| 79 | + ) ); | |
| 80 | + | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * User deserves check | |
| 85 | + * | |
| 86 | + * @since 1.0.0 | |
| 87 | + * | |
| 88 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 89 | + * @param stdClass $trigger The trigger object | |
| 90 | + * @param int $user_id The user ID | |
| 91 | + * @param array $event Event information | |
| 92 | + * @param array $trigger_options The trigger's stored options | |
| 93 | + * @param stdClass $automation The trigger's automation object | |
| 94 | + * | |
| 95 | + * @return bool True if user deserves trigger, false otherwise | |
| 96 | + */ | |
| 97 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 98 | + | |
| 99 | + // Don't deserve if automation is not received | |
| 100 | + if( ! isset( $event['automation_id'] ) ) { | |
| 101 | + return false; | |
| 102 | + } | |
| 103 | + | |
| 104 | + $automation = automatorwp_get_automation_object( absint( $event['automation_id'] ) ); | |
| 105 | + | |
| 106 | + // Don't deserve if automation doesn't exists | |
| 107 | + if( ! $automation ) { | |
| 108 | + return false; | |
| 109 | + } | |
| 110 | + | |
| 111 | + $automation_id = absint( $trigger_options['automation'] ); | |
| 112 | + | |
| 113 | + // Don't deserve if automation doesn't match with the trigger option | |
| 114 | + if( $automation_id !== 0 && absint( $automation->id ) !== $automation_id ) { | |
| 115 | + return false; | |
| 116 | + } | |
| 117 | + | |
| 118 | + return $deserves_trigger; | |
| 119 | + | |
| 120 | + } | |
| 121 | + | |
| 122 | +} | |
| 123 | + | |
| 124 | +new AutomatorWP_AutomatorWP_Post_Created( 'automatorwp' ); | |
| 125 | +new AutomatorWP_AutomatorWP_Post_Created( 'posts' ); | |