हुक और प्लगइन डेवलपमेंट
TubePress को इसके WordPress-शैली एक्शन और फ़िल्टर हुक सिस्टम से विस्तृत करें: 15+ हुक पॉइंट, प्लगइन जीवनचक्र, और अपना खुद का प्लगइन कैसे बनाएँ व पैकेज करें।
Plugins TubePress में core को बिना छुए features जोड़ने का तरीका है। ये CMS में एक छोटे WordPress-style system के ज़रिए hook होते हैं जिसमें actions (fire-and-forget events) और filters (किसी value को modify करके return करना) शामिल हैं। तीन bundled plugins — Age Gate, Backup Pro और CTR Ranking — पूरी तरह उसी API से बने हैं जो यहाँ documented है।
Hook सिस्टम
Static HookSystem class दोनों प्रकार के hooks को manage करती है। Callbacks ascending priority order में चलते हैं (default 10); callback द्वारा throw किया गया कोई भी exception catch और log किया जाता है, इसलिए एक खराब plugin कभी भी page को down नहीं कर सकता।
// Actions — do something when an event fires
HookSystem::addAction('head.meta', function () {
echo '<meta name="rating" content="adult">';
}, 10);
// Filters — receive a value, return a (possibly) changed value
HookSystem::addFilter('theme.template_data', function ($data, $template) {
if ($template === 'home') {
$data['promo'] = Promo::current();
}
return $data;
});| Method | उद्देश्य |
|---|---|
addAction($hook, $cb, $priority = 10) | Action listener register करें। |
doAction($hook, ...$args) | Action fire करें (core इन्हें call करता है)। |
addFilter($hook, $cb, $priority = 10) | Filter register करें। |
applyFilter($hook, $value, ...$args) | किसी value को उसके filters से pass करके return करें। |
hasAction() / hasFilter() | जाँचें कि कोई सुन रहा है या नहीं। |
removeAction() / removeFilter() | Hook के सभी listeners को detach करें। |
उपलब्ध Hook Points
Core इन hooks को fire करता है। आप plugin से अपने खुद के hooks भी define और fire कर सकते हैं।
Actions
| Hook | कब Fire होता है |
|---|---|
head.meta | <head> के अंदर, CSS के बाद — meta tags, verification tags आदि जोड़ें। |
footer.scripts | </body> से पहले, JS के बाद — analytics या widgets जोड़ें। |
routes.registered | सभी core routes register होने के बाद, page catch-all से पहले। |
router.before_dispatch | Matched handler चलने से ठीक पहले। Arg: resolved path। |
Filters
| Hook | Modifies (args) |
|---|---|
theme.template_data | Template render होने से पहले data array। Args: ($data, $templateName)। |
head.css | Combined CSS output string। |
footer.scripts | Combined JS output string। |
video.card.html | पूरा video-card HTML। Args: ($html, $video)। इसे replace करने के लिए एक non-empty string return करें। |
Plugin की संरचना
Plugin, plugins/ के अंतर्गत एक folder है जिसमें एक manifest और PluginInterface implement करने वाली एक class होती है।
plugins/myplugin/
├── plugin.json Manifest (name, slug, version, …)
├── myplugin.php Main class implementing PluginInterface
├── icon.svg Optional icon shown in the admin
└── … Your assets, templates, migrationsManifest bundled plugins को mirror करता है:
{
"name": "My Plugin",
"slug": "myplugin",
"description": "What it does, in one sentence.",
"version": "1.0.0",
"author": "You",
"requires": "1.0.0",
"icon": "icon.svg"
}Main class PluginInterface के छह lifecycle methods implement करती है:
| Method | कब Call होता है |
|---|---|
boot() | Plugin active रहने पर हर request — यहाँ hooks, routes और assets register करें। |
activate() | Admin plugin को enable करता है। |
deactivate() | Admin इसे disable करता है। |
install() | पहली activation — tables बनाएं, settings seed करें। |
uninstall() | Plugin हटाया जाता है — cleanup करें। |
info() | Manifest data को array के रूप में return करता है। |
Routes & Assets Register करना
अपनी सभी wiring boot() में करें:
public function boot(): void
{
// A front-end route
Router::get('/promo/{code}', [PromoController::class, 'show']);
// Inject a meta tag
HookSystem::addAction('head.meta', [$this, 'meta']);
// Tweak every video card
HookSystem::addFilter('video.card.html', [$this, 'badge'], 20);
}Plugin में Scheduled Work
boot() से CronManager के साथ एक recurring task register करें — यह built-in pseudo-cron पर चलता है, इसलिए कोई system crontab आवश्यक नहीं है। CTR plugin इसी तरह scores recalculate करता है और Backup Pro scheduled backups चलाता है।
Reference के रूप में Bundled Plugins
वितरण & Licensing
आप plugin को private रख सकते हैं, उसे freely share कर सकते हैं, या marketplace के माध्यम से बेच सकते हैं, जो signed licensing और per-domain activation जोड़ता है। Presentation side पर इसके equivalent के लिए Theme development देखें, और template code के लिए उपलब्ध सब कुछ के लिए Theme API reference देखें।
अभी भी अटके हैं?
अपने डैशबोर्ड से टिकट खोलें और हमारी टीम आपकी मदद करेगी।