PageRenderTime 189ms CodeModel.GetById 147ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/types/embedded/usermeta-init.php

https://github.com/ngocphuuit/wordpress-tax
PHP | 1130 lines | 809 code | 120 blank | 201 comment | 173 complexity | 9e2aa489ada1af75892152189bed0c93 MD5 | raw file
   1<?php
   2// Add usermeta and post fileds groups to access.
   3$usermeta_access = new Usermeta_Access;
   4$fields_access = new Post_Fields_Access;
   5//setlocale(LC_ALL, 'nl_NL');
   6
   7/**
   8 * Add User Fileds menus, need add to wpcf_admin_menu_hook
   9 *
  10 * @author Gen gen.i@icanlocalize.com
  11 * @since Types 1.3
  12 */
  13function wpcf_admin_menu_edit_user_fields_hook() {
  14    do_action( 'wpcf_admin_page_init' );
  15
  16    // Group filter
  17    wp_enqueue_script( 'wpcf-filter-js',
  18            WPCF_EMBEDDED_RES_RELPATH
  19            . '/js/custom-fields-form-filter.js', array('jquery'), WPCF_VERSION );
  20    // Form
  21    wp_enqueue_script( 'wpcf-form-validation',
  22            WPCF_EMBEDDED_RES_RELPATH . '/js/'
  23            . 'jquery-form-validation/jquery.validate.min.js', array('jquery'),
  24            WPCF_VERSION );
  25    wp_enqueue_script( 'wpcf-form-validation-additional',
  26            WPCF_EMBEDDED_RES_RELPATH . '/js/'
  27            . 'jquery-form-validation/additional-methods.min.js',
  28            array('jquery'), WPCF_VERSION );
  29    // Scroll
  30    wp_enqueue_script( 'wpcf-scrollbar',
  31            WPCF_EMBEDDED_RELPATH . '/common/visual-editor/res/js/scrollbar.js',
  32            array('jquery') );
  33    wp_enqueue_script( 'wpcf-mousewheel',
  34            WPCF_EMBEDDED_RELPATH . '/common/visual-editor/res/js/mousewheel.js',
  35            array('wpcf-scrollbar') );
  36    //Css editor
  37    wp_enqueue_script( 'wpcf-form-codemirror',
  38            WPCF_RELPATH . '/resources/js/codemirror234/lib/codemirror.js',
  39            array('wpcf-js') );
  40    wp_enqueue_script( 'wpcf-form-codemirror-css-editor',
  41            WPCF_RELPATH . '/resources/js/codemirror234/mode/css/css.js',
  42            array('wpcf-js') );
  43    wp_enqueue_script( 'wpcf-form-codemirror-html-editor',
  44            WPCF_RELPATH . '/resources/js/codemirror234/mode/xml/xml.js',
  45            array('wpcf-js') );
  46    wp_enqueue_script( 'wpcf-form-codemirror-html-editor2',
  47            WPCF_RELPATH . '/resources/js/codemirror234/mode/htmlmixed/htmlmixed.js',
  48            array('wpcf-js') );
  49    wp_enqueue_script( 'wpcf-form-codemirror-editor-resize',
  50            WPCF_RELPATH . '/resources/js/jquery_ui/jquery.ui.resizable.min.js',
  51            array('wpcf-js') );
  52
  53
  54
  55    wp_enqueue_style( 'wpcf-css-editor',
  56            WPCF_RELPATH . '/resources/js/codemirror234/lib/codemirror.css' );
  57    wp_enqueue_style( 'wpcf-css-editor-resize',
  58            WPCF_RELPATH . '/resources/js/jquery_ui/jquery.ui.theme.min.css' );
  59    wp_enqueue_style( 'wpcf-usermeta',
  60            WPCF_EMBEDDED_RES_RELPATH . '/css/usermeta.css' );
  61    
  62    // MAIN
  63    wp_enqueue_script( 'wpcf-fields-form',
  64            WPCF_EMBEDDED_RES_RELPATH
  65            . '/js/fields-form.js', array('wpcf-js') );
  66
  67    /*
  68     * Enqueue styles
  69     */
  70    wp_enqueue_style( 'wpcf-scroll',
  71            WPCF_EMBEDDED_RELPATH . '/common/visual-editor/res/css/scroll.css' );
  72
  73    add_action( 'admin_footer', 'wpcf_admin_fields_form_js_validation' );
  74    require_once WPCF_INC_ABSPATH . '/fields.php';
  75    require_once WPCF_INC_ABSPATH . '/usermeta.php';
  76    require_once WPCF_INC_ABSPATH . '/fields-form.php';
  77    require_once WPCF_INC_ABSPATH . '/usermeta-form.php';
  78    $form = wpcf_admin_usermeta_form();
  79    wpcf_form( 'wpcf_form_fields', $form );
  80
  81}
  82
  83/**
  84 * Add/Edit usermeta fields group
  85 *
  86 * @author Gen gen.i@icanlocalize.com
  87 * @since Types 1.3
  88 */
  89function wpcf_admin_menu_edit_user_fields() {
  90    if ( isset( $_GET['group_id'] ) ) {
  91        $title = __( 'Edit Usermeta Group', 'wpcf' );
  92    } else {
  93        $title = __( 'Add New Usermeta Group', 'wpcf' );
  94    }
  95    echo wpcf_add_admin_header( $title );
  96
  97    $form = wpcf_form( 'wpcf_form_fields' );
  98    echo '<br /><form method="post" action="" class="wpcf-fields-form '
  99    . 'wpcf-form-validate" onsubmit="';
 100    echo 'if (jQuery(\'#wpcf-group-name\').val() == \'' . __( 'Enter group title',
 101            'wpcf' ) . '\') { jQuery(\'#wpcf-group-name\').val(\'\'); }';
 102    echo 'if (jQuery(\'#wpcf-group-description\').val() == \'' . __( 'Enter a description for this group',
 103            'wpcf' ) . '\') { jQuery(\'#wpcf-group-description\').val(\'\'); }';
 104    echo 'jQuery(\'.wpcf-forms-set-legend\').each(function(){
 105        if (jQuery(this).val() == \'' . __( 'Enter field name',
 106            'wpcf' ) . '\') {
 107            jQuery(this).val(\'\');
 108        }
 109        if (jQuery(this).next().val() == \'' . __( 'Enter field slug',
 110            'wpcf' ) . '\') {
 111            jQuery(this).next().val(\'\');
 112        }
 113        if (jQuery(this).next().next().val() == \'' . __( 'Describe this field',
 114            'wpcf' ) . '\') {
 115            jQuery(this).next().next().val(\'\');
 116        }
 117	});';
 118    echo '">';
 119    echo $form->renderForm();
 120    echo '</form>';
 121    echo wpcf_add_admin_footer();
 122}
 123
 124/**
 125 * Add Usermeta Fields manager page.
 126 *
 127 * @author Gen gen.i@icanlocalize.com
 128 * @since Types 1.3
 129 */
 130function wpcf_admin_menu_user_fields_control_hook() {
 131    do_action( 'wpcf_admin_page_init' );
 132
 133    add_action( 'admin_head', 'wpcf_admin_user_fields_control_js' );
 134    add_thickbox();
 135    require_once WPCF_INC_ABSPATH . '/fields.php';
 136    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
 137    require_once WPCF_INC_ABSPATH . '/fields-control.php';
 138    require_once WPCF_INC_ABSPATH . '/usermeta-control.php';
 139
 140    if ( isset( $_REQUEST['_wpnonce'] )
 141            && wp_verify_nonce( $_REQUEST['_wpnonce'],
 142                    'user_fields_control_bulk' )
 143            && (isset( $_POST['action'] ) || isset( $_POST['action2'] )) && !empty( $_POST['fields'] ) ) {
 144        $action = $_POST['action'] == '-1' ? $_POST['action2'] : $_POST['action'];
 145        wpcf_admin_user_fields_control_bulk_actions( $action );
 146    }
 147
 148    global $wpcf_control_table;
 149    $wpcf_control_table = new WPCF_User_Fields_Control_Table( array(
 150                'ajax' => true,
 151                'singular' => __( 'User Field', 'wpcf' ),
 152                'plural' => __( 'User Fields', 'wpcf' ),
 153                    ) );
 154    $wpcf_control_table->prepare_items();
 155}
 156
 157/**
 158 * Menu page display.
 159 */
 160function wpcf_admin_menu_user_fields_control() {
 161    global $wpcf_control_table;
 162    echo wpcf_add_admin_header( __( 'User Fields Control', 'wpcf' ) );
 163    echo '<br /><form method="post" action="" id="wpcf-custom-fields-control-form" class="wpcf-custom-fields-control-form '
 164    . 'wpcf-form-validate" enctype="multipart/form-data">';
 165    echo wpcf_admin_custom_fields_control_form( $wpcf_control_table );
 166    wp_nonce_field( 'user_fields_control_bulk' );
 167    echo '</form>';
 168    echo wpcf_add_admin_footer();
 169}
 170
 171/**
 172 * Usermeta groups listing
 173 *
 174 * @author Gen gen.i@icanlocalize.com
 175 * @since Types 1.3
 176 */
 177function wpcf_usermeta_summary() {
 178    echo wpcf_add_admin_header( __( 'User Fields', 'wpcf' ) );
 179    require_once WPCF_INC_ABSPATH . '/fields.php';
 180    require_once WPCF_INC_ABSPATH . '/usermeta.php';
 181    require_once WPCF_INC_ABSPATH . '/usermeta-list.php';
 182    $to_display = wpcf_admin_fields_get_fields();
 183    if ( !empty( $to_display ) ) {
 184        add_action( 'wpcf_groups_list_table_after',
 185                'wpcf_admin_promotional_text' );
 186    }
 187    wpcf_admin_usermeta_list();
 188    echo wpcf_add_admin_footer();
 189}
 190
 191//Add usermeta hook when user profile loaded
 192add_action( 'show_user_profile', 'wpcf_admin_user_profile_load_hook' );
 193add_action( 'edit_user_profile', 'wpcf_admin_user_profile_load_hook' );
 194
 195//Save usermeta hook
 196add_action( 'personal_options_update', 'wpcf_admin_user_profile_save_hook' );
 197add_action( 'edit_user_profile_update', 'wpcf_admin_user_profile_save_hook' );
 198
 199
 200
 201/**
 202 * Add usermeta groups to post editor
 203 */
 204add_filter( 'editor_addon_menus_types',
 205        'wpcf_admin_post_add_usermeta_to_editor_js' );
 206
 207
 208add_action( 'load-post.php', '__wpcf_usermeta_test', 999999999999999999999 );
 209add_action( 'load-post-new.php', '__wpcf_usermeta_test', 999999999999999999999 );
 210if ( is_admin() ) {
 211    add_filter( 'wpcf_post_groups', '__wpcf_usermeta_test_groups' );
 212}
 213
 214function __wpcf_usermeta_test() {
 215    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
 216    $field['id'] = md5( 'date' . time() );
 217    $here = array(basename( $_SERVER['REQUEST_URI'] ), basename( $_SERVER['SCRIPT_FILENAME'] ));
 218    global $post;
 219    // Get post_type
 220    if ( $post ) {
 221        $post_type = get_post_type( $post );
 222    } else if ( !empty( $_GET['post'] ) ) {
 223        $post_type = get_post_type( $_GET['post'] );
 224    }
 225    if ( ( $here[0] == ('index.php' || 'wp-admin')) && ( $here[1] != 'index.php') ) {
 226        if ( isset( $post_type )
 227                && in_array( $post_type,
 228                        array('view', 'view-template', 'cred-form') ) ) {
 229            return;
 230        }
 231        wpcf_admin_post_add_to_editor( $field );
 232    }
 233}
 234
 235function __wpcf_usermeta_test_groups( $groups ) {
 236    if ( !empty( $groups ) ) {
 237        return $groups;
 238    }
 239    $groups = wpcf_admin_fields_get_groups( 'wp-types-user-group' );
 240    $check = false;
 241    if ( !empty( $groups ) ) {
 242
 243        foreach ( $groups as $group_id => $group ) {
 244
 245            // Mark meta box as hidden
 246            $groups[$group_id]['__show_meta_box'] = false;
 247
 248            if ( empty( $group['is_active'] ) ) {
 249                unset( $groups[$group_id] );
 250                continue;
 251            }
 252            $fields = wpcf_admin_fields_get_fields_by_group( $group['id'],
 253                    'slug', true, false, true, 'wp-types-user-group',
 254                    'wpcf-usermeta' );
 255            if ( empty( $fields ) ) {
 256                unset( $groups[$group_id] );
 257                continue;
 258            }
 259            $check = true;
 260        }
 261    }
 262    if ( !$check ) {
 263        remove_action( 'admin_enqueue_scripts',
 264                'wpcf_admin_post_add_to_editor_js' );
 265    } else {
 266        wpcf_edit_post_screen_scripts();
 267    }
 268    return $groups;
 269}
 270
 271if ( !isset( $_GET['post_type'] ) && isset( $_GET['post'] ) ) {
 272    $post_type = get_post_type( $_GET['post'] );
 273} else if ( isset( $_GET['post_type'] ) && in_array( $_GET['post_type'],
 274                get_post_types( array('show_ui' => true) ) ) ) {
 275    $post_type = $_GET['post_type'];
 276}
 277
 278/*
 279 * 
 280 * This is not needed for Views 1.3
 281 * Kept for compatibility with older versions
 282 */
 283if ( isset( $post_type ) && in_array( $post_type,
 284                array('view', 'view-template', 'cred-form') ) ) {
 285    add_filter( 'editor_addon_menus_wpv-views',
 286            'wpcf_admin_post_add_usermeta_to_editor_js' );
 287    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
 288    add_action( 'admin_footer', 'wpcf_admin_post_js_validation' );
 289    //wpcf_enqueue_scripts();
 290}
 291
 292/**
 293 * Get current logged user ID
 294 *
 295 * @author Gen gen.i@icanlocalize.com
 296 * @since Types 1.3
 297 */
 298function wpcf_usermeta_get_user( $method = '' ){
 299    if ( empty( $method ) ) {
 300        $current_user = wp_get_current_user();
 301        $user_id = $current_user->ID;
 302    }
 303
 304    return $user_id;
 305}
 306
 307/**
 308 * Add User Fields to editor
 309 *
 310 * @author Gen gen.i@icanlocalize.com
 311 * @since Types 1.3
 312 */
 313function wpcf_admin_post_add_usermeta_to_editor_js( $items, $views_callback = false ){
 314    global $wpcf;
 315
 316    $post = wpcf_admin_get_edited_post();
 317    if ( empty( $post ) ) {
 318        $post = (object) array('ID' => -1);
 319    }
 320
 321    $groups = wpcf_admin_fields_get_groups( 'wp-types-user-group' );
 322    $user_id = wpcf_usermeta_get_user();
 323    $add = array();
 324    if ( !empty( $groups ) ) {
 325        $item_styles = array();
 326        foreach ( $groups as $group_id => $group ) {
 327            if ( empty( $group['is_active'] ) ) {
 328                continue;
 329            }
 330            $group['name'] .= ' (User meta fields)';
 331            $fields = wpcf_admin_fields_get_fields_by_group( $group['id'],
 332                    'slug', true, false, true, 'wp-types-user-group',
 333                    'wpcf-usermeta' );
 334
 335            if ( !empty( $fields ) ) {
 336                foreach ( $fields as $field_id => $field ) {
 337                    // Use field class
 338                    $wpcf->usermeta_field->set( $user_id, $field );
 339
 340                    // Get field data
 341                    $data = (array) $wpcf->usermeta_field->config;
 342
 343                    // Get inherited field
 344                    if ( isset( $data['inherited_field_type'] ) ) {
 345                        $inherited_field_data = wpcf_fields_type_action( $data['inherited_field_type'] );
 346                    }
 347
 348                    $callback = 'wpcfFieldsEditorCallback(\'' . $field['id']
 349                            . '\', \'usermeta\', ' . $post->ID . ')';
 350                            
 351                    // Added for Views:users filter Vicon popup
 352                    if ( $views_callback ){
 353                        $callback = 'wpcfFieldsEditorCallback(\'' . $field['id']
 354                            . '\', \'views-usermeta\', ' . $post->ID . ')';
 355                    }        
 356
 357                    $add[$group['name']][stripslashes( $field['name'] )] = array(stripslashes( $field['name'] ), trim( wpcf_usermeta_get_shortcode( $field ),
 358                                '[]' ), $group['name'], $callback);
 359                }
 360                /*
 361                 * Since Types 1.2
 362                 * We use field class to enqueue JS and CSS
 363                 */
 364                $wpcf->usermeta_field->enqueue_script();
 365                $wpcf->usermeta_field->enqueue_style();
 366            }
 367        }
 368    }
 369
 370    $items = $items + $add;
 371    return $items;
 372
 373}
 374
 375/**
 376 * Returns shortcode for specified usermeta field.
 377 *
 378 * @param type $field
 379 * @param type $add Additional attributes
 380 */
 381function wpcf_usermeta_get_shortcode( $field, $add = '', $content = '' ) {
 382    /*if ( isset($_GET['field_type']) && $_GET['field_type'] =='views-usermeta' ) {
 383            $add .= ' user_from_this_loop="true"';
 384    }*/
 385    $shortcode = '[';
 386    $shortcode .= 'types usermeta="' . $field['slug'] . '"' . $add;
 387    $shortcode .= ']' . $content . '[/types]';
 388    $shortcode = apply_filters( 'wpcf_fields_shortcode', $shortcode, $field );
 389    $shortcode = apply_filters( 'wpcf_fields_shortcode_type_' . $field['type'],
 390            $shortcode, $field );
 391    $shortcode = apply_filters( 'wpcf_fields_shortcode_slug_' . $field['slug'],
 392            $shortcode, $field );
 393    return $shortcode;
 394}
 395
 396/**
 397 * Calls view function for specific usermeta field type.
 398 *
 399 * @param type $field
 400 * @param type $atts (additional attributes: user_id, user_name, user_is_author, user_current)
 401 * @return type
 402 */
 403function types_render_usermeta( $field_id, $params, $content = null, $code = '' ) {
 404
 405    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
 406    
 407    global $wpcf, $post, $wpdb, $WP_Views;
 408    // HTML var holds actual output
 409    $html = '';
 410    $current_user = wpcf_usermeta_get_user();
 411
 412    // Set post ID
 413    // user_id, user_name, user_is_author, user_current
 414    if ( is_object( $post ) ) {
 415        $post_id = $post->ID;
 416    } else {
 417        $post_id = 0;
 418    }
 419    if ( isset( $params['post_id'] ) && !empty( $params['post_id'] ) ) {
 420        $post_id = $params['post_id'];
 421    }
 422    
 423    //Get User id from views loop
 424    if ( isset($WP_Views->users_data['term']->ID) && !empty($WP_Views->users_data['term']->ID) ){
 425        $params['user_id'] = $WP_Views->users_data['term']->ID;
 426    }
 427    //print_r($params);exit;
 428    //Get user By ID
 429    if ( isset( $params['user_id'] ) ) {
 430        $user_id = $params['user_id'];
 431    } else if ( isset( $params['user_name'] ) ) { //Get user by login
 432        $user_id = $wpdb->get_var( "SELECT * FROM " . $wpdb->users . " WHERE user_login = '" . $params['user_name'] . "'",
 433                0, 0 );
 434    } else if ( isset( $params['user_is_author'] ) ) { //Get Post author
 435        $user_id = $post->post_author;
 436    } else if ( isset( $params['user_current'] ) ) {//Get current logged user
 437        $user_id = wpcf_usermeta_get_user();
 438    } else { //If empty get post author, if no post, return empty
 439        if ( !empty( $post_id ) ) {
 440            $user_id = $post->post_author;
 441        } else {
 442            return;
 443        }
 444    }
 445
 446    if ( empty( $user_id ) ) {
 447        return;
 448    }
 449    // Get field
 450    $field = wpcf_fields_get_field_by_slug( $field_id, 'wpcf-usermeta' );
 451
 452    // If field not found return empty string
 453    if ( empty( $field ) ) {
 454
 455        // Log
 456        if ( !function_exists( 'wplogger' ) ) {
 457            require_once WPCF_EMBEDDED_ABSPATH . '/common/wplogger.php';
 458        }
 459        global $wplogger;
 460        $wplogger->log( 'types_render_field call for missing field \''
 461                . $field_id . '\'', WPLOG_DEBUG );
 462
 463        return '';
 464    }
 465
 466    // See if repetitive
 467    if ( wpcf_admin_is_repetitive( $field ) ) {
 468
 469        $wpcf->usermeta_repeater->set( $user_id, $field );
 470        $_meta = $wpcf->usermeta_repeater->_get_meta();
 471        $meta = '';
 472        if ( isset( $_meta['custom_order'] ) ) {
 473            $meta = $_meta['custom_order'];
 474        }
 475
 476        if ( (count( $meta ) == 1 ) ) {
 477            $meta_id = key( $meta );
 478            $_temp = array_shift( $meta );
 479            if ( strval( $_temp ) == '' ) {
 480                return '';
 481            } else {
 482                $params['field_value'] = $_temp;
 483                return types_render_field_single( $field, $params, $content,
 484                                $code, $meta_id );
 485            }
 486        } else if ( !empty( $meta ) ) {
 487            $output = '';
 488
 489            if ( isset( $params['index'] ) ) {
 490                $index = $params['index'];
 491            } else {
 492                $index = '';
 493            }
 494
 495            // Allow wpv-for-each shortcode to set the index
 496            $index = apply_filters( 'wpv-for-each-index', $index );
 497
 498            if ( $index === '' ) {
 499                $output = array();
 500                foreach ( $meta as $temp_key => $temp_value ) {
 501                    $params['field_value'] = $temp_value;
 502                    $temp_output = types_render_field_single( $field, $params,
 503                            $content, $code, $temp_key );
 504                    if ( !empty( $temp_output ) ) {
 505                        $output[] = $temp_output;
 506                    }
 507                }
 508                if ( !empty( $output ) && isset( $params['separator'] ) ) {
 509                    $output = implode( html_entity_decode( $params['separator'] ),
 510                            $output );
 511                } else if ( !empty( $output ) ) {
 512                    $output = implode( '', $output );
 513                } else {
 514                    return '';
 515                }
 516            } else {
 517                // Make sure indexed right
 518                $_index = 0;
 519                foreach ( $meta as $temp_key => $temp_value ) {
 520                    if ( $_index == $index ) {
 521                        $params['field_value'] = $temp_value;
 522                        $output = types_render_field_single( $field, $params,
 523                                $content, $code, $temp_key );
 524                    }
 525                    $_index++;
 526                }
 527            }
 528            $html = $output;
 529        } else {
 530            return '';
 531        }
 532    } else {
 533
 534        $params['field_value'] = get_user_meta( $user_id,
 535                wpcf_types_get_meta_prefix( $field ) . $field['slug'], true );
 536        if ( $params['field_value'] == '' && $field['type'] != 'checkbox' ) {
 537            return '';
 538        }
 539        $html = types_render_field_single( $field, $params, $content, $code );
 540        //print_r($params);
 541        //print "$html <br><br>";
 542        /* if ( !empty($params['field_value']) && $field['type'] != 'date' ){
 543          $html = types_render_field_single( $field, $params, $content, $code );
 544          }
 545          if ( $field['type'] == 'date' && !empty($params['field_value']['timestamp']) ){
 546          $html = types_render_field_single( $field, $params );
 547          } */
 548    }
 549
 550    // API filter
 551    $wpcf->usermeta_field->set( $user_id, $field );
 552    return $wpcf->usermeta_field->html( $html, $params );
 553}
 554
 555/**
 556 * Calls view function for specific field type.
 557 *
 558 * @param type $field
 559 * @param type $atts
 560 * @return type
 561 */
 562function types_render_usermeta_field( $field_id, $params, $content = null,
 563        $code = '' ) {
 564
 565    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
 566    global $wpcf, $post;
 567
 568    // HTML var holds actual output
 569    $html = '';
 570
 571    // Set post ID
 572    $post_id = $post->ID;
 573    if ( isset( $params['post_id'] ) && !empty( $params['post_id'] ) ) {
 574        $post_id = $params['post_id'];
 575    }
 576
 577    // Get field
 578    $field = wpcf_fields_get_field_by_slug( $field_id, 'wpcf-usermeta' );
 579
 580    // If field not found return empty string
 581    if ( empty( $field ) ) {
 582
 583        // Log
 584        if ( !function_exists( 'wplogger' ) ) {
 585            require_once WPCF_EMBEDDED_ABSPATH . '/common/wplogger.php';
 586        }
 587        global $wplogger;
 588        $wplogger->log( 'types_render_usermeta_field call for missing field \''
 589                . $field_id . '\'', WPLOG_DEBUG );
 590
 591        return '';
 592    }
 593
 594    //Get user By ID
 595    if ( isset( $params['user_id'] ) ) {
 596        $user_id = $params['user_id'];
 597    } else if ( isset( $params['user_name'] ) ) { //Get user by login
 598        $user_id = $wpdb->get_var( "SELECT * FROM " . $wpdb->prefix . "users WHERE user_login = '" . $params['user_name'] . "'",
 599                0, 0 );
 600    } else if ( isset( $params['user_is_author'] ) ) { //Get Post author
 601        $user_id = $post->post_author;
 602    } else if ( isset( $params['user_current'] ) ) {//Get current logged user
 603        $user_id = wpcf_usermeta_get_user();
 604    } else { //If empty get post author, if no post, return empty
 605        if ( !empty( $post_id ) ) {
 606            $user_id = $post->post_author;
 607        } else {
 608            return;
 609        }
 610    }
 611
 612    if ( empty( $user_id ) ) {
 613        return;
 614    }
 615
 616    // Set field
 617    $wpcf->usermeta_field->set( $user_id, $field );
 618
 619
 620    // See if repetitive
 621    if ( wpcf_admin_is_repetitive( $field ) ) {
 622        $wpcf->usermeta_repeater->set( $user_id, $field );
 623        $_meta = $wpcf->usermeta_repeater->_get_meta();
 624        $meta = $_meta['custom_order'];
 625//        $meta = get_post_meta( $post_id,
 626//                wpcf_types_get_meta_prefix( $field ) . $field['slug'], false );
 627        // Sometimes if meta is empty - array(0 => '') is returned
 628        if ( (count( $meta ) == 1 ) ) {
 629            $meta_id = key( $meta );
 630            $_temp = array_shift( $meta );
 631            if ( strval( $_temp ) == '' ) {
 632                return '';
 633            } else {
 634
 635                $params['field_value'] = $_temp;
 636                return types_render_field_single( $field, $params, $content,
 637                                $code, $meta_id );
 638            }
 639        } else if ( !empty( $meta ) ) {
 640            $output = '';
 641
 642            if ( isset( $params['index'] ) ) {
 643                $index = $params['index'];
 644            } else {
 645                $index = '';
 646            }
 647
 648            // Allow wpv-for-each shortcode to set the index
 649            $index = apply_filters( 'wpv-for-each-index', $index );
 650
 651            if ( $index === '' ) {
 652                $output = array();
 653                foreach ( $meta as $temp_key => $temp_value ) {
 654                    $params['field_value'] = $temp_value;
 655                    $temp_output = types_render_field_single( $field, $params,
 656                            $content, $code, $temp_key );
 657                    if ( !empty( $temp_output ) ) {
 658                        $output[] = $temp_output;
 659                    }
 660                }
 661                if ( !empty( $output ) && isset( $params['separator'] ) ) {
 662                    $output = implode( html_entity_decode( $params['separator'] ),
 663                            $output );
 664                } else if ( !empty( $output ) ) {
 665                    $output = implode( '', $output );
 666                } else {
 667                    return '';
 668                }
 669            } else {
 670                // Make sure indexed right
 671                $_index = 0;
 672                foreach ( $meta as $temp_key => $temp_value ) {
 673                    if ( $_index == $index ) {
 674                        $params['field_value'] = $temp_value;
 675                        return types_render_field_single( $field, $params,
 676                                        $content, $code, $temp_key );
 677                    }
 678                    $_index++;
 679                }
 680                // If missed index
 681                return '';
 682            }
 683            $html = $output;
 684        } else {
 685            return '';
 686        }
 687    } else {
 688        $params['field_value'] = get_user_meta( $user_id,
 689                wpcf_types_get_meta_prefix( $field ) . $field['slug'], true );
 690        if ( $params['field_value'] == '' && $field['type'] != 'checkbox' ) {
 691            return '';
 692        }
 693        $html = types_render_field_single( $field, $params, $content, $code,
 694                $wpcf->usermeta_field->meta_object->umeta_id );
 695    }
 696
 697    // API filter
 698//    $wpcf->usermeta_field->set( $user_id, $field );
 699    return $wpcf->usermeta_field->html( $html, $params );
 700}
 701
 702/**
 703 * Add fields to user profile
 704 */
 705function wpcf_admin_user_profile_load_hook( $user ){
 706    if ( !current_user_can( 'edit_user', $user->ID ) )
 707        return false;
 708
 709    if ( !wpcf_is_embedded() ) {
 710        require_once WPCF_INC_ABSPATH . '/usermeta.php';
 711    }
 712    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
 713    require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta.php';
 714    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
 715    require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php';
 716    add_action( 'admin_footer', 'wpcf_admin_fields_usermeta_styles' );
 717
 718
 719    wpcf_admin_userprofile_init( $user );
 720}
 721
 722/**
 723 * Add styles to admin fields groups
 724 */
 725function wpcf_admin_fields_usermeta_styles(){
 726
 727    if ( !wpcf_is_embedded() ) {
 728        require_once WPCF_INC_ABSPATH . '/usermeta.php';
 729    }
 730    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
 731    require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta.php';
 732    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
 733    require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php';
 734
 735    $groups = wpcf_admin_fields_get_groups( 'wp-types-user-group' );
 736    echo '<style type="text/css">';
 737    if ( !empty( $groups ) ) {
 738        foreach ( $groups as $group ) {
 739            echo str_replace( "}", "}\n",
 740                    wpcf_admin_get_groups_admin_styles_by_group( $group['id'] ) );
 741        }
 742    }
 743    echo '</style>';
 744}
 745
 746/**
 747 * Add fields to user profile
 748 */
 749function wpcf_admin_user_profile_save_hook( $user_id ){
 750    if ( !current_user_can( 'edit_user', $user_id ) )
 751        return false;
 752
 753    if ( !wpcf_is_embedded() ) {
 754        require_once WPCF_INC_ABSPATH . '/usermeta.php';
 755    }
 756    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
 757    require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta.php';
 758    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
 759    require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php';
 760    wpcf_admin_userprofilesave_init( $user_id );
 761}
 762
 763/*
 764 *  Register Usermeta Groups in Types Access
 765 *
 766 *
 767 */
 768
 769class Usermeta_Access
 770{
 771
 772    public static $user_groups = '';
 773    
 774    /**
 775     * Initialize plugin enviroment
 776     */
 777    public function __construct() {
 778        // setup custom capabilities
 779        self::$user_groups = wpcf_admin_fields_get_groups( 'wp-types-user-group' );
 780        //If access plugin installed
 781        if ( function_exists( 'wpcf_access_register_caps' ) ) { // integrate with Types Access
 782            if ( !empty( self::$user_groups ) ) {
 783                //Add Usermeta Fields area
 784                add_filter( 'types-access-area',
 785                        array('Usermeta_Access', 'register_access_usermeta_area'),
 786                        10, 2 );
 787                //Add Usermeta Fields groups
 788                add_filter( 'types-access-group',
 789                        array('Usermeta_Access', 'register_access_usermeta_groups'),
 790                        10, 2 );
 791                //Add Usermeta Fields caps to groups
 792                add_filter( 'types-access-cap',
 793                        array('Usermeta_Access', 'register_access_usermeta_caps'),
 794                        10, 3 );
 795            }
 796        }
 797    }
 798
 799    // register custom CRED Frontend capabilities specific to each group
 800    public static function register_access_usermeta_caps( $caps, $area_id,
 801            $group_id )
 802    {
 803        $USERMETA_ACCESS_AREA_NAME = __( 'User Meta Fields Frontend Access',
 804                'wpcf' );
 805        $USERMETA_ACCESS_AREA_ID = '__USERMETA_FIELDS';
 806        $default_role = 'guest'; //'administrator';
 807        //List of caps with default permissions
 808        $usermeta_caps = array(
 809            array('view_own_in_profile', $default_role, __( 'View own fields in profile',
 810                        'wpcf' )),
 811            array('modify_own', $default_role, __( 'Modify own fields', 'wpcf' )),
 812                /*
 813                  array('view_others_in_profile',$default_role,__('View others fields in profile','wpcf')),
 814                  array('modify_others_','administrator',__('Modify others fields','wpcf')), */
 815        );
 816        if ( $area_id == $USERMETA_ACCESS_AREA_ID ) {
 817            $fields_groups = wpcf_admin_fields_get_groups( 'wp-types-user-group' );
 818            if ( !empty( $fields_groups ) ) {
 819                foreach ( $fields_groups as $group ) {
 820                    $USERMETA_ACCESS_GROUP_NAME = $group['name'] . ' Access Group';
 821                    $USERMETA_ACCESS_GROUP_ID = '__USERMETA_FIELDS_GROUP_' . $group['slug'];
 822                    if ( $group_id == $USERMETA_ACCESS_GROUP_ID ) {
 823                        for ( $i = 0; $i < count( $usermeta_caps ); $i++ ) {
 824                            $caps[$usermeta_caps[$i][0] . '_' . $group['slug']] = array(
 825                                'cap_id' => $usermeta_caps[$i][0] . '_' . $group['slug'],
 826                                'title' => $usermeta_caps[$i][2],
 827                                'default_role' => $usermeta_caps[$i][1]
 828                            );
 829                        }
 830                    }
 831                }
 832            }
 833        }
 834
 835        return $caps;
 836    }
 837
 838    // register a new Types Access Group within Area for Usermeta Fields Groups Frontend capabilities
 839    public static function register_access_usermeta_groups( $groups, $id )
 840    {
 841        $USERMETA_ACCESS_AREA_NAME = __( 'User Meta Fields Frontend Access',
 842                'wpcf' );
 843        $USERMETA_ACCESS_AREA_ID = '__USERMETA_FIELDS';
 844
 845        if ( $id == $USERMETA_ACCESS_AREA_ID ) {
 846            $fields_groups = wpcf_admin_fields_get_groups( 'wp-types-user-group' );
 847            if ( !empty( $fields_groups ) ) {
 848                foreach ( $fields_groups as $group ) {
 849                    $USERMETA_ACCESS_GROUP_NAME = $group['name'];
 850                    //. ' User Meta Fields Access Group'
 851                    $USERMETA_ACCESS_GROUP_ID = '__USERMETA_FIELDS_GROUP_' . $group['slug'];
 852                    $groups[] = array('id' => $USERMETA_ACCESS_GROUP_ID, 'name' => '' . $USERMETA_ACCESS_GROUP_NAME);
 853                }
 854            }
 855        }
 856        return $groups;
 857    }
 858
 859    // register a new Types Access Area for Usermeta Fields Groups Frontend capabilities
 860    public static function register_access_usermeta_area( $areas,
 861            $area_type = 'usermeta' )
 862    {
 863    	$fields_groups = wpcf_admin_fields_get_groups( 'wp-types-user-group' );
 864		if ( !empty( $fields_groups ) ) {	
 865        $USERMETA_ACCESS_AREA_NAME = __( 'User Meta Fields Access', 'wpcf' );
 866        $USERMETA_ACCESS_AREA_ID = '__USERMETA_FIELDS';
 867        $areas[] = array('id' => $USERMETA_ACCESS_AREA_ID, 'name' => $USERMETA_ACCESS_AREA_NAME);
 868		}
 869        return $areas;
 870    }
 871
 872}
 873
 874/*
 875 *  Register Post Fields Groups in Types Access
 876 *
 877 * @author Gen gen.i@icanlocalize.com
 878 * @since Types 1.3
 879 */
 880
 881class Post_Fields_Access
 882{
 883
 884    /**
 885     * Initialize plugin enviroment
 886     */
 887    public static $fields_groups = '';
 888	
 889    public function __construct() {
 890    	//Get list of groups
 891    	self::$fields_groups = wpcf_admin_fields_get_groups();
 892        // setup custom capabilities
 893        //If access plugin installed
 894        if ( function_exists( 'wpcf_access_register_caps' ) ) { // integrate with Types Access
 895            if ( !empty( self::$fields_groups ) ) {
 896                //Add Fields area
 897                add_filter( 'types-access-area',
 898                        array('Post_Fields_Access', 'register_access_fields_area'),
 899                        10, 2 );
 900                //Add Fields groups
 901                add_filter( 'types-access-group',
 902                        array('Post_Fields_Access', 'register_access_fields_groups'),
 903                        10, 2 );
 904                
 905                //Add Fields caps to groups
 906                add_filter( 'types-access-cap',
 907                        array('Post_Fields_Access', 'register_access_fields_caps'),
 908                        10, 3 );
 909				//}		
 910            }
 911        }
 912    }
 913
 914    // register custom CRED Frontend capabilities specific to each group
 915    public static function register_access_fields_caps( $caps, $area_id,
 916            $group_id )
 917    {
 918        $FIELDS_ACCESS_AREA_NAME = __( 'Post Custom Fields Frontend Access',
 919                'wpcf' );
 920        $FIELDS_ACCESS_AREA_ID = '__FIELDS';
 921        $default_role = 'guest'; //'administrator';
 922        //List of caps with default permissions
 923        $fields_caps = array(
 924            array('view_fields_in_edit_page', $default_role, __( 'View Fields In Edit Page',
 925                        'wpcf' )),
 926            array('modify_fields_in_edit_page', 'author', __( 'Modify Fields In Edit Page',
 927                        'wpcf' )),
 928        );
 929        if ( $area_id == $FIELDS_ACCESS_AREA_ID ) {
 930
 931            if ( !empty( self::$fields_groups ) ) {
 932                foreach ( self::$fields_groups as $group ) {
 933                    $FIELDS_ACCESS_GROUP_NAME = $group['name'] . ' Access Group';
 934                    $FIELDS_ACCESS_GROUP_ID = '__FIELDS_GROUP_' . $group['slug'];
 935                    if ( $group_id == $FIELDS_ACCESS_GROUP_ID ) {
 936                        for ( $i = 0; $i < count( $fields_caps ); $i++ ) {
 937                            $caps[$fields_caps[$i][0] . '_' . $group['slug']] = array(
 938                                'cap_id' => $fields_caps[$i][0] . '_' . $group['slug'],
 939                                'title' => $fields_caps[$i][2],
 940                                'default_role' => $fields_caps[$i][1]
 941                            );
 942                        }
 943                    }
 944                }
 945            }
 946        }
 947
 948        return $caps;
 949    }
 950
 951    // register a new Types Access Group within Area for Post Fields Groups Frontend capabilities
 952    public static function register_access_fields_groups( $groups, $id )
 953    {
 954        $FIELDS_ACCESS_AREA_NAME = __( 'Post Fields Frontend Access', 'wpcf' );
 955        $FIELDS_ACCESS_AREA_ID = '__FIELDS';
 956		
 957        if ( $id == $FIELDS_ACCESS_AREA_ID ) {
 958            if ( !empty( self::$fields_groups ) ) {
 959                foreach ( self::$fields_groups as $group ) {
 960                    $FIELDS_ACCESS_GROUP_NAME = $group['name'];
 961                    //. ' User Meta Fields Access Group'
 962                    $FIELDS_ACCESS_GROUP_ID = '__FIELDS_GROUP_' . $group['slug'];
 963                    $groups[] = array('id' => $FIELDS_ACCESS_GROUP_ID, 'name' => '' . $FIELDS_ACCESS_GROUP_NAME);
 964                }
 965            }
 966        }
 967        return $groups;
 968    }
 969
 970    // register a new Types Access Area for Post Fields Groups Frontend capabilities
 971    public static function register_access_fields_area( $areas,
 972            $area_type = 'usermeta' )
 973    {
 974        $FIELDS_ACCESS_AREA_NAME = __( 'Post Meta Fields Access', 'wpcf' );
 975        $FIELDS_ACCESS_AREA_ID = '__FIELDS';
 976        $areas[] = array('id' => $FIELDS_ACCESS_AREA_ID, 'name' => $FIELDS_ACCESS_AREA_NAME);
 977        return $areas;
 978    }
 979
 980}
 981
 982add_action( 'wp_ajax_wpcf_types_suggest_user',
 983        'wpcf_access_wpcf_types_suggest_user_ajax' );
 984
 985/**
 986 * Suggest user AJAX.
 987 */
 988function wpcf_access_wpcf_types_suggest_user_ajax() {
 989    global $wpdb;
 990    /* $users = array();
 991      $q = $wpdb->escape(trim($_POST['q']));
 992      $q = like_escape($q);
 993      $found = $wpdb->get_results("SELECT ID, display_name, user_login FROM $wpdb->users WHERE user_nicename LIKE '%%$q%%' OR user_login LIKE '%%$q%%' OR display_name LIKE '%%$q%%' OR user_email LIKE '%%$q%%' LIMIT 10");
 994      if (!empty($found)) {
 995      foreach ($found as $user) {
 996      $users[$user->user_login] = $user->display_name . ' (' . $user->user_login . ')';
 997      }
 998      } */
 999    $users = '';
1000    $q = esc_sql( trim( $_GET['q'] ) );
1001    $q = like_escape( $q );
1002    $found = $wpdb->get_results( "SELECT ID, display_name, user_login FROM $wpdb->users WHERE user_nicename LIKE '%%$q%%' OR user_login LIKE '%%$q%%' OR display_name LIKE '%%$q%%' OR user_email LIKE '%%$q%%' LIMIT 10" );
1003
1004    if ( !empty( $found ) ) {
1005        foreach ( $found as $user ) {
1006            //$users[$user->user_login] = $user->display_name . ' (' . $user->user_login . ')';
1007            $users .= '<li>' . $user->user_login . '</li>';
1008        }
1009    }
1010    echo $users;
1011    die();
1012}
1013
1014/*
1015 * Callback sumit form usermeta addon
1016 */
1017
1018function wpcf_get_usermeta_form_addon_submit( $views_usermeta = false ){
1019    $add = '';
1020    if ( !empty( $_POST['is_usermeta'] ) ) {
1021        if ( $_POST['display_username_for'] == 'post_autor' ) {
1022            $add .= ' user_is_author="true"';
1023        } elseif ( $_POST['display_username_for'] == 'current_user' ) {
1024            $add .= ' user_current="true"';
1025        }
1026         else {
1027            if ( $_POST['display_username_for_suser_selector'] == 'specific_user_by_id' ) {
1028                $add .= ' user_id="' . $_POST['display_username_for_suser_id_value'] . '"';
1029            } else {
1030                $add .= ' user_name="' . $_POST['display_username_for_suser_username_value'] . '"';
1031            }
1032        }
1033    }
1034    return $add;
1035}
1036
1037/*
1038 * Usermeta fields addon.
1039 * Add form user users
1040*/
1041
1042function wpcf_get_usermeta_form_addon( $settings = array() ){
1043	global $wpdb;
1044	$form = array();
1045	$users = $wpdb->get_results("SELECT ID, user_login, display_name FROM $wpdb->users LIMIT 5");
1046	$form[] = array(
1047		'#type' => 'hidden',
1048		'#value' => 'true',
1049        '#name' => 'is_usermeta',
1050	);
1051	$__default = 'post_autor';
1052	$form[] = array(
1053		'#type' => 'radio',
1054		'#before' => '<div class="fieldset"><p class="form-inline">',
1055		'#suffix' => '</p>',
1056		'#value' => 'post_autor',
1057		'#title' => 'Author of this post',
1058        '#name' => 'display_username_for',
1059		'#default_value' => isset( $settings['user_is_author'] ) && $settings['user_is_author'] == 'true' ? 'post_autor' : $__default,
1060        '#inline' => true,
1061		'#attributes' => array('onclick' => 'wpcf_showmore(false)')
1062	);
1063	$form[] = array(
1064		'#type' => 'radio',
1065		'#before' => '<p class="form-inline">',
1066		'#suffix' => '</p>',
1067		'#value' => 'current_user',
1068		'#title' => 'The current logged in user',
1069        '#name' => 'display_username_for',
1070		'#default_value' => isset( $settings['user_current'] ) && $settings['user_current'] == 'true' ? 'current_user' : $__default,
1071        '#inline' => true,
1072		'#attributes' => array('onclick' => 'wpcf_showmore(false)')
1073	);
1074	$form[] = array(
1075		'#type' => 'radio',
1076		'#before' => '<p class="form-inline">',
1077		'#suffix' => '</p>',
1078		'#title' => 'A specific user',
1079		'#value' => 'pecific_user',
1080		'#id' => 'display_username_for_suser',
1081        '#name' => 'display_username_for',
1082		'#default_value' => isset( $settings['user_id'] ) || isset( $settings['user_name'] ) ? 'pecific_user' : $__default,
1083		'#after' => '',
1084        '#inline' => true,
1085		'#attributes' => array('onclick' => 'wpcf_showmore(true)')
1086	);
1087    $__username = isset( $settings['user_name'] ) ? $settings['user_name'] : '';
1088    $__userid = isset( $settings['user_id'] ) ? intval( $settings['user_id'] ) : '';
1089    $__hidden = !isset( $settings['user_id'] ) && !isset( $settings['user_name'] ) ? ' style="display:none;"' : '';
1090    $__hiddenId = !isset( $settings['user_id'] ) && isset( $settings['user_name'] ) ? ' style="display:none;"' : '';
1091	$form[] = array(
1092		'#type' => 'radio',
1093		'#title' => 'User ID',
1094		'#value' => 'specific_user_by_id',
1095		'#id' => 'display_username_for_suser_id',
1096        '#name' => 'display_username_for_suser_selector',
1097		'#before' => '<div class="group-nested form-inline" id="specific_user_div"' . $__hidden . '><p>',
1098		'#after' => '<input type="text" class="wpcf-form-textfield form-textfield textfield" name="display_username_for_suser_id_value" value="' . $__userid . '"'.
1099		' id="display_username_for_suser_id_value" value=""' . $__hiddenId . '></p>',
1100		'#default_value' => isset( $settings['user_id'] ) || !isset( $settings['user_name'] ) ? 'specific_user_by_id' : '',
1101        '#inline' => true,
1102		'#attributes' => array('onclick' => 'hideControls(\'display_username_for_suser_username_value\',\'display_username_for_suser_id_value\')')
1103	);
1104	$dropdown_users = '';
1105	foreach ($users as $u) {
1106        $dropdown_users .= '<option value="' . $u->user_login . '">' . $u->display_name . ' (' . $u->user_login . ')' . '</option>';
1107    }
1108    $__hidden = !isset( $settings['user_name'] ) ? ' style="display:none;"' : '';
1109	$form[] = array(
1110		'#type' => 'radio',
1111		'#title' => 'User name',
1112		'#value' => 'specific_user_by_username',
1113		'#id' => 'display_username_for_suser_username',
1114        '#name' => 'display_username_for_suser_selector',
1115		'#before' => '<p class="types-suggest-user types-suggest" id="types-suggest-user">',
1116		'#after' => '<input type="text" class="input wpcf-form-textfield form-textfield textfield"'. $__hidden .
1117		' name="display_username_for_suser_username_value" id="display_username_for_suser_username_value" value="' . $__username . '"></p></div></div>',
1118		'#default_value' => isset( $settings['user_name'] ) ? 'specific_user_by_username' : '',
1119        '#inline' => true,
1120		'#attributes' => array('onclick' => 'hideControls(\'display_username_for_suser_id_value\',\'display_username_for_suser_username_value\')')
1121	);
1122
1123	return $form;
1124}
1125
1126add_action('load-user-new.php', 'wpcf_usermeta_add_user_screen');
1127function wpcf_usermeta_add_user_screen() {
1128    require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-add-user.php';
1129    wpcf_usermeta_add_user_screen_init();
1130}