wp-fusion.php 13 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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
<?php

/*
Plugin Name: WP Fusion
Description: WP Fusion connects your website to your CRM, with support for several CRMs and dozens of plugins.
Plugin URI: https://wpfusionplugin.com/
Version: 2.6.8
Author: Very Good Plugins
Author URI: http://verygoodplugins.com/
Text Domain: wp-fusion

*/

/**
 * @copyright Copyright (c) 2016. All rights reserved.
 *
 * @license   Released under the GPL license http://www.opensource.org/licenses/gpl-license.php
 *
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * **********************************************************************
 *
 */

define( 'WP_FUSION_VERSION', '2.6.8' );

// deny direct access
if(!function_exists('add_action')) {
	header('Status: 403 Forbidden');
	header('HTTP/1.1 403 Forbidden');
	exit();
}


final class WP_Fusion {

	/** Singleton *************************************************************/

	/**
	 * @var WP_Fusion The one true WP_Fusion
	 * @since 1.0
	 */
	private static $instance;

	/**
	 * The integrations handler instance variable
	 *
	 * @var WPF_Integrations
	 * @since 2.0
	 */
	public $integrations;


	/**
	 * Manages configured CRMs
	 *
	 * @var WPF_CRMS
	 * @since 2.0
	 */
	public $crm_base;


	/**
	 * Access to the currently selected CRM
	 *
	 * @var crm
	 * @since 2.0
	 */
	public $crm;


	/**
	 * Handler for AJAX and and asynchronous functions
	 *
	 * @var crm
	 * @since 2.0
	 */
	public $ajax;


	/**
	 * User handler - registration, sync, and updates
	 *
	 * @var WPF_CRMS
	 * @since 2.0
	 */
	public $user;


	/**
	 * The settings instance variable
	 *
	 * @var WP_Fusion_Settings
	 * @since 1.0
	 */
	public $settings;


	/**
	 * Main Wp_Fusion Instance
	 *
	 * Insures that only one instance of WP_Fusion exists in memory at any one
	 * time. Also prevents needing to define globals all over the place.
	 *
	 * @since 1.0
	 * @static
	 * @staticvar array $instance
	 * @return The one true WP_Fusion
	 */

