| 1 |
<?php |
| 2 |
/** |
| 3 |
* WordPress Core Triggers |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Triggers; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class WordPress_Core |
| 16 |
*/ |
| 17 |
class WordPress_Core extends Trigger { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'wordpress'; |
| 24 |
$this->group = __( 'WordPress', 'wpbot-automator' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get available sub-triggers |
| 29 |
*/ |
| 30 |
public static function get_sub_triggers() { |
| 31 |
return array( |
| 32 |
'wp_user_register' => array( |
| 33 |
'title' => __( 'User Registers', 'wpbot-automator' ), |
| 34 |
'hook' => 'user_register', |
| 35 |
), |
| 36 |
'wp_user_login' => array( |
| 37 |
'title' => __( 'User Logs In', 'wpbot-automator' ), |
| 38 |
'hook' => 'wp_login', |
| 39 |
), |
| 40 |
'wp_user_logout' => array( |
| 41 |
'title' => __( 'User Logs Out', 'wpbot-automator' ), |
| 42 |
'hook' => 'clear_auth_cookie', |
| 43 |
), |
| 44 |
'wp_post_publish' => array( |
| 45 |
'title' => __( 'Post/Page Published', 'wpbot-automator' ), |
| 46 |
'hook' => 'save_post', |
| 47 |
), |
| 48 |
'wp_comment_added' => array( |
| 49 |
'title' => __( 'Comment Added', 'wpbot-automator' ), |
| 50 |
'hook' => 'comment_post', |
| 51 |
), |
| 52 |
'wp_media_upload' => array( |
| 53 |
'title' => __( 'Media Uploaded', 'wpbot-automator' ), |
| 54 |
'hook' => 'add_attachment', |
| 55 |
), |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Register the triggers |
| 61 |
*/ |
| 62 |
public function register() { |
| 63 |
$triggers = self::get_sub_triggers(); |
| 64 |
|
| 65 |
foreach ( $triggers as $id => $config ) { |
| 66 |
add_action( $config['hook'], function( ...$args ) use ( $id ) { |
| 67 |
$this->handle_trigger( $id, $args ); |
| 68 |
}, 10, 10 ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Handle the trigger execution |
| 74 |
* |
| 75 |
* @param string $sub_id Sub-trigger ID. |
| 76 |
* @param array $args Arguments from the hook. |
| 77 |
*/ |
| 78 |
public function handle_trigger( $sub_id, $args ) { |
| 79 |
$data = array(); |
| 80 |
|
| 81 |
switch ( $sub_id ) { |
| 82 |
case 'wp_user_register': |
| 83 |
$data['user_id'] = $args[0]; |
| 84 |
$user = get_userdata( $data['user_id'] ); |
| 85 |
$data['user_email'] = $user ? $user->user_email : ''; |
| 86 |
$data['user_login'] = $user ? $user->user_login : ''; |
| 87 |
break; |
| 88 |
case 'wp_user_login': |
| 89 |
$data['user_login'] = $args[0]; |
| 90 |
$data['user'] = $args[1]; |
| 91 |
$data['user_id'] = $data['user']->ID; |
| 92 |
break; |
| 93 |
case 'wp_post_publish': |
| 94 |
$data['post_id'] = $args[0]; |
| 95 |
$data['post'] = $args[1]; |
| 96 |
$data['update'] = $args[2]; |
| 97 |
$data['post_title'] = $data['post']->post_title; |
| 98 |
$data['post_content'] = wp_strip_all_tags($data['post']->post_content); |
| 99 |
$data['post_excerpt'] = wp_strip_all_tags($data['post']->post_excerpt); |
| 100 |
$data['post_url'] = get_permalink( $data['post']->ID ); |
| 101 |
$data['featured_image_url'] = get_the_post_thumbnail_url($data['post']->ID, 'full') ?: 'Image Not found'; |
| 102 |
// Only trigger for published posts. |
| 103 |
if ( 'publish' !== get_post_status( $data['post_id'] ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
break; |
| 107 |
case 'wp_comment_added': |
| 108 |
$data['comment_id'] = $args[0]; |
| 109 |
$data['comment_approved'] = $args[1]; |
| 110 |
$data['comment_data'] = $args[2]; |
| 111 |
$data['comment_content'] = isset( $args[2]['comment_content'] ) ? $args[2]['comment_content'] : ''; |
| 112 |
$data['comment_author'] = isset( $args[2]['comment_author'] ) ? $args[2]['comment_author'] : ''; |
| 113 |
$data['comment_author_email'] = isset( $args[2]['comment_author_email'] ) ? $args[2]['comment_author_email'] : ''; |
| 114 |
break; |
| 115 |
case 'wp_media_upload': |
| 116 |
$data['attachment_id'] = $args[0]; |
| 117 |
$data['file_path'] = get_attached_file( $data['attachment_id'] ); |
| 118 |
$data['file_url'] = wp_get_attachment_url( $data['attachment_id'] ); |
| 119 |
break; |
| 120 |
} |
| 121 |
|
| 122 |
$this->run( array( |
| 123 |
'sub_id' => $sub_id, |
| 124 |
'data' => $data, |
| 125 |
) ); |
| 126 |
} |
| 127 |
} |
| 128 |
|