id = $id; $this->title = $title; $this->nonce = $this->id . '_nonce'; // View Additional User Fields. add_action( 'show_user_profile', array( $this, 'view' ) ); add_action( 'edit_user_profile', array( $this, 'view' ) ); // Update Additional User Fields. add_action( 'personal_options_update', array( $this, 'save' ) ); add_action( 'edit_user_profile_update', array( $this, 'save' ) ); // Load scripts. add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) ); } /** * Load user meta scripts. */ public function scripts() { // Color Picker. wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'wp-color-picker' ); // Media Upload. wp_enqueue_media(); // jQuery UI. wp_enqueue_script( 'jquery-ui-sortable' ); // user_meta. wp_enqueue_script( 'odin-admin', get_template_directory_uri() . '/core/assets/js/admin.js', array( 'jquery' ), null, true ); wp_enqueue_style( 'odin-admin', get_template_directory_uri() . '/core/assets/css/admin.css', array(), null, 'all' ); // Localize strings. wp_localize_script( 'odin-admin', 'odinAdminParams', array( 'galleryTitle' => __( 'Add images in gallery', 'avalon-b' ), 'galleryButton' => __( 'Add in gallery', 'avalon-b' ), 'galleryRemove' => __( 'Remove image', 'avalon-b' ), 'uploadTitle' => __( 'Choose a file', 'avalon-b' ), 'uploadButton' => __( 'Add file', 'avalon-b' ), ) ); } /** * Set user meta fields. * * @param array $fields User meta fields. */ public function set_fields( $fields = array() ) { $this->fields = $fields; } /** * User meta view. * * @param object $post Post object. * * @return string User meta HTML fields. */ public function view() { // Use nonce for verification. wp_nonce_field( basename( __FILE__ ), $this->nonce ); echo '

' . $this->title . '