	public static function instance() {

		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Fusion ) ) {

			self::$instance = new WP_Fusion;
			self::$instance->setup_constants();
			self::$instance->includes();

			// Create settings
			self::$instance->settings 		= new WPF_Settings;

			// Load active CRM
			self::$instance->crm_base 		= new WPF_CRM_Base;
			self::$instance->crm 			= self::$instance->crm_base->crm;

			// Only useful if a CRM is selected and valid
			if(!empty(self::$instance->crm)) {

				self::$instance->setup_crm_constants();
				self::$instance->integrations_includes();

				self::$instance->user 		= new WPF_User;
				self::$instance->access 	= new WPF_Access_Control;
				self::$instance->ajax 		= new WPF_AJAX;
				
				self::$instance->updater();

			}

		}

		return self::$instance;
	}

	/**
	 * Throw error on object clone
	 *
	 * The whole idea of the singleton design pattern is that there is a single
	 * object therefore, we don't want the object to be cloned.
	 *
	 * @access protected
	 * @return void
	 */

	public function __clone() {
		// Cloning instances of the class is forbidden
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'wp-fusion' ), WP_FUSION_VERSION );
	}

	/**
	 * Disable unserializing of the class
	 *
	 * @access protected
	 * @return void
	 */

	public function __wakeup() {
		// Unserializing instances of the class is forbidden
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'wp-fusion' ), WP_FUSION_VERSION );
	}

	/**
	 * Setup plugin constants
	 *
	 * @access private
	 * @return void
	 */

	private function setup_constants() {

		if(!defined('WPF_MIN_WP_VERSION')) {
			define('WPF_MIN_WP_VERSION', '4.0');
		}

		if(!defined('WPF_DIR_PATH')) {
			define('WPF_DIR_PATH', plugin_dir_path(__FILE__));
		}

		if(!defined('WPF_PLUGIN_PATH')) {
			define('WPF_PLUGIN_PATH', plugin_basename(__FILE__));
		}

		if(!defined('WPF_DIR_URL')) {
			define('WPF_DIR_URL', plugin_dir_url(__FILE__));
		}

		if(!defined('WPF_STORE_URL')) {
			define( 'WPF_STORE_URL', 'http://wpfusionplugin.com' );
		}

	}

	/**
	 * Setup CRM related constants
	 *
	 * @access private
	 * @return void
	 */

	private function setup_crm_constants() {

		if(!defined('WPF_CRM_NAME')) {
			define('WPF_CRM_NAME', self::$instance->crm->name);
		}

	}


	/**
	 * Defines default supported plugin integrations
	 *
	 * @access private
	 * @return array Integrations
	 */

	public function get_integrations() {

		return apply_filters( 'wpf_integrations', array(
			'edd'          		=> 'Easy Digital Downloads',
			'edd-recurring'		=> 'EDD Recurring Payments',
			'gforms' 			=> 'Gravity Forms',
			'woocommerce'  		=> 'WooCommerce',
			'woo-subscriptions' => 'WooCommerce Subscriptions',
			'um'				=> 'Ultimate Member',
			'userpro'			=> 'UserPro',
			'acf'				=> 'Advanced Custom Fields',
			'learndash'			=> 'LearnDash',
			'sensei'			=> 'Sensei',
			'bbpress'			=> 'bbPress',
			'cf7' 				=> 'Contact form 7',
			'membermouse' 		=> 'MemberMouse',
			'buddypress' 		=> 'BuddyPress',
			'pmp'				=> 'Paid Memberships Pro'
		) );

	}

	/**
	 * Defines supported CRMs
	 *
	 * @access private
	 * @return array CRMS
	 */

	public function get_crms() {

		return apply_filters( 'wpf_crms', array(
			'infusionsoft'				=> 'WPF_Infusionsoft_iSDK',
			'activecampaign'			=> 'WPF_ActiveCampaign', 
			'ontraport'					=> 'WPF_Ontraport',
			'drip'						=> 'WPF_Drip'
		) );

	}

	/**
	 * Include required files
	 *
	 * @access private
	 * @return void
	 */

	private function includes() {

		require_once WPF_DIR_PATH .'includes/admin/class-settings.php';

		if(is_admin()) {

			require_once WPF_DIR_PATH .'includes/admin/class-notices.php';
			require_once WPF_DIR_PATH .'includes/admin/admin-functions.php';

		}

		// Autoload CRMs
		require_once WPF_DIR_PATH .'includes/crms/class-base.php';

		foreach( $this->get_crms() as $filename => $integration ) {
			if( file_exists( WPF_DIR_PATH . 'includes/crms/' . $filename . '/class-' . $filename . '.php' ) ) {
				require_once WPF_DIR_PATH . 'includes/crms/' . $filename . '/class-' . $filename . '.php';
			}
		}

	}

	/**
	 * Includes classes applicable for after the connection is configured
	 *
	 * @access private
	 * @return void
	 */

	private function integrations_includes() {

		require_once WPF_DIR_PATH . 'includes/class-user.php';
		require_once WPF_DIR_PATH . 'includes/class-api.php';
		require_once WPF_DIR_PATH . 'includes/class-ajax.php';
		require_once WPF_DIR_PATH . 'includes/class-access-control.php';

		if(is_admin()) {

			require_once WPF_DIR_PATH . 'includes/admin/class-updater.php';
			require_once WPF_DIR_PATH . 'includes/admin/class-meta-boxes.php';
			require_once WPF_DIR_PATH . 'includes/admin/class-user-profile.php';

		} else {

			require_once WPF_DIR_PATH . 'includes/admin/class-admin-bar.php';
			require_once WPF_DIR_PATH . 'includes/class-shortcodes.php';

		}

		// Autoload integrations
		require_once WPF_DIR_PATH . 'includes/integrations/class-base.php';

		foreach( $this->get_integrations() as $filename => $integration ) {
			if( file_exists( WPF_DIR_PATH . 'includes/integrations/class-' . $filename . '.php' ) ) {
				require_once WPF_DIR_PATH . 'includes/integrations/class-' . $filename . '.php';
			}
		}


	}

	/**
	 * Set up EDD updater
	 *
	 * @access public
	 * @return void
	 */

	public function updater() {

		if( ! is_admin() )
			return;

		$license_key = $this->settings->get( 'license_key' );
		$license_status = $this->settings->edd_check_license($license_key);

		if($license_status == 'valid') {
			
			// setup the updater
			$edd_updater = new WPF_Plugin_Updater( WPF_STORE_URL, __FILE__, array( 
					'version' 	=> WP_FUSION_VERSION, 		// current version number
					'license' 	=> $license_key, 			// license key (used get_option above to retrieve from DB)
					'item_name' => 'WP Fusion', 			// name of this plugin
					'author' 	=> 'Very Good Plugins'  	// author of this plugin
				)
			);

		} else {

			global $pagenow;

			if ( 'plugins.php' === $pagenow ) {
				add_action("after_plugin_row_" . WPF_PLUGIN_PATH, array( self::$instance, 'wpf_update_message' ), 10, 3);
			}
		}

	}

	/**
	 * Display update message
	 *
	 * @access public
	 * @return void
	 */

	public function wpf_update_message( $plugin_file, $plugin_data, $status ) {

		echo '<tr class="plugin-update-tr">';
			echo '<td colspan="3" class="plugin-update colspanchange">';
				echo '<div class="update-message notice inline notice-warning notice-alt">';
					echo '<p>Your WP Fusion License key is currently inactive or expired. <a href="' . get_admin_url() . './options-general.php?page=wpf-settings#setup">Activate your license key</a> or <a href="http://wpfusionplugin.com/" target="_blank">purchase a license</a> to enable automatic updates and support.</p>';
				echo '</div>';
			echo '</td>';
		echo '</tr>';

	}


	/*
	//
	//
	// DEPRECATED FUNCTIONS
	//
	//
	*/

	/**
	 * Logs error, to PHP error log or screen if WP_DEBUG is on
	 *
	 * @access private
	 * @return mixed
	 */

	private function log_deprecated( $function, $new_function ) {

		if ( WP_DEBUG ) {
			trigger_error( sprintf( __( 'Error: The function wp_fusion()->%1$s() was <strong>deprecated</strong> in WP Fusion 2.0. Please use %2$s instead.' ), $function, $new_function ) );
		} else {
			error_log( 'Error: The function wp_fusion()->' . $function . '() was deprecated in WP Fusion 2.0. Please use ' . $new_function . ' instead.');
		}

	}


	/**
	 * Connects to CRM (deprecated)
	 *
	 * @access public
	 * @return bool Whether or not connection succeeded
	 */

	public function connect() {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->crm->connect()');
		return wp_fusion()->crm->connect();

	}

	/**
	 * Get contact ID (deprecated)
	 *
	 * @access public
	 * @return int Contact ID for user
	 */

	public function get_contact_id( $user_id, $force_update = false) {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->user->get_contact_id()');
		return wp_fusion()->user->get_contact_id( $user_id, $force_update );

	}


	/**
	 * Updates a contact based on ID (deprecated)
	 *
	 * @access public
	 * @return bool Success or failure
	 */

	public function update_contact( $contact_id, $update_data ) {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->crm->update_contact()');
		return wp_fusion()->crm->update_contact( $contact_id, $update_data, false );

	}


	/**
	 * Updates a single meta field (deprecated)
	 *
	 * @access public
	 * @return bool Success or failure
	 */

	public function push_meta_data_single($meta_id, $user_id, $meta_key, $_meta_value ) {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->user->push_user_meta()');
		return wp_fusion()->crm->push_user_meta( $user_id, array( $meta_key => $_meta_value ) );

	}

	/**
	 * Gets tags for a contact (deprecated)
	 *
	 * @access public
	 * @return array Applied tags
	 */

	public function get_tags( $user_id, $force_update = false) {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->user->get_tags()');
		return wp_fusion()->user->get_tags( $user_id, $force_update );

	}

	/**
	 * Applies tags to a contact (deprecated)
	 *
	 * @access public
	 * @return bool Success or failure
	 */

	public function apply_tags( $tags, $user_id ) {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->user->apply_tags()');
		return wp_fusion()->user->apply_tags( $tags, $user_id );

	}

	/**
	 * Gets tags for a contact (deprecated)
	 *
	 * @access public
	 * @return bool Whether or not the user has the tag
	 */

	public function has_tag( $tag_id, $user_id ) {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->user->has_tag()');
		return wp_fusion()->user->has_tag( $tag_id, $user_id );

	}

	/**
	 * Create user (deprecated)
	 *
	 * @access public
	 * @return int Contact ID for new user
	 */

	public function create_user( $user_id ) {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->user->user_register()');
		return wp_fusion()->user->user_register( $user_id );

	}

	/**
	 * Import user (deprecated)
	 *
	 * @access public
	 * @return int Contact ID for new user
	 */

	public function import_user( $contact_id, $send_notification ) {

		self::$instance->log_deprecated(__FUNCTION__, 'wp_fusion()->user->import_user()');
		return wp_fusion()->user->import_user( $contact_id, $send_notification );

	}

}


/**
 * The main function responsible for returning the one true WP Fusion
 * Instance to functions everywhere.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 * Example: <?php $wpf = wp_fusion(); ?>
 *
 * @return object The one true WP Fusion Instance
 */

function wp_fusion() {
	return WP_Fusion::instance();
}

// Get WP Fusion Running
wp_fusion();