Your IP : 216.73.216.224


Current Path : /var/www/html/plugins/content/membershipplans/src/Extension/
Upload File :
Current File : /var/www/html/plugins/content/membershipplans/src/Extension/MembershipPlans.php

<?php
/**
 * @package        Joomla
 * @subpackage     Membership Pro
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2012 - 2026 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

namespace JoomDonation\MembershipPro\Plugin\Content\MembershipPlans\Extension;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\DispatcherInterface;
use Joomla\Event\Event;
use Joomla\Event\SubscriberInterface;

defined('_JEXEC') or die;

class MembershipPlans extends CMSPlugin implements SubscriberInterface
{
	public static function getSubscribedEvents(): array
	{
		return [
			'onContentPrepare' => 'onContentPrepare',
		];
	}

	/**
	 * Constructor.
	 *
	 * @param   DispatcherInterface  $dispatcher  The dispatcher
	 * @param   array                $config      An optional associative array of configuration settings
	 */
	public function __construct(DispatcherInterface $dispatcher, array $config)
	{
		parent::__construct($dispatcher, $config);
	}

	/**
	 * Parse article and display plans if configured
	 *
	 * @param   Event  $event
	 *
	 * @return void
	 */
	public function onContentPrepare(Event $event): void
	{
		[$context, $article, $params, $limitstart] = array_values($event->getArguments());

		if (!str_contains($article->text, '{membershipplans'))
		{
			return;
		}

		$regex         = '#{membershipplans ids="(.*?)"}#s';
		$article->text = preg_replace_callback($regex, [&$this, 'displayPlans'], $article->text);
	}

	/**
	 * Replace callback function
	 *
	 * @param   array  $matches
	 *
	 * @return string
	 * @throws \Exception
	 */
	private function displayPlans($matches)
	{
		require_once JPATH_ADMINISTRATOR . '/components/com_osmembership/loader.php';

		$planIds = $matches[1];
		$layout  = $this->params->get('layout_type', 'default');
		\OSMembershipHelper::loadLanguage();
		$request = [
			'option'          => 'com_osmembership',
			'view'            => 'plans',
			'layout'          => $layout,
			'filter_plan_ids' => $planIds,
			'limit'           => 0,
			'hmvc_call'       => 1,
			'Itemid'          => \OSMembershipHelper::getItemid(),
		];
		$input   = new \MPFInput($request);
		$config  = [
			'default_controller_class' => 'OSMembershipController',
			'default_view'             => 'plans',
			'class_prefix'             => 'OSMembership',
			'language_prefix'          => 'OSM',
			'remember_states'          => false,
			'ignore_request'           => false,
		];

		ob_start();

		//Initialize the controller, execute the task
		\MPFController::getInstance('com_osmembership', $input, $config)
			->execute();

		return ob_get_clean();
	}

	/**
	 * Register listeners
	 *
	 * @return void
	 */
	public function registerListeners()
	{
		if (!ComponentHelper::isEnabled('com_osmembership'))
		{
			return;
		}

		if (!$this->getApplication()->isClient('site'))
		{
			return;
		}

		parent::registerListeners();
	}
}