public function display_results($access_id, $secret_key, $domain)
 {
     try {
         $seomozapi = new SEOMozAPI($access_id, $secret_key);
         $urlmetrics = json_decode($seomozapi->urlmetrics($domain));
         $target_url = preg_replace('!http(s)?:\\/\\/!', '', $domain);
         $attribution = str_replace('/', '%252F', rtrim($target_url, "/"));
         $attribution = "http://www.opensiteexplorer.org/links/?site=" . $attribution;
         $wrapper = new XMLElement('div');
         $info = new XMLElement('div');
         $info->setAttribute('class', 'info');
         //mozRank
         $rank_header = new XMLElement('h4', 'Domain mozRank');
         $info->appendChild($rank_header);
         $rank_info = new XMLElement('p', 'Measure of the mozRank <a href="http://www.opensiteexplorer.org/About#faq_5" target="_blank">(?)</a> of the domain in the Linkscape index');
         $info->appendChild($rank_info);
         $dl_results = new XMLElement('dl');
         //10 Point Score
         $dt_score = new XMLElement('dt', '10-point score');
         $dd_score = new XMLElement('dd', '<a target="_blank" href="' . $attribution . '">' . $urlmetrics->fmrp . '</a>');
         $dl_results->appendChild($dt_score);
         $dl_results->appendChild($dd_score);
         //Raw Score
         $dt_score = new XMLElement('dt', 'Raw score');
         $dd_score = new XMLElement('dd', '<a target="_blank" href="' . $attribution . '">' . $urlmetrics->fmrr . '</a>');
         $dl_results->appendChild($dt_score);
         $dl_results->appendChild($dd_score);
         //Domain Authority
         $dt_score = new XMLElement('dt', 'Domain Authority <a href="http://apiwiki.seomoz.org/w/page/20902104/Domain-Authority/" target="_blank">(?)</a>');
         $dd_score = new XMLElement('dd', '<a target="_blank" href="' . $attribution . '" target="_blank">' . $urlmetrics->pda . '</a>');
         $dl_results->appendChild($dt_score);
         $dl_results->appendChild($dd_score);
         $info->appendChild($dl_results);
         //External Links
         $external_links_header = new XMLElement('h4', 'External Links to Homepage');
         $info->appendChild($external_links_header);
         $dl_results = new XMLElement('dl');
         $dt_score = new XMLElement('dt', 'The number of external (from other subdomains), juice passing links <a href="http://apiwiki.seomoz.org/w/page/13991139/Juice-Passing" target="_blank">(?)</a> to the target URL in the Linkscape index');
         $dd_score = new XMLElement('dd', '<a target="_blank" href="' . $attribution . '" target="_blank">' . $urlmetrics->ueid . '</a>');
         $dl_results->appendChild($dt_score);
         $dl_results->appendChild($dd_score);
         $info->appendChild($dl_results);
         //Links to homepage
         $homepage_links_header = new XMLElement('h4', 'Links to Homepage');
         $info->appendChild($homepage_links_header);
         $dl_results = new XMLElement('dl');
         $dt_score = new XMLElement('dt', 'The number of internal and external, juice and non-juice passing links <a href="http://apiwiki.seomoz.org/w/page/13991139/Juice-Passing" target="_blank">(?)</a> to the target URL in the Linkscape index');
         $dd_score = new XMLElement('dd', '<a href="' . $attribution . '" target="_blank">' . $urlmetrics->uid . '</a>');
         $dl_results->appendChild($dt_score);
         $dl_results->appendChild($dd_score);
         $info->appendChild($dl_results);
         //Homepage mozRank
         $homepage_mozrank_header = new XMLElement('h4', 'Homepage mozRank');
         $info->appendChild($homepage_mozrank_header);
         $homepage_mozrank_info = new XMLElement('p', 'Measure of the mozRank <a href="http://www.opensiteexplorer.org/About#faq_5" target="_blank">(?)</a> of the homepage URL in the Linkscape index');
         $info->appendChild($homepage_mozrank_info);
         $dl_results = new XMLElement('dl');
         //10 Point Score
         $dt_score = new XMLElement('dt', '10-point score');
         $dd_score = new XMLElement('dd', '<a href="' . $attribution . '">' . $urlmetrics->umrp . '</a>');
         $dl_results->appendChild($dt_score);
         $dl_results->appendChild($dd_score);
         //Raw Score
         $dt_score = new XMLElement('dt', 'Raw score');
         $dd_score = new XMLElement('dd', '<a href="' . $attribution . '">' . $urlmetrics->umrr . '</a>');
         $dl_results->appendChild($dt_score);
         $dl_results->appendChild($dd_score);
         //Homepage Authority
         $dt_score = new XMLElement('dt', 'Homepage Authority <a href="http://apiwiki.seomoz.org/Page-Authority" target="_blank">(?)</a>');
         $dd_score = new XMLElement('dd', '<a href="' . $attribution . '" target="_blank">' . $urlmetrics->upa . '</a>');
         $dl_results->appendChild($dt_score);
         $dl_results->appendChild($dd_score);
         $info->appendChild($dl_results);
         $wrapper->appendChild($info);
         return $wrapper;
     } catch (Exception $e) {
         $info = new XMLElement('div');
         $info->setAttribute('class', 'info');
         $info_header = new XMLElement('h4', 'No data found. Check your account details.');
         $info->appendChild($info_header);
         $info_header->appendChild(new XMLElement('p', '<code>' . (string) $e->getMessage() . '</code>'));
         return $info;
     }
 }
