Permalink
Browse files

Begin the dissolution of classes.php. see #10287.

git-svn-id: http://svn.automattic.com/wordpress/trunk@16099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information...
1 parent 244216d commit 731bf1fb066950c0c17400bc3e6ee5ddfc3f9a92 nacin committed Oct 30, 2010
@@ -42,7 +42,7 @@
require_once(ABSPATH . WPINC . '/load.php');
require_once(ABSPATH . WPINC . '/compat.php');
require_once(ABSPATH . WPINC . '/functions.php');
-require_once(ABSPATH . WPINC . '/classes.php');
+require_once(ABSPATH . WPINC . '/class-wp-error.php');
require_once(ABSPATH . WPINC . '/version.php');
if (!file_exists(ABSPATH . 'wp-config-sample.php'))
@@ -0,0 +1,149 @@
+<?php
+/**
+ * Holds Most of the WordPress classes.
+ *
+ * Some of the other classes are contained in other files. For example, the
+ * WordPress cache is in cache.php and the WordPress roles API is in
+ * capabilities.php. The third party libraries are contained in their own
+ * separate files.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Send XML response back to AJAX request.
+ *
+ * @package WordPress
+ * @since 2.1.0
+ */
+class WP_Ajax_Response {
+ /**
+ * Store XML responses to send.
+ *
+ * @since 2.1.0
+ * @var array
+ * @access private
+ */
+ var $responses = array();
+
+ /**
+ * PHP4 Constructor - Passes args to {@link WP_Ajax_Response::add()}.
+ *
+ * @since 2.1.0
+ * @see WP_Ajax_Response::add()
+ *
+ * @param string|array $args Optional. Will be passed to add() method.
+ * @return WP_Ajax_Response
+ */
+ function WP_Ajax_Response( $args = '' ) {
+ if ( !empty($args) )
+ $this->add($args);
+ }
+
+ /**
+ * Append to XML response based on given arguments.
+ *
+ * The arguments that can be passed in the $args parameter are below. It is
+ * also possible to pass a WP_Error object in either the 'id' or 'data'
+ * argument. The parameter isn't actually optional, content should be given
+ * in order to send the correct response.
+ *
+ * 'what' argument is a string that is the XMLRPC response type.
+ * 'action' argument is a boolean or string that acts like a nonce.
+ * 'id' argument can be WP_Error or an integer.
+ * 'old_id' argument is false by default or an integer of the previous ID.
+ * 'position' argument is an integer or a string with -1 = top, 1 = bottom,
+ * html ID = after, -html ID = before.
+ * 'data' argument is a string with the content or message.
+ * 'supplemental' argument is an array of strings that will be children of
+ * the supplemental element.
+ *
+ * @since 2.1.0
+ *
+ * @param string|array $args Override defaults.
+ * @return string XML response.
+ */
+ function add( $args = '' ) {
+ $defaults = array(
+ 'what' => 'object', 'action' => false,
+ 'id' => '0', 'old_id' => false,
+ 'position' => 1,
+ 'data' => '', 'supplemental' => array()
+ );
+
+ $r = wp_parse_args( $args, $defaults );
+ extract( $r, EXTR_SKIP );
+ $position = preg_replace( '/[^a-z0-9:_-]/i', '', $position );
+
+ if ( is_wp_error($id) ) {
+ $data = $id;
+ $id = 0;
+ }
+
+ $response = '';
+ if ( is_wp_error($data) ) {
+ foreach ( (array) $data->get_error_codes() as $code ) {
+ $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>";
+ if ( !$error_data = $data->get_error_data($code) )
+ continue;
+ $class = '';
+ if ( is_object($error_data) ) {
+ $class = ' class="' . get_class($error_data) . '"';
+ $error_data = get_object_vars($error_data);
+ }
+
+ $response .= "<wp_error_data code='$code'$class>";
+
+ if ( is_scalar($error_data) ) {
+ $response .= "<![CDATA[$error_data]]>";
+ } elseif ( is_array($error_data) ) {
+ foreach ( $error_data as $k => $v )
+ $response .= "<$k><![CDATA[$v]]></$k>";
+ }
+
+ $response .= "</wp_error_data>";
+ }
+ } else {
+ $response = "<response_data><![CDATA[$data]]></response_data>";
+ }
+
+ $s = '';
+ if ( is_array($supplemental) ) {
+ foreach ( $supplemental as $k => $v )
+ $s .= "<$k><![CDATA[$v]]></$k>";
+ $s = "<supplemental>$s</supplemental>";
+ }
+
+ if ( false === $action )
+ $action = $_POST['action'];
+
+ $x = '';
+ $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
+ $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
+ $x .= $response;
+ $x .= $s;
+ $x .= "</$what>";
+ $x .= "</response>";
+
+ $this->responses[] = $x;
+ return $x;
+ }
+
+ /**
+ * Display XML formatted responses.
+ *
+ * Sets the content type header to text/xml.
+ *
+ * @since 2.1.0
+ */
+ function send() {
+ header('Content-Type: text/xml');
+ echo "<?xml version='1.0' standalone='yes'?><wp_ajax>";
+ foreach ( (array) $this->responses as $response )
+ echo $response;
+ echo '</wp_ajax>';
+ die();
+ }
+}
+
+?>
@@ -0,0 +1,215 @@
+<?php
+/**
+ * Holds Most of the WordPress classes.
+ *
+ * Some of the other classes are contained in other files. For example, the
+ * WordPress cache is in cache.php and the WordPress roles API is in
+ * capabilities.php. The third party libraries are contained in their own
+ * separate files.
+ *
+ * @package WordPress
+ */
+
+/**
+ * WordPress Error class.
+ *
+ * Container for checking for WordPress errors and error messages. Return
+ * WP_Error and use {@link is_wp_error()} to check if this class is returned.
+ * Many core WordPress functions pass this class in the event of an error and
+ * if not handled properly will result in code errors.
+ *
+ * @package WordPress
+ * @since 2.1.0
+ */
+class WP_Error {
+ /**
+ * Stores the list of errors.
+ *
+ * @since 2.1.0
+ * @var array
+ * @access private
+ */
+ var $errors = array();
+
+ /**
+ * Stores the list of data for error codes.
+ *
+ * @since 2.1.0
+ * @var array
+ * @access private
+ */
+ var $error_data = array();
+
+ /**
+ * PHP4 Constructor - Sets up error message.
+ *
+ * If code parameter is empty then nothing will be done. It is possible to
+ * add multiple messages to the same code, but with other methods in the
+ * class.
+ *
+ * All parameters are optional, but if the code parameter is set, then the
+ * data parameter is optional.
+ *
+ * @since 2.1.0
+ *
+ * @param string|int $code Error code
+ * @param string $message Error message
+ * @param mixed $data Optional. Error data.
+ * @return WP_Error
+ */
+ function WP_Error($code = '', $message = '', $data = '') {
+ if ( empty($code) )
+ return;
+
+ $this->errors[$code][] = $message;
+
+ if ( ! empty($data) )
+ $this->error_data[$code] = $data;
+ }
+
+ /**
+ * Retrieve all error codes.
+ *
+ * @since 2.1.0
+ * @access public
+ *
+ * @return array List of error codes, if avaiable.
+ */
+ function get_error_codes() {
+ if ( empty($this->errors) )
+ return array();
+
+ return array_keys($this->errors);
+ }
+
+ /**
+ * Retrieve first error code available.
+ *
+ * @since 2.1.0
+ * @access public
+ *
+ * @return string|int Empty string, if no error codes.
+ */
+ function get_error_code() {
+ $codes = $this->get_error_codes();
+
+ if ( empty($codes) )
+ return '';
+
+ return $codes[0];
+ }
+
+ /**
+ * Retrieve all error messages or error messages matching code.
+ *
+ * @since 2.1.0
+ *
+ * @param string|int $code Optional. Retrieve messages matching code, if exists.
+ * @return array Error strings on success, or empty array on failure (if using codee parameter).
+ */
+ function get_error_messages($code = '') {
+ // Return all messages if no code specified.
+ if ( empty($code) ) {
+ $all_messages = array();
+ foreach ( (array) $this->errors as $code => $messages )
+ $all_messages = array_merge($all_messages, $messages);
+
+ return $all_messages;
+ }
+
+ if ( isset($this->errors[$code]) )
+ return $this->errors[$code];
+ else
+ return array();
+ }
+
+ /**
+ * Get single error message.
+ *
+ * This will get the first message available for the code. If no code is
+ * given then the first code available will be used.
+ *
+ * @since 2.1.0
+ *
+ * @param string|int $code Optional. Error code to retrieve message.
+ * @return string
+ */
+ function get_error_message($code = '') {
+ if ( empty($code) )
+ $code = $this->get_error_code();
+ $messages = $this->get_error_messages($code);
+ if ( empty($messages) )
+ return '';
+ return $messages[0];
+ }
+
+ /**
+ * Retrieve error data for error code.
+ *
+ * @since 2.1.0
+ *
+ * @param string|int $code Optional. Error code.
+ * @return mixed Null, if no errors.
+ */
+ function get_error_data($code = '') {
+ if ( empty($code) )
+ $code = $this->get_error_code();
+
+ if ( isset($this->error_data[$code]) )
+ return $this->error_data[$code];
+ return null;
+ }
+
+ /**
+ * Append more error messages to list of error messages.
+ *
+ * @since 2.1.0
+ * @access public
+ *
+ * @param string|int $code Error code.
+ * @param string $message Error message.
+ * @param mixed $data Optional. Error data.
+ */
+ function add($code, $message, $data = '') {
+ $this->errors[$code][] = $message;
+ if ( ! empty($data) )
+ $this->error_data[$code] = $data;
+ }
+
+ /**
+ * Add data for error code.
+ *
+ * The error code can only contain one error data.
+ *
+ * @since 2.1.0
+ *
+ * @param mixed $data Error data.
+ * @param string|int $code Error code.
+ */
+ function add_data($data, $code = '') {
+ if ( empty($code) )
+ $code = $this->get_error_code();
+
+ $this->error_data[$code] = $data;
+ }
+}
+
+/**
+ * Check whether variable is a WordPress Error.
+ *
+ * Looks at the object and if a WP_Error class. Does not check to see if the
+ * parent is also WP_Error, so can't inherit WP_Error and still use this
+ * function.
+ *
+ * @since 2.1.0
+ *
+ * @param mixed $thing Check if unknown variable is WordPress Error object.
+ * @return bool True, if WP_Error. False, if not WP_Error.
+ */
+function is_wp_error($thing) {
+ if ( is_object($thing) && is_a($thing, 'WP_Error') )
+ return true;
+ return false;
+}
+
+?>
Oops, something went wrong.

0 comments on commit 731bf1f

Please sign in to comment.