इसे छोड़कर कंटेंट पर जाएं

प्रकाशक - हुक्स एण्ड इवेंट्स

प्रकाशक हुक और घटनाएँ

Section titled “प्रकाशक हुक और घटनाएँ”

इवेंट, हुक और प्लगइन्स का उपयोग करके प्रकाशक कार्यक्षमता का विस्तार करने के लिए संपूर्ण मार्गदर्शिका।


इवेंट सिस्टम अवलोकन

Section titled “इवेंट सिस्टम अवलोकन”

घटनाएँ क्या हैं?

Section titled “घटनाएँ क्या हैं?”

ईवेंट अन्य मॉड्यूल को प्रकाशक की कार्रवाइयों पर प्रतिक्रिया करने की अनुमति देते हैं:

Publisher Action → Trigger Event → Other modules listen/react
Examples:
- Article created → Send notification email
- Article published → Update social media
- Comment posted → Notify author
- Category created → Update search index
graph LR
A[Action in Publisher] -->|Trigger| B[Event fired]
B -->|Listeners notified| C[Other modules react]
C -->|Execute callbacks| D[Plugins/Hooks run]

आइटम (अनुच्छेद) घटनाएँ

Section titled “आइटम (अनुच्छेद) घटनाएँ”

प्रकाशक.आइटम.बनाया गया

Section titled “प्रकाशक.आइटम.बनाया गया”

नया लेख बनने पर निकाल दिया जाता है।

// Trigger point in Publisher
xoops_events()->trigger('publisher.item.created', array(
'item' => $item,
'itemid' => $item->getVar('itemid'),
'title' => $item->getVar('title'),
'uid' => $item->getVar('uid')
));

उदाहरण श्रोता:

// Listen for article creation
xoops_events()->attach('publisher.item.created', 'onArticleCreated');
function onArticleCreated($item) {
$itemId = $item['itemid'];
$title = $item['title'];
$uid = $item['uid'];
// Send email notification
sendEmailNotification($uid, "New article: $title");
// Log activity
logActivity('Article created', $itemId);
// Update search index
updateSearchIndex($itemId);
}

प्रकाशक.आइटम.अद्यतन

Section titled “प्रकाशक.आइटम.अद्यतन”

जब कोई लेख अद्यतन किया जाता है तो निकाल दिया जाता है।

xoops_events()->trigger('publisher.item.updated', array(
'item' => $item,
'itemid' => $itemId,
'changes' => $changes
));

प्रकाशक.आइटम.हटाया गया

Section titled “प्रकाशक.आइटम.हटाया गया”

जब कोई लेख हटा दिया जाता है तो निकाल दिया जाता है।

xoops_events()->trigger('publisher.item.deleted', array(
'itemid' => $itemId,
'title' => $title,
'categoryid' => $categoryId
));

प्रकाशक.आइटम.प्रकाशित

Section titled “प्रकाशक.आइटम.प्रकाशित”

जब लेख की स्थिति प्रकाशित में बदल जाती है तो निकाल दिया जाता है।

xoops_events()->trigger('publisher.item.published', array(
'item' => $item,
'itemid' => $itemId
));

प्रकाशक.आइटम.अनुमोदित

Section titled “प्रकाशक.आइटम.अनुमोदित”

लंबित लेख स्वीकृत होने पर निकाल दिया जाता है।

xoops_events()->trigger('publisher.item.approved', array(
'item' => $item,
'itemid' => $itemId,
'uid' => $uid
));

प्रकाशक.आइटम.अस्वीकृत

Section titled “प्रकाशक.आइटम.अस्वीकृत”

लेख अस्वीकृत होने पर निकाल दिया गया।

xoops_events()->trigger('publisher.item.rejected', array(
'item' => $item,
'itemid' => $itemId,
'reason' => $reason
));

प्रकाशक.श्रेणी.बनाया गया

Section titled “प्रकाशक.श्रेणी.बनाया गया”

श्रेणी निर्मित होने पर निकाल दिया गया।

xoops_events()->trigger('publisher.category.created', array(
'category' => $category,
'categoryid' => $categoryId,
'name' => $name
));

प्रकाशक.श्रेणी.अद्यतन

Section titled “प्रकाशक.श्रेणी.अद्यतन”

श्रेणी अद्यतन होने पर निकाल दिया गया।

xoops_events()->trigger('publisher.category.updated', array(
'category' => $category,
'categoryid' => $categoryId
));

प्रकाशक.श्रेणी.हटाया गया

Section titled “प्रकाशक.श्रेणी.हटाया गया”

श्रेणी हटाए जाने पर निकाल दिया गया.

xoops_events()->trigger('publisher.category.deleted', array(
'categoryid' => $categoryId,
'name' => $name,
'itemCount' => $itemCount
));

