class.llms.playnice.php 4.5 KB
Newer Older
cyrille's avatar
cyrille committed
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
<?php
/**
 * Make LifterLMS play nicely with other plugins, themes, & webhosts
 *
 * * * * * * * * * * * * * * * * * *
 * True, there is no joy           *
 * in software conflicts (or war)  *
 * Here we are, trying             *
 * * * * * * * * * * * * * * * * * *
 *
 * @package LifterLMS/Classes
 *
 * @since 3.1.3
 * @version 5.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LLMS_PlayNice class
 *
 * @since 3.1.3
 * @since 3.31.0 Resolve dashboard endpoint 404s resulting from changes in WC 3.6.
 * @since 3.37.17 Changed the way we handle the dashboard endpoints conflict, using a different wc filter hook.
 *                Deprecated `LLMS_PlayNice::wc_is_account_page()`.
 * @since 3.37.18 Resolve Divi/WC conflict encountered using the frontend pagebuilder on courses and memberships.
 * @since 4.0.0 Removed previously deprecated method `LLMS_PlayNice::wc_is_account_page()`.
 *              Remove Divi Frontend Builder WC conflict code.
 */
class LLMS_PlayNice {

	/**
	 * Hold temporary variables used by methods in this class.
	 *
	 * @var array
	 */
	private $temp_vars = array();

	/**
	 * Constructor
	 *
	 * @since 3.1.3
	 * @since 3.31.0 Add `plugins_loaded` hook.
	 *
	 * @return void
	 */
	public function __construct() {

		// Optimize press live editor initialization.
		add_action( 'op_liveeditor_init', array( $this, 'wp_optimizepress_live_editor' ) );

		// WPEngine heartbeat fix.
		add_filter( 'wpe_heartbeat_allowed_pages', array( $this, 'wpe_heartbeat_allowed_pages' ) );

		// Load other playnice things based on the presence of other plugins.
		add_action( 'init', array( $this, 'plugins_loaded' ), 11 );

	}

	/**
	 * Conditionally add hooks after the other plugin is loaded.
	 *
	 * @since 3.31.0
	 * @since 3.37.17 Changed the way we handle endpoints conflict, using a different WC filter hook.
	 * @since 3.37.18 Add fix for Divi Frontend-Builder WC conflict.
	 * @since 4.0.0 Remove Divi Frontend Builder WC conflict code.
	 *
	 * @return void
	 */
	public function plugins_loaded() {

		$wc_exists = function_exists( 'WC' );

		if ( $wc_exists ) {
			add_filter( 'woocommerce_account_endpoint_page_not_found', array( $this, 'wc_account_endpoint_page_not_found' ) );
		}

	}

	/**
	 * Allow our dashboard endpoints sharing a query var with WC to function
	 *
	 * Inform WC that it should not force a 404 because we're on a valid endpoint.
	 *
	 * @since 3.37.17
	 *
	 * @link https://github.com/gocodebox/lifterlms/issues/849
	 *
	 * @param bool $is_page_not_found True from `woocommerce_account_endpoint_page_not_found` filter.
	 * @return bool
	 */
	public function wc_account_endpoint_page_not_found( $is_page_not_found ) {

		if ( is_llms_account_page() && is_wc_endpoint_url() ) {
			$is_page_not_found = false;
		}

		return $is_page_not_found;

	}

	/**
	 * OptimizePress LiveEditor fix
	 *
	 * The live editor for OptimizePress does not work because it is trying to load a frontend environment
	 * in the admin area and needs access lifterlms frontend files.
	 *
	 * This function loads all frontend files when the optimizepress live editor is initialized.
	 *
	 * @since 3.2.2
	 * @since 3.19.6 Unknown.
	 * @since 4.0.0 Removed inclusion of removed 'class.llms.person.php' file.
	 * @since 5.0.0 Remove inclusion of removed files:
	 *                    + forms/frontend/class.llms.frontend.forms.php
	 *                    + forms/frontend/class.llms.frontend.password.php
	 *
	 * @return void
	 */
	public function wp_optimizepress_live_editor() {

		// These files are necessary to get optimizepress ajax to play nicely in the liveeditor.
		include_once 'class.llms.ajax.php';
		include_once 'class.llms.ajax.handler.php';

		// These files are all necessary to get the liveeditor to open.
		include_once 'llms.template.functions.php';
		include_once 'class.llms.https.php';

		include_once 'class.llms.template.loader.php';
		include_once 'class.llms.frontend.assets.php';
		include_once 'shortcodes/class.llms.shortcodes.php';

		include_once 'shortcodes/class.llms.shortcode.my.account.php';
		include_once 'shortcodes/class.llms.shortcode.checkout.php';

	}

	/**
	 * WPE blocks the WordPress Heartbeat script from being loaded
	 *
	 * Event when it's explicitly defined as a dependency.
	 *
	 * @since 3.16.4
	 *
	 * @param array $pages List of pages that the heartbeat is allowed to load on.
	 * @return array
	 */
	public function wpe_heartbeat_allowed_pages( $pages ) {

		if ( is_admin() && isset( $_GET['page'] ) && 'llms-course-builder' === $_GET['page'] ) {

			$pages[] = 'admin.php';

		}

		return $pages;

	}

}

return new LLMS_PlayNice();