class-um.php 12.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
<?php

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

if(class_exists( 'UM_API' )) {

	class WPF_UM extends WPF_Integrations_Base {

		/**
		 * Gets things started
		 *
		 * @access  public
		 * @since   1.0
		 * @return  void
		 */

		public function init() {

			add_filter( 'wpf_configure_settings', 	array( $this, 'register_settings' ), 15, 2 );
			add_filter( 'wpf_configure_sections', 	array($this, 'configure_sections'), 10, 2 );
			add_filter( 'wpf_meta_fields', 			array( $this, 'set_contact_field_names' ), 30 );

			add_action( 'add_meta_boxes', 			array( $this, 'add_meta_box' ) );
			add_action( 'admin_enqueue_scripts', 	array( $this, 'metabox_scripts') );
			add_action( 'save_post', 				array( $this, 'save_meta_box_data' ) );
			add_filter( 'wpf_meta_box_post_types', 	array( $this, 'unset_wpf_meta_boxes' ));

			// Registration
			add_action( 'um_before_new_user_register', array( $this, 'before_user_registration' ) );
			add_action( 'um_post_registration_global_hook', array( $this, 'registration_global_hook' ), 20, 2 );
			add_action( 'um_after_user_is_approved', array( $this, 'after_user_is_approved' ) );

			// Profile
			add_action( 'profile_update', 			array( $this, 'profile_update' ), 10, 2 );
			add_action( 'um_profile_before_header', array( $this, 'pull_profile_changes' ));

			// Callbacks and filters
			add_filter( 'wpf_pulled_user_meta', 	array($this, 'format_user_meta'), 10, 2 );
			add_filter( 'wpf_user_register', 		array( $this, 'filter_form_fields'), 10, 2 );
			add_filter( 'wpf_user_update', 			array( $this, 'filter_form_fields'), 10, 2 );
			add_action( 'wpf_user_updated', 		array( $this, 'user_updated' ) );
			add_action( 'wpf_tags_modified', 		array( $this, 'update_role' ), 10, 2);
			add_action( 'wpf_user_created', 		array( $this, 'user_created' ), 10, 3 );

		}


		/**
		 * Add fields to settings page
		 *
		 * @access public
		 * @return array Settings
		 */

		public function register_settings( $settings, $options ) {

			$settings['um_header'] = array(
					'title'   => __('Ultimate Member Integration', 'wp-fusion' ),
					'std'     => 0,
					'type'    => 'heading',
					'section' => 'integrations'
			);

			$settings['um_pull'] = array(
					'title'   => __('Pull', 'wp-fusion' ),
					'desc'    => __('Update the local profile data for a given user from ' . wp_fusion()->crm->name . ' before displaying. May slow down profile load times.', 'wp-fusion' ),
					'std'     => 0,
					'type'    => 'checkbox',
					'section' => 'integrations'
			);

			$settings['um_defer'] = array(
					'title'   => __('Defer Until Activation', 'wp-fusion' ),
					'desc'    => __('Don\'t send any data to ' . wp_fusion()->crm->name . ' until the account has been activated, either by an administrator or via email activation.', 'wp-fusion' ),
					'std'     => 0,
					'type'    => 'checkbox',
					'section' => 'integrations'
			);

			return $settings;

		}

		/**
		 * Adds Integrations tab if not already present
		 *
		 * @access public
		 * @return void
		 */

		public function configure_sections($page, $options) {

			if(!isset($page['sections']['integrations']))
				$page['sections'] = wp_fusion()->settings->insert_setting_after('contact-fields', $page['sections'], array('integrations' => __('Integrations', 'wp-fusion' )));

			return $page;

		}

		/**
		 * Set field labels from UM field labels
		 *
		 * @access public
		 * @return array Settings
		 */

		public function set_contact_field_names( $meta_fields ) {

			$um_fields = get_option('um_fields');

			foreach ((array)$um_fields as $key => $field) {

				if(!isset($field['label']))
					$field['label'] = '';

				if(!isset($field['type']))
					$field['type'] = '';

				$meta_fields[$key] = array(
					'label'	=> $field['label'],
					'type'	=> $field['type']
					);

			}

			return $meta_fields;

		}


		/**
		 * Removes standard WPF meta boxes from UM related post types
		 *
		 * @access  public
		 * @return  array Post Types
		 */

		public function unset_wpf_meta_boxes( $post_types ) {

			unset($post_types['um_form']);
			unset($post_types['um_role']);
			unset($post_types['um_directory']);
			
			return $post_types;

		}


		/**
		 * Filters registration data before sending to the CRM
		 *
		 * @access public
		 * @return array Registration data
		 */

		public function filter_form_fields( $post_data, $user_id ) {

			if(isset($post_data['um_role']))
				$post_data['role'] = $post_data['um_role'];

			// Quit early if it's not a UM form
			if(!isset($post_data['form_id']))
				return $post_data;

			$unique_id = '-' . $post_data['form_id'];

			$registration_data = array();

			foreach($post_data as $key => $value) {

				if(substr( $key, -strlen( $unique_id ) ) == $unique_id) {

					// Trim the unique ID from the end of the string
					$key = substr($key, 0, -strlen($unique_id));

					$post_data[$key] = $value;

				}

			}

			// Adapt Ultimate Member fields to WordPress values
			if(isset($post_data['user_password'])) {
				$post_data['user_pass'] = $post_data['user_password'];
				unset($post_data['user_password']);
			}

			return $post_data;

		}

		/**
		 * Triggered before registration, allows removing WPF create_user hook
		 *
		 * @access public
		 * @return null
		 */

		public function before_user_registration($args) {

			if( wp_fusion()->settings->get('um_defer') == true )
				remove_action('user_register', array( wp_fusion()->user, 'user_register' ), 20);

		}

		/**
		 * Triggered after registration to save custom tags as meta to the user's profile
		 *
		 * @access public
		 * @return null
		 */

		public function registration_global_hook($user_id, $args) {

			$settings = get_post_meta( $args['form_id'], 'wpf-settings-um', true);

			if(!empty($settings['apply_tags'])) {
				update_user_meta( $user_id, 'wpf-settings-um', $settings['apply_tags'] );
			}

		}

		/**
		 * Triggered after user is approved. Applies tags stored in registration_global_hook
		 *
		 * @access public
		 * @return null
		 */

		public function after_user_is_approved($user_id) {

			// If we're deferring registration until activation
			if(wp_fusion()->settings->get('um_defer') == true)
				wp_fusion()->user->user_register($user_id);

			$tags = get_user_meta( $user_id, 'wpf-settings-um', true );
			
			if(!empty($tags)) {
				wp_fusion()->user->apply_tags($tags, $user_id);

				// Clean up
				delete_user_meta( $user_id, 'wpf-settings-um' );

			}

		}

		/**
		 * Triggered after new user meta data is pulled down from the CRM
		 *
		 * @access public
		 * @return null
		 */

		public function user_updated( $user_id ) {

			do_action( 'um_after_user_updated', $user_id );

		}

		/**
		 * Remove um profile update hooks to prevent duplicate API calls after user register
		 *
		 * @access public
		 * @return null
		 */

		public function user_created( $user_id, $contact_id, $post_data ) {

			remove_action( 'profile_update', array( $this, 'profile_update') );

		}


		/**
		 * Triggered on profile update
		 *
		 * @access public
		 * @return void
		 */

		public function profile_update( $user_id, $old_user_data ) {

			if( wp_fusion()->settings->get('push') == true )
				wp_fusion()->user->push_user_meta( $user_id, $_POST );

		}


		/**
		 * Loads new values from CRM before displaying UM profile
		 *
		 * @access public
		 * @return void
		 */

		public function pull_profile_changes( $args ) {

			if( wp_fusion()->settings->get('um_pull') == true ) {
				$user_id = um_profile_id();
				wp_fusion()->user->pull_user_meta($user_id);
			}

		}

		/**
		 * Formats loaded user meta fields to match UM syntax
		 *
		 * @access public
		 * @return array User meta
		 */

		public function format_user_meta( $user_meta, $user_id ) {

			$um_fields = get_option('um_fields');

			foreach ((array)$um_fields as $key => $field) {

				// Reformat dates to match the configured UM format
				if(isset( $user_meta[$key] ) && $field['type'] == 'date' )
					$user_meta[$key] = date($field['format'], strtotime($user_meta[$key]));

			}

			return $user_meta;

		}

		/**
		 * Updates user's role if tag is linked to a UM role
		 *
		 * @access public
		 * @return void
		 */

		public function update_role( $user_id, $user_tags ) {

			if($user_tags == false)
				return;

			$linked_roles = get_posts( array(
				'post_type'	=> 'um_role',
				'nopaging'	=> true,
				'meta_query' => array(
						array(
						'key' => 'wpf-settings-um',
						'compare' => 'EXISTS'
						),
					),
				) );

			// Update role based on user tags
			foreach( $linked_roles as $role ) {

				$settings = get_post_meta( $role->ID, 'wpf-settings-um', true );

				if(empty($settings) || empty($settings['tag_link']))
					continue;

				global $ultimatemember;
				um_fetch_user( $user_id );

				$tag_id = $settings['tag_link'][0];
				$user_role = $ultimatemember->user->get_role();

				if(in_array($tag_id, $user_tags) && $user_role != $role->post_name)
					$ultimatemember->user->set_role($role->post_name);

			}

		}

		/**
		 * Loads scripts for UM meta box
		 *
		 * @access public
		 * @return void
		 */

		public function metabox_scripts(){

			wp_enqueue_style( 'select4', WPF_DIR_URL . '/includes/admin/options/lib/select2/select4.min.css' );
			wp_enqueue_script( 'select4', WPF_DIR_URL . 'includes/admin/options/lib/select2/select4.min.js', array('jquery'), '4.0.1', true);

			wp_enqueue_script( 'wpf-admin', WPF_DIR_URL . 'assets/js/wpf-admin.js', array('jquery', 'select4'), WP_FUSION_VERSION, true);
			wp_enqueue_style( 'wpf-admin', WPF_DIR_URL . 'assets/css/wpf-admin.css', array(), WP_FUSION_VERSION);

		}

		/**
		 * Adds meta box, only for UM registration type forms
		 *
		 * @access public
		 * @return void
		 */

		public function add_meta_box() {

			global $post;

			if(get_post_meta( $post->ID, '_um_mode', true ) == 'register') {
				add_meta_box('wpf-um-meta','WP Fusion', array($this, 'meta_box_callback'), 'um_form', 'side', 'default');
			} elseif( $post->post_type == 'um_role' ) {
				add_meta_box('wpf-um-meta','WP Fusion', array($this, 'meta_box_callback_role'), 'um_role', 'side', 'default');
			}
			
		}

		/**
		 * Displays meta box content
		 *
		 * @access public
		 * @return mixed
		 */

		public function meta_box_callback( $post ) {

			// Add an nonce field so we can check for it later.
			wp_nonce_field( 'wpf_meta_box_um', 'wpf_meta_box_um_nonce' );

			$settings = array(
				'apply_tags'	=> array(),
			);

			if(get_post_meta( $post->ID, 'wpf-settings-um', true )) 
				$settings = array_merge($settings, get_post_meta( $post->ID, 'wpf-settings-um', true ));

			/*
			// Apply tags
			*/

			echo '<p><label for="wpf-um-apply-tags">Apply these tags when a user registers:</label><br />';
			wpf_render_tag_multiselect( $settings['apply_tags'], 'wpf-settings-um', 'apply_tags' );
			echo '</p>';

		}

		/**
		 * Displays meta box content
		 *
		 * @access public
		 * @return mixed
		 */

		public function meta_box_callback_role( $post ) {

			// Add an nonce field so we can check for it later.
			wp_nonce_field( 'wpf_meta_box_um', 'wpf_meta_box_um_nonce' );

			$settings = array(
				'tag_link'	=> array(),
			);

			if(get_post_meta( $post->ID, 'wpf-settings-um', true )) 
				$settings = array_merge($settings, get_post_meta( $post->ID, 'wpf-settings-um', true ));

			/*
			// Apply tags
			*/

			echo '<p><label for="wpf-um-apply-tags">Link with ' . wp_fusion()->crm->name . ' tag:</label><br />';
			wpf_render_tag_multiselect( $settings['tag_link'], 'wpf-settings-um', 'tag_link', null, false, 'Select Tag', 1 );
			echo '<span class="description">When the selected tag is applied, users will automatically be given the ' . $post->post_title .' role.</span>';
			echo '</p>';

		}

		/**
		 * Saves UM meta box data
		 *
		 * @access public
		 * @return null
		 */

		public function save_meta_box_data($post_id) {

			/*
			 * We need to verify this came from our screen and with proper authorization,
			 * because the save_post action can be triggered at other times.
			 */

			// Check if our nonce is set.
			if ( ! isset( $_POST['wpf_meta_box_um_nonce'] ) )
				return;

			// Verify that the nonce is valid.
			if ( ! wp_verify_nonce( $_POST['wpf_meta_box_um_nonce'], 'wpf_meta_box_um' ) )
				return;

			// If this is an autosave, our form has not been submitted, so we don't want to do anything.
			if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
				return;


			// Don't update on revisions
			if($_POST['post_type'] == 'revision')
				return;
			

			if(isset($_POST['wpf-settings-um'])) {
				update_post_meta( $post_id, 'wpf-settings-um', $_POST['wpf-settings-um'] );
			}
		}

	}

	new WPF_UM;

}