Пример #1
0
 /**
  * Create shortlink function
  */
 public function create_yourls()
 {
     // only run on admin
     if (!is_admin()) {
         die;
     }
     // start our return
     $ret = array();
     // verify our nonce
     $check = check_ajax_referer('yourls_editor_create', '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;
     }
     // bail without a post ID
     if (empty($_POST['post_id'])) {
         $ret['success'] = false;
         $ret['errcode'] = 'NO_POST_ID';
         $ret['message'] = __('No post ID was present.', 'wpyourls');
         echo json_encode($ret);
         die;
     }
     // now cast the post ID
     $post_id = absint($_POST['post_id']);
     // bail if we aren't working with a published or scheduled post
     if (!in_array(get_post_status($post_id), YOURLSCreator_Helper::get_yourls_status())) {
         $ret['success'] = false;
         $ret['errcode'] = 'INVALID_STATUS';
         $ret['message'] = __('This is not a valid post status.', 'wpyourls');
         echo json_encode($ret);
         die;
     }
     // do a quick check for a URL
     if (false !== ($link = YOURLSCreator_Helper::get_yourls_meta($post_id, '_yourls_url'))) {
         $ret['success'] = false;
         $ret['errcode'] = 'URL_EXISTS';
         $ret['message'] = __('A URL already exists.', 'wpyourls');
         echo json_encode($ret);
         die;
     }
     // do a quick check for a permalink
     if (false === ($url = YOURLSCreator_Helper::prepare_api_link($post_id))) {
         $ret['success'] = false;
         $ret['errcode'] = 'NO_PERMALINK';
         $ret['message'] = __('No permalink could be retrieved.', 'wpyourls');
         echo json_encode($ret);
         die;
     }
     // check for keyword and get the title
     $keyword = !empty($_POST['keyword']) ? YOURLSCreator_Helper::prepare_api_keyword($_POST['keyword']) : '';
     $title = get_the_title($post_id);
     // set my args for the API call
     $args = array('url' => esc_url($url), 'title' => sanitize_text_field($title), 'keyword' => $keyword);
     // make the API call
     $build = YOURLSCreator_Helper::run_yourls_api_call('shorturl', $args);
     // bail if empty data
     if (empty($build)) {
         $ret['success'] = false;
         $ret['errcode'] = 'EMPTY_API';
         $ret['message'] = __('There was an unknown API error.', 'wpyourls');
         echo json_encode($ret);
         die;
     }
     // bail error received
     if (false === $build['success']) {
         $ret['success'] = false;
         $ret['errcode'] = $build['errcode'];
         $ret['message'] = $build['message'];
         echo json_encode($ret);
         die;
     }
     // we have done our error checking and we are ready to go
     if (false !== $build['success'] && !empty($build['data']['shorturl'])) {
         // get my short URL
         $shorturl = esc_url($build['data']['shorturl']);
         // update the post meta
         update_post_meta($post_id, '_yourls_url', $shorturl);
         update_post_meta($post_id, '_yourls_clicks', '0');
         // and do the API return
         $ret['success'] = true;
         $ret['message'] = __('You have created a new YOURLS link.', 'wpyourls');
         $ret['linkurl'] = $shorturl;
         $ret['linkbox'] = YOURLSCreator_Helper::get_yourls_linkbox($shorturl, $post_id);
         echo json_encode($ret);
         die;
     }
     // we've reached the end, and nothing worked....
     $ret['success'] = false;
     $ret['errcode'] = 'UNKNOWN';
     $ret['message'] = __('There was an unknown error.', 'wpyourls');
     echo json_encode($ret);
     die;
 }
Пример #2
0
 /**
  * Create yourls link on publish if one doesn't exist
  *
  * @param  integer $post_id [description]
  *
  * @return void
  */
 public function yourls_on_save($post_id)
 {
     // bail if this is an import since it'll potentially mess up the process
     if (!empty($_POST['import_id'])) {
         return;
     }
     // run various checks to make sure we aren't doing anything weird
     if (YOURLSCreator_Helper::meta_save_check($post_id)) {
         return;
     }
     // bail if we aren't working with a published or scheduled post
     if (!in_array(get_post_status($post_id), YOURLSCreator_Helper::get_yourls_status('save'))) {
         return;
     }
     // make sure we're working with an approved post type
     if (!in_array(get_post_type($post_id), YOURLSCreator_Helper::get_yourls_types())) {
         return;
     }
     // bail if the API key or URL have not been entered
     if (false === ($api = YOURLSCreator_Helper::get_yourls_api_data())) {
         return;
     }
     // bail if user hasn't checked the box
     if (false === ($onsave = YOURLSCreator_Helper::get_yourls_option('sav'))) {
         return;
     }
     // check for a link and bail if one exists
     if (false !== ($exist = YOURLSCreator_Helper::get_yourls_meta($post_id))) {
         return;
     }
     // get my post URL and title
     $url = YOURLSCreator_Helper::prepare_api_link($post_id);
     $title = get_the_title($post_id);
     // and optional keyword
     $keywd = !empty($_POST['yourls-keyw']) ? YOURLSCreator_Helper::prepare_api_keyword($_POST['yourls-keyw']) : '';
     // set my args for the API call
     $args = array('url' => esc_url($url), 'title' => sanitize_text_field($title), 'keyword' => $keywd);
     // make the API call
     $build = YOURLSCreator_Helper::run_yourls_api_call('shorturl', $args);
     // bail if empty data or error received
     if (empty($build) || false === $build['success']) {
         return;
     }
     // we have done our error checking and we are ready to go
     if (false !== $build['success'] && !empty($build['data']['shorturl'])) {
         // get my short URL
         $shorturl = esc_url($build['data']['shorturl']);
         // update the post meta
         update_post_meta($post_id, '_yourls_url', $shorturl);
         update_post_meta($post_id, '_yourls_clicks', '0');
         // do the action after saving
         do_action('yourls_after_url_save', $post_id, $shorturl);
     }
 }