/** * 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); }
/** * check the YOURLS install for existing links * and pull the data if it exists */ public function import_yourls() { // only run on admin if (!is_admin()) { die; } // verify our nonce $check = check_ajax_referer('yourls_import_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; } // set my args for the API call $args = array('filter' => 'top', 'limit' => apply_filters('yourls_import_limit', 999)); // make the API call $fetch = YOURLSCreator_Helper::run_yourls_api_call('stats', $args); // bail if empty data if (empty($fetch)) { $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 === $fetch['success']) { $ret['success'] = false; $ret['errcode'] = $build['errcode']; $ret['message'] = $build['message']; echo json_encode($ret); die; } // bail error received if (empty($fetch['data']['links'])) { $ret['success'] = false; $ret['errcode'] = 'NO_LINKS'; $ret['message'] = __('There was no available link data to import.', 'wpyourls'); echo json_encode($ret); die; } // filter the incoming for matching links $filter = YOURLSCreator_Helper::filter_yourls_import($fetch['data']['links']); // bail error received if (empty($filter)) { $ret['success'] = false; $ret['errcode'] = 'NO_MATCHING_LINKS'; $ret['message'] = __('There were no matching links to import.', 'wpyourls'); echo json_encode($ret); die; } // set a false flag $error = false; // now filter them foreach ($filter as $item) { // do the import $import = YOURLSCreator_Helper::maybe_import_link($item); // bail error received if (empty($import)) { $error = true; break; } } // bail if we had true on the import if (true === $error) { $ret['success'] = false; $ret['errcode'] = 'NO_IMPORT_ACTION'; $ret['message'] = __('The data could not be imported.', 'wpyourls'); echo json_encode($ret); die; } // hooray. it worked. do the ajax return if (false === $error) { $ret['success'] = true; $ret['message'] = __('All available YOURLS data has been imported.', 'wpyourls'); 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 status sidebox */ public static function sidebox_status() { // get my API status data if (false === ($data = YOURLSCreator_Helper::get_api_status_data())) { return; } ?> <div id="yourls-admin-status" class="postbox yourls-sidebox"> <h3 class="hndle" id="status-sidebar"><?php echo $data['icon']; _e('API Status Check', 'wpyourls'); ?> </h3> <div class="inside"> <form> <p class="api-status-text"><?php echo esc_attr($data['text']); ?> </p> <p class="api-status-actions"> <input type="button" class="yourls-click-status button-primary" value="<?php _e('Check Status', 'wpyourls'); ?> " > <span class="spinner yourls-spinner yourls-status-spinner"></span> <?php wp_nonce_field('yourls_status_nonce', 'yourls_status', false, true); ?> </p> </form> </div> </div> <?php }
/** * make a API request to the YOURLS server * * @param string $action [description] * @param array $args [description] * @param string $format [description] * @return [type] [description] */ public static function run_yourls_api_call($action = '', $args = array(), $user = true, $format = 'json', $decode = true) { // bail if no action is passed if (empty($action)) { return array('success' => false, 'errcode' => 'MISSING_ACTION', 'message' => __('No API action was provided.', 'wpyourls')); } // bail if an invalid action is passed if (!in_array($action, array('shorturl', 'expand', 'url-stats', 'stats', 'db-stats'))) { return array('success' => false, 'errcode' => 'INVALID_ACTION', 'message' => __('The API action was invalid.', 'wpyourls')); } // bail if the API key or URL have not been entered if (false === ($apikey = self::get_yourls_api_data('key'))) { return array('success' => false, 'errcode' => 'NO_API_DATA', 'message' => __('No data was returned from the API call.', 'wpyourls')); } // only fire if user has the option if (false !== $user && false === ($check = YOURLSCreator_Helper::check_yourls_cap('ajax'))) { return array('success' => false, 'errcode' => 'INVALID_USER', 'message' => __('The user requesting the URL does not have authorization.', 'wpyourls')); } // set the default query args with the required items $base = array('signature' => esc_attr($apikey), 'action' => esc_attr($action), 'format' => esc_attr($format)); // now add our optional args $args = !empty($args) ? array_merge($args, $base) : $base; // construct the args for a remote POST $build = wp_remote_post(self::get_yourls_api_url(), array('method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'sslverify' => false, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => $args, 'cookies' => array())); // bail on empty return if (empty($build)) { return array('success' => false, 'errcode' => 'EMPTY_RESPONSE', 'message' => __('The response from the API was empty.', 'wpyourls')); } // bail on wp_error if (is_wp_error($build)) { return array('success' => false, 'errcode' => 'API_ERROR', 'message' => $build->get_error_message()); } // get our response code $code = wp_remote_retrieve_response_code($build); // bail on a not 200 if ($code !== 200) { return array('success' => false, 'errcode' => 'RESPONSE_CODE', 'message' => sprintf(__('The API call returned a %s response code.', 'wpyourls'), $code)); } // get the body $body = wp_remote_retrieve_body($build); // bail on empty body if (empty($body)) { return array('success' => false, 'errcode' => 'EMPTY_BODY', 'message' => __('No data was present in the body from the API call.', 'wpyourls')); } // if we do not want it decoded, return as is if (empty($decode)) { return array('success' => true, 'errcode' => null, 'data' => $body); } // decode the JSON $data = json_decode($body, true); // bail on empty JSON if (empty($data)) { return array('success' => false, 'errcode' => 'EMPTY_JSON', 'message' => __('The JSON could not be parsed.', 'wpyourls')); } // return the decoded data return array('success' => true, 'errcode' => null, 'data' => $data); }
/** * 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); }
/** * run a daily test to make sure the API is available * * @return void */ public function yourls_test_cron() { // bail if the API key or URL have not been entered if (false === ($api = YOURLSCreator_Helper::get_yourls_api_data())) { return; } // make the API call $build = YOURLSCreator_Helper::run_yourls_api_call('db-stats', array(), false); // handle the check and set it $check = !empty($build) && false !== $build['success'] ? 'connect' : 'noconnect'; // set the option return if (false !== get_option('yourls_api_test')) { update_option('yourls_api_test', $check); } else { add_option('yourls_api_test', $check, null, 'no'); } }