/** * run update job to get click counts via manual ajax */ public function refresh_yourls() { // only run on admin if (!is_admin()) { die; } // start our return $ret = array(); // verify our nonce $check = check_ajax_referer('yourls_refresh_nonce', 'nonce', false); // check to see if our nonce failed if (!$check) { $ret['success'] = false; $ret['errcode'] = 'NONCE_FAILED'; $ret['message'] = __('The nonce did not validate.', 'wpyourls'); echo json_encode($ret); die; } // bail if the API key or URL have not been entered if (false === ($api = YOURLSCreator_Helper::get_yourls_api_data())) { $ret['success'] = false; $ret['errcode'] = 'NO_API_DATA'; $ret['message'] = __('No API data has been entered.', 'wpyourls'); echo json_encode($ret); die; } // fetch the IDs that contain a YOURLS url meta key if (false === ($items = YOURLSCreator_Helper::get_yourls_post_ids())) { $ret['success'] = false; $ret['errcode'] = 'NO_POST_IDS'; $ret['message'] = __('There are no items with stored URLs.', 'wpyourls'); echo json_encode($ret); die; } // loop the IDs foreach ($items as $item_id) { // get my click number $clicks = YOURLSCreator_Helper::get_single_click_count($item_id); // bad API call if (empty($clicks['success'])) { $ret['success'] = false; $ret['errcode'] = $clicks['errcode']; $ret['message'] = $clicks['message']; echo json_encode($ret); die; } // got it. update the meta update_post_meta($item_id, '_yourls_clicks', $clicks['clicknm']); } // and do the API return $ret['success'] = true; $ret['message'] = __('The click counts have been updated', 'wpyourls'); echo json_encode($ret); die; }
/** * run update job to get click counts via cron * * @return void */ public function yourls_click_cron() { // bail if the API key or URL have not been entered if (false === ($api = YOURLSCreator_Helper::get_yourls_api_data())) { return; } // fetch the IDs that contain a YOURLS url meta key $items = YOURLSCreator_Helper::get_yourls_post_ids(); // bail if none are present if (empty($items)) { return false; } // loop the IDs foreach ($items as $item_id) { // get my click number $clicks = YOURLSCreator_Helper::get_single_click_count($item_id); // and update my meta if (!empty($clicks['clicknm'])) { update_post_meta($item_id, '_yourls_clicks', $clicks['clicknm']); } } }