| @@ -1,0 +1,125 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Delete user | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Actions\Delete_User | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>, Dionisio Sánchez <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_WordPress_Delete_User extends AutomatorWP_Integration_Action { | |
| 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->action = $integration . '_delete_user'; | |
| 23 | + | |
| 24 | + parent::__construct(); | |
| 25 | + | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Register the action | |
| 30 | + * | |
| 31 | + * @since 1.0.0 | |
| 32 | + */ | |
| 33 | + public function register() { | |
| 34 | + | |
| 35 | + automatorwp_register_action( $this->action, array( | |
| 36 | + 'integration' => $this->integration, | |
| 37 | + 'label' => __( 'Delete a user', 'automatorwp' ), | |
| 38 | + 'select_option' => __( 'Delete <strong>a user</strong>', 'automatorwp' ), | |
| 39 | + /* translators: %1$s: User. */ | |
| 40 | + 'edit_label' => sprintf( __( 'Delete a %1$s', 'automatorwp' ), '{user}' ), | |
| 41 | + /* translators: %1$s: User. */ | |
| 42 | + 'log_label' => sprintf( __( 'Delete a %1$s', 'automatorwp' ), '{user}' ), | |
| 43 | + 'options' => array( | |
| 44 | + 'user' => array( | |
| 45 | + 'default' => __ ( 'user', 'automatorwp' ), | |
| 46 | + 'fields' => array( | |
| 47 | + 'user_id' => array( | |
| 48 | + 'name' => __( 'User ID:', 'automatorwp' ), | |
| 49 | + 'desc' => __( 'The user\'s ID to delete. Leave empty to assign the user that completes the automation.', 'automatorwp' ), | |
| 50 | + 'type' => 'text', | |
| 51 | + 'default' => '' | |
| 52 | + ), | |
| 53 | + 'target_user_id' => array( | |
| 54 | + 'name' => __( 'User ID to reassign the content:', 'automatorwp' ), | |
| 55 | + 'desc' => __( 'The user\'s ID to reassign the content owned by the user that will get deleted. Leave empty to do not reassign the content.', 'automatorwp' ), | |
| 56 | + 'type' => 'text', | |
| 57 | + 'default' => '' | |
| 58 | + ), | |
| 59 | + ), | |
| 60 | + ), | |
| 61 | + | |
| 62 | + ), | |
| 63 | + ) ); | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Action execution function | |
| 69 | + * | |
| 70 | + * @since 1.0.0 | |
| 71 | + * | |
| 72 | + * @param stdClass $action The action object | |
| 73 | + * @param int $user_id The user ID | |
| 74 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 75 | + * @param stdClass $automation The action's automation object | |
| 76 | + */ | |
| 77 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 78 | + | |
| 79 | + $user_id_to_delete = absint( $action_options['user_id'] ); | |
| 80 | + $target_user_id = absint( $action_options['target_user_id'] ); | |
| 81 | + | |
| 82 | + if( $user_id_to_delete === 0 ) { | |
| 83 | + $user_id_to_delete = $user_id; | |
| 84 | + } | |
| 85 | + | |
| 86 | + $user = get_userdata( $user_id_to_delete ); | |
| 87 | + | |
| 88 | + // Bail if user to delete does not exists | |
| 89 | + if ( ! $user ) { | |
| 90 | + return; | |
| 91 | + } | |
| 92 | + | |
| 93 | + // Ensure that wp_delete_user is available | |
| 94 | + if( ! function_exists( 'wp_delete_user' ) ) { | |
| 95 | + include_once( ABSPATH . 'wp-admin/includes/user.php' ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + if ( $target_user_id !== 0 ) { | |
| 99 | + | |
| 100 | + $target_user = get_userdata( $target_user_id ); | |
| 101 | + | |
| 102 | + // Bail if user to reassign does not exists | |
| 103 | + if ( ! $target_user ) { | |
| 104 | + return; | |
| 105 | + } | |
| 106 | + | |
| 107 | + // Bail if user to reassign is equal to the user that will get deleted | |
| 108 | + if( $target_user->ID === $user->ID ) { | |
| 109 | + return; | |
| 110 | + } | |
| 111 | + | |
| 112 | + // Delete the user and reassign its content | |
| 113 | + wp_delete_user( $user->ID, $target_user->ID ); | |
| 114 | + | |
| 115 | + } else { | |
| 116 | + // Delete the user | |
| 117 | + wp_delete_user( $user->ID ); | |
| 118 | + } | |
| 119 | + | |
| 120 | + } | |
| 121 | + | |
| 122 | +} | |
| 123 | + | |
| 124 | +new AutomatorWP_WordPress_Delete_User( 'wordpress' ); | |
| 125 | +new AutomatorWP_WordPress_Delete_User( 'users' ); | |