تخطَّ إلى المحتوى

إضافات الطرف الثالث

يبني 4SEF روابط SEF لمكوّن معيّن باستخدام جزء مخصص من الشيفرة، مصمم خصيصًا لذلك المكوّن. ويأتي بدعم لجميع أنواع المحتوى الأصلية في Joomla، بالإضافة إلى بعض إضافات الطرف الثالث الشائعة الاستخدام.

بالنسبة للمكوّنات التي لا توجد لها إضافة دعم خاصة، يعود 4SEF إلى استخدام ملف router.php الخاص بالمكوّن، والمخصص عادةً لنظام روابط SEF في Joomla. هذه العملية تلقائية وستعمل في معظم الحالات.

إذا أردت توفير مجموعة روابط أكثر دقة لمكوّن تستخدمه أو بنيته، يمكنك إنشاء مشغّل روابط SEF لـ 4SEF لهذا الغرض.

المشغّل هو في حده الأدنى ملف صنف PHP واحد، يمكنك تخزينه في أي مكان تريده. يمكن أن يكون جزءًا من مكوّنك الخاص على سبيل المثال، أو داخل إضافة مخصصة (انظر الفقرة أدناه، تحميل المشغّل).

يوسّع المشغّل صنفًا أساسيًا يوفره 4SEF، وله 3 أدوار رئيسية:

  • بناء رابط SEF استنادًا إلى متغيرات الرابط غير SEF، على غرار ملف router.php الخاص بـ Joomla
  • بناء الرابط غير SEF “الموحّد”. يتولى 4SEF معظم ذلك بالفعل، لكنك قد ترغب في إزالة بعض متغيرات الاستعلام الخاصة بالمكوّن.
  • اختياريًا، تحديد ما إذا كان يجب ترك رابط SEF بصيغة غير SEF، وهو أمر مفيد أحيانًا ويوفر المساحة
<?php
/**
* Project: 4SEF
*
* @package 4SEF
* @copyright Copyright Weeblr llc - 2022
* @author Yannick Gaultier - Weeblr llc
* @license GNU General Public License version 3; see LICENSE.md
* @version 1.0.0
* @date 2022-06-03
*/
namespace Weeblr\Forsef\Platform\Extensions;
use Joomla\CMS\Uri;
use Weeblr\Forsef\Data;
use Weeblr\Forsef\Helper;
use Weeblr\Wblib\Forsef\Wb;
use Weeblr\Wblib\Forsef\System;
use Weeblr\Wblib\Forsef\Factory;
// no direct access
defined('_JEXEC') || die();
/**
* Sample plugin to build URL for a com_sample component.
*
* - Drop the "com_" at the start from the component to name the class
* - Below are only the most important methods. For more advanced logic,
* be sure to check the built-in plugins in
*
* /plugins/system/forsef/platform/extensions
*
*/
class Sample extends Base
{
/**
* @var Helper\Platform
*/
private $platformHelper;
/**
* Stores factory instance.
*
* @param string $option Extension this applies to, in com_xxx format.
* @param array $options Can inject custom factory and platform.
*/
public function __construct($option, $options = [])
{
parent::__construct($option, $options);
$this->platformHelper = $this->factory
->getA(Helper\Platform::class);
}
/**
* Builds the SEF URL for a non-sef.
*
* @param Uri\Uri $uriToBuild
* @param Uri\Uri $platformUri
* @param Uri\Uri $originalUri
*
* @return \array
* @throws \Exception
*/
public function build($uriToBuild, $platformUri, $originalUri)
{
$sefSegments = parent::build($uriToBuild, $platformUri, $originalUri);
// Read whatever variables you need to build your SEF URL.
$view = $uriToBuild->getVar('view');
$layout = $uriToBuild->getVar('layout');
$id = $uriToBuild->getVar('id');
$Itemid = $uriToBuild->getVar('Itemid');
// and add strings to the $sefSegments array to build the URL.
// $sefSegments[] = 'something'
// Add a / as the last element of the sefSegments array to prevent the
// .html suffix to be appended to the URL later on (if set in 4SEF configuration).
// $sefSegments[] = '/';
return $sefSegments;
}
/**
* Participate in building a normalized non-sef URL based on an incoming URI. Query vars values are URL-encoded.
* Stripping slugs, sorting vars and possibly other things are taken care globally. Only plugin-specific
* vars processing should happen here. For instance, stripping pagination variables if the plugin
* handles pagination dynamically.
*
* @param array $vars
* @return array
*/
public function buildNormalizedNonSef($vars)
{
return $this->nonSefHelper->stripFeedVars(
parent::buildNormalizedNonSef(
$vars
)
);
}
/**
* Check if passed URI is for an extension configured to be left non-sef.
*
* @param Uri\Uri $uriToBuild
* @return bool
*/
public function shouldLeaveNonSef($uriToBuild)
{
// read whatever variables you need to decide whether a
// URL must be left non-sef
$view = $uriToBuild->getVar('view');
$task = $uriToBuild->getVar('task');
$layout = $uriToBuild->getVar('layout');
$a_id = $uriToBuild->getVar('a_id');
// then check these variables and return true
// if this URL must be left non-sef.
// by default, all URLs are made SEF
return false;
}
}

