/** * convert from Ozh (and Otto's) plugin */ public function convert_yourls() { // only run on admin if (!is_admin()) { die; } // verify our nonce $check = check_ajax_referer('yourls_convert_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; } // filter our key to replace $key = apply_filters('yourls_key_to_convert', 'yourls_shorturl'); // fetch the IDs that contain a YOURLS url meta key if (false === ($items = YOURLSCreator_Helper::get_yourls_post_ids($key))) { $ret['success'] = false; $ret['errcode'] = 'NO_KEYS'; $ret['message'] = __('There are no meta keys to convert.', 'wpyourls'); echo json_encode($ret); die; } // set up SQL query global $wpdb; // prepare my query $setup = $wpdb->prepare("\n\t\t\tUPDATE {$wpdb->postmeta}\n\t\t\tSET meta_key = '%s'\n\t\t\tWHERE meta_key = '%s'\n\t\t\t", esc_sql('_yourls_url'), esc_sql($key)); // run SQL query $query = $wpdb->query($setup); // start our return $ret = array(); // no matches, return message if ($query == 0) { $ret['success'] = false; $ret['errcode'] = 'KEY_MISSING'; $ret['message'] = __('There are no keys matching this criteria. Please try again.', 'wpyourls'); echo json_encode($ret); die; } // we had matches. return the success message with a count if ($query > 0) { $ret['success'] = true; $ret['errcode'] = null; $ret['updated'] = $query; $ret['message'] = sprintf(_n('%d key has been updated.', '%d keys have been updated.', $query, 'wpyourls'), $query); 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']); } } }