| | 1142 | * Create a new term. |
| | 1143 | * |
| | 1144 | * @uses wp_insert_term() |
| | 1145 | * @param array $args Method parameters. Contains: |
| | 1146 | * - int $blog_id |
| | 1147 | * - string $username |
| | 1148 | * - string $password |
| | 1149 | * - array $content_struct |
| | 1150 | * The $content_struct must contain: |
| | 1151 | * - 'name' |
| | 1152 | * - 'taxonomy' |
| | 1153 | * Also, it can optionally contain: |
| | 1154 | * - 'parent' |
| | 1155 | * - 'description' |
| | 1156 | * - 'slug' |
| | 1157 | * @return string term_id |
| | 1158 | */ |
| | 1159 | function wp_newTerm( $args ) { |
| | 1160 | $this->escape( $args ); |
| | 1161 | |
| | 1162 | $blog_id = (int) $args[0]; |
| | 1163 | $username = $args[1]; |
| | 1164 | $password = $args[2]; |
| | 1165 | $content_struct = $args[3]; |
| | 1166 | |
| | 1167 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1168 | return $this->error; |
| | 1169 | |
| | 1170 | do_action( 'xmlrpc_call', 'wp.newTerm' ); |
| | 1171 | |
| | 1172 | if ( ! taxonomy_exists( $content_struct['taxonomy'] ) ) |
| | 1173 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 1174 | |
| | 1175 | $taxonomy = get_taxonomy( $content_struct['taxonomy'] ); |
| | 1176 | |
| | 1177 | if ( ! current_user_can( $taxonomy->cap->manage_terms ) ) |
| | 1178 | return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) ); |
| | 1179 | |
| | 1180 | $taxonomy = (array) $taxonomy; |
| | 1181 | |
| | 1182 | // hold the data of the term |
| | 1183 | $term_data = array(); |
| | 1184 | |
| | 1185 | $term_data['name'] = trim( $content_struct['name'] ); |
| | 1186 | if ( empty( $term_data['name'] ) ) |
| | 1187 | return new IXR_Error( 403, __( 'The term name cannot be empty.' ) ); |
| | 1188 | |
| | 1189 | if ( isset( $content_struct['parent'] ) ) { |
| | 1190 | if ( ! $taxonomy['hierarchical'] ) |
| | 1191 | return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) ); |
| | 1192 | |
| | 1193 | $parent_term_id = (int) $content_struct['parent']; |
| | 1194 | $parent_term = get_term( $parent_term_id , $taxonomy['name'] ); |
| | 1195 | |
| | 1196 | if ( is_wp_error( $parent_term ) ) |
| | 1197 | return new IXR_Error( 500, $parent_term->get_error_message() ); |
| | 1198 | |
| | 1199 | if ( ! $parent_term ) |
| | 1200 | return new IXR_Error( 403, __( 'Parent term does not exist.' ) ); |
| | 1201 | |
| | 1202 | $term_data['parent'] = $content_struct['parent']; |
| | 1203 | } |
| | 1204 | |
| | 1205 | if ( isset( $content_struct['description'] ) ) |
| | 1206 | $term_data['description'] = $content_struct['description']; |
| | 1207 | |
| | 1208 | if ( isset( $content_struct['slug'] ) ) |
| | 1209 | $term_data['slug'] = $content_struct['slug']; |
| | 1210 | |
| | 1211 | $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data ); |
| | 1212 | |
| | 1213 | if ( is_wp_error( $term ) ) |
| | 1214 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 1215 | |
| | 1216 | if ( ! $term ) |
| | 1217 | return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) ); |
| | 1218 | |
| | 1219 | return strval( $term['term_id'] ); |
| | 1220 | } |
| | 1221 | |
| | 1222 | /** |
| | 1223 | * Edit a term. |
| | 1224 | * |
| | 1225 | * @uses wp_update_term() |
| | 1226 | * @param array $args Method parameters. Contains: |
| | 1227 | * - int $blog_id |
| | 1228 | * - string $username |
| | 1229 | * - string $password |
| | 1230 | * - string $term_id |
| | 1231 | * - array $content_struct |
| | 1232 | * The $content_struct must contain: |
| | 1233 | * - 'taxonomy' |
| | 1234 | * Also, it can optionally contain: |
| | 1235 | * - 'name' |
| | 1236 | * - 'parent' |
| | 1237 | * - 'description' |
| | 1238 | * - 'slug' |
| | 1239 | * @return bool True, on success. |
| | 1240 | */ |
| | 1241 | function wp_editTerm( $args ) { |
| | 1242 | $this->escape( $args ); |
| | 1243 | |
| | 1244 | $blog_id = (int) $args[0]; |
| | 1245 | $username = $args[1]; |
| | 1246 | $password = $args[2]; |
| | 1247 | $term_id = (int) $args[3]; |
| | 1248 | $content_struct = $args[4]; |
| | 1249 | |
| | 1250 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1251 | return $this->error; |
| | 1252 | |
| | 1253 | do_action( 'xmlrpc_call', 'wp.editTerm' ); |
| | 1254 | |
| | 1255 | if ( ! taxonomy_exists( $content_struct['taxonomy'] ) ) |
| | 1256 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 1257 | |
| | 1258 | $taxonomy = get_taxonomy( $content_struct['taxonomy'] ); |
| | 1259 | |
| | 1260 | if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) |
| | 1261 | return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) ); |
| | 1262 | |
| | 1263 | $taxonomy = (array) $taxonomy; |
| | 1264 | |
| | 1265 | // hold the data of the term |
| | 1266 | $term_data = array(); |
| | 1267 | |
| | 1268 | $term = get_term( $term_id , $content_struct['taxonomy'] ); |
| | 1269 | |
| | 1270 | if ( is_wp_error( $term ) ) |
| | 1271 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 1272 | |
| | 1273 | if ( ! $term ) |
| | 1274 | return new IXR_Error( 404, __( 'Invalid term ID.' ) ); |
| | 1275 | |
| | 1276 | if ( isset( $content_struct['name'] ) ) { |
| | 1277 | $term_data['name'] = trim( $content_struct['name'] ); |
| | 1278 | |
| | 1279 | if ( empty( $term_data['name'] ) ) |
| | 1280 | return new IXR_Error( 403, __( 'The term name cannot be empty.' ) ); |
| | 1281 | } |
| | 1282 | |
| | 1283 | if ( isset( $content_struct['parent'] ) ) { |
| | 1284 | if ( ! $taxonomy['hierarchical'] ) |
| | 1285 | return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) ); |
| | 1286 | |
| | 1287 | $parent_term_id = (int) $content_struct['parent']; |
| | 1288 | $parent_term = get_term( $parent_term_id , $taxonomy['name'] ); |
| | 1289 | |
| | 1290 | if ( is_wp_error( $parent_term ) ) |
| | 1291 | return new IXR_Error( 500, $parent_term->get_error_message() ); |
| | 1292 | |
| | 1293 | if ( ! $parent_term ) |
| | 1294 | return new IXR_Error( 403, __( 'Parent term does not exist.' ) ); |
| | 1295 | |
| | 1296 | $term_data['parent'] = $content_struct['parent']; |
| | 1297 | } |
| | 1298 | |
| | 1299 | if ( isset( $content_struct['description'] ) ) |
| | 1300 | $term_data['description'] = $content_struct['description']; |
| | 1301 | |
| | 1302 | if ( isset( $content_struct['slug'] ) ) |
| | 1303 | $term_data['slug'] = $content_struct['slug']; |
| | 1304 | |
| | 1305 | $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data ); |
| | 1306 | |
| | 1307 | if ( is_wp_error( $term ) ) |
| | 1308 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 1309 | |
| | 1310 | if ( ! $term ) |
| | 1311 | return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) ); |
| | 1312 | |
| | 1313 | return true; |
| | 1314 | } |
| | 1315 | |
| | 1316 | /** |
| | 1317 | * Delete a term. |
| | 1318 | * |
| | 1319 | * @uses wp_delete_term() |
| | 1320 | * @param array $args Method parameters. Contains: |
| | 1321 | * - int $blog_id |
| | 1322 | * - string $username |
| | 1323 | * - string $password |
| | 1324 | * - string $taxnomy_name |
| | 1325 | * - string $term_id |
| | 1326 | * @return boolean|IXR_Error If it suceeded true else a reason why not |
| | 1327 | */ |
| | 1328 | function wp_deleteTerm( $args ) { |
| | 1329 | $this->escape( $args ); |
| | 1330 | |
| | 1331 | $blog_id = (int) $args[0]; |
| | 1332 | $username = $args[1]; |
| | 1333 | $password = $args[2]; |
| | 1334 | $taxonomy_name = $args[3]; |
| | 1335 | $term_id = (int) $args[4]; |
| | 1336 | |
| | 1337 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1338 | return $this->error; |
| | 1339 | |
| | 1340 | do_action( 'xmlrpc_call', 'wp.deleteTerm' ); |
| | 1341 | |
| | 1342 | if ( ! taxonomy_exists( $taxonomy_name ) ) |
| | 1343 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 1344 | |
| | 1345 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| | 1346 | |
| | 1347 | if ( ! current_user_can( $taxonomy->cap->delete_terms ) ) |
| | 1348 | return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) ); |
| | 1349 | |
| | 1350 | $term = get_term( $term_id, $taxonomy_name ); |
| | 1351 | |
| | 1352 | if ( is_wp_error( $term ) ) |
| | 1353 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 1354 | |
| | 1355 | if ( ! $term ) |
| | 1356 | return new IXR_Error( 404, __( 'Invalid term ID.' ) ); |
| | 1357 | |
| | 1358 | $result = wp_delete_term( $term_id, $taxonomy_name ); |
| | 1359 | |
| | 1360 | if ( is_wp_error( $result ) ) |
| | 1361 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 1362 | |
| | 1363 | if ( ! $result ) |
| | 1364 | return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) ); |
| | 1365 | |
| | 1366 | return $result; |
| | 1367 | } |
| | 1368 | |
| | 1369 | /** |
| | 1370 | * Retrieve a term. |
| | 1371 | * |
| | 1372 | * @uses get_term() |
| | 1373 | * @param array $args Method parameters. Contains: |
| | 1374 | * - int $blog_id |
| | 1375 | * - string $username |
| | 1376 | * - string $password |
| | 1377 | * - string $taxonomy_name |
| | 1378 | * - string $term_id |
| | 1379 | * @return array contains: |
| | 1380 | * - 'term_id' |
| | 1381 | * - 'name' |
| | 1382 | * - 'slug' |
| | 1383 | * - 'term_group' |
| | 1384 | * - 'term_taxonomy_id' |
| | 1385 | * - 'taxonomy' |
| | 1386 | * - 'description' |
| | 1387 | * - 'parent' |
| | 1388 | * - 'count' |
| | 1389 | */ |
| | 1390 | function wp_getTerm( $args ) { |
| | 1391 | $this->escape( $args ); |
| | 1392 | |
| | 1393 | $blog_id = (int) $args[0]; |
| | 1394 | $username = $args[1]; |
| | 1395 | $password = $args[2]; |
| | 1396 | $taxonomy_name = $args[3]; |
| | 1397 | $term_id = (int) $args[4]; |
| | 1398 | |
| | 1399 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1400 | return $this->error; |
| | 1401 | |
| | 1402 | do_action( 'xmlrpc_call', 'wp.getTerm' ); |
| | 1403 | |
| | 1404 | if ( ! taxonomy_exists( $taxonomy_name ) ) |
| | 1405 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 1406 | |
| | 1407 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| | 1408 | |
| | 1409 | if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) |
| | 1410 | return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) ); |
| | 1411 | |
| | 1412 | $term = get_term( $term_id , $taxonomy_name ); |
| | 1413 | |
| | 1414 | if ( is_wp_error( $term ) ) |
| | 1415 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 1416 | |
| | 1417 | if ( ! $term ) |
| | 1418 | return new IXR_Error( 404, __( 'Invalid term ID.' ) ); |
| | 1419 | |
| | 1420 | return $this->_prepare_term( $term ); |
| | 1421 | } |
| | 1422 | |
| | 1423 | /** |
| | 1424 | * Retrieve all terms for a taxonomy. |
| | 1425 | * |
| | 1426 | * @uses get_terms() |
| | 1427 | * @param array $args Method parameters. Contains: |
| | 1428 | * - int $blog_id |
| | 1429 | * - string $username |
| | 1430 | * - string $password |
| | 1431 | * - string $taxonomy_name |
| | 1432 | * @return array terms |
| | 1433 | */ |
| | 1434 | function wp_getTerms( $args ) { |
| | 1435 | $this->escape( $args ); |
| | 1436 | |
| | 1437 | $blog_id = (int) $args[0]; |
| | 1438 | $username = $args[1]; |
| | 1439 | $password = $args[2]; |
| | 1440 | $taxonomy_name = $args[3]; |
| | 1441 | |
| | 1442 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1443 | return $this->error; |
| | 1444 | |
| | 1445 | do_action( 'xmlrpc_call', 'wp.getTerms' ); |
| | 1446 | |
| | 1447 | if ( ! taxonomy_exists( $taxonomy_name ) ) |
| | 1448 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 1449 | |
| | 1450 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| | 1451 | |
| | 1452 | if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) |
| | 1453 | return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) ); |
| | 1454 | |
| | 1455 | $terms = get_terms( $taxonomy_name , array( 'get' => 'all' ) ); |
| | 1456 | |
| | 1457 | if ( is_wp_error( $terms ) ) |
| | 1458 | return new IXR_Error( 500, $terms->get_error_message() ); |
| | 1459 | |
| | 1460 | $struct = array(); |
| | 1461 | |
| | 1462 | foreach ( $terms as $term ) { |
| | 1463 | $struct[] = $this->_prepare_term( $term ); |
| | 1464 | } |
| | 1465 | |
| | 1466 | return $struct; |
| | 1467 | } |
| | 1468 | |
| | 1469 | /** |
| | 1470 | * Retrieve a taxonomy. |
| | 1471 | * |
| | 1472 | * @uses get_taxonomy() |
| | 1473 | * @param array $args Method parameters. Contains: |
| | 1474 | * - int $blog_id |
| | 1475 | * - string $username |
| | 1476 | * - string $password |
| | 1477 | * - string $taxonomy_name |
| | 1478 | * @return array (@see get_taxonomy()) |
| | 1479 | */ |
| | 1480 | function wp_getTaxonomy( $args ) { |
| | 1481 | $this->escape( $args ); |
| | 1482 | |
| | 1483 | $blog_id = (int) $args[0]; |
| | 1484 | $username = $args[1]; |
| | 1485 | $password = $args[2]; |
| | 1486 | $taxonomy_name = $args[3]; |
| | 1487 | |
| | 1488 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1489 | return $this->error; |
| | 1490 | |
| | 1491 | do_action( 'xmlrpc_call', 'wp.getTaxonomy' ); |
| | 1492 | |
| | 1493 | if ( ! taxonomy_exists( $taxonomy_name ) ) |
| | 1494 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 1495 | |
| | 1496 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| | 1497 | |
| | 1498 | if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) |
| | 1499 | return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) ); |
| | 1500 | |
| | 1501 | return $this->_prepare_taxonomy( $taxonomy ); |
| | 1502 | } |
| | 1503 | |
| | 1504 | /** |
| | 1505 | * Retrieve all taxonomies. |
| | 1506 | * |
| | 1507 | * @uses get_taxonomies() |
| | 1508 | * @param array $args Method parameters. Contains: |
| | 1509 | * - int $blog_id |
| | 1510 | * - string $username |
| | 1511 | * - string $password |
| | 1512 | * @return array taxonomies |
| | 1513 | */ |
| | 1514 | function wp_getTaxonomies( $args ) { |
| | 1515 | $this->escape( $args ); |
| | 1516 | |
| | 1517 | $blog_id = (int) $args[0]; |
| | 1518 | $username = $args[1]; |
| | 1519 | $password = $args[2]; |
| | 1520 | |
| | 1521 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1522 | return $this->error; |
| | 1523 | |
| | 1524 | do_action( 'xmlrpc_call', 'wp.getTaxonomies' ); |
| | 1525 | |
| | 1526 | $taxonomies = get_taxonomies( '', 'objects' ); |
| | 1527 | |
| | 1528 | // holds all the taxonomy data |
| | 1529 | $struct = array(); |
| | 1530 | |
| | 1531 | foreach ( $taxonomies as $taxonomy ) { |
| | 1532 | // capability check for post_types |
| | 1533 | if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) |
| | 1534 | continue; |
| | 1535 | |
| | 1536 | $struct[] = $this->_prepare_taxonomy( $taxonomy ); |
| | 1537 | } |
| | 1538 | |
| | 1539 | return $struct; |
| | 1540 | } |
| | 1541 | |
| | 1542 | /** |