| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Ai; |
| 3 |
|
| 4 |
use Elementor\User; |
| 5 |
use Elementor\Utils; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Preferences { |
| 12 |
|
| 13 |
const ENABLE_AI = 'elementor_enable_ai'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Register actions and hooks. |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public function register() { |
| 21 |
add_action( 'personal_options', function ( \WP_User $user ) { |
| 22 |
$this->add_personal_options_settings( $user ); |
| 23 |
} ); |
| 24 |
|
| 25 |
add_action( 'personal_options_update', function ( $user_id ) { |
| 26 |
$this->update_personal_options_settings( $user_id ); |
| 27 |
} ); |
| 28 |
|
| 29 |
add_action( 'edit_user_profile_update', function ( $user_id ) { |
| 30 |
$this->update_personal_options_settings( $user_id ); |
| 31 |
} ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Determine if AI features are enabled for a user. |
| 36 |
* |
| 37 |
* @param int $user_id - User ID. |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public static function is_ai_enabled( $user_id ) { |
| 42 |
return (bool) User::get_user_option_with_default( static::ENABLE_AI, $user_id, true ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add settings to the "Personal Options". |
| 47 |
* |
| 48 |
* @param \WP_User $user - User object. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
protected function add_personal_options_settings( \WP_User $user ) { |
| 53 |
if ( ! $this->has_permissions_to_edit_user( $user->ID ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$ai_value = User::get_user_option_with_default( static::ENABLE_AI, $user->ID, '1' ); |
| 58 |
?> |
| 59 |
<tr> |
| 60 |
<th style="padding:0px"> |
| 61 |
<h2><?php echo esc_html__( 'Elementor - AI', 'elementor' ); ?></h2> |
| 62 |
</th> |
| 63 |
</tr> |
| 64 |
<tr> |
| 65 |
<th> |
| 66 |
<label for="<?php echo esc_attr( static::ENABLE_AI ); ?>"> |
| 67 |
<?php echo esc_html__( 'Status', 'elementor' ); ?> |
| 68 |
</label> |
| 69 |
</th> |
| 70 |
<td> |
| 71 |
<label for="<?php echo esc_attr( static::ENABLE_AI ); ?>"> |
| 72 |
<input name="<?php echo esc_attr( static::ENABLE_AI ); ?>" id="<?php echo esc_attr( static::ENABLE_AI ); ?>" type="checkbox" value="1"<?php checked( '1', $ai_value ); ?> /> |
| 73 |
<?php echo esc_html__( 'Enable Elementor AI functionality', 'elementor' ); ?> |
| 74 |
</label> |
| 75 |
</td> |
| 76 |
</tr> |
| 77 |
<?php |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Save the settings in the "Personal Options". |
| 82 |
* |
| 83 |
* @param int $user_id - User ID. |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
protected function update_personal_options_settings( $user_id ) { |
| 88 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce already verified in `wp_verify_nonce`. |
| 89 |
$wpnonce = Utils::get_super_global_value( $_POST, '_wpnonce' ); |
| 90 |
if ( ! wp_verify_nonce( $wpnonce, 'update-user_' . $user_id ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! $this->has_permissions_to_edit_user( $user_id ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$ai_value = empty( $_POST[ static::ENABLE_AI ] ) ? '0' : '1'; |
| 99 |
|
| 100 |
update_user_option( $user_id, static::ENABLE_AI, sanitize_text_field( $ai_value ) ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Determine if the current user has permission to view/change preferences of a user. |
| 105 |
* |
| 106 |
* @param int $user_id |
| 107 |
* |
| 108 |
* @return bool |
| 109 |
*/ |
| 110 |
protected function has_permissions_to_edit_user( $user_id ) { |
| 111 |
return current_user_can( 'edit_user', $user_id ); |
| 112 |
} |
| 113 |
} |
| 114 |
|