<?php
/**
 * Add GravityView integration to LifterLMS.
 *
 * @since 2.20
 * @since 3.0.0 Migrated to PSR-4 namespace.
 * @license GPL2+
 *
 * @package GravityKit\GravityView\Integration
 */

namespace GravityKit\GravityView\Integration;

/**
 * Registers the GravityView integration with LifterLMS.
 *
 * Unlike most integrations, LifterLMS requires a class name string passed to the
 * `lifterlms_integrations` filter. That class must extend `LLMS_Abstract_Integration`,
 * which only exists when LifterLMS is active. The inner class is defined inside the
 * filter callback so it is only parsed when LifterLMS is loaded.
 *
 * @since 2.20
 * @since 3.0.0 Migrated to PSR-4 namespace.
 */
final class LifterLMS extends AbstractPluginHooks {

	/**
	 * @since 2.20
	 * @var string
	 */
	protected $function_name = 'llms';

	/**
	 * @since 3.0.0
	 */
	protected function add_hooks() {
		add_filter( 'lifterlms_integrations', [ $this, 'register_integration' ], 20 );
	}

	/**
	 * Register the GravityView integration class with LifterLMS.
	 *
	 * The LLMS_Integration_GravityView class is defined here so that
	 * LLMS_Abstract_Integration is guaranteed to exist when PHP parses it.
	 *
	 * @since 2.20
	 * @since 3.0.0 Refactored from standalone file-level callback.
	 *
	 * @param array $integrations LifterLMS integration class names.
	 *
	 * @return array
	 */
	public function register_integration( $integrations = [] ) {
		if ( ! class_exists( 'LLMS_Integration_GravityView' ) ) {
			$this->define_integration_class();
		}

		$integrations[] = 'LLMS_Integration_GravityView';

		return $integrations;
	}

	/**
	 * Define the LLMS_Integration_GravityView class.
	 *
	 * Extracted to a method so the class is only defined when LifterLMS is active
	 * and the filter fires. The class name is dictated by the LifterLMS API.
	 *
	 * @since 3.0.0
	 *
	 * @return void
	 */
	private function define_integration_class() {
		require_once __DIR__ . '/LifterLMS/Integration.php';
	}
}
