PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

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