Skip to content

Instantly share code, notes, and snippets.

@RupW
Last active May 16, 2020 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RupW/541e316b241f22e40ce74d0c42dfc445 to your computer and use it in GitHub Desktop.
Save RupW/541e316b241f22e40ce74d0c42dfc445 to your computer and use it in GitHub Desktop.
Combining two shortcode functions for the Moz API https://wordpress.stackexchange.com/q/366687/3276
<?php
// https://wordpress.stackexchange.com/q/366687/3276
function post_title_shortcode(){
return get_the_title();
}
add_shortcode('post_title','post_title_shortcode');
/**
* Make a Moz v1 URL Metrics request for a given domain
*
* @see https://moz.com/help/links-api/v1-archive/v1-url-metrics
* @see https://moz.com/help/links-api/v1-archive/response-fields
* @param $domain
*/
function moz_v1_links_api_request($domain) {
if ( !$domain ) {
return NULL;
}
$cache_key = 'agency_moz_url_metrics_' . $domain;
$url_metrics = get_transient( $cache_key );
if ( false === $url_metrics ) {
// Setting Moz API connection
$accessID = "mozscape-####"; // * Add unique Access ID
$secretKey = "####"; // * Add unique Secret Key
$expires = time() + 300;
$SignInStr = $accessID. "\n" .$expires;
$binarySignature = hash_hmac('sha1', $SignInStr, $secretKey, true);
$SafeSignature = urlencode(base64_encode($binarySignature));
// Connecting to Moz API url
// 103079215140 = 0x1800000024, the flags for pda, upa, ueid and uu respectively
$reqUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($domain)."?Cols=103079215140&AccessID=".$accessID."&Expires=".$expires."&Signature=".$SafeSignature;
// Send request with curl
$opts = array(
CURLOPT_RETURNTRANSFER => true
);
$curlhandle = curl_init($reqUrl);
curl_setopt_array($curlhandle, $opts);
$content = curl_exec($curlhandle);
$status_code = curl_getinfo($curlhandle, CURLINFO_HTTP_CODE);
curl_close($curlhandle);
if ( $status_code < 200 || $status_code >= 300 ) {
// HTTP request failed
error_log('moz_v1_links_api_request for ' . $domain . ' failed: ' . $status_code . ' ' . print_r( $content, true) );
// Cache an empty object for 30 seconds so we retry shortly
$url_metrics = new stdClass();
set_transient( $cache_key, $url_metrics, 30 );
} else {
// Cache the object returned for three days
$url_metrics = json_decode($content);
set_transient( $cache_key, $url_metrics, (60*60*72) );
}
}
return $url_metrics;
}
function moz_score_shortcode($atts) {
extract(
shortcode_atts(
array(
'domain' => get_the_title(),
),
$atts
)
);
if ( ! $domain )
return NULL; // No domain, nothing to return
$url_metrics = moz_v1_links_api_request( $domain );
// Getting 'pda' from Moz API and then rounding
if ( isset( $url_metrics->pda ) ) {
$seo_grade = $url_metrics->pda;
if (is_numeric($seo_grade)) {
$seo_grade = round($seo_grade, 0);
}
} else {
// No value returned
$seo_grade = NULL;
}
return $seo_grade;
}
add_shortcode( 'moz_score','moz_score_shortcode' );
function moz_pa_shortcode($atts) {
extract(
shortcode_atts(
array(
'domain' => get_the_title(),
),
$atts
)
);
if ( ! $domain )
return NULL; // No domain, nothing to return
$url_metrics = moz_v1_links_api_request( $domain );
// Getting 'upa' from Moz API and then rounding
if ( isset( $url_metrics->upa ) ) {
$seop_grade = $url_metrics->upa;
if ( is_numeric( $seop_grade ) ) {
$seop_grade = round($seop_grade, 0);
}
} else {
// No value returned
$seop_grade = NULL;
}
return $seop_grade;
}
add_shortcode( 'moz_pa','moz_pa_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment