PluginProbe
OttoKit: All-in-One Automation Platform / trunk
OttoKit: All-in-One Automation Platform vtrunk
1.1.38 1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 All 124 releases
suretriggers / src / Integrations / wordpress / triggers / create-post.php
create-post.php
141 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CreatePost.
4 * php version 5.6
5 *
6 * @category CreatePost
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\Wordpress\Triggers;
15
16 use SureTriggers\Controllers\AutomationController;
17 use SureTriggers\Integrations\WordPress\WordPress;
18 use SureTriggers\Traits\SingletonLoader;
19 use WP_Post;
20
21 if ( ! class_exists( 'CreatePost' ) ) :
22
23 /**
24 * CreatePost
25 *
26 * @category CreatePost
27 * @package SureTriggers
28 * @author BSF <username@example.com>
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
30 * @link https://www.brainstormforce.com/
31 * @since 1.0.0
32 */
33 class CreatePost {
34
35 /**
36 * Integration type.
37 *
38 * @var string
39 */
40 public $integration = 'WordPress';
41
42 /**
43 * Trigger name.
44 *
45 * @var string
46 */
47 public $trigger = 'post_created';
48
49 use SingletonLoader;
50
51 /**
52 * Constructor
53 *
54 * @since 1.0.0
55 */
56 public function __construct() {
57 add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] );
58 }
59
60 /**
61 * Register action.
62 *
63 * @param array $triggers trigger data.
64 * @return array
65 */
66 public function register( $triggers ) {
67
68 $triggers[ $this->integration ][ $this->trigger ] = [
69 'label' => __( 'User creates a post', 'suretriggers' ),
70 'action' => $this->trigger,
71 'common_action' => 'transition_post_status',
72 'function' => [ $this, 'trigger_listener' ],
73 'priority' => 10,
74 'accepted_args' => 3,
75 ];
76
77 return $triggers;
78 }
79
80 /**
81 * Trigger listener.
82 *
83 * @param string $new_status new status.
84 * @param string $old_status old status.
85 * @param WP_Post $post post.
86 * @return void
87 */
88 public function trigger_listener( $new_status, $old_status, $post ) {
89 if ( ! $post instanceof WP_Post ) {
90 return;
91 }
92
93 if ( 'publish' === $new_status && ( 'draft' === $old_status || 'auto-draft' === $old_status || 'new' === $old_status ) ) {
94
95 if ( wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) ) {
96 return;
97 }
98
99 $user_id = ap_get_current_user_id();
100 $context = WordPress::get_post_context( $post->ID );
101 $context['permalink'] = get_permalink( $post->ID );
102 $featured_image = wp_get_attachment_image_src( (int) get_post_thumbnail_id( $post->ID ), 'full' );
103
104 if ( ! empty( $featured_image ) && is_array( $featured_image ) ) {
105 $context['featured_image'] = $featured_image[0];
106 } else {
107 $context['featured_image'] = $featured_image;
108 }
109
110 $taxonomies = get_object_taxonomies( $post, 'objects' );
111 if ( ! empty( $taxonomies ) && is_array( $taxonomies ) ) {
112 foreach ( $taxonomies as $taxonomy => $taxonomy_object ) {
113 $terms = get_the_terms( $post->ID, $taxonomy );
114 if ( ! empty( $terms ) && is_array( $terms ) ) {
115 foreach ( $terms as $term ) {
116 $context[ $taxonomy ] = $term->name;
117 }
118 }
119 }
120 }
121
122 $context = array_merge( $context, WordPress::get_user_context( $user_id ) );
123 $context['post'] = $post->ID;
124 $context['post_type'] = $post->post_type;
125 $custom_metas = get_post_meta( $post->ID );
126 $context['custom_metas'] = $custom_metas;
127
128 AutomationController::sure_trigger_handle_trigger(
129 [
130 'trigger' => $this->trigger,
131 'context' => $context,
132 ]
133 );
134 }
135 }
136 }
137
138 CreatePost::get_instance();
139
140 endif;
141