| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Actions_V2\Update_User; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Actions\Action_Handler; |
| 6 |
use JFB_Modules\Actions_V2\Update_User\Properties\User_Modifier; |
| 7 |
use Jet_Form_Builder\Actions\Types\Base; |
| 8 |
use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 9 |
use Jet_Form_Builder\Admin\Pages\Pages_Manager; |
| 10 |
use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 11 |
use Jet_Form_Builder\Classes\Tools; |
| 12 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 13 |
|
| 14 |
// If this file is called directly, abort. |
| 15 |
if ( ! defined( 'WPINC' ) ) { |
| 16 |
die; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Define Base_Type class |
| 21 |
*/ |
| 22 |
class Update_User_Action extends Base { |
| 23 |
|
| 24 |
public function get_name() { |
| 25 |
return __( 'Update User', 'jet-form-builder' ); |
| 26 |
} |
| 27 |
|
| 28 |
public function get_id() { |
| 29 |
return 'update_user'; |
| 30 |
} |
| 31 |
|
| 32 |
public function action_attributes() { |
| 33 |
return array( |
| 34 |
'fields_map' => array( |
| 35 |
'default' => array(), |
| 36 |
), |
| 37 |
'user_role' => array( |
| 38 |
'default' => '', |
| 39 |
), |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @param array $request |
| 45 |
* @param Action_Handler $handler |
| 46 |
* |
| 47 |
* @return void |
| 48 |
* @throws Action_Exception |
| 49 |
*/ |
| 50 |
public function do_action( array $request, Action_Handler $handler ) { |
| 51 |
$modifier = ( new User_Modifier() ) |
| 52 |
->set_request( $request ) |
| 53 |
->set_fields_map( $this->settings['fields_map'] ?? array() ) |
| 54 |
->set_self_promotable_roles( $this->get_self_promotable_roles() ); |
| 55 |
|
| 56 |
$user_roles = isset( $this->settings['user_role'] ) ? Tools::get_array_of_user_roles( $this->settings['user_role'] ) : ''; |
| 57 |
|
| 58 |
if ( ! empty( $user_roles ) ) { |
| 59 |
$main_role = Tools::get_main_user_role_by_priority( $user_roles ); |
| 60 |
|
| 61 |
if ( ! empty( $main_role ) ) { |
| 62 |
$additional_roles = array_filter( $user_roles, fn( $r ) => $r !== $main_role ); |
| 63 |
|
| 64 |
$roles = array_merge( array( $main_role ), $additional_roles ); |
| 65 |
|
| 66 |
$modifier->set( 'role', $roles ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$modifier->run(); |
| 71 |
} |
| 72 |
|
| 73 |
public function self_script_name() { |
| 74 |
return 'jetFormUpdateUserData'; |
| 75 |
} |
| 76 |
|
| 77 |
public function editor_labels() { |
| 78 |
return array( |
| 79 |
'fields_map' => __( 'Fields Map:', 'jet-form-builder' ), |
| 80 |
'user_role' => __( 'User Role:', 'jet-form-builder' ), |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Regsiter custom action data for the editor |
| 86 |
* |
| 87 |
* @return [type] [description] |
| 88 |
*/ |
| 89 |
public function action_data() { |
| 90 |
return array( |
| 91 |
'userRoles' => Tools::get_user_roles_for_js(), |
| 92 |
'globalSelfPromotableRoles' => $this->get_self_promotable_roles(), |
| 93 |
'globalSettingsUrl' => Pages_Manager::instance()->get_stable_url( 'jfb-settings' ) . '#options-tab', |
| 94 |
'properties' => Tools::with_placeholder( |
| 95 |
Array_Tools::to_array( ( new User_Modifier() )->properties->all() ) |
| 96 |
), |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
private function get_self_promotable_roles(): array { |
| 101 |
$options = Tab_Handler_Manager::get_options( 'options-tab' ); |
| 102 |
$roles = Tools::get_array_of_user_roles( $options['self_promotable_roles'] ?? array() ); |
| 103 |
|
| 104 |
return array_values( |
| 105 |
array_filter( |
| 106 |
array_map( 'strval', $roles ) |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|