/** * smd_article_stats * * A Textpattern CMS plugin for counting words in article fields and optionally displaying them to visitors * -> Choose which fields to count on the admin side * -> Customize where you want the count to be displayed * -> Shows ID of currently edited article. * * @author Stef Dawson * @link http://stefdawson.com/ */ // TODO: // * TinyMCE -- accessing fields inside iframes? if (@txpinterface == 'admin') { $all_privs = array_keys(get_groups()); unset($all_privs[array_search('0', $all_privs)]); // Remove 'none' $all_joined = join(',', $all_privs); add_privs('smd_artstat_prefs', $all_joined); add_privs('prefs.smd_artstat', $all_joined); register_callback('smd_artstat_prefs', 'prefs', '', 1); register_callback('smd_article_info', 'article'); $smd_ai_prefs = smd_article_info_prefs(); foreach ($smd_ai_prefs as $key => $prefobj) { register_callback('smd_article_info_pophelp', 'admin_help', $key); } } // Public 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 not specified? Use the array 'keys' from the pref if (empty($item)) { $fldList = do_list(get_pref('smd_artstat_fields')); $cfs = getCustomFields(); foreach ($fldList as $fld) { $fldInfo = do_list($fld, '->'); $field = $fldInfo[0]; if (strpos($field, 'custom_') !== false) { $cfnum = str_replace('custom_', '', $field); if (array_key_exists($cfnum, $cfs)) { $field = $cfs[$cfnum]; } else { // Bogus CF: skip it continue; } } $item[] = strtolower($field); } } else { $item = do_list($item); } $out[] = smd_article_info_count($item, $thisarticle); return doLabel($label, $labeltag).doWrap($out, $wraptag, $break, $class); } // Admin-side info -- auto-updated via jQuery function smd_article_info($event, $step) { global $app_mode; extract(gpsa(array('view'))); include_once txpath.'/publish/taghandlers.php'; if(!$view || gps('save') || gps('publish')) { $view = 'text'; } if ($view == 'text') { $screen_locs = array( 'none' => '', 'excerpt_below' => 'jq|.excerpt|after', 'author_below' => 'jq|.author|after', 'status_above' => 'jq|#write-status|before', 'title_above' => 'jq|#article-main|prepend', 'textfilter_help_above' => 'jq|#textfilter_group|before', 'textfilter_help_below' => 'jq|#textfilter_group|after', // For 4.6.x+ 'textile_help_above' => 'jq|#article-col-1|prepend', 'textile_help_below' => 'jq|#textile_help|after', // For 4.5.x ); // Check hidden pref and sanitize $posn = get_pref('smd_artstat_pos', 'status_above'); $posn = (array_key_exists($posn, $screen_locs)) ? $posn : 'status_above'; $placer = explode('|', $screen_locs[$posn]); doArray($placer, 'escape_js'); // Split and recombine to get rid of spaces // TODO: error detection if missing entries $fldList = do_list(get_pref('smd_artstat_fields', 'Body -> #body, Excerpt -> #excerpt')); $fldAnchors = array('0'); // Placeholder since Status isn't a countable field, but we need it later $db_fields = array('Status'); foreach ($fldList as $fld) { $fldInfo = do_list($fld, '->'); $db_fields[] = $fldInfo[0]; if (isset($fldInfo[1])) { $fldAnchors[] = $fldInfo[1]; } } array_shift($fldAnchors); // Goodbye Status anchor $js_fields = escape_js(implode(',', $fldAnchors)); $js_array_fields = implode(',', doArray(doArray($fldAnchors, 'escape_js'), 'doQuote')); $id = (empty($GLOBALS['ID']) ? gps('ID') : $GLOBALS['ID']); if (empty($id)) { $rs = $db_fields; } else { $rs = safe_row(join(',', doArray($db_fields, 'doSlash')), 'textpattern', 'ID='.doSlash($id)); } $idlink = (get_pref('smd_artstat_id') === '1') ? (($id && in_array($rs['Status'], array(STATUS_LIVE, STATUS_STICKY))) ? href($id, permlinkurl_id($id)) : $id) : ''; $indiv = array(); $words = 0; array_shift($db_fields); // Goodbye Status field foreach ($db_fields as $idx => $fld) { $wc = smd_article_info_count($fld, $rs); $words += $wc; $indiv[] = ''.$wc.''; } gTxtScript(array('smd_artstat_word_singular', 'smd_artstat_word_plural')); $singstring = get_pref('smd_artstat_singular', '1'); $singles = do_list($singstring); $out1 = escape_js( defined('PREF_PLUGIN') ? wrapGroup('smd_artstat', ''.$words.' '.(in_array($words, $singles) ? gTxt('smd_artstat_word_singular') : gTxt('smd_artstat_word_plural')).': ( ' . join(' / ', $indiv) .' )'.(($idlink) ? ' | ' . gTxt('id') .n. $idlink : ''), 'smd_artstat') : '
' ); $out2 = script_js(<<