Εκδότης - Hooks and Events
Publisher Hooks and Events
Ενότητα με τίτλο «Publisher Hooks and Events»Πλήρης οδηγός για την επέκταση της λειτουργικότητας του Publisher χρησιμοποιώντας συμβάντα, άγκιστρα και προσθήκες.
Επισκόπηση συστήματος συμβάντων
Ενότητα με τίτλο «Επισκόπηση συστήματος συμβάντων»# Τι είναι οι εκδηλώσεις;
Ενότητα με τίτλο «# Τι είναι οι εκδηλώσεις;»Τα συμβάντα επιτρέπουν σε άλλες λειτουργικές μονάδες να αντιδρούν στις ενέργειες του Publisher:
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]Διαθέσιμες εκδηλώσεις
Ενότητα με τίτλο «Διαθέσιμες εκδηλώσεις»# Στοιχείο (Άρθρο) Εκδηλώσεις
Ενότητα με τίτλο «# Στοιχείο (Άρθρο) Εκδηλώσεις»# publisher.item.created
Ενότητα με τίτλο «# publisher.item.created»Ενεργοποιήθηκε όταν δημιουργείται ένα νέο άρθρο.
// Trigger point in Publisherxoops_events()->trigger('publisher.item.created', array( 'item' => $item, 'itemid' => $item->getVar('itemid'), 'title' => $item->getVar('title'), 'uid' => $item->getVar('uid')));Παράδειγμα ακροατή:
// Listen for article creationxoops_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);}# publisher.item.updated
Ενότητα με τίτλο «# publisher.item.updated»Ενεργοποιήθηκε όταν ενημερώνεται ένα άρθρο.
xoops_events()->trigger('publisher.item.updated', array( 'item' => $item, 'itemid' => $itemId, 'changes' => $changes));# publisher.item.deleted
Ενότητα με τίτλο «# publisher.item.deleted»Ενεργοποιείται όταν διαγράφεται ένα άρθρο.
xoops_events()->trigger('publisher.item.deleted', array( 'itemid' => $itemId, 'title' => $title, 'categoryid' => $categoryId));# publisher.item.published
Ενότητα με τίτλο «# publisher.item.published»Ενεργοποιήθηκε όταν η κατάσταση του άρθρου αλλάζει σε δημοσιευμένο.
xoops_events()->trigger('publisher.item.published', array( 'item' => $item, 'itemid' => $itemId));# publisher.item.approved
Ενότητα με τίτλο «# publisher.item.approved»Πυροδοτήθηκε όταν εγκριθεί το εκκρεμές άρθρο.
xoops_events()->trigger('publisher.item.approved', array( 'item' => $item, 'itemid' => $itemId, 'uid' => $uid));# publisher.item.rejected
Ενότητα με τίτλο «# publisher.item.rejected»Πυροδοτήθηκε όταν το άρθρο απορρίπτεται.
xoops_events()->trigger('publisher.item.rejected', array( 'item' => $item, 'itemid' => $itemId, 'reason' => $reason));# Εκδηλώσεις κατηγορίας
Ενότητα με τίτλο «# Εκδηλώσεις κατηγορίας»# publisher.category.created
Ενότητα με τίτλο «# publisher.category.created»Ενεργοποιήθηκε όταν δημιουργείται η κατηγορία.
xoops_events()->trigger('publisher.category.created', array( 'category' => $category, 'categoryid' => $categoryId, 'name' => $name));# publisher.category.updated
Ενότητα με τίτλο «# publisher.category.updated»Ενεργοποιήθηκε όταν ενημερωθεί η κατηγορία.
xoops_events()->trigger('publisher.category.updated', array( 'category' => $category, 'categoryid' => $categoryId));# publisher.category.deleted
Ενότητα με τίτλο «# publisher.category.deleted»Ενεργοποιήθηκε όταν διαγραφεί η κατηγορία.
xoops_events()->trigger('publisher.category.deleted', array( 'categoryid' => $categoryId, 'name' => $name, 'itemCount' => $itemCount));# Σχόλιο συμβάντων
Ενότητα με τίτλο «# Σχόλιο συμβάντων»# publisher.comment.created
Ενότητα με τίτλο «# publisher.comment.created»Ενεργοποιήθηκε όταν δημοσιεύτηκε σχόλιο.
xoops_events()->trigger('publisher.comment.created', array( 'comment' => $comment, 'commentid' => $commentId, 'itemid' => $itemId));# publisher.comment.approved
Ενότητα με τίτλο «# publisher.comment.approved»Ενεργοποιήθηκε όταν εγκρίνεται το σχόλιο.
xoops_events()->trigger('publisher.comment.approved', array( 'comment' => $comment, 'commentid' => $commentId));# publisher.comment.deleted
Ενότητα με τίτλο «# publisher.comment.deleted»Ενεργοποιήθηκε όταν διαγραφεί το σχόλιο.
xoops_events()->trigger('publisher.comment.deleted', array( 'commentid' => $commentId, 'itemid' => $itemId));Ακούγοντας Εκδηλώσεις
Ενότητα με τίτλο «Ακούγοντας Εκδηλώσεις»# Εγγραφή ακρόασης εκδήλωσης
Ενότητα με τίτλο «# Εγγραφή ακρόασης εκδήλωσης»Στη λειτουργική μονάδα ή την προσθήκη:
<?php// Register listener in xoops_version.php or initialization filexoops_events()->attach( 'publisher.item.created', array('MyModuleListener', 'onPublisherItemCreated'));
// Or use function namexoops_events()->attach( 'publisher.item.created', 'my_module_on_item_created');?># Μέθοδος κλάσης ακροατών
Ενότητα με τίτλο «# Μέθοδος κλάσης ακροατών»<?phpclass 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 }}?># Λειτουργία ακροατή
Ενότητα με τίτλο «# Λειτουργία ακροατή»<?phpfunction my_module_on_item_created($data) { $itemId = $data['itemid']; $title = $data['title']; $uid = $data['uid'];
// Send notification notifyUser($uid, "Article created: $title");}?>Παραδείγματα συμβάντων
Ενότητα με τίτλο «Παραδείγματα συμβάντων»# Παράδειγμα 1: Αποστολή email για τη δημιουργία άρθρου
Ενότητα με τίτλο «# Παράδειγμα 1: Αποστολή email για τη δημιουργία άρθρου»<?php// Listen for article creationxoops_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: Ενημέρωση ευρετηρίου αναζήτησης
Ενότητα με τίτλο «# Παράδειγμα 2: Ενημέρωση ευρετηρίου αναζήτησης»<?php// Listen for article published eventxoops_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: Αυτόματη ανάρτηση στα μέσα κοινωνικής δικτύωσης
Ενότητα με τίτλο «# Παράδειγμα 3: Αυτόματη ανάρτηση στα μέσα κοινωνικής δικτύωσης»<?php// Listen for article publicationxoops_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: Συγχρονισμός με εξωτερικό σύστημα
Ενότητα με τίτλο «# Παράδειγμα 4: Συγχρονισμός με εξωτερικό σύστημα»<?php// Listen for article creation and updatexoops_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);}?>Σύστημα αγκίστρων
Ενότητα με τίτλο «Σύστημα αγκίστρων»# Publisher Hooks
Ενότητα με τίτλο «# Publisher Hooks»Τα άγκιστρα επιτρέπουν τροποποιήσεις στη συμπεριφορά του Publisher:
# publisher.view.article.start
Ενότητα με τίτλο «# publisher.view.article.start»Κλήση πριν από την απόδοση του άρθρου.
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);}# publisher.view.article.end
Ενότητα με τίτλο «# publisher.view.article.end»Καλείται μετά την απόδοση του άρθρου.
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>';}# publisher.permission.check
Ενότητα με τίτλο «# publisher.permission.check»Κλήση κατά τον έλεγχο των αδειών.
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; }}Σύστημα προσθηκών
Ενότητα με τίτλο «Σύστημα προσθηκών»# Δημιουργήστε ένα πρόσθετο
Ενότητα με τίτλο «# Δημιουργήστε ένα πρόσθετο»Οι προσθήκες επεκτείνουν τη λειτουργικότητα του Publisher:
Δομή αρχείου:
modules/publisher/plugins/├── myplugin/│ ├── plugin.php (main file)│ ├── language/│ │ └── english.php│ ├── templates/│ └── css/plugin.php:
<?php// Plugin informationdefine('MYPLUGIN_NAME', 'My Publisher Plugin');define('MYPLUGIN_VERSION', '1.0.0');define('MYPLUGIN_DESCRIPTION', 'Extends Publisher with custom features');
// Register hooks/eventsxoops_events()->attach( 'publisher.item.created', 'myplugin_on_item_created');
xoops_events()->attach( 'publisher.view.article.end', 'myplugin_append_content');
// Plugin functionsfunction 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 APIclass 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(); }}?># Φόρτωση πρόσθετου
Ενότητα με τίτλο «# Φόρτωση πρόσθετου»Στην προετοιμασία του Publisher:
<?php// Load plugin$pluginPath = XOOPS_ROOT_PATH . '/modules/publisher/plugins/myplugin/plugin.php';if (file_exists($pluginPath)) { include_once $pluginPath;}?># Φίλτρα περιεχομένου
Ενότητα με τίτλο «# Φίλτρα περιεχομένου»Τα φίλτρα τροποποιούν την επεξεργασία δεδομένων before/after:
<?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);?># Φίλτρο εγγραφής
Ενότητα με τίτλο «# Φίλτρο εγγραφής»<?php// Add filteradd_filter('publisher_item_title', 'my_title_filter');
function my_title_filter($title, $itemId) { // Modify title return strtoupper($title);}
// Add filter with priorityadd_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 actiondo_action('publisher_article_saved', $itemId, $item);
// Do action with argumentsdo_action('publisher_comment_approved', $commentId, $comment);
// Listen to actionadd_action('publisher_article_saved', 'my_action_handler');
function my_action_handler($itemId, $item) { // Execute code log_article_save($itemId); update_statistics();}?>Επέκταση με πρόσθετα
Ενότητα με τίτλο «Επέκταση με πρόσθετα»# Παράδειγμα προσθήκης: Σχετικά άρθρα
Ενότητα με τίτλο «# Παράδειγμα προσθήκης: Σχετικά άρθρα»<?phpclass 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 pluginRelatedArticlesPlugin::init();?>Βέλτιστες πρακτικές
Ενότητα με τίτλο «Βέλτιστες πρακτικές»# Οδηγίες ακρόασης συμβάντων
Ενότητα με τίτλο «# Οδηγίες ακρόασης συμβάντων»✓ 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# Συμβουλές απόδοσης
Ενότητα με τίτλο «# Συμβουλές απόδοσης»✗ 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Εντοπισμός σφαλμάτων συμβάντων
Ενότητα με τίτλο «Εντοπισμός σφαλμάτων συμβάντων»# Ενεργοποίηση λειτουργίας εντοπισμού σφαλμάτων
Ενότητα με τίτλο «# Ενεργοποίηση λειτουργίας εντοπισμού σφαλμάτων»<?php// In module initializationif (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 dataxoops_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);}?>Σχετική τεκμηρίωση
Ενότητα με τίτλο «Σχετική τεκμηρίωση»- API Αναφορά
- Προσαρμοσμένα πρότυπα
- Δημιουργία άρθρου