LLMS_Abstract_Integration

LifterLMS Integration abstract class


Description Description


Source Source

File: includes/abstracts/llms.abstract.integration.php

abstract class LLMS_Abstract_Integration extends LLMS_Abstract_Options_Data {

	/**
	 * Integration ID
	 * Defined by extending class as a variable
	 *
	 * @var  string
	 */
	public $id = '';

	/**
	 * Integration Title
	 * Should be defined by extending class in configure() function (so it can be i18n)
	 *
	 * @var  string
	 */
	public $title = '';

	/**
	 * Integration Description
	 * Should be defined by extending class in configure() function (so it can be i18n)
	 *
	 * @var  string
	 */
	public $description = '';

	/**
	 * Integration Missing Dependencies Description
	 * Should be defined by extending class in configure() function (so it can be i18n)
	 * Displays on the settings screen when $this->is_installed() is false
	 * to help users identify what requirements are missing
	 *
	 * @var  string
	 */
	public $description_missing = '';

	/**
	 * Reference to the integration plugin's main plugin file basename
	 *
	 * In the configure() method call `plugin_basename()` on the main plugin file.
	 *
	 * @var string
	 */
	protected $plugin_basename = '';

	/**
	 * Integration Priority
	 * Determines the order of the settings on the Integrations settings table
	 * Don't be arrogant developers, your integration may not be the most important to the user
	 * even if it is the most important to you
	 *
	 * Core integrations fire at 5
	 *
	 * @var integer
	 */
	protected $priority = 20;

	/**
	 * Constructor
	 *
	 * @return   void
	 * @since    3.8.0
	 * @version  3.18.2
	 */
	public function __construct() {

		$this->configure();
		add_filter( 'lifterlms_integrations_settings_' . $this->id, array( $this, 'add_settings' ), $this->priority, 1 );
		do_action( 'llms_integration_' . $this->id . '_init', $this );

		if ( ! empty( $this->plugin_basename ) ) {
			add_action( "plugin_action_links_{$this->plugin_basename}", array( $this, 'plugin_action_links' ), 100, 4 );
		}

	}

	/**
	 * Configure the integration
	 * Do things like configure ID and title here
	 *
	 * @return   void
	 * @since    3.8.0
	 * @version  3.8.0
	 */
	abstract protected function configure();

	/**
	 * Merge the default abstract settings with the actual integration settings
	 * Automatically called via filter upon construction
	 *
	 * @param    array $settings   existing settings from other integrations
	 * @return   array
	 * @since    3.17.8
	 * @version  3.17.8
	 */
	public function add_settings( $settings ) {
		return array_merge( $settings, $this->get_settings() );
	}

	/**
	 * Get additional settings specific to the integration
	 * extending classes should override this with the settings
	 * specific to the integration
	 *
	 * @return   array
	 * @since    3.8.0
	 * @version  3.8.0
	 */
	protected function get_integration_settings() {
		return array();
	}

	/**
	 * Retrieve the integration priority property.
	 *
	 * @since 3.33.1
	 *
	 * @return int
	 */
	public function get_priority() {
		return $this->priority;
	}

	/**
	 * Retrieve an array of integration related settings
	 *
	 * @return   array
	 * @since    3.8.0
	 * @version  3.21.1
	 */
	protected function get_settings() {

		$settings[] = array(
			'type'  => 'sectionstart',
			'id'    => 'llms_integration_' . $this->id . '_start',
			'class' => 'top',
		);
		$settings[] = array(
			'desc'  => $this->description,
			'id'    => 'llms_integration_' . $this->id . '_title',
			'title' => $this->title,
			'type'  => 'title',
		);
		$settings[] = array(
			'desc'    => __( 'Check to enable this integration.', 'lifterlms' ),
			'default' => 'no',
			'id'      => $this->get_option_name( 'enabled' ),
			'type'    => 'checkbox',
			'title'   => __( 'Enable / Disable', 'lifterlms' ),
		);
		if ( ! $this->is_installed() && ! empty( $this->description_missing ) ) {
			$settings[] = array(
				'type'  => 'custom-html',
				'value' => '<em>' . $this->description_missing . '</em>',
			);
		}
		$settings   = array_merge( $settings, $this->get_integration_settings() );
		$settings[] = array(
			'type' => 'sectionend',
			'id'   => 'llms_integration_' . $this->id . '_end',
		);

		return apply_filters( 'llms_integration_' . $this->id . '_get_settings', $settings, $this );
	}

	/**
	 * @return   string
	 * @since    3.8.0
	 * @version  3.8.0
	 */
	protected function get_option_prefix() {
		return $this->option_prefix . 'integration_' . $this->id . '_';
	}

	/**
	 * Determine if the integration is enabled via the checkbox on the admin panel
	 * and the necessary plugin (if any) is installed and activated
	 *
	 * @return   boolean
	 * @since    3.0.0
	 * @version  3.17.8
	 */
	public function is_available() {
		return ( $this->is_installed() && $this->is_enabled() );
	}

	/**
	 * Determine if the integration had been enabled via checkbox
	 *
	 * @return   boolean
	 * @since    3.0.0
	 * @version  3.8.0
	 */
	public function is_enabled() {
		return ( 'yes' === $this->get_option( 'enabled', 'no' ) );
	}

	/**
	 * Determine if the related plugin, theme, 3rd party is
	 * installed and activated
	 * extending classes should override this to perform dependency checks
	 *
	 * @return   boolean
	 * @since    3.0.0
	 * @version  3.8.0
	 */
	public function is_installed() {
		return true;
	}

	/**
	 * Add plugin settings Action Links
	 *
	 * @since 3.37.9
	 *
	 * @param string[] $links Existing action links.
	 * @param string   $file Path to the plugin file, relative to the plugin directory.
	 * @param array    $data Plugin data
	 * @param string   $context Plugin's content (eg: active, invactive, etc...);
	 * @return string[]
	 */
	public function plugin_action_links( $links, $file, $data, $context ) {

		// Only add links if the plugin is active.
		if ( in_array( $context, array( 'all', 'active' ), true ) ) {

			$url = add_query_arg(
				array(
					'page'    => 'llms-settings',
					'tab'     => 'integrations',
					'section' => $this->id,
				),
				admin_url( 'admin.php' )
			);

			$links[] = '<a href="' . esc_url( $url ) . '">' . _x( 'Settings', 'Link text for integration plugin settings', 'lifterlms' ) . '</a>';

		}

		return $links;
	}

}

Top ↑

Changelog Changelog

Changelog
Version Description
3.37.9 Added automatically generated "Settings" link to plugins screen.
3.33.1 Added get_priority method to allow reading of the protected priority property.
3.21.1 Updated.
3.0.0 Introduced.


Top ↑

Methods Methods

  • __construct — Constructor
  • add_settings — Merge the default abstract settings with the actual integration settings Automatically called via filter upon construction
  • configure — Configure the integration Do things like configure ID and title here
  • get_integration_settings — Get additional settings specific to the integration extending classes should override this with the settings specific to the integration
  • get_option_prefix
  • get_priority — Retrieve the integration priority property.
  • get_settings — Retrieve an array of integration related settings
  • is_available — Determine if the integration is enabled via the checkbox on the admin panel and the necessary plugin (if any) is installed and activated
  • is_enabled — Determine if the integration had been enabled via checkbox
  • is_installed — Determine if the related plugin, theme, 3rd party is installed and activated extending classes should override this to perform dependency checks
  • plugin_action_links — Add plugin settings Action Links

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.