50 * @since 2.1.0 |
48 * @since 2.1.0 |
51 * |
49 * |
52 * @param string|array $args Override defaults. |
50 * @param string|array $args Override defaults. |
53 * @return string XML response. |
51 * @return string XML response. |
54 */ |
52 */ |
55 function add( $args = '' ) { |
53 public function add( $args = '' ) { |
56 $defaults = array( |
54 $defaults = array( |
57 'what' => 'object', 'action' => false, |
55 'what' => 'object', 'action' => false, |
58 'id' => '0', 'old_id' => false, |
56 'id' => '0', 'old_id' => false, |
59 'position' => 1, |
57 'position' => 1, |
60 'data' => '', 'supplemental' => array() |
58 'data' => '', 'supplemental' => array() |
61 ); |
59 ); |
62 |
60 |
63 $r = wp_parse_args( $args, $defaults ); |
61 $r = wp_parse_args( $args, $defaults ); |
64 extract( $r, EXTR_SKIP ); |
|
65 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $position ); |
|
66 |
62 |
67 if ( is_wp_error($id) ) { |
63 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] ); |
|
64 $id = $r['id']; |
|
65 $what = $r['what']; |
|
66 $action = $r['action']; |
|
67 $old_id = $r['old_id']; |
|
68 $data = $r['data']; |
|
69 |
|
70 if ( is_wp_error( $id ) ) { |
68 $data = $id; |
71 $data = $id; |
69 $id = 0; |
72 $id = 0; |
70 } |
73 } |
71 |
74 |
72 $response = ''; |
75 $response = ''; |
73 if ( is_wp_error($data) ) { |
76 if ( is_wp_error( $data ) ) { |
74 foreach ( (array) $data->get_error_codes() as $code ) { |
77 foreach ( (array) $data->get_error_codes() as $code ) { |
75 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>"; |
78 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . "]]></wp_error>"; |
76 if ( !$error_data = $data->get_error_data($code) ) |
79 if ( ! $error_data = $data->get_error_data( $code ) ) { |
77 continue; |
80 continue; |
|
81 } |
78 $class = ''; |
82 $class = ''; |
79 if ( is_object($error_data) ) { |
83 if ( is_object( $error_data ) ) { |
80 $class = ' class="' . get_class($error_data) . '"'; |
84 $class = ' class="' . get_class( $error_data ) . '"'; |
81 $error_data = get_object_vars($error_data); |
85 $error_data = get_object_vars( $error_data ); |
82 } |
86 } |
83 |
87 |
84 $response .= "<wp_error_data code='$code'$class>"; |
88 $response .= "<wp_error_data code='$code'$class>"; |
85 |
89 |
86 if ( is_scalar($error_data) ) { |
90 if ( is_scalar( $error_data ) ) { |
87 $response .= "<![CDATA[$error_data]]>"; |
91 $response .= "<![CDATA[$error_data]]>"; |
88 } elseif ( is_array($error_data) ) { |
92 } elseif ( is_array( $error_data ) ) { |
89 foreach ( $error_data as $k => $v ) |
93 foreach ( $error_data as $k => $v ) { |
90 $response .= "<$k><![CDATA[$v]]></$k>"; |
94 $response .= "<$k><![CDATA[$v]]></$k>"; |
|
95 } |
91 } |
96 } |
92 |
97 |
93 $response .= "</wp_error_data>"; |
98 $response .= "</wp_error_data>"; |
94 } |
99 } |
95 } else { |
100 } else { |
96 $response = "<response_data><![CDATA[$data]]></response_data>"; |
101 $response = "<response_data><![CDATA[$data]]></response_data>"; |
97 } |
102 } |
98 |
103 |
99 $s = ''; |
104 $s = ''; |
100 if ( is_array($supplemental) ) { |
105 if ( is_array( $r['supplemental'] ) ) { |
101 foreach ( $supplemental as $k => $v ) |
106 foreach ( $r['supplemental'] as $k => $v ) { |
102 $s .= "<$k><![CDATA[$v]]></$k>"; |
107 $s .= "<$k><![CDATA[$v]]></$k>"; |
|
108 } |
103 $s = "<supplemental>$s</supplemental>"; |
109 $s = "<supplemental>$s</supplemental>"; |
104 } |
110 } |
105 |
111 |
106 if ( false === $action ) |
112 if ( false === $action ) { |
107 $action = $_POST['action']; |
113 $action = $_POST['action']; |
108 |
114 } |
109 $x = ''; |
115 $x = ''; |
110 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action |
116 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action |
111 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; |
117 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; |
112 $x .= $response; |
118 $x .= $response; |
113 $x .= $s; |
119 $x .= $s; |