View source
<?php
DEFINE(WORDSTREAM_LINK_API_ACCOUNT, 'https://www.wordstream.com/register-free-keyword-tool');
DEFINE(WORDSTREAM_LINK_WIDGET_GENERATOR, 'http://www.wordstream.com/widget-generator');
DEFINE(WORDTRACKER_ADULT_FILTER_DEFAULT, TRUE);
function wordstream_menu() {
$items = array();
$items['admin/settings/wordstream'] = array(
'title' => t('WordStream'),
'description' => 'Analyze and optimize node content.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'wordstream_admin_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'admin wordstream',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'wordstream.admin.inc',
);
$items['admin/content/wordstream'] = array(
'title' => t('Wordstream'),
'page callback' => 'wordstream_keyword_stats_report_page',
'page arguments' => array(
'keywords',
),
'access callback' => 'user_access',
'access arguments' => array(
'access wordstream reports',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'wordstream.admin.inc',
);
$items['admin/content/wordstream/keywords'] = array(
'title' => t('Keywords tool'),
'page callback' => 'wordstream_keyword_stats_report_page',
'page arguments' => array(
'keywords',
),
'access callback' => 'user_access',
'access arguments' => array(
'access wordstream reports',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'wordstream.admin.inc',
);
$items['wordstream/util'] = array(
'title' => 'WordStream util',
'page callback' => 'wordstream_util',
'access callback' => TRUE,
'access arguments' => array(
'access wordstream reports',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function wordstream_perm() {
return array(
'admin wordstream',
'access wordstream reports',
);
}
function wordstream_util() {
return 'ok';
}
function wordstream_include_api_class() {
$files['module_path'] = './' . drupal_get_path('module', 'wordstream') . '/lib/class.wordstream.inc';
if (module_exists('libraries') && ($p = libraries_get_path('wordstream'))) {
$files['libraries_path'] = './' . $p . '/class.wordstream.inc';
}
foreach ($files as $f) {
if (file_exists($f)) {
$file = $f;
break;
}
}
if (!$file) {
$msg = t('The WordStream module requires the WordStream API class. ');
$msg .= l(t('Download the class here.'), 'https://api.wordstream.com/doc/sample_code?doc=wordstream_api.php&label=WordStream%20API%20Sample%20Code', array(
'attributes' => array(
'target' => 'alchemy',
),
));
$msg .= "<br><br>";
if ($files['libraries_path']) {
$msg .= t(' Save the file on your server in a folder named "wordstream" under the libraries directory; %path.', array(
'%path' => $files['libraries_path'],
));
}
else {
$msg .= t(' Save the file on your server in a folder named "lib" under the WordStream module directory; %path.', array(
'%path' => $files['module_path'],
));
}
if ($analysis) {
$analysis['messages'][] = contentanalysis_format_message($msg, 'error');
}
else {
drupal_set_message($msg, 'error');
}
return FALSE;
}
require_once $file;
$un = variable_get('wordstream_username', '');
$pw = variable_get('wordstream_password', '');
$wordstream = new WordStream($un, $pw);
if (!$_SESSION['wordstream']['sid']) {
$ret = $wordstream
->login();
list($un, $sid) = explode(':', $ret);
$_SESSION['wordstream']['sid'] = $sid;
}
return $wordstream;
}
function wordstream_keyword_stats_report_page($type = 'keywords', $keyword = NULL) {
if (module_exists('kwresearch')) {
drupal_add_css(drupal_get_path('module', 'kwresearch') . '/kwresearch.css');
drupal_add_js(drupal_get_path('module', 'kwresearch') . '/kwresearch.js');
}
$analysis = $_SESSION['wordstream']['analysis'];
$_SESSION['wordstream']['analysis'] = '';
$stats = TRUE;
if (!$analysis && $keyword) {
$analysis = array();
$analysis['inputs']['keyword'] = $keyword;
$analysis['analysis'] = wordstream_get_keyword_data($keyword, $msgs);
if (!empty($msgs)) {
$analysis['messages'] = $msgs;
}
}
if ($analysis) {
$analysis['inputs']['operations'] = TRUE;
}
$output .= drupal_get_form('wordstream_stats_report_form', $analysis);
if ($analysis) {
$output .= '<div id="wordstream-report" style="clear: both;">';
$output .= '<h3>' . t('Report') . '</h3>';
$output .= theme_wordstream_stats_report($analysis);
$output .= '</div>';
if (is_array($analysis['analysis'])) {
foreach ($analysis['analysis'] as $kw => $v) {
$site_kw[$v['term']] = array(
'kid' => (int) $v['kid'],
'priority' => (int) $v['priority'],
'value' => (int) $v['value'],
'page_count' => (int) $v['page_count'],
);
}
}
}
if (module_exists('kwresearch')) {
$site_priority_options = kwresearch_get_priority_options();
drupal_add_js(array(
'kwresearch' => array(
'form' => 'admin_keyword_stats',
'analyze_path' => base_path() . 'admin/content/kwresearch/keyword_report/',
'keyword_edit_path' => base_path() . 'admin/content/kwresearch/keyword_list/edit/',
'return_destination' => 'admin/content/kwresearch/keyword_report/' . $keyword,
'toggle_site_keyword_callback' => base_path() . 'kwresearch/toggle_site_keyword_js',
'module_path' => base_path() . drupal_get_path('module', 'kwresearch'),
'enable_site_keyword_tool' => user_access('kwresearch admin site keywords'),
'site_keywords_data' => $site_kw,
'site_priority_options' => $site_priority_options,
),
), 'setting');
}
return $output;
}
function wordstream_stats_report_form($form_state, $analysis, $ajax = FALSE) {
$form['pre_inputs'] = array(
'#type' => 'markup',
'#value' => '<div id="wordstream-form-inputs">',
);
$form['wordstream_keyword'] = array(
'#type' => 'textfield',
'#title' => t('Keyword'),
'#default_value' => $analysis && $analysis['inputs']['wordstream_keyword'] ? $analysis['inputs']['wordstream_keyword'] : '',
);
$form['wordstream_stats_report_options'] = array(
'#type' => 'fieldset',
'#title' => t('Filters'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['wordstream_stats_report_options'] = array_merge($form['wordstream_stats_report_options'], wordstream_stats_report_form_options($analysis));
$form['post_inputs'] = array(
'#type' => 'markup',
'#value' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function wordstream_stats_report_form_options($analysis = NULL) {
$defvals = $analysis['inputs'];
$form['wordstream_adult_filter'] = array(
'#type' => 'checkbox',
'#title' => t('Adult keyword filter default'),
'#return_value' => 'on',
'#default_value' => isset($defvals['wordstream_adult_filter']) ? $defvals['wordstream_adult_filter'] : variable_get('wordstream_adult_filter', WORDSTREAM_ADULT_FILTER_DEFAULT),
);
return $form;
}
function wordstream_stats_report_form_submit($form, &$form_state) {
$analysis['inputs'] = $form_state['values'];
$params = array(
'type' => 'keywords',
'adult_filter' => $analysis['inputs']['wordstream_adult_filter'],
);
$analysis['analysis'] = wordstream_get_keyword_data($form_state['values']['wordstream_keyword'], $msgs, $params);
if (!empty($msgs)) {
$analysis['messages'] = $msgs;
}
$_SESSION['wordstream']['analysis'] = $analysis;
return;
}
function wordstream_get_keyword_data($keywords, &$msgs, $params) {
$type = $params['type'] ? $params['type'] : NULL;
$adult_filter = $params['adult_filter'] ? 1 : 0;
if (is_null($adult_filter)) {
$adult_filter = variable_get('kwresearch_adult_filter', 'remove_dubious');
}
if (!$type) {
$type = 'keywords';
$params['type'] = 'keywords';
}
$last_cache = variable_get('wordstream_last_cache', 0);
$cachecheck = FALSE;
if (time - $last_cache > variable_get('wordstream_cache_time', 604800)) {
$cachecheck = TRUE;
}
$sql = '
SELECT *
FROM {wordstream_cache}
WHERE
keyphrase = "%s" AND
adult_filter = %d AND
api_call = "%s"
';
$dbresult = db_fetch_array(db_query($sql, $keywords, $adult_filter, $type));
if ($dbresult) {
$dbret = unserialize($dbresult['data']);
if (!$cachecheck) {
return $dbret;
}
}
$wordstream = wordstream_include_api_class();
if (!$wordstream) {
return array();
}
if ($type == 'keywords') {
$apiret = $wordstream
->getKeywords($keywords);
}
if (!is_array($apiret)) {
$errmsg = t('Expected return of type array, but got [@type]', array(
'@type' => gettype($apiret),
));
$msgs[] = kwresearch_format_message($errmsg, 'error');
return FALSE;
}
if (strlen($keywords) > 255) {
return $apiret;
}
if (!$dbresult) {
$sql = '
INSERT INTO {wordstream_cache}
(keyphrase, adult_filter, api_call, updated, data)
VALUES ("%s", %d, "%s", %d,"%s")
';
$query = db_query($sql, $keywords, $adult_filter, $type, time(), serialize($apiret));
}
elseif (!$dbret) {
$sql = '
UPDATE {wordstream_cache}
SET
time = %d,
data = "%s"
WHERE
keyphrase = "%s" AND
adult_filter = %d AND
api_call = "%s"
';
$query = db_query($sql, time(), serialize($apiret), $keywords, $adult_filter, $type);
}
elseif (array_slice($dbret, 0, 10) != array_slice($apiret, 0, 10)) {
$sql = '
DELETE FROM {wordstream_data}
';
$query = db_query($sql);
drupal_set_message(t('Keyword cache reset.'));
variable_set('wordstream_last_cache', time());
scribeseo_update_kwresearch_searches_ratio();
}
return $apiret;
}
function theme_wordstream_stats_report($analysis) {
$hdrs = array(
t('Term'),
t('Frequency'),
);
$rows = array();
foreach ($analysis['analysis'] as $v) {
$row = array(
$v[0],
number_format($v[1]),
);
$rows[] = array(
'data' => $row,
'id' => 'kid-' . $values['kid'],
);
}
$attr = array(
'id' => 'kwresearch-result-table-' . check_plain(str_replace(' ', '-', $analysis['inputs']['keyword'])),
'class' => 'kwresearch-result-table',
);
$output = '<div id="kwresearch-result-block-' . check_plain(str_replace(' ', '-', $analysis['inputs']['keyword'])) . '" class="kwresearch-result-block">';
$output .= theme_table($hdrs, $rows, $attr);
return $output;
}
function wordstream_kwresearch_sources() {
$sources['wordstream'] = array(
'title' => t('WordStream'),
'module' => 'wordstream',
'form_options' => 'wordstream_stats_report_form_options',
'stats_callback' => 'wordstream_kwresearch_get_keyword_stats',
'searches_ratio' => variable_get('wordstream_kwresearch_searches_ratio', 3549.7),
'stats_report_columns' => array(
'wordstream_searches' => t('WS count'),
),
'stats_report_values_callback' => 'wordstream_kwresearch_format_stats_values',
);
return $sources;
}
function wordstream_update_kwresearch_searches_ratio() {
if (time() > variable_get('wordstream_kwresearch_searches_ratio_updated', 0) + 604800) {
$apiret = xmlrpc('http://www.leveltendesign.com/xmlrpc.php', 'l10seoapi.searches_ratio', 'wordstream');
if (is_numeric($apiret) && $apiret > 0) {
variable_set('wordstream_kwresearch_searches_ratio', (double) $apiret);
}
variable_set('wordstream_kwresearch_searches_ratio_updated', time());
}
}
function wordstream_kwresearch_get_keyword_stats($keywords, $params = NULL) {
$data = wordstream_get_keyword_data($keywords, $msgs, $params);
$ret = array();
if (is_array($data)) {
foreach ($data as $v) {
$ret[$v[0]] = array(
'searches' => $v[1],
);
}
}
return $ret;
}
function wordstream_kwresearch_format_stats_values($values, $keyword = '', $type = 'term') {
switch ($type) {
case 'wordstream_searches':
if ($values['wordstream_searches'] > 0) {
$output = $values['wordstream_searches'] == 'NA' ? 'NA' : number_format($values['wordstream_searches']);
}
else {
$output = $values['wordstream_searches'];
}
break;
default:
return FALSE;
}
return $output;
}