प्रकाशक.टिप्पणी.बनाया गया

Section titled “प्रकाशक.टिप्पणी.बनाया गया”

टिप्पणी पोस्ट होने पर निकाल दिया गया.

xoops_events()->trigger('publisher.comment.created', array(
'comment' => $comment,
'commentid' => $commentId,
'itemid' => $itemId
));

प्रकाशक.टिप्पणी.अनुमोदित

Section titled “प्रकाशक.टिप्पणी.अनुमोदित”

टिप्पणी स्वीकृत होने पर निकाल दिया गया।

xoops_events()->trigger('publisher.comment.approved', array(
'comment' => $comment,
'commentid' => $commentId
));

प्रकाशक.टिप्पणी.हटाई गई

Section titled “प्रकाशक.टिप्पणी.हटाई गई”

टिप्पणी हटा दिए जाने पर निकाल दिया गया.

xoops_events()->trigger('publisher.comment.deleted', array(
'commentid' => $commentId,
'itemid' => $itemId
));

इवेंट श्रोता पंजीकृत करें

Section titled “इवेंट श्रोता पंजीकृत करें”

आपके मॉड्यूल या प्लगइन में:

<?php
// Register listener in xoops_version.php or initialization file
xoops_events()->attach(
'publisher.item.created',
array('MyModuleListener', 'onPublisherItemCreated')
);
// Or use function name
xoops_events()->attach(
'publisher.item.created',
'my_module_on_item_created'
);
?>

श्रोता वर्ग पद्धति

Section titled “श्रोता वर्ग पद्धति”
<?php
class MyModuleListener {
public static function onPublisherItemCreated($data) {
$itemId = $data['itemid'];
$title = $data['title'];
// Perform action
self::notifySubscribers($itemId, $title);
}
protected static function notifySubscribers($itemId, $title) {
// Implementation
}
}
?>
<?php
function my_module_on_item_created($data) {
$itemId = $data['itemid'];
$title = $data['title'];
$uid = $data['uid'];
// Send notification
notifyUser($uid, "Article created: $title");
}
?>

उदाहरण 1: आलेख निर्माण पर ईमेल भेजें

Section titled “उदाहरण 1: आलेख निर्माण पर ईमेल भेजें”
<?php
// Listen for article creation
xoops_events()->attach(
'publisher.item.created',
'send_article_notification_email'
);
function send_article_notification_email($data) {
$itemId = $data['itemid'];
$title = $data['title'];
$uid = $data['uid'];
// Get user object
$userHandler = xoops_getHandler('user');
$user = $userHandler->get($uid);
if (!$user) {
return;
}
// Get admin emails
$config = xoops_getModuleConfig();
$adminEmails = $config['admin_emails'];
// Prepare email
$subject = "New Article: $title";
$message = "A new article has been created:\n\n";
$message .= "Title: $title\n";
$message .= "Author: " . $user->getVar('uname') . "\n";
$message .= "Date: " . date('Y-m-d H:i:s') . "\n";
$message .= "ID: $itemId\n\n";
$message .= "Link: " . XOOPS_URL . "/modules/publisher/?op=showitem&itemid=$itemId\n";
// Send to admins
foreach (explode(',', $adminEmails) as $email) {
xoops_mail($email, $subject, $message);
}
}
?>

उदाहरण 2: खोज सूचकांक अद्यतन करें

Section titled “उदाहरण 2: खोज सूचकांक अद्यतन करें”
<?php
// Listen for article published event
xoops_events()->attach(
'publisher.item.published',
'update_search_index'
);
function update_search_index($data) {
$itemId = $data['itemid'];
$item = $data['item'];
// Update search index
$searchHandler = xoops_getModuleHandler('Search');
$searchHandler->indexArticle($itemId, array(
'title' => $item->getVar('title'),
'content' => $item->getVar('body'),
'author' => $item->getVar('uname'),
'date' => $item->getVar('datesub')
));
}
?>

उदाहरण 3: सोशल मीडिया पर ऑटो-पोस्ट

Section titled “उदाहरण 3: सोशल मीडिया पर ऑटो-पोस्ट”
<?php
// Listen for article publication
xoops_events()->attach(
'publisher.item.published',
'post_to_social_media'
);
function post_to_social_media($data) {
$item = $data['item'];
$itemId = $data['itemid'];
// Get config
$config = xoops_getModuleConfig();
if ($config['post_to_twitter']) {
postToTwitter(
$item->getVar('title'),
XOOPS_URL . '/modules/publisher/?op=showitem&itemid=' . $itemId
);
}
if ($config['post_to_facebook']) {
postToFacebook(
$item->getVar('title'),
$item->getVar('description')
);
}
}
function postToTwitter($text, $url) {
// Twitter API integration
// Use Twitter OAuth library
}
function postToFacebook($title, $description) {
// Facebook API integration
}
?>

