/** * our pre-built template tag * * @return [type] [description] */ public function yourls_display($echo = true) { // fetch the global post object global $post; // check existing postmeta for YOURLS $link = YOURLSCreator_Helper::get_yourls_meta($post->ID); // bail if there is no shortlink if (empty($link)) { return; } // set an empty $show = ''; // build the markup $show .= '<p class="yourls-display">' . __('Shortlink:', 'wpyourls'); $show .= '<input id="yourls-link" size="28" title="' . __('click to highlight', 'wpyourls') . '" type="text" name="yourls-link" value="' . esc_url($link) . '" readonly="readonly" tabindex="501" onclick="this.focus();this.select()" />'; $show .= '</p>'; // echo the box if requested if ($echo === true) { echo apply_filters('yourls_template_tag', $show, $post->ID); } // return the box return apply_filters('yourls_template_tag', $show, $post->ID); }
function get_yourls_shortlink($post_id = 0, $echo = false) { // fetch the post ID if not provided if (empty($post_id)) { // call the object global $post; // bail if missing if (empty($post) || !is_object($post)) { return; } // set my post ID $post_id = absint($post->ID); } // check for the link if (false === ($link = YOURLSCreator_Helper::get_yourls_meta($post_id))) { return; } // echo the link if requested if ($echo === true) { echo esc_url($link); } // return the link return esc_url($link); }
/** * Create shortlink function inline. Called on ajax */ public function inline_yourls() { // only run on admin if (!is_admin()) { die; } // start our return $ret = array(); // 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; } // verify our nonce $check = check_ajax_referer('yourls_inline_create_' . absint($post_id), '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; } // 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; } // get my post URL and title $title = get_the_title($post_id); // set my args for the API call $args = array('url' => esc_url($url), 'title' => sanitize_text_field($title)); // 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['rowactn'] = '<span class="update-yourls">' . YOURLSCreator_Helper::update_row_action($post_id) . '</span>'; 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; }
/** * the action row link based on the status * * @param [type] $actions [description] * @param [type] $post [description] * @return [type] [description] */ public function yourls_row_action($actions, $post) { // make sure we're working with an approved post type if (!in_array($post->post_type, YOURLSCreator_Helper::get_yourls_types())) { return $actions; } // 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())) { return $actions; } // check for existing and add our new action if (false === ($exist = YOURLSCreator_Helper::get_yourls_meta($post->ID))) { $actions['create-yourls'] = YOURLSCreator_Helper::create_row_action($post->ID); } else { $actions['update-yourls'] = YOURLSCreator_Helper::update_row_action($post->ID); } // return the actions return $actions; }
/** * our pre-built template tag * * @return [type] [description] */ public function yourls_display($post_id = 0, $echo = false) { // no display exist on non-singular items, so bail if (!is_singular()) { return; } // fetch the post ID if not provided if (empty($post_id)) { // call the object global $post; // bail if missing if (empty($post) || !is_object($post) || empty($post->ID)) { return; } // set my post ID $post_id = absint($post->ID); } // check for the link if (false === ($link = YOURLSCreator_Helper::get_yourls_meta($post_id))) { return; } // set an empty $show = ''; // build the markup $show .= '<p class="yourls-display">' . __('Shortlink:', 'wpyourls'); $show .= '<input id="yourls-link-' . absint($post_id) . '" class="yourls-link" size="28" title="' . __('click to highlight', 'wpyourls') . '" type="url" name="yourls-link-' . absint($post_id) . '" value="' . esc_url($link) . '" readonly="readonly" tabindex="501" onclick="this.focus();this.select()" />'; $show .= '</p>'; // echo the box if requested if (!empty($echo)) { echo apply_filters('yourls_template_tag', $show, $post_id); } // return the box return apply_filters('yourls_template_tag', $show, $post_id); }
/** * Filter wp_shortlink with new YOURLS link * * @param [type] $shortlink [description] * @param [type] $id [description] * @param [type] $context [description] * @return [type] [description] */ public function yourls_shortlink($shortlink, $id, $context) { // no shortlinks exist on non-singular items, so bail if (!is_singular()) { return; } // Look for the post ID passed by wp_get_shortlink() first if (empty($id)) { // call the global post object global $post; // and get the ID $id = absint($post->ID); } // Fall back in case we still don't have a post ID if (empty($id)) { return !empty($shortlink) ? $shortlink : false; } // check existing postmeta for YOURLS $custom = YOURLSCreator_Helper::get_yourls_meta($id); // return the custom YOURLS link or the regular one return !empty($custom) ? $custom : $shortlink; }