'login' && $module <> 'register') {
return false;
}
//predefined args
$args = array(
'user_url' => get_bloginfo('url'),
'lang' => get_bloginfo('language'),
'token' => SQ_Classes_Helpers_Tools::getOption('sq_api'),
'versq' => (int)str_replace('.', '', SQ_VERSION)
);
//predefined options
$options = array_merge(
array(
'method' => self::$apimethod,
'sslverify' => false,
'timeout' => 5,
'headers' => array(
'USER-TOKEN' => SQ_Classes_Helpers_Tools::getOption('sq_api'),
'USER-URL' => get_bloginfo('url'),
'LANG' => get_bloginfo('language'),
'VERSQ' => (int)str_replace('.', '', SQ_VERSION)
)
),
$options);
try {
$args = array_merge($args, $params);
foreach ($args as $key => $value) {
if ($value <> '') {
$parameters .= ($parameters == "" ? "" : "&") . $key . "=" . urlencode($value);
}
}
//call it with http to prevent curl issues with ssls
$url = self::cleanUrl(((self::$apiversion == 1) ? _SQ_API_URL_ : _SQ_APIV2_URL_) . $module . "?" . $parameters);
//echo $url .'
';
if (!isset(self::$cache[md5($url)])) {
if ($options['method'] == 'post') {
$options['body'] = $args;
}
self::$cache[md5($url)] = self::sq_wpcall($url, $options);
}
return self::$cache[md5($url)];
} catch (Exception $e) {
return '';
}
}
/**
* Clear the url before the call
* @param string $url
* @return string
*/
private static function cleanUrl($url) {
return str_replace(array(' '), array('+'), $url);
}
public static function generatePassword($length = 12) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $password;
}
/**
* Get My Squirrly Link
*
* @param $path
* @return string
*/
public static function getMySquirrlyLink($path) {
if (SQ_Classes_Helpers_Tools::getMenuVisible('show_panel')) {
return _SQ_DASH_URL_ . 'login/?token=' . SQ_Classes_Helpers_Tools::getOption('sq_api') . '&user_url=' . get_bloginfo('url') . '&redirect_to=' . _SQ_DASH_URL_ . 'user/' . $path;
} else {
return _SQ_DASH_URL_;
}
}
/**
* Get API Link
*
* @param string $path
* @param integer $version
* @return string
*/
public static function getApiLink($path) {
return _SQ_APIV2_URL_ . $path . '?token=' . SQ_Classes_Helpers_Tools::getOption('sq_api') . '&url=' . get_bloginfo('url');
}
/**
* Use the WP remote call
*
* @param $url
* @param $options
* @return array|bool|string|WP_Error
*/
public static function sq_wpcall($url, $options) {
$method = $options['method'];
switch ($method) {
case 'get':
unset($options['method']);
$response = wp_remote_get($url, $options);
break;
case 'post':
unset($options['method']);
$response = wp_remote_post($url, $options);
break;
default:
$response = wp_remote_request($url, $options);
break;
}
if (is_wp_error($response)) {
SQ_Classes_Error::setError($response->get_error_message(), 'sq_error');
return false;
}
$response = self::cleanResponce(wp_remote_retrieve_body($response)); //clear and get the body
SQ_Debug::dump('wp_remote_get', $method, $url, $options, $response); //output debug
return $response;
}
/**
* Get the Json from responce if any
* @param string $response
* @return string
*/
private static function cleanResponce($response) {
return trim($response, '()');
}
/********************** API CALLs ******************************/
/**
* Login user to API
*
* @param array $args
* @return bool|WP_Error
*/
public static function login($args = array()) {
self::$apiversion = 1; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('login', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
return $json;
}
/**
* Register user to API
*
* @param array $args
* @return bool|WP_Error
*/
public static function register($args = array()) {
self::$apiversion = 1; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('register', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
return $json;
}
/**
* Checkin on API v2
*
* @param array $args
* @return bool|WP_Error
*/
public static function checkin($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('api/user/checkin', $args));
if (isset($json->error) && $json->error <> '') {
if($json->error == 'invalid_token'){
SQ_Classes_Helpers_Tools::saveOptions('sq_api',false);
}
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
//Save the connections into database
if (isset($json->data->connection_gsc) && isset($json->data->connection_ga)) {
$connect = SQ_Classes_Helpers_Tools::getOption('connect');
$connect['google_analytics'] = $json->data->connection_ga;
$connect['google_search_console'] = $json->data->connection_gsc;
SQ_Debug::dump($connect);
SQ_Classes_Helpers_Tools::saveOptions('connect', $connect);
}
return $json->data;
}
return false;
}
/**
* Get the API stats for this blog
* @return array
*/
public static function getStats() {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
$args = $stats = array();
if ($json = json_decode(self::apiCall('api/user/stats', $args))) {
if (isset($json->error) && $json->error <> '') {
if($json->error == 'invalid_token'){
SQ_Classes_Helpers_Tools::saveOptions('sq_api',false);
}
return (new WP_Error('api_error', $json->error));
}
if (!isset($json->data)) {
return (new WP_Error('api_error', 'no_data'));
}
$data = $json->data;
if (isset($data->optimized_articles) && isset($data->average_optimization) && isset($data->kr_research) && isset($data->kr_in_briefcase)
&& isset($data->ranked_top) && isset($data->audits_made)) {
//do not show stats if not optimization is made
if ($data->optimized_articles == 0 && $data->kr_research == 0) {
return false;
}
//Get last month articles
$stats['all_articles'] = array();
$stats['all_articles']['value'] = ((int)$data->optimized_articles);
$stats['all_articles']['text'] = __('Articles optimized so far', _SQ_PLUGIN_NAME_);
$stats['all_articles']['link'] = SQ_Classes_Helpers_Tools::getAdminUrl('post-new.php');
$stats['all_articles']['linktext'] = __('add post', _SQ_PLUGIN_NAME_);
//Get last month articles
$stats['avg_articles'] = array();
$stats['avg_articles']['value'] = ((int)$data->average_optimization);
$stats['avg_articles']['text'] = __('Average optimization', _SQ_PLUGIN_NAME_);
$stats['avg_articles']['link'] = SQ_Classes_Helpers_Tools::getAdminUrl('sq_assistant');
$stats['avg_articles']['linktext'] = __('add post', _SQ_PLUGIN_NAME_);
//Get all keyword researched
$stats['all_researches'] = array();
$stats['all_researches']['value'] = (int)$data->kr_research;
$stats['all_researches']['text'] = __('All Keyword Researches performed for all websites', _SQ_PLUGIN_NAME_);
$stats['all_researches']['link'] = SQ_Classes_Helpers_Tools::getAdminUrl('sq_research');
$stats['all_researches']['linktext'] = __('do research', _SQ_PLUGIN_NAME_);
//Get all keywords from briefcase
$stats['all_briefcase'] = array();
$stats['all_briefcase']['value'] = (int)$data->kr_in_briefcase;
$stats['all_briefcase']['text'] = __('Keywords stored in Squirrly Briefcase', _SQ_PLUGIN_NAME_);
$stats['all_briefcase']['link'] = SQ_Classes_Helpers_Tools::getAdminUrl('sq_research', 'briefcase');
$stats['all_briefcase']['linktext'] = __('add keyword', _SQ_PLUGIN_NAME_);
//Get the top 100 ranking
$stats['top_ranking'] = array();
$stats['top_ranking']['value'] = (int)$data->ranked_top;
$stats['top_ranking']['text'] = __('Pages ranking in top 100 Google', _SQ_PLUGIN_NAME_);
$stats['top_ranking']['link'] = SQ_Classes_Helpers_Tools::getAdminUrl('sq_rankings');
$stats['top_ranking']['linktext'] = __('see rankings', _SQ_PLUGIN_NAME_);
//Get last month audits
$stats['lm_audit'] = array();
$stats['lm_audit']['value'] = (int)$data->audits_made;
$stats['lm_audit']['text'] = __('SEO Audits', _SQ_PLUGIN_NAME_);
$stats['lm_audit']['link'] = SQ_Classes_Helpers_Tools::getAdminUrl('sq_audits');
$stats['lm_audit']['linktext'] = __('see audits', _SQ_PLUGIN_NAME_);
}
}
return $stats;
}
/**
* Get audits from API
* @param array $args
* @return array|bool
*/
public static function getBlogAudits($args = array()) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('audit/get-blog-audits', $args));
if (isset($json->audits)) {
return $json->audits;
}
return false;
}
/******************************** BRIEFCASE *********************/
public static function getBriefcase($args = array()) {
self::$apiversion = 2; //api v1
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('api/briefcase/get', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
public static function addBriefcaseKeyword($args = array()) {
self::$apiversion = 2; //api v1
self::$apimethod = 'post'; //call method
$json = json_decode(self::apiCall('api/briefcase/add', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
public static function removeBriefcaseKeyword($args = array()) {
self::$apiversion = 2; //api v1
self::$apimethod = 'post'; //call method
if ($json = json_decode(self::apiCall('api/briefcase/hide', $args))) {
return $json;
}
return false;
}
public static function getBriefcaseStats($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('api/briefcase/stats', $args));
if (isset($json->error) && $json->error <> '') {
if($json->error == 'invalid_token'){
SQ_Classes_Helpers_Tools::saveOptions('sq_api',false);
}
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
public static function saveBriefcaseKeywordLabel($args = array()) {
self::$apiversion = 2; //api v1
self::$apimethod = 'post'; //call method
$json = json_decode(self::apiCall('api/briefcase/label/keyword', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
public static function addBriefcaseLabel($args = array()) {
self::$apiversion = 2; //api v1
self::$apimethod = 'post'; //call method
$json = json_decode(self::apiCall('api/briefcase/label/add', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
public static function saveBriefcaseLabel($args = array()) {
self::$apiversion = 2; //api v1
self::$apimethod = 'post'; //call method
$json = json_decode(self::apiCall('api/briefcase/label/save', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
public static function removeBriefcaseLabel($args = array()) {
self::$apiversion = 2; //api v1
self::$apimethod = 'post'; //call method
$json = json_decode(self::apiCall('api/briefcase/label/delete', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
/******************************** KEYWORD RESEARCH ****************/
public static function getKROthers($args = array()) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
if ($json = json_decode(self::apiCall('kr/other', $args))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
return $json;
}
return false;
}
/**
* Get Keyword Research Suggestion
*
* @param array $args
* @return array|bool|mixed|object|WP_Error
*/
public static function getKRSuggestion($args = array()) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
if ($json = json_decode(self::apiCall('kr/suggestion', $args, array('timeout' => 300)))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
return $json;
}
return false;
}
/**
* Get Keyword Research History
* @param array $args
* @return array|bool|mixed|object|WP_Error
*/
public static function getKRHistory($args = array()) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('kr/history/json/1', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->json)) {
return $json->json;
}
return false;
}
/**
* Get the Kr Found by API
* @param array $args
* @return bool|WP_Error
*/
public static function getKrFound($args = array()) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('kr/found', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->json)) {
return $json->json;
}
return false;
}
/**
* Get KR Countries
* @param array $args
* @return bool|WP_Error
*/
public static function getKrCountries($args = array()) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
$args['json'] = true;
$json = json_decode(self::apiCall('kr/countries', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->json)) {
return $json->json;
}
return false;
}
/******************** WP Posts ***************************/
/**
* Save the post status on API
* @param array $args
* @return bool
*/
public static function savePost($args = array()) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
return json_decode(self::apiCall('seo/post', $args));
}
/**
* Get the post optimization
*
* @param array $args
* @return array|mixed|object
*/
public static function getPostOptimization($args = array()) {
self::$apiversion = 1; //api v1
return json_decode(self::apiCall('seo/get-optimization', $args));
}
/**
* Update the post status on API
* @param array $args
* @return bool
*/
public static function updatePost($args = array()) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
return json_decode(self::apiCall('seo/update', $args));
}
/******************** RANKINGS ***************************/
/**
* Add a keyword in Rank Checker
* @param array $args
* @return bool|WP_Error
*/
public static function addSerpKeyword($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'post'; //call method
$json = json_decode(self::apiCall('api/briefcase/serp', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
/**
* Delete a keyword from Rank Checker
* @param array $args
* @return bool|WP_Error
*/
public static function deleteSerpKeyword($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'post'; //call method
$json = json_decode(self::apiCall('api/briefcase/serp-delete', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
/**
* Get the Ranks for this blog
* @param array $args
* @return bool|WP_Error
*/
public static function getRanksStats($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('api/serp/stats', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
/**
* Get the Ranks for this blog
* @param array $args
* @return bool|WP_Error
*/
public static function getRanks($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('api/serp/get-ranks', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
/**
* Refresh the rank for a page/post
*
* @param array $args
* @return bool|WP_Error
*/
public static function checkPostRank($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('api/serp/refresh', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return true;
}
/******************** FOCUS PAGES ***********************/
/**
* Get all focus pages and add them in the SQ_Models_Domain_FocusPage object
* Add the audit data for each focus page
* @param array $args
* @return SQ_Models_Domain_FocusPage|WP_Error|false
*/
public static function getFocusPages($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('api/posts/focus', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
return false;
}
/**
* Get the focus page audit
*
* @param array $args
* @return bool|WP_Error
*/
public static function getFocusAudits($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
$json = json_decode(self::apiCall('api/audits/focus', $args));
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data) && !empty($json->data)) {
return $json->data;
}
return false;
}
/**
* Get the audit for a specific page
* @param array $args
* @return array|bool|mixed|object|WP_Error
*/
public static function getSeoAudit($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //call method
if ($json_audit = json_decode(self::apiCall('api/audits/old', $args))) {
if (isset($json_audit->error) && $json_audit->error <> '') {
return (new WP_Error($json_audit->error));
}
if (isset($json_audit->data)) {
return json_decode(json_encode($json_audit->data));
}
}
return false;
}
/**
* Add Focus Page
* @param array $args
* @return bool|WP_Error
*/
public static function addFocusPage($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'post'; //post call
if ($json = json_decode(self::apiCall('api/posts/set-focus', $args))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
}
return false;
}
public static function updateFocusPage($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'post'; //post call
if ($json = json_decode(self::apiCall('api/posts/update-focus', $args))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
}
return false;
}
/**
* Delete the Focus Page
* @param array $args
* @return bool|WP_Error
*/
public static function deleteFocusPage($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'post'; //post call
if (isset($args['user_post_id']) && $args['user_post_id'] > 0) {
if ($json = json_decode(self::apiCall('api/posts/remove-focus/' . $args['user_post_id']))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
}
}
return false;
}
/**************************************** CONNECTIONS */
/**
* Disconnect Google Analytics account
* @param array $args
* @return bool|WP_Error
*/
public static function revokeGaConnection($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //post call
if ($json = json_decode(self::apiCall('api/ga/revoke'))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
}
return false;
}
/**
* Disconnect Google Search Console account
* @param array $args
* @return bool|WP_Error
*/
public static function revokeGscConnection($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //post call
if ($json = json_decode(self::apiCall('api/gsc/revoke'))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
}
return false;
}
public static function syncGSC($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //post call
if ($json = json_decode(self::apiCall('api/gsc/sync/kr', $args))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
}
return false;
}
/******************** OTHERS *****************************/
public static function crawlURL($args = array()) {
self::$apiversion = 2; //api v2
self::$apimethod = 'get'; //post call
if ($json = json_decode(self::apiCall('api/url/crawl', $args, array('timeout' => 60)))) {
if (isset($json->error) && $json->error <> '') {
return (new WP_Error('api_error', $json->error));
}
if (isset($json->data)) {
return $json->data;
}
}
return false;
}
public static function saveSettings($args) {
self::$apiversion = 1; //api v1
self::$apimethod = 'get'; //call method
self::apiCall('user/settings', array('settings' => json_encode($args)));
}
/**
* Load the JS for API
*/
public static function loadJsVars() {
global $post;
$sq_postID = (isset($post->ID) ? $post->ID : 0);
echo '';
}
public static function versionOne() {
return 1;
}
public static function versionTwo() {
return 2;
}
}