if (@txpinterface == 'admin') { register_callback("smd_article_info", "article"); } // Client-side info function smd_article_stats($atts, $thing=NULL) { global $thisarticle; assert_article(); extract(lAtts(array( 'wraptag' => '', 'class' => __FUNCTION__, 'break' => '', 'label' => '', 'labeltag' => '', 'item' => '', ), $atts)); $out = array(); $item = do_list($item); $out[] = smd_article_info_count($item, $thisarticle['body'], $thisarticle['excerpt'], $thisarticle['title']); return doLabel($label, $labeltag).doWrap($out, $wraptag, $break, $class); } // Admin-side info -- auto-updated via jQuery function smd_article_info($event, $step) { extract(gpsa(array('view'))); if(!$view || gps('save') || gps('publish')) { $view = 'text'; } if ($view == 'text') { $screen_locs = array( 'none' => '', 'excerpt_below' => 'da|article-main', 'status_above' => 'jq|#write-status|before', 'title_above' => 'jq|#article-main|prepend', 'textile_help_above' => 'jq|#article-col-1|prepend', 'textile_help_below' => 'jq|#textile_help|after', ); // Check hidden pref and sanitize $posn = get_pref('smd_article_stats_pos', 'status_above'); $posn = (array_key_exists($posn, $screen_locs)) ? $posn : 'status_above'; $placer = explode('|', $screen_locs[$posn]); $id = (empty($GLOBALS['ID']) ? gps('ID') : $GLOBALS['ID']); $item = array('excerpt', 'body'); if (empty($id)) { $rs = array('Body' => '', 'Excerpt' => '', 'Status' => ''); } else { $rs = safe_row('Body, Excerpt, Status', 'textpattern', 'ID='.doSlash($id)); } $bwc = smd_article_info_count($item, $rs['Body'], '', ''); $ewc = smd_article_info_count($item, '', $rs['Excerpt'], ''); $words = $bwc + $ewc; $idlink = ($id && in_array($rs['Status'], array(4,5))) ? '' .$id. '' : $id; $out1 = '
Article Stats'.$words.' word'.(($words==1)?'':'s').': ( '.$bwc.' / '.$ewc.' )'.(($id) ? ' | id: ' .$idlink : '').'
'; $out2 = << jQuery(function() { jQuery("#body, #excerpt").keyup(function() { var bdy = jQuery("#body").val(); bdy += (bdy.length>0) ? " " : ""; var exc = jQuery("#excerpt").val(); exc += (exc.length>0) ? " " : ""; var bwc = bdy.replace(/(<([^>]+)>)/ig,"").split(/\s+/).length-1; var ewc = exc.replace(/(<([^>]+)>)/ig,"").split(/\s+/).length-1; var wds = bwc+ewc; jQuery(".smd_article_stats_wc").text(wds); jQuery(".smd_article_stats_wd").text(((wds==1)?"":"s")); jQuery(".smd_article_stats_bc").text(bwc); jQuery(".smd_article_stats_ec").text(ewc); }); }); EOJS; if ($placer[0] == 'jq') { echo ''.$out2; } else if ($placer[0] == 'da') { echo dom_attach($placer[1], $out1.$out2, $out1, 'div'); } } } // Library function function smd_article_info_count($item, $body, $excerpt, $title) { $words = 0; $notags = '/(<([^>]+?)>)/'; $body = preg_replace($notags, '', trim($body)) . ((strlen($body)==0) ? '' : ' '); $excerpt = preg_replace($notags, '', trim($excerpt)) . ((strlen($excerpt)==0) ? '' : ' '); $title = preg_replace($notags, '', trim($title)) . ((strlen($title)==0) ? '' : ' '); foreach ($item as $whatnot) { switch ($whatnot) { case "body": $words += preg_match_all('@\s+@', $body, $m); break; case "excerpt": $words += preg_match_all('@\s+@', $excerpt, $m); break; case "title": $words += preg_match_all('@\s+@', $title, $m); break; } } return $words; }