उदाहरण 4: बाहरी सिस्टम के साथ समन्वयन

Section titled “उदाहरण 4: बाहरी सिस्टम के साथ समन्वयन”
<?php
// Listen for article creation and update
xoops_events()->attach(
'publisher.item.created',
'sync_external_system'
);
xoops_events()->attach(
'publisher.item.updated',
'sync_external_system'
);
function sync_external_system($data) {
$item = $data['item'];
$itemId = $data['itemid'];
// Get external API config
$config = xoops_getModuleConfig();
$apiUrl = $config['external_api_url'];
$apiKey = $config['external_api_key'];
// Prepare payload
$payload = json_encode(array(
'id' => $itemId,
'title' => $item->getVar('title'),
'content' => $item->getVar('body'),
'date' => date('c', $item->getVar('datesub'))
));
// Send to external system
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
));
curl_exec($ch);
curl_close($ch);
}
?>

हुक प्रकाशक के व्यवहार में संशोधन की अनुमति देते हैं:

प्रकाशक.दृश्य.लेख.प्रारंभ

Section titled “प्रकाशक.दृश्य.लेख.प्रारंभ”

आलेख प्रस्तुत करने से पहले कॉल किया गया।

xoops_events()->attach(
'publisher.view.article.start',
'modify_article_before_display'
);
function modify_article_before_display(&$item) {
// Modify item before display
$title = $item->getVar('title');
$item->setVar('title', '[FEATURED] ' . $title);
}

प्रकाशक.दृश्य.लेख.अंत

Section titled “प्रकाशक.दृश्य.लेख.अंत”

लेख प्रस्तुत होने के बाद कॉल किया जाता है।

xoops_events()->attach(
'publisher.view.article.end',
'append_to_article'
);
function append_to_article(&$article) {
// Add content after article
$article .= '<div class="related-articles">';
$article .= '<!-- Related articles content -->';
$article .= '</div>';
}

प्रकाशक.अनुमति.चेक

Section titled “प्रकाशक.अनुमति.चेक”

अनुमतियाँ जाँचते समय कॉल किया गया।

xoops_events()->attach(
'publisher.permission.check',
'custom_permission_logic'
);
function custom_permission_logic(&$allowed, $permission, $itemId) {
// Custom permission logic
if (custom_rule_applies($itemId)) {
$allowed = true;
}
}

प्लगइन्स प्रकाशक कार्यक्षमता का विस्तार करते हैं:

फ़ाइल संरचना:

modules/publisher/plugins/
├── myplugin/
│ ├── plugin.php (main file)
│ ├── language/
│ │ └── english.php
│ ├── templates/
│ └── css/

प्लगइन.php:

<?php
// Plugin information
define('MYPLUGIN_NAME', 'My Publisher Plugin');
define('MYPLUGIN_VERSION', '1.0.0');
define('MYPLUGIN_DESCRIPTION', 'Extends Publisher with custom features');
// Register hooks/events
xoops_events()->attach(
'publisher.item.created',
'myplugin_on_item_created'
);
xoops_events()->attach(
'publisher.view.article.end',
'myplugin_append_content'
);
// Plugin functions
function myplugin_on_item_created($data) {
// Handle item creation
}
function myplugin_append_content(&$content) {
// Append content to article
$content .= '<div class="myplugin-content">Custom content</div>';
}
// Plugin API
class MyPublisherPlugin {
public static function getArticles($limit = 10) {
$itemHandler = xoops_getModuleHandler('Item', 'publisher');
return $itemHandler->getRecent($limit);
}
public static function getCategoryTree() {
$catHandler = xoops_getModuleHandler('Category', 'publisher');
return $catHandler->getRoots();
}
}
?>

प्रकाशक आरंभीकरण में:

<?php
// Load plugin
$pluginPath = XOOPS_ROOT_PATH . '/modules/publisher/plugins/myplugin/plugin.php';
if (file_exists($pluginPath)) {
include_once $pluginPath;
}
?>

सामग्री फ़िल्टर

Section titled “सामग्री फ़िल्टर”

प्रसंस्करण से पहले/बाद में फ़िल्टर डेटा को संशोधित करते हैं:

<?php
// Filter article title
$title = apply_filters('publisher_item_title', $title, $itemId);
// Filter article body
$body = apply_filters('publisher_item_body', $body, $itemId);
// Filter article display
$display = apply_filters('publisher_item_display', $display, $item);
?>

रजिस्टर फ़िल्टर

Section titled “रजिस्टर फ़िल्टर”
<?php
// Add filter
add_filter('publisher_item_title', 'my_title_filter');
function my_title_filter($title, $itemId) {
// Modify title
return strtoupper($title);
}
// Add filter with priority
add_filter(
'publisher_item_body',
'my_body_filter',
10, // priority (lower = earlier)
2 // number of arguments
);
function my_body_filter($body, $itemId) {
// Add watermark to body
return $body . '<p class="watermark">© ' . date('Y') . '</p>';
}
?>

विशिष्ट बिंदुओं पर कोड निष्पादित करें:

<?php
// Do action
do_action('publisher_article_saved', $itemId, $item);
// Do action with arguments
do_action('publisher_comment_approved', $commentId, $comment);
// Listen to action
add_action('publisher_article_saved', 'my_action_handler');
function my_action_handler($itemId, $item) {
// Execute code
log_article_save($itemId);
update_statistics();
}
?>

प्लगइन्स के साथ विस्तार

Section titled “प्लगइन्स के साथ विस्तार”

उदाहरण प्लगइन: संबंधित लेख

Section titled “उदाहरण प्लगइन: संबंधित लेख”
modules/publisher/plugins/related-articles/plugin.php
<?php
class RelatedArticlesPlugin {
public static function init() {
xoops_events()->attach(
'publisher.view.article.end',
array(__CLASS__, 'displayRelated')
);
}
public static function displayRelated(&$content) {
// Get related articles
$related = self::getRelatedArticles();
if (count($related) > 0) {
$html = '<div class="related-articles">';
$html .= '<h3>Related Articles</h3>';
$html .= '<ul>';
foreach ($related as $article) {
$html .= '<li>';
$html .= '<a href="' . $article->url() . '">';
$html .= $article->title();
$html .= '</a>';
$html .= '</li>';
}
$html .= '</ul>';
$html .= '</div>';
$content .= $html;
}
}
protected static function getRelatedArticles() {
// Get current article
global $itemId;
$itemHandler = xoops_getModuleHandler('Item', 'publisher');
$item = $itemHandler->get($itemId);
if (!$item) {
return array();
}
// Get articles in same category
$related = $itemHandler->getByCategory(
$item->getVar('categoryid'),
$limit = 5
);
// Remove current article
$related = array_filter($related, function($article) {
global $itemId;
return $article->getVar('itemid') != $itemId;
});
return array_slice($related, 0, 3);
}
}
// Initialize plugin
RelatedArticlesPlugin::init();
?>

सर्वोत्तम प्रथाएँ

Section titled “सर्वोत्तम प्रथाएँ”

इवेंट श्रोता दिशानिर्देश

Section titled “इवेंट श्रोता दिशानिर्देश”
Keep listeners performant
- Don't do heavy processing in events
- Cache results when possible
✓ Handle errors gracefully
- Use try/catch
- Log errors
- Don't break main flow
Use meaningful names
- my_module_on_publisher_item_created
- Instead of: process_event_1
Document your events
- Comment what trigger point is
- List expected data
- Show usage examples
Unload listeners properly
- Clean up on module uninstall
- Remove hooks when no longer needed

प्रदर्शन युक्तियाँ

Section titled “प्रदर्शन युक्तियाँ”
✗ Avoid database queries in listeners
✗ Don't block execution with slow operations
✗ Avoid modifying data unnecessarily
✓ Queue long-running tasks
✓ Cache external API calls
✓ Use lazy loading for dependencies
✓ Batch database operations

डिबग मोड सक्षम करें

Section titled “डिबग मोड सक्षम करें”
<?php
// In module initialization
if (defined('XOOPS_DEBUG')) {
xoops_events()->attach(
'publisher.item.created',
'publisher_debug_event'
);
}
function publisher_debug_event($data) {
error_log('Publisher Event: ' . print_r($data, true));
}
?>
<?php
// Log event data
xoops_events()->attach(
'publisher.item.created',
'log_publisher_events'
);
function log_publisher_events($data) {
$log = XOOPS_ROOT_PATH . '/var/log/publisher.log';
$entry = date('Y-m-d H:i:s') . ' - ';
$entry .= 'Event: publisher.item.created' . "\n";
$entry .= 'Data: ' . json_encode($data) . "\n\n";
file_put_contents($log, $entry, FILE_APPEND);
}
?>

संबंधित दस्तावेज़ीकरण

Section titled “संबंधित दस्तावेज़ीकरण”
  • API संदर्भ
  • कस्टम टेम्पलेट्स
  • लेख निर्माण


#प्रकाशक #हुक #इवेंट #प्लगइन्स #एक्सटेंशन #कस्टमाइज़ेशन #xoops