henrysmoking

wp forum help request

Sep 14th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.28 KB | None | 0 0
  1. <?php
  2.  
  3. // classe istanziata dal plugin (tolto tutto ciò che non serve)
  4.  
  5. class Nda4Axis {
  6.  
  7.     function __construct() {
  8.  
  9.         // role: admin
  10.         add_action('edit_user_profile',[self::class, "edit_user_profile"]);
  11.         add_action('edit_user_profile_update',[self::class, "save_user_profile"]);
  12.         // custom role: partner
  13.         add_action('show_user_profile',[self::class, "edit_user_profile"]);
  14.         add_action('personal_options_update',[self::class, "save_user_profile"]);
  15.     }
  16.  
  17.  
  18.     static function edit_user_profile(WP_User $user){
  19.         $utente = new NdaUser($user->ID);
  20.         if ( $utente->is_partner() ){
  21.             $utente->show_admin_user_fields();
  22.         }
  23.     }
  24.  
  25.     static function save_user_profile($user){
  26.         $utente = new NdaUser($user);
  27.         if ($utente->is_partner()){
  28.             $utente->save_admin_user_fields();
  29.         }
  30.     }
  31.  
  32. }
  33.  
  34. // classe che gestisce l'utente (tolto tutto ciò che non serve)
  35.  
  36. class NdaUser extends WP_User {
  37.  
  38.     /**
  39.      * user meta key / value
  40.      */
  41.     const META_K_NDA_ACCEPTED = '_nda_accepted';
  42.     const META_V_NDA_IS_OK = '1';
  43.     const META_V_NDA_IS_NOT_OK = "0";
  44.  
  45.     const META_K_NDA_USER_ACTIVE = '_nda_user_active';
  46.     const META_V_NDA_USER_ACTIVE_OK = '1';
  47.     const META_V_NDA_USER_ACTIVE_NOT_OK = '0';
  48.    
  49.     const META_K_NDA_AZIENDA_NOME = '_nda_azienda_nome';
  50.     const META_K_NDA_AZIENDA_INDIRIZZO = '_nda_azienda_indirizzo';
  51.  
  52.  
  53.     public function __construct($id = 0, $name = '', $blog_id = '') {
  54.         parent::__construct($id, $name, $blog_id);
  55.     }
  56.  
  57.     public function is_partner() {
  58.         if (in_array(NDA_ROLE, $this->roles)) {
  59.             return true;
  60.         }
  61.         return false;
  62.     }
  63.    
  64.     public function show_admin_user_fields() {
  65.         ?>
  66.         <h2 style='background: #ccc; padding: 5px 10px; border-radius: 2px;'><?php _e("Other Partner Info", NDA_TEXTDOMAIN);?></h2>
  67.        
  68.         <h3 style='border-bottom: 1px solid gray;'><?php _e("User status", NDA_TEXTDOMAIN);?></h3>
  69.        
  70.         <table class="form-table">
  71.             <tr>
  72.                 <th>
  73.                     <label for=""><?php _e("NDA accepted",NDA_TEXTDOMAIN);?></label>
  74.                 </th>
  75.                 <td>
  76.                     <?php echo $this->is_meta_nda_accepted() ? _e("Yes",NDA_TEXTDOMAIN) : _e("No",NDA_TEXTDOMAIN);?>
  77.                 </td>
  78.             </tr>
  79.             <tr>
  80.                 <th>
  81.                     <label for="<?php echo self::META_K_NDA_USER_ACTIVE?>"><?php _e("User status",NDA_TEXTDOMAIN);?></label>
  82.                 </th>
  83.                 <td>
  84.     <input type="checkbox" name="<?php echo self::META_K_NDA_USER_ACTIVE?>" value="<?php echo self::META_V_NDA_USER_ACTIVE_OK?>" <?php if ($this->is_meta_nda_user_active()){ echo " checked ";}?> <?php if (!is_super_admin()){echo " disabled";}?>> <?php _e("User active",NDA_TEXTDOMAIN)?>
  85.                 </td>
  86.             </tr>
  87.            
  88.         </table>
  89.        
  90.         <h3 style='border-bottom: 1px solid gray'><?php _e("Company",NDA_TEXTDOMAIN)?></h3>
  91.        
  92.         <table class="form-table">
  93.             <tr>
  94.                 <th>
  95.                     <label for="<?php self::META_K_NDA_AZIENDA_NOME?>"><?php _e('Company',NDA_TEXTDOMAIN); ?></label>
  96.                 </th>
  97.                 <td>
  98.                     <input type="text" name="<?php echo self::META_K_NDA_AZIENDA_NOME?>" id="<?php echo self::META_K_NDA_AZIENDA_NOME?>" value="<?php echo esc_attr(get_the_author_meta(self::META_K_NDA_AZIENDA_NOME,$this->ID)); ?>" class="regular-text" />
  99.                     <br><span class="description"><?php _e('Your company name.', NDA_TEXTDOMAIN); ?></span>
  100.                 </td>
  101.             </tr>
  102.             <tr>
  103.                 <th>
  104.                     <label for="<?php self::META_K_NDA_AZIENDA_INDIRIZZO?>"><?php _e('Address',NDA_TEXTDOMAIN); ?></label>
  105.                 </th>
  106.                 <td>
  107.                     <input type="text" name="<?php echo self::META_K_NDA_AZIENDA_INDIRIZZO?>" id="<?php echo self::META_K_NDA_AZIENDA_INDIRIZZO?>" value="<?php echo esc_attr(get_the_author_meta(self::META_K_NDA_AZIENDA_INDIRIZZO,$this->ID)); ?>" class="regular-text" />
  108.                     <br><span class="description"><?php _e('Complete address.', NDA_TEXTDOMAIN); ?></span>
  109.                 </td>
  110.             </tr>
  111.         </table>
  112.  
  113.         <?php
  114.     }
  115.    
  116.     public function save_admin_user_fields(){
  117.         $meta_v_nda_user_active_form_value = filter_input(INPUT_POST, self::META_K_NDA_USER_ACTIVE, FILTER_SANITIZE_STRING);
  118.         if (is_super_admin()) {
  119.             update_user_meta($this->ID, self::META_K_NDA_USER_ACTIVE, $meta_v_nda_user_active_form_value);
  120.         }
  121.         update_user_meta($this->ID, self::META_K_NDA_AZIENDA_NOME, filter_input(INPUT_POST, self::META_K_NDA_AZIENDA_NOME, FILTER_SANITIZE_STRING));
  122.         update_user_meta($this->ID, self::META_K_NDA_AZIENDA_INDIRIZZO, filter_input(INPUT_POST, self::META_K_NDA_AZIENDA_INDIRIZZO, FILTER_SANITIZE_STRING));
  123.        
  124.     }
  125.    
  126. }
Add Comment
Please, Sign In to add comment