Last active
July 20, 2022 08:27
-
-
Save Dmi3yy/2bfd18a4a24ee755c36f488a07b65de9 to your computer and use it in GitHub Desktop.
hits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<?Hits % OnLogPageHit, OnBeforeDocFormSave, OnDocFormRender | |
if(!defined('MODX_BASE_PATH')){die('What are you doing? Get out of here!');} | |
/* | |
Hits 1.0.3 by Yurik Dertlyan | yurik@unix.am | |
update by Dmi3yy@gmail.com | |
Plugin for Evolution CMS to register and output page hits | |
Installation: | |
- SQL 'ALTER TABLE modx_site_content ADD COLUMN `hits` INT(10) UNSIGNED NOT NULL'; | |
- Create plugin with name "Hits" and description "1.0.3" | |
- Check OnLogPageHit,OnWebPagePrerender event | |
- Enable page hits logging in MODx settings page | |
To output hits use: | |
- от page: [*hits*] | |
- in Snippets like DocLister, Ditto: {counter:[+id+]}. | |
*/ | |
$e = &$modx->event; | |
switch($e->name) { | |
case 'OnLogPageHit': | |
$document = array( | |
'id'=>$modx->documentIdentifier, | |
'table'=>$modx->getFullTableName('site_content') | |
); | |
$sql = 'SELECT donthit,hits FROM '.$document['table'].' WHERE id='.$document['id']; | |
// MODx DBAPI is not recommended here | |
$r = $modx->db->query($sql); | |
if(!$r){ | |
// Creating hits column | |
$r = $modx->db->query($sql); | |
} | |
if(!$modx->db->getRecordCount($r)) return; | |
// Merging arrays of document data | |
$document = array_merge($document, $modx->db->getRow($r)); | |
// Skip logging for manager hits | |
if($document['donthit'] == 1 || (isset($_SESSION['usertype']) && $_SESSION['usertype'] == 'manager')) return; | |
// Recording hit to database | |
$modx->db->update('hits=hits+1', $document['table'], 'id='.$document['id']); | |
break; | |
case 'OnWebPagePrerender' : | |
preg_match_all("'{counter:([^>]*)}'si", $modx->documentOutput, $match); | |
foreach($match[1] as $val) | |
{ | |
$sql = $modx->db->query("SELECT hits FROM ".$modx->getFullTableName('site_content')." WHERE `id`='{$val}'"); | |
$row = $modx->db->getRow( $sql ); | |
$modx->documentOutput=str_replace('{counter:'.$val.'}',$row['hits'],$modx->documentOutput); | |
} | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment