| 1 |
<?php |
| 2 |
|
| 3 |
class Mmb_Page extends Mmb_Core |
| 4 |
{ |
| 5 |
function __construct() |
| 6 |
{ |
| 7 |
parent::__construct(); |
| 8 |
} |
| 9 |
|
| 10 |
/************************************************************* |
| 11 |
* FACADE functions |
| 12 |
* (functions to be called after a remote XMLRPC from Master) |
| 13 |
**************************************************************/ |
| 14 |
function get_edit_data($args) |
| 15 |
{ |
| 16 |
$this->_escape($args); |
| 17 |
$username = $args[0]; |
| 18 |
$password = $args[1]; |
| 19 |
$page_id = $args[2]; |
| 20 |
|
| 21 |
if (!$user = $this->login($username, $password)) |
| 22 |
{ |
| 23 |
return $this->error; |
| 24 |
} |
| 25 |
|
| 26 |
if (!current_user_can('edit_page', $page_id)) |
| 27 |
return new IXR_Error(401, 'You are not allowed to edit this page.'); |
| 28 |
|
| 29 |
$tmp_pages = get_pages("exclude=$page_id"); |
| 30 |
|
| 31 |
|
| 32 |
// trim off redundant data |
| 33 |
$parents = array(); |
| 34 |
foreach ($tmp_pages as $tmp_page) |
| 35 |
{ |
| 36 |
$parents[] = array( |
| 37 |
'ID' => $tmp_page->ID, |
| 38 |
'title' => $tmp_page->post_title, |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
$page = get_page($page_id, OBJECT, 'edit'); |
| 43 |
// $this->_log($page); |
| 44 |
|
| 45 |
$tmp_custom = get_post_custom($page_id); |
| 46 |
$custom = array(); |
| 47 |
if(is_array($tmp_custom)) |
| 48 |
foreach ($tmp_custom as $key => $value_array){ |
| 49 |
if ('_wp_page_template' == $key) |
| 50 |
{ |
| 51 |
// the page template! important!!! |
| 52 |
$page->template = $value_array[0]; |
| 53 |
} |
| 54 |
|
| 55 |
if ('_' == $key[0]) continue; |
| 56 |
foreach ($value_array as $value) |
| 57 |
{ |
| 58 |
$custom[$key][] = base64_encode($value); // keep the new lines |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
// encode the page content and excerpt to keep the new lines |
| 64 |
// as they would be trimmed off during XMLRPC |
| 65 |
$page->post_content = base64_encode($page->post_content); |
| 66 |
$page->post_excerpt = base64_encode($page->post_excerpt); |
| 67 |
|
| 68 |
// visibility |
| 69 |
if ('private' == $page->post_status) |
| 70 |
{ |
| 71 |
$page->post_password = ''; |
| 72 |
$page->visibility = 'private'; |
| 73 |
} |
| 74 |
elseif (!empty($page->post_password)) |
| 75 |
{ |
| 76 |
$page->visibility = 'password'; |
| 77 |
} |
| 78 |
else |
| 79 |
{ |
| 80 |
$page->visibility = 'public'; |
| 81 |
} |
| 82 |
|
| 83 |
$templates = get_page_templates(); |
| 84 |
if (empty($templates)) $templates = array(); |
| 85 |
|
| 86 |
return array( |
| 87 |
'page' => $page, |
| 88 |
'parents' => $parents, |
| 89 |
'custom_fields' => $custom, |
| 90 |
'templates' => $templates, |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Gets necessary local data for page creation |
| 96 |
* |
| 97 |
* @param mixed $args |
| 98 |
* @return IXR_Error |
| 99 |
*/ |
| 100 |
function get_new_data($args) |
| 101 |
{ |
| 102 |
$this->_escape($args); |
| 103 |
$username = $args[0]; |
| 104 |
$password = $args[1]; |
| 105 |
|
| 106 |
if (!$user = $this->login($username, $password)) |
| 107 |
{ |
| 108 |
return $this->error; |
| 109 |
} |
| 110 |
|
| 111 |
if (!current_user_can('edit_pages')) |
| 112 |
return new IXR_Error(401, 'You are not allowed to create a new page.'); |
| 113 |
|
| 114 |
$parents = array(); |
| 115 |
foreach ((array)get_pages() as $tmp_page) |
| 116 |
{ |
| 117 |
$parents[] = array( |
| 118 |
'ID' => $tmp_page->ID, |
| 119 |
'title' => $tmp_page->post_title, |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
$templates = get_page_templates(); |
| 124 |
if (empty($templates)) $templates = array(); |
| 125 |
|
| 126 |
$page = get_default_page_to_edit(); |
| 127 |
|
| 128 |
// some modifications to the default WordPress data |
| 129 |
$page->post_date = date('Y-m-d H:i:s'); |
| 130 |
$page->post_status = 'publish'; |
| 131 |
|
| 132 |
return array( |
| 133 |
'page' => $page, |
| 134 |
'parents' => $parents, |
| 135 |
'custom_fields' => array(), // default is an empty array |
| 136 |
'templates' => $templates, |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Locally updates a page |
| 142 |
* |
| 143 |
* @param mixed $args |
| 144 |
*/ |
| 145 |
function update($args) |
| 146 |
{ |
| 147 |
$this->_escape($args); |
| 148 |
$username = $args[0]; |
| 149 |
$password = $args[1]; |
| 150 |
$page_data = unserialize(base64_decode($args[2])); |
| 151 |
|
| 152 |
if (!$user = $this->login($username, $password)) |
| 153 |
{ |
| 154 |
return $this->error; |
| 155 |
} |
| 156 |
|
| 157 |
if (!current_user_can('edit_page', $page_data['post_ID'])) |
| 158 |
return new IXR_Error(401, 'You are not allowed to edit this page.'); |
| 159 |
|
| 160 |
// wp_update_post needs ID key |
| 161 |
$page_data['ID'] = $page_data['post_ID']; |
| 162 |
|
| 163 |
// wrap the function inside an output buffer to prevent errors from printed |
| 164 |
ob_start(); |
| 165 |
$custom_fields = get_post_custom($page_data['ID']); |
| 166 |
foreach ($custom_fields as $key => $value) |
| 167 |
{ |
| 168 |
delete_post_meta($page_data['ID'], $key); |
| 169 |
} |
| 170 |
|
| 171 |
$result = edit_post($page_data); |
| 172 |
foreach ($page_data['meta'] as $id => $meta) |
| 173 |
{ |
| 174 |
add_post_meta($page_data['ID'], $meta['key'], $meta['value']); |
| 175 |
} |
| 176 |
ob_end_clean(); |
| 177 |
|
| 178 |
if ($result) |
| 179 |
{ |
| 180 |
return 'Success'; |
| 181 |
} |
| 182 |
|
| 183 |
return new IXR_Error(401, 'Failed to update the page.'); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Locally creates a page |
| 188 |
* |
| 189 |
* @param mixed $args |
| 190 |
*/ |
| 191 |
function create($args) |
| 192 |
{ |
| 193 |
$this->_escape($args); |
| 194 |
$username = $args[0]; |
| 195 |
$password = $args[1]; |
| 196 |
|
| 197 |
if (!$user = $this->login($username, $password)) |
| 198 |
{ |
| 199 |
return $this->error; |
| 200 |
} |
| 201 |
|
| 202 |
if (!current_user_can('edit_pages')) |
| 203 |
return new IXR_Error(401, 'You are not allowed to create a new page.'); |
| 204 |
|
| 205 |
$page_struct = unserialize(base64_decode($args[2])); |
| 206 |
$page_data = $page_struct['post_data']; |
| 207 |
$page_meta = $page_struct['post_extras']['post_meta']; |
| 208 |
|
| 209 |
//create post |
| 210 |
$page_id = wp_insert_post($page_data); |
| 211 |
if($page_id){ |
| 212 |
//get current custom fields |
| 213 |
$cur_custom = get_post_custom($page_id); |
| 214 |
//check which values doesnot exists in new custom fields |
| 215 |
$diff_values = array_diff_key($cur_custom, $page_meta); |
| 216 |
if(is_array($diff_values)) |
| 217 |
foreach ($diff_values as $meta_key => $value) { |
| 218 |
delete_post_meta($page_id, $meta_key); |
| 219 |
} |
| 220 |
|
| 221 |
//insert new post meta |
| 222 |
foreach($page_meta as $key => $value){ |
| 223 |
if(strpos($key, '_mmb') === 0 || strpos($key, '_edit') === 0) |
| 224 |
continue; |
| 225 |
update_post_meta($page_id, $key, $value[0]); |
| 226 |
} |
| 227 |
} |
| 228 |
return $page_id; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
// wrap the function inside an output buffer to prevent errors from printed |
| 233 |
// ob_start(); |
| 234 |
// $_POST = $page_data; |
| 235 |
// $post_ID = wp_write_post(); // this function gets data from $_POST |
| 236 |
// if (is_wp_error($post_ID)) |
| 237 |
// { |
| 238 |
// return new IXR_Error(401, 'Failed to create the page. ' . $result->get_error_message()); |
| 239 |
// } |
| 240 |
// |
| 241 |
// if (empty($post_ID)) |
| 242 |
// { |
| 243 |
// return new IXR_Error(401, 'Failed to create the page: Unknown error.'); |
| 244 |
// } |
| 245 |
// |
| 246 |
// foreach ($page_data['meta'] as $id => $meta) |
| 247 |
// { |
| 248 |
// add_post_meta($post_ID, $meta['key'], $meta['value']); |
| 249 |
// } |
| 250 |
// ob_end_clean(); |
| 251 |
// |
| 252 |
// if ($post_ID) |
| 253 |
// { |
| 254 |
// return 'Success'; |
| 255 |
// } |
| 256 |
// |
| 257 |
// return new IXR_Error(401, 'Failed to create the page.'); |
| 258 |
} |
| 259 |
} |
| 260 |
|