بما أن المشغّل مجرد صنف PHP محدد، سيكتشفه 4SEF ويستخدمه تلقائيًا عند الحاجة، ما دام ملف PHP هذا مُضمَّنًا مرة واحدة.

هناك 3 طرق للقيام بذلك، كما هو موضّح أدناه. نقترح بناء إضافة Joomla حقيقية لتغليف مشغّلك: فهذه الطريقة الأنسب للمستخدمين النهائيين، وللتحديث وغير ذلك، رغم أن الخيارات الثلاثة كلها صالحة تمامًا.

يمكنك ببساطة استخدام include_once 'my_4sef_driver.php' في أي مكان في إضافتك لجعل المشغّل متاحًا. إذا كانت تسميته صحيحة (انظر المثال أعلاه)، يستطيع 4SEF اكتشافه واستخدامه.

الاستجابة لحدث تحميل إضافات 4SEF

Section titled “الاستجابة لحدث تحميل إضافات 4SEF”

قبل أن يحتاج إلى بناء أول رابط SEF مباشرة، يُطلق 4SEF حدثًا ليتيح لك تضمين مشغّلك. يمكنك تحميل المشغّل بإضافة الشيفرة التالية إلى إضافتك:

<?php
// Your extension own code here
// Include use statement for 4SEF factory class
use Weeblr\Forsef\Factory as ForsefFactory;
// somewhere in your extension, add the following to respond to 4SEF event:
ForsefFactory::get()->getThe('hook')->add(
'forsef_on_load_plugins',
function () {
// include your plugin file here
// Sample code to include your driver
$filename = '/absolute/path/to/your/driver/componentname.php';
if (file_exists($filename))
{
include_once $filename;
}
}
);

يمكنك بناء إضافة Joomla حقيقية لتغليف مشغّلك وتسليمه إلى المستخدمين. وهذا بسيط جدًا في الواقع، وسيتكفّل بتحميل مشغّلك في وقت مبكر بما يكفي.

إليك بنية مثل هذه الإضافة:

بنية مجلدات إضافة روابط لـ 4SEF

يحتوي الملف /plugin/sample.php على مشغّلك، كما هو موضّح في فقرة مثال على شيفرة المشغّل أعلاه.

إليك ملف البيان، sample.xml:

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" group="forsef" method="upgrade">
<name>PLG_FORSEF_SAMPLE</name>
<author>Yannick Gaultier - Weeblr llc</author>
<creationDate>2022-06-03</creationDate>
<copyright>Copyright Weeblr llc - 2022</copyright>
<license>GNU General Public License version 3; see LICENSE.md</license>
<authorEmail>[email protected]</authorEmail>
<authorUrl>https://weeblr.com/</authorUrl>
<version>1.0.0</version>
<description>PLG_FORSEF_SAMPLE_XML_DESCRIPTION</description>
<files>
<filename plugin="sample">sample.php</filename>
<folder plugin="sample">plugin</folder>
</files>
</extension>

وإليك ملف إضافة Joomla الرئيسي، sample.php:

<?php
/**
* Project: 4SEF
*
* @package 4SEF
* @copyright Copyright Weeblr llc - 2022
* @author Yannick Gaultier - Weeblr llc
* @license GNU General Public License version 3; see LICENSE.md
* @version 1.0.0
* @date 2022-06-03
*/
defined('_JEXEC') or die;
use Weeblr\Forsef\Factory as ForsefFactory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\DispatcherInterface;
/**
* 4SEF - A loader for a 4SEF SEF URL plugin.
*
* @since 1.0.0
*/
class PlgForsefSample extends CMSPlugin
{
/**
* @var string location of plugin file from
*/
private $pluginFileName = '/plugin/sample.php';
/**
* Constructor
*
* @param DispatcherInterface &$subject The object to observe
* @param array $config An optional associative array of configuration settings.
* Recognized key values include 'name', 'group', 'params', 'language'
* (this list is not meant to be comprehensive).
*
* @since 1.5
*/
public function __construct(&$subject, $config = array())
{
parent::__construct($subject, $config);
ForsefFactory::get()->getThe('hook')->add(
'forsef_on_load_plugins',
function () {
$filename = __DIR__ . $this->pluginFileName;
if (file_exists($filename))
{
include_once $filename;
}
}
);
}
}