| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Achievement |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\GamiPress\Actions\User_Achievement |
| 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_GamiPress_User_Achievement extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
public $integration = 'gamipress'; |
| 15 |
public $action = 'gamipress_user_achievement'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_action( $this->action, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'Award achievement to user', 'automatorwp' ), |
| 27 |
'select_option' => __( 'Award <strong>achievement</strong> to user', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Post title. %2$s: User. */ |
| 29 |
'edit_label' => sprintf( __( 'Award the achievement %1$s to %2$s', 'automatorwp' ), '{post}', '{user}' ), |
| 30 |
/* translators: %1$s: Post title. %2$s: User. */ |
| 31 |
'log_label' => sprintf( __( 'Award the achievement %1$s to %2$s', 'automatorwp' ), '{post}', '{user}' ), |
| 32 |
'options' => array( |
| 33 |
'post' => automatorwp_gamipress_utilities_post_option( array( |
| 34 |
'name' => __( 'Achievement:', 'automatorwp' ), |
| 35 |
'option_default' => __( 'Select an achievement', 'automatorwp' ), |
| 36 |
'option_none' => false, |
| 37 |
'option_custom' => true, |
| 38 |
'option_custom_desc' => __( 'Achievement ID', 'automatorwp' ), |
| 39 |
'post_type_cb' => 'gamipress_get_achievement_types_slugs' |
| 40 |
) ), |
| 41 |
'user' => array( |
| 42 |
'from' => 'user', |
| 43 |
'default' => __( 'user', 'automatorwp' ), |
| 44 |
'fields' => array( |
| 45 |
'user' => array( |
| 46 |
'name' => __( 'User ID:', 'automatorwp' ), |
| 47 |
'desc' => __( 'User ID that will receive this achievement. Leave blank to award the achievement to the user that completes the automation.', 'automatorwp' ), |
| 48 |
'type' => 'text', |
| 49 |
'default' => '' |
| 50 |
), |
| 51 |
) |
| 52 |
), |
| 53 |
), |
| 54 |
) ); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Action execution function |
| 60 |
* |
| 61 |
* @since 1.0.0 |
| 62 |
* |
| 63 |
* @param stdClass $action The action object |
| 64 |
* @param int $user_id The user ID |
| 65 |
* @param array $action_options The action's stored options (with tags already passed) |
| 66 |
* @param stdClass $automation The action's automation object |
| 67 |
*/ |
| 68 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 69 |
|
| 70 |
// Shorthand |
| 71 |
$post_id = absint( $action_options['post'] ); |
| 72 |
$user_id_to_award = absint( $action_options['user'] ); |
| 73 |
|
| 74 |
if( $user_id_to_award === 0 ) { |
| 75 |
$user_id_to_award = $user_id; |
| 76 |
} |
| 77 |
|
| 78 |
$user = get_userdata( $user_id_to_award ); |
| 79 |
|
| 80 |
// Bail if user does not exists |
| 81 |
if( ! $user ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$post = get_post( $post_id ); |
| 86 |
|
| 87 |
// Bail if post doesn't exists |
| 88 |
if( ! $post ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Bail if post is not an achievement |
| 93 |
if( ! in_array( $post->post_type, gamipress_get_achievement_types_slugs() ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// Award the achievement |
| 98 |
gamipress_award_achievement_to_user( $post_id, $user_id_to_award ); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
new AutomatorWP_GamiPress_User_Achievement(); |