/** * 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); }
/** * call the metabox if on an appropriate * post type and post status * * @return [type] [description] */ public function yourls_metabox() { // fetch the global post object global $post; // make sure we're working with an approved post type if (!in_array($post->post_type, 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; } // only fire if user has the option if (false === ($check = YOURLSCreator_Helper::check_yourls_cap())) { return; } // now add the meta box add_meta_box('yourls-post-display', __('YOURLS Shortlink', 'wpyourls'), array(__CLASS__, 'yourls_post_display'), $post->post_type, 'side', 'high'); }