'; echo ''; foreach ( $this->fields as $field ) { echo ''; $title = sprintf( '', $field['id'], $field['label'] ); echo $title; echo apply_filters( 'odin_user_meta_field_before_' . $this->id, '', $field ); echo ''; } echo '
', $field ); $this->process_fields( $field ); if ( isset( $field['description'] ) ) { echo sprintf( '%s', $field['description'] ); } echo apply_filters( 'odin_user_meta_field_after_' . $this->id, '
'; } /** * Process the user meta fields. * * @param array $args Field arguments * @param int $user_id ID of the current post type. * * @return string HTML of the field. */ protected function process_fields( $args ) { $id = $args['id']; $type = $args['type']; $options = isset( $args['options'] ) ? $args['options'] : ''; $attrs = isset( $args['attributes'] ) ? $args['attributes'] : array(); // Gets current value or default. if ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE ) { $current_user = wp_get_current_user(); $user_id = $current_user->ID; // If is another user's profile page } elseif ( ! empty( $_GET['user_id'] ) && is_numeric( $_GET['user_id'] ) ) { $user_id = $_GET['user_id']; } $current = get_user_meta( $user_id, $id, true ); if ( ! $current ) { $current = isset( $args['default'] ) ? $args['default'] : ''; } switch ( $type ) { case 'text': $this->field_input( $id, $current, array_merge( array( 'class' => 'regular-text' ), $attrs ) ); break; case 'input': $this->field_input( $id, $current, $attrs ); break; case 'textarea': $this->field_textarea( $id, $current, $attrs ); break; case 'checkbox': $this->field_checkbox( $id, $current, $attrs ); break; case 'select': $this->field_select( $id, $current, $options, $attrs ); break; case 'radio': $this->field_radio( $id, $current, $options, $attrs ); break; case 'editor': $this->field_editor( $id, $current, $options ); break; case 'color': $this->field_input( $id, $current, array_merge( array( 'class' => 'odin-color-field' ), $attrs ) ); break; case 'upload': $this->field_upload( $id, $current, $attrs ); break; case 'image': $this->field_image( $id, $current ); break; case 'image_plupload': $this->field_image_plupload( $id, $current ); break; default: do_action( 'odin_user_meta_field_' . $this->id, $type, $id, $current, $options, $attrs ); break; } } /** * Build field attributes. * * @param array $attrs Attributes as array. * * @return string Attributes as string. */ protected function build_field_attributes( $attrs ) { $attributes = ''; if ( ! empty( $attrs ) ) { foreach ( $attrs as $key => $attr ) { $attributes .= ' ' . $key . '="' . $attr . '"'; } } return $attributes; } /** * Input field. * * @param string $id Field id. * @param string $current Field current value. * @param array $attrs Array with field attributes. * * @return string HTML of the field. */ protected function field_input( $id, $current, $attrs ) { if ( ! isset( $attrs['type'] ) ) { $attrs['type'] = 'text'; } echo sprintf( '', $id, esc_attr( $current ), $this->build_field_attributes( $attrs ) ); } /** * Textarea field. * * @param string $id Field id. * @param string $current Field current value. * @param array $attrs Array with field attributes. * * @return string HTML of the field. */ protected function field_textarea( $id, $current, $attrs ) { if ( ! isset( $attrs['cols'] ) ) { $attrs['cols'] = '60'; } if ( ! isset( $attrs['rows'] ) ) { $attrs['rows'] = '5'; } echo sprintf( '
', $id, esc_attr( $current ), $this->build_field_attributes( $attrs ) ); } /** * Checkbox field. * * @param string $id Field id. * @param string $current Field current value. * @param array $attrs Array with field attributes. * * @return string HTML of the field. */ protected function field_checkbox( $id, $current, $attrs ) { echo sprintf( '', $id, checked( 1, $current, false ), $this->build_field_attributes( $attrs ) ); } /** * Select field. * * @param string $id Field id. * @param string $current Field current value. * @param array $options Array with select options. * @param array $attrs Array with field attributes. * * @return string HTML of the field. */ protected function field_select( $id, $current, $options, $attrs ) { // If multiple add a array in the option. $multiple = ( in_array( 'multiple', $attrs ) ) ? '[]' : ''; $html = sprintf( ''; echo $html; } /** * Radio field. * * @param string $id Field id. * @param string $current Field current value. * @param array $options Array with input options. * @param array $attrs Array with field attributes. * * @return string HTML of the field. */ protected function field_radio( $id, $current, $options, $attrs ) { $html = ''; foreach ( $options as $key => $label ) { $html .= sprintf( '
', $id, $key, checked( $current, $key, false ), $label, $this->build_field_attributes( $attrs ) ); } echo $html; } /** * Editor field. * * @param string $id Field id. * @param string $current Field current value. * @param array $options Array with wp_editor options. * * @return string HTML of the field. */ protected function field_editor( $id, $current, $options ) { // Set default options. if ( empty( $options ) ) { $options = array( 'textarea_rows' => 10 ); } $options[ 'textarea_name' ] = $id; echo '
'; wp_editor( wpautop( $current ), $id, $options ); echo '
'; } /** * Upload field. * * @param string $id Field id. * @param string $current Field current value. * @param array $attrs Array with field attributes. * * @return string HTML of the field. */ protected function field_upload( $id, $current, $attrs ) { echo sprintf( '
', $id, esc_url( $current ), __( 'Select file', 'avalon-b' ), $this->build_field_attributes( $attrs ) ); } /** * Image field. * * @param string $id Field id. * @param string $current Field current value. * * @return string HTML of the field. */ protected function field_image( $id, $current ) { // Gets placeholder image. $image = get_template_directory_uri() . '/core/assets/images/placeholder.png'; $html = '
'; $html .= '' . $image . ''; if ( $current ) { $image = wp_get_attachment_image_src( $current, 'thumbnail' ); $image = $image[0]; } $html .= sprintf( '', $id, $current, $image, __( 'Select image', 'avalon-b' ), __( 'Remove image', 'avalon-b' ) ); $html .= '
'; $html .= '
'; echo $html; } /** * Image plupload field. * * @param string $id Field id. * @param string $current Field current value. * * @return string HTML of the field. */ protected function field_image_plupload( $id, $current ) { $html = ''; echo $html; } /** * Save user meta data. * * @param string $id Field id. * @param string $current Field current value. */ public function save( $user_id ) { // Verify nonce. if ( ! isset( $_POST[ $this->nonce ] ) || ! wp_verify_nonce( $_POST[ $this->nonce ], basename( __FILE__ ) ) ) { return ''; } // Only saves if the current user can edit user profiles. if ( ! current_user_can( 'edit_user', $user_id ) ) { return ''; } foreach ( $this->fields as $field ) { $name = $field['id']; $old = get_user_meta( $user_id, $name, true ); $new = apply_filters( 'odin_save_user_meta_' . $this->id, $_POST[ $name ], $name ); if ( $new && $new != $old ) { update_user_meta( $user_id, $name, $new ); } elseif ( '' == $new && $old ) { delete_user_meta( $user_id, $name, $old ); } } } }