function wds_seomoz_settings()
{
    global $wds_options;
    $name = 'wds_seomoz';
    $title = 'Moz';
    $description = __('<p>We make it easy to integrate with Moz - the industry leader in SEO reports.</p>
	<p><a href="http://moz.com/products/api" target="_blank">Sign-up for a free account</a> to gain access to reports that will tell you how your site stacks up against the competition with all of the important SEO measurement tools - ranking, links, and much more.</p>', 'wds');
    $fields = array('authentication' => array('title' => __('Authentication', 'wds'), 'intro' => '', 'options' => array(array('type' => 'text', 'name' => 'access-id', 'title' => __('Access ID', 'wds'), 'description' => ''), array('type' => 'text', 'name' => 'secret-key', 'title' => __('Secret Key', 'wds'), 'description' => ''))));
    $contextual_help = '';
    $target_url = str_replace('http://', '', get_bloginfo('url'));
    //if( $pagenow = 'wds_seomoz' && isset( $_GET['updated'] ) ) { // <-- This is the way it was before. It doesn't really work.
    if (wds_is_wizard_step('4') && isset($_GET['settings-updated'])) {
        // Changed how we determine settings being saved
        delete_transient("seomoz_urlmetrics_{$target_url}");
    }
    $additional = '';
    if (isset($wds_options['access-id']) && isset($wds_options['secret-key'])) {
        $seomozapi = new SEOMozAPI($wds_options['access-id'], $wds_options['secret-key']);
        $urlmetrics = $seomozapi->urlmetrics($target_url);
        $attribution = str_replace('/', '%252F', untrailingslashit($target_url));
        $attribution = "http://www.opensiteexplorer.org/links?site={$attribution}";
        $additional = is_object($urlmetrics) ? '
<h3>' . __('Domain Metrics', 'wds') . '</h3>
<table class="widefat" style="width:500px">
	<thead>
		<tr>
			<th width="75%">' . __('Metric', 'wds') . '</th>
			<th>' . __('Value', 'wds') . '</th>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th>' . __('Metric', 'wds') . '</th>
			<th>' . __('Value', 'wds') . '</th>
		</tr>
	</tfoot>
	<tbody>
		<tr>
			<td><b>' . __('Domain mozRank', 'wds') . '</b><br />Measure of the mozRank <a href="http://www.opensiteexplorer.org/About#faq_5" target="_blank">(?)</a> of the domain in the Linkscape index</td>
			<td>' . sprintf(__('10-point score: %s', 'wds'), "<a href='{$attribution}'>{$urlmetrics->fmrp}</a>") . '<br />' . sprintf(__('Raw score: %s', 'wds'), "<a href='{$attribution}' target='_blank'>{$urlmetrics->fmrr}</a>") . '
			</td>
		</tr>
		<tr class="alt">
			<td><b>' . __('Domain Authority', 'wds') . '</b> <a href="http://apiwiki.seomoz.org/w/page/20902104/Domain-Authority/" target="_blank">(?)</a></td>
			<td><a href="' . $attribution . '" target="_blank">' . (!empty($urlmetrics->pda) ? $urlmetrics->pda : '') . '</a></td>
		</tr>
		<tr>
			<td><b>' . __('External Links to Homepage', 'wds') . '</b><br />The number of external (from other subdomains), juice passing links <a href="http://apiwiki.seomoz.org/w/page/13991139/Juice-Passing" target="_blank">(?)</a> to the target URL in the Linkscape index </td>
			<td><a href="' . $attribution . '" target="_blank">' . (!empty($urlmetrics->ueid) ? $urlmetrics->ueid : '') . '</a></td>
		</tr>
		<tr>
			<td><b>' . __('Links to Homepage', 'wds') . '</b><br />The number of internal and external, juice and non-juice passing links <a href="http://apiwiki.seomoz.org/w/page/13991139/Juice-Passing" target="_blank">(?)</a> to the target URL in the Linkscape index</td>
			<td><a href="' . $attribution . '" target="_blank">' . (!empty($urlmetrics->uid) ? $urlmetrics->uid : '') . '</a></td>
		</tr>
		<tr>
			<td><b>' . __('Homepage mozRank', 'wds') . '</b><br />Measure of the mozRank <a href="http://www.opensiteexplorer.org/About#faq_5" target="_blank">(?)</a> of the homepage URL in the Linkscape index</td>
			<td>' . sprintf(__('10-point score: %s', 'wds'), "<a href='{$attribution}'>{$urlmetrics->umrp}</a>") . '<br />' . sprintf(__('Raw score: %s', 'wds'), "<a href='{$attribution}' target='_blank'>{$urlmetrics->umrr}</a>") . '</td>
		</tr>
		<tr>
			<td><b>' . __('Homepage Authority', 'wds') . '</b> <a href="http://apiwiki.seomoz.org/Page-Authority" target="_blank">(?)</a></td>
			<td><a href="' . $attribution . '" target="_blank">' . (!empty($urlmetrics->upa) ? $urlmetrics->upa : '') . '</a></td>
		</tr>
	</tbody>
</table>
<p>' . __('For posts / pages specific metrics refer to the Moz URL metrics module on the Edit Post / Page screen,', 'wds') . '</p>
' : '<p>' . sprintf(__('Unable to retrieve data from the Moz API. Error: %s.', 'wds'), $urlmetrics) . '</p>';
    }
    $additional .= '<p><a href="http://moz.com/" target="_blank"><img src="' . WDS_PLUGIN_URL . 'images/linkscape-logo.png" title="Moz Linkscape API" /></a></p>';
    if (wds_is_wizard_step('4')) {
        $settings = new WDS_Core_Admin_Tab($name, $title, $description, $fields, 'wds', $contextual_help, $additional);
    }
}
function wds_seomoz_dashboard_widget()
{
    global $wds_options;
    if (!isset($wds_options['access-id']) || !isset($wds_options['secret-key'])) {
        _e('<p>Moz credentials not properly set up.</p>');
        return;
    }
    $target_url = preg_replace('!http(s)?:\\/\\/!', '', get_bloginfo('url'));
    $seomozapi = new SEOMozAPI($wds_options['access-id'], $wds_options['secret-key']);
    $urlmetrics = $seomozapi->urlmetrics($target_url);
    $attribution = str_replace('/', '%252F', untrailingslashit($target_url));
    //$attribution = "http://www.opensiteexplorer.org/$attribution/a";
    $attribution = "http://www.opensiteexplorer.org/links?site={$attribution}";
    if (!is_object($urlmetrics)) {
        printf(__('Unable to retrieve data from the Moz API. Error: %s.', 'wds'), $urlmetrics);
        return;
    }
    echo '<h4>' . __('Domain Metrics', 'wds') . ' (' . $target_url . ')</h4>
<table class="widefat">
	<thead>
		<tr>
			<th width="75%">' . __('Metric', 'wds') . '</th>
			<th>' . __('Value', 'wds') . '</th>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th>' . __('Metric', 'wds') . '</th>
			<th>' . __('Value', 'wds') . '</th>
		</tr>
	</tfoot>
	<tbody>
		<tr>
			<td><b>' . __('Domain mozRank', 'wds') . '</b><br />Measure of the mozRank <a href="http://www.opensiteexplorer.org/About#faq_5" target="_blank">(?)</a> of the domain in the Linkscape index</td>
			<td>' . sprintf(__('10-point score: %s', 'wds'), "<a href='{$attribution}'>{$urlmetrics->fmrp}</a>") . '<br />' . sprintf(__('Raw score: %s', 'wds'), "<a href='{$attribution}' target='_blank'>{$urlmetrics->fmrr}</a>") . '
			</td>
		</tr>
		<tr class="alt">
			<td><b>' . __('Domain Authority', 'wds') . '</b> <a href="http://apiwiki.seomoz.org/w/page/20902104/Domain-Authority/" target="_blank">(?)</a></td>
			<td><a href="' . $attribution . '" target="_blank">' . (!empty($urlmetrics->pda) ? $urlmetrics->pda : '') . '</a></td>
		</tr>
		<tr>
			<td><b>' . __('External Links to Homepage', 'wds') . '</b><br />The number of external (from other subdomains), juice passing links <a href="http://apiwiki.seomoz.org/w/page/13991139/Juice-Passing" target="_blank">(?)</a> to the target URL in the Linkscape index </td>
			<td><a href="' . $attribution . '" target="_blank">' . (!empty($urlmetrics->ueid) ? $urlmetrics->ueid : '') . '</a></td>
		</tr>
		<tr>
			<td><b>' . __('Links to Homepage', 'wds') . '</b><br />The number of internal and external, juice and non-juice passing links <a href="http://apiwiki.seomoz.org/w/page/13991139/Juice-Passing" target="_blank">(?)</a> to the target URL in the Linkscape index</td>
			<td><a href="' . $attribution . '" target="_blank">' . (!empty($urlmetrics->uid) ? $urlmetrics->uid : '') . '</a></td>
		</tr>
		<tr>
			<td><b>' . __('Homepage mozRank', 'wds') . '</b><br />Measure of the mozRank <a href="http://www.opensiteexplorer.org/About#faq_5" target="_blank">(?)</a> of the homepage URL in the Linkscape index</td>
			<td>' . sprintf(__('10-point score: %s', 'wds'), "<a href='{$attribution}'>{$urlmetrics->umrp}</a>") . '<br />' . sprintf(__('Raw score: %s', 'wds'), "<a href='{$attribution}' target='_blank'>{$urlmetrics->umrr}</a>") . '</td>
		</tr>
		<tr>
			<td><b>' . __('Homepage Authority', 'wds') . '</b> <a href="http://apiwiki.seomoz.org/Page-Authority" target="_blank">(?)</a></td>
			<td><a href="' . $attribution . '" target="_blank">' . (!empty($urlmetrics->upa) ? $urlmetrics->upa : '') . '</a></td>
		</tr>
	</tbody>
</table>
<p>' . __('For posts / pages specific metrics refer to the Moz URL metrics module on the Edit Post / Page screen', 'wds') . '</p>' . '<p><a href="http://moz.com/" target="_blank"><img src="' . WDS_PLUGIN_URL . 'images/linkscape-logo.png" title="Moz Linkscape API" /></a></p>';
}
function wds_seomoz_urlmetrics_box($post)
{
    global $wds_options;
    $page = str_replace('/', '%252F', untrailingslashit(str_replace('http://', '', get_permalink($post->ID))));
    $seomozapi = new SEOMozAPI($wds_options['access-id'], $wds_options['secret-key']);
    $urlmetrics = $seomozapi->urlmetrics($page);
    ?>
<table class="widefat">
	<tbody>
		<tr class="alt">
			<th width="30%"><?php 
    _e('Metric', 'wds');
    ?>
</th>
			<th>Value</th>
		</tr>
		<tr>
			<th><?php 
    _e('External Links', 'wds');
    ?>
</th>
			<td><p><a href="http://www.opensiteexplorer.org/links?site=<?php 
    echo $page;
    ?>
" target="_blank"><?php 
    echo !empty($urlmetrics->ueid) ? $urlmetrics->ueid : '';
    ?>
</a></p></td>
		</tr>
		<tr>
			<th><?php 
    _e('Links', 'wds');
    ?>
</th>
			<td><p><a href="http://www.opensiteexplorer.org/links?site=<?php 
    echo $page;
    ?>
" target="_blank"><?php 
    echo !empty($urlmetrics->uid) ? $urlmetrics->uid : '';
    ?>
</a></p></td>
		</tr>
		<tr>
			<th><?php 
    _e('mozRank', 'wds');
    ?>
</th>
			<td><p><?php 
    echo '<b>' . __('10-point score:', 'wds') . '</b> <a href="http://www.opensiteexplorer.org/links?site=' . $page . '" target="_blank">' . (!empty($urlmetrics->umrp) ? $urlmetrics->umrp : '') . '</a><br /><br /><b>' . __('Raw score:', 'wds') . '</b> <a href="http://www.opensiteexplorer.org/links?site=' . $page . '" target="_blank">' . (!empty($urlmetrics->umrr) ? $urlmetrics->umrr : '');
    ?>
</a></p></td>
		</tr>
		<tr>
			<th><?php 
    _e('Page Authority', 'wds');
    ?>
</th>
			<td><p><a href="http://www.opensiteexplorer.org/links?site=<?php 
    echo $page;
    ?>
" target="_blank"><?php 
    echo !empty($urlmetrics->upa) ? $urlmetrics->upa : '';
    ?>
</a></p></td>
		</tr>
	</tbody>
</table>
<?php 
    echo '<p><a href="http://moz.com/" target="_blank"><img src="' . WDS_PLUGIN_URL . 'images/linkscape-logo.png" title="Moz Linkscape API" /></a></